diff --git a/AppFrame/src/main/java/com/arialyy/frame/cache/DiskLruCache.java b/AppFrame/src/main/java/com/arialyy/frame/cache/DiskLruCache.java index 48c60fb0..1b1f25a5 100644 --- a/AppFrame/src/main/java/com/arialyy/frame/cache/DiskLruCache.java +++ b/AppFrame/src/main/java/com/arialyy/frame/cache/DiskLruCache.java @@ -245,7 +245,6 @@ public final class DiskLruCache implements Closeable { /** * Recursively delete everything in {@code dir}. */ - // TODO: this should specify paths as Strings rather than as Files public static void deleteContents(File dir) throws IOException { File[] files = dir.listFiles(); if (files == null) { diff --git a/Aria/build.gradle b/Aria/build.gradle index 39b76600..08cb9b35 100644 --- a/Aria/build.gradle +++ b/Aria/build.gradle @@ -7,8 +7,8 @@ android { defaultConfig { minSdkVersion rootProject.ext.minSdkVersion targetSdkVersion rootProject.ext.targetSdkVersion - versionCode 329 - versionName "3.2.9" + versionCode rootProject.ext.versionCode + versionName rootProject.ext.versionName } buildTypes { release { @@ -26,9 +26,13 @@ dependencies { testImplementation 'junit:junit:4.12' implementation "androidx.appcompat:appcompat:${rootProject.ext.XAppcompatVersion}" api project(':AriaAnnotations') - api 'com.arialyy.aria:aria-ftp-plug:1.0.4' // 打包时用这个 + api project(path: ':PublicComponent') + + api 'com.arialyy.aria:aria-ftp-plug:1.0.4' + implementation project(path: ':HttpComponent') // 打包时用这个 // compile project(':AriaFtpPlug') +// implementation project(path: ':AriaFtpComponent') } apply from: 'bintray-release.gradle' 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 6ace735b..3461574a 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/AriaManager.java +++ b/Aria/src/main/java/com/arialyy/aria/core/AriaManager.java @@ -34,18 +34,16 @@ import androidx.fragment.app.DialogFragment; import androidx.fragment.app.Fragment; import com.arialyy.aria.core.command.CommandManager; import com.arialyy.aria.core.common.QueueMod; -import com.arialyy.aria.core.common.RecordHandler; import com.arialyy.aria.core.config.AppConfig; -import com.arialyy.aria.core.config.Configuration; import com.arialyy.aria.core.config.DGroupConfig; import com.arialyy.aria.core.config.DownloadConfig; import com.arialyy.aria.core.config.UploadConfig; -import com.arialyy.aria.core.config.XMLReader; import com.arialyy.aria.core.download.DownloadEntity; import com.arialyy.aria.core.download.DownloadGroupEntity; import com.arialyy.aria.core.download.DownloadReceiver; import com.arialyy.aria.core.inf.AbsReceiver; import com.arialyy.aria.core.inf.IReceiver; +import com.arialyy.aria.core.inf.IRecordHandler; import com.arialyy.aria.core.inf.ReceiverType; import com.arialyy.aria.core.upload.UploadEntity; import com.arialyy.aria.core.upload.UploadReceiver; @@ -53,19 +51,13 @@ import com.arialyy.aria.orm.DbEntity; import com.arialyy.aria.orm.DelegateWrapper; import com.arialyy.aria.util.ALog; import com.arialyy.aria.util.AriaCrashHandler; -import com.arialyy.aria.util.CommonUtil; import com.arialyy.aria.util.RecordUtil; import java.io.File; -import java.io.IOException; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; -import javax.xml.parsers.ParserConfigurationException; -import javax.xml.parsers.SAXParser; -import javax.xml.parsers.SAXParserFactory; -import org.xml.sax.SAXException; /** * Created by lyy on 2016/12/1. https://github.com/AriaLyy/Aria Aria管理器,任务操作在这里执行 @@ -73,12 +65,8 @@ import org.xml.sax.SAXException; @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH) public class AriaManager { private static final String TAG = "AriaManager"; private static final Object LOCK = new Object(); - public static final String DOWNLOAD_TEMP_DIR = "/Aria/temp/download/"; - public static final String UPLOAD_TEMP_DIR = "/Aria/temp/upload/"; - /** - * 是否已经联网,true 已经联网 - */ - private static boolean isConnectedNet = true; + + @SuppressLint("StaticFieldLeak") private static volatile AriaManager INSTANCE = null; private Map mReceivers = new ConcurrentHashMap<>(); @@ -87,12 +75,9 @@ import org.xml.sax.SAXException; */ private Map> mSubClass = new ConcurrentHashMap<>(); private static Context APP; - private DownloadConfig mDConfig; - private UploadConfig mUConfig; - private AppConfig mAConfig; - private DGroupConfig mDGConfig; private Handler mAriaHandler; private DelegateWrapper mDbWrapper; + private AriaConfig mConfig; private AriaManager(Context context) { APP = context.getApplicationContext(); @@ -118,55 +103,18 @@ import org.xml.sax.SAXException; } private void initData() { + mConfig = AriaConfig.init(APP); initDb(APP); regAppLifeCallback(APP); - initConfig(); initAria(); amendTaskState(); - regNetCallBack(APP); } public Context getAPP() { return APP; } - /** - * 注册网络监听,只有配置了检查网络{@link AppConfig#isNetCheck()}才会注册事件 - */ - private void regNetCallBack(Context context) { - if (!getAppConfig().isNetCheck()) { - return; - } - if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { - return; - } - ConnectivityManager cm = - (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); - if (cm == null) { - return; - } - - NetworkRequest.Builder builder = new NetworkRequest.Builder(); - NetworkRequest request = builder.addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET) - .addTransportType(NetworkCapabilities.TRANSPORT_WIFI) - .addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR) - .build(); - cm.registerNetworkCallback(request, new ConnectivityManager.NetworkCallback() { - - @Override public void onLost(Network network) { - super.onLost(network); - isConnectedNet = false; - ALog.d(TAG, "onLost"); - } - - @Override public void onAvailable(Network network) { - super.onAvailable(network); - ALog.d(TAG, "onAvailable"); - isConnectedNet = true; - } - }); - } /** * 初始化数据库 @@ -186,10 +134,11 @@ import org.xml.sax.SAXException; } private void initAria() { - if (mAConfig.getUseAriaCrashHandler()) { + AppConfig appConfig = mConfig.getAConfig(); + if (appConfig.getUseAriaCrashHandler()) { Thread.setDefaultUncaughtExceptionHandler(new AriaCrashHandler()); } - mAConfig.setLogLevel(mAConfig.getLogLevel()); + appConfig.setLogLevel(appConfig.getLogLevel()); CommandManager.init(); } @@ -217,9 +166,7 @@ import org.xml.sax.SAXException; return mAriaHandler; } - public boolean isConnectedNet() { - return isConnectedNet; - } + public Map getReceiver() { return mReceivers; @@ -237,7 +184,7 @@ import org.xml.sax.SAXException; * @deprecated 后续版本会删除该api */ @Deprecated public AriaManager setUploadQueueMod(QueueMod mod) { - mUConfig.setQueueMod(mod.tag); + mConfig.getUConfig().setQueueMod(mod.tag); return this; } @@ -253,7 +200,7 @@ import org.xml.sax.SAXException; * @deprecated 后续版本会删除该api */ @Deprecated public AriaManager setDownloadQueueMod(QueueMod mod) { - mDConfig.setQueueMod(mod.tag); + mConfig.getDConfig().setQueueMod(mod.tag); return this; } @@ -267,7 +214,7 @@ import org.xml.sax.SAXException; * */ public DownloadConfig getDownloadConfig() { - return mDConfig; + return mConfig.getDConfig(); } /** @@ -280,14 +227,14 @@ import org.xml.sax.SAXException; * */ public UploadConfig getUploadConfig() { - return mUConfig; + return mConfig.getUConfig(); } /** * 获取APP配置 */ public AppConfig getAppConfig() { - return mAConfig; + return mConfig.getAConfig(); } /** @@ -300,7 +247,7 @@ import org.xml.sax.SAXException; * */ public DGroupConfig getDGroupConfig() { - return mDGConfig; + return mConfig.getDGConfig(); } /** @@ -335,13 +282,13 @@ import org.xml.sax.SAXException; public void delRecord(int type, String key, boolean removeFile) { switch (type) { case 1: // 删除普通任务记录 - RecordUtil.delTaskRecord(key, RecordHandler.TYPE_DOWNLOAD, removeFile, true); + RecordUtil.delTaskRecord(key, IRecordHandler.TYPE_DOWNLOAD, removeFile, true); break; case 2: RecordUtil.delGroupTaskRecord(key, removeFile); break; case 3: - RecordUtil.delTaskRecord(key, RecordHandler.TYPE_UPLOAD); + RecordUtil.delTaskRecord(key, IRecordHandler.TYPE_UPLOAD); break; } } @@ -442,58 +389,6 @@ import org.xml.sax.SAXException; return String.format("%s_%s_%s", obj.getClass().getName(), type, obj.hashCode()); } - /** - * 初始化配置文件 - */ - private void initConfig() { - mDConfig = Configuration.getInstance().downloadCfg; - mUConfig = Configuration.getInstance().uploadCfg; - mAConfig = Configuration.getInstance().appCfg; - mDGConfig = Configuration.getInstance().dGroupCfg; - - File xmlFile = new File(APP.getFilesDir().getPath() + Configuration.XML_FILE); - File tempDir = new File(APP.getFilesDir().getPath() + "/temp"); - if (!xmlFile.exists()) { - loadConfig(); - } else { - try { - String md5Code = CommonUtil.getFileMD5(xmlFile); - File file = new File(APP.getFilesDir().getPath() + "/temp.xml"); - if (file.exists()) { - file.delete(); - } - CommonUtil.createFileFormInputStream(APP.getAssets().open("aria_config.xml"), - file.getPath()); - if (!CommonUtil.checkMD5(md5Code, file) || !Configuration.getInstance().configExists()) { - loadConfig(); - } - } catch (IOException e) { - e.printStackTrace(); - } - } - if (tempDir.exists()) { - File newDir = new File(APP.getFilesDir().getPath() + DOWNLOAD_TEMP_DIR); - newDir.mkdirs(); - tempDir.renameTo(newDir); - } - } - - /** - * 加载配置文件 - */ - private void loadConfig() { - try { - XMLReader helper = new XMLReader(); - SAXParserFactory factory = SAXParserFactory.newInstance(); - SAXParser parser = factory.newSAXParser(); - parser.parse(APP.getAssets().open("aria_config.xml"), helper); - CommonUtil.createFileFormInputStream(APP.getAssets().open("aria_config.xml"), - APP.getFilesDir().getPath() + Configuration.XML_FILE); - } catch (ParserConfigurationException | IOException | SAXException e) { - ALog.e(TAG, e.toString()); - } - } - /** * 注册APP生命周期回调 */ diff --git a/Aria/src/main/java/com/arialyy/aria/core/command/AbsCmd.java b/Aria/src/main/java/com/arialyy/aria/core/command/AbsCmd.java index 4867c87b..85ee7650 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/command/AbsCmd.java +++ b/Aria/src/main/java/com/arialyy/aria/core/command/AbsCmd.java @@ -16,7 +16,7 @@ package com.arialyy.aria.core.command; -import com.arialyy.aria.core.inf.AbsTaskWrapper; +import com.arialyy.aria.core.wrapper.AbsTaskWrapper; import com.arialyy.aria.core.queue.AbsTaskQueue; /** diff --git a/Aria/src/main/java/com/arialyy/aria/core/command/AbsCmdFactory.java b/Aria/src/main/java/com/arialyy/aria/core/command/AbsCmdFactory.java index 14caef9a..2af0e2ff 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/command/AbsCmdFactory.java +++ b/Aria/src/main/java/com/arialyy/aria/core/command/AbsCmdFactory.java @@ -15,7 +15,7 @@ */ package com.arialyy.aria.core.command; -import com.arialyy.aria.core.inf.AbsTaskWrapper; +import com.arialyy.aria.core.wrapper.AbsTaskWrapper; /** * Created by AriaL on 2017/6/29. diff --git a/Aria/src/main/java/com/arialyy/aria/core/command/AbsGroupCmd.java b/Aria/src/main/java/com/arialyy/aria/core/command/AbsGroupCmd.java index be976d12..dc97c141 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/command/AbsGroupCmd.java +++ b/Aria/src/main/java/com/arialyy/aria/core/command/AbsGroupCmd.java @@ -15,11 +15,11 @@ */ package com.arialyy.aria.core.command; +import com.arialyy.aria.core.download.AbsGroupTaskWrapper; import com.arialyy.aria.core.download.DGTaskWrapper; -import com.arialyy.aria.core.inf.AbsGroupTask; -import com.arialyy.aria.core.inf.AbsGroupTaskWrapper; -import com.arialyy.aria.core.inf.AbsTask; -import com.arialyy.aria.core.queue.DownloadGroupTaskQueue; +import com.arialyy.aria.core.queue.DGroupTaskQueue; +import com.arialyy.aria.core.task.AbsGroupTask; +import com.arialyy.aria.core.task.AbsTask; import com.arialyy.aria.util.ALog; import com.arialyy.aria.util.CommonUtil; @@ -39,7 +39,7 @@ public abstract class AbsGroupCmd extends AbsCmd< mTaskWrapper = entity; TAG = CommonUtil.getClassName(this); if (entity instanceof DGTaskWrapper) { - mQueue = DownloadGroupTaskQueue.getInstance(); + mQueue = DGroupTaskQueue.getInstance(); isDownloadCmd = true; } } diff --git a/Aria/src/main/java/com/arialyy/aria/core/command/AbsNormalCmd.java b/Aria/src/main/java/com/arialyy/aria/core/command/AbsNormalCmd.java index 10494a6f..10a11a96 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/command/AbsNormalCmd.java +++ b/Aria/src/main/java/com/arialyy/aria/core/command/AbsNormalCmd.java @@ -18,14 +18,14 @@ package com.arialyy.aria.core.command; import com.arialyy.aria.core.download.DGTaskWrapper; import com.arialyy.aria.core.download.DTaskWrapper; -import com.arialyy.aria.core.inf.AbsTask; -import com.arialyy.aria.core.inf.AbsTaskWrapper; +import com.arialyy.aria.core.task.AbsTask; +import com.arialyy.aria.core.wrapper.AbsTaskWrapper; import com.arialyy.aria.core.inf.IEntity; -import com.arialyy.aria.core.inf.ITask; -import com.arialyy.aria.core.queue.DownloadGroupTaskQueue; -import com.arialyy.aria.core.queue.DownloadTaskQueue; -import com.arialyy.aria.core.queue.UploadTaskQueue; -import com.arialyy.aria.core.scheduler.ISchedulers; +import com.arialyy.aria.core.task.ITask; +import com.arialyy.aria.core.queue.DGroupTaskQueue; +import com.arialyy.aria.core.queue.DTaskQueue; +import com.arialyy.aria.core.queue.UTaskQueue; +import com.arialyy.aria.core.listener.ISchedulers; import com.arialyy.aria.core.upload.UTaskWrapper; import com.arialyy.aria.util.ALog; import com.arialyy.aria.util.CommonUtil; @@ -54,19 +54,19 @@ public abstract class AbsNormalCmd extends AbsCmd { ALog.e(TAG, "任务类型错误,任务类型应该为ICM.TASK_TYPE_DOWNLOAD"); return; } - mQueue = DownloadTaskQueue.getInstance(); + mQueue = DTaskQueue.getInstance(); } else if (taskType == ITask.DOWNLOAD_GROUP) { if (!(entity instanceof DGTaskWrapper)) { ALog.e(TAG, "任务类型错误,任务类型应该为ICM.TASK_TYPE_DOWNLOAD_GROUP"); return; } - mQueue = DownloadGroupTaskQueue.getInstance(); + mQueue = DGroupTaskQueue.getInstance(); } else if (taskType == ITask.UPLOAD) { if (!(entity instanceof UTaskWrapper)) { ALog.e(TAG, "任务类型错误,任务类型应该为ICM.TASK_TYPE_UPLOAD"); return; } - mQueue = UploadTaskQueue.getInstance(); + mQueue = UTaskQueue.getInstance(); } else { ALog.e(TAG, "任务类型错误,任务类型应该为ICM.TASK_TYPE_DOWNLOAD、TASK_TYPE_DOWNLOAD_GROUP、TASK_TYPE_UPLOAD"); return; diff --git a/Aria/src/main/java/com/arialyy/aria/core/command/AddCmd.java b/Aria/src/main/java/com/arialyy/aria/core/command/AddCmd.java index 299ba3f8..03046ff8 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/command/AddCmd.java +++ b/Aria/src/main/java/com/arialyy/aria/core/command/AddCmd.java @@ -16,8 +16,8 @@ package com.arialyy.aria.core.command; -import com.arialyy.aria.core.inf.AbsTask; -import com.arialyy.aria.core.inf.AbsTaskWrapper; +import com.arialyy.aria.core.task.AbsTask; +import com.arialyy.aria.core.wrapper.AbsTaskWrapper; import com.arialyy.aria.core.inf.IEntity; import com.arialyy.aria.util.ALog; diff --git a/Aria/src/main/java/com/arialyy/aria/core/command/CancelAllCmd.java b/Aria/src/main/java/com/arialyy/aria/core/command/CancelAllCmd.java index 52cb7de5..f589d984 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/command/CancelAllCmd.java +++ b/Aria/src/main/java/com/arialyy/aria/core/command/CancelAllCmd.java @@ -20,11 +20,11 @@ import com.arialyy.aria.core.download.DGTaskWrapper; import com.arialyy.aria.core.download.DTaskWrapper; import com.arialyy.aria.core.download.DownloadEntity; import com.arialyy.aria.core.download.DownloadGroupEntity; -import com.arialyy.aria.core.inf.AbsTaskWrapper; +import com.arialyy.aria.core.wrapper.AbsTaskWrapper; 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.UploadTaskQueue; +import com.arialyy.aria.core.queue.DGroupTaskQueue; +import com.arialyy.aria.core.queue.DTaskQueue; +import com.arialyy.aria.core.queue.UTaskQueue; import com.arialyy.aria.core.upload.UTaskWrapper; import com.arialyy.aria.core.upload.UploadEntity; import com.arialyy.aria.orm.DbEntity; @@ -104,11 +104,11 @@ final public class CancelAllCmd extends AbsNormalCmd AbsNormalCmd createNormalCmd(T entity, int cmd, + int taskType) { + return NormalCmdFactory.getInstance().createCmd(entity, cmd, taskType); + } + + /** + * 创建任务组命令 + * + * @param childUrl 子任务url + */ + public static AbsGroupCmd createGroupCmd(T entity, int cmd, + String childUrl) { + return GroupCmdFactory.getInstance().createCmd(entity, cmd, childUrl); + } +} diff --git a/Aria/src/main/java/com/arialyy/aria/core/command/DGSubStartCmd.java b/Aria/src/main/java/com/arialyy/aria/core/command/DGSubStartCmd.java index 4cd0e163..bb16de55 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/command/DGSubStartCmd.java +++ b/Aria/src/main/java/com/arialyy/aria/core/command/DGSubStartCmd.java @@ -15,7 +15,7 @@ */ package com.arialyy.aria.core.command; -import com.arialyy.aria.core.inf.AbsGroupTaskWrapper; +import com.arialyy.aria.core.download.AbsGroupTaskWrapper; /** * Created by AriaL on 2017/6/29. diff --git a/Aria/src/main/java/com/arialyy/aria/core/command/DGSubStopCmd.java b/Aria/src/main/java/com/arialyy/aria/core/command/DGSubStopCmd.java index 78b8b63f..7cdcd226 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/command/DGSubStopCmd.java +++ b/Aria/src/main/java/com/arialyy/aria/core/command/DGSubStopCmd.java @@ -15,7 +15,7 @@ */ package com.arialyy.aria.core.command; -import com.arialyy.aria.core.inf.AbsGroupTaskWrapper; +import com.arialyy.aria.core.download.AbsGroupTaskWrapper; /** * Created by AriaL on 2017/6/29. diff --git a/Aria/src/main/java/com/arialyy/aria/core/command/GroupCmdFactory.java b/Aria/src/main/java/com/arialyy/aria/core/command/GroupCmdFactory.java index 0edbfec9..e1241d65 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/command/GroupCmdFactory.java +++ b/Aria/src/main/java/com/arialyy/aria/core/command/GroupCmdFactory.java @@ -15,7 +15,7 @@ */ package com.arialyy.aria.core.command; -import com.arialyy.aria.core.inf.AbsGroupTaskWrapper; +import com.arialyy.aria.core.download.AbsGroupTaskWrapper; /** * Created by AriaL on 2017/6/29. 任务组子任务控制命令 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 42fe3c20..692d083c 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 @@ -16,9 +16,9 @@ package com.arialyy.aria.core.command; import com.arialyy.aria.core.AriaManager; -import com.arialyy.aria.core.download.DownloadTask; -import com.arialyy.aria.core.inf.AbsTaskWrapper; -import com.arialyy.aria.core.queue.DownloadTaskQueue; +import com.arialyy.aria.core.task.DownloadTask; +import com.arialyy.aria.core.wrapper.AbsTaskWrapper; +import com.arialyy.aria.core.queue.DTaskQueue; import com.arialyy.aria.util.ALog; import com.arialyy.aria.util.NetUtils; @@ -50,7 +50,7 @@ final class HighestPriorityCmd extends AbsNormalCmd task = (DownloadTask) createTask(); } if (task != null) { - ((DownloadTaskQueue) mQueue).setTaskHighestPriority(task); + ((DTaskQueue) mQueue).setTaskHighestPriority(task); } } } diff --git a/Aria/src/main/java/com/arialyy/aria/core/command/NormalCmdFactory.java b/Aria/src/main/java/com/arialyy/aria/core/command/NormalCmdFactory.java index 767a7fdd..8ba64474 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/command/NormalCmdFactory.java +++ b/Aria/src/main/java/com/arialyy/aria/core/command/NormalCmdFactory.java @@ -16,7 +16,7 @@ package com.arialyy.aria.core.command; -import com.arialyy.aria.core.inf.AbsTaskWrapper; +import com.arialyy.aria.core.wrapper.AbsTaskWrapper; /** * Created by Lyy on 2016/9/23. diff --git a/Aria/src/main/java/com/arialyy/aria/core/command/ReStartCmd.java b/Aria/src/main/java/com/arialyy/aria/core/command/ReStartCmd.java index bc3d8629..10598993 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/command/ReStartCmd.java +++ b/Aria/src/main/java/com/arialyy/aria/core/command/ReStartCmd.java @@ -15,8 +15,8 @@ */ package com.arialyy.aria.core.command; -import com.arialyy.aria.core.inf.AbsTask; -import com.arialyy.aria.core.inf.AbsTaskWrapper; +import com.arialyy.aria.core.task.AbsTask; +import com.arialyy.aria.core.wrapper.AbsTaskWrapper; import com.arialyy.aria.core.inf.TaskSchedulerType; /** 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 f29e79f6..5a486e5f 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 @@ -5,14 +5,14 @@ import com.arialyy.aria.core.download.DGTaskWrapper; import com.arialyy.aria.core.download.DTaskWrapper; import com.arialyy.aria.core.download.DownloadEntity; import com.arialyy.aria.core.download.DownloadGroupEntity; -import com.arialyy.aria.core.inf.AbsEntity; -import com.arialyy.aria.core.inf.AbsTask; -import com.arialyy.aria.core.inf.AbsTaskWrapper; +import com.arialyy.aria.core.common.AbsEntity; +import com.arialyy.aria.core.task.AbsTask; +import com.arialyy.aria.core.wrapper.AbsTaskWrapper; import com.arialyy.aria.core.inf.IEntity; 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.UploadTaskQueue; +import com.arialyy.aria.core.queue.DGroupTaskQueue; +import com.arialyy.aria.core.queue.DTaskQueue; +import com.arialyy.aria.core.queue.UTaskQueue; import com.arialyy.aria.core.upload.UTaskWrapper; import com.arialyy.aria.core.upload.UploadEntity; import com.arialyy.aria.orm.DbEntity; @@ -112,11 +112,11 @@ final class ResumeAllCmd extends AbsNormalCmd { for (AbsTaskWrapper te : mWaitList) { if (te instanceof DTaskWrapper) { - mQueue = DownloadTaskQueue.getInstance(); + mQueue = DTaskQueue.getInstance(); } else if (te instanceof UTaskWrapper) { - mQueue = UploadTaskQueue.getInstance(); + mQueue = UTaskQueue.getInstance(); } else if (te instanceof DGTaskWrapper) { - mQueue = DownloadGroupTaskQueue.getInstance(); + mQueue = DGroupTaskQueue.getInstance(); } if (mQueue.getCurrentExePoolNum() < maxTaskNum) { startTask(createTask(te)); 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 5aec44d8..8c520bf9 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 @@ -22,16 +22,17 @@ import com.arialyy.aria.core.download.DGTaskWrapper; import com.arialyy.aria.core.download.DTaskWrapper; import com.arialyy.aria.core.download.DownloadEntity; import com.arialyy.aria.core.download.DownloadGroupEntity; -import com.arialyy.aria.core.inf.AbsTask; -import com.arialyy.aria.core.inf.AbsTaskWrapper; +import com.arialyy.aria.core.task.AbsTask; +import com.arialyy.aria.core.wrapper.AbsTaskWrapper; import com.arialyy.aria.core.inf.IEntity; -import com.arialyy.aria.core.inf.ITaskWrapper; +import com.arialyy.aria.core.inf.IOptionConstant; 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.UploadTaskQueue; +import com.arialyy.aria.core.queue.DGroupTaskQueue; +import com.arialyy.aria.core.queue.DTaskQueue; +import com.arialyy.aria.core.queue.UTaskQueue; import com.arialyy.aria.core.upload.UTaskWrapper; import com.arialyy.aria.core.upload.UploadEntity; +import com.arialyy.aria.core.wrapper.ITaskWrapper; import com.arialyy.aria.orm.DbEntity; import com.arialyy.aria.util.ALog; import com.arialyy.aria.util.CommonUtil; @@ -156,22 +157,24 @@ final class StartCmd extends AbsNormalCmd { } private void handleTask(List waitList) { - for (AbsTaskWrapper te : waitList) { - if (te.getEntity() == null) continue; - AbsTask task = getTask(te.getKey()); + for (AbsTaskWrapper wrapper : waitList) { + if (wrapper.getEntity() == null) continue; + AbsTask task = getTask(wrapper.getKey()); if (task != null) continue; - if (te instanceof DTaskWrapper) { - if (te.getRequestType() == ITaskWrapper.D_FTP - || te.getRequestType() == ITaskWrapper.U_FTP) { - te.asFtp().setUrlEntity(CommonUtil.getFtpUrlInfo(te.getEntity().getKey())); + if (wrapper instanceof DTaskWrapper) { + if (wrapper.getRequestType() == ITaskWrapper.D_FTP + || wrapper.getRequestType() == ITaskWrapper.U_FTP) { + wrapper.getOptionParams() + .setParams(IOptionConstant.ftpUrlEntity, + CommonUtil.getFtpUrlInfo(wrapper.getEntity().getKey())); } - mQueue = DownloadTaskQueue.getInstance(); - } else if (te instanceof UTaskWrapper) { - mQueue = UploadTaskQueue.getInstance(); - } else if (te instanceof DGTaskWrapper) { - mQueue = DownloadGroupTaskQueue.getInstance(); + mQueue = DTaskQueue.getInstance(); + } else if (wrapper instanceof UTaskWrapper) { + mQueue = UTaskQueue.getInstance(); + } else if (wrapper instanceof DGTaskWrapper) { + mQueue = DGroupTaskQueue.getInstance(); } - createTask(te); + createTask(wrapper); } } } diff --git a/Aria/src/main/java/com/arialyy/aria/core/command/StopAllCmd.java b/Aria/src/main/java/com/arialyy/aria/core/command/StopAllCmd.java index 81966da4..bd95cbc8 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/command/StopAllCmd.java +++ b/Aria/src/main/java/com/arialyy/aria/core/command/StopAllCmd.java @@ -1,6 +1,6 @@ package com.arialyy.aria.core.command; -import com.arialyy.aria.core.inf.AbsTaskWrapper; +import com.arialyy.aria.core.wrapper.AbsTaskWrapper; /** * Created by AriaL on 2017/6/13. diff --git a/Aria/src/main/java/com/arialyy/aria/core/command/StopCmd.java b/Aria/src/main/java/com/arialyy/aria/core/command/StopCmd.java index 57c04eb4..5032fc83 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/command/StopCmd.java +++ b/Aria/src/main/java/com/arialyy/aria/core/command/StopCmd.java @@ -16,8 +16,8 @@ package com.arialyy.aria.core.command; -import com.arialyy.aria.core.inf.AbsTask; -import com.arialyy.aria.core.inf.AbsTaskWrapper; +import com.arialyy.aria.core.task.AbsTask; +import com.arialyy.aria.core.wrapper.AbsTaskWrapper; import com.arialyy.aria.core.inf.IEntity; import com.arialyy.aria.util.ALog; diff --git a/Aria/src/main/java/com/arialyy/aria/core/common/AbsNormalTarget.java b/Aria/src/main/java/com/arialyy/aria/core/common/AbsNormalTarget.java index bcaf9b10..0a4e505b 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/common/AbsNormalTarget.java +++ b/Aria/src/main/java/com/arialyy/aria/core/common/AbsNormalTarget.java @@ -18,7 +18,6 @@ package com.arialyy.aria.core.common; import com.arialyy.aria.core.common.controller.INormalFeature; import com.arialyy.aria.core.common.controller.NormalController; import com.arialyy.aria.core.download.DownloadGroupEntity; -import com.arialyy.aria.core.inf.AbsNormalEntity; import com.arialyy.aria.core.inf.AbsTarget; import com.arialyy.aria.core.inf.IEntity; import com.arialyy.aria.core.manager.TaskWrapperManager; diff --git a/Aria/src/main/java/com/arialyy/aria/core/common/BaseDelegate.java b/Aria/src/main/java/com/arialyy/aria/core/common/BaseDelegate.java index f1496339..439f0a40 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/common/BaseDelegate.java +++ b/Aria/src/main/java/com/arialyy/aria/core/common/BaseDelegate.java @@ -19,7 +19,8 @@ import androidx.annotation.CheckResult; import com.arialyy.aria.core.common.controller.ControllerType; import com.arialyy.aria.core.common.controller.FeatureController; import com.arialyy.aria.core.inf.AbsTarget; -import com.arialyy.aria.core.inf.AbsTaskWrapper; +import com.arialyy.aria.core.inf.Suggest; +import com.arialyy.aria.core.wrapper.AbsTaskWrapper; import com.arialyy.aria.util.CommonUtil; public abstract class BaseDelegate { diff --git a/Aria/src/main/java/com/arialyy/aria/core/common/ftp/FTPSDelegate.java b/Aria/src/main/java/com/arialyy/aria/core/common/FTPSDelegate.java similarity index 74% rename from Aria/src/main/java/com/arialyy/aria/core/common/ftp/FTPSDelegate.java rename to Aria/src/main/java/com/arialyy/aria/core/common/FTPSDelegate.java index 3a610e18..1197c370 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/common/ftp/FTPSDelegate.java +++ b/Aria/src/main/java/com/arialyy/aria/core/common/FTPSDelegate.java @@ -13,15 +13,17 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.arialyy.aria.core.common.ftp; +package com.arialyy.aria.core.common; import android.text.TextUtils; import androidx.annotation.CheckResult; -import com.arialyy.aria.core.common.BaseDelegate; -import com.arialyy.aria.core.common.ProtocolType; -import com.arialyy.aria.core.common.Suggest; +import com.arialyy.aria.core.FtpUrlEntity; +import com.arialyy.aria.core.ProtocolType; +import com.arialyy.aria.core.inf.Suggest; import com.arialyy.aria.core.inf.AbsTarget; -import com.arialyy.aria.core.inf.AbsTaskWrapper; +import com.arialyy.aria.core.wrapper.AbsTaskWrapper; +import com.arialyy.aria.core.inf.IOptionConstant; +import com.arialyy.aria.util.ALog; /** * D_FTP SSL/TSL 参数委托 @@ -32,8 +34,11 @@ public class FTPSDelegate extends BaseDelegate public FTPSDelegate(TARGET target, AbsTaskWrapper wrapper) { super(target, wrapper); - mUrlEntity = getTaskWrapper().asFtp().getUrlEntity(); - mUrlEntity.isFtps = true; + mUrlEntity = (FtpUrlEntity) getTaskWrapper().getOptionParams() + .getParam(IOptionConstant.ftpUrlEntity); + if (mUrlEntity != null) { + mUrlEntity.isFtps = true; + } } /** @@ -44,7 +49,8 @@ public class FTPSDelegate extends BaseDelegate @CheckResult(suggest = Suggest.TO_CONTROLLER) public FTPSDelegate setProtocol(@ProtocolType String protocol) { if (TextUtils.isEmpty(protocol)) { - throw new NullPointerException("协议为空"); + ALog.e(TAG, "设置协议失败,协议信息为空"); + return this; } mUrlEntity.protocol = protocol; return this; @@ -58,7 +64,8 @@ public class FTPSDelegate extends BaseDelegate @CheckResult(suggest = Suggest.TO_CONTROLLER) public FTPSDelegate setAlias(String keyAlias) { if (TextUtils.isEmpty(keyAlias)) { - throw new NullPointerException("别名为空"); + ALog.e(TAG, "设置证书别名失败,证书别名为空"); + return this; } mUrlEntity.keyAlias = keyAlias; return this; @@ -72,7 +79,8 @@ public class FTPSDelegate extends BaseDelegate @CheckResult(suggest = Suggest.TO_CONTROLLER) public FTPSDelegate setStorePass(String storePass) { if (TextUtils.isEmpty(storePass)) { - throw new NullPointerException("证书密码为空"); + ALog.e(TAG, "设置证书密码失败,证书密码为空"); + return this; } mUrlEntity.storePass = storePass; return this; @@ -86,7 +94,8 @@ public class FTPSDelegate extends BaseDelegate @CheckResult(suggest = Suggest.TO_CONTROLLER) public FTPSDelegate setStorePath(String storePath) { if (TextUtils.isEmpty(storePath)) { - throw new NullPointerException("证书路径为空"); + ALog.e(TAG, "设置证书路径失败,证书路径为空"); + return this; } mUrlEntity.storePath = storePath; 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/FtpDelegate.java similarity index 80% rename from Aria/src/main/java/com/arialyy/aria/core/common/ftp/FtpDelegate.java rename to Aria/src/main/java/com/arialyy/aria/core/common/FtpDelegate.java index 2c019156..be681bd2 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/common/ftp/FtpDelegate.java +++ b/Aria/src/main/java/com/arialyy/aria/core/common/FtpDelegate.java @@ -13,15 +13,16 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.arialyy.aria.core.common.ftp; +package com.arialyy.aria.core.common; import android.text.TextUtils; import androidx.annotation.CheckResult; import aria.apache.commons.net.ftp.FTPClientConfig; -import com.arialyy.aria.core.common.BaseDelegate; -import com.arialyy.aria.core.common.Suggest; +import com.arialyy.aria.core.FtpUrlEntity; +import com.arialyy.aria.core.inf.Suggest; import com.arialyy.aria.core.inf.AbsTarget; -import com.arialyy.aria.core.inf.AbsTaskWrapper; +import com.arialyy.aria.core.wrapper.AbsTaskWrapper; +import com.arialyy.aria.core.inf.IOptionConstant; import com.arialyy.aria.util.ALog; /** @@ -39,7 +40,7 @@ public class FtpDelegate extends BaseDelegate if (TextUtils.isEmpty(charSet)) { throw new NullPointerException("字符编码为空"); } - getTaskWrapper().asFtp().setCharSet(charSet); + getTaskWrapper().getOptionParams().setParams(IOptionConstant.charSet, charSet); return this; } @@ -58,7 +59,13 @@ public class FtpDelegate extends BaseDelegate return this; } // urlEntity 不能在构造函数中获取,因为ftp上传时url是后于构造函数的 - FtpUrlEntity urlEntity = getTaskWrapper().asFtp().getUrlEntity(); + FtpUrlEntity urlEntity = + (FtpUrlEntity) getTaskWrapper().getOptionParams() + .getParam(IOptionConstant.ftpUrlEntity); + if (urlEntity == null) { + ALog.e(TAG, "设置登陆信息失败,FtpUrlEntity为空"); + return this; + } urlEntity.needLogin = true; urlEntity.user = userName; urlEntity.password = password; @@ -81,7 +88,7 @@ public class FtpDelegate extends BaseDelegate */ @CheckResult(suggest = Suggest.TO_CONTROLLER) public FtpDelegate setFtpClentConfig(FTPClientConfig config) { - getTaskWrapper().asFtp().setClientConfig(config); + getTaskWrapper().getOptionParams().setParams(IOptionConstant.clientConfig, config); return this; } 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/HttpDelegate.java similarity index 58% rename from Aria/src/main/java/com/arialyy/aria/core/common/http/HttpDelegate.java rename to Aria/src/main/java/com/arialyy/aria/core/common/HttpDelegate.java index 10ebeea7..8e0d2505 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/common/http/HttpDelegate.java +++ b/Aria/src/main/java/com/arialyy/aria/core/common/HttpDelegate.java @@ -13,28 +13,28 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.arialyy.aria.core.common.http; +package com.arialyy.aria.core.common; import android.text.TextUtils; import androidx.annotation.CheckResult; import androidx.annotation.NonNull; -import com.arialyy.aria.core.common.BaseDelegate; -import com.arialyy.aria.core.common.RequestEnum; -import com.arialyy.aria.core.common.Suggest; +import com.arialyy.aria.core.inf.Suggest; import com.arialyy.aria.core.inf.AbsTarget; -import com.arialyy.aria.core.inf.AbsTaskWrapper; +import com.arialyy.aria.core.wrapper.AbsTaskWrapper; +import com.arialyy.aria.core.inf.IOptionConstant; import com.arialyy.aria.util.ALog; import java.net.Proxy; -import java.util.Collection; import java.util.HashMap; import java.util.Map; -import java.util.Set; /** * HTTP协议处理 */ public class HttpDelegate extends BaseDelegate { + private Map params; + private Map headers; + public HttpDelegate(TARGET target, AbsTaskWrapper wrapper) { super(target, wrapper); } @@ -46,7 +46,7 @@ public class HttpDelegate extends BaseDelegate */ @CheckResult(suggest = Suggest.TO_CONTROLLER) public HttpDelegate setRequestType(RequestEnum requestEnum) { - getTaskWrapper().asHttp().setRequestEnum(requestEnum); + getTaskWrapper().getOptionParams().setParams(IOptionConstant.requestEnum, requestEnum); return this; } @@ -55,7 +55,11 @@ public class HttpDelegate extends BaseDelegate */ @CheckResult(suggest = Suggest.TO_CONTROLLER) public HttpDelegate setParams(Map params) { - getTaskWrapper().asHttp().setParams(params); + if (this.params == null) { + this.params = new HashMap<>(); + } + this.params.putAll(params); + getTaskWrapper().getOptionParams().setParams(IOptionConstant.params, this.params); return this; } @@ -68,12 +72,11 @@ public class HttpDelegate extends BaseDelegate ALog.d(TAG, "key 或value 为空"); return this; } - Map params = getTaskWrapper().asHttp().getParams(); if (params == null) { params = new HashMap<>(); - getTaskWrapper().asHttp().setParams(params); } params.put(key, value); + getTaskWrapper().getOptionParams().setParams(IOptionConstant.params, params); return this; } @@ -82,7 +85,7 @@ public class HttpDelegate extends BaseDelegate */ @CheckResult(suggest = Suggest.TO_CONTROLLER) public HttpDelegate setFormFields(Map params) { - getTaskWrapper().asHttp().setFormFields(params); + getTaskWrapper().getOptionParams().setParams(IOptionConstant.formFields, params); return this; } @@ -102,7 +105,11 @@ public class HttpDelegate extends BaseDelegate ALog.w(TAG, "设置header失败,header对应的value不能为null"); return this; } - addHeader(getTaskWrapper(), key, value); + if (this.headers == null) { + this.headers = new HashMap<>(); + } + this.headers.put(key, value); + getTaskWrapper().getOptionParams().setParams(IOptionConstant.headers, this.headers); return this; } @@ -118,7 +125,11 @@ public class HttpDelegate extends BaseDelegate ALog.w(TAG, "设置header失败,map没有header数据"); return this; } - addHeaders(getTaskWrapper(), headers); + if (this.headers == null) { + this.headers = new HashMap<>(); + } + this.headers.putAll(headers); + getTaskWrapper().getOptionParams().setParams(IOptionConstant.headers, this.headers); return this; } @@ -127,60 +138,7 @@ public class HttpDelegate extends BaseDelegate */ @CheckResult(suggest = Suggest.TO_CONTROLLER) public HttpDelegate setUrlProxy(Proxy proxy) { - getTaskWrapper().asHttp().setProxy(proxy); + getTaskWrapper().getOptionParams().setParams(IOptionConstant.proxy, proxy); return this; } - - private void addHeader(AbsTaskWrapper taskWrapper, String key, String value) { - HttpTaskConfig taskDelegate = taskWrapper.asHttp(); - if (taskDelegate.getHeaders().get(key) == null) { - taskDelegate.getHeaders().put(key, value); - } else if (!taskDelegate.getHeaders().get(key).equals(value)) { - taskDelegate.getHeaders().put(key, value); - } - } - - private void addHeaders(AbsTaskWrapper taskWrapper, Map headers) { - HttpTaskConfig taskDelegate = taskWrapper.asHttp(); - /* - 两个map比较逻辑 - 1、比对key是否相同 - 2、如果key相同,比对value是否相同 - 3、只有当上面两个步骤中key 和 value都相同时才能任务两个map数据一致 - */ - boolean mapEquals = false; - if (taskDelegate.getHeaders().size() == headers.size()) { - int i = 0; - Set keys = taskDelegate.getHeaders().keySet(); - for (String key : keys) { - if (headers.containsKey(key)) { - i++; - } else { - break; - } - } - if (i == taskDelegate.getHeaders().size()) { - int j = 0; - Collection values = taskDelegate.getHeaders().values(); - for (String value : values) { - if (headers.containsValue(value)) { - j++; - } else { - break; - } - } - if (j == taskDelegate.getHeaders().size()) { - mapEquals = true; - } - } - } - - if (!mapEquals) { - taskDelegate.getHeaders().clear(); - Set keys = headers.keySet(); - for (String key : keys) { - taskDelegate.getHeaders().put(key, headers.get(key)); - } - } - } } diff --git a/Aria/src/main/java/com/arialyy/aria/core/common/RecordHandler.java b/Aria/src/main/java/com/arialyy/aria/core/common/RecordHandler.java deleted file mode 100644 index f39f4c59..00000000 --- a/Aria/src/main/java/com/arialyy/aria/core/common/RecordHandler.java +++ /dev/null @@ -1,457 +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.common; - -import com.arialyy.aria.core.config.Configuration; -import com.arialyy.aria.core.download.DTaskWrapper; -import com.arialyy.aria.core.download.DownloadEntity; -import com.arialyy.aria.core.download.m3u8.BaseM3U8Loader; -import com.arialyy.aria.core.inf.AbsNormalEntity; -import com.arialyy.aria.core.inf.AbsTaskWrapper; -import com.arialyy.aria.core.inf.ITaskWrapper; -import com.arialyy.aria.core.upload.UploadEntity; -import com.arialyy.aria.orm.DbEntity; -import com.arialyy.aria.util.ALog; -import com.arialyy.aria.util.CommonUtil; -import com.arialyy.aria.util.DbDataHelper; -import com.arialyy.aria.util.FileUtil; -import com.arialyy.aria.util.RecordUtil; -import java.io.File; -import java.util.ArrayList; -import java.util.HashSet; -import java.util.List; -import java.util.Properties; -import java.util.Set; - -/** - * 处理任务记录,分配线程区间 - */ -public class RecordHandler { - private final String TAG = "RecordHandler"; - - public static final int TYPE_DOWNLOAD = 1; - public static final int TYPE_UPLOAD = 2; - public static final int TYPE_M3U8_VOD = 3; - public static final int TYPE_M3U8_LIVE = 4; - - private static final String STATE = "_state_"; - private static final String RECORD = "_record_"; - /** - * 小于1m的文件不启用多线程 - */ - private static final long SUB_LEN = 1024 * 1024; - - /** - * 分块文件路径 - */ - public static final String SUB_PATH = "%s.%s.part"; - - @Deprecated private File mConfigFile; - private TaskRecord mTaskRecord; - private AbsTaskWrapper mTaskWrapper; - private AbsNormalEntity mEntity; - - RecordHandler(AbsTaskWrapper wrapper) { - mTaskWrapper = wrapper; - mEntity = (AbsNormalEntity) mTaskWrapper.getEntity(); - } - - /** - * 获取任务记录,如果任务记录存在,检查任务记录 - * 检查记录 对于分块任务: 子分块不存在或被删除,子线程将重新下载 - * 对于普通任务: 预下载文件不存在,则任务任务呗删除 - * 如果任务记录不存在或线程记录不存在,初始化记录 - * - * @return 任务记录 - */ - TaskRecord getRecord() { - mConfigFile = new File(CommonUtil.getFileConfigPath(false, mEntity.getFileName())); - if (mConfigFile.exists()) { - convertDb(); - } else { - mTaskRecord = DbDataHelper.getTaskRecord(getFilePath()); - if (mTaskRecord == null) { - initRecord(true); - } else { - if (mTaskRecord.threadRecords == null || mTaskRecord.threadRecords.isEmpty()) { - initRecord(false); - } - - if (mTaskWrapper.getRequestType() == ITaskWrapper.M3U8_VOD) { - handleM3U8Record(); - } else if (mTaskWrapper.getRequestType() == ITaskWrapper.M3U8_LIVE) { - ALog.i(TAG, "直播下载不处理历史记录"); - } else { - if (mTaskRecord.isBlock) { - handleBlockRecord(); - } else if (!mTaskWrapper.isSupportBP()) { - handleNoSupportBPRecord(); - } else if (!mTaskRecord.isBlock && mTaskRecord.threadNum > 1) { - handleNoBlockMultiThreadRecord(); - } else { - handleSingleThreadRecord(); - } - } - } - } - saveRecord(); - return mTaskRecord; - } - - /** - * 处理m3u8记录, - * 1、如果分片文件存在,并且分片文件的记录没有完成,则需要删除该分片文件 - * 2、如果记录显示已完成,但是分片文件不存在,则重新开始该分片 - * 3、如果记录显示已完成,并且文件存在,记录当前任务进度 - */ - private void handleM3U8Record() { - DTaskWrapper wrapper = (DTaskWrapper) mTaskWrapper; - String cacheDir = wrapper.asM3U8().getCacheDir(); - long currentProgress = 0; - int completeNum = 0; - for (ThreadRecord record : mTaskRecord.threadRecords) { - File temp = new File(BaseM3U8Loader.getTsFilePath(cacheDir, record.threadId)); - if (!record.isComplete) { - if (temp.exists()) { - temp.delete(); - } - record.startLocation = 0; - //ALog.d(TAG, String.format("分片【%s】未完成,将重新下载该分片", record.threadId)); - } else { - if (!temp.exists()) { - record.startLocation = 0; - record.isComplete = false; - ALog.w(TAG, String.format("分片【%s】不存在,将重新下载该分片", record.threadId)); - } else { - completeNum++; - currentProgress += temp.length(); - } - } - } - wrapper.asM3U8().setCompleteNum(completeNum); - wrapper.getEntity().setCurrentProgress(currentProgress); - mTaskRecord.bandWidth = wrapper.asM3U8().getBandWidth(); - } - - /** - * 处理不支持断点的记录 - */ - private void handleNoSupportBPRecord() { - ThreadRecord tr = mTaskRecord.threadRecords.get(0); - tr.startLocation = 0; - tr.endLocation = mEntity.getFileSize(); - tr.taskKey = mTaskRecord.filePath; - tr.blockLen = tr.endLocation; - tr.isComplete = false; - } - - /** - * 处理为不分块的多线程任务 - */ - private void handleNoBlockMultiThreadRecord() { - File file = new File(mTaskRecord.filePath); - if (!file.exists()) { - ALog.w(TAG, String.format("文件【%s】不存在,重新分配线程区间", mTaskRecord.filePath)); - DbEntity.deleteData(ThreadRecord.class, "taskKey=?", mTaskRecord.filePath); - initRecord(false); - } - } - - /** - * 处理单线程的任务的记录 - */ - private void handleSingleThreadRecord() { - File file = new File(mTaskRecord.filePath); - ThreadRecord tr = mTaskRecord.threadRecords.get(0); - if (!file.exists()) { - ALog.w(TAG, String.format("文件【%s】不存在,任务将重新开始", file.getPath())); - tr.startLocation = 0; - tr.isComplete = false; - tr.endLocation = mEntity.getFileSize(); - } else if (mTaskRecord.isOpenDynamicFile) { - if (file.length() > mEntity.getFileSize()) { - ALog.i(TAG, String.format("文件【%s】错误,任务重新开始", file.getPath())); - file.delete(); - tr.startLocation = 0; - tr.isComplete = false; - tr.endLocation = mEntity.getFileSize(); - } else if (file.length() == mEntity.getFileSize()) { - tr.isComplete = true; - } else { - if (file.length() != tr.startLocation) { - ALog.i(TAG, String.format("修正【%s】的进度记录为:%s", file.getPath(), file.length())); - tr.startLocation = file.length(); - tr.isComplete = false; - } - } - } - mTaskWrapper.setNewTask(false); - } - - /** - * 处理分块任务的记录,分块文件(blockFileLen)长度必须需要小于等于线程区间(threadRectLen)的长度 - */ - private void handleBlockRecord() { - // 默认线程分块长度 - long normalRectLen = mEntity.getFileSize() / mTaskRecord.threadRecords.size(); - for (ThreadRecord tr : mTaskRecord.threadRecords) { - long threadRect = tr.blockLen; - - File temp = new File(String.format(SUB_PATH, mTaskRecord.filePath, tr.threadId)); - if (!temp.exists()) { - ALog.i(TAG, String.format("分块文件【%s】不存在,该分块将重新开始", temp.getPath())); - tr.isComplete = false; - tr.startLocation = tr.threadId * normalRectLen; - } else { - if (!tr.isComplete) { - ALog.i(TAG, String.format( - "startLocation = %s; endLocation = %s; block = %s; tempLen = %s; threadId = %s", - tr.startLocation, tr.endLocation, threadRect, temp.length(), tr.threadId)); - - long blockFileLen = temp.length(); // 磁盘中的分块文件长度 - /* - * 检查磁盘中的分块文件 - */ - if (blockFileLen > threadRect) { - ALog.i(TAG, String.format("分块【%s】错误,分块长度【%s】 > 线程区间长度【%s】,将重新开始该分块", - tr.threadId, blockFileLen, threadRect)); - temp.delete(); - tr.startLocation = tr.threadId * threadRect; - continue; - } - - long realLocation = - tr.threadId * normalRectLen + blockFileLen; //正常情况下,该线程的startLocation的位置 - /* - * 检查记录文件 - */ - if (blockFileLen == threadRect) { - ALog.i(TAG, String.format("分块【%s】已完成,更新记录", temp.getPath())); - tr.startLocation = blockFileLen; - tr.isComplete = true; - } else if (tr.startLocation != realLocation) { // 处理记录小于分块文件长度的情况 - ALog.i(TAG, String.format("修正分块【%s】的进度记录为:%s", temp.getPath(), realLocation)); - tr.startLocation = realLocation; - } - } else { - ALog.i(TAG, String.format("分块【%s】已完成", temp.getPath())); - } - } - } - mTaskWrapper.setNewTask(false); - } - - /** - * convertDb 是兼容性代码 从3.4.1开始,线程配置信息将存储在数据库中。 将配置文件的内容复制到数据库中,并将配置文件删除 - */ - private void convertDb() { - List records = - DbEntity.findRelationData(RecordWrapper.class, "TaskRecord.filePath=?", - getFilePath()); - if (records == null || records.size() == 0) { - Properties pro = FileUtil.loadConfig(mConfigFile); - if (pro.isEmpty()) { - ALog.d(TAG, "老版本的线程记录为空,任务为新任务"); - initRecord(true); - return; - } - - Set keys = pro.keySet(); - // 老版本记录是5s存一次,但是5s中内,如果线程执行完成,record记录是没有的,只有state记录... - // 第一步应该是record 和 state去重取正确的线程数 - Set set = new HashSet<>(); - for (Object key : keys) { - String str = String.valueOf(key); - int i = Integer.parseInt(str.substring(str.length() - 1)); - set.add(i); - } - int threadNum = set.size(); - if (threadNum == 0) { - ALog.d(TAG, "线程数为空,任务为新任务"); - initRecord(true); - return; - } - mTaskWrapper.setNewTask(false); - mTaskRecord = createTaskRecord(threadNum); - mTaskRecord.isOpenDynamicFile = false; - mTaskRecord.isBlock = false; - File tempFile = new File(getFilePath()); - for (int i = 0; i < threadNum; i++) { - ThreadRecord tRecord = new ThreadRecord(); - tRecord.taskKey = mTaskRecord.filePath; - Object state = pro.getProperty(tempFile.getName() + STATE + i); - Object record = pro.getProperty(tempFile.getName() + RECORD + i); - if (state != null && Integer.parseInt(String.valueOf(state)) == 1) { - tRecord.isComplete = true; - continue; - } - if (record != null) { - long temp = Long.parseLong(String.valueOf(record)); - tRecord.startLocation = temp > 0 ? temp : 0; - } else { - tRecord.startLocation = 0; - } - mTaskRecord.threadRecords.add(tRecord); - } - mConfigFile.delete(); - } - } - - /** - * 初始化任务记录,分配线程区间 - * - * @param newRecord {@code true} 需要创建新{@link TaskRecord} - */ - private void initRecord(boolean newRecord) { - if (newRecord) { - mTaskRecord = createTaskRecord(getNewTaskThreadNum()); - } - mTaskWrapper.setNewTask(true); - int requestType = mTaskWrapper.getRequestType(); - if (requestType == ITaskWrapper.M3U8_LIVE) { - return; - } - long blockSize = mEntity.getFileSize() / mTaskRecord.threadNum; - // 处理线程区间记录 - for (int i = 0; i < mTaskRecord.threadNum; i++) { - long startL = i * blockSize, endL = (i + 1) * blockSize; - ThreadRecord tr; - tr = new ThreadRecord(); - tr.taskKey = mTaskRecord.filePath; - tr.threadId = i; - tr.startLocation = startL; - tr.isComplete = false; - if (requestType == ITaskWrapper.M3U8_VOD) { - tr.startLocation = 0; - tr.threadType = TaskRecord.TYPE_M3U8_VOD; - tr.tsUrl = ((DTaskWrapper) mTaskWrapper).asM3U8().getUrls().get(i); - } else { - tr.threadType = TaskRecord.TYPE_HTTP_FTP; - //最后一个线程的结束位置即为文件的总长度 - if (i == (mTaskRecord.threadNum - 1)) { - endL = mEntity.getFileSize(); - } - tr.endLocation = endL; - tr.blockLen = RecordUtil.getBlockLen(mEntity.getFileSize(), i, mTaskRecord.threadNum); - } - mTaskRecord.threadRecords.add(tr); - } - } - - /** - * 创建任务记录 - * - * @param threadNum 线程总数 - */ - private TaskRecord createTaskRecord(int threadNum) { - TaskRecord record = new TaskRecord(); - record.fileName = mEntity.getFileName(); - record.filePath = getFilePath(); - record.threadRecords = new ArrayList<>(); - record.threadNum = threadNum; - int requestType = mTaskWrapper.getRequestType(); - if (requestType == ITaskWrapper.M3U8_VOD) { - record.taskType = TaskRecord.TYPE_M3U8_VOD; - record.isOpenDynamicFile = true; - record.bandWidth = ((DTaskWrapper)mTaskWrapper).asM3U8().getBandWidth(); - } else if (requestType == ITaskWrapper.M3U8_LIVE) { - record.taskType = TaskRecord.TYPE_M3U8_LIVE; - record.isOpenDynamicFile = true; - record.bandWidth = ((DTaskWrapper)mTaskWrapper).asM3U8().getBandWidth(); - } else { - if (getRecordType() == TYPE_DOWNLOAD) { - record.isBlock = threadNum > 1 && Configuration.getInstance().downloadCfg.isUseBlock(); - // 线程数为1,或者使用了分块,则认为是使用动态长度文件 - record.isOpenDynamicFile = threadNum == 1 || record.isBlock; - } else { - record.isBlock = false; - } - record.taskType = TaskRecord.TYPE_HTTP_FTP; - record.isGroupRecord = mEntity.isGroupChild(); - if (record.isGroupRecord) { - if (mEntity instanceof DownloadEntity) { - record.dGroupHash = ((DownloadEntity) mEntity).getGroupHash(); - } - } - } - - return record; - } - - /** - * 保存任务记录 - */ - private void saveRecord() { - mTaskRecord.threadNum = mTaskRecord.threadRecords.size(); - mTaskRecord.save(); - if (mTaskRecord.threadRecords != null && !mTaskRecord.threadRecords.isEmpty()) { - DbEntity.saveAll(mTaskRecord.threadRecords); - } - ALog.d(TAG, String.format("保存记录,线程记录数:%s", mTaskRecord.threadRecords.size())); - } - - /** - * 获取记录类型 - * - * @return {@link #TYPE_DOWNLOAD}、{@link #TYPE_UPLOAD} - */ - private int getRecordType() { - if (mEntity instanceof DownloadEntity) { - return TYPE_DOWNLOAD; - } else { - return TYPE_UPLOAD; - } - } - - /** - * 获取任务路径 - * - * @return 任务文件路径 - */ - private String getFilePath() { - if (mEntity instanceof DownloadEntity) { - return ((DownloadEntity) mTaskWrapper.getEntity()).getFilePath(); - } else { - return ((UploadEntity) mTaskWrapper.getEntity()).getFilePath(); - } - } - - /** - * 小于1m的文件或是任务组的子任务、线程数强制为1 - * 不支持断点或chunked模式的线程数都为,线程数强制为1 - */ - private int getNewTaskThreadNum() { - if (getRecordType() == TYPE_DOWNLOAD) { - if (mTaskWrapper.getRequestType() == ITaskWrapper.M3U8_VOD) { - return ((DTaskWrapper) mTaskWrapper).asM3U8().getUrls().size(); - } - if (mTaskWrapper.getRequestType() == ITaskWrapper.M3U8_LIVE) { - return 1; - } - if (!mTaskWrapper.isSupportBP() || mTaskWrapper.asHttp().isChunked()) { - return 1; - } - int threadNum = Configuration.getInstance().downloadCfg.getThreadNum(); - return mEntity.getFileSize() <= SUB_LEN - || mEntity.isGroupChild() - || threadNum == 1 - ? 1 - : threadNum; - } else { - return 1; - } - } -} diff --git a/Aria/src/main/java/com/arialyy/aria/core/common/controller/BuilderController.java b/Aria/src/main/java/com/arialyy/aria/core/common/controller/BuilderController.java index 78f102f2..bbf4db72 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/common/controller/BuilderController.java +++ b/Aria/src/main/java/com/arialyy/aria/core/common/controller/BuilderController.java @@ -17,8 +17,8 @@ package com.arialyy.aria.core.common.controller; import com.arialyy.aria.core.command.NormalCmdFactory; import com.arialyy.aria.core.event.EventMsgUtil; -import com.arialyy.aria.core.inf.AbsTaskWrapper; -import com.arialyy.aria.util.CommonUtil; +import com.arialyy.aria.core.wrapper.AbsTaskWrapper; +import com.arialyy.aria.core.command.CmdHelper; /** * 创建任务时使用的控制器 @@ -37,7 +37,7 @@ public final class BuilderController extends FeatureController implements IStart public long add() { if (checkConfig()) { EventMsgUtil.getDefault() - .post(CommonUtil.createNormalCmd(getTaskWrapper(), NormalCmdFactory.TASK_CREATE, + .post(CmdHelper.createNormalCmd(getTaskWrapper(), NormalCmdFactory.TASK_CREATE, checkTaskType())); return getEntity().getId(); } @@ -52,7 +52,7 @@ public final class BuilderController extends FeatureController implements IStart public long create() { if (checkConfig()) { EventMsgUtil.getDefault() - .post(CommonUtil.createNormalCmd(getTaskWrapper(), NormalCmdFactory.TASK_START, + .post(CmdHelper.createNormalCmd(getTaskWrapper(), NormalCmdFactory.TASK_START, checkTaskType())); return getEntity().getId(); } @@ -63,7 +63,7 @@ public final class BuilderController extends FeatureController implements IStart @Override public long setHighestPriority() { if (checkConfig()) { EventMsgUtil.getDefault() - .post(CommonUtil.createNormalCmd(getTaskWrapper(), NormalCmdFactory.TASK_HIGHEST_PRIORITY, + .post(CmdHelper.createNormalCmd(getTaskWrapper(), NormalCmdFactory.TASK_HIGHEST_PRIORITY, checkTaskType())); return getEntity().getId(); } 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 6e385a19..d3bbca51 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 @@ -25,12 +25,12 @@ import com.arialyy.aria.core.download.CheckDGEntityUtil; import com.arialyy.aria.core.download.CheckFtpDirEntityUtil; import com.arialyy.aria.core.download.DGTaskWrapper; import com.arialyy.aria.core.download.DTaskWrapper; -import com.arialyy.aria.core.inf.AbsEntity; -import com.arialyy.aria.core.inf.AbsTaskWrapper; +import com.arialyy.aria.core.common.AbsEntity; +import com.arialyy.aria.core.wrapper.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.ISchedulers; +import com.arialyy.aria.core.task.ITask; +import com.arialyy.aria.core.wrapper.ITaskWrapper; +import com.arialyy.aria.core.listener.ISchedulers; import com.arialyy.aria.core.scheduler.TaskSchedulers; import com.arialyy.aria.core.upload.CheckUEntityUtil; import com.arialyy.aria.core.upload.UTaskWrapper; diff --git a/Aria/src/main/java/com/arialyy/aria/core/common/controller/NormalController.java b/Aria/src/main/java/com/arialyy/aria/core/common/controller/NormalController.java index 6cb27d12..9567d908 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/common/controller/NormalController.java +++ b/Aria/src/main/java/com/arialyy/aria/core/common/controller/NormalController.java @@ -18,9 +18,9 @@ package com.arialyy.aria.core.common.controller; import com.arialyy.aria.core.command.CancelCmd; import com.arialyy.aria.core.command.NormalCmdFactory; import com.arialyy.aria.core.event.EventMsgUtil; -import com.arialyy.aria.core.inf.AbsTaskWrapper; +import com.arialyy.aria.core.wrapper.AbsTaskWrapper; import com.arialyy.aria.util.ALog; -import com.arialyy.aria.util.CommonUtil; +import com.arialyy.aria.core.command.CmdHelper; /** * 启动控制器 @@ -39,7 +39,7 @@ public final class NormalController extends FeatureController implements INormal public void stop() { if (checkConfig()) { EventMsgUtil.getDefault() - .post(CommonUtil.createNormalCmd(getTaskWrapper(), NormalCmdFactory.TASK_STOP, + .post(CmdHelper.createNormalCmd(getTaskWrapper(), NormalCmdFactory.TASK_STOP, checkTaskType())); } } @@ -51,7 +51,7 @@ public final class NormalController extends FeatureController implements INormal public void resume() { if (checkConfig()) { EventMsgUtil.getDefault() - .post(CommonUtil.createNormalCmd(getTaskWrapper(), NormalCmdFactory.TASK_START, + .post(CmdHelper.createNormalCmd(getTaskWrapper(), NormalCmdFactory.TASK_START, checkTaskType())); } } @@ -63,7 +63,7 @@ public final class NormalController extends FeatureController implements INormal public void cancel() { if (checkConfig()) { EventMsgUtil.getDefault() - .post(CommonUtil.createNormalCmd(getTaskWrapper(), NormalCmdFactory.TASK_CANCEL, + .post(CmdHelper.createNormalCmd(getTaskWrapper(), NormalCmdFactory.TASK_CANCEL, checkTaskType())); } } @@ -76,10 +76,10 @@ public final class NormalController extends FeatureController implements INormal if (checkConfig()) { int taskType = checkTaskType(); EventMsgUtil.getDefault() - .post(CommonUtil.createNormalCmd(getTaskWrapper(), NormalCmdFactory.TASK_STOP, taskType)); + .post(CmdHelper.createNormalCmd(getTaskWrapper(), NormalCmdFactory.TASK_STOP, taskType)); EventMsgUtil.getDefault() .post( - CommonUtil.createNormalCmd(getTaskWrapper(), NormalCmdFactory.TASK_START, taskType)); + CmdHelper.createNormalCmd(getTaskWrapper(), NormalCmdFactory.TASK_START, taskType)); } } @@ -93,7 +93,7 @@ public final class NormalController extends FeatureController implements INormal public void cancel(boolean removeFile) { if (checkConfig()) { CancelCmd cancelCmd = - (CancelCmd) CommonUtil.createNormalCmd(getTaskWrapper(), NormalCmdFactory.TASK_CANCEL, + (CancelCmd) CmdHelper.createNormalCmd(getTaskWrapper(), NormalCmdFactory.TASK_CANCEL, checkTaskType()); cancelCmd.removeFile = removeFile; EventMsgUtil.getDefault().post(cancelCmd); @@ -107,7 +107,7 @@ public final class NormalController extends FeatureController implements INormal public void reStart() { if (checkConfig()) { EventMsgUtil.getDefault() - .post(CommonUtil.createNormalCmd(getTaskWrapper(), NormalCmdFactory.TASK_RESTART, + .post(CmdHelper.createNormalCmd(getTaskWrapper(), NormalCmdFactory.TASK_RESTART, checkTaskType())); } } 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 360cec28..7bc96639 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 @@ -16,11 +16,11 @@ package com.arialyy.aria.core.download; import android.text.TextUtils; -import com.arialyy.aria.core.common.RecordHandler; -import com.arialyy.aria.core.download.m3u8.M3U8Entity; import com.arialyy.aria.core.inf.ICheckEntityUtil; +import com.arialyy.aria.core.inf.IOptionConstant; +import com.arialyy.aria.core.inf.IRecordHandler; import com.arialyy.aria.core.inf.ITargetHandler; -import com.arialyy.aria.core.inf.ITaskWrapper; +import com.arialyy.aria.core.wrapper.ITaskWrapper; import com.arialyy.aria.orm.DbEntity; import com.arialyy.aria.util.ALog; import com.arialyy.aria.util.CheckUtil; @@ -64,20 +64,24 @@ public class CheckDEntityUtil implements ICheckEntityUtil { private void handleM3U8() { File file = new File(mWrapper.getTempFilePath()); + int bandWidth = (int) mWrapper.getM3U8Params().getParam(IOptionConstant.bandWidth); // 缓存文件夹格式:问文件夹/.文件名_码率 - String cacheDir = String.format("%s/.%s_%s", file.getParent(), file.getName(), - mWrapper.asM3U8().getBandWidth()); - mWrapper.asM3U8().setCacheDir(cacheDir); + String cacheDir = String.format("%s/.%s_%s", file.getParent(), file.getName(), bandWidth); + + mWrapper.getM3U8Params().setParams(IOptionConstant.cacheDir, cacheDir); M3U8Entity m3U8Entity = mEntity.getM3U8Entity(); + + Object temp = mWrapper.getM3U8Params().getParam(IOptionConstant.generateIndexFileTemp); + boolean generateIndexFileTemp = temp != null && (boolean) temp; if (m3U8Entity == null) { m3U8Entity = new M3U8Entity(); m3U8Entity.setFilePath(mEntity.getFilePath()); m3U8Entity.setPeerIndex(0); m3U8Entity.setCacheDir(cacheDir); - m3U8Entity.setGenerateIndexFile(mWrapper.asM3U8().isGenerateIndexFileTemp()); + m3U8Entity.setGenerateIndexFile(generateIndexFileTemp); m3U8Entity.insert(); } else { - m3U8Entity.setGenerateIndexFile(mWrapper.asM3U8().isGenerateIndexFileTemp()); + m3U8Entity.setGenerateIndexFile(generateIndexFileTemp); m3U8Entity.update(); } if (mWrapper.getRequestType() == ITaskWrapper.M3U8_VOD) { @@ -92,8 +96,8 @@ public class CheckDEntityUtil implements ICheckEntityUtil { } } - if (mWrapper.asM3U8().getBandWidthUrlConverter() != null - && mWrapper.asM3U8().getBandWidth() == 0) { + if (mWrapper.getM3U8Params().getHandler(IOptionConstant.bandWidthUrlConverter) != null + && bandWidth == 0) { ALog.w(TAG, "你已经设置了码率url转换器,但是没有设置码率,Aria框架将采用第一个获取到的码率"); } } @@ -140,7 +144,7 @@ public class CheckDEntityUtil implements ICheckEntityUtil { return false; } else { ALog.w(TAG, String.format("保存路径【%s】已经被其它任务占用,当前任务将覆盖该路径的文件", filePath)); - RecordUtil.delTaskRecord(filePath, RecordHandler.TYPE_DOWNLOAD); + RecordUtil.delTaskRecord(filePath, IRecordHandler.TYPE_DOWNLOAD); } } @@ -149,7 +153,7 @@ public class CheckDEntityUtil implements ICheckEntityUtil { mEntity.setFileName(newFile.getName()); // 如过使用Content-Disposition中的文件名,将不会执行重命名工作 - if (mWrapper.asHttp().isUseServerFileName() + if ((boolean) mWrapper.getOptionParams().getParam(IOptionConstant.useServerFileName) || mWrapper.getRequestType() == ITaskWrapper.M3U8_LIVE) { return true; } diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/CheckDGEntityUtil.java b/Aria/src/main/java/com/arialyy/aria/core/download/CheckDGEntityUtil.java index 94d3fc8b..8a1b222a 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/CheckDGEntityUtil.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/CheckDGEntityUtil.java @@ -18,6 +18,7 @@ package com.arialyy.aria.core.download; import android.text.TextUtils; import com.arialyy.aria.core.common.RequestEnum; import com.arialyy.aria.core.inf.ICheckEntityUtil; +import com.arialyy.aria.core.inf.IOptionConstant; import com.arialyy.aria.orm.DbEntity; import com.arialyy.aria.util.ALog; import com.arialyy.aria.util.CommonUtil; @@ -133,9 +134,10 @@ public class CheckDGEntityUtil implements ICheckEntityUtil { return false; } - if (mWrapper.asHttp().getRequestEnum() == RequestEnum.POST) { - for (DTaskWrapper subTask : mWrapper.getSubTaskWrapper()) { - subTask.asHttp().setRequestEnum(RequestEnum.POST); + if (mWrapper.getOptionParams().getParam(IOptionConstant.requestEnum) + == RequestEnum.POST) { + for (DTaskWrapper subWrapper : mWrapper.getSubTaskWrapper()) { + subWrapper.getOptionParams().setParams(IOptionConstant.requestEnum, RequestEnum.POST); } } diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/CheckFtpDirEntityUtil.java b/Aria/src/main/java/com/arialyy/aria/core/download/CheckFtpDirEntityUtil.java index 3f82a400..9db5605b 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/CheckFtpDirEntityUtil.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/CheckFtpDirEntityUtil.java @@ -16,7 +16,9 @@ package com.arialyy.aria.core.download; import android.text.TextUtils; +import com.arialyy.aria.core.FtpUrlEntity; import com.arialyy.aria.core.inf.ICheckEntityUtil; +import com.arialyy.aria.core.inf.IOptionConstant; import com.arialyy.aria.orm.DbEntity; import com.arialyy.aria.util.ALog; import java.io.File; @@ -83,12 +85,15 @@ public class CheckFtpDirEntityUtil implements ICheckEntityUtil { if (b) { mEntity.save(); } - if (mWrapper.asFtp().getUrlEntity().isFtps) { - if (TextUtils.isEmpty(mWrapper.asFtp().getUrlEntity().storePath)) { + FtpUrlEntity urlEntity = + (FtpUrlEntity) mWrapper.getOptionParams().getParam(IOptionConstant.ftpUrlEntity); + assert urlEntity != null; + if (urlEntity.isFtps) { + if (TextUtils.isEmpty(urlEntity.storePath)) { ALog.e(TAG, "证书路径为空"); return false; } - if (TextUtils.isEmpty(mWrapper.asFtp().getUrlEntity().keyAlias)) { + if (TextUtils.isEmpty(urlEntity.keyAlias)) { ALog.e(TAG, "证书别名为空"); return false; } 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 71e5d906..fd7ab2ae 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 @@ -33,15 +33,17 @@ import com.arialyy.aria.core.download.target.GroupNormalTarget; import com.arialyy.aria.core.download.target.HttpBuilderTarget; import com.arialyy.aria.core.download.target.HttpNormalTarget; import com.arialyy.aria.core.event.EventMsgUtil; -import com.arialyy.aria.core.inf.AbsEntity; +import com.arialyy.aria.core.common.AbsEntity; import com.arialyy.aria.core.inf.AbsReceiver; -import com.arialyy.aria.core.inf.ITask; +import com.arialyy.aria.core.task.ITask; import com.arialyy.aria.core.inf.ReceiverType; 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; +import com.arialyy.aria.core.command.CmdHelper; import com.arialyy.aria.util.CommonUtil; +import com.arialyy.aria.util.ComponentUtil; import com.arialyy.aria.util.DbDataHelper; import java.util.ArrayList; import java.util.List; @@ -73,6 +75,7 @@ public class DownloadReceiver extends AbsReceiver { */ @CheckResult public HttpBuilderTarget load(@NonNull String url) { + ComponentUtil.getInstance().checkComponentExist(ComponentUtil.COMPONENT_TYPE_HTTP); CheckUtil.checkUrlInvalidThrow(url); return DTargetFactory.getInstance() .generateBuilderTarget(HttpBuilderTarget.class, url); @@ -86,6 +89,7 @@ public class DownloadReceiver extends AbsReceiver { */ @CheckResult public HttpNormalTarget load(long taskId) { + ComponentUtil.getInstance().checkComponentExist(ComponentUtil.COMPONENT_TYPE_HTTP); CheckUtil.checkTaskId(taskId); return DTargetFactory.getInstance() .generateNormalTarget(HttpNormalTarget.class, taskId); @@ -98,6 +102,7 @@ public class DownloadReceiver extends AbsReceiver { */ @CheckResult public GroupBuilderTarget loadGroup(List urls) { + ComponentUtil.getInstance().checkComponentExist(ComponentUtil.COMPONENT_TYPE_HTTP); CheckUtil.checkDownloadUrls(urls); return DTargetFactory.getInstance().generateGroupBuilderTarget(urls); } @@ -110,6 +115,7 @@ public class DownloadReceiver extends AbsReceiver { */ @CheckResult public GroupNormalTarget loadGroup(long taskId) { + ComponentUtil.getInstance().checkComponentExist(ComponentUtil.COMPONENT_TYPE_HTTP); CheckUtil.checkTaskId(taskId); return DTargetFactory.getInstance() .generateNormalTarget(GroupNormalTarget.class, taskId); @@ -120,6 +126,7 @@ public class DownloadReceiver extends AbsReceiver { */ @CheckResult public FtpBuilderTarget loadFtp(@NonNull String url) { + ComponentUtil.getInstance().checkComponentExist(ComponentUtil.COMPONENT_TYPE_FTP); CheckUtil.checkUrlInvalidThrow(url); return DTargetFactory.getInstance() .generateBuilderTarget(FtpBuilderTarget.class, url); @@ -133,6 +140,7 @@ public class DownloadReceiver extends AbsReceiver { */ @CheckResult public FtpNormalTarget loadFtp(long taskId) { + ComponentUtil.getInstance().checkComponentExist(ComponentUtil.COMPONENT_TYPE_FTP); CheckUtil.checkTaskId(taskId); return DTargetFactory.getInstance() .generateNormalTarget(FtpNormalTarget.class, taskId); @@ -143,6 +151,7 @@ public class DownloadReceiver extends AbsReceiver { */ @CheckResult public FtpDirBuilderTarget loadFtpDir(@NonNull String dirUrl) { + ComponentUtil.getInstance().checkComponentExist(ComponentUtil.COMPONENT_TYPE_FTP); CheckUtil.checkUrlInvalidThrow(dirUrl); return DTargetFactory.getInstance().generateDirBuilderTarget(dirUrl); } @@ -155,6 +164,7 @@ public class DownloadReceiver extends AbsReceiver { */ @CheckResult public FtpDirNormalTarget loadFtpDir(long taskId) { + ComponentUtil.getInstance().checkComponentExist(ComponentUtil.COMPONENT_TYPE_FTP); CheckUtil.checkTaskId(taskId); return DTargetFactory.getInstance() .generateNormalTarget(FtpDirNormalTarget.class, taskId); @@ -461,7 +471,7 @@ public class DownloadReceiver extends AbsReceiver { public void removeAllTask(boolean removeFile) { final AriaManager ariaManager = AriaManager.getInstance(); CancelAllCmd cancelCmd = - (CancelAllCmd) CommonUtil.createNormalCmd(new DTaskWrapper(null), + (CancelAllCmd) CmdHelper.createNormalCmd(new DTaskWrapper(null), NormalCmdFactory.TASK_CANCEL_ALL, ITask.DOWNLOAD); cancelCmd.removeFile = removeFile; EventMsgUtil.getDefault().post(cancelCmd); diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/Downloader.java b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/Downloader.java deleted file mode 100644 index 3fb04c6c..00000000 --- a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/Downloader.java +++ /dev/null @@ -1,129 +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.download.downloader; - -import com.arialyy.aria.core.common.AbsThreadTask; -import com.arialyy.aria.core.common.NormalFileer; -import com.arialyy.aria.core.common.RecordHandler; -import com.arialyy.aria.core.common.SubThreadConfig; -import com.arialyy.aria.core.download.DTaskWrapper; -import com.arialyy.aria.core.download.DownloadEntity; -import com.arialyy.aria.core.event.Event; -import com.arialyy.aria.core.event.SpeedEvent; -import com.arialyy.aria.core.inf.IDownloadListener; -import com.arialyy.aria.core.inf.ITaskWrapper; -import com.arialyy.aria.exception.BaseException; -import com.arialyy.aria.exception.TaskException; -import com.arialyy.aria.util.ALog; -import com.arialyy.aria.util.BufferedRandomAccessFile; -import java.io.File; -import java.io.IOException; - -/** - * Created by AriaL on 2017/7/1. 文件下载器 - */ -public class Downloader extends NormalFileer { - private String TAG = "Downloader"; - - public Downloader(IDownloadListener listener, DTaskWrapper taskWrapper) { - super(listener, taskWrapper); - mTempFile = new File(mEntity.getFilePath()); - setUpdateInterval(taskWrapper.getConfig().getUpdateInterval()); - } - - @Override protected boolean handleNewTask() { - if (!mRecord.isBlock) { - if (mTempFile.exists()) { - mTempFile.delete(); - } - //CommonUtil.createFile(mTempFile.getPath()); - } else { - for (int i = 0; i < mTotalThreadNum; i++) { - File blockFile = new File(String.format(RecordHandler.SUB_PATH, mTempFile.getPath(), i)); - if (blockFile.exists()) { - ALog.d(TAG, String.format("分块【%s】已经存在,将删除该分块", i)); - blockFile.delete(); - } - } - } - BufferedRandomAccessFile file = null; - try { - if (mTotalThreadNum > 1 && !mRecord.isBlock) { - file = new BufferedRandomAccessFile(new File(mTempFile.getPath()), "rwd", 8192); - //设置文件长度 - file.setLength(mEntity.getFileSize()); - } - return true; - } catch (IOException e) { - failDownload(new TaskException(TAG, - String.format("下载失败,filePath: %s, url: %s", mEntity.getDownloadPath(), - mEntity.getUrl()), e)); - } finally { - if (file != null) { - try { - file.close(); - } catch (IOException e) { - e.printStackTrace(); - } - } - } - return false; - } - - /** - * 如果使用"Content-Disposition"中的文件名,需要更新{@link #mTempFile}的路径 - */ - void updateTempFile() { - if (!mTempFile.getPath().equals(mEntity.getFilePath())) { - if (!mTempFile.exists()) { - mTempFile = new File(mEntity.getFilePath()); - } else { - boolean b = mTempFile.renameTo(new File(mEntity.getDownloadPath())); - ALog.d(TAG, String.format("更新tempFile文件名%s", b ? "成功" : "失败")); - } - } - } - - @Event - public void setMaxSpeed(SpeedEvent event) { - setMaxSpeed(event.speed); - } - - @Override protected void onPostPre() { - super.onPostPre(); - ((IDownloadListener) mListener).onPostPre(mEntity.getFileSize()); - File file = new File(mEntity.getDownloadPath()); - if (!file.getParentFile().exists()) { - file.getParentFile().mkdirs(); - } - } - - @Override protected AbsThreadTask selectThreadTask(SubThreadConfig config) { - switch (mTaskWrapper.getRequestType()) { - case ITaskWrapper.D_FTP: - case ITaskWrapper.D_FTP_DIR: - return new FtpThreadTask(config); - case ITaskWrapper.D_HTTP: - return new HttpThreadTask(config); - } - return null; - } - - private void failDownload(BaseException e) { - closeTimer(); - mListener.onFail(false, e); - } -} diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/SimpleDownloadUtil.java b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/SimpleDownloadUtil.java deleted file mode 100644 index ad28bb41..00000000 --- a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/SimpleDownloadUtil.java +++ /dev/null @@ -1,140 +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.download.downloader; - -import com.arialyy.aria.core.common.CompleteInfo; -import com.arialyy.aria.core.common.IUtil; -import com.arialyy.aria.core.common.OnFileInfoCallback; -import com.arialyy.aria.core.download.DTaskWrapper; -import com.arialyy.aria.core.inf.AbsEntity; -import com.arialyy.aria.core.inf.IDownloadListener; -import com.arialyy.aria.core.inf.ITaskWrapper; -import com.arialyy.aria.exception.BaseException; - -/** - * Created by lyy on 2015/8/25. - * D_HTTP\FTP单任务下载工具 - */ -public class SimpleDownloadUtil implements IUtil { - private String TAG = "SimpleDownloadUtil"; - private IDownloadListener mListener; - private Downloader mDownloader; - private DTaskWrapper mTaskWrapper; - private boolean isStop = false, isCancel = false; - - public SimpleDownloadUtil(DTaskWrapper wrapper, IDownloadListener downloadListener) { - mTaskWrapper = wrapper; - mListener = downloadListener; - mDownloader = new Downloader(downloadListener, wrapper); - } - - @Override public String getKey() { - return mTaskWrapper.getKey(); - } - - @Override public long getFileSize() { - return mDownloader.getFileSize(); - } - - /** - * 获取当前下载位置 - */ - @Override public long getCurrentLocation() { - return mDownloader.getCurrentLocation(); - } - - @Override public boolean isRunning() { - return mDownloader.isRunning(); - } - - /** - * 取消下载 - */ - @Override public void cancel() { - isCancel = true; - mDownloader.cancel(); - } - - /** - * 停止下载 - */ - @Override public void stop() { - isStop = true; - mDownloader.stop(); - } - - /** - * 多线程断点续传下载文件,开始下载 - */ - @Override public void start() { - if (isStop || isCancel) { - return; - } - mListener.onPre(); - // 如果网址没有变,而服务器端端文件改变,以下代码就没有用了 - //if (mTaskWrapper.getEntity().getFileSize() <= 1 - // || mTaskWrapper.isRefreshInfo() - // || mTaskWrapper.getRequestType() == AbsTaskWrapper.D_FTP - // || mTaskWrapper.getState() == IEntity.STATE_FAIL) { - // new Thread(createInfoThread()).create(); - //} else { - // mDownloader.create(); - //} - new Thread(createInfoThread()).start(); - } - - private void failDownload(BaseException e, boolean needRetry) { - if (isStop || isCancel) { - return; - } - mListener.onFail(needRetry, e); - mDownloader.onDestroy(); - } - - /** - * 通过链接类型创建不同的获取文件信息的线程 - */ - private Runnable createInfoThread() { - switch (mTaskWrapper.getRequestType()) { - case ITaskWrapper.D_FTP: - return new FtpFileInfoThread(mTaskWrapper, new OnFileInfoCallback() { - @Override public void onComplete(String url, CompleteInfo info) { - mDownloader.updateTempFile(); - mDownloader.start(); - } - - @Override public void onFail(AbsEntity entity, BaseException e, boolean needRetry) { - failDownload(e, needRetry); - mDownloader.closeTimer(); - } - }); - case ITaskWrapper.D_HTTP: - return new HttpFileInfoThread(mTaskWrapper, new OnFileInfoCallback() { - @Override public void onComplete(String url, CompleteInfo info) { - mDownloader.updateTempFile(); - mDownloader.start(); - } - - @Override public void onFail(AbsEntity entity, BaseException e, boolean needRetry) { - failDownload(e, needRetry); - mDownloader.closeTimer(); - } - }); - } - return null; - } -} \ No newline at end of file diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/group/SubDLoadUtil.java b/Aria/src/main/java/com/arialyy/aria/core/download/group/SubDLoadUtil.java deleted file mode 100644 index ee9713b8..00000000 --- a/Aria/src/main/java/com/arialyy/aria/core/download/group/SubDLoadUtil.java +++ /dev/null @@ -1,133 +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.download.group; - -import android.os.Handler; -import com.arialyy.aria.core.common.CompleteInfo; -import com.arialyy.aria.core.common.IUtil; -import com.arialyy.aria.core.common.OnFileInfoCallback; -import com.arialyy.aria.core.download.DTaskWrapper; -import com.arialyy.aria.core.download.DownloadEntity; -import com.arialyy.aria.core.download.downloader.Downloader; -import com.arialyy.aria.core.download.downloader.HttpFileInfoThread; -import com.arialyy.aria.core.inf.AbsEntity; -import com.arialyy.aria.core.inf.ITaskWrapper; -import com.arialyy.aria.core.scheduler.ISchedulers; -import com.arialyy.aria.exception.BaseException; -import com.arialyy.aria.util.ALog; - -/** - * 子任务下载器,负责创建{@link Downloader} - */ -class SubDLoadUtil implements IUtil { - private final String TAG = "SubDownloadLoader"; - - private Downloader mDownloader; - private DTaskWrapper mWrapper; - private Handler mSchedulers; - private ChildDLoadListener mListener; - private boolean needGetInfo; - - /** - * @param schedulers 调度器 - * @param needGetInfo {@code true} 需要获取文件信息。{@code false} 不需要获取文件信息 - */ - SubDLoadUtil(Handler schedulers, DTaskWrapper taskWrapper, boolean needGetInfo) { - mWrapper = taskWrapper; - mSchedulers = schedulers; - this.needGetInfo = needGetInfo; - mListener = new ChildDLoadListener(mSchedulers, SubDLoadUtil.this); - } - - @Override public String getKey() { - return mWrapper.getKey(); - } - - public DTaskWrapper getWrapper() { - return mWrapper; - } - - public DownloadEntity getEntity() { - return mWrapper.getEntity(); - } - - /** - * 重新开始任务 - */ - void reStart() { - if (mDownloader != null) { - mDownloader.retryTask(); - } - } - - public Downloader getDownloader() { - return mDownloader; - } - - @Override public long getFileSize() { - return mDownloader == null ? -1 : mDownloader.getFileSize(); - } - - @Override public long getCurrentLocation() { - return mDownloader == null ? -1 : mDownloader.getCurrentLocation(); - } - - @Override public boolean isRunning() { - return mDownloader != null && mDownloader.isRunning(); - } - - @Override public void cancel() { - if (mDownloader != null && isRunning()) { - mDownloader.cancel(); - } else { - mSchedulers.obtainMessage(ISchedulers.CANCEL, this).sendToTarget(); - } - } - - @Override public void stop() { - if (mDownloader != null && isRunning()) { - mDownloader.stop(); - } else { - mSchedulers.obtainMessage(ISchedulers.STOP, this).sendToTarget(); - } - } - - @Override public void start() { - if (mWrapper.getRequestType() == ITaskWrapper.D_HTTP) { - if (needGetInfo) { - new Thread(new HttpFileInfoThread(mWrapper, new OnFileInfoCallback() { - - @Override public void onComplete(String url, CompleteInfo info) { - mDownloader = new Downloader(mListener, mWrapper); - mDownloader.start(); - } - - @Override public void onFail(AbsEntity entity, BaseException e, boolean needRetry) { - mSchedulers.obtainMessage(ISchedulers.FAIL, SubDLoadUtil.this).sendToTarget(); - } - })).start(); - } else { - mDownloader = new Downloader(mListener, mWrapper); - mDownloader.start(); - } - } else if (mWrapper.getRequestType() == ITaskWrapper.D_FTP) { - mDownloader = new Downloader(mListener, mWrapper); - mDownloader.start(); - } else { - ALog.w(TAG, String.format("不识别的类型,requestType:%s", mWrapper.getRequestType())); - } - } -} diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/m3u8/M3U8Delegate.java b/Aria/src/main/java/com/arialyy/aria/core/download/m3u8/M3U8Delegate.java index beea90f8..f498d3cb 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/m3u8/M3U8Delegate.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/m3u8/M3U8Delegate.java @@ -17,10 +17,15 @@ package com.arialyy.aria.core.download.m3u8; import androidx.annotation.CheckResult; import com.arialyy.aria.core.common.BaseDelegate; -import com.arialyy.aria.core.common.Suggest; import com.arialyy.aria.core.download.DTaskWrapper; import com.arialyy.aria.core.inf.AbsTarget; -import com.arialyy.aria.core.inf.AbsTaskWrapper; +import com.arialyy.aria.core.inf.IOptionConstant; +import com.arialyy.aria.core.inf.Suggest; +import com.arialyy.aria.core.processor.IBandWidthUrlConverter; +import com.arialyy.aria.core.processor.ITsMergeHandler; +import com.arialyy.aria.core.processor.IVodTsUrlConverter; +import com.arialyy.aria.core.wrapper.AbsTaskWrapper; +import com.arialyy.aria.util.ComponentUtil; /** * m3u8 委托 @@ -30,6 +35,7 @@ public class M3U8Delegate extends BaseDelegate public M3U8Delegate(TARGET target, AbsTaskWrapper wrapper) { super(target, wrapper); + ComponentUtil.getInstance().checkComponentExist(ComponentUtil.COMPONENT_TYPE_M3U8); mTaskWrapper = (DTaskWrapper) getTaskWrapper(); mTaskWrapper.setRequestType(AbsTaskWrapper.M3U8_VOD); } @@ -40,7 +46,7 @@ public class M3U8Delegate extends BaseDelegate */ @CheckResult(suggest = Suggest.TO_CONTROLLER) public M3U8Delegate generateIndexFile() { - mTaskWrapper.asM3U8().setGenerateIndexFileTemp(true); + mTaskWrapper.getM3U8Params().setParams(IOptionConstant.generateIndexFileTemp, true); return this; } @@ -51,7 +57,7 @@ public class M3U8Delegate extends BaseDelegate */ @CheckResult(suggest = Suggest.TO_CONTROLLER) public M3U8Delegate merge(boolean merge) { - mTaskWrapper.asM3U8().setMergeFile(merge); + mTaskWrapper.getM3U8Params().setParams(IOptionConstant.mergeFile, merge); return this; } @@ -61,7 +67,7 @@ public class M3U8Delegate extends BaseDelegate */ @CheckResult(suggest = Suggest.TO_CONTROLLER) public M3U8Delegate setMergeHandler(ITsMergeHandler handler) { - mTaskWrapper.asM3U8().setMergeHandler(handler); + mTaskWrapper.getM3U8Params().setParams(IOptionConstant.mergeHandler, handler); return this; } @@ -73,7 +79,7 @@ public class M3U8Delegate extends BaseDelegate */ @CheckResult(suggest = Suggest.TO_CONTROLLER) public M3U8Delegate setTsUrlConvert(IVodTsUrlConverter converter) { - mTaskWrapper.asM3U8().setVodUrlConverter(converter); + mTaskWrapper.getM3U8Params().setParams(IOptionConstant.vodUrlConverter, converter); return this; } @@ -84,7 +90,7 @@ public class M3U8Delegate extends BaseDelegate */ @CheckResult(suggest = Suggest.TO_CONTROLLER) public M3U8Delegate setBandWidth(int bandWidth) { - mTaskWrapper.asM3U8().setBandWidth(bandWidth); + mTaskWrapper.getM3U8Params().setParams(IOptionConstant.bandWidth, bandWidth); return this; } @@ -96,7 +102,7 @@ public class M3U8Delegate extends BaseDelegate */ @CheckResult(suggest = Suggest.TO_CONTROLLER) public M3U8Delegate setBandWidthUrlConverter(IBandWidthUrlConverter converter) { - mTaskWrapper.asM3U8().setBandWidthUrlConverter(converter); + mTaskWrapper.getM3U8Params().setParams(IOptionConstant.bandWidthUrlConverter, converter); return this; } diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/m3u8/M3U8LiveDelegate.java b/Aria/src/main/java/com/arialyy/aria/core/download/m3u8/M3U8LiveDelegate.java index edf7cf36..353ce357 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/m3u8/M3U8LiveDelegate.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/m3u8/M3U8LiveDelegate.java @@ -17,10 +17,12 @@ package com.arialyy.aria.core.download.m3u8; import androidx.annotation.CheckResult; import com.arialyy.aria.core.common.BaseDelegate; -import com.arialyy.aria.core.common.Suggest; +import com.arialyy.aria.core.processor.ILiveTsUrlConverter; +import com.arialyy.aria.core.inf.Suggest; import com.arialyy.aria.core.download.DTaskWrapper; import com.arialyy.aria.core.inf.AbsTarget; -import com.arialyy.aria.core.inf.AbsTaskWrapper; +import com.arialyy.aria.core.wrapper.AbsTaskWrapper; +import com.arialyy.aria.core.inf.IOptionConstant; import com.arialyy.aria.util.ALog; /** @@ -41,7 +43,8 @@ public class M3U8LiveDelegate extends BaseDelegate setLiveTsUrlConvert(ILiveTsUrlConverter converter) { - ((DTaskWrapper) getTaskWrapper()).asM3U8().setLiveTsUrlConverter(converter); + ((DTaskWrapper) getTaskWrapper()).getM3U8Params() + .setParams(IOptionConstant.liveTsUrlConverter, converter); return this; } @@ -56,7 +59,8 @@ public class M3U8LiveDelegate extends BaseDelegate extends BaseDelegate extends BaseDelegate extends BaseDelegate mUrls = new ArrayList<>(); - private M3U8VodLoader mLoader; - - public M3U8VodUtil(DTaskWrapper wrapper, M3U8Listener listener) { - mWrapper = wrapper; - mListener = listener; - mLoader = new M3U8VodLoader(mListener, mWrapper); - } - - @Override public String getKey() { - return mWrapper.getKey(); - } - - @Override public long getFileSize() { - return 0; - } - - @Override public long getCurrentLocation() { - return 0; - } - - @Override public boolean isRunning() { - return mLoader.isRunning(); - } - - @Override public void cancel() { - isCancel = true; - mLoader.cancel(); - } - - @Override public void stop() { - isStop = true; - mLoader.stop(); - } - - @Override public void start() { - if (isStop || isCancel) { - return; - } - mListener.onPre(); - // peer数量小于0, - M3U8Entity m3U8Entity = mWrapper.getEntity().getM3U8Entity(); - if (m3U8Entity.getPeerNum() <= 0 || (m3U8Entity.isGenerateIndexFile() && !new File( - String.format(M3U8InfoThread.M3U8_INDEX_FORMAT, - mWrapper.getEntity().getFilePath())).exists())) { - getVodInfo(); - } else { - mLoader.start(); - } - } - - /** - * 获取点播文件信息 - */ - private void getVodInfo() { - M3U8InfoThread thread = new M3U8InfoThread(mWrapper, new OnFileInfoCallback() { - @Override public void onComplete(String key, CompleteInfo info) { - IVodTsUrlConverter converter = mWrapper.asM3U8().getVodUrlConverter(); - if (converter != null) { - if (TextUtils.isEmpty(mWrapper.asM3U8().getBandWidthUrl())) { - mUrls.addAll(converter.convert(mWrapper.getEntity().getUrl(), (List) info.obj)); - } else { - mUrls.addAll( - converter.convert(mWrapper.asM3U8().getBandWidthUrl(), (List) info.obj)); - } - } else { - mUrls.addAll((Collection) info.obj); - } - if (mUrls.isEmpty()) { - failDownload(new M3U8Exception(TAG, "获取地址失败"), false); - return; - } else if (!mUrls.get(0).startsWith("http")) { - failDownload(new M3U8Exception(TAG, "地址错误,请使用IM3U8UrlExtInfHandler处理你的url信息"), false); - return; - } - mWrapper.asM3U8().setUrls(mUrls); - if (isStop) { - mListener.onStop(mWrapper.getEntity().getCurrentProgress()); - } else if (isCancel) { - mListener.onCancel(); - } else { - mLoader.start(); - } - } - - @Override public void onFail(AbsEntity entity, BaseException e, boolean needRetry) { - failDownload(e, needRetry); - } - }); - new Thread(thread).start(); - } - - private void failDownload(BaseException e, boolean needRetry) { - if (isStop || isCancel) { - return; - } - mListener.onFail(needRetry, e); - mLoader.onDestroy(); - } -} diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/target/AbsGroupConfigHandler.java b/Aria/src/main/java/com/arialyy/aria/core/download/target/AbsGroupConfigHandler.java index d3eae6e8..e0fa063b 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/target/AbsGroupConfigHandler.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/target/AbsGroupConfigHandler.java @@ -19,13 +19,13 @@ import android.text.TextUtils; import androidx.annotation.CheckResult; import com.arialyy.aria.core.download.DGTaskWrapper; import com.arialyy.aria.core.download.DownloadGroupEntity; -import com.arialyy.aria.core.download.DownloadGroupTask; +import com.arialyy.aria.core.task.DownloadGroupTask; import com.arialyy.aria.core.event.ErrorEvent; import com.arialyy.aria.core.inf.AbsTarget; import com.arialyy.aria.core.inf.IConfigHandler; import com.arialyy.aria.core.manager.SubTaskManager; import com.arialyy.aria.core.manager.TaskWrapperManager; -import com.arialyy.aria.core.queue.DownloadGroupTaskQueue; +import com.arialyy.aria.core.queue.DGroupTaskQueue; import com.arialyy.aria.orm.DbEntity; import com.arialyy.aria.util.CommonUtil; @@ -77,7 +77,7 @@ abstract class AbsGroupConfigHandler implements IConfi } @Override public boolean isRunning() { - DownloadGroupTask task = DownloadGroupTaskQueue.getInstance().getTask(getEntity().getKey()); + DownloadGroupTask task = DGroupTaskQueue.getInstance().getTask(getEntity().getKey()); return task != null && task.isRunning(); } diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/target/DNormalConfigHandler.java b/Aria/src/main/java/com/arialyy/aria/core/download/target/DNormalConfigHandler.java index 5eeb8aea..32343bc8 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/target/DNormalConfigHandler.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/target/DNormalConfigHandler.java @@ -22,7 +22,7 @@ import com.arialyy.aria.core.event.ErrorEvent; import com.arialyy.aria.core.inf.AbsTarget; import com.arialyy.aria.core.inf.IConfigHandler; import com.arialyy.aria.core.manager.TaskWrapperManager; -import com.arialyy.aria.core.queue.DownloadTaskQueue; +import com.arialyy.aria.core.queue.DTaskQueue; import com.arialyy.aria.orm.DbEntity; import com.arialyy.aria.util.ALog; @@ -83,7 +83,7 @@ class DNormalConfigHandler implements IConfigHandler { } @Override public boolean isRunning() { - return DownloadTaskQueue.getInstance().taskIsRunning(mEntity.getKey()); + return DTaskQueue.getInstance().taskIsRunning(mEntity.getKey()); } void setForceDownload(boolean forceDownload) { diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/target/FtpBuilderTarget.java b/Aria/src/main/java/com/arialyy/aria/core/download/target/FtpBuilderTarget.java index 55c44716..f0ed90dc 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/target/FtpBuilderTarget.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/target/FtpBuilderTarget.java @@ -18,10 +18,11 @@ package com.arialyy.aria.core.download.target; import androidx.annotation.CheckResult; import androidx.annotation.NonNull; 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.Suggest; +import com.arialyy.aria.core.common.FtpDelegate; import com.arialyy.aria.core.download.DownloadEntity; -import com.arialyy.aria.core.inf.ITaskWrapper; +import com.arialyy.aria.core.inf.IOptionConstant; +import com.arialyy.aria.core.wrapper.ITaskWrapper; import com.arialyy.aria.util.CommonUtil; /** @@ -33,7 +34,8 @@ public class FtpBuilderTarget extends AbsBuilderTarget { FtpBuilderTarget(String url) { mConfigHandler = new DNormalConfigHandler<>(this, -1); mConfigHandler.setUrl(url); - getTaskWrapper().asFtp().setUrlEntity(CommonUtil.getFtpUrlInfo(url)); + getTaskWrapper().getOptionParams() + .setParams(IOptionConstant.ftpUrlEntity, CommonUtil.getFtpUrlInfo(url)); getTaskWrapper().setRequestType(ITaskWrapper.D_FTP); } diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/target/FtpDirBuilderTarget.java b/Aria/src/main/java/com/arialyy/aria/core/download/target/FtpDirBuilderTarget.java index a2e97691..421756f7 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/target/FtpDirBuilderTarget.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/target/FtpDirBuilderTarget.java @@ -17,9 +17,10 @@ package com.arialyy.aria.core.download.target; import androidx.annotation.CheckResult; 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.Suggest; +import com.arialyy.aria.core.common.FtpDelegate; import com.arialyy.aria.core.download.DownloadGroupEntity; +import com.arialyy.aria.core.inf.IOptionConstant; import com.arialyy.aria.core.manager.SubTaskManager; import com.arialyy.aria.util.CommonUtil; @@ -33,7 +34,8 @@ public class FtpDirBuilderTarget extends AbsBuilderTarget { FtpDirBuilderTarget(String url) { mConfigHandler = new FtpDirConfigHandler<>(this, -1); getEntity().setGroupHash(url); - getTaskWrapper().asFtp().setUrlEntity(CommonUtil.getFtpUrlInfo(url)); + getTaskWrapper().getOptionParams() + .setParams(IOptionConstant.ftpUrlEntity, CommonUtil.getFtpUrlInfo(url)); } /** diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/target/FtpDirConfigHandler.java b/Aria/src/main/java/com/arialyy/aria/core/download/target/FtpDirConfigHandler.java index 35720be0..f731e41b 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/target/FtpDirConfigHandler.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/target/FtpDirConfigHandler.java @@ -16,10 +16,9 @@ package com.arialyy.aria.core.download.target; import com.arialyy.aria.core.download.DTaskWrapper; -import com.arialyy.aria.core.download.target.AbsGroupConfigHandler; import com.arialyy.aria.core.inf.AbsTarget; -import com.arialyy.aria.core.inf.AbsTaskWrapper; -import com.arialyy.aria.core.inf.ITaskWrapper; +import com.arialyy.aria.core.wrapper.AbsTaskWrapper; +import com.arialyy.aria.core.wrapper.ITaskWrapper; import java.util.List; /** diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/target/FtpDirNormalTarget.java b/Aria/src/main/java/com/arialyy/aria/core/download/target/FtpDirNormalTarget.java index 742f455f..baff2e01 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/target/FtpDirNormalTarget.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/target/FtpDirNormalTarget.java @@ -17,9 +17,10 @@ package com.arialyy.aria.core.download.target; import androidx.annotation.CheckResult; import com.arialyy.aria.core.common.AbsNormalTarget; -import com.arialyy.aria.core.common.Suggest; -import com.arialyy.aria.core.common.ftp.FtpDelegate; +import com.arialyy.aria.core.inf.Suggest; +import com.arialyy.aria.core.common.FtpDelegate; import com.arialyy.aria.core.download.DownloadGroupEntity; +import com.arialyy.aria.core.inf.IOptionConstant; import com.arialyy.aria.core.manager.SubTaskManager; import com.arialyy.aria.util.CommonUtil; @@ -32,7 +33,8 @@ public class FtpDirNormalTarget extends AbsNormalTarget { FtpDirNormalTarget(long taskId) { mConfigHandler = new FtpDirConfigHandler<>(this, taskId); - getTaskWrapper().asFtp().setUrlEntity(CommonUtil.getFtpUrlInfo(getEntity().getKey())); + getTaskWrapper().getOptionParams() + .setParams(IOptionConstant.ftpUrlEntity, CommonUtil.getFtpUrlInfo(getEntity().getKey())); } @Override public boolean isRunning() { diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/target/FtpNormalTarget.java b/Aria/src/main/java/com/arialyy/aria/core/download/target/FtpNormalTarget.java index 8c912f06..c7e48d0d 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/target/FtpNormalTarget.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/target/FtpNormalTarget.java @@ -18,10 +18,11 @@ package com.arialyy.aria.core.download.target; import androidx.annotation.CheckResult; import androidx.annotation.NonNull; import com.arialyy.aria.core.common.AbsNormalTarget; -import com.arialyy.aria.core.common.Suggest; -import com.arialyy.aria.core.common.ftp.FtpDelegate; +import com.arialyy.aria.core.inf.Suggest; +import com.arialyy.aria.core.common.FtpDelegate; import com.arialyy.aria.core.download.DownloadEntity; -import com.arialyy.aria.core.inf.ITaskWrapper; +import com.arialyy.aria.core.inf.IOptionConstant; +import com.arialyy.aria.core.wrapper.ITaskWrapper; import com.arialyy.aria.util.CommonUtil; /** @@ -33,7 +34,8 @@ public class FtpNormalTarget extends AbsNormalTarget { FtpNormalTarget(long taskId) { mConfigHandler = new DNormalConfigHandler<>(this, taskId); - getTaskWrapper().asFtp().setUrlEntity(CommonUtil.getFtpUrlInfo(getEntity().getUrl())); + getTaskWrapper().getOptionParams() + .setParams(IOptionConstant.ftpUrlEntity, CommonUtil.getFtpUrlInfo(getEntity().getUrl())); getTaskWrapper().setRequestType(ITaskWrapper.D_FTP); } diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/target/GroupBuilderTarget.java b/Aria/src/main/java/com/arialyy/aria/core/download/target/GroupBuilderTarget.java index 813561d8..b44f37a4 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/target/GroupBuilderTarget.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/target/GroupBuilderTarget.java @@ -17,12 +17,13 @@ package com.arialyy.aria.core.download.target; import androidx.annotation.CheckResult; 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.Suggest; +import com.arialyy.aria.core.common.HttpDelegate; import com.arialyy.aria.core.download.DGTaskWrapper; -import com.arialyy.aria.core.inf.IHttpFileLenAdapter; -import com.arialyy.aria.core.inf.ITaskWrapper; +import com.arialyy.aria.core.processor.IHttpFileLenAdapter; +import com.arialyy.aria.core.inf.IOptionConstant; import com.arialyy.aria.core.manager.SubTaskManager; +import com.arialyy.aria.core.wrapper.ITaskWrapper; import com.arialyy.aria.util.ALog; import java.util.List; @@ -150,7 +151,7 @@ public class GroupBuilderTarget extends AbsBuilderTarget { if (adapter == null) { throw new IllegalArgumentException("adapter为空"); } - getTaskWrapper().asHttp().setFileLenAdapter(adapter); + getTaskWrapper().getOptionParams().setObjs(IOptionConstant.fileLenAdapter, adapter); return this; } } diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/target/GroupNormalTarget.java b/Aria/src/main/java/com/arialyy/aria/core/download/target/GroupNormalTarget.java index 2e7855b1..e70d2143 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/target/GroupNormalTarget.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/target/GroupNormalTarget.java @@ -17,9 +17,9 @@ package com.arialyy.aria.core.download.target; import androidx.annotation.CheckResult; import com.arialyy.aria.core.common.AbsNormalTarget; -import com.arialyy.aria.core.common.Suggest; -import com.arialyy.aria.core.common.http.HttpDelegate; -import com.arialyy.aria.core.inf.ITaskWrapper; +import com.arialyy.aria.core.inf.Suggest; +import com.arialyy.aria.core.common.HttpDelegate; +import com.arialyy.aria.core.wrapper.ITaskWrapper; import com.arialyy.aria.core.manager.SubTaskManager; import java.util.List; diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/target/HttpBuilderTarget.java b/Aria/src/main/java/com/arialyy/aria/core/download/target/HttpBuilderTarget.java index e5eb6d5b..90112482 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/target/HttpBuilderTarget.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/target/HttpBuilderTarget.java @@ -18,11 +18,12 @@ package com.arialyy.aria.core.download.target; import androidx.annotation.CheckResult; import androidx.annotation.NonNull; 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.common.HttpDelegate; import com.arialyy.aria.core.download.m3u8.M3U8Delegate; -import com.arialyy.aria.core.inf.IHttpFileLenAdapter; -import com.arialyy.aria.core.inf.ITaskWrapper; +import com.arialyy.aria.core.processor.IHttpFileLenAdapter; +import com.arialyy.aria.core.inf.IOptionConstant; +import com.arialyy.aria.core.inf.Suggest; +import com.arialyy.aria.core.wrapper.ITaskWrapper; public class HttpBuilderTarget extends AbsBuilderTarget { @@ -54,7 +55,7 @@ public class HttpBuilderTarget extends AbsBuilderTarget { */ @CheckResult(suggest = Suggest.TASK_CONTROLLER) public HttpBuilderTarget useServerFileName(boolean use) { - getTaskWrapper().asHttp().setUseServerFileName(use); + getTaskWrapper().getOptionParams().setParams(IOptionConstant.useServerFileName, use); return this; } @@ -94,7 +95,8 @@ public class HttpBuilderTarget extends AbsBuilderTarget { if (adapter == null) { throw new IllegalArgumentException("adapter为空"); } - getTaskWrapper().asHttp().setFileLenAdapter(adapter); + + getTaskWrapper().getOptionParams().setParams(IOptionConstant.fileLenAdapter, adapter); return this; } } diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/target/HttpGroupConfigHandler.java b/Aria/src/main/java/com/arialyy/aria/core/download/target/HttpGroupConfigHandler.java index 5ac49fb1..8b8f1d3d 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/target/HttpGroupConfigHandler.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/target/HttpGroupConfigHandler.java @@ -18,7 +18,6 @@ package com.arialyy.aria.core.download.target; import androidx.annotation.CheckResult; import com.arialyy.aria.core.download.DTaskWrapper; import com.arialyy.aria.core.download.DownloadEntity; -import com.arialyy.aria.core.download.target.AbsGroupConfigHandler; import com.arialyy.aria.core.inf.AbsTarget; import com.arialyy.aria.orm.DbEntity; import com.arialyy.aria.util.ALog; diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/target/HttpNormalTarget.java b/Aria/src/main/java/com/arialyy/aria/core/download/target/HttpNormalTarget.java index ada14aff..aed1f7ba 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/target/HttpNormalTarget.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/target/HttpNormalTarget.java @@ -17,8 +17,8 @@ package com.arialyy.aria.core.download.target; import androidx.annotation.CheckResult; import com.arialyy.aria.core.common.AbsNormalTarget; -import com.arialyy.aria.core.common.Suggest; -import com.arialyy.aria.core.common.http.HttpDelegate; +import com.arialyy.aria.core.inf.Suggest; +import com.arialyy.aria.core.common.HttpDelegate; import com.arialyy.aria.core.download.DownloadEntity; import com.arialyy.aria.core.download.m3u8.M3U8Delegate; diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/target/TcpBuilderTarget.java b/Aria/src/main/java/com/arialyy/aria/core/download/target/TcpBuilderTarget.java index 64b6041a..bd2918fe 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/target/TcpBuilderTarget.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/target/TcpBuilderTarget.java @@ -18,9 +18,9 @@ package com.arialyy.aria.core.download.target; import androidx.annotation.CheckResult; import androidx.annotation.NonNull; import com.arialyy.aria.core.common.AbsBuilderTarget; -import com.arialyy.aria.core.common.Suggest; +import com.arialyy.aria.core.inf.Suggest; import com.arialyy.aria.core.download.tcp.TcpDelegate; -import com.arialyy.aria.core.inf.ITaskWrapper; +import com.arialyy.aria.core.wrapper.ITaskWrapper; /** * @Author aria diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/tcp/TcpDelegate.java b/Aria/src/main/java/com/arialyy/aria/core/download/tcp/TcpDelegate.java index b8d395a5..6bbc83c0 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/tcp/TcpDelegate.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/tcp/TcpDelegate.java @@ -18,10 +18,9 @@ package com.arialyy.aria.core.download.tcp; import android.text.TextUtils; import androidx.annotation.CheckResult; import com.arialyy.aria.core.common.BaseDelegate; -import com.arialyy.aria.core.common.Suggest; -import com.arialyy.aria.core.download.DTaskWrapper; +import com.arialyy.aria.core.inf.Suggest; import com.arialyy.aria.core.inf.AbsTarget; -import com.arialyy.aria.core.inf.AbsTaskWrapper; +import com.arialyy.aria.core.wrapper.AbsTaskWrapper; import com.arialyy.aria.util.ALog; import java.nio.charset.Charset; @@ -36,7 +35,7 @@ public class TcpDelegate extends BaseDelegate public TcpDelegate(TARGET target, AbsTaskWrapper wrapper) { super(target, wrapper); - mTcpConfig = ((DTaskWrapper) wrapper).asTcp(); + mTcpConfig = (TcpTaskConfig) wrapper.getTaskOption(); } /** diff --git a/Aria/src/main/java/com/arialyy/aria/core/inf/AbsNormalTask.java b/Aria/src/main/java/com/arialyy/aria/core/inf/AbsNormalTask.java deleted file mode 100644 index d3fcb898..00000000 --- a/Aria/src/main/java/com/arialyy/aria/core/inf/AbsNormalTask.java +++ /dev/null @@ -1,36 +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.inf; - -/** - * Created by lyy on 2017/6/3. - */ -public abstract class AbsNormalTask - extends AbsTask { - - /** - * 最高优先级命令,最高优先级命令有以下属性 - * 1、在下载队列中,有且只有一个最高优先级任务 - * 2、最高优先级任务会一直存在,直到用户手动暂停或任务完成 - * 3、任务调度器不会暂停最高优先级任务 - * 4、用户手动暂停或任务完成后,第二次重新执行该任务,该命令将失效 - * 5、如果下载队列中已经满了,则会停止队尾的任务 - * 6、把任务设置为最高优先级任务后,将自动执行任务,不需要重新调用start()启动任务 - */ - public void setHighestPriority(boolean isHighestPriority) { - isHeighestTask = isHighestPriority; - } -} diff --git a/Aria/src/main/java/com/arialyy/aria/core/inf/AbsReceiver.java b/Aria/src/main/java/com/arialyy/aria/core/inf/AbsReceiver.java index 5d8fefed..95af5111 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/inf/AbsReceiver.java +++ b/Aria/src/main/java/com/arialyy/aria/core/inf/AbsReceiver.java @@ -16,9 +16,9 @@ package com.arialyy.aria.core.inf; -import com.arialyy.aria.core.queue.DownloadGroupTaskQueue; -import com.arialyy.aria.core.queue.DownloadTaskQueue; -import com.arialyy.aria.core.queue.UploadTaskQueue; +import com.arialyy.aria.core.queue.DGroupTaskQueue; +import com.arialyy.aria.core.queue.DTaskQueue; +import com.arialyy.aria.core.queue.UTaskQueue; import java.util.Iterator; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; @@ -85,7 +85,7 @@ public abstract class AbsReceiver implements IReceiver { } /** - * 移除{@link DownloadTaskQueue}、{@link DownloadGroupTaskQueue}、{@link UploadTaskQueue}中注册的观察者 + * 移除{@link DTaskQueue}、{@link DGroupTaskQueue}、{@link UTaskQueue}中注册的观察者 */ protected abstract void unRegisterListener(); } diff --git a/Aria/src/main/java/com/arialyy/aria/core/inf/AbsTarget.java b/Aria/src/main/java/com/arialyy/aria/core/inf/AbsTarget.java index 3c190bf0..3fc67aa2 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/inf/AbsTarget.java +++ b/Aria/src/main/java/com/arialyy/aria/core/inf/AbsTarget.java @@ -17,7 +17,8 @@ package com.arialyy.aria.core.inf; import android.text.TextUtils; import androidx.annotation.CheckResult; -import com.arialyy.aria.core.common.Suggest; +import com.arialyy.aria.core.common.AbsEntity; +import com.arialyy.aria.core.wrapper.AbsTaskWrapper; import com.arialyy.aria.core.common.controller.BuilderController; import com.arialyy.aria.core.common.controller.NormalController; import com.arialyy.aria.util.ALog; diff --git a/Aria/src/main/java/com/arialyy/aria/core/inf/IConfigHandler.java b/Aria/src/main/java/com/arialyy/aria/core/inf/IConfigHandler.java index 445b23f5..98ede7ef 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/inf/IConfigHandler.java +++ b/Aria/src/main/java/com/arialyy/aria/core/inf/IConfigHandler.java @@ -15,6 +15,8 @@ */ package com.arialyy.aria.core.inf; +import com.arialyy.aria.core.common.AbsEntity; + /** * Created by lyy on 2019/4/5. * 普通任务配置处理 diff --git a/Aria/src/main/java/com/arialyy/aria/core/manager/DTaskWrapperFactory.java b/Aria/src/main/java/com/arialyy/aria/core/manager/DTaskWrapperFactory.java index 02bca7ff..91f72f25 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/manager/DTaskWrapperFactory.java +++ b/Aria/src/main/java/com/arialyy/aria/core/manager/DTaskWrapperFactory.java @@ -15,11 +15,11 @@ */ package com.arialyy.aria.core.manager; -import com.arialyy.aria.core.common.RecordHandler; -import com.arialyy.aria.core.common.TaskRecord; +import com.arialyy.aria.core.TaskRecord; import com.arialyy.aria.core.download.DTaskWrapper; import com.arialyy.aria.core.download.DownloadEntity; import com.arialyy.aria.core.inf.IEntity; +import com.arialyy.aria.core.inf.IRecordHandler; import java.io.File; /** @@ -75,7 +75,7 @@ class DTaskWrapperFactory implements INormalTEFactory= 1) { for (int i = 0; i < diff; i++) { TASK nextTask = getNextTask(); diff --git a/Aria/src/main/java/com/arialyy/aria/core/queue/DownloadGroupTaskQueue.java b/Aria/src/main/java/com/arialyy/aria/core/queue/DGroupTaskQueue.java similarity index 75% rename from Aria/src/main/java/com/arialyy/aria/core/queue/DownloadGroupTaskQueue.java rename to Aria/src/main/java/com/arialyy/aria/core/queue/DGroupTaskQueue.java index 9dbfbc70..ce59202b 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/queue/DownloadGroupTaskQueue.java +++ b/Aria/src/main/java/com/arialyy/aria/core/queue/DGroupTaskQueue.java @@ -18,35 +18,44 @@ package com.arialyy.aria.core.queue; import com.arialyy.aria.core.AriaManager; import com.arialyy.aria.core.download.DGTaskWrapper; -import com.arialyy.aria.core.download.DownloadGroupTask; +import com.arialyy.aria.core.task.DownloadGroupTask; +import com.arialyy.aria.core.event.DGMaxNumEvent; +import com.arialyy.aria.core.event.Event; +import com.arialyy.aria.core.event.EventMsgUtil; import com.arialyy.aria.core.scheduler.TaskSchedulers; import com.arialyy.aria.util.ALog; /** * Created by AriaL on 2017/6/29. 任务组下载队列 */ -public class DownloadGroupTaskQueue +public class DGroupTaskQueue extends AbsTaskQueue { - private static volatile DownloadGroupTaskQueue INSTANCE = null; + private static volatile DGroupTaskQueue INSTANCE = null; private final String TAG = "DownloadGroupTaskQueue"; - public static DownloadGroupTaskQueue getInstance() { + public static DGroupTaskQueue getInstance() { if (INSTANCE == null) { - synchronized (DownloadGroupTaskQueue.class) { - INSTANCE = new DownloadGroupTaskQueue(); + synchronized (DGroupTaskQueue.class) { + INSTANCE = new DGroupTaskQueue(); + EventMsgUtil.getDefault().register(INSTANCE); } } return INSTANCE; } - private DownloadGroupTaskQueue() { + private DGroupTaskQueue() { } @Override int getQueueType() { return TYPE_DG_QUEUE; } + @Event + public void maxTaskNum(DGMaxNumEvent event) { + setMaxTaskNum(event.maxNum); + } + @Override public int getMaxTaskNum() { return AriaManager.getInstance().getDGroupConfig().getMaxTaskNum(); } diff --git a/Aria/src/main/java/com/arialyy/aria/core/queue/DownloadTaskQueue.java b/Aria/src/main/java/com/arialyy/aria/core/queue/DTaskQueue.java similarity index 85% rename from Aria/src/main/java/com/arialyy/aria/core/queue/DownloadTaskQueue.java rename to Aria/src/main/java/com/arialyy/aria/core/queue/DTaskQueue.java index 8b7d7589..eb552fad 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/queue/DownloadTaskQueue.java +++ b/Aria/src/main/java/com/arialyy/aria/core/queue/DTaskQueue.java @@ -18,7 +18,10 @@ package com.arialyy.aria.core.queue; import com.arialyy.aria.core.AriaManager; import com.arialyy.aria.core.download.DTaskWrapper; -import com.arialyy.aria.core.download.DownloadTask; +import com.arialyy.aria.core.task.DownloadTask; +import com.arialyy.aria.core.event.DMaxNumEvent; +import com.arialyy.aria.core.event.Event; +import com.arialyy.aria.core.event.EventMsgUtil; import com.arialyy.aria.core.inf.TaskSchedulerType; import com.arialyy.aria.core.scheduler.TaskSchedulers; import com.arialyy.aria.util.ALog; @@ -30,26 +33,32 @@ import java.util.Set; * Created by lyy on 2016/8/17. * 下载任务队列 */ -public class DownloadTaskQueue extends AbsTaskQueue { +public class DTaskQueue extends AbsTaskQueue { private static final String TAG = "DownloadTaskQueue"; - private static volatile DownloadTaskQueue INSTANCE = null; + private static volatile DTaskQueue INSTANCE = null; - public static DownloadTaskQueue getInstance() { + public static DTaskQueue getInstance() { if (INSTANCE == null) { - synchronized (DownloadTaskQueue.class) { - INSTANCE = new DownloadTaskQueue(); + synchronized (DTaskQueue.class) { + INSTANCE = new DTaskQueue(); + EventMsgUtil.getDefault().register(INSTANCE); } } return INSTANCE; } - private DownloadTaskQueue() { + private DTaskQueue() { } @Override int getQueueType() { return TYPE_D_QUEUE; } + @Event + public void maxTaskNum(DMaxNumEvent event) { + setMaxTaskNum(event.maxNum); + } + @Override public int getOldMaxNum() { return AriaManager.getInstance().getDownloadConfig().oldMaxTaskNum; } diff --git a/Aria/src/main/java/com/arialyy/aria/core/queue/ITaskQueue.java b/Aria/src/main/java/com/arialyy/aria/core/queue/ITaskQueue.java index b669150b..e25c0813 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/queue/ITaskQueue.java +++ b/Aria/src/main/java/com/arialyy/aria/core/queue/ITaskQueue.java @@ -17,13 +17,12 @@ package com.arialyy.aria.core.queue; import com.arialyy.aria.core.download.DGTaskWrapper; -import com.arialyy.aria.core.download.DownloadGroupTask; -import com.arialyy.aria.core.download.DownloadTask; +import com.arialyy.aria.core.task.DownloadGroupTask; +import com.arialyy.aria.core.task.DownloadTask; import com.arialyy.aria.core.download.DTaskWrapper; -import com.arialyy.aria.core.inf.AbsTask; -import com.arialyy.aria.core.inf.AbsTaskWrapper; -import com.arialyy.aria.core.inf.ITask; -import com.arialyy.aria.core.upload.UploadTask; +import com.arialyy.aria.core.wrapper.AbsTaskWrapper; +import com.arialyy.aria.core.task.ITask; +import com.arialyy.aria.core.task.UploadTask; import com.arialyy.aria.core.upload.UTaskWrapper; /** 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 80a38001..eebde726 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 @@ -18,13 +18,13 @@ package com.arialyy.aria.core.queue; import com.arialyy.aria.core.download.DGTaskWrapper; import com.arialyy.aria.core.download.DTaskWrapper; -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.ISchedulers; +import com.arialyy.aria.core.task.DownloadGroupTask; +import com.arialyy.aria.core.task.DownloadTask; +import com.arialyy.aria.core.wrapper.AbsTaskWrapper; +import com.arialyy.aria.core.task.ITask; +import com.arialyy.aria.core.listener.ISchedulers; import com.arialyy.aria.core.upload.UTaskWrapper; -import com.arialyy.aria.core.upload.UploadTask; +import com.arialyy.aria.core.task.UploadTask; /** * Created by lyy on 2016/8/18. diff --git a/Aria/src/main/java/com/arialyy/aria/core/queue/UploadTaskQueue.java b/Aria/src/main/java/com/arialyy/aria/core/queue/UTaskQueue.java similarity index 74% rename from Aria/src/main/java/com/arialyy/aria/core/queue/UploadTaskQueue.java rename to Aria/src/main/java/com/arialyy/aria/core/queue/UTaskQueue.java index a6b17733..026096ec 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/queue/UploadTaskQueue.java +++ b/Aria/src/main/java/com/arialyy/aria/core/queue/UTaskQueue.java @@ -17,28 +17,37 @@ package com.arialyy.aria.core.queue; import com.arialyy.aria.core.AriaManager; +import com.arialyy.aria.core.event.Event; +import com.arialyy.aria.core.event.EventMsgUtil; +import com.arialyy.aria.core.event.UMaxNumEvent; 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.core.task.UploadTask; import com.arialyy.aria.util.ALog; /** * Created by lyy on 2017/2/27. 上传任务队列 */ -public class UploadTaskQueue extends AbsTaskQueue { +public class UTaskQueue extends AbsTaskQueue { private static final String TAG = "UploadTaskQueue"; - private static volatile UploadTaskQueue INSTANCE = null; + private static volatile UTaskQueue INSTANCE = null; - public static UploadTaskQueue getInstance() { + public static UTaskQueue getInstance() { if (INSTANCE == null) { - synchronized (UploadTaskQueue.class) { - INSTANCE = new UploadTaskQueue(); + synchronized (UTaskQueue.class) { + INSTANCE = new UTaskQueue(); + EventMsgUtil.getDefault().register(INSTANCE); } } return INSTANCE; } - private UploadTaskQueue() { + private UTaskQueue() { + } + + @Event + public void maxTaskNum(UMaxNumEvent event){ + setMaxTaskNum(event.maxNum); } @Override int getQueueType() { diff --git a/Aria/src/main/java/com/arialyy/aria/core/queue/pool/BaseCachePool.java b/Aria/src/main/java/com/arialyy/aria/core/queue/pool/BaseCachePool.java index 5a5a96de..2fd7f409 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/queue/pool/BaseCachePool.java +++ b/Aria/src/main/java/com/arialyy/aria/core/queue/pool/BaseCachePool.java @@ -17,7 +17,7 @@ package com.arialyy.aria.core.queue.pool; import android.text.TextUtils; -import com.arialyy.aria.core.inf.AbsTask; +import com.arialyy.aria.core.task.AbsTask; import com.arialyy.aria.util.ALog; import com.arialyy.aria.util.CommonUtil; import java.util.LinkedHashSet; diff --git a/Aria/src/main/java/com/arialyy/aria/core/queue/pool/BaseExecutePool.java b/Aria/src/main/java/com/arialyy/aria/core/queue/pool/BaseExecutePool.java index 03fe9e49..4ae49c99 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/queue/pool/BaseExecutePool.java +++ b/Aria/src/main/java/com/arialyy/aria/core/queue/pool/BaseExecutePool.java @@ -18,7 +18,7 @@ package com.arialyy.aria.core.queue.pool; import android.text.TextUtils; import com.arialyy.aria.core.AriaManager; -import com.arialyy.aria.core.inf.AbsTask; +import com.arialyy.aria.core.task.AbsTask; import com.arialyy.aria.util.ALog; import com.arialyy.aria.util.CommonUtil; import java.util.Map; 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 f9e64b0a..5a494822 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 @@ -16,7 +16,7 @@ package com.arialyy.aria.core.queue.pool; import com.arialyy.aria.core.AriaManager; -import com.arialyy.aria.core.inf.AbsTask; +import com.arialyy.aria.core.task.AbsTask; /** * Created by AriaL on 2017/6/29. 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 f43c2bb2..be863d4e 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 @@ -16,7 +16,7 @@ package com.arialyy.aria.core.queue.pool; import com.arialyy.aria.core.AriaManager; -import com.arialyy.aria.core.inf.AbsTask; +import com.arialyy.aria.core.task.AbsTask; import com.arialyy.aria.util.ALog; import com.arialyy.aria.util.CommonUtil; import java.util.Set; diff --git a/Aria/src/main/java/com/arialyy/aria/core/queue/pool/IPool.java b/Aria/src/main/java/com/arialyy/aria/core/queue/pool/IPool.java index 313520cb..d016c81f 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/queue/pool/IPool.java +++ b/Aria/src/main/java/com/arialyy/aria/core/queue/pool/IPool.java @@ -16,8 +16,8 @@ package com.arialyy.aria.core.queue.pool; -import com.arialyy.aria.core.inf.AbsEntity; -import com.arialyy.aria.core.inf.AbsTask; +import com.arialyy.aria.core.common.AbsEntity; +import com.arialyy.aria.core.task.AbsTask; /** * Created by lyy on 2016/8/14. 任务池 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 7988fc5b..ae31ef4b 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 @@ -16,7 +16,7 @@ package com.arialyy.aria.core.queue.pool; import com.arialyy.aria.core.AriaManager; -import com.arialyy.aria.core.inf.AbsTask; +import com.arialyy.aria.core.task.AbsTask; /** * Created by Aria.Lao on 2017/7/17. 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 index 18c352f2..3b5873e1 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/scheduler/FailureTaskHandler.java +++ b/Aria/src/main/java/com/arialyy/aria/core/scheduler/FailureTaskHandler.java @@ -16,8 +16,8 @@ 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.common.AbsEntity; +import com.arialyy.aria.core.task.ITask; import com.arialyy.aria.core.manager.TaskWrapperManager; import com.arialyy.aria.core.queue.ITaskQueue; import com.arialyy.aria.util.ALog; diff --git a/Aria/src/main/java/com/arialyy/aria/core/scheduler/NormalTaskListener.java b/Aria/src/main/java/com/arialyy/aria/core/scheduler/NormalTaskListener.java index 08b1a8bf..4757ab9a 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/scheduler/NormalTaskListener.java +++ b/Aria/src/main/java/com/arialyy/aria/core/scheduler/NormalTaskListener.java @@ -15,10 +15,10 @@ */ package com.arialyy.aria.core.scheduler; -import com.arialyy.aria.core.download.DownloadGroupTask; -import com.arialyy.aria.core.download.DownloadTask; -import com.arialyy.aria.core.inf.ITask; -import com.arialyy.aria.core.upload.UploadTask; +import com.arialyy.aria.core.task.DownloadGroupTask; +import com.arialyy.aria.core.task.DownloadTask; +import com.arialyy.aria.core.task.ITask; +import com.arialyy.aria.core.task.UploadTask; /** * Created by Aria.Lao on 2017/6/7. diff --git a/Aria/src/main/java/com/arialyy/aria/core/scheduler/SubTaskListener.java b/Aria/src/main/java/com/arialyy/aria/core/scheduler/SubTaskListener.java index d29fea62..036b1185 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/scheduler/SubTaskListener.java +++ b/Aria/src/main/java/com/arialyy/aria/core/scheduler/SubTaskListener.java @@ -15,8 +15,8 @@ */ package com.arialyy.aria.core.scheduler; -import com.arialyy.aria.core.inf.AbsNormalEntity; -import com.arialyy.aria.core.inf.ITask; +import com.arialyy.aria.core.common.AbsNormalEntity; +import com.arialyy.aria.core.task.ITask; /** * Created by Aria.Lao on 2019/6/26. diff --git a/Aria/src/main/java/com/arialyy/aria/core/scheduler/TaskSchedulers.java b/Aria/src/main/java/com/arialyy/aria/core/scheduler/TaskSchedulers.java index 33f8c7eb..771040c5 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/scheduler/TaskSchedulers.java +++ b/Aria/src/main/java/com/arialyy/aria/core/scheduler/TaskSchedulers.java @@ -20,21 +20,22 @@ import android.os.Bundle; import android.os.Message; import com.arialyy.annotations.TaskEnum; import com.arialyy.aria.core.AriaManager; -import com.arialyy.aria.core.download.DownloadGroupTask; -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.GroupSendParams; +import com.arialyy.aria.core.task.DownloadGroupTask; +import com.arialyy.aria.core.task.DownloadTask; +import com.arialyy.aria.core.common.AbsEntity; +import com.arialyy.aria.core.common.AbsNormalEntity; +import com.arialyy.aria.core.task.AbsTask; +import com.arialyy.aria.core.group.GroupSendParams; import com.arialyy.aria.core.inf.IEntity; -import com.arialyy.aria.core.inf.ITask; +import com.arialyy.aria.core.listener.ISchedulers; +import com.arialyy.aria.core.task.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.DGroupTaskQueue; +import com.arialyy.aria.core.queue.DTaskQueue; 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.core.queue.UTaskQueue; +import com.arialyy.aria.core.task.UploadTask; import com.arialyy.aria.util.ALog; import com.arialyy.aria.util.NetUtils; import java.util.Iterator; @@ -78,13 +79,13 @@ public class TaskSchedulers implements ISchedulers { ITaskQueue getQueue(TASK curTask) { int taskType = curTask.getTaskType(); if (taskType == ITask.DOWNLOAD) { - return DownloadTaskQueue.getInstance(); + return DTaskQueue.getInstance(); } if (taskType == ITask.DOWNLOAD_GROUP) { - return DownloadGroupTaskQueue.getInstance(); + return DGroupTaskQueue.getInstance(); } if (taskType == ITask.UPLOAD) { - return UploadTaskQueue.getInstance(); + return UTaskQueue.getInstance(); } throw new NullPointerException("任务类型错误,type = " + taskType); } 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 0424fa47..50995ee0 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 @@ -21,15 +21,16 @@ import androidx.annotation.NonNull; import com.arialyy.annotations.TaskEnum; import com.arialyy.aria.core.AriaManager; import com.arialyy.aria.core.command.CancelAllCmd; +import com.arialyy.aria.core.command.CmdHelper; import com.arialyy.aria.core.command.NormalCmdFactory; import com.arialyy.aria.core.common.AbsBuilderTarget; +import com.arialyy.aria.core.common.AbsEntity; 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.TaskSchedulers; +import com.arialyy.aria.core.task.ITask; import com.arialyy.aria.core.upload.target.FtpBuilderTarget; import com.arialyy.aria.core.upload.target.FtpNormalTarget; import com.arialyy.aria.core.upload.target.HttpBuilderTarget; @@ -38,7 +39,7 @@ import com.arialyy.aria.core.upload.target.UTargetFactory; import com.arialyy.aria.orm.DbEntity; import com.arialyy.aria.util.ALog; import com.arialyy.aria.util.CheckUtil; -import com.arialyy.aria.util.CommonUtil; +import com.arialyy.aria.util.ComponentUtil; import java.util.List; import java.util.Set; @@ -68,6 +69,7 @@ public class UploadReceiver extends AbsReceiver { */ @CheckResult public HttpBuilderTarget load(@NonNull String filePath) { + ComponentUtil.getInstance().checkComponentExist(ComponentUtil.COMPONENT_TYPE_HTTP); CheckUtil.checkUploadPath(filePath); return UTargetFactory.getInstance() .generateBuilderTarget(HttpBuilderTarget.class, filePath); @@ -81,6 +83,7 @@ public class UploadReceiver extends AbsReceiver { */ @CheckResult public HttpNormalTarget load(long taskId) { + ComponentUtil.getInstance().checkComponentExist(ComponentUtil.COMPONENT_TYPE_HTTP); CheckUtil.checkTaskId(taskId); return UTargetFactory.getInstance() .generateNormalTarget(HttpNormalTarget.class, taskId); @@ -93,6 +96,7 @@ public class UploadReceiver extends AbsReceiver { */ @CheckResult public FtpBuilderTarget loadFtp(@NonNull String filePath) { + ComponentUtil.getInstance().checkComponentExist(ComponentUtil.COMPONENT_TYPE_FTP); CheckUtil.checkUploadPath(filePath); return UTargetFactory.getInstance() .generateBuilderTarget(FtpBuilderTarget.class, filePath); @@ -106,6 +110,7 @@ public class UploadReceiver extends AbsReceiver { */ @CheckResult public FtpNormalTarget loadFtp(long taskId) { + ComponentUtil.getInstance().checkComponentExist(ComponentUtil.COMPONENT_TYPE_FTP); CheckUtil.checkTaskId(taskId); return UTargetFactory.getInstance() .generateNormalTarget(FtpNormalTarget.class, taskId); @@ -237,7 +242,7 @@ public class UploadReceiver extends AbsReceiver { public void removeAllTask(boolean removeFile) { final AriaManager am = AriaManager.getInstance(); CancelAllCmd cancelCmd = - (CancelAllCmd) CommonUtil.createNormalCmd(new UTaskWrapper(null), + (CancelAllCmd) CmdHelper.createNormalCmd(new UTaskWrapper(null), NormalCmdFactory.TASK_CANCEL_ALL, ITask.UPLOAD); cancelCmd.removeFile = removeFile; diff --git a/Aria/src/main/java/com/arialyy/aria/core/upload/target/FtpBuilderTarget.java b/Aria/src/main/java/com/arialyy/aria/core/upload/target/FtpBuilderTarget.java index a23b04a2..5dd3d981 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/upload/target/FtpBuilderTarget.java +++ b/Aria/src/main/java/com/arialyy/aria/core/upload/target/FtpBuilderTarget.java @@ -17,11 +17,11 @@ package com.arialyy.aria.core.upload.target; import androidx.annotation.CheckResult; import androidx.annotation.NonNull; +import com.arialyy.aria.core.processor.IFtpUploadInterceptor; 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; -import com.arialyy.aria.core.inf.AbsTaskWrapper; +import com.arialyy.aria.core.common.FtpDelegate; +import com.arialyy.aria.core.inf.Suggest; +import com.arialyy.aria.core.wrapper.AbsTaskWrapper; /** * Created by Aria.Lao on 2017/7/27. diff --git a/Aria/src/main/java/com/arialyy/aria/core/upload/target/FtpNormalTarget.java b/Aria/src/main/java/com/arialyy/aria/core/upload/target/FtpNormalTarget.java index e2db2cc4..994b3004 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/upload/target/FtpNormalTarget.java +++ b/Aria/src/main/java/com/arialyy/aria/core/upload/target/FtpNormalTarget.java @@ -17,9 +17,10 @@ package com.arialyy.aria.core.upload.target; import androidx.annotation.CheckResult; import com.arialyy.aria.core.common.AbsNormalTarget; -import com.arialyy.aria.core.common.Suggest; -import com.arialyy.aria.core.common.ftp.FtpDelegate; -import com.arialyy.aria.core.inf.AbsTaskWrapper; +import com.arialyy.aria.core.inf.Suggest; +import com.arialyy.aria.core.common.FtpDelegate; +import com.arialyy.aria.core.wrapper.AbsTaskWrapper; +import com.arialyy.aria.core.inf.IOptionConstant; import com.arialyy.aria.core.upload.UploadEntity; import com.arialyy.aria.util.CommonUtil; @@ -32,7 +33,9 @@ public class FtpNormalTarget extends AbsNormalTarget { FtpNormalTarget(long taskId) { mConfigHandler = new UNormalConfigHandler<>(this, taskId); - getTaskWrapper().asFtp().setUrlEntity(CommonUtil.getFtpUrlInfo(getEntity().getUrl())); + getTaskWrapper().getOptionParams() + .setParams(IOptionConstant.ftpUrlEntity, CommonUtil.getFtpUrlInfo(getEntity().getUrl())); + getTaskWrapper().setRequestType(AbsTaskWrapper.U_FTP); } diff --git a/Aria/src/main/java/com/arialyy/aria/core/upload/target/HttpBuilderTarget.java b/Aria/src/main/java/com/arialyy/aria/core/upload/target/HttpBuilderTarget.java index 53682c4e..89ede1dd 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/upload/target/HttpBuilderTarget.java +++ b/Aria/src/main/java/com/arialyy/aria/core/upload/target/HttpBuilderTarget.java @@ -17,9 +17,9 @@ package com.arialyy.aria.core.upload.target; import androidx.annotation.CheckResult; 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; +import com.arialyy.aria.core.inf.Suggest; +import com.arialyy.aria.core.common.HttpDelegate; +import com.arialyy.aria.core.wrapper.AbsTaskWrapper; /** * Created by lyy on 2017/2/28. diff --git a/Aria/src/main/java/com/arialyy/aria/core/upload/target/HttpNormalTarget.java b/Aria/src/main/java/com/arialyy/aria/core/upload/target/HttpNormalTarget.java index 95bc0c94..8edf5eb5 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/upload/target/HttpNormalTarget.java +++ b/Aria/src/main/java/com/arialyy/aria/core/upload/target/HttpNormalTarget.java @@ -17,9 +17,9 @@ package com.arialyy.aria.core.upload.target; import androidx.annotation.CheckResult; import com.arialyy.aria.core.common.AbsNormalTarget; -import com.arialyy.aria.core.common.Suggest; -import com.arialyy.aria.core.common.http.HttpDelegate; -import com.arialyy.aria.core.inf.AbsTaskWrapper; +import com.arialyy.aria.core.inf.Suggest; +import com.arialyy.aria.core.common.HttpDelegate; +import com.arialyy.aria.core.wrapper.AbsTaskWrapper; /** * Created by lyy on 2017/2/28. diff --git a/Aria/src/main/java/com/arialyy/aria/core/upload/target/UNormalConfigHandler.java b/Aria/src/main/java/com/arialyy/aria/core/upload/target/UNormalConfigHandler.java index 9ccf2faa..251e628c 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/upload/target/UNormalConfigHandler.java +++ b/Aria/src/main/java/com/arialyy/aria/core/upload/target/UNormalConfigHandler.java @@ -15,16 +15,17 @@ */ package com.arialyy.aria.core.upload.target; -import com.arialyy.aria.core.common.ftp.IFtpUploadInterceptor; +import com.arialyy.aria.core.processor.IFtpUploadInterceptor; +import com.arialyy.aria.core.common.AbsEntity; import com.arialyy.aria.core.event.ErrorEvent; -import com.arialyy.aria.core.inf.AbsEntity; import com.arialyy.aria.core.inf.AbsTarget; import com.arialyy.aria.core.inf.IConfigHandler; +import com.arialyy.aria.core.inf.IOptionConstant; import com.arialyy.aria.core.manager.TaskWrapperManager; -import com.arialyy.aria.core.queue.UploadTaskQueue; +import com.arialyy.aria.core.queue.UTaskQueue; import com.arialyy.aria.core.upload.UTaskWrapper; import com.arialyy.aria.core.upload.UploadEntity; -import com.arialyy.aria.core.upload.UploadTask; +import com.arialyy.aria.core.task.UploadTask; import com.arialyy.aria.orm.DbEntity; import com.arialyy.aria.util.CommonUtil; import java.io.File; @@ -65,7 +66,8 @@ class UNormalConfigHandler implements IConfigHandler { if (uploadInterceptor == null) { throw new NullPointerException("ftp拦截器为空"); } - getTaskWrapper().asFtp().setUploadInterceptor(uploadInterceptor); + getTaskWrapper().getOptionParams() + .setObjs(IOptionConstant.uploadInterceptor, uploadInterceptor); return mTarget; } @@ -78,13 +80,14 @@ class UNormalConfigHandler implements IConfigHandler { } @Override public boolean isRunning() { - UploadTask task = UploadTaskQueue.getInstance().getTask(mEntity.getKey()); + UploadTask task = UTaskQueue.getInstance().getTask(mEntity.getKey()); return task != null && task.isRunning(); } void setTempUrl(String tempUrl) { getTaskWrapper().setTempUrl(tempUrl); - getTaskWrapper().asFtp().setUrlEntity(CommonUtil.getFtpUrlInfo(tempUrl)); + getTaskWrapper().getOptionParams() + .setParams(IOptionConstant.ftpUrlEntity, CommonUtil.getFtpUrlInfo(tempUrl)); } private UTaskWrapper getTaskWrapper() { diff --git a/Aria/src/main/java/com/arialyy/aria/core/upload/uploader/SimpleUploadUtil.java b/Aria/src/main/java/com/arialyy/aria/core/upload/uploader/SimpleUploadUtil.java deleted file mode 100644 index 528da2d4..00000000 --- a/Aria/src/main/java/com/arialyy/aria/core/upload/uploader/SimpleUploadUtil.java +++ /dev/null @@ -1,116 +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.upload.uploader; - -import com.arialyy.aria.core.common.CompleteInfo; -import com.arialyy.aria.core.common.IUtil; -import com.arialyy.aria.core.common.OnFileInfoCallback; -import com.arialyy.aria.core.inf.AbsEntity; -import com.arialyy.aria.core.inf.AbsTaskWrapper; -import com.arialyy.aria.core.inf.IUploadListener; -import com.arialyy.aria.core.upload.UTaskWrapper; -import com.arialyy.aria.exception.BaseException; -import com.arialyy.aria.util.CheckUtil; - -/** - * Created by lyy on 2017/2/9. - * 简单的文件上传工具 - */ -public class SimpleUploadUtil implements IUtil, Runnable { - private static final String TAG = "SimpleUploadUtil"; - - private UTaskWrapper mTaskWrapper; - private IUploadListener mListener; - private Uploader mUploader; - private boolean isStop = false, isCancel = false; - - public SimpleUploadUtil(UTaskWrapper taskWrapper, IUploadListener listener) { - mTaskWrapper = taskWrapper; - CheckUtil.checkTaskEntity(taskWrapper); - if (listener == null) { - throw new IllegalArgumentException("上传监听不能为空"); - } - mListener = listener; - mUploader = new Uploader(mListener, taskWrapper); - } - - @Override public void run() { - mListener.onPre(); - switch (mTaskWrapper.getRequestType()) { - case AbsTaskWrapper.U_FTP: - FtpFileInfoThread infoThread = - new FtpFileInfoThread(mTaskWrapper, new OnFileInfoCallback() { - @Override public void onComplete(String url, CompleteInfo info) { - if (info.code == FtpFileInfoThread.CODE_COMPLETE) { - mListener.onComplete(); - } else { - mUploader.start(); - } - } - - @Override public void onFail(AbsEntity entity, BaseException e, boolean needRetry) { - failUpload(e, needRetry); - } - }); - new Thread(infoThread).start(); - break; - case AbsTaskWrapper.U_HTTP: - mUploader.start(); - break; - } - } - - private void failUpload(BaseException e, boolean needRetry) { - if (isStop || isCancel) { - return; - } - mListener.onFail(needRetry, e); - mUploader.onDestroy(); - } - - @Override public String getKey() { - return mTaskWrapper.getKey(); - } - - @Override public long getFileSize() { - return mUploader.getFileSize(); - } - - @Override public long getCurrentLocation() { - return mUploader.getCurrentLocation(); - } - - @Override public boolean isRunning() { - return mUploader.isRunning(); - } - - @Override public void cancel() { - isCancel = true; - mUploader.cancel(); - } - - @Override public void stop() { - isStop = true; - mUploader.stop(); - } - - @Override public void start() { - if (isStop || isCancel) { - return; - } - new Thread(this).start(); - } -} 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 deleted file mode 100644 index 32858fce..00000000 --- a/Aria/src/main/java/com/arialyy/aria/core/upload/uploader/Uploader.java +++ /dev/null @@ -1,61 +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.upload.uploader; - -import com.arialyy.aria.core.AriaManager; -import com.arialyy.aria.core.common.AbsThreadTask; -import com.arialyy.aria.core.common.NormalFileer; -import com.arialyy.aria.core.common.SubThreadConfig; -import com.arialyy.aria.core.event.Event; -import com.arialyy.aria.core.event.SpeedEvent; -import com.arialyy.aria.core.inf.AbsTaskWrapper; -import com.arialyy.aria.core.inf.IUploadListener; -import com.arialyy.aria.core.upload.UTaskWrapper; -import com.arialyy.aria.core.upload.UploadEntity; -import java.io.File; - -/** - * Created by Aria.Lao on 2017/7/27. - * 文件上传器 - */ -class Uploader extends NormalFileer { - - Uploader(IUploadListener listener, UTaskWrapper taskEntity) { - super(listener, taskEntity); - mTempFile = new File(mEntity.getFilePath()); - setUpdateInterval( - AriaManager.getInstance().getUploadConfig().getUpdateInterval()); - } - - @Override protected boolean handleNewTask() { - return true; - } - - @Override protected AbsThreadTask selectThreadTask(SubThreadConfig config) { - switch (mTaskWrapper.getRequestType()) { - case AbsTaskWrapper.U_FTP: - return new FtpThreadTask(config); - case AbsTaskWrapper.U_HTTP: - return new HttpThreadTask(config); - } - return null; - } - - @Event - public void setMaxSpeed(SpeedEvent event) { - setMaxSpeed(event.speed); - } -} diff --git a/AriaAnnotations/src/main/java/com/arialyy/annotations/TaskEnum.java b/AriaAnnotations/src/main/java/com/arialyy/annotations/TaskEnum.java index 5723fad1..29cf5163 100644 --- a/AriaAnnotations/src/main/java/com/arialyy/annotations/TaskEnum.java +++ b/AriaAnnotations/src/main/java/com/arialyy/annotations/TaskEnum.java @@ -20,15 +20,15 @@ package com.arialyy.annotations; * 任务类型枚举 */ public enum TaskEnum { - DOWNLOAD("com.arialyy.aria.core.download", "DownloadTask", "$$DownloadListenerProxy", + DOWNLOAD("com.arialyy.aria.core.task", "DownloadTask", "$$DownloadListenerProxy", "NormalTaskListener"), - DOWNLOAD_GROUP("com.arialyy.aria.core.download", "DownloadGroupTask", + DOWNLOAD_GROUP("com.arialyy.aria.core.task", "DownloadGroupTask", "$$DownloadGroupListenerProxy", "NormalTaskListener"), - DOWNLOAD_GROUP_SUB("com.arialyy.aria.core.download", "DownloadGroupTask", + DOWNLOAD_GROUP_SUB("com.arialyy.aria.core.task", "DownloadGroupTask", "$$DGSubListenerProxy", "SubTaskListener"), - UPLOAD("com.arialyy.aria.core.upload", "UploadTask", "$$UploadListenerProxy", + UPLOAD("com.arialyy.aria.core.task", "UploadTask", "$$UploadListenerProxy", "NormalTaskListener"), - M3U8_PEER("com.arialyy.aria.core.download", "DownloadTask", "$$M3U8PeerListenerProxy", + M3U8_PEER("com.arialyy.aria.core.task", "DownloadTask", "$$M3U8PeerListenerProxy", "M3U8PeerTaskListener"); public String pkg, className, proxySuffix, proxySuperClass; diff --git a/AriaCompiler/src/main/java/com/arialyy/compiler/EntityInfo.java b/AriaCompiler/src/main/java/com/arialyy/compiler/EntityInfo.java index 27f35ced..20742573 100644 --- a/AriaCompiler/src/main/java/com/arialyy/compiler/EntityInfo.java +++ b/AriaCompiler/src/main/java/com/arialyy/compiler/EntityInfo.java @@ -20,7 +20,7 @@ package com.arialyy.compiler; * 实体信息 */ enum EntityInfo { - NORMAL("com.arialyy.aria.core.inf", "AbsNormalEntity"), + NORMAL("com.arialyy.aria.core.common", "AbsNormalEntity"), DOWNLOAD("com.arialyy.aria.core.download", "DownloadEntity"), UPLOAD("com.arialyy.aria.core.upload", "UploadEntity"); String pkg, className; diff --git a/AriaFtpComponent/src/androidTest/java/com/example/ariaftpcomponent/ExampleInstrumentedTest.java b/AriaFtpComponent/src/androidTest/java/com/example/ariaftpcomponent/ExampleInstrumentedTest.java deleted file mode 100644 index 9e18a5cd..00000000 --- a/AriaFtpComponent/src/androidTest/java/com/example/ariaftpcomponent/ExampleInstrumentedTest.java +++ /dev/null @@ -1,26 +0,0 @@ -package com.example.ariaftpcomponent; - -import android.content.Context; -import androidx.test.platform.app.InstrumentationRegistry; -import androidx.test.ext.junit.runners.AndroidJUnit4; - -import org.junit.Test; -import org.junit.runner.RunWith; - -import static org.junit.Assert.*; - -/** - * Instrumented test, which will execute on an Android device. - * - * @see Testing documentation - */ -@RunWith(AndroidJUnit4.class) -public class ExampleInstrumentedTest { - @Test - public void useAppContext() { - // Context of the app under test. - Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext(); - - assertEquals("com.example.ariaftpcomponent.test", appContext.getPackageName()); - } -} diff --git a/AriaFtpComponent/src/main/java/com/arialyy/aria/ftpcomponent/bb.java b/AriaFtpComponent/src/main/java/com/arialyy/aria/ftpcomponent/bb.java deleted file mode 100644 index 84ba97e3..00000000 --- a/AriaFtpComponent/src/main/java/com/arialyy/aria/ftpcomponent/bb.java +++ /dev/null @@ -1,4 +0,0 @@ -package com.arialyy.aria.ftpcomponent; - -public class bb { -} diff --git a/AriaFtpComponent/src/test/java/com/example/ariaftpcomponent/ExampleUnitTest.java b/AriaFtpComponent/src/test/java/com/example/ariaftpcomponent/ExampleUnitTest.java deleted file mode 100644 index 79cfa215..00000000 --- a/AriaFtpComponent/src/test/java/com/example/ariaftpcomponent/ExampleUnitTest.java +++ /dev/null @@ -1,17 +0,0 @@ -package com.example.ariaftpcomponent; - -import org.junit.Test; - -import static org.junit.Assert.*; - -/** - * Example local unit test, which will execute on the development machine (host). - * - * @see Testing documentation - */ -public class ExampleUnitTest { - @Test - public void addition_isCorrect() { - assertEquals(4, 2 + 2); - } -} \ No newline at end of file diff --git a/AriaFtpComponent/.gitignore b/FtpComponent/.gitignore similarity index 100% rename from AriaFtpComponent/.gitignore rename to FtpComponent/.gitignore diff --git a/FtpComponent/build.gradle b/FtpComponent/build.gradle new file mode 100644 index 00000000..9417b467 --- /dev/null +++ b/FtpComponent/build.gradle @@ -0,0 +1,33 @@ +apply plugin: 'com.android.library' + +android { + compileSdkVersion rootProject.ext.compileSdkVersion + buildToolsVersion rootProject.ext.buildToolsVersion + + defaultConfig { + minSdkVersion rootProject.ext.minSdkVersion + targetSdkVersion rootProject.ext.targetSdkVersion + versionCode rootProject.ext.versionCode + versionName rootProject.ext.versionName + + testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" + consumerProguardFiles 'consumer-rules.pro' + } + + buildTypes { + release { + minifyEnabled false + proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' + } + } +} + +dependencies { + implementation fileTree(dir: 'libs', include: ['*.jar']) + + implementation "androidx.appcompat:appcompat:${rootProject.ext.XAppcompatVersion}" + + testImplementation 'junit:junit:4.12' + implementation project(path: ':AriaFtpPlug') + implementation project(path: ':PublicComponent') +} diff --git a/AriaFtpComponent/consumer-rules.pro b/FtpComponent/consumer-rules.pro similarity index 100% rename from AriaFtpComponent/consumer-rules.pro rename to FtpComponent/consumer-rules.pro diff --git a/AriaFtpComponent/proguard-rules.pro b/FtpComponent/proguard-rules.pro similarity index 100% rename from AriaFtpComponent/proguard-rules.pro rename to FtpComponent/proguard-rules.pro diff --git a/AriaFtpComponent/src/main/AndroidManifest.xml b/FtpComponent/src/main/AndroidManifest.xml similarity index 100% rename from AriaFtpComponent/src/main/AndroidManifest.xml rename to FtpComponent/src/main/AndroidManifest.xml diff --git a/Aria/src/main/java/com/arialyy/aria/core/common/ftp/AbsFtpInfoThread.java b/FtpComponent/src/main/java/com/arialyy/aria/ftp/AbsFtpInfoThread.java similarity index 90% rename from Aria/src/main/java/com/arialyy/aria/core/common/ftp/AbsFtpInfoThread.java rename to FtpComponent/src/main/java/com/arialyy/aria/ftp/AbsFtpInfoThread.java index a3345e2c..87f63342 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/common/ftp/AbsFtpInfoThread.java +++ b/FtpComponent/src/main/java/com/arialyy/aria/ftp/AbsFtpInfoThread.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.arialyy.aria.core.common.ftp; +package com.arialyy.aria.ftp; import android.net.TrafficStats; import android.os.Process; @@ -24,10 +24,11 @@ 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.common.OnFileInfoCallback; -import com.arialyy.aria.core.inf.AbsEntity; -import com.arialyy.aria.core.inf.AbsTaskWrapper; +import com.arialyy.aria.core.AriaConfig; +import com.arialyy.aria.core.FtpUrlEntity; +import com.arialyy.aria.core.common.AbsEntity; +import com.arialyy.aria.core.wrapper.AbsTaskWrapper; +import com.arialyy.aria.core.inf.OnFileInfoCallback; import com.arialyy.aria.core.upload.UploadEntity; import com.arialyy.aria.exception.AriaIOException; import com.arialyy.aria.exception.BaseException; @@ -55,7 +56,7 @@ public abstract class AbsFtpInfoThread> - extends AbsThreadTask { - private final String TAG = "AbsFtpThreadTask"; +public abstract class BaseFtpThreadTaskAdapter extends AbsThreadTaskAdapter { + + protected FtpTaskOption mTaskOption; protected String charSet; - protected AbsFtpThreadTask(SubThreadConfig config) { + protected BaseFtpThreadTaskAdapter(SubThreadConfig config) { super(config); + } protected void closeClient(FTPClient client) { @@ -61,7 +62,7 @@ public abstract class AbsFtpThreadTask { +public class FtpDirInfoThread extends AbsFtpInfoThread { - FtpDirInfoThread(DGTaskWrapper taskEntity, OnFileInfoCallback callback) { + public FtpDirInfoThread(DGTaskWrapper taskEntity, OnFileInfoCallback callback) { super(taskEntity, callback); } @Override protected String getRemotePath() { - return mTaskWrapper.asFtp().getUrlEntity().remotePath; + return mTaskOption.getUrlEntity().remotePath; } @Override protected void handleFile(String remotePath, FTPFile ftpFile) { @@ -59,7 +58,7 @@ class FtpDirInfoThread extends AbsFtpInfoThread(); + record.threadNum = threadNum; + + int requestType = getWrapper().getRequestType(); + if (requestType == ITaskWrapper.D_FTP || requestType == ITaskWrapper.D_FTP_DIR) { + record.isBlock = threadNum > 1 && Configuration.getInstance().downloadCfg.isUseBlock(); + // 线程数为1,或者使用了分块,则认为是使用动态长度文件 + record.isOpenDynamicFile = threadNum == 1 || record.isBlock; + } else { + record.isBlock = false; + } + record.taskType = TaskRecord.TYPE_HTTP_FTP; + record.isGroupRecord = getEntity().isGroupChild(); + if (record.isGroupRecord) { + if (getEntity() instanceof DownloadEntity) { + record.dGroupHash = ((DownloadEntity) getEntity()).getGroupHash(); + } + } + + return record; + } + + @Override public int initTaskThreadNum() { + int requestType = getWrapper().getRequestType(); + if (requestType == ITaskWrapper.D_FTP || requestType == ITaskWrapper.D_FTP_DIR) { + int threadNum = Configuration.getInstance().downloadCfg.getThreadNum(); + return getEntity().getFileSize() <= IRecordHandler.SUB_LEN + || getEntity().isGroupChild() + || threadNum == 1 + ? 1 + : threadNum; + } else { + return 1; + } + } + + +} diff --git a/Aria/src/main/java/com/arialyy/aria/core/common/ftp/FtpTaskConfig.java b/FtpComponent/src/main/java/com/arialyy/aria/ftp/FtpTaskOption.java similarity index 86% rename from Aria/src/main/java/com/arialyy/aria/core/common/ftp/FtpTaskConfig.java rename to FtpComponent/src/main/java/com/arialyy/aria/ftp/FtpTaskOption.java index c4b5a332..e9a67d3e 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/common/ftp/FtpTaskConfig.java +++ b/FtpComponent/src/main/java/com/arialyy/aria/ftp/FtpTaskOption.java @@ -13,17 +13,20 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.arialyy.aria.core.common.ftp; +package com.arialyy.aria.ftp; import aria.apache.commons.net.ftp.FTPClientConfig; -import com.arialyy.aria.core.inf.ITaskConfig; +import com.arialyy.aria.core.FtpUrlEntity; +import com.arialyy.aria.core.processor.FtpInterceptHandler; +import com.arialyy.aria.core.processor.IFtpUploadInterceptor; +import com.arialyy.aria.core.inf.ITaskOption; import java.lang.ref.SoftReference; import java.net.Proxy; /** * fTP任务设置的信息,如:用户名、密码、端口等信息 */ -public class FtpTaskConfig implements ITaskConfig { +public class FtpTaskOption implements ITaskOption { /** * 账号和密码 @@ -69,7 +72,7 @@ public class FtpTaskConfig implements ITaskConfig { } public IFtpUploadInterceptor getUploadInterceptor() { - return uploadInterceptor.get(); + return uploadInterceptor == null ? null : uploadInterceptor.get(); } public void setUploadInterceptor(IFtpUploadInterceptor uploadInterceptor) { diff --git a/Aria/src/main/java/com/arialyy/aria/core/common/ftp/SSLSessionReuseFTPSClient.java b/FtpComponent/src/main/java/com/arialyy/aria/ftp/SSLSessionReuseFTPSClient.java similarity index 97% rename from Aria/src/main/java/com/arialyy/aria/core/common/ftp/SSLSessionReuseFTPSClient.java rename to FtpComponent/src/main/java/com/arialyy/aria/ftp/SSLSessionReuseFTPSClient.java index 63fed6e3..5e915e65 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/common/ftp/SSLSessionReuseFTPSClient.java +++ b/FtpComponent/src/main/java/com/arialyy/aria/ftp/SSLSessionReuseFTPSClient.java @@ -1,4 +1,4 @@ -package com.arialyy.aria.core.common.ftp; +package com.arialyy.aria.ftp; import aria.apache.commons.net.ftp.FTPSClient; import java.io.IOException; diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/FtpFileInfoThread.java b/FtpComponent/src/main/java/com/arialyy/aria/ftp/download/FtpDFileInfoThread.java similarity index 83% rename from Aria/src/main/java/com/arialyy/aria/core/download/downloader/FtpFileInfoThread.java rename to FtpComponent/src/main/java/com/arialyy/aria/ftp/download/FtpDFileInfoThread.java index a3f81b8c..a9f35116 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/FtpFileInfoThread.java +++ b/FtpComponent/src/main/java/com/arialyy/aria/ftp/download/FtpDFileInfoThread.java @@ -13,15 +13,15 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.arialyy.aria.core.download.downloader; +package com.arialyy.aria.ftp.download; import aria.apache.commons.net.ftp.FTPFile; import com.arialyy.aria.core.common.CompleteInfo; -import com.arialyy.aria.core.common.OnFileInfoCallback; -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.core.inf.OnFileInfoCallback; import com.arialyy.aria.exception.AriaIOException; +import com.arialyy.aria.ftp.AbsFtpInfoThread; import com.arialyy.aria.util.ALog; import com.arialyy.aria.util.FileUtil; @@ -29,10 +29,10 @@ import com.arialyy.aria.util.FileUtil; * Created by Aria.Lao on 2017/7/25. * 获取ftp文件信息 */ -class FtpFileInfoThread extends AbsFtpInfoThread { +class FtpDFileInfoThread extends AbsFtpInfoThread { private final String TAG = "FtpFileInfoThread"; - FtpFileInfoThread(DTaskWrapper taskEntity, OnFileInfoCallback callback) { + FtpDFileInfoThread(DTaskWrapper taskEntity, OnFileInfoCallback callback) { super(taskEntity, callback); } @@ -46,7 +46,7 @@ class FtpFileInfoThread extends AbsFtpInfoThread { } @Override protected String getRemotePath() { - return mTaskWrapper.asFtp().getUrlEntity().remotePath; + return mTaskOption.getUrlEntity().remotePath; } @Override protected void onPreComplete(int code) { diff --git a/FtpComponent/src/main/java/com/arialyy/aria/ftp/download/FtpDLoaderAdapter.java b/FtpComponent/src/main/java/com/arialyy/aria/ftp/download/FtpDLoaderAdapter.java new file mode 100644 index 00000000..f15367f8 --- /dev/null +++ b/FtpComponent/src/main/java/com/arialyy/aria/ftp/download/FtpDLoaderAdapter.java @@ -0,0 +1,73 @@ +/* + * 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.ftp.download; + +import com.arialyy.aria.core.TaskRecord; +import com.arialyy.aria.core.task.AbsNormalLoaderAdapter; +import com.arialyy.aria.core.common.RecordHandler; +import com.arialyy.aria.core.common.SubThreadConfig; +import com.arialyy.aria.core.task.ThreadTask; +import com.arialyy.aria.core.wrapper.AbsTaskWrapper; +import com.arialyy.aria.core.inf.IRecordHandler; +import com.arialyy.aria.core.task.IThreadTask; +import com.arialyy.aria.core.wrapper.ITaskWrapper; +import com.arialyy.aria.ftp.FtpRecordAdapter; +import com.arialyy.aria.util.ALog; +import java.io.File; + +/** + * @Author lyy + * @Date 2019-09-19 + */ +final class FtpDLoaderAdapter extends AbsNormalLoaderAdapter { + + FtpDLoaderAdapter(ITaskWrapper wrapper) { + super(wrapper); + } + + @Override public boolean handleNewTask(TaskRecord record, int totalThreadNum) { + if (!record.isBlock) { + if (getTempFile().exists()) { + getTempFile().delete(); + } + //CommonUtil.createFile(mTempFile.getPath()); + } else { + for (int i = 0; i < totalThreadNum; i++) { + File blockFile = + new File(String.format(IRecordHandler.SUB_PATH, getTempFile().getPath(), i)); + if (blockFile.exists()) { + ALog.d(TAG, String.format("分块【%s】已经存在,将删除该分块", i)); + blockFile.delete(); + } + } + } + return true; + } + + @Override public IThreadTask createThreadTask(SubThreadConfig config) { + ThreadTask threadTask = new ThreadTask(config); + FtpDThreadTaskAdapter adapter = new FtpDThreadTaskAdapter(config); + threadTask.setAdapter(adapter); + return threadTask; + } + + @Override public IRecordHandler recordHandler(AbsTaskWrapper wrapper) { + FtpRecordAdapter adapter = new FtpRecordAdapter(wrapper); + RecordHandler handler = new RecordHandler(wrapper); + handler.setAdapter(adapter); + return handler; + } +} diff --git a/FtpComponent/src/main/java/com/arialyy/aria/ftp/download/FtpDLoaderUtil.java b/FtpComponent/src/main/java/com/arialyy/aria/ftp/download/FtpDLoaderUtil.java new file mode 100644 index 00000000..64b8b474 --- /dev/null +++ b/FtpComponent/src/main/java/com/arialyy/aria/ftp/download/FtpDLoaderUtil.java @@ -0,0 +1,59 @@ +/* + * 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.ftp.download; + +import com.arialyy.aria.core.common.AbsEntity; +import com.arialyy.aria.core.common.CompleteInfo; +import com.arialyy.aria.core.download.DTaskWrapper; +import com.arialyy.aria.core.inf.OnFileInfoCallback; +import com.arialyy.aria.core.listener.IDLoadListener; +import com.arialyy.aria.core.loader.AbsLoader; +import com.arialyy.aria.core.loader.AbsNormalLoaderUtil; +import com.arialyy.aria.core.loader.NormalLoader; +import com.arialyy.aria.exception.BaseException; +import com.arialyy.aria.ftp.FtpTaskOption; + +/** + * @Author lyy + * @Date 2019-09-19 + */ +public class FtpDLoaderUtil extends AbsNormalLoaderUtil { + + public FtpDLoaderUtil(DTaskWrapper wrapper, IDLoadListener downloadListener) { + super(wrapper, downloadListener); + wrapper.generateTaskOption(FtpTaskOption.class); + } + + @Override protected AbsLoader createLoader() { + NormalLoader loader = new NormalLoader(getListener(), getTaskWrapper()); + loader.setAdapter(new FtpDLoaderAdapter(getTaskWrapper())); + return loader; + } + + @Override protected Runnable createInfoThread() { + return new FtpDFileInfoThread((DTaskWrapper) getTaskWrapper(), new OnFileInfoCallback() { + @Override public void onComplete(String url, CompleteInfo info) { + ((NormalLoader) getLoader()).updateTempFile(); + getLoader().start(); + } + + @Override public void onFail(AbsEntity entity, BaseException e, boolean needRetry) { + fail(e, needRetry); + getLoader().closeTimer(); + } + }); + } +} diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/FtpThreadTask.java b/FtpComponent/src/main/java/com/arialyy/aria/ftp/download/FtpDThreadTaskAdapter.java similarity index 60% rename from Aria/src/main/java/com/arialyy/aria/core/download/downloader/FtpThreadTask.java rename to FtpComponent/src/main/java/com/arialyy/aria/ftp/download/FtpDThreadTaskAdapter.java index a67e9e10..544ece5d 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/FtpThreadTask.java +++ b/FtpComponent/src/main/java/com/arialyy/aria/ftp/download/FtpDThreadTaskAdapter.java @@ -13,17 +13,14 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.arialyy.aria.core.download.downloader; +package com.arialyy.aria.ftp.download; import aria.apache.commons.net.ftp.FTPClient; import aria.apache.commons.net.ftp.FTPReply; import com.arialyy.aria.core.common.SubThreadConfig; -import com.arialyy.aria.core.common.ftp.AbsFtpThreadTask; -import com.arialyy.aria.core.config.DownloadConfig; -import com.arialyy.aria.core.download.DTaskWrapper; -import com.arialyy.aria.core.download.DownloadEntity; import com.arialyy.aria.exception.AriaIOException; import com.arialyy.aria.exception.TaskException; +import com.arialyy.aria.ftp.BaseFtpThreadTaskAdapter; import com.arialyy.aria.util.ALog; import com.arialyy.aria.util.BufferedRandomAccessFile; import com.arialyy.aria.util.CommonUtil; @@ -36,56 +33,54 @@ import java.nio.channels.FileChannel; import java.nio.channels.ReadableByteChannel; /** - * Created by Aria.Lao on 2017/7/24. Ftp下载任务 + * @Author lyy + * @Date 2019-09-18 */ -class FtpThreadTask extends AbsFtpThreadTask { - private final String TAG = "FtpThreadTask"; +final class FtpDThreadTaskAdapter extends BaseFtpThreadTaskAdapter { - FtpThreadTask(SubThreadConfig config) { + FtpDThreadTaskAdapter(SubThreadConfig config) { super(config); } - @Override public FtpThreadTask call() throws Exception { - super.call(); - if (mRecord.isComplete) { + @Override protected void handlerThreadTask() { + if (getThreadRecord().isComplete) { handleComplete(); - return this; + return; } FTPClient client = null; InputStream is = null; try { ALog.d(TAG, - String.format("任务【%s】线程__%s__开始下载【开始位置 : %s,结束位置:%s】", getFileName(), - mRecord.threadId, mRecord.startLocation, mRecord.endLocation)); + String.format("任务【%s】线程__%s__开始下载【开始位置 : %s,结束位置:%s】", getTaskWrapper().getKey(), + getThreadRecord().threadId, getThreadRecord().startLocation, + getThreadRecord().endLocation)); client = createClient(); if (client == null) { - fail(mChildCurrentLocation, new TaskException(TAG, "ftp client 创建失败")); - return this; + fail(new TaskException(TAG, "ftp client 创建失败"), false); + return; } - if (mRecord.startLocation > 0) { - client.setRestartOffset(mRecord.startLocation); + if (getThreadRecord().startLocation > 0) { + client.setRestartOffset(getThreadRecord().startLocation); } //发送第二次指令时,还需要再做一次判断 int reply = client.getReplyCode(); if (!FTPReply.isPositivePreliminary(reply) && reply != FTPReply.COMMAND_OK) { - fail(mChildCurrentLocation, - new AriaIOException(TAG, - String.format("获取文件信息错误,错误码为:%s,msg:%s", reply, client.getReplyString()))); + fail(new AriaIOException(TAG, + String.format("获取文件信息错误,错误码为:%s,msg:%s", reply, client.getReplyString())), false); client.disconnect(); - return this; + return; } String remotePath = - CommonUtil.convertFtpChar(charSet, getTaskWrapper().asFtp().getUrlEntity().remotePath); + CommonUtil.convertFtpChar(charSet, mTaskOption.getUrlEntity().remotePath); ALog.i(TAG, String.format("remotePath【%s】", remotePath)); is = client.retrieveFileStream(remotePath); reply = client.getReplyCode(); if (!FTPReply.isPositivePreliminary(reply)) { - fail(mChildCurrentLocation, - new AriaIOException(TAG, - String.format("获取流失败,错误码为:%s,msg:%s", reply, client.getReplyString()))); + fail(new AriaIOException(TAG, + String.format("获取流失败,错误码为:%s,msg:%s", reply, client.getReplyString())), true); client.disconnect(); - return this; + return; } if (getConfig().isOpenDynamicFile) { @@ -95,11 +90,9 @@ class FtpThreadTask extends AbsFtpThreadTask { handleComplete(); } } catch (IOException e) { - fail(mChildCurrentLocation, - new AriaIOException(TAG, String.format("下载失败【%s】", getConfig().url), e)); + fail(new AriaIOException(TAG, String.format("下载失败【%s】", getConfig().url), e), true); } catch (Exception e) { - fail(mChildCurrentLocation, - new AriaIOException(TAG, String.format("下载失败【%s】", getConfig().url), e)); + fail(new AriaIOException(TAG, String.format("下载失败【%s】", getConfig().url), e), false); } finally { try { if (is != null) { @@ -109,24 +102,20 @@ class FtpThreadTask extends AbsFtpThreadTask { e.printStackTrace(); } closeClient(client); - onThreadComplete(); } - return this; } /** * 处理线程完成的情况 */ private void handleComplete() { - if (isBreak()) { + if (getThreadTask().isBreak()) { return; } - if (!checkBlock()) { + if (!getThreadTask().checkBlock()) { return; } - ALog.i(TAG, String.format("任务【%s】线程__%s__下载完毕", getFileName(), mRecord.threadId)); - writeConfig(true, mRecord.endLocation); - sendCompleteMsg(); + complete(); } /** @@ -142,15 +131,15 @@ class FtpThreadTask extends AbsFtpThreadTask { foc = fos.getChannel(); fic = Channels.newChannel(is); ByteBuffer bf = ByteBuffer.allocate(getTaskConfig().getBuffSize()); - while (isLive() && (len = fic.read(bf)) != -1) { - if (isBreak()) { + while (getThreadTask().isLive() && (len = fic.read(bf)) != -1) { + if (getThreadTask().isBreak()) { break; } if (mSpeedBandUtil != null) { mSpeedBandUtil.limitNextBytes(len); } - if (mChildCurrentLocation + len >= mRecord.endLocation) { - len = (int) (mRecord.endLocation - mChildCurrentLocation); + if (getRangeProgress() + len >= getThreadRecord().endLocation) { + len = (int) (getThreadRecord().endLocation - getRangeProgress()); bf.flip(); fos.write(bf.array(), 0, len); bf.compact(); @@ -165,8 +154,7 @@ class FtpThreadTask extends AbsFtpThreadTask { } handleComplete(); } catch (IOException e) { - fail(mChildCurrentLocation, - new AriaIOException(TAG, String.format("下载失败【%s】", getConfig().url), e)); + fail(new AriaIOException(TAG, String.format("下载失败【%s】", getConfig().url), e), true); } finally { try { if (fos != null) { @@ -192,18 +180,18 @@ class FtpThreadTask extends AbsFtpThreadTask { try { file = new BufferedRandomAccessFile(getConfig().tempFile, "rwd", getTaskConfig().getBuffSize()); - file.seek(mRecord.startLocation); + file.seek(getThreadRecord().startLocation); byte[] buffer = new byte[getTaskConfig().getBuffSize()]; int len; - while (isLive() && (len = is.read(buffer)) != -1) { - if (isBreak()) { + while (getThreadTask().isLive() && (len = is.read(buffer)) != -1) { + if (getThreadTask().isBreak()) { break; } if (mSpeedBandUtil != null) { mSpeedBandUtil.limitNextBytes(len); } - if (mChildCurrentLocation + len >= mRecord.endLocation) { - len = (int) (mRecord.endLocation - mChildCurrentLocation); + if (getRangeProgress() + len >= getThreadRecord().endLocation) { + len = (int) (getThreadRecord().endLocation - getRangeProgress()); file.write(buffer, 0, len); progress(len); break; @@ -213,8 +201,7 @@ class FtpThreadTask extends AbsFtpThreadTask { } } } catch (IOException e) { - fail(mChildCurrentLocation, - new AriaIOException(TAG, String.format("下载失败【%s】", getConfig().url), e)); + fail(new AriaIOException(TAG, String.format("下载失败【%s】", getConfig().url), e), true); } finally { try { if (file != null) { @@ -225,12 +212,4 @@ class FtpThreadTask extends AbsFtpThreadTask { } } } - - @Override public int getMaxSpeed() { - return getTaskConfig().getMaxSpeed(); - } - - @Override protected DownloadConfig getTaskConfig() { - return getTaskWrapper().getConfig(); - } } diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/group/FtpDirDownloadUtil.java b/FtpComponent/src/main/java/com/arialyy/aria/ftp/download/FtpDirDLoaderUtil.java similarity index 62% rename from Aria/src/main/java/com/arialyy/aria/core/download/group/FtpDirDownloadUtil.java rename to FtpComponent/src/main/java/com/arialyy/aria/ftp/download/FtpDirDLoaderUtil.java index 85935c0b..987ba33a 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/group/FtpDirDownloadUtil.java +++ b/FtpComponent/src/main/java/com/arialyy/aria/ftp/download/FtpDirDLoaderUtil.java @@ -13,18 +13,23 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.arialyy.aria.core.download.group; +package com.arialyy.aria.ftp.download; import android.net.Uri; import android.text.TextUtils; -import com.arialyy.aria.core.common.ftp.FtpUrlEntity; +import com.arialyy.aria.core.FtpUrlEntity; +import com.arialyy.aria.core.common.AbsEntity; import com.arialyy.aria.core.common.CompleteInfo; -import com.arialyy.aria.core.common.OnFileInfoCallback; import com.arialyy.aria.core.download.DGTaskWrapper; import com.arialyy.aria.core.download.DTaskWrapper; -import com.arialyy.aria.core.inf.AbsEntity; import com.arialyy.aria.core.inf.IEntity; +import com.arialyy.aria.core.inf.OnFileInfoCallback; import com.arialyy.aria.exception.BaseException; +import com.arialyy.aria.ftp.FtpDirInfoThread; +import com.arialyy.aria.ftp.FtpTaskOption; +import com.arialyy.aria.core.group.AbsGroupUtil; +import com.arialyy.aria.core.group.AbsSubDLoadUtil; +import com.arialyy.aria.core.listener.IDGroupListener; import java.util.concurrent.locks.Condition; import java.util.concurrent.locks.ReentrantLock; @@ -32,26 +37,27 @@ import java.util.concurrent.locks.ReentrantLock; * Created by Aria.Lao on 2017/7/27. * ftp文件夹下载工具 */ -public class FtpDirDownloadUtil extends AbsGroupUtil { - private String TAG = "FtpDirDownloadUtil"; +public class FtpDirDLoaderUtil extends AbsGroupUtil { private ReentrantLock LOCK = new ReentrantLock(); private Condition condition = LOCK.newCondition(); - public FtpDirDownloadUtil(IDGroupListener listener, DGTaskWrapper taskEntity) { - super(listener, taskEntity); + public FtpDirDLoaderUtil(IDGroupListener listener, DGTaskWrapper wrapper) { + super(listener, wrapper); + wrapper.generateTaskOption(FtpTaskOption.class); } - @Override int getTaskType() { - return FTP_DIR; + @Override + protected AbsSubDLoadUtil createSubLoader(DTaskWrapper wrapper, boolean needGetFileInfo) { + return new SubDLoaderUtil(getScheduler(), wrapper, needGetFileInfo); } - @Override protected boolean onPreStart() { - super.onPreStart(); + @Override protected boolean onStart() { + super.onStart(); - if (mGTWrapper.getEntity().getFileSize() > 1) { + if (getWrapper().getEntity().getFileSize() > 1) { startDownload(true); } else { - FtpDirInfoThread infoThread = new FtpDirInfoThread(mGTWrapper, new OnFileInfoCallback() { + FtpDirInfoThread infoThread = new FtpDirInfoThread(getWrapper(), new OnFileInfoCallback() { @Override public void onComplete(String url, CompleteInfo info) { if (info.code >= 200 && info.code < 300) { startDownload(false); @@ -86,24 +92,26 @@ public class FtpDirDownloadUtil extends AbsGroupUtil { LOCK.unlock(); } initState(); - for (DTaskWrapper wrapper : mGTWrapper.getSubTaskWrapper()) { + for (DTaskWrapper wrapper : getWrapper().getSubTaskWrapper()) { if (needCloneInfo) { cloneInfo(wrapper); } if (wrapper.getState() != IEntity.STATE_COMPLETE) { - createAndStartSubLoader(wrapper); + startSubLoader(createSubLoader(wrapper, true)); } } } private void cloneInfo(DTaskWrapper subWrapper) { - FtpUrlEntity urlEntity = mGTWrapper.asFtp().getUrlEntity().clone(); + FtpTaskOption option = (FtpTaskOption) getWrapper().getTaskOption(); + FtpUrlEntity urlEntity = option.getUrlEntity().clone(); Uri uri = Uri.parse(subWrapper.getEntity().getUrl()); String remotePath = uri.getPath(); urlEntity.remotePath = TextUtils.isEmpty(remotePath) ? "/" : remotePath; - subWrapper.asFtp().setUrlEntity(urlEntity); - subWrapper.asFtp().setCharSet(mGTWrapper.asFtp().getCharSet()); - subWrapper.asFtp().setProxy(mGTWrapper.asFtp().getProxy()); + FtpTaskOption subOption = ((FtpTaskOption) subWrapper.getTaskOption()); + subOption.setUrlEntity(urlEntity); + subOption.setCharSet(option.getCharSet()); + subOption.setProxy(option.getProxy()); } } diff --git a/FtpComponent/src/main/java/com/arialyy/aria/ftp/download/SubDLoaderUtil.java b/FtpComponent/src/main/java/com/arialyy/aria/ftp/download/SubDLoaderUtil.java new file mode 100644 index 00000000..4512e1d6 --- /dev/null +++ b/FtpComponent/src/main/java/com/arialyy/aria/ftp/download/SubDLoaderUtil.java @@ -0,0 +1,49 @@ +/* + * 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.ftp.download; + +import android.os.Handler; +import com.arialyy.aria.core.download.DTaskWrapper; +import com.arialyy.aria.core.loader.NormalLoader; +import com.arialyy.aria.ftp.FtpTaskOption; +import com.arialyy.aria.core.group.AbsSubDLoadUtil; +import com.arialyy.aria.core.group.ChildDLoadListener; + +/** + * @Author lyy + * @Date 2019-09-28 + */ +class SubDLoaderUtil extends AbsSubDLoadUtil { + /** + * @param schedulers 调度器 + * @param needGetInfo {@code true} 需要获取文件信息。{@code false} 不需要获取文件信息 + */ + SubDLoaderUtil(Handler schedulers, DTaskWrapper taskWrapper, boolean needGetInfo) { + super(schedulers, taskWrapper, needGetInfo); + taskWrapper.generateTaskOption(FtpTaskOption.class); + } + + @Override protected NormalLoader createLoader(ChildDLoadListener listener, DTaskWrapper wrapper) { + NormalLoader loader = new NormalLoader(listener, wrapper); + FtpDLoaderAdapter adapter = new FtpDLoaderAdapter(wrapper); + loader.setAdapter(adapter); + return loader; + } + + @Override public void start() { + getDownloader().start(); + } +} diff --git a/Aria/src/main/java/com/arialyy/aria/core/upload/uploader/FtpFISAdapter.java b/FtpComponent/src/main/java/com/arialyy/aria/ftp/upload/FtpFISAdapter.java similarity index 97% rename from Aria/src/main/java/com/arialyy/aria/core/upload/uploader/FtpFISAdapter.java rename to FtpComponent/src/main/java/com/arialyy/aria/ftp/upload/FtpFISAdapter.java index c85a0308..ae78eeea 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/upload/uploader/FtpFISAdapter.java +++ b/FtpComponent/src/main/java/com/arialyy/aria/ftp/upload/FtpFISAdapter.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.arialyy.aria.core.upload.uploader; +package com.arialyy.aria.ftp.upload; import androidx.annotation.NonNull; import com.arialyy.aria.util.BufferedRandomAccessFile; diff --git a/Aria/src/main/java/com/arialyy/aria/core/upload/uploader/FtpFileInfoThread.java b/FtpComponent/src/main/java/com/arialyy/aria/ftp/upload/FtpUFileInfoThread.java similarity index 86% rename from Aria/src/main/java/com/arialyy/aria/core/upload/uploader/FtpFileInfoThread.java rename to FtpComponent/src/main/java/com/arialyy/aria/ftp/upload/FtpUFileInfoThread.java index 3be4fbb1..e13a174e 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/upload/uploader/FtpFileInfoThread.java +++ b/FtpComponent/src/main/java/com/arialyy/aria/ftp/upload/FtpUFileInfoThread.java @@ -13,20 +13,20 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.arialyy.aria.core.upload.uploader; +package com.arialyy.aria.ftp.upload; import android.text.TextUtils; import aria.apache.commons.net.ftp.FTPClient; import aria.apache.commons.net.ftp.FTPFile; -import com.arialyy.aria.core.common.ftp.AbsFtpInfoThread; +import com.arialyy.aria.core.TaskRecord; +import com.arialyy.aria.core.ThreadRecord; import com.arialyy.aria.core.common.CompleteInfo; -import com.arialyy.aria.core.common.OnFileInfoCallback; -import com.arialyy.aria.core.common.TaskRecord; -import com.arialyy.aria.core.common.ThreadRecord; -import com.arialyy.aria.core.common.ftp.FtpInterceptHandler; -import com.arialyy.aria.core.common.ftp.IFtpUploadInterceptor; +import com.arialyy.aria.core.inf.OnFileInfoCallback; import com.arialyy.aria.core.upload.UTaskWrapper; import com.arialyy.aria.core.upload.UploadEntity; +import com.arialyy.aria.ftp.AbsFtpInfoThread; +import com.arialyy.aria.core.processor.FtpInterceptHandler; +import com.arialyy.aria.core.processor.IFtpUploadInterceptor; import com.arialyy.aria.util.ALog; import com.arialyy.aria.util.CommonUtil; import com.arialyy.aria.util.DbDataHelper; @@ -38,7 +38,7 @@ import java.util.List; * Created by Aria.Lao on 2017/9/26. * 单任务上传远程服务器文件信息 */ -class FtpFileInfoThread extends AbsFtpInfoThread { +class FtpUFileInfoThread extends AbsFtpInfoThread { private static final String TAG = "FtpUploadFileInfoThread"; static final int CODE_COMPLETE = 0xab1; private boolean isComplete = false; @@ -48,13 +48,13 @@ class FtpFileInfoThread extends AbsFtpInfoThread { */ private boolean useInterceptor = false; - FtpFileInfoThread(UTaskWrapper taskEntity, OnFileInfoCallback callback) { + FtpUFileInfoThread(UTaskWrapper taskEntity, OnFileInfoCallback callback) { super(taskEntity, callback); } @Override protected String getRemotePath() { return remotePath == null ? - mTaskWrapper.asFtp().getUrlEntity().remotePath + "/" + mEntity.getFileName() : remotePath; + mTaskOption.getUrlEntity().remotePath + "/" + mEntity.getFileName() : remotePath; } @Override protected boolean onInterceptor(FTPClient client, FTPFile[] ftpFiles) { @@ -63,7 +63,7 @@ class FtpFileInfoThread extends AbsFtpInfoThread { return true; } try { - IFtpUploadInterceptor interceptor = mTaskWrapper.asFtp().getUploadInterceptor(); + IFtpUploadInterceptor interceptor = mTaskOption.getUploadInterceptor(); if (interceptor != null) { useInterceptor = true; List files = new ArrayList<>(); @@ -89,10 +89,10 @@ class FtpFileInfoThread extends AbsFtpInfoThread { } else if (!TextUtils.isEmpty(interceptHandler.getNewFileName())) { ALog.i(TAG, String.format("远端已拥有同名文件,将修改remotePath,原文件名:%s,新文件名:%s", mEntity.getFileName(), interceptHandler.getNewFileName())); - remotePath = mTaskWrapper.asFtp().getUrlEntity().remotePath + remotePath = mTaskOption.getUrlEntity().remotePath + "/" + interceptHandler.getNewFileName(); - mTaskWrapper.asFtp().setNewFileName(interceptHandler.getNewFileName()); + mTaskOption.setNewFileName(interceptHandler.getNewFileName()); closeClient(client); run(); return false; diff --git a/FtpComponent/src/main/java/com/arialyy/aria/ftp/upload/FtpULoaderUtil.java b/FtpComponent/src/main/java/com/arialyy/aria/ftp/upload/FtpULoaderUtil.java new file mode 100644 index 00000000..1589bc78 --- /dev/null +++ b/FtpComponent/src/main/java/com/arialyy/aria/ftp/upload/FtpULoaderUtil.java @@ -0,0 +1,62 @@ +/* + * 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.ftp.upload; + +import com.arialyy.aria.core.common.AbsEntity; +import com.arialyy.aria.core.common.CompleteInfo; +import com.arialyy.aria.core.download.DTaskWrapper; +import com.arialyy.aria.core.inf.OnFileInfoCallback; +import com.arialyy.aria.core.listener.IUploadListener; +import com.arialyy.aria.core.loader.AbsLoader; +import com.arialyy.aria.core.loader.AbsNormalLoaderUtil; +import com.arialyy.aria.core.loader.NormalLoader; +import com.arialyy.aria.core.upload.UTaskWrapper; +import com.arialyy.aria.exception.BaseException; +import com.arialyy.aria.ftp.FtpTaskOption; + +/** + * @Author lyy + * @Date 2019-09-19 + */ +public class FtpULoaderUtil extends AbsNormalLoaderUtil { + + public FtpULoaderUtil(DTaskWrapper wrapper, IUploadListener uploadListener) { + super(wrapper, uploadListener); + wrapper.generateTaskOption(FtpTaskOption.class); + } + + @Override protected AbsLoader createLoader() { + NormalLoader loader = new NormalLoader(getListener(), getTaskWrapper()); + loader.setAdapter(new FtpULoaferAdapter(getTaskWrapper())); + return null; + } + + @Override protected Runnable createInfoThread() { + return new FtpUFileInfoThread((UTaskWrapper) getTaskWrapper(), new OnFileInfoCallback() { + @Override public void onComplete(String url, CompleteInfo info) { + if (info.code == FtpUFileInfoThread.CODE_COMPLETE) { + getListener().onComplete(); + } else { + getLoader().start(); + } + } + + @Override public void onFail(AbsEntity entity, BaseException e, boolean needRetry) { + fail(e, needRetry); + } + }); + } +} diff --git a/FtpComponent/src/main/java/com/arialyy/aria/ftp/upload/FtpULoaferAdapter.java b/FtpComponent/src/main/java/com/arialyy/aria/ftp/upload/FtpULoaferAdapter.java new file mode 100644 index 00000000..f3a44b2b --- /dev/null +++ b/FtpComponent/src/main/java/com/arialyy/aria/ftp/upload/FtpULoaferAdapter.java @@ -0,0 +1,56 @@ +/* + * 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.ftp.upload; + +import com.arialyy.aria.core.TaskRecord; +import com.arialyy.aria.core.task.AbsNormalLoaderAdapter; +import com.arialyy.aria.core.common.RecordHandler; +import com.arialyy.aria.core.common.SubThreadConfig; +import com.arialyy.aria.core.task.ThreadTask; +import com.arialyy.aria.core.wrapper.AbsTaskWrapper; +import com.arialyy.aria.core.inf.IRecordHandler; +import com.arialyy.aria.core.task.IThreadTask; +import com.arialyy.aria.core.wrapper.ITaskWrapper; +import com.arialyy.aria.ftp.FtpRecordAdapter; + +/** + * @Author lyy + * @Date 2019-09-19 + */ +class FtpULoaferAdapter extends AbsNormalLoaderAdapter { + + FtpULoaferAdapter(ITaskWrapper wrapper) { + super(wrapper); + } + + @Override public boolean handleNewTask(TaskRecord record, int totalThreadNum) { + return true; + } + + @Override public IThreadTask createThreadTask(SubThreadConfig config) { + ThreadTask threadTask = new ThreadTask(config); + FtpUThreadTaskAdapter adapter = new FtpUThreadTaskAdapter(config); + threadTask.setAdapter(adapter); + return threadTask; + } + + @Override public IRecordHandler recordHandler(AbsTaskWrapper wrapper) { + FtpRecordAdapter adapter = new FtpRecordAdapter(wrapper); + RecordHandler handler = new RecordHandler(wrapper); + handler.setAdapter(adapter); + return handler; + } +} diff --git a/Aria/src/main/java/com/arialyy/aria/core/upload/uploader/FtpThreadTask.java b/FtpComponent/src/main/java/com/arialyy/aria/ftp/upload/FtpUThreadTaskAdapter.java similarity index 63% rename from Aria/src/main/java/com/arialyy/aria/core/upload/uploader/FtpThreadTask.java rename to FtpComponent/src/main/java/com/arialyy/aria/ftp/upload/FtpUThreadTaskAdapter.java index a41e3f9a..c988d83b 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/upload/uploader/FtpThreadTask.java +++ b/FtpComponent/src/main/java/com/arialyy/aria/ftp/upload/FtpUThreadTaskAdapter.java @@ -13,19 +13,16 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.arialyy.aria.core.upload.uploader; +package com.arialyy.aria.ftp.upload; import android.text.TextUtils; import aria.apache.commons.net.ftp.FTPClient; import aria.apache.commons.net.ftp.FTPReply; import aria.apache.commons.net.ftp.OnFtpInputStreamListener; import com.arialyy.aria.core.common.SubThreadConfig; -import com.arialyy.aria.core.common.ftp.AbsFtpThreadTask; -import com.arialyy.aria.core.common.ftp.FtpTaskConfig; -import com.arialyy.aria.core.config.UploadConfig; -import com.arialyy.aria.core.upload.UTaskWrapper; import com.arialyy.aria.core.upload.UploadEntity; import com.arialyy.aria.exception.AriaIOException; +import com.arialyy.aria.ftp.BaseFtpThreadTaskAdapter; import com.arialyy.aria.util.ALog; import com.arialyy.aria.util.BufferedRandomAccessFile; import com.arialyy.aria.util.CommonUtil; @@ -35,67 +32,58 @@ import java.io.UnsupportedEncodingException; /** * Created by Aria.Lao on 2017/7/28. D_FTP 单线程上传任务,需要FTP 服务器给用户打开append和write的权限 */ -class FtpThreadTask extends AbsFtpThreadTask { +class FtpUThreadTaskAdapter extends BaseFtpThreadTaskAdapter { private final String TAG = "FtpThreadTask"; private String dir, remotePath; - FtpThreadTask(SubThreadConfig config) { + FtpUThreadTaskAdapter(SubThreadConfig config) { super(config); } - @Override public int getMaxSpeed() { - return getTaskConfig().getMaxSpeed(); - } - - @Override protected UploadConfig getTaskConfig() { - return getTaskWrapper().getConfig(); - } - - @Override public FtpThreadTask call() throws Exception { - super.call(); + @Override protected void handlerThreadTask() { FTPClient client = null; BufferedRandomAccessFile file = null; try { ALog.d(TAG, - String.format("任务【%s】线程__%s__开始上传【开始位置 : %s,结束位置:%s】", getFileName(), - mRecord.threadId, mRecord.startLocation, mRecord.endLocation)); + String.format("任务【%s】线程__%s__开始上传【开始位置 : %s,结束位置:%s】", getEntity().getKey(), + getThreadRecord().threadId, getThreadRecord().startLocation, + getThreadRecord().endLocation)); client = createClient(); if (client == null) { - return this; + return; } initPath(); client.makeDirectory(dir); client.changeWorkingDirectory(dir); - client.setRestartOffset(mRecord.startLocation); + client.setRestartOffset(getThreadRecord().startLocation); int reply = client.getReplyCode(); if (!FTPReply.isPositivePreliminary(reply) && reply != FTPReply.FILE_ACTION_OK) { - fail(mChildCurrentLocation, - new AriaIOException(TAG, - String.format("文件上传错误,错误码为:%s, msg:%s, filePath: %s", reply, - client.getReplyString(), getEntity().getFilePath()))); + fail(new AriaIOException(TAG, + String.format("文件上传错误,错误码为:%s, msg:%s, filePath: %s", reply, + client.getReplyString(), getEntity().getFilePath())), false); client.disconnect(); - return this; + return; } file = new BufferedRandomAccessFile(getConfig().tempFile, "rwd", getTaskConfig().getBuffSize()); - if (mRecord.startLocation != 0) { + if (getThreadRecord().startLocation != 0) { //file.skipBytes((int) getConfig().START_LOCATION); - file.seek(mRecord.startLocation); + file.seek(getThreadRecord().startLocation); } boolean complete = upload(client, file); - if (!complete || isBreak()) { - return this; + if (!complete || getThreadTask().isBreak()) { + return; } - ALog.i(TAG, String.format("任务【%s】线程__%s__上传完毕", getFileName(), mRecord.threadId)); - writeConfig(true, mRecord.endLocation); - sendCompleteMsg(); + ALog.i(TAG, + String.format("任务【%s】线程__%s__上传完毕", getEntity().getKey(), getThreadRecord().threadId)); + complete(); } catch (IOException e) { - fail(mChildCurrentLocation, new AriaIOException(TAG, + fail(new AriaIOException(TAG, String.format("上传失败,filePath: %s, uploadUrl: %s", getEntity().getFilePath(), - getConfig().url))); + getConfig().url)), true); } catch (Exception e) { - fail(mChildCurrentLocation, new AriaIOException(TAG, null, e)); + fail(new AriaIOException(TAG, null, e), false); } finally { try { if (file != null) { @@ -105,23 +93,24 @@ class FtpThreadTask extends AbsFtpThreadTask { e.printStackTrace(); } closeClient(client); - onThreadComplete(); } - return this; + } + + private UploadEntity getEntity() { + return (UploadEntity) getTaskWrapper().getEntity(); } private void initPath() throws UnsupportedEncodingException { - FtpTaskConfig delegate = getTaskWrapper().asFtp(); - dir = CommonUtil.convertFtpChar(charSet, delegate.getUrlEntity().remotePath); + dir = CommonUtil.convertFtpChar(charSet, mTaskOption.getUrlEntity().remotePath); String fileName = - TextUtils.isEmpty(delegate.getNewFileName()) ? CommonUtil.convertFtpChar(charSet, + TextUtils.isEmpty(mTaskOption.getNewFileName()) ? CommonUtil.convertFtpChar(charSet, getEntity().getFileName()) - : CommonUtil.convertFtpChar(charSet, delegate.getNewFileName()); + : CommonUtil.convertFtpChar(charSet, mTaskOption.getNewFileName()); remotePath = CommonUtil.convertFtpChar(charSet, - String.format("%s/%s", delegate.getUrlEntity().remotePath, fileName)); + String.format("%s/%s", mTaskOption.getUrlEntity().remotePath, fileName)); } /** @@ -140,7 +129,7 @@ class FtpThreadTask extends AbsFtpThreadTask { @Override public void onFtpInputStream(FTPClient client, long totalBytesTransferred, int bytesTransferred, long streamSize) { try { - if (isBreak() && !isStoped) { + if (getThreadTask().isBreak() && !isStoped) { isStoped = true; client.abor(); } @@ -162,8 +151,7 @@ class FtpThreadTask extends AbsFtpThreadTask { if (e.getMessage().contains("AriaIOException caught while copying")) { e.printStackTrace(); } else { - fail(mChildCurrentLocation, - new AriaIOException(TAG, msg, e)); + fail(new AriaIOException(TAG, msg, e), true); } return false; } @@ -171,10 +159,9 @@ class FtpThreadTask extends AbsFtpThreadTask { int reply = client.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { if (reply != FTPReply.TRANSFER_ABORTED) { - fail(mChildCurrentLocation, - new AriaIOException(TAG, - String.format("文件上传错误,错误码为:%s, msg:%s, filePath: %s", reply, - client.getReplyString(), getEntity().getFilePath())), false); + fail(new AriaIOException(TAG, + String.format("文件上传错误,错误码为:%s, msg:%s, filePath: %s", reply, client.getReplyString(), + getEntity().getFilePath())), false); } if (client.isConnected()) { client.disconnect(); diff --git a/AriaFtpComponent/src/main/res/values/strings.xml b/FtpComponent/src/main/res/values/strings.xml similarity index 100% rename from AriaFtpComponent/src/main/res/values/strings.xml rename to FtpComponent/src/main/res/values/strings.xml diff --git a/HttpComponent/.gitignore b/HttpComponent/.gitignore new file mode 100644 index 00000000..796b96d1 --- /dev/null +++ b/HttpComponent/.gitignore @@ -0,0 +1 @@ +/build diff --git a/HttpComponent/build.gradle b/HttpComponent/build.gradle new file mode 100644 index 00000000..290fc2ad --- /dev/null +++ b/HttpComponent/build.gradle @@ -0,0 +1,32 @@ +apply plugin: 'com.android.library' + +android { + compileSdkVersion rootProject.ext.compileSdkVersion + buildToolsVersion rootProject.ext.buildToolsVersion + + defaultConfig { + minSdkVersion rootProject.ext.minSdkVersion + targetSdkVersion rootProject.ext.targetSdkVersion + versionCode rootProject.ext.versionCode + versionName rootProject.ext.versionName + + testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" + consumerProguardFiles 'consumer-rules.pro' + } + + buildTypes { + release { + minifyEnabled false + proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' + } + } +} + +dependencies { + implementation fileTree(dir: 'libs', include: ['*.jar']) + + implementation "androidx.appcompat:appcompat:${rootProject.ext.XAppcompatVersion}" + + testImplementation 'junit:junit:4.12' + implementation project(path: ':PublicComponent') +} diff --git a/HttpComponent/consumer-rules.pro b/HttpComponent/consumer-rules.pro new file mode 100644 index 00000000..e69de29b diff --git a/HttpComponent/proguard-rules.pro b/HttpComponent/proguard-rules.pro new file mode 100644 index 00000000..f1b42451 --- /dev/null +++ b/HttpComponent/proguard-rules.pro @@ -0,0 +1,21 @@ +# Add project specific ProGuard rules here. +# You can control the set of applied configuration files using the +# proguardFiles setting in build.gradle. +# +# For more details, see +# http://developer.android.com/guide/developing/tools/proguard.html + +# If your project uses WebView with JS, uncomment the following +# and specify the fully qualified class name to the JavaScript interface +# class: +#-keepclassmembers class fqcn.of.javascript.interface.for.webview { +# public *; +#} + +# Uncomment this to preserve the line number information for +# debugging stack traces. +#-keepattributes SourceFile,LineNumberTable + +# If you keep the line number information, uncomment this to +# hide the original source file name. +#-renamesourcefileattribute SourceFile diff --git a/HttpComponent/src/main/AndroidManifest.xml b/HttpComponent/src/main/AndroidManifest.xml new file mode 100644 index 00000000..bb0d2bdf --- /dev/null +++ b/HttpComponent/src/main/AndroidManifest.xml @@ -0,0 +1,2 @@ + diff --git a/HttpComponent/src/main/java/com/arialyy/aria/http/BaseHttpThreadTaskAdapter.java b/HttpComponent/src/main/java/com/arialyy/aria/http/BaseHttpThreadTaskAdapter.java new file mode 100644 index 00000000..07e81f36 --- /dev/null +++ b/HttpComponent/src/main/java/com/arialyy/aria/http/BaseHttpThreadTaskAdapter.java @@ -0,0 +1,41 @@ +/* + * 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.http; + +import com.arialyy.aria.core.common.AbsNormalEntity; +import com.arialyy.aria.core.common.SubThreadConfig; +import com.arialyy.aria.core.task.AbsThreadTaskAdapter; + +/** + * @Author lyy + * @Date 2019-09-22 + */ +public abstract class BaseHttpThreadTaskAdapter extends AbsThreadTaskAdapter { + protected HttpTaskOption mTaskOption; + + protected BaseHttpThreadTaskAdapter(SubThreadConfig config) { + super(config); + mTaskOption = (HttpTaskOption) getTaskWrapper().getTaskOption(); + } + + protected String getFileName() { + return getEntity().getFileName(); + } + + protected AbsNormalEntity getEntity() { + return (AbsNormalEntity) getTaskWrapper().getEntity(); + } +} diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/ChunkedInputStream.java b/HttpComponent/src/main/java/com/arialyy/aria/http/ChunkedInputStream.java similarity index 98% rename from Aria/src/main/java/com/arialyy/aria/core/download/downloader/ChunkedInputStream.java rename to HttpComponent/src/main/java/com/arialyy/aria/http/ChunkedInputStream.java index 04ce9390..360cf06f 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/ChunkedInputStream.java +++ b/HttpComponent/src/main/java/com/arialyy/aria/http/ChunkedInputStream.java @@ -16,7 +16,7 @@ * limitations under the License. */ -package com.arialyy.aria.core.download.downloader; +package com.arialyy.aria.http; import com.arialyy.aria.util.ALog; import java.io.DataInputStream; diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/ConnectionHelp.java b/HttpComponent/src/main/java/com/arialyy/aria/http/ConnectionHelp.java similarity index 92% rename from Aria/src/main/java/com/arialyy/aria/core/download/downloader/ConnectionHelp.java rename to HttpComponent/src/main/java/com/arialyy/aria/http/ConnectionHelp.java index 9124c5a6..3c92381f 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/ConnectionHelp.java +++ b/HttpComponent/src/main/java/com/arialyy/aria/http/ConnectionHelp.java @@ -13,13 +13,12 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.arialyy.aria.core.download.downloader; +package com.arialyy.aria.http; import android.text.TextUtils; -import com.arialyy.aria.core.AriaManager; -import com.arialyy.aria.core.common.ProtocolType; +import com.arialyy.aria.core.AriaConfig; +import com.arialyy.aria.core.ProtocolType; import com.arialyy.aria.core.common.RequestEnum; -import com.arialyy.aria.core.common.http.HttpTaskConfig; import com.arialyy.aria.util.ALog; import com.arialyy.aria.util.CommonUtil; import com.arialyy.aria.util.SSLContextUtil; @@ -52,7 +51,7 @@ public class ConnectionHelp { * * @throws MalformedURLException */ - public static URL handleUrl(String url, HttpTaskConfig taskDelegate) + public static URL handleUrl(String url, HttpTaskOption taskDelegate) throws MalformedURLException { Map params = taskDelegate.getParams(); if (params != null && taskDelegate.getRequestEnum() == RequestEnum.GET) { @@ -99,7 +98,7 @@ public class ConnectionHelp { * * @throws IOException */ - public static HttpURLConnection handleConnection(URL url, HttpTaskConfig taskDelegate) + public static HttpURLConnection handleConnection(URL url, HttpTaskOption taskDelegate) throws IOException { HttpURLConnection conn; URLConnection urlConn; @@ -109,11 +108,11 @@ public class ConnectionHelp { urlConn = url.openConnection(); } if (urlConn instanceof HttpsURLConnection) { - AriaManager manager = AriaManager.getInstance(); + AriaConfig config = AriaConfig.getInstance(); conn = (HttpsURLConnection) urlConn; SSLContext sslContext = - SSLContextUtil.getSSLContextFromAssets(manager.getDownloadConfig().getCaName(), - manager.getDownloadConfig().getCaPath(), ProtocolType.Default); + SSLContextUtil.getSSLContextFromAssets(config.getDConfig().getCaName(), + config.getDConfig().getCaPath(), ProtocolType.Default); if (sslContext == null) { sslContext = SSLContextUtil.getDefaultSLLContext(ProtocolType.Default); } @@ -131,7 +130,7 @@ public class ConnectionHelp { * * @throws ProtocolException */ - public static HttpURLConnection setConnectParam(HttpTaskConfig delegate, HttpURLConnection conn) { + public static HttpURLConnection setConnectParam(HttpTaskOption delegate, HttpURLConnection conn) { if (delegate.getRequestEnum() == RequestEnum.POST) { conn.setDoInput(true); conn.setDoOutput(true); diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/HttpFileInfoThread.java b/HttpComponent/src/main/java/com/arialyy/aria/http/HttpFileInfoThread.java similarity index 90% rename from Aria/src/main/java/com/arialyy/aria/core/download/downloader/HttpFileInfoThread.java rename to HttpComponent/src/main/java/com/arialyy/aria/http/HttpFileInfoThread.java index 857a7268..121f5e07 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/HttpFileInfoThread.java +++ b/HttpComponent/src/main/java/com/arialyy/aria/http/HttpFileInfoThread.java @@ -13,20 +13,19 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.arialyy.aria.core.download.downloader; +package com.arialyy.aria.http; import android.net.TrafficStats; import android.net.Uri; import android.os.Process; import android.text.TextUtils; -import com.arialyy.aria.core.AriaManager; +import com.arialyy.aria.core.AriaConfig; import com.arialyy.aria.core.common.CompleteInfo; -import com.arialyy.aria.core.common.OnFileInfoCallback; import com.arialyy.aria.core.common.RequestEnum; -import com.arialyy.aria.core.common.http.HttpTaskConfig; import com.arialyy.aria.core.download.DTaskWrapper; import com.arialyy.aria.core.download.DownloadEntity; -import com.arialyy.aria.core.inf.IHttpFileLenAdapter; +import com.arialyy.aria.core.processor.IHttpFileLenAdapter; +import com.arialyy.aria.core.inf.OnFileInfoCallback; import com.arialyy.aria.exception.AriaIOException; import com.arialyy.aria.exception.BaseException; import com.arialyy.aria.exception.TaskException; @@ -61,15 +60,14 @@ public class HttpFileInfoThread implements Runnable { private DTaskWrapper mTaskWrapper; private int mConnectTimeOut; private OnFileInfoCallback onFileInfoCallback; - private HttpTaskConfig mTaskDelegate; + private HttpTaskOption taskOption; public HttpFileInfoThread(DTaskWrapper taskWrapper, OnFileInfoCallback callback) { this.mTaskWrapper = taskWrapper; mEntity = taskWrapper.getEntity(); - mConnectTimeOut = - AriaManager.getInstance().getDownloadConfig().getConnectTimeOut(); + mConnectTimeOut = AriaConfig.getInstance().getDConfig().getConnectTimeOut(); onFileInfoCallback = callback; - mTaskDelegate = taskWrapper.asHttp(); + taskOption = (HttpTaskOption) taskWrapper.getTaskOption(); } @Override public void run() { @@ -77,9 +75,9 @@ public class HttpFileInfoThread implements Runnable { TrafficStats.setThreadStatsTag(UUID.randomUUID().toString().hashCode()); HttpURLConnection conn = null; try { - URL url = ConnectionHelp.handleUrl(mEntity.getUrl(), mTaskDelegate); - conn = ConnectionHelp.handleConnection(url, mTaskDelegate); - ConnectionHelp.setConnectParam(mTaskDelegate, conn); + URL url = ConnectionHelp.handleUrl(mEntity.getUrl(), taskOption); + conn = ConnectionHelp.handleConnection(url, taskOption); + ConnectionHelp.setConnectParam(taskOption, conn); conn.setRequestProperty("Range", "bytes=" + 0 + "-"); conn.setConnectTimeout(mConnectTimeOut); conn.connect(); @@ -97,8 +95,8 @@ public class HttpFileInfoThread implements Runnable { } private void handleConnect(HttpURLConnection conn) throws IOException { - if (mTaskDelegate.getRequestEnum() == RequestEnum.POST) { - Map params = mTaskDelegate.getParams(); + if (taskOption.getRequestEnum() == RequestEnum.POST) { + Map params = taskOption.getParams(); if (params != null) { OutputStreamWriter dos = new OutputStreamWriter(conn.getOutputStream()); Set keys = params.keySet(); @@ -114,7 +112,7 @@ public class HttpFileInfoThread implements Runnable { } } - IHttpFileLenAdapter lenAdapter = mTaskWrapper.asHttp().getFileLenAdapter(); + IHttpFileLenAdapter lenAdapter = taskOption.getFileLenAdapter(); if (lenAdapter == null) { lenAdapter = new FileLenAdapter(); } else { @@ -144,7 +142,7 @@ public class HttpFileInfoThread implements Runnable { Map> headers = conn.getHeaderFields(); String disposition = conn.getHeaderField("Content-Disposition"); - if (mTaskDelegate.isUseServerFileName()) { + if (taskOption.isUseServerFileName()) { if (!TextUtils.isEmpty(disposition)) { mEntity.setDisposition(CommonUtil.encryptBASE64(disposition)); handleContentDisposition(disposition); @@ -159,7 +157,7 @@ public class HttpFileInfoThread implements Runnable { for (String cookie : cookiesHeader) { msCookieManager.getCookieStore().add(null, HttpCookie.parse(cookie).get(0)); } - mTaskDelegate.setCookieManager(msCookieManager); + taskOption.setCookieManager(msCookieManager); } mTaskWrapper.setCode(code); @@ -219,7 +217,7 @@ public class HttpFileInfoThread implements Runnable { conn.getResponseMessage(), mEntity.getUrl())), true); } if (end) { - mTaskDelegate.setChunked(isChunked); + taskOption.setChunked(isChunked); if (onFileInfoCallback != null) { CompleteInfo info = new CompleteInfo(code, mTaskWrapper); onFileInfoCallback.onComplete(mEntity.getUrl(), info); @@ -302,14 +300,14 @@ public class HttpFileInfoThread implements Runnable { failDownload(new TaskException(TAG, "下载失败,重定向url错误"), false); return; } - mTaskDelegate.setRedirectUrl(newUrl); + taskOption.setRedirectUrl(newUrl); mEntity.setRedirect(true); mEntity.setRedirectUrl(newUrl); String cookies = conn.getHeaderField("Set-Cookie"); conn.disconnect(); - URL url = ConnectionHelp.handleUrl(newUrl, mTaskDelegate); - conn = ConnectionHelp.handleConnection(url, mTaskDelegate); - ConnectionHelp.setConnectParam(mTaskDelegate, conn); + URL url = ConnectionHelp.handleUrl(newUrl, taskOption); + conn = ConnectionHelp.handleConnection(url, taskOption); + ConnectionHelp.setConnectParam(taskOption, conn); conn.setRequestProperty("Cookie", cookies); conn.setRequestProperty("Range", "bytes=" + 0 + "-"); conn.setConnectTimeout(mConnectTimeOut); diff --git a/HttpComponent/src/main/java/com/arialyy/aria/http/HttpRecordAdapter.java b/HttpComponent/src/main/java/com/arialyy/aria/http/HttpRecordAdapter.java new file mode 100644 index 00000000..1955c1be --- /dev/null +++ b/HttpComponent/src/main/java/com/arialyy/aria/http/HttpRecordAdapter.java @@ -0,0 +1,109 @@ +/* + * 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.http; + +import com.arialyy.aria.core.TaskRecord; +import com.arialyy.aria.core.ThreadRecord; +import com.arialyy.aria.core.common.AbsRecordHandlerAdapter; +import com.arialyy.aria.core.common.RecordHelper; +import com.arialyy.aria.core.config.Configuration; +import com.arialyy.aria.core.download.DownloadEntity; +import com.arialyy.aria.core.inf.IRecordHandler; +import com.arialyy.aria.core.wrapper.AbsTaskWrapper; +import com.arialyy.aria.core.wrapper.ITaskWrapper; +import com.arialyy.aria.util.RecordUtil; +import java.util.ArrayList; + +/** + * @Author lyy + * @Date 2019-09-23 + */ +public class HttpRecordAdapter extends AbsRecordHandlerAdapter { + public HttpRecordAdapter(AbsTaskWrapper wrapper) { + super(wrapper); + } + + @Override public void handlerTaskRecord(TaskRecord record) { + RecordHelper helper = new RecordHelper(getWrapper(), record); + if (record.isBlock) { + helper.handleBlockRecord(); + } else if (!getWrapper().isSupportBP()) { + helper.handleNoSupportBPRecord(); + } else { + helper.handleSingleThreadRecord(); + } + } + + @Override + public ThreadRecord createThreadRecord(TaskRecord record, int threadId, long startL, long endL) { + ThreadRecord tr; + tr = new ThreadRecord(); + tr.taskKey = record.filePath; + tr.threadId = threadId; + tr.startLocation = startL; + tr.isComplete = false; + + tr.threadType = TaskRecord.TYPE_HTTP_FTP; + //最后一个线程的结束位置即为文件的总长度 + if (threadId == (record.threadNum - 1)) { + endL = getEntity().getFileSize(); + } + tr.endLocation = endL; + tr.blockLen = RecordUtil.getBlockLen(getEntity().getFileSize(), threadId, record.threadNum); + return tr; + } + + @Override public TaskRecord createTaskRecord(int threadNum) { + TaskRecord record = new TaskRecord(); + record.fileName = getEntity().getFileName(); + record.filePath = getEntity().getFilePath(); + record.threadRecords = new ArrayList<>(); + record.threadNum = threadNum; + + int requestType = getWrapper().getRequestType(); + if (requestType == ITaskWrapper.D_FTP || requestType == ITaskWrapper.D_FTP_DIR) { + record.isBlock = threadNum > 1 && Configuration.getInstance().downloadCfg.isUseBlock(); + // 线程数为1,或者使用了分块,则认为是使用动态长度文件 + record.isOpenDynamicFile = threadNum == 1 || record.isBlock; + } else { + record.isBlock = false; + } + record.taskType = TaskRecord.TYPE_HTTP_FTP; + record.isGroupRecord = getEntity().isGroupChild(); + if (record.isGroupRecord) { + if (getEntity() instanceof DownloadEntity) { + record.dGroupHash = ((DownloadEntity) getEntity()).getGroupHash(); + } + } + + return record; + } + + @Override public int initTaskThreadNum() { + int requestTpe = getWrapper().getRequestType(); + if (requestTpe == ITaskWrapper.U_HTTP + || (requestTpe == ITaskWrapper.D_HTTP && (!getWrapper().isSupportBP()) + || ((HttpTaskOption) getWrapper().getTaskOption()).isChunked())) { + return 1; + } + int threadNum = Configuration.getInstance().downloadCfg.getThreadNum(); + return getEntity().getFileSize() <= IRecordHandler.SUB_LEN + || getEntity().isGroupChild() + || threadNum == 1 + ? 1 + : threadNum; + } +} diff --git a/Aria/src/main/java/com/arialyy/aria/core/common/http/HttpTaskConfig.java b/HttpComponent/src/main/java/com/arialyy/aria/http/HttpTaskOption.java similarity index 95% rename from Aria/src/main/java/com/arialyy/aria/core/common/http/HttpTaskConfig.java rename to HttpComponent/src/main/java/com/arialyy/aria/http/HttpTaskOption.java index 096b29d0..d013f42e 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/common/http/HttpTaskConfig.java +++ b/HttpComponent/src/main/java/com/arialyy/aria/http/HttpTaskOption.java @@ -14,11 +14,11 @@ * limitations under the License. */ -package com.arialyy.aria.core.common.http; +package com.arialyy.aria.http; import com.arialyy.aria.core.common.RequestEnum; -import com.arialyy.aria.core.inf.IHttpFileLenAdapter; -import com.arialyy.aria.core.inf.ITaskConfig; +import com.arialyy.aria.core.processor.IHttpFileLenAdapter; +import com.arialyy.aria.core.inf.ITaskOption; import java.lang.ref.SoftReference; import java.net.CookieManager; import java.net.Proxy; @@ -28,7 +28,7 @@ import java.util.Map; /** * Http任务设置的信息,如:cookie、请求参数 */ -public class HttpTaskConfig implements ITaskConfig { +public class HttpTaskOption implements ITaskOption { private CookieManager cookieManager; diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/group/DGroupUtil.java b/HttpComponent/src/main/java/com/arialyy/aria/http/download/DGroupLoaderUtil.java similarity index 67% rename from Aria/src/main/java/com/arialyy/aria/core/download/group/DGroupUtil.java rename to HttpComponent/src/main/java/com/arialyy/aria/http/download/DGroupLoaderUtil.java index 272583ef..65229f10 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/group/DGroupUtil.java +++ b/HttpComponent/src/main/java/com/arialyy/aria/http/download/DGroupLoaderUtil.java @@ -13,20 +13,22 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.arialyy.aria.core.download.group; +package com.arialyy.aria.http.download; +import com.arialyy.aria.core.common.AbsEntity; import com.arialyy.aria.core.common.CompleteInfo; -import com.arialyy.aria.core.common.IUtil; -import com.arialyy.aria.core.common.OnFileInfoCallback; -import com.arialyy.aria.core.common.http.HttpTaskConfig; import com.arialyy.aria.core.download.DGTaskWrapper; import com.arialyy.aria.core.download.DTaskWrapper; import com.arialyy.aria.core.download.DownloadEntity; -import com.arialyy.aria.core.download.downloader.HttpFileInfoThread; -import com.arialyy.aria.core.inf.AbsEntity; import com.arialyy.aria.core.inf.IEntity; +import com.arialyy.aria.core.inf.OnFileInfoCallback; import com.arialyy.aria.exception.AriaIOException; import com.arialyy.aria.exception.BaseException; +import com.arialyy.aria.core.group.AbsGroupUtil; +import com.arialyy.aria.core.group.AbsSubDLoadUtil; +import com.arialyy.aria.core.listener.IDGroupListener; +import com.arialyy.aria.http.HttpFileInfoThread; +import com.arialyy.aria.http.HttpTaskOption; import com.arialyy.aria.util.ALog; import com.arialyy.aria.util.CommonUtil; import java.util.ArrayList; @@ -38,19 +40,20 @@ import java.util.concurrent.Executors; * Created by AriaL on 2017/6/30. * 任务组下载工具 */ -public class DGroupUtil extends AbsGroupUtil implements IUtil { - private static final String TAG = "DownloadGroupUtil"; +public class DGroupLoaderUtil extends AbsGroupUtil { private final Object LOCK = new Object(); private ExecutorService mPool = null; private boolean getLenComplete = false; private List mTempWrapper = new ArrayList<>(); - public DGroupUtil(IDGroupListener listener, DGTaskWrapper taskWrapper) { + public DGroupLoaderUtil(IDGroupListener listener, DGTaskWrapper taskWrapper) { super(listener, taskWrapper); + taskWrapper.generateTaskOption(HttpTaskOption.class); } - @Override int getTaskType() { - return HTTP_GROUP; + @Override + protected AbsSubDLoadUtil createSubLoader(DTaskWrapper wrapper, boolean needGetFileInfo) { + return new SubDLoaderUtil(getScheduler(), wrapper, needGetFileInfo); } @Override public void onPreCancel() { @@ -68,14 +71,14 @@ public class DGroupUtil extends AbsGroupUtil implements IUtil { return false; } - @Override protected boolean onPreStart() { - super.onPreStart(); + @Override protected boolean onStart() { + super.onStart(); initState(); - if (mState.getCompleteNum() == mState.getSubSize()) { + if (getState().getCompleteNum() == getState().getSubSize()) { mListener.onComplete(); } else { // 处理组合任务大小未知的情况 - if (mGTWrapper.isUnknownSize() && mGTWrapper.getEntity().getFileSize() < 1) { + if (getWrapper().isUnknownSize() && getWrapper().getEntity().getFileSize() < 1) { mPool = Executors.newCachedThreadPool(); getGroupSize(); try { @@ -87,9 +90,9 @@ public class DGroupUtil extends AbsGroupUtil implements IUtil { } return getLenComplete; } else { - for (DTaskWrapper wrapper : mGTWrapper.getSubTaskWrapper()) { + for (DTaskWrapper wrapper : getWrapper().getSubTaskWrapper()) { if (wrapper.getState() != IEntity.STATE_COMPLETE) { - createAndStartSubLoader(wrapper); + startSubLoader(createSubLoader(wrapper, true)); } } } @@ -106,12 +109,12 @@ public class DGroupUtil extends AbsGroupUtil implements IUtil { int failCount; @Override public void run() { - for (DTaskWrapper dTaskWrapper : mGTWrapper.getSubTaskWrapper()) { + for (DTaskWrapper dTaskWrapper : getWrapper().getSubTaskWrapper()) { cloneHeader(dTaskWrapper); mPool.submit(new HttpFileInfoThread(dTaskWrapper, new OnFileInfoCallback() { @Override public void onComplete(String url, CompleteInfo info) { - if (!mGTWrapper.isUnknownSize()) { - createAndStartSubLoader((DTaskWrapper) info.wrapper, false); + if (!getWrapper().isUnknownSize()) { + startSubLoader(createSubLoader((DTaskWrapper) info.wrapper, false)); } else { mTempWrapper.add((DTaskWrapper) info.wrapper); } @@ -127,7 +130,7 @@ public class DGroupUtil extends AbsGroupUtil implements IUtil { mListener.onSubFail((DownloadEntity) entity, new AriaIOException(TAG, String.format("子任务获取文件长度失败,url:%s", ((DownloadEntity) entity).getUrl()))); checkGetSizeComplete(count, failCount); - mState.countFailNum(entity.getKey()); + getState().countFailNum(entity.getKey()); } })); } @@ -139,26 +142,26 @@ public class DGroupUtil extends AbsGroupUtil implements IUtil { * 检查组合任务大小是否获取完成,获取完成后取消阻塞,并设置组合任务大小 */ private void checkGetSizeComplete(int count, int failCount) { - if (failCount == mGTWrapper.getSubTaskWrapper().size()) { - mState.isRunning = false; + if (failCount == getWrapper().getSubTaskWrapper().size()) { + getState().setRunning(false); mListener.onFail(false, new AriaIOException(TAG, "获取子任务长度失败")); notifyLock(); return; } - if (count == mGTWrapper.getSubTaskWrapper().size()) { + if (count == getWrapper().getSubTaskWrapper().size()) { long size = 0; - for (DTaskWrapper wrapper : mGTWrapper.getSubTaskWrapper()) { + for (DTaskWrapper wrapper : getWrapper().getSubTaskWrapper()) { size += wrapper.getEntity().getFileSize(); } - mGTWrapper.getEntity().setConvertFileSize(CommonUtil.formatFileSize(size)); - mGTWrapper.getEntity().setFileSize(size); - mGTWrapper.getEntity().update(); + getWrapper().getEntity().setConvertFileSize(CommonUtil.formatFileSize(size)); + getWrapper().getEntity().setFileSize(size); + getWrapper().getEntity().update(); getLenComplete = true; ALog.d(TAG, String.format("获取组合任务长度完成,组合任务总长度:%s,失败的只任务数:%s", size, failCount)); // 未知大小的组合任务,延迟下载 - if (mGTWrapper.isUnknownSize()) { + if (getWrapper().isUnknownSize()) { for (DTaskWrapper wrapper : mTempWrapper) { - createAndStartSubLoader(wrapper, false); + startSubLoader(createSubLoader(wrapper, false)); } } notifyLock(); @@ -175,14 +178,14 @@ public class DGroupUtil extends AbsGroupUtil implements IUtil { * 子任务使用父包裹器的属性 */ private void cloneHeader(DTaskWrapper taskWrapper) { - HttpTaskConfig groupDelegate = mGTWrapper.asHttp(); - HttpTaskConfig subDelegate = taskWrapper.asHttp(); + HttpTaskOption groupOption = (HttpTaskOption) getWrapper().getTaskOption(); + HttpTaskOption subOption = (HttpTaskOption) taskWrapper.getTaskOption(); // 设置属性 - subDelegate.setFileLenAdapter(groupDelegate.getFileLenAdapter()); - subDelegate.setRequestEnum(groupDelegate.getRequestEnum()); - subDelegate.setHeaders(groupDelegate.getHeaders()); - subDelegate.setProxy(groupDelegate.getProxy()); - subDelegate.setParams(groupDelegate.getParams()); + subOption.setFileLenAdapter(groupOption.getFileLenAdapter()); + subOption.setRequestEnum(groupOption.getRequestEnum()); + subOption.setHeaders(groupOption.getHeaders()); + subOption.setProxy(groupOption.getProxy()); + subOption.setParams(groupOption.getParams()); } } \ No newline at end of file diff --git a/HttpComponent/src/main/java/com/arialyy/aria/http/download/HttpDLoaderAdapter.java b/HttpComponent/src/main/java/com/arialyy/aria/http/download/HttpDLoaderAdapter.java new file mode 100644 index 00000000..442b9816 --- /dev/null +++ b/HttpComponent/src/main/java/com/arialyy/aria/http/download/HttpDLoaderAdapter.java @@ -0,0 +1,100 @@ +/* + * 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.http.download; + +import com.arialyy.aria.core.TaskRecord; +import com.arialyy.aria.core.common.RecordHandler; +import com.arialyy.aria.core.common.SubThreadConfig; +import com.arialyy.aria.core.download.DownloadEntity; +import com.arialyy.aria.core.inf.IRecordHandler; +import com.arialyy.aria.core.task.AbsNormalLoaderAdapter; +import com.arialyy.aria.core.task.IThreadTask; +import com.arialyy.aria.core.task.ThreadTask; +import com.arialyy.aria.core.wrapper.AbsTaskWrapper; +import com.arialyy.aria.core.wrapper.ITaskWrapper; +import com.arialyy.aria.http.HttpRecordAdapter; +import com.arialyy.aria.util.ALog; +import com.arialyy.aria.util.BufferedRandomAccessFile; +import java.io.File; +import java.io.IOException; + +/** + * @Author lyy + * @Date 2019-09-21 + */ +final class HttpDLoaderAdapter extends AbsNormalLoaderAdapter { + HttpDLoaderAdapter(ITaskWrapper wrapper) { + super(wrapper); + } + + @Override public boolean handleNewTask(TaskRecord record, int totalThreadNum) { + if (!record.isBlock) { + if (getTempFile().exists()) { + getTempFile().delete(); + } + //CommonUtil.createFile(mTempFile.getPath()); + } else { + for (int i = 0; i < totalThreadNum; i++) { + File blockFile = + new File(String.format(IRecordHandler.SUB_PATH, getTempFile().getPath(), i)); + if (blockFile.exists()) { + ALog.d(TAG, String.format("分块【%s】已经存在,将删除该分块", i)); + blockFile.delete(); + } + } + } + BufferedRandomAccessFile file = null; + try { + if (totalThreadNum > 1 && !record.isBlock) { + file = new BufferedRandomAccessFile(new File(getTempFile().getPath()), "rwd", 8192); + //设置文件长度 + file.setLength(getEntity().getFileSize()); + } + return true; + } catch (IOException e) { + e.printStackTrace(); + ALog.e(TAG, String.format("下载失败,filePath: %s, url: %s", getEntity().getFilePath(), + getEntity().getUrl())); + } finally { + if (file != null) { + try { + file.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + return false; + } + + @Override public IThreadTask createThreadTask(SubThreadConfig config) { + ThreadTask task = new ThreadTask(config); + HttpDThreadTaskAdapter adapter = new HttpDThreadTaskAdapter(config); + task.setAdapter(adapter); + return task; + } + + @Override public IRecordHandler recordHandler(AbsTaskWrapper wrapper) { + RecordHandler recordHandler = new RecordHandler(wrapper); + HttpRecordAdapter adapter = new HttpRecordAdapter(wrapper); + recordHandler.setAdapter(adapter); + return recordHandler; + } + + private DownloadEntity getEntity() { + return (DownloadEntity) getWrapper().getEntity(); + } +} diff --git a/HttpComponent/src/main/java/com/arialyy/aria/http/download/HttpDLoaderUtil.java b/HttpComponent/src/main/java/com/arialyy/aria/http/download/HttpDLoaderUtil.java new file mode 100644 index 00000000..d66025e1 --- /dev/null +++ b/HttpComponent/src/main/java/com/arialyy/aria/http/download/HttpDLoaderUtil.java @@ -0,0 +1,61 @@ +/* + * 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.http.download; + +import com.arialyy.aria.core.common.AbsEntity; +import com.arialyy.aria.core.common.CompleteInfo; +import com.arialyy.aria.core.download.DTaskWrapper; +import com.arialyy.aria.core.inf.OnFileInfoCallback; +import com.arialyy.aria.core.listener.IEventListener; +import com.arialyy.aria.core.loader.AbsLoader; +import com.arialyy.aria.core.loader.AbsNormalLoaderUtil; +import com.arialyy.aria.core.loader.NormalLoader; +import com.arialyy.aria.core.wrapper.AbsTaskWrapper; +import com.arialyy.aria.exception.BaseException; +import com.arialyy.aria.http.HttpFileInfoThread; +import com.arialyy.aria.http.HttpTaskOption; + +/** + * @Author lyy + * @Date 2019-09-21 + */ +public class HttpDLoaderUtil extends AbsNormalLoaderUtil { + public HttpDLoaderUtil(AbsTaskWrapper wrapper, IEventListener listener) { + super(wrapper, listener); + wrapper.generateTaskOption(HttpTaskOption.class); + } + + @Override protected AbsLoader createLoader() { + NormalLoader loader = new NormalLoader(getListener(), getTaskWrapper()); + HttpDLoaderAdapter adapter = new HttpDLoaderAdapter(getTaskWrapper()); + loader.setAdapter(adapter); + return loader; + } + + @Override protected Runnable createInfoThread() { + return new HttpFileInfoThread((DTaskWrapper) getTaskWrapper(), new OnFileInfoCallback() { + @Override public void onComplete(String url, CompleteInfo info) { + ((NormalLoader) getLoader()).updateTempFile(); + getLoader().start(); + } + + @Override public void onFail(AbsEntity entity, BaseException e, boolean needRetry) { + fail(e, needRetry); + getLoader().closeTimer(); + } + }); + } +} diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/HttpThreadTask.java b/HttpComponent/src/main/java/com/arialyy/aria/http/download/HttpDThreadTaskAdapter.java similarity index 70% rename from Aria/src/main/java/com/arialyy/aria/core/download/downloader/HttpThreadTask.java rename to HttpComponent/src/main/java/com/arialyy/aria/http/download/HttpDThreadTaskAdapter.java index d396ba46..69b63cb2 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/HttpThreadTask.java +++ b/HttpComponent/src/main/java/com/arialyy/aria/http/download/HttpDThreadTaskAdapter.java @@ -13,17 +13,15 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.arialyy.aria.core.download.downloader; +package com.arialyy.aria.http.download; -import com.arialyy.aria.core.common.AbsThreadTask; -import com.arialyy.aria.core.common.RequestEnum; import com.arialyy.aria.core.common.SubThreadConfig; -import com.arialyy.aria.core.config.DownloadConfig; import com.arialyy.aria.core.download.DTaskWrapper; -import com.arialyy.aria.core.download.DownloadEntity; -import com.arialyy.aria.core.common.http.HttpTaskConfig; import com.arialyy.aria.exception.AriaIOException; import com.arialyy.aria.exception.TaskException; +import com.arialyy.aria.http.BaseHttpThreadTaskAdapter; +import com.arialyy.aria.http.ConnectionHelp; +import com.arialyy.aria.core.common.RequestEnum; import com.arialyy.aria.util.ALog; import com.arialyy.aria.util.BufferedRandomAccessFile; import java.io.BufferedInputStream; @@ -45,46 +43,48 @@ import java.util.Set; /** * Created by lyy on 2017/1/18. 下载线程 */ -final class HttpThreadTask extends AbsThreadTask { +final class HttpDThreadTaskAdapter extends BaseHttpThreadTaskAdapter { private final String TAG = "HttpThreadTask"; + private DTaskWrapper mTaskWrapper; - HttpThreadTask(SubThreadConfig config) { + HttpDThreadTaskAdapter(SubThreadConfig config) { super(config); } - @Override public HttpThreadTask call() throws Exception { - super.call(); - if (mRecord.isComplete) { + @Override protected void handlerThreadTask() { + mTaskWrapper = (DTaskWrapper) getTaskWrapper(); + if (getThreadRecord().isComplete) { handleComplete(); - return this; + return; } HttpURLConnection conn = null; BufferedInputStream is = null; BufferedRandomAccessFile file = null; try { - HttpTaskConfig taskDelegate = getTaskWrapper().asHttp(); - URL url = ConnectionHelp.handleUrl(getConfig().url, taskDelegate); - conn = ConnectionHelp.handleConnection(url, taskDelegate); + URL url = ConnectionHelp.handleUrl(getConfig().url, mTaskOption); + conn = ConnectionHelp.handleConnection(url, mTaskOption); if (mTaskWrapper.isSupportBP()) { ALog.d(TAG, String.format("任务【%s】线程__%s__开始下载【开始位置 : %s,结束位置:%s】", getFileName(), - mRecord.threadId, mRecord.startLocation, mRecord.endLocation)); - conn.setRequestProperty("Range", String.format("bytes=%s-%s", mRecord.startLocation, - (mRecord.endLocation - 1))); + getThreadRecord().threadId, getThreadRecord().startLocation, + getThreadRecord().endLocation)); + conn.setRequestProperty("Range", + String.format("bytes=%s-%s", getThreadRecord().startLocation, + (getThreadRecord().endLocation - 1))); } else { ALog.w(TAG, "该下载不支持断点"); } - ConnectionHelp.setConnectParam(taskDelegate, conn); + ConnectionHelp.setConnectParam(mTaskOption, conn); conn.setConnectTimeout(getTaskConfig().getConnectTimeOut()); conn.setReadTimeout(getTaskConfig().getIOTimeOut()); //设置读取流的等待时间,必须设置该参数 - if (taskDelegate.isChunked()) { + if (mTaskOption.isChunked()) { conn.setDoInput(true); conn.setChunkedStreamingMode(0); } conn.connect(); // 传递参数 - if (taskDelegate.getRequestEnum() == RequestEnum.POST) { - Map params = taskDelegate.getParams(); + if (mTaskOption.getRequestEnum() == RequestEnum.POST) { + Map params = mTaskOption.getParams(); if (params != null) { OutputStreamWriter dos = new OutputStreamWriter(conn.getOutputStream()); Set keys = params.keySet(); @@ -101,7 +101,7 @@ final class HttpThreadTask extends AbsThreadTask { } is = new BufferedInputStream(ConnectionHelp.convertInputStream(conn)); - if (taskDelegate.isChunked()) { + if (mTaskOption.isChunked()) { readChunked(is); } else if (getConfig().isOpenDynamicFile) { readDynamicFile(is); @@ -111,22 +111,22 @@ final class HttpThreadTask extends AbsThreadTask { new BufferedRandomAccessFile(getConfig().tempFile, "rwd", getTaskConfig().getBuffSize()); //设置每条线程写入文件的位置 - file.seek(mRecord.startLocation); + file.seek(getThreadRecord().startLocation); readNormal(is, file); handleComplete(); } } catch (MalformedURLException e) { - fail(mChildCurrentLocation, new TaskException(TAG, + fail(new TaskException(TAG, String.format("任务【%s】下载失败,filePath: %s, url: %s", getFileName(), - getEntity().getDownloadPath(), getEntity().getUrl()), e)); + getEntity().getFilePath(), getEntity().getUrl()), e), false); } catch (IOException e) { - fail(mChildCurrentLocation, new TaskException(TAG, + fail(new TaskException(TAG, String.format("任务【%s】下载失败,filePath: %s, url: %s", getFileName(), - getEntity().getDownloadPath(), getEntity().getUrl()), e)); + getEntity().getFilePath(), getEntity().getUrl()), e), true); } catch (Exception e) { - fail(mChildCurrentLocation, new TaskException(TAG, + fail(new TaskException(TAG, String.format("任务【%s】下载失败,filePath: %s, url: %s", getFileName(), - getEntity().getDownloadPath(), getEntity().getUrl()), e)); + getEntity().getFilePath(), getEntity().getUrl()), e), false); } finally { try { if (file != null) { @@ -141,10 +141,7 @@ final class HttpThreadTask extends AbsThreadTask { } catch (IOException e) { e.printStackTrace(); } - - onThreadComplete(); } - return this; } /** @@ -156,8 +153,8 @@ final class HttpThreadTask extends AbsThreadTask { fos = new FileOutputStream(getConfig().tempFile, true); byte[] buffer = new byte[getTaskConfig().getBuffSize()]; int len; - while (isLive() && (len = is.read(buffer)) != -1) { - if (isBreak()) { + while (getThreadTask().isLive() && (len = is.read(buffer)) != -1) { + if (getThreadTask().isBreak()) { break; } if (mSpeedBandUtil != null) { @@ -168,10 +165,9 @@ final class HttpThreadTask extends AbsThreadTask { } handleComplete(); } catch (IOException e) { - fail(mChildCurrentLocation, new AriaIOException(TAG, - String.format("文件下载失败,savePath: %s, url: %s", getEntity().getDownloadPath(), - getConfig().url), - e)); + fail(new AriaIOException(TAG, + String.format("文件下载失败,savePath: %s, url: %s", getEntity().getFilePath(), + getConfig().url)), true); } finally { if (fos != null) { try { @@ -198,15 +194,15 @@ final class HttpThreadTask extends AbsThreadTask { ByteBuffer bf = ByteBuffer.allocate(getTaskConfig().getBuffSize()); //如果要通过 Future 的 cancel 方法取消正在运行的任务,那么该任务必定是可以 对线程中断做出响应 的任务。 - while (isLive() && (len = fic.read(bf)) != -1) { - if (isBreak()) { + while (getThreadTask().isLive() && (len = fic.read(bf)) != -1) { + if (getThreadTask().isBreak()) { break; } if (mSpeedBandUtil != null) { mSpeedBandUtil.limitNextBytes(len); } - if (mChildCurrentLocation + len >= mRecord.endLocation) { - len = (int) (mRecord.endLocation - mChildCurrentLocation); + if (getRangeProgress() + len >= getThreadRecord().endLocation) { + len = (int) (getThreadRecord().endLocation - getRangeProgress()); bf.flip(); fos.write(bf.array(), 0, len); bf.compact(); @@ -221,10 +217,9 @@ final class HttpThreadTask extends AbsThreadTask { } handleComplete(); } catch (IOException e) { - fail(mChildCurrentLocation, new AriaIOException(TAG, - String.format("文件下载失败,savePath: %s, url: %s", getEntity().getDownloadPath(), - getConfig().url), - e)); + fail(new AriaIOException(TAG, + String.format("文件下载失败,savePath: %s, url: %s", getEntity().getFilePath(), + getConfig().url), e), true); } finally { try { if (fos != null) { @@ -250,8 +245,8 @@ final class HttpThreadTask extends AbsThreadTask { throws IOException { byte[] buffer = new byte[getTaskConfig().getBuffSize()]; int len; - while (isLive() && (len = is.read(buffer)) != -1) { - if (isBreak()) { + while (getThreadTask().isLive() && (len = is.read(buffer)) != -1) { + if (getThreadTask().isBreak()) { break; } if (mSpeedBandUtil != null) { @@ -266,31 +261,13 @@ final class HttpThreadTask extends AbsThreadTask { * 处理完成配置文件的更新或事件回调 */ private void handleComplete() { - if (isBreak()) { - return; - } - if (!checkBlock()) { + if (getThreadTask().isBreak()) { return; } - if (getTaskWrapper().asHttp().isChunked()) { - sendCompleteMsg(); + if (!getThreadTask().checkBlock()) { return; } - //支持断点的处理 - if (mTaskWrapper.isSupportBP()) { - writeConfig(true, mRecord.endLocation); - sendCompleteMsg(); - } else { - sendCompleteMsg(); - } - } - - @Override public int getMaxSpeed() { - return mAridManager.getDownloadConfig().getMaxSpeed(); - } - - @Override protected DownloadConfig getTaskConfig() { - return getTaskWrapper().getConfig(); + complete(); } } diff --git a/HttpComponent/src/main/java/com/arialyy/aria/http/download/SubDLoaderUtil.java b/HttpComponent/src/main/java/com/arialyy/aria/http/download/SubDLoaderUtil.java new file mode 100644 index 00000000..a32b0699 --- /dev/null +++ b/HttpComponent/src/main/java/com/arialyy/aria/http/download/SubDLoaderUtil.java @@ -0,0 +1,69 @@ +/* + * 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.http.download; + +import android.os.Handler; +import com.arialyy.aria.core.common.AbsEntity; +import com.arialyy.aria.core.common.CompleteInfo; +import com.arialyy.aria.core.download.DTaskWrapper; +import com.arialyy.aria.core.inf.OnFileInfoCallback; +import com.arialyy.aria.core.listener.ISchedulers; +import com.arialyy.aria.core.loader.NormalLoader; +import com.arialyy.aria.exception.BaseException; +import com.arialyy.aria.core.group.AbsSubDLoadUtil; +import com.arialyy.aria.core.group.ChildDLoadListener; +import com.arialyy.aria.http.HttpFileInfoThread; +import com.arialyy.aria.http.HttpTaskOption; + +/** + * @Author lyy + * @Date 2019-09-28 + */ +class SubDLoaderUtil extends AbsSubDLoadUtil { + /** + * @param schedulers 调度器 + * @param needGetInfo {@code true} 需要获取文件信息。{@code false} 不需要获取文件信息 + */ + SubDLoaderUtil(Handler schedulers, DTaskWrapper taskWrapper, boolean needGetInfo) { + super(schedulers, taskWrapper, needGetInfo); + taskWrapper.generateTaskOption(HttpTaskOption.class); + } + + @Override protected NormalLoader createLoader(ChildDLoadListener listener, DTaskWrapper wrapper) { + NormalLoader loader = new NormalLoader(listener, wrapper); + HttpDLoaderAdapter adapter = new HttpDLoaderAdapter(wrapper); + loader.setAdapter(adapter); + return loader; + } + + @Override public void start() { + if (isNeedGetInfo()) { + new Thread(new HttpFileInfoThread(getWrapper(), new OnFileInfoCallback() { + + @Override public void onComplete(String url, CompleteInfo info) { + getDownloader().start(); + } + + @Override public void onFail(AbsEntity entity, BaseException e, boolean needRetry) { + getSchedulers().obtainMessage(ISchedulers.FAIL, SubDLoaderUtil.this).sendToTarget(); + } + })).start(); + } else { + getDownloader().start(); + } + } +} + diff --git a/HttpComponent/src/main/java/com/arialyy/aria/http/upload/HttpULoaderAdapter.java b/HttpComponent/src/main/java/com/arialyy/aria/http/upload/HttpULoaderAdapter.java new file mode 100644 index 00000000..e8899840 --- /dev/null +++ b/HttpComponent/src/main/java/com/arialyy/aria/http/upload/HttpULoaderAdapter.java @@ -0,0 +1,55 @@ +/* + * 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.http.upload; + +import com.arialyy.aria.core.TaskRecord; +import com.arialyy.aria.core.common.RecordHandler; +import com.arialyy.aria.core.common.SubThreadConfig; +import com.arialyy.aria.core.inf.IRecordHandler; +import com.arialyy.aria.core.task.AbsNormalLoaderAdapter; +import com.arialyy.aria.core.task.IThreadTask; +import com.arialyy.aria.core.task.ThreadTask; +import com.arialyy.aria.core.wrapper.AbsTaskWrapper; +import com.arialyy.aria.core.wrapper.ITaskWrapper; +import com.arialyy.aria.http.HttpRecordAdapter; + +/** + * @Author lyy + * @Date 2019-09-19 + */ +final class HttpULoaderAdapter extends AbsNormalLoaderAdapter { + public HttpULoaderAdapter(ITaskWrapper wrapper) { + super(wrapper); + } + + @Override public boolean handleNewTask(TaskRecord record, int totalThreadNum) { + return true; + } + + @Override public IThreadTask createThreadTask(SubThreadConfig config) { + ThreadTask task = new ThreadTask(config); + HttpUThreadTaskAdapter adapter = new HttpUThreadTaskAdapter(config); + task.setAdapter(adapter); + return task; + } + + @Override public IRecordHandler recordHandler(AbsTaskWrapper wrapper) { + RecordHandler recordHandler = new RecordHandler(wrapper); + HttpRecordAdapter adapter = new HttpRecordAdapter(wrapper); + recordHandler.setAdapter(adapter); + return recordHandler; + } +} diff --git a/HttpComponent/src/main/java/com/arialyy/aria/http/upload/HttpULoaderUtil.java b/HttpComponent/src/main/java/com/arialyy/aria/http/upload/HttpULoaderUtil.java new file mode 100644 index 00000000..be15e5ac --- /dev/null +++ b/HttpComponent/src/main/java/com/arialyy/aria/http/upload/HttpULoaderUtil.java @@ -0,0 +1,49 @@ +/* + * 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.http.upload; + +import com.arialyy.aria.core.listener.IEventListener; +import com.arialyy.aria.core.loader.AbsLoader; +import com.arialyy.aria.core.loader.AbsNormalLoaderUtil; +import com.arialyy.aria.core.loader.NormalLoader; +import com.arialyy.aria.core.wrapper.AbsTaskWrapper; +import com.arialyy.aria.http.HttpTaskOption; + +/** + * @Author lyy + * @Date 2019-09-19 + */ +public class HttpULoaderUtil extends AbsNormalLoaderUtil { + protected HttpULoaderUtil(AbsTaskWrapper wrapper, IEventListener listener) { + super(wrapper, listener); + wrapper.generateTaskOption(HttpTaskOption.class); + } + + @Override protected AbsLoader createLoader() { + NormalLoader loader = new NormalLoader(getListener(), getTaskWrapper()); + HttpULoaderAdapter adapter = new HttpULoaderAdapter(getTaskWrapper()); + loader.setAdapter(adapter); + return loader; + } + + @Override protected Runnable createInfoThread() { + return new Runnable() { + @Override public void run() { + getLoader().start(); + } + }; + } +} diff --git a/Aria/src/main/java/com/arialyy/aria/core/upload/uploader/HttpThreadTask.java b/HttpComponent/src/main/java/com/arialyy/aria/http/upload/HttpUThreadTaskAdapter.java similarity index 75% rename from Aria/src/main/java/com/arialyy/aria/core/upload/uploader/HttpThreadTask.java rename to HttpComponent/src/main/java/com/arialyy/aria/http/upload/HttpUThreadTaskAdapter.java index 009c3283..67e3b21d 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/upload/uploader/HttpThreadTask.java +++ b/HttpComponent/src/main/java/com/arialyy/aria/http/upload/HttpUThreadTaskAdapter.java @@ -13,17 +13,14 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.arialyy.aria.core.upload.uploader; +package com.arialyy.aria.http.upload; import android.text.TextUtils; -import com.arialyy.aria.core.common.AbsThreadTask; import com.arialyy.aria.core.common.SubThreadConfig; -import com.arialyy.aria.core.common.http.HttpTaskConfig; -import com.arialyy.aria.core.config.UploadConfig; -import com.arialyy.aria.core.upload.UTaskWrapper; import com.arialyy.aria.core.upload.UploadEntity; import com.arialyy.aria.exception.BaseException; import com.arialyy.aria.exception.TaskException; +import com.arialyy.aria.http.BaseHttpThreadTaskAdapter; import com.arialyy.aria.util.ALog; import com.arialyy.aria.util.CommonUtil; import java.io.BufferedReader; @@ -44,34 +41,31 @@ import java.util.UUID; /** * Created by Aria.Lao on 2017/7/28. 不支持断点的HTTP上传任务 */ -class HttpThreadTask extends AbsThreadTask { - private final String TAG = "HttpThreadTask"; +final class HttpUThreadTaskAdapter extends BaseHttpThreadTaskAdapter { private final String BOUNDARY = UUID.randomUUID().toString(); // 边界标识 随机生成 private final String PREFIX = "--", LINE_END = "\r\n"; private HttpURLConnection mHttpConn; private OutputStream mOutputStream; - HttpThreadTask(SubThreadConfig config) { + HttpUThreadTaskAdapter(SubThreadConfig config) { super(config); } - @Override public HttpThreadTask call() throws Exception { - super.call(); + @Override protected void handlerThreadTask() { File uploadFile = new File(getEntity().getFilePath()); if (!uploadFile.exists()) { fail(new TaskException(TAG, String.format("上传失败,文件不存在;filePath: %s, url: %s", getEntity().getFilePath(), getEntity().getUrl()))); - return this; + return; } URL url; try { - HttpTaskConfig taskDelegate = getTaskWrapper().asHttp(); url = new URL(CommonUtil.convertUrl(getConfig().url)); mHttpConn = (HttpURLConnection) url.openConnection(); - mHttpConn.setRequestMethod(taskDelegate.getRequestEnum().name); + mHttpConn.setRequestMethod(mTaskOption.getRequestEnum().name); mHttpConn.setUseCaches(false); mHttpConn.setDoOutput(true); mHttpConn.setDoInput(true); @@ -86,15 +80,15 @@ class HttpThreadTask extends AbsThreadTask { mHttpConn.setChunkedStreamingMode(getTaskConfig().getBuffSize()); //添加Http请求头部 - Set keys = taskDelegate.getHeaders().keySet(); + Set keys = mTaskOption.getHeaders().keySet(); for (String key : keys) { - mHttpConn.setRequestProperty(key, taskDelegate.getHeaders().get(key)); + mHttpConn.setRequestProperty(key, mTaskOption.getHeaders().get(key)); } mOutputStream = mHttpConn.getOutputStream(); PrintWriter writer = - new PrintWriter(new OutputStreamWriter(mOutputStream, taskDelegate.getCharSet()), true); + new PrintWriter(new OutputStreamWriter(mOutputStream, mTaskOption.getCharSet()), true); // 添加参数 - Map params = taskDelegate.getParams(); + Map params = mTaskOption.getParams(); if (params != null && !params.isEmpty()) { for (String key : params.keySet()) { addFormField(writer, key, params.get(key)); @@ -102,28 +96,24 @@ class HttpThreadTask extends AbsThreadTask { } //添加文件上传表单字段 - keys = taskDelegate.getFormFields().keySet(); + keys = mTaskOption.getFormFields().keySet(); for (String key : keys) { - addFormField(writer, key, taskDelegate.getFormFields().get(key)); + addFormField(writer, key, mTaskOption.getFormFields().get(key)); } - uploadFile(writer, taskDelegate.getAttachment(), uploadFile); - getEntity().setResponseStr(finish(writer)); - sendCompleteMsg(); + uploadFile(writer, mTaskOption.getAttachment(), uploadFile); + complete(); } catch (Exception e) { e.printStackTrace(); fail(new TaskException(TAG, String.format("上传失败,filePath: %s, url: %s", getEntity().getFilePath(), getEntity().getUrl()), e)); - } finally { - onThreadComplete(); } - return this; } private void fail(BaseException e1) { try { - sendFailMsg(e1); + fail(e1, false); if (mOutputStream != null) { mOutputStream.close(); } @@ -133,20 +123,16 @@ class HttpThreadTask extends AbsThreadTask { } private String getContentType() { - HttpTaskConfig config = getTaskWrapper().asHttp(); - return - (config.getHeaders() == null || TextUtils.isEmpty(config.getHeaders().get("Content-Type"))) - ? - "multipart/form-data" : config.getHeaders().get("Content-Type"); + return (mTaskOption.getHeaders() == null || TextUtils.isEmpty( + mTaskOption.getHeaders().get("Content-Type"))) ? "multipart/form-data" + : mTaskOption.getHeaders().get("Content-Type"); } private String getUserAgent() { - HttpTaskConfig config = getTaskWrapper().asHttp(); - return - (config.getHeaders() == null || TextUtils.isEmpty(config.getHeaders().get("User-Agent"))) - ? - "Mozilla/5.0 (Windows; U; Windows NT 6.1; zh-CN; rv:1.9.2.6)" - : config.getHeaders().get("User-Agent"); + return (mTaskOption.getHeaders() == null || TextUtils.isEmpty( + mTaskOption.getHeaders().get("User-Agent"))) + ? "Mozilla/5.0 (Windows; U; Windows NT 6.1; zh-CN; rv:1.9.2.6)" + : mTaskOption.getHeaders().get("User-Agent"); } /** @@ -159,7 +145,7 @@ class HttpThreadTask extends AbsThreadTask { .append("\"") .append(LINE_END); writer.append("Content-Type: text/plain; charset=") - .append(getTaskWrapper().asHttp().getCharSet()) + .append(mTaskOption.getCharSet()) .append(LINE_END); writer.append(LINE_END); writer.append(value).append(LINE_END); @@ -194,7 +180,7 @@ class HttpThreadTask extends AbsThreadTask { while ((bytesRead = inputStream.read(buffer)) != -1) { progress(bytesRead); mOutputStream.write(buffer, 0, bytesRead); - if (isCancel) { + if (getThreadTask().isBreak()) { break; } if (mSpeedBandUtil != null) { @@ -231,7 +217,7 @@ class HttpThreadTask extends AbsThreadTask { if (status == HttpURLConnection.HTTP_OK) { BufferedReader reader = new BufferedReader(new InputStreamReader(mHttpConn.getInputStream())); String line; - while (isLive() && (line = reader.readLine()) != null) { + while (getThreadTask().isLive() && (line = reader.readLine()) != null) { response.append(line); } reader.close(); @@ -246,11 +232,7 @@ class HttpThreadTask extends AbsThreadTask { return response.toString(); } - @Override public int getMaxSpeed() { - return getTaskConfig().getMaxSpeed(); - } - - @Override protected UploadConfig getTaskConfig() { - return getTaskWrapper().getConfig(); + @Override protected UploadEntity getEntity() { + return (UploadEntity) super.getEntity(); } } diff --git a/HttpComponent/src/main/res/values/strings.xml b/HttpComponent/src/main/res/values/strings.xml new file mode 100644 index 00000000..c76105c3 --- /dev/null +++ b/HttpComponent/src/main/res/values/strings.xml @@ -0,0 +1,3 @@ + + HttpComponent + diff --git a/M3U8Component/.gitignore b/M3U8Component/.gitignore new file mode 100644 index 00000000..796b96d1 --- /dev/null +++ b/M3U8Component/.gitignore @@ -0,0 +1 @@ +/build diff --git a/M3U8Component/build.gradle b/M3U8Component/build.gradle new file mode 100644 index 00000000..6df0cb47 --- /dev/null +++ b/M3U8Component/build.gradle @@ -0,0 +1,31 @@ +apply plugin: 'com.android.library' + +android { + compileSdkVersion rootProject.ext.compileSdkVersion + buildToolsVersion rootProject.ext.buildToolsVersion + + defaultConfig { + minSdkVersion rootProject.ext.minSdkVersion + targetSdkVersion rootProject.ext.targetSdkVersion + versionCode rootProject.ext.versionCode + versionName rootProject.ext.versionName + + testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" + consumerProguardFiles 'consumer-rules.pro' + } + + buildTypes { + release { + minifyEnabled false + proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' + } + } +} + +dependencies { + implementation fileTree(dir: 'libs', include: ['*.jar']) + + implementation "androidx.appcompat:appcompat:${rootProject.ext.XAppcompatVersion}" + implementation project(path: ':HttpComponent') + implementation project(path: ':PublicComponent') +} diff --git a/M3U8Component/consumer-rules.pro b/M3U8Component/consumer-rules.pro new file mode 100644 index 00000000..e69de29b diff --git a/M3U8Component/proguard-rules.pro b/M3U8Component/proguard-rules.pro new file mode 100644 index 00000000..f1b42451 --- /dev/null +++ b/M3U8Component/proguard-rules.pro @@ -0,0 +1,21 @@ +# Add project specific ProGuard rules here. +# You can control the set of applied configuration files using the +# proguardFiles setting in build.gradle. +# +# For more details, see +# http://developer.android.com/guide/developing/tools/proguard.html + +# If your project uses WebView with JS, uncomment the following +# and specify the fully qualified class name to the JavaScript interface +# class: +#-keepclassmembers class fqcn.of.javascript.interface.for.webview { +# public *; +#} + +# Uncomment this to preserve the line number information for +# debugging stack traces. +#-keepattributes SourceFile,LineNumberTable + +# If you keep the line number information, uncomment this to +# hide the original source file name. +#-renamesourcefileattribute SourceFile diff --git a/M3U8Component/src/main/AndroidManifest.xml b/M3U8Component/src/main/AndroidManifest.xml new file mode 100644 index 00000000..d1b50eee --- /dev/null +++ b/M3U8Component/src/main/AndroidManifest.xml @@ -0,0 +1,2 @@ + diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/m3u8/BaseM3U8Loader.java b/M3U8Component/src/main/java/com/arialyy/aria/m3u8/BaseM3U8Loader.java similarity index 77% rename from Aria/src/main/java/com/arialyy/aria/core/download/m3u8/BaseM3U8Loader.java rename to M3U8Component/src/main/java/com/arialyy/aria/m3u8/BaseM3U8Loader.java index e4312db9..c6b17fbe 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/m3u8/BaseM3U8Loader.java +++ b/M3U8Component/src/main/java/com/arialyy/aria/m3u8/BaseM3U8Loader.java @@ -13,12 +13,15 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.arialyy.aria.core.download.m3u8; +package com.arialyy.aria.m3u8; -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.core.download.M3U8Entity; +import com.arialyy.aria.core.inf.IRecordHandler; +import com.arialyy.aria.core.listener.IEventListener; +import com.arialyy.aria.core.loader.AbsLoader; +import com.arialyy.aria.core.wrapper.AbsTaskWrapper; import com.arialyy.aria.util.ALog; import com.arialyy.aria.util.FileUtil; import java.io.BufferedReader; @@ -30,10 +33,12 @@ import java.io.IOException; import java.io.InputStreamReader; import java.nio.charset.Charset; -public abstract class BaseM3U8Loader extends AbsFileer { +public abstract class BaseM3U8Loader extends AbsLoader { + protected M3U8TaskOption mM3U8Option; - BaseM3U8Loader(IEventListener listener, DTaskWrapper wrapper) { + public BaseM3U8Loader(IEventListener listener, DTaskWrapper wrapper) { super(listener, wrapper); + mM3U8Option = (M3U8TaskOption) wrapper.getM3u8Option(); mTempFile = new File(wrapper.getEntity().getFilePath()); } @@ -51,8 +56,8 @@ public abstract class BaseM3U8Loader extends AbsFileer mInfos = new ArrayList<>(); - interface OnGetLivePeerCallback { + public interface OnGetLivePeerCallback { void onGetPeer(String url); } - M3U8InfoThread(DTaskWrapper taskWrapper, OnFileInfoCallback callback) { + public M3U8InfoThread(DTaskWrapper taskWrapper, OnFileInfoCallback callback) { this.mTaskWrapper = taskWrapper; mEntity = taskWrapper.getEntity(); - mConnectTimeOut = - AriaManager.getInstance().getDownloadConfig().getConnectTimeOut(); + mConnectTimeOut = AriaConfig.getInstance().getDConfig().getConnectTimeOut(); onFileInfoCallback = callback; - mTaskDelegate = taskWrapper.asHttp(); + mHttpOption = (HttpTaskOption) taskWrapper.getTaskOption(); + mM3U8Option = (M3U8TaskOption) taskWrapper.getM3u8Option(); mEntity.getM3U8Entity().setLive(mTaskWrapper.getRequestType() == AbsTaskWrapper.M3U8_LIVE); } @@ -93,9 +96,9 @@ final class M3U8InfoThread implements Runnable { TrafficStats.setThreadStatsTag(UUID.randomUUID().toString().hashCode()); HttpURLConnection conn = null; try { - URL url = ConnectionHelp.handleUrl(mEntity.getUrl(), mTaskDelegate); - conn = ConnectionHelp.handleConnection(url, mTaskDelegate); - ConnectionHelp.setConnectParam(mTaskDelegate, conn); + URL url = ConnectionHelp.handleUrl(mEntity.getUrl(), mHttpOption); + conn = ConnectionHelp.handleConnection(url, mHttpOption); + ConnectionHelp.setConnectParam(mHttpOption, conn); conn.setConnectTimeout(mConnectTimeOut); conn.connect(); handleConnect(conn); @@ -145,7 +148,7 @@ final class M3U8InfoThread implements Runnable { extInf.add(info); } } else if (line.startsWith("#EXT-X-STREAM-INF")) { - int setBand = mTaskWrapper.asM3U8().getBandWidth(); + int setBand = mM3U8Option.getBandWidth(); int bandWidth = getBandWidth(line); // 多码率的m3u8配置文件,清空信息 if (isGenerateIndexFile && mInfos != null) { @@ -252,8 +255,9 @@ final class M3U8InfoThread implements Runnable { m3U8Entity.method = param.split("=")[1]; } else if (param.startsWith("URI")) { m3U8Entity.keyUrl = param.split("=")[1].replaceAll("\"", ""); - m3U8Entity.keyPath = new File(mEntity.getFilePath()).getParent() + "/" + CommonUtil.getStrMd5( - m3U8Entity.keyUrl) + ".key"; + m3U8Entity.keyPath = + new File(mEntity.getFilePath()).getParent() + "/" + CommonUtil.getStrMd5( + m3U8Entity.keyUrl) + ".key"; } else if (param.startsWith("IV")) { m3U8Entity.iv = param.split("=")[1]; } @@ -294,14 +298,14 @@ final class M3U8InfoThread implements Runnable { failDownload("下载失败,重定向url错误", false); return; } - mTaskDelegate.setRedirectUrl(newUrl); + mHttpOption.setRedirectUrl(newUrl); mEntity.setRedirect(true); mEntity.setRedirectUrl(newUrl); String cookies = conn.getHeaderField("Set-Cookie"); conn.disconnect(); // 关闭上一个连接 - URL url = ConnectionHelp.handleUrl(newUrl, mTaskDelegate); - conn = ConnectionHelp.handleConnection(url, mTaskDelegate); - ConnectionHelp.setConnectParam(mTaskDelegate, conn); + URL url = ConnectionHelp.handleUrl(newUrl, mHttpOption); + conn = ConnectionHelp.handleConnection(url, mHttpOption); + ConnectionHelp.setConnectParam(mHttpOption, conn); conn.setRequestProperty("Cookie", cookies); conn.setConnectTimeout(mConnectTimeOut); conn.connect(); @@ -313,7 +317,7 @@ final class M3U8InfoThread implements Runnable { * 处理码率 */ private void handleBandWidth(HttpURLConnection conn, String bandWidthM3u8Url) throws IOException { - IBandWidthUrlConverter converter = mTaskWrapper.asM3U8().getBandWidthUrlConverter(); + IBandWidthUrlConverter converter = mM3U8Option.getBandWidthUrlConverter(); if (converter != null) { bandWidthM3u8Url = converter.convert(bandWidthM3u8Url); if (!bandWidthM3u8Url.startsWith("http")) { @@ -323,13 +327,13 @@ final class M3U8InfoThread implements Runnable { } else { ALog.d(TAG, "没有设置码率转换器"); } - mTaskWrapper.asM3U8().setBandWidthUrl(bandWidthM3u8Url); + mM3U8Option.setBandWidthUrl(bandWidthM3u8Url); ALog.d(TAG, String.format("新码率url:%s", bandWidthM3u8Url)); String cookies = conn.getHeaderField("Set-Cookie"); conn.disconnect(); // 关闭上一个连接 - URL url = ConnectionHelp.handleUrl(bandWidthM3u8Url, mTaskDelegate); - conn = ConnectionHelp.handleConnection(url, mTaskDelegate); - ConnectionHelp.setConnectParam(mTaskDelegate, conn); + URL url = ConnectionHelp.handleUrl(bandWidthM3u8Url, mHttpOption); + conn = ConnectionHelp.handleConnection(url, mHttpOption); + ConnectionHelp.setConnectParam(mHttpOption, conn); conn.setRequestProperty("Cookie", cookies); conn.setConnectTimeout(mConnectTimeOut); conn.connect(); @@ -355,9 +359,9 @@ final class M3U8InfoThread implements Runnable { } else { return; } - URL url = ConnectionHelp.handleUrl(info.keyUrl, mTaskDelegate); - conn = ConnectionHelp.handleConnection(url, mTaskDelegate); - ConnectionHelp.setConnectParam(mTaskDelegate, conn); + URL url = ConnectionHelp.handleUrl(info.keyUrl, mHttpOption); + conn = ConnectionHelp.handleConnection(url, mHttpOption); + ConnectionHelp.setConnectParam(mHttpOption, conn); conn.setConnectTimeout(mConnectTimeOut); conn.connect(); InputStream is = conn.getInputStream(); diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/M3U8Listener.java b/M3U8Component/src/main/java/com/arialyy/aria/m3u8/M3U8Listener.java similarity index 82% rename from Aria/src/main/java/com/arialyy/aria/core/download/M3U8Listener.java rename to M3U8Component/src/main/java/com/arialyy/aria/m3u8/M3U8Listener.java index 6c46eae1..c2b97fcb 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/M3U8Listener.java +++ b/M3U8Component/src/main/java/com/arialyy/aria/m3u8/M3U8Listener.java @@ -13,22 +13,26 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.arialyy.aria.core.download; +package com.arialyy.aria.m3u8; import android.os.Bundle; import android.os.Handler; import android.os.Message; -import com.arialyy.aria.core.inf.IDownloadListener; +import com.arialyy.aria.core.download.DTaskWrapper; import com.arialyy.aria.core.inf.IEntity; -import com.arialyy.aria.core.scheduler.ISchedulers; +import com.arialyy.aria.core.listener.BaseDListener; +import com.arialyy.aria.core.listener.IDLoadListener; +import com.arialyy.aria.core.listener.ISchedulers; +import com.arialyy.aria.core.task.AbsTask; +import com.arialyy.aria.core.task.DownloadTask; import com.arialyy.aria.util.CommonUtil; /** * 下载监听类 */ -public class M3U8Listener extends BaseDListener implements IDownloadListener { +public class M3U8Listener extends BaseDListener implements IDLoadListener { - M3U8Listener(DownloadTask task, Handler outHandler) { + M3U8Listener(AbsTask task, Handler outHandler) { super(task, outHandler); } diff --git a/M3U8Component/src/main/java/com/arialyy/aria/m3u8/M3U8RecordAdapter.java b/M3U8Component/src/main/java/com/arialyy/aria/m3u8/M3U8RecordAdapter.java new file mode 100644 index 00000000..06193d55 --- /dev/null +++ b/M3U8Component/src/main/java/com/arialyy/aria/m3u8/M3U8RecordAdapter.java @@ -0,0 +1,129 @@ +/* + * 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.m3u8; + +import com.arialyy.aria.core.TaskRecord; +import com.arialyy.aria.core.ThreadRecord; +import com.arialyy.aria.core.common.AbsRecordHandlerAdapter; +import com.arialyy.aria.core.download.DTaskWrapper; +import com.arialyy.aria.core.download.DownloadEntity; +import com.arialyy.aria.core.download.M3U8Entity; +import com.arialyy.aria.core.wrapper.ITaskWrapper; +import com.arialyy.aria.util.ALog; +import java.io.File; +import java.util.ArrayList; + +/** + * @Author lyy + * @Date 2019-09-24 + */ +public class M3U8RecordAdapter extends AbsRecordHandlerAdapter { + private M3U8TaskOption mOption; + + public M3U8RecordAdapter(DTaskWrapper wrapper) { + super(wrapper); + mOption = (M3U8TaskOption) wrapper.getM3u8Option(); + } + + /** + * 不处理live的记录 + */ + @Override public void handlerTaskRecord(TaskRecord mTaskRecord) { + String cacheDir = mOption.getCacheDir(); + long currentProgress = 0; + int completeNum = 0; + + M3U8Entity m3U8Entity = ((DownloadEntity) getEntity()).getM3U8Entity(); + // 重新下载所有切片 + boolean reDownload = + (m3U8Entity.getPeerNum() <= 0 || (m3U8Entity.isGenerateIndexFile() && !new File( + String.format(M3U8InfoThread.M3U8_INDEX_FORMAT, getEntity().getFilePath())).exists())); + + for (ThreadRecord record : mTaskRecord.threadRecords) { + File temp = new File(BaseM3U8Loader.getTsFilePath(cacheDir, record.threadId)); + if (!record.isComplete || reDownload) { + if (temp.exists()) { + temp.delete(); + } + record.startLocation = 0; + //ALog.d(TAG, String.format("分片【%s】未完成,将重新下载该分片", record.threadId)); + } else { + if (!temp.exists()) { + record.startLocation = 0; + record.isComplete = false; + ALog.w(TAG, String.format("分片【%s】不存在,将重新下载该分片", record.threadId)); + } else { + completeNum++; + currentProgress += temp.length(); + } + } + } + mOption.setCompleteNum(completeNum); + getEntity().setCurrentProgress(currentProgress); + mTaskRecord.bandWidth = mOption.getBandWidth(); + } + + /** + * 不处理live的记录 + * + * @param record 任务记录 + * @param threadId 线程id + * @param startL 线程开始位置 + * @param endL 线程结束位置 + */ + @Override + public ThreadRecord createThreadRecord(TaskRecord record, int threadId, long startL, long endL) { + ThreadRecord tr; + tr = new ThreadRecord(); + tr.taskKey = record.filePath; + tr.threadId = threadId; + tr.isComplete = false; + tr.startLocation = 0; + tr.threadType = TaskRecord.TYPE_M3U8_VOD; + tr.tsUrl = mOption.getUrls().get(threadId); + return tr; + } + + @Override public TaskRecord createTaskRecord(int threadNum) { + TaskRecord record = new TaskRecord(); + record.fileName = getEntity().getFileName(); + record.filePath = getEntity().getFilePath(); + record.threadRecords = new ArrayList<>(); + record.threadNum = threadNum; + + int requestType = getWrapper().getRequestType(); + if (requestType == ITaskWrapper.M3U8_VOD) { + record.taskType = TaskRecord.TYPE_M3U8_VOD; + record.isOpenDynamicFile = true; + record.bandWidth = mOption.getBandWidth(); + } else if (requestType == ITaskWrapper.M3U8_LIVE) { + record.taskType = TaskRecord.TYPE_M3U8_LIVE; + record.isOpenDynamicFile = true; + record.bandWidth = mOption.getBandWidth(); + } + return record; + } + + @Override public int initTaskThreadNum() { + if (getWrapper().getRequestType() == ITaskWrapper.M3U8_VOD) { + return mOption.getUrls().size(); + } + if (getWrapper().getRequestType() == ITaskWrapper.M3U8_LIVE) { + return 1; + } + return 0; + } +} diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/m3u8/M3U8TaskConfig.java b/M3U8Component/src/main/java/com/arialyy/aria/m3u8/M3U8TaskOption.java similarity index 86% rename from Aria/src/main/java/com/arialyy/aria/core/download/m3u8/M3U8TaskConfig.java rename to M3U8Component/src/main/java/com/arialyy/aria/m3u8/M3U8TaskOption.java index c1854005..bad1cbfc 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/m3u8/M3U8TaskConfig.java +++ b/M3U8Component/src/main/java/com/arialyy/aria/m3u8/M3U8TaskOption.java @@ -13,15 +13,20 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.arialyy.aria.core.download.m3u8; +package com.arialyy.aria.m3u8; +import com.arialyy.aria.core.processor.IBandWidthUrlConverter; +import com.arialyy.aria.core.inf.ITaskOption; +import com.arialyy.aria.core.processor.ITsMergeHandler; +import com.arialyy.aria.core.processor.ILiveTsUrlConverter; +import com.arialyy.aria.core.processor.IVodTsUrlConverter; import java.lang.ref.SoftReference; import java.util.List; /** * m3u8任务配信息 */ -public class M3U8TaskConfig { +public class M3U8TaskOption implements ITaskOption { /** * 所有ts文件的下载地址 @@ -46,7 +51,7 @@ public class M3U8TaskConfig { /** * 合并处理器 */ - private ITsMergeHandler mergeHandler; + private SoftReference mergeHandler; /** * 已完成的ts分片数量 @@ -131,7 +136,7 @@ public class M3U8TaskConfig { } public ILiveTsUrlConverter getLiveTsUrlConverter() { - return liveTsUrlConverter.get(); + return liveTsUrlConverter == null ? null : liveTsUrlConverter.get(); } public void setLiveTsUrlConverter(ILiveTsUrlConverter liveTsUrlConverter) { @@ -187,15 +192,15 @@ public class M3U8TaskConfig { } public ITsMergeHandler getMergeHandler() { - return mergeHandler; + return mergeHandler == null ? null : mergeHandler.get(); } public void setMergeHandler(ITsMergeHandler mergeHandler) { - this.mergeHandler = mergeHandler; + this.mergeHandler = new SoftReference<>(mergeHandler); } public IVodTsUrlConverter getVodUrlConverter() { - return vodUrlConverter.get(); + return vodUrlConverter == null ? null : vodUrlConverter.get(); } public void setVodUrlConverter(IVodTsUrlConverter vodUrlConverter) { diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/m3u8/M3U8ThreadTask.java b/M3U8Component/src/main/java/com/arialyy/aria/m3u8/M3U8ThreadTaskAdapter.java similarity index 70% rename from Aria/src/main/java/com/arialyy/aria/core/download/m3u8/M3U8ThreadTask.java rename to M3U8Component/src/main/java/com/arialyy/aria/m3u8/M3U8ThreadTaskAdapter.java index 6b9365d3..b4c2e4c6 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/m3u8/M3U8ThreadTask.java +++ b/M3U8Component/src/main/java/com/arialyy/aria/m3u8/M3U8ThreadTaskAdapter.java @@ -13,18 +13,16 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.arialyy.aria.core.download.m3u8; +package com.arialyy.aria.m3u8; -import com.arialyy.aria.core.common.AbsThreadTask; import com.arialyy.aria.core.common.RequestEnum; import com.arialyy.aria.core.common.SubThreadConfig; -import com.arialyy.aria.core.common.http.HttpTaskConfig; -import com.arialyy.aria.core.config.DownloadConfig; -import com.arialyy.aria.core.download.DTaskWrapper; import com.arialyy.aria.core.download.DownloadEntity; -import com.arialyy.aria.core.download.downloader.ConnectionHelp; +import com.arialyy.aria.core.task.AbsThreadTaskAdapter; import com.arialyy.aria.exception.AriaIOException; import com.arialyy.aria.exception.TaskException; +import com.arialyy.aria.http.ConnectionHelp; +import com.arialyy.aria.http.HttpTaskOption; import com.arialyy.aria.util.ALog; import java.io.BufferedInputStream; import java.io.FileOutputStream; @@ -45,37 +43,37 @@ import java.util.Set; /** * Created by lyy on 2017/1/18. 下载线程 */ -final class M3U8ThreadTask extends AbsThreadTask { +public class M3U8ThreadTaskAdapter extends AbsThreadTaskAdapter { private final String TAG = "M3U8ThreadTask"; + private HttpTaskOption mHttpTaskOption; - M3U8ThreadTask(SubThreadConfig config) { + public M3U8ThreadTaskAdapter(SubThreadConfig config) { super(config); + mHttpTaskOption = (HttpTaskOption) getTaskWrapper().getTaskOption(); } - @Override public M3U8ThreadTask call() throws Exception { - super.call(); - if (mRecord.isComplete) { + @Override protected void handlerThreadTask() { + if (getThreadRecord().isComplete) { handleComplete(); - return this; + return; } HttpURLConnection conn = null; BufferedInputStream is = null; try { - HttpTaskConfig taskDelegate = getTaskWrapper().asHttp(); - URL url = ConnectionHelp.handleUrl(getConfig().url, taskDelegate); - conn = ConnectionHelp.handleConnection(url, taskDelegate); - ALog.d(TAG, String.format("分片【%s】开始下载", mRecord.threadId)); - ConnectionHelp.setConnectParam(taskDelegate, conn); + URL url = ConnectionHelp.handleUrl(getConfig().url, mHttpTaskOption); + conn = ConnectionHelp.handleConnection(url, mHttpTaskOption); + ALog.d(TAG, String.format("分片【%s】开始下载", getThreadRecord().threadId)); + ConnectionHelp.setConnectParam(mHttpTaskOption, conn); conn.setConnectTimeout(getTaskConfig().getConnectTimeOut()); conn.setReadTimeout(getTaskConfig().getIOTimeOut()); //设置读取流的等待时间,必须设置该参数 - if (taskDelegate.isChunked()) { + if (mHttpTaskOption.isChunked()) { conn.setDoInput(true); conn.setChunkedStreamingMode(0); } conn.connect(); // 传递参数 - if (taskDelegate.getRequestEnum() == RequestEnum.POST) { - Map params = taskDelegate.getParams(); + if (mHttpTaskOption.getRequestEnum() == RequestEnum.POST) { + Map params = mHttpTaskOption.getParams(); if (params != null) { OutputStreamWriter dos = new OutputStreamWriter(conn.getOutputStream()); Set keys = params.keySet(); @@ -92,23 +90,23 @@ final class M3U8ThreadTask extends AbsThreadTask { } is = new BufferedInputStream(ConnectionHelp.convertInputStream(conn)); - if (taskDelegate.isChunked()) { + if (mHttpTaskOption.isChunked()) { readChunked(is); } else if (getConfig().isOpenDynamicFile) { readDynamicFile(is); } } catch (MalformedURLException e) { - fail(mChildCurrentLocation, new TaskException(TAG, - String.format("分片【%s】下载失败,filePath: %s, url: %s", mRecord.threadId, - getConfig().tempFile.getPath(), getEntity().getUrl()), e)); + fail(new TaskException(TAG, + String.format("分片【%s】下载失败,filePath: %s, url: %s", getThreadRecord().threadId, + getConfig().tempFile.getPath(), getEntity().getUrl()), e), false); } catch (IOException e) { - fail(mChildCurrentLocation, new TaskException(TAG, - String.format("分片【%s】下载失败,filePath: %s, url: %s", mRecord.threadId, - getConfig().tempFile.getPath(), getEntity().getUrl()), e)); + fail(new TaskException(TAG, + String.format("分片【%s】下载失败,filePath: %s, url: %s", getThreadRecord().threadId, + getConfig().tempFile.getPath(), getEntity().getUrl()), e), true); } catch (Exception e) { - fail(mChildCurrentLocation, new TaskException(TAG, - String.format("分片【%s】下载失败,filePath: %s, url: %s", mRecord.threadId, - getConfig().tempFile.getPath(), getEntity().getUrl()), e)); + fail(new TaskException(TAG, + String.format("分片【%s】下载失败,filePath: %s, url: %s", getThreadRecord().threadId, + getConfig().tempFile.getPath(), getEntity().getUrl()), e), false); } finally { try { if (is != null) { @@ -120,9 +118,7 @@ final class M3U8ThreadTask extends AbsThreadTask { } catch (IOException e) { e.printStackTrace(); } - onThreadComplete(); } - return this; } /** @@ -134,8 +130,8 @@ final class M3U8ThreadTask extends AbsThreadTask { fos = new FileOutputStream(getConfig().tempFile, true); byte[] buffer = new byte[getTaskConfig().getBuffSize()]; int len; - while (isLive() && (len = is.read(buffer)) != -1) { - if (isBreak()) { + while (getThreadTask().isLive() && (len = is.read(buffer)) != -1) { + if (getThreadTask().isBreak()) { break; } if (mSpeedBandUtil != null) { @@ -146,10 +142,9 @@ final class M3U8ThreadTask extends AbsThreadTask { } handleComplete(); } catch (IOException e) { - fail(mChildCurrentLocation, new AriaIOException(TAG, + fail(new AriaIOException(TAG, String.format("文件下载失败,savePath: %s, url: %s", getConfig().tempFile.getPath(), - getConfig().url), - e)); + getConfig().url), e), true); } finally { if (fos != null) { try { @@ -176,8 +171,8 @@ final class M3U8ThreadTask extends AbsThreadTask { ByteBuffer bf = ByteBuffer.allocate(getTaskConfig().getBuffSize()); //如果要通过 Future 的 cancel 方法取消正在运行的任务,那么该任务必定是可以 对线程中断做出响应 的任务。 - while (isLive() && (len = fic.read(bf)) != -1) { - if (isBreak()) { + while (getThreadTask().isLive() && (len = fic.read(bf)) != -1) { + if (getThreadTask().isBreak()) { break; } if (mSpeedBandUtil != null) { @@ -190,10 +185,9 @@ final class M3U8ThreadTask extends AbsThreadTask { } handleComplete(); } catch (IOException e) { - fail(mChildCurrentLocation, new AriaIOException(TAG, + fail(new AriaIOException(TAG, String.format("文件下载失败,savePath: %s, url: %s", getConfig().tempFile.getPath(), - getConfig().url), - e)); + getConfig().url), e), true); } finally { try { if (fos != null) { @@ -212,32 +206,17 @@ final class M3U8ThreadTask extends AbsThreadTask { } } + private DownloadEntity getEntity() { + return (DownloadEntity) getTaskWrapper().getEntity(); + } + /** * 处理完成配置文件的更新或事件回调 */ private void handleComplete() { - if (isBreak()) { + if (getThreadTask().isBreak()) { return; } - if (getTaskWrapper().asHttp().isChunked()) { - sendCompleteMsg(); - return; - } - - //支持断点的处理 - if (mTaskWrapper.isSupportBP()) { - writeConfig(true, getConfig().tempFile.length()); - sendCompleteMsg(); - } else { - sendCompleteMsg(); - } - } - - @Override public int getMaxSpeed() { - return mAridManager.getDownloadConfig().getMaxSpeed(); - } - - @Override protected DownloadConfig getTaskConfig() { - return getTaskWrapper().getConfig(); + complete(); } } diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/m3u8/M3U8LiveLoader.java b/M3U8Component/src/main/java/com/arialyy/aria/m3u8/live/M3U8LiveLoader.java similarity index 83% rename from Aria/src/main/java/com/arialyy/aria/core/download/m3u8/M3U8LiveLoader.java rename to M3U8Component/src/main/java/com/arialyy/aria/m3u8/live/M3U8LiveLoader.java index 7986096c..b7433ca3 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/m3u8/M3U8LiveLoader.java +++ b/M3U8Component/src/main/java/com/arialyy/aria/m3u8/live/M3U8LiveLoader.java @@ -13,21 +13,26 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.arialyy.aria.core.download.m3u8; +package com.arialyy.aria.m3u8.live; import android.os.Handler; import android.os.Looper; import android.os.Message; -import com.arialyy.aria.core.common.IThreadState; +import com.arialyy.aria.core.TaskRecord; +import com.arialyy.aria.core.ThreadRecord; import com.arialyy.aria.core.common.SubThreadConfig; -import com.arialyy.aria.core.common.TaskRecord; -import com.arialyy.aria.core.common.ThreadRecord; import com.arialyy.aria.core.download.DTaskWrapper; -import com.arialyy.aria.core.inf.IEventListener; +import com.arialyy.aria.core.inf.IThreadState; +import com.arialyy.aria.core.listener.IEventListener; import com.arialyy.aria.core.manager.ThreadTaskManager; +import com.arialyy.aria.core.task.ThreadTask; +import com.arialyy.aria.m3u8.BaseM3U8Loader; +import com.arialyy.aria.core.processor.ITsMergeHandler; +import com.arialyy.aria.m3u8.IdGenerator; +import com.arialyy.aria.m3u8.M3U8Listener; +import com.arialyy.aria.m3u8.M3U8ThreadTaskAdapter; import com.arialyy.aria.util.ALog; import com.arialyy.aria.util.FileUtil; -import com.arialyy.aria.util.IdGenerator; import java.io.File; import java.io.FilenameFilter; import java.util.ArrayList; @@ -52,11 +57,11 @@ public class M3U8LiveLoader extends BaseM3U8Loader { private Condition mCondition = LOCK.newCondition(); private LinkedBlockingQueue mPeerQueue = new LinkedBlockingQueue<>(); - M3U8LiveLoader(IEventListener listener, DTaskWrapper wrapper) { + public M3U8LiveLoader(M3U8Listener listener, DTaskWrapper wrapper) { super(listener, wrapper); } - @Override protected IThreadState getStateManager(Looper looper) { + @Override protected IThreadState createStateManager(Looper looper) { mManager = new LiveStateManager(looper, mListener); mStateHandler = new Handler(looper, mManager); return mManager; @@ -80,7 +85,7 @@ public class M3U8LiveLoader extends BaseM3U8Loader { if (url == null) { break; } - M3U8ThreadTask task = createThreadTask(cacheDir, index, url); + ThreadTask task = createThreadTask(cacheDir, index, url); getTaskList().put(index, task); mFlagQueue.offer(startThreadTask(task)); index++; @@ -98,6 +103,10 @@ public class M3U8LiveLoader extends BaseM3U8Loader { }).start(); } + @Override public long getFileSize() { + return mTempFile.length(); + } + private void notifyLock() { try { LOCK.lock(); @@ -116,7 +125,7 @@ public class M3U8LiveLoader extends BaseM3U8Loader { * * @return 线程唯一id标志 */ - private long startThreadTask(M3U8ThreadTask task) { + private long startThreadTask(ThreadTask task) { ThreadTaskManager.getInstance().startThread(mTaskWrapper.getKey(), task); return IdGenerator.getInstance().nextId(); } @@ -124,7 +133,7 @@ public class M3U8LiveLoader extends BaseM3U8Loader { /** * 配置config */ - private M3U8ThreadTask createThreadTask(String cacheDir, int indexId, String tsUrl) { + private ThreadTask createThreadTask(String cacheDir, int indexId, String tsUrl) { ThreadRecord record = new ThreadRecord(); record.taskKey = mRecord.filePath; record.isComplete = false; @@ -132,7 +141,7 @@ public class M3U8LiveLoader extends BaseM3U8Loader { record.threadType = TaskRecord.TYPE_M3U8_LIVE; record.threadId = indexId; - SubThreadConfig config = new SubThreadConfig<>(); + SubThreadConfig config = new SubThreadConfig(); config.url = tsUrl; config.tempFile = new File(getTsFilePath(cacheDir, indexId)); config.isBlock = mRecord.isBlock; @@ -144,7 +153,10 @@ public class M3U8LiveLoader extends BaseM3U8Loader { if (!config.tempFile.exists()) { FileUtil.createFile(config.tempFile.getPath()); } - return new M3U8ThreadTask(config); + ThreadTask threadTask = new ThreadTask(config); + M3U8ThreadTaskAdapter adapter = new M3U8ThreadTaskAdapter(config); + threadTask.setAdapter(adapter); + return threadTask; } /** @@ -156,7 +168,7 @@ public class M3U8LiveLoader extends BaseM3U8Loader { if (getEntity().getM3U8Entity().isGenerateIndexFile()) { return generateIndexFile(); } - ITsMergeHandler mergeHandler = mTaskWrapper.asM3U8().getMergeHandler(); + ITsMergeHandler mergeHandler = mM3U8Option.getMergeHandler(); String cacheDir = getCacheDir(); List partPath = new ArrayList<>(); String[] tsNames = new File(cacheDir).list(new FilenameFilter() { @@ -172,7 +184,7 @@ public class M3U8LiveLoader extends BaseM3U8Loader { if (mergeHandler != null) { isSuccess = mergeHandler.merge(getEntity().getM3U8Entity(), partPath); } else { - isSuccess = FileUtil.mergeFile(mEntity.getFilePath(), partPath); + isSuccess = FileUtil.mergeFile(getEntity().getFilePath(), partPath); } if (isSuccess) { // 合并成功,删除缓存文件 diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/m3u8/M3U8LiveUtil.java b/M3U8Component/src/main/java/com/arialyy/aria/m3u8/live/M3U8LiveUtil.java similarity index 53% rename from Aria/src/main/java/com/arialyy/aria/core/download/m3u8/M3U8LiveUtil.java rename to M3U8Component/src/main/java/com/arialyy/aria/m3u8/live/M3U8LiveUtil.java index 45fa8f5b..29581bc3 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/m3u8/M3U8LiveUtil.java +++ b/M3U8Component/src/main/java/com/arialyy/aria/m3u8/live/M3U8LiveUtil.java @@ -13,17 +13,22 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.arialyy.aria.core.download.m3u8; +package com.arialyy.aria.m3u8.live; import android.text.TextUtils; +import com.arialyy.aria.core.common.AbsEntity; import com.arialyy.aria.core.common.CompleteInfo; -import com.arialyy.aria.core.common.IUtil; -import com.arialyy.aria.core.common.OnFileInfoCallback; import com.arialyy.aria.core.download.DTaskWrapper; -import com.arialyy.aria.core.inf.AbsEntity; -import com.arialyy.aria.core.inf.IDownloadListener; +import com.arialyy.aria.core.download.DownloadEntity; +import com.arialyy.aria.core.processor.ILiveTsUrlConverter; +import com.arialyy.aria.core.inf.OnFileInfoCallback; +import com.arialyy.aria.core.loader.AbsLoader; +import com.arialyy.aria.core.loader.AbsNormalLoaderUtil; import com.arialyy.aria.exception.BaseException; import com.arialyy.aria.exception.M3U8Exception; +import com.arialyy.aria.m3u8.M3U8InfoThread; +import com.arialyy.aria.m3u8.M3U8Listener; +import com.arialyy.aria.m3u8.M3U8TaskOption; import com.arialyy.aria.util.ALog; import java.util.ArrayList; import java.util.List; @@ -41,42 +46,65 @@ import java.util.concurrent.TimeUnit; * 4、对于直播来说是没有停止的,停止就代表完成 * 5、不处理直播切片下载失败的状态 */ -public class M3U8LiveUtil implements IUtil { +public class M3U8LiveUtil extends AbsNormalLoaderUtil { private final String TAG = "M3U8LiveDownloadUtil"; - private DTaskWrapper mWrapper; - private IDownloadListener mListener; - private boolean isStop = false, isCancel = false; - private M3U8LiveLoader mLoader; private M3U8InfoThread mInfoThread; private ScheduledThreadPoolExecutor mTimer; private ExecutorService mInfoPool = Executors.newCachedThreadPool(); private List mPeerUrls = new ArrayList<>(); + private M3U8TaskOption mM3U8Option; - public M3U8LiveUtil(DTaskWrapper wrapper, IDownloadListener listener) { - mWrapper = wrapper; - mListener = listener; - mLoader = new M3U8LiveLoader(mListener, mWrapper); + protected M3U8LiveUtil(DTaskWrapper wrapper, M3U8Listener listener) { + super(wrapper, listener); + wrapper.generateM3u8Option(M3U8TaskOption.class); + mM3U8Option = (M3U8TaskOption) wrapper.getM3u8Option(); } - @Override public String getKey() { - return mWrapper.getKey(); + @Override protected AbsLoader createLoader() { + return new M3U8LiveLoader((M3U8Listener) getListener(), (DTaskWrapper) getTaskWrapper()); } - @Override public long getFileSize() { - return 0; + @Override protected Runnable createInfoThread() { + return null; } - @Override public long getCurrentLocation() { - return 0; - } + private Runnable createLiveInfoThread(){ + M3U8InfoThread infoThread = + new M3U8InfoThread((DTaskWrapper) getTaskWrapper(), new OnFileInfoCallback() { + @Override public void onComplete(String key, CompleteInfo info) { + ALog.d(TAG, "更新直播的m3u8文件"); + } - @Override public boolean isRunning() { - return mLoader.isRunning(); + @Override public void onFail(AbsEntity entity, BaseException e, boolean needRetry) { + fail(e, needRetry); + } + }); + infoThread.setOnGetPeerCallback(new M3U8InfoThread.OnGetLivePeerCallback() { + @Override public void onGetPeer(String url) { + if (mPeerUrls.contains(url)) { + return; + } + mPeerUrls.add(url); + ILiveTsUrlConverter converter = mM3U8Option.getLiveTsUrlConverter(); + if (converter != null) { + if (TextUtils.isEmpty(mM3U8Option.getBandWidthUrl())) { + url = converter.convert(((DownloadEntity) getTaskWrapper().getEntity()).getUrl(), url); + } else { + url = converter.convert(mM3U8Option.getBandWidthUrl(), url); + } + } + if (TextUtils.isEmpty(url) || !url.startsWith("http")) { + fail(new M3U8Exception(TAG, String.format("ts地址错误,url:%s", url)), false); + return; + } + getLoader().offerPeer(url); + } + }); + return infoThread; } - @Override public void cancel() { - isCancel = true; - mLoader.cancel(); + @Override protected void onCancel() { + super.onCancel(); if (mInfoThread != null) { mInfoThread.setStop(true); } @@ -85,9 +113,8 @@ public class M3U8LiveUtil implements IUtil { /** * 对于直播来说是没有停止的,停止就代表完成 */ - @Override public void stop() { - isStop = true; - mLoader.stop(); + @Override protected void onStop() { + super.onStop(); handleComplete(); } @@ -95,25 +122,20 @@ public class M3U8LiveUtil implements IUtil { if (mInfoThread != null) { mInfoThread.setStop(true); closeTimer(); - if (mWrapper.asM3U8().isMergeFile()) { - if (mLoader.mergeFile()) { - mListener.onComplete(); + if (mM3U8Option.isMergeFile()) { + if (getLoader().mergeFile()) { + getListener().onComplete(); } else { - mListener.onFail(false, new M3U8Exception(TAG, "合并文件失败")); + getListener().onFail(false, new M3U8Exception(TAG, "合并文件失败")); } } else { - mListener.onComplete(); + getListener().onComplete(); } } } - @Override public void start() { - if (isStop || isCancel) { - return; - } - mListener.onPre(); - getLiveInfo(); - mLoader.start(); + @Override protected void onStart() { + super.onStart(); startTimer(); } @@ -121,10 +143,10 @@ public class M3U8LiveUtil implements IUtil { mTimer = new ScheduledThreadPoolExecutor(1); mTimer.scheduleWithFixedDelay(new Runnable() { @Override public void run() { - mInfoThread = (M3U8InfoThread) getLiveInfo(); + mInfoThread = (M3U8InfoThread) createLiveInfoThread(); mInfoPool.execute(mInfoThread); } - }, 0, mWrapper.asM3U8().getLiveUpdateInterval(), TimeUnit.MILLISECONDS); + }, 0, mM3U8Option.getLiveUpdateInterval(), TimeUnit.MILLISECONDS); } private void closeTimer() { @@ -133,48 +155,12 @@ public class M3U8LiveUtil implements IUtil { } } - /** - * 获取直播文件信息 - */ - private Runnable getLiveInfo() { - M3U8InfoThread infoThread = new M3U8InfoThread(mWrapper, new OnFileInfoCallback() { - @Override public void onComplete(String key, CompleteInfo info) { - ALog.d(TAG, "更新直播的m3u8文件"); - } - - @Override public void onFail(AbsEntity entity, BaseException e, boolean needRetry) { - failDownload(e, needRetry); - } - }); - infoThread.setOnGetPeerCallback(new M3U8InfoThread.OnGetLivePeerCallback() { - @Override public void onGetPeer(String url) { - if (mPeerUrls.contains(url)) { - return; - } - mPeerUrls.add(url); - ILiveTsUrlConverter converter = mWrapper.asM3U8().getLiveTsUrlConverter(); - if (converter != null) { - if (TextUtils.isEmpty(mWrapper.asM3U8().getBandWidthUrl())) { - url = converter.convert(mWrapper.getEntity().getUrl(), url); - } else { - url = converter.convert(mWrapper.asM3U8().getBandWidthUrl(), url); - } - } - if (TextUtils.isEmpty(url) || !url.startsWith("http")) { - failDownload(new M3U8Exception(TAG, String.format("ts地址错误,url:%s", url)), false); - return; - } - mLoader.offerPeer(url); - } - }); - return infoThread; + @Override protected void fail(BaseException e, boolean needRetry) { + super.fail(e, needRetry); + handleComplete(); } - private void failDownload(BaseException e, boolean needRetry) { - if (isStop || isCancel) { - return; - } - handleComplete(); - mListener.onFail(needRetry, e); + @Override public M3U8LiveLoader getLoader() { + return (M3U8LiveLoader) super.getLoader(); } } diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/m3u8/M3U8VodLoader.java b/M3U8Component/src/main/java/com/arialyy/aria/m3u8/vod/M3U8VodLoader.java similarity index 88% rename from Aria/src/main/java/com/arialyy/aria/core/download/m3u8/M3U8VodLoader.java rename to M3U8Component/src/main/java/com/arialyy/aria/m3u8/vod/M3U8VodLoader.java index 0fb68a06..dc688518 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/m3u8/M3U8VodLoader.java +++ b/M3U8Component/src/main/java/com/arialyy/aria/m3u8/vod/M3U8VodLoader.java @@ -13,27 +13,31 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.arialyy.aria.core.download.m3u8; +package com.arialyy.aria.m3u8.vod; import android.os.Bundle; import android.os.Handler; import android.os.Looper; import android.os.Message; import android.util.SparseArray; -import com.arialyy.aria.core.common.AbsThreadTask; -import com.arialyy.aria.core.common.IThreadState; +import com.arialyy.aria.core.TaskRecord; +import com.arialyy.aria.core.ThreadRecord; import com.arialyy.aria.core.common.SubThreadConfig; -import com.arialyy.aria.core.common.TaskRecord; -import com.arialyy.aria.core.common.ThreadRecord; import com.arialyy.aria.core.download.DTaskWrapper; -import com.arialyy.aria.core.download.M3U8Listener; import com.arialyy.aria.core.event.Event; import com.arialyy.aria.core.event.EventMsgUtil; import com.arialyy.aria.core.event.PeerIndexEvent; -import com.arialyy.aria.core.inf.IEventListener; +import com.arialyy.aria.core.inf.IThreadState; +import com.arialyy.aria.core.listener.IEventListener; +import com.arialyy.aria.core.listener.ISchedulers; import com.arialyy.aria.core.manager.ThreadTaskManager; -import com.arialyy.aria.core.scheduler.ISchedulers; +import com.arialyy.aria.core.task.ThreadTask; import com.arialyy.aria.exception.BaseException; +import com.arialyy.aria.m3u8.BaseM3U8Loader; +import com.arialyy.aria.core.processor.ITsMergeHandler; +import com.arialyy.aria.m3u8.M3U8Listener; +import com.arialyy.aria.m3u8.M3U8TaskOption; +import com.arialyy.aria.m3u8.M3U8ThreadTaskAdapter; import com.arialyy.aria.util.ALog; import com.arialyy.aria.util.FileUtil; import java.io.File; @@ -74,16 +78,18 @@ public class M3U8VodLoader extends BaseM3U8Loader { private int mCompleteNum = 0; private ExecutorService mJumpThreadPool; private Thread jumpThread = null; + private M3U8TaskOption mM3U8Option; M3U8VodLoader(M3U8Listener listener, DTaskWrapper wrapper) { super(listener, wrapper); - mFlagQueue = new ArrayBlockingQueue<>(wrapper.asM3U8().getMaxTsQueueNum()); - EXEC_MAX_NUM = wrapper.asM3U8().getMaxTsQueueNum(); + mM3U8Option = (M3U8TaskOption) wrapper.getM3u8Option(); + mFlagQueue = new ArrayBlockingQueue<>(mM3U8Option.getMaxTsQueueNum()); + EXEC_MAX_NUM = mM3U8Option.getMaxTsQueueNum(); mJumpQueue = new ArrayBlockingQueue<>(10); EventMsgUtil.getDefault().register(this); } - @Override protected IThreadState getStateManager(Looper looper) { + @Override protected IThreadState createStateManager(Looper looper) { mManager = new VodStateManager(looper, mRecord, mListener); mStateHandler = new Handler(looper, mManager); return mManager; @@ -151,6 +157,10 @@ public class M3U8VodLoader extends BaseM3U8Loader { th.start(); } + @Override public long getFileSize() { + return getEntity().getFileSize(); + } + /** * 获取线程记录 */ @@ -180,9 +190,9 @@ public class M3U8VodLoader extends BaseM3U8Loader { * 启动线程任务 */ private void addTaskToQueue(ThreadRecord tr) throws InterruptedException { - M3U8ThreadTask task = createThreadTask(mCacheDir, tr, tr.threadId); + ThreadTask task = createThreadTask(mCacheDir, tr, tr.threadId); getTaskList().put(tr.threadId, task); - mEntity.getM3U8Entity().setPeerIndex(tr.threadId); + getEntity().getM3U8Entity().setPeerIndex(tr.threadId); TempFlag flag = startThreadTask(task, tr.threadId); if (flag != null) { mFlagQueue.put(flag); @@ -194,9 +204,8 @@ public class M3U8VodLoader extends BaseM3U8Loader { */ private void initData() { mCacheDir = getCacheDir(); - if (mTaskWrapper.asM3U8().getJumpIndex() != 0) { - mCurrentEvent = - new PeerIndexEvent(mTaskWrapper.getKey(), mTaskWrapper.asM3U8().getJumpIndex()); + if (mM3U8Option.getJumpIndex() != 0) { + mCurrentEvent = new PeerIndexEvent(mTaskWrapper.getKey(), mM3U8Option.getJumpIndex()); resumeTask(); return; } @@ -397,7 +406,7 @@ public class M3U8VodLoader extends BaseM3U8Loader { * * @return 线程唯一id标志 */ - private TempFlag startThreadTask(M3U8ThreadTask task, int peerIndex) { + private TempFlag startThreadTask(ThreadTask task, int peerIndex) { if (isBreak()) { ALog.w(TAG, "任务已停止,启动线程任务失败"); return null; @@ -414,10 +423,10 @@ public class M3U8VodLoader extends BaseM3U8Loader { /** * 配置config */ - private M3U8ThreadTask createThreadTask(String cacheDir, ThreadRecord record, int index) { - SubThreadConfig config = new SubThreadConfig<>(); + private ThreadTask createThreadTask(String cacheDir, ThreadRecord record, int index) { + SubThreadConfig config = new SubThreadConfig(); config.url = record.tsUrl; - config.tempFile = new File(getTsFilePath(cacheDir, record.threadId)); + config.tempFile = new File(BaseM3U8Loader.getTsFilePath(cacheDir, record.threadId)); config.isBlock = mRecord.isBlock; config.isOpenDynamicFile = mRecord.isOpenDynamicFile; config.taskWrapper = mTaskWrapper; @@ -427,7 +436,10 @@ public class M3U8VodLoader extends BaseM3U8Loader { if (!config.tempFile.exists()) { FileUtil.createFile(config.tempFile.getPath()); } - return new M3U8ThreadTask(config); + ThreadTask threadTask = new ThreadTask(config); + M3U8ThreadTaskAdapter adapter = new M3U8ThreadTaskAdapter(config); + threadTask.setAdapter(adapter); + return threadTask; } /** @@ -482,7 +494,7 @@ public class M3U8VodLoader extends BaseM3U8Loader { switch (msg.what) { case STATE_STOP: stopNum++; - removeSignThread((AbsThreadTask) msg.obj); + removeSignThread((ThreadTask) msg.obj); // 处理跳转位置后,恢复任务 if (isJump && (stopNum == mCurrentFlagSize || mCurrentFlagSize == 0) && !isBreak()) { resumeTask(); @@ -496,7 +508,7 @@ public class M3U8VodLoader extends BaseM3U8Loader { break; case STATE_CANCEL: cancelNum++; - removeSignThread((AbsThreadTask) msg.obj); + removeSignThread((ThreadTask) msg.obj); if (isBreak()) { ALog.d(TAG, String.format("vod任务【%s】取消", mTempFile.getName())); @@ -527,7 +539,7 @@ public class M3U8VodLoader extends BaseM3U8Loader { quitLooper(); } mCompleteNum++; - // 正在切换位置时,切片完成,队列大小减小 + // 正在切换位置时,切片完成,队列减小 if (isJump) { mCurrentFlagSize--; if (mCurrentFlagSize < 0) { @@ -535,7 +547,7 @@ public class M3U8VodLoader extends BaseM3U8Loader { } } - removeSignThread((AbsThreadTask) msg.obj); + removeSignThread((ThreadTask) msg.obj); getListener().onPeerComplete(mTaskWrapper.getKey(), msg.getData().getString(ISchedulers.DATA_M3U8_PEER_PATH), peerIndex); handlerPercent(); @@ -547,7 +559,7 @@ public class M3U8VodLoader extends BaseM3U8Loader { "startThreadNum = %s, stopNum = %s, cancelNum = %s, failNum = %s, completeNum = %s, flagQueueSize = %s", startThreadNum, stopNum, cancelNum, failNum, mCompleteNum, mFlagQueue.size())); ALog.d(TAG, String.format("vod任务【%s】完成", mTempFile.getName())); - if (mTaskWrapper.asM3U8().isMergeFile()) { + if (mM3U8Option.isMergeFile()) { if (mergeFile()) { listener.onComplete(); } else { @@ -566,7 +578,7 @@ public class M3U8VodLoader extends BaseM3U8Loader { return true; } - private void removeSignThread(AbsThreadTask threadTask) { + private void removeSignThread(ThreadTask threadTask) { int index = getTaskList().indexOfValue(threadTask); if (index != -1) { getTaskList().removeAt(index); @@ -578,12 +590,12 @@ public class M3U8VodLoader extends BaseM3U8Loader { * 设置进度 */ private void handlerPercent() { - int completeNum = mTaskWrapper.asM3U8().getCompleteNum(); + int completeNum = mM3U8Option.getCompleteNum(); completeNum++; - mTaskWrapper.asM3U8().setCompleteNum(completeNum); + mM3U8Option.setCompleteNum(completeNum); int percent = completeNum * 100 / taskRecord.threadRecords.size(); - mEntity.setPercent(percent); - mEntity.update(); + getEntity().setPercent(percent); + getEntity().update(); } @Override public boolean isFail() { @@ -616,18 +628,18 @@ public class M3U8VodLoader extends BaseM3U8Loader { if (getEntity().getM3U8Entity().isGenerateIndexFile()) { return generateIndexFile(); } - ITsMergeHandler mergeHandler = mTaskWrapper.asM3U8().getMergeHandler(); + ITsMergeHandler mergeHandler = mM3U8Option.getMergeHandler(); String cacheDir = getCacheDir(); List partPath = new ArrayList<>(); for (ThreadRecord tr : taskRecord.threadRecords) { - partPath.add(getTsFilePath(cacheDir, tr.threadId)); + partPath.add(BaseM3U8Loader.getTsFilePath(cacheDir, tr.threadId)); } boolean isSuccess; if (mergeHandler != null) { isSuccess = mergeHandler.merge(getEntity().getM3U8Entity(), partPath); if (mergeHandler.getClass().isAnonymousClass()) { - mTaskWrapper.asM3U8().setMergeHandler(null); + mM3U8Option.setMergeHandler(null); } } else { isSuccess = FileUtil.mergeFile(taskRecord.filePath, partPath); @@ -653,7 +665,7 @@ public class M3U8VodLoader extends BaseM3U8Loader { } private static class TempFlag { - AbsThreadTask threadTask; + ThreadTask threadTask; int threadId; } } diff --git a/M3U8Component/src/main/java/com/arialyy/aria/m3u8/vod/M3U8VodUtil.java b/M3U8Component/src/main/java/com/arialyy/aria/m3u8/vod/M3U8VodUtil.java new file mode 100644 index 00000000..abb28034 --- /dev/null +++ b/M3U8Component/src/main/java/com/arialyy/aria/m3u8/vod/M3U8VodUtil.java @@ -0,0 +1,101 @@ +/* + * 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.m3u8.vod; + +import android.text.TextUtils; +import com.arialyy.aria.core.common.AbsEntity; +import com.arialyy.aria.core.common.CompleteInfo; +import com.arialyy.aria.core.download.DTaskWrapper; +import com.arialyy.aria.core.download.DownloadEntity; +import com.arialyy.aria.core.processor.IVodTsUrlConverter; +import com.arialyy.aria.core.inf.OnFileInfoCallback; +import com.arialyy.aria.core.loader.AbsLoader; +import com.arialyy.aria.core.loader.AbsNormalLoaderUtil; +import com.arialyy.aria.exception.BaseException; +import com.arialyy.aria.exception.M3U8Exception; +import com.arialyy.aria.m3u8.M3U8InfoThread; +import com.arialyy.aria.m3u8.M3U8Listener; +import com.arialyy.aria.m3u8.M3U8TaskOption; +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; + +/** + * M3U8点播文件下载工具 + * 工作流程: + * 1、创建一个和文件同父路径并且同名隐藏文件夹 + * 2、将所有m3u8的ts文件下载到该文件夹中 + * 3、完成所有分片下载后,合并ts文件 + * 4、删除该隐藏文件夹 + */ +public class M3U8VodUtil extends AbsNormalLoaderUtil { + + private M3U8Listener mListener; + private List mUrls = new ArrayList<>(); + private M3U8TaskOption mM3U8Option; + + public M3U8VodUtil(DTaskWrapper wrapper, M3U8Listener listener) { + super(wrapper, listener); + wrapper.generateM3u8Option(M3U8TaskOption.class); + mListener = listener; + mM3U8Option = (M3U8TaskOption) wrapper.getM3u8Option(); + } + + @Override protected AbsLoader createLoader() { + return new M3U8VodLoader((M3U8Listener) getListener(), (DTaskWrapper) getTaskWrapper()); + } + + @Override protected Runnable createInfoThread() { + return new M3U8InfoThread((DTaskWrapper) getTaskWrapper(), new OnFileInfoCallback() { + @Override public void onComplete(String key, CompleteInfo info) { + IVodTsUrlConverter converter = mM3U8Option.getVodUrlConverter(); + if (converter != null) { + if (TextUtils.isEmpty(mM3U8Option.getBandWidthUrl())) { + mUrls.addAll(converter.convert(getEntity().getUrl(), (List) info.obj)); + } else { + mUrls.addAll( + converter.convert(mM3U8Option.getBandWidthUrl(), (List) info.obj)); + } + } else { + mUrls.addAll((Collection) info.obj); + } + if (mUrls.isEmpty()) { + fail(new M3U8Exception(TAG, "获取地址失败"), false); + return; + } else if (!mUrls.get(0).startsWith("http")) { + fail(new M3U8Exception(TAG, "地址错误,请使用IM3U8UrlExtInfHandler处理你的url信息"), false); + return; + } + mM3U8Option.setUrls(mUrls); + if (isStop()) { + getListener().onStop(getEntity().getCurrentProgress()); + } else if (isCancel()) { + getListener().onCancel(); + } else { + getLoader().start(); + } + } + + @Override public void onFail(AbsEntity entity, BaseException e, boolean needRetry) { + fail(e, needRetry); + } + }); + } + + private DownloadEntity getEntity() { + return (DownloadEntity) getTaskWrapper().getEntity(); + } +} diff --git a/M3U8Component/src/main/res/values/strings.xml b/M3U8Component/src/main/res/values/strings.xml new file mode 100644 index 00000000..bd8f4556 --- /dev/null +++ b/M3U8Component/src/main/res/values/strings.xml @@ -0,0 +1,3 @@ + + M3U8Component + diff --git a/PublicComponent/.gitignore b/PublicComponent/.gitignore new file mode 100644 index 00000000..796b96d1 --- /dev/null +++ b/PublicComponent/.gitignore @@ -0,0 +1 @@ +/build diff --git a/AriaFtpComponent/build.gradle b/PublicComponent/build.gradle similarity index 89% rename from AriaFtpComponent/build.gradle rename to PublicComponent/build.gradle index 12924738..55c66697 100644 --- a/AriaFtpComponent/build.gradle +++ b/PublicComponent/build.gradle @@ -7,8 +7,8 @@ android { defaultConfig { minSdkVersion rootProject.ext.minSdkVersion targetSdkVersion rootProject.ext.targetSdkVersion - versionCode 1 - versionName "1.0" + versionCode rootProject.ext.versionCode + versionName rootProject.ext.versionName testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" consumerProguardFiles 'consumer-rules.pro' @@ -24,9 +24,6 @@ android { dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) - implementation "androidx.appcompat:appcompat:${rootProject.ext.XAppcompatVersion}" - testImplementation 'junit:junit:4.12' - implementation project(path: ':AriaFtpPlug') } diff --git a/PublicComponent/consumer-rules.pro b/PublicComponent/consumer-rules.pro new file mode 100644 index 00000000..e69de29b diff --git a/PublicComponent/proguard-rules.pro b/PublicComponent/proguard-rules.pro new file mode 100644 index 00000000..f1b42451 --- /dev/null +++ b/PublicComponent/proguard-rules.pro @@ -0,0 +1,21 @@ +# Add project specific ProGuard rules here. +# You can control the set of applied configuration files using the +# proguardFiles setting in build.gradle. +# +# For more details, see +# http://developer.android.com/guide/developing/tools/proguard.html + +# If your project uses WebView with JS, uncomment the following +# and specify the fully qualified class name to the JavaScript interface +# class: +#-keepclassmembers class fqcn.of.javascript.interface.for.webview { +# public *; +#} + +# Uncomment this to preserve the line number information for +# debugging stack traces. +#-keepattributes SourceFile,LineNumberTable + +# If you keep the line number information, uncomment this to +# hide the original source file name. +#-renamesourcefileattribute SourceFile diff --git a/PublicComponent/src/main/AndroidManifest.xml b/PublicComponent/src/main/AndroidManifest.xml new file mode 100644 index 00000000..14fe4789 --- /dev/null +++ b/PublicComponent/src/main/AndroidManifest.xml @@ -0,0 +1,5 @@ + + + + diff --git a/PublicComponent/src/main/java/com/arialyy/aria/core/AriaConfig.java b/PublicComponent/src/main/java/com/arialyy/aria/core/AriaConfig.java new file mode 100644 index 00000000..009c77e7 --- /dev/null +++ b/PublicComponent/src/main/java/com/arialyy/aria/core/AriaConfig.java @@ -0,0 +1,199 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.arialyy.aria.core; + +import android.content.Context; +import android.net.ConnectivityManager; +import android.net.Network; +import android.net.NetworkCapabilities; +import android.net.NetworkRequest; +import android.os.Build; +import com.arialyy.aria.core.config.AppConfig; +import com.arialyy.aria.core.config.Configuration; +import com.arialyy.aria.core.config.DGroupConfig; +import com.arialyy.aria.core.config.DownloadConfig; +import com.arialyy.aria.core.config.UploadConfig; +import com.arialyy.aria.core.config.XMLReader; +import com.arialyy.aria.util.ALog; +import com.arialyy.aria.util.CommonUtil; +import java.io.File; +import java.io.IOException; +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.parsers.SAXParser; +import javax.xml.parsers.SAXParserFactory; +import org.xml.sax.SAXException; + +public class AriaConfig { + private static final String TAG = "AriaConfig"; + + public static final String DOWNLOAD_TEMP_DIR = "/Aria/temp/download/"; + public static final String UPLOAD_TEMP_DIR = "/Aria/temp/upload/"; + + private static volatile AriaConfig Instance; + private static Context APP; + private DownloadConfig mDConfig; + private UploadConfig mUConfig; + private AppConfig mAConfig; + private DGroupConfig mDGConfig; + /** + * 是否已经联网,true 已经联网 + */ + private static boolean isConnectedNet = true; + + private AriaConfig(Context context) { + APP = context.getApplicationContext(); + } + + public static AriaConfig init(Context context) { + if (Instance == null) { + synchronized (AriaConfig.class) { + if (Instance == null) { + Instance = new AriaConfig(context); + Instance.initData(); + } + } + } + return Instance; + } + + public static AriaConfig getInstance() { + if (Instance == null) { + ALog.e(TAG, "请使用init()初始化"); + } + return Instance; + } + + public Context getAPP() { + return APP; + } + + private void initData() { + initConfig(); + regNetCallBack(APP); + } + + public DownloadConfig getDConfig() { + return mDConfig; + } + + public UploadConfig getUConfig() { + return mUConfig; + } + + public AppConfig getAConfig() { + return mAConfig; + } + + public DGroupConfig getDGConfig() { + return mDGConfig; + } + + /** + * 注册网络监听,只有配置了检查网络{@link AppConfig#isNetCheck()}才会注册事件 + */ + private void regNetCallBack(Context context) { + if (!getAConfig().isNetCheck()) { + return; + } + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { + return; + } + ConnectivityManager cm = + (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); + if (cm == null) { + return; + } + + NetworkRequest.Builder builder = new NetworkRequest.Builder(); + NetworkRequest request = builder.addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET) + .addTransportType(NetworkCapabilities.TRANSPORT_WIFI) + .addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR) + .build(); + + cm.registerNetworkCallback(request, new ConnectivityManager.NetworkCallback() { + + @Override public void onLost(Network network) { + super.onLost(network); + isConnectedNet = false; + ALog.d(TAG, "onLost"); + } + + @Override public void onAvailable(Network network) { + super.onAvailable(network); + ALog.d(TAG, "onAvailable"); + isConnectedNet = true; + } + }); + } + + public boolean isConnectedNet() { + return isConnectedNet; + } + + /** + * 初始化配置文件 + */ + private void initConfig() { + mDConfig = Configuration.getInstance().downloadCfg; + mUConfig = Configuration.getInstance().uploadCfg; + mAConfig = Configuration.getInstance().appCfg; + mDGConfig = Configuration.getInstance().dGroupCfg; + + File xmlFile = new File(APP.getFilesDir().getPath() + Configuration.XML_FILE); + File tempDir = new File(APP.getFilesDir().getPath() + "/temp"); + if (!xmlFile.exists()) { + loadConfig(); + } else { + try { + String md5Code = CommonUtil.getFileMD5(xmlFile); + File file = new File(APP.getFilesDir().getPath() + "/temp.xml"); + if (file.exists()) { + file.delete(); + } + CommonUtil.createFileFormInputStream(APP.getAssets().open("aria_config.xml"), + file.getPath()); + if (!CommonUtil.checkMD5(md5Code, file) || !Configuration.getInstance().configExists()) { + loadConfig(); + } + } catch (IOException e) { + e.printStackTrace(); + } + } + if (tempDir.exists()) { + File newDir = new File(APP.getFilesDir().getPath() + AriaConfig.DOWNLOAD_TEMP_DIR); + newDir.mkdirs(); + tempDir.renameTo(newDir); + } + } + + /** + * 加载配置文件 + */ + private void loadConfig() { + try { + XMLReader helper = new XMLReader(); + SAXParserFactory factory = SAXParserFactory.newInstance(); + SAXParser parser = factory.newSAXParser(); + parser.parse(APP.getAssets().open("aria_config.xml"), helper); + CommonUtil.createFileFormInputStream(APP.getAssets().open("aria_config.xml"), + APP.getFilesDir().getPath() + Configuration.XML_FILE); + } catch (ParserConfigurationException | IOException | SAXException e) { + ALog.e(TAG, e.toString()); + } + } +} diff --git a/Aria/src/main/java/com/arialyy/aria/core/common/ftp/FtpUrlEntity.java b/PublicComponent/src/main/java/com/arialyy/aria/core/FtpUrlEntity.java similarity index 96% rename from Aria/src/main/java/com/arialyy/aria/core/common/ftp/FtpUrlEntity.java rename to PublicComponent/src/main/java/com/arialyy/aria/core/FtpUrlEntity.java index ad5ac3ed..24b8ede4 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/common/ftp/FtpUrlEntity.java +++ b/PublicComponent/src/main/java/com/arialyy/aria/core/FtpUrlEntity.java @@ -15,9 +15,8 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.arialyy.aria.core.common.ftp; +package com.arialyy.aria.core; -import com.arialyy.aria.core.common.ProtocolType; import java.net.InetAddress; /** diff --git a/Aria/src/main/java/com/arialyy/aria/core/common/ProtocolType.java b/PublicComponent/src/main/java/com/arialyy/aria/core/ProtocolType.java similarity index 96% rename from Aria/src/main/java/com/arialyy/aria/core/common/ProtocolType.java rename to PublicComponent/src/main/java/com/arialyy/aria/core/ProtocolType.java index 829489a9..d1602bd0 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/common/ProtocolType.java +++ b/PublicComponent/src/main/java/com/arialyy/aria/core/ProtocolType.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.arialyy.aria.core.common; +package com.arialyy.aria.core; import androidx.annotation.StringDef; import java.lang.annotation.Retention; diff --git a/PublicComponent/src/main/java/com/arialyy/aria/core/TaskOptionParams.java b/PublicComponent/src/main/java/com/arialyy/aria/core/TaskOptionParams.java new file mode 100644 index 00000000..baa2fa5d --- /dev/null +++ b/PublicComponent/src/main/java/com/arialyy/aria/core/TaskOptionParams.java @@ -0,0 +1,74 @@ +/* + * Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.arialyy.aria.core; + +import com.arialyy.aria.core.inf.IEventHandler; +import com.arialyy.aria.core.inf.IOptionConstant; +import java.util.HashMap; +import java.util.Map; + +/** + * 任务配置参数 + * + * @Author lyy + * @Date 2019-09-10 + */ +public class TaskOptionParams { + + /** + * 普通参数 + */ + private Map params = new HashMap<>(); + + /** + * 事件处理对象 + */ + private Map handler = new HashMap<>(); + + /** + * 设置普通参数 + * + * @param key {@link IOptionConstant} + */ + public TaskOptionParams setParams(String key, Object value) { + params.put(key, value); + return this; + } + + /** + * 设置对象参数 + */ + public TaskOptionParams setObjs(String key, IEventHandler handler) { + this.handler.put(key, handler); + return this; + } + + public Map getParams() { + return params; + } + + public Object getParam(String key){ + return params.get(key); + } + + public IEventHandler getHandler(String key){ + return handler.get(key); + } + + public Map getHandler() { + return handler; + } +} diff --git a/Aria/src/main/java/com/arialyy/aria/core/common/TaskRecord.java b/PublicComponent/src/main/java/com/arialyy/aria/core/TaskRecord.java similarity index 98% rename from Aria/src/main/java/com/arialyy/aria/core/common/TaskRecord.java rename to PublicComponent/src/main/java/com/arialyy/aria/core/TaskRecord.java index 90a03463..035d7127 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/common/TaskRecord.java +++ b/PublicComponent/src/main/java/com/arialyy/aria/core/TaskRecord.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.arialyy.aria.core.common; +package com.arialyy.aria.core; import com.arialyy.aria.orm.DbEntity; import com.arialyy.aria.orm.annotation.Ignore; diff --git a/Aria/src/main/java/com/arialyy/aria/core/common/ThreadRecord.java b/PublicComponent/src/main/java/com/arialyy/aria/core/ThreadRecord.java similarity index 97% rename from Aria/src/main/java/com/arialyy/aria/core/common/ThreadRecord.java rename to PublicComponent/src/main/java/com/arialyy/aria/core/ThreadRecord.java index ec29e4fd..aca2b47f 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/common/ThreadRecord.java +++ b/PublicComponent/src/main/java/com/arialyy/aria/core/ThreadRecord.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.arialyy.aria.core.common; +package com.arialyy.aria.core; import com.arialyy.aria.orm.DbEntity; diff --git a/Aria/src/main/java/com/arialyy/aria/core/inf/AbsEntity.java b/PublicComponent/src/main/java/com/arialyy/aria/core/common/AbsEntity.java similarity index 97% rename from Aria/src/main/java/com/arialyy/aria/core/inf/AbsEntity.java rename to PublicComponent/src/main/java/com/arialyy/aria/core/common/AbsEntity.java index f7ea95fd..02eb8105 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/inf/AbsEntity.java +++ b/PublicComponent/src/main/java/com/arialyy/aria/core/common/AbsEntity.java @@ -13,10 +13,12 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.arialyy.aria.core.inf; +package com.arialyy.aria.core.common; import android.os.Parcel; import android.os.Parcelable; +import com.arialyy.aria.core.inf.IEntity; +import com.arialyy.aria.core.wrapper.ITaskWrapper; import com.arialyy.aria.orm.DbEntity; import com.arialyy.aria.orm.annotation.Default; import com.arialyy.aria.orm.annotation.Ignore; diff --git a/Aria/src/main/java/com/arialyy/aria/core/inf/AbsGroupEntity.java b/PublicComponent/src/main/java/com/arialyy/aria/core/common/AbsGroupEntity.java similarity index 98% rename from Aria/src/main/java/com/arialyy/aria/core/inf/AbsGroupEntity.java rename to PublicComponent/src/main/java/com/arialyy/aria/core/common/AbsGroupEntity.java index c35381d9..4acd8d81 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/inf/AbsGroupEntity.java +++ b/PublicComponent/src/main/java/com/arialyy/aria/core/common/AbsGroupEntity.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.arialyy.aria.core.inf; +package com.arialyy.aria.core.common; import android.os.Parcel; import android.os.Parcelable; diff --git a/Aria/src/main/java/com/arialyy/aria/core/inf/AbsNormalEntity.java b/PublicComponent/src/main/java/com/arialyy/aria/core/common/AbsNormalEntity.java similarity index 97% rename from Aria/src/main/java/com/arialyy/aria/core/inf/AbsNormalEntity.java rename to PublicComponent/src/main/java/com/arialyy/aria/core/common/AbsNormalEntity.java index 52d531db..d26bce47 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/inf/AbsNormalEntity.java +++ b/PublicComponent/src/main/java/com/arialyy/aria/core/common/AbsNormalEntity.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.arialyy.aria.core.inf; +package com.arialyy.aria.core.common; import android.os.Parcel; import android.os.Parcelable; @@ -84,6 +84,8 @@ public abstract class AbsNormalEntity extends AbsEntity implements Parcelable { this.redirectUrl = redirectUrl; } + public abstract String getFilePath(); + public AbsNormalEntity() { } diff --git a/PublicComponent/src/main/java/com/arialyy/aria/core/common/AbsRecordHandlerAdapter.java b/PublicComponent/src/main/java/com/arialyy/aria/core/common/AbsRecordHandlerAdapter.java new file mode 100644 index 00000000..139a5633 --- /dev/null +++ b/PublicComponent/src/main/java/com/arialyy/aria/core/common/AbsRecordHandlerAdapter.java @@ -0,0 +1,41 @@ +/* + * 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.common; + +import com.arialyy.aria.core.inf.IRecordHandlerAdapter; +import com.arialyy.aria.core.wrapper.AbsTaskWrapper; +import com.arialyy.aria.util.CommonUtil; + +/** + * @Author lyy + * @Date 2019-09-19 + */ +public abstract class AbsRecordHandlerAdapter implements IRecordHandlerAdapter { + private AbsTaskWrapper mWrapper; + protected String TAG = CommonUtil.getClassName(getClass()); + + public AbsRecordHandlerAdapter(AbsTaskWrapper wrapper) { + mWrapper = wrapper; + } + + public AbsTaskWrapper getWrapper() { + return mWrapper; + } + + protected AbsNormalEntity getEntity() { + return (AbsNormalEntity) getWrapper().getEntity(); + } +} diff --git a/Aria/src/main/java/com/arialyy/aria/core/common/CompleteInfo.java b/PublicComponent/src/main/java/com/arialyy/aria/core/common/CompleteInfo.java similarity index 87% rename from Aria/src/main/java/com/arialyy/aria/core/common/CompleteInfo.java rename to PublicComponent/src/main/java/com/arialyy/aria/core/common/CompleteInfo.java index 34a04ed3..f6107d28 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/common/CompleteInfo.java +++ b/PublicComponent/src/main/java/com/arialyy/aria/core/common/CompleteInfo.java @@ -15,7 +15,7 @@ */ package com.arialyy.aria.core.common; -import com.arialyy.aria.core.inf.AbsTaskWrapper; +import com.arialyy.aria.core.wrapper.ITaskWrapper; /** * Created by AriaL on 2018/3/3. @@ -27,7 +27,7 @@ public class CompleteInfo { */ public int code; - public AbsTaskWrapper wrapper; + public ITaskWrapper wrapper; public Object obj; @@ -35,7 +35,7 @@ public class CompleteInfo { } - public CompleteInfo(int code, AbsTaskWrapper wrapper) { + public CompleteInfo(int code, ITaskWrapper wrapper) { this.code = code; this.wrapper = wrapper; } diff --git a/PublicComponent/src/main/java/com/arialyy/aria/core/common/RecordHandler.java b/PublicComponent/src/main/java/com/arialyy/aria/core/common/RecordHandler.java new file mode 100644 index 00000000..bef20c95 --- /dev/null +++ b/PublicComponent/src/main/java/com/arialyy/aria/core/common/RecordHandler.java @@ -0,0 +1,208 @@ +/* + * 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.common; + +import com.arialyy.aria.core.TaskRecord; +import com.arialyy.aria.core.ThreadRecord; +import com.arialyy.aria.core.download.DownloadEntity; +import com.arialyy.aria.core.inf.IRecordHandler; +import com.arialyy.aria.core.inf.IRecordHandlerAdapter; +import com.arialyy.aria.core.upload.UploadEntity; +import com.arialyy.aria.core.wrapper.AbsTaskWrapper; +import com.arialyy.aria.core.wrapper.ITaskWrapper; +import com.arialyy.aria.core.wrapper.RecordWrapper; +import com.arialyy.aria.orm.DbEntity; +import com.arialyy.aria.util.ALog; +import com.arialyy.aria.util.CommonUtil; +import com.arialyy.aria.util.DbDataHelper; +import com.arialyy.aria.util.FileUtil; +import java.io.File; +import java.util.HashSet; +import java.util.List; +import java.util.Properties; +import java.util.Set; + +/** + * 处理任务记录,分配线程区间 + */ +public class RecordHandler implements IRecordHandler { + private final String TAG = "RecordHandler"; + + @Deprecated private File mConfigFile; + private TaskRecord mTaskRecord; + private AbsTaskWrapper mTaskWrapper; + private AbsNormalEntity mEntity; + private IRecordHandlerAdapter mAdapter; + + public RecordHandler(AbsTaskWrapper wrapper) { + mTaskWrapper = wrapper; + mEntity = (AbsNormalEntity) mTaskWrapper.getEntity(); + } + + public void setAdapter(IRecordHandlerAdapter mAdapter) { + this.mAdapter = mAdapter; + } + + /** + * 获取任务记录,如果任务记录存在,检查任务记录 + * 检查记录 对于分块任务: 子分块不存在或被删除,子线程将重新下载 + * 对于普通任务: 预下载文件不存在,则任务任务呗删除 + * 如果任务记录不存在或线程记录不存在,初始化记录 + * + * @return 任务记录 + */ + @Override + public TaskRecord getRecord() { + mConfigFile = new File(CommonUtil.getFileConfigPath(false, mEntity.getFileName())); + if (mConfigFile.exists()) { + convertDb(); + } else { + mTaskRecord = DbDataHelper.getTaskRecord(getFilePath()); + if (mTaskRecord == null) { + initRecord(true); + } else { + File file = new File(mTaskRecord.filePath); + if (!file.exists()) { + ALog.w(TAG, String.format("文件【%s】不存在,重新分配线程区间", mTaskRecord.filePath)); + DbEntity.deleteData(ThreadRecord.class, "taskKey=?", mTaskRecord.filePath); + initRecord(false); + } else if (mTaskRecord.threadRecords == null || mTaskRecord.threadRecords.isEmpty()) { + initRecord(false); + } + mAdapter.handlerTaskRecord(mTaskRecord); + } + } + saveRecord(); + return mTaskRecord; + } + + /** + * convertDb 是兼容性代码 从3.4.1开始,线程配置信息将存储在数据库中。 将配置文件的内容复制到数据库中,并将配置文件删除 + */ + private void convertDb() { + List records = + DbEntity.findRelationData(RecordWrapper.class, "TaskRecord.filePath=?", + getFilePath()); + if (records == null || records.size() == 0) { + Properties pro = FileUtil.loadConfig(mConfigFile); + if (pro.isEmpty()) { + ALog.d(TAG, "老版本的线程记录为空,任务为新任务"); + initRecord(true); + return; + } + + Set keys = pro.keySet(); + // 老版本记录是5s存一次,但是5s中内,如果线程执行完成,record记录是没有的,只有state记录... + // 第一步应该是record 和 state去重取正确的线程数 + Set set = new HashSet<>(); + for (Object key : keys) { + String str = String.valueOf(key); + int i = Integer.parseInt(str.substring(str.length() - 1)); + set.add(i); + } + int threadNum = set.size(); + if (threadNum == 0) { + ALog.d(TAG, "线程数为空,任务为新任务"); + initRecord(true); + return; + } + mTaskWrapper.setNewTask(false); + mTaskRecord = mAdapter.createTaskRecord(threadNum); + mTaskRecord.isOpenDynamicFile = false; + mTaskRecord.isBlock = false; + File tempFile = new File(getFilePath()); + for (int i = 0; i < threadNum; i++) { + ThreadRecord tRecord = new ThreadRecord(); + tRecord.taskKey = mTaskRecord.filePath; + Object state = pro.getProperty(tempFile.getName() + STATE + i); + Object record = pro.getProperty(tempFile.getName() + RECORD + i); + if (state != null && Integer.parseInt(String.valueOf(state)) == 1) { + tRecord.isComplete = true; + continue; + } + if (record != null) { + long temp = Long.parseLong(String.valueOf(record)); + tRecord.startLocation = temp > 0 ? temp : 0; + } else { + tRecord.startLocation = 0; + } + mTaskRecord.threadRecords.add(tRecord); + } + mConfigFile.delete(); + } + } + + /** + * 初始化任务记录,分配线程区间,如果任务记录不存在,则创建新的任务记录 + * + * @param newRecord {@code true} 需要创建新{@link TaskRecord} + */ + private void initRecord(boolean newRecord) { + if (newRecord) { + mTaskRecord = mAdapter.createTaskRecord(mAdapter.initTaskThreadNum()); + } + mTaskWrapper.setNewTask(true); + int requestType = mTaskWrapper.getRequestType(); + if (requestType == ITaskWrapper.M3U8_LIVE) { + return; + } + long blockSize = mEntity.getFileSize() / mTaskRecord.threadNum; + // 处理线程区间记录 + for (int i = 0; i < mTaskRecord.threadNum; i++) { + long startL = i * blockSize, endL = (i + 1) * blockSize; + ThreadRecord tr = mAdapter.createThreadRecord(mTaskRecord, i, startL, endL); + mTaskRecord.threadRecords.add(tr); + } + } + + /** + * 保存任务记录 + */ + private void saveRecord() { + mTaskRecord.threadNum = mTaskRecord.threadRecords.size(); + mTaskRecord.save(); + if (mTaskRecord.threadRecords != null && !mTaskRecord.threadRecords.isEmpty()) { + DbEntity.saveAll(mTaskRecord.threadRecords); + } + ALog.d(TAG, String.format("保存记录,线程记录数:%s", mTaskRecord.threadRecords.size())); + } + + /** + * 获取记录类型 + * + * @return {@link #TYPE_DOWNLOAD}、{@link #TYPE_UPLOAD} + */ + private int getRecordType() { + if (mEntity instanceof DownloadEntity) { + return TYPE_DOWNLOAD; + } else { + return TYPE_UPLOAD; + } + } + + /** + * 获取任务路径 + * + * @return 任务文件路径 + */ + private String getFilePath() { + if (mEntity instanceof DownloadEntity) { + return ((DownloadEntity) mTaskWrapper.getEntity()).getFilePath(); + } else { + return ((UploadEntity) mTaskWrapper.getEntity()).getFilePath(); + } + } +} diff --git a/PublicComponent/src/main/java/com/arialyy/aria/core/common/RecordHelper.java b/PublicComponent/src/main/java/com/arialyy/aria/core/common/RecordHelper.java new file mode 100644 index 00000000..a3edd328 --- /dev/null +++ b/PublicComponent/src/main/java/com/arialyy/aria/core/common/RecordHelper.java @@ -0,0 +1,138 @@ +/* + * 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.common; + +import com.arialyy.aria.core.TaskRecord; +import com.arialyy.aria.core.ThreadRecord; +import com.arialyy.aria.core.inf.IRecordHandler; +import com.arialyy.aria.core.wrapper.AbsTaskWrapper; +import com.arialyy.aria.util.ALog; +import java.io.File; + +/** + * 任务记录帮助类,用于处理统一的逻辑 + * + * @Author lyy + * @Date 2019-09-19 + */ +public class RecordHelper { + private String TAG = "RecordHelper"; + + private AbsTaskWrapper mWrapper; + protected TaskRecord mTaskRecord; + + public RecordHelper(AbsTaskWrapper wrapper, TaskRecord record) { + mWrapper = wrapper; + mTaskRecord = record; + } + + /** + * 处理分块任务的记录,分块文件(blockFileLen)长度必须需要小于等于线程区间(threadRectLen)的长度 + */ + public void handleBlockRecord() { + // 默认线程分块长度 + long normalRectLen = mWrapper.getEntity().getFileSize() / mTaskRecord.threadRecords.size(); + for (ThreadRecord tr : mTaskRecord.threadRecords) { + long threadRect = tr.blockLen; + + File temp = + new File(String.format(IRecordHandler.SUB_PATH, mTaskRecord.filePath, tr.threadId)); + if (!temp.exists()) { + ALog.i(TAG, String.format("分块文件【%s】不存在,该分块将重新开始", temp.getPath())); + tr.isComplete = false; + tr.startLocation = tr.threadId * normalRectLen; + } else { + if (!tr.isComplete) { + ALog.i(TAG, String.format( + "startLocation = %s; endLocation = %s; block = %s; tempLen = %s; threadId = %s", + tr.startLocation, tr.endLocation, threadRect, temp.length(), tr.threadId)); + + long blockFileLen = temp.length(); // 磁盘中的分块文件长度 + /* + * 检查磁盘中的分块文件 + */ + if (blockFileLen > threadRect) { + ALog.i(TAG, String.format("分块【%s】错误,分块长度【%s】 > 线程区间长度【%s】,将重新开始该分块", + tr.threadId, blockFileLen, threadRect)); + temp.delete(); + tr.startLocation = tr.threadId * threadRect; + continue; + } + + long realLocation = + tr.threadId * normalRectLen + blockFileLen; //正常情况下,该线程的startLocation的位置 + /* + * 检查记录文件 + */ + if (blockFileLen == threadRect) { + ALog.i(TAG, String.format("分块【%s】已完成,更新记录", temp.getPath())); + tr.startLocation = blockFileLen; + tr.isComplete = true; + } else if (tr.startLocation != realLocation) { // 处理记录小于分块文件长度的情况 + ALog.i(TAG, String.format("修正分块【%s】的进度记录为:%s", temp.getPath(), realLocation)); + tr.startLocation = realLocation; + } + } else { + ALog.i(TAG, String.format("分块【%s】已完成", temp.getPath())); + } + } + } + mWrapper.setNewTask(false); + } + + /** + * 处理单线程的任务的记录 + */ + public void handleSingleThreadRecord() { + File file = new File(mTaskRecord.filePath); + ThreadRecord tr = mTaskRecord.threadRecords.get(0); + if (!file.exists()) { + ALog.w(TAG, String.format("文件【%s】不存在,任务将重新开始", file.getPath())); + tr.startLocation = 0; + tr.isComplete = false; + tr.endLocation = mWrapper.getEntity().getFileSize(); + } else if (mTaskRecord.isOpenDynamicFile) { + if (file.length() > mWrapper.getEntity().getFileSize()) { + ALog.i(TAG, String.format("文件【%s】错误,任务重新开始", file.getPath())); + file.delete(); + tr.startLocation = 0; + tr.isComplete = false; + tr.endLocation = mWrapper.getEntity().getFileSize(); + } else if (file.length() == mWrapper.getEntity().getFileSize()) { + tr.isComplete = true; + } else { + if (file.length() != tr.startLocation) { + ALog.i(TAG, String.format("修正【%s】的进度记录为:%s", file.getPath(), file.length())); + tr.startLocation = file.length(); + tr.isComplete = false; + } + } + } + mWrapper.setNewTask(false); + } + + /** + * 处理不支持断点的记录 + */ + public void handleNoSupportBPRecord() { + ThreadRecord tr = mTaskRecord.threadRecords.get(0); + tr.startLocation = 0; + tr.endLocation = mWrapper.getEntity().getFileSize(); + tr.taskKey = mTaskRecord.filePath; + tr.blockLen = tr.endLocation; + tr.isComplete = false; + } +} diff --git a/Aria/src/main/java/com/arialyy/aria/core/common/RequestEnum.java b/PublicComponent/src/main/java/com/arialyy/aria/core/common/RequestEnum.java similarity index 100% rename from Aria/src/main/java/com/arialyy/aria/core/common/RequestEnum.java rename to PublicComponent/src/main/java/com/arialyy/aria/core/common/RequestEnum.java diff --git a/Aria/src/main/java/com/arialyy/aria/core/common/SubThreadConfig.java b/PublicComponent/src/main/java/com/arialyy/aria/core/common/SubThreadConfig.java similarity index 86% rename from Aria/src/main/java/com/arialyy/aria/core/common/SubThreadConfig.java rename to PublicComponent/src/main/java/com/arialyy/aria/core/common/SubThreadConfig.java index 11cf9e52..3964916d 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/common/SubThreadConfig.java +++ b/PublicComponent/src/main/java/com/arialyy/aria/core/common/SubThreadConfig.java @@ -16,15 +16,16 @@ package com.arialyy.aria.core.common; import android.os.Handler; -import com.arialyy.aria.core.inf.AbsTaskWrapper; +import com.arialyy.aria.core.ThreadRecord; +import com.arialyy.aria.core.wrapper.AbsTaskWrapper; import java.io.File; /** * 子线程下载信息类 */ -public class SubThreadConfig { +public class SubThreadConfig { - public TASK_WRAPPER taskWrapper; + public AbsTaskWrapper taskWrapper; public boolean isBlock = false; // 启动的线程 public int startThreadNum; diff --git a/Aria/src/main/java/com/arialyy/aria/core/config/AppConfig.java b/PublicComponent/src/main/java/com/arialyy/aria/core/config/AppConfig.java similarity index 100% rename from Aria/src/main/java/com/arialyy/aria/core/config/AppConfig.java rename to PublicComponent/src/main/java/com/arialyy/aria/core/config/AppConfig.java diff --git a/Aria/src/main/java/com/arialyy/aria/core/config/BaseConfig.java b/PublicComponent/src/main/java/com/arialyy/aria/core/config/BaseConfig.java similarity index 91% rename from Aria/src/main/java/com/arialyy/aria/core/config/BaseConfig.java rename to PublicComponent/src/main/java/com/arialyy/aria/core/config/BaseConfig.java index c30791ed..a1f63f8f 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/config/BaseConfig.java +++ b/PublicComponent/src/main/java/com/arialyy/aria/core/config/BaseConfig.java @@ -1,7 +1,7 @@ package com.arialyy.aria.core.config; import android.text.TextUtils; -import com.arialyy.aria.core.AriaManager; +import com.arialyy.aria.core.AriaConfig; import com.arialyy.aria.util.ALog; import com.arialyy.aria.util.FileUtil; import java.io.Serializable; @@ -24,7 +24,7 @@ abstract class BaseConfig implements Serializable { * 保存配置 */ void save() { - String basePath = AriaManager.getInstance().getAPP().getFilesDir().getPath(); + String basePath = AriaConfig.getInstance().getAPP().getFilesDir().getPath(); String path = null; switch (getType()) { case TYPE_DOWNLOAD: diff --git a/Aria/src/main/java/com/arialyy/aria/core/config/BaseTaskConfig.java b/PublicComponent/src/main/java/com/arialyy/aria/core/config/BaseTaskConfig.java similarity index 97% rename from Aria/src/main/java/com/arialyy/aria/core/config/BaseTaskConfig.java rename to PublicComponent/src/main/java/com/arialyy/aria/core/config/BaseTaskConfig.java index 8d912618..5e33a485 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/config/BaseTaskConfig.java +++ b/PublicComponent/src/main/java/com/arialyy/aria/core/config/BaseTaskConfig.java @@ -16,15 +16,15 @@ package com.arialyy.aria.core.config; -import com.arialyy.aria.core.common.QueueMod; import com.arialyy.aria.util.ALog; +import com.arialyy.aria.util.CommonUtil; import java.io.Serializable; /** * 通用任务配置 */ public abstract class BaseTaskConfig extends BaseConfig implements Serializable { - + protected String TAG = CommonUtil.getClassName(getClass()); /** * 设置写文件buff大小,该数值大小不能小于2048,数值变小,下载速度会变慢 @@ -65,8 +65,6 @@ public abstract class BaseTaskConfig extends BaseConfig implements Serializable /** * 执行队列类型 - * - * @see QueueMod */ String queueMod = "wait"; diff --git a/Aria/src/main/java/com/arialyy/aria/core/config/ConfigType.java b/PublicComponent/src/main/java/com/arialyy/aria/core/config/ConfigType.java similarity index 100% rename from Aria/src/main/java/com/arialyy/aria/core/config/ConfigType.java rename to PublicComponent/src/main/java/com/arialyy/aria/core/config/ConfigType.java diff --git a/Aria/src/main/java/com/arialyy/aria/core/config/Configuration.java b/PublicComponent/src/main/java/com/arialyy/aria/core/config/Configuration.java similarity index 95% rename from Aria/src/main/java/com/arialyy/aria/core/config/Configuration.java rename to PublicComponent/src/main/java/com/arialyy/aria/core/config/Configuration.java index 79c46604..2978e092 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/config/Configuration.java +++ b/PublicComponent/src/main/java/com/arialyy/aria/core/config/Configuration.java @@ -15,7 +15,7 @@ */ package com.arialyy.aria.core.config; -import com.arialyy.aria.core.AriaManager; +import com.arialyy.aria.core.AriaConfig; import com.arialyy.aria.util.FileUtil; import java.io.File; @@ -37,7 +37,7 @@ public final class Configuration { private Configuration() { //删除老版本的配置文件 - String basePath = AriaManager.getInstance().getAPP().getFilesDir().getPath(); + String basePath = AriaConfig.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.getInstance().getAPP().getFilesDir().getPath(); + String basePath = AriaConfig.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/config/DGroupConfig.java b/PublicComponent/src/main/java/com/arialyy/aria/core/config/DGroupConfig.java similarity index 88% rename from Aria/src/main/java/com/arialyy/aria/core/config/DGroupConfig.java rename to PublicComponent/src/main/java/com/arialyy/aria/core/config/DGroupConfig.java index 5d63a34b..17989d3c 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/config/DGroupConfig.java +++ b/PublicComponent/src/main/java/com/arialyy/aria/core/config/DGroupConfig.java @@ -15,9 +15,10 @@ */ package com.arialyy.aria.core.config; +import com.arialyy.aria.core.event.DGMaxNumEvent; +import com.arialyy.aria.core.event.DSpeedEvent; import com.arialyy.aria.core.event.EventMsgUtil; -import com.arialyy.aria.core.event.SpeedEvent; -import com.arialyy.aria.core.queue.DownloadGroupTaskQueue; +import com.arialyy.aria.util.ALog; import java.io.Serializable; /** @@ -52,7 +53,7 @@ public class DGroupConfig extends BaseTaskConfig implements Serializable { @Override public DGroupConfig setMaxSpeed(int maxSpeed) { super.setMaxSpeed(maxSpeed); - EventMsgUtil.getDefault().post(new SpeedEvent(maxSpeed)); + EventMsgUtil.getDefault().post(new DSpeedEvent(maxSpeed)); return this; } @@ -77,8 +78,12 @@ public class DGroupConfig extends BaseTaskConfig implements Serializable { } public DGroupConfig setMaxTaskNum(int maxTaskNum) { + if (maxTaskNum <= 0) { + ALog.e(TAG, "组合任务最大任务数不能小于0"); + return this; + } super.setMaxTaskNum(maxTaskNum); - DownloadGroupTaskQueue.getInstance().setMaxTaskNum(maxTaskNum); + EventMsgUtil.getDefault().post(new DGMaxNumEvent(maxTaskNum)); return this; } diff --git a/Aria/src/main/java/com/arialyy/aria/core/config/DownloadConfig.java b/PublicComponent/src/main/java/com/arialyy/aria/core/config/DownloadConfig.java similarity index 86% rename from Aria/src/main/java/com/arialyy/aria/core/config/DownloadConfig.java rename to PublicComponent/src/main/java/com/arialyy/aria/core/config/DownloadConfig.java index 2a4a300f..21114cce 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/config/DownloadConfig.java +++ b/PublicComponent/src/main/java/com/arialyy/aria/core/config/DownloadConfig.java @@ -15,9 +15,10 @@ */ package com.arialyy.aria.core.config; +import com.arialyy.aria.core.event.DMaxNumEvent; +import com.arialyy.aria.core.event.DSpeedEvent; import com.arialyy.aria.core.event.EventMsgUtil; -import com.arialyy.aria.core.event.SpeedEvent; -import com.arialyy.aria.core.queue.DownloadTaskQueue; +import com.arialyy.aria.util.ALog; import java.io.Serializable; /** @@ -49,7 +50,7 @@ public class DownloadConfig extends BaseTaskConfig implements Serializable { @Override public DownloadConfig setMaxSpeed(int maxSpeed) { super.setMaxSpeed(maxSpeed); - EventMsgUtil.getDefault().post(new SpeedEvent(maxSpeed)); + EventMsgUtil.getDefault().post(new DSpeedEvent(maxSpeed)); return this; } @@ -60,8 +61,12 @@ public class DownloadConfig extends BaseTaskConfig implements Serializable { } public DownloadConfig setMaxTaskNum(int maxTaskNum) { + if (maxTaskNum <= 0) { + ALog.e(TAG, "下载任务最大任务数不能小于0"); + return this; + } super.setMaxTaskNum(maxTaskNum); - DownloadTaskQueue.getInstance().setMaxTaskNum(maxTaskNum); + EventMsgUtil.getDefault().post(new DMaxNumEvent(maxTaskNum)); return this; } diff --git a/Aria/src/main/java/com/arialyy/aria/core/config/TTaskConfigAdapeter.java b/PublicComponent/src/main/java/com/arialyy/aria/core/config/TTaskConfigAdapeter.java similarity index 100% rename from Aria/src/main/java/com/arialyy/aria/core/config/TTaskConfigAdapeter.java rename to PublicComponent/src/main/java/com/arialyy/aria/core/config/TTaskConfigAdapeter.java diff --git a/Aria/src/main/java/com/arialyy/aria/core/config/UploadConfig.java b/PublicComponent/src/main/java/com/arialyy/aria/core/config/UploadConfig.java similarity index 75% rename from Aria/src/main/java/com/arialyy/aria/core/config/UploadConfig.java rename to PublicComponent/src/main/java/com/arialyy/aria/core/config/UploadConfig.java index 3c4996c0..da2108d8 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/config/UploadConfig.java +++ b/PublicComponent/src/main/java/com/arialyy/aria/core/config/UploadConfig.java @@ -16,8 +16,9 @@ package com.arialyy.aria.core.config; import com.arialyy.aria.core.event.EventMsgUtil; -import com.arialyy.aria.core.event.SpeedEvent; -import com.arialyy.aria.core.queue.UploadTaskQueue; +import com.arialyy.aria.core.event.UMaxNumEvent; +import com.arialyy.aria.core.event.USpeedEvent; +import com.arialyy.aria.util.ALog; import java.io.Serializable; /** @@ -30,13 +31,17 @@ public class UploadConfig extends BaseTaskConfig implements Serializable { @Override public UploadConfig setMaxSpeed(int maxSpeed) { super.setMaxSpeed(maxSpeed); - EventMsgUtil.getDefault().post(new SpeedEvent(maxSpeed)); + EventMsgUtil.getDefault().post(new USpeedEvent(maxSpeed)); return this; } public UploadConfig setMaxTaskNum(int maxTaskNum) { + if (maxTaskNum <= 0){ + ALog.e(TAG, "上传任务最大任务数不能小于0"); + return this; + } super.setMaxTaskNum(maxTaskNum); - UploadTaskQueue.getInstance().setMaxTaskNum(maxTaskNum); + EventMsgUtil.getDefault().post(new UMaxNumEvent(maxTaskNum)); return this; } diff --git a/Aria/src/main/java/com/arialyy/aria/core/config/XMLReader.java b/PublicComponent/src/main/java/com/arialyy/aria/core/config/XMLReader.java similarity index 100% rename from Aria/src/main/java/com/arialyy/aria/core/config/XMLReader.java rename to PublicComponent/src/main/java/com/arialyy/aria/core/config/XMLReader.java diff --git a/Aria/src/main/java/com/arialyy/aria/core/inf/AbsGroupTaskWrapper.java b/PublicComponent/src/main/java/com/arialyy/aria/core/download/AbsGroupTaskWrapper.java similarity index 90% rename from Aria/src/main/java/com/arialyy/aria/core/inf/AbsGroupTaskWrapper.java rename to PublicComponent/src/main/java/com/arialyy/aria/core/download/AbsGroupTaskWrapper.java index fe6f199c..e1d222ca 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/inf/AbsGroupTaskWrapper.java +++ b/PublicComponent/src/main/java/com/arialyy/aria/core/download/AbsGroupTaskWrapper.java @@ -13,10 +13,11 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.arialyy.aria.core.inf; +package com.arialyy.aria.core.download; -import com.arialyy.aria.core.download.DownloadGroupEntity; import com.arialyy.aria.core.event.ErrorEvent; +import com.arialyy.aria.core.common.AbsEntity; +import com.arialyy.aria.core.wrapper.AbsTaskWrapper; import java.util.List; /** diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/DGEntityWrapper.java b/PublicComponent/src/main/java/com/arialyy/aria/core/download/DGEntityWrapper.java similarity index 92% rename from Aria/src/main/java/com/arialyy/aria/core/download/DGEntityWrapper.java rename to PublicComponent/src/main/java/com/arialyy/aria/core/download/DGEntityWrapper.java index 936b0964..0e63a544 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/DGEntityWrapper.java +++ b/PublicComponent/src/main/java/com/arialyy/aria/core/download/DGEntityWrapper.java @@ -15,6 +15,8 @@ */ package com.arialyy.aria.core.download; +import com.arialyy.aria.core.download.DownloadEntity; +import com.arialyy.aria.core.download.DownloadGroupEntity; import com.arialyy.aria.orm.AbsDbWrapper; import com.arialyy.aria.orm.annotation.Many; import com.arialyy.aria.orm.annotation.One; diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/DGTaskWrapper.java b/PublicComponent/src/main/java/com/arialyy/aria/core/download/DGTaskWrapper.java similarity index 97% rename from Aria/src/main/java/com/arialyy/aria/core/download/DGTaskWrapper.java rename to PublicComponent/src/main/java/com/arialyy/aria/core/download/DGTaskWrapper.java index 9b4ca29e..2df6b1fd 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/DGTaskWrapper.java +++ b/PublicComponent/src/main/java/com/arialyy/aria/core/download/DGTaskWrapper.java @@ -17,7 +17,6 @@ package com.arialyy.aria.core.download; import com.arialyy.aria.core.config.Configuration; import com.arialyy.aria.core.config.DGroupConfig; -import com.arialyy.aria.core.inf.AbsGroupTaskWrapper; import java.util.ArrayList; import java.util.List; diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/DTaskWrapper.java b/PublicComponent/src/main/java/com/arialyy/aria/core/download/DTaskWrapper.java similarity index 80% rename from Aria/src/main/java/com/arialyy/aria/core/download/DTaskWrapper.java rename to PublicComponent/src/main/java/com/arialyy/aria/core/download/DTaskWrapper.java index 8ee29ab7..64edb7d6 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/DTaskWrapper.java +++ b/PublicComponent/src/main/java/com/arialyy/aria/core/download/DTaskWrapper.java @@ -15,11 +15,12 @@ */ package com.arialyy.aria.core.download; +import com.arialyy.aria.core.TaskOptionParams; import com.arialyy.aria.core.config.Configuration; import com.arialyy.aria.core.config.DownloadConfig; -import com.arialyy.aria.core.download.m3u8.M3U8TaskConfig; -import com.arialyy.aria.core.download.tcp.TcpTaskConfig; -import com.arialyy.aria.core.inf.AbsTaskWrapper; +import com.arialyy.aria.core.inf.ITaskOption; +import com.arialyy.aria.core.wrapper.AbsTaskWrapper; +import com.arialyy.aria.util.ComponentUtil; /** * Created by lyy on 2017/1/23. 下载任务实体和下载实体为一对一关系,下载实体删除,任务实体自动删除 @@ -39,12 +40,9 @@ public class DTaskWrapper extends AbsTaskWrapper { /** * M3u8任务配置信息 */ - private M3U8TaskConfig m3u8TaskConfig; + private ITaskOption m3u8Option; - /** - * TCP任务配置信息 - */ - private TcpTaskConfig tcpTaskConfig; + private TaskOptionParams m3u8Params = new TaskOptionParams(); /** * 文件下载url的临时保存变量 @@ -65,18 +63,19 @@ public class DTaskWrapper extends AbsTaskWrapper { super(entity); } - public M3U8TaskConfig asM3U8() { - if (m3u8TaskConfig == null) { - m3u8TaskConfig = new M3U8TaskConfig(); - } - return m3u8TaskConfig; + public ITaskOption getM3u8Option() { + return m3u8Option; + } + + public void generateM3u8Option(Class clazz) { + m3u8Option = ComponentUtil.getInstance().buildTaskOption(clazz, m3u8Params); } - public TcpTaskConfig asTcp() { - if (tcpTaskConfig == null) { - tcpTaskConfig = new TcpTaskConfig(); + public TaskOptionParams getM3U8Params() { + if (m3u8Params == null) { + m3u8Params = new TaskOptionParams(); } - return tcpTaskConfig; + return m3u8Params; } /** diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadEntity.java b/PublicComponent/src/main/java/com/arialyy/aria/core/download/DownloadEntity.java similarity index 97% rename from Aria/src/main/java/com/arialyy/aria/core/download/DownloadEntity.java rename to PublicComponent/src/main/java/com/arialyy/aria/core/download/DownloadEntity.java index e8ebfeaa..168cd70a 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadEntity.java +++ b/PublicComponent/src/main/java/com/arialyy/aria/core/download/DownloadEntity.java @@ -19,9 +19,8 @@ package com.arialyy.aria.core.download; import android.os.Parcel; import android.os.Parcelable; import android.text.TextUtils; -import com.arialyy.aria.core.download.m3u8.M3U8Entity; -import com.arialyy.aria.core.inf.AbsNormalEntity; -import com.arialyy.aria.core.inf.ITaskWrapper; +import com.arialyy.aria.core.common.AbsNormalEntity; +import com.arialyy.aria.core.wrapper.ITaskWrapper; import com.arialyy.aria.orm.DbEntity; import com.arialyy.aria.orm.annotation.Ignore; import com.arialyy.aria.orm.annotation.Unique; @@ -137,6 +136,7 @@ public class DownloadEntity extends AbsNormalEntity implements Parcelable { return downloadPath; } + @Override public String getFilePath() { return downloadPath; } diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadGroupEntity.java b/PublicComponent/src/main/java/com/arialyy/aria/core/download/DownloadGroupEntity.java similarity index 91% rename from Aria/src/main/java/com/arialyy/aria/core/download/DownloadGroupEntity.java rename to PublicComponent/src/main/java/com/arialyy/aria/core/download/DownloadGroupEntity.java index 97662415..ce95a3e9 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadGroupEntity.java +++ b/PublicComponent/src/main/java/com/arialyy/aria/core/download/DownloadGroupEntity.java @@ -16,8 +16,8 @@ package com.arialyy.aria.core.download; import android.os.Parcel; -import com.arialyy.aria.core.inf.AbsGroupEntity; -import com.arialyy.aria.core.inf.AbsTaskWrapper; +import com.arialyy.aria.core.common.AbsGroupEntity; +import com.arialyy.aria.core.wrapper.ITaskWrapper; import com.arialyy.aria.orm.annotation.Ignore; import java.util.ArrayList; import java.util.List; @@ -48,7 +48,7 @@ public class DownloadGroupEntity extends AbsGroupEntity { } @Override public int getTaskType() { - return getKey().startsWith("ftp") ? AbsTaskWrapper.D_FTP_DIR : AbsTaskWrapper.DG_HTTP; + return getKey().startsWith("ftp") ? ITaskWrapper.D_FTP_DIR : ITaskWrapper.DG_HTTP; } public DownloadGroupEntity() { diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/m3u8/M3U8Entity.java b/PublicComponent/src/main/java/com/arialyy/aria/core/download/M3U8Entity.java similarity index 95% rename from Aria/src/main/java/com/arialyy/aria/core/download/m3u8/M3U8Entity.java rename to PublicComponent/src/main/java/com/arialyy/aria/core/download/M3U8Entity.java index 715d62df..05a01a41 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/m3u8/M3U8Entity.java +++ b/PublicComponent/src/main/java/com/arialyy/aria/core/download/M3U8Entity.java @@ -13,13 +13,13 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.arialyy.aria.core.download.m3u8; +package com.arialyy.aria.core.download; import android.os.Parcel; import android.os.Parcelable; import android.text.TextUtils; -import com.arialyy.aria.core.common.TaskRecord; -import com.arialyy.aria.core.common.ThreadRecord; +import com.arialyy.aria.core.TaskRecord; +import com.arialyy.aria.core.ThreadRecord; import com.arialyy.aria.orm.DbEntity; import com.arialyy.aria.util.ALog; import com.arialyy.aria.util.DbDataHelper; @@ -130,7 +130,7 @@ public class M3U8Entity extends DbEntity implements Parcelable { /** * 获取m3u8切片 * 如果任务未完成,则返回所有已下载完成的切片; - * 如果任务已完成,如果你设置{@link M3U8Delegate#merge(boolean)}合并分块的请求,返回null;如果没有设置该请求,则返回所有已下载完成的切片 + * 如果任务已完成,如果你设置了合并分块的请求,返回null;如果没有设置该请求,则返回所有已下载完成的切片 */ public List getCompletedPeer() { if (TextUtils.isEmpty(getCacheDir())) { diff --git a/Aria/src/main/java/com/arialyy/aria/core/common/AEvent.java b/PublicComponent/src/main/java/com/arialyy/aria/core/event/DGMaxNumEvent.java similarity index 63% rename from Aria/src/main/java/com/arialyy/aria/core/common/AEvent.java rename to PublicComponent/src/main/java/com/arialyy/aria/core/event/DGMaxNumEvent.java index f5852b92..ac5f9286 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/common/AEvent.java +++ b/PublicComponent/src/main/java/com/arialyy/aria/core/event/DGMaxNumEvent.java @@ -13,25 +13,16 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.arialyy.aria.core.common; +package com.arialyy.aria.core.event; /** - * Aria事件总线 + * 组合任务最大下载任务数事件 */ -public class AEvent { - private static final Object LOCK = new Object(); - private static volatile AEvent INSTANCE = null; +public class DGMaxNumEvent { - private AEvent() { + public int maxNum; - } - - public static AEvent getInstance() { - if (INSTANCE == null) { - synchronized (LOCK) { - INSTANCE = new AEvent(); - } - } - return INSTANCE; + public DGMaxNumEvent(int maxNum) { + this.maxNum = maxNum; } } diff --git a/PublicComponent/src/main/java/com/arialyy/aria/core/event/DMaxNumEvent.java b/PublicComponent/src/main/java/com/arialyy/aria/core/event/DMaxNumEvent.java new file mode 100644 index 00000000..ef1adb02 --- /dev/null +++ b/PublicComponent/src/main/java/com/arialyy/aria/core/event/DMaxNumEvent.java @@ -0,0 +1,28 @@ +/* + * Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.arialyy.aria.core.event; + +/** + * 最大下载任务数事件 + */ +public class DMaxNumEvent { + + public int maxNum; + + public DMaxNumEvent(int maxNum) { + this.maxNum = maxNum; + } +} diff --git a/Aria/src/main/java/com/arialyy/aria/core/event/SpeedEvent.java b/PublicComponent/src/main/java/com/arialyy/aria/core/event/DSpeedEvent.java similarity index 92% rename from Aria/src/main/java/com/arialyy/aria/core/event/SpeedEvent.java rename to PublicComponent/src/main/java/com/arialyy/aria/core/event/DSpeedEvent.java index 8feb7f59..4b2e30cb 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/event/SpeedEvent.java +++ b/PublicComponent/src/main/java/com/arialyy/aria/core/event/DSpeedEvent.java @@ -15,11 +15,11 @@ */ package com.arialyy.aria.core.event; -public class SpeedEvent { +public class DSpeedEvent { public int speed; - public SpeedEvent(int speed) { + public DSpeedEvent(int speed) { this.speed = speed; } } diff --git a/Aria/src/main/java/com/arialyy/aria/core/event/ErrorEvent.java b/PublicComponent/src/main/java/com/arialyy/aria/core/event/ErrorEvent.java similarity index 100% rename from Aria/src/main/java/com/arialyy/aria/core/event/ErrorEvent.java rename to PublicComponent/src/main/java/com/arialyy/aria/core/event/ErrorEvent.java diff --git a/Aria/src/main/java/com/arialyy/aria/core/event/Event.java b/PublicComponent/src/main/java/com/arialyy/aria/core/event/Event.java similarity index 100% rename from Aria/src/main/java/com/arialyy/aria/core/event/Event.java rename to PublicComponent/src/main/java/com/arialyy/aria/core/event/Event.java diff --git a/Aria/src/main/java/com/arialyy/aria/core/event/EventMethodInfo.java b/PublicComponent/src/main/java/com/arialyy/aria/core/event/EventMethodInfo.java similarity index 100% rename from Aria/src/main/java/com/arialyy/aria/core/event/EventMethodInfo.java rename to PublicComponent/src/main/java/com/arialyy/aria/core/event/EventMethodInfo.java diff --git a/Aria/src/main/java/com/arialyy/aria/core/event/EventMsgUtil.java b/PublicComponent/src/main/java/com/arialyy/aria/core/event/EventMsgUtil.java similarity index 100% rename from Aria/src/main/java/com/arialyy/aria/core/event/EventMsgUtil.java rename to PublicComponent/src/main/java/com/arialyy/aria/core/event/EventMsgUtil.java diff --git a/Aria/src/main/java/com/arialyy/aria/core/event/PeerIndexEvent.java b/PublicComponent/src/main/java/com/arialyy/aria/core/event/PeerIndexEvent.java similarity index 100% rename from Aria/src/main/java/com/arialyy/aria/core/event/PeerIndexEvent.java rename to PublicComponent/src/main/java/com/arialyy/aria/core/event/PeerIndexEvent.java diff --git a/PublicComponent/src/main/java/com/arialyy/aria/core/event/UMaxNumEvent.java b/PublicComponent/src/main/java/com/arialyy/aria/core/event/UMaxNumEvent.java new file mode 100644 index 00000000..6c59f59e --- /dev/null +++ b/PublicComponent/src/main/java/com/arialyy/aria/core/event/UMaxNumEvent.java @@ -0,0 +1,28 @@ +/* + * Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.arialyy.aria.core.event; + +/** + * 最大上传任务数事件 + */ +public class UMaxNumEvent { + + public int maxNum; + + public UMaxNumEvent(int maxNum) { + this.maxNum = maxNum; + } +} diff --git a/PublicComponent/src/main/java/com/arialyy/aria/core/event/USpeedEvent.java b/PublicComponent/src/main/java/com/arialyy/aria/core/event/USpeedEvent.java new file mode 100644 index 00000000..c3693709 --- /dev/null +++ b/PublicComponent/src/main/java/com/arialyy/aria/core/event/USpeedEvent.java @@ -0,0 +1,25 @@ +/* + * Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.arialyy.aria.core.event; + +public class USpeedEvent { + + public int speed; + + public USpeedEvent(int speed) { + this.speed = speed; + } +} diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/group/AbsGroupUtil.java b/PublicComponent/src/main/java/com/arialyy/aria/core/group/AbsGroupUtil.java similarity index 80% rename from Aria/src/main/java/com/arialyy/aria/core/download/group/AbsGroupUtil.java rename to PublicComponent/src/main/java/com/arialyy/aria/core/group/AbsGroupUtil.java index a0335b02..8a66e24d 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/group/AbsGroupUtil.java +++ b/PublicComponent/src/main/java/com/arialyy/aria/core/group/AbsGroupUtil.java @@ -13,16 +13,18 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.arialyy.aria.core.download.group; +package com.arialyy.aria.core.group; import android.os.Handler; import android.os.Looper; -import com.arialyy.aria.core.common.IUtil; import com.arialyy.aria.core.config.Configuration; import com.arialyy.aria.core.download.DGTaskWrapper; import com.arialyy.aria.core.download.DTaskWrapper; import com.arialyy.aria.core.inf.IEntity; +import com.arialyy.aria.core.inf.IUtil; +import com.arialyy.aria.core.listener.IDGroupListener; import com.arialyy.aria.util.ALog; +import com.arialyy.aria.util.CommonUtil; import java.util.Map; import java.util.WeakHashMap; import java.util.concurrent.ScheduledThreadPoolExecutor; @@ -32,16 +34,8 @@ import java.util.concurrent.TimeUnit; * Created by AriaL on 2017/6/30. * 任务组核心逻辑 */ -public abstract class AbsGroupUtil implements IUtil { - private final String TAG = "AbsGroupUtil"; - /** - * FTP文件夹 - */ - int FTP_DIR = 0xa1; - /** - * D_HTTP 任务组 - */ - int HTTP_GROUP = 0xa2; +public abstract class AbsGroupUtil implements IUtil, Runnable { + protected final String TAG = CommonUtil.getClassName(getClass()); private long mCurrentLocation = 0; protected IDGroupListener mListener; @@ -50,25 +44,44 @@ public abstract class AbsGroupUtil implements IUtil { private boolean isStop = false, isCancel = false; private Handler mScheduler; private SimpleSubQueue mSubQueue = SimpleSubQueue.newInstance(); - private Map mExeLoader = new WeakHashMap<>(); + private Map mExeLoader = new WeakHashMap<>(); private Map mCache = new WeakHashMap<>(); - DGTaskWrapper mGTWrapper; - GroupRunState mState; + private DGTaskWrapper mGTWrapper; + private GroupRunState mState; - AbsGroupUtil(IDGroupListener listener, DGTaskWrapper groupWrapper) { + protected AbsGroupUtil(IDGroupListener listener, DGTaskWrapper groupWrapper) { mListener = listener; mGTWrapper = groupWrapper; mUpdateInterval = Configuration.getInstance().downloadCfg.getUpdateInterval(); + mState = new GroupRunState(groupWrapper.getKey(), mListener, + groupWrapper.getSubTaskWrapper().size(), mSubQueue); + mScheduler = new Handler(Looper.getMainLooper(), SimpleSchedulers.newInstance(mState)); + initState(); + } + + /** + * 创建子任务加载器工具 + * + * @param needGetFileInfo {@code true} 需要获取文件信息。{@code false} 不需要获取文件信息 + */ + protected abstract AbsSubDLoadUtil createSubLoader(DTaskWrapper wrapper, boolean needGetFileInfo); + + protected DGTaskWrapper getWrapper() { + return mGTWrapper; + } + + protected GroupRunState getState() { + return mState; + } + + public Handler getScheduler() { + return mScheduler; } /** * 初始化组合任务状态 */ - void initState() { - mState = - new GroupRunState(mGTWrapper.getKey(), mListener, mGTWrapper.getSubTaskWrapper().size(), - mSubQueue); - mScheduler = new Handler(Looper.getMainLooper(), SimpleSchedulers.newInstance(mState)); + protected void initState() { for (DTaskWrapper wrapper : mGTWrapper.getSubTaskWrapper()) { if (wrapper.getEntity().getState() == IEntity.STATE_COMPLETE) { mState.updateCompleteNum(); @@ -85,13 +98,6 @@ public abstract class AbsGroupUtil implements IUtil { return mGTWrapper.getKey(); } - /** - * 获取任务类型 - * - * @return {@link #FTP_DIR}、{@link #HTTP_GROUP} - */ - abstract int getTaskType(); - /** * 启动子任务下载 * @@ -102,7 +108,7 @@ public abstract class AbsGroupUtil implements IUtil { if (!mState.isRunning) { startTimer(); } - SubDLoadUtil d = getDownloader(url); + AbsSubDLoadUtil d = getDownloader(url); if (d != null && !d.isRunning()) { mSubQueue.startTask(d); } @@ -115,7 +121,7 @@ public abstract class AbsGroupUtil implements IUtil { */ public void stopSubTask(String url) { if (!checkSubTask(url, "停止")) return; - SubDLoadUtil d = getDownloader(url); + AbsSubDLoadUtil d = getDownloader(url); if (d != null && d.isRunning()) { mSubQueue.stopTask(d); } @@ -147,10 +153,10 @@ public abstract class AbsGroupUtil implements IUtil { * * @param url 子任务下载地址 */ - private SubDLoadUtil getDownloader(String url) { - SubDLoadUtil d = mExeLoader.get(url); + private AbsSubDLoadUtil getDownloader(String url) { + AbsSubDLoadUtil d = mExeLoader.get(url); if (d == null) { - return createAndStartSubLoader(mCache.get(url)); + return createSubLoader(mCache.get(url), true); } return d; } @@ -164,7 +170,7 @@ public abstract class AbsGroupUtil implements IUtil { } @Override public boolean isRunning() { - return mState != null && mState.isRunning; + return mState.isRunning; } @Override public void cancel() { @@ -203,11 +209,15 @@ public abstract class AbsGroupUtil implements IUtil { } @Override public void start() { + new Thread(this).start(); + } + + @Override public void run() { if (isStop || isCancel) { closeTimer(); return; } - if (onPreStart()) { + if (onStart()) { startRunningFlow(); } } @@ -217,7 +227,8 @@ public abstract class AbsGroupUtil implements IUtil { * * @return {@code false} 将不再走后续流程,任务介绍 */ - protected boolean onPreStart() { + protected boolean onStart() { + return false; } @@ -266,21 +277,10 @@ public abstract class AbsGroupUtil implements IUtil { } /** - * 创建并启动子任务下载器 - */ - SubDLoadUtil createAndStartSubLoader(DTaskWrapper taskWrapper) { - return createAndStartSubLoader(taskWrapper, true); - } - - /** - * 创建并启动子任务下载器 - * - * @param needGetFileInfo {@code true} 需要获取文件信息。{@code false} 不需要获取文件信息 + * 启动子任务下载器 */ - SubDLoadUtil createAndStartSubLoader(DTaskWrapper taskWrapper, boolean needGetFileInfo) { - SubDLoadUtil loader = new SubDLoadUtil(mScheduler, taskWrapper, needGetFileInfo); + protected void startSubLoader(AbsSubDLoadUtil loader) { mExeLoader.put(loader.getKey(), loader); mSubQueue.startTask(loader); - return loader; } } diff --git a/PublicComponent/src/main/java/com/arialyy/aria/core/group/AbsSubDLoadUtil.java b/PublicComponent/src/main/java/com/arialyy/aria/core/group/AbsSubDLoadUtil.java new file mode 100644 index 00000000..2de7fad6 --- /dev/null +++ b/PublicComponent/src/main/java/com/arialyy/aria/core/group/AbsSubDLoadUtil.java @@ -0,0 +1,141 @@ +/* + * 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.group; + +import android.os.Handler; +import com.arialyy.aria.core.download.DTaskWrapper; +import com.arialyy.aria.core.download.DownloadEntity; +import com.arialyy.aria.core.inf.IUtil; +import com.arialyy.aria.core.listener.ISchedulers; +import com.arialyy.aria.core.loader.NormalLoader; +import com.arialyy.aria.util.CommonUtil; + +/** + * 子任务下载器,负责创建 + */ +public abstract class AbsSubDLoadUtil implements IUtil { + protected final String TAG = CommonUtil.getClassName(getClass()); + + private NormalLoader mDLoader; + private DTaskWrapper mWrapper; + private Handler mSchedulers; + private ChildDLoadListener mListener; + private boolean needGetInfo; + + /** + * @param schedulers 调度器 + * @param needGetInfo {@code true} 需要获取文件信息。{@code false} 不需要获取文件信息 + */ + protected AbsSubDLoadUtil(Handler schedulers, DTaskWrapper taskWrapper, boolean needGetInfo) { + mWrapper = taskWrapper; + mSchedulers = schedulers; + this.needGetInfo = needGetInfo; + mListener = new ChildDLoadListener(mSchedulers, AbsSubDLoadUtil.this); + mDLoader = createLoader(mListener, taskWrapper); + } + + /** + * 创建加载器 + */ + protected abstract NormalLoader createLoader(ChildDLoadListener listener, DTaskWrapper wrapper); + + protected boolean isNeedGetInfo() { + return needGetInfo; + } + + public Handler getSchedulers() { + return mSchedulers; + } + + @Override public String getKey() { + return mWrapper.getKey(); + } + + public DTaskWrapper getWrapper() { + return mWrapper; + } + + public DownloadEntity getEntity() { + return mWrapper.getEntity(); + } + + /** + * 重新开始任务 + */ + void reStart() { + if (mDLoader != null) { + mDLoader.retryTask(); + } + } + + public NormalLoader getDownloader() { + return mDLoader; + } + + @Override public long getFileSize() { + return mDLoader == null ? -1 : mDLoader.getFileSize(); + } + + @Override public long getCurrentLocation() { + return mDLoader == null ? -1 : mDLoader.getCurrentLocation(); + } + + @Override public boolean isRunning() { + return mDLoader != null && mDLoader.isRunning(); + } + + @Override public void cancel() { + if (mDLoader != null && isRunning()) { + mDLoader.cancel(); + } else { + mSchedulers.obtainMessage(ISchedulers.CANCEL, this).sendToTarget(); + } + } + + @Override public void stop() { + if (mDLoader != null && isRunning()) { + mDLoader.stop(); + } else { + mSchedulers.obtainMessage(ISchedulers.STOP, this).sendToTarget(); + } + } + + //@Override public void start() { + // if (mWrapper.getRequestType() == ITaskWrapper.D_HTTP) { + // if (needGetInfo) { + // new Thread(new HttpFileInfoThread(mWrapper, new OnFileInfoCallback() { + // + // @Override public void onComplete(String url, CompleteInfo info) { + // mDLoader = new Downloader(mListener, mWrapper); + // mDLoader.start(); + // } + // + // @Override public void onFail(AbsEntity entity, BaseException e, boolean needRetry) { + // mSchedulers.obtainMessage(ISchedulers.FAIL, SubDLoadUtil.this).sendToTarget(); + // } + // })).start(); + // } else { + // mDLoader = new Downloader(mListener, mWrapper); + // mDLoader.start(); + // } + // } else if (mWrapper.getRequestType() == ITaskWrapper.D_FTP) { + // mDLoader = new Downloader(mListener, mWrapper); + // mDLoader.start(); + // } else { + // ALog.w(TAG, String.format("不识别的类型,requestType:%s", mWrapper.getRequestType())); + // } + //} +} diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/group/ChildDLoadListener.java b/PublicComponent/src/main/java/com/arialyy/aria/core/group/ChildDLoadListener.java similarity index 91% rename from Aria/src/main/java/com/arialyy/aria/core/download/group/ChildDLoadListener.java rename to PublicComponent/src/main/java/com/arialyy/aria/core/group/ChildDLoadListener.java index 4cd0ec03..c606a78b 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/group/ChildDLoadListener.java +++ b/PublicComponent/src/main/java/com/arialyy/aria/core/group/ChildDLoadListener.java @@ -13,29 +13,28 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.arialyy.aria.core.download.group; +package com.arialyy.aria.core.group; import android.os.Handler; import com.arialyy.aria.core.download.DownloadEntity; -import com.arialyy.aria.core.inf.IDownloadListener; import com.arialyy.aria.core.inf.IEntity; -import com.arialyy.aria.core.scheduler.ISchedulers; +import com.arialyy.aria.core.listener.IDLoadListener; +import com.arialyy.aria.core.listener.ISchedulers; import com.arialyy.aria.exception.BaseException; -import com.arialyy.aria.util.ALog; import com.arialyy.aria.util.CommonUtil; /** * 子任务事件监听 */ -class ChildDLoadListener implements IDownloadListener { +public class ChildDLoadListener implements IDLoadListener { private DownloadEntity subEntity; private int RUN_SAVE_INTERVAL = 5 * 1000; //5s保存一次下载中的进度 private long lastSaveTime; private long lastLen; private Handler schedulers; - private SubDLoadUtil loader; + private AbsSubDLoadUtil loader; - ChildDLoadListener(Handler schedulers, SubDLoadUtil loader) { + ChildDLoadListener(Handler schedulers, AbsSubDLoadUtil loader) { this.loader = loader; this.schedulers = schedulers; subEntity = loader.getEntity(); @@ -141,7 +140,7 @@ class ChildDLoadListener implements IDownloadListener { * * @param state {@link ISchedulers} */ - private void sendToTarget(int state, SubDLoadUtil util) { + private void sendToTarget(int state, AbsSubDLoadUtil util) { schedulers.obtainMessage(state, util).sendToTarget(); } } \ No newline at end of file diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/group/GroupRunState.java b/PublicComponent/src/main/java/com/arialyy/aria/core/group/GroupRunState.java similarity index 79% rename from Aria/src/main/java/com/arialyy/aria/core/download/group/GroupRunState.java rename to PublicComponent/src/main/java/com/arialyy/aria/core/group/GroupRunState.java index 75616709..8d68a9a8 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/group/GroupRunState.java +++ b/PublicComponent/src/main/java/com/arialyy/aria/core/group/GroupRunState.java @@ -13,16 +13,17 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.arialyy.aria.core.download.group; +package com.arialyy.aria.core.group; -import com.arialyy.aria.core.inf.AbsTaskWrapper; +import com.arialyy.aria.core.listener.IDGroupListener; +import com.arialyy.aria.core.wrapper.AbsTaskWrapper; import java.util.HashSet; import java.util.Set; /** * 组合任务执行中的状态信息 */ -class GroupRunState { +public class GroupRunState { /** * 子任务数 */ @@ -79,6 +80,19 @@ class GroupRunState { mGroupHash = groupHash; } + /** + * 组合任务是否正在自行 + * + * @return {@code true}组合任务正在执行 + */ + public boolean isRunning() { + return isRunning; + } + + public void setRunning(boolean running) { + isRunning = running; + } + String getGroupHash() { return mGroupHash; } @@ -86,49 +100,49 @@ class GroupRunState { /** * 获取组合任务子任务数 */ - int getSubSize() { + public int getSubSize() { return mSubSize; } /** * 获取失败的数量 */ - int getFailNum() { + public int getFailNum() { return mFailNum; } /** * 获取停止的数量 */ - int getStopNum() { + public int getStopNum() { return mStopNum; } /** * 获取完成的数量 */ - int getCompleteNum() { + public int getCompleteNum() { return mCompleteNum; } /** * 获取当前组合任务总进度 */ - long getProgress() { + public long getProgress() { return mProgress; } /** * 更新完成的数量,mCompleteNum + 1 */ - void updateCompleteNum() { + public void updateCompleteNum() { mCompleteNum++; } /** * 更新任务进度 */ - void updateProgress(long newProgress) { + public void updateProgress(long newProgress) { this.mProgress = newProgress; } @@ -137,7 +151,7 @@ class GroupRunState { * * @param key {@link AbsTaskWrapper#getKey()} */ - void updateCount(String key) { + public void updateCount(String key) { if (mFailTemp.contains(key)) { mFailTemp.remove(key); mFailNum--; @@ -152,7 +166,7 @@ class GroupRunState { * * @param key {@link AbsTaskWrapper#getKey()} */ - void countStopNum(String key) { + public void countStopNum(String key) { mStopTemp.add(key); mStopNum++; } @@ -162,7 +176,7 @@ class GroupRunState { * * @param key {@link AbsTaskWrapper#getKey()} */ - void countFailNum(String key) { + public void countFailNum(String key) { mFailTemp.add(key); mFailNum++; } diff --git a/Aria/src/main/java/com/arialyy/aria/core/inf/GroupSendParams.java b/PublicComponent/src/main/java/com/arialyy/aria/core/group/GroupSendParams.java similarity index 87% rename from Aria/src/main/java/com/arialyy/aria/core/inf/GroupSendParams.java rename to PublicComponent/src/main/java/com/arialyy/aria/core/group/GroupSendParams.java index 2a977b9e..82a157ec 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/inf/GroupSendParams.java +++ b/PublicComponent/src/main/java/com/arialyy/aria/core/group/GroupSendParams.java @@ -13,7 +13,10 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.arialyy.aria.core.inf; +package com.arialyy.aria.core.group; + +import com.arialyy.aria.core.common.AbsNormalEntity; +import com.arialyy.aria.core.task.AbsGroupTask; /** * Created by lyy on 2017/9/8. diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/group/ISubQueue.java b/PublicComponent/src/main/java/com/arialyy/aria/core/group/ISubQueue.java similarity index 91% rename from Aria/src/main/java/com/arialyy/aria/core/download/group/ISubQueue.java rename to PublicComponent/src/main/java/com/arialyy/aria/core/group/ISubQueue.java index fab093dd..3019b935 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/group/ISubQueue.java +++ b/PublicComponent/src/main/java/com/arialyy/aria/core/group/ISubQueue.java @@ -13,16 +13,16 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.arialyy.aria.core.download.group; +package com.arialyy.aria.core.group; -import com.arialyy.aria.core.common.AbsFileer; -import com.arialyy.aria.core.common.IUtil; +import com.arialyy.aria.core.loader.AbsLoader; +import com.arialyy.aria.core.inf.IUtil; import com.arialyy.aria.core.config.DGroupConfig; /** * 组合任务子任务队列 * - * @param {@link AbsFileer}下载器 + * @param {@link AbsLoader}下载器 */ interface ISubQueue { diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/group/SimpleSchedulers.java b/PublicComponent/src/main/java/com/arialyy/aria/core/group/SimpleSchedulers.java similarity index 92% rename from Aria/src/main/java/com/arialyy/aria/core/download/group/SimpleSchedulers.java rename to PublicComponent/src/main/java/com/arialyy/aria/core/group/SimpleSchedulers.java index 59633f6d..f4bbf9df 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/group/SimpleSchedulers.java +++ b/PublicComponent/src/main/java/com/arialyy/aria/core/group/SimpleSchedulers.java @@ -14,14 +14,14 @@ * limitations under the License. */ -package com.arialyy.aria.core.download.group; +package com.arialyy.aria.core.group; import android.os.Message; -import com.arialyy.aria.core.AriaManager; +import com.arialyy.aria.core.AriaConfig; +import com.arialyy.aria.core.common.AbsEntity; import com.arialyy.aria.core.config.Configuration; -import com.arialyy.aria.core.inf.AbsEntity; +import com.arialyy.aria.core.listener.ISchedulers; import com.arialyy.aria.core.manager.ThreadTaskManager; -import com.arialyy.aria.core.scheduler.ISchedulers; import com.arialyy.aria.exception.TaskException; import com.arialyy.aria.util.ALog; import com.arialyy.aria.util.NetUtils; @@ -48,7 +48,7 @@ class SimpleSchedulers implements ISchedulers { } @Override public boolean handleMessage(Message msg) { - SubDLoadUtil loader = (SubDLoadUtil) msg.obj; + AbsSubDLoadUtil loader = (AbsSubDLoadUtil) msg.obj; switch (msg.what) { case RUNNING: mGState.listener.onSubRunning(loader.getEntity()); @@ -79,7 +79,7 @@ class SimpleSchedulers implements ISchedulers { * 2、stopNum + failNum + completeNum + cacheNum == subSize,则认为组合任务停止 * 3、failNum == subSize,只有全部的子任务都失败了,才能任务组合任务失败 */ - private synchronized void handleFail(final SubDLoadUtil loaderUtil) { + private synchronized void handleFail(final AbsSubDLoadUtil loaderUtil) { Configuration config = Configuration.getInstance(); long interval = config.dGroupCfg.getSubReTryInterval(); @@ -87,7 +87,7 @@ class SimpleSchedulers implements ISchedulers { boolean isNotNetRetry = config.appCfg.isNotNetRetry(); final int reTryNum = num; - if ((!NetUtils.isConnected(AriaManager.getInstance().getAPP()) && !isNotNetRetry) + if ((!NetUtils.isConnected(AriaConfig.getInstance().getAPP()) && !isNotNetRetry) || loaderUtil.getDownloader() == null // 如果获取不到文件信息,loader为空 || loaderUtil.getEntity().getFailNum() > reTryNum) { mQueue.removeTaskFromExecQ(loaderUtil); @@ -128,7 +128,7 @@ class SimpleSchedulers implements ISchedulers { * 1、所有的子任务已经停止,则认为组合任务停止 * 2、completeNum + failNum + stopNum = subSize,则认为组合任务停止 */ - private synchronized void handleStop(SubDLoadUtil loader) { + private synchronized void handleStop(AbsSubDLoadUtil loader) { mGState.listener.onSubStop(loader.getEntity()); mGState.countStopNum(loader.getKey()); if (mGState.getStopNum() == mGState.getSubSize() @@ -153,7 +153,7 @@ class SimpleSchedulers implements ISchedulers { * GroupRunState#getFailNum()}不为0,则认为组合任务被停止 * 3、只有有缓存的子任务,则任务组合任务没有完成 */ - private synchronized void handleComplete(SubDLoadUtil loader) { + private synchronized void handleComplete(AbsSubDLoadUtil loader) { ALog.d(TAG, String.format("子任务【%s】完成", loader.getEntity().getFileName())); ThreadTaskManager.getInstance().removeTaskThread(loader.getKey()); mGState.listener.onSubComplete(loader.getEntity()); @@ -182,7 +182,7 @@ class SimpleSchedulers implements ISchedulers { if (mQueue.isStopAll()) { return; } - SubDLoadUtil next = mQueue.getNextTask(); + AbsSubDLoadUtil next = mQueue.getNextTask(); if (next != null) { ALog.d(TAG, String.format("启动任务:%s", next.getEntity().getFileName())); mQueue.startTask(next); diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/group/SimpleSubQueue.java b/PublicComponent/src/main/java/com/arialyy/aria/core/group/SimpleSubQueue.java similarity index 81% rename from Aria/src/main/java/com/arialyy/aria/core/download/group/SimpleSubQueue.java rename to PublicComponent/src/main/java/com/arialyy/aria/core/group/SimpleSubQueue.java index f004345b..c4f87b70 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/group/SimpleSubQueue.java +++ b/PublicComponent/src/main/java/com/arialyy/aria/core/group/SimpleSubQueue.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.arialyy.aria.core.download.group; +package com.arialyy.aria.core.group; import com.arialyy.aria.core.config.Configuration; import com.arialyy.aria.util.ALog; @@ -28,16 +28,16 @@ import java.util.Set; /** * 组合任务队列,该队列生命周期和{@link AbsGroupUtil}生命周期一致 */ -class SimpleSubQueue implements ISubQueue { +class SimpleSubQueue implements ISubQueue { private static final String TAG = "SimpleSubQueue"; /** * 缓存下载器 */ - private Map mCache = new LinkedHashMap<>(); + private Map mCache = new LinkedHashMap<>(); /** * 执行中的下载器 */ - private Map mExec = new LinkedHashMap<>(); + private Map mExec = new LinkedHashMap<>(); /** * 最大执行任务数 @@ -57,7 +57,7 @@ class SimpleSubQueue implements ISubQueue { return new SimpleSubQueue(); } - Map getExec() { + Map getExec() { return mExec; } @@ -72,11 +72,11 @@ class SimpleSubQueue implements ISubQueue { return isStopAll; } - @Override public void addTask(SubDLoadUtil fileer) { + @Override public void addTask(AbsSubDLoadUtil fileer) { mCache.put(fileer.getKey(), fileer); } - @Override public void startTask(SubDLoadUtil fileer) { + @Override public void startTask(AbsSubDLoadUtil fileer) { if (mExec.size() < mExecSize) { mCache.remove(fileer.getKey()); mExec.put(fileer.getKey(), fileer); @@ -88,7 +88,7 @@ class SimpleSubQueue implements ISubQueue { } } - @Override public void stopTask(SubDLoadUtil fileer) { + @Override public void stopTask(AbsSubDLoadUtil fileer) { fileer.stop(); mExec.remove(fileer.getKey()); } @@ -98,7 +98,7 @@ class SimpleSubQueue implements ISubQueue { ALog.d(TAG, "停止组合任务"); Set keys = mExec.keySet(); for (String key : keys) { - SubDLoadUtil loader = mExec.get(key); + AbsSubDLoadUtil loader = mExec.get(key); if (loader != null) { ALog.d(TAG, String.format("停止子任务:%s", loader.getEntity().getFileName())); loader.stop(); @@ -122,7 +122,7 @@ class SimpleSubQueue implements ISubQueue { if (oldSize < num) { // 处理队列变小的情况,该情况下将停止队尾任务,并将这些任务添加到缓存队列中 if (mExec.size() > num) { Set keys = mExec.keySet(); - List caches = new ArrayList<>(); + List caches = new ArrayList<>(); int i = 0; for (String key : keys) { if (i > num) { @@ -130,19 +130,19 @@ class SimpleSubQueue implements ISubQueue { } i++; } - Collection temp = mCache.values(); + Collection temp = mCache.values(); mCache.clear(); - for (SubDLoadUtil cache : caches) { + for (AbsSubDLoadUtil cache : caches) { addTask(cache); } - for (SubDLoadUtil t : temp) { + for (AbsSubDLoadUtil t : temp) { addTask(t); } } } else { // 处理队列变大的情况,该情况下将增加任务 if (mExec.size() < num) { for (int i = 0; i < diff; i++) { - SubDLoadUtil next = getNextTask(); + AbsSubDLoadUtil next = getNextTask(); if (next != null) { startTask(next); } else { @@ -153,7 +153,7 @@ class SimpleSubQueue implements ISubQueue { } } - @Override public void removeTaskFromExecQ(SubDLoadUtil fileer) { + @Override public void removeTaskFromExecQ(AbsSubDLoadUtil fileer) { if (mExec.containsKey(fileer.getKey())) { if (fileer.isRunning()) { fileer.stop(); @@ -162,7 +162,7 @@ class SimpleSubQueue implements ISubQueue { } } - @Override public void removeTask(SubDLoadUtil fileer) { + @Override public void removeTask(AbsSubDLoadUtil fileer) { removeTaskFromExecQ(fileer); mCache.remove(fileer.getKey()); } @@ -171,7 +171,7 @@ class SimpleSubQueue implements ISubQueue { ALog.d(TAG, "删除组合任务"); Set keys = mExec.keySet(); for (String key : keys) { - SubDLoadUtil loader = mExec.get(key); + AbsSubDLoadUtil loader = mExec.get(key); if (loader != null) { ALog.d(TAG, String.format("停止子任务:%s", loader.getEntity().getFileName())); loader.cancel(); @@ -179,7 +179,7 @@ class SimpleSubQueue implements ISubQueue { } } - @Override public SubDLoadUtil getNextTask() { + @Override public AbsSubDLoadUtil getNextTask() { Iterator keys = mCache.keySet().iterator(); if (keys.hasNext()) { return mCache.get(keys.next()); diff --git a/PublicComponent/src/main/java/com/arialyy/aria/core/inf/EventFlag.java b/PublicComponent/src/main/java/com/arialyy/aria/core/inf/EventFlag.java new file mode 100644 index 00000000..689cb365 --- /dev/null +++ b/PublicComponent/src/main/java/com/arialyy/aria/core/inf/EventFlag.java @@ -0,0 +1,26 @@ +/* + * Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.arialyy.aria.core.inf; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Target(ElementType.FIELD) +@Retention(RetentionPolicy.RUNTIME) +public @interface EventFlag { +} diff --git a/Aria/src/main/java/com/arialyy/aria/core/inf/IEntity.java b/PublicComponent/src/main/java/com/arialyy/aria/core/inf/IEntity.java similarity index 100% rename from Aria/src/main/java/com/arialyy/aria/core/inf/IEntity.java rename to PublicComponent/src/main/java/com/arialyy/aria/core/inf/IEntity.java diff --git a/PublicComponent/src/main/java/com/arialyy/aria/core/inf/IEventHandler.java b/PublicComponent/src/main/java/com/arialyy/aria/core/inf/IEventHandler.java new file mode 100644 index 00000000..560e8410 --- /dev/null +++ b/PublicComponent/src/main/java/com/arialyy/aria/core/inf/IEventHandler.java @@ -0,0 +1,25 @@ +/* + * Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.arialyy.aria.core.inf; + +/** + * 事件处理对象 + * + * @Author lyy + * @Date 2019-09-10 + */ +public interface IEventHandler { +} diff --git a/PublicComponent/src/main/java/com/arialyy/aria/core/inf/IOptionConstant.java b/PublicComponent/src/main/java/com/arialyy/aria/core/inf/IOptionConstant.java new file mode 100644 index 00000000..e63ef70b --- /dev/null +++ b/PublicComponent/src/main/java/com/arialyy/aria/core/inf/IOptionConstant.java @@ -0,0 +1,52 @@ +/* + * Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.arialyy.aria.core.inf; + +/** + * @Author lyy + * @Date 2019-09-10 + */ +public interface IOptionConstant { + // ftp 任务设置常量 + String ftpUrlEntity = "urlEntity"; + String charSet = "charSet"; + String clientConfig = "clientConfig"; + String uploadInterceptor = "uploadInterceptor"; + + // http + String useServerFileName = "useServerFileName"; + String requestEnum = "requestEnum"; + String fileLenAdapter = "fileLenAdapter"; + String params = "params"; + String formFields = "formFields"; + String headers = "headers"; + String proxy = "proxy"; + + // m3u8 vod + String bandWidth = "bandWidth"; + String cacheDir = "cacheDir"; + String generateIndexFileTemp = "generateIndexFileTemp"; + String bandWidthUrlConverter = "bandWidthUrlConverter"; + String mergeFile = "mergeFile"; + String mergeHandler = "mergeHandler"; + String vodUrlConverter = "vodUrlConverter"; + String maxTsQueueNum = "maxTsQueueNum"; + String jumpIndex = "jumpIndex"; + + // m3u8 live + String liveTsUrlConverter = "liveTsUrlConverter"; + String liveUpdateInterval = "liveUpdateInterval"; +} diff --git a/PublicComponent/src/main/java/com/arialyy/aria/core/inf/IRecordHandler.java b/PublicComponent/src/main/java/com/arialyy/aria/core/inf/IRecordHandler.java new file mode 100644 index 00000000..17e75de3 --- /dev/null +++ b/PublicComponent/src/main/java/com/arialyy/aria/core/inf/IRecordHandler.java @@ -0,0 +1,47 @@ +/* + * Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.arialyy.aria.core.inf; + +import com.arialyy.aria.core.TaskRecord; + +/** + * @Author lyy + * @Date 2019-09-18 + */ +public interface IRecordHandler { + + int TYPE_DOWNLOAD = 1; + int TYPE_UPLOAD = 2; + int TYPE_M3U8_VOD = 3; + int TYPE_M3U8_LIVE = 4; + + String STATE = "_state_"; + String RECORD = "_record_"; + /** + * 小于1m的文件不启用多线程 + */ + long SUB_LEN = 1024 * 1024; + + /** + * 分块文件路径 + */ + String SUB_PATH = "%s.%s.part"; + + /** + * 获取任务记录 + */ + TaskRecord getRecord(); +} diff --git a/PublicComponent/src/main/java/com/arialyy/aria/core/inf/IRecordHandlerAdapter.java b/PublicComponent/src/main/java/com/arialyy/aria/core/inf/IRecordHandlerAdapter.java new file mode 100644 index 00000000..85e75f1b --- /dev/null +++ b/PublicComponent/src/main/java/com/arialyy/aria/core/inf/IRecordHandlerAdapter.java @@ -0,0 +1,55 @@ +/* + * Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.arialyy.aria.core.inf; + +import com.arialyy.aria.core.TaskRecord; +import com.arialyy.aria.core.ThreadRecord; + +/** + * 任务记录处理适配器 + * + * @Author lyy + * @Date 2019-09-19 + */ +public interface IRecordHandlerAdapter { + + /** + * 处理任务记录 + */ + void handlerTaskRecord(TaskRecord record); + + /** + * 处理线程任务 + * + * @param record 任务记录 + * @param threadId 线程id + * @param startL 线程开始位置 + * @param endL 线程结束位置 + */ + ThreadRecord createThreadRecord(TaskRecord record, int threadId, long startL, long endL); + + /** + * 新任务创建任务记录 + */ + TaskRecord createTaskRecord(int threadNum); + + /** + * 配置新任务的线程数 + * + * @return 新任务的线程数 + */ + int initTaskThreadNum(); +} diff --git a/Aria/src/main/java/com/arialyy/aria/core/inf/ITaskConfig.java b/PublicComponent/src/main/java/com/arialyy/aria/core/inf/ITaskOption.java similarity index 95% rename from Aria/src/main/java/com/arialyy/aria/core/inf/ITaskConfig.java rename to PublicComponent/src/main/java/com/arialyy/aria/core/inf/ITaskOption.java index 72ab22c4..5929eb8a 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/inf/ITaskConfig.java +++ b/PublicComponent/src/main/java/com/arialyy/aria/core/inf/ITaskOption.java @@ -19,6 +19,6 @@ package com.arialyy.aria.core.inf; * Created by AriaL on 2017/6/29. * 任务信息设置接口 */ -public interface ITaskConfig { +public interface ITaskOption { } diff --git a/Aria/src/main/java/com/arialyy/aria/core/common/IThreadState.java b/PublicComponent/src/main/java/com/arialyy/aria/core/inf/IThreadState.java similarity index 97% rename from Aria/src/main/java/com/arialyy/aria/core/common/IThreadState.java rename to PublicComponent/src/main/java/com/arialyy/aria/core/inf/IThreadState.java index 996e6108..b48bbc47 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/common/IThreadState.java +++ b/PublicComponent/src/main/java/com/arialyy/aria/core/inf/IThreadState.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.arialyy.aria.core.common; +package com.arialyy.aria.core.inf; import android.os.Handler; diff --git a/Aria/src/main/java/com/arialyy/aria/core/common/IUtil.java b/PublicComponent/src/main/java/com/arialyy/aria/core/inf/IUtil.java similarity index 97% rename from Aria/src/main/java/com/arialyy/aria/core/common/IUtil.java rename to PublicComponent/src/main/java/com/arialyy/aria/core/inf/IUtil.java index 23c6d007..a2f29a92 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/common/IUtil.java +++ b/PublicComponent/src/main/java/com/arialyy/aria/core/inf/IUtil.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package com.arialyy.aria.core.common; +package com.arialyy.aria.core.inf; import com.arialyy.aria.core.download.DownloadEntity; import com.arialyy.aria.core.download.DownloadGroupEntity; diff --git a/Aria/src/main/java/com/arialyy/aria/core/common/OnFileInfoCallback.java b/PublicComponent/src/main/java/com/arialyy/aria/core/inf/OnFileInfoCallback.java similarity index 87% rename from Aria/src/main/java/com/arialyy/aria/core/common/OnFileInfoCallback.java rename to PublicComponent/src/main/java/com/arialyy/aria/core/inf/OnFileInfoCallback.java index 88fa77a9..71eccce9 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/common/OnFileInfoCallback.java +++ b/PublicComponent/src/main/java/com/arialyy/aria/core/inf/OnFileInfoCallback.java @@ -13,9 +13,10 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.arialyy.aria.core.common; +package com.arialyy.aria.core.inf; -import com.arialyy.aria.core.inf.AbsEntity; +import com.arialyy.aria.core.common.CompleteInfo; +import com.arialyy.aria.core.common.AbsEntity; import com.arialyy.aria.exception.BaseException; public interface OnFileInfoCallback { diff --git a/Aria/src/main/java/com/arialyy/aria/core/common/Suggest.java b/PublicComponent/src/main/java/com/arialyy/aria/core/inf/Suggest.java similarity index 95% rename from Aria/src/main/java/com/arialyy/aria/core/common/Suggest.java rename to PublicComponent/src/main/java/com/arialyy/aria/core/inf/Suggest.java index b9c56fb6..86cf1466 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/common/Suggest.java +++ b/PublicComponent/src/main/java/com/arialyy/aria/core/inf/Suggest.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.arialyy.aria.core.common; +package com.arialyy.aria.core.inf; public interface Suggest { diff --git a/Aria/src/main/java/com/arialyy/aria/core/inf/TaskSchedulerType.java b/PublicComponent/src/main/java/com/arialyy/aria/core/inf/TaskSchedulerType.java similarity index 100% rename from Aria/src/main/java/com/arialyy/aria/core/inf/TaskSchedulerType.java rename to PublicComponent/src/main/java/com/arialyy/aria/core/inf/TaskSchedulerType.java diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/BaseDListener.java b/PublicComponent/src/main/java/com/arialyy/aria/core/listener/BaseDListener.java similarity index 73% rename from Aria/src/main/java/com/arialyy/aria/core/download/BaseDListener.java rename to PublicComponent/src/main/java/com/arialyy/aria/core/listener/BaseDListener.java index fa1ee9a8..fa9b6a51 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/BaseDListener.java +++ b/PublicComponent/src/main/java/com/arialyy/aria/core/listener/BaseDListener.java @@ -13,28 +13,25 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.arialyy.aria.core.download; +package com.arialyy.aria.core.listener; import android.os.Handler; - -import com.arialyy.aria.core.common.BaseListener; -import com.arialyy.aria.core.common.RecordHandler; -import com.arialyy.aria.core.inf.AbsTaskWrapper; -import com.arialyy.aria.core.inf.IDownloadListener; +import com.arialyy.aria.core.download.DTaskWrapper; +import com.arialyy.aria.core.download.DownloadEntity; import com.arialyy.aria.core.inf.IEntity; +import com.arialyy.aria.core.inf.IRecordHandler; import com.arialyy.aria.core.inf.TaskSchedulerType; -import com.arialyy.aria.core.scheduler.ISchedulers; +import com.arialyy.aria.core.task.AbsTask; import com.arialyy.aria.util.CommonUtil; import com.arialyy.aria.util.RecordUtil; -import java.io.File; /** * 下载监听类 */ -public class BaseDListener extends BaseListener - implements IDownloadListener { +public class BaseDListener extends BaseListener> + implements IDLoadListener { - BaseDListener(DownloadTask task, Handler outHandler) { + public BaseDListener(AbsTask task, Handler outHandler) { super(task, outHandler); } @@ -58,10 +55,10 @@ public class BaseDListener extends BaseListener +class BaseUListener extends BaseListener> implements IUploadListener { - BaseUListener(UploadTask task, Handler outHandler) { + BaseUListener(AbsTask task, Handler outHandler) { super(task, outHandler); } @@ -38,10 +39,10 @@ class BaseUListener extends BaseListener if (sType == TaskSchedulerType.TYPE_CANCEL_AND_NOT_NOTIFY) { mEntity.setComplete(false); mEntity.setState(IEntity.STATE_WAIT); - RecordUtil.delTaskRecord(mEntity.getFilePath(), RecordHandler.TYPE_UPLOAD, + RecordUtil.delTaskRecord(mEntity.getFilePath(), IRecordHandler.TYPE_UPLOAD, mTaskWrapper.isRemoveFile(), false); } else { - RecordUtil.delTaskRecord(mEntity.getFilePath(), RecordHandler.TYPE_UPLOAD, + RecordUtil.delTaskRecord(mEntity.getFilePath(), IRecordHandler.TYPE_UPLOAD, mTaskWrapper.isRemoveFile(), true); } } diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadGroupListener.java b/PublicComponent/src/main/java/com/arialyy/aria/core/listener/DownloadGroupListener.java similarity index 90% rename from Aria/src/main/java/com/arialyy/aria/core/download/DownloadGroupListener.java rename to PublicComponent/src/main/java/com/arialyy/aria/core/listener/DownloadGroupListener.java index a5afbe57..6d882148 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadGroupListener.java +++ b/PublicComponent/src/main/java/com/arialyy/aria/core/listener/DownloadGroupListener.java @@ -13,16 +13,17 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.arialyy.aria.core.download; +package com.arialyy.aria.core.listener; import android.os.Handler; -import com.arialyy.aria.core.common.BaseListener; -import com.arialyy.aria.core.common.RecordHandler; -import com.arialyy.aria.core.download.group.IDGroupListener; -import com.arialyy.aria.core.inf.GroupSendParams; +import com.arialyy.aria.core.download.DGTaskWrapper; +import com.arialyy.aria.core.download.DownloadEntity; +import com.arialyy.aria.core.download.DownloadGroupEntity; +import com.arialyy.aria.core.group.GroupSendParams; import com.arialyy.aria.core.inf.IEntity; +import com.arialyy.aria.core.inf.IRecordHandler; import com.arialyy.aria.core.inf.TaskSchedulerType; -import com.arialyy.aria.core.scheduler.ISchedulers; +import com.arialyy.aria.core.task.DownloadGroupTask; import com.arialyy.aria.exception.BaseException; import com.arialyy.aria.util.ALog; import com.arialyy.aria.util.CommonUtil; @@ -32,12 +33,12 @@ import com.arialyy.aria.util.RecordUtil; /** * Created by Aria.Lao on 2017/7/20. 任务组下载事件 */ -class DownloadGroupListener +public class DownloadGroupListener extends BaseListener implements IDGroupListener { private GroupSendParams mSeedEntity; - DownloadGroupListener(DownloadGroupTask task, Handler outHandler) { + public DownloadGroupListener(DownloadGroupTask task, Handler outHandler) { super(task, outHandler); mSeedEntity = new GroupSendParams<>(); mSeedEntity.groupTask = task; @@ -125,7 +126,7 @@ class DownloadGroupListener subEntity.setConvertSpeed("0kb/s"); subEntity.setSpeed(0); ALog.i(TAG, String.format("任务【%s】完成,将删除线程任务记录", mEntity.getKey())); - RecordUtil.delTaskRecord(subEntity.getFilePath(), RecordHandler.TYPE_DOWNLOAD, false, false); + RecordUtil.delTaskRecord(subEntity.getFilePath(), IRecordHandler.TYPE_DOWNLOAD, false, false); } subEntity.update(); } diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/group/IDGroupListener.java b/PublicComponent/src/main/java/com/arialyy/aria/core/listener/IDGroupListener.java similarity index 90% rename from Aria/src/main/java/com/arialyy/aria/core/download/group/IDGroupListener.java rename to PublicComponent/src/main/java/com/arialyy/aria/core/listener/IDGroupListener.java index 918c45f6..eca54860 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/group/IDGroupListener.java +++ b/PublicComponent/src/main/java/com/arialyy/aria/core/listener/IDGroupListener.java @@ -13,17 +13,17 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.arialyy.aria.core.download.group; +package com.arialyy.aria.core.listener; import com.arialyy.aria.core.download.DownloadEntity; -import com.arialyy.aria.core.inf.IDownloadListener; +import com.arialyy.aria.core.listener.IDLoadListener; import com.arialyy.aria.exception.BaseException; /** * Created by Aria.Lao on 2017/7/20. * 下载任务组事件 */ -public interface IDGroupListener extends IDownloadListener { +public interface IDGroupListener extends IDLoadListener { /** * 子任务预处理 diff --git a/Aria/src/main/java/com/arialyy/aria/core/inf/IDownloadListener.java b/PublicComponent/src/main/java/com/arialyy/aria/core/listener/IDLoadListener.java similarity index 90% rename from Aria/src/main/java/com/arialyy/aria/core/inf/IDownloadListener.java rename to PublicComponent/src/main/java/com/arialyy/aria/core/listener/IDLoadListener.java index d5cec1df..fc93f9e4 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/inf/IDownloadListener.java +++ b/PublicComponent/src/main/java/com/arialyy/aria/core/listener/IDLoadListener.java @@ -14,12 +14,12 @@ * limitations under the License. */ -package com.arialyy.aria.core.inf; +package com.arialyy.aria.core.listener; /** * 下载监听 */ -public interface IDownloadListener extends IEventListener { +public interface IDLoadListener extends IEventListener { /** * 预处理完成,准备下载---开始下载之间 diff --git a/Aria/src/main/java/com/arialyy/aria/core/inf/IEventListener.java b/PublicComponent/src/main/java/com/arialyy/aria/core/listener/IEventListener.java similarity index 97% rename from Aria/src/main/java/com/arialyy/aria/core/inf/IEventListener.java rename to PublicComponent/src/main/java/com/arialyy/aria/core/listener/IEventListener.java index 2691797b..da772820 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/inf/IEventListener.java +++ b/PublicComponent/src/main/java/com/arialyy/aria/core/listener/IEventListener.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.arialyy.aria.core.inf; +package com.arialyy.aria.core.listener; import com.arialyy.aria.exception.BaseException; diff --git a/Aria/src/main/java/com/arialyy/aria/core/scheduler/ISchedulers.java b/PublicComponent/src/main/java/com/arialyy/aria/core/listener/ISchedulers.java similarity index 97% rename from Aria/src/main/java/com/arialyy/aria/core/scheduler/ISchedulers.java rename to PublicComponent/src/main/java/com/arialyy/aria/core/listener/ISchedulers.java index 987e5125..75755d7d 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/scheduler/ISchedulers.java +++ b/PublicComponent/src/main/java/com/arialyy/aria/core/listener/ISchedulers.java @@ -14,12 +14,12 @@ * limitations under the License. */ -package com.arialyy.aria.core.scheduler; +package com.arialyy.aria.core.listener; import android.os.Handler; import com.arialyy.aria.core.download.DownloadEntity; import com.arialyy.aria.core.download.DownloadGroupEntity; -import com.arialyy.aria.core.inf.ITask; +import com.arialyy.aria.core.task.ITask; import com.arialyy.aria.core.upload.UploadEntity; /** diff --git a/Aria/src/main/java/com/arialyy/aria/core/inf/IUploadListener.java b/PublicComponent/src/main/java/com/arialyy/aria/core/listener/IUploadListener.java similarity index 94% rename from Aria/src/main/java/com/arialyy/aria/core/inf/IUploadListener.java rename to PublicComponent/src/main/java/com/arialyy/aria/core/listener/IUploadListener.java index e6196375..b571f37c 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/inf/IUploadListener.java +++ b/PublicComponent/src/main/java/com/arialyy/aria/core/listener/IUploadListener.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.arialyy.aria.core.inf; +package com.arialyy.aria.core.listener; /** * Created by lyy on 2017/2/9. diff --git a/Aria/src/main/java/com/arialyy/aria/core/upload/UploadListener.java b/PublicComponent/src/main/java/com/arialyy/aria/core/listener/UploadListener.java similarity index 93% rename from Aria/src/main/java/com/arialyy/aria/core/upload/UploadListener.java rename to PublicComponent/src/main/java/com/arialyy/aria/core/listener/UploadListener.java index c37416c2..2cd1f0db 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/upload/UploadListener.java +++ b/PublicComponent/src/main/java/com/arialyy/aria/core/listener/UploadListener.java @@ -13,9 +13,8 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.arialyy.aria.core.upload; +package com.arialyy.aria.core.listener; -import com.arialyy.aria.core.inf.IUploadListener; import com.arialyy.aria.exception.BaseException; /** diff --git a/Aria/src/main/java/com/arialyy/aria/core/common/AbsFileer.java b/PublicComponent/src/main/java/com/arialyy/aria/core/loader/AbsLoader.java similarity index 82% rename from Aria/src/main/java/com/arialyy/aria/core/common/AbsFileer.java rename to PublicComponent/src/main/java/com/arialyy/aria/core/loader/AbsLoader.java index c6988ceb..c74e8c58 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/common/AbsFileer.java +++ b/PublicComponent/src/main/java/com/arialyy/aria/core/loader/AbsLoader.java @@ -13,14 +13,17 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.arialyy.aria.core.common; +package com.arialyy.aria.core.loader; import android.os.Looper; import android.util.SparseArray; -import com.arialyy.aria.core.inf.AbsNormalEntity; -import com.arialyy.aria.core.inf.AbsTaskWrapper; -import com.arialyy.aria.core.inf.IEventListener; +import com.arialyy.aria.core.TaskRecord; +import com.arialyy.aria.core.inf.IRecordHandler; +import com.arialyy.aria.core.inf.IThreadState; +import com.arialyy.aria.core.listener.IEventListener; import com.arialyy.aria.core.manager.ThreadTaskManager; +import com.arialyy.aria.core.task.IThreadTask; +import com.arialyy.aria.core.wrapper.AbsTaskWrapper; import com.arialyy.aria.util.ALog; import com.arialyy.aria.util.CommonUtil; import java.io.File; @@ -31,15 +34,13 @@ import java.util.concurrent.TimeUnit; * Created by AriaL on 2017/7/1. * 控制线程任务状态,如:开始,停止,取消,重试 */ -public abstract class AbsFileer> - implements Runnable { +public abstract class AbsLoader implements Runnable { protected final String TAG; protected IEventListener mListener; - protected TASK_WRAPPER mTaskWrapper; - protected ENTITY mEntity; + protected AbsTaskWrapper mTaskWrapper; protected File mTempFile; - private SparseArray mTask = new SparseArray<>(); + private SparseArray mTask = new SparseArray<>(); private ScheduledThreadPoolExecutor mTimer; /** @@ -51,29 +52,38 @@ public abstract class AbsFileer getTaskList() { + public SparseArray getTaskList() { return mTask; } @@ -90,6 +100,11 @@ public abstract class AbsFileer> - extends AbsFileer { +/** + * 单文件 + */ +public class NormalLoader extends AbsLoader { private ThreadStateManager mStateManager; private Handler mStateHandler; protected int mTotalThreadNum; //总线程数 private int mStartThreadNum; //启动的线程数 + private ILoaderAdapter mAdapter; - protected NormalFileer(IEventListener listener, TASK_WRAPPER wrapper) { + public NormalLoader(IEventListener listener, AbsTaskWrapper wrapper) { super(listener, wrapper); + mTempFile = new File(getEntity().getFilePath()); EventMsgUtil.getDefault().register(this); + setUpdateInterval(wrapper.getConfig().getUpdateInterval()); } - /** - * 处理新任务 - * - * @return {@code true}创建新任务成功 - */ - protected abstract boolean handleNewTask(); + public void setAdapter(ILoaderAdapter adapter) { + mAdapter = adapter; + } - /** - * 选择单任务线程的类型 - */ - protected abstract AbsThreadTask selectThreadTask(SubThreadConfig config); + public AbsNormalEntity getEntity() { + return (AbsNormalEntity) mTaskWrapper.getEntity(); + } + + @Override public long getFileSize() { + return getEntity().getFileSize(); + } + + @Override public long getCurrentLocation() { + return isRunning() ? mStateManager.getCurrentProgress() : getEntity().getCurrentProgress(); + } + + @Override protected IRecordHandler getRecordHandler(AbsTaskWrapper wrapper) { + return mAdapter.recordHandler(wrapper); + } /** * 设置最大下载/上传速度 @@ -57,7 +76,7 @@ public abstract class NormalFileer 0) { task.setMaxSpeed(maxSpeed / mStartThreadNum); } @@ -71,10 +90,31 @@ public abstract class NormalFileer config = new SubThreadConfig<>(); - config.url = mEntity.isRedirect() ? mEntity.getRedirectUrl() : mEntity.getUrl(); + private IThreadTask createSingThreadTask(ThreadRecord record, int startNum) { + SubThreadConfig config = new SubThreadConfig(); + config.url = getEntity().isRedirect() ? getEntity().getRedirectUrl() : getEntity().getUrl(); config.tempFile = mRecord.isBlock ? new File( - String.format(RecordHandler.SUB_PATH, mTempFile.getPath(), record.threadId)) + String.format(IRecordHandler.SUB_PATH, mTempFile.getPath(), record.threadId)) : mTempFile; config.isBlock = mRecord.isBlock; config.isOpenDynamicFile = mRecord.isOpenDynamicFile; @@ -107,16 +147,18 @@ public abstract class NormalFileer partPath = new ArrayList<>(); for (int i = 0, len = mTaskRecord.threadNum; i < len; i++) { - partPath.add(String.format(RecordHandler.SUB_PATH, mTaskRecord.filePath, i)); + partPath.add(String.format(IRecordHandler.SUB_PATH, mTaskRecord.filePath, i)); } boolean isSuccess = FileUtil.mergeFile(mTaskRecord.filePath, partPath); if (isSuccess) { diff --git a/Aria/src/main/java/com/arialyy/aria/core/manager/ThreadTaskManager.java b/PublicComponent/src/main/java/com/arialyy/aria/core/manager/ThreadTaskManager.java similarity index 92% rename from Aria/src/main/java/com/arialyy/aria/core/manager/ThreadTaskManager.java rename to PublicComponent/src/main/java/com/arialyy/aria/core/manager/ThreadTaskManager.java index 7d4ad2d0..75a28cbd 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/manager/ThreadTaskManager.java +++ b/PublicComponent/src/main/java/com/arialyy/aria/core/manager/ThreadTaskManager.java @@ -16,9 +16,8 @@ package com.arialyy.aria.core.manager; -import com.arialyy.aria.core.common.AbsThreadTask; -import com.arialyy.aria.core.inf.AbsTask; -import com.arialyy.aria.core.inf.AbsTaskWrapper; +import com.arialyy.aria.core.wrapper.AbsTaskWrapper; +import com.arialyy.aria.core.task.IThreadTask; import com.arialyy.aria.util.ALog; import com.arialyy.aria.util.CommonUtil; import java.util.HashSet; @@ -82,9 +81,9 @@ public class ThreadTaskManager { * 启动线程任务 * * @param key 任务对应的key{@link AbsTaskWrapper#getKey()} - * @param threadTask 线程任务{@link AbsThreadTask} + * @param threadTask 线程任务{@link IThreadTask} */ - public void startThread(String key, AbsThreadTask threadTask) { + public void startThread(String key, IThreadTask threadTask) { try { LOCK.tryLock(2, TimeUnit.SECONDS); if (mExePool.isShutdown()) { @@ -111,7 +110,7 @@ public class ThreadTaskManager { /** * 任务是否在执行 * - * @param key 任务的key{@link AbsTask#getKey()} + * @param key 任务的key * @return {@code true} 任务正在运行 */ public boolean taskIsRunning(String key) { @@ -155,7 +154,7 @@ public class ThreadTaskManager { * @param key 任务的key * @param task 线程任务 */ - public boolean removeSingleTaskThread(String key, AbsThreadTask task) { + public boolean removeSingleTaskThread(String key, IThreadTask task) { try { LOCK.tryLock(2, TimeUnit.SECONDS); if (mExePool.isShutdown()) { @@ -195,7 +194,7 @@ public class ThreadTaskManager { * * @param task 线程任务 */ - public void retryThread(AbsThreadTask task) { + public void retryThread(IThreadTask task) { try { LOCK.tryLock(2, TimeUnit.SECONDS); if (mExePool.isShutdown()) { @@ -231,6 +230,6 @@ public class ThreadTaskManager { private class FutureContainer { Future future; - AbsThreadTask threadTask; + IThreadTask threadTask; } } diff --git a/Aria/src/main/java/com/arialyy/aria/core/common/ftp/FtpInterceptHandler.java b/PublicComponent/src/main/java/com/arialyy/aria/core/processor/FtpInterceptHandler.java similarity index 98% rename from Aria/src/main/java/com/arialyy/aria/core/common/ftp/FtpInterceptHandler.java rename to PublicComponent/src/main/java/com/arialyy/aria/core/processor/FtpInterceptHandler.java index 53114368..bcf5ef17 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/common/ftp/FtpInterceptHandler.java +++ b/PublicComponent/src/main/java/com/arialyy/aria/core/processor/FtpInterceptHandler.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.arialyy.aria.core.common.ftp; +package com.arialyy.aria.core.processor; import android.text.TextUtils; diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/m3u8/IBandWidthUrlConverter.java b/PublicComponent/src/main/java/com/arialyy/aria/core/processor/IBandWidthUrlConverter.java similarity index 89% rename from Aria/src/main/java/com/arialyy/aria/core/download/m3u8/IBandWidthUrlConverter.java rename to PublicComponent/src/main/java/com/arialyy/aria/core/processor/IBandWidthUrlConverter.java index ffbde948..21fab034 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/m3u8/IBandWidthUrlConverter.java +++ b/PublicComponent/src/main/java/com/arialyy/aria/core/processor/IBandWidthUrlConverter.java @@ -13,13 +13,15 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.arialyy.aria.core.download.m3u8; +package com.arialyy.aria.core.processor; + +import com.arialyy.aria.core.inf.IEventHandler; /** * M3U8 bandWidth 码率url转换器,对于某些服务器,返回的ts地址可以是相对地址,也可能是处理过的, * 对于这种情况,你需要使用url转换器将地址转换为可正常访问的http地址 */ -public interface IBandWidthUrlConverter { +public interface IBandWidthUrlConverter extends IEventHandler { /** * 转换码率地址为可用的http地址,对于某些服务器,返回的切片信息有可能是相对地址,也可能是处理过的, diff --git a/Aria/src/main/java/com/arialyy/aria/core/common/ftp/IFtpUploadInterceptor.java b/PublicComponent/src/main/java/com/arialyy/aria/core/processor/IFtpUploadInterceptor.java similarity index 91% rename from Aria/src/main/java/com/arialyy/aria/core/common/ftp/IFtpUploadInterceptor.java rename to PublicComponent/src/main/java/com/arialyy/aria/core/processor/IFtpUploadInterceptor.java index 1feddc7d..4d8e6277 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/common/ftp/IFtpUploadInterceptor.java +++ b/PublicComponent/src/main/java/com/arialyy/aria/core/processor/IFtpUploadInterceptor.java @@ -13,8 +13,9 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.arialyy.aria.core.common.ftp; +package com.arialyy.aria.core.processor; +import com.arialyy.aria.core.inf.IEventHandler; import com.arialyy.aria.core.upload.UploadEntity; import java.util.List; @@ -41,7 +42,7 @@ import java.util.List; * * */ -public interface IFtpUploadInterceptor { +public interface IFtpUploadInterceptor extends IEventHandler { /** * 处理拦截事件 diff --git a/Aria/src/main/java/com/arialyy/aria/core/inf/IHttpFileLenAdapter.java b/PublicComponent/src/main/java/com/arialyy/aria/core/processor/IHttpFileLenAdapter.java similarity index 86% rename from Aria/src/main/java/com/arialyy/aria/core/inf/IHttpFileLenAdapter.java rename to PublicComponent/src/main/java/com/arialyy/aria/core/processor/IHttpFileLenAdapter.java index 55b8c507..f7090a86 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/inf/IHttpFileLenAdapter.java +++ b/PublicComponent/src/main/java/com/arialyy/aria/core/processor/IHttpFileLenAdapter.java @@ -13,9 +13,9 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.arialyy.aria.core.inf; +package com.arialyy.aria.core.processor; -import java.io.Serializable; +import com.arialyy.aria.core.inf.IEventHandler; import java.net.URLConnection; import java.util.List; import java.util.Map; @@ -23,8 +23,7 @@ import java.util.Map; /** * Http文件长度适配器 */ -public interface IHttpFileLenAdapter extends Serializable { - long serialVersionUID = 1L; +public interface IHttpFileLenAdapter extends IEventHandler { /** * 同伙header中的数据获取文件长度 diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/m3u8/ILiveTsUrlConverter.java b/PublicComponent/src/main/java/com/arialyy/aria/core/processor/ILiveTsUrlConverter.java similarity index 88% rename from Aria/src/main/java/com/arialyy/aria/core/download/m3u8/ILiveTsUrlConverter.java rename to PublicComponent/src/main/java/com/arialyy/aria/core/processor/ILiveTsUrlConverter.java index 09a6abb8..bfd06ceb 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/m3u8/ILiveTsUrlConverter.java +++ b/PublicComponent/src/main/java/com/arialyy/aria/core/processor/ILiveTsUrlConverter.java @@ -13,13 +13,15 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.arialyy.aria.core.download.m3u8; +package com.arialyy.aria.core.processor; + +import com.arialyy.aria.core.inf.IEventHandler; /** * M3U8 直播下载,ts url转换器,对于某些服务器,返回的ts地址可以是相对地址,也可能是处理过的 * 对于这种情况,你需要使用url转换器将地址转换为可正常访问的http地址 */ -public interface ILiveTsUrlConverter { +public interface ILiveTsUrlConverter extends IEventHandler { /** * 处理#EXTINF信息,对于某些服务器,返回的切片信息有可能是相对地址,因此,你需要自行转换为可下载http连接 diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/m3u8/ITsMergeHandler.java b/PublicComponent/src/main/java/com/arialyy/aria/core/processor/ITsMergeHandler.java similarity index 85% rename from Aria/src/main/java/com/arialyy/aria/core/download/m3u8/ITsMergeHandler.java rename to PublicComponent/src/main/java/com/arialyy/aria/core/processor/ITsMergeHandler.java index 35e923d3..5f093806 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/m3u8/ITsMergeHandler.java +++ b/PublicComponent/src/main/java/com/arialyy/aria/core/processor/ITsMergeHandler.java @@ -13,15 +13,17 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.arialyy.aria.core.download.m3u8; +package com.arialyy.aria.core.processor; import androidx.annotation.Nullable; +import com.arialyy.aria.core.download.M3U8Entity; +import com.arialyy.aria.core.inf.IEventHandler; import java.util.List; /** * Ts文件合并处理,如果你希望使用自行处理ts文件的合并,你可以实现该接口 */ -public interface ITsMergeHandler { +public interface ITsMergeHandler extends IEventHandler { /** * 合并ts文件 diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/m3u8/IVodTsUrlConverter.java b/PublicComponent/src/main/java/com/arialyy/aria/core/processor/IVodTsUrlConverter.java similarity index 90% rename from Aria/src/main/java/com/arialyy/aria/core/download/m3u8/IVodTsUrlConverter.java rename to PublicComponent/src/main/java/com/arialyy/aria/core/processor/IVodTsUrlConverter.java index 2ccfc7a6..ff7a7b9b 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/m3u8/IVodTsUrlConverter.java +++ b/PublicComponent/src/main/java/com/arialyy/aria/core/processor/IVodTsUrlConverter.java @@ -13,15 +13,16 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.arialyy.aria.core.download.m3u8; +package com.arialyy.aria.core.processor; +import com.arialyy.aria.core.inf.IEventHandler; import java.util.List; /** * M3U8 点播文件下载,ts url转换器,对于某些服务器,返回的ts地址可以是相对地址,也可能是处理过的 * 对于这种情况,你需要使用url转换器将地址转换为可正常访问的http地址 */ -public interface IVodTsUrlConverter { +public interface IVodTsUrlConverter extends IEventHandler { /** * 处理#EXTINF信息,对于某些服务器,返回的切片信息有可能是相对地址,因此,你需要自行转换为可下载http连接 diff --git a/Aria/src/main/java/com/arialyy/aria/core/inf/AbsGroupTask.java b/PublicComponent/src/main/java/com/arialyy/aria/core/task/AbsGroupTask.java similarity index 90% rename from Aria/src/main/java/com/arialyy/aria/core/inf/AbsGroupTask.java rename to PublicComponent/src/main/java/com/arialyy/aria/core/task/AbsGroupTask.java index 421fe86d..c8f0918e 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/inf/AbsGroupTask.java +++ b/PublicComponent/src/main/java/com/arialyy/aria/core/task/AbsGroupTask.java @@ -13,9 +13,10 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.arialyy.aria.core.inf; +package com.arialyy.aria.core.task; -import com.arialyy.aria.core.download.group.AbsGroupUtil; +import com.arialyy.aria.core.download.AbsGroupTaskWrapper; +import com.arialyy.aria.core.group.AbsGroupUtil; /** * Created by AriaL on 2017/6/29. diff --git a/PublicComponent/src/main/java/com/arialyy/aria/core/task/AbsNormalLoaderAdapter.java b/PublicComponent/src/main/java/com/arialyy/aria/core/task/AbsNormalLoaderAdapter.java new file mode 100644 index 00000000..1515e27a --- /dev/null +++ b/PublicComponent/src/main/java/com/arialyy/aria/core/task/AbsNormalLoaderAdapter.java @@ -0,0 +1,48 @@ +/* + * 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.task; + +import com.arialyy.aria.core.common.AbsNormalEntity; +import com.arialyy.aria.core.loader.ILoaderAdapter; +import com.arialyy.aria.core.wrapper.ITaskWrapper; +import com.arialyy.aria.util.CommonUtil; +import java.io.File; + +/** + * 但文件任务适配器 + * + * @Author lyy + * @Date 2019-09-19 + */ +public abstract class AbsNormalLoaderAdapter implements ILoaderAdapter { + protected String TAG = CommonUtil.getClassName(getClass()); + + private ITaskWrapper mWrapper; + private File mTempFile; + + public AbsNormalLoaderAdapter(ITaskWrapper wrapper) { + mWrapper = wrapper; + mTempFile = new File(((AbsNormalEntity) wrapper.getEntity()).getFilePath()); + } + + public ITaskWrapper getWrapper() { + return mWrapper; + } + + public File getTempFile() { + return mTempFile; + } +} diff --git a/Aria/src/main/java/com/arialyy/aria/core/inf/AbsTask.java b/PublicComponent/src/main/java/com/arialyy/aria/core/task/AbsTask.java similarity index 86% rename from Aria/src/main/java/com/arialyy/aria/core/inf/AbsTask.java rename to PublicComponent/src/main/java/com/arialyy/aria/core/task/AbsTask.java index fc206bd2..514129a6 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/inf/AbsTask.java +++ b/PublicComponent/src/main/java/com/arialyy/aria/core/task/AbsTask.java @@ -13,12 +13,16 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.arialyy.aria.core.inf; +package com.arialyy.aria.core.task; import android.content.Context; import android.os.Handler; import android.text.TextUtils; -import com.arialyy.aria.core.common.IUtil; +import com.arialyy.aria.core.inf.IEntity; +import com.arialyy.aria.core.inf.IUtil; +import com.arialyy.aria.core.inf.TaskSchedulerType; +import com.arialyy.aria.core.listener.IEventListener; +import com.arialyy.aria.core.wrapper.AbsTaskWrapper; import com.arialyy.aria.util.ALog; import com.arialyy.aria.util.CommonUtil; import java.util.HashMap; @@ -30,7 +34,7 @@ import java.util.Map; public abstract class AbsTask implements ITask { public static final String ERROR_INFO_KEY = "ERROR_INFO_KEY"; - + protected String TAG = CommonUtil.getClassName(getClass()); /** * 是否需要重试,默认为false */ @@ -38,7 +42,7 @@ public abstract class AbsTask protected TASK_WRAPPER mTaskWrapper; protected Handler mOutHandler; protected Context mContext; - boolean isHeighestTask = false; + protected boolean isHeighestTask = false; private boolean isCancel = false, isStop = false; private IUtil mUtil; /** @@ -51,10 +55,8 @@ public abstract class AbsTask @TaskSchedulerType private int mSchedulerType = TaskSchedulerType.TYPE_DEFAULT; protected IEventListener mListener; - protected String TAG; protected AbsTask() { - TAG = CommonUtil.getClassName(this); } public Handler getOutHandler() { @@ -92,6 +94,19 @@ public abstract class AbsTask this.needRetry = needRetry; } + /** + * 最高优先级命令,最高优先级命令有以下属性 + * 1、在下载队列中,有且只有一个最高优先级任务 + * 2、最高优先级任务会一直存在,直到用户手动暂停或任务完成 + * 3、任务调度器不会暂停最高优先级任务 + * 4、用户手动暂停或任务完成后,第二次重新执行该任务,该命令将失效 + * 5、如果下载队列中已经满了,则会停止队尾的任务 + * 6、把任务设置为最高优先级任务后,将自动执行任务,不需要重新调用start()启动任务 + */ + public void setHighestPriority(boolean isHighestPriority) { + isHeighestTask = isHighestPriority; + } + /** * 读取扩展数据 */ diff --git a/PublicComponent/src/main/java/com/arialyy/aria/core/task/AbsThreadTaskAdapter.java b/PublicComponent/src/main/java/com/arialyy/aria/core/task/AbsThreadTaskAdapter.java new file mode 100644 index 00000000..8e4efe97 --- /dev/null +++ b/PublicComponent/src/main/java/com/arialyy/aria/core/task/AbsThreadTaskAdapter.java @@ -0,0 +1,133 @@ +/* + * 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.task; + +import com.arialyy.aria.core.ThreadRecord; +import com.arialyy.aria.core.common.SubThreadConfig; +import com.arialyy.aria.core.config.BaseTaskConfig; +import com.arialyy.aria.core.wrapper.AbsTaskWrapper; +import com.arialyy.aria.exception.BaseException; +import com.arialyy.aria.util.BandwidthLimiter; +import com.arialyy.aria.util.CommonUtil; + +/** + * @Author lyy + * @Date 2019-09-18 + */ +public abstract class AbsThreadTaskAdapter implements IThreadTaskAdapter { + + protected String TAG = CommonUtil.getClassName(getClass()); + /** + * 速度限制工具 + */ + protected BandwidthLimiter mSpeedBandUtil; + /** + * 当前线程的下去区间的进度 + */ + private long mRangeProgress = 0; + private ThreadRecord mThreadRecord; + private IThreadTaskObserver mObserver; + private AbsTaskWrapper mWrapper; + private SubThreadConfig mThreadConfig; + private IThreadTask mThreadTask; + + protected AbsThreadTaskAdapter(SubThreadConfig config) { + mThreadRecord = config.record; + mWrapper = config.taskWrapper; + mThreadConfig = config; + mRangeProgress = mThreadRecord.startLocation; + if (getTaskConfig().getMaxSpeed() > 0) { + mSpeedBandUtil = new BandwidthLimiter(getTaskConfig().getMaxSpeed(), config.startThreadNum); + } + } + + @Override public void call(IThreadTask threadTask) throws Exception { + mThreadTask = threadTask; + handlerThreadTask(); + } + + /** + * 开始处理线程任务 + */ + protected abstract void handlerThreadTask(); + + /** + * 当前线程的下去区间的进度 + */ + protected long getRangeProgress() { + return mRangeProgress; + } + + protected ThreadRecord getThreadRecord() { + return mThreadRecord; + } + + @Deprecated + protected AbsTaskWrapper getTaskWrapper() { + // TODO: 2019-09-18 需要修改方法名称为:getWrapper + return mWrapper; + } + + /** + * 获取任务配置信息 + */ + protected BaseTaskConfig getTaskConfig() { + return getTaskWrapper().getConfig(); + } + + protected IThreadTask getThreadTask() { + return mThreadTask; + } + + /** + * 获取线程配置信息 + */ + @Deprecated + protected SubThreadConfig getConfig() { + // TODO: 2019-09-18 需要修改方法名称为:getThreadConfig + return mThreadConfig; + } + + @Override public void setThreadStateObserver(IThreadTaskObserver observer) { + mObserver = observer; + } + + @Override public void setMaxSpeed(int speed) { + if (mSpeedBandUtil == null) { + mSpeedBandUtil = + new BandwidthLimiter(getTaskConfig().getMaxSpeed(), getConfig().startThreadNum); + } + mSpeedBandUtil.setMaxRate(speed); + } + + protected void complete() { + if (mObserver != null) { + mObserver.updateCompleteState(); + } + } + + protected void fail(BaseException ex, boolean needRetry) { + if (mObserver != null) { + mObserver.updateFailState(ex, needRetry); + } + } + + protected void progress(long len) { + if (mObserver != null) { + mObserver.updateProgress(len); + } + } +} diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadGroupTask.java b/PublicComponent/src/main/java/com/arialyy/aria/core/task/DownloadGroupTask.java similarity index 67% rename from Aria/src/main/java/com/arialyy/aria/core/download/DownloadGroupTask.java rename to PublicComponent/src/main/java/com/arialyy/aria/core/task/DownloadGroupTask.java index 7ad5568c..329e1b3d 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadGroupTask.java +++ b/PublicComponent/src/main/java/com/arialyy/aria/core/task/DownloadGroupTask.java @@ -13,20 +13,18 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.arialyy.aria.core.download; +package com.arialyy.aria.core.task; import android.os.Handler; import android.os.Looper; import android.text.TextUtils; -import com.arialyy.aria.core.AriaManager; -import com.arialyy.aria.core.common.IUtil; -import com.arialyy.aria.core.download.group.DGroupUtil; -import com.arialyy.aria.core.download.group.FtpDirDownloadUtil; -import com.arialyy.aria.core.download.group.IDGroupListener; -import com.arialyy.aria.core.inf.AbsGroupTask; -import com.arialyy.aria.core.inf.ITaskWrapper; -import com.arialyy.aria.core.scheduler.ISchedulers; +import com.arialyy.aria.core.AriaConfig; +import com.arialyy.aria.core.download.DGTaskWrapper; +import com.arialyy.aria.core.download.DownloadGroupEntity; +import com.arialyy.aria.core.inf.IUtil; +import com.arialyy.aria.core.listener.ISchedulers; import com.arialyy.aria.util.CheckUtil; +import com.arialyy.aria.util.ComponentUtil; /** * Created by AriaL on 2017/6/27. @@ -37,8 +35,9 @@ public class DownloadGroupTask extends AbsGroupTask { private DownloadGroupTask(DGTaskWrapper taskWrapper, Handler outHandler) { mTaskWrapper = taskWrapper; mOutHandler = outHandler; - mContext = AriaManager.getInstance().getAPP(); - mListener = new DownloadGroupListener(this, mOutHandler); + mContext = AriaConfig.getInstance().getAPP(); + mListener = + ComponentUtil.getInstance().buildListener(taskWrapper.getRequestType(), this, mOutHandler); } public DownloadGroupEntity getEntity() { @@ -55,14 +54,7 @@ public class DownloadGroupTask extends AbsGroupTask { } @Override protected synchronized IUtil createUtil() { - int taskType = mTaskWrapper.getRequestType(); - if (taskType == ITaskWrapper.DG_HTTP) { - return new DGroupUtil((IDGroupListener) mListener, mTaskWrapper); - } else if (taskType == ITaskWrapper.D_FTP_DIR) { - return new FtpDirDownloadUtil((IDGroupListener) mListener, mTaskWrapper); - } else { - throw new IllegalArgumentException(String.format("不识别的任务, 任务类型:%s", taskType)); - } + return ComponentUtil.getInstance().buildUtil(mTaskWrapper, mListener); } public static class Builder { diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadTask.java b/PublicComponent/src/main/java/com/arialyy/aria/core/task/DownloadTask.java similarity index 55% rename from Aria/src/main/java/com/arialyy/aria/core/download/DownloadTask.java rename to PublicComponent/src/main/java/com/arialyy/aria/core/task/DownloadTask.java index 685682c6..002fdbff 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadTask.java +++ b/PublicComponent/src/main/java/com/arialyy/aria/core/task/DownloadTask.java @@ -14,48 +14,29 @@ * limitations under the License. */ -package com.arialyy.aria.core.download; +package com.arialyy.aria.core.task; import android.os.Handler; import android.os.Looper; -import com.arialyy.aria.core.AriaManager; -import com.arialyy.aria.core.common.IUtil; -import com.arialyy.aria.core.download.downloader.SimpleDownloadUtil; -import com.arialyy.aria.core.download.m3u8.M3U8LiveUtil; -import com.arialyy.aria.core.download.m3u8.M3U8VodUtil; -import com.arialyy.aria.core.inf.AbsNormalTask; -import com.arialyy.aria.core.inf.AbsTaskWrapper; -import com.arialyy.aria.core.inf.IDownloadListener; -import com.arialyy.aria.core.inf.ITaskWrapper; -import com.arialyy.aria.core.scheduler.ISchedulers; +import com.arialyy.aria.core.AriaConfig; +import com.arialyy.aria.core.download.DTaskWrapper; +import com.arialyy.aria.core.download.DownloadEntity; +import com.arialyy.aria.core.inf.IUtil; +import com.arialyy.aria.core.listener.ISchedulers; +import com.arialyy.aria.util.ComponentUtil; /** * Created by lyy on 2016/8/11. * 下载任务类 */ -public class DownloadTask extends AbsNormalTask { - public static final String TAG = "DownloadTask"; +public class DownloadTask extends AbsTask { private DownloadTask(DTaskWrapper taskWrapper, Handler outHandler) { mTaskWrapper = taskWrapper; mOutHandler = outHandler; - mContext = AriaManager.getInstance().getAPP(); - if (taskWrapper.getRequestType() == AbsTaskWrapper.M3U8_VOD - || taskWrapper.getRequestType() == AbsTaskWrapper.M3U8_LIVE) { - mListener = new M3U8Listener(this, mOutHandler); - } else { - mListener = new BaseDListener(this, mOutHandler); - } - } - - /** - * 获取文件保存路径 - * - * @deprecated 后续版本将删除该方法,请使用{@link #getFilePath()} - */ - @Deprecated - public String getDownloadPath() { - return getFilePath(); + mContext = AriaConfig.getInstance().getAPP(); + mListener = + ComponentUtil.getInstance().buildListener(taskWrapper.getRequestType(), this, mOutHandler); } /** @@ -79,7 +60,7 @@ public class DownloadTask extends AbsNormalTask { } @Override public int getTaskType() { - return DOWNLOAD; + return ITask.DOWNLOAD; } @Override public String getKey() { @@ -95,16 +76,7 @@ public class DownloadTask extends AbsNormalTask { } @Override protected synchronized IUtil createUtil() { - int taskType = mTaskWrapper.getRequestType(); - if (taskType == ITaskWrapper.M3U8_VOD) { - return new M3U8VodUtil(mTaskWrapper, (M3U8Listener) mListener); - } else if (taskType == ITaskWrapper.M3U8_LIVE) { - return new M3U8LiveUtil(mTaskWrapper, (M3U8Listener) mListener); - } else if (taskType == ITaskWrapper.D_HTTP || taskType == ITaskWrapper.D_FTP) { - return new SimpleDownloadUtil(mTaskWrapper, (IDownloadListener) mListener); - } else { - throw new IllegalArgumentException(String.format("不识别的任务, 任务类型:%s", taskType)); - } + return ComponentUtil.getInstance().buildUtil(mTaskWrapper, mListener); } public static class Builder { diff --git a/Aria/src/main/java/com/arialyy/aria/core/inf/ITask.java b/PublicComponent/src/main/java/com/arialyy/aria/core/task/ITask.java similarity index 90% rename from Aria/src/main/java/com/arialyy/aria/core/inf/ITask.java rename to PublicComponent/src/main/java/com/arialyy/aria/core/task/ITask.java index 2cdc9d61..e9f1e5a4 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/inf/ITask.java +++ b/PublicComponent/src/main/java/com/arialyy/aria/core/task/ITask.java @@ -13,15 +13,13 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.arialyy.aria.core.inf; +package com.arialyy.aria.core.task; -import com.arialyy.aria.core.download.DownloadGroupTask; -import com.arialyy.aria.core.download.DownloadTask; -import com.arialyy.aria.core.upload.UploadTask; +import com.arialyy.aria.core.inf.TaskSchedulerType; +import com.arialyy.aria.core.wrapper.AbsTaskWrapper; /** * Created by lyy on 2017/2/13. - * 任务接口{@link DownloadTask}、{@link UploadTask}、{@link DownloadGroupTask} */ public interface ITask { diff --git a/PublicComponent/src/main/java/com/arialyy/aria/core/task/IThreadTask.java b/PublicComponent/src/main/java/com/arialyy/aria/core/task/IThreadTask.java new file mode 100644 index 00000000..7af9aca5 --- /dev/null +++ b/PublicComponent/src/main/java/com/arialyy/aria/core/task/IThreadTask.java @@ -0,0 +1,90 @@ +/* + * 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.task; + +import java.util.concurrent.Callable; + +/** + * @Author lyy + * @Date 2019-09-18 + */ +public interface IThreadTask extends Callable { + + /** + * 销毁任务 + */ + void destroy(); + + /** + * 判断线程任务是否销毁 + * + * @return true 已经销毁 + */ + boolean isDestroy(); + + /** + * 中断任务 + */ + void breakTask(); + + /** + * 当前线程是否完成,对于不支持断点的任务,一律未完成 + * + * @return {@code true} 完成;{@code false} 未完成 + */ + boolean isThreadComplete(); + + /** + * 取消任务 + */ + void cancel(); + + /** + * 停止任务 + */ + void stop(); + + /** + * 设置当前线程最大下载速度 + * + * @param speed 单位为:kb + */ + void setMaxSpeed(int speed); + + /** + * 线程是否存活 + * + * @return {@code true}存活 + */ + boolean isLive(); + + /** + * 任务是否中断,中断条件: + * 1、任务取消 + * 2、任务停止 + * 3、手动中断 + * + * @return {@code true} 中断,{@code false} 不是中断 + */ + boolean isBreak(); + + /** + * 检查下载完成的分块大小,如果下载完成的分块大小大于或小于分配的大小,则需要重新下载该分块 如果是非分块任务,直接返回{@code true} + * + * @return {@code true} 分块分大小正常,{@code false} 分块大小错误 + */ + boolean checkBlock(); +} diff --git a/PublicComponent/src/main/java/com/arialyy/aria/core/task/IThreadTaskAdapter.java b/PublicComponent/src/main/java/com/arialyy/aria/core/task/IThreadTaskAdapter.java new file mode 100644 index 00000000..f03deb65 --- /dev/null +++ b/PublicComponent/src/main/java/com/arialyy/aria/core/task/IThreadTaskAdapter.java @@ -0,0 +1,42 @@ +/* + * 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.task; + +/** + * 线程适配器 + * + * @Author lyy + * @Date 2019-09-18 + */ +public interface IThreadTaskAdapter { + + /** + * 执行任务 + */ + void call(IThreadTask threadTask) throws Exception; + + /** + * 设置当前线程最大下载速度 + * + * @param speed 单位为:kb + */ + void setMaxSpeed(int speed); + + /** + * 设置线程任务状态观察者 + */ + void setThreadStateObserver(IThreadTaskObserver observer); +} diff --git a/PublicComponent/src/main/java/com/arialyy/aria/core/task/IThreadTaskObserver.java b/PublicComponent/src/main/java/com/arialyy/aria/core/task/IThreadTaskObserver.java new file mode 100644 index 00000000..8a92bcf3 --- /dev/null +++ b/PublicComponent/src/main/java/com/arialyy/aria/core/task/IThreadTaskObserver.java @@ -0,0 +1,56 @@ +/* + * 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.task; + +import android.os.Bundle; +import androidx.annotation.Nullable; +import com.arialyy.aria.core.inf.IThreadState; +import com.arialyy.aria.exception.BaseException; + +/** + * 线程任务观察者 + * + * @Author lyy + * @Date 2019-09-18 + */ +public interface IThreadTaskObserver { + + /** + * 更新所有状态 + * + * @param state state {@link IThreadState#STATE_STOP}.. + */ + void updateState(int state, @Nullable Bundle bundle); + + /** + * 更新完成的状态 + */ + void updateCompleteState(); + + /** + * 更新失败的状态 + * + * @param needRetry 是否需要重试,一般是网络错误才需要重试 + */ + void updateFailState(@Nullable BaseException e, boolean needRetry); + + /** + * 更新进度 + * + * @param len 新增的长度 + */ + void updateProgress(long len); +} diff --git a/Aria/src/main/java/com/arialyy/aria/core/common/AbsThreadTask.java b/PublicComponent/src/main/java/com/arialyy/aria/core/task/ThreadTask.java similarity index 78% rename from Aria/src/main/java/com/arialyy/aria/core/common/AbsThreadTask.java rename to PublicComponent/src/main/java/com/arialyy/aria/core/task/ThreadTask.java index f061e8b7..d711559a 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/common/AbsThreadTask.java +++ b/PublicComponent/src/main/java/com/arialyy/aria/core/task/ThreadTask.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.arialyy.aria.core.common; +package com.arialyy.aria.core.task; import android.net.TrafficStats; import android.os.Bundle; @@ -21,16 +21,15 @@ import android.os.Handler; import android.os.Message; import android.os.Process; import androidx.annotation.Nullable; -import com.arialyy.aria.core.AriaManager; -import com.arialyy.aria.core.config.BaseTaskConfig; -import com.arialyy.aria.core.config.DGroupConfig; -import com.arialyy.aria.core.config.DownloadConfig; -import com.arialyy.aria.core.config.UploadConfig; -import com.arialyy.aria.core.inf.AbsNormalEntity; -import com.arialyy.aria.core.inf.AbsTaskWrapper; -import com.arialyy.aria.core.inf.ITaskWrapper; +import com.arialyy.aria.core.AriaConfig; +import com.arialyy.aria.core.ThreadRecord; +import com.arialyy.aria.core.common.SubThreadConfig; +import com.arialyy.aria.core.inf.IEntity; +import com.arialyy.aria.core.listener.ISchedulers; +import com.arialyy.aria.core.inf.IThreadState; import com.arialyy.aria.core.manager.ThreadTaskManager; -import com.arialyy.aria.core.scheduler.ISchedulers; +import com.arialyy.aria.core.wrapper.AbsTaskWrapper; +import com.arialyy.aria.core.wrapper.ITaskWrapper; import com.arialyy.aria.exception.BaseException; import com.arialyy.aria.util.ALog; import com.arialyy.aria.util.BufferedRandomAccessFile; @@ -39,39 +38,35 @@ import com.arialyy.aria.util.FileUtil; import com.arialyy.aria.util.NetUtils; import java.io.File; import java.util.UUID; -import java.util.concurrent.Callable; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; /** * Created by lyy on 2017/1/18. 任务线程 */ -public abstract class AbsThreadTask> - implements Callable { +public class ThreadTask implements IThreadTask, IThreadTaskObserver { /** * 线程重试次数 */ private final int RETRY_NUM = 2; - private final String TAG = "AbsThreadTask"; - /** - * 当前子线程相对于总长度的位置 - */ - protected long mChildCurrentLocation = 0; - private ENTITY mEntity; - protected TASK_WRAPPER mTaskWrapper; + private IEntity mEntity; + protected AbsTaskWrapper mTaskWrapper; private int mFailTimes = 0; private long mLastSaveTime; - private ExecutorService mConfigThreadPool; private boolean isNotNetRetry; //断网情况是否重试 private boolean taskBreak = false; //任务跳出 - protected BandwidthLimiter mSpeedBandUtil; //速度限制工具 - protected AriaManager mAridManager; private boolean isDestroy = false; protected boolean isCancel = false, isStop = false; - protected ThreadRecord mRecord; + private ExecutorService mConfigThreadPool; private Handler mStateHandler; - private SubThreadConfig mConfig; + private SubThreadConfig mConfig; + /** + * 当前子线程相对于总长度的位置 + */ + protected long mChildCurrentLocation = 0; + private IThreadTaskAdapter mAdapter; + protected ThreadRecord mRecord; private Thread mConfigThread = new Thread(new Runnable() { @Override public void run() { @@ -81,7 +76,7 @@ public abstract class AbsThreadTask config) { + public ThreadTask(SubThreadConfig config) { mConfig = config; mTaskWrapper = config.taskWrapper; mRecord = config.record; @@ -89,14 +84,18 @@ public abstract class AbsThreadTask 0) { - mSpeedBandUtil = new BandwidthLimiter(getMaxSpeed(), config.startThreadNum); - } - isNotNetRetry = mAridManager.getAppConfig().isNotNetRetry(); + + isNotNetRetry = AriaConfig.getInstance().getAConfig().isNotNetRetry(); mChildCurrentLocation = mRecord.startLocation; } + /** + * 设置线程任务适配器 + */ + public void setAdapter(IThreadTaskAdapter adapter) { + mAdapter = adapter; + } + /** * 当前线程处理的文件名 */ @@ -104,89 +103,71 @@ public abstract class AbsThreadTask getConfig() { + public SubThreadConfig getConfig() { return mConfig; } /** * 设置线程是否中断 */ + @Override public void destroy() { this.isDestroy = true; } - /** - * 线程执行完成 - */ - protected void onThreadComplete() { - - } - /** * 线程是否存活 * * @return {@code true}存活 */ - protected boolean isLive() { + @Override + public boolean isLive() { return !Thread.currentThread().isInterrupted() && !isDestroy; } /** * 当前线程是否完成,对于不支持断点的任务,一律未完成 {@code true} 完成;{@code false} 未完成 */ - boolean isThreadComplete() { + @Override + public boolean isThreadComplete() { return mRecord.isComplete; } /** * 获取实体 */ - protected ENTITY getEntity() { + protected IEntity getEntity() { return mEntity; } /** * 获取任务驱动对象 */ - protected TASK_WRAPPER getTaskWrapper() { + protected ITaskWrapper getTaskWrapper() { return mTaskWrapper; } - /** - * 获取配置的最大上传/下载速度 - * - * @return 单位为:kb - */ - public abstract int getMaxSpeed(); - - /** - * 读取任务配置 - * - * @return {@link DownloadConfig}、{@link UploadConfig}、{@link DGroupConfig} - */ - protected abstract BaseTaskConfig getTaskConfig(); - /** * 设置当前线程最大下载速度 * * @param speed 单位为:kb */ - + @Override public void setMaxSpeed(int speed) { - if (mSpeedBandUtil == null) { - mSpeedBandUtil = new BandwidthLimiter(getMaxSpeed(), getConfig().startThreadNum); + if (mAdapter != null) { + mAdapter.setMaxSpeed(speed); } - mSpeedBandUtil.setMaxRate(speed); } /** * 中断任务 */ - void breakTask() { + @Override + public void breakTask() { taskBreak = true; if (mTaskWrapper.isSupportBP()) { final long currentTemp = mChildCurrentLocation; - sendState(IThreadState.STATE_STOP, null); + updateState(IThreadState.STATE_STOP, null); ALog.d(TAG, String.format("任务【%s】thread__%s__中断【停止位置:%s】", getFileName(), mRecord.threadId, currentTemp)); writeConfig(false, currentTemp); @@ -195,39 +176,7 @@ public abstract class AbsThreadTask getEntity().getFileSize() && !getTaskWrapper().asHttp() - // .isChunked()) { - // String errorMsg = - // String.format("下载失败,下载长度超出文件真实长度;currentLocation=%s, fileSize=%s", - // getState().CURRENT_LOCATION, - // getEntity().getFileSize()); - // taskBreak = true; - // fail(mChildCurrentLocation, new FileException(TAG, errorMsg), false); - // return; - //} + @Override + public synchronized void updateState(int state, @Nullable Bundle bundle) { + Message msg = mStateHandler.obtainMessage(); + msg.what = state; + if (state != IThreadState.STATE_UPDATE_PROGRESS) { + msg.obj = this; + } + + if ((state == IThreadState.STATE_COMPLETE || state == IThreadState.STATE_FAIL) + && (mTaskWrapper.getRequestType() == AbsTaskWrapper.M3U8_VOD + || mTaskWrapper.getRequestType() == AbsTaskWrapper.M3U8_LIVE)) { + if (bundle == null) { + bundle = new Bundle(); + } + bundle.putString(ISchedulers.DATA_M3U8_URL, getConfig().url); + bundle.putString(ISchedulers.DATA_M3U8_PEER_PATH, getConfig().tempFile.getPath()); + bundle.putInt(ISchedulers.DATA_M3U8_PEER_INDEX, getConfig().peerIndex); + } + if (bundle != null) { + msg.setData(bundle); + } + Thread loopThread = mStateHandler.getLooper().getThread(); + if (!loopThread.isAlive() || loopThread.isInterrupted()) { + return; + } + msg.sendToTarget(); + } + + @Override public synchronized void updateCompleteState() { + ALog.i(TAG, String.format("任务【%s】线程__%s__下载完毕", getTaskWrapper().getKey(), mRecord.threadId)); + writeConfig(true, mRecord.endLocation); + updateState(IThreadState.STATE_COMPLETE, null); + } + + /** + * 更新失败的状态 + * + * @param needRetry 是否需要重试,一般是网络错误才需要重试 + */ + @Override public void updateFailState(@Nullable BaseException e, boolean needRetry) { + + } + + @Override + public synchronized void updateProgress(long len) { mChildCurrentLocation += len; Thread loopThread = mStateHandler.getLooper().getThread(); if (!loopThread.isAlive() || loopThread.isInterrupted()) { @@ -330,9 +319,10 @@ public abstract class AbsThreadTask { +public class UploadTask extends AbsTask { private UploadTask(UTaskWrapper taskWrapper, Handler outHandler) { mTaskWrapper = taskWrapper; mOutHandler = outHandler; - mListener = new BaseUListener(this, mOutHandler); + mListener = + ComponentUtil.getInstance().buildListener(taskWrapper.getRequestType(), this, mOutHandler); } @Override public int getTaskType() { @@ -51,7 +52,7 @@ public class UploadTask extends AbsNormalTask { } @Override protected synchronized IUtil createUtil() { - return new SimpleUploadUtil(mTaskWrapper, (IUploadListener) mListener); + return ComponentUtil.getInstance().buildUtil(mTaskWrapper, mListener); } public static class Builder { diff --git a/Aria/src/main/java/com/arialyy/aria/core/upload/UTaskWrapper.java b/PublicComponent/src/main/java/com/arialyy/aria/core/upload/UTaskWrapper.java similarity index 96% rename from Aria/src/main/java/com/arialyy/aria/core/upload/UTaskWrapper.java rename to PublicComponent/src/main/java/com/arialyy/aria/core/upload/UTaskWrapper.java index 29bdb811..42e5d4c8 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/upload/UTaskWrapper.java +++ b/PublicComponent/src/main/java/com/arialyy/aria/core/upload/UTaskWrapper.java @@ -17,7 +17,7 @@ package com.arialyy.aria.core.upload; import com.arialyy.aria.core.config.Configuration; import com.arialyy.aria.core.config.UploadConfig; -import com.arialyy.aria.core.inf.AbsTaskWrapper; +import com.arialyy.aria.core.wrapper.AbsTaskWrapper; /** * Created by lyy on 2017/2/9. 上传任务实体 diff --git a/Aria/src/main/java/com/arialyy/aria/core/upload/UploadEntity.java b/PublicComponent/src/main/java/com/arialyy/aria/core/upload/UploadEntity.java similarity index 94% rename from Aria/src/main/java/com/arialyy/aria/core/upload/UploadEntity.java rename to PublicComponent/src/main/java/com/arialyy/aria/core/upload/UploadEntity.java index 9cff621f..f7d74df9 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/upload/UploadEntity.java +++ b/PublicComponent/src/main/java/com/arialyy/aria/core/upload/UploadEntity.java @@ -17,8 +17,8 @@ package com.arialyy.aria.core.upload; import android.os.Parcel; import android.os.Parcelable; -import com.arialyy.aria.core.inf.AbsNormalEntity; -import com.arialyy.aria.core.inf.ITaskWrapper; +import com.arialyy.aria.core.common.AbsNormalEntity; +import com.arialyy.aria.core.wrapper.ITaskWrapper; import com.arialyy.aria.orm.annotation.Primary; /** @@ -44,6 +44,7 @@ public class UploadEntity extends AbsNormalEntity implements Parcelable { this.responseStr = responseStr; } + @Override public String getFilePath() { return filePath; } diff --git a/Aria/src/main/java/com/arialyy/aria/core/inf/AbsTaskWrapper.java b/PublicComponent/src/main/java/com/arialyy/aria/core/wrapper/AbsTaskWrapper.java similarity index 77% rename from Aria/src/main/java/com/arialyy/aria/core/inf/AbsTaskWrapper.java rename to PublicComponent/src/main/java/com/arialyy/aria/core/wrapper/AbsTaskWrapper.java index 5d41213e..b97e8f61 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/inf/AbsTaskWrapper.java +++ b/PublicComponent/src/main/java/com/arialyy/aria/core/wrapper/AbsTaskWrapper.java @@ -13,27 +13,24 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.arialyy.aria.core.inf; +package com.arialyy.aria.core.wrapper; -import com.arialyy.aria.core.common.ftp.FtpTaskConfig; -import com.arialyy.aria.core.common.http.HttpTaskConfig; +import com.arialyy.aria.core.TaskOptionParams; +import com.arialyy.aria.core.common.AbsEntity; import com.arialyy.aria.core.config.BaseTaskConfig; -import com.arialyy.aria.core.config.DGroupConfig; -import com.arialyy.aria.core.config.DownloadConfig; -import com.arialyy.aria.core.config.UploadConfig; import com.arialyy.aria.core.download.DownloadEntity; import com.arialyy.aria.core.download.DownloadGroupEntity; import com.arialyy.aria.core.event.ErrorEvent; +import com.arialyy.aria.core.inf.IEntity; +import com.arialyy.aria.core.inf.ITaskOption; import com.arialyy.aria.core.upload.UploadEntity; +import com.arialyy.aria.util.ComponentUtil; /** * Created by lyy on 2017/2/23. 所有任务实体的父类 */ public abstract class AbsTaskWrapper - implements ITaskWrapper { - - private HttpTaskConfig httpTaskConfig; - private FtpTaskConfig ftpTaskConfig; + implements ITaskWrapper { /** * 刷新信息 {@code true} 重新刷新下载信息 @@ -47,7 +44,7 @@ public abstract class AbsTaskWrapper /** * 请求类型 {@link ITaskWrapper#D_HTTP}、{@link ITaskWrapper#D_FTP}、{@link - * ITaskWrapper#D_FTP_DIR}。。。 + * ITaskWrapper#D_FTP_DIR}.. */ private int requestType = D_HTTP; @@ -77,6 +74,31 @@ public abstract class AbsTaskWrapper */ private ErrorEvent errorEvent; + /** + * 任务配置 + */ + private ITaskOption taskOption; + + /** + * 任务配置信息 + */ + private TaskOptionParams optionParams = new TaskOptionParams(); + + public ITaskOption getTaskOption() { + return taskOption; + } + + public void generateTaskOption(Class clazz) { + taskOption = ComponentUtil.getInstance().buildTaskOption(clazz, optionParams); + } + + public TaskOptionParams getOptionParams() { + if (optionParams == null) { + optionParams = new TaskOptionParams(); + } + return optionParams; + } + public AbsTaskWrapper(ENTITY entity) { this.entity = entity; } @@ -89,42 +111,10 @@ public abstract class AbsTaskWrapper this.errorEvent = errorEvent; } - /** - * 任务识别标志 {@link AbsEntity#getKey()} - */ - public abstract String getKey(); - - /** - * 任务配置 - * - * @return {@link DownloadConfig}、{@link UploadConfig}、{@link DGroupConfig} - */ - public abstract BaseTaskConfig getConfig(); - @Override public ENTITY getEntity() { return entity; } - /** - * 设置或获取HTTP任务相关参数 - */ - public HttpTaskConfig asHttp() { - if (httpTaskConfig == null) { - httpTaskConfig = new HttpTaskConfig(); - } - return httpTaskConfig; - } - - /** - * 设置或获取FTP任务相关参数 - */ - public FtpTaskConfig asFtp() { - if (ftpTaskConfig == null) { - ftpTaskConfig = new FtpTaskConfig(); - } - return ftpTaskConfig; - } - /** * 获取任务下载状态 * @@ -134,6 +124,8 @@ public abstract class AbsTaskWrapper return getEntity().getState(); } + public abstract BaseTaskConfig getConfig(); + public boolean isRefreshInfo() { return refreshInfo; } @@ -154,6 +146,7 @@ public abstract class AbsTaskWrapper entity.setState(state); } + @Override public int getRequestType() { return requestType; } diff --git a/Aria/src/main/java/com/arialyy/aria/core/inf/ITaskWrapper.java b/PublicComponent/src/main/java/com/arialyy/aria/core/wrapper/ITaskWrapper.java similarity index 80% rename from Aria/src/main/java/com/arialyy/aria/core/inf/ITaskWrapper.java rename to PublicComponent/src/main/java/com/arialyy/aria/core/wrapper/ITaskWrapper.java index 192be8b5..e2b628ff 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/inf/ITaskWrapper.java +++ b/PublicComponent/src/main/java/com/arialyy/aria/core/wrapper/ITaskWrapper.java @@ -13,14 +13,14 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.arialyy.aria.core.inf; +package com.arialyy.aria.core.wrapper; -import com.arialyy.aria.core.download.DownloadGroupEntity; +import com.arialyy.aria.core.inf.IEntity; /** - * 组合任务实体包裹器,用于加载和任务相关的参数,如:组合任务实体{@link DownloadGroupEntity}、header头部 + * 组合任务实体包裹器,用于加载和任务相关的参数 */ -public interface ITaskWrapper { +public interface ITaskWrapper { int DEFAULT = 0; @@ -79,5 +79,20 @@ public interface ITaskWrapper { */ int U_TCP_PEER = 11; - ENTITY getEntity(); + /** + * 获取任务类型 + * + * @return {@link ITaskWrapper} + */ + int getRequestType(); + + /** + * 获取任务实体 + */ + IEntity getEntity(); + + /** + * 获取任务唯一标志 + */ + String getKey(); } diff --git a/Aria/src/main/java/com/arialyy/aria/core/common/RecordWrapper.java b/PublicComponent/src/main/java/com/arialyy/aria/core/wrapper/RecordWrapper.java similarity index 91% rename from Aria/src/main/java/com/arialyy/aria/core/common/RecordWrapper.java rename to PublicComponent/src/main/java/com/arialyy/aria/core/wrapper/RecordWrapper.java index 05e77cf8..bbd9c4c1 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/common/RecordWrapper.java +++ b/PublicComponent/src/main/java/com/arialyy/aria/core/wrapper/RecordWrapper.java @@ -13,8 +13,10 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.arialyy.aria.core.common; +package com.arialyy.aria.core.wrapper; +import com.arialyy.aria.core.TaskRecord; +import com.arialyy.aria.core.ThreadRecord; import com.arialyy.aria.orm.AbsDbWrapper; import com.arialyy.aria.orm.annotation.Many; import com.arialyy.aria.orm.annotation.One; diff --git a/Aria/src/main/java/com/arialyy/aria/exception/AriaIOException.java b/PublicComponent/src/main/java/com/arialyy/aria/exception/AriaIOException.java similarity index 100% rename from Aria/src/main/java/com/arialyy/aria/exception/AriaIOException.java rename to PublicComponent/src/main/java/com/arialyy/aria/exception/AriaIOException.java diff --git a/Aria/src/main/java/com/arialyy/aria/exception/BaseException.java b/PublicComponent/src/main/java/com/arialyy/aria/exception/BaseException.java similarity index 100% rename from Aria/src/main/java/com/arialyy/aria/exception/BaseException.java rename to PublicComponent/src/main/java/com/arialyy/aria/exception/BaseException.java diff --git a/aria/src/main/java/com/arialyy/aria/exception/FileException.java b/PublicComponent/src/main/java/com/arialyy/aria/exception/FileException.java similarity index 100% rename from aria/src/main/java/com/arialyy/aria/exception/FileException.java rename to PublicComponent/src/main/java/com/arialyy/aria/exception/FileException.java diff --git a/Aria/src/main/java/com/arialyy/aria/exception/FileNotFoundException.java b/PublicComponent/src/main/java/com/arialyy/aria/exception/FileNotFoundException.java similarity index 100% rename from Aria/src/main/java/com/arialyy/aria/exception/FileNotFoundException.java rename to PublicComponent/src/main/java/com/arialyy/aria/exception/FileNotFoundException.java diff --git a/Aria/src/main/java/com/arialyy/aria/exception/M3U8Exception.java b/PublicComponent/src/main/java/com/arialyy/aria/exception/M3U8Exception.java similarity index 100% rename from Aria/src/main/java/com/arialyy/aria/exception/M3U8Exception.java rename to PublicComponent/src/main/java/com/arialyy/aria/exception/M3U8Exception.java diff --git a/Aria/src/main/java/com/arialyy/aria/exception/ParamException.java b/PublicComponent/src/main/java/com/arialyy/aria/exception/ParamException.java similarity index 100% rename from Aria/src/main/java/com/arialyy/aria/exception/ParamException.java rename to PublicComponent/src/main/java/com/arialyy/aria/exception/ParamException.java diff --git a/Aria/src/main/java/com/arialyy/aria/exception/TaskException.java b/PublicComponent/src/main/java/com/arialyy/aria/exception/TaskException.java similarity index 100% rename from Aria/src/main/java/com/arialyy/aria/exception/TaskException.java rename to PublicComponent/src/main/java/com/arialyy/aria/exception/TaskException.java diff --git a/Aria/src/main/java/com/arialyy/aria/orm/AbsDbWrapper.java b/PublicComponent/src/main/java/com/arialyy/aria/orm/AbsDbWrapper.java similarity index 100% rename from Aria/src/main/java/com/arialyy/aria/orm/AbsDbWrapper.java rename to PublicComponent/src/main/java/com/arialyy/aria/orm/AbsDbWrapper.java diff --git a/Aria/src/main/java/com/arialyy/aria/orm/AbsDelegate.java b/PublicComponent/src/main/java/com/arialyy/aria/orm/AbsDelegate.java similarity index 100% rename from Aria/src/main/java/com/arialyy/aria/orm/AbsDelegate.java rename to PublicComponent/src/main/java/com/arialyy/aria/orm/AbsDelegate.java diff --git a/Aria/src/main/java/com/arialyy/aria/orm/ActionPolicy.java b/PublicComponent/src/main/java/com/arialyy/aria/orm/ActionPolicy.java similarity index 100% rename from Aria/src/main/java/com/arialyy/aria/orm/ActionPolicy.java rename to PublicComponent/src/main/java/com/arialyy/aria/orm/ActionPolicy.java diff --git a/Aria/src/main/java/com/arialyy/aria/orm/DBConfig.java b/PublicComponent/src/main/java/com/arialyy/aria/orm/DBConfig.java similarity index 84% rename from Aria/src/main/java/com/arialyy/aria/orm/DBConfig.java rename to PublicComponent/src/main/java/com/arialyy/aria/orm/DBConfig.java index fae9bb31..d74f02dc 100644 --- a/Aria/src/main/java/com/arialyy/aria/orm/DBConfig.java +++ b/PublicComponent/src/main/java/com/arialyy/aria/orm/DBConfig.java @@ -16,16 +16,12 @@ package com.arialyy.aria.orm; import android.text.TextUtils; -import com.arialyy.aria.core.common.TaskRecord; -import com.arialyy.aria.core.common.ThreadRecord; +import com.arialyy.aria.core.TaskRecord; +import com.arialyy.aria.core.ThreadRecord; import com.arialyy.aria.core.download.DownloadEntity; import com.arialyy.aria.core.download.DownloadGroupEntity; -import com.arialyy.aria.core.download.DGTaskWrapper; -import com.arialyy.aria.core.download.DTaskWrapper; -import com.arialyy.aria.core.download.m3u8.M3U8Entity; +import com.arialyy.aria.core.download.M3U8Entity; import com.arialyy.aria.core.upload.UploadEntity; -import com.arialyy.aria.core.upload.UTaskWrapper; -import java.util.HashMap; import java.util.LinkedHashMap; import java.util.Map; diff --git a/Aria/src/main/java/com/arialyy/aria/orm/DatabaseContext.java b/PublicComponent/src/main/java/com/arialyy/aria/orm/DatabaseContext.java similarity index 100% rename from Aria/src/main/java/com/arialyy/aria/orm/DatabaseContext.java rename to PublicComponent/src/main/java/com/arialyy/aria/orm/DatabaseContext.java diff --git a/Aria/src/main/java/com/arialyy/aria/orm/DbEntity.java b/PublicComponent/src/main/java/com/arialyy/aria/orm/DbEntity.java similarity index 100% rename from Aria/src/main/java/com/arialyy/aria/orm/DbEntity.java rename to PublicComponent/src/main/java/com/arialyy/aria/orm/DbEntity.java diff --git a/Aria/src/main/java/com/arialyy/aria/orm/DelegateCommon.java b/PublicComponent/src/main/java/com/arialyy/aria/orm/DelegateCommon.java similarity index 98% rename from Aria/src/main/java/com/arialyy/aria/orm/DelegateCommon.java rename to PublicComponent/src/main/java/com/arialyy/aria/orm/DelegateCommon.java index 3408a8af..2fc2dcdd 100644 --- a/Aria/src/main/java/com/arialyy/aria/orm/DelegateCommon.java +++ b/PublicComponent/src/main/java/com/arialyy/aria/orm/DelegateCommon.java @@ -21,7 +21,6 @@ import android.text.TextUtils; import com.arialyy.aria.orm.annotation.Default; import com.arialyy.aria.orm.annotation.Foreign; import com.arialyy.aria.orm.annotation.Primary; -import com.arialyy.aria.util.CheckUtil; import com.arialyy.aria.util.CommonUtil; import java.lang.reflect.Field; import java.util.ArrayList; @@ -106,7 +105,7 @@ class DelegateCommon extends AbsDelegate { */ boolean checkDataExist(SQLiteDatabase db, Class clazz, String... expression) { db = checkDb(db); - if (!CheckUtil.checkSqlExpression(expression)) { + if (!CommonUtil.checkSqlExpression(expression)) { return false; } String sql = String.format("SELECT rowid, * FROM %s WHERE %s ", CommonUtil.getClassName(clazz), diff --git a/Aria/src/main/java/com/arialyy/aria/orm/DelegateFind.java b/PublicComponent/src/main/java/com/arialyy/aria/orm/DelegateFind.java similarity index 99% rename from Aria/src/main/java/com/arialyy/aria/orm/DelegateFind.java rename to PublicComponent/src/main/java/com/arialyy/aria/orm/DelegateFind.java index a008628f..6fdd6695 100644 --- a/Aria/src/main/java/com/arialyy/aria/orm/DelegateFind.java +++ b/PublicComponent/src/main/java/com/arialyy/aria/orm/DelegateFind.java @@ -23,7 +23,6 @@ import com.arialyy.aria.orm.annotation.Many; import com.arialyy.aria.orm.annotation.One; import com.arialyy.aria.orm.annotation.Wrapper; import com.arialyy.aria.util.ALog; -import com.arialyy.aria.util.CheckUtil; import com.arialyy.aria.util.CommonUtil; import java.lang.reflect.Field; import java.net.URLDecoder; @@ -198,7 +197,7 @@ class DelegateFind extends AbsDelegate { .append(cTableName.concat(".").concat(m.entityColumn())); String sql; if (expression != null && expression.length > 0) { - if (!CheckUtil.checkSqlExpression(expression)) { + if (!CommonUtil.checkSqlExpression(expression)) { return null; } sb.append(" WHERE ").append(expression[0]).append(" "); @@ -320,7 +319,7 @@ class DelegateFind extends AbsDelegate { */ List findData(SQLiteDatabase db, Class clazz, String... expression) { db = checkDb(db); - if (!CheckUtil.checkSqlExpression(expression)) { + if (!CommonUtil.checkSqlExpression(expression)) { return null; } String sql = String.format("SELECT rowid, * FROM %s WHERE %s", CommonUtil.getClassName(clazz), @@ -346,7 +345,7 @@ class DelegateFind extends AbsDelegate { return null; } db = checkDb(db); - if (!CheckUtil.checkSqlExpression(expression)) { + if (!CommonUtil.checkSqlExpression(expression)) { return null; } String sql = String.format("SELECT rowid, * FROM %s WHERE %s LIMIT %s,%s", diff --git a/Aria/src/main/java/com/arialyy/aria/orm/DelegateManager.java b/PublicComponent/src/main/java/com/arialyy/aria/orm/DelegateManager.java similarity index 100% rename from Aria/src/main/java/com/arialyy/aria/orm/DelegateManager.java rename to PublicComponent/src/main/java/com/arialyy/aria/orm/DelegateManager.java diff --git a/Aria/src/main/java/com/arialyy/aria/orm/DelegateUpdate.java b/PublicComponent/src/main/java/com/arialyy/aria/orm/DelegateUpdate.java similarity index 98% rename from Aria/src/main/java/com/arialyy/aria/orm/DelegateUpdate.java rename to PublicComponent/src/main/java/com/arialyy/aria/orm/DelegateUpdate.java index 669e0280..9835b250 100644 --- a/Aria/src/main/java/com/arialyy/aria/orm/DelegateUpdate.java +++ b/PublicComponent/src/main/java/com/arialyy/aria/orm/DelegateUpdate.java @@ -20,7 +20,6 @@ import android.database.sqlite.SQLiteDatabase; import android.text.TextUtils; import com.arialyy.aria.orm.annotation.Primary; import com.arialyy.aria.util.ALog; -import com.arialyy.aria.util.CheckUtil; import com.arialyy.aria.util.CommonUtil; import java.lang.reflect.Field; import java.lang.reflect.Type; @@ -40,7 +39,7 @@ class DelegateUpdate extends AbsDelegate { synchronized void delData(SQLiteDatabase db, Class clazz, String... expression) { db = checkDb(db); - if (!CheckUtil.checkSqlExpression(expression)) { + if (!CommonUtil.checkSqlExpression(expression)) { return; } diff --git a/Aria/src/main/java/com/arialyy/aria/orm/DelegateWrapper.java b/PublicComponent/src/main/java/com/arialyy/aria/orm/DelegateWrapper.java similarity index 100% rename from Aria/src/main/java/com/arialyy/aria/orm/DelegateWrapper.java rename to PublicComponent/src/main/java/com/arialyy/aria/orm/DelegateWrapper.java diff --git a/Aria/src/main/java/com/arialyy/aria/orm/SqlHelper.java b/PublicComponent/src/main/java/com/arialyy/aria/orm/SqlHelper.java similarity index 100% rename from Aria/src/main/java/com/arialyy/aria/orm/SqlHelper.java rename to PublicComponent/src/main/java/com/arialyy/aria/orm/SqlHelper.java diff --git a/Aria/src/main/java/com/arialyy/aria/orm/SqlUtil.java b/PublicComponent/src/main/java/com/arialyy/aria/orm/SqlUtil.java similarity index 100% rename from Aria/src/main/java/com/arialyy/aria/orm/SqlUtil.java rename to PublicComponent/src/main/java/com/arialyy/aria/orm/SqlUtil.java diff --git a/Aria/src/main/java/com/arialyy/aria/orm/annotation/Default.java b/PublicComponent/src/main/java/com/arialyy/aria/orm/annotation/Default.java similarity index 100% rename from Aria/src/main/java/com/arialyy/aria/orm/annotation/Default.java rename to PublicComponent/src/main/java/com/arialyy/aria/orm/annotation/Default.java diff --git a/Aria/src/main/java/com/arialyy/aria/orm/annotation/Foreign.java b/PublicComponent/src/main/java/com/arialyy/aria/orm/annotation/Foreign.java similarity index 100% rename from Aria/src/main/java/com/arialyy/aria/orm/annotation/Foreign.java rename to PublicComponent/src/main/java/com/arialyy/aria/orm/annotation/Foreign.java diff --git a/Aria/src/main/java/com/arialyy/aria/orm/annotation/Ignore.java b/PublicComponent/src/main/java/com/arialyy/aria/orm/annotation/Ignore.java similarity index 100% rename from Aria/src/main/java/com/arialyy/aria/orm/annotation/Ignore.java rename to PublicComponent/src/main/java/com/arialyy/aria/orm/annotation/Ignore.java diff --git a/Aria/src/main/java/com/arialyy/aria/orm/annotation/Many.java b/PublicComponent/src/main/java/com/arialyy/aria/orm/annotation/Many.java similarity index 100% rename from Aria/src/main/java/com/arialyy/aria/orm/annotation/Many.java rename to PublicComponent/src/main/java/com/arialyy/aria/orm/annotation/Many.java diff --git a/Aria/src/main/java/com/arialyy/aria/orm/annotation/NoNull.java b/PublicComponent/src/main/java/com/arialyy/aria/orm/annotation/NoNull.java similarity index 100% rename from Aria/src/main/java/com/arialyy/aria/orm/annotation/NoNull.java rename to PublicComponent/src/main/java/com/arialyy/aria/orm/annotation/NoNull.java diff --git a/Aria/src/main/java/com/arialyy/aria/orm/annotation/One.java b/PublicComponent/src/main/java/com/arialyy/aria/orm/annotation/One.java similarity index 100% rename from Aria/src/main/java/com/arialyy/aria/orm/annotation/One.java rename to PublicComponent/src/main/java/com/arialyy/aria/orm/annotation/One.java diff --git a/Aria/src/main/java/com/arialyy/aria/orm/annotation/Primary.java b/PublicComponent/src/main/java/com/arialyy/aria/orm/annotation/Primary.java similarity index 100% rename from Aria/src/main/java/com/arialyy/aria/orm/annotation/Primary.java rename to PublicComponent/src/main/java/com/arialyy/aria/orm/annotation/Primary.java diff --git a/Aria/src/main/java/com/arialyy/aria/orm/annotation/Table.java b/PublicComponent/src/main/java/com/arialyy/aria/orm/annotation/Table.java similarity index 100% rename from Aria/src/main/java/com/arialyy/aria/orm/annotation/Table.java rename to PublicComponent/src/main/java/com/arialyy/aria/orm/annotation/Table.java diff --git a/Aria/src/main/java/com/arialyy/aria/orm/annotation/Unique.java b/PublicComponent/src/main/java/com/arialyy/aria/orm/annotation/Unique.java similarity index 100% rename from Aria/src/main/java/com/arialyy/aria/orm/annotation/Unique.java rename to PublicComponent/src/main/java/com/arialyy/aria/orm/annotation/Unique.java diff --git a/Aria/src/main/java/com/arialyy/aria/orm/annotation/Wrapper.java b/PublicComponent/src/main/java/com/arialyy/aria/orm/annotation/Wrapper.java similarity index 100% rename from Aria/src/main/java/com/arialyy/aria/orm/annotation/Wrapper.java rename to PublicComponent/src/main/java/com/arialyy/aria/orm/annotation/Wrapper.java diff --git a/Aria/src/main/java/com/arialyy/aria/util/ALog.java b/PublicComponent/src/main/java/com/arialyy/aria/util/ALog.java similarity index 100% rename from Aria/src/main/java/com/arialyy/aria/util/ALog.java rename to PublicComponent/src/main/java/com/arialyy/aria/util/ALog.java diff --git a/Aria/src/main/java/com/arialyy/aria/util/AriaCrashHandler.java b/PublicComponent/src/main/java/com/arialyy/aria/util/AriaCrashHandler.java similarity index 100% rename from Aria/src/main/java/com/arialyy/aria/util/AriaCrashHandler.java rename to PublicComponent/src/main/java/com/arialyy/aria/util/AriaCrashHandler.java diff --git a/Aria/src/main/java/com/arialyy/aria/core/common/BandwidthLimiter.java b/PublicComponent/src/main/java/com/arialyy/aria/util/BandwidthLimiter.java similarity index 98% rename from Aria/src/main/java/com/arialyy/aria/core/common/BandwidthLimiter.java rename to PublicComponent/src/main/java/com/arialyy/aria/util/BandwidthLimiter.java index b42a58ff..fff1e553 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/common/BandwidthLimiter.java +++ b/PublicComponent/src/main/java/com/arialyy/aria/util/BandwidthLimiter.java @@ -15,7 +15,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.arialyy.aria.core.common; +package com.arialyy.aria.util; /** * 速度限制 diff --git a/Aria/src/main/java/com/arialyy/aria/util/BufferedRandomAccessFile.java b/PublicComponent/src/main/java/com/arialyy/aria/util/BufferedRandomAccessFile.java similarity index 100% rename from Aria/src/main/java/com/arialyy/aria/util/BufferedRandomAccessFile.java rename to PublicComponent/src/main/java/com/arialyy/aria/util/BufferedRandomAccessFile.java diff --git a/Aria/src/main/java/com/arialyy/aria/util/CheckUtil.java b/PublicComponent/src/main/java/com/arialyy/aria/util/CheckUtil.java similarity index 85% rename from Aria/src/main/java/com/arialyy/aria/util/CheckUtil.java rename to PublicComponent/src/main/java/com/arialyy/aria/util/CheckUtil.java index ef291be5..345c6560 100644 --- a/Aria/src/main/java/com/arialyy/aria/util/CheckUtil.java +++ b/PublicComponent/src/main/java/com/arialyy/aria/util/CheckUtil.java @@ -19,15 +19,12 @@ package com.arialyy.aria.util; import android.text.TextUtils; import com.arialyy.aria.core.download.DTaskWrapper; import com.arialyy.aria.core.download.DownloadEntity; -import com.arialyy.aria.core.inf.AbsTaskWrapper; +import com.arialyy.aria.core.wrapper.AbsTaskWrapper; import com.arialyy.aria.core.upload.UTaskWrapper; import com.arialyy.aria.core.upload.UploadEntity; import com.arialyy.aria.exception.ParamException; import java.io.File; -import java.util.Arrays; import java.util.List; -import java.util.regex.Matcher; -import java.util.regex.Pattern; /** * Created by Lyy on 2016/9/23. @@ -67,42 +64,6 @@ public class CheckUtil { if (page < 1 || num < 1) throw new NullPointerException("page和num不能小于1"); } - /** - * 检查sql的expression是否合法 - * - * @return false 不合法 - */ - public static boolean checkSqlExpression(String... expression) { - if (expression.length == 0) { - ALog.e(TAG, "sql语句表达式不能为null"); - return false; - } - if (expression.length == 1) { - ALog.e(TAG, String.format("表达式需要写入参数,参数信息:%s", Arrays.toString(expression))); - return false; - } - String where = expression[0]; - if (!where.contains("?")) { - ALog.e(TAG, String.format("请在where语句的'='后编写?,参数信息:%s", Arrays.toString(expression))); - return false; - } - Pattern pattern = Pattern.compile("\\?"); - Matcher matcher = pattern.matcher(where); - int count = 0; - while (matcher.find()) { - count++; - } - if (count < expression.length - 1) { - ALog.e(TAG, String.format("条件语句的?个数不能小于参数个数,参数信息:%s", Arrays.toString(expression))); - return false; - } - if (count > expression.length - 1) { - ALog.e(TAG, String.format("条件语句的?个数不能大于参数个数, 参数信息:%s", Arrays.toString(expression))); - return false; - } - return true; - } - /** * 检查下载实体 */ diff --git a/Aria/src/main/java/com/arialyy/aria/util/CommonUtil.java b/PublicComponent/src/main/java/com/arialyy/aria/util/CommonUtil.java similarity index 91% rename from Aria/src/main/java/com/arialyy/aria/util/CommonUtil.java rename to PublicComponent/src/main/java/com/arialyy/aria/util/CommonUtil.java index 1d0871c3..d2713dc6 100644 --- a/Aria/src/main/java/com/arialyy/aria/util/CommonUtil.java +++ b/PublicComponent/src/main/java/com/arialyy/aria/util/CommonUtil.java @@ -21,17 +21,17 @@ import android.content.Intent; import android.content.SharedPreferences; import android.net.Uri; import android.os.Environment; +import android.os.Handler; import android.text.TextUtils; import android.util.Base64; -import com.arialyy.aria.core.AriaManager; -import com.arialyy.aria.core.command.AbsGroupCmd; -import com.arialyy.aria.core.command.AbsNormalCmd; -import com.arialyy.aria.core.command.GroupCmdFactory; -import com.arialyy.aria.core.command.NormalCmdFactory; -import com.arialyy.aria.core.common.ftp.FtpUrlEntity; -import com.arialyy.aria.core.inf.AbsGroupTaskWrapper; -import com.arialyy.aria.core.inf.AbsTaskWrapper; -import com.arialyy.aria.core.inf.ITask; +import com.arialyy.aria.core.AriaConfig; +import com.arialyy.aria.core.FtpUrlEntity; +import com.arialyy.aria.core.TaskOptionParams; +import com.arialyy.aria.core.inf.IEventHandler; +import com.arialyy.aria.core.inf.ITaskOption; +import com.arialyy.aria.core.listener.IEventListener; +import com.arialyy.aria.core.task.ITask; +import com.arialyy.aria.core.wrapper.ITaskWrapper; import dalvik.system.DexFile; import java.io.File; import java.io.FileFilter; @@ -41,6 +41,7 @@ import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.UnsupportedEncodingException; +import java.lang.ref.SoftReference; import java.lang.reflect.Field; import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; @@ -50,6 +51,7 @@ import java.net.URLEncoder; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collections; import java.util.Enumeration; import java.util.HashSet; @@ -67,6 +69,43 @@ public class CommonUtil { public static final String SERVER_CHARSET = "ISO-8859-1"; private static long lastClickTime; + + /** + * 检查sql的expression是否合法 + * + * @return false 不合法 + */ + public static boolean checkSqlExpression(String... expression) { + if (expression.length == 0) { + ALog.e(TAG, "sql语句表达式不能为null"); + return false; + } + if (expression.length == 1) { + ALog.e(TAG, String.format("表达式需要写入参数,参数信息:%s", Arrays.toString(expression))); + return false; + } + String where = expression[0]; + if (!where.contains("?")) { + ALog.e(TAG, String.format("请在where语句的'='后编写?,参数信息:%s", Arrays.toString(expression))); + return false; + } + Pattern pattern = Pattern.compile("\\?"); + Matcher matcher = pattern.matcher(where); + int count = 0; + while (matcher.find()) { + count++; + } + if (count < expression.length - 1) { + ALog.e(TAG, String.format("条件语句的?个数不能小于参数个数,参数信息:%s", Arrays.toString(expression))); + return false; + } + if (count > expression.length - 1) { + ALog.e(TAG, String.format("条件语句的?个数不能大于参数个数, 参数信息:%s", Arrays.toString(expression))); + return false; + } + return true; + } + /** * 是否是快速点击,500ms内快速点击无效 * @@ -543,26 +582,6 @@ public class CommonUtil { } } - /** - * 创建任务命令 - * - * @param taskType {@link ITask#DOWNLOAD}、{@link ITask#DOWNLOAD_GROUP}、{@link ITask#UPLOAD} - */ - public static AbsNormalCmd createNormalCmd(T entity, int cmd, - int taskType) { - return NormalCmdFactory.getInstance().createCmd(entity, cmd, taskType); - } - - /** - * 创建任务组命令 - * - * @param childUrl 子任务url - */ - public static AbsGroupCmd createGroupCmd(T entity, int cmd, - String childUrl) { - return GroupCmdFactory.getInstance().createCmd(entity, cmd, childUrl); - } - /** * 创建隐性的Intent */ @@ -770,8 +789,8 @@ public class CommonUtil { * @param fileName 文件名 */ public static String getFileConfigPath(boolean isDownload, String fileName) { - return AriaManager.getInstance().getAPP().getFilesDir().getPath() + (isDownload - ? AriaManager.DOWNLOAD_TEMP_DIR - : AriaManager.UPLOAD_TEMP_DIR) + fileName + ".properties"; + return AriaConfig.getInstance().getAPP().getFilesDir().getPath() + (isDownload + ? AriaConfig.DOWNLOAD_TEMP_DIR + : AriaConfig.UPLOAD_TEMP_DIR) + fileName + ".properties"; } } \ No newline at end of file diff --git a/PublicComponent/src/main/java/com/arialyy/aria/util/ComponentUtil.java b/PublicComponent/src/main/java/com/arialyy/aria/util/ComponentUtil.java new file mode 100644 index 00000000..37d93aee --- /dev/null +++ b/PublicComponent/src/main/java/com/arialyy/aria/util/ComponentUtil.java @@ -0,0 +1,243 @@ +/* + * 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.util; + +import android.os.Handler; +import com.arialyy.aria.core.TaskOptionParams; +import com.arialyy.aria.core.inf.IEventHandler; +import com.arialyy.aria.core.inf.ITaskOption; +import com.arialyy.aria.core.inf.IUtil; +import com.arialyy.aria.core.listener.BaseListener; +import com.arialyy.aria.core.listener.IEventListener; +import com.arialyy.aria.core.task.AbsTask; +import com.arialyy.aria.core.task.ITask; +import com.arialyy.aria.core.wrapper.AbsTaskWrapper; +import com.arialyy.aria.core.wrapper.ITaskWrapper; +import java.lang.ref.SoftReference; +import java.lang.reflect.Constructor; +import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; + +/** + * 组件工具,用于跨组件创建对应的工具类 + * + * @Author lyy + * @Date 2019-09-23 + */ +public class ComponentUtil { + public static final int COMPONENT_TYPE_HTTP = 1; + public static final int COMPONENT_TYPE_FTP = 2; + public static final int COMPONENT_TYPE_M3U8 = 3; + + private static volatile ComponentUtil INSTANCE = null; + private String TAG = CommonUtil.getClassName(getClass()); + + private ComponentUtil() { + + } + + public static ComponentUtil getInstance() { + if (INSTANCE == null) { + synchronized (ComponentUtil.class) { + if (INSTANCE == null) { + INSTANCE = new ComponentUtil(); + } + } + } + + return INSTANCE; + } + + /** + * 检查组件是否存在,不存在抛出异常退出 + * + * @param componentType 组件类型{@link #COMPONENT_TYPE_FTP}, {@link #COMPONENT_TYPE_M3U8}, {@link + * #COMPONENT_TYPE_HTTP} + * @return true 组件存在 + */ + public boolean checkComponentExist(int componentType) { + String errorStr = "", className = null; + switch (componentType) { + case COMPONENT_TYPE_M3U8: + className = "com.arialyy.aria.m3u8.M3U8TaskOption"; + errorStr = "m3u8插件不存在,请添加m3u8插件"; + break; + case COMPONENT_TYPE_FTP: + className = "com.arialyy.aria.ftp.FtpTaskOption"; + errorStr = "ftp插件不存在,请添加ftp插件"; + break; + case COMPONENT_TYPE_HTTP: + className = "com.arialyy.aria.http.HttpTaskOption"; + errorStr = "http插件不存在,请添加http插件"; + break; + } + + try { + getClass().getClassLoader().loadClass(className); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + throw new IllegalArgumentException(errorStr); + } + return true; + } + + /** + * 创建下载工具 + * + * @return 返回下载工具,创建失败返回null + */ + public T buildUtil(ITaskWrapper wrapper, IEventListener listener) { + int requestType = wrapper.getRequestType(); + String className = null; + switch (requestType) { + case ITaskWrapper.M3U8_LIVE: + className = "com.arialyy.aria.m3u8.live.M3U8LiveUtil"; + break; + case ITaskWrapper.M3U8_VOD: + className = "com.arialyy.aria.m3u8.vod.M3U8VodUtil"; + break; + case ITaskWrapper.D_FTP: + className = "com.arialyy.aria.ftp.download.FtpDLoaderUtil"; + break; + case ITaskWrapper.D_HTTP: + className = "com.arialyy.aria.http.download.HttpDLoaderUtil"; + break; + case ITaskWrapper.U_FTP: + className = "com.arialyy.aria.ftp.upload.FtpULoaderUtil"; + break; + case ITaskWrapper.U_HTTP: + className = "com.arialyy.aria.http.upload.HttpULoaderUtil"; + break; + case ITaskWrapper.D_FTP_DIR: + className = "com.arialyy.aria.http.download.DGroupLoaderUtil"; + break; + case ITaskWrapper.DG_HTTP: + className = "com.arialyy.aria.ftp.download.FtpDirDLoaderUtil"; + break; + } + if (className == null) { + return null; + } + T util = null; + try { + Class clazz = (Class) getClass().getClassLoader().loadClass(className); + Class[] paramTypes = { AbsTaskWrapper.class, IEventListener.class }; + Object[] params = { wrapper, listener }; + Constructor con = clazz.getConstructor(paramTypes); + util = con.newInstance(params); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } catch (NoSuchMethodException e) { + e.printStackTrace(); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } catch (InstantiationException e) { + e.printStackTrace(); + } catch (InvocationTargetException e) { + e.printStackTrace(); + } + return util; + } + + /** + * 创建任务事件监听 + * + * @param wrapperType 任务类型{@link ITaskWrapper} + * @return 返回事件监听,如果创建失败返回null + */ + public T buildListener(int wrapperType, ITask task, + Handler outHandler) { + String className = null, errorStr = "请添加FTP插件"; + switch (wrapperType) { + case ITaskWrapper.M3U8_LIVE: + case ITaskWrapper.M3U8_VOD: + className = "com.arialyy.aria.m3u8.M3U8Listener"; + errorStr = "请添加m3u8插件"; + break; + case ITaskWrapper.D_FTP: + case ITaskWrapper.D_HTTP: + className = "com.arialyy.aria.core.listener.BaseDListener"; + break; + case ITaskWrapper.U_FTP: + case ITaskWrapper.U_HTTP: + className = "com.arialyy.aria.core.listener.BaseUListener"; + break; + case ITaskWrapper.DG_HTTP: + case ITaskWrapper.D_FTP_DIR: + className = "com.arialyy.aria.core.listener.DownloadGroupListener"; + break; + } + if (className == null) { + return null; + } + T listener = null; + try { + Class clazz = (Class) getClass().getClassLoader().loadClass(className); + Class[] paramTypes = { AbsTask.class, Handler.class }; + Object[] params = { task, outHandler }; + Constructor con = clazz.getConstructor(paramTypes); + listener = con.newInstance(params); + } catch (ClassNotFoundException e) { + throw new IllegalArgumentException(errorStr); + } catch (NoSuchMethodException e) { + e.printStackTrace(); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } catch (InstantiationException e) { + e.printStackTrace(); + } catch (InvocationTargetException e) { + e.printStackTrace(); + } + return listener; + } + + /** + * 构建TaskOption信息 + * + * @param clazz 实现{@link ITaskOption}接口的配置信息类 + * @param params 任务配置信息参数 + * @return 构建失败,返回null + */ + public T buildTaskOption(Class clazz, TaskOptionParams params) { + Field[] fields = CommonUtil.getFields(clazz); + T taskOption = null; + try { + taskOption = clazz.newInstance(); + for (Field field : fields) { + Class type = field.getType(); + String key = field.getName(); + if (type != SoftReference.class) { + Object obj = params.getParams().get(key); + if (obj == null) { + continue; + } + field.set(taskOption, obj); + } else { + IEventHandler handler = params.getHandler().get(key); + if (handler == null) { + continue; + } + field.set(taskOption, new SoftReference<>(handler)); + } + } + } catch (IllegalAccessException e) { + e.printStackTrace(); + } catch (InstantiationException e) { + e.printStackTrace(); + } + return taskOption; + } +} diff --git a/Aria/src/main/java/com/arialyy/aria/util/DbDataHelper.java b/PublicComponent/src/main/java/com/arialyy/aria/util/DbDataHelper.java similarity index 97% rename from Aria/src/main/java/com/arialyy/aria/util/DbDataHelper.java rename to PublicComponent/src/main/java/com/arialyy/aria/util/DbDataHelper.java index 95cb5789..d9382dd4 100644 --- a/Aria/src/main/java/com/arialyy/aria/util/DbDataHelper.java +++ b/PublicComponent/src/main/java/com/arialyy/aria/util/DbDataHelper.java @@ -15,8 +15,8 @@ */ package com.arialyy.aria.util; -import com.arialyy.aria.core.common.RecordWrapper; -import com.arialyy.aria.core.common.TaskRecord; +import com.arialyy.aria.core.wrapper.RecordWrapper; +import com.arialyy.aria.core.TaskRecord; import com.arialyy.aria.core.download.DGEntityWrapper; import com.arialyy.aria.core.download.DTaskWrapper; import com.arialyy.aria.core.download.DownloadEntity; diff --git a/Aria/src/main/java/com/arialyy/aria/util/ErrorHelp.java b/PublicComponent/src/main/java/com/arialyy/aria/util/ErrorHelp.java similarity index 96% rename from Aria/src/main/java/com/arialyy/aria/util/ErrorHelp.java rename to PublicComponent/src/main/java/com/arialyy/aria/util/ErrorHelp.java index 2fbee6a1..a89b933e 100644 --- a/Aria/src/main/java/com/arialyy/aria/util/ErrorHelp.java +++ b/PublicComponent/src/main/java/com/arialyy/aria/util/ErrorHelp.java @@ -16,7 +16,7 @@ package com.arialyy.aria.util; import android.annotation.SuppressLint; -import com.arialyy.aria.core.AriaManager; +import com.arialyy.aria.core.AriaConfig; import java.io.File; import java.io.FileWriter; import java.io.IOException; @@ -47,7 +47,7 @@ public class ErrorHelp { */ private static String getLogPath() { String path = String.format("%slog/AriaCrash_%s.log", - CommonUtil.getAppPath(AriaManager.getInstance().getAPP()), + CommonUtil.getAppPath(AriaConfig.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/PublicComponent/src/main/java/com/arialyy/aria/util/FileUtil.java similarity index 99% rename from Aria/src/main/java/com/arialyy/aria/util/FileUtil.java rename to PublicComponent/src/main/java/com/arialyy/aria/util/FileUtil.java index 937caba2..424c4679 100644 --- a/Aria/src/main/java/com/arialyy/aria/util/FileUtil.java +++ b/PublicComponent/src/main/java/com/arialyy/aria/util/FileUtil.java @@ -25,7 +25,7 @@ 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 com.arialyy.aria.core.AriaConfig; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; @@ -364,7 +364,7 @@ public class FileUtil { * @return {@code false} 内存空间不足,{@code true}内存空间足够 */ public static boolean checkSDMemorySpace(String filePath, long fileSize) { - List dirs = FileUtil.getSDPathList(AriaManager.getInstance().getAPP()); + List dirs = FileUtil.getSDPathList(AriaConfig.getInstance().getAPP()); if (dirs == null || dirs.isEmpty()) { return true; } @@ -546,7 +546,7 @@ public class FileUtil { /** * Raturns all available SD-Cards in the system (include emulated) Warning: Hack! Based on Android * source code of version 4.3 - * (API 18) Because there is no standard way to get it. TODO: Test on future Android versions + * (API 18) Because there is no standard way to get it. * 4.2+ * * @return paths to all available SD-Cards in the system (include emulated) diff --git a/Aria/src/main/java/com/arialyy/aria/util/NetUtils.java b/PublicComponent/src/main/java/com/arialyy/aria/util/NetUtils.java similarity index 96% rename from Aria/src/main/java/com/arialyy/aria/util/NetUtils.java rename to PublicComponent/src/main/java/com/arialyy/aria/util/NetUtils.java index 2200f03a..dac9de4d 100644 --- a/Aria/src/main/java/com/arialyy/aria/util/NetUtils.java +++ b/PublicComponent/src/main/java/com/arialyy/aria/util/NetUtils.java @@ -23,8 +23,7 @@ import android.net.NetworkInfo; import android.os.Build; import android.telephony.TelephonyManager; import android.text.TextUtils; -import com.arialyy.aria.core.Aria; -import com.arialyy.aria.core.AriaManager; +import com.arialyy.aria.core.AriaConfig; /** * 跟网络相关的工具类 @@ -58,11 +57,11 @@ public class NetUtils { * @return {@code true} 网络已连接、{@code false}网络未连接 */ public static boolean isConnected(Context context) { - if (!Aria.get(context).getAppConfig().isNetCheck()) { + if (!AriaConfig.getInstance().getAConfig().isNetCheck()) { return true; } if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { - return AriaManager.getInstance().isConnectedNet(); + return AriaConfig.getInstance().isConnectedNet(); } ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); diff --git a/Aria/src/main/java/com/arialyy/aria/util/RecordUtil.java b/PublicComponent/src/main/java/com/arialyy/aria/util/RecordUtil.java similarity index 89% rename from Aria/src/main/java/com/arialyy/aria/util/RecordUtil.java rename to PublicComponent/src/main/java/com/arialyy/aria/util/RecordUtil.java index 5f83d08c..ce8a9b58 100644 --- a/Aria/src/main/java/com/arialyy/aria/util/RecordUtil.java +++ b/PublicComponent/src/main/java/com/arialyy/aria/util/RecordUtil.java @@ -16,15 +16,15 @@ package com.arialyy.aria.util; import android.text.TextUtils; -import com.arialyy.aria.core.common.RecordHandler; -import com.arialyy.aria.core.common.RecordWrapper; -import com.arialyy.aria.core.common.TaskRecord; -import com.arialyy.aria.core.common.ThreadRecord; +import com.arialyy.aria.core.inf.IRecordHandler; +import com.arialyy.aria.core.wrapper.RecordWrapper; +import com.arialyy.aria.core.TaskRecord; +import com.arialyy.aria.core.ThreadRecord; import com.arialyy.aria.core.download.DownloadEntity; import com.arialyy.aria.core.download.DownloadGroupEntity; -import com.arialyy.aria.core.download.m3u8.M3U8Entity; -import com.arialyy.aria.core.inf.AbsEntity; -import com.arialyy.aria.core.inf.AbsNormalEntity; +import com.arialyy.aria.core.download.M3U8Entity; +import com.arialyy.aria.core.common.AbsEntity; +import com.arialyy.aria.core.common.AbsNormalEntity; import com.arialyy.aria.core.upload.UploadEntity; import com.arialyy.aria.orm.DbEntity; import java.io.File; @@ -78,7 +78,7 @@ public class RecordUtil { if (record.taskRecord.isBlock) { for (int i = 0, len = record.taskRecord.threadNum; i < len; i++) { File partFile = - new File(String.format(RecordHandler.SUB_PATH, record.taskRecord.filePath, i)); + new File(String.format(IRecordHandler.SUB_PATH, record.taskRecord.filePath, i)); if (partFile.exists()) { partFile.delete(); } @@ -124,10 +124,10 @@ public class RecordUtil { String filePath; int type; if (entity instanceof DownloadEntity) { - type = RecordHandler.TYPE_DOWNLOAD; + type = IRecordHandler.TYPE_DOWNLOAD; filePath = ((DownloadEntity) entity).getDownloadPath(); } else if (entity instanceof UploadEntity) { - type = RecordHandler.TYPE_UPLOAD; + type = IRecordHandler.TYPE_UPLOAD; filePath = ((UploadEntity) entity).getFilePath(); } else { ALog.w(TAG, "删除记录失败,未知类型"); @@ -171,7 +171,7 @@ public class RecordUtil { * @param removeFile {@code true} 无论任务是否完成,都会删除记录和文件; * {@code false} 如果是下载任务,并且任务已经完成,则只删除记录,不删除文件;任务未完成,记录和文件都会删除。 * 如果是上传任务,无论任务是否完成,都只删除记录 - * @param type {@link RecordHandler#TYPE_DOWNLOAD}下载任务的记录,{@link RecordHandler#TYPE_UPLOAD} + * @param type {@link IRecordHandler#TYPE_DOWNLOAD}下载任务的记录,{@link IRecordHandler#TYPE_UPLOAD} * 上传任务的记录 * @param removeEntity {@code true} 删除任务实体, */ @@ -183,12 +183,12 @@ public class RecordUtil { if (!filePath.startsWith("/")) { throw new IllegalArgumentException(String.format("文件路径错误,filePath:%s", filePath)); } - if (type != RecordHandler.TYPE_DOWNLOAD && type != RecordHandler.TYPE_UPLOAD) { + if (type != IRecordHandler.TYPE_DOWNLOAD && type != IRecordHandler.TYPE_UPLOAD) { throw new IllegalArgumentException("任务记录类型错误"); } AbsEntity entity; - if (type == RecordHandler.TYPE_DOWNLOAD) { + if (type == IRecordHandler.TYPE_DOWNLOAD) { entity = DbEntity.findFirst(DownloadEntity.class, "downloadPath=?", filePath); } else { entity = DbEntity.findFirst(UploadEntity.class, "filePath=?", filePath); @@ -245,11 +245,11 @@ public class RecordUtil { /** * 删除实体 * - * @param type {@link RecordHandler#TYPE_DOWNLOAD}下载任务的记录,{@link RecordHandler#TYPE_UPLOAD} + * @param type {@link IRecordHandler#TYPE_DOWNLOAD}下载任务的记录,{@link IRecordHandler#TYPE_UPLOAD} * 上传任务的记录 */ private static void removeEntity(int type, String filePath) { - if (type == RecordHandler.TYPE_DOWNLOAD) { + if (type == IRecordHandler.TYPE_DOWNLOAD) { DbEntity.deleteData(DownloadEntity.class, "downloadPath=?", filePath); } else { DbEntity.deleteData(UploadEntity.class, "filePath=?", filePath); @@ -277,7 +277,7 @@ public class RecordUtil { */ private static void removeBlockFile(TaskRecord record) { for (int i = 0, len = record.threadNum; i < len; i++) { - File partFile = new File(String.format(RecordHandler.SUB_PATH, record.filePath, i)); + File partFile = new File(String.format(IRecordHandler.SUB_PATH, record.filePath, i)); if (partFile.exists()) { partFile.delete(); } @@ -317,7 +317,7 @@ public class RecordUtil { * 删除任务记录,默认删除文件,删除任务实体 * * @param filePath 文件路径 - * @param type {@link RecordHandler#TYPE_DOWNLOAD}下载任务的记录,{@link RecordHandler#TYPE_UPLOAD} + * @param type {@link IRecordHandler#TYPE_DOWNLOAD}下载任务的记录,{@link IRecordHandler#TYPE_UPLOAD} * 上传任务的记录 */ public static void delTaskRecord(String filePath, int type) { @@ -355,9 +355,9 @@ public class RecordUtil { if (record.threadRecords != null && !record.threadRecords.isEmpty()) { for (ThreadRecord tr : record.threadRecords) { tr.taskKey = newPath; - File blockFile = new File(String.format(RecordHandler.SUB_PATH, oldPath, tr.threadId)); + File blockFile = new File(String.format(IRecordHandler.SUB_PATH, oldPath, tr.threadId)); if (blockFile.exists()) { - blockFile.renameTo(new File(String.format(RecordHandler.SUB_PATH, newPath, tr.threadId))); + blockFile.renameTo(new File(String.format(IRecordHandler.SUB_PATH, newPath, tr.threadId))); } } DbEntity.updateManyData(record.threadRecords); @@ -371,7 +371,7 @@ public class RecordUtil { * @return {@code true} 分块文件存在 */ public static boolean blockTaskExists(String filePath) { - return new File(String.format(RecordHandler.SUB_PATH, filePath, 0)).exists(); + return new File(String.format(IRecordHandler.SUB_PATH, filePath, 0)).exists(); } /** diff --git a/Aria/src/main/java/com/arialyy/aria/util/Regular.java b/PublicComponent/src/main/java/com/arialyy/aria/util/Regular.java similarity index 100% rename from Aria/src/main/java/com/arialyy/aria/util/Regular.java rename to PublicComponent/src/main/java/com/arialyy/aria/util/Regular.java diff --git a/Aria/src/main/java/com/arialyy/aria/util/SSLContextUtil.java b/PublicComponent/src/main/java/com/arialyy/aria/util/SSLContextUtil.java similarity index 97% rename from Aria/src/main/java/com/arialyy/aria/util/SSLContextUtil.java rename to PublicComponent/src/main/java/com/arialyy/aria/util/SSLContextUtil.java index a44f36ea..78395132 100644 --- a/Aria/src/main/java/com/arialyy/aria/util/SSLContextUtil.java +++ b/PublicComponent/src/main/java/com/arialyy/aria/util/SSLContextUtil.java @@ -16,8 +16,8 @@ package com.arialyy.aria.util; import android.text.TextUtils; -import com.arialyy.aria.core.AriaManager; -import com.arialyy.aria.core.common.ProtocolType; +import com.arialyy.aria.core.AriaConfig; +import com.arialyy.aria.core.ProtocolType; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; @@ -58,7 +58,7 @@ public class SSLContextUtil { */ public static SSLContext getSSLContextFromAssets(String caAlias, String caPath, @ProtocolType String protocol) { - if (TextUtils.isEmpty(caAlias) || TextUtils.isEmpty(caPath)){ + if (TextUtils.isEmpty(caAlias) || TextUtils.isEmpty(caPath)) { return null; } try { @@ -67,7 +67,7 @@ public class SSLContextUtil { if (sslContext != null) { return sslContext; } - InputStream caInput = AriaManager.getInstance().getAPP().getAssets().open(caPath); + InputStream caInput = AriaConfig.getInstance().getAPP().getAssets().open(caPath); Certificate ca = loadCert(caInput); return createContext(caAlias, ca, protocol, cacheKey); } catch (IOException | CertificateException e) { diff --git a/Aria/src/main/java/com/arialyy/aria/util/WeakHandler.java b/PublicComponent/src/main/java/com/arialyy/aria/util/WeakHandler.java similarity index 100% rename from Aria/src/main/java/com/arialyy/aria/util/WeakHandler.java rename to PublicComponent/src/main/java/com/arialyy/aria/util/WeakHandler.java diff --git a/PublicComponent/src/main/res/values/strings.xml b/PublicComponent/src/main/res/values/strings.xml new file mode 100644 index 00000000..568a9247 --- /dev/null +++ b/PublicComponent/src/main/res/values/strings.xml @@ -0,0 +1,3 @@ + + PublicComponent + diff --git a/app/build.gradle b/app/build.gradle index 76cc2bad..7d4bca30 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -60,7 +60,7 @@ dependencies { implementation 'com.google.android.material:material:1.0.0' implementation "org.jetbrains.kotlin:kotlin-stdlib:${rootProject.ext.kotlin_version}" - api project(':Aria') + implementation project(':Aria') kapt project(':AriaCompiler') // api 'com.arialyy.aria:aria-core:3.6.4' // kapt 'com.arialyy.aria:aria-compiler:3.6.4' diff --git a/app/src/main/assets/help_code/FtpUpload.java b/app/src/main/assets/help_code/FtpUpload.java index 48ad3c99..1a977623 100644 --- a/app/src/main/assets/help_code/FtpUpload.java +++ b/app/src/main/assets/help_code/FtpUpload.java @@ -23,7 +23,7 @@ import com.arialyy.aria.core.Aria; import com.arialyy.aria.core.common.ftp.FtpInterceptHandler; import com.arialyy.aria.core.common.ftp.IFtpUploadInterceptor; import com.arialyy.aria.core.upload.UploadEntity; -import com.arialyy.aria.core.upload.UploadTask; +import com.arialyy.aria.core.task.UploadTask; import com.arialyy.frame.util.FileUtil; import com.arialyy.frame.util.show.T; import com.arialyy.simple.R; diff --git a/app/src/main/assets/help_code/KotlinHttpDownload.kt b/app/src/main/assets/help_code/KotlinHttpDownload.kt index 7f3033b9..a1dfe1cb 100644 --- a/app/src/main/assets/help_code/KotlinHttpDownload.kt +++ b/app/src/main/assets/help_code/KotlinHttpDownload.kt @@ -22,8 +22,8 @@ import android.widget.Toast import com.arialyy.annotations.Download import com.arialyy.aria.core.Aria import com.arialyy.aria.core.download.target.HttpNormalTarget -import com.arialyy.aria.core.download.DownloadTask -import com.arialyy.aria.core.inf.IEntity +import com.arialyy.aria.core.task.DownloadTask +import com.arialyy.aria.inf.IEntity import com.arialyy.simple.R import com.arialyy.simple.base.BaseActivity import com.arialyy.simple.databinding.ActivitySingleBinding @@ -59,7 +59,7 @@ class KotlinDownloadActivity : BaseActivity() { target = Aria.download(this) .load(DOWNLOAD_URL) binding.progress = target.percent - if (target.taskState == IEntity.STATE_STOP) { + if (target.taskState == com.arialyy.aria.inf.IEntity.STATE_STOP) { mStart.text = "恢复" } else if (target.isRunning) { mStart.text = "停止" @@ -71,7 +71,7 @@ class KotlinDownloadActivity : BaseActivity() { * 注解方法不能添加internal修饰符,否则会出现e: [kapt] An exception occurred: java.lang.IllegalArgumentException: peerIndex 1 for '$a' not in range (received 0 arguments)错误 */ @Download.onTaskRunning - fun running(task: DownloadTask) { + fun running(task: com.arialyy.aria.core.task.DownloadTask) { Log.d(TAG, task.percent.toString()) val len = task.fileSize if (len == 0L) { @@ -83,40 +83,40 @@ class KotlinDownloadActivity : BaseActivity() { } @Download.onWait - fun onWait(task: DownloadTask) { + fun onWait(task: com.arialyy.aria.core.task.DownloadTask) { if (task.key == DOWNLOAD_URL) { Log.d(TAG, "wait ==> " + task.downloadEntity.fileName) } } @Download.onPre - fun onPre(task: DownloadTask) { + fun onPre(task: com.arialyy.aria.core.task.DownloadTask) { if (task.key == DOWNLOAD_URL) { mStart.text = "停止" } } @Download.onTaskStart - fun taskStart(task: DownloadTask) { + fun taskStart(task: com.arialyy.aria.core.task.DownloadTask) { if (task.key == DOWNLOAD_URL) { binding.fileSize = task.convertFileSize } } @Download.onTaskComplete - fun complete(task: DownloadTask) { + fun complete(task: com.arialyy.aria.core.task.DownloadTask) { Log.d(TAG, "完成") } @Download.onTaskResume - fun taskResume(task: DownloadTask) { + fun taskResume(task: com.arialyy.aria.core.task.DownloadTask) { if (task.key == DOWNLOAD_URL) { mStart.text = "停止" } } @Download.onTaskStop - fun taskStop(task: DownloadTask) { + fun taskStop(task: com.arialyy.aria.core.task.DownloadTask) { if (task.key == DOWNLOAD_URL) { mStart.text = "恢复" binding.speed = "" @@ -124,7 +124,7 @@ class KotlinDownloadActivity : BaseActivity() { } @Download.onTaskCancel - fun taskCancel(task: DownloadTask) { + fun taskCancel(task: com.arialyy.aria.core.task.DownloadTask) { if (task.key == DOWNLOAD_URL) { binding.progress = 0 Toast.makeText(this@KotlinDownloadActivity, "取消下载", Toast.LENGTH_SHORT) @@ -136,7 +136,7 @@ class KotlinDownloadActivity : BaseActivity() { } @Download.onTaskFail - fun taskFail(task: DownloadTask) { + fun taskFail(task: com.arialyy.aria.core.task.DownloadTask) { if (task.key == DOWNLOAD_URL) { Toast.makeText(this@KotlinDownloadActivity, "下载失败", Toast.LENGTH_SHORT) .show() diff --git a/app/src/main/java/com/arialyy/simple/DbTestActivity.java b/app/src/main/java/com/arialyy/simple/DbTestActivity.java index b1dbe3d4..feb15785 100644 --- a/app/src/main/java/com/arialyy/simple/DbTestActivity.java +++ b/app/src/main/java/com/arialyy/simple/DbTestActivity.java @@ -1,12 +1,11 @@ package com.arialyy.simple; import android.os.Bundle; - import android.util.Log; import android.view.View; +import com.arialyy.aria.core.common.AbsEntity; import com.arialyy.aria.core.download.DTaskWrapper; import com.arialyy.aria.core.download.DownloadEntity; -import com.arialyy.aria.core.inf.AbsEntity; import com.arialyy.aria.orm.DbEntity; import com.arialyy.simple.base.BaseActivity; import com.arialyy.simple.databinding.ActivityDbTestBinding; @@ -38,7 +37,7 @@ public class DbTestActivity extends BaseActivity { } } - private void searchAll(){ + private void searchAll() { long startT = System.currentTimeMillis(); //List data = DownloadEntity.findRelationData(DownloadEntity.class); 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 d4075d0e..40899abf 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 @@ -24,7 +24,7 @@ import android.widget.TextView; import com.arialyy.annotations.Download; import com.arialyy.aria.core.Aria; import com.arialyy.aria.core.download.DownloadEntity; -import com.arialyy.aria.core.download.DownloadTask; +import com.arialyy.aria.core.task.DownloadTask; import com.arialyy.aria.util.CommonUtil; import com.arialyy.frame.core.AbsDialog; import com.arialyy.simple.R; diff --git a/app/src/main/java/com/arialyy/simple/core/download/DownloadDialogFragment.java b/app/src/main/java/com/arialyy/simple/core/download/DownloadDialogFragment.java index b6755c18..4a1e5895 100644 --- a/app/src/main/java/com/arialyy/simple/core/download/DownloadDialogFragment.java +++ b/app/src/main/java/com/arialyy/simple/core/download/DownloadDialogFragment.java @@ -8,8 +8,8 @@ import android.widget.Toast; import com.arialyy.annotations.Download; import com.arialyy.aria.core.Aria; import com.arialyy.aria.core.download.DownloadEntity; -import com.arialyy.aria.core.download.DownloadTask; import com.arialyy.aria.core.inf.IEntity; +import com.arialyy.aria.core.task.DownloadTask; import com.arialyy.aria.util.CommonUtil; import com.arialyy.simple.R; import com.arialyy.simple.base.BaseDialog; diff --git a/app/src/main/java/com/arialyy/simple/core/download/DownloadPopupWindow.java b/app/src/main/java/com/arialyy/simple/core/download/DownloadPopupWindow.java index 95e5856d..4c7e2bd6 100644 --- a/app/src/main/java/com/arialyy/simple/core/download/DownloadPopupWindow.java +++ b/app/src/main/java/com/arialyy/simple/core/download/DownloadPopupWindow.java @@ -26,8 +26,8 @@ import android.widget.TextView; import com.arialyy.annotations.Download; import com.arialyy.aria.core.Aria; import com.arialyy.aria.core.download.DownloadEntity; -import com.arialyy.aria.core.download.DownloadTask; import com.arialyy.aria.core.inf.IEntity; +import com.arialyy.aria.core.task.DownloadTask; import com.arialyy.aria.util.CommonUtil; import com.arialyy.frame.core.AbsPopupWindow; import com.arialyy.simple.R; 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 1c9ff07a..94d3a04e 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 @@ -23,11 +23,11 @@ import androidx.lifecycle.Observer; import androidx.lifecycle.ViewModelProviders; import com.arialyy.annotations.Download; import com.arialyy.aria.core.Aria; -import com.arialyy.aria.core.common.ProtocolType; +import com.arialyy.aria.core.ProtocolType; import com.arialyy.aria.core.common.controller.ControllerType; import com.arialyy.aria.core.download.DownloadEntity; -import com.arialyy.aria.core.download.DownloadTask; import com.arialyy.aria.core.inf.IEntity; +import com.arialyy.aria.core.task.DownloadTask; import com.arialyy.aria.util.CommonUtil; import com.arialyy.frame.util.show.L; import com.arialyy.frame.util.show.T; diff --git a/app/src/main/java/com/arialyy/simple/core/download/HighestPriorityActivity.java b/app/src/main/java/com/arialyy/simple/core/download/HighestPriorityActivity.java index dec8a60f..a20803b2 100644 --- a/app/src/main/java/com/arialyy/simple/core/download/HighestPriorityActivity.java +++ b/app/src/main/java/com/arialyy/simple/core/download/HighestPriorityActivity.java @@ -25,10 +25,10 @@ import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; import com.arialyy.annotations.Download; import com.arialyy.aria.core.Aria; +import com.arialyy.aria.core.common.AbsEntity; import com.arialyy.aria.core.download.DownloadEntity; -import com.arialyy.aria.core.download.DownloadTask; -import com.arialyy.aria.core.inf.AbsEntity; import com.arialyy.aria.core.inf.IEntity; +import com.arialyy.aria.core.task.DownloadTask; import com.arialyy.frame.util.show.L; import com.arialyy.simple.R; import com.arialyy.simple.base.BaseActivity; diff --git a/app/src/main/java/com/arialyy/simple/core/download/HttpDownloadModule.java b/app/src/main/java/com/arialyy/simple/core/download/HttpDownloadModule.java index 609ff721..dd449cb9 100644 --- a/app/src/main/java/com/arialyy/simple/core/download/HttpDownloadModule.java +++ b/app/src/main/java/com/arialyy/simple/core/download/HttpDownloadModule.java @@ -33,7 +33,7 @@ public class HttpDownloadModule extends BaseViewModule { private final String HTTP_PATH_KEY = "HTTP_PATH_KEY"; private final String defUrl = - "http://hzdown.muzhiwan.com/2017/05/08/nl.noio.kingdom_59104935e56f0.apk1"; + "http://hzdown.muzhiwan.com/2017/05/08/nl.noio.kingdom_59104935e56f0.apk"; //"http://9.9.9.205:5000/download/Cyberduck-6.9.4.30164.zip"; //"http://202.98.201.103:7000/vrs/TPK/ZTC440402001Z.tpk"; private final String defFilePath = diff --git a/app/src/main/java/com/arialyy/simple/core/download/KotlinDownloadActivity.kt b/app/src/main/java/com/arialyy/simple/core/download/KotlinDownloadActivity.kt index 1550420c..8cf91a69 100644 --- a/app/src/main/java/com/arialyy/simple/core/download/KotlinDownloadActivity.kt +++ b/app/src/main/java/com/arialyy/simple/core/download/KotlinDownloadActivity.kt @@ -30,9 +30,8 @@ import androidx.lifecycle.ViewModelProviders import com.arialyy.annotations.Download import com.arialyy.aria.core.Aria import com.arialyy.aria.core.download.DownloadEntity -import com.arialyy.aria.core.download.DownloadTask import com.arialyy.aria.core.inf.IEntity -import com.arialyy.aria.core.scheduler.ISchedulers +import com.arialyy.aria.core.listener.ISchedulers import com.arialyy.aria.util.ALog import com.arialyy.aria.util.CommonUtil import com.arialyy.frame.util.show.T @@ -59,10 +58,22 @@ class KotlinDownloadActivity : BaseActivity() { intent: Intent ) { if (intent.action == ISchedulers.ARIA_TASK_INFO_ACTION) { - ALog.d(TAG, "state = " + intent.getIntExtra(ISchedulers.TASK_STATE, -1)) - ALog.d(TAG, "type = " + intent.getIntExtra(ISchedulers.TASK_TYPE, -1)) - ALog.d(TAG, "speed = " + intent.getLongExtra(ISchedulers.TASK_SPEED, -1)) - ALog.d(TAG, "percent = " + intent.getIntExtra(ISchedulers.TASK_PERCENT, -1)) + ALog.d( + TAG, + "state = " + intent.getIntExtra(ISchedulers.TASK_STATE, -1) + ) + ALog.d( + TAG, "type = " + intent.getIntExtra(ISchedulers.TASK_TYPE, -1) + ) + ALog.d( + TAG, + "speed = " + intent.getLongExtra(ISchedulers.TASK_SPEED, -1) + ) + ALog.d( + TAG, "percent = " + intent.getIntExtra( + ISchedulers.TASK_PERCENT, -1 + ) + ) ALog.d( TAG, "entity = " + intent.getParcelableExtra( ISchedulers.TASK_ENTITY @@ -167,28 +178,28 @@ class KotlinDownloadActivity : BaseActivity() { } @Download.onWait - fun onWait(task: DownloadTask) { + fun onWait(task: com.arialyy.aria.core.task.DownloadTask) { if (task.key == mUrl) { Log.d(TAG, "wait ==> " + task.downloadEntity.fileName) } } @Download.onPre - fun onPre(task: DownloadTask) { + fun onPre(task: com.arialyy.aria.core.task.DownloadTask) { if (task.key == mUrl) { binding.stateStr = getString(R.string.stop) } } @Download.onTaskStart - fun taskStart(task: DownloadTask) { + fun taskStart(task: com.arialyy.aria.core.task.DownloadTask) { if (task.key == mUrl) { binding.fileSize = task.convertFileSize } } @Download.onTaskRunning - fun running(task: DownloadTask) { + fun running(task: com.arialyy.aria.core.task.DownloadTask) { if (task.key == mUrl) { //Log.d(TAG, task.getKey()); val len = task.fileSize @@ -202,14 +213,14 @@ class KotlinDownloadActivity : BaseActivity() { } @Download.onTaskResume - fun taskResume(task: DownloadTask) { + fun taskResume(task: com.arialyy.aria.core.task.DownloadTask) { if (task.key == mUrl) { binding.stateStr = getString(R.string.stop) } } @Download.onTaskStop - fun taskStop(task: DownloadTask) { + fun taskStop(task: com.arialyy.aria.core.task.DownloadTask) { if (task.key == mUrl) { binding.stateStr = getString(R.string.resume) binding.speed = "" @@ -217,7 +228,7 @@ class KotlinDownloadActivity : BaseActivity() { } @Download.onTaskCancel - fun taskCancel(task: DownloadTask) { + fun taskCancel(task: com.arialyy.aria.core.task.DownloadTask) { if (task.key == mUrl) { binding.progress = 0 binding.stateStr = getString(R.string.start) @@ -231,7 +242,7 @@ class KotlinDownloadActivity : BaseActivity() { */ @Download.onTaskFail fun taskFail( - task: DownloadTask, + task: com.arialyy.aria.core.task.DownloadTask, e: Exception ) { if (task.key == mUrl) { @@ -242,7 +253,7 @@ class KotlinDownloadActivity : BaseActivity() { } @Download.onTaskComplete - fun taskComplete(task: DownloadTask) { + fun taskComplete(task: com.arialyy.aria.core.task.DownloadTask) { if (task.key == mUrl) { binding.progress = 100 diff --git a/app/src/main/java/com/arialyy/simple/core/download/SimpleNotification.java b/app/src/main/java/com/arialyy/simple/core/download/SimpleNotification.java index f3ac70df..ab6e6043 100644 --- a/app/src/main/java/com/arialyy/simple/core/download/SimpleNotification.java +++ b/app/src/main/java/com/arialyy/simple/core/download/SimpleNotification.java @@ -22,7 +22,7 @@ //import android.support.v4.app.NotificationCompat; //import com.arialyy.annotations.Download; //import com.arialyy.aria.core.Aria; -//import com.arialyy.aria.core.download.DownloadTask; +//import com.arialyy.aria.core.task.DownloadTask; //import com.arialyy.simple.R; // ///** 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 151d7fa2..88ec8680 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 @@ -31,12 +31,12 @@ import androidx.lifecycle.Observer; import androidx.lifecycle.ViewModelProviders; import com.arialyy.annotations.Download; import com.arialyy.aria.core.Aria; +import com.arialyy.aria.core.processor.IHttpFileLenAdapter; import com.arialyy.aria.core.common.controller.ControllerType; import com.arialyy.aria.core.download.DownloadEntity; -import com.arialyy.aria.core.download.DownloadTask; import com.arialyy.aria.core.inf.IEntity; -import com.arialyy.aria.core.inf.IHttpFileLenAdapter; -import com.arialyy.aria.core.scheduler.ISchedulers; +import com.arialyy.aria.core.listener.ISchedulers; +import com.arialyy.aria.core.task.DownloadTask; import com.arialyy.aria.util.ALog; import com.arialyy.aria.util.CommonUtil; import com.arialyy.frame.util.show.T; diff --git a/app/src/main/java/com/arialyy/simple/core/download/fragment/DownloadFragment.java b/app/src/main/java/com/arialyy/simple/core/download/fragment/DownloadFragment.java index 994d0295..f65cf865 100644 --- a/app/src/main/java/com/arialyy/simple/core/download/fragment/DownloadFragment.java +++ b/app/src/main/java/com/arialyy/simple/core/download/fragment/DownloadFragment.java @@ -23,8 +23,8 @@ import android.widget.Button; import com.arialyy.annotations.Download; import com.arialyy.aria.core.Aria; import com.arialyy.aria.core.download.DownloadEntity; -import com.arialyy.aria.core.download.DownloadTask; import com.arialyy.aria.core.inf.IEntity; +import com.arialyy.aria.core.task.DownloadTask; import com.arialyy.aria.util.CommonUtil; import com.arialyy.frame.core.AbsFragment; import com.arialyy.simple.R; diff --git a/app/src/main/java/com/arialyy/simple/core/download/group/ChildHandleDialog.java b/app/src/main/java/com/arialyy/simple/core/download/group/ChildHandleDialog.java index c8c2c3c0..944df089 100644 --- a/app/src/main/java/com/arialyy/simple/core/download/group/ChildHandleDialog.java +++ b/app/src/main/java/com/arialyy/simple/core/download/group/ChildHandleDialog.java @@ -27,7 +27,7 @@ import android.widget.TextView; import com.arialyy.annotations.DownloadGroup; import com.arialyy.aria.core.Aria; import com.arialyy.aria.core.download.DownloadEntity; -import com.arialyy.aria.core.download.DownloadGroupTask; +import com.arialyy.aria.core.task.DownloadGroupTask; import com.arialyy.frame.util.show.L; import com.arialyy.simple.R; import com.arialyy.simple.base.BaseDialog; diff --git a/app/src/main/java/com/arialyy/simple/core/download/group/DownloadGroupActivity.java b/app/src/main/java/com/arialyy/simple/core/download/group/DownloadGroupActivity.java index db13ddc4..13256730 100644 --- a/app/src/main/java/com/arialyy/simple/core/download/group/DownloadGroupActivity.java +++ b/app/src/main/java/com/arialyy/simple/core/download/group/DownloadGroupActivity.java @@ -21,11 +21,11 @@ import android.util.Log; import android.view.View; import com.arialyy.annotations.DownloadGroup; import com.arialyy.aria.core.Aria; +import com.arialyy.aria.core.processor.IHttpFileLenAdapter; import com.arialyy.aria.core.download.DownloadEntity; import com.arialyy.aria.core.download.DownloadGroupEntity; -import com.arialyy.aria.core.download.DownloadGroupTask; import com.arialyy.aria.core.inf.IEntity; -import com.arialyy.aria.core.inf.IHttpFileLenAdapter; +import com.arialyy.aria.core.task.DownloadGroupTask; import com.arialyy.aria.util.ALog; import com.arialyy.frame.util.show.L; import com.arialyy.frame.util.show.T; 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 2eac991a..755e4b90 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 @@ -22,8 +22,8 @@ import com.arialyy.annotations.DownloadGroup; import com.arialyy.aria.core.Aria; import com.arialyy.aria.core.common.controller.ControllerType; import com.arialyy.aria.core.download.DownloadGroupEntity; -import com.arialyy.aria.core.download.DownloadGroupTask; import com.arialyy.aria.core.inf.IEntity; +import com.arialyy.aria.core.task.DownloadGroupTask; import com.arialyy.frame.util.show.L; import com.arialyy.frame.util.show.T; import com.arialyy.simple.R; diff --git a/app/src/main/java/com/arialyy/simple/core/download/m3u8/M3U8LiveDLoadActivity.java b/app/src/main/java/com/arialyy/simple/core/download/m3u8/M3U8LiveDLoadActivity.java index 1067b633..69fcb184 100644 --- a/app/src/main/java/com/arialyy/simple/core/download/m3u8/M3U8LiveDLoadActivity.java +++ b/app/src/main/java/com/arialyy/simple/core/download/m3u8/M3U8LiveDLoadActivity.java @@ -29,8 +29,8 @@ import com.arialyy.annotations.Download; import com.arialyy.aria.core.Aria; import com.arialyy.aria.core.common.controller.ControllerType; import com.arialyy.aria.core.download.DownloadEntity; -import com.arialyy.aria.core.download.DownloadTask; -import com.arialyy.aria.core.download.m3u8.ILiveTsUrlConverter; +import com.arialyy.aria.core.processor.ILiveTsUrlConverter; +import com.arialyy.aria.core.task.DownloadTask; import com.arialyy.aria.util.ALog; import com.arialyy.aria.util.CommonUtil; import com.arialyy.frame.util.show.T; @@ -200,7 +200,7 @@ public class M3U8LiveDLoadActivity extends BaseActivity Toast.LENGTH_SHORT).show(); getBinding().setStateStr(getString(R.string.re_start)); getBinding().setSpeed(""); - ALog.d(TAG, "md5: " + CommonUtil.getFileMD5(new File(task.getDownloadPath()))); + ALog.d(TAG, "md5: " + CommonUtil.getFileMD5(new File(task.getFilePath()))); } } 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 53d746e2..60086a2a 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 @@ -33,11 +33,11 @@ import com.arialyy.aria.core.Aria; import com.arialyy.aria.core.common.controller.BuilderController; import com.arialyy.aria.core.common.controller.ControllerType; import com.arialyy.aria.core.download.DownloadEntity; -import com.arialyy.aria.core.download.DownloadTask; -import com.arialyy.aria.core.download.m3u8.ITsMergeHandler; -import com.arialyy.aria.core.download.m3u8.IVodTsUrlConverter; -import com.arialyy.aria.core.download.m3u8.M3U8Entity; +import com.arialyy.aria.core.download.M3U8Entity; import com.arialyy.aria.core.inf.IEntity; +import com.arialyy.aria.core.processor.ITsMergeHandler; +import com.arialyy.aria.core.processor.IVodTsUrlConverter; +import com.arialyy.aria.core.task.DownloadTask; import com.arialyy.aria.util.ALog; import com.arialyy.aria.util.CommonUtil; import com.arialyy.frame.util.show.T; @@ -259,7 +259,7 @@ public class M3U8VodDLoadActivity extends BaseActivity { Toast.LENGTH_SHORT).show(); getBinding().setStateStr(getString(R.string.re_start)); getBinding().setSpeed(""); - ALog.d(TAG, "md5: " + CommonUtil.getFileMD5(new File(task.getDownloadPath()))); + ALog.d(TAG, "md5: " + CommonUtil.getFileMD5(new File(task.getFilePath()))); } } @@ -350,6 +350,16 @@ public class M3U8VodDLoadActivity extends BaseActivity { .generateIndexFile() .controller(ControllerType.CREATE_CONTROLLER) .create(); + + Aria.download(M3U8VodDLoadActivity.this) + .load(mUrl) + .useServerFileName(true) + .setFilePath(mFilePath, true) + .asM3U8().setTsUrlConvert(new IVodTsUrlConverter() { + @Override public List convert(String m3u8Url, List tsUrls) { + return null; + } + }); } private Class c = BuilderController.class; diff --git a/app/src/main/java/com/arialyy/simple/core/download/m3u8/VideoPlayerFragment.java b/app/src/main/java/com/arialyy/simple/core/download/m3u8/VideoPlayerFragment.java index d5d001c3..501da945 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 @@ -13,7 +13,7 @@ import android.widget.SeekBar; import androidx.annotation.RequiresApi; import androidx.lifecycle.ViewModelProviders; import com.arialyy.aria.core.download.DownloadEntity; -import com.arialyy.aria.core.download.m3u8.M3U8Entity; +import com.arialyy.aria.core.download.M3U8Entity; import com.arialyy.aria.util.ALog; import com.arialyy.frame.base.BaseFragment; import com.arialyy.simple.R; diff --git a/app/src/main/java/com/arialyy/simple/core/download/mutil/DownloadAdapter.java b/app/src/main/java/com/arialyy/simple/core/download/mutil/DownloadAdapter.java index 902b90cf..f59fb5d6 100644 --- a/app/src/main/java/com/arialyy/simple/core/download/mutil/DownloadAdapter.java +++ b/app/src/main/java/com/arialyy/simple/core/download/mutil/DownloadAdapter.java @@ -24,11 +24,11 @@ import android.view.View; import android.widget.Button; import android.widget.TextView; import com.arialyy.aria.core.Aria; +import com.arialyy.aria.core.common.AbsEntity; import com.arialyy.aria.core.download.DownloadEntity; import com.arialyy.aria.core.download.DownloadGroupEntity; -import com.arialyy.aria.core.inf.AbsEntity; -import com.arialyy.aria.core.inf.AbsTaskWrapper; import com.arialyy.aria.core.inf.IEntity; +import com.arialyy.aria.core.wrapper.AbsTaskWrapper; import com.arialyy.aria.util.CommonUtil; import com.arialyy.simple.R; import com.arialyy.simple.base.adapter.AbsHolder; diff --git a/app/src/main/java/com/arialyy/simple/core/download/mutil/MultiDownloadActivity.java b/app/src/main/java/com/arialyy/simple/core/download/mutil/MultiDownloadActivity.java index 4b46ddb7..503a674e 100644 --- a/app/src/main/java/com/arialyy/simple/core/download/mutil/MultiDownloadActivity.java +++ b/app/src/main/java/com/arialyy/simple/core/download/mutil/MultiDownloadActivity.java @@ -25,9 +25,9 @@ import androidx.recyclerview.widget.RecyclerView; import com.arialyy.annotations.Download; import com.arialyy.annotations.DownloadGroup; import com.arialyy.aria.core.Aria; -import com.arialyy.aria.core.download.DownloadGroupTask; -import com.arialyy.aria.core.download.DownloadTask; -import com.arialyy.aria.core.inf.AbsEntity; +import com.arialyy.aria.core.common.AbsEntity; +import com.arialyy.aria.core.task.DownloadGroupTask; +import com.arialyy.aria.core.task.DownloadTask; import com.arialyy.aria.util.ALog; import com.arialyy.simple.R; import com.arialyy.simple.base.BaseActivity; @@ -54,7 +54,7 @@ public class MultiDownloadActivity extends BaseActivity temps = Aria.download(this).getTotalTaskList(); if (temps != null && !temps.isEmpty()) { - for (AbsEntity temp : temps){ + for (AbsEntity temp : temps) { ALog.d(TAG, "state = " + temp.getState()); } mData.addAll(temps); diff --git a/app/src/main/java/com/arialyy/simple/core/download/mutil/MultiTaskActivity.java b/app/src/main/java/com/arialyy/simple/core/download/mutil/MultiTaskActivity.java index 1e390ad7..c3250673 100644 --- a/app/src/main/java/com/arialyy/simple/core/download/mutil/MultiTaskActivity.java +++ b/app/src/main/java/com/arialyy/simple/core/download/mutil/MultiTaskActivity.java @@ -26,8 +26,8 @@ import androidx.recyclerview.widget.RecyclerView; import com.arialyy.annotations.Download; import com.arialyy.annotations.DownloadGroup; import com.arialyy.aria.core.Aria; -import com.arialyy.aria.core.download.DownloadGroupTask; -import com.arialyy.aria.core.download.DownloadTask; +import com.arialyy.aria.core.task.DownloadGroupTask; +import com.arialyy.aria.core.task.DownloadTask; import com.arialyy.aria.util.ALog; import com.arialyy.frame.util.FileUtil; import com.arialyy.simple.R; @@ -105,7 +105,7 @@ public class MultiTaskActivity extends BaseActivity { } @Download.onTaskComplete void taskComplete(DownloadTask task) { - Log.d(TAG, FileUtil.getFileMD5(new File(task.getDownloadPath()))); + Log.d(TAG, FileUtil.getFileMD5(new File(task.getFilePath()))); } //############################### 任务组 ############################## diff --git a/app/src/main/java/com/arialyy/simple/core/download/service/DownloadService.java b/app/src/main/java/com/arialyy/simple/core/download/service/DownloadService.java index 29a54e98..8d7745a4 100644 --- a/app/src/main/java/com/arialyy/simple/core/download/service/DownloadService.java +++ b/app/src/main/java/com/arialyy/simple/core/download/service/DownloadService.java @@ -22,7 +22,7 @@ import android.os.IBinder; import androidx.annotation.Nullable; import com.arialyy.annotations.Download; import com.arialyy.aria.core.Aria; -import com.arialyy.aria.core.download.DownloadTask; +import com.arialyy.aria.core.task.DownloadTask; import com.arialyy.frame.util.show.T; /** diff --git a/app/src/main/java/com/arialyy/simple/core/test/AnyRunActivity.java b/app/src/main/java/com/arialyy/simple/core/test/AnyRunActivity.java index 073f3b4a..1333f538 100644 --- a/app/src/main/java/com/arialyy/simple/core/test/AnyRunActivity.java +++ b/app/src/main/java/com/arialyy/simple/core/test/AnyRunActivity.java @@ -1,7 +1,6 @@ package com.arialyy.simple.core.test; import android.os.Bundle; -import android.util.Log; import android.view.View; import com.arialyy.aria.core.Aria; import com.arialyy.aria.core.download.DTaskWrapper; diff --git a/app/src/main/java/com/arialyy/simple/core/upload/FtpUploadActivity.java b/app/src/main/java/com/arialyy/simple/core/upload/FtpUploadActivity.java index a6535705..a3631530 100644 --- a/app/src/main/java/com/arialyy/simple/core/upload/FtpUploadActivity.java +++ b/app/src/main/java/com/arialyy/simple/core/upload/FtpUploadActivity.java @@ -25,12 +25,12 @@ import androidx.lifecycle.Observer; import androidx.lifecycle.ViewModelProviders; import com.arialyy.annotations.Upload; import com.arialyy.aria.core.Aria; +import com.arialyy.aria.core.processor.FtpInterceptHandler; +import com.arialyy.aria.core.processor.IFtpUploadInterceptor; import com.arialyy.aria.core.common.controller.ControllerType; -import com.arialyy.aria.core.common.ftp.FtpInterceptHandler; -import com.arialyy.aria.core.common.ftp.IFtpUploadInterceptor; import com.arialyy.aria.core.inf.IEntity; import com.arialyy.aria.core.upload.UploadEntity; -import com.arialyy.aria.core.upload.UploadTask; +import com.arialyy.aria.core.task.UploadTask; import com.arialyy.aria.util.ALog; import com.arialyy.aria.util.CommonUtil; import com.arialyy.frame.util.FileUtil; 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 992d83f5..49eb0a00 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 @@ -23,7 +23,7 @@ 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.aria.core.task.UploadTask; import com.arialyy.frame.util.FileUtil; import com.arialyy.frame.util.show.L; import com.arialyy.simple.R; 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 6b87c9b6..9bd6459f 100644 --- a/app/src/main/java/com/arialyy/simple/modlue/AnyRunnModule.java +++ b/app/src/main/java/com/arialyy/simple/modlue/AnyRunnModule.java @@ -22,7 +22,7 @@ import com.arialyy.annotations.Download; import com.arialyy.aria.core.Aria; import com.arialyy.aria.core.common.controller.ControllerType; import com.arialyy.aria.core.download.DownloadEntity; -import com.arialyy.aria.core.download.DownloadTask; +import com.arialyy.aria.core.task.DownloadTask; import com.arialyy.aria.util.CommonUtil; import com.arialyy.frame.util.show.L; import com.arialyy.simple.util.AppUtil; @@ -77,7 +77,7 @@ public class AnyRunnModule { @Download.onTaskComplete void taskComplete(DownloadTask task) { L.d(TAG, "path ==> " + task.getDownloadEntity().getDownloadPath()); - L.d(TAG, "md5Code ==> " + CommonUtil.getFileMD5(new File(task.getDownloadPath()))); + L.d(TAG, "md5Code ==> " + CommonUtil.getFileMD5(new File(task.getFilePath()))); } public void start(String url) { diff --git a/app/src/main/java/com/arialyy/simple/util/AppUtil.java b/app/src/main/java/com/arialyy/simple/util/AppUtil.java index e22e75c7..0a0582b0 100644 --- a/app/src/main/java/com/arialyy/simple/util/AppUtil.java +++ b/app/src/main/java/com/arialyy/simple/util/AppUtil.java @@ -24,7 +24,7 @@ import android.net.Uri; import android.os.Build; import android.text.TextUtils; import androidx.core.content.FileProvider; -import com.arialyy.aria.core.inf.AbsEntity; +import com.arialyy.aria.core.common.AbsEntity; import com.arialyy.aria.util.ALog; import com.arialyy.aria.util.CommonUtil; import com.arialyy.aria.util.FileUtil; diff --git a/build.gradle b/build.gradle index 40ad5c26..2a99294f 100644 --- a/build.gradle +++ b/build.gradle @@ -41,9 +41,11 @@ task clean(type: Delete) { } ext { + versionCode = 370 + versionName = '3.7' userOrg = 'arialyy' groupId = 'com.arialyy.aria' - publishVersion = '3.6.6' + publishVersion = versionName // publishVersion = '1.0.4' //FTP插件 repoName='maven' desc = 'android 下载框架' diff --git a/settings.gradle b/settings.gradle index b6d6bae2..04e09e40 100644 --- a/settings.gradle +++ b/settings.gradle @@ -1 +1,2 @@ -include ':app', ':Aria', ':AriaAnnotations', ':AriaCompiler', ':AriaFtpPlug', ':AppFrame', ':AriaFtpComponent' +include ':app', ':Aria', ':AriaAnnotations', ':AriaCompiler', ':AriaFtpPlug', ':AppFrame', ':HttpComponent', ':M3U8Component', + ':FtpComponent', ':PublicComponent'