Merge branch 'v_3.0'

pull/330/head
AriaLyy 7 years ago
commit a63cc11966
  1. 36
      Aria/src/main/java/com/arialyy/aria/core/common/AbsThreadTask.java
  2. 17
      Aria/src/main/java/com/arialyy/aria/core/download/BaseGroupTarget.java
  3. 21
      Aria/src/main/java/com/arialyy/aria/core/download/DownloadGroupTarget.java
  4. 20
      Aria/src/main/java/com/arialyy/aria/core/download/downloader/AbsGroupUtil.java
  5. 12
      Aria/src/main/java/com/arialyy/aria/core/download/downloader/DownloadGroupUtil.java
  6. 1
      Aria/src/main/java/com/arialyy/aria/core/download/downloader/FtpDirDownloadUtil.java
  7. 38
      app/src/main/java/com/arialyy/simple/download/DownloadModule.java
  8. 4
      app/src/main/java/com/arialyy/simple/download/SingleTaskActivity.java
  9. 3
      app/src/main/java/com/arialyy/simple/download/group/DownloadGroupActivity.java
  10. 27
      app/src/main/java/com/arialyy/simple/download/multi_download/FileListAdapter.java
  11. 5
      app/src/main/java/com/arialyy/simple/download/multi_download/FileListEntity.java
  12. 32
      app/src/main/java/com/arialyy/simple/download/multi_download/MultiTaskActivity.java
  13. 30
      app/src/main/res/values/strings.xml
  14. 6
      build.gradle

