AriaLyy 8 years ago
parent 8813baf107
commit da5fbcb462
  1. 20
      Aria/src/main/java/com/arialyy/aria/core/AriaManager.java
  2. 17
      Aria/src/main/java/com/arialyy/aria/core/ConfigHelper.java
  3. 29
      Aria/src/main/java/com/arialyy/aria/core/QueueMod.java
  4. 7
      Aria/src/main/java/com/arialyy/aria/core/command/AbsCmd.java
  5. 19
      Aria/src/main/java/com/arialyy/aria/core/command/StartCmd.java
  6. 6
      Aria/src/main/java/com/arialyy/aria/core/download/DownloadReceiver.java
  7. 4
      Aria/src/main/java/com/arialyy/aria/core/queue/AbsTaskQueue.java
  8. 8
      Aria/src/main/java/com/arialyy/aria/core/queue/ITaskQueue.java
  9. 3
      Aria/src/main/java/com/arialyy/aria/orm/SqlHelper.java
  10. 4
      app/build.gradle
  11. 3
      app/src/main/assets/aria_config.xml
  12. 2
      app/src/main/java/com/arialyy/simple/download/SingleTaskActivity.java
  13. 23
      app/src/main/java/com/arialyy/simple/test/TestMutilTaskSysDownload.java
  14. 17
      aria/src/main/java/com/arialyy/aria/core/Configuration.java

@ -86,6 +86,26 @@ import org.xml.sax.SAXException;
return mReceivers; return mReceivers;
} }
/**
* 设置上传任务的执行队列类型
*
* @param mod {@link com.arialyy.aria.core.QueueMod}
*/
public AriaManager setUploadQueueMod(QueueMod mod) {
mUConfig.setQueueMod(mod.tag);
return this;
}
/**
* 设置下载任务的执行队列类型
*
* @param mod {@link com.arialyy.aria.core.QueueMod}
*/
public AriaManager setDownloadQueueMod(QueueMod mod) {
mDConfig.setQueueMod(mod.tag);
return this;
}
/** /**
* 如果需要在代码中修改下载配置请使用以下方法 * 如果需要在代码中修改下载配置请使用以下方法
* <pre> * <pre>

@ -86,7 +86,24 @@ class ConfigHelper extends DefaultHandler {
case "maxSpeed": case "maxSpeed":
loadMaxSpeed(value); loadMaxSpeed(value);
break; break;
case "queueMod":
loadQueueMod(value);
break;
}
}
}
private void loadQueueMod(String value) {
String mod = "now";
if (!TextUtils.isEmpty(value) && (value.equalsIgnoreCase("now") || value.equalsIgnoreCase(
"wait"))) {
mod = value;
}
if (isDownloadConfig) {
mDownloadConfig.queueMod = mod;
} }
if (isUploadConfig) {
mUploadConfig.queueMod = mod;
} }
} }

@ -0,0 +1,29 @@
package com.arialyy.aria.core;
/**
* Created by Aria.Lao on 2017/6/21.
* 执行队列类型
*/
public enum QueueMod {
/**
* 等待模式如果执行队列已经满了再次使用start命令执行任务时该任务会被添加到缓存队列中
* 当执行队列的任务完成时将自动执行缓存队列中的任务
*/
WAIT("wait"),
/**
* 立刻执行模式
* 如果执行队列已经满了再次使用start命令执行任务时该任务会添加到执行队列队尾而原来执行队列的队首任务会停止
*/
NOW("now");
String tag;
public String getTag() {
return tag;
}
QueueMod(String tag) {
this.tag = tag;
}
}

