Apt 下载事件完成

pull/330/head
AriaLyy 8 years ago
parent 8049af34e0
commit 603a21fe43
  1. 3
      Aria/src/main/java/com/arialyy/aria/core/AriaManager.java
  2. 10
      Aria/src/main/java/com/arialyy/aria/core/download/DownloadReceiver.java
  3. 5
      Aria/src/main/java/com/arialyy/aria/core/inf/IReceiver.java
  4. 8
      Aria/src/main/java/com/arialyy/aria/core/scheduler/AbsSchedulerListener.java
  5. 27
      Aria/src/main/java/com/arialyy/aria/core/scheduler/AbsSchedulers.java
  6. 8
      Aria/src/main/java/com/arialyy/aria/core/scheduler/ISchedulers.java
  7. 15
      Aria/src/main/java/com/arialyy/aria/core/upload/UploadReceiver.java
  8. 31
      AriaAnnotations/src/main/java/com/arialyy/annotations/Download.java
  9. 34
      AriaAnnotations/src/main/java/com/arialyy/annotations/Upload.java
  10. 9
      AriaCompiler/src/main/java/com/arialyy/compiler/ElementHandle.java
  11. 128
      app/src/main/java/com/arialyy/simple/download/SingleTaskActivity.java
  12. 66
      app/src/main/java/com/arialyy/simple/download/multi_download/MultiDownloadActivity.java
  13. 53
      app/src/main/java/com/arialyy/simple/download/multi_download/MultiTaskActivity.java
  14. 57
      app/src/main/java/com/arialyy/simple/upload/UploadActivity.java

