From 8a49447fa3e322d824debd9a24ac0e592add588b Mon Sep 17 00:00:00 2001 From: AriaLyy <511455842@qq.com> Date: Thu, 1 Dec 2016 18:12:49 +0800 Subject: [PATCH] =?UTF-8?q?=E9=80=BB=E8=BE=91=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../simple/activity/SingleTaskActivity.java | 9 +- .../arialyy/downloadutil/core/AMEntity.java | 9 ++ .../com/arialyy/downloadutil/core/Aria.java | 105 ++++++++++++++++++ .../downloadutil/core/AriaManager.java | 36 ++++++ .../downloadutil/core/DownloadManager.java | 10 -- .../core/scheduler/DownloadSchedulers.java | 43 +++++-- .../core/scheduler/IDownloadSchedulers.java | 7 +- 7 files changed, 186 insertions(+), 33 deletions(-) create mode 100644 downloadutil/src/main/java/com/arialyy/downloadutil/core/AMEntity.java create mode 100644 downloadutil/src/main/java/com/arialyy/downloadutil/core/Aria.java create mode 100644 downloadutil/src/main/java/com/arialyy/downloadutil/core/AriaManager.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 66d160bc..4acabf89 100644 --- a/app/src/main/java/com/arialyy/simple/activity/SingleTaskActivity.java +++ b/app/src/main/java/com/arialyy/simple/activity/SingleTaskActivity.java @@ -17,9 +17,6 @@ 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; @@ -39,13 +36,9 @@ 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; import com.arialyy.simple.R; import com.arialyy.simple.base.BaseActivity; import com.arialyy.simple.databinding.ActivitySingleBinding; -import com.arialyy.simple.module.DownloadModule; -import java.util.ArrayList; -import java.util.List; public class SingleTaskActivity extends BaseActivity { public static final int DOWNLOAD_PRE = 0x01; @@ -207,7 +200,7 @@ public class SingleTaskActivity extends BaseActivity { //commands.add(startCmd); //mManager.setCmds(commands).exe(); mManager.setCmd(CmdFactory.getInstance().createCmd(mEntity, CmdFactory.TASK_SINGLE)) - .regSchedulerListener(new OnSchedulerListener() { + .addSchedulerListener(new OnSchedulerListener() { @Override public void onTaskStart(Task task) { mUpdateHandler.obtainMessage(DOWNLOAD_PRE, task.getDownloadEntity().getFileSize()) .sendToTarget(); diff --git a/downloadutil/src/main/java/com/arialyy/downloadutil/core/AMEntity.java b/downloadutil/src/main/java/com/arialyy/downloadutil/core/AMEntity.java new file mode 100644 index 00000000..0a6bdf40 --- /dev/null +++ b/downloadutil/src/main/java/com/arialyy/downloadutil/core/AMEntity.java @@ -0,0 +1,9 @@ +package com.arialyy.downloadutil.core; + +import android.content.Context; +import com.arialyy.downloadutil.core.scheduler.OnSchedulerListener; + +public class AMEntity { + Context context; + OnSchedulerListener listener; +} \ No newline at end of file diff --git a/downloadutil/src/main/java/com/arialyy/downloadutil/core/Aria.java b/downloadutil/src/main/java/com/arialyy/downloadutil/core/Aria.java new file mode 100644 index 00000000..1bcf97a3 --- /dev/null +++ b/downloadutil/src/main/java/com/arialyy/downloadutil/core/Aria.java @@ -0,0 +1,105 @@ +/* + * 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.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. + */ +@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1) +public class Aria { + private static final Object LOCK = new Object(); + private static volatile Aria INSTANCE = null; + private DownloadManager mDownloadManager; + + private Aria() { + //mDownloadManager = DownloadManager.getInstance(); + } + + private Aria(Context context) { + mDownloadManager = DownloadManager.init(context); + } + + public static Aria get(Context context) { + if (INSTANCE == null) { + synchronized (LOCK) { + INSTANCE = new Aria(); + } + } + return INSTANCE; + } + + /** + * 开始下载 + */ + public Aria start(DownloadEntity entity) { + List 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); + } +} diff --git a/downloadutil/src/main/java/com/arialyy/downloadutil/core/AriaManager.java b/downloadutil/src/main/java/com/arialyy/downloadutil/core/AriaManager.java new file mode 100644 index 00000000..7a9814b8 --- /dev/null +++ b/downloadutil/src/main/java/com/arialyy/downloadutil/core/AriaManager.java @@ -0,0 +1,36 @@ +package com.arialyy.downloadutil.core; + +import android.content.Context; +import com.arialyy.downloadutil.core.scheduler.OnSchedulerListener; +import java.util.HashMap; +import java.util.Map; + +/** + * Created by lyy on 2016/12/1. + */ +public class AriaManager { + private Map mAria = new HashMap<>(); + + private static final Object LOCK = new Object(); + private static volatile AriaManager INSTANCE = null; + + private AriaManager(){ + } + + public static AriaManager getInstance() { + if (INSTANCE == null) { + synchronized (LOCK) { + INSTANCE = new AriaManager(); + } + } + return INSTANCE; + } + + public synchronized void get(Context context){ + + } + + + + +} 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 c9d105d6..bf68a2d7 100644 --- a/downloadutil/src/main/java/com/arialyy/downloadutil/core/DownloadManager.java +++ b/downloadutil/src/main/java/com/arialyy/downloadutil/core/DownloadManager.java @@ -22,7 +22,6 @@ 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; @@ -124,15 +123,6 @@ 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/scheduler/DownloadSchedulers.java b/downloadutil/src/main/java/com/arialyy/downloadutil/core/scheduler/DownloadSchedulers.java index 6c3c5c01..747f93f6 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 @@ -16,13 +16,19 @@ 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; 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; /** * Created by lyy on 2016/8/16. @@ -69,7 +75,8 @@ public class DownloadSchedulers implements IDownloadSchedulers { /** * 下载器任务监听 */ - OnSchedulerListener mTargetListener; + OnSchedulerListener mSchedulerListener; + Map mSchedulerListeners = new HashMap<>(); ITaskQueue mQueue; public DownloadSchedulers(ITaskQueue downloadTaskQueue) { @@ -116,26 +123,38 @@ public class DownloadSchedulers implements IDownloadSchedulers { * @param entity 下载实体 */ private void callback(int state, DownloadEntity entity) { - if (mTargetListener != null) { + if (mSchedulerListeners.size() > 0) { + //Set> + for (Map.Entry entry : mSchedulerListeners.entrySet()) { + callback(state, entity, entry.getValue()); + } + } + } + + private void callback(int state, DownloadEntity entity, OnSchedulerListener listener) { + if (listener != null) { Task task = mQueue.getTask(entity); switch (state) { case RUNNING: - mTargetListener.onTaskRunning(task); + listener.onTaskRunning(task); break; case START: - mTargetListener.onTaskStart(task); + listener.onTaskStart(task); break; case STOP: - mTargetListener.onTaskStop(task); + listener.onTaskStop(task); break; case CANCEL: - mTargetListener.onTaskCancel(task); + listener.onTaskCancel(task); + removeSchedulerListener(listener); break; case COMPLETE: - mTargetListener.onTaskComplete(task); + listener.onTaskComplete(task); + removeSchedulerListener(listener); break; case FAIL: - mTargetListener.onTaskFail(task); + listener.onTaskFail(task); + removeSchedulerListener(listener); break; } } @@ -172,12 +191,12 @@ public class DownloadSchedulers implements IDownloadSchedulers { } } - @Override public void regTargetListener(OnSchedulerListener targetListener) { - this.mTargetListener = targetListener; + @Override public void addSchedulerListener(Context context, OnSchedulerListener schedulerListener) { + mSchedulerListeners.put(schedulerListener.hashCode(), schedulerListener); } - @Override public void unRegTargetListener(OnSchedulerListener targetListener) { - mTargetListener = null; + @Override public void removeSchedulerListener(OnSchedulerListener schedulerListener) { + mSchedulerListeners.remove(schedulerListener.hashCode()); } public void setFailNum(int mFailNum) { 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 2c91bdba..c9e72b08 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 @@ -16,6 +16,7 @@ package com.arialyy.downloadutil.core.scheduler; +import android.content.Context; import android.os.Handler; import com.arialyy.downloadutil.core.DownloadEntity; @@ -28,14 +29,14 @@ public interface IDownloadSchedulers extends Handler.Callback { /** * 注册下载器监听 * - * @param targetListener {@link OnSchedulerListener} + * @param schedulerListener {@link OnSchedulerListener} */ - public void regTargetListener(OnSchedulerListener targetListener); + public void addSchedulerListener(Context context, OnSchedulerListener schedulerListener); /** * 取消注册监听器 */ - public void unRegTargetListener(OnSchedulerListener targetListener); + public void removeSchedulerListener(OnSchedulerListener schedulerListener); /** * 处理下载任务下载失败的情形