From 9c428bcdb8633444c1aa4328c31ab0459a18ec01 Mon Sep 17 00:00:00 2001 From: AriaLyy <511455842@qq.com> Date: Mon, 2 Jan 2017 17:21:13 +0800 Subject: [PATCH 1/5] =?UTF-8?q?=E4=BF=AE=E6=94=B9aria=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/misc.xml | 2 +- Aria/src/main/java/com/arialyy/aria/core/Aria.java | 14 +++++++------- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/.idea/misc.xml b/.idea/misc.xml index fbb68289..5d199810 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -37,7 +37,7 @@ - + diff --git a/Aria/src/main/java/com/arialyy/aria/core/Aria.java b/Aria/src/main/java/com/arialyy/aria/core/Aria.java index 705352d3..c45f3be8 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/Aria.java +++ b/Aria/src/main/java/com/arialyy/aria/core/Aria.java @@ -34,13 +34,13 @@ import com.arialyy.aria.core.task.Task; * Aria启动,管理全局任务 *
  *   
- *      DownloadEntity mEntity = new DownloadEntity();
- *      mEntity.setFileName(fileName);          //设置文件名
- *      mEntity.setDownloadUrl(downloadUrl);    //设置下载链接
- *      mEntity.setDownloadPath(downloadPath);  //设置存放路径
- *
- *      //启动下载
- *     Aria.whit(this).load(mEntity).start();
+ *   //启动下载
+ *   Aria.whit(this)
+ *       .load(DOWNLOAD_URL)     //下载地址,必填
+ *       //文件保存路径,必填
+ *       .setDownloadPath(Environment.getExternalStorageDirectory().getPath() + "/test.apk")
+ *       .setDownloadName("test.apk")    //文件名,必填
+ *       .start();
  *   
  * 
