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. 12
      app/src/main/java/com/arialyy/simple/activity/MultiTaskActivity.java
  4. 16
      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) {
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);
}
/**

@ -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<IDownloadCmd> 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();
}
/**

@ -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,6 +33,8 @@ 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.
@ -41,6 +43,7 @@ public class MultiTaskActivity extends BaseActivity<ActivityMultiBinding> {
@Bind(R.id.list) RecyclerView mList;
@Bind(R.id.toolbar) Toolbar mBar;
DownloadAdapter mAdapter;
List<DownloadEntity> mData = new ArrayList<>();
@Override protected int setLayoutId() {
return R.layout.activity_multi;
@ -50,7 +53,8 @@ public class MultiTaskActivity extends BaseActivity<ActivityMultiBinding> {
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);
}
@ -112,6 +116,10 @@ public class MultiTaskActivity extends BaseActivity<ActivityMultiBinding> {
case R.id.stop_all:
Aria.get(this).stopAllTask();
break;
case R.id.add_task:
mData.add(getModule(DownloadModule.class).createRandomDownloadEntity());
mAdapter.notifyDataSetChanged();
break;
}
}

@ -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<String> 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());
}
/**

@ -16,6 +16,7 @@
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/toolbar"
android:layout_above="@+id/num"
/>
<Button
@ -40,5 +41,17 @@
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>
</layout>

Loading…
Cancel
Save