diff --git a/Aria/src/main/java/com/arialyy/aria/core/AMReceiver.java b/Aria/src/main/java/com/arialyy/aria/core/AMReceiver.java index f73d3b42..c08ff3eb 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/AMReceiver.java +++ b/Aria/src/main/java/com/arialyy/aria/core/AMReceiver.java @@ -15,6 +15,9 @@ */ package com.arialyy.aria.core; +import android.os.Build; +import android.support.annotation.NonNull; +import android.text.TextUtils; import com.arialyy.aria.core.scheduler.DownloadSchedulers; import com.arialyy.aria.core.scheduler.OnSchedulerListener; @@ -27,11 +30,23 @@ public class AMReceiver { OnSchedulerListener listener; DownloadEntity entity; - public AMTarget load(DownloadEntity entity) { + @Deprecated public AMTarget load(DownloadEntity entity) { this.entity = entity; return new AMTarget(this); } + /** + * 读取下载链接 + */ + public AMTarget load(@NonNull String downloadUrl) { + if (TextUtils.isEmpty(downloadUrl)) { + throw new IllegalArgumentException("下载链接不能为null"); + } + entity = new DownloadEntity(); + entity.setDownloadUrl(downloadUrl); + return new AMTarget(this); + } + /** * 添加调度器回调 */ diff --git a/Aria/src/main/java/com/arialyy/aria/core/AMTarget.java b/Aria/src/main/java/com/arialyy/aria/core/AMTarget.java index 6c9b23a4..54f18861 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/AMTarget.java +++ b/Aria/src/main/java/com/arialyy/aria/core/AMTarget.java @@ -15,10 +15,10 @@ */ package com.arialyy.aria.core; +import android.support.annotation.NonNull; +import android.text.TextUtils; import com.arialyy.aria.core.command.CmdFactory; -import com.arialyy.aria.core.scheduler.OnSchedulerListener; import com.arialyy.aria.core.command.IDownloadCmd; -import com.arialyy.aria.core.task.Task; import com.arialyy.aria.util.CommonUtil; import java.util.ArrayList; import java.util.List; @@ -28,10 +28,32 @@ import java.util.List; * https://github.com/AriaLyy/Aria */ public class AMTarget { - private AMReceiver receiver; + private AMReceiver mReceiver; AMTarget(AMReceiver receiver) { - this.receiver = receiver; + this.mReceiver = receiver; + } + + /** + * 设置文件存储路径 + */ + public AMTarget setDownloadPath(@NonNull String downloadPath) { + if (TextUtils.isEmpty(downloadPath)) { + throw new IllegalArgumentException("文件保持路径不能为null"); + } + mReceiver.entity.setDownloadPath(downloadPath); + return this; + } + + /** + * 设置文件名 + */ + public AMTarget setDownloadName(@NonNull String downloadName) { + if (TextUtils.isEmpty(downloadName)) { + throw new IllegalArgumentException("文件名不能为null"); + } + mReceiver.entity.setFileName(downloadName); + return this; } /** @@ -39,7 +61,7 @@ public class AMTarget { */ public void add() { DownloadManager.getInstance() - .setCmd(CommonUtil.createCmd(receiver.obj, receiver.entity, CmdFactory.TASK_CREATE)) + .setCmd(CommonUtil.createCmd(mReceiver.obj, mReceiver.entity, CmdFactory.TASK_CREATE)) .exe(); } @@ -48,8 +70,8 @@ public class AMTarget { */ public void start() { List cmds = new ArrayList<>(); - cmds.add(CommonUtil.createCmd(receiver.obj, receiver.entity, CmdFactory.TASK_CREATE)); - cmds.add(CommonUtil.createCmd(receiver.obj, receiver.entity, CmdFactory.TASK_START)); + cmds.add(CommonUtil.createCmd(mReceiver.obj, mReceiver.entity, CmdFactory.TASK_CREATE)); + cmds.add(CommonUtil.createCmd(mReceiver.obj, mReceiver.entity, CmdFactory.TASK_START)); DownloadManager.getInstance().setCmds(cmds).exe(); cmds.clear(); } @@ -59,7 +81,7 @@ public class AMTarget { */ public void stop() { DownloadManager.getInstance() - .setCmd(CommonUtil.createCmd(receiver.obj, receiver.entity, CmdFactory.TASK_STOP)) + .setCmd(CommonUtil.createCmd(mReceiver.obj, mReceiver.entity, CmdFactory.TASK_STOP)) .exe(); } @@ -68,7 +90,7 @@ public class AMTarget { */ public void resume() { DownloadManager.getInstance() - .setCmd(CommonUtil.createCmd(receiver.obj, receiver.entity, CmdFactory.TASK_START)) + .setCmd(CommonUtil.createCmd(mReceiver.obj, mReceiver.entity, CmdFactory.TASK_START)) .exe(); } @@ -77,7 +99,7 @@ public class AMTarget { */ public void cancel() { DownloadManager.getInstance() - .setCmd(CommonUtil.createCmd(receiver.obj, receiver.entity, CmdFactory.TASK_CANCEL)) + .setCmd(CommonUtil.createCmd(mReceiver.obj, mReceiver.entity, CmdFactory.TASK_CANCEL)) .exe(); } @@ -85,7 +107,7 @@ public class AMTarget { * 是否在下载 */ public boolean isDownloading() { - return DownloadManager.getInstance().getTaskQueue().getTask(receiver.entity).isDownloading(); + return DownloadManager.getInstance().getTaskQueue().getTask(mReceiver.entity).isDownloading(); } /** @@ -96,38 +118,13 @@ public class AMTarget { start(); } - 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) { - - } - } + //static class Build { + // DownloadEntity entity; + // + // Build(DownloadEntity entity) { + // this.entity = entity; + // } + // + // + //} } diff --git a/Aria/src/main/java/com/arialyy/aria/core/Aria.java b/Aria/src/main/java/com/arialyy/aria/core/Aria.java index 3fce3d74..705352d3 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/Aria.java +++ b/Aria/src/main/java/com/arialyy/aria/core/Aria.java @@ -25,6 +25,8 @@ import android.app.Fragment; import android.app.Service; import android.content.Context; import android.os.Build; +import com.arialyy.aria.core.scheduler.OnSchedulerListener; +import com.arialyy.aria.core.task.Task; /** * Created by lyy on 2016/12/1. @@ -46,51 +48,51 @@ import android.os.Build; /** * 预处理完成 */ - 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"; + public static final String CURRENT_SPEED = "CURRENT_SPEED"; private Aria() { } @@ -145,4 +147,39 @@ import android.os.Build; throw new IllegalArgumentException("这是不支持的context"); } } + + 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) { + + } + } } diff --git a/Aria/src/main/java/com/arialyy/aria/core/task/Task.java b/Aria/src/main/java/com/arialyy/aria/core/task/Task.java index 141fd8d0..5420362b 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/task/Task.java +++ b/Aria/src/main/java/com/arialyy/aria/core/task/Task.java @@ -44,6 +44,7 @@ public class Task { private Handler mOutHandler; private Context mContext; private IDownloadUtil mUtil; + private long mSpeed; private Task(Context context, DownloadEntity entity, Handler outHandler) { mContext = context.getApplicationContext(); @@ -57,6 +58,13 @@ public class Task { mUtil = new DownloadUtil(mContext, mEntity, mListener); } + /** + * 获取下载速度 + */ + public long getSpeed() { + return mSpeed; + } + /** * 开始下载 */ @@ -151,7 +159,7 @@ public class Task { this("", context, downloadEntity); } - public Builder(String targetName, Context context, DownloadEntity downloadEntity) { + Builder(String targetName, Context context, DownloadEntity downloadEntity) { this.targetName = targetName; this.context = context; this.downloadEntity = downloadEntity; @@ -162,7 +170,7 @@ public class Task { * * @param schedulers {@link IDownloadSchedulers} */ - public Builder setOutHandler(IDownloadSchedulers schedulers) { + Builder setOutHandler(IDownloadSchedulers schedulers) { this.outHandler = new Handler(schedulers); return this; } @@ -198,7 +206,7 @@ public class Task { */ private static class DListener extends DownloadListener { WeakReference outHandler; - WeakReference task; + WeakReference wTask; Context context; Intent sendIntent; long INTERVAL = 1024 * 10; //10k大小的间隔 @@ -207,12 +215,14 @@ public class Task { long INTERVAL_TIME = 1000; //1m更新周期 boolean isFirst = true; DownloadEntity downloadEntity; + Task task; DListener(Context context, Task task, Handler outHandler) { this.context = context; this.outHandler = new WeakReference<>(outHandler); - this.task = new WeakReference<>(task); - this.downloadEntity = this.task.get().getDownloadEntity(); + this.wTask = new WeakReference<>(task); + task = wTask.get(); + this.downloadEntity = this.task.getDownloadEntity(); sendIntent = CommonUtil.createIntent(context.getPackageName(), Aria.ACTION_RUNNING); sendIntent.putExtra(Aria.ENTITY, downloadEntity); } @@ -307,7 +317,7 @@ public class Task { */ private void sendInState2Target(int state) { if (outHandler.get() != null) { - outHandler.get().obtainMessage(state, task.get()).sendToTarget(); + outHandler.get().obtainMessage(state, task).sendToTarget(); } } diff --git a/Aria/src/main/java/com/arialyy/aria/util/CheckUtil.java b/Aria/src/main/java/com/arialyy/aria/util/CheckUtil.java index ba70cda1..26ad8fe3 100644 --- a/Aria/src/main/java/com/arialyy/aria/util/CheckUtil.java +++ b/Aria/src/main/java/com/arialyy/aria/util/CheckUtil.java @@ -14,7 +14,6 @@ * limitations under the License. */ - package com.arialyy.aria.util; import android.text.TextUtils; @@ -43,11 +42,10 @@ public class CheckUtil { Log.w(TAG, "下载链接不能为空"); return false; } else if (TextUtils.isEmpty(entity.getFileName())) { - Log.w(TAG, "文件名不能为空"); - return false; + //Log.w(TAG, "文件名不能为空"); + throw new IllegalArgumentException("文件名不能为null"); } else if (TextUtils.isEmpty(entity.getDownloadPath())) { - Log.w(TAG, "存储地址不能为空"); - return false; + throw new IllegalArgumentException("文件保存路径不能为null"); } String fileName = entity.getFileName(); if (fileName.contains(" ")) {