diff --git a/AppFrame/src/main/java/com/arialyy/frame/base/BaseFragment.java b/AppFrame/src/main/java/com/arialyy/frame/base/BaseFragment.java index 304ffb90..26d53a24 100644 --- a/AppFrame/src/main/java/com/arialyy/frame/base/BaseFragment.java +++ b/AppFrame/src/main/java/com/arialyy/frame/base/BaseFragment.java @@ -9,6 +9,7 @@ import com.arialyy.frame.core.AbsFragment; public abstract class BaseFragment extends AbsFragment { public int color; + @Override protected void dataCallback(int result, Object obj) { } 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 baa18c9f..10494a6f 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 @@ -21,6 +21,7 @@ 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.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; @@ -41,26 +42,26 @@ public abstract class AbsNormalCmd extends AbsCmd { int taskType; /** - * @param taskType 下载任务类型{@link ICmd#TASK_TYPE_DOWNLOAD}、{@link ICmd#TASK_TYPE_DOWNLOAD_GROUP}、{@link - * ICmd#TASK_TYPE_UPLOAD} + * @param taskType 下载任务类型{@link ITask#DOWNLOAD}、{@link ITask#DOWNLOAD_GROUP}、{@link + * ITask#UPLOAD} */ AbsNormalCmd(T entity, int taskType) { this.taskType = taskType; mTaskWrapper = entity; TAG = CommonUtil.getClassName(this); - if (taskType == ICmd.TASK_TYPE_DOWNLOAD) { + if (taskType == ITask.DOWNLOAD) { if (!(entity instanceof DTaskWrapper)) { ALog.e(TAG, "任务类型错误,任务类型应该为ICM.TASK_TYPE_DOWNLOAD"); return; } mQueue = DownloadTaskQueue.getInstance(); - } else if (taskType == ICmd.TASK_TYPE_DOWNLOAD_GROUP) { + } else if (taskType == ITask.DOWNLOAD_GROUP) { if (!(entity instanceof DGTaskWrapper)) { ALog.e(TAG, "任务类型错误,任务类型应该为ICM.TASK_TYPE_DOWNLOAD_GROUP"); return; } mQueue = DownloadGroupTaskQueue.getInstance(); - } else if (taskType == ICmd.TASK_TYPE_UPLOAD) { + } else if (taskType == ITask.UPLOAD) { if (!(entity instanceof UTaskWrapper)) { ALog.e(TAG, "任务类型错误,任务类型应该为ICM.TASK_TYPE_UPLOAD"); return; @@ -70,7 +71,7 @@ public abstract class AbsNormalCmd extends AbsCmd { ALog.e(TAG, "任务类型错误,任务类型应该为ICM.TASK_TYPE_DOWNLOAD、TASK_TYPE_DOWNLOAD_GROUP、TASK_TYPE_UPLOAD"); return; } - isDownloadCmd = taskType < ICmd.TASK_TYPE_UPLOAD; + isDownloadCmd = taskType == ITask.DOWNLOAD || taskType == ITask.DOWNLOAD_GROUP; } /** diff --git a/Aria/src/main/java/com/arialyy/aria/core/command/ICmd.java b/Aria/src/main/java/com/arialyy/aria/core/command/ICmd.java index f23bca06..c92d866e 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/command/ICmd.java +++ b/Aria/src/main/java/com/arialyy/aria/core/command/ICmd.java @@ -20,18 +20,6 @@ package com.arialyy.aria.core.command; */ public interface ICmd { - /** - * 单任务下载任务 - */ - int TASK_TYPE_DOWNLOAD = 0x01; - /** - * 任务组下载任务 - */ - int TASK_TYPE_DOWNLOAD_GROUP = 0x02; - /** - * 上传任务 - */ - int TASK_TYPE_UPLOAD = 0x10; /** * 执行命令 diff --git a/Aria/src/main/java/com/arialyy/aria/core/common/BaseListener.java b/Aria/src/main/java/com/arialyy/aria/core/common/BaseListener.java index aba7fbbc..58040931 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/common/BaseListener.java +++ b/Aria/src/main/java/com/arialyy/aria/core/common/BaseListener.java @@ -38,12 +38,12 @@ public abstract class BaseListener> implements IEventListener { protected static String TAG; - private static final int RUN_SAVE_INTERVAL = 5 * 1000; //5s保存一次下载中的进度 + protected static final int RUN_SAVE_INTERVAL = 5 * 1000; //5s保存一次下载中的进度 protected WeakReference outHandler; private long mLastLen; //上一次发送长度 private boolean isFirst = true; private TASK mTask; - private long mLastSaveTime; + protected long mLastSaveTime; protected ENTITY mEntity; protected TASK_WRAPPER mTaskWrapper; private boolean isConvertSpeed; diff --git a/Aria/src/main/java/com/arialyy/aria/core/common/http/HttpTaskConfig.java b/Aria/src/main/java/com/arialyy/aria/core/common/http/HttpTaskConfig.java index 447868f8..36a3f7ca 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/common/http/HttpTaskConfig.java +++ b/Aria/src/main/java/com/arialyy/aria/core/common/http/HttpTaskConfig.java @@ -86,7 +86,7 @@ public class HttpTaskConfig implements ITaskConfig { private WeakReference fileLenAdapter; public IHttpFileLenAdapter getFileLenAdapter() { - return fileLenAdapter.get(); + return fileLenAdapter == null ? null : fileLenAdapter.get(); } /** diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadGroupListener.java b/Aria/src/main/java/com/arialyy/aria/core/download/DownloadGroupListener.java index c9f039bf..36a0eb4b 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadGroupListener.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/DownloadGroupListener.java @@ -16,7 +16,6 @@ package com.arialyy.aria.core.download; 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.IDownloadGroupListener; @@ -46,6 +45,7 @@ class DownloadGroupListener @Override public void onSubPre(DownloadEntity subEntity) { + saveSubState(IEntity.STATE_PRE, subEntity); sendInState2Target(ISchedulers.SUB_PRE, subEntity); } @@ -56,23 +56,27 @@ class DownloadGroupListener @Override public void onSubStart(DownloadEntity subEntity) { + saveSubState(IEntity.STATE_RUNNING, subEntity); sendInState2Target(ISchedulers.SUB_START, subEntity); } @Override public void onSubStop(DownloadEntity subEntity) { + saveSubState(IEntity.STATE_STOP, subEntity); saveCurrentLocation(); sendInState2Target(ISchedulers.SUB_STOP, subEntity); } @Override public void onSubComplete(DownloadEntity subEntity) { + saveSubState(IEntity.STATE_COMPLETE, subEntity); saveCurrentLocation(); sendInState2Target(ISchedulers.SUB_COMPLETE, subEntity); } @Override public void onSubFail(DownloadEntity subEntity, BaseException e) { + saveSubState(IEntity.STATE_FAIL, subEntity); saveCurrentLocation(); sendInState2Target(ISchedulers.SUB_FAIL, subEntity); if (e != null) { @@ -83,12 +87,17 @@ class DownloadGroupListener @Override public void onSubCancel(DownloadEntity subEntity) { + saveSubState(IEntity.STATE_CANCEL, subEntity); saveCurrentLocation(); sendInState2Target(ISchedulers.SUB_CANCEL, subEntity); } @Override public void onSubRunning(DownloadEntity subEntity) { + if (System.currentTimeMillis() - mLastSaveTime >= RUN_SAVE_INTERVAL) { + saveSubState(IEntity.STATE_RUNNING, subEntity); + mLastSaveTime = System.currentTimeMillis(); + } sendInState2Target(ISchedulers.SUB_RUNNING, subEntity); } @@ -102,7 +111,6 @@ class DownloadGroupListener mSeedEntity.entity = subEntity; outHandler.get().obtainMessage(state, ISchedulers.IS_SUB_TASK, 0, mSeedEntity).sendToTarget(); } - saveSubState(state, subEntity); } private void saveSubState(int state, DownloadEntity subEntity) { @@ -117,7 +125,7 @@ class DownloadGroupListener subEntity.setConvertSpeed("0kb/s"); subEntity.setSpeed(0); ALog.i(TAG, String.format("任务【%s】完成,将删除线程任务记录", mEntity.getKey())); - RecordUtil.delTaskRecord(subEntity.getKey(), RecordHandler.TYPE_DOWNLOAD, false, false); + RecordUtil.delTaskRecord(subEntity.getFilePath(), RecordHandler.TYPE_DOWNLOAD, false, false); } subEntity.update(); } 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 46a76f93..01f3960a 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 @@ -21,12 +21,12 @@ import android.text.TextUtils; import com.arialyy.annotations.TaskEnum; import com.arialyy.aria.core.AriaManager; import com.arialyy.aria.core.command.CancelAllCmd; -import com.arialyy.aria.core.command.ICmd; import com.arialyy.aria.core.command.NormalCmdFactory; import com.arialyy.aria.core.common.ProxyHelper; import com.arialyy.aria.core.event.EventMsgUtil; import com.arialyy.aria.core.inf.AbsEntity; import com.arialyy.aria.core.inf.AbsReceiver; +import com.arialyy.aria.core.inf.ITask; import com.arialyy.aria.core.inf.ReceiverType; import com.arialyy.aria.core.scheduler.DownloadGroupSchedulers; import com.arialyy.aria.core.scheduler.DownloadSchedulers; @@ -423,7 +423,7 @@ public class DownloadReceiver extends AbsReceiver { public void stopAllTask() { EventMsgUtil.getDefault().post(NormalCmdFactory.getInstance() .createCmd(new DTaskWrapper(null), NormalCmdFactory.TASK_STOP_ALL, - ICmd.TASK_TYPE_DOWNLOAD)); + ITask.DOWNLOAD)); } /** @@ -434,7 +434,7 @@ public class DownloadReceiver extends AbsReceiver { public void resumeAllTask() { EventMsgUtil.getDefault().post(NormalCmdFactory.getInstance() .createCmd(new DTaskWrapper(null), NormalCmdFactory.TASK_RESUME_ALL, - ICmd.TASK_TYPE_DOWNLOAD)); + ITask.DOWNLOAD)); } /** @@ -447,7 +447,7 @@ public class DownloadReceiver extends AbsReceiver { final AriaManager ariaManager = AriaManager.getInstance(AriaManager.APP); CancelAllCmd cancelCmd = (CancelAllCmd) CommonUtil.createNormalCmd(new DTaskWrapper(null), - NormalCmdFactory.TASK_CANCEL_ALL, ICmd.TASK_TYPE_DOWNLOAD); + 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/group/AbsGroupUtil.java b/Aria/src/main/java/com/arialyy/aria/core/download/group/AbsGroupUtil.java index f2310b7c..4a1f7874 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/group/AbsGroupUtil.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/group/AbsGroupUtil.java @@ -50,7 +50,7 @@ public abstract class AbsGroupUtil implements IUtil, Runnable { 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; @@ -102,7 +102,7 @@ public abstract class AbsGroupUtil implements IUtil, Runnable { if (!mState.isRunning) { startTimer(); } - SubDownloadLoader d = getDownloader(url); + SubDLoadUtil d = getDownloader(url); if (d != null && !d.isRunning()) { mSubQueue.startTask(d); } @@ -115,7 +115,7 @@ public abstract class AbsGroupUtil implements IUtil, Runnable { */ public void stopSubTask(String url) { if (!checkSubTask(url, "停止")) return; - SubDownloadLoader d = getDownloader(url); + SubDLoadUtil d = getDownloader(url); if (d != null && d.isRunning()) { mSubQueue.stopTask(d); } @@ -147,8 +147,8 @@ public abstract class AbsGroupUtil implements IUtil, Runnable { * * @param url 子任务下载地址 */ - private SubDownloadLoader getDownloader(String url) { - SubDownloadLoader d = mExeLoader.get(url); + private SubDLoadUtil getDownloader(String url) { + SubDLoadUtil d = mExeLoader.get(url); if (d == null) { return createAndStartSubLoader(mCache.get(url)); } @@ -266,7 +266,7 @@ public abstract class AbsGroupUtil implements IUtil, Runnable { /** * 创建并启动子任务下载器 */ - SubDownloadLoader createAndStartSubLoader(DTaskWrapper taskWrapper) { + SubDLoadUtil createAndStartSubLoader(DTaskWrapper taskWrapper) { return createAndStartSubLoader(taskWrapper, true); } @@ -275,8 +275,8 @@ public abstract class AbsGroupUtil implements IUtil, Runnable { * * @param needGetFileInfo {@code true} 需要获取文件信息。{@code false} 不需要获取文件信息 */ - SubDownloadLoader createAndStartSubLoader(DTaskWrapper taskWrapper, boolean needGetFileInfo) { - SubDownloadLoader loader = new SubDownloadLoader(mScheduler, taskWrapper, needGetFileInfo); + SubDLoadUtil createAndStartSubLoader(DTaskWrapper taskWrapper, boolean needGetFileInfo) { + SubDLoadUtil loader = new SubDLoadUtil(mScheduler, taskWrapper, needGetFileInfo); mExeLoader.put(loader.getKey(), loader); mSubQueue.startTask(loader); return loader; diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/group/ChildDownloadListener.java b/Aria/src/main/java/com/arialyy/aria/core/download/group/ChildDownloadListener.java index dcccf5be..91c383ff 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/group/ChildDownloadListener.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/group/ChildDownloadListener.java @@ -32,9 +32,9 @@ class ChildDownloadListener implements IDownloadListener { private long lastSaveTime; private long lastLen; private Handler schedulers; - private SubDownloadLoader loader; + private SubDLoadUtil loader; - ChildDownloadListener(Handler schedulers, SubDownloadLoader loader) { + ChildDownloadListener(Handler schedulers, SubDLoadUtil loader) { this.loader = loader; this.schedulers = schedulers; subEntity = loader.getEntity(); @@ -140,7 +140,7 @@ class ChildDownloadListener implements IDownloadListener { * * @param state {@link ISchedulers} */ - private void sendToTarget(int state, SubDownloadLoader util) { + private void sendToTarget(int state, SubDLoadUtil 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/SimpleSchedulers.java b/Aria/src/main/java/com/arialyy/aria/core/download/group/SimpleSchedulers.java index 6d1bc125..bff34012 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/group/SimpleSchedulers.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/group/SimpleSchedulers.java @@ -20,6 +20,7 @@ import android.os.Message; import com.arialyy.aria.core.AriaManager; import com.arialyy.aria.core.config.Configuration; import com.arialyy.aria.core.inf.AbsEntity; +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; @@ -47,7 +48,7 @@ class SimpleSchedulers implements ISchedulers { } @Override public boolean handleMessage(Message msg) { - SubDownloadLoader loader = (SubDownloadLoader) msg.obj; + SubDLoadUtil loader = (SubDLoadUtil) msg.obj; switch (msg.what) { case RUNNING: mGState.listener.onSubRunning(loader.getEntity()); @@ -78,7 +79,7 @@ class SimpleSchedulers implements ISchedulers { * 2、stopNum + failNum + completeNum + cacheNum == subSize,则认为组合任务停止 * 3、failNum == subSize,只有全部的子任务都失败了,才能任务组合任务失败 */ - private synchronized void handleFail(final SubDownloadLoader loader) { + private synchronized void handleFail(final SubDLoadUtil loader) { Configuration config = Configuration.getInstance(); long interval = config.dGroupCfg.getSubReTryInterval(); @@ -125,7 +126,7 @@ class SimpleSchedulers implements ISchedulers { * 1、所有的子任务已经停止,则认为组合任务停止 * 2、completeNum + failNum + stopNum = subSize,则认为组合任务停止 */ - private synchronized void handleStop(SubDownloadLoader loader) { + private synchronized void handleStop(SubDLoadUtil loader) { mGState.listener.onSubStop(loader.getEntity()); mGState.countStopNum(loader.getKey()); if (mGState.getStopNum() == mGState.getSubSize() @@ -150,8 +151,9 @@ class SimpleSchedulers implements ISchedulers { * GroupRunState#getFailNum()}不为0,则认为组合任务被停止 * 3、只有有缓存的子任务,则任务组合任务没有完成 */ - private synchronized void handleComplete(SubDownloadLoader loader) { + private synchronized void handleComplete(SubDLoadUtil loader) { ALog.d(TAG, String.format("子任务【%s】完成", loader.getEntity().getFileName())); + ThreadTaskManager.getInstance().removeTaskThread(loader.getKey()); mGState.listener.onSubComplete(loader.getEntity()); mQueue.removeTaskFromExecQ(loader); mGState.updateCompleteNum(); @@ -177,7 +179,7 @@ class SimpleSchedulers implements ISchedulers { if (mQueue.isStopAll()) { return; } - SubDownloadLoader next = mQueue.getNextTask(); + SubDLoadUtil 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/Aria/src/main/java/com/arialyy/aria/core/download/group/SimpleSubQueue.java index 493e408d..f004345b 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/group/SimpleSubQueue.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/group/SimpleSubQueue.java @@ -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(SubDownloadLoader fileer) { + @Override public void addTask(SubDLoadUtil fileer) { mCache.put(fileer.getKey(), fileer); } - @Override public void startTask(SubDownloadLoader fileer) { + @Override public void startTask(SubDLoadUtil 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(SubDownloadLoader fileer) { + @Override public void stopTask(SubDLoadUtil 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) { - SubDownloadLoader loader = mExec.get(key); + SubDLoadUtil 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,20 +130,19 @@ class SimpleSubQueue implements ISubQueue { } i++; } - Collection temp = mCache.values(); + Collection temp = mCache.values(); mCache.clear(); - ALog.d(TAG, String.format("测试, cacheSize: %s", mCache.size())); - for (SubDownloadLoader cache : caches) { + for (SubDLoadUtil cache : caches) { addTask(cache); } - for (SubDownloadLoader t : temp) { + for (SubDLoadUtil t : temp) { addTask(t); } } } else { // 处理队列变大的情况,该情况下将增加任务 if (mExec.size() < num) { for (int i = 0; i < diff; i++) { - SubDownloadLoader next = getNextTask(); + SubDLoadUtil next = getNextTask(); if (next != null) { startTask(next); } else { @@ -154,7 +153,7 @@ class SimpleSubQueue implements ISubQueue { } } - @Override public void removeTaskFromExecQ(SubDownloadLoader fileer) { + @Override public void removeTaskFromExecQ(SubDLoadUtil fileer) { if (mExec.containsKey(fileer.getKey())) { if (fileer.isRunning()) { fileer.stop(); @@ -163,7 +162,7 @@ class SimpleSubQueue implements ISubQueue { } } - @Override public void removeTask(SubDownloadLoader fileer) { + @Override public void removeTask(SubDLoadUtil fileer) { removeTaskFromExecQ(fileer); mCache.remove(fileer.getKey()); } @@ -172,7 +171,7 @@ class SimpleSubQueue implements ISubQueue { ALog.d(TAG, "删除组合任务"); Set keys = mExec.keySet(); for (String key : keys) { - SubDownloadLoader loader = mExec.get(key); + SubDLoadUtil loader = mExec.get(key); if (loader != null) { ALog.d(TAG, String.format("停止子任务:%s", loader.getEntity().getFileName())); loader.cancel(); @@ -180,7 +179,7 @@ class SimpleSubQueue implements ISubQueue { } } - @Override public SubDownloadLoader getNextTask() { + @Override public SubDLoadUtil getNextTask() { Iterator keys = mCache.keySet().iterator(); if (keys.hasNext()) { return mCache.get(keys.next()); diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/group/SubDownloadLoader.java b/Aria/src/main/java/com/arialyy/aria/core/download/group/SubDLoadUtil.java similarity index 91% rename from Aria/src/main/java/com/arialyy/aria/core/download/group/SubDownloadLoader.java rename to Aria/src/main/java/com/arialyy/aria/core/download/group/SubDLoadUtil.java index 85417f8f..27131b36 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/group/SubDownloadLoader.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/group/SubDLoadUtil.java @@ -19,7 +19,6 @@ 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.DGTaskWrapper; import com.arialyy.aria.core.download.DTaskWrapper; import com.arialyy.aria.core.download.DownloadEntity; import com.arialyy.aria.core.download.downloader.Downloader; @@ -33,7 +32,7 @@ import com.arialyy.aria.util.ALog; /** * 子任务下载器,负责创建{@link Downloader} */ -class SubDownloadLoader implements IUtil { +class SubDLoadUtil implements IUtil { private final String TAG = "SubDownloadLoader"; private Downloader mDownloader; @@ -46,11 +45,11 @@ class SubDownloadLoader implements IUtil { * @param schedulers 调度器 * @param needGetInfo {@code true} 需要获取文件信息。{@code false} 不需要获取文件信息 */ - SubDownloadLoader(Handler schedulers, DTaskWrapper taskWrapper, boolean needGetInfo) { + SubDLoadUtil(Handler schedulers, DTaskWrapper taskWrapper, boolean needGetInfo) { mWrapper = taskWrapper; mSchedulers = schedulers; this.needGetInfo = needGetInfo; - mListener = new ChildDownloadListener(mSchedulers, SubDownloadLoader.this); + mListener = new ChildDownloadListener(mSchedulers, SubDLoadUtil.this); } @Override public String getKey() { @@ -113,7 +112,7 @@ class SubDownloadLoader implements IUtil { } @Override public void onFail(AbsEntity entity, BaseException e, boolean needRetry) { - mSchedulers.obtainMessage(ISchedulers.FAIL, SubDownloadLoader.this); + mSchedulers.obtainMessage(ISchedulers.FAIL, SubDLoadUtil.this); } })).start(); } else { 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 9911e239..34ec7dee 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 @@ -20,7 +20,6 @@ import android.os.Looper; import android.support.annotation.CheckResult; import android.text.TextUtils; import com.arialyy.aria.core.command.CancelCmd; -import com.arialyy.aria.core.command.ICmd; import com.arialyy.aria.core.command.NormalCmdFactory; import com.arialyy.aria.core.download.DGTaskWrapper; import com.arialyy.aria.core.download.DTaskWrapper; @@ -193,11 +192,11 @@ public abstract class AbsTarget implements ITargetHand protected int checkTaskType() { int taskType = 0; if (mTaskWrapper instanceof DTaskWrapper) { - taskType = ICmd.TASK_TYPE_DOWNLOAD; + taskType = ITask.DOWNLOAD; } else if (mTaskWrapper instanceof DGTaskWrapper) { - taskType = ICmd.TASK_TYPE_DOWNLOAD_GROUP; + taskType = ITask.DOWNLOAD_GROUP; } else if (mTaskWrapper instanceof UTaskWrapper) { - taskType = ICmd.TASK_TYPE_UPLOAD; + taskType = ITask.UPLOAD; } return taskType; } @@ -209,8 +208,9 @@ public abstract class AbsTarget implements ITargetHand boolean b = checkEntity(); ISchedulers schedulers = getScheduler(); if (!b && schedulers != null) { - new Handler(Looper.getMainLooper(), schedulers).obtainMessage(ISchedulers.FAIL, - new TempTask(mTaskWrapper, mEntity)).sendToTarget(); + + new Handler(Looper.getMainLooper(), schedulers).obtainMessage(ISchedulers.CHECK_FAIL, + checkTaskType(), -1, null).sendToTarget(); } return b; @@ -365,83 +365,4 @@ public abstract class AbsTarget implements ITargetHand checkTaskType())); } } - - private static class TempTask implements ITask { - - private AbsTaskWrapper wrapper; - private AbsEntity entity; - - private TempTask(AbsTaskWrapper wrapper, AbsEntity entity) { - this.wrapper = wrapper; - this.entity = entity; - } - - @Override public int getTaskType() { - return TEMP; - } - - @Override public int getState() { - return entity.getState(); - } - - @Override public String getKey() { - return entity.getKey(); - } - - @Override public boolean isRunning() { - return false; - } - - @Override public AbsTaskWrapper getTaskWrapper() { - return wrapper; - } - - @Override public void start() { - - } - - @Override public void start(int type) { - - } - - @Override public void stop() { - - } - - @Override public void stop(int type) { - - } - - @Override public void cancel() { - - } - - @Override public void cancel(int type) { - - } - - @Override public Object getExpand(String key) { - return null; - } - - @Override public boolean isStop() { - return false; - } - - @Override public boolean isCancel() { - return false; - } - - @Override public boolean isNeedRetry() { - return false; - } - - @Override public String getTaskName() { - return "TempTask"; - } - - @Override public int getSchedulerType() { - return TaskSchedulerType.TYPE_DEFAULT; - } - } } diff --git a/Aria/src/main/java/com/arialyy/aria/core/manager/ThreadTaskManager.java b/Aria/src/main/java/com/arialyy/aria/core/manager/ThreadTaskManager.java index 0e5e4d19..1ba8a37c 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/manager/ThreadTaskManager.java +++ b/Aria/src/main/java/com/arialyy/aria/core/manager/ThreadTaskManager.java @@ -114,8 +114,8 @@ public class ThreadTaskManager { container.threadTask.destroy(); } temp.clear(); + mThreadTasks.remove(key); } - mThreadTasks.remove(key); } catch (Exception e) { e.printStackTrace(); } finally { diff --git a/Aria/src/main/java/com/arialyy/aria/core/scheduler/AbsSchedulers.java b/Aria/src/main/java/com/arialyy/aria/core/scheduler/AbsSchedulers.java index 612f63a7..39953c18 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/scheduler/AbsSchedulers.java +++ b/Aria/src/main/java/com/arialyy/aria/core/scheduler/AbsSchedulers.java @@ -148,11 +148,11 @@ abstract class AbsSchedulers 0) { + Set keys = mObservers.keySet(); + for (String key : keys) { + Map listeners = mObservers.get(key); + if (listeners == null || listeners.isEmpty()) { + continue; + } + NormalTaskListener listener = null; + if (mObservers.get(key) != null) { + if (taskType == ITask.DOWNLOAD) { + listener = (NormalTaskListener) listeners.get(TaskEnum.DOWNLOAD); + } else if (taskType == ITask.DOWNLOAD_GROUP) { + listener = (NormalTaskListener) listeners.get(TaskEnum.DOWNLOAD_GROUP); + } else if (taskType == ITask.DOWNLOAD_GROUP) { + listener = (NormalTaskListener) listeners.get(TaskEnum.UPLOAD); + } + } + if (listener != null) { + normalTaskCallback(ISchedulers.CHECK_FAIL, null, listener); + } + } } } @@ -337,7 +381,7 @@ abstract class AbsSchedulers listener) { if (listener != null) { - if (task == null) { + if (task == null && state != ISchedulers.CHECK_FAIL) { ALog.e(TAG, "TASK 为null,回调失败"); return; } @@ -373,6 +417,9 @@ abstract class AbsSchedulers reTryNum) { mQueue.removeTaskFormQueue(task.getKey()); - startNextTask(task); + startNextTask(task.getSchedulerType()); TaskWrapperManager.getInstance().removeTaskWrapper(task.getKey()); normalTaskCallback(FAIL, task); return; @@ -456,7 +503,7 @@ abstract class AbsSchedulers { + public LoadingDialog(Object obj) { + super(obj); + } + + @Override protected int setLayoutId() { + return R.layout.dialog_loading; + } + + @Override + public void onStart() { + super.onStart(); + Dialog dialog = getDialog(); + if (dialog != null) { + dialog.getWindow().setLayout(DensityUtils.dp2px(120), DensityUtils.dp2px(120)); + dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); + getDialog().setCanceledOnTouchOutside(false); + } + } +} 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 528a6ec7..006daf20 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 @@ -29,9 +29,7 @@ import android.view.MenuItem; import android.view.MotionEvent; import android.view.View; import android.widget.Toast; - import com.arialyy.annotations.Download; -import com.arialyy.annotations.M3U8; import com.arialyy.aria.core.Aria; import com.arialyy.aria.core.download.DownloadEntity; import com.arialyy.aria.core.download.DownloadTarget; @@ -47,7 +45,6 @@ import com.arialyy.simple.base.BaseActivity; import com.arialyy.simple.common.ModifyPathDialog; import com.arialyy.simple.common.ModifyUrlDialog; import com.arialyy.simple.databinding.ActivitySingleBinding; - import com.arialyy.simple.util.AppUtil; import java.io.File; import java.io.IOException; @@ -243,9 +240,9 @@ public class SingleTaskActivity extends BaseActivity { @Download.onTaskFail void taskFail(DownloadTask task, Exception e) { - if (task.getKey().equals(mUrl)) { - Toast.makeText(SingleTaskActivity.this, getString(R.string.download_fail), Toast.LENGTH_SHORT) - .show(); + Toast.makeText(SingleTaskActivity.this, getString(R.string.download_fail), Toast.LENGTH_SHORT) + .show(); + if (task != null && task.getKey().equals(mUrl)) { getBinding().setStateStr(getString(R.string.start)); } } 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 b8edaf5c..c8c2c3c0 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 @@ -75,7 +75,7 @@ import java.util.List; findViewById(R.id.stop).setOnClickListener(this); findViewById(R.id.start).setOnClickListener(this); - findViewById(R.id.cancel).setOnClickListener(this); + //findViewById(R.id.cancel).setOnClickListener(this); mGroup.setText("任务组:" + mGroupHash); mSub.setText("子任务:" + mChildName); mPb.setProgress((int) (mChildEntity.getCurrentProgress() * 100 / mChildEntity.getFileSize())); 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 579fd083..7d7bb9ba 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 @@ -23,6 +23,7 @@ import com.arialyy.annotations.DownloadGroup; import com.arialyy.aria.core.Aria; import com.arialyy.aria.core.download.DownloadEntity; import com.arialyy.aria.core.download.DownloadGroupEntity; +import com.arialyy.aria.core.download.DownloadGroupTarget; import com.arialyy.aria.core.download.DownloadGroupTask; import com.arialyy.aria.core.inf.IHttpFileLenAdapter; import com.arialyy.aria.util.ALog; @@ -40,18 +41,23 @@ import java.util.Map; */ public class DownloadGroupActivity extends BaseActivity { - SubStateLinearLayout mChildList; - List mUrls; + private SubStateLinearLayout mChildList; + private List mUrls; + private DownloadGroupTarget mTarget; @Override protected void init(Bundle savedInstanceState) { super.init(savedInstanceState); Aria.download(this).register(); setTitle("任务组"); + mChildList = getBinding().childList; mUrls = getModule(GroupModule.class).getUrls(); DownloadGroupEntity entity = Aria.download(this).getDownloadGroupEntity(mUrls); if (entity != null) { + mTarget = Aria.download(this).loadGroup(entity); mChildList.addData(entity.getSubEntities()); + getBinding().setStateStr( + mTarget.isRunning() ? getString(R.string.stop) : getString(R.string.resume)); getBinding().setFileSize(entity.getConvertFileSize()); if (entity.getFileSize() == 0) { getBinding().setProgress(0); @@ -59,7 +65,8 @@ public class DownloadGroupActivity extends BaseActivity> headers) { - - List sLength = headers.get("Content-Length"); - if (sLength == null || sLength.isEmpty()) { - return -1; + if (mTarget == null || !mTarget.isRunning()) { + Aria.download(this) + .loadGroup(mUrls) + .setDirPath( + //Environment.getExternalStorageDirectory().getPath() + "/Download/group_test_5") + Environment.getExternalStorageDirectory().getPath() + "/Download/group_test_2") + .setGroupAlias("任务组测试") + //.setSubFileName(getModule(GroupModule.class).getSubName2()) + .setSubFileName(getModule(GroupModule.class).getSubName()) + .unknownSize() + .setFileLenAdapter(new IHttpFileLenAdapter() { + @Override public long handleFileLen(Map> headers) { + + List sLength = headers.get("Content-Length"); + if (sLength == null || sLength.isEmpty()) { + return -1; + } + String temp = sLength.get(0); + + return Long.parseLong(temp); } - String temp = sLength.get(0); - - return Long.parseLong(temp); - } - }) - //.setFileSize(114981416) - //.updateUrls(temp) - .start(); - break; - case R.id.stop: - Aria.download(this).loadGroup(mUrls).stop(); + }) + //.setFileSize(114981416) + //.updateUrls(temp) + .start(); + getBinding().setStateStr(getString(R.string.stop)); + } else { + Aria.download(this).loadGroup(mUrls).stop(); + getBinding().setStateStr(getString(R.string.resume)); + } break; case R.id.cancel: Aria.download(this).loadGroup(mUrls).cancel(true); @@ -166,10 +176,12 @@ public class DownloadGroupActivity extends BaseActivity { @@ -105,6 +109,21 @@ public class M3U8VodDLoadActivity extends BaseActivity { dialog.show(getSupportFragmentManager(), "ModifyPathDialog"); } + @Override protected void onStart() { + super.onStart(); + EventBus.getDefault().register(this); + } + + @Override protected void onStop() { + super.onStop(); + EventBus.getDefault().unregister(this); + } + + @Subscribe(threadMode = ThreadMode.MAIN) + public void jumpIndex(PeerIndex index) { + Aria.download(this).load(mUrl).asM3U8().asVod().jumPeerIndex(index.index); + } + @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.menu_single_task_activity, menu); @@ -303,8 +322,6 @@ public class M3U8VodDLoadActivity extends BaseActivity { mModule.uploadUrl(this, String.valueOf(data)); } else if (result == ModifyPathDialog.MODIFY_PATH_RESULT) { mModule.updateFilePath(this, String.valueOf(data)); - } else if (result == VideoPlayerFragment.SEEK_BAR_PROGRESS_KEY) { - Aria.download(this).load(mUrl).asM3U8().asVod().jumPeerIndex((Integer) data); } } } \ No newline at end of file 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 01bd87c1..9b5f6b4f 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 @@ -14,24 +14,29 @@ import android.view.View; import android.widget.SeekBar; import com.arialyy.aria.core.download.DownloadEntity; import com.arialyy.aria.core.download.m3u8.M3U8Entity; +import com.arialyy.aria.util.ALog; import com.arialyy.frame.base.BaseFragment; import com.arialyy.simple.R; +import com.arialyy.simple.common.LoadingDialog; import com.arialyy.simple.databinding.FragmentVideoPlayerBinding; +import com.arialyy.simple.to.PeerIndex; import java.io.IOException; import java.util.List; +import org.greenrobot.eventbus.EventBus; @SuppressLint("ValidFragment") @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN) public class VideoPlayerFragment extends BaseFragment { - static final int SEEK_BAR_PROGRESS_KEY = 0xC1; - private M3U8VodModule mModule; private DownloadEntity mEntity; private int mPeerIndex; - SparseArray mPlayers = new SparseArray<>(); + private SparseArray mPlayers = new SparseArray<>(); private SurfaceHolder mSurfaceHolder; - private NextMediaPlayer nexPlayer; + private NextMediaPlayer mNextPlayer; + private MediaPlayer mCurrentPlayer; + private LoadingDialog mLoadingDialog; + private int mJumpIndex = -1; VideoPlayerFragment(int peerIndex, DownloadEntity entity) { mEntity = entity; @@ -46,13 +51,11 @@ public class VideoPlayerFragment extends BaseFragment + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/activity_download_group.xml b/app/src/main/res/layout/activity_download_group.xml index 29120818..5191f4d5 100644 --- a/app/src/main/res/layout/activity_download_group.xml +++ b/app/src/main/res/layout/activity_download_group.xml @@ -15,6 +15,11 @@ name="progress" type="int" /> + + + + + + + + + + + diff --git a/app/src/main/res/layout/fragment_video_player.xml b/app/src/main/res/layout/fragment_video_player.xml index b163de79..6335856d 100644 --- a/app/src/main/res/layout/fragment_video_player.xml +++ b/app/src/main/res/layout/fragment_video_player.xml @@ -12,7 +12,7 @@ @@ -31,7 +31,8 @@ android:layout_height="wrap_content" android:layout_centerVertical="true" android:clickable="true" - app:srcCompat="@drawable/ic_start" + android:visibility="visible" + app:srcCompat="@drawable/selector_start_stop_bt" />