|
|
@@ -32,6 +32,7 @@ import com.sheep.gamegroup.greendao.download.DownLoadInfo;
|
|
|
import com.sheep.gamegroup.model.entity.Applications;
|
|
|
import com.sheep.gamegroup.model.entity.TaskEty;
|
|
|
import com.sheep.gamegroup.util.DownloadUtil;
|
|
|
+import com.sheep.gamegroup.util.FileUtil;
|
|
|
import com.sheep.gamegroup.util.LogUtil;
|
|
|
import com.sheep.gamegroup.util.StringUtils;
|
|
|
import com.sheep.gamegroup.util.TestUtil;
|
|
|
@@ -55,6 +56,7 @@ import static com.sheep.jiuyan.samllsheep.utils.ClassFileHelper.DIR;
|
|
|
public class DownloadService extends Service {
|
|
|
|
|
|
private final static int TAG_RETRY = 1001;
|
|
|
+ private final static int TAG_TYPE = 1002;
|
|
|
|
|
|
private DownloadUtil downloadUtil;
|
|
|
|
|
|
@@ -91,15 +93,20 @@ public class DownloadService extends Service {
|
|
|
if (intent != null) {
|
|
|
String download_url = intent.getStringExtra("download_url");
|
|
|
String file_path = intent.getStringExtra("file_path");
|
|
|
- startDownload(download_url, file_path, 0);
|
|
|
+ String type = intent.getStringExtra("type");
|
|
|
+ if(type == null){
|
|
|
+ type = FileUtil.getExtensionName(file_path);
|
|
|
+ }
|
|
|
+ startDownload(download_url, file_path, type, 0);
|
|
|
}
|
|
|
return super.onStartCommand(intent, flags, startId);
|
|
|
}
|
|
|
|
|
|
- private void startDownload(String download_url, String file_path, int retry) {
|
|
|
+ private void startDownload(String download_url, String file_path, String type, int retry) {
|
|
|
if (download_url != null && file_path != null) {
|
|
|
DownloadTask task = DownloadUtil.getTask(download_url, file_path);
|
|
|
task.addTag(TAG_RETRY, retry);
|
|
|
+ task.addTag(TAG_TYPE, type);
|
|
|
task.enqueue(downloadListener);
|
|
|
}
|
|
|
}
|
|
|
@@ -107,8 +114,16 @@ public class DownloadService extends Service {
|
|
|
private DownloadListener downloadListener = new DownloadListener4WithSpeed() {
|
|
|
@Override
|
|
|
public void taskStart(@NonNull DownloadTask task) {
|
|
|
- LogUtil.println("DownloadListener", "taskStart");
|
|
|
- DDProviderHelper.getInstance().updateDownload(task.getId(), task.getUrl(), DownloadUtil.STATUS_INIT);
|
|
|
+ String type = (String) task.getTag(TAG_TYPE);
|
|
|
+ LogUtil.println("DownloadListener", "taskStart", task.getTag(TAG_RETRY), type);
|
|
|
+ switch (type){
|
|
|
+ case "mp4":
|
|
|
+ break;
|
|
|
+ case "apk"://下载游戏
|
|
|
+ default:
|
|
|
+ DDProviderHelper.getInstance().updateDownload(task.getId(), task.getUrl(), DownloadUtil.STATUS_INIT);
|
|
|
+ break;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
@@ -125,8 +140,16 @@ public class DownloadService extends Service {
|
|
|
public void infoReady(@NonNull DownloadTask task, @NonNull BreakpointInfo info, boolean fromBreakpoint, @NonNull Listener4SpeedAssistExtend.Listener4SpeedModel model) {
|
|
|
long totalLength = info.getTotalLength();
|
|
|
LogUtil.println("DownloadListener", "infoReady", "fromBreakpoint", fromBreakpoint, "totalLength", totalLength);
|
|
|
- DownLoadInfo downloadInfo = downloadUtil.setDownloadTaskTotal(task.getUrl(), totalLength);
|
|
|
- EventBus.getDefault().post(new BigEvent().setEventTypes(EventTypes.DOWNLOAD_START).setData(downloadInfo));
|
|
|
+ String type = (String) task.getTag(TAG_TYPE);
|
|
|
+ switch (type){
|
|
|
+ case "mp4":
|
|
|
+ break;
|
|
|
+ case "apk":
|
|
|
+ default:
|
|
|
+ DownLoadInfo downloadInfo = downloadUtil.setDownloadTaskTotal(task.getUrl(), totalLength);
|
|
|
+ EventBus.getDefault().post(new BigEvent().setEventTypes(EventTypes.DOWNLOAD_START).setData(downloadInfo));
|
|
|
+ break;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
@@ -137,8 +160,16 @@ public class DownloadService extends Service {
|
|
|
@Override
|
|
|
public void progress(@NonNull DownloadTask task, long currentOffset, @NonNull SpeedCalculator taskSpeed) {
|
|
|
LogUtil.println("DownloadListener", "progress", "currentOffset", currentOffset, "taskSpeed", taskSpeed.averageSpeed());
|
|
|
- DownLoadInfo downloadInfo = downloadUtil.setDownloadTaskPercent(currentOffset, taskSpeed.averageSpeed(), task.getId(), task.getUrl());
|
|
|
- EventBus.getDefault().post(new BigEvent().setEventTypes(EventTypes.DOWNLOAD_RUNNING).setData(downloadInfo));
|
|
|
+ String type = (String) task.getTag(TAG_TYPE);
|
|
|
+ switch (type){
|
|
|
+ case "mp4":
|
|
|
+ break;
|
|
|
+ case "apk":
|
|
|
+ default:
|
|
|
+ DownLoadInfo downloadInfo = downloadUtil.setDownloadTaskPercent(currentOffset, taskSpeed.averageSpeed(), task.getId(), task.getUrl());
|
|
|
+ EventBus.getDefault().post(new BigEvent().setEventTypes(EventTypes.DOWNLOAD_RUNNING).setData(downloadInfo));
|
|
|
+ break;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
@@ -149,57 +180,65 @@ public class DownloadService extends Service {
|
|
|
@Override
|
|
|
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());
|
|
|
- if (realCause != null) {
|
|
|
- realCause.printStackTrace();
|
|
|
- }
|
|
|
- DownLoadInfo downLoadInfo;
|
|
|
- switch (cause) {
|
|
|
- case COMPLETED://下载完成
|
|
|
- if (task.getFile() != null) {
|
|
|
- downLoadInfo = downloadUtil.setDownloadTaskFinish(task.getUrl(), task.getFile().getAbsolutePath());
|
|
|
- ApkUtils.installApk(getApplicationContext(), task.getFile().getAbsolutePath());
|
|
|
- EventBus.getDefault().post(new BigEvent().setEventTypes(EventTypes.DOWNLOAD_COMPLETE).setData(downLoadInfo));
|
|
|
- }
|
|
|
- break;
|
|
|
- case PRE_ALLOCATE_FAILED://预先分配失败
|
|
|
- //空间不足,弹框提示
|
|
|
- try {
|
|
|
- DialogStorageLow.showDialog(realCause != null ? realCause.getMessage() : null);
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
- downLoadInfo = downloadUtil.getDownloadTaskByUrl(task.getUrl());
|
|
|
- downLoadInfo.setMStatus(DownloadUtil.STATUS_FAIL);
|
|
|
- if (realCause != null)
|
|
|
- downLoadInfo.setRealCauseMsg(realCause.getMessage());
|
|
|
- DDProviderHelper.getInstance().updateDownload(downLoadInfo);
|
|
|
- EventBus.getDefault().post(new BigEvent().setEventTypes(EventTypes.DOWNLOAD_FAIL).setData(downLoadInfo));
|
|
|
+ String type = (String) task.getTag(TAG_TYPE);
|
|
|
+ switch (type){
|
|
|
+ case "mp4":
|
|
|
break;
|
|
|
- case FILE_BUSY:
|
|
|
- case ERROR://下载错误
|
|
|
- downLoadInfo = downloadUtil.getDownloadTaskByUrl(task.getUrl());
|
|
|
- if (downLoadInfo != null) {
|
|
|
- downLoadInfo.setMStatus(DownloadUtil.STATUS_FAIL);
|
|
|
- if (realCause != null)
|
|
|
- downLoadInfo.setRealCauseMsg(realCause.getMessage());
|
|
|
- DDProviderHelper.getInstance().updateDownload(downLoadInfo);
|
|
|
+ case "apk":
|
|
|
+ default:
|
|
|
+ if (realCause != null) {
|
|
|
+ realCause.printStackTrace();
|
|
|
}
|
|
|
- int retry = (int) task.getTag(TAG_RETRY);
|
|
|
- if (retry < 15 && task.getFile() != null) {
|
|
|
- startDownload(task.getUrl(), task.getFile().getAbsolutePath(), ++retry);
|
|
|
- Log.e("DownloadRetry", "Retry " + retry + " " + task.getUrl());
|
|
|
- } else {
|
|
|
- if (downLoadInfo != null) {
|
|
|
+ DownLoadInfo downLoadInfo;
|
|
|
+ switch (cause) {
|
|
|
+ case COMPLETED://下载完成
|
|
|
+ if (task.getFile() != null) {
|
|
|
+ downLoadInfo = downloadUtil.setDownloadTaskFinish(task.getUrl(), task.getFile().getAbsolutePath());
|
|
|
+ ApkUtils.installApk(getApplicationContext(), task.getFile().getAbsolutePath());
|
|
|
+ EventBus.getDefault().post(new BigEvent().setEventTypes(EventTypes.DOWNLOAD_COMPLETE).setData(downLoadInfo));
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ case PRE_ALLOCATE_FAILED://预先分配失败
|
|
|
+ //空间不足,弹框提示
|
|
|
+ try {
|
|
|
+ DialogStorageLow.showDialog(realCause != null ? realCause.getMessage() : null);
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ downLoadInfo = downloadUtil.getDownloadTaskByUrl(task.getUrl());
|
|
|
+ downLoadInfo.setMStatus(DownloadUtil.STATUS_FAIL);
|
|
|
+ if (realCause != null)
|
|
|
+ downLoadInfo.setRealCauseMsg(realCause.getMessage());
|
|
|
+ DDProviderHelper.getInstance().updateDownload(downLoadInfo);
|
|
|
EventBus.getDefault().post(new BigEvent().setEventTypes(EventTypes.DOWNLOAD_FAIL).setData(downLoadInfo));
|
|
|
- }
|
|
|
+ break;
|
|
|
+ case FILE_BUSY:
|
|
|
+ case ERROR://下载错误
|
|
|
+ downLoadInfo = downloadUtil.getDownloadTaskByUrl(task.getUrl());
|
|
|
+ if (downLoadInfo != null) {
|
|
|
+ downLoadInfo.setMStatus(DownloadUtil.STATUS_FAIL);
|
|
|
+ if (realCause != null)
|
|
|
+ downLoadInfo.setRealCauseMsg(realCause.getMessage());
|
|
|
+ DDProviderHelper.getInstance().updateDownload(downLoadInfo);
|
|
|
+ }
|
|
|
+ int retry = (int) task.getTag(TAG_RETRY);
|
|
|
+ if (retry < 15 && task.getFile() != null) {
|
|
|
+ startDownload(task.getUrl(), task.getFile().getAbsolutePath(), type, ++retry);
|
|
|
+ Log.e("DownloadRetry", "Retry " + retry + " " + task.getUrl());
|
|
|
+ } else {
|
|
|
+ if (downLoadInfo != null) {
|
|
|
+ EventBus.getDefault().post(new BigEvent().setEventTypes(EventTypes.DOWNLOAD_FAIL).setData(downLoadInfo));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ case CANCELED://取消下载
|
|
|
+ downLoadInfo = downloadUtil.setDownloadTaskStatus(task.getUrl(), DownloadUtil.STATUS_PAUSE);
|
|
|
+ EventBus.getDefault().post(new BigEvent().setEventTypes(EventTypes.DOWNLOAD_STOP).setData(downLoadInfo));
|
|
|
+ break;
|
|
|
+ case SAME_TASK_BUSY:
|
|
|
+ break;
|
|
|
}
|
|
|
break;
|
|
|
- case CANCELED://取消下载
|
|
|
- downLoadInfo = downloadUtil.setDownloadTaskStatus(task.getUrl(), DownloadUtil.STATUS_PAUSE);
|
|
|
- EventBus.getDefault().post(new BigEvent().setEventTypes(EventTypes.DOWNLOAD_STOP).setData(downLoadInfo));
|
|
|
- break;
|
|
|
- case SAME_TASK_BUSY:
|
|
|
- break;
|
|
|
}
|
|
|
|
|
|
}
|