diff --git a/Aria/src/main/java/com/arialyy/aria/core/Aria.java b/Aria/src/main/java/com/arialyy/aria/core/Aria.java index e7381e54..efdbce23 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/Aria.java +++ b/Aria/src/main/java/com/arialyy/aria/core/Aria.java @@ -1,154 +1,154 @@ -/* - * 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; - -import android.annotation.TargetApi; -import android.app.Activity; -import android.app.Application; -import android.app.Dialog; -import android.app.Service; -import android.content.Context; -import android.os.Build; -import android.support.v4.app.DialogFragment; -import android.support.v4.app.Fragment; -import android.widget.PopupWindow; -import com.arialyy.aria.core.download.DownloadReceiver; -import com.arialyy.aria.core.upload.UploadReceiver; -import com.arialyy.aria.util.ALog; - -/** - * Created by lyy on 2016/12/1. - * - * @see Aria - * @see Aria doc - * Aria启动,管理全局任务 - *
- *   
- *   //下载
- *   Aria.download(this)
- *       .load(URL)     //下载地址,必填
- *       //文件保存路径,必填
- *       .setDownloadPath(Environment.getExternalStorageDirectory().getPath() + "/test.apk")
- *       .start();
- *   
- *   
- *    //上传
- *    Aria.upload(this)
- *        .load(filePath)     //文件路径,必填
- *        .setTempUrl(uploadUrl)  //上传路径,必填
- *        .setAttachment(fileKey)   //服务器读取文件的key,必填
- *        .start();
- *   
- * 
- * - * 如果你需要在【Activity、Service、Application、DialogFragment、Fragment、PopupWindow、Dialog】 - * 之外的java中使用Aria,那么你应该在Application或Activity初始化的时候调用{@link #init(Context)}对Aria进行初始化 - * 然后才能使用{@link #download(Object)}、{@link #upload(Object)} - * - *
- *   
- *       Aria.init(this);
- *
- *      Aria.download(this)
- *       .load(URL)     //下载地址,必填
- *       //文件保存路径,必填
- *       .setDownloadPath(Environment.getExternalStorageDirectory().getPath() + "/test.apk")
- *       .start();
- *
- *   
- *
- * 
- */ -@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1) public class Aria { - - private Aria() { - } - - /** - * 下载,在当前类中调用Aria方法,参数需要使用this,否则将 - * 如果不是【Activity、Service、Application、DialogFragment、Fragment、PopupWindow、Dialog】对象,那么你 - * 需要在对象中初始化下载前,在Application或Activity初始化的时候调用{@link #init(Context)}对Aria进行初始化 - * - * @param obj 观察者对象,为本类对象,使用{@code this} - */ - public static DownloadReceiver download(Object obj) { - if (AriaManager.getInstance() != null){ - return AriaManager.getInstance().download(obj); - } - return get(convertContext(obj)).download(obj); - } - - /** - * 上传 - * 如果不是【Activity、Service、Application、DialogFragment、Fragment、PopupWindow、Dialog】对象,那么你 - * 需要在对象中初始化下载前,在Application或Activity初始化的时候调用{@link #init(Context)}对Aria进行初始化 - * - * @param obj 观察者对象,为本类对象,使用{@code this} - */ - public static UploadReceiver upload(Object obj) { - if (AriaManager.getInstance() != null){ - return AriaManager.getInstance().upload(obj); - } - return get(convertContext(obj)).upload(obj); - } - - /** - * 处理通用事件 - */ - public static AriaManager get(Context context) { - if (context == null) { - throw new NullPointerException("context 无效,在非【Activity、Service、Application、DialogFragment、Fragment、PopupWindow、Dialog】," - + "请参考【https://aria.laoyuyu.me/aria_doc/start/any_java.html】,参数请使用 download(this) 或 upload(this);" - + "不要使用 download(getContext()) 或 upload(getContext())"); - } - return AriaManager.getInstance(context); - } - - /** - * 初始化Aria,如果你需要在【Activity、Service、Application、DialogFragment、Fragment、PopupWindow、Dialog】 - * 之外的java中使用Aria,那么你应该在Application或Activity初始化的时候调用本方法对Aria进行初始化 - * 只需要初始化一次就可以 - * {@link #download(Object)}、{@link #upload(Object)} - */ - public static AriaManager init(Context context) { - return AriaManager.getInstance(context); - } - - private static Context convertContext(Object obj) { - if (obj instanceof Application) { - return (Application) obj; - } else if (obj instanceof Service) { - return (Service) obj; - } else if (obj instanceof Activity) { - return (Activity) obj; - } else if (obj instanceof DialogFragment) { - return ((DialogFragment) obj).getContext(); - } else if (obj instanceof android.app.DialogFragment) { - return ((android.app.DialogFragment) obj).getActivity(); - } else if (obj instanceof android.support.v4.app.Fragment) { - return ((Fragment) obj).getContext(); - } else if (obj instanceof android.app.Fragment) { - return ((android.app.Fragment) obj).getActivity(); - } else if (obj instanceof Dialog) { - return ((Dialog) obj).getContext(); - } else if (obj instanceof PopupWindow) { - return ((PopupWindow) obj).getContentView().getContext(); - } - ALog.e("Aria", "请使用download(this)或upload(this)"); - return null; - } -} +/* + * 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; + +import android.annotation.TargetApi; +import android.app.Activity; +import android.app.Application; +import android.app.Dialog; +import android.app.Service; +import android.content.Context; +import android.os.Build; +import android.support.v4.app.DialogFragment; +import android.support.v4.app.Fragment; +import android.widget.PopupWindow; +import com.arialyy.aria.core.download.DownloadReceiver; +import com.arialyy.aria.core.upload.UploadReceiver; +import com.arialyy.aria.util.ALog; + +/** + * Created by lyy on 2016/12/1. + * + * @see Aria + * @see Aria doc + * Aria启动,管理全局任务 + *
+ *   
+ *   //下载
+ *   Aria.download(this)
+ *       .load(URL)     //下载地址,必填
+ *       //文件保存路径,必填
+ *       .setDownloadPath(Environment.getExternalStorageDirectory().getPath() + "/test.apk")
+ *       .start();
+ *   
+ *   
+ *    //上传
+ *    Aria.upload(this)
+ *        .load(filePath)     //文件路径,必填
+ *        .setTempUrl(uploadUrl)  //上传路径,必填
+ *        .setAttachment(fileKey)   //服务器读取文件的key,必填
+ *        .start();
+ *   
+ * 
+ * + * 如果你需要在【Activity、Service、Application、DialogFragment、Fragment、PopupWindow、Dialog】 + * 之外的java中使用Aria,那么你应该在Application或Activity初始化的时候调用{@link #init(Context)}对Aria进行初始化 + * 然后才能使用{@link #download(Object)}、{@link #upload(Object)} + * + *
+ *   
+ *       Aria.init(this);
+ *
+ *      Aria.download(this)
+ *       .load(URL)     //下载地址,必填
+ *       //文件保存路径,必填
+ *       .setDownloadPath(Environment.getExternalStorageDirectory().getPath() + "/test.apk")
+ *       .start();
+ *
+ *   
+ *
+ * 
+ */ +@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1) public class Aria { + + private Aria() { + } + + /** + * 下载,在当前类中调用Aria方法,参数需要使用this,否则将 + * 如果不是【Activity、Service、Application、DialogFragment、Fragment、PopupWindow、Dialog】对象,那么你 + * 需要在对象中初始化下载前,在Application或Activity初始化的时候调用{@link #init(Context)}对Aria进行初始化 + * + * @param obj 观察者对象,为本类对象,使用{@code this} + */ + public static DownloadReceiver download(Object obj) { + if (AriaManager.getInstance() != null){ + return AriaManager.getInstance().download(obj); + } + return get(convertContext(obj)).download(obj); + } + + /** + * 上传 + * 如果不是【Activity、Service、Application、DialogFragment、Fragment、PopupWindow、Dialog】对象,那么你 + * 需要在对象中初始化下载前,在Application或Activity初始化的时候调用{@link #init(Context)}对Aria进行初始化 + * + * @param obj 观察者对象,为本类对象,使用{@code this} + */ + public static UploadReceiver upload(Object obj) { + if (AriaManager.getInstance() != null){ + return AriaManager.getInstance().upload(obj); + } + return get(convertContext(obj)).upload(obj); + } + + /** + * 处理通用事件 + */ + public static AriaManager get(Context context) { + if (context == null) { + throw new NullPointerException("context 无效,在非【Activity、Service、Application、DialogFragment、Fragment、PopupWindow、Dialog】," + + "请参考【https://aria.laoyuyu.me/aria_doc/start/any_java.html】,参数请使用 download(this) 或 upload(this);" + + "不要使用 download(getContext()) 或 upload(getContext())"); + } + return AriaManager.getInstance(context); + } + + /** + * 初始化Aria,如果你需要在【Activity、Service、Application、DialogFragment、Fragment、PopupWindow、Dialog】 + * 之外的java中使用Aria,那么你应该在Application或Activity初始化的时候调用本方法对Aria进行初始化 + * 只需要初始化一次就可以 + * {@link #download(Object)}、{@link #upload(Object)} + */ + public static AriaManager init(Context context) { + return AriaManager.getInstance(context); + } + + private static Context convertContext(Object obj) { + if (obj instanceof Application) { + return (Application) obj; + } else if (obj instanceof Service) { + return (Service) obj; + } else if (obj instanceof Activity) { + return (Activity) obj; + } else if (obj instanceof DialogFragment) { + return ((DialogFragment) obj).getContext(); + } else if (obj instanceof android.app.DialogFragment) { + return ((android.app.DialogFragment) obj).getActivity(); + } else if (obj instanceof android.support.v4.app.Fragment) { + return ((Fragment) obj).getContext(); + } else if (obj instanceof android.app.Fragment) { + return ((android.app.Fragment) obj).getActivity(); + } else if (obj instanceof Dialog) { + return ((Dialog) obj).getContext(); + } else if (obj instanceof PopupWindow) { + return ((PopupWindow) obj).getContentView().getContext(); + } + ALog.e("Aria", "请使用download(this)或upload(this)"); + return null; + } +} 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 21b80459..d99589ff 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/AriaManager.java +++ b/Aria/src/main/java/com/arialyy/aria/core/AriaManager.java @@ -161,13 +161,11 @@ import org.xml.sax.SAXException; * 初始化数据库 */ private void initDb(Context context) { - // 这个地方错误了,获取数据库路径见:https://www.jianshu.com/p/815c9efc5449 String oldDbName = "AriaLyyDb"; - String oldDbPath = context.getDatabasePath(oldDbName).getPath(); - File db = new File(oldDbPath); - File dbConfig = new File(String.format("%s/%s", db.getParent(), "AriaLyyDb-journal")); - if (db.exists()) { - db.renameTo(new File(String.format("%s/%s", db.getParent(), "AndroidAria.db"))); + File oldDbFile = context.getDatabasePath(oldDbName); + if (oldDbFile != null && oldDbFile.exists()) { + File dbConfig = new File(String.format("%s/%s", oldDbFile.getParent(), "AriaLyyDb-journal")); + oldDbFile.renameTo(new File(String.format("%s/%s", oldDbFile.getParent(), "AndroidAria.db"))); // 如果数据库是在/data/data/{packagename}/databases/下面,journal文件因权限问题将无法删除和重命名 if (dbConfig.exists()) { dbConfig.delete(); diff --git a/Aria/src/main/java/com/arialyy/aria/core/command/CommandManager.java b/Aria/src/main/java/com/arialyy/aria/core/command/CommandManager.java index 786e0d51..6ca3a813 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/command/CommandManager.java +++ b/Aria/src/main/java/com/arialyy/aria/core/command/CommandManager.java @@ -43,7 +43,7 @@ public class CommandManager { @Event public void add(AddCmd cmd) { - if (CommonUtil.isFastDoubleClick()){ + if (CommonUtil.isFastDoubleClick()) { return; } cmd.executeCmd(); @@ -51,7 +51,7 @@ public class CommandManager { @Event public void start(StartCmd cmd) { - if (CommonUtil.isFastDoubleClick()){ + if (CommonUtil.isFastDoubleClick()) { return; } cmd.executeCmd(); @@ -59,23 +59,17 @@ public class CommandManager { @Event public void stop(StopCmd cmd) { - if (CommonUtil.isFastDoubleClick()){ - return; - } cmd.executeCmd(); } @Event public void cancel(CancelCmd cmd) { - if (CommonUtil.isFastDoubleClick()){ - return; - } cmd.executeCmd(); } @Event public void stopAll(StopAllCmd cmd) { - if (CommonUtil.isFastDoubleClick()){ + if (CommonUtil.isFastDoubleClick()) { return; } cmd.executeCmd(); @@ -83,7 +77,7 @@ public class CommandManager { @Event public void cancelAll(CancelAllCmd cmd) { - if (CommonUtil.isFastDoubleClick()){ + if (CommonUtil.isFastDoubleClick()) { return; } cmd.executeCmd(); @@ -91,7 +85,7 @@ public class CommandManager { @Event public void reStart(ReStartCmd cmd) { - if (CommonUtil.isFastDoubleClick()){ + if (CommonUtil.isFastDoubleClick()) { return; } cmd.executeCmd(); @@ -99,7 +93,7 @@ public class CommandManager { @Event public void highestPriority(HighestPriorityCmd cmd) { - if (CommonUtil.isFastDoubleClick()){ + if (CommonUtil.isFastDoubleClick()) { return; } cmd.executeCmd(); @@ -107,7 +101,7 @@ public class CommandManager { @Event public void resumeAll(ResumeAllCmd cmd) { - if (CommonUtil.isFastDoubleClick()){ + if (CommonUtil.isFastDoubleClick()) { return; } cmd.executeCmd(); @@ -115,7 +109,7 @@ public class CommandManager { @Event public void subStart(DGSubStartCmd cmd) { - if (CommonUtil.isFastDoubleClick()){ + if (CommonUtil.isFastDoubleClick()) { return; } cmd.executeCmd(); @@ -123,9 +117,6 @@ public class CommandManager { @Event public void subStop(DGSubStopCmd cmd) { - if (CommonUtil.isFastDoubleClick()){ - return; - } cmd.executeCmd(); } } diff --git a/Aria/src/main/java/com/arialyy/aria/core/command/StartCmd.java b/Aria/src/main/java/com/arialyy/aria/core/command/StartCmd.java index c1a7bda1..477575b4 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/command/StartCmd.java +++ b/Aria/src/main/java/com/arialyy/aria/core/command/StartCmd.java @@ -25,6 +25,7 @@ import com.arialyy.aria.core.download.DownloadGroupEntity; import com.arialyy.aria.core.inf.AbsTask; import com.arialyy.aria.core.inf.AbsTaskWrapper; import com.arialyy.aria.core.inf.IEntity; +import com.arialyy.aria.core.inf.ITaskWrapper; import com.arialyy.aria.core.manager.TaskWrapperManager; import com.arialyy.aria.core.queue.DownloadGroupTaskQueue; import com.arialyy.aria.core.queue.DownloadTaskQueue; @@ -135,9 +136,9 @@ final class StartCmd extends AbsNormalCmd { DbEntity.findDatas(DownloadGroupEntity.class, "state=?", "3"); if (dEntities != null && !dEntities.isEmpty()) { for (DownloadGroupEntity e : dEntities) { - if (e.getTaskType() == AbsTaskWrapper.DG_HTTP) { + if (e.getTaskType() == ITaskWrapper.DG_HTTP) { waitList.add(tManager.getGroupWrapper(DGTaskWrapper.class, e.getId())); - } else if (e.getTaskType() == AbsTaskWrapper.D_FTP_DIR) { + } else if (e.getTaskType() == ITaskWrapper.D_FTP_DIR) { waitList.add(tManager.getGroupWrapper(DGTaskWrapper.class, e.getId())); } } @@ -160,8 +161,8 @@ final class StartCmd extends AbsNormalCmd { AbsTask task = getTask(te.getKey()); if (task != null) continue; if (te instanceof DTaskWrapper) { - if (te.getRequestType() == AbsTaskWrapper.D_FTP - || te.getRequestType() == AbsTaskWrapper.U_FTP) { + if (te.getRequestType() == ITaskWrapper.D_FTP + || te.getRequestType() == ITaskWrapper.U_FTP) { te.asFtp().setUrlEntity(CommonUtil.getFtpUrlInfo(te.getEntity().getKey())); } mQueue = DownloadTaskQueue.getInstance(); diff --git a/Aria/src/main/java/com/arialyy/aria/core/common/BaseDelegate.java b/Aria/src/main/java/com/arialyy/aria/core/common/BaseDelegate.java index 0d9ae003..0d4048a0 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/common/BaseDelegate.java +++ b/Aria/src/main/java/com/arialyy/aria/core/common/BaseDelegate.java @@ -45,7 +45,7 @@ public abstract class BaseDelegate { * @param clazz {@link ControllerType#START_CONTROLLER}、{@link ControllerType#NORMAL_CONTROLLER} */ @CheckResult(suggest = Suggest.TASK_CONTROLLER) - public T controller(@ControllerType Class clazz) { + public synchronized T controller(@ControllerType Class clazz) { return FeatureController.newInstance(clazz, getTaskWrapper()); } } diff --git a/Aria/src/main/java/com/arialyy/aria/core/common/BaseListener.java b/Aria/src/main/java/com/arialyy/aria/core/common/BaseListener.java index 58040931..b29789e0 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/common/BaseListener.java +++ b/Aria/src/main/java/com/arialyy/aria/core/common/BaseListener.java @@ -16,8 +16,6 @@ package com.arialyy.aria.core.common; import android.os.Handler; -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.AbsTask; import com.arialyy.aria.core.inf.AbsTaskWrapper; @@ -26,12 +24,10 @@ import com.arialyy.aria.core.inf.IEventListener; import com.arialyy.aria.core.inf.ITaskWrapper; import com.arialyy.aria.core.inf.TaskSchedulerType; import com.arialyy.aria.core.scheduler.ISchedulers; -import com.arialyy.aria.core.upload.UploadEntity; import com.arialyy.aria.exception.BaseException; import com.arialyy.aria.util.ALog; import com.arialyy.aria.util.CommonUtil; import com.arialyy.aria.util.ErrorHelp; -import com.arialyy.aria.util.RecordUtil; import java.lang.ref.WeakReference; public abstract class BaseListener, @@ -156,16 +152,6 @@ public abstract class BaseListener threadRecords; @Override protected void handleConvert() { diff --git a/Aria/src/main/java/com/arialyy/aria/core/common/ThreadRecord.java b/Aria/src/main/java/com/arialyy/aria/core/common/ThreadRecord.java index 7e408b00..ec29e4fd 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/common/ThreadRecord.java +++ b/Aria/src/main/java/com/arialyy/aria/core/common/ThreadRecord.java @@ -26,7 +26,7 @@ public class ThreadRecord extends DbEntity { /** * 任务的文件路径,不是当前线程记录的的分块文件路径 */ - public String key; + public String taskKey; /** * 开始位置 diff --git a/Aria/src/main/java/com/arialyy/aria/core/common/controller/FeatureController.java b/Aria/src/main/java/com/arialyy/aria/core/common/controller/FeatureController.java index a31aed3e..e2efa207 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/common/controller/FeatureController.java +++ b/Aria/src/main/java/com/arialyy/aria/core/common/controller/FeatureController.java @@ -24,12 +24,16 @@ import com.arialyy.aria.core.download.DGTaskWrapper; import com.arialyy.aria.core.download.DTaskWrapper; import com.arialyy.aria.core.inf.AbsEntity; import com.arialyy.aria.core.inf.AbsTaskWrapper; +import com.arialyy.aria.core.inf.ICheckEntityUtil; import com.arialyy.aria.core.inf.ITask; +import com.arialyy.aria.core.inf.ITaskWrapper; +import com.arialyy.aria.core.scheduler.DownloadGroupSchedulers; import com.arialyy.aria.core.scheduler.DownloadSchedulers; import com.arialyy.aria.core.scheduler.ISchedulers; import com.arialyy.aria.core.scheduler.UploadSchedulers; import com.arialyy.aria.core.upload.CheckUEntityUtil; import com.arialyy.aria.core.upload.UTaskWrapper; +import com.arialyy.aria.util.ALog; import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; @@ -57,7 +61,8 @@ public abstract class FeatureController { throw new IllegalArgumentException("对于不存在的任务(第一次下载),只能使用\"ControllerType.START_CONTROLLER\""); } if (wrapper.getEntity().getId() != -1 && clazz != ControllerType.NORMAL_CONTROLLER) { - throw new IllegalArgumentException("对于已存在的任务,只能使用\" ControllerType.NORMAL_CONTROLLER\""); + throw new IllegalArgumentException( + "对于已存在的任务,只能使用\" ControllerType.NORMAL_CONTROLLER\",请检查是否重复调用#start()方法"); } Class[] paramTypes = { AbsTaskWrapper.class }; @@ -119,27 +124,24 @@ public abstract class FeatureController { return UploadSchedulers.getInstance(); } if (mTaskWrapper instanceof DGTaskWrapper) { - return DownloadSchedulers.getInstance(); + return DownloadGroupSchedulers.getInstance(); } return null; } private boolean checkEntity() { + ICheckEntityUtil checkUtil = null; if (mTaskWrapper instanceof DTaskWrapper) { - return CheckDEntityUtil.newInstance((DTaskWrapper) mTaskWrapper).checkEntity(); - } - if (mTaskWrapper instanceof DGTaskWrapper) { - if (mTaskWrapper.getRequestType() == AbsTaskWrapper.D_FTP_DIR) { - return CheckFtpDirEntityUtil.newInstance((DGTaskWrapper) mTaskWrapper).checkEntity(); - } - - if (mTaskWrapper.getRequestType() == AbsTaskWrapper.DG_HTTP) { - return CheckDGEntityUtil.newInstance((DGTaskWrapper) mTaskWrapper).checkEntity(); + checkUtil = CheckDEntityUtil.newInstance((DTaskWrapper) mTaskWrapper); + } else if (mTaskWrapper instanceof DGTaskWrapper) { + if (mTaskWrapper.getRequestType() == ITaskWrapper.D_FTP_DIR) { + checkUtil = CheckFtpDirEntityUtil.newInstance((DGTaskWrapper) mTaskWrapper); + } else if (mTaskWrapper.getRequestType() == ITaskWrapper.DG_HTTP) { + checkUtil = CheckDGEntityUtil.newInstance((DGTaskWrapper) mTaskWrapper); } + } else if (mTaskWrapper instanceof UTaskWrapper) { + checkUtil = CheckUEntityUtil.newInstance((UTaskWrapper) mTaskWrapper); } - if (mTaskWrapper instanceof UTaskWrapper) { - return CheckUEntityUtil.newInstance((UTaskWrapper) mTaskWrapper).checkEntity(); - } - return false; + return checkUtil != null && checkUtil.checkEntity(); } } diff --git a/Aria/src/main/java/com/arialyy/aria/core/common/ftp/AbsFtpInfoThread.java b/Aria/src/main/java/com/arialyy/aria/core/common/ftp/AbsFtpInfoThread.java index 3ef7cf29..ad4d965f 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/common/ftp/AbsFtpInfoThread.java +++ b/Aria/src/main/java/com/arialyy/aria/core/common/ftp/AbsFtpInfoThread.java @@ -384,7 +384,7 @@ public abstract class AbsFtpInfoThread implements IConfi TAG = CommonUtil.getClassName(getClass()); mTarget = target; mWrapper = TaskWrapperManager.getInstance().getGroupWrapper(DGTaskWrapper.class, taskId); + if (taskId != -1 && mWrapper.getEntity().getId() == -1) { + mWrapper.setErrorEvent(new ErrorEvent(taskId, String.format("没有id为%s的任务", taskId))); + } mTarget.setTaskWrapper(mWrapper); if (getEntity() != null) { getTaskWrapper().setDirPathTemp(getEntity().getDirPath()); diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/CheckDEntityUtil.java b/Aria/src/main/java/com/arialyy/aria/core/download/CheckDEntityUtil.java index 0313842f..af37bde0 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/CheckDEntityUtil.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/CheckDEntityUtil.java @@ -17,6 +17,8 @@ package com.arialyy.aria.core.download; import android.text.TextUtils; import com.arialyy.aria.core.common.RecordHandler; +import com.arialyy.aria.core.download.m3u8.M3U8Entity; +import com.arialyy.aria.core.inf.ICheckEntityUtil; import com.arialyy.aria.core.inf.ITargetHandler; import com.arialyy.aria.core.inf.ITaskWrapper; import com.arialyy.aria.orm.DbEntity; @@ -28,7 +30,7 @@ import java.io.File; /** * 检查下载任务实体 */ -public class CheckDEntityUtil { +public class CheckDEntityUtil implements ICheckEntityUtil { private final String TAG = "CheckDLoadEntity"; private DTaskWrapper mWrapper; private DownloadEntity mEntity; @@ -42,7 +44,13 @@ public class CheckDEntityUtil { mEntity = mWrapper.getEntity(); } + @Override public boolean checkEntity() { + if (mWrapper.getErrorEvent() != null) { + ALog.e(TAG, mWrapper.getErrorEvent().errorMsg); + return false; + } + boolean b = checkFtps() && checkUrl() && checkFilePath(); if (b) { mEntity.save(); @@ -55,12 +63,19 @@ public class CheckDEntityUtil { } private void handleM3U8() { - File file = new File(mWrapper.getmTempFilePath()); + File file = new File(mWrapper.getTempFilePath()); // 缓存文件夹格式:问文件夹/.文件名_码率 String cacheDir = String.format("%s/.%s_%s", file.getParent(), file.getName(), mWrapper.asM3U8().getBandWidth()); mWrapper.asM3U8().setCacheDir(cacheDir); - mEntity.getM3U8Entity().setCacheDir(cacheDir); + M3U8Entity m3U8Entity = mEntity.getM3U8Entity(); + if (m3U8Entity == null) { + m3U8Entity = new M3U8Entity(); + m3U8Entity.setFilePath(mEntity.getFilePath()); + m3U8Entity.setPeerIndex(0); + m3U8Entity.setCacheDir(cacheDir); + m3U8Entity.insert(); + } if (mWrapper.getRequestType() == ITaskWrapper.M3U8_VOD) { if (mEntity.getFileSize() == 0) { ALog.w(TAG, @@ -80,7 +95,7 @@ public class CheckDEntityUtil { } private boolean checkFilePath() { - String filePath = mWrapper.getmTempFilePath(); + String filePath = mWrapper.getTempFilePath(); if (TextUtils.isEmpty(filePath)) { ALog.e(TAG, "下载失败,文件保存路径为null"); return false; @@ -124,7 +139,7 @@ public class CheckDEntityUtil { RecordUtil.delTaskRecord(filePath, RecordHandler.TYPE_DOWNLOAD); } } - File oldFile = new File(mEntity.getFilePath()); + File newFile = new File(filePath); mEntity.setFilePath(filePath); mEntity.setFileName(newFile.getName()); @@ -134,14 +149,17 @@ public class CheckDEntityUtil { || mWrapper.getRequestType() == ITaskWrapper.M3U8_LIVE) { return true; } - if (oldFile.exists()) { - // 处理普通任务的重命名 - RecordUtil.modifyTaskRecord(oldFile.getPath(), newFile.getPath()); - ALog.i(TAG, String.format("将任务重命名为:%s", newFile.getName())); - } else if (RecordUtil.blockTaskExists(oldFile.getPath())) { - // 处理分块任务的重命名 - RecordUtil.modifyTaskRecord(oldFile.getPath(), newFile.getPath()); - ALog.i(TAG, String.format("将分块任务重命名为:%s", newFile.getName())); + if (!TextUtils.isEmpty(mEntity.getFilePath())) { + File oldFile = new File(mEntity.getFilePath()); + if (oldFile.exists()) { + // 处理普通任务的重命名 + RecordUtil.modifyTaskRecord(oldFile.getPath(), newFile.getPath()); + ALog.i(TAG, String.format("将任务重命名为:%s", newFile.getName())); + } else if (RecordUtil.blockTaskExists(oldFile.getPath())) { + // 处理分块任务的重命名 + RecordUtil.modifyTaskRecord(oldFile.getPath(), newFile.getPath()); + ALog.i(TAG, String.format("将分块任务重命名为:%s", newFile.getName())); + } } } return true; @@ -161,14 +179,15 @@ public class CheckDEntityUtil { ALog.e(TAG, "下载失败,url【" + url + "】不合法"); return false; } - if (!TextUtils.isEmpty(mWrapper.getmTempUrl())) { - mEntity.setUrl(mWrapper.getmTempUrl()); + if (!TextUtils.isEmpty(mWrapper.getTempUrl())) { + mEntity.setUrl(mWrapper.getTempUrl()); } return true; } private boolean checkFtps() { - if (mWrapper.asFtp().getUrlEntity().isFtps) { + if (mWrapper.getRequestType() == ITaskWrapper.D_FTP && mWrapper.asFtp() + .getUrlEntity().isFtps) { if (TextUtils.isEmpty(mWrapper.asFtp().getUrlEntity().storePath)) { ALog.e(TAG, "证书路径为空"); return false; diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/CheckDGEntityUtil.java b/Aria/src/main/java/com/arialyy/aria/core/download/CheckDGEntityUtil.java index 166b0ef9..94d3fc8b 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/CheckDGEntityUtil.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/CheckDGEntityUtil.java @@ -17,6 +17,7 @@ package com.arialyy.aria.core.download; import android.text.TextUtils; import com.arialyy.aria.core.common.RequestEnum; +import com.arialyy.aria.core.inf.ICheckEntityUtil; import com.arialyy.aria.orm.DbEntity; import com.arialyy.aria.util.ALog; import com.arialyy.aria.util.CommonUtil; @@ -28,7 +29,7 @@ import java.util.HashSet; import java.util.List; import java.util.Set; -public class CheckDGEntityUtil { +public class CheckDGEntityUtil implements ICheckEntityUtil { private final String TAG = "CheckDGEntityUtil"; private DGTaskWrapper mWrapper; @@ -108,7 +109,13 @@ public class CheckDGEntityUtil { } } + @Override public boolean checkEntity() { + if (mWrapper.getErrorEvent() != null) { + ALog.e(TAG, mWrapper.getErrorEvent().errorMsg); + return false; + } + if (!checkDirPath()) { return false; } @@ -183,7 +190,6 @@ public class CheckDGEntityUtil { */ private boolean checkUrls() { if (mEntity.getUrls().isEmpty()) { - ALog.e(TAG, "下载失败,子任务下载列表为null"); return false; } diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/CheckFtpDirEntityUtil.java b/Aria/src/main/java/com/arialyy/aria/core/download/CheckFtpDirEntityUtil.java index 6d05fb10..3f82a400 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/CheckFtpDirEntityUtil.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/CheckFtpDirEntityUtil.java @@ -16,12 +16,12 @@ package com.arialyy.aria.core.download; import android.text.TextUtils; -import com.arialyy.aria.core.FtpUrlEntity; +import com.arialyy.aria.core.inf.ICheckEntityUtil; import com.arialyy.aria.orm.DbEntity; import com.arialyy.aria.util.ALog; import java.io.File; -public class CheckFtpDirEntityUtil { +public class CheckFtpDirEntityUtil implements ICheckEntityUtil { private final String TAG = "CheckFtpDirEntityUtil"; private DGTaskWrapper mWrapper; private DownloadGroupEntity mEntity; @@ -72,29 +72,16 @@ public class CheckFtpDirEntityUtil { return true; } + @Override public boolean checkEntity() { + if (mWrapper.getErrorEvent() != null) { + ALog.e(TAG, mWrapper.getErrorEvent().errorMsg); + return false; + } + boolean b = checkDirPath() && checkUrl(); if (b) { mEntity.save(); - if (mWrapper.getSubTaskWrapper() != null) { - //初始化子项的登录信息 - FtpUrlEntity tUrlEntity = mWrapper.asFtp().getUrlEntity(); - for (DTaskWrapper wrapper : mWrapper.getSubTaskWrapper()) { - FtpUrlEntity urlEntity = wrapper.asFtp().getUrlEntity(); - urlEntity.needLogin = tUrlEntity.needLogin; - urlEntity.account = tUrlEntity.account; - urlEntity.user = tUrlEntity.user; - urlEntity.password = tUrlEntity.password; - // 处理ftps详细 - if (tUrlEntity.isFtps) { - urlEntity.isFtps = true; - urlEntity.protocol = tUrlEntity.protocol; - urlEntity.storePath = tUrlEntity.storePath; - urlEntity.storePass = tUrlEntity.storePass; - urlEntity.keyAlias = tUrlEntity.keyAlias; - } - } - } } if (mWrapper.asFtp().getUrlEntity().isFtps) { if (TextUtils.isEmpty(mWrapper.asFtp().getUrlEntity().storePath)) { diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/DGTaskWrapper.java b/Aria/src/main/java/com/arialyy/aria/core/download/DGTaskWrapper.java index 4f9b28dd..9b4ca29e 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/DGTaskWrapper.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/DGTaskWrapper.java @@ -26,7 +26,7 @@ import java.util.List; */ public class DGTaskWrapper extends AbsGroupTaskWrapper { - private List subTaskEntities; + private List subWrappers; private boolean unknownSize = false; @@ -62,7 +62,7 @@ public class DGTaskWrapper extends AbsGroupTaskWrapper subTaskEntities) { - this.subTaskEntities = subTaskEntities; + this.subWrappers = subTaskEntities; } public boolean isUnknownSize() { @@ -82,6 +82,9 @@ public class DGTaskWrapper extends AbsGroupTaskWrapper getSubTaskWrapper() { - return subTaskEntities; + if (subWrappers == null) { + subWrappers = new ArrayList<>(); + } + return subWrappers; } } diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/DNormalConfigHandler.java b/Aria/src/main/java/com/arialyy/aria/core/download/DNormalConfigHandler.java index 0cf537d7..240d6136 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/DNormalConfigHandler.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/DNormalConfigHandler.java @@ -16,6 +16,7 @@ package com.arialyy.aria.core.download; import android.text.TextUtils; +import com.arialyy.aria.core.event.ErrorEvent; import com.arialyy.aria.core.inf.AbsTarget; import com.arialyy.aria.core.inf.IConfigHandler; import com.arialyy.aria.core.manager.TaskWrapperManager; @@ -49,12 +50,15 @@ class DNormalConfigHandler implements IConfigHandler { private void initTarget(long taskId, String targetName) { mWrapper = TaskWrapperManager.getInstance().getNormalTaskWrapper(DTaskWrapper.class, taskId); + if (taskId != -1 && mWrapper.getEntity().getId() == -1) { + mWrapper.setErrorEvent(new ErrorEvent(taskId, String.format("没有id为%s的任务", taskId))); + } mEntity = mWrapper.getEntity(); mTarget.setTargetName(targetName); mTarget.setTaskWrapper(mWrapper); if (mEntity != null) { - getWrapper().setmTempFilePath(mEntity.getFilePath()); + getWrapper().setTempFilePath(mEntity.getFilePath()); } } @@ -68,7 +72,7 @@ class DNormalConfigHandler implements IConfigHandler { return mTarget; } getWrapper().setRefreshInfo(true); - getWrapper().setmTempUrl(newUrl); + getWrapper().setTempUrl(newUrl); return mTarget; } @@ -98,7 +102,7 @@ class DNormalConfigHandler implements IConfigHandler { } void setTempFilePath(String tempFilePath) { - getWrapper().setmTempFilePath(tempFilePath); + getWrapper().setTempFilePath(tempFilePath); } private DTaskWrapper getWrapper() { diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/DTaskWrapper.java b/Aria/src/main/java/com/arialyy/aria/core/download/DTaskWrapper.java index 4cd5a439..f86fcb91 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/DTaskWrapper.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/DTaskWrapper.java @@ -93,19 +93,19 @@ public class DTaskWrapper extends AbsTaskWrapper { isGroupTask = groupTask; } - public String getmTempUrl() { + public String getTempUrl() { return mTempUrl; } - public void setmTempUrl(String mTempUrl) { + public void setTempUrl(String mTempUrl) { this.mTempUrl = mTempUrl; } - public String getmTempFilePath() { + public String getTempFilePath() { return mTempFilePath; } - public void setmTempFilePath(String mTempFilePath) { + public void setTempFilePath(String mTempFilePath) { this.mTempFilePath = mTempFilePath; } 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 fe1a1696..cac8a9f1 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadEntity.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/DownloadEntity.java @@ -21,7 +21,7 @@ import android.os.Parcelable; import android.text.TextUtils; import com.arialyy.aria.core.download.m3u8.M3U8Entity; import com.arialyy.aria.core.inf.AbsNormalEntity; -import com.arialyy.aria.core.inf.AbsTaskWrapper; +import com.arialyy.aria.core.inf.ITaskWrapper; import com.arialyy.aria.orm.DbEntity; import com.arialyy.aria.orm.annotation.Ignore; import com.arialyy.aria.orm.annotation.Unique; @@ -67,13 +67,6 @@ public class DownloadEntity extends AbsNormalEntity implements Parcelable { if (m3U8Entity == null) { m3U8Entity = DbEntity.findFirst(M3U8Entity.class, "filePath=?", downloadPath); } - if (m3U8Entity == null) { - m3U8Entity = new M3U8Entity(); - m3U8Entity.setFilePath(downloadPath); - m3U8Entity.setPeerIndex(0); - m3U8Entity.insert(); - } - return m3U8Entity; } @@ -82,7 +75,18 @@ public class DownloadEntity extends AbsNormalEntity implements Parcelable { } @Override public int getTaskType() { - return getUrl().startsWith("ftp") ? AbsTaskWrapper.D_FTP : AbsTaskWrapper.D_HTTP; + int type; + if (getUrl().startsWith("ftp")) { + type = ITaskWrapper.D_FTP; + } else { + M3U8Entity temp = getM3U8Entity(); + if (temp == null) { + type = ITaskWrapper.D_HTTP; + } else { + type = temp.isLive() ? ITaskWrapper.M3U8_LIVE : ITaskWrapper.M3U8_VOD; + } + } + return type; } 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 28e2bf6f..97662415 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 @@ -19,6 +19,7 @@ import android.os.Parcel; import com.arialyy.aria.core.inf.AbsGroupEntity; import com.arialyy.aria.core.inf.AbsTaskWrapper; import com.arialyy.aria.orm.annotation.Ignore; +import java.util.ArrayList; import java.util.List; /** @@ -32,6 +33,9 @@ public class DownloadGroupEntity extends AbsGroupEntity { * 子任务实体列表 */ public List getSubEntities() { + if (subEntities == null) { + subEntities = new ArrayList<>(); + } return subEntities; } diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadGroupTask.java b/Aria/src/main/java/com/arialyy/aria/core/download/DownloadGroupTask.java index a1612209..b4406e1e 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadGroupTask.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/DownloadGroupTask.java @@ -24,7 +24,7 @@ import com.arialyy.aria.core.download.group.DGroupUtil; import com.arialyy.aria.core.download.group.FtpDirDownloadUtil; import com.arialyy.aria.core.download.group.IDGroupListener; import com.arialyy.aria.core.inf.AbsGroupTask; -import com.arialyy.aria.core.inf.AbsTaskWrapper; +import com.arialyy.aria.core.inf.ITaskWrapper; import com.arialyy.aria.core.scheduler.ISchedulers; import com.arialyy.aria.util.CheckUtil; @@ -55,13 +55,14 @@ public class DownloadGroupTask extends AbsGroupTask { } @Override protected synchronized IUtil createUtil() { - switch (mTaskWrapper.getRequestType()) { - case AbsTaskWrapper.D_HTTP: - return new DGroupUtil((IDGroupListener) mListener, mTaskWrapper); - case AbsTaskWrapper.D_FTP_DIR: - return new FtpDirDownloadUtil((IDGroupListener) mListener, mTaskWrapper); + int taskType = mTaskWrapper.getRequestType(); + if (taskType == ITaskWrapper.DG_HTTP) { + return new DGroupUtil((IDGroupListener) mListener, mTaskWrapper); + } else if (taskType == ITaskWrapper.D_FTP_DIR) { + return new FtpDirDownloadUtil((IDGroupListener) mListener, mTaskWrapper); + } else { + throw new IllegalArgumentException(String.format("不识别的任务, 任务类型:%s", taskType)); } - return null; } public static class Builder { 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 48429040..abee2b4f 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 @@ -95,12 +95,15 @@ public class DownloadTask extends AbsNormalTask { } @Override protected synchronized IUtil createUtil() { - if (mTaskWrapper.getRequestType() == ITaskWrapper.M3U8_VOD) { + int taskType = mTaskWrapper.getRequestType(); + if (taskType == ITaskWrapper.M3U8_VOD) { return new M3U8VodUtil(mTaskWrapper, (M3U8Listener) mListener); - } else if (mTaskWrapper.getRequestType() == ITaskWrapper.M3U8_LIVE) { + } else if (taskType == ITaskWrapper.M3U8_LIVE) { return new M3U8LiveUtil(mTaskWrapper, (M3U8Listener) mListener); - } else { + } else if (taskType == ITaskWrapper.D_HTTP || taskType == ITaskWrapper.D_FTP) { return new SimpleDownloadUtil(mTaskWrapper, (IDownloadListener) mListener); + } else { + throw new IllegalArgumentException(String.format("不识别的任务, 任务类型:%s", taskType)); } } diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/FtpDirConfigHandler.java b/Aria/src/main/java/com/arialyy/aria/core/download/FtpDirConfigHandler.java index 96826939..1e9c3548 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/FtpDirConfigHandler.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/FtpDirConfigHandler.java @@ -17,6 +17,8 @@ package com.arialyy.aria.core.download; import com.arialyy.aria.core.inf.AbsTarget; import com.arialyy.aria.core.inf.AbsTaskWrapper; +import com.arialyy.aria.core.inf.ITaskWrapper; +import java.util.List; /** * Created by lyy on 2017/4/9. @@ -26,6 +28,16 @@ class FtpDirConfigHandler extends AbsGroupConfigHandle FtpDirConfigHandler(TARGET target, long taskId) { super(target, taskId); + init(); + } + + private void init() { getTaskWrapper().setRequestType(AbsTaskWrapper.D_FTP_DIR); + List wrappers = getTaskWrapper().getSubTaskWrapper(); + if (!wrappers.isEmpty()) { + for (DTaskWrapper subWrapper : wrappers) { + subWrapper.setRequestType(ITaskWrapper.D_FTP); + } + } } } diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/FtpNormalTarget.java b/Aria/src/main/java/com/arialyy/aria/core/download/FtpNormalTarget.java index c7ce4410..291ffd2b 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/FtpNormalTarget.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/FtpNormalTarget.java @@ -20,7 +20,7 @@ import android.support.annotation.NonNull; import com.arialyy.aria.core.common.AbsNormalTarget; import com.arialyy.aria.core.common.Suggest; import com.arialyy.aria.core.common.ftp.FtpDelegate; -import com.arialyy.aria.core.inf.AbsTaskWrapper; +import com.arialyy.aria.core.inf.ITaskWrapper; import com.arialyy.aria.util.CommonUtil; /** @@ -32,8 +32,8 @@ public class FtpNormalTarget extends AbsNormalTarget { FtpNormalTarget(long taskId, String targetName) { mConfigHandler = new DNormalConfigHandler<>(this, taskId, targetName); - getTaskWrapper().asFtp().setUrlEntity(CommonUtil.getFtpUrlInfo(mConfigHandler.getUrl())); - getTaskWrapper().setRequestType(AbsTaskWrapper.D_FTP); + getTaskWrapper().asFtp().setUrlEntity(CommonUtil.getFtpUrlInfo(getEntity().getUrl())); + getTaskWrapper().setRequestType(ITaskWrapper.D_FTP); } /** diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/FtpStartTarget.java b/Aria/src/main/java/com/arialyy/aria/core/download/FtpStartTarget.java index fe7077a4..cad74ed1 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/FtpStartTarget.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/FtpStartTarget.java @@ -21,6 +21,7 @@ import com.arialyy.aria.core.common.AbsStartTarget; import com.arialyy.aria.core.common.Suggest; import com.arialyy.aria.core.common.ftp.FtpDelegate; import com.arialyy.aria.core.inf.AbsTaskWrapper; +import com.arialyy.aria.core.inf.ITaskWrapper; import com.arialyy.aria.util.CommonUtil; /** @@ -33,7 +34,7 @@ public class FtpStartTarget extends AbsStartTarget { mConfigHandler = new DNormalConfigHandler<>(this, -1, targetName); mConfigHandler.setUrl(url); getTaskWrapper().asFtp().setUrlEntity(CommonUtil.getFtpUrlInfo(url)); - getTaskWrapper().setRequestType(AbsTaskWrapper.D_FTP); + getTaskWrapper().setRequestType(ITaskWrapper.D_FTP); } /** diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/GroupNormalTarget.java b/Aria/src/main/java/com/arialyy/aria/core/download/GroupNormalTarget.java index 1062e1e9..a227f7ce 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/GroupNormalTarget.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/GroupNormalTarget.java @@ -19,6 +19,7 @@ import android.support.annotation.CheckResult; import com.arialyy.aria.core.common.AbsNormalTarget; import com.arialyy.aria.core.common.Suggest; import com.arialyy.aria.core.common.http.HttpDelegate; +import com.arialyy.aria.core.inf.ITaskWrapper; import com.arialyy.aria.core.manager.SubTaskManager; import java.util.List; @@ -32,6 +33,7 @@ public class GroupNormalTarget extends AbsNormalTarget { GroupNormalTarget(long taskId, String targetName) { setTargetName(targetName); mConfigHandler = new HttpGroupConfigHandler<>(this, taskId); + getTaskWrapper().setRequestType(ITaskWrapper.DG_HTTP); } /** diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/GroupStartTarget.java b/Aria/src/main/java/com/arialyy/aria/core/download/GroupStartTarget.java index a5d20c01..55a32d86 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/GroupStartTarget.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/GroupStartTarget.java @@ -20,6 +20,7 @@ import com.arialyy.aria.core.common.AbsStartTarget; import com.arialyy.aria.core.common.Suggest; import com.arialyy.aria.core.common.http.HttpDelegate; import com.arialyy.aria.core.inf.IHttpFileLenAdapter; +import com.arialyy.aria.core.inf.ITaskWrapper; import com.arialyy.aria.core.manager.SubTaskManager; import com.arialyy.aria.util.ALog; import java.util.List; @@ -34,6 +35,7 @@ public class GroupStartTarget extends AbsStartTarget { GroupStartTarget(List urls, String targetName) { setTargetName(targetName); mConfigHandler = new HttpGroupConfigHandler<>(this, -1); + getTaskWrapper().setRequestType(ITaskWrapper.DG_HTTP); mConfigHandler.setGroupUrl(urls); } diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/HttpGroupConfigHandler.java b/Aria/src/main/java/com/arialyy/aria/core/download/HttpGroupConfigHandler.java index daf9851b..01067365 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/HttpGroupConfigHandler.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/HttpGroupConfigHandler.java @@ -17,6 +17,7 @@ package com.arialyy.aria.core.download; import android.support.annotation.CheckResult; import com.arialyy.aria.core.inf.AbsTarget; +import com.arialyy.aria.orm.DbEntity; import com.arialyy.aria.util.ALog; import com.arialyy.aria.util.CommonUtil; import com.arialyy.aria.util.DbDataHelper; @@ -53,7 +54,14 @@ class HttpGroupConfigHandler extends AbsGroupConfigHan mUrls.addAll(urls); String groupHash = CommonUtil.getMd5Code(urls); getEntity().setGroupHash(groupHash); - getEntity().setSubEntities(DbDataHelper.createHttpSubTask(groupHash, mUrls)); + List subEntities = DbDataHelper.createHttpSubTask(groupHash, mUrls); + List wrappers = new ArrayList<>(); + for (DownloadEntity subEntity : subEntities) { + wrappers.add(new DTaskWrapper(subEntity)); + } + getEntity().setUrls(urls); + getEntity().setSubEntities(subEntities); + getTaskWrapper().setSubTaskWrapper(wrappers); } /** @@ -91,13 +99,17 @@ class HttpGroupConfigHandler extends AbsGroupConfigHan mUrls.clear(); mUrls.addAll(urls); String newHash = CommonUtil.getMd5Code(urls); + getEntity().setUrls(mUrls); getEntity().setGroupHash(newHash); getEntity().update(); if (getEntity().getSubEntities() != null && !getEntity().getSubEntities().isEmpty()) { + int i = 0; for (DownloadEntity de : getEntity().getSubEntities()) { + de.setUrl(mUrls.get(i)); de.setGroupHash(newHash); - de.update(); + i++; } + DbEntity.updateManyData(getEntity().getSubEntities()); } return getTarget(); } diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/HttpNormalTarget.java b/Aria/src/main/java/com/arialyy/aria/core/download/HttpNormalTarget.java index ee83c106..2ed1cfc8 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/HttpNormalTarget.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/HttpNormalTarget.java @@ -30,6 +30,7 @@ public class HttpNormalTarget extends AbsNormalTarget { HttpNormalTarget(long taskId, String targetName) { mConfigHandler = new DNormalConfigHandler<>(this, taskId, targetName); + getTaskWrapper().setRequestType(getTaskWrapper().getEntity().getTaskType()); } @CheckResult(suggest = Suggest.TASK_CONTROLLER) diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/HttpStartTarget.java b/Aria/src/main/java/com/arialyy/aria/core/download/HttpStartTarget.java index 761b0b34..6807942c 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/HttpStartTarget.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/HttpStartTarget.java @@ -22,6 +22,7 @@ import com.arialyy.aria.core.common.Suggest; import com.arialyy.aria.core.common.http.HttpDelegate; import com.arialyy.aria.core.download.m3u8.M3U8Delegate; import com.arialyy.aria.core.inf.IHttpFileLenAdapter; +import com.arialyy.aria.core.inf.ITaskWrapper; public class HttpStartTarget extends AbsStartTarget { @@ -29,6 +30,7 @@ public class HttpStartTarget extends AbsStartTarget { HttpStartTarget(String url, String targetName) { mConfigHandler = new DNormalConfigHandler<>(this, -1, targetName); + getTaskWrapper().setRequestType(ITaskWrapper.D_HTTP); mConfigHandler.setUrl(url); } 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 c015b9b3..3fb04c6c 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 @@ -19,12 +19,12 @@ import com.arialyy.aria.core.common.AbsThreadTask; import com.arialyy.aria.core.common.NormalFileer; import com.arialyy.aria.core.common.RecordHandler; import com.arialyy.aria.core.common.SubThreadConfig; -import com.arialyy.aria.core.download.DownloadEntity; import com.arialyy.aria.core.download.DTaskWrapper; +import com.arialyy.aria.core.download.DownloadEntity; import com.arialyy.aria.core.event.Event; import com.arialyy.aria.core.event.SpeedEvent; -import com.arialyy.aria.core.inf.AbsTaskWrapper; import com.arialyy.aria.core.inf.IDownloadListener; +import com.arialyy.aria.core.inf.ITaskWrapper; import com.arialyy.aria.exception.BaseException; import com.arialyy.aria.exception.TaskException; import com.arialyy.aria.util.ALog; @@ -113,10 +113,10 @@ public class Downloader extends NormalFileer { @Override protected AbsThreadTask selectThreadTask(SubThreadConfig config) { switch (mTaskWrapper.getRequestType()) { - case AbsTaskWrapper.D_FTP: - case AbsTaskWrapper.D_FTP_DIR: + case ITaskWrapper.D_FTP: + case ITaskWrapper.D_FTP_DIR: return new FtpThreadTask(config); - case AbsTaskWrapper.D_HTTP: + case ITaskWrapper.D_HTTP: return new HttpThreadTask(config); } return null; diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/HttpFileInfoThread.java b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/HttpFileInfoThread.java index 3bd63673..40e1b13b 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/HttpFileInfoThread.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/HttpFileInfoThread.java @@ -16,15 +16,16 @@ package com.arialyy.aria.core.download.downloader; import android.net.TrafficStats; +import android.net.Uri; import android.os.Process; import android.text.TextUtils; import com.arialyy.aria.core.AriaManager; import com.arialyy.aria.core.common.CompleteInfo; import com.arialyy.aria.core.common.OnFileInfoCallback; import com.arialyy.aria.core.common.RequestEnum; +import com.arialyy.aria.core.common.http.HttpTaskConfig; import com.arialyy.aria.core.download.DTaskWrapper; import com.arialyy.aria.core.download.DownloadEntity; -import com.arialyy.aria.core.common.http.HttpTaskConfig; import com.arialyy.aria.core.inf.IHttpFileLenAdapter; import com.arialyy.aria.exception.AriaIOException; import com.arialyy.aria.exception.BaseException; @@ -54,7 +55,7 @@ import java.util.UUID; * 下载文件信息获取 */ public class HttpFileInfoThread implements Runnable { - private final String TAG = "HttpFileInfoThread"; + private static final String TAG = "HttpFileInfoThread"; private DownloadEntity mEntity; private DTaskWrapper mTaskWrapper; private int mConnectTimeOut; @@ -83,6 +84,7 @@ public class HttpFileInfoThread implements Runnable { conn.connect(); handleConnect(conn); } catch (IOException e) { + e.printStackTrace(); failDownload(new AriaIOException(TAG, String.format("下载失败,filePath: %s, url: %s", mEntity.getDownloadPath(), mEntity.getUrl())), true); @@ -119,7 +121,7 @@ public class HttpFileInfoThread implements Runnable { } long len = lenAdapter.handleFileLen(conn.getHeaderFields()); - if (!CommonUtil.checkSDMemorySpace(mEntity.getDownloadPath(), len)) { + if (!CommonUtil.checkSDMemorySpace(mEntity.getFilePath(), len)) { failDownload(new TaskException(TAG, String.format("下载失败,内存空间不足;filePath: %s, url: %s", mEntity.getDownloadPath(), mEntity.getUrl())), false); @@ -284,13 +286,17 @@ public class HttpFileInfoThread implements Runnable { */ private void handleUrlReTurn(HttpURLConnection conn, String newUrl) throws IOException { ALog.d(TAG, "30x跳转,新url为【" + newUrl + "】"); - if (TextUtils.isEmpty(newUrl) || newUrl.equalsIgnoreCase("null") || !newUrl.startsWith( - "http")) { + if (TextUtils.isEmpty(newUrl) || newUrl.equalsIgnoreCase("null")) { if (onFileInfoCallback != null) { onFileInfoCallback.onFail(mEntity, new TaskException(TAG, "获取重定向链接失败"), false); } return; } + if (newUrl.startsWith("/")) { + Uri uri = Uri.parse(mEntity.getUrl()); + newUrl = uri.getHost() + newUrl; + } + if (!CheckUtil.checkUrlNotThrow(newUrl)) { failDownload(new TaskException(TAG, "下载失败,重定向url错误"), false); return; @@ -334,6 +340,10 @@ public class HttpFileInfoThread implements Runnable { private static class FileLenAdapter implements IHttpFileLenAdapter { @Override public long handleFileLen(Map> headers) { + if (headers == null || headers.isEmpty()) { + ALog.e(TAG, "header为空,获取文件长度失败"); + return -1; + } List sLength = headers.get("Content-Length"); if (sLength == null || sLength.isEmpty()) { return -1; diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/SimpleDownloadUtil.java b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/SimpleDownloadUtil.java index e3d2333f..39591639 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/SimpleDownloadUtil.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/SimpleDownloadUtil.java @@ -21,8 +21,8 @@ import com.arialyy.aria.core.common.IUtil; import com.arialyy.aria.core.common.OnFileInfoCallback; import com.arialyy.aria.core.download.DTaskWrapper; import com.arialyy.aria.core.inf.AbsEntity; -import com.arialyy.aria.core.inf.AbsTaskWrapper; import com.arialyy.aria.core.inf.IDownloadListener; +import com.arialyy.aria.core.inf.ITaskWrapper; import com.arialyy.aria.exception.BaseException; /** @@ -110,7 +110,7 @@ public class SimpleDownloadUtil implements IUtil { */ private Runnable createInfoThread() { switch (mTaskWrapper.getRequestType()) { - case AbsTaskWrapper.D_FTP: + case ITaskWrapper.D_FTP: return new FtpFileInfoThread(mTaskWrapper, new OnFileInfoCallback() { @Override public void onComplete(String url, CompleteInfo info) { mDownloader.updateTempFile(); @@ -122,7 +122,7 @@ public class SimpleDownloadUtil implements IUtil { mDownloader.closeTimer(); } }); - case AbsTaskWrapper.D_HTTP: + case ITaskWrapper.D_HTTP: return new HttpFileInfoThread(mTaskWrapper, new OnFileInfoCallback() { @Override public void onComplete(String url, CompleteInfo info) { mDownloader.updateTempFile(); diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/group/AbsGroupUtil.java b/Aria/src/main/java/com/arialyy/aria/core/download/group/AbsGroupUtil.java index 4ae6ac2e..e97d89ef 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/group/AbsGroupUtil.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/group/AbsGroupUtil.java @@ -32,7 +32,7 @@ import java.util.concurrent.TimeUnit; * Created by AriaL on 2017/6/30. * 任务组核心逻辑 */ -public abstract class AbsGroupUtil implements IUtil, Runnable { +public abstract class AbsGroupUtil implements IUtil { private final String TAG = "AbsGroupUtil"; /** * FTP文件夹 @@ -59,16 +59,16 @@ public abstract class AbsGroupUtil implements IUtil, Runnable { mListener = listener; mGTWrapper = groupWrapper; mUpdateInterval = Configuration.getInstance().downloadCfg.getUpdateInterval(); - mState = new GroupRunState(groupWrapper.getKey(), mListener, - groupWrapper.getSubTaskWrapper().size(), mSubQueue); - mScheduler = new Handler(Looper.getMainLooper(), SimpleSchedulers.newInstance(mState)); - initState(); } /** * 初始化组合任务状态 */ - private void initState() { + void initState() { + mState = + new GroupRunState(mGTWrapper.getKey(), mListener, mGTWrapper.getSubTaskWrapper().size(), + mSubQueue); + mScheduler = new Handler(Looper.getMainLooper(), SimpleSchedulers.newInstance(mState)); for (DTaskWrapper wrapper : mGTWrapper.getSubTaskWrapper()) { if (wrapper.getEntity().getState() == IEntity.STATE_COMPLETE) { mState.updateCompleteNum(); @@ -164,7 +164,7 @@ public abstract class AbsGroupUtil implements IUtil, Runnable { } @Override public boolean isRunning() { - return mState.isRunning; + return mState != null && mState.isRunning; } @Override public void cancel() { @@ -203,19 +203,15 @@ public abstract class AbsGroupUtil implements IUtil, Runnable { } @Override public void start() { - new Thread(this).start(); - } - - @Override public void run() { if (isStop || isCancel) { closeTimer(); return; } - onStart(); + onPreStart(); startRunningFlow(); } - protected void onStart() { + protected void onPreStart() { } diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/group/ChildDLoadListener.java b/Aria/src/main/java/com/arialyy/aria/core/download/group/ChildDLoadListener.java index ad4d60e6..4cd0ec03 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/group/ChildDLoadListener.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/group/ChildDLoadListener.java @@ -21,6 +21,7 @@ import com.arialyy.aria.core.inf.IDownloadListener; import com.arialyy.aria.core.inf.IEntity; import com.arialyy.aria.core.scheduler.ISchedulers; import com.arialyy.aria.exception.BaseException; +import com.arialyy.aria.util.ALog; import com.arialyy.aria.util.CommonUtil; /** @@ -48,25 +49,25 @@ class ChildDLoadListener implements IDownloadListener { } @Override public void onPre() { - saveData(IEntity.STATE_PRE, -1); + saveData(IEntity.STATE_PRE, subEntity.getCurrentProgress()); } @Override public void onPostPre(long fileSize) { subEntity.setFileSize(fileSize); subEntity.setConvertFileSize(CommonUtil.formatFileSize(fileSize)); - saveData(IEntity.STATE_POST_PRE, -1); + saveData(IEntity.STATE_POST_PRE, subEntity.getCurrentProgress()); sendToTarget(ISchedulers.POST_PRE, loader); } @Override public void onResume(long resumeLocation) { lastLen = resumeLocation; - saveData(IEntity.STATE_POST_PRE, IEntity.STATE_RUNNING); + saveData(IEntity.STATE_POST_PRE, subEntity.getCurrentProgress()); sendToTarget(ISchedulers.START, loader); } @Override public void onStart(long startLocation) { lastLen = startLocation; - saveData(IEntity.STATE_POST_PRE, IEntity.STATE_RUNNING); + saveData(IEntity.STATE_POST_PRE, subEntity.getCurrentProgress()); sendToTarget(ISchedulers.START, loader); } @@ -106,7 +107,7 @@ class ChildDLoadListener implements IDownloadListener { @Override public void onFail(boolean needRetry, BaseException e) { subEntity.setFailNum(subEntity.getFailNum() + 1); - saveData(IEntity.STATE_FAIL, lastLen); + saveData(IEntity.STATE_FAIL, subEntity.getCurrentProgress()); handleSpeed(0); sendToTarget(ISchedulers.FAIL, loader); } diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/group/DGroupUtil.java b/Aria/src/main/java/com/arialyy/aria/core/download/group/DGroupUtil.java index 0c124366..3595fde2 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/group/DGroupUtil.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/group/DGroupUtil.java @@ -68,8 +68,9 @@ public class DGroupUtil extends AbsGroupUtil implements IUtil { return false; } - @Override protected void onStart() { - super.onStart(); + @Override protected void onPreStart() { + super.onPreStart(); + initState(); if (mState.getCompleteNum() == mState.getSubSize()) { mListener.onComplete(); } else { diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/group/FtpDirDownloadUtil.java b/Aria/src/main/java/com/arialyy/aria/core/download/group/FtpDirDownloadUtil.java index 7d1d54ba..8fdf2892 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/group/FtpDirDownloadUtil.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/group/FtpDirDownloadUtil.java @@ -15,6 +15,9 @@ */ package com.arialyy.aria.core.download.group; +import android.net.Uri; +import android.text.TextUtils; +import com.arialyy.aria.core.FtpUrlEntity; import com.arialyy.aria.core.common.CompleteInfo; import com.arialyy.aria.core.common.OnFileInfoCallback; import com.arialyy.aria.core.download.DGTaskWrapper; @@ -22,6 +25,8 @@ import com.arialyy.aria.core.download.DTaskWrapper; import com.arialyy.aria.core.inf.AbsEntity; import com.arialyy.aria.core.inf.IEntity; import com.arialyy.aria.exception.BaseException; +import java.util.concurrent.locks.Condition; +import java.util.concurrent.locks.ReentrantLock; /** * Created by Aria.Lao on 2017/7/27. @@ -29,6 +34,8 @@ import com.arialyy.aria.exception.BaseException; */ public class FtpDirDownloadUtil extends AbsGroupUtil { private String TAG = "FtpDirDownloadUtil"; + private ReentrantLock LOCK = new ReentrantLock(); + private Condition condition = LOCK.newCondition(); public FtpDirDownloadUtil(IDGroupListener listener, DGTaskWrapper taskEntity) { super(listener, taskEntity); @@ -38,16 +45,15 @@ public class FtpDirDownloadUtil extends AbsGroupUtil { return FTP_DIR; } - @Override protected void onStart() { - super.onStart(); + @Override protected void onPreStart() { + super.onPreStart(); if (mGTWrapper.getEntity().getFileSize() > 1) { - startDownload(); + startDownload(true); } else { - FtpDirInfoThread infoThread = new FtpDirInfoThread(mGTWrapper, new OnFileInfoCallback() { @Override public void onComplete(String url, CompleteInfo info) { if (info.code >= 200 && info.code < 300) { - startDownload(); + startDownload(false); } } @@ -56,14 +62,46 @@ public class FtpDirDownloadUtil extends AbsGroupUtil { } }); new Thread(infoThread).start(); + try { + LOCK.lock(); + condition.await(); + } catch (InterruptedException e) { + e.printStackTrace(); + } finally { + LOCK.unlock(); + } } } - private void startDownload() { + /** + * @param needCloneInfo 第一次下载,信息已经在{@link FtpDirInfoThread}中clone了 + */ + private void startDownload(boolean needCloneInfo) { + try { + LOCK.lock(); + condition.signalAll(); + } finally { + LOCK.unlock(); + } + initState(); for (DTaskWrapper wrapper : mGTWrapper.getSubTaskWrapper()) { + if (needCloneInfo) { + cloneInfo(wrapper); + } if (wrapper.getState() != IEntity.STATE_COMPLETE) { createAndStartSubLoader(wrapper); } } } + + private void cloneInfo(DTaskWrapper subWrapper) { + FtpUrlEntity urlEntity = mGTWrapper.asFtp().getUrlEntity().clone(); + Uri uri = Uri.parse(subWrapper.getEntity().getUrl()); + String remotePath = uri.getPath(); + urlEntity.remotePath = TextUtils.isEmpty(remotePath) ? "/" : remotePath; + + subWrapper.asFtp().setUrlEntity(urlEntity); + subWrapper.asFtp().setCharSet(mGTWrapper.asFtp().getCharSet()); + subWrapper.asFtp().setProxy(mGTWrapper.asFtp().getProxy()); + } } diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/group/FtpDirInfoThread.java b/Aria/src/main/java/com/arialyy/aria/core/download/group/FtpDirInfoThread.java index d88eb05c..3fa05f97 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/group/FtpDirInfoThread.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/group/FtpDirInfoThread.java @@ -25,7 +25,9 @@ import com.arialyy.aria.core.download.DTaskWrapper; import com.arialyy.aria.core.download.DownloadEntity; import com.arialyy.aria.core.download.DownloadGroupEntity; import com.arialyy.aria.core.inf.AbsTaskWrapper; +import com.arialyy.aria.exception.BaseException; import com.arialyy.aria.util.CommonUtil; +import com.arialyy.aria.util.RecordUtil; import java.nio.charset.Charset; import java.util.ArrayList; @@ -94,4 +96,9 @@ class FtpDirInfoThread extends AbsFtpInfoThread mergeHandler; + private ITsMergeHandler mergeHandler; /** * 已完成的ts分片数量 @@ -147,7 +147,7 @@ public class M3U8TaskConfig { } public IBandWidthUrlConverter getBandWidthUrlConverter() { - return bandWidthUrlConverter.get(); + return bandWidthUrlConverter == null ? null : bandWidthUrlConverter.get(); } public void setBandWidthUrlConverter(IBandWidthUrlConverter bandWidthUrlConverter) { @@ -187,11 +187,11 @@ public class M3U8TaskConfig { } public ITsMergeHandler getMergeHandler() { - return mergeHandler.get(); + return mergeHandler; } public void setMergeHandler(ITsMergeHandler mergeHandler) { - this.mergeHandler = new WeakReference<>(mergeHandler); + this.mergeHandler = mergeHandler; } public IVodTsUrlConverter getVodUrlConverter() { diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/m3u8/M3U8VodLoader.java b/Aria/src/main/java/com/arialyy/aria/core/download/m3u8/M3U8VodLoader.java index af375b79..a4ecced1 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/m3u8/M3U8VodLoader.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/m3u8/M3U8VodLoader.java @@ -619,6 +619,10 @@ public class M3U8VodLoader extends BaseM3U8Loader { boolean isSuccess; if (mergeHandler != null) { isSuccess = mergeHandler.merge(mTaskWrapper.asM3U8().getKeyInfo(), partPath); + + if (mergeHandler.getClass().isAnonymousClass()) { + mTaskWrapper.asM3U8().setMergeHandler(null); + } } else { isSuccess = FileUtil.mergeFile(taskRecord.filePath, partPath); } diff --git a/Aria/src/main/java/com/arialyy/aria/core/event/ErrorEvent.java b/Aria/src/main/java/com/arialyy/aria/core/event/ErrorEvent.java new file mode 100644 index 00000000..82124c9d --- /dev/null +++ b/Aria/src/main/java/com/arialyy/aria/core/event/ErrorEvent.java @@ -0,0 +1,26 @@ +/* + * 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.event; + +public class ErrorEvent { + public long taskId; + public String errorMsg; + + public ErrorEvent(long taskId, String errorMsg) { + this.taskId = taskId; + this.errorMsg = errorMsg; + } +} 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 b4e25d02..f7ea95fd 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 @@ -191,8 +191,8 @@ public abstract class AbsEntity extends DbEntity implements IEntity, Parcelable, /** * 实体驱动的下载任务类型 * - * @return {@link AbsTaskWrapper#D_FTP}、{@link AbsTaskWrapper#D_FTP_DIR}、{@link - * AbsTaskWrapper#U_HTTP}... + * @return {@link ITaskWrapper#D_FTP}、{@link ITaskWrapper#D_FTP_DIR}、{@link + * ITaskWrapper#U_HTTP}... */ public abstract int getTaskType(); 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 5324435d..c35381d9 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 @@ -26,7 +26,8 @@ import java.util.List; */ public abstract class AbsGroupEntity extends AbsEntity implements Parcelable { /** - * 为子任务地址相加的url的Md5 + * 组合任务等hash为: 为子任务地址相加的url的Md5 + * ftpdir为:ftpdir下载地址 */ @Unique protected String groupHash; diff --git a/Aria/src/main/java/com/arialyy/aria/core/inf/AbsGroupTaskWrapper.java b/Aria/src/main/java/com/arialyy/aria/core/inf/AbsGroupTaskWrapper.java index 321e6964..fe6f199c 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/inf/AbsGroupTaskWrapper.java +++ b/Aria/src/main/java/com/arialyy/aria/core/inf/AbsGroupTaskWrapper.java @@ -16,6 +16,7 @@ package com.arialyy.aria.core.inf; import com.arialyy.aria.core.download.DownloadGroupEntity; +import com.arialyy.aria.core.event.ErrorEvent; import java.util.List; /** @@ -24,6 +25,19 @@ import java.util.List; public abstract class AbsGroupTaskWrapper extends AbsTaskWrapper { + /** + * 错误信息 + */ + private ErrorEvent errorEvent; + + public ErrorEvent getErrorEvent() { + return errorEvent; + } + + public void setErrorEvent(ErrorEvent errorEvent) { + this.errorEvent = errorEvent; + } + public AbsGroupTaskWrapper(ENTITY entity) { super(entity); } diff --git a/Aria/src/main/java/com/arialyy/aria/core/inf/AbsTaskWrapper.java b/Aria/src/main/java/com/arialyy/aria/core/inf/AbsTaskWrapper.java index 42a5625e..5d41213e 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/inf/AbsTaskWrapper.java +++ b/Aria/src/main/java/com/arialyy/aria/core/inf/AbsTaskWrapper.java @@ -23,6 +23,7 @@ import com.arialyy.aria.core.config.DownloadConfig; import com.arialyy.aria.core.config.UploadConfig; import com.arialyy.aria.core.download.DownloadEntity; import com.arialyy.aria.core.download.DownloadGroupEntity; +import com.arialyy.aria.core.event.ErrorEvent; import com.arialyy.aria.core.upload.UploadEntity; /** @@ -45,8 +46,8 @@ public abstract class AbsTaskWrapper private boolean isNewTask = false; /** - * 请求类型 {@link AbsTaskWrapper#D_HTTP}、{@link AbsTaskWrapper#D_FTP}、{@link - * AbsTaskWrapper#D_FTP_DIR}。。。 + * 请求类型 {@link ITaskWrapper#D_HTTP}、{@link ITaskWrapper#D_FTP}、{@link + * ITaskWrapper#D_FTP_DIR}。。。 */ private int requestType = D_HTTP; @@ -71,10 +72,23 @@ public abstract class AbsTaskWrapper */ private ENTITY entity; + /** + * 错误信息 + */ + private ErrorEvent errorEvent; + public AbsTaskWrapper(ENTITY entity) { this.entity = entity; } + public ErrorEvent getErrorEvent() { + return errorEvent; + } + + public void setErrorEvent(ErrorEvent errorEvent) { + this.errorEvent = errorEvent; + } + /** * 任务识别标志 {@link AbsEntity#getKey()} */ diff --git a/Aria/src/main/java/com/arialyy/aria/core/inf/ICheckEntityUtil.java b/Aria/src/main/java/com/arialyy/aria/core/inf/ICheckEntityUtil.java new file mode 100644 index 00000000..6f164169 --- /dev/null +++ b/Aria/src/main/java/com/arialyy/aria/core/inf/ICheckEntityUtil.java @@ -0,0 +1,26 @@ +/* + * 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.inf; + +public interface ICheckEntityUtil { + + /** + * 检查实体对象 + * + * @return true 成功;false 失败 + */ + boolean checkEntity(); +} diff --git a/Aria/src/main/java/com/arialyy/aria/core/inf/ITaskWrapper.java b/Aria/src/main/java/com/arialyy/aria/core/inf/ITaskWrapper.java index a99b8707..701247fa 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/inf/ITaskWrapper.java +++ b/Aria/src/main/java/com/arialyy/aria/core/inf/ITaskWrapper.java @@ -22,6 +22,8 @@ import com.arialyy.aria.core.download.DownloadGroupEntity; */ public interface ITaskWrapper { + int DEFAULT = 0; + /** * HTTP单任务载 */ diff --git a/Aria/src/main/java/com/arialyy/aria/core/manager/DGTaskWrapperFactory.java b/Aria/src/main/java/com/arialyy/aria/core/manager/DGTaskWrapperFactory.java index f503cd61..efca22d4 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/manager/DGTaskWrapperFactory.java +++ b/Aria/src/main/java/com/arialyy/aria/core/manager/DGTaskWrapperFactory.java @@ -53,20 +53,6 @@ class DGTaskWrapperFactory implements IGroupWrapperFactory()); - // } - // if (fte.getSubTaskWrapper() == null) { - // fte.setSubTaskWrapper(new ArrayList()); - // } - // return fte; - //} - /** * 获取组合任务实体 如果数据库不存在该实体,则新创建一个新的任务组实体 */ diff --git a/Aria/src/main/java/com/arialyy/aria/core/manager/TaskWrapperManager.java b/Aria/src/main/java/com/arialyy/aria/core/manager/TaskWrapperManager.java index 061b79ce..239c86bf 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/manager/TaskWrapperManager.java +++ b/Aria/src/main/java/com/arialyy/aria/core/manager/TaskWrapperManager.java @@ -29,7 +29,7 @@ import java.util.concurrent.locks.ReentrantLock; * Created by Aria.Lao on 2017/11/1. 任务实体管理器 */ public class TaskWrapperManager { - private static final String TAG = "TaskManager"; + private static final String TAG = "TaskWrapperManager"; private static volatile TaskWrapperManager INSTANCE = null; private LruCache cache = new LruCache<>(1024); private Lock lock; @@ -37,7 +37,9 @@ public class TaskWrapperManager { public static TaskWrapperManager getInstance() { if (INSTANCE == null) { synchronized (TaskWrapperManager.class) { - INSTANCE = new TaskWrapperManager(); + if (INSTANCE == null) { + INSTANCE = new TaskWrapperManager(); + } } } return INSTANCE; @@ -81,7 +83,7 @@ public class TaskWrapperManager { return null; } wrapper = factory.create(taskId); - cache.put(convertKey(clazz, taskId), wrapper); + putTaskWrapper(wrapper); } return (TW) wrapper; } finally { @@ -107,7 +109,7 @@ public class TaskWrapperManager { return null; } tWrapper = factory.getGroupWrapper(taskId); - cache.put(convertKey(clazz, taskId), tWrapper); + putTaskWrapper(tWrapper); } return (TW) tWrapper; } finally { @@ -119,30 +121,17 @@ public class TaskWrapperManager { * 更新任务Wrapper */ public void putTaskWrapper(AbsTaskWrapper wrapper) { - final Lock lock = this.lock; - lock.lock(); - try { - cache.put(convertKey(wrapper.getClass(), wrapper.getEntity().getId()), wrapper); - } finally { - lock.unlock(); - } - } - - /** - * 向管理器中增加任务实体 - * - * @return {@code false} 实体为null,添加失败 - */ - public boolean addTaskWrapper(AbsTaskWrapper wrapper) { if (wrapper == null) { ALog.e(TAG, "任务实体添加失败"); - return false; + return; + } + if (wrapper.getEntity() == null || wrapper.getEntity().getId() == -1) { + return; } final Lock lock = this.lock; lock.lock(); try { - return cache.put(convertKey(wrapper.getClass(), wrapper.getEntity().getId()), wrapper) - != null; + cache.put(convertKey(wrapper.getClass(), wrapper.getEntity().getId()), wrapper); } finally { lock.unlock(); } @@ -151,11 +140,11 @@ public class TaskWrapperManager { /** * 通过key删除任务实体 当任务complete或删除记录时将删除缓存 */ - public AbsTaskWrapper removeTaskWrapper(AbsTaskWrapper wrapper) { + public void removeTaskWrapper(AbsTaskWrapper wrapper) { final Lock lock = this.lock; lock.lock(); try { - return cache.remove(convertKey(wrapper.getClass(), wrapper.getEntity().getId())); + cache.remove(convertKey(wrapper.getClass(), wrapper.getEntity().getId())); } finally { lock.unlock(); } 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 98ab974f..91a1963a 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 @@ -177,7 +177,7 @@ public abstract class AbsTaskQueue implements IPool { @Override public TASK getTask(String key) { synchronized (LOCK) { if (TextUtils.isEmpty(key)) { - ALog.e(TAG, "key 为null"); + ALog.e(TAG, "key为null"); return null; } return mExecuteMap.get(CommonUtil.keyToHashKey(key)); diff --git a/Aria/src/main/java/com/arialyy/aria/core/upload/CheckUEntityUtil.java b/Aria/src/main/java/com/arialyy/aria/core/upload/CheckUEntityUtil.java index 881d9867..156cc36b 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/upload/CheckUEntityUtil.java +++ b/Aria/src/main/java/com/arialyy/aria/core/upload/CheckUEntityUtil.java @@ -16,11 +16,13 @@ package com.arialyy.aria.core.upload; import android.text.TextUtils; +import com.arialyy.aria.core.inf.ICheckEntityUtil; +import com.arialyy.aria.core.inf.ITaskWrapper; import com.arialyy.aria.util.ALog; import com.arialyy.aria.util.CheckUtil; import java.io.File; -public class CheckUEntityUtil { +public class CheckUEntityUtil implements ICheckEntityUtil { private final String TAG = "CheckUEntityUtil"; private UTaskWrapper mWrapper; private UploadEntity mEntity; @@ -34,7 +36,13 @@ public class CheckUEntityUtil { mEntity = mWrapper.getEntity(); } + @Override public boolean checkEntity() { + if (mWrapper.getErrorEvent() != null) { + ALog.e(TAG, mWrapper.getErrorEvent().errorMsg); + return false; + } + boolean b = checkFtps() && checkUrl() && checkFilePath(); if (b) { mEntity.save(); @@ -94,7 +102,8 @@ public class CheckUEntityUtil { } private boolean checkFtps() { - if (mWrapper.asFtp().getUrlEntity().isFtps) { + if (mWrapper.getRequestType() == ITaskWrapper.U_FTP && mWrapper.asFtp() + .getUrlEntity().isFtps) { if (TextUtils.isEmpty(mWrapper.asFtp().getUrlEntity().storePath)) { ALog.e(TAG, "证书路径为空"); return false; diff --git a/Aria/src/main/java/com/arialyy/aria/core/upload/UNormalConfigHandler.java b/Aria/src/main/java/com/arialyy/aria/core/upload/UNormalConfigHandler.java index cb8fb2c9..f9eae1de 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/upload/UNormalConfigHandler.java +++ b/Aria/src/main/java/com/arialyy/aria/core/upload/UNormalConfigHandler.java @@ -16,6 +16,7 @@ package com.arialyy.aria.core.upload; import com.arialyy.aria.core.common.ftp.IFtpUploadInterceptor; +import com.arialyy.aria.core.event.ErrorEvent; import com.arialyy.aria.core.inf.AbsEntity; import com.arialyy.aria.core.inf.AbsTarget; import com.arialyy.aria.core.inf.IConfigHandler; @@ -42,6 +43,9 @@ class UNormalConfigHandler implements IConfigHandler { private void initTarget(long taskId, String targetName) { mWrapper = TaskWrapperManager.getInstance().getNormalTaskWrapper(UTaskWrapper.class, taskId); + if (taskId != -1 && mWrapper.getEntity().getId() == -1) { + mWrapper.setErrorEvent(new ErrorEvent(taskId, String.format("没有id为%s的任务", taskId))); + } mEntity = mWrapper.getEntity(); mTarget.setTargetName(targetName); mTarget.setTaskWrapper(mWrapper); @@ -50,6 +54,7 @@ class UNormalConfigHandler implements IConfigHandler { void setFilePath(String filePath) { File file = new File(filePath); + mEntity.setFilePath(filePath); mEntity.setFileName(file.getName()); mEntity.setFileSize(file.length()); } 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 476773f4..9cff621f 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 @@ -18,7 +18,7 @@ package com.arialyy.aria.core.upload; import android.os.Parcel; import android.os.Parcelable; import com.arialyy.aria.core.inf.AbsNormalEntity; -import com.arialyy.aria.core.inf.AbsTaskWrapper; +import com.arialyy.aria.core.inf.ITaskWrapper; import com.arialyy.aria.orm.annotation.Primary; /** @@ -57,7 +57,7 @@ public class UploadEntity extends AbsNormalEntity implements Parcelable { } @Override public int getTaskType() { - return getUrl().startsWith("ftp") ? AbsTaskWrapper.D_FTP : AbsTaskWrapper.D_HTTP; + return getUrl().startsWith("ftp") ? ITaskWrapper.D_FTP : ITaskWrapper.D_HTTP; } public UploadEntity() { diff --git a/Aria/src/main/java/com/arialyy/aria/core/upload/uploader/FtpFileInfoThread.java b/Aria/src/main/java/com/arialyy/aria/core/upload/uploader/FtpFileInfoThread.java index fbe55682..3be4fbb1 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/upload/uploader/FtpFileInfoThread.java +++ b/Aria/src/main/java/com/arialyy/aria/core/upload/uploader/FtpFileInfoThread.java @@ -144,7 +144,7 @@ class FtpFileInfoThread extends AbsFtpInfoThread { ThreadRecord threadRecord; if (record.threadRecords == null || record.threadRecords.isEmpty()) { threadRecord = new ThreadRecord(); - threadRecord.key = record.filePath; + threadRecord.taskKey = record.filePath; } else { threadRecord = record.threadRecords.get(0); } diff --git a/Aria/src/main/java/com/arialyy/aria/core/upload/uploader/FtpThreadTask.java b/Aria/src/main/java/com/arialyy/aria/core/upload/uploader/FtpThreadTask.java index 15890a06..e681508f 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/upload/uploader/FtpThreadTask.java +++ b/Aria/src/main/java/com/arialyy/aria/core/upload/uploader/FtpThreadTask.java @@ -176,7 +176,7 @@ class FtpThreadTask extends AbsFtpThreadTask { fail(mChildCurrentLocation, new AriaIOException(TAG, String.format("文件上传错误,错误码为:%s, msg:%s, filePath: %s", reply, - client.getReplyString(), getEntity().getFilePath()))); + client.getReplyString(), getEntity().getFilePath())), false); } if (client.isConnected()) { client.disconnect(); diff --git a/Aria/src/main/java/com/arialyy/aria/core/upload/uploader/SimpleUploadUtil.java b/Aria/src/main/java/com/arialyy/aria/core/upload/uploader/SimpleUploadUtil.java index 51a56135..528da2d4 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/upload/uploader/SimpleUploadUtil.java +++ b/Aria/src/main/java/com/arialyy/aria/core/upload/uploader/SimpleUploadUtil.java @@ -22,7 +22,6 @@ import com.arialyy.aria.core.inf.AbsEntity; import com.arialyy.aria.core.inf.AbsTaskWrapper; import com.arialyy.aria.core.inf.IUploadListener; import com.arialyy.aria.core.upload.UTaskWrapper; -import com.arialyy.aria.core.upload.UploadEntity; import com.arialyy.aria.exception.BaseException; import com.arialyy.aria.util.CheckUtil; @@ -33,7 +32,6 @@ import com.arialyy.aria.util.CheckUtil; public class SimpleUploadUtil implements IUtil, Runnable { private static final String TAG = "SimpleUploadUtil"; - private UploadEntity mUploadEntity; private UTaskWrapper mTaskWrapper; private IUploadListener mListener; private Uploader mUploader; @@ -42,7 +40,6 @@ public class SimpleUploadUtil implements IUtil, Runnable { public SimpleUploadUtil(UTaskWrapper taskWrapper, IUploadListener listener) { mTaskWrapper = taskWrapper; CheckUtil.checkTaskEntity(taskWrapper); - mUploadEntity = taskWrapper.getEntity(); if (listener == null) { throw new IllegalArgumentException("上传监听不能为空"); } diff --git a/Aria/src/main/java/com/arialyy/aria/exception/ParamException.java b/Aria/src/main/java/com/arialyy/aria/exception/ParamException.java index fb77a9e0..b2299c2f 100644 --- a/Aria/src/main/java/com/arialyy/aria/exception/ParamException.java +++ b/Aria/src/main/java/com/arialyy/aria/exception/ParamException.java @@ -1,24 +1,24 @@ -/* - * 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.exception; - -public class ParamException extends RuntimeException { - private static final String ARIA_NET_EXCEPTION = "Aria Params Exception:"; - - public ParamException(String message) { - super(String.format("%s%s", ARIA_NET_EXCEPTION, message)); - } -} +/* + * 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.exception; + +public class ParamException extends RuntimeException { + private static final String ARIA_NET_EXCEPTION = "Aria Params Exception:"; + + public ParamException(String message) { + super(String.format("%s%s", ARIA_NET_EXCEPTION, message)); + } +} diff --git a/Aria/src/main/java/com/arialyy/aria/orm/DBConfig.java b/Aria/src/main/java/com/arialyy/aria/orm/DBConfig.java index cedd69d8..e50113e7 100644 --- a/Aria/src/main/java/com/arialyy/aria/orm/DBConfig.java +++ b/Aria/src/main/java/com/arialyy/aria/orm/DBConfig.java @@ -38,7 +38,7 @@ class DBConfig { static boolean DEBUG = false; static Map mapping = new LinkedHashMap<>(); static String DB_NAME; - static int VERSION = 51; + static int VERSION = 52; /** * 是否将数据库保存在Sd卡,{@code true} 是 diff --git a/Aria/src/main/java/com/arialyy/aria/orm/DelegateFind.java b/Aria/src/main/java/com/arialyy/aria/orm/DelegateFind.java index ae8e01ad..fad82460 100644 --- a/Aria/src/main/java/com/arialyy/aria/orm/DelegateFind.java +++ b/Aria/src/main/java/com/arialyy/aria/orm/DelegateFind.java @@ -15,7 +15,6 @@ */ package com.arialyy.aria.orm; -import android.annotation.SuppressLint; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.text.TextUtils; @@ -30,10 +29,8 @@ import java.lang.reflect.Field; import java.net.URLDecoder; import java.util.ArrayList; import java.util.Date; -import java.util.HashMap; import java.util.List; import java.util.Map; -import java.util.Set; /** * Created by laoyuyu on 2018/3/22. 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 5957d750..1795cb79 100644 --- a/Aria/src/main/java/com/arialyy/aria/orm/SqlHelper.java +++ b/Aria/src/main/java/com/arialyy/aria/orm/SqlHelper.java @@ -100,6 +100,8 @@ final class SqlHelper extends SQLiteOpenHelper { handle360AriaUpdate(db); } else if (oldVersion < 51) { handle365Update(db); + } else if (oldVersion < 52) { + handle366Update(db); } else { handleDbUpdate(db, null, null); } @@ -239,23 +241,43 @@ final class SqlHelper extends SQLiteOpenHelper { */ private void delRepeatThreadRecord(SQLiteDatabase db) { String repeatSql = - "DELETE FROM ThreadRecord WHERE (key, threadId, endLocation) " - + "IN (SELECT key, threadId, endLocation FROM ThreadRecord GROUP BY key, threadId, endLocation " + "DELETE FROM ThreadRecord WHERE (taskKey, threadId, endLocation) " + + "IN (SELECT taskKey, threadId, endLocation FROM ThreadRecord GROUP BY taskKey, threadId, endLocation " + "HAVING COUNT(*) > 1) AND rowid " - + "NOT IN (SELECT MIN(rowid) FROM ThreadRecord GROUP BY key, threadId, endLocation " + + "NOT IN (SELECT MIN(rowid) FROM ThreadRecord GROUP BY taskKey, threadId, endLocation " + "HAVING COUNT(*)> 1)"; ALog.d(TAG, repeatSql); db.execSQL(repeatSql); } + /** + * 处理366版本以下的升级 + */ + private void handle366Update(SQLiteDatabase db) { + Map> modifyMap = new HashMap<>(); + // 处理ThreadRecord的key字段名修改 + Map threadRecordModify = new HashMap<>(); + threadRecordModify.put("key", "taskKey"); + modifyMap.put("ThreadRecord", threadRecordModify); + + // 执行升级操作 + handleDbUpdate(db, modifyMap, null); + } + /** * 处理365版本以下的升级 */ private void handle365Update(SQLiteDatabase db) { db.execSQL("UPDATE ThreadRecord SET threadId=0 WHERE threadId=-1"); + Map> modifyMap = new HashMap<>(); + // 处理ThreadRecord的key字段名修改 + Map threadRecordModify = new HashMap<>(); + threadRecordModify.put("key", "taskKey"); + modifyMap.put("ThreadRecord", threadRecordModify); + // 执行升级操作 - handleDbUpdate(db, null, null); + handleDbUpdate(db, modifyMap, null); delRepeatThreadRecord(db); } @@ -271,19 +293,25 @@ final class SqlHelper extends SQLiteOpenHelper { } } - Map> columnMap = new HashMap<>(); + Map> modifyMap = new HashMap<>(); // 处理DownloadEntity、DownloadGroupEntity的 groupName字段名的修改 - Map groupNameModify = new HashMap<>(); - groupNameModify.put("groupName", "groupHash"); - columnMap.put("DownloadEntity", groupNameModify); - columnMap.put("DownloadGroupEntity", groupNameModify); + Map entityModify = new HashMap<>(); + entityModify.put("groupName", "groupHash"); + modifyMap.put("DownloadEntity", entityModify); + modifyMap.put("DownloadGroupEntity", entityModify); + // 处理TaskRecord的dGroupName字段名的修改 - Map dGroupNameModify = new HashMap<>(); - dGroupNameModify.put("dGroupName", "dGroupHash"); - columnMap.put("TaskRecord", dGroupNameModify); + Map taskRecordModify = new HashMap<>(); + taskRecordModify.put("dGroupName", "dGroupHash"); + modifyMap.put("TaskRecord", taskRecordModify); + + // 处理ThreadRecord的key字段名修改 + Map threadRecordModify = new HashMap<>(); + threadRecordModify.put("key", "taskKey"); + modifyMap.put("ThreadRecord", threadRecordModify); // 执行升级操作 - handleDbUpdate(db, columnMap, null); + handleDbUpdate(db, modifyMap, null); delRepeatThreadRecord(db); } @@ -324,18 +352,19 @@ final class SqlHelper extends SQLiteOpenHelper { i++; } - Map> modifyColumnMap = new HashMap<>(); - Map dMap = new HashMap<>(); - dMap.put("groupName", "groupHash"); // 处理数据库版本小于3的字段改变 - dMap.put("downloadUrl", "url"); - dMap.put("isDownloadComplete", "isComplete"); - modifyColumnMap.put("DownloadEntity", dMap); - - Map dGMap = new HashMap<>(); - dGMap.put("groupName", "groupHash"); - modifyColumnMap.put("DownloadGroupEntity", dGMap); - + Map> modifyMap = new HashMap<>(); + Map dEntityModifyMap = new HashMap<>(); + dEntityModifyMap.put("groupName", "groupHash"); + dEntityModifyMap.put("downloadUrl", "url"); + dEntityModifyMap.put("isDownloadComplete", "isComplete"); + modifyMap.put("DownloadEntity", dEntityModifyMap); + + Map dGEntityModifyMap = new HashMap<>(); + dGEntityModifyMap.put("groupName", "groupHash"); + modifyMap.put("DownloadGroupEntity", dGEntityModifyMap); + + // 删除字段 Map> delColumnMap = new HashMap<>(); List dEntityDel = new ArrayList<>(); dEntityDel.add("taskKey"); @@ -344,6 +373,6 @@ final class SqlHelper extends SQLiteOpenHelper { dgEntityDel.add("subtask"); delColumnMap.put("DownloadGroupEntity", dgEntityDel); - handleDbUpdate(db, modifyColumnMap, delColumnMap); + handleDbUpdate(db, modifyMap, delColumnMap); } } \ No newline at end of file diff --git a/Aria/src/main/java/com/arialyy/aria/util/RecordUtil.java b/Aria/src/main/java/com/arialyy/aria/util/RecordUtil.java index 313568f8..c5d5b522 100644 --- a/Aria/src/main/java/com/arialyy/aria/util/RecordUtil.java +++ b/Aria/src/main/java/com/arialyy/aria/util/RecordUtil.java @@ -83,7 +83,7 @@ public class RecordUtil { } } } - DbEntity.deleteData(ThreadRecord.class, "key=?", record.taskRecord.filePath); + DbEntity.deleteData(ThreadRecord.class, "taskKey=?", record.taskRecord.filePath); record.taskRecord.deleteData(); } } @@ -148,15 +148,14 @@ public class RecordUtil { */ if (!entity.isComplete()) { if (record.taskType == TaskRecord.TYPE_M3U8_VOD) { // 删除ts分片文件 - String cacheDir = null; - if (!targetFile.isDirectory()) { - cacheDir = targetFile.getParent() + "/." + targetFile.getName(); - } - removeTsCache(cacheDir); + removeTsCache(targetFile, record.bandWidth); } else if (record.isBlock) { // 删除分块文件 removeBlockFile(record); } } else if (removeFile) { // 处理任务完成情况 + if (record.taskType == TaskRecord.TYPE_M3U8_VOD) { + removeTsCache(targetFile, record.bandWidth); + } removeTargetFile(targetFile); } @@ -209,16 +208,14 @@ public class RecordUtil { */ if (!entity.isComplete()) { if (record.taskType == TaskRecord.TYPE_M3U8_VOD) { // 删除ts分片文件 - String cacheDir = null; - if (!targetFile.isDirectory()) { - cacheDir = String.format("%s/.%s_%s", targetFile.getParent(), targetFile.getName(), - record.bandWidth); - } - removeTsCache(cacheDir); + removeTsCache(targetFile, record.bandWidth); } else if (record.isBlock) { // 删除分块文件 removeBlockFile(record); } } else if (removeFile) { // 处理任务完成情况 + if (record.taskType == TaskRecord.TYPE_M3U8_VOD) { + removeTsCache(targetFile, record.bandWidth); + } removeTargetFile(targetFile); } @@ -238,7 +235,7 @@ public class RecordUtil { private static void removeRecord(String filePath) { ALog.i(TAG, "删除任务记录"); - DbEntity.deleteData(ThreadRecord.class, "key=?", filePath); + DbEntity.deleteData(ThreadRecord.class, "taskKey=?", filePath); DbEntity.deleteData(TaskRecord.class, "filePath=?", filePath); } @@ -287,11 +284,17 @@ public class RecordUtil { /** * 删除ts文件 */ - private static void removeTsCache(String cacheDir) { + private static void removeTsCache(File targetFile, long bandWidth) { + + String cacheDir = null; + if (!targetFile.isDirectory()) { + cacheDir = + String.format("%s/.%s_%s", targetFile.getParent(), targetFile.getName(), bandWidth); + } if (!TextUtils.isEmpty(cacheDir)) { File cacheDirF = new File(cacheDir); - if (!cacheDirF.exists()){ + if (!cacheDirF.exists()) { return; } File[] files = cacheDirF.listFiles(); @@ -348,7 +351,7 @@ public class RecordUtil { // 修改线程记录 if (record.threadRecords != null && !record.threadRecords.isEmpty()) { for (ThreadRecord tr : record.threadRecords) { - tr.key = newPath; + tr.taskKey = newPath; File blockFile = new File(String.format(RecordHandler.SUB_PATH, oldPath, tr.threadId)); if (blockFile.exists()) { blockFile.renameTo(new File(String.format(RecordHandler.SUB_PATH, newPath, tr.threadId))); diff --git a/DEV_LOG.md b/DEV_LOG.md index 9023c673..6da61d7d 100644 --- a/DEV_LOG.md +++ b/DEV_LOG.md @@ -1,4 +1,7 @@ ## 开发日志 + + v_3.7 + - 适配androidX + - 修复组合任务,恢复下载,会出现进度显示为0的问题 + v_3.6.5 - fix bug https://github.com/AriaLyy/Aria/issues/403 - fix bug https://github.com/AriaLyy/Aria/issues/414 diff --git a/README.md b/README.md index a695aef5..99598aee 100644 --- a/README.md +++ b/README.md @@ -158,7 +158,6 @@ protected void onCreate(Bundle savedInstanceState) { ## 其他 有任何问题,可以在[issues](https://github.com/AriaLyy/Aria/issues)给我留言反馈。
在提交问题前,希望你已经查看过[wiki](https://aria.laoyuyu.me/aria_doc/)或搜索过[issues](https://github.com/AriaLyy/Aria/issues)。
- 交流群:524329160 *** diff --git a/app/src/main/assets/aria_config.xml b/app/src/main/assets/aria_config.xml index dab01a5e..4956bd53 100644 --- a/app/src/main/assets/aria_config.xml +++ b/app/src/main/assets/aria_config.xml @@ -41,10 +41,10 @@ 3、从3.4.1开始,如果线程数为1,文件初始化时将不再预占用对应长度的空间,下载多少byte,则占多大的空间; 对于3.4.1之前版本的未完成的老任务,依然采用原来的文件空间占用方式; --> - + - + diff --git a/app/src/main/assets/help_code/FtpDownload.java b/app/src/main/assets/help_code/FtpDownload.java index 6e97d2d2..86a27cb1 100644 --- a/app/src/main/assets/help_code/FtpDownload.java +++ b/app/src/main/assets/help_code/FtpDownload.java @@ -40,7 +40,7 @@ public class FtpDownload extends BaseActivity { } @Download.onTaskStart void taskStart(DownloadTask task) { - Log.d(TAG, "onStart"); + Log.d(TAG, "onPreStart"); } @Download.onTaskRunning protected void running(DownloadTask task) { diff --git a/app/src/main/java/com/arialyy/simple/core/download/FtpDownloadActivity.java b/app/src/main/java/com/arialyy/simple/core/download/FtpDownloadActivity.java index 4d0e75e9..8bb4be82 100644 --- a/app/src/main/java/com/arialyy/simple/core/download/FtpDownloadActivity.java +++ b/app/src/main/java/com/arialyy/simple/core/download/FtpDownloadActivity.java @@ -46,7 +46,7 @@ import java.io.IOException; public class FtpDownloadActivity extends BaseActivity { private String mUrl, mFilePath; private FtpDownloadModule mModule; - private DownloadEntity mEntity; + private long mTaskId; @Override protected void init(Bundle savedInstanceState) { super.init(savedInstanceState); @@ -60,10 +60,10 @@ public class FtpDownloadActivity extends BaseActivity " + CommonUtil.getFileMD5(new File(task.getDownloadPath()))); + Log.d(TAG, "md5 ==> " + CommonUtil.getFileMD5(new File(task.getFilePath()))); T.showShort(this, "文件:" + task.getEntity().getFileName() + ",下载完成"); } diff --git a/app/src/main/java/com/arialyy/simple/core/download/HighestPriorityActivity.java b/app/src/main/java/com/arialyy/simple/core/download/HighestPriorityActivity.java index e16baec6..97569912 100644 --- a/app/src/main/java/com/arialyy/simple/core/download/HighestPriorityActivity.java +++ b/app/src/main/java/com/arialyy/simple/core/download/HighestPriorityActivity.java @@ -16,7 +16,6 @@ package com.arialyy.simple.core.download; import android.os.Bundle; -import android.os.Environment; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.view.Menu; @@ -149,15 +148,11 @@ public class HighestPriorityActivity extends BaseActivity { private String mUrl; private String mFilePath; private HttpDownloadModule mModule; - private DownloadEntity mEntity; + private long mTaskId = -1; BroadcastReceiver receiver = new BroadcastReceiver() { @Override @@ -95,11 +95,10 @@ public class SingleTaskActivity extends BaseActivity { if (entity == null) { return; } - mEntity = entity; - - if (mEntity.getState() == IEntity.STATE_STOP) { + mTaskId = entity.getId(); + if (entity.getState() == IEntity.STATE_STOP) { getBinding().setStateStr(getString(R.string.resume)); - } else if (mEntity.getState() == IEntity.STATE_RUNNING) { + } else if (entity.getState() == IEntity.STATE_RUNNING) { getBinding().setStateStr(getString(R.string.stop)); } @@ -231,6 +230,7 @@ public class SingleTaskActivity extends BaseActivity { @Download.onTaskCancel void taskCancel(DownloadTask task) { if (task.getKey().equals(mUrl)) { + mTaskId = -1; getBinding().setProgress(0); getBinding().setStateStr(getString(R.string.start)); getBinding().setSpeed(""); @@ -267,25 +267,24 @@ public class SingleTaskActivity extends BaseActivity { public void onClick(View view) { switch (view.getId()) { case R.id.start: - if (!AppUtil.chekEntityValid(mEntity)) { + if (mTaskId == -1) { startD(); + break; } - if (Aria.download(this).load(mEntity.getId()).isRunning()) { - Aria.download(this).load(mEntity.getId()).stop(); + if (Aria.download(this).load(mTaskId).isRunning()) { + Aria.download(this).load(mTaskId).stop(); } else { - Aria.download(this).load(mEntity.getId()).resume(); + Aria.download(this).load(mTaskId).resume(); } break; case R.id.cancel: - if (AppUtil.chekEntityValid(mEntity)) { - Aria.download(this).load(mEntity.getId()).cancel(true); - } + Aria.download(this).load(mTaskId).cancel(true); break; } } private void startD() { - Aria.download(SingleTaskActivity.this) + mTaskId = Aria.download(SingleTaskActivity.this) .load(mUrl) .useServerFileName(true) .setFilePath(mFilePath, true) diff --git a/app/src/main/java/com/arialyy/simple/core/download/group/DownloadGroupActivity.java b/app/src/main/java/com/arialyy/simple/core/download/group/DownloadGroupActivity.java index 9fce4b02..70e3b102 100644 --- a/app/src/main/java/com/arialyy/simple/core/download/group/DownloadGroupActivity.java +++ b/app/src/main/java/com/arialyy/simple/core/download/group/DownloadGroupActivity.java @@ -32,7 +32,6 @@ import com.arialyy.frame.util.show.T; import com.arialyy.simple.R; import com.arialyy.simple.base.BaseActivity; import com.arialyy.simple.databinding.ActivityDownloadGroupBinding; -import com.arialyy.simple.util.AppUtil; import com.arialyy.simple.widget.SubStateLinearLayout; import java.util.List; import java.util.Map; @@ -44,7 +43,7 @@ public class DownloadGroupActivity extends BaseActivity mUrls; - private DownloadGroupEntity mEntity; + private long mTaskId = -1; @Override protected void init(Bundle savedInstanceState) { super.init(savedInstanceState); @@ -53,21 +52,22 @@ public class DownloadGroupActivity extends BaseActivity { - private static final String dir = "ftp://9.9.9.50:21/upload/测试"; + private static final String dir = "ftp://9.9.9.205:2121/upload/测试"; private SubStateLinearLayout mChildList; - private DownloadGroupEntity mEntity; + private long mTaskId = -1; @Override protected void init(Bundle savedInstanceState) { super.init(savedInstanceState); Aria.download(this).register(); setTitle("FTP文件夹下载"); mChildList = findViewById(R.id.child_list); - mEntity = Aria.download(this).getFtpDirEntity(dir); - if (mEntity != null) { - mChildList.addData(mEntity.getSubEntities()); - getBinding().setFileSize(mEntity.getConvertFileSize()); - if (mEntity.getFileSize() == 0) { + DownloadGroupEntity entity = Aria.download(this).getFtpDirEntity(dir); + if (entity != null) { + mTaskId = entity.getId(); + mChildList.addData(entity.getSubEntities()); + getBinding().setFileSize(entity.getConvertFileSize()); + if (entity.getState() == IEntity.STATE_RUNNING) { + getBinding().setStateStr(getString(R.string.stop)); + } else { + getBinding().setStateStr(getString(R.string.start)); + } + if (entity.getFileSize() == 0) { getBinding().setProgress(0); } else { - getBinding().setProgress(mEntity.isComplete() ? 100 - : (int) (mEntity.getCurrentProgress() * 100 / mEntity.getFileSize())); + getBinding().setProgress(entity.isComplete() ? 100 + : (int) (entity.getCurrentProgress() * 100 / entity.getFileSize())); } } } @@ -65,28 +71,35 @@ public class FTPDirDownloadActivity extends BaseActivity { private String mFilePath; private M3U8VodModule mModule; private VideoPlayerFragment mVideoFragment; - private DownloadEntity mEntity; + private long mTaskId; @Override protected void init(Bundle savedInstanceState) { @@ -75,10 +74,10 @@ public class M3U8VodDLoadActivity extends BaseActivity { if (entity == null) { return; } - mEntity = entity; - if (mEntity.getState() == IEntity.STATE_STOP) { + mTaskId = entity.getId(); + if (entity.getState() == IEntity.STATE_STOP) { getBinding().setStateStr(getString(R.string.resume)); - } else if (mEntity.getState() == IEntity.STATE_RUNNING) { + } else if (entity.getState() == IEntity.STATE_RUNNING) { getBinding().setStateStr(getString(R.string.stop)); } @@ -272,26 +271,47 @@ public class M3U8VodDLoadActivity extends BaseActivity { public void onClick(View view) { switch (view.getId()) { case R.id.start: - if (!AppUtil.chekEntityValid(mEntity)) { + if (mTaskId == -1) { startD(); break; } - if (Aria.download(this).load(mEntity.getId()).isRunning()) { - Aria.download(this).load(mEntity.getId()).stop(); + if (Aria.download(this).load(mTaskId).isRunning()) { + Aria.download(this).load(mTaskId).stop(); } else { - Aria.download(this).load(mEntity.getId()).resume(); + Aria.download(this) + .load(mTaskId) + .asM3U8() + .setBandWidthUrlConverter(new IBandWidthUrlConverter() { + @Override public String convert(String bandWidthUrl) { + int index = mUrl.lastIndexOf("/"); + return mUrl.substring(0, index + 1) + bandWidthUrl; + } + }) + .setTsUrlConvert(new IVodTsUrlConverter() { + @Override public List convert(String m3u8Url, List tsUrls) { + int index = m3u8Url.lastIndexOf("/"); + String parentUrl = m3u8Url.substring(0, index + 1); + List newUrls = new ArrayList<>(); + for (String url : tsUrls) { + newUrls.add(parentUrl + url); + } + + return newUrls; + } + }) + .controller(ControllerType.NORMAL_CONTROLLER) + .resume(); } break; case R.id.cancel: - if (AppUtil.chekEntityValid(mEntity)) { - Aria.download(this).load(mEntity.getId()).cancel(true); - } + Aria.download(this).load(mTaskId).cancel(true); + mTaskId = -1; break; } } private void startD() { - Aria.download(M3U8VodDLoadActivity.this) + mTaskId = Aria.download(M3U8VodDLoadActivity.this) .load(mUrl) .useServerFileName(true) .setFilePath(mFilePath, true) @@ -315,15 +335,13 @@ public class M3U8VodDLoadActivity extends BaseActivity { } }) .setMergeHandler(new ITsMergeHandler() { - @Override public boolean merge(@Nullable M3U8KeyInfo keyInfo, List tsPath) { + public boolean merge(@Nullable M3U8KeyInfo keyInfo, List tsPath) { + ALog.d(TAG, "合并TS...."); return false; } }) .controller(ControllerType.START_CONTROLLER) .start(); - //.start(); - //.asVod().setPeerIndex(50) - //.start(); } private Class c = StartController.class; diff --git a/app/src/main/java/com/arialyy/simple/core/download/m3u8/VideoPlayerFragment.java b/app/src/main/java/com/arialyy/simple/core/download/m3u8/VideoPlayerFragment.java index 9b5f6b4f..fd119ef0 100644 --- a/app/src/main/java/com/arialyy/simple/core/download/m3u8/VideoPlayerFragment.java +++ b/app/src/main/java/com/arialyy/simple/core/download/m3u8/VideoPlayerFragment.java @@ -55,7 +55,7 @@ public class VideoPlayerFragment extends BaseFragment { } @DownloadGroup.onTaskFail void groupTaskFail(DownloadGroupTask task) { - if (task == null){ + if (task == null) { return; } mAdapter.updateBtState(task.getKey(), true); diff --git a/app/src/main/java/com/arialyy/simple/core/upload/FtpUploadActivity.java b/app/src/main/java/com/arialyy/simple/core/upload/FtpUploadActivity.java index bb4b57bc..a2967129 100644 --- a/app/src/main/java/com/arialyy/simple/core/upload/FtpUploadActivity.java +++ b/app/src/main/java/com/arialyy/simple/core/upload/FtpUploadActivity.java @@ -52,7 +52,7 @@ public class FtpUploadActivity extends BaseActivity { private String mFilePath; private String mUrl; private UploadModule mModule; - private UploadEntity mEntity; + private long mTaskId = -1; @Override protected void init(Bundle savedInstanceState) { setTile("D_FTP 文件上传"); @@ -67,8 +67,8 @@ public class FtpUploadActivity extends BaseActivity { mModule = ViewModelProviders.of(this).get(UploadModule.class); mModule.getFtpInfo(this).observe(this, new Observer() { @Override public void onChanged(@Nullable UploadEntity entity) { - mEntity = entity; if (entity != null) { + mTaskId = entity.getId(); if (entity.getFileSize() != 0) { getBinding().setFileSize(CommonUtil.formatFileSize(entity.getFileSize())); getBinding().setProgress(entity.isComplete() ? 100 @@ -79,7 +79,7 @@ public class FtpUploadActivity extends BaseActivity { mUrl = entity.getUrl(); mFilePath = entity.getFilePath(); getBinding().setStateStr(getString( - entity.getState() == IEntity.STATE_RUNNING ? R.string.stop : R.string.resume)); + entity.getState() == IEntity.STATE_RUNNING ? R.string.stop : R.string.start)); } else { getBinding().setStateStr(getString(R.string.resume)); } @@ -113,38 +113,47 @@ public class FtpUploadActivity extends BaseActivity { public void onClick(View view) { switch (view.getId()) { case R.id.start: - if (!AppUtil.chekEntityValid(mEntity)) { - Aria.upload(this).loadFtp(mFilePath).setUploadUrl(mUrl).setUploadInterceptor( - new IFtpUploadInterceptor() { - - @Override - public FtpInterceptHandler onIntercept(UploadEntity entity, List fileList) { - FtpInterceptHandler.Builder builder = new FtpInterceptHandler.Builder(); - //builder.coverServerFile(); - builder.resetFileName("test.zip"); - return builder.build(); - } - }) + if (mTaskId == -1) { + mTaskId = Aria.upload(this) + .loadFtp(mFilePath) + .setUploadUrl(mUrl) + .setUploadInterceptor( + new IFtpUploadInterceptor() { + + @Override + public FtpInterceptHandler onIntercept(UploadEntity entity, + List fileList) { + FtpInterceptHandler.Builder builder = new FtpInterceptHandler.Builder(); + //builder.coverServerFile(); + builder.resetFileName("test.zip"); + return builder.build(); + } + }) .option() - .login("N0rI", "0qcK") + .login("8L8e", "8guD") .controller(ControllerType.START_CONTROLLER) .start(); getBinding().setStateStr(getString(R.string.stop)); break; } - if (Aria.download(this).loadFtp(mEntity.getId()).isRunning()) { - Aria.download(this).loadFtp(mEntity.getId()).stop(); + if (Aria.upload(this).loadFtp(mTaskId).isRunning()) { + Aria.upload(this).loadFtp(mTaskId).stop(); getBinding().setStateStr(getString(R.string.resume)); } else { - Aria.download(this).loadFtp(mEntity.getId()).resume(); + Aria.upload(this) + .loadFtp(mTaskId) + .option() + .login("8L8e", "8guD") + .controller(ControllerType.NORMAL_CONTROLLER) + .resume(); getBinding().setStateStr(getString(R.string.stop)); } break; case R.id.cancel: - if (AppUtil.chekEntityValid(mEntity)) { - Aria.upload(this).loadFtp(mEntity.getId()).cancel(); - } + Aria.upload(this).loadFtp(mTaskId).cancel(); + mTaskId = -1; + getBinding().setStateStr(getString(R.string.start)); break; } } @@ -179,6 +188,7 @@ public class FtpUploadActivity extends BaseActivity { @Upload.onTaskFail public void taskFail(UploadTask task) { Log.d(TAG, "上传失败"); + getBinding().setStateStr(getString(R.string.resume)); } @Upload.onTaskRunning public void taskRunning(UploadTask task) { @@ -191,6 +201,7 @@ public class FtpUploadActivity extends BaseActivity { getBinding().setProgress(100); getBinding().setSpeed(""); T.showShort(this, "文件:" + task.getEntity().getFileName() + ",上传完成"); + getBinding().setStateStr(getString(R.string.re_start)); } @Override protected void dataCallback(int result, Object data) { diff --git a/app/src/main/java/com/arialyy/simple/modlue/AnyRunnModule.java b/app/src/main/java/com/arialyy/simple/modlue/AnyRunnModule.java index ab4158f1..f7e91123 100644 --- a/app/src/main/java/com/arialyy/simple/modlue/AnyRunnModule.java +++ b/app/src/main/java/com/arialyy/simple/modlue/AnyRunnModule.java @@ -52,7 +52,7 @@ public class AnyRunnModule { } @Download.onTaskStart void taskStart(DownloadTask task) { - Log.d(TAG, "onStart"); + Log.d(TAG, "onPreStart"); } @Download.onTaskRunning protected void running(DownloadTask task) { diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index cfd04d89..2e4adb69 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -64,26 +64,16 @@ - http://static.gaoshouyou.com/d/22/94/822260b849944492caadd2983f9bb624.apk - http://static.gaoshouyou.com/d/21/e8/61218d78d0e8b79df68dbc18dd484c97.apk - http://static.gaoshouyou.com/d/12/0d/7f120f50c80d2e7b8c4ba24ece4f9cdd.apk http://static.gaoshouyou.com/d/d4/4f/d6d48db3794fb9ecf47e83c346570881.apk - - - - - - - - 阴阳师 - 奇迹暖暖 - 幻影纹章 - 史上最坑爹的游戏10 - 鲜果消消乐 - 航海奇迹 + 王者荣耀 + 植物大战僵尸 + 部落冲突 + 瘟疫公司 + 水上公园大战 + 死亡之雨 test_1 test_2 @@ -91,13 +81,12 @@ - https://g37.gdl.netease.com/onmyoji_netease_10_1.0.20.apk - http://static.gaoshouyou.com/d/eb/f2/dfeba30541f209ab8a50d847fc1661ce.apk - http://rs.0.gaoshouyou.com/d/51/46/58514d126c46b8a3f27fc8c7db3b09ec.apk - http://rs.0.gaoshouyou.com/d/23/69/07238f952669727878d7a0e180534c8b.apk - http://rs.0.gaoshouyou.com/d/e7/3d/73e716d3353de5b479fcf7da8d36a5ef.apk - http://static.gaoshouyou.com/d/22/94/822260b849944492caadd2983f9bb624.apk - + https://downs.muzhiwan.com/2019/06/28/com.tencent.tmgp.sgame5d15f9df3bb4d.gpk + https://downs.muzhiwan.com/2017/03/15/com.ea.game.pvz2_row58c8bfc00eea8.gpk + https://downs.muzhiwan.com/2019/07/04/com.supercell.clashofclans.ewan.mzw_5d1da09397884.apk + https://downs.muzhiwan.com/2019/02/28/com.miniclip.plagueinc_5c77596c79179.apk + https://downs.muzhiwan.com/2019/06/03/com.aqua.slide_5cf4b69e90e6a.apk + https://downs.muzhiwan.com/2019/05/07/com.tinydevbox.deadrain2_g_5cd14a8aedb2f.apk http://dby-resource-android.duobeiyun.com/jz22a8508b4265466b9fb4bb29082eaa2d.zip http://dby-resource-android.duobeiyun.com/jz05fa8faf068145fcb25c93c8091297ad.zip http://dby-resource-android.duobeiyun.com/jz684e1b4c2f6b4576979e60fd95edebad.zip diff --git a/build.gradle b/build.gradle index 1fac0216..05b0c05b 100644 --- a/build.gradle +++ b/build.gradle @@ -43,7 +43,7 @@ task clean(type: Delete) { ext { userOrg = 'arialyy' groupId = 'com.arialyy.aria' - publishVersion = '3.6.5' + publishVersion = '3.6.6_beta_1' // publishVersion = '1.0.4' //FTP插件 repoName='maven' desc = 'android 下载框架' diff --git a/gradle.properties b/gradle.properties index 3f9aa62f..e720aeae 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,27 +1,27 @@ -## Project-wide Gradle settings. -# -# For more details on how to configure your build environment visit -# http://www.gradle.org/docs/current/userguide/build_environment.html -# -# Specifies the JVM arguments used for the daemon process. -# The setting is particularly useful for tweaking memory settings. -# Default value: -Xmx1024m -XX:MaxPermSize=256m -# org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8 -# -# When configured, Gradle will run in incubating parallel mode. -# This option should only be used with decoupled projects. More details, visit -# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects -# org.gradle.parallel=true -#Wed Dec 07 20:19:22 CST 2016 -#org.gradle.daemon=true -#org.gradle.jvmargs=-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005 -# gradle proxy https://chaosleong.github.io/2017/02/10/Configuring-Gradle-Proxy/ -#systemProp.socksProxyHost=127.0.0.1 -#systemProp.socksProxyPort=1080 -#systemprop.socksProxyVersion=5 - -#Pandroid.debug.obsoleteApi=true - -# androidx https://developer.android.com/studio/preview/features/?hl=zh-cn#androidx_migration -#android.useAndroidX=true +## Project-wide Gradle settings. +# +# For more details on how to configure your build environment visit +# http://www.gradle.org/docs/current/userguide/build_environment.html +# +# Specifies the JVM arguments used for the daemon process. +# The setting is particularly useful for tweaking memory settings. +# Default value: -Xmx1024m -XX:MaxPermSize=256m +# org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8 +# +# When configured, Gradle will run in incubating parallel mode. +# This option should only be used with decoupled projects. More details, visit +# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects +# org.gradle.parallel=true +#Wed Dec 07 20:19:22 CST 2016 +#org.gradle.daemon=true +#org.gradle.jvmargs=-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005 +# gradle proxy https://chaosleong.github.io/2017/02/10/Configuring-Gradle-Proxy/ +#systemProp.socksProxyHost=127.0.0.1 +#systemProp.socksProxyPort=1080 +#systemprop.socksProxyVersion=5 + +#Pandroid.debug.obsoleteApi=true + +# androidx https://developer.android.com/studio/preview/features/?hl=zh-cn#androidx_migration +#android.useAndroidX=true #android.enableJetifier=true \ No newline at end of file