From 1a2f86a5af5302a78034a956cd9c33d836aa8bad Mon Sep 17 00:00:00 2001 From: AriaLyy <511455842@qq.com> Date: Thu, 8 Dec 2016 22:59:41 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=8B=E8=BD=BD=E9=85=8D=E7=BD=AE=E8=AE=BE?= =?UTF-8?q?=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/arialyy/aria/core/AriaManager.java | 32 ++- .../arialyy/aria/core/DownloadManager.java | 18 +- .../aria/core/queue/DownloadTaskQueue.java | 1 - .../aria/core/queue/pool/ExecutePool.java | 6 +- .../core/scheduler/DownloadSchedulers.java | 20 +- .../java/com/arialyy/aria/orm/DbEntity.java | 9 +- .../com/arialyy/aria/util/CommonUtil.java | 29 +++ .../com/arialyy/aria/util/Configuration.java | 189 ++++++++++++++++++ .../simple/adapter/DownloadAdapter.java | 2 +- .../arialyy/simple/base/BaseApplication.java | 2 +- 10 files changed, 269 insertions(+), 39 deletions(-) create mode 100644 Aria/src/main/java/com/arialyy/aria/util/Configuration.java diff --git a/Aria/src/main/java/com/arialyy/aria/core/AriaManager.java b/Aria/src/main/java/com/arialyy/aria/core/AriaManager.java index d0553f81..4eff1677 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/AriaManager.java +++ b/Aria/src/main/java/com/arialyy/aria/core/AriaManager.java @@ -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 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); + } + /** * 删除所有任务 */ diff --git a/Aria/src/main/java/com/arialyy/aria/core/DownloadManager.java b/Aria/src/main/java/com/arialyy/aria/core/DownloadManager.java index f6eb2b9e..a50906ad 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/DownloadManager.java +++ b/Aria/src/main/java/com/arialyy/aria/core/DownloadManager.java @@ -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 mCommands = new ArrayList<>(); - private Context mContext; - private ITaskQueue mTaskQueue; + 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 getAllDownloadEntity() { + List 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 commands) { + DownloadManager setCmds(List 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(); } diff --git a/Aria/src/main/java/com/arialyy/aria/core/queue/DownloadTaskQueue.java b/Aria/src/main/java/com/arialyy/aria/core/queue/DownloadTaskQueue.java index 1e597934..d0c3268c 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/queue/DownloadTaskQueue.java +++ b/Aria/src/main/java/com/arialyy/aria/core/queue/DownloadTaskQueue.java @@ -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() { } diff --git a/Aria/src/main/java/com/arialyy/aria/core/queue/pool/ExecutePool.java b/Aria/src/main/java/com/arialyy/aria/core/queue/pool/ExecutePool.java index 054ffab8..6f3f5009 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/queue/pool/ExecutePool.java +++ b/Aria/src/main/java/com/arialyy/aria/core/queue/pool/ExecutePool.java @@ -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 mExecuteQueue; private Map 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(); } diff --git a/Aria/src/main/java/com/arialyy/aria/core/scheduler/DownloadSchedulers.java b/Aria/src/main/java/com/arialyy/aria/core/scheduler/DownloadSchedulers.java index 397f2085..16e59274 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/scheduler/DownloadSchedulers.java +++ b/Aria/src/main/java/com/arialyy/aria/core/scheduler/DownloadSchedulers.java @@ -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; - } } \ No newline at end of file diff --git a/Aria/src/main/java/com/arialyy/aria/orm/DbEntity.java b/Aria/src/main/java/com/arialyy/aria/orm/DbEntity.java index 74047a7a..15581067 100644 --- a/Aria/src/main/java/com/arialyy/aria/orm/DbEntity.java +++ b/Aria/src/main/java/com/arialyy/aria/orm/DbEntity.java @@ -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 findFirst(Class clazz) { + List list = findAllData(clazz); + return (list == null || list.size() == 0) ? null : list.get(0); + } + /** * 查询一组数据 * diff --git a/Aria/src/main/java/com/arialyy/aria/util/CommonUtil.java b/Aria/src/main/java/com/arialyy/aria/util/CommonUtil.java index 98f2df6e..30247c37 100644 --- a/Aria/src/main/java/com/arialyy/aria/util/CommonUtil.java +++ b/Aria/src/main/java/com/arialyy/aria/util/CommonUtil.java @@ -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, ""); + } + /** * 获取类里面的所在字段 */ diff --git a/Aria/src/main/java/com/arialyy/aria/util/Configuration.java b/Aria/src/main/java/com/arialyy/aria/util/Configuration.java new file mode 100644 index 00000000..90fe78a5 --- /dev/null +++ b/Aria/src/main/java/com/arialyy/aria/util/Configuration.java @@ -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 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 config) { + if (config == null || config.size() == 0) { + return; + } + Properties properties = CommonUtil.loadConfig(mConfigFile); + Set 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 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 + ""); + } +} diff --git a/app/src/main/java/com/arialyy/simple/adapter/DownloadAdapter.java b/app/src/main/java/com/arialyy/simple/adapter/DownloadAdapter.java index ef15f824..d13baa83 100644 --- a/app/src/main/java/com/arialyy/simple/adapter/DownloadAdapter.java +++ b/app/src/main/java/com/arialyy/simple/adapter/DownloadAdapter.java @@ -147,7 +147,7 @@ public class DownloadAdapter extends AbsRVAdapter