@PermissionResult
    public static int checkPermission(@NonNull Context context, @NonNull String permission,
                                      int pid, int uid, @Nullable String packageName) {
        if (context.checkPermission(permission, pid, uid) == PackageManager.PERMISSION_DENIED) {
            return PERMISSION_DENIED;
        }

        String op = AppOpsManagerCompat.permissionToOp(permission);
        if (op == null) {
            return PERMISSION_GRANTED;
        }

        if (packageName == null) {
            String[] packageNames = context.getPackageManager().getPackagesForUid(uid);
            if (packageNames == null || packageNames.length <= 0) {
                return PERMISSION_DENIED;
            }
            packageName = packageNames[0];
        }

        int proxyUid = Process.myUid();
        String proxyPackageName = context.getPackageName();
        boolean isCheckSelfPermission =
                proxyUid == uid && ObjectsCompat.equals(proxyPackageName, packageName);

        int checkOpResult;
        if (isCheckSelfPermission) {
            checkOpResult = AppOpsManagerCompat.checkOrNoteProxyOp(context, uid, op, packageName);
        } else {
            checkOpResult = AppOpsManagerCompat.noteProxyOpNoThrow(context, op, packageName);
        }

        return checkOpResult == AppOpsManagerCompat.MODE_ALLOWED ? PERMISSION_GRANTED :
                PERMISSION_DENIED_APP_OP;
    }
Checks whether a given package in a UID and PID has a given permission
and whether the app op that corresponds to this permission is allowed.

@param context     Context for accessing resources.
@param permission  The permission to check.
@param pid         The process id for which to check.
@param uid         The uid for which to check.
@param packageName The package name for which to check. If null the
                   the first package for the calling UID will be used.
@return The permission check result which is either {@link #PERMISSION_GRANTED}
or {@link #PERMISSION_DENIED} or {@link #PERMISSION_DENIED_APP_OP}.
android/android_framework/base/src/main/java/github/tornaco/android/thanos/core/compat/PermissionChecker.java