当前位置:首页 » 《关注互联网》 » 正文

Android APP 默认赋予权限_Hello World

20 人参与  2021年09月07日 11:23  分类 : 《关注互联网》  评论

点击全文阅读


Android APP 默认赋予权限

首先在/vendor/xxxx/中创建etc/文件夹

在etc/中创建Android.bpdefault-permissions-xxxx.xml文件

Android.bp文件写法:

prebuilt_etc {
    name: "default_permissions_whitelist_xxxx",
    product_specific: true,
    sub_dir: "default-permissions",
    src: "default-permissions-xxxx.xml",
    filename_from_src: true,
}

default-permissions-xxxx.xml文件写法:

<?xml version='1.0' encoding='utf-8' standalone='yes' ?>

<!--
  ~ Copyright (C) 2016 The Android Open Source Project
  ~
  ~ Licensed under the Apache License, Version 2.0 (the "License");
  ~ you may not use this file except in compliance with the License.
  ~ You may obtain a copy of the License at
  ~
  ~       http://www.apache.org/licenses/LICENSE-2.0
  ~
  ~ Unless required by applicable law or agreed to in writing, software
  ~ distributed under the License is distributed on an "AS IS" BASIS,
  ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  ~ See the License for the specific language governing permissions and
  ~ limitations under the License
  -->

<!--
This XML file declares which signature|privileged permissions should be granted to privileged
applications that come with the platform
-->

<exceptions>
 
  <!-- exception package 为要授予权限的app包名 -->
  <!-- permission name 为要授予权限的app默认授予的权限 -->
  <!-- permission的fixed表示授权后是否可以被非系统组件修改权限 -->
  <exception package="com.xxxx.demo">
    <permission name="android.permission.INTERNET" fixed="false"/>
    <permission name="android.permission.WRITE_EXTERNAL_STORAGE" fixed="false"/>
    <permission name="android.permission.ACCESS_NETWORK_STATE" fixed="false"/>
    <permission name="android.permission.READ_EXTERNAL_STORAGE" fixed="false"/>
    <permission name="android.permission.RECORD_AUDIO" fixed="false"/>
    <permission name="android.permission.READ_PHONE_STATE" fixed="false"/>
    <permission name="android.permission.ACCESS_WIFI_STATE" fixed="false"/>
    <permission name="Manifest.permission.CAMERA" fixed="false"/>
    <permission name="Manifest.permission.READ_PHONE_STATE" fixed="false"/>
    <permission name="Manifest.permission.RECORD_AUDIO" fixed="false"/>
    <permission name="Manifest.permission.ACCESS_COARSE_LOCATION" fixed="false"/>
    <permission name="Manifest.permission.ACCESS_FINE_LOCATION" fixed="false"/>
    <permission name="android.permission.CHANGE_WIFI_STATE" fixed="false"/>
    <permission name="android.permission.CAMERA" fixed="false"/>
    <permission name="android.permission.FLASHLIGHT" fixed="false"/>
    <permission name="android.permission.VIBRATE" fixed="false"/>
    <permission name="android.permission.FOREGROUND_SERVICE" fixed="false"/>
    <permission name="android.permission.WAKE_LOCK" fixed="false"/>
    <permission name="android.permission.MODIFY_AUDIO_SETTINGS" fixed="false"/>
    <permission name="android.permission.BROADCAST_STICKY" fixed="false"/>
    <permission name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" fixed="false"/>
    <permission name="android.permission.RECEIVE_BOOT_COMPLETED" fixed="false"/>
    <permission name="android.permission.INTERACT_ACROSS_USERS" fixed="false"/>
    <permission name="android.permission.GET_TASKS" fixed="false"/>
    <permission name="android.permission.CHANGE_CONFIGURATION" fixed="false"/>
  </exception>
  <!-- 赋予app权限只需在后面添加对应的exception package和permission name即可 -->

</exceptions>

修改/device/rockchip/rk356x/device.mk

# add by mazhuang for By default, Grant app permissions 2021/08/30  
$(call inherit-product, vendor/xxxx/device.mk)

在/vendor/xxxx/device.mk中添加

# add by mazhuang for By default, Grant app permissions 2021/08/30 
PRODUCT_PACKAGES += \
    default_permissions_whitelist_xxxx

default-permissions-xxxx.xml中添加的app和权限最终在/frameworks/base/services/core/java/com/android/server/pm/permission/DefaultPermissionGrantPolicy.java中的grantDefaultPermissionExceptions()方法中赋予权限。

