修改逻辑

pull/2/head
AriaLyy 8 years ago
parent 0d3c157f38
commit cd2e27d55f
  1. 17
      Aria/src/main/java/com/arialyy/aria/core/AMReceiver.java
  2. 87
      Aria/src/main/java/com/arialyy/aria/core/AMTarget.java
  3. 61
      Aria/src/main/java/com/arialyy/aria/core/Aria.java
  4. 22
      Aria/src/main/java/com/arialyy/aria/core/task/Task.java
  5. 8
      Aria/src/main/java/com/arialyy/aria/util/CheckUtil.java

@ -15,6 +15,9 @@
*/ */
package com.arialyy.aria.core; 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.DownloadSchedulers;
import com.arialyy.aria.core.scheduler.OnSchedulerListener; import com.arialyy.aria.core.scheduler.OnSchedulerListener;
@ -27,11 +30,23 @@ public class AMReceiver {
OnSchedulerListener listener; OnSchedulerListener listener;
DownloadEntity entity; DownloadEntity entity;
public AMTarget load(DownloadEntity entity) { @Deprecated public AMTarget load(DownloadEntity entity) {
this.entity = entity; this.entity = entity;
return new AMTarget(this); 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);
}
/** /**
* 添加调度器回调 * 添加调度器回调
*/ */

@ -15,10 +15,10 @@
*/ */
package com.arialyy.aria.core; 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.command.CmdFactory;
import com.arialyy.aria.core.scheduler.OnSchedulerListener;
import com.arialyy.aria.core.command.IDownloadCmd; import com.arialyy.aria.core.command.IDownloadCmd;
import com.arialyy.aria.core.task.Task;
import com.arialyy.aria.util.CommonUtil; import com.arialyy.aria.util.CommonUtil;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -28,10 +28,32 @@ import java.util.List;
* https://github.com/AriaLyy/Aria * https://github.com/AriaLyy/Aria
*/ */
public class AMTarget { public class AMTarget {
private AMReceiver receiver; private AMReceiver mReceiver;
AMTarget(AMReceiver receiver) { 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() { public void add() {
DownloadManager.getInstance() DownloadManager.getInstance()
.setCmd(CommonUtil.createCmd(receiver.obj, receiver.entity, CmdFactory.TASK_CREATE)) .setCmd(CommonUtil.createCmd(mReceiver.obj, mReceiver.entity, CmdFactory.TASK_CREATE))
.exe(); .exe();
} }
@ -48,8 +70,8 @@ public class AMTarget {
*/ */
public void start() { public void start() {
List<IDownloadCmd> cmds = new ArrayList<>(); List<IDownloadCmd> cmds = new ArrayList<>();
cmds.add(CommonUtil.createCmd(receiver.obj, receiver.entity, CmdFactory.TASK_CREATE)); cmds.add(CommonUtil.createCmd(mReceiver.obj, mReceiver.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_START));
DownloadManager.getInstance().setCmds(cmds).exe(); DownloadManager.getInstance().setCmds(cmds).exe();
cmds.clear(); cmds.clear();
} }
@ -59,7 +81,7 @@ public class AMTarget {
*/ */
public void stop() { public void stop() {
DownloadManager.getInstance() DownloadManager.getInstance()
.setCmd(CommonUtil.createCmd(receiver.obj, receiver.entity, CmdFactory.TASK_STOP)) .setCmd(CommonUtil.createCmd(mReceiver.obj, mReceiver.entity, CmdFactory.TASK_STOP))
.exe(); .exe();
} }
@ -68,7 +90,7 @@ public class AMTarget {
*/ */
public void resume() { public void resume() {
DownloadManager.getInstance() DownloadManager.getInstance()
.setCmd(CommonUtil.createCmd(receiver.obj, receiver.entity, CmdFactory.TASK_START)) .setCmd(CommonUtil.createCmd(mReceiver.obj, mReceiver.entity, CmdFactory.TASK_START))
.exe(); .exe();
} }
@ -77,7 +99,7 @@ public class AMTarget {
*/ */
public void cancel() { public void cancel() {
DownloadManager.getInstance() DownloadManager.getInstance()
.setCmd(CommonUtil.createCmd(receiver.obj, receiver.entity, CmdFactory.TASK_CANCEL)) .setCmd(CommonUtil.createCmd(mReceiver.obj, mReceiver.entity, CmdFactory.TASK_CANCEL))
.exe(); .exe();
} }
@ -85,7 +107,7 @@ public class AMTarget {
* 是否在下载 * 是否在下载
*/ */
public boolean isDownloading() { 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(); start();
} }
public static class SimpleSchedulerListener implements OnSchedulerListener { //static class Build {
// DownloadEntity entity;
@Override public void onTaskPre(Task task) { //
// Build(DownloadEntity entity) {
} // this.entity = entity;
// }
@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) {
}
}
} }

@ -25,6 +25,8 @@ import android.app.Fragment;
import android.app.Service; import android.app.Service;
import android.content.Context; import android.content.Context;
import android.os.Build; 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. * 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() { private Aria() {
} }
@ -145,4 +147,39 @@ import android.os.Build;
throw new IllegalArgumentException("这是不支持的context"); 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) {
}
}
} }

@ -44,6 +44,7 @@ public class Task {
private Handler mOutHandler; private Handler mOutHandler;
private Context mContext; private Context mContext;
private IDownloadUtil mUtil; private IDownloadUtil mUtil;
private long mSpeed;
private Task(Context context, DownloadEntity entity, Handler outHandler) { private Task(Context context, DownloadEntity entity, Handler outHandler) {
mContext = context.getApplicationContext(); mContext = context.getApplicationContext();
@ -57,6 +58,13 @@ public class Task {
mUtil = new DownloadUtil(mContext, mEntity, mListener); mUtil = new DownloadUtil(mContext, mEntity, mListener);
} }
/**
* 获取下载速度
*/
public long getSpeed() {
return mSpeed;
}
/** /**
* 开始下载 * 开始下载
*/ */
@ -151,7 +159,7 @@ public class Task {
this("", context, downloadEntity); this("", context, downloadEntity);
} }
public Builder(String targetName, Context context, DownloadEntity downloadEntity) { Builder(String targetName, Context context, DownloadEntity downloadEntity) {
this.targetName = targetName; this.targetName = targetName;
this.context = context; this.context = context;
this.downloadEntity = downloadEntity; this.downloadEntity = downloadEntity;
@ -162,7 +170,7 @@ public class Task {
* *
* @param schedulers {@link IDownloadSchedulers} * @param schedulers {@link IDownloadSchedulers}
*/ */
public Builder setOutHandler(IDownloadSchedulers schedulers) { Builder setOutHandler(IDownloadSchedulers schedulers) {
this.outHandler = new Handler(schedulers); this.outHandler = new Handler(schedulers);
return this; return this;
} }
@ -198,7 +206,7 @@ public class Task {
*/ */
private static class DListener extends DownloadListener { private static class DListener extends DownloadListener {
WeakReference<Handler> outHandler; WeakReference<Handler> outHandler;
WeakReference<Task> task; WeakReference<Task> wTask;
Context context; Context context;
Intent sendIntent; Intent sendIntent;
long INTERVAL = 1024 * 10; //10k大小的间隔 long INTERVAL = 1024 * 10; //10k大小的间隔
@ -207,12 +215,14 @@ public class Task {
long INTERVAL_TIME = 1000; //1m更新周期 long INTERVAL_TIME = 1000; //1m更新周期
boolean isFirst = true; boolean isFirst = true;
DownloadEntity downloadEntity; DownloadEntity downloadEntity;
Task task;
DListener(Context context, Task task, Handler outHandler) { DListener(Context context, Task task, Handler outHandler) {
this.context = context; this.context = context;
this.outHandler = new WeakReference<>(outHandler); this.outHandler = new WeakReference<>(outHandler);
this.task = new WeakReference<>(task); this.wTask = new WeakReference<>(task);
this.downloadEntity = this.task.get().getDownloadEntity(); task = wTask.get();
this.downloadEntity = this.task.getDownloadEntity();
sendIntent = CommonUtil.createIntent(context.getPackageName(), Aria.ACTION_RUNNING); sendIntent = CommonUtil.createIntent(context.getPackageName(), Aria.ACTION_RUNNING);
sendIntent.putExtra(Aria.ENTITY, downloadEntity); sendIntent.putExtra(Aria.ENTITY, downloadEntity);
} }
@ -307,7 +317,7 @@ public class Task {
*/ */
private void sendInState2Target(int state) { private void sendInState2Target(int state) {
if (outHandler.get() != null) { if (outHandler.get() != null) {
outHandler.get().obtainMessage(state, task.get()).sendToTarget(); outHandler.get().obtainMessage(state, task).sendToTarget();
} }
} }

@ -14,7 +14,6 @@
* limitations under the License. * limitations under the License.
*/ */
package com.arialyy.aria.util; package com.arialyy.aria.util;
import android.text.TextUtils; import android.text.TextUtils;
@ -43,11 +42,10 @@ public class CheckUtil {
Log.w(TAG, "下载链接不能为空"); Log.w(TAG, "下载链接不能为空");
return false; return false;
} else if (TextUtils.isEmpty(entity.getFileName())) { } else if (TextUtils.isEmpty(entity.getFileName())) {
Log.w(TAG, "文件名不能为空"); //Log.w(TAG, "文件名不能为空");
return false; throw new IllegalArgumentException("文件名不能为null");
} else if (TextUtils.isEmpty(entity.getDownloadPath())) { } else if (TextUtils.isEmpty(entity.getDownloadPath())) {
Log.w(TAG, "存储地址不能为空"); throw new IllegalArgumentException("文件保存路径不能为null");
return false;
} }
String fileName = entity.getFileName(); String fileName = entity.getFileName();
if (fileName.contains(" ")) { if (fileName.contains(" ")) {

Loading…
Cancel
Save