Skip to content

Commit

Permalink
适配Android Q仅使用期间允许功能
Browse files Browse the repository at this point in the history
  • Loading branch information
zcxshare committed Dec 23, 2020
1 parent 21b35a3 commit acb2a6a
Show file tree
Hide file tree
Showing 13 changed files with 158 additions and 63 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ NeedPermission支持在所有的方法中使用和activity的类上使用,提供
1. **@PermissionBefore**:请求权限前的操作,可以在请求权限的本类中和配置类中使用,被注解的方法的参数只能是PermissionBeforeBean
,与NeedPermission联合使用,通过requestBefore匹配
1. **@PermissionCanceled**:权限被取消(用户点击禁止权限)时调用的方法,可以在请求权限的本类中和配置类中使用
,被注解的方法的参数只能是PermissionCanceledBean,与Neddpermission联合使用,通过permissionCanceled匹配
,被注解的方法的参数只能是PermissionCanceledBean,与NeedPermission联合使用,通过permissionCanceled匹配
1. **@PermissionDenied**:权限被取消(用户勾选禁止后不再提示并点击禁止权限)时调用的方法,可以在请求权限的本类中和配置类中使用
,被注解的方法的参数只能是PermissionDeniedBean,与Neddpermission联合使用,通过permissionDenied匹配
,被注解的方法的参数只能是PermissionDeniedBean,与NeedPermission联合使用,通过permissionDenied匹配

