Java Code Examples for android.content.Context#checkCallingPermission()

The following examples show how to use android.content.Context#checkCallingPermission() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: NetworkStatsAccess.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
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;
}
 
Example 2
Source File: PermissionHelper.java    From Saiy-PS with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * 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;
    }
}
 
Example 3
Source File: PermissionHelper.java    From Saiy-PS with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * 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;
}
 
Example 4
Source File: PackageUtils.java    From android_packages_apps_GmsCore with Apache License 2.0 5 votes vote down vote up
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;
}