任务组代码优化

pull/330/head
AriaLyy 7 years ago
parent 746e8752bd
commit e21d2591af
  1. 2
      Aria/src/main/java/com/arialyy/aria/core/command/group/AbsGroupCmd.java
  2. 2
      Aria/src/main/java/com/arialyy/aria/core/command/group/GroupCmdFactory.java
  3. 1
      Aria/src/main/java/com/arialyy/aria/core/command/normal/AddCmd.java
  4. 23
      Aria/src/main/java/com/arialyy/aria/core/download/DownloadReceiver.java
  5. 12
      Aria/src/main/java/com/arialyy/aria/core/download/DownloadTarget.java
  6. 30
      Aria/src/main/java/com/arialyy/aria/core/download/downloader/DownloadGroupUtil.java
  7. 2
      Aria/src/main/java/com/arialyy/aria/orm/DBConfig.java
  8. 6
      Aria/src/main/java/com/arialyy/aria/orm/SqlHelper.java
  9. 20
      AriaAnnotations/src/main/java/com/arialyy/annotations/AriaConstance.java
  10. 11
      app/src/main/java/com/arialyy/simple/download/DownloadModule.java
  11. 7
      app/src/main/java/com/arialyy/simple/download/group/DownloadGroupActivity.java
  12. 2
      app/src/main/java/com/arialyy/simple/download/multi_download/MultiDownloadActivity.java