private void grantDefaultPermissionExceptions(PackageManagerWrapper pm, 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(pm);
            }
        }

        Set<String> permissions = null;
        final int exceptionCount = mGrantExceptions.size();
        for (int i = 0; i < exceptionCount; i++) {
            String packageName = mGrantExceptions.keyAt(i);
            PackageInfo pkg = pm.getSystemPackageInfo(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 (!pm.isPermissionDangerous(permissionGrant.name)) {
                    Log.w(TAG, "Ignoring permission " + permissionGrant.name
                            + " which isn't dangerous");
                    continue;
                }
                if (permissions == null) {
                    permissions = new ArraySet<>();
                } else {
                    permissions.clear();
                }
                permissions.add(permissionGrant.name);


                grantRuntimePermissions(pm, pkg, permissions, permissionGrant.fixed,
                        permissionGrant.whitelisted, true /*whitelistRestrictedPermissions*/,
                        userId);
            }
        }
    }

读取default-permissions-xxxx.xml的方法应该是readDefaultPermissionExceptionsLocked()

private @NonNull ArrayMap<String, List<DefaultPermissionGrant>>
            readDefaultPermissionExceptionsLocked(PackageManagerWrapper pm) {
        File[] files = getDefaultPermissionFiles();
        if (files == null) {
            return new ArrayMap<>(0);
        }

        ArrayMap<String, List<DefaultPermissionGrant>> grantExceptions = new ArrayMap<>();

        // Iterate over the files in the directory and scan .xml files
        for (File file : files) {
            if (!file.getPath().endsWith(".xml")) {
                Slog.i(TAG, "Non-xml file " + file
                        + " in " + file.getParent() + " directory, ignoring");
                continue;
            }
            if (!file.canRead()) {
                Slog.w(TAG, "Default permissions file " + file + " cannot be read");
                continue;
            }
            try (
                InputStream str = new BufferedInputStream(new FileInputStream(file))
            ) {
                XmlPullParser parser = Xml.newPullParser();
                parser.setInput(str, null);
                parse(pm, parser, grantExceptions);
            } catch (XmlPullParserException | IOException e) {
                Slog.w(TAG, "Error reading default permissions file " + file, e);
            }
        }

        return grantExceptions;
    }

最终调用到grantRuntimePermissions()方法对app赋予权限。


点击全文阅读


本文链接:http://m.zhangshiyu.com/post/27226.html

权限  赋予  授予  
<< 上一篇 下一篇 >>

  • 评论(0)
  • 赞助本站

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

最新文章

  • 贵妃每天只想当咸鱼小说(萧兮兮洛清寒)(贵妃每天只想当咸鱼)整本+后续+结局在线阅读
  • 云纾君迟玉:+后续+番外半溪明月枕清风无删减小说在线无广告高口碑小说
  • 新章速递既负如来又负卿是什么小说(莫司淮唐乐薇)完本阅读无广告(莫司淮唐乐薇)
  • 林齐(林齐)火爆小说全集免费阅读_反转传奇林齐:结局+番外无弹窗最新章节笔趣阁(林齐)
  • 萧兮兮洛清寒小说(贵妃每天只想当咸鱼)起点章节+全篇阅读热门作品预订
  • 穿成塌房顶流,我靠爆料翻红(陈昭陈莹静)_穿成塌房顶流,我靠爆料翻红
  • (番外)+(全文)谢青霄林相宜(八零奉子成婚,死对头成了妻管严:全文+结局+番外)全文免费阅读无弹窗大结局_(谢青霄林相宜)最新章节列表_笔趣阁(八零奉子成婚,死对头成了妻管严:全文+结局+番外)
  • 傅修言沈知穗小说(沈知穗傅修言)小说***下载_章节前文+后续(傅修言沈知穗)
  • 陆译林初夏小说(替身攻略失败后摆烂了)全文免费阅读_(替身攻略失败后摆烂了)陆译林初夏小说最新章节列表
  • 全书浏览说好摸鱼打游戏,你爆杀华尔街?(王文斌方幻)_说好摸鱼打游戏,你爆杀华尔街?(王文斌方幻)全书结局
  • (重生后高傲妻子我不爱了)重生后高傲妻子我不爱了(梁言彻尹暖芸)无套路无弹窗全部章节列表
  • (番外)+(全文)傅修言沈知穗:番外+全文+后续(沈知穗傅修言)完整版小说阅读_傅修言沈知穗:番外+全文+后续免费阅读_笔趣阁(沈知穗傅修言)

    关于我们 | 我要投稿 | 免责申明

    Copyright © 2020-2022 ZhangShiYu.com Rights Reserved.豫ICP备2022013469号-1