laoyuyu 7 years ago
parent 7bac199856
commit a7e238e1d1
  1. 31
      Aria/src/main/java/com/arialyy/aria/core/common/AbsFileer.java
  2. 13
      Aria/src/main/java/com/arialyy/aria/core/common/AbsThreadTask.java
  3. 63
      Aria/src/main/java/com/arialyy/aria/core/delegate/HttpHeaderDelegate.java
  4. 32
      Aria/src/main/java/com/arialyy/aria/core/download/DownloadGroupTarget.java
  5. 2
      Aria/src/main/java/com/arialyy/aria/core/download/DownloadGroupTask.java
  6. 2
      Aria/src/main/java/com/arialyy/aria/core/download/DownloadTask.java
  7. 11
      Aria/src/main/java/com/arialyy/aria/core/download/downloader/AbsGroupUtil.java
  8. 27
      Aria/src/main/java/com/arialyy/aria/core/inf/AbsTask.java
  9. 13
      Aria/src/main/java/com/arialyy/aria/core/queue/AbsTaskQueue.java
  10. 4
      Aria/src/main/java/com/arialyy/aria/core/scheduler/AbsSchedulers.java
  11. 4
      app/src/main/AndroidManifest.xml
  12. 2
      aria/src/main/java/com/arialyy/aria/core/upload/UploadTask.java
  13. 2
      build.gradle

