优化任务配置

v3.6.6
laoyuyu 6 years ago
parent 56af75c35c
commit 1a83617045
  1. 7
      .github/ISSUE_TEMPLATE/Custom.md
  2. 28
      Aria/src/main/java/com/arialyy/aria/core/AriaManager.java
  3. 441
      Aria/src/main/java/com/arialyy/aria/core/ConfigHelper.java.back
  4. 4
      Aria/src/main/java/com/arialyy/aria/core/common/AbsFileer.java
  5. 41
      Aria/src/main/java/com/arialyy/aria/core/common/AbsThreadTask.java
  6. 22
      Aria/src/main/java/com/arialyy/aria/core/common/BaseListener.java
  7. 4
      Aria/src/main/java/com/arialyy/aria/core/common/RecordWrapper.java
  8. 4
      Aria/src/main/java/com/arialyy/aria/core/common/SubThreadConfig.java
  9. 7
      Aria/src/main/java/com/arialyy/aria/core/common/ftp/AbsFtpThreadTask.java
  10. 112
      Aria/src/main/java/com/arialyy/aria/core/config/AppConfig.java
  11. 51
      Aria/src/main/java/com/arialyy/aria/core/config/BaseConfig.java
  12. 221
      Aria/src/main/java/com/arialyy/aria/core/config/BaseTaskConfig.java
  13. 2
      Aria/src/main/java/com/arialyy/aria/core/config/ConfigType.java
  14. 117
      Aria/src/main/java/com/arialyy/aria/core/config/Configuration.java
  15. 114
      Aria/src/main/java/com/arialyy/aria/core/config/DGroupConfig.java
  16. 75
      Aria/src/main/java/com/arialyy/aria/core/config/DownloadConfig.java
  17. 5
      Aria/src/main/java/com/arialyy/aria/core/config/TTaskConfigAdapeter.java
  18. 44
      Aria/src/main/java/com/arialyy/aria/core/config/UploadConfig.java
  19. 25
      Aria/src/main/java/com/arialyy/aria/core/config/XMLReader.java
  20. 6
      Aria/src/main/java/com/arialyy/aria/core/download/BaseDListener.java
  21. 4
      Aria/src/main/java/com/arialyy/aria/core/download/DGEntityWrapper.java
  22. 8
      Aria/src/main/java/com/arialyy/aria/core/download/DGTaskWrapper.java
  23. 10
      Aria/src/main/java/com/arialyy/aria/core/download/DTaskWrapper.java
  24. 6
      Aria/src/main/java/com/arialyy/aria/core/download/DownloadGroupListener.java
  25. 6
      Aria/src/main/java/com/arialyy/aria/core/download/DownloadGroupTask.java
  26. 8
      Aria/src/main/java/com/arialyy/aria/core/download/DownloadTask.java
  27. 24
      Aria/src/main/java/com/arialyy/aria/core/download/downloader/FtpThreadTask.java
  28. 2
      Aria/src/main/java/com/arialyy/aria/core/download/downloader/HttpFileInfoThread.java
  29. 24
      Aria/src/main/java/com/arialyy/aria/core/download/downloader/HttpThreadTask.java
  30. 14
      Aria/src/main/java/com/arialyy/aria/core/inf/AbsTaskWrapper.java
  31. 4
      Aria/src/main/java/com/arialyy/aria/core/queue/DownloadGroupTaskQueue.java
  32. 13
      Aria/src/main/java/com/arialyy/aria/core/scheduler/AbsSchedulers.java
  33. 9
      Aria/src/main/java/com/arialyy/aria/core/upload/BaseUListener.java
  34. 6
      Aria/src/main/java/com/arialyy/aria/core/upload/UTaskWrapper.java
  35. 15
      Aria/src/main/java/com/arialyy/aria/core/upload/uploader/FtpThreadTask.java
  36. 21
      Aria/src/main/java/com/arialyy/aria/core/upload/uploader/HttpThreadTask.java
  37. 2
      Aria/src/main/java/com/arialyy/aria/orm/AbsDbWrapper.java
  38. 4
      Aria/src/main/java/com/arialyy/aria/orm/DbEntity.java
  39. 4
      Aria/src/main/java/com/arialyy/aria/orm/DelegateFind.java
  40. 2
      Aria/src/main/java/com/arialyy/aria/orm/DelegateUpdate.java
  41. 2
      Aria/src/main/java/com/arialyy/aria/orm/DelegateWrapper.java
  42. 6
      app/src/main/assets/aria_config.xml
  43. 621
      aria/src/main/java/com/arialyy/aria/core/Configuration.java
  44. 6
      aria/src/main/java/com/arialyy/aria/core/upload/UploadTask.java

@ -4,7 +4,7 @@ about: 描述你遇到的问题或bug
--- ---
## 版本 ## Aria版本
## 什么问题 ## 什么问题
@ -13,8 +13,5 @@ about: 描述你遇到的问题或bug
## 如何复现此问题 ## 如何复现此问题
## 手机型号 ## 控制台日志
## 控制台日志(越完整越好)

