下面列出了android.content.Context#checkCallingPermission ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private static boolean hasAppOpsPermission(
Context context, int callingUid, String callingPackage) {
if (callingPackage != null) {
AppOpsManager appOps = (AppOpsManager) context.getSystemService(
Context.APP_OPS_SERVICE);
final int mode = appOps.noteOp(AppOpsManager.OP_GET_USAGE_STATS,
callingUid, callingPackage);
if (mode == AppOpsManager.MODE_DEFAULT) {
// The default behavior here is to check if PackageManager has given the app
// permission.
final int permissionCheck = context.checkCallingPermission(
Manifest.permission.PACKAGE_USAGE_STATS);
return permissionCheck == PackageManager.PERMISSION_GRANTED;
}
return (mode == AppOpsManager.MODE_ALLOWED);
}
return false;
}
/**
* Check if the calling application has the correct permission to control Saiy.
*
* @param ctx the application context
* @return true if the permission has been granted.
*/
public static boolean checkSaiyRemotePermission(@NonNull final Context ctx) {
switch (ctx.checkCallingPermission(Manifest.permission.CONTROL_SAIY)) {
case PackageManager.PERMISSION_GRANTED:
if (DEBUG) {
MyLog.i(CLS_NAME, "checkSaiyRemotePermission: PERMISSION_GRANTED");
}
return true;
case PackageManager.PERMISSION_DENIED:
if (DEBUG) {
MyLog.i(CLS_NAME, "checkSaiyRemotePermission: PERMISSION_DENIED");
}
default:
return false;
}
}
/**
* Check if the calling application has the correct permission to control Saiy.
*
* @param ctx the application context
* @param callingUid of the remote request
* @return true if the permission has been granted.
*/
public static boolean checkSaiyPermission(@NonNull final Context ctx, final int callingUid) {
final String packageName = ctx.getPackageManager().getNameForUid(callingUid);
if (DEBUG) {
MyLog.i(CLS_NAME, "checkSaiyPermission: " + packageName);
}
if (UtilsString.notNaked(packageName) && callingUid > 0) {
if (!packageName.matches(ctx.getPackageName())) {
switch (ctx.checkCallingPermission(Manifest.permission.CONTROL_SAIY)) {
case PackageManager.PERMISSION_GRANTED:
if (DEBUG) {
MyLog.i(CLS_NAME, "checkSaiyPermission: PERMISSION_GRANTED");
}
return true;
case PackageManager.PERMISSION_DENIED:
default:
if (DEBUG) {
MyLog.i(CLS_NAME, "checkSaiyPermission: PERMISSION_DENIED");
}
return false;
}
} else {
if (DEBUG) {
MyLog.i(CLS_NAME, "checkSaiyPermission: self");
}
return true;
}
} else {
MyLog.e(CLS_NAME, ctx.getString(ai.saiy.android.R.string.error_package_name_null));
}
return false;
}
public static boolean callerHasExtendedAccess(Context context) {
String[] packagesForUid = context.getPackageManager().getPackagesForUid(Binder.getCallingUid());
if (packagesForUid != null && packagesForUid.length != 0) {
for (String packageName : packagesForUid) {
if (isGooglePackage(context, packageName) || GMS_PACKAGE_NAME.equals(packageName))
return true;
}
}
return context.checkCallingPermission(Manifest.permission.EXTENDED_ACCESS) == PackageManager.PERMISSION_GRANTED;
}