@ -29,7 +29,7 @@ import com.arialyy.aria.util.CommonUtil;
* Created by AriaL on 2017/6/29. * Created by AriaL on 2017/6/29.
* 任务组命令 * 任务组命令
*/ */
public abstract class AbsGroupCmd<T extends AbsTaskEntity> extends AbsCmd<T> { abstract class AbsGroupCmd<T extends AbsTaskEntity> extends AbsCmd<T> {
/** /**
* @param targetName 创建任务的对象名 * @param targetName 创建任务的对象名

@ -22,7 +22,7 @@ import com.arialyy.aria.core.inf.AbsTaskEntity;
/** /**
* Created by AriaL on 2017/6/29. * Created by AriaL on 2017/6/29.
*/ */
public class GroupCmdFactory extends AbsCmdFactory<AbsGroupCmd> { class GroupCmdFactory extends AbsCmdFactory<AbsGroupCmd> {
/** /**
* 启动任务 * 启动任务
*/ */

@ -17,7 +17,6 @@
package com.arialyy.aria.core.command.normal; package com.arialyy.aria.core.command.normal;
import android.util.Log; import android.util.Log;
import com.arialyy.aria.core.inf.AbsNormalTask;
import com.arialyy.aria.core.inf.AbsTask; import com.arialyy.aria.core.inf.AbsTask;
import com.arialyy.aria.core.inf.IEntity; import com.arialyy.aria.core.inf.IEntity;
import com.arialyy.aria.core.inf.AbsTaskEntity; import com.arialyy.aria.core.inf.AbsTaskEntity;

@ -25,13 +25,10 @@ import com.arialyy.aria.core.scheduler.DownloadSchedulers;
import com.arialyy.aria.core.scheduler.ISchedulerListener; import com.arialyy.aria.core.scheduler.ISchedulerListener;
import com.arialyy.aria.core.upload.ProxyHelper; import com.arialyy.aria.core.upload.ProxyHelper;
import com.arialyy.aria.orm.DbEntity; import com.arialyy.aria.orm.DbEntity;
import com.arialyy.aria.orm.DbUtil;
import com.arialyy.aria.util.CheckUtil; import com.arialyy.aria.util.CheckUtil;
import com.arialyy.aria.util.CommonUtil; import com.arialyy.aria.util.CommonUtil;
import java.lang.reflect.InvocationTargetException;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
import java.lang.reflect.Method;
/** /**
* Created by lyy on 2016/12/5. * Created by lyy on 2016/12/5.
@ -138,7 +135,8 @@ public class DownloadReceiver extends AbsReceiver {
*/ */
public DownloadEntity getDownloadEntity(String downloadUrl) { public DownloadEntity getDownloadEntity(String downloadUrl) {
CheckUtil.checkDownloadUrl(downloadUrl); CheckUtil.checkDownloadUrl(downloadUrl);
return DbEntity.findFirst(DownloadEntity.class, "downloadUrl=?", downloadUrl); return DbEntity.findFirst(DownloadEntity.class, "downloadUrl=? and isGroupChild='false'",
downloadUrl);
} }
/** /**
@ -146,13 +144,14 @@ public class DownloadReceiver extends AbsReceiver {
*/ */
public DownloadTaskEntity getDownloadTask(String downloadUrl) { public DownloadTaskEntity getDownloadTask(String downloadUrl) {
CheckUtil.checkDownloadUrl(downloadUrl); CheckUtil.checkDownloadUrl(downloadUrl);
return DbEntity.findFirst(DownloadTaskEntity.class, "key=?", downloadUrl); return DbEntity.findFirst(DownloadTaskEntity.class, "key=? and isGroupTask='false'",
downloadUrl);
} }
/** /**
* 通过下载链接获取保存在数据库的下载任务组实体 * 通过下载链接获取保存在数据库的下载任务组实体
*/ */
public DownloadGroupTaskEntity getDownlaodGroupTask(List<String> urls) { public DownloadGroupTaskEntity getDownloadGroupTask(List<String> urls) {
CheckUtil.checkDownloadUrls(urls); CheckUtil.checkDownloadUrls(urls);
String hashCode = CommonUtil.getMd5Code(urls); String hashCode = CommonUtil.getMd5Code(urls);
return DbEntity.findFirst(DownloadGroupTaskEntity.class, "key=?", hashCode); return DbEntity.findFirst(DownloadGroupTaskEntity.class, "key=?", hashCode);
@ -165,8 +164,18 @@ public class DownloadReceiver extends AbsReceiver {
return DownloadEntity.findFirst(DownloadEntity.class, "downloadUrl=?", downloadUrl) != null; return DownloadEntity.findFirst(DownloadEntity.class, "downloadUrl=?", downloadUrl) != null;
} }
/**
* 获取普通下载任务列表
*/
@Override public List<DownloadEntity> getTaskList() { @Override public List<DownloadEntity> getTaskList() {
return DownloadEntity.findAllData(DownloadEntity.class); return DownloadEntity.findDatas(DownloadEntity.class, "isGroupChild=?", "false");
}
/**
* 获取任务组列表
*/
public List<DownloadGroupTaskEntity> getGroupTaskList() {
return DownloadEntity.findAllData(DownloadGroupTaskEntity.class);
} }
/** /**

@ -36,12 +36,9 @@ public class DownloadTarget
DownloadTarget(String url, String targetName) { DownloadTarget(String url, String targetName) {
mTargetName = targetName; mTargetName = targetName;
DownloadEntity entity = DbEntity.findFirst(DownloadEntity.class, "downloadUrl=?", url); DownloadEntity entity = getEntity(url);
if (entity == null) { mTaskEntity = DbEntity.findFirst(DownloadTaskEntity.class, "key=? and isGroupTask='false'",
entity = getEntity(url); entity.getDownloadPath());
}
mEntity = entity;
mTaskEntity = DbEntity.findFirst(DownloadTaskEntity.class, "key=?", entity.getDownloadPath());
if (mTaskEntity == null) { if (mTaskEntity == null) {
mTaskEntity = new DownloadTaskEntity(); mTaskEntity = new DownloadTaskEntity();
mTaskEntity.key = entity.getDownloadPath(); mTaskEntity.key = entity.getDownloadPath();
@ -49,8 +46,9 @@ public class DownloadTarget
mTaskEntity.save(); mTaskEntity.save();
} }
if (mTaskEntity.entity == null) { if (mTaskEntity.entity == null) {
mTaskEntity.entity = mEntity; mTaskEntity.entity = entity;
} }
mEntity = entity;
} }
/** /**

@ -76,24 +76,23 @@ public class DownloadGroupUtil implements IDownloadUtil {
* 文件信息回调组 * 文件信息回调组
*/ */
private SparseArray<FileInfoThread.OnFileInfoCallback> mFileInfoCallbacks = new SparseArray<>(); private SparseArray<FileInfoThread.OnFileInfoCallback> mFileInfoCallbacks = new SparseArray<>();
//任务总数
private int mTaskNum = 0;
//已经完成的任务数
private int mCompleteNum = 0;
//失败的任务数
private int mFailNum = 0;
/** /**
* 该任务组对应的所有任务 * 该任务组对应的所有任务
*/ */
private Map<String, DownloadTaskEntity> mTasksMap = new HashMap<>(); private Map<String, DownloadTaskEntity> mTasksMap = new HashMap<>();
//已经完成的任务数
private int mCompleteNum = 0;
//失败的任务数
private int mFailNum = 0;
//实际的下载任务数
private int mActualTaskNum = 0;
public DownloadGroupUtil(IDownloadListener listener, DownloadGroupTaskEntity taskEntity) { public DownloadGroupUtil(IDownloadListener listener, DownloadGroupTaskEntity taskEntity) {
mListener = listener; mListener = listener;
mTaskEntity = taskEntity; mTaskEntity = taskEntity;
mInfoPool = Executors.newCachedThreadPool(); mInfoPool = Executors.newCachedThreadPool();
mExePool = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors()); mExePool = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
mTaskNum = mTaskEntity.entity.getSubTask().size(); mActualTaskNum = mTaskEntity.entity.getSubTask().size();
List<DownloadTaskEntity> tasks = List<DownloadTaskEntity> tasks =
DbEntity.findDatas(DownloadTaskEntity.class, "groupName=?", mTaskEntity.key); DbEntity.findDatas(DownloadTaskEntity.class, "groupName=?", mTaskEntity.key);
if (tasks != null && !tasks.isEmpty()) { if (tasks != null && !tasks.isEmpty()) {
@ -224,12 +223,14 @@ public class DownloadGroupUtil implements IDownloadUtil {
mFailMap.put(url, te); mFailMap.put(url, te);
mFileInfoCallbacks.put(te.hashCode(), this); mFileInfoCallbacks.put(te.hashCode(), this);
} }
mInitFailNum++;
//404链接不重试下载 //404链接不重试下载
if (!errorMsg.contains("错误码:404")) { if (failNum < 10 && !errorMsg.contains("错误码:404") && !errorMsg.contains(
if (failNum < 10) { "UnknownHostException")) {
mInfoPool.execute(createFileInfoThread(te)); mInfoPool.execute(createFileInfoThread(te));
} } else {
mInitFailNum++;
mActualTaskNum--;
if (mActualTaskNum < 0) mActualTaskNum = 0;
} }
failNum++; failNum++;
if (mInitNum + mInitFailNum == mTaskEntity.getEntity().getSubTask().size()) { if (mInitNum + mInitFailNum == mTaskEntity.getEntity().getSubTask().size()) {
@ -238,7 +239,6 @@ public class DownloadGroupUtil implements IDownloadUtil {
} }
}; };
} }
return new FileInfoThread(taskEntity, callback); return new FileInfoThread(taskEntity, callback);
} }
@ -349,7 +349,7 @@ public class DownloadGroupUtil implements IDownloadUtil {
@Override public void onComplete() { @Override public void onComplete() {
saveData(IEntity.STATE_COMPLETE, entity.getFileSize()); saveData(IEntity.STATE_COMPLETE, entity.getFileSize());
mCompleteNum++; mCompleteNum++;
if (mCompleteNum >= mTaskNum) { if (mCompleteNum + mFailNum >= mActualTaskNum) {
mListener.onComplete(); mListener.onComplete();
closeTimer(); closeTimer();
} }
@ -368,6 +368,8 @@ public class DownloadGroupUtil implements IDownloadUtil {
if (entity.getFailNum() < 5) { if (entity.getFailNum() < 5) {
Downloader dt = mDownloaderMap.get(entity.getDownloadUrl()); Downloader dt = mDownloaderMap.get(entity.getDownloadUrl());
dt.startDownload(); dt.startDownload();
} else {
mFailNum++;
} }
} }

@ -32,7 +32,7 @@ import java.util.Map;
public class DBConfig { public class DBConfig {
static Map<String, Class> mapping = new HashMap<>(); static Map<String, Class> mapping = new HashMap<>();
static String DB_NAME; static String DB_NAME;
static int VERSION = 8; static int VERSION = 9;
static { static {
if (TextUtils.isEmpty(DB_NAME)) { if (TextUtils.isEmpty(DB_NAME)) {

@ -375,9 +375,8 @@ final class SqlHelper extends SQLiteOpenHelper {
/** /**
* 保存一对一的数据 * 保存一对一的数据
* @param field
*/ */
private void saveOneToOneFile(Field field){ private void saveOneToOneFile(Field field) {
} }
@ -498,7 +497,8 @@ final class SqlHelper extends SQLiteOpenHelper {
continue; continue;
} }
if (isPrimary(field)) { if (isPrimary(field)) {
sb.append(" PRIMARY KEY"); //sb.append(" PRIMARY KEY");
sb.append(" NOT NULL");
} }
sb.append(","); sb.append(",");
} }

@ -21,5 +21,25 @@ package com.arialyy.annotations;
public interface AriaConstance { public interface AriaConstance {
String NO_URL = ""; String NO_URL = "";
/**
* 注解方法为普通任务下载
*/
int DOWNLOAD = 0xa1;
/**
* 注解方法为任务组下载
*/
int DOWNLOAD_GROUP = 0xa2;
/**
* 注解方法为普通任务上传
*/
int UPLOAD = 0xb1;
/**
* 注解方法为任务组上传
*/
int UPLOAD_GROUP = 0xb2;
} }

@ -16,27 +16,16 @@
package com.arialyy.simple.download; package com.arialyy.simple.download;
import android.content.BroadcastReceiver;
import android.content.Context; import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.res.Resources; import android.content.res.Resources;
import android.os.Environment; import android.os.Environment;
import android.os.Handler;
import com.arialyy.aria.core.Aria;
import com.arialyy.aria.core.download.DownloadEntity; import com.arialyy.aria.core.download.DownloadEntity;
import com.arialyy.aria.util.CommonUtil;
import com.arialyy.frame.util.AndroidUtils;
import com.arialyy.frame.util.StringUtil;
import com.arialyy.frame.util.show.L;
import com.arialyy.simple.R; import com.arialyy.simple.R;
import com.arialyy.simple.download.multi_download.FileListEntity; import com.arialyy.simple.download.multi_download.FileListEntity;
import com.arialyy.simple.base.BaseModule; import com.arialyy.simple.base.BaseModule;
import java.io.File;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Random;
/** /**
* Created by Lyy on 2016/9/27. * Created by Lyy on 2016/9/27.

@ -25,7 +25,6 @@ import com.arialyy.aria.core.Aria;
import com.arialyy.aria.core.download.DownloadGroupEntity; import com.arialyy.aria.core.download.DownloadGroupEntity;
import com.arialyy.aria.core.download.DownloadGroupTask; import com.arialyy.aria.core.download.DownloadGroupTask;
import com.arialyy.aria.core.download.DownloadGroupTaskEntity; import com.arialyy.aria.core.download.DownloadGroupTaskEntity;
import com.arialyy.aria.util.CommonUtil;
import com.arialyy.frame.util.show.L; 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;
@ -48,14 +47,18 @@ public class DownloadGroupActivity extends BaseActivity<ActivityDownloadGroupBin
Aria.download(this).register(); Aria.download(this).register();
setTitle("任务组"); setTitle("任务组");
mUrls = getModule(GroupModule.class).getUrls(); mUrls = getModule(GroupModule.class).getUrls();
DownloadGroupTaskEntity entity = Aria.download(this).getDownlaodGroupTask(mUrls); DownloadGroupTaskEntity entity = Aria.download(this).getDownloadGroupTask(mUrls);
if (entity != null && entity.getEntity() != null) { if (entity != null && entity.getEntity() != null) {
DownloadGroupEntity groupEntity = entity.getEntity(); DownloadGroupEntity groupEntity = entity.getEntity();
getBinding().setFileSize(groupEntity.getConvertFileSize()); getBinding().setFileSize(groupEntity.getConvertFileSize());
if (groupEntity.getFileSize() == 0) {
getBinding().setProgress(0);
} else {
getBinding().setProgress(groupEntity.isComplete() ? 100 getBinding().setProgress(groupEntity.isComplete() ? 100
: (int) (groupEntity.getCurrentProgress() * 100 / groupEntity.getFileSize())); : (int) (groupEntity.getCurrentProgress() * 100 / groupEntity.getFileSize()));
} }
} }
}
@Override protected int setLayoutId() { @Override protected int setLayoutId() {
return R.layout.activity_download_group; return R.layout.activity_download_group;

@ -19,7 +19,6 @@ package com.arialyy.simple.download.multi_download;
import android.os.Bundle; import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView; import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.Menu; import android.view.Menu;
import android.view.MenuItem; import android.view.MenuItem;
import butterknife.Bind; import butterknife.Bind;
@ -28,7 +27,6 @@ import com.arialyy.aria.core.Aria;
import com.arialyy.aria.core.download.DownloadEntity; import com.arialyy.aria.core.download.DownloadEntity;
import com.arialyy.aria.core.download.DownloadTask; import com.arialyy.aria.core.download.DownloadTask;
import com.arialyy.frame.util.show.L; import com.arialyy.frame.util.show.L;
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.ActivityMultiDownloadBinding; import com.arialyy.simple.databinding.ActivityMultiDownloadBinding;

Loading…
Cancel
Save