@ -28,6 +28,12 @@ import android.support.v4.app.Fragment;
import android.widget.PopupWindow; import android.widget.PopupWindow;
import com.arialyy.aria.core.command.ICmd; import com.arialyy.aria.core.command.ICmd;
import com.arialyy.aria.core.common.QueueMod; import com.arialyy.aria.core.common.QueueMod;
import com.arialyy.aria.core.config.AppConfig;
import com.arialyy.aria.core.config.DGroupConfig;
import com.arialyy.aria.core.config.DownloadConfig;
import com.arialyy.aria.core.config.UploadConfig;
import com.arialyy.aria.core.config.XMLReader;
import com.arialyy.aria.core.config.Configuration;
import com.arialyy.aria.core.download.DownloadEntity; import com.arialyy.aria.core.download.DownloadEntity;
import com.arialyy.aria.core.download.DownloadGroupEntity; import com.arialyy.aria.core.download.DownloadGroupEntity;
import com.arialyy.aria.core.download.DownloadReceiver; import com.arialyy.aria.core.download.DownloadReceiver;
@ -54,9 +60,7 @@ import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.SAXException; import org.xml.sax.SAXException;
/** /**
* Created by lyy on 2016/12/1. * Created by lyy on 2016/12/1. https://github.com/AriaLyy/Aria Aria管理器,任务操作在这里执行
* https://github.com/AriaLyy/Aria
* Aria管理器任务操作在这里执行
*/ */
@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH) public class AriaManager { @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH) public class AriaManager {
private static final String TAG = "AriaManager"; private static final String TAG = "AriaManager";
@ -72,10 +76,10 @@ import org.xml.sax.SAXException;
private Map<String, List<String>> mSubClass = new ConcurrentHashMap<>(); private Map<String, List<String>> mSubClass = new ConcurrentHashMap<>();
public static Context APP; public static Context APP;
private List<ICmd> mCommands = new ArrayList<>(); private List<ICmd> mCommands = new ArrayList<>();
private Configuration.DownloadConfig mDConfig; private DownloadConfig mDConfig;
private Configuration.UploadConfig mUConfig; private UploadConfig mUConfig;
private Configuration.AppConfig mAConfig; private AppConfig mAConfig;
private Configuration.DGroupConfig mDGConfig; private DGroupConfig mDGConfig;
private AriaManager(Context context) { private AriaManager(Context context) {
APP = context.getApplicationContext(); APP = context.getApplicationContext();
@ -179,7 +183,7 @@ import org.xml.sax.SAXException;
* </code> * </code>
* </pre> * </pre>
*/ */
public Configuration.DownloadConfig getDownloadConfig() { public DownloadConfig getDownloadConfig() {
return mDConfig; return mDConfig;
} }
@ -192,14 +196,14 @@ import org.xml.sax.SAXException;
* </code> * </code>
* </pre> * </pre>
*/ */
public Configuration.UploadConfig getUploadConfig() { public UploadConfig getUploadConfig() {
return mUConfig; return mUConfig;
} }
/** /**
* 获取APP配置 * 获取APP配置
*/ */
public Configuration.AppConfig getAppConfig() { public AppConfig getAppConfig() {
return mAConfig; return mAConfig;
} }
@ -212,7 +216,7 @@ import org.xml.sax.SAXException;
* </code> * </code>
* </pre> * </pre>
*/ */
public Configuration.DGroupConfig getDGroupConfig(){ public DGroupConfig getDGroupConfig() {
return mDGConfig; return mDGConfig;
} }
@ -423,7 +427,7 @@ import org.xml.sax.SAXException;
*/ */
private void loadConfig() { private void loadConfig() {
try { try {
ConfigHelper helper = new ConfigHelper(); XMLReader helper = new XMLReader();
SAXParserFactory factory = SAXParserFactory.newInstance(); SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser parser = factory.newSAXParser(); SAXParser parser = factory.newSAXParser();
parser.parse(APP.getAssets().open("aria_config.xml"), helper); parser.parse(APP.getAssets().open("aria_config.xml"), helper);

@ -1,441 +0,0 @@
/*
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.arialyy.aria.core;
import android.text.TextUtils;
import com.arialyy.aria.util.ALog;
import javax.xml.validation.Validator;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
/**
* Created by lyy on 2017/5/22. 读取配置文件
*/
class ConfigHelper extends DefaultHandler {
private final String TAG = "ConfigHelper";
private Configuration.DownloadConfig mDownloadConfig = Configuration.getInstance().downloadCfg;
private Configuration.UploadConfig mUploadConfig = Configuration.getInstance().uploadCfg;
private Configuration.AppConfig mAppConfig = Configuration.getInstance().appCfg;
private Configuration.DGroupConfig mDGroupConfig = Configuration.getInstance().dGroupCfg;
private @ConfigType int mType;
@Override public void startDocument() throws SAXException {
super.startDocument();
}
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes)
throws SAXException {
super.startElement(uri, localName, qName, attributes);
switch (qName) {
case "download":
mType = ConfigType.DOWNLOAD;
break;
case "upload":
mType = ConfigType.UPLOAD;
break;
case "app":
mType = ConfigType.APP;
break;
case "dGroup":
mType = ConfigType.D_GROUP;
break;
}
if (mType == ConfigType.DOWNLOAD || mType == ConfigType.UPLOAD || mType == ConfigType.D_GROUP) {
String value = attributes.getValue("value");
switch (qName) {
case "threadNum":
loadThreadNum(value);
break;
case "maxTaskNum":
loadMaxQueue(value);
break;
case "reTryNum":
loadReTry(value);
break;
case "connectTimeOut":
loadConnectTime(value);
break;
case "iOTimeOut":
loadIOTimeout(value);
break;
case "reTryInterval":
loadReTryInterval(value);
break;
case "buffSize":
loadBuffSize(value);
break;
case "ca":
String caName = attributes.getValue("name");
String caPath = attributes.getValue("path");
loadCA(caName, caPath);
break;
case "convertSpeed":
loadConvertSpeed(value);
break;
case "maxSpeed":
loadMaxSpeed(value);
break;
case "queueMod":
loadQueueMod(value);
break;
case "updateInterval":
loadUpdateInterval(value);
break;
case "notNetRetry":
loadNotNetRetry(value);
break;
case "useBlock":
loadUseBlock(value);
break;
case "subMaxTaskNum":
loadSubMaxTaskNum(value);
break;
case "subReTryNum":
loadSubReTryNum(value);
break;
case "subReTryInterval":
loadSubReTryInterval(value);
break;
}
} else if (mType == ConfigType.APP) {
String value = attributes.getValue("value");
switch (qName) {
case "useAriaCrashHandler":
loadUseAriaCrashHandler(value);
break;
case "logLevel":
loadLogLevel(value);
break;
case "netCheck":
loadNetCheck(value);
break;
case "useBroadcast":
loadUseBroadcast(value);
break;
}
}
}
private void loadSubReTryInterval(String value) {
int temp = checkInt(value) ? Integer.parseInt(value) : 2000;
if (mType == ConfigType.D_GROUP) {
mDGroupConfig.subReTryInterval = temp;
}
}
private void loadSubReTryNum(String value) {
int temp = checkInt(value) ? Integer.parseInt(value) : 5;
if (mType == ConfigType.D_GROUP) {
mDGroupConfig.subReTryNum = temp;
}
}
private void loadSubMaxTaskNum(String value) {
int temp = checkInt(value) ? Integer.parseInt(value) : 3;
if (mType == ConfigType.D_GROUP) {
mDGroupConfig.subMaxTaskNum = temp;
}
}
private void loadNetCheck(String value) {
boolean b = checkBoolean(value), temp = false;
if (b) {
temp = Boolean.valueOf(value);
}
mAppConfig.netCheck = temp;
}
private void loadUseBroadcast(String value) {
boolean b = checkBoolean(value), temp = false;
if (b) {
temp = Boolean.valueOf(value);
}
if (mType == ConfigType.APP) {
mAppConfig.useBroadcast = temp;
}
}
private void loadUseBlock(String value) {
if (mType == ConfigType.DOWNLOAD) {
mDownloadConfig.useBlock = checkBoolean(value) ? Boolean.valueOf(value) : false;
}
}
private void loadNotNetRetry(String value) {
if (mType == ConfigType.DOWNLOAD) {
mDownloadConfig.notNetRetry = checkBoolean(value) ? Boolean.valueOf(value) : false;
} else if (mType == ConfigType.UPLOAD) {
mUploadConfig.notNetRetry = checkBoolean(value) ? Boolean.valueOf(value) : false;
} else if (mType == ConfigType.D_GROUP) {
mDGroupConfig.notNetRetry = checkBoolean(value) ? Boolean.valueOf(value) : false;
}
}
private void loadLogLevel(String value) {
int level;
try {
level = Integer.parseInt(value);
} catch (NumberFormatException e) {
e.printStackTrace();
level = ALog.LOG_LEVEL_VERBOSE;
}
if (level < ALog.LOG_LEVEL_VERBOSE || level > ALog.LOG_CLOSE) {
ALog.w(TAG, "level【" + level + "】错误");
mAppConfig.logLevel = ALog.LOG_LEVEL_VERBOSE;
} else {
mAppConfig.logLevel = level;
}
}
private void loadUseAriaCrashHandler(String value) {
if (checkBoolean(value)) {
mAppConfig.useAriaCrashHandler = Boolean.parseBoolean(value);
} else {
ALog.w(TAG, "useAriaCrashHandler【" + value + "】错误");
mAppConfig.useAriaCrashHandler = true;
}
}
private void loadUpdateInterval(String value) {
long temp = checkLong(value) ? Long.parseLong(value) : 1000;
if (mType == ConfigType.DOWNLOAD) {
mDownloadConfig.updateInterval = temp;
} else if (mType == ConfigType.UPLOAD) {
mUploadConfig.updateInterval = temp;
} else if (mType == ConfigType.D_GROUP) {
mDGroupConfig.updateInterval = temp;
}
}
private void loadQueueMod(String value) {
String mod = "now";
if (!TextUtils.isEmpty(value) && (value.equalsIgnoreCase("now") || value.equalsIgnoreCase(
"wait"))) {
mod = value;
} else if (mType == ConfigType.DOWNLOAD) {
mDownloadConfig.queueMod = mod;
} else if (mType == ConfigType.UPLOAD) {
mUploadConfig.queueMod = mod;
} else if (mType == ConfigType.D_GROUP) {
mDGroupConfig.queueMod = mod;
}
}
private void loadMaxSpeed(String value) {
int maxSpeed = checkInt(value) ? Integer.parseInt(value) : 0;
if (mType == ConfigType.DOWNLOAD) {
mDownloadConfig.maxSpeed = maxSpeed;
} else if (mType == ConfigType.UPLOAD) {
mUploadConfig.maxSpeed = maxSpeed;
} else if (mType == ConfigType.D_GROUP) {
mDownloadConfig.maxSpeed = maxSpeed;
}
}
private void loadConvertSpeed(String value) {
boolean open = true;
if (checkBoolean(value)) {
open = Boolean.parseBoolean(value);
}
if (mType == ConfigType.DOWNLOAD) {
mDownloadConfig.isConvertSpeed = open;
} else if (mType == ConfigType.UPLOAD) {
mUploadConfig.isConvertSpeed = open;
} else if (mType == ConfigType.D_GROUP) {
mUploadConfig.isConvertSpeed = open;
}
}
private void loadReTryInterval(String value) {
int time = checkInt(value) ? Integer.parseInt(value) : 2 * 1000;
if (time < 2 * 1000) {
time = 2 * 1000;
}
if (mType == ConfigType.DOWNLOAD) {
mDownloadConfig.reTryInterval = time;
} else if (mType == ConfigType.UPLOAD) {
mUploadConfig.reTryInterval = time;
} else if (mType == ConfigType.D_GROUP) {
mDGroupConfig.reTryInterval = time;
}
}
private void loadCA(String name, String path) {
if (mType == ConfigType.DOWNLOAD) {
mDownloadConfig.caName = name;
mDownloadConfig.caPath = path;
} else if (mType == ConfigType.UPLOAD) {
mUploadConfig.caName = name;
mUploadConfig.caPath = path;
} else if (mType == ConfigType.D_GROUP) {
mDGroupConfig.caName = name;
mDGroupConfig.caPath = path;
}
}
private void loadBuffSize(String value) {
int buffSize = checkInt(value) ? Integer.parseInt(value) : 8192;
if (buffSize < 2048) {
buffSize = 2048;
}
if (mType == ConfigType.DOWNLOAD) {
mDownloadConfig.buffSize = buffSize;
}
if (mType == ConfigType.UPLOAD) {
mUploadConfig.buffSize = buffSize;
}
}
private void loadIOTimeout(String value) {
int time = checkInt(value) ? Integer.parseInt(value) : 10 * 1000;
if (time < 10 * 1000) {
time = 10 * 1000;
}
if (mType == ConfigType.DOWNLOAD) {
mDownloadConfig.iOTimeOut = time;
} else if (mType == ConfigType.UPLOAD) {
mUploadConfig.iOTimeOut = time;
} else if (mType == ConfigType.D_GROUP) {
mDGroupConfig.iOTimeOut = time;
}
}
private void loadConnectTime(String value) {
int time = checkInt(value) ? Integer.parseInt(value) : 5 * 1000;
if (mType == ConfigType.DOWNLOAD) {
mDownloadConfig.connectTimeOut = time;
} else if (mType == ConfigType.UPLOAD) {
mUploadConfig.connectTimeOut = time;
} else if (mType == ConfigType.D_GROUP) {
mDGroupConfig.connectTimeOut = time;
}
}
private void loadReTry(String value) {
int num = checkInt(value) ? Integer.parseInt(value) : 0;
if (mType == ConfigType.DOWNLOAD) {
mDownloadConfig.reTryNum = num;
} else if (mType == ConfigType.UPLOAD) {
mUploadConfig.reTryNum = num;
} else if (mType == ConfigType.D_GROUP) {
mDGroupConfig.reTryNum = num;
}
}
private void loadMaxQueue(String value) {
int num = checkInt(value) ? Integer.parseInt(value) : 2;
if (num < 1) {
ALog.w(TAG, "任务队列数不能小于 1");
num = 2;
}
if (mType == ConfigType.DOWNLOAD) {
mDownloadConfig.maxTaskNum = num;
} else if (mType == ConfigType.UPLOAD) {
mUploadConfig.maxTaskNum = num;
} else if (mType == ConfigType.D_GROUP) {
mDGroupConfig.maxTaskNum = num;
}
}
private void loadThreadNum(String value) {
int num = checkInt(value) ? Integer.parseInt(value) : 3;
if (num < 1) {
ALog.e(TAG, "下载线程数不能小于 1");
num = 1;
}
if (mType == ConfigType.DOWNLOAD) {
mDownloadConfig.threadNum = num;
}
}
/**
* 检查是否int值是否合法
*
* @return {@code true} 合法
*/
private boolean checkInt(String value) {
if (TextUtils.isEmpty(value)) {
return false;
}
try {
Integer l = Integer.parseInt(value);
return true;
} catch (NumberFormatException e) {
e.printStackTrace();
return false;
}
}
/**
* 检查是否long值是否合法
*
* @return {@code true} 合法
*/
private boolean checkLong(String value) {
if (TextUtils.isEmpty(value)) {
return false;
}
try {
Long l = Long.parseLong(value);
return true;
} catch (NumberFormatException e) {
e.printStackTrace();
return false;
}
}
/**
* 检查boolean值是否合法
*
* @return {@code true} 合法
*/
private boolean checkBoolean(String value) {
return !TextUtils.isEmpty(value) && (value.equalsIgnoreCase("true") || value.equalsIgnoreCase(
"false"));
}
@Override public void characters(char[] ch, int start, int length) throws SAXException {
super.characters(ch, start, length);
}
@Override public void endElement(String uri, String localName, String qName) throws SAXException {
super.endElement(uri, localName, qName);
}
@Override public void endDocument() throws SAXException {
super.endDocument();
mDownloadConfig.save();
mUploadConfig.save();
mAppConfig.save();
mDGroupConfig.save();
}
}

@ -552,7 +552,7 @@ public abstract class AbsFileer<ENTITY extends AbsNormalEntity, TASK_WRAPPER ext
config.START_LOCATION = startL; config.START_LOCATION = startL;
config.END_LOCATION = endL; config.END_LOCATION = endL;
config.SUPPORT_BP = mTaskWrapper.isSupportBP(); config.SUPPORT_BP = mTaskWrapper.isSupportBP();
config.TASK_ENTITY = mTaskWrapper; config.TASK_WRAPPER = mTaskWrapper;
config.THREAD_RECORD = record; config.THREAD_RECORD = record;
return selectThreadTask(config); return selectThreadTask(config);
} }
@ -672,7 +672,7 @@ public abstract class AbsFileer<ENTITY extends AbsNormalEntity, TASK_WRAPPER ext
config.START_LOCATION = 0; config.START_LOCATION = 0;
config.END_LOCATION = config.TOTAL_FILE_SIZE; config.END_LOCATION = config.TOTAL_FILE_SIZE;
config.SUPPORT_BP = mTaskWrapper.isSupportBP(); config.SUPPORT_BP = mTaskWrapper.isSupportBP();
config.TASK_ENTITY = mTaskWrapper; config.TASK_WRAPPER = mTaskWrapper;
ThreadRecord record = DbEntity.findFirst(ThreadRecord.class, "key=?", mRecord.filePath); ThreadRecord record = DbEntity.findFirst(ThreadRecord.class, "key=?", mRecord.filePath);
if (record != null) { if (record != null) {
record.deleteData(); record.deleteData();

@ -17,6 +17,10 @@ package com.arialyy.aria.core.common;
import android.os.Process; import android.os.Process;
import com.arialyy.aria.core.AriaManager; import com.arialyy.aria.core.AriaManager;
import com.arialyy.aria.core.config.BaseTaskConfig;
import com.arialyy.aria.core.config.DGroupConfig;
import com.arialyy.aria.core.config.DownloadConfig;
import com.arialyy.aria.core.config.UploadConfig;
import com.arialyy.aria.core.inf.AbsNormalEntity; import com.arialyy.aria.core.inf.AbsNormalEntity;
import com.arialyy.aria.core.inf.AbsTaskWrapper; import com.arialyy.aria.core.inf.AbsTaskWrapper;
import com.arialyy.aria.core.inf.IEventListener; import com.arialyy.aria.core.inf.IEventListener;
@ -37,8 +41,7 @@ import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
/** /**
* Created by lyy on 2017/1/18. * Created by lyy on 2017/1/18. 任务线程
* 任务线程
*/ */
public abstract class AbsThreadTask<ENTITY extends AbsNormalEntity, TASK_WRAPPER extends AbsTaskWrapper<ENTITY>> public abstract class AbsThreadTask<ENTITY extends AbsNormalEntity, TASK_WRAPPER extends AbsTaskWrapper<ENTITY>>
implements Callable<AbsThreadTask> { implements Callable<AbsThreadTask> {
@ -52,7 +55,6 @@ public abstract class AbsThreadTask<ENTITY extends AbsNormalEntity, TASK_WRAPPER
* 当前子线程相对于总长度的位置 * 当前子线程相对于总长度的位置
*/ */
protected long mChildCurrentLocation = 0; protected long mChildCurrentLocation = 0;
protected int mBufSize;
protected IEventListener mListener; protected IEventListener mListener;
protected StateConstance STATE; protected StateConstance STATE;
protected SubThreadConfig<TASK_WRAPPER> mConfig; protected SubThreadConfig<TASK_WRAPPER> mConfig;
@ -61,9 +63,7 @@ public abstract class AbsThreadTask<ENTITY extends AbsNormalEntity, TASK_WRAPPER
private int mFailTimes = 0; private int mFailTimes = 0;
private long mLastSaveTime; private long mLastSaveTime;
private ExecutorService mConfigThreadPool; private ExecutorService mConfigThreadPool;
protected int mConnectTimeOut; //连接超时时间 protected boolean isNotNetRetry; //断网情况是否重试
protected int mReadTimeOut; //流读取的超时时间
protected boolean isNotNetRetry = false; //断网情况是否重试
private boolean taskBreak = false; //任务跳出 private boolean taskBreak = false; //任务跳出
protected int mThreadNum; protected int mThreadNum;
/** /**
@ -86,7 +86,7 @@ public abstract class AbsThreadTask<ENTITY extends AbsNormalEntity, TASK_WRAPPER
STATE = constance; STATE = constance;
mListener = listener; mListener = listener;
mConfig = config; mConfig = config;
mTaskWrapper = mConfig.TASK_ENTITY; mTaskWrapper = mConfig.TASK_WRAPPER;
mEntity = mTaskWrapper.getEntity(); mEntity = mTaskWrapper.getEntity();
mLastSaveTime = System.currentTimeMillis(); mLastSaveTime = System.currentTimeMillis();
mConfigThreadPool = Executors.newCachedThreadPool(); mConfigThreadPool = Executors.newCachedThreadPool();
@ -95,6 +95,7 @@ public abstract class AbsThreadTask<ENTITY extends AbsNormalEntity, TASK_WRAPPER
if (getMaxSpeed() > 0) { if (getMaxSpeed() > 0) {
mSpeedBandUtil = new BandwidthLimiter(getMaxSpeed(), mThreadNum); mSpeedBandUtil = new BandwidthLimiter(getMaxSpeed(), mThreadNum);
} }
isNotNetRetry = mAridManager.getAppConfig().isNotNetRetry();
} }
/** /**
@ -117,24 +118,23 @@ public abstract class AbsThreadTask<ENTITY extends AbsNormalEntity, TASK_WRAPPER
} }
/** /**
* 当前线程是否完成对于不支持断点的任务一律未完成 * 当前线程是否完成对于不支持断点的任务一律未完成 {@code true} 完成{@code false} 未完成
* {@code true} 完成{@code false} 未完成
*/ */
public boolean isThreadComplete() { boolean isThreadComplete() {
return mConfig.THREAD_RECORD.isComplete; return mConfig.THREAD_RECORD.isComplete;
} }
/** /**
* 获取任务记录 * 获取任务记录
*/ */
public TaskRecord getTaskRecord() { private TaskRecord getTaskRecord() {
return STATE.TASK_RECORD; return STATE.TASK_RECORD;
} }
/** /**
* 获取线程记录 * 获取线程记录
*/ */
public ThreadRecord getThreadRecord() { protected ThreadRecord getThreadRecord() {
return mConfig.THREAD_RECORD; return mConfig.THREAD_RECORD;
} }
@ -145,6 +145,13 @@ public abstract class AbsThreadTask<ENTITY extends AbsNormalEntity, TASK_WRAPPER
*/ */
public abstract int getMaxSpeed(); public abstract int getMaxSpeed();
/**
* 读取任务配置
*
* @return {@link DownloadConfig}{@link UploadConfig}{@link DGroupConfig}
*/
protected abstract BaseTaskConfig getTaskConfig();
/** /**
* 设置最大下载速度 * 设置最大下载速度
* *
@ -159,7 +166,7 @@ public abstract class AbsThreadTask<ENTITY extends AbsNormalEntity, TASK_WRAPPER
/** /**
* 中断任务 * 中断任务
*/ */
public void breakTask() { void breakTask() {
synchronized (AriaManager.LOCK) { synchronized (AriaManager.LOCK) {
taskBreak = true; taskBreak = true;
if (mConfig.SUPPORT_BP) { if (mConfig.SUPPORT_BP) {
@ -207,10 +214,7 @@ public abstract class AbsThreadTask<ENTITY extends AbsNormalEntity, TASK_WRAPPER
} }
/** /**
* 任务是否中断中断条件 * 任务是否中断中断条件 1任务取消 2任务停止 3手动中断 {@link #taskBreak}
* 1任务取消
* 2任务停止
* 3手动中断 {@link #taskBreak}
* *
* @return {@code true} 中断{@code false} 不是中断 * @return {@code true} 中断{@code false} 不是中断
*/ */
@ -249,8 +253,7 @@ public abstract class AbsThreadTask<ENTITY extends AbsNormalEntity, TASK_WRAPPER
} }
/** /**
* 检查下载完成的分块大小如果下载完成的分块大小大于或小于分配的大小则需要重新下载该分块 * 检查下载完成的分块大小如果下载完成的分块大小大于或小于分配的大小则需要重新下载该分块 如果是非分块任务直接返回{@code true}
* 如果是非分块任务直接返回{@code true}
* *
* @return {@code true} 分块分大小正常{@code false} 分块大小错误 * @return {@code true} 分块分大小正常{@code false} 分块大小错误
*/ */

@ -16,7 +16,6 @@
package com.arialyy.aria.core.common; package com.arialyy.aria.core.common;
import android.os.Handler; import android.os.Handler;
import com.arialyy.aria.core.AriaManager;
import com.arialyy.aria.core.inf.AbsEntity; import com.arialyy.aria.core.inf.AbsEntity;
import com.arialyy.aria.core.inf.AbsTask; import com.arialyy.aria.core.inf.AbsTask;
import com.arialyy.aria.core.inf.AbsTaskWrapper; import com.arialyy.aria.core.inf.AbsTaskWrapper;
@ -30,27 +29,28 @@ import com.arialyy.aria.util.CommonUtil;
import com.arialyy.aria.util.ErrorHelp; import com.arialyy.aria.util.ErrorHelp;
import java.lang.ref.WeakReference; import java.lang.ref.WeakReference;
public abstract class BaseListener<ENTITY extends AbsEntity, TASK_ENTITY extends AbsTaskWrapper<ENTITY>, TASK extends AbsTask<ENTITY, TASK_ENTITY>> public abstract class BaseListener<ENTITY extends AbsEntity, TASK_WRAPPER extends AbsTaskWrapper<ENTITY>,
TASK extends AbsTask<ENTITY, TASK_WRAPPER>>
implements IEventListener { implements IEventListener {
private static final String TAG = "BaseListener"; private static final String TAG = "BaseListener";
private static final int RUN_SAVE_INTERVAL = 5 * 1000; //5s保存一次下载中的进度
protected WeakReference<Handler> outHandler; protected WeakReference<Handler> outHandler;
private int RUN_SAVE_INTERVAL = 5 * 1000; //5s保存一次下载中的进度
private long mLastLen; //上一次发送长度 private long mLastLen; //上一次发送长度
private boolean isFirst = true; private boolean isFirst = true;
private TASK mTask; private TASK mTask;
private long mLastSaveTime; private long mLastSaveTime;
protected ENTITY mEntity; protected ENTITY mEntity;
protected TASK_ENTITY mTaskEntity; protected TASK_WRAPPER mTaskWrapper;
protected boolean isConvertSpeed; private boolean isConvertSpeed;
protected long mUpdateInterval; private long mUpdateInterval;
protected AriaManager manager;
protected BaseListener(TASK task, Handler outHandler) { protected BaseListener(TASK task, Handler outHandler) {
this.outHandler = new WeakReference<>(outHandler); this.outHandler = new WeakReference<>(outHandler);
this.mTask = new WeakReference<>(task).get(); mTask = new WeakReference<>(task).get();
this.mEntity = mTask.getTaskWrapper().getEntity(); mEntity = mTask.getTaskWrapper().getEntity();
this.mTaskEntity = mTask.getTaskWrapper(); mTaskWrapper = mTask.getTaskWrapper();
manager = AriaManager.getInstance(AriaManager.APP); isConvertSpeed = mTaskWrapper.getConfig().isConvertSpeed();
mUpdateInterval = mTaskWrapper.getConfig().getUpdateInterval();
mLastLen = mEntity.getCurrentProgress(); mLastLen = mEntity.getCurrentProgress();
mLastSaveTime = System.currentTimeMillis(); mLastSaveTime = System.currentTimeMillis();
} }

@ -15,7 +15,7 @@
*/ */
package com.arialyy.aria.core.common; package com.arialyy.aria.core.common;
import com.arialyy.aria.orm.AbsWrapper; import com.arialyy.aria.orm.AbsDbWrapper;
import com.arialyy.aria.orm.annotation.Many; import com.arialyy.aria.orm.annotation.Many;
import com.arialyy.aria.orm.annotation.One; import com.arialyy.aria.orm.annotation.One;
import com.arialyy.aria.orm.annotation.Wrapper; import com.arialyy.aria.orm.annotation.Wrapper;
@ -27,7 +27,7 @@ import java.util.List;
* 任务记录和线程记录的关系 * 任务记录和线程记录的关系
*/ */
@Wrapper @Wrapper
public class RecordWrapper extends AbsWrapper { public class RecordWrapper extends AbsDbWrapper {
@One @One
public TaskRecord taskRecord; public TaskRecord taskRecord;

@ -6,7 +6,7 @@ import java.io.File;
/** /**
* 子线程下载信息类 * 子线程下载信息类
*/ */
public class SubThreadConfig<TASK_ENTITY extends AbsTaskWrapper> { public class SubThreadConfig<TASK_WRAPPER extends AbsTaskWrapper> {
//线程Id //线程Id
public int THREAD_ID; public int THREAD_ID;
//文件总长度 //文件总长度
@ -19,7 +19,7 @@ public class SubThreadConfig<TASK_ENTITY extends AbsTaskWrapper> {
public File TEMP_FILE; public File TEMP_FILE;
//服务器地址 //服务器地址
public String URL; public String URL;
public TASK_ENTITY TASK_ENTITY; public TASK_WRAPPER TASK_WRAPPER;
public boolean SUPPORT_BP = true; public boolean SUPPORT_BP = true;
public ThreadRecord THREAD_RECORD; public ThreadRecord THREAD_RECORD;
} }

@ -36,8 +36,7 @@ import java.net.UnknownHostException;
import javax.net.ssl.SSLContext; import javax.net.ssl.SSLContext;
/** /**
* Created by lyy on 2017/9/26. * Created by lyy on 2017/9/26. FTP单任务父类
* FTP单任务父类
*/ */
public abstract class AbsFtpThreadTask<ENTITY extends AbsNormalEntity, TASK_ENTITY extends AbsTaskWrapper<ENTITY>> public abstract class AbsFtpThreadTask<ENTITY extends AbsNormalEntity, TASK_ENTITY extends AbsTaskWrapper<ENTITY>>
extends AbsThreadTask<ENTITY, TASK_ENTITY> { extends AbsThreadTask<ENTITY, TASK_ENTITY> {
@ -112,8 +111,8 @@ public abstract class AbsFtpThreadTask<ENTITY extends AbsNormalEntity, TASK_ENTI
} }
} }
client.setControlEncoding(charSet); client.setControlEncoding(charSet);
client.setDataTimeout(mReadTimeOut); client.setDataTimeout(getTaskConfig().getIOTimeOut());
client.setConnectTimeout(mConnectTimeOut); client.setConnectTimeout(getTaskConfig().getConnectTimeOut());
client.enterLocalPassiveMode(); client.enterLocalPassiveMode();
client.setFileType(FTP.BINARY_FILE_TYPE); client.setFileType(FTP.BINARY_FILE_TYPE);
client.setControlKeepAliveTimeout(5000); client.setControlKeepAliveTimeout(5000);

@ -0,0 +1,112 @@
/*
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.arialyy.aria.core.config;
import com.arialyy.aria.util.ALog;
import com.arialyy.aria.util.AriaCrashHandler;
import java.io.Serializable;
/**
* 应用配置
*/
public class AppConfig extends BaseConfig implements Serializable {
/**
* 是否使用{@link AriaCrashHandler}来捕获异常 {@code true} 使用{@code false} 不使用
*/
boolean useAriaCrashHandler;
/**
* 设置Aria的日志级别
*
* {@link ALog#LOG_LEVEL_VERBOSE}
*/
int logLevel;
/**
* 是否检查网络{@code true}检查网络
*/
boolean netCheck = true;
/**
* 是否使用广播 除非无法使用注解否则不建议使用广播来接受任务 {@code true} 使用广播{@code false} 不适用广播
*/
boolean useBroadcast = false;
/**
* 断网的时候是否重试{@code true}断网也重试{@code false}断网不重试直接走失败的回调
*/
boolean notNetRetry = false;
public boolean isNotNetRetry() {
return notNetRetry;
}
public AppConfig setNotNetRetry(boolean notNetRetry) {
this.notNetRetry = notNetRetry;
save();
return this;
}
public boolean isUseBroadcast() {
return useBroadcast;
}
public AppConfig setUseBroadcast(boolean useBroadcast) {
this.useBroadcast = useBroadcast;
save();
return this;
}
public boolean isNetCheck() {
return netCheck;
}
public AppConfig setNetCheck(boolean netCheck) {
this.netCheck = netCheck;
save();
return this;
}
public AppConfig setLogLevel(int level) {
this.logLevel = level;
ALog.LOG_LEVEL = level;
save();
return this;
}
public int getLogLevel() {
return logLevel;
}
public boolean getUseAriaCrashHandler() {
return useAriaCrashHandler;
}
public AppConfig setUseAriaCrashHandler(boolean useAriaCrashHandler) {
this.useAriaCrashHandler = useAriaCrashHandler;
if (useAriaCrashHandler) {
Thread.setDefaultUncaughtExceptionHandler(new AriaCrashHandler());
} else {
Thread.setDefaultUncaughtExceptionHandler(null);
}
save();
return this;
}
@Override int getType() {
return TYPE_APP;
}
}

@ -0,0 +1,51 @@
package com.arialyy.aria.core.config;
import android.text.TextUtils;
import com.arialyy.aria.core.AriaManager;
import com.arialyy.aria.util.ALog;
import com.arialyy.aria.util.CommonUtil;
import java.io.Serializable;
abstract class BaseConfig implements Serializable {
private static final String TAG = "BaseConfig";
static final int TYPE_DOWNLOAD = 1;
static final int TYPE_UPLOAD = 2;
static final int TYPE_APP = 3;
static final int TYPE_DGROUP = 4;
/**
* 类型
*
* @return {@link #TYPE_DOWNLOAD}{@link #TYPE_UPLOAD}{@link #TYPE_APP}{@link #TYPE_DGROUP}
*/
abstract int getType();
/**
* 保存配置
*/
void save() {
String basePath = AriaManager.APP.getFilesDir().getPath();
String path = null;
switch (getType()) {
case TYPE_DOWNLOAD:
path = Configuration.DOWNLOAD_CONFIG_FILE;
break;
case TYPE_UPLOAD:
path = Configuration.UPLOAD_CONFIG_FILE;
break;
case TYPE_APP:
path = Configuration.APP_CONFIG_FILE;
break;
case TYPE_DGROUP:
path = Configuration.DGROUP_CONFIG_FILE;
break;
}
if (!TextUtils.isEmpty(path)) {
String tempPath = String.format("%s%s", basePath, path);
CommonUtil.deleteFile(tempPath);
CommonUtil.writeObjToFile(tempPath, this);
} else {
ALog.e(TAG, String.format("保存配置失败,配置类型:%s,原因:路径错误", getType()));
}
}
}

@ -0,0 +1,221 @@
/*
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.arialyy.aria.core.config;
import com.arialyy.aria.core.common.QueueMod;
import com.arialyy.aria.util.ALog;
import java.io.Serializable;
/**
* 通用任务配置
*/
public abstract class BaseTaskConfig extends BaseConfig implements Serializable {
/**
* 设置写文件buff大小该数值大小不能小于2048数值变小下载速度会变慢
*/
int buffSize = 8192;
/**
* 进度刷新间隔默认1秒
*/
long updateInterval = 1000;
/**
* 旧任务数
*/
public int oldMaxTaskNum = 2;
/**
* 任务队列最大任务数 默认为2
*/
int maxTaskNum = 2;
/**
* 下载失败重试次数默认为10
*/
int reTryNum = 10;
/**
* 设置重试间隔单位为毫秒默认2000毫秒
*/
int reTryInterval = 2000;
/**
* 设置url连接超时时间单位为毫秒默认5000毫秒
*/
int connectTimeOut = 5000;
/**
* 是否需要转换速度单位转换完成后为1b/s1k/s1m/s1g/s1t/s如果不需要将返回byte长度
*/
boolean isConvertSpeed = false;
/**
* 执行队列类型
*
* @see QueueMod
*/
String queueMod = "wait";
/**
* 设置IO流读取时间单位为毫秒默认20000毫秒该时间不能少于10000毫秒
*/
int iOTimeOut = 20 * 1000;
/**
* 设置最大下载/上传速度单位kb, 为0表示不限速
*/
int maxSpeed = 0;
/**
* 设置https ca 证书信息path 为assets目录下的CA证书完整路径
*/
String caPath;
/**
* name 为CA证书名
*/
String caName;
public String getCaPath() {
return caPath;
}
public BaseConfig setCaPath(String caPath) {
this.caPath = caPath;
save();
return this;
}
public String getCaName() {
return caName;
}
public BaseConfig setCaName(String caName) {
this.caName = caName;
save();
return this;
}
public BaseTaskConfig setMaxTaskNum(int maxTaskNum) {
oldMaxTaskNum = this.maxTaskNum;
this.maxTaskNum = maxTaskNum;
save();
return this;
}
public int getMaxSpeed() {
return maxSpeed;
}
public BaseTaskConfig setMaxSpeed(int maxSpeed) {
this.maxSpeed = maxSpeed;
save();
return this;
}
public long getUpdateInterval() {
return updateInterval;
}
/**
* 设置进度更新间隔该设置对正在运行的任务无效默认为1000毫秒
*
* @param updateInterval 不能小于0
*/
public BaseTaskConfig setUpdateInterval(long updateInterval) {
if (updateInterval <= 0) {
ALog.w("Configuration", "进度更新间隔不能小于0");
return this;
}
this.updateInterval = updateInterval;
save();
return this;
}
public String getQueueMod() {
return queueMod;
}
public BaseTaskConfig setQueueMod(String queueMod) {
this.queueMod = queueMod;
save();
return this;
}
public int getMaxTaskNum() {
return maxTaskNum;
}
public int getReTryNum() {
return reTryNum;
}
public BaseTaskConfig setReTryNum(int reTryNum) {
this.reTryNum = reTryNum;
save();
return this;
}
public int getReTryInterval() {
return reTryInterval;
}
public BaseTaskConfig setReTryInterval(int reTryInterval) {
this.reTryInterval = reTryInterval;
save();
return this;
}
public boolean isConvertSpeed() {
return isConvertSpeed;
}
public BaseTaskConfig setConvertSpeed(boolean convertSpeed) {
isConvertSpeed = convertSpeed;
save();
return this;
}
public int getConnectTimeOut() {
return connectTimeOut;
}
public BaseTaskConfig setConnectTimeOut(int connectTimeOut) {
this.connectTimeOut = connectTimeOut;
save();
return this;
}
public int getIOTimeOut() {
return iOTimeOut;
}
public BaseTaskConfig setIOTimeOut(int iOTimeOut) {
this.iOTimeOut = iOTimeOut;
save();
return this;
}
public int getBuffSize() {
return buffSize;
}
public BaseTaskConfig setBuffSize(int buffSize) {
this.buffSize = buffSize;
save();
return this;
}
}

@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package com.arialyy.aria.core; package com.arialyy.aria.core.config;
import android.support.annotation.IntDef; import android.support.annotation.IntDef;
import java.lang.annotation.Retention; import java.lang.annotation.Retention;

@ -0,0 +1,117 @@
/*
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.arialyy.aria.core.config;
import com.arialyy.aria.core.AriaManager;
import com.arialyy.aria.util.CommonUtil;
import java.io.File;
/**
* Created by lyy on 2016/12/8. 信息配置 kotlin 方式有bug不能将public去掉
*/
public final class Configuration {
private static final String TAG = "Configuration";
private static volatile Configuration INSTANCE = null;
public static final String XML_FILE = "/Aria/aria_config.xml";
static final String DOWNLOAD_CONFIG_FILE = "/Aria/AriaDownload.cfg";
static final String UPLOAD_CONFIG_FILE = "/Aria/AriaUpload.cfg";
static final String APP_CONFIG_FILE = "/Aria/AriaApp.cfg";
static final String DGROUP_CONFIG_FILE = "/Aria/AriaDGroup.cfg";
public DownloadConfig downloadCfg;
public UploadConfig uploadCfg;
public AppConfig appCfg;
public DGroupConfig dGroupCfg;
private Configuration() {
//删除老版本的配置文件
String basePath = AriaManager.APP.getFilesDir().getPath();
del351Config(basePath);
File newDCfg = new File(String.format("%s%s", basePath, DOWNLOAD_CONFIG_FILE));
File newUCfg = new File(String.format("%s%s", basePath, UPLOAD_CONFIG_FILE));
File newACfg = new File(String.format("%s%s", basePath, APP_CONFIG_FILE));
File dgCfg = new File(String.format("%s%s", basePath, DGROUP_CONFIG_FILE));
// 加载下载配置
if (newDCfg.exists()) {
downloadCfg = (DownloadConfig) CommonUtil.readObjFromFile(newDCfg.getPath());
}
if (downloadCfg == null) {
downloadCfg = new DownloadConfig();
}
// 加载上传配置
if (newUCfg.exists()) {
uploadCfg = (UploadConfig) CommonUtil.readObjFromFile(newUCfg.getPath());
}
if (uploadCfg == null) {
uploadCfg = new UploadConfig();
}
// 加载app配置
if (newACfg.exists()) {
appCfg = (AppConfig) CommonUtil.readObjFromFile(newACfg.getPath());
}
if (appCfg == null) {
appCfg = new AppConfig();
}
// 加载下载类型组合任务的配置
if (dgCfg.exists()) {
dGroupCfg = (DGroupConfig) CommonUtil.readObjFromFile(dgCfg.getPath());
}
if (dGroupCfg == null) {
dGroupCfg = new DGroupConfig();
}
}
public static Configuration getInstance() {
if (INSTANCE == null) {
synchronized (AppConfig.class) {
INSTANCE = new Configuration();
}
}
return INSTANCE;
}
/**
* 检查配置文件是否存在只要{@link DownloadConfig}{@link UploadConfig}{@link AppConfig}{@link
* DGroupConfig}其中一个不存在 则任务配置文件不存在
*
* @return {@code true}配置存在{@code false}配置不存在
*/
public boolean configExists() {
String basePath = AriaManager.APP.getFilesDir().getPath();
return (new File(String.format("%s%s", basePath, DOWNLOAD_CONFIG_FILE))).exists()
&& (new File(String.format("%s%s", basePath, UPLOAD_CONFIG_FILE))).exists()
&& (new File(String.format("%s%s", basePath, APP_CONFIG_FILE))).exists()
&& (new File(String.format("%s%s", basePath, DGROUP_CONFIG_FILE))).exists();
}
/**
* 删除3.5.2之前版本的配置文件从3.5.2开始配置文件的保存不再使用properties文件
*/
private void del351Config(String basePath) {
File oldDCfg = new File(String.format("%s/Aria/DownloadConfig.properties", basePath));
if (oldDCfg.exists()) { // 只需要判断一个
File oldUCfg = new File(String.format("%s/Aria/UploadConfig.properties", basePath));
File oldACfg = new File(String.format("%s/Aria/AppConfig.properties", basePath));
oldDCfg.delete();
oldUCfg.delete();
oldACfg.delete();
// 删除配置触发更新
File temp = new File(String.format("%s%s", basePath, XML_FILE));
if (temp.exists()) {
temp.delete();
}
}
}
}

@ -0,0 +1,114 @@
/*
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.arialyy.aria.core.config;
import com.arialyy.aria.core.queue.DownloadGroupTaskQueue;
import java.io.Serializable;
/**
* 下载类型的组合任务
*/
public class DGroupConfig extends BaseTaskConfig implements Serializable {
/**
* 能同时下载的子任务最大任务数默认3
*/
int subMaxTaskNum = 3;
/**
* 子任务重试次数默认为5
*/
int subReTryNum = 5;
/**
* 子任务下载失败时的重试间隔单位为毫秒默认2000毫秒-
*/
int subReTryInterval = 2000;
private DownloadConfig subConfig;
DGroupConfig() {
getSubConfig();
}
@Override int getType() {
return TYPE_DGROUP;
}
@Override public DGroupConfig setMaxSpeed(int maxSpeed) {
super.setMaxSpeed(maxSpeed);
DownloadGroupTaskQueue.getInstance().setMaxSpeed(maxSpeed);
return this;
}
public DownloadConfig getSubConfig() {
if (subConfig == null) {
subConfig = new DownloadConfig();
subConfig.threadNum = 1;
subConfig.useBlock = false;
subConfig.buffSize = buffSize;
subConfig.caName = caName;
subConfig.caPath = caPath;
subConfig.connectTimeOut = connectTimeOut;
subConfig.iOTimeOut = iOTimeOut;
subConfig.isConvertSpeed = isConvertSpeed;
subConfig.maxSpeed = maxSpeed;
subConfig.queueMod = "now";
subConfig.reTryInterval = subReTryInterval;
subConfig.reTryNum = subReTryNum;
subConfig.updateInterval = updateInterval;
}
return subConfig;
}
public DGroupConfig setMaxTaskNum(int maxTaskNum) {
super.setMaxTaskNum(maxTaskNum);
DownloadGroupTaskQueue.getInstance().setMaxTaskNum(maxTaskNum);
return this;
}
public int getSubMaxTaskNum() {
return subMaxTaskNum;
}
public DGroupConfig setSubMaxTaskNum(int subMaxTaskNum) {
this.subMaxTaskNum = subMaxTaskNum;
save();
return this;
}
public int getSubReTryNum() {
return subReTryNum;
}
public DGroupConfig setSubReTryNum(int subReTryNum) {
this.subReTryNum = subReTryNum;
subConfig.reTryNum = subReTryNum;
save();
return this;
}
public int getSubReTryInterval() {
return subReTryInterval;
}
public DGroupConfig setSubReTryInterval(int subReTryInterval) {
this.subReTryInterval = subReTryInterval;
subConfig.reTryInterval = subReTryInterval;
save();
return this;
}
}

@ -0,0 +1,75 @@
/*
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.arialyy.aria.core.config;
import com.arialyy.aria.core.queue.DownloadTaskQueue;
import java.io.Serializable;
/**
* 下载配置
*/
public class DownloadConfig extends BaseTaskConfig implements Serializable {
/**
* 下载线程数下载线程数不能小于1 注意 1线程下载数改变后新的下载任务才会生效 2如果任务大小小于1m该设置不会生效
* 3从3.4.1开始如果线程数为1文件初始化时将不再预占用对应长度的空间下载多少byte则占多大的空间 对于采用多线程的任务或旧任务依然采用原来的文件空间占用方式
*/
int threadNum = 3;
/**
* 多线程下载是否使用块下载模式{@code true}使用{@code false}不使用 注意 1使用分块模式在读写性能底下的手机上合并文件需要的时间会更加长
* 2优点是使用多线程的块下载初始化时文件初始化时将不会预占用对应长度的空间 3只对新的多线程下载任务有效 4只对多线程的任务有效
*/
boolean useBlock = false;
public boolean isUseBlock() {
return useBlock;
}
@Override public DownloadConfig setMaxSpeed(int maxSpeed) {
super.setMaxSpeed(maxSpeed);
DownloadTaskQueue.getInstance().setMaxSpeed(maxSpeed);
return this;
}
public DownloadConfig setUseBlock(boolean useBlock) {
this.useBlock = useBlock;
save();
return this;
}
public DownloadConfig setMaxTaskNum(int maxTaskNum) {
super.setMaxTaskNum(maxTaskNum);
DownloadTaskQueue.getInstance().setMaxTaskNum(maxTaskNum);
return this;
}
public DownloadConfig setThreadNum(int threadNum) {
this.threadNum = threadNum;
save();
return this;
}
public int getThreadNum() {
return threadNum;
}
DownloadConfig() {
}
@Override int getType() {
return TYPE_DOWNLOAD;
}
}

@ -0,0 +1,5 @@
package com.arialyy.aria.core.config;
public class TTaskConfigAdapeter {
}

@ -0,0 +1,44 @@
/*
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.arialyy.aria.core.config;
import com.arialyy.aria.core.queue.UploadTaskQueue;
import java.io.Serializable;
/**
* 上传配置
*/
public class UploadConfig extends BaseTaskConfig implements Serializable {
UploadConfig() {
}
@Override public UploadConfig setMaxSpeed(int maxSpeed) {
super.setMaxSpeed(maxSpeed);
UploadTaskQueue.getInstance().setMaxSpeed(maxSpeed);
return this;
}
public UploadConfig setMaxTaskNum(int maxTaskNum) {
super.setMaxTaskNum(maxTaskNum);
UploadTaskQueue.getInstance().setMaxTaskNum(maxTaskNum);
return this;
}
@Override int getType() {
return TYPE_UPLOAD;
}
}

@ -13,13 +13,12 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package com.arialyy.aria.core; package com.arialyy.aria.core.config;
import android.text.TextUtils; import android.text.TextUtils;
import com.arialyy.aria.util.ALog; import com.arialyy.aria.util.ALog;
import com.arialyy.aria.util.CommonUtil; import com.arialyy.aria.util.CommonUtil;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import javax.xml.validation.Validator;
import org.xml.sax.Attributes; import org.xml.sax.Attributes;
import org.xml.sax.SAXException; import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler; import org.xml.sax.helpers.DefaultHandler;
@ -27,13 +26,13 @@ import org.xml.sax.helpers.DefaultHandler;
/** /**
* Created by lyy on 2017/5/22. 读取配置文件 * Created by lyy on 2017/5/22. 读取配置文件
*/ */
class ConfigHelper extends DefaultHandler { public class XMLReader extends DefaultHandler {
private final String TAG = "ConfigHelper"; private final String TAG = "XMLReader";
private Configuration.DownloadConfig mDownloadConfig = Configuration.getInstance().downloadCfg; private DownloadConfig mDownloadConfig = Configuration.getInstance().downloadCfg;
private Configuration.UploadConfig mUploadConfig = Configuration.getInstance().uploadCfg; private UploadConfig mUploadConfig = Configuration.getInstance().uploadCfg;
private Configuration.AppConfig mAppConfig = Configuration.getInstance().appCfg; private AppConfig mAppConfig = Configuration.getInstance().appCfg;
private Configuration.DGroupConfig mDGroupConfig = Configuration.getInstance().dGroupCfg; private DGroupConfig mDGroupConfig = Configuration.getInstance().dGroupCfg;
private @ConfigType int mType; private @ConfigType int mType;
@Override public void startDocument() throws SAXException { @Override public void startDocument() throws SAXException {
@ -167,7 +166,7 @@ class ConfigHelper extends DefaultHandler {
ALog.w(TAG, "level【" + level + "】错误"); ALog.w(TAG, "level【" + level + "】错误");
level = ALog.LOG_LEVEL_VERBOSE; level = ALog.LOG_LEVEL_VERBOSE;
} }
setField("level", level, ConfigType.APP); setField("logLevel", level, ConfigType.APP);
break; break;
case "netCheck": // 是否检查网络 case "netCheck": // 是否检查网络
setField("netCheck", checkBoolean(value) ? Boolean.valueOf(value) : false, setField("netCheck", checkBoolean(value) ? Boolean.valueOf(value) : false,
@ -196,13 +195,13 @@ class ConfigHelper extends DefaultHandler {
private void setField(String key, Object value, int... types) { private void setField(String key, Object value, int... types) {
for (int type : types) { for (int type : types) {
if (type == ConfigType.DOWNLOAD) { if (type == ConfigType.DOWNLOAD) {
setField(Configuration.DownloadConfig.class, mDownloadConfig, key, value); setField(DownloadConfig.class, mDownloadConfig, key, value);
} else if (type == ConfigType.UPLOAD) { } else if (type == ConfigType.UPLOAD) {
setField(Configuration.UploadConfig.class, mUploadConfig, key, value); setField(UploadConfig.class, mUploadConfig, key, value);
} else if (type == ConfigType.APP) { } else if (type == ConfigType.APP) {
setField(Configuration.AppConfig.class, mAppConfig, key, value); setField(AppConfig.class, mAppConfig, key, value);
} else if (type == ConfigType.D_GROUP) { } else if (type == ConfigType.D_GROUP) {
setField(Configuration.DGroupConfig.class, mDGroupConfig, key, value); setField(DGroupConfig.class, mDGroupConfig, key, value);
} }
} }
} }

@ -31,8 +31,6 @@ public class BaseDListener extends BaseListener<DownloadEntity, DTaskWrapper, Do
BaseDListener(DownloadTask task, Handler outHandler) { BaseDListener(DownloadTask task, Handler outHandler) {
super(task, outHandler); super(task, outHandler);
isConvertSpeed = manager.getDownloadConfig().isConvertSpeed();
mUpdateInterval = manager.getDownloadConfig().getUpdateInterval();
} }
@Override public void onPostPre(long fileSize) { @Override public void onPostPre(long fileSize) {
@ -50,12 +48,12 @@ public class BaseDListener extends BaseListener<DownloadEntity, DTaskWrapper, Do
@Override @Override
protected void saveData(int state, long location) { protected void saveData(int state, long location) {
mTaskEntity.setState(state); mTaskWrapper.setState(state);
mEntity.setState(state); mEntity.setState(state);
if (state == IEntity.STATE_CANCEL) { if (state == IEntity.STATE_CANCEL) {
if (mEntity instanceof DownloadEntity) { if (mEntity instanceof DownloadEntity) {
CommonUtil.delTaskRecord(mEntity.getDownloadPath(), 1, mTaskEntity.isRemoveFile()); CommonUtil.delTaskRecord(mEntity.getDownloadPath(), 1, mTaskWrapper.isRemoveFile());
} }
return; return;
} else if (state == IEntity.STATE_STOP) { } else if (state == IEntity.STATE_STOP) {

@ -15,7 +15,7 @@
*/ */
package com.arialyy.aria.core.download; package com.arialyy.aria.core.download;
import com.arialyy.aria.orm.AbsWrapper; import com.arialyy.aria.orm.AbsDbWrapper;
import com.arialyy.aria.orm.annotation.Many; import com.arialyy.aria.orm.annotation.Many;
import com.arialyy.aria.orm.annotation.One; import com.arialyy.aria.orm.annotation.One;
import com.arialyy.aria.orm.annotation.Wrapper; import com.arialyy.aria.orm.annotation.Wrapper;
@ -26,7 +26,7 @@ import java.util.List;
* 任务组实体和子任务实体的关系 * 任务组实体和子任务实体的关系
*/ */
@Wrapper @Wrapper
public class DGEntityWrapper extends AbsWrapper { public class DGEntityWrapper extends AbsDbWrapper {
@One @One
public DownloadGroupEntity groupEntity; public DownloadGroupEntity groupEntity;

@ -15,11 +15,13 @@
*/ */
package com.arialyy.aria.core.download; package com.arialyy.aria.core.download;
import com.arialyy.aria.core.config.Configuration;
import com.arialyy.aria.core.config.DGroupConfig;
import com.arialyy.aria.core.inf.AbsGroupTaskWrapper; import com.arialyy.aria.core.inf.AbsGroupTaskWrapper;
import java.util.List; import java.util.List;
/** /**
* Created by AriaL on 2017/7/1. 任务组的任务实体 * Created by AriaL on 2017/7/1. 任务组的任务实体修饰器
*/ */
public class DGTaskWrapper extends AbsGroupTaskWrapper<DownloadGroupEntity, DTaskWrapper> { public class DGTaskWrapper extends AbsGroupTaskWrapper<DownloadGroupEntity, DTaskWrapper> {
@ -41,6 +43,10 @@ public class DGTaskWrapper extends AbsGroupTaskWrapper<DownloadGroupEntity, DTas
return getEntity().getKey(); return getEntity().getKey();
} }
@Override public DGroupConfig getConfig() {
return Configuration.getInstance().dGroupCfg;
}
@Override public List<DTaskWrapper> getSubTaskWrapper() { @Override public List<DTaskWrapper> getSubTaskWrapper() {
return subTaskEntities; return subTaskEntities;
} }

@ -15,6 +15,8 @@
*/ */
package com.arialyy.aria.core.download; package com.arialyy.aria.core.download;
import com.arialyy.aria.core.config.Configuration;
import com.arialyy.aria.core.config.DownloadConfig;
import com.arialyy.aria.core.inf.AbsTaskWrapper; import com.arialyy.aria.core.inf.AbsTaskWrapper;
/** /**
@ -43,6 +45,14 @@ public class DTaskWrapper extends AbsTaskWrapper<DownloadEntity> {
return getEntity().getKey(); return getEntity().getKey();
} }
@Override public DownloadConfig getConfig() {
if (isGroupTask) {
return Configuration.getInstance().dGroupCfg.getSubConfig();
} else {
return Configuration.getInstance().downloadCfg;
}
}
public String getGroupHash() { public String getGroupHash() {
return groupHash; return groupHash;
} }

@ -39,8 +39,6 @@ class DownloadGroupListener
super(task, outHandler); super(task, outHandler);
mSeedEntity = new GroupSendParams<>(); mSeedEntity = new GroupSendParams<>();
mSeedEntity.groupTask = task; mSeedEntity.groupTask = task;
isConvertSpeed = manager.getDownloadConfig().isConvertSpeed();
mUpdateInterval = manager.getDownloadConfig().getUpdateInterval();
} }
@Override public void onSubPre(DownloadEntity subEntity) { @Override public void onSubPre(DownloadEntity subEntity) {
@ -121,11 +119,11 @@ class DownloadGroupListener
} }
@Override protected void saveData(int state, long location) { @Override protected void saveData(int state, long location) {
mTaskEntity.setState(state); mTaskWrapper.setState(state);
mEntity.setState(state); mEntity.setState(state);
if (state == IEntity.STATE_CANCEL) { if (state == IEntity.STATE_CANCEL) {
if (mEntity instanceof DownloadGroupEntity) { if (mEntity instanceof DownloadGroupEntity) {
CommonUtil.delGroupTaskRecord(mTaskEntity.isRemoveFile(), mEntity); CommonUtil.delGroupTaskRecord(mTaskWrapper.isRemoveFile(), mEntity);
} }
return; return;
} else if (state == IEntity.STATE_STOP) { } else if (state == IEntity.STATE_STOP) {

@ -33,12 +33,12 @@ import com.arialyy.aria.util.CheckUtil;
*/ */
public class DownloadGroupTask extends AbsGroupTask<DownloadGroupEntity, DGTaskWrapper> { public class DownloadGroupTask extends AbsGroupTask<DownloadGroupEntity, DGTaskWrapper> {
private DownloadGroupTask(DGTaskWrapper taskEntity, Handler outHandler) { private DownloadGroupTask(DGTaskWrapper taskWrapper, Handler outHandler) {
mTaskWrapper = taskEntity; mTaskWrapper = taskWrapper;
mOutHandler = outHandler; mOutHandler = outHandler;
mContext = AriaManager.APP; mContext = AriaManager.APP;
mListener = new DownloadGroupListener(this, mOutHandler); mListener = new DownloadGroupListener(this, mOutHandler);
switch (taskEntity.getRequestType()) { switch (taskWrapper.getRequestType()) {
case AbsTaskWrapper.D_HTTP: case AbsTaskWrapper.D_HTTP:
mUtil = new DownloadGroupUtil((IDownloadGroupListener) mListener, mTaskWrapper); mUtil = new DownloadGroupUtil((IDownloadGroupListener) mListener, mTaskWrapper);
break; break;

@ -32,13 +32,13 @@ import java.io.File;
public class DownloadTask extends AbsNormalTask<DownloadEntity, DTaskWrapper> { public class DownloadTask extends AbsNormalTask<DownloadEntity, DTaskWrapper> {
public static final String TAG = "DownloadTask"; public static final String TAG = "DownloadTask";
private DownloadTask(DTaskWrapper taskEntity, Handler outHandler) { private DownloadTask(DTaskWrapper taskWrapper, Handler outHandler) {
mTaskWrapper = taskEntity; mTaskWrapper = taskWrapper;
mOutHandler = outHandler; mOutHandler = outHandler;
mContext = AriaManager.APP; mContext = AriaManager.APP;
mListener = new BaseDListener(this, mOutHandler); mListener = new BaseDListener(this, mOutHandler);
mUtil = new SimpleDownloadUtil(taskEntity, (IDownloadListener) mListener); mUtil = new SimpleDownloadUtil(taskWrapper, (IDownloadListener) mListener);
mEntity = taskEntity.getEntity(); mEntity = taskWrapper.getEntity();
} }
/** /**

@ -20,6 +20,8 @@ import aria.apache.commons.net.ftp.FTPReply;
import com.arialyy.aria.core.common.StateConstance; import com.arialyy.aria.core.common.StateConstance;
import com.arialyy.aria.core.common.SubThreadConfig; import com.arialyy.aria.core.common.SubThreadConfig;
import com.arialyy.aria.core.common.ftp.AbsFtpThreadTask; import com.arialyy.aria.core.common.ftp.AbsFtpThreadTask;
import com.arialyy.aria.core.config.BaseTaskConfig;
import com.arialyy.aria.core.config.DownloadConfig;
import com.arialyy.aria.core.download.DTaskWrapper; import com.arialyy.aria.core.download.DTaskWrapper;
import com.arialyy.aria.core.download.DownloadEntity; import com.arialyy.aria.core.download.DownloadEntity;
import com.arialyy.aria.core.inf.IDownloadListener; import com.arialyy.aria.core.inf.IDownloadListener;
@ -36,8 +38,7 @@ import java.nio.channels.FileChannel;
import java.nio.channels.ReadableByteChannel; import java.nio.channels.ReadableByteChannel;
/** /**
* Created by Aria.Lao on 2017/7/24. * Created by Aria.Lao on 2017/7/24. Ftp下载任务
* Ftp下载任务
*/ */
class FtpThreadTask extends AbsFtpThreadTask<DownloadEntity, DTaskWrapper> { class FtpThreadTask extends AbsFtpThreadTask<DownloadEntity, DTaskWrapper> {
private final String TAG = "FtpThreadTask"; private final String TAG = "FtpThreadTask";
@ -47,10 +48,6 @@ class FtpThreadTask extends AbsFtpThreadTask<DownloadEntity, DTaskWrapper> {
FtpThreadTask(StateConstance constance, IDownloadListener listener, FtpThreadTask(StateConstance constance, IDownloadListener listener,
SubThreadConfig<DTaskWrapper> downloadInfo) { SubThreadConfig<DTaskWrapper> downloadInfo) {
super(constance, listener, downloadInfo); super(constance, listener, downloadInfo);
mConnectTimeOut = mAridManager.getDownloadConfig().getConnectTimeOut();
mReadTimeOut = mAridManager.getDownloadConfig().getIOTimeOut();
mBufSize = mAridManager.getDownloadConfig().getBuffSize();
isNotNetRetry = mAridManager.getAppConfig().isNotNetRetry();
isOpenDynamicFile = STATE.TASK_RECORD.isOpenDynamicFile; isOpenDynamicFile = STATE.TASK_RECORD.isOpenDynamicFile;
isBlock = STATE.TASK_RECORD.isBlock; isBlock = STATE.TASK_RECORD.isBlock;
} }
@ -87,7 +84,8 @@ class FtpThreadTask extends AbsFtpThreadTask<DownloadEntity, DTaskWrapper> {
return this; return this;
} }
String remotePath = String remotePath =
new String(mTaskWrapper.asFtp().getUrlEntity().remotePath.getBytes(charSet), SERVER_CHARSET); new String(mTaskWrapper.asFtp().getUrlEntity().remotePath.getBytes(charSet),
SERVER_CHARSET);
ALog.i(TAG, String.format("remotePath【%s】", remotePath)); ALog.i(TAG, String.format("remotePath【%s】", remotePath));
is = client.retrieveFileStream(remotePath); is = client.retrieveFileStream(remotePath);
reply = client.getReplyCode(); reply = client.getReplyCode();
@ -174,7 +172,7 @@ class FtpThreadTask extends AbsFtpThreadTask<DownloadEntity, DTaskWrapper> {
fos = new FileOutputStream(mConfig.TEMP_FILE, true); fos = new FileOutputStream(mConfig.TEMP_FILE, true);
foc = fos.getChannel(); foc = fos.getChannel();
fic = Channels.newChannel(is); fic = Channels.newChannel(is);
ByteBuffer bf = ByteBuffer.allocate(mBufSize); ByteBuffer bf = ByteBuffer.allocate(getTaskConfig().getBuffSize());
while (isLive() && (len = fic.read(bf)) != -1) { while (isLive() && (len = fic.read(bf)) != -1) {
if (isBreak()) { if (isBreak()) {
break; break;
@ -223,9 +221,9 @@ class FtpThreadTask extends AbsFtpThreadTask<DownloadEntity, DTaskWrapper> {
private void readNormal(InputStream is) { private void readNormal(InputStream is) {
BufferedRandomAccessFile file = null; BufferedRandomAccessFile file = null;
try { try {
file = new BufferedRandomAccessFile(mConfig.TEMP_FILE, "rwd", mBufSize); file = new BufferedRandomAccessFile(mConfig.TEMP_FILE, "rwd", getTaskConfig().getBuffSize());
file.seek(mConfig.START_LOCATION); file.seek(mConfig.START_LOCATION);
byte[] buffer = new byte[mBufSize]; byte[] buffer = new byte[getTaskConfig().getBuffSize()];
int len; int len;
while (isLive() && (len = is.read(buffer)) != -1) { while (isLive() && (len = is.read(buffer)) != -1) {
if (isBreak()) { if (isBreak()) {
@ -259,6 +257,10 @@ class FtpThreadTask extends AbsFtpThreadTask<DownloadEntity, DTaskWrapper> {
} }
@Override public int getMaxSpeed() { @Override public int getMaxSpeed() {
return mAridManager.getDownloadConfig().getMaxSpeed(); return getTaskConfig().getMaxSpeed();
}
@Override protected DownloadConfig getTaskConfig() {
return mTaskWrapper.getConfig();
} }
} }

@ -118,7 +118,7 @@ class HttpFileInfoThread implements Runnable {
len = -1; len = -1;
} else { } else {
int start = temp.indexOf("/"); int start = temp.indexOf("/");
len = Long.parseLong(temp.substring(start + 1, temp.length())); len = Long.parseLong(temp.substring(start + 1));
} }
} }
} }

@ -19,6 +19,8 @@ import com.arialyy.aria.core.common.AbsThreadTask;
import com.arialyy.aria.core.common.RequestEnum; import com.arialyy.aria.core.common.RequestEnum;
import com.arialyy.aria.core.common.StateConstance; import com.arialyy.aria.core.common.StateConstance;
import com.arialyy.aria.core.common.SubThreadConfig; import com.arialyy.aria.core.common.SubThreadConfig;
import com.arialyy.aria.core.config.BaseTaskConfig;
import com.arialyy.aria.core.config.DownloadConfig;
import com.arialyy.aria.core.download.DTaskWrapper; import com.arialyy.aria.core.download.DTaskWrapper;
import com.arialyy.aria.core.download.DownloadEntity; import com.arialyy.aria.core.download.DownloadEntity;
import com.arialyy.aria.core.common.http.HttpTaskDelegate; import com.arialyy.aria.core.common.http.HttpTaskDelegate;
@ -44,8 +46,7 @@ import java.util.Map;
import java.util.Set; import java.util.Set;
/** /**
* Created by lyy on 2017/1/18. * Created by lyy on 2017/1/18. 下载线程
* 下载线程
*/ */
final class HttpThreadTask extends AbsThreadTask<DownloadEntity, DTaskWrapper> { final class HttpThreadTask extends AbsThreadTask<DownloadEntity, DTaskWrapper> {
private final String TAG = "HttpThreadTask"; private final String TAG = "HttpThreadTask";
@ -55,10 +56,6 @@ final class HttpThreadTask extends AbsThreadTask<DownloadEntity, DTaskWrapper> {
HttpThreadTask(StateConstance constance, IDownloadListener listener, HttpThreadTask(StateConstance constance, IDownloadListener listener,
SubThreadConfig<DTaskWrapper> downloadInfo) { SubThreadConfig<DTaskWrapper> downloadInfo) {
super(constance, listener, downloadInfo); super(constance, listener, downloadInfo);
mConnectTimeOut = mAridManager.getDownloadConfig().getConnectTimeOut();
mReadTimeOut = mAridManager.getDownloadConfig().getIOTimeOut();
mBufSize = mAridManager.getDownloadConfig().getBuffSize();
isNotNetRetry = mAridManager.getAppConfig().isNotNetRetry();
isOpenDynamicFile = STATE.TASK_RECORD.isOpenDynamicFile; isOpenDynamicFile = STATE.TASK_RECORD.isOpenDynamicFile;
isBlock = STATE.TASK_RECORD.isBlock; isBlock = STATE.TASK_RECORD.isBlock;
} }
@ -88,8 +85,8 @@ final class HttpThreadTask extends AbsThreadTask<DownloadEntity, DTaskWrapper> {
ALog.w(TAG, "该下载不支持断点"); ALog.w(TAG, "该下载不支持断点");
} }
ConnectionHelp.setConnectParam(taskDelegate, conn); ConnectionHelp.setConnectParam(taskDelegate, conn);
conn.setConnectTimeout(mConnectTimeOut); conn.setConnectTimeout(getTaskConfig().getConnectTimeOut());
conn.setReadTimeout(mReadTimeOut); //设置读取流的等待时间,必须设置该参数 conn.setReadTimeout(getTaskConfig().getIOTimeOut()); //设置读取流的等待时间,必须设置该参数
conn.connect(); conn.connect();
if (taskDelegate.getRequestEnum() == RequestEnum.POST) { if (taskDelegate.getRequestEnum() == RequestEnum.POST) {
Map<String, String> params = taskDelegate.getParams(); Map<String, String> params = taskDelegate.getParams();
@ -113,7 +110,8 @@ final class HttpThreadTask extends AbsThreadTask<DownloadEntity, DTaskWrapper> {
readDynamicFile(is); readDynamicFile(is);
} else { } else {
//创建可设置位置的文件 //创建可设置位置的文件
file = new BufferedRandomAccessFile(mConfig.TEMP_FILE, "rwd", mBufSize); file =
new BufferedRandomAccessFile(mConfig.TEMP_FILE, "rwd", getTaskConfig().getBuffSize());
//设置每条线程写入文件的位置 //设置每条线程写入文件的位置
file.seek(mConfig.START_LOCATION); file.seek(mConfig.START_LOCATION);
if (taskDelegate.isChunked()) { if (taskDelegate.isChunked()) {
@ -165,7 +163,7 @@ final class HttpThreadTask extends AbsThreadTask<DownloadEntity, DTaskWrapper> {
fos = new FileOutputStream(mConfig.TEMP_FILE, true); fos = new FileOutputStream(mConfig.TEMP_FILE, true);
foc = fos.getChannel(); foc = fos.getChannel();
fic = Channels.newChannel(is); fic = Channels.newChannel(is);
ByteBuffer bf = ByteBuffer.allocate(mBufSize); ByteBuffer bf = ByteBuffer.allocate(getTaskConfig().getBuffSize());
//如果要通过 Future 的 cancel 方法取消正在运行的任务,那么该任务必定是可以 对线程中断做出响应 的任务。 //如果要通过 Future 的 cancel 方法取消正在运行的任务,那么该任务必定是可以 对线程中断做出响应 的任务。
while (isLive() && (len = fic.read(bf)) != -1) { while (isLive() && (len = fic.read(bf)) != -1) {
@ -227,7 +225,7 @@ final class HttpThreadTask extends AbsThreadTask<DownloadEntity, DTaskWrapper> {
*/ */
private void readNormal(InputStream is, BufferedRandomAccessFile file) private void readNormal(InputStream is, BufferedRandomAccessFile file)
throws IOException { throws IOException {
byte[] buffer = new byte[mBufSize]; byte[] buffer = new byte[getTaskConfig().getBuffSize()];
int len; int len;
while (isLive() && (len = is.read(buffer)) != -1) { while (isLive() && (len = is.read(buffer)) != -1) {
if (isBreak()) { if (isBreak()) {
@ -293,4 +291,8 @@ final class HttpThreadTask extends AbsThreadTask<DownloadEntity, DTaskWrapper> {
@Override public int getMaxSpeed() { @Override public int getMaxSpeed() {
return mAridManager.getDownloadConfig().getMaxSpeed(); return mAridManager.getDownloadConfig().getMaxSpeed();
} }
@Override protected DownloadConfig getTaskConfig() {
return mTaskWrapper.getConfig();
}
} }

@ -17,6 +17,10 @@ package com.arialyy.aria.core.inf;
import com.arialyy.aria.core.common.ftp.FtpTaskDelegate; import com.arialyy.aria.core.common.ftp.FtpTaskDelegate;
import com.arialyy.aria.core.common.http.HttpTaskDelegate; import com.arialyy.aria.core.common.http.HttpTaskDelegate;
import com.arialyy.aria.core.config.BaseTaskConfig;
import com.arialyy.aria.core.config.DGroupConfig;
import com.arialyy.aria.core.config.DownloadConfig;
import com.arialyy.aria.core.config.UploadConfig;
import com.arialyy.aria.core.download.DownloadEntity; import com.arialyy.aria.core.download.DownloadEntity;
import com.arialyy.aria.core.download.DownloadGroupEntity; import com.arialyy.aria.core.download.DownloadGroupEntity;
import com.arialyy.aria.core.upload.UploadEntity; import com.arialyy.aria.core.upload.UploadEntity;
@ -76,8 +80,18 @@ public abstract class AbsTaskWrapper<ENTITY extends AbsEntity>
this.entity = entity; this.entity = entity;
} }
/**
* 任务识别标志 {@link AbsEntity#getKey()}
*/
public abstract String getKey(); public abstract String getKey();
/**
* 任务配置
*
* @return {@link DownloadConfig}{@link UploadConfig}{@link DGroupConfig}
*/
public abstract BaseTaskConfig getConfig();
@Override public ENTITY getEntity() { @Override public ENTITY getEntity() {
return entity; return entity;
} }

@ -48,7 +48,7 @@ public class DownloadGroupTaskQueue
} }
@Override public int getMaxTaskNum() { @Override public int getMaxTaskNum() {
return AriaManager.getInstance(AriaManager.APP).getDownloadConfig().getMaxTaskNum(); return AriaManager.getInstance(AriaManager.APP).getDGroupConfig().getMaxTaskNum();
} }
@Override public DownloadGroupTask createTask(DGTaskWrapper wrapper) { @Override public DownloadGroupTask createTask(DGTaskWrapper wrapper) {
@ -65,6 +65,6 @@ public class DownloadGroupTaskQueue
} }
@Override public int getOldMaxNum() { @Override public int getOldMaxNum() {
return AriaManager.getInstance(AriaManager.APP).getDownloadConfig().oldMaxTaskNum; return AriaManager.getInstance(AriaManager.APP).getDGroupConfig().oldMaxTaskNum;
} }
} }

@ -320,16 +320,9 @@ abstract class AbsSchedulers<TASK_ENTITY extends AbsTaskWrapper, TASK extends Ab
callback(FAIL, task); callback(FAIL, task);
return; return;
} }
long interval = 2000; long interval = task.getTaskWrapper().getConfig().getReTryInterval();
int num = 10; int num = task.getTaskWrapper().getConfig().getReTryNum();
boolean isNotNetRetry = manager.getAppConfig().isNotNetRetry();; boolean isNotNetRetry = manager.getAppConfig().isNotNetRetry();
if (task instanceof DownloadTask || task instanceof DownloadGroupTask) {
interval = manager.getDownloadConfig().getReTryInterval();
num = manager.getDownloadConfig().getReTryNum();
} else if (task instanceof UploadTask) {
interval = manager.getUploadConfig().getReTryInterval();
num = manager.getUploadConfig().getReTryNum();
}
final int reTryNum = num; final int reTryNum = num;
if ((!NetUtils.isConnected(AriaManager.APP) && !isNotNetRetry) if ((!NetUtils.isConnected(AriaManager.APP) && !isNotNetRetry)

@ -29,19 +29,14 @@ class BaseUListener extends BaseListener<UploadEntity, UTaskWrapper, UploadTask>
BaseUListener(UploadTask task, Handler outHandler) { BaseUListener(UploadTask task, Handler outHandler) {
super(task, outHandler); super(task, outHandler);
isConvertSpeed = manager.getUploadConfig().isConvertSpeed();
mUpdateInterval = manager.getUploadConfig().getUpdateInterval();
} }
@Override @Override
protected void saveData(int state, long location) { protected void saveData(int state, long location) {
mTaskEntity.setState(state); mTaskWrapper.setState(state);
mEntity.setState(state); mEntity.setState(state);
if (state == IEntity.STATE_CANCEL) { if (state == IEntity.STATE_CANCEL) {
if (mEntity instanceof UploadEntity) { CommonUtil.delTaskRecord(mEntity.getFilePath(), 2, mTaskWrapper.isRemoveFile());
CommonUtil.delTaskRecord(mEntity.getFilePath(), 2, mTaskEntity.isRemoveFile());
}
return;
} else if (state == IEntity.STATE_STOP) { } else if (state == IEntity.STATE_STOP) {
mEntity.setStopTime(System.currentTimeMillis()); mEntity.setStopTime(System.currentTimeMillis());
} else if (state == IEntity.STATE_COMPLETE) { } else if (state == IEntity.STATE_COMPLETE) {

@ -15,6 +15,8 @@
*/ */
package com.arialyy.aria.core.upload; package com.arialyy.aria.core.upload;
import com.arialyy.aria.core.config.Configuration;
import com.arialyy.aria.core.config.UploadConfig;
import com.arialyy.aria.core.inf.AbsTaskWrapper; import com.arialyy.aria.core.inf.AbsTaskWrapper;
/** /**
@ -32,4 +34,8 @@ public class UTaskWrapper extends AbsTaskWrapper<UploadEntity> {
@Override public String getKey() { @Override public String getKey() {
return getEntity().getKey(); return getEntity().getKey();
} }
@Override public UploadConfig getConfig() {
return Configuration.getInstance().uploadCfg;
}
} }

@ -21,6 +21,9 @@ import aria.apache.commons.net.ftp.OnFtpInputStreamListener;
import com.arialyy.aria.core.common.StateConstance; import com.arialyy.aria.core.common.StateConstance;
import com.arialyy.aria.core.common.SubThreadConfig; import com.arialyy.aria.core.common.SubThreadConfig;
import com.arialyy.aria.core.common.ftp.AbsFtpThreadTask; import com.arialyy.aria.core.common.ftp.AbsFtpThreadTask;
import com.arialyy.aria.core.config.BaseTaskConfig;
import com.arialyy.aria.core.config.DownloadConfig;
import com.arialyy.aria.core.config.UploadConfig;
import com.arialyy.aria.core.inf.IEventListener; import com.arialyy.aria.core.inf.IEventListener;
import com.arialyy.aria.core.upload.UploadEntity; import com.arialyy.aria.core.upload.UploadEntity;
import com.arialyy.aria.core.upload.UTaskWrapper; import com.arialyy.aria.core.upload.UTaskWrapper;
@ -41,14 +44,14 @@ class FtpThreadTask extends AbsFtpThreadTask<UploadEntity, UTaskWrapper> {
FtpThreadTask(StateConstance constance, IEventListener listener, FtpThreadTask(StateConstance constance, IEventListener listener,
SubThreadConfig<UTaskWrapper> info) { SubThreadConfig<UTaskWrapper> info) {
super(constance, listener, info); super(constance, listener, info);
mConnectTimeOut = mAridManager.getUploadConfig().getConnectTimeOut();
mReadTimeOut = mAridManager.getUploadConfig().getIOTimeOut();
mBufSize = mAridManager.getUploadConfig().getBuffSize();
isNotNetRetry = mAridManager.getAppConfig().isNotNetRetry();
} }
@Override public int getMaxSpeed() { @Override public int getMaxSpeed() {
return mAridManager.getUploadConfig().getMaxSpeed(); return getTaskConfig().getMaxSpeed();
}
@Override protected UploadConfig getTaskConfig() {
return mTaskWrapper.getConfig();
} }
@Override public FtpThreadTask call() throws Exception { @Override public FtpThreadTask call() throws Exception {
@ -79,7 +82,7 @@ class FtpThreadTask extends AbsFtpThreadTask<UploadEntity, UTaskWrapper> {
return this; return this;
} }
file = new BufferedRandomAccessFile(mConfig.TEMP_FILE, "rwd", mBufSize); file = new BufferedRandomAccessFile(mConfig.TEMP_FILE, "rwd", getTaskConfig().getBuffSize());
if (mConfig.START_LOCATION != 0) { if (mConfig.START_LOCATION != 0) {
//file.skipBytes((int) mConfig.START_LOCATION); //file.skipBytes((int) mConfig.START_LOCATION);
file.seek(mConfig.START_LOCATION); file.seek(mConfig.START_LOCATION);

@ -19,6 +19,8 @@ import com.arialyy.aria.core.common.AbsThreadTask;
import com.arialyy.aria.core.common.StateConstance; import com.arialyy.aria.core.common.StateConstance;
import com.arialyy.aria.core.common.SubThreadConfig; import com.arialyy.aria.core.common.SubThreadConfig;
import com.arialyy.aria.core.common.http.HttpTaskDelegate; import com.arialyy.aria.core.common.http.HttpTaskDelegate;
import com.arialyy.aria.core.config.BaseTaskConfig;
import com.arialyy.aria.core.config.UploadConfig;
import com.arialyy.aria.core.inf.IUploadListener; import com.arialyy.aria.core.inf.IUploadListener;
import com.arialyy.aria.core.upload.UTaskWrapper; import com.arialyy.aria.core.upload.UTaskWrapper;
import com.arialyy.aria.core.upload.UploadEntity; import com.arialyy.aria.core.upload.UploadEntity;
@ -40,8 +42,7 @@ import java.util.Set;
import java.util.UUID; import java.util.UUID;
/** /**
* Created by Aria.Lao on 2017/7/28. * Created by Aria.Lao on 2017/7/28. 不支持断点的HTTP上传任务
* 不支持断点的HTTP上传任务
*/ */
class HttpThreadTask extends AbsThreadTask<UploadEntity, UTaskWrapper> { class HttpThreadTask extends AbsThreadTask<UploadEntity, UTaskWrapper> {
private final String TAG = "HttpThreadTask"; private final String TAG = "HttpThreadTask";
@ -54,10 +55,6 @@ class HttpThreadTask extends AbsThreadTask<UploadEntity, UTaskWrapper> {
HttpThreadTask(StateConstance constance, IUploadListener listener, HttpThreadTask(StateConstance constance, IUploadListener listener,
SubThreadConfig<UTaskWrapper> uploadInfo) { SubThreadConfig<UTaskWrapper> uploadInfo) {
super(constance, listener, uploadInfo); super(constance, listener, uploadInfo);
mConnectTimeOut = mAridManager.getUploadConfig().getConnectTimeOut();
mReadTimeOut = mAridManager.getUploadConfig().getIOTimeOut();
mBufSize = mAridManager.getUploadConfig().getBuffSize();
isNotNetRetry = mAridManager.getAppConfig().isNotNetRetry();
} }
@Override public HttpThreadTask call() throws Exception { @Override public HttpThreadTask call() throws Exception {
@ -83,11 +80,11 @@ class HttpThreadTask extends AbsThreadTask<UploadEntity, UTaskWrapper> {
mHttpConn.setRequestProperty("Content-Type", mHttpConn.setRequestProperty("Content-Type",
taskDelegate.getContentType() + "; boundary=" + BOUNDARY); taskDelegate.getContentType() + "; boundary=" + BOUNDARY);
mHttpConn.setRequestProperty("User-Agent", taskDelegate.getUserAgent()); mHttpConn.setRequestProperty("User-Agent", taskDelegate.getUserAgent());
mHttpConn.setConnectTimeout(mConnectTimeOut); mHttpConn.setConnectTimeout(getTaskConfig().getConnectTimeOut());
mHttpConn.setReadTimeout(mReadTimeOut); mHttpConn.setReadTimeout(getTaskConfig().getIOTimeOut());
//mHttpConn.setRequestProperty("Range", "bytes=" + 0 + "-" + "100"); //mHttpConn.setRequestProperty("Range", "bytes=" + 0 + "-" + "100");
//内部缓冲区---分段上传防止oom //内部缓冲区---分段上传防止oom
mHttpConn.setChunkedStreamingMode(mBufSize); mHttpConn.setChunkedStreamingMode(getTaskConfig().getBuffSize());
//添加Http请求头部 //添加Http请求头部
Set<String> keys = taskDelegate.getHeaders().keySet(); Set<String> keys = taskDelegate.getHeaders().keySet();
@ -223,6 +220,10 @@ class HttpThreadTask extends AbsThreadTask<UploadEntity, UTaskWrapper> {
} }
@Override public int getMaxSpeed() { @Override public int getMaxSpeed() {
return mAridManager.getUploadConfig().getMaxSpeed(); return getTaskConfig().getMaxSpeed();
}
@Override protected UploadConfig getTaskConfig() {
return mTaskWrapper.getConfig();
} }
} }

@ -18,7 +18,7 @@ package com.arialyy.aria.orm;
/** /**
* Created by laoyuyu on 2018/3/30. * Created by laoyuyu on 2018/3/30.
*/ */
public abstract class AbsWrapper { public abstract class AbsDbWrapper {
/** /**
* 处理转换 * 处理转换

@ -33,7 +33,7 @@ public abstract class DbEntity {
///** ///**
// * 保存关联数据 // * 保存关联数据
// */ // */
//public static void saveRelationData(AbsWrapper wrapper) { //public static void saveRelationData(AbsDbWrapper wrapper) {
// DelegateWrapper.getInstance().saveRelationData(wrapper); // DelegateWrapper.getInstance().saveRelationData(wrapper);
//} //}
@ -42,7 +42,7 @@ public abstract class DbEntity {
* *
* @param expression 查询条件 * @param expression 查询条件
*/ */
public static <T extends AbsWrapper> List<T> findRelationData(Class<T> clazz, public static <T extends AbsDbWrapper> List<T> findRelationData(Class<T> clazz,
String... expression) { String... expression) {
return DelegateWrapper.getInstance().findRelationData(clazz, expression); return DelegateWrapper.getInstance().findRelationData(clazz, expression);
} }

@ -53,7 +53,7 @@ class DelegateFind extends AbsDelegate {
* 如果实体中有多个{@link One}{@link Many}注解将返回nul * 如果实体中有多个{@link One}{@link Many}注解将返回nul
* {@link One} 的注解对象必须是{@link DbEntity}{@link Many}的注解对象必须是List并且List中的类型必须是{@link DbEntity} * {@link One} 的注解对象必须是{@link DbEntity}{@link Many}的注解对象必须是List并且List中的类型必须是{@link DbEntity}
*/ */
<T extends AbsWrapper> List<T> findRelationData(SQLiteDatabase db, Class<T> clazz, <T extends AbsDbWrapper> List<T> findRelationData(SQLiteDatabase db, Class<T> clazz,
String... expression) { String... expression) {
db = checkDb(db); db = checkDb(db);
@ -202,7 +202,7 @@ class DelegateFind extends AbsDelegate {
* @param pColumnAlias 关联查询父表别名 * @param pColumnAlias 关联查询父表别名
* @param cColumnAlias 关联查询子表别名 * @param cColumnAlias 关联查询子表别名
*/ */
private synchronized <T extends AbsWrapper, P extends DbEntity, C extends DbEntity> List<T> newInstanceEntity( private synchronized <T extends AbsDbWrapper, P extends DbEntity, C extends DbEntity> List<T> newInstanceEntity(
Class<T> clazz, Class<P> parent, Class<T> clazz, Class<P> parent,
Class<C> child, Class<C> child,
Cursor cursor, Cursor cursor,

@ -37,7 +37,7 @@ class DelegateUpdate extends AbsDelegate {
///** ///**
// * 添加或更新关联数据 // * 添加或更新关联数据
// */ // */
//void saveRelationData(SQLiteDatabase db, AbsWrapper wrapper) { //void saveRelationData(SQLiteDatabase db, AbsDbWrapper wrapper) {
// Class clazz = wrapper.getClass(); // Class clazz = wrapper.getClass();
// List<Field> fields = CommonUtil.getAllFields(clazz); // List<Field> fields = CommonUtil.getAllFields(clazz);
// DbEntity one = null; // DbEntity one = null;

@ -60,7 +60,7 @@ public class DelegateWrapper {
* *
* @param expression 查询条件 * @param expression 查询条件
*/ */
<T extends AbsWrapper> List<T> findRelationData(Class<T> clazz, String... expression) { <T extends AbsDbWrapper> List<T> findRelationData(Class<T> clazz, String... expression) {
return mDManager.getDelegate(DelegateFind.class).findRelationData(mDb, clazz, expression); return mDManager.getDelegate(DelegateFind.class).findRelationData(mDb, clazz, expression);
} }

@ -112,9 +112,6 @@
<!-- 下载类组合任务 --> <!-- 下载类组合任务 -->
<dGroup> <dGroup>
<!--设置任务最大下载速度,0表示不限速,单位为:kb-->
<maxSpeed value="128"/>
<!--组合任务下载队列最大任务数, 默认为2--> <!--组合任务下载队列最大任务数, 默认为2-->
<maxTaskNum value="1"/> <maxTaskNum value="1"/>
@ -157,5 +154,8 @@
<!--子任务是否需要转换速度单位,转换完成后为:1b/s、1kb/s、1mb/s、1gb/s、1tb/s,如果不需要将返回byte长度--> <!--子任务是否需要转换速度单位,转换完成后为:1b/s、1kb/s、1mb/s、1gb/s、1tb/s,如果不需要将返回byte长度-->
<convertSpeed value="true"/> <convertSpeed value="true"/>
<!--子任务的最大下载速度,0表示不限速,单位为:kb; -->
<maxSpeed value="128"/>
</dGroup> </dGroup>
</aria> </aria>

@ -1,621 +0,0 @@
/*
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.arialyy.aria.core;
import android.text.TextUtils;
import com.arialyy.aria.core.common.QueueMod;
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.util.ALog;
import com.arialyy.aria.util.AriaCrashHandler;
import com.arialyy.aria.util.CommonUtil;
import java.io.File;
import java.io.Serializable;
/**
* Created by lyy on 2016/12/8. 信息配置 kotlin 方式有bug不能将public去掉
*/
public final class Configuration {
private static final String TAG = "Configuration";
private static final String DOWNLOAD_CONFIG_FILE = "/Aria/AriaDownload.cfg";
private static final String UPLOAD_CONFIG_FILE = "/Aria/AriaUpload.cfg";
private static final String APP_CONFIG_FILE = "/Aria/AriaApp.cfg";
private static final String DGROUP_CONFIG_FILE = "/Aria/AriaDGroup.cfg";
private static final int TYPE_DOWNLOAD = 1;
private static final int TYPE_UPLOAD = 2;
private static final int TYPE_APP = 3;
private static final int TYPE_DGROUP = 4;
private static volatile Configuration INSTANCE = null;
static final String XML_FILE = "/Aria/aria_config.xml";
DownloadConfig downloadCfg;
UploadConfig uploadCfg;
AppConfig appCfg;
DGroupConfig dGroupCfg;
private Configuration() {
//删除老版本的配置文件
String basePath = AriaManager.APP.getFilesDir().getPath();
File oldDCfg = new File(String.format("%s/Aria/DownloadConfig.properties", basePath));
if (oldDCfg.exists()) { // 只需要判断一个
File oldUCfg = new File(String.format("%s/Aria/UploadConfig.properties", basePath));
File oldACfg = new File(String.format("%s/Aria/AppConfig.properties", basePath));
oldDCfg.delete();
oldUCfg.delete();
oldACfg.delete();
// 删除配置触发更新
File temp = new File(String.format("%s%s", basePath, XML_FILE));
if (temp.exists()) {
temp.delete();
}
}
File newDCfg = new File(String.format("%s%s", basePath, DOWNLOAD_CONFIG_FILE));
File newUCfg = new File(String.format("%s%s", basePath, UPLOAD_CONFIG_FILE));
File newACfg = new File(String.format("%s%s", basePath, APP_CONFIG_FILE));
File dgCfg = new File(String.format("%s%s", basePath, DGROUP_CONFIG_FILE));
// 加载下载配置
if (newDCfg.exists()) {
downloadCfg = (DownloadConfig) CommonUtil.readObjFromFile(newDCfg.getPath());
}
if (downloadCfg == null) {
downloadCfg = new DownloadConfig();
}
// 加载上传配置
if (newUCfg.exists()) {
uploadCfg = (UploadConfig) CommonUtil.readObjFromFile(newUCfg.getPath());
}
if (uploadCfg == null) {
uploadCfg = new UploadConfig();
}
// 加载app配置
if (newACfg.exists()) {
appCfg = (AppConfig) CommonUtil.readObjFromFile(newACfg.getPath());
}
if (appCfg == null) {
appCfg = new AppConfig();
}
// 加载下载类型组合任务的配置
if (dgCfg.exists()) {
dGroupCfg = (DGroupConfig) CommonUtil.readObjFromFile(dgCfg.getPath());
}
if (dGroupCfg == null) {
dGroupCfg = new DGroupConfig();
}
}
static Configuration getInstance() {
if (INSTANCE == null) {
synchronized (AppConfig.class) {
INSTANCE = new Configuration();
}
}
return INSTANCE;
}
/**
* 检查配置文件是否存在只要{@link DownloadConfig}{@link UploadConfig}{@link AppConfig}{@link
* DGroupConfig}其中一个不存在 则任务配置文件不存在
*
* @return {@code true}配置存在{@code false}配置不存在
*/
boolean configExists() {
String basePath = AriaManager.APP.getFilesDir().getPath();
return (new File(String.format("%s%s", basePath, DOWNLOAD_CONFIG_FILE))).exists()
&& (new File(String.format("%s%s", basePath, UPLOAD_CONFIG_FILE))).exists()
&& (new File(String.format("%s%s", basePath, APP_CONFIG_FILE))).exists()
&& (new File(String.format("%s%s", basePath, DGROUP_CONFIG_FILE))).exists();
}
abstract static class BaseConfig implements Serializable {
/**
* 类型
*
* @return {@link #TYPE_DOWNLOAD}{@link #TYPE_UPLOAD}{@link #TYPE_APP}{@link #TYPE_DGROUP}
*/
abstract int getType();
/**
* 保存配置
*/
void save() {
String basePath = AriaManager.APP.getFilesDir().getPath();
String path = null;
switch (getType()) {
case TYPE_DOWNLOAD:
path = DOWNLOAD_CONFIG_FILE;
break;
case TYPE_UPLOAD:
path = UPLOAD_CONFIG_FILE;
break;
case TYPE_APP:
path = APP_CONFIG_FILE;
break;
}
if (!TextUtils.isEmpty(path)) {
String tempPath = String.format("%s%s", basePath, path);
CommonUtil.deleteFile(tempPath);
CommonUtil.writeObjToFile(tempPath, this);
} else {
ALog.e(TAG, String.format("保存配置失败,配置类型:%s,原因:路径错误", getType()));
}
}
}
/**
* 通用任务配置
*/
abstract static class BaseTaskConfig extends BaseConfig implements Serializable {
/**
* 设置写文件buff大小该数值大小不能小于2048数值变小下载速度会变慢
*/
int buffSize = 8192;
/**
* 进度刷新间隔默认1秒
*/
long updateInterval = 1000;
/**
* 旧任务数
*/
public int oldMaxTaskNum = 2;
/**
* 任务队列最大任务数 默认为2
*/
int maxTaskNum = 2;
/**
* 下载失败重试次数默认为10
*/
int reTryNum = 10;
/**
* 设置重试间隔单位为毫秒默认2000毫秒
*/
int reTryInterval = 2000;
/**
* 设置url连接超时时间单位为毫秒默认5000毫秒
*/
int connectTimeOut = 5000;
/**
* 是否需要转换速度单位转换完成后为1b/s1k/s1m/s1g/s1t/s如果不需要将返回byte长度
*/
boolean isConvertSpeed = false;
/**
* 执行队列类型
*
* @see QueueMod
*/
String queueMod = "wait";
/**
* 设置IO流读取时间单位为毫秒默认20000毫秒该时间不能少于10000毫秒
*/
int iOTimeOut = 20 * 1000;
/**
* 设置最大下载/上传速度单位kb, 为0表示不限速
*/
int maxSpeed = 0;
/**
* 设置https ca 证书信息path 为assets目录下的CA证书完整路径
*/
String caPath;
/**
* name 为CA证书名
*/
String caName;
public String getCaPath() {
return caPath;
}
public BaseConfig setCaPath(String caPath) {
this.caPath = caPath;
save();
return this;
}
public String getCaName() {
return caName;
}
public BaseConfig setCaName(String caName) {
this.caName = caName;
save();
return this;
}
public BaseTaskConfig setMaxTaskNum(int maxTaskNum) {
oldMaxTaskNum = this.maxTaskNum;
this.maxTaskNum = maxTaskNum;
save();
return this;
}
public int getMaxSpeed() {
return maxSpeed;
}
public BaseTaskConfig setMaxSpeed(int maxSpeed) {
this.maxSpeed = maxSpeed;
save();
return this;
}
public long getUpdateInterval() {
return updateInterval;
}
/**
* 设置进度更新间隔该设置对正在运行的任务无效默认为1000毫秒
*
* @param updateInterval 不能小于0
*/
public BaseTaskConfig setUpdateInterval(long updateInterval) {
if (updateInterval <= 0) {
ALog.w("Configuration", "进度更新间隔不能小于0");
return this;
}
this.updateInterval = updateInterval;
save();
return this;
}
public String getQueueMod() {
return queueMod;
}
public BaseTaskConfig setQueueMod(String queueMod) {
this.queueMod = queueMod;
save();
return this;
}
public int getMaxTaskNum() {
return maxTaskNum;
}
public int getReTryNum() {
return reTryNum;
}
public BaseTaskConfig setReTryNum(int reTryNum) {
this.reTryNum = reTryNum;
save();
return this;
}
public int getReTryInterval() {
return reTryInterval;
}
public BaseTaskConfig setReTryInterval(int reTryInterval) {
this.reTryInterval = reTryInterval;
save();
return this;
}
public boolean isConvertSpeed() {
return isConvertSpeed;
}
public BaseTaskConfig setConvertSpeed(boolean convertSpeed) {
isConvertSpeed = convertSpeed;
save();
return this;
}
public int getConnectTimeOut() {
return connectTimeOut;
}
public BaseTaskConfig setConnectTimeOut(int connectTimeOut) {
this.connectTimeOut = connectTimeOut;
save();
return this;
}
public int getIOTimeOut() {
return iOTimeOut;
}
public BaseTaskConfig setIOTimeOut(int iOTimeOut) {
this.iOTimeOut = iOTimeOut;
save();
return this;
}
public int getBuffSize() {
return buffSize;
}
public BaseTaskConfig setBuffSize(int buffSize) {
this.buffSize = buffSize;
save();
return this;
}
}
/**
* 下载配置
*/
public static class DownloadConfig extends BaseTaskConfig implements Serializable {
/**
* 下载线程数下载线程数不能小于1 注意 1线程下载数改变后新的下载任务才会生效 2如果任务大小小于1m该设置不会生效
* 3从3.4.1开始如果线程数为1文件初始化时将不再预占用对应长度的空间下载多少byte则占多大的空间 对于采用多线程的任务或旧任务依然采用原来的文件空间占用方式
*/
int threadNum = 3;
/**
* 多线程下载是否使用块下载模式{@code true}使用{@code false}不使用 注意 1使用分块模式在读写性能底下的手机上合并文件需要的时间会更加长
* 2优点是使用多线程的块下载初始化时文件初始化时将不会预占用对应长度的空间 3只对新的多线程下载任务有效 4只对多线程的任务有效
*/
boolean useBlock = false;
public boolean isUseBlock() {
return useBlock;
}
@Override public DownloadConfig setMaxSpeed(int maxSpeed) {
super.setMaxSpeed(maxSpeed);
DownloadTaskQueue.getInstance().setMaxSpeed(maxSpeed);
return this;
}
public DownloadConfig setUseBlock(boolean useBlock) {
this.useBlock = useBlock;
save();
return this;
}
public DownloadConfig setMaxTaskNum(int maxTaskNum) {
super.setMaxTaskNum(maxTaskNum);
DownloadTaskQueue.getInstance().setMaxTaskNum(maxTaskNum);
return this;
}
public DownloadConfig setThreadNum(int threadNum) {
this.threadNum = threadNum;
save();
return this;
}
public int getThreadNum() {
return threadNum;
}
private DownloadConfig() {
}
@Override int getType() {
return TYPE_DOWNLOAD;
}
}
/**
* 上传配置
*/
public static class UploadConfig extends BaseTaskConfig implements Serializable {
private static UploadConfig INSTANCE = null;
private UploadConfig() {
//loadConfig();
}
@Override public UploadConfig setMaxSpeed(int maxSpeed) {
super.setMaxSpeed(maxSpeed);
UploadTaskQueue.getInstance().setMaxSpeed(maxSpeed);
return this;
}
public UploadConfig setMaxTaskNum(int maxTaskNum) {
super.setMaxTaskNum(maxTaskNum);
UploadTaskQueue.getInstance().setMaxTaskNum(maxTaskNum);
return this;
}
static UploadConfig getInstance() {
if (INSTANCE == null) {
synchronized (UploadConfig.class) {
INSTANCE = new UploadConfig();
}
}
return INSTANCE;
}
@Override int getType() {
return TYPE_UPLOAD;
}
}
/**
* 下载类型的组合任务
*/
public static class DGroupConfig extends BaseTaskConfig implements Serializable {
private static DGroupConfig INSTANCE = null;
/**
* 能同时下载的子任务最大任务数默认3
*/
int subMaxTaskNum = 3;
/**
* 子任务重试次数默认为5
*/
int subReTryNum = 5;
/**
* 子任务下载失败时的重试间隔单位为毫秒默认2000毫秒-
*/
int subReTryInterval = 2000;
private DGroupConfig() {
}
static DGroupConfig getInstance() {
if (INSTANCE == null) {
synchronized (DGroupConfig.class) {
INSTANCE = new DGroupConfig();
}
}
return INSTANCE;
}
@Override int getType() {
return TYPE_DGROUP;
}
@Override public DGroupConfig setMaxSpeed(int maxSpeed) {
super.setMaxSpeed(maxSpeed);
DownloadGroupTaskQueue.getInstance().setMaxSpeed(maxSpeed);
return this;
}
public DGroupConfig setMaxTaskNum(int maxTaskNum) {
super.setMaxTaskNum(maxTaskNum);
DownloadGroupTaskQueue.getInstance().setMaxTaskNum(maxSpeed);
return this;
}
public int getSubMaxTaskNum() {
return subMaxTaskNum;
}
public DGroupConfig setSubMaxTaskNum(int subMaxTaskNum) {
this.subMaxTaskNum = subMaxTaskNum;
save();
return this;
}
public int getSubReTryNum() {
return subReTryNum;
}
public DGroupConfig setSubReTryNum(int subReTryNum) {
this.subReTryNum = subReTryNum;
save();
return this;
}
public int getSubReTryInterval() {
return subReTryInterval;
}
public DGroupConfig setSubReTryInterval(int subReTryInterval) {
this.subReTryInterval = subReTryInterval;
save();
return this;
}
}
/**
* 应用配置
*/
public static class AppConfig extends BaseConfig implements Serializable {
/**
* 是否使用{@link AriaCrashHandler}来捕获异常 {@code true} 使用{@code false} 不使用
*/
boolean useAriaCrashHandler;
/**
* 设置Aria的日志级别
*
* {@link ALog#LOG_LEVEL_VERBOSE}
*/
int logLevel;
/**
* 是否检查网络{@code true}检查网络
*/
boolean netCheck = true;
/**
* 是否使用广播 除非无法使用注解否则不建议使用广播来接受任务 {@code true} 使用广播{@code false} 不适用广播
*/
boolean useBroadcast = false;
/**
* 断网的时候是否重试{@code true}断网也重试{@code false}断网不重试直接走失败的回调
*/
boolean notNetRetry = false;
public boolean isNotNetRetry() {
return notNetRetry;
}
public AppConfig setNotNetRetry(boolean notNetRetry) {
this.notNetRetry = notNetRetry;
save();
return this;
}
public boolean isUseBroadcast() {
return useBroadcast;
}
public AppConfig setUseBroadcast(boolean useBroadcast) {
this.useBroadcast = useBroadcast;
save();
return this;
}
public boolean isNetCheck() {
return netCheck;
}
public AppConfig setNetCheck(boolean netCheck) {
this.netCheck = netCheck;
save();
return this;
}
public AppConfig setLogLevel(int level) {
this.logLevel = level;
ALog.LOG_LEVEL = level;
save();
return this;
}
public int getLogLevel() {
return logLevel;
}
public boolean getUseAriaCrashHandler() {
return useAriaCrashHandler;
}
public AppConfig setUseAriaCrashHandler(boolean useAriaCrashHandler) {
this.useAriaCrashHandler = useAriaCrashHandler;
if (useAriaCrashHandler) {
Thread.setDefaultUncaughtExceptionHandler(new AriaCrashHandler());
} else {
Thread.setDefaultUncaughtExceptionHandler(null);
}
save();
return this;
}
@Override int getType() {
return TYPE_APP;
}
}
}

@ -27,11 +27,11 @@ import com.arialyy.aria.core.upload.uploader.SimpleUploadUtil;
*/ */
public class UploadTask extends AbsNormalTask<UploadEntity, UTaskWrapper> { public class UploadTask extends AbsNormalTask<UploadEntity, UTaskWrapper> {
private UploadTask(UTaskWrapper taskEntity, Handler outHandler) { private UploadTask(UTaskWrapper taskWrapper, Handler outHandler) {
mTaskWrapper = taskEntity; mTaskWrapper = taskWrapper;
mOutHandler = outHandler; mOutHandler = outHandler;
mListener = new BaseUListener(this, mOutHandler); mListener = new BaseUListener(this, mOutHandler);
mUtil = new SimpleUploadUtil(taskEntity, (IUploadListener) mListener); mUtil = new SimpleUploadUtil(taskWrapper, (IUploadListener) mListener);
} }
@Override public int getTaskType() { @Override public int getTaskType() {

Loading…
Cancel
Save