|
@@ -12,6 +12,7 @@ import android.support.annotation.NonNull;
|
|
|
import android.support.annotation.Nullable;
|
|
import android.support.annotation.Nullable;
|
|
|
import android.support.v7.app.AlertDialog;
|
|
import android.support.v7.app.AlertDialog;
|
|
|
import android.text.TextUtils;
|
|
import android.text.TextUtils;
|
|
|
|
|
+import android.util.Log;
|
|
|
import android.view.View;
|
|
import android.view.View;
|
|
|
|
|
|
|
|
import com.kfzs.duanduan.utils.ApkUtils;
|
|
import com.kfzs.duanduan.utils.ApkUtils;
|
|
@@ -19,6 +20,7 @@ import com.kfzs.duanduan.view.DialogStorageLow;
|
|
|
import com.liulishuo.okdownload.DownloadListener;
|
|
import com.liulishuo.okdownload.DownloadListener;
|
|
|
import com.liulishuo.okdownload.DownloadTask;
|
|
import com.liulishuo.okdownload.DownloadTask;
|
|
|
import com.liulishuo.okdownload.SpeedCalculator;
|
|
import com.liulishuo.okdownload.SpeedCalculator;
|
|
|
|
|
+import com.liulishuo.okdownload.core.Util;
|
|
|
import com.liulishuo.okdownload.core.breakpoint.BlockInfo;
|
|
import com.liulishuo.okdownload.core.breakpoint.BlockInfo;
|
|
|
import com.liulishuo.okdownload.core.breakpoint.BreakpointInfo;
|
|
import com.liulishuo.okdownload.core.breakpoint.BreakpointInfo;
|
|
|
import com.liulishuo.okdownload.core.cause.EndCause;
|
|
import com.liulishuo.okdownload.core.cause.EndCause;
|
|
@@ -52,12 +54,36 @@ import static com.sheep.jiuyan.samllsheep.utils.ClassFileHelper.DIR;
|
|
|
|
|
|
|
|
public class DownloadService extends Service {
|
|
public class DownloadService extends Service {
|
|
|
|
|
|
|
|
|
|
+ private final static int TAG_RETRY = 1001;
|
|
|
|
|
+
|
|
|
private DownloadUtil downloadUtil;
|
|
private DownloadUtil downloadUtil;
|
|
|
|
|
|
|
|
@Override
|
|
@Override
|
|
|
public void onCreate() {
|
|
public void onCreate() {
|
|
|
super.onCreate();
|
|
super.onCreate();
|
|
|
downloadUtil = new DownloadUtil();
|
|
downloadUtil = new DownloadUtil();
|
|
|
|
|
+ Util.setLogger(new Util.Logger() {
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void e(String tag, String msg, Exception e) {
|
|
|
|
|
+ Log.e("sheep-" + tag, msg + "\n" + e.getMessage());
|
|
|
|
|
+ e.printStackTrace();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void w(String tag, String msg) {
|
|
|
|
|
+ Log.w("sheep-" + tag, msg);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void d(String tag, String msg) {
|
|
|
|
|
+ Log.d("sheep-" + tag, msg);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void i(String tag, String msg) {
|
|
|
|
|
+ Log.i("sheep-" + tag, msg);
|
|
|
|
|
+ }
|
|
|
|
|
+ });
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
@Override
|
|
@@ -65,14 +91,19 @@ public class DownloadService extends Service {
|
|
|
if (intent != null) {
|
|
if (intent != null) {
|
|
|
String download_url = intent.getStringExtra("download_url");
|
|
String download_url = intent.getStringExtra("download_url");
|
|
|
String file_path = intent.getStringExtra("file_path");
|
|
String file_path = intent.getStringExtra("file_path");
|
|
|
- if (download_url != null && file_path != null) {
|
|
|
|
|
- DownloadTask task = DownloadUtil.getTask(download_url, file_path);
|
|
|
|
|
- task.enqueue(downloadListener);
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ startDownload(download_url, file_path, 0);
|
|
|
}
|
|
}
|
|
|
return super.onStartCommand(intent, flags, startId);
|
|
return super.onStartCommand(intent, flags, startId);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ private void startDownload(String download_url, String file_path, int retry) {
|
|
|
|
|
+ if (download_url != null && file_path != null) {
|
|
|
|
|
+ DownloadTask task = DownloadUtil.getTask(download_url, file_path);
|
|
|
|
|
+ task.addTag(TAG_RETRY, retry);
|
|
|
|
|
+ task.enqueue(downloadListener);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
private DownloadListener downloadListener = new DownloadListener4WithSpeed() {
|
|
private DownloadListener downloadListener = new DownloadListener4WithSpeed() {
|
|
|
@Override
|
|
@Override
|
|
|
public void taskStart(@NonNull DownloadTask task) {
|
|
public void taskStart(@NonNull DownloadTask task) {
|
|
@@ -118,6 +149,10 @@ public class DownloadService extends Service {
|
|
|
@Override
|
|
@Override
|
|
|
public void taskEnd(@NonNull DownloadTask task, @NonNull EndCause cause, @Nullable Exception realCause, @NonNull SpeedCalculator taskSpeed) {
|
|
public void taskEnd(@NonNull DownloadTask task, @NonNull EndCause cause, @Nullable Exception realCause, @NonNull SpeedCalculator taskSpeed) {
|
|
|
LogUtil.println("DownloadListener", "taskEnd", "cause", cause.name(), "realCause", realCause != null ? realCause.getMessage() : "null", "taskSpeed", taskSpeed.averageSpeed());
|
|
LogUtil.println("DownloadListener", "taskEnd", "cause", cause.name(), "realCause", realCause != null ? realCause.getMessage() : "null", "taskSpeed", taskSpeed.averageSpeed());
|
|
|
|
|
+ if (realCause != null) {
|
|
|
|
|
+ realCause.printStackTrace();
|
|
|
|
|
+ }
|
|
|
|
|
+ new Exception("Download exception: ").printStackTrace();
|
|
|
DownLoadInfo downLoadInfo;
|
|
DownLoadInfo downLoadInfo;
|
|
|
switch (cause) {
|
|
switch (cause) {
|
|
|
case COMPLETED://下载完成
|
|
case COMPLETED://下载完成
|
|
@@ -136,29 +171,26 @@ public class DownloadService extends Service {
|
|
|
}
|
|
}
|
|
|
downLoadInfo = downloadUtil.getDownloadTaskByUrl(task.getUrl());
|
|
downLoadInfo = downloadUtil.getDownloadTaskByUrl(task.getUrl());
|
|
|
downLoadInfo.setMStatus(DownloadUtil.STATUS_FAIL);
|
|
downLoadInfo.setMStatus(DownloadUtil.STATUS_FAIL);
|
|
|
- if(realCause != null)
|
|
|
|
|
|
|
+ if (realCause != null)
|
|
|
downLoadInfo.setRealCauseMsg(realCause.getMessage());
|
|
downLoadInfo.setRealCauseMsg(realCause.getMessage());
|
|
|
DDProviderHelper.getInstance().updateDownload(downLoadInfo);
|
|
DDProviderHelper.getInstance().updateDownload(downLoadInfo);
|
|
|
EventBus.getDefault().post(new BigEvent().setEventTypes(EventTypes.DOWNLOAD_FAIL).setData(downLoadInfo));
|
|
EventBus.getDefault().post(new BigEvent().setEventTypes(EventTypes.DOWNLOAD_FAIL).setData(downLoadInfo));
|
|
|
break;
|
|
break;
|
|
|
case FILE_BUSY:
|
|
case FILE_BUSY:
|
|
|
case ERROR://下载错误
|
|
case ERROR://下载错误
|
|
|
- //空间不足,弹框提示
|
|
|
|
|
- try {
|
|
|
|
|
- if (0 <= task.getReadBufferSize() && task.getReadBufferSize() > G.getFreeSpaceB()) {
|
|
|
|
|
- DialogStorageLow.showDialog(realCause != null ? realCause.getMessage() : null);
|
|
|
|
|
- }
|
|
|
|
|
- } catch (Exception e) {
|
|
|
|
|
- e.printStackTrace();
|
|
|
|
|
- }
|
|
|
|
|
downLoadInfo = downloadUtil.getDownloadTaskByUrl(task.getUrl());
|
|
downLoadInfo = downloadUtil.getDownloadTaskByUrl(task.getUrl());
|
|
|
- if(downLoadInfo!=null) {
|
|
|
|
|
|
|
+ if (downLoadInfo != null) {
|
|
|
downLoadInfo.setMStatus(DownloadUtil.STATUS_FAIL);
|
|
downLoadInfo.setMStatus(DownloadUtil.STATUS_FAIL);
|
|
|
if (realCause != null)
|
|
if (realCause != null)
|
|
|
downLoadInfo.setRealCauseMsg(realCause.getMessage());
|
|
downLoadInfo.setRealCauseMsg(realCause.getMessage());
|
|
|
DDProviderHelper.getInstance().updateDownload(downLoadInfo);
|
|
DDProviderHelper.getInstance().updateDownload(downLoadInfo);
|
|
|
EventBus.getDefault().post(new BigEvent().setEventTypes(EventTypes.DOWNLOAD_FAIL).setData(downLoadInfo));
|
|
EventBus.getDefault().post(new BigEvent().setEventTypes(EventTypes.DOWNLOAD_FAIL).setData(downLoadInfo));
|
|
|
}
|
|
}
|
|
|
|
|
+ int retry = (int) task.getTag(TAG_RETRY);
|
|
|
|
|
+ if (retry < 10) {
|
|
|
|
|
+ startDownload(task.getUrl(), task.getFile().getAbsolutePath(), ++retry);
|
|
|
|
|
+ Log.e("DownloadRetry", "Retry " + retry + " " + task.getUrl());
|
|
|
|
|
+ }
|
|
|
break;
|
|
break;
|
|
|
case CANCELED://取消下载
|
|
case CANCELED://取消下载
|
|
|
downLoadInfo = downloadUtil.setDownloadTaskStatus(task.getUrl(), DownloadUtil.STATUS_PAUSE);
|
|
downLoadInfo = downloadUtil.setDownloadTaskStatus(task.getUrl(), DownloadUtil.STATUS_PAUSE);
|