From 2566f37403a83f08099b0c24f8ea4718aadf0829 Mon Sep 17 00:00:00 2001 From: AriaLyy <511455842@qq.com> Date: Fri, 9 Jun 2017 14:16:48 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E6=9C=80=E5=A4=A7=E9=80=9F=E5=BA=A6?= =?UTF-8?q?=E9=99=90=E5=88=B6=E5=AE=9E=E7=8E=B0=20https://github.com/AriaL?= =?UTF-8?q?yy/Aria/issues/32?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Aria/build.gradle | 4 +-- .../com/arialyy/aria/core/ConfigHelper.java | 13 +++++++ .../aria/core/download/DownloadReceiver.java | 10 ++++++ .../aria/core/download/DownloadTask.java | 9 +++++ .../aria/core/download/DownloadUtil.java | 9 +++++ .../aria/core/download/IDownloadUtil.java | 5 +++ .../aria/core/download/SingleThreadTask.java | 15 ++++++++ .../aria/core/queue/DownloadTaskQueue.java | 9 +++++ .../aria/core/scheduler/AbsSchedulers.java | 8 +++-- app/build.gradle | 2 ++ app/src/main/assets/aria_config.xml | 3 ++ .../simple/download/SingleTaskActivity.java | 36 +++++++++++++++---- .../res/menu/menu_single_task_activity.xml | 22 ++++++++++++ .../com/arialyy/aria/core/Configuration.java | 20 +++++++++++ build.gradle | 2 +- 15 files changed, 156 insertions(+), 11 deletions(-) diff --git a/Aria/build.gradle b/Aria/build.gradle index c0c5108c..af990516 100644 --- a/Aria/build.gradle +++ b/Aria/build.gradle @@ -23,8 +23,8 @@ dependencies { compile fileTree(include: ['*.jar'], dir: 'libs') testCompile 'junit:junit:4.12' compile 'com.android.support:appcompat-v7:23.1.1' -// compile project(':AriaCompiler') - compile project(':AriaAnnotations') + compile project(':AriaCompiler') +// compile project(':AriaAnnotations') } //apply from: 'jcenter.gradle' 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 191101a2..e4c94262 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/ConfigHelper.java +++ b/Aria/src/main/java/com/arialyy/aria/core/ConfigHelper.java @@ -83,10 +83,23 @@ class ConfigHelper extends DefaultHandler { case "convertSpeed": loadConvertSpeed(value); break; + case "maxSpeed": + loadMaxSpeed(value); + break; } } } + private void loadMaxSpeed(String value) { + double maxSpeed = 0.0; + if (!TextUtils.isEmpty(value)) { + maxSpeed = Double.parseDouble(value); + } + if (isDownloadConfig){ + mDownloadConfig.msxSpeed = maxSpeed; + } + } + private void loadConvertSpeed(String value) { boolean open = Boolean.parseBoolean(value); if (isDownloadConfig) { diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadReceiver.java b/Aria/src/main/java/com/arialyy/aria/core/download/DownloadReceiver.java index 0ef0d5ad..364ccf4a 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadReceiver.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/DownloadReceiver.java @@ -39,6 +39,16 @@ public class DownloadReceiver implements IReceiver { public Object obj; public ISchedulerListener listener; + + /** + * 设置最大下载速度,单位:kb + * + * @param maxSpeed 为0表示不限速 + */ + public void setMaxSpeed(double maxSpeed) { + AriaManager.getInstance(AriaManager.APP).getDownloadConfig().setMsxSpeed(maxSpeed); + } + /** * {@link #load(String)},请使用该方法 */ 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 97bd7917..00dac674 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 @@ -88,6 +88,15 @@ public class DownloadTask extends AbsTask { stop(true); } + /** + * 设置最大下载速度,单位:kb + * + * @param maxSpeed 为0表示不限速 + */ + public void setMaxSpeed(double maxSpeed) { + mUtil.setMaxSpeed(maxSpeed); + } + /** * 开始下载 */ 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 0dfda502..5c5fef19 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 @@ -107,6 +107,15 @@ class DownloadUtil implements IDownloadUtil, Runnable { return CONSTANCE.isDownloading; } + public void setMaxSpeed(double maxSpeed) { + for (int i = 0; i < THREAD_NUM; i++) { + SingleThreadTask task = (SingleThreadTask) mTask.get(i); + if (task != null) { + task.setMaxSpeed(maxSpeed); + } + } + } + /** * 取消下载 */ diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/IDownloadUtil.java b/Aria/src/main/java/com/arialyy/aria/core/download/IDownloadUtil.java index 5c0ba1cd..471d0048 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/IDownloadUtil.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/IDownloadUtil.java @@ -63,4 +63,9 @@ interface IDownloadUtil { * 删除temp文件 */ public void delTempFile(); + + /** + * 设置最大下载速度 + */ + public void setMaxSpeed(double maxSpeed); } \ No newline at end of file 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 973a956f..bb2cb8ea 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 @@ -23,6 +23,7 @@ import com.arialyy.aria.util.CommonUtil; import java.io.File; import java.io.IOException; import java.io.InputStream; +import java.math.BigDecimal; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; @@ -40,6 +41,10 @@ final class SingleThreadTask implements Runnable { private int mBufSize; private IDownloadListener mListener; private DownloadStateConstance CONSTANCE; + /** + * speed = (bufSize / 1024) * threadNum / sleepTime; (8192 / 1024) * 4 / 1= 32 kb/s + */ + private long mSleepTime = 0; SingleThreadTask(DownloadStateConstance constance, IDownloadListener listener, DownloadUtil.ConfigEntity downloadInfo) { @@ -53,6 +58,15 @@ final class SingleThreadTask implements Runnable { mConfigFPath = downloadInfo.CONFIG_FILE_PATH; } mBufSize = manager.getDownloadConfig().getBuffSize(); + setMaxSpeed(AriaManager.getInstance(AriaManager.APP).getDownloadConfig().getMsxSpeed()); + } + + void setMaxSpeed(double maxSpeed) { + if (-0.9999 < maxSpeed && maxSpeed < 0.00001) { + mSleepTime = 0; + } else { + mSleepTime = (long) ((mBufSize / 1024) * CONSTANCE.THREAD_NUM / maxSpeed * 1000); + } } @Override public void run() { @@ -97,6 +111,7 @@ final class SingleThreadTask implements Runnable { if (CONSTANCE.isStop) { break; } + Thread.sleep(mSleepTime); //把下载数据数据写入文件 file.write(buffer, 0, len); progress(len); 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 1dfe99de..4140433a 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 @@ -91,6 +91,15 @@ public class DownloadTaskQueue } } + public void setMaxSpeed(double maxSpeed){ + Map tasks = mExecutePool.getAllTask(); + Set keys = tasks.keySet(); + for (String key : keys){ + DownloadTask task = tasks.get(key); + task.setMaxSpeed(maxSpeed); + } + } + @Override public void setMaxTaskNum(int downloadNum) { int oldMaxSize = AriaManager.getInstance(AriaManager.APP).getDownloadConfig().oldMaxTaskNum; int diff = downloadNum - oldMaxSize; diff --git a/Aria/src/main/java/com/arialyy/aria/core/scheduler/AbsSchedulers.java b/Aria/src/main/java/com/arialyy/aria/core/scheduler/AbsSchedulers.java index 2f601917..4356d8a9 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/scheduler/AbsSchedulers.java +++ b/Aria/src/main/java/com/arialyy/aria/core/scheduler/AbsSchedulers.java @@ -84,8 +84,12 @@ public abstract class AbsSchedulers listener = mObservers.get(targetName); if (listener == null) { listener = createListener(targetName); - listener.setListener(obj); - mObservers.put(targetName, listener); + if (listener != null) { + listener.setListener(obj); + mObservers.put(targetName, listener); + } else { + Log.e(TAG, "注册错误,没有【" + targetName + "】观察者"); + } } } diff --git a/app/build.gradle b/app/build.gradle index 71bbd02e..495d1f87 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -42,6 +42,8 @@ dependencies { compile 'com.squareup.okhttp3:okhttp:3.2.0' compile 'com.arialyy.frame:MVVM2:2.2.0' compile project(':Aria') +// compile 'com.arialyy.aria_pre:aria-annotations:0.0.2' +// compile 'com.arialyy.aria_pre:aria-compiler:0.0.2' // compile 'com.arialyy.aria:Aria:3.0.0' // compile 'com.arialyy.downloadutil:aria-annotations:2.1.3' // compile 'com.arialyy.downloadutil:aria-core:2.1.3' diff --git a/app/src/main/assets/aria_config.xml b/app/src/main/assets/aria_config.xml index 522cfa0c..a87be4a9 100644 --- a/app/src/main/assets/aria_config.xml +++ b/app/src/main/assets/aria_config.xml @@ -34,6 +34,9 @@ + + + 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 91fe5a3f..58d1dea6 100644 --- a/app/src/main/java/com/arialyy/simple/download/SingleTaskActivity.java +++ b/app/src/main/java/com/arialyy/simple/download/SingleTaskActivity.java @@ -143,12 +143,36 @@ public class SingleTaskActivity extends BaseActivity { } @Override public boolean onMenuItemClick(MenuItem item) { - if (item.getItemId() == R.id.help) { - String msg = "一些小知识点:\n" - + "1、你可以通过task.getKey().equals(DOWNLOAD_URL)判断是否是当前页面的下载,以防止progress乱跳\n" - + "2、当遇到网络慢的情况时,你可以先使用onPre()更新UI界面,待连接成功时,再在onTaskPre()获取完整的task数据,然后给UI界面设置正确的数据\n" - + "3、你可以在界面初始化时通过Aria.download(this).load(DOWNLOAD_URL).getPercent()等方法快速获取相关任务的一些数据"; - showMsgDialog("tip", msg); + double speed = -1; + String msg = ""; + switch (item.getItemId()) { + case R.id.help: + msg = "一些小知识点:\n" + + "1、你可以通过task.getKey().equals(DOWNLOAD_URL)判断是否是当前页面的下载,以防止progress乱跳\n" + + "2、当遇到网络慢的情况时,你可以先使用onPre()更新UI界面,待连接成功时,再在onTaskPre()获取完整的task数据,然后给UI界面设置正确的数据\n" + + "3、你可以在界面初始化时通过Aria.download(this).load(DOWNLOAD_URL).getPercent()等方法快速获取相关任务的一些数据"; + showMsgDialog("tip", msg); + break; + case R.id.speed_0: + speed = 0.0; + break; + case R.id.speed_128: + speed = 128.0; + break; + case R.id.speed_256: + speed = 256.0; + break; + case R.id.speed_512: + speed = 512.0; + break; + case R.id.speed_1m: + speed = 1024.0; + break; + } + if (speed > -1) { + msg = item.getTitle().toString(); + Aria.download(this).setMaxSpeed(speed); + T.showShort(this, msg); } return true; } diff --git a/app/src/main/res/menu/menu_single_task_activity.xml b/app/src/main/res/menu/menu_single_task_activity.xml index 1015a040..0b852a7c 100644 --- a/app/src/main/res/menu/menu_single_task_activity.xml +++ b/app/src/main/res/menu/menu_single_task_activity.xml @@ -9,4 +9,26 @@ android:title="tip" app:showAsAction="ifRoom" /> + + + + + + + \ No newline at end of file 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 02d9ea17..1745ecc9 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,7 @@ */ package com.arialyy.aria.core; +import android.text.TextUtils; import com.arialyy.aria.core.queue.DownloadTaskQueue; import com.arialyy.aria.util.CommonUtil; import java.io.File; @@ -171,6 +172,9 @@ class Configuration { } else if (type == float.class || type == Float.class) { field.setFloat(this, Float.parseFloat(value)); } else if (type == double.class || type == Double.class) { + if (TextUtils.isEmpty(value)){ + value = "0.0"; + } field.setDouble(this, Double.parseDouble(value)); } else if (type == long.class || type == Long.class) { field.setLong(this, Long.parseLong(value)); @@ -235,10 +239,26 @@ class Configuration { */ int threadNum = 3; + /** + * 设置最大下载速度,单位:kb, 为0表示不限速 + */ + double msxSpeed = 0.0; + public int getIOTimeOut() { return iOTimeOut; } + public double getMsxSpeed() { + return msxSpeed; + } + + public DownloadConfig setMsxSpeed(double msxSpeed) { + this.msxSpeed = msxSpeed; + saveKey("msxSpeed", String.valueOf(msxSpeed)); + DownloadTaskQueue.getInstance().setMaxSpeed(msxSpeed); + return this; + } + public DownloadConfig setIOTimeOut(int iOTimeOut) { this.iOTimeOut = iOTimeOut; saveKey("iOTimeOut", iOTimeOut + ""); diff --git a/build.gradle b/build.gradle index 4249f4a6..62ab4071 100644 --- a/build.gradle +++ b/build.gradle @@ -38,7 +38,7 @@ ext { groupId = 'com.arialyy.aria_pre' uploadName = 'AriaPre' // uploadName = 'AriaPre' - publishVersion = '0.0.2' + publishVersion = '0.0.3' repoName='maven' desc = 'android 下载框架' website = 'https://github.com/AriaLyy/Aria' From 987e54eb25a7c3182d03d2c8f813629587509f4b Mon Sep 17 00:00:00 2001 From: AriaLyy <511455842@qq.com> Date: Fri, 9 Jun 2017 17:58:13 +0800 Subject: [PATCH 2/2] jcenter --- Aria/build.gradle | 7 ++++--- AriaAnnotations/build.gradle | 23 ++++++++++++----------- AriaCompiler/build.gradle | 23 ++++++++++++----------- build.gradle | 3 +-- 4 files changed, 29 insertions(+), 27 deletions(-) diff --git a/Aria/build.gradle b/Aria/build.gradle index af990516..099b9447 100644 --- a/Aria/build.gradle +++ b/Aria/build.gradle @@ -23,8 +23,8 @@ dependencies { compile fileTree(include: ['*.jar'], dir: 'libs') testCompile 'junit:junit:4.12' compile 'com.android.support:appcompat-v7:23.1.1' - compile project(':AriaCompiler') -// compile project(':AriaAnnotations') +// compile project(':AriaCompiler') + compile project(':AriaAnnotations') } //apply from: 'jcenter.gradle' @@ -61,7 +61,8 @@ publish { artifactId = 'aria-core' userOrg = rootProject.userOrg groupId = rootProject.groupId - uploadName = rootProject.uploadName +// uploadName = rootProject.uploadName + uploadName = 'AriaApi' publishVersion = rootProject.publishVersion description = rootProject.description website = rootProject.website diff --git a/AriaAnnotations/build.gradle b/AriaAnnotations/build.gradle index fef879df..c028c11e 100644 --- a/AriaAnnotations/build.gradle +++ b/AriaAnnotations/build.gradle @@ -1,5 +1,5 @@ apply plugin: 'java' -apply plugin: 'bintray-release' +//apply plugin: 'bintray-release' tasks.withType(JavaCompile) { options.encoding = "UTF-8" @@ -12,13 +12,14 @@ dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) } -publish { - artifactId = 'aria-annotations' - userOrg = rootProject.userOrg - groupId = rootProject.groupId - uploadName = rootProject.uploadName - publishVersion = rootProject.publishVersion - description = rootProject.description - website = rootProject.website - licences = rootProject.licences -} \ No newline at end of file +//publish { +// artifactId = 'aria-annotations' +// userOrg = rootProject.userOrg +// groupId = rootProject.groupId +//// uploadName = rootProject.uploadName +// uploadName = 'AriaAnnotations' +// publishVersion = rootProject.publishVersion +// description = rootProject.description +// website = rootProject.website +// licences = rootProject.licences +//} \ No newline at end of file diff --git a/AriaCompiler/build.gradle b/AriaCompiler/build.gradle index 84cc9758..2ac754f2 100644 --- a/AriaCompiler/build.gradle +++ b/AriaCompiler/build.gradle @@ -1,5 +1,5 @@ apply plugin: 'java' -apply plugin: 'bintray-release' +//apply plugin: 'bintray-release' tasks.withType(JavaCompile) { options.encoding = "UTF-8" @@ -15,13 +15,14 @@ dependencies { compile project(':AriaAnnotations') } -publish { - artifactId = 'aria-compiler' - userOrg = rootProject.userOrg - groupId = rootProject.groupId - uploadName = rootProject.uploadName - publishVersion = rootProject.publishVersion - description = rootProject.description - website = rootProject.website - licences = rootProject.licences -} \ No newline at end of file +//publish { +// artifactId = 'aria-compiler' +// userOrg = rootProject.userOrg +// groupId = rootProject.groupId +//// uploadName = rootProject.uploadName +// uploadName = 'AriaCompiler' +// publishVersion = rootProject.publishVersion +// description = rootProject.description +// website = rootProject.website +// licences = rootProject.licences +//} \ No newline at end of file diff --git a/build.gradle b/build.gradle index 62ab4071..b0ae0c5f 100644 --- a/build.gradle +++ b/build.gradle @@ -35,8 +35,7 @@ task clean(type: Delete) { ext { userOrg = 'arialyy' - groupId = 'com.arialyy.aria_pre' - uploadName = 'AriaPre' + groupId = 'com.arialyy.aria' // uploadName = 'AriaPre' publishVersion = '0.0.3' repoName='maven'