@ -176,11 +176,13 @@ import org.xml.sax.SAXException;
if (isDownload) { if (isDownload) {
DownloadReceiver dReceiver = new DownloadReceiver(); DownloadReceiver dReceiver = new DownloadReceiver();
dReceiver.targetName = obj.getClass().getName(); dReceiver.targetName = obj.getClass().getName();
dReceiver.obj = obj;
mReceivers.put(key, dReceiver); mReceivers.put(key, dReceiver);
receiver = dReceiver; receiver = dReceiver;
} else { } else {
UploadReceiver uReceiver = new UploadReceiver(); UploadReceiver uReceiver = new UploadReceiver();
uReceiver.targetName = obj.getClass().getName(); uReceiver.targetName = obj.getClass().getName();
uReceiver.obj = obj;
mReceivers.put(key, uReceiver); mReceivers.put(key, uReceiver);
receiver = uReceiver; receiver = uReceiver;
} }
@ -303,6 +305,7 @@ import org.xml.sax.SAXException;
if (key.contains(clsName)) { if (key.contains(clsName)) {
IReceiver receiver = mReceivers.get(key); IReceiver receiver = mReceivers.get(key);
receiver.removeSchedulerListener(); receiver.removeSchedulerListener();
receiver.unRegister();
receiver.destroy(); receiver.destroy();
iter.remove(); iter.remove();
break; break;

@ -36,6 +36,7 @@ import java.util.Set;
public class DownloadReceiver implements IReceiver<DownloadEntity> { public class DownloadReceiver implements IReceiver<DownloadEntity> {
private static final String TAG = "DownloadReceiver"; private static final String TAG = "DownloadReceiver";
public String targetName; public String targetName;
public Object obj;
public ISchedulerListener<DownloadTask> listener; public ISchedulerListener<DownloadTask> listener;
/** /**
@ -72,16 +73,16 @@ public class DownloadReceiver implements IReceiver<DownloadEntity> {
* 将当前类注册到Aria * 将当前类注册到Aria
*/ */
public DownloadReceiver register() { public DownloadReceiver register() {
DownloadSchedulers.getInstance().register(targetName); DownloadSchedulers.getInstance().register(obj);
return this; return this;
} }
/** /**
* 取消注册 * 取消注册
*/ */
public DownloadReceiver unRegister() { @Override
DownloadSchedulers.getInstance().unRegister(targetName); public void unRegister() {
return this; DownloadSchedulers.getInstance().unRegister(obj);
} }
/** /**
@ -149,6 +150,7 @@ public class DownloadReceiver implements IReceiver<DownloadEntity> {
for (String key : keys) { for (String key : keys) {
IReceiver receiver = ariaManager.getReceiver().get(key); IReceiver receiver = ariaManager.getReceiver().get(key);
receiver.removeSchedulerListener(); receiver.removeSchedulerListener();
receiver.unRegister();
ariaManager.getReceiver().remove(key); ariaManager.getReceiver().remove(key);
} }
} }

@ -31,6 +31,11 @@ public interface IReceiver<ENTITY extends IEntity> {
*/ */
public void removeSchedulerListener(); public void removeSchedulerListener();
/**
* 移除观察者
*/
public void unRegister();
/** /**
* 停止所有任务 * 停止所有任务
*/ */

@ -56,4 +56,12 @@ public class AbsSchedulerListener<TASK extends ITask> implements ISchedulerListe
@Override public void onTaskRunning(TASK task) { @Override public void onTaskRunning(TASK task) {
} }
public void onNoSupportBreakPoint(TASK task) {
}
public void setListener(Object obj) {
}
} }

@ -49,7 +49,7 @@ public abstract class AbsSchedulers<TASK_ENTITY extends AbsTaskEntity, ENTITY ex
/** /**
* @param targetName 观察者创建该监听器的对象类名 * @param targetName 观察者创建该监听器的对象类名
* @param schedulerListener {@link ISchedulerListener} * @param schedulerListener {@link ISchedulerListener}
* @see #register(String) * @see #register(Object)
*/ */
@Deprecated @Override public void addSchedulerListener(String targetName, @Deprecated @Override public void addSchedulerListener(String targetName,
ISchedulerListener<TASK> schedulerListener) { ISchedulerListener<TASK> schedulerListener) {
@ -58,7 +58,7 @@ public abstract class AbsSchedulers<TASK_ENTITY extends AbsTaskEntity, ENTITY ex
/** /**
* @param targetName 观察者创建该监听器的对象类名 * @param targetName 观察者创建该监听器的对象类名
* @see #unRegister(String) * @see #unRegister(Object)
*/ */
@Override public void removeSchedulerListener(String targetName, @Override public void removeSchedulerListener(String targetName,
ISchedulerListener<TASK> schedulerListener) { ISchedulerListener<TASK> schedulerListener) {
@ -70,18 +70,21 @@ public abstract class AbsSchedulers<TASK_ENTITY extends AbsTaskEntity, ENTITY ex
} }
} }
@Override public void register(String targetName) { @Override public void register(Object obj) {
String targetName = obj.getClass().getName();
AbsSchedulerListener<TASK> listener = mObservers.get(targetName); AbsSchedulerListener<TASK> listener = mObservers.get(targetName);
if (listener == null) { if (listener == null) {
mObservers.put(targetName, createListener(targetName)); listener = createListener(targetName);
listener.setListener(obj);
mObservers.put(targetName, listener);
} }
} }
@Override public void unRegister(String targetName) { @Override public void unRegister(Object obj) {
for (Iterator<Map.Entry<String, AbsSchedulerListener<TASK>>> iter = for (Iterator<Map.Entry<String, AbsSchedulerListener<TASK>>> iter =
mObservers.entrySet().iterator(); iter.hasNext(); ) { mObservers.entrySet().iterator(); iter.hasNext(); ) {
Map.Entry<String, AbsSchedulerListener<TASK>> entry = iter.next(); Map.Entry<String, AbsSchedulerListener<TASK>> entry = iter.next();
if (entry.getKey().equals(targetName)) iter.remove(); if (entry.getKey().equals(obj.getClass().getName())) iter.remove();
} }
} }
@ -151,12 +154,12 @@ public abstract class AbsSchedulers<TASK_ENTITY extends AbsTaskEntity, ENTITY ex
if (mSchedulerListeners.size() > 0) { if (mSchedulerListeners.size() > 0) {
Set<String> keys = mSchedulerListeners.keySet(); Set<String> keys = mSchedulerListeners.keySet();
for (String key : keys) { for (String key : keys) {
if (mSchedulerListeners.size() > 0) { callback(state, task, mSchedulerListeners.get(key));
callback(state, task, mSchedulerListeners.get(key)); }
} } else if (mObservers.size() > 0) {
if (mObservers.size() > 0) { Set<String> keys = mObservers.keySet();
callback(state, task, mObservers.get(key)); for (String key : keys) {
} callback(state, task, mObservers.get(key));
} }
} }
} }

@ -84,14 +84,14 @@ public interface ISchedulers<Task extends ITask> extends Handler.Callback {
/** /**
* 将当前类注册到Aria * 将当前类注册到Aria
* *
* @param targetName 类完整路径 * @param obj 观察者类
*/ */
public void register(String targetName); public void register(Object obj);
/** /**
* 移除注册 * 移除注册
* *
* @param targetName 类完整路径 * @param obj 观察者类
*/ */
public void unRegister(String targetName); public void unRegister(Object obj);
} }

