From 82e8aee382b5159dc9804f07cf9014a280cc9753 Mon Sep 17 00:00:00 2001 From: AriaLyy <511455842@qq.com> Date: Wed, 21 Dec 2016 23:35:01 +0800 Subject: [PATCH] =?UTF-8?q?dialog=20=E7=94=9F=E5=91=BD=E5=91=A8=E6=9C=9F?= =?UTF-8?q?=E6=B3=A8=E5=86=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/com/arialyy/aria/core/Aria.java | 38 +++++++++++++- .../com/arialyy/aria/core/AriaManager.java | 50 ++++++++++++++++--- 2 files changed, 81 insertions(+), 7 deletions(-) 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 bfb884f9..2d2b3aaf 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/Aria.java +++ b/Aria/src/main/java/com/arialyy/aria/core/Aria.java @@ -19,6 +19,8 @@ package com.arialyy.aria.core; import android.annotation.TargetApi; import android.app.Activity; import android.app.Application; +import android.app.Dialog; +import android.app.DialogFragment; import android.app.Fragment; import android.app.Service; import android.content.Context; @@ -28,14 +30,29 @@ import android.os.Build; * Created by lyy on 2016/12/1. * https://github.com/AriaLyy/Aria * Aria启动,管理全局任务 + *
+ *   
+ *      DownloadEntity mEntity = new DownloadEntity();
+ *      mEntity.setFileName(fileName);          //设置文件名
+ *      mEntity.setDownloadUrl(downloadUrl);    //设置下载链接
+ *      mEntity.setDownloadPath(downloadPath);  //设置存放路径
+ *
+ *      //启动下载
+ *     Aria.whit(this).load(mEntity).start();
+ *   
+ * 
*/ @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1) public class Aria { private Aria() { } + /** + * 接受Activity、Service、Application + */ public static AMReceiver whit(Context context) { - if (context == null) throw new IllegalArgumentException("context 不能为 null"); + //if (context == null) throw new IllegalArgumentException("context 不能为 null"); + checkNull(context); if (context instanceof Activity || context instanceof Service || context instanceof Application) { @@ -45,12 +62,31 @@ import android.os.Build; } } + /** + * 处理Fragment、或者DialogFragment + */ 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); } + /** + * 处理Dialog + */ + public static AMReceiver whit(Dialog dialog) { + checkNull(dialog); + return AriaManager.getInstance(dialog.getContext()).get(dialog); + } + + private static void checkNull(Object obj) { + if (obj == null) throw new IllegalArgumentException("不能传入空对象"); + } + + /** + * 处理通用事件 + */ public static AriaManager get(Context context) { if (context == null) throw new IllegalArgumentException("context 不能为 null"); if (context instanceof Activity 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 8ea0ded5..8310d4b0 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/AriaManager.java +++ b/Aria/src/main/java/com/arialyy/aria/core/AriaManager.java @@ -18,10 +18,13 @@ package com.arialyy.aria.core; import android.annotation.TargetApi; import android.app.Activity; import android.app.Application; +import android.app.Dialog; import android.app.Service; import android.content.Context; +import android.content.DialogInterface; import android.os.Build; import android.os.Bundle; +import android.os.Message; import android.support.v4.app.Fragment; import android.text.TextUtils; import android.util.Log; @@ -29,6 +32,7 @@ import com.arialyy.aria.core.command.CmdFactory; import com.arialyy.aria.util.CommonUtil; import com.arialyy.aria.core.command.IDownloadCmd; import com.arialyy.aria.util.Configuration; +import java.lang.reflect.Field; import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -69,7 +73,7 @@ import java.util.Set; /** * 获取下载列表 */ - public List getDownloadList(){ + public List getDownloadList() { return DownloadEntity.findAllData(DownloadEntity.class); } @@ -101,8 +105,7 @@ import java.util.Set; /** * 设置下载超时时间 */ - @Deprecated - private AriaManager setTimeOut(int timeOut) { + @Deprecated private AriaManager setTimeOut(int timeOut) { Configuration.getInstance().setTimeOut(timeOut); return this; } @@ -169,16 +172,51 @@ import java.util.Set; if (target == null) { target = new AMReceiver(); target.obj = obj; + String key = ""; if (obj instanceof android.support.v4.app.Fragment) { - clsName += "_" + ((Fragment) obj).getActivity().getClass().getName(); + key = "_" + ((Fragment) obj).getActivity().getClass().getName(); } else if (obj instanceof android.app.Fragment) { - clsName += "_" + ((android.app.Fragment) obj).getActivity().getClass().getName(); + key = "_" + ((android.app.Fragment) obj).getActivity().getClass().getName(); + } else if (obj instanceof Dialog) { + Activity activity = ((Dialog) obj).getOwnerActivity(); + if (activity != null) { + key = "_" + activity.getClass().getName(); + } + handleDialogDialogLift((Dialog) obj); + } + + if (!TextUtils.isEmpty(key)) { + mTargets.put(clsName, target); } - mTargets.put(clsName, target); } return target; } + /** + * 处理对话框取消或dismiss + */ + private void handleDialogDialogLift(Dialog dialog) { + try { + 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) dismissField.get(dialog); + if (cancelMsg != null) { + Log.e(TAG, "你已经对Dialog设置了Dismiss和cancel事件。为了防止内存泄露," + + "请在dismiss方法中调用Aria.whit(this).removeSchedulerListener();来注销事件"); + } else { + + } + } else { + + } + } catch (IllegalAccessException e) { + e.printStackTrace(); + } + } + private AMReceiver getTarget(Object obj) { AMReceiver target = mTargets.get(obj.getClass().getName()); if (target == null) {