From f6792b225aa2473f884f3720d65d4a3c9bfc2fe9 Mon Sep 17 00:00:00 2001 From: laoyuyu <511455842@qq.com> Date: Thu, 17 Jan 2019 20:22:49 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E4=BB=A3=E7=A0=81=EF=BC=8C?= =?UTF-8?q?=E7=A7=BB=E9=99=A4=E4=B8=8D=E5=BF=85=E8=A6=81=E7=9A=84TaskEntit?= =?UTF-8?q?y?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../core/command/normal/AbsNormalCmd.java | 4 +- .../core/command/normal/CancelAllCmd.java | 8 +- .../core/command/normal/ResumeAllCmd.java | 8 +- .../aria/core/command/normal/StartCmd.java | 62 +++--- .../arialyy/aria/core/common/AbsFileer.java | 4 +- .../aria/core/common/StateConstance.java | 4 +- .../aria/core/download/BaseNormalTarget.java | 6 +- .../DGEWrapper.java => DGEntityWrapper.java} | 6 +- .../aria/core/download/DGTaskWrapper.java | 10 +- .../aria/core/download/DTaskWrapper.java | 13 +- .../core/download/DownloadGroupEntity.java | 5 +- .../core/download/DownloadGroupTarget.java | 4 +- .../aria/core/download/DownloadReceiver.java | 19 +- .../core/download/FtpDirDownloadTarget.java | 4 +- .../download/downloader/FtpDirInfoThread.java | 3 +- .../core/download/wrapper/DGSTEWrapper.java | 44 ----- .../core/download/wrapper/DGTEWrapper.java | 65 ------- .../core/download/wrapper/DTEWrapper.java | 46 ----- .../aria/core/inf/AbsGroupTaskWrapper.java | 4 + .../com/arialyy/aria/core/inf/AbsTarget.java | 4 +- .../arialyy/aria/core/inf/AbsTaskWrapper.java | 16 ++ .../aria/core/manager/DGTEFactory.java | 168 ---------------- .../core/manager/DGTaskWrapperFactory.java | 90 +++++++++ ...EFactory.java => DTaskWrapperFactory.java} | 44 ++--- ...TEManager.java => TaskWrapperManager.java} | 179 +++++------------- ...EFactory.java => UTaskWrapperFactory.java} | 43 ++--- .../aria/core/scheduler/AbsSchedulers.java | 10 +- .../aria/core/upload/BaseNormalTarget.java | 4 +- .../aria/core/upload/UTaskWrapper.java | 13 +- .../aria/core/upload/UploadReceiver.java | 4 +- .../upload/uploader/FtpFileInfoThread.java | 4 +- .../aria/core/upload/wrapper/UTEWrapper.java | 47 ----- .../java/com/arialyy/aria/orm/DBConfig.java | 2 +- .../com/arialyy/aria/util/CommonUtil.java | 3 +- .../com/arialyy/aria/util/DbDataHelper.java | 115 +++++++++++ .../java/com/arialyy/aria/util/DbHelper.java | 42 ---- app/src/main/AndroidManifest.xml | 6 +- .../com/arialyy/simple/DbTestActivity.java | 4 +- .../simple/download/SingleTaskActivity.java | 6 +- .../download/group/DownloadGroupActivity.java | 2 +- 40 files changed, 398 insertions(+), 727 deletions(-) rename Aria/src/main/java/com/arialyy/aria/core/download/{wrapper/DGEWrapper.java => DGEntityWrapper.java} (85%) delete mode 100644 Aria/src/main/java/com/arialyy/aria/core/download/wrapper/DGSTEWrapper.java delete mode 100644 Aria/src/main/java/com/arialyy/aria/core/download/wrapper/DGTEWrapper.java delete mode 100644 Aria/src/main/java/com/arialyy/aria/core/download/wrapper/DTEWrapper.java delete mode 100644 Aria/src/main/java/com/arialyy/aria/core/manager/DGTEFactory.java create mode 100644 Aria/src/main/java/com/arialyy/aria/core/manager/DGTaskWrapperFactory.java rename Aria/src/main/java/com/arialyy/aria/core/manager/{DTEFactory.java => DTaskWrapperFactory.java} (69%) rename Aria/src/main/java/com/arialyy/aria/core/manager/{TEManager.java => TaskWrapperManager.java} (55%) rename Aria/src/main/java/com/arialyy/aria/core/manager/{UTEFactory.java => UTaskWrapperFactory.java} (57%) delete mode 100644 Aria/src/main/java/com/arialyy/aria/core/upload/wrapper/UTEWrapper.java create mode 100644 Aria/src/main/java/com/arialyy/aria/util/DbDataHelper.java delete mode 100644 Aria/src/main/java/com/arialyy/aria/util/DbHelper.java diff --git a/Aria/src/main/java/com/arialyy/aria/core/command/normal/AbsNormalCmd.java b/Aria/src/main/java/com/arialyy/aria/core/command/normal/AbsNormalCmd.java index 7099146f..976ebf9a 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/command/normal/AbsNormalCmd.java +++ b/Aria/src/main/java/com/arialyy/aria/core/command/normal/AbsNormalCmd.java @@ -24,7 +24,7 @@ import com.arialyy.aria.core.inf.AbsEntity; import com.arialyy.aria.core.inf.AbsTask; import com.arialyy.aria.core.inf.AbsTaskWrapper; import com.arialyy.aria.core.inf.IEntity; -import com.arialyy.aria.core.manager.TEManager; +import com.arialyy.aria.core.manager.TaskWrapperManager; import com.arialyy.aria.core.queue.DownloadGroupTaskQueue; import com.arialyy.aria.core.queue.DownloadTaskQueue; import com.arialyy.aria.core.queue.UploadTaskQueue; @@ -192,7 +192,7 @@ public abstract class AbsNormalCmd extends AbsCmd { * @return 创建的任务 */ AbsTask createTask(AbsTaskWrapper taskEntity) { - TEManager.getInstance().addTEntity(taskEntity); + TaskWrapperManager.getInstance().addTaskWrapper(taskEntity); return mQueue.createTask(taskEntity); } } \ No newline at end of file diff --git a/Aria/src/main/java/com/arialyy/aria/core/command/normal/CancelAllCmd.java b/Aria/src/main/java/com/arialyy/aria/core/command/normal/CancelAllCmd.java index bd845482..f173ac17 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/command/normal/CancelAllCmd.java +++ b/Aria/src/main/java/com/arialyy/aria/core/command/normal/CancelAllCmd.java @@ -21,7 +21,7 @@ import com.arialyy.aria.core.download.DTaskWrapper; import com.arialyy.aria.core.download.DownloadEntity; import com.arialyy.aria.core.download.DownloadGroupEntity; import com.arialyy.aria.core.inf.AbsTaskWrapper; -import com.arialyy.aria.core.manager.TEManager; +import com.arialyy.aria.core.manager.TaskWrapperManager; import com.arialyy.aria.core.queue.DownloadGroupTaskQueue; import com.arialyy.aria.core.queue.DownloadTaskQueue; import com.arialyy.aria.core.queue.UploadTaskQueue; @@ -64,7 +64,7 @@ public class CancelAllCmd extends AbsNormalCmd { DbEntity.findDatas(DownloadEntity.class, "isGroupChild=?", "false"); if (entities != null && !entities.isEmpty()) { for (DownloadEntity entity : entities) { - remove(TEManager.getInstance().getTEntity(DTaskWrapper.class, entity.getKey())); + remove(TaskWrapperManager.getInstance().getHttpTaskWrapper(DTaskWrapper.class, entity.getKey())); } } } @@ -78,7 +78,7 @@ public class CancelAllCmd extends AbsNormalCmd { if (entities != null && !entities.isEmpty()) { for (DownloadGroupEntity entity : entities) { remove( - TEManager.getInstance().getGTEntity(DGTaskWrapper.class, entity.getUrls())); + TaskWrapperManager.getInstance().getDGTaskWrapper(DGTaskWrapper.class, entity.getUrls())); } } } @@ -91,7 +91,7 @@ public class CancelAllCmd extends AbsNormalCmd { DbEntity.findDatas(UploadEntity.class, "isGroupChild=?", "false"); if (entities != null && !entities.isEmpty()) { for (UploadEntity entity : entities) { - remove(TEManager.getInstance().getTEntity(UTaskWrapper.class, entity.getKey())); + remove(TaskWrapperManager.getInstance().getHttpTaskWrapper(UTaskWrapper.class, entity.getKey())); } } } diff --git a/Aria/src/main/java/com/arialyy/aria/core/command/normal/ResumeAllCmd.java b/Aria/src/main/java/com/arialyy/aria/core/command/normal/ResumeAllCmd.java index 15d4770d..590e2af5 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/command/normal/ResumeAllCmd.java +++ b/Aria/src/main/java/com/arialyy/aria/core/command/normal/ResumeAllCmd.java @@ -8,7 +8,7 @@ import com.arialyy.aria.core.download.DownloadGroupEntity; import com.arialyy.aria.core.inf.AbsTask; import com.arialyy.aria.core.inf.AbsTaskWrapper; import com.arialyy.aria.core.inf.IEntity; -import com.arialyy.aria.core.manager.TEManager; +import com.arialyy.aria.core.manager.TaskWrapperManager; import com.arialyy.aria.core.queue.DownloadGroupTaskQueue; import com.arialyy.aria.core.queue.DownloadTaskQueue; import com.arialyy.aria.core.queue.UploadTaskQueue; @@ -65,7 +65,7 @@ final class ResumeAllCmd extends AbsNormalCmd { //if (TextUtils.isEmpty(entity.getDownloadPath())){ // continue; //} - resumeTask(TEManager.getInstance().getTEntity(DTaskWrapper.class, entity.getKey())); + resumeTask(TaskWrapperManager.getInstance().getHttpTaskWrapper(DTaskWrapper.class, entity.getKey())); } } } else if (type == 2) { @@ -74,7 +74,7 @@ final class ResumeAllCmd extends AbsNormalCmd { if (entities != null && !entities.isEmpty()) { for (DownloadGroupEntity entity : entities) { resumeTask( - TEManager.getInstance().getGTEntity(DGTaskWrapper.class, entity.getUrls())); + TaskWrapperManager.getInstance().getDGTaskWrapper(DGTaskWrapper.class, entity.getUrls())); } } } else if (type == 3) { @@ -82,7 +82,7 @@ final class ResumeAllCmd extends AbsNormalCmd { DbEntity.findDatas(UploadEntity.class, "state!=? ORDER BY stopTime DESC", "1"); if (entities != null && !entities.isEmpty()) { for (UploadEntity entity : entities) { - resumeTask(TEManager.getInstance().getTEntity(UTaskWrapper.class, entity.getKey())); + resumeTask(TaskWrapperManager.getInstance().getHttpTaskWrapper(UTaskWrapper.class, entity.getKey())); } } } diff --git a/Aria/src/main/java/com/arialyy/aria/core/command/normal/StartCmd.java b/Aria/src/main/java/com/arialyy/aria/core/command/normal/StartCmd.java index 8969ed1a..75220953 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/command/normal/StartCmd.java +++ b/Aria/src/main/java/com/arialyy/aria/core/command/normal/StartCmd.java @@ -20,19 +20,22 @@ import com.arialyy.aria.core.AriaManager; import com.arialyy.aria.core.common.QueueMod; import com.arialyy.aria.core.download.DGTaskWrapper; import com.arialyy.aria.core.download.DTaskWrapper; -import com.arialyy.aria.core.download.wrapper.DGTEWrapper; -import com.arialyy.aria.core.download.wrapper.DTEWrapper; +import com.arialyy.aria.core.download.DownloadEntity; +import com.arialyy.aria.core.download.DownloadGroupEntity; import com.arialyy.aria.core.inf.AbsTask; import com.arialyy.aria.core.inf.AbsTaskWrapper; import com.arialyy.aria.core.inf.IEntity; +import com.arialyy.aria.core.manager.TaskWrapperManager; import com.arialyy.aria.core.queue.DownloadGroupTaskQueue; import com.arialyy.aria.core.queue.DownloadTaskQueue; import com.arialyy.aria.core.queue.UploadTaskQueue; import com.arialyy.aria.core.upload.UTaskWrapper; -import com.arialyy.aria.core.upload.wrapper.UTEWrapper; +import com.arialyy.aria.core.upload.UploadEntity; +import com.arialyy.aria.orm.AbsWrapper; import com.arialyy.aria.orm.DbEntity; import com.arialyy.aria.util.ALog; import com.arialyy.aria.util.CommonUtil; +import com.arialyy.aria.util.DbDataHelper; import com.arialyy.aria.util.NetUtils; import java.util.ArrayList; import java.util.List; @@ -69,7 +72,7 @@ class StartCmd extends AbsNormalCmd { startTask(); } else if (mod.equals(QueueMod.WAIT.getTag())) { int state = task.getState(); - if (mQueue.getCurrentExePoolNum() < maxTaskNum){ + if (mQueue.getCurrentExePoolNum() < maxTaskNum) { if (state == IEntity.STATE_STOP || task.getState() == IEntity.STATE_FAIL || task.getState() == IEntity.STATE_OTHER @@ -77,7 +80,7 @@ class StartCmd extends AbsNormalCmd { || task.getState() == IEntity.STATE_POST_PRE || task.getState() == IEntity.STATE_COMPLETE) { resumeTask(); - }else { + } else { startTask(); } } else { @@ -117,28 +120,41 @@ class StartCmd extends AbsNormalCmd { private List findWaitData(int type) { List waitList = new ArrayList<>(); - if (type == 1) { - List wrappers = DbEntity.findRelationData(DTEWrapper.class, - "DTaskWrapper.isGroupTask=? and DTaskWrapper.state=?", "false", "3"); - if (wrappers != null && !wrappers.isEmpty()) { - for (DTEWrapper w : wrappers) { - waitList.add(w.taskEntity); + TaskWrapperManager tManager = TaskWrapperManager.getInstance(); + if (type == 1) { // 普通下载任务 + List dEntities = DbEntity.findDatas(DownloadEntity.class, + "isGroupChild=? and state=?", "false", "3"); + if (dEntities != null && !dEntities.isEmpty()) { + for (DownloadEntity e : dEntities) { + if (e.getTaskType() == AbsTaskWrapper.D_FTP) { + waitList.add(tManager.getFtpTaskWrapper(DTaskWrapper.class, e.getKey())); + } else if (e.getTaskType() == AbsTaskWrapper.D_HTTP) { + waitList.add(tManager.getHttpTaskWrapper(DTaskWrapper.class, e.getKey())); + } } } - } else if (type == 2) { - List wrappers = - DbEntity.findRelationData(DGTEWrapper.class, "DGTaskWrapper.state=?", "3"); - if (wrappers != null && !wrappers.isEmpty()) { - for (DGTEWrapper w : wrappers) { - waitList.add(w.taskEntity); + } else if (type == 2) { // 组合任务 + List dEntities = + DbEntity.findDatas(DownloadGroupEntity.class, "state=?", "3"); + if (dEntities != null && !dEntities.isEmpty()) { + for (DownloadGroupEntity e : dEntities) { + if (e.getTaskType() == AbsTaskWrapper.DG_HTTP) { + waitList.add(tManager.getDGTaskWrapper(DGTaskWrapper.class, e.getUrls())); + } else if (e.getTaskType() == AbsTaskWrapper.D_FTP_DIR) { + waitList.add(tManager.getFtpTaskWrapper(DGTaskWrapper.class, e.getKey())); + } } } - } else if (type == 3) { - List wrappers = DbEntity.findRelationData(UTEWrapper.class, - "UTaskWrapper.state=?", "3"); - if (wrappers != null && !wrappers.isEmpty()) { - for (UTEWrapper w : wrappers) { - waitList.add(w.taskEntity); + } else if (type == 3) { //普通上传任务 + List dEntities = DbEntity.findDatas(UploadEntity.class, "state=?", "3"); + + if (dEntities != null && !dEntities.isEmpty()) { + for (UploadEntity e : dEntities) { + if (e.getTaskType() == AbsTaskWrapper.D_FTP) { + waitList.add(tManager.getFtpTaskWrapper(UTaskWrapper.class, e.getKey())); + } else if (e.getTaskType() == AbsTaskWrapper.D_HTTP) { + waitList.add(tManager.getHttpTaskWrapper(UTaskWrapper.class, e.getKey())); + } } } } diff --git a/Aria/src/main/java/com/arialyy/aria/core/common/AbsFileer.java b/Aria/src/main/java/com/arialyy/aria/core/common/AbsFileer.java index 75fa6613..c8cca845 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/common/AbsFileer.java +++ b/Aria/src/main/java/com/arialyy/aria/core/common/AbsFileer.java @@ -28,7 +28,7 @@ import com.arialyy.aria.core.manager.ThreadTaskManager; import com.arialyy.aria.orm.DbEntity; import com.arialyy.aria.util.ALog; import com.arialyy.aria.util.CommonUtil; -import com.arialyy.aria.util.DbHelper; +import com.arialyy.aria.util.DbDataHelper; import java.io.File; import java.util.ArrayList; import java.util.HashSet; @@ -305,7 +305,7 @@ public abstract class AbsFileer void initTarget(String url, String targetName) { this.url = url; mTargetName = targetName; - mTaskWrapper = TEManager.getInstance().getTEntity(DTaskWrapper.class, url); + mTaskWrapper = TaskWrapperManager.getInstance().getHttpTaskWrapper(DTaskWrapper.class, url); mEntity = mTaskWrapper.getEntity(); if (mEntity != null) { @@ -164,7 +164,7 @@ abstract class BaseNormalTarget } else { ALog.w(TAG, "保存路径【" + filePath + "】已经被其它任务占用,当前任务将覆盖该路径的文件"); CommonUtil.delTaskRecord(filePath, 1); - mTaskWrapper = TEManager.getInstance().getTEntity(DTaskWrapper.class, url); + mTaskWrapper = TaskWrapperManager.getInstance().getHttpTaskWrapper(DTaskWrapper.class, url); } } File oldFile = new File(mEntity.getDownloadPath()); diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/wrapper/DGEWrapper.java b/Aria/src/main/java/com/arialyy/aria/core/download/DGEntityWrapper.java similarity index 85% rename from Aria/src/main/java/com/arialyy/aria/core/download/wrapper/DGEWrapper.java rename to Aria/src/main/java/com/arialyy/aria/core/download/DGEntityWrapper.java index 0c50181d..143aabf9 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/wrapper/DGEWrapper.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/DGEntityWrapper.java @@ -13,10 +13,8 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.arialyy.aria.core.download.wrapper; +package com.arialyy.aria.core.download; -import com.arialyy.aria.core.download.DownloadEntity; -import com.arialyy.aria.core.download.DownloadGroupEntity; import com.arialyy.aria.orm.AbsWrapper; import com.arialyy.aria.orm.annotation.Many; import com.arialyy.aria.orm.annotation.One; @@ -28,7 +26,7 @@ import java.util.List; * 任务组实体和子任务实体的关系 */ @Wrapper -public class DGEWrapper extends AbsWrapper { +public class DGEntityWrapper extends AbsWrapper { @One public DownloadGroupEntity groupEntity; diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/DGTaskWrapper.java b/Aria/src/main/java/com/arialyy/aria/core/download/DGTaskWrapper.java index 5a996c1f..7832d539 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/DGTaskWrapper.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/DGTaskWrapper.java @@ -23,18 +23,12 @@ import java.util.List; */ public class DGTaskWrapper extends AbsGroupTaskWrapper { - private DownloadGroupEntity entity; - private List subTaskEntities; private String key; - @Override public DownloadGroupEntity getEntity() { - return entity; - } - - public void setEntity(DownloadGroupEntity entity) { - this.entity = entity; + public DGTaskWrapper(DownloadGroupEntity entity) { + super(entity); } @Override diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/DTaskWrapper.java b/Aria/src/main/java/com/arialyy/aria/core/download/DTaskWrapper.java index e5bbf65c..fb83618f 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/DTaskWrapper.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/DTaskWrapper.java @@ -22,8 +22,6 @@ import com.arialyy.aria.core.inf.AbsTaskWrapper; */ public class DTaskWrapper extends AbsTaskWrapper { - private DownloadEntity entity; - /** * 任务的url */ @@ -44,11 +42,8 @@ public class DTaskWrapper extends AbsTaskWrapper { */ private String key; - public DTaskWrapper() { - } - - @Override public DownloadEntity getEntity() { - return entity; + public DTaskWrapper(DownloadEntity entity) { + super(entity); } @Override public String getKey() { @@ -71,10 +66,6 @@ public class DTaskWrapper extends AbsTaskWrapper { return isGroupTask; } - public void setEntity(DownloadEntity entity) { - this.entity = entity; - } - public void setUrl(String url) { this.url = url; } diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadGroupEntity.java b/Aria/src/main/java/com/arialyy/aria/core/download/DownloadGroupEntity.java index e2845454..7174120d 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadGroupEntity.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/DownloadGroupEntity.java @@ -22,15 +22,12 @@ import com.arialyy.aria.orm.annotation.Ignore; import java.util.List; /** - * Created by AriaL on 2017/6/29. - * 下载任务组实体 + * Created by AriaL on 2017/6/29. 下载任务组实体 */ public class DownloadGroupEntity extends AbsGroupEntity { @Ignore private List subEntities; - - /** * 子任务实体列表 */ diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadGroupTarget.java b/Aria/src/main/java/com/arialyy/aria/core/download/DownloadGroupTarget.java index bd1f2cc4..635e2c92 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadGroupTarget.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/DownloadGroupTarget.java @@ -22,7 +22,7 @@ import com.arialyy.aria.core.common.RequestEnum; import com.arialyy.aria.core.common.http.HttpHeaderDelegate; import com.arialyy.aria.core.common.http.PostDelegate; import com.arialyy.aria.core.inf.IHttpHeaderDelegate; -import com.arialyy.aria.core.manager.TEManager; +import com.arialyy.aria.core.manager.TaskWrapperManager; import com.arialyy.aria.orm.DbEntity; import com.arialyy.aria.util.ALog; import com.arialyy.aria.util.CommonUtil; @@ -67,7 +67,7 @@ public class DownloadGroupTarget extends BaseGroupTarget im private void init() { mGroupName = CommonUtil.getMd5Code(mUrls); - mTaskWrapper = TEManager.getInstance().getGTEntity(DGTaskWrapper.class, mUrls); + mTaskWrapper = TaskWrapperManager.getInstance().getDGTaskWrapper(DGTaskWrapper.class, mUrls); mEntity = mTaskWrapper.getEntity(); if (mEntity != null) { mDirPathTemp = mEntity.getDirPath(); diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadReceiver.java b/Aria/src/main/java/com/arialyy/aria/core/download/DownloadReceiver.java index 3f3baee0..18f17a9f 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadReceiver.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/DownloadReceiver.java @@ -23,11 +23,10 @@ import com.arialyy.aria.core.command.ICmd; import com.arialyy.aria.core.command.normal.CancelAllCmd; import com.arialyy.aria.core.command.normal.NormalCmdFactory; import com.arialyy.aria.core.common.ProxyHelper; -import com.arialyy.aria.core.download.wrapper.DGEWrapper; import com.arialyy.aria.core.inf.AbsEntity; import com.arialyy.aria.core.inf.AbsReceiver; import com.arialyy.aria.core.inf.ReceiverType; -import com.arialyy.aria.core.manager.TEManager; +import com.arialyy.aria.core.manager.TaskWrapperManager; import com.arialyy.aria.core.scheduler.DownloadGroupSchedulers; import com.arialyy.aria.core.scheduler.DownloadSchedulers; import com.arialyy.aria.orm.DbEntity; @@ -243,7 +242,7 @@ public class DownloadReceiver extends AbsReceiver { if (!taskExists(downloadUrl)) { return null; } - return TEManager.getInstance().getTEntity(DTaskWrapper.class, downloadUrl); + return TaskWrapperManager.getInstance().getHttpTaskWrapper(DTaskWrapper.class, downloadUrl); } /** @@ -260,7 +259,7 @@ public class DownloadReceiver extends AbsReceiver { if (!taskExists(urls)) { return null; } - return TEManager.getInstance().getGTEntity(DGTaskWrapper.class, urls); + return TaskWrapperManager.getInstance().getDGTaskWrapper(DGTaskWrapper.class, urls); } /** @@ -279,7 +278,7 @@ public class DownloadReceiver extends AbsReceiver { if (!b) { return null; } - return TEManager.getInstance().getFDTEntity(DGTaskWrapper.class, dirUrl); + return TaskWrapperManager.getInstance().getFtpTaskWrapper(DGTaskWrapper.class, dirUrl); } /** @@ -336,12 +335,12 @@ public class DownloadReceiver extends AbsReceiver { * @return 如果没有任务组列表,则返回null */ public List getGroupTaskList() { - List wrappers = DbEntity.findRelationData(DGEWrapper.class); + List wrappers = DbEntity.findRelationData(DGEntityWrapper.class); if (wrappers == null || wrappers.isEmpty()) { return null; } List entities = new ArrayList<>(); - for (DGEWrapper wrapper : wrappers) { + for (DGEntityWrapper wrapper : wrappers) { entities.add(wrapper.groupEntity); } return entities; @@ -369,7 +368,7 @@ public class DownloadReceiver extends AbsReceiver { public void stopAllTask() { AriaManager.getInstance(AriaManager.APP) .setCmd(NormalCmdFactory.getInstance() - .createCmd(new DTaskWrapper(), NormalCmdFactory.TASK_STOP_ALL, + .createCmd(new DTaskWrapper(null), NormalCmdFactory.TASK_STOP_ALL, ICmd.TASK_TYPE_DOWNLOAD)) .exe(); } @@ -382,7 +381,7 @@ public class DownloadReceiver extends AbsReceiver { public void resumeAllTask() { AriaManager.getInstance(AriaManager.APP) .setCmd(NormalCmdFactory.getInstance() - .createCmd(new DTaskWrapper(), NormalCmdFactory.TASK_RESUME_ALL, + .createCmd(new DTaskWrapper(null), NormalCmdFactory.TASK_RESUME_ALL, ICmd.TASK_TYPE_DOWNLOAD)) .exe(); } @@ -396,7 +395,7 @@ public class DownloadReceiver extends AbsReceiver { public void removeAllTask(boolean removeFile) { final AriaManager ariaManager = AriaManager.getInstance(AriaManager.APP); CancelAllCmd cancelCmd = - (CancelAllCmd) CommonUtil.createNormalCmd(new DTaskWrapper(), + (CancelAllCmd) CommonUtil.createNormalCmd(new DTaskWrapper(null), NormalCmdFactory.TASK_CANCEL_ALL, ICmd.TASK_TYPE_DOWNLOAD); cancelCmd.removeFile = removeFile; ariaManager.setCmd(cancelCmd).exe(); diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/FtpDirDownloadTarget.java b/Aria/src/main/java/com/arialyy/aria/core/download/FtpDirDownloadTarget.java index 203c2439..ad8f3b00 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/FtpDirDownloadTarget.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/FtpDirDownloadTarget.java @@ -22,7 +22,7 @@ import com.arialyy.aria.core.common.ftp.FTPSConfig; import com.arialyy.aria.core.common.ftp.FtpDelegate; import com.arialyy.aria.core.inf.AbsTaskWrapper; import com.arialyy.aria.core.inf.IFtpTarget; -import com.arialyy.aria.core.manager.TEManager; +import com.arialyy.aria.core.manager.TaskWrapperManager; import com.arialyy.aria.util.ALog; import java.net.Proxy; @@ -41,7 +41,7 @@ public class FtpDirDownloadTarget extends BaseGroupTarget private void init(String key) { mGroupName = key; - mTaskWrapper = TEManager.getInstance().getFDTEntity(DGTaskWrapper.class, key); + mTaskWrapper = TaskWrapperManager.getInstance().getFtpTaskWrapper(DGTaskWrapper.class, key); mTaskWrapper.setRequestType(AbsTaskWrapper.D_FTP_DIR); mEntity = mTaskWrapper.getEntity(); if (mEntity != null) { diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/FtpDirInfoThread.java b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/FtpDirInfoThread.java index f9ffae7c..4163c825 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/FtpDirInfoThread.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/FtpDirInfoThread.java @@ -72,10 +72,9 @@ class FtpDirInfoThread extends AbsFtpInfoThread subTaskEntity; - - @Override protected void handleConvert() { - if (subTaskEntity != null && !subTaskEntity.isEmpty()) { - dgTaskEntity.setSubTaskWrapper(subTaskEntity); - } - } -} diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/wrapper/DGTEWrapper.java b/Aria/src/main/java/com/arialyy/aria/core/download/wrapper/DGTEWrapper.java deleted file mode 100644 index d0497e7b..00000000 --- a/Aria/src/main/java/com/arialyy/aria/core/download/wrapper/DGTEWrapper.java +++ /dev/null @@ -1,65 +0,0 @@ -/* - * Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria) - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.arialyy.aria.core.download.wrapper; - -import com.arialyy.aria.core.download.DGTaskWrapper; -import com.arialyy.aria.core.download.DownloadGroupEntity; -import com.arialyy.aria.core.download.DTaskWrapper; -import com.arialyy.aria.core.inf.AbsTaskWrapper; -import com.arialyy.aria.orm.AbsWrapper; -import com.arialyy.aria.orm.DbEntity; -import com.arialyy.aria.orm.annotation.Many; -import com.arialyy.aria.orm.annotation.One; -import com.arialyy.aria.orm.annotation.Wrapper; -import com.arialyy.aria.util.CommonUtil; -import java.util.ArrayList; -import java.util.List; - -/** - * Created by laoyuyu on 2018/3/30. - * 任务组实体和任务组任务实体的关系 - */ -@Wrapper -public class DGTEWrapper extends AbsWrapper { - - @One - public DownloadGroupEntity entity; - - @Many(parentColumn = "groupName", entityColumn = "key") - private List taskEntitys; - - public DGTaskWrapper taskEntity; - - @Override protected void handleConvert() { - taskEntity = (taskEntitys == null || taskEntitys.isEmpty()) ? null : taskEntitys.get(0); - if (taskEntity != null) { - taskEntity.setEntity(entity); - List subWrappers = - DbEntity.findRelationData(DTEWrapper.class, "DTaskWrapper.groupName=?", - taskEntity.getKey()); - if (subWrappers != null && !subWrappers.isEmpty()) { - List temp = new ArrayList<>(); - for (DTEWrapper dw : subWrappers) { - if (dw.taskEntity.getRequestType() == AbsTaskWrapper.D_FTP) { - dw.taskEntity.asFtp().setUrlEntity(CommonUtil.getFtpUrlInfo(dw.taskEntity.getUrl())); - } - temp.add(dw.taskEntity); - } - taskEntity.setSubTaskWrapper(temp); - } - } - } -} diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/wrapper/DTEWrapper.java b/Aria/src/main/java/com/arialyy/aria/core/download/wrapper/DTEWrapper.java deleted file mode 100644 index 74782405..00000000 --- a/Aria/src/main/java/com/arialyy/aria/core/download/wrapper/DTEWrapper.java +++ /dev/null @@ -1,46 +0,0 @@ -/* - * Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria) - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.arialyy.aria.core.download.wrapper; - -import com.arialyy.aria.core.download.DownloadEntity; -import com.arialyy.aria.core.download.DTaskWrapper; -import com.arialyy.aria.orm.AbsWrapper; -import com.arialyy.aria.orm.annotation.Many; -import com.arialyy.aria.orm.annotation.One; -import com.arialyy.aria.orm.annotation.Wrapper; -import java.util.List; - -/** - * Created by laoyuyu on 2018/3/30. - */ -@Wrapper -public class DTEWrapper extends AbsWrapper { - - @One - public DownloadEntity entity; - - @Many(parentColumn = "downloadPath", entityColumn = "key") - private List taskEntitys = null; - - public DTaskWrapper taskEntity; - - @Override public void handleConvert() { - taskEntity = (taskEntitys == null || taskEntitys.isEmpty()) ? null : taskEntitys.get(0); - if (taskEntity != null) { - taskEntity.setEntity(entity); - } - } -} diff --git a/Aria/src/main/java/com/arialyy/aria/core/inf/AbsGroupTaskWrapper.java b/Aria/src/main/java/com/arialyy/aria/core/inf/AbsGroupTaskWrapper.java index 0bc856f3..321e6964 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/inf/AbsGroupTaskWrapper.java +++ b/Aria/src/main/java/com/arialyy/aria/core/inf/AbsGroupTaskWrapper.java @@ -24,6 +24,10 @@ import java.util.List; public abstract class AbsGroupTaskWrapper extends AbsTaskWrapper { + public AbsGroupTaskWrapper(ENTITY entity) { + super(entity); + } + public abstract List getSubTaskWrapper(); public abstract void setSubTaskWrapper(List subTaskWrapper); diff --git a/Aria/src/main/java/com/arialyy/aria/core/inf/AbsTarget.java b/Aria/src/main/java/com/arialyy/aria/core/inf/AbsTarget.java index a100a1fc..80dded92 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/inf/AbsTarget.java +++ b/Aria/src/main/java/com/arialyy/aria/core/inf/AbsTarget.java @@ -25,7 +25,7 @@ import com.arialyy.aria.core.common.TaskRecord; import com.arialyy.aria.core.download.DGTaskWrapper; import com.arialyy.aria.core.download.DownloadGroupEntity; import com.arialyy.aria.core.download.DTaskWrapper; -import com.arialyy.aria.core.manager.TEManager; +import com.arialyy.aria.core.manager.TaskWrapperManager; import com.arialyy.aria.core.upload.UTaskWrapper; import com.arialyy.aria.orm.DbEntity; import com.arialyy.aria.util.ALog; @@ -78,7 +78,7 @@ public abstract class AbsTarget */ private int code; + /** + * {@link DownloadEntity} or {@link UploadEntity} or {@link DownloadGroupEntity} + */ + private ENTITY entity; + + public AbsTaskWrapper(ENTITY entity) { + this.entity = entity; + } + public abstract String getKey(); public abstract void setKey(String key); + @Override public ENTITY getEntity() { + return entity; + } + /** * 设置或获取HTTP任务相关参数 */ diff --git a/Aria/src/main/java/com/arialyy/aria/core/manager/DGTEFactory.java b/Aria/src/main/java/com/arialyy/aria/core/manager/DGTEFactory.java deleted file mode 100644 index e19240bd..00000000 --- a/Aria/src/main/java/com/arialyy/aria/core/manager/DGTEFactory.java +++ /dev/null @@ -1,168 +0,0 @@ -/* - * Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria) - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.arialyy.aria.core.manager; - -import com.arialyy.aria.core.download.DGTaskWrapper; -import com.arialyy.aria.core.download.DTaskWrapper; -import com.arialyy.aria.core.download.DownloadEntity; -import com.arialyy.aria.core.download.DownloadGroupEntity; -import com.arialyy.aria.core.download.wrapper.DGEWrapper; -import com.arialyy.aria.core.download.wrapper.DGTEWrapper; -import com.arialyy.aria.orm.DbEntity; -import com.arialyy.aria.util.CommonUtil; -import java.io.File; -import java.util.ArrayList; -import java.util.List; - -/** - * Created by Aria.Lao on 2017/11/1. - * 任务实体工厂 - */ -class DGTEFactory implements IGTEFactory { - private static final String TAG = "DTEFactory"; - private static volatile DGTEFactory INSTANCE = null; - - private DGTEFactory() { - } - - public static DGTEFactory getInstance() { - if (INSTANCE == null) { - synchronized (DGTEFactory.class) { - INSTANCE = new DGTEFactory(); - } - } - return INSTANCE; - } - - @Override public DGTaskWrapper getGTE(String groupName, List urls) { - DownloadGroupEntity entity = createDGroupEntity(groupName, urls); - List wrapper = - DbEntity.findRelationData(DGTEWrapper.class, "DGTaskWrapper.key=?", - entity.getGroupName()); - DGTaskWrapper gte; - - if (wrapper != null && !wrapper.isEmpty()) { - gte = wrapper.get(0).taskEntity; - if (gte == null) { - // 创建新的任务组任务实体 - gte = new DGTaskWrapper(); - } - } else { - gte = new DGTaskWrapper(); - } - gte.setSubTaskWrapper(createDGSubTaskEntity(entity)); - gte.setKey(entity.getGroupName()); - gte.setEntity(entity); - - return gte; - } - - @Override public DGTaskWrapper getFTE(String ftpUrl) { - List wrapper = - DbEntity.findRelationData(DGTEWrapper.class, "DGTaskWrapper.key=?", - ftpUrl); - DGTaskWrapper fte; - - if (wrapper != null && !wrapper.isEmpty()) { - fte = wrapper.get(0).taskEntity; - if (fte == null) { - fte = new DGTaskWrapper(); - DownloadGroupEntity dge = new DownloadGroupEntity(); - dge.setGroupName(ftpUrl); - fte.setEntity(dge); - } else if (fte.getEntity() == null) { - DownloadGroupEntity dge = new DownloadGroupEntity(); - dge.setGroupName(ftpUrl); - fte.setEntity(dge); - } - } else { - fte = new DGTaskWrapper(); - DownloadGroupEntity dge = new DownloadGroupEntity(); - dge.setGroupName(ftpUrl); - fte.setEntity(dge); - } - fte.setKey(ftpUrl); - fte.asFtp().setUrlEntity(CommonUtil.getFtpUrlInfo(ftpUrl)); - - if (fte.getEntity().getSubEntities() == null) { - fte.getEntity().setSubEntities(new ArrayList()); - } - if (fte.getSubTaskWrapper() == null) { - fte.setSubTaskWrapper(new ArrayList()); - } - return fte; - } - - /** - * 创建任务组子任务的任务实体 - */ - private List createDGSubTaskEntity(DownloadGroupEntity dge) { - List list = new ArrayList<>(); - for (DownloadEntity entity : dge.getSubEntities()) { - DTaskWrapper taskEntity = new DTaskWrapper(); - taskEntity.setEntity(entity); - taskEntity.setKey(entity.getDownloadPath()); - taskEntity.setGroupName(dge.getKey()); - taskEntity.setGroupTask(true); - taskEntity.setUrl(entity.getUrl()); - list.add(taskEntity); - } - return list; - } - - /** - * 查询任务组实体,如果数据库不存在该实体,则新创建一个新的任务组实体 - */ - private DownloadGroupEntity createDGroupEntity(String groupName, List urls) { - List wrapper = - DbEntity.findRelationData(DGEWrapper.class, "DownloadGroupEntity.groupName=?", - groupName); - - DownloadGroupEntity groupEntity; - if (wrapper != null && !wrapper.isEmpty()) { - groupEntity = wrapper.get(0).groupEntity; - if (groupEntity == null) { - groupEntity = new DownloadGroupEntity(); - groupEntity.setSubEntities(createSubTask(groupName, urls)); - } - } else { - groupEntity = new DownloadGroupEntity(); - groupEntity.setSubEntities(createSubTask(groupName, urls)); - } - groupEntity.setGroupName(groupName); - groupEntity.setUrls(urls); - return groupEntity; - } - - /** - * 创建子任务 - */ - private List createSubTask(String groupName, List urls) { - List list = new ArrayList<>(); - for (int i = 0, len = urls.size(); i < len; i++) { - String url = urls.get(i); - DownloadEntity entity = new DownloadEntity(); - entity.setUrl(url); - entity.setDownloadPath(groupName + "_" + i); - int lastIndex = url.lastIndexOf(File.separator); - entity.setFileName(url.substring(lastIndex + 1, url.length())); - entity.setGroupName(groupName); - entity.setGroupChild(true); - list.add(entity); - } - return list; - } -} diff --git a/Aria/src/main/java/com/arialyy/aria/core/manager/DGTaskWrapperFactory.java b/Aria/src/main/java/com/arialyy/aria/core/manager/DGTaskWrapperFactory.java new file mode 100644 index 00000000..75b5c4a5 --- /dev/null +++ b/Aria/src/main/java/com/arialyy/aria/core/manager/DGTaskWrapperFactory.java @@ -0,0 +1,90 @@ +/* + * Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.arialyy.aria.core.manager; + +import com.arialyy.aria.core.download.DGTaskWrapper; +import com.arialyy.aria.core.download.DTaskWrapper; +import com.arialyy.aria.core.download.DownloadEntity; +import com.arialyy.aria.core.download.DownloadGroupEntity; +import com.arialyy.aria.core.download.DGEntityWrapper; +import com.arialyy.aria.orm.DbEntity; +import com.arialyy.aria.util.CommonUtil; +import com.arialyy.aria.util.DbDataHelper; +import java.io.File; +import java.util.ArrayList; +import java.util.List; + +/** + * Created by Aria.Lao on 2017/11/1. 组合任务wrapper + */ +class DGTaskWrapperFactory implements IGTEFactory { + private static final String TAG = "DTaskWrapperFactory"; + private static volatile DGTaskWrapperFactory INSTANCE = null; + + private DGTaskWrapperFactory() { + } + + public static DGTaskWrapperFactory getInstance() { + if (INSTANCE == null) { + synchronized (DGTaskWrapperFactory.class) { + INSTANCE = new DGTaskWrapperFactory(); + } + } + return INSTANCE; + } + + @Override public DGTaskWrapper getGTE(String groupName, List urls) { + DownloadGroupEntity entity = DbDataHelper.getHttpDGEntity(groupName, urls); + DGTaskWrapper wrapper = new DGTaskWrapper(entity); + wrapper.setSubTaskWrapper(createDGSubTaskWrapper(entity)); + wrapper.setKey(entity.getGroupName()); + return wrapper; + } + + @Override public DGTaskWrapper getFTE(String ftpUrl) { + DownloadGroupEntity entity = DbDataHelper.getFtpDGEntity(ftpUrl); + DGTaskWrapper fte = new DGTaskWrapper(entity); + + fte.setKey(ftpUrl); + fte.asFtp().setUrlEntity(CommonUtil.getFtpUrlInfo(ftpUrl)); + + if (fte.getEntity().getSubEntities() == null) { + fte.getEntity().setSubEntities(new ArrayList()); + } + if (fte.getSubTaskWrapper() == null) { + fte.setSubTaskWrapper(new ArrayList()); + } + return fte; + } + + /** + * 创建任务组子任务的任务实体 + */ + private List createDGSubTaskWrapper(DownloadGroupEntity dge) { + List list = new ArrayList<>(); + for (DownloadEntity entity : dge.getSubEntities()) { + DTaskWrapper taskEntity = new DTaskWrapper(entity); + taskEntity.setKey(entity.getDownloadPath()); + taskEntity.setGroupName(dge.getKey()); + taskEntity.setGroupTask(true); + taskEntity.setUrl(entity.getUrl()); + list.add(taskEntity); + } + return list; + } + + +} diff --git a/Aria/src/main/java/com/arialyy/aria/core/manager/DTEFactory.java b/Aria/src/main/java/com/arialyy/aria/core/manager/DTaskWrapperFactory.java similarity index 69% rename from Aria/src/main/java/com/arialyy/aria/core/manager/DTEFactory.java rename to Aria/src/main/java/com/arialyy/aria/core/manager/DTaskWrapperFactory.java index da428909..60d9effb 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/manager/DTEFactory.java +++ b/Aria/src/main/java/com/arialyy/aria/core/manager/DTaskWrapperFactory.java @@ -15,33 +15,28 @@ */ 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.DTaskWrapper; import com.arialyy.aria.core.download.DownloadEntity; -import com.arialyy.aria.core.download.wrapper.DTEWrapper; import com.arialyy.aria.core.inf.IEntity; -import com.arialyy.aria.orm.DbEntity; import java.io.File; -import java.util.List; import java.util.UUID; /** - * Created by Aria.Lao on 2017/11/1. - * 任务实体工厂 + * 创建下载任务wrapper Created by Aria.Lao on 2017/11/1. */ -class DTEFactory implements INormalTEFactory { - private static final String TAG = "DTEFactory"; - private static volatile DTEFactory INSTANCE = null; +class DTaskWrapperFactory implements INormalTEFactory { + private static final String TAG = "DTaskWrapperFactory"; + private static volatile DTaskWrapperFactory INSTANCE = null; - private DTEFactory() { + private DTaskWrapperFactory() { } - public static DTEFactory getInstance() { + public static DTaskWrapperFactory getInstance() { if (INSTANCE == null) { - synchronized (DTEFactory.class) { - INSTANCE = new DTEFactory(); + synchronized (DTaskWrapperFactory.class) { + INSTANCE = new DTaskWrapperFactory(); } } return INSTANCE; @@ -51,25 +46,10 @@ class DTEFactory implements INormalTEFactory { * 通过下载实体创建任务实体 */ private DTaskWrapper create(DownloadEntity entity) { - List wrapper = DbEntity.findRelationData(DTEWrapper.class, - "DTaskWrapper.key=? and DTaskWrapper.isGroupTask='false' and DTaskWrapper.url=?", - entity.getDownloadPath(), entity.getUrl()); - DTaskWrapper taskEntity; - if (wrapper != null && !wrapper.isEmpty()) { - taskEntity = wrapper.get(0).taskEntity; - if (taskEntity == null) { - taskEntity = new DTaskWrapper(); - } else if (taskEntity.getEntity() == null || TextUtils.isEmpty( - taskEntity.getEntity().getUrl())) { - taskEntity.setEntity(entity); - } - } else { - taskEntity = new DTaskWrapper(); - } - taskEntity.setKey(entity.getDownloadPath()); - taskEntity.setUrl(entity.getUrl()); - taskEntity.setEntity(entity); - return taskEntity; + DTaskWrapper wrapper = new DTaskWrapper(entity); + wrapper.setKey(entity.getDownloadPath()); + wrapper.setUrl(entity.getUrl()); + return wrapper; } /** diff --git a/Aria/src/main/java/com/arialyy/aria/core/manager/TEManager.java b/Aria/src/main/java/com/arialyy/aria/core/manager/TaskWrapperManager.java similarity index 55% rename from Aria/src/main/java/com/arialyy/aria/core/manager/TEManager.java rename to Aria/src/main/java/com/arialyy/aria/core/manager/TaskWrapperManager.java index 72e049f9..08288e45 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/manager/TEManager.java +++ b/Aria/src/main/java/com/arialyy/aria/core/manager/TaskWrapperManager.java @@ -27,106 +27,76 @@ import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; /** - * Created by Aria.Lao on 2017/11/1. - * 任务实体管理器 + * Created by Aria.Lao on 2017/11/1. 任务实体管理器 */ -public class TEManager { +public class TaskWrapperManager { private static final String TAG = "TaskManager"; - private static volatile TEManager INSTANCE = null; + private static volatile TaskWrapperManager INSTANCE = null; private LruCache cache = new LruCache<>(1024); private Lock lock; - public static TEManager getInstance() { + public static TaskWrapperManager getInstance() { if (INSTANCE == null) { - synchronized (TEManager.class) { - INSTANCE = new TEManager(); + synchronized (TaskWrapperManager.class) { + INSTANCE = new TaskWrapperManager(); } } return INSTANCE; } - private TEManager() { + private TaskWrapperManager() { lock = new ReentrantLock(); } - /** - * 通过key创建任务,只适应于单任务 - * - * @return 如果任务实体创建失败,返回null - */ - private TE createNormalTE(Class clazz, String key) { - final Lock lock = this.lock; - lock.lock(); - try { - AbsTaskWrapper tEntity = cache.get(convertKey(key)); - if (tEntity == null || tEntity.getClass() != clazz) { - INormalTEFactory factory = chooseNormalFactory(clazz); - if (factory == null) { - ALog.e(TAG, "任务实体创建失败"); - return null; - } - tEntity = factory.create(key); - cache.put(convertKey(key), tEntity); - } - return (TE) tEntity; - } finally { - lock.unlock(); + private IGTEFactory chooseGroupFactory(Class clazz) { + if (clazz == DGTaskWrapper.class) { + return DGTaskWrapperFactory.getInstance(); } + return null; } - /** - * 通过key创建不需要缓存的任务实体,只适应于单任务 - * - * @return 如果任务实体创建失败,返回null - */ - public TE createNormalNoCacheTE(Class clazz, String key) { - final Lock lock = this.lock; - lock.lock(); - try { - INormalTEFactory factory = chooseNormalFactory(clazz); - if (factory == null) { - ALog.e(TAG, "任务实体创建失败"); - return null; - } - AbsTaskWrapper tEntity = factory.create(key); - return (TE) tEntity; - } finally { - lock.unlock(); + private INormalTEFactory chooseNormalFactory(Class clazz) { + if (clazz == DTaskWrapper.class) { + return DTaskWrapperFactory.getInstance(); + } else if (clazz == UTaskWrapper.class) { + return UTaskWrapperFactory.getInstance(); } + return null; } /** - * 创建任务组 + * 从缓存中获取单任务实体,如果任务实体不存在,则创建任务实体 * - * @return 如果任务实体创建失败,返回null + * @param key 下载任务,key为下载地址;上传任务,key为本地文件路径 + * @return 创建失败,返回null;成功返回{@link DTaskWrapper}或者{@link UTaskWrapper} */ - private TE createGTEntity(Class clazz, List urls) { + public TE getHttpTaskWrapper(Class clazz, String key) { final Lock lock = this.lock; lock.lock(); try { - String groupName = CommonUtil.getMd5Code(urls); - AbsTaskWrapper tEntity = cache.get(convertKey(groupName)); - if (tEntity == null || tEntity.getClass() != clazz) { - IGTEFactory factory = chooseGroupFactory(clazz); + AbsTaskWrapper wrapper = cache.get(convertKey(key)); + if (wrapper == null || wrapper.getClass() != clazz) { + INormalTEFactory factory = chooseNormalFactory(clazz); if (factory == null) { ALog.e(TAG, "任务实体创建失败"); return null; } - tEntity = factory.getGTE(groupName, urls); - cache.put(convertKey(groupName), tEntity); + wrapper = factory.create(key); + cache.put(convertKey(key), wrapper); } - return (TE) tEntity; + return (TE) wrapper; } finally { lock.unlock(); } } /** - * 通过ftp文件夹路径,创建FTP文件夹实体 + * 从缓存中获取FTP文件夹任务实体,如果任务实体不存在,则创建任务实体 * - * @return 如果任务实体创建失败,返回null + * @param key 下载任务,key为下载地址;上传任务,key为本地文件路径 + * @return 创建失败,返回null;成功返回{@link DTaskWrapper}, */ - private TE createFDTE(Class clazz, String key) { + public TE getFtpTaskWrapper(Class clazz, String key) { final Lock lock = this.lock; lock.lock(); try { @@ -146,70 +116,13 @@ public class TEManager { } } - private IGTEFactory chooseGroupFactory(Class clazz) { - if (clazz == DGTaskWrapper.class) { - return DGTEFactory.getInstance(); - } - return null; - } - - private INormalTEFactory chooseNormalFactory(Class clazz) { - if (clazz == DTaskWrapper.class) { - return DTEFactory.getInstance(); - } else if (clazz == UTaskWrapper.class) { - return UTEFactory.getInstance(); - } - return null; - } - - /** - * 从缓存中获取单任务实体,如果任务实体不存在,则创建任务实体 - * - * @return 创建失败,返回null;成功返回{@link DTaskWrapper}或者{@link UTaskWrapper} - */ - public TE getTEntity(Class clazz, String key) { - final Lock lock = this.lock; - lock.lock(); - try { - AbsTaskWrapper tEntity = cache.get(convertKey(key)); - if (tEntity == null) { - return createNormalTE(clazz, key); - } else { - return (TE) tEntity; - } - } finally { - lock.unlock(); - } - } - /** - * 从缓存中获取FTP文件夹任务实体,如果任务实体不存在,则创建任务实体 - * - * @return 创建失败,返回null;成功返回{@link DTaskWrapper}, - */ - public TE getFDTEntity(Class clazz, String key) { - final Lock lock = this.lock; - lock.lock(); - try { - AbsTaskWrapper tEntity = cache.get(convertKey(key)); - if (tEntity == null) { - return createFDTE(clazz, key); - } else { - return (TE) tEntity; - } - } finally { - lock.unlock(); - } - } - - /** - * 从缓存中获取HTTP任务组的任务实体,如果任务实体不存在,则创建任务实体 - * 获取{} + * 从缓存中获取HTTP任务组的任务实体,如果任务实体不存在,则创建任务实体 获取{} * * @param urls HTTP任务组的子任务下载地址列表 * @return 地址列表为null或创建实体失败,返回null;成功返回{@link DGTaskWrapper} */ - public TE getGTEntity(Class clazz, List urls) { + public TE getDGTaskWrapper(Class clazz, List urls) { if (urls == null || urls.isEmpty()) { ALog.e(TAG, "获取HTTP任务组实体失败:任务组的子任务下载地址列表为null"); return null; @@ -218,21 +131,26 @@ public class TEManager { lock.lock(); try { String groupName = CommonUtil.getMd5Code(urls); - AbsTaskWrapper tEntity = cache.get(convertKey(groupName)); - if (tEntity == null) { - return createGTEntity(clazz, urls); - } else { - return (TE) tEntity; + AbsTaskWrapper tWrapper = cache.get(convertKey(groupName)); + if (tWrapper == null || tWrapper.getClass() != clazz) { + IGTEFactory factory = chooseGroupFactory(clazz); + if (factory == null) { + ALog.e(TAG, "任务实体创建失败"); + return null; + } + tWrapper = factory.getGTE(groupName, urls); + cache.put(convertKey(groupName), tWrapper); } + return (TE) tWrapper; } finally { lock.unlock(); } } /** - * 更新任务实体 + * 更新任务Wrapper */ - public void putTEntity(String key, AbsTaskWrapper tEntity) { + public void putTaskWrapper(String key, AbsTaskWrapper tEntity) { final Lock lock = this.lock; lock.lock(); try { @@ -247,7 +165,7 @@ public class TEManager { * * @return {@code false} 实体为null,添加失败 */ - public boolean addTEntity(AbsTaskWrapper te) { + public boolean addTaskWrapper(AbsTaskWrapper te) { if (te == null) { ALog.e(TAG, "任务实体添加失败"); return false; @@ -262,10 +180,9 @@ public class TEManager { } /** - * 通过key删除任务实体 - * 当任务complete或删除记录时将删除缓存 + * 通过key删除任务实体 当任务complete或删除记录时将删除缓存 */ - public AbsTaskWrapper removeTEntity(String key) { + public AbsTaskWrapper removeTaskWrapper(String key) { final Lock lock = this.lock; lock.lock(); try { diff --git a/Aria/src/main/java/com/arialyy/aria/core/manager/UTEFactory.java b/Aria/src/main/java/com/arialyy/aria/core/manager/UTaskWrapperFactory.java similarity index 57% rename from Aria/src/main/java/com/arialyy/aria/core/manager/UTEFactory.java rename to Aria/src/main/java/com/arialyy/aria/core/manager/UTaskWrapperFactory.java index c56aeda6..0899b094 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/manager/UTEFactory.java +++ b/Aria/src/main/java/com/arialyy/aria/core/manager/UTaskWrapperFactory.java @@ -15,55 +15,34 @@ */ package com.arialyy.aria.core.manager; -import android.text.TextUtils; import com.arialyy.aria.core.upload.UTaskWrapper; import com.arialyy.aria.core.upload.UploadEntity; -import com.arialyy.aria.core.upload.wrapper.UTEWrapper; -import com.arialyy.aria.orm.DbEntity; import com.arialyy.aria.util.Regular; -import java.util.List; import java.util.regex.Pattern; /** - * Created by Aria.Lao on 2017/11/1. - * 任务实体工厂 + * Created by Aria.Lao on 2017/11/1. 任务实体工厂 */ -class UTEFactory implements INormalTEFactory { - private static final String TAG = "DTEFactory"; - private static volatile UTEFactory INSTANCE = null; +class UTaskWrapperFactory implements INormalTEFactory { + private static final String TAG = "UTaskWrapperFactory"; + private static volatile UTaskWrapperFactory INSTANCE = null; - private UTEFactory() { + private UTaskWrapperFactory() { } - public static UTEFactory getInstance() { + public static UTaskWrapperFactory getInstance() { if (INSTANCE == null) { - synchronized (UTEFactory.class) { - INSTANCE = new UTEFactory(); + synchronized (UTaskWrapperFactory.class) { + INSTANCE = new UTaskWrapperFactory(); } } return INSTANCE; } private UTaskWrapper create(UploadEntity entity) { - List wrapper = - DbEntity.findRelationData(UTEWrapper.class, "UTaskWrapper.key=?", - entity.getFilePath()); - UTaskWrapper uTaskEntity = null; - if (wrapper != null && !wrapper.isEmpty()) { - uTaskEntity = wrapper.get(0).taskEntity; - if (uTaskEntity == null) { - uTaskEntity = new UTaskWrapper(); - uTaskEntity.setEntity(entity); - } else if (uTaskEntity.getEntity() == null || TextUtils.isEmpty( - uTaskEntity.getEntity().getFilePath())) { - uTaskEntity.setEntity(entity); - } - } else { - uTaskEntity = new UTaskWrapper(); - uTaskEntity.setEntity(entity); - } - uTaskEntity.setKey(entity.getFilePath()); - return uTaskEntity; + UTaskWrapper wrapper = new UTaskWrapper(entity); + wrapper.setKey(entity.getFilePath()); + return wrapper; } @Override public UTaskWrapper create(String key) { diff --git a/Aria/src/main/java/com/arialyy/aria/core/scheduler/AbsSchedulers.java b/Aria/src/main/java/com/arialyy/aria/core/scheduler/AbsSchedulers.java index e425f5dd..66ba8b52 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/scheduler/AbsSchedulers.java +++ b/Aria/src/main/java/com/arialyy/aria/core/scheduler/AbsSchedulers.java @@ -30,7 +30,7 @@ import com.arialyy.aria.core.inf.GroupSendParams; import com.arialyy.aria.core.inf.IEntity; import com.arialyy.aria.core.inf.ITask; import com.arialyy.aria.core.inf.TaskSchedulerType; -import com.arialyy.aria.core.manager.TEManager; +import com.arialyy.aria.core.manager.TaskWrapperManager; import com.arialyy.aria.core.queue.ITaskQueue; import com.arialyy.aria.core.upload.UploadTask; import com.arialyy.aria.util.ALog; @@ -199,10 +199,10 @@ abstract class AbsSchedulers reTryNum) { mQueue.removeTaskFormQueue(task.getKey()); startNextTask(task); - TEManager.getInstance().removeTEntity(task.getKey()); + TaskWrapperManager.getInstance().removeTaskWrapper(task.getKey()); callback(FAIL, task); return; } @@ -359,7 +359,7 @@ abstract class AbsSchedulers protected String mTempUrl; void initTarget(String filePath) { - mTaskWrapper = TEManager.getInstance().getTEntity(UTaskWrapper.class, filePath); + mTaskWrapper = TaskWrapperManager.getInstance().getHttpTaskWrapper(UTaskWrapper.class, filePath); mEntity = mTaskWrapper.getEntity(); File file = new File(filePath); mEntity.setFileName(file.getName()); diff --git a/Aria/src/main/java/com/arialyy/aria/core/upload/UTaskWrapper.java b/Aria/src/main/java/com/arialyy/aria/core/upload/UTaskWrapper.java index 207b3065..33881f8b 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/upload/UTaskWrapper.java +++ b/Aria/src/main/java/com/arialyy/aria/core/upload/UTaskWrapper.java @@ -22,27 +22,18 @@ import com.arialyy.aria.core.inf.AbsTaskWrapper; */ public class UTaskWrapper extends AbsTaskWrapper { - private UploadEntity entity; - private String filePath; private String key; - public UTaskWrapper() { - } - - @Override public UploadEntity getEntity() { - return entity; + public UTaskWrapper(UploadEntity entity) { + super(entity); } @Override public String getKey() { return key; } - public void setEntity(UploadEntity entity) { - this.entity = entity; - } - public String getFilePath() { return filePath; } diff --git a/Aria/src/main/java/com/arialyy/aria/core/upload/UploadReceiver.java b/Aria/src/main/java/com/arialyy/aria/core/upload/UploadReceiver.java index 15248434..7bedc8c2 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/upload/UploadReceiver.java +++ b/Aria/src/main/java/com/arialyy/aria/core/upload/UploadReceiver.java @@ -123,7 +123,7 @@ public class UploadReceiver extends AbsReceiver { public void stopAllTask() { AriaManager.getInstance(AriaManager.APP) .setCmd(NormalCmdFactory.getInstance() - .createCmd(new UTaskWrapper(), NormalCmdFactory.TASK_STOP_ALL, + .createCmd(new UTaskWrapper(null), NormalCmdFactory.TASK_STOP_ALL, ICmd.TASK_TYPE_UPLOAD)) .exe(); } @@ -137,7 +137,7 @@ public class UploadReceiver extends AbsReceiver { public void removeAllTask(boolean removeFile) { final AriaManager am = AriaManager.getInstance(AriaManager.APP); CancelAllCmd cancelCmd = - (CancelAllCmd) CommonUtil.createNormalCmd(new UTaskWrapper(), + (CancelAllCmd) CommonUtil.createNormalCmd(new UTaskWrapper(null), NormalCmdFactory.TASK_CANCEL_ALL, ICmd.TASK_TYPE_UPLOAD); cancelCmd.removeFile = removeFile; am.setCmd(cancelCmd).exe(); diff --git a/Aria/src/main/java/com/arialyy/aria/core/upload/uploader/FtpFileInfoThread.java b/Aria/src/main/java/com/arialyy/aria/core/upload/uploader/FtpFileInfoThread.java index c967d2ce..3beef7c8 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/upload/uploader/FtpFileInfoThread.java +++ b/Aria/src/main/java/com/arialyy/aria/core/upload/uploader/FtpFileInfoThread.java @@ -24,7 +24,7 @@ import com.arialyy.aria.core.common.ThreadRecord; import com.arialyy.aria.core.upload.UTaskWrapper; import com.arialyy.aria.core.upload.UploadEntity; import com.arialyy.aria.util.ALog; -import com.arialyy.aria.util.DbHelper; +import com.arialyy.aria.util.DbDataHelper; import java.util.ArrayList; /** @@ -71,7 +71,7 @@ class FtpFileInfoThread extends AbsFtpInfoThread { mTaskWrapper.setNewTask(false); // 修改记录 - TaskRecord record = DbHelper.getTaskRecord(mTaskWrapper.getKey()); + TaskRecord record = DbDataHelper.getTaskRecord(mTaskWrapper.getKey()); if (record == null) { record = new TaskRecord(); record.fileName = mEntity.getFileName(); diff --git a/Aria/src/main/java/com/arialyy/aria/core/upload/wrapper/UTEWrapper.java b/Aria/src/main/java/com/arialyy/aria/core/upload/wrapper/UTEWrapper.java deleted file mode 100644 index ce430c76..00000000 --- a/Aria/src/main/java/com/arialyy/aria/core/upload/wrapper/UTEWrapper.java +++ /dev/null @@ -1,47 +0,0 @@ -/* - * Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria) - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.arialyy.aria.core.upload.wrapper; - -import com.arialyy.aria.core.upload.UTaskWrapper; -import com.arialyy.aria.core.upload.UploadEntity; -import com.arialyy.aria.orm.AbsWrapper; -import com.arialyy.aria.orm.annotation.Many; -import com.arialyy.aria.orm.annotation.One; -import com.arialyy.aria.orm.annotation.Wrapper; -import java.util.List; - -/** - * Created by laoyuyu on 2018/3/30. - */ -@Wrapper -public class UTEWrapper extends AbsWrapper { - - @One - public UploadEntity entity; - - @Many(parentColumn = "filePath", entityColumn = "key") - private List taskEntitys = null; - - public UTaskWrapper taskEntity; - - @Override public void handleConvert() { - //taskEntity.entity = (tEntity == null || tEntity.isEmpty()) ? null : tEntity.get(0); - taskEntity = (taskEntitys == null || taskEntitys.isEmpty()) ? null : taskEntitys.get(0); - if (taskEntity != null) { - taskEntity.setEntity(entity); - } - } -} diff --git a/Aria/src/main/java/com/arialyy/aria/orm/DBConfig.java b/Aria/src/main/java/com/arialyy/aria/orm/DBConfig.java index 7597ed9b..03195266 100644 --- a/Aria/src/main/java/com/arialyy/aria/orm/DBConfig.java +++ b/Aria/src/main/java/com/arialyy/aria/orm/DBConfig.java @@ -35,7 +35,7 @@ class DBConfig { /*adb pull /mnt/sdcard/Android/data/com.arialyy.simple/files/DB/AriaLyyDb d:/db*/ static Map mapping = new HashMap<>(); static String DB_NAME; - static int VERSION = 42; + static int VERSION = 43; /** * 是否将数据库保存在Sd卡,{@code true} 是 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 a3aed86c..44d7b8b3 100644 --- a/Aria/src/main/java/com/arialyy/aria/util/CommonUtil.java +++ b/Aria/src/main/java/com/arialyy/aria/util/CommonUtil.java @@ -35,7 +35,6 @@ 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.DownloadGroupTask; import com.arialyy.aria.core.inf.AbsGroupTaskWrapper; import com.arialyy.aria.core.inf.AbsNormalEntity; import com.arialyy.aria.core.inf.AbsTaskWrapper; @@ -1143,7 +1142,7 @@ public class CommonUtil { ALog.w(TAG, "修改任务记录失败,新文件路径和旧文件路径一致"); return; } - TaskRecord record = DbHelper.getTaskRecord(oldPath); + TaskRecord record = DbDataHelper.getTaskRecord(oldPath); if (record == null) { if (new File(oldPath).exists()) { ALog.w(TAG, "修改任务记录失败,文件【" + oldPath + "】对应的任务记录不存在"); diff --git a/Aria/src/main/java/com/arialyy/aria/util/DbDataHelper.java b/Aria/src/main/java/com/arialyy/aria/util/DbDataHelper.java new file mode 100644 index 00000000..e0315eac --- /dev/null +++ b/Aria/src/main/java/com/arialyy/aria/util/DbDataHelper.java @@ -0,0 +1,115 @@ +/* + * Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.arialyy.aria.util; + +import com.arialyy.aria.core.common.RecordWrapper; +import com.arialyy.aria.core.common.TaskRecord; +import com.arialyy.aria.core.download.DGEntityWrapper; +import com.arialyy.aria.core.download.DownloadEntity; +import com.arialyy.aria.core.download.DownloadGroupEntity; +import com.arialyy.aria.orm.DbEntity; +import java.io.File; +import java.util.ArrayList; +import java.util.List; + +/** + * 数据库帮助类 + */ +public class DbDataHelper { + + /** + * 获取任务记录 + * + * @param filePath 文件地址 + * @return 没有记录返回null,有记录则返回任务记录 + */ + public static TaskRecord getTaskRecord(String filePath) { + List records = + DbEntity.findRelationData(RecordWrapper.class, "TaskRecord.filePath=?", filePath); + if (records == null || records.size() == 0) { + return null; + } + return records.get(0).taskRecord; + } + + /** + * 获取组合任务实体 如果数据库不存在该实体,则新创建一个新的任务组实体 + * + * @param groupName 组合任务名称 + * @param urls 子任务url列表 + */ + public static DownloadGroupEntity getHttpDGEntity(String groupName, List urls) { + List wrapper = + DbEntity.findRelationData(DGEntityWrapper.class, "DownloadGroupEntity.groupName=?", + groupName); + + DownloadGroupEntity groupEntity; + if (wrapper != null && !wrapper.isEmpty()) { + groupEntity = wrapper.get(0).groupEntity; + if (groupEntity == null) { + groupEntity = new DownloadGroupEntity(); + groupEntity.setSubEntities(createHttpSubTask(groupName, urls)); + } + } else { + groupEntity = new DownloadGroupEntity(); + groupEntity.setSubEntities(createHttpSubTask(groupName, urls)); + } + groupEntity.setGroupName(groupName); + groupEntity.setUrls(urls); + return groupEntity; + } + + /** + * 创建HTTP子任务实体 + */ + private static List createHttpSubTask(String groupName, List urls) { + List list = new ArrayList<>(); + for (int i = 0, len = urls.size(); i < len; i++) { + String url = urls.get(i); + DownloadEntity entity = new DownloadEntity(); + entity.setUrl(url); + entity.setDownloadPath(groupName + "_" + i); + int lastIndex = url.lastIndexOf(File.separator); + entity.setFileName(url.substring(lastIndex + 1)); + entity.setGroupName(groupName); + entity.setGroupChild(true); + list.add(entity); + } + return list; + } + + /** + * 通过Ftp下载地址获取组合任务实体 + * + * @param ftpUrl ftp下载地址 + */ + public static DownloadGroupEntity getFtpDGEntity(String ftpUrl) { + List wrapper = + DbEntity.findRelationData(DGEntityWrapper.class, "DownloadGroupEntity.groupName=?", + ftpUrl); + DownloadGroupEntity groupEntity; + if (wrapper != null && !wrapper.isEmpty()) { + groupEntity = wrapper.get(0).groupEntity; + if (groupEntity == null) { + groupEntity = new DownloadGroupEntity(); + } + } else { + groupEntity = new DownloadGroupEntity(); + } + groupEntity.setGroupName(ftpUrl); + return groupEntity; + } +} diff --git a/Aria/src/main/java/com/arialyy/aria/util/DbHelper.java b/Aria/src/main/java/com/arialyy/aria/util/DbHelper.java deleted file mode 100644 index 5277c433..00000000 --- a/Aria/src/main/java/com/arialyy/aria/util/DbHelper.java +++ /dev/null @@ -1,42 +0,0 @@ -/* - * Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria) - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.arialyy.aria.util; - -import com.arialyy.aria.core.common.RecordWrapper; -import com.arialyy.aria.core.common.TaskRecord; -import com.arialyy.aria.orm.DbEntity; -import java.util.List; - -/** - * 数据库帮助类 - */ -public class DbHelper { - - /** - * 获取任务记录 - * - * @param filePath 文件地址 - * @return 没有记录返回null,有记录则返回任务记录 - */ - public static TaskRecord getTaskRecord(String filePath) { - List records = - DbEntity.findRelationData(RecordWrapper.class, "TaskRecord.filePath=?", filePath); - if (records == null || records.size() == 0) { - return null; - } - return records.get(0).taskRecord; - } -} diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index ba098916..9b76910c 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -20,7 +20,7 @@ @@ -30,7 +30,7 @@ - + @@ -40,7 +40,7 @@ - + diff --git a/app/src/main/java/com/arialyy/simple/DbTestActivity.java b/app/src/main/java/com/arialyy/simple/DbTestActivity.java index 30de9f59..b38ce61e 100644 --- a/app/src/main/java/com/arialyy/simple/DbTestActivity.java +++ b/app/src/main/java/com/arialyy/simple/DbTestActivity.java @@ -58,13 +58,11 @@ public class DbTestActivity extends BaseActivity { entity.setFileName("ssssssssssssssssss"); entity.setDownloadPath(key); - DTaskWrapper dte = new DTaskWrapper(); + DTaskWrapper dte = new DTaskWrapper(entity); dte.setUrl(url); dte.setKey(key); - dte.setEntity(entity); datas.add(entity); - datas.add(dte); } //AbsEntity.insertManyData(DownloadEntity.class, datas); 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 c638c9d8..4209526a 100644 --- a/app/src/main/java/com/arialyy/simple/download/SingleTaskActivity.java +++ b/app/src/main/java/com/arialyy/simple/download/SingleTaskActivity.java @@ -58,7 +58,7 @@ public class SingleTaskActivity extends BaseActivity { //"http://static.gaoshouyou.com/d/22/94/822260b849944492caadd2983f9bb624.apks"; //"http://120.55.95.61:8811/ghcg/zg/武义总规纲要成果.zip"; //"https://yizi-kejian.oss-cn-beijing.aliyuncs.com/qimeng/package1/qmtable11.zip"; - //"http://rs.0.gaoshouyou.com/d/04/1e/400423a7551e1f3f0eb1812afa1f9b44.apk"; + "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://www.baidu.com/link?url=_LFCuTPtnzFxVJByJ504QymRywIA1Z_T5xUxe9ZLuxcGM0C_RcdpWyB1eGjbJC-e5wv5wAKM4WmLMAS5KeF6EZJHB8Va3YqZUiaErqK_pxm&wd=&eqid=e8583fe70002d126000000065a99f864"; @@ -67,7 +67,7 @@ public class SingleTaskActivity extends BaseActivity { //"https://dl.genymotion.com/releases/genymotion-2.12.1/genymotion-2.12.1-vbox.exe"; //"http://9.9.9.50:5000/download1"; //"http://9.9.9.50:5000/download/CentOS-7-x86_64-Minimal-1804.iso"; - "http://v2.qingdian1.com/m_20180730_991/2/2B9FB34A4BCD8CE61481D1C8418EFE36_1080P.m3u8"; + //"http://v2.qingdian1.com/m_20180730_991/2/2B9FB34A4BCD8CE61481D1C8418EFE36_1080P.m3u8"; //"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; @@ -93,7 +93,7 @@ public class SingleTaskActivity extends BaseActivity { @Override protected void onDestroy() { super.onDestroy(); - unregisterReceiver(receiver); + //unregisterReceiver(receiver); Aria.download(this).unRegister(); } diff --git a/app/src/main/java/com/arialyy/simple/download/group/DownloadGroupActivity.java b/app/src/main/java/com/arialyy/simple/download/group/DownloadGroupActivity.java index 021f83a8..85e3d66a 100644 --- a/app/src/main/java/com/arialyy/simple/download/group/DownloadGroupActivity.java +++ b/app/src/main/java/com/arialyy/simple/download/group/DownloadGroupActivity.java @@ -183,7 +183,7 @@ public class DownloadGroupActivity extends BaseActivity