commit
3fb93080b1
@ -0,0 +1,234 @@ |
||||
/* |
||||
* 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 android.util.Log; |
||||
import org.xml.sax.Attributes; |
||||
import org.xml.sax.SAXException; |
||||
import org.xml.sax.helpers.DefaultHandler; |
||||
|
||||
/** |
||||
* Created by Aria.Lao on 2017/5/22. |
||||
* 读取配置文件 |
||||
*/ |
||||
public class ConfigHelper extends DefaultHandler { |
||||
private final String TAG = "ConfigHelper"; |
||||
|
||||
private boolean isDownloadConfig = false, isUploadConfig; |
||||
private Configuration.DownloadConfig mDownloadConfig = Configuration.DownloadConfig.getInstance(); |
||||
private Configuration.UploadConfig mUploadConfig = Configuration.UploadConfig.getInstance(); |
||||
|
||||
@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); |
||||
if (qName.equals("download")) { |
||||
isDownloadConfig = true; |
||||
isUploadConfig = false; |
||||
} else if (qName.equals("upload")) { |
||||
isUploadConfig = true; |
||||
isDownloadConfig = false; |
||||
} |
||||
if (isDownloadConfig || isUploadConfig) { |
||||
|
||||
String value = attributes.getValue("value"); |
||||
switch (qName) { |
||||
case "threadNum": |
||||
loadThreadNum(value); |
||||
break; |
||||
case "openBroadcast": |
||||
loadBroadcast(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; |
||||
} |
||||
} |
||||
} |
||||
|
||||
private void loadConvertSpeed(String value) { |
||||
boolean open = Boolean.parseBoolean(value); |
||||
if (isDownloadConfig) { |
||||
mDownloadConfig.isConvertSpeed = open; |
||||
} |
||||
if (isUploadConfig) { |
||||
mUploadConfig.isConvertSpeed = open; |
||||
} |
||||
} |
||||
|
||||
private void loadReTryInterval(String value) { |
||||
int time = 2 * 1000; |
||||
if (!TextUtils.isEmpty(value)) { |
||||
time = Integer.parseInt(value); |
||||
} |
||||
|
||||
if (time < 2 * 1000) { |
||||
time = 2 * 1000; |
||||
} |
||||
|
||||
if (isDownloadConfig) { |
||||
mDownloadConfig.reTryInterval = time; |
||||
} |
||||
} |
||||
|
||||
private void loadCA(String name, String path) { |
||||
if (isDownloadConfig) { |
||||
mDownloadConfig.caName = name; |
||||
mDownloadConfig.caPath = path; |
||||
} |
||||
} |
||||
|
||||
private void loadBuffSize(String value) { |
||||
int buffSize = 8192; |
||||
if (!TextUtils.isEmpty(value)) { |
||||
buffSize = Integer.parseInt(value); |
||||
} |
||||
|
||||
if (buffSize < 2048) { |
||||
buffSize = 2048; |
||||
} |
||||
|
||||
if (isDownloadConfig) { |
||||
mDownloadConfig.buffSize = buffSize; |
||||
} |
||||
} |
||||
|
||||
private void loadIOTimeout(String value) { |
||||
int time = 10 * 1000; |
||||
if (!TextUtils.isEmpty(value)) { |
||||
time = Integer.parseInt(value); |
||||
} |
||||
|
||||
if (time < 10 * 1000) { |
||||
time = 10 * 1000; |
||||
} |
||||
|
||||
if (isDownloadConfig) { |
||||
mDownloadConfig.iOTimeOut = time; |
||||
} |
||||
} |
||||
|
||||
private void loadConnectTime(String value) { |
||||
int time = 5 * 1000; |
||||
if (!TextUtils.isEmpty(value)) { |
||||
time = Integer.parseInt(value); |
||||
} |
||||
|
||||
if (isDownloadConfig) { |
||||
mDownloadConfig.connectTimeOut = time; |
||||
} |
||||
if (isUploadConfig) { |
||||
mUploadConfig.connectTimeOut = time; |
||||
} |
||||
} |
||||
|
||||
private void loadReTry(String value) { |
||||
int num = 0; |
||||
if (!TextUtils.isEmpty(value)) { |
||||
num = Integer.parseInt(value); |
||||
} |
||||
|
||||
if (isDownloadConfig) { |
||||
mDownloadConfig.reTryNum = num; |
||||
} |
||||
if (isUploadConfig) { |
||||
mUploadConfig.reTryNum = num; |
||||
} |
||||
} |
||||
|
||||
private void loadMaxQueue(String value) { |
||||
int num = 2; |
||||
if (!TextUtils.isEmpty(value)) { |
||||
num = Integer.parseInt(value); |
||||
} |
||||
if (num < 1) { |
||||
Log.e(TAG, "任务队列数不能小于 1"); |
||||
num = 2; |
||||
} |
||||
if (isDownloadConfig) { |
||||
mDownloadConfig.maxTaskNum = num; |
||||
} |
||||
if (isUploadConfig) { |
||||
mUploadConfig.maxTaskNum = num; |
||||
} |
||||
} |
||||
|
||||
private void loadBroadcast(String value) { |
||||
boolean open = Boolean.parseBoolean(value); |
||||
if (isDownloadConfig) { |
||||
mDownloadConfig.isOpenBreadCast = open; |
||||
} |
||||
if (isUploadConfig) { |
||||
mUploadConfig.isOpenBreadCast = open; |
||||
} |
||||
} |
||||
|
||||
private void loadThreadNum(String value) { |
||||
int num = 3; |
||||
if (!TextUtils.isEmpty(value)) { |
||||
num = Integer.parseInt(value); |
||||
} |
||||
if (num < 1) { |
||||
Log.e(TAG, "下载线程数不能小于 1"); |
||||
num = 3; |
||||
} |
||||
if (isDownloadConfig) { |
||||
mDownloadConfig.threadNum = num; |
||||
} |
||||
} |
||||
|
||||
@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.saveAll(); |
||||
mUploadConfig.saveAll(); |
||||
} |
||||
} |
@ -1,228 +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.util; |
||||
|
||||
import android.util.Log; |
||||
import com.arialyy.aria.core.AriaManager; |
||||
import java.io.File; |
||||
import java.io.IOException; |
||||
import java.util.Map; |
||||
import java.util.Properties; |
||||
import java.util.Set; |
||||
import java.util.WeakHashMap; |
||||
|
||||
/** |
||||
* Created by AriaL on 2016/12/8. |
||||
* 信息配置 |
||||
*/ |
||||
public class Configuration_1 { |
||||
private static final String TAG = "Configuration_1"; |
||||
private static final String CONFIG_FILE = "/Aria/ADConfig.properties"; |
||||
/** |
||||
* 当前调度器最大下载数,默认最大下载数为 “2” |
||||
*/ |
||||
private static final String DOWNLOAD_NUM = "DOWNLOAD_NUM"; |
||||
/** |
||||
* 失败重试次数,默认最多重试 10 次 |
||||
*/ |
||||
private static final String RE_TRY_NUM = "RE_TRY_NUM"; |
||||
/** |
||||
* 是否打开下载广播,默认 false |
||||
*/ |
||||
private static final String OPEN_BROADCAST = "OPEN_BROADCAST"; |
||||
/** |
||||
* 失败重试间隔时间,默认 4000 ms |
||||
*/ |
||||
private static final String RE_TRY_INTERVAL = "RE_TRY_INTERVAL"; |
||||
/** |
||||
* 超时时间,默认 10000 ms |
||||
*/ |
||||
private static final String DOWNLOAD_TIME_OUT = "DOWNLOAD_TIME_OUT"; |
||||
/** |
||||
* 设置最大速度 |
||||
*/ |
||||
private static final String MAX_SPEED = "MAX_SPEED"; |
||||
|
||||
public static boolean isOpenBreadCast = false; |
||||
|
||||
private static Configuration_1 INSTANCE = null; |
||||
private File mConfigFile = null; |
||||
private static final Object LOCK = new Object(); |
||||
|
||||
public static Configuration_1 getInstance() { |
||||
if (INSTANCE == null) { |
||||
synchronized (LOCK) { |
||||
INSTANCE = new Configuration_1(); |
||||
} |
||||
} |
||||
return INSTANCE; |
||||
} |
||||
|
||||
private Configuration_1() { |
||||
mConfigFile = new File(AriaManager.APP.getFilesDir().getPath() + CONFIG_FILE); |
||||
try { |
||||
if (!mConfigFile.exists()) { |
||||
mConfigFile.getParentFile().mkdirs(); |
||||
mConfigFile.createNewFile(); |
||||
init(); |
||||
} else { |
||||
isOpenBreadCast = isOpenBroadcast(); |
||||
} |
||||
} catch (IOException e) { |
||||
e.printStackTrace(); |
||||
} |
||||
} |
||||
|
||||
private void init() { |
||||
Map<String, String> config = new WeakHashMap<>(); |
||||
config.put(DOWNLOAD_NUM, 2 + ""); |
||||
config.put(RE_TRY_NUM, 10 + ""); |
||||
config.put(OPEN_BROADCAST, false + ""); |
||||
config.put(RE_TRY_INTERVAL, 4000 + ""); |
||||
config.put(DOWNLOAD_TIME_OUT, 10000 + ""); |
||||
config.put(MAX_SPEED, 64 + ""); |
||||
saveConfig(config); |
||||
} |
||||
|
||||
private void saveConfig(Map<String, String> config) { |
||||
if (config == null || config.size() == 0) { |
||||
return; |
||||
} |
||||
Properties properties = CommonUtil.loadConfig(mConfigFile); |
||||
Set<String> keys = config.keySet(); |
||||
for (String key : keys) { |
||||
properties.setProperty(key, config.get(key)); |
||||
} |
||||
CommonUtil.saveConfig(mConfigFile, properties); |
||||
} |
||||
|
||||
private void save(String key, String value) { |
||||
Map<String, String> map = new WeakHashMap<>(); |
||||
map.put(key, value); |
||||
saveConfig(map); |
||||
} |
||||
|
||||
/** |
||||
* 设置最大下载速度 |
||||
*/ |
||||
public void setMaxSpeed(Speed speed) { |
||||
save(MAX_SPEED, speed.buf + ""); |
||||
} |
||||
|
||||
/** |
||||
* 获取最大速度 |
||||
*/ |
||||
public int getMaxSpeed() { |
||||
return Integer.parseInt(CommonUtil.loadConfig(mConfigFile).getProperty(MAX_SPEED, "8192")); |
||||
} |
||||
|
||||
/** |
||||
* 获取下载超时时间 |
||||
* |
||||
* @return 默认4000ms |
||||
*/ |
||||
public int getTimeOut() { |
||||
return Integer.parseInt(CommonUtil.loadConfig(mConfigFile).getProperty(DOWNLOAD_TIME_OUT)); |
||||
} |
||||
|
||||
/** |
||||
* 设置重试间隔 |
||||
*/ |
||||
public void setTimeOut(int timeOut) { |
||||
if (timeOut < 10000) { |
||||
Log.w(TAG, "下载超时时间不能小于 10000 ms"); |
||||
return; |
||||
} |
||||
save(DOWNLOAD_TIME_OUT, timeOut + ""); |
||||
} |
||||
|
||||
/** |
||||
* 获取失败重试间隔时间 |
||||
* |
||||
* @return 默认4000ms |
||||
*/ |
||||
public int getReTryInterval() { |
||||
return Integer.parseInt(CommonUtil.loadConfig(mConfigFile).getProperty(RE_TRY_INTERVAL)); |
||||
} |
||||
|
||||
/** |
||||
* 设置重试间隔 |
||||
*/ |
||||
public void setReTryInterval(int reTryInterval) { |
||||
if (reTryInterval < 4000) { |
||||
Log.w(TAG, "重试间隔不能小于4000ms"); |
||||
return; |
||||
} |
||||
save(RE_TRY_INTERVAL, reTryInterval + ""); |
||||
} |
||||
|
||||
/** |
||||
* 获取最大下载数 |
||||
* |
||||
* @return 默认返回2 |
||||
*/ |
||||
public int getDownloadNum() { |
||||
return Integer.parseInt(CommonUtil.loadConfig(mConfigFile).getProperty(DOWNLOAD_NUM)); |
||||
} |
||||
|
||||
/** |
||||
* 设置最大下载数 |
||||
*/ |
||||
public void setDownloadNum(int downloadNum) { |
||||
if (downloadNum < 1) { |
||||
Log.w(TAG, "最大下载数不能小于1"); |
||||
return; |
||||
} |
||||
save(DOWNLOAD_NUM, downloadNum + ""); |
||||
} |
||||
|
||||
/** |
||||
* 获取最大重试数 |
||||
* |
||||
* @return 默认返回 10 |
||||
*/ |
||||
public int getReTryNum() { |
||||
return Integer.parseInt(CommonUtil.loadConfig(mConfigFile).getProperty(RE_TRY_NUM)); |
||||
} |
||||
|
||||
/** |
||||
* 设置重试数 |
||||
*/ |
||||
public void setReTryNum(int reTryNum) { |
||||
if (reTryNum < 1) { |
||||
Log.w(TAG, "最大下载数不能小于1"); |
||||
return; |
||||
} |
||||
save(RE_TRY_NUM, reTryNum + ""); |
||||
} |
||||
|
||||
/** |
||||
* 是否打开下载广播 |
||||
* |
||||
* @return 默认false |
||||
*/ |
||||
public boolean isOpenBroadcast() { |
||||
return Boolean.parseBoolean(CommonUtil.loadConfig(mConfigFile).getProperty(RE_TRY_NUM)); |
||||
} |
||||
|
||||
/** |
||||
* 设置是否打开下载广播 |
||||
*/ |
||||
public void setOpenBroadcast(boolean openBroadcast) { |
||||
isOpenBreadCast = openBroadcast; |
||||
save(OPEN_BROADCAST, openBroadcast + ""); |
||||
} |
||||
} |
@ -0,0 +1,113 @@ |
||||
/* |
||||
* 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.util; |
||||
|
||||
import android.util.Log; |
||||
import java.lang.reflect.Field; |
||||
import java.lang.reflect.Method; |
||||
import java.util.ArrayList; |
||||
import java.util.Collections; |
||||
import java.util.List; |
||||
|
||||
/** |
||||
* Created by “AriaLyy@outlook.com” on 2015/7/30. |
||||
* 反射工具类 |
||||
*/ |
||||
public class ReflectionUtil { |
||||
private static final String TAG = "ReflectionUtil"; |
||||
|
||||
/** |
||||
* 获取类里面的所在字段 |
||||
*/ |
||||
public static Field[] getFields(Class clazz) { |
||||
Field[] fields = null; |
||||
fields = clazz.getDeclaredFields(); |
||||
if (fields == null || fields.length == 0) { |
||||
Class superClazz = clazz.getSuperclass(); |
||||
if (superClazz != null) { |
||||
fields = getFields(superClazz); |
||||
} |
||||
} |
||||
return fields; |
||||
} |
||||
|
||||
/** |
||||
* 获取所有字段,包括父类的字段 |
||||
*/ |
||||
public static List<Field> getAllFields(Class clazz) { |
||||
List<Field> fields = new ArrayList<>(); |
||||
Class personClazz = clazz.getSuperclass(); |
||||
if (personClazz != null) { |
||||
Collections.addAll(fields, personClazz.getDeclaredFields()); |
||||
} |
||||
Collections.addAll(fields, clazz.getDeclaredFields()); |
||||
return fields; |
||||
} |
||||
|
||||
/** |
||||
* 获取类里面的指定对象,如果该类没有则从父类查询 |
||||
*/ |
||||
public static Field getField(Class clazz, String name) { |
||||
Field field = null; |
||||
try { |
||||
field = clazz.getDeclaredField(name); |
||||
} catch (NoSuchFieldException e) { |
||||
try { |
||||
field = clazz.getField(name); |
||||
} catch (NoSuchFieldException e1) { |
||||
if (clazz.getSuperclass() == null) { |
||||
return field; |
||||
} else { |
||||
field = getField(clazz.getSuperclass(), name); |
||||
} |
||||
} |
||||
} |
||||
if (field != null) { |
||||
field.setAccessible(true); |
||||
} |
||||
return field; |
||||
} |
||||
|
||||
/** |
||||
* 利用递归找一个类的指定方法,如果找不到,去父亲里面找直到最上层Object对象为止。 |
||||
* |
||||
* @param clazz 目标类 |
||||
* @param methodName 方法名 |
||||
* @param params 方法参数类型数组 |
||||
* @return 方法对象 |
||||
*/ |
||||
public static Method getMethod(Class clazz, String methodName, final Class<?>... params) { |
||||
Method method = null; |
||||
try { |
||||
method = clazz.getDeclaredMethod(methodName, params); |
||||
} catch (NoSuchMethodException e) { |
||||
try { |
||||
method = clazz.getMethod(methodName, params); |
||||
} catch (NoSuchMethodException ex) { |
||||
if (clazz.getSuperclass() == null) { |
||||
Log.e(TAG, "无法找到" + methodName + "方法"); |
||||
return method; |
||||
} else { |
||||
method = getMethod(clazz.getSuperclass(), methodName, params); |
||||
} |
||||
} |
||||
} |
||||
if (method != null) { |
||||
method.setAccessible(true); |
||||
} |
||||
return method; |
||||
} |
||||
} |
Loading…
Reference in new issue