pull/9/head
AriaLyy 8 years ago
parent 173a9c50f2
commit ec7cfd3549
  1. 4
      Aria/src/main/java/com/arialyy/aria/core/AMReceiver.java
  2. 33
      Aria/src/main/java/com/arialyy/aria/core/AMTarget.java
  3. 24
      app/src/main/java/com/arialyy/simple/activity/MultiTaskActivity.java
  4. 20
      app/src/main/java/com/arialyy/simple/module/DownloadModule.java
  5. 13
      app/src/main/res/layout/activity_multi.xml

@ -37,7 +37,7 @@ public class AMReceiver {
*/ */
@Deprecated public AMTarget load(DownloadEntity entity) { @Deprecated public AMTarget load(DownloadEntity entity) {
this.entity = entity; this.entity = entity;
return new AMTarget(this); return new AMTarget(entity, targetName);
} }
/** /**
@ -52,7 +52,7 @@ public class AMReceiver {
entity = new DownloadEntity(); entity = new DownloadEntity();
} }
entity.setDownloadUrl(downloadUrl); entity.setDownloadUrl(downloadUrl);
return new AMTarget(this); return new AMTarget(entity, targetName);
} }
/** /**

@ -29,10 +29,13 @@ import java.util.List;
* https://github.com/AriaLyy/Aria * https://github.com/AriaLyy/Aria
*/ */
public class AMTarget { public class AMTarget {
private AMReceiver mReceiver; //private AMReceiver mReceiver;
DownloadEntity entity;
String targetName;
AMTarget(AMReceiver receiver) { AMTarget(DownloadEntity entity, String targetName) {
this.mReceiver = receiver; this.entity = entity;
this.targetName = targetName;
} }
/** /**
@ -42,7 +45,7 @@ public class AMTarget {
if (TextUtils.isEmpty(downloadPath)) { if (TextUtils.isEmpty(downloadPath)) {
throw new IllegalArgumentException("文件保持路径不能为null"); throw new IllegalArgumentException("文件保持路径不能为null");
} }
mReceiver.entity.setDownloadPath(downloadPath); entity.setDownloadPath(downloadPath);
return this; return this;
} }
@ -53,7 +56,7 @@ public class AMTarget {
if (TextUtils.isEmpty(downloadName)) { if (TextUtils.isEmpty(downloadName)) {
throw new IllegalArgumentException("文件名不能为null"); throw new IllegalArgumentException("文件名不能为null");
} }
mReceiver.entity.setFileName(downloadName); entity.setFileName(downloadName);
return this; return this;
} }
@ -61,7 +64,7 @@ public class AMTarget {
* 获取下载文件大小 * 获取下载文件大小
*/ */
public long getFileSize() { public long getFileSize() {
DownloadEntity entity = getDownloadEntity(mReceiver.entity.getDownloadUrl()); DownloadEntity entity = getDownloadEntity(this.entity.getDownloadUrl());
if (entity == null) { if (entity == null) {
throw new NullPointerException("下载管理器中没有改任务"); throw new NullPointerException("下载管理器中没有改任务");
} }
@ -72,7 +75,7 @@ public class AMTarget {
* 获取当前下载进度如果下載实体存在则返回当前进度 * 获取当前下载进度如果下載实体存在则返回当前进度
*/ */
public long getCurrentProgress() { public long getCurrentProgress() {
DownloadEntity entity = getDownloadEntity(mReceiver.entity.getDownloadUrl()); DownloadEntity entity = getDownloadEntity(this.entity.getDownloadUrl());
if (entity == null) { if (entity == null) {
throw new NullPointerException("下载管理器中没有改任务"); throw new NullPointerException("下载管理器中没有改任务");
} }
@ -89,8 +92,7 @@ public class AMTarget {
*/ */
public void add() { public void add() {
DownloadManager.getInstance() DownloadManager.getInstance()
.setCmd( .setCmd(CommonUtil.createCmd(targetName, entity, CmdFactory.TASK_CREATE))
CommonUtil.createCmd(mReceiver.targetName, mReceiver.entity, CmdFactory.TASK_CREATE))
.exe(); .exe();
} }
@ -99,8 +101,8 @@ public class AMTarget {
*/ */
public void start() { public void start() {
List<IDownloadCmd> cmds = new ArrayList<>(); List<IDownloadCmd> cmds = new ArrayList<>();
cmds.add(CommonUtil.createCmd(mReceiver.targetName, mReceiver.entity, CmdFactory.TASK_CREATE)); cmds.add(CommonUtil.createCmd(targetName, entity, CmdFactory.TASK_CREATE));
cmds.add(CommonUtil.createCmd(mReceiver.targetName, mReceiver.entity, CmdFactory.TASK_START)); cmds.add(CommonUtil.createCmd(targetName, entity, CmdFactory.TASK_START));
DownloadManager.getInstance().setCmds(cmds).exe(); DownloadManager.getInstance().setCmds(cmds).exe();
cmds.clear(); cmds.clear();
} }
@ -110,7 +112,7 @@ public class AMTarget {
*/ */
public void stop() { public void stop() {
DownloadManager.getInstance() DownloadManager.getInstance()
.setCmd(CommonUtil.createCmd(mReceiver.targetName, mReceiver.entity, CmdFactory.TASK_STOP)) .setCmd(CommonUtil.createCmd(targetName, entity, CmdFactory.TASK_STOP))
.exe(); .exe();
} }
@ -119,7 +121,7 @@ public class AMTarget {
*/ */
public void resume() { public void resume() {
DownloadManager.getInstance() DownloadManager.getInstance()
.setCmd(CommonUtil.createCmd(mReceiver.targetName, mReceiver.entity, CmdFactory.TASK_START)) .setCmd(CommonUtil.createCmd(targetName, entity, CmdFactory.TASK_START))
.exe(); .exe();
} }
@ -128,8 +130,7 @@ public class AMTarget {
*/ */
public void cancel() { public void cancel() {
DownloadManager.getInstance() DownloadManager.getInstance()
.setCmd( .setCmd(CommonUtil.createCmd(targetName, entity, CmdFactory.TASK_CANCEL))
CommonUtil.createCmd(mReceiver.targetName, mReceiver.entity, CmdFactory.TASK_CANCEL))
.exe(); .exe();
} }
@ -137,7 +138,7 @@ public class AMTarget {
* 是否在下载 * 是否在下载
*/ */
public boolean isDownloading() { public boolean isDownloading() {
return DownloadManager.getInstance().getTaskQueue().getTask(mReceiver.entity).isDownloading(); return DownloadManager.getInstance().getTaskQueue().getTask(entity).isDownloading();
} }
/** /**

@ -14,7 +14,6 @@
* limitations under the License. * limitations under the License.
*/ */
package com.arialyy.simple.activity; package com.arialyy.simple.activity;
import android.os.Bundle; import android.os.Bundle;
@ -25,6 +24,7 @@ import android.view.View;
import butterknife.Bind; import butterknife.Bind;
import com.arialyy.aria.core.AMTarget; import com.arialyy.aria.core.AMTarget;
import com.arialyy.aria.core.Aria; import com.arialyy.aria.core.Aria;
import com.arialyy.aria.core.DownloadEntity;
import com.arialyy.aria.core.task.Task; import com.arialyy.aria.core.task.Task;
import com.arialyy.frame.util.show.L; import com.arialyy.frame.util.show.L;
import com.arialyy.simple.R; 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.databinding.ActivityMultiBinding;
import com.arialyy.simple.dialog.DownloadNumDialog; import com.arialyy.simple.dialog.DownloadNumDialog;
import com.arialyy.simple.module.DownloadModule; import com.arialyy.simple.module.DownloadModule;
import java.util.ArrayList;
import java.util.List;
/** /**
* Created by Lyy on 2016/9/27. * Created by Lyy on 2016/9/27.
*/ */
public class MultiTaskActivity extends BaseActivity<ActivityMultiBinding> { public class MultiTaskActivity extends BaseActivity<ActivityMultiBinding> {
@Bind(R.id.list) RecyclerView mList; @Bind(R.id.list) RecyclerView mList;
@Bind(R.id.toolbar) Toolbar mBar; @Bind(R.id.toolbar) Toolbar mBar;
DownloadAdapter mAdapter; DownloadAdapter mAdapter;
List<DownloadEntity> mData = new ArrayList<>();
@Override protected int setLayoutId() { @Override protected int setLayoutId() {
return R.layout.activity_multi; return R.layout.activity_multi;
@ -50,7 +53,8 @@ public class MultiTaskActivity extends BaseActivity<ActivityMultiBinding> {
super.init(savedInstanceState); super.init(savedInstanceState);
setSupportActionBar(mBar); setSupportActionBar(mBar);
mBar.setTitle("多任务下载"); 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.setLayoutManager(new LinearLayoutManager(this));
mList.setAdapter(mAdapter); mList.setAdapter(mAdapter);
} }
@ -103,8 +107,8 @@ public class MultiTaskActivity extends BaseActivity<ActivityMultiBinding> {
// } // }
//}; //};
public void onClick(View view){ public void onClick(View view) {
switch (view.getId()){ switch (view.getId()) {
case R.id.num: case R.id.num:
DownloadNumDialog dialog = new DownloadNumDialog(this); DownloadNumDialog dialog = new DownloadNumDialog(this);
dialog.show(getSupportFragmentManager(), "download_num"); dialog.show(getSupportFragmentManager(), "download_num");
@ -112,6 +116,10 @@ public class MultiTaskActivity extends BaseActivity<ActivityMultiBinding> {
case R.id.stop_all: case R.id.stop_all:
Aria.get(this).stopAllTask(); Aria.get(this).stopAllTask();
break; break;
case R.id.add_task:
mData.add(getModule(DownloadModule.class).createRandomDownloadEntity());
mAdapter.notifyDataSetChanged();
break;
} }
} }
@ -127,12 +135,12 @@ public class MultiTaskActivity extends BaseActivity<ActivityMultiBinding> {
@Override protected void dataCallback(int result, Object data) { @Override protected void dataCallback(int result, Object data) {
super.dataCallback(result, data); super.dataCallback(result, data);
if (result == DownloadNumDialog.RESULT_CODE){ if (result == DownloadNumDialog.RESULT_CODE) {
mAdapter.setDownloadNum(Integer.parseInt(data + "")); mAdapter.setDownloadNum(Integer.parseInt(data + ""));
} }
} }
private class MySchedulerListener extends Aria.SimpleSchedulerListener{ private class MySchedulerListener extends Aria.SimpleSchedulerListener {
@Override public void onTaskPre(Task task) { @Override public void onTaskPre(Task task) {
super.onTaskPre(task); super.onTaskPre(task);
L.d(TAG, "download pre"); L.d(TAG, "download pre");

@ -35,13 +35,29 @@ import com.arialyy.simple.base.BaseModule;
import java.io.File; import java.io.File;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Random;
/** /**
* Created by Lyy on 2016/9/27. * Created by Lyy on 2016/9/27.
*/ */
public class DownloadModule extends BaseModule { public class DownloadModule extends BaseModule {
private List<String> mTestDownloadUrl = new ArrayList<>();
public DownloadModule(Context context) { public DownloadModule(Context context) {
super(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) { private DownloadEntity createDownloadEntity(String url) {
String fileName = CommonUtil.keyToHashCode(url) + ".apk"; String fileName = CommonUtil.keyToHashCode(url) + ".apk";
DownloadEntity entity = new DownloadEntity(); DownloadEntity entity = new DownloadEntity();
entity.setDownloadUrl(url); entity.setDownloadUrl(url);
entity.setDownloadPath(getDownloadPath(url)); entity.setDownloadPath(getDownloadPath(url));
entity.setFileName(fileName); entity.setFileName(fileName);

@ -16,6 +16,7 @@
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:layout_below="@+id/toolbar" android:layout_below="@+id/toolbar"
android:layout_above="@+id/num"
/> />
<Button <Button
@ -40,5 +41,17 @@
android:text="停止所有" android:text="停止所有"
/> />
<Button
android:id="@+id/add_task"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_margin="16dp"
android:onClick="onClick"
android:text="添加任务"
/>
</RelativeLayout> </RelativeLayout>
</layout> </layout>

Loading…
Cancel
Save