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 a1c169e7..e52c90c8 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/AriaManager.java +++ b/Aria/src/main/java/com/arialyy/aria/core/AriaManager.java @@ -69,14 +69,7 @@ import org.xml.sax.SAXException; public static final String DOWNLOAD_TEMP_DIR = "/Aria/temp/download/"; public static final String UPLOAD_TEMP_DIR = "/Aria/temp/upload/"; - public static final int LOG_LEVEL_VERBOSE = 2; - public static final int LOG_LEVEL_DEBUG = 3; - public static final int LOG_LEVEL_INFO = 4; - public static final int LOG_LEVEL_WARN = 5; - public static final int LOG_LEVEL_ERROR = 6; - public static final int LOG_LEVEL_ASSERT = 7; - public static final int LOG_CLOSE = 8; - public static final int LOG_DEFAULT = LOG_LEVEL_DEBUG; + @SuppressLint("StaticFieldLeak") private static volatile AriaManager INSTANCE = null; private Map mReceivers = new ConcurrentHashMap<>(); @@ -108,7 +101,7 @@ import org.xml.sax.SAXException; /** * 设置Aria 日志级别 * - * @param level {@link #LOG_LEVEL_VERBOSE} + * @param level {@link ALog#LOG_LEVEL_VERBOSE} */ public void setLogLevel(int level) { ALog.LOG_LEVEL = level; diff --git a/Aria/src/main/java/com/arialyy/aria/core/command/normal/AbsNormalCmd.java b/Aria/src/main/java/com/arialyy/aria/core/command/normal/AbsNormalCmd.java index 31264f7a..b6819314 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/command/normal/AbsNormalCmd.java +++ b/Aria/src/main/java/com/arialyy/aria/core/command/normal/AbsNormalCmd.java @@ -23,6 +23,7 @@ import com.arialyy.aria.core.download.DownloadTaskEntity; import com.arialyy.aria.core.inf.AbsEntity; import com.arialyy.aria.core.inf.AbsTask; import com.arialyy.aria.core.inf.AbsTaskEntity; +import com.arialyy.aria.core.manager.TEManager; import com.arialyy.aria.core.queue.DownloadGroupTaskQueue; import com.arialyy.aria.core.queue.DownloadTaskQueue; import com.arialyy.aria.core.queue.UploadTaskQueue; @@ -161,6 +162,7 @@ public abstract class AbsNormalCmd extends AbsCmd { * @return 创建的任务 */ AbsTask createTask(AbsTaskEntity taskEntity) { + TEManager.getInstance().addTEntity(taskEntity); return mQueue.createTask(mTargetName, taskEntity); } } \ No newline at end of file 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 ec3560c0..0ffe8715 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 @@ -1,5 +1,6 @@ package com.arialyy.aria.core.command.normal; +import android.util.Log; import com.arialyy.aria.core.AriaManager; import com.arialyy.aria.core.download.DownloadGroupTaskEntity; import com.arialyy.aria.core.download.DownloadTaskEntity; 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 8a47f681..32f6b54d 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 @@ -24,6 +24,7 @@ import com.arialyy.aria.core.download.DownloadTaskEntity; import com.arialyy.aria.core.inf.AbsTask; import com.arialyy.aria.core.inf.AbsTaskEntity; import com.arialyy.aria.core.inf.IEntity; +import com.arialyy.aria.core.manager.TEManager; import com.arialyy.aria.core.queue.DownloadGroupTaskQueue; import com.arialyy.aria.core.queue.DownloadTaskQueue; import com.arialyy.aria.core.queue.UploadTaskQueue; diff --git a/Aria/src/main/java/com/arialyy/aria/core/common/AbsFileer.java b/Aria/src/main/java/com/arialyy/aria/core/common/AbsFileer.java index c0687c9a..f22a81f1 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/common/AbsFileer.java +++ b/Aria/src/main/java/com/arialyy/aria/core/common/AbsFileer.java @@ -367,6 +367,7 @@ public abstract class AbsFileer 0) { - ALog.i(TAG, "路径【" + setRemotePath() + "】该下文件列表 ==================================="); + ALog.i(TAG, "路径【" + setRemotePath() + "】下的文件列表 ==================================="); for (FTPFile file : files1) { ALog.d(TAG, file.toString()); } - ALog.i(TAG, "================================= --end-- ==================================="); + ALog.i(TAG, + "================================= --end-- ==================================="); } client.disconnect(); return; @@ -206,7 +207,7 @@ public abstract class AbsFtpInfoThread " - + currentTemp); + + "__停止【停止位置: " + + currentTemp + "】"); writeConfig(false, currentTemp); if (STATE.isStop()) { ALog.i(TAG, "任务【" + mConfig.TEMP_FILE.getName() + "】已停止"); diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/BaseGroupTarget.java b/Aria/src/main/java/com/arialyy/aria/core/download/BaseGroupTarget.java index 9d651fdc..e6ed4274 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/BaseGroupTarget.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/BaseGroupTarget.java @@ -16,10 +16,10 @@ package com.arialyy.aria.core.download; import android.text.TextUtils; -import com.arialyy.aria.core.SubTaskManager; import com.arialyy.aria.core.inf.AbsDownloadTarget; -import com.arialyy.aria.core.inf.AbsTarget; import com.arialyy.aria.core.inf.AbsGroupTaskEntity; +import com.arialyy.aria.core.inf.AbsTarget; +import com.arialyy.aria.core.manager.SubTaskManager; import com.arialyy.aria.orm.DbEntity; import com.arialyy.aria.util.CommonUtil; import java.io.File; @@ -45,20 +45,6 @@ abstract class BaseGroupTarget urls, String targetName) { this.mTargetName = targetName; this.mUrls = urls; - init(CommonUtil.getMd5Code(urls)); - } - - private void init(String key) { - mGroupName = key; - mTaskEntity = DbEntity.findFirst(DownloadGroupTaskEntity.class, "key=?", key); + mGroupName = CommonUtil.getMd5Code(urls); + mTaskEntity = TEManager.getInstance().getTEntity(DownloadGroupTaskEntity.class, mGroupName); if (mTaskEntity == null) { - mTaskEntity = new DownloadGroupTaskEntity(); - mTaskEntity.save(getDownloadGroupEntity()); - } - if (mTaskEntity.entity == null || TextUtils.isEmpty(mTaskEntity.entity.getKey())) { - mTaskEntity.save(getDownloadGroupEntity()); + mTaskEntity = TEManager.getInstance().createGTEntity(DownloadGroupTaskEntity.class, mUrls); } mEntity = mTaskEntity.entity; } 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 b08c803a..b242af47 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 @@ -17,8 +17,9 @@ package com.arialyy.aria.core.download; import android.support.annotation.NonNull; import android.text.TextUtils; +import android.util.Log; import com.arialyy.aria.core.inf.AbsDownloadTarget; -import com.arialyy.aria.core.inf.IEntity; +import com.arialyy.aria.core.manager.TEManager; import com.arialyy.aria.core.queue.DownloadTaskQueue; import com.arialyy.aria.orm.DbEntity; import com.arialyy.aria.util.CommonUtil; @@ -39,7 +40,11 @@ public class DownloadTarget DownloadTarget(DownloadEntity entity, String targetName, boolean refreshInfo) { this.url = entity.getUrl(); mTargetName = targetName; - initTask(entity); + mTaskEntity = TEManager.getInstance().getTEntity(DownloadTaskEntity.class, url); + if (mTaskEntity == null) { + mTaskEntity = TEManager.getInstance().createTEntity(DownloadTaskEntity.class, entity); + } + mEntity = mTaskEntity.entity; mTaskEntity.refreshInfo = refreshInfo; } @@ -50,44 +55,12 @@ public class DownloadTarget DownloadTarget(String url, String targetName, boolean refreshInfo) { this.url = url; mTargetName = targetName; - initTask(getEntity(url)); - mTaskEntity.refreshInfo = refreshInfo; - } - - private void initTask(DownloadEntity entity) { - mTaskEntity = - DbEntity.findFirst(DownloadTaskEntity.class, "key=? and isGroupTask='false' and url=?", - entity.getDownloadPath(), entity.getUrl()); + mTaskEntity = TEManager.getInstance().getTEntity(DownloadTaskEntity.class, url); if (mTaskEntity == null) { - mTaskEntity = new DownloadTaskEntity(); - mTaskEntity.save(entity); - } else if (mTaskEntity.entity == null || TextUtils.isEmpty(mTaskEntity.entity.getUrl())) { - mTaskEntity.save(entity); - } else if (!mTaskEntity.entity.getUrl().equals(entity.getUrl())) { //处理地址切换而保存路径不变 - mTaskEntity.save(entity); + mTaskEntity = TEManager.getInstance().createTEntity(DownloadTaskEntity.class, url); } - mEntity = entity; - } - - /** - * 如果任务存在,但是下载实体不存在,则通过下载地址获取下载实体 - * - * @param downloadUrl 下载地址 - */ - private DownloadEntity getEntity(String downloadUrl) { - DownloadEntity entity = - DownloadEntity.findFirst(DownloadEntity.class, "url=? and isGroupChild='false'", - downloadUrl); - if (entity == null) { - entity = new DownloadEntity(); - entity.setUrl(downloadUrl); - entity.setGroupChild(false); - } - File file = new File(entity.getDownloadPath()); - if (!file.exists()) { - entity.setState(IEntity.STATE_WAIT); - } - return entity; + mEntity = mTaskEntity.entity; + mTaskEntity.refreshInfo = refreshInfo; } /** @@ -128,7 +101,8 @@ public class DownloadTarget throw new IllegalArgumentException("保存路径不能为文件夹,路径需要是完整的文件路径,如:/mnt/sdcard/game.zip"); } if (!downloadPath.equals(mEntity.getDownloadPath())) { - if (!mTaskEntity.refreshInfo && DbEntity.checkDataExist(DownloadEntity.class, "downloadPath=?", downloadPath)) { + if (!mTaskEntity.refreshInfo && DbEntity.checkDataExist(DownloadEntity.class, + "downloadPath=?", downloadPath)) { throw new IllegalArgumentException("保存路径【" + downloadPath + "】已经被其它任务占用,请设置其它保存路径"); } File oldFile = new File(mEntity.getDownloadPath()); diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadTask.java b/Aria/src/main/java/com/arialyy/aria/core/download/DownloadTask.java index b1f838a0..b32d5634 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadTask.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/DownloadTask.java @@ -18,6 +18,7 @@ package com.arialyy.aria.core.download; import android.os.Handler; import android.os.Looper; +import android.util.Log; import com.arialyy.aria.core.AriaManager; import com.arialyy.aria.core.common.IUtil; import com.arialyy.aria.core.download.downloader.SimpleDownloadUtil; 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 206887b9..26c9e4c6 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 @@ -17,9 +17,8 @@ package com.arialyy.aria.core.download; import android.text.TextUtils; import com.arialyy.aria.core.inf.AbsTaskEntity; -import com.arialyy.aria.orm.DbEntity; +import com.arialyy.aria.core.manager.TEManager; import com.arialyy.aria.util.ALog; -import com.arialyy.aria.util.CommonUtil; /** * Created by Aria.Lao on 2017/7/26. @@ -30,22 +29,17 @@ public class FtpDirDownloadTarget private final String TAG = "FtpDirDownloadTarget"; FtpDirDownloadTarget(String url, String targetName) { - init(url); mTargetName = targetName; - mTaskEntity.urlEntity = CommonUtil.getFtpUrlInfo(url); - mTaskEntity.requestType = AbsTaskEntity.FTP_DIR; + init(url); } private void init(String key) { mGroupName = key; - mTaskEntity = DbEntity.findFirst(DownloadGroupTaskEntity.class, "key=?", key); + mTaskEntity = TEManager.getInstance().getTEntity(DownloadGroupTaskEntity.class, key); if (mTaskEntity == null) { - mTaskEntity = new DownloadGroupTaskEntity(); - mTaskEntity.save(getDownloadGroupEntity()); - } - if (mTaskEntity.entity == null) { - mTaskEntity.save(getDownloadGroupEntity()); + mTaskEntity = TEManager.getInstance().createTEntity(DownloadGroupTaskEntity.class, key); } + mTaskEntity.requestType = AbsTaskEntity.FTP_DIR; mEntity = mTaskEntity.entity; } diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/FtpDownloadTarget.java b/Aria/src/main/java/com/arialyy/aria/core/download/FtpDownloadTarget.java index e886a170..86733688 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/FtpDownloadTarget.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/FtpDownloadTarget.java @@ -42,6 +42,37 @@ public class FtpDownloadTarget extends DownloadTarget { mTaskEntity.requestType = AbsTaskEntity.FTP; } + /** + * 设置文件保存文件夹路径 + * 关于文件名: + * 1、如果保存路径是该文件的保存路径,如:/mnt/sdcard/file.zip,则使用路径中的文件名file.zip + * 2、如果保存路径是文件夹路径,如:/mnt/sdcard/,则使用FTP服务器该文件的文件名 + * + * @param downloadPath 路径必须为文件路径,不能为文件夹路径 + */ + @Override public FtpDownloadTarget setDownloadPath(@NonNull String downloadPath) { + if (TextUtils.isEmpty(downloadPath)) { + throw new IllegalArgumentException("文件保持路径不能为null"); + } + File file = new File(downloadPath); + if (file.isDirectory()) { + downloadPath += mEntity.getFileName(); + } + if (!downloadPath.equals(mEntity.getDownloadPath())) { + File oldFile = new File(mEntity.getDownloadPath()); + File newFile = new File(downloadPath); + if (TextUtils.isEmpty(mEntity.getDownloadPath()) || oldFile.renameTo(newFile)) { + mEntity.setDownloadPath(downloadPath); + mEntity.setFileName(newFile.getName()); + mTaskEntity.key = downloadPath; + mEntity.update(); + mTaskEntity.update(); + CommonUtil.renameDownloadConfig(oldFile.getName(), newFile.getName()); + } + } + return this; + } + /** * 设置字符编码 */ diff --git a/Aria/src/main/java/com/arialyy/aria/core/inf/AbsUploadTarget.java b/Aria/src/main/java/com/arialyy/aria/core/inf/AbsUploadTarget.java index e3aec7ba..74150386 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/inf/AbsUploadTarget.java +++ b/Aria/src/main/java/com/arialyy/aria/core/inf/AbsUploadTarget.java @@ -45,27 +45,6 @@ public abstract class AbsUploadTarget { - private static final String TAG = "DTaskEntityFactory"; - private static volatile DGTaskEntityFactory INSTANCE = null; +class DGTEntityFactory implements ITEntityFactory, + IGTEntityFactory { + private static final String TAG = "DTEntityFactory"; + private static volatile DGTEntityFactory INSTANCE = null; - private DGTaskEntityFactory() { + private DGTEntityFactory() { } - public static DGTaskEntityFactory getInstance() { + public static DGTEntityFactory getInstance() { if (INSTANCE == null) { - synchronized (DGTaskEntityFactory.class) { - INSTANCE = new DGTaskEntityFactory(); + synchronized (DGTEntityFactory.class) { + INSTANCE = new DGTEntityFactory(); } } return INSTANCE; } + /** + * 通过下载实体创建任务实体 + */ @Override public DownloadGroupTaskEntity create(DownloadGroupEntity entity) { DownloadGroupTaskEntity dgTaskEntity = DbEntity.findFirst(DownloadGroupTaskEntity.class, "key=?", entity.getGroupName()); @@ -53,4 +58,41 @@ class DGTaskEntityFactory } return dgTaskEntity; } + + /** + * 对于任务组,不能使用这个,可用于FTP文件夹下载 + * + * @deprecated {@link #create(String, List)} + */ + @Override @Deprecated public DownloadGroupTaskEntity create(String key) { + DownloadGroupTaskEntity dgTaskEntity = + DbEntity.findFirst(DownloadGroupTaskEntity.class, "key=?", key); + if (dgTaskEntity == null) { + dgTaskEntity = new DownloadGroupTaskEntity(); + dgTaskEntity.save(getDownloadGroupEntity(key, null)); + } + if (dgTaskEntity.entity == null || TextUtils.isEmpty(dgTaskEntity.entity.getKey())) { + dgTaskEntity.save(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) { + DownloadGroupEntity entity = + DbEntity.findFirst(DownloadGroupEntity.class, "groupName=?", groupName); + if (entity == null) { + entity = new DownloadGroupEntity(); + entity.setGroupName(groupName); + entity.setUrls(urls); + } + return entity; + } } diff --git a/Aria/src/main/java/com/arialyy/aria/core/manager/DTEntityFactory.java b/Aria/src/main/java/com/arialyy/aria/core/manager/DTEntityFactory.java index 01fd48a4..437b4171 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/manager/DTEntityFactory.java +++ b/Aria/src/main/java/com/arialyy/aria/core/manager/DTEntityFactory.java @@ -18,28 +18,33 @@ 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.inf.IEntity; import com.arialyy.aria.orm.DbEntity; +import java.io.File; /** * Created by Aria.Lao on 2017/11/1. * 任务实体工厂 */ -class DTaskEntityFactory implements ITaskEntityFactory { - private static final String TAG = "DTaskEntityFactory"; - private static volatile DTaskEntityFactory INSTANCE = null; +class DTEntityFactory implements ITEntityFactory { + private static final String TAG = "DTEntityFactory"; + private static volatile DTEntityFactory INSTANCE = null; - private DTaskEntityFactory() { + private DTEntityFactory() { } - public static DTaskEntityFactory getInstance() { + public static DTEntityFactory getInstance() { if (INSTANCE == null) { - synchronized (DTaskEntityFactory.class) { - INSTANCE = new DTaskEntityFactory(); + synchronized (DTEntityFactory.class) { + INSTANCE = new DTEntityFactory(); } } return INSTANCE; } + /** + * 通过下载实体创建任务实体 + */ @Override public DownloadTaskEntity create(DownloadEntity entity) { DownloadTaskEntity taskEntity = DbEntity.findFirst(DownloadTaskEntity.class, "key=? and isGroupTask='false' and url=?", @@ -54,4 +59,32 @@ class DTaskEntityFactory implements ITaskEntityFactory> { - /** - * 通过信息实体创建任务实体 - */ - TASK_ENTITY create(ENTITY entity); +interface IGTEntityFactory> { /** - * 通过key创建任务,只适应于单任务 + * 通过key创建任务 */ - TASK_ENTITY create(String key); + TASK_ENTITY create(String groupName, List urls); } diff --git a/Aria/src/main/java/com/arialyy/aria/core/manager/ITEntityFactory.java b/Aria/src/main/java/com/arialyy/aria/core/manager/ITEntityFactory.java index e0532651..d92f7f2c 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/manager/ITEntityFactory.java +++ b/Aria/src/main/java/com/arialyy/aria/core/manager/ITEntityFactory.java @@ -21,7 +21,7 @@ import com.arialyy.aria.core.inf.AbsTaskEntity; /** * Created by Aria.Lao on 2017/11/1. */ -interface IEntityFactory> { +interface ITEntityFactory> { /** * 通过信息实体创建任务实体 */ diff --git a/Aria/src/main/java/com/arialyy/aria/core/manager/SubTaskManager.java b/Aria/src/main/java/com/arialyy/aria/core/manager/SubTaskManager.java index 8169f10d..9c4c826d 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/manager/SubTaskManager.java +++ b/Aria/src/main/java/com/arialyy/aria/core/manager/SubTaskManager.java @@ -13,9 +13,10 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.arialyy.aria.core; +package com.arialyy.aria.core.manager; import android.text.TextUtils; +import com.arialyy.aria.core.AriaManager; import com.arialyy.aria.core.command.group.GroupCmdFactory; import com.arialyy.aria.core.inf.AbsGroupTaskEntity; import com.arialyy.aria.util.ALog; 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 69e500c0..02c46356 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 @@ -15,79 +15,194 @@ */ package com.arialyy.aria.core.manager; -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.core.inf.AbsEntity; import com.arialyy.aria.core.inf.AbsTaskEntity; -import com.arialyy.aria.core.upload.UploadEntity; import com.arialyy.aria.core.upload.UploadTaskEntity; +import com.arialyy.aria.util.ALog; import com.arialyy.aria.util.CommonUtil; +import java.util.List; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.locks.Lock; +import java.util.concurrent.locks.ReentrantLock; /** * Created by Aria.Lao on 2017/11/1. * 任务实体管理器,负责 */ -public class TaskEntityManager { +public class TEManager { private static final String TAG = "TaskManager"; - private static volatile TaskEntityManager INSTANCE = null; + private static volatile TEManager INSTANCE = null; private Map map = new ConcurrentHashMap<>(); + private Lock lock; - public static TaskEntityManager getInstance() { + public static TEManager getInstance() { if (INSTANCE == null) { - synchronized (TaskEntityManager.class) { - INSTANCE = new TaskEntityManager(); + synchronized (TEManager.class) { + INSTANCE = new TEManager(); } } return INSTANCE; } + private TEManager() { + lock = new ReentrantLock(); + } + /** - * 通过下载实体获取下载任务实体,如果查找不到任务实体,则重新创建任务实体 + * 通过key创建任务,只适应于单任务,不能用于HTTP任务组,可用于Ftp文件夹 + * 如果是任务组,请使用{@link #createGTEntity(Class, List)} + * + * @return 如果任务实体创建失败,返回null */ - public DownloadTaskEntity getDTEntity(DownloadEntity entity) { - AbsTaskEntity tEntity = map.get(convertKey(entity.getKey())); - if (tEntity == null || !(tEntity instanceof DownloadTaskEntity)) { - tEntity = DTEntityFactory.getInstance().create(entity); - map.put(convertKey(entity.getKey()), tEntity); + public TE createTEntity(Class clazz, String key) { + final Lock lock = this.lock; + lock.lock(); + try { + AbsTaskEntity tEntity = map.get(convertKey(key)); + if (tEntity == null || tEntity.getClass() != clazz) { + ITEntityFactory factory = chooseFactory(clazz); + if (factory == null) { + ALog.e(TAG, "任务实体创建失败"); + return null; + } + tEntity = factory.create(key); + map.put(convertKey(key), tEntity); + } + return (TE) tEntity; + } finally { + lock.unlock(); } - return (DownloadTaskEntity) tEntity; } /** - * 通过下载实体获取下载任务组实体,如果查找不到任务组实体,则重新创建任务组实体 + * 创建任务组实体 + * + * @return 如果任务实体创建失败,返回null */ - public DownloadGroupTaskEntity getDGTEntity(DownloadGroupEntity entity) { - AbsTaskEntity tEntity = map.get(convertKey(entity.getKey())); - if (tEntity == null || !(tEntity instanceof DownloadGroupTaskEntity)) { - tEntity = DGTEntityFactory.getInstance().create(entity); - map.put(convertKey(entity.getKey()), tEntity); + public TE createGTEntity(Class clazz, List urls) { + final Lock lock = this.lock; + lock.lock(); + try { + String groupName = CommonUtil.getMd5Code(urls); + AbsTaskEntity tEntity = map.get(convertKey(groupName)); + if (tEntity == null || tEntity.getClass() != clazz) { + IGTEntityFactory factory = chooseGroupFactory(clazz); + if (factory == null) { + ALog.e(TAG, "任务实体创建失败"); + return null; + } + tEntity = factory.create(groupName, urls); + map.put(convertKey(groupName), tEntity); + } + return (TE) tEntity; + } finally { + lock.unlock(); } - return (DownloadGroupTaskEntity) tEntity; } /** - * 通过下载实体获取下载任务组实体,如果查找不到任务组实体,则重新创建任务组实体 + * 通过实体创建任务 + * + * @return 如果任务实体创建失败,返回null */ - public UploadTaskEntity getUTEntity(UploadEntity entity) { - AbsTaskEntity tEntity = map.get(convertKey(entity.getKey())); - if (tEntity == null || !(tEntity instanceof UploadTaskEntity)) { - tEntity = UTEntityFactory.getInstance().create(entity); - map.put(convertKey(entity.getKey()), tEntity); + public TE createTEntity(Class clazz, AbsEntity absEntity) { + final Lock lock = this.lock; + lock.lock(); + try { + AbsTaskEntity tEntity = map.get(convertKey(absEntity.getKey())); + if (tEntity == null || tEntity.getClass() != clazz) { + ITEntityFactory factory = chooseFactory(clazz); + if (factory == null) { + ALog.e(TAG, "任务实体创建失败"); + return null; + } + tEntity = factory.create(absEntity); + map.put(convertKey(absEntity.getKey()), tEntity); + } + return (TE) tEntity; + } finally { + lock.unlock(); + } + } + + private IGTEntityFactory chooseGroupFactory(Class clazz) { + if (clazz == DownloadGroupTaskEntity.class) { + return DGTEntityFactory.getInstance(); + } + return null; + } + + private ITEntityFactory chooseFactory(Class clazz) { + if (clazz == DownloadTaskEntity.class) { + return DTEntityFactory.getInstance(); + } else if (clazz == UploadTaskEntity.class) { + return UTEntityFactory.getInstance(); + } else if (clazz == DownloadGroupTaskEntity.class) { + return DGTEntityFactory.getInstance(); + } + return null; + } + + /** + * 从任务实体管理器中获取任务实体 + */ + public TE getTEntity(Class clazz, String key) { + final Lock lock = this.lock; + lock.lock(); + try { + AbsTaskEntity tEntity = map.get(convertKey(key)); + if (tEntity == null) { + return null; + } else { + return (TE) tEntity; + } + } finally { + lock.unlock(); + } + } + + /** + * 向管理器中增加任务实体 + * + * @return {@code false} 实体为null,添加失败 + */ + public boolean addTEntity(AbsTaskEntity te) { + if (te == null) { + ALog.e(TAG, "任务实体添加失败"); + return false; + } + final Lock lock = this.lock; + lock.lock(); + try { + return map.put(convertKey(te.key), te) != null; + } finally { + lock.unlock(); } - return (UploadTaskEntity) tEntity; } /** * 通过key删除任务实体 */ public AbsTaskEntity removeTEntity(String key) { - return map.remove(convertKey(key)); + final Lock lock = this.lock; + lock.lock(); + try { + return map.remove(convertKey(key)); + } finally { + lock.unlock(); + } } private String convertKey(String key) { - return CommonUtil.encryptBASE64(key); + final Lock lock = this.lock; + lock.lock(); + try { + return CommonUtil.keyToHashKey(key); + } finally { + lock.unlock(); + } } } diff --git a/Aria/src/main/java/com/arialyy/aria/core/manager/TaskManager.java b/Aria/src/main/java/com/arialyy/aria/core/manager/TaskManager.java index 72c5a3d3..e0f17a0b 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/manager/TaskManager.java +++ b/Aria/src/main/java/com/arialyy/aria/core/manager/TaskManager.java @@ -13,8 +13,9 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.arialyy.aria.core; +package com.arialyy.aria.core.manager; +import com.arialyy.aria.core.AriaManager; import com.arialyy.aria.core.inf.AbsTask; import com.arialyy.aria.util.ALog; import com.arialyy.aria.util.CommonUtil; @@ -26,14 +27,14 @@ import java.util.concurrent.ConcurrentHashMap; * Created by Aria.Lao on 2017/9/1. * 任务管理器 */ -public class TaskManager { +class TaskManager { private static final String TAG = "TaskManager"; private static volatile TaskManager INSTANCE = null; private Map map = new ConcurrentHashMap<>(); public static TaskManager getInstance() { if (INSTANCE == null) { - synchronized (AriaManager.LOCK) { + synchronized (TaskManager.class) { INSTANCE = new TaskManager(); } } diff --git a/Aria/src/main/java/com/arialyy/aria/core/manager/UTEntityFactory.java b/Aria/src/main/java/com/arialyy/aria/core/manager/UTEntityFactory.java index 01fd48a4..629daa5d 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/manager/UTEntityFactory.java +++ b/Aria/src/main/java/com/arialyy/aria/core/manager/UTEntityFactory.java @@ -15,43 +15,67 @@ */ 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.upload.UploadEntity; +import com.arialyy.aria.core.upload.UploadTaskEntity; import com.arialyy.aria.orm.DbEntity; +import com.arialyy.aria.util.Regular; +import java.util.regex.Pattern; /** * Created by Aria.Lao on 2017/11/1. * 任务实体工厂 */ -class DTaskEntityFactory implements ITaskEntityFactory { - private static final String TAG = "DTaskEntityFactory"; - private static volatile DTaskEntityFactory INSTANCE = null; +class UTEntityFactory implements ITEntityFactory { + private static final String TAG = "DTEntityFactory"; + private static volatile UTEntityFactory INSTANCE = null; - private DTaskEntityFactory() { + private UTEntityFactory() { } - public static DTaskEntityFactory getInstance() { + public static UTEntityFactory getInstance() { if (INSTANCE == null) { - synchronized (DTaskEntityFactory.class) { - INSTANCE = new DTaskEntityFactory(); + synchronized (UTEntityFactory.class) { + INSTANCE = new UTEntityFactory(); } } return INSTANCE; } - @Override public DownloadTaskEntity create(DownloadEntity entity) { - DownloadTaskEntity taskEntity = - DbEntity.findFirst(DownloadTaskEntity.class, "key=? and isGroupTask='false' and url=?", - entity.getDownloadPath(), entity.getUrl()); - if (taskEntity == null) { - taskEntity = new DownloadTaskEntity(); - taskEntity.save(entity); - } else if (taskEntity.entity == null || TextUtils.isEmpty(taskEntity.entity.getUrl())) { - taskEntity.save(entity); - } else if (!taskEntity.entity.getUrl().equals(entity.getUrl())) { //处理地址切换而保存路径不变 - taskEntity.save(entity); + @Override public UploadTaskEntity create(UploadEntity entity) { + UploadTaskEntity uTaskEntity = + DbEntity.findFirst(UploadTaskEntity.class, "key=?", entity.getFilePath()); + if (uTaskEntity == null) { + uTaskEntity = new UploadTaskEntity(); + uTaskEntity.entity = entity; } - return taskEntity; + if (uTaskEntity.entity == null) { + uTaskEntity.entity = entity; + } + return uTaskEntity; + } + + @Override public UploadTaskEntity create(String key) { + return create(getUploadEntity(key)); + } + + /** + * 从数据中读取上传实体,如果数据库查不到,则新创建一个上传实体 + * + * @param filePath 上传文件的文件路径 + */ + private UploadEntity getUploadEntity(String filePath) { + UploadEntity entity = UploadEntity.findFirst(UploadEntity.class, "filePath=?", filePath); + if (entity == null) { + entity = new UploadEntity(); + //String regex = "[/|\\\\|//]"; + String regex = Regular.REG_FILE_NAME; + Pattern p = Pattern.compile(regex); + String[] strs = p.split(filePath); + String fileName = strs[strs.length - 1]; + entity.setFileName(fileName); + entity.setFilePath(filePath); + entity.insert(); + } + return entity; } } 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 c76e983d..b5f161dc 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 @@ -20,6 +20,7 @@ import com.arialyy.aria.core.AriaManager; import com.arialyy.aria.core.inf.AbsTask; import com.arialyy.aria.core.inf.AbsTaskEntity; import com.arialyy.aria.core.inf.IEntity; +import com.arialyy.aria.core.manager.TEManager; import com.arialyy.aria.core.queue.pool.BaseCachePool; import com.arialyy.aria.core.queue.pool.BaseExecutePool; import com.arialyy.aria.util.ALog; @@ -164,6 +165,7 @@ abstract class AbsTaskQueue { private final String TAG = "FtpUploadTarget"; - private FtpUrlEntity mUrlEntity; FtpUploadTarget(String filePath, String targetName) { this.mTargetName = targetName; - mTaskEntity = DbEntity.findFirst(UploadTaskEntity.class, "key=?", filePath); + initTask(filePath); + } + + private void initTask(String filePath) { + mTaskEntity = TEManager.getInstance().getTEntity(UploadTaskEntity.class, filePath); if (mTaskEntity == null) { - mTaskEntity = new UploadTaskEntity(); - mTaskEntity.entity = getUploadEntity(filePath); - } - if (mTaskEntity.entity == null) { - mTaskEntity.entity = getUploadEntity(filePath); + mTaskEntity = TEManager.getInstance().createTEntity(UploadTaskEntity.class, filePath); } - mTaskEntity.requestType = AbsTaskEntity.FTP; mEntity = mTaskEntity.entity; File file = new File(filePath); mEntity.setFileName(file.getName()); mEntity.setFileSize(file.length()); + mTaskEntity.requestType = AbsTaskEntity.FTP; } /** 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 094f3c36..86ca8468 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 @@ -16,12 +16,9 @@ package com.arialyy.aria.core.upload; import android.support.annotation.NonNull; -import com.arialyy.aria.core.inf.AbsDownloadTarget; import com.arialyy.aria.core.inf.AbsUploadTarget; -import com.arialyy.aria.core.queue.UploadTaskQueue; -import com.arialyy.aria.orm.DbEntity; +import com.arialyy.aria.core.manager.TEManager; import java.io.File; -import java.util.regex.Pattern; /** * Created by lyy on 2017/2/28. @@ -31,18 +28,18 @@ public class UploadTarget extends AbsUploadTarget - + @@ -17,7 +17,7 @@ - + diff --git a/app/src/main/java/com/arialyy/simple/download/FtpDownloadActivity.java b/app/src/main/java/com/arialyy/simple/download/FtpDownloadActivity.java index 1b12ae96..0ef51911 100644 --- a/app/src/main/java/com/arialyy/simple/download/FtpDownloadActivity.java +++ b/app/src/main/java/com/arialyy/simple/download/FtpDownloadActivity.java @@ -37,7 +37,7 @@ import java.io.File; public class FtpDownloadActivity extends BaseActivity { //private final String URL = "ftp://192.168.1.9:21/下载/AriaPrj.zip"; //private final String URL = "ftp://192.168.1.9:21/下载/[电影天堂www.dy2018.com]赛车总动员3BD中英双字.mp4"; - private final String URL = "ftp://j:j@g.dygod18.com:7978/江湖告急BD国粤双语中字[电影天堂www.dy2018.com].mkv"; + private final String URL = "ftp://h:h@tv.dl1234.com:2199/付岩洞复仇者们05.mkv"; //private final String URL = "ftp://172.18.104.64:21/upload/测试/成都.mp3"; @Override protected void init(Bundle savedInstanceState) { 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 05cb13b2..9c0a28b2 100644 --- a/app/src/main/java/com/arialyy/simple/download/SingleTaskActivity.java +++ b/app/src/main/java/com/arialyy/simple/download/SingleTaskActivity.java @@ -34,6 +34,7 @@ import com.arialyy.aria.core.download.DownloadGroupTask; import com.arialyy.aria.core.download.DownloadTarget; import com.arialyy.aria.core.download.DownloadTask; import com.arialyy.aria.core.inf.IEntity; +import com.arialyy.aria.util.ALog; import com.arialyy.aria.util.CommonUtil; import com.arialyy.frame.util.show.L; import com.arialyy.frame.util.show.T; @@ -47,7 +48,7 @@ public class SingleTaskActivity extends BaseActivity { private static final String DOWNLOAD_URL = //"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://static.gaoshouyou.com/d/22/94/822260b849944492caadd2983f9bb624.apk"; //"http://sitcac.daxincf.cn/wp-content/uploads/swift_vido/01/element.mp4_1"; //"http://120.25.196.56:8000/filereq?id=15692406294&ipncid=105635&client=android&filename=20170819185541.avi"; //"http://down2.xiaoshuofuwuqi.com/d/file/filetxt/20170608/14/%BA%DA%CE%D7%CA%A6%E1%C8%C6%F0.txt"; @@ -59,7 +60,7 @@ public class SingleTaskActivity extends BaseActivity { //"http://ox.konsung.net:5555/ksdc-web/download/downloadFile/?fileName=ksdc_1.0.2.apk&rRange=0-"; //"http://172.18.104.50:8080/download/_302turn"; //"http://gdown.baidu.com/data/wisegame/0904344dee4a2d92/QQ_718.apk"; - "http://172.21.1.99:8080/download/test+ 中文123.zip"; + //"http://172.21.1.99:8080/download/test+ 中文123.zip"; @Bind(R.id.start) Button mStart; @Bind(R.id.stop) Button mStop; @Bind(R.id.cancel) Button mCancel; @@ -221,11 +222,17 @@ public class SingleTaskActivity extends BaseActivity { } private void startD(){ - Aria.download(this).load("aaaa.apk"); + //Aria.get(this).setLogLevel(ALog.LOG_CLOSE); + //Aria.download(this).load("aaaa.apk"); Aria.download(SingleTaskActivity.this) .load(DOWNLOAD_URL) .addHeader("groupName", "value") .setDownloadPath(Environment.getExternalStorageDirectory().getPath() + "/hhhhhhhh.apk") .start(); } + + @Override protected void onDestroy() { + super.onDestroy(); + Aria.download(this).unRegister(); + } } \ No newline at end of file diff --git a/build.gradle b/build.gradle index 3187e67b..7b0671ce 100644 --- a/build.gradle +++ b/build.gradle @@ -39,7 +39,7 @@ task clean(type: Delete) { ext { userOrg = 'arialyy' groupId = 'com.arialyy.aria' - publishVersion = '3.3.6_dev_1' + publishVersion = '3.3.6_dev_2' // publishVersion = '1.0.3' //FTP插件 repoName='maven' desc = 'android 下载框架'