|
36 | 36 |
|
37 | 37 | public class FileLoader { |
38 | 38 | private static final String TAG = "FileLoader"; |
| 39 | + public static final int STATUS_START_LOCAL_SEARCH = 100; |
| 40 | + public static final int STATUS_START_DOWNLOADING = 101; |
| 41 | + public static final int STATUS_DOWNLOAD_END = 102; |
39 | 42 | // Directory type |
40 | 43 | public static final int DIR_INTERNAL = 1; //Only your app can access. { android FilesDir() } |
41 | 44 | public static final int DIR_CACHE = 2; // Only your app can access, can be deleted by system. { android CacheDir() } |
@@ -247,23 +250,47 @@ private void sendFileResponseToListeners(FileResponse fileResponse) { |
247 | 250 | } |
248 | 251 | } |
249 | 252 |
|
| 253 | + private void sendStatusToListeners(int status) { |
| 254 | + if (!requestListenersMap.isEmpty()) { |
| 255 | + synchronized (REQUEST_LISTENER_QUEUE_LOCK) { |
| 256 | + List<FileRequestListener> listenerList = requestListenersMap.get(fileLoadRequest); |
| 257 | + if (listenerList != null) { |
| 258 | + for (FileRequestListener listener : listenerList) { |
| 259 | + listener.onStatusChange(status); |
| 260 | + } |
| 261 | + } |
| 262 | + } |
| 263 | + } |
| 264 | + } |
| 265 | + |
250 | 266 | @NonNull |
251 | | - private AsyncTask<Void, Void, DownloadResponse> getFileLoaderAsyncTask() { |
252 | | - return new AsyncTask<Void, Void, DownloadResponse>() { |
| 267 | + private AsyncTask<Void, Integer, DownloadResponse> getFileLoaderAsyncTask() { |
| 268 | + return new AsyncTask<Void, Integer, DownloadResponse>() { |
| 269 | + |
| 270 | + @Override |
| 271 | + protected void onProgressUpdate(Integer... values) { |
| 272 | + super.onProgressUpdate(values); |
| 273 | + if (values.length > 0) |
| 274 | + sendStatusToListeners(values[0]); |
| 275 | + } |
| 276 | + |
253 | 277 | @Override |
254 | 278 | protected DownloadResponse doInBackground(Void... voids) { |
255 | 279 | DownloadResponse downloadResponse = new DownloadResponse(); |
256 | 280 | File loadedFile = null; |
257 | 281 | try { |
258 | 282 | if (!fileLoadRequest.isForceLoadFromNetwork()) { |
259 | 283 | //search file locally |
| 284 | + publishProgress(STATUS_START_LOCAL_SEARCH); |
260 | 285 | loadedFile = AndroidFileManager.searchAndGetLocalFile(context, fileLoadRequest.getUri(), |
261 | 286 | fileLoadRequest.getDirectoryName(), fileLoadRequest.getDirectoryType()); |
262 | 287 | } |
263 | | - if (loadedFile == null || !loadedFile.exists()) { |
| 288 | + if (loadedFile == null || !loadedFile.exists() || fileLoadRequest.isAutoRefresh()) { |
264 | 289 | //download from internet |
| 290 | + publishProgress(STATUS_START_DOWNLOADING); |
265 | 291 | FileDownloader downloader = new FileDownloader(context, fileLoadRequest.getUri(), fileLoadRequest.getDirectoryName(), fileLoadRequest.getDirectoryType()); |
266 | 292 | loadedFile = downloader.download(fileLoadRequest.isAutoRefresh(), fileLoadRequest.isCheckIntegrity()); |
| 293 | + publishProgress(STATUS_DOWNLOAD_END); |
267 | 294 | } |
268 | 295 | downloadResponse.setDownloadedFile(loadedFile); |
269 | 296 | } catch (Exception e) { |
|
0 commit comments