当前位置:   article > 正文

android12指定应用白名单默认授权_defaultpermissiongrantpolicy.java

defaultpermissiongrantpolicy.java

AOSP定制实现时候,为了增加用户体验,系统预装的三方app会要求默认授权权限。也就是相当于在首次开机时候用代码默认将“设置”中申请的权限,用代码默认全部授权通过。这样用户可以直接进入app,不会在弹出确认授权提示框。
一、未授权前提示框

在这里插入图片描述

二、修改方式

1.路径frameworks/base/services/core/java/com/android/server/pm/permission/DefaultPermissionGrantPolicy.java尾部加入如下代码

import android.os.Binder;
import android.os.Environment;
import java.io.BufferedReader;
import java.util.HashSet;

......

  //tyw add start
    /**
     * 给符合条件的app赋予权限
     */
    public void grantAllWhitePermissions(DelayingPackageManagerCache pm,int userId) {
        Log.i(TAG, "启动给白名单符合条件的app赋予权限,userId--> "+userId);
        final long ident = Binder.clearCallingIdentity();
        try {
            HashSet<String> packageNames = getGrantSystemAppFromFile(GRANT_SYS_APP_LIST_SYSTEM);
            List<PackageInfo> packages = mContext.getPackageManager().getInstalledPackagesAsUser( DEFAULT_PACKAGE_INFO_QUERY_FLAGS, userId);
            Log.i(TAG,packageNames.size()+"<-白名单条数,总安装包->"+packages.size());
            for (PackageInfo pkg : packages) {
                 String packageName =pkg.packageName;
                if (pkg == null||packageName==null) {
                    continue;
                }
                if(packageNames.contains(packageName)){
                    Log.i(TAG, "授权PackageInfo=" + pkg +"--packageName:"+packageName);
                    //指定包名授权
                    grantRuntimePermissionsForPackage(pm,userId, pkg);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            Binder.restoreCallingIdentity(ident);
        }
        Log.i(TAG, "app权限结束");
    }
    /**
     *授予app的运行时权限
     * @param userId
     * @param pkg
     */
    private void grantRuntimePermissionsForPackage(DelayingPackageManagerCache pm,int userId, PackageInfo pkg) {
        Set<String> permissions = new ArraySet<>();
    
        String [] pkgofPermissions = pkg.requestedPermissions;
        if(pkgofPermissions== null || pkgofPermissions.length==0){
            return;
        }
        for (String permission :  pkgofPermissions) {
            final PermissionInfo permissioninfo = pm.getPermissionInfo(permission);
            if (permissioninfo == null) {
                continue;
            }
            if (permissioninfo.isRuntime()) {
                Log.i(TAG, "授权:permission="+permission);
                permissions.add(permission);
            }
        }
        if (!permissions.isEmpty()) {
            grantRuntimePermissions(pm,pkg, permissions, true, userId);
        }
        
    }



    /**
     * 记录白名单app包名
     * PRODUCT_COPY_FILES += $(LOCAL_PATH)/sys_app_grant_permission_list.conf:system/etc/permissions/sys_app_grant_permission_list.conf
     */
    private static final File GRANT_SYS_APP_LIST_SYSTEM = Environment.buildPath(Environment.getRootDirectory(), "etc", "permissions","sys_app_grant_permission_list.conf");

    /**
     * Get removable system app list from config file
     *

     * @param file
     *            The config file
     */
    private  HashSet<String>  getGrantSystemAppFromFile(File file) {
        HashSet<String> resultSet = new HashSet<>();
        FileReader fr = null;
        BufferedReader br = null;
        try {
            if (file.exists()) {
                fr = new FileReader(file);
            } else {
                Log.e(TAG, "app白名单不存在->" + file);
                return resultSet;
            }
            br = new BufferedReader(fr);
            String line;
            while ((line = br.readLine()) != null) {
                line = line.trim();
                if (!TextUtils.isEmpty(line)) {
                    Log.d(TAG, "read line--》 " + line);
                    resultSet.add(line);
                }
            }

            Log.i(TAG,"GRANT_SYS_APP_LIST_SYSTEM size="+resultSet.size());
        } catch (Exception io) {
            Log.d(TAG, io.getMessage());
        } finally {
            try {
                if (br != null) {
                    br.close();
                }
                if (fr != null) {
                    fr.close();
                }
            } catch (IOException io) {
                Log.d(TAG, io.getMessage());
            }
        }
        return resultSet;
    }
    // tyw add end
   

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120

2.约380行grantDefaultPermissions(int userId)方法加入

   // add start
        //授权白名单应用
        grantAllWhitePermissions(pm,userId);
   // add end
      
  • 1
  • 2
  • 3
  • 4
  • 5

在这里插入图片描述

3.新建sys_app_grant_permission_list.txt文件,写入白名单授权包名

net.***
com.***.***2

  • 1
  • 2
  • 3

4.在mk中构建加入sys_app_grant_permission_list.conf拷贝

PRODUCT_COPY_FILES += $(LOCAL_PATH)/sys_app_grant_permission_list.conf:system/etc/permissions/sys_app_grant_permission_list.conf
  • 1

5.这是重新刷机,应用就可以面授权直接进入了。进入设置权限页面也可以看到。相应的权限已经默认授予了。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/1008164
推荐阅读
相关标签
  

闽ICP备14008679号