From b8a86890a72a2904ae445e31291100d43d1e684d Mon Sep 17 00:00:00 2001 From: AriaLyy <511455842@qq.com> Date: Mon, 17 Jul 2017 18:10:37 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BB=BB=E5=8A=A1=E7=BB=84bug=20=E4=BF=AE?= =?UTF-8?q?=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../aria/core/command/normal/StartCmd.java | 4 +- .../arialyy/aria/core/download/DListener.java | 6 ++ .../core/download/DownloadGroupTarget.java | 46 ++++++++-- .../aria/core/download/DownloadReceiver.java | 3 +- .../downloader/DownloadGroupUtil.java | 8 +- .../core/download/downloader/Downloader.java | 4 +- .../download/downloader/StateConstance.java | 4 - .../arialyy/aria/core/inf/AbsNormalTask.java | 6 +- .../com/arialyy/aria/core/inf/AbsTask.java | 15 +++ .../arialyy/aria/core/queue/AbsTaskQueue.java | 11 ++- .../core/queue/DownloadGroupTaskQueue.java | 11 ++- .../aria/core/queue/DownloadTaskQueue.java | 13 ++- .../aria/core/queue/UploadTaskQueue.java | 11 ++- .../aria/core/queue/pool/BaseCachePool.java | 3 +- .../aria/core/queue/pool/BaseExecutePool.java | 22 +++-- .../core/queue/pool/DownloadExecutePool.java | 12 +-- .../core/queue/pool/DownloadSharePool.java | 43 +++++++++ .../arialyy/aria/core/queue/pool/IPool.java | 4 +- .../core/queue/pool/UploadExecutePool.java | 28 ++++++ .../aria/core/queue/pool/UploadSharePool.java | 43 +++++++++ .../java/com/arialyy/aria/orm/DbEntity.java | 7 ++ .../java/com/arialyy/aria/orm/DbUtil.java | 7 ++ .../com/arialyy/aria/util/CommonUtil.java | 21 +++-- .../simple/base/adapter/AbsRVAdapter.java | 12 +++ .../download/group/DownloadGroupActivity.java | 4 +- .../simple/download/group/GroupModule.java | 10 +- .../multi_download/DownloadAdapter.java | 31 ++++++- .../simple/widget/SubStateLinearLayout.java | 92 +++++++++++++++++++ .../main/res/layout/item_group_download.xml | 12 ++- .../main/res/layout/layout_child_state.xml | 8 ++ app/src/main/res/values/strings.xml | 20 ++-- 31 files changed, 449 insertions(+), 72 deletions(-) create mode 100644 Aria/src/main/java/com/arialyy/aria/core/queue/pool/DownloadSharePool.java create mode 100644 Aria/src/main/java/com/arialyy/aria/core/queue/pool/UploadExecutePool.java create mode 100644 Aria/src/main/java/com/arialyy/aria/core/queue/pool/UploadSharePool.java create mode 100644 app/src/main/java/com/arialyy/simple/widget/SubStateLinearLayout.java create mode 100644 app/src/main/res/layout/layout_child_state.xml 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 c19ce78d..7da4a5ca 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 @@ -63,7 +63,9 @@ class StartCmd extends AbsNormalCmd { if (mod.equals(QueueMod.NOW.getTag())) { mQueue.startTask(task); } else if (mod.equals(QueueMod.WAIT.getTag())) { - if (mQueue.getCurrentExePoolNum() < maxTaskNum || task.getState() == IEntity.STATE_STOP) { + if (mQueue.getCurrentExePoolNum() < maxTaskNum + || task.getState() == IEntity.STATE_STOP + || task.getState() == IEntity.STATE_COMPLETE) { mQueue.startTask(task); } } diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/DListener.java b/Aria/src/main/java/com/arialyy/aria/core/download/DListener.java index d415826e..e9e15f70 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/DListener.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/DListener.java @@ -128,6 +128,12 @@ final class DListener> private void saveData(int state, long location) { if (state == IEntity.STATE_CANCEL) { entity.deleteData(); + } else if (state == IEntity.STATE_COMPLETE) { + entity.setState(state); + entity.setComplete(true); + entity.setCompleteTime(System.currentTimeMillis()); + entity.setCurrentProgress(entity.getFileSize()); + entity.update(); } else { entity.setState(state); if (location != -1) { 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 e0fd311a..e77e1c06 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.orm.DbUtil; import com.arialyy.aria.util.CheckUtil; import com.arialyy.aria.util.CommonUtil; import java.io.File; @@ -131,6 +132,7 @@ public class DownloadGroupTarget throw new NullPointerException("任务组文件夹保存路径不能为null"); } + isSetDirPathed = true; if (mEntity.getDirPath().equals(groupDirPath)) return this; File file = new File(groupDirPath); @@ -148,12 +150,11 @@ public class DownloadGroupTarget mEntity.setSubTasks(createSubTask()); } mEntity.update(); - isSetDirPathed = true; return this; } /** - * 改变任务组文件夹路径 + * 改变任务组文件夹路径,修改文件夹路径会将子任务所有路径更换 * * @param newDirPath 新的文件夹路径 */ @@ -161,8 +162,17 @@ public class DownloadGroupTarget List subTask = mEntity.getSubTask(); if (subTask != null && !subTask.isEmpty()) { for (DownloadEntity entity : subTask) { - File file = new File(entity.getDownloadPath()); - file.renameTo(new File(newDirPath, entity.getFileName())); + String oldPath = entity.getDownloadPath(); + String newPath = newDirPath + "/" + entity.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 + "'"); } } else { mEntity.setSubTasks(createSubTask()); @@ -170,9 +180,9 @@ public class DownloadGroupTarget } /** - * 设置子任务文件名,该方法如果在{@link #setDownloadDirPath(String)}之前调用,则不生效 + * 设置子任务文件名,该方法必须在{@link #setDownloadDirPath(String)}之后调用,否则不生效 */ - public DownloadGroupTarget setSubtaskFileName(List subTaskFileName) { + public DownloadGroupTarget setSubTaskFileName(List subTaskFileName) { if (subTaskFileName == null || subTaskFileName.isEmpty()) return this; mSubTaskFileName.addAll(subTaskFileName); if (mUrls.size() != subTaskFileName.size()) { @@ -182,13 +192,33 @@ public class DownloadGroupTarget List entities = mEntity.getSubTask(); int i = 0; for (DownloadEntity entity : entities) { - entity.setFileName(mSubTaskFileName.get(i)); - entity.update(); + String newName = mSubTaskFileName.get(i); + updateSubFileName(entity, newName); + i++; } } return this; } + /** + * 更新子任务文件名 + */ + private void updateSubFileName(DownloadEntity entity, String newName) { + if (!newName.equals(entity.getFileName())) { + String oldPath = mEntity.getDirPath() + "/" + entity.getFileName(); + String newPath = mEntity.getDirPath() + "/" + newName; + File file = new File(oldPath); + if (file.exists()) { + file.renameTo(new File(newPath)); + } + DbEntity.exeSql( + "UPDATE DownloadTaskEntity SET key='" + newPath + "' WHERE key='" + oldPath + "'"); + entity.setDownloadPath(newPath); + entity.setFileName(newName); + entity.update(); + } + } + /** * 创建子任务 */ 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 2a28c567..0fc94b31 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 @@ -180,7 +180,8 @@ public class DownloadReceiver extends AbsReceiver { * 获取普通下载任务列表 */ @Override public List getSimpleTaskList() { - return DownloadEntity.findDatas(DownloadEntity.class, "isGroupChild=?", "false"); + return DownloadEntity.findDatas(DownloadEntity.class, "isGroupChild=? and downloadPath!=''", + "false"); } /** 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 403d2b4c..6c3edd69 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 @@ -105,6 +105,7 @@ public class DownloadGroupUtil implements IDownloadUtil { if (entity.isComplete() && file.exists()) { mTotalSize += entity.getFileSize(); mCompleteNum++; + mInitNum++; } else { mExeMap.put(entity.getDownloadUrl(), createChildDownloadTask(entity)); } @@ -376,7 +377,12 @@ public class DownloadGroupUtil implements IDownloadUtil { private void saveData(int state, long location) { entity.setState(state); entity.setComplete(state == IEntity.STATE_COMPLETE); - entity.setCurrentProgress(location); + if (entity.isComplete()) { + entity.setCompleteTime(System.currentTimeMillis()); + entity.setCurrentProgress(entity.getFileSize()); + } else { + entity.setCurrentProgress(location); + } entity.update(); } } diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/Downloader.java b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/Downloader.java index 71789239..676ec5e0 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/Downloader.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/Downloader.java @@ -93,11 +93,13 @@ class Downloader implements Runnable, IDownloadUtil { try { if (!mTaskEntity.isSupportBP) { mThreadNum = 1; + mConstance.THREAD_NUM = mThreadNum; handleNoSupportBreakpointDownload(); } else { mThreadNum = isNewTask ? (mEntity.getFileSize() <= SUB_LEN ? 1 : AriaManager.getInstance(mContext).getDownloadConfig().getThreadNum()) : mRealThreadNum; + mConstance.THREAD_NUM = mThreadNum; mFixedThreadPool = Executors.newFixedThreadPool(mThreadNum); handleBreakpoint(); } @@ -298,7 +300,6 @@ class Downloader implements Runnable, IDownloadUtil { entity.CONFIG_FILE_PATH = mConfigFile.getPath(); entity.IS_SUPPORT_BREAK_POINT = mTaskEntity.isSupportBP; entity.DOWNLOAD_TASK_ENTITY = mTaskEntity; - mConstance.THREAD_NUM = mThreadNum; SingleThreadTask task = new SingleThreadTask(mConstance, mListener, entity); mTask.put(i, task); } @@ -390,7 +391,6 @@ class Downloader implements Runnable, IDownloadUtil { entity.CONFIG_FILE_PATH = mConfigFile.getPath(); entity.IS_SUPPORT_BREAK_POINT = mTaskEntity.isSupportBP; entity.DOWNLOAD_TASK_ENTITY = mTaskEntity; - mConstance.THREAD_NUM = mThreadNum; SingleThreadTask task = new SingleThreadTask(mConstance, mListener, entity); mTask.put(0, task); mFixedThreadPool.execute(task); diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/StateConstance.java b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/StateConstance.java index fa082bd9..323f65ff 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/StateConstance.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/StateConstance.java @@ -45,10 +45,6 @@ final class StateConstance { FAIL_NUM = 0; } - void setThreadNum(int threadNum) { - THREAD_NUM = threadNum; - } - /** * 所有子线程是否都已经停止下载 */ diff --git a/Aria/src/main/java/com/arialyy/aria/core/inf/AbsNormalTask.java b/Aria/src/main/java/com/arialyy/aria/core/inf/AbsNormalTask.java index c86dc3c2..a89f5e31 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/inf/AbsNormalTask.java +++ b/Aria/src/main/java/com/arialyy/aria/core/inf/AbsNormalTask.java @@ -20,8 +20,6 @@ package com.arialyy.aria.core.inf; */ public abstract class AbsNormalTask extends AbsTask { - private boolean isHeighestTask = false; - /** * 暂停任务,并让任务处于等待状态 */ @@ -42,7 +40,5 @@ public abstract class AbsNormalTask extends AbsTask implements ITask private String mTargetName; protected Context mContext; + protected boolean isHeighestTask = false; + + /** + * 任务是否完成 + * + * @return {@code true} 已经完成,{@code false} 未完成 + */ + public boolean isComplete() { + return mEntity.isComplete(); + } + /** * 获取当前下载进度 */ @@ -154,4 +165,8 @@ public abstract class AbsTask implements ITask @Override public void setTargetName(String targetName) { this.mTargetName = targetName; } + + public boolean isHighestPriorityTask() { + return isHeighestTask; + } } 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 097bbafd..8dab1afb 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 @@ -35,9 +35,18 @@ import java.util.Set; abstract class AbsTaskQueue implements ITaskQueue { private final String TAG = "AbsTaskQueue"; - BaseCachePool mCachePool = new BaseCachePool<>(); + BaseCachePool mCachePool; BaseExecutePool mExecutePool; + AbsTaskQueue() { + mCachePool = setCachePool(); + mExecutePool = setExecutePool(); + } + + abstract BaseCachePool setCachePool(); + + abstract BaseExecutePool setExecutePool(); + @Override public boolean taskIsRunning(String key) { return mExecutePool.getTask(key) != null; } diff --git a/Aria/src/main/java/com/arialyy/aria/core/queue/DownloadGroupTaskQueue.java b/Aria/src/main/java/com/arialyy/aria/core/queue/DownloadGroupTaskQueue.java index dbf5f665..1cbd63f1 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/queue/DownloadGroupTaskQueue.java +++ b/Aria/src/main/java/com/arialyy/aria/core/queue/DownloadGroupTaskQueue.java @@ -22,7 +22,9 @@ import com.arialyy.aria.core.AriaManager; import com.arialyy.aria.core.download.DownloadGroupEntity; import com.arialyy.aria.core.download.DownloadGroupTask; import com.arialyy.aria.core.download.DownloadGroupTaskEntity; +import com.arialyy.aria.core.queue.pool.BaseCachePool; import com.arialyy.aria.core.queue.pool.BaseExecutePool; +import com.arialyy.aria.core.queue.pool.DownloadSharePool; import com.arialyy.aria.core.scheduler.DQueueMapping; import com.arialyy.aria.core.scheduler.DownloadGroupSchedulers; @@ -46,7 +48,14 @@ public class DownloadGroupTaskQueue } private DownloadGroupTaskQueue() { - mExecutePool = new BaseExecutePool<>(true); + } + + @Override BaseCachePool setCachePool() { + return DownloadSharePool.getInstance().cachePool; + } + + @Override BaseExecutePool setExecutePool() { + return DownloadSharePool.getInstance().executePool; } @Override public DownloadGroupTask createTask(String targetName, DownloadGroupTaskEntity entity) { diff --git a/Aria/src/main/java/com/arialyy/aria/core/queue/DownloadTaskQueue.java b/Aria/src/main/java/com/arialyy/aria/core/queue/DownloadTaskQueue.java index 6f83e58f..24d4344c 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/queue/DownloadTaskQueue.java +++ b/Aria/src/main/java/com/arialyy/aria/core/queue/DownloadTaskQueue.java @@ -22,7 +22,9 @@ import com.arialyy.aria.core.AriaManager; import com.arialyy.aria.core.download.DownloadEntity; import com.arialyy.aria.core.download.DownloadTask; import com.arialyy.aria.core.download.DownloadTaskEntity; -import com.arialyy.aria.core.queue.pool.DownloadExecutePool; +import com.arialyy.aria.core.queue.pool.BaseCachePool; +import com.arialyy.aria.core.queue.pool.BaseExecutePool; +import com.arialyy.aria.core.queue.pool.DownloadSharePool; import com.arialyy.aria.core.scheduler.DQueueMapping; import com.arialyy.aria.core.scheduler.DownloadSchedulers; import java.util.LinkedHashSet; @@ -48,7 +50,14 @@ public class DownloadTaskQueue } private DownloadTaskQueue() { - mExecutePool = new DownloadExecutePool(true); + } + + @Override BaseCachePool setCachePool() { + return DownloadSharePool.getInstance().cachePool; + } + + @Override BaseExecutePool setExecutePool() { + return DownloadSharePool.getInstance().executePool; } @Override public String getKey(DownloadEntity entity) { diff --git a/Aria/src/main/java/com/arialyy/aria/core/queue/UploadTaskQueue.java b/Aria/src/main/java/com/arialyy/aria/core/queue/UploadTaskQueue.java index b58013f9..46e3f9b4 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/queue/UploadTaskQueue.java +++ b/Aria/src/main/java/com/arialyy/aria/core/queue/UploadTaskQueue.java @@ -19,7 +19,9 @@ package com.arialyy.aria.core.queue; import android.text.TextUtils; import android.util.Log; import com.arialyy.aria.core.AriaManager; +import com.arialyy.aria.core.queue.pool.BaseCachePool; import com.arialyy.aria.core.queue.pool.BaseExecutePool; +import com.arialyy.aria.core.queue.pool.UploadSharePool; import com.arialyy.aria.core.scheduler.UploadSchedulers; import com.arialyy.aria.core.upload.UploadEntity; import com.arialyy.aria.core.upload.UploadTask; @@ -43,7 +45,14 @@ public class UploadTaskQueue extends AbsTaskQueue(false); + } + + @Override BaseCachePool setCachePool() { + return UploadSharePool.getInstance().cachePool; + } + + @Override BaseExecutePool setExecutePool() { + return UploadSharePool.getInstance().executePool; } @Override public String getKey(UploadEntity entity) { diff --git a/Aria/src/main/java/com/arialyy/aria/core/queue/pool/BaseCachePool.java b/Aria/src/main/java/com/arialyy/aria/core/queue/pool/BaseCachePool.java index 14648f60..1dd9cd7c 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/queue/pool/BaseCachePool.java +++ b/Aria/src/main/java/com/arialyy/aria/core/queue/pool/BaseCachePool.java @@ -19,6 +19,7 @@ package com.arialyy.aria.core.queue.pool; import android.text.TextUtils; import android.util.Log; import com.arialyy.aria.core.AriaManager; +import com.arialyy.aria.core.inf.AbsTask; import com.arialyy.aria.core.inf.ITask; import com.arialyy.aria.util.CommonUtil; import java.util.HashMap; @@ -32,7 +33,7 @@ import java.util.concurrent.TimeUnit; * Created by lyy on 2016/8/14. * 任务缓存池,所有下载任务最先缓存在这个池中 */ -public class BaseCachePool implements IPool { +public class BaseCachePool implements IPool { private static final String TAG = "BaseCachePool"; private static final int MAX_NUM = Integer.MAX_VALUE; //最大下载任务数 private static final long TIME_OUT = 1000; diff --git a/Aria/src/main/java/com/arialyy/aria/core/queue/pool/BaseExecutePool.java b/Aria/src/main/java/com/arialyy/aria/core/queue/pool/BaseExecutePool.java index 9c3136dd..fda42905 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/queue/pool/BaseExecutePool.java +++ b/Aria/src/main/java/com/arialyy/aria/core/queue/pool/BaseExecutePool.java @@ -19,6 +19,7 @@ package com.arialyy.aria.core.queue.pool; import android.text.TextUtils; import android.util.Log; import com.arialyy.aria.core.AriaManager; +import com.arialyy.aria.core.inf.AbsTask; import com.arialyy.aria.core.inf.ITask; import com.arialyy.aria.core.scheduler.DQueueMapping; import com.arialyy.aria.util.CommonUtil; @@ -31,23 +32,28 @@ import java.util.concurrent.TimeUnit; * Created by lyy on 2016/8/15. * 任务执行池,所有当前下载任务都该任务池中,默认下载大小为2 */ -public class BaseExecutePool implements IPool { +public class BaseExecutePool implements IPool { private final String TAG = "BaseExecutePool"; final long TIME_OUT = 1000; ArrayBlockingQueue mExecuteQueue; Map mExecuteMap; - protected int mSize; + int mSize; - public BaseExecutePool(boolean isDownload) { - if (isDownload) { - mSize = AriaManager.getInstance(AriaManager.APP).getDownloadConfig().getMaxTaskNum(); - } else { - mSize = AriaManager.getInstance(AriaManager.APP).getUploadConfig().getMaxTaskNum(); - } + BaseExecutePool() { + mSize = getMaxSize(); mExecuteQueue = new ArrayBlockingQueue<>(mSize); mExecuteMap = new HashMap<>(); } + /** + * 获取最大任务数配置 + * + * @return {@link AriaManager#getDownloadConfig()} {@link AriaManager#getUploadConfig()},如果不设置,默认返回2 + */ + protected int getMaxSize() { + return 2; + } + /** * 获取所有正在执行的任务 */ diff --git a/Aria/src/main/java/com/arialyy/aria/core/queue/pool/DownloadExecutePool.java b/Aria/src/main/java/com/arialyy/aria/core/queue/pool/DownloadExecutePool.java index 0e8e53b0..6123a62b 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/queue/pool/DownloadExecutePool.java +++ b/Aria/src/main/java/com/arialyy/aria/core/queue/pool/DownloadExecutePool.java @@ -17,7 +17,7 @@ package com.arialyy.aria.core.queue.pool; import android.util.Log; import com.arialyy.aria.core.AriaManager; -import com.arialyy.aria.core.download.DownloadTask; +import com.arialyy.aria.core.inf.AbsTask; import com.arialyy.aria.util.CommonUtil; import java.util.Set; import java.util.concurrent.TimeUnit; @@ -26,14 +26,14 @@ import java.util.concurrent.TimeUnit; * Created by AriaL on 2017/6/29. * 单个下载任务的执行池 */ -public class DownloadExecutePool extends BaseExecutePool { +public class DownloadExecutePool extends BaseExecutePool { private final String TAG = "DownloadExecutePool"; - public DownloadExecutePool(boolean isDownload) { - super(isDownload); + @Override protected int getMaxSize() { + return AriaManager.getInstance(AriaManager.APP).getDownloadConfig().getMaxTaskNum(); } - @Override public boolean putTask(DownloadTask task) { + @Override public boolean putTask(TASK task) { synchronized (AriaManager.LOCK) { if (task == null) { Log.e(TAG, "任务不能为空!!"); @@ -62,7 +62,7 @@ public class DownloadExecutePool extends BaseExecutePool { @Override boolean pollFirstTask() { try { - DownloadTask oldTask = mExecuteQueue.poll(TIME_OUT, TimeUnit.MICROSECONDS); + TASK oldTask = mExecuteQueue.poll(TIME_OUT, TimeUnit.MICROSECONDS); if (oldTask == null) { Log.e(TAG, "移除任务失败"); return false; diff --git a/Aria/src/main/java/com/arialyy/aria/core/queue/pool/DownloadSharePool.java b/Aria/src/main/java/com/arialyy/aria/core/queue/pool/DownloadSharePool.java new file mode 100644 index 00000000..cd7625bd --- /dev/null +++ b/Aria/src/main/java/com/arialyy/aria/core/queue/pool/DownloadSharePool.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.aria.core.queue.pool; + +import com.arialyy.aria.core.AriaManager; + +/** + * Created by Aria.Lao on 2017/7/17. + * 下载任务池,该池子为简单任务和任务组共用 + */ +public class DownloadSharePool { + private static volatile DownloadSharePool INSTANCE; + + public DownloadExecutePool executePool; + public BaseCachePool cachePool; + + private DownloadSharePool() { + executePool = new DownloadExecutePool<>(); + cachePool = new BaseCachePool<>(); + } + + public static DownloadSharePool getInstance() { + if (INSTANCE == null) { + synchronized (AriaManager.LOCK) { + INSTANCE = new DownloadSharePool(); + } + } + return INSTANCE; + } +} diff --git a/Aria/src/main/java/com/arialyy/aria/core/queue/pool/IPool.java b/Aria/src/main/java/com/arialyy/aria/core/queue/pool/IPool.java index 7e143b9b..ae6ab6c7 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/queue/pool/IPool.java +++ b/Aria/src/main/java/com/arialyy/aria/core/queue/pool/IPool.java @@ -16,13 +16,13 @@ package com.arialyy.aria.core.queue.pool; -import com.arialyy.aria.core.inf.ITask; +import com.arialyy.aria.core.inf.AbsTask; /** * Created by lyy on 2016/8/14. * 任务池 */ -interface IPool { +interface IPool { /** * 将下载任务添加到任务池中 */ diff --git a/Aria/src/main/java/com/arialyy/aria/core/queue/pool/UploadExecutePool.java b/Aria/src/main/java/com/arialyy/aria/core/queue/pool/UploadExecutePool.java new file mode 100644 index 00000000..b5437f2c --- /dev/null +++ b/Aria/src/main/java/com/arialyy/aria/core/queue/pool/UploadExecutePool.java @@ -0,0 +1,28 @@ +/* + * 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.queue.pool; + +import com.arialyy.aria.core.AriaManager; +import com.arialyy.aria.core.inf.AbsTask; + +/** + * Created by Aria.Lao on 2017/7/17. + */ +public class UploadExecutePool extends BaseExecutePool { + @Override protected int getMaxSize() { + return AriaManager.getInstance(AriaManager.APP).getUploadConfig().getMaxTaskNum(); + } +} diff --git a/Aria/src/main/java/com/arialyy/aria/core/queue/pool/UploadSharePool.java b/Aria/src/main/java/com/arialyy/aria/core/queue/pool/UploadSharePool.java new file mode 100644 index 00000000..f75af152 --- /dev/null +++ b/Aria/src/main/java/com/arialyy/aria/core/queue/pool/UploadSharePool.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.aria.core.queue.pool; + +import com.arialyy.aria.core.AriaManager; + +/** + * Created by Aria.Lao on 2017/7/17. + * 下载任务池,该池子为简单任务和任务组共用 + */ +public class UploadSharePool { + private static volatile UploadSharePool INSTANCE; + + public UploadExecutePool executePool; + public BaseCachePool cachePool; + + private UploadSharePool() { + executePool = new UploadExecutePool(); + cachePool = new BaseCachePool<>(); + } + + public static UploadSharePool getInstance() { + if (INSTANCE == null) { + synchronized (AriaManager.LOCK) { + INSTANCE = new UploadSharePool(); + } + } + return INSTANCE; + } +} 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 51edc1a4..f872e5d2 100644 --- a/Aria/src/main/java/com/arialyy/aria/orm/DbEntity.java +++ b/Aria/src/main/java/com/arialyy/aria/orm/DbEntity.java @@ -36,6 +36,13 @@ public class DbEntity { } + /** + * 直接执行sql语句 + */ + public static void exeSql(String sql) { + DbUtil.getInstance().exeSql(sql); + } + /** * 查询所有数据 * 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 eeaf96f7..32cf7b03 100644 --- a/Aria/src/main/java/com/arialyy/aria/orm/DbUtil.java +++ b/Aria/src/main/java/com/arialyy/aria/orm/DbUtil.java @@ -64,6 +64,13 @@ public class DbUtil { return INSTANCE; } + /** + * 执行sql语句 + */ + void exeSql(String sql) { + mDb.execSQL(sql); + } + /** * 删除某条数据 */ 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 e51097e4..b4d6db90 100644 --- a/Aria/src/main/java/com/arialyy/aria/util/CommonUtil.java +++ b/Aria/src/main/java/com/arialyy/aria/util/CommonUtil.java @@ -541,9 +541,11 @@ public class CommonUtil { } /** - * 创建文件 当文件不存在的时候就创建一个文件,否则直接返回文件 + * 创建文件 + * 当文件不存在的时候就创建一个文件。 + * 如果文件存在,先删除原文件,然后重新创建一个新文件 */ - public static File createFile(String path) { + public static void createFile(String path) { File file = new File(path); if (!file.getParentFile().exists()) { Log.d(TAG, "目标文件所在路径不存在,准备创建……"); @@ -552,19 +554,18 @@ public class CommonUtil { } } // 创建目标文件 + if (file.exists()) { + final File to = new File(file.getAbsolutePath() + System.currentTimeMillis()); + file.renameTo(to); + to.delete(); + } try { - if (!file.exists()) { - if (file.createNewFile()) { - Log.d(TAG, "创建文件成功:" + file.getAbsolutePath()); - } - return file; - } else { - return file; + if (file.createNewFile()) { + Log.d(TAG, "创建文件成功:" + file.getAbsolutePath()); } } catch (IOException e) { e.printStackTrace(); } - return null; } /** 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 f3f4d103..e7eecd4a 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 @@ -60,6 +60,14 @@ public abstract class AbsRVAdapter bindData(holder, position, mData.get(position)); } + @Override public void onBindViewHolder(Holder holder, int position, List payloads) { + if (payloads == null || payloads.isEmpty()) { + bindData(holder, position, mData.get(position)); + } else { + bindData(holder, position, mData.get(position), payloads); + } + } + public Context getContext() { return mContext; } @@ -74,4 +82,8 @@ public abstract class AbsRVAdapter protected abstract int setLayoutId(int type); protected abstract void bindData(Holder holder, int position, T item); + + protected void bindData(Holder holder, int position, T item, List payloads) { + + } } \ No newline at end of file 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 2a8ae080..1839add9 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 @@ -69,7 +69,9 @@ public class DownloadGroupActivity extends BaseActivity getUrls() { List urls = new ArrayList<>(); - String[] str = getContext().getResources().getStringArray(R.array.download_url); - //String[] str = getContext().getResources().getStringArray(R.array.group_urls); + String[] str = getContext().getResources().getStringArray(R.array.group_urls); Collections.addAll(urls, str); return urls; } + List getSubName(){ + List names = new ArrayList<>(); + String[] str = getContext().getResources().getStringArray(R.array.group_names); + Collections.addAll(names, str); + return names; + } + //NormalList convertPath(NormalList urls){ // NormalList paths = new ArrayList<>(); // 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 2e293105..38c507d5 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 @@ -21,6 +21,7 @@ import android.content.Context; import android.content.res.Resources; import android.view.View; import android.widget.Button; +import android.widget.LinearLayout; import android.widget.TextView; import butterknife.Bind; import com.arialyy.aria.core.Aria; @@ -111,6 +112,9 @@ public class DownloadAdapter extends AbsRVAdapter payloads) { + AbsEntity entity = (AbsEntity) payloads.get(0); + updateSpeed(holder, entity); + } + + /** + * 只更新速度 + */ + private void updateSpeed(SimpleHolder holder, final AbsEntity entity) { + long size = entity.getFileSize(); + long progress = entity.getCurrentProgress(); + int current = size == 0 ? 0 : (int) (progress * 100 / size); + holder.speed.setText(entity.getConvertSpeed()); + holder.fileSize.setText(covertCurrentSize(progress) + "/" + CommonUtil.formatFileSize(size)); + holder.progress.setProgress(current); + } + @SuppressLint("SetTextI18n") private void handleProgress(SimpleHolder holder, final AbsEntity entity) { String str = ""; @@ -189,6 +211,11 @@ public class DownloadAdapter extends AbsRVAdapter mSubData = new LinkedList<>(); + + public SubStateLinearLayout(Context context) { + super(context); + } + + public SubStateLinearLayout(Context context, @Nullable AttributeSet attrs) { + super(context, attrs); + } + + public SubStateLinearLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { + super(context, attrs, defStyleAttr); + } + + public void addData(List datas) { + removeAllViews(); + mSubData.clear(); + mSubData.addAll(datas); + createShowView(); + int i = 1; + for (DownloadEntity entity : datas) { + TextView view = createView(entity); + addView(view, i); + i++; + } + } + + public void update(DownloadEntity entity) { + int position = mSubData.indexOf(entity) + 1; + if (position != 0) { + ((TextView) getChildAt(position)).setText(entity.getFileName() + ": " + getPercent(entity)); + } + } + + private TextView createView(DownloadEntity entity) { + TextView view = + (TextView) LayoutInflater.from(getContext()).inflate(R.layout.layout_child_state, null); + view.setText(entity.getFileName() + ": " + getPercent(entity)); + return view; + } + + private void createShowView() { + + TextView view = + (TextView) LayoutInflater.from(getContext()).inflate(R.layout.layout_child_state, null); + view.setText("点击显示子任务"); + view.setOnClickListener(new OnClickListener() { + @Override public void onClick(View v) { + if (getVisibility() == GONE) { + showChild(true); + ((TextView) v).setText("点击隐藏子任务"); + } else { + showChild(false); + ((TextView) v).setText("点击显示子任务"); + } + } + }); + addView(view, 0); + } + + private void showChild(boolean show) { + for (int i = 1, count = getChildCount(); i < count; i++) { + getChildAt(i).setVisibility(show ? VISIBLE : GONE); + } + } + + private int getPercent(DownloadEntity entity) { + long size = entity.getFileSize(); + long progress = entity.getCurrentProgress(); + int current = size == 0 ? 0 : (int) (progress * 100 / size); + return current; + } +} diff --git a/app/src/main/res/layout/item_group_download.xml b/app/src/main/res/layout/item_group_download.xml index 6049a053..51223360 100644 --- a/app/src/main/res/layout/item_group_download.xml +++ b/app/src/main/res/layout/item_group_download.xml @@ -4,15 +4,23 @@ android:layout_height="wrap_content" > - - + \ No newline at end of file diff --git a/app/src/main/res/layout/layout_child_state.xml b/app/src/main/res/layout/layout_child_state.xml new file mode 100644 index 00000000..6c0c49fc --- /dev/null +++ b/app/src/main/res/layout/layout_child_state.xml @@ -0,0 +1,8 @@ + + diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index ecb70eb6..bec3be14 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -47,17 +47,15 @@ - http://img.sc115.com/uploads/allimg/110420/20110420225600154.jpg - http://img05.tooopen.com/images/20160121/tooopen_sy_155168162826.jpg - http://img03.tooopen.com/images/20130811/tooopen_15265353.jpg - - - - - - - - + https://res5.d.cn/5a6a3384c1b2be1a65d84b914e6a6fef697637578b6db2eb1056d50b09cf1dcf289d4045df7ef95746e498e3d6a848ab84c89b77aa60194e2c48e5a7cb748265.apk + https://res5.d.cn/5a6a3384c1b2be1a52034c72752e8475414630ebc69318b84ef584115ebf5eaaab945ae07b7fe3596afc72a7940ff328d4a9553f6ae92d6c09ba4bfb533137f6.apk + https://res5.d.cn/5a6a3384c1b2be1a426f06bfc69034d69c44ae1a01da180cab8e59bd1a5e1a784bac46ba0c64579d14f0e80a4ce4f068af89b0369a393456f4f449a8829cad5c.apk + + + + 王者荣耀.apk + 战斗吧剑灵.apk + 天魔幻想.apk