|
|
@@ -1,7 +1,6 @@
|
|
|
package com.sheep.gamegroup.view.activity;
|
|
|
|
|
|
import android.content.Context;
|
|
|
-import android.os.Handler;
|
|
|
import android.text.TextUtils;
|
|
|
import android.view.View;
|
|
|
import android.view.ViewGroup;
|
|
|
@@ -10,24 +9,21 @@ import android.widget.ProgressBar;
|
|
|
import android.widget.TextView;
|
|
|
import android.widget.Toast;
|
|
|
|
|
|
-import com.arialyy.annotations.Download;
|
|
|
-import com.arialyy.aria.core.Aria;
|
|
|
-import com.arialyy.aria.core.download.DownloadTask;
|
|
|
import com.kfzs.appstore.utils.adapter.OnMDLItemChildClickListener;
|
|
|
-import com.sheep.gamegroup.greendao.download.DownLoadInfo;
|
|
|
-import com.sheep.gamegroup.model.entity.DownloadStatus;
|
|
|
-import com.sheep.gamegroup.event.BigEvent;
|
|
|
-import com.sheep.gamegroup.event.EventDownloadHandler;
|
|
|
-import com.sheep.gamegroup.event.EventTypes;
|
|
|
-import com.sheep.gamegroup.util.DownloadUtil;
|
|
|
import com.kfzs.duanduan.utils.ApkUtils;
|
|
|
-import com.kfzs.duanduan.view.DialogStorageLow;
|
|
|
import com.kfzs.duanduan.view.KFProgressButton;
|
|
|
+import com.liulishuo.okdownload.DownloadTask;
|
|
|
+import com.liulishuo.okdownload.OkDownload;
|
|
|
import com.sheep.gamegroup.absBase.BaseActivity;
|
|
|
+import com.sheep.gamegroup.event.BigEvent;
|
|
|
+import com.sheep.gamegroup.event.EventDownloadHandler;
|
|
|
+import com.sheep.gamegroup.event.EventTypes;
|
|
|
+import com.sheep.gamegroup.greendao.download.DownLoadInfo;
|
|
|
+import com.sheep.gamegroup.model.entity.DownloadStatus;
|
|
|
import com.sheep.gamegroup.util.CommonUtil;
|
|
|
+import com.sheep.gamegroup.util.DownloadUtil;
|
|
|
import com.sheep.gamegroup.view.adapter.DownloadMgrAdapter;
|
|
|
import com.sheep.jiuyan.samllsheep.R;
|
|
|
-import com.sheep.jiuyan.samllsheep.SheepApp;
|
|
|
import com.sheep.jiuyan.samllsheep.utils.ClassFileHelper;
|
|
|
import com.sheep.jiuyan.samllsheep.utils.G;
|
|
|
import com.sheep.jiuyan.samllsheep.utils.TitleBarUtils;
|
|
|
@@ -68,7 +64,7 @@ public class ActDownloadMgr extends BaseActivity {
|
|
|
/**
|
|
|
* The service about persistence data and sqlite helper
|
|
|
*/
|
|
|
- DownloadUtil downloadTaskService;
|
|
|
+ DownloadUtil downloadUtil;
|
|
|
|
|
|
/**
|
|
|
* Data source
|
|
|
@@ -92,12 +88,7 @@ public class ActDownloadMgr extends BaseActivity {
|
|
|
@Override
|
|
|
public void onClick(View view) {
|
|
|
try {
|
|
|
- Aria.download(SheepApp.getInstance()).removeAllTask(true);
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
- try {
|
|
|
- downloadTaskService.deleteAllDownloadTask();
|
|
|
+ downloadUtil.deleteAllDownloadTask();
|
|
|
downloadTasks.clear();
|
|
|
downloadMgrAdapter.clear();
|
|
|
} catch (Exception e) {
|
|
|
@@ -115,14 +106,13 @@ public class ActDownloadMgr extends BaseActivity {
|
|
|
|
|
|
@Override
|
|
|
public void initListener() {
|
|
|
- Aria.download(this).register();
|
|
|
EventBus.getDefault().register(this);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public void initData() {
|
|
|
downloadTasks = new ArrayList<>();
|
|
|
- downloadTaskService = new DownloadUtil(this);
|
|
|
+ downloadUtil = new DownloadUtil(this);
|
|
|
initControlsEvents();
|
|
|
}
|
|
|
|
|
|
@@ -142,7 +132,7 @@ public class ActDownloadMgr extends BaseActivity {
|
|
|
if (downloadTasks.size() <= position) {
|
|
|
return;
|
|
|
}
|
|
|
- final DownLoadInfo downloadTask = downloadTaskService.getDownloadTaskByUrl(downloadTasks.get(position).getMDownloadUrl());
|
|
|
+ final DownLoadInfo downloadTask = downloadUtil.getDownloadTaskByUrl(downloadTasks.get(position).getMDownloadUrl());
|
|
|
if (null == downloadTask) {
|
|
|
return;
|
|
|
}
|
|
|
@@ -173,32 +163,13 @@ public class ActDownloadMgr extends BaseActivity {
|
|
|
|
|
|
private void setDownloadTaskData() {
|
|
|
downloadTasks.clear();
|
|
|
- downloadTasks.addAll(downloadTaskService.getAllDownloadTasks());
|
|
|
+ downloadTasks.addAll(downloadUtil.getAllDownloadTasks());
|
|
|
|
|
|
downloadMgrAdapter.clear();
|
|
|
downloadMgrAdapter.addMoreDatas(downloadTasks);
|
|
|
showDownloadTaskList(!downloadTasks.isEmpty());
|
|
|
}
|
|
|
|
|
|
-
|
|
|
- /*@Subscribe
|
|
|
- public void onEventMainThread(final DownloadStatus info) {
|
|
|
- if (mOnDownCallback == null) {
|
|
|
- mOnDownCallback = DownBtnUtils.installBtnStatus(downloadMgrAdapter.getmInstallButtonMgrMap());
|
|
|
- }
|
|
|
- EventDownloadHandler
|
|
|
- .newInstance(this, mOnDownCallback)
|
|
|
- .setmTag(TAG)
|
|
|
- .handlDownloadResult(info);
|
|
|
- }*/
|
|
|
-
|
|
|
- @Subscribe
|
|
|
- public void onEventMainThread(BigEvent bigEvent) {
|
|
|
- if (bigEvent.getEventTypes() == EventTypes.STORAGE_LOW) {
|
|
|
- DialogStorageLow.showDialog(this);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
@Subscribe
|
|
|
public void onEventMainThread(final DownloadStatus info) {
|
|
|
EventDownloadHandler.newInstance(this, new EventDownloadHandler.OnDownCallback() {
|
|
|
@@ -272,18 +243,14 @@ public class ActDownloadMgr extends BaseActivity {
|
|
|
//删除任务;
|
|
|
private void downloadTaskItemEventDelete(DownLoadInfo downloadTask, int position) {
|
|
|
if (downloadTask.getMDownloadTaskId() != null) {
|
|
|
- downloadTaskService.cancel(downloadTask);
|
|
|
- } else {
|
|
|
-
|
|
|
- }
|
|
|
- if (!TextUtils.isEmpty(downloadTask.getMApkPath())) {
|
|
|
- new File(downloadTask.getMApkPath()).delete();
|
|
|
+ downloadUtil.delete(downloadTask);
|
|
|
+ removeTask(position);
|
|
|
}
|
|
|
- removeTask(position);
|
|
|
doubleKill = false;
|
|
|
}
|
|
|
|
|
|
public static final int MAX_DOWNLOAD_TASK_SIZE = 5;
|
|
|
+
|
|
|
private void downloadTaskItemEventAction(DownLoadInfo downloadTask) {
|
|
|
switch (downloadTask.getMStatus()) {
|
|
|
case DownloadUtil.STATUS_INIT:
|
|
|
@@ -293,7 +260,7 @@ public class ActDownloadMgr extends BaseActivity {
|
|
|
downloadTask.getMPercent() == null ? 0 : downloadTask.getMPercent(), true);
|
|
|
break;
|
|
|
case DownloadUtil.STATUS_PAUSE:
|
|
|
- int currentDownloadTaskSize = downloadTaskService.getDownloadingTasksCount();
|
|
|
+ int currentDownloadTaskSize = downloadUtil.getDownloadingTasksCount();
|
|
|
if (currentDownloadTaskSize >= MAX_DOWNLOAD_TASK_SIZE) {
|
|
|
String strPrompt = String.format(getString(R.string.toast_download_task_out_of_rang), MAX_DOWNLOAD_TASK_SIZE + "");
|
|
|
Toast.makeText(this, strPrompt, Toast.LENGTH_SHORT).show();
|
|
|
@@ -313,7 +280,7 @@ public class ActDownloadMgr extends BaseActivity {
|
|
|
boolean isPkgInstalled = ApkUtils.getInstance().isPkgInstalled(downloadTask.getMPackageName());
|
|
|
if (isPkgInstalled) {
|
|
|
int apkStatus = ApkUtils.getInstance().comparePkgVersionCode(downloadTask.getMPackageName(), downloadTask.getMVersionCode());
|
|
|
- switch (apkStatus){
|
|
|
+ switch (apkStatus) {
|
|
|
case EQUALS:
|
|
|
case LT:
|
|
|
ApkUtils.openApplication(this, downloadTask.getMPackageName());
|
|
|
@@ -336,24 +303,21 @@ public class ActDownloadMgr extends BaseActivity {
|
|
|
}
|
|
|
|
|
|
private void restartTaskByDownloadUrl(final DownLoadInfo task) {
|
|
|
- downloadTaskService.cancel(task);
|
|
|
- downloadTaskService.deleteDownloadTaskByDownloadUrl(task.getMDownloadUrl());
|
|
|
- downloadTaskService.start(task);//重新下载
|
|
|
+ downloadUtil.delete(task);
|
|
|
+ downloadUtil.deleteDownloadTaskByDownloadUrl(task.getMDownloadUrl());
|
|
|
+ downloadUtil.start(task);//重新下载
|
|
|
setDownloadTaskData();
|
|
|
}
|
|
|
|
|
|
private void changeTaskPersistenceStatus(String downloadUrl, int status, int percent, boolean bPauseTask) {
|
|
|
- boolean isUpdate = downloadTaskService.setDownloadTaskStatus(downloadUrl, status);
|
|
|
- DownLoadInfo downLoadInfo = downloadTaskService.getDownloadTaskByUrl(downloadUrl);
|
|
|
- if (isUpdate) {
|
|
|
- setDownloadTaskData();
|
|
|
+ DownLoadInfo downLoadInfo = downloadUtil.setDownloadTaskStatus(downloadUrl, status);
|
|
|
+ if (downLoadInfo != null) {
|
|
|
+ DownloadTask task = DownloadUtil.getTask(downLoadInfo);
|
|
|
if (bPauseTask) {
|
|
|
- downloadTaskService.pause(downLoadInfo);
|
|
|
+ OkDownload.with().downloadDispatcher().cancel(task);
|
|
|
} else {
|
|
|
- downloadTaskService.start(downLoadInfo);
|
|
|
-
|
|
|
+ downloadUtil.start(downLoadInfo);
|
|
|
}
|
|
|
- postDownloadTaskEvents(downloadUrl, status, percent);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -385,29 +349,10 @@ public class ActDownloadMgr extends BaseActivity {
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
- boolean isSuccess = downloadTaskService.deleteDownloadTaskByDownloadUrl(targetUrl);
|
|
|
+ boolean isSuccess = downloadUtil.deleteDownloadTaskByDownloadUrl(targetUrl);
|
|
|
if (isSuccess) {
|
|
|
setDownloadTaskData();
|
|
|
- //延时执行,不然删除消息可能比下载进度信息先到,导致删除后还提示安装中
|
|
|
- new Handler().postDelayed(new Runnable() {
|
|
|
- @Override
|
|
|
- public void run() {
|
|
|
- postDownloadTaskEvents(targetUrl, DownloadUtil.STATUS_DELETE, 0);
|
|
|
- }
|
|
|
- }, 500);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- private void postDownloadTaskEvents(String targetUrl, int status, int percent) {
|
|
|
- if ("".equals(targetUrl)) {
|
|
|
- return;
|
|
|
}
|
|
|
- DownloadStatus downloadStatusInfo = new DownloadStatus();
|
|
|
- downloadStatusInfo.setStatus(status);
|
|
|
- downloadStatusInfo.setDownloadUrl(targetUrl);
|
|
|
- downloadStatusInfo.setDownloadPgrs(percent);
|
|
|
- EventBus.getDefault().post(downloadStatusInfo);
|
|
|
}
|
|
|
|
|
|
|
|
|
@@ -416,62 +361,64 @@ public class ActDownloadMgr extends BaseActivity {
|
|
|
if (fileDownloadSize == null || fileTotalSize == null) {
|
|
|
return context.getString(R.string.calculating);
|
|
|
}
|
|
|
- return df.format(fileDownloadSize) + "MB / " + fileTotalSize.intValue() + "MB";
|
|
|
+ return df.format(fileDownloadSize / (1024 * 1024)) + "MB / " + (fileTotalSize.intValue() / (1024 * 1024)) + "MB";
|
|
|
}
|
|
|
+
|
|
|
public static String strFormat(Context context, Double fileDownloadSize) {
|
|
|
DecimalFormat df = new DecimalFormat("######0.0");
|
|
|
if (fileDownloadSize == null) {
|
|
|
return context.getString(R.string.calculating);
|
|
|
}
|
|
|
- return df.format(fileDownloadSize) + "MB" ;
|
|
|
+ return df.format(fileDownloadSize) + "MB";
|
|
|
}
|
|
|
|
|
|
private void showDownloadTaskList(boolean isShow) {
|
|
|
CommonUtil.getInstance().updateEmptyView(empty_view, !isShow);
|
|
|
lvDownloadMgr.setVisibility(!isShow ? View.GONE : View.VISIBLE);
|
|
|
}
|
|
|
- @Download.onPre void onPre(DownloadTask task) {
|
|
|
- }
|
|
|
- @Download.onTaskStart void taskStart(DownloadTask task) {
|
|
|
- }
|
|
|
- @Download.onTaskResume void taskResume(DownloadTask task) {
|
|
|
- }
|
|
|
+
|
|
|
//在这里处理任务执行中的状态,如进度进度条的刷新
|
|
|
- @Download.onTaskRunning protected void running(DownloadTask task) {
|
|
|
-// downloadTaskService.setDownloadTaskPercent(p, task.getCurrentProgress()/1024d/1024d,task.getFileSize()/1024d/1024d,0,task.getKey());
|
|
|
+ protected void running(DownLoadInfo task) {
|
|
|
try {
|
|
|
- int p = task.getPercent(); //任务进度百分比
|
|
|
- String speed = task.getConvertSpeed(); //转换单位后的下载速度,单位转换需要在配置文件中打开
|
|
|
- System.out.println("Aria "+task.getKey()+",p " + p + ", speed = " + speed);
|
|
|
- String url = task.getKey();
|
|
|
+ String url = task.getMDownloadUrl();
|
|
|
KFProgressButton targetBtn = lvDownloadMgr.findViewWithTag(DownloadMgrAdapter.PUBLIC_TAG_PREFIX_BUTTON + url);
|
|
|
ProgressBar pbProgress = lvDownloadMgr.findViewWithTag(DownloadMgrAdapter.PUBLIC_TAG_PREFIX_PROGRESS + url);
|
|
|
TextView tvProgress = lvDownloadMgr.findViewWithTag(DownloadMgrAdapter.PUBLIC_TAG_PREFIX_TEXTVIEW + url);
|
|
|
if (tvProgress == null) {
|
|
|
return;
|
|
|
}
|
|
|
- tvProgress.setText(strFormat(this,task.getCurrentProgress()/1024d/1024d,task.getFileSize()/1024d/1024d));
|
|
|
- pbProgress.setProgress(task.getPercent());
|
|
|
+ tvProgress.setText(strFormat(this, task.getMDownloadedSize(), task.getMTotalSize()));
|
|
|
+ pbProgress.setProgress(task.getMPercent());
|
|
|
targetBtn.setText(R.string.downloading);
|
|
|
|
|
|
- setTaskStatusByDownloadUrl(url, DownloadUtil.STATUS_ING, task.getDownloadPath());
|
|
|
+ setTaskStatusByDownloadUrl(url, DownloadUtil.STATUS_ING, task.getMApkPath());
|
|
|
} catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- @Download.onTaskStop void taskStop(DownloadTask task) {
|
|
|
-// String url = task.getKey();
|
|
|
-// downloadTaskService.setDownloadTaskStatus(url, DownloadTaskService.STATUS_PAUSE);
|
|
|
- }
|
|
|
+ void taskStop(DownLoadInfo task) {
|
|
|
+ try {
|
|
|
+ String url = task.getMDownloadUrl();
|
|
|
+ KFProgressButton targetBtn = lvDownloadMgr.findViewWithTag(DownloadMgrAdapter.PUBLIC_TAG_PREFIX_BUTTON + url);
|
|
|
+ ProgressBar pbProgress = lvDownloadMgr.findViewWithTag(DownloadMgrAdapter.PUBLIC_TAG_PREFIX_PROGRESS + url);
|
|
|
+ TextView tvProgress = lvDownloadMgr.findViewWithTag(DownloadMgrAdapter.PUBLIC_TAG_PREFIX_TEXTVIEW + url);
|
|
|
+ if (tvProgress == null) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ tvProgress.setText(strFormat(this, task.getMDownloadedSize(), task.getMTotalSize()));
|
|
|
+ pbProgress.setProgress(task.getMPercent());
|
|
|
+ targetBtn.setText(R.string.has_pause);
|
|
|
|
|
|
- @Download.onNoSupportBreakPoint void onNoSupportBreakPoint(DownloadTask task) {
|
|
|
+ setTaskStatusByDownloadUrl(url, DownloadUtil.STATUS_PAUSE, task.getMApkPath());
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
- @Download.onTaskCancel void taskCancel(DownloadTask task) {
|
|
|
-// downloadTaskService.deleteDownloadTaskByDownloadUrl(task.getKey());
|
|
|
+ void taskCancel(DownLoadInfo task) {
|
|
|
try {
|
|
|
- String url = task.getKey();
|
|
|
+ String url = task.getMDownloadUrl();
|
|
|
int iTempPosition = -1;
|
|
|
for (int position = 0; position < downloadTasks.size(); position++) {
|
|
|
DownLoadInfo downLoadInfo = downloadTasks.get(position);
|
|
|
@@ -489,31 +436,57 @@ public class ActDownloadMgr extends BaseActivity {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- @Download.onTaskFail void taskFail(DownloadTask task) {
|
|
|
-// downloadTaskService.setDownloadTaskStatus(task.getKey(), DownloadTaskService.STATUS_FAIL);
|
|
|
+ void taskFail(DownLoadInfo task) {
|
|
|
try {
|
|
|
- String url = task.getKey();
|
|
|
+ String url = task.getMDownloadUrl();
|
|
|
KFProgressButton targetBtn = lvDownloadMgr.findViewWithTag(DownloadMgrAdapter.PUBLIC_TAG_PREFIX_BUTTON + url);
|
|
|
targetBtn.setText(R.string.download_fail);
|
|
|
- setTaskStatusByDownloadUrl(url, DownloadUtil.STATUS_FAIL, task.getDownloadPath());
|
|
|
+ setTaskStatusByDownloadUrl(url, DownloadUtil.STATUS_FAIL, task.getMApkPath());
|
|
|
} catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- @Download.onTaskComplete void taskComplete(DownloadTask task) {
|
|
|
-// downloadTaskService.setDownloadTaskFinish(task.getKey(), task.getDownloadPath());
|
|
|
+ void taskComplete(DownLoadInfo task) {
|
|
|
try {
|
|
|
- TextView tvProgress = lvDownloadMgr.findViewWithTag(DownloadMgrAdapter.PUBLIC_TAG_PREFIX_TEXTVIEW + task.getKey());
|
|
|
+ TextView tvProgress = lvDownloadMgr.findViewWithTag(DownloadMgrAdapter.PUBLIC_TAG_PREFIX_TEXTVIEW + task.getMDownloadUrl());
|
|
|
tvProgress.setText(R.string.download_completed);
|
|
|
|
|
|
- setTaskStatusByDownloadUrl(task.getKey(), DownloadUtil.STATUS_FINISH, task.getDownloadPath());
|
|
|
+ setTaskStatusByDownloadUrl(task.getMDownloadUrl(), DownloadUtil.STATUS_FINISH, task.getMApkPath());
|
|
|
} catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
- ApkUtils.installApk(ActDownloadMgr.this, task.getDownloadPath());
|
|
|
+ ApkUtils.installApk(ActDownloadMgr.this, task.getMApkPath());
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 主要的事件广播接收更新处理
|
|
|
+ *
|
|
|
+ * @param event
|
|
|
+ */
|
|
|
+ @Subscribe
|
|
|
+ public void onEventMainThread(BigEvent event) {
|
|
|
+ if (event.getData() instanceof DownLoadInfo) {
|
|
|
+ DownLoadInfo task = (DownLoadInfo) event.getData();
|
|
|
+ switch (event.getEventTypes()) {
|
|
|
+ case DOWNLOAD_RUNNING:
|
|
|
+ running(task);
|
|
|
+ break;
|
|
|
+ case DOWNLOAD_STOP:
|
|
|
+ taskStop(task);
|
|
|
+ break;
|
|
|
+ case DOWNLOAD_COMPLETE:
|
|
|
+ taskComplete(task);
|
|
|
+ break;
|
|
|
+ case DOWNLOAD_CANCEL:
|
|
|
+// taskCancel(task);
|
|
|
+ break;
|
|
|
+ case DOWNLOAD_FAIL:
|
|
|
+ taskFail(task);
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
|