Skip to content

Commit

Permalink
增强Downloader,在已存在的任务id无效时,重新入对下载任务。
Browse files Browse the repository at this point in the history
  • Loading branch information
uni-cstar committed Mar 23, 2017
1 parent cfd18c6 commit c8db105
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 12 deletions.
1 change: 0 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,4 @@ dependencies {
})
compile 'com.android.support:appcompat-v7:23.4.0'
testCompile 'junit:junit:4.12'
// compile 'com.github.SupLuo:Easy:0.0.1'
}
1 change: 1 addition & 0 deletions app/src/main/java/ms/lucio/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ public class MainActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

}
}
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ buildscript {

allprojects {
repositories {
jcenter()
maven { url 'https://jitpack.io' }
jcenter()
}
}

Expand Down
3 changes: 0 additions & 3 deletions easy/build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
apply plugin: 'com.android.library'
apply plugin: 'com.github.dcendents.android-maven'

group='com.github.SupLuo'
android {
compileSdkVersion 23
Expand Down Expand Up @@ -40,8 +39,6 @@ dependencies {

compile 'io.reactivex.rxjava2:rxjava:2.0.7'
compile 'io.reactivex.rxjava2:rxandroid:2.0.1'


}

task makeJar(type: proguard.gradle.ProGuardTask, dependsOn: "build") {
Expand Down
53 changes: 46 additions & 7 deletions easy/src/main/java/easy/app/download/Downloader.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public class Downloader {

/**
* 开始下载任务
*
* @param params
* @return 下载任务
*/
Expand All @@ -33,16 +34,23 @@ public static DownloadTask startRequest(Context context, DownloadTask.DownloadRe
long id = DownloadRequestCache.getExistRequestId(context, url);
//已经存在现在任务
if (id != DownloadTask.INVALID_DOWNLOAD_ID && id > 0) {
String fileName = DownloadManagerQuery.getFileName(dm, id);
File file = new File(fileName);
//如果队列中存在任务,但是本地文件已经被删除,则移除队列id,清除缓存id
if (!file.exists() || !file.isFile()) {
removeDownloadTask(context, params.mUrl, id);
DownloadRequestCache.setRequestId(context, url, DownloadTask.INVALID_DOWNLOAD_ID);
//重新入队请求
boolean isTaskValid = isExistTaskIdValid(dm,id);
//如果任务无效,则重新入队下载任务
if(!isTaskValid){
id = dm.enqueue(params.build());
DownloadRequestCache.setRequestId(context, url, id);
}
//
// String fileName = DownloadManagerQuery.getFileName(dm, id);
// File file = new File(fileName);
// //如果队列中存在任务,但是本地文件已经被删除,则移除队列id,清除缓存id
// if (!file.exists() || !file.isFile()) {
// removeDownloadTask(context, params.mUrl, id);
// DownloadRequestCache.setRequestId(context, url, DownloadTask.INVALID_DOWNLOAD_ID);
// //重新入队请求
// id = dm.enqueue(params.build());
// DownloadRequestCache.setRequestId(context, url, id);
// }
} else {
//任务入队执行
id = dm.enqueue(params.build());
Expand All @@ -51,6 +59,37 @@ public static DownloadTask startRequest(Context context, DownloadTask.DownloadRe
return new DownloadTask(dm, id, params, listener);
}

/**
* 存在的任务id是否有效
* @param dm
* @param id
* @return
*/
private static boolean isExistTaskIdValid(DownloadManager dm, long id) {
String fileName = DownloadManagerQuery.getFileName(dm, id);
File file = new File(fileName);
//如果任务id存在,文件不存在,则说明文件已被删除
if (!file.exists() || !file.isFile()) {
return false;
}

int status = DownloadManagerQuery.getStatusById(dm, id);
//如果任务处于暂停中,尝试继续下载
if (status == DownloadTask.TaskStatus.PAUSED) {
try {
DownloadManagerQuery.resumeDownload(dm, id);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
} else {
//状态为错误或失败则是不可用任务
return (status != DownloadTask.TaskStatus.ERROR_TASK && status != DownloadTask.TaskStatus.FAILED);
}

}


/**
* 移除下载任务
Expand Down

0 comments on commit c8db105

Please sign in to comment.