android automotive permission

  1. 1. 添加自定义权限的定义
  2. 2. 为添加的自定义属性添加读写权限
  3. 3. 在 service 的 manifest 文件中定义权限
  4. 4. 编译 CarService
  5. 验证

1. 添加自定义权限的定义

packages/services/Car/car-lib/src/android/car/Car.java

1
2
3
4
5
6
7
8
9
10
11
12
diff --git a/car-lib/src/android/car/Car.java b/car-lib/src/android/car/Car.java
index 12955ee..c094d54 100644
--- a/car-lib/src/android/car/Car.java
+++ b/car-lib/src/android/car/Car.java
@@ -1139,6 +1139,13 @@ public final class Car {
public static final String PERMISSION_MANAGE_THREAD_PRIORITY = "android.car.permission.MANAGE_THREAD_PRIORITY";
/**
+ * Custom Permission.
+ * @hide
+ */
@AddedInOrBefore(majorVersion = 33)
public static final String PERMISSION_CAR_CUSTOM = "android.car.permission.CAR_CUSTOM";

2. 为添加的自定义属性添加读写权限

packages/services/Car/car-lib/src/android/car/VehiclePropertyIds.java

1
2
3
4
5
6
7
8
9
10
11
diff --git a/car-lib/src/android/car/VehiclePropertyIds.java b/car-lib/src/android/car/VehiclePropertyIds.java
index 1531ff1..f8966f5 100644
--- a/car-lib/src/android/car/VehiclePropertyIds.java
+++ b/car-lib/src/android/car/VehiclePropertyIds.java
@@ -2229,6 +2229,15 @@ public final class VehiclePropertyIds {
@AddedInOrBefore(majorVersion = 33)
public static final int VEHICLE_CURB_WEIGHT = 289410886;

+ @RequiresPermission(Car.PERMISSION_CAR_CUSTOM)
+ @AddedInOrBefore(majorVersion = 33)
public static final int VEHICLE_TEST_PROPERTY = 289410896;

packages/services/Car/car-lib/src/com/android/car/internal/PropertyPermissionMapping.java

1
2
3
4
5
@@ -34,6 +34,9 @@ public final class PropertyPermissionMapping {
public PropertyPermissionMapping() {
// Add propertyId and read/write permissions
// Cabin Properties
+ map(Car.PERMISSION_CAR_CUSTOM, VehiclePropertyIds.VEHICLE_TEST_PROPERTY);

packages/services/Car/service/src/com/android/car/hal/PropertyHalServiceIds.java

1
2
3
4
5
6
7
8
9
--- a/service/src/com/android/car/hal/PropertyHalServiceIds.java
+++ b/service/src/com/android/car/hal/PropertyHalServiceIds.java
@@ -184,6 +184,12 @@ public class PropertyHalServiceIds {
mPropToValidBitFlag = new HashMap<>();
// Add propertyId and read/write permissions
// Cabin Properties
+ mProps.put(VehicleProperty.VEHICLE_TEST_PROPERTY, new Pair<>(
+ Car.PERMISSION_CAR_CUSTOM,
+ Car.PERMISSION_CAR_CUSTOM));

3. 在 service 的 manifest 文件中定义权限

packages/services/Car/service/AndroidManifest.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
--git a/service/AndroidManifest.xml b/service/AndroidManifest.xml
index 1752eab..fe48906 100644
--- a/service/AndroidManifest.xml
+++ b/service/AndroidManifest.xml
@@ -915,6 +915,11@@

android:label="@string/car_permission_label_manage_thread_priority"
android:description="@string/car_permission_desc_manage_thread_priority"/>

+ <permission android:name="android.car.permission.CAR_CUSTOM"
+ android:protectionLevel="signature|privileged"
+ android:label="@string/car_permission_label_custom"
+ android:description="@string/car_permission_desc_custom"/>
+

4. 编译 CarService

回到根目录 mmm packages/services/Car/
这个时候编译会报错,需要执行以下命令生成新的 API 签名

m android.car-stubs-docs-update-current-api
m android.car-system-stubs-docs-update-current-api
m android.car-test-stubs-docs-update-current-api

执行之后会在 current.txt 文件中新增内容(不需要手动修改)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
--git a/car-lib-module/api/current.txt b/car-lib-module/api/current.txt
index 05464e4..c39a981 100644
--- a/car-lib-module/api/current.txt
+++ b/car-lib-module/api/current.txt
@@ -329,6 +329,8 @@ package android.car {
field @RequiresPermission.Read(@androidx.annotation.RequiresPermission(android.car.Car.PERMISSION_ENERGY))

public static final int EV_REGENERATIVE_BRAKING_STATE = 289410884;
// 0x11400f44
field @RequiresPermission("android.car.permission.CAR_EXTERIOR_LIGHTS")
public static final int FOG_LIGHTS_STATE = 289410562;
// 0x11400e02
field @RequiresPermission("android.car.permission.CONTROL_CAR_EXTERIOR_LIGHTS")
public static final int FOG_LIGHTS_SWITCH = 289410578;
// 0x11400F50
+ field @RequiresPermission("android.car.permission.CAR_CUSTOM")
public static final int VEHICLE_TEST_PROPERTY = 289410896;
// 0x11200f48
field @RequiresPermission("android.car.permission.CAR_EXTERIOR_LIGHTS")
public static final int FRONT_FOG_LIGHTS_STATE = 289410875;
// 0x11400f3b
field @RequiresPermission("android.car.permission.CONTROL_CAR_EXTERIOR_LIGHTS")
public static final int FRONT_FOG_LIGHTS_SWITCH = 289410876;
// 0x11400f3c
--git a/car-lib/api/current.txt b/car-lib/api/current.txt
index 05464e4..c39a981 100644
--- a/car-lib/api/current.txt
+++ b/car-lib/api/current.txt
@@ -329,6 +329,8 @@ package android.car {
field @RequiresPermission.Read(@androidx.annotation.RequiresPermission(android.car.Car.PERMISSION_ENERGY)) public static final int EV_REGENERATIVE_BRAKING_STATE = 289410884;
// 0x11400f44
field @RequiresPermission("android.car.permission.CAR_EXTERIOR_LIGHTS") public static final int FOG_LIGHTS_STATE = 289410562;
// 0x11400e02
field @RequiresPermission("android.car.permission.CONTROL_CAR_EXTERIOR_LIGHTS") public static final int FOG_LIGHTS_SWITCH = 289410578;
// 0x11400e12
+field @RequiresPermission("android.car.permission.CAR_CUSTOM") public static final int VEHICLE_TEST_PROPERTY = 289410896; // 0x11400F50

验证

回到项目根目录,使用 m 命令重新编译整个项目。
使用 make emu_img_zip 重新编译模拟器镜像。
编译好的镜像文件在 out/target/product/emulator_car_x86_64/sdk-repo-linux-system-images-eng.$(whoami).zip。
将生成的 zip 文件复制出来,解压之后替换本地 SDK 中的镜像~/Library/Android/sdk/system-images/android-33/android-automotive/x86_64,然后把模拟器 wipe data 并重新启动模拟器。


转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以在下面评论区评论,也可以邮件至 nathanwriting@126.com

文章标题:android automotive permission

字数:840

本文作者:Nathaniel

发布时间:2024-01-10, 21:57:50

最后更新:2024-01-10, 22:08:14

原始链接:http://example.com/2024/01/10/android-automotive-permission/

版权声明: "署名-非商用-相同方式共享 4.0" 转载请保留原文链接及作者。

×

喜欢就点赞,疼爱就打赏