一给 发表于 2024-8-19 16:58:04

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

做AOSP定制实现时间,为了增长用户体验,系统预装的三方app会要求默认授权权限。也就是相当于在首次开机时间用代码默认将“设置”中申请的权限,用代码默认全部授权通过。这样用户可以直接进入app,不会在弹出确认授权提示框。
一、未授权前提示框
https://i-blog.csdnimg.cn/blog_migrate/db8335ec8eab5bcfa96e26d25c5eb0bd.png
二、修改方式
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
   */
    privateHashSet<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
   

2.约380行grantDefaultPermissions(int userId)方法加入
   // add start
      //授权白名单应用
      grantAllWhitePermissions(pm,userId);
   // add end
      
https://i-blog.csdnimg.cn/blog_migrate/0b9f8df0a0fe0cd18c90bcf1fda0e22c.png
3.新建sys_app_grant_permission_list.txt文件,写入白名单授权包名
net.***
com.***.***2

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

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
页: [1]
查看完整版本: android12指定应用白名单默认授权