@ -113,6 +113,9 @@ public abstract class AbsFileer<ENTITY extends AbsNormalEntity, TASK_ENTITY exte
* 开始流程 * 开始流程
*/ */
private void startFlow() { private void startFlow() {
if (isBreak()) {
return;
}
mConstance.resetState(); mConstance.resetState();
checkTask(); checkTask();
mConstance.TASK_RECORD = mRecord; mConstance.TASK_RECORD = mRecord;
@ -497,6 +500,9 @@ public abstract class AbsFileer<ENTITY extends AbsNormalEntity, TASK_ENTITY exte
* 启动单线程任务 * 启动单线程任务
*/ */
private void startThreadTask(int[] recordL) { private void startThreadTask(int[] recordL) {
if (isBreak()) {
return;
}
if (mConstance.CURRENT_LOCATION > 0) { if (mConstance.CURRENT_LOCATION > 0) {
mListener.onResume(mConstance.CURRENT_LOCATION); mListener.onResume(mConstance.CURRENT_LOCATION);
} else { } else {
@ -516,6 +522,10 @@ public abstract class AbsFileer<ENTITY extends AbsNormalEntity, TASK_ENTITY exte
* 重试线程任务只有线程创建成功才能重试 * 重试线程任务只有线程创建成功才能重试
*/ */
public void retryThreadTask() { public void retryThreadTask() {
if (isBreak()) {
return;
}
if (mTask == null || mTask.size() == 0) { if (mTask == null || mTask.size() == 0) {
ALog.w(TAG, "没有线程任务"); ALog.w(TAG, "没有线程任务");
return; return;
@ -524,7 +534,7 @@ public abstract class AbsFileer<ENTITY extends AbsNormalEntity, TASK_ENTITY exte
for (Integer key : keys) { for (Integer key : keys) {
AbsThreadTask task = mTask.get(key); AbsThreadTask task = mTask.get(key);
if (task != null && !task.isThreadComplete()) { if (task != null && !task.isThreadComplete()) {
task.getConfig().START_LOCATION = task.getCurrentLocation(); task.getConfig().START_LOCATION = task.getConfig().THREAD_RECORD.startLocation;
mConstance.isStop = false; mConstance.isStop = false;
mConstance.isCancel = false; mConstance.isCancel = false;
mConstance.isRunning = true; mConstance.isRunning = true;
@ -561,6 +571,11 @@ public abstract class AbsFileer<ENTITY extends AbsNormalEntity, TASK_ENTITY exte
config.END_LOCATION = config.FILE_SIZE; config.END_LOCATION = config.FILE_SIZE;
config.SUPPORT_BP = mTaskEntity.isSupportBP(); config.SUPPORT_BP = mTaskEntity.isSupportBP();
config.TASK_ENTITY = mTaskEntity; config.TASK_ENTITY = mTaskEntity;
ThreadRecord record = new ThreadRecord();
record.startLocation = 0;
record.endLocation = config.FILE_SIZE;
record.key = mTempFile.getPath();
config.THREAD_RECORD = record;
AbsThreadTask task = selectThreadTask(config); AbsThreadTask task = selectThreadTask(config);
if (task == null) return; if (task == null) return;
mTask.put(0, task); mTask.put(0, task);
@ -573,4 +588,18 @@ public abstract class AbsFileer<ENTITY extends AbsNormalEntity, TASK_ENTITY exte
* 选择单任务线程的类型 * 选择单任务线程的类型
*/ */
protected abstract AbsThreadTask selectThreadTask(SubThreadConfig<TASK_ENTITY> config); protected abstract AbsThreadTask selectThreadTask(SubThreadConfig<TASK_ENTITY> config);
/**
* 任务是否已经中断
*
* @return {@code true}中断
*/
public boolean isBreak() {
if (mConstance.isCancel || mConstance.isStop) {
closeTimer();
ALog.d(TAG, String.format("任务【%s】已停止或取消了", mEntity.getFileName()));
return true;
}
return false;
}
} }

@ -83,7 +83,7 @@ public abstract class AbsThreadTask<ENTITY extends AbsNormalEntity, TASK_ENTITY
mConfigThreadPool = Executors.newCachedThreadPool(); mConfigThreadPool = Executors.newCachedThreadPool();
} }
public void setMaxSpeed(double maxSpeed) { protected void setMaxSpeed(double maxSpeed) {
if (-0.9999 < maxSpeed && maxSpeed < 0.00001) { if (-0.9999 < maxSpeed && maxSpeed < 0.00001) {
mSleepTime = 0; mSleepTime = 0;
} else { } else {
@ -108,13 +108,6 @@ public abstract class AbsThreadTask<ENTITY extends AbsNormalEntity, TASK_ENTITY
return mConfig; return mConfig;
} }
/**
* 当前线程下载进度
*/
public long getCurrentLocation() {
return mChildCurrentLocation;
}
private boolean filterVersion() { private boolean filterVersion() {
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP; return Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP;
} }
@ -291,8 +284,8 @@ public abstract class AbsThreadTask<ENTITY extends AbsNormalEntity, TASK_ENTITY
mFailTimes++; mFailTimes++;
ALog.w(TAG, String.format("任务【%s】thread__%s__正在重试", mConfig.TEMP_FILE.getName(), ALog.w(TAG, String.format("任务【%s】thread__%s__正在重试", mConfig.TEMP_FILE.getName(),
mConfig.THREAD_ID)); mConfig.THREAD_ID));
final long temp = mChildCurrentLocation; mConfig.START_LOCATION = mChildCurrentLocation == 0 ? mConfig.START_LOCATION
mConfig.START_LOCATION = mChildCurrentLocation == 0 ? mConfig.START_LOCATION : temp; : mConfig.THREAD_RECORD.startLocation;
AbsThreadTask.this.run(); AbsThreadTask.this.run();
} }
}, RETRY_INTERVAL); }, RETRY_INTERVAL);

@ -61,11 +61,7 @@ public class HttpHeaderDelegate<TARGET extends ITarget, ENTITY extends AbsEntity
ALog.w(TAG, "设置header失败,header对应的value不能为null"); ALog.w(TAG, "设置header失败,header对应的value不能为null");
return mTarget; return mTarget;
} }
if (mTaskEntity.getHeaders().get(key) == null) { addHeader(mTaskEntity, key, value);
mTaskEntity.getHeaders().put(key, value);
} else if (!mTaskEntity.getHeaders().get(key).equals(value)) {
mTaskEntity.getHeaders().put(key, value);
}
return mTarget; return mTarget;
} }
@ -81,6 +77,35 @@ public class HttpHeaderDelegate<TARGET extends ITarget, ENTITY extends AbsEntity
ALog.w(TAG, "设置header失败,map没有header数据"); ALog.w(TAG, "设置header失败,map没有header数据");
return mTarget; return mTarget;
} }
addHeaders(mTaskEntity, headers);
return mTarget;
}
/**
* 设置请求类型POST或GET默认为在GET
* 只试用于HTTP请求
*
* @param requestEnum {@link RequestEnum}
*/
@Override
public TARGET setRequestMode(RequestEnum requestEnum) {
setRequestMode(mTaskEntity, requestEnum);
return mTarget;
}
public void addHeader(AbsTaskEntity taskEntity, String key, String value) {
if (taskEntity.getHeaders().get(key) == null) {
taskEntity.getHeaders().put(key, value);
} else if (!taskEntity.getHeaders().get(key).equals(value)) {
taskEntity.getHeaders().put(key, value);
}
}
public void setRequestMode(AbsTaskEntity taskEntity, RequestEnum requestEnum) {
taskEntity.setRequestEnum(requestEnum);
}
public void addHeaders(AbsTaskEntity taskEntity, Map<String, String> headers) {
/* /*
两个map比较逻辑 两个map比较逻辑
1比对key是否相同 1比对key是否相同
@ -88,9 +113,9 @@ public class HttpHeaderDelegate<TARGET extends ITarget, ENTITY extends AbsEntity
3只有当上面两个步骤中key value都相同时才能任务两个map数据一致 3只有当上面两个步骤中key value都相同时才能任务两个map数据一致
*/ */
boolean mapEquals = false; boolean mapEquals = false;
if (mTaskEntity.getHeaders().size() == headers.size()) { if (taskEntity.getHeaders().size() == headers.size()) {
int i = 0; int i = 0;
Set<String> keys = mTaskEntity.getHeaders().keySet(); Set<String> keys = taskEntity.getHeaders().keySet();
for (String key : keys) { for (String key : keys) {
if (headers.containsKey(key)) { if (headers.containsKey(key)) {
i++; i++;
@ -98,9 +123,9 @@ public class HttpHeaderDelegate<TARGET extends ITarget, ENTITY extends AbsEntity
break; break;
} }
} }
if (i == mTaskEntity.getHeaders().size()) { if (i == taskEntity.getHeaders().size()) {
int j = 0; int j = 0;
Collection<String> values = mTaskEntity.getHeaders().values(); Collection<String> values = taskEntity.getHeaders().values();
for (String value : values) { for (String value : values) {
if (headers.containsValue(value)) { if (headers.containsValue(value)) {
j++; j++;
@ -108,32 +133,18 @@ public class HttpHeaderDelegate<TARGET extends ITarget, ENTITY extends AbsEntity
break; break;
} }
} }
if (j == mTaskEntity.getHeaders().size()) { if (j == taskEntity.getHeaders().size()) {
mapEquals = true; mapEquals = true;
} }
} }
} }
if (!mapEquals) { if (!mapEquals) {
mTaskEntity.getHeaders().clear(); taskEntity.getHeaders().clear();
Set<String> keys = headers.keySet(); Set<String> keys = headers.keySet();
for (String key : keys) { for (String key : keys) {
mTaskEntity.getHeaders().put(key, headers.get(key)); taskEntity.getHeaders().put(key, headers.get(key));
}
} }
return mTarget;
} }
/**
* 设置请求类型POST或GET默认为在GET
* 只试用于HTTP请求
*
* @param requestEnum {@link RequestEnum}
*/
@Override
public TARGET setRequestMode(RequestEnum requestEnum) {
mTaskEntity.setRequestEnum(requestEnum);
return mTarget;
} }
} }

