逻辑重构
This commit is contained in:
@ -0,0 +1,59 @@
|
||||
/*
|
||||
* Copyright (C) 2016 AriaLyy(DownloadUtil)
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.arialyy.downloadutil.core;
|
||||
|
||||
import android.content.Context;
|
||||
import com.arialyy.downloadutil.core.command.CmdFactory;
|
||||
import com.arialyy.downloadutil.core.command.IDownloadCmd;
|
||||
import com.arialyy.downloadutil.core.scheduler.OnSchedulerListener;
|
||||
import com.arialyy.downloadutil.core.task.Task;
|
||||
import com.arialyy.downloadutil.util.CommonUtil;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* AM 接收器
|
||||
*/
|
||||
public class AMReceiver {
|
||||
Context context;
|
||||
OnSchedulerListener listener;
|
||||
DownloadEntity entity;
|
||||
DownloadManager manager = DownloadManager.getInstance();
|
||||
|
||||
public AMTarget load(DownloadEntity entity) {
|
||||
this.entity = entity;
|
||||
return new AMTarget(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加调度器回调
|
||||
*/
|
||||
public AMReceiver addSchedulerListener(OnSchedulerListener listener) {
|
||||
this.listener = listener;
|
||||
manager.getTaskQueue().getDownloadSchedulers().addSchedulerListener(context, listener);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 移除回调
|
||||
*/
|
||||
public AMReceiver removeSchedulerListener() {
|
||||
if (listener != null) {
|
||||
manager.getTaskQueue().getDownloadSchedulers().removeSchedulerListener(listener);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
}
|
@ -1,16 +1,87 @@
|
||||
package com.arialyy.downloadutil.core;
|
||||
|
||||
import android.content.Context;
|
||||
import com.arialyy.downloadutil.core.command.CmdFactory;
|
||||
import com.arialyy.downloadutil.core.command.IDownloadCmd;
|
||||
import com.arialyy.downloadutil.core.scheduler.OnSchedulerListener;
|
||||
import com.arialyy.downloadutil.core.task.Task;
|
||||
import com.arialyy.downloadutil.util.CommonUtil;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Created by utstarcom on 2016/12/5.
|
||||
*/
|
||||
public class AMTarget {
|
||||
Context context;
|
||||
OnSchedulerListener listener;
|
||||
DownloadEntity entity;
|
||||
private AMReceiver receiver;
|
||||
|
||||
public AMTarget(AMReceiver receiver){
|
||||
this.receiver = receiver;
|
||||
}
|
||||
|
||||
//public DownloadManager load(DownloadEntity entity){
|
||||
// this.entity = entity;
|
||||
//}
|
||||
/**
|
||||
* 开始下载
|
||||
*/
|
||||
public void start() {
|
||||
List<IDownloadCmd> cmds = new ArrayList<>();
|
||||
cmds.add(CommonUtil.createCmd(receiver.entity, CmdFactory.TASK_CREATE));
|
||||
cmds.add(CommonUtil.createCmd(receiver.entity, CmdFactory.TASK_START));
|
||||
receiver.manager.setCmds(cmds).exe();
|
||||
}
|
||||
|
||||
}
|
||||
/**
|
||||
* 停止下载
|
||||
*/
|
||||
public void stop() {
|
||||
receiver.manager.setCmd(CommonUtil.createCmd(receiver.entity, CmdFactory.TASK_STOP)).exe();
|
||||
}
|
||||
|
||||
/**
|
||||
* 恢复下载
|
||||
*/
|
||||
public void resume() {
|
||||
receiver.manager.setCmd(CommonUtil.createCmd(receiver.entity, CmdFactory.TASK_START)).exe();
|
||||
}
|
||||
|
||||
/**
|
||||
* 取消下载
|
||||
*/
|
||||
public void cancel() {
|
||||
receiver.manager.setCmd(CommonUtil.createCmd(receiver.entity, CmdFactory.TASK_CANCEL)).exe();
|
||||
}
|
||||
|
||||
public static class SimpleSchedulerListener implements OnSchedulerListener {
|
||||
|
||||
@Override public void onTaskPre(Task task) {
|
||||
|
||||
}
|
||||
|
||||
@Override public void onTaskResume(Task task) {
|
||||
|
||||
}
|
||||
|
||||
@Override public void onTaskStart(Task task) {
|
||||
|
||||
}
|
||||
|
||||
@Override public void onTaskStop(Task task) {
|
||||
|
||||
}
|
||||
|
||||
@Override public void onTaskCancel(Task task) {
|
||||
|
||||
}
|
||||
|
||||
@Override public void onTaskFail(Task task) {
|
||||
|
||||
}
|
||||
|
||||
@Override public void onTaskComplete(Task task) {
|
||||
|
||||
}
|
||||
|
||||
@Override public void onTaskRunning(Task task) {
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -19,14 +19,10 @@ package com.arialyy.downloadutil.core;
|
||||
import android.annotation.TargetApi;
|
||||
import android.content.Context;
|
||||
import android.os.Build;
|
||||
import com.arialyy.downloadutil.core.command.CmdFactory;
|
||||
import com.arialyy.downloadutil.core.command.IDownloadCmd;
|
||||
import com.arialyy.downloadutil.core.scheduler.OnSchedulerListener;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Created by lyy on 2016/12/1.
|
||||
* Aria启动,管理全局任务
|
||||
*/
|
||||
@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1)
|
||||
public class Aria {
|
||||
@ -38,64 +34,11 @@ public class Aria {
|
||||
//mDownloadManager = DownloadManager.getInstance();
|
||||
}
|
||||
|
||||
private Aria(Context context) {
|
||||
mDownloadManager = DownloadManager.init(context);
|
||||
public static AMReceiver whit(Context context) {
|
||||
return AriaManager.getInstance(context).get(context);
|
||||
}
|
||||
|
||||
public static AMTarget whit(Context context) {
|
||||
AMTarget target = AriaManager.getInstance(context).get(context);
|
||||
return target;
|
||||
}
|
||||
|
||||
/**
|
||||
* 开始下载
|
||||
*/
|
||||
public Aria start(DownloadEntity entity) {
|
||||
List<IDownloadCmd> cmds = new ArrayList<>();
|
||||
cmds.add(createCmd(entity, CmdFactory.TASK_CREATE));
|
||||
cmds.add(createCmd(entity, CmdFactory.TASK_START));
|
||||
mDownloadManager.setCmds(cmds).exe();
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 停止下载
|
||||
*/
|
||||
public void stop(DownloadEntity entity) {
|
||||
mDownloadManager.setCmd(createCmd(entity, CmdFactory.TASK_STOP)).exe();
|
||||
}
|
||||
|
||||
/**
|
||||
* 恢复下载
|
||||
*/
|
||||
public void resume(DownloadEntity entity) {
|
||||
mDownloadManager.setCmd(createCmd(entity, CmdFactory.TASK_START)).exe();
|
||||
}
|
||||
|
||||
/**
|
||||
* 取消下载
|
||||
*/
|
||||
public void cancel(DownloadEntity entity) {
|
||||
mDownloadManager.setCmd(createCmd(entity, CmdFactory.TASK_CANCEL)).exe();
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加调度器回调
|
||||
*/
|
||||
public Aria addSchedulerListener(Context context, OnSchedulerListener listener) {
|
||||
//mDownloadManager.getTaskQueue().getDownloadSchedulers().addSchedulerListener(listener);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 移除回调
|
||||
*/
|
||||
public Aria removeSchedulerListener(Context context) {
|
||||
//mDownloadManager.getTaskQueue().getDownloadSchedulers().removeSchedulerListener(listener);
|
||||
return this;
|
||||
}
|
||||
|
||||
private IDownloadCmd createCmd(DownloadEntity entity, int cmd) {
|
||||
return CmdFactory.getInstance().createCmd(entity, cmd);
|
||||
public static AriaManager get(Context context){
|
||||
return AriaManager.getInstance(context);
|
||||
}
|
||||
}
|
||||
|
@ -3,28 +3,36 @@ package com.arialyy.downloadutil.core;
|
||||
import android.annotation.TargetApi;
|
||||
import android.app.Activity;
|
||||
import android.app.Application;
|
||||
import android.app.Service;
|
||||
import android.content.Context;
|
||||
import android.os.Build;
|
||||
import android.os.Bundle;
|
||||
import com.arialyy.downloadutil.core.command.CmdFactory;
|
||||
import com.arialyy.downloadutil.core.command.IDownloadCmd;
|
||||
import com.arialyy.downloadutil.util.CommonUtil;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* Created by lyy on 2016/12/1.
|
||||
* Aria管理器,任务操作在这里执行
|
||||
*/
|
||||
@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH) public class AriaManager {
|
||||
private static final Object LOCK = new Object();
|
||||
private static volatile AriaManager INSTANCE = null;
|
||||
private Map<String, AMTarget> mTargets = new HashMap<>();
|
||||
private static final Object LOCK = new Object();
|
||||
private static volatile AriaManager INSTANCE = null;
|
||||
private Map<String, AMReceiver> mTargets = new HashMap<>();
|
||||
private DownloadManager mManager;
|
||||
private LifeCallback mLifeCallback;
|
||||
private LifeCallback mLifeCallback;
|
||||
|
||||
private AriaManager(Context context) {
|
||||
regAppLifeCallback(context);
|
||||
mManager = DownloadManager.init(context);
|
||||
}
|
||||
|
||||
public static AriaManager getInstance(Context context) {
|
||||
static AriaManager getInstance(Context context) {
|
||||
if (INSTANCE == null) {
|
||||
synchronized (LOCK) {
|
||||
INSTANCE = new AriaManager(context);
|
||||
@ -33,21 +41,59 @@ import java.util.Map;
|
||||
return INSTANCE;
|
||||
}
|
||||
|
||||
public AMTarget get(Context context){
|
||||
AMReceiver get(Context context) {
|
||||
return getTarget(context);
|
||||
}
|
||||
|
||||
private void putTarget(Context context) {
|
||||
String clsName = context.getClass().getName();
|
||||
AMTarget target = mTargets.get(clsName);
|
||||
if (target == null) {
|
||||
target = new AMTarget();
|
||||
mTargets.put(clsName, target);
|
||||
/**
|
||||
* 停止所有正在执行的任务
|
||||
*/
|
||||
public void stopAllTask() {
|
||||
List<DownloadEntity> allEntity = mManager.getAllDownloadEntity();
|
||||
List<IDownloadCmd> stopCmds = new ArrayList<>();
|
||||
for (DownloadEntity entity : allEntity) {
|
||||
if (entity.getState() == DownloadEntity.STATE_DOWNLOAD_ING) {
|
||||
stopCmds.add(CommonUtil.createCmd(entity, CmdFactory.TASK_STOP));
|
||||
}
|
||||
}
|
||||
mManager.setCmds(stopCmds).exe();
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除所有任务
|
||||
*/
|
||||
public void cancelAllTask() {
|
||||
List<DownloadEntity> allEntity = mManager.getAllDownloadEntity();
|
||||
List<IDownloadCmd> cancelCmds = new ArrayList<>();
|
||||
for (DownloadEntity entity : allEntity) {
|
||||
cancelCmds.add(CommonUtil.createCmd(entity, CmdFactory.TASK_CANCEL));
|
||||
}
|
||||
mManager.setCmds(cancelCmds).exe();
|
||||
Set<String> keys = mTargets.keySet();
|
||||
for (String key : keys) {
|
||||
AMReceiver target = mTargets.get(key);
|
||||
target.removeSchedulerListener();
|
||||
mTargets.remove(key);
|
||||
}
|
||||
}
|
||||
|
||||
private AMTarget getTarget(Context context) {
|
||||
return mTargets.get(context.getClass().getName());
|
||||
private AMReceiver putTarget(Context context) {
|
||||
String clsName = context.getClass().getName();
|
||||
AMReceiver target = mTargets.get(clsName);
|
||||
if (target == null) {
|
||||
target = new AMReceiver();
|
||||
target.context = context;
|
||||
mTargets.put(clsName, target);
|
||||
}
|
||||
return target;
|
||||
}
|
||||
|
||||
private AMReceiver getTarget(Context context) {
|
||||
AMReceiver target = mTargets.get(context.getClass().getName());
|
||||
if (target == null) {
|
||||
target = putTarget(context);
|
||||
}
|
||||
return target;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -91,7 +137,18 @@ import java.util.Map;
|
||||
}
|
||||
|
||||
@Override public void onActivityDestroyed(Activity activity) {
|
||||
|
||||
Set<String> keys = mTargets.keySet();
|
||||
for (String key : keys) {
|
||||
if (key.equals(activity.getClass().getName())) {
|
||||
AMReceiver target = mTargets.get(key);
|
||||
if (target.context != null) {
|
||||
if (target.context instanceof Application || target.context instanceof Service) break;
|
||||
target.removeSchedulerListener();
|
||||
mTargets.remove(key);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -86,7 +86,6 @@ public class DownloadManager {
|
||||
private List<IDownloadCmd> mCommands = new ArrayList<>();
|
||||
private Context mContext;
|
||||
private ITaskQueue mTaskQueue;
|
||||
private DownloadEntity mTempDEntity;
|
||||
|
||||
private DownloadManager() {
|
||||
|
||||
|
@ -19,8 +19,6 @@ package com.arialyy.downloadutil.core.scheduler;
|
||||
import android.content.Context;
|
||||
import android.os.Message;
|
||||
import android.util.Log;
|
||||
import android.util.SparseArray;
|
||||
import android.util.SparseIntArray;
|
||||
import com.arialyy.downloadutil.core.DownloadEntity;
|
||||
import com.arialyy.downloadutil.core.queue.ITaskQueue;
|
||||
import com.arialyy.downloadutil.core.task.Task;
|
||||
@ -28,13 +26,17 @@ import com.arialyy.downloadutil.core.queue.pool.ExecutePool;
|
||||
import com.arialyy.downloadutil.core.queue.DownloadTaskQueue;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
/**
|
||||
* Created by lyy on 2016/8/16.
|
||||
* 任务下载器,提供抽象的方法供具体的实现类操作
|
||||
*/
|
||||
public class DownloadSchedulers implements IDownloadSchedulers {
|
||||
/**
|
||||
* 任务预加载
|
||||
*/
|
||||
public static final int PRE = 0;
|
||||
/**
|
||||
* 任务开始
|
||||
*/
|
||||
@ -59,6 +61,10 @@ public class DownloadSchedulers implements IDownloadSchedulers {
|
||||
* 下载中
|
||||
*/
|
||||
public static final int RUNNING = 6;
|
||||
/**
|
||||
* 恢复下载
|
||||
*/
|
||||
public static final int RESUME = 7;
|
||||
private static final String TAG = "DownloadSchedulers";
|
||||
private static final Object LOCK = new Object();
|
||||
private static volatile DownloadSchedulers INSTANCE = null;
|
||||
@ -75,8 +81,7 @@ public class DownloadSchedulers implements IDownloadSchedulers {
|
||||
/**
|
||||
* 下载器任务监听
|
||||
*/
|
||||
OnSchedulerListener mSchedulerListener;
|
||||
Map<Integer, OnSchedulerListener> mSchedulerListeners = new HashMap<>();
|
||||
Map<Integer, OnSchedulerListener> mSchedulerListeners = new ConcurrentHashMap<>();
|
||||
ITaskQueue mQueue;
|
||||
|
||||
public DownloadSchedulers(ITaskQueue downloadTaskQueue) {
|
||||
@ -134,6 +139,10 @@ public class DownloadSchedulers implements IDownloadSchedulers {
|
||||
private void callback(int state, DownloadEntity entity, OnSchedulerListener listener) {
|
||||
if (listener != null) {
|
||||
Task task = mQueue.getTask(entity);
|
||||
if (task == null) {
|
||||
Log.e(TAG, "队列中没有下载链接【" + entity.getDownloadUrl() + "】的任务");
|
||||
return;
|
||||
}
|
||||
switch (state) {
|
||||
case RUNNING:
|
||||
listener.onTaskRunning(task);
|
||||
@ -144,6 +153,12 @@ public class DownloadSchedulers implements IDownloadSchedulers {
|
||||
case STOP:
|
||||
listener.onTaskStop(task);
|
||||
break;
|
||||
case RESUME:
|
||||
listener.onTaskResume(task);
|
||||
break;
|
||||
case PRE:
|
||||
listener.onTaskPre(task);
|
||||
break;
|
||||
case CANCEL:
|
||||
listener.onTaskCancel(task);
|
||||
removeSchedulerListener(listener);
|
||||
@ -191,7 +206,8 @@ public class DownloadSchedulers implements IDownloadSchedulers {
|
||||
}
|
||||
}
|
||||
|
||||
@Override public void addSchedulerListener(Context context, OnSchedulerListener schedulerListener) {
|
||||
@Override
|
||||
public void addSchedulerListener(Context context, OnSchedulerListener schedulerListener) {
|
||||
mSchedulerListeners.put(schedulerListener.hashCode(), schedulerListener);
|
||||
}
|
||||
|
||||
|
@ -6,6 +6,16 @@ import com.arialyy.downloadutil.core.task.Task;
|
||||
* Target处理任务监听
|
||||
*/
|
||||
public interface OnSchedulerListener {
|
||||
/**
|
||||
* 任务预加载
|
||||
*/
|
||||
public void onTaskPre(Task task);
|
||||
|
||||
/**
|
||||
* 任务恢复下载
|
||||
*/
|
||||
public void onTaskResume(Task task);
|
||||
|
||||
/**
|
||||
* 任务开始
|
||||
*/
|
||||
|
@ -14,7 +14,6 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
|
||||
package com.arialyy.downloadutil.core.task;
|
||||
|
||||
import android.content.Context;
|
||||
@ -34,11 +33,11 @@ import com.arialyy.downloadutil.core.scheduler.DownloadSchedulers;
|
||||
public class Task {
|
||||
public static final String TAG = "Task";
|
||||
|
||||
private DownloadEntity mEntity;
|
||||
private DownloadEntity mEntity;
|
||||
private IDownloadListener mListener;
|
||||
private Handler mOutHandler;
|
||||
private Context mContext;
|
||||
private IDownloadUtil mUtil;
|
||||
private Handler mOutHandler;
|
||||
private Context mContext;
|
||||
private IDownloadUtil mUtil;
|
||||
|
||||
private Task(Context context, DownloadEntity entity) {
|
||||
mContext = context.getApplicationContext();
|
||||
@ -129,7 +128,7 @@ public class Task {
|
||||
private Intent createIntent(String action) {
|
||||
Uri.Builder builder = new Uri.Builder();
|
||||
builder.scheme(mContext.getPackageName());
|
||||
Uri uri = builder.build();
|
||||
Uri uri = builder.build();
|
||||
Intent intent = new Intent(action);
|
||||
intent.setData(uri);
|
||||
return intent;
|
||||
@ -148,8 +147,8 @@ public class Task {
|
||||
|
||||
public static class Builder {
|
||||
DownloadEntity downloadEntity;
|
||||
Handler outHandler;
|
||||
Context context;
|
||||
Handler outHandler;
|
||||
Context context;
|
||||
int threadNum = 3;
|
||||
IDownloadUtil downloadUtil;
|
||||
|
||||
@ -200,12 +199,12 @@ public class Task {
|
||||
private class DListener extends DownloadListener {
|
||||
Handler outHandler;
|
||||
Context context;
|
||||
Intent sendIntent;
|
||||
long INTERVAL = 1024 * 10; //10k大小的间隔
|
||||
long lastLen = 0; //上一次发送长度
|
||||
long lastTime = 0;
|
||||
long INTERVAL_TIME = 1000; //1m更新周期
|
||||
boolean isFirst = true;
|
||||
Intent sendIntent;
|
||||
long INTERVAL = 1024 * 10; //10k大小的间隔
|
||||
long lastLen = 0; //上一次发送长度
|
||||
long lastTime = 0;
|
||||
long INTERVAL_TIME = 1000; //1m更新周期
|
||||
boolean isFirst = true;
|
||||
DownloadEntity downloadEntity;
|
||||
|
||||
DListener(Context context, DownloadEntity downloadEntity, Handler outHandler) {
|
||||
@ -226,12 +225,14 @@ public class Task {
|
||||
super.onPostPre(fileSize);
|
||||
downloadEntity.setFileSize(fileSize);
|
||||
downloadEntity.setState(DownloadEntity.STATE_POST_PRE);
|
||||
sendInState2Target(DownloadSchedulers.PRE);
|
||||
sendIntent(DownloadManager.ACTION_POST_PRE, -1);
|
||||
}
|
||||
|
||||
@Override public void onResume(long resumeLocation) {
|
||||
super.onResume(resumeLocation);
|
||||
downloadEntity.setState(DownloadEntity.STATE_DOWNLOAD_ING);
|
||||
sendInState2Target(DownloadSchedulers.RESUME);
|
||||
sendIntent(DownloadManager.ACTION_RESUME, resumeLocation);
|
||||
}
|
||||
|
||||
|
@ -14,10 +14,12 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
|
||||
package com.arialyy.downloadutil.util;
|
||||
|
||||
import android.util.Log;
|
||||
import com.arialyy.downloadutil.core.DownloadEntity;
|
||||
import com.arialyy.downloadutil.core.command.CmdFactory;
|
||||
import com.arialyy.downloadutil.core.command.IDownloadCmd;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
@ -34,6 +36,10 @@ import java.util.Properties;
|
||||
public class CommonUtil {
|
||||
private static final String TAG = "util";
|
||||
|
||||
public static IDownloadCmd createCmd(DownloadEntity entity, int cmd) {
|
||||
return CmdFactory.getInstance().createCmd(entity, cmd);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取类里面的所在字段
|
||||
*/
|
||||
@ -249,9 +255,8 @@ public class CommonUtil {
|
||||
* 读取下载配置文件
|
||||
*/
|
||||
public static Properties loadConfig(File file) {
|
||||
Properties properties = new Properties();
|
||||
FileInputStream fis = null;
|
||||
|
||||
Properties properties = new Properties();
|
||||
FileInputStream fis = null;
|
||||
|
||||
try {
|
||||
fis = new FileInputStream(file);
|
||||
|
Reference in New Issue
Block a user