Skip to content

Commit

Permalink
将传入 FragmentActivity 修改为 Activity(以便更好地支持跨平台)
Browse files Browse the repository at this point in the history
  • Loading branch information
getActivity committed Jun 7, 2021
1 parent 1c4cf78 commit 00f7fc1
Show file tree
Hide file tree
Showing 13 changed files with 98 additions and 49 deletions.
8 changes: 4 additions & 4 deletions HelpDoc.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ public class XxxApplication extends Application {
public class PermissionInterceptor implements IPermissionInterceptor {

@Override
public void requestPermissions(FragmentActivity activity, OnPermissionCallback callback, List<String> permissions) {
public void requestPermissions(Activity activity, OnPermissionCallback callback, List<String> permissions) {
new AlertDialog.Builder(activity)
.setTitle("授权提示")
.setMessage("使用此功能需要先授予权限")
Expand All @@ -157,13 +157,13 @@ public class PermissionInterceptor implements IPermissionInterceptor {
}

@Override
public void grantedPermissions(FragmentActivity activity, OnPermissionCallback callback, List<String> permissions, boolean all) {
public void grantedPermissions(Activity activity, OnPermissionCallback callback, List<String> permissions, boolean all) {
// 回调授权成功的方法
callback.onGranted(permissions, all);
}

@Override
public void deniedPermissions(FragmentActivity activity, OnPermissionCallback callback, List<String> permissions, boolean never) {
public void deniedPermissions(Activity activity, OnPermissionCallback callback, List<String> permissions, boolean never) {
// 回调授权失败的方法
callback.onDenied(permissions, never);
if (never) {
Expand All @@ -182,7 +182,7 @@ public class PermissionInterceptor implements IPermissionInterceptor {
/**
* 显示授权对话框
*/
protected void showPermissionDialog(FragmentActivity activity, List<String> permissions) {
protected void showPermissionDialog(Activity activity, List<String> permissions) {
new AlertDialog.Builder(activity)
.setTitle("授权提醒")
.setMessage(getPermissionHint(activity, permissions))
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ android {
dependencies {
// 权限请求框架:https://github.com/getActivity/XXPermissions
implementation 'com.github.getActivity:XXPermissions:11.2'
implementation 'com.github.getActivity:XXPermissions:11.5'
}
```

Expand Down Expand Up @@ -178,7 +178,7 @@ public class XxxActivity extends AppCompatActivity {

* 在框架的日常维护中,有很多人跟我反馈过框架有 Bug,但是经过排查和定位发现,这其中有 95% 的问题来自于调用者一些不规范操作导致的,这不仅对我造成很大的困扰,同时也极大浪费了很多小伙伴的时间和精力,于是我在框架中加入了很多审查元素,在 **Debug** 模式下,一旦有某些操作不符合规范,那么框架会直接抛出异常给调用者,并在异常信息中正确指引调用者纠正错误,例如:

1. 传入的 Activity 对象不是 FragmentActivity 的子类,框架会抛出异常,又或者传入的 Activity 的状态异常(已经 **Finishing** 或者 **Destroyed**),这种情况一般是在异步申请权限导致的,框架也会抛出异常,请在合适的时机申请权限,如果申请的时机无法预估,请在外层做好 Activity 状态判断再进行权限申请。
1. 传入的 Context 实例不是 Activity 对象,框架会抛出异常,又或者传入的 Activity 的状态异常(已经 **Finishing** 或者 **Destroyed**),这种情况一般是在异步申请权限导致的,框架也会抛出异常,请在合适的时机申请权限,如果申请的时机无法预估,请在外层做好 Activity 状态判断再进行权限申请。

2. 如果调用者没有传入任何权限就申请权限的话,框架会抛出异常,如果调用者传入的权限不是危险权限或者特殊权限,框架也会抛出异常,因为有的人会把普通权限当做危险权限传给框架,系统会直接拒绝。

Expand Down
Binary file modified XXPermissions.apk
Binary file not shown.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ android {
applicationId "com.hjq.permissions.demo"
minSdkVersion 14
targetSdkVersion 30
versionCode 1120
versionName "11.2"
versionCode 1150
versionName "11.5"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}

Expand Down
4 changes: 2 additions & 2 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@

<uses-permission android:name="android.permission.WRITE_SETTINGS" tools:ignore="ProtectedPermissions" />

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" tools:node="replace" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" tools:node="replace" />
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />

<uses-permission android:name="android.permission.RECORD_AUDIO" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ public void onGranted(List<String> permissions, boolean all) {
XXPermissions.with(this)
.permission(Permission.ACCESS_COARSE_LOCATION)
.permission(Permission.ACCESS_FINE_LOCATION)
// 如果不需要在后台使用定位功能,请不要申请此权限
.permission(Permission.ACCESS_BACKGROUND_LOCATION)
.request(new OnPermissionCallback() {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.hjq.permissions.demo;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Build;
import android.support.v4.app.FragmentActivity;
import android.support.v7.app.AlertDialog;

import com.hjq.permissions.IPermissionInterceptor;
Expand All @@ -24,7 +24,7 @@
public final class PermissionInterceptor implements IPermissionInterceptor {

// @Override
// public void requestPermissions(FragmentActivity activity, OnPermissionCallback callback, List<String> permissions) {
// public void requestPermissions(Activity activity, OnPermissionCallback callback, List<String> permissions) {
// // 这里的 Dialog 只是示例,没有用 DialogFragment 来处理 Dialog 生命周期
// new AlertDialog.Builder(activity)
// .setTitle(R.string.common_permission_hint)
Expand All @@ -48,13 +48,13 @@ public final class PermissionInterceptor implements IPermissionInterceptor {
// }

@Override
public void grantedPermissions(FragmentActivity activity, OnPermissionCallback callback, List<String> permissions, boolean all) {
public void grantedPermissions(Activity activity, OnPermissionCallback callback, List<String> permissions, boolean all) {
// 回调授权失败的方法
callback.onGranted(permissions, all);
}

@Override
public void deniedPermissions(FragmentActivity activity, OnPermissionCallback callback, List<String> permissions, boolean never) {
public void deniedPermissions(Activity activity, OnPermissionCallback callback, List<String> permissions, boolean never) {
// 回调授权失败的方法
callback.onDenied(permissions, never);
if (never) {
Expand All @@ -73,7 +73,7 @@ public void deniedPermissions(FragmentActivity activity, OnPermissionCallback ca
/**
* 显示授权对话框
*/
protected void showPermissionDialog(FragmentActivity activity, List<String> permissions) {
protected void showPermissionDialog(Activity activity, List<String> permissions) {
// 这里的 Dialog 只是示例,没有用 DialogFragment 来处理 Dialog 生命周期
new AlertDialog.Builder(activity)
.setTitle(R.string.common_permission_alert)
Expand Down
4 changes: 2 additions & 2 deletions library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ android {

defaultConfig {
minSdkVersion 11
versionCode 1120
versionName "11.2"
versionCode 1150
versionName "11.5"
}

// 使用 JDK 1.8
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.hjq.permissions;

import android.support.v4.app.FragmentActivity;
import android.app.Activity;

import java.util.ArrayList;
import java.util.List;
Expand All @@ -16,21 +16,21 @@ public interface IPermissionInterceptor {
/**
* 权限申请拦截,可在此处先弹 Dialog 再申请权限
*/
default void requestPermissions(FragmentActivity activity, OnPermissionCallback callback, List<String> permissions) {
default void requestPermissions(Activity activity, OnPermissionCallback callback, List<String> permissions) {
PermissionFragment.beginRequest(activity, new ArrayList<>(permissions), callback);
}

/**
* 权限授予回调拦截,参见 {@link OnPermissionCallback#onGranted(List, boolean)}
*/
default void grantedPermissions(FragmentActivity activity, OnPermissionCallback callback, List<String> permissions, boolean all) {
default void grantedPermissions(Activity activity, OnPermissionCallback callback, List<String> permissions, boolean all) {
callback.onGranted(permissions, all);
}

/**
* 权限拒绝回调拦截,参见 {@link OnPermissionCallback#onDenied(List, boolean)}
*/
default void deniedPermissions(FragmentActivity activity, OnPermissionCallback callback, List<String> permissions, boolean never) {
default void deniedPermissions(Activity activity, OnPermissionCallback callback, List<String> permissions, boolean never) {
callback.onDenied(permissions, never);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ static boolean checkActivityStatus(Activity activity, boolean debugMode) {
// 检查当前 Activity 状态是否是正常的,如果不是则不请求权限
if (activity == null) {
if (debugMode) {
// 这个 Activity 对象必须是 FragmentActivity 的子类,请直接继承 AppCompatActivity
throw new IllegalArgumentException("The Activity must be a subclass of FragmentActivity, Please directly inherit AppCompatActivity");
// Context 的实例必须是 Activity 对象
throw new IllegalArgumentException("The instance of the Context must be an Activity object");
}
return false;
}
Expand Down Expand Up @@ -104,7 +104,6 @@ static boolean checkPermissionArgument(List<String> requestPermissions, boolean
* @param requestPermissions 请求的权限组
* @param scopedStorage 是否适配了分区存储
*/
@SuppressWarnings({"ConstantConditions"})
static void checkStoragePermission(Context context, List<String> requestPermissions, boolean scopedStorage) {
// 如果请求的权限中没有包含外部存储相关的权限,那么就直接返回
if (!requestPermissions.contains(Permission.MANAGE_EXTERNAL_STORAGE) &&
Expand Down
32 changes: 18 additions & 14 deletions library/src/main/java/com/hjq/permissions/PermissionFragment.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
package com.hjq.permissions;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Activity;
import android.app.Fragment;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.content.res.Configuration;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.util.SparseBooleanArray;

import java.util.ArrayList;
Expand All @@ -21,6 +23,8 @@
* time : 2018/06/15
* desc : 权限请求处理类
*/
@SuppressWarnings("deprecation")
@TargetApi(Build.VERSION_CODES.M)
public final class PermissionFragment extends Fragment implements Runnable {

/** 请求的权限组 */
Expand All @@ -35,14 +39,14 @@ public final class PermissionFragment extends Fragment implements Runnable {
/** 权限请求码存放集合 */
private final static SparseBooleanArray REQUEST_CODE_ARRAY = new SparseBooleanArray();

public static void beginRequest(FragmentActivity activity, ArrayList<String> permissions, OnPermissionCallback callback) {
public static void beginRequest(Activity activity, ArrayList<String> permissions, OnPermissionCallback callback) {
beginRequest(activity, permissions, true, callback);
}

/**
* 开启权限申请
*/
private static void beginRequest(FragmentActivity activity, ArrayList<String> permissions, boolean interceptor, OnPermissionCallback callback) {
private static void beginRequest(Activity activity, ArrayList<String> permissions, boolean interceptor, OnPermissionCallback callback) {
PermissionFragment fragment = new PermissionFragment();
Bundle bundle = new Bundle();
int requestCode;
Expand Down Expand Up @@ -79,15 +83,15 @@ private static void beginRequest(FragmentActivity activity, ArrayList<String> pe
/**
* 绑定 Activity
*/
public void attachActivity(FragmentActivity activity) {
activity.getSupportFragmentManager().beginTransaction().add(this, this.toString()).commitAllowingStateLoss();
public void attachActivity(Activity activity) {
activity.getFragmentManager().beginTransaction().add(this, this.toString()).commitAllowingStateLoss();
}

/**
* 解绑 Activity
*/
public void detachActivity(FragmentActivity activity) {
activity.getSupportFragmentManager().beginTransaction().remove(this).commitAllowingStateLoss();
public void detachActivity(Activity activity) {
activity.getFragmentManager().beginTransaction().remove(this).commitAllowingStateLoss();
}

/**
Expand All @@ -101,7 +105,7 @@ public void setCallBack(OnPermissionCallback callback) {
@Override
public void onAttach(Context context) {
super.onAttach(context);
FragmentActivity activity = getActivity();
Activity activity = getActivity();
if (activity == null) {
return;
}
Expand All @@ -128,7 +132,7 @@ public void onAttach(Context context) {
@Override
public void onDetach() {
super.onDetach();
FragmentActivity activity = getActivity();
Activity activity = getActivity();
if (activity == null || mScreenOrientation != ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED) {
return;
}
Expand Down Expand Up @@ -161,7 +165,7 @@ public void onResume() {
*/
public void requestSpecialPermission() {
Bundle arguments = getArguments();
FragmentActivity activity = getActivity();
Activity activity = getActivity();
if (arguments == null || activity == null) {
return;
}
Expand Down Expand Up @@ -218,7 +222,7 @@ public void requestSpecialPermission() {
* 申请危险权限
*/
public void requestDangerousPermission() {
FragmentActivity activity = getActivity();
Activity activity = getActivity();
Bundle arguments = getArguments();
if (activity == null || arguments == null) {
return;
Expand Down Expand Up @@ -309,7 +313,7 @@ public void onDenied(List<String> permissions, boolean never) {
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
Bundle arguments = getArguments();
FragmentActivity activity = getActivity();
Activity activity = getActivity();
if (activity == null || arguments == null || mCallBack == null || requestCode != arguments.getInt(REQUEST_CODE)) {
return;
}
Expand Down Expand Up @@ -397,7 +401,7 @@ public void onRequestPermissionsResult(int requestCode, String[] permissions, in

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
FragmentActivity activity = getActivity();
Activity activity = getActivity();
Bundle arguments = getArguments();
if (activity == null || arguments == null || requestCode != arguments.getInt(REQUEST_CODE) || mDangerousRequest) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import android.os.Build;
import android.os.Environment;
import android.provider.Settings;
import android.support.v4.app.FragmentActivity;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
Expand Down Expand Up @@ -464,10 +463,10 @@ static int getRandomRequestCode() {
/**
* 寻找上下文中的 Activity 对象
*/
static FragmentActivity findFragmentActivity(Context context) {
static Activity findActivity(Context context) {
do {
if (context instanceof FragmentActivity) {
return (FragmentActivity) context;
if (context instanceof Activity) {
return (Activity) context;
} else if (context instanceof ContextWrapper){
context = ((ContextWrapper) context).getBaseContext();
} else {
Expand Down
Loading

0 comments on commit 00f7fc1

Please sign in to comment.