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

相关文章

SpringCloud动态配置注解@RefreshScope与@Component的深度解析

《SpringCloud动态配置注解@RefreshScope与@Component的深度解析》在现代微服务架构中,动态配置管理是一个关键需求,本文将为大家介绍SpringCloud中相关的注解@Re... 目录引言1. @RefreshScope 的作用与原理1.1 什么是 @RefreshScope1.

Java并发编程必备之Synchronized关键字深入解析

《Java并发编程必备之Synchronized关键字深入解析》本文我们深入探索了Java中的Synchronized关键字,包括其互斥性和可重入性的特性,文章详细介绍了Synchronized的三种... 目录一、前言二、Synchronized关键字2.1 Synchronized的特性1. 互斥2.

Java的IO模型、Netty原理解析

《Java的IO模型、Netty原理解析》Java的I/O是以流的方式进行数据输入输出的,Java的类库涉及很多领域的IO内容:标准的输入输出,文件的操作、网络上的数据传输流、字符串流、对象流等,这篇... 目录1.什么是IO2.同步与异步、阻塞与非阻塞3.三种IO模型BIO(blocking I/O)NI

Python 中的异步与同步深度解析(实践记录)

《Python中的异步与同步深度解析(实践记录)》在Python编程世界里,异步和同步的概念是理解程序执行流程和性能优化的关键,这篇文章将带你深入了解它们的差异,以及阻塞和非阻塞的特性,同时通过实际... 目录python中的异步与同步:深度解析与实践异步与同步的定义异步同步阻塞与非阻塞的概念阻塞非阻塞同步

Android Kotlin 高阶函数详解及其在协程中的应用小结

《AndroidKotlin高阶函数详解及其在协程中的应用小结》高阶函数是Kotlin中的一个重要特性,它能够将函数作为一等公民(First-ClassCitizen),使得代码更加简洁、灵活和可... 目录1. 引言2. 什么是高阶函数?3. 高阶函数的基础用法3.1 传递函数作为参数3.2 Lambda

Android自定义Scrollbar的两种实现方式

《Android自定义Scrollbar的两种实现方式》本文介绍两种实现自定义滚动条的方法,分别通过ItemDecoration方案和独立View方案实现滚动条定制化,文章通过代码示例讲解的非常详细,... 目录方案一:ItemDecoration实现(推荐用于RecyclerView)实现原理完整代码实现

Redis中高并发读写性能的深度解析与优化

《Redis中高并发读写性能的深度解析与优化》Redis作为一款高性能的内存数据库,广泛应用于缓存、消息队列、实时统计等场景,本文将深入探讨Redis的读写并发能力,感兴趣的小伙伴可以了解下... 目录引言一、Redis 并发能力概述1.1 Redis 的读写性能1.2 影响 Redis 并发能力的因素二、

Spring MVC使用视图解析的问题解读

《SpringMVC使用视图解析的问题解读》:本文主要介绍SpringMVC使用视图解析的问题解读,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录Spring MVC使用视图解析1. 会使用视图解析的情况2. 不会使用视图解析的情况总结Spring MVC使用视图

Android App安装列表获取方法(实践方案)

《AndroidApp安装列表获取方法(实践方案)》文章介绍了Android11及以上版本获取应用列表的方案调整,包括权限配置、白名单配置和action配置三种方式,并提供了相应的Java和Kotl... 目录前言实现方案         方案概述一、 androidManifest 三种配置方式

Spring AI ectorStore的使用流程

《SpringAIectorStore的使用流程》SpringAI中的VectorStore是一种用于存储和检索高维向量数据的数据库或存储解决方案,它在AI应用中发挥着至关重要的作用,本文给大家介... 目录一、VectorStore的基本概念二、VectorStore的核心接口三、VectorStore的