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. 65
      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() {
if (isBreak()) {
return;
}
mConstance.resetState();
checkTask();
mConstance.TASK_RECORD = mRecord;
@ -497,6 +500,9 @@ public abstract class AbsFileer<ENTITY extends AbsNormalEntity, TASK_ENTITY exte
* 启动单线程任务
*/
private void startThreadTask(int[] recordL) {
if (isBreak()) {
return;
}
if (mConstance.CURRENT_LOCATION > 0) {
mListener.onResume(mConstance.CURRENT_LOCATION);
} else {
@ -516,6 +522,10 @@ public abstract class AbsFileer<ENTITY extends AbsNormalEntity, TASK_ENTITY exte
* 重试线程任务只有线程创建成功才能重试
*/
public void retryThreadTask() {
if (isBreak()) {
return;
}
if (mTask == null || mTask.size() == 0) {
ALog.w(TAG, "没有线程任务");
return;
@ -524,7 +534,7 @@ public abstract class AbsFileer<ENTITY extends AbsNormalEntity, TASK_ENTITY exte
for (Integer key : keys) {
AbsThreadTask task = mTask.get(key);
if (task != null && !task.isThreadComplete()) {
task.getConfig().START_LOCATION = task.getCurrentLocation();
task.getConfig().START_LOCATION = task.getConfig().THREAD_RECORD.startLocation;
mConstance.isStop = false;
mConstance.isCancel = false;
mConstance.isRunning = true;
@ -561,6 +571,11 @@ public abstract class AbsFileer<ENTITY extends AbsNormalEntity, TASK_ENTITY exte
config.END_LOCATION = config.FILE_SIZE;
config.SUPPORT_BP = mTaskEntity.isSupportBP();
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);
if (task == null) return;
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);
/**
* 任务是否已经中断
*
* @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();
}
public void setMaxSpeed(double maxSpeed) {
protected void setMaxSpeed(double maxSpeed) {
if (-0.9999 < maxSpeed && maxSpeed < 0.00001) {
mSleepTime = 0;
} else {
@ -108,13 +108,6 @@ public abstract class AbsThreadTask<ENTITY extends AbsNormalEntity, TASK_ENTITY
return mConfig;
}
/**
* 当前线程下载进度
*/
public long getCurrentLocation() {
return mChildCurrentLocation;
}
private boolean filterVersion() {
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP;
}
@ -291,8 +284,8 @@ public abstract class AbsThreadTask<ENTITY extends AbsNormalEntity, TASK_ENTITY
mFailTimes++;
ALog.w(TAG, String.format("任务【%s】thread__%s__正在重试", mConfig.TEMP_FILE.getName(),
mConfig.THREAD_ID));
final long temp = mChildCurrentLocation;
mConfig.START_LOCATION = mChildCurrentLocation == 0 ? mConfig.START_LOCATION : temp;
mConfig.START_LOCATION = mChildCurrentLocation == 0 ? mConfig.START_LOCATION
: mConfig.THREAD_RECORD.startLocation;
AbsThreadTask.this.run();
}
}, RETRY_INTERVAL);

@ -61,11 +61,7 @@ public class HttpHeaderDelegate<TARGET extends ITarget, ENTITY extends AbsEntity
ALog.w(TAG, "设置header失败,header对应的value不能为null");
return mTarget;
}
if (mTaskEntity.getHeaders().get(key) == null) {
mTaskEntity.getHeaders().put(key, value);
} else if (!mTaskEntity.getHeaders().get(key).equals(value)) {
mTaskEntity.getHeaders().put(key, value);
}
addHeader(mTaskEntity, key, value);
return mTarget;
}
@ -81,16 +77,45 @@ public class HttpHeaderDelegate<TARGET extends ITarget, ENTITY extends AbsEntity
ALog.w(TAG, "设置header失败,map没有header数据");
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比较逻辑
1比对key是否相同
2如果key相同比对value是否相同
3只有当上面两个步骤中key value都相同时才能任务两个map数据一致
*/
boolean mapEquals = false;
if (mTaskEntity.getHeaders().size() == headers.size()) {
if (taskEntity.getHeaders().size() == headers.size()) {
int i = 0;
Set<String> keys = mTaskEntity.getHeaders().keySet();
Set<String> keys = taskEntity.getHeaders().keySet();
for (String key : keys) {
if (headers.containsKey(key)) {
i++;
@ -98,9 +123,9 @@ public class HttpHeaderDelegate<TARGET extends ITarget, ENTITY extends AbsEntity
break;
}
}
if (i == mTaskEntity.getHeaders().size()) {
if (i == taskEntity.getHeaders().size()) {
int j = 0;
Collection<String> values = mTaskEntity.getHeaders().values();
Collection<String> values = taskEntity.getHeaders().values();
for (String value : values) {
if (headers.containsValue(value)) {
j++;
@ -108,32 +133,18 @@ public class HttpHeaderDelegate<TARGET extends ITarget, ENTITY extends AbsEntity
break;
}
}
if (j == mTaskEntity.getHeaders().size()) {
if (j == taskEntity.getHeaders().size()) {
mapEquals = true;
}
}
}
if (!mapEquals) {
mTaskEntity.getHeaders().clear();
taskEntity.getHeaders().clear();
Set<String> keys = headers.keySet();
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;
import android.support.annotation.CheckResult;
import android.support.annotation.NonNull;
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.util.ALog;
import com.arialyy.aria.util.CommonUtil;
@ -24,13 +28,17 @@ import java.io.File;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* 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) {
mDirPathTemp = mEntity.getDirPath();
}
mDelegate = new HttpHeaderDelegate<>(this, mTaskEntity);
}
/**
@ -253,4 +262,25 @@ public class DownloadGroupTarget extends BaseGroupTarget<DownloadGroupTarget> {
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() {
super.stop();
if (!mUtil.isRunning()) {
mListener.onStop(getCurrentProgress());
} else {
@ -77,6 +78,7 @@ public class DownloadGroupTask extends AbsGroupTask<DownloadGroupTaskEntity> {
}
@Override public void cancel() {
super.cancel();
if (!mUtil.isRunning()) {
mListener.onCancel();
} else {

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

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

@ -37,6 +37,7 @@ public abstract class AbsTask<TASK_ENTITY extends AbsTaskEntity> implements ITas
private String mTargetName;
protected Context mContext;
protected boolean isHeighestTask = false;
private boolean isCancel = false, isStop = false;
public Handler getOutHandler() {
return mOutHandler;
@ -121,6 +122,32 @@ public abstract class AbsTask<TASK_ENTITY extends AbsTaskEntity> implements ITas
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速度需要你在配置文件中配置
* <pre>

@ -67,11 +67,20 @@ abstract class AbsTaskQueue<TASK extends AbsTask, TASK_ENTITY extends AbsTaskEnt
* 停止所有任务
*/
@Override public void stopAllTask() {
mCachePool.clear();
//mCachePool.clear();
for (String key : mExecutePool.getAllTask().keySet()) {
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.Message;
import android.util.Log;
import com.arialyy.aria.core.AriaManager;
import com.arialyy.aria.core.download.DownloadGroupTask;
import com.arialyy.aria.core.download.DownloadTask;
@ -257,7 +256,7 @@ abstract class AbsSchedulers<TASK_ENTITY extends AbsTaskEntity, TASK extends Abs
* @param task 下载任务
*/
private void handleFailTask(final TASK task) {
if (!task.needRetry) {
if (!task.needRetry || task.isStop() || task.isCancel()) {
mQueue.removeTaskFormQueue(task.getKey());
startNextTask();
return;
@ -285,6 +284,7 @@ abstract class AbsSchedulers<TASK_ENTITY extends AbsTaskEntity, TASK extends Abs
TEManager.getInstance().removeTEntity(task.getKey());
return;
}
CountDownTimer timer = new CountDownTimer(interval, 1000) {
@Override public void onTick(long millisUntilFinished) {

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

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

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

Loading…
Cancel
Save