From 041df07617e92d8d0cf66fd725307ae269d7da1f Mon Sep 17 00:00:00 2001 From: AriaLyy <511455842@qq.com> Date: Fri, 14 Jul 2017 17:49:07 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BB=BB=E5=8A=A1=E7=BB=84=E4=BA=8B=E4=BB=B6?= =?UTF-8?q?=E5=92=8C=E6=99=AE=E9=80=9A=E4=BB=BB=E5=8A=A1=E4=BA=8B=E4=BB=B6?= =?UTF-8?q?=E5=90=8C=E6=97=B6=E4=BD=BF=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Aria/jcenter.gradle | 2 +- .../core/command/group/GroupStartCmd.java | 2 +- .../core/command/normal/ResumeAllCmd.java | 2 +- .../aria/core/command/normal/StartCmd.java | 11 +- .../aria/core/download/DownloadEntity.java | 4 + .../core/download/DownloadGroupEntity.java | 29 ++- .../core/download/DownloadGroupTarget.java | 46 ++++- .../aria/core/download/DownloadReceiver.java | 40 ++++- .../aria/core/download/DownloadTarget.java | 1 + .../com/arialyy/aria/core/inf/AbsEntity.java | 6 +- .../arialyy/aria/core/inf/AbsGroupEntity.java | 27 ++- .../com/arialyy/aria/core/inf/IEntity.java | 19 +- .../com/arialyy/aria/core/inf/IReceiver.java | 2 +- .../arialyy/aria/core/queue/AbsTaskQueue.java | 4 +- .../arialyy/aria/core/queue/ITaskQueue.java | 8 +- .../aria/core/scheduler/AbsSchedulers.java | 19 +- .../aria/core/scheduler/DQueueMapping.java | 11 +- .../scheduler/DownloadGroupSchedulers.java | 5 + .../core/scheduler/DownloadSchedulers.java | 5 + .../aria/core/upload/UploadEntity.java | 4 + .../aria/core/upload/UploadReceiver.java | 2 +- .../aria/core/upload/UploadTarget.java | 2 +- .../java/com/arialyy/aria/orm/DBConfig.java | 2 +- .../java/com/arialyy/aria/orm/DbEntity.java | 6 +- .../java/com/arialyy/aria/orm/DbUtil.java | 2 +- .../java/com/arialyy/aria/orm/NormalList.java | 32 ++++ .../java/com/arialyy/aria/orm/SqlHelper.java | 129 +++++++++----- .../com/arialyy/compiler/ElementHandler.java | 44 +++-- .../arialyy/compiler/ProxyMethodParam.java | 4 +- .../java/com/arialyy/compiler/TaskEnum.java | 10 +- .../simple/base/adapter/AbsRVAdapter.java | 5 + .../download/HighestPriorityActivity.java | 5 +- .../simple/download/SingleTaskActivity.java | 2 +- .../simple/download/group/GroupModule.java | 4 +- .../multi_download/DownloadAdapter.java | 165 ++++++++++++------ .../multi_download/FileListAdapter.java | 2 +- .../multi_download/MultiDownloadActivity.java | 65 +++++-- .../download/multi_download/MultiEntity.java | 33 ++++ .../download/multi_download/MultiModule.java | 29 +++ .../multi_download/MultiTaskActivity.java | 3 - .../simple/widget/NoScrollListView.java | 43 +++++ .../main/res/layout/item_group_download.xml | 18 ++ .../main/res/layout/item_simple_download.xml | 9 + ..._download.xml => layout_item_progress.xml} | 21 +-- 44 files changed, 653 insertions(+), 231 deletions(-) create mode 100644 Aria/src/main/java/com/arialyy/aria/orm/NormalList.java create mode 100644 app/src/main/java/com/arialyy/simple/download/multi_download/MultiEntity.java create mode 100644 app/src/main/java/com/arialyy/simple/download/multi_download/MultiModule.java create mode 100644 app/src/main/java/com/arialyy/simple/widget/NoScrollListView.java create mode 100644 app/src/main/res/layout/item_group_download.xml create mode 100644 app/src/main/res/layout/item_simple_download.xml rename app/src/main/res/layout/{item_download.xml => layout_item_progress.xml} (81%) diff --git a/Aria/jcenter.gradle b/Aria/jcenter.gradle index efbb3ea1..8ce3129a 100644 --- a/Aria/jcenter.gradle +++ b/Aria/jcenter.gradle @@ -76,7 +76,7 @@ artifacts { //################################# jcenter 上传配置 start ######################################### bintray { // user = hasProperty("bintrayUser") ? getProperty("bintrayUser") : getProperty("BINTRAY_USER") -// key = hasProperty("bintrayKey") ? getProperty("bintrayKey") : getProperty("BINTRAY_KEY") +// groupName = hasProperty("bintrayKey") ? getProperty("bintrayKey") : getProperty("BINTRAY_KEY") user = BINTRAY_USER key = BINTRAY_KEY configurations = ['archives'] diff --git a/Aria/src/main/java/com/arialyy/aria/core/command/group/GroupStartCmd.java b/Aria/src/main/java/com/arialyy/aria/core/command/group/GroupStartCmd.java index 3bd7bea8..4fde6115 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/command/group/GroupStartCmd.java +++ b/Aria/src/main/java/com/arialyy/aria/core/command/group/GroupStartCmd.java @@ -56,7 +56,7 @@ class GroupStartCmd extends AbsGroupCmd { if (mod.equals(QueueMod.NOW.getTag())) { mQueue.startTask(task); } else if (mod.equals(QueueMod.WAIT.getTag())) { - if (mQueue.getExePoolSize() < maxTaskNum) { + if (mQueue.getCurrentExePoolNum() < maxTaskNum) { mQueue.startTask(task); } } 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 2a019bb7..1233ebe1 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 @@ -27,7 +27,7 @@ final class ResumeAllCmd extends AbsNormalCmd { List allEntity = DbEntity.findDatas(DownloadEntity.class, "state=?", IEntity.STATE_STOP + ""); for (DownloadEntity entity : allEntity) { - int exeNum = mQueue.getExePoolSize(); + int exeNum = mQueue.getCurrentExePoolNum(); if (exeNum == 0 || exeNum < mQueue.getMaxTaskNum()) { AbsTask task = createTask(entity); mQueue.startTask(task); 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 c8f81cb1..c19ce78d 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 @@ -19,9 +19,15 @@ package com.arialyy.aria.core.command.normal; import android.text.TextUtils; import com.arialyy.aria.core.AriaManager; import com.arialyy.aria.core.QueueMod; +import com.arialyy.aria.core.download.DownloadGroupTask; +import com.arialyy.aria.core.download.DownloadTask; import com.arialyy.aria.core.inf.AbsTask; import com.arialyy.aria.core.inf.IEntity; import com.arialyy.aria.core.inf.AbsTaskEntity; +import com.arialyy.aria.core.queue.DownloadGroupTaskQueue; +import com.arialyy.aria.core.queue.DownloadTaskQueue; +import com.arialyy.aria.core.scheduler.DQueueMapping; +import com.arialyy.aria.orm.Primary; /** * Created by lyy on 2016/8/22. @@ -57,14 +63,13 @@ class StartCmd extends AbsNormalCmd { if (mod.equals(QueueMod.NOW.getTag())) { mQueue.startTask(task); } else if (mod.equals(QueueMod.WAIT.getTag())) { - if (mQueue.getExePoolSize() < maxTaskNum) { + if (mQueue.getCurrentExePoolNum() < maxTaskNum || task.getState() == IEntity.STATE_STOP) { mQueue.startTask(task); } } } else { // 任务不存在时,根据配置不同,对任务执行操作 - if (!task.isRunning() && mod.equals(QueueMod.WAIT.getTag()) && (task.getState() - == IEntity.STATE_WAIT || task.getState() == IEntity.STATE_STOP)) { + if (!task.isRunning()) { mQueue.startTask(task); } } 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 b55b7f00..f710c50f 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 @@ -53,6 +53,10 @@ public class DownloadEntity extends AbsNormalEntity implements Parcelable { */ private String serverFileName = ""; + @Override public String getKey() { + return downloadUrl; + } + public DownloadEntity() { } 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 ff0b683a..da96b5ff 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 @@ -17,6 +17,7 @@ package com.arialyy.aria.core.download; import android.os.Parcel; import com.arialyy.aria.core.inf.AbsGroupEntity; +import com.arialyy.aria.orm.NormalList; import com.arialyy.aria.orm.OneToMany; import java.util.ArrayList; import java.util.List; @@ -27,16 +28,26 @@ import java.util.List; */ public class DownloadGroupEntity extends AbsGroupEntity { - @OneToMany(table = DownloadEntity.class, key = "groupName") private List subtask = new ArrayList<>(); + @OneToMany(table = DownloadEntity.class, key = "groupName") private List subtask = + new ArrayList<>(); - //任务组下载文件的文件夹地址 + /** + * 子任务链接组 + */ + @NormalList(clazz = String.class) private List urls = new ArrayList<>(); + + /** + * 任务组下载文件的文件夹地址 + * + * @see DownloadGroupTarget#setDownloadDirPath(String) + */ private String dirPath = ""; public List getSubTask() { return subtask; } - public void setSubTasks(List subTasks) { + void setSubTasks(List subTasks) { this.subtask = subTasks; } @@ -48,6 +59,18 @@ public class DownloadGroupEntity extends AbsGroupEntity { this.dirPath = dirPath; } + public List getUrls() { + return urls; + } + + void setUrls(List urls) { + this.urls = urls; + } + + void setGroupName(String key) { + this.groupName = key; + } + public DownloadGroupEntity() { } 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 7f6f342e..e0fd311a 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 @@ -18,6 +18,7 @@ package com.arialyy.aria.core.download; import android.text.TextUtils; import com.arialyy.aria.core.inf.AbsGroupTarget; import com.arialyy.aria.orm.DbEntity; +import com.arialyy.aria.util.CheckUtil; import com.arialyy.aria.util.CommonUtil; import java.io.File; import java.util.ArrayList; @@ -40,14 +41,26 @@ public class DownloadGroupTarget */ private boolean isSetDirPathed = false; + DownloadGroupTarget(DownloadGroupEntity groupEntity, String targetName) { + this.mTargetName = targetName; + if (groupEntity.getUrls() != null && !groupEntity.getUrls().isEmpty()) { + this.mUrls.addAll(groupEntity.getUrls()); + } + init(groupEntity.getGroupName()); + } + DownloadGroupTarget(List urls, String targetName) { this.mTargetName = targetName; this.mUrls = urls; - mGroupName = CommonUtil.getMd5Code(urls); - mTaskEntity = DbEntity.findFirst(DownloadGroupTaskEntity.class, "key=?", mGroupName); + init(CommonUtil.getMd5Code(urls)); + } + + private void init(String key) { + mGroupName = key; + mTaskEntity = DbEntity.findFirst(DownloadGroupTaskEntity.class, "key=?", key); if (mTaskEntity == null) { mTaskEntity = new DownloadGroupTaskEntity(); - mTaskEntity.key = mGroupName; + mTaskEntity.key = key; mTaskEntity.entity = getDownloadGroupEntity(); mTaskEntity.insert(); } @@ -57,18 +70,43 @@ public class DownloadGroupTarget mEntity = mTaskEntity.entity; } + /** + * 查询任务组实体,如果数据库不存在该实体,则新创建一个新的任务组实体 + */ private DownloadGroupEntity getDownloadGroupEntity() { DownloadGroupEntity entity = DbEntity.findFirst(DownloadGroupEntity.class, "groupName=?", mGroupName); if (entity == null) { entity = new DownloadGroupEntity(); entity.setGroupName(mGroupName); - entity.setUrlmd5(mGroupName); + entity.setUrls(mUrls); entity.insert(); } return entity; } + /** + * 设置任务组别名 + */ + public DownloadGroupTarget setGroupAlias(String alias) { + if (TextUtils.isEmpty(alias)) return this; + mEntity.setAlias(alias); + mEntity.update(); + return this; + } + + /** + * 如果你是使用{@link DownloadReceiver#load(DownloadGroupEntity)}进行下载操作,那么你需要设置任务组的下载地址 + */ + public DownloadGroupTarget setGroupUrl(List urls) { + CheckUtil.checkDownloadUrls(urls); + mUrls.clear(); + mUrls.addAll(urls); + mEntity.setGroupName(CommonUtil.getMd5Code(urls)); + mEntity.update(); + return this; + } + /** * 设置任务组的文件夹路径,在Aria中,任务组的所有子任务都会下载到以任务组组名的文件夹中。 * 如:groupDirPath = "/mnt/sdcard/download/group_test" 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 be7815c1..2a28c567 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 @@ -17,6 +17,7 @@ package com.arialyy.aria.core.download; import android.support.annotation.NonNull; import com.arialyy.aria.core.AriaManager; +import com.arialyy.aria.core.inf.AbsEntity; import com.arialyy.aria.core.inf.AbsReceiver; import com.arialyy.aria.core.inf.IReceiver; import com.arialyy.aria.core.command.normal.NormalCmdFactory; @@ -27,6 +28,7 @@ import com.arialyy.aria.core.upload.ProxyHelper; import com.arialyy.aria.orm.DbEntity; import com.arialyy.aria.util.CheckUtil; import com.arialyy.aria.util.CommonUtil; +import java.util.ArrayList; import java.util.List; import java.util.Set; @@ -49,9 +51,9 @@ public class DownloadReceiver extends AbsReceiver { } /** - * {@link #load(String)},请使用该方法 + * 使用下载实体执行下载操作 */ - @Deprecated public DownloadTarget load(DownloadEntity entity) { + public DownloadTarget load(DownloadEntity entity) { return new DownloadTarget(entity, targetName); } @@ -71,6 +73,16 @@ public class DownloadReceiver extends AbsReceiver { return new DownloadGroupTarget(urls, targetName); } + /** + * 使用任务组实体执行任务组的实体执行任务组的下载操作 + * + * @param groupEntity 如果加载的任务实体没有子项的下载地址, + * 那么你需要使用{@link DownloadGroupTarget#setGroupUrl(List)}设置子项的下载地址 + */ + public DownloadGroupTarget load(DownloadGroupEntity groupEntity) { + return new DownloadGroupTarget(groupEntity, targetName); + } + /** * 将当前类注册到Aria */ @@ -144,7 +156,7 @@ public class DownloadReceiver extends AbsReceiver { */ public DownloadTaskEntity getDownloadTask(String downloadUrl) { CheckUtil.checkDownloadUrl(downloadUrl); - return DbEntity.findFirst(DownloadTaskEntity.class, "key=? and isGroupTask='false'", + return DbEntity.findFirst(DownloadTaskEntity.class, "groupName=? and isGroupTask='false'", downloadUrl); } @@ -167,15 +179,31 @@ public class DownloadReceiver extends AbsReceiver { /** * 获取普通下载任务列表 */ - @Override public List getTaskList() { + @Override public List getSimpleTaskList() { return DownloadEntity.findDatas(DownloadEntity.class, "isGroupChild=?", "false"); } /** * 获取任务组列表 */ - public List getGroupTaskList() { - return DownloadEntity.findAllData(DownloadGroupTaskEntity.class); + public List getGroupTaskList() { + return DownloadEntity.findAllData(DownloadGroupEntity.class); + } + + /** + * 获取普通任务和任务组的任务列表 + */ + public List getTotleTaskList() { + List list = new ArrayList<>(); + List simpleTask = getSimpleTaskList(); + List groupTask = getGroupTaskList(); + if (simpleTask != null && !simpleTask.isEmpty()) { + list.addAll(simpleTask); + } + if (groupTask != null && !groupTask.isEmpty()) { + list.addAll(groupTask); + } + return list; } /** diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadTarget.java b/Aria/src/main/java/com/arialyy/aria/core/download/DownloadTarget.java index 4dec4117..c77e853c 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadTarget.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/DownloadTarget.java @@ -112,6 +112,7 @@ public class DownloadTarget mEntity.setDownloadPath(downloadPath); mEntity.setFileName(file.getName()); mTaskEntity.key = downloadPath; + mEntity.update(); mTaskEntity.update(); return this; } 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 91ad7090..cb35232d 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 @@ -142,6 +142,11 @@ public abstract class AbsEntity extends DbEntity implements IEntity, Parcelable this.completeTime = completeTime; } + /** + * 实体唯一标识符 + */ + public abstract String getKey(); + public AbsEntity() { } @@ -174,5 +179,4 @@ public abstract class AbsEntity extends DbEntity implements IEntity, Parcelable this.completeTime = in.readLong(); this.isComplete = in.readByte() != 0; } - } 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 6a708b98..35ef1d0d 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 @@ -24,30 +24,29 @@ import com.arialyy.aria.orm.Primary; */ public abstract class AbsGroupEntity extends AbsEntity implements Parcelable { /** - * 组名 + * 组名,组名为任务地址相加的urlMd5 */ - @Primary - private String groupName = ""; + @Primary protected String groupName = ""; /** - * 任务地址相加的urlmd5 + * 任务组别名 */ - private String urlmd5 = ""; + private String alias = ""; - public String getUrlmd5() { - return urlmd5; + public String getGroupName() { + return groupName; } - public void setUrlmd5(String urlmd5) { - this.urlmd5 = urlmd5; + public String getAlias() { + return alias; } - public String getGroupName() { + @Override public String getKey() { return groupName; } - public void setGroupName(String groupName) { - this.groupName = groupName; + public void setAlias(String alias) { + this.alias = alias; } public AbsGroupEntity() { @@ -60,12 +59,12 @@ public abstract class AbsGroupEntity extends AbsEntity implements Parcelable { @Override public void writeToParcel(Parcel dest, int flags) { super.writeToParcel(dest, flags); dest.writeString(this.groupName); - dest.writeString(this.urlmd5); + dest.writeString(this.alias); } protected AbsGroupEntity(Parcel in) { super(in); this.groupName = in.readString(); - this.urlmd5 = in.readString(); + this.alias = in.readString(); } } diff --git a/Aria/src/main/java/com/arialyy/aria/core/inf/IEntity.java b/Aria/src/main/java/com/arialyy/aria/core/inf/IEntity.java index fe529b49..156a01ce 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/inf/IEntity.java +++ b/Aria/src/main/java/com/arialyy/aria/core/inf/IEntity.java @@ -20,42 +20,41 @@ import com.arialyy.aria.orm.Ignore; /** * Created by lyy on 2017/2/23. */ - public interface IEntity { /** * 其它状态 */ - @Ignore public static final int STATE_OTHER = -1; + @Ignore int STATE_OTHER = -1; /** * 失败状态 */ - @Ignore public static final int STATE_FAIL = 0; + @Ignore int STATE_FAIL = 0; /** * 完成状态 */ - @Ignore public static final int STATE_COMPLETE = 1; + @Ignore int STATE_COMPLETE = 1; /** * 停止状态 */ - @Ignore public static final int STATE_STOP = 2; + @Ignore int STATE_STOP = 2; /** * 等待状态 */ - @Ignore public static final int STATE_WAIT = 3; + @Ignore int STATE_WAIT = 3; /** * 下载中 */ - @Ignore public static final int STATE_RUNNING = 4; + @Ignore int STATE_RUNNING = 4; /** * 预处理 */ - @Ignore public static final int STATE_PRE = 5; + @Ignore int STATE_PRE = 5; /** * 预处理完成 */ - @Ignore public static final int STATE_POST_PRE = 6; + @Ignore int STATE_POST_PRE = 6; /** * 取消下载 */ - @Ignore public static final int STATE_CANCEL = 7; + @Ignore int STATE_CANCEL = 7; } diff --git a/Aria/src/main/java/com/arialyy/aria/core/inf/IReceiver.java b/Aria/src/main/java/com/arialyy/aria/core/inf/IReceiver.java index 19b1b11f..dc347eee 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/inf/IReceiver.java +++ b/Aria/src/main/java/com/arialyy/aria/core/inf/IReceiver.java @@ -56,5 +56,5 @@ public interface IReceiver { /** * 获取任务列表 */ - public List getTaskList(); + public List getSimpleTaskList(); } diff --git a/Aria/src/main/java/com/arialyy/aria/core/queue/AbsTaskQueue.java b/Aria/src/main/java/com/arialyy/aria/core/queue/AbsTaskQueue.java index 2edafc91..097bbafd 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/queue/AbsTaskQueue.java +++ b/Aria/src/main/java/com/arialyy/aria/core/queue/AbsTaskQueue.java @@ -102,7 +102,7 @@ abstract class AbsTaskQueue 0; + boolean hasNextTask() { + return mQueue.getCurrentCachePoolNum() > 0; + } + + /** + * 获取正在执行的队列数 + */ + int getExeTaskNum() { + return mQueue.getCurrentExePoolNum(); } } diff --git a/Aria/src/main/java/com/arialyy/aria/core/scheduler/DQueueMapping.java b/Aria/src/main/java/com/arialyy/aria/core/scheduler/DQueueMapping.java index f7be0703..623a4a0e 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/scheduler/DQueueMapping.java +++ b/Aria/src/main/java/com/arialyy/aria/core/scheduler/DQueueMapping.java @@ -16,10 +16,11 @@ package com.arialyy.aria.core.scheduler; import com.arialyy.aria.core.AriaManager; +import com.arialyy.aria.core.queue.DownloadGroupTaskQueue; +import com.arialyy.aria.core.queue.DownloadTaskQueue; import java.util.Iterator; import java.util.LinkedHashMap; import java.util.Map; -import java.util.Set; /** * Created by Aria.Lao on 2017/7/13. @@ -30,7 +31,7 @@ public class DQueueMapping { public static final int QUEUE_TYPE_DOWNLOAD = 0xa1; public static final int QUEUE_TYPE_DOWNLOAD_GROUP = 0xa2; public static final int QUEUE_NONE = 0xab2; - LinkedHashMap types = new LinkedHashMap<>(); + private LinkedHashMap types = new LinkedHashMap<>(); private static volatile DQueueMapping instance = null; @@ -79,4 +80,10 @@ public class DQueueMapping { } return QUEUE_NONE; } + + public boolean canStart() { + return DownloadTaskQueue.getInstance().getCurrentExePoolNum() + + DownloadGroupTaskQueue.getInstance().getCurrentExePoolNum() >= AriaManager.getInstance( + AriaManager.APP).getDownloadConfig().getMaxTaskNum(); + } } diff --git a/Aria/src/main/java/com/arialyy/aria/core/scheduler/DownloadGroupSchedulers.java b/Aria/src/main/java/com/arialyy/aria/core/scheduler/DownloadGroupSchedulers.java index 4ff938ae..dd4dde4a 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/scheduler/DownloadGroupSchedulers.java +++ b/Aria/src/main/java/com/arialyy/aria/core/scheduler/DownloadGroupSchedulers.java @@ -23,6 +23,7 @@ import com.arialyy.aria.core.queue.DownloadGroupTaskQueue; /** * Created by AriaL on 2017/7/2. + * 任务组调度器 */ public class DownloadGroupSchedulers extends AbsSchedulers { @@ -51,6 +52,10 @@ public class DownloadGroupSchedulers extends } @Override protected void startNextTask() { + if (getExeTaskNum() + DownloadSchedulers.getInstance().getExeTaskNum() + >= AriaManager.getInstance(AriaManager.APP).getDownloadConfig().getMaxTaskNum()) { + return; + } if (!DownloadSchedulers.getInstance().hasNextTask()) { nextSelf(); } else { diff --git a/Aria/src/main/java/com/arialyy/aria/core/scheduler/DownloadSchedulers.java b/Aria/src/main/java/com/arialyy/aria/core/scheduler/DownloadSchedulers.java index b5fae673..5226c13b 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/scheduler/DownloadSchedulers.java +++ b/Aria/src/main/java/com/arialyy/aria/core/scheduler/DownloadSchedulers.java @@ -16,6 +16,7 @@ package com.arialyy.aria.core.scheduler; +import com.arialyy.aria.core.Aria; import com.arialyy.aria.core.AriaManager; import com.arialyy.aria.core.download.DownloadTaskEntity; import com.arialyy.aria.core.queue.DownloadTaskQueue; @@ -55,6 +56,10 @@ public class DownloadSchedulers } @Override protected void startNextTask() { + if (getExeTaskNum() + DownloadGroupSchedulers.getInstance().getExeTaskNum() + >= AriaManager.getInstance(AriaManager.APP).getDownloadConfig().getMaxTaskNum()) { + return; + } if (!DownloadGroupSchedulers.getInstance().hasNextTask()) { nextSelf(); } else { diff --git a/Aria/src/main/java/com/arialyy/aria/core/upload/UploadEntity.java b/Aria/src/main/java/com/arialyy/aria/core/upload/UploadEntity.java index 992cf254..05bfc5cf 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/upload/UploadEntity.java +++ b/Aria/src/main/java/com/arialyy/aria/core/upload/UploadEntity.java @@ -38,6 +38,10 @@ public class UploadEntity extends AbsNormalEntity implements Parcelable { this.filePath = filePath; } + @Override public String getKey() { + return filePath; + } + public UploadEntity() { } 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 00d44acb..ba938c5d 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 @@ -62,7 +62,7 @@ public class UploadReceiver extends AbsReceiver { return DbEntity.findFirst(UploadEntity.class, "filePath=?", filePath) != null; } - @Override public List getTaskList() { + @Override public List getSimpleTaskList() { return DbEntity.findAllData(UploadEntity.class); } diff --git a/Aria/src/main/java/com/arialyy/aria/core/upload/UploadTarget.java b/Aria/src/main/java/com/arialyy/aria/core/upload/UploadTarget.java index 007ac0e3..b8e473e1 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/upload/UploadTarget.java +++ b/Aria/src/main/java/com/arialyy/aria/core/upload/UploadTarget.java @@ -28,7 +28,7 @@ public class UploadTarget extends AbsNormalTarget mapping = new HashMap<>(); static String DB_NAME; - static int VERSION = 9; + static int VERSION = 10; static { if (TextUtils.isEmpty(DB_NAME)) { diff --git a/Aria/src/main/java/com/arialyy/aria/orm/DbEntity.java b/Aria/src/main/java/com/arialyy/aria/orm/DbEntity.java index c8636c8c..51edc1a4 100644 --- a/Aria/src/main/java/com/arialyy/aria/orm/DbEntity.java +++ b/Aria/src/main/java/com/arialyy/aria/orm/DbEntity.java @@ -171,7 +171,11 @@ public class DbEntity { if (SqlHelper.isOneToOne(field)) { values.add(SqlHelper.getOneToOneParams(field)); } else if (type == List.class) { - values.add(SqlHelper.getListElementParams(field)); + if (SqlHelper.isOneToMany(field)) { + values.add(SqlHelper.getOneToManyElementParams(field)); + } else { + values.add(SqlHelper.list2Str(this, field)); + } } else if (type == Map.class) { values.add(SqlHelper.map2Str((Map) field.get(this))); } else { diff --git a/Aria/src/main/java/com/arialyy/aria/orm/DbUtil.java b/Aria/src/main/java/com/arialyy/aria/orm/DbUtil.java index 1cd65317..eeaf96f7 100644 --- a/Aria/src/main/java/com/arialyy/aria/orm/DbUtil.java +++ b/Aria/src/main/java/com/arialyy/aria/orm/DbUtil.java @@ -187,7 +187,7 @@ public class DbUtil { Log.e(TAG, "请输入删除条件"); return -1; } else if (wheres.length != values.length) { - Log.e(TAG, "key 和 vaule 长度不相等"); + Log.e(TAG, "groupName 和 vaule 长度不相等"); return -1; } StringBuilder sb = new StringBuilder(); diff --git a/Aria/src/main/java/com/arialyy/aria/orm/NormalList.java b/Aria/src/main/java/com/arialyy/aria/orm/NormalList.java new file mode 100644 index 00000000..2d2ba512 --- /dev/null +++ b/Aria/src/main/java/com/arialyy/aria/orm/NormalList.java @@ -0,0 +1,32 @@ +/* + * 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.orm; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * Created by AriaL on 2017/7/4. + * 基本类型的List,只能用于常见的数据类型,如果是一对多的复杂数据结构,需要使用{@link OneToMany} + */ +@Target(ElementType.FIELD) @Retention(RetentionPolicy.RUNTIME) public @interface NormalList { + /** + * 数据类型 + */ + Class clazz(); +} 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 11896bf2..2cdc017d 100644 --- a/Aria/src/main/java/com/arialyy/aria/orm/SqlHelper.java +++ b/Aria/src/main/java/com/arialyy/aria/orm/SqlHelper.java @@ -29,10 +29,10 @@ import com.arialyy.aria.util.CommonUtil; import java.lang.reflect.Field; import java.lang.reflect.Modifier; import java.lang.reflect.Type; -import java.lang.reflect.TypeVariable; import java.util.ArrayList; import java.util.Date; import java.util.HashMap; +import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Set; @@ -220,7 +220,7 @@ final class SqlHelper extends SQLiteOpenHelper { Log.e(TAG, "请输入查询条件"); return null; } else if (wheres.length != values.length) { - Log.e(TAG, "key 和 vaule 长度不相等"); + Log.e(TAG, "groupName 和 vaule 长度不相等"); return null; } StringBuilder sb = new StringBuilder(); @@ -297,7 +297,11 @@ final class SqlHelper extends SQLiteOpenHelper { if (type == Map.class) { value = map2Str((Map) field.get(dbEntity)); } else if (type == List.class) { - value = getListElementParams(field); + if (isOneToMany(field)) { + value = getOneToManyElementParams(field); + } else { + value = list2Str(dbEntity, field); + } } else if (isOneToOne(field)) { value = getOneToOneParams(field); } else { @@ -354,7 +358,11 @@ final class SqlHelper extends SQLiteOpenHelper { if (type == Map.class) { sb.append(map2Str((Map) field.get(dbEntity))); } else if (type == List.class) { - sb.append(getListElementParams(field)); + if (isOneToMany(field)) { + sb.append(getOneToManyElementParams(field)); + } else { + sb.append(list2Str(dbEntity, field)); + } } else if (isOneToOne(field)) { sb.append(getOneToOneParams(field)); } else { @@ -373,13 +381,6 @@ final class SqlHelper extends SQLiteOpenHelper { close(db); } - /** - * 保存一对一的数据 - */ - private void saveOneToOneFile(Field field) { - - } - /** * 获取一对一参数 */ @@ -396,10 +397,10 @@ final class SqlHelper extends SQLiteOpenHelper { * * @param field list反射字段 */ - static String getListElementParams(Field field) { + static String getOneToManyElementParams(Field field) { OneToMany oneToMany = field.getAnnotation(OneToMany.class); if (oneToMany == null) { - throw new IllegalArgumentException("List中元素必须被@OneToMany注解"); + throw new IllegalArgumentException("一对多元素必须被@OneToMany注解"); } //关联的表名 String tableName = oneToMany.table().getName(); @@ -408,6 +409,63 @@ final class SqlHelper extends SQLiteOpenHelper { return tableName + "$$" + key; } + /** + * 列表数据转字符串 + * + * @param field list反射字段 + */ + static String list2Str(DbEntity dbEntity, Field field) throws IllegalAccessException { + NormalList normalList = field.getAnnotation(NormalList.class); + if (normalList == null) { + throw new IllegalArgumentException("List中元素必须被@NormalList注解"); + } + List list = (List) field.get(dbEntity); + if (list == null || list.isEmpty()) return ""; + StringBuilder sb = new StringBuilder(); + for (Object aList : list) { + sb.append(aList).append("$$"); + } + return sb.toString(); + } + + /** + * 字符串转列表 + * + * @param str 数据库中的字段 + * @return 如果str为null,则返回null + */ + private static List str2List(String str, Field field) { + NormalList normalList = field.getAnnotation(NormalList.class); + if (normalList == null) { + throw new IllegalArgumentException("List中元素必须被@NormalList注解"); + } + if (TextUtils.isEmpty(str)) return null; + String[] datas = str.split("$$"); + List list = new ArrayList(); + String type = normalList.clazz().getName(); + for (String data : datas) { + list.add(checkData(data, type)); + } + return list; + } + + private static Object checkData(String type, String data) { + switch (type) { + case "String": + return data; + case "int": + case "Integer": + return Integer.parseInt(data); + case "double": + case "Double": + return Double.parseDouble(data); + case "float": + case "Float": + return Float.parseFloat(data); + } + return null; + } + /** * 查找class的主键字段 * @@ -510,23 +568,6 @@ final class SqlHelper extends SQLiteOpenHelper { close(db); } - ///** - // * 通过字段类型和获取保存在数据库表字段名 - // */ - //private static String getFieldName(Class type, Field field) { - // String fieldName; - // if (type == Map.class) { - // fieldName = MAP_FIELD + field.getName(); - // } else if (type == List.class) { - // fieldName = LIST_FIELD + field.getName(); - // } else if (isGeneric(field)) { - // fieldName = GENERIC_FIELD + field.getName(); - // } else { - // fieldName = field.getName(); - // } - // return fieldName; - //} - /** * 打印数据库日志 * @@ -598,19 +639,23 @@ final class SqlHelper extends SQLiteOpenHelper { } else if (type == Map.class) { field.set(entity, str2Map(cursor.getString(column))); } else if (type == List.class) { - //主键字段 - String primaryKey = getPrimaryName(clazz); - if (TextUtils.isEmpty(primaryKey)) { - throw new IllegalArgumentException("List中的元素对象必须需要@Primary注解的字段"); + String value = cursor.getString(column); + if (isOneToMany(field)) { + //主键字段 + String primaryKey = getPrimaryName(clazz); + if (TextUtils.isEmpty(primaryKey)) { + throw new IllegalArgumentException("List中的元素对象必须需要@Primary注解的字段"); + } + //list字段保存的数据 + int kc = cursor.getColumnIndex(primaryKey); + String primaryData = cursor.getString(kc); + if (TextUtils.isEmpty(primaryData)) continue; + List list = findForeignData(db, primaryData, value); + if (list == null) continue; + field.set(entity, findForeignData(db, primaryData, value)); + } else { + field.set(entity, str2List(value, field)); } - //list字段保存的数据 - int kc = cursor.getColumnIndex(primaryKey); - String params = cursor.getString(column); - String primaryData = cursor.getString(kc); - if (TextUtils.isEmpty(primaryData)) continue; - List list = findForeignData(db, primaryData, params); - if (list == null) continue; - field.set(entity, findForeignData(db, primaryData, params)); } else if (isOneToOne(field)) { String primaryKey = getPrimaryName(clazz); if (TextUtils.isEmpty(primaryKey)) { diff --git a/AriaCompiler/src/main/java/com/arialyy/compiler/ElementHandler.java b/AriaCompiler/src/main/java/com/arialyy/compiler/ElementHandler.java index 1f460e0c..5cbdd005 100644 --- a/AriaCompiler/src/main/java/com/arialyy/compiler/ElementHandler.java +++ b/AriaCompiler/src/main/java/com/arialyy/compiler/ElementHandler.java @@ -50,6 +50,7 @@ import javax.lang.model.util.Elements; * 元素处理 */ class ElementHandler { + private static final boolean DEBUG = false; private Filer mFiler; private Elements mElementUtil; @@ -170,16 +171,25 @@ class ElementHandler { Set keys = mMethods.keySet(); for (String key : keys) { ProxyMethodParam entity = mMethods.get(key); - JavaFile jf = JavaFile.builder(entity.packageName, createProxyClass(entity)).build(); + for (TaskEnum taskEnum : entity.taskEnums) { + JavaFile jf = + JavaFile.builder(entity.packageName, createProxyClass(entity, taskEnum)).build(); + createFile(jf); + } + } + } - jf.writeTo(mFiler); + private void createFile(JavaFile jf) throws IOException { + if (DEBUG) { // 如果需要在控制台打印生成的文件,则去掉下面的注释 - //jf.writeTo(System.out); + jf.writeTo(System.out); + } else { + jf.writeTo(mFiler); } } /** - * 每一种注解的类集合 + * 每一种注解对应的类集合 */ private void createProxyClassFile() throws IOException { Set keys = mListenerClass.keySet(); @@ -212,8 +222,7 @@ class ElementHandler { ProxyConstance.COUNT_DOWNLOAD_GROUP)); JavaFile jf = JavaFile.builder(ProxyConstance.PROXY_COUNTER_PACKAGE, builder.build()).build(); - jf.writeTo(mFiler); - //jf.writeTo(System.out); + createFile(jf); } /** @@ -287,8 +296,7 @@ class ElementHandler { /** * 创建代理类 */ - private TypeSpec createProxyClass(ProxyMethodParam entity) { - TaskEnum taskEnum = entity.taskEnum; + private TypeSpec createProxyClass(ProxyMethodParam entity, TaskEnum taskEnum) { TypeSpec.Builder builder = TypeSpec.classBuilder(entity.className + taskEnum.getProxySuffix()) .addModifiers(Modifier.PUBLIC, Modifier.FINAL); @@ -305,9 +313,12 @@ class ElementHandler { builder.addField(mappingField); //添加注解方法 - for (Class annotation : entity.methods.keySet()) { - MethodSpec method = createProxyMethod(taskEnum, annotation, entity.methods.get(annotation)); - builder.addMethod(method); + Map, String> temp = entity.methods.get(taskEnum); + if (temp != null) { + for (Class annotation : temp.keySet()) { + MethodSpec method = createProxyMethod(taskEnum, annotation, temp.get(annotation)); + builder.addMethod(method); + } } //增加构造函数 @@ -345,8 +356,7 @@ class ElementHandler { //创建父类参数 ClassName superClass = ClassName.get("com.arialyy.aria.core.scheduler", "AbsSchedulerListener"); //创建泛型 - ClassName typeVariableName = - ClassName.get(entity.taskEnum.getPkg(), entity.taskEnum.getClassName()); + ClassName typeVariableName = ClassName.get(taskEnum.getPkg(), taskEnum.getClassName()); builder.superclass(ParameterizedTypeName.get(superClass, typeVariableName)); builder.addMethod(listener); return builder.build(); @@ -373,12 +383,16 @@ class ElementHandler { ProxyMethodParam proxyEntity = mMethods.get(className); if (proxyEntity == null) { proxyEntity = new ProxyMethodParam(); - proxyEntity.taskEnum = taskEnum; + proxyEntity.taskEnums = new HashSet<>(); proxyEntity.packageName = packageElement.getQualifiedName().toString(); proxyEntity.className = classElement.getSimpleName().toString(); mMethods.put(className, proxyEntity); } - proxyEntity.methods.put(annotationClazz, methodName); + proxyEntity.taskEnums.add(taskEnum); + if (proxyEntity.methods.get(taskEnum) == null) { + proxyEntity.methods.put(taskEnum, new HashMap, String>()); + } + proxyEntity.methods.get(taskEnum).put(annotationClazz, methodName); proxyEntity.keyMappings.put(methodName, getValues(taskEnum, method, annotationType)); } } diff --git a/AriaCompiler/src/main/java/com/arialyy/compiler/ProxyMethodParam.java b/AriaCompiler/src/main/java/com/arialyy/compiler/ProxyMethodParam.java index c0912ca9..3cc813aa 100644 --- a/AriaCompiler/src/main/java/com/arialyy/compiler/ProxyMethodParam.java +++ b/AriaCompiler/src/main/java/com/arialyy/compiler/ProxyMethodParam.java @@ -27,7 +27,7 @@ import java.util.Set; class ProxyMethodParam { String packageName; String className; - TaskEnum taskEnum; + Set taskEnums; Map> keyMappings = new HashMap<>(); - Map, String> methods = new HashMap<>(); + Map, String>> methods = new HashMap<>(); } diff --git a/AriaCompiler/src/main/java/com/arialyy/compiler/TaskEnum.java b/AriaCompiler/src/main/java/com/arialyy/compiler/TaskEnum.java index 293c19a5..7620f1df 100644 --- a/AriaCompiler/src/main/java/com/arialyy/compiler/TaskEnum.java +++ b/AriaCompiler/src/main/java/com/arialyy/compiler/TaskEnum.java @@ -20,10 +20,11 @@ package com.arialyy.compiler; * 任务类型枚举 */ enum TaskEnum { - DOWNLOAD("com.arialyy.aria.core.download", "DownloadTask", "$$DownloadListenerProxy"), - DOWNLOAD_GROUP("com.arialyy.aria.core.download", "DownloadGroupTask", "$$DownloadGroupListenerProxy"), - UPLOAD("com.arialyy.aria.core.upload", "UploadTask", "$$UploadListenerProxy"), - UPLOAD_GROUP("com.arialyy.aria.core.upload", "UploadGroupTask", "$$UploadGroupListenerProxy"); + DOWNLOAD("com.arialyy.aria.core.download", "DownloadTask", + "$$DownloadListenerProxy"), DOWNLOAD_GROUP("com.arialyy.aria.core.download", + "DownloadGroupTask", "$$DownloadGroupListenerProxy"), UPLOAD("com.arialyy.aria.core.upload", + "UploadTask", "$$UploadListenerProxy"), UPLOAD_GROUP("com.arialyy.aria.core.upload", + "UploadGroupTask", "$$UploadGroupListenerProxy"); String pkg, className, proxySuffix; @@ -49,5 +50,4 @@ enum TaskEnum { this.className = className; this.proxySuffix = proxySuffix; } - } diff --git a/app/src/main/java/com/arialyy/simple/base/adapter/AbsRVAdapter.java b/app/src/main/java/com/arialyy/simple/base/adapter/AbsRVAdapter.java index 744ccd94..f3f4d103 100644 --- a/app/src/main/java/com/arialyy/simple/base/adapter/AbsRVAdapter.java +++ b/app/src/main/java/com/arialyy/simple/base/adapter/AbsRVAdapter.java @@ -16,6 +16,8 @@ package com.arialyy.simple.base.adapter; import android.content.Context; +import android.databinding.DataBindingUtil; +import android.databinding.ViewDataBinding; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; @@ -45,6 +47,9 @@ public abstract class AbsRVAdapter @Override public Holder onCreateViewHolder(ViewGroup parent, int viewType) { View view = LayoutInflater.from(parent.getContext()).inflate(setLayoutId(viewType), parent, false); + //LayoutInflater inflater = LayoutInflater.from(parent.getContext()); + //VD binding = DataBindingUtil.inflate(inflater, setLayoutId(viewType), parent, false); + //; holder = getViewHolder(view, viewType); return holder; } diff --git a/app/src/main/java/com/arialyy/simple/download/HighestPriorityActivity.java b/app/src/main/java/com/arialyy/simple/download/HighestPriorityActivity.java index aee06f79..611861dd 100644 --- a/app/src/main/java/com/arialyy/simple/download/HighestPriorityActivity.java +++ b/app/src/main/java/com/arialyy/simple/download/HighestPriorityActivity.java @@ -29,6 +29,7 @@ import com.arialyy.aria.core.Aria; import com.arialyy.aria.core.download.DownloadEntity; import com.arialyy.aria.core.download.DownloadTarget; import com.arialyy.aria.core.download.DownloadTask; +import com.arialyy.aria.core.inf.AbsEntity; import com.arialyy.aria.core.inf.IEntity; import com.arialyy.frame.util.show.L; import com.arialyy.simple.R; @@ -58,7 +59,7 @@ public class HighestPriorityActivity extends BaseActivity mData = new ArrayList<>(); + private List mData = new ArrayList<>(); private Set mRecord = new HashSet<>(); @Override protected int setLayoutId() { @@ -83,7 +84,7 @@ public class HighestPriorityActivity extends BaseActivity temp = Aria.download(this).getTaskList(); + List temp = Aria.download(this).getSimpleTaskList(); if (temp != null && !temp.isEmpty()) { for (DownloadEntity entity : temp) { if (entity.getDownloadUrl().equals(DOWNLOAD_URL)) continue; 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 3e556da1..5e4612fa 100644 --- a/app/src/main/java/com/arialyy/simple/download/SingleTaskActivity.java +++ b/app/src/main/java/com/arialyy/simple/download/SingleTaskActivity.java @@ -190,7 +190,7 @@ public class SingleTaskActivity extends BaseActivity { if (text.equals("重新开始?") || text.equals("开始")) { Aria.download(this) .load(DOWNLOAD_URL) - .addHeader("key", "value") + .addHeader("groupName", "value") .setDownloadPath(Environment.getExternalStorageDirectory().getPath() + "/test.apk") .start(); } else if (text.equals("恢复")) { diff --git a/app/src/main/java/com/arialyy/simple/download/group/GroupModule.java b/app/src/main/java/com/arialyy/simple/download/group/GroupModule.java index ed98dff3..db478609 100644 --- a/app/src/main/java/com/arialyy/simple/download/group/GroupModule.java +++ b/app/src/main/java/com/arialyy/simple/download/group/GroupModule.java @@ -38,8 +38,8 @@ public class GroupModule extends BaseModule { return urls; } - //List convertPath(List urls){ - // List paths = new ArrayList<>(); + //NormalList convertPath(NormalList urls){ + // NormalList paths = new ArrayList<>(); // // for (String url : urls){ // 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 16ccf516..2e293105 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 @@ -16,6 +16,7 @@ package com.arialyy.simple.download.multi_download; +import android.annotation.SuppressLint; import android.content.Context; import android.content.res.Resources; import android.view.View; @@ -24,12 +25,15 @@ import android.widget.TextView; import butterknife.Bind; import com.arialyy.aria.core.Aria; import com.arialyy.aria.core.download.DownloadEntity; +import com.arialyy.aria.core.download.DownloadGroupEntity; +import com.arialyy.aria.core.inf.AbsEntity; import com.arialyy.aria.core.inf.IEntity; import com.arialyy.aria.util.CommonUtil; import com.arialyy.simple.R; import com.arialyy.simple.base.adapter.AbsHolder; import com.arialyy.simple.base.adapter.AbsRVAdapter; import com.arialyy.simple.widget.HorizontalProgressBarWithNumber; +import com.arialyy.simple.widget.NoScrollListView; import java.util.List; import java.util.Map; import java.util.Set; @@ -39,21 +43,32 @@ import java.util.concurrent.ConcurrentHashMap; * Created by Lyy on 2016/9/27. * 下载列表适配器 */ -public class DownloadAdapter extends AbsRVAdapter { +public class DownloadAdapter extends AbsRVAdapter { private static final String TAG = "DownloadAdapter"; private Map mPositions = new ConcurrentHashMap<>(); - public DownloadAdapter(Context context, List data) { + public DownloadAdapter(Context context, List data) { super(context, data); int i = 0; - for (DownloadEntity entity : data) { - mPositions.put(entity.getDownloadUrl(), i); + for (AbsEntity entity : data) { + mPositions.put(getKey(entity), i); i++; } } - @Override protected MyHolder getViewHolder(View convertView, int viewType) { - return new MyHolder(convertView); + private String getKey(AbsEntity entity) { + if (entity instanceof DownloadEntity) { + return ((DownloadEntity) entity).getDownloadUrl(); + } else if (entity instanceof DownloadGroupEntity) { + return ((DownloadGroupEntity) entity).getGroupName(); + } + return ""; + } + + @Override protected SimpleHolder getViewHolder(View convertView, int viewType) { + if (viewType == 1) return new SimpleHolder(convertView); + if (viewType == 2) return new GroupHolder(convertView); + return null; } public void addDownloadEntity(DownloadEntity entity) { @@ -61,21 +76,33 @@ public class DownloadAdapter extends AbsRVAdapter= mData.size()) { return; } @@ -84,8 +111,8 @@ public class DownloadAdapter extends AbsRVAdapter= mData.size()) { return; @@ -105,53 +132,67 @@ public class DownloadAdapter extends AbsRVAdapter { @Bind(R.id.list) RecyclerView mList; private DownloadAdapter mAdapter; - private List mData = new ArrayList<>(); - - String[] mFilterStr = new String[] { - "https://g37.gdl.netease.com/onmyoji_netease_10_1.0.20.apk", - "http://static.gaoshouyou.com/d/eb/f2/dfeba30541f209ab8a50d847fc1661ce.apk" - }; + private List mData = new ArrayList<>(); @Override protected int setLayoutId() { return R.layout.activity_multi_download; @@ -54,7 +52,7 @@ public class MultiDownloadActivity extends BaseActivity temps = Aria.download(this).getTaskList(); + List temps = Aria.download(this).getTotleTaskList(); if (temps != null && !temps.isEmpty()) { mData.addAll(temps); } @@ -74,37 +72,68 @@ public class MultiDownloadActivity extends BaseActivity { mAdapter.updateBtState(task.getKey(), true); } - - - @Override protected void dataCallback(int result, Object data) { super.dataCallback(result, data); if (result == DownloadNumDialog.RESULT_CODE) { diff --git a/app/src/main/java/com/arialyy/simple/widget/NoScrollListView.java b/app/src/main/java/com/arialyy/simple/widget/NoScrollListView.java new file mode 100644 index 00000000..b481ca64 --- /dev/null +++ b/app/src/main/java/com/arialyy/simple/widget/NoScrollListView.java @@ -0,0 +1,43 @@ +/* + * 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.simple.widget; + +import android.content.Context; +import android.util.AttributeSet; +import android.widget.ListView; + +/** + * Created by lyy on 2015/4/22. + * 不带有滑动功能的ListView + */ +public class NoScrollListView extends ListView { + public NoScrollListView(Context context) { + super(context); + } + + public NoScrollListView(Context context, AttributeSet attrs) { + super(context, attrs); + } + + public NoScrollListView(Context context, AttributeSet attrs, int defStyleAttr) { + super(context, attrs, defStyleAttr); + } + + @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { + int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST); + super.onMeasure(widthMeasureSpec, expandSpec); + } +} \ No newline at end of file diff --git a/app/src/main/res/layout/item_group_download.xml b/app/src/main/res/layout/item_group_download.xml new file mode 100644 index 00000000..6049a053 --- /dev/null +++ b/app/src/main/res/layout/item_group_download.xml @@ -0,0 +1,18 @@ + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/item_simple_download.xml b/app/src/main/res/layout/item_simple_download.xml new file mode 100644 index 00000000..3a46ad9e --- /dev/null +++ b/app/src/main/res/layout/item_simple_download.xml @@ -0,0 +1,9 @@ + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/item_download.xml b/app/src/main/res/layout/layout_item_progress.xml similarity index 81% rename from app/src/main/res/layout/item_download.xml rename to app/src/main/res/layout/layout_item_progress.xml index 0458d780..ae6f69a4 100644 --- a/app/src/main/res/layout/item_download.xml +++ b/app/src/main/res/layout/layout_item_progress.xml @@ -65,7 +65,6 @@ android:textColor="@color/bt_selector_cancel" /> - - - - - - \ No newline at end of file +