配置文件新增app设置参数,添加异常捕获日志

pull/330/head
laoyuyu 7 years ago
parent 4d55b95a8d
commit a7ca196258
  1. 66
      Aria/src/main/java/com/arialyy/aria/core/Aria.java
  2. 50
      Aria/src/main/java/com/arialyy/aria/core/AriaManager.java
  3. 146
      Aria/src/main/java/com/arialyy/aria/core/ConfigHelper.java
  4. 77
      Aria/src/main/java/com/arialyy/aria/core/ErrorEntity.java
  5. 44
      Aria/src/main/java/com/arialyy/aria/core/command/normal/ResumeAllCmd.java
  6. 44
      Aria/src/main/java/com/arialyy/aria/core/command/normal/StartCmd.java
  7. 4
      Aria/src/main/java/com/arialyy/aria/core/common/AbsThreadTask.java
  8. 8
      Aria/src/main/java/com/arialyy/aria/core/download/DownloadReceiver.java
  9. 1
      Aria/src/main/java/com/arialyy/aria/core/download/DownloadTask.java
  10. 4
      Aria/src/main/java/com/arialyy/aria/core/download/downloader/Downloader.java
  11. 4
      Aria/src/main/java/com/arialyy/aria/core/download/downloader/FtpDirDownloadUtil.java
  12. 2
      Aria/src/main/java/com/arialyy/aria/core/download/downloader/HttpFileInfoThread.java
  13. 3
      Aria/src/main/java/com/arialyy/aria/core/download/downloader/HttpThreadTask.java
  14. 4
      Aria/src/main/java/com/arialyy/aria/core/download/downloader/SimpleDownloadUtil.java
  15. 2
      Aria/src/main/java/com/arialyy/aria/core/download/wrapper/DGEWrapper.java
  16. 2
      Aria/src/main/java/com/arialyy/aria/core/download/wrapper/DGSTEWrapper.java
  17. 8
      Aria/src/main/java/com/arialyy/aria/core/download/wrapper/DGTEWrapper.java
  18. 2
      Aria/src/main/java/com/arialyy/aria/core/download/wrapper/DTEWrapper.java
  19. 16
      Aria/src/main/java/com/arialyy/aria/core/manager/DGTEFactory.java
  20. 4
      Aria/src/main/java/com/arialyy/aria/core/manager/DTEFactory.java
  21. 6
      Aria/src/main/java/com/arialyy/aria/core/manager/UTEFactory.java
  22. 1
      Aria/src/main/java/com/arialyy/aria/core/upload/uploader/HttpThreadTask.java
  23. 6
      Aria/src/main/java/com/arialyy/aria/core/upload/wrapper/UTEWrapper.java
  24. 2
      Aria/src/main/java/com/arialyy/aria/orm/DBConfig.java
  25. 69
      Aria/src/main/java/com/arialyy/aria/orm/DatabaseContext.java
  26. 3
      Aria/src/main/java/com/arialyy/aria/orm/SqlHelper.java
  27. 73
      Aria/src/main/java/com/arialyy/aria/util/AriaCrashHandler.java
  28. 57
      Aria/src/main/java/com/arialyy/aria/util/CommonUtil.java
  29. 95
      Aria/src/main/java/com/arialyy/aria/util/ErrorHelp.java
  30. 4
      app/src/main/AndroidManifest.xml
  31. 8
      app/src/main/assets/aria_config.xml
  32. 12
      app/src/main/java/com/arialyy/simple/download/SingleTaskActivity.java
  33. 11
      app/src/main/java/com/arialyy/simple/test/AnyRunActivity.java
  34. 20
      app/src/main/java/com/arialyy/simple/test/AnyRunnModule.java
  35. 11
      app/src/main/java/com/arialyy/simple/test/TestActivity.java
  36. 341
      aria/src/main/java/com/arialyy/aria/core/Configuration.java