### 使用方式
在你的Application的onCreate方法中使用FastPermission.getInstance().init()初始化,然后在需要权限的方法
Expand Down
2 changes: 1 addition & 1 deletion fast_permission_runtime/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ android {
versionCode 1
versionName "1.0"

testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner'

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,10 @@ public void onPermissionGranted() {

@Override
public void onPermissionCanceled(PermissionCanceledBean bean) {
bindInfo(bean,context);
if (object instanceof PermissionListener) {
((PermissionListener) object).onPermissionCanceled(bean);
return;
}
String canceledKey = needPermission.permissionCanceled();
boolean isExecuteCanceled;
Expand All @@ -116,8 +118,10 @@ public void onPermissionCanceled(PermissionCanceledBean bean) {

@Override
public void onPermissionDenied(PermissionDeniedBean bean) {
bindInfo(bean,context);
if (object instanceof PermissionListener) {
((PermissionListener) object).onPermissionDenied(bean);
return;
}

String deniedKey = needPermission.permissionDenied();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,14 @@
import com.zcx.fast_permission_runtime.bean.PermissionCanceledBean;
import com.zcx.fast_permission_runtime.bean.PermissionDeniedBean;
import com.zcx.fast_permission_runtime.exception.FastPermissionException;
import com.zcx.fast_permission_runtime.util.PermissionUtils;

import org.aspectj.lang.ProceedingJoinPoint;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.List;

/**
* author: zhouchaoxiang
Expand Down Expand Up @@ -44,10 +47,12 @@ protected boolean executeBefore(Context context, Object object, Method[] methods
PermissionBefore annotation = method.getAnnotation(PermissionBefore.class);
if (annotation != null) {
String value = annotation.value();
List<String> notPermissions = PermissionUtils.getNotPermissions(context, needPermission.value());
PermissionBeforeBean beforeBean = new PermissionBeforeBean(null, null, null, notPermissions);
if (TextUtils.isEmpty(beforeKey) && TextUtils.isEmpty(value)) {
if (executeBeforeMethod(context, object, method, needPermission)) return true;
if (executeBeforeMethod(beforeBean, context, object, method)) return true;
} else if (!TextUtils.isEmpty(beforeKey) && beforeKey.equals(value)) {
if (executeBeforeMethod(context, object, method, needPermission)) return true;
if (executeBeforeMethod(beforeBean, context, object, method)) return true;
}
}
}
Expand Down Expand Up @@ -84,26 +89,28 @@ protected boolean executeDenied(Context context, Object object, Method[] methods
return false;
}

protected boolean executeBeforeMethod(Context context, Object object, Method method, NeedPermission needPermission) {
PermissionBeforeBean beforeBean = new PermissionBeforeBean(context, this, needPermission);
return executeMethod(beforeBean, object, method,
protected boolean executeBeforeMethod(PermissionBeforeBean bean, Context context, Object object, Method method) {
bindInfo(bean, context);
return executeMethod(bean, object, method,
"The method parameters decorated by the PermissionBefore annotation can only be the PermissionBeforeBean");
}

protected boolean executeCanceledMethod(PermissionCanceledBean bean, Context context, Object object, Method method) {
bean.setContext(context);
bean.setAspect(this);
bean.setNeedPermission(mNeedPermission);
bindInfo(bean, context);
return executeMethod(bean, object, method,
"The method parameters decorated by the PermissionCanceled annotation can only be the PermissionCanceledBean");
}

protected boolean executeDeniedMethod(PermissionDeniedBean bean, Context context, Object object, Method method) {
bindInfo(bean, context);
return executeMethod(bean, object, method,
"The method parameters decorated by the PermissionDenied annotation can only be the PermissionDeniedBean");
}

protected void bindInfo(PermissionBaseBean bean, Context context) {
bean.setContext(context);
bean.setAspect(this);
bean.setNeedPermission(mNeedPermission);
return executeMethod(bean, object, method,
"The method parameters decorated by the PermissionDenied annotation can only be the PermissionDeniedBean");
}

protected <B extends PermissionBaseBean> boolean executeMethod(B bean, Object object, Method method, String exceptionPrompt) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,25 @@
import com.zcx.fast_permission_runtime.annotation.NeedPermission;
import com.zcx.fast_permission_runtime.aspect.PermissionBaseAspect;

import java.util.List;

/**
* author: zhouchaoxiang
* date: 2019/9/29
* explain:
*/
public class PermissionBeforeBean extends PermissionBaseBean {

public PermissionBeforeBean(Context context, PermissionBaseAspect aspect, NeedPermission needPermission) {
private List<String> mNotPermissions;
public PermissionBeforeBean(Context context, PermissionBaseAspect aspect, NeedPermission needPermission,List<String> notPermissions) {
super(context, aspect, needPermission);
mNotPermissions =notPermissions;
}

public void proceed(boolean isRequest) {
getAspect().proceed(isRequest);
}

public List<String> getNotPermissions() {
return mNotPermissions;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
import com.zcx.fast_permission_runtime.exception.FastPermissionException;
import com.zcx.fast_permission_runtime.listener.PermissionListener;

import java.util.ArrayList;
import java.util.List;


/**
* author: zhouchaoxiang
Expand Down Expand Up @@ -65,4 +68,17 @@ public static void requestPermissions(Context context,String[] permissions, int
PermissionRequestActivity.start(context, permissions, requestCode, listener);
}

public static List<String> getNotPermissions(Context context, String... permissions){
List<String> notPermissions = new ArrayList<>();
if (permissions == null || permissions.length == 0) {
throw new FastPermissionException("The permission requested is empty");
}
for (String permission : permissions) {
if (!checkPermission(context, permission)) {
notPermissions.add(permission);
}
}
return notPermissions;
}

}
2 changes: 2 additions & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

# Specifies the JVM arguments used for the daemon process.
# The setting is particularly useful for tweaking memory settings.
android.enableJetifier=true
android.useAndroidX=true
org.gradle.jvmargs=-Xmx1536m

# When configured, Gradle will run in incubating parallel mode.
Expand Down
16 changes: 8 additions & 8 deletions sample/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ apply plugin: 'com.android.application'
apply plugin: 'android-aspectjx'

android {
compileSdkVersion 28
compileSdkVersion 29
defaultConfig {
applicationId "com.zcx.fastpermission"
minSdkVersion 21
targetSdkVersion 28
targetSdkVersion 29
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner'
}
buildTypes {
release {
Expand All @@ -33,12 +33,12 @@ android {

dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
implementation 'com.android.support:design:28.0.0'
implementation 'androidx.appcompat:appcompat:1.0.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'com.google.android.material:material:1.0.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0'
implementation project(':fast_permission_runtime')

}
4 changes: 4 additions & 0 deletions sample/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION "/>
<!--允许App在后台获得位置信息-->
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION"/>

<application
android:name=".App"
Expand Down
79 changes: 61 additions & 18 deletions sample/src/main/java/com/zcx/fastpermission/CameraActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,30 @@
import android.widget.Button;
import android.widget.FrameLayout;

import androidx.annotation.NonNull;
import androidx.core.app.ActivityCompat;

import com.zcx.fast_permission_runtime.annotation.NeedPermission;
import com.zcx.fast_permission_runtime.annotation.PermissionBefore;
import com.zcx.fast_permission_runtime.bean.PermissionBeforeBean;
import com.zcx.fast_permission_runtime.bean.PermissionCanceledBean;
import com.zcx.fast_permission_runtime.bean.PermissionDeniedBean;
import com.zcx.fast_permission_runtime.listener.PermissionListener;

import java.util.Arrays;
import java.util.List;

/**
* A login screen that offers login via email/password.
*/
@NeedPermission(value = {Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE},
@NeedPermission(value = {Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.ACCESS_COARSE_LOCATION,Manifest.permission.ACCESS_BACKGROUND_LOCATION},
requestBefore = Manifest.permission.CAMERA, permissionCanceled = Manifest.permission.CAMERA,
permissionDenied = Manifest.permission.CAMERA, isAllowExecution = true)
public class CameraActivity extends Activity implements PermissionListener {
private static final String TAG = "CameraActivity";

private static final int CAMERA_ID = 0;
private static final int requestCode = 3;

private CameraPreview preview;
private Camera camera;
Expand Down Expand Up @@ -68,31 +75,61 @@ public void onPermissionGranted() {

@Override
public void onPermissionCanceled(PermissionCanceledBean bean) {

List<String> cancelList = bean.getCancelList();
if (cancelList.size() == 1 && Manifest.permission.ACCESS_BACKGROUND_LOCATION.equals(cancelList.get(0))) {
bean.proceed();
mHasPermission = true;
initCamera();
} else {
new androidx.appcompat.app.AlertDialog.Builder(bean.getContext())
.setTitle("来自配置文件,我们需要相机权限,请同意")
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
bean.againRequest();
}
})
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
bean.proceed();
}
})
.show();
}
}

@Override
public void onPermissionDenied(PermissionDeniedBean bean) {

}

@PermissionBefore
@PermissionBefore(Manifest.permission.CAMERA)
public void before(final PermissionBeforeBean beforeBean) {
new android.support.v7.app.AlertDialog.Builder(this)
.setTitle("我们需要相机权限来正常拍照")
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
beforeBean.proceed(true);
}
})
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
beforeBean.proceed(false);
}
})
.show();
List<String> notPermissions = beforeBean.getNotPermissions();
if (notPermissions.size() == 1 && Manifest.permission.ACCESS_BACKGROUND_LOCATION.equals(notPermissions.get(0))) {
beforeBean.proceed(false);
mHasPermission = true;
initCamera();
} else {
new androidx.appcompat.app.AlertDialog.Builder(this)
.setTitle("我们需要相机权限来正常拍照")
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
beforeBean.proceed(true);
}
})
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
beforeBean.proceed(false);
}
})
.show();
}
}

