Android 修改源码,默认授予第三方APP权限,不用动态授权

53 篇文章 2 订阅
2 篇文章 0 订阅

前言

android 6.0 开始引入动态权限机制,某些敏感权限需要用户手动同意后才可以使用。
作为系统APP ,无需动态申请,声明后直接使用即可。
如果第三方APP需要默认拥有某些权限,无需动态申请,该怎么做呢?
两种方法:
1.第三方APP打系统签名;
2.修改源码,默认授予第三方APP权限。

修改源码

基于AN 8.0 ,默认权限的管理在 frameworks/base/services/core/java/com/android/server/pm/DefaultPermissionGrantPolicy.java 类,首次开机会执行,我们关注grantDefaultSystemHandlerPermissions(int userId) 函数,

synchronized (mService.mPackages) {
            // Installer
            PackageParser.Package installerPackage = getSystemPackageLPr(
                    mService.mRequiredInstallerPackage);
            if (installerPackage != null
                    && doesPackageSupportRuntimePermissions(installerPackage)) {
                grantRuntimePermissionsLPw(installerPackage, STORAGE_PERMISSIONS, true, userId);
            }

            // Verifier
            PackageParser.Package verifierPackage = getSystemPackageLPr(
                    mService.mRequiredVerifierPackage);
            if (verifierPackage != null
                    && doesPackageSupportRuntimePermissions(verifierPackage)) {
                grantRuntimePermissionsLPw(verifierPackage, STORAGE_PERMISSIONS, true, userId);
                grantRuntimePermissionsLPw(verifierPackage, PHONE_PERMISSIONS, false, userId);
                grantRuntimePermissionsLPw(verifierPackage, SMS_PERMISSIONS, false, userId);
            }

            // SetupWizard
            PackageParser.Package setupPackage = getSystemPackageLPr(
                    mService.mSetupWizardPackage);
            if (setupPackage != null
                    && doesPackageSupportRuntimePermissions(setupPackage)) {
                grantRuntimePermissionsLPw(setupPackage, PHONE_PERMISSIONS, userId);
                grantRuntimePermissionsLPw(setupPackage, CONTACTS_PERMISSIONS, userId);
                grantRuntimePermissionsLPw(setupPackage, LOCATION_PERMISSIONS, userId);
                grantRuntimePermissionsLPw(setupPackage, CAMERA_PERMISSIONS, userId);
            }

            // Camera
            Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            PackageParser.Package cameraPackage = getDefaultSystemHandlerActivityPackageLPr(
                    cameraIntent, userId);
            if (cameraPackage != null
                    && doesPackageSupportRuntimePermissions(cameraPackage)) {
                grantRuntimePermissionsLPw(cameraPackage, CAMERA_PERMISSIONS, userId);
                grantRuntimePermissionsLPw(cameraPackage, MICROPHONE_PERMISSIONS, userId);
                grantRuntimePermissionsLPw(cameraPackage, STORAGE_PERMISSIONS, userId);
            }
			
			//省略部分代码
			
            mService.mSettings.onDefaultRuntimePermissionsGrantedLPr(userId);
        }

以 Camera 为例,通过 grantRuntimePermissionsLPw 函数授予其相关的权限,权限的定义为

	private static final Set<String> MICROPHONE_PERMISSIONS = new ArraySet<>();
    static {
        MICROPHONE_PERMISSIONS.add(Manifest.permission.RECORD_AUDIO);
    }

    private static final Set<String> CAMERA_PERMISSIONS = new ArraySet<>();
    static {
        CAMERA_PERMISSIONS.add(Manifest.permission.CAMERA);
    }
    
    private static final Set<String> STORAGE_PERMISSIONS = new ArraySet<>();
    static {
        STORAGE_PERMISSIONS.add(Manifest.permission.READ_EXTERNAL_STORAGE);
        STORAGE_PERMISSIONS.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
    }

如此,参考 Camera ,给第三方应用授予相关权限:

private static final String MYAPPE_PKG_NAME= "com.myapp.test";

// test app
PackageParser.Package myAppPackage = getPackageLPr(MYAPPE_PKG_NAME);
	if(null != fxHomePackage
		&& doesPackageSupportRuntimePermissions(MYAPPE_PKG_NAME)){
			grantRuntimePermissionsLPw(myAppPackage , CAMERA_PERMISSIONS, userId);
			grantRuntimePermissionsLPw(myAppPackage , CONTACTS_PERMISSIONS, userId);
			grantRuntimePermissionsLPw(myAppPackage , LOCATION_PERMISSIONS, userId);
			grantRuntimePermissionsLPw(myAppPackage , MICROPHONE_PERMISSIONS, userId);
			grantRuntimePermissionsLPw(myAppPackage , STORAGE_PERMISSIONS, userId);
			Log.d(TAG, "Granting permissions to package com.bestv.ott");
	}

烧录后查看 /data/system/users/0/runtime-permissions.xml 文件,
通过包名查找 <pkg name="com.myapp.test"> 应用的权限授予情况,
当然,代码逻辑及功能检查更稳妥。

参考:https://blog.csdn.net/fmc088/article/details/81050520

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值