逻辑重构

pull/2/head
AriaLyy 8 years ago
parent a842f74c6f
commit 374fdd12b9
  1. 146
      app/src/main/java/com/arialyy/simple/activity/MultiTaskActivity.java
  2. 108
      app/src/main/java/com/arialyy/simple/activity/SingleTaskActivity.java
  3. 11
      app/src/main/java/com/arialyy/simple/adapter/DownloadAdapter.java
  4. 59
      downloadutil/src/main/java/com/arialyy/downloadutil/core/AMReceiver.java
  5. 85
      downloadutil/src/main/java/com/arialyy/downloadutil/core/AMTarget.java
  6. 67
      downloadutil/src/main/java/com/arialyy/downloadutil/core/Aria.java
  7. 83
      downloadutil/src/main/java/com/arialyy/downloadutil/core/AriaManager.java
  8. 1
      downloadutil/src/main/java/com/arialyy/downloadutil/core/DownloadManager.java
  9. 28
      downloadutil/src/main/java/com/arialyy/downloadutil/core/scheduler/DownloadSchedulers.java
  10. 10
      downloadutil/src/main/java/com/arialyy/downloadutil/core/scheduler/OnSchedulerListener.java
  11. 29
      downloadutil/src/main/java/com/arialyy/downloadutil/core/task/Task.java
  12. 13
      downloadutil/src/main/java/com/arialyy/downloadutil/util/CommonUtil.java

