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 5b82ddfc..42c9040c 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 @@ -18,6 +18,7 @@ package com.arialyy.aria.core.download; import android.os.Parcel; import com.arialyy.aria.core.inf.AbsGroupEntity; import com.arialyy.aria.orm.OneToMany; +import java.util.ArrayList; import java.util.LinkedList; import java.util.List; @@ -27,15 +28,29 @@ import java.util.List; */ public class DownloadGroupEntity extends AbsGroupEntity { - @OneToMany(table = DownloadEntity.class, key = "groupName") - private List mChild = new LinkedList<>(); + @OneToMany(table = DownloadEntity.class, key = "groupName") private List + mSubtask = new ArrayList<>(); - public List getChild() { - return mChild; + //任务组下载文件的文件夹地址 + private String mDirPath = ""; + + public List getSubTask() { + return mSubtask; + } + + public void setSubTasks(List subTasks) { + this.mSubtask = subTasks; } - public void setChild(List child) { - this.mChild = child; + public String getDirPath() { + return mDirPath; + } + + public void setDirPath(String dirPath) { + this.mDirPath = dirPath; + } + + public DownloadGroupEntity() { } @Override public int describeContents() { @@ -44,15 +59,14 @@ public class DownloadGroupEntity extends AbsGroupEntity { @Override public void writeToParcel(Parcel dest, int flags) { super.writeToParcel(dest, flags); - dest.writeTypedList(this.mChild); - } - - public DownloadGroupEntity() { + dest.writeTypedList(this.mSubtask); + dest.writeString(this.mDirPath); } protected DownloadGroupEntity(Parcel in) { super(in); - this.mChild = in.createTypedArrayList(DownloadEntity.CREATOR); + this.mSubtask = in.createTypedArrayList(DownloadEntity.CREATOR); + this.mDirPath = in.readString(); } public static final Creator CREATOR = new Creator() { 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 f8577e60..11a03da7 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 @@ -15,27 +15,38 @@ */ package com.arialyy.aria.core.download; +import android.text.TextUtils; import com.arialyy.aria.core.inf.AbsGroupTarget; import com.arialyy.aria.core.inf.IEntity; 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; +import java.util.LinkedList; import java.util.List; +import java.util.Map; /** * Created by AriaL on 2017/6/29. */ public class DownloadGroupTarget extends AbsGroupTarget { - private List mUrls; + private List mUrls = new ArrayList<>(); + /** + * 子任务文件名 + */ + private List mSubtaskFileName = new ArrayList<>(); private String mGroupName; + /** + * 是否已经设置了文件路径 + */ + private boolean isSetDirPathed = false; DownloadGroupTarget(List urls, String targetName) { this.mTargetName = targetName; this.mUrls = urls; mGroupName = CommonUtil.getMd5Code(urls); - mTaskEntity = DbEntity.findData(DownloadGroupTaskEntity.class, "key=?", mGroupName); + mTaskEntity = DbEntity.findFirst(DownloadGroupTaskEntity.class, "key=?", mGroupName); if (mTaskEntity == null) { mTaskEntity = new DownloadGroupTaskEntity(); mTaskEntity.key = mGroupName; @@ -49,7 +60,7 @@ public class DownloadGroupTarget private DownloadGroupEntity getDownloadGroupEntity() { DownloadGroupEntity entity = - DbEntity.findData(DownloadGroupEntity.class, "groupName=?", mGroupName); + DbEntity.findFirst(DownloadGroupEntity.class, "groupName=?", mGroupName); if (entity == null) { entity = new DownloadGroupEntity(); } @@ -58,7 +69,7 @@ public class DownloadGroupTarget /** * 设置任务组的文件夹路径,在Aria中,任务组的所有子任务都会下载到以任务组组名的文件夹中。 - * 如:groupDirPath = "/mnt/sdcard/download/", groupName = "group_test" + * 如:groupDirPath = "/mnt/sdcard/download/group_test" *
    *   {@code
    *      + mnt
@@ -75,43 +86,86 @@ public class DownloadGroupTarget
    *
    * @param groupDirPath 任务组保存文件夹路径
    */
-  public DownloadGroupTarget setDownloadPath(String groupDirPath) {
+  public DownloadGroupTarget setDownloadDirPath(String groupDirPath) {
+    if (TextUtils.isEmpty(groupDirPath)) {
+      throw new NullPointerException("任务组文件夹保存路径不能为null");
+    }
+
+    if (mEntity.getDirPath().equals(groupDirPath)) return this;
 
+    File file = new File(groupDirPath);
+    if (file.exists() && file.isFile()) {
+      throw new IllegalArgumentException("路径不能为文件");
+    }
+    if (!file.exists()) {
+      file.mkdirs();
+    }
+
+    if (!TextUtils.isEmpty(mEntity.getDirPath())) {
+      reChangeDirPath(groupDirPath);
+    } else {
+      mEntity.setSubTasks(createSubTask());
+    }
+    mEntity.setDirPath(groupDirPath);
+    mEntity.update();
+    isSetDirPathed = true;
     return this;
   }
 
   /**
-   * 设置保存路径组
+   * 改变任务组文件夹路径
+   *
+   * @param newDirPath 新的文件夹路径
+   */
+  private void reChangeDirPath(String newDirPath) {
+    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()));
+      }
+    } else {
+      mEntity.setSubTasks(createSubTask());
+    }
+  }
+
+  /**
+   * 设置子任务文件名,该方法如果在{@link #setDownloadDirPath(String)}之前调用,则不生效
    */
