Use LiveData

Julien LepillerWed Jun 10 21:25:28+0200 2020

a9dc139

Use LiveData

app/src/main/java/eu/lepiller/nani/DictionaryDownloadService.java

1111
1212
import androidx.annotation.Nullable;
1313
import androidx.core.app.NotificationCompat;
14+
import androidx.lifecycle.LiveData;
15+
import androidx.lifecycle.MutableLiveData;
1416
1517
import java.io.File;
1618
import java.io.FileOutputStream;

3032
    private NotificationCompat.Builder builder;
3133
    private DownloadQueue downloadQueue;
3234
35+
    static public class DownloadData {
36+
        public String currentName;
37+
        public int currentProgress;
38+
        public ArrayList<String> downloading;
39+
40+
        DownloadData(String name, int progress, ArrayList<String> next) {
41+
            currentName = name;
42+
            currentProgress = progress;
43+
            downloading = next;
44+
        }
45+
    }
46+
47+
    private static MutableLiveData<DownloadData> data;
48+
3349
    public static final String DOWNLOAD_ACTION = "eu.lepiller.nani.action.DOWNLOAD";
3450
    public static final String PAUSE_ACTION = "eu.lepiller.nani.action.PAUSE";
3551
    private static final String TAG = "DicoDownloadService";
3652
53+
    public static LiveData<DownloadData> getData() {
54+
        if(data == null)
55+
            data = new MutableLiveData<>();
56+
57+
        return data;
58+
    }
59+
3760
    @Override
3861
    public void onCreate() {
3962
        Log.d(TAG, "onCreate");

4669
        Downloader downloadThread = new Downloader(new Runnable() {
4770
            @Override
4871
            public void run() {
72+
                stopForeground(true);
4973
                stopSelf();
5074
            }
5175
        });

7296
    @Override
7397
    public void onDestroy() {
7498
        Log.d(TAG, "onDestroy");
99+
        downloadQueue.clear();
100+
        updateNotification(0, null);
75101
        downloadQueue = null;
76102
        super.onDestroy();
77103
    }
78104
79105
    void updateNotification(int progress, String name) {
80-
        Intent notificationIntent = new Intent(this, DictionaryDownloadActivity.class);
81-
        notificationIntent.putExtra(DictionaryDownloadActivity.EXTRA_DICTIONARY, name);
82-
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
106+
        PendingIntent pendingIntent;
107+
        if(name == null) {
108+
            Intent notificationIntent = new Intent(this, DictionaryActivity.class);
109+
            pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
110+
            builder.setContentTitle(getString(R.string.downloading));
111+
        } else {
112+
            Intent notificationIntent = new Intent(this, DictionaryDownloadActivity.class);
113+
            notificationIntent.putExtra(DictionaryDownloadActivity.EXTRA_DICTIONARY, name);
114+
            pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
115+
            builder.setContentTitle(name);
116+
        }
83117
84-
        builder.setContentTitle(name)
85-
               .setContentIntent(pendingIntent)
118+
        builder.setContentIntent(pendingIntent)
86119
               .setProgress(100, progress, (progress < 0));
87120
        startForeground(1, builder.build());
121+
122+
        final DownloadData downloadData = new DownloadData(name, progress, downloadQueue.downloadQueue());
123+
124+
        Handler threadHandler = new Handler(Looper.getMainLooper());
125+
        threadHandler.post(new Runnable() {
126+
            @Override
127+
            public void run() {
128+
                if(data == null)
129+
                    data = new MutableLiveData<>();
130+
                data.setValue(downloadData);
131+
            }
132+
        });
88133
    }
89134
90135
    private static class DownloadQueue {

93138
        private boolean stopAsked = false;
94139
        private ReentrantLock mutex = new ReentrantLock();
95140
141+
        ArrayList<String> downloadQueue() {
142+
            try {
143+
                mutex.lock();
144+
                return new ArrayList<>(downloadQueue);
145+
            } finally {
146+
                mutex.unlock();
147+
            }
148+
        }
149+
150+
        void clear() {
151+
            try {
152+
                mutex.lock();
153+
                downloadQueue.clear();
154+
            } finally {
155+
                mutex.unlock();
156+
            }
157+
        }
158+
96159
        String popNextJob() {
97160
            try {
98161
                mutex.lock();

155218
                mutex.unlock();
156219
            }
157220
        }
158-
159-
        boolean isDownloading(String name) {
160-
            try {
161-
                mutex.lock();
162-
                for(String n: downloadQueue) {
163-
                    if(n.equals(name))
164-
                        return true;
165-
                }
166-
                return false;
167-
            } finally {
168-
                mutex.unlock();
169-
            }
170-
        }
171221
    }
172222
173223
    private class Downloader implements Runnable {

188238
            }
189239
            String name;
190240
            while((name = downloadQueue.popNextJob()) != null) {
241+
                updateNotification(-1, null);
191242
                doDownload(name);
243+
                updateNotification(-1, null);
192244
            }
193245
194246
            Handler threadHandler = new Handler(Looper.getMainLooper());

217269
218270
                    long expectedFileLength = getRange(new URL(uri));
219271
                    downloadFile(new URL(uri), temporaryFile, expectedFileLength, name);
220-
                } catch (Exception ignored) {
272+
                } catch (Exception exception) {
273+
                    exception.printStackTrace();
221274
                }
222275
            }
276+
277+
            d.switchToCacheFile();
223278
        }
224279
225280
        private boolean downloadSha256(URL url, File dest) throws IOException {