pull/330/head
laoyuyu 7 years ago
parent cb1f0e730b
commit cb6f08aa0d
  1. 83
      Aria/src/main/java/com/arialyy/aria/core/command/normal/ResumeAllCmd.java
  2. 7
      Aria/src/main/java/com/arialyy/aria/core/common/AbsFileer.java
  3. 21
      Aria/src/main/java/com/arialyy/aria/core/common/ProxyHelper.java
  4. 25
      Aria/src/main/java/com/arialyy/aria/core/download/DownloadReceiver.java
  5. 3
      Aria/src/main/java/com/arialyy/aria/core/download/downloader/AbsGroupUtil.java
  6. 6
      Aria/src/main/java/com/arialyy/aria/core/download/downloader/DownloadGroupUtil.java
  7. 3
      Aria/src/main/java/com/arialyy/aria/core/inf/AbsEntity.java
  8. 2
      Aria/src/main/java/com/arialyy/aria/core/scheduler/AbsSchedulers.java
  9. 7
      Aria/src/main/java/com/arialyy/aria/core/upload/UploadReceiver.java
  10. 27
      Aria/src/main/java/com/arialyy/aria/util/CommonUtil.java
  11. 4
      app/src/main/AndroidManifest.xml
  12. 2
      app/src/main/java/com/arialyy/simple/download/group/GroupModule.java
  13. 2
      app/src/main/java/com/arialyy/simple/download/multi_download/MultiTaskActivity.java
  14. 5
      app/src/main/res/values/strings.xml
  15. 2
      build.gradle