private void initCamera() {
Expand Down Expand Up @@ -141,5 +178,11 @@ private void releaseCamera() {
camera = null;
}
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
Log.w(TAG, "onRequestPermissionsResult: requestCode:"+requestCode+"--permissions:"+ Arrays.toString(permissions) +"--grantResults:"+ Arrays.toString(grantResults));
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import android.app.Fragment;
import android.hardware.Camera;
import android.os.Bundle;
import android.support.annotation.Nullable;
import androidx.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
Expand Down
8 changes: 4 additions & 4 deletions sample/src/main/java/com/zcx/fastpermission/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import androidx.appcompat.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;

Expand Down Expand Up @@ -71,7 +71,7 @@ private void onClickContacts() {

@PermissionBefore
public void before(final PermissionBeforeBean beforeBean) {
new android.support.v7.app.AlertDialog.Builder(this)
new androidx.appcompat.app.AlertDialog.Builder(this)
.setTitle("我们需要相机权限来正常拍照")
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
Expand All @@ -90,7 +90,7 @@ public void onClick(DialogInterface dialog, int which) {

@PermissionCanceled
public void cancel(final PermissionCanceledBean canceledBean) {
new android.support.v7.app.AlertDialog.Builder(this)
new androidx.appcompat.app.AlertDialog.Builder(this)
.setTitle("我们需要权限,是否同意一下")
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
Expand All @@ -108,7 +108,7 @@ public void onClick(DialogInterface dialog, int which) {

@PermissionDenied
public void denied(final PermissionDeniedBean deniedBean) {
new android.support.v7.app.AlertDialog.Builder(this)
new androidx.appcompat.app.AlertDialog.Builder(this)
.setTitle("我们需要权限,是否设置")
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
Expand Down
Loading

0 comments on commit acb2a6a

Please sign in to comment.