Android P包管理机制之PackageManagerService授予权限流程解析

本文主要是介绍Android P包管理机制之PackageManagerService授予权限流程解析,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

PackageManagerService中默认给系统app授予相关需要的权限。

PackageManagerService中的systemReady是在SystemServer中执行的。

\frameworks\base\services\java\com\android\server\SystemServer.java

/**
* Starts a miscellaneous grab bag of stuff that has yet to be refactored
* and organized.
*/
private void startOtherServices() {//省略一部分代码//...traceBeginAndSlog("MakePackageManagerServiceReady");mPackageManagerService.systemReady();traceEnd();//省略一部分代码//...
}

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

@Override
public void systemReady() {int[] grantPermissionsUserIds = EMPTY_INT_ARRAY;synchronized (mPackages) {// Verify that all of the preferred activity components actually// exist.  It is possible for applications to be updated and at// that point remove a previously declared activity component that// had been set as a preferred activity.  We try to clean this up// the next time we encounter that preferred activity, but it is// possible for the user flow to never be able to return to that// situation so here we do a sanity check to make sure we haven't// left any junk around.ArrayList<PreferredActivity> removed = new ArrayList<PreferredActivity>();for (int i=0; i<mSettings.mPreferredActivities.size(); i++) {PreferredIntentResolver pir = mSettings.mPreferredActivities.valueAt(i);removed.clear();for (PreferredActivity pa : pir.filterSet()) {if (mActivities.mActivities.get(pa.mPref.mComponent) == null) {removed.add(pa);}}if (removed.size() > 0) {for (int r=0; r<removed.size(); r++) {PreferredActivity pa = removed.get(r);Slog.w(TAG, "Removing dangling preferred activity: "+ pa.mPref.mComponent);pir.removeFilter(pa);}mSettings.writePackageRestrictionsLPr(mSettings.mPreferredActivities.keyAt(i));}}for (int userId : UserManagerService.getInstance().getUserIds()) {/* 为了方便授予权限修改了此处,注释掉了grantPermissionsUserIds初始化的条件* areDefaultRuntimePermissionsGrantedLPr(userId)的判断条件的含义:* true,正常启动,没有发生升级的情况* false,表示升级后第一次启动* 所以,该出的意思是,只有升级后第一次启动,发生了升级,才满足条件,执行赋予权限grantPermissionsUserIds *///modify by sunxiaolin 20190912//if (!mSettings.areDefaultRuntimePermissionsGrantedLPr(userId)) {grantPermissionsUserIds = ArrayUtils.appendInt(grantPermissionsUserIds, userId);//}}}sUserManager.systemReady();/* 跟上面的grantPermissionsUserIds条件有关,没有发生升级,grantPermissionsUserIds 为null,不执行			grantDefaultPermissions赋予权限* 发生升级,才执行mDefaultPermissionPolicy.grantDefaultPermissions(userId),执行默认授予权限的处理*/// If we upgraded grant all default permissions before kicking off.for (int userId : grantPermissionsUserIds) {Log.d(TAG, "sunxiaolin,systemReady,grantDefaultPermissions,userId:" + userId);mDefaultPermissionPolicy.grantDefaultPermissions(userId);}/**	没有发生升级,grantPermissionsUserIds 为null*	mDefaultPermissionPolicy.scheduleReadDefaultPermissionExceptions()为执行读取默认权限xml的处理*	xml路径为:/system/etc/default-permissions/default-car-permissions.xml*/if (grantPermissionsUserIds == EMPTY_INT_ARRAY) {// If we did not grant default permissions, we preload from this the// default permission exceptions lazily to ensure we don't hit the// disk on a new user creation.Log.d(TAG, "sunxiaolin,systemReady,grantPermissionsUserIds == EMPTY_INT_ARRAY");mDefaultPermissionPolicy.scheduleReadDefaultPermissionExceptions();}
}

授予权限的处理主要是在DefaultPermissionGrantPolicy中处理的。

\frameworks\base\services\core\java\com\android\server\pm\permission\DefaultPermissionGrantPolicy.java

public void grantDefaultPermissions(int userId) {//给系统组件和priv-app应用授予权限grantPermissionsToSysComponentsAndPrivApps(userId);//给系统中重要的应用包授予权限grantDefaultSystemHandlerPermissions(userId);//处理权限授予异常的情况,会去读default-car-permissions.xml的权限配置grantDefaultPermissionExceptions(userId);
}

不管grantPermissionsToSysComponentsAndPrivApps,grantDefaultSystemHandlerPermissions还是grantDefaultPermissionExceptions,最终都是执行了grantRuntimePermissions()方法,在grantRuntimePermissions中执行授权操作。

关于给第三方应用授权问题:

private void grantDefaultPermissionExceptions(int userId) {mHandler.removeMessages(MSG_READ_DEFAULT_PERMISSION_EXCEPTIONS);synchronized (mLock) {// mGrantExceptions is null only before the first read and then// it serves as a cache of the default grants that should be// performed for every user. If there is an entry then the app// is on the system image and supports runtime permissions.if (mGrantExceptions == null) {mGrantExceptions = readDefaultPermissionExceptionsLocked();}}Set<String> permissions = null;final int exceptionCount = mGrantExceptions.size();for (int i = 0; i < exceptionCount; i++) {String packageName = mGrantExceptions.keyAt(i);//modify by sunxiaolin 20190912//PackageParser.Package pkg = getSystemPackage(packageName);PackageParser.Package pkg = getPackage(packageName);List<DefaultPermissionGrant> permissionGrants = mGrantExceptions.valueAt(i);final int permissionGrantCount = permissionGrants.size();for (int j = 0; j < permissionGrantCount; j++) {DefaultPermissionGrant permissionGrant = permissionGrants.get(j);if (permissions == null) {permissions = new ArraySet<>();} else {permissions.clear();}permissions.add(permissionGrant.name);grantRuntimePermissions(pkg, permissions,permissionGrant.fixed, userId);}}
}

修改:

PackageParser.Package pkg = getSystemPackage(packageName);

为:

PackageParser.Package pkg = getPackage(packageName);
private void parseExceptions(XmlPullParser parser, Map<String, List<DefaultPermissionGrant>>outGrantExceptions) throws IOException, XmlPullParserException {final int outerDepth = parser.getDepth();int type;while ((type = parser.next()) != XmlPullParser.END_DOCUMENT&& (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {continue;}if (TAG_EXCEPTION.equals(parser.getName())) {String packageName = parser.getAttributeValue(null, ATTR_PACKAGE);Log.i(TAG, "sunxiaolin,parseExceptions packageName:" + packageName);List<DefaultPermissionGrant> packageExceptions =outGrantExceptions.get(packageName);if (packageExceptions == null) {// The package must be on the system image//modify by sunxiaolin getSystemPackage()-->getPackage()PackageParser.Package pkg = getPackage(packageName);//PackageParser.Package pkg = getSystemPackage(packageName);if (pkg == null) {Log.w(TAG, "Unknown package:" + packageName);XmlUtils.skipCurrentTag(parser);continue;}// The package must support runtime permissionsif (!doesPackageSupportRuntimePermissions(pkg)) {Log.w(TAG, "Skipping non supporting runtime permissions package:" + packageName);XmlUtils.skipCurrentTag(parser);continue;}packageExceptions = new ArrayList<>();outGrantExceptions.put(packageName, packageExceptions);}
Log.i(TAG, "sunxiaolin,parseExceptions packageName:" + packageName);parsePermission(parser, packageExceptions);} else {Log.e(TAG, "Unknown tag " + parser.getName() + "under <exceptions>");}}
}

修改:

PackageParser.Package pkg = getSystemPackage(packageName);

为:

PackageParser.Package pkg = getPackage(packageName);

默认权限文件xml解析

private File[] getDefaultPermissionFiles() {ArrayList<File> ret = new ArrayList<File>();File dir = new File(Environment.getRootDirectory(), "etc/default-permissions");if (dir.isDirectory() && dir.canRead()) {Collections.addAll(ret, dir.listFiles());}dir = new File(Environment.getVendorDirectory(), "etc/default-permissions");if (dir.isDirectory() && dir.canRead()) {Collections.addAll(ret, dir.listFiles());}dir = new File(Environment.getOdmDirectory(), "etc/default-permissions");if (dir.isDirectory() && dir.canRead()) {Collections.addAll(ret, dir.listFiles());}dir = new File(Environment.getProductDirectory(), "etc/default-permissions");if (dir.isDirectory() && dir.canRead()) {Collections.addAll(ret, dir.listFiles());}// For IoT devices, we check the oem partition for default permissions for each app.if (mContext.getPackageManager().hasSystemFeature(PackageManager.FEATURE_EMBEDDED, 0)) {dir = new File(Environment.getOemDirectory(), "etc/default-permissions");if (dir.isDirectory() && dir.canRead()) {Collections.addAll(ret, dir.listFiles());}}return ret.isEmpty() ? null : ret.toArray(new File[0]);
}

default-car-permissions.xml中添加应用权限方法:
packages/services/Car/car_product/build/default-car-permissions.xml:system/etc/default-permissions/default-car-permissions.xml
packages\services\Car\car_product\build\default-car-permissions.xml

<exceptions><!-- This is an example of an exception:<exceptionpackage="foo.bar.permission"<permission name="android.permission.READ_CONTACTS" fixed="true"/><permission name="android.permission.READ_CALENDAR" fixed="false"/></exception>--><exceptionpackage="com.android.car.messenger"><!-- Contacts --><permission name="android.permission.READ_CONTACTS" fixed="false"/><!-- SMS --><permission name="android.permission.SEND_SMS" fixed="false"/><permission name="android.permission.READ_SMS" fixed="false"/></exception></exceptions>

这篇关于Android P包管理机制之PackageManagerService授予权限流程解析的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/733619

相关文章

Linux中chmod权限设置方式

《Linux中chmod权限设置方式》本文介绍了Linux系统中文件和目录权限的设置方法,包括chmod、chown和chgrp命令的使用,以及权限模式和符号模式的详细说明,通过这些命令,用户可以灵活... 目录设置基本权限命令:chmod1、权限介绍2、chmod命令常见用法和示例3、文件权限详解4、ch

Android数据库Room的实际使用过程总结

《Android数据库Room的实际使用过程总结》这篇文章主要给大家介绍了关于Android数据库Room的实际使用过程,详细介绍了如何创建实体类、数据访问对象(DAO)和数据库抽象类,需要的朋友可以... 目录前言一、Room的基本使用1.项目配置2.创建实体类(Entity)3.创建数据访问对象(DAO

SpringBoot使用minio进行文件管理的流程步骤

《SpringBoot使用minio进行文件管理的流程步骤》MinIO是一个高性能的对象存储系统,兼容AmazonS3API,该软件设计用于处理非结构化数据,如图片、视频、日志文件以及备份数据等,本文... 目录一、拉取minio镜像二、创建配置文件和上传文件的目录三、启动容器四、浏览器登录 minio五、

在C#中合并和解析相对路径方式

《在C#中合并和解析相对路径方式》Path类提供了几个用于操作文件路径的静态方法,其中包括Combine方法和GetFullPath方法,Combine方法将两个路径合并在一起,但不会解析包含相对元素... 目录C#合并和解析相对路径System.IO.Path类幸运的是总结C#合并和解析相对路径对于 C

Java解析JSON的六种方案

《Java解析JSON的六种方案》这篇文章介绍了6种JSON解析方案,包括Jackson、Gson、FastJSON、JsonPath、、手动解析,分别阐述了它们的功能特点、代码示例、高级功能、优缺点... 目录前言1. 使用 Jackson:业界标配功能特点代码示例高级功能优缺点2. 使用 Gson:轻量

Java如何接收并解析HL7协议数据

《Java如何接收并解析HL7协议数据》文章主要介绍了HL7协议及其在医疗行业中的应用,详细描述了如何配置环境、接收和解析数据,以及与前端进行交互的实现方法,文章还分享了使用7Edit工具进行调试的经... 目录一、前言二、正文1、环境配置2、数据接收:HL7Monitor3、数据解析:HL7Busines

Mybatis拦截器如何实现数据权限过滤

《Mybatis拦截器如何实现数据权限过滤》本文介绍了MyBatis拦截器的使用,通过实现Interceptor接口对SQL进行处理,实现数据权限过滤功能,通过在本地线程变量中存储数据权限相关信息,并... 目录背景基础知识MyBATis 拦截器介绍代码实战总结背景现在的项目负责人去年年底离职,导致前期规

python解析HTML并提取span标签中的文本

《python解析HTML并提取span标签中的文本》在网页开发和数据抓取过程中,我们经常需要从HTML页面中提取信息,尤其是span元素中的文本,span标签是一个行内元素,通常用于包装一小段文本或... 目录一、安装相关依赖二、html 页面结构三、使用 BeautifulSoup javascript

Android WebView的加载超时处理方案

《AndroidWebView的加载超时处理方案》在Android开发中,WebView是一个常用的组件,用于在应用中嵌入网页,然而,当网络状况不佳或页面加载过慢时,用户可能会遇到加载超时的问题,本... 目录引言一、WebView加载超时的原因二、加载超时处理方案1. 使用Handler和Timer进行超

Nginx、Tomcat等项目部署问题以及解决流程

《Nginx、Tomcat等项目部署问题以及解决流程》本文总结了项目部署中常见的four类问题及其解决方法:Nginx未按预期显示结果、端口未开启、日志分析的重要性以及开发环境与生产环境运行结果不一致... 目录前言1. Nginx部署后未按预期显示结果1.1 查看Nginx的启动情况1.2 解决启动失败的