代码优化,修复广播的一个bug

pull/330/head
AriaLyy 8 years ago
parent 9b2e2eb231
commit 6202f0d4e3
  1. 2
      .idea/misc.xml
  2. 2
      .idea/modules.xml
  3. 6
      Aria/src/main/java/com/arialyy/aria/core/Aria.java
  4. 2
      Aria/src/main/java/com/arialyy/aria/core/Configuration.java
  5. 1
      Aria/src/main/java/com/arialyy/aria/core/download/DownloadEntity.java
  6. 8
      Aria/src/main/java/com/arialyy/aria/core/download/DownloadTask.java
  7. 7
      Aria/src/main/java/com/arialyy/aria/core/inf/AbsTarget.java
  8. 2
      Aria/src/main/java/com/arialyy/aria/core/inf/IEntity.java
  9. 82
      Aria/src/main/java/com/arialyy/aria/core/queue/AbsTaskQueue.java
  10. 86
      Aria/src/main/java/com/arialyy/aria/core/queue/DownloadTaskQueue.java
  11. 27
      Aria/src/main/java/com/arialyy/aria/core/queue/ITaskQueue.java
  12. 51
      Aria/src/main/java/com/arialyy/aria/core/queue/UploadTaskQueue.java
  13. 2
      Aria/src/main/java/com/arialyy/aria/core/scheduler/DownloadSchedulers.java
  14. 2
      Aria/src/main/java/com/arialyy/aria/core/scheduler/UploadSchedulers.java
  15. 2
      Aria/src/main/java/com/arialyy/aria/core/upload/UploadEntity.java
  16. 6
      Aria/src/main/java/com/arialyy/aria/core/upload/UploadTask.java
  17. 2
      app/src/main/java/com/arialyy/simple/download/DownloadModule.java

@ -27,7 +27,7 @@
</value>
</option>
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" assert-keyword="true" jdk-15="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_7" default="true" assert-keyword="true" jdk-15="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/build/classes" />
</component>
<component name="ProjectType">

@ -2,8 +2,8 @@
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/Aria/Aria.iml" filepath="$PROJECT_DIR$/Aria/Aria.iml" />
<module fileurl="file://$PROJECT_DIR$/Aria.iml" filepath="$PROJECT_DIR$/Aria.iml" />
<module fileurl="file://$PROJECT_DIR$/Aria/Aria.iml" filepath="$PROJECT_DIR$/Aria/Aria.iml" />
<module fileurl="file://$PROJECT_DIR$/Aria/Aria-Aria.iml" filepath="$PROJECT_DIR$/Aria/Aria-Aria.iml" />
<module fileurl="file://$PROJECT_DIR$/AriaPrj.iml" filepath="$PROJECT_DIR$/AriaPrj.iml" />
<module fileurl="file://$PROJECT_DIR$/app/app.iml" filepath="$PROJECT_DIR$/app/app.iml" />

@ -100,7 +100,11 @@ import com.arialyy.aria.core.upload.UploadTask;
/**
* 下载实体
*/
public static final String ENTITY = "DOWNLOAD_ENTITY";
public static final String DOWNLOAD_ENTITY = "DOWNLOAD_ENTITY";
/**
* 上传实体
*/
public static final String UPLOAD_ENTITY = "UPLOAD_ENTITY";
/**
* 位置
*/

@ -86,7 +86,7 @@ class Configuration {
oldMaxTaskNum = this.maxTaskNum;
this.maxTaskNum = maxTaskNum;
saveKey("maxTaskNum", maxTaskNum + "");
DownloadTaskQueue.getInstance().setDownloadNum(maxTaskNum);
DownloadTaskQueue.getInstance().setMaxTaskNum(maxTaskNum);
return this;
}

@ -69,6 +69,7 @@ public class DownloadEntity extends DbEntity implements Parcelable, IEntity {
return failNum;
}
@Override
public void setFailNum(int failNum) {
this.failNum = failNum;
}

