From f1e28fe491a5b9ea668dc71782ddc05a47cea027 Mon Sep 17 00:00:00 2001 From: AriaLyy <511455842@qq.com> Date: Wed, 30 Nov 2016 18:12:15 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=8D=95=E4=BB=BB=E5=8A=A1?= =?UTF-8?q?=E4=B8=8B=E8=BD=BD=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../simple/activity/SingleTaskActivity.java | 150 ++++++++++-------- .../arialyy/simple/module/DownloadModule.java | 53 +++++++ .../downloadutil/core/DownloadEntity.java | 9 +- .../downloadutil/core/DownloadManager.java | 46 +++--- .../downloadutil/core/command/CmdFactory.java | 4 + .../downloadutil/core/command/SingleCmd.java | 28 ++++ .../core/queue/DownloadTaskQueue.java | 4 + .../downloadutil/core/queue/ITaskQueue.java | 5 + .../core/scheduler/DownloadSchedulers.java | 67 +++----- .../core/scheduler/IDownloadSchedulers.java | 13 +- .../core/scheduler/OnSchedulerListener.java | 38 +++++ .../arialyy/downloadutil/core/task/Task.java | 1 + .../arialyy/downloadutil/util/CheckUtil.java | 10 +- .../arialyy/downloadutil/util/CommonUtil.java | 2 + 14 files changed, 288 insertions(+), 142 deletions(-) create mode 100644 downloadutil/src/main/java/com/arialyy/downloadutil/core/command/SingleCmd.java create mode 100644 downloadutil/src/main/java/com/arialyy/downloadutil/core/scheduler/OnSchedulerListener.java diff --git a/app/src/main/java/com/arialyy/simple/activity/SingleTaskActivity.java b/app/src/main/java/com/arialyy/simple/activity/SingleTaskActivity.java index 0db815af..66d160bc 100644 --- a/app/src/main/java/com/arialyy/simple/activity/SingleTaskActivity.java +++ b/app/src/main/java/com/arialyy/simple/activity/SingleTaskActivity.java @@ -14,12 +14,12 @@ * limitations under the License. */ - package com.arialyy.simple.activity; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; +import android.content.IntentFilter; import android.os.Bundle; import android.os.Environment; import android.os.Handler; @@ -35,6 +35,8 @@ 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; import com.arialyy.frame.util.show.L; @@ -46,26 +48,31 @@ import java.util.ArrayList; import java.util.List; public class SingleTaskActivity extends BaseActivity { - private static final int DOWNLOAD_PRE = 0x01; - private static final int DOWNLOAD_STOP = 0x02; - private static final int DOWNLOAD_FAILE = 0x03; - private static final int DOWNLOAD_CANCEL = 0x04; - private static final int DOWNLOAD_RESUME = 0x05; - private static final int DOWNLOAD_COMPLETE = 0x06; + public static final int DOWNLOAD_PRE = 0x01; + public static final int DOWNLOAD_STOP = 0x02; + public static final int DOWNLOAD_FAILE = 0x03; + public static final int DOWNLOAD_CANCEL = 0x04; + public static final int DOWNLOAD_RESUME = 0x05; + public static final int DOWNLOAD_COMPLETE = 0x06; + public static final int DOWNLOAD_RUNNING = 0x07; private ProgressBar mPb; private String mDownloadUrl = "http://static.gaoshouyou.com/d/12/0d/7f120f50c80d2e7b8c4ba24ece4f9cdd.apk"; private Button mStart, mStop, mCancel; - private TextView mSize; - @Bind(R.id.toolbar) Toolbar toolbar; - private CmdFactory mFactory; - private DownloadManager mManager; - private DownloadEntity mEntity; + private TextView mSize; + @Bind(R.id.toolbar) Toolbar toolbar; + private CmdFactory mFactory; + private DownloadManager mManager; + private DownloadEntity mEntity; + private BroadcastReceiver mReceiver; private Handler mUpdateHandler = new Handler() { @Override public void handleMessage(Message msg) { super.handleMessage(msg); switch (msg.what) { + case DOWNLOAD_RUNNING: + mPb.setProgress((Integer) msg.obj); + break; case DOWNLOAD_PRE: mSize.setText(CommonUtil.formatFileSize((Long) msg.obj)); setBtState(false); @@ -86,8 +93,9 @@ public class SingleTaskActivity extends BaseActivity { setBtState(true); break; case DOWNLOAD_RESUME: - Toast.makeText(SingleTaskActivity.this, - "恢复下载,恢复位置 ==> " + CommonUtil.formatFileSize((Long) msg.obj), Toast.LENGTH_SHORT).show(); + //Toast.makeText(SingleTaskActivity.this, + // "恢复下载,恢复位置 ==> " + CommonUtil.formatFileSize((Long) msg.obj), Toast.LENGTH_SHORT) + // .show(); setBtState(false); break; case DOWNLOAD_COMPLETE: @@ -108,51 +116,6 @@ public class SingleTaskActivity extends BaseActivity { mStop.setEnabled(!state); } - private BroadcastReceiver mReceiver = new BroadcastReceiver() { - long len = 0; - - @Override public void onReceive(Context context, Intent intent) { - String action = intent.getAction(); - switch (action) { - case DownloadManager.ACTION_POST_PRE: - DownloadEntity entity = intent.getParcelableExtra(DownloadManager.ENTITY); - len = entity.getFileSize(); - L.d(TAG, "download pre"); - mUpdateHandler.obtainMessage(DOWNLOAD_PRE, len).sendToTarget(); - 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); - mUpdateHandler.obtainMessage(DOWNLOAD_RESUME, location).sendToTarget(); - break; - case DownloadManager.ACTION_RUNNING: - long current = intent.getLongExtra(DownloadManager.CURRENT_LOCATION, 0); - if (len == 0) { - mPb.setProgress(0); - } else { - mPb.setProgress((int) ((current * 100) / len)); - } - break; - case DownloadManager.ACTION_STOP: - L.d(TAG, "download stop"); - mUpdateHandler.sendEmptyMessage(DOWNLOAD_STOP); - break; - case DownloadManager.ACTION_COMPLETE: - mUpdateHandler.sendEmptyMessage(DOWNLOAD_COMPLETE); - break; - case DownloadManager.ACTION_CANCEL: - mUpdateHandler.sendEmptyMessage(DOWNLOAD_CANCEL); - break; - case DownloadManager.ACTION_FAIL: - mUpdateHandler.sendEmptyMessage(DOWNLOAD_FAILE); - break; - } - } - }; - @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); init(); @@ -160,12 +123,14 @@ public class SingleTaskActivity extends BaseActivity { @Override protected void onResume() { super.onResume(); - registerReceiver(mReceiver, getModule(DownloadModule.class).getDownloadFilter()); + //IntentFilter filter = getModule(DownloadModule.class).getDownloadFilter(); + //mReceiver = getModule(DownloadModule.class).createReceiver(mUpdateHandler); + //registerReceiver(mReceiver, filter); } @Override protected void onDestroy() { super.onDestroy(); - unregisterReceiver(mReceiver); + //unregisterReceiver(mReceiver); } @Override protected int setLayoutId() { @@ -200,13 +165,21 @@ public class SingleTaskActivity extends BaseActivity { } } else { mEntity = new DownloadEntity(); + mEntity.setFileName("test.apk"); + mEntity.setDownloadUrl(mDownloadUrl); + mEntity.setDownloadPath(Environment.getExternalStorageDirectory().getPath() + "/test.apk"); } } public void onClick(View view) { switch (view.getId()) { case R.id.start: - start(); + String text = ((TextView) view).getText().toString(); + if (text.equals("重新开始?") || text.equals("开始")) { + start(); + }else if (text.equals("恢复")){ + resume(); + } break; case R.id.stop: stop(); @@ -217,16 +190,57 @@ public class SingleTaskActivity extends BaseActivity { } } + private void resume(){ + IDownloadCmd startCmd = mFactory.createCmd(mEntity, CmdFactory.TASK_START); + mManager.setCmd(startCmd).exe(); + mUpdateHandler.obtainMessage(DOWNLOAD_RESUME, mEntity.getCurrentProgress()).sendToTarget(); + } + private void start() { mEntity.setFileName("test.apk"); mEntity.setDownloadUrl(mDownloadUrl); mEntity.setDownloadPath(Environment.getExternalStorageDirectory().getPath() + "/test.apk"); - List 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(); + //List 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)) + .regSchedulerListener(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(); } private void stop() { diff --git a/app/src/main/java/com/arialyy/simple/module/DownloadModule.java b/app/src/main/java/com/arialyy/simple/module/DownloadModule.java index dd8eedb1..2165880c 100644 --- a/app/src/main/java/com/arialyy/simple/module/DownloadModule.java +++ b/app/src/main/java/com/arialyy/simple/module/DownloadModule.java @@ -17,15 +17,20 @@ package com.arialyy.simple.module; +import android.content.BroadcastReceiver; import android.content.Context; +import android.content.Intent; import android.content.IntentFilter; import android.os.Environment; +import android.os.Handler; import com.arialyy.downloadutil.core.DownloadEntity; import com.arialyy.downloadutil.core.DownloadManager; import com.arialyy.downloadutil.util.CommonUtil; import com.arialyy.frame.util.AndroidUtils; import com.arialyy.frame.util.StringUtil; +import com.arialyy.frame.util.show.L; import com.arialyy.simple.R; +import com.arialyy.simple.activity.SingleTaskActivity; import com.arialyy.simple.base.BaseModule; import java.io.File; import java.util.ArrayList; @@ -114,6 +119,54 @@ public class DownloadModule extends BaseModule { return filter; } + /** + * 创建Receiver + */ + public BroadcastReceiver createReceiver(final Handler handler){ + + return new BroadcastReceiver() { + long len = 0; + + @Override public void onReceive(Context context, Intent intent) { + String action = intent.getAction(); + switch (action) { + case DownloadManager.ACTION_POST_PRE: + DownloadEntity entity = intent.getParcelableExtra(DownloadManager.ENTITY); + len = entity.getFileSize(); + L.d(TAG, "download pre"); + handler.obtainMessage(SingleTaskActivity.DOWNLOAD_PRE, len).sendToTarget(); + 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); + handler.obtainMessage(SingleTaskActivity.DOWNLOAD_RESUME, location).sendToTarget(); + break; + case DownloadManager.ACTION_RUNNING: + long current = intent.getLongExtra(DownloadManager.CURRENT_LOCATION, 0); + int progress = len ==0 ? 0 : (int) ((current * 100) / len); + handler.obtainMessage(SingleTaskActivity.DOWNLOAD_RUNNING, progress).sendToTarget(); + break; + case DownloadManager.ACTION_STOP: + L.d(TAG, "download stop"); + handler.sendEmptyMessage(SingleTaskActivity.DOWNLOAD_STOP); + break; + case DownloadManager.ACTION_COMPLETE: + handler.sendEmptyMessage(SingleTaskActivity.DOWNLOAD_COMPLETE); + break; + case DownloadManager.ACTION_CANCEL: + handler.sendEmptyMessage(SingleTaskActivity.DOWNLOAD_CANCEL); + break; + case DownloadManager.ACTION_FAIL: + handler.sendEmptyMessage(SingleTaskActivity.DOWNLOAD_FAILE); + break; + } + } + }; + } + /** * 设置下载队列 */ diff --git a/downloadutil/src/main/java/com/arialyy/downloadutil/core/DownloadEntity.java b/downloadutil/src/main/java/com/arialyy/downloadutil/core/DownloadEntity.java index b466340e..6e53bc7f 100644 --- a/downloadutil/src/main/java/com/arialyy/downloadutil/core/DownloadEntity.java +++ b/downloadutil/src/main/java/com/arialyy/downloadutil/core/DownloadEntity.java @@ -116,8 +116,9 @@ public class DownloadEntity extends DbEntity implements Parcelable { return fileName; } - public void setFileName(String fileName) { + public DownloadEntity setFileName(String fileName) { this.fileName = fileName; + return this; } public int getFailNum() { @@ -132,8 +133,9 @@ public class DownloadEntity extends DbEntity implements Parcelable { return downloadUrl; } - public void setDownloadUrl(String downloadUrl) { + public DownloadEntity setDownloadUrl(String downloadUrl) { this.downloadUrl = downloadUrl; + return this; } public long getCompleteTime() { @@ -148,8 +150,9 @@ public class DownloadEntity extends DbEntity implements Parcelable { return downloadPath; } - public void setDownloadPath(String downloadPath) { + public DownloadEntity setDownloadPath(String downloadPath) { this.downloadPath = downloadPath; + return this; } public long getFileSize() { diff --git a/downloadutil/src/main/java/com/arialyy/downloadutil/core/DownloadManager.java b/downloadutil/src/main/java/com/arialyy/downloadutil/core/DownloadManager.java index 2a550423..c9d105d6 100644 --- a/downloadutil/src/main/java/com/arialyy/downloadutil/core/DownloadManager.java +++ b/downloadutil/src/main/java/com/arialyy/downloadutil/core/DownloadManager.java @@ -14,7 +14,6 @@ * limitations under the License. */ - package com.arialyy.downloadutil.core; import android.app.Application; @@ -23,6 +22,7 @@ import android.util.Log; import com.arialyy.downloadutil.core.command.IDownloadCmd; import com.arialyy.downloadutil.core.queue.ITaskQueue; import com.arialyy.downloadutil.core.queue.DownloadTaskQueue; +import com.arialyy.downloadutil.core.scheduler.OnSchedulerListener; import com.arialyy.downloadutil.orm.DbEntity; import com.arialyy.downloadutil.orm.DbUtil; import java.util.ArrayList; @@ -36,57 +36,58 @@ public class DownloadManager { /** * 预处理完成 */ - public static final String ACTION_PRE = "ACTION_PRE"; + public static final String ACTION_PRE = "ACTION_PRE"; /** * 下载开始前事件 */ - public static final String ACTION_POST_PRE = "ACTION_POST_PRE"; + public static final String ACTION_POST_PRE = "ACTION_POST_PRE"; /** * 开始下载事件 */ - public static final String ACTION_START = "ACTION_START"; + public static final String ACTION_START = "ACTION_START"; /** * 恢复下载事件 */ - public static final String ACTION_RESUME = "ACTION_RESUME"; + public static final String ACTION_RESUME = "ACTION_RESUME"; /** * 正在下载事件 */ - public static final String ACTION_RUNNING = "ACTION_RUNNING"; + public static final String ACTION_RUNNING = "ACTION_RUNNING"; /** * 停止下载事件 */ - public static final String ACTION_STOP = "ACTION_STOP"; + public static final String ACTION_STOP = "ACTION_STOP"; /** * 取消下载事件 */ - public static final String ACTION_CANCEL = "ACTION_CANCEL"; + public static final String ACTION_CANCEL = "ACTION_CANCEL"; /** * 下载完成事件 */ - public static final String ACTION_COMPLETE = "ACTION_COMPLETE"; + public static final String ACTION_COMPLETE = "ACTION_COMPLETE"; /** * 下载失败事件 */ - public static final String ACTION_FAIL = "ACTION_FAIL"; + public static final String ACTION_FAIL = "ACTION_FAIL"; /** * 下载实体 */ - public static final String ENTITY = "DOWNLOAD_ENTITY"; + public static final String ENTITY = "DOWNLOAD_ENTITY"; /** * 位置 */ - public static final String CURRENT_LOCATION = "CURRENT_LOCATION"; + public static final String CURRENT_LOCATION = "CURRENT_LOCATION"; /** * 速度 */ - public static final String CURRENT_SPEED = "CURRENT_SPEED"; - private static final String TAG = "DownloadManager"; - private static final Object LOCK = new Object(); - private static volatile DownloadManager INSTANCE = null; - private List mCommands = new ArrayList<>(); - private Context mContext; + public static final String CURRENT_SPEED = "CURRENT_SPEED"; + private static final String TAG = "DownloadManager"; + private static final Object LOCK = new Object(); + private static volatile DownloadManager INSTANCE = null; + private List mCommands = new ArrayList<>(); + private Context mContext; private ITaskQueue mTaskQueue; + private DownloadEntity mTempDEntity; private DownloadManager() { @@ -123,6 +124,15 @@ public class DownloadManager { return DbEntity.findAllData(DownloadEntity.class); } + /** + * 注册 + * @param listener + */ + public DownloadManager regSchedulerListener(OnSchedulerListener listener) { + mTaskQueue.getDownloadSchedulers().regTargetListener(listener); + return this; + } + /** * 获取任务队列 */ diff --git a/downloadutil/src/main/java/com/arialyy/downloadutil/core/command/CmdFactory.java b/downloadutil/src/main/java/com/arialyy/downloadutil/core/command/CmdFactory.java index 1dd58af9..4f50ca53 100644 --- a/downloadutil/src/main/java/com/arialyy/downloadutil/core/command/CmdFactory.java +++ b/downloadutil/src/main/java/com/arialyy/downloadutil/core/command/CmdFactory.java @@ -44,6 +44,7 @@ public class CmdFactory { * 停止任务 */ public static final int TASK_STOP = 0x125; + public static final int TASK_SINGLE = 0x126; private static final Object LOCK = new Object(); private static volatile CmdFactory INSTANCE = null; @@ -77,11 +78,14 @@ public class CmdFactory { return createCancelCmd(entity); case TASK_STOP: return createStopCmd(entity); + case TASK_SINGLE: + return new SingleCmd(entity); default: return null; } } + /** * 创建停止命令 * diff --git a/downloadutil/src/main/java/com/arialyy/downloadutil/core/command/SingleCmd.java b/downloadutil/src/main/java/com/arialyy/downloadutil/core/command/SingleCmd.java new file mode 100644 index 00000000..6a44da9c --- /dev/null +++ b/downloadutil/src/main/java/com/arialyy/downloadutil/core/command/SingleCmd.java @@ -0,0 +1,28 @@ +package com.arialyy.downloadutil.core.command; + +import android.util.Log; +import com.arialyy.downloadutil.core.DownloadEntity; +import com.arialyy.downloadutil.core.task.Task; + +/** + * Created by lyy on 2016/11/30. + * 获取任务状态命令 + */ +class SingleCmd extends IDownloadCmd { + /** + * @param entity 下载实体 + */ + SingleCmd(DownloadEntity entity) { + super(entity); + } + + @Override public void executeCmd() { + Task task = mQueue.getTask(mEntity); + if (task == null) { + task = mQueue.createTask(mEntity); + } else { + Log.w(TAG, "添加命令执行失败,【该任务已经存在】"); + } + mQueue.startTask(task); + } +} diff --git a/downloadutil/src/main/java/com/arialyy/downloadutil/core/queue/DownloadTaskQueue.java b/downloadutil/src/main/java/com/arialyy/downloadutil/core/queue/DownloadTaskQueue.java index 9fab5801..48ca946c 100644 --- a/downloadutil/src/main/java/com/arialyy/downloadutil/core/queue/DownloadTaskQueue.java +++ b/downloadutil/src/main/java/com/arialyy/downloadutil/core/queue/DownloadTaskQueue.java @@ -111,6 +111,10 @@ public class DownloadTaskQueue implements ITaskQueue { } } + @Override public IDownloadSchedulers getDownloadSchedulers() { + return mSchedulers; + } + @Override public int size() { return mExecutePool.size(); } diff --git a/downloadutil/src/main/java/com/arialyy/downloadutil/core/queue/ITaskQueue.java b/downloadutil/src/main/java/com/arialyy/downloadutil/core/queue/ITaskQueue.java index 327085b7..e02e5a9a 100644 --- a/downloadutil/src/main/java/com/arialyy/downloadutil/core/queue/ITaskQueue.java +++ b/downloadutil/src/main/java/com/arialyy/downloadutil/core/queue/ITaskQueue.java @@ -26,6 +26,11 @@ import com.arialyy.downloadutil.core.task.Task; */ public interface ITaskQueue extends IDownloader { + /** + * 获取调度器 + */ + public IDownloadSchedulers getDownloadSchedulers(); + /** * 任务池队列大小 */ diff --git a/downloadutil/src/main/java/com/arialyy/downloadutil/core/scheduler/DownloadSchedulers.java b/downloadutil/src/main/java/com/arialyy/downloadutil/core/scheduler/DownloadSchedulers.java index 889c3911..6c3c5c01 100644 --- a/downloadutil/src/main/java/com/arialyy/downloadutil/core/scheduler/DownloadSchedulers.java +++ b/downloadutil/src/main/java/com/arialyy/downloadutil/core/scheduler/DownloadSchedulers.java @@ -14,7 +14,6 @@ * limitations under the License. */ - package com.arialyy.downloadutil.core.scheduler; import android.os.Message; @@ -33,25 +32,29 @@ public class DownloadSchedulers implements IDownloadSchedulers { /** * 任务开始 */ - public static final int START = 1; + public static final int START = 1; /** * 任务停止 */ - public static final int STOP = 2; + public static final int STOP = 2; /** * 任务失败 */ - public static final int FAIL = 3; + public static final int FAIL = 3; /** * 任务取消 */ - public static final int CANCEL = 4; + public static final int CANCEL = 4; /** * 任务完成 */ - public static final int COMPLETE = 5; - private static final String TAG = "DownloadSchedulers"; - private static final Object LOCK = new Object(); + public static final int COMPLETE = 5; + /** + * 下载中 + */ + public static final int RUNNING = 6; + private static final String TAG = "DownloadSchedulers"; + private static final Object LOCK = new Object(); private static volatile DownloadSchedulers INSTANCE = null; /** * 下载失败次数 @@ -66,8 +69,8 @@ public class DownloadSchedulers implements IDownloadSchedulers { /** * 下载器任务监听 */ - OnTargetListener mTargetListener; - ITaskQueue mQueue; + OnSchedulerListener mTargetListener; + ITaskQueue mQueue; public DownloadSchedulers(ITaskQueue downloadTaskQueue) { mQueue = downloadTaskQueue; @@ -116,6 +119,9 @@ public class DownloadSchedulers implements IDownloadSchedulers { if (mTargetListener != null) { Task task = mQueue.getTask(entity); switch (state) { + case RUNNING: + mTargetListener.onTaskRunning(task); + break; case START: mTargetListener.onTaskStart(task); break; @@ -166,15 +172,14 @@ public class DownloadSchedulers implements IDownloadSchedulers { } } - /** - * 设置下载器监听 - * - * @param targetListener {@link OnTargetListener} - */ - public void setOnTargetListener(OnTargetListener targetListener) { + @Override public void regTargetListener(OnSchedulerListener targetListener) { this.mTargetListener = targetListener; } + @Override public void unRegTargetListener(OnSchedulerListener targetListener) { + mTargetListener = null; + } + public void setFailNum(int mFailNum) { this.mFailNum = mFailNum; } @@ -182,34 +187,4 @@ public class DownloadSchedulers implements IDownloadSchedulers { public void setTimeOut(long timeOut) { this.mTimeOut = timeOut; } - - /** - * Target处理任务监听 - */ - public interface OnTargetListener { - /** - * 任务开始 - */ - public void onTaskStart(Task task); - - /** - * 任务停止 - */ - public void onTaskStop(Task task); - - /** - * 任务取消 - */ - public void onTaskCancel(Task task); - - /** - * 任务下载失败 - */ - public void onTaskFail(Task task); - - /** - * 任务完成 - */ - public void onTaskComplete(Task task); - } } \ No newline at end of file diff --git a/downloadutil/src/main/java/com/arialyy/downloadutil/core/scheduler/IDownloadSchedulers.java b/downloadutil/src/main/java/com/arialyy/downloadutil/core/scheduler/IDownloadSchedulers.java index c3d08716..2c91bdba 100644 --- a/downloadutil/src/main/java/com/arialyy/downloadutil/core/scheduler/IDownloadSchedulers.java +++ b/downloadutil/src/main/java/com/arialyy/downloadutil/core/scheduler/IDownloadSchedulers.java @@ -14,7 +14,6 @@ * limitations under the License. */ - package com.arialyy.downloadutil.core.scheduler; import android.os.Handler; @@ -26,6 +25,18 @@ import com.arialyy.downloadutil.core.DownloadEntity; */ public interface IDownloadSchedulers extends Handler.Callback { + /** + * 注册下载器监听 + * + * @param targetListener {@link OnSchedulerListener} + */ + public void regTargetListener(OnSchedulerListener targetListener); + + /** + * 取消注册监听器 + */ + public void unRegTargetListener(OnSchedulerListener targetListener); + /** * 处理下载任务下载失败的情形 * diff --git a/downloadutil/src/main/java/com/arialyy/downloadutil/core/scheduler/OnSchedulerListener.java b/downloadutil/src/main/java/com/arialyy/downloadutil/core/scheduler/OnSchedulerListener.java new file mode 100644 index 00000000..313d161f --- /dev/null +++ b/downloadutil/src/main/java/com/arialyy/downloadutil/core/scheduler/OnSchedulerListener.java @@ -0,0 +1,38 @@ +package com.arialyy.downloadutil.core.scheduler; + +import com.arialyy.downloadutil.core.task.Task; + +/** + * Target处理任务监听 + */ +public interface OnSchedulerListener { + /** + * 任务开始 + */ + public void onTaskStart(Task task); + + /** + * 任务停止 + */ + public void onTaskStop(Task task); + + /** + * 任务取消 + */ + public void onTaskCancel(Task task); + + /** + * 任务下载失败 + */ + public void onTaskFail(Task task); + + /** + * 任务完成 + */ + public void onTaskComplete(Task task); + + /** + * 任务执行中 + */ + public void onTaskRunning(Task task); +} \ No newline at end of file diff --git a/downloadutil/src/main/java/com/arialyy/downloadutil/core/task/Task.java b/downloadutil/src/main/java/com/arialyy/downloadutil/core/task/Task.java index dfdc1b0d..33f0fab5 100644 --- a/downloadutil/src/main/java/com/arialyy/downloadutil/core/task/Task.java +++ b/downloadutil/src/main/java/com/arialyy/downloadutil/core/task/Task.java @@ -257,6 +257,7 @@ public class Task { } downloadEntity.setCurrentProgress(currentLocation); lastLen = currentLocation; + sendInState2Target(DownloadSchedulers.RUNNING); context.sendBroadcast(sendIntent); } } diff --git a/downloadutil/src/main/java/com/arialyy/downloadutil/util/CheckUtil.java b/downloadutil/src/main/java/com/arialyy/downloadutil/util/CheckUtil.java index 8cd11d57..85fb9795 100644 --- a/downloadutil/src/main/java/com/arialyy/downloadutil/util/CheckUtil.java +++ b/downloadutil/src/main/java/com/arialyy/downloadutil/util/CheckUtil.java @@ -17,10 +17,8 @@ package com.arialyy.downloadutil.util; -import android.content.res.Resources; import android.text.TextUtils; import android.util.Log; -import com.arialyy.downloadutil.R; import com.arialyy.downloadutil.core.DownloadEntity; import java.io.File; @@ -39,16 +37,16 @@ public class CheckUtil { */ public static boolean checkDownloadEntity(DownloadEntity entity) { if (entity == null) { - Log.w(TAG, Resources.getSystem().getString(R.string.error_entity_null)); + Log.w(TAG, "下载实体不能为空"); return false; } else if (TextUtils.isEmpty(entity.getDownloadUrl())) { - Log.w(TAG, Resources.getSystem().getString(R.string.error_download_url_null)); + Log.w(TAG, "下载链接不能为空"); return false; } else if (TextUtils.isEmpty(entity.getFileName())) { - Log.w(TAG, Resources.getSystem().getString(R.string.error_file_name_null)); + Log.w(TAG, "文件名不能为空"); return false; } else if (TextUtils.isEmpty(entity.getDownloadPath())) { - Log.w(TAG, Resources.getSystem().getString(R.string.error_download_path_null)); + Log.w(TAG, "存储地址不能为空"); return false; } String fileName = entity.getFileName(); diff --git a/downloadutil/src/main/java/com/arialyy/downloadutil/util/CommonUtil.java b/downloadutil/src/main/java/com/arialyy/downloadutil/util/CommonUtil.java index 35ad3cb7..21ffc588 100644 --- a/downloadutil/src/main/java/com/arialyy/downloadutil/util/CommonUtil.java +++ b/downloadutil/src/main/java/com/arialyy/downloadutil/util/CommonUtil.java @@ -251,6 +251,8 @@ public class CommonUtil { public static Properties loadConfig(File file) { Properties properties = new Properties(); FileInputStream fis = null; + + try { fis = new FileInputStream(file); properties.load(fis);