@ -19,8 +19,10 @@ import android.support.annotation.NonNull;
import com.arialyy.aria.core.AriaManager; import com.arialyy.aria.core.AriaManager;
import com.arialyy.aria.core.command.AbsCmd; import com.arialyy.aria.core.command.AbsCmd;
import com.arialyy.aria.core.command.CmdFactory; import com.arialyy.aria.core.command.CmdFactory;
import com.arialyy.aria.core.download.DownloadReceiver;
import com.arialyy.aria.core.inf.IEntity; import com.arialyy.aria.core.inf.IEntity;
import com.arialyy.aria.core.inf.IReceiver; import com.arialyy.aria.core.inf.IReceiver;
import com.arialyy.aria.core.scheduler.DownloadSchedulers;
import com.arialyy.aria.core.scheduler.ISchedulerListener; import com.arialyy.aria.core.scheduler.ISchedulerListener;
import com.arialyy.aria.core.scheduler.UploadSchedulers; import com.arialyy.aria.core.scheduler.UploadSchedulers;
import com.arialyy.aria.orm.DbEntity; import com.arialyy.aria.orm.DbEntity;
@ -38,6 +40,7 @@ import java.util.regex.Pattern;
public class UploadReceiver implements IReceiver<UploadEntity> { public class UploadReceiver implements IReceiver<UploadEntity> {
private static final String TAG = "DownloadReceiver"; private static final String TAG = "DownloadReceiver";
public String targetName; public String targetName;
public Object obj;
public ISchedulerListener<UploadTask> listener; public ISchedulerListener<UploadTask> listener;
/** /**
@ -127,4 +130,16 @@ public class UploadReceiver implements IReceiver<UploadEntity> {
UploadSchedulers.getInstance().removeSchedulerListener(targetName, listener); UploadSchedulers.getInstance().removeSchedulerListener(targetName, listener);
} }
} }
/**
* 将当前类注册到Aria
*/
public UploadReceiver register() {
UploadSchedulers.getInstance().register(obj);
return this;
}
@Override public void unRegister() {
UploadSchedulers.getInstance().unRegister(obj);
}
} }