@ -16,7 +16,11 @@
package com.arialyy.aria.core.download; package com.arialyy.aria.core.download;
import android.support.annotation.CheckResult; import android.support.annotation.CheckResult;
import android.support.annotation.NonNull;
import android.text.TextUtils; import android.text.TextUtils;
import com.arialyy.aria.core.common.RequestEnum;
import com.arialyy.aria.core.delegate.HttpHeaderDelegate;
import com.arialyy.aria.core.inf.IHttpHeaderTarget;
import com.arialyy.aria.core.manager.TEManager; import com.arialyy.aria.core.manager.TEManager;
import com.arialyy.aria.util.ALog; import com.arialyy.aria.util.ALog;
import com.arialyy.aria.util.CommonUtil; import com.arialyy.aria.util.CommonUtil;
@ -24,13 +28,17 @@ import java.io.File;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.Set; import java.util.Set;
/** /**
* Created by AriaL on 2017/6/29. * Created by AriaL on 2017/6/29.
* 下载任务组 * 下载任务组
*/ */
public class DownloadGroupTarget extends BaseGroupTarget<DownloadGroupTarget> { public class DownloadGroupTarget extends BaseGroupTarget<DownloadGroupTarget> implements
IHttpHeaderTarget<DownloadGroupTarget> {
private HttpHeaderDelegate<DownloadGroupTarget, DownloadGroupEntity, DownloadGroupTaskEntity>
mDelegate;
/** /**
* 子任务下载地址 * 子任务下载地址
*/ */
@ -63,6 +71,7 @@ public class DownloadGroupTarget extends BaseGroupTarget<DownloadGroupTarget> {
if (mEntity != null) { if (mEntity != null) {
mDirPathTemp = mEntity.getDirPath(); mDirPathTemp = mEntity.getDirPath();
} }
mDelegate = new HttpHeaderDelegate<>(this, mTaskEntity);
} }
/** /**
@ -253,4 +262,25 @@ public class DownloadGroupTarget extends BaseGroupTarget<DownloadGroupTarget> {
return true; return true;
} }
@Override public DownloadGroupTarget addHeader(@NonNull String key, @NonNull String value) {
for (DownloadTaskEntity subTask : mTaskEntity.getSubTaskEntities()) {
mDelegate.addHeader(subTask, key, value);
}
return mDelegate.addHeader(key, value);
}
@Override public DownloadGroupTarget addHeaders(Map<String, String> headers) {
for (DownloadTaskEntity subTask : mTaskEntity.getSubTaskEntities()) {
mDelegate.addHeaders(subTask, headers);
}
return mDelegate.addHeaders(headers);
}
@Override public DownloadGroupTarget setRequestMode(RequestEnum requestEnum) {
for (DownloadTaskEntity subTask : mTaskEntity.getSubTaskEntities()) {
subTask.setRequestEnum(requestEnum);
}
return mDelegate.setRequestMode(requestEnum);
}
} }

@ -69,6 +69,7 @@ public class DownloadGroupTask extends AbsGroupTask<DownloadGroupTaskEntity> {
} }
@Override public void stop() { @Override public void stop() {
super.stop();
if (!mUtil.isRunning()) { if (!mUtil.isRunning()) {
mListener.onStop(getCurrentProgress()); mListener.onStop(getCurrentProgress());
} else { } else {
@ -77,6 +78,7 @@ public class DownloadGroupTask extends AbsGroupTask<DownloadGroupTaskEntity> {
} }
@Override public void cancel() { @Override public void cancel() {
super.cancel();
if (!mUtil.isRunning()) { if (!mUtil.isRunning()) {
mListener.onCancel(); mListener.onCancel();
} else { } else {

@ -121,6 +121,7 @@ public class DownloadTask extends AbsNormalTask<DownloadTaskEntity> {
* 停止下载 * 停止下载
*/ */
@Override public void stop() { @Override public void stop() {
super.stop();
stop(false); stop(false);
} }
@ -137,6 +138,7 @@ public class DownloadTask extends AbsNormalTask<DownloadTaskEntity> {
* 取消下载 * 取消下载
*/ */
@Override public void cancel() { @Override public void cancel() {
super.cancel();
if (mUtil.isRunning()) { if (mUtil.isRunning()) {
mUtil.cancel(); mUtil.cancel();
} else { } else {

@ -15,7 +15,6 @@
*/ */
package com.arialyy.aria.core.download.downloader; package com.arialyy.aria.core.download.downloader;
import android.util.Log;
import com.arialyy.aria.core.AriaManager; import com.arialyy.aria.core.AriaManager;
import com.arialyy.aria.core.common.IUtil; import com.arialyy.aria.core.common.IUtil;
import com.arialyy.aria.core.download.DownloadEntity; import com.arialyy.aria.core.download.DownloadEntity;
@ -491,10 +490,11 @@ public abstract class AbsGroupUtil implements IUtil {
* 重试下载只有全部都下载失败才会执行任务组的整体重试否则只会执行单个子任务的重试 * 重试下载只有全部都下载失败才会执行任务组的整体重试否则只会执行单个子任务的重试
*/ */
private void reTry(boolean needRetry) { private void reTry(boolean needRetry) {
Downloader dt = mDownloaderMap.get(subEntity.getUrl());
synchronized (AbsGroupUtil.LOCK) { synchronized (AbsGroupUtil.LOCK) {
if (subEntity.getFailNum() < 5 if (dt != null && !dt.isBreak() && needRetry && subEntity.getFailNum() < 5
&& needRetry && (NetUtils.isConnected(AriaManager.APP) || isNotNetRetry)) { && (NetUtils.isConnected(AriaManager.APP) || isNotNetRetry)) {
reStartTask(); reStartTask(dt);
} else { } else {
mFailMap.put(subTaskEntity.getUrl(), subTaskEntity); mFailMap.put(subTaskEntity.getUrl(), subTaskEntity);
mListener.onSubFail(subEntity); mListener.onSubFail(subEntity);
@ -510,7 +510,7 @@ public abstract class AbsGroupUtil implements IUtil {
} }
} }
private void reStartTask() { private void reStartTask(final Downloader dt) {
if (timer != null) { if (timer != null) {
timer.purge(); timer.purge();
timer.cancel(); timer.cancel();
@ -518,7 +518,6 @@ public abstract class AbsGroupUtil implements IUtil {
timer = new Timer(); timer = new Timer();
timer.schedule(new TimerTask() { timer.schedule(new TimerTask() {
@Override public void run() { @Override public void run() {
Downloader dt = mDownloaderMap.get(subEntity.getUrl());
if (dt != null) { if (dt != null) {
dt.retryThreadTask(); dt.retryThreadTask();
} }

@ -37,6 +37,7 @@ public abstract class AbsTask<TASK_ENTITY extends AbsTaskEntity> implements ITas
private String mTargetName; private String mTargetName;
protected Context mContext; protected Context mContext;
protected boolean isHeighestTask = false; protected boolean isHeighestTask = false;
private boolean isCancel = false, isStop = false;
public Handler getOutHandler() { public Handler getOutHandler() {
return mOutHandler; return mOutHandler;
@ -121,6 +122,32 @@ public abstract class AbsTask<TASK_ENTITY extends AbsTaskEntity> implements ITas
return mTaskEntity.getEntity() == null ? null : mTaskEntity.getEntity().getStr(); return mTaskEntity.getEntity() == null ? null : mTaskEntity.getEntity().getStr();
} }
@Override public void stop() {
isStop = true;
}
@Override public void cancel() {
isCancel = true;
}
/**
* 任务是否取消了
*
* @return {@code true}任务已经取消
*/
public boolean isCancel() {
return isCancel;
}
/**
* 任务是否停止了
*
* @return {@code true}任务已经停止
*/
public boolean isStop() {
return isStop;
}
/** /**
* @return 返回原始byte速度需要你在配置文件中配置 * @return 返回原始byte速度需要你在配置文件中配置
* <pre> * <pre>

@ -67,12 +67,21 @@ abstract class AbsTaskQueue<TASK extends AbsTask, TASK_ENTITY extends AbsTaskEnt
* 停止所有任务 * 停止所有任务
*/ */
@Override public void stopAllTask() { @Override public void stopAllTask() {
mCachePool.clear(); //mCachePool.clear();
for (String key : mExecutePool.getAllTask().keySet()) { for (String key : mExecutePool.getAllTask().keySet()) {
TASK task = mExecutePool.getAllTask().get(key); TASK task = mExecutePool.getAllTask().get(key);
if (task != null && task.isRunning()) task.stop(); if (task != null && task.isRunning()) {
task.stop();
} }
} }
for(String key : mCachePool.getAllTask().keySet()){
TASK task = mCachePool.getAllTask().get(key);
if (task != null){
task.stop();
}
}
mCachePool.clear();
}
/** /**
* 获取配置文件配置的最大可执行任务数 * 获取配置文件配置的最大可执行任务数

@ -17,7 +17,6 @@ package com.arialyy.aria.core.scheduler;
import android.os.CountDownTimer; import android.os.CountDownTimer;
import android.os.Message; import android.os.Message;
import android.util.Log;
import com.arialyy.aria.core.AriaManager; import com.arialyy.aria.core.AriaManager;
import com.arialyy.aria.core.download.DownloadGroupTask; import com.arialyy.aria.core.download.DownloadGroupTask;
import com.arialyy.aria.core.download.DownloadTask; import com.arialyy.aria.core.download.DownloadTask;
@ -257,7 +256,7 @@ abstract class AbsSchedulers<TASK_ENTITY extends AbsTaskEntity, TASK extends Abs
* @param task 下载任务 * @param task 下载任务
*/ */
private void handleFailTask(final TASK task) { private void handleFailTask(final TASK task) {
if (!task.needRetry) { if (!task.needRetry || task.isStop() || task.isCancel()) {
mQueue.removeTaskFormQueue(task.getKey()); mQueue.removeTaskFormQueue(task.getKey());
startNextTask(); startNextTask();
return; return;
@ -285,6 +284,7 @@ abstract class AbsSchedulers<TASK_ENTITY extends AbsTaskEntity, TASK extends Abs
TEManager.getInstance().removeTEntity(task.getKey()); TEManager.getInstance().removeTEntity(task.getKey());
return; return;
} }
CountDownTimer timer = new CountDownTimer(interval, 1000) { CountDownTimer timer = new CountDownTimer(interval, 1000) {
@Override public void onTick(long millisUntilFinished) { @Override public void onTick(long millisUntilFinished) {

@ -14,12 +14,12 @@
android:supportsRtl="true" android:supportsRtl="true"
android:theme="@style/AppTheme.NoActionBar"> android:theme="@style/AppTheme.NoActionBar">
<!--android:name=".test.TestActivity"--> <!--android:name=".test.TestActivity"-->
<!--android:name=".test.TestGroupActivity"-->
<!--android:name=".test.AnyRunActivity"--> <!--android:name=".test.AnyRunActivity"-->
<!--android:name=".download.group.DownloadGroupActivity"--> <!--android:name=".download.group.DownloadGroupActivity"-->
<!--android:name=".MainActivity"--> <!--android:name=".MainActivity"-->
<!--android:name=".test.AnyRunActivity"-->
<activity <activity
android:name=".test.AnyRunActivity" android:name=".test.TestGroupActivity"
android:label="@string/app_name"> android:label="@string/app_name">
<intent-filter> <intent-filter>
<action android:name="android.intent.action.MAIN"/> <action android:name="android.intent.action.MAIN"/>

@ -60,6 +60,7 @@ public class UploadTask extends AbsNormalTask<UploadTaskEntity> {
} }
@Override public void stop() { @Override public void stop() {
super.stop();
if (mUtil.isRunning()) { if (mUtil.isRunning()) {
mUtil.stop(); mUtil.stop();
} else { } else {
@ -68,6 +69,7 @@ public class UploadTask extends AbsNormalTask<UploadTaskEntity> {
} }
@Override public void cancel() { @Override public void cancel() {
super.cancel();
if (mUtil.isRunning()) { if (mUtil.isRunning()) {
mUtil.cancel(); mUtil.cancel();
} else { } else {

@ -39,7 +39,7 @@ task clean(type: Delete) {
ext { ext {
userOrg = 'arialyy' userOrg = 'arialyy'
groupId = 'com.arialyy.aria' groupId = 'com.arialyy.aria'
publishVersion = '3.4.3' publishVersion = '3.4.4_dev2'
// publishVersion = '1.0.3' //FTP插件 // publishVersion = '1.0.3' //FTP插件
repoName='maven' repoName='maven'
desc = 'android 下载框架' desc = 'android 下载框架'

Loading…
Cancel
Save