下载配置设置
This commit is contained in:
@ -8,6 +8,7 @@ import android.content.Context;
|
||||
import android.os.Build;
|
||||
import android.os.Bundle;
|
||||
import android.support.v4.app.Fragment;
|
||||
import android.util.Log;
|
||||
import com.arialyy.aria.core.command.CmdFactory;
|
||||
import com.arialyy.aria.util.CommonUtil;
|
||||
import com.arialyy.aria.core.command.IDownloadCmd;
|
||||
@ -22,6 +23,7 @@ import java.util.Set;
|
||||
* Aria管理器,任务操作在这里执行
|
||||
*/
|
||||
@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH) public class AriaManager {
|
||||
private static final String TAG = "AriaManager";
|
||||
private static final Object LOCK = new Object();
|
||||
private static volatile AriaManager INSTANCE = null;
|
||||
private Map<String, AMReceiver> mTargets = new HashMap<>();
|
||||
@ -46,16 +48,6 @@ import java.util.Set;
|
||||
return getTarget(obj);
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置同时下载的任务数
|
||||
*/
|
||||
public void setDownloadNum(int num) {
|
||||
if (num <= 0) {
|
||||
throw new IllegalArgumentException("下载任务数不能小于1");
|
||||
}
|
||||
mManager.getTaskQueue().setDownloadNum(num);
|
||||
}
|
||||
|
||||
/**
|
||||
* 停止所有正在执行的任务
|
||||
*/
|
||||
@ -70,6 +62,26 @@ import java.util.Set;
|
||||
mManager.setCmds(stopCmds).exe();
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否打开下载广播
|
||||
*/
|
||||
public void openBroadcast(boolean open) {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置最大下载数,最大下载数不能小于1
|
||||
*
|
||||
* @param maxDownloadNum 最大下载数
|
||||
*/
|
||||
public void setMaxDownloadNum(int maxDownloadNum) {
|
||||
if (maxDownloadNum < 1){
|
||||
Log.w(TAG, "最大任务数不能小于 1");
|
||||
return;
|
||||
}
|
||||
mManager.getTaskQueue().setDownloadNum(maxDownloadNum);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除所有任务
|
||||
*/
|
||||
|
@ -22,6 +22,7 @@ import com.arialyy.aria.orm.DbUtil;
|
||||
import com.arialyy.aria.core.command.IDownloadCmd;
|
||||
import com.arialyy.aria.core.queue.DownloadTaskQueue;
|
||||
import com.arialyy.aria.orm.DbEntity;
|
||||
import com.arialyy.aria.util.Configuration;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@ -82,21 +83,22 @@ public class DownloadManager {
|
||||
private static final Object LOCK = new Object();
|
||||
private static volatile DownloadManager INSTANCE = null;
|
||||
private List<IDownloadCmd> mCommands = new ArrayList<>();
|
||||
private Context mContext;
|
||||
public static Context APP;
|
||||
private ITaskQueue mTaskQueue;
|
||||
private static Configuration mConfig;
|
||||
|
||||
private DownloadManager() {
|
||||
|
||||
}
|
||||
|
||||
private DownloadManager(Context context) {
|
||||
mContext = context;
|
||||
APP = context;
|
||||
DownloadTaskQueue.Builder builder = new DownloadTaskQueue.Builder(context);
|
||||
mTaskQueue = builder.build();
|
||||
DbUtil.init(context);
|
||||
}
|
||||
|
||||
public static DownloadManager init(Context context) {
|
||||
static DownloadManager init(Context context) {
|
||||
if (INSTANCE == null) {
|
||||
synchronized (LOCK) {
|
||||
INSTANCE = new DownloadManager(context.getApplicationContext());
|
||||
@ -112,7 +114,7 @@ public class DownloadManager {
|
||||
return INSTANCE;
|
||||
}
|
||||
|
||||
public List<DownloadEntity> getAllDownloadEntity() {
|
||||
List<DownloadEntity> getAllDownloadEntity() {
|
||||
return DbEntity.findAllData(DownloadEntity.class);
|
||||
}
|
||||
|
||||
@ -126,7 +128,7 @@ public class DownloadManager {
|
||||
/**
|
||||
* 设置命令
|
||||
*/
|
||||
public DownloadManager setCmd(IDownloadCmd command) {
|
||||
DownloadManager setCmd(IDownloadCmd command) {
|
||||
mCommands.add(command);
|
||||
return this;
|
||||
}
|
||||
@ -134,7 +136,7 @@ public class DownloadManager {
|
||||
/**
|
||||
* 设置一组命令
|
||||
*/
|
||||
public DownloadManager setCmds(List<IDownloadCmd> commands) {
|
||||
DownloadManager setCmds(List<IDownloadCmd> commands) {
|
||||
if (commands != null && commands.size() > 0) {
|
||||
mCommands.addAll(commands);
|
||||
}
|
||||
@ -144,7 +146,7 @@ public class DownloadManager {
|
||||
/**
|
||||
* 执行所有设置的命令
|
||||
*/
|
||||
public synchronized void exe() {
|
||||
synchronized void exe() {
|
||||
for (IDownloadCmd command : mCommands) {
|
||||
command.executeCmd();
|
||||
}
|
||||
|
@ -36,7 +36,6 @@ public class DownloadTaskQueue implements ITaskQueue {
|
||||
private ExecutePool mExecutePool = ExecutePool.getInstance();
|
||||
private Context mContext;
|
||||
private IDownloadSchedulers mSchedulers;
|
||||
private int mDownloadNum = 2;
|
||||
|
||||
private DownloadTaskQueue() {
|
||||
}
|
||||
|
@ -14,7 +14,6 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
|
||||
package com.arialyy.aria.core.queue.pool;
|
||||
|
||||
import android.text.TextUtils;
|
||||
@ -22,6 +21,7 @@ import android.util.Log;
|
||||
import com.arialyy.aria.util.CommonUtil;
|
||||
import com.arialyy.aria.core.queue.IPool;
|
||||
import com.arialyy.aria.core.task.Task;
|
||||
import com.arialyy.aria.util.Configuration;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ArrayBlockingQueue;
|
||||
@ -36,11 +36,12 @@ public class ExecutePool implements IPool {
|
||||
private static final Object LOCK = new Object();
|
||||
private static final long TIME_OUT = 1000;
|
||||
private static volatile ExecutePool INSTANCE = null;
|
||||
public static int mSize = 2;
|
||||
private ArrayBlockingQueue<Task> mExecuteQueue;
|
||||
private Map<String, Task> mExecuteArray;
|
||||
private int mSize;
|
||||
|
||||
private ExecutePool() {
|
||||
mSize = Configuration.getInstance().getDownloadNum();
|
||||
mExecuteQueue = new ArrayBlockingQueue<>(mSize);
|
||||
mExecuteArray = new HashMap<>();
|
||||
}
|
||||
@ -91,6 +92,7 @@ public class ExecutePool implements IPool {
|
||||
}
|
||||
mExecuteQueue = temp;
|
||||
mSize = downloadNum;
|
||||
Configuration.getInstance().setDownloadNum(mSize);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
@ -24,6 +24,7 @@ import com.arialyy.aria.core.DownloadEntity;
|
||||
import com.arialyy.aria.core.task.Task;
|
||||
import com.arialyy.aria.core.queue.pool.ExecutePool;
|
||||
import com.arialyy.aria.core.queue.DownloadTaskQueue;
|
||||
import com.arialyy.aria.util.Configuration;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
@ -67,10 +68,6 @@ public class DownloadSchedulers implements IDownloadSchedulers {
|
||||
private static final String TAG = "DownloadSchedulers";
|
||||
private static final Object LOCK = new Object();
|
||||
private static volatile DownloadSchedulers INSTANCE = null;
|
||||
/**
|
||||
* 下载失败次数
|
||||
*/
|
||||
int mFailNum = 10;
|
||||
|
||||
/**
|
||||
* 超时时间
|
||||
@ -108,7 +105,7 @@ public class DownloadSchedulers implements IDownloadSchedulers {
|
||||
case STOP:
|
||||
case CANCEL:
|
||||
mQueue.removeTask(entity);
|
||||
if (mQueue.size() != ExecutePool.mSize) {
|
||||
if (mQueue.size() != Configuration.getInstance().getDownloadNum()) {
|
||||
startNextTask(entity);
|
||||
}
|
||||
break;
|
||||
@ -178,11 +175,12 @@ public class DownloadSchedulers implements IDownloadSchedulers {
|
||||
* @param entity 失败实体
|
||||
*/
|
||||
@Override public void handleFailTask(DownloadEntity entity) {
|
||||
if (entity.getFailNum() <= mFailNum) {
|
||||
final Configuration config = Configuration.getInstance();
|
||||
if (entity.getFailNum() <= config.getReTryNum()) {
|
||||
Task task = mQueue.getTask(entity);
|
||||
mQueue.reTryStart(task);
|
||||
try {
|
||||
Thread.currentThread().sleep(4000);
|
||||
Thread.currentThread().sleep(config.getReTryInterval());
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
@ -227,12 +225,4 @@ public class DownloadSchedulers implements IDownloadSchedulers {
|
||||
}
|
||||
mSchedulerListeners.remove(target.getClass().getName());
|
||||
}
|
||||
|
||||
public void setFailNum(int mFailNum) {
|
||||
this.mFailNum = mFailNum;
|
||||
}
|
||||
|
||||
public void setTimeOut(long timeOut) {
|
||||
this.mTimeOut = timeOut;
|
||||
}
|
||||
}
|
@ -14,7 +14,6 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
|
||||
package com.arialyy.aria.orm;
|
||||
|
||||
import android.support.annotation.NonNull;
|
||||
@ -46,6 +45,14 @@ public class DbEntity {
|
||||
return util.findAllData(clazz);
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询第一条数据
|
||||
*/
|
||||
public static <T extends DbEntity> T findFirst(Class<T> clazz) {
|
||||
List<T> list = findAllData(clazz);
|
||||
return (list == null || list.size() == 0) ? null : list.get(0);
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询一组数据
|
||||
*
|
||||
|
@ -16,6 +16,8 @@
|
||||
|
||||
package com.arialyy.aria.util;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.SharedPreferences;
|
||||
import android.util.Log;
|
||||
import com.arialyy.aria.core.command.CmdFactory;
|
||||
import com.arialyy.aria.core.DownloadEntity;
|
||||
@ -44,6 +46,33 @@ public class CommonUtil {
|
||||
return CmdFactory.getInstance().createCmd(entity, cmd);
|
||||
}
|
||||
|
||||
/**
|
||||
* 存储字符串到配置文件
|
||||
*
|
||||
* @param preName 配置文件名
|
||||
* @param key 存储的键值
|
||||
* @param value 需要存储的字符串
|
||||
* @return 成功标志
|
||||
*/
|
||||
public static Boolean putString(String preName, Context context, String key, String value) {
|
||||
SharedPreferences pre = context.getSharedPreferences(preName, Context.MODE_PRIVATE);
|
||||
SharedPreferences.Editor editor = pre.edit();
|
||||
editor.putString(key, value);
|
||||
return editor.commit();
|
||||
}
|
||||
|
||||
/**
|
||||
* 从配置文件读取字符串
|
||||
*
|
||||
* @param preName 配置文件名
|
||||
* @param key 字符串键值
|
||||
* @return 键值对应的字符串, 默认返回""
|
||||
*/
|
||||
public static String getString(String preName, Context context, String key) {
|
||||
SharedPreferences pre = context.getSharedPreferences(preName, Context.MODE_PRIVATE);
|
||||
return pre.getString(key, "");
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取类里面的所在字段
|
||||
*/
|
||||
|
189
Aria/src/main/java/com/arialyy/aria/util/Configuration.java
Normal file
189
Aria/src/main/java/com/arialyy/aria/util/Configuration.java
Normal file
@ -0,0 +1,189 @@
|
||||
package com.arialyy.aria.util;
|
||||
|
||||
import android.util.Log;
|
||||
import com.arialyy.aria.core.DownloadManager;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
import java.util.Set;
|
||||
import java.util.WeakHashMap;
|
||||
|
||||
/**
|
||||
* Created by AriaL on 2016/12/8.
|
||||
* 信息配置
|
||||
*/
|
||||
public class Configuration {
|
||||
private static final String TAG = "Configuration";
|
||||
private static final String CONFIG_FILE = "/Aria/ADConfig.properties";
|
||||
/**
|
||||
* 当前调度器最大下载数,默认最大下载数为 “2”
|
||||
*/
|
||||
private static final String DOWNLOAD_NUM = "DOWNLOAD_NUM";
|
||||
/**
|
||||
* 失败重试次数,默认最多重试 10 次
|
||||
*/
|
||||
private static final String RE_TRY_NUM = "RE_TRY_NUM";
|
||||
/**
|
||||
* 是否打开下载广播,默认 false
|
||||
*/
|
||||
private static final String OPEN_BROADCAST = "OPEN_BROADCAST";
|
||||
/**
|
||||
* 失败重试间隔时间,默认 4000 ms
|
||||
*/
|
||||
private static final String RE_TRY_INTERVAL = "RE_TRY_INTERVAL";
|
||||
/**
|
||||
* 超时时间,默认 10000 ms
|
||||
*/
|
||||
private static final String DOWNLOAD_TIME_OUT = "DOWNLOAD_TIME_OUT";
|
||||
|
||||
private static Configuration INSTANCE = null;
|
||||
private File mConfigFile = null;
|
||||
private static final Object LOCK = new Object();
|
||||
|
||||
public static Configuration getInstance() {
|
||||
if (INSTANCE == null) {
|
||||
synchronized (LOCK) {
|
||||
INSTANCE = new Configuration();
|
||||
}
|
||||
}
|
||||
return INSTANCE;
|
||||
}
|
||||
|
||||
private Configuration() {
|
||||
mConfigFile = new File(DownloadManager.APP.getFilesDir().getPath() + CONFIG_FILE);
|
||||
try {
|
||||
if (!mConfigFile.exists()) {
|
||||
mConfigFile.getParentFile().mkdirs();
|
||||
mConfigFile.createNewFile();
|
||||
init();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
private void init() {
|
||||
Map<String, String> config = new WeakHashMap<>();
|
||||
config.put(DOWNLOAD_NUM, 2 + "");
|
||||
config.put(RE_TRY_NUM, 10 + "");
|
||||
config.put(OPEN_BROADCAST, false + "");
|
||||
config.put(RE_TRY_INTERVAL, 4000 + "");
|
||||
config.put(DOWNLOAD_TIME_OUT, 10000 + "");
|
||||
saveConfig(config);
|
||||
}
|
||||
|
||||
private void saveConfig(Map<String, String> config) {
|
||||
if (config == null || config.size() == 0) {
|
||||
return;
|
||||
}
|
||||
Properties properties = CommonUtil.loadConfig(mConfigFile);
|
||||
Set<String> keys = config.keySet();
|
||||
for (String key : keys) {
|
||||
properties.setProperty(key, config.get(key));
|
||||
}
|
||||
CommonUtil.saveConfig(mConfigFile, properties);
|
||||
}
|
||||
|
||||
private void save(String key, String value) {
|
||||
Map<String, String> map = new WeakHashMap<>();
|
||||
map.put(key, value);
|
||||
saveConfig(map);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取下载超时时间
|
||||
*
|
||||
* @return 默认4000ms
|
||||
*/
|
||||
public int getTimeOut() {
|
||||
return Integer.parseInt(CommonUtil.loadConfig(mConfigFile).getProperty(DOWNLOAD_TIME_OUT));
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置重试间隔
|
||||
*/
|
||||
public void setTimeOut(int timeOut) {
|
||||
if (timeOut < 10000) {
|
||||
Log.w(TAG, "下载超时时间不能小于 10000 ms");
|
||||
return;
|
||||
}
|
||||
save(DOWNLOAD_TIME_OUT, timeOut + "");
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取失败重试间隔时间
|
||||
*
|
||||
* @return 默认4000ms
|
||||
*/
|
||||
public int getReTryInterval() {
|
||||
return Integer.parseInt(CommonUtil.loadConfig(mConfigFile).getProperty(RE_TRY_INTERVAL));
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置重试间隔
|
||||
*/
|
||||
public void setReTryreTryInterval(int reTryInterval) {
|
||||
if (reTryInterval < 4000) {
|
||||
Log.w(TAG, "重试间隔不能小于4000ms");
|
||||
return;
|
||||
}
|
||||
save(RE_TRY_INTERVAL, reTryInterval + "");
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取最大下载数
|
||||
*
|
||||
* @return 默认返回2
|
||||
*/
|
||||
public int getDownloadNum() {
|
||||
return Integer.parseInt(CommonUtil.loadConfig(mConfigFile).getProperty(DOWNLOAD_NUM));
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置最大下载数
|
||||
*/
|
||||
public void setDownloadNum(int downloadNum) {
|
||||
if (downloadNum < 1) {
|
||||
Log.w(TAG, "最大下载数不能小于1");
|
||||
return;
|
||||
}
|
||||
save(DOWNLOAD_NUM, downloadNum + "");
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取最大重试数
|
||||
*
|
||||
* @return 默认返回 10
|
||||
*/
|
||||
public int getReTryNum() {
|
||||
return Integer.parseInt(CommonUtil.loadConfig(mConfigFile).getProperty(RE_TRY_NUM));
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置重试数
|
||||
*/
|
||||
public void setReTryNum(int reTryNum) {
|
||||
if (reTryNum < 1) {
|
||||
Log.w(TAG, "最大下载数不能小于1");
|
||||
return;
|
||||
}
|
||||
save(RE_TRY_NUM, reTryNum + "");
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否打开下载广播
|
||||
*
|
||||
* @return 默认false
|
||||
*/
|
||||
public boolean isOpenBroadcast() {
|
||||
return Boolean.parseBoolean(CommonUtil.loadConfig(mConfigFile).getProperty(RE_TRY_NUM));
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置是否打开下载广播
|
||||
*/
|
||||
public void setOpenBroadcast(boolean openBroadcast) {
|
||||
save(OPEN_BROADCAST, openBroadcast + "");
|
||||
}
|
||||
}
|
@ -147,7 +147,7 @@ public class DownloadAdapter extends AbsRVAdapter<DownloadEntity, DownloadAdapte
|
||||
}
|
||||
|
||||
public void setDownloadNum(int num) {
|
||||
Aria.get(getContext()).setDownloadNum(num);
|
||||
Aria.get(getContext()).setMaxDownloadNum(num);
|
||||
}
|
||||
|
||||
private String covertCurrentSize(long currentSize) {
|
||||
|
@ -28,6 +28,6 @@ public class BaseApplication extends Application {
|
||||
@Override public void onCreate() {
|
||||
super.onCreate();
|
||||
AbsFrame.init(this);
|
||||
DownloadManager.init(this);
|
||||
//DownloadManager.init(this);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user