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 fc9be612..b52101e7 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/AMReceiver.java +++ b/Aria/src/main/java/com/arialyy/aria/core/AMReceiver.java @@ -37,7 +37,7 @@ public class AMReceiver { */ @Deprecated public AMTarget load(DownloadEntity entity) { this.entity = entity; - return new AMTarget(this); + return new AMTarget(entity, targetName); } /** @@ -52,7 +52,7 @@ public class AMReceiver { entity = new DownloadEntity(); } entity.setDownloadUrl(downloadUrl); - return new AMTarget(this); + return new AMTarget(entity, targetName); } /** 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 022c2607..cf7e5742 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/AMTarget.java +++ b/Aria/src/main/java/com/arialyy/aria/core/AMTarget.java @@ -29,10 +29,13 @@ import java.util.List; * https://github.com/AriaLyy/Aria */ public class AMTarget { - private AMReceiver mReceiver; + //private AMReceiver mReceiver; + DownloadEntity entity; + String targetName; - AMTarget(AMReceiver receiver) { - this.mReceiver = receiver; + AMTarget(DownloadEntity entity, String targetName) { + this.entity = entity; + this.targetName = targetName; } /** @@ -42,7 +45,7 @@ public class AMTarget { if (TextUtils.isEmpty(downloadPath)) { throw new IllegalArgumentException("文件保持路径不能为null"); } - mReceiver.entity.setDownloadPath(downloadPath); + entity.setDownloadPath(downloadPath); return this; } @@ -53,7 +56,7 @@ public class AMTarget { if (TextUtils.isEmpty(downloadName)) { throw new IllegalArgumentException("文件名不能为null"); } - mReceiver.entity.setFileName(downloadName); + entity.setFileName(downloadName); return this; } @@ -61,7 +64,7 @@ public class AMTarget { * 获取下载文件大小 */ public long getFileSize() { - DownloadEntity entity = getDownloadEntity(mReceiver.entity.getDownloadUrl()); + DownloadEntity entity = getDownloadEntity(this.entity.getDownloadUrl()); if (entity == null) { throw new NullPointerException("下载管理器中没有改任务"); } @@ -72,7 +75,7 @@ public class AMTarget { * 获取当前下载进度,如果下載实体存在,则返回当前进度 */ public long getCurrentProgress() { - DownloadEntity entity = getDownloadEntity(mReceiver.entity.getDownloadUrl()); + DownloadEntity entity = getDownloadEntity(this.entity.getDownloadUrl()); if (entity == null) { throw new NullPointerException("下载管理器中没有改任务"); } @@ -89,8 +92,7 @@ public class AMTarget { */ public void add() { DownloadManager.getInstance() - .setCmd( - CommonUtil.createCmd(mReceiver.targetName, mReceiver.entity, CmdFactory.TASK_CREATE)) + .setCmd(CommonUtil.createCmd(targetName, entity, CmdFactory.TASK_CREATE)) .exe(); } @@ -99,8 +101,8 @@ public class AMTarget { */ public void start() { List cmds = new ArrayList<>(); - cmds.add(CommonUtil.createCmd(mReceiver.targetName, mReceiver.entity, CmdFactory.TASK_CREATE)); - cmds.add(CommonUtil.createCmd(mReceiver.targetName, mReceiver.entity, CmdFactory.TASK_START)); + cmds.add(CommonUtil.createCmd(targetName, entity, CmdFactory.TASK_CREATE)); + cmds.add(CommonUtil.createCmd(targetName, entity, CmdFactory.TASK_START)); DownloadManager.getInstance().setCmds(cmds).exe(); cmds.clear(); } @@ -110,7 +112,7 @@ public class AMTarget { */ public void stop() { DownloadManager.getInstance() - .setCmd(CommonUtil.createCmd(mReceiver.targetName, mReceiver.entity, CmdFactory.TASK_STOP)) + .setCmd(CommonUtil.createCmd(targetName, entity, CmdFactory.TASK_STOP)) .exe(); } @@ -119,7 +121,7 @@ public class AMTarget { */ public void resume() { DownloadManager.getInstance() - .setCmd(CommonUtil.createCmd(mReceiver.targetName, mReceiver.entity, CmdFactory.TASK_START)) + .setCmd(CommonUtil.createCmd(targetName, entity, CmdFactory.TASK_START)) .exe(); } @@ -128,8 +130,7 @@ public class AMTarget { */ public void cancel() { DownloadManager.getInstance() - .setCmd( - CommonUtil.createCmd(mReceiver.targetName, mReceiver.entity, CmdFactory.TASK_CANCEL)) + .setCmd(CommonUtil.createCmd(targetName, entity, CmdFactory.TASK_CANCEL)) .exe(); } @@ -137,7 +138,7 @@ public class AMTarget { * 是否在下载 */ public boolean isDownloading() { - return DownloadManager.getInstance().getTaskQueue().getTask(mReceiver.entity).isDownloading(); + return DownloadManager.getInstance().getTaskQueue().getTask(entity).isDownloading(); } /** diff --git a/app/src/main/java/com/arialyy/simple/activity/MultiTaskActivity.java b/app/src/main/java/com/arialyy/simple/activity/MultiTaskActivity.java index 5556eafa..6feba7a3 100644 --- a/app/src/main/java/com/arialyy/simple/activity/MultiTaskActivity.java +++ b/app/src/main/java/com/arialyy/simple/activity/MultiTaskActivity.java @@ -14,7 +14,6 @@ * limitations under the License. */ - package com.arialyy.simple.activity; import android.os.Bundle; @@ -25,6 +24,7 @@ import android.view.View; import butterknife.Bind; import com.arialyy.aria.core.AMTarget; import com.arialyy.aria.core.Aria; +import com.arialyy.aria.core.DownloadEntity; import com.arialyy.aria.core.task.Task; import com.arialyy.frame.util.show.L; import com.arialyy.simple.R; @@ -33,14 +33,17 @@ import com.arialyy.simple.base.BaseActivity; import com.arialyy.simple.databinding.ActivityMultiBinding; import com.arialyy.simple.dialog.DownloadNumDialog; import com.arialyy.simple.module.DownloadModule; +import java.util.ArrayList; +import java.util.List; /** * Created by Lyy on 2016/9/27. */ public class MultiTaskActivity extends BaseActivity { - @Bind(R.id.list) RecyclerView mList; - @Bind(R.id.toolbar) Toolbar mBar; + @Bind(R.id.list) RecyclerView mList; + @Bind(R.id.toolbar) Toolbar mBar; DownloadAdapter mAdapter; + List mData = new ArrayList<>(); @Override protected int setLayoutId() { return R.layout.activity_multi; @@ -50,7 +53,8 @@ public class MultiTaskActivity extends BaseActivity { super.init(savedInstanceState); setSupportActionBar(mBar); mBar.setTitle("多任务下载"); - mAdapter = new DownloadAdapter(this, getModule(DownloadModule.class).getDownloadData()); + mData.addAll(getModule(DownloadModule.class).getDownloadData()); + mAdapter = new DownloadAdapter(this, mData); mList.setLayoutManager(new LinearLayoutManager(this)); mList.setAdapter(mAdapter); } @@ -103,8 +107,8 @@ public class MultiTaskActivity extends BaseActivity { // } //}; - public void onClick(View view){ - switch (view.getId()){ + public void onClick(View view) { + switch (view.getId()) { case R.id.num: DownloadNumDialog dialog = new DownloadNumDialog(this); dialog.show(getSupportFragmentManager(), "download_num"); @@ -112,6 +116,10 @@ public class MultiTaskActivity extends BaseActivity { case R.id.stop_all: Aria.get(this).stopAllTask(); break; + case R.id.add_task: + mData.add(getModule(DownloadModule.class).createRandomDownloadEntity()); + mAdapter.notifyDataSetChanged(); + break; } } @@ -127,12 +135,12 @@ public class MultiTaskActivity extends BaseActivity { @Override protected void dataCallback(int result, Object data) { super.dataCallback(result, data); - if (result == DownloadNumDialog.RESULT_CODE){ + if (result == DownloadNumDialog.RESULT_CODE) { mAdapter.setDownloadNum(Integer.parseInt(data + "")); } } - private class MySchedulerListener extends Aria.SimpleSchedulerListener{ + private class MySchedulerListener extends Aria.SimpleSchedulerListener { @Override public void onTaskPre(Task task) { super.onTaskPre(task); L.d(TAG, "download pre"); diff --git a/app/src/main/java/com/arialyy/simple/module/DownloadModule.java b/app/src/main/java/com/arialyy/simple/module/DownloadModule.java index 186ddcd4..7d383a1c 100644 --- a/app/src/main/java/com/arialyy/simple/module/DownloadModule.java +++ b/app/src/main/java/com/arialyy/simple/module/DownloadModule.java @@ -35,13 +35,29 @@ import com.arialyy.simple.base.BaseModule; import java.io.File; import java.util.ArrayList; import java.util.List; +import java.util.Random; /** * Created by Lyy on 2016/9/27. */ public class DownloadModule extends BaseModule { + private List mTestDownloadUrl = new ArrayList<>(); + public DownloadModule(Context context) { super(context); + mTestDownloadUrl.add("static.gaoshouyou.com/d/e6/f5/4de6329f9cf5dc3a1d1e6bbcca0d003c.apk"); + mTestDownloadUrl.add("static.gaoshouyou.com/d/6e/e5/ff6ecaaf45e532e6d07747af82357472.apk"); + mTestDownloadUrl.add("static.gaoshouyou.com/d/36/69/2d3699acfa69e9632262442c46516ad8.apk"); + } + + public String getRadomUrl() { + Random random = new Random(); + int i = random.nextInt(2); + return mTestDownloadUrl.get(i); + } + + public DownloadEntity createRandomDownloadEntity(){ + return createDownloadEntity(getRadomUrl()); } /** @@ -86,8 +102,8 @@ public class DownloadModule extends BaseModule { } private DownloadEntity createDownloadEntity(String url) { - String fileName = CommonUtil.keyToHashCode(url) + ".apk"; - DownloadEntity entity = new DownloadEntity(); + String fileName = CommonUtil.keyToHashCode(url) + ".apk"; + DownloadEntity entity = new DownloadEntity(); entity.setDownloadUrl(url); entity.setDownloadPath(getDownloadPath(url)); entity.setFileName(fileName); diff --git a/app/src/main/res/layout/activity_multi.xml b/app/src/main/res/layout/activity_multi.xml index 0414246c..ceaedec6 100644 --- a/app/src/main/res/layout/activity_multi.xml +++ b/app/src/main/res/layout/activity_multi.xml @@ -16,6 +16,7 @@ android:layout_width="match_parent" android:layout_height="match_parent" android:layout_below="@+id/toolbar" + android:layout_above="@+id/num" />