重构设置部分代码

pull/330/head
AriaLyy 8 years ago
parent 9a6e60f67f
commit e4ba3df523
  1. 120
      Aria/src/main/java/com/arialyy/aria/core/AriaManager.java
  2. 221
      Aria/src/main/java/com/arialyy/aria/core/ConfigHelper.java
  3. 220
      Aria/src/main/java/com/arialyy/aria/core/Configuration.java
  4. 22
      Aria/src/main/java/com/arialyy/aria/core/download/DownloadTask.java
  5. 1
      Aria/src/main/java/com/arialyy/aria/core/queue/AbsTaskQueue.java
  6. 5
      Aria/src/main/java/com/arialyy/aria/core/queue/DownloadTaskQueue.java
  7. 3
      Aria/src/main/java/com/arialyy/aria/core/queue/UploadTaskQueue.java
  8. 18
      Aria/src/main/java/com/arialyy/aria/core/queue/pool/ExecutePool.java
  9. 17
      Aria/src/main/java/com/arialyy/aria/core/scheduler/DownloadSchedulers.java
  10. 16
      Aria/src/main/java/com/arialyy/aria/core/scheduler/UploadSchedulers.java
  11. 27
      Aria/src/main/java/com/arialyy/aria/core/upload/UploadTask.java
  12. 113
      Aria/src/main/java/com/arialyy/aria/util/ReflectionUtil.java
  13. 13
      app/src/main/assets/aria_config.xml
  14. 1
      app/src/main/java/com/arialyy/simple/MainActivity.java
  15. 58
      app/src/main/java/com/arialyy/simple/download/SingleTaskActivity.java
  16. 4
      app/src/main/java/com/arialyy/simple/download/multi_download/DownloadAdapter.java
  17. 2
      app/src/main/java/com/arialyy/simple/download/multi_download/MultiTaskActivity.java

