添加Dialog、popupWindow支持

pull/4/head
AriaLyy 8 years ago
parent 765f42d73c
commit 47993ba297
  1. 4
      Aria/build.gradle
  2. 37
      Aria/src/main/java/com/arialyy/aria/core/Aria.java
  3. 86
      Aria/src/main/java/com/arialyy/aria/core/AriaManager.java
  4. 4
      Aria/src/main/java/com/arialyy/aria/core/scheduler/DownloadSchedulers.java
  5. 6
      README.md
  6. 11
      app/src/main/java/com/arialyy/simple/activity/MainActivity.java
  7. 50
      app/src/main/java/com/arialyy/simple/dialog/DownloadDialog.java
  8. 124
      app/src/main/java/com/arialyy/simple/pop/DownloadPopupWindow.java
  9. 9
      app/src/main/res/layout/activity_main.xml
  10. 79
      app/src/main/res/layout/dialog_download.xml
  11. BIN
      img/dialog_use.gif
  12. BIN
      img/popupwindow_use.gif

@ -7,8 +7,8 @@ android {
defaultConfig {
minSdkVersion 9
targetSdkVersion 23
versionCode 82
versionName "2.3.4"
versionCode 84
versionName "2.3.6"
}
buildTypes {
release {

@ -24,7 +24,10 @@ 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;
@ -113,15 +116,33 @@ import com.arialyy.aria.core.task.Task;
}
/**
* 处理Fragment或者DialogFragment
* 处理Fragment
*/
public static AMReceiver whit(Fragment fragment) {
private 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或者DialogFragment
*/
public static AMReceiver whit(DialogFragment dialog) {
checkNull(dialog);
return AriaManager.getInstance(
Build.VERSION.SDK_INT >= Build.VERSION_CODES.M ? dialog.getContext() : dialog.getActivity())
.get(dialog);
}
/**
* 处理popupwindow
*/
public static AMReceiver whit(PopupWindow popupWindow) {
checkNull(popupWindow);
return AriaManager.getInstance(popupWindow.getContentView().getContext()).get(popupWindow);
}
/**
* 处理Dialog
*/
@ -151,9 +172,17 @@ import com.arialyy.aria.core.task.Task;
/**
* 处理Dialog的通用任务
*/
public static AMReceiver get(Dialog dialog) {
public static AriaManager get(Dialog dialog) {
checkNull(dialog);
return AriaManager.getInstance(dialog.getContext()).get(dialog);
return AriaManager.getInstance(dialog.getContext());
}
/**
* 处理Dialog的通用任务
*/
public static AriaManager get(PopupWindow popupWindow) {
checkNull(popupWindow);
return AriaManager.getInstance(popupWindow.getContentView().getContext());
}
public static class SimpleSchedulerListener implements OnSchedulerListener {

@ -28,13 +28,12 @@ import android.os.Message;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.util.Log;
import android.widget.PopupWindow;
import com.arialyy.aria.core.command.CmdFactory;
import com.arialyy.aria.core.scheduler.OnSchedulerListener;
import com.arialyy.aria.util.CheckUtil;
import com.arialyy.aria.util.CommonUtil;
import com.arialyy.aria.core.command.IDownloadCmd;
import com.arialyy.aria.util.Configuration;
import java.io.File;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
@ -187,29 +186,68 @@ import java.util.Set;
Activity activity = ((Dialog) obj).getOwnerActivity();
if (activity != null) {
key = clsName + "_" + activity.getClass().getName();
} else {
key = clsName;
}
handleDialogDialogLift((Dialog) obj);
handleDialogLift((Dialog) obj);
} else if (obj instanceof PopupWindow) {
Context context = ((PopupWindow) obj).getContentView().getContext();
if (context instanceof Activity) {
key = clsName + "_" + context.getClass().getName();
} else {
key = clsName;
}
handlePopupWindowLift((PopupWindow) obj);
}
} else {
key = clsName;
}
if (TextUtils.isEmpty(key)) {
throw new IllegalArgumentException("未知类型");
} else {
}
target = mTargets.get(key);
if (target == null) {
target = new AMReceiver();
target.targetName = obj.getClass().getName();
mTargets.put(key, target);
}
}
return target;
}
/**
* 出来悬浮框取消或dismiss
*/
private void handlePopupWindowLift(PopupWindow popupWindow) {
try {
Field dismissField = CommonUtil.getField(popupWindow.getClass(), "mOnDismissListener");
PopupWindow.OnDismissListener listener =
(PopupWindow.OnDismissListener) dismissField.get(popupWindow);
if (listener != null) {
Log.e(TAG, "你已经对PopupWindow设置了Dismiss事件。为了防止内存泄露,"
+ "请在dismiss方法中调用Aria.whit(this).removeSchedulerListener();来注销事件");
} else {
popupWindow.setOnDismissListener(createPopupWindowListener(popupWindow));
}
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
/**
* 创建popupWindow dismiss事件
*/
private PopupWindow.OnDismissListener createPopupWindowListener(final PopupWindow popupWindow) {
return new PopupWindow.OnDismissListener() {
@Override public void onDismiss() {
destroySchedulerListener(popupWindow);
}
};
}
/**
* 处理对话框取消或dismiss
*/
private void handleDialogDialogLift(Dialog dialog) {
private void handleDialogLift(Dialog dialog) {
try {
Field dismissField = CommonUtil.getField(dialog.getClass(), "mDismissMessage");
Message dismissMsg = (Message) dismissField.get(dialog);
@ -221,10 +259,10 @@ import java.util.Set;
Log.e(TAG, "你已经对Dialog设置了Dismiss和cancel事件。为了防止内存泄露,"
+ "请在dismiss方法中调用Aria.whit(this).removeSchedulerListener();来注销事件");
} else {
dialog.setCancelMessage(createCancelMessage());
dialog.setOnCancelListener(createCancelListener());
}
} else {
dialog.setCancelMessage(createDismissMessage());
dialog.setOnDismissListener(createDismissListener());
}
} catch (IllegalAccessException e) {
e.printStackTrace();
@ -251,33 +289,27 @@ import java.util.Set;
}
/**
* 创建Dialog取消消息
* 创建Dialog取消事件
*/
private Message createCancelMessage() {
final Message cancelMsg = new Message();
cancelMsg.what = 0x44;
cancelMsg.obj = new Dialog.OnCancelListener() {
private Dialog.OnCancelListener createCancelListener() {
return new Dialog.OnCancelListener() {
@Override public void onCancel(DialogInterface dialog) {
destroySchedulerListener(dialog);
}
};
return cancelMsg;
}
/**
* 创建Dialog dismiss取消消息
* 创建Dialog dismiss取消事件
*/
private Message createDismissMessage() {
final Message cancelMsg = new Message();
cancelMsg.what = 0x43;
cancelMsg.obj = new Dialog.OnDismissListener() {
private Dialog.OnDismissListener createDismissListener() {
return new Dialog.OnDismissListener() {
@Override public void onDismiss(DialogInterface dialog) {
destroySchedulerListener(dialog);
}
};
return cancelMsg;
}
/**
@ -294,25 +326,13 @@ import java.util.Set;
AMReceiver receiver = mTargets.get(key);
if (receiver.obj != null) {
if (receiver.obj instanceof Application || receiver.obj instanceof Service) break;
}
receiver.removeSchedulerListener();
receiver.destroy();
iter.remove();
}
break;
}
}
//for (String key : keys) {
// if (key.equals(clsName) || key.contains(clsName)) {
// AMReceiver receiver = mTargets.get(key);
// if (receiver.obj != null) {
// if (receiver.obj instanceof Application || receiver.obj instanceof Service) break;
// receiver.removeSchedulerListener();
// receiver.destroy();
// mTargets.remove(key);
// }
// break;
// }
//}
}
/**

@ -93,10 +93,6 @@ public class DownloadSchedulers implements IDownloadSchedulers {
@Override
public void addSchedulerListener(String targetName, OnSchedulerListener schedulerListener) {
if (mSchedulerListeners.get(targetName) != null) {
Log.w(TAG, "监听器已存在");
return;
}
mSchedulerListeners.put(targetName, schedulerListener);
}

@ -3,6 +3,7 @@
Aria,致力于让下载傻瓜化</br>
+ Aria有以下特点:
- 简单
- 可在Dialog、popupWindow等组件中使用
- 可自定义是否使用广播
- 支持多线程、多任务下载
- 支持任务自动切换
@ -15,14 +16,14 @@ Aria,致力于让下载傻瓜化</br>
## 下载
[![Download](https://api.bintray.com/packages/arialyy/maven/Aria/images/download.svg)](https://bintray.com/arialyy/maven/Aria/_latestVersion)</br>
```java
compile 'com.arialyy.aria:Aria:2.3.3'
compile 'com.arialyy.aria:Aria:2.3.6'
```
## 示例
![多任务下载](https://github.com/AriaLyy/DownloadUtil/blob/v_2.0/img/download_img.gif)
![单任务下载](https://github.com/AriaLyy/DownloadUtil/blob/master/img/11.gif "")
![Dialog使用](https://github.com/AriaLyy/DownloadUtil/blob/v_2.0/img/dialog_use.gif "")
## 性能展示
![性能展示](https://github.com/AriaLyy/DownloadUtil/blob/v_2.0/img/performance.png)
@ -113,6 +114,7 @@ compile 'com.arialyy.aria:Aria:2.3.3'
***
## 开发日志
+ v_2.3.6 添加dialog、popupWindow支持
+ v_2.3.3
- 添加断点支持
- 修改下载逻辑,让使用更加简单

@ -21,6 +21,7 @@ import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import butterknife.Bind;
@ -31,6 +32,7 @@ import com.arialyy.simple.R;
import com.arialyy.simple.base.BaseActivity;
import com.arialyy.simple.databinding.ActivityMainBinding;
import com.arialyy.simple.dialog.DownloadDialog;
import com.arialyy.simple.pop.DownloadPopupWindow;
/**
* Created by Lyy on 2016/10/13.
@ -39,6 +41,8 @@ public class MainActivity extends BaseActivity<ActivityMainBinding> {
@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;
@ -74,6 +78,8 @@ public class MainActivity extends BaseActivity<ActivityMainBinding> {
private void setEnable(boolean enable) {
mSigleBt.setEnabled(enable);
mMultiBt.setEnabled(enable);
mDialogBt.setEnabled(enable);
mPopBt.setEnabled(enable);
}
public void onClick(View view) {
@ -88,6 +94,11 @@ public class MainActivity extends BaseActivity<ActivityMainBinding> {
DownloadDialog dialog = new DownloadDialog(this);
dialog.show();
break;
case R.id.pop_task:
DownloadPopupWindow pop = new DownloadPopupWindow(this);
//pop.showAsDropDown(mRootView);
pop.showAtLocation(mRootView, Gravity.CENTER_VERTICAL, 0, 0);
break;
}
}
}

@ -2,7 +2,6 @@ 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;
@ -10,17 +9,16 @@ 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.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;
@ -38,32 +36,40 @@ public class DownloadDialog extends AbsDialog {
}
@Override protected int setLayoutId() {
return R.layout.content_single;
return R.layout.dialog_download;
}
private void init() {
if (Aria.get(getContext()).taskExists(DOWNLOAD_URL)) {
AMTarget target = Aria.whit(getContext()).load(DOWNLOAD_URL);
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);
}
Aria.whit(this).addSchedulerListener(new MyDialogDownloadCallback());
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);
}
}
@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())
Aria.whit(this)
.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();
Aria.whit(this).load(DOWNLOAD_URL).stop();
break;
case R.id.cancel:
Aria.whit(getContext()).load(DOWNLOAD_URL).cancel();
Aria.whit(this).load(DOWNLOAD_URL).cancel();
break;
}
}
@ -72,7 +78,33 @@ public class DownloadDialog extends AbsDialog {
}
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();

@ -0,0 +1,124 @@
package com.arialyy.simple.pop;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ClipDrawable;
import android.graphics.drawable.ColorDrawable;
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.AbsDialog;
import com.arialyy.frame.core.AbsPopupWindow;
import com.arialyy.simple.R;
import com.arialyy.simple.widget.HorizontalProgressBarWithNumber;
/**
* Created by AriaL on 2017/1/2.
*/
public class DownloadPopupWindow extends AbsPopupWindow {
@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 DownloadPopupWindow(Context context) {
super(context, new ColorDrawable(Color.WHITE));
initWidget();
}
@Override protected int setLayoutId() {
return R.layout.dialog_download;
}
private void initWidget() {
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);
}
Aria.whit(this).addSchedulerListener(new MyDialogDownloadCallback());
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);
}
}
@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 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");
}
}
}

@ -38,5 +38,14 @@
style="?buttonBarButtonStyle"
/>
<Button
android:id="@+id/pop_task"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="在popupwindow中使用"
style="?buttonBarButtonStyle"
/>
</LinearLayout>
</layout>

@ -0,0 +1,79 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<com.arialyy.simple.widget.HorizontalProgressBarWithNumber
android:id="@+id/progressBar"
android:layout_width="wrap_content"
android:layout_height="20dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_margin="16dp"
android:layout_toLeftOf="@+id/size"
android:max="100"
style="?android:attr/progressBarStyleHorizontal"
/>
<TextView
android:id="@+id/size"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignTop="@+id/progressBar"
android:layout_marginRight="16dp"
android:text="0mb"
android:textSize="16sp"
/>
<LinearLayout
android:id="@+id/handle_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/progressBar"
android:orientation="horizontal"
>
<TextView
android:id="@+id/speed"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:text="0kb/s"
android:textColor="@color/black"
/>
<Button
android:id="@+id/start"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onClick"
android:text="开始"
style="?buttonBarButtonStyle"
/>
<Button
android:id="@+id/stop"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onClick"
android:text="暂停"
style="?buttonBarButtonStyle"
/>
<Button
android:id="@+id/cancel"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onClick"
android:text="删除"
style="?buttonBarButtonStyle"
/>
</LinearLayout>
</RelativeLayout>

Binary file not shown.

After

Width:  |  Height:  |  Size: 301 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 231 KiB

Loading…
Cancel
Save