@ -208,7 +208,7 @@ public class DownloadTask implements ITask {
// 发送停止下载的广播
Intent intent = CommonUtil.createIntent(mContext.getPackageName(), Aria.ACTION_STOP);
intent.putExtra(Aria.CURRENT_LOCATION, mEntity.getCurrentProgress());
intent.putExtra(Aria.ENTITY, mEntity);
intent.putExtra(Aria.DOWNLOAD_ENTITY, mEntity);
mContext.sendBroadcast(intent);
}
}
@ -227,7 +227,7 @@ public class DownloadTask implements ITask {
}
//发送取消下载的广播
Intent intent = CommonUtil.createIntent(mContext.getPackageName(), Aria.ACTION_CANCEL);
intent.putExtra(Aria.ENTITY, mEntity);
intent.putExtra(Aria.DOWNLOAD_ENTITY, mEntity);
mContext.sendBroadcast(intent);
}
}
@ -285,7 +285,7 @@ public class DownloadTask implements ITask {
this.task = wTask.get();
this.downloadEntity = this.task.getDownloadEntity();
sendIntent = CommonUtil.createIntent(context.getPackageName(), Aria.ACTION_RUNNING);
sendIntent.putExtra(Aria.ENTITY, downloadEntity);
sendIntent.putExtra(Aria.DOWNLOAD_ENTITY, downloadEntity);
final AriaManager manager = AriaManager.getInstance(context);
isOpenBroadCast = manager.getDownloadConfig().isOpenBreadCast();
isConvertSpeed = manager.getDownloadConfig().isConvertSpeed();
@ -406,7 +406,7 @@ public class DownloadTask implements ITask {
downloadEntity.update();
if (!isOpenBroadCast) return;
Intent intent = CommonUtil.createIntent(context.getPackageName(), action);
intent.putExtra(Aria.ENTITY, downloadEntity);
intent.putExtra(Aria.DOWNLOAD_ENTITY, downloadEntity);
if (location != -1) {
intent.putExtra(Aria.CURRENT_LOCATION, location);
}

@ -40,6 +40,13 @@ public class AbsTarget<ENTITY extends IEntity, TASK_ENTITY extends ITaskEntity>
protected TASK_ENTITY taskEntity;
protected String targetName;
/**
* 将该任务优先级提到最高
*/
protected void setToFirst(){
}
/**
* 重定向后新url的key默认为location
*/

@ -60,4 +60,6 @@ public interface IEntity {
@Ignore public static final int STATE_CANCEL = 7;
public int getState();
public void setFailNum(int failNum);
}

@ -16,6 +16,7 @@
package com.arialyy.aria.core.queue;
import android.util.Log;
import com.arialyy.aria.core.inf.IEntity;
import com.arialyy.aria.core.inf.ITask;
import com.arialyy.aria.core.inf.ITaskEntity;
@ -24,8 +25,89 @@ import com.arialyy.aria.core.queue.pool.ExecutePool;
/**
* Created by Aria.Lao on 2017/2/23.
* 任务队列
*/
abstract class AbsTaskQueue<TASK extends ITask, TASK_ENTITY extends ITaskEntity, ENTITY extends IEntity>
implements ITaskQueue<TASK, TASK_ENTITY, ENTITY> {
private final String TAG = "AbsTaskQueue";
CachePool<TASK> mCachePool = new CachePool<>();
ExecutePool<TASK> mExecutePool = null;
/**
* 获取任务执行池
*/
public ExecutePool getExecutePool() {
return mExecutePool;
}
/**
* 获取缓存池
*/
public CachePool getCachePool() {
return mCachePool;
}
/**
* 获取缓存任务数
*
* @return 获取缓存的任务数
*/
@Override public int cachePoolSize() {
return mCachePool.size();
}
/**
* 获取当前运行的任务数
*
* @return 当前正在执行的任务数
*/
@Override public int executePoolSize() {
return mExecutePool.size();
}
@Override public TASK getTask(String url) {
TASK task = mExecutePool.getTask(url);
if (task == null) {
task = mCachePool.getTask(url);
}
return task;
}
@Override public void startTask(TASK task) {
if (mExecutePool.putTask(task)) {
mCachePool.removeTask(task);
task.getEntity().setFailNum(0);
task.start();
}
}
@Override public void stopTask(TASK task) {
if (!task.isRunning()) Log.w(TAG, "停止任务失败,【任务已经停止】");
if (mExecutePool.removeTask(task)) {
task.stop();
} else {
task.stop();
Log.w(TAG, "停止任务失败,【任务已经停止】");
}
}
@Override public void reTryStart(TASK task) {
if (task == null) {
Log.w(TAG, "重试下载失败,task 为null");
return;
}
if (!task.isRunning()) {
task.start();
} else {
Log.w(TAG, "任务没有完全停止,重试下载失败");
}
}
@Override public void cancelTask(TASK task) {
task.cancel();
}
@Override public TASK getNextTask() {
return mCachePool.pollTask();
}
}

@ -23,7 +23,6 @@ import com.arialyy.aria.core.download.DownloadEntity;
import com.arialyy.aria.core.download.DownloadTask;
import com.arialyy.aria.core.download.DownloadTaskEntity;
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;
@ -36,7 +35,6 @@ public class DownloadTaskQueue
private static final String TAG = "DownloadTaskQueue";
private static volatile DownloadTaskQueue INSTANCE = null;
private static final Object LOCK = new Object();
private ExecutePool<DownloadTask> mExecutePool = new ExecutePool<>(true);
public static DownloadTaskQueue getInstance() {
if (INSTANCE == null) {
@ -48,79 +46,10 @@ public class DownloadTaskQueue
}
private DownloadTaskQueue() {
mExecutePool = new ExecutePool<>(true);
}
/**
* 获取任务执行池
*/
public ExecutePool getExecutePool() {
return mExecutePool;
}
/**
* 获取缓存池
*/
public CachePool getCachePool() {
return mCachePool;
}
/**
* 获取当前运行的任务数
*
* @return 当前正在执行的任务数
*/
public int getCurrentTaskNum() {
return mExecutePool.size();
}
/**
* 获取缓存任务数
*
* @return 获取缓存的任务数
*/
public int getCacheTaskNum() {
return mCachePool.size();
}
@Override public void startTask(DownloadTask task) {
if (mExecutePool.putTask(task)) {
mCachePool.removeTask(task);
task.getDownloadEntity().setFailNum(0);
task.start();
}
}
@Override public void stopTask(DownloadTask task) {
if (!task.isRunning()) Log.w(TAG, "停止任务失败,【任务已经停止】");
if (mExecutePool.removeTask(task)) {
task.stop();
} else {
task.stop();
Log.w(TAG, "停止任务失败,【任务已经停止】");
}
}
@Override public void cancelTask(DownloadTask task) {
task.cancel();
}
@Override public void reTryStart(DownloadTask task) {
if (task == null) {
Log.w(TAG, "重试下载失败,task 为null");
return;
}
if (!task.isRunning()) {
task.start();
} else {
Log.w(TAG, "任务没有完全停止,重试下载失败");
}
}
@Override public int size() {
return mExecutePool.size();
}
@Override public void setDownloadNum(int downloadNum) {
@Override public void setMaxTaskNum(int downloadNum) {
//原始长度
int size = AriaManager.getInstance(AriaManager.APP).getDownloadConfig().oldMaxTaskNum;
int diff = downloadNum - size;
@ -164,14 +93,6 @@ public class DownloadTaskQueue
return getTask(entity.getDownloadUrl());
}
@Override public DownloadTask getTask(String url) {
DownloadTask task = mExecutePool.getTask(url);
if (task == null) {
task = mCachePool.getTask(url);
}
return task;
}
@Override public void removeTask(DownloadEntity entity) {
DownloadTask task = mExecutePool.getTask(entity.getDownloadUrl());
if (task != null) {
@ -183,7 +104,4 @@ public class DownloadTaskQueue
}
}
@Override public DownloadTask getNextTask() {
return mCachePool.pollTask();
}
}

@ -37,40 +37,45 @@ public interface ITaskQueue<TASK extends ITask, TASK_ENTITY extends ITaskEntity,
*
* @param task {@link DownloadTask}{@link UploadTask}
*/
public void startTask(TASK task);
void startTask(TASK task);
/**
* 停止任务
*
* @param task {@link DownloadTask}{@link UploadTask}
*/
public void stopTask(TASK task);
void stopTask(TASK task);
/**
* 取消任务
*
* @param task {@link DownloadTask}{@link UploadTask}
*/
public void cancelTask(TASK task);
void cancelTask(TASK task);
/**
* 重试下载
*
* @param task {@link DownloadTask}{@link UploadTask}
*/
public void reTryStart(TASK task);
void reTryStart(TASK task);
/**
* 任务池队列大小
*/
public int size();
int executePoolSize();
/**
* 任务缓存池大小
*/
int cachePoolSize();
/**
* 设置最大任务数
*
* @param newMaxNum 下载任务数
*/
public void setDownloadNum(int newMaxNum);
void setMaxTaskNum(int newMaxNum);
/**
* 创建一个新的任务创建时只是将新任务存储到缓存池
@ -79,7 +84,7 @@ public interface ITaskQueue<TASK extends ITask, TASK_ENTITY extends ITaskEntity,
* @param targetName 生成该任务的对象
* @return {@link DownloadTask}{@link UploadTask}
*/
public TASK createTask(String targetName, TASK_ENTITY entity);
TASK createTask(String targetName, TASK_ENTITY entity);
/**
* 通过工作实体缓存池或任务池搜索下载任务如果缓存池或任务池都没有任务则创建新任务
@ -87,7 +92,7 @@ public interface ITaskQueue<TASK extends ITask, TASK_ENTITY extends ITaskEntity,
* @param entity 工作实体{@link DownloadEntity}{@link UploadEntity}
* @return {@link DownloadTask}{@link UploadTask}
*/
public TASK getTask(ENTITY entity);
TASK getTask(ENTITY entity);
/**
* 通过工作实体缓存池或任务池搜索下载任务如果缓存池或任务池都没有任务则创建新任务
@ -95,19 +100,19 @@ public interface ITaskQueue<TASK extends ITask, TASK_ENTITY extends ITaskEntity,
* @param url 链接地址如果是下载则为下载链接如果是上传为文件保存路径
* @return {@link DownloadTask}{@link UploadTask}
*/
public TASK getTask(String url);
TASK getTask(String url);
/**
* 通过工作实体删除任务
*
* @param entity 工作实体{@link DownloadEntity}{@link UploadEntity}
*/
public void removeTask(ENTITY entity);
void removeTask(ENTITY entity);
/**
* 获取缓存池的下一个任务
*
* @return 下载任务 or null
*/
public TASK getNextTask();
TASK getNextTask();
}

@ -18,7 +18,6 @@ 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;
@ -44,45 +43,8 @@ public class UploadTaskQueue extends AbsTaskQueue<UploadTask, UploadTaskEntity,
return INSTANCE;
}
@Override public void startTask(UploadTask task) {
if (mExecutePool.putTask(task)) {
mCachePool.removeTask(task);
//task.getEntity().setFailNum(0);
task.start();
}
}
@Override public void stopTask(UploadTask task) {
if (!task.isRunning()) Log.w(TAG, "停止任务失败,【任务已经停止】");
if (mExecutePool.removeTask(task)) {
task.stop();
} else {
task.stop();
Log.w(TAG, "停止任务失败,【任务已经停止】");
}
}
@Override public void cancelTask(UploadTask task) {
task.cancel();
}
@Override public void reTryStart(UploadTask task) {
if (task == null) {
Log.w(TAG, "重试下载失败,task 为null");
return;
}
if (!task.isRunning()) {
task.start();
} else {
Log.w(TAG, "任务没有完全停止,重试下载失败");
}
}
@Override public int size() {
return mExecutePool.size();
}
@Override public void setDownloadNum(int downloadNum) {
@Override public void setMaxTaskNum(int newMaxNum) {
}
@ -102,14 +64,6 @@ public class UploadTaskQueue extends AbsTaskQueue<UploadTask, UploadTaskEntity,
return getTask(entity.getFilePath());
}
@Override public UploadTask getTask(String url) {
UploadTask task = mExecutePool.getTask(url);
if (task == null) {
task = mCachePool.getTask(url);
}
return task;
}
@Override public void removeTask(UploadEntity entity) {
UploadTask task = mExecutePool.getTask(entity.getFilePath());
if (task != null) {
@ -121,7 +75,4 @@ public class UploadTaskQueue extends AbsTaskQueue<UploadTask, UploadTaskEntity,
}
}
@Override public UploadTask getNextTask() {
return mCachePool.pollTask();
}
}