@ -39,6 +39,11 @@ public abstract class AbsCmd<T extends AbsTaskEntity> implements ICmd {
* 能否执行命令 * 能否执行命令
*/ */
boolean canExeCmd = true; boolean canExeCmd = true;
/**
* 是否是下载任务的命令
* {@code true} 下载任务的命令{@code false} 上传任务的命令
*/
boolean isDownloadCmd = true;
/** /**
* @param targetName 产生任务的对象名 * @param targetName 产生任务的对象名
@ -51,8 +56,10 @@ public abstract class AbsCmd<T extends AbsTaskEntity> implements ICmd {
TAG = CommonUtil.getClassName(this); TAG = CommonUtil.getClassName(this);
if (entity instanceof DownloadTaskEntity) { if (entity instanceof DownloadTaskEntity) {
mQueue = DownloadTaskQueue.getInstance(); mQueue = DownloadTaskQueue.getInstance();
isDownloadCmd = true;
} else if (entity instanceof UploadTaskEntity) { } else if (entity instanceof UploadTaskEntity) {
mQueue = UploadTaskQueue.getInstance(); mQueue = UploadTaskQueue.getInstance();
isDownloadCmd = false;
} }
} }
} }

@ -17,6 +17,9 @@
package com.arialyy.aria.core.command; package com.arialyy.aria.core.command;
import android.text.TextUtils; import android.text.TextUtils;
import android.util.Log;
import com.arialyy.aria.core.AriaManager;
import com.arialyy.aria.core.QueueMod;
import com.arialyy.aria.core.inf.ITask; import com.arialyy.aria.core.inf.ITask;
import com.arialyy.aria.core.inf.AbsTaskEntity; import com.arialyy.aria.core.inf.AbsTaskEntity;
@ -40,7 +43,23 @@ class StartCmd<T extends AbsTaskEntity> extends AbsCmd<T> {
if (!TextUtils.isEmpty(mTargetName)) { if (!TextUtils.isEmpty(mTargetName)) {
task.setTargetName(mTargetName); task.setTargetName(mTargetName);
} }
String mod;
int maxTaskNum;
AriaManager manager = AriaManager.getInstance(AriaManager.APP);
if (isDownloadCmd) {
mod = manager.getDownloadConfig().getQueueMod();
maxTaskNum = manager.getDownloadConfig().getMaxTaskNum();
} else {
mod = manager.getUploadConfig().getQueueMod();
maxTaskNum = manager.getUploadConfig().getMaxTaskNum();
}
if (mod.equals(QueueMod.NOW.getTag())) {
mQueue.startTask(task);
}else if (mod.equals(QueueMod.WAIT.getTag())){
if (mQueue.getExeTaskNum() < maxTaskNum){
mQueue.startTask(task); mQueue.startTask(task);
} }
} }
} }
}
}

@ -18,6 +18,7 @@ package com.arialyy.aria.core.download;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import com.arialyy.aria.core.AriaManager; import com.arialyy.aria.core.AriaManager;
import com.arialyy.aria.core.inf.ICmd; import com.arialyy.aria.core.inf.ICmd;
import com.arialyy.aria.core.inf.IEntity;
import com.arialyy.aria.core.inf.IReceiver; import com.arialyy.aria.core.inf.IReceiver;
import com.arialyy.aria.core.command.CmdFactory; import com.arialyy.aria.core.command.CmdFactory;
import com.arialyy.aria.core.command.AbsCmd; import com.arialyy.aria.core.command.AbsCmd;
@ -26,6 +27,7 @@ import com.arialyy.aria.core.scheduler.ISchedulerListener;
import com.arialyy.aria.orm.DbEntity; import com.arialyy.aria.orm.DbEntity;
import com.arialyy.aria.util.CheckUtil; import com.arialyy.aria.util.CheckUtil;
import com.arialyy.aria.util.CommonUtil; import com.arialyy.aria.util.CommonUtil;
import java.io.File;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
@ -67,6 +69,10 @@ public class DownloadReceiver implements IReceiver<DownloadEntity> {
if (entity == null) { if (entity == null) {
entity = new DownloadEntity(); entity = new DownloadEntity();
} }
File file = new File(entity.getDownloadPath());
if (!file.exists()) {
entity.setState(IEntity.STATE_WAIT);
}
entity.setDownloadUrl(downloadUrl); entity.setDownloadUrl(downloadUrl);
return new DownloadTarget(entity, targetName); return new DownloadTarget(entity, targetName);
} }

@ -36,6 +36,10 @@ abstract class AbsTaskQueue<TASK extends ITask, TASK_ENTITY extends AbsTaskEntit
CachePool<TASK> mCachePool = new CachePool<>(); CachePool<TASK> mCachePool = new CachePool<>();
ExecutePool<TASK> mExecutePool; ExecutePool<TASK> mExecutePool;
@Override public boolean taskIsRunning(String key) {
return mExecutePool.getTask(key) != null;
}
/** /**
* 停止所有任务 * 停止所有任务
*/ */

@ -32,6 +32,14 @@ import com.arialyy.aria.core.upload.UploadTaskEntity;
*/ */
public interface ITaskQueue<TASK extends ITask, TASK_ENTITY extends AbsTaskEntity, ENTITY extends IEntity> { public interface ITaskQueue<TASK extends ITask, TASK_ENTITY extends AbsTaskEntity, ENTITY extends IEntity> {
/**
* 通过key判断任务是否正在执行
*
* @param key 下载链接或上传文件的路径
* @return {@code true} 任务正在运行
*/
boolean taskIsRunning(String key);
/** /**
* 停止所有任务 * 停止所有任务
*/ */

@ -187,6 +187,7 @@ final class SqlHelper extends SQLiteOpenHelper {
print(FIND_DATA, sql); print(FIND_DATA, sql);
Cursor cursor = db.rawQuery(sql, null); Cursor cursor = db.rawQuery(sql, null);
List<T> data = cursor.getCount() > 0 ? newInstanceEntity(clazz, cursor) : null; List<T> data = cursor.getCount() > 0 ? newInstanceEntity(clazz, cursor) : null;
cursor.close();
close(db); close(db);
return data; return data;
} }
@ -215,6 +216,7 @@ final class SqlHelper extends SQLiteOpenHelper {
print(FIND_DATA, sb.toString()); print(FIND_DATA, sb.toString());
Cursor cursor = db.rawQuery(sb.toString(), null); Cursor cursor = db.rawQuery(sb.toString(), null);
List<T> data = cursor.getCount() > 0 ? newInstanceEntity(clazz, cursor) : null; List<T> data = cursor.getCount() > 0 ? newInstanceEntity(clazz, cursor) : null;
cursor.close();
close(db); close(db);
return data; return data;
} }
@ -229,6 +231,7 @@ final class SqlHelper extends SQLiteOpenHelper {
print(FIND_ALL_DATA, sb.toString()); print(FIND_ALL_DATA, sb.toString());
Cursor cursor = db.rawQuery(sb.toString(), null); Cursor cursor = db.rawQuery(sb.toString(), null);
List<T> data = cursor.getCount() > 0 ? newInstanceEntity(clazz, cursor) : null; List<T> data = cursor.getCount() > 0 ? newInstanceEntity(clazz, cursor) : null;
cursor.close();
close(db); close(db);
return data; return data;
} }