@ -27,8 +27,11 @@ import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import butterknife.Bind;
import com.arialyy.downloadutil.core.AMTarget;
import com.arialyy.downloadutil.core.Aria;
import com.arialyy.downloadutil.core.DownloadEntity;
import com.arialyy.downloadutil.core.DownloadManager;
import com.arialyy.downloadutil.core.task.Task;
import com.arialyy.frame.util.show.L;
import com.arialyy.simple.R;
import com.arialyy.simple.adapter.DownloadAdapter;
@ -58,53 +61,53 @@ public class MultiTaskActivity extends BaseActivity<ActivityMultiBinding> {
mList.setAdapter(mAdapter);
}
private BroadcastReceiver mReceiver = new BroadcastReceiver() {
long len = 0;
@Override public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
DownloadEntity entity = intent.getParcelableExtra(DownloadManager.ENTITY);
switch (action) {
case DownloadManager.ACTION_PRE:
L.d(TAG, "download pre");
mAdapter.updateState(entity);
break;
case DownloadManager.ACTION_POST_PRE:
len = entity.getFileSize();
L.d(TAG, "download post pre");
break;
case DownloadManager.ACTION_START:
L.d(TAG, "download start");
break;
case DownloadManager.ACTION_RESUME:
L.d(TAG, "download resume");
long location = intent.getLongExtra(DownloadManager.CURRENT_LOCATION, 1);
mAdapter.updateState(entity);
break;
case DownloadManager.ACTION_RUNNING:
long current = intent.getLongExtra(DownloadManager.CURRENT_LOCATION, 0);
long speed = intent.getLongExtra(DownloadManager.CURRENT_SPEED, 0);
//mAdapter.setProgress(entity.getDownloadUrl(), current, speed);
mAdapter.setProgress(entity);
break;
case DownloadManager.ACTION_STOP:
L.d(TAG, "download stop");
mAdapter.updateState(entity);
break;
case DownloadManager.ACTION_COMPLETE:
L.d(TAG, "download complete");
mAdapter.updateState(entity);
break;
case DownloadManager.ACTION_CANCEL:
L.d(TAG, "download cancel");
mAdapter.updateState(entity);
break;
case DownloadManager.ACTION_FAIL:
L.d(TAG, "download fail");
break;
}
}
};
//private BroadcastReceiver mReceiver = new BroadcastReceiver() {
// long len = 0;
//
// @Override public void onReceive(Context context, Intent intent) {
// String action = intent.getAction();
// DownloadEntity entity = intent.getParcelableExtra(DownloadManager.ENTITY);
// switch (action) {
// case DownloadManager.ACTION_PRE:
// L.d(TAG, "download pre");
// mAdapter.updateState(entity);
// break;
// case DownloadManager.ACTION_POST_PRE:
// len = entity.getFileSize();
// L.d(TAG, "download post pre");
// break;
// case DownloadManager.ACTION_START:
// L.d(TAG, "download start");
// break;
// case DownloadManager.ACTION_RESUME:
// L.d(TAG, "download resume");
// long location = intent.getLongExtra(DownloadManager.CURRENT_LOCATION, 1);
// mAdapter.updateState(entity);
// break;
// case DownloadManager.ACTION_RUNNING:
// long current = intent.getLongExtra(DownloadManager.CURRENT_LOCATION, 0);
// long speed = intent.getLongExtra(DownloadManager.CURRENT_SPEED, 0);
// //mAdapter.setProgress(entity.getDownloadUrl(), current, speed);
// mAdapter.setProgress(entity);
// break;
// case DownloadManager.ACTION_STOP:
// L.d(TAG, "download stop");
// mAdapter.updateState(entity);
// break;
// case DownloadManager.ACTION_COMPLETE:
// L.d(TAG, "download complete");
// mAdapter.updateState(entity);
// break;
// case DownloadManager.ACTION_CANCEL:
// L.d(TAG, "download cancel");
// mAdapter.updateState(entity);
// break;
// case DownloadManager.ACTION_FAIL:
// L.d(TAG, "download fail");
// break;
// }
// }
//};
public void onClick(View view){
DownloadNumDialog dialog = new DownloadNumDialog(this);
@ -113,12 +116,13 @@ public class MultiTaskActivity extends BaseActivity<ActivityMultiBinding> {
@Override protected void onResume() {
super.onResume();
registerReceiver(mReceiver, getModule(DownloadModule.class).getDownloadFilter());
//registerReceiver(mReceiver, getModule(DownloadModule.class).getDownloadFilter());
Aria.whit(this).addSchedulerListener(new MySchedulerListener());
}
@Override protected void onDestroy() {
super.onDestroy();
unregisterReceiver(mReceiver);
//unregisterReceiver(mReceiver);
}
@Override protected void dataCallback(int result, Object data) {
@ -127,4 +131,48 @@ public class MultiTaskActivity extends BaseActivity<ActivityMultiBinding> {
mAdapter.setDownloadNum(Integer.parseInt(data + ""));
}
}
private class MySchedulerListener extends AMTarget.SimpleSchedulerListener{
@Override public void onTaskPre(Task task) {
super.onTaskPre(task);
L.d(TAG, "download pre");
mAdapter.updateState(task.getDownloadEntity());
}
@Override public void onTaskStart(Task task) {
super.onTaskStart(task);
L.d(TAG, "download start");
}
@Override public void onTaskResume(Task task) {
super.onTaskResume(task);
L.d(TAG, "download resume");
mAdapter.updateState(task.getDownloadEntity());
}
@Override public void onTaskRunning(Task task) {
super.onTaskRunning(task);
mAdapter.setProgress(task.getDownloadEntity());
}
@Override public void onTaskStop(Task task) {
super.onTaskStop(task);
mAdapter.updateState(task.getDownloadEntity());
}
@Override public void onTaskCancel(Task task) {
super.onTaskCancel(task);
mAdapter.updateState(task.getDownloadEntity());
}
@Override public void onTaskComplete(Task task) {
super.onTaskComplete(task);
mAdapter.updateState(task.getDownloadEntity());
}
@Override public void onTaskFail(Task task) {
super.onTaskFail(task);
L.d(TAG, "download fail");
}
}
}

@ -28,11 +28,10 @@ import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import butterknife.Bind;
import com.arialyy.downloadutil.core.AMReceiver;
import com.arialyy.downloadutil.core.AMTarget;
import com.arialyy.downloadutil.core.Aria;
import com.arialyy.downloadutil.core.DownloadEntity;
import com.arialyy.downloadutil.core.DownloadManager;
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.orm.DbEntity;
import com.arialyy.downloadutil.util.CommonUtil;
@ -54,8 +53,6 @@ public class SingleTaskActivity extends BaseActivity<ActivitySingleBinding> {
private Button mStart, mStop, mCancel;
private TextView mSize;
@Bind(R.id.toolbar) Toolbar toolbar;
private CmdFactory mFactory;
private DownloadManager mManager;
private DownloadEntity mEntity;
private BroadcastReceiver mReceiver;
@ -69,6 +66,7 @@ public class SingleTaskActivity extends BaseActivity<ActivitySingleBinding> {
case DOWNLOAD_PRE:
mSize.setText(CommonUtil.formatFileSize((Long) msg.obj));
setBtState(false);
mStart.setText("暂停");
break;
case DOWNLOAD_FAILE:
Toast.makeText(SingleTaskActivity.this, "下载失败", Toast.LENGTH_SHORT).show();
@ -89,6 +87,7 @@ public class SingleTaskActivity extends BaseActivity<ActivitySingleBinding> {
//Toast.makeText(SingleTaskActivity.this,
// "恢复下载,恢复位置 ==> " + CommonUtil.formatFileSize((Long) msg.obj), Toast.LENGTH_SHORT)
// .show();
mStart.setText("暂停");
setBtState(false);
break;
case DOWNLOAD_COMPLETE:
@ -119,6 +118,7 @@ public class SingleTaskActivity extends BaseActivity<ActivitySingleBinding> {
//IntentFilter filter = getModule(DownloadModule.class).getDownloadFilter();
//mReceiver = getModule(DownloadModule.class).createReceiver(mUpdateHandler);
//registerReceiver(mReceiver, filter);
Aria.whit(this).addSchedulerListener(new MySchedulerListener());
}
@Override protected void onDestroy() {
@ -143,8 +143,8 @@ public class SingleTaskActivity extends BaseActivity<ActivitySingleBinding> {
mStop = (Button) findViewById(R.id.stop);
mCancel = (Button) findViewById(R.id.cancel);
mSize = (TextView) findViewById(R.id.size);
mFactory = CmdFactory.getInstance();
mManager = DownloadManager.getInstance();
//mFactory = CmdFactory.getInstance();
//mManager = DownloadManager.getInstance();
mEntity = DbEntity.findData(DownloadEntity.class, new String[] { "downloadUrl" },
new String[] { mDownloadUrl });
if (mEntity != null) {
@ -184,65 +184,61 @@ public class SingleTaskActivity extends BaseActivity<ActivitySingleBinding> {
}
private void resume(){
IDownloadCmd startCmd = mFactory.createCmd(mEntity, CmdFactory.TASK_START);
mManager.setCmd(startCmd).exe();
mUpdateHandler.obtainMessage(DOWNLOAD_RESUME, mEntity.getCurrentProgress()).sendToTarget();
Aria.whit(this).load(mEntity).resume();
}
private void start() {
mEntity.setFileName("test.apk");
mEntity.setDownloadUrl(mDownloadUrl);
mEntity.setDownloadPath(Environment.getExternalStorageDirectory().getPath() + "/test.apk");
//List<IDownloadCmd> commands = new ArrayList<>();
//IDownloadCmd addCMD = mFactory.createCmd(mEntity, CmdFactory.TASK_CREATE);
//IDownloadCmd startCmd = mFactory.createCmd(mEntity, CmdFactory.TASK_START);
//commands.add(addCMD);
//commands.add(startCmd);
//mManager.setCmds(commands).exe();
mManager.setCmd(CmdFactory.getInstance().createCmd(mEntity, CmdFactory.TASK_SINGLE))
.addSchedulerListener(new OnSchedulerListener() {
@Override public void onTaskStart(Task task) {
mUpdateHandler.obtainMessage(DOWNLOAD_PRE, task.getDownloadEntity().getFileSize())
.sendToTarget();
}
@Override public void onTaskStop(Task task) {
mUpdateHandler.sendEmptyMessage(DOWNLOAD_STOP);
}
@Override public void onTaskCancel(Task task) {
mUpdateHandler.sendEmptyMessage(DOWNLOAD_CANCEL);
}
@Override public void onTaskFail(Task task) {
mUpdateHandler.sendEmptyMessage(DOWNLOAD_FAILE);
}
@Override public void onTaskComplete(Task task) {
mUpdateHandler.sendEmptyMessage(DOWNLOAD_COMPLETE);
}
@Override public void onTaskRunning(Task task) {
//L.d(TAG, task.getDownloadEntity().getCurrentProgress() + "");
long current = task.getDownloadEntity().getCurrentProgress();
long len = task.getDownloadEntity().getFileSize();
if (len == 0) {
mPb.setProgress(0);
} else {
mPb.setProgress((int) ((current * 100) / len));
}
}
})
.exe();
Aria.whit(this).load(mEntity).start();
}
private void stop() {
IDownloadCmd stopCmd = mFactory.createCmd(mEntity, CmdFactory.TASK_STOP);
mManager.setCmd(stopCmd).exe();
Aria.whit(this).load(mEntity).stop();
}
private void cancel() {
IDownloadCmd cancelCmd = mFactory.createCmd(mEntity, CmdFactory.TASK_CANCEL);
mManager.setCmd(cancelCmd).exe();
Aria.whit(this).load(mEntity).cancel();
}
private class MySchedulerListener extends AMTarget.SimpleSchedulerListener{
@Override public void onTaskStart(Task task) {
mUpdateHandler.obtainMessage(DOWNLOAD_PRE, task.getDownloadEntity().getFileSize())
.sendToTarget();
}
@Override public void onTaskResume(Task task) {
super.onTaskResume(task);
mUpdateHandler.obtainMessage(DOWNLOAD_PRE, task.getDownloadEntity().getFileSize())
.sendToTarget();
}
@Override public void onTaskStop(Task task) {
mUpdateHandler.sendEmptyMessage(DOWNLOAD_STOP);
}
@Override public void onTaskCancel(Task task) {
mUpdateHandler.sendEmptyMessage(DOWNLOAD_CANCEL);
}
@Override public void onTaskFail(Task task) {
mUpdateHandler.sendEmptyMessage(DOWNLOAD_FAILE);
}
@Override public void onTaskComplete(Task task) {
mUpdateHandler.sendEmptyMessage(DOWNLOAD_COMPLETE);
}
@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));
}
}
}
}

@ -25,6 +25,7 @@ import android.widget.TextView;
import butterknife.Bind;
import com.arialyy.absadapter.common.AbsHolder;
import com.arialyy.absadapter.recycler_view.AbsRVAdapter;
import com.arialyy.downloadutil.core.Aria;
import com.arialyy.downloadutil.core.DownloadEntity;
import com.arialyy.downloadutil.core.DownloadManager;
import com.arialyy.downloadutil.core.command.CmdFactory;
@ -188,13 +189,15 @@ public class DownloadAdapter extends AbsRVAdapter<DownloadEntity, DownloadAdapte
}
private void start(DownloadEntity entity) {
IDownloadCmd startCmd = mFactory.createCmd(entity, CmdFactory.TASK_START);
mManager.setCmd(startCmd).exe();
//IDownloadCmd startCmd = mFactory.createCmd(entity, CmdFactory.TASK_START);
//mManager.setCmd(startCmd).exe();
Aria.whit(getContext()).load(entity).start();
}
private void stop(DownloadEntity entity) {
IDownloadCmd stopCmd = mFactory.createCmd(entity, CmdFactory.TASK_STOP);
mManager.setCmd(stopCmd).exe();
//IDownloadCmd stopCmd = mFactory.createCmd(entity, CmdFactory.TASK_STOP);
//mManager.setCmd(stopCmd).exe();
Aria.whit(getContext()).load(entity).stop();
}
}

@ -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);
/**
* 停止所有正在执行的任务
*/
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 AMReceiver putTarget(Context context) {
String clsName = context.getClass().getName();
AMReceiver target = mTargets.get(clsName);
if (target == null) {
target = new AMTarget();
target = new AMReceiver();
target.context = context;
mTargets.put(clsName, target);
}
return target;
}
private AMTarget getTarget(Context context) {
return mTargets.get(context.getClass().getName());
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);

Loading…
Cancel
Save