Android Framework不弹窗默认授予所有权限

简介

安卓权限,有普通动态权限与特殊权限比如MANAGE_EXTERNAL_STORAGE,要自动授予需要修改不同的代码。
二者差异:
在这里插入图片描述

Android11

动态权限(Runtime Permissions)

在安卓系统中,通过弹窗(对话框)授予的权限通常指的是 运行时权限(Runtime Permissions),也称为 危险权限(Dangerous Permissions)。

PermissionManagerService.java
该文件定义了 PermissionManagerService 类,它是 Android 权限模型的关键组成部分,负责处理应用权限的授予、撤销、查询、运行时权限检查等逻辑。

关键修改,左边是原始,右边是修改过代码,实现大部分权限运行时自动授予不弹窗,如下图:
在这里插入图片描述
源码路径

frameworks/base/services/core/java/com/android/server/pm/permission/PermissionManagerService.java

原始restorePermissionState函数中部分代码如下:

                // Keep track of app op permissions.
                if (bp.isAppOp()) {
                    mSettings.addAppOpPackage(perm, pkg.getPackageName());
                }

                if (bp.isNormal()) {
                    // For all apps normal permissions are install time ones.
                    grant = GRANT_INSTALL;
                } else if (bp.isRuntime()) {
                    if (origPermissions.hasInstallPermission(bp.getName())
                            || upgradedActivityRecognitionPermission != null) {
                        // Before Q we represented some runtime permissions as install permissions,
                        // in Q we cannot do this anymore. Hence upgrade them all.
                        grant = GRANT_UPGRADE;
                    } else {
                        // For modern apps keep runtime permissions unchanged.
                        grant = GRANT_RUNTIME;
                    }
                } else if (bp.isSignature()) {
                    // For all apps signature permissions are install time ones.
                    allowedSig = grantSignaturePermission(perm, pkg, ps, bp, origPermissions);
                    if (allowedSig) {
                        grant = GRANT_INSTALL;
                    }
                }

                if (DEBUG_PERMISSIONS && Build.IS_DEBUGGABLE) {
                    Slog.i(TAG, "Considering granting permission " + perm + " to package "
                            + pkg.getPackageName() + " grant " + grant);
                }

                if (grant != GRANT_DENIED) {
                    if (!ps.isSystem() && ps.areInstallPermissionsFixed() && !bp.isRuntime()) {
                        // If this is an existing, non-system package, then
                        // we can't add any new permissions to it. Runtime
                        // permissions can be added any time - they ad dynamic.
                        if (!allowedSig && !origPermissions.hasInstallPermission(perm)) {
                            // Except...  if this is a permission that was added
                            // to the platform (note: need to only do this when
                            // updating the platform).
                            if (!isNewPlatformPermissionForPackage(perm, pkg)) {
                                grant = GRANT_DENIED;
                            }
                        }
                    }

bp 是一个 BasePermission 对象,代表系统中已知的一个权限定义(如 CAMERA, READ_CONTACTS 等)。

GRANT_INSTALL表示在安装时自动授予,无需用户交互。

所以可以改成如下代码:
grant赋值为GRANT_INSTALL

                // Keep track of app op permissions.
                if (bp.isAppOp()) {
                    mSettings.addAppOpPackage(perm, pkg.getPackageName());
                }

                if (bp.isNormal()) {
                    // For all apps normal permissions are install time ones.
                    grant = GRANT_INSTALL;
                } else if (bp.isRuntime()) {
                    if (origPermissions.hasInstallPermission(bp.getName())
                            || upgradedActivityRecognitionPermission != null) {
                        // Before Q we represented some runtime permissions as install permissions,
                        // in Q we cannot do this anymore. Hence upgrade them all.
                        grant = GRANT_INSTALL;
                    } else {
                        // For modern apps keep runtime permissions unchanged.
                        grant = GRANT_INSTALL;
                    }
                } else if (bp.isSignature()) {
                    // For all apps signature permissions are install time ones.
                    allowedSig = grantSignaturePermission(perm, pkg, ps, bp, origPermissions);
                    if (allowedSig) {
                        grant = GRANT_INSTALL;
                    }
                }

                if (DEBUG_PERMISSIONS && Build.IS_DEBUGGABLE) {
                    Slog.i(TAG, "Considering granting permission " + perm + " to package "
                            + pkg.getPackageName() + " grant " + grant);
                }
				

                if (grant != GRANT_DENIED) {
                    if (!ps.isSystem() && ps.areInstallPermissionsFixed() && !bp.isRuntime()) {
                        // If this is an existing, non-system package, then
                        // we can't add any new permissions to it. Runtime
                        // permissions can be added any time - they ad dynamic.
                        if (!allowedSig && !origPermissions.hasInstallPermission(perm)) {
                            // Except...  if this is a permission that was added
                            // to the platform (note: need to only do this when
                            // updating the platform).
                            if (!isNewPlatformPermissionForPackage(perm, pkg)) {
                                grant = GRANT_INSTALL;
                            }
                        }
                    }

MANAGE_EXTERNAL_STORAGE权限

MANAGE_EXTERNAL_STORAGE与传统的读写权限有很大的区别, 它与浮窗的权限类似, 由AppOpsService进行管理, 要与其它的动态权限改法分开。

MANAGE_EXTERNAL_STORAGE 权限概述
‌- 权限定义‌:MANAGE_EXTERNAL_STORAGE 权限允许应用对共享存储空间中的所有文件进行读写访问,包括 MediaStore.Files 表的内容、USB On-The-Go (OTG) 驱动器和 SD 卡的根目录,以及对大多数内部存储目录的写入权限。
‌- 使用场景‌:该权限通常用于需要广泛访问设备文件的应用,如文件管理器、备份和恢复应用、防病毒应用等。
在这里插入图片描述

修改
frameworks/base/core/java/android/app/AppOpsManager.java

   /**
     * This specifies the default mode for each operation.
     */
    private static int[] sOpDefaultMode = new int[] {
            AppOpsManager.MODE_ALLOWED, // COARSE_LOCATION
            AppOpsManager.MODE_ALLOWED, // FINE_LOCATION
            AppOpsManager.MODE_ALLOWED, // GPS
            AppOpsManager.MODE_DEFAULT, // MANAGE_EXTERNAL_STORAGE
    }

把MANAGE_EXTERNAL_STORAGE改成MODE_ALLOWED,如下代码

   /**
     * This specifies the default mode for each operation.
     */
    private static int[] sOpDefaultMode = new int[] {
            AppOpsManager.MODE_ALLOWED, // COARSE_LOCATION
            AppOpsManager.MODE_ALLOWED, // FINE_LOCATION
            AppOpsManager.MODE_ALLOWED, // GPS
            AppOpsManager.MODE_ALLOWED, // MANAGE_EXTERNAL_STORAGE
    }

Android12

动态权限(Runtime Permissions)

与Android11的改法有点不一样
改法如下图:
在这里插入图片描述
实际修改此文件
frameworks/base/services/core/java/com/android/server/pm/permission/Permission.java

    public boolean isNormal() {
        return (mPermissionInfo.protectionLevel & PermissionInfo.PROTECTION_MASK_BASE)
                == PermissionInfo.PROTECTION_NORMAL;
    }
    public boolean isRuntime() {
        return (mPermissionInfo.protectionLevel & PermissionInfo.PROTECTION_MASK_BASE)
                == PermissionInfo.PROTECTION_DANGEROUS;
    }

改成

    public boolean isNormal() {
        //return (mPermissionInfo.protectionLevel & PermissionInfo.PROTECTION_MASK_BASE)
        //        == PermissionInfo.PROTECTION_NORMAL;
        return true;
    }
    public boolean isRuntime() {
        //return (mPermissionInfo.protectionLevel & PermissionInfo.PROTECTION_MASK_BASE)
        //        == PermissionInfo.PROTECTION_DANGEROUS;
        return false;
    }

MANAGE_EXTERNAL_STORAGE权限

与Android11改法一样。
Android Framework专栏链接
作者:帅得不敢出门

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

帅得不敢出门

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值