@ -26,22 +26,23 @@ import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.util.Log;
import android.widget.PopupWindow;
import com.arialyy.aria.core.download.DownloadReceiver;
import com.arialyy.aria.core.inf.ICmd;
import com.arialyy.aria.core.inf.IReceiver;
import com.arialyy.aria.core.queue.DownloadTaskQueue;
import com.arialyy.aria.core.upload.UploadReceiver;
import com.arialyy.aria.orm.DbUtil;
import com.arialyy.aria.util.CAConfiguration;
import com.arialyy.aria.util.Configuration_1;
import com.arialyy.aria.util.Speed;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.SAXException;
/**
* Created by lyy on 2016/12/1.
@ -57,12 +58,20 @@ import java.util.Map;
private Map<String, IReceiver> mReceivers = new HashMap<>();
public static Context APP;
private List<ICmd> mCommands = new ArrayList<>();
private Configuration mConfig;
private Configuration.DownloadConfig mDConfig;
private Configuration.UploadConfig mUConfig;
private AriaManager(Context context) {
DbUtil.init(context.getApplicationContext());
APP = context.getApplicationContext();
regAppLifeCallback(context);
File dFile = new File(Configuration.DOWNLOAD_CONFIG_FILE);
File uFile = new File(Configuration.UPLOAD_CONFIG_FILE);
if (!dFile.exists() || !uFile.exists()) {
loadConfig();
}
mDConfig = Configuration.DownloadConfig.getInstance();
mUConfig = Configuration.UploadConfig.getInstance();
}
public static AriaManager getInstance(Context context) {
@ -79,10 +88,39 @@ import java.util.Map;
}
/**
* 设置最大下载速度
* 加载配置文件
*/
private void loadConfig() {
try {
ConfigHelper helper = new ConfigHelper();
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser parser = factory.newSAXParser();
parser.parse(APP.getAssets().open("aria_config.xml"), helper);
} catch (ParserConfigurationException | IOException | SAXException e) {
e.printStackTrace();
}
}
/**
* 如果需要在代码中修改下载配置请使用以下方法
*
* @<code> //修改最大任务队列数
* Aria.get(this).getDownloadConfig().setMaxTaskNum(3).save();
* </code>
*/
public Configuration.DownloadConfig getDownloadConfig() {
return mDConfig;
}
/**
* 如果需要在代码中修改下载配置请使用以下方法
*
* @<code> //修改最大任务队列数
* Aria.get(this).getUploadConfig().setMaxTaskNum(3).save();
* </code>
*/
public void setMaxSpeed(Speed speed) {
Configuration_1.getInstance().setMaxSpeed(speed);
public Configuration.UploadConfig getUploadConfig() {
return mUConfig;
}
/**
@ -135,70 +173,6 @@ import java.util.Map;
return (receiver instanceof UploadReceiver) ? (UploadReceiver) receiver : null;
}
/**
* 设置CA证书信息
*
* @param caAlias ca证书别名
* @param caPath assets 文件夹下的ca证书完整路径
*/
public void setCAInfo(String caAlias, String caPath) {
if (TextUtils.isEmpty(caAlias)) {
Log.e(TAG, "ca证书别名不能为null");
return;
} else if (TextUtils.isEmpty(caPath)) {
Log.e(TAG, "ca证书路径不能为null");
return;
}
CAConfiguration.CA_ALIAS = caAlias;
CAConfiguration.CA_PATH = caPath;
}
/**
* 设置下载超时时间
*/
@Deprecated private AriaManager setTimeOut(int timeOut) {
Configuration_1.getInstance().setTimeOut(timeOut);
return this;
}
/**
* 设置失败重试次数
*/
public AriaManager setReTryNum(int reTryNum) {
Configuration_1.getInstance().setReTryNum(reTryNum);
return this;
}
/**
* 设置失败重试间隔
*/
public AriaManager setReTryInterval(int interval) {
Configuration_1.getInstance().setReTryInterval(interval);
return this;
}
/**
* 是否打开下载广播
*/
public AriaManager openBroadcast(boolean open) {
Configuration_1.getInstance().setOpenBroadcast(open);
return this;
}
/**
* 设置最大下载数最大下载数不能小于1
*
* @param maxDownloadNum 最大下载数
*/
public AriaManager setMaxDownloadNum(int maxDownloadNum) {
if (maxDownloadNum < 1) {
Log.w(TAG, "最大任务数不能小于 1");
return this;
}
DownloadTaskQueue.getInstance().setDownloadNum(maxDownloadNum);
return this;
}
private IReceiver putReceiver(boolean isDownload, Object obj) {
final String key = getKey(isDownload, obj);
IReceiver receiver = mReceivers.get(key);

@ -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;
import android.text.TextUtils;
import android.util.Log;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
/**
* Created by Aria.Lao on 2017/5/22.
* 读取配置文件
*/
public class ConfigHelper extends DefaultHandler {
private final String TAG = "ConfigHelper";
private boolean isDownloadConfig = false, isUploadConfig;
private Configuration.DownloadConfig mDownloadConfig = Configuration.DownloadConfig.getInstance();
private Configuration.UploadConfig mUploadConfig = Configuration.UploadConfig.getInstance();
@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);
if (qName.equals("download")) {
isDownloadConfig = true;
isUploadConfig = false;
} else if (qName.equals("upload")) {
isUploadConfig = true;
isDownloadConfig = false;
}
if (isDownloadConfig || isUploadConfig) {
String value = attributes.getValue("value");
switch (qName) {
case "threadNum":
loadThreadNum(value);
break;
case "openBroadcast":
loadBroadcast(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;
}
}
}
private void loadReTryInterval(String value) {
int time = 2 * 1000;
if (!TextUtils.isEmpty(value)) {
time = Integer.parseInt(value);
}
if (time < 2 * 1000) {
time = 2 * 1000;
}
if (isDownloadConfig) {
mDownloadConfig.reTryInterval = time;
}
}
private void loadCA(String name, String path) {
if (isDownloadConfig) {
mDownloadConfig.caName = name;
mDownloadConfig.caPath = path;
}
}
private void loadBuffSize(String value) {
int buffSize = 8192;
if (!TextUtils.isEmpty(value)) {
buffSize = Integer.parseInt(value);
}
if (buffSize < 2048) {
buffSize = 2048;
}
if (isDownloadConfig) {
mDownloadConfig.buffSize = buffSize;
}
}
private void loadIOTimeout(String value) {
int time = 10 * 1000;
if (!TextUtils.isEmpty(value)) {
time = Integer.parseInt(value);
}
if (time < 10 * 1000) {
time = 10 * 1000;
}
if (isDownloadConfig) {
mDownloadConfig.iOTimeOut = time;
}
}
private void loadConnectTime(String value) {
int time = 5 * 1000;
if (!TextUtils.isEmpty(value)) {
time = Integer.parseInt(value);
}
if (isDownloadConfig) {
mDownloadConfig.connectTimeOut = time;
}
if (isUploadConfig) {
mUploadConfig.connectTimeOut = time;
}
}
private void loadReTry(String value) {
int num = 0;
if (!TextUtils.isEmpty(value)) {
num = Integer.parseInt(value);
}
if (isDownloadConfig) {
mDownloadConfig.reTryNum = num;
}
if (isUploadConfig) {
mUploadConfig.reTryNum = num;
}
}
private void loadMaxQueue(String value) {
int num = 2;
if (!TextUtils.isEmpty(value)) {
num = Integer.parseInt(value);
}
if (num < 1) {
Log.e(TAG, "任务队列数不能小于 1");
num = 2;
}
if (isDownloadConfig) {
mDownloadConfig.maxTaskNum = num;
}
if (isUploadConfig) {
mUploadConfig.maxTaskNum = num;
}
}
private void loadBroadcast(String value) {
boolean open = Boolean.parseBoolean(value);
if (isDownloadConfig) {
mDownloadConfig.isOpenBreadCast = open;
}
if (isUploadConfig) {
mUploadConfig.isOpenBreadCast = open;
}
}
private void loadThreadNum(String value) {
int num = 3;
if (!TextUtils.isEmpty(value)) {
num = Integer.parseInt(value);
}
if (num < 1) {
Log.e(TAG, "下载线程数不能小于 1");
num = 3;
}
if (isDownloadConfig) {
mDownloadConfig.threadNum = num;
}
}
@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();
}
}

