From a7ca196258a5bb16a542e005c0785c2049cc160b Mon Sep 17 00:00:00 2001 From: laoyuyu <511455842@qq.com> Date: Fri, 20 Apr 2018 22:59:24 +0800 Subject: [PATCH] =?UTF-8?q?=E9=85=8D=E7=BD=AE=E6=96=87=E4=BB=B6=E6=96=B0?= =?UTF-8?q?=E5=A2=9Eapp=E8=AE=BE=E7=BD=AE=E5=8F=82=E6=95=B0=EF=BC=8C?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=BC=82=E5=B8=B8=E6=8D=95=E8=8E=B7=E6=97=A5?= =?UTF-8?q?=E5=BF=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/com/arialyy/aria/core/Aria.java | 66 ++-- .../com/arialyy/aria/core/AriaManager.java | 50 +-- .../com/arialyy/aria/core/ConfigHelper.java | 146 +++++--- .../com/arialyy/aria/core/ErrorEntity.java | 77 ---- .../core/command/normal/ResumeAllCmd.java | 44 ++- .../aria/core/command/normal/StartCmd.java | 44 ++- .../aria/core/common/AbsThreadTask.java | 4 +- .../aria/core/download/DownloadReceiver.java | 8 +- .../aria/core/download/DownloadTask.java | 1 + .../core/download/downloader/Downloader.java | 4 +- .../downloader/FtpDirDownloadUtil.java | 4 +- .../downloader/HttpFileInfoThread.java | 2 +- .../download/downloader/HttpThreadTask.java | 3 +- .../downloader/SimpleDownloadUtil.java | 4 +- ...roupEntityWrapper.java => DGEWrapper.java} | 2 +- ...skEntityWrapper.java => DGSTEWrapper.java} | 2 +- ...askEntityWrapper.java => DGTEWrapper.java} | 8 +- ...wnloadTaskWrapper.java => DTEWrapper.java} | 2 +- .../aria/core/manager/DGTEFactory.java | 16 +- .../arialyy/aria/core/manager/DTEFactory.java | 4 +- .../arialyy/aria/core/manager/UTEFactory.java | 6 +- .../core/upload/uploader/HttpThreadTask.java | 1 + .../UTEWrapper.java} | 6 +- .../java/com/arialyy/aria/orm/DBConfig.java | 2 - .../com/arialyy/aria/orm/DatabaseContext.java | 69 ++-- .../java/com/arialyy/aria/orm/SqlHelper.java | 3 +- .../arialyy/aria/util/AriaCrashHandler.java | 73 ++++ .../com/arialyy/aria/util/CommonUtil.java | 57 +-- .../java/com/arialyy/aria/util/ErrorHelp.java | 95 +++-- app/src/main/AndroidManifest.xml | 4 +- app/src/main/assets/aria_config.xml | 8 + .../simple/download/SingleTaskActivity.java | 12 +- .../arialyy/simple/test/AnyRunActivity.java | 11 +- .../arialyy/simple/test/AnyRunnModule.java | 20 +- .../com/arialyy/simple/test/TestActivity.java | 11 +- .../com/arialyy/aria/core/Configuration.java | 341 ++++++++++++------ 36 files changed, 731 insertions(+), 479 deletions(-) delete mode 100644 Aria/src/main/java/com/arialyy/aria/core/ErrorEntity.java rename Aria/src/main/java/com/arialyy/aria/core/download/wrapper/{DGroupEntityWrapper.java => DGEWrapper.java} (96%) rename Aria/src/main/java/com/arialyy/aria/core/download/wrapper/{DGroupSubTaskEntityWrapper.java => DGSTEWrapper.java} (96%) rename Aria/src/main/java/com/arialyy/aria/core/download/wrapper/{DGroupTaskEntityWrapper.java => DGTEWrapper.java} (89%) rename Aria/src/main/java/com/arialyy/aria/core/download/wrapper/{DownloadTaskWrapper.java => DTEWrapper.java} (96%) rename Aria/src/main/java/com/arialyy/aria/core/upload/{UploadTaskWrapper.java => wrapper/UTEWrapper.java} (87%) create mode 100644 Aria/src/main/java/com/arialyy/aria/util/AriaCrashHandler.java diff --git a/Aria/src/main/java/com/arialyy/aria/core/Aria.java b/Aria/src/main/java/com/arialyy/aria/core/Aria.java index f4d2b65e..10a40fa2 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/Aria.java +++ b/Aria/src/main/java/com/arialyy/aria/core/Aria.java @@ -44,6 +44,24 @@ import com.arialyy.aria.core.upload.UploadReceiver; * .start(); * * + * + * 如果你需要在【Activity、Service、Application、DialogFragment、Fragment、PopupWindow、Dialog】 + * 之外的java中使用Aria,那么你应该在Application或Activity初始化的时候调用{@link #init(Context)}对Aria进行初始化 + * 然后才能使用{@link #download(Object)}、{@link #upload(Object)} + * + *
+ *   
+ *       Aria.init(getContext());
+ *
+ *      Aria.download(this)
+ *       .load(URL)     //下载地址,必填
+ *       //文件保存路径,必填
+ *       .setDownloadPath(Environment.getExternalStorageDirectory().getPath() + "/test.apk")
+ *       .start();
+ *
+ *   
+ *
+ * 
*/ @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1) public class Aria { @@ -69,21 +87,21 @@ import com.arialyy.aria.core.upload.UploadReceiver; } /** - * 在任意对象中初始化下载 + * 在任意对象中初始化下载,前提是你需要在Application或Activity初始化的时候调用{@link #init(Context)}对Aria进行初始化 * * @param obj 任意对象 */ - public static DownloadReceiver download(Context context, Object obj) { - return get(context).download(obj); + public static DownloadReceiver download(Object obj) { + return AriaManager.getInstance().download(obj); } /** - * 在任意对象中初始化上传 + * 在任意对象中初始化上传,前提是你需要在Application或Activity初始化的时候调用{@link #init(Context)}对Aria进行初始化 * * @param obj 任意对象 */ - public static UploadReceiver upload(Context context, Object obj) { - return get(context).upload(obj); + public static UploadReceiver upload(Object obj) { + return AriaManager.getInstance().upload(obj); } /** @@ -91,31 +109,15 @@ import com.arialyy.aria.core.upload.UploadReceiver; */ public static AriaManager get(Context context) { return AriaManager.getInstance(context); - //if (obj instanceof Activity || obj instanceof Service || obj instanceof Application) { - // return AriaManager.getInstance((Context) obj); - //} else if (obj instanceof DialogFragment) { - // DialogFragment dialog = (DialogFragment) obj; - // return AriaManager.getInstance( - // Build.VERSION.SDK_INT >= Build.VERSION_CODES.M ? dialog.getContext() - // : dialog.getActivity()); - //} else if (obj instanceof android.support.v4.app.Fragment) { - // android.support.v4.app.Fragment fragment = (android.support.v4.app.Fragment) obj; - // return AriaManager.getInstance( - // Build.VERSION.SDK_INT >= Build.VERSION_CODES.M ? fragment.getContext() - // : fragment.getActivity()); - //} else if (obj instanceof Fragment) { - // Fragment fragment = (Fragment) obj; - // return AriaManager.getInstance( - // Build.VERSION.SDK_INT >= Build.VERSION_CODES.M ? fragment.getContext() - // : fragment.getActivity()); - //} else if (obj instanceof PopupWindow) { - // PopupWindow popupWindow = (PopupWindow) obj; - // return AriaManager.getInstance(popupWindow.getContentView().getContext()); - //} else if (obj instanceof Dialog) { - // Dialog dialog = (Dialog) obj; - // return AriaManager.getInstance(dialog.getContext()); - //} else { - // throw new IllegalArgumentException("不支持的类型"); - //} + } + + /** + * 初始化Aria,如果你需要在【Activity、Service、Application、DialogFragment、Fragment、PopupWindow、Dialog】 + * 之外的java中使用Aria,那么你应该在Application或Activity初始化的时候调用本方法对Aria进行初始化 + * 只需要初始化一次就可以 + * {@link #download(Object)}、{@link #upload(Object)} + */ + public static AriaManager init(Context context) { + return AriaManager.getInstance(context); } } 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 645b458b..2fe50163 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/AriaManager.java +++ b/Aria/src/main/java/com/arialyy/aria/core/AriaManager.java @@ -41,6 +41,7 @@ import com.arialyy.aria.core.upload.UploadTaskEntity; import com.arialyy.aria.orm.DbEntity; import com.arialyy.aria.orm.DelegateWrapper; 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.IOException; @@ -73,12 +74,14 @@ import org.xml.sax.SAXException; private List mCommands = new ArrayList<>(); private Configuration.DownloadConfig mDConfig; private Configuration.UploadConfig mUConfig; + private Configuration.AppConfig mAConfig; private AriaManager(Context context) { DelegateWrapper.init(context.getApplicationContext()); APP = context.getApplicationContext(); regAppLifeCallback(context); initConfig(); + initAria(); } public static AriaManager getInstance(Context context) { @@ -90,17 +93,22 @@ import org.xml.sax.SAXException; return INSTANCE; } - public Map getReceiver() { - return mReceivers; + static AriaManager getInstance() { + if (INSTANCE == null) { + throw new NullPointerException("请在Application或Activity初始化时调用一次Aria.init(context)方法进行初始化操作"); + } + return INSTANCE; } - /** - * 设置Aria的日志级别 - * - * @param level {@link ALog#LOG_LEVEL_VERBOSE} - */ - public void setLogLevel(int level) { - ALog.LOG_LEVEL = level; + private void initAria() { + if (mAConfig.getUseAriaCrashHandler()) { + Thread.setDefaultUncaughtExceptionHandler(new AriaCrashHandler()); + } + mAConfig.setLogLevel(mAConfig.getLogLevel()); + } + + public Map getReceiver() { + return mReceivers; } /** @@ -163,6 +171,13 @@ import org.xml.sax.SAXException; return mUConfig; } + /** + * 获取APP配置 + */ + public Configuration.AppConfig getAppConfig() { + return mAConfig; + } + /** * 设置命令 */ @@ -213,22 +228,6 @@ import org.xml.sax.SAXException; return (receiver instanceof UploadReceiver) ? (UploadReceiver) receiver : null; } - /** - * 获取Aria下载错误日志 - * - * @return 如果错误日志不存在则返回空,否则返回错误日志列表 - */ - public List getErrorLog() { - return DbEntity.findAllData(ErrorEntity.class); - } - - /** - * 清楚错误日志 - */ - public void cleanLog() { - DbEntity.clean(ErrorEntity.class); - } - /** * 删除任务记录 * @@ -379,6 +378,7 @@ import org.xml.sax.SAXException; } mDConfig = Configuration.DownloadConfig.getInstance(); mUConfig = Configuration.UploadConfig.getInstance(); + mAConfig = Configuration.AppConfig.getInstance(); if (tempDir.exists()) { File newDir = new File(APP.getFilesDir().getPath() + DOWNLOAD_TEMP_DIR); newDir.mkdirs(); diff --git a/Aria/src/main/java/com/arialyy/aria/core/ConfigHelper.java b/Aria/src/main/java/com/arialyy/aria/core/ConfigHelper.java index 1eb12427..c21012a0 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/ConfigHelper.java +++ b/Aria/src/main/java/com/arialyy/aria/core/ConfigHelper.java @@ -28,9 +28,10 @@ import org.xml.sax.helpers.DefaultHandler; class ConfigHelper extends DefaultHandler { private final String TAG = "ConfigHelper"; - private boolean isDownloadConfig = false, isUploadConfig; + private boolean isDownloadConfig = false, isUploadConfig = false, isAppConfig = false; private Configuration.DownloadConfig mDownloadConfig = Configuration.DownloadConfig.getInstance(); private Configuration.UploadConfig mUploadConfig = Configuration.UploadConfig.getInstance(); + private Configuration.AppConfig mAppConfig = Configuration.AppConfig.getInstance(); @Override public void startDocument() throws SAXException { super.startDocument(); @@ -43,10 +44,17 @@ class ConfigHelper extends DefaultHandler { if (qName.equals("download")) { isDownloadConfig = true; isUploadConfig = false; + isAppConfig = false; } else if (qName.equals("upload")) { isUploadConfig = true; isDownloadConfig = false; + isAppConfig = false; + } else if (qName.equals("app")) { + isUploadConfig = false; + isDownloadConfig = false; + isAppConfig = true; } + if (isDownloadConfig || isUploadConfig) { String value = attributes.getValue("value"); @@ -90,17 +98,46 @@ class ConfigHelper extends DefaultHandler { loadUpdateInterval(value); break; } + } else if (isAppConfig) { + String value = attributes.getValue("value"); + switch (value) { + case "useAriaCrashHandler": + loadUseAriaCrashHandler(value); + break; + case "logLevel": + loadLogLevel(value); + break; + } } } - private void loadUpdateInterval(String value) { - long temp = 1000; - if (!TextUtils.isEmpty(value)) { - temp = Long.parseLong(value); - if (temp <= 0) { - temp = 1000; - } + 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)) { + ALog.w(TAG, "useAriaCrashHandler【" + value + "】错误"); + mAppConfig.useAriaCrashHandler = Boolean.parseBoolean(value); + } else { + mAppConfig.useAriaCrashHandler = true; + } + } + + private void loadUpdateInterval(String value) { + long temp = checkLong(value) ? Long.parseLong(value) : 1000; if (isDownloadConfig) { mDownloadConfig.updateInterval = temp; } @@ -124,17 +161,18 @@ class ConfigHelper extends DefaultHandler { } private void loadMaxSpeed(String value) { - double maxSpeed = 0.0; - if (!TextUtils.isEmpty(value)) { - maxSpeed = Double.parseDouble(value); - } + int maxSpeed = checkInt(value) ? Integer.parseInt(value) : 0; if (isDownloadConfig) { mDownloadConfig.msxSpeed = maxSpeed; } } private void loadConvertSpeed(String value) { - boolean open = Boolean.parseBoolean(value); + boolean open = true; + if (checkBoolean(value)) { + open = Boolean.parseBoolean(value); + } + if (isDownloadConfig) { mDownloadConfig.isConvertSpeed = open; } @@ -144,10 +182,7 @@ class ConfigHelper extends DefaultHandler { } private void loadReTryInterval(String value) { - int time = 2 * 1000; - if (!TextUtils.isEmpty(value)) { - time = Integer.parseInt(value); - } + int time = checkInt(value) ? Integer.parseInt(value) : 2 * 1000; if (time < 2 * 1000) { time = 2 * 1000; @@ -166,10 +201,7 @@ class ConfigHelper extends DefaultHandler { } private void loadBuffSize(String value) { - int buffSize = 8192; - if (!TextUtils.isEmpty(value)) { - buffSize = Integer.parseInt(value); - } + int buffSize = checkInt(value) ? Integer.parseInt(value) : 8192; if (buffSize < 2048) { buffSize = 2048; @@ -181,10 +213,7 @@ class ConfigHelper extends DefaultHandler { } private void loadIOTimeout(String value) { - int time = 10 * 1000; - if (!TextUtils.isEmpty(value)) { - time = Integer.parseInt(value); - } + int time = checkInt(value) ? Integer.parseInt(value) : 10 * 1000; if (time < 10 * 1000) { time = 10 * 1000; @@ -196,10 +225,7 @@ class ConfigHelper extends DefaultHandler { } private void loadConnectTime(String value) { - int time = 5 * 1000; - if (!TextUtils.isEmpty(value)) { - time = Integer.parseInt(value); - } + int time = checkInt(value) ? Integer.parseInt(value) : 5 * 1000; if (isDownloadConfig) { mDownloadConfig.connectTimeOut = time; @@ -210,10 +236,7 @@ class ConfigHelper extends DefaultHandler { } private void loadReTry(String value) { - int num = 0; - if (!TextUtils.isEmpty(value)) { - num = Integer.parseInt(value); - } + int num = checkInt(value) ? Integer.parseInt(value) : 0; if (isDownloadConfig) { mDownloadConfig.reTryNum = num; @@ -224,10 +247,7 @@ class ConfigHelper extends DefaultHandler { } private void loadMaxQueue(String value) { - int num = 2; - if (!TextUtils.isEmpty(value)) { - num = Integer.parseInt(value); - } + int num = checkInt(value) ? Integer.parseInt(value) : 2; if (num < 1) { ALog.w(TAG, "任务队列数不能小于 1"); num = 2; @@ -241,10 +261,7 @@ class ConfigHelper extends DefaultHandler { } private void loadThreadNum(String value) { - int num = 3; - if (!TextUtils.isEmpty(value)) { - num = Integer.parseInt(value); - } + int num = checkInt(value) ? Integer.parseInt(value) : 3; if (num < 1) { ALog.e(TAG, "下载线程数不能小于 1"); num = 1; @@ -254,6 +271,52 @@ class ConfigHelper extends DefaultHandler { } } + /** + * 检查是否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); } @@ -266,5 +329,6 @@ class ConfigHelper extends DefaultHandler { super.endDocument(); mDownloadConfig.saveAll(); mUploadConfig.saveAll(); + mAppConfig.saveAll(); } } diff --git a/Aria/src/main/java/com/arialyy/aria/core/ErrorEntity.java b/Aria/src/main/java/com/arialyy/aria/core/ErrorEntity.java deleted file mode 100644 index 0ba2bbe6..00000000 --- a/Aria/src/main/java/com/arialyy/aria/core/ErrorEntity.java +++ /dev/null @@ -1,77 +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 com.arialyy.aria.orm.DbEntity; - -/** - * Created by Aria.Lao on 2017/8/29. - * 错误实体 - */ -public class ErrorEntity extends DbEntity { - - /** - * 插入时间 - */ - public long insertTime; - - /** - * 错误信息 - */ - public String err; - - /** - * 任务名 - */ - public String taskName; - - /** - *任务类型 - */ - public String taskType; - - /** - * 提示 - */ - public String msg; - - /** - * 任务key - */ - public String key; - - @Override public String toString() { - return "ErrorEntity{" - + "insertTime=" - + insertTime - + ", err='" - + err - + '\'' - + ", taskName='" - + taskName - + '\'' - + ", taskType='" - + taskType - + '\'' - + ", msg='" - + msg - + '\'' - + ", key='" - + key - + '\'' - + '}'; - } -} diff --git a/Aria/src/main/java/com/arialyy/aria/core/command/normal/ResumeAllCmd.java b/Aria/src/main/java/com/arialyy/aria/core/command/normal/ResumeAllCmd.java index b952b205..6cf11bfa 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/command/normal/ResumeAllCmd.java +++ b/Aria/src/main/java/com/arialyy/aria/core/command/normal/ResumeAllCmd.java @@ -3,12 +3,15 @@ package com.arialyy.aria.core.command.normal; import com.arialyy.aria.core.AriaManager; import com.arialyy.aria.core.download.DownloadGroupTaskEntity; import com.arialyy.aria.core.download.DownloadTaskEntity; +import com.arialyy.aria.core.download.wrapper.DGTEWrapper; +import com.arialyy.aria.core.download.wrapper.DTEWrapper; import com.arialyy.aria.core.inf.AbsTaskEntity; import com.arialyy.aria.core.inf.IEntity; 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.core.upload.UploadTaskEntity; +import com.arialyy.aria.core.upload.wrapper.UTEWrapper; import com.arialyy.aria.orm.DbEntity; import com.arialyy.aria.util.ALog; import com.arialyy.aria.util.CommonUtil; @@ -53,29 +56,32 @@ final class ResumeAllCmd extends AbsNormalCmd { * @param type {@code 1}单任务下载任务;{@code 2}任务组下载任务;{@code 3} 单任务上传任务 */ private List findTaskData(int type) { + // TODO: 2018/4/20 需要测试 List tempList = new ArrayList<>(); - switch (type) { - case 1: - List dTaskEntity = - DbEntity.findDatas(DownloadTaskEntity.class, "isGroupTask=?", "false"); - if (dTaskEntity != null && !dTaskEntity.isEmpty()) { - tempList.addAll(dTaskEntity); + if (type == 1) { + List wrappers = DbEntity.findRelationData(DTEWrapper.class, + "DownloadTaskEntity.isGroupTask=false and DownloadTaskEntity.state!=?", "1"); + if (wrappers != null && !wrappers.isEmpty()) { + for (DTEWrapper w : wrappers) { + tempList.add(w.taskEntity); } - break; - case 2: - List groupTask = - DbEntity.findAllData(DownloadGroupTaskEntity.class); - if (groupTask != null && !groupTask.isEmpty()) { - tempList.addAll(groupTask); + } + } else if (type == 2) { + List wrappers = + DbEntity.findRelationData(DGTEWrapper.class, "DownloadGroupTaskEntity.state!=?", "1"); + if (wrappers != null && !wrappers.isEmpty()) { + for (DGTEWrapper w : wrappers) { + tempList.add(w.taskEntity); } - break; - case 3: - List uTaskEntity = - DbEntity.findDatas(UploadTaskEntity.class, "isGroupTask=?", "false"); - if (uTaskEntity != null && !uTaskEntity.isEmpty()) { - tempList.addAll(uTaskEntity); + } + } else if (type == 3) { + List wrappers = DbEntity.findRelationData(UTEWrapper.class, + "UploadTaskEntity.state!=?", "1"); + if (wrappers != null && !wrappers.isEmpty()) { + for (UTEWrapper w : wrappers) { + tempList.add(w.taskEntity); } - break; + } } return tempList; } diff --git a/Aria/src/main/java/com/arialyy/aria/core/command/normal/StartCmd.java b/Aria/src/main/java/com/arialyy/aria/core/command/normal/StartCmd.java index 45bbab04..80ff11d8 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/command/normal/StartCmd.java +++ b/Aria/src/main/java/com/arialyy/aria/core/command/normal/StartCmd.java @@ -21,6 +21,8 @@ import com.arialyy.aria.core.AriaManager; import com.arialyy.aria.core.common.QueueMod; import com.arialyy.aria.core.download.DownloadGroupTaskEntity; import com.arialyy.aria.core.download.DownloadTaskEntity; +import com.arialyy.aria.core.download.wrapper.DGTEWrapper; +import com.arialyy.aria.core.download.wrapper.DTEWrapper; import com.arialyy.aria.core.inf.AbsTask; import com.arialyy.aria.core.inf.AbsTaskEntity; import com.arialyy.aria.core.inf.IEntity; @@ -28,6 +30,7 @@ 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.core.upload.UploadTaskEntity; +import com.arialyy.aria.core.upload.wrapper.UTEWrapper; import com.arialyy.aria.orm.DbEntity; import com.arialyy.aria.util.ALog; import com.arialyy.aria.util.CommonUtil; @@ -112,29 +115,32 @@ class StartCmd extends AbsNormalCmd { } private List findWaitData(int type) { + // TODO: 2018/4/20 需要测试 List waitList = new ArrayList<>(); - switch (type) { - case 1: - List dEntity = - DbEntity.findDatas(DownloadTaskEntity.class, "groupName=? and state=?", "", "3"); - if (dEntity != null && !dEntity.isEmpty()) { - waitList.addAll(dEntity); + if (type == 1) { + List wrappers = DbEntity.findRelationData(DTEWrapper.class, + "DownloadTaskEntity.isGroupTask=false and DownloadTaskEntity.state=?", "3"); + if (wrappers != null && !wrappers.isEmpty()) { + for (DTEWrapper w : wrappers) { + waitList.add(w.taskEntity); } - break; - case 2: - List dgEntity = - DbEntity.findDatas(DownloadGroupTaskEntity.class, "state=?", "3"); - if (dgEntity != null && !dgEntity.isEmpty()) { - waitList.addAll(dgEntity); + } + } else if (type == 2) { + List wrappers = + DbEntity.findRelationData(DGTEWrapper.class, "DownloadGroupTaskEntity.state=?", "3"); + if (wrappers != null && !wrappers.isEmpty()) { + for (DGTEWrapper w : wrappers) { + waitList.add(w.taskEntity); } - break; - case 3: - List uEntity = - DbEntity.findDatas(UploadTaskEntity.class, "state=?", "3"); - if (uEntity != null && !uEntity.isEmpty()) { - waitList.addAll(uEntity); + } + } else if (type == 3) { + List wrappers = DbEntity.findRelationData(UTEWrapper.class, + "UploadTaskEntity.state=?", "3"); + if (wrappers != null && !wrappers.isEmpty()) { + for (UTEWrapper w : wrappers) { + waitList.add(w.taskEntity); } - break; + } } return waitList; } diff --git a/Aria/src/main/java/com/arialyy/aria/core/common/AbsThreadTask.java b/Aria/src/main/java/com/arialyy/aria/core/common/AbsThreadTask.java index 1dafb639..84bec8f1 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/common/AbsThreadTask.java +++ b/Aria/src/main/java/com/arialyy/aria/core/common/AbsThreadTask.java @@ -200,7 +200,7 @@ public abstract class AbsThreadTask getGroupTaskList() { - List wrappers = DbEntity.findRelationData(DGroupEntityWrapper.class); + List wrappers = DbEntity.findRelationData(DGEWrapper.class); if (wrappers == null || wrappers.isEmpty()) { return null; } List entities = new ArrayList<>(); - for (DGroupEntityWrapper wrapper : wrappers) { + for (DGEWrapper wrapper : wrappers) { entities.add(wrapper.groupEntity); } return entities; diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadTask.java b/Aria/src/main/java/com/arialyy/aria/core/download/DownloadTask.java index aeecb62f..ea770433 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadTask.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/DownloadTask.java @@ -78,6 +78,7 @@ public class DownloadTask extends AbsNormalTask { /** * 是否真正下载 + * * @return {@code true} 真正下载 */ @Override public boolean isRunning() { diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/Downloader.java b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/Downloader.java index efbea34d..89bc60dd 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/Downloader.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/Downloader.java @@ -87,7 +87,7 @@ class Downloader extends AbsFileer { + "】\n【filePath:" + mEntity.getDownloadPath() + "】\n" - + CommonUtil.getPrintException(e)); + + ALog.getExceptionString(e)); } finally { if (file != null) { try { @@ -116,6 +116,6 @@ class Downloader extends AbsFileer { ALog.e(TAG, errorMsg); mConstance.isRunning = false; mListener.onFail(false); - ErrorHelp.saveError("", mEntity, "", errorMsg); + ErrorHelp.saveError(TAG, "", errorMsg); } } diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/FtpDirDownloadUtil.java b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/FtpDirDownloadUtil.java index 5a348ded..00ec7df8 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/FtpDirDownloadUtil.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/FtpDirDownloadUtil.java @@ -27,6 +27,8 @@ import java.util.Set; * ftp文件夹下载工具 */ public class FtpDirDownloadUtil extends AbsGroupUtil { + private String TAG = "FtpDirDownloadUtil"; + public FtpDirDownloadUtil(IDownloadGroupListener listener, DownloadGroupTaskEntity taskEntity) { super(listener, taskEntity); } @@ -56,7 +58,7 @@ public class FtpDirDownloadUtil extends AbsGroupUtil { mExeMap.remove(url); } mListener.onFail(needRetry); - ErrorHelp.saveError("D_FTP_DIR", mGTEntity.getEntity(), "", errorMsg); + ErrorHelp.saveError(TAG, "", errorMsg); } }).start(); } diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/HttpFileInfoThread.java b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/HttpFileInfoThread.java index 8e385e82..7b4ca45a 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/HttpFileInfoThread.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/HttpFileInfoThread.java @@ -64,7 +64,7 @@ class HttpFileInfoThread implements Runnable { + "】\n【filePath:" + mEntity.getDownloadPath() + "】\n" - + CommonUtil.getPrintException(e), true); + + ALog.getExceptionString(e), true); } finally { if (conn != null) { conn.disconnect(); diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/HttpThreadTask.java b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/HttpThreadTask.java index 68850799..fba3fca2 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/HttpThreadTask.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/HttpThreadTask.java @@ -116,11 +116,12 @@ final class HttpThreadTask extends AbsThreadTask subWrappers = - DbEntity.findRelationData(DownloadTaskWrapper.class, "DownloadTaskEntity.groupName=?", + List subWrappers = + DbEntity.findRelationData(DTEWrapper.class, "DownloadTaskEntity.groupName=?", taskEntity.getKey()); if (subWrappers != null && !subWrappers.isEmpty()) { List temp = new ArrayList<>(); - for (DownloadTaskWrapper dw : subWrappers) { + for (DTEWrapper dw : subWrappers) { if (dw.taskEntity.requestType == AbsTaskEntity.D_FTP) { dw.taskEntity.urlEntity = CommonUtil.getFtpUrlInfo(dw.taskEntity.url); } diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/wrapper/DownloadTaskWrapper.java b/Aria/src/main/java/com/arialyy/aria/core/download/wrapper/DTEWrapper.java similarity index 96% rename from Aria/src/main/java/com/arialyy/aria/core/download/wrapper/DownloadTaskWrapper.java rename to Aria/src/main/java/com/arialyy/aria/core/download/wrapper/DTEWrapper.java index 133a47ad..97d099dc 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/wrapper/DownloadTaskWrapper.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/wrapper/DTEWrapper.java @@ -27,7 +27,7 @@ import java.util.List; * Created by laoyuyu on 2018/3/30. */ @Wrapper -public class DownloadTaskWrapper extends AbsWrapper { +public class DTEWrapper extends AbsWrapper { @One public DownloadEntity entity; diff --git a/Aria/src/main/java/com/arialyy/aria/core/manager/DGTEFactory.java b/Aria/src/main/java/com/arialyy/aria/core/manager/DGTEFactory.java index 69e3b559..da64cb11 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/manager/DGTEFactory.java +++ b/Aria/src/main/java/com/arialyy/aria/core/manager/DGTEFactory.java @@ -19,8 +19,8 @@ import com.arialyy.aria.core.download.DownloadEntity; import com.arialyy.aria.core.download.DownloadGroupEntity; import com.arialyy.aria.core.download.DownloadGroupTaskEntity; import com.arialyy.aria.core.download.DownloadTaskEntity; -import com.arialyy.aria.core.download.wrapper.DGroupEntityWrapper; -import com.arialyy.aria.core.download.wrapper.DGroupTaskEntityWrapper; +import com.arialyy.aria.core.download.wrapper.DGEWrapper; +import com.arialyy.aria.core.download.wrapper.DGTEWrapper; import com.arialyy.aria.orm.DbEntity; import com.arialyy.aria.util.CommonUtil; import java.util.ArrayList; @@ -48,8 +48,8 @@ class DGTEFactory implements IGTEFactory urls) { DownloadGroupEntity entity = createDGroupEntity(groupName, urls); - List wrapper = - DbEntity.findRelationData(DGroupTaskEntityWrapper.class, "DownloadGroupTaskEntity.key=?", + List wrapper = + DbEntity.findRelationData(DGTEWrapper.class, "DownloadGroupTaskEntity.key=?", entity.getGroupName()); DownloadGroupTaskEntity gte; @@ -74,8 +74,8 @@ class DGTEFactory implements IGTEFactory wrapper = - DbEntity.findRelationData(DGroupTaskEntityWrapper.class, "DownloadGroupTaskEntity.key=?", + List wrapper = + DbEntity.findRelationData(DGTEWrapper.class, "DownloadGroupTaskEntity.key=?", ftpUrl); DownloadGroupTaskEntity fte; @@ -130,8 +130,8 @@ class DGTEFactory implements IGTEFactory urls) { - List wrapper = - DbEntity.findRelationData(DGroupEntityWrapper.class, "DownloadGroupEntity.groupName=?", + List wrapper = + DbEntity.findRelationData(DGEWrapper.class, "DownloadGroupEntity.groupName=?", groupName); DownloadGroupEntity groupEntity; diff --git a/Aria/src/main/java/com/arialyy/aria/core/manager/DTEFactory.java b/Aria/src/main/java/com/arialyy/aria/core/manager/DTEFactory.java index 29f5299a..082fe2c1 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/manager/DTEFactory.java +++ b/Aria/src/main/java/com/arialyy/aria/core/manager/DTEFactory.java @@ -18,7 +18,7 @@ package com.arialyy.aria.core.manager; import android.text.TextUtils; import com.arialyy.aria.core.download.DownloadEntity; import com.arialyy.aria.core.download.DownloadTaskEntity; -import com.arialyy.aria.core.download.wrapper.DownloadTaskWrapper; +import com.arialyy.aria.core.download.wrapper.DTEWrapper; import com.arialyy.aria.core.inf.IEntity; import com.arialyy.aria.orm.DbEntity; import java.io.File; @@ -49,7 +49,7 @@ class DTEFactory implements INormalTEFactory * 通过下载实体创建任务实体 */ private DownloadTaskEntity create(DownloadEntity entity) { - List wrapper = DbEntity.findRelationData(DownloadTaskWrapper.class, + List wrapper = DbEntity.findRelationData(DTEWrapper.class, "DownloadTaskEntity.key=? and DownloadTaskEntity.isGroupTask='false' and DownloadTaskEntity.url=?", entity.getDownloadPath(), entity.getUrl()); DownloadTaskEntity taskEntity; diff --git a/Aria/src/main/java/com/arialyy/aria/core/manager/UTEFactory.java b/Aria/src/main/java/com/arialyy/aria/core/manager/UTEFactory.java index 9f2e60cb..f0437b9e 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/manager/UTEFactory.java +++ b/Aria/src/main/java/com/arialyy/aria/core/manager/UTEFactory.java @@ -18,7 +18,7 @@ package com.arialyy.aria.core.manager; import android.text.TextUtils; import com.arialyy.aria.core.upload.UploadEntity; import com.arialyy.aria.core.upload.UploadTaskEntity; -import com.arialyy.aria.core.upload.UploadTaskWrapper; +import com.arialyy.aria.core.upload.wrapper.UTEWrapper; import com.arialyy.aria.orm.DbEntity; import com.arialyy.aria.util.Regular; import java.util.List; @@ -45,8 +45,8 @@ class UTEFactory implements INormalTEFactory { } private UploadTaskEntity create(UploadEntity entity) { - List wrapper = - DbEntity.findRelationData(UploadTaskWrapper.class, "UploadTaskEntity.key=?", + List wrapper = + DbEntity.findRelationData(UTEWrapper.class, "UploadTaskEntity.key=?", entity.getFilePath()); if (wrapper != null && !wrapper.isEmpty()) { diff --git a/Aria/src/main/java/com/arialyy/aria/core/upload/uploader/HttpThreadTask.java b/Aria/src/main/java/com/arialyy/aria/core/upload/uploader/HttpThreadTask.java index a46feac4..833b28bd 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/upload/uploader/HttpThreadTask.java +++ b/Aria/src/main/java/com/arialyy/aria/core/upload/uploader/HttpThreadTask.java @@ -15,6 +15,7 @@ */ package com.arialyy.aria.core.upload.uploader; +import android.util.Log; import com.arialyy.aria.core.common.AbsThreadTask; import com.arialyy.aria.core.common.StateConstance; import com.arialyy.aria.core.common.SubThreadConfig; diff --git a/Aria/src/main/java/com/arialyy/aria/core/upload/UploadTaskWrapper.java b/Aria/src/main/java/com/arialyy/aria/core/upload/wrapper/UTEWrapper.java similarity index 87% rename from Aria/src/main/java/com/arialyy/aria/core/upload/UploadTaskWrapper.java rename to Aria/src/main/java/com/arialyy/aria/core/upload/wrapper/UTEWrapper.java index 3eea9148..b5dcede1 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/upload/UploadTaskWrapper.java +++ b/Aria/src/main/java/com/arialyy/aria/core/upload/wrapper/UTEWrapper.java @@ -13,8 +13,10 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.arialyy.aria.core.upload; +package com.arialyy.aria.core.upload.wrapper; +import com.arialyy.aria.core.upload.UploadEntity; +import com.arialyy.aria.core.upload.UploadTaskEntity; import com.arialyy.aria.orm.AbsWrapper; import com.arialyy.aria.orm.annotation.Many; import com.arialyy.aria.orm.annotation.One; @@ -25,7 +27,7 @@ import java.util.List; * Created by laoyuyu on 2018/3/30. */ @Wrapper -public class UploadTaskWrapper extends AbsWrapper { +public class UTEWrapper extends AbsWrapper { @One public UploadEntity entity; diff --git a/Aria/src/main/java/com/arialyy/aria/orm/DBConfig.java b/Aria/src/main/java/com/arialyy/aria/orm/DBConfig.java index 53c104b4..c33d71a3 100644 --- a/Aria/src/main/java/com/arialyy/aria/orm/DBConfig.java +++ b/Aria/src/main/java/com/arialyy/aria/orm/DBConfig.java @@ -16,7 +16,6 @@ package com.arialyy.aria.orm; import android.text.TextUtils; -import com.arialyy.aria.core.ErrorEntity; import com.arialyy.aria.core.download.DownloadEntity; import com.arialyy.aria.core.download.DownloadGroupEntity; import com.arialyy.aria.core.download.DownloadGroupTaskEntity; @@ -52,6 +51,5 @@ class DBConfig { mapping.put("UploadTaskEntity", UploadTaskEntity.class); mapping.put("DownloadEntity", DownloadEntity.class); mapping.put("UploadEntity", UploadEntity.class); - mapping.put("ErrorEntity", ErrorEntity.class); } } diff --git a/Aria/src/main/java/com/arialyy/aria/orm/DatabaseContext.java b/Aria/src/main/java/com/arialyy/aria/orm/DatabaseContext.java index e5aa3292..c606676a 100644 --- a/Aria/src/main/java/com/arialyy/aria/orm/DatabaseContext.java +++ b/Aria/src/main/java/com/arialyy/aria/orm/DatabaseContext.java @@ -19,7 +19,7 @@ import android.content.Context; import android.content.ContextWrapper; import android.database.DatabaseErrorHandler; import android.database.sqlite.SQLiteDatabase; -import android.os.Environment; +import com.arialyy.aria.util.CommonUtil; import java.io.File; import java.io.IOException; @@ -36,52 +36,35 @@ class DatabaseContext extends ContextWrapper { */ @Override public File getDatabasePath(String name) { - //判断是否存在sd卡 - boolean sdExist = android.os.Environment.MEDIA_MOUNTED.equals( - android.os.Environment.getExternalStorageState()); - if (!sdExist) { - return null; - } else { - //获取sd卡路径 - File file = getBaseContext().getExternalFilesDir(null); - String dbDir; - if (file != null) { - dbDir = file.getPath() + "/"; - } else { - dbDir = Environment.getExternalStorageDirectory().getPath() - + "/Android/data/" - + getBaseContext().getPackageName() - + "/files/"; - } + String dbDir = CommonUtil.getAppPath(getBaseContext()); - dbDir += "DB";//数据库所在目录 - String dbPath = dbDir + "/" + name;//数据库路径 - //判断目录是否存在,不存在则创建该目录 - File dirFile = new File(dbDir); - if (!dirFile.exists()) { - dirFile.mkdirs(); - } + dbDir += "DB";//数据库所在目录 + String dbPath = dbDir + "/" + name;//数据库路径 + //判断目录是否存在,不存在则创建该目录 + File dirFile = new File(dbDir); + if (!dirFile.exists()) { + dirFile.mkdirs(); + } - //数据库文件是否创建成功 - boolean isFileCreateSuccess = false; - //判断文件是否存在,不存在则创建该文件 - File dbFile = new File(dbPath); - if (!dbFile.exists()) { - try { - isFileCreateSuccess = dbFile.createNewFile();//创建文件 - } catch (IOException e) { - e.printStackTrace(); - } - } else { - isFileCreateSuccess = true; + //数据库文件是否创建成功 + boolean isFileCreateSuccess = false; + //判断文件是否存在,不存在则创建该文件 + File dbFile = new File(dbPath); + if (!dbFile.exists()) { + try { + isFileCreateSuccess = dbFile.createNewFile();//创建文件 + } catch (IOException e) { + e.printStackTrace(); } + } else { + isFileCreateSuccess = true; + } - //返回数据库文件对象 - if (isFileCreateSuccess) { - return dbFile; - } else { - return null; - } + //返回数据库文件对象 + if (isFileCreateSuccess) { + return dbFile; + } else { + return null; } } diff --git a/Aria/src/main/java/com/arialyy/aria/orm/SqlHelper.java b/Aria/src/main/java/com/arialyy/aria/orm/SqlHelper.java index f4560755..3ddcfdbd 100644 --- a/Aria/src/main/java/com/arialyy/aria/orm/SqlHelper.java +++ b/Aria/src/main/java/com/arialyy/aria/orm/SqlHelper.java @@ -22,7 +22,6 @@ import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.text.TextUtils; import com.arialyy.aria.util.ALog; -import com.arialyy.aria.util.CommonUtil; import java.lang.reflect.Field; import java.util.List; import java.util.Set; @@ -193,7 +192,7 @@ final class SqlHelper extends SQLiteOpenHelper { update.insertData(db, entity); } } catch (Exception e) { - ALog.e(TAG, CommonUtil.getPrintException(e)); + ALog.e(TAG, ALog.getExceptionString(e)); } } diff --git a/Aria/src/main/java/com/arialyy/aria/util/AriaCrashHandler.java b/Aria/src/main/java/com/arialyy/aria/util/AriaCrashHandler.java new file mode 100644 index 00000000..20a17483 --- /dev/null +++ b/Aria/src/main/java/com/arialyy/aria/util/AriaCrashHandler.java @@ -0,0 +1,73 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; + +/** + * Created by Aria.Lao on 2017/10/25. + * 程序异常日志捕获器 + */ +public class AriaCrashHandler implements Thread.UncaughtExceptionHandler { + private Thread.UncaughtExceptionHandler mDefaultHandler; + private ExecutorService mExecutorService; + + public AriaCrashHandler() { + mDefaultHandler = Thread.getDefaultUncaughtExceptionHandler(); + mExecutorService = Executors.newSingleThreadExecutor(); + } + + @Override + public void uncaughtException(Thread thread, Throwable ex) { + ALog.d(thread.getName(), ex.getLocalizedMessage()); + handleException(thread.getName(), ex); + try { + Thread.sleep(1000); + } catch (InterruptedException e) { + e.printStackTrace(); + } finally { + mDefaultHandler.uncaughtException(thread, ex); + exit(); + } + } + + /** + * 处理异常 + */ + private void handleException(final String name, final Throwable ex) { + if (ex == null) { + return; + } + + mExecutorService.execute(new Runnable() { + @Override + public void run() { + ErrorHelp.saveError(name, "", ALog.getExceptionString(ex)); + } + }); + } + + /** + * 退出当前应用 + */ + private void exit() { + android.os.Process.killProcess(android.os.Process.myPid()); + System.exit(1); + } +} \ No newline at end of file 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 131a3a4f..0ecd9869 100644 --- a/Aria/src/main/java/com/arialyy/aria/util/CommonUtil.java +++ b/Aria/src/main/java/com/arialyy/aria/util/CommonUtil.java @@ -20,6 +20,7 @@ import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.net.Uri; +import android.os.Environment; import android.text.TextUtils; import android.util.Base64; import com.arialyy.aria.core.AriaManager; @@ -68,6 +69,33 @@ import java.util.regex.Pattern; public class CommonUtil { private static final String TAG = "CommonUtil"; + /** + * 获取sdcard app的缓存目录 + * + * @return "/mnt/sdcard/Android/data/{package_name}/files/" + */ + public static String getAppPath(Context context) { + //判断是否存在sd卡 + boolean sdExist = android.os.Environment.MEDIA_MOUNTED.equals( + android.os.Environment.getExternalStorageState()); + if (!sdExist) { + return null; + } else { + //获取sd卡路径 + File file = context.getExternalFilesDir(null); + String dir; + if (file != null) { + dir = file.getPath() + "/"; + } else { + dir = Environment.getExternalStorageDirectory().getPath() + + "/Android/data/" + + context.getPackageName() + + "/files/"; + } + return dir; + } + } + /** * 获取map泛型类型 * @@ -105,6 +133,7 @@ public class CommonUtil { * @param list list类型字段 * @return 泛型类型 */ + public static Class getListParamType(Field list) { Class type = list.getType(); if (!type.isAssignableFrom(List.class)) { @@ -788,34 +817,6 @@ public class CommonUtil { } } - /** - * 设置打印的异常格式 - */ - public static String getPrintException(Throwable ex) { - if (ex == null) return ""; - StringBuilder err = new StringBuilder(); - err.append("ExceptionDetailed:\n"); - err.append("====================Exception Info====================\n"); - err.append(ex.toString()); - err.append("\n"); - StackTraceElement[] stack = ex.getStackTrace(); - for (StackTraceElement stackTraceElement : stack) { - err.append(stackTraceElement.toString()).append("\n"); - } - Throwable cause = ex.getCause(); - if (cause != null) { - err.append("【Caused by】: "); - err.append(cause.toString()); - err.append("\n"); - StackTraceElement[] stackTrace = cause.getStackTrace(); - for (StackTraceElement stackTraceElement : stackTrace) { - err.append(stackTraceElement.toString()).append("\n"); - } - } - err.append("==================================================="); - return err.toString(); - } - /** * 通过文件名获取下载配置文件路径 * diff --git a/Aria/src/main/java/com/arialyy/aria/util/ErrorHelp.java b/Aria/src/main/java/com/arialyy/aria/util/ErrorHelp.java index 20258930..efd41da1 100644 --- a/Aria/src/main/java/com/arialyy/aria/util/ErrorHelp.java +++ b/Aria/src/main/java/com/arialyy/aria/util/ErrorHelp.java @@ -15,11 +15,14 @@ */ package com.arialyy.aria.util; -import com.arialyy.aria.core.ErrorEntity; -import com.arialyy.aria.core.download.DownloadEntity; -import com.arialyy.aria.core.download.DownloadGroupEntity; -import com.arialyy.aria.core.inf.AbsEntity; -import com.arialyy.aria.core.upload.UploadEntity; +import android.annotation.SuppressLint; +import com.arialyy.aria.core.AriaManager; +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.io.PrintWriter; +import java.text.SimpleDateFormat; +import java.util.Date; /** * Created by Aria.Lao on 2017/8/29. @@ -30,31 +33,71 @@ public class ErrorHelp { /** * 保存错误信息 * - * @param taskType 任务类型 - * @param entity 任务实体 * @param msg 错误提示 * @param ex 异常 */ - // TODO: 2018/4/4 错误应该写在文件中 - @Deprecated - public static void saveError(String taskType, AbsEntity entity, String msg, String ex) { - ErrorEntity errorEntity = new ErrorEntity(); - errorEntity.insertTime = System.currentTimeMillis(); - errorEntity.err = ex; - errorEntity.msg = msg; - errorEntity.taskType = taskType; - String name = ""; - String key = entity.getKey(); - if (entity instanceof DownloadEntity) { - name = ((DownloadEntity) entity).getFileName(); - } else if (entity instanceof DownloadGroupEntity) { - name = ((DownloadGroupEntity) entity).getGroupName(); - } else if (entity instanceof UploadEntity) { - name = ((UploadEntity) entity).getFileName(); + public static void saveError(String tag, String msg, String ex) { + String message = "msg【" + msg + "】\nException:" + ex; + writeLogToFile(tag, message); + } + + /** + * 返回日志路径 + * + * @return "/mnt/sdcard/Android/data/{package_name}/files/log/*" + */ + private static String getLogPath() { + String path = android.os.Environment.getExternalStorageDirectory().getPath() + + "/" + + CommonUtil.getAppPath(AriaManager.APP) + + "/log/AriaCrash_" + + getData("yyyy-MM-dd") + + ".log"; + + File log = new File(path); + if (!log.getParentFile().exists()) { + log.getParentFile().mkdirs(); + } + if (!log.exists()) { + try { + log.createNewFile(); + } catch (IOException e) { + e.printStackTrace(); + } } + return path; + } + + /** + * 把日志记录到文件 + */ + private static int writeLogToFile(String tag, String message) { + StringBuffer stringBuffer = new StringBuffer(); + stringBuffer.append(getData("yyyy-MM-dd HH:mm:ss")); + stringBuffer.append(" "); + stringBuffer.append(tag); + stringBuffer.append(" "); + stringBuffer.append(message); + stringBuffer.append("\n\n"); + PrintWriter writer = null; + try { + writer = new PrintWriter(new FileWriter(getLogPath(), true)); + writer.append(stringBuffer); + writer.flush(); + } catch (Exception e) { + e.printStackTrace(); + } finally { + if (writer != null) { + writer.close(); + } + } + return 0; + } - errorEntity.taskName = name; - errorEntity.key = key; - errorEntity.insert(); + @SuppressLint("SimpleDateFormat") + private static String getData(String format) { + Date date = new Date(System.currentTimeMillis()); + SimpleDateFormat sdf = new SimpleDateFormat(format); + return sdf.format(date); } } diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index a5338d32..ffe769e9 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -15,10 +15,10 @@ android:supportsRtl="true" android:theme="@style/AppTheme.NoActionBar"> - + diff --git a/app/src/main/assets/aria_config.xml b/app/src/main/assets/aria_config.xml index ce92a575..3c437798 100644 --- a/app/src/main/assets/aria_config.xml +++ b/app/src/main/assets/aria_config.xml @@ -1,6 +1,14 @@ + + + + + + + + diff --git a/app/src/main/java/com/arialyy/simple/download/SingleTaskActivity.java b/app/src/main/java/com/arialyy/simple/download/SingleTaskActivity.java index d98d94b7..7b163305 100644 --- a/app/src/main/java/com/arialyy/simple/download/SingleTaskActivity.java +++ b/app/src/main/java/com/arialyy/simple/download/SingleTaskActivity.java @@ -72,7 +72,7 @@ public class SingleTaskActivity extends BaseActivity { } @Override public boolean onMenuItemClick(MenuItem item) { - double speed = -1; + int speed = -1; String msg = ""; switch (item.getItemId()) { case R.id.help: @@ -83,19 +83,19 @@ public class SingleTaskActivity extends BaseActivity { showMsgDialog("tip", msg); break; case R.id.speed_0: - speed = 0.0; + speed = 0; break; case R.id.speed_128: - speed = 128.0; + speed = 128; break; case R.id.speed_256: - speed = 256.0; + speed = 256; break; case R.id.speed_512: - speed = 512.0; + speed = 512; break; case R.id.speed_1m: - speed = 1024.0; + speed = 1024; break; } if (speed > -1) { diff --git a/app/src/main/java/com/arialyy/simple/test/AnyRunActivity.java b/app/src/main/java/com/arialyy/simple/test/AnyRunActivity.java index c15d99be..8a7c15fd 100644 --- a/app/src/main/java/com/arialyy/simple/test/AnyRunActivity.java +++ b/app/src/main/java/com/arialyy/simple/test/AnyRunActivity.java @@ -2,6 +2,7 @@ package com.arialyy.simple.test; import android.os.Bundle; import android.view.View; +import com.arialyy.aria.core.Aria; import com.arialyy.simple.R; import com.arialyy.simple.base.BaseActivity; import com.arialyy.simple.databinding.ActivityTestBinding; @@ -12,6 +13,8 @@ import com.arialyy.simple.databinding.ActivityTestBinding; public class AnyRunActivity extends BaseActivity { AnyRunnModule module; + String[] urls; + int index = 0; @Override protected int setLayoutId() { return R.layout.activity_test; @@ -19,14 +22,20 @@ public class AnyRunActivity extends BaseActivity { @Override protected void init(Bundle savedInstanceState) { super.init(savedInstanceState); + Aria.init(this); mBar.setVisibility(View.GONE); module = new AnyRunnModule(this); + urls = getResources().getStringArray(R.array.group_urls); } public void onClick(View view) { switch (view.getId()) { case R.id.start: - module.start(); + //module.start(); + if (index < urls.length) { + module.start(urls[index]); + index++; + } break; case R.id.stop: module.stop(); diff --git a/app/src/main/java/com/arialyy/simple/test/AnyRunnModule.java b/app/src/main/java/com/arialyy/simple/test/AnyRunnModule.java index 4817e0f0..136b1984 100644 --- a/app/src/main/java/com/arialyy/simple/test/AnyRunnModule.java +++ b/app/src/main/java/com/arialyy/simple/test/AnyRunnModule.java @@ -21,7 +21,7 @@ public class AnyRunnModule { private Context mContext; public AnyRunnModule(Context context) { - Aria.download(context, this).register(); + Aria.download(this).register(); mContext = context; } @@ -63,7 +63,7 @@ public class AnyRunnModule { } void start() { - Aria.download(mContext, this) + Aria.download(this) .load(URL) .addHeader("Accept-Encoding", "gzip, deflate") .setRequestMode(RequestEnum.GET) @@ -72,15 +72,25 @@ public class AnyRunnModule { .start(); } + void start(String url) { + Aria.download(this) + .load(url) + .addHeader("Accept-Encoding", "gzip, deflate") + .setRequestMode(RequestEnum.GET) + .setFilePath(Environment.getExternalStorageDirectory().getPath() + "/ggsg1.apk") + .resetState() + .start(); + } + void stop() { - Aria.download(mContext, this).load(URL).stop(); + Aria.download(this).load(URL).stop(); } void cancel() { - Aria.download(mContext, this).load(URL).cancel(); + Aria.download(this).load(URL).cancel(); } void unRegist() { - Aria.download(mContext, this).unRegister(); + Aria.download(this).unRegister(); } } diff --git a/app/src/main/java/com/arialyy/simple/test/TestActivity.java b/app/src/main/java/com/arialyy/simple/test/TestActivity.java index 75296fba..cb786642 100644 --- a/app/src/main/java/com/arialyy/simple/test/TestActivity.java +++ b/app/src/main/java/com/arialyy/simple/test/TestActivity.java @@ -5,6 +5,7 @@ import android.util.Log; import android.view.View; import com.arialyy.annotations.Upload; import com.arialyy.aria.core.Aria; +import com.arialyy.aria.core.common.RequestEnum; import com.arialyy.aria.core.upload.UploadTask; import com.arialyy.aria.util.CommonUtil; import com.arialyy.simple.R; @@ -20,7 +21,7 @@ public class TestActivity extends BaseActivity { String TAG = "TestActivity"; //String URL = "http://58.210.9.131/tpk/sipgt//TDLYZTGH.tpk"; //chunked 下载 //private final String URL = "ftp://192.168.1.3:21/download//AriaPrj.rar"; - private final String FILE_PATH = "/mnt/sdcard/AriaPrj.rar"; + private final String FILE_PATH = "/mnt/sdcard/SDK_Demo-release.apk"; //private final String URL = "ftp://192.168.29.140:21/aa//你好"; private final String URL = "http://192.168.29.140:5000/upload/"; @@ -73,7 +74,13 @@ public class TestActivity extends BaseActivity { public void onClick(View view) { switch (view.getId()) { case R.id.start: - Aria.upload(this).load(FILE_PATH).setUploadUrl(URL).setExtendField("韩寒哈大双").setAttachment("file").start(); + Aria.upload(this) + .load(FILE_PATH) + .setUploadUrl(URL) + .setRequestMode(RequestEnum.POST) + .setExtendField("韩寒哈大双") + .setAttachment("file") + .start(); break; case R.id.stop: Aria.upload(this).load(FILE_PATH).stop(); diff --git a/aria/src/main/java/com/arialyy/aria/core/Configuration.java b/aria/src/main/java/com/arialyy/aria/core/Configuration.java index 1b4b060e..aa527b1e 100644 --- a/aria/src/main/java/com/arialyy/aria/core/Configuration.java +++ b/aria/src/main/java/com/arialyy/aria/core/Configuration.java @@ -20,7 +20,9 @@ import com.arialyy.aria.core.common.QueueMod; 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 com.arialyy.aria.util.ErrorHelp; import java.io.File; import java.lang.reflect.Field; import java.lang.reflect.Modifier; @@ -32,14 +34,147 @@ import java.util.Properties; * 信息配置 */ class Configuration { + static final String TAG = "Configuration"; static final String DOWNLOAD_CONFIG_FILE = "/Aria/DownloadConfig.properties"; static final String UPLOAD_CONFIG_FILE = "/Aria/UploadConfig.properties"; + static final String APP_CONFIG_FILE = "/Aria/AppConfig.properties"; static final String XML_FILE = "/Aria/aria_config.xml"; + static final int TYPE_DOWNLOAD = 1; + static final int TYPE_UPLOAD = 2; + static final int TYPE_APP = 3; + + abstract static class BaseConfig { + + /** + * 类型 + * + * @return {@link #TYPE_DOWNLOAD}、{@link #TYPE_UPLOAD}、{@link #TYPE_APP} + */ + abstract int getType(); + + /** + * 加载配置 + */ + void loadConfig() { + String path = null; + Class clazz = null; + switch (getType()) { + case TYPE_DOWNLOAD: + path = DOWNLOAD_CONFIG_FILE; + clazz = DownloadConfig.class; + break; + case TYPE_UPLOAD: + path = UPLOAD_CONFIG_FILE; + clazz = UploadConfig.class; + break; + case TYPE_APP: + path = APP_CONFIG_FILE; + clazz = AppConfig.class; + break; + } + if (TextUtils.isEmpty(path)) { + ALog.e(TAG, "读取配置失败:未知文件类型"); + ErrorHelp.saveError(TAG, "读取配置失败:未知文件类型", ""); + return; + } + + File file = new File(AriaManager.APP.getFilesDir().getPath() + path); + if (file.exists()) { + Properties properties = CommonUtil.loadConfig(file); + List fields = CommonUtil.getAllFields(clazz); + try { + for (Field field : fields) { + int m = field.getModifiers(); + String fileName = field.getName(); + if (fileName.equals("oldMaxTaskNum") + || field.isSynthetic() + || Modifier.isFinal(m) + || Modifier.isStatic(m) + || fileName.equals("shadow$_klass_") + || fileName.equals("shadow$_monitor_")) { + continue; + } + field.setAccessible(true); + String value = properties.getProperty(field.getName()); + if (TextUtils.isEmpty(value) || value.equalsIgnoreCase("null")) continue; + Class type = field.getType(); + if (type == String.class) { + field.set(this, value); + } else if (type == int.class || type == Integer.class) { + if (fileName.equalsIgnoreCase("maxSpeed")) { //兼容以前版本,以前maxSpeed是double类型的 + Double d = Double.parseDouble(value); + field.setInt(this, (int) d.doubleValue()); + } else { + 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) { + if (TextUtils.isEmpty(value)) { + value = "0.0"; + } + 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(); + } + } + } + + /** + * 保存key + */ + void saveKey(String key, String value) { + 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); + properties.setProperty(key, value); + CommonUtil.saveConfig(file, properties); + } + } + + /** + * 保存配置 + */ + void saveAll() { + List fields = CommonUtil.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 (field.isSynthetic() + || Modifier.isFinal(m) + || Modifier.isStatic(m) + || field.getName().equals("shadow$_klass_") + || field.getName().equals("shadow$_monitor_")) { + continue; + } + field.setAccessible(true); + properties.setProperty(field.getName(), field.get(this) + ""); + } + CommonUtil.saveConfig(file, properties); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } + } + } /** - * 通用配置 + * 通用任务配置 */ - public static class BaseConfig { + abstract static class BaseTaskConfig extends BaseConfig { /** * 进度刷新间隔,默认1秒 */ @@ -88,7 +223,7 @@ class Configuration { * * @param updateInterval 不能小于0 */ - public BaseConfig setUpdateInterval(long updateInterval) { + public BaseTaskConfig setUpdateInterval(long updateInterval) { if (updateInterval <= 0) { ALog.w("Configuration", "进度更新间隔不能小于0"); return this; @@ -102,7 +237,7 @@ class Configuration { return queueMod; } - public BaseConfig setQueueMod(String queueMod) { + public BaseTaskConfig setQueueMod(String queueMod) { this.queueMod = queueMod; saveKey("queueMod", queueMod); return this; @@ -116,9 +251,9 @@ class Configuration { return reTryNum; } - public BaseConfig setReTryNum(int reTryNum) { + public BaseTaskConfig setReTryNum(int reTryNum) { this.reTryNum = reTryNum; - saveKey("reTryNum", reTryNum + ""); + saveKey("reTryNum", String.valueOf(reTryNum)); return this; } @@ -126,9 +261,9 @@ class Configuration { return reTryInterval; } - public BaseConfig setReTryInterval(int reTryInterval) { + public BaseTaskConfig setReTryInterval(int reTryInterval) { this.reTryInterval = reTryInterval; - saveKey("reTryInterval", reTryInterval + ""); + saveKey("reTryInterval", String.valueOf(reTryInterval)); return this; } @@ -136,9 +271,9 @@ class Configuration { return isConvertSpeed; } - public BaseConfig setConvertSpeed(boolean convertSpeed) { + public BaseTaskConfig setConvertSpeed(boolean convertSpeed) { isConvertSpeed = convertSpeed; - saveKey("isConvertSpeed", isConvertSpeed + ""); + saveKey("isConvertSpeed", String.valueOf(isConvertSpeed)); return this; } @@ -146,108 +281,17 @@ class Configuration { return connectTimeOut; } - public BaseConfig setConnectTimeOut(int connectTimeOut) { + public BaseTaskConfig setConnectTimeOut(int connectTimeOut) { this.connectTimeOut = connectTimeOut; - saveKey("connectTimeOut", connectTimeOut + ""); + saveKey("connectTimeOut", String.valueOf(connectTimeOut)); return this; } - - /** - * 保存key - */ - void saveKey(String key, String value) { - 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); - properties.setProperty(key, value); - CommonUtil.saveConfig(file, properties); - } - } - - /** - * 加载配置 - */ - 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 fields = - CommonUtil.getAllFields(isDownload ? DownloadConfig.class : UploadConfig.class); - try { - for (Field field : fields) { - int m = field.getModifiers(); - String fileName = field.getName(); - if (fileName.equals("oldMaxTaskNum") - || field.isSynthetic() - || Modifier.isFinal(m) - || Modifier.isStatic(m) - || fileName.equals("shadow$_klass_") - || fileName.equals("shadow$_monitor_")) { - continue; - } - field.setAccessible(true); - String value = properties.getProperty(field.getName()); - if (TextUtils.isEmpty(value) || value.equalsIgnoreCase("null")) continue; - 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) { - if (TextUtils.isEmpty(value)) { - value = "0.0"; - } - 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(); - } - } - } - - /** - * 保存配置 - */ - void saveAll() { - List fields = CommonUtil.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(); - } - } } /** * 下载配置 */ - public static class DownloadConfig extends BaseConfig { + public static class DownloadConfig extends BaseTaskConfig { /** * 设置IO流读取时间,单位为毫秒,默认20000毫秒,该时间不能少于10000毫秒 */ @@ -272,12 +316,12 @@ class Configuration { /** * 设置最大下载速度,单位:kb, 为0表示不限速 */ - double msxSpeed = 0.0; + int msxSpeed = 0; public DownloadConfig setMaxTaskNum(int maxTaskNum) { oldMaxTaskNum = this.maxTaskNum; this.maxTaskNum = maxTaskNum; - saveKey("maxTaskNum", maxTaskNum + ""); + saveKey("maxTaskNum", String.valueOf(maxTaskNum)); DownloadTaskQueue.getInstance().setMaxTaskNum(maxTaskNum); return this; } @@ -286,11 +330,11 @@ class Configuration { return iOTimeOut; } - public double getMsxSpeed() { + public int getMsxSpeed() { return msxSpeed; } - public DownloadConfig setMsxSpeed(double msxSpeed) { + public DownloadConfig setMsxSpeed(int msxSpeed) { this.msxSpeed = msxSpeed; saveKey("msxSpeed", String.valueOf(msxSpeed)); DownloadTaskQueue.getInstance().setMaxSpeed(msxSpeed); @@ -304,7 +348,7 @@ class Configuration { public DownloadConfig setIOTimeOut(int iOTimeOut) { this.iOTimeOut = iOTimeOut; - saveKey("iOTimeOut", iOTimeOut + ""); + saveKey("iOTimeOut", String.valueOf(iOTimeOut)); return this; } @@ -314,7 +358,7 @@ class Configuration { public DownloadConfig setBuffSize(int buffSize) { this.buffSize = buffSize; - saveKey("buffSize", buffSize + ""); + saveKey("buffSize", String.valueOf(buffSize)); return this; } @@ -356,23 +400,26 @@ class Configuration { } return INSTANCE; } + + @Override int getType() { + return TYPE_DOWNLOAD; + } } /** * 上传配置 */ - public static class UploadConfig extends BaseConfig { + public static class UploadConfig extends BaseTaskConfig { + private static UploadConfig INSTANCE = null; private UploadConfig() { loadConfig(); } - private static UploadConfig INSTANCE = null; - public UploadConfig setMaxTaskNum(int maxTaskNum) { oldMaxTaskNum = this.maxTaskNum; this.maxTaskNum = maxTaskNum; - saveKey("maxTaskNum", maxTaskNum + ""); + saveKey("maxTaskNum", String.valueOf(maxTaskNum)); UploadTaskQueue.getInstance().setMaxTaskNum(maxTaskNum); return this; } @@ -385,5 +432,71 @@ class Configuration { } return INSTANCE; } + + @Override int getType() { + return TYPE_UPLOAD; + } + } + + /** + * 应用配置 + */ + public static class AppConfig extends BaseConfig { + private static AppConfig INSTANCE = null; + /** + * 是否使用{@link AriaCrashHandler}来捕获异常 + * {@code true} 使用;{@code false} 不使用 + */ + Boolean useAriaCrashHandler; + + /** + * 设置Aria的日志级别 + * + * {@link ALog#LOG_LEVEL_VERBOSE} + */ + int logLevel; + + AppConfig() { + loadConfig(); + } + + static AppConfig getInstance() { + if (INSTANCE == null) { + synchronized (AppConfig.class) { + INSTANCE = new AppConfig(); + } + } + return INSTANCE; + } + + public AppConfig setLogLevel(int level) { + this.logLevel = level; + ALog.LOG_LEVEL = level; + saveKey("logLevel", String.valueOf(logLevel)); + return this; + } + + public int getLogLevel() { + return logLevel; + } + + public Boolean getUseAriaCrashHandler() { + return useAriaCrashHandler; + } + + public AppConfig setUseAriaCrashHandler(Boolean useAriaCrashHandler) { + this.useAriaCrashHandler = useAriaCrashHandler; + saveKey("useAriaCrashHandler", String.valueOf(useAriaCrashHandler)); + if (useAriaCrashHandler) { + Thread.setDefaultUncaughtExceptionHandler(new AriaCrashHandler()); + } else { + Thread.setDefaultUncaughtExceptionHandler(null); + } + return this; + } + + @Override int getType() { + return TYPE_APP; + } } }