Skip to content

Commit 36558cb

Browse files
author
krishna
committed
add status listener to request
1 parent fa2a000 commit 36558cb

File tree

4 files changed

+38
-7
lines changed

4 files changed

+38
-7
lines changed
0 Bytes
Binary file not shown.

fileloader/src/main/java/com/krishna/fileloader/FileLoader.java

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@
3636

3737
public class FileLoader {
3838
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;
3942
// Directory type
4043
public static final int DIR_INTERNAL = 1; //Only your app can access. { android FilesDir() }
4144
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) {
247250
}
248251
}
249252

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+
250266
@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+
253277
@Override
254278
protected DownloadResponse doInBackground(Void... voids) {
255279
DownloadResponse downloadResponse = new DownloadResponse();
256280
File loadedFile = null;
257281
try {
258282
if (!fileLoadRequest.isForceLoadFromNetwork()) {
259283
//search file locally
284+
publishProgress(STATUS_START_LOCAL_SEARCH);
260285
loadedFile = AndroidFileManager.searchAndGetLocalFile(context, fileLoadRequest.getUri(),
261286
fileLoadRequest.getDirectoryName(), fileLoadRequest.getDirectoryType());
262287
}
263-
if (loadedFile == null || !loadedFile.exists()) {
288+
if (loadedFile == null || !loadedFile.exists() || fileLoadRequest.isAutoRefresh()) {
264289
//download from internet
290+
publishProgress(STATUS_START_DOWNLOADING);
265291
FileDownloader downloader = new FileDownloader(context, fileLoadRequest.getUri(), fileLoadRequest.getDirectoryName(), fileLoadRequest.getDirectoryType());
266292
loadedFile = downloader.download(fileLoadRequest.isAutoRefresh(), fileLoadRequest.isCheckIntegrity());
293+
publishProgress(STATUS_DOWNLOAD_END);
267294
}
268295
downloadResponse.setDownloadedFile(loadedFile);
269296
} catch (Exception e) {

fileloader/src/main/java/com/krishna/fileloader/listener/FileRequestListener.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,12 @@
77
* Created by krishna on 12/10/17.
88
*/
99

10-
public interface FileRequestListener<T> {
11-
void onLoad(FileLoadRequest request, FileResponse<T> response);
10+
public abstract class FileRequestListener<T> {
11+
public void onStatusChange(int status) {
1212

13-
void onError(FileLoadRequest request, Throwable t);
13+
}
14+
15+
public abstract void onLoad(FileLoadRequest request, FileResponse<T> response);
16+
17+
public abstract void onError(FileLoadRequest request, Throwable t);
1418
}

fileloader/src/main/java/com/krishna/fileloader/network/FileDownloader.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public File download(boolean autoRefresh, boolean checkIntegrity) throws Excepti
7575

7676
//if file on server is not modified, return null.
7777
if (autoRefresh && response.code() == 304) {
78-
return null;
78+
return downloadFilePath;
7979
}
8080

8181
if (!response.isSuccessful() || response.body() == null) {

0 commit comments

Comments
 (0)