@ -86,7 +86,7 @@ public class DownloadSchedulers implements ISchedulers<DownloadTask> {
case STOP:
case CANCEL:
mQueue.removeTask(entity);
if (mQueue.size() < AriaManager.getInstance(AriaManager.APP)
if (mQueue.executePoolSize() < AriaManager.getInstance(AriaManager.APP)
.getUploadConfig()
.getMaxTaskNum()) {
startNextTask();

@ -171,7 +171,7 @@ public class UploadSchedulers implements ISchedulers<UploadTask> {
case STOP:
case CANCEL:
mQueue.removeTask(entity);
if (mQueue.size() < AriaManager.getInstance(AriaManager.APP)
if (mQueue.executePoolSize() < AriaManager.getInstance(AriaManager.APP)
.getUploadConfig()
.getMaxTaskNum()) {
startNextTask();

@ -81,7 +81,7 @@ public class UploadEntity extends DbEntity implements IEntity, Parcelable {
return failNum;
}
public void setFailNum(int failNum) {
@Override public void setFailNum(int failNum) {
this.failNum = failNum;
}

@ -99,7 +99,7 @@ public class UploadTask implements ITask {
}
//发送取消下载的广播
Intent intent = CommonUtil.createIntent(AriaManager.APP.getPackageName(), Aria.ACTION_CANCEL);
intent.putExtra(Aria.ENTITY, mUploadEntity);
intent.putExtra(Aria.UPLOAD_ENTITY, mUploadEntity);
AriaManager.APP.sendBroadcast(intent);
}
}
@ -201,7 +201,7 @@ public class UploadTask implements ITask {
this.task = new WeakReference<>(task);
uploadEntity = this.task.get().getUploadEntity();
sendIntent = CommonUtil.createIntent(AriaManager.APP.getPackageName(), Aria.ACTION_RUNNING);
sendIntent.putExtra(Aria.ENTITY, uploadEntity);
sendIntent.putExtra(Aria.UPLOAD_ENTITY, uploadEntity);
context = AriaManager.APP;
final AriaManager manager = AriaManager.getInstance(context);
isOpenBroadCast = manager.getUploadConfig().isOpenBreadCast();
@ -301,7 +301,7 @@ public class UploadTask implements ITask {
uploadEntity.update();
if (!isOpenBroadCast) return;
Intent intent = CommonUtil.createIntent(context.getPackageName(), action);
intent.putExtra(Aria.ENTITY, uploadEntity);
intent.putExtra(Aria.UPLOAD_ENTITY, uploadEntity);
if (location != -1) {
intent.putExtra(Aria.CURRENT_LOCATION, location);
}

@ -176,7 +176,7 @@ public class DownloadModule extends BaseModule {
String action = intent.getAction();
switch (action) {
case Aria.ACTION_POST_PRE:
DownloadEntity entity = intent.getParcelableExtra(Aria.ENTITY);
DownloadEntity entity = intent.getParcelableExtra(Aria.DOWNLOAD_ENTITY);
len = entity.getFileSize();
L.d(TAG, "download pre");
handler.obtainMessage(SingleTaskActivity.DOWNLOAD_PRE, len).sendToTarget();

Loading…
Cancel
Save