From 1e5f01cbd8819152e86f60207236e944517ca562 Mon Sep 17 00:00:00 2001 From: laoyuyu <511455842@qq.com> Date: Thu, 12 Apr 2018 21:41:32 +0800 Subject: [PATCH] =?UTF-8?q?=E6=95=B0=E6=8D=AE=E5=BA=93=E4=BC=98=E5=8C=96?= =?UTF-8?q?=EF=BC=8C=E4=BB=A3=E7=A0=81=E9=80=BB=E8=BE=91=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/arialyy/aria/core/AriaManager.java | 6 +- .../aria/core/command/normal/StartCmd.java | 1 + .../aria/core/download/BaseDListener.java | 2 +- .../aria/core/download/BaseGroupTarget.java | 52 +++--- .../aria/core/download/BaseNormalTarget.java | 3 +- .../aria/core/download/DownloadEntity.java | 20 +-- .../core/download/DownloadGroupEntity.java | 20 +-- .../core/download/DownloadGroupListener.java | 2 +- .../core/download/DownloadGroupTarget.java | 65 +++---- .../aria/core/download/DownloadGroupTask.java | 1 - .../download/DownloadGroupTaskEntity.java | 26 +-- .../aria/core/download/DownloadReceiver.java | 15 +- .../core/download/DownloadTaskEntity.java | 21 +-- .../core/download/FtpDirDownloadTarget.java | 5 +- .../download/downloader/AbsGroupUtil.java | 97 +++-------- .../downloader/DownloadGroupUtil.java | 4 +- .../downloader/FtpDirDownloadUtil.java | 6 +- .../download/downloader/FtpDirInfoThread.java | 22 ++- .../DGroupEntityWrapper.java} | 9 +- .../DGroupSubTaskEntityWrapper.java} | 20 +-- .../wrapper/DGroupTaskEntityWrapper.java | 60 +++++++ .../{ => wrapper}/DownloadTaskWrapper.java | 4 +- .../com/arialyy/aria/core/inf/AbsEntity.java | 6 +- .../arialyy/aria/core/inf/AbsGroupEntity.java | 4 +- .../aria/core/inf/AbsNormalEntity.java | 6 +- .../com/arialyy/aria/core/inf/AbsTarget.java | 2 +- .../aria/core/manager/DGTEFactory.java | 162 ++++++++++++++++++ .../aria/core/manager/DGTEntityFactory.java | 128 -------------- .../{DTEntityFactory.java => DTEFactory.java} | 32 ++-- ...IGTEntityFactory.java => IGTEFactory.java} | 20 ++- ...tityFactory.java => INormalTEFactory.java} | 2 +- .../arialyy/aria/core/manager/TEManager.java | 97 +++++++++-- .../{UTEntityFactory.java => UTEFactory.java} | 14 +- .../aria/core/upload/BaseNormalTarget.java | 3 +- .../aria/core/upload/BaseUListener.java | 2 +- .../java/com/arialyy/aria/orm/DbEntity.java | 67 +------- .../com/arialyy/aria/orm/DelegateFind.java | 2 +- .../com/arialyy/aria/orm/DelegateUpdate.java | 85 +++++---- .../com/arialyy/aria/orm/DelegateWrapper.java | 2 +- .../java/com/arialyy/aria/orm/SqlHelper.java | 2 +- .../com/arialyy/aria/orm/annotation/Many.java | 7 + .../main/java/com/arialyy/aria/util/ALog.java | 50 ++++++ .../com/arialyy/aria/util/CommonUtil.java | 4 +- app/src/main/AndroidManifest.xml | 4 +- .../simple/download/SingleTaskActivity.java | 1 - .../download/group/DownloadGroupActivity.java | 10 +- .../group/FTPDirDownloadActivity.java | 8 +- .../simple/download/group/GroupModule.java | 4 +- .../multi_download/DownloadAdapter.java | 4 +- .../com/arialyy/simple/test/TestActivity.java | 92 ++++++++++ .../simple/test/TestGroupActivity.java | 91 ++++++++++ app/src/main/res/layout/activity_test.xml | 40 +++++ .../aria/core/upload/UploadTaskEntity.java | 17 +- 53 files changed, 885 insertions(+), 544 deletions(-) rename Aria/src/main/java/com/arialyy/aria/core/download/{DownloadGroupWrapper.java => wrapper/DGroupEntityWrapper.java} (78%) rename Aria/src/main/java/com/arialyy/aria/core/download/{DownloadGroupTaskWrapper.java => wrapper/DGroupSubTaskEntityWrapper.java} (63%) create mode 100644 Aria/src/main/java/com/arialyy/aria/core/download/wrapper/DGroupTaskEntityWrapper.java rename Aria/src/main/java/com/arialyy/aria/core/download/{ => wrapper}/DownloadTaskWrapper.java (89%) create mode 100644 Aria/src/main/java/com/arialyy/aria/core/manager/DGTEFactory.java delete mode 100644 Aria/src/main/java/com/arialyy/aria/core/manager/DGTEntityFactory.java rename Aria/src/main/java/com/arialyy/aria/core/manager/{DTEntityFactory.java => DTEFactory.java} (78%) rename Aria/src/main/java/com/arialyy/aria/core/manager/{IGTEntityFactory.java => IGTEFactory.java} (55%) rename Aria/src/main/java/com/arialyy/aria/core/manager/{ITEntityFactory.java => INormalTEFactory.java} (90%) rename Aria/src/main/java/com/arialyy/aria/core/manager/{UTEntityFactory.java => UTEFactory.java} (88%) create mode 100644 app/src/main/java/com/arialyy/simple/test/TestActivity.java create mode 100644 app/src/main/java/com/arialyy/simple/test/TestGroupActivity.java create mode 100644 app/src/main/res/layout/activity_test.xml 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 efaca07f..c76c4cc0 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/AriaManager.java +++ b/Aria/src/main/java/com/arialyy/aria/core/AriaManager.java @@ -323,6 +323,7 @@ import org.xml.sax.SAXException; * 根据功能类型和控件类型获取对应的key */ private String getKey(boolean isDownload, Object obj) { + // TODO: 2018/4/12 不做限制了,让所有类都可以使用 String clsName = obj.getClass().getName(); String key = ""; if (!(obj instanceof Activity)) { @@ -421,8 +422,8 @@ import org.xml.sax.SAXException; private void regAppLifeCallback(Context context) { Context app = context.getApplicationContext(); if (app instanceof Application) { - LifeCallback mLifeCallback = new LifeCallback(); - ((Application) app).registerActivityLifecycleCallbacks(mLifeCallback); + LifeCallback lifeCallback = new LifeCallback(); + ((Application) app).registerActivityLifecycleCallbacks(lifeCallback); } } @@ -493,6 +494,7 @@ import org.xml.sax.SAXException; @Override public void onActivityDestroyed(Activity activity) { destroySchedulerListener(activity); + // TODO: 2018/4/11 维护一个activity堆栈,应用被kill,activity会回调onDestroy方法,需要考虑server后台情况 } } } 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 62824b10..45bbab04 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 @@ -53,6 +53,7 @@ class StartCmd extends AbsNormalCmd { return; } String mod; + // TODO: 2018/4/12 配置文件不存在,是否会出现wait获取不到 ? int maxTaskNum = mQueue.getMaxTaskNum(); AriaManager manager = AriaManager.getInstance(AriaManager.APP); if (isDownloadCmd) { diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/BaseDListener.java b/Aria/src/main/java/com/arialyy/aria/core/download/BaseDListener.java index 04b6d44f..10ca12c0 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/BaseDListener.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/BaseDListener.java @@ -154,7 +154,7 @@ class BaseDListener /** * 文件夹临时路径 */ - protected String mDirPathTemp; + String mDirPathTemp; + /** + * 是否需要修改路径 + */ + boolean needModifyPath = false; private SubTaskManager mSubTaskManager; @@ -110,21 +113,21 @@ abstract class BaseGroupTarget * * @param newDirPath 新的文件夹路径 */ - private void reChangeDirPath(String newDirPath) { - List subTask = mEntity.getSubTask(); - if (subTask != null && !subTask.isEmpty()) { - for (DownloadEntity entity : subTask) { - String oldPath = entity.getDownloadPath(); - String newPath = newDirPath + "/" + entity.getFileName(); + void reChangeDirPath(String newDirPath) { + List subTasks = mTaskEntity.getSubTaskEntities(); + if (subTasks != null && !subTasks.isEmpty()) { + for (DownloadTaskEntity dte : subTasks) { + DownloadEntity de = dte.getEntity(); + String oldPath = de.getDownloadPath(); + String newPath = newDirPath + "/" + de.getFileName(); File file = new File(oldPath); - file.renameTo(new File(newPath)); - DbEntity.exeSql("UPDATE DownloadEntity SET downloadPath='" - + newPath - + "' WHERE downloadPath='" - + oldPath - + "'"); - DbEntity.exeSql( - "UPDATE DownloadTaskEntity SET key='" + newPath + "' WHERE key='" + oldPath + "'"); + if (file.exists()) { + file.renameTo(new File(newPath)); + } + de.setDownloadPath(newPath); + dte.key = newPath; + de.save(); + dte.save(); } } } @@ -135,26 +138,25 @@ abstract class BaseGroupTarget * @return {@code true} 合法 */ boolean checkDirPath() { - final String dirPath = mDirPathTemp; - if (TextUtils.isEmpty(dirPath)) { + if (TextUtils.isEmpty(mDirPathTemp)) { ALog.e(TAG, "文件夹路径不能为null"); return false; - } else if (!dirPath.startsWith("/")) { - ALog.e(TAG, "文件夹路径【" + dirPath + "】错误"); + } else if (!mDirPathTemp.startsWith("/")) { + ALog.e(TAG, "文件夹路径【" + mDirPathTemp + "】错误"); return false; } - File file = new File(dirPath); + File file = new File(mDirPathTemp); if (file.isFile()) { - ALog.e(TAG, "路径【" + dirPath + "】是文件,请设置文件夹路径"); + ALog.e(TAG, "路径【" + mDirPathTemp + "】是文件,请设置文件夹路径"); return false; } - if (!mEntity.getDirPath().equals(dirPath)) { + if (TextUtils.isEmpty(mEntity.getDirPath()) || !mEntity.getDirPath().equals(mDirPathTemp)) { if (!file.exists()) { file.mkdirs(); } - mEntity.setDirPath(dirPath); - reChangeDirPath(dirPath); + needModifyPath = true; + mEntity.setDirPath(mDirPathTemp); } return true; 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 141d5fb3..f5e8e6b0 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 @@ -108,7 +108,8 @@ abstract class BaseNormalTarget @Override protected boolean checkEntity() { boolean b = getTargetType() < GROUP_HTTP && checkUrl() && checkFilePath(); if (b) { - mTaskEntity.save(mEntity); + mEntity.save(); + mTaskEntity.save(); } return b; } diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadEntity.java b/Aria/src/main/java/com/arialyy/aria/core/download/DownloadEntity.java index 89704153..b22ae995 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadEntity.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/DownloadEntity.java @@ -21,6 +21,7 @@ import android.os.Parcelable; import android.text.TextUtils; import com.arialyy.aria.core.inf.AbsNormalEntity; import com.arialyy.aria.core.inf.AbsTaskEntity; +import com.arialyy.aria.orm.ActionPolicy; import com.arialyy.aria.orm.annotation.Foreign; import com.arialyy.aria.orm.annotation.Primary; import com.arialyy.aria.util.CommonUtil; @@ -30,28 +31,30 @@ import com.arialyy.aria.util.CommonUtil; * 下载实体 */ public class DownloadEntity extends AbsNormalEntity implements Parcelable { - @Primary private String downloadPath = ""; //保存路径 + @Primary private String downloadPath; //保存路径 /** * 所属任务组 */ - @Foreign(parent = DownloadGroupEntity.class, column = "groupName") private String groupName = ""; + @Foreign(parent = DownloadGroupEntity.class, column = "groupName", + onUpdate = ActionPolicy.CASCADE, onDelete = ActionPolicy.CASCADE) + private String groupName; /** * 从服务器的返回信息中获取的文件md5信息,如果服务器没有返回,则不会设置该信息 * 如果你已经设置了该任务的MD5信息,Aria也不会从服务器返回的信息中获取该信息 */ - private String md5Code = ""; + private String md5Code; /** * 从服务器的返回信息中获取的文件描述信息 */ - private String disposition = ""; + private String disposition; /** * 从disposition获取到的文件名,如果可以获取到,则会赋值到这个字段 */ - private String serverFileName = ""; + private String serverFileName; @Override public String getKey() { return getUrl(); @@ -96,13 +99,6 @@ public class DownloadEntity extends AbsNormalEntity implements Parcelable { this.groupName = groupName; } - /** - * {@link #getUrl()} - */ - @Deprecated public String getDownloadUrl() { - return getUrl(); - } - public String getDownloadPath() { return downloadPath; } 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 d90b05eb..e061c009 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 @@ -27,25 +27,21 @@ import java.util.List; */ public class DownloadGroupEntity extends AbsGroupEntity { - //@OneToMany(table = DownloadEntity.class, key = "groupName") private List subtask = - // new ArrayList<>(); - - @Ignore - private List subtask; + @Ignore private List subEntities; /** * 任务组下载文件的文件夹地址 * * @see DownloadGroupTarget#setDirPath(String) */ - private String dirPath = ""; + private String dirPath; - public List getSubTask() { - return subtask; + public List getSubEntities() { + return subEntities; } - public void setSubTasks(List subTasks) { - this.subtask = subTasks; + public void setSubEntities(List subTasks) { + this.subEntities = subTasks; } public String getDirPath() { @@ -73,13 +69,13 @@ public class DownloadGroupEntity extends AbsGroupEntity { @Override public void writeToParcel(Parcel dest, int flags) { super.writeToParcel(dest, flags); - dest.writeTypedList(this.subtask); + dest.writeTypedList(this.subEntities); dest.writeString(this.dirPath); } protected DownloadGroupEntity(Parcel in) { super(in); - this.subtask = in.createTypedArrayList(DownloadEntity.CREATOR); + this.subEntities = in.createTypedArrayList(DownloadEntity.CREATOR); this.dirPath = in.readString(); } diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadGroupListener.java b/Aria/src/main/java/com/arialyy/aria/core/download/DownloadGroupListener.java index 321a8311..806d341f 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadGroupListener.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/DownloadGroupListener.java @@ -86,7 +86,7 @@ class DownloadGroupListener private void saveCurrentLocation() { long location = 0; - for (DownloadEntity e : mEntity.getSubTask()) { + for (DownloadEntity e : mEntity.getSubEntities()) { location += e.getCurrentProgress(); } mEntity.setCurrentProgress(location); 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 1d2b0a8d..5a938021 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 @@ -17,7 +17,6 @@ package com.arialyy.aria.core.download; import android.text.TextUtils; import com.arialyy.aria.core.manager.TEManager; -import com.arialyy.aria.orm.DbEntity; import com.arialyy.aria.util.ALog; import com.arialyy.aria.util.CommonUtil; import java.io.File; @@ -46,21 +45,23 @@ public class DownloadGroupTarget extends BaseGroupTarget { if (groupEntity.getUrls() != null && !groupEntity.getUrls().isEmpty()) { this.mUrls.addAll(groupEntity.getUrls()); } - mGroupName = CommonUtil.getMd5Code(groupEntity.getUrls()); - mTaskEntity = TEManager.getInstance().getGTEntity(DownloadGroupTaskEntity.class, mUrls); - mEntity = mTaskEntity.getEntity(); - - if (mEntity != null) { - mDirPathTemp = mEntity.getDirPath(); - } + init(); } DownloadGroupTarget(List urls, String targetName) { this.mTargetName = targetName; this.mUrls = urls; - mGroupName = CommonUtil.getMd5Code(urls); + init(); + } + + private void init() { + mGroupName = CommonUtil.getMd5Code(mUrls); mTaskEntity = TEManager.getInstance().getGTEntity(DownloadGroupTaskEntity.class, mUrls); mEntity = mTaskEntity.getEntity(); + + if (mEntity != null) { + mDirPathTemp = mEntity.getDirPath(); + } } /** @@ -105,6 +106,11 @@ public class DownloadGroupTarget extends BaseGroupTarget { */ public DownloadGroupTarget setSubFileName(List subTaskFileName) { if (subTaskFileName == null || subTaskFileName.isEmpty()) { + ALog.e(TAG, "修改子任务的文件名失败:列表为null"); + return this; + } + if (subTaskFileName.size() != mTaskEntity.getSubTaskEntities().size()) { + ALog.e(TAG, "修改子任务的文件名失败:子任务文件名列表数量和子任务的数量不匹配"); return this; } mSubNameTemp.clear(); @@ -112,27 +118,6 @@ public class DownloadGroupTarget extends BaseGroupTarget { return this; } - /** - * 创建子任务 - */ - private List createSubTask() { - List list = new ArrayList<>(); - for (int i = 0, len = mUrls.size(); i < len; i++) { - DownloadEntity entity = new DownloadEntity(); - entity.setUrl(mUrls.get(i)); - String fileName = - mSubNameTemp.isEmpty() ? CommonUtil.createFileName(entity.getUrl()) : mSubNameTemp.get(i); - entity.setDownloadPath(mEntity.getDirPath() + "/" + fileName); - entity.setGroupName(mGroupName); - entity.setGroupChild(true); - entity.setFileName(fileName); - // TODO: 2018/4/3 是否需要在这个插入? - //entity.insert(); - list.add(entity); - } - return list; - } - @Override protected int getTargetType() { return GROUP_HTTP; } @@ -151,10 +136,14 @@ public class DownloadGroupTarget extends BaseGroupTarget { return false; } - //文件夹路径通过后,并且该实体没有子任务,则创建子任务 - if (mEntity.getSubTask() == null || mEntity.getSubTask().isEmpty()) { - mEntity.setSubTasks(createSubTask()); - } else { + mEntity.save(); + mTaskEntity.save(); + + if (needModifyPath) { + reChangeDirPath(mDirPathTemp); + } + + if (!mSubNameTemp.isEmpty()) { updateSingleSubFileName(); } return true; @@ -166,7 +155,7 @@ public class DownloadGroupTarget extends BaseGroupTarget { * 更新所有改动的子任务文件名 */ private void updateSingleSubFileName() { - List entities = mEntity.getSubTask(); + List entities = mEntity.getSubEntities(); int i = 0; for (DownloadEntity entity : entities) { if (i < mSubNameTemp.size()) { @@ -235,12 +224,10 @@ public class DownloadGroupTarget extends BaseGroupTarget { oldFile.renameTo(new File(newPath)); } CommonUtil.renameDownloadConfig(oldFile.getName(), newName); - DbEntity.exeSql( - "UPDATE DownloadTaskEntity SET key='" + newPath + "' WHERE key='" + oldPath + "'"); + // TODO: 2018/4/11 需要校验数据库级联操作是否生效 entity.setDownloadPath(newPath); entity.setFileName(newName); - // TODO: 2018/4/3 是否需要在这个更新? - //entity.update(); + entity.update(); } } diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadGroupTask.java b/Aria/src/main/java/com/arialyy/aria/core/download/DownloadGroupTask.java index ee2b44b4..87fb4e2d 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadGroupTask.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/DownloadGroupTask.java @@ -116,7 +116,6 @@ public class DownloadGroupTask extends AbsGroupTask { public DownloadGroupTask build() { DownloadGroupTask task = new DownloadGroupTask(taskEntity, outHandler); task.setTargetName(targetName); - taskEntity.save(); return task; } } diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadGroupTaskEntity.java b/Aria/src/main/java/com/arialyy/aria/core/download/DownloadGroupTaskEntity.java index bd24c154..0fff9682 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadGroupTaskEntity.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/DownloadGroupTaskEntity.java @@ -16,18 +16,25 @@ package com.arialyy.aria.core.download; import com.arialyy.aria.core.inf.AbsGroupTaskEntity; +import com.arialyy.aria.orm.ActionPolicy; import com.arialyy.aria.orm.annotation.Foreign; import com.arialyy.aria.orm.annotation.Ignore; import com.arialyy.aria.orm.annotation.Primary; +import java.util.List; /** * Created by AriaL on 2017/7/1. + * 任务组的任务实体 */ public class DownloadGroupTaskEntity extends AbsGroupTaskEntity { @Ignore private DownloadGroupEntity entity; - @Primary @Foreign(parent = DownloadGroupEntity.class, column = "groupName") + @Ignore private List subTaskEntities; + + @Primary + @Foreign(parent = DownloadGroupEntity.class, column = "groupName", + onUpdate = ActionPolicy.CASCADE, onDelete = ActionPolicy.CASCADE) private String key; @Override public DownloadGroupEntity getEntity() { @@ -38,6 +45,14 @@ public class DownloadGroupTaskEntity extends AbsGroupTaskEntity getSubTaskEntities() { + return subTaskEntities; + } + + public void setSubTaskEntities(List subTaskEntities) { + this.subTaskEntities = subTaskEntities; + } + @Override public String getKey() { return key; } @@ -45,13 +60,4 @@ public class DownloadGroupTaskEntity extends AbsGroupTaskEntity getGroupTaskList() { - List wrappers = DbEntity.findRelationData(DownloadGroupWrapper.class); + List wrappers = DbEntity.findRelationData(DGroupEntityWrapper.class); if (wrappers == null || wrappers.isEmpty()) { return null; } List entities = new ArrayList<>(); - for (DownloadGroupWrapper wrapper : wrappers) { + for (DGroupEntityWrapper wrapper : wrappers) { entities.add(wrapper.groupEntity); } return entities; diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadTaskEntity.java b/Aria/src/main/java/com/arialyy/aria/core/download/DownloadTaskEntity.java index a089da27..8bdff9a6 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadTaskEntity.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/DownloadTaskEntity.java @@ -16,6 +16,7 @@ package com.arialyy.aria.core.download; import com.arialyy.aria.core.inf.AbsNormalTaskEntity; +import com.arialyy.aria.orm.ActionPolicy; import com.arialyy.aria.orm.annotation.Foreign; import com.arialyy.aria.orm.annotation.Ignore; import com.arialyy.aria.orm.annotation.NoNull; @@ -27,7 +28,6 @@ import com.arialyy.aria.orm.annotation.Primary; */ public class DownloadTaskEntity extends AbsNormalTaskEntity { - //@OneToOne(table = DownloadEntity.class, key = "downloadPath") public DownloadEntity entity; @Ignore public DownloadEntity entity; /** @@ -38,7 +38,9 @@ public class DownloadTaskEntity extends AbsNormalTaskEntity { /** * 所属的任务组组名,如果不属于任务组,则为null */ - public String groupName = ""; + @Foreign(parent = DownloadGroupTaskEntity.class, column = "key", + onUpdate = ActionPolicy.CASCADE, onDelete = ActionPolicy.CASCADE) + public String groupName; /** * 是否是chunk模式 @@ -53,7 +55,10 @@ public class DownloadTaskEntity extends AbsNormalTaskEntity { /** * Task实体对应的key */ - @Primary @Foreign(parent = DownloadEntity.class, column = "downloadPath") public String key = ""; + @Primary + @Foreign(parent = DownloadEntity.class, column = "downloadPath", + onUpdate = ActionPolicy.CASCADE, onDelete = ActionPolicy.CASCADE) + public String key; public DownloadTaskEntity() { } @@ -65,14 +70,4 @@ public class DownloadTaskEntity extends AbsNormalTaskEntity { @Override public String getKey() { return key; } - - public void save(DownloadEntity entity) { - this.entity = entity; - if (entity != null) { - url = entity.getUrl(); - key = entity.getDownloadPath(); - entity.save(); - } - save(); - } } 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 7b82e2fd..a81142f3 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 @@ -37,7 +37,7 @@ public class FtpDirDownloadTarget extends BaseGroupTarget private void init(String key) { mGroupName = key; - mTaskEntity = TEManager.getInstance().getTEntity(DownloadGroupTaskEntity.class, key); + mTaskEntity = TEManager.getInstance().getFDTEntity(DownloadGroupTaskEntity.class, key); mTaskEntity.requestType = AbsTaskEntity.D_FTP_DIR; mEntity = mTaskEntity.getEntity(); if (mEntity != null) { @@ -53,7 +53,8 @@ public class FtpDirDownloadTarget extends BaseGroupTarget @Override protected boolean checkEntity() { boolean b = getTargetType() == GROUP_FTP_DIR && checkDirPath() && checkUrl(); if (b) { - mTaskEntity.save(mEntity); + mEntity.save(); + mTaskEntity.save(); } return b; } diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/AbsGroupUtil.java b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/AbsGroupUtil.java index 012e9fc3..06a82131 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/AbsGroupUtil.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/AbsGroupUtil.java @@ -16,20 +16,17 @@ package com.arialyy.aria.core.download.downloader; import com.arialyy.aria.core.AriaManager; -import com.arialyy.aria.core.FtpUrlEntity; import com.arialyy.aria.core.common.IUtil; import com.arialyy.aria.core.download.DownloadEntity; import com.arialyy.aria.core.download.DownloadGroupTaskEntity; import com.arialyy.aria.core.download.DownloadTaskEntity; import com.arialyy.aria.core.inf.IDownloadListener; import com.arialyy.aria.core.inf.IEntity; -import com.arialyy.aria.orm.DbEntity; import com.arialyy.aria.util.ALog; import com.arialyy.aria.util.CommonUtil; import com.arialyy.aria.util.NetUtils; import java.io.File; import java.util.HashMap; -import java.util.List; import java.util.Map; import java.util.Set; import java.util.Timer; @@ -73,14 +70,15 @@ public abstract class AbsGroupUtil implements IUtil { Map mFailMap = new HashMap<>(); /** - * 下载器映射表,key为下载地址 + * 该任务组对应的所有任务 */ - private Map mDownloaderMap = new HashMap<>(); + private Map mTasksMap = new HashMap<>(); /** - * 该任务组对应的所有任务 + * 下载器映射表,key为下载地址 */ - private Map mTasksMap = new HashMap<>(); + private Map mDownloaderMap = new HashMap<>(); + /** * 是否需要读取文件长度,{@code true}需要 */ @@ -99,37 +97,31 @@ public abstract class AbsGroupUtil implements IUtil { int mInitFailNum = 0; //任务组大小 int mGroupSize = 0; - long mUpdateInterval = 1000; + private long mUpdateInterval = 1000; AbsGroupUtil(IDownloadGroupListener listener, DownloadGroupTaskEntity groupEntity) { mListener = listener; mGTEntity = groupEntity; mExePool = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors()); - List tasks = - DbEntity.findDatas(DownloadTaskEntity.class, "groupName=?", mGTEntity.getKey()); - if (tasks != null && !tasks.isEmpty()) { - for (DownloadTaskEntity te : tasks) { - te.removeFile = mGTEntity.removeFile; - if (te.getEntity() == null) continue; - mTasksMap.put(te.getEntity().getUrl(), te); - } - } - mGroupSize = mGTEntity.getEntity().getSubTask().size(); + + mGroupSize = mGTEntity.getSubTaskEntities().size(); mTotalLen = groupEntity.getEntity().getFileSize(); isNeedLoadFileSize = mTotalLen <= 1; - for (DownloadEntity entity : mGTEntity.getEntity().getSubTask()) { - File file = new File(entity.getDownloadPath()); - if (entity.getState() == IEntity.STATE_COMPLETE && file.exists()) { + + for (DownloadTaskEntity te : mGTEntity.getSubTaskEntities()) { + File file = new File(te.getKey()); + if (te.getState() == IEntity.STATE_COMPLETE && file.exists()) { mCompleteNum++; - mCurrentLocation += entity.getFileSize(); + mCurrentLocation += te.getEntity().getFileSize(); } else { - mExeMap.put(entity.getUrl(), createChildDownloadTask(entity)); - mCurrentLocation += file.exists() ? entity.getCurrentProgress() : 0; + mExeMap.put(te.url, te); + mCurrentLocation += file.exists() ? te.getEntity().getCurrentProgress() : 0; mActualTaskNum++; } if (isNeedLoadFileSize) { - mTotalLen += entity.getFileSize(); + mTotalLen += te.getEntity().getFileSize(); } + mTasksMap.put(te.url, te); } updateFileSize(); mUpdateInterval = @@ -190,12 +182,9 @@ public abstract class AbsGroupUtil implements IUtil { * @param url 子任务下载地址 */ public void cancelSubTask(String url) { - List urls = mGTEntity.getEntity().getUrls(); - if (urls != null && !urls.isEmpty() && urls.contains(url)) { - urls.remove(url); - // TODO: 2018/4/4 有问题 - DownloadTaskEntity det = - DbEntity.findFirst(DownloadTaskEntity.class, "url=? and isGroupTask='true'", url); + Set urls = mTasksMap.keySet(); + if (!urls.isEmpty() && urls.contains(url)) { + DownloadTaskEntity det = mTasksMap.get(url); if (det != null) { mTotalLen -= det.getEntity().getFileSize(); mGroupSize--; @@ -275,7 +264,6 @@ public abstract class AbsGroupUtil implements IUtil { } } CommonUtil.delDownloadGroupTaskConfig(mGTEntity.removeFile, mGTEntity.getEntity()); - mGTEntity.deleteData(); } public void onCancel() { @@ -306,7 +294,11 @@ public abstract class AbsGroupUtil implements IUtil { isRunning = true; mFailNum = 0; mListener.onPre(); - onStart(); + if (mCompleteNum == mGroupSize) { + mListener.onComplete(); + } else { + onStart(); + } } protected void onStart() { @@ -377,43 +369,6 @@ public abstract class AbsGroupUtil implements IUtil { return dt; } - /** - * 创建子任务下载信息 - */ - DownloadTaskEntity createChildDownloadTask(DownloadEntity entity) { - DownloadTaskEntity taskEntity = mTasksMap.get(entity.getUrl()); - if (taskEntity != null) { - taskEntity.entity = entity; - if (getTaskType() == FTP_DIR) { - taskEntity.urlEntity = createFtpUrlEntity(entity); - } - mTasksMap.put(entity.getUrl(), taskEntity); - return taskEntity; - } - taskEntity = new DownloadTaskEntity(); - taskEntity.entity = entity; - taskEntity.headers = mGTEntity.headers; - taskEntity.requestEnum = mGTEntity.requestEnum; - taskEntity.removeFile = mGTEntity.removeFile; - taskEntity.groupName = mGTEntity.getKey(); - taskEntity.isGroupTask = true; - taskEntity.requestType = mGTEntity.requestType; - taskEntity.key = entity.getDownloadPath(); - if (getTaskType() == FTP_DIR) { - taskEntity.urlEntity = createFtpUrlEntity(entity); - } - taskEntity.save(); - mTasksMap.put(entity.getUrl(), taskEntity); - return taskEntity; - } - - private FtpUrlEntity createFtpUrlEntity(DownloadEntity entity) { - FtpUrlEntity urlEntity = mGTEntity.urlEntity.clone(); - urlEntity.url = entity.getUrl(); - urlEntity.remotePath = CommonUtil.getRemotePath(entity.getUrl()); - return urlEntity; - } - /** * 子任务事件监听 */ @@ -554,7 +509,7 @@ public abstract class AbsGroupUtil implements IUtil { subEntity.setState(state); subEntity.setComplete(state == IEntity.STATE_COMPLETE); if (state == IEntity.STATE_CANCEL) { - subTaskEntity.deleteData(); + subEntity.deleteData(); return; } else if (subEntity.isComplete()) { subEntity.setCompleteTime(System.currentTimeMillis()); diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/DownloadGroupUtil.java b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/DownloadGroupUtil.java index 7699077d..e5a47a82 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/DownloadGroupUtil.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/DownloadGroupUtil.java @@ -109,7 +109,7 @@ public class DownloadGroupUtil extends AbsGroupUtil implements IUtil { createChildDownload(te); } mInitNum++; - if (mInitNum + mInitFailNum >= mGTEntity.getEntity().getSubTask().size() + if (mInitNum + mInitFailNum >= mGTEntity.getEntity().getSubEntities().size() || !isNeedLoadFileSize) { startRunningFlow(); updateFileSize(); @@ -132,7 +132,7 @@ public class DownloadGroupUtil extends AbsGroupUtil implements IUtil { if (mActualTaskNum < 0) mActualTaskNum = 0; } failNum++; - if (mInitNum + mInitFailNum >= mGTEntity.getEntity().getSubTask().size() + if (mInitNum + mInitFailNum >= mGTEntity.getEntity().getSubEntities().size() || !isNeedLoadFileSize) { startRunningFlow(); updateFileSize(); diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/FtpDirDownloadUtil.java b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/FtpDirDownloadUtil.java index f2c9665d..1b1b4b3c 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/FtpDirDownloadUtil.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/FtpDirDownloadUtil.java @@ -20,6 +20,7 @@ import com.arialyy.aria.core.common.OnFileInfoCallback; import com.arialyy.aria.core.download.DownloadEntity; import com.arialyy.aria.core.download.DownloadGroupTaskEntity; import com.arialyy.aria.core.download.DownloadTaskEntity; +import com.arialyy.aria.core.manager.TEManager; import com.arialyy.aria.util.ErrorHelp; import java.util.Set; @@ -44,10 +45,7 @@ public class FtpDirDownloadUtil extends AbsGroupUtil { new FtpDirInfoThread(mGTEntity, new OnFileInfoCallback() { @Override public void onComplete(String url, CompleteInfo info) { if (info.code >= 200 && info.code < 300) { - for (DownloadEntity entity : mGTEntity.getEntity().getSubTask()) { - mExeMap.put(entity.getUrl(), createChildDownloadTask(entity)); - } - mActualTaskNum = mGTEntity.getEntity().getSubTask().size(); + mActualTaskNum = mGTEntity.getEntity().getSubEntities().size(); mGroupSize = mActualTaskNum; mTotalLen = mGTEntity.getEntity().getFileSize(); startDownload(); 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 8b4bf128..7efa26b0 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 @@ -22,6 +22,7 @@ import com.arialyy.aria.core.common.OnFileInfoCallback; 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.DownloadTaskEntity; import com.arialyy.aria.util.CommonUtil; import java.nio.charset.Charset; import java.util.ArrayList; @@ -52,10 +53,14 @@ class FtpDirInfoThread extends AbsFtpInfoThread()); } - if (mEntity.getSubTask() == null) { - mEntity.setSubTasks(new ArrayList()); + if (mEntity.getSubEntities() == null) { + mEntity.setSubEntities(new ArrayList()); } - mEntity.getSubTask().add(entity); + mEntity.getSubEntities().add(entity); + mTaskEntity.getSubTaskEntities().add(taskEntity); } } diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadGroupWrapper.java b/Aria/src/main/java/com/arialyy/aria/core/download/wrapper/DGroupEntityWrapper.java similarity index 78% rename from Aria/src/main/java/com/arialyy/aria/core/download/DownloadGroupWrapper.java rename to Aria/src/main/java/com/arialyy/aria/core/download/wrapper/DGroupEntityWrapper.java index e5a1e9d9..25c6a5ac 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadGroupWrapper.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/wrapper/DGroupEntityWrapper.java @@ -13,8 +13,10 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.arialyy.aria.core.download; +package com.arialyy.aria.core.download.wrapper; +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; @@ -23,9 +25,10 @@ import java.util.List; /** * Created by laoyuyu on 2018/3/30. + * 任务组实体和子任务实体的关系 */ @Wrapper -public class DownloadGroupWrapper extends AbsWrapper { +public class DGroupEntityWrapper extends AbsWrapper { @One public DownloadGroupEntity groupEntity; @@ -35,7 +38,7 @@ public class DownloadGroupWrapper extends AbsWrapper { @Override protected void handleConvert() { if (subEntity != null && !subEntity.isEmpty()) { - groupEntity.setSubTasks(subEntity); + groupEntity.setSubEntities(subEntity); } } } diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadGroupTaskWrapper.java b/Aria/src/main/java/com/arialyy/aria/core/download/wrapper/DGroupSubTaskEntityWrapper.java similarity index 63% rename from Aria/src/main/java/com/arialyy/aria/core/download/DownloadGroupTaskWrapper.java rename to Aria/src/main/java/com/arialyy/aria/core/download/wrapper/DGroupSubTaskEntityWrapper.java index c0f3fbfd..057c42bd 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadGroupTaskWrapper.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/wrapper/DGroupSubTaskEntityWrapper.java @@ -13,8 +13,10 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.arialyy.aria.core.download; +package com.arialyy.aria.core.download.wrapper; +import com.arialyy.aria.core.download.DownloadGroupTaskEntity; +import com.arialyy.aria.core.download.DownloadTaskEntity; import com.arialyy.aria.orm.AbsWrapper; import com.arialyy.aria.orm.annotation.Many; import com.arialyy.aria.orm.annotation.One; @@ -22,23 +24,21 @@ import com.arialyy.aria.orm.annotation.Wrapper; import java.util.List; /** - * Created by laoyuyu on 2018/3/30. + * Created by laoyuyu on 2018/4/11. + * 任务组任务实体和任务组任务实体的子任务实体对应关系 */ @Wrapper -public class DownloadGroupTaskWrapper extends AbsWrapper { +public class DGroupSubTaskEntityWrapper extends AbsWrapper { @One - public DownloadGroupEntity entity; + public DownloadGroupTaskEntity dgTaskEntity; @Many(parentColumn = "key", entityColumn = "groupName") - private List taskEntitys; - - public DownloadGroupTaskEntity taskEntity; + public List subTaskEntity; @Override protected void handleConvert() { - taskEntity = (taskEntitys == null || taskEntitys.isEmpty()) ? null : taskEntitys.get(0); - if (taskEntity != null) { - taskEntity.setEntity(entity); + if (subTaskEntity != null && !subTaskEntity.isEmpty()) { + dgTaskEntity.setSubTaskEntities(subTaskEntity); } } } diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/wrapper/DGroupTaskEntityWrapper.java b/Aria/src/main/java/com/arialyy/aria/core/download/wrapper/DGroupTaskEntityWrapper.java new file mode 100644 index 00000000..0575daee --- /dev/null +++ b/Aria/src/main/java/com/arialyy/aria/core/download/wrapper/DGroupTaskEntityWrapper.java @@ -0,0 +1,60 @@ +/* + * 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.DownloadGroupEntity; +import com.arialyy.aria.core.download.DownloadGroupTaskEntity; +import com.arialyy.aria.core.download.DownloadTaskEntity; +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 java.util.ArrayList; +import java.util.List; + +/** + * Created by laoyuyu on 2018/3/30. + * 任务组实体和任务组任务实体的关系 + */ +@Wrapper +public class DGroupTaskEntityWrapper extends AbsWrapper { + + @One + public DownloadGroupEntity entity; + + @Many(parentColumn = "groupName", entityColumn = "key") + private List taskEntitys; + + public DownloadGroupTaskEntity taskEntity; + + @Override protected void handleConvert() { + taskEntity = (taskEntitys == null || taskEntitys.isEmpty()) ? null : taskEntitys.get(0); + if (taskEntity != null) { + taskEntity.setEntity(entity); + List subWrappers = + DbEntity.findRelationData(DownloadTaskWrapper.class, "DownloadTaskEntity.groupName=?", + taskEntity.getKey()); + if (subWrappers != null && !subWrappers.isEmpty()) { + List temp = new ArrayList<>(); + for (DownloadTaskWrapper dw : subWrappers) { + temp.add(dw.taskEntity); + } + taskEntity.setSubTaskEntities(temp); + } + } + } +} diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadTaskWrapper.java b/Aria/src/main/java/com/arialyy/aria/core/download/wrapper/DownloadTaskWrapper.java similarity index 89% rename from Aria/src/main/java/com/arialyy/aria/core/download/DownloadTaskWrapper.java rename to Aria/src/main/java/com/arialyy/aria/core/download/wrapper/DownloadTaskWrapper.java index a2fe9137..133a47ad 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadTaskWrapper.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/wrapper/DownloadTaskWrapper.java @@ -13,8 +13,10 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.arialyy.aria.core.download; +package com.arialyy.aria.core.download.wrapper; +import com.arialyy.aria.core.download.DownloadEntity; +import com.arialyy.aria.core.download.DownloadTaskEntity; import com.arialyy.aria.orm.AbsWrapper; import com.arialyy.aria.orm.annotation.Many; import com.arialyy.aria.orm.annotation.One; diff --git a/Aria/src/main/java/com/arialyy/aria/core/inf/AbsEntity.java b/Aria/src/main/java/com/arialyy/aria/core/inf/AbsEntity.java index aa297862..c4a10c5c 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/inf/AbsEntity.java +++ b/Aria/src/main/java/com/arialyy/aria/core/inf/AbsEntity.java @@ -31,7 +31,7 @@ public abstract class AbsEntity extends DbEntity implements IEntity, Parcelable /** * 单位转换后的速度 */ - @Ignore private String convertSpeed = ""; + @Ignore private String convertSpeed; /** * 下载失败计数,每次开始都重置为0 */ @@ -40,7 +40,7 @@ public abstract class AbsEntity extends DbEntity implements IEntity, Parcelable /** * 扩展字段 */ - private String str = ""; + private String str; /** * 文件大小 */ @@ -48,7 +48,7 @@ public abstract class AbsEntity extends DbEntity implements IEntity, Parcelable /** * 转换后的文件大小 */ - private String convertFileSize = ""; + private String convertFileSize; private int state = STATE_WAIT; /** diff --git a/Aria/src/main/java/com/arialyy/aria/core/inf/AbsGroupEntity.java b/Aria/src/main/java/com/arialyy/aria/core/inf/AbsGroupEntity.java index d8250ab7..ad7f6abc 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/inf/AbsGroupEntity.java +++ b/Aria/src/main/java/com/arialyy/aria/core/inf/AbsGroupEntity.java @@ -28,12 +28,12 @@ public abstract class AbsGroupEntity extends AbsEntity implements Parcelable { /** * 组名,组名为任务地址相加的url的Md5 */ - @Primary protected String groupName = ""; + @Primary protected String groupName; /** * 任务组别名 */ - private String alias = ""; + private String alias; private List urls = new ArrayList<>(); diff --git a/Aria/src/main/java/com/arialyy/aria/core/inf/AbsNormalEntity.java b/Aria/src/main/java/com/arialyy/aria/core/inf/AbsNormalEntity.java index c6391ae2..86e6ed12 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/inf/AbsNormalEntity.java +++ b/Aria/src/main/java/com/arialyy/aria/core/inf/AbsNormalEntity.java @@ -26,12 +26,12 @@ public abstract class AbsNormalEntity extends AbsEntity implements Parcelable { /** * 服务器地址 */ - private String url = ""; + private String url; /** * 文件名 */ - private String fileName = ""; + private String fileName; /** * 是否是任务组里面的下载实体 @@ -39,7 +39,7 @@ public abstract class AbsNormalEntity extends AbsEntity implements Parcelable { private boolean isGroupChild = false; private boolean isRedirect = false; //是否重定向 - private String redirectUrl = ""; //重定向链接 + private String redirectUrl; //重定向链接 public String getUrl() { return url; 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 eac0d191..f67a7727 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 @@ -64,7 +64,7 @@ public abstract class AbsTarget { + 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 DownloadGroupTaskEntity getGTE(String groupName, List urls) { + DownloadGroupEntity entity = createDGroupEntity(groupName, urls); + List wrapper = + DbEntity.findRelationData(DGroupTaskEntityWrapper.class, "DownloadGroupTaskEntity.key=?", + entity.getGroupName()); + DownloadGroupTaskEntity gte; + + if (wrapper != null && !wrapper.isEmpty()) { + gte = wrapper.get(0).taskEntity; + if (gte == null) { + // 创建新的任务组任务实体 + gte = new DownloadGroupTaskEntity(); + //创建子任务的任务实体 + gte.setSubTaskEntities(createDGSubTaskEntity(entity)); + } else if (gte.getSubTaskEntities() == null || gte.getSubTaskEntities().isEmpty()) { + gte.setSubTaskEntities(createDGSubTaskEntity(entity)); + } + } else { + gte = new DownloadGroupTaskEntity(); + gte.setSubTaskEntities(createDGSubTaskEntity(entity)); + } + gte.setKey(entity.getGroupName()); + gte.setEntity(entity); + + return gte; + } + + @Override public DownloadGroupTaskEntity getFTE(String ftpUrl) { + List wrapper = + DbEntity.findRelationData(DGroupTaskEntityWrapper.class, "DownloadGroupTaskEntity.key=?", + ftpUrl); + DownloadGroupTaskEntity fte; + + if (wrapper != null && !wrapper.isEmpty()) { + fte = wrapper.get(0).taskEntity; + if (fte == null) { + fte = new DownloadGroupTaskEntity(); + 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 DownloadGroupTaskEntity(); + DownloadGroupEntity dge = new DownloadGroupEntity(); + dge.setGroupName(ftpUrl); + fte.setEntity(dge); + } + fte.setKey(ftpUrl); + fte.urlEntity = CommonUtil.getFtpUrlInfo(ftpUrl); + return fte; + } + + /** + * 创建任务组子任务的任务实体 + */ + private List createDGSubTaskEntity(DownloadGroupEntity dge) { + List list = new ArrayList<>(); + for (DownloadEntity entity : dge.getSubEntities()) { + DownloadTaskEntity taskEntity = new DownloadTaskEntity(); + taskEntity.entity = entity; + taskEntity.key = entity.getDownloadPath(); + taskEntity.groupName = dge.getKey(); + taskEntity.isGroupTask = true; + taskEntity.url = entity.getUrl(); + list.add(taskEntity); + } + return list; + } + + /** + * 查询任务组实体,如果数据库不存在该实体,则新创建一个新的任务组实体 + */ + private DownloadGroupEntity createDGroupEntity(String groupName, List urls) { + List wrapper = + DbEntity.findRelationData(DGroupEntityWrapper.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++) { + DownloadEntity entity = new DownloadEntity(); + entity.setUrl(urls.get(i)); + entity.setDownloadPath(groupName + "_" + i); + entity.setFileName(groupName + "_" + i); + entity.setGroupName(groupName); + entity.setGroupChild(true); + list.add(entity); + } + return list; + } +} diff --git a/Aria/src/main/java/com/arialyy/aria/core/manager/DGTEntityFactory.java b/Aria/src/main/java/com/arialyy/aria/core/manager/DGTEntityFactory.java deleted file mode 100644 index af0ebce2..00000000 --- a/Aria/src/main/java/com/arialyy/aria/core/manager/DGTEntityFactory.java +++ /dev/null @@ -1,128 +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 android.text.TextUtils; -import com.arialyy.aria.core.download.DownloadGroupEntity; -import com.arialyy.aria.core.download.DownloadGroupTaskEntity; -import com.arialyy.aria.core.download.DownloadGroupTaskWrapper; -import com.arialyy.aria.core.download.DownloadGroupWrapper; -import com.arialyy.aria.orm.DbEntity; -import com.arialyy.aria.util.CommonUtil; -import java.util.List; - -/** - * Created by Aria.Lao on 2017/11/1. - * 任务实体工厂 - */ -class DGTEntityFactory implements ITEntityFactory, - IGTEntityFactory { - private static final String TAG = "DTEntityFactory"; - private static volatile DGTEntityFactory INSTANCE = null; - - private DGTEntityFactory() { - } - - public static DGTEntityFactory getInstance() { - if (INSTANCE == null) { - synchronized (DGTEntityFactory.class) { - INSTANCE = new DGTEntityFactory(); - } - } - return INSTANCE; - } - - /** - * 通过下载实体创建任务实体 - */ - private DownloadGroupTaskEntity create(DownloadGroupEntity entity) { - List wrapper = - DbEntity.findRelationData(DownloadGroupTaskWrapper.class, "DownloadGroupTaskWrapper.key=?", - entity.getGroupName()); - DownloadGroupTaskEntity dgTaskEntity; - - if (wrapper != null && !wrapper.isEmpty()) { - dgTaskEntity = wrapper.get(0).taskEntity; - if (dgTaskEntity == null) { - dgTaskEntity = new DownloadGroupTaskEntity(); - dgTaskEntity.setEntity(entity); - } else if (dgTaskEntity.getEntity() == null || TextUtils.isEmpty( - dgTaskEntity.getEntity().getKey())) { - dgTaskEntity.setEntity(entity); - } - } else { - dgTaskEntity = new DownloadGroupTaskEntity(); - dgTaskEntity.setEntity(entity); - } - - return dgTaskEntity; - } - - /** - * 对于任务组,不能使用这个,该方法只用于FTP文件夹下载 - * - * @deprecated 任务组使用:{@link #create(String, List)} - */ - @Override @Deprecated public DownloadGroupTaskEntity create(String key) { - List wrapper = - DbEntity.findRelationData(DownloadGroupTaskWrapper.class, "DownloadGroupTaskWrapper.key=?", - key); - DownloadGroupTaskEntity dgTaskEntity; - if (wrapper != null && !wrapper.isEmpty()) { - dgTaskEntity = wrapper.get(0).taskEntity; - if (dgTaskEntity == null) { - dgTaskEntity = new DownloadGroupTaskEntity(); - dgTaskEntity.setEntity(getDownloadGroupEntity(key, null)); - } else if (dgTaskEntity.getEntity() == null || TextUtils.isEmpty( - dgTaskEntity.getEntity().getKey())) { - dgTaskEntity.setEntity(getDownloadGroupEntity(key, null)); - } - dgTaskEntity.urlEntity = CommonUtil.getFtpUrlInfo(key); - return dgTaskEntity; - } else { - dgTaskEntity = new DownloadGroupTaskEntity(); - dgTaskEntity.setEntity(getDownloadGroupEntity(key, null)); - dgTaskEntity.urlEntity = CommonUtil.getFtpUrlInfo(key); - return dgTaskEntity; - } - } - - @Override public DownloadGroupTaskEntity create(String groupName, List urls) { - return create(getDownloadGroupEntity(groupName, urls)); - } - - /** - * 查询任务组实体,如果数据库不存在该实体,则新创建一个新的任务组实体 - */ - private DownloadGroupEntity getDownloadGroupEntity(String groupName, List urls) { - - List wrapper = - DbEntity.findRelationData(DownloadGroupWrapper.class, "DownloadGroupEntity.groupName=?", - groupName); - DownloadGroupEntity groupEntity; - if (wrapper != null && !wrapper.isEmpty()) { - groupEntity = wrapper.get(0).groupEntity; - if (groupEntity == null) { - groupEntity = new DownloadGroupEntity(); - } - } else { - groupEntity = new DownloadGroupEntity(); - } - groupEntity.setGroupName(groupName); - groupEntity.setUrls(urls); - return groupEntity; - } -} diff --git a/Aria/src/main/java/com/arialyy/aria/core/manager/DTEntityFactory.java b/Aria/src/main/java/com/arialyy/aria/core/manager/DTEFactory.java similarity index 78% rename from Aria/src/main/java/com/arialyy/aria/core/manager/DTEntityFactory.java rename to Aria/src/main/java/com/arialyy/aria/core/manager/DTEFactory.java index 6ec6eb63..b958f11e 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/manager/DTEntityFactory.java +++ b/Aria/src/main/java/com/arialyy/aria/core/manager/DTEFactory.java @@ -18,7 +18,7 @@ package com.arialyy.aria.core.manager; import android.text.TextUtils; import com.arialyy.aria.core.download.DownloadEntity; import com.arialyy.aria.core.download.DownloadTaskEntity; -import com.arialyy.aria.core.download.DownloadTaskWrapper; +import com.arialyy.aria.core.download.wrapper.DownloadTaskWrapper; import com.arialyy.aria.core.inf.IEntity; import com.arialyy.aria.orm.DbEntity; import java.io.File; @@ -28,17 +28,17 @@ import java.util.List; * Created by Aria.Lao on 2017/11/1. * 任务实体工厂 */ -class DTEntityFactory implements ITEntityFactory { - private static final String TAG = "DTEntityFactory"; - private static volatile DTEntityFactory INSTANCE = null; +class DTEFactory implements INormalTEFactory { + private static final String TAG = "DTEFactory"; + private static volatile DTEFactory INSTANCE = null; - private DTEntityFactory() { + private DTEFactory() { } - public static DTEntityFactory getInstance() { + public static DTEFactory getInstance() { if (INSTANCE == null) { - synchronized (DTEntityFactory.class) { - INSTANCE = new DTEntityFactory(); + synchronized (DTEFactory.class) { + INSTANCE = new DTEFactory(); } } return INSTANCE; @@ -51,22 +51,21 @@ class DTEntityFactory implements ITEntityFactory wrapper = DbEntity.findRelationData(DownloadTaskWrapper.class, "DownloadTaskEntity.key=? and DownloadTaskEntity.isGroupTask='false' and DownloadTaskEntity.url=?", entity.getDownloadPath(), entity.getUrl()); - + DownloadTaskEntity taskEntity; if (wrapper != null && !wrapper.isEmpty()) { - DownloadTaskEntity taskEntity = wrapper.get(0).taskEntity; + taskEntity = wrapper.get(0).taskEntity; if (taskEntity == null) { taskEntity = new DownloadTaskEntity(); - taskEntity.entity = entity; } else if (taskEntity.entity == null || TextUtils.isEmpty(taskEntity.entity.getUrl())) { taskEntity.entity = entity; } - - return taskEntity; } else { - DownloadTaskEntity taskEntity = new DownloadTaskEntity(); - taskEntity.entity = entity; - return taskEntity; + taskEntity = new DownloadTaskEntity(); } + taskEntity.key = entity.getDownloadPath(); + taskEntity.url = entity.getUrl(); + taskEntity.entity = entity; + return taskEntity; } /** @@ -89,6 +88,7 @@ class DTEntityFactory implements ITEntityFactory> { +interface IGTEFactory> { /** - * 通过key创建任务 + * 获取任务组的任务实体, + * 1、创建实体和任务实体之间的关联 + * 2、如果在数据库中查找不到对应的数据,则新创建任务实体 + * + * @param groupName 任务组名 + * @param urls 子任务的下载地址 */ - TASK_ENTITY create(String groupName, List urls); + TASK_ENTITY getGTE(String groupName, List urls); + + /** + * 获取FTP文件夹的任务实体,该方法需要以下操作: + * 1、创建实体和任务实体之间的关联 + * 2、如果在数据库中查找不到对应的数据,则新创建任务实体 + * + * @param ftpUrl ftp文件夹下载路径 + */ + TASK_ENTITY getFTE(String ftpUrl); } diff --git a/Aria/src/main/java/com/arialyy/aria/core/manager/ITEntityFactory.java b/Aria/src/main/java/com/arialyy/aria/core/manager/INormalTEFactory.java similarity index 90% rename from Aria/src/main/java/com/arialyy/aria/core/manager/ITEntityFactory.java rename to Aria/src/main/java/com/arialyy/aria/core/manager/INormalTEFactory.java index d5d2b97d..b55ddcf7 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/manager/ITEntityFactory.java +++ b/Aria/src/main/java/com/arialyy/aria/core/manager/INormalTEFactory.java @@ -21,7 +21,7 @@ import com.arialyy.aria.core.inf.AbsTaskEntity; /** * Created by Aria.Lao on 2017/11/1. */ -interface ITEntityFactory> { +interface INormalTEFactory> { /** * 通过key创建任务,只适应于单任务 diff --git a/Aria/src/main/java/com/arialyy/aria/core/manager/TEManager.java b/Aria/src/main/java/com/arialyy/aria/core/manager/TEManager.java index 3ed9f3c1..daba832d 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/manager/TEManager.java +++ b/Aria/src/main/java/com/arialyy/aria/core/manager/TEManager.java @@ -50,18 +50,17 @@ public class TEManager { } /** - * 通过key创建任务,只适应于单任务,不能用于HTTP任务组,可用于Ftp文件夹 - * 如果是任务组,请使用{@link #createGTEntity(Class, List)} + * 通过key创建任务,只适应于单任务 * * @return 如果任务实体创建失败,返回null */ - public TE createTEntity(Class clazz, String key) { + private TE createNormalTE(Class clazz, String key) { final Lock lock = this.lock; lock.lock(); try { AbsTaskEntity tEntity = cache.get(convertKey(key)); if (tEntity == null || tEntity.getClass() != clazz) { - ITEntityFactory factory = chooseFactory(clazz); + INormalTEFactory factory = chooseNormalFactory(clazz); if (factory == null) { ALog.e(TAG, "任务实体创建失败"); return null; @@ -76,23 +75,44 @@ public class TEManager { } /** - * 创建任务组实体 + * 通过key创建不需要缓存的任务实体,只适应于单任务 * * @return 如果任务实体创建失败,返回null */ - public TE createGTEntity(Class clazz, List urls) { + 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; + } + AbsTaskEntity tEntity = factory.create(key); + return (TE) tEntity; + } finally { + lock.unlock(); + } + } + + /** + * 创建任务组 + * + * @return 如果任务实体创建失败,返回null + */ + private TE createGTEntity(Class clazz, List urls) { final Lock lock = this.lock; lock.lock(); try { String groupName = CommonUtil.getMd5Code(urls); AbsTaskEntity tEntity = cache.get(convertKey(groupName)); if (tEntity == null || tEntity.getClass() != clazz) { - IGTEntityFactory factory = chooseGroupFactory(clazz); + IGTEFactory factory = chooseGroupFactory(clazz); if (factory == null) { ALog.e(TAG, "任务实体创建失败"); return null; } - tEntity = factory.create(groupName, urls); + tEntity = factory.getGTE(groupName, urls); cache.put(convertKey(groupName), tEntity); } return (TE) tEntity; @@ -101,26 +121,49 @@ public class TEManager { } } - private IGTEntityFactory chooseGroupFactory(Class clazz) { + /** + * 通过ftp文件夹路径,创建FTP文件夹实体 + * + * @return 如果任务实体创建失败,返回null + */ + private TE createFDTE(Class clazz, String key) { + final Lock lock = this.lock; + lock.lock(); + try { + AbsTaskEntity tEntity = cache.get(convertKey(key)); + if (tEntity == null || tEntity.getClass() != clazz) { + IGTEFactory factory = chooseGroupFactory(clazz); + if (factory == null) { + ALog.e(TAG, "任务实体创建失败"); + return null; + } + tEntity = factory.getFTE(key); + cache.put(convertKey(key), tEntity); + } + return (TE) tEntity; + } finally { + lock.unlock(); + } + } + + private IGTEFactory chooseGroupFactory(Class clazz) { if (clazz == DownloadGroupTaskEntity.class) { - return DGTEntityFactory.getInstance(); + return DGTEFactory.getInstance(); } return null; } - private ITEntityFactory chooseFactory(Class clazz) { + private INormalTEFactory chooseNormalFactory(Class clazz) { if (clazz == DownloadTaskEntity.class) { - return DTEntityFactory.getInstance(); + return DTEFactory.getInstance(); } else if (clazz == UploadTaskEntity.class) { - return UTEntityFactory.getInstance(); - } else if (clazz == DownloadGroupTaskEntity.class) { - return DGTEntityFactory.getInstance(); + return UTEFactory.getInstance(); } return null; } /** - * 从缓存中获取任务实体,如果任务实体不存在,则创建任务实体 + * 从缓存中获取单任务实体,如果任务实体不存在,则创建任务实体 * * @return 创建失败,返回null */ @@ -130,7 +173,27 @@ public class TEManager { try { AbsTaskEntity tEntity = cache.get(convertKey(key)); if (tEntity == null) { - return createTEntity(clazz, key); + return createNormalTE(clazz, key); + } else { + return (TE) tEntity; + } + } finally { + lock.unlock(); + } + } + + /** + * 从缓存中获取FTP文件夹任务实体,如果任务实体不存在,则创建任务实体 + * + * @return 创建失败,返回null + */ + public TE getFDTEntity(Class clazz, String key) { + final Lock lock = this.lock; + lock.lock(); + try { + AbsTaskEntity tEntity = cache.get(convertKey(key)); + if (tEntity == null) { + return createFDTE(clazz, key); } else { return (TE) tEntity; } diff --git a/Aria/src/main/java/com/arialyy/aria/core/manager/UTEntityFactory.java b/Aria/src/main/java/com/arialyy/aria/core/manager/UTEFactory.java similarity index 88% rename from Aria/src/main/java/com/arialyy/aria/core/manager/UTEntityFactory.java rename to Aria/src/main/java/com/arialyy/aria/core/manager/UTEFactory.java index fa2e8a60..9f2e60cb 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/manager/UTEntityFactory.java +++ b/Aria/src/main/java/com/arialyy/aria/core/manager/UTEFactory.java @@ -28,17 +28,17 @@ import java.util.regex.Pattern; * Created by Aria.Lao on 2017/11/1. * 任务实体工厂 */ -class UTEntityFactory implements ITEntityFactory { - private static final String TAG = "DTEntityFactory"; - private static volatile UTEntityFactory INSTANCE = null; +class UTEFactory implements INormalTEFactory { + private static final String TAG = "DTEFactory"; + private static volatile UTEFactory INSTANCE = null; - private UTEntityFactory() { + private UTEFactory() { } - public static UTEntityFactory getInstance() { + public static UTEFactory getInstance() { if (INSTANCE == null) { - synchronized (UTEntityFactory.class) { - INSTANCE = new UTEntityFactory(); + synchronized (UTEFactory.class) { + INSTANCE = new UTEFactory(); } } return INSTANCE; diff --git a/Aria/src/main/java/com/arialyy/aria/core/upload/BaseNormalTarget.java b/Aria/src/main/java/com/arialyy/aria/core/upload/BaseNormalTarget.java index 5a747bae..eca4d68f 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/upload/BaseNormalTarget.java +++ b/Aria/src/main/java/com/arialyy/aria/core/upload/BaseNormalTarget.java @@ -69,7 +69,8 @@ abstract class BaseNormalTarget @Override protected boolean checkEntity() { boolean b = checkUrl() && checkFilePath(); if (b) { - mTaskEntity.save(mEntity); + mEntity.save(); + mTaskEntity.save(); } return b; } diff --git a/Aria/src/main/java/com/arialyy/aria/core/upload/BaseUListener.java b/Aria/src/main/java/com/arialyy/aria/core/upload/BaseUListener.java index 48aa5b73..eeb07b4f 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/upload/BaseUListener.java +++ b/Aria/src/main/java/com/arialyy/aria/core/upload/BaseUListener.java @@ -140,7 +140,7 @@ class BaseUListener List findRelationData(Class clazz, String... expression) { + public static List findRelationData(Class clazz, + String... expression) { return DelegateWrapper.getInstance().findRelationData(clazz, expression); } @@ -114,20 +109,6 @@ public abstract class DbEntity { return datas == null ? null : datas.size() > 0 ? datas.get(0) : null; } - /** - * 获取所有行的rowid - */ - public int[] getRowIds() { - return DelegateWrapper.getInstance().getRowId(getClass()); - } - - /** - * 获取rowid - */ - public int getRowId(@NonNull Object[] wheres, @NonNull Object[] values) { - return DelegateWrapper.getInstance().getRowId(getClass(), wheres, values); - } - /** * 删除当前数据 */ @@ -155,6 +136,7 @@ public abstract class DbEntity { /** * 保存自身,如果表中已经有数据,则更新数据,否则插入数据 + * 只有 target中checkEntity成功后才能保存,创建实体部分也不允许保存 */ public void save() { synchronized (LOCK) { @@ -171,50 +153,13 @@ public abstract class DbEntity { */ private boolean thisIsExist() { DelegateWrapper util = DelegateWrapper.getInstance(); - return util.isExist(getClass(), rowID); + return rowID != -1 && util.isExist(getClass(), rowID); } /** - * 插入数据 + * 插入数据,只有 target中checkEntity成功后才能插入,创建实体部分也不允许操作 */ public void insert() { DelegateWrapper.getInstance().insertData(this); - updateRowID(); - } - - private void updateRowID() { - try { - List fields = CommonUtil.getAllFields(getClass()); - StringBuilder sb = new StringBuilder(); - List params = new ArrayList<>(); - List values = new ArrayList<>(); - for (Field field : fields) { - field.setAccessible(true); - if (SqlUtil.isIgnore(field)) { - continue; - } - sb.append(field.getName()).append("=? AND "); - Type type = field.getType(); - if (type == List.class) { - values.add(SqlUtil.list2Str(this, field)); - } else if (type == Map.class) { - values.add(SqlUtil.map2Str((Map) field.get(this))); - } else { - values.add(field.get(this) + ""); - } - } - - String temp = sb.toString(); - String p = temp.substring(0, temp.length() - 5); - - params.add(p); - params.addAll(values); - DbEntity entity = findFirst(getClass(), params.toArray(new String[params.size()])); - if (entity != null) { - rowID = entity.rowID; - } - } catch (IllegalAccessException e) { - e.printStackTrace(); - } } } \ No newline at end of file diff --git a/Aria/src/main/java/com/arialyy/aria/orm/DelegateFind.java b/Aria/src/main/java/com/arialyy/aria/orm/DelegateFind.java index 98a5866d..89c9916f 100644 --- a/Aria/src/main/java/com/arialyy/aria/orm/DelegateFind.java +++ b/Aria/src/main/java/com/arialyy/aria/orm/DelegateFind.java @@ -500,7 +500,7 @@ class DelegateFind extends AbsDelegate { * 通过rowId判断数据是否存在 */ boolean itemExist(SQLiteDatabase db, Class clazz, - int rowId) { + long rowId) { db = checkDb(db); String sql = "SELECT rowid FROM " + CommonUtil.getClassName(clazz) + " WHERE rowid=" + rowId; print(ROW_ID, sql); diff --git a/Aria/src/main/java/com/arialyy/aria/orm/DelegateUpdate.java b/Aria/src/main/java/com/arialyy/aria/orm/DelegateUpdate.java index d697f5f0..41e94823 100644 --- a/Aria/src/main/java/com/arialyy/aria/orm/DelegateUpdate.java +++ b/Aria/src/main/java/com/arialyy/aria/orm/DelegateUpdate.java @@ -15,9 +15,11 @@ */ package com.arialyy.aria.orm; +import android.content.ContentValues; import android.database.sqlite.SQLiteDatabase; import android.text.TextUtils; import com.arialyy.aria.orm.annotation.Primary; +import com.arialyy.aria.util.ALog; import com.arialyy.aria.util.CheckUtil; import com.arialyy.aria.util.CommonUtil; import java.lang.reflect.Field; @@ -61,10 +63,7 @@ class DelegateUpdate extends AbsDelegate { List fields = CommonUtil.getAllFields(clazz); DbEntity cacheEntity = mDataCache.get(getCacheKey(dbEntity)); if (fields != null && fields.size() > 0) { - StringBuilder sql = new StringBuilder(); - StringBuilder prams = new StringBuilder(); - sql.append("UPDATE ").append(CommonUtil.getClassName(dbEntity)).append(" SET "); - int i = 0; + ContentValues values = new ContentValues(); try { for (Field field : fields) { field.setAccessible(true); @@ -76,10 +75,7 @@ class DelegateUpdate extends AbsDelegate { && !field.getName().equals("state")) { //在LruCache中 state字段总是不能重新赋值... continue; } - String value; - prams.append(i > 0 ? ", " : ""); - prams.append(field.getName()).append("='"); Type type = field.getType(); if (type == Map.class && checkMap(field)) { value = SqlUtil.map2Str((Map) field.get(dbEntity)); @@ -89,19 +85,17 @@ class DelegateUpdate extends AbsDelegate { Object obj = field.get(dbEntity); value = obj == null ? "" : convertValue(obj.toString()); } - - prams.append(TextUtils.isEmpty(value) ? "" : value); - prams.append("'"); - i++; + values.put(field.getName(), value); } } catch (IllegalAccessException e) { e.printStackTrace(); } - if (!TextUtils.isEmpty(prams.toString())) { - sql.append(prams.toString()); - sql.append(" where rowid=").append(dbEntity.rowID); - print(MODIFY_DATA, sql.toString()); - db.execSQL(sql.toString()); + //print(MODIFY_DATA, sql.toString()); + if (values.size() > 0) { + db.update(CommonUtil.getClassName(dbEntity), values, "rowid=?", + new String[] { String.valueOf(dbEntity.rowID) }); + } else { + ALog.d(TAG, "没有数据更新"); } } mDataCache.put(getCacheKey(dbEntity), dbEntity); @@ -116,48 +110,32 @@ class DelegateUpdate extends AbsDelegate { Class clazz = dbEntity.getClass(); List fields = CommonUtil.getAllFields(clazz); if (fields != null && fields.size() > 0) { - StringBuilder sb = new StringBuilder(); - sb.append("INSERT INTO ").append(CommonUtil.getClassName(dbEntity)).append("("); - int i = 0; + ContentValues values = new ContentValues(); try { for (Field field : fields) { field.setAccessible(true); if (isIgnore(dbEntity, field)) { continue; } - sb.append(i > 0 ? ", " : ""); - sb.append(field.getName()); - i++; - } - sb.append(") VALUES ("); - i = 0; - for (Field field : fields) { - field.setAccessible(true); - if (isIgnore(dbEntity, field)) { - continue; - } - sb.append(i > 0 ? ", " : ""); - sb.append("'"); + String value = null; Type type = field.getType(); if (type == Map.class && checkMap(field)) { - sb.append(SqlUtil.map2Str((Map) field.get(dbEntity))); + value = SqlUtil.map2Str((Map) field.get(dbEntity)); } else if (type == List.class && checkList(field)) { - sb.append(SqlUtil.list2Str(dbEntity, field)); + value = SqlUtil.list2Str(dbEntity, field); } else { Object obj = field.get(dbEntity); if (obj != null) { - sb.append(convertValue(field.get(dbEntity).toString())); + value = convertValue(field.get(dbEntity).toString()); } } - sb.append("'"); - i++; + values.put(field.getName(), value); } } catch (IllegalAccessException e) { e.printStackTrace(); } - sb.append(")"); - print(INSERT_DATA, sb.toString()); - db.execSQL(sb.toString()); + //print(INSERT_DATA, ); + dbEntity.rowID = db.insert(CommonUtil.getClassName(dbEntity), null, values); } close(db); } @@ -169,18 +147,33 @@ class DelegateUpdate extends AbsDelegate { if (SqlUtil.isIgnore(field)) { return true; } - if (SqlUtil.isPrimary(field)) { //忽略自动增长的主键 - Primary p = field.getAnnotation(Primary.class); - if (p.autoincrement()) { + Object value = field.get(obj); + if (value == null) { // 忽略为空的字段 + return true; + } + if (value instanceof String) { + if (TextUtils.isEmpty(String.valueOf(value))) { return true; } } - if (SqlUtil.isForeign(field)) { //忽略外键为null的字段 - Object value = field.get(obj); - if (value == null || (value instanceof String && TextUtils.isEmpty(String.valueOf(value)))) { + if (value instanceof List) { + if (((List) value).size() == 0) { return true; } } + if (value instanceof Map) { + if (((Map) value).size() == 0) { + return true; + } + } + + if (SqlUtil.isPrimary(field)) { //忽略自动增长的主键 + Primary p = field.getAnnotation(Primary.class); + if (p.autoincrement()) { + return true; + } + } + return false; } } diff --git a/Aria/src/main/java/com/arialyy/aria/orm/DelegateWrapper.java b/Aria/src/main/java/com/arialyy/aria/orm/DelegateWrapper.java index ca33e472..6a817885 100644 --- a/Aria/src/main/java/com/arialyy/aria/orm/DelegateWrapper.java +++ b/Aria/src/main/java/com/arialyy/aria/orm/DelegateWrapper.java @@ -121,7 +121,7 @@ public class DelegateWrapper { /** * 通过rowId判断数据是否存在 */ - boolean isExist(Class clazz, int rowId) { + boolean isExist(Class clazz, long rowId) { return mDManager.getDelegate(DelegateFind.class).itemExist(mDb, clazz, rowId); } diff --git a/Aria/src/main/java/com/arialyy/aria/orm/SqlHelper.java b/Aria/src/main/java/com/arialyy/aria/orm/SqlHelper.java index 5231d09a..f4560755 100644 --- a/Aria/src/main/java/com/arialyy/aria/orm/SqlHelper.java +++ b/Aria/src/main/java/com/arialyy/aria/orm/SqlHelper.java @@ -36,7 +36,7 @@ final class SqlHelper extends SQLiteOpenHelper { /** * 是否将数据库保存在Sd卡,{@code true} 是 */ - private static final boolean SAVE_IN_SDCARD = false; + private static final boolean SAVE_IN_SDCARD = true; static volatile SqlHelper INSTANCE = null; diff --git a/Aria/src/main/java/com/arialyy/aria/orm/annotation/Many.java b/Aria/src/main/java/com/arialyy/aria/orm/annotation/Many.java index 59d5367c..03f6fd72 100644 --- a/Aria/src/main/java/com/arialyy/aria/orm/annotation/Many.java +++ b/Aria/src/main/java/com/arialyy/aria/orm/annotation/Many.java @@ -34,4 +34,11 @@ import java.lang.annotation.Target; * 父表在子表对应的字段 */ String entityColumn(); + + ///** + // * 是否是一对一关系 + // * + // * @return {@code true} 是,{@code false} 不是 + // */ + //boolean isOne2One() default false; } diff --git a/Aria/src/main/java/com/arialyy/aria/util/ALog.java b/Aria/src/main/java/com/arialyy/aria/util/ALog.java index c49a1ca9..d7c7081f 100644 --- a/Aria/src/main/java/com/arialyy/aria/util/ALog.java +++ b/Aria/src/main/java/com/arialyy/aria/util/ALog.java @@ -19,6 +19,12 @@ package com.arialyy.aria.util; import android.text.TextUtils; import android.util.Log; +import java.util.Arrays; +import java.util.Map; +import java.util.Set; +import org.json.JSONArray; +import org.json.JSONException; +import org.json.JSONObject; /** * Created by Aria.Lao on 2017/10/25. @@ -61,6 +67,50 @@ public class ALog { return println(Log.ERROR, tag, getExceptionString(e)); } + /** + * 打印MAp,debug级别日志 + */ + public static void m(String tag, Map map) { + if (LOG_LEVEL <= Log.DEBUG) { + Set set = map.entrySet(); + if (set.size() < 1) { + d(tag, "[]"); + return; + } + int i = 0; + String[] s = new String[set.size()]; + for (Object aSet : set) { + Map.Entry entry = (Map.Entry) aSet; + s[i] = entry.getKey() + " = " + entry.getValue() + ",\n"; + i++; + } + println(Log.DEBUG, tag, Arrays.toString(s)); + } + } + + /** + * 打印JSON,debug级别日志 + */ + public static void j(String tag, String jsonStr) { + if (LOG_LEVEL <= Log.DEBUG) { + String message; + try { + if (jsonStr.startsWith("{")) { + JSONObject jsonObject = new JSONObject(jsonStr); + message = jsonObject.toString(4); //这个是核心方法 + } else if (jsonStr.startsWith("[")) { + JSONArray jsonArray = new JSONArray(jsonStr); + message = jsonArray.toString(4); + } else { + message = jsonStr; + } + } catch (JSONException e) { + message = jsonStr; + } + println(Log.DEBUG, tag, message); + } + } + /** * 将异常信息转换为字符串 */ 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 eb9da319..131a3a4f 100644 --- a/Aria/src/main/java/com/arialyy/aria/util/CommonUtil.java +++ b/Aria/src/main/java/com/arialyy/aria/util/CommonUtil.java @@ -318,7 +318,7 @@ public class CommonUtil { return; } - for (DownloadEntity taskEntity : groupEntity.getSubTask()) { + for (DownloadEntity taskEntity : groupEntity.getSubEntities()) { delDownloadTaskConfig(removeFile, taskEntity); } @@ -767,7 +767,7 @@ public class CommonUtil { return; } File file = new File(path); - if (!file.getParentFile().exists()) { + if (file.getParentFile() == null || !file.getParentFile().exists()) { ALog.d(TAG, "目标文件所在路径不存在,准备创建……"); if (!createDir(file.getParent())) { ALog.d(TAG, "创建目录文件所在的目录失败!文件路径【" + path + "】"); diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 6730d35c..18cc4fd4 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -14,8 +14,10 @@ android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme.NoActionBar"> + + 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 cd0aade9..d98d94b7 100644 --- a/app/src/main/java/com/arialyy/simple/download/SingleTaskActivity.java +++ b/app/src/main/java/com/arialyy/simple/download/SingleTaskActivity.java @@ -46,7 +46,6 @@ public class SingleTaskActivity extends BaseActivity { //"http://kotlinlang.org/docs/kotlin-docs.pdf"; //"https://atom-installer.github.com/v1.13.0/AtomSetup.exe?s=1484074138&ext=.exe"; //"http://static.gaoshouyou.com/d/22/94/822260b849944492caadd2983f9bb624.apk"; - //不支持断点的链接 "http://58.210.9.131/tpk/sipgt//TDLYZTGH.tpk"; //chunked 下载 //"https://static.donguo.me//video/ip/course/pfys_1.mp4"; @Bind(R.id.start) Button mStart; 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 7140a086..b0527c6f 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 @@ -48,7 +48,7 @@ public class DownloadGroupActivity extends BaseActivity getUrls() { + public List getUrls() { List urls = new ArrayList<>(); String[] str = getContext().getResources().getStringArray(R.array.group_urls_1); Collections.addAll(urls, str); @@ -44,7 +44,7 @@ public class GroupModule extends BaseModule { return urls; } - List getSubName(){ + List getSubName() { List names = new ArrayList<>(); String[] str = getContext().getResources().getStringArray(R.array.group_names); Collections.addAll(names, str); diff --git a/app/src/main/java/com/arialyy/simple/download/multi_download/DownloadAdapter.java b/app/src/main/java/com/arialyy/simple/download/multi_download/DownloadAdapter.java index 83b75caa..db4284ac 100644 --- a/app/src/main/java/com/arialyy/simple/download/multi_download/DownloadAdapter.java +++ b/app/src/main/java/com/arialyy/simple/download/multi_download/DownloadAdapter.java @@ -217,9 +217,9 @@ public class DownloadAdapter extends AbsRVAdapter 0) { - holder.childList.updateChildProgress(((DownloadGroupEntity) entity).getSubTask()); + holder.childList.updateChildProgress(((DownloadGroupEntity) entity).getSubEntities()); } else { - holder.childList.addData(((DownloadGroupEntity) entity).getSubTask()); + holder.childList.addData(((DownloadGroupEntity) entity).getSubEntities()); } } diff --git a/app/src/main/java/com/arialyy/simple/test/TestActivity.java b/app/src/main/java/com/arialyy/simple/test/TestActivity.java new file mode 100644 index 00000000..3f1f88d9 --- /dev/null +++ b/app/src/main/java/com/arialyy/simple/test/TestActivity.java @@ -0,0 +1,92 @@ +package com.arialyy.simple.test; + +import android.os.Bundle; +import android.os.Environment; +import android.util.Log; +import android.view.View; +import com.arialyy.annotations.Download; +import com.arialyy.aria.core.Aria; +import com.arialyy.aria.core.common.RequestEnum; +import com.arialyy.aria.core.download.DownloadTask; +import com.arialyy.aria.util.CommonUtil; +import com.arialyy.frame.util.show.L; +import com.arialyy.simple.R; +import com.arialyy.simple.base.BaseActivity; +import com.arialyy.simple.databinding.ActivityTestBinding; +import java.io.File; + +/** + * Created by Administrator on 2018/4/12. + */ + +public class TestActivity extends BaseActivity { + String TAG = "TestActivity"; + String URL = "http://58.210.9.131/tpk/sipgt//TDLYZTGH.tpk"; //chunked 下载 + + @Override protected int setLayoutId() { + return R.layout.activity_test; + } + + @Override protected void init(Bundle savedInstanceState) { + super.init(savedInstanceState); + mBar.setVisibility(View.GONE); + Aria.download(this).register(); + } + + @Download.onWait void onWait(DownloadTask task) { + Log.d(TAG, "wait ==> " + task.getDownloadEntity().getFileName()); + } + + @Download.onPre protected void onPre(DownloadTask task) { + Log.d(TAG, "onPre"); + } + + @Download.onTaskStart void taskStart(DownloadTask task) { + Log.d(TAG, "onStart"); + } + + @Download.onTaskRunning protected void running(DownloadTask task) { + Log.d(TAG, "running"); + } + + @Download.onTaskResume void taskResume(DownloadTask task) { + Log.d(TAG, "resume"); + } + + @Download.onTaskStop void taskStop(DownloadTask task) { + Log.d(TAG, "stop"); + } + + @Download.onTaskCancel void taskCancel(DownloadTask task) { + Log.d(TAG, "cancel"); + } + + @Download.onTaskFail void taskFail(DownloadTask task) { + Log.d(TAG, "fail"); + } + + @Download.onTaskComplete void taskComplete(DownloadTask task) { + L.d(TAG, "path ==> " + task.getDownloadEntity().getDownloadPath()); + L.d(TAG, "md5Code ==> " + CommonUtil.getFileMD5(new File(task.getDownloadPath()))); + } + + public void onClick(View view) { + switch (view.getId()) { + case R.id.start: + Aria.download(this) + .load(URL) + .addHeader("Accept-Encoding", "gzip, deflate") + .setRequestMode(RequestEnum.GET) + .setFilePath(Environment.getExternalStorageDirectory().getPath() + "/ggsg1.apk") + .resetState() + .start(); + break; + case R.id.stop: + Aria.download(this).load(URL).stop(); + break; + case R.id.cancel: + Aria.download(this).load(URL).cancel(); + break; + } + } +} diff --git a/app/src/main/java/com/arialyy/simple/test/TestGroupActivity.java b/app/src/main/java/com/arialyy/simple/test/TestGroupActivity.java new file mode 100644 index 00000000..d8455218 --- /dev/null +++ b/app/src/main/java/com/arialyy/simple/test/TestGroupActivity.java @@ -0,0 +1,91 @@ +package com.arialyy.simple.test; + +import android.os.Bundle; +import android.os.Environment; +import android.view.View; +import com.arialyy.annotations.DownloadGroup; +import com.arialyy.aria.core.Aria; +import com.arialyy.aria.core.download.DownloadGroupTask; +import com.arialyy.frame.util.show.L; +import com.arialyy.simple.R; +import com.arialyy.simple.base.BaseActivity; +import com.arialyy.simple.databinding.ActivityTestBinding; +import com.arialyy.simple.download.group.GroupModule; +import java.util.List; + +/** + * Created by Administrator on 2018/4/12. + */ + +public class TestGroupActivity extends BaseActivity { + List mUrls; + + @Override protected int setLayoutId() { + return R.layout.activity_test; + } + + @Override protected void init(Bundle savedInstanceState) { + super.init(savedInstanceState); + mBar.setVisibility(View.GONE); + Aria.download(this).register(); + mUrls = getModule(GroupModule.class).getUrls(); + } + + @DownloadGroup.onWait void taskWait(DownloadGroupTask task) { + L.d(TAG, task.getTaskName() + "wait"); + } + + @DownloadGroup.onPre() protected void onPre(DownloadGroupTask task) { + L.d(TAG, "group pre"); + } + + @DownloadGroup.onTaskPre() protected void onTaskPre(DownloadGroupTask task) { + L.d(TAG, "group task pre"); + } + + @DownloadGroup.onTaskStart() void taskStart(DownloadGroupTask task) { + L.d(TAG, "group task start"); + } + + @DownloadGroup.onTaskRunning() protected void running(DownloadGroupTask task) { + L.d(TAG, "group task running"); + } + + @DownloadGroup.onTaskResume() void taskResume(DownloadGroupTask task) { + L.d(TAG, "group task resume"); + } + + @DownloadGroup.onTaskStop() void taskStop(DownloadGroupTask task) { + L.d(TAG, "group task stop"); + } + + @DownloadGroup.onTaskCancel() void taskCancel(DownloadGroupTask task) { + L.d(TAG, "group task cancel"); + } + + @DownloadGroup.onTaskFail() void taskFail(DownloadGroupTask task) { + L.d(TAG, "group task fail"); + } + + @DownloadGroup.onTaskComplete() void taskComplete(DownloadGroupTask task) { + L.d(TAG, "group task complete"); + } + + public void onClick(View view) { + switch (view.getId()) { + case R.id.start: + Aria.download(this) + .loadGroup(mUrls) + .setDirPath(Environment.getExternalStorageDirectory().getPath() + "/download/test/") + .resetState() + .start(); + break; + case R.id.stop: + Aria.download(this).loadGroup(mUrls).stop(); + break; + case R.id.cancel: + Aria.download(this).loadGroup(mUrls).cancel(); + break; + } + } +} diff --git a/app/src/main/res/layout/activity_test.xml b/app/src/main/res/layout/activity_test.xml new file mode 100644 index 00000000..b5b4cf62 --- /dev/null +++ b/app/src/main/res/layout/activity_test.xml @@ -0,0 +1,40 @@ + + + + + + +