bug fix ,修复多监听器导致的异常问题,修复性能问题
This commit is contained in:
@ -37,6 +37,7 @@ import com.arialyy.downloadutil.util.CommonUtil;
|
||||
import com.arialyy.simple.R;
|
||||
import com.arialyy.simple.base.BaseActivity;
|
||||
import com.arialyy.simple.databinding.ActivitySingleBinding;
|
||||
import com.arialyy.simple.widget.HorizontalProgressBarWithNumber;
|
||||
|
||||
public class SingleTaskActivity extends BaseActivity<ActivitySingleBinding> {
|
||||
public static final int DOWNLOAD_PRE = 0x01;
|
||||
@ -48,12 +49,13 @@ public class SingleTaskActivity extends BaseActivity<ActivitySingleBinding> {
|
||||
public static final int DOWNLOAD_RUNNING = 0x07;
|
||||
private static final String DOWNLOAD_URL =
|
||||
"http://static.gaoshouyou.com/d/3a/93/573ae1db9493a801c24bf66128b11e39.apk";
|
||||
@Bind(R.id.progressBar) ProgressBar mPb;
|
||||
@Bind(R.id.progressBar) HorizontalProgressBarWithNumber mPb;
|
||||
@Bind(R.id.start) Button mStart;
|
||||
@Bind(R.id.stop) Button mStop;
|
||||
@Bind(R.id.cancel) Button mCancel;
|
||||
@Bind(R.id.size) TextView mSize;
|
||||
@Bind(R.id.toolbar) Toolbar toolbar;
|
||||
@Bind(R.id.speed) TextView mSpeed;
|
||||
private DownloadEntity mEntity;
|
||||
private BroadcastReceiver mReceiver;
|
||||
|
||||
@ -62,7 +64,15 @@ public class SingleTaskActivity extends BaseActivity<ActivitySingleBinding> {
|
||||
super.handleMessage(msg);
|
||||
switch (msg.what) {
|
||||
case DOWNLOAD_RUNNING:
|
||||
mPb.setProgress((Integer) msg.obj);
|
||||
Task task = (Task) msg.obj;
|
||||
long current = task.getDownloadEntity().getCurrentProgress();
|
||||
long len = task.getDownloadEntity().getFileSize();
|
||||
if (len == 0) {
|
||||
mPb.setProgress(0);
|
||||
} else {
|
||||
mPb.setProgress((int) ((current * 100) / len));
|
||||
}
|
||||
mSpeed.setText(CommonUtil.formatFileSize(task.getDownloadEntity().getSpeed()) + "/s");
|
||||
break;
|
||||
case DOWNLOAD_PRE:
|
||||
mSize.setText(CommonUtil.formatFileSize((Long) msg.obj));
|
||||
@ -218,13 +228,7 @@ public class SingleTaskActivity extends BaseActivity<ActivitySingleBinding> {
|
||||
}
|
||||
|
||||
@Override public void onTaskRunning(Task task) {
|
||||
long current = task.getDownloadEntity().getCurrentProgress();
|
||||
long len = task.getDownloadEntity().getFileSize();
|
||||
if (len == 0) {
|
||||
mPb.setProgress(0);
|
||||
} else {
|
||||
mPb.setProgress((int) ((current * 100) / len));
|
||||
}
|
||||
mUpdateHandler.obtainMessage(DOWNLOAD_RUNNING, task).sendToTarget();
|
||||
}
|
||||
}
|
||||
}
|
@ -9,9 +9,8 @@
|
||||
tools:showIn="@layout/activity_single"
|
||||
>
|
||||
|
||||
<ProgressBar
|
||||
<com.arialyy.simple.widget.HorizontalProgressBarWithNumber
|
||||
android:id="@+id/progressBar"
|
||||
style="?android:attr/progressBarStyleHorizontal"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="20dp"
|
||||
android:layout_alignParentLeft="true"
|
||||
@ -19,6 +18,8 @@
|
||||
android:layout_alignParentTop="true"
|
||||
android:layout_margin="16dp"
|
||||
android:layout_toLeftOf="@+id/size"
|
||||
android:max="100"
|
||||
style="?android:attr/progressBarStyleHorizontal"
|
||||
/>
|
||||
|
||||
<TextView
|
||||
@ -28,45 +29,54 @@
|
||||
android:layout_alignParentRight="true"
|
||||
android:layout_alignTop="@+id/progressBar"
|
||||
android:layout_marginRight="16dp"
|
||||
android:text="ssss"
|
||||
android:text="0mb"
|
||||
android:textSize="16sp"
|
||||
/>
|
||||
|
||||
|
||||
<LinearLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_below="@+id/progressBar"
|
||||
android:orientation="horizontal"
|
||||
>
|
||||
<TextView
|
||||
android:id="@+id/speed"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginLeft="16dp"
|
||||
android:text="0kb/s"
|
||||
android:textColor="@color/black"
|
||||
/>
|
||||
|
||||
<Button
|
||||
android:id="@+id/start"
|
||||
style="?buttonBarButtonStyle"
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_weight="1"
|
||||
android:onClick="onClick"
|
||||
android:text="开始"
|
||||
style="?buttonBarButtonStyle"
|
||||
/>
|
||||
|
||||
<Button
|
||||
android:id="@+id/stop"
|
||||
style="?buttonBarButtonStyle"
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_weight="1"
|
||||
android:onClick="onClick"
|
||||
android:text="暂停"
|
||||
style="?buttonBarButtonStyle"
|
||||
/>
|
||||
|
||||
<Button
|
||||
android:id="@+id/cancel"
|
||||
style="?buttonBarButtonStyle"
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_weight="1"
|
||||
android:onClick="onClick"
|
||||
android:text="删除任务"
|
||||
style="?buttonBarButtonStyle"
|
||||
/>
|
||||
</LinearLayout>
|
||||
</RelativeLayout>
|
||||
|
@ -37,7 +37,7 @@ public class AMReceiver {
|
||||
*/
|
||||
public AMReceiver addSchedulerListener(OnSchedulerListener listener) {
|
||||
this.listener = listener;
|
||||
manager.getTaskQueue().getDownloadSchedulers().addSchedulerListener(listener);
|
||||
manager.getTaskQueue().getDownloadSchedulers().addSchedulerListener(obj, listener);
|
||||
return this;
|
||||
}
|
||||
|
||||
@ -46,7 +46,7 @@ public class AMReceiver {
|
||||
*/
|
||||
public AMReceiver removeSchedulerListener() {
|
||||
if (listener != null) {
|
||||
manager.getTaskQueue().getDownloadSchedulers().removeSchedulerListener(listener);
|
||||
manager.getTaskQueue().getDownloadSchedulers().removeSchedulerListener(obj, listener);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
@ -24,8 +24,8 @@ public class AMTarget {
|
||||
*/
|
||||
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));
|
||||
cmds.add(CommonUtil.createCmd(receiver.obj, receiver.entity, CmdFactory.TASK_CREATE));
|
||||
cmds.add(CommonUtil.createCmd(receiver.obj, receiver.entity, CmdFactory.TASK_START));
|
||||
receiver.manager.setCmds(cmds).exe();
|
||||
}
|
||||
|
||||
@ -33,21 +33,24 @@ public class AMTarget {
|
||||
* 停止下载
|
||||
*/
|
||||
public void stop() {
|
||||
receiver.manager.setCmd(CommonUtil.createCmd(receiver.entity, CmdFactory.TASK_STOP)).exe();
|
||||
receiver.manager.setCmd(
|
||||
CommonUtil.createCmd(receiver.obj, receiver.entity, CmdFactory.TASK_STOP)).exe();
|
||||
}
|
||||
|
||||
/**
|
||||
* 恢复下载
|
||||
*/
|
||||
public void resume() {
|
||||
receiver.manager.setCmd(CommonUtil.createCmd(receiver.entity, CmdFactory.TASK_START)).exe();
|
||||
receiver.manager.setCmd(
|
||||
CommonUtil.createCmd(receiver.obj, receiver.entity, CmdFactory.TASK_START)).exe();
|
||||
}
|
||||
|
||||
/**
|
||||
* 取消下载
|
||||
*/
|
||||
public void cancel() {
|
||||
receiver.manager.setCmd(CommonUtil.createCmd(receiver.entity, CmdFactory.TASK_CANCEL)).exe();
|
||||
receiver.manager.setCmd(
|
||||
CommonUtil.createCmd(receiver.obj, receiver.entity, CmdFactory.TASK_CANCEL)).exe();
|
||||
}
|
||||
|
||||
public static class SimpleSchedulerListener implements OnSchedulerListener {
|
||||
|
@ -14,7 +14,6 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
|
||||
package com.arialyy.downloadutil.core.command;
|
||||
|
||||
import android.util.Log;
|
||||
@ -31,10 +30,14 @@ class AddCmd extends IDownloadCmd {
|
||||
super(entity);
|
||||
}
|
||||
|
||||
AddCmd(Object target, DownloadEntity entity) {
|
||||
super(target, entity);
|
||||
}
|
||||
|
||||
@Override public void executeCmd() {
|
||||
Task task = mQueue.getTask(mEntity);
|
||||
if (task == null) {
|
||||
mQueue.createTask(mEntity);
|
||||
mQueue.createTask(mTarget, mEntity);
|
||||
} else {
|
||||
Log.w(TAG, "添加命令执行失败,【该任务已经存在】");
|
||||
}
|
||||
|
@ -26,6 +26,10 @@ import com.arialyy.downloadutil.core.task.Task;
|
||||
*/
|
||||
class CancelCmd extends IDownloadCmd {
|
||||
|
||||
CancelCmd(Object target, DownloadEntity entity) {
|
||||
super(target, entity);
|
||||
}
|
||||
|
||||
CancelCmd(DownloadEntity entity) {
|
||||
super(entity);
|
||||
}
|
||||
@ -33,7 +37,7 @@ class CancelCmd extends IDownloadCmd {
|
||||
@Override public void executeCmd() {
|
||||
Task task = mQueue.getTask(mEntity);
|
||||
if (task == null) {
|
||||
task = mQueue.createTask(mEntity);
|
||||
task = mQueue.createTask(mTarget, mEntity);
|
||||
}
|
||||
if (task != null) {
|
||||
mQueue.cancelTask(task);
|
||||
|
@ -14,7 +14,6 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
|
||||
package com.arialyy.downloadutil.core.command;
|
||||
|
||||
import com.arialyy.downloadutil.core.DownloadEntity;
|
||||
@ -85,6 +84,38 @@ public class CmdFactory {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param target 创建任务的对象
|
||||
* @param entity 下载实体
|
||||
* @param type 命令类型{@link #TASK_CREATE}、{@link #TASK_START}、{@link #TASK_CANCEL}、{@link
|
||||
* #TASK_STOP}
|
||||
*/
|
||||
public IDownloadCmd createCmd(Object target, DownloadEntity entity, int type) {
|
||||
switch (type) {
|
||||
case TASK_CREATE:
|
||||
return createAddCmd(target, entity);
|
||||
case TASK_RESUME:
|
||||
case TASK_START:
|
||||
return createStartCmd(target, entity);
|
||||
case TASK_CANCEL:
|
||||
return createCancelCmd(target, entity);
|
||||
case TASK_STOP:
|
||||
return createStopCmd(target, entity);
|
||||
case TASK_SINGLE:
|
||||
return new SingleCmd(target, entity);
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建停止命令
|
||||
*
|
||||
* @return {@link StopCmd}
|
||||
*/
|
||||
private StopCmd createStopCmd(Object target, DownloadEntity entity) {
|
||||
return new StopCmd(target, entity);
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建停止命令
|
||||
@ -95,6 +126,15 @@ public class CmdFactory {
|
||||
return new StopCmd(entity);
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建下载任务命令
|
||||
*
|
||||
* @return {@link AddCmd}
|
||||
*/
|
||||
private AddCmd createAddCmd(Object target, DownloadEntity entity) {
|
||||
return new AddCmd(target, entity);
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建下载任务命令
|
||||
*
|
||||
@ -104,6 +144,15 @@ public class CmdFactory {
|
||||
return new AddCmd(entity);
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建启动下载命令
|
||||
*
|
||||
* @return {@link StartCmd}
|
||||
*/
|
||||
private StartCmd createStartCmd(Object target, DownloadEntity entity) {
|
||||
return new StartCmd(target, entity);
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建启动下载命令
|
||||
*
|
||||
@ -113,6 +162,15 @@ public class CmdFactory {
|
||||
return new StartCmd(entity);
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建 取消下载的命令
|
||||
*
|
||||
* @return {@link CancelCmd}
|
||||
*/
|
||||
private CancelCmd createCancelCmd(Object target, DownloadEntity entity) {
|
||||
return new CancelCmd(target, entity);
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建 取消下载的命令
|
||||
*
|
||||
|
@ -14,7 +14,6 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
|
||||
package com.arialyy.downloadutil.core.command;
|
||||
|
||||
import com.arialyy.downloadutil.core.DownloadEntity;
|
||||
@ -31,14 +30,23 @@ public abstract class IDownloadCmd {
|
||||
ITaskQueue mQueue;
|
||||
DownloadEntity mEntity;
|
||||
String TAG;
|
||||
Object mTarget;
|
||||
|
||||
/**
|
||||
* @param entity 下载实体
|
||||
*/
|
||||
IDownloadCmd(DownloadEntity entity) {
|
||||
this(null, entity);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param target 产生任务的对象
|
||||
*/
|
||||
IDownloadCmd(Object target, DownloadEntity entity) {
|
||||
if (!CheckUtil.checkDownloadEntity(entity)) {
|
||||
return;
|
||||
}
|
||||
mTarget = target;
|
||||
mEntity = entity;
|
||||
TAG = CommonUtil.getClassName(this);
|
||||
mQueue = DownloadManager.getInstance().getTaskQueue();
|
||||
|
@ -12,6 +12,10 @@ class SingleCmd extends IDownloadCmd {
|
||||
/**
|
||||
* @param entity 下载实体
|
||||
*/
|
||||
SingleCmd(Object target, DownloadEntity entity) {
|
||||
super(target, entity);
|
||||
}
|
||||
|
||||
SingleCmd(DownloadEntity entity) {
|
||||
super(entity);
|
||||
}
|
||||
@ -19,7 +23,7 @@ class SingleCmd extends IDownloadCmd {
|
||||
@Override public void executeCmd() {
|
||||
Task task = mQueue.getTask(mEntity);
|
||||
if (task == null) {
|
||||
task = mQueue.createTask(mEntity);
|
||||
task = mQueue.createTask(mTarget, mEntity);
|
||||
} else {
|
||||
Log.w(TAG, "添加命令执行失败,【该任务已经存在】");
|
||||
}
|
||||
|
@ -14,7 +14,6 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
|
||||
package com.arialyy.downloadutil.core.command;
|
||||
|
||||
import com.arialyy.downloadutil.core.DownloadEntity;
|
||||
@ -26,6 +25,10 @@ import com.arialyy.downloadutil.core.task.Task;
|
||||
*/
|
||||
class StartCmd extends IDownloadCmd {
|
||||
|
||||
StartCmd(Object target, DownloadEntity entity) {
|
||||
super(target, entity);
|
||||
}
|
||||
|
||||
StartCmd(DownloadEntity entity) {
|
||||
super(entity);
|
||||
}
|
||||
@ -33,7 +36,7 @@ class StartCmd extends IDownloadCmd {
|
||||
@Override public void executeCmd() {
|
||||
Task task = mQueue.getTask(mEntity);
|
||||
if (task == null) {
|
||||
task = mQueue.createTask(mEntity);
|
||||
task = mQueue.createTask(mTarget, mEntity);
|
||||
}
|
||||
if (task != null) {
|
||||
mQueue.startTask(task);
|
||||
|
@ -30,6 +30,10 @@ class StopCmd extends IDownloadCmd {
|
||||
/**
|
||||
* @param entity 下载实体
|
||||
*/
|
||||
StopCmd(Object target, DownloadEntity entity) {
|
||||
super(target, entity);
|
||||
}
|
||||
|
||||
StopCmd(DownloadEntity entity) {
|
||||
super(entity);
|
||||
}
|
||||
@ -38,7 +42,7 @@ class StopCmd extends IDownloadCmd {
|
||||
Task task = mQueue.getTask(mEntity);
|
||||
if (task == null) {
|
||||
if (mEntity.getState() == DownloadEntity.STATE_DOWNLOAD_ING) {
|
||||
task = mQueue.createTask(mEntity);
|
||||
task = mQueue.createTask(mTarget, mEntity);
|
||||
mQueue.stopTask(task);
|
||||
} else {
|
||||
Log.w(TAG, "停止命令执行失败,【调度器中没有该任务】");
|
||||
|
@ -14,7 +14,6 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
|
||||
package com.arialyy.downloadutil.core.queue;
|
||||
|
||||
import android.content.Context;
|
||||
@ -127,8 +126,14 @@ public class DownloadTaskQueue implements ITaskQueue {
|
||||
mExecutePool.setDownloadNum(downloadNum);
|
||||
}
|
||||
|
||||
@Override public Task createTask(DownloadEntity entity) {
|
||||
Task task = TaskFactory.getInstance().createTask(mContext, entity, mSchedulers);
|
||||
@Override public Task createTask(Object target, DownloadEntity entity) {
|
||||
Task task;
|
||||
if (target == null) {
|
||||
task = TaskFactory.getInstance().createTask(mContext, entity, mSchedulers);
|
||||
} else {
|
||||
task = TaskFactory.getInstance()
|
||||
.createTask(target.getClass().getName(), mContext, entity, mSchedulers);
|
||||
}
|
||||
mCachePool.putTask(task);
|
||||
return task;
|
||||
}
|
||||
|
@ -47,9 +47,10 @@ public interface ITaskQueue extends IDownloader {
|
||||
* 创建一个新的下载任务,创建时只是将新任务存储到缓存池
|
||||
*
|
||||
* @param entity 下载实体{@link DownloadEntity}
|
||||
* @param target 生成该任务的对象
|
||||
* @return {@link Task}
|
||||
*/
|
||||
public Task createTask(DownloadEntity entity);
|
||||
public Task createTask(Object target, DownloadEntity entity);
|
||||
|
||||
/**
|
||||
* 通过下载链接从缓存池或任务池搜索下载任务,如果缓存池或任务池都没有任务,则创建新任务
|
||||
|
@ -16,15 +16,14 @@
|
||||
|
||||
package com.arialyy.downloadutil.core.scheduler;
|
||||
|
||||
import android.content.Context;
|
||||
import android.os.Message;
|
||||
import android.text.TextUtils;
|
||||
import android.util.Log;
|
||||
import com.arialyy.downloadutil.core.DownloadEntity;
|
||||
import com.arialyy.downloadutil.core.queue.ITaskQueue;
|
||||
import com.arialyy.downloadutil.core.task.Task;
|
||||
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.concurrent.ConcurrentHashMap;
|
||||
|
||||
@ -81,7 +80,7 @@ public class DownloadSchedulers implements IDownloadSchedulers {
|
||||
/**
|
||||
* 下载器任务监听
|
||||
*/
|
||||
Map<Integer, OnSchedulerListener> mSchedulerListeners = new ConcurrentHashMap<>();
|
||||
Map<String, OnSchedulerListener> mSchedulerListeners = new ConcurrentHashMap<>();
|
||||
ITaskQueue mQueue;
|
||||
|
||||
public DownloadSchedulers(ITaskQueue downloadTaskQueue) {
|
||||
@ -130,9 +129,8 @@ public class DownloadSchedulers implements IDownloadSchedulers {
|
||||
*/
|
||||
private void callback(int state, Task task) {
|
||||
if (mSchedulerListeners.size() > 0) {
|
||||
//Set<Map.Entry<Integer, String>>
|
||||
for (Map.Entry<Integer, OnSchedulerListener> entry : mSchedulerListeners.entrySet()) {
|
||||
callback(state, task, entry.getValue());
|
||||
if (!TextUtils.isEmpty(task.getTargetName())) {
|
||||
callback(state, task, mSchedulerListeners.get(task.getTargetName()));
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -163,15 +161,12 @@ public class DownloadSchedulers implements IDownloadSchedulers {
|
||||
break;
|
||||
case CANCEL:
|
||||
listener.onTaskCancel(task);
|
||||
removeSchedulerListener(listener);
|
||||
break;
|
||||
case COMPLETE:
|
||||
listener.onTaskComplete(task);
|
||||
removeSchedulerListener(listener);
|
||||
break;
|
||||
case FAIL:
|
||||
listener.onTaskFail(task);
|
||||
removeSchedulerListener(listener);
|
||||
break;
|
||||
}
|
||||
}
|
||||
@ -213,13 +208,24 @@ public class DownloadSchedulers implements IDownloadSchedulers {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addSchedulerListener(OnSchedulerListener schedulerListener) {
|
||||
mSchedulerListeners.put(schedulerListener.hashCode(), schedulerListener);
|
||||
@Override public void addSchedulerListener(Object target, OnSchedulerListener schedulerListener) {
|
||||
if (target == null) {
|
||||
throw new IllegalArgumentException("target 不能为null");
|
||||
}
|
||||
String name = target.getClass().getName();
|
||||
if (mSchedulerListeners.get(name) != null) {
|
||||
Log.w(TAG, "监听器已存在");
|
||||
return;
|
||||
}
|
||||
mSchedulerListeners.put(name, schedulerListener);
|
||||
}
|
||||
|
||||
@Override public void removeSchedulerListener(OnSchedulerListener schedulerListener) {
|
||||
mSchedulerListeners.remove(schedulerListener.hashCode());
|
||||
@Override
|
||||
public void removeSchedulerListener(Object target, OnSchedulerListener schedulerListener) {
|
||||
if (target == null) {
|
||||
throw new IllegalArgumentException("target 不能为null");
|
||||
}
|
||||
mSchedulerListeners.remove(target.getClass().getName());
|
||||
}
|
||||
|
||||
public void setFailNum(int mFailNum) {
|
||||
|
@ -16,6 +16,7 @@
|
||||
|
||||
package com.arialyy.downloadutil.core.scheduler;
|
||||
|
||||
import android.content.Context;
|
||||
import android.os.Handler;
|
||||
import com.arialyy.downloadutil.core.DownloadEntity;
|
||||
|
||||
@ -26,16 +27,18 @@ import com.arialyy.downloadutil.core.DownloadEntity;
|
||||
public interface IDownloadSchedulers extends Handler.Callback {
|
||||
|
||||
/**
|
||||
* 注册下载器监听
|
||||
* 注册下载器监听,一个观察者只能注册一次监听
|
||||
*
|
||||
* @param target 观察者,创建该监听器的对象
|
||||
* @param schedulerListener {@link OnSchedulerListener}
|
||||
*/
|
||||
public void addSchedulerListener(OnSchedulerListener schedulerListener);
|
||||
public void addSchedulerListener(Object target, OnSchedulerListener schedulerListener);
|
||||
|
||||
/**
|
||||
* @param target 观察者,创建该监听器的对象
|
||||
* 取消注册监听器
|
||||
*/
|
||||
public void removeSchedulerListener(OnSchedulerListener schedulerListener);
|
||||
public void removeSchedulerListener(Object target, OnSchedulerListener schedulerListener);
|
||||
|
||||
/**
|
||||
* 处理下载任务下载失败的情形
|
||||
|
@ -32,7 +32,10 @@ import com.arialyy.downloadutil.core.scheduler.DownloadSchedulers;
|
||||
*/
|
||||
public class Task {
|
||||
public static final String TAG = "Task";
|
||||
|
||||
/**
|
||||
* 产生该任务对象的hash码
|
||||
*/
|
||||
private String mTargetName;
|
||||
private DownloadEntity mEntity;
|
||||
private IDownloadListener mListener;
|
||||
private Handler mOutHandler;
|
||||
@ -68,6 +71,14 @@ public class Task {
|
||||
return mEntity;
|
||||
}
|
||||
|
||||
public String getTargetName() {
|
||||
return mTargetName;
|
||||
}
|
||||
|
||||
public void setmTargetName(String targetName) {
|
||||
this.mTargetName = targetName;
|
||||
}
|
||||
|
||||
/**
|
||||
* 停止下载
|
||||
*/
|
||||
@ -150,9 +161,15 @@ public class Task {
|
||||
Handler outHandler;
|
||||
Context context;
|
||||
int threadNum = 3;
|
||||
String targetName;
|
||||
IDownloadUtil downloadUtil;
|
||||
|
||||
public Builder(Context context, DownloadEntity downloadEntity) {
|
||||
this("", context, downloadEntity);
|
||||
}
|
||||
|
||||
public Builder(String targetName, Context context, DownloadEntity downloadEntity) {
|
||||
this.targetName = targetName;
|
||||
this.context = context;
|
||||
this.downloadEntity = downloadEntity;
|
||||
}
|
||||
@ -188,6 +205,7 @@ public class Task {
|
||||
public Task build() {
|
||||
Task task = new Task(context, downloadEntity);
|
||||
task.mOutHandler = outHandler;
|
||||
task.setmTargetName(targetName);
|
||||
downloadEntity.save();
|
||||
return task;
|
||||
}
|
||||
|
@ -14,7 +14,6 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
|
||||
package com.arialyy.downloadutil.core.task;
|
||||
|
||||
import android.content.Context;
|
||||
@ -51,7 +50,12 @@ public class TaskFactory {
|
||||
* @param schedulers {@link IDownloadSchedulers}
|
||||
*/
|
||||
public Task createTask(Context context, DownloadEntity entity, IDownloadSchedulers schedulers) {
|
||||
Task.Builder builder = new Task.Builder(context, entity);
|
||||
return createTask("", context, entity, schedulers);
|
||||
}
|
||||
|
||||
public Task createTask(String targetName, Context context, DownloadEntity entity,
|
||||
IDownloadSchedulers schedulers) {
|
||||
Task.Builder builder = new Task.Builder(targetName, context, entity);
|
||||
builder.setOutHandler(schedulers);
|
||||
return builder.build();
|
||||
}
|
||||
|
@ -36,6 +36,10 @@ import java.util.Properties;
|
||||
public class CommonUtil {
|
||||
private static final String TAG = "util";
|
||||
|
||||
public static IDownloadCmd createCmd(Object target, DownloadEntity entity, int cmd) {
|
||||
return CmdFactory.getInstance().createCmd(target, entity, cmd);
|
||||
}
|
||||
|
||||
public static IDownloadCmd createCmd(DownloadEntity entity, int cmd) {
|
||||
return CmdFactory.getInstance().createCmd(entity, cmd);
|
||||
}
|
||||
|
Reference in New Issue
Block a user