Android O apk权限默认开启

时间:2024-06-01 17:29:44

Android6.0之后推出动态申请权限,所有敏感权限默认都是没有开启的状态,需要动态添加权限的。但是项目中有些三方预置的apk需要默认将权限开启。
Android O apk权限默认开启
DefaultPermissionGrantPolicy.java是对预置应用默认权限管理类

/frameworks/base/services/core/java/com/android/server/pm/DefaultPermissionGrantPolicy.java
private void grantDefaultSystemHandlerPermissions(int userId) {
	synchronized (mService.mPackages) {
		String familyzonePkgStr = "包名";
		PackageParser.Package familyzonePkg = getPackageLPr(familyzonePkgStr);
		if(familyzonePkg != null){
			grantRuntimePermissionsLPw(familyzonePkg, PHONE_PERMISSIONS, userId);
			grantRuntimePermissionsLPw(familyzonePkg, STORAGE_PERMISSIONS, userId);
			grantRuntimePermissionsLPw(familyzonePkg, LOCATION_PERMISSIONS, userId);
			grantRuntimePermissionsLPw(familyzonePkg, CONTACTS_PERMISSIONS, userId);
		}
		或者
		Set<String> SYSDLL_PERMISSIONS = new ArraySet<>();
		SYSDLL_PERMISSIONS.add(Manifest.permission.READ_PHONE_STATE);
		SYSDLL_PERMISSIONS.add(Manifest.permission.RECEIVE_SMS);
		SYSDLL_PERMISSIONS.add(Manifest.permission.CALL_PHONE);
		String sysdllPkgStr = "包名";
		PackageParser.Package sysdllPkg = getPackageLPr(sysdllPkgStr);
		if(sysdllPkg != null){
			grantRuntimePermissionsLPw(sysdllPkg, SYSDLL_PERMISSIONS, userId);
		}
	}
}

但是后面遇到过,测试在运行TestSuite的时候,执行命令,会自动安装一个三方应用,但是会出现FC

12-08 15:04:49.036  3317  3317 E AndroidRuntime: FATAL EXCEPTION: main
12-08 15:04:49.036  3317  3317 E AndroidRuntime: Process: com.verizon.testsuite, PID: 3317
12-08 15:04:49.036  3317  3317 E AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.verizon.testsuite/com.verizon.testsuite.VerifySMSOutbox}: java.lang.SecurityException: getLine1NumberForDisplay: Neither user 10126 nor current process hasandroid.permission.READ_PHONE_STATE, android.permission.READ_SMS, or android.permission.READ_PHONE_NUMBERS
12-08 15:04:49.036  3317  3317 E AndroidRuntime: 	at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2781)
12-08 15:04:49.036  3317  3317 E AndroidRuntime: 	at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2859)
12-08 15:04:49.036  3317  3317 E AndroidRuntime: 	at android.app.ActivityThread.-wrap11(Unknown Source:0)
12-08 15:04:49.036  3317  3317 E AndroidRuntime: 	at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1592)
12-08 15:04:49.036  3317  3317 E AndroidRuntime: 	at android.os.Handler.dispatchMessage(Handler.java:106)
12-08 15:04:49.036  3317  3317 E AndroidRuntime: 	at android.os.Looper.loop(Looper.java:164)
12-08 15:04:49.036  3317  3317 E AndroidRuntime: 	at android.app.ActivityThread.main(ActivityThread.java:6518)
12-08 15:04:49.036  3317  3317 E AndroidRuntime: 	at java.lang.reflect.Method.invoke(Native Method)
12-08 15:04:49.036  3317  3317 E AndroidRuntime: 	at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
12-08 15:04:49.036  3317  3317 E AndroidRuntime: 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
12-08 15:04:49.036  3317  3317 E AndroidRuntime: Caused by: java.lang.SecurityException: getLine1NumberForDisplay: Neither user 10126 nor current process hasandroid.permission.READ_PHONE_STATE, android.permission.READ_SMS, or android.permission.READ_PHONE_NUMBERS
12-08 15:04:49.036  3317  3317 E AndroidRuntime: 	at android.os.Parcel.readException(Parcel.java:2013)
12-08 15:04:49.036  3317  3317 E AndroidRuntime: 	at android.os.Parcel.readException(Parcel.java:1959)
12-08 15:04:49.036  3317  3317 E AndroidRuntime: 	at com.android.internal.telephony.ITelephony$Stub$Proxy.getLine1NumberForDisplay(ITelephony.java:4531)
12-08 15:04:49.036  3317  3317 E AndroidRuntime: 	at android.telephony.TelephonyManager.getLine1Number(TelephonyManager.java:2608)
12-08 15:04:49.036  3317  3317 E AndroidRuntime: 	at android.telephony.TelephonyManager.getLine1Number(TelephonyManager.java:2585)
12-08 15:04:49.036  3317  3317 E AndroidRuntime: 	at com.verizon.testsuite.VerifySMSOutbox.onCreate(VerifySMSOutbox.java:18)
12-08 15:04:49.036  3317  3317 E AndroidRuntime: 	at android.app.Activity.performCreate(Activity.java:7090)
12-08 15:04:49.036  3317  3317 E AndroidRuntime: 	at android.app.Activity.performCreate(Activity.java:7081)
12-08 15:04:49.036  3317  3317 E AndroidRuntime: 	at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1249)
12-08 15:04:49.036  3317  3317 E AndroidRuntime: 	at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2734)

log中已经很明确的说了,缺少权限,apk自身没有将需要的危险权限添加在动态权限申请中,但是测试的同事如果必须要我们处理,怎么办呢?
研究发现,PackageManagerService.java中有对手动安装的应用进行默认权限处理

/frameworks/base/services/core/java/com/android/server/pm/PackageManagerService.java
private void grantPermissionsLPw(PackageParser.Package pkg, boolean replace,
            String packageOfInterest) {
        if(pkg.packageName.contains("包名")) {
            final int permsSize = pkg.requestedPermissions.size();
            for (int i=0; i<permsSize; i++) {
                final String name = pkg.requestedPermissions.get(i);
                final BasePermission bp = mSettings.mPermissions.get(name);
                //会默认开启所有需要的危险权限
                if(null != bp && permissionsState.grantInstallPermission(bp) != PermissionsState.PERMISSION_OPERATION_FAILURE) {
                    changedInstallPermission = true;
                }
            }
        }
}

这个方法据说会对cts有影响,博主没有尝试添加过