parent
56af75c35c
commit
1a83617045
@ -1,441 +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 android.text.TextUtils; |
||||
import com.arialyy.aria.util.ALog; |
||||
import javax.xml.validation.Validator; |
||||
import org.xml.sax.Attributes; |
||||
import org.xml.sax.SAXException; |
||||
import org.xml.sax.helpers.DefaultHandler; |
||||
|
||||
/** |
||||
* Created by lyy on 2017/5/22. 读取配置文件 |
||||
*/ |
||||
class ConfigHelper extends DefaultHandler { |
||||
private final String TAG = "ConfigHelper"; |
||||
|
||||
private Configuration.DownloadConfig mDownloadConfig = Configuration.getInstance().downloadCfg; |
||||
private Configuration.UploadConfig mUploadConfig = Configuration.getInstance().uploadCfg; |
||||
private Configuration.AppConfig mAppConfig = Configuration.getInstance().appCfg; |
||||
private Configuration.DGroupConfig mDGroupConfig = Configuration.getInstance().dGroupCfg; |
||||
private @ConfigType int mType; |
||||
|
||||
@Override public void startDocument() throws SAXException { |
||||
super.startDocument(); |
||||
} |
||||
|
||||
@Override |
||||
public void startElement(String uri, String localName, String qName, Attributes attributes) |
||||
throws SAXException { |
||||
super.startElement(uri, localName, qName, attributes); |
||||
switch (qName) { |
||||
case "download": |
||||
mType = ConfigType.DOWNLOAD; |
||||
break; |
||||
case "upload": |
||||
mType = ConfigType.UPLOAD; |
||||
break; |
||||
case "app": |
||||
mType = ConfigType.APP; |
||||
break; |
||||
case "dGroup": |
||||
mType = ConfigType.D_GROUP; |
||||
break; |
||||
} |
||||
|
||||
if (mType == ConfigType.DOWNLOAD || mType == ConfigType.UPLOAD || mType == ConfigType.D_GROUP) { |
||||
|
||||
String value = attributes.getValue("value"); |
||||
switch (qName) { |
||||
case "threadNum": |
||||
loadThreadNum(value); |
||||
break; |
||||
case "maxTaskNum": |
||||
loadMaxQueue(value); |
||||
break; |
||||
case "reTryNum": |
||||
loadReTry(value); |
||||
break; |
||||
case "connectTimeOut": |
||||
loadConnectTime(value); |
||||
break; |
||||
case "iOTimeOut": |
||||
loadIOTimeout(value); |
||||
break; |
||||
case "reTryInterval": |
||||
loadReTryInterval(value); |
||||
break; |
||||
case "buffSize": |
||||
loadBuffSize(value); |
||||
break; |
||||
case "ca": |
||||
String caName = attributes.getValue("name"); |
||||
String caPath = attributes.getValue("path"); |
||||
loadCA(caName, caPath); |
||||
break; |
||||
case "convertSpeed": |
||||
loadConvertSpeed(value); |
||||
break; |
||||
case "maxSpeed": |
||||
loadMaxSpeed(value); |
||||
break; |
||||
case "queueMod": |
||||
loadQueueMod(value); |
||||
break; |
||||
case "updateInterval": |
||||
loadUpdateInterval(value); |
||||
break; |
||||
case "notNetRetry": |
||||
loadNotNetRetry(value); |
||||
break; |
||||
case "useBlock": |
||||
loadUseBlock(value); |
||||
break; |
||||
case "subMaxTaskNum": |
||||
loadSubMaxTaskNum(value); |
||||
break; |
||||
case "subReTryNum": |
||||
loadSubReTryNum(value); |
||||
break; |
||||
case "subReTryInterval": |
||||
loadSubReTryInterval(value); |
||||
break; |
||||
} |
||||
} else if (mType == ConfigType.APP) { |
||||
String value = attributes.getValue("value"); |
||||
switch (qName) { |
||||
case "useAriaCrashHandler": |
||||
loadUseAriaCrashHandler(value); |
||||
break; |
||||
case "logLevel": |
||||
loadLogLevel(value); |
||||
break; |
||||
case "netCheck": |
||||
loadNetCheck(value); |
||||
break; |
||||
case "useBroadcast": |
||||
loadUseBroadcast(value); |
||||
break; |
||||
} |
||||
} |
||||
} |
||||
|
||||
private void loadSubReTryInterval(String value) { |
||||
int temp = checkInt(value) ? Integer.parseInt(value) : 2000; |
||||
if (mType == ConfigType.D_GROUP) { |
||||
mDGroupConfig.subReTryInterval = temp; |
||||
} |
||||
} |
||||
|
||||
private void loadSubReTryNum(String value) { |
||||
int temp = checkInt(value) ? Integer.parseInt(value) : 5; |
||||
if (mType == ConfigType.D_GROUP) { |
||||
mDGroupConfig.subReTryNum = temp; |
||||
} |
||||
} |
||||
|
||||
private void loadSubMaxTaskNum(String value) { |
||||
int temp = checkInt(value) ? Integer.parseInt(value) : 3; |
||||
if (mType == ConfigType.D_GROUP) { |
||||
mDGroupConfig.subMaxTaskNum = temp; |
||||
} |
||||
} |
||||
|
||||
private void loadNetCheck(String value) { |
||||
boolean b = checkBoolean(value), temp = false; |
||||
if (b) { |
||||
temp = Boolean.valueOf(value); |
||||
} |
||||
mAppConfig.netCheck = temp; |
||||
} |
||||
|
||||
private void loadUseBroadcast(String value) { |
||||
boolean b = checkBoolean(value), temp = false; |
||||
|
||||
if (b) { |
||||
temp = Boolean.valueOf(value); |
||||
} |
||||
if (mType == ConfigType.APP) { |
||||
mAppConfig.useBroadcast = temp; |
||||
} |
||||
} |
||||
|
||||
private void loadUseBlock(String value) { |
||||
if (mType == ConfigType.DOWNLOAD) { |
||||
mDownloadConfig.useBlock = checkBoolean(value) ? Boolean.valueOf(value) : false; |
||||
} |
||||
} |
||||
|
||||
private void loadNotNetRetry(String value) { |
||||
if (mType == ConfigType.DOWNLOAD) { |
||||
mDownloadConfig.notNetRetry = checkBoolean(value) ? Boolean.valueOf(value) : false; |
||||
} else if (mType == ConfigType.UPLOAD) { |
||||
mUploadConfig.notNetRetry = checkBoolean(value) ? Boolean.valueOf(value) : false; |
||||
} else if (mType == ConfigType.D_GROUP) { |
||||
mDGroupConfig.notNetRetry = checkBoolean(value) ? Boolean.valueOf(value) : false; |
||||
} |
||||
} |
||||
|
||||
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)) { |
||||
mAppConfig.useAriaCrashHandler = Boolean.parseBoolean(value); |
||||
} else { |
||||
ALog.w(TAG, "useAriaCrashHandler【" + value + "】错误"); |
||||
mAppConfig.useAriaCrashHandler = true; |
||||
} |
||||
} |
||||
|
||||
private void loadUpdateInterval(String value) { |
||||
long temp = checkLong(value) ? Long.parseLong(value) : 1000; |
||||
if (mType == ConfigType.DOWNLOAD) { |
||||
mDownloadConfig.updateInterval = temp; |
||||
} else if (mType == ConfigType.UPLOAD) { |
||||
mUploadConfig.updateInterval = temp; |
||||
} else if (mType == ConfigType.D_GROUP) { |
||||
mDGroupConfig.updateInterval = temp; |
||||
} |
||||
} |
||||
|
||||
private void loadQueueMod(String value) { |
||||
String mod = "now"; |
||||
if (!TextUtils.isEmpty(value) && (value.equalsIgnoreCase("now") || value.equalsIgnoreCase( |
||||
"wait"))) { |
||||
mod = value; |
||||
} else if (mType == ConfigType.DOWNLOAD) { |
||||
mDownloadConfig.queueMod = mod; |
||||
} else if (mType == ConfigType.UPLOAD) { |
||||
mUploadConfig.queueMod = mod; |
||||
} else if (mType == ConfigType.D_GROUP) { |
||||
mDGroupConfig.queueMod = mod; |
||||
} |
||||
} |
||||
|
||||
private void loadMaxSpeed(String value) { |
||||
int maxSpeed = checkInt(value) ? Integer.parseInt(value) : 0; |
||||
if (mType == ConfigType.DOWNLOAD) { |
||||
mDownloadConfig.maxSpeed = maxSpeed; |
||||
} else if (mType == ConfigType.UPLOAD) { |
||||
mUploadConfig.maxSpeed = maxSpeed; |
||||
} else if (mType == ConfigType.D_GROUP) { |
||||
mDownloadConfig.maxSpeed = maxSpeed; |
||||
} |
||||
} |
||||
|
||||
private void loadConvertSpeed(String value) { |
||||
boolean open = true; |
||||
if (checkBoolean(value)) { |
||||
open = Boolean.parseBoolean(value); |
||||
} |
||||
|
||||
if (mType == ConfigType.DOWNLOAD) { |
||||
mDownloadConfig.isConvertSpeed = open; |
||||
} else if (mType == ConfigType.UPLOAD) { |
||||
mUploadConfig.isConvertSpeed = open; |
||||
} else if (mType == ConfigType.D_GROUP) { |
||||
mUploadConfig.isConvertSpeed = open; |
||||
} |
||||
} |
||||
|
||||
private void loadReTryInterval(String value) { |
||||
int time = checkInt(value) ? Integer.parseInt(value) : 2 * 1000; |
||||
|
||||
if (time < 2 * 1000) { |
||||
time = 2 * 1000; |
||||
} |
||||
|
||||
if (mType == ConfigType.DOWNLOAD) { |
||||
mDownloadConfig.reTryInterval = time; |
||||
} else if (mType == ConfigType.UPLOAD) { |
||||
mUploadConfig.reTryInterval = time; |
||||
} else if (mType == ConfigType.D_GROUP) { |
||||
mDGroupConfig.reTryInterval = time; |
||||
} |
||||
} |
||||
|
||||
private void loadCA(String name, String path) { |
||||
if (mType == ConfigType.DOWNLOAD) { |
||||
mDownloadConfig.caName = name; |
||||
mDownloadConfig.caPath = path; |
||||
} else if (mType == ConfigType.UPLOAD) { |
||||
mUploadConfig.caName = name; |
||||
mUploadConfig.caPath = path; |
||||
} else if (mType == ConfigType.D_GROUP) { |
||||
mDGroupConfig.caName = name; |
||||
mDGroupConfig.caPath = path; |
||||
} |
||||
} |
||||
|
||||
private void loadBuffSize(String value) { |
||||
int buffSize = checkInt(value) ? Integer.parseInt(value) : 8192; |
||||
|
||||
if (buffSize < 2048) { |
||||
buffSize = 2048; |
||||
} |
||||
|
||||
if (mType == ConfigType.DOWNLOAD) { |
||||
mDownloadConfig.buffSize = buffSize; |
||||
} |
||||
|
||||
if (mType == ConfigType.UPLOAD) { |
||||
mUploadConfig.buffSize = buffSize; |
||||
} |
||||
} |
||||
|
||||
private void loadIOTimeout(String value) { |
||||
int time = checkInt(value) ? Integer.parseInt(value) : 10 * 1000; |
||||
|
||||
if (time < 10 * 1000) { |
||||
time = 10 * 1000; |
||||
} |
||||
|
||||
if (mType == ConfigType.DOWNLOAD) { |
||||
mDownloadConfig.iOTimeOut = time; |
||||
} else if (mType == ConfigType.UPLOAD) { |
||||
mUploadConfig.iOTimeOut = time; |
||||
} else if (mType == ConfigType.D_GROUP) { |
||||
mDGroupConfig.iOTimeOut = time; |
||||
} |
||||
} |
||||
|
||||
private void loadConnectTime(String value) { |
||||
int time = checkInt(value) ? Integer.parseInt(value) : 5 * 1000; |
||||
|
||||
if (mType == ConfigType.DOWNLOAD) { |
||||
mDownloadConfig.connectTimeOut = time; |
||||
} else if (mType == ConfigType.UPLOAD) { |
||||
mUploadConfig.connectTimeOut = time; |
||||
} else if (mType == ConfigType.D_GROUP) { |
||||
mDGroupConfig.connectTimeOut = time; |
||||
} |
||||
} |
||||
|
||||
private void loadReTry(String value) { |
||||
int num = checkInt(value) ? Integer.parseInt(value) : 0; |
||||
|
||||
if (mType == ConfigType.DOWNLOAD) { |
||||
mDownloadConfig.reTryNum = num; |
||||
} else if (mType == ConfigType.UPLOAD) { |
||||
mUploadConfig.reTryNum = num; |
||||
} else if (mType == ConfigType.D_GROUP) { |
||||
mDGroupConfig.reTryNum = num; |
||||
} |
||||
} |
||||
|
||||
private void loadMaxQueue(String value) { |
||||
int num = checkInt(value) ? Integer.parseInt(value) : 2; |
||||
if (num < 1) { |
||||
ALog.w(TAG, "任务队列数不能小于 1"); |
||||
num = 2; |
||||
} |
||||
if (mType == ConfigType.DOWNLOAD) { |
||||
mDownloadConfig.maxTaskNum = num; |
||||
} else if (mType == ConfigType.UPLOAD) { |
||||
mUploadConfig.maxTaskNum = num; |
||||
} else if (mType == ConfigType.D_GROUP) { |
||||
mDGroupConfig.maxTaskNum = num; |
||||
} |
||||
} |
||||
|
||||
private void loadThreadNum(String value) { |
||||
int num = checkInt(value) ? Integer.parseInt(value) : 3; |
||||
if (num < 1) { |
||||
ALog.e(TAG, "下载线程数不能小于 1"); |
||||
num = 1; |
||||
} |
||||
if (mType == ConfigType.DOWNLOAD) { |
||||
mDownloadConfig.threadNum = num; |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* 检查是否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); |
||||
} |
||||
|
||||
@Override public void endElement(String uri, String localName, String qName) throws SAXException { |
||||
super.endElement(uri, localName, qName); |
||||
} |
||||
|
||||
@Override public void endDocument() throws SAXException { |
||||
super.endDocument(); |
||||
mDownloadConfig.save(); |
||||
mUploadConfig.save(); |
||||
mAppConfig.save(); |
||||
mDGroupConfig.save(); |
||||
} |
||||
} |
@ -0,0 +1,112 @@ |
||||
/* |
||||
* 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.config; |
||||
|
||||
import com.arialyy.aria.util.ALog; |
||||
import com.arialyy.aria.util.AriaCrashHandler; |
||||
import java.io.Serializable; |
||||
|
||||
/** |
||||
* 应用配置 |
||||
*/ |
||||
public class AppConfig extends BaseConfig implements Serializable { |
||||
/** |
||||
* 是否使用{@link AriaCrashHandler}来捕获异常 {@code true} 使用;{@code false} 不使用 |
||||
*/ |
||||
boolean useAriaCrashHandler; |
||||
|
||||
/** |
||||
* 设置Aria的日志级别 |
||||
* |
||||
* {@link ALog#LOG_LEVEL_VERBOSE} |
||||
*/ |
||||
int logLevel; |
||||
|
||||
/** |
||||
* 是否检查网络,{@code true}检查网络 |
||||
*/ |
||||
boolean netCheck = true; |
||||
|
||||
/** |
||||
* 是否使用广播 除非无法使用注解,否则不建议使用广播来接受任务 {@code true} 使用广播,{@code false} 不适用广播 |
||||
*/ |
||||
boolean useBroadcast = false; |
||||
|
||||
/** |
||||
* 断网的时候是否重试,{@code true}断网也重试;{@code false}断网不重试,直接走失败的回调 |
||||
*/ |
||||
boolean notNetRetry = false; |
||||
|
||||
public boolean isNotNetRetry() { |
||||
return notNetRetry; |
||||
} |
||||
|
||||
public AppConfig setNotNetRetry(boolean notNetRetry) { |
||||
this.notNetRetry = notNetRetry; |
||||
save(); |
||||
return this; |
||||
} |
||||
|
||||
public boolean isUseBroadcast() { |
||||
return useBroadcast; |
||||
} |
||||
|
||||
public AppConfig setUseBroadcast(boolean useBroadcast) { |
||||
this.useBroadcast = useBroadcast; |
||||
save(); |
||||
return this; |
||||
} |
||||
|
||||
public boolean isNetCheck() { |
||||
return netCheck; |
||||
} |
||||
|
||||
public AppConfig setNetCheck(boolean netCheck) { |
||||
this.netCheck = netCheck; |
||||
save(); |
||||
return this; |
||||
} |
||||
|
||||
public AppConfig setLogLevel(int level) { |
||||
this.logLevel = level; |
||||
ALog.LOG_LEVEL = level; |
||||
save(); |
||||
return this; |
||||
} |
||||
|
||||
public int getLogLevel() { |
||||
return logLevel; |
||||
} |
||||
|
||||
public boolean getUseAriaCrashHandler() { |
||||
return useAriaCrashHandler; |
||||
} |
||||
|
||||
public AppConfig setUseAriaCrashHandler(boolean useAriaCrashHandler) { |
||||
this.useAriaCrashHandler = useAriaCrashHandler; |
||||
if (useAriaCrashHandler) { |
||||
Thread.setDefaultUncaughtExceptionHandler(new AriaCrashHandler()); |
||||
} else { |
||||
Thread.setDefaultUncaughtExceptionHandler(null); |
||||
} |
||||
save(); |
||||
return this; |
||||
} |
||||
|
||||
@Override int getType() { |
||||
return TYPE_APP; |
||||
} |
||||
} |
@ -0,0 +1,51 @@ |
||||
package com.arialyy.aria.core.config; |
||||
|
||||
import android.text.TextUtils; |
||||
import com.arialyy.aria.core.AriaManager; |
||||
import com.arialyy.aria.util.ALog; |
||||
import com.arialyy.aria.util.CommonUtil; |
||||
import java.io.Serializable; |
||||
|
||||
abstract class BaseConfig implements Serializable { |
||||
private static final String TAG = "BaseConfig"; |
||||
static final int TYPE_DOWNLOAD = 1; |
||||
static final int TYPE_UPLOAD = 2; |
||||
static final int TYPE_APP = 3; |
||||
static final int TYPE_DGROUP = 4; |
||||
|
||||
/** |
||||
* 类型 |
||||
* |
||||
* @return {@link #TYPE_DOWNLOAD}、{@link #TYPE_UPLOAD}、{@link #TYPE_APP}、{@link #TYPE_DGROUP} |
||||
*/ |
||||
abstract int getType(); |
||||
|
||||
/** |
||||
* 保存配置 |
||||
*/ |
||||
void save() { |
||||
String basePath = AriaManager.APP.getFilesDir().getPath(); |
||||
String path = null; |
||||
switch (getType()) { |
||||
case TYPE_DOWNLOAD: |
||||
path = Configuration.DOWNLOAD_CONFIG_FILE; |
||||
break; |
||||
case TYPE_UPLOAD: |
||||
path = Configuration.UPLOAD_CONFIG_FILE; |
||||
break; |
||||
case TYPE_APP: |
||||
path = Configuration.APP_CONFIG_FILE; |
||||
break; |
||||
case TYPE_DGROUP: |
||||
path = Configuration.DGROUP_CONFIG_FILE; |
||||
break; |
||||
} |
||||
if (!TextUtils.isEmpty(path)) { |
||||
String tempPath = String.format("%s%s", basePath, path); |
||||
CommonUtil.deleteFile(tempPath); |
||||
CommonUtil.writeObjToFile(tempPath, this); |
||||
} else { |
||||
ALog.e(TAG, String.format("保存配置失败,配置类型:%s,原因:路径错误", getType())); |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,221 @@ |
||||
/* |
||||
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria)
|
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package com.arialyy.aria.core.config; |
||||
|
||||
import com.arialyy.aria.core.common.QueueMod; |
||||
import com.arialyy.aria.util.ALog; |
||||
import java.io.Serializable; |
||||
|
||||
/** |
||||
* 通用任务配置 |
||||
*/ |
||||
public abstract class BaseTaskConfig extends BaseConfig implements Serializable { |
||||
|
||||
|
||||
/** |
||||
* 设置写文件buff大小,该数值大小不能小于2048,数值变小,下载速度会变慢 |
||||
*/ |
||||
int buffSize = 8192; |
||||
|
||||
/** |
||||
* 进度刷新间隔,默认1秒 |
||||
*/ |
||||
long updateInterval = 1000; |
||||
|
||||
/** |
||||
* 旧任务数 |
||||
*/ |
||||
public int oldMaxTaskNum = 2; |
||||
|
||||
/** |
||||
* 任务队列最大任务数, 默认为2 |
||||
*/ |
||||
int maxTaskNum = 2; |
||||
/** |
||||
* 下载失败,重试次数,默认为10 |
||||
*/ |
||||
int reTryNum = 10; |
||||
/** |
||||
* 设置重试间隔,单位为毫秒,默认2000毫秒 |
||||
*/ |
||||
int reTryInterval = 2000; |
||||
/** |
||||
* 设置url连接超时时间,单位为毫秒,默认5000毫秒 |
||||
*/ |
||||
int connectTimeOut = 5000; |
||||
|
||||
/** |
||||
* 是否需要转换速度单位,转换完成后为:1b/s、1k/s、1m/s、1g/s、1t/s,如果不需要将返回byte长度 |
||||
*/ |
||||
boolean isConvertSpeed = false; |
||||
|
||||
/** |
||||
* 执行队列类型 |
||||
* |
||||
* @see QueueMod |
||||
*/ |
||||
String queueMod = "wait"; |
||||
|
||||
/** |
||||
* 设置IO流读取时间,单位为毫秒,默认20000毫秒,该时间不能少于10000毫秒 |
||||
*/ |
||||
int iOTimeOut = 20 * 1000; |
||||
|
||||
/** |
||||
* 设置最大下载/上传速度,单位:kb, 为0表示不限速 |
||||
*/ |
||||
int maxSpeed = 0; |
||||
|
||||
/** |
||||
* 设置https ca 证书信息;path 为assets目录下的CA证书完整路径 |
||||
*/ |
||||
String caPath; |
||||
/** |
||||
* name 为CA证书名 |
||||
*/ |
||||
String caName; |
||||
|
||||
public String getCaPath() { |
||||
return caPath; |
||||
} |
||||
|
||||
public BaseConfig setCaPath(String caPath) { |
||||
this.caPath = caPath; |
||||
save(); |
||||
return this; |
||||
} |
||||
|
||||
public String getCaName() { |
||||
return caName; |
||||
} |
||||
|
||||
public BaseConfig setCaName(String caName) { |
||||
this.caName = caName; |
||||
save(); |
||||
return this; |
||||
} |
||||
|
||||
public BaseTaskConfig setMaxTaskNum(int maxTaskNum) { |
||||
oldMaxTaskNum = this.maxTaskNum; |
||||
this.maxTaskNum = maxTaskNum; |
||||
save(); |
||||
return this; |
||||
} |
||||
|
||||
public int getMaxSpeed() { |
||||
return maxSpeed; |
||||
} |
||||
|
||||
public BaseTaskConfig setMaxSpeed(int maxSpeed) { |
||||
this.maxSpeed = maxSpeed; |
||||
save(); |
||||
return this; |
||||
} |
||||
|
||||
public long getUpdateInterval() { |
||||
return updateInterval; |
||||
} |
||||
|
||||
/** |
||||
* 设置进度更新间隔,该设置对正在运行的任务无效,默认为1000毫秒 |
||||
* |
||||
* @param updateInterval 不能小于0 |
||||
*/ |
||||
public BaseTaskConfig setUpdateInterval(long updateInterval) { |
||||
if (updateInterval <= 0) { |
||||
ALog.w("Configuration", "进度更新间隔不能小于0"); |
||||
return this; |
||||
} |
||||
this.updateInterval = updateInterval; |
||||
save(); |
||||
return this; |
||||
} |
||||
|
||||
public String getQueueMod() { |
||||
return queueMod; |
||||
} |
||||
|
||||
public BaseTaskConfig setQueueMod(String queueMod) { |
||||
this.queueMod = queueMod; |
||||
save(); |
||||
return this; |
||||
} |
||||
|
||||
public int getMaxTaskNum() { |
||||
return maxTaskNum; |
||||
} |
||||
|
||||
public int getReTryNum() { |
||||
return reTryNum; |
||||
} |
||||
|
||||
public BaseTaskConfig setReTryNum(int reTryNum) { |
||||
this.reTryNum = reTryNum; |
||||
save(); |
||||
return this; |
||||
} |
||||
|
||||
public int getReTryInterval() { |
||||
return reTryInterval; |
||||
} |
||||
|
||||
public BaseTaskConfig setReTryInterval(int reTryInterval) { |
||||
this.reTryInterval = reTryInterval; |
||||
save(); |
||||
return this; |
||||
} |
||||
|
||||
public boolean isConvertSpeed() { |
||||
return isConvertSpeed; |
||||
} |
||||
|
||||
public BaseTaskConfig setConvertSpeed(boolean convertSpeed) { |
||||
isConvertSpeed = convertSpeed; |
||||
save(); |
||||
return this; |
||||
} |
||||
|
||||
public int getConnectTimeOut() { |
||||
return connectTimeOut; |
||||
} |
||||
|
||||
public BaseTaskConfig setConnectTimeOut(int connectTimeOut) { |
||||
this.connectTimeOut = connectTimeOut; |
||||
save(); |
||||
return this; |
||||
} |
||||
|
||||
public int getIOTimeOut() { |
||||
return iOTimeOut; |
||||
} |
||||
|
||||
public BaseTaskConfig setIOTimeOut(int iOTimeOut) { |
||||
this.iOTimeOut = iOTimeOut; |
||||
save(); |
||||
return this; |
||||
} |
||||
|
||||
public int getBuffSize() { |
||||
return buffSize; |
||||
} |
||||
|
||||
public BaseTaskConfig setBuffSize(int buffSize) { |
||||
this.buffSize = buffSize; |
||||
save(); |
||||
return this; |
||||
} |
||||
} |
@ -0,0 +1,117 @@ |
||||
/* |
||||
* 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.config; |
||||
|
||||
import com.arialyy.aria.core.AriaManager; |
||||
import com.arialyy.aria.util.CommonUtil; |
||||
import java.io.File; |
||||
|
||||
/** |
||||
* Created by lyy on 2016/12/8. 信息配置 kotlin 方式有bug,不能将public去掉 |
||||
*/ |
||||
public final class Configuration { |
||||
private static final String TAG = "Configuration"; |
||||
private static volatile Configuration INSTANCE = null; |
||||
public static final String XML_FILE = "/Aria/aria_config.xml"; |
||||
static final String DOWNLOAD_CONFIG_FILE = "/Aria/AriaDownload.cfg"; |
||||
static final String UPLOAD_CONFIG_FILE = "/Aria/AriaUpload.cfg"; |
||||
static final String APP_CONFIG_FILE = "/Aria/AriaApp.cfg"; |
||||
static final String DGROUP_CONFIG_FILE = "/Aria/AriaDGroup.cfg"; |
||||
public DownloadConfig downloadCfg; |
||||
public UploadConfig uploadCfg; |
||||
public AppConfig appCfg; |
||||
public DGroupConfig dGroupCfg; |
||||
|
||||
private Configuration() { |
||||
//删除老版本的配置文件
|
||||
String basePath = AriaManager.APP.getFilesDir().getPath(); |
||||
del351Config(basePath); |
||||
File newDCfg = new File(String.format("%s%s", basePath, DOWNLOAD_CONFIG_FILE)); |
||||
File newUCfg = new File(String.format("%s%s", basePath, UPLOAD_CONFIG_FILE)); |
||||
File newACfg = new File(String.format("%s%s", basePath, APP_CONFIG_FILE)); |
||||
File dgCfg = new File(String.format("%s%s", basePath, DGROUP_CONFIG_FILE)); |
||||
// 加载下载配置
|
||||
if (newDCfg.exists()) { |
||||
downloadCfg = (DownloadConfig) CommonUtil.readObjFromFile(newDCfg.getPath()); |
||||
} |
||||
if (downloadCfg == null) { |
||||
downloadCfg = new DownloadConfig(); |
||||
} |
||||
// 加载上传配置
|
||||
if (newUCfg.exists()) { |
||||
uploadCfg = (UploadConfig) CommonUtil.readObjFromFile(newUCfg.getPath()); |
||||
} |
||||
if (uploadCfg == null) { |
||||
uploadCfg = new UploadConfig(); |
||||
} |
||||
// 加载app配置
|
||||
if (newACfg.exists()) { |
||||
appCfg = (AppConfig) CommonUtil.readObjFromFile(newACfg.getPath()); |
||||
} |
||||
if (appCfg == null) { |
||||
appCfg = new AppConfig(); |
||||
} |
||||
// 加载下载类型组合任务的配置
|
||||
if (dgCfg.exists()) { |
||||
dGroupCfg = (DGroupConfig) CommonUtil.readObjFromFile(dgCfg.getPath()); |
||||
} |
||||
if (dGroupCfg == null) { |
||||
dGroupCfg = new DGroupConfig(); |
||||
} |
||||
} |
||||
|
||||
public static Configuration getInstance() { |
||||
if (INSTANCE == null) { |
||||
synchronized (AppConfig.class) { |
||||
INSTANCE = new Configuration(); |
||||
} |
||||
} |
||||
return INSTANCE; |
||||
} |
||||
|
||||
/** |
||||
* 检查配置文件是否存在,只要{@link DownloadConfig}、{@link UploadConfig}、{@link AppConfig}、{@link |
||||
* DGroupConfig}其中一个不存在 则任务配置文件不存在 |
||||
* |
||||
* @return {@code true}配置存在,{@code false}配置不存在 |
||||
*/ |
||||
public boolean configExists() { |
||||
String basePath = AriaManager.APP.getFilesDir().getPath(); |
||||
return (new File(String.format("%s%s", basePath, DOWNLOAD_CONFIG_FILE))).exists() |
||||
&& (new File(String.format("%s%s", basePath, UPLOAD_CONFIG_FILE))).exists() |
||||
&& (new File(String.format("%s%s", basePath, APP_CONFIG_FILE))).exists() |
||||
&& (new File(String.format("%s%s", basePath, DGROUP_CONFIG_FILE))).exists(); |
||||
} |
||||
|
||||
/** |
||||
* 删除3.5.2之前版本的配置文件,从3.5.2开始,配置文件的保存不再使用properties文件 |
||||
*/ |
||||
private void del351Config(String basePath) { |
||||
File oldDCfg = new File(String.format("%s/Aria/DownloadConfig.properties", basePath)); |
||||
if (oldDCfg.exists()) { // 只需要判断一个
|
||||
File oldUCfg = new File(String.format("%s/Aria/UploadConfig.properties", basePath)); |
||||
File oldACfg = new File(String.format("%s/Aria/AppConfig.properties", basePath)); |
||||
oldDCfg.delete(); |
||||
oldUCfg.delete(); |
||||
oldACfg.delete(); |
||||
// 删除配置触发更新
|
||||
File temp = new File(String.format("%s%s", basePath, XML_FILE)); |
||||
if (temp.exists()) { |
||||
temp.delete(); |
||||
} |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,114 @@ |
||||
/* |
||||
* 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.config; |
||||
|
||||
import com.arialyy.aria.core.queue.DownloadGroupTaskQueue; |
||||
import java.io.Serializable; |
||||
|
||||
/** |
||||
* 下载类型的组合任务 |
||||
*/ |
||||
public class DGroupConfig extends BaseTaskConfig implements Serializable { |
||||
|
||||
/** |
||||
* 能同时下载的子任务最大任务数,默认3 |
||||
*/ |
||||
int subMaxTaskNum = 3; |
||||
|
||||
/** |
||||
* 子任务重试次数,默认为5 |
||||
*/ |
||||
int subReTryNum = 5; |
||||
|
||||
/** |
||||
* 子任务下载失败时的重试间隔,单位为毫秒,默认2000毫秒- |
||||
*/ |
||||
int subReTryInterval = 2000; |
||||
|
||||
private DownloadConfig subConfig; |
||||
|
||||
DGroupConfig() { |
||||
getSubConfig(); |
||||
} |
||||
|
||||
@Override int getType() { |
||||
return TYPE_DGROUP; |
||||
} |
||||
|
||||
@Override public DGroupConfig setMaxSpeed(int maxSpeed) { |
||||
super.setMaxSpeed(maxSpeed); |
||||
DownloadGroupTaskQueue.getInstance().setMaxSpeed(maxSpeed); |
||||
return this; |
||||
} |
||||
|
||||
public DownloadConfig getSubConfig() { |
||||
if (subConfig == null) { |
||||
subConfig = new DownloadConfig(); |
||||
subConfig.threadNum = 1; |
||||
subConfig.useBlock = false; |
||||
subConfig.buffSize = buffSize; |
||||
subConfig.caName = caName; |
||||
subConfig.caPath = caPath; |
||||
subConfig.connectTimeOut = connectTimeOut; |
||||
subConfig.iOTimeOut = iOTimeOut; |
||||
subConfig.isConvertSpeed = isConvertSpeed; |
||||
subConfig.maxSpeed = maxSpeed; |
||||
subConfig.queueMod = "now"; |
||||
subConfig.reTryInterval = subReTryInterval; |
||||
subConfig.reTryNum = subReTryNum; |
||||
subConfig.updateInterval = updateInterval; |
||||
} |
||||
return subConfig; |
||||
} |
||||
|
||||
public DGroupConfig setMaxTaskNum(int maxTaskNum) { |
||||
super.setMaxTaskNum(maxTaskNum); |
||||
DownloadGroupTaskQueue.getInstance().setMaxTaskNum(maxTaskNum); |
||||
return this; |
||||
} |
||||
|
||||
public int getSubMaxTaskNum() { |
||||
return subMaxTaskNum; |
||||
} |
||||
|
||||
public DGroupConfig setSubMaxTaskNum(int subMaxTaskNum) { |
||||
this.subMaxTaskNum = subMaxTaskNum; |
||||
save(); |
||||
return this; |
||||
} |
||||
|
||||
public int getSubReTryNum() { |
||||
return subReTryNum; |
||||
} |
||||
|
||||
public DGroupConfig setSubReTryNum(int subReTryNum) { |
||||
this.subReTryNum = subReTryNum; |
||||
subConfig.reTryNum = subReTryNum; |
||||
save(); |
||||
return this; |
||||
} |
||||
|
||||
public int getSubReTryInterval() { |
||||
return subReTryInterval; |
||||
} |
||||
|
||||
public DGroupConfig setSubReTryInterval(int subReTryInterval) { |
||||
this.subReTryInterval = subReTryInterval; |
||||
subConfig.reTryInterval = subReTryInterval; |
||||
save(); |
||||
return this; |
||||
} |
||||
} |
@ -0,0 +1,75 @@ |
||||
/* |
||||
* 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.config; |
||||
|
||||
import com.arialyy.aria.core.queue.DownloadTaskQueue; |
||||
import java.io.Serializable; |
||||
|
||||
/** |
||||
* 下载配置 |
||||
*/ |
||||
public class DownloadConfig extends BaseTaskConfig implements Serializable { |
||||
/** |
||||
* 下载线程数,下载线程数不能小于1 注意: 1、线程下载数改变后,新的下载任务才会生效; 2、如果任务大小小于1m,该设置不会生效; |
||||
* 3、从3.4.1开始,如果线程数为1,文件初始化时将不再预占用对应长度的空间,下载多少byte,则占多大的空间; 对于采用多线程的任务或旧任务,依然采用原来的文件空间占用方式; |
||||
*/ |
||||
int threadNum = 3; |
||||
|
||||
/** |
||||
* 多线程下载是否使用块下载模式,{@code true}使用,{@code false}不使用 注意: 1、使用分块模式,在读写性能底下的手机上,合并文件需要的时间会更加长; |
||||
* 2、优点是使用多线程的块下载,初始化时,文件初始化时将不会预占用对应长度的空间; 3、只对新的多线程下载任务有效 4、只对多线程的任务有效 |
||||
*/ |
||||
boolean useBlock = false; |
||||
|
||||
public boolean isUseBlock() { |
||||
return useBlock; |
||||
} |
||||
|
||||
@Override public DownloadConfig setMaxSpeed(int maxSpeed) { |
||||
super.setMaxSpeed(maxSpeed); |
||||
DownloadTaskQueue.getInstance().setMaxSpeed(maxSpeed); |
||||
return this; |
||||
} |
||||
|
||||
public DownloadConfig setUseBlock(boolean useBlock) { |
||||
this.useBlock = useBlock; |
||||
save(); |
||||
return this; |
||||
} |
||||
|
||||
public DownloadConfig setMaxTaskNum(int maxTaskNum) { |
||||
super.setMaxTaskNum(maxTaskNum); |
||||
DownloadTaskQueue.getInstance().setMaxTaskNum(maxTaskNum); |
||||
return this; |
||||
} |
||||
|
||||
public DownloadConfig setThreadNum(int threadNum) { |
||||
this.threadNum = threadNum; |
||||
save(); |
||||
return this; |
||||
} |
||||
|
||||
public int getThreadNum() { |
||||
return threadNum; |
||||
} |
||||
|
||||
DownloadConfig() { |
||||
} |
||||
|
||||
@Override int getType() { |
||||
return TYPE_DOWNLOAD; |
||||
} |
||||
} |
@ -0,0 +1,5 @@ |
||||
package com.arialyy.aria.core.config; |
||||
|
||||
public class TTaskConfigAdapeter { |
||||
|
||||
} |
@ -0,0 +1,44 @@ |
||||
/* |
||||
* 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.config; |
||||
|
||||
import com.arialyy.aria.core.queue.UploadTaskQueue; |
||||
import java.io.Serializable; |
||||
|
||||
/** |
||||
* 上传配置 |
||||
*/ |
||||
public class UploadConfig extends BaseTaskConfig implements Serializable { |
||||
|
||||
UploadConfig() { |
||||
} |
||||
|
||||
@Override public UploadConfig setMaxSpeed(int maxSpeed) { |
||||
super.setMaxSpeed(maxSpeed); |
||||
UploadTaskQueue.getInstance().setMaxSpeed(maxSpeed); |
||||
return this; |
||||
} |
||||
|
||||
public UploadConfig setMaxTaskNum(int maxTaskNum) { |
||||
super.setMaxTaskNum(maxTaskNum); |
||||
UploadTaskQueue.getInstance().setMaxTaskNum(maxTaskNum); |
||||
return this; |
||||
} |
||||
|
||||
@Override int getType() { |
||||
return TYPE_UPLOAD; |
||||
} |
||||
} |
@ -1,621 +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 android.text.TextUtils; |
||||
import com.arialyy.aria.core.common.QueueMod; |
||||
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.util.ALog; |
||||
import com.arialyy.aria.util.AriaCrashHandler; |
||||
import com.arialyy.aria.util.CommonUtil; |
||||
import java.io.File; |
||||
import java.io.Serializable; |
||||
|
||||
/** |
||||
* Created by lyy on 2016/12/8. 信息配置 kotlin 方式有bug,不能将public去掉 |
||||
*/ |
||||
public final class Configuration { |
||||
private static final String TAG = "Configuration"; |
||||
private static final String DOWNLOAD_CONFIG_FILE = "/Aria/AriaDownload.cfg"; |
||||
private static final String UPLOAD_CONFIG_FILE = "/Aria/AriaUpload.cfg"; |
||||
private static final String APP_CONFIG_FILE = "/Aria/AriaApp.cfg"; |
||||
private static final String DGROUP_CONFIG_FILE = "/Aria/AriaDGroup.cfg"; |
||||
private static final int TYPE_DOWNLOAD = 1; |
||||
private static final int TYPE_UPLOAD = 2; |
||||
private static final int TYPE_APP = 3; |
||||
private static final int TYPE_DGROUP = 4; |
||||
private static volatile Configuration INSTANCE = null; |
||||
static final String XML_FILE = "/Aria/aria_config.xml"; |
||||
DownloadConfig downloadCfg; |
||||
UploadConfig uploadCfg; |
||||
AppConfig appCfg; |
||||
DGroupConfig dGroupCfg; |
||||
|
||||
private Configuration() { |
||||
//删除老版本的配置文件
|
||||
String basePath = AriaManager.APP.getFilesDir().getPath(); |
||||
File oldDCfg = new File(String.format("%s/Aria/DownloadConfig.properties", basePath)); |
||||
if (oldDCfg.exists()) { // 只需要判断一个
|
||||
File oldUCfg = new File(String.format("%s/Aria/UploadConfig.properties", basePath)); |
||||
File oldACfg = new File(String.format("%s/Aria/AppConfig.properties", basePath)); |
||||
oldDCfg.delete(); |
||||
oldUCfg.delete(); |
||||
oldACfg.delete(); |
||||
// 删除配置触发更新
|
||||
File temp = new File(String.format("%s%s", basePath, XML_FILE)); |
||||
if (temp.exists()) { |
||||
temp.delete(); |
||||
} |
||||
} |
||||
File newDCfg = new File(String.format("%s%s", basePath, DOWNLOAD_CONFIG_FILE)); |
||||
File newUCfg = new File(String.format("%s%s", basePath, UPLOAD_CONFIG_FILE)); |
||||
File newACfg = new File(String.format("%s%s", basePath, APP_CONFIG_FILE)); |
||||
File dgCfg = new File(String.format("%s%s", basePath, DGROUP_CONFIG_FILE)); |
||||
// 加载下载配置
|
||||
if (newDCfg.exists()) { |
||||
downloadCfg = (DownloadConfig) CommonUtil.readObjFromFile(newDCfg.getPath()); |
||||
} |
||||
if (downloadCfg == null) { |
||||
downloadCfg = new DownloadConfig(); |
||||
} |
||||
// 加载上传配置
|
||||
if (newUCfg.exists()) { |
||||
uploadCfg = (UploadConfig) CommonUtil.readObjFromFile(newUCfg.getPath()); |
||||
} |
||||
if (uploadCfg == null) { |
||||
uploadCfg = new UploadConfig(); |
||||
} |
||||
// 加载app配置
|
||||
if (newACfg.exists()) { |
||||
appCfg = (AppConfig) CommonUtil.readObjFromFile(newACfg.getPath()); |
||||
} |
||||
if (appCfg == null) { |
||||
appCfg = new AppConfig(); |
||||
} |
||||
// 加载下载类型组合任务的配置
|
||||
if (dgCfg.exists()) { |
||||
dGroupCfg = (DGroupConfig) CommonUtil.readObjFromFile(dgCfg.getPath()); |
||||
} |
||||
if (dGroupCfg == null) { |
||||
dGroupCfg = new DGroupConfig(); |
||||
} |
||||
} |
||||
|
||||
static Configuration getInstance() { |
||||
if (INSTANCE == null) { |
||||
synchronized (AppConfig.class) { |
||||
INSTANCE = new Configuration(); |
||||
} |
||||
} |
||||
return INSTANCE; |
||||
} |
||||
|
||||
/** |
||||
* 检查配置文件是否存在,只要{@link DownloadConfig}、{@link UploadConfig}、{@link AppConfig}、{@link |
||||
* DGroupConfig}其中一个不存在 则任务配置文件不存在 |
||||
* |
||||
* @return {@code true}配置存在,{@code false}配置不存在 |
||||
*/ |
||||
boolean configExists() { |
||||
String basePath = AriaManager.APP.getFilesDir().getPath(); |
||||
return (new File(String.format("%s%s", basePath, DOWNLOAD_CONFIG_FILE))).exists() |
||||
&& (new File(String.format("%s%s", basePath, UPLOAD_CONFIG_FILE))).exists() |
||||
&& (new File(String.format("%s%s", basePath, APP_CONFIG_FILE))).exists() |
||||
&& (new File(String.format("%s%s", basePath, DGROUP_CONFIG_FILE))).exists(); |
||||
} |
||||
|
||||
abstract static class BaseConfig implements Serializable { |
||||
|
||||
/** |
||||
* 类型 |
||||
* |
||||
* @return {@link #TYPE_DOWNLOAD}、{@link #TYPE_UPLOAD}、{@link #TYPE_APP}、{@link #TYPE_DGROUP} |
||||
*/ |
||||
abstract int getType(); |
||||
|
||||
/** |
||||
* 保存配置 |
||||
*/ |
||||
void save() { |
||||
String basePath = AriaManager.APP.getFilesDir().getPath(); |
||||
String path = null; |
||||
switch (getType()) { |
||||
case TYPE_DOWNLOAD: |
||||
path = DOWNLOAD_CONFIG_FILE; |
||||
break; |
||||
case TYPE_UPLOAD: |
||||
path = UPLOAD_CONFIG_FILE; |
||||
break; |
||||
case TYPE_APP: |
||||
path = APP_CONFIG_FILE; |
||||
break; |
||||
} |
||||
if (!TextUtils.isEmpty(path)) { |
||||
String tempPath = String.format("%s%s", basePath, path); |
||||
CommonUtil.deleteFile(tempPath); |
||||
CommonUtil.writeObjToFile(tempPath, this); |
||||
} else { |
||||
ALog.e(TAG, String.format("保存配置失败,配置类型:%s,原因:路径错误", getType())); |
||||
} |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* 通用任务配置 |
||||
*/ |
||||
abstract static class BaseTaskConfig extends BaseConfig implements Serializable { |
||||
|
||||
/** |
||||
* 设置写文件buff大小,该数值大小不能小于2048,数值变小,下载速度会变慢 |
||||
*/ |
||||
int buffSize = 8192; |
||||
|
||||
/** |
||||
* 进度刷新间隔,默认1秒 |
||||
*/ |
||||
long updateInterval = 1000; |
||||
|
||||
/** |
||||
* 旧任务数 |
||||
*/ |
||||
public int oldMaxTaskNum = 2; |
||||
|
||||
/** |
||||
* 任务队列最大任务数, 默认为2 |
||||
*/ |
||||
int maxTaskNum = 2; |
||||
/** |
||||
* 下载失败,重试次数,默认为10 |
||||
*/ |
||||
int reTryNum = 10; |
||||
/** |
||||
* 设置重试间隔,单位为毫秒,默认2000毫秒 |
||||
*/ |
||||
int reTryInterval = 2000; |
||||
/** |
||||
* 设置url连接超时时间,单位为毫秒,默认5000毫秒 |
||||
*/ |
||||
int connectTimeOut = 5000; |
||||
|
||||
/** |
||||
* 是否需要转换速度单位,转换完成后为:1b/s、1k/s、1m/s、1g/s、1t/s,如果不需要将返回byte长度 |
||||
*/ |
||||
boolean isConvertSpeed = false; |
||||
|
||||
/** |
||||
* 执行队列类型 |
||||
* |
||||
* @see QueueMod |
||||
*/ |
||||
String queueMod = "wait"; |
||||
|
||||
/** |
||||
* 设置IO流读取时间,单位为毫秒,默认20000毫秒,该时间不能少于10000毫秒 |
||||
*/ |
||||
int iOTimeOut = 20 * 1000; |
||||
|
||||
/** |
||||
* 设置最大下载/上传速度,单位:kb, 为0表示不限速 |
||||
*/ |
||||
int maxSpeed = 0; |
||||
|
||||
/** |
||||
* 设置https ca 证书信息;path 为assets目录下的CA证书完整路径 |
||||
*/ |
||||
String caPath; |
||||
/** |
||||
* name 为CA证书名 |
||||
*/ |
||||
String caName; |
||||
|
||||
public String getCaPath() { |
||||
return caPath; |
||||
} |
||||
|
||||
public BaseConfig setCaPath(String caPath) { |
||||
this.caPath = caPath; |
||||
save(); |
||||
return this; |
||||
} |
||||
|
||||
public String getCaName() { |
||||
return caName; |
||||
} |
||||
|
||||
public BaseConfig setCaName(String caName) { |
||||
this.caName = caName; |
||||
save(); |
||||
return this; |
||||
} |
||||
|
||||
public BaseTaskConfig setMaxTaskNum(int maxTaskNum) { |
||||
oldMaxTaskNum = this.maxTaskNum; |
||||
this.maxTaskNum = maxTaskNum; |
||||
save(); |
||||
return this; |
||||
} |
||||
|
||||
public int getMaxSpeed() { |
||||
return maxSpeed; |
||||
} |
||||
|
||||
public BaseTaskConfig setMaxSpeed(int maxSpeed) { |
||||
this.maxSpeed = maxSpeed; |
||||
save(); |
||||
return this; |
||||
} |
||||
|
||||
public long getUpdateInterval() { |
||||
return updateInterval; |
||||
} |
||||
|
||||
/** |
||||
* 设置进度更新间隔,该设置对正在运行的任务无效,默认为1000毫秒 |
||||
* |
||||
* @param updateInterval 不能小于0 |
||||
*/ |
||||
public BaseTaskConfig setUpdateInterval(long updateInterval) { |
||||
if (updateInterval <= 0) { |
||||
ALog.w("Configuration", "进度更新间隔不能小于0"); |
||||
return this; |
||||
} |
||||
this.updateInterval = updateInterval; |
||||
save(); |
||||
return this; |
||||
} |
||||
|
||||
public String getQueueMod() { |
||||
return queueMod; |
||||
} |
||||
|
||||
public BaseTaskConfig setQueueMod(String queueMod) { |
||||
this.queueMod = queueMod; |
||||
save(); |
||||
return this; |
||||
} |
||||
|
||||
public int getMaxTaskNum() { |
||||
return maxTaskNum; |
||||
} |
||||
|
||||
public int getReTryNum() { |
||||
return reTryNum; |
||||
} |
||||
|
||||
public BaseTaskConfig setReTryNum(int reTryNum) { |
||||
this.reTryNum = reTryNum; |
||||
save(); |
||||
return this; |
||||
} |
||||
|
||||
public int getReTryInterval() { |
||||
return reTryInterval; |
||||
} |
||||
|
||||
public BaseTaskConfig setReTryInterval(int reTryInterval) { |
||||
this.reTryInterval = reTryInterval; |
||||
save(); |
||||
return this; |
||||
} |
||||
|
||||
public boolean isConvertSpeed() { |
||||
return isConvertSpeed; |
||||
} |
||||
|
||||
public BaseTaskConfig setConvertSpeed(boolean convertSpeed) { |
||||
isConvertSpeed = convertSpeed; |
||||
save(); |
||||
return this; |
||||
} |
||||
|
||||
public int getConnectTimeOut() { |
||||
return connectTimeOut; |
||||
} |
||||
|
||||
public BaseTaskConfig setConnectTimeOut(int connectTimeOut) { |
||||
this.connectTimeOut = connectTimeOut; |
||||
save(); |
||||
return this; |
||||
} |
||||
|
||||
public int getIOTimeOut() { |
||||
return iOTimeOut; |
||||
} |
||||
|
||||
public BaseTaskConfig setIOTimeOut(int iOTimeOut) { |
||||
this.iOTimeOut = iOTimeOut; |
||||
save(); |
||||
return this; |
||||
} |
||||
|
||||
public int getBuffSize() { |
||||
return buffSize; |
||||
} |
||||
|
||||
public BaseTaskConfig setBuffSize(int buffSize) { |
||||
this.buffSize = buffSize; |
||||
save(); |
||||
return this; |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* 下载配置 |
||||
*/ |
||||
public static class DownloadConfig extends BaseTaskConfig implements Serializable { |
||||
/** |
||||
* 下载线程数,下载线程数不能小于1 注意: 1、线程下载数改变后,新的下载任务才会生效; 2、如果任务大小小于1m,该设置不会生效; |
||||
* 3、从3.4.1开始,如果线程数为1,文件初始化时将不再预占用对应长度的空间,下载多少byte,则占多大的空间; 对于采用多线程的任务或旧任务,依然采用原来的文件空间占用方式; |
||||
*/ |
||||
int threadNum = 3; |
||||
|
||||
/** |
||||
* 多线程下载是否使用块下载模式,{@code true}使用,{@code false}不使用 注意: 1、使用分块模式,在读写性能底下的手机上,合并文件需要的时间会更加长; |
||||
* 2、优点是使用多线程的块下载,初始化时,文件初始化时将不会预占用对应长度的空间; 3、只对新的多线程下载任务有效 4、只对多线程的任务有效 |
||||
*/ |
||||
boolean useBlock = false; |
||||
|
||||
public boolean isUseBlock() { |
||||
return useBlock; |
||||
} |
||||
|
||||
@Override public DownloadConfig setMaxSpeed(int maxSpeed) { |
||||
super.setMaxSpeed(maxSpeed); |
||||
DownloadTaskQueue.getInstance().setMaxSpeed(maxSpeed); |
||||
return this; |
||||
} |
||||
|
||||
public DownloadConfig setUseBlock(boolean useBlock) { |
||||
this.useBlock = useBlock; |
||||
save(); |
||||
return this; |
||||
} |
||||
|
||||
public DownloadConfig setMaxTaskNum(int maxTaskNum) { |
||||
super.setMaxTaskNum(maxTaskNum); |
||||
DownloadTaskQueue.getInstance().setMaxTaskNum(maxTaskNum); |
||||
return this; |
||||
} |
||||
|
||||
public DownloadConfig setThreadNum(int threadNum) { |
||||
this.threadNum = threadNum; |
||||
save(); |
||||
return this; |
||||
} |
||||
|
||||
public int getThreadNum() { |
||||
return threadNum; |
||||
} |
||||
|
||||
private DownloadConfig() { |
||||
} |
||||
|
||||
@Override int getType() { |
||||
return TYPE_DOWNLOAD; |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* 上传配置 |
||||
*/ |
||||
public static class UploadConfig extends BaseTaskConfig implements Serializable { |
||||
private static UploadConfig INSTANCE = null; |
||||
|
||||
private UploadConfig() { |
||||
//loadConfig();
|
||||
} |
||||
|
||||
@Override public UploadConfig setMaxSpeed(int maxSpeed) { |
||||
super.setMaxSpeed(maxSpeed); |
||||
UploadTaskQueue.getInstance().setMaxSpeed(maxSpeed); |
||||
return this; |
||||
} |
||||
|
||||
public UploadConfig setMaxTaskNum(int maxTaskNum) { |
||||
super.setMaxTaskNum(maxTaskNum); |
||||
UploadTaskQueue.getInstance().setMaxTaskNum(maxTaskNum); |
||||
return this; |
||||
} |
||||
|
||||
static UploadConfig getInstance() { |
||||
if (INSTANCE == null) { |
||||
synchronized (UploadConfig.class) { |
||||
INSTANCE = new UploadConfig(); |
||||
} |
||||
} |
||||
return INSTANCE; |
||||
} |
||||
|
||||
@Override int getType() { |
||||
return TYPE_UPLOAD; |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* 下载类型的组合任务 |
||||
*/ |
||||
public static class DGroupConfig extends BaseTaskConfig implements Serializable { |
||||
|
||||
private static DGroupConfig INSTANCE = null; |
||||
|
||||
/** |
||||
* 能同时下载的子任务最大任务数,默认3 |
||||
*/ |
||||
int subMaxTaskNum = 3; |
||||
|
||||
/** |
||||
* 子任务重试次数,默认为5 |
||||
*/ |
||||
int subReTryNum = 5; |
||||
|
||||
/** |
||||
* 子任务下载失败时的重试间隔,单位为毫秒,默认2000毫秒- |
||||
*/ |
||||
int subReTryInterval = 2000; |
||||
|
||||
private DGroupConfig() { |
||||
} |
||||
|
||||
static DGroupConfig getInstance() { |
||||
if (INSTANCE == null) { |
||||
synchronized (DGroupConfig.class) { |
||||
INSTANCE = new DGroupConfig(); |
||||
} |
||||
} |
||||
return INSTANCE; |
||||
} |
||||
|
||||
@Override int getType() { |
||||
return TYPE_DGROUP; |
||||
} |
||||
|
||||
@Override public DGroupConfig setMaxSpeed(int maxSpeed) { |
||||
super.setMaxSpeed(maxSpeed); |
||||
DownloadGroupTaskQueue.getInstance().setMaxSpeed(maxSpeed); |
||||
return this; |
||||
} |
||||
|
||||
public DGroupConfig setMaxTaskNum(int maxTaskNum) { |
||||
super.setMaxTaskNum(maxTaskNum); |
||||
DownloadGroupTaskQueue.getInstance().setMaxTaskNum(maxSpeed); |
||||
return this; |
||||
} |
||||
|
||||
public int getSubMaxTaskNum() { |
||||
return subMaxTaskNum; |
||||
} |
||||
|
||||
public DGroupConfig setSubMaxTaskNum(int subMaxTaskNum) { |
||||
this.subMaxTaskNum = subMaxTaskNum; |
||||
save(); |
||||
return this; |
||||
} |
||||
|
||||
public int getSubReTryNum() { |
||||
return subReTryNum; |
||||
} |
||||
|
||||
public DGroupConfig setSubReTryNum(int subReTryNum) { |
||||
this.subReTryNum = subReTryNum; |
||||
save(); |
||||
return this; |
||||
} |
||||
|
||||
public int getSubReTryInterval() { |
||||
return subReTryInterval; |
||||
} |
||||
|
||||
public DGroupConfig setSubReTryInterval(int subReTryInterval) { |
||||
this.subReTryInterval = subReTryInterval; |
||||
save(); |
||||
return this; |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* 应用配置 |
||||
*/ |
||||
public static class AppConfig extends BaseConfig implements Serializable { |
||||
/** |
||||
* 是否使用{@link AriaCrashHandler}来捕获异常 {@code true} 使用;{@code false} 不使用 |
||||
*/ |
||||
boolean useAriaCrashHandler; |
||||
|
||||
/** |
||||
* 设置Aria的日志级别 |
||||
* |
||||
* {@link ALog#LOG_LEVEL_VERBOSE} |
||||
*/ |
||||
int logLevel; |
||||
|
||||
/** |
||||
* 是否检查网络,{@code true}检查网络 |
||||
*/ |
||||
boolean netCheck = true; |
||||
|
||||
/** |
||||
* 是否使用广播 除非无法使用注解,否则不建议使用广播来接受任务 {@code true} 使用广播,{@code false} 不适用广播 |
||||
*/ |
||||
boolean useBroadcast = false; |
||||
|
||||
/** |
||||
* 断网的时候是否重试,{@code true}断网也重试;{@code false}断网不重试,直接走失败的回调 |
||||
*/ |
||||
boolean notNetRetry = false; |
||||
|
||||
public boolean isNotNetRetry() { |
||||
return notNetRetry; |
||||
} |
||||
|
||||
public AppConfig setNotNetRetry(boolean notNetRetry) { |
||||
this.notNetRetry = notNetRetry; |
||||
save(); |
||||
return this; |
||||
} |
||||
|
||||
public boolean isUseBroadcast() { |
||||
return useBroadcast; |
||||
} |
||||
|
||||
public AppConfig setUseBroadcast(boolean useBroadcast) { |
||||
this.useBroadcast = useBroadcast; |
||||
save(); |
||||
return this; |
||||
} |
||||
|
||||
public boolean isNetCheck() { |
||||
return netCheck; |
||||
} |
||||
|
||||
public AppConfig setNetCheck(boolean netCheck) { |
||||
this.netCheck = netCheck; |
||||
save(); |
||||
return this; |
||||
} |
||||
|
||||
public AppConfig setLogLevel(int level) { |
||||
this.logLevel = level; |
||||
ALog.LOG_LEVEL = level; |
||||
save(); |
||||
return this; |
||||
} |
||||
|
||||
public int getLogLevel() { |
||||
return logLevel; |
||||
} |
||||
|
||||
public boolean getUseAriaCrashHandler() { |
||||
return useAriaCrashHandler; |
||||
} |
||||
|
||||
public AppConfig setUseAriaCrashHandler(boolean useAriaCrashHandler) { |
||||
this.useAriaCrashHandler = useAriaCrashHandler; |
||||
if (useAriaCrashHandler) { |
||||
Thread.setDefaultUncaughtExceptionHandler(new AriaCrashHandler()); |
||||
} else { |
||||
Thread.setDefaultUncaughtExceptionHandler(null); |
||||
} |
||||
save(); |
||||
return this; |
||||
} |
||||
|
||||
@Override int getType() { |
||||
return TYPE_APP; |
||||
} |
||||
} |
||||
} |
Loading…
Reference in new issue