*/ From 0887cc8a4c2e4707bc49606e90129dad46c44501 Mon Sep 17 00:00:00 2001 From: AriaLyy <511455842@qq.com> Date: Mon, 2 Jan 2017 19:53:37 +0800 Subject: [PATCH 2/5] dialog --- .../main/java/com/arialyy/aria/core/Aria.java | 8 ++ .../arialyy/simple/activity/MainActivity.java | 11 ++- .../arialyy/simple/dialog/DownloadDialog.java | 88 +++++++++++++++++++ app/src/main/res/layout/activity_main.xml | 11 ++- 4 files changed, 114 insertions(+), 4 deletions(-) create mode 100644 app/src/main/java/com/arialyy/simple/dialog/DownloadDialog.java diff --git a/Aria/src/main/java/com/arialyy/aria/core/Aria.java b/Aria/src/main/java/com/arialyy/aria/core/Aria.java index c45f3be8..e3a2cbc4 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/Aria.java +++ b/Aria/src/main/java/com/arialyy/aria/core/Aria.java @@ -148,6 +148,14 @@ import com.arialyy.aria.core.task.Task; } } + /** + * 处理Dialog的通用任务 + */ + public static AMReceiver get(Dialog dialog) { + checkNull(dialog); + return AriaManager.getInstance(dialog.getContext()).get(dialog); + } + public static class SimpleSchedulerListener implements OnSchedulerListener { @Override public void onTaskPre(Task task) { diff --git a/app/src/main/java/com/arialyy/simple/activity/MainActivity.java b/app/src/main/java/com/arialyy/simple/activity/MainActivity.java index f0b71af9..b3c957d9 100644 --- a/app/src/main/java/com/arialyy/simple/activity/MainActivity.java +++ b/app/src/main/java/com/arialyy/simple/activity/MainActivity.java @@ -30,14 +30,15 @@ import com.arialyy.frame.util.show.T; import com.arialyy.simple.R; import com.arialyy.simple.base.BaseActivity; import com.arialyy.simple.databinding.ActivityMainBinding; +import com.arialyy.simple.dialog.DownloadDialog; /** * Created by Lyy on 2016/10/13. */ public class MainActivity extends BaseActivity { - @Bind(R.id.toolbar) Toolbar mBar; - @Bind(R.id.single_task) Button mSigleBt; - @Bind(R.id.multi_task) Button mMultiBt; + @Bind(R.id.toolbar) Toolbar mBar; + @Bind(R.id.single_task) Button mSigleBt; + @Bind(R.id.multi_task) Button mMultiBt; @Override protected int setLayoutId() { return R.layout.activity_main; @@ -83,6 +84,10 @@ public class MainActivity extends BaseActivity { case R.id.multi_task: startActivity(new Intent(this, MultiTaskActivity.class)); break; + case R.id.dialog_task: + DownloadDialog dialog = new DownloadDialog(this); + dialog.show(); + break; } } } \ No newline at end of file diff --git a/app/src/main/java/com/arialyy/simple/dialog/DownloadDialog.java b/app/src/main/java/com/arialyy/simple/dialog/DownloadDialog.java new file mode 100644 index 00000000..bb087dc8 --- /dev/null +++ b/app/src/main/java/com/arialyy/simple/dialog/DownloadDialog.java @@ -0,0 +1,88 @@ +package com.arialyy.simple.dialog; + +import android.content.Context; +import android.os.Environment; +import android.support.v7.widget.Toolbar; +import android.view.View; +import android.widget.Button; +import android.widget.TextView; +import butterknife.Bind; +import butterknife.OnClick; +import com.arialyy.aria.core.AMTarget; +import com.arialyy.aria.core.Aria; +import com.arialyy.aria.core.task.Task; +import com.arialyy.aria.util.CommonUtil; +import com.arialyy.frame.core.AbsDialog; +import com.arialyy.simple.R; +import com.arialyy.simple.base.BaseDialog; +import com.arialyy.simple.widget.HorizontalProgressBarWithNumber; + +/** + * Created by AriaL on 2017/1/2. + */ + +public class DownloadDialog extends AbsDialog { + @Bind(R.id.progressBar) HorizontalProgressBarWithNumber mPb; + @Bind(R.id.start) Button mStart; + @Bind(R.id.stop) Button mStop; + @Bind(R.id.cancel) Button mCancel; + @Bind(R.id.size) TextView mSize; + @Bind(R.id.speed) TextView mSpeed; + + private static final String DOWNLOAD_URL = + "http://static.gaoshouyou.com/d/3a/93/573ae1db9493a801c24bf66128b11e39.apk"; + + public DownloadDialog(Context context) { + super(context); + init(); + } + + @Override protected int setLayoutId() { + return R.layout.content_single; + } + + private void init() { + if (Aria.get(getContext()).taskExists(DOWNLOAD_URL)) { + AMTarget target = Aria.whit(getContext()).load(DOWNLOAD_URL); + int p = (int) (target.getCurrentProgress() * 100 / target.getFileSize()); + mPb.setProgress(p); + } + Aria.whit(this).addSchedulerListener(new MyDialogDownloadCallback()); + } + + @OnClick({ R.id.start, R.id.stop, R.id.cancel }) public void onClick(View view) { + switch (view.getId()) { + case R.id.start: + Aria.whit(getContext()) + .load(DOWNLOAD_URL) + .setDownloadPath(Environment.getExternalStorageDirectory().getPath() + "/daialog.apk") + .setDownloadName("daialog.apk") + .start(); + break; + case R.id.stop: + Aria.whit(getContext()).load(DOWNLOAD_URL).stop(); + break; + case R.id.cancel: + Aria.whit(getContext()).load(DOWNLOAD_URL).cancel(); + break; + } + } + + @Override protected void dataCallback(int result, Object obj) { + + } + + private class MyDialogDownloadCallback extends Aria.SimpleSchedulerListener { + @Override public void onTaskRunning(Task task) { + super.onTaskRunning(task); + long current = task.getCurrentProgress(); + long len = task.getFileSize(); + if (len == 0) { + mPb.setProgress(0); + } else { + mPb.setProgress((int) ((current * 100) / len)); + } + mSpeed.setText(CommonUtil.formatFileSize(task.getSpeed()) + "/s"); + } + } +} diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index d3137e4e..aeecab69 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -3,9 +3,9 @@ > @@ -29,5 +29,14 @@ style="?buttonBarButtonStyle" /> +