pull/330/head
AriaLyy 8 years ago
parent 431fc35deb
commit 2566f37403
  1. 4
      Aria/build.gradle
  2. 13
      Aria/src/main/java/com/arialyy/aria/core/ConfigHelper.java
  3. 10
      Aria/src/main/java/com/arialyy/aria/core/download/DownloadReceiver.java
  4. 9
      Aria/src/main/java/com/arialyy/aria/core/download/DownloadTask.java
  5. 9
      Aria/src/main/java/com/arialyy/aria/core/download/DownloadUtil.java
  6. 5
      Aria/src/main/java/com/arialyy/aria/core/download/IDownloadUtil.java
  7. 15
      Aria/src/main/java/com/arialyy/aria/core/download/SingleThreadTask.java
  8. 9
      Aria/src/main/java/com/arialyy/aria/core/queue/DownloadTaskQueue.java
  9. 8
      Aria/src/main/java/com/arialyy/aria/core/scheduler/AbsSchedulers.java
  10. 2
      app/build.gradle
  11. 3
      app/src/main/assets/aria_config.xml
  12. 36
      app/src/main/java/com/arialyy/simple/download/SingleTaskActivity.java
  13. 22
      app/src/main/res/menu/menu_single_task_activity.xml
  14. 20
      aria/src/main/java/com/arialyy/aria/core/Configuration.java
  15. 2
      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'

@ -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) {

@ -39,6 +39,16 @@ public class DownloadReceiver implements IReceiver<DownloadEntity> {
public Object obj;
public ISchedulerListener<DownloadTask> listener;
/**
* 设置最大下载速度单位kb
*
* @param maxSpeed 为0表示不限速
*/
public void setMaxSpeed(double maxSpeed) {
AriaManager.getInstance(AriaManager.APP).getDownloadConfig().setMsxSpeed(maxSpeed);
}
/**
* {@link #load(String)}请使用该方法
*/

@ -88,6 +88,15 @@ public class DownloadTask extends AbsTask<DownloadTaskEntity, DownloadEntity> {
stop(true);
}
/**
* 设置最大下载速度单位kb
*
* @param maxSpeed 为0表示不限速
*/
public void setMaxSpeed(double maxSpeed) {
mUtil.setMaxSpeed(maxSpeed);
}
/**
* 开始下载
*/

@ -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);
}
}
}
/**
* 取消下载
*/

@ -63,4 +63,9 @@ interface IDownloadUtil {
* 删除temp文件
*/
public void delTempFile();
/**
* 设置最大下载速度
*/
public void setMaxSpeed(double maxSpeed);
}

@ -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);

@ -91,6 +91,15 @@ public class DownloadTaskQueue
}
}
public void setMaxSpeed(double maxSpeed){
Map<String, DownloadTask> tasks = mExecutePool.getAllTask();
Set<String> 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;

@ -84,8 +84,12 @@ public abstract class AbsSchedulers<TASK_ENTITY extends AbsTaskEntity, ENTITY ex
AbsSchedulerListener<TASK> 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 + "】观察者");
}
}
}

@ -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'

@ -34,6 +34,9 @@
<!--是否需要转换速度单位,转换完成后为:1b/s、1kb/s、1mb/s、1gb/s、1tb/s,如果不需要将返回byte长度-->
<convertSpeed value="true"/>
<!--设置最大下载速度,单位:kb, 为0表示不限速-->
<maxSpeed value="0"/>
</download>
<upload>

@ -143,12 +143,36 @@ public class SingleTaskActivity extends BaseActivity<ActivitySingleBinding> {
}
@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;
}

@ -9,4 +9,26 @@
android:title="tip"
app:showAsAction="ifRoom"
/>
<item
android:id="@+id/speed_0"
android:title="不限速"
/>
<item
android:id="@+id/speed_128"
android:title="最大速度(128kb)"
/>
<item
android:id="@+id/speed_256"
android:title="最大速度(256kb)"
/>
<item
android:id="@+id/speed_512"
android:title="最大速度(512kb)"
/>
<item
android:id="@+id/speed_1m"
android:title="最大速度(1mb)"
/>
</menu>

@ -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 + "");

@ -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'

Loading…
Cancel
Save