Android 默认授予指定应用所需权限

2 篇文章 0 订阅
1 篇文章 0 订阅
该文介绍了如何在Android系统中自动授予第三方应用权限,包括修改系统源码以默认授予所有第三方应用权限,并详细说明了如何去除USB权限弹窗,实现自动获取USB权限。同时,展示了`dumpsys`命令检查特定应用权限状态的结果。
摘要由CSDN通过智能技术生成

本文主要介绍授予第三方应用权限和自动获取USB权限

1.授予指定手动安装第三方应用权限

frameworks/base/services/core/java/com/android/server/pm/PackageManagerService.java

--- a/services/core/java/com/android/server/pm/PackageManagerService.java
+++ b/services/core/java/com/android/server/pm/PackageManagerService.java
            // Now that we successfully installed the package, grant runtime
            // permissions if requested before broadcasting the install. Also
            // for legacy apps in permission review mode we clear the permission
            // review flag which is used to emulate runtime permissions for
            // legacy apps.
+            if (grantPermissions) {
+            if (grantPermissions || res.name.equals("package.name")) {
                final int callingUid = Binder.getCallingUid();
                mPermissionManager.grantRequestedRuntimePermissions(
                        res.pkg, res.newUsers, grantedPermissions, callingUid,
                        mPermissionCallback);
            }

2. 默认授予所有第三方应用权限

--- a/services/core/java/com/android/server/pm/PackageManagerService.java
+++ b/services/core/java/com/android/server/pm/PackageManagerService.java
@@ -1511,8 +1511,10 @@ public class PackageManagerService extends IPackageManager.Stub
                         InstallArgs args = data.args;
                         PackageInstalledInfo parentRes = data.res;
 
-                        final boolean grantPermissions = (args.installFlags
-                                & PackageManager.INSTALL_GRANT_RUNTIME_PERMISSIONS) != 0;
+                        //final boolean grantPermissions = (args.installFlags
+                                //& PackageManager.INSTALL_GRANT_RUNTIME_PERMISSIONS) != 0;
+                        //Grant all permission to thirdParty app as default
+                        final boolean grantPermissions = true;

3. 去掉USB权限弹窗

使用usb设备时会弹出弹窗提示申请权限,修改关闭弹窗提示,默认自动获取USB权限

(1)配置config_disableUsbPermissionDialogs属性,但会影响CTS测试

--- a/core/res/res/values/config.xml
+++ b/core/res/res/values/config.xml
@@ -2517,7 +2517,7 @@
 
     <!-- If true, then we do not ask user for permission for apps to connect to USB devices.
          Do not set this to true for production devices. Doing so will cause you to fail CTS. -->
-    <bool name="config_disableUsbPermissionDialogs">false</bool>
+    <bool name="config_disableUsbPermissionDialogs">true</bool>

(2)在hasPermission默认return true

--- a/services/usb/java/com/android/server/usb/UsbPermissionManager.java
+++ b/services/usb/java/com/android/server/usb/UsbPermissionManager.java
@@ -133,7 +133,8 @@ class UsbPermissionManager {
      */
     boolean hasPermission(@NonNull UsbDevice device, int uid) {
         synchronized (mLock) {
-            if (uid == Process.SYSTEM_UID || mDisablePermissionDialogs) {
+                       //[Toast]add auto-granted USB permissions
+            if (uid == Process.SYSTEM_UID || mDisablePermissionDialogs || true) {
                 return true;
             }
             SparseBooleanArray uidList = mDevicePermissionMap.get(device.getDeviceName());
@@ -153,7 +154,8 @@ class UsbPermissionManager {
      */
     boolean hasPermission(@NonNull UsbAccessory accessory, int uid) {
         synchronized (mLock) {
-            if (uid == Process.SYSTEM_UID || mDisablePermissionDialogs) {
+                       //[Toast]add auto-granted USB permissions
+            if (uid == Process.SYSTEM_UID || mDisablePermissionDialogs || true) {
                 return true;
             }

3.确认permission stauts

 $ dumpsys package com.andorid.package_name

    declared permissions:
      android.permission.QUERY_ALL_PACKAGES: prot=normal, INSTALLED
    requested permissions:
      android.permission.INTERNET
      android.permission.ACCESS_NETWORK_STATE
      android.permission.ACCESS_WIFI_STATE
      android.permission.REORDER_TASKS
      android.permission.WRITE_EXTERNAL_STORAGE: restricted=true
      android.permission.WRITE_INTERNAL_STORAGE
      android.permission.READ_EXTERNAL_STORAGE: restricted=true
      android.permission.CHANGE_WIFI_MULTICAST_STATE
      android.permission.READ_PHONE_STATE
      android.permission.WAKE_LOCK
    install permissions:
      android.permission.CHANGE_WIFI_MULTICAST_STATE: granted=true
      android.permission.INTERNET: granted=true
      android.permission.REORDER_TASKS: granted=true
      android.permission.ACCESS_NETWORK_STATE: granted=true
      android.permission.ACCESS_WIFI_STATE: granted=true
      android.permission.WAKE_LOCK: granted=true
    User 0: ceDataInode=1551 installed=true hidden=false suspended=false stopped=false notLaunched=false enabled=0 instant=false virtual=false
      gids=[3003]
      runtime permissions:
        android.permission.READ_EXTERNAL_STORAGE: granted=false, flags=[ USER_SENSITIVE_WHEN_GRANTED|USER_SENSITIVE_WHEN_DENIED|RESTRICTION_UPGRADE_EXEMPT]
        android.permission.READ_PHONE_STATE: granted=false, flags=[ USER_SENSITIVE_WHEN_GRANTED|USER_SENSITIVE_WHEN_DENIED]
        android.permission.WRITE_EXTERNAL_STORAGE: granted=false, flags=[ USER_SENSITIVE_WHEN_GRANTED|USER_SENSITIVE_WHEN_DENIED|RESTRICTION_UPGRADE_EXEMPT]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

ander_chen1024

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

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

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

打赏作者

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

抵扣说明:

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

余额充值