@ -1,18 +1,19 @@
package com.arialyy.aria.core.command.normal;
import com.arialyy.aria.core.AriaManager;
import com.arialyy.aria.core.download.DownloadEntity;
import com.arialyy.aria.core.download.DownloadGroupEntity;
import com.arialyy.aria.core.download.DownloadGroupTaskEntity;
import com.arialyy.aria.core.download.DownloadTaskEntity;
import com.arialyy.aria.core.download.wrapper.DGTEWrapper;
import com.arialyy.aria.core.download.wrapper.DTEWrapper;
import com.arialyy.aria.core.inf.AbsTask;
import com.arialyy.aria.core.inf.AbsTaskEntity;
import com.arialyy.aria.core.inf.IEntity;
import com.arialyy.aria.core.manager.TEManager;
import com.arialyy.aria.core.queue.DownloadGroupTaskQueue;
import com.arialyy.aria.core.queue.DownloadTaskQueue;
import com.arialyy.aria.core.queue.UploadTaskQueue;
import com.arialyy.aria.core.upload.UploadEntity;
import com.arialyy.aria.core.upload.UploadTaskEntity;
import com.arialyy.aria.core.upload.wrapper.UTEWrapper;
import com.arialyy.aria.orm.DbEntity;
import com.arialyy.aria.util.ALog;
import com.arialyy.aria.util.CommonUtil;
@ -43,10 +44,10 @@ final class ResumeAllCmd<T extends AbsTaskEntity> extends AbsNormalCmd<T> {
return;
}
if (isDownloadCmd) {
resumeTask(findTaskData(1));
resumeTask(findTaskData(2));
findTaskData(1);
findTaskData(2);
} else {
resumeTask(findTaskData(3));
findTaskData(3);
}
resumeWaitTask();
}
@ -56,53 +57,49 @@ final class ResumeAllCmd<T extends AbsTaskEntity> extends AbsNormalCmd<T> {
*
* @param type {@code 1}单任务下载任务{@code 2}任务组下载任务{@code 3} 单任务上传任务
*/
private List<AbsTaskEntity> findTaskData(int type) {
List<AbsTaskEntity> tempList = new ArrayList<>();
private void findTaskData(int type) {
if (type == 1) {
List<DTEWrapper> wrappers = DbEntity.findRelationData(DTEWrapper.class,
"DownloadTaskEntity.isGroupTask=? and DownloadTaskEntity.state!=?", "false", "1");
if (wrappers != null && !wrappers.isEmpty()) {
for (DTEWrapper w : wrappers) {
tempList.add(w.taskEntity);
List<DownloadEntity> entities =
DbEntity.findDatas(DownloadEntity.class,
"isGroupChild=? and state!=?", "false", "1");
if (entities != null && !entities.isEmpty()) {
for (DownloadEntity entity : entities) {
resumeTask(TEManager.getInstance().getTEntity(DownloadTaskEntity.class, entity.getKey()));
}
}
} else if (type == 2) {
List<DGTEWrapper> wrappers =
DbEntity.findRelationData(DGTEWrapper.class, "DownloadGroupTaskEntity.state!=?", "1");
if (wrappers != null && !wrappers.isEmpty()) {
for (DGTEWrapper w : wrappers) {
tempList.add(w.taskEntity);
List<DownloadGroupEntity> entities =
DbEntity.findDatas(DownloadGroupEntity.class, "state!=?", "1");
if (entities != null && !entities.isEmpty()) {
for (DownloadGroupEntity entity : entities) {
resumeTask(
TEManager.getInstance().getGTEntity(DownloadGroupTaskEntity.class, entity.getUrls()));
}
}
} else if (type == 3) {
List<UTEWrapper> wrappers =
DbEntity.findRelationData(UTEWrapper.class, "UploadTaskEntity.state!=?", "1");
if (wrappers != null && !wrappers.isEmpty()) {
for (UTEWrapper w : wrappers) {
tempList.add(w.taskEntity);
List<UploadEntity> entities =
DbEntity.findDatas(UploadEntity.class, "state!=?", "1");
if (entities != null && !entities.isEmpty()) {
for (UploadEntity entity : entities) {
resumeTask(TEManager.getInstance().getTEntity(UploadTaskEntity.class, entity.getKey()));
}
}
}
return tempList;
}
/**
* 恢复任务
*/
private void resumeTask(List<AbsTaskEntity> taskList) {
if (taskList != null && !taskList.isEmpty()) {
for (AbsTaskEntity te : taskList) {
if (te == null || te.getEntity() == null) continue;
int state = te.getState();
if (state == IEntity.STATE_STOP || state == IEntity.STATE_OTHER) {
resumeEntity(te);
} else if (state == IEntity.STATE_WAIT) {
mWaitList.add(te);
} else if (state == IEntity.STATE_RUNNING) {
if (!mQueue.taskIsRunning(te.getEntity().getKey())) {
resumeEntity(te);
}
}
private void resumeTask(AbsTaskEntity te) {
if (te == null || te.getEntity() == null) return;
int state = te.getState();
if (state == IEntity.STATE_STOP || state == IEntity.STATE_OTHER) {
resumeEntity(te);
} else if (state == IEntity.STATE_WAIT) {
mWaitList.add(te);
} else if (state == IEntity.STATE_RUNNING) {
if (!mQueue.taskIsRunning(te.getEntity().getKey())) {
resumeEntity(te);
}
}
}
@ -117,6 +114,13 @@ final class ResumeAllCmd<T extends AbsTaskEntity> extends AbsNormalCmd<T> {
if (mQueue.getCurrentExePoolNum() < maxTaskNum) {
startTask(createTask(te));
} else {
if (te instanceof DownloadTaskEntity) {
mQueue = DownloadTaskQueue.getInstance();
} else if (te instanceof UploadTaskEntity) {
mQueue = UploadTaskQueue.getInstance();
} else if (te instanceof DownloadGroupTaskEntity) {
mQueue = DownloadGroupTaskQueue.getInstance();
}
createTask(te);
}
}
@ -129,7 +133,8 @@ final class ResumeAllCmd<T extends AbsTaskEntity> extends AbsNormalCmd<T> {
*/
private void resumeEntity(AbsTaskEntity te) {
if (te instanceof DownloadTaskEntity) {
if (te.getRequestType() == AbsTaskEntity.D_FTP || te.getRequestType() == AbsTaskEntity.U_FTP) {
if (te.getRequestType() == AbsTaskEntity.D_FTP
|| te.getRequestType() == AbsTaskEntity.U_FTP) {
te.setUrlEntity(CommonUtil.getFtpUrlInfo(te.getEntity().getKey()));
}
mQueue = DownloadTaskQueue.getInstance();

@ -511,7 +511,12 @@ public abstract class AbsFileer<ENTITY extends AbsNormalEntity, TASK_ENTITY exte
mConstance.isRunning = true;
ALog.d(TAG, String.format("任务【%s】开始重试,线程__%s__【开始位置:%s,结束位置:%s】", mEntity.getFileName(),
key, task.getConfig().START_LOCATION, task.getConfig().END_LOCATION));
mFixedThreadPool.execute(task);
if (!mFixedThreadPool.isShutdown()) {
mFixedThreadPool.execute(task);
} else {
ALog.w(TAG, "线程池已关闭");
mListener.onFail(true);
}
}
}
}

@ -16,7 +16,6 @@
package com.arialyy.aria.core.common;
import com.arialyy.aria.core.AriaManager;
import com.arialyy.aria.util.ALog;
import com.arialyy.aria.util.CommonUtil;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
@ -29,7 +28,8 @@ import java.util.Set;
* 代理参数获取
*/
public class ProxyHelper {
public Set<String> downloadCounter, uploadCounter, downloadGroupCounter, downloadGroupSubCounter;
public Set<String> downloadCounter = new HashSet<>(), uploadCounter = new HashSet<>(),
downloadGroupCounter = new HashSet<>(), downloadGroupSubCounter = new HashSet<>();
public static volatile ProxyHelper INSTANCE = null;
@ -47,9 +47,10 @@ public class ProxyHelper {
}
private void init() {
List<String> classes = CommonUtil.getClassName(AriaManager.APP, "com.arialyy.aria");
List<String> classes = CommonUtil.getClassName(AriaManager.APP,
"com.arialyy.aria.ProxyClassCounter");
for (String className : classes) {
if (!className.startsWith("com.arialyy.aria.ProxyClassCounter")){
if (!className.startsWith("com.arialyy.aria.ProxyClassCounter")) {
continue;
}
count(className);
@ -66,30 +67,18 @@ public class ProxyHelper {
Object object = clazz.newInstance();
Object dc = download.invoke(object);
if (dc != null) {
if (downloadCounter == null) {
downloadCounter = new HashSet<>();
}
downloadCounter.addAll((Set<String>) dc);
}
Object dgc = downloadGroup.invoke(object);
if (dgc != null) {
if (downloadGroupCounter == null) {
downloadGroupCounter = new HashSet<>();
}
downloadGroupCounter.addAll((Set<String>) dgc);
}
Object dgsc = downloadGroupSub.invoke(object);
if (dgsc != null) {
if (downloadGroupSubCounter == null) {
downloadGroupSubCounter = new HashSet<>();
}
downloadGroupSubCounter.addAll((Set<String>) dgsc);
}
Object uc = upload.invoke(object);
if (uc != null) {
if (uploadCounter == null) {
uploadCounter = new HashSet<>();
}
uploadCounter.addAll((Set<String>) uc);
}
} catch (ClassNotFoundException e) {

@ -294,6 +294,9 @@ public class DownloadReceiver extends AbsReceiver {
if (!CheckUtil.checkUrl(downloadUrl)) {
return null;
}
if (!taskExists(downloadUrl)) {
return null;
}
return TEManager.getInstance().getTEntity(DownloadTaskEntity.class, downloadUrl);
}
@ -308,6 +311,9 @@ public class DownloadReceiver extends AbsReceiver {
ALog.e(TAG, "获取任务组实体失败:任务组子任务下载地址列表为null");
return null;
}
if (!taskExists(urls)) {
return null;
}
return TEManager.getInstance().getGTEntity(DownloadGroupTaskEntity.class, urls);
}
@ -322,6 +328,11 @@ public class DownloadReceiver extends AbsReceiver {
ALog.e(TAG, "获取FTP文件夹实体失败:下载路径为null");
return null;
}
boolean b =
DownloadGroupEntity.findFirst(DownloadGroupEntity.class, "groupName=?", dirUrl) != null;
if (!b) {
return null;
}
return TEManager.getInstance().getFDTEntity(DownloadGroupTaskEntity.class, dirUrl);
}
@ -332,6 +343,20 @@ public class DownloadReceiver extends AbsReceiver {
return DownloadEntity.findFirst(DownloadEntity.class, "url=?", downloadUrl) != null;
}
/**
* 判断任务组是否存在
*
* @return {@code true} 存在{@code false} 不存在
*/
public boolean taskExists(List<String> urls) {
if (urls == null || urls.isEmpty()) {
return false;
}
String groupName = CommonUtil.getMd5Code(urls);
return DownloadGroupEntity.findFirst(DownloadGroupEntity.class, "groupName=?", groupName)
!= null;
}
/**
* 获取所有普通下载任务
* 获取未完成的普通任务列表{@link #getAllNotCompletTask()}

@ -259,6 +259,9 @@ public abstract class AbsGroupUtil implements IUtil {
dt.stop();
}
}
if (mDownloaderMap.size() == 0){
mListener.onStop(mCurrentLocation);
}
}
protected void onStop() {

@ -44,6 +44,7 @@ public class DownloadGroupUtil extends AbsGroupUtil implements IUtil {
private int mInitFailNum;
private boolean isStop = false;
private boolean isStart = false;
private int mExeNum;
/**
* 文件信息回调组
@ -89,6 +90,7 @@ public class DownloadGroupUtil extends AbsGroupUtil implements IUtil {
return;
}
Set<String> keys = mExeMap.keySet();
mExeNum = mExeMap.size();
for (String key : keys) {
DownloadTaskEntity taskEntity = mExeMap.get(key);
if (taskEntity != null) {
@ -161,11 +163,11 @@ public class DownloadGroupUtil extends AbsGroupUtil implements IUtil {
*/
private void checkStartFlow() {
synchronized (DownloadGroupUtil.class) {
if (mInitFailNum == mExeMap.size()) {
if (mInitFailNum == mExeNum) {
closeTimer(false);
mListener.onFail(true);
}
if (!isStart && mInitCompleteNum + mInitFailNum == mExeMap.size() || !isNeedLoadFileSize) {
if (!isStart && mInitCompleteNum + mInitFailNum == mExeNum || !isNeedLoadFileSize) {
startRunningFlow();
updateFileSize();
isStart = true;

@ -50,6 +50,9 @@ public abstract class AbsEntity extends DbEntity implements IEntity, Parcelable
*/
private String convertFileSize;
/**
* 任务状态{@link IEntity}
*/
private int state = STATE_WAIT;
/**
* 当前下载进度

@ -158,7 +158,7 @@ abstract class AbsSchedulers<TASK_ENTITY extends AbsTaskEntity, TASK extends Abs
}
/**
* 处理普通任务事件
* 处理普通任务和任务组的事件
*/
private void handleNormalEvent(TASK task, int what) {
switch (what) {

@ -17,11 +17,11 @@ package com.arialyy.aria.core.upload;
import android.support.annotation.CheckResult;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import com.arialyy.aria.core.AriaManager;
import com.arialyy.aria.core.command.ICmd;
import com.arialyy.aria.core.command.normal.NormalCmdFactory;
import com.arialyy.aria.core.common.ProxyHelper;
import com.arialyy.aria.core.download.DownloadEntity;
import com.arialyy.aria.core.inf.AbsReceiver;
import com.arialyy.aria.core.scheduler.UploadSchedulers;
import com.arialyy.aria.orm.DbEntity;
@ -61,8 +61,12 @@ public class UploadReceiver extends AbsReceiver<UploadEntity> {
/**
* 通过上传路径获取上传实体
* 如果任务不存在方便null
*/
public UploadEntity getUploadEntity(String filePath) {
if (TextUtils.isEmpty(filePath)) {
return null;
}
return DbEntity.findFirst(UploadEntity.class, "filePath=?", filePath);
}
@ -98,7 +102,6 @@ public class UploadReceiver extends AbsReceiver<UploadEntity> {
"isGroupChild=? and isComplete=?", "false", "true");
}
@Override public void stopAllTask() {
AriaManager.getInstance(AriaManager.APP)
.setCmd(NormalCmdFactory.getInstance()

@ -40,6 +40,7 @@ import com.arialyy.aria.core.inf.AbsTaskEntity;
import com.arialyy.aria.core.upload.UploadEntity;
import com.arialyy.aria.orm.DbEntity;
import dalvik.system.DexFile;
import dalvik.system.PathClassLoader;
import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
@ -74,26 +75,36 @@ public class CommonUtil {
/**
* 获取某包下所有类
*
* @param packageName
* @param className 过滤的类
* @return 类的完整名称
*/
public static List<String> getClassName(Context context, String packageName) {
public static List<String> getClassName(Context context, String className) {
List<String> classNameList = new ArrayList<>();
try {
String pPath = context.getPackageCodePath();
File dir = new File(pPath).getParentFile();
//PathClassLoader classLoader = (PathClassLoader) context.getClassLoader();
String dPath = dir.getPath();
for (String path : dir.list()) {
String fPath = dPath + "/" + path;
Log.d(TAG, fPath);
if (!fPath.endsWith(".apk")) {
continue;
}
DexFile df = new DexFile(fPath);//通过DexFile查找当前的APK中可执行文件
Enumeration<String> enumeration = df.entries();//获取df中的元素 这里包含了所有可执行的类名 该类名包含了包名+类名的方式
while (enumeration.hasMoreElements()) {//遍历
String className = enumeration.nextElement();
if (className.contains(packageName)) {//在当前所有可执行的类里面查找包含有该包名的所有类
classNameList.add(className);
while (enumeration.hasMoreElements()) {
String _className = enumeration.nextElement();
if (!_className.contains(className)) {
continue;
}
//Class clazz = classLoader.loadClass(_className); // 处理4.4手机无法获取到类的问题
//String cn = clazz.getName();
//if (clazz.getName().contains(className)) {//在当前所有可执行的类里面查找包含有该包名的所有类
// classNameList.add(_className);
//}
if (_className.contains(className)){
classNameList.add(_className);
}
}
df.close();
@ -101,6 +112,9 @@ public class CommonUtil {
} catch (IOException e) {
e.printStackTrace();
}
//catch (ClassNotFoundException e) {
// e.printStackTrace();
//}
return classNameList;
}
@ -395,7 +409,6 @@ public class CommonUtil {
if (subs != null) {
for (DownloadEntity sub : subs) {
File file = new File(sub.getDownloadPath());
Log.d(TAG, "exist == " + file.exists() + ", rf == " + removeFile + ", complete = " + sub.isComplete());
if (file.exists() && (removeFile || !sub.isComplete())) {
file.delete();
}

@ -16,10 +16,10 @@
<!--android:name=".test.TestActivity"-->
<!--android:name=".test.AnyRunActivity"-->
<!--android:name=".test.TestGroupActivity"-->
<!--android:name=".MainActivity"-->
<!--android:name=".test.AnyRunActivity"-->
<!--android:name=".download.group.DownloadGroupActivity"-->
<activity
android:name=".test.AnyRunActivity"
android:name=".MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>

@ -32,7 +32,7 @@ public class GroupModule extends BaseModule {
public List<String> getUrls() {
List<String> urls = new ArrayList<>();
String[] str = getContext().getResources().getStringArray(R.array.group_urls_1);
String[] str = getContext().getResources().getStringArray(R.array.group_url4);
Collections.addAll(urls, str);
return urls;
}

@ -121,6 +121,7 @@ public class MultiTaskActivity extends BaseActivity<ActivityMultiBinding> {
@DownloadGroup.onTaskStop void groupTaskStop(DownloadGroupTask task) {
mAdapter.updateBtState(task.getKey(), true);
Log.d(TAG, String.format("group【%s】stop", task.getTaskName()));
}
@DownloadGroup.onTaskCancel void groupTaskCancel(DownloadGroupTask task) {
@ -129,6 +130,7 @@ public class MultiTaskActivity extends BaseActivity<ActivityMultiBinding> {
@DownloadGroup.onTaskFail void groupTaskFail(DownloadGroupTask task) {
mAdapter.updateBtState(task.getKey(), true);
Log.d(TAG, String.format("group【%s】fail", task.getTaskName()));
}
@DownloadGroup.onTaskComplete void taskComplete(DownloadGroupTask task) {

@ -79,6 +79,11 @@
<item>http://static.gaoshouyou.com/d/92/12/5592a647b8126755647abbe8074fde39.apk</item>
</string-array>
<string-array name="group_url4">
<item>http://120.79.135.3/img/20180601/VR Karts Sprint.ver.1.04.build.15_crack_align.apk</item>
<item>http://120.79.135.3/img/20180601/main.11.com.daydvr.acefishing.vr.freefull.google.global.android.common.obb</item>
</string-array>
<string-array name="group_names">
<!--<item>王者荣耀.apk</item>-->
<item>战斗吧剑灵.apk</item>

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

Loading…
Cancel
Save