任务组测试

pull/330/head
AriaLyy 7 years ago
parent 16612b9c6c
commit 23f063edf2
  1. 34
      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. 38
      app/src/main/java/com/arialyy/simple/download/DownloadModule.java
  4. 4
      app/src/main/java/com/arialyy/simple/download/SingleTaskActivity.java
  5. 21
      app/src/main/java/com/arialyy/simple/download/multi_download/FileListAdapter.java
  6. 5
      app/src/main/java/com/arialyy/simple/download/multi_download/FileListEntity.java
  7. 32
      app/src/main/java/com/arialyy/simple/download/multi_download/MultiTaskActivity.java
  8. 30
      app/src/main/res/values/strings.xml

@ -140,8 +140,7 @@ public abstract class AbsThreadTask<ENTITY extends AbsEntity, TASK_ENTITY extend
synchronized (AriaManager.LOCK) { synchronized (AriaManager.LOCK) {
if (mConfig.SUPPORT_BP) { if (mConfig.SUPPORT_BP) {
STATE.CANCEL_NUM++; STATE.CANCEL_NUM++;
Log.d(TAG, Log.d(TAG, "任务【" + mConfig.TEMP_FILE.getName() + "】thread__" + mConfig.THREAD_ID + "__取消");
"任务【" + mConfig.TEMP_FILE.getName() + "】thread__" + mConfig.THREAD_ID + "__取消下载");
if (STATE.isCancel()) { if (STATE.isCancel()) {
File configFile = new File(mConfigFPath); File configFile = new File(mConfigFPath);
if (configFile.exists()) { 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) { protected void fail(long currentLocation, String msg, Exception ex) {
synchronized (AriaManager.LOCK) { synchronized (AriaManager.LOCK) {
try { try {
STATE.FAIL_NUM++; //STATE.FAIL_NUM++;
STATE.isRunning = false; //STATE.isRunning = false;
STATE.isStop = true; //STATE.isStop = true;
if (ex != null) { if (ex != null) {
Log.e(TAG, msg + "\n" + CommonUtil.getPrintException(ex)); Log.e(TAG, msg + "\n" + CommonUtil.getPrintException(ex));
} else { } else {
@ -178,10 +177,11 @@ public abstract class AbsThreadTask<ENTITY extends AbsEntity, TASK_ENTITY extend
} }
if (mConfig.SUPPORT_BP) { if (mConfig.SUPPORT_BP) {
writeConfig(false, currentLocation); writeConfig(false, currentLocation);
if (STATE.isFail()) { //if (STATE.isFail()) {
Log.e(TAG, "任务【" + mConfig.TEMP_FILE.getName() + "】执行失败"); // Log.e(TAG, "任务【" + mConfig.TEMP_FILE.getName() + "】执行失败");
mListener.onFail(true); // mListener.onFail(true);
} //}
retryThis(true);
} else { } else {
Log.e(TAG, "任务【" + mConfig.TEMP_FILE.getName() + "】执行失败"); Log.e(TAG, "任务【" + mConfig.TEMP_FILE.getName() + "】执行失败");
mListener.onFail(true); mListener.onFail(true);
@ -193,7 +193,9 @@ public abstract class AbsThreadTask<ENTITY extends AbsEntity, TASK_ENTITY extend
} }
/** /**
* 重试当前线程 * 重试当前线程如果其中一条线程已经下载失败则任务该任务下载失败并且停止该任务的所有线程
*
* @param needRetry 是否可以重试
*/ */
private void retryThis(boolean needRetry) { private void retryThis(boolean needRetry) {
if (mFailNum < RETRY_NUM && needRetry) { if (mFailNum < RETRY_NUM && needRetry) {
@ -201,12 +203,22 @@ public abstract class AbsThreadTask<ENTITY extends AbsEntity, TASK_ENTITY extend
mFailTimer.purge(); mFailTimer.purge();
mFailTimer.cancel(); mFailTimer.cancel();
} }
mFailTimer = new Timer(); mFailTimer = new Timer(true);
mFailTimer.schedule(new TimerTask() { mFailTimer.schedule(new TimerTask() {
@Override public void run() { @Override public void run() {
mFailNum++;
Log.w(TAG,
"任务【" + mConfig.TEMP_FILE.getName() + "】thread__" + mConfig.THREAD_ID + "__正在重试");
mConfig.START_LOCATION = mChildCurrentLocation;
AbsThreadTask.this.run(); AbsThreadTask.this.run();
} }
}, RETRY_INTERVAL); }, 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)}之后调用否则不生效 * 设置子任务文件名该方法必须在{@link #setDownloadDirPath(String)}之后调用否则不生效
*/ */
public TARGET setSubTaskFileName(List<String> subTaskFileName) { public TARGET setSubFileName(List<String> subTaskFileName) {
if (subTaskFileName == null || subTaskFileName.isEmpty()) return (TARGET) this; if (subTaskFileName == null || subTaskFileName.isEmpty()) return (TARGET) this;
mSubTaskFileName.addAll(subTaskFileName); mSubTaskFileName.addAll(subTaskFileName);
if (mUrls.size() != subTaskFileName.size()) { 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++) { for (int i = 0, len = mUrls.size(); i < len; i++) {
DownloadEntity entity = new DownloadEntity(); DownloadEntity entity = new DownloadEntity();
entity.setUrl(mUrls.get(i)); entity.setUrl(mUrls.get(i));
String fileName = mSubTaskFileName.isEmpty() ? createFileName(entity.getUrl()) String fileName =
: mSubTaskFileName.get(i); mSubTaskFileName.isEmpty() ? createFileName(entity.getUrl()) : mSubTaskFileName.get(i);
entity.setDownloadPath(mEntity.getDirPath() + "/" + fileName); entity.setDownloadPath(mEntity.getDirPath() + "/" + fileName);
entity.setGroupName(mGroupName); entity.setGroupName(mGroupName);
entity.setGroupChild(true); entity.setGroupChild(true);
@ -198,5 +208,4 @@ abstract class BaseGroupTarget<TARGET extends AbsTarget, TASK_ENTITY extends Abs
} }
return list; return list;
} }
} }

@ -20,10 +20,12 @@ import android.content.Context;
import android.content.res.Resources; import android.content.res.Resources;
import android.os.Environment; import android.os.Environment;
import com.arialyy.aria.core.download.DownloadEntity; import com.arialyy.aria.core.download.DownloadEntity;
import com.arialyy.aria.util.CommonUtil;
import com.arialyy.simple.R; import com.arialyy.simple.R;
import com.arialyy.simple.download.multi_download.FileListEntity;
import com.arialyy.simple.base.BaseModule; import com.arialyy.simple.base.BaseModule;
import com.arialyy.simple.download.multi_download.FileListEntity;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
@ -54,14 +56,14 @@ public class DownloadModule extends BaseModule {
* 创建下载地址 * 创建下载地址
*/ */
public List<FileListEntity> createMultiTestList() { public List<FileListEntity> createMultiTestList() {
String[] names = getContext().getResources().getStringArray(R.array.file_nams); String[] names = getStringArray(R.array.file_nams);
String[] downloadUrl = getContext().getResources().getStringArray(R.array.download_url); String[] downloadUrl = getStringArray(R.array.download_url);
List<FileListEntity> list = new ArrayList<>(); List<FileListEntity> list = new ArrayList<>();
int i = 0; int i = 0;
for (String name : names) { for (String name : names) {
FileListEntity entity = new FileListEntity(); FileListEntity entity = new FileListEntity();
entity.name = name; entity.name = name;
entity.downloadUrl = downloadUrl[i]; entity.key = downloadUrl[i];
entity.downloadPath = Environment.getExternalStorageDirectory() + "/Download/" + name; entity.downloadPath = Environment.getExternalStorageDirectory() + "/Download/" + name;
list.add(entity); list.add(entity);
i++; i++;
@ -69,6 +71,33 @@ public class DownloadModule extends BaseModule {
return list; 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也可以通过下载实体启动下载 * 创建下载实体Aria也可以通过下载实体启动下载
*/ */
@ -80,5 +109,4 @@ public class DownloadModule extends BaseModule {
entity.setDownloadPath(path); entity.setDownloadPath(path);
return entity; 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.DownloadTarget;
import com.arialyy.aria.core.download.DownloadTask; import com.arialyy.aria.core.download.DownloadTask;
import com.arialyy.aria.core.inf.IEntity; 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.frame.util.show.T;
import com.arialyy.simple.R; import com.arialyy.simple.R;
import com.arialyy.simple.base.BaseActivity; import com.arialyy.simple.base.BaseActivity;
import com.arialyy.simple.databinding.ActivitySingleBinding; import com.arialyy.simple.databinding.ActivitySingleBinding;
import java.io.File;
public class SingleTaskActivity extends BaseActivity<ActivitySingleBinding> { public class SingleTaskActivity extends BaseActivity<ActivitySingleBinding> {
@ -157,6 +160,7 @@ public class SingleTaskActivity extends BaseActivity<ActivitySingleBinding> {
mCancel.setEnabled(false); mCancel.setEnabled(false);
setBtState(true); setBtState(true);
getBinding().setSpeed(""); getBinding().setSpeed("");
L.d(TAG, "md5Code ==> " + CommonUtil.getFileMD5(new File(task.getDownloadPath())));
} }
@Download.onNoSupportBreakPoint(DOWNLOAD_URL) @Download.onNoSupportBreakPoint(DOWNLOAD_URL)

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

@ -21,5 +21,8 @@ package com.arialyy.simple.download.multi_download;
*/ */
public class FileListEntity { 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 android.view.View;
import butterknife.Bind; import butterknife.Bind;
import com.arialyy.annotations.Download; import com.arialyy.annotations.Download;
import com.arialyy.annotations.DownloadGroup;
import com.arialyy.aria.core.Aria; 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.download.DownloadTask;
import com.arialyy.frame.util.show.T;
import com.arialyy.simple.R; import com.arialyy.simple.R;
import com.arialyy.simple.base.BaseActivity; import com.arialyy.simple.base.BaseActivity;
import com.arialyy.simple.databinding.ActivityMultiBinding; import com.arialyy.simple.databinding.ActivityMultiBinding;
import com.arialyy.simple.download.DownloadModule; import com.arialyy.simple.download.DownloadModule;
import com.arialyy.simple.download.SingleTaskActivity;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -52,6 +52,7 @@ public class MultiTaskActivity extends BaseActivity<ActivityMultiBinding> {
super.init(savedInstanceState); super.init(savedInstanceState);
Aria.download(this).register(); Aria.download(this).register();
setTitle("多任务下载"); setTitle("多任务下载");
mData.addAll(getModule(DownloadModule.class).createGroupTestList());
mData.addAll(getModule(DownloadModule.class).createMultiTestList()); mData.addAll(getModule(DownloadModule.class).createMultiTestList());
mAdapter = new FileListAdapter(this, mData); mAdapter = new FileListAdapter(this, mData);
mList.setLayoutManager(new LinearLayoutManager(this)); mList.setLayoutManager(new LinearLayoutManager(this));
@ -93,7 +94,32 @@ public class MultiTaskActivity extends BaseActivity<ActivityMultiBinding> {
mAdapter.updateBtState(task.getKey(), true); 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); mAdapter.updateBtState(task.getKey(), true);
} }

@ -50,10 +50,23 @@
<!--<item>https://res5.d.cn/5a6a3384c1b2be1a65d84b914e6a6fef697637578b6db2eb1056d50b09cf1dcf289d4045df7ef95746e498e3d6a848ab84c89b77aa60194e2c48e5a7cb748265.apk</item>--> <!--<item>https://res5.d.cn/5a6a3384c1b2be1a65d84b914e6a6fef697637578b6db2eb1056d50b09cf1dcf289d4045df7ef95746e498e3d6a848ab84c89b77aa60194e2c48e5a7cb748265.apk</item>-->
<!--<item>https://res5.d.cn/5a6a3384c1b2be1a52034c72752e8475414630ebc69318b84ef584115ebf5eaaab945ae07b7fe3596afc72a7940ff328d4a9553f6ae92d6c09ba4bfb533137f6.apk</item>--> <!--<item>https://res5.d.cn/5a6a3384c1b2be1a52034c72752e8475414630ebc69318b84ef584115ebf5eaaab945ae07b7fe3596afc72a7940ff328d4a9553f6ae92d6c09ba4bfb533137f6.apk</item>-->
<!--<item>https://res5.d.cn/5a6a3384c1b2be1a426f06bfc69034d69c44ae1a01da180cab8e59bd1a5e1a784bac46ba0c64579d14f0e80a4ce4f068af89b0369a393456f4f449a8829cad5c.apk</item>--> <!--<item>https://res5.d.cn/5a6a3384c1b2be1a426f06bfc69034d69c44ae1a01da180cab8e59bd1a5e1a784bac46ba0c64579d14f0e80a4ce4f068af89b0369a393456f4f449a8829cad5c.apk</item>-->
<item>http://static.gaoshouyou.com/d/12/0d/7f120f50c80d2e7b8c4ba24ece4f9cdd.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> <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>
<string-array name="group_names"> <string-array name="group_names">
@ -62,4 +75,19 @@
<!--<item>天魔幻想.apk</item>--> <!--<item>天魔幻想.apk</item>-->
</string-array> </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> </resources>

Loading…
Cancel
Save