@ -42,7 +42,7 @@ dependencies {
compile 'com.squareup.okhttp3:okhttp:3.2.0' compile 'com.squareup.okhttp3:okhttp:3.2.0'
compile 'com.arialyy.frame:MVVM2:2.2.0' compile 'com.arialyy.frame:MVVM2:2.2.0'
compile project(':Aria') compile project(':Aria')
// compile 'com.arialyy.aria:aria-core:3.1.8' // compile 'com.arialyy.aria:aria-core:3.1.9'
// annotationProcessor 'com.arialyy.aria:aria-compiler:3.1.8' // annotationProcessor 'com.arialyy.aria:aria-compiler:3.1.9'
} }

@ -34,6 +34,9 @@
<!--是否需要转换速度单位,转换完成后为:1b/s、1kb/s、1mb/s、1gb/s、1tb/s,如果不需要将返回byte长度--> <!--是否需要转换速度单位,转换完成后为:1b/s、1kb/s、1mb/s、1gb/s、1tb/s,如果不需要将返回byte长度-->
<convertSpeed value="true"/> <convertSpeed value="true"/>
<!--执行队列类型,见com.arialyy.aria.core.QueueMod,默认类型为now-->
<queueMod value="wait"/>
</download> </download>
<upload> <upload>

@ -153,7 +153,7 @@ public class SingleTaskActivity extends BaseActivity<ActivitySingleBinding> {
switch (item.getItemId()) { switch (item.getItemId()) {
case R.id.help: case R.id.help:
msg = "一些小知识点:\n" msg = "一些小知识点:\n"
+ "1、你可以通过task.getKey().equals(DOWNLOAD_URL)判断是否是当前页面的下载,以防止progress乱跳\n" + "1、你可以在注解中增加链接,用于指定被注解的方法只能被特定的下载任务回调,以防止progress乱跳\n"
+ "2、当遇到网络慢的情况时,你可以先使用onPre()更新UI界面,待连接成功时,再在onTaskPre()获取完整的task数据,然后给UI界面设置正确的数据\n" + "2、当遇到网络慢的情况时,你可以先使用onPre()更新UI界面,待连接成功时,再在onTaskPre()获取完整的task数据,然后给UI界面设置正确的数据\n"
+ "3、你可以在界面初始化时通过Aria.download(this).load(DOWNLOAD_URL).getPercent()等方法快速获取相关任务的一些数据"; + "3、你可以在界面初始化时通过Aria.download(this).load(DOWNLOAD_URL).getPercent()等方法快速获取相关任务的一些数据";
showMsgDialog("tip", msg); showMsgDialog("tip", msg);

@ -3,6 +3,7 @@ package com.arialyy.simple.test;
import android.os.Environment; import android.os.Environment;
import android.view.View; import android.view.View;
import com.arialyy.aria.core.Aria; import com.arialyy.aria.core.Aria;
import com.arialyy.aria.core.QueueMod;
import com.arialyy.simple.R; import com.arialyy.simple.R;
import com.arialyy.simple.base.BaseActivity; import com.arialyy.simple.base.BaseActivity;
import com.arialyy.simple.databinding.TestActivityMultiBinding; import com.arialyy.simple.databinding.TestActivityMultiBinding;
@ -54,20 +55,26 @@ public class TestMutilTaskSysDownload extends BaseActivity<TestActivityMultiBind
"M02/3B/A5/oYYBAFaOeaSAdFyoAACaxVxgUJA092.jpg" "M02/3B/A5/oYYBAFaOeaSAdFyoAACaxVxgUJA092.jpg"
}; };
int maxNum = Aria.get(this).getDownloadConfig().getMaxTaskNum(); int maxNum = Aria.get(this).getDownloadConfig().getMaxTaskNum();
Aria.get(this).setDownloadQueueMod(QueueMod.NOW);
for (int i = 0; i < urlArray.length; i++) { for (int i = 0; i < urlArray.length; i++) {
if (i < maxNum) {
Aria.download(this) Aria.download(this)
.load(baseUrl + urlArray[i]) .load(baseUrl + urlArray[i])
.setDownloadPath(Environment.getExternalStorageDirectory() + "/test/" + i + ".jpg") .setDownloadPath(Environment.getExternalStorageDirectory() + "/test/" + i + ".jpg")
//.addHeader("Accept-Encoding", "gzip,deflate,sdcn") //.addHeader("Accept-Encoding", "gzip,deflate,sdcn")
.start(); .start();
} else { //if (i < maxNum) {
Aria.download(this) // Aria.download(this)
.load(baseUrl + urlArray[i]) // .load(baseUrl + urlArray[i])
.setDownloadPath(Environment.getExternalStorageDirectory() + "/test/" + i + ".jpg") // .setDownloadPath(Environment.getExternalStorageDirectory() + "/test/" + i + ".jpg")
//.addHeader("Accept-Encoding", "gzip,deflate,sdcn") // //.addHeader("Accept-Encoding", "gzip,deflate,sdcn")
.add(); // .start();
} //} else {
// Aria.download(this)
// .load(baseUrl + urlArray[i])
// .setDownloadPath(Environment.getExternalStorageDirectory() + "/test/" + i + ".jpg")
// //.addHeader("Accept-Encoding", "gzip,deflate,sdcn")
// .add();
//}
} }
} }
} }

@ -68,6 +68,23 @@ class Configuration {
*/ */
boolean isConvertSpeed = false; boolean isConvertSpeed = false;
/**
* 执行队列类型
*
* @see QueueMod
*/
String queueMod = "now";
public String getQueueMod() {
return queueMod;
}
public BaseConfig setQueueMod(String queueMod) {
this.queueMod = queueMod;
saveKey("queueMod", queueMod);
return this;
}
public boolean isOpenBreadCast() { public boolean isOpenBreadCast() {
return isOpenBreadCast; return isOpenBreadCast;
} }

Loading…
Cancel
Save