|
@@ -3,6 +3,7 @@ package com.benyanyi.permissionlib;
|
|
|
import android.app.Activity;
|
|
|
|
|
|
import com.benyanyi.permissionlib.annotation.GetPermissionComplete;
|
|
|
+import com.benyanyi.permissionlib.annotation.GetPermissionDialogInfo;
|
|
|
import com.benyanyi.permissionlib.annotation.GetPermissionFailure;
|
|
|
import com.benyanyi.permissionlib.annotation.GetPermissions;
|
|
|
import com.benyanyi.permissionlib.annotation.GetPermissionSuccess;
|
|
@@ -18,12 +19,12 @@ import java.lang.reflect.Method;
|
|
|
*/
|
|
|
public final class PermissionBind {
|
|
|
private static PermissionBind instance;
|
|
|
- private PermissionHelper permissionHelper;
|
|
|
+ private PermissionConfig permissionConfig;
|
|
|
private Object object;
|
|
|
|
|
|
private PermissionBind(Activity activity, Object object) {
|
|
|
this.object = object;
|
|
|
- this.permissionHelper = PermissionHelper.getInstance(activity);
|
|
|
+ this.permissionConfig = PermissionHelper.getInstance(activity);
|
|
|
initPermission();
|
|
|
}
|
|
|
|
|
@@ -46,15 +47,48 @@ public final class PermissionBind {
|
|
|
String[] value = annotation.value();
|
|
|
if (value.length > 0) {
|
|
|
this.setPermissions(value);
|
|
|
- PermissionDialogInfo info = new PermissionDialogInfo();
|
|
|
- info.isShow = false;
|
|
|
- this.setPermissionDialogInfo(info);
|
|
|
+ this.setPermissionDialogInfo(annotationDialogInfo());
|
|
|
this.annotationCallBack();
|
|
|
- this.hasPermission(annotation.permissionCode());
|
|
|
+ this.hasPermission();
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 设置权限dialog
|
|
|
+ */
|
|
|
+ private PermissionDialogInfo annotationDialogInfo() {
|
|
|
+ PermissionDialogInfo dialogInfo = new PermissionDialogInfo();
|
|
|
+ Method[] methods = this.object.getClass().getDeclaredMethods();
|
|
|
+ for (Method method : methods) {
|
|
|
+ GetPermissionDialogInfo permissionDialogInfo = method.getAnnotation(GetPermissionDialogInfo.class);
|
|
|
+ if (permissionDialogInfo != null && method.getReturnType().getSimpleName().equals("PermissionDialogInfo")) {
|
|
|
+ Class<?>[] parameterTypes = method.getParameterTypes();
|
|
|
+ String i1 = "PermissionDialogInfo";
|
|
|
+ if (parameterTypes.length == 1) {
|
|
|
+ boolean boo1 = parameterTypes[0].getSimpleName().equals(i1);
|
|
|
+ try {
|
|
|
+ method.setAccessible(true);
|
|
|
+ if (boo1) {
|
|
|
+ Object invoke = method.invoke(this.object, dialogInfo);
|
|
|
+ if (invoke instanceof PermissionDialogInfo) {
|
|
|
+ return (PermissionDialogInfo) invoke;
|
|
|
+ } else {
|
|
|
+ dialogInfo.isShow = false;
|
|
|
+ return dialogInfo;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ dialogInfo.isShow = false;
|
|
|
+ return dialogInfo;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ dialogInfo.isShow = false;
|
|
|
+ return dialogInfo;
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 权限申请结果回调注解处理
|
|
|
*/
|
|
@@ -62,51 +96,34 @@ public final class PermissionBind {
|
|
|
this.setPermissionCallBack(new PermissionCallBack() {
|
|
|
@Override
|
|
|
public void onPermissionSuccess(int permissionCode) {
|
|
|
- annotationSuccess(permissionCode);
|
|
|
+ annotationSuccess();
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public void onPermissionFailure(FailureMsg failureMsg) {
|
|
|
- annotationFailure(failureMsg);
|
|
|
+ annotationFailure(failureMsg.getFailurePermissions());
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public void onPermissionComplete(int permissionCode) {
|
|
|
- annotationComplete(permissionCode);
|
|
|
+ annotationComplete();
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 权限获取成功处理注解
|
|
|
- *
|
|
|
- * @param permissionCode
|
|
|
*/
|
|
|
- private void annotationSuccess(int permissionCode) {
|
|
|
+ private void annotationSuccess() {
|
|
|
Method[] methods = this.object.getClass().getDeclaredMethods();
|
|
|
for (Method method : methods) {
|
|
|
GetPermissionSuccess permissionSuccess = method.getAnnotation(GetPermissionSuccess.class);
|
|
|
if (permissionSuccess != null) {
|
|
|
- int[] values = permissionSuccess.value();
|
|
|
- if (values.length > 0) {
|
|
|
- for (int value : values) {
|
|
|
- if (value == permissionCode) {
|
|
|
- Class<?>[] parameterTypes = method.getParameterTypes();
|
|
|
- String i1 = "Integer";
|
|
|
- String i2 = "int";
|
|
|
- boolean boo1 = parameterTypes != null && parameterTypes.length == 1
|
|
|
- && (parameterTypes[0].getSimpleName().equals(i1)
|
|
|
- || parameterTypes[0].getSimpleName().equals(i2));
|
|
|
- if (boo1) {
|
|
|
- method.setAccessible(true);
|
|
|
- try {
|
|
|
- method.invoke(this.object, permissionCode);
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
+ method.setAccessible(true);
|
|
|
+ try {
|
|
|
+ method.invoke(this.object);
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
}
|
|
|
}
|
|
|
}
|
|
@@ -115,90 +132,65 @@ public final class PermissionBind {
|
|
|
|
|
|
/**
|
|
|
* 权限未全部获取成功注解处理
|
|
|
- *
|
|
|
- * @param failureMsg
|
|
|
*/
|
|
|
- private void annotationFailure(FailureMsg failureMsg) {
|
|
|
+ private void annotationFailure(String[] failurePermissions) {
|
|
|
Method[] methods = this.object.getClass().getDeclaredMethods();
|
|
|
for (Method method : methods) {
|
|
|
GetPermissionFailure permissionFailure = method.getAnnotation(GetPermissionFailure.class);
|
|
|
if (permissionFailure != null) {
|
|
|
- int[] values = permissionFailure.value();
|
|
|
- if (values.length > 0) {
|
|
|
- for (int value : values) {
|
|
|
- if (value == failureMsg.getPermissionCode()) {
|
|
|
- Class<?>[] parameterTypes = method.getParameterTypes();
|
|
|
- String str1 = "FailureMsg";
|
|
|
- if (parameterTypes != null && parameterTypes.length == 1
|
|
|
- && parameterTypes[0].getSimpleName().equals(str1)) {
|
|
|
- method.setAccessible(true);
|
|
|
- try {
|
|
|
- method.invoke(this.object, failureMsg);
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
+ Class<?>[] parameterTypes = method.getParameterTypes();
|
|
|
+ String str1 = "String[]";
|
|
|
+ if (parameterTypes != null) {
|
|
|
+ for (Class<?> aClass : parameterTypes) {
|
|
|
+ if (aClass.getSimpleName().equals(str1)) {
|
|
|
+ method.setAccessible(true);
|
|
|
+ try {
|
|
|
+ method.invoke(this.object, (Object) failurePermissions);
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 权限请求完成后注解处理
|
|
|
- *
|
|
|
- * @param permissionCode
|
|
|
*/
|
|
|
- private void annotationComplete(int permissionCode) {
|
|
|
+ private void annotationComplete() {
|
|
|
Method[] methods = this.object.getClass().getDeclaredMethods();
|
|
|
for (Method method : methods) {
|
|
|
GetPermissionComplete permissionComplete = method.getAnnotation(GetPermissionComplete.class);
|
|
|
if (permissionComplete != null) {
|
|
|
- int[] values = permissionComplete.value();
|
|
|
- if (values.length > 0) {
|
|
|
- for (int value : values) {
|
|
|
- if (value == permissionCode) {
|
|
|
- Class<?>[] parameterTypes = method.getParameterTypes();
|
|
|
- String i1 = "Integer";
|
|
|
- String i2 = "int";
|
|
|
- boolean boo = parameterTypes != null && parameterTypes.length == 1
|
|
|
- && (parameterTypes[0].getSimpleName().equals(i1)
|
|
|
- || parameterTypes[0].getSimpleName().equals(i2));
|
|
|
- if (boo) {
|
|
|
- method.setAccessible(true);
|
|
|
- try {
|
|
|
- method.invoke(this.object, permissionCode);
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
+ method.setAccessible(true);
|
|
|
+ try {
|
|
|
+ method.invoke(this.object);
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-
|
|
|
private void setPermissions(String... permissions) {
|
|
|
- this.permissionHelper.setPermissions(permissions);
|
|
|
+ this.permissionConfig.setPermissions(permissions);
|
|
|
}
|
|
|
|
|
|
private void setPermissionDialogInfo(PermissionDialogInfo info) {
|
|
|
- this.permissionHelper.setPermissionDialogInfo(info);
|
|
|
+ this.permissionConfig.setPermissionDialogInfo(info);
|
|
|
}
|
|
|
|
|
|
private void setPermissionCallBack(PermissionCallBack callBack) {
|
|
|
- this.permissionHelper.setPermissionCallBack(callBack);
|
|
|
+ this.permissionConfig.setPermissionCallBack(callBack);
|
|
|
}
|
|
|
|
|
|
- private void hasPermission(int permissionCode) {
|
|
|
- this.permissionHelper.hasPermission(permissionCode);
|
|
|
+ private void hasPermission() {
|
|
|
+ this.permissionConfig.hasPermission(0x100);
|
|
|
}
|
|
|
|
|
|
- public PermissionHelper getPermissionHelper() {
|
|
|
- return this.permissionHelper;
|
|
|
+ public PermissionConfig getPermissionConfig() {
|
|
|
+ return this.permissionConfig;
|
|
|
}
|
|
|
}
|