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'