From 603a21fe430fbac02a0c5cb24fa96e5b7686c134 Mon Sep 17 00:00:00 2001 From: AriaLyy <511455842@qq.com> Date: Wed, 7 Jun 2017 20:15:05 +0800 Subject: [PATCH] =?UTF-8?q?Apt=20=E4=B8=8B=E8=BD=BD=E4=BA=8B=E4=BB=B6?= =?UTF-8?q?=E5=AE=8C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/arialyy/aria/core/AriaManager.java | 3 + .../aria/core/download/DownloadReceiver.java | 10 +- .../com/arialyy/aria/core/inf/IReceiver.java | 5 + .../core/scheduler/AbsSchedulerListener.java | 8 ++ .../aria/core/scheduler/AbsSchedulers.java | 27 ++-- .../aria/core/scheduler/ISchedulers.java | 8 +- .../aria/core/upload/UploadReceiver.java | 15 ++ .../com/arialyy/annotations/Download.java | 31 +++++ .../java/com/arialyy/annotations/Upload.java | 34 ++++- .../com/arialyy/compiler/ElementHandle.java | 9 +- .../simple/download/SingleTaskActivity.java | 128 ++++++++---------- .../multi_download/MultiDownloadActivity.java | 66 ++++----- .../multi_download/MultiTaskActivity.java | 53 ++++---- .../arialyy/simple/upload/UploadActivity.java | 57 +++----- 14 files changed, 259 insertions(+), 195 deletions(-) diff --git a/Aria/src/main/java/com/arialyy/aria/core/AriaManager.java b/Aria/src/main/java/com/arialyy/aria/core/AriaManager.java index 7c14afa0..dacb0aea 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/AriaManager.java +++ b/Aria/src/main/java/com/arialyy/aria/core/AriaManager.java @@ -176,11 +176,13 @@ import org.xml.sax.SAXException; if (isDownload) { DownloadReceiver dReceiver = new DownloadReceiver(); dReceiver.targetName = obj.getClass().getName(); + dReceiver.obj = obj; mReceivers.put(key, dReceiver); receiver = dReceiver; } else { UploadReceiver uReceiver = new UploadReceiver(); uReceiver.targetName = obj.getClass().getName(); + uReceiver.obj = obj; mReceivers.put(key, uReceiver); receiver = uReceiver; } @@ -303,6 +305,7 @@ import org.xml.sax.SAXException; if (key.contains(clsName)) { IReceiver receiver = mReceivers.get(key); receiver.removeSchedulerListener(); + receiver.unRegister(); receiver.destroy(); iter.remove(); break; diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadReceiver.java b/Aria/src/main/java/com/arialyy/aria/core/download/DownloadReceiver.java index da2cd54b..d32800de 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadReceiver.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/DownloadReceiver.java @@ -36,6 +36,7 @@ import java.util.Set; public class DownloadReceiver implements IReceiver { private static final String TAG = "DownloadReceiver"; public String targetName; + public Object obj; public ISchedulerListener listener; /** @@ -72,16 +73,16 @@ public class DownloadReceiver implements IReceiver { * 将当前类注册到Aria */ public DownloadReceiver register() { - DownloadSchedulers.getInstance().register(targetName); + DownloadSchedulers.getInstance().register(obj); return this; } /** * 取消注册 */ - public DownloadReceiver unRegister() { - DownloadSchedulers.getInstance().unRegister(targetName); - return this; + @Override + public void unRegister() { + DownloadSchedulers.getInstance().unRegister(obj); } /** @@ -149,6 +150,7 @@ public class DownloadReceiver implements IReceiver { for (String key : keys) { IReceiver receiver = ariaManager.getReceiver().get(key); receiver.removeSchedulerListener(); + receiver.unRegister(); ariaManager.getReceiver().remove(key); } } diff --git a/Aria/src/main/java/com/arialyy/aria/core/inf/IReceiver.java b/Aria/src/main/java/com/arialyy/aria/core/inf/IReceiver.java index c69b6cf1..0c8d701c 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/inf/IReceiver.java +++ b/Aria/src/main/java/com/arialyy/aria/core/inf/IReceiver.java @@ -31,6 +31,11 @@ public interface IReceiver { */ public void removeSchedulerListener(); + /** + * 移除观察者 + */ + public void unRegister(); + /** * 停止所有任务 */ diff --git a/Aria/src/main/java/com/arialyy/aria/core/scheduler/AbsSchedulerListener.java b/Aria/src/main/java/com/arialyy/aria/core/scheduler/AbsSchedulerListener.java index 4c73c8f5..ae411967 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/scheduler/AbsSchedulerListener.java +++ b/Aria/src/main/java/com/arialyy/aria/core/scheduler/AbsSchedulerListener.java @@ -56,4 +56,12 @@ public class AbsSchedulerListener implements ISchedulerListe @Override public void onTaskRunning(TASK task) { } + + public void onNoSupportBreakPoint(TASK task) { + + } + + public void setListener(Object obj) { + + } } diff --git a/Aria/src/main/java/com/arialyy/aria/core/scheduler/AbsSchedulers.java b/Aria/src/main/java/com/arialyy/aria/core/scheduler/AbsSchedulers.java index 864010a1..351bd07d 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/scheduler/AbsSchedulers.java +++ b/Aria/src/main/java/com/arialyy/aria/core/scheduler/AbsSchedulers.java @@ -49,7 +49,7 @@ public abstract class AbsSchedulers schedulerListener) { @@ -58,7 +58,7 @@ public abstract class AbsSchedulers schedulerListener) { @@ -70,18 +70,21 @@ public abstract class AbsSchedulers listener = mObservers.get(targetName); 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>> iter = mObservers.entrySet().iterator(); iter.hasNext(); ) { Map.Entry> 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 0) { Set keys = mSchedulerListeners.keySet(); for (String key : keys) { - if (mSchedulerListeners.size() > 0) { - callback(state, task, mSchedulerListeners.get(key)); - } - if (mObservers.size() > 0) { - callback(state, task, mObservers.get(key)); - } + callback(state, task, mSchedulerListeners.get(key)); + } + } else if (mObservers.size() > 0) { + Set keys = mObservers.keySet(); + for (String key : keys) { + callback(state, task, mObservers.get(key)); } } } diff --git a/Aria/src/main/java/com/arialyy/aria/core/scheduler/ISchedulers.java b/Aria/src/main/java/com/arialyy/aria/core/scheduler/ISchedulers.java index dc673315..7549b55a 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/scheduler/ISchedulers.java +++ b/Aria/src/main/java/com/arialyy/aria/core/scheduler/ISchedulers.java @@ -84,14 +84,14 @@ public interface ISchedulers extends Handler.Callback { /** * 将当前类注册到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); } \ No newline at end of file diff --git a/Aria/src/main/java/com/arialyy/aria/core/upload/UploadReceiver.java b/Aria/src/main/java/com/arialyy/aria/core/upload/UploadReceiver.java index 98ceff94..38e71953 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/upload/UploadReceiver.java +++ b/Aria/src/main/java/com/arialyy/aria/core/upload/UploadReceiver.java @@ -19,8 +19,10 @@ import android.support.annotation.NonNull; import com.arialyy.aria.core.AriaManager; import com.arialyy.aria.core.command.AbsCmd; 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.IReceiver; +import com.arialyy.aria.core.scheduler.DownloadSchedulers; import com.arialyy.aria.core.scheduler.ISchedulerListener; import com.arialyy.aria.core.scheduler.UploadSchedulers; import com.arialyy.aria.orm.DbEntity; @@ -38,6 +40,7 @@ import java.util.regex.Pattern; public class UploadReceiver implements IReceiver { private static final String TAG = "DownloadReceiver"; public String targetName; + public Object obj; public ISchedulerListener listener; /** @@ -127,4 +130,16 @@ public class UploadReceiver implements IReceiver { UploadSchedulers.getInstance().removeSchedulerListener(targetName, listener); } } + + /** + * 将当前类注册到Aria + */ + public UploadReceiver register() { + UploadSchedulers.getInstance().register(obj); + return this; + } + + @Override public void unRegister() { + UploadSchedulers.getInstance().unRegister(obj); + } } \ No newline at end of file diff --git a/AriaAnnotations/src/main/java/com/arialyy/annotations/Download.java b/AriaAnnotations/src/main/java/com/arialyy/annotations/Download.java index 7a6cf5d6..0e5d1ea5 100644 --- a/AriaAnnotations/src/main/java/com/arialyy/annotations/Download.java +++ b/AriaAnnotations/src/main/java/com/arialyy/annotations/Download.java @@ -22,35 +22,66 @@ import java.lang.annotation.Target; /** * Created by Aria.Lao on 2017/6/6. + * 下载注解 */ @Retention(RetentionPolicy.CLASS) @Target(ElementType.METHOD) public @interface Download { + /** + * 如果你在方法中添加{@code @Download.onPre}注解,在预处理完成时,Aria会调用该方法 + */ @Retention(RetentionPolicy.CLASS) @Target(ElementType.METHOD) public @interface onPre { } + /** + * 如果你在方法中添加{@code @Download.onTaskPre}注解,在任务预处理完成时,Aria会调用该方法 + */ @Retention(RetentionPolicy.CLASS) @Target(ElementType.METHOD) public @interface onTaskPre { } + /** + * 如果你在方法中添加{@code @Download.onTaskResume}注解,在任务恢复下载时,Aria会调用该方法 + */ @Retention(RetentionPolicy.CLASS) @Target(ElementType.METHOD) public @interface onTaskResume { } + /** + * 如果你在方法中添加{@code @Download.onTaskStart}注解,在任务开始下载时,Aria会调用该方法 + */ @Retention(RetentionPolicy.CLASS) @Target(ElementType.METHOD) public @interface onTaskStart { } + /** + * 如果你在方法中添加{@code @Download.onTaskStop}注解,在任务停止时,Aria会调用该方法 + */ @Retention(RetentionPolicy.CLASS) @Target(ElementType.METHOD) public @interface onTaskStop { } + /** + * 如果你在方法中添加{@code @Download.onTaskCancel}l注解,在任务取消时,Aria会调用该方法 + */ @Retention(RetentionPolicy.CLASS) @Target(ElementType.METHOD) public @interface onTaskCancel { } + /** + * 如果你在方法中添加{@code @Download.onTaskFail)注解,在任务预失败时,Aria会调用该方法 + */ @Retention(RetentionPolicy.CLASS) @Target(ElementType.METHOD) public @interface onTaskFail { } + /** + * 如果你在方法中添加{@code @Download.onTaskComplete}注解,在任务完成时,Aria会调用该方法 + */ @Retention(RetentionPolicy.CLASS) @Target(ElementType.METHOD) public @interface onTaskComplete { } + /** + * 如果你在方法中添加{@code @Download.onTaskRunning}注解,在任务正在下载,Aria会调用该方法 + */ @Retention(RetentionPolicy.CLASS) @Target(ElementType.METHOD) public @interface onTaskRunning { } + /** + * 如果你在方法中添加{@code @Download.onNoSupportBreakPoint}注解,如果该任务不支持断点,Aria会调用该方法 + */ @Retention(RetentionPolicy.CLASS) @Target(ElementType.METHOD) public @interface onNoSupportBreakPoint { } diff --git a/AriaAnnotations/src/main/java/com/arialyy/annotations/Upload.java b/AriaAnnotations/src/main/java/com/arialyy/annotations/Upload.java index 50de0d2a..d224d14f 100644 --- a/AriaAnnotations/src/main/java/com/arialyy/annotations/Upload.java +++ b/AriaAnnotations/src/main/java/com/arialyy/annotations/Upload.java @@ -21,36 +21,68 @@ import java.lang.annotation.RetentionPolicy; 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 { + + /** + * 如果你在方法中添加{@code @Upload.onPre}注解,在预处理完成时,Aria会调用该方法 + */ @Retention(RetentionPolicy.CLASS) @Target(ElementType.METHOD) public @interface onPre { } + /** + * 如果你在方法中添加{@code @Upload.onTaskPre}注解,在任务预处理完成时,Aria会调用该方法 + */ @Retention(RetentionPolicy.CLASS) @Target(ElementType.METHOD) public @interface onTaskPre { } + /** + * 如果你在方法中添加{@code @Upload.onTaskResume}注解,在任务恢复下载时,Aria会调用该方法 + */ //@Retention(RetentionPolicy.CLASS) @Target(ElementType.METHOD) public @interface onTaskResume { //} + /** + * 如果你在方法中添加{@code @Upload.onTaskStart}注解,在任务开始下载时,Aria会调用该方法 + */ @Retention(RetentionPolicy.CLASS) @Target(ElementType.METHOD) public @interface onTaskStart { } + /** + * 如果你在方法中添加{@code @Upload.onTaskStop}注解,在任务停止时,Aria会调用该方法 + */ @Retention(RetentionPolicy.CLASS) @Target(ElementType.METHOD) public @interface onTaskStop { } + /** + * 如果你在方法中添加{@code @Upload.onTaskCancel}l注解,在任务取消时,Aria会调用该方法 + */ @Retention(RetentionPolicy.CLASS) @Target(ElementType.METHOD) public @interface onTaskCancel { } + /** + * 如果你在方法中添加{@code @Upload.onTaskFail)注解,在任务预失败时,Aria会调用该方法 + */ @Retention(RetentionPolicy.CLASS) @Target(ElementType.METHOD) public @interface onTaskFail { } + /** + * 如果你在方法中添加{@code @Upload.onTaskComplete}注解,在任务完成时,Aria会调用该方法 + */ @Retention(RetentionPolicy.CLASS) @Target(ElementType.METHOD) public @interface onTaskComplete { } + /** + * 如果你在方法中添加{@code @Upload.onTaskRunning}注解,在任务正在下载,Aria会调用该方法 + */ @Retention(RetentionPolicy.CLASS) @Target(ElementType.METHOD) public @interface onTaskRunning { } + /** + * 如果你在方法中添加{@code @Upload.onNoSupportBreakPoint}注解,如果该任务不支持断点,Aria会调用该方法 + */ @Retention(RetentionPolicy.CLASS) @Target(ElementType.METHOD) public @interface onNoSupportBreakPoint { } diff --git a/AriaCompiler/src/main/java/com/arialyy/compiler/ElementHandle.java b/AriaCompiler/src/main/java/com/arialyy/compiler/ElementHandle.java index 8bb9cfea..3ec58d74 100644 --- a/AriaCompiler/src/main/java/com/arialyy/compiler/ElementHandle.java +++ b/AriaCompiler/src/main/java/com/arialyy/compiler/ElementHandle.java @@ -120,8 +120,8 @@ class ElementHandle { * obj.onStart((DownloadTask)task); * } * - * public void setListener(final SingleTaskActivity obj) { - * this.obj = obj; + * public void setListener(final Object obj) { + * this.obj = (SingleTaskActivity)obj; * } * } * @@ -193,12 +193,13 @@ class ElementHandle { //添加设置代理的类 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) .addModifiers(Modifier.PUBLIC) .returns(void.class) .addParameter(parameterSpec) - .addCode("this.obj = obj;\n") + .addAnnotation(Override.class) + .addCode("this.obj = (" + entity.className + ")obj;\n") .build(); builder.addJavadoc("该文件为Aria自动生成的代理文件,请不要修改该文件的任何代码!\n"); diff --git a/app/src/main/java/com/arialyy/simple/download/SingleTaskActivity.java b/app/src/main/java/com/arialyy/simple/download/SingleTaskActivity.java index d4dcbdda..7de2db02 100644 --- a/app/src/main/java/com/arialyy/simple/download/SingleTaskActivity.java +++ b/app/src/main/java/com/arialyy/simple/download/SingleTaskActivity.java @@ -133,15 +133,6 @@ public class SingleTaskActivity extends BaseActivity { } }; - @Download.onPre void onPre(DownloadTask task) { - - } - - @Download.onTaskStart - void onStart(DownloadTask task){ - - } - /** * 设置start 和 stop 按钮状态 */ @@ -152,7 +143,8 @@ public class SingleTaskActivity extends BaseActivity { @Override protected void 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()); } @@ -172,6 +164,63 @@ public class SingleTaskActivity extends BaseActivity { 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() { return R.layout.activity_single; } @@ -213,63 +262,4 @@ public class SingleTaskActivity extends BaseActivity { 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(); - } - } - } } \ No newline at end of file diff --git a/app/src/main/java/com/arialyy/simple/download/multi_download/MultiDownloadActivity.java b/app/src/main/java/com/arialyy/simple/download/multi_download/MultiDownloadActivity.java index c6fa7825..f49309f1 100644 --- a/app/src/main/java/com/arialyy/simple/download/multi_download/MultiDownloadActivity.java +++ b/app/src/main/java/com/arialyy/simple/download/multi_download/MultiDownloadActivity.java @@ -21,6 +21,7 @@ import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.support.v7.widget.Toolbar; import butterknife.Bind; +import com.arialyy.annotations.Download; import com.arialyy.aria.core.Aria; import com.arialyy.aria.core.download.DownloadEntity; import com.arialyy.aria.core.download.DownloadTask; @@ -58,51 +59,42 @@ public class MultiDownloadActivity extends BaseActivity { @Override protected void onResume() { super.onResume(); - Aria.download(this).addSchedulerListener(new DownloadListener()); + Aria.download(this).register(); } - @Override protected void onDestroy() { - super.onDestroy(); - //unregisterReceiver(mReceiver); + @Download.onTaskStart void taskStart(DownloadTask task) { + mAdapter.updateBtState(task.getKey(), false); } - @Override protected void dataCallback(int result, Object data) { - super.dataCallback(result, data); - if (result == DownloadNumDialog.RESULT_CODE) { - Aria.get(this).getDownloadConfig().setMaxTaskNum(Integer.parseInt(data + "")); - } + @Download.onTaskResume void taskResume(DownloadTask task) { + mAdapter.updateBtState(task.getKey(), false); } - private class DownloadListener extends Aria.DownloadSchedulerListener { - - @Override public void onTaskStart(DownloadTask task) { - super.onTaskStart(task); - mAdapter.updateBtState(task.getDownloadUrl(), false); - } + @Download.onTaskStop void taskStop(DownloadTask task) { + mAdapter.updateBtState(task.getKey(), true); + } - @Override public void onTaskRunning(DownloadTask task) { - super.onTaskRunning(task); - } + @Download.onTaskCancel void taskCancel(DownloadTask task) { + mAdapter.updateBtState(task.getKey(), true); + } - @Override public void onTaskResume(DownloadTask task) { - super.onTaskResume(task); - mAdapter.updateBtState(task.getDownloadUrl(), false); - } + @Download.onTaskFail void taskFail(DownloadTask task) { + mAdapter.updateBtState(task.getKey(), true); + } - @Override public void onTaskStop(DownloadTask task) { - super.onTaskStop(task); - mAdapter.updateBtState(task.getDownloadUrl(), true); - } + @Download.onTaskComplete void taskComplete(DownloadTask task) { + mAdapter.updateBtState(task.getKey(), true); + } - @Override public void onTaskComplete(DownloadTask task) { - super.onTaskComplete(task); - mAdapter.updateBtState(task.getDownloadUrl(), true); + @Override protected void dataCallback(int result, Object data) { + super.dataCallback(result, data); + if (result == DownloadNumDialog.RESULT_CODE) { + Aria.get(this).getDownloadConfig().setMaxTaskNum(Integer.parseInt(data + "")); } } } \ No newline at end of file diff --git a/app/src/main/java/com/arialyy/simple/upload/UploadActivity.java b/app/src/main/java/com/arialyy/simple/upload/UploadActivity.java index f958789b..be6351e6 100644 --- a/app/src/main/java/com/arialyy/simple/upload/UploadActivity.java +++ b/app/src/main/java/com/arialyy/simple/upload/UploadActivity.java @@ -21,6 +21,7 @@ import android.os.Handler; import android.os.Message; import butterknife.Bind; import butterknife.OnClick; +import com.arialyy.annotations.Upload; import com.arialyy.aria.core.Aria; import com.arialyy.aria.core.upload.UploadTask; import com.arialyy.frame.util.FileUtil; @@ -98,48 +99,34 @@ public class UploadActivity extends BaseActivity { @Override protected void 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 { - WeakReference handler; - - UploadListener(Handler handler) { - this.handler = new WeakReference<>(handler); - } - - @Override public void onPre(UploadTask task) { - super.onPre(task); - } + @Upload.onPre public void onPre(UploadTask task) { + } - @Override public void onTaskPre(UploadTask task) { - super.onTaskPre(task); - L.d(TAG, "fileSize = " + task.getConvertFileSize()); - } + @Upload.onTaskPre public void taskPre(UploadTask task) { + L.d(TAG, "fileSize = " + task.getConvertFileSize()); + } - @Override public void onTaskStart(UploadTask task) { - super.onTaskStart(task); - handler.get().obtainMessage(START, task).sendToTarget(); - } + @Upload.onTaskStart public void taskStart(UploadTask task) { + mHandler.obtainMessage(START, task).sendToTarget(); + } - @Override public void onTaskStop(UploadTask task) { - super.onTaskStop(task); - handler.get().obtainMessage(STOP, task).sendToTarget(); - } + @Upload.onTaskStop public void taskStop(UploadTask task) { + mHandler.obtainMessage(STOP, task).sendToTarget(); + } - @Override public void onTaskCancel(UploadTask task) { - super.onTaskCancel(task); - handler.get().obtainMessage(CANCEL, task).sendToTarget(); - } + @Upload.onTaskCancel public void taskCancel(UploadTask task) { + mHandler.obtainMessage(CANCEL, task).sendToTarget(); + } - @Override public void onTaskRunning(UploadTask task) { - super.onTaskRunning(task); - handler.get().obtainMessage(RUNNING, task).sendToTarget(); - } + @Upload.onTaskRunning public void taskRunning(UploadTask task) { + mHandler.obtainMessage(RUNNING, task).sendToTarget(); + } - @Override public void onTaskComplete(UploadTask task) { - super.onTaskComplete(task); - handler.get().obtainMessage(COMPLETE, task).sendToTarget(); - } + @Upload.onTaskComplete public void taskComplete(UploadTask task) { + mHandler.obtainMessage(COMPLETE, task).sendToTarget(); } }