diff --git a/Aria/build.gradle b/Aria/build.gradle index b101e38d..be8d2a90 100644 --- a/Aria/build.gradle +++ b/Aria/build.gradle @@ -7,8 +7,8 @@ android { defaultConfig { minSdkVersion 9 targetSdkVersion 23 - versionCode 82 - versionName "2.3.4" + versionCode 84 + versionName "2.3.6" } 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 e3a2cbc4..167d7cc4 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/Aria.java +++ b/Aria/src/main/java/com/arialyy/aria/core/Aria.java @@ -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 { diff --git a/Aria/src/main/java/com/arialyy/aria/core/AriaManager.java b/Aria/src/main/java/com/arialyy/aria/core/AriaManager.java index 77edd65f..b02cdceb 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/AriaManager.java +++ b/Aria/src/main/java/com/arialyy/aria/core/AriaManager.java @@ -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; @@ -49,12 +48,12 @@ import java.util.Set; * Aria管理器,任务操作在这里执行 */ @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH) public class AriaManager { - private static final String TAG = "AriaManager"; - private static final Object LOCK = new Object(); - private static volatile AriaManager INSTANCE = null; - private Map mTargets = new HashMap<>(); + private static final String TAG = "AriaManager"; + private static final Object LOCK = new Object(); + private static volatile AriaManager INSTANCE = null; + private Map mTargets = new HashMap<>(); private DownloadManager mManager; - private LifeCallback mLifeCallback; + private LifeCallback mLifeCallback; private AriaManager(Context context) { regAppLifeCallback(context); @@ -101,7 +100,7 @@ import java.util.Set; */ public void stopAllTask() { List allEntity = mManager.getAllDownloadEntity(); - List stopCmds = new ArrayList<>(); + List stopCmds = new ArrayList<>(); for (DownloadEntity entity : allEntity) { if (entity.getState() == DownloadEntity.STATE_DOWNLOAD_ING) { stopCmds.add(CommonUtil.createCmd(entity, CmdFactory.TASK_STOP)); @@ -160,8 +159,8 @@ import java.util.Set; * 删除所有任务 */ public void cancelAllTask() { - List allEntity = mManager.getAllDownloadEntity(); - List cancelCmds = new ArrayList<>(); + List allEntity = mManager.getAllDownloadEntity(); + List cancelCmds = new ArrayList<>(); for (DownloadEntity entity : allEntity) { cancelCmds.add(CommonUtil.createCmd(entity, CmdFactory.TASK_CANCEL)); } @@ -175,9 +174,9 @@ import java.util.Set; } private AMReceiver putTarget(Object obj) { - String clsName = obj.getClass().getName(); - AMReceiver target = null; - String key = ""; + String clsName = obj.getClass().getName(); + AMReceiver target = null; + String key = ""; if (!(obj instanceof Activity)) { if (obj instanceof android.support.v4.app.Fragment) { key = clsName + "_" + ((Fragment) obj).getActivity().getClass().getName(); @@ -187,44 +186,83 @@ import java.util.Set; Activity activity = ((Dialog) obj).getOwnerActivity(); if (activity != null) { key = clsName + "_" + activity.getClass().getName(); + } else { + key = clsName; + } + 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; } - handleDialogDialogLift((Dialog) obj); + 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); - } + } + 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); + Field dismissField = CommonUtil.getField(dialog.getClass(), "mDismissMessage"); + Message dismissMsg = (Message) dismissField.get(dialog); //如果Dialog已经设置Dismiss事件,则查找cancel事件 if (dismissMsg != null) { - Field cancelField = CommonUtil.getField(dialog.getClass(), "mCancelMessage"); - Message cancelMsg = (Message) cancelField.get(dialog); + Field cancelField = CommonUtil.getField(dialog.getClass(), "mCancelMessage"); + Message cancelMsg = (Message) cancelField.get(dialog); if (cancelMsg != null) { 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,68 +289,50 @@ 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; } /** * onDestroy */ private void destroySchedulerListener(Object obj) { - Set keys = mTargets.keySet(); - String clsName = obj.getClass().getName(); + Set keys = mTargets.keySet(); + String clsName = obj.getClass().getName(); for (Iterator> iter = mTargets.entrySet().iterator(); iter.hasNext(); ) { Map.Entry entry = iter.next(); - String key = entry.getKey(); + String key = entry.getKey(); 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(); - iter.remove(); } + 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; - // } - //} } /** diff --git a/Aria/src/main/java/com/arialyy/aria/core/scheduler/DownloadSchedulers.java b/Aria/src/main/java/com/arialyy/aria/core/scheduler/DownloadSchedulers.java index afaed7a7..5f5b8a4e 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/scheduler/DownloadSchedulers.java +++ b/Aria/src/main/java/com/arialyy/aria/core/scheduler/DownloadSchedulers.java @@ -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); } diff --git a/README.md b/README.md index 2ba6230a..36052019 100644 --- a/README.md +++ b/README.md @@ -3,6 +3,7 @@ Aria,致力于让下载傻瓜化
+ Aria有以下特点: - 简单 + - 可在Dialog、popupWindow等组件中使用 - 可自定义是否使用广播 - 支持多线程、多任务下载 - 支持任务自动切换 @@ -15,14 +16,14 @@ Aria,致力于让下载傻瓜化
## 下载 [![Download](https://api.bintray.com/packages/arialyy/maven/Aria/images/download.svg)](https://bintray.com/arialyy/maven/Aria/_latestVersion)
```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 - 添加断点支持 - 修改下载逻辑,让使用更加简单 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 b3c957d9..8959c5a7 100644 --- a/app/src/main/java/com/arialyy/simple/activity/MainActivity.java +++ b/app/src/main/java/com/arialyy/simple/activity/MainActivity.java @@ -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 { @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 { 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 { 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; } } } \ 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 index bb087dc8..f8e723fd 100644 --- a/app/src/main/java/com/arialyy/simple/dialog/DownloadDialog.java +++ b/app/src/main/java/com/arialyy/simple/dialog/DownloadDialog.java @@ -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(); diff --git a/app/src/main/java/com/arialyy/simple/pop/DownloadPopupWindow.java b/app/src/main/java/com/arialyy/simple/pop/DownloadPopupWindow.java new file mode 100644 index 00000000..20692903 --- /dev/null +++ b/app/src/main/java/com/arialyy/simple/pop/DownloadPopupWindow.java @@ -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"); + } + } +} diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index aeecab69..87847e7a 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -38,5 +38,14 @@ style="?buttonBarButtonStyle" /> +