@ -44,6 +44,24 @@ import com.arialyy.aria.core.upload.UploadReceiver;
* .start();
* </code>
* </pre>
*
* 如果你需要在ActivityServiceApplicationDialogFragmentFragmentPopupWindowDialog
* 之外的java中使用Aria那么你应该在Application或Activity初始化的时候调用{@link #init(Context)}对Aria进行初始化
* 然后才能使用{@link #download(Object)}{@link #upload(Object)}
*
* <pre>
* <code>
* Aria.init(getContext());
*
* Aria.download(this)
* .load(URL) //下载地址,必填
* //文件保存路径,必填
* .setDownloadPath(Environment.getExternalStorageDirectory().getPath() + "/test.apk")
* .start();
*
* </code>
*
* </pre>
*/
@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如果你需要在ActivityServiceApplicationDialogFragmentFragmentPopupWindowDialog
* 之外的java中使用Aria那么你应该在Application或Activity初始化的时候调用本方法对Aria进行初始化
* 只需要初始化一次就可以
* {@link #download(Object)}{@link #upload(Object)}
*/
public static AriaManager init(Context context) {
return AriaManager.getInstance(context);
}
}

@ -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<ICmd> 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<String, AbsReceiver> 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<String, AbsReceiver> 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<ErrorEntity> 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();

@ -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();
}
}

@ -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
+ '\''
+ '}';
}
}

