v_2.0 开发,运行池编写

pull/2/head
lyy 8 years ago
parent aeb1820079
commit 78b76f5399
  1. 33
      downloadutil/src/main/java/com/arialyy/downloadutil/core/DownloadTarget.java
  2. 38
      downloadutil/src/main/java/com/arialyy/downloadutil/core/Task.java
  3. 31
      downloadutil/src/main/java/com/arialyy/downloadutil/core/inf/IDownloader.java
  4. 7
      downloadutil/src/main/java/com/arialyy/downloadutil/core/inf/IPool.java
  5. 34
      downloadutil/src/main/java/com/arialyy/downloadutil/core/inf/ITask.java
  6. 5
      downloadutil/src/main/java/com/arialyy/downloadutil/core/pool/CachePool.java
  7. 5
      downloadutil/src/main/java/com/arialyy/downloadutil/core/pool/ExecutePool.java

@ -0,0 +1,33 @@
package com.arialyy.downloadutil.core;
import com.arialyy.downloadutil.core.inf.IDownloader;
import com.arialyy.downloadutil.core.inf.ITask;
import com.arialyy.downloadutil.core.pool.CachePool;
import com.arialyy.downloadutil.core.pool.ExecutePool;
/**
* Created by lyy on 2016/8/16.
* 任务下载器提供抽象的方法供具体的实现类操作
*/
public abstract class DownloadTarget implements IDownloader, ITask {
protected CachePool mCachePool = CachePool.getInstance();
protected ExecutePool mExecutePool = ExecutePool.getInstance();
/**
* 获取当前运行的任务数
*
* @return 当前正在执行的任务数
*/
public int getCurrentTaskNum() {
return mExecutePool.size();
}
/**
* 获取缓存任务数
*
* @return 获取缓存的任务数
*/
public int getCacheTaskNum() {
return mCachePool.size();
}
}

@ -17,7 +17,27 @@ import java.net.HttpURLConnection;
* 下载任务类 * 下载任务类
*/ */
public class Task { public class Task {
public static final String TAG = "Task"; public static final String TAG = "Task";
/**
* 任务开始
*/
public static final int START = 1;
/**
* 任务停止
*/
public static final int STOP = 2;
/**
* 任务失败
*/
public static final int FAILE = 3;
/**
* 任务取消
*/
public static final int CANCEL = 4;
/**
* 任务完成
*/
public static final int COMPLETE = 5;
DownloadEntity downloadEntity; DownloadEntity downloadEntity;
IDownloadListener listener; IDownloadListener listener;
Handler outHandler; Handler outHandler;
@ -101,6 +121,7 @@ public class Task {
public void onStart(long startLocation) { public void onStart(long startLocation) {
super.onStart(startLocation); super.onStart(startLocation);
downloadEntity.setState(DownloadEntity.STATE_DOWNLOAD_ING); downloadEntity.setState(DownloadEntity.STATE_DOWNLOAD_ING);
sendInState2Target(START);
sendIntent(DownloadManager.ACTION_START, startLocation); sendIntent(DownloadManager.ACTION_START, startLocation);
} }
@ -117,6 +138,7 @@ public class Task {
public void onStop(long stopLocation) { public void onStop(long stopLocation) {
super.onStop(stopLocation); super.onStop(stopLocation);
downloadEntity.setState(DownloadEntity.STATE_STOP); downloadEntity.setState(DownloadEntity.STATE_STOP);
sendInState2Target(STOP);
sendIntent(DownloadManager.ACTION_STOP, stopLocation); sendIntent(DownloadManager.ACTION_STOP, stopLocation);
} }
@ -124,6 +146,7 @@ public class Task {
public void onCancel() { public void onCancel() {
super.onCancel(); super.onCancel();
downloadEntity.setState(DownloadEntity.STATE_CANCEL); downloadEntity.setState(DownloadEntity.STATE_CANCEL);
sendInState2Target(CANCEL);
sendIntent(DownloadManager.ACTION_CANCEL, -1); sendIntent(DownloadManager.ACTION_CANCEL, -1);
downloadEntity.deleteData(); downloadEntity.deleteData();
} }
@ -133,6 +156,7 @@ public class Task {
super.onComplete(); super.onComplete();
downloadEntity.setState(DownloadEntity.STATE_COMPLETE); downloadEntity.setState(DownloadEntity.STATE_COMPLETE);
downloadEntity.setDownloadComplete(true); downloadEntity.setDownloadComplete(true);
sendInState2Target(COMPLETE);
sendIntent(DownloadManager.ACTION_COMPLETE, -1); sendIntent(DownloadManager.ACTION_COMPLETE, -1);
} }
@ -140,9 +164,21 @@ public class Task {
public void onFail() { public void onFail() {
super.onFail(); super.onFail();
downloadEntity.setState(DownloadEntity.STATE_FAIL); downloadEntity.setState(DownloadEntity.STATE_FAIL);
sendInState2Target(FAILE);
sendIntent(DownloadManager.ACTION_FAIL, -1); sendIntent(DownloadManager.ACTION_FAIL, -1);
} }
/**
* 将任务状态发送给下载器
*
* @param state {@link Task#START}
*/
private void sendInState2Target(int state) {
if (outHandler != null) {
outHandler.obtainMessage(state, downloadEntity).sendToTarget();
}
}
private void sendIntent(String action, long location) { private void sendIntent(String action, long location) {
downloadEntity.save(); downloadEntity.save();
Intent intent = new Intent(); Intent intent = new Intent();

@ -0,0 +1,31 @@
package com.arialyy.downloadutil.core.inf;
import com.arialyy.downloadutil.core.Task;
/**
* Created by lyy on 2016/8/16.
* 下载功能接口
*/
public interface IDownloader {
/**
* 开始任务
*
* @param task {@link Task}
*/
public void startTask(Task task);
/**
* 停止任务
*
* @param task {@link Task}
*/
public void stopTask(Task task);
/**
* 取消任务
*
* @param task {@link Task}
*/
public void cancelTask(Task task);
}

@ -36,4 +36,11 @@ public interface IPool {
* @return true:移除成功 * @return true:移除成功
*/ */
public boolean removeTask(Task task); public boolean removeTask(Task task);
/**
* 池子大小
*
* @return 返回缓存池或者当前任务池大小
*/
public int size();
} }

@ -0,0 +1,34 @@
package com.arialyy.downloadutil.core.inf;
import com.arialyy.downloadutil.core.Task;
/**
* Created by lyy on 2016/8/16.
* 任务功能接口
*/
public interface ITask {
/**
* 创建一个新的下载任务
*
* @param downloadUrl 下载链接
* @param downloadPath 保存路径
*/
public void createTask(String downloadUrl, String downloadPath);
/**
* 通过下载链接从缓存池或任务池搜索下载任务
*
* @param downloadUrl 下载链接
* @return {@link Task}
*/
public Task searchTask(String downloadUrl);
/**
* 通过下载链接搜索下载任务
*
* @param downloadUrl 下载链接
* @return {@link com.arialyy.downloadutil.entity.DownloadEntity#STATE_FAIL}
*/
public int getTaskState(String downloadUrl);
}

@ -96,4 +96,9 @@ public class CachePool implements IPool {
} }
} }
} }
@Override
public int size() {
return mCacheQueue.size();
}
} }

@ -141,4 +141,9 @@ public class ExecutePool implements IPool {
} }
} }
@Override
public int size() {
return mExecuteQueue.size();
}
} }

Loading…
Cancel
Save