From e4ba3df523c6dcec08cb12dd3a832e15f1260f2f Mon Sep 17 00:00:00 2001 From: AriaLyy <511455842@qq.com> Date: Mon, 22 May 2017 18:10:31 +0800 Subject: [PATCH 01/11] =?UTF-8?q?=E9=87=8D=E6=9E=84=E8=AE=BE=E7=BD=AE?= =?UTF-8?q?=E9=83=A8=E5=88=86=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/arialyy/aria/core/AriaManager.java | 120 ++++------ .../com/arialyy/aria/core/ConfigHelper.java | 221 ++++++++++++++++++ .../com/arialyy/aria/core/Configuration.java | 220 +++++++++++++++-- .../aria/core/download/DownloadTask.java | 22 +- .../arialyy/aria/core/queue/AbsTaskQueue.java | 1 - .../aria/core/queue/DownloadTaskQueue.java | 5 +- .../aria/core/queue/UploadTaskQueue.java | 3 + .../aria/core/queue/pool/ExecutePool.java | 18 +- .../core/scheduler/DownloadSchedulers.java | 17 +- .../aria/core/scheduler/UploadSchedulers.java | 16 +- .../arialyy/aria/core/upload/UploadTask.java | 27 +-- .../com/arialyy/aria/util/ReflectionUtil.java | 113 +++++++++ app/src/main/assets/aria_config.xml | 13 +- .../java/com/arialyy/simple/MainActivity.java | 1 + .../simple/download/SingleTaskActivity.java | 58 ++--- .../multi_download/DownloadAdapter.java | 4 - .../multi_download/MultiTaskActivity.java | 2 +- 17 files changed, 681 insertions(+), 180 deletions(-) create mode 100644 Aria/src/main/java/com/arialyy/aria/core/ConfigHelper.java create mode 100644 Aria/src/main/java/com/arialyy/aria/util/ReflectionUtil.java diff --git a/Aria/src/main/java/com/arialyy/aria/core/AriaManager.java b/Aria/src/main/java/com/arialyy/aria/core/AriaManager.java index 98cf7a27..639649eb 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/AriaManager.java +++ b/Aria/src/main/java/com/arialyy/aria/core/AriaManager.java @@ -26,22 +26,23 @@ import android.os.Build; import android.os.Bundle; import android.support.v4.app.Fragment; import android.text.TextUtils; -import android.util.Log; import android.widget.PopupWindow; import com.arialyy.aria.core.download.DownloadReceiver; import com.arialyy.aria.core.inf.ICmd; import com.arialyy.aria.core.inf.IReceiver; -import com.arialyy.aria.core.queue.DownloadTaskQueue; import com.arialyy.aria.core.upload.UploadReceiver; import com.arialyy.aria.orm.DbUtil; -import com.arialyy.aria.util.CAConfiguration; -import com.arialyy.aria.util.Configuration_1; -import com.arialyy.aria.util.Speed; +import java.io.File; +import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.parsers.SAXParser; +import javax.xml.parsers.SAXParserFactory; +import org.xml.sax.SAXException; /** * Created by lyy on 2016/12/1. @@ -57,12 +58,20 @@ import java.util.Map; private Map mReceivers = new HashMap<>(); public static Context APP; private List mCommands = new ArrayList<>(); - private Configuration mConfig; + private Configuration.DownloadConfig mDConfig; + private Configuration.UploadConfig mUConfig; private AriaManager(Context context) { DbUtil.init(context.getApplicationContext()); APP = context.getApplicationContext(); regAppLifeCallback(context); + File dFile = new File(Configuration.DOWNLOAD_CONFIG_FILE); + File uFile = new File(Configuration.UPLOAD_CONFIG_FILE); + if (!dFile.exists() || !uFile.exists()) { + loadConfig(); + } + mDConfig = Configuration.DownloadConfig.getInstance(); + mUConfig = Configuration.UploadConfig.getInstance(); } public static AriaManager getInstance(Context context) { @@ -79,10 +88,39 @@ import java.util.Map; } /** - * 设置最大下载速度 + * 加载配置文件 + */ + private void loadConfig() { + try { + ConfigHelper helper = new ConfigHelper(); + SAXParserFactory factory = SAXParserFactory.newInstance(); + SAXParser parser = factory.newSAXParser(); + parser.parse(APP.getAssets().open("aria_config.xml"), helper); + } catch (ParserConfigurationException | IOException | SAXException e) { + e.printStackTrace(); + } + } + + /** + * 如果需要在代码中修改下载配置,请使用以下方法 + * + * @ //修改最大任务队列数 + * Aria.get(this).getDownloadConfig().setMaxTaskNum(3).save(); + * + */ + public Configuration.DownloadConfig getDownloadConfig() { + return mDConfig; + } + + /** + * 如果需要在代码中修改下载配置,请使用以下方法 + * + * @ //修改最大任务队列数 + * Aria.get(this).getUploadConfig().setMaxTaskNum(3).save(); + * */ - public void setMaxSpeed(Speed speed) { - Configuration_1.getInstance().setMaxSpeed(speed); + public Configuration.UploadConfig getUploadConfig() { + return mUConfig; } /** @@ -135,70 +173,6 @@ import java.util.Map; return (receiver instanceof UploadReceiver) ? (UploadReceiver) receiver : null; } - /** - * 设置CA证书信息 - * - * @param caAlias ca证书别名 - * @param caPath assets 文件夹下的ca证书完整路径 - */ - public void setCAInfo(String caAlias, String caPath) { - if (TextUtils.isEmpty(caAlias)) { - Log.e(TAG, "ca证书别名不能为null"); - return; - } else if (TextUtils.isEmpty(caPath)) { - Log.e(TAG, "ca证书路径不能为null"); - return; - } - CAConfiguration.CA_ALIAS = caAlias; - CAConfiguration.CA_PATH = caPath; - } - - /** - * 设置下载超时时间 - */ - @Deprecated private AriaManager setTimeOut(int timeOut) { - Configuration_1.getInstance().setTimeOut(timeOut); - return this; - } - - /** - * 设置失败重试次数 - */ - public AriaManager setReTryNum(int reTryNum) { - Configuration_1.getInstance().setReTryNum(reTryNum); - return this; - } - - /** - * 设置失败重试间隔 - */ - public AriaManager setReTryInterval(int interval) { - Configuration_1.getInstance().setReTryInterval(interval); - return this; - } - - /** - * 是否打开下载广播 - */ - public AriaManager openBroadcast(boolean open) { - Configuration_1.getInstance().setOpenBroadcast(open); - return this; - } - - /** - * 设置最大下载数,最大下载数不能小于1 - * - * @param maxDownloadNum 最大下载数 - */ - public AriaManager setMaxDownloadNum(int maxDownloadNum) { - if (maxDownloadNum < 1) { - Log.w(TAG, "最大任务数不能小于 1"); - return this; - } - DownloadTaskQueue.getInstance().setDownloadNum(maxDownloadNum); - return this; - } - private IReceiver putReceiver(boolean isDownload, Object obj) { final String key = getKey(isDownload, obj); IReceiver receiver = mReceivers.get(key); diff --git a/Aria/src/main/java/com/arialyy/aria/core/ConfigHelper.java b/Aria/src/main/java/com/arialyy/aria/core/ConfigHelper.java new file mode 100644 index 00000000..db5f37a9 --- /dev/null +++ b/Aria/src/main/java/com/arialyy/aria/core/ConfigHelper.java @@ -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; + +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; + } + } + } + + 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.save(); + mUploadConfig.save(); + } +} diff --git a/Aria/src/main/java/com/arialyy/aria/core/Configuration.java b/Aria/src/main/java/com/arialyy/aria/core/Configuration.java index 1c11d5f6..f7a1aef7 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/Configuration.java +++ b/Aria/src/main/java/com/arialyy/aria/core/Configuration.java @@ -15,36 +15,157 @@ */ package com.arialyy.aria.core; +import com.arialyy.aria.util.CommonUtil; +import com.arialyy.aria.util.ReflectionUtil; +import java.io.File; +import java.lang.reflect.Field; +import java.lang.reflect.Modifier; +import java.util.List; +import java.util.Properties; + /** * Created by AriaL on 2016/12/8. * 信息配置 */ class Configuration { - private static final String TAG = "Configuration"; - private static final String CONFIG_FILE = "/Aria/ADConfig.properties"; - private static final String CONFIG_KEY = "ARIA_CONFIG"; + static final String DOWNLOAD_CONFIG_FILE = "/Aria/DownloadConfig.properties"; + static final String UPLOAD_CONFIG_FILE = "/Aria/UploadConfig.properties"; /** * 通用配置 */ - static class BaseConfig { - public boolean isOpenBreadCast = false; + public static class BaseConfig { + + /** + * 是否发送任务广播,true,发送 + */ + boolean isOpenBreadCast = false; /** * 任务队列最大任务数, 默认为2 */ - public int maxQueueNum = 2; + int maxTaskNum = 2; /** * 下载失败,重试次数,默认为10 */ - public int reTryNum = 10; + int reTryNum = 10; /** * 设置重试间隔,单位为毫秒,默认2000毫秒 */ - public long reTryInterval = 2000; + long reTryInterval = 2000; /** * 设置url连接超时时间,单位为毫秒,默认5000毫秒 */ - public long connectTimeOut = 5000; + long connectTimeOut = 5000; + + public boolean isOpenBreadCast() { + return isOpenBreadCast; + } + + public BaseConfig setOpenBreadCast(boolean openBreadCast) { + isOpenBreadCast = openBreadCast; + return this; + } + + public int getMaxTaskNum() { + return maxTaskNum; + } + + public BaseConfig setMaxTaskNum(int maxTaskNum) { + this.maxTaskNum = maxTaskNum; + return this; + } + + public int getReTryNum() { + return reTryNum; + } + + public BaseConfig setReTryNum(int reTryNum) { + this.reTryNum = reTryNum; + return this; + } + + public long getReTryInterval() { + return reTryInterval; + } + + public BaseConfig setReTryInterval(long reTryInterval) { + this.reTryInterval = reTryInterval; + return this; + } + + public long getConnectTimeOut() { + return connectTimeOut; + } + + public BaseConfig setConnectTimeOut(long connectTimeOut) { + this.connectTimeOut = connectTimeOut; + return this; + } + + /** + * 加载配置 + */ + void loadConfig() { + boolean isDownload = this instanceof DownloadConfig; + File file = new File( + AriaManager.APP.getFilesDir().getPath() + (isDownload ? DOWNLOAD_CONFIG_FILE + : UPLOAD_CONFIG_FILE)); + if (file.exists()) { + Properties properties = CommonUtil.loadConfig(file); + List fields = ReflectionUtil.getAllFields(getClass()); + try { + for (Field field : fields) { + int m = field.getModifiers(); + if (Modifier.isFinal(m) || Modifier.isStatic(m)) { + continue; + } + field.setAccessible(true); + String value = properties.getProperty(field.getName()); + 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) { + 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(); + } + } + } + + /** + * 保存配置 + */ + public void save() { + List fields = ReflectionUtil.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(); + } + } } /** @@ -54,19 +175,78 @@ class Configuration { /** * 设置IO流读取时间,单位为毫秒,默认20000毫秒,该时间不能少于10000毫秒 */ - public long readTimeout = 20 * 1000; + long iOTimeOut = 20 * 1000; /** * 设置写文件buff大小,该数值大小不能小于2048,数值变小,下载速度会变慢 */ - public int buffSize = 8192; + int buffSize = 8192; /** * 设置https ca 证书信息;path 为assets目录下的CA证书完整路径 */ - public String caPath; + String caPath; /** * name 为CA证书名 */ - public String caName; + String caName; + /** + * 下载线程数,下载线程数不能小于1 + */ + int threadNum = 3; + + public long getiOTimeOut() { + return iOTimeOut; + } + + public DownloadConfig setiOTimeOut(long iOTimeOut) { + this.iOTimeOut = iOTimeOut; + return this; + } + + public int getBuffSize() { + return buffSize; + } + + public DownloadConfig setBuffSize(int buffSize) { + this.buffSize = buffSize; + return this; + } + + public String getCaPath() { + return caPath; + } + + public DownloadConfig setCaPath(String caPath) { + this.caPath = caPath; + return this; + } + + public String getCaName() { + return caName; + } + + public DownloadConfig setCaName(String caName) { + this.caName = caName; + return this; + } + + public int getThreadNum() { + return threadNum; + } + + private DownloadConfig() { + loadConfig(); + } + + private static DownloadConfig INSTANCE = null; + + static DownloadConfig getInstance() { + if (INSTANCE == null) { + synchronized (DownloadConfig.class) { + INSTANCE = new DownloadConfig(); + } + } + return INSTANCE; + } } /** @@ -74,5 +254,19 @@ class Configuration { */ public static class UploadConfig extends BaseConfig { + private UploadConfig() { + loadConfig(); + } + + private static UploadConfig INSTANCE = null; + + static UploadConfig getInstance() { + if (INSTANCE == null) { + synchronized (DownloadConfig.class) { + INSTANCE = new UploadConfig(); + } + } + return INSTANCE; + } } } diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadTask.java b/Aria/src/main/java/com/arialyy/aria/core/download/DownloadTask.java index 6be7b99e..35645c25 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadTask.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/DownloadTask.java @@ -27,7 +27,6 @@ import com.arialyy.aria.core.scheduler.DownloadSchedulers; import com.arialyy.aria.core.scheduler.ISchedulers; import com.arialyy.aria.util.CheckUtil; import com.arialyy.aria.util.CommonUtil; -import com.arialyy.aria.util.Configuration_1; import java.lang.ref.WeakReference; /** @@ -178,22 +177,6 @@ public class DownloadTask implements ITask { intent.putExtra(Aria.ENTITY, mEntity); mContext.sendBroadcast(intent); } - //if (mEntity.isDownloadComplete()) { - // //mUtil.cancelDownload(); - //} else { - // // 如果任务不是下载状态 - // mUtil.cancelDownload(); - // mUtil.delConfigFile(); - // mUtil.delTempFile(); - // mEntity.deleteData(); - // if (mOutHandler != null) { - // mOutHandler.obtainMessage(DownloadSchedulers.CANCEL, this).sendToTarget(); - // } - // //发送取消下载的广播 - // Intent intent = CommonUtil.createIntent(mContext.getPackageName(), Aria.ACTION_CANCEL); - // intent.putExtra(Aria.ENTITY, mEntity); - // mContext.sendBroadcast(intent); - //} } public static class Builder { @@ -203,7 +186,6 @@ public class DownloadTask implements ITask { String targetName; public Builder(String targetName, DownloadTaskEntity taskEntity) { - //CheckUtil.checkDownloadTaskEntity(taskEntity.downloadEntity); CheckUtil.checkTaskEntity(taskEntity); this.targetName = targetName; this.taskEntity = taskEntity; @@ -250,6 +232,7 @@ public class DownloadTask implements ITask { boolean isFirst = true; DownloadEntity downloadEntity; DownloadTask task; + boolean isOpenBroadCast = false; DListener(Context context, DownloadTask task, Handler outHandler) { this.context = context; @@ -259,6 +242,7 @@ public class DownloadTask implements ITask { this.downloadEntity = this.task.getDownloadEntity(); sendIntent = CommonUtil.createIntent(context.getPackageName(), Aria.ACTION_RUNNING); sendIntent.putExtra(Aria.ENTITY, downloadEntity); + isOpenBroadCast = AriaManager.getInstance(context).getDownloadConfig().isOpenBreadCast(); } @Override public void supportBreakpoint(boolean support) { @@ -366,7 +350,7 @@ public class DownloadTask implements ITask { downloadEntity.setDownloadComplete(action.equals(Aria.ACTION_COMPLETE)); downloadEntity.setCurrentProgress(location); downloadEntity.update(); - if (!Configuration_1.isOpenBreadCast) return; + if (!isOpenBroadCast) return; Intent intent = CommonUtil.createIntent(context.getPackageName(), action); intent.putExtra(Aria.ENTITY, downloadEntity); if (location != -1) { diff --git a/Aria/src/main/java/com/arialyy/aria/core/queue/AbsTaskQueue.java b/Aria/src/main/java/com/arialyy/aria/core/queue/AbsTaskQueue.java index a61b12fe..7fc1d722 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/queue/AbsTaskQueue.java +++ b/Aria/src/main/java/com/arialyy/aria/core/queue/AbsTaskQueue.java @@ -28,5 +28,4 @@ import com.arialyy.aria.core.queue.pool.ExecutePool; abstract class AbsTaskQueue implements ITaskQueue { CachePool mCachePool = new CachePool<>(); - ExecutePool mExecutePool = new ExecutePool<>(); } diff --git a/Aria/src/main/java/com/arialyy/aria/core/queue/DownloadTaskQueue.java b/Aria/src/main/java/com/arialyy/aria/core/queue/DownloadTaskQueue.java index 7bb7eba8..073621ef 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/queue/DownloadTaskQueue.java +++ b/Aria/src/main/java/com/arialyy/aria/core/queue/DownloadTaskQueue.java @@ -18,6 +18,7 @@ package com.arialyy.aria.core.queue; import android.text.TextUtils; import android.util.Log; +import com.arialyy.aria.core.AriaManager; import com.arialyy.aria.core.download.DownloadEntity; import com.arialyy.aria.core.download.DownloadTask; import com.arialyy.aria.core.download.DownloadTaskEntity; @@ -25,7 +26,6 @@ import com.arialyy.aria.core.inf.IEntity; import com.arialyy.aria.core.queue.pool.CachePool; import com.arialyy.aria.core.queue.pool.ExecutePool; import com.arialyy.aria.core.scheduler.DownloadSchedulers; -import com.arialyy.aria.util.Configuration_1; /** * Created by lyy on 2016/8/17. @@ -36,6 +36,7 @@ public class DownloadTaskQueue private static final String TAG = "DownloadTaskQueue"; private static volatile DownloadTaskQueue INSTANCE = null; private static final Object LOCK = new Object(); + private ExecutePool mExecutePool = new ExecutePool<>(true); public static DownloadTaskQueue getInstance() { if (INSTANCE == null) { @@ -121,7 +122,7 @@ public class DownloadTaskQueue @Override public void setDownloadNum(int downloadNum) { //原始长度 - int size = Configuration_1.getInstance().getDownloadNum(); + int size = AriaManager.getInstance(AriaManager.APP).getUploadConfig().getMaxTaskNum(); int diff = downloadNum - size; if (size == downloadNum) { Log.d(TAG, "设置的下载任务数和配置文件的下载任务数一直,跳过"); diff --git a/Aria/src/main/java/com/arialyy/aria/core/queue/UploadTaskQueue.java b/Aria/src/main/java/com/arialyy/aria/core/queue/UploadTaskQueue.java index 0dc69cee..8ce43a40 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/queue/UploadTaskQueue.java +++ b/Aria/src/main/java/com/arialyy/aria/core/queue/UploadTaskQueue.java @@ -18,6 +18,8 @@ package com.arialyy.aria.core.queue; import android.text.TextUtils; import android.util.Log; +import com.arialyy.aria.core.download.DownloadTask; +import com.arialyy.aria.core.queue.pool.ExecutePool; import com.arialyy.aria.core.scheduler.UploadSchedulers; import com.arialyy.aria.core.upload.UploadEntity; import com.arialyy.aria.core.upload.UploadTask; @@ -31,6 +33,7 @@ public class UploadTaskQueue extends AbsTaskQueue mExecutePool = new ExecutePool<>(false); public static UploadTaskQueue getInstance() { if (INSTANCE == null) { diff --git a/Aria/src/main/java/com/arialyy/aria/core/queue/pool/ExecutePool.java b/Aria/src/main/java/com/arialyy/aria/core/queue/pool/ExecutePool.java index 29d23972..e3a1acd5 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/queue/pool/ExecutePool.java +++ b/Aria/src/main/java/com/arialyy/aria/core/queue/pool/ExecutePool.java @@ -18,9 +18,9 @@ package com.arialyy.aria.core.queue.pool; import android.text.TextUtils; import android.util.Log; +import com.arialyy.aria.core.AriaManager; import com.arialyy.aria.core.inf.ITask; import com.arialyy.aria.util.CommonUtil; -import com.arialyy.aria.util.Configuration_1; import java.util.HashMap; import java.util.Map; import java.util.concurrent.ArrayBlockingQueue; @@ -37,9 +37,15 @@ public class ExecutePool implements IPool { private ArrayBlockingQueue mExecuteQueue; private Map mExecuteArray; private int mSize; + private boolean isDownload; - public ExecutePool() { - mSize = Configuration_1.getInstance().getDownloadNum(); + public ExecutePool(boolean isDownload) { + this.isDownload = isDownload; + if (isDownload) { + mSize = AriaManager.getInstance(AriaManager.APP).getDownloadConfig().getMaxTaskNum(); + } else { + mSize = AriaManager.getInstance(AriaManager.APP).getUploadConfig().getMaxTaskNum(); + } mExecuteQueue = new ArrayBlockingQueue<>(mSize); mExecuteArray = new HashMap<>(); } @@ -81,7 +87,11 @@ public class ExecutePool implements IPool { } mExecuteQueue = temp; mSize = downloadNum; - Configuration_1.getInstance().setDownloadNum(mSize); + if (isDownload) { + AriaManager.getInstance(AriaManager.APP).getDownloadConfig().setMaxTaskNum(downloadNum); + } else { + AriaManager.getInstance(AriaManager.APP).getUploadConfig().setMaxTaskNum(downloadNum); + } } catch (InterruptedException e) { e.printStackTrace(); } diff --git a/Aria/src/main/java/com/arialyy/aria/core/scheduler/DownloadSchedulers.java b/Aria/src/main/java/com/arialyy/aria/core/scheduler/DownloadSchedulers.java index 21bb1189..b1ff3ddb 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/scheduler/DownloadSchedulers.java +++ b/Aria/src/main/java/com/arialyy/aria/core/scheduler/DownloadSchedulers.java @@ -19,10 +19,10 @@ package com.arialyy.aria.core.scheduler; import android.os.CountDownTimer; import android.os.Message; import android.util.Log; +import com.arialyy.aria.core.AriaManager; import com.arialyy.aria.core.queue.DownloadTaskQueue; import com.arialyy.aria.core.download.DownloadEntity; import com.arialyy.aria.core.download.DownloadTask; -import com.arialyy.aria.util.Configuration_1; import java.util.Iterator; import java.util.Map; import java.util.Set; @@ -86,7 +86,9 @@ public class DownloadSchedulers implements ISchedulers { case STOP: case CANCEL: mQueue.removeTask(entity); - if (mQueue.size() < Configuration_1.getInstance().getDownloadNum()) { + if (mQueue.size() < AriaManager.getInstance(AriaManager.APP) + .getUploadConfig() + .getMaxTaskNum()) { startNextTask(); } break; @@ -163,19 +165,22 @@ public class DownloadSchedulers implements ISchedulers { * @param task 下载任务 */ private void handleFailTask(final DownloadTask task) { - final Configuration_1 config = Configuration_1.getInstance(); - CountDownTimer timer = new CountDownTimer(config.getReTryInterval(), 1000) { + final long interval = + AriaManager.getInstance(AriaManager.APP).getUploadConfig().getReTryInterval(); + final int reTryNum = AriaManager.getInstance(AriaManager.APP).getUploadConfig().getReTryNum(); + + CountDownTimer timer = new CountDownTimer(interval, 1000) { @Override public void onTick(long millisUntilFinished) { } @Override public void onFinish() { DownloadEntity entity = task.getDownloadEntity(); - if (entity.getFailNum() < config.getReTryNum()) { + if (entity.getFailNum() < reTryNum) { DownloadTask task = mQueue.getTask(entity); mQueue.reTryStart(task); try { - Thread.sleep(config.getReTryInterval()); + Thread.sleep(interval); } catch (InterruptedException e) { e.printStackTrace(); } diff --git a/Aria/src/main/java/com/arialyy/aria/core/scheduler/UploadSchedulers.java b/Aria/src/main/java/com/arialyy/aria/core/scheduler/UploadSchedulers.java index 8c2777d2..0b2dd657 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/scheduler/UploadSchedulers.java +++ b/Aria/src/main/java/com/arialyy/aria/core/scheduler/UploadSchedulers.java @@ -18,11 +18,11 @@ package com.arialyy.aria.core.scheduler; import android.os.CountDownTimer; import android.os.Message; import android.util.Log; +import com.arialyy.aria.core.AriaManager; import com.arialyy.aria.core.inf.IEntity; import com.arialyy.aria.core.queue.UploadTaskQueue; import com.arialyy.aria.core.upload.UploadEntity; import com.arialyy.aria.core.upload.UploadTask; -import com.arialyy.aria.util.Configuration_1; import java.util.Iterator; import java.util.Map; import java.util.Set; @@ -69,19 +69,21 @@ public class UploadSchedulers implements ISchedulers { } private void handleFailTask(final UploadTask task) { - final Configuration_1 config = Configuration_1.getInstance(); - CountDownTimer timer = new CountDownTimer(config.getReTryInterval(), 1000) { + final long interval = + AriaManager.getInstance(AriaManager.APP).getUploadConfig().getReTryInterval(); + final int reTryNum = AriaManager.getInstance(AriaManager.APP).getUploadConfig().getReTryNum(); + CountDownTimer timer = new CountDownTimer(interval, 1000) { @Override public void onTick(long millisUntilFinished) { } @Override public void onFinish() { UploadEntity entity = task.getUploadEntity(); - if (entity.getFailNum() <= config.getReTryNum()) { + if (entity.getFailNum() <= reTryNum) { UploadTask task = mQueue.getTask(entity); mQueue.reTryStart(task); try { - Thread.sleep(config.getReTryInterval()); + Thread.sleep(interval); } catch (InterruptedException e) { e.printStackTrace(); } @@ -169,7 +171,9 @@ public class UploadSchedulers implements ISchedulers { case STOP: case CANCEL: mQueue.removeTask(entity); - if (mQueue.size() < Configuration_1.getInstance().getDownloadNum()) { + if (mQueue.size() < AriaManager.getInstance(AriaManager.APP) + .getUploadConfig() + .getMaxTaskNum()) { startNextTask(); } break; diff --git a/Aria/src/main/java/com/arialyy/aria/core/upload/UploadTask.java b/Aria/src/main/java/com/arialyy/aria/core/upload/UploadTask.java index 9ae43bcb..8772b9fd 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/upload/UploadTask.java +++ b/Aria/src/main/java/com/arialyy/aria/core/upload/UploadTask.java @@ -15,6 +15,7 @@ */ package com.arialyy.aria.core.upload; +import android.content.Context; import android.content.Intent; import android.os.Handler; import android.util.Log; @@ -26,7 +27,6 @@ import com.arialyy.aria.core.inf.ITask; import com.arialyy.aria.core.scheduler.DownloadSchedulers; import com.arialyy.aria.core.scheduler.ISchedulers; import com.arialyy.aria.util.CommonUtil; -import com.arialyy.aria.util.Configuration_1; import java.lang.ref.WeakReference; /** @@ -104,21 +104,6 @@ public class UploadTask implements ITask { intent.putExtra(Aria.ENTITY, mUploadEntity); AriaManager.APP.sendBroadcast(intent); } - - //if (mUtil.isRunning()) { - // mUtil.cancel(); - //} else { - // // 如果任务不是下载状态 - // mUtil.cancel(); - // mUploadEntity.deleteData(); - // if (mOutHandler != null) { - // mOutHandler.obtainMessage(DownloadSchedulers.CANCEL, this).sendToTarget(); - // } - // //发送取消下载的广播 - // Intent intent = CommonUtil.createIntent(AriaManager.APP.getPackageName(), Aria.ACTION_CANCEL); - // intent.putExtra(Aria.ENTITY, mUploadEntity); - // AriaManager.APP.sendBroadcast(intent); - //} } public String getTargetName() { @@ -146,6 +131,8 @@ public class UploadTask implements ITask { boolean isFirst = true; UploadEntity entity; Intent sendIntent; + boolean isOpenBroadCast = false; + Context context; UListener(Handler outHandle, UploadTask task) { this.outHandler = new WeakReference<>(outHandle); @@ -153,6 +140,8 @@ public class UploadTask implements ITask { entity = this.task.get().getUploadEntity(); sendIntent = CommonUtil.createIntent(AriaManager.APP.getPackageName(), Aria.ACTION_RUNNING); sendIntent.putExtra(Aria.ENTITY, entity); + context = AriaManager.APP; + isOpenBroadCast = AriaManager.getInstance(context).getUploadConfig().isOpenBreadCast(); } @Override public void onPre() { @@ -238,13 +227,13 @@ public class UploadTask implements ITask { entity.setComplete(action.equals(Aria.ACTION_COMPLETE)); entity.setCurrentProgress(location); entity.update(); - if (!Configuration_1.isOpenBreadCast) return; - Intent intent = CommonUtil.createIntent(AriaManager.APP.getPackageName(), action); + if (!isOpenBroadCast) return; + Intent intent = CommonUtil.createIntent(context.getPackageName(), action); intent.putExtra(Aria.ENTITY, entity); if (location != -1) { intent.putExtra(Aria.CURRENT_LOCATION, location); } - AriaManager.APP.sendBroadcast(intent); + context.sendBroadcast(intent); } } diff --git a/Aria/src/main/java/com/arialyy/aria/util/ReflectionUtil.java b/Aria/src/main/java/com/arialyy/aria/util/ReflectionUtil.java new file mode 100644 index 00000000..9c197881 --- /dev/null +++ b/Aria/src/main/java/com/arialyy/aria/util/ReflectionUtil.java @@ -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 getAllFields(Class clazz) { + List 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; + } +} diff --git a/app/src/main/assets/aria_config.xml b/app/src/main/assets/aria_config.xml index a87a1297..80d8ded9 100644 --- a/app/src/main/assets/aria_config.xml +++ b/app/src/main/assets/aria_config.xml @@ -3,11 +3,15 @@ + + + + - + @@ -15,8 +19,11 @@ + + + - + @@ -31,7 +38,7 @@ - + diff --git a/app/src/main/java/com/arialyy/simple/MainActivity.java b/app/src/main/java/com/arialyy/simple/MainActivity.java index f67abcc4..c9aa634d 100644 --- a/app/src/main/java/com/arialyy/simple/MainActivity.java +++ b/app/src/main/java/com/arialyy/simple/MainActivity.java @@ -22,6 +22,7 @@ import android.support.v7.widget.Toolbar; import android.view.View; import butterknife.Bind; import butterknife.OnClick; +import com.arialyy.aria.core.Aria; import com.arialyy.simple.base.BaseActivity; import com.arialyy.simple.databinding.ActivityMainBinding; import com.arialyy.simple.download.DownloadActivity; diff --git a/app/src/main/java/com/arialyy/simple/download/SingleTaskActivity.java b/app/src/main/java/com/arialyy/simple/download/SingleTaskActivity.java index 42319695..b06e5e91 100644 --- a/app/src/main/java/com/arialyy/simple/download/SingleTaskActivity.java +++ b/app/src/main/java/com/arialyy/simple/download/SingleTaskActivity.java @@ -56,7 +56,7 @@ public class SingleTaskActivity extends BaseActivity { //"http://kotlinlang.org/docs/kotlin-docs.pdf"; //"https://atom-installer.github.com/v1.13.0/AtomSetup.exe?s=1484074138&ext=.exe"; "http://static.gaoshouyou.com/d/22/94/822260b849944492caadd2983f9bb624.apk"; - //"http://static.gaoshouyou.com/d/36/69/2d3699acfa69e9632262442c46516ad8.apk"; + //"http://static.gaoshouyou.com/d/36/69/2d3699acfa69e9632262442c46516ad8.apk"; //不支持断点的链接 //"http://ox.konsung.net:5555/ksdc-web/download/downloadFile/?fileName=ksdc_1.0.2.apk&rRange=0-"; //"http://172.18.104.50:8080/download/_302turn"; @@ -164,7 +164,7 @@ public class SingleTaskActivity extends BaseActivity { setSupportActionBar(toolbar); toolbar.setTitle("单任务下载"); init(); - Aria.get(this).openBroadcast(true); + Aria.get(this).getDownloadConfig().setOpenBreadCast(true); } private void init() { @@ -173,33 +173,33 @@ public class SingleTaskActivity extends BaseActivity { int p = (int) (target.getCurrentProgress() * 100 / target.getFileSize()); mPb.setProgress(p); } - mRg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { - @Override public void onCheckedChanged(RadioGroup group, int checkedId) { - switch (checkedId) { - case 1: - Aria.get(this).setMaxSpeed(Speed.KB_256); - break; - case 2: - Aria.get(this).setMaxSpeed(Speed.KB_512); - break; - case 3: - Aria.get(this).setMaxSpeed(Speed.MB_1); - break; - case 4: - Aria.get(this).setMaxSpeed(Speed.MB_2); - break; - case 5: - Aria.get(this).setMaxSpeed(Speed.MAX); - break; - } - stop(); - new Handler().postDelayed(new Runnable() { - @Override public void run() { - start(); - } - }, 2000); - } - }); + //mRg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { + // @Override public void onCheckedChanged(RadioGroup group, int checkedId) { + // switch (checkedId) { + // case 1: + // Aria.get(this).setMaxSpeed(Speed.KB_256); + // break; + // case 2: + // Aria.get(this).setMaxSpeed(Speed.KB_512); + // break; + // case 3: + // Aria.get(this).setMaxSpeed(Speed.MB_1); + // break; + // case 4: + // Aria.get(this).setMaxSpeed(Speed.MB_2); + // break; + // case 5: + // Aria.get(this).setMaxSpeed(Speed.MAX); + // break; + // } + // stop(); + // new Handler().postDelayed(new Runnable() { + // @Override public void run() { + // start(); + // } + // }, 2000); + // } + //}); } public void onClick(View view) { diff --git a/app/src/main/java/com/arialyy/simple/download/multi_download/DownloadAdapter.java b/app/src/main/java/com/arialyy/simple/download/multi_download/DownloadAdapter.java index 430d3008..646e8e1e 100644 --- a/app/src/main/java/com/arialyy/simple/download/multi_download/DownloadAdapter.java +++ b/app/src/main/java/com/arialyy/simple/download/multi_download/DownloadAdapter.java @@ -147,10 +147,6 @@ final class DownloadAdapter extends AbsRVAdapter { @Override protected void dataCallback(int result, Object data) { super.dataCallback(result, data); if (result == DownloadNumDialog.RESULT_CODE) { - Aria.get(this).setMaxDownloadNum(Integer.parseInt(data + "")); + Aria.get(this).getDownloadConfig().setMaxTaskNum(Integer.parseInt(data + "")); } } From b67ff4ca11454601be13f991185dffb22f608076 Mon Sep 17 00:00:00 2001 From: AriaLyy <511455842@qq.com> Date: Tue, 23 May 2017 10:41:11 +0800 Subject: [PATCH 02/11] =?UTF-8?q?=E9=85=8D=E7=BD=AE=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/arialyy/aria/core/AriaManager.java | 9 ++--- .../com/arialyy/aria/core/ConfigHelper.java | 4 +-- .../com/arialyy/aria/core/Configuration.java | 34 ++++++++++++++++++- .../aria/core/queue/DownloadTaskQueue.java | 2 +- .../arialyy/aria/core/queue/ITaskQueue.java | 6 ++-- .../aria/core/queue/pool/ExecutePool.java | 7 ---- app/src/main/res/values/strings.xml | 2 +- 7 files changed, 45 insertions(+), 19 deletions(-) diff --git a/Aria/src/main/java/com/arialyy/aria/core/AriaManager.java b/Aria/src/main/java/com/arialyy/aria/core/AriaManager.java index 639649eb..c57e6f01 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/AriaManager.java +++ b/Aria/src/main/java/com/arialyy/aria/core/AriaManager.java @@ -26,6 +26,7 @@ import android.os.Build; import android.os.Bundle; import android.support.v4.app.Fragment; import android.text.TextUtils; +import android.util.Log; import android.widget.PopupWindow; import com.arialyy.aria.core.download.DownloadReceiver; import com.arialyy.aria.core.inf.ICmd; @@ -65,8 +66,8 @@ import org.xml.sax.SAXException; DbUtil.init(context.getApplicationContext()); APP = context.getApplicationContext(); regAppLifeCallback(context); - File dFile = new File(Configuration.DOWNLOAD_CONFIG_FILE); - File uFile = new File(Configuration.UPLOAD_CONFIG_FILE); + File dFile = new File(APP.getFilesDir().getPath() + Configuration.DOWNLOAD_CONFIG_FILE); + File uFile = new File(APP.getFilesDir().getPath() +Configuration.UPLOAD_CONFIG_FILE); if (!dFile.exists() || !uFile.exists()) { loadConfig(); } @@ -105,7 +106,7 @@ import org.xml.sax.SAXException; * 如果需要在代码中修改下载配置,请使用以下方法 * * @ //修改最大任务队列数 - * Aria.get(this).getDownloadConfig().setMaxTaskNum(3).save(); + * Aria.get(this).getDownloadConfig().setMaxTaskNum(3).saveAll(); * */ public Configuration.DownloadConfig getDownloadConfig() { @@ -116,7 +117,7 @@ import org.xml.sax.SAXException; * 如果需要在代码中修改下载配置,请使用以下方法 * * @ //修改最大任务队列数 - * Aria.get(this).getUploadConfig().setMaxTaskNum(3).save(); + * Aria.get(this).getUploadConfig().setMaxTaskNum(3).saveAll(); * */ public Configuration.UploadConfig getUploadConfig() { diff --git a/Aria/src/main/java/com/arialyy/aria/core/ConfigHelper.java b/Aria/src/main/java/com/arialyy/aria/core/ConfigHelper.java index db5f37a9..dcb9cb68 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/ConfigHelper.java +++ b/Aria/src/main/java/com/arialyy/aria/core/ConfigHelper.java @@ -215,7 +215,7 @@ public class ConfigHelper extends DefaultHandler { @Override public void endDocument() throws SAXException { super.endDocument(); - mDownloadConfig.save(); - mUploadConfig.save(); + mDownloadConfig.saveAll(); + mUploadConfig.saveAll(); } } diff --git a/Aria/src/main/java/com/arialyy/aria/core/Configuration.java b/Aria/src/main/java/com/arialyy/aria/core/Configuration.java index f7a1aef7..3ce98ac4 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/Configuration.java +++ b/Aria/src/main/java/com/arialyy/aria/core/Configuration.java @@ -15,6 +15,8 @@ */ package com.arialyy.aria.core; +import android.util.Log; +import com.arialyy.aria.core.queue.DownloadTaskQueue; import com.arialyy.aria.util.CommonUtil; import com.arialyy.aria.util.ReflectionUtil; import java.io.File; @@ -35,6 +37,10 @@ class Configuration { * 通用配置 */ public static class BaseConfig { + /** + * 旧任务数 + */ + public static int oldMaxTaskNum = 2; /** * 是否发送任务广播,true,发送 @@ -63,6 +69,7 @@ class Configuration { public BaseConfig setOpenBreadCast(boolean openBreadCast) { isOpenBreadCast = openBreadCast; + saveKey("isOpenBreadCast", openBreadCast + ""); return this; } @@ -71,7 +78,10 @@ class Configuration { } public BaseConfig setMaxTaskNum(int maxTaskNum) { + oldMaxTaskNum = this.maxTaskNum; this.maxTaskNum = maxTaskNum; + saveKey("maxTaskNum", maxTaskNum + ""); + DownloadTaskQueue.getInstance().setDownloadNum(maxTaskNum); return this; } @@ -81,6 +91,7 @@ class Configuration { public BaseConfig setReTryNum(int reTryNum) { this.reTryNum = reTryNum; + saveKey("reTryNum", reTryNum + ""); return this; } @@ -90,6 +101,7 @@ class Configuration { public BaseConfig setReTryInterval(long reTryInterval) { this.reTryInterval = reTryInterval; + saveKey("reTryInterval", reTryInterval + ""); return this; } @@ -99,9 +111,25 @@ class Configuration { public BaseConfig setConnectTimeOut(long connectTimeOut) { this.connectTimeOut = connectTimeOut; + saveKey("connectTimeOut", 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); + } + } + /** * 加载配置 */ @@ -145,7 +173,7 @@ class Configuration { /** * 保存配置 */ - public void save() { + void saveAll() { List fields = ReflectionUtil.getAllFields(getClass()); boolean isDownload = this instanceof DownloadConfig; try { @@ -199,6 +227,7 @@ class Configuration { public DownloadConfig setiOTimeOut(long iOTimeOut) { this.iOTimeOut = iOTimeOut; + saveKey("iOTimeOut", iOTimeOut + ""); return this; } @@ -208,6 +237,7 @@ class Configuration { public DownloadConfig setBuffSize(int buffSize) { this.buffSize = buffSize; + saveKey("buffSize", buffSize + ""); return this; } @@ -217,6 +247,7 @@ class Configuration { public DownloadConfig setCaPath(String caPath) { this.caPath = caPath; + saveKey("caPath", caPath); return this; } @@ -226,6 +257,7 @@ class Configuration { public DownloadConfig setCaName(String caName) { this.caName = caName; + saveKey("caName", caName); return this; } diff --git a/Aria/src/main/java/com/arialyy/aria/core/queue/DownloadTaskQueue.java b/Aria/src/main/java/com/arialyy/aria/core/queue/DownloadTaskQueue.java index 073621ef..210b835e 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/queue/DownloadTaskQueue.java +++ b/Aria/src/main/java/com/arialyy/aria/core/queue/DownloadTaskQueue.java @@ -122,7 +122,7 @@ public class DownloadTaskQueue @Override public void setDownloadNum(int downloadNum) { //原始长度 - int size = AriaManager.getInstance(AriaManager.APP).getUploadConfig().getMaxTaskNum(); + int size = AriaManager.getInstance(AriaManager.APP).getDownloadConfig().oldMaxTaskNum; int diff = downloadNum - size; if (size == downloadNum) { Log.d(TAG, "设置的下载任务数和配置文件的下载任务数一直,跳过"); diff --git a/Aria/src/main/java/com/arialyy/aria/core/queue/ITaskQueue.java b/Aria/src/main/java/com/arialyy/aria/core/queue/ITaskQueue.java index 7f05cbff..8469cb0f 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/queue/ITaskQueue.java +++ b/Aria/src/main/java/com/arialyy/aria/core/queue/ITaskQueue.java @@ -66,11 +66,11 @@ public interface ITaskQueue implements IPool { private ArrayBlockingQueue mExecuteQueue; private Map mExecuteArray; private int mSize; - private boolean isDownload; public ExecutePool(boolean isDownload) { - this.isDownload = isDownload; if (isDownload) { mSize = AriaManager.getInstance(AriaManager.APP).getDownloadConfig().getMaxTaskNum(); } else { @@ -87,11 +85,6 @@ public class ExecutePool implements IPool { } mExecuteQueue = temp; mSize = downloadNum; - if (isDownload) { - AriaManager.getInstance(AriaManager.APP).getDownloadConfig().setMaxTaskNum(downloadNum); - } else { - AriaManager.getInstance(AriaManager.APP).getUploadConfig().setMaxTaskNum(downloadNum); - } } catch (InterruptedException e) { e.printStackTrace(); } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 510df605..c94a4aab 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -25,7 +25,7 @@ - http://g37.gdl.netease.com/onmyoji_netease_1.apk + https://g37.gdl.netease.com/onmyoji_netease_10_1.0.20.apk http://static.gaoshouyou.com/d/eb/f2/dfeba30541f209ab8a50d847fc1661ce.apk http://rs.0.gaoshouyou.com/d/51/46/58514d126c46b8a3f27fc8c7db3b09ec.apk http://rs.0.gaoshouyou.com/d/23/69/07238f952669727878d7a0e180534c8b.apk From aac060b9bb3193ed700385407db02d1ce0348915 Mon Sep 17 00:00:00 2001 From: AriaLyy <511455842@qq.com> Date: Tue, 23 May 2017 18:04:31 +0800 Subject: [PATCH 03/11] =?UTF-8?q?=E9=85=8D=E7=BD=AE=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/arialyy/aria/core/AriaManager.java | 38 ++- .../com/arialyy/aria/core/Configuration.java | 20 +- .../aria/core/download/DownloadEntity.java | 13 + .../core/download/DownloadStateConstance.java | 6 +- .../aria/core/download/DownloadUtil.java | 100 ++++---- .../aria/core/download/SingleThreadTask.java | 105 +++----- .../java/com/arialyy/aria/orm/DBConfig.java | 2 +- .../arialyy/aria/util/Configuration_1.java | 228 ------------------ .../java/com/arialyy/aria/util/FileUtil.java | 173 ++++++++----- README.md | 129 ++++++++-- app/src/main/assets/aria_config.xml | 9 +- 11 files changed, 365 insertions(+), 458 deletions(-) delete mode 100644 Aria/src/main/java/com/arialyy/aria/util/Configuration_1.java diff --git a/Aria/src/main/java/com/arialyy/aria/core/AriaManager.java b/Aria/src/main/java/com/arialyy/aria/core/AriaManager.java index c57e6f01..d9972ac1 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/AriaManager.java +++ b/Aria/src/main/java/com/arialyy/aria/core/AriaManager.java @@ -33,6 +33,7 @@ import com.arialyy.aria.core.inf.ICmd; import com.arialyy.aria.core.inf.IReceiver; import com.arialyy.aria.core.upload.UploadReceiver; import com.arialyy.aria.orm.DbUtil; +import com.arialyy.aria.util.FileUtil; import java.io.File; import java.io.IOException; import java.util.ArrayList; @@ -66,13 +67,7 @@ import org.xml.sax.SAXException; DbUtil.init(context.getApplicationContext()); APP = context.getApplicationContext(); regAppLifeCallback(context); - File dFile = new File(APP.getFilesDir().getPath() + Configuration.DOWNLOAD_CONFIG_FILE); - File uFile = new File(APP.getFilesDir().getPath() +Configuration.UPLOAD_CONFIG_FILE); - if (!dFile.exists() || !uFile.exists()) { - loadConfig(); - } - mDConfig = Configuration.DownloadConfig.getInstance(); - mUConfig = Configuration.UploadConfig.getInstance(); + initConfig(); } public static AriaManager getInstance(Context context) { @@ -98,7 +93,7 @@ import org.xml.sax.SAXException; SAXParser parser = factory.newSAXParser(); parser.parse(APP.getAssets().open("aria_config.xml"), helper); } catch (ParserConfigurationException | IOException | SAXException e) { - e.printStackTrace(); + Log.d(TAG, e.toString()); } } @@ -106,7 +101,7 @@ import org.xml.sax.SAXException; * 如果需要在代码中修改下载配置,请使用以下方法 * * @ //修改最大任务队列数 - * Aria.get(this).getDownloadConfig().setMaxTaskNum(3).saveAll(); + * Aria.get(this).getDownloadConfig().setMaxTaskNum(3); * */ public Configuration.DownloadConfig getDownloadConfig() { @@ -117,7 +112,7 @@ import org.xml.sax.SAXException; * 如果需要在代码中修改下载配置,请使用以下方法 * * @ //修改最大任务队列数 - * Aria.get(this).getUploadConfig().setMaxTaskNum(3).saveAll(); + * Aria.get(this).getUploadConfig().setMaxTaskNum(3); * */ public Configuration.UploadConfig getUploadConfig() { @@ -243,6 +238,29 @@ import org.xml.sax.SAXException; return key; } + /** + * 初始化配置文件 + */ + private void initConfig() { + //File dFile = new File(APP.getFilesDir().getPath() + Configuration.DOWNLOAD_CONFIG_FILE); + //File uFile = new File(APP.getFilesDir().getPath() + Configuration.UPLOAD_CONFIG_FILE); + File xmlFile = new File(APP.getFilesDir().getPath() + Configuration.XML_FILE); + if (!xmlFile.exists()) { + loadConfig(); + } else { + try { + String md5Code = FileUtil.getFileMD5(xmlFile); + if (!FileUtil.checkMD5(md5Code, APP.getAssets().open("aria_config.xml"))) { + loadConfig(); + } + } catch (IOException e) { + e.printStackTrace(); + } + } + mDConfig = Configuration.DownloadConfig.getInstance(); + mUConfig = Configuration.UploadConfig.getInstance(); + } + /** * 注册APP生命周期回调 */ diff --git a/Aria/src/main/java/com/arialyy/aria/core/Configuration.java b/Aria/src/main/java/com/arialyy/aria/core/Configuration.java index 3ce98ac4..260281ac 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/Configuration.java +++ b/Aria/src/main/java/com/arialyy/aria/core/Configuration.java @@ -15,7 +15,6 @@ */ package com.arialyy.aria.core; -import android.util.Log; import com.arialyy.aria.core.queue.DownloadTaskQueue; import com.arialyy.aria.util.CommonUtil; import com.arialyy.aria.util.ReflectionUtil; @@ -32,6 +31,7 @@ import java.util.Properties; class Configuration { static final String DOWNLOAD_CONFIG_FILE = "/Aria/DownloadConfig.properties"; static final String UPLOAD_CONFIG_FILE = "/Aria/UploadConfig.properties"; + static final String XML_FILE = "/Aria/aria_config.xml"; /** * 通用配置 @@ -57,11 +57,11 @@ class Configuration { /** * 设置重试间隔,单位为毫秒,默认2000毫秒 */ - long reTryInterval = 2000; + int reTryInterval = 2000; /** * 设置url连接超时时间,单位为毫秒,默认5000毫秒 */ - long connectTimeOut = 5000; + int connectTimeOut = 5000; public boolean isOpenBreadCast() { return isOpenBreadCast; @@ -95,21 +95,21 @@ class Configuration { return this; } - public long getReTryInterval() { + public int getReTryInterval() { return reTryInterval; } - public BaseConfig setReTryInterval(long reTryInterval) { + public BaseConfig setReTryInterval(int reTryInterval) { this.reTryInterval = reTryInterval; saveKey("reTryInterval", reTryInterval + ""); return this; } - public long getConnectTimeOut() { + public int getConnectTimeOut() { return connectTimeOut; } - public BaseConfig setConnectTimeOut(long connectTimeOut) { + public BaseConfig setConnectTimeOut(int connectTimeOut) { this.connectTimeOut = connectTimeOut; saveKey("connectTimeOut", connectTimeOut + ""); return this; @@ -203,7 +203,7 @@ class Configuration { /** * 设置IO流读取时间,单位为毫秒,默认20000毫秒,该时间不能少于10000毫秒 */ - long iOTimeOut = 20 * 1000; + int iOTimeOut = 20 * 1000; /** * 设置写文件buff大小,该数值大小不能小于2048,数值变小,下载速度会变慢 */ @@ -221,11 +221,11 @@ class Configuration { */ int threadNum = 3; - public long getiOTimeOut() { + public int getIOTimeOut() { return iOTimeOut; } - public DownloadConfig setiOTimeOut(long iOTimeOut) { + public DownloadConfig setIOTimeOut(int iOTimeOut) { this.iOTimeOut = iOTimeOut; saveKey("iOTimeOut", iOTimeOut + ""); return this; diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadEntity.java b/Aria/src/main/java/com/arialyy/aria/core/download/DownloadEntity.java index aa46891d..f2ddb057 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadEntity.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/DownloadEntity.java @@ -43,10 +43,19 @@ public class DownloadEntity extends DbEntity implements Parcelable, IEntity { private long completeTime; //完成时间 private boolean isRedirect = false; private String redirectUrl = ""; //重定向链接 + private int threadNum; //下载线程数 public DownloadEntity() { } + public int getThreadNum() { + return threadNum; + } + + public void setThreadNum(int threadNum) { + this.threadNum = threadNum; + } + public String getStr() { return str; } @@ -191,6 +200,8 @@ public class DownloadEntity extends DbEntity implements Parcelable, IEntity { + ", redirectUrl='" + redirectUrl + '\'' + + ", threadNum=" + + threadNum + '}'; } @@ -212,6 +223,7 @@ public class DownloadEntity extends DbEntity implements Parcelable, IEntity { dest.writeLong(this.completeTime); dest.writeByte(this.isRedirect ? (byte) 1 : (byte) 0); dest.writeString(this.redirectUrl); + dest.writeInt(this.threadNum); } protected DownloadEntity(Parcel in) { @@ -228,6 +240,7 @@ public class DownloadEntity extends DbEntity implements Parcelable, IEntity { this.completeTime = in.readLong(); this.isRedirect = in.readByte() != 0; this.redirectUrl = in.readString(); + this.threadNum = in.readInt(); } @Ignore public static final Creator CREATOR = new Creator() { diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadStateConstance.java b/Aria/src/main/java/com/arialyy/aria/core/download/DownloadStateConstance.java index 934ff001..8c815ee9 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadStateConstance.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/DownloadStateConstance.java @@ -23,10 +23,10 @@ final class DownloadStateConstance { int CANCEL_NUM = 0; int STOP_NUM = 0; int FAIL_NUM = 0; - int CONNECT_TIME_OUT = 5000 * 4; //连接超时时间 - int READ_TIME_OUT = 1000 * 20; //流读取的超时时间 + int CONNECT_TIME_OUT; //连接超时时间 + int READ_TIME_OUT; //流读取的超时时间 int COMPLETE_THREAD_NUM = 0; - int THREAD_NUM = 3; + int THREAD_NUM; long CURRENT_LOCATION = 0; boolean isDownloading = false; boolean isCancel = false; diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadUtil.java b/Aria/src/main/java/com/arialyy/aria/core/download/DownloadUtil.java index 4d87e055..f6bd01a3 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadUtil.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/DownloadUtil.java @@ -19,6 +19,7 @@ package com.arialyy.aria.core.download; import android.content.Context; import android.util.Log; import android.util.SparseArray; +import com.arialyy.aria.core.AriaManager; import com.arialyy.aria.util.BufferedRandomAccessFile; import com.arialyy.aria.util.CheckUtil; import com.arialyy.aria.util.CommonUtil; @@ -34,7 +35,7 @@ import java.util.concurrent.Executors; * Created by lyy on 2015/8/25. * 下载工具类 */ -public class DownloadUtil implements IDownloadUtil, Runnable { +class DownloadUtil implements IDownloadUtil, Runnable { private static final String TAG = "DownloadUtil"; /** * 线程数 @@ -46,8 +47,7 @@ public class DownloadUtil implements IDownloadUtil, Runnable { private static final long SUB_LEN = 1024 * 1024; //下载监听 private IDownloadListener mListener; - private int mConnectTimeOut = 5000 * 4; //连接超时时间 - private int mReadTimeOut = 5000 * 20; //流读取的超时时间 + private int mConnectTimeOut = 0; //连接超时时间 private boolean isNewTask = true; private boolean isSupportBreakpoint = true; private Context mContext; @@ -57,34 +57,29 @@ public class DownloadUtil implements IDownloadUtil, Runnable { private File mDownloadFile; //下载的文件 private File mConfigFile;//下载信息配置文件 private SparseArray mTask = new SparseArray<>(); - private DownloadStateConstance mConstance; + private DownloadStateConstance CONSTANCE; DownloadUtil(Context context, DownloadTaskEntity entity, IDownloadListener downloadListener) { - this(context, entity, downloadListener, 3); - } - - DownloadUtil(Context context, DownloadTaskEntity entity, IDownloadListener downloadListener, - int threadNum) { - //CheckUtil.checkDownloadTaskEntity(entity.downloadEntity); CheckUtil.checkTaskEntity(entity); mDownloadEntity = entity.downloadEntity; mContext = context.getApplicationContext(); mDownloadTaskEntity = entity; mListener = downloadListener; - THREAD_NUM = threadNum; + // 线程下载数改变后,新的下载才会生效 mFixedThreadPool = Executors.newFixedThreadPool(Integer.MAX_VALUE); - mConstance = new DownloadStateConstance(); + CONSTANCE = new DownloadStateConstance(); init(); } private void init() { + mConnectTimeOut = AriaManager.getInstance(mContext).getDownloadConfig().getConnectTimeOut(); mDownloadFile = new File(mDownloadTaskEntity.downloadEntity.getDownloadPath()); //读取已完成的线程数 mConfigFile = new File( mContext.getFilesDir().getPath() + "/temp/" + mDownloadFile.getName() + ".properties"); try { if (!mConfigFile.exists()) { //记录文件被删除,则重新下载 - isNewTask = true; + handleNewTask(); CommonUtil.createFile(mConfigFile.getPath()); } else { isNewTask = !mDownloadFile.exists(); @@ -99,37 +94,23 @@ public class DownloadUtil implements IDownloadUtil, Runnable { return mListener; } - /** - * 设置连接超时时间 - */ - public void setConnectTimeOut(int timeOut) { - mConnectTimeOut = timeOut; - } - - /** - * 设置流读取的超时时间 - */ - public void setReadTimeOut(int readTimeOut) { - mReadTimeOut = readTimeOut; - } - /** * 获取当前下载位置 */ @Override public long getCurrentLocation() { - return mConstance.CURRENT_LOCATION; + return CONSTANCE.CURRENT_LOCATION; } @Override public boolean isDownloading() { - return mConstance.isDownloading; + return CONSTANCE.isDownloading; } /** * 取消下载 */ @Override public void cancelDownload() { - mConstance.isCancel = true; - mConstance.isDownloading = false; + CONSTANCE.isCancel = true; + CONSTANCE.isDownloading = false; mFixedThreadPool.shutdown(); for (int i = 0; i < THREAD_NUM; i++) { SingleThreadTask task = (SingleThreadTask) mTask.get(i); @@ -143,8 +124,8 @@ public class DownloadUtil implements IDownloadUtil, Runnable { * 停止下载 */ @Override public void stopDownload() { - mConstance.isStop = true; - mConstance.isDownloading = false; + CONSTANCE.isStop = true; + CONSTANCE.isDownloading = false; mFixedThreadPool.shutdown(); for (int i = 0; i < THREAD_NUM; i++) { SingleThreadTask task = (SingleThreadTask) mTask.get(i); @@ -184,7 +165,7 @@ public class DownloadUtil implements IDownloadUtil, Runnable { * 多线程断点续传下载文件,开始下载 */ @Override public void startDownload() { - mConstance.cleanState(); + CONSTANCE.cleanState(); mListener.onPre(); new Thread(this).start(); } @@ -195,7 +176,7 @@ public class DownloadUtil implements IDownloadUtil, Runnable { private void failDownload(String msg) { Log.e(TAG, msg); - mConstance.isDownloading = false; + CONSTANCE.isDownloading = false; stopDownload(); mListener.onFail(); } @@ -206,7 +187,7 @@ public class DownloadUtil implements IDownloadUtil, Runnable { HttpURLConnection conn = ConnectionHelp.handleConnection(url); conn = ConnectionHelp.setConnectParam(mDownloadTaskEntity, conn); conn.setRequestProperty("Range", "bytes=" + 0 + "-"); - conn.setConnectTimeout(mConnectTimeOut * 4); + conn.setConnectTimeout(mConnectTimeOut); conn.connect(); handleConnect(conn); } catch (IOException e) { @@ -283,7 +264,7 @@ public class DownloadUtil implements IDownloadUtil, Runnable { conn = ConnectionHelp.setConnectParam(mDownloadTaskEntity, conn); conn.setRequestProperty("Cookie", cookies); conn.setRequestProperty("Range", "bytes=" + 0 + "-"); - conn.setConnectTimeout(mConnectTimeOut * 4); + conn.setConnectTimeout(mConnectTimeOut); conn.connect(); handleConnect(conn); @@ -301,7 +282,7 @@ public class DownloadUtil implements IDownloadUtil, Runnable { int fileLength = conn.getContentLength(); if (fileLength < SUB_LEN) { THREAD_NUM = 1; - mConstance.THREAD_NUM = THREAD_NUM; + CONSTANCE.THREAD_NUM = THREAD_NUM; } Properties pro = createConfigFile(fileLength); int blockSize = fileLength / THREAD_NUM; @@ -322,14 +303,14 @@ public class DownloadUtil implements IDownloadUtil, Runnable { //如果有记录,则恢复下载 if (!isNewTask && record != null && Long.parseLong(record + "") > 0) { Long r = Long.parseLong(record + ""); - mConstance.CURRENT_LOCATION += r - startL; + CONSTANCE.CURRENT_LOCATION += r - startL; Log.d(TAG, "++++++++++ 线程_" + i + "_恢复下载 ++++++++++"); mListener.onChildResume(r); startL = r; recordL[rl] = i; rl++; } else { - isNewTask = true; + handleNewTask(); } if (isNewTask) { recordL[rl] = i; @@ -351,7 +332,6 @@ public class DownloadUtil implements IDownloadUtil, Runnable { ConfigEntity entity = new ConfigEntity(); long len = conn.getContentLength(); entity.FILE_SIZE = len; - //entity.DOWNLOAD_URL = mDownloadEntity.getDownloadUrl(); entity.DOWNLOAD_URL = mDownloadEntity.isRedirect() ? mDownloadEntity.getRedirectUrl() : mDownloadEntity.getDownloadUrl(); entity.TEMP_FILE = mDownloadFile; @@ -362,8 +342,8 @@ public class DownloadUtil implements IDownloadUtil, Runnable { entity.isSupportBreakpoint = isSupportBreakpoint; entity.DOWNLOAD_TASK_ENTITY = mDownloadTaskEntity; THREAD_NUM = 1; - mConstance.THREAD_NUM = THREAD_NUM; - SingleThreadTask task = new SingleThreadTask(mConstance, mListener, entity); + CONSTANCE.THREAD_NUM = THREAD_NUM; + SingleThreadTask task = new SingleThreadTask(CONSTANCE, mListener, entity); mTask.put(0, task); mFixedThreadPool.execute(task); mListener.onPostPre(len); @@ -385,15 +365,16 @@ public class DownloadUtil implements IDownloadUtil, Runnable { //分配每条线程的下载区间 pro = CommonUtil.loadConfig(mConfigFile); if (pro.isEmpty()) { - isNewTask = true; + handleNewTask(); } else { + THREAD_NUM = pro.keySet().size(); for (int i = 0; i < THREAD_NUM; i++) { if (pro.getProperty(mDownloadFile.getName() + "_record_" + i) == null) { Object state = pro.getProperty(mDownloadFile.getName() + "_state_" + i); if (state != null && Integer.parseInt(state + "") == 1) { continue; } - isNewTask = true; + handleNewTask(); break; } } @@ -401,23 +382,31 @@ public class DownloadUtil implements IDownloadUtil, Runnable { return pro; } + /** + * 处理新任务 + */ + private void handleNewTask() { + isNewTask = true; + THREAD_NUM = AriaManager.getInstance(mContext).getDownloadConfig().getThreadNum(); + } + /** * 恢复记录地址 * * @return true 表示下载完成 */ private boolean resumeRecordLocation(int i, long startL, long endL) { - mConstance.CURRENT_LOCATION += endL - startL; + CONSTANCE.CURRENT_LOCATION += endL - startL; Log.d(TAG, "++++++++++ 线程_" + i + "_已经下载完成 ++++++++++"); - mConstance.COMPLETE_THREAD_NUM++; - mConstance.STOP_NUM++; - mConstance.CANCEL_NUM++; - if (mConstance.isComplete()) { + CONSTANCE.COMPLETE_THREAD_NUM++; + CONSTANCE.STOP_NUM++; + CONSTANCE.CANCEL_NUM++; + if (CONSTANCE.isComplete()) { if (mConfigFile.exists()) { mConfigFile.delete(); } mListener.onComplete(); - mConstance.isDownloading = false; + CONSTANCE.isDownloading = false; return true; } return false; @@ -439,7 +428,8 @@ public class DownloadUtil implements IDownloadUtil, Runnable { entity.CONFIG_FILE_PATH = mConfigFile.getPath(); entity.isSupportBreakpoint = isSupportBreakpoint; entity.DOWNLOAD_TASK_ENTITY = mDownloadTaskEntity; - SingleThreadTask task = new SingleThreadTask(mConstance, mListener, entity); + CONSTANCE.THREAD_NUM = THREAD_NUM; + SingleThreadTask task = new SingleThreadTask(CONSTANCE, mListener, entity); mTask.put(i, task); } @@ -447,10 +437,10 @@ public class DownloadUtil implements IDownloadUtil, Runnable { * 启动单线程下载任务 */ private void startSingleTask(int[] recordL) { - if (mConstance.CURRENT_LOCATION > 0) { - mListener.onResume(mConstance.CURRENT_LOCATION); + if (CONSTANCE.CURRENT_LOCATION > 0) { + mListener.onResume(CONSTANCE.CURRENT_LOCATION); } else { - mListener.onStart(mConstance.CURRENT_LOCATION); + mListener.onStart(CONSTANCE.CURRENT_LOCATION); } for (int l : recordL) { if (l == -1) continue; diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/SingleThreadTask.java b/Aria/src/main/java/com/arialyy/aria/core/download/SingleThreadTask.java index bedf86c1..1ca9fe1b 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/SingleThreadTask.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/SingleThreadTask.java @@ -15,10 +15,8 @@ */ package com.arialyy.aria.core.download; -import android.os.Handler; -import android.os.Looper; -import android.os.Message; import android.util.Log; +import com.arialyy.aria.core.AriaManager; import com.arialyy.aria.util.BufferedRandomAccessFile; import com.arialyy.aria.util.CommonUtil; import java.io.File; @@ -34,35 +32,34 @@ import java.util.Properties; * 下载线程 */ final class SingleThreadTask implements Runnable { - private static final String TAG = "SingleThreadTask"; - // TODO: 2017/2/22 不能使用1024 否则最大速度不能超过3m - private static final int BUF_SIZE = 8192; + private static final String TAG = "SingleThreadTask"; private DownloadUtil.ConfigEntity mConfigEntity; private String mConfigFPath; private long mChildCurrentLocation = 0; private static final Object LOCK = new Object(); - private int mBufSize = 8192; - //private int mBufSize = 64; + private int mBufSize; private IDownloadListener mListener; - private DownloadStateConstance mConstance; + private DownloadStateConstance CONSTANCE; SingleThreadTask(DownloadStateConstance constance, IDownloadListener listener, DownloadUtil.ConfigEntity downloadInfo) { - mConstance = constance; + AriaManager manager = AriaManager.getInstance(AriaManager.APP); + CONSTANCE = constance; + CONSTANCE.CONNECT_TIME_OUT = manager.getDownloadConfig().getConnectTimeOut(); + CONSTANCE.READ_TIME_OUT = manager.getDownloadConfig().getIOTimeOut(); mListener = listener; this.mConfigEntity = downloadInfo; if (mConfigEntity.isSupportBreakpoint) { mConfigFPath = downloadInfo.CONFIG_FILE_PATH; } - //mBufSize = Configuration_1.getInstance().getMaxSpeed(); + mBufSize = manager.getDownloadConfig().getBuffSize(); } @Override public void run() { - HttpURLConnection conn = null; - InputStream is = null; + HttpURLConnection conn; + InputStream is; try { URL url = new URL(mConfigEntity.DOWNLOAD_URL); - //conn = (HttpURLConnection) url.openConnection(); conn = ConnectionHelp.handleConnection(url); if (mConfigEntity.isSupportBreakpoint) { Log.d(TAG, "线程_" @@ -79,8 +76,8 @@ final class SingleThreadTask implements Runnable { Log.w(TAG, "该下载不支持断点"); } conn = ConnectionHelp.setConnectParam(mConfigEntity.DOWNLOAD_TASK_ENTITY, conn); - conn.setConnectTimeout(mConstance.CONNECT_TIME_OUT); - conn.setReadTimeout(mConstance.READ_TIME_OUT); //设置读取流的等待时间,必须设置该参数 + conn.setConnectTimeout(CONSTANCE.CONNECT_TIME_OUT); + conn.setReadTimeout(CONSTANCE.READ_TIME_OUT); //设置读取流的等待时间,必须设置该参数 is = conn.getInputStream(); //创建可设置位置的文件 BufferedRandomAccessFile file = @@ -92,10 +89,10 @@ final class SingleThreadTask implements Runnable { //当前子线程的下载位置 mChildCurrentLocation = mConfigEntity.START_LOCATION; while ((len = is.read(buffer)) != -1) { - if (mConstance.isCancel) { + if (CONSTANCE.isCancel) { break; } - if (mConstance.isStop) { + if (CONSTANCE.isStop) { break; } //把下载数据数据写入文件 @@ -106,11 +103,11 @@ final class SingleThreadTask implements Runnable { //close 为阻塞的,需要使用线程池来处理 is.close(); conn.disconnect(); - if (mConstance.isCancel) { + if (CONSTANCE.isCancel) { return; } //停止状态不需要删除记录文件 - if (mConstance.isStop) { + if (CONSTANCE.isStop) { return; } //支持断点的处理 @@ -119,29 +116,29 @@ final class SingleThreadTask implements Runnable { writeConfig(mConfigEntity.TEMP_FILE.getName() + "_state_" + mConfigEntity.THREAD_ID, 1 + ""); mListener.onChildComplete(mConfigEntity.END_LOCATION); - mConstance.COMPLETE_THREAD_NUM++; - if (mConstance.isComplete()) { + CONSTANCE.COMPLETE_THREAD_NUM++; + if (CONSTANCE.isComplete()) { File configFile = new File(mConfigFPath); if (configFile.exists()) { configFile.delete(); } - mConstance.isDownloading = false; + CONSTANCE.isDownloading = false; mListener.onComplete(); } } else { Log.i(TAG, "下载任务完成"); - mConstance.isDownloading = false; + CONSTANCE.isDownloading = false; mListener.onComplete(); } } catch (MalformedURLException e) { - mConstance.FAIL_NUM++; + CONSTANCE.FAIL_NUM++; failDownload(mConfigEntity, mChildCurrentLocation, "下载链接异常", e); } catch (IOException e) { - mConstance.FAIL_NUM++; + CONSTANCE.FAIL_NUM++; failDownload(mConfigEntity, mChildCurrentLocation, "下载失败【" + mConfigEntity.DOWNLOAD_URL + "】", e); } catch (Exception e) { - mConstance.FAIL_NUM++; + CONSTANCE.FAIL_NUM++; failDownload(mConfigEntity, mChildCurrentLocation, "获取流失败", e); } } @@ -153,7 +150,7 @@ final class SingleThreadTask implements Runnable { synchronized (LOCK) { try { if (mConfigEntity.isSupportBreakpoint) { - mConstance.STOP_NUM++; + CONSTANCE.STOP_NUM++; String location = String.valueOf(mChildCurrentLocation); Log.d(TAG, "thread_" + mConfigEntity.THREAD_ID @@ -161,15 +158,15 @@ final class SingleThreadTask implements Runnable { + mChildCurrentLocation); writeConfig(mConfigEntity.TEMP_FILE.getName() + "_record_" + mConfigEntity.THREAD_ID, location); - if (mConstance.isStop()) { + if (CONSTANCE.isStop()) { Log.d(TAG, "++++++++++++++++ onStop +++++++++++++++++"); - mConstance.isDownloading = false; - mListener.onStop(mConstance.CURRENT_LOCATION); + CONSTANCE.isDownloading = false; + mListener.onStop(CONSTANCE.CURRENT_LOCATION); } } else { Log.d(TAG, "++++++++++++++++ onStop +++++++++++++++++"); - mConstance.isDownloading = false; - mListener.onStop(mConstance.CURRENT_LOCATION); + CONSTANCE.isDownloading = false; + mListener.onStop(CONSTANCE.CURRENT_LOCATION); } } catch (IOException e) { e.printStackTrace(); @@ -183,44 +180,20 @@ final class SingleThreadTask implements Runnable { private void progress(long len) { synchronized (LOCK) { mChildCurrentLocation += len; - mConstance.CURRENT_LOCATION += len; - mListener.onProgress(mConstance.CURRENT_LOCATION); - //mHandler.sendEmptyMessage(1); - //mHandler.post(t); + CONSTANCE.CURRENT_LOCATION += len; + mListener.onProgress(CONSTANCE.CURRENT_LOCATION); } } - Handler mHandler = new Handler(Looper.getMainLooper()) { - @Override public void handleMessage(Message msg) { - super.handleMessage(msg); - mListener.onProgress(mConstance.CURRENT_LOCATION); - } - }; - - Thread t = new Thread(new Runnable() { - @Override public void run() { - mListener.onProgress(mConstance.CURRENT_LOCATION); - } - }); - - //Handler handler = new Handler(){ - // @Override public void handleMessage(Message msg) { - // super.handleMessage(msg); - // mListener.onProgress(mConstance.CURRENT_LOCATION); - // } - //}; - - Thread thread = new Thread(); - /** * 取消下载 */ protected void cancel() { synchronized (LOCK) { if (mConfigEntity.isSupportBreakpoint) { - mConstance.CANCEL_NUM++; + CONSTANCE.CANCEL_NUM++; Log.d(TAG, "++++++++++ thread_" + mConfigEntity.THREAD_ID + "_cancel ++++++++++"); - if (mConstance.isCancel()) { + if (CONSTANCE.isCancel()) { File configFile = new File(mConfigFPath); if (configFile.exists()) { configFile.delete(); @@ -229,12 +202,12 @@ final class SingleThreadTask implements Runnable { mConfigEntity.TEMP_FILE.delete(); } Log.d(TAG, "++++++++++++++++ onCancel +++++++++++++++++"); - mConstance.isDownloading = false; + CONSTANCE.isDownloading = false; mListener.onCancel(); } } else { Log.d(TAG, "++++++++++++++++ onCancel +++++++++++++++++"); - mConstance.isDownloading = false; + CONSTANCE.isDownloading = false; mListener.onCancel(); } } @@ -247,8 +220,8 @@ final class SingleThreadTask implements Runnable { Exception ex) { synchronized (LOCK) { try { - mConstance.isDownloading = false; - mConstance.isStop = true; + CONSTANCE.isDownloading = false; + CONSTANCE.isStop = true; if (ex != null) { Log.e(TAG, CommonUtil.getPrintException(ex)); } @@ -257,7 +230,7 @@ final class SingleThreadTask implements Runnable { String location = String.valueOf(currentLocation); writeConfig(dEntity.TEMP_FILE.getName() + "_record_" + dEntity.THREAD_ID, location); } - if (mConstance.isFail()) { + if (CONSTANCE.isFail()) { Log.d(TAG, "++++++++++++++++ onFail +++++++++++++++++"); mListener.onFail(); } diff --git a/Aria/src/main/java/com/arialyy/aria/orm/DBConfig.java b/Aria/src/main/java/com/arialyy/aria/orm/DBConfig.java index 8f2ed69c..d380c879 100644 --- a/Aria/src/main/java/com/arialyy/aria/orm/DBConfig.java +++ b/Aria/src/main/java/com/arialyy/aria/orm/DBConfig.java @@ -28,7 +28,7 @@ import java.util.Map; public class DBConfig { static Map mapping = new HashMap<>(); static String DB_NAME; - static int VERSION = 2; + static int VERSION = 3; static { if (TextUtils.isEmpty(DB_NAME)) { diff --git a/Aria/src/main/java/com/arialyy/aria/util/Configuration_1.java b/Aria/src/main/java/com/arialyy/aria/util/Configuration_1.java deleted file mode 100644 index d89ffed4..00000000 --- a/Aria/src/main/java/com/arialyy/aria/util/Configuration_1.java +++ /dev/null @@ -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 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 config) { - if (config == null || config.size() == 0) { - return; - } - Properties properties = CommonUtil.loadConfig(mConfigFile); - Set keys = config.keySet(); - for (String key : keys) { - properties.setProperty(key, config.get(key)); - } - CommonUtil.saveConfig(mConfigFile, properties); - } - - private void save(String key, String value) { - Map map = new WeakHashMap<>(); - map.put(key, value); - saveConfig(map); - } - - /** - * 设置最大下载速度 - */ - 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 + ""); - } -} diff --git a/Aria/src/main/java/com/arialyy/aria/util/FileUtil.java b/Aria/src/main/java/com/arialyy/aria/util/FileUtil.java index 6fb9bf09..f6d4df5b 100644 --- a/Aria/src/main/java/com/arialyy/aria/util/FileUtil.java +++ b/Aria/src/main/java/com/arialyy/aria/util/FileUtil.java @@ -15,105 +15,144 @@ */ package com.arialyy.aria.util; -import android.content.Context; -import android.content.pm.ApplicationInfo; -import android.content.pm.PackageInfo; -import android.content.pm.PackageManager; -import android.graphics.drawable.Drawable; -import android.graphics.drawable.Icon; +import android.text.TextUtils; +import android.util.Log; import com.arialyy.aria.window.FileEntity; import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.math.BigInteger; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; import java.util.ArrayList; import java.util.List; /** * Created by Aria.Lao on 2017/3/21. */ - public class FileUtil { - Context mContext; + private static final String TAG = "FileUtil"; - public FileUtil(Context context) { - mContext = context; + /** + * 通过流创建文件 + */ + public static void createFileFormInputStream(InputStream is, String path) { + try { + FileOutputStream fos = new FileOutputStream(path); + byte[] buf = new byte[1376]; + while (is.read(buf) > 0) { + fos.write(buf, 0, buf.length); + } + is.close(); + fos.flush(); + fos.close(); + } catch (IOException e) { + e.printStackTrace(); + } } /** - * 文件列表 + * 校验文件MD5码 */ - public List loadFiles(String path) { - File file = new File(path); - File[] files = file.listFiles(); - List list = new ArrayList<>(); - for (File f : files) { - FileEntity entity = new FileEntity(); - entity.fileName = f.getName(); - //entity.fileInfo = getFileType(f.getPath()); - //entity.fileDrawable = getApkIcon(mContext, f.getPath()); - list.add(entity); + public static boolean checkMD5(String md5, File updateFile) { + if (TextUtils.isEmpty(md5) || updateFile == null) { + Log.e(TAG, "MD5 string empty or updateFile null"); + return false; } - return list; + + String calculatedDigest = getFileMD5(updateFile); + if (calculatedDigest == null) { + Log.e(TAG, "calculatedDigest null"); + return false; + } + return calculatedDigest.equalsIgnoreCase(md5); } /** - * 获取文件类型 + * 校验文件MD5码 */ - public FileType getFileType(String path) { - String exName = getExName(path); - String type = ""; - FileType fType = null; - if (exName.equalsIgnoreCase("apk")) { - fType = new FileType("应用", getApkIcon(path)); - } else if (exName.equalsIgnoreCase("img") - || exName.equalsIgnoreCase("png") - || exName.equalsIgnoreCase("jpg") - || exName.equalsIgnoreCase("jepg")) { - //fType = new FileType("图片", ) - } else if (exName.equalsIgnoreCase("mp3") || exName.equalsIgnoreCase("wm")) { - //fType = new FileType("音乐", ); - } else if (exName.equalsIgnoreCase("mp4") - || exName.equalsIgnoreCase("rm") - || exName.equalsIgnoreCase("rmvb")) { - //fType = new FileType("视频", ); + public static boolean checkMD5(String md5, InputStream is) { + if (TextUtils.isEmpty(md5) || is == null) { + Log.e(TAG, "MD5 string empty or updateFile null"); + return false; + } + + String calculatedDigest = getFileMD5(is); + if (calculatedDigest == null) { + Log.e(TAG, "calculatedDigest null"); + return false; } - return fType; + return calculatedDigest.equalsIgnoreCase(md5); } /** - * 获取扩展名 + * 获取文件MD5码 */ - public String getExName(String path) { - int separatorIndex = path.lastIndexOf("."); - return (separatorIndex < 0) ? path : path.substring(separatorIndex + 1, path.length()); + public static String getFileMD5(File updateFile) { + InputStream is; + try { + is = new FileInputStream(updateFile); + } catch (FileNotFoundException e) { + Log.e(TAG, "Exception while getting FileInputStream", e); + return null; + } + + return getFileMD5(is); } /** - * 获取apk文件的icon - * - * @param path apk文件路径 + * 获取文件MD5码 */ - public Drawable getApkIcon(String path) { - PackageManager pm = mContext.getPackageManager(); - PackageInfo info = pm.getPackageArchiveInfo(path, PackageManager.GET_ACTIVITIES); - if (info != null) { - ApplicationInfo appInfo = info.applicationInfo; - //android有bug,需要下面这两句话来修复才能获取apk图片 - appInfo.sourceDir = path; - appInfo.publicSourceDir = path; - // String packageName = appInfo.packageName; //得到安装包名称 - // String version=info.versionName; //得到版本信息 - return pm.getApplicationIcon(appInfo); + public static String getFileMD5(InputStream is) { + MessageDigest digest; + try { + digest = MessageDigest.getInstance("MD5"); + } catch (NoSuchAlgorithmException e) { + Log.e(TAG, "Exception while getting digest", e); + return null; } - return null; - } - class FileType { - String name; - Drawable icon; + byte[] buffer = new byte[8192]; + int read; + try { + while ((read = is.read(buffer)) > 0) { + digest.update(buffer, 0, read); + } + byte[] md5sum = digest.digest(); + BigInteger bigInt = new BigInteger(1, md5sum); + String output = bigInt.toString(16); + // Fill to 32 chars + output = String.format("%32s", output).replace(' ', '0'); + return output; + } catch (IOException e) { + throw new RuntimeException("Unable to process file for MD5", e); + } finally { + try { + is.close(); + } catch (IOException e) { + Log.e(TAG, "Exception on closing MD5 input stream", e); + } + } + } - public FileType(String name, Drawable icon) { - this.name = name; - this.icon = icon; + /** + * 文件列表 + */ + public List loadFiles(String path) { + File file = new File(path); + File[] files = file.listFiles(); + List list = new ArrayList<>(); + for (File f : files) { + FileEntity entity = new FileEntity(); + entity.fileName = f.getName(); + //entity.fileInfo = getFileType(f.getPath()); + //entity.fileDrawable = getApkIcon(mContext, f.getPath()); + list.add(entity); } + return list; } } diff --git a/README.md b/README.md index 3b2b43a2..877197db 100644 --- a/README.md +++ b/README.md @@ -1,15 +1,20 @@ # Aria ![图标](https://github.com/AriaLyy/DownloadUtil/blob/v_2.0/app/src/main/res/mipmap-hdpi/ic_launcher.png)
-Aria,让上传、下载更容易实现
+Aria项目源于15年工作中遇到的一个文件下载管理的需求,当时被下载折磨的痛不欲生,从那时起便萌生了编写一个简单易用,稳当高效的下载框架,aria经历了1.0到3.0的开发,算是越来越接近当初所制定的目标了。 Aria有以下特点: - - 简单 - - 可在Dialog、popupWindow等组件中使用 - - 支持多线程、多任务下载 - - 支持多任务自动调度 - - 可以直接获取速度 - - 支持https地址下载 - - 支持上传操作 + + 简单、方便 + - 可以在Activity、Service、Fragment、Dialog、popupWindow、Notification等组件中使用 + - 支持任务自动调度,使用者不需要关心任务状态切换的逻辑 + - [通过Aria的事件,能很容易获取当前下载任务的下载状态](#二、下载状态获取) + - [一句代码就可以动态设置最大下载数](#通过代码修改Aria参数) + - [一句代码加可以获取当前的下载速度](#其它好用的API) + - [通过修改配置文件很容易就能修改下载线程数](#通过文件修改Aria配置参数) + + 支持https地址下载 + - 在配置文件中很容易就可以设置CA证书的信息 + + 支持300、301、302重定向下载链接下载 + + 支持上传操作 + Aria怎样使用? * [下载](#使用) @@ -20,7 +25,7 @@ Aria怎样使用? ## 下载 [![Download](https://api.bintray.com/packages/arialyy/maven/Aria/images/download.svg)](https://bintray.com/arialyy/maven/Aria/_latestVersion)
```java -compile 'com.arialyy.aria:Aria:3.0.3' +compile 'com.arialyy.aria:Aria:3.1.0' ``` ## 示例 @@ -72,7 +77,8 @@ compile 'com.arialyy.aria:Aria:3.0.3' Aria.download(this).load(DOWNLOAD_URL).cancel(); ``` -### 二、如果你希望读取下载进度或下载信息,那么你需要创建事件类,并在onResume(Activity、Fragment)或构造函数(Dialog、PopupWindow),将该事件类注册到Aria管理器。 +### 二、下载状态获取 +如果你希望读取下载进度或下载信息,那么你需要创建事件类,并在onResume(Activity、Fragment)或构造函数(Dialog、PopupWindow),将该事件类注册到Aria管理器。 * 创建事件类 ```java @@ -104,8 +110,99 @@ compile 'com.arialyy.aria:Aria:3.0.3' } ``` -### 关于下载的其它api -[Download API](https://github.com/AriaLyy/Aria/blob/master/DownloadApi.md) +### 三、Aria参数配置 +#### 通过文件修改Aria配置参数 +创建`aria_config.xml`文件,将其放在`assets`目录下,添加以下内容 +```xml + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +``` + +#### 通过代码修改Aria参数 +除了文件方式外修改Aria参数外,同样的,你也可以在代码中动态修改Aria参数
+通过`Aria.get(this).getDownloadConfig()`或`Aria.get(this).getUploadConfig()`直接获取配置文件,然后修改参数
+如以下所示: +```java +// 修改最大下载数,调用完成后,立即生效 +// 如当前下载任务数是4,修改完成后,当前任务数会被Aria自动调度任务数 +Aria.get(this).getDownloadConfig().setMaxTaskNum(3); +``` + +### 其它好用的API +* 停止所有任务 + +```java +Aria.download(this).stopAllTask(); +``` +* 删除所有任务 + +```java +Aria.download(this).removeAllTask(); +``` +* 获取当前任务的下载速度 +速度参数有点特殊,需要[下载事件支持](#下载事件监听) +``` java +@Override public void onTaskRunning(DownloadTask task) { + //如果你打开了速度单位转换配置,将可以通过以下方法获取带单位的下载速度,如:1 m/s + String convertSpeed = task.getConvertSpeed(); + //如果你有自己的单位格式,可以通过以下方法获取原始byte长度 + long speed = task.getSpeed(); +} +``` **tips:为了防止内存泄露的情况,事件类需要使用staic进行修饰** @@ -132,9 +229,7 @@ compile 'com.arialyy.aria:Aria:3.0.3' * 取消上传 ```java - Aria.upload(this) - .load(filePath) - .cancel(); + Aria.upload(this).load(filePath).cancel(); ``` ## 其他 @@ -142,7 +237,11 @@ compile 'com.arialyy.aria:Aria:3.0.3' *** +## 后续版本开发规划 +* 实现上传队列调度功能 + ## 开发日志 + + v_3.1.0 添加Aria配置文件,优化代码 + v_3.0.3 修复暂停后删除任务,闪退问题,添加删除记录的api + v_3.0.2 支持30x重定向链接下载 + v_3.0.0 添加上传任务支持,修复一些已发现的bug diff --git a/app/src/main/assets/aria_config.xml b/app/src/main/assets/aria_config.xml index 80d8ded9..f80e558e 100644 --- a/app/src/main/assets/aria_config.xml +++ b/app/src/main/assets/aria_config.xml @@ -1,11 +1,11 @@ - + - - + + @@ -31,6 +31,9 @@ + + + From c402dde329cfcd615be71735ce32c1d0b0415518 Mon Sep 17 00:00:00 2001 From: AriaLyy <511455842@qq.com> Date: Tue, 23 May 2017 22:07:46 +0800 Subject: [PATCH 04/11] =?UTF-8?q?=E9=80=9F=E5=BA=A6=E5=8D=95=E4=BD=8D?= =?UTF-8?q?=E8=BD=AC=E6=8D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/modules.xml | 2 +- .../com/arialyy/aria/core/ConfigHelper.java | 13 +++ .../com/arialyy/aria/core/Configuration.java | 15 +++ .../aria/core/download/DownloadEntity.java | 27 ++--- .../aria/core/download/DownloadTask.java | 72 +++++++----- .../java/com/arialyy/aria/core/inf/ITask.java | 8 ++ .../aria/core/upload/UploadEntity.java | 11 ++ .../arialyy/aria/core/upload/UploadTask.java | 104 ++++++++++++------ .../java/com/arialyy/aria/orm/DBConfig.java | 2 +- app/src/main/assets/aria_config.xml | 2 +- .../simple/download/DownloadDialog.java | 6 +- .../simple/download/SingleTaskActivity.java | 2 +- .../fragment_download/DownloadFragment.java | 2 +- .../multi_download/DownloadAdapter.java | 3 +- gradle.properties | 4 - 15 files changed, 189 insertions(+), 84 deletions(-) diff --git a/.idea/modules.xml b/.idea/modules.xml index 9f4bd159..5f6de159 100644 --- a/.idea/modules.xml +++ b/.idea/modules.xml @@ -2,8 +2,8 @@ - + diff --git a/Aria/src/main/java/com/arialyy/aria/core/ConfigHelper.java b/Aria/src/main/java/com/arialyy/aria/core/ConfigHelper.java index dcb9cb68..28065ee7 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/ConfigHelper.java +++ b/Aria/src/main/java/com/arialyy/aria/core/ConfigHelper.java @@ -80,10 +80,23 @@ public class ConfigHelper extends DefaultHandler { 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)) { diff --git a/Aria/src/main/java/com/arialyy/aria/core/Configuration.java b/Aria/src/main/java/com/arialyy/aria/core/Configuration.java index 260281ac..ccc040c0 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/Configuration.java +++ b/Aria/src/main/java/com/arialyy/aria/core/Configuration.java @@ -63,6 +63,11 @@ class Configuration { */ int connectTimeOut = 5000; + /** + * 是否需要转换速度单位,转换完成后为:1b/s、1k/s、1m/s、1g/s、1t/s,如果不需要将返回byte长度 + */ + boolean isConvertSpeed = false; + public boolean isOpenBreadCast() { return isOpenBreadCast; } @@ -105,6 +110,16 @@ class Configuration { return this; } + public boolean isConvertSpeed() { + return isConvertSpeed; + } + + public BaseConfig setConvertSpeed(boolean convertSpeed) { + isConvertSpeed = convertSpeed; + saveKey("isConvertSpeed", isConvertSpeed + ""); + return this; + } + public int getConnectTimeOut() { return connectTimeOut; } diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadEntity.java b/Aria/src/main/java/com/arialyy/aria/core/download/DownloadEntity.java index f2ddb057..65dcfc28 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadEntity.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/DownloadEntity.java @@ -31,6 +31,7 @@ import com.arialyy.aria.orm.DbEntity; */ public class DownloadEntity extends DbEntity implements Parcelable, IEntity { @Ignore private long speed = 0; //下载速度 + @Ignore private String convertSpeed = "0b/s"; @Ignore private int failNum = 0; private String downloadUrl = ""; //下载路径 private String downloadPath = ""; //保存路径 @@ -43,19 +44,10 @@ public class DownloadEntity extends DbEntity implements Parcelable, IEntity { private long completeTime; //完成时间 private boolean isRedirect = false; private String redirectUrl = ""; //重定向链接 - private int threadNum; //下载线程数 public DownloadEntity() { } - public int getThreadNum() { - return threadNum; - } - - public void setThreadNum(int threadNum) { - this.threadNum = threadNum; - } - public String getStr() { return str; } @@ -131,6 +123,14 @@ public class DownloadEntity extends DbEntity implements Parcelable, IEntity { isDownloadComplete = downloadComplete; } + public String getConvertSpeed() { + return convertSpeed; + } + + public void setConvertSpeed(String convertSpeed) { + this.convertSpeed = convertSpeed; + } + public long getSpeed() { return speed; } @@ -171,6 +171,9 @@ public class DownloadEntity extends DbEntity implements Parcelable, IEntity { return "DownloadEntity{" + "speed=" + speed + + ", convertSpeed='" + + convertSpeed + + '\'' + ", failNum=" + failNum + ", downloadUrl='" @@ -200,8 +203,6 @@ public class DownloadEntity extends DbEntity implements Parcelable, IEntity { + ", redirectUrl='" + redirectUrl + '\'' - + ", threadNum=" - + threadNum + '}'; } @@ -211,6 +212,7 @@ public class DownloadEntity extends DbEntity implements Parcelable, IEntity { @Override public void writeToParcel(Parcel dest, int flags) { dest.writeLong(this.speed); + dest.writeString(this.convertSpeed); dest.writeInt(this.failNum); dest.writeString(this.downloadUrl); dest.writeString(this.downloadPath); @@ -223,11 +225,11 @@ public class DownloadEntity extends DbEntity implements Parcelable, IEntity { dest.writeLong(this.completeTime); dest.writeByte(this.isRedirect ? (byte) 1 : (byte) 0); dest.writeString(this.redirectUrl); - dest.writeInt(this.threadNum); } protected DownloadEntity(Parcel in) { this.speed = in.readLong(); + this.convertSpeed = in.readString(); this.failNum = in.readInt(); this.downloadUrl = in.readString(); this.downloadPath = in.readString(); @@ -240,7 +242,6 @@ public class DownloadEntity extends DbEntity implements Parcelable, IEntity { this.completeTime = in.readLong(); this.isRedirect = in.readByte() != 0; this.redirectUrl = in.readString(); - this.threadNum = in.readInt(); } @Ignore public static final Creator CREATOR = new Creator() { diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadTask.java b/Aria/src/main/java/com/arialyy/aria/core/download/DownloadTask.java index 35645c25..059db183 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadTask.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/DownloadTask.java @@ -40,32 +40,53 @@ public class DownloadTask implements ITask { */ private String mTargetName; private DownloadEntity mEntity; - private DownloadTaskEntity mTaskEntity; private IDownloadListener mListener; private Handler mOutHandler; private IDownloadUtil mUtil; private Context mContext; private DownloadTask(DownloadTaskEntity taskEntity, Handler outHandler) { - mTaskEntity = taskEntity; mEntity = taskEntity.downloadEntity; mOutHandler = outHandler; mContext = AriaManager.APP; - init(); - } - - private void init() { mListener = new DListener(mContext, this, mOutHandler); - mUtil = new DownloadUtil(mContext, mTaskEntity, mListener); + mUtil = new DownloadUtil(mContext, taskEntity, mListener); } /** - * 获取下载速度 + * @return 返回原始byte速度,需要你在配置文件中配置 + *
+   *   
+   *    ...
+   *    
+   *   
+   *
+   *  或在代码中设置
+   *  Aria.get(this).getDownloadConfig().setConvertSpeed(false);
+   * 
+ * 才能生效 */ @Override public long getSpeed() { return mEntity.getSpeed(); } + /** + * @return 返回转换单位后的速度,需要你在配置文件中配置,转换完成后为:1b/s、1k/s、1m/s、1g/s、1t/s + *
+   *   
+   *    ...
+   *    
+   *   
+   *
+   *  或在代码中设置
+   *  Aria.get(this).getDownloadConfig().setConvertSpeed(true);
+   * 
+ * 才能生效 + */ + @Override public String getConvertSpeed() { + return mEntity.getConvertSpeed(); + } + /** * 获取文件大小 */ @@ -182,7 +203,6 @@ public class DownloadTask implements ITask { public static class Builder { DownloadTaskEntity taskEntity; Handler outHandler; - int threadNum = 3; String targetName; public Builder(String targetName, DownloadTaskEntity taskEntity) { @@ -201,14 +221,6 @@ public class DownloadTask implements ITask { return this; } - /** - * 设置线程数 - */ - public Builder setThreadNum(int threadNum) { - this.threadNum = threadNum; - return this; - } - public DownloadTask build() { DownloadTask task = new DownloadTask(taskEntity, outHandler); task.setTargetName(targetName); @@ -225,7 +237,6 @@ public class DownloadTask implements ITask { WeakReference wTask; Context context; Intent sendIntent; - long INTERVAL = 1024 * 10; //10k大小的间隔 long lastLen = 0; //上一次发送长度 long lastTime = 0; long INTERVAL_TIME = 1000; //1m更新周期 @@ -233,6 +244,7 @@ public class DownloadTask implements ITask { DownloadEntity downloadEntity; DownloadTask task; boolean isOpenBroadCast = false; + boolean isConvertSpeed = false; DListener(Context context, DownloadTask task, Handler outHandler) { this.context = context; @@ -242,7 +254,9 @@ public class DownloadTask implements ITask { this.downloadEntity = this.task.getDownloadEntity(); sendIntent = CommonUtil.createIntent(context.getPackageName(), Aria.ACTION_RUNNING); sendIntent.putExtra(Aria.ENTITY, downloadEntity); - isOpenBroadCast = AriaManager.getInstance(context).getDownloadConfig().isOpenBreadCast(); + final AriaManager manager = AriaManager.getInstance(context); + isOpenBroadCast = manager.getDownloadConfig().isOpenBreadCast(); + isConvertSpeed = manager.getDownloadConfig().isConvertSpeed(); } @Override public void supportBreakpoint(boolean support) { @@ -289,11 +303,10 @@ public class DownloadTask implements ITask { sendIntent.putExtra(Aria.CURRENT_SPEED, speed); lastTime = System.currentTimeMillis(); if (isFirst) { - downloadEntity.setSpeed(0); + speed = 0; isFirst = false; - } else { - downloadEntity.setSpeed(speed); } + handleSpeed(speed); downloadEntity.setCurrentProgress(currentLocation); lastLen = currentLocation; sendInState2Target(DownloadSchedulers.RUNNING); @@ -304,7 +317,7 @@ public class DownloadTask implements ITask { @Override public void onStop(long stopLocation) { super.onStop(stopLocation); downloadEntity.setState(DownloadEntity.STATE_STOP); - downloadEntity.setSpeed(0); + handleSpeed(0); sendInState2Target(DownloadSchedulers.STOP); sendIntent(Aria.ACTION_STOP, stopLocation); } @@ -312,6 +325,7 @@ public class DownloadTask implements ITask { @Override public void onCancel() { super.onCancel(); downloadEntity.setState(DownloadEntity.STATE_CANCEL); + handleSpeed(0); sendInState2Target(DownloadSchedulers.CANCEL); sendIntent(Aria.ACTION_CANCEL, -1); downloadEntity.deleteData(); @@ -321,7 +335,7 @@ public class DownloadTask implements ITask { super.onComplete(); downloadEntity.setState(DownloadEntity.STATE_COMPLETE); downloadEntity.setDownloadComplete(true); - downloadEntity.setSpeed(0); + handleSpeed(0); sendInState2Target(DownloadSchedulers.COMPLETE); sendIntent(Aria.ACTION_COMPLETE, downloadEntity.getFileSize()); } @@ -330,11 +344,19 @@ public class DownloadTask implements ITask { super.onFail(); downloadEntity.setFailNum(downloadEntity.getFailNum() + 1); downloadEntity.setState(DownloadEntity.STATE_FAIL); - downloadEntity.setSpeed(0); + handleSpeed(0); sendInState2Target(DownloadSchedulers.FAIL); sendIntent(Aria.ACTION_FAIL, -1); } + private void handleSpeed(long speed) { + if (isConvertSpeed) { + downloadEntity.setConvertSpeed(CommonUtil.formatFileSize(speed) + "/s"); + } else { + downloadEntity.setSpeed(speed); + } + } + /** * 将任务状态发送给下载器 * diff --git a/Aria/src/main/java/com/arialyy/aria/core/inf/ITask.java b/Aria/src/main/java/com/arialyy/aria/core/inf/ITask.java index e9b75864..d8aa1607 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/inf/ITask.java +++ b/Aria/src/main/java/com/arialyy/aria/core/inf/ITask.java @@ -44,8 +44,16 @@ public interface ITask { public void cancel(); + /** + * 原始byte速度 + */ public long getSpeed(); + /** + * 转换单位后的速度 + */ + public String getConvertSpeed(); + public long getFileSize(); public long getCurrentProgress(); diff --git a/Aria/src/main/java/com/arialyy/aria/core/upload/UploadEntity.java b/Aria/src/main/java/com/arialyy/aria/core/upload/UploadEntity.java index dd4a54eb..816cb24c 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/upload/UploadEntity.java +++ b/Aria/src/main/java/com/arialyy/aria/core/upload/UploadEntity.java @@ -34,8 +34,17 @@ public class UploadEntity extends DbEntity implements IEntity, Parcelable { private long currentProgress = 0; private boolean isComplete = false; @Ignore private long speed = 0; //下载速度 + @Ignore private String convertSpeed = "0/s"; @Ignore private int failNum = 0; + public String getConvertSpeed() { + return convertSpeed; + } + + public void setConvertSpeed(String convertSpeed) { + this.convertSpeed = convertSpeed; + } + public boolean isComplete() { return isComplete; } @@ -115,6 +124,7 @@ public class UploadEntity extends DbEntity implements IEntity, Parcelable { dest.writeLong(this.currentProgress); dest.writeByte(this.isComplete ? (byte) 1 : (byte) 0); dest.writeLong(this.speed); + dest.writeString(this.convertSpeed); dest.writeInt(this.failNum); } @@ -126,6 +136,7 @@ public class UploadEntity extends DbEntity implements IEntity, Parcelable { this.currentProgress = in.readLong(); this.isComplete = in.readByte() != 0; this.speed = in.readLong(); + this.convertSpeed = in.readString(); this.failNum = in.readInt(); } diff --git a/Aria/src/main/java/com/arialyy/aria/core/upload/UploadTask.java b/Aria/src/main/java/com/arialyy/aria/core/upload/UploadTask.java index 8772b9fd..bc5c2d76 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/upload/UploadTask.java +++ b/Aria/src/main/java/com/arialyy/aria/core/upload/UploadTask.java @@ -36,19 +36,17 @@ import java.lang.ref.WeakReference; public class UploadTask implements ITask { private static final String TAG = "UploadTask"; private Handler mOutHandler; - private UploadTaskEntity mTaskEntity; private UploadEntity mUploadEntity; private String mTargetName; private UploadUtil mUtil; private UListener mListener; - UploadTask(UploadTaskEntity taskEntity, Handler outHandler) { - mTaskEntity = taskEntity; + private UploadTask(UploadTaskEntity taskEntity, Handler outHandler) { mOutHandler = outHandler; - mUploadEntity = mTaskEntity.uploadEntity; + mUploadEntity = taskEntity.uploadEntity; mListener = new UListener(mOutHandler, this); - mUtil = new UploadUtil(mTaskEntity, mListener); + mUtil = new UploadUtil(taskEntity, mListener); } @Override public void setTargetName(String targetName) { @@ -110,10 +108,41 @@ public class UploadTask implements ITask { return mTargetName; } + /** + * @return 返回原始byte速度,需要你在配置文件中配置 + *
+   *   
+   *    ...
+   *    
+   *   
+   *
+   *  或在代码中设置
+   *  Aria.get(this).getUploadConfig().setConvertSpeed(false);
+   * 
+ * 才能生效 + */ @Override public long getSpeed() { return mUploadEntity.getSpeed(); } + /** + * @return 返回转换单位后的速度,需要你在配置文件中配置,转换完成后为:1b/s、1k/s、1m/s、1g/s、1t/s + *
+   *   
+   *    ...
+   *    
+   *   
+   *
+   *  或在代码中设置
+   *  Aria.get(this).getUploadConfig().setConvertSpeed(true);
+   * 
+ * + * 才能生效 + */ + @Override public String getConvertSpeed() { + return mUploadEntity.getConvertSpeed(); + } + @Override public long getFileSize() { return mUploadEntity.getFileSize(); } @@ -129,43 +158,46 @@ public class UploadTask implements ITask { long lastTime = 0; long INTERVAL_TIME = 1000; //1m更新周期 boolean isFirst = true; - UploadEntity entity; + UploadEntity uploadEntity; Intent sendIntent; boolean isOpenBroadCast = false; + boolean isConvertSpeed = false; Context context; UListener(Handler outHandle, UploadTask task) { this.outHandler = new WeakReference<>(outHandle); this.task = new WeakReference<>(task); - entity = this.task.get().getUploadEntity(); + uploadEntity = this.task.get().getUploadEntity(); sendIntent = CommonUtil.createIntent(AriaManager.APP.getPackageName(), Aria.ACTION_RUNNING); - sendIntent.putExtra(Aria.ENTITY, entity); + sendIntent.putExtra(Aria.ENTITY, uploadEntity); context = AriaManager.APP; - isOpenBroadCast = AriaManager.getInstance(context).getUploadConfig().isOpenBreadCast(); + final AriaManager manager = AriaManager.getInstance(context); + isOpenBroadCast = manager.getUploadConfig().isOpenBreadCast(); + isConvertSpeed = manager.getUploadConfig().isConvertSpeed(); } @Override public void onPre() { - entity.setState(IEntity.STATE_PRE); + uploadEntity.setState(IEntity.STATE_PRE); sendIntent(Aria.ACTION_PRE, -1); sendInState2Target(ISchedulers.PRE); } @Override public void onStart(long fileSize) { - entity.setFileSize(fileSize); - entity.setState(IEntity.STATE_RUNNING); + uploadEntity.setFileSize(fileSize); + uploadEntity.setState(IEntity.STATE_RUNNING); sendIntent(Aria.ACTION_PRE, -1); sendInState2Target(ISchedulers.START); } @Override public void onResume(long resumeLocation) { - entity.setState(DownloadEntity.STATE_RUNNING); + uploadEntity.setState(DownloadEntity.STATE_RUNNING); sendInState2Target(DownloadSchedulers.RESUME); sendIntent(Aria.ACTION_RESUME, resumeLocation); } @Override public void onStop(long stopLocation) { - entity.setState(DownloadEntity.STATE_STOP); - entity.setSpeed(0); + uploadEntity.setState(DownloadEntity.STATE_STOP); + handleSpeed(0); sendInState2Target(DownloadSchedulers.STOP); sendIntent(Aria.ACTION_STOP, stopLocation); } @@ -177,12 +209,11 @@ public class UploadTask implements ITask { sendIntent.putExtra(Aria.CURRENT_SPEED, speed); lastTime = System.currentTimeMillis(); if (isFirst) { - entity.setSpeed(0); + speed = 0; isFirst = false; - } else { - entity.setSpeed(speed); } - entity.setCurrentProgress(currentLocation); + handleSpeed(speed); + uploadEntity.setCurrentProgress(currentLocation); lastLen = currentLocation; sendInState2Target(DownloadSchedulers.RUNNING); AriaManager.APP.sendBroadcast(sendIntent); @@ -190,28 +221,37 @@ public class UploadTask implements ITask { } @Override public void onCancel() { - entity.setState(DownloadEntity.STATE_CANCEL); + uploadEntity.setState(DownloadEntity.STATE_CANCEL); + handleSpeed(0); sendInState2Target(DownloadSchedulers.CANCEL); sendIntent(Aria.ACTION_CANCEL, -1); - entity.deleteData(); + uploadEntity.deleteData(); } @Override public void onComplete() { - entity.setState(DownloadEntity.STATE_COMPLETE); - entity.setComplete(true); - entity.setSpeed(0); + uploadEntity.setState(DownloadEntity.STATE_COMPLETE); + uploadEntity.setComplete(true); + handleSpeed(0); sendInState2Target(DownloadSchedulers.COMPLETE); - sendIntent(Aria.ACTION_COMPLETE, entity.getFileSize()); + sendIntent(Aria.ACTION_COMPLETE, uploadEntity.getFileSize()); } @Override public void onFail() { - entity.setFailNum(entity.getFailNum() + 1); - entity.setState(DownloadEntity.STATE_FAIL); - entity.setSpeed(0); + uploadEntity.setFailNum(uploadEntity.getFailNum() + 1); + uploadEntity.setState(DownloadEntity.STATE_FAIL); + handleSpeed(0); sendInState2Target(DownloadSchedulers.FAIL); sendIntent(Aria.ACTION_FAIL, -1); } + private void handleSpeed(long speed) { + if (isConvertSpeed) { + uploadEntity.setConvertSpeed(CommonUtil.formatFileSize(speed) + "/s"); + } else { + uploadEntity.setSpeed(speed); + } + } + /** * 将任务状态发送给下载器 * @@ -224,12 +264,12 @@ public class UploadTask implements ITask { } private void sendIntent(String action, long location) { - entity.setComplete(action.equals(Aria.ACTION_COMPLETE)); - entity.setCurrentProgress(location); - entity.update(); + uploadEntity.setComplete(action.equals(Aria.ACTION_COMPLETE)); + uploadEntity.setCurrentProgress(location); + uploadEntity.update(); if (!isOpenBroadCast) return; Intent intent = CommonUtil.createIntent(context.getPackageName(), action); - intent.putExtra(Aria.ENTITY, entity); + intent.putExtra(Aria.ENTITY, uploadEntity); if (location != -1) { intent.putExtra(Aria.CURRENT_LOCATION, location); } diff --git a/Aria/src/main/java/com/arialyy/aria/orm/DBConfig.java b/Aria/src/main/java/com/arialyy/aria/orm/DBConfig.java index d380c879..8f2ed69c 100644 --- a/Aria/src/main/java/com/arialyy/aria/orm/DBConfig.java +++ b/Aria/src/main/java/com/arialyy/aria/orm/DBConfig.java @@ -28,7 +28,7 @@ import java.util.Map; public class DBConfig { static Map mapping = new HashMap<>(); static String DB_NAME; - static int VERSION = 3; + static int VERSION = 2; static { if (TextUtils.isEmpty(DB_NAME)) { diff --git a/app/src/main/assets/aria_config.xml b/app/src/main/assets/aria_config.xml index f80e558e..c6ca4927 100644 --- a/app/src/main/assets/aria_config.xml +++ b/app/src/main/assets/aria_config.xml @@ -32,7 +32,7 @@ - +
diff --git a/app/src/main/java/com/arialyy/simple/download/DownloadDialog.java b/app/src/main/java/com/arialyy/simple/download/DownloadDialog.java index c88904b6..1f18dc73 100644 --- a/app/src/main/java/com/arialyy/simple/download/DownloadDialog.java +++ b/app/src/main/java/com/arialyy/simple/download/DownloadDialog.java @@ -110,14 +110,14 @@ public class DownloadDialog extends AbsDialog { @Override public void onTaskStop(DownloadTask task) { super.onTaskStop(task); setBtState(true); - mSpeed.setText("0.0kb/s"); + mSpeed.setText(task.getConvertSpeed()); } @Override public void onTaskCancel(DownloadTask task) { super.onTaskCancel(task); setBtState(true); mPb.setProgress(0); - mSpeed.setText("0.0kb/s"); + mSpeed.setText(task.getConvertSpeed()); } @Override public void onTaskRunning(DownloadTask task) { @@ -129,7 +129,7 @@ public class DownloadDialog extends AbsDialog { } else { mPb.setProgress((int) ((current * 100) / len)); } - mSpeed.setText(CommonUtil.formatFileSize(task.getSpeed()) + "/s"); + mSpeed.setText(task.getConvertSpeed()); } } } diff --git a/app/src/main/java/com/arialyy/simple/download/SingleTaskActivity.java b/app/src/main/java/com/arialyy/simple/download/SingleTaskActivity.java index b06e5e91..7b4dd7d6 100644 --- a/app/src/main/java/com/arialyy/simple/download/SingleTaskActivity.java +++ b/app/src/main/java/com/arialyy/simple/download/SingleTaskActivity.java @@ -91,7 +91,7 @@ public class SingleTaskActivity extends BaseActivity { } else { mPb.setProgress((int) ((current * 100) / len)); } - mSpeed.setText(CommonUtil.formatFileSize(task.getSpeed()) + "/s"); + mSpeed.setText(task.getConvertSpeed()); break; case DOWNLOAD_PRE: mSize.setText(CommonUtil.formatFileSize((Long) msg.obj)); diff --git a/app/src/main/java/com/arialyy/simple/download/fragment_download/DownloadFragment.java b/app/src/main/java/com/arialyy/simple/download/fragment_download/DownloadFragment.java index 661032d1..afb2f068 100644 --- a/app/src/main/java/com/arialyy/simple/download/fragment_download/DownloadFragment.java +++ b/app/src/main/java/com/arialyy/simple/download/fragment_download/DownloadFragment.java @@ -134,7 +134,7 @@ public class DownloadFragment extends AbsFragment { } else { mPb.setProgress((int) ((current * 100) / len)); } - mSpeed.setText(CommonUtil.formatFileSize(task.getSpeed()) + "/s"); + mSpeed.setText(task.getConvertSpeed()); } } } diff --git a/app/src/main/java/com/arialyy/simple/download/multi_download/DownloadAdapter.java b/app/src/main/java/com/arialyy/simple/download/multi_download/DownloadAdapter.java index 646e8e1e..7d5bb80f 100644 --- a/app/src/main/java/com/arialyy/simple/download/multi_download/DownloadAdapter.java +++ b/app/src/main/java/com/arialyy/simple/download/multi_download/DownloadAdapter.java @@ -103,7 +103,6 @@ final class DownloadAdapter extends AbsRVAdapter Date: Tue, 23 May 2017 22:19:42 +0800 Subject: [PATCH 05/11] Update README.md --- README.md | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 877197db..c06f5420 100644 --- a/README.md +++ b/README.md @@ -4,14 +4,14 @@ Aria项目源于15年工作中遇到的一个文件下载管理的需求,当 Aria有以下特点: + 简单、方便 - - 可以在Activity、Service、Fragment、Dialog、popupWindow、Notification等组件中使用 - - 支持任务自动调度,使用者不需要关心任务状态切换的逻辑 - - [通过Aria的事件,能很容易获取当前下载任务的下载状态](#二、下载状态获取) - - [一句代码就可以动态设置最大下载数](#通过代码修改Aria参数) - - [一句代码加可以获取当前的下载速度](#其它好用的API) - - [通过修改配置文件很容易就能修改下载线程数](#通过文件修改Aria配置参数) + - 可以在Activity、Service、Fragment、Dialog、popupWindow、Notification等组件中使用 + - 支持任务自动调度,使用者不需要关心任务状态切换的逻辑 + - [通过Aria的事件,能很容易获取当前下载任务的下载状态](#二、下载状态获取) + - [一句代码就可以动态设置最大下载数](#通过代码修改Aria参数) + - [一句代码加可以获取当前的下载速度](#其它好用的API) + - [通过修改配置文件很容易就能修改下载线程数](#通过文件修改Aria配置参数) + 支持https地址下载 - - 在配置文件中很容易就可以设置CA证书的信息 + - 在配置文件中很容易就可以设置CA证书的信息 + 支持300、301、302重定向下载链接下载 + 支持上传操作 @@ -203,6 +203,14 @@ Aria.download(this).removeAllTask(); long speed = task.getSpeed(); } ``` +* 获取下载的文件大小 +同样的,你也可以在DownloadTask对象中获取下载的文件大小 +``` +@Override public void onTaskRunning(DownloadTask task) { +  //获取文件大小 + long fileSize = task.getFileSize(); +} +``` **tips:为了防止内存泄露的情况,事件类需要使用staic进行修饰** @@ -238,7 +246,7 @@ Aria.download(this).removeAllTask(); *** ## 后续版本开发规划 -* 实现上传队列调度功能 +* ~~实现上传队列调度功能~~ ## 开发日志 + v_3.1.0 添加Aria配置文件,优化代码 From ac329c2342fd4decf21c35fbcea4e4284fd1e403 Mon Sep 17 00:00:00 2001 From: laoyuyu <511455842@QQ.com> Date: Tue, 23 May 2017 22:29:50 +0800 Subject: [PATCH 06/11] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index c06f5420..75f86790 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ Aria有以下特点: - 可以在Activity、Service、Fragment、Dialog、popupWindow、Notification等组件中使用 - 支持任务自动调度,使用者不需要关心任务状态切换的逻辑 - [通过Aria的事件,能很容易获取当前下载任务的下载状态](#二、下载状态获取) - - [一句代码就可以动态设置最大下载数](#通过代码修改Aria参数) + - [一句代码就可以动态设置最大下载数](#示例) - [一句代码加可以获取当前的下载速度](#其它好用的API) - [通过修改配置文件很容易就能修改下载线程数](#通过文件修改Aria配置参数) + 支持https地址下载 From a64983a64efdca721b2459d153395f27f5510999 Mon Sep 17 00:00:00 2001 From: laoyuyu <511455842@QQ.com> Date: Tue, 23 May 2017 22:31:21 +0800 Subject: [PATCH 07/11] Update README.md --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 75f86790..ba075117 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ Aria有以下特点: - 可以在Activity、Service、Fragment、Dialog、popupWindow、Notification等组件中使用 - 支持任务自动调度,使用者不需要关心任务状态切换的逻辑 - [通过Aria的事件,能很容易获取当前下载任务的下载状态](#二、下载状态获取) - - [一句代码就可以动态设置最大下载数](#示例) +   - [一句代码就可以动态设置最大下载数](#代码) - [一句代码加可以获取当前的下载速度](#其它好用的API) - [通过修改配置文件很容易就能修改下载线程数](#通过文件修改Aria配置参数) + 支持https地址下载 @@ -172,7 +172,7 @@ compile 'com.arialyy.aria:Aria:3.1.0' ``` -#### 通过代码修改Aria参数 +#### 代码 除了文件方式外修改Aria参数外,同样的,你也可以在代码中动态修改Aria参数
通过`Aria.get(this).getDownloadConfig()`或`Aria.get(this).getUploadConfig()`直接获取配置文件,然后修改参数
如以下所示: From 838e57c57a5ce938074851050a562cbf96360411 Mon Sep 17 00:00:00 2001 From: laoyuyu <511455842@QQ.com> Date: Tue, 23 May 2017 22:33:01 +0800 Subject: [PATCH 08/11] Update README.md --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index ba075117..1acd798a 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ Aria有以下特点: - 可以在Activity、Service、Fragment、Dialog、popupWindow、Notification等组件中使用 - 支持任务自动调度,使用者不需要关心任务状态切换的逻辑 - [通过Aria的事件,能很容易获取当前下载任务的下载状态](#二、下载状态获取) -   - [一句代码就可以动态设置最大下载数](#代码) +   - [一句代码就可以动态设置最大下载数](#通过代码修改Aria参数) - [一句代码加可以获取当前的下载速度](#其它好用的API) - [通过修改配置文件很容易就能修改下载线程数](#通过文件修改Aria配置参数) + 支持https地址下载 @@ -172,7 +172,7 @@ compile 'com.arialyy.aria:Aria:3.1.0' ``` -#### 代码 +#### 通过代码修改Aria参数 除了文件方式外修改Aria参数外,同样的,你也可以在代码中动态修改Aria参数
通过`Aria.get(this).getDownloadConfig()`或`Aria.get(this).getUploadConfig()`直接获取配置文件,然后修改参数
如以下所示: From 93768cf2c29bfe8b267cd6de7ca3b36b81c260e1 Mon Sep 17 00:00:00 2001 From: laoyuyu <511455842@QQ.com> Date: Tue, 23 May 2017 22:38:24 +0800 Subject: [PATCH 09/11] Update README.md --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 1acd798a..c52bc007 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ Aria有以下特点: - 可以在Activity、Service、Fragment、Dialog、popupWindow、Notification等组件中使用 - 支持任务自动调度,使用者不需要关心任务状态切换的逻辑 - [通过Aria的事件,能很容易获取当前下载任务的下载状态](#二、下载状态获取) -   - [一句代码就可以动态设置最大下载数](#通过代码修改Aria参数) +   - [一句代码就可以动态设置最大下载数](#代码中设置参数) - [一句代码加可以获取当前的下载速度](#其它好用的API) - [通过修改配置文件很容易就能修改下载线程数](#通过文件修改Aria配置参数) + 支持https地址下载 @@ -111,7 +111,7 @@ compile 'com.arialyy.aria:Aria:3.1.0' ``` ### 三、Aria参数配置 -#### 通过文件修改Aria配置参数 +#### 配置文件设置参数 创建`aria_config.xml`文件,将其放在`assets`目录下,添加以下内容 ```xml @@ -172,7 +172,7 @@ compile 'com.arialyy.aria:Aria:3.1.0' ``` -#### 通过代码修改Aria参数 +#### 代码中设置参数 除了文件方式外修改Aria参数外,同样的,你也可以在代码中动态修改Aria参数
通过`Aria.get(this).getDownloadConfig()`或`Aria.get(this).getUploadConfig()`直接获取配置文件,然后修改参数
如以下所示: From e5aa798df5061f0a5b0f0dcce73008aad4cb0656 Mon Sep 17 00:00:00 2001 From: laoyuyu <511455842@QQ.com> Date: Tue, 23 May 2017 22:42:23 +0800 Subject: [PATCH 10/11] Update README.md --- README.md | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index c52bc007..367a50d1 100644 --- a/README.md +++ b/README.md @@ -6,10 +6,10 @@ Aria有以下特点: + 简单、方便 - 可以在Activity、Service、Fragment、Dialog、popupWindow、Notification等组件中使用 - 支持任务自动调度,使用者不需要关心任务状态切换的逻辑 - - [通过Aria的事件,能很容易获取当前下载任务的下载状态](#二、下载状态获取) -   - [一句代码就可以动态设置最大下载数](#代码中设置参数) - - [一句代码加可以获取当前的下载速度](#其它好用的API) - - [通过修改配置文件很容易就能修改下载线程数](#通过文件修改Aria配置参数) + - [通过Aria的事件,能很容易获取当前下载任务的下载状态](#下载状态获取) + - [一句代码加可以获取当前的下载速度](#常用接口) + - [一句代码就可以动态设置最大下载数](#代码中设置参数) + - [通过修改配置文件很容易就能修改下载线程数](#配置文件设置参数) + 支持https地址下载 - 在配置文件中很容易就可以设置CA证书的信息 + 支持300、301、302重定向下载链接下载 @@ -77,7 +77,7 @@ compile 'com.arialyy.aria:Aria:3.1.0' Aria.download(this).load(DOWNLOAD_URL).cancel(); ``` -### 二、下载状态获取 +### 下载状态获取 如果你希望读取下载进度或下载信息,那么你需要创建事件类,并在onResume(Activity、Fragment)或构造函数(Dialog、PopupWindow),将该事件类注册到Aria管理器。 * 创建事件类 @@ -110,7 +110,7 @@ compile 'com.arialyy.aria:Aria:3.1.0' } ``` -### 三、Aria参数配置 +### Aria参数配置 #### 配置文件设置参数 创建`aria_config.xml`文件,将其放在`assets`目录下,添加以下内容 ```xml @@ -182,7 +182,7 @@ compile 'com.arialyy.aria:Aria:3.1.0' Aria.get(this).getDownloadConfig().setMaxTaskNum(3); ``` -### 其它好用的API +### 常用接口 * 停止所有任务 ```java @@ -194,7 +194,7 @@ Aria.download(this).stopAllTask(); Aria.download(this).removeAllTask(); ``` * 获取当前任务的下载速度 -速度参数有点特殊,需要[下载事件支持](#下载事件监听) +速度参数有点特殊,需要[下载事件支持](#下载状态获取) ``` java @Override public void onTaskRunning(DownloadTask task) { //如果你打开了速度单位转换配置,将可以通过以下方法获取带单位的下载速度,如:1 m/s From 5b0951c7650323da05f8c405aa15e0038d1bdc5c Mon Sep 17 00:00:00 2001 From: AriaLyy <511455842@qq.com> Date: Wed, 24 May 2017 11:26:33 +0800 Subject: [PATCH 11/11] doc --- .idea/modules.xml | 2 +- Aria/build.gradle | 4 +-- .../com/arialyy/aria/core/AriaManager.java | 24 +++++++------- .../aria/core/download/DownloadTask.java | 32 ++++++++++++------- .../arialyy/aria/core/upload/UploadTask.java | 32 ++++++++++++------- .../simple/download/DownloadDialog.java | 2 +- .../simple/download/DownloadPopupWindow.java | 2 +- .../fragment_download/DownloadFragment.java | 3 +- .../service_download/DownloadService.java | 6 +--- 9 files changed, 60 insertions(+), 47 deletions(-) diff --git a/.idea/modules.xml b/.idea/modules.xml index 5f6de159..9f4bd159 100644 --- a/.idea/modules.xml +++ b/.idea/modules.xml @@ -2,8 +2,8 @@ - + diff --git a/Aria/build.gradle b/Aria/build.gradle index 9eeea133..36c3b695 100644 --- a/Aria/build.gradle +++ b/Aria/build.gradle @@ -7,8 +7,8 @@ android { defaultConfig { minSdkVersion 9 targetSdkVersion 23 - versionCode 102 - versionName "3.0.3" + versionCode 310 + versionName "3.1.0" } buildTypes { release { diff --git a/Aria/src/main/java/com/arialyy/aria/core/AriaManager.java b/Aria/src/main/java/com/arialyy/aria/core/AriaManager.java index d9972ac1..1e4a2ed0 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/AriaManager.java +++ b/Aria/src/main/java/com/arialyy/aria/core/AriaManager.java @@ -93,16 +93,18 @@ import org.xml.sax.SAXException; SAXParser parser = factory.newSAXParser(); parser.parse(APP.getAssets().open("aria_config.xml"), helper); } catch (ParserConfigurationException | IOException | SAXException e) { - Log.d(TAG, e.toString()); + Log.e(TAG, e.toString()); } } /** * 如果需要在代码中修改下载配置,请使用以下方法 - * - * @ //修改最大任务队列数 - * Aria.get(this).getDownloadConfig().setMaxTaskNum(3); - * + *
+   *   
+   *     //修改最大任务队列数
+   *     Aria.get(this).getDownloadConfig().setMaxTaskNum(3);
+   *   
+   * 
*/ public Configuration.DownloadConfig getDownloadConfig() { return mDConfig; @@ -110,10 +112,12 @@ import org.xml.sax.SAXException; /** * 如果需要在代码中修改下载配置,请使用以下方法 - * - * @ //修改最大任务队列数 - * Aria.get(this).getUploadConfig().setMaxTaskNum(3); - * + *
+   *   
+   *     //修改最大任务队列数
+   *     Aria.get(this).getUploadConfig().setMaxTaskNum(3);
+   *   
+   * 
*/ public Configuration.UploadConfig getUploadConfig() { return mUConfig; @@ -242,8 +246,6 @@ import org.xml.sax.SAXException; * 初始化配置文件 */ private void initConfig() { - //File dFile = new File(APP.getFilesDir().getPath() + Configuration.DOWNLOAD_CONFIG_FILE); - //File uFile = new File(APP.getFilesDir().getPath() + Configuration.UPLOAD_CONFIG_FILE); File xmlFile = new File(APP.getFilesDir().getPath() + Configuration.XML_FILE); if (!xmlFile.exists()) { loadConfig(); diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadTask.java b/Aria/src/main/java/com/arialyy/aria/core/download/DownloadTask.java index 059db183..80a99edf 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadTask.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/DownloadTask.java @@ -56,13 +56,17 @@ public class DownloadTask implements ITask { /** * @return 返回原始byte速度,需要你在配置文件中配置 *
-   *   
-   *    ...
-   *    
-   *   
+   *   {@code
+   *    
+   *      
+   *        ...
+   *        
+   *      
    *
-   *  或在代码中设置
-   *  Aria.get(this).getDownloadConfig().setConvertSpeed(false);
+   *      或在代码中设置
+   *      Aria.get(this).getDownloadConfig().setConvertSpeed(false);
+   *    
+   *   }
    * 
* 才能生效 */ @@ -73,13 +77,17 @@ public class DownloadTask implements ITask { /** * @return 返回转换单位后的速度,需要你在配置文件中配置,转换完成后为:1b/s、1k/s、1m/s、1g/s、1t/s *
-   *   
-   *    ...
-   *    
-   *   
+   *   {@code
+   *    
+   *      
+   *        ...
+   *        
+   *      
    *
-   *  或在代码中设置
-   *  Aria.get(this).getDownloadConfig().setConvertSpeed(true);
+   *      或在代码中设置
+   *      Aria.get(this).getDownloadConfig().setConvertSpeed(true);
+   *    
+   *   }
    * 
* 才能生效 */ diff --git a/Aria/src/main/java/com/arialyy/aria/core/upload/UploadTask.java b/Aria/src/main/java/com/arialyy/aria/core/upload/UploadTask.java index bc5c2d76..87b05d41 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/upload/UploadTask.java +++ b/Aria/src/main/java/com/arialyy/aria/core/upload/UploadTask.java @@ -111,13 +111,17 @@ public class UploadTask implements ITask { /** * @return 返回原始byte速度,需要你在配置文件中配置 *
-   *   
-   *    ...
-   *    
-   *   
+   *  {@code
+   *   
+   *    
+   *      ...
+   *      
+   *    
    *
-   *  或在代码中设置
-   *  Aria.get(this).getUploadConfig().setConvertSpeed(false);
+   *    或在代码中设置
+   *    Aria.get(this).getUploadConfig().setConvertSpeed(false);
+   *   
+   *  }
    * 
* 才能生效 */ @@ -128,13 +132,17 @@ public class UploadTask implements ITask { /** * @return 返回转换单位后的速度,需要你在配置文件中配置,转换完成后为:1b/s、1k/s、1m/s、1g/s、1t/s *
-   *   
-   *    ...
-   *    
-   *   
+   *   {@code
+   *   
+   *    
+   *      ...
+   *      
+   *    
    *
-   *  或在代码中设置
-   *  Aria.get(this).getUploadConfig().setConvertSpeed(true);
+   *    或在代码中设置
+   *    Aria.get(this).getUploadConfig().setConvertSpeed(true);
+   *   
+   *   }
    * 
* * 才能生效 diff --git a/app/src/main/java/com/arialyy/simple/download/DownloadDialog.java b/app/src/main/java/com/arialyy/simple/download/DownloadDialog.java index 1f18dc73..ffc5e6ef 100644 --- a/app/src/main/java/com/arialyy/simple/download/DownloadDialog.java +++ b/app/src/main/java/com/arialyy/simple/download/DownloadDialog.java @@ -44,7 +44,7 @@ public class DownloadDialog extends AbsDialog { @Bind(R.id.speed) TextView mSpeed; private static final String DOWNLOAD_URL = - "http://static.gaoshouyou.com/d/3a/93/573ae1db9493a801c24bf66128b11e39.apk"; + "http://clashroyalecdn.static.kunlun.com/Clash_Royale-1.2.6-kunlun_landing_page-release.apk.apk"; public DownloadDialog(Context context) { super(context); diff --git a/app/src/main/java/com/arialyy/simple/download/DownloadPopupWindow.java b/app/src/main/java/com/arialyy/simple/download/DownloadPopupWindow.java index 450f3004..538f320d 100644 --- a/app/src/main/java/com/arialyy/simple/download/DownloadPopupWindow.java +++ b/app/src/main/java/com/arialyy/simple/download/DownloadPopupWindow.java @@ -131,7 +131,7 @@ public class DownloadPopupWindow extends AbsPopupWindow { } else { mPb.setProgress((int) ((current * 100) / len)); } - mSpeed.setText(CommonUtil.formatFileSize(task.getSpeed()) + "/s"); + mSpeed.setText(task.getConvertSpeed()); } } } diff --git a/app/src/main/java/com/arialyy/simple/download/fragment_download/DownloadFragment.java b/app/src/main/java/com/arialyy/simple/download/fragment_download/DownloadFragment.java index afb2f068..bb90e783 100644 --- a/app/src/main/java/com/arialyy/simple/download/fragment_download/DownloadFragment.java +++ b/app/src/main/java/com/arialyy/simple/download/fragment_download/DownloadFragment.java @@ -45,7 +45,7 @@ public class DownloadFragment extends AbsFragment { @Bind(R.id.speed) TextView mSpeed; private static final String DOWNLOAD_URL = - "http://static.gaoshouyou.com/d/3a/93/573ae1db9493a801c24bf66128b11e39.apk"; + "http://rs.0.gaoshouyou.com/d/90/d7/7490c6fd6cd733bef336e766778507c5.apk"; @Override protected void init(Bundle savedInstanceState) { if (Aria.download(this).taskExists(DOWNLOAD_URL)) { @@ -74,7 +74,6 @@ public class DownloadFragment extends AbsFragment { Aria.download(this) .load(DOWNLOAD_URL) .setDownloadPath(Environment.getExternalStorageDirectory().getPath() + "/daialog.apk") - .setDownloadName("daialog.apk") .start(); break; case R.id.stop: diff --git a/app/src/main/java/com/arialyy/simple/download/service_download/DownloadService.java b/app/src/main/java/com/arialyy/simple/download/service_download/DownloadService.java index 92cc65f4..5e2038c0 100644 --- a/app/src/main/java/com/arialyy/simple/download/service_download/DownloadService.java +++ b/app/src/main/java/com/arialyy/simple/download/service_download/DownloadService.java @@ -31,11 +31,7 @@ import com.arialyy.frame.util.show.T; public class DownloadService extends Service { private static final String DOWNLOAD_URL = - //"http://kotlinlang.org/docs/kotlin-docs.pdf"; - //"https://atom-installer.github.com/v1.13.0/AtomSetup.exe?s=1484074138&ext=.exe"; - //"http://static.gaoshouyou.com/d/21/e8/61218d78d0e8b79df68dbc18dd484c97.apk"; - //不支持断点的链接 - "http://ox.konsung.net:5555/ksdc-web/download/downloadFile/?fileName=ksdc_1.0.2.apk&rRange=0-"; + "http://rs.0.gaoshouyou.com/d/df/db/03df9eab61dbc48a5939f671f05f1cdf.apk"; private DownloadNotification mNotify; @Nullable @Override public IBinder onBind(Intent intent) {