@ -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<T extends AbsTaskEntity> extends AbsNormalCmd<T> {
* @param type {@code 1}单任务下载任务{@code 2}任务组下载任务{@code 3} 单任务上传任务
*/
private List<AbsTaskEntity> findTaskData(int type) {
// TODO: 2018/4/20 需要测试
List<AbsTaskEntity> tempList = new ArrayList<>();
switch (type) {
case 1:
List<DownloadTaskEntity> dTaskEntity =
DbEntity.findDatas(DownloadTaskEntity.class, "isGroupTask=?", "false");
if (dTaskEntity != null && !dTaskEntity.isEmpty()) {
tempList.addAll(dTaskEntity);
if (type == 1) {
List<DTEWrapper> 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<DownloadGroupTaskEntity> groupTask =
DbEntity.findAllData(DownloadGroupTaskEntity.class);
if (groupTask != null && !groupTask.isEmpty()) {
tempList.addAll(groupTask);
}
} else if (type == 2) {
List<DGTEWrapper> 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<UploadTaskEntity> uTaskEntity =
DbEntity.findDatas(UploadTaskEntity.class, "isGroupTask=?", "false");
if (uTaskEntity != null && !uTaskEntity.isEmpty()) {
tempList.addAll(uTaskEntity);
}
} else if (type == 3) {
List<UTEWrapper> wrappers = DbEntity.findRelationData(UTEWrapper.class,
"UploadTaskEntity.state!=?", "1");
if (wrappers != null && !wrappers.isEmpty()) {
for (UTEWrapper w : wrappers) {
tempList.add(w.taskEntity);
}
break;
}
}
return tempList;
}

@ -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<T extends AbsTaskEntity> extends AbsNormalCmd<T> {
}
private List<AbsTaskEntity> findWaitData(int type) {
// TODO: 2018/4/20 需要测试
List<AbsTaskEntity> waitList = new ArrayList<>();
switch (type) {
case 1:
List<DownloadTaskEntity> dEntity =
DbEntity.findDatas(DownloadTaskEntity.class, "groupName=? and state=?", "", "3");
if (dEntity != null && !dEntity.isEmpty()) {
waitList.addAll(dEntity);
if (type == 1) {
List<DTEWrapper> 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<DownloadGroupTaskEntity> dgEntity =
DbEntity.findDatas(DownloadGroupTaskEntity.class, "state=?", "3");
if (dgEntity != null && !dgEntity.isEmpty()) {
waitList.addAll(dgEntity);
}
} else if (type == 2) {
List<DGTEWrapper> 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<UploadTaskEntity> uEntity =
DbEntity.findDatas(UploadTaskEntity.class, "state=?", "3");
if (uEntity != null && !uEntity.isEmpty()) {
waitList.addAll(uEntity);
}
} else if (type == 3) {
List<UTEWrapper> wrappers = DbEntity.findRelationData(UTEWrapper.class,
"UploadTaskEntity.state=?", "3");
if (wrappers != null && !wrappers.isEmpty()) {
for (UTEWrapper w : wrappers) {
waitList.add(w.taskEntity);
}
break;
}
}
return waitList;
}

@ -200,7 +200,7 @@ public abstract class AbsThreadTask<ENTITY extends AbsNormalEntity, TASK_ENTITY
synchronized (AriaManager.LOCK) {
try {
if (ex != null) {
ALog.e(TAG, msg + "\n" + CommonUtil.getPrintException(ex));
ALog.e(TAG, msg + "\n" + ALog.getExceptionString(ex));
} else {
ALog.e(TAG, msg);
}
@ -210,7 +210,7 @@ public abstract class AbsThreadTask<ENTITY extends AbsNormalEntity, TASK_ENTITY
} else {
ALog.e(TAG, "任务【" + mConfig.TEMP_FILE.getName() + "】执行失败");
mListener.onFail(true);
ErrorHelp.saveError(mTaskType, mEntity, "", CommonUtil.getPrintException(ex));
ErrorHelp.saveError(TAG, "", ALog.getExceptionString(ex));
}
} catch (IOException e) {
e.printStackTrace();

@ -22,7 +22,7 @@ import com.arialyy.aria.core.command.ICmd;
import com.arialyy.aria.core.command.normal.CancelAllCmd;
import com.arialyy.aria.core.command.normal.NormalCmdFactory;
import com.arialyy.aria.core.common.ProxyHelper;
import com.arialyy.aria.core.download.wrapper.DGroupEntityWrapper;
import com.arialyy.aria.core.download.wrapper.DGEWrapper;
import com.arialyy.aria.core.inf.AbsEntity;
import com.arialyy.aria.core.inf.AbsReceiver;
import com.arialyy.aria.core.inf.AbsTarget;
@ -50,7 +50,7 @@ public class DownloadReceiver extends AbsReceiver {
*
* @param maxSpeed 为0表示不限速
*/
@Deprecated public void setMaxSpeed(double maxSpeed) {
@Deprecated public void setMaxSpeed(int maxSpeed) {
AriaManager.getInstance(AriaManager.APP).getDownloadConfig().setMsxSpeed(maxSpeed);
}
@ -350,12 +350,12 @@ public class DownloadReceiver extends AbsReceiver {
* @return 如果没有任务组列表则返回null
*/
public List<DownloadGroupEntity> getGroupTaskList() {
List<DGroupEntityWrapper> wrappers = DbEntity.findRelationData(DGroupEntityWrapper.class);
List<DGEWrapper> wrappers = DbEntity.findRelationData(DGEWrapper.class);
if (wrappers == null || wrappers.isEmpty()) {
return null;
}
List<DownloadGroupEntity> entities = new ArrayList<>();
for (DGroupEntityWrapper wrapper : wrappers) {
for (DGEWrapper wrapper : wrappers) {
entities.add(wrapper.groupEntity);
}
return entities;

@ -78,6 +78,7 @@ public class DownloadTask extends AbsNormalTask<DownloadTaskEntity> {
/**
* 是否真正下载
*
* @return {@code true} 真正下载
*/
@Override public boolean isRunning() {

@ -87,7 +87,7 @@ class Downloader extends AbsFileer<DownloadEntity, DownloadTaskEntity> {
+ "】\n【filePath:"
+ mEntity.getDownloadPath()
+ "】\n"
+ CommonUtil.getPrintException(e));
+ ALog.getExceptionString(e));
} finally {
if (file != null) {
try {
@ -116,6 +116,6 @@ class Downloader extends AbsFileer<DownloadEntity, DownloadTaskEntity> {
ALog.e(TAG, errorMsg);
mConstance.isRunning = false;
mListener.onFail(false);
ErrorHelp.saveError("", mEntity, "", errorMsg);
ErrorHelp.saveError(TAG, "", errorMsg);
}
}

@ -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();
}

@ -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();

@ -116,11 +116,12 @@ final class HttpThreadTask extends AbsThreadTask<DownloadEntity, DownloadTaskEnt
/**
* 读取chunk模式的文件流
*
* @deprecated 暂时先这样处理无chun
*/
private void readChunk(InputStream is, RandomAccessFile file)
throws IOException, InterruptedException {
readNormal(is, file);
readNormal(is, file);
}
/**

@ -29,7 +29,7 @@ import com.arialyy.aria.util.ErrorHelp;
* D_HTTP\FTP单任务下载工具
*/
public class SimpleDownloadUtil implements IUtil, Runnable {
private static final String TAG = "SimpleDownloadUtil";
private String TAG = "SimpleDownloadUtil";
private IDownloadListener mListener;
private Downloader mDownloader;
private DownloadTaskEntity mTaskEntity;
@ -86,7 +86,7 @@ public class SimpleDownloadUtil implements IUtil, Runnable {
private void failDownload(String msg, boolean needRetry) {
mListener.onFail(needRetry);
ErrorHelp.saveError("HTTP_DOWNLOAD", mTaskEntity.getEntity(), msg, "");
ErrorHelp.saveError(TAG, msg, "");
}
@Override public void run() {

@ -28,7 +28,7 @@ import java.util.List;
* 任务组实体和子任务实体的关系
*/
@Wrapper
public class DGroupEntityWrapper extends AbsWrapper {
public class DGEWrapper extends AbsWrapper {
@One
public DownloadGroupEntity groupEntity;

@ -28,7 +28,7 @@ import java.util.List;
* 任务组任务实体和任务组任务实体的子任务实体对应关系
*/
@Wrapper
public class DGroupSubTaskEntityWrapper extends AbsWrapper {
public class DGSTEWrapper extends AbsWrapper {
@One
public DownloadGroupTaskEntity dgTaskEntity;

@ -33,7 +33,7 @@ import java.util.List;
* 任务组实体和任务组任务实体的关系
*/
@Wrapper
public class DGroupTaskEntityWrapper extends AbsWrapper {
public class DGTEWrapper extends AbsWrapper {
@One
public DownloadGroupEntity entity;
@ -47,12 +47,12 @@ public class DGroupTaskEntityWrapper extends AbsWrapper {
taskEntity = (taskEntitys == null || taskEntitys.isEmpty()) ? null : taskEntitys.get(0);
if (taskEntity != null) {
taskEntity.setEntity(entity);
List<DownloadTaskWrapper> subWrappers =
DbEntity.findRelationData(DownloadTaskWrapper.class, "DownloadTaskEntity.groupName=?",
List<DTEWrapper> subWrappers =
DbEntity.findRelationData(DTEWrapper.class, "DownloadTaskEntity.groupName=?",
taskEntity.getKey());
if (subWrappers != null && !subWrappers.isEmpty()) {
List<DownloadTaskEntity> 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);
}

@ -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;

@ -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<DownloadGroupEntity, DownloadGroupTaskE
@Override public DownloadGroupTaskEntity getGTE(String groupName, List<String> urls) {
DownloadGroupEntity entity = createDGroupEntity(groupName, urls);
List<DGroupTaskEntityWrapper> wrapper =
DbEntity.findRelationData(DGroupTaskEntityWrapper.class, "DownloadGroupTaskEntity.key=?",
List<DGTEWrapper> wrapper =
DbEntity.findRelationData(DGTEWrapper.class, "DownloadGroupTaskEntity.key=?",
entity.getGroupName());
DownloadGroupTaskEntity gte;
@ -74,8 +74,8 @@ class DGTEFactory implements IGTEFactory<DownloadGroupEntity, DownloadGroupTaskE
}
@Override public DownloadGroupTaskEntity getFTE(String ftpUrl) {
List<DGroupTaskEntityWrapper> wrapper =
DbEntity.findRelationData(DGroupTaskEntityWrapper.class, "DownloadGroupTaskEntity.key=?",
List<DGTEWrapper> wrapper =
DbEntity.findRelationData(DGTEWrapper.class, "DownloadGroupTaskEntity.key=?",
ftpUrl);
DownloadGroupTaskEntity fte;
@ -130,8 +130,8 @@ class DGTEFactory implements IGTEFactory<DownloadGroupEntity, DownloadGroupTaskE
* 查询任务组实体如果数据库不存在该实体则新创建一个新的任务组实体
*/
private DownloadGroupEntity createDGroupEntity(String groupName, List<String> urls) {
List<DGroupEntityWrapper> wrapper =
DbEntity.findRelationData(DGroupEntityWrapper.class, "DownloadGroupEntity.groupName=?",
List<DGEWrapper> wrapper =
DbEntity.findRelationData(DGEWrapper.class, "DownloadGroupEntity.groupName=?",
groupName);
DownloadGroupEntity groupEntity;

@ -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<DownloadEntity, DownloadTaskEntity>
* 通过下载实体创建任务实体
*/
private DownloadTaskEntity create(DownloadEntity entity) {
List<DownloadTaskWrapper> wrapper = DbEntity.findRelationData(DownloadTaskWrapper.class,
List<DTEWrapper> wrapper = DbEntity.findRelationData(DTEWrapper.class,
"DownloadTaskEntity.key=? and DownloadTaskEntity.isGroupTask='false' and DownloadTaskEntity.url=?",
entity.getDownloadPath(), entity.getUrl());
DownloadTaskEntity taskEntity;

@ -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<UploadEntity, UploadTaskEntity> {
}
private UploadTaskEntity create(UploadEntity entity) {
List<UploadTaskWrapper> wrapper =
DbEntity.findRelationData(UploadTaskWrapper.class, "UploadTaskEntity.key=?",
List<UTEWrapper> wrapper =
DbEntity.findRelationData(UTEWrapper.class, "UploadTaskEntity.key=?",
entity.getFilePath());
if (wrapper != null && !wrapper.isEmpty()) {

@ -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;

@ -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;

@ -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);
}
}

@ -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;
}
}

@ -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));
}
}

@ -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);
}
}

@ -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();
}
/**
* 通过文件名获取下载配置文件路径
*

@ -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);
}
}

@ -15,10 +15,10 @@
android:supportsRtl="true"
android:theme="@style/AppTheme.NoActionBar">
<!--android:name=".MainActivity"-->
<!--android:name=".test.AnyRunActivity"-->
<!--android:name=".test.TestGroupActivity"-->
<!--android:name=".test.TestActivity"-->
<activity
android:name=".test.TestActivity"
android:name=".test.AnyRunActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>

@ -1,6 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<aria>
<app>
<!--是否使用AriaCrashHandler来捕获异常,异常日志保存在:/mnt/sdcard/Android/data/{package_name}/files/log/-->
<useAriaCrashHandler value="true"/>
<!--设置Aria的日志级别,{@link ALog#LOG_LEVEL_VERBOSE}-->
<logLevel value="2"/>
</app>
<!--注意,修改该配置文件中的属性会覆盖代码中所设置的属性-->
<download>

@ -72,7 +72,7 @@ public class SingleTaskActivity extends BaseActivity<ActivitySingleBinding> {
}
@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<ActivitySingleBinding> {
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) {

@ -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<ActivityTestBinding> {
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<ActivityTestBinding> {
@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();

@ -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();
}
}

@ -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<ActivityTestBinding> {
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<ActivityTestBinding> {
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();

@ -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<Field> 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<Field> 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<Field> 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<Field> 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;
}
}
}

Loading…
Cancel
Save