Skip to content

Commit

Permalink
bug fix & test
Browse files Browse the repository at this point in the history
  • Loading branch information
SirLYC committed Jun 16, 2019
1 parent 26b86fc commit 0b5debc
Show file tree
Hide file tree
Showing 7 changed files with 174 additions and 144 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.3.0-beta'
'versionName' : '0.4.0-beta'
]
deps = [
'kotlin-stdlib' : "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version",
Expand Down
44 changes: 23 additions & 21 deletions downloader/src/main/java/com/lyc/downloader/DownloadManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ private DownloadManager(OkHttpClient client, Context appContext) {
SQLiteDatabase db = new DevOpenHelper(appContext, DB_NAME).getWritableDatabase();
daoSession = new DaoMaster(db).newSession();
maxRunningTask = 4;
Logger.d("DownloadManager", "DownloadManager: maxRunningTask = " + maxRunningTask);
recoverDownloadTasks();
}

Expand Down Expand Up @@ -134,39 +135,29 @@ private void recoverDownloadTasks() {
DownloadExecutors.io.execute(() -> {
try {
List<DownloadInfo> downloadInfoList = queryActiveDownloadInfoListInner();
List<DownloadInfo> finishedDownloadInfoList = queryFinishedDownloadInfoList();
Collections.reverse(finishedDownloadInfoList);
// make sure to add last
List<DownloadInfo> finishedDownloadInfoList = queryFinishedDownloadInfoListInner();
for (DownloadInfo downloadInfo : downloadInfoList) {
long id = downloadInfo.getId();
DownloadTask downloadTask = new DownloadTask(downloadInfo, client);
taskTable.put(id, downloadTask);
infoTable.put(id, downloadInfo);
switch (downloadInfo.getDownloadItemState()) {
case PENDING:
case CONNECTING:
case RUNNING:
case WAITING:
enqueueTask(id, false, false);
break;
case STOPPING:
case PAUSED:
pausingTasksId.add(id);
break;
case ERROR:
case FATAL_ERROR:
errorTasksId.add(id);
break;

int downloadItemState = downloadInfo.getDownloadItemState();
if (downloadItemState == ERROR || downloadItemState == FATAL_ERROR) {
errorTasksId.add(id);
} else {
downloadInfo.setDownloadItemState(PAUSED);
pausingTasksId.add(id);
}
DownloadTask downloadTask = new DownloadTask(downloadInfo, client);
taskTable.put(id, downloadTask);
downloadInfo.setDownloadItemState(downloadTask.getState());
}

daoSession.getDownloadInfoDao().saveInTx(downloadInfoList);
for (DownloadInfo downloadInfo : finishedDownloadInfoList) {
taskTable.put(downloadInfo.getId(), new DownloadTask(downloadInfo, client));
infoTable.put(downloadInfo.getId(), downloadInfo);
}
} finally {
schedule();
recoverCountDownLatch.countDown();
}
});
Expand Down Expand Up @@ -583,6 +574,11 @@ public void setMaxRunningTask(int count) {
if (maxRunningTask != count) {
DownloadExecutors.message.execute(() -> {
maxRunningTask = count;
try {
throw new Exception();
} catch (Exception e) {
Logger.d("DownloadManager", "MaxRunningTask = " + maxRunningTask, e);
}
schedule();
});
}
Expand Down Expand Up @@ -613,11 +609,17 @@ public void setSendMessageIntervalNanos(long time) {

@Override
public DownloadInfo queryDownloadInfo(long id) {
waitForRecovering();
return infoTable.get(id);
}

@Override
public List<DownloadInfo> queryFinishedDownloadInfoList() {
waitForRecovering();
return queryFinishedDownloadInfoListInner();
}

public List<DownloadInfo> queryFinishedDownloadInfoListInner() {
DownloadInfoDao downloadInfoDao = daoSession.getDownloadInfoDao();
return downloadInfoDao.queryBuilder()
.where(DownloadInfoDao.Properties.DownloadItemState.eq(FINISH))
Expand Down
Loading

0 comments on commit 0b5debc

Please sign in to comment.