This commit is contained in:
kehaoyuan
2019-12-30 18:15:30 +08:00
parent a69669a7ca
commit 899da43682
7 changed files with 178 additions and 15 deletions

View File

@ -157,9 +157,12 @@ public class DownloadManager implements DownloadStatusListener {
String url = (String) msg.obj;
switch (msg.what) {
case DownloadConfig.CONTINUE_DOWNLOAD_TASK:
case DownloadConfig.CONTINUE_DOWNLOAD_AUTO_TASK:
if (lastTimeMap.get(url) != null && System.currentTimeMillis() - lastTimeMap.get(url) >= 1000) {
DownloadEntity downloadEntity = getDownloadEntityByUrl(url);
if (downloadEntity != null) add(downloadEntity);
if (downloadEntity != null) {
resume(downloadEntity, msg.what == DownloadConfig.CONTINUE_DOWNLOAD_AUTO_TASK);
}
}
break;
case DownloadConfig.PAUSE_DOWNLOAD_TASK:
@ -309,6 +312,42 @@ public class DownloadManager implements DownloadStatusListener {
Utils.log(DownloadManager.class.getSimpleName(), "add");
}
/**
* 继续下载任务
*
* @param downloadEntity
* @param automatic 是否是自动下载
*/
public void resume(DownloadEntity downloadEntity, boolean automatic) {
if (downloadEntity != null) {
String url = downloadEntity.getUrl();
checkDownloadEntryRecordValidate(url);
if (isFileCompleted(url)) {
downloadEntity.setStatus(DownloadStatus.done);
DataChanger.INSTANCE.notifyDataChanged(downloadEntity);
} else if (!isTaskDownloading(url)) {
DownloadEntity daoEntity = mDownloadDao.get(downloadEntity.getUrl());
if (automatic) {
if (daoEntity != null) {
daoEntity.getMeta().put(DownloadDataHelper.DOWNLOAD_RESUME_WAY, DownloadDataHelper.DOWNLOAD_RESUME_AUTO);
mDownloadDao.newOrUpdate(daoEntity);
}
downloadEntity.getMeta().put(DownloadDataHelper.DOWNLOAD_RESUME_WAY, DownloadDataHelper.DOWNLOAD_RESUME_AUTO);
} else {
if (daoEntity != null) {
daoEntity.getMeta().put(DownloadDataHelper.DOWNLOAD_RESUME_WAY, DownloadDataHelper.DOWNLOAD_RESUME_MANUAL);
mDownloadDao.newOrUpdate(daoEntity);
}
downloadEntity.getMeta().put(DownloadDataHelper.DOWNLOAD_RESUME_WAY, DownloadDataHelper.DOWNLOAD_RESUME_MANUAL);
}
startDownloadService(downloadEntity, DownloadStatus.resume);
}
put(url, System.currentTimeMillis());
putStatus(url, DownloadStatus.downloading);
}
Utils.log(DownloadManager.class.getSimpleName(), "resume");
}
/**
* 添加一个下载任务(WiFi时自动下载)
*
@ -482,10 +521,16 @@ public class DownloadManager implements DownloadStatusListener {
* @param url
*/
public void cancel(String url) {
cancel(url, true);
cancel(url, true, false);
}
public void cancel(String url, boolean isDeleteFile) {
/**
* 根据url取消下载并删除已下载的文件
*
* @param url
* @param automatic 是否是安装完自动删除
*/
public void cancel(String url, boolean isDeleteFile, boolean automatic) {
DownloadEntity entry = mDownloadDao.get(url);
if (entry != null) {
if (isDeleteFile) {
@ -495,6 +540,11 @@ public class DownloadManager implements DownloadStatusListener {
Utils.log(DownloadManager.class.getSimpleName(), "cancel==>file and record were deleted!");
}
if (entry != null) {
if (automatic) {
entry.getMeta().put(DownloadDataHelper.DOWNLOAD_CANCEL_WAY, DownloadDataHelper.DOWNLOAD_CANCEL_AUTO);
} else {
entry.getMeta().put(DownloadDataHelper.DOWNLOAD_CANCEL_WAY, DownloadDataHelper.DOWNLOAD_CANCEL_MANUAL);
}
entry.setStatus(DownloadStatus.cancel);
startDownloadService(entry, DownloadStatus.cancel);
Utils.log(DownloadManager.class.getSimpleName(), "cancel");
@ -506,7 +556,7 @@ public class DownloadManager implements DownloadStatusListener {
*/
public void cancelAll() {
for (DownloadEntity entry : DataChanger.INSTANCE.getDownloadEntries().values()) {
cancel(entry.getUrl(), true);
cancel(entry.getUrl(), true, false);
}
Utils.log(DownloadManager.class.getSimpleName(), "cancel all");
}
@ -591,7 +641,7 @@ public class DownloadManager implements DownloadStatusListener {
public void startDownloadService(DownloadEntity downloadEntity, DownloadStatus status) {
// 在启动服务时添加该条下载的网络状态
if (status == DownloadStatus.add) {
if (status == DownloadStatus.add || status == DownloadStatus.resume) {
String network = DeviceUtils.getNetwork(mContext);
DownloadEntity daoEntity = mDownloadDao.get(downloadEntity.getUrl());
if (daoEntity != null) {
@ -629,7 +679,7 @@ public class DownloadManager implements DownloadStatusListener {
DownloadStatus.subscribe.equals(downloadEntity.getStatus())) {
DownloadManager.getInstance(mContext).put(downloadEntity.getUrl(), System.currentTimeMillis());
Message msg = Message.obtain();
msg.what = DownloadConfig.CONTINUE_DOWNLOAD_TASK;
msg.what = DownloadConfig.CONTINUE_DOWNLOAD_AUTO_TASK;
msg.obj = downloadEntity.getUrl();
DownloadManager.getInstance(mContext).sendMessageDelayed(msg, 1000);
}