@ -22,35 +22,66 @@ import java.lang.annotation.Target;
/** /**
* Created by Aria.Lao on 2017/6/6. * Created by Aria.Lao on 2017/6/6.
* 下载注解
*/ */
@Retention(RetentionPolicy.CLASS) @Target(ElementType.METHOD) public @interface Download { @Retention(RetentionPolicy.CLASS) @Target(ElementType.METHOD) public @interface Download {
/**
* 如果你在方法中添加{@code @Download.onPre}注解在预处理完成时Aria会调用该方法
*/
@Retention(RetentionPolicy.CLASS) @Target(ElementType.METHOD) public @interface onPre { @Retention(RetentionPolicy.CLASS) @Target(ElementType.METHOD) public @interface onPre {
} }
/**
* 如果你在方法中添加{@code @Download.onTaskPre}注解在任务预处理完成时Aria会调用该方法
*/
@Retention(RetentionPolicy.CLASS) @Target(ElementType.METHOD) public @interface onTaskPre { @Retention(RetentionPolicy.CLASS) @Target(ElementType.METHOD) public @interface onTaskPre {
} }
/**
* 如果你在方法中添加{@code @Download.onTaskResume}注解在任务恢复下载时Aria会调用该方法
*/
@Retention(RetentionPolicy.CLASS) @Target(ElementType.METHOD) public @interface onTaskResume { @Retention(RetentionPolicy.CLASS) @Target(ElementType.METHOD) public @interface onTaskResume {
} }
/**
* 如果你在方法中添加{@code @Download.onTaskStart}注解在任务开始下载时Aria会调用该方法
*/
@Retention(RetentionPolicy.CLASS) @Target(ElementType.METHOD) public @interface onTaskStart { @Retention(RetentionPolicy.CLASS) @Target(ElementType.METHOD) public @interface onTaskStart {
} }
/**
* 如果你在方法中添加{@code @Download.onTaskStop}注解在任务停止时Aria会调用该方法
*/
@Retention(RetentionPolicy.CLASS) @Target(ElementType.METHOD) public @interface onTaskStop { @Retention(RetentionPolicy.CLASS) @Target(ElementType.METHOD) public @interface onTaskStop {
} }
/**
* 如果你在方法中添加{@code @Download.onTaskCancel}l注解在任务取消时Aria会调用该方法
*/
@Retention(RetentionPolicy.CLASS) @Target(ElementType.METHOD) public @interface onTaskCancel { @Retention(RetentionPolicy.CLASS) @Target(ElementType.METHOD) public @interface onTaskCancel {
} }
/**
* 如果你在方法中添加{@code @Download.onTaskFail)注解在任务预失败时Aria会调用该方法
*/
@Retention(RetentionPolicy.CLASS) @Target(ElementType.METHOD) public @interface onTaskFail { @Retention(RetentionPolicy.CLASS) @Target(ElementType.METHOD) public @interface onTaskFail {
} }
/**
* 如果你在方法中添加{@code @Download.onTaskComplete}注解在任务完成时Aria会调用该方法
*/
@Retention(RetentionPolicy.CLASS) @Target(ElementType.METHOD) public @interface onTaskComplete { @Retention(RetentionPolicy.CLASS) @Target(ElementType.METHOD) public @interface onTaskComplete {
} }
/**
* 如果你在方法中添加{@code @Download.onTaskRunning}注解在任务正在下载Aria会调用该方法
*/
@Retention(RetentionPolicy.CLASS) @Target(ElementType.METHOD) public @interface onTaskRunning { @Retention(RetentionPolicy.CLASS) @Target(ElementType.METHOD) public @interface onTaskRunning {
} }
/**
* 如果你在方法中添加{@code @Download.onNoSupportBreakPoint}注解如果该任务不支持断点Aria会调用该方法
*/
@Retention(RetentionPolicy.CLASS) @Target(ElementType.METHOD) @Retention(RetentionPolicy.CLASS) @Target(ElementType.METHOD)
public @interface onNoSupportBreakPoint { public @interface onNoSupportBreakPoint {
} }

@ -21,36 +21,68 @@ import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target; import java.lang.annotation.Target;
/** /**
* Created by Aria.Lao on 2017/6/6. * Created by lyy on 2017/6/6.
* Aria下载事件注解
*/ */
@Retention(RetentionPolicy.CLASS) @Target(ElementType.METHOD) public @interface Upload { @Retention(RetentionPolicy.CLASS) @Target(ElementType.METHOD) public @interface Upload {
/**
* 如果你在方法中添加{@code @Upload.onPre}注解在预处理完成时Aria会调用该方法
*/
@Retention(RetentionPolicy.CLASS) @Target(ElementType.METHOD) public @interface onPre { @Retention(RetentionPolicy.CLASS) @Target(ElementType.METHOD) public @interface onPre {
} }
/**
* 如果你在方法中添加{@code @Upload.onTaskPre}注解在任务预处理完成时Aria会调用该方法
*/
@Retention(RetentionPolicy.CLASS) @Target(ElementType.METHOD) public @interface onTaskPre { @Retention(RetentionPolicy.CLASS) @Target(ElementType.METHOD) public @interface onTaskPre {
} }
/**
* 如果你在方法中添加{@code @Upload.onTaskResume}注解在任务恢复下载时Aria会调用该方法
*/
//@Retention(RetentionPolicy.CLASS) @Target(ElementType.METHOD) public @interface onTaskResume { //@Retention(RetentionPolicy.CLASS) @Target(ElementType.METHOD) public @interface onTaskResume {
//} //}
/**
* 如果你在方法中添加{@code @Upload.onTaskStart}注解在任务开始下载时Aria会调用该方法
*/
@Retention(RetentionPolicy.CLASS) @Target(ElementType.METHOD) public @interface onTaskStart { @Retention(RetentionPolicy.CLASS) @Target(ElementType.METHOD) public @interface onTaskStart {
} }
/**
* 如果你在方法中添加{@code @Upload.onTaskStop}注解在任务停止时Aria会调用该方法
*/
@Retention(RetentionPolicy.CLASS) @Target(ElementType.METHOD) public @interface onTaskStop { @Retention(RetentionPolicy.CLASS) @Target(ElementType.METHOD) public @interface onTaskStop {
} }
/**
* 如果你在方法中添加{@code @Upload.onTaskCancel}l注解在任务取消时Aria会调用该方法
*/
@Retention(RetentionPolicy.CLASS) @Target(ElementType.METHOD) public @interface onTaskCancel { @Retention(RetentionPolicy.CLASS) @Target(ElementType.METHOD) public @interface onTaskCancel {
} }
/**
* 如果你在方法中添加{@code @Upload.onTaskFail)注解在任务预失败时Aria会调用该方法
*/
@Retention(RetentionPolicy.CLASS) @Target(ElementType.METHOD) public @interface onTaskFail { @Retention(RetentionPolicy.CLASS) @Target(ElementType.METHOD) public @interface onTaskFail {
} }
/**
* 如果你在方法中添加{@code @Upload.onTaskComplete}注解在任务完成时Aria会调用该方法
*/
@Retention(RetentionPolicy.CLASS) @Target(ElementType.METHOD) public @interface onTaskComplete { @Retention(RetentionPolicy.CLASS) @Target(ElementType.METHOD) public @interface onTaskComplete {
} }
/**
* 如果你在方法中添加{@code @Upload.onTaskRunning}注解在任务正在下载Aria会调用该方法
*/
@Retention(RetentionPolicy.CLASS) @Target(ElementType.METHOD) public @interface onTaskRunning { @Retention(RetentionPolicy.CLASS) @Target(ElementType.METHOD) public @interface onTaskRunning {
} }
/**
* 如果你在方法中添加{@code @Upload.onNoSupportBreakPoint}注解如果该任务不支持断点Aria会调用该方法
*/
@Retention(RetentionPolicy.CLASS) @Target(ElementType.METHOD) @Retention(RetentionPolicy.CLASS) @Target(ElementType.METHOD)
public @interface onNoSupportBreakPoint { public @interface onNoSupportBreakPoint {
} }

@ -120,8 +120,8 @@ class ElementHandle {
* obj.onStart((DownloadTask)task); * obj.onStart((DownloadTask)task);
* } * }
* *
* public void setListener(final SingleTaskActivity obj) { * public void setListener(final Object obj) {
* this.obj = obj; * this.obj = (SingleTaskActivity)obj;
* } * }
* } * }
* </code> * </code>
@ -193,12 +193,13 @@ class ElementHandle {
//添加设置代理的类 //添加设置代理的类
ParameterSpec parameterSpec = ParameterSpec parameterSpec =
ParameterSpec.builder(obj, "obj").addModifiers(Modifier.FINAL).build(); ParameterSpec.builder(Object.class, "obj").addModifiers(Modifier.FINAL).build();
MethodSpec listener = MethodSpec.methodBuilder(ProxyConstance.SET_LISTENER) MethodSpec listener = MethodSpec.methodBuilder(ProxyConstance.SET_LISTENER)
.addModifiers(Modifier.PUBLIC) .addModifiers(Modifier.PUBLIC)
.returns(void.class) .returns(void.class)
.addParameter(parameterSpec) .addParameter(parameterSpec)
.addCode("this.obj = obj;\n") .addAnnotation(Override.class)
.addCode("this.obj = (" + entity.className + ")obj;\n")
.build(); .build();
builder.addJavadoc("该文件为Aria自动生成的代理文件,请不要修改该文件的任何代码!\n"); builder.addJavadoc("该文件为Aria自动生成的代理文件,请不要修改该文件的任何代码!\n");

@ -133,15 +133,6 @@ public class SingleTaskActivity extends BaseActivity<ActivitySingleBinding> {
} }
}; };
@Download.onPre void onPre(DownloadTask task) {
}
@Download.onTaskStart
void onStart(DownloadTask task){
}
/** /**
* 设置start stop 按钮状态 * 设置start stop 按钮状态
*/ */
@ -152,7 +143,8 @@ public class SingleTaskActivity extends BaseActivity<ActivitySingleBinding> {
@Override protected void onResume() { @Override protected void onResume() {
super.onResume(); super.onResume();
Aria.download(this).addSchedulerListener(new MySchedulerListener()); Aria.download(this).register();
//Aria.download(this).addSchedulerListener(new MySchedulerListener());
//registerReceiver(mReceiver, getModule(DownloadModule.class).getDownloadFilter()); //registerReceiver(mReceiver, getModule(DownloadModule.class).getDownloadFilter());
} }
@ -172,6 +164,63 @@ public class SingleTaskActivity extends BaseActivity<ActivitySingleBinding> {
return true; return true;
} }
@Download.onPre protected void onPre(DownloadTask task) {
if (task.getKey().equals(DOWNLOAD_URL)) {
mUpdateHandler.obtainMessage(DOWNLOAD_PRE, task.getDownloadEntity().getFileSize())
.sendToTarget();
}
}
@Download.onTaskStart void taskStart(DownloadTask task) {
//通过下载地址可以判断任务是否是你指定的任务
if (task.getKey().equals(DOWNLOAD_URL)) {
mUpdateHandler.obtainMessage(DOWNLOAD_START, task.getDownloadEntity().getFileSize())
.sendToTarget();
}
}
@Download.onTaskRunning protected void running(DownloadTask task) {
if (task.getKey().equals(DOWNLOAD_URL)) {
mUpdateHandler.obtainMessage(DOWNLOAD_RUNNING, task).sendToTarget();
}
}
@Download.onTaskResume void taskResume(DownloadTask task) {
if (task.getKey().equals(DOWNLOAD_URL)) {
mUpdateHandler.obtainMessage(DOWNLOAD_START, task.getFileSize()).sendToTarget();
}
}
@Download.onTaskStop void taskStop(DownloadTask task) {
if (task.getKey().equals(DOWNLOAD_URL)) {
mUpdateHandler.sendEmptyMessage(DOWNLOAD_STOP);
}
}
@Download.onTaskCancel void taskCancel(DownloadTask task) {
if (task.getKey().equals(DOWNLOAD_URL)) {
mUpdateHandler.sendEmptyMessage(DOWNLOAD_CANCEL);
}
}
@Download.onTaskFail void taskFail(DownloadTask task) {
if (task.getKey().equals(DOWNLOAD_URL)) {
mUpdateHandler.sendEmptyMessage(DOWNLOAD_FAILE);
}
}
@Download.onTaskComplete void taskComplete(DownloadTask task) {
if (task.getKey().equals(DOWNLOAD_URL)) {
mUpdateHandler.sendEmptyMessage(DOWNLOAD_COMPLETE);
}
}
@Download.onNoSupportBreakPoint public void onNoSupportBreakPoint(DownloadTask task) {
if (task.getKey().equals(DOWNLOAD_URL)) {
T.showShort(SingleTaskActivity.this, "该下载链接不支持断点");
}
}
@Override protected int setLayoutId() { @Override protected int setLayoutId() {
return R.layout.activity_single; return R.layout.activity_single;
} }
@ -213,63 +262,4 @@ public class SingleTaskActivity extends BaseActivity<ActivitySingleBinding> {
break; break;
} }
} }
private class MySchedulerListener extends Aria.DownloadSchedulerListener {
@Override public void onPre(DownloadTask task) {
if (task.getKey().equals(DOWNLOAD_URL)) {
mUpdateHandler.obtainMessage(DOWNLOAD_PRE, task.getDownloadEntity().getFileSize())
.sendToTarget();
}
}
@Override public void onNoSupportBreakPoint(DownloadTask task) {
super.onNoSupportBreakPoint(task);
T.showShort(SingleTaskActivity.this, "该下载链接不支持断点");
}
@Override public void onTaskStart(DownloadTask task) {
//通过下载地址可以判断任务是否是你指定的任务
if (task.getKey().equals(DOWNLOAD_URL)) {
mUpdateHandler.obtainMessage(DOWNLOAD_START, task.getDownloadEntity().getFileSize())
.sendToTarget();
}
}
@Override public void onTaskResume(DownloadTask task) {
if (task.getKey().equals(DOWNLOAD_URL)) {
mUpdateHandler.obtainMessage(DOWNLOAD_START, task.getFileSize()).sendToTarget();
}
}
@Override public void onTaskStop(DownloadTask task) {
if (task.getKey().equals(DOWNLOAD_URL)) {
mUpdateHandler.sendEmptyMessage(DOWNLOAD_STOP);
}
}
@Override public void onTaskCancel(DownloadTask task) {
if (task.getKey().equals(DOWNLOAD_URL)) {
mUpdateHandler.sendEmptyMessage(DOWNLOAD_CANCEL);
}
}
@Override public void onTaskFail(DownloadTask task) {
if (task.getKey().equals(DOWNLOAD_URL)) {
mUpdateHandler.sendEmptyMessage(DOWNLOAD_FAILE);
}
}
@Override public void onTaskComplete(DownloadTask task) {
if (task.getKey().equals(DOWNLOAD_URL)) {
mUpdateHandler.sendEmptyMessage(DOWNLOAD_COMPLETE);
}
}
@Override public void onTaskRunning(DownloadTask task) {
if (task.getKey().equals(DOWNLOAD_URL)) {
mUpdateHandler.obtainMessage(DOWNLOAD_RUNNING, task).sendToTarget();
}
}
}
} }