-  public DownloadGroupTarget setDownloadPaths(List paths) {
-    CheckUtil.checkDownloadPaths(paths);
-    if (mUrls.size() != paths.size()) {
+  public DownloadGroupTarget setSubtaskFileName(List subTaskFileName) {
+    if (subTaskFileName == null || subTaskFileName.isEmpty()) return this;
+    mSubtaskFileName.addAll(subTaskFileName);
+    if (mUrls.size() != subTaskFileName.size()) {
       throw new IllegalArgumentException("下载链接数必须要和保存路径的数量一致");
     }
-    for (int i = 0, len = mUrls.size(); i < len; i++) {
-      mTaskEntity.getEntity().getChild().add(createDownloadEntity(mUrls.get(i), paths.get(i)));
+    if (isSetDirPathed) {
+      List entities = mEntity.getSubTask();
+      int i = 0;
+      for (DownloadEntity entity : entities) {
+        entity.setFileName(mSubtaskFileName.get(i));
+        entity.update();
+      }
     }
     return this;
   }
 
   /**
-   * 创建子任务下载实体
-   *
-   * @param url 下载地址
-   * @param path 保存路径
+   * 创建子任务
    */
-  private DownloadEntity createDownloadEntity(String url, String path) {
-    DownloadEntity entity = DownloadEntity.findData(DownloadEntity.class, "downloadUrl=?", url);
-    if (entity == null) {
-      entity = new DownloadEntity();
-    }
-    File file = new File(path);
-    if (!file.exists()) {
-      entity.setState(IEntity.STATE_WAIT);
+  private List createSubTask() {
+    List list = new ArrayList<>();
+    for (int i = 0, len = mUrls.size(); i < len; i++) {
+      DownloadEntity entity = new DownloadEntity();
+      entity.setDownloadUrl(mUrls.get(i));
+      String fileName = mSubtaskFileName.get(i);
+      if (TextUtils.isEmpty(fileName)) {
+        fileName = CommonUtil.keyToHashKey(mUrls.get(i));
+      }
+      entity.setDownloadPath(mEntity.getDirPath() + "/" + fileName);
+      entity.setGroupName(mGroupName);
+      entity.save();
+      list.add(entity);
     }
-    entity.setDownloadPath(path);
-    entity.setDownloadUrl(url);
-    entity.setFileName(file.getName());
-    return entity;
+    return list;
   }
 }
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 260988bf..182d752d 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
@@ -18,15 +18,12 @@ package com.arialyy.aria.core.download;
 import android.support.annotation.NonNull;
 import com.arialyy.aria.core.AriaManager;
 import com.arialyy.aria.core.inf.AbsReceiver;
-import com.arialyy.aria.core.inf.IEntity;
 import com.arialyy.aria.core.inf.IReceiver;
 import com.arialyy.aria.core.command.normal.NormalCmdFactory;
 import com.arialyy.aria.core.scheduler.DownloadSchedulers;
 import com.arialyy.aria.core.scheduler.ISchedulerListener;
-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.List;
 import java.util.Set;
 
@@ -119,14 +116,14 @@ public class DownloadReceiver extends AbsReceiver {
    */
   public DownloadEntity getDownloadEntity(String downloadUrl) {
     CheckUtil.checkDownloadUrl(downloadUrl);
-    return DownloadEntity.findData(DownloadEntity.class, "downloadUrl=?", downloadUrl);
+    return DownloadEntity.findFirst(DownloadEntity.class, "downloadUrl=?", downloadUrl);
   }
 
   /**
    * 下载任务是否存在
    */
   @Override public boolean taskExists(String downloadUrl) {
-    return DownloadEntity.findData(DownloadEntity.class, "downloadUrl=?", downloadUrl) != null;
+    return DownloadEntity.findFirst(DownloadEntity.class, "downloadUrl=?", downloadUrl) != null;
   }
 
   @Override public List getTaskList() {
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 1fa76a8f..799e729d 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
@@ -36,7 +36,7 @@ public class DownloadTarget
 
   DownloadTarget(String url, String targetName) {
     mTargetName = targetName;
-    mTaskEntity = DbEntity.findData(DownloadTaskEntity.class, "key=?", url);
+    mTaskEntity = DbEntity.findFirst(DownloadTaskEntity.class, "key=?", url);
     if (mTaskEntity == null) {
       mTaskEntity = new DownloadTaskEntity();
       mTaskEntity.entity = new DownloadEntity();
@@ -54,7 +54,7 @@ public class DownloadTarget
    */
   private DownloadEntity getEntity(String downloadUrl) {
     DownloadEntity entity =
-        DownloadEntity.findData(DownloadEntity.class, "downloadUrl=?", downloadUrl);
+        DownloadEntity.findFirst(DownloadEntity.class, "downloadUrl=?", downloadUrl);
     if (entity == null) {
       entity = new DownloadEntity();
     }
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 2232722c..90ed0cbe 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
@@ -81,7 +81,7 @@ public class DownloadGroupUtil implements IDownloadUtil {
     mInfoPool = Executors.newCachedThreadPool();
     mExePool = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
 
-    for (DownloadEntity entity : mTaskEntity.entity.getChild()) {
+    for (DownloadEntity entity : mTaskEntity.entity.getSubTask()) {
       File file = new File(entity.getDownloadPath());
       if (entity.isDownloadComplete() && file.exists()) {
         mTotalSize += entity.getFileSize();
@@ -175,7 +175,7 @@ public class DownloadGroupUtil implements IDownloadUtil {
             startChildDownload(te);
           }
           mInitNum++;
-          if (mInitNum + mFailNum == mTaskEntity.getEntity().getChild().size()) {
+          if (mInitNum + mFailNum == mTaskEntity.getEntity().getSubTask().size()) {
             startRunningFlow();
           }
         }
@@ -191,7 +191,7 @@ public class DownloadGroupUtil implements IDownloadUtil {
           if (failNum < 10) {
             mInfoPool.execute(createFileInfoThread(te));
           }
-          if (mInitNum + mFailNum == mTaskEntity.getEntity().getChild().size()) {
+          if (mInitNum + mFailNum == mTaskEntity.getEntity().getSubTask().size()) {
             startRunningFlow();
           }
         }
@@ -238,7 +238,7 @@ public class DownloadGroupUtil implements IDownloadUtil {
    */
   private DownloadTaskEntity createDownloadTask(DownloadEntity entity) {
     DownloadTaskEntity taskEntity =
-        DbEntity.findData(DownloadTaskEntity.class, "key=?", entity.getDownloadUrl());
+        DbEntity.findFirst(DownloadTaskEntity.class, "key=?", entity.getDownloadUrl());
     if (taskEntity != null) {
       return taskEntity;
     }
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 7d1eae99..8ff32489 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
@@ -192,7 +192,7 @@ class Downloader implements Runnable, IDownloadUtil {
       CommonUtil.createFile(mConfigFile.getPath());
     } else if (!mTempFile.exists()) {
       isNewTask = true;
-    } else if (DbEntity.findData(DownloadEntity.class, "downloadUrl=?", mEntity.getDownloadUrl())
+    } else if (DbEntity.findFirst(DownloadEntity.class, "downloadUrl=?", mEntity.getDownloadUrl())
         == null) {
       isNewTask = true;
     } else {
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 3d47e2ca..eb410f07 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
@@ -17,21 +17,17 @@ package com.arialyy.aria.core.upload;
 
 import android.support.annotation.NonNull;
 import com.arialyy.aria.core.AriaManager;
-import com.arialyy.aria.core.command.normal.AbsNormalCmd;
 import com.arialyy.aria.core.command.normal.NormalCmdFactory;
 import com.arialyy.aria.core.download.DownloadTaskEntity;
 import com.arialyy.aria.core.inf.AbsReceiver;
-import com.arialyy.aria.core.inf.IEntity;
 import com.arialyy.aria.core.inf.IReceiver;
 import com.arialyy.aria.core.scheduler.ISchedulerListener;
 import com.arialyy.aria.core.scheduler.UploadSchedulers;
 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;
-import java.util.regex.Pattern;
 
 /**
  * Created by lyy on 2017/2/6.
@@ -56,14 +52,14 @@ public class UploadReceiver extends AbsReceiver {
    */
   public UploadEntity getUploadEntity(String filePath) {
     CheckUtil.checkUploadPath(filePath);
-    return DbEntity.findData(UploadEntity.class, "filePath=?", filePath);
+    return DbEntity.findFirst(UploadEntity.class, "filePath=?", filePath);
   }
 
   /**
    * 下载任务是否存在
    */
   @Override public boolean taskExists(String filePath) {
-    return DbEntity.findData(UploadEntity.class, "filePath=?", filePath) != null;
+    return DbEntity.findFirst(UploadEntity.class, "filePath=?", filePath) != null;
   }
 
   @Override public List getTaskList() {
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 94d5ca07..007ac0e3 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
-   * DownloadEntity.findData(DownloadEntity.class, "downloadUrl=?", downloadUrl);
+   * DownloadEntity.findFirst(DownloadEntity.class, "downloadUrl=?", downloadUrl);
    * 
    *
    * @return 没有数据返回null
@@ -70,12 +70,12 @@ public class DbEntity {
   /**
    * 查询一行数据
    * 
-   * DownloadEntity.findData(DownloadEntity.class, "downloadUrl=?", downloadUrl);
+   * DownloadEntity.findFirst(DownloadEntity.class, "downloadUrl=?", downloadUrl);
    * 
    *
    * @return 没有数据返回null
    */
-  public static  T findData(Class clazz, String... expression) {
+  public static  T findFirst(Class clazz, String... expression) {
     DbUtil util = DbUtil.getInstance();
     List datas = util.findData(clazz, expression);
     return datas == null ? null : datas.size() > 0 ? datas.get(0) : null;
@@ -149,7 +149,7 @@ public class DbEntity {
     updateRowID();
   }
 
-  private  T findData(Class clazz, @NonNull String[] wheres,
+  private  T findFirst(Class clazz, @NonNull String[] wheres,
       @NonNull String[] values) {
     DbUtil util = DbUtil.getInstance();
     List list = util.findData(clazz, wheres, values);
@@ -178,7 +178,7 @@ public class DbEntity {
           values.add(field.get(this) + "");
         }
       }
-      DbEntity entity = findData(getClass(), where.toArray(new String[where.size()]),
+      DbEntity entity = findFirst(getClass(), where.toArray(new String[where.size()]),
           values.toArray(new String[values.size()]));
       if (entity != null) {
         rowID = entity.rowID;