From 09e49a28deafe8aaba6951ac80384e9619979f6c Mon Sep 17 00:00:00 2001 From: laoyuyu <511455842@qq.com> Date: Sat, 24 Nov 2018 12:13:40 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=9D=E5=A7=8B=E5=8C=96Aria=E6=97=B6?= =?UTF-8?q?=E4=BC=9A=E5=B0=86=E6=89=80=E6=9C=89=E6=95=B0=E6=8D=AE=E5=BA=93?= =?UTF-8?q?=E7=8A=B6=E6=80=81=E4=B8=BA=E4=B8=8B=E8=BD=BD=E4=B8=AD=E7=9A=84?= =?UTF-8?q?=E4=BB=BB=E5=8A=A1=E6=94=B9=E4=B8=BA=E5=B7=B2=E5=81=9C=E6=AD=A2?= =?UTF-8?q?=EF=BC=8C=E9=98=B2=E6=AD=A2=E5=BA=94=E7=94=A8=E8=A2=ABkill?= =?UTF-8?q?=E5=90=8E=EF=BC=8C=E4=BB=BB=E5=8A=A1=E7=8A=B6=E6=80=81=E9=94=99?= =?UTF-8?q?=E8=AF=AF,=20=E5=88=9D=E5=A7=8B=E5=8C=96=E6=97=B6=E8=87=AA?= =?UTF-8?q?=E5=8A=A8=E5=88=A4=E6=96=AD=E6=96=87=E4=BB=B6=E6=98=AF=E5=90=A6?= =?UTF-8?q?=E8=A2=AB=E5=88=A0=E9=99=A4=EF=BC=8C=E6=96=87=E4=BB=B6=E8=A2=AB?= =?UTF-8?q?=E5=88=A0=E9=99=A4=E7=9A=84=E4=BB=BB=E5=8A=A1=E5=B0=86=E8=87=AA?= =?UTF-8?q?=E5=8A=A8=E9=87=8D=E7=BD=AE=E9=BB=98=E8=AE=A4=E5=80=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/arialyy/aria/core/AriaManager.java | 31 +++++++++--- .../aria/core/download/BaseNormalTarget.java | 7 +-- .../arialyy/aria/core/manager/DTEFactory.java | 37 ++++++++++++-- .../java/com/arialyy/aria/orm/DbEntity.java | 8 +++ .../com/arialyy/aria/util/CommonUtil.java | 50 ++++++++++++++++++- DEV_LOG.md | 3 ++ .../simple/download/SingleTaskActivity.java | 5 +- build.gradle | 2 +- 8 files changed, 124 insertions(+), 19 deletions(-) diff --git a/Aria/src/main/java/com/arialyy/aria/core/AriaManager.java b/Aria/src/main/java/com/arialyy/aria/core/AriaManager.java index ba810bb9..84e0e80b 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/AriaManager.java +++ b/Aria/src/main/java/com/arialyy/aria/core/AriaManager.java @@ -30,12 +30,15 @@ import com.arialyy.aria.core.command.ICmd; import com.arialyy.aria.core.common.QueueMod; import com.arialyy.aria.core.download.DownloadEntity; import com.arialyy.aria.core.download.DownloadGroupEntity; +import com.arialyy.aria.core.download.DownloadGroupTaskEntity; import com.arialyy.aria.core.download.DownloadReceiver; +import com.arialyy.aria.core.download.DownloadTaskEntity; import com.arialyy.aria.core.inf.AbsReceiver; import com.arialyy.aria.core.inf.IReceiver; import com.arialyy.aria.core.inf.ReceiverType; import com.arialyy.aria.core.upload.UploadEntity; import com.arialyy.aria.core.upload.UploadReceiver; +import com.arialyy.aria.core.upload.UploadTaskEntity; import com.arialyy.aria.orm.DbEntity; import com.arialyy.aria.orm.DelegateWrapper; import com.arialyy.aria.util.ALog; @@ -82,6 +85,7 @@ import org.xml.sax.SAXException; regAppLifeCallback(context); initConfig(); initAria(); + amendTaskState(); } public static AriaManager getInstance(Context context) { @@ -94,9 +98,6 @@ import org.xml.sax.SAXException; } static AriaManager getInstance() { - //if (INSTANCE == null) { - // throw new NullPointerException("请在Application或Activity初始化时调用一次Aria.init(context)方法进行初始化操作"); - //} return INSTANCE; } @@ -121,6 +122,21 @@ import org.xml.sax.SAXException; mAConfig.setLogLevel(mAConfig.getLogLevel()); } + /** + * 修正任务状态 + */ + private void amendTaskState() { + Class[] clazzs = new Class[] { + DownloadEntity.class, UploadEntity.class, DownloadGroupEntity.class, + DownloadTaskEntity.class, UploadTaskEntity.class, DownloadGroupTaskEntity.class + }; + String sql = "UPDATE %s SET state=2 WHERE state=4"; + for (Class clazz : clazzs) { + String temp = String.format(sql, clazz.getSimpleName()); + DbEntity.exeSql(temp); + } + } + public Map getReceiver() { return mReceivers; } @@ -136,8 +152,7 @@ import org.xml.sax.SAXException; * @param mod {@link QueueMod} * @deprecated 后续版本会删除该api */ - @Deprecated - public AriaManager setUploadQueueMod(QueueMod mod) { + @Deprecated public AriaManager setUploadQueueMod(QueueMod mod) { mUConfig.setQueueMod(mod.tag); return this; } @@ -153,8 +168,7 @@ import org.xml.sax.SAXException; * @param mod {@link QueueMod} * @deprecated 后续版本会删除该api */ - @Deprecated - public AriaManager setDownloadQueueMod(QueueMod mod) { + @Deprecated public AriaManager setDownloadQueueMod(QueueMod mod) { mDConfig.setQueueMod(mod.tag); return this; } @@ -351,7 +365,8 @@ import org.xml.sax.SAXException; * * @param type {@link ReceiverType} * @param obj 观察者对象 - * @return key的格式为:{@code String.format("%s_%s_%s", obj.getClass().getName(), type, obj.hashCode());} + * @return key的格式为:{@code String.format("%s_%s_%s", obj.getClass().getName(), type, + * obj.hashCode());} */ private String createKey(@ReceiverType String type, Object obj) { return String.format("%s_%s_%s", obj.getClass().getName(), type, obj.hashCode()); diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/BaseNormalTarget.java b/Aria/src/main/java/com/arialyy/aria/core/download/BaseNormalTarget.java index 538de167..5028515d 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/BaseNormalTarget.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/BaseNormalTarget.java @@ -16,6 +16,7 @@ package com.arialyy.aria.core.download; import android.text.TextUtils; +import com.arialyy.aria.core.common.TaskRecord; import com.arialyy.aria.core.manager.TEManager; import com.arialyy.aria.core.queue.DownloadTaskQueue; import com.arialyy.aria.orm.DbEntity; @@ -168,9 +169,9 @@ abstract class BaseNormalTarget if (!forceDownload) { ALog.e(TAG, "下载失败,保存路径【" + filePath + "】已经被其它任务占用,请设置其它保存路径"); return false; - }else { - ALog.d(TAG, "保存路径【" + filePath + "】已经被其它任务占用,当前任务将覆盖该路径的文件"); - DbEntity.deleteData(DownloadEntity.class, "downloadPath=?", filePath); + } else { + ALog.w(TAG, "保存路径【" + filePath + "】已经被其它任务占用,当前任务将覆盖该路径的文件"); + CommonUtil.delTaskRecord(filePath, 1); mTaskEntity = TEManager.getInstance().getTEntity(DownloadTaskEntity.class, url); } } diff --git a/Aria/src/main/java/com/arialyy/aria/core/manager/DTEFactory.java b/Aria/src/main/java/com/arialyy/aria/core/manager/DTEFactory.java index 16a5c84d..bbc78383 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/manager/DTEFactory.java +++ b/Aria/src/main/java/com/arialyy/aria/core/manager/DTEFactory.java @@ -16,6 +16,8 @@ package com.arialyy.aria.core.manager; import android.text.TextUtils; +import com.arialyy.aria.core.common.AbsFileer; +import com.arialyy.aria.core.common.TaskRecord; import com.arialyy.aria.core.download.DownloadEntity; import com.arialyy.aria.core.download.DownloadTaskEntity; import com.arialyy.aria.core.download.wrapper.DTEWrapper; @@ -57,7 +59,8 @@ class DTEFactory implements INormalTEFactory taskEntity = wrapper.get(0).taskEntity; if (taskEntity == null) { taskEntity = new DownloadTaskEntity(); - } else if (taskEntity.getEntity() == null || TextUtils.isEmpty(taskEntity.getEntity().getUrl())) { + } else if (taskEntity.getEntity() == null || TextUtils.isEmpty( + taskEntity.getEntity().getUrl())) { taskEntity.setEntity(entity); } } else { @@ -93,9 +96,37 @@ class DTEFactory implements INormalTEFactory entity.setDownloadPath(UUID.randomUUID().toString().replace("-", "")); //设置临时路径 } File file = new File(entity.getDownloadPath()); - if (!file.exists()) { - entity.setState(IEntity.STATE_WAIT); + TaskRecord record = + TaskRecord.findFirst(TaskRecord.class, "filePath=?", entity.getDownloadPath()); + if (record == null) { + resetEntity(entity); + } else { + if (record.isBlock) { + int count = 0; + for (int i = 0, len = record.threadNum; i < len; i++) { + File temp = new File(String.format(AbsFileer.SUB_PATH, record.filePath, i)); + if (!temp.exists()) { + count++; + } + } + if (count == record.threadNum) { + resetEntity(entity); + } + } else if (!file.exists()) { // 非分块文件需要判断文件是否存在 + resetEntity(entity); + } } return entity; } + + /** + * 初始化下载实体 + */ + private void resetEntity(DownloadEntity entity) { + entity.setPercent(0); + entity.setCompleteTime(0); + entity.setComplete(false); + entity.setCurrentProgress(0); + entity.setState(IEntity.STATE_WAIT); + } } diff --git a/Aria/src/main/java/com/arialyy/aria/orm/DbEntity.java b/Aria/src/main/java/com/arialyy/aria/orm/DbEntity.java index 15b71db8..9e9dbf11 100644 --- a/Aria/src/main/java/com/arialyy/aria/orm/DbEntity.java +++ b/Aria/src/main/java/com/arialyy/aria/orm/DbEntity.java @@ -176,6 +176,14 @@ public abstract class DbEntity { return rowID != -1 && util.isExist(getClass(), rowID); } + /** + * 表是否存在 + * @return {@code true} 存在 + */ + public static boolean tableExists(Class clazz){ + return DelegateWrapper.getInstance().tableExists(clazz); + } + /** * 插入数据,只有 target中checkEntity成功后才能插入,创建实体部分也不允许操作 */ diff --git a/Aria/src/main/java/com/arialyy/aria/util/CommonUtil.java b/Aria/src/main/java/com/arialyy/aria/util/CommonUtil.java index 1a786860..132f5100 100644 --- a/Aria/src/main/java/com/arialyy/aria/util/CommonUtil.java +++ b/Aria/src/main/java/com/arialyy/aria/util/CommonUtil.java @@ -35,10 +35,12 @@ import com.arialyy.aria.core.common.AbsFileer; import com.arialyy.aria.core.common.TaskRecord; import com.arialyy.aria.core.download.DownloadEntity; import com.arialyy.aria.core.download.DownloadGroupEntity; +import com.arialyy.aria.core.download.DownloadTaskEntity; import com.arialyy.aria.core.inf.AbsGroupTaskEntity; import com.arialyy.aria.core.inf.AbsNormalEntity; import com.arialyy.aria.core.inf.AbsTaskEntity; import com.arialyy.aria.core.upload.UploadEntity; +import com.arialyy.aria.core.upload.UploadTaskEntity; import com.arialyy.aria.orm.DbEntity; import dalvik.system.DexFile; import java.io.File; @@ -567,6 +569,50 @@ public class CommonUtil { dEntity.deleteData(); } + /** + * 删除任务记录,默认删除文件 + * + * @param filePath 文件路径 + * @param type {@code 1}下载任务的记录,{@code 2} 上传任务的记录 + * {@code false}如果任务已经完成,只删除任务数据库记录 + */ + public static void delTaskRecord(String filePath, int type) { + if (TextUtils.isEmpty(filePath)) { + throw new NullPointerException("删除记录失败,文件路径为空"); + } + if (type != 1 && type != 2) { + throw new IllegalArgumentException("任务记录类型错误"); + } + TaskRecord record = DbEntity.findFirst(TaskRecord.class, "filePath=?", filePath); + if (record == null) { + ALog.w(TAG, "删除记录失败,记录为空"); + return; + } + File file = new File(filePath); + // 删除分块文件 + if (record.isBlock) { + for (int i = 0, len = record.threadNum; i < len; i++) { + File partFile = new File(String.format(AbsFileer.SUB_PATH, record.filePath, i)); + if (partFile.exists()) { + partFile.delete(); + } + } + } + if (file.exists()) { + file.delete(); + } + + record.deleteData(); + //下载任务实体和下载实体为一对一关系,下载实体删除,任务实体自动删除 + if (type == 1) { + DbEntity.deleteData(DownloadTaskEntity.class, "key=?", filePath); + DbEntity.deleteData(DownloadEntity.class, "downloadPath=?", filePath); + }else { + DbEntity.deleteData(UploadTaskEntity.class, "key=?", filePath); + DbEntity.deleteData(UploadEntity.class, "filePath=?", filePath); + } + } + /** * 获取CPU核心数 */ @@ -705,8 +751,8 @@ public class CommonUtil { * @param taskType {@link ICmd#TASK_TYPE_DOWNLOAD}、{@link ICmd#TASK_TYPE_DOWNLOAD_GROUP}、{@link * ICmd#TASK_TYPE_UPLOAD} */ - public static AbsNormalCmd createNormalCmd(T entity, - int cmd, int taskType) { + public static AbsNormalCmd createNormalCmd(T entity, int cmd, + int taskType) { return NormalCmdFactory.getInstance().createCmd(entity, cmd, taskType); } diff --git a/DEV_LOG.md b/DEV_LOG.md index 3357ff74..7929edac 100644 --- a/DEV_LOG.md +++ b/DEV_LOG.md @@ -14,6 +14,9 @@ - 优化logcat日志提示 - 修改下载线程的优先级为`Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);` - fix bug https://github.com/AriaLyy/Aria/issues/319 + - 修复分卡下载失败的问题 https://github.com/AriaLyy/Aria/issues/326 + - 初始化Aria时会将所有数据库状态为下载中的任务改为已停止,防止应用被kill后,任务状态错误 + - 初始化时自动判断文件是否被删除,文件被删除的任务将自动重置默认值 + v_3.5.1 - 优化`taskExists`方法 - 添加`post`参数请求支持 diff --git a/app/src/main/java/com/arialyy/simple/download/SingleTaskActivity.java b/app/src/main/java/com/arialyy/simple/download/SingleTaskActivity.java index 621bdbf8..4e1b7d40 100644 --- a/app/src/main/java/com/arialyy/simple/download/SingleTaskActivity.java +++ b/app/src/main/java/com/arialyy/simple/download/SingleTaskActivity.java @@ -55,13 +55,13 @@ public class SingleTaskActivity extends BaseActivity { //"https://yizi-kejian.oss-cn-beijing.aliyuncs.com/qimeng/package1/qmtable11.zip"; //"http://rs.0.gaoshouyou.com/d/04/1e/400423a7551e1f3f0eb1812afa1f9b44.apk"; //"http://58.210.9.131/tpk/sipgt//TDLYZTGH.tpk"; //chunked 下载 - //"https://static.donguo.me//video/ip/course/pfys_1.mp4"; + "https://static.donguo.me//video/ip/course/pfys_1.mp4"; //"https://www.baidu.com/link?url=_LFCuTPtnzFxVJByJ504QymRywIA1Z_T5xUxe9ZLuxcGM0C_RcdpWyB1eGjbJC-e5wv5wAKM4WmLMAS5KeF6EZJHB8Va3YqZUiaErqK_pxm&wd=&eqid=e8583fe70002d126000000065a99f864"; //"https://d.pcs.baidu.com/file/a02c89a2d479d4fd2756f3313d42491d?fid=4232431903-250528-1114369760340736&dstime=1525491372&rt=sh&sign=FDtAERVY-DCb740ccc5511e5e8fedcff06b081203-3C13vkOkuk4TqXvVYW05zj1K0ao%3D&expires=8h&chkv=1&chkbd=0&chkpc=et&dp-logid=8651730921842106225&dp-callid=0&r=165533013"; //"http://apk500.bce.baidu-mgame.com/game/67000/67734/20170622040827_oem_5502845.apk?r=1"; //"https://dl.genymotion.com/releases/genymotion-2.12.1/genymotion-2.12.1-vbox.exe"; //"http://9.9.9.59:5000/download/CentOS-7-x86_64-Minimal-1804.iso"; - "https://firmwareapi.azurewebsites.net/firmware-overview?name=A19_Filament_W_IMG0038_00102411-encrypted.ota"; + //"https://firmwareapi.azurewebsites.net/firmware-overview?name=A19_Filament_W_IMG0038_00102411-encrypted.ota"; @Bind(R.id.start) Button mStart; @Bind(R.id.stop) Button mStop; @Bind(R.id.cancel) Button mCancel; @@ -70,6 +70,7 @@ public class SingleTaskActivity extends BaseActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Aria.download(this).register(); + Log.d(TAG, "TAG========" + Aria.download(this).load(DOWNLOAD_URL).getTaskState() + ""); } /** diff --git a/build.gradle b/build.gradle index 1b8d2e3e..f50713aa 100644 --- a/build.gradle +++ b/build.gradle @@ -39,7 +39,7 @@ task clean(type: Delete) { ext { userOrg = 'arialyy' groupId = 'com.arialyy.aria' - publishVersion = '3.5.2_dev_4' + publishVersion = '3.5.2_dev_5' // publishVersion = '1.0.4' //FTP插件 repoName='maven' desc = 'android 下载框架'