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 576133c6..fb218198 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/Aria.java +++ b/Aria/src/main/java/com/arialyy/aria/core/Aria.java @@ -116,7 +116,7 @@ import com.arialyy.aria.util.ALog; + "请参考【https://aria.laoyuyu.me/aria_doc/create/any_java.html】,参数请使用 download(this) 或 upload(this);" + "不要使用 download(getContext()) 或 upload(getContext())"); } - return AriaManager.getInstance(context); + return AriaManager.init(context); } /** @@ -126,7 +126,7 @@ import com.arialyy.aria.util.ALog; * {@link #download(Object)}、{@link #upload(Object)} */ public static AriaManager init(Context context) { - return AriaManager.getInstance(context); + return get(context); } private static Context convertContext(Object obj) { 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 d299ffb4..6b1e9558 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/AriaManager.java +++ b/Aria/src/main/java/com/arialyy/aria/core/AriaManager.java @@ -86,7 +86,7 @@ import org.xml.sax.SAXException; * activity 和其Dialog、Fragment的映射表 */ private Map> mSubClass = new ConcurrentHashMap<>(); - public static Context APP; + private static Context APP; private DownloadConfig mDConfig; private UploadConfig mUConfig; private AppConfig mAConfig; @@ -96,27 +96,38 @@ import org.xml.sax.SAXException; private AriaManager(Context context) { APP = context.getApplicationContext(); - initDb(APP); - regAppLifeCallback(context); - initConfig(); - initAria(); - amendTaskState(); - regNetCallBack(context); } - public static AriaManager getInstance(Context context) { + public static AriaManager getInstance() { + if (INSTANCE == null) { + throw new NullPointerException("请使用AriaManager.init(context)初始化管理器"); + } + return INSTANCE; + } + + static AriaManager init(Context context) { if (INSTANCE == null) { synchronized (LOCK) { if (INSTANCE == null) { INSTANCE = new AriaManager(context); + INSTANCE.initData(); } } } return INSTANCE; } - static AriaManager getInstance() { - return INSTANCE; + private void initData() { + initDb(APP); + regAppLifeCallback(APP); + initConfig(); + initAria(); + amendTaskState(); + regNetCallBack(APP); + } + + public Context getAPP() { + return APP; } /** diff --git a/Aria/src/main/java/com/arialyy/aria/core/FtpUrlEntity.java b/Aria/src/main/java/com/arialyy/aria/core/FtpUrlEntity.java index 7e88d5df..39b43afa 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/FtpUrlEntity.java +++ b/Aria/src/main/java/com/arialyy/aria/core/FtpUrlEntity.java @@ -94,7 +94,12 @@ public class FtpUrlEntity implements Cloneable { * {@link ProtocolType} */ @ProtocolType - public String protocol; + public String protocol = ProtocolType.Default; + + /** + * 安全模式 true 隐式,false 显式 + */ + public boolean isImplicit = true; /** * 私钥别名 diff --git a/Aria/src/main/java/com/arialyy/aria/core/WidgetLiftManager.java b/Aria/src/main/java/com/arialyy/aria/core/WidgetLiftManager.java index a1f7c1e6..2dae53cb 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/WidgetLiftManager.java +++ b/Aria/src/main/java/com/arialyy/aria/core/WidgetLiftManager.java @@ -80,7 +80,7 @@ final class WidgetLiftManager { private PopupWindow.OnDismissListener createPopupWindowListener(final PopupWindow popupWindow) { return new PopupWindow.OnDismissListener() { @Override public void onDismiss() { - AriaManager.getInstance(AriaManager.APP).removeReceiver(popupWindow); + AriaManager.getInstance().removeReceiver(popupWindow); } }; } @@ -120,7 +120,7 @@ final class WidgetLiftManager { return new Dialog.OnCancelListener() { @Override public void onCancel(DialogInterface dialog) { - AriaManager.getInstance(AriaManager.APP).removeReceiver(dialog); + AriaManager.getInstance().removeReceiver(dialog); } }; } @@ -132,7 +132,7 @@ final class WidgetLiftManager { return new Dialog.OnDismissListener() { @Override public void onDismiss(DialogInterface dialog) { - AriaManager.getInstance(AriaManager.APP).removeReceiver(dialog); + AriaManager.getInstance().removeReceiver(dialog); } }; } diff --git a/Aria/src/main/java/com/arialyy/aria/core/command/HighestPriorityCmd.java b/Aria/src/main/java/com/arialyy/aria/core/command/HighestPriorityCmd.java index f1f8ee9c..42fe3c20 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/command/HighestPriorityCmd.java +++ b/Aria/src/main/java/com/arialyy/aria/core/command/HighestPriorityCmd.java @@ -41,7 +41,7 @@ final class HighestPriorityCmd extends AbsNormalCmd @Override public void executeCmd() { if (!canExeCmd) return; - if (!NetUtils.isConnected(AriaManager.APP)){ + if (!NetUtils.isConnected(AriaManager.getInstance().getAPP())){ ALog.e(TAG, "启动任务失败,网络未连接"); return; } diff --git a/Aria/src/main/java/com/arialyy/aria/core/command/ResumeAllCmd.java b/Aria/src/main/java/com/arialyy/aria/core/command/ResumeAllCmd.java index 94e0b4bc..a17dfefc 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/command/ResumeAllCmd.java +++ b/Aria/src/main/java/com/arialyy/aria/core/command/ResumeAllCmd.java @@ -36,7 +36,7 @@ final class ResumeAllCmd extends AbsNormalCmd { } @Override public void executeCmd() { - if (!NetUtils.isConnected(AriaManager.APP)) { + if (!NetUtils.isConnected(AriaManager.getInstance().getAPP())) { ALog.w(TAG, "恢复任务失败,网络未连接"); return; } 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 477575b4..5aec44d8 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 @@ -50,13 +50,13 @@ final class StartCmd extends AbsNormalCmd { @Override public void executeCmd() { if (!canExeCmd) return; - if (!NetUtils.isConnected(AriaManager.APP)) { + if (!NetUtils.isConnected(AriaManager.getInstance().getAPP())) { ALog.e(TAG, "启动任务失败,网络未连接"); return; } String mod; int maxTaskNum = mQueue.getMaxTaskNum(); - AriaManager manager = AriaManager.getInstance(AriaManager.APP); + AriaManager manager = AriaManager.getInstance(); if (isDownloadCmd) { mod = manager.getDownloadConfig().getQueueMod(); } else { diff --git a/Aria/src/main/java/com/arialyy/aria/core/common/AbsStartTarget.java b/Aria/src/main/java/com/arialyy/aria/core/common/AbsBuilderTarget.java similarity index 85% rename from Aria/src/main/java/com/arialyy/aria/core/common/AbsStartTarget.java rename to Aria/src/main/java/com/arialyy/aria/core/common/AbsBuilderTarget.java index 65614b6d..beb66c9b 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/common/AbsStartTarget.java +++ b/Aria/src/main/java/com/arialyy/aria/core/common/AbsBuilderTarget.java @@ -16,20 +16,20 @@ package com.arialyy.aria.core.common; import com.arialyy.aria.core.common.controller.IStartFeature; -import com.arialyy.aria.core.common.controller.StartController; +import com.arialyy.aria.core.common.controller.BuilderController; import com.arialyy.aria.core.inf.AbsTarget; /** * 处理第一次下载 */ -public abstract class AbsStartTarget extends AbsTarget +public abstract class AbsBuilderTarget extends AbsTarget implements IStartFeature { - private StartController mStartController; + private BuilderController mStartController; - private synchronized StartController getController() { + private synchronized BuilderController getController() { if (mStartController == null) { - mStartController = new StartController(getTaskWrapper()); + mStartController = new BuilderController(getTaskWrapper()); } return mStartController; } diff --git a/Aria/src/main/java/com/arialyy/aria/core/common/AbsThreadTask.java b/Aria/src/main/java/com/arialyy/aria/core/common/AbsThreadTask.java index aa65249a..f061e8b7 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/common/AbsThreadTask.java +++ b/Aria/src/main/java/com/arialyy/aria/core/common/AbsThreadTask.java @@ -89,7 +89,7 @@ public abstract class AbsThreadTask 0) { mSpeedBandUtil = new BandwidthLimiter(getMaxSpeed(), config.startThreadNum); } @@ -375,13 +375,14 @@ public abstract class AbsThreadTask { /** * 使用对应等控制器,注意: - * 1、对于不存在的任务(第一次下载),只能使用{@link ControllerType#START_CONTROLLER} - * 2、对于已存在的任务,只能使用{@link ControllerType#NORMAL_CONTROLLER} + * 1、对于不存在的任务(第一次下载),只能使用{@link ControllerType#CREATE_CONTROLLER} + * 2、对于已存在的任务,只能使用{@link ControllerType#TASK_CONTROLLER} * - * @param clazz {@link ControllerType#START_CONTROLLER}、{@link ControllerType#NORMAL_CONTROLLER} + * @param clazz {@link ControllerType#CREATE_CONTROLLER}、{@link ControllerType#TASK_CONTROLLER} */ @CheckResult(suggest = Suggest.TASK_CONTROLLER) public synchronized T controller(@ControllerType Class clazz) { diff --git a/Aria/src/main/java/com/arialyy/aria/core/common/controller/StartController.java b/Aria/src/main/java/com/arialyy/aria/core/common/controller/BuilderController.java similarity index 91% rename from Aria/src/main/java/com/arialyy/aria/core/common/controller/StartController.java rename to Aria/src/main/java/com/arialyy/aria/core/common/controller/BuilderController.java index a4149954..78f102f2 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/common/controller/StartController.java +++ b/Aria/src/main/java/com/arialyy/aria/core/common/controller/BuilderController.java @@ -21,11 +21,11 @@ import com.arialyy.aria.core.inf.AbsTaskWrapper; import com.arialyy.aria.util.CommonUtil; /** - * 启动控制器 + * 创建任务时使用的控制器 */ -public final class StartController extends FeatureController implements IStartFeature { +public final class BuilderController extends FeatureController implements IStartFeature { - public StartController(AbsTaskWrapper wrapper) { + public BuilderController(AbsTaskWrapper wrapper) { super(wrapper); } diff --git a/Aria/src/main/java/com/arialyy/aria/core/common/controller/ControllerType.java b/Aria/src/main/java/com/arialyy/aria/core/common/controller/ControllerType.java index 5f8360ae..5dce6424 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/common/controller/ControllerType.java +++ b/Aria/src/main/java/com/arialyy/aria/core/common/controller/ControllerType.java @@ -22,9 +22,9 @@ import java.lang.annotation.RetentionPolicy; /** * 仅用于第一次创建任务,后续可调用`#create()、#add()、#setHighestPriority()`方法。 */ - Class START_CONTROLLER = StartController.class; + Class CREATE_CONTROLLER = BuilderController.class; /** * 用于后续的任务控制,后续可调用`#stop()、#resume()、#cancel()、#cancel(boolean)、#retry()、#restart()`方法。 */ - Class NORMAL_CONTROLLER = NormalController.class; + Class TASK_CONTROLLER = NormalController.class; } 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 d981e340..6e385a19 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 @@ -15,8 +15,11 @@ */ package com.arialyy.aria.core.common.controller; +import android.Manifest; +import android.content.pm.PackageManager; import android.os.Handler; import android.os.Looper; +import com.arialyy.aria.core.AriaManager; import com.arialyy.aria.core.download.CheckDEntityUtil; import com.arialyy.aria.core.download.CheckDGEntityUtil; import com.arialyy.aria.core.download.CheckFtpDirEntityUtil; @@ -27,13 +30,12 @@ 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.scheduler.TaskSchedulers; import com.arialyy.aria.core.upload.CheckUEntityUtil; import com.arialyy.aria.core.upload.UTaskWrapper; import com.arialyy.aria.util.ALog; +import com.arialyy.aria.util.CommonUtil; import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; @@ -41,28 +43,31 @@ import java.lang.reflect.InvocationTargetException; * 功能控制器 */ public abstract class FeatureController { + private final String TAG; private AbsTaskWrapper mTaskWrapper; FeatureController(AbsTaskWrapper wrapper) { mTaskWrapper = wrapper; + TAG = CommonUtil.getClassName(getClass()); } /** * 使用对应等控制器,注意: - * 1、对于不存在的任务(第一次下载),只能使用{@link ControllerType#START_CONTROLLER} - * 2、对于已存在的任务,只能使用{@link ControllerType#NORMAL_CONTROLLER} + * 1、对于不存在的任务(第一次下载),只能使用{@link ControllerType#CREATE_CONTROLLER} + * 2、对于已存在的任务,只能使用{@link ControllerType#TASK_CONTROLLER} * - * @param clazz {@link ControllerType#START_CONTROLLER}、{@link ControllerType#NORMAL_CONTROLLER} + * @param clazz {@link ControllerType#CREATE_CONTROLLER}、{@link ControllerType#TASK_CONTROLLER} */ public static T newInstance(@ControllerType Class clazz, AbsTaskWrapper wrapper) { - if (wrapper.getEntity().getId() == -1 && clazz != ControllerType.START_CONTROLLER) { - throw new IllegalArgumentException("对于不存在的任务(第一次下载),只能使用\"ControllerType.START_CONTROLLER\""); + if (wrapper.getEntity().getId() == -1 && clazz != ControllerType.CREATE_CONTROLLER) { + throw new IllegalArgumentException( + "对于不存在的任务(第一次下载),只能使用\"ControllerType.CREATE_CONTROLLER\""); } - if (wrapper.getEntity().getId() != -1 && clazz != ControllerType.NORMAL_CONTROLLER) { + if (wrapper.getEntity().getId() != -1 && clazz != ControllerType.TASK_CONTROLLER) { throw new IllegalArgumentException( - "对于已存在的任务,只能使用\" ControllerType.NORMAL_CONTROLLER\",请检查是否重复调用#create()方法"); + "对于已存在的任务,只能使用\" ControllerType.TASK_CONTROLLER\",请检查是否重复调用#create()方法"); } Class[] paramTypes = { AbsTaskWrapper.class }; @@ -106,8 +111,11 @@ public abstract class FeatureController { * 如果检查实体失败,将错误回调 */ boolean checkConfig() { + if (!checkPermission()) { + return false; + } boolean b = checkEntity(); - ISchedulers schedulers = getScheduler(); + TaskSchedulers schedulers = TaskSchedulers.getInstance(); if (!b && schedulers != null) { new Handler(Looper.getMainLooper(), schedulers).obtainMessage(ISchedulers.CHECK_FAIL, checkTaskType(), -1, null).sendToTarget(); @@ -116,17 +124,39 @@ public abstract class FeatureController { return b; } - private ISchedulers getScheduler() { - if (mTaskWrapper instanceof DTaskWrapper) { - return DownloadSchedulers.getInstance(); + /** + * 检查权限,需要的权限有 + * {@link Manifest.permission#WRITE_EXTERNAL_STORAGE} + * {@link Manifest.permission#READ_EXTERNAL_STORAGE} + * {@link Manifest.permission#INTERNET} + * + * @return {@code false} 缺少权限 + */ + private boolean checkPermission() { + + if (AriaManager.getInstance() + .getAPP() + .checkCallingOrSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) + != PackageManager.PERMISSION_GRANTED) { + ALog.e(TAG, "启动失败,缺少权限:Manifest.permission.WRITE_EXTERNAL_STORAGE"); + return false; } - if (mTaskWrapper instanceof UTaskWrapper) { - return UploadSchedulers.getInstance(); + if (AriaManager.getInstance() + .getAPP() + .checkCallingOrSelfPermission(Manifest.permission.INTERNET) + != PackageManager.PERMISSION_GRANTED) { + ALog.e(TAG, "启动失败,缺少权限:Manifest.permission.INTERNET"); + return false; } - if (mTaskWrapper instanceof DGTaskWrapper) { - return DownloadGroupSchedulers.getInstance(); + if (AriaManager.getInstance() + .getAPP() + .checkCallingOrSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE) + != PackageManager.PERMISSION_GRANTED) { + ALog.e(TAG, "启动失败,缺少权限:Manifest.permission.READ_EXTERNAL_STORAGE"); + return false; } - return null; + + return true; } private boolean 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 ad4d965f..d7f5cd49 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 @@ -18,17 +18,15 @@ package com.arialyy.aria.core.common.ftp; import android.net.TrafficStats; import android.os.Process; import android.text.TextUtils; - import aria.apache.commons.net.ftp.FTP; import aria.apache.commons.net.ftp.FTPClient; +import aria.apache.commons.net.ftp.FTPClientConfig; import aria.apache.commons.net.ftp.FTPFile; import aria.apache.commons.net.ftp.FTPReply; import aria.apache.commons.net.ftp.FTPSClient; - import com.arialyy.aria.core.AriaManager; import com.arialyy.aria.core.FtpUrlEntity; import com.arialyy.aria.core.common.OnFileInfoCallback; -import com.arialyy.aria.core.common.ProtocolType; import com.arialyy.aria.core.inf.AbsEntity; import com.arialyy.aria.core.inf.AbsTaskWrapper; import com.arialyy.aria.core.upload.UploadEntity; @@ -40,7 +38,6 @@ import com.arialyy.aria.util.ALog; import com.arialyy.aria.util.CommonUtil; import com.arialyy.aria.util.Regular; import com.arialyy.aria.util.SSLContextUtil; - import java.io.File; import java.io.IOException; import java.net.InetAddress; @@ -48,7 +45,6 @@ import java.net.UnknownHostException; import java.util.UUID; import java.util.regex.Matcher; import java.util.regex.Pattern; - import javax.net.ssl.SSLContext; /** @@ -72,7 +68,7 @@ public abstract class AbsFtpInfoThread 0) { client = newInstanceClient(urlEntity); - InetAddress ip = InetAddress.getByName(urlEntity.hostName); client.setConnectTimeout(mConnectTimeOut); // 连接10s超时 - client.connect(ip, Integer.parseInt(urlEntity.port)); + InetAddress ip = InetAddress.getByName(urlEntity.hostName); + + client = connect(client, new InetAddress[] { ip }, 0, Integer.parseInt(urlEntity.port)); mTaskDelegate.getUrlEntity().validAddr = ip; } else { DNSQueryThread dnsThread = new DNSQueryThread(urlEntity.hostName); @@ -219,12 +213,6 @@ public abstract class AbsFtpInfoThread= ips.length) { - ALog.w(TAG, "遇到[ECONNREFUSED-连接被服务器拒绝]错误,已没有其他地址,链接失败"); + ALog.w(TAG, "遇到[ECONNREFUSED-连接被服务器拒绝]错误,已没有其他地址,链接失败;如果是ftps,请检查端口是否使用了ftp的端口而不是ftps的端口"); return null; } try { diff --git a/Aria/src/main/java/com/arialyy/aria/core/common/ftp/AbsFtpThreadTask.java b/Aria/src/main/java/com/arialyy/aria/core/common/ftp/AbsFtpThreadTask.java index 62ab2476..27f3f716 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/common/ftp/AbsFtpThreadTask.java +++ b/Aria/src/main/java/com/arialyy/aria/core/common/ftp/AbsFtpThreadTask.java @@ -18,17 +18,18 @@ package com.arialyy.aria.core.common.ftp; import android.text.TextUtils; import aria.apache.commons.net.ftp.FTP; import aria.apache.commons.net.ftp.FTPClient; +import aria.apache.commons.net.ftp.FTPClientConfig; import aria.apache.commons.net.ftp.FTPReply; import aria.apache.commons.net.ftp.FTPSClient; import com.arialyy.aria.core.FtpUrlEntity; import com.arialyy.aria.core.common.AbsThreadTask; -import com.arialyy.aria.core.common.ProtocolType; import com.arialyy.aria.core.common.SubThreadConfig; import com.arialyy.aria.core.inf.AbsNormalEntity; import com.arialyy.aria.core.inf.AbsTaskWrapper; import com.arialyy.aria.exception.AriaIOException; import com.arialyy.aria.util.ALog; import com.arialyy.aria.util.SSLContextUtil; +import java.io.IOException; import java.net.InetAddress; import java.net.UnknownHostException; import javax.net.ssl.SSLContext; @@ -45,6 +46,17 @@ public abstract class AbsFtpThreadTask extends BaseDelegate public FTPSDelegate(TARGET target, AbsTaskWrapper wrapper) { super(target, wrapper); mUrlEntity = getTaskWrapper().asFtp().getUrlEntity(); + mUrlEntity.isFtps = true; } /** @@ -91,4 +92,15 @@ public class FTPSDelegate extends BaseDelegate mUrlEntity.storePath = storePath; return this; } + + /** + * 设置安全模式,默认true + * + * @param isImplicit true 隐式,false 显式 + */ + @CheckResult(suggest = Suggest.TO_CONTROLLER) + public FTPSDelegate setImplicit(boolean isImplicit) { + mUrlEntity.isImplicit = isImplicit; + return this; + } } diff --git a/Aria/src/main/java/com/arialyy/aria/core/common/ftp/FtpDelegate.java b/Aria/src/main/java/com/arialyy/aria/core/common/ftp/FtpDelegate.java index 1d93b409..31da7af3 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/common/ftp/FtpDelegate.java +++ b/Aria/src/main/java/com/arialyy/aria/core/common/ftp/FtpDelegate.java @@ -15,8 +15,9 @@ */ package com.arialyy.aria.core.common.ftp; -import androidx.annotation.CheckResult; import android.text.TextUtils; +import androidx.annotation.CheckResult; +import aria.apache.commons.net.ftp.FTPClientConfig; import com.arialyy.aria.core.FtpUrlEntity; import com.arialyy.aria.core.common.BaseDelegate; import com.arialyy.aria.core.common.Suggest; @@ -76,6 +77,15 @@ public class FtpDelegate extends BaseDelegate return new FTPSDelegate<>(mTarget, mWrapper); } + /** + * 配置ftp客户端信息 + */ + @CheckResult(suggest = Suggest.TO_CONTROLLER) + public FtpDelegate setFtpClentConfig(FTPClientConfig config) { + getTaskWrapper().asFtp().setClientConfig(config); + return this; + } + //@Override public TARGET setProxy(Proxy proxy) { // mTarget.getTaskWrapper().asFtp().setProxy(proxy); // return mTarget; diff --git a/Aria/src/main/java/com/arialyy/aria/core/common/ftp/FtpTaskConfig.java b/Aria/src/main/java/com/arialyy/aria/core/common/ftp/FtpTaskConfig.java index b5838ab5..5a8fff0d 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/common/ftp/FtpTaskConfig.java +++ b/Aria/src/main/java/com/arialyy/aria/core/common/ftp/FtpTaskConfig.java @@ -15,6 +15,7 @@ */ package com.arialyy.aria.core.common.ftp; +import aria.apache.commons.net.ftp.FTPClientConfig; import com.arialyy.aria.core.FtpUrlEntity; import com.arialyy.aria.core.inf.ITaskConfig; import java.lang.ref.WeakReference; @@ -47,6 +48,19 @@ public class FtpTaskConfig implements ITaskConfig { */ private String newFileName; + /** + * client配置信息 + */ + private FTPClientConfig clientConfig; + + public FTPClientConfig getClientConfig() { + return clientConfig; + } + + public void setClientConfig(FTPClientConfig clientConfig) { + this.clientConfig = clientConfig; + } + public String getNewFileName() { return newFileName; } diff --git a/Aria/src/main/java/com/arialyy/aria/core/common/http/HttpDelegate.java b/Aria/src/main/java/com/arialyy/aria/core/common/http/HttpDelegate.java index f9ff08a1..10ebeea7 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/common/http/HttpDelegate.java +++ b/Aria/src/main/java/com/arialyy/aria/core/common/http/HttpDelegate.java @@ -77,6 +77,15 @@ public class HttpDelegate extends BaseDelegate return this; } + /** + * 设置http表单字段 + */ + @CheckResult(suggest = Suggest.TO_CONTROLLER) + public HttpDelegate setFormFields(Map params) { + getTaskWrapper().asHttp().setFormFields(params); + return this; + } + /** * 给url请求添加Header数据 * 如果新的header数据和数据保存的不一致,则更新数据库中对应的header数据 diff --git a/Aria/src/main/java/com/arialyy/aria/core/config/BaseConfig.java b/Aria/src/main/java/com/arialyy/aria/core/config/BaseConfig.java index 3d9ae123..c30791ed 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/config/BaseConfig.java +++ b/Aria/src/main/java/com/arialyy/aria/core/config/BaseConfig.java @@ -24,7 +24,7 @@ abstract class BaseConfig implements Serializable { * 保存配置 */ void save() { - String basePath = AriaManager.APP.getFilesDir().getPath(); + String basePath = AriaManager.getInstance().getAPP().getFilesDir().getPath(); String path = null; switch (getType()) { case TYPE_DOWNLOAD: diff --git a/Aria/src/main/java/com/arialyy/aria/core/config/Configuration.java b/Aria/src/main/java/com/arialyy/aria/core/config/Configuration.java index b0faf0c1..79c46604 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/config/Configuration.java +++ b/Aria/src/main/java/com/arialyy/aria/core/config/Configuration.java @@ -37,7 +37,7 @@ public final class Configuration { private Configuration() { //删除老版本的配置文件 - String basePath = AriaManager.APP.getFilesDir().getPath(); + String basePath = AriaManager.getInstance().getAPP().getFilesDir().getPath(); del351Config(basePath); File newDCfg = new File(String.format("%s%s", basePath, DOWNLOAD_CONFIG_FILE)); File newUCfg = new File(String.format("%s%s", basePath, UPLOAD_CONFIG_FILE)); @@ -89,7 +89,7 @@ public final class Configuration { * @return {@code true}配置存在,{@code false}配置不存在 */ public boolean configExists() { - String basePath = AriaManager.APP.getFilesDir().getPath(); + String basePath = AriaManager.getInstance().getAPP().getFilesDir().getPath(); return (new File(String.format("%s%s", basePath, DOWNLOAD_CONFIG_FILE))).exists() && (new File(String.format("%s%s", basePath, UPLOAD_CONFIG_FILE))).exists() && (new File(String.format("%s%s", basePath, APP_CONFIG_FILE))).exists() 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 1d686979..ea81c0e1 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 @@ -190,17 +190,14 @@ public class CheckDEntityUtil implements ICheckEntityUtil { } private boolean checkFtps() { - if (mWrapper.getRequestType() == ITaskWrapper.D_FTP && mWrapper.asFtp() - .getUrlEntity().isFtps) { - if (TextUtils.isEmpty(mWrapper.asFtp().getUrlEntity().storePath)) { - ALog.e(TAG, "证书路径为空"); - return false; - } - if (TextUtils.isEmpty(mWrapper.asFtp().getUrlEntity().keyAlias)) { - ALog.e(TAG, "证书别名为空"); - return false; - } - } + //if (mWrapper.getRequestType() == ITaskWrapper.D_FTP && mWrapper.asFtp() + // .getUrlEntity().isFtps) { + // String ftpUrl = mEntity.getUrl(); + // if (!ftpUrl.startsWith("ftps") && !ftpUrl.startsWith("sftp")) { + // ALog.e(TAG, String.format("地址【%s】错误,ftps地址开头必须是:ftps或sftp", ftpUrl)); + // return false; + // } + //} return true; } } 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 b4406e1e..7ad5568c 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 @@ -37,7 +37,7 @@ public class DownloadGroupTask extends AbsGroupTask { private DownloadGroupTask(DGTaskWrapper taskWrapper, Handler outHandler) { mTaskWrapper = taskWrapper; mOutHandler = outHandler; - mContext = AriaManager.APP; + mContext = AriaManager.getInstance().getAPP(); mListener = new DownloadGroupListener(this, mOutHandler); } diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadReceiver.java b/Aria/src/main/java/com/arialyy/aria/core/download/DownloadReceiver.java index 63fdcc90..c89383dd 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadReceiver.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/DownloadReceiver.java @@ -22,15 +22,14 @@ import com.arialyy.annotations.TaskEnum; import com.arialyy.aria.core.AriaManager; import com.arialyy.aria.core.command.CancelAllCmd; import com.arialyy.aria.core.command.NormalCmdFactory; -import com.arialyy.aria.core.common.AbsStartTarget; +import com.arialyy.aria.core.common.AbsBuilderTarget; import com.arialyy.aria.core.common.ProxyHelper; import com.arialyy.aria.core.event.EventMsgUtil; import com.arialyy.aria.core.inf.AbsEntity; import com.arialyy.aria.core.inf.AbsReceiver; import com.arialyy.aria.core.inf.ITask; import com.arialyy.aria.core.inf.ReceiverType; -import com.arialyy.aria.core.scheduler.DownloadGroupSchedulers; -import com.arialyy.aria.core.scheduler.DownloadSchedulers; +import com.arialyy.aria.core.scheduler.TaskSchedulers; import com.arialyy.aria.orm.DbEntity; import com.arialyy.aria.util.ALog; import com.arialyy.aria.util.CheckUtil; @@ -55,7 +54,7 @@ public class DownloadReceiver extends AbsReceiver { */ @Deprecated public DownloadReceiver setMaxSpeed(int maxSpeed) { - AriaManager.getInstance(AriaManager.APP).getDownloadConfig().setMaxSpeed(maxSpeed); + AriaManager.getInstance().getDownloadConfig().setMaxSpeed(maxSpeed); return this; } @@ -65,15 +64,15 @@ public class DownloadReceiver extends AbsReceiver { * @param url 下载地址 */ @CheckResult - public HttpStartTarget load(@NonNull String url) { + public HttpBuilderTarget load(@NonNull String url) { CheckUtil.checkUrlInvalidThrow(url); - return new HttpStartTarget(url, targetName); + return new HttpBuilderTarget(url, targetName); } /** * 用于任务停止、删除等操作 * - * @param taskId 任务id,可从{@link AbsStartTarget#create()}、{@link AbsStartTarget#add()}、{@link + * @param taskId 任务id,可从{@link AbsBuilderTarget#create()}、{@link AbsBuilderTarget#add()}、{@link * AbsEntity#getId()}读取任务id */ @CheckResult @@ -88,15 +87,15 @@ public class DownloadReceiver extends AbsReceiver { * @param urls 组合任务只任务列被,如果任务组的中的下载地址改变了,则任务从新的一个任务组 */ @CheckResult - public GroupStartTarget loadGroup(List urls) { + public GroupBuilderTarget loadGroup(List urls) { CheckUtil.checkDownloadUrls(urls); - return new GroupStartTarget(urls, targetName); + return new GroupBuilderTarget(urls, targetName); } /** * 加载组合任务,用于任务停止、删除等操作 * - * @param taskId 任务id,可从{@link AbsStartTarget#create()}、{@link AbsStartTarget#add()}、{@link + * @param taskId 任务id,可从{@link AbsBuilderTarget#create()}、{@link AbsBuilderTarget#add()}、{@link * * AbsEntity#getId()}读取任务id */ @CheckResult @@ -109,15 +108,15 @@ public class DownloadReceiver extends AbsReceiver { * 加载ftp单任务下载地址,用于任务第一次下载,如果需要控制任务停止或删除等操作,请使用{@link #loadFtp(long)} */ @CheckResult - public FtpStartTarget loadFtp(@NonNull String url) { + public FtpBuilderTarget loadFtp(@NonNull String url) { CheckUtil.checkUrlInvalidThrow(url); - return new FtpStartTarget(url, targetName); + return new FtpBuilderTarget(url, targetName); } /** * 用于任务停止、删除等操作 * - * @param taskId 任务id,可从{@link AbsStartTarget#create()}、{@link AbsStartTarget#add()}、{@link + * @param taskId 任务id,可从{@link AbsBuilderTarget#create()}、{@link AbsBuilderTarget#add()}、{@link * AbsEntity#getId()}读取任务id */ @CheckResult @@ -130,15 +129,15 @@ public class DownloadReceiver extends AbsReceiver { * 加载ftp文件夹下载地址,用于任务第一次下载,如果需要控制任务停止或删除等操作,请使用{@link #loadFtpDir(long)} */ @CheckResult - public FtpDirStartTarget loadFtpDir(@NonNull String dirUrl) { + public FtpDirBuilderTarget loadFtpDir(@NonNull String dirUrl) { CheckUtil.checkUrlInvalidThrow(dirUrl); - return new FtpDirStartTarget(dirUrl, targetName); + return new FtpDirBuilderTarget(dirUrl, targetName); } /** * 加载ftp文件夹下载地址,用于任务停止、删除等操作 * - * @param taskId 任务id,可从{@link AbsStartTarget#create()}、{@link AbsStartTarget#add()}、{@link + * @param taskId 任务id,可从{@link AbsBuilderTarget#create()}、{@link AbsBuilderTarget#add()}、{@link * AbsEntity#getId()}读取任务id */ @CheckResult @@ -164,13 +163,13 @@ public class DownloadReceiver extends AbsReceiver { if (set != null && !set.isEmpty()) { for (Integer type : set) { if (type == ProxyHelper.PROXY_TYPE_DOWNLOAD) { - DownloadSchedulers.getInstance().register(obj, TaskEnum.DOWNLOAD); + TaskSchedulers.getInstance().register(obj, TaskEnum.DOWNLOAD); } else if (type == ProxyHelper.PROXY_TYPE_DOWNLOAD_GROUP) { - DownloadGroupSchedulers.getInstance().register(obj, TaskEnum.DOWNLOAD_GROUP); + TaskSchedulers.getInstance().register(obj, TaskEnum.DOWNLOAD_GROUP); } else if (type == ProxyHelper.PROXY_TYPE_M3U8_PEER) { - DownloadSchedulers.getInstance().register(obj, TaskEnum.M3U8_PEER); + TaskSchedulers.getInstance().register(obj, TaskEnum.M3U8_PEER); } else if (type == ProxyHelper.PROXY_TYPE_DOWNLOAD_GROUP_SUB) { - DownloadGroupSchedulers.getInstance().register(obj, TaskEnum.DOWNLOAD_GROUP_SUB); + TaskSchedulers.getInstance().register(obj, TaskEnum.DOWNLOAD_GROUP_SUB); } } } else { @@ -193,7 +192,7 @@ public class DownloadReceiver extends AbsReceiver { if (needRmListener) { unRegisterListener(); } - AriaManager.getInstance(AriaManager.APP).removeReceiver(OBJ_MAP.get(getKey())); + AriaManager.getInstance().removeReceiver(OBJ_MAP.get(getKey())); } @Override public String getType() { @@ -214,9 +213,9 @@ public class DownloadReceiver extends AbsReceiver { if (set != null) { for (Integer integer : set) { if (integer == ProxyHelper.PROXY_TYPE_DOWNLOAD) { - DownloadSchedulers.getInstance().unRegister(obj); + TaskSchedulers.getInstance().unRegister(obj); } else if (integer == ProxyHelper.PROXY_TYPE_DOWNLOAD_GROUP) { - DownloadGroupSchedulers.getInstance().unRegister(obj); + TaskSchedulers.getInstance().unRegister(obj); } } } @@ -454,7 +453,7 @@ public class DownloadReceiver extends AbsReceiver { * 如果文件已经下载完成,只删除下载记录 */ public void removeAllTask(boolean removeFile) { - final AriaManager ariaManager = AriaManager.getInstance(AriaManager.APP); + final AriaManager ariaManager = AriaManager.getInstance(); CancelAllCmd cancelCmd = (CancelAllCmd) CommonUtil.createNormalCmd(new DTaskWrapper(null), NormalCmdFactory.TASK_CANCEL_ALL, ITask.DOWNLOAD); 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 abee2b4f..685682c6 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 @@ -39,7 +39,7 @@ public class DownloadTask extends AbsNormalTask { private DownloadTask(DTaskWrapper taskWrapper, Handler outHandler) { mTaskWrapper = taskWrapper; mOutHandler = outHandler; - mContext = AriaManager.APP; + mContext = AriaManager.getInstance().getAPP(); if (taskWrapper.getRequestType() == AbsTaskWrapper.M3U8_VOD || taskWrapper.getRequestType() == AbsTaskWrapper.M3U8_LIVE) { mListener = new M3U8Listener(this, mOutHandler); diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/FtpStartTarget.java b/Aria/src/main/java/com/arialyy/aria/core/download/FtpBuilderTarget.java similarity index 85% rename from Aria/src/main/java/com/arialyy/aria/core/download/FtpStartTarget.java rename to Aria/src/main/java/com/arialyy/aria/core/download/FtpBuilderTarget.java index a2b8813e..ae29e75a 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/FtpStartTarget.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/FtpBuilderTarget.java @@ -17,7 +17,7 @@ package com.arialyy.aria.core.download; import androidx.annotation.CheckResult; import androidx.annotation.NonNull; -import com.arialyy.aria.core.common.AbsStartTarget; +import com.arialyy.aria.core.common.AbsBuilderTarget; import com.arialyy.aria.core.common.Suggest; import com.arialyy.aria.core.common.ftp.FtpDelegate; import com.arialyy.aria.core.inf.ITaskWrapper; @@ -26,10 +26,10 @@ import com.arialyy.aria.util.CommonUtil; /** * 处理ftp第一次下载等逻辑 */ -public class FtpStartTarget extends AbsStartTarget { - private DNormalConfigHandler mConfigHandler; +public class FtpBuilderTarget extends AbsBuilderTarget { + private DNormalConfigHandler mConfigHandler; - FtpStartTarget(String url, String targetName) { + FtpBuilderTarget(String url, String targetName) { mConfigHandler = new DNormalConfigHandler<>(this, -1, targetName); mConfigHandler.setUrl(url); getTaskWrapper().asFtp().setUrlEntity(CommonUtil.getFtpUrlInfo(url)); @@ -40,7 +40,7 @@ public class FtpStartTarget extends AbsStartTarget { * 设置登陆、字符串编码、ftps等参数 */ @CheckResult(suggest = Suggest.TASK_CONTROLLER) - public FtpDelegate option() { + public FtpDelegate option() { return new FtpDelegate<>(this, getTaskWrapper()); } @@ -51,7 +51,7 @@ public class FtpStartTarget extends AbsStartTarget { * 2、如果保存路径是文件夹路径,如:/mnt/sdcard/,则使用FTP服务器该文件的文件名 */ @CheckResult(suggest = Suggest.TASK_CONTROLLER) - public FtpStartTarget setFilePath(@NonNull String filePath) { + public FtpBuilderTarget setFilePath(@NonNull String filePath) { int lastIndex = mConfigHandler.getUrl().lastIndexOf("/"); getEntity().setFileName(mConfigHandler.getUrl().substring(lastIndex + 1)); mConfigHandler.setTempFilePath(filePath); @@ -67,7 +67,7 @@ public class FtpStartTarget extends AbsStartTarget { * @param forceDownload {@code true}强制下载,不考虑文件路径是否被占用 */ @CheckResult(suggest = Suggest.TASK_CONTROLLER) - public FtpStartTarget setFilePath(@NonNull String filePath, boolean forceDownload) { + public FtpBuilderTarget setFilePath(@NonNull String filePath, boolean forceDownload) { mConfigHandler.setTempFilePath(filePath); mConfigHandler.setForceDownload(forceDownload); return this; diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/FtpDirStartTarget.java b/Aria/src/main/java/com/arialyy/aria/core/download/FtpDirBuilderTarget.java similarity index 85% rename from Aria/src/main/java/com/arialyy/aria/core/download/FtpDirStartTarget.java rename to Aria/src/main/java/com/arialyy/aria/core/download/FtpDirBuilderTarget.java index 9d65e2b0..0b0f0b7a 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/FtpDirStartTarget.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/FtpDirBuilderTarget.java @@ -16,7 +16,7 @@ package com.arialyy.aria.core.download; import androidx.annotation.CheckResult; -import com.arialyy.aria.core.common.AbsStartTarget; +import com.arialyy.aria.core.common.AbsBuilderTarget; import com.arialyy.aria.core.common.Suggest; import com.arialyy.aria.core.common.ftp.FtpDelegate; import com.arialyy.aria.core.manager.SubTaskManager; @@ -26,10 +26,10 @@ import com.arialyy.aria.util.CommonUtil; * Created by Aria.Lao on 2017/7/26. * ftp文件夹下载 */ -public class FtpDirStartTarget extends AbsStartTarget { - private FtpDirConfigHandler mConfigHandler; +public class FtpDirBuilderTarget extends AbsBuilderTarget { + private FtpDirConfigHandler mConfigHandler; - FtpDirStartTarget(String url, String targetName) { + FtpDirBuilderTarget(String url, String targetName) { setTargetName(targetName); mConfigHandler = new FtpDirConfigHandler<>(this, -1); getEntity().setGroupHash(url); @@ -56,7 +56,7 @@ public class FtpDirStartTarget extends AbsStartTarget { * @param dirPath 任务组保存文件夹路径 */ @CheckResult(suggest = Suggest.TASK_CONTROLLER) - public FtpDirStartTarget setDirPath(String dirPath) { + public FtpDirBuilderTarget setDirPath(String dirPath) { return mConfigHandler.setDirPath(dirPath); } @@ -64,7 +64,7 @@ public class FtpDirStartTarget extends AbsStartTarget { * 设置任务组别名 */ @CheckResult(suggest = Suggest.TASK_CONTROLLER) - public FtpDirStartTarget setGroupAlias(String alias) { + public FtpDirBuilderTarget setGroupAlias(String alias) { mConfigHandler.setGroupAlias(alias); return this; } @@ -73,7 +73,7 @@ public class FtpDirStartTarget extends AbsStartTarget { * 设置登陆、字符串编码、ftps等参数 */ @CheckResult(suggest = Suggest.TASK_CONTROLLER) - public FtpDelegate option() { + public FtpDelegate option() { return new FtpDelegate<>(this, getTaskWrapper()); } diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/GroupStartTarget.java b/Aria/src/main/java/com/arialyy/aria/core/download/GroupBuilderTarget.java similarity index 86% rename from Aria/src/main/java/com/arialyy/aria/core/download/GroupStartTarget.java rename to Aria/src/main/java/com/arialyy/aria/core/download/GroupBuilderTarget.java index 9c71102c..89fcabe3 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/GroupStartTarget.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/GroupBuilderTarget.java @@ -16,7 +16,7 @@ package com.arialyy.aria.core.download; import androidx.annotation.CheckResult; -import com.arialyy.aria.core.common.AbsStartTarget; +import com.arialyy.aria.core.common.AbsBuilderTarget; import com.arialyy.aria.core.common.Suggest; import com.arialyy.aria.core.common.http.HttpDelegate; import com.arialyy.aria.core.inf.IHttpFileLenAdapter; @@ -29,10 +29,10 @@ import java.util.List; * Created by AriaL on 2017/6/29. * 下载任务组 */ -public class GroupStartTarget extends AbsStartTarget { - private HttpGroupConfigHandler mConfigHandler; +public class GroupBuilderTarget extends AbsBuilderTarget { + private HttpGroupConfigHandler mConfigHandler; - GroupStartTarget(List urls, String targetName) { + GroupBuilderTarget(List urls, String targetName) { setTargetName(targetName); mConfigHandler = new HttpGroupConfigHandler<>(this, -1); getTaskWrapper().setRequestType(ITaskWrapper.DG_HTTP); @@ -43,7 +43,7 @@ public class GroupStartTarget extends AbsStartTarget { * 设置http请求参数,header等信息 */ @CheckResult(suggest = Suggest.TASK_CONTROLLER) - public HttpDelegate option() { + public HttpDelegate option() { return new HttpDelegate<>(this, getTaskWrapper()); } @@ -56,7 +56,7 @@ public class GroupStartTarget extends AbsStartTarget { * @param fileSize 任务组总大小 */ @CheckResult(suggest = Suggest.TASK_CONTROLLER) - public GroupStartTarget setFileSize(long fileSize) { + public GroupBuilderTarget setFileSize(long fileSize) { if (fileSize <= 0) { ALog.e(TAG, "文件大小不能小于 0"); return this; @@ -75,7 +75,7 @@ public class GroupStartTarget extends AbsStartTarget { * 3、由于网络或其它原因的存在,这种方式获取的组合任务大小有可能是不准确的。 */ @CheckResult(suggest = Suggest.TASK_CONTROLLER) - public GroupStartTarget unknownSize() { + public GroupBuilderTarget unknownSize() { ((DGTaskWrapper) getTaskWrapper()).setUnknownSize(true); return this; } @@ -96,7 +96,7 @@ public class GroupStartTarget extends AbsStartTarget { * @deprecated {@link #setSubFileName(List)} 请使用该api */ @CheckResult(suggest = Suggest.TASK_CONTROLLER) - @Deprecated public GroupStartTarget setSubTaskFileName(List subTaskFileName) { + @Deprecated public GroupBuilderTarget setSubTaskFileName(List subTaskFileName) { return setSubFileName(subTaskFileName); } @@ -104,7 +104,7 @@ public class GroupStartTarget extends AbsStartTarget { * 设置任务组别名 */ @CheckResult(suggest = Suggest.TASK_CONTROLLER) - public GroupStartTarget setGroupAlias(String alias) { + public GroupBuilderTarget setGroupAlias(String alias) { mConfigHandler.setGroupAlias(alias); return this; } @@ -129,7 +129,7 @@ public class GroupStartTarget extends AbsStartTarget { * @param dirPath 任务组保存文件夹路径 */ @CheckResult(suggest = Suggest.TASK_CONTROLLER) - public GroupStartTarget setDirPath(String dirPath) { + public GroupBuilderTarget setDirPath(String dirPath) { return mConfigHandler.setDirPath(dirPath); } @@ -137,7 +137,7 @@ public class GroupStartTarget extends AbsStartTarget { * 设置子任务文件名,该方法必须在{@link #setDirPath(String)}之后调用,否则不生效 */ @CheckResult(suggest = Suggest.TASK_CONTROLLER) - public GroupStartTarget setSubFileName(List subTaskFileName) { + public GroupBuilderTarget setSubFileName(List subTaskFileName) { return mConfigHandler.setSubFileName(subTaskFileName); } @@ -145,7 +145,7 @@ public class GroupStartTarget extends AbsStartTarget { * 如果你需要使用header中特定的key来设置文件长度,或有定制文件长度的需要,那么你可以通过该方法自行处理文件长度 */ @CheckResult(suggest = Suggest.TASK_CONTROLLER) - public GroupStartTarget setFileLenAdapter(IHttpFileLenAdapter adapter) { + public GroupBuilderTarget setFileLenAdapter(IHttpFileLenAdapter adapter) { if (adapter == null) { throw new IllegalArgumentException("adapter为空"); diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/HttpStartTarget.java b/Aria/src/main/java/com/arialyy/aria/core/download/HttpBuilderTarget.java similarity index 83% rename from Aria/src/main/java/com/arialyy/aria/core/download/HttpStartTarget.java rename to Aria/src/main/java/com/arialyy/aria/core/download/HttpBuilderTarget.java index 0ac74b1c..3bde5bd8 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/HttpStartTarget.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/HttpBuilderTarget.java @@ -17,24 +17,24 @@ package com.arialyy.aria.core.download; import androidx.annotation.CheckResult; import androidx.annotation.NonNull; -import com.arialyy.aria.core.common.AbsStartTarget; +import com.arialyy.aria.core.common.AbsBuilderTarget; 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 { +public class HttpBuilderTarget extends AbsBuilderTarget { - private DNormalConfigHandler mConfigHandler; + private DNormalConfigHandler mConfigHandler; - HttpStartTarget(String url, String targetName) { + HttpBuilderTarget(String url, String targetName) { mConfigHandler = new DNormalConfigHandler<>(this, -1, targetName); getTaskWrapper().setRequestType(ITaskWrapper.D_HTTP); mConfigHandler.setUrl(url); } - @CheckResult(suggest = Suggest.TASK_CONTROLLER) public M3U8Delegate asM3U8() { + @CheckResult(suggest = Suggest.TASK_CONTROLLER) public M3U8Delegate asM3U8() { return new M3U8Delegate<>(this, getTaskWrapper()); } @@ -42,7 +42,7 @@ public class HttpStartTarget extends AbsStartTarget { * 设置http请求参数,header等信息 */ @CheckResult(suggest = Suggest.TASK_CONTROLLER) - public HttpDelegate option() { + public HttpDelegate option() { return new HttpDelegate<>(this, getTaskWrapper()); } @@ -53,7 +53,7 @@ public class HttpStartTarget extends AbsStartTarget { * @param use {@code true} 使用 */ @CheckResult(suggest = Suggest.TASK_CONTROLLER) - public HttpStartTarget useServerFileName(boolean use) { + public HttpBuilderTarget useServerFileName(boolean use) { getTaskWrapper().asHttp().setUseServerFileName(use); return this; } @@ -66,7 +66,7 @@ public class HttpStartTarget extends AbsStartTarget { * @param filePath 路径必须为文件路径,不能为文件夹路径 */ @CheckResult(suggest = Suggest.TASK_CONTROLLER) - public HttpStartTarget setFilePath(@NonNull String filePath) { + public HttpBuilderTarget setFilePath(@NonNull String filePath) { mConfigHandler.setTempFilePath(filePath); return this; } @@ -80,7 +80,7 @@ public class HttpStartTarget extends AbsStartTarget { * @param forceDownload {@code true}强制下载,不考虑文件路径是否被占用 */ @CheckResult(suggest = Suggest.TASK_CONTROLLER) - public HttpStartTarget setFilePath(@NonNull String filePath, boolean forceDownload) { + public HttpBuilderTarget setFilePath(@NonNull String filePath, boolean forceDownload) { mConfigHandler.setTempFilePath(filePath); mConfigHandler.setForceDownload(forceDownload); return this; @@ -90,7 +90,7 @@ public class HttpStartTarget extends AbsStartTarget { * 如果你需要使用header中特定的key来设置文件长度,或有定制文件长度的需要,那么你可以通过该方法自行处理文件长度 */ @CheckResult(suggest = Suggest.TASK_CONTROLLER) - public HttpStartTarget setFileLenAdapter(IHttpFileLenAdapter adapter) { + public HttpBuilderTarget setFileLenAdapter(IHttpFileLenAdapter adapter) { if (adapter == null) { throw new IllegalArgumentException("adapter为空"); } diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/ConnectionHelp.java b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/ConnectionHelp.java index 587510bb..9124c5a6 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/ConnectionHelp.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/ConnectionHelp.java @@ -109,7 +109,7 @@ public class ConnectionHelp { urlConn = url.openConnection(); } if (urlConn instanceof HttpsURLConnection) { - AriaManager manager = AriaManager.getInstance(AriaManager.APP); + AriaManager manager = AriaManager.getInstance(); conn = (HttpsURLConnection) urlConn; SSLContext sslContext = SSLContextUtil.getSSLContextFromAssets(manager.getDownloadConfig().getCaName(), diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/FtpFileInfoThread.java b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/FtpFileInfoThread.java index 8e825ff2..a3f81b8c 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/FtpFileInfoThread.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/FtpFileInfoThread.java @@ -22,7 +22,8 @@ import com.arialyy.aria.core.common.ftp.AbsFtpInfoThread; import com.arialyy.aria.core.download.DTaskWrapper; import com.arialyy.aria.core.download.DownloadEntity; import com.arialyy.aria.exception.AriaIOException; -import com.arialyy.aria.util.CommonUtil; +import com.arialyy.aria.util.ALog; +import com.arialyy.aria.util.FileUtil; /** * Created by Aria.Lao on 2017/7/25. @@ -37,7 +38,7 @@ class FtpFileInfoThread extends AbsFtpInfoThread { @Override protected void handleFile(String remotePath, FTPFile ftpFile) { super.handleFile(remotePath, ftpFile); - if (!CommonUtil.checkSDMemorySpace(mEntity.getFilePath(), ftpFile.getSize())) { + if (!FileUtil.checkSDMemorySpace(mEntity.getFilePath(), ftpFile.getSize())) { mCallback.onFail(mEntity, new AriaIOException(TAG, String.format("获取ftp文件信息失败,内存空间不足, filePath: %s", mEntity.getFilePath())), false); @@ -49,6 +50,7 @@ class FtpFileInfoThread extends AbsFtpInfoThread { } @Override protected void onPreComplete(int code) { + ALog.i(TAG, "FTP下载预处理完成"); super.onPreComplete(code); if (mSize != mTaskWrapper.getEntity().getFileSize()) { mTaskWrapper.setNewTask(true); diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/FtpThreadTask.java b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/FtpThreadTask.java index ed93b711..a67e9e10 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/FtpThreadTask.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/FtpThreadTask.java @@ -105,13 +105,10 @@ class FtpThreadTask extends AbsFtpThreadTask { if (is != null) { is.close(); } - if (client != null && client.isConnected()) { - client.disconnect(); - } } catch (IOException e) { e.printStackTrace(); } - + closeClient(client); onThreadComplete(); } return this; 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 40e1b13b..857a7268 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 @@ -33,6 +33,7 @@ import com.arialyy.aria.exception.TaskException; import com.arialyy.aria.util.ALog; import com.arialyy.aria.util.CheckUtil; import com.arialyy.aria.util.CommonUtil; +import com.arialyy.aria.util.FileUtil; import com.arialyy.aria.util.RecordUtil; import java.io.BufferedReader; import java.io.File; @@ -66,7 +67,7 @@ public class HttpFileInfoThread implements Runnable { this.mTaskWrapper = taskWrapper; mEntity = taskWrapper.getEntity(); mConnectTimeOut = - AriaManager.getInstance(AriaManager.APP).getDownloadConfig().getConnectTimeOut(); + AriaManager.getInstance().getDownloadConfig().getConnectTimeOut(); onFileInfoCallback = callback; mTaskDelegate = taskWrapper.asHttp(); } @@ -121,7 +122,7 @@ public class HttpFileInfoThread implements Runnable { } long len = lenAdapter.handleFileLen(conn.getHeaderFields()); - if (!CommonUtil.checkSDMemorySpace(mEntity.getFilePath(), len)) { + if (!FileUtil.checkSDMemorySpace(mEntity.getFilePath(), len)) { failDownload(new TaskException(TAG, String.format("下载失败,内存空间不足;filePath: %s, url: %s", mEntity.getDownloadPath(), mEntity.getUrl())), false); diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/group/SimpleSchedulers.java b/Aria/src/main/java/com/arialyy/aria/core/download/group/SimpleSchedulers.java index 6e4643dd..59633f6d 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/group/SimpleSchedulers.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/group/SimpleSchedulers.java @@ -87,7 +87,7 @@ class SimpleSchedulers implements ISchedulers { boolean isNotNetRetry = config.appCfg.isNotNetRetry(); final int reTryNum = num; - if ((!NetUtils.isConnected(AriaManager.APP) && !isNotNetRetry) + if ((!NetUtils.isConnected(AriaManager.getInstance().getAPP()) && !isNotNetRetry) || loaderUtil.getDownloader() == null // 如果获取不到文件信息,loader为空 || loaderUtil.getEntity().getFailNum() > reTryNum) { mQueue.removeTaskFromExecQ(loaderUtil); diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/m3u8/BaseM3U8Loader.java b/Aria/src/main/java/com/arialyy/aria/core/download/m3u8/BaseM3U8Loader.java index 3cd54b50..e4312db9 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/m3u8/BaseM3U8Loader.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/m3u8/BaseM3U8Loader.java @@ -19,8 +19,16 @@ import com.arialyy.aria.core.common.AbsFileer; import com.arialyy.aria.core.download.DTaskWrapper; import com.arialyy.aria.core.download.DownloadEntity; import com.arialyy.aria.core.inf.IEventListener; +import com.arialyy.aria.util.ALog; import com.arialyy.aria.util.FileUtil; +import java.io.BufferedReader; import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStreamReader; +import java.nio.charset.Charset; public abstract class BaseM3U8Loader extends AbsFileer { @@ -50,4 +58,62 @@ public abstract class BaseM3U8Loader extends AbsFileer partPath = new ArrayList<>(); diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/m3u8/M3U8TaskConfig.java b/Aria/src/main/java/com/arialyy/aria/core/download/m3u8/M3U8TaskConfig.java index 8be1e550..cad5398e 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/m3u8/M3U8TaskConfig.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/m3u8/M3U8TaskConfig.java @@ -15,6 +15,7 @@ */ package com.arialyy.aria.core.download.m3u8; +import java.lang.ref.SoftReference; import java.lang.ref.WeakReference; import java.util.List; @@ -31,7 +32,7 @@ public class M3U8TaskConfig { /** * #EXTINF 标签信息处理器 */ - private WeakReference vodUrlConverter; + private SoftReference vodUrlConverter; /** * 缓存目录 @@ -199,7 +200,7 @@ public class M3U8TaskConfig { } public void setVodUrlConverter(IVodTsUrlConverter vodUrlConverter) { - this.vodUrlConverter = new WeakReference<>(vodUrlConverter); + this.vodUrlConverter = new SoftReference<>(vodUrlConverter); } public List getUrls() { diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/m3u8/M3U8VodDelegate.java b/Aria/src/main/java/com/arialyy/aria/core/download/m3u8/M3U8VodDelegate.java index deaf23f7..c69da6d4 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/m3u8/M3U8VodDelegate.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/m3u8/M3U8VodDelegate.java @@ -110,6 +110,6 @@ public class M3U8VodDelegate extends BaseDelegate { /** * 重置状态,将任务状态设置为未开始状态 * 注意:如果在后续方法调用链中没有调用 {@link NormalController#stop()}、{@link NormalController#cancel()}、 - * {@link NormalController#resume()}、{@link StartController#create()}、{@link StartController#add()} + * {@link NormalController#resume()}、{@link BuilderController#create()}、{@link BuilderController#add()} * 等操作任务的方法,那么你需要调用{@link NormalController#save()}才能将修改保存到数据库 */ @CheckResult(suggest = "after use #create()、#stop()、#cancel()、#resume()、#save()?") diff --git a/Aria/src/main/java/com/arialyy/aria/core/manager/ThreadTaskManager.java b/Aria/src/main/java/com/arialyy/aria/core/manager/ThreadTaskManager.java index 1ba8a37c..7d4ad2d0 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/manager/ThreadTaskManager.java +++ b/Aria/src/main/java/com/arialyy/aria/core/manager/ThreadTaskManager.java @@ -52,6 +52,32 @@ public class ThreadTaskManager { mExePool = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors()); } + /** + * 删除所有线程任务 + */ + public void removeAllThreadTask() { + if (mThreadTasks.isEmpty()) { + return; + } + try { + LOCK.tryLock(2, TimeUnit.SECONDS); + for (Set threads : mThreadTasks.values()) { + for (FutureContainer container : threads) { + if (container.future.isDone() || container.future.isCancelled()) { + continue; + } + container.threadTask.destroy(); + } + threads.clear(); + } + mThreadTasks.clear(); + } catch (InterruptedException e) { + e.printStackTrace(); + } 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 f932d019..f851fd9a 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 @@ -112,7 +112,7 @@ public abstract class AbsTaskQueue } @Override public int getOldMaxNum() { - return AriaManager.getInstance(AriaManager.APP).getDownloadConfig().oldMaxTaskNum; + return AriaManager.getInstance().getDownloadConfig().oldMaxTaskNum; } /** @@ -72,7 +72,7 @@ public class DownloadTaskQueue extends AbsTaskQueue } } } - int maxSize = AriaManager.getInstance(AriaManager.APP).getDownloadConfig().getMaxTaskNum(); + int maxSize = AriaManager.getInstance().getDownloadConfig().getMaxTaskNum(); int currentSize = mExecutePool.size(); if (currentSize == 0 || currentSize < maxSize) { startTask(task); @@ -102,7 +102,7 @@ public class DownloadTaskQueue extends AbsTaskQueue DownloadTask task = null; if (!mCachePool.taskExits(wrapper.getKey()) && !mExecutePool.taskExits(wrapper.getKey())) { task = (DownloadTask) TaskFactory.getInstance() - .createTask(wrapper, DownloadSchedulers.getInstance()); + .createTask(wrapper, TaskSchedulers.getInstance()); addTask(task); } else { ALog.w(TAG, "任务已存在"); @@ -117,6 +117,6 @@ public class DownloadTaskQueue extends AbsTaskQueue } @Override public int getMaxTaskNum() { - return AriaManager.getInstance(AriaManager.APP).getDownloadConfig().getMaxTaskNum(); + return AriaManager.getInstance().getDownloadConfig().getMaxTaskNum(); } } \ No newline at end of file diff --git a/Aria/src/main/java/com/arialyy/aria/core/queue/TaskFactory.java b/Aria/src/main/java/com/arialyy/aria/core/queue/TaskFactory.java index 4379ee61..80a38001 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/queue/TaskFactory.java +++ b/Aria/src/main/java/com/arialyy/aria/core/queue/TaskFactory.java @@ -22,9 +22,7 @@ import com.arialyy.aria.core.download.DownloadGroupTask; import com.arialyy.aria.core.download.DownloadTask; import com.arialyy.aria.core.inf.AbsTaskWrapper; import com.arialyy.aria.core.inf.ITask; -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.UTaskWrapper; import com.arialyy.aria.core.upload.UploadTask; @@ -56,7 +54,6 @@ class TaskFactory { * @param schedulers 对应的任务调度器 * @param {@link DTaskWrapper}、{@link UTaskWrapper}、{@link * DGTaskWrapper} - * @param {@link DownloadSchedulers}、{@link UploadSchedulers} * @return {@link DownloadTask}、{@link UploadTask}、{@link DownloadGroupTask} */ ITask createTask( diff --git a/Aria/src/main/java/com/arialyy/aria/core/queue/UploadTaskQueue.java b/Aria/src/main/java/com/arialyy/aria/core/queue/UploadTaskQueue.java index b2f541e4..a6b17733 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/queue/UploadTaskQueue.java +++ b/Aria/src/main/java/com/arialyy/aria/core/queue/UploadTaskQueue.java @@ -17,7 +17,7 @@ package com.arialyy.aria.core.queue; import com.arialyy.aria.core.AriaManager; -import com.arialyy.aria.core.scheduler.UploadSchedulers; +import com.arialyy.aria.core.scheduler.TaskSchedulers; import com.arialyy.aria.core.upload.UTaskWrapper; import com.arialyy.aria.core.upload.UploadTask; import com.arialyy.aria.util.ALog; @@ -46,11 +46,11 @@ public class UploadTaskQueue extends AbsTaskQueue { } @Override public int getOldMaxNum() { - return AriaManager.getInstance(AriaManager.APP).getUploadConfig().oldMaxTaskNum; + return AriaManager.getInstance().getUploadConfig().oldMaxTaskNum; } @Override public int getMaxTaskNum() { - return AriaManager.getInstance(AriaManager.APP).getUploadConfig().getMaxTaskNum(); + return AriaManager.getInstance().getUploadConfig().getMaxTaskNum(); } @Override public UploadTask createTask(UTaskWrapper wrapper) { @@ -58,7 +58,7 @@ public class UploadTaskQueue extends AbsTaskQueue { UploadTask task = null; if (!mCachePool.taskExits(wrapper.getKey()) && !mExecutePool.taskExits(wrapper.getKey())) { task = (UploadTask) TaskFactory.getInstance() - .createTask(wrapper, UploadSchedulers.getInstance()); + .createTask(wrapper, TaskSchedulers.getInstance()); addTask(task); } else { ALog.w(TAG, "任务已存在"); diff --git a/Aria/src/main/java/com/arialyy/aria/core/queue/pool/DGLoadExecutePool.java b/Aria/src/main/java/com/arialyy/aria/core/queue/pool/DGLoadExecutePool.java index e9ec61e8..f9e64b0a 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/queue/pool/DGLoadExecutePool.java +++ b/Aria/src/main/java/com/arialyy/aria/core/queue/pool/DGLoadExecutePool.java @@ -26,6 +26,6 @@ class DGLoadExecutePool extends DLoadExecutePool { private final String TAG = "DGLoadExecutePool"; @Override protected int getMaxSize() { - return AriaManager.getInstance(AriaManager.APP).getDGroupConfig().getMaxTaskNum(); + return AriaManager.getInstance().getDGroupConfig().getMaxTaskNum(); } } diff --git a/Aria/src/main/java/com/arialyy/aria/core/queue/pool/DLoadExecutePool.java b/Aria/src/main/java/com/arialyy/aria/core/queue/pool/DLoadExecutePool.java index 81e5f340..f43c2bb2 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/queue/pool/DLoadExecutePool.java +++ b/Aria/src/main/java/com/arialyy/aria/core/queue/pool/DLoadExecutePool.java @@ -30,7 +30,7 @@ class DLoadExecutePool extends BaseExecutePool { private final String TAG = "DownloadExecutePool"; @Override protected int getMaxSize() { - return AriaManager.getInstance(AriaManager.APP).getDownloadConfig().getMaxTaskNum(); + return AriaManager.getInstance().getDownloadConfig().getMaxTaskNum(); } @Override public boolean putTask(TASK task) { diff --git a/Aria/src/main/java/com/arialyy/aria/core/queue/pool/UploadExecutePool.java b/Aria/src/main/java/com/arialyy/aria/core/queue/pool/UploadExecutePool.java index b5437f2c..7988fc5b 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/queue/pool/UploadExecutePool.java +++ b/Aria/src/main/java/com/arialyy/aria/core/queue/pool/UploadExecutePool.java @@ -23,6 +23,6 @@ import com.arialyy.aria.core.inf.AbsTask; */ public class UploadExecutePool extends BaseExecutePool { @Override protected int getMaxSize() { - return AriaManager.getInstance(AriaManager.APP).getUploadConfig().getMaxTaskNum(); + return AriaManager.getInstance().getUploadConfig().getMaxTaskNum(); } } diff --git a/Aria/src/main/java/com/arialyy/aria/core/scheduler/DownloadGroupSchedulers.java b/Aria/src/main/java/com/arialyy/aria/core/scheduler/DownloadGroupSchedulers.java deleted file mode 100644 index 8a57f833..00000000 --- a/Aria/src/main/java/com/arialyy/aria/core/scheduler/DownloadGroupSchedulers.java +++ /dev/null @@ -1,44 +0,0 @@ -/* - * 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.scheduler; - -import com.arialyy.aria.core.download.DGTaskWrapper; -import com.arialyy.aria.core.download.DownloadGroupTask; -import com.arialyy.aria.core.queue.DownloadGroupTaskQueue; - -/** - * Created by AriaL on 2017/7/2. - * 任务组调度器 - */ -public class DownloadGroupSchedulers extends - AbsSchedulers { - private final String TAG = "DownloadGroupSchedulers"; - private static volatile DownloadGroupSchedulers INSTANCE = null; - - private DownloadGroupSchedulers() { - super(); - mQueue = DownloadGroupTaskQueue.getInstance(); - } - - public static DownloadGroupSchedulers getInstance() { - if (INSTANCE == null) { - synchronized (DownloadGroupSchedulers.class) { - INSTANCE = new DownloadGroupSchedulers(); - } - } - return INSTANCE; - } -} diff --git a/Aria/src/main/java/com/arialyy/aria/core/scheduler/DownloadSchedulers.java b/Aria/src/main/java/com/arialyy/aria/core/scheduler/DownloadSchedulers.java deleted file mode 100644 index 80c3c8af..00000000 --- a/Aria/src/main/java/com/arialyy/aria/core/scheduler/DownloadSchedulers.java +++ /dev/null @@ -1,46 +0,0 @@ -/* - * 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.scheduler; - -import com.arialyy.aria.core.download.DTaskWrapper; -import com.arialyy.aria.core.download.DownloadTask; -import com.arialyy.aria.core.queue.DownloadTaskQueue; - -/** - * Created by lyy on 2016/8/16. - * 任务下载器,提供抽象的方法供具体的实现类操作 - */ -public class DownloadSchedulers - extends AbsSchedulers { - - private final String TAG = "DownloadSchedulers"; - private static volatile DownloadSchedulers INSTANCE = null; - - private DownloadSchedulers() { - super(); - mQueue = DownloadTaskQueue.getInstance(); - } - - public static DownloadSchedulers getInstance() { - if (INSTANCE == null) { - synchronized (DownloadSchedulers.class) { - INSTANCE = new DownloadSchedulers(); - } - } - return INSTANCE; - } -} diff --git a/Aria/src/main/java/com/arialyy/aria/core/scheduler/FailureTaskHandler.java b/Aria/src/main/java/com/arialyy/aria/core/scheduler/FailureTaskHandler.java new file mode 100644 index 00000000..18c352f2 --- /dev/null +++ b/Aria/src/main/java/com/arialyy/aria/core/scheduler/FailureTaskHandler.java @@ -0,0 +1,124 @@ +/* + * 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.scheduler; + +import com.arialyy.aria.core.AriaManager; +import com.arialyy.aria.core.inf.AbsEntity; +import com.arialyy.aria.core.inf.ITask; +import com.arialyy.aria.core.manager.TaskWrapperManager; +import com.arialyy.aria.core.queue.ITaskQueue; +import com.arialyy.aria.util.ALog; +import com.arialyy.aria.util.CommonUtil; +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.ArrayBlockingQueue; +import java.util.concurrent.locks.Condition; +import java.util.concurrent.locks.ReentrantLock; + +/** + * 处理失败的任务 + */ +class FailureTaskHandler { + private final String TAG = CommonUtil.getClassName(getClass()); + + private static volatile FailureTaskHandler INSTANCE; + private final int MAX_EXE_NUM = 5; // 同时处理的失败任务数量 + private ArrayBlockingQueue mQueue = new ArrayBlockingQueue<>(100); + private List mExeQueue = new ArrayList<>(MAX_EXE_NUM); + private List mHashList = new ArrayList<>(); + private TaskSchedulers mSchedulers; + private final ReentrantLock LOCK = new ReentrantLock(); + private Condition mCondition = LOCK.newCondition(); + private AriaManager mAriaManager; + + static FailureTaskHandler init(TaskSchedulers schedulers) { + if (INSTANCE == null) { + synchronized (FailureTaskHandler.class) { + if (INSTANCE == null) { + INSTANCE = new FailureTaskHandler(schedulers); + } + } + } + return INSTANCE; + } + + private FailureTaskHandler(TaskSchedulers schedulers) { + mSchedulers = schedulers; + mAriaManager = AriaManager.getInstance(); + new Thread(new Runnable() { + @Override public void run() { + while (true) { + try { + TASK task = mQueue.take(); + if (mExeQueue.size() >= MAX_EXE_NUM) { + try { + LOCK.lock(); + mCondition.await(); + } finally { + LOCK.unlock(); + } + } else { + retryTask(task); + } + } catch (Exception e) { + e.printStackTrace(); + } + } + } + }).start(); + } + + private void retryTask(final TASK task) { + long interval = task.getTaskWrapper().getConfig().getReTryInterval(); + final int num = task.getTaskWrapper().getConfig().getReTryNum(); + final ITaskQueue queue = mSchedulers.getQueue(task); + mAriaManager.getAriaHandler().postDelayed(new Runnable() { + @Override public void run() { + AbsEntity entity = task.getTaskWrapper().getEntity(); + if (entity.getFailNum() <= num) { + ALog.d(TAG, String.format("任务【%s】开始重试", task.getTaskName())); + queue.reTryStart(task); + } else { + queue.removeTaskFormQueue(task.getKey()); + mSchedulers.startNextTask(queue, task.getSchedulerType()); + TaskWrapperManager.getInstance().removeTaskWrapper(task.getTaskWrapper()); + } + mExeQueue.remove(task); + int index = mHashList.indexOf(task.hashCode()); + if (index != -1) { + mHashList.remove(index); + } + if (LOCK.isLocked()) { + try { + LOCK.lock(); + mCondition.signalAll(); + } finally { + LOCK.unlock(); + } + } + } + }, interval); + } + + void offer(TASK task) { + int hashCode = task.hashCode(); + if (mHashList.contains(hashCode)) { + return; + } + mQueue.offer(task); + mHashList.add(hashCode); + } +} diff --git a/Aria/src/main/java/com/arialyy/aria/core/scheduler/AbsSchedulers.java b/Aria/src/main/java/com/arialyy/aria/core/scheduler/TaskSchedulers.java similarity index 82% rename from Aria/src/main/java/com/arialyy/aria/core/scheduler/AbsSchedulers.java rename to Aria/src/main/java/com/arialyy/aria/core/scheduler/TaskSchedulers.java index 6df8ad9d..33f8c7eb 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/scheduler/AbsSchedulers.java +++ b/Aria/src/main/java/com/arialyy/aria/core/scheduler/TaskSchedulers.java @@ -25,13 +25,15 @@ import com.arialyy.aria.core.download.DownloadTask; import com.arialyy.aria.core.inf.AbsEntity; import com.arialyy.aria.core.inf.AbsNormalEntity; import com.arialyy.aria.core.inf.AbsTask; -import com.arialyy.aria.core.inf.AbsTaskWrapper; import com.arialyy.aria.core.inf.GroupSendParams; import com.arialyy.aria.core.inf.IEntity; import com.arialyy.aria.core.inf.ITask; import com.arialyy.aria.core.inf.TaskSchedulerType; import com.arialyy.aria.core.manager.TaskWrapperManager; +import com.arialyy.aria.core.queue.DownloadGroupTaskQueue; +import com.arialyy.aria.core.queue.DownloadTaskQueue; import com.arialyy.aria.core.queue.ITaskQueue; +import com.arialyy.aria.core.queue.UploadTaskQueue; import com.arialyy.aria.core.upload.UploadTask; import com.arialyy.aria.util.ALog; import com.arialyy.aria.util.NetUtils; @@ -39,23 +41,52 @@ import java.util.Iterator; import java.util.Map; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.ScheduledThreadPoolExecutor; -import java.util.concurrent.TimeUnit; /** * Created by lyy on 2017/6/4. 事件调度器,用于处理任务状态的调度 */ -abstract class AbsSchedulers> implements ISchedulers { +public class TaskSchedulers implements ISchedulers { private final String TAG = "AbsSchedulers"; - protected QUEUE mQueue; + private static volatile TaskSchedulers INSTANCE; + private static FailureTaskHandler mFailureTaskHandler; private Map> mObservers = new ConcurrentHashMap<>(); - private AriaManager manager; + private AriaManager mAriaManager; - AbsSchedulers() { - manager = AriaManager.getInstance(AriaManager.APP); + private TaskSchedulers() { + mAriaManager = AriaManager.getInstance(); + } + + public static TaskSchedulers getInstance() { + if (INSTANCE == null) { + synchronized (TaskSchedulers.class) { + if (INSTANCE == null) { + INSTANCE = new TaskSchedulers(); + mFailureTaskHandler = FailureTaskHandler.init(INSTANCE); + } + } + } + return INSTANCE; + } + + /** + * 通过任务类型获取任务队列 + * + * @param curTask 当前接收到的的任务 + */ + ITaskQueue getQueue(TASK curTask) { + int taskType = curTask.getTaskType(); + if (taskType == ITask.DOWNLOAD) { + return DownloadTaskQueue.getInstance(); + } + if (taskType == ITask.DOWNLOAD_GROUP) { + return DownloadGroupTaskQueue.getInstance(); + } + if (taskType == ITask.UPLOAD) { + return UploadTaskQueue.getInstance(); + } + throw new NullPointerException("任务类型错误,type = " + taskType); } /** @@ -194,11 +225,11 @@ abstract class AbsSchedulers reTryNum) { - mQueue.removeTaskFormQueue(task.getKey()); - startNextTask(task.getSchedulerType()); + if ((!NetUtils.isConnected(mAriaManager.getAPP()) && !isNotNetRetry) + || task.getTaskWrapper().getEntity().getFailNum() > num) { + queue.removeTaskFormQueue(task.getKey()); + startNextTask(queue, task.getSchedulerType()); TaskWrapperManager.getInstance().removeTaskWrapper(task.getTaskWrapper()); normalTaskCallback(FAIL, task); return; } - final ScheduledThreadPoolExecutor timer = new ScheduledThreadPoolExecutor(1); - - timer.schedule(new Runnable() { - @Override public void run() { - AbsEntity entity = task.getTaskWrapper().getEntity(); - if (entity.getFailNum() <= reTryNum) { - ALog.d(TAG, String.format("任务【%s】开始重试", task.getTaskName())); - TASK task = mQueue.getTask(entity.getKey()); - mQueue.reTryStart(task); - } else { - mQueue.removeTaskFormQueue(task.getKey()); - startNextTask(task.getSchedulerType()); - TaskWrapperManager.getInstance().removeTaskWrapper(task.getTaskWrapper()); - } - } - }, interval, TimeUnit.MILLISECONDS); + mFailureTaskHandler.offer(task); } /** * 启动下一个任务,条件:任务停止,取消下载,任务完成 */ - private void startNextTask(int schedulerType) { + void startNextTask(final ITaskQueue queue, int schedulerType) { if (schedulerType == TaskSchedulerType.TYPE_STOP_NOT_NEXT) { return; } - TASK newTask = mQueue.getNextTask(); + TASK newTask = (TASK) queue.getNextTask(); if (newTask == null) { - if (mQueue.getCurrentExePoolNum() == 0) { + if (queue.getCurrentExePoolNum() == 0) { ALog.i(TAG, "没有等待中的任务"); } return; } if (newTask.getState() == IEntity.STATE_WAIT) { - mQueue.startTask(newTask); + queue.startTask(newTask); } } } diff --git a/Aria/src/main/java/com/arialyy/aria/core/scheduler/UploadSchedulers.java b/Aria/src/main/java/com/arialyy/aria/core/scheduler/UploadSchedulers.java deleted file mode 100644 index 90b4158a..00000000 --- a/Aria/src/main/java/com/arialyy/aria/core/scheduler/UploadSchedulers.java +++ /dev/null @@ -1,44 +0,0 @@ -/* - * 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.scheduler; - -import com.arialyy.aria.core.queue.UploadTaskQueue; -import com.arialyy.aria.core.upload.UTaskWrapper; -import com.arialyy.aria.core.upload.UploadTask; - -/** - * Created by lyy on 2017/2/27. - * 上传任务调度器 - */ -public class UploadSchedulers extends AbsSchedulers { - private static final String TAG = "UploadSchedulers"; - private static volatile UploadSchedulers INSTANCE = null; - - private UploadSchedulers() { - super(); - mQueue = UploadTaskQueue.getInstance(); - } - - public static UploadSchedulers getInstance() { - if (INSTANCE == null) { - synchronized (UploadSchedulers.class) { - INSTANCE = new UploadSchedulers(); - } - } - - return INSTANCE; - } -} 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 156cc36b..b92446bb 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 @@ -47,16 +47,6 @@ public class CheckUEntityUtil implements ICheckEntityUtil { if (b) { mEntity.save(); } - if (mWrapper.asFtp().getUrlEntity() != null && mWrapper.asFtp().getUrlEntity().isFtps) { - if (TextUtils.isEmpty(mWrapper.asFtp().getUrlEntity().storePath)) { - ALog.e(TAG, "证书路径为空"); - return false; - } - if (TextUtils.isEmpty(mWrapper.asFtp().getUrlEntity().keyAlias)) { - ALog.e(TAG, "证书别名为空"); - return false; - } - } return b; } @@ -102,17 +92,14 @@ public class CheckUEntityUtil implements ICheckEntityUtil { } private boolean checkFtps() { - if (mWrapper.getRequestType() == ITaskWrapper.U_FTP && mWrapper.asFtp() - .getUrlEntity().isFtps) { - if (TextUtils.isEmpty(mWrapper.asFtp().getUrlEntity().storePath)) { - ALog.e(TAG, "证书路径为空"); - return false; - } - if (TextUtils.isEmpty(mWrapper.asFtp().getUrlEntity().keyAlias)) { - ALog.e(TAG, "证书别名为空"); - return false; - } - } + //if (mWrapper.getRequestType() == ITaskWrapper.U_FTP && mWrapper.asFtp() + // .getUrlEntity().isFtps) { + // String ftpUrl = mEntity.getUrl(); + // if (!ftpUrl.startsWith("ftps") && !ftpUrl.startsWith("sftp")) { + // ALog.e(TAG, String.format("地址【%s】错误,ftps地址开头必须是:ftps或sftp", ftpUrl)); + // return false; + // } + //} return true; } } diff --git a/Aria/src/main/java/com/arialyy/aria/core/upload/FtpStartTarget.java b/Aria/src/main/java/com/arialyy/aria/core/upload/FtpBuilderTarget.java similarity index 80% rename from Aria/src/main/java/com/arialyy/aria/core/upload/FtpStartTarget.java rename to Aria/src/main/java/com/arialyy/aria/core/upload/FtpBuilderTarget.java index 721858c0..3bc48816 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/upload/FtpStartTarget.java +++ b/Aria/src/main/java/com/arialyy/aria/core/upload/FtpBuilderTarget.java @@ -17,7 +17,7 @@ package com.arialyy.aria.core.upload; import androidx.annotation.CheckResult; import androidx.annotation.NonNull; -import com.arialyy.aria.core.common.AbsStartTarget; +import com.arialyy.aria.core.common.AbsBuilderTarget; import com.arialyy.aria.core.common.Suggest; import com.arialyy.aria.core.common.ftp.FtpDelegate; import com.arialyy.aria.core.common.ftp.IFtpUploadInterceptor; @@ -27,10 +27,10 @@ import com.arialyy.aria.core.inf.AbsTaskWrapper; * Created by Aria.Lao on 2017/7/27. * ftp单任务上传 */ -public class FtpStartTarget extends AbsStartTarget { - private UNormalConfigHandler mConfigHandler; +public class FtpBuilderTarget extends AbsBuilderTarget { + private UNormalConfigHandler mConfigHandler; - FtpStartTarget(String filePath, String targetName) { + FtpBuilderTarget(String filePath, String targetName) { mConfigHandler = new UNormalConfigHandler<>(this, -1, targetName); mConfigHandler.setFilePath(filePath); getTaskWrapper().setRequestType(AbsTaskWrapper.U_FTP); @@ -42,7 +42,7 @@ public class FtpStartTarget extends AbsStartTarget { * @param tempUrl 上传路径 */ @CheckResult(suggest = Suggest.TASK_CONTROLLER) - public FtpStartTarget setUploadUrl(String tempUrl) { + public FtpBuilderTarget setUploadUrl(String tempUrl) { mConfigHandler.setTempUrl(tempUrl); return this; } @@ -51,7 +51,7 @@ public class FtpStartTarget extends AbsStartTarget { * FTP文件上传拦截器,如果远端已有同名文件,可使用该拦截器控制覆盖文件或修改该文件上传到服务器端端的文件名 */ @CheckResult(suggest = Suggest.TASK_CONTROLLER) - public FtpStartTarget setUploadInterceptor(@NonNull IFtpUploadInterceptor uploadInterceptor) { + public FtpBuilderTarget setUploadInterceptor(@NonNull IFtpUploadInterceptor uploadInterceptor) { return mConfigHandler.setUploadInterceptor(uploadInterceptor); } @@ -59,7 +59,7 @@ public class FtpStartTarget extends AbsStartTarget { * 设置登陆、字符串编码、ftps等参数 */ @CheckResult(suggest = Suggest.TASK_CONTROLLER) - public FtpDelegate option() { + public FtpDelegate option() { return new FtpDelegate<>(this, getTaskWrapper()); } } diff --git a/Aria/src/main/java/com/arialyy/aria/core/upload/HttpStartTarget.java b/Aria/src/main/java/com/arialyy/aria/core/upload/HttpBuilderTarget.java similarity index 80% rename from Aria/src/main/java/com/arialyy/aria/core/upload/HttpStartTarget.java rename to Aria/src/main/java/com/arialyy/aria/core/upload/HttpBuilderTarget.java index cd77ecae..fe05c77f 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/upload/HttpStartTarget.java +++ b/Aria/src/main/java/com/arialyy/aria/core/upload/HttpBuilderTarget.java @@ -16,7 +16,7 @@ package com.arialyy.aria.core.upload; import androidx.annotation.CheckResult; -import com.arialyy.aria.core.common.AbsStartTarget; +import com.arialyy.aria.core.common.AbsBuilderTarget; import com.arialyy.aria.core.common.Suggest; import com.arialyy.aria.core.common.http.HttpDelegate; import com.arialyy.aria.core.inf.AbsTaskWrapper; @@ -25,10 +25,10 @@ import com.arialyy.aria.core.inf.AbsTaskWrapper; * Created by lyy on 2017/2/28. * http 单文件上传 */ -public class HttpStartTarget extends AbsStartTarget { - private UNormalConfigHandler mConfigHandler; +public class HttpBuilderTarget extends AbsBuilderTarget { + private UNormalConfigHandler mConfigHandler; - HttpStartTarget(String filePath, String targetName) { + HttpBuilderTarget(String filePath, String targetName) { mConfigHandler = new UNormalConfigHandler<>(this, -1, targetName); mConfigHandler.setFilePath(filePath); @@ -43,7 +43,7 @@ public class HttpStartTarget extends AbsStartTarget { * @param tempUrl 上传路径 */ @CheckResult(suggest = Suggest.TASK_CONTROLLER) - public HttpStartTarget setUploadUrl(String tempUrl) { + public HttpBuilderTarget setUploadUrl(String tempUrl) { mConfigHandler.setTempUrl(tempUrl); return this; } @@ -52,7 +52,7 @@ public class HttpStartTarget extends AbsStartTarget { * 设置http请求参数,header等信息 */ @CheckResult(suggest = Suggest.TASK_CONTROLLER) - public HttpDelegate option() { + public HttpDelegate option() { return new HttpDelegate<>(this, getTaskWrapper()); } } diff --git a/Aria/src/main/java/com/arialyy/aria/core/upload/UploadReceiver.java b/Aria/src/main/java/com/arialyy/aria/core/upload/UploadReceiver.java index aea9fca2..94a2ad0d 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/upload/UploadReceiver.java +++ b/Aria/src/main/java/com/arialyy/aria/core/upload/UploadReceiver.java @@ -22,14 +22,14 @@ import com.arialyy.annotations.TaskEnum; import com.arialyy.aria.core.AriaManager; import com.arialyy.aria.core.command.CancelAllCmd; import com.arialyy.aria.core.command.NormalCmdFactory; -import com.arialyy.aria.core.common.AbsStartTarget; +import com.arialyy.aria.core.common.AbsBuilderTarget; import com.arialyy.aria.core.common.ProxyHelper; import com.arialyy.aria.core.event.EventMsgUtil; import com.arialyy.aria.core.inf.AbsEntity; import com.arialyy.aria.core.inf.AbsReceiver; import com.arialyy.aria.core.inf.ITask; import com.arialyy.aria.core.inf.ReceiverType; -import com.arialyy.aria.core.scheduler.UploadSchedulers; +import com.arialyy.aria.core.scheduler.TaskSchedulers; import com.arialyy.aria.orm.DbEntity; import com.arialyy.aria.util.ALog; import com.arialyy.aria.util.CheckUtil; @@ -52,7 +52,7 @@ public class UploadReceiver extends AbsReceiver { */ @Deprecated public UploadReceiver setMaxSpeed(int maxSpeed) { - AriaManager.getInstance(AriaManager.APP).getUploadConfig().setMaxSpeed(maxSpeed); + AriaManager.getInstance().getUploadConfig().setMaxSpeed(maxSpeed); return this; } @@ -62,15 +62,15 @@ public class UploadReceiver extends AbsReceiver { * @param filePath 文件路径 */ @CheckResult - public HttpStartTarget load(@NonNull String filePath) { + public HttpBuilderTarget load(@NonNull String filePath) { CheckUtil.checkUploadPath(filePath); - return new HttpStartTarget(filePath, targetName); + return new HttpBuilderTarget(filePath, targetName); } /** * 用于任务停止、删除等操作 * - * @param taskId 任务id,可从{@link AbsStartTarget#create()}、{@link AbsStartTarget#add()}、{@link + * @param taskId 任务id,可从{@link AbsBuilderTarget#create()}、{@link AbsBuilderTarget#add()}、{@link * AbsEntity#getId()}读取任务id */ @CheckResult @@ -85,15 +85,15 @@ public class UploadReceiver extends AbsReceiver { * @param filePath 文件路径 */ @CheckResult - public FtpStartTarget loadFtp(@NonNull String filePath) { + public FtpBuilderTarget loadFtp(@NonNull String filePath) { CheckUtil.checkUploadPath(filePath); - return new FtpStartTarget(filePath, targetName); + return new FtpBuilderTarget(filePath, targetName); } /** * 加载FTP单文件上传任务,用于任务第一次上传,如果需要控制任务停止或删除等操作,请使用{@link #load(long)} * - * @param taskId 任务id,可从{@link AbsStartTarget#create()}、{@link AbsStartTarget#add()}、{@link + * @param taskId 任务id,可从{@link AbsBuilderTarget#create()}、{@link AbsBuilderTarget#add()}、{@link * AbsEntity#getId()}读取任务id */ @CheckResult @@ -226,7 +226,7 @@ public class UploadReceiver extends AbsReceiver { * 如果文件已经上传完成,只删除上传记录 */ public void removeAllTask(boolean removeFile) { - final AriaManager am = AriaManager.getInstance(AriaManager.APP); + final AriaManager am = AriaManager.getInstance(); CancelAllCmd cancelCmd = (CancelAllCmd) CommonUtil.createNormalCmd(new UTaskWrapper(null), NormalCmdFactory.TASK_CANCEL_ALL, ITask.UPLOAD); @@ -256,7 +256,7 @@ public class UploadReceiver extends AbsReceiver { if (set != null && !set.isEmpty()) { for (Integer type : set) { if (type == ProxyHelper.PROXY_TYPE_UPLOAD) { - UploadSchedulers.getInstance().register(obj, TaskEnum.UPLOAD); + TaskSchedulers.getInstance().register(obj, TaskEnum.UPLOAD); } } } else { @@ -272,7 +272,7 @@ public class UploadReceiver extends AbsReceiver { if (needRmListener) { unRegisterListener(); } - AriaManager.getInstance(AriaManager.APP).removeReceiver(OBJ_MAP.get(getKey())); + AriaManager.getInstance().removeReceiver(OBJ_MAP.get(getKey())); } @Override public String getType() { @@ -294,7 +294,7 @@ public class UploadReceiver extends AbsReceiver { if (set != null) { for (Integer integer : set) { if (integer == ProxyHelper.PROXY_TYPE_UPLOAD) { - UploadSchedulers.getInstance().unRegister(obj); + TaskSchedulers.getInstance().unRegister(obj); } } } 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 e681508f..a41e3f9a 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 @@ -101,12 +101,10 @@ class FtpThreadTask extends AbsFtpThreadTask { if (file != null) { file.close(); } - if (client != null && client.isConnected()) { - client.disconnect(); - } } catch (IOException e) { e.printStackTrace(); } + closeClient(client); onThreadComplete(); } return this; diff --git a/Aria/src/main/java/com/arialyy/aria/core/upload/uploader/HttpThreadTask.java b/Aria/src/main/java/com/arialyy/aria/core/upload/uploader/HttpThreadTask.java index 99263776..009c3283 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/upload/uploader/HttpThreadTask.java +++ b/Aria/src/main/java/com/arialyy/aria/core/upload/uploader/HttpThreadTask.java @@ -25,6 +25,7 @@ import com.arialyy.aria.core.upload.UploadEntity; import com.arialyy.aria.exception.BaseException; import com.arialyy.aria.exception.TaskException; import com.arialyy.aria.util.ALog; +import com.arialyy.aria.util.CommonUtil; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; @@ -36,6 +37,7 @@ import java.io.PrintWriter; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLConnection; +import java.util.Map; import java.util.Set; import java.util.UUID; @@ -65,8 +67,9 @@ class HttpThreadTask extends AbsThreadTask { } URL url; try { - url = new URL(getEntity().getUrl()); HttpTaskConfig taskDelegate = getTaskWrapper().asHttp(); + url = new URL(CommonUtil.convertUrl(getConfig().url)); + mHttpConn = (HttpURLConnection) url.openConnection(); mHttpConn.setRequestMethod(taskDelegate.getRequestEnum().name); mHttpConn.setUseCaches(false); @@ -90,11 +93,20 @@ class HttpThreadTask extends AbsThreadTask { mOutputStream = mHttpConn.getOutputStream(); PrintWriter writer = new PrintWriter(new OutputStreamWriter(mOutputStream, taskDelegate.getCharSet()), true); + // 添加参数 + Map params = taskDelegate.getParams(); + if (params != null && !params.isEmpty()) { + for (String key : params.keySet()) { + addFormField(writer, key, params.get(key)); + } + } + //添加文件上传表单字段 keys = taskDelegate.getFormFields().keySet(); for (String key : keys) { addFormField(writer, key, taskDelegate.getFormFields().get(key)); } + uploadFile(writer, taskDelegate.getAttachment(), uploadFile); getEntity().setResponseStr(finish(writer)); sendCompleteMsg(); diff --git a/Aria/src/main/java/com/arialyy/aria/core/upload/uploader/Uploader.java b/Aria/src/main/java/com/arialyy/aria/core/upload/uploader/Uploader.java index 396280be..32858fce 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/upload/uploader/Uploader.java +++ b/Aria/src/main/java/com/arialyy/aria/core/upload/uploader/Uploader.java @@ -37,7 +37,7 @@ class Uploader extends NormalFileer { super(listener, taskEntity); mTempFile = new File(mEntity.getFilePath()); setUpdateInterval( - AriaManager.getInstance(AriaManager.APP).getUploadConfig().getUpdateInterval()); + AriaManager.getInstance().getUploadConfig().getUpdateInterval()); } @Override protected boolean handleNewTask() { 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 0bb8dff0..a008628f 100644 --- a/Aria/src/main/java/com/arialyy/aria/orm/DelegateFind.java +++ b/Aria/src/main/java/com/arialyy/aria/orm/DelegateFind.java @@ -326,7 +326,13 @@ class DelegateFind extends AbsDelegate { String sql = String.format("SELECT rowid, * FROM %s WHERE %s", CommonUtil.getClassName(clazz), expression[0]); String[] params = new String[expression.length - 1]; - System.arraycopy(expression, 1, params, 0, params.length); + try { + // 处理系统出现的问题:https://github.com/AriaLyy/Aria/issues/450 + System.arraycopy(expression, 1, params, 0, params.length); + } catch (Exception e) { + e.printStackTrace(); + return null; + } return exeNormalDataSql(db, clazz, sql, params); } @@ -348,7 +354,13 @@ class DelegateFind extends AbsDelegate { expression[0], (page - 1) * num, num); String[] params = new String[expression.length - 1]; - System.arraycopy(expression, 1, params, 0, params.length); + try { + // 处理系统出现的问题:https://github.com/AriaLyy/Aria/issues/450 + System.arraycopy(expression, 1, params, 0, params.length); + } catch (Exception e) { + e.printStackTrace(); + return null; + } return exeNormalDataSql(db, clazz, sql, params); } diff --git a/Aria/src/main/java/com/arialyy/aria/util/CommonUtil.java b/Aria/src/main/java/com/arialyy/aria/util/CommonUtil.java index 7973ca0e..4eeec4c7 100644 --- a/Aria/src/main/java/com/arialyy/aria/util/CommonUtil.java +++ b/Aria/src/main/java/com/arialyy/aria/util/CommonUtil.java @@ -77,6 +77,7 @@ public class CommonUtil { long time = System.currentTimeMillis(); long timeD = time - lastClickTime; if (0 < timeD && timeD < 500) { + ALog.i(TAG, "操作太频繁了,缓一下吧~"); return true; } lastClickTime = time; @@ -95,54 +96,6 @@ public class CommonUtil { return new String(str.getBytes(charSet), SERVER_CHARSET); } - /** - * 检查SD内存空间是否充足 - * - * @param filePath 文件保存路径 - * @param fileSize 文件大小 - * @return {@code false} 内存空间不足,{@code true}内存空间足够 - */ - public static boolean checkSDMemorySpace(String filePath, long fileSize) { - List dirs = FileUtil.getSDPathList(AriaManager.APP); - if (dirs == null || dirs.isEmpty()) { - return true; - } - for (String path : dirs) { - if (filePath.contains(path)) { - if (fileSize > 0 && fileSize > getAvailableExternalMemorySize(path)) { - return false; - } - } - } - return true; - } - - /** - * sdcard 可用大小 - * - * @param sdcardPath sdcard 根路径 - * @return 单位为:byte - */ - public static long getAvailableExternalMemorySize(String sdcardPath) { - StatFs stat = new StatFs(sdcardPath); - long blockSize = stat.getBlockSize(); - long availableBlocks = stat.getAvailableBlocks(); - return availableBlocks * blockSize; - } - - /** - * sdcard 总大小 - * - * @param sdcardPath sdcard 根路径 - * @return 单位为:byte - */ - public static long getTotalExternalMemorySize(String sdcardPath) { - StatFs stat = new StatFs(sdcardPath); - long blockSize = stat.getBlockSize(); - long totalBlocks = stat.getBlockCount(); - return totalBlocks * blockSize; - } - /** * 获取某包下所有类 * @@ -818,7 +771,8 @@ public class CommonUtil { * @param fileName 文件名 */ public static String getFileConfigPath(boolean isDownload, String fileName) { - return AriaManager.APP.getFilesDir().getPath() + (isDownload ? AriaManager.DOWNLOAD_TEMP_DIR + return AriaManager.getInstance().getAPP().getFilesDir().getPath() + (isDownload + ? AriaManager.DOWNLOAD_TEMP_DIR : AriaManager.UPLOAD_TEMP_DIR) + fileName + ".properties"; } } \ No newline at end of file diff --git a/Aria/src/main/java/com/arialyy/aria/util/ErrorHelp.java b/Aria/src/main/java/com/arialyy/aria/util/ErrorHelp.java index 16d3e4f4..2fbee6a1 100644 --- a/Aria/src/main/java/com/arialyy/aria/util/ErrorHelp.java +++ b/Aria/src/main/java/com/arialyy/aria/util/ErrorHelp.java @@ -46,7 +46,8 @@ public class ErrorHelp { * @return "/mnt/sdcard/Android/data/{package_name}/files/log/*" */ private static String getLogPath() { - String path = String.format("%slog/AriaCrash_%s.log", CommonUtil.getAppPath(AriaManager.APP), + String path = String.format("%slog/AriaCrash_%s.log", + CommonUtil.getAppPath(AriaManager.getInstance().getAPP()), getData("yyyy-MM-dd_HH_mm_ss")); File log = new File(path); diff --git a/Aria/src/main/java/com/arialyy/aria/util/FileUtil.java b/Aria/src/main/java/com/arialyy/aria/util/FileUtil.java index fcfe3efd..937caba2 100644 --- a/Aria/src/main/java/com/arialyy/aria/util/FileUtil.java +++ b/Aria/src/main/java/com/arialyy/aria/util/FileUtil.java @@ -21,7 +21,11 @@ import android.app.ActivityManager; import android.content.Context; import android.os.Build; import android.os.Environment; +import android.os.StatFs; +import android.os.storage.StorageManager; +import android.os.storage.StorageVolume; import android.text.TextUtils; +import com.arialyy.aria.core.AriaManager; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; @@ -34,13 +38,11 @@ import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.SequenceInputStream; import java.io.Serializable; -import java.lang.reflect.Method; import java.nio.ByteBuffer; import java.nio.channels.Channels; import java.nio.channels.FileChannel; import java.nio.channels.ReadableByteChannel; import java.util.ArrayList; -import java.util.Arrays; import java.util.Collections; import java.util.Enumeration; import java.util.LinkedHashMap; @@ -149,7 +151,16 @@ public class FileUtil { ALog.e(TAG, "删除文件失败,路径为空"); return false; } - File file = new File(path); + return deleteFile(new File(path)); + } + + /** + * 删除文件 + * + * @param file 文件路径 + * @return {@code true}删除成功、{@code false}删除失败 + */ + public static boolean deleteFile(File file) { if (file.exists()) { final File to = new File(file.getAbsolutePath() + System.currentTimeMillis()); if (file.renameTo(to)) { @@ -345,6 +356,54 @@ public class FileUtil { } } + /** + * 检查SD内存空间是否充足 + * + * @param filePath 文件保存路径 + * @param fileSize 文件大小 + * @return {@code false} 内存空间不足,{@code true}内存空间足够 + */ + public static boolean checkSDMemorySpace(String filePath, long fileSize) { + List dirs = FileUtil.getSDPathList(AriaManager.getInstance().getAPP()); + if (dirs == null || dirs.isEmpty()) { + return true; + } + for (String path : dirs) { + if (filePath.contains(path)) { + if (fileSize > 0 && fileSize > getAvailableExternalMemorySize(path)) { + return false; + } + } + } + return true; + } + + /** + * sdcard 可用大小 + * + * @param sdcardPath sdcard 根路径 + * @return 单位为:byte + */ + public static long getAvailableExternalMemorySize(String sdcardPath) { + StatFs stat = new StatFs(sdcardPath); + long blockSize = stat.getBlockSize(); + long availableBlocks = stat.getAvailableBlocks(); + return availableBlocks * blockSize; + } + + /** + * sdcard 总大小 + * + * @param sdcardPath sdcard 根路径 + * @return 单位为:byte + */ + public static long getTotalExternalMemorySize(String sdcardPath) { + StatFs stat = new StatFs(sdcardPath); + long blockSize = stat.getBlockSize(); + long totalBlocks = stat.getBlockCount(); + return totalBlocks * blockSize; + } + /** * 获取SD卡目录列表 */ @@ -352,11 +411,6 @@ public class FileUtil { List paths; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { paths = getVolumeList(context); - if (paths == null || paths.isEmpty()) { - paths = getStorageDirectories(); - } - } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) { - paths = getVolumeList(context); } else { List mounts = readMountsFile(); List volds = readVoldFile(); @@ -417,17 +471,22 @@ public class FileUtil { * getSDPathList */ private static List getVolumeList(final Context context) { - List pathList = null; - try { - android.os.storage.StorageManager manager = - (android.os.storage.StorageManager) context.getSystemService(Context.STORAGE_SERVICE); - Method method = manager.getClass().getMethod("getVolumePaths"); - String[] paths = (String[]) method.invoke(manager); - pathList = Arrays.asList(paths); - } catch (Exception e) { - e.printStackTrace(); + List pathList = new ArrayList<>(); + + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { + StorageManager manager = (StorageManager) context.getSystemService(Context.STORAGE_SERVICE); + List volumes = manager.getStorageVolumes(); + for (StorageVolume volume : volumes) { + String state = volume.getState(); + if (state.equals(Environment.MEDIA_MOUNTED)) { + pathList.add(volume.toString()); + } + } + } else { + pathList.addAll(getStorageDirectories()); } - if (pathList == null || pathList.isEmpty()) { + + if (pathList.isEmpty()) { pathList = new ArrayList<>(); pathList.add(EXTERNAL_STORAGE_PATH); } @@ -588,7 +647,7 @@ public class FileUtil { private static List readVoldFile() { // read /etc/vold.conf or /etc/vold.fstab (it depends on version what - // config file is present) + // clientConfig file is present) List vold = null; Scanner scanner = null; try { diff --git a/Aria/src/main/java/com/arialyy/aria/util/NetUtils.java b/Aria/src/main/java/com/arialyy/aria/util/NetUtils.java index fd864e13..2200f03a 100644 --- a/Aria/src/main/java/com/arialyy/aria/util/NetUtils.java +++ b/Aria/src/main/java/com/arialyy/aria/util/NetUtils.java @@ -62,7 +62,7 @@ public class NetUtils { return true; } if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { - return AriaManager.getInstance(context).isConnectedNet(); + return AriaManager.getInstance().isConnectedNet(); } ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); diff --git a/Aria/src/main/java/com/arialyy/aria/util/SSLContextUtil.java b/Aria/src/main/java/com/arialyy/aria/util/SSLContextUtil.java index b6a9427d..a44f36ea 100644 --- a/Aria/src/main/java/com/arialyy/aria/util/SSLContextUtil.java +++ b/Aria/src/main/java/com/arialyy/aria/util/SSLContextUtil.java @@ -67,7 +67,7 @@ public class SSLContextUtil { if (sslContext != null) { return sslContext; } - InputStream caInput = AriaManager.APP.getAssets().open(caPath); + InputStream caInput = AriaManager.getInstance().getAPP().getAssets().open(caPath); Certificate ca = loadCert(caInput); return createContext(caAlias, ca, protocol, cacheKey); } catch (IOException | CertificateException e) { diff --git a/DEV_LOG.md b/DEV_LOG.md index b08873fc..70e98b6a 100644 --- a/DEV_LOG.md +++ b/DEV_LOG.md @@ -1,8 +1,16 @@ ## 开发日志 + v_3.7 + - fix bug https://github.com/AriaLyy/Aria/issues/450 + - fix bug https://github.com/AriaLyy/Aria/issues/466 + - fix bug https://github.com/AriaLyy/Aria/issues/454 + - 移除隐藏api的反射 https://github.com/AriaLyy/Aria/issues/456 + - 新增ftp免证书登陆功能h ttps://github.com/AriaLyy/Aria/issues/455 - 适配androidX - 修复组合任务,恢复下载,会出现进度显示为0的问题 - + v_3.6.6 + - m3u8点播下载新增创建ts索引功能 + - 修复多任务的m3u8点播下载时,一个任务调用`jumpIndex`,其它m3u8任务也会自动调用`jumpIndex`的问题 + - 添加权限检查 + + v_3.6.6 (2019/8/7) - fix bug https://github.com/AriaLyy/Aria/issues/426 - fix bug https://github.com/AriaLyy/Aria/issues/429 - fix bug https://github.com/AriaLyy/Aria/issues/428 diff --git a/README.md b/README.md index 1849c8fd..76cd3b40 100644 --- a/README.md +++ b/README.md @@ -47,8 +47,8 @@ Aria有以下特点: [![Compiler](https://api.bintray.com/packages/arialyy/maven/AriaCompiler/images/download.svg)](https://bintray.com/arialyy/maven/AriaCompiler/_latestVersion) ```java -compile 'com.arialyy.aria:aria-core:3.6.5' -annotationProcessor 'com.arialyy.aria:aria-compiler:3.6.5' +compile 'com.arialyy.aria:aria-core:3.6.6' +annotationProcessor 'com.arialyy.aria:aria-compiler:3.6.6' ``` 如果出现android support依赖错误,请将 `compile 'com.arialyy.aria:aria-core:'`替换为 ``` @@ -67,6 +67,7 @@ __⚠️注意:3.5.4以下版本升级时,需要更新[配置文件](https:/ + ``` @@ -127,25 +128,15 @@ protected void onCreate(Bundle savedInstanceState) { ### 版本日志 - + v_3.6.5 - - fix bug https://github.com/AriaLyy/Aria/issues/403 - - fix bug https://github.com/AriaLyy/Aria/issues/414 - - fix bug https://github.com/AriaLyy/Aria/issues/406 - - fix bug https://github.com/AriaLyy/Aria/issues/407 - - fix bug https://github.com/AriaLyy/Aria/issues/416 - - fix bug https://github.com/AriaLyy/Aria/issues/420 - - fix bug https://github.com/AriaLyy/Aria/issues/422 - - 新增ftp上传拦截器 https://github.com/AriaLyy/Aria/issues/402 - - 重构线程任务模块 - - 新增m3u8协议的文件下载 - - 修复拦截器可能出现的空指针问题 - - 移除`DownloadGroupEntity`字段`groupHash`的主键约束,`DownloadEntity`字段`groupHash`的外键约束,`TaskRecord`字段`dGroupHash`的外键约束 - - 优化关联查询的性能 - - 修复任务记录删除失败的问题 - - 优化网络连接状态获取的逻辑 - - 修复配置文件的某些配置失效的问题 - - 新增m3u8切片状态注解`@M3U8.onPeerStart`,`@M3U8.onPeerComplete`,`@M3U8.onPeerFail` - - 新增动态指定m3u8协议视频的下载功能(边下边播下载支持),[详情](https://aria.laoyuyu.me/aria_doc/download/m3u8_vod.html) + + v_3.6.6 + - fix bug https://github.com/AriaLyy/Aria/issues/426 + - fix bug https://github.com/AriaLyy/Aria/issues/429 + - fix bug https://github.com/AriaLyy/Aria/issues/428 + - fix bug https://github.com/AriaLyy/Aria/issues/427 + - fix bug https://github.com/AriaLyy/Aria/issues/431 + - fix bug https://github.com/AriaLyy/Aria/issues/441 + - 修复普通下载任务、组合任务共享执行队列、缓存池的问题 + - 修复组合任务启动失败时,`DownloadGroupEntity`的状态变为执行中的问题 [更多版本记录](https://github.com/AriaLyy/Aria/blob/master/DEV_LOG.md) diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index c736d221..b577538a 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -5,6 +5,7 @@ + - - - + + + @@ -42,6 +43,7 @@ + - + { + + public NormalToAdapter(Context context, List data) { + super(context, data); + } + + @Override protected NormalToAdapter.Holder getViewHolder(View convertView, int viewType) { + return new NormalToAdapter.Holder(convertView); + } + + @Override protected int setLayoutId(int type) { + return R.layout.item_download; + } + + @Override protected void bindData(NormalToAdapter.Holder holder, int position, NormalTo item) { + holder.text.setText(item.title); + holder.image.setImageResource(item.icon); + } + + class Holder extends AbsHolder { + TextView text; + AppCompatImageView image; + + Holder(View itemView) { + super(itemView); + text = findViewById(R.id.title); + image = findViewById(R.id.image); + } + } +} \ No newline at end of file diff --git a/app/src/main/java/com/arialyy/simple/core/download/ComponentActivity.java b/app/src/main/java/com/arialyy/simple/core/download/ComponentActivity.java new file mode 100644 index 00000000..a4f3c792 --- /dev/null +++ b/app/src/main/java/com/arialyy/simple/core/download/ComponentActivity.java @@ -0,0 +1,78 @@ +/* + * Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.arialyy.simple.core.download; + +import android.os.Bundle; +import android.view.View; +import androidx.annotation.Nullable; +import androidx.lifecycle.Observer; +import androidx.lifecycle.ViewModelProviders; +import androidx.recyclerview.widget.GridLayoutManager; +import androidx.recyclerview.widget.RecyclerView; +import com.arialyy.simple.MainActivity; +import com.arialyy.simple.R; +import com.arialyy.simple.base.BaseActivity; +import com.arialyy.simple.base.adapter.RvItemClickSupport; +import com.arialyy.simple.common.NormalToAdapter; +import com.arialyy.simple.core.download.fragment.FragmentActivity; +import com.arialyy.simple.databinding.ActivityComponentBinding; +import com.arialyy.simple.modlue.CommonModule; +import com.arialyy.simple.to.NormalTo; +import java.util.ArrayList; +import java.util.List; + +public class ComponentActivity extends BaseActivity { + @Override protected int setLayoutId() { + return R.layout.activity_component; + } + + @Override protected void init(Bundle savedInstanceState) { + super.init(savedInstanceState); + NormalTo to = getIntent().getParcelableExtra(MainActivity.KEY_MAIN_DATA); + setTitle(to.title); + + final List data = new ArrayList<>(); + getBinding().list.setLayoutManager(new GridLayoutManager(this, 2)); + final NormalToAdapter adapter = new NormalToAdapter(this, data); + getBinding().list.setAdapter(adapter); + final CommonModule module = ViewModelProviders.of(this).get(CommonModule.class); + + module.getComponentData(this).observe(this, + new Observer>() { + @Override public void onChanged(@Nullable List normalTos) { + if (normalTos != null) { + data.addAll(normalTos); + adapter.notifyDataSetChanged(); + } + } + }); + RvItemClickSupport.addTo(getBinding().list).setOnItemClickListener( + new RvItemClickSupport.OnItemClickListener() { + @Override public void onItemClicked(RecyclerView recyclerView, int position, View v) { + switch (position) { + case 0: + module.startNextActivity(ComponentActivity.this, data.get(position), + FragmentActivity.class); + break; + case 1: + DownloadDialog dialog = new DownloadDialog(ComponentActivity.this); + dialog.show(); + break; + } + } + }); + } +} diff --git a/app/src/main/java/com/arialyy/simple/core/download/DownloadActivity.java b/app/src/main/java/com/arialyy/simple/core/download/DownloadActivity.java index c72a5a0e..fa83b425 100644 --- a/app/src/main/java/com/arialyy/simple/core/download/DownloadActivity.java +++ b/app/src/main/java/com/arialyy/simple/core/download/DownloadActivity.java @@ -16,12 +16,9 @@ package com.arialyy.simple.core.download; -import android.content.Context; import android.os.Bundle; import android.view.View; -import android.widget.TextView; import androidx.annotation.Nullable; -import androidx.appcompat.widget.AppCompatImageView; import androidx.lifecycle.Observer; import androidx.lifecycle.ViewModelProviders; import androidx.recyclerview.widget.GridLayoutManager; @@ -29,10 +26,9 @@ import androidx.recyclerview.widget.RecyclerView; import com.arialyy.simple.MainActivity; import com.arialyy.simple.R; import com.arialyy.simple.base.BaseActivity; -import com.arialyy.simple.base.adapter.AbsHolder; -import com.arialyy.simple.base.adapter.AbsRVAdapter; import com.arialyy.simple.base.adapter.RvItemClickSupport; -import com.arialyy.simple.core.download.multi_download.MultiTaskActivity; +import com.arialyy.simple.common.NormalToAdapter; +import com.arialyy.simple.core.download.mutil.MultiTaskActivity; import com.arialyy.simple.databinding.ActivityDownloadMeanBinding; import com.arialyy.simple.modlue.CommonModule; import com.arialyy.simple.to.NormalTo; @@ -56,7 +52,7 @@ public class DownloadActivity extends BaseActivity final List data = new ArrayList<>(); getBinding().list.setLayoutManager(new GridLayoutManager(this, 2)); - final Adapter adapter = new Adapter(this, data); + final NormalToAdapter adapter = new NormalToAdapter(this, data); getBinding().list.setAdapter(adapter); final CommonModule module = ViewModelProviders.of(this).get(CommonModule.class); @@ -91,40 +87,15 @@ public class DownloadActivity extends BaseActivity KotlinDownloadActivity.class); break; case 4: + // 服务中 + break; + case 5: + // 组件中使用 + module.startNextActivity(DownloadActivity.this, data.get(position), + ComponentActivity.class); break; } } }); } - - private static class Adapter extends AbsRVAdapter { - - Adapter(Context context, List data) { - super(context, data); - } - - @Override protected Adapter.Holder getViewHolder(View convertView, int viewType) { - return new Adapter.Holder(convertView); - } - - @Override protected int setLayoutId(int type) { - return R.layout.item_download; - } - - @Override protected void bindData(Adapter.Holder holder, int position, NormalTo item) { - holder.text.setText(item.title); - holder.image.setImageResource(item.icon); - } - - private static class Holder extends AbsHolder { - TextView text; - AppCompatImageView image; - - Holder(View itemView) { - super(itemView); - text = findViewById(R.id.title); - image = findViewById(R.id.image); - } - } - } } \ No newline at end of file diff --git a/app/src/main/java/com/arialyy/simple/core/download/DownloadDialog.java b/app/src/main/java/com/arialyy/simple/core/download/DownloadDialog.java index 831988fb..d4075d0e 100644 --- a/app/src/main/java/com/arialyy/simple/core/download/DownloadDialog.java +++ b/app/src/main/java/com/arialyy/simple/core/download/DownloadDialog.java @@ -42,7 +42,7 @@ public class DownloadDialog extends AbsDialog implements View.OnClickListener { private long mTaskId = -1; private static final String DOWNLOAD_URL = - "http://static.gaoshouyou.com/d/4b/d7/e04b308d9cd7f0ad4cac18d1a514544c.apk"; + "http://sdkdown.muzhiwan.com/openfile/2019/07/12/com.nswj.acing.mzw_5d283abee9a3c.apk"; public DownloadDialog(Context context) { super(context); @@ -82,7 +82,7 @@ public class DownloadDialog extends AbsDialog implements View.OnClickListener { if (mTaskId == -1) { mTaskId = Aria.download(this) .load(DOWNLOAD_URL) - .setFilePath(Environment.getExternalStorageDirectory().getPath() + "/飞机大战.apk") + .setFilePath(Environment.getExternalStorageDirectory().getPath() + "/女神危机.apk") .create(); mStart.setText(getContext().getString(R.string.stop)); break; diff --git a/app/src/main/java/com/arialyy/simple/core/download/DownloadModule.java b/app/src/main/java/com/arialyy/simple/core/download/DownloadModule.java index 441b5a1a..046eb007 100644 --- a/app/src/main/java/com/arialyy/simple/core/download/DownloadModule.java +++ b/app/src/main/java/com/arialyy/simple/core/download/DownloadModule.java @@ -23,7 +23,7 @@ import com.arialyy.aria.core.download.DownloadEntity; import com.arialyy.aria.util.CommonUtil; import com.arialyy.simple.R; import com.arialyy.simple.base.BaseModule; -import com.arialyy.simple.core.download.multi_download.FileListEntity; +import com.arialyy.simple.core.download.mutil.FileListEntity; import java.util.ArrayList; import java.util.Arrays; import java.util.LinkedList; 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 35bb96db..7afe9b0b 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 @@ -94,8 +94,9 @@ public class FtpDownloadActivity extends BaseActivity() { .get(HttpDownloadModule::class.java) mModule!!.getHttpDownloadInfo(this) .observe(this, Observer { entity -> - if (entity == null) { + if (entity == null || entity.id < 0) { return@Observer } if (entity.state == IEntity.STATE_STOP) { diff --git a/app/src/main/java/com/arialyy/simple/core/download/SingleTaskActivity.java b/app/src/main/java/com/arialyy/simple/core/download/SingleTaskActivity.java index 3db16e1d..151d7fa2 100644 --- a/app/src/main/java/com/arialyy/simple/core/download/SingleTaskActivity.java +++ b/app/src/main/java/com/arialyy/simple/core/download/SingleTaskActivity.java @@ -305,7 +305,7 @@ public class SingleTaskActivity extends BaseActivity { }) .option() .addHeader("1", "@") - .controller(ControllerType.START_CONTROLLER) + .controller(ControllerType.CREATE_CONTROLLER) .create(); } diff --git a/app/src/main/java/com/arialyy/simple/core/download/fragment_download/DownloadFragment.java b/app/src/main/java/com/arialyy/simple/core/download/fragment/DownloadFragment.java similarity index 92% rename from app/src/main/java/com/arialyy/simple/core/download/fragment_download/DownloadFragment.java rename to app/src/main/java/com/arialyy/simple/core/download/fragment/DownloadFragment.java index 6192db13..994d0295 100644 --- a/app/src/main/java/com/arialyy/simple/core/download/fragment_download/DownloadFragment.java +++ b/app/src/main/java/com/arialyy/simple/core/download/fragment/DownloadFragment.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package com.arialyy.simple.core.download.fragment_download; +package com.arialyy.simple.core.download.fragment; import android.os.Bundle; import android.os.Environment; @@ -41,6 +41,7 @@ public class DownloadFragment extends AbsFragment private static final String DOWNLOAD_URL = "https://res5.d.cn/2137e42d610b3488d9420c6421529386eee5bdbfd9be1fafe0a05d6dabaec8c156ddbd00581055bbaeac03904fb63310e80010680235d16bd4c040b50096a0c20dd1c4b0854529a1.apk"; + private static final String FILE_NAME = "王者军团"; @Override protected void init(Bundle savedInstanceState) { mStart = mRootView.findViewById(R.id.start); @@ -51,7 +52,6 @@ public class DownloadFragment extends AbsFragment DownloadEntity entity = Aria.download(this).getFirstDownloadEntity(DOWNLOAD_URL); if (entity != null) { getBinding().setFileSize(CommonUtil.formatFileSize(entity.getFileSize())); - int state = entity.getState(); getBinding().setProgress(entity.getPercent()); if (entity.getState() == IEntity.STATE_RUNNING) { getBinding().setStateStr(getString(R.string.stop)); @@ -62,6 +62,8 @@ public class DownloadFragment extends AbsFragment } else { getBinding().setStateStr(getString(R.string.start)); } + getBinding().setUrl(DOWNLOAD_URL); + getBinding().setFileName(FILE_NAME); Aria.download(this).register(); } @@ -71,7 +73,9 @@ public class DownloadFragment extends AbsFragment if (mTaskId == -1) { mTaskId = Aria.download(this) .load(DOWNLOAD_URL) - .setFilePath(Environment.getExternalStorageDirectory().getPath() + "/王者军团.apk") + .setFilePath( + Environment.getExternalStorageDirectory().getPath() + String.format("%s.apk", + FILE_NAME)) .create(); getBinding().setStateStr(getString(R.string.stop)); break; diff --git a/app/src/main/java/com/arialyy/simple/core/download/fragment_download/FragmentActivity.java b/app/src/main/java/com/arialyy/simple/core/download/fragment/FragmentActivity.java similarity index 83% rename from app/src/main/java/com/arialyy/simple/core/download/fragment_download/FragmentActivity.java rename to app/src/main/java/com/arialyy/simple/core/download/fragment/FragmentActivity.java index c06453c1..483e0fb8 100644 --- a/app/src/main/java/com/arialyy/simple/core/download/fragment_download/FragmentActivity.java +++ b/app/src/main/java/com/arialyy/simple/core/download/fragment/FragmentActivity.java @@ -14,13 +14,15 @@ * limitations under the License. */ -package com.arialyy.simple.core.download.fragment_download; +package com.arialyy.simple.core.download.fragment; import android.os.Bundle; import com.arialyy.aria.core.Aria; +import com.arialyy.simple.MainActivity; import com.arialyy.simple.R; import com.arialyy.simple.base.BaseActivity; import com.arialyy.simple.databinding.FragmentDownloadBinding; +import com.arialyy.simple.to.NormalTo; /** * Created by lyy on 2017/1/4. @@ -33,7 +35,9 @@ public class FragmentActivity extends BaseActivity { @Override protected void init(Bundle savedInstanceState) { super.init(savedInstanceState); - setTile("Fragment 中使用"); + + final NormalTo to = getIntent().getParcelableExtra(MainActivity.KEY_MAIN_DATA); + setTitle(to.title); Aria.download(this).register(); } } diff --git a/app/src/main/java/com/arialyy/simple/core/download/group/FTPDirDownloadActivity.java b/app/src/main/java/com/arialyy/simple/core/download/group/FTPDirDownloadActivity.java index 47962aaa..2eac991a 100644 --- a/app/src/main/java/com/arialyy/simple/core/download/group/FTPDirDownloadActivity.java +++ b/app/src/main/java/com/arialyy/simple/core/download/group/FTPDirDownloadActivity.java @@ -79,7 +79,7 @@ public class FTPDirDownloadActivity extends BaseActivity return parentUrl + tsUrl; } }) - .controller(ControllerType.START_CONTROLLER) + .controller(ControllerType.CREATE_CONTROLLER) //.setLiveTsUrlConvert(new IVodTsUrlConverter() { // @Override public List convert(String m3u8Url, List tsUrls) { // int peerIndex = m3u8Url.lastIndexOf("/"); diff --git a/app/src/main/java/com/arialyy/simple/core/download/m3u8/M3U8VodDLoadActivity.java b/app/src/main/java/com/arialyy/simple/core/download/m3u8/M3U8VodDLoadActivity.java index 11e503c8..c2afa91d 100644 --- a/app/src/main/java/com/arialyy/simple/core/download/m3u8/M3U8VodDLoadActivity.java +++ b/app/src/main/java/com/arialyy/simple/core/download/m3u8/M3U8VodDLoadActivity.java @@ -30,7 +30,7 @@ import com.arialyy.annotations.Download; import com.arialyy.annotations.M3U8; import com.arialyy.aria.core.Aria; import com.arialyy.aria.core.common.controller.ControllerType; -import com.arialyy.aria.core.common.controller.StartController; +import com.arialyy.aria.core.common.controller.BuilderController; import com.arialyy.aria.core.download.DownloadEntity; import com.arialyy.aria.core.download.DownloadTask; import com.arialyy.aria.core.download.m3u8.IBandWidthUrlConverter; @@ -299,7 +299,7 @@ public class M3U8VodDLoadActivity extends BaseActivity { return newUrls; } }) - .controller(ControllerType.NORMAL_CONTROLLER) + .controller(ControllerType.TASK_CONTROLLER) .resume(); } break; @@ -341,11 +341,11 @@ public class M3U8VodDLoadActivity extends BaseActivity { } }) .generateIndexFile() - .controller(ControllerType.START_CONTROLLER) + .controller(ControllerType.CREATE_CONTROLLER) .create(); } - private Class c = StartController.class; + private Class c = BuilderController.class; @Override protected void dataCallback(int result, Object data) { super.dataCallback(result, data); 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 c31137ad..d5d001c3 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 @@ -59,7 +59,7 @@ public class VideoPlayerFragment extends BaseFragment { }) .option() .login("8L8e", "8guD") - .controller(ControllerType.START_CONTROLLER) + .controller(ControllerType.CREATE_CONTROLLER) .create(); getBinding().setStateStr(getString(R.string.stop)); break; @@ -144,7 +144,7 @@ public class FtpUploadActivity extends BaseActivity { .loadFtp(mTaskId) .option() .login("8L8e", "8guD") - .controller(ControllerType.NORMAL_CONTROLLER) + .controller(ControllerType.TASK_CONTROLLER) .resume(); getBinding().setStateStr(getString(R.string.stop)); } diff --git a/app/src/main/java/com/arialyy/simple/core/upload/HttpUploadActivity.java b/app/src/main/java/com/arialyy/simple/core/upload/HttpUploadActivity.java index 4d1b288c..992d83f5 100644 --- a/app/src/main/java/com/arialyy/simple/core/upload/HttpUploadActivity.java +++ b/app/src/main/java/com/arialyy/simple/core/upload/HttpUploadActivity.java @@ -20,6 +20,8 @@ import android.os.Bundle; import android.view.View; import com.arialyy.annotations.Upload; import com.arialyy.aria.core.Aria; +import com.arialyy.aria.core.common.RequestEnum; +import com.arialyy.aria.core.common.controller.ControllerType; import com.arialyy.aria.core.upload.UploadEntity; import com.arialyy.aria.core.upload.UploadTask; import com.arialyy.frame.util.FileUtil; @@ -38,7 +40,7 @@ public class HttpUploadActivity extends BaseActivity { private static final String TAG = "HttpUploadActivity"; HorizontalProgressBarWithNumber mPb; - private final String FILE_PATH = "/mnt/sdcard/test.apk"; + private final String FILE_PATH = "/mnt/sdcard/ggsg14.apk"; private UploadEntity mEntity; @Override protected int setLayoutId() { @@ -72,10 +74,14 @@ public class HttpUploadActivity extends BaseActivity { void upload() { Aria.upload(HttpUploadActivity.this).load(FILE_PATH) - .setUploadUrl( - "http://lib-test.xzxyun.com:8042/Api/upload?data={\"type\":\"1\",\"fileType\":\".apk\"}") + //.setUploadUrl("http://lib-test.xzxyun.com:8042/Api/upload?data={\"type\":\"1\",\"fileType\":\".apk\"}") + .setUploadUrl("http://9.9.9.205:5000/upload/") //.setTempUrl("http://192.168.1.6:8080/upload/sign_file/").setAttachment("file") //.addHeader("iplanetdirectorypro", "11a09102fb934ad0bc206f9c611d7933") + .option() + .setRequestType(RequestEnum.POST) + .setParam("params", "bbbbbbbb") + .controller(ControllerType.CREATE_CONTROLLER) .create(); } 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 4e1d7406..6b87c9b6 100644 --- a/app/src/main/java/com/arialyy/simple/modlue/AnyRunnModule.java +++ b/app/src/main/java/com/arialyy/simple/modlue/AnyRunnModule.java @@ -113,7 +113,7 @@ public class AnyRunnModule { .setStorePath("/mnt/sdcard/Download/server.crt") .setAlias("www.laoyuyu.me") .setStorePass("123456") - .controller(ControllerType.START_CONTROLLER) + .controller(ControllerType.CREATE_CONTROLLER) .create(); } else { Aria.download(this).load(mEntity.getId()).resume(); diff --git a/app/src/main/java/com/arialyy/simple/modlue/CommonModule.java b/app/src/main/java/com/arialyy/simple/modlue/CommonModule.java index 16861662..467af13f 100644 --- a/app/src/main/java/com/arialyy/simple/modlue/CommonModule.java +++ b/app/src/main/java/com/arialyy/simple/modlue/CommonModule.java @@ -43,6 +43,28 @@ public class CommonModule extends BaseViewModule { context.startActivity(intent); } + public LiveData> getComponentData(Context context) { + List list = new ArrayList<>(); + String[] titles = context.getResources().getStringArray(R.array.component_items); + int[] icons = new int[] { + R.drawable.ic_fragment, + R.drawable.ic_dialog + }; + int i = 0; + for (String title : titles) { + NormalTo to = new NormalTo(); + to.icon = icons[i]; + to.title = title; + i++; + list.add(to); + } + mLiveData.postValue(list); + return mLiveData; + } + + /** + * http下载功能页面数据 + */ public LiveData> getDownloadData(Context context) { List list = new ArrayList<>(); String[] titles = context.getResources().getStringArray(R.array.download_items); @@ -66,6 +88,9 @@ public class CommonModule extends BaseViewModule { return mLiveData; } + /** + * 首页数据 + */ public LiveData> getMainData(Context context) { List list = new ArrayList<>(); String[] titles = context.getResources().getStringArray(R.array.main_items); diff --git a/app/src/main/res/drawable/ic_dialog.xml b/app/src/main/res/drawable/ic_dialog.xml new file mode 100644 index 00000000..033b2268 --- /dev/null +++ b/app/src/main/res/drawable/ic_dialog.xml @@ -0,0 +1,9 @@ + + + diff --git a/app/src/main/res/layout/activity_component.xml b/app/src/main/res/layout/activity_component.xml new file mode 100644 index 00000000..28de55c9 --- /dev/null +++ b/app/src/main/res/layout/activity_component.xml @@ -0,0 +1,8 @@ + + + + diff --git a/app/src/main/res/layout/activity_fragment.xml b/app/src/main/res/layout/activity_fragment.xml index 7f768a6e..43efee06 100644 --- a/app/src/main/res/layout/activity_fragment.xml +++ b/app/src/main/res/layout/activity_fragment.xml @@ -9,7 +9,7 @@ diff --git a/app/src/main/res/layout/fragment_download.xml b/app/src/main/res/layout/fragment_download.xml index 97ace27a..9c6a47fb 100644 --- a/app/src/main/res/layout/fragment_download.xml +++ b/app/src/main/res/layout/fragment_download.xml @@ -20,6 +20,14 @@ name="stateStr" type="String" /> + + + + 组件中使用 + + fragment中使用 + dialog中使用 + + http://static.gaoshouyou.com/d/d4/4f/d6d48db3794fb9ecf47e83c346570881.apk diff --git a/app/src/main/res/xml/network_security_config.xml b/app/src/main/res/xml/network_security_config.xml index 4ab6a7a7..418ff58e 100644 --- a/app/src/main/res/xml/network_security_config.xml +++ b/app/src/main/res/xml/network_security_config.xml @@ -1,7 +1,7 @@ - + - + \ No newline at end of file diff --git a/build.gradle b/build.gradle index b0fe1385..4bf2e476 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.6_beta_3' + publishVersion = '3.6.6' // publishVersion = '1.0.4' //FTP插件 repoName='maven' desc = 'android 下载框架' diff --git a/py/upload.py b/py/upload.py index 7dd9ad97..884b337e 100644 --- a/py/upload.py +++ b/py/upload.py @@ -43,13 +43,14 @@ def test(): @app.route('/upload/', methods=['GET', 'POST']) def upload_file(): - print 'upload' if request.method == 'POST': - print 'post' + print(request.values) + print('params = ' + request.values.get('params')) + file = request.files['file'] - print file + print(file) if file and allowed_file(file.filename): - print 'start save' + print('start save') filename = secure_filename(file.filename) file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename)) # file_url = url_for('uploaded_file', filename=filename)