Skip to content

Commit

Permalink
improve restart callback sequence
Browse files Browse the repository at this point in the history
  • Loading branch information
SirLYC committed Jun 29, 2019
1 parent e450f81 commit 1021e3b
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 28 deletions.
2 changes: 1 addition & 1 deletion config.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ ext {
'targetSdkVersion' : 28,
'minSdkVersion' : 19,
'versionCode' : 1,
'versionName' : '0.6.1-beta'
'versionName' : '0.6.2-beta'
]
deps = [
'kotlin-stdlib' : "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version",
Expand Down
59 changes: 34 additions & 25 deletions downloader/src/main/java/com/lyc/downloader/DownloadManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -421,14 +421,7 @@ private void submitInner(String url, String path, String filename, ISubmitCallba
} catch (RemoteException e) {
Logger.e(TAG, "submitSuccess", e);
}
IDownloadTasksChangeCallback downloadTasksChangeCallback = this.downloadTasksChangeCallback;
if (downloadTasksChangeCallback != null) {
try {
downloadTasksChangeCallback.onNewDownloadTaskArrive(downloadInfo);
} catch (RemoteException e) {
Logger.e(TAG, "onNewDownloadTaskArrive", e);
}
}
notifyDownloadManagerArrive(downloadInfo);
schedule();
} else {
try {
Expand Down Expand Up @@ -490,25 +483,55 @@ public void startAll() {
public void startOrResume(long id, boolean restart) {
DownloadExecutors.message.execute(() -> {
DownloadTask downloadTask = taskTable.get(id);
if (downloadTask == null && restart) {
final DownloadInfo info = infoTable.get(id);
if ((downloadTask == null || info == null) && restart) {
DownloadExecutors.io.execute(() -> {
DownloadInfo downloadInfo = daoSession.getDownloadInfoDao().load(id);
if (downloadInfo != null) {
DownloadExecutors.message.execute(() -> {
DownloadTask newDownloadTask = new DownloadTask(downloadInfo, client);
taskTable.put(id, newDownloadTask);
infoTable.put(id, downloadInfo);
notifyDownloadManagerArrive(downloadInfo);
enqueueTask(id, true, true);
});
}
});
} else if (downloadTask != null && ((pausingTasksId.remove(id) | errorTasksId.remove(id)) ||
downloadTask.getState() == FINISH)) {
if (restart) {
info.setDownloadedSize(0);
info.setTotalSize(0);
info.setLastModified(null);
onDownloadUpdateInfo(info);
}
enqueueTask(id, restart, true);
}
});
}

private void notifyDownloadManagerArrive(DownloadInfo info) {
try {
final IDownloadTasksChangeCallback downloadTasksChangeCallback = this.downloadTasksChangeCallback;
if (downloadTasksChangeCallback != null) {
downloadTasksChangeCallback.onNewDownloadTaskArrive(info);
}
} catch (RemoteException e) {
Logger.e("DownloadManager", "onNewDownloadTaskArrive", e);
}
}

private void notifyDownloadInfoRemoved(long id) {
final IDownloadTasksChangeCallback downloadTasksChangeCallback = this.downloadTasksChangeCallback;
if (downloadTasksChangeCallback != null) {
try {
downloadTasksChangeCallback.onDownloadTaskRemove(id);
} catch (RemoteException e) {
Logger.e("DownloadManager", "onNewDownloadTaskArrive", e);
}
}
}

@Override
public void pause(long id) {
DownloadExecutors.message.execute(() -> {
Expand All @@ -525,14 +548,7 @@ public void cancel(long id) {
DownloadTask downloadTask = taskTable.get(id);
if (downloadTask == null) return;
downloadTask.cancel();
IDownloadTasksChangeCallback downloadTasksChangeCallback = this.downloadTasksChangeCallback;
if (downloadTasksChangeCallback != null) {
try {
downloadTasksChangeCallback.onDownloadTaskRemove(id);
} catch (RemoteException e) {
Logger.e(TAG, "onDownloadTaskRemove", e);
}
}
notifyDownloadInfoRemoved(id);
});
}

Expand Down Expand Up @@ -567,14 +583,7 @@ public void delete(long id, boolean deleteFile) {
errorTasksId.remove(id);
pausingTasksId.remove(id);
downloadTask.delete(deleteFile);
IDownloadTasksChangeCallback downloadTasksChangeCallback = this.downloadTasksChangeCallback;
if (downloadTasksChangeCallback != null) {
try {
downloadTasksChangeCallback.onDownloadTaskRemove(id);
} catch (RemoteException e) {
Logger.e(TAG, "onDownloadTaskRemove", e);
}
}
notifyDownloadInfoRemoved(id);
schedule();
} else {
Logger.w("DownloadManager", "delete a task that is not present in DownloadManager! find in db. id = " + id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@
public interface DownloadTasksChangeListener {

/**
* notified when a new download Task is added to
* Notified when a new download Task is added to
* {@link DownloadManager}
*
* @param downloadInfo new-added info about download task
*/
void onNewDownloadTaskArrive(DownloadInfo downloadInfo);

/**
* notified when a download task is deleted
* Notified when a download task is deleted
*
* @param id the id of the download task
*/
Expand Down

0 comments on commit 1021e3b

Please sign in to comment.