Use LiveData
app/src/main/java/eu/lepiller/nani/DictionaryDownloadService.java
| 11 | 11 | ||
| 12 | 12 | import androidx.annotation.Nullable; | |
| 13 | 13 | import androidx.core.app.NotificationCompat; | |
| 14 | + | import androidx.lifecycle.LiveData; | |
| 15 | + | import androidx.lifecycle.MutableLiveData; | |
| 14 | 16 | ||
| 15 | 17 | import java.io.File; | |
| 16 | 18 | import java.io.FileOutputStream; | |
… | |||
| 30 | 32 | private NotificationCompat.Builder builder; | |
| 31 | 33 | private DownloadQueue downloadQueue; | |
| 32 | 34 | ||
| 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 | + | ||
| 33 | 49 | public static final String DOWNLOAD_ACTION = "eu.lepiller.nani.action.DOWNLOAD"; | |
| 34 | 50 | public static final String PAUSE_ACTION = "eu.lepiller.nani.action.PAUSE"; | |
| 35 | 51 | private static final String TAG = "DicoDownloadService"; | |
| 36 | 52 | ||
| 53 | + | public static LiveData<DownloadData> getData() { | |
| 54 | + | if(data == null) | |
| 55 | + | data = new MutableLiveData<>(); | |
| 56 | + | ||
| 57 | + | return data; | |
| 58 | + | } | |
| 59 | + | ||
| 37 | 60 | @Override | |
| 38 | 61 | public void onCreate() { | |
| 39 | 62 | Log.d(TAG, "onCreate"); | |
… | |||
| 46 | 69 | Downloader downloadThread = new Downloader(new Runnable() { | |
| 47 | 70 | @Override | |
| 48 | 71 | public void run() { | |
| 72 | + | stopForeground(true); | |
| 49 | 73 | stopSelf(); | |
| 50 | 74 | } | |
| 51 | 75 | }); | |
… | |||
| 72 | 96 | @Override | |
| 73 | 97 | public void onDestroy() { | |
| 74 | 98 | Log.d(TAG, "onDestroy"); | |
| 99 | + | downloadQueue.clear(); | |
| 100 | + | updateNotification(0, null); | |
| 75 | 101 | downloadQueue = null; | |
| 76 | 102 | super.onDestroy(); | |
| 77 | 103 | } | |
| 78 | 104 | ||
| 79 | 105 | 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 | + | } | |
| 83 | 117 | ||
| 84 | - | builder.setContentTitle(name) | |
| 85 | - | .setContentIntent(pendingIntent) | |
| 118 | + | builder.setContentIntent(pendingIntent) | |
| 86 | 119 | .setProgress(100, progress, (progress < 0)); | |
| 87 | 120 | 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 | + | }); | |
| 88 | 133 | } | |
| 89 | 134 | ||
| 90 | 135 | private static class DownloadQueue { | |
… | |||
| 93 | 138 | private boolean stopAsked = false; | |
| 94 | 139 | private ReentrantLock mutex = new ReentrantLock(); | |
| 95 | 140 | ||
| 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 | + | ||
| 96 | 159 | String popNextJob() { | |
| 97 | 160 | try { | |
| 98 | 161 | mutex.lock(); | |
… | |||
| 155 | 218 | mutex.unlock(); | |
| 156 | 219 | } | |
| 157 | 220 | } | |
| 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 | - | } | |
| 171 | 221 | } | |
| 172 | 222 | ||
| 173 | 223 | private class Downloader implements Runnable { | |
… | |||
| 188 | 238 | } | |
| 189 | 239 | String name; | |
| 190 | 240 | while((name = downloadQueue.popNextJob()) != null) { | |
| 241 | + | updateNotification(-1, null); | |
| 191 | 242 | doDownload(name); | |
| 243 | + | updateNotification(-1, null); | |
| 192 | 244 | } | |
| 193 | 245 | ||
| 194 | 246 | Handler threadHandler = new Handler(Looper.getMainLooper()); | |
… | |||
| 217 | 269 | ||
| 218 | 270 | long expectedFileLength = getRange(new URL(uri)); | |
| 219 | 271 | downloadFile(new URL(uri), temporaryFile, expectedFileLength, name); | |
| 220 | - | } catch (Exception ignored) { | |
| 272 | + | } catch (Exception exception) { | |
| 273 | + | exception.printStackTrace(); | |
| 221 | 274 | } | |
| 222 | 275 | } | |
| 276 | + | ||
| 277 | + | d.switchToCacheFile(); | |
| 223 | 278 | } | |
| 224 | 279 | ||
| 225 | 280 | private boolean downloadSha256(URL url, File dest) throws IOException { | |