2025-11-20 14:21:42 +08:00
|
|
|
package com.tfq.library.permission;
|
|
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
|
import android.util.Log;
|
|
|
|
|
|
|
|
|
|
import com.hjq.permissions.permission.base.IPermission;
|
|
|
|
|
import com.hjq.toast.Toaster;
|
|
|
|
|
import com.tfq.library.R;
|
|
|
|
|
|
|
|
|
|
import java.lang.reflect.Method;
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
|
|
public class PermissionUtils {
|
|
|
|
|
|
2025-11-20 15:06:02 +08:00
|
|
|
public static IPermission invokeGetCachePermission(Context context, String permission) {
|
2025-11-20 14:21:42 +08:00
|
|
|
try {
|
2025-11-20 15:06:02 +08:00
|
|
|
Class<?> xxPermissionsClass = Class.forName("com.hjq.permissions.permissions.PermissionLists");
|
2025-11-20 14:21:42 +08:00
|
|
|
// 尝试获取方法,可能需要调整方法名和参数
|
|
|
|
|
Method method = null;
|
|
|
|
|
try {
|
|
|
|
|
// 尝试获取静态方法
|
|
|
|
|
method = xxPermissionsClass.getDeclaredMethod("getCachePermission", Context.class, String.class);
|
|
|
|
|
} catch (NoSuchMethodException e1) {
|
|
|
|
|
try {
|
|
|
|
|
// 尝试获取实例方法
|
|
|
|
|
method = xxPermissionsClass.getDeclaredMethod("getCachePermission", Context.class, String.class);
|
|
|
|
|
} catch (NoSuchMethodException e2) {
|
|
|
|
|
// 尝试其他可能的方法名
|
|
|
|
|
String[] possibleMethodNames = {
|
|
|
|
|
"getPermissionCache",
|
|
|
|
|
"getCachedPermission",
|
|
|
|
|
"checkCachedPermission"
|
|
|
|
|
};
|
|
|
|
|
for (String methodName : possibleMethodNames) {
|
|
|
|
|
try {
|
|
|
|
|
method = xxPermissionsClass.getDeclaredMethod(methodName, Context.class, String.class);
|
|
|
|
|
break;
|
|
|
|
|
} catch (NoSuchMethodException e3) {
|
|
|
|
|
// 继续尝试下一个方法名
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (method == null) {
|
|
|
|
|
throw new NoSuchMethodException("No suitable cache method found");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// 设置可访问
|
|
|
|
|
method.setAccessible(true);
|
|
|
|
|
// 调用方法
|
2025-11-20 15:06:02 +08:00
|
|
|
Object result = method.invoke(null, permission);
|
2025-11-20 14:21:42 +08:00
|
|
|
// 处理返回值
|
|
|
|
|
if (result instanceof IPermission) {
|
|
|
|
|
return (IPermission) result;
|
|
|
|
|
} else {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
Log.e("Reflection", "Failed to invoke cache method", e);
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static List<IPermission> convertToIPermissionArray(Context context, String[] permissions) {
|
|
|
|
|
// 将String[]转换为List<IPermission>
|
|
|
|
|
List<IPermission> permissionList = new ArrayList<>();
|
|
|
|
|
|
|
|
|
|
if (permissions == null || permissions.length == 0) {
|
|
|
|
|
return permissionList;
|
|
|
|
|
}
|
|
|
|
|
for (int i = 0; i < permissions.length; i++) {
|
|
|
|
|
String permission = permissions[i];
|
|
|
|
|
// 1. 尝试通过反射获取缓存的IPermission
|
|
|
|
|
IPermission cachedPermission = invokeGetCachePermission(context, permission);
|
|
|
|
|
if (cachedPermission != null) {
|
|
|
|
|
permissionList.add(cachedPermission);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return permissionList;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static void showGrantedPermissionsToast(Context context, List<IPermission> grantedList) {
|
|
|
|
|
Toaster.show(String.format(context.getString(R.string.demo_obtain_permission_success_hint)
|
|
|
|
|
, PermissionConverter.getNickNamesByPermissions(context, grantedList)));
|
|
|
|
|
}
|
|
|
|
|
}
|