dialog 生命周期注册

pull/2/head
AriaLyy 8 years ago
parent 9c26068ad7
commit 82e8aee382
  1. 38
      Aria/src/main/java/com/arialyy/aria/core/Aria.java
  2. 50
      Aria/src/main/java/com/arialyy/aria/core/AriaManager.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启动管理全局任务
* <pre>
* <code>
* DownloadEntity mEntity = new DownloadEntity();
* mEntity.setFileName(fileName); //设置文件名
* mEntity.setDownloadUrl(downloadUrl); //设置下载链接
* mEntity.setDownloadPath(downloadPath); //设置存放路径
*
* //启动下载
* Aria.whit(this).load(mEntity).start();
* </code>
* </pre>
*/
@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1) public class Aria {
private Aria() {
}
/**
* 接受ActivityServiceApplication
*/
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

@ -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<DownloadEntity> getDownloadList(){
public List<DownloadEntity> 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) {

Loading…
Cancel
Save