@ -140,8 +140,7 @@ public abstract class AbsThreadTask<ENTITY extends AbsEntity, TASK_ENTITY extend
synchronized (AriaManager.LOCK) {
if (mConfig.SUPPORT_BP) {
STATE.CANCEL_NUM++;
Log.d(TAG,
"任务【" + mConfig.TEMP_FILE.getName() + "】thread__" + mConfig.THREAD_ID + "__取消下载");
Log.d(TAG, "任务【" + mConfig.TEMP_FILE.getName() + "】thread__" + mConfig.THREAD_ID + "__取消");
if (STATE.isCancel()) {
File configFile = new File(mConfigFPath);
if (configFile.exists()) {
@ -168,9 +167,9 @@ public abstract class AbsThreadTask<ENTITY extends AbsEntity, TASK_ENTITY extend
protected void fail(long currentLocation, String msg, Exception ex) {
synchronized (AriaManager.LOCK) {
try {
STATE.FAIL_NUM++;
STATE.isRunning = false;
STATE.isStop = true;
//STATE.FAIL_NUM++;
//STATE.isRunning = false;
//STATE.isStop = true;
if (ex != null) {
Log.e(TAG, msg + "\n" + CommonUtil.getPrintException(ex));
} else {
@ -178,10 +177,11 @@ public abstract class AbsThreadTask<ENTITY extends AbsEntity, TASK_ENTITY extend
}
if (mConfig.SUPPORT_BP) {
writeConfig(false, currentLocation);
if (STATE.isFail()) {
Log.e(TAG, "任务【" + mConfig.TEMP_FILE.getName() + "】执行失败");
mListener.onFail(true);
}
//if (STATE.isFail()) {
// Log.e(TAG, "任务【" + mConfig.TEMP_FILE.getName() + "】执行失败");
// mListener.onFail(true);
//}
retryThis(true);
} else {
Log.e(TAG, "任务【" + mConfig.TEMP_FILE.getName() + "】执行失败");
mListener.onFail(true);
@ -193,20 +193,32 @@ public abstract class AbsThreadTask<ENTITY extends AbsEntity, TASK_ENTITY extend
}
/**
* 重试当前线程
* 重试当前线程如果其中一条线程已经下载失败则任务该任务下载失败并且停止该任务的所有线程
*
* @param needRetry 是否可以重试
*/
private void retryThis(boolean needRetry) {
if (mFailNum < RETRY_NUM && needRetry) {
if (mFailTimer != null){
if (mFailTimer != null) {
mFailTimer.purge();
mFailTimer.cancel();
}
mFailTimer = new Timer();
mFailTimer = new Timer(true);
mFailTimer.schedule(new TimerTask() {
@Override public void run() {
mFailNum++;
Log.w(TAG,
"任务【" + mConfig.TEMP_FILE.getName() + "】thread__" + mConfig.THREAD_ID + "__正在重试");
mConfig.START_LOCATION = mChildCurrentLocation;
AbsThreadTask.this.run();
}
}, RETRY_INTERVAL);
} else {
STATE.FAIL_NUM++;
STATE.isRunning = false;
STATE.isStop = true;
Log.e(TAG, "任务【" + mConfig.TEMP_FILE.getName() + "】执行失败");
mListener.onFail(true);
}
}

@ -138,10 +138,20 @@ abstract class BaseGroupTarget<TARGET extends AbsTarget, TASK_ENTITY extends Abs
}
}
/**
* 设置子任务文件名该方法必须在{@link #setDownloadDirPath(String)}之后调用否则不生效
*
* @see #setSubFileName(List)
*/
@Deprecated public TARGET setSubTaskFileName(List<String> subTaskFileName) {
return setSubFileName(subTaskFileName);
}
/**
* 设置子任务文件名该方法必须在{@link #setDownloadDirPath(String)}之后调用否则不生效
*/
public TARGET setSubTaskFileName(List<String> subTaskFileName) {
public TARGET setSubFileName(List<String> subTaskFileName) {
if (subTaskFileName == null || subTaskFileName.isEmpty()) return (TARGET) this;
mSubTaskFileName.addAll(subTaskFileName);
if (mUrls.size() != subTaskFileName.size()) {
@ -187,8 +197,8 @@ abstract class BaseGroupTarget<TARGET extends AbsTarget, TASK_ENTITY extends Abs
for (int i = 0, len = mUrls.size(); i < len; i++) {
DownloadEntity entity = new DownloadEntity();
entity.setUrl(mUrls.get(i));
String fileName = mSubTaskFileName.isEmpty() ? createFileName(entity.getUrl())
: mSubTaskFileName.get(i);
String fileName =
mSubTaskFileName.isEmpty() ? createFileName(entity.getUrl()) : mSubTaskFileName.get(i);
entity.setDownloadPath(mEntity.getDirPath() + "/" + fileName);
entity.setGroupName(mGroupName);
entity.setGroupChild(true);
@ -198,5 +208,4 @@ abstract class BaseGroupTarget<TARGET extends AbsTarget, TASK_ENTITY extends Abs
}
return list;
}
}

@ -16,6 +16,7 @@
package com.arialyy.aria.core.download;
import android.text.TextUtils;
import android.util.Log;
import com.arialyy.aria.orm.DbEntity;
import com.arialyy.aria.util.CheckUtil;
import com.arialyy.aria.util.CommonUtil;
@ -59,6 +60,26 @@ public class DownloadGroupTarget
mEntity = mTaskEntity.entity;
}
/**
* 任务组总任务大小任务组是一个抽象的概念没有真实的数据实体任务组的大小是Aria动态获取子任务大小相加而得到的
* 如果你知道当前任务组总大小你也可以调用该方法给任务组设置大小
*
* 为了更好的用户体验建议直接设置任务组文件大小
*
* @param fileSize 任务组总大小
*/
public DownloadGroupTarget setFileSize(long fileSize) {
if (fileSize <= 0) {
Log.w(TAG, "文件大小不能小于 0");
return this;
}
if (mEntity.getFileSize() <= 1 || mEntity.getFileSize() != fileSize) {
mEntity.setFileSize(fileSize);
mEntity.update();
}
return this;
}
/**
* 如果你是使用{@link DownloadReceiver#load(DownloadGroupEntity)}进行下载操作那么你需要设置任务组的下载地址
*/

@ -44,7 +44,7 @@ abstract class AbsGroupUtil implements IUtil {
* 任务组所有任务总大小
*/
long mTotalSize = 0;
protected long mCurrentLocation = 0;
long mCurrentLocation = 0;
private ExecutorService mExePool;
protected IDownloadGroupListener mListener;
protected DownloadGroupTaskEntity mTaskEntity;
@ -83,6 +83,10 @@ abstract class AbsGroupUtil implements IUtil {
private int mFailNum = 0;
//实际的下载任务数
int mActualTaskNum = 0;
/**
* 是否需要读取文件长度{@code true}需要
*/
boolean isNeedLoadFileSize = true;
AbsGroupUtil(IDownloadGroupListener listener, DownloadGroupTaskEntity taskEntity) {
mListener = listener;
@ -95,6 +99,8 @@ abstract class AbsGroupUtil implements IUtil {
mTasksMap.put(te.getEntity().getUrl(), te);
}
}
mTotalSize = taskEntity.getEntity().getFileSize();
isNeedLoadFileSize = mTotalSize <= 1;
for (DownloadEntity entity : mTaskEntity.entity.getSubTask()) {
File file = new File(entity.getDownloadPath());
if (entity.getState() == IEntity.STATE_COMPLETE && file.exists()) {
@ -106,7 +112,17 @@ abstract class AbsGroupUtil implements IUtil {
mCurrentLocation += file.exists() ? entity.getCurrentProgress() : 0;
mActualTaskNum++;
}
mTotalSize += entity.getFileSize();
if (isNeedLoadFileSize) {
mTotalSize += entity.getFileSize();
}
}
updateFileSize();
}
void updateFileSize() {
if (isNeedLoadFileSize) {
mTaskEntity.getEntity().setFileSize(mTotalSize);
mTaskEntity.getEntity().update();
}
}

@ -91,12 +91,16 @@ public class DownloadGroupUtil extends AbsGroupUtil implements IUtil {
@Override public void onComplete(String url, int code) {
DownloadTaskEntity te = mExeMap.get(url);
if (te != null) {
mTotalSize += te.getEntity().getFileSize();
if (isNeedLoadFileSize) {
mTotalSize += te.getEntity().getFileSize();
}
startChildDownload(te);
}
mInitNum++;
if (mInitNum + mInitFailNum >= mTaskEntity.getEntity().getSubTask().size()) {
if (mInitNum + mInitFailNum >= mTaskEntity.getEntity().getSubTask().size()
|| !isNeedLoadFileSize) {
startRunningFlow();
updateFileSize();
}
}
@ -116,8 +120,10 @@ public class DownloadGroupUtil extends AbsGroupUtil implements IUtil {
if (mActualTaskNum < 0) mActualTaskNum = 0;
}
failNum++;
if (mInitNum + mInitFailNum >= mTaskEntity.getEntity().getSubTask().size()) {
if (mInitNum + mInitFailNum >= mTaskEntity.getEntity().getSubTask().size()
|| !isNeedLoadFileSize) {
startRunningFlow();
updateFileSize();
}
}
};

@ -37,7 +37,6 @@ public class FtpDirDownloadUtil extends AbsGroupUtil {
new FtpDirInfoThread(mTaskEntity, new OnFileInfoCallback() {
@Override public void onComplete(String url, int code) {
if (code >= 200 && code < 300) {
mTotalSize = mTaskEntity.getEntity().getFileSize();
for (DownloadEntity entity : mTaskEntity.entity.getSubTask()) {
mExeMap.put(entity.getUrl(), createChildDownloadTask(entity));
}

@ -20,10 +20,12 @@ import android.content.Context;
import android.content.res.Resources;
import android.os.Environment;
import com.arialyy.aria.core.download.DownloadEntity;
import com.arialyy.aria.util.CommonUtil;
import com.arialyy.simple.R;
import com.arialyy.simple.download.multi_download.FileListEntity;
import com.arialyy.simple.base.BaseModule;
import com.arialyy.simple.download.multi_download.FileListEntity;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
@ -54,14 +56,14 @@ public class DownloadModule extends BaseModule {
* 创建下载地址
*/
public List<FileListEntity> createMultiTestList() {
String[] names = getContext().getResources().getStringArray(R.array.file_nams);
String[] downloadUrl = getContext().getResources().getStringArray(R.array.download_url);
String[] names = getStringArray(R.array.file_nams);
String[] downloadUrl = getStringArray(R.array.download_url);
List<FileListEntity> list = new ArrayList<>();
int i = 0;
for (String name : names) {
FileListEntity entity = new FileListEntity();
entity.name = name;
entity.downloadUrl = downloadUrl[i];
entity.key = downloadUrl[i];
entity.downloadPath = Environment.getExternalStorageDirectory() + "/Download/" + name;
list.add(entity);
i++;
@ -69,6 +71,33 @@ public class DownloadModule extends BaseModule {
return list;
}
private String[] getStringArray(int array) {
return getContext().getResources().getStringArray(array);
}
/**
* 创建任务组
*/
public List<FileListEntity> createGroupTestList() {
List<FileListEntity> list = new ArrayList<>();
list.add(createGroupEntity(R.array.group_urls, R.array.group_names, "任务组_0"));
list.add(createGroupEntity(R.array.group_urls_1, R.array.group_names_1, "任务组_1"));
list.add(createGroupEntity(R.array.group_urls_2, R.array.group_names_2, "任务组_2"));
list.add(createGroupEntity(R.array.group_urls_3, R.array.group_names_3, "任务组_3"));
return list;
}
private FileListEntity createGroupEntity(int urls, int names, String alias) {
FileListEntity entity = new FileListEntity();
entity.urls = getStringArray(urls);
entity.names = getStringArray(names);
entity.isGroup = true;
entity.name = alias;
entity.key = CommonUtil.getMd5Code(Arrays.asList(entity.urls));
entity.downloadPath = Environment.getExternalStorageDirectory() + "/Download/" + alias;
return entity;
}
/**
* 创建下载实体Aria也可以通过下载实体启动下载
*/
@ -80,5 +109,4 @@ public class DownloadModule extends BaseModule {
entity.setDownloadPath(path);
return entity;
}
}

@ -30,10 +30,13 @@ import com.arialyy.aria.core.Aria;
import com.arialyy.aria.core.download.DownloadTarget;
import com.arialyy.aria.core.download.DownloadTask;
import com.arialyy.aria.core.inf.IEntity;
import com.arialyy.aria.util.CommonUtil;
import com.arialyy.frame.util.show.L;
import com.arialyy.frame.util.show.T;
import com.arialyy.simple.R;
import com.arialyy.simple.base.BaseActivity;
import com.arialyy.simple.databinding.ActivitySingleBinding;
import java.io.File;
public class SingleTaskActivity extends BaseActivity<ActivitySingleBinding> {
@ -157,6 +160,7 @@ public class SingleTaskActivity extends BaseActivity<ActivitySingleBinding> {
mCancel.setEnabled(false);
setBtState(true);
getBinding().setSpeed("");
L.d(TAG, "md5Code ==> " + CommonUtil.getFileMD5(new File(task.getDownloadPath())));
}
@Download.onNoSupportBreakPoint(DOWNLOAD_URL)

@ -74,7 +74,8 @@ public class DownloadGroupActivity extends BaseActivity<ActivityDownloadGroupBin
.setDownloadDirPath(
Environment.getExternalStorageDirectory().getPath() + "/Download/group_test_3")
.setGroupAlias("任务组测试")
.setSubTaskFileName(getModule(GroupModule.class).getSubName())
.setSubFileName(getModule(GroupModule.class).getSubName())
.setFileSize(32895492)
.start();
break;
case R.id.stop:

@ -26,6 +26,7 @@ import com.arialyy.aria.core.Aria;
import com.arialyy.simple.R;
import com.arialyy.simple.base.adapter.AbsHolder;
import com.arialyy.simple.base.adapter.AbsRVAdapter;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;
@ -43,8 +44,8 @@ final class FileListAdapter extends AbsRVAdapter<FileListEntity, FileListAdapter
public FileListAdapter(Context context, List<FileListEntity> data) {
super(context, data);
for (int i = 0, len = data.size(); i < len; i++) {
mBtStates.put(data.get(i).downloadUrl, true);
mPositions.put(data.get(i).downloadUrl, i);
mBtStates.put(data.get(i).key, true);
mPositions.put(data.get(i).key, i);
}
}
@ -59,17 +60,27 @@ final class FileListAdapter extends AbsRVAdapter<FileListEntity, FileListAdapter
@Override
protected void bindData(FileListHolder holder, int position, final FileListEntity item) {
holder.name.setText("文件名:" + item.name);
holder.url.setText("下载地址:" + item.downloadUrl);
holder.url.setText("下载地址:" + item.key);
holder.url.setVisibility(item.isGroup ? View.GONE : View.VISIBLE);
holder.path.setText("保存路径:" + item.downloadPath);
if (mBtStates.get(item.downloadUrl)) {
if (mBtStates.get(item.key)) {
holder.bt.setEnabled(true);
holder.bt.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View v) {
Toast.makeText(getContext(), "开始下载:" + item.name, Toast.LENGTH_SHORT).show();
Aria.download(getContext())
.load(item.downloadUrl)
.setDownloadPath(item.downloadPath)
.start();
if (item.isGroup) {
Aria.download(getContext())
.load(Arrays.asList(item.urls))
.setSubFileName(Arrays.asList(item.names))
.setDownloadDirPath(item.downloadPath)
.setGroupAlias(item.name)
.start();
} else {
Aria.download(getContext())
.load(item.key)
.setDownloadPath(item.downloadPath)
.start();
}
}
});
} else {

@ -21,5 +21,8 @@ package com.arialyy.simple.download.multi_download;
*/
public class FileListEntity {
public String name, downloadUrl, downloadPath;
public String name, key, downloadPath;
public boolean isGroup = false;
public String[] urls, names;
}

@ -24,14 +24,14 @@ import android.support.v7.widget.Toolbar;
import android.view.View;
import butterknife.Bind;
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.frame.util.show.T;
import com.arialyy.simple.R;
import com.arialyy.simple.base.BaseActivity;
import com.arialyy.simple.databinding.ActivityMultiBinding;
import com.arialyy.simple.download.DownloadModule;
import com.arialyy.simple.download.SingleTaskActivity;
import java.util.ArrayList;
import java.util.List;
@ -52,6 +52,7 @@ public class MultiTaskActivity extends BaseActivity<ActivityMultiBinding> {
super.init(savedInstanceState);
Aria.download(this).register();
setTitle("多任务下载");
mData.addAll(getModule(DownloadModule.class).createGroupTestList());
mData.addAll(getModule(DownloadModule.class).createMultiTestList());
mAdapter = new FileListAdapter(this, mData);
mList.setLayoutManager(new LinearLayoutManager(this));
@ -93,7 +94,32 @@ public class MultiTaskActivity extends BaseActivity<ActivityMultiBinding> {
mAdapter.updateBtState(task.getKey(), true);
}
@Download.onTaskComplete void taskComplete(DownloadTask task) {
//############################### 任务组 ##############################
@DownloadGroup.onTaskComplete void groupTaskComplete(DownloadGroupTask task) {
mAdapter.updateBtState(task.getKey(), true);
}
@DownloadGroup.onTaskStart void groupTaskStart(DownloadGroupTask task) {
mAdapter.updateBtState(task.getKey(), false);
}
@DownloadGroup.onTaskResume void groupTaskResume(DownloadGroupTask task) {
mAdapter.updateBtState(task.getKey(), false);
}
@DownloadGroup.onTaskStop void groupTaskStop(DownloadGroupTask task) {
mAdapter.updateBtState(task.getKey(), true);
}
@DownloadGroup.onTaskCancel void groupTaskCancel(DownloadGroupTask task) {
mAdapter.updateBtState(task.getKey(), true);
}
@DownloadGroup.onTaskFail void groupTaskFail(DownloadGroupTask task) {
mAdapter.updateBtState(task.getKey(), true);
}
@DownloadGroup.onTaskComplete void taskComplete(DownloadGroupTask task) {
mAdapter.updateBtState(task.getKey(), true);
}

@ -50,10 +50,23 @@
<!--<item>https://res5.d.cn/5a6a3384c1b2be1a65d84b914e6a6fef697637578b6db2eb1056d50b09cf1dcf289d4045df7ef95746e498e3d6a848ab84c89b77aa60194e2c48e5a7cb748265.apk</item>-->
<!--<item>https://res5.d.cn/5a6a3384c1b2be1a52034c72752e8475414630ebc69318b84ef584115ebf5eaaab945ae07b7fe3596afc72a7940ff328d4a9553f6ae92d6c09ba4bfb533137f6.apk</item>-->
<!--<item>https://res5.d.cn/5a6a3384c1b2be1a426f06bfc69034d69c44ae1a01da180cab8e59bd1a5e1a784bac46ba0c64579d14f0e80a4ce4f068af89b0369a393456f4f449a8829cad5c.apk</item>-->
<item>http://static.gaoshouyou.com/d/12/0d/7f120f50c80d2e7b8c4ba24ece4f9cdd.apk</item>
<item>http://static.ilongyuan.cn/rayark/RayarkFZ_2.0.7.apk</item>
</string-array>
<string-array name="group_urls_1">
<item>http://droidweb.smartgame-down.com/201708171123/C183E9C2010C7CABACFB398250A3DB75/new/game1/1/107001/qq_1502875308555.apk?f=web_1</item>
<item>http://droidweb.smartgame-down.com/201708171124/6E5AF91791661236D0A2F60125D3B45F/new/game1/93/113093/00D42A6AA63A039485F510C2E4D69BAA_1498831237.apk?f=web_1</item>
</string-array>
<string-array name="group_urls_2">
<item>http://droidweb.smartgame-down.com/201708171125/005BCF0FFBB75510958D14074286B25F/new/game1/2/106202/qqkj_1502868291416.apk?f=web_1</item>
<item>http://droidweb.smartgame-down.com/201708171126/05FAAA4FD294BC63AA8F792636961D19/new/game1/72/110472/com.taobao.taobao-6.9.0-157-0629_1498744826.apk?f=web_1</item>
</string-array>
<string-array name="group_urls_3">
<item>http://droidweb.smartgame-down.com/201708171127/F55445FD8B7BBA8D72CA4B7D7BD91F90/new/game1/84/33184/txtim_1502854269836.apk?f=web_1</item>
<item>http://droidweb.smartgame-down.com/201708171128/5C4D0289F0B83690BF252420F473FB9F/new/game1/46/108046/com.shuqi.controller-10.5.2.50-137-0703_1499083244.apk?f=web_1</item>
</string-array>
<string-array name="group_names">
@ -62,4 +75,19 @@
<!--<item>天魔幻想.apk</item>-->
</string-array>
<string-array name="group_names_1">
<item>QQ.apk</item>
<item>百度贴吧.apk</item>
</string-array>
<string-array name="group_names_2">
<item>QQ空间.apk</item>
<item>淘宝.apk</item>
</string-array>
<string-array name="group_names_3">
<item>TIM.apk</item>
<item>书旗小说.apk</item>
</string-array>
</resources>

@ -5,8 +5,8 @@ buildscript {
mavenCentral()
}
dependencies {
// classpath 'com.android.tools.build:gradle:2.3.0'
classpath 'com.android.tools.build:gradle:2.2.2'
classpath 'com.android.tools.build:gradle:2.3.3'
// classpath 'com.android.tools.build:gradle:2.2.2'
classpath 'com.novoda:bintray-release:0.5.0'
// classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.7'
// classpath 'com.github.dcendents:android-maven-gradle-plugin:1.5'
@ -37,7 +37,7 @@ task clean(type: Delete) {
ext {
userOrg = 'arialyy'
groupId = 'com.arialyy.aria'
publishVersion = '3.2.20'
publishVersion = '3.2.22'
repoName='maven'
desc = 'android 下载框架'
website = 'https://github.com/AriaLyy/Aria'

Loading…
Cancel
Save