From 173a9c50f273f650c9310f09186aab428a69d6f7 Mon Sep 17 00:00:00 2001
From: AriaLyy <511455842@qq.com>
Date: Wed, 4 Jan 2017 18:28:44 +0800
Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0fragment=E6=94=AF=E6=8C=81?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.idea/misc.xml | 2 +-
Aria/build.gradle | 4 +-
.../main/java/com/arialyy/aria/core/Aria.java | 64 ++++++---
app/src/main/AndroidManifest.xml | 1 +
.../simple/activity/FragmentActivity.java | 15 +++
.../arialyy/simple/activity/MainActivity.java | 13 +-
.../simple/fragment/DownloadFragment.java | 125 ++++++++++++++++++
app/src/main/res/layout/activity_fragment.xml | 17 +++
app/src/main/res/layout/activity_main.xml | 9 ++
app/src/main/res/layout/fragment_download.xml | 12 ++
10 files changed, 236 insertions(+), 26 deletions(-)
create mode 100644 app/src/main/java/com/arialyy/simple/activity/FragmentActivity.java
create mode 100644 app/src/main/java/com/arialyy/simple/fragment/DownloadFragment.java
create mode 100644 app/src/main/res/layout/activity_fragment.xml
create mode 100644 app/src/main/res/layout/fragment_download.xml
diff --git a/.idea/misc.xml b/.idea/misc.xml
index 5d199810..fbb68289 100644
--- a/.idea/misc.xml
+++ b/.idea/misc.xml
@@ -37,7 +37,7 @@
-
+
diff --git a/Aria/build.gradle b/Aria/build.gradle
index be8d2a90..4aae21af 100644
--- a/Aria/build.gradle
+++ b/Aria/build.gradle
@@ -7,8 +7,8 @@ android {
defaultConfig {
minSdkVersion 9
targetSdkVersion 23
- versionCode 84
- versionName "2.3.6"
+ versionCode 85
+ versionName "2.3.7"
}
buildTypes {
release {
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 167d7cc4..4aab85f2 100644
--- a/Aria/src/main/java/com/arialyy/aria/core/Aria.java
+++ b/Aria/src/main/java/com/arialyy/aria/core/Aria.java
@@ -24,9 +24,7 @@ import android.app.DialogFragment;
import android.app.Fragment;
import android.app.Service;
import android.content.Context;
-import android.content.ContextWrapper;
import android.os.Build;
-import android.view.ContextThemeWrapper;
import android.widget.PopupWindow;
import com.arialyy.aria.core.scheduler.OnSchedulerListener;
import com.arialyy.aria.core.task.Task;
@@ -51,43 +49,43 @@ import com.arialyy.aria.core.task.Task;
/**
* 预处理完成
*/
- public static final String ACTION_PRE = "ACTION_PRE";
+ public static final String ACTION_PRE = "ACTION_PRE";
/**
* 下载开始前事件
*/
- public static final String ACTION_POST_PRE = "ACTION_POST_PRE";
+ public static final String ACTION_POST_PRE = "ACTION_POST_PRE";
/**
* 开始下载事件
*/
- public static final String ACTION_START = "ACTION_START";
+ public static final String ACTION_START = "ACTION_START";
/**
* 恢复下载事件
*/
- public static final String ACTION_RESUME = "ACTION_RESUME";
+ public static final String ACTION_RESUME = "ACTION_RESUME";
/**
* 正在下载事件
*/
- public static final String ACTION_RUNNING = "ACTION_RUNNING";
+ public static final String ACTION_RUNNING = "ACTION_RUNNING";
/**
* 停止下载事件
*/
- public static final String ACTION_STOP = "ACTION_STOP";
+ public static final String ACTION_STOP = "ACTION_STOP";
/**
* 取消下载事件
*/
- public static final String ACTION_CANCEL = "ACTION_CANCEL";
+ public static final String ACTION_CANCEL = "ACTION_CANCEL";
/**
* 下载完成事件
*/
- public static final String ACTION_COMPLETE = "ACTION_COMPLETE";
+ public static final String ACTION_COMPLETE = "ACTION_COMPLETE";
/**
* 下载失败事件
*/
- public static final String ACTION_FAIL = "ACTION_FAIL";
+ public static final String ACTION_FAIL = "ACTION_FAIL";
/**
* 下载实体
*/
- public static final String ENTITY = "DOWNLOAD_ENTITY";
+ public static final String ENTITY = "DOWNLOAD_ENTITY";
/**
* 位置
*/
@@ -95,7 +93,7 @@ import com.arialyy.aria.core.task.Task;
/**
* 速度
*/
- public static final String CURRENT_SPEED = "CURRENT_SPEED";
+ public static final String CURRENT_SPEED = "CURRENT_SPEED";
private Aria() {
}
@@ -118,7 +116,17 @@ import com.arialyy.aria.core.task.Task;
/**
* 处理Fragment
*/
- private static AMReceiver whit(Fragment fragment) {
+ public static AMReceiver whit(Fragment fragment) {
+ checkNull(fragment);
+ return AriaManager.getInstance(
+ Build.VERSION.SDK_INT >= Build.VERSION_CODES.M ? fragment.getContext()
+ : fragment.getActivity()).get(fragment);
+ }
+
+ /**
+ * 处理Fragment
+ */
+ public static AMReceiver whit(android.support.v4.app.Fragment fragment) {
checkNull(fragment);
return AriaManager.getInstance(
Build.VERSION.SDK_INT >= Build.VERSION_CODES.M ? fragment.getContext()
@@ -151,10 +159,6 @@ import com.arialyy.aria.core.task.Task;
return AriaManager.getInstance(dialog.getContext()).get(dialog);
}
- private static void checkNull(Object obj) {
- if (obj == null) throw new IllegalArgumentException("不能传入空对象");
- }
-
/**
* 处理通用事件
*/
@@ -185,6 +189,30 @@ import com.arialyy.aria.core.task.Task;
return AriaManager.getInstance(popupWindow.getContentView().getContext());
}
+ /**
+ * 处理Fragment的通用任务
+ */
+ public static AriaManager get(Fragment fragment) {
+ checkNull(fragment);
+ return AriaManager.getInstance(
+ Build.VERSION.SDK_INT >= Build.VERSION_CODES.M ? fragment.getContext()
+ : fragment.getActivity());
+ }
+
+ /**
+ * 处理Fragment的通用任务
+ */
+ public static AriaManager get(android.support.v4.app.Fragment fragment) {
+ checkNull(fragment);
+ return AriaManager.getInstance(
+ Build.VERSION.SDK_INT >= Build.VERSION_CODES.M ? fragment.getContext()
+ : fragment.getActivity());
+ }
+
+ private static void checkNull(Object obj) {
+ if (obj == null) throw new IllegalArgumentException("不能传入空对象");
+ }
+
public static class SimpleSchedulerListener implements OnSchedulerListener {
@Override public void onTaskPre(Task task) {
diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml
index 5c882e2a..99eed35e 100644
--- a/app/src/main/AndroidManifest.xml
+++ b/app/src/main/AndroidManifest.xml
@@ -26,6 +26,7 @@
+
diff --git a/app/src/main/java/com/arialyy/simple/activity/FragmentActivity.java b/app/src/main/java/com/arialyy/simple/activity/FragmentActivity.java
new file mode 100644
index 00000000..2d8a4f07
--- /dev/null
+++ b/app/src/main/java/com/arialyy/simple/activity/FragmentActivity.java
@@ -0,0 +1,15 @@
+package com.arialyy.simple.activity;
+
+import com.arialyy.simple.R;
+import com.arialyy.simple.base.BaseActivity;
+import com.arialyy.simple.databinding.FragmentDownloadBinding;
+
+/**
+ * Created by Aria.Lao on 2017/1/4.
+ */
+
+public class FragmentActivity extends BaseActivity {
+ @Override protected int setLayoutId() {
+ return R.layout.activity_fragment;
+ }
+}
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 8959c5a7..4a53c3cd 100644
--- a/app/src/main/java/com/arialyy/simple/activity/MainActivity.java
+++ b/app/src/main/java/com/arialyy/simple/activity/MainActivity.java
@@ -38,11 +38,11 @@ import com.arialyy.simple.pop.DownloadPopupWindow;
* 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.dialog_task) Button mDialogBt;
- @Bind(R.id.pop_task) Button mPopBt;
+ @Bind(R.id.toolbar) Toolbar mBar;
+ @Bind(R.id.single_task) Button mSigleBt;
+ @Bind(R.id.multi_task) Button mMultiBt;
+ @Bind(R.id.dialog_task) Button mDialogBt;
+ @Bind(R.id.pop_task) Button mPopBt;
@Override protected int setLayoutId() {
return R.layout.activity_main;
@@ -99,6 +99,9 @@ public class MainActivity extends BaseActivity {
//pop.showAsDropDown(mRootView);
pop.showAtLocation(mRootView, Gravity.CENTER_VERTICAL, 0, 0);
break;
+ case R.id.fragment_task:
+ startActivity(new Intent(this, FragmentActivity.class));
+ break;
}
}
}
\ No newline at end of file
diff --git a/app/src/main/java/com/arialyy/simple/fragment/DownloadFragment.java b/app/src/main/java/com/arialyy/simple/fragment/DownloadFragment.java
new file mode 100644
index 00000000..0a9f3b4e
--- /dev/null
+++ b/app/src/main/java/com/arialyy/simple/fragment/DownloadFragment.java
@@ -0,0 +1,125 @@
+package com.arialyy.simple.fragment;
+
+import android.os.Bundle;
+import android.os.Environment;
+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.DownloadEntity;
+import com.arialyy.aria.core.task.Task;
+import com.arialyy.aria.util.CommonUtil;
+import com.arialyy.frame.core.AbsFragment;
+import com.arialyy.simple.R;
+import com.arialyy.simple.databinding.FragmentDownloadBinding;
+import com.arialyy.simple.dialog.DownloadDialog;
+import com.arialyy.simple.widget.HorizontalProgressBarWithNumber;
+
+/**
+ * Created by Aria.Lao on 2017/1/4.
+ */
+public class DownloadFragment extends AbsFragment {
+ @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";
+
+ @Override protected void init(Bundle savedInstanceState) {
+ if (Aria.get(this).taskExists(DOWNLOAD_URL)) {
+ AMTarget target = Aria.whit(this).load(DOWNLOAD_URL);
+ int p = (int) (target.getCurrentProgress() * 100 / target.getFileSize());
+ mPb.setProgress(p);
+ }
+ DownloadEntity entity = Aria.get(this).getDownloadEntity(DOWNLOAD_URL);
+ if (entity != null) {
+ mSize.setText(CommonUtil.formatFileSize(entity.getFileSize()));
+ int state = entity.getState();
+ setBtState(state != DownloadEntity.STATE_DOWNLOAD_ING);
+ } else {
+ setBtState(true);
+ }
+ }
+
+ @Override public void onResume() {
+ super.onResume();
+ Aria.whit(this).addSchedulerListener(new DownloadFragment.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(this)
+ .load(DOWNLOAD_URL)
+ .setDownloadPath(Environment.getExternalStorageDirectory().getPath() + "/daialog.apk")
+ .setDownloadName("daialog.apk")
+ .start();
+ break;
+ case R.id.stop:
+ Aria.whit(this).load(DOWNLOAD_URL).stop();
+ break;
+ case R.id.cancel:
+ Aria.whit(this).load(DOWNLOAD_URL).cancel();
+ break;
+ }
+ }
+
+ @Override protected void onDelayLoad() {
+
+ }
+
+ @Override protected int setLayoutId() {
+ return R.layout.fragment_download;
+ }
+
+ @Override protected void dataCallback(int result, Object obj) {
+
+ }
+
+ private void setBtState(boolean startEnable) {
+ mStart.setEnabled(startEnable);
+ mCancel.setEnabled(!startEnable);
+ mStop.setEnabled(!startEnable);
+ }
+
+ private class MyDialogDownloadCallback extends Aria.SimpleSchedulerListener {
+
+ @Override public void onTaskPre(Task task) {
+ super.onTaskPre(task);
+ mSize.setText(CommonUtil.formatFileSize(task.getFileSize()));
+ setBtState(false);
+ }
+
+ @Override public void onTaskStop(Task task) {
+ super.onTaskStop(task);
+ setBtState(true);
+ mSpeed.setText("0.0kb/s");
+ }
+
+ @Override public void onTaskCancel(Task task) {
+ super.onTaskCancel(task);
+ setBtState(true);
+ mPb.setProgress(0);
+ mSpeed.setText("0.0kb/s");
+ }
+
+ @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_fragment.xml b/app/src/main/res/layout/activity_fragment.xml
new file mode 100644
index 00000000..03bb007d
--- /dev/null
+++ b/app/src/main/res/layout/activity_fragment.xml
@@ -0,0 +1,17 @@
+
+
+
+
+
+
+
+
diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml
index 87847e7a..464bb3a5 100644
--- a/app/src/main/res/layout/activity_main.xml
+++ b/app/src/main/res/layout/activity_main.xml
@@ -47,5 +47,14 @@
style="?buttonBarButtonStyle"
/>
+
+
diff --git a/app/src/main/res/layout/fragment_download.xml b/app/src/main/res/layout/fragment_download.xml
new file mode 100644
index 00000000..1656210f
--- /dev/null
+++ b/app/src/main/res/layout/fragment_download.xml
@@ -0,0 +1,12 @@
+
+
+
+
+
+
+
+