@ -15,36 +15,157 @@
*/
package com.arialyy.aria.core;
import com.arialyy.aria.util.CommonUtil;
import com.arialyy.aria.util.ReflectionUtil;
import java.io.File;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.List;
import java.util.Properties;
/**
* Created by AriaL on 2016/12/8.
* 信息配置
*/
class Configuration {
private static final String TAG = "Configuration";
private static final String CONFIG_FILE = "/Aria/ADConfig.properties";
private static final String CONFIG_KEY = "ARIA_CONFIG";
static final String DOWNLOAD_CONFIG_FILE = "/Aria/DownloadConfig.properties";
static final String UPLOAD_CONFIG_FILE = "/Aria/UploadConfig.properties";
/**
* 通用配置
*/
static class BaseConfig {
public boolean isOpenBreadCast = false;
public static class BaseConfig {
/**
* 是否发送任务广播true发送
*/
boolean isOpenBreadCast = false;
/**
* 任务队列最大任务数 默认为2
*/
public int maxQueueNum = 2;
int maxTaskNum = 2;
/**
* 下载失败重试次数默认为10
*/
public int reTryNum = 10;
int reTryNum = 10;
/**
* 设置重试间隔单位为毫秒默认2000毫秒
*/
public long reTryInterval = 2000;
long reTryInterval = 2000;
/**
* 设置url连接超时时间单位为毫秒默认5000毫秒
*/
public long connectTimeOut = 5000;
long connectTimeOut = 5000;
public boolean isOpenBreadCast() {
return isOpenBreadCast;
}
public BaseConfig setOpenBreadCast(boolean openBreadCast) {
isOpenBreadCast = openBreadCast;
return this;
}
public int getMaxTaskNum() {
return maxTaskNum;
}
public BaseConfig setMaxTaskNum(int maxTaskNum) {
this.maxTaskNum = maxTaskNum;
return this;
}
public int getReTryNum() {
return reTryNum;
}
public BaseConfig setReTryNum(int reTryNum) {
this.reTryNum = reTryNum;
return this;
}
public long getReTryInterval() {
return reTryInterval;
}
public BaseConfig setReTryInterval(long reTryInterval) {
this.reTryInterval = reTryInterval;
return this;
}
public long getConnectTimeOut() {
return connectTimeOut;
}
public BaseConfig setConnectTimeOut(long connectTimeOut) {
this.connectTimeOut = connectTimeOut;
return this;
}
/**
* 加载配置
*/
void loadConfig() {
boolean isDownload = this instanceof DownloadConfig;
File file = new File(
AriaManager.APP.getFilesDir().getPath() + (isDownload ? DOWNLOAD_CONFIG_FILE
: UPLOAD_CONFIG_FILE));
if (file.exists()) {
Properties properties = CommonUtil.loadConfig(file);
List<Field> fields = ReflectionUtil.getAllFields(getClass());
try {
for (Field field : fields) {
int m = field.getModifiers();
if (Modifier.isFinal(m) || Modifier.isStatic(m)) {
continue;
}
field.setAccessible(true);
String value = properties.getProperty(field.getName());
Class<?> type = field.getType();
if (type == String.class) {
field.set(this, value);
} else if (type == int.class || type == Integer.class) {
field.setInt(this, Integer.parseInt(value));
} else if (type == float.class || type == Float.class) {
field.setFloat(this, Float.parseFloat(value));
} else if (type == double.class || type == Double.class) {
field.setDouble(this, Double.parseDouble(value));
} else if (type == long.class || type == Long.class) {
field.setLong(this, Long.parseLong(value));
} else if (type == boolean.class || type == Boolean.class) {
field.setBoolean(this, Boolean.parseBoolean(value));
}
}
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
/**
* 保存配置
*/
public void save() {
List<Field> fields = ReflectionUtil.getAllFields(getClass());
boolean isDownload = this instanceof DownloadConfig;
try {
File file = new File(
AriaManager.APP.getFilesDir().getPath() + (isDownload ? DOWNLOAD_CONFIG_FILE
: UPLOAD_CONFIG_FILE));
Properties properties = CommonUtil.loadConfig(file);
for (Field field : fields) {
int m = field.getModifiers();
if (Modifier.isFinal(m) || Modifier.isStatic(m)) {
continue;
}
field.setAccessible(true);
properties.setProperty(field.getName(), field.get(this) + "");
}
CommonUtil.saveConfig(file, properties);
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
/**
@ -54,19 +175,78 @@ class Configuration {
/**
* 设置IO流读取时间单位为毫秒默认20000毫秒该时间不能少于10000毫秒
*/
public long readTimeout = 20 * 1000;
long iOTimeOut = 20 * 1000;
/**
* 设置写文件buff大小该数值大小不能小于2048数值变小下载速度会变慢
*/
public int buffSize = 8192;
int buffSize = 8192;
/**
* 设置https ca 证书信息path 为assets目录下的CA证书完整路径
*/
public String caPath;
String caPath;
/**
* name 为CA证书名
*/
public String caName;
String caName;
/**
* 下载线程数下载线程数不能小于1
*/
int threadNum = 3;
public long getiOTimeOut() {
return iOTimeOut;
}
public DownloadConfig setiOTimeOut(long iOTimeOut) {
this.iOTimeOut = iOTimeOut;
return this;
}
public int getBuffSize() {
return buffSize;
}
public DownloadConfig setBuffSize(int buffSize) {
this.buffSize = buffSize;
return this;
}
public String getCaPath() {
return caPath;
}
public DownloadConfig setCaPath(String caPath) {
this.caPath = caPath;
return this;
}
public String getCaName() {
return caName;
}
public DownloadConfig setCaName(String caName) {
this.caName = caName;
return this;
}
public int getThreadNum() {
return threadNum;
}
private DownloadConfig() {
loadConfig();
}
private static DownloadConfig INSTANCE = null;
static DownloadConfig getInstance() {
if (INSTANCE == null) {
synchronized (DownloadConfig.class) {
INSTANCE = new DownloadConfig();
}
}
return INSTANCE;
}
}
/**
@ -74,5 +254,19 @@ class Configuration {
*/
public static class UploadConfig extends BaseConfig {
private UploadConfig() {
loadConfig();
}
private static UploadConfig INSTANCE = null;
static UploadConfig getInstance() {
if (INSTANCE == null) {
synchronized (DownloadConfig.class) {
INSTANCE = new UploadConfig();
}
}
return INSTANCE;
}
}
}

@ -27,7 +27,6 @@ import com.arialyy.aria.core.scheduler.DownloadSchedulers;
import com.arialyy.aria.core.scheduler.ISchedulers;
import com.arialyy.aria.util.CheckUtil;
import com.arialyy.aria.util.CommonUtil;
import com.arialyy.aria.util.Configuration_1;
import java.lang.ref.WeakReference;
/**
@ -178,22 +177,6 @@ public class DownloadTask implements ITask {
intent.putExtra(Aria.ENTITY, mEntity);
mContext.sendBroadcast(intent);
}
//if (mEntity.isDownloadComplete()) {
// //mUtil.cancelDownload();
//} else {
// // 如果任务不是下载状态
// mUtil.cancelDownload();
// mUtil.delConfigFile();
// mUtil.delTempFile();
// mEntity.deleteData();
// if (mOutHandler != null) {
// mOutHandler.obtainMessage(DownloadSchedulers.CANCEL, this).sendToTarget();
// }
// //发送取消下载的广播
// Intent intent = CommonUtil.createIntent(mContext.getPackageName(), Aria.ACTION_CANCEL);
// intent.putExtra(Aria.ENTITY, mEntity);
// mContext.sendBroadcast(intent);
//}
}
public static class Builder {
@ -203,7 +186,6 @@ public class DownloadTask implements ITask {
String targetName;
public Builder(String targetName, DownloadTaskEntity taskEntity) {
//CheckUtil.checkDownloadTaskEntity(taskEntity.downloadEntity);
CheckUtil.checkTaskEntity(taskEntity);
this.targetName = targetName;
this.taskEntity = taskEntity;
@ -250,6 +232,7 @@ public class DownloadTask implements ITask {
boolean isFirst = true;
DownloadEntity downloadEntity;
DownloadTask task;
boolean isOpenBroadCast = false;
DListener(Context context, DownloadTask task, Handler outHandler) {
this.context = context;
@ -259,6 +242,7 @@ public class DownloadTask implements ITask {
this.downloadEntity = this.task.getDownloadEntity();
sendIntent = CommonUtil.createIntent(context.getPackageName(), Aria.ACTION_RUNNING);
sendIntent.putExtra(Aria.ENTITY, downloadEntity);
isOpenBroadCast = AriaManager.getInstance(context).getDownloadConfig().isOpenBreadCast();
}
@Override public void supportBreakpoint(boolean support) {
@ -366,7 +350,7 @@ public class DownloadTask implements ITask {
downloadEntity.setDownloadComplete(action.equals(Aria.ACTION_COMPLETE));
downloadEntity.setCurrentProgress(location);
downloadEntity.update();
if (!Configuration_1.isOpenBreadCast) return;
if (!isOpenBroadCast) return;
Intent intent = CommonUtil.createIntent(context.getPackageName(), action);
intent.putExtra(Aria.ENTITY, downloadEntity);
if (location != -1) {

@ -28,5 +28,4 @@ import com.arialyy.aria.core.queue.pool.ExecutePool;
abstract class AbsTaskQueue<TASK extends ITask, TASK_ENTITY extends ITaskEntity, ENTITY extends IEntity>
implements ITaskQueue<TASK, TASK_ENTITY, ENTITY> {
CachePool<TASK> mCachePool = new CachePool<>();
ExecutePool<TASK> mExecutePool = new ExecutePool<>();
}

@ -18,6 +18,7 @@ package com.arialyy.aria.core.queue;
import android.text.TextUtils;
import android.util.Log;
import com.arialyy.aria.core.AriaManager;
import com.arialyy.aria.core.download.DownloadEntity;
import com.arialyy.aria.core.download.DownloadTask;
import com.arialyy.aria.core.download.DownloadTaskEntity;
@ -25,7 +26,6 @@ import com.arialyy.aria.core.inf.IEntity;
import com.arialyy.aria.core.queue.pool.CachePool;
import com.arialyy.aria.core.queue.pool.ExecutePool;
import com.arialyy.aria.core.scheduler.DownloadSchedulers;
import com.arialyy.aria.util.Configuration_1;
/**
* Created by lyy on 2016/8/17.
@ -36,6 +36,7 @@ public class DownloadTaskQueue
private static final String TAG = "DownloadTaskQueue";
private static volatile DownloadTaskQueue INSTANCE = null;
private static final Object LOCK = new Object();
private ExecutePool<DownloadTask> mExecutePool = new ExecutePool<>(true);
public static DownloadTaskQueue getInstance() {
if (INSTANCE == null) {
@ -121,7 +122,7 @@ public class DownloadTaskQueue
@Override public void setDownloadNum(int downloadNum) {
//原始长度
int size = Configuration_1.getInstance().getDownloadNum();
int size = AriaManager.getInstance(AriaManager.APP).getUploadConfig().getMaxTaskNum();
int diff = downloadNum - size;
if (size == downloadNum) {
Log.d(TAG, "设置的下载任务数和配置文件的下载任务数一直,跳过");

@ -18,6 +18,8 @@ package com.arialyy.aria.core.queue;
import android.text.TextUtils;
import android.util.Log;
import com.arialyy.aria.core.download.DownloadTask;
import com.arialyy.aria.core.queue.pool.ExecutePool;
import com.arialyy.aria.core.scheduler.UploadSchedulers;
import com.arialyy.aria.core.upload.UploadEntity;
import com.arialyy.aria.core.upload.UploadTask;
@ -31,6 +33,7 @@ public class UploadTaskQueue extends AbsTaskQueue<UploadTask, UploadTaskEntity,
private static final String TAG = "UploadTaskQueue";
private static volatile UploadTaskQueue INSTANCE = null;
private static final Object LOCK = new Object();
private ExecutePool<UploadTask> mExecutePool = new ExecutePool<>(false);
public static UploadTaskQueue getInstance() {
if (INSTANCE == null) {

@ -18,9 +18,9 @@ package com.arialyy.aria.core.queue.pool;
import android.text.TextUtils;
import android.util.Log;
import com.arialyy.aria.core.AriaManager;
import com.arialyy.aria.core.inf.ITask;
import com.arialyy.aria.util.CommonUtil;
import com.arialyy.aria.util.Configuration_1;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ArrayBlockingQueue;
@ -37,9 +37,15 @@ public class ExecutePool<TASK extends ITask> implements IPool<TASK> {
private ArrayBlockingQueue<TASK> mExecuteQueue;
private Map<String, TASK> mExecuteArray;
private int mSize;
private boolean isDownload;
public ExecutePool() {
mSize = Configuration_1.getInstance().getDownloadNum();
public ExecutePool(boolean isDownload) {
this.isDownload = isDownload;
if (isDownload) {
mSize = AriaManager.getInstance(AriaManager.APP).getDownloadConfig().getMaxTaskNum();
} else {
mSize = AriaManager.getInstance(AriaManager.APP).getUploadConfig().getMaxTaskNum();
}
mExecuteQueue = new ArrayBlockingQueue<>(mSize);
mExecuteArray = new HashMap<>();
}
@ -81,7 +87,11 @@ public class ExecutePool<TASK extends ITask> implements IPool<TASK> {
}
mExecuteQueue = temp;
mSize = downloadNum;
Configuration_1.getInstance().setDownloadNum(mSize);
if (isDownload) {
AriaManager.getInstance(AriaManager.APP).getDownloadConfig().setMaxTaskNum(downloadNum);
} else {
AriaManager.getInstance(AriaManager.APP).getUploadConfig().setMaxTaskNum(downloadNum);
}
} catch (InterruptedException e) {
e.printStackTrace();
}

@ -19,10 +19,10 @@ package com.arialyy.aria.core.scheduler;
import android.os.CountDownTimer;
import android.os.Message;
import android.util.Log;
import com.arialyy.aria.core.AriaManager;
import com.arialyy.aria.core.queue.DownloadTaskQueue;
import com.arialyy.aria.core.download.DownloadEntity;
import com.arialyy.aria.core.download.DownloadTask;
import com.arialyy.aria.util.Configuration_1;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
@ -86,7 +86,9 @@ public class DownloadSchedulers implements ISchedulers<DownloadTask> {
case STOP:
case CANCEL:
mQueue.removeTask(entity);
if (mQueue.size() < Configuration_1.getInstance().getDownloadNum()) {
if (mQueue.size() < AriaManager.getInstance(AriaManager.APP)
.getUploadConfig()
.getMaxTaskNum()) {
startNextTask();
}
break;
@ -163,19 +165,22 @@ public class DownloadSchedulers implements ISchedulers<DownloadTask> {
* @param task 下载任务
*/
private void handleFailTask(final DownloadTask task) {
final Configuration_1 config = Configuration_1.getInstance();
CountDownTimer timer = new CountDownTimer(config.getReTryInterval(), 1000) {
final long interval =
AriaManager.getInstance(AriaManager.APP).getUploadConfig().getReTryInterval();
final int reTryNum = AriaManager.getInstance(AriaManager.APP).getUploadConfig().getReTryNum();
CountDownTimer timer = new CountDownTimer(interval, 1000) {
@Override public void onTick(long millisUntilFinished) {
}
@Override public void onFinish() {
DownloadEntity entity = task.getDownloadEntity();
if (entity.getFailNum() < config.getReTryNum()) {
if (entity.getFailNum() < reTryNum) {
DownloadTask task = mQueue.getTask(entity);
mQueue.reTryStart(task);
try {
Thread.sleep(config.getReTryInterval());
Thread.sleep(interval);
} catch (InterruptedException e) {
e.printStackTrace();
}

@ -18,11 +18,11 @@ package com.arialyy.aria.core.scheduler;
import android.os.CountDownTimer;
import android.os.Message;
import android.util.Log;
import com.arialyy.aria.core.AriaManager;
import com.arialyy.aria.core.inf.IEntity;
import com.arialyy.aria.core.queue.UploadTaskQueue;
import com.arialyy.aria.core.upload.UploadEntity;
import com.arialyy.aria.core.upload.UploadTask;
import com.arialyy.aria.util.Configuration_1;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
@ -69,19 +69,21 @@ public class UploadSchedulers implements ISchedulers<UploadTask> {
}
private void handleFailTask(final UploadTask task) {
final Configuration_1 config = Configuration_1.getInstance();
CountDownTimer timer = new CountDownTimer(config.getReTryInterval(), 1000) {
final long interval =
AriaManager.getInstance(AriaManager.APP).getUploadConfig().getReTryInterval();
final int reTryNum = AriaManager.getInstance(AriaManager.APP).getUploadConfig().getReTryNum();
CountDownTimer timer = new CountDownTimer(interval, 1000) {
@Override public void onTick(long millisUntilFinished) {
}
@Override public void onFinish() {
UploadEntity entity = task.getUploadEntity();
if (entity.getFailNum() <= config.getReTryNum()) {
if (entity.getFailNum() <= reTryNum) {
UploadTask task = mQueue.getTask(entity);
mQueue.reTryStart(task);
try {
Thread.sleep(config.getReTryInterval());
Thread.sleep(interval);
} catch (InterruptedException e) {
e.printStackTrace();
}
@ -169,7 +171,9 @@ public class UploadSchedulers implements ISchedulers<UploadTask> {
case STOP:
case CANCEL:
mQueue.removeTask(entity);
if (mQueue.size() < Configuration_1.getInstance().getDownloadNum()) {
if (mQueue.size() < AriaManager.getInstance(AriaManager.APP)
.getUploadConfig()
.getMaxTaskNum()) {
startNextTask();
}
break;

@ -15,6 +15,7 @@
*/
package com.arialyy.aria.core.upload;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.util.Log;
@ -26,7 +27,6 @@ import com.arialyy.aria.core.inf.ITask;
import com.arialyy.aria.core.scheduler.DownloadSchedulers;
import com.arialyy.aria.core.scheduler.ISchedulers;
import com.arialyy.aria.util.CommonUtil;
import com.arialyy.aria.util.Configuration_1;
import java.lang.ref.WeakReference;
/**
@ -104,21 +104,6 @@ public class UploadTask implements ITask {
intent.putExtra(Aria.ENTITY, mUploadEntity);
AriaManager.APP.sendBroadcast(intent);
}
//if (mUtil.isRunning()) {
// mUtil.cancel();
//} else {
// // 如果任务不是下载状态
// mUtil.cancel();
// mUploadEntity.deleteData();
// if (mOutHandler != null) {
// mOutHandler.obtainMessage(DownloadSchedulers.CANCEL, this).sendToTarget();
// }
// //发送取消下载的广播
// Intent intent = CommonUtil.createIntent(AriaManager.APP.getPackageName(), Aria.ACTION_CANCEL);
// intent.putExtra(Aria.ENTITY, mUploadEntity);
// AriaManager.APP.sendBroadcast(intent);
//}
}
public String getTargetName() {
@ -146,6 +131,8 @@ public class UploadTask implements ITask {
boolean isFirst = true;
UploadEntity entity;
Intent sendIntent;
boolean isOpenBroadCast = false;
Context context;
UListener(Handler outHandle, UploadTask task) {
this.outHandler = new WeakReference<>(outHandle);
@ -153,6 +140,8 @@ public class UploadTask implements ITask {
entity = this.task.get().getUploadEntity();
sendIntent = CommonUtil.createIntent(AriaManager.APP.getPackageName(), Aria.ACTION_RUNNING);
sendIntent.putExtra(Aria.ENTITY, entity);
context = AriaManager.APP;
isOpenBroadCast = AriaManager.getInstance(context).getUploadConfig().isOpenBreadCast();
}
@Override public void onPre() {
@ -238,13 +227,13 @@ public class UploadTask implements ITask {
entity.setComplete(action.equals(Aria.ACTION_COMPLETE));
entity.setCurrentProgress(location);
entity.update();
if (!Configuration_1.isOpenBreadCast) return;
Intent intent = CommonUtil.createIntent(AriaManager.APP.getPackageName(), action);
if (!isOpenBroadCast) return;
Intent intent = CommonUtil.createIntent(context.getPackageName(), action);
intent.putExtra(Aria.ENTITY, entity);
if (location != -1) {
intent.putExtra(Aria.CURRENT_LOCATION, location);
}
AriaManager.APP.sendBroadcast(intent);
context.sendBroadcast(intent);
}
}

@ -0,0 +1,113 @@
/*
* 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.util;
import android.util.Log;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
* Created by AriaLyy@outlook.com on 2015/7/30.
* 反射工具类
*/
public class ReflectionUtil {
private static final String TAG = "ReflectionUtil";
/**
* 获取类里面的所在字段
*/
public static Field[] getFields(Class clazz) {
Field[] fields = null;
fields = clazz.getDeclaredFields();
if (fields == null || fields.length == 0) {
Class superClazz = clazz.getSuperclass();
if (superClazz != null) {
fields = getFields(superClazz);
}
}
return fields;
}
/**
* 获取所有字段包括父类的字段
*/
public static List<Field> getAllFields(Class clazz) {
List<Field> fields = new ArrayList<>();
Class personClazz = clazz.getSuperclass();
if (personClazz != null) {
Collections.addAll(fields, personClazz.getDeclaredFields());
}
Collections.addAll(fields, clazz.getDeclaredFields());
return fields;
}
/**
* 获取类里面的指定对象如果该类没有则从父类查询
*/
public static Field getField(Class clazz, String name) {
Field field = null;
try {
field = clazz.getDeclaredField(name);
} catch (NoSuchFieldException e) {
try {
field = clazz.getField(name);
} catch (NoSuchFieldException e1) {
if (clazz.getSuperclass() == null) {
return field;
} else {
field = getField(clazz.getSuperclass(), name);
}
}
}
if (field != null) {
field.setAccessible(true);
}
return field;
}
/**
* 利用递归找一个类的指定方法如果找不到去父亲里面找直到最上层Object对象为止
*
* @param clazz 目标类
* @param methodName 方法名
* @param params 方法参数类型数组
* @return 方法对象
*/
public static Method getMethod(Class clazz, String methodName, final Class<?>... params) {
Method method = null;
try {
method = clazz.getDeclaredMethod(methodName, params);
} catch (NoSuchMethodException e) {
try {
method = clazz.getMethod(methodName, params);
} catch (NoSuchMethodException ex) {
if (clazz.getSuperclass() == null) {
Log.e(TAG, "无法找到" + methodName + "方法");
return method;
} else {
method = getMethod(clazz.getSuperclass(), methodName, params);
}
}
}
if (method != null) {
method.setAccessible(true);
}
return method;
}
}

@ -3,11 +3,15 @@
<download>
<!--设置下载线程-->
<threadNum value="3"/>
<!--是否打开下载广播,默认为false-->
<openBroadcast value="false"/>
<!--设置下载队列最大任务数, 默认为2-->
<maxQueueNum value="2"/>
<maxTaskNum value="2"/>
<!--设置下载失败,重试次数,默认为10-->
<reTryNum value="10"/>
@ -15,8 +19,11 @@
<!--设置重试间隔,单位为毫秒,默认2000毫秒-->
<reTryInterval value="2000"/>
<!--设置url连接超时时间,单位为毫秒,默认5000毫秒-->
<connectTimeOut value="5000"/>
<!--设置IO流读取时间,单位为毫秒,默认20000毫秒,该时间不能少于10000毫秒-->
<readTimeout value="20000"/>
<iOTimeOut value="20000"/>
<!--设置写文件buff大小,该数值大小不能小于2048,数值变小,下载速度会变慢-->
<buffSize value="8192"/>
@ -31,7 +38,7 @@
<openBroadcast value="false"/>
<!--设置上传队列最大任务数, 默认为2-->
<maxQueueNum value="2"/>
<maxTaskNum value="2"/>
<!--设置上传失败,重试次数,默认为10-->
<reTryNum value="10"/>

@ -22,6 +22,7 @@ import android.support.v7.widget.Toolbar;
import android.view.View;
import butterknife.Bind;
import butterknife.OnClick;
import com.arialyy.aria.core.Aria;
import com.arialyy.simple.base.BaseActivity;
import com.arialyy.simple.databinding.ActivityMainBinding;
import com.arialyy.simple.download.DownloadActivity;

@ -56,7 +56,7 @@ public class SingleTaskActivity extends BaseActivity<ActivitySingleBinding> {
//"http://kotlinlang.org/docs/kotlin-docs.pdf";
//"https://atom-installer.github.com/v1.13.0/AtomSetup.exe?s=1484074138&ext=.exe";
"http://static.gaoshouyou.com/d/22/94/822260b849944492caadd2983f9bb624.apk";
//"http://static.gaoshouyou.com/d/36/69/2d3699acfa69e9632262442c46516ad8.apk";
//"http://static.gaoshouyou.com/d/36/69/2d3699acfa69e9632262442c46516ad8.apk";
//不支持断点的链接
//"http://ox.konsung.net:5555/ksdc-web/download/downloadFile/?fileName=ksdc_1.0.2.apk&rRange=0-";
//"http://172.18.104.50:8080/download/_302turn";
@ -164,7 +164,7 @@ public class SingleTaskActivity extends BaseActivity<ActivitySingleBinding> {
setSupportActionBar(toolbar);
toolbar.setTitle("单任务下载");
init();
Aria.get(this).openBroadcast(true);
Aria.get(this).getDownloadConfig().setOpenBreadCast(true);
}
private void init() {
@ -173,33 +173,33 @@ public class SingleTaskActivity extends BaseActivity<ActivitySingleBinding> {
int p = (int) (target.getCurrentProgress() * 100 / target.getFileSize());
mPb.setProgress(p);
}
mRg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId) {
case 1:
Aria.get(this).setMaxSpeed(Speed.KB_256);
break;
case 2:
Aria.get(this).setMaxSpeed(Speed.KB_512);
break;
case 3:
Aria.get(this).setMaxSpeed(Speed.MB_1);
break;
case 4:
Aria.get(this).setMaxSpeed(Speed.MB_2);
break;
case 5:
Aria.get(this).setMaxSpeed(Speed.MAX);
break;
}
stop();
new Handler().postDelayed(new Runnable() {
@Override public void run() {
start();
}
}, 2000);
}
});
//mRg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
// @Override public void onCheckedChanged(RadioGroup group, int checkedId) {
// switch (checkedId) {
// case 1:
// Aria.get(this).setMaxSpeed(Speed.KB_256);
// break;
// case 2:
// Aria.get(this).setMaxSpeed(Speed.KB_512);
// break;
// case 3:
// Aria.get(this).setMaxSpeed(Speed.MB_1);
// break;
// case 4:
// Aria.get(this).setMaxSpeed(Speed.MB_2);
// break;
// case 5:
// Aria.get(this).setMaxSpeed(Speed.MAX);
// break;
// }
// stop();
// new Handler().postDelayed(new Runnable() {
// @Override public void run() {
// start();
// }
// }, 2000);
// }
//});
}
public void onClick(View view) {

@ -147,10 +147,6 @@ final class DownloadAdapter extends AbsRVAdapter<DownloadEntity, DownloadAdapter
});
}
public void setDownloadNum(int num) {
Aria.get(getContext()).setMaxDownloadNum(num);
}
private String covertCurrentSize(long currentSize) {
String size = CommonUtil.formatFileSize(currentSize);
return size.substring(0, size.length() - 1);

@ -83,7 +83,7 @@ public class MultiTaskActivity extends BaseActivity<ActivityMultiBinding> {
@Override protected void dataCallback(int result, Object data) {
super.dataCallback(result, data);
if (result == DownloadNumDialog.RESULT_CODE) {
Aria.get(this).setMaxDownloadNum(Integer.parseInt(data + ""));
Aria.get(this).getDownloadConfig().setMaxTaskNum(Integer.parseInt(data + ""));
}
}

Loading…
Cancel
Save