@ -21,6 +21,7 @@ import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView; import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar; import android.support.v7.widget.Toolbar;
import butterknife.Bind; import butterknife.Bind;
import com.arialyy.annotations.Download;
import com.arialyy.aria.core.Aria; import com.arialyy.aria.core.Aria;
import com.arialyy.aria.core.download.DownloadEntity; import com.arialyy.aria.core.download.DownloadEntity;
import com.arialyy.aria.core.download.DownloadTask; import com.arialyy.aria.core.download.DownloadTask;
@ -58,51 +59,42 @@ public class MultiDownloadActivity extends BaseActivity<ActivityMultiDownloadBin
@Override protected void onResume() { @Override protected void onResume() {
super.onResume(); super.onResume();
Aria.download(this).addSchedulerListener(new MySchedulerListener()); //Aria.download(this).addSchedulerListener(new MySchedulerListener());
Aria.download(this).register();
} }
private class MySchedulerListener extends Aria.DownloadSchedulerListener { @Download.onPre void onPre(DownloadTask task) {
@Override public void onTaskPre(DownloadTask task) { L.d(TAG, "download onPre");
super.onTaskPre(task); mAdapter.updateState(task.getDownloadEntity());
L.d(TAG, "download onPre"); }
mAdapter.updateState(task.getDownloadEntity());
}
@Override public void onTaskStart(DownloadTask task) { @Download.onTaskStart void taskStart(DownloadTask task) {
super.onTaskStart(task); L.d(TAG, "download start");
L.d(TAG, "download start"); mAdapter.updateState(task.getDownloadEntity());
mAdapter.updateState(task.getDownloadEntity()); }
}
@Override public void onTaskResume(DownloadTask task) { @Download.onTaskResume void taskResume(DownloadTask task) {
super.onTaskResume(task); L.d(TAG, "download resume");
L.d(TAG, "download resume"); mAdapter.updateState(task.getDownloadEntity());
mAdapter.updateState(task.getDownloadEntity()); }
}
@Override public void onTaskRunning(DownloadTask task) { @Download.onTaskStop void taskStop(DownloadTask task) {
super.onTaskRunning(task); mAdapter.updateState(task.getDownloadEntity());
mAdapter.setProgress(task.getDownloadEntity()); }
}
@Override public void onTaskStop(DownloadTask task) { @Download.onTaskCancel void taskCancel(DownloadTask task) {
super.onTaskStop(task); mAdapter.updateState(task.getDownloadEntity());
mAdapter.updateState(task.getDownloadEntity()); }
}
@Override public void onTaskCancel(DownloadTask task) { @Download.onTaskFail void taskFail(DownloadTask task) {
super.onTaskCancel(task); L.d(TAG, "download fail");
mAdapter.updateState(task.getDownloadEntity()); }
}
@Override public void onTaskComplete(DownloadTask task) { @Download.onTaskComplete void taskComplete(DownloadTask task) {
super.onTaskComplete(task); mAdapter.updateState(task.getDownloadEntity());
mAdapter.updateState(task.getDownloadEntity()); }
}
@Override public void onTaskFail(DownloadTask task) { @Download.onTaskRunning void taskRunning(DownloadTask task) {
super.onTaskFail(task); mAdapter.setProgress(task.getDownloadEntity());
L.d(TAG, "download fail");
}
} }
} }

@ -23,12 +23,15 @@ import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar; import android.support.v7.widget.Toolbar;
import android.view.View; import android.view.View;
import butterknife.Bind; import butterknife.Bind;
import com.arialyy.annotations.Download;
import com.arialyy.aria.core.Aria; import com.arialyy.aria.core.Aria;
import com.arialyy.aria.core.download.DownloadTask; import com.arialyy.aria.core.download.DownloadTask;
import com.arialyy.frame.util.show.T;
import com.arialyy.simple.R; import com.arialyy.simple.R;
import com.arialyy.simple.base.BaseActivity; import com.arialyy.simple.base.BaseActivity;
import com.arialyy.simple.databinding.ActivityMultiBinding; import com.arialyy.simple.databinding.ActivityMultiBinding;
import com.arialyy.simple.download.DownloadModule; import com.arialyy.simple.download.DownloadModule;
import com.arialyy.simple.download.SingleTaskActivity;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -71,45 +74,37 @@ public class MultiTaskActivity extends BaseActivity<ActivityMultiBinding> {
@Override protected void onResume() { @Override protected void onResume() {
super.onResume(); super.onResume();
Aria.download(this).addSchedulerListener(new DownloadListener()); Aria.download(this).register();
} }
@Override protected void onDestroy() { @Download.onTaskStart void taskStart(DownloadTask task) {
super.onDestroy(); mAdapter.updateBtState(task.getKey(), false);
//unregisterReceiver(mReceiver);
} }
@Override protected void dataCallback(int result, Object data) { @Download.onTaskResume void taskResume(DownloadTask task) {
super.dataCallback(result, data); mAdapter.updateBtState(task.getKey(), false);
if (result == DownloadNumDialog.RESULT_CODE) {
Aria.get(this).getDownloadConfig().setMaxTaskNum(Integer.parseInt(data + ""));
}
} }
private class DownloadListener extends Aria.DownloadSchedulerListener { @Download.onTaskStop void taskStop(DownloadTask task) {
mAdapter.updateBtState(task.getKey(), true);
@Override public void onTaskStart(DownloadTask task) { }
super.onTaskStart(task);
mAdapter.updateBtState(task.getDownloadUrl(), false);
}
@Override public void onTaskRunning(DownloadTask task) { @Download.onTaskCancel void taskCancel(DownloadTask task) {
super.onTaskRunning(task); mAdapter.updateBtState(task.getKey(), true);
} }
@Override public void onTaskResume(DownloadTask task) { @Download.onTaskFail void taskFail(DownloadTask task) {
super.onTaskResume(task); mAdapter.updateBtState(task.getKey(), true);
mAdapter.updateBtState(task.getDownloadUrl(), false); }
}
@Override public void onTaskStop(DownloadTask task) { @Download.onTaskComplete void taskComplete(DownloadTask task) {
super.onTaskStop(task); mAdapter.updateBtState(task.getKey(), true);
mAdapter.updateBtState(task.getDownloadUrl(), true); }
}
@Override public void onTaskComplete(DownloadTask task) { @Override protected void dataCallback(int result, Object data) {
super.onTaskComplete(task); super.dataCallback(result, data);
mAdapter.updateBtState(task.getDownloadUrl(), true); if (result == DownloadNumDialog.RESULT_CODE) {
Aria.get(this).getDownloadConfig().setMaxTaskNum(Integer.parseInt(data + ""));
} }
} }
} }

@ -21,6 +21,7 @@ import android.os.Handler;
import android.os.Message; import android.os.Message;
import butterknife.Bind; import butterknife.Bind;
import butterknife.OnClick; import butterknife.OnClick;
import com.arialyy.annotations.Upload;
import com.arialyy.aria.core.Aria; import com.arialyy.aria.core.Aria;
import com.arialyy.aria.core.upload.UploadTask; import com.arialyy.aria.core.upload.UploadTask;
import com.arialyy.frame.util.FileUtil; import com.arialyy.frame.util.FileUtil;
@ -98,48 +99,34 @@ public class UploadActivity extends BaseActivity<ActivityUploadMeanBinding> {
@Override protected void onResume() { @Override protected void onResume() {
super.onResume(); super.onResume();
Aria.upload(this).addSchedulerListener(new UploadListener(mHandler)); //Aria.upload(this).addSchedulerListener(new UploadListener(mHandler));
Aria.upload(this).register();
} }
static class UploadListener extends Aria.UploadSchedulerListener { @Upload.onPre public void onPre(UploadTask task) {
WeakReference<Handler> handler; }
UploadListener(Handler handler) {
this.handler = new WeakReference<>(handler);
}
@Override public void onPre(UploadTask task) {
super.onPre(task);
}
@Override public void onTaskPre(UploadTask task) { @Upload.onTaskPre public void taskPre(UploadTask task) {
super.onTaskPre(task); L.d(TAG, "fileSize = " + task.getConvertFileSize());
L.d(TAG, "fileSize = " + task.getConvertFileSize()); }
}
@Override public void onTaskStart(UploadTask task) { @Upload.onTaskStart public void taskStart(UploadTask task) {
super.onTaskStart(task); mHandler.obtainMessage(START, task).sendToTarget();
handler.get().obtainMessage(START, task).sendToTarget(); }
}
@Override public void onTaskStop(UploadTask task) { @Upload.onTaskStop public void taskStop(UploadTask task) {
super.onTaskStop(task); mHandler.obtainMessage(STOP, task).sendToTarget();
handler.get().obtainMessage(STOP, task).sendToTarget(); }
}
@Override public void onTaskCancel(UploadTask task) { @Upload.onTaskCancel public void taskCancel(UploadTask task) {
super.onTaskCancel(task); mHandler.obtainMessage(CANCEL, task).sendToTarget();
handler.get().obtainMessage(CANCEL, task).sendToTarget(); }
}
@Override public void onTaskRunning(UploadTask task) { @Upload.onTaskRunning public void taskRunning(UploadTask task) {
super.onTaskRunning(task); mHandler.obtainMessage(RUNNING, task).sendToTarget();
handler.get().obtainMessage(RUNNING, task).sendToTarget(); }
}
@Override public void onTaskComplete(UploadTask task) { @Upload.onTaskComplete public void taskComplete(UploadTask task) {
super.onTaskComplete(task); mHandler.obtainMessage(COMPLETE, task).sendToTarget();
handler.get().obtainMessage(COMPLETE, task).sendToTarget();
}
} }
} }

Loading…
Cancel
Save