Android R(11)为自定义HIDL接口添加DM&&FCM(六)

HIDL接口定制
本文介绍如何通过DM和FCM实现自定义HIDL接口,包括在Android.bp中添加DM信息及在设备清单和兼容矩阵中配置的过程。适用于Android Framework与Vendor间代码解耦。

为自定义HIDL接口添加DM&&FCM(六)

1.概览

  引入HIDL的一个重要原因是Android团队想要将Android Framework和Android vendor之间的代码进行解耦,即franmework不依赖于vendor的代码,如此Android团队在开发franework的时候就可以屏蔽底层的硬件差异了。在面向对象编程中提倡基于接口编程其实也是这个思想,调用者并不直接调用实现类提供的接口,而是通过事先定义好的接口进行调用/实现。为了实现framework和vendor解耦,Android引入了VINTF object(Vendor Interface Object)。接下来要讲的DM(device manifest)和FCM(Framework Compatibility Matrixes)则属于VINTF object的一部分。
  VINTF object则是用于framework告知vendor其想要什么,以及vendor告知framework它有什么。下面看下它们的关系图。
在这里插入图片描述

VINTF object 的涉及主要涉及的功能如下:
  对于vendor
    a) Defines a schema for the static component.(the device manifest file).
    b) Adds build time support for defining the device manifest file for a given device.
    c) Defines the queryable API at runtime that retrieves the device manifest file (along with the other runtime-collectible information) and packages them into the query result.
  对于framework
    a) Defines a schema for the static component (the framework manifest file).
    b) Defines the queryable API at runtime that retrieves the framework manifest file and packages it into the query result.

2.设备清单-Manifests

   一个VINTF object数据由来自device manifest 和 framework manifest files 的所匹配的信息组成,例如DM中提供的和FCM所要求的组件成功匹配上的一例。
  Device Manifests由厂商维护,其中包含vendor Manifests 和 ODM Manifests两个部分。
Device Manifests
  The vendor manifest specifies HALs, SELinux policy versions, etc. common to an SoC. It is recommended to be placed in the Android source tree at device/VENDOR/DEVICE/manifest.xml, but multiple fragment files can be used.
ODM Manifests
  The ODM manifest lists HALs specific to the product in the ODM partition.

3.兼容矩阵-Compatibility Matrixes

  FCM 用于描述 framework 在运行时所需要的组件。FCM又分为SCM(system compatibilty matrix),PCM(product compatibility matrix)和 SECM(system_ext compatibility matrix)。 被列出在FCM的组件则静态的写入xml文件中,并且Android在编译、运行以及做VTS测试的时候都会检测设备能否满足FCM的要求。

4.ICustomHardware的 DM

4.1 在Android.bp中添加DM信息

//file:test/flagstaffTest/hardware/interfaces/custom_hardware/1.0/default/Android.bp
cc_binary {
    name: "flagstaff.hardware.custom_hardware@1.0-service",
    ...
    vintf_fragments: ["flagstaff.hardware.custom_hardware@1.0.xml"],
}

4.2 flagstaff.hardware.custom_hardware@1.0.xml

//file:test/flagstaffTest/hardware/interfaces/custom_hardware/1.0/default/flagstaff.hardware.custom_hardware@1.0.xml
<manifest version="1.0" type="device">
    <hal format="hidl">
        <name>flagstaff.hardware.custom_hardware</name>
        <transport>hwbinder</transport>
        <version>1.0</version>
        <interface>
            <name>ICustomHardware</name>
            <instance>default</instance>
            <instance>custom</instance>
        </interface>
    </hal>
</manifest>

  此处的DM信息说明,vendor侧提供了2个版本1.0接口ICustomHardware的实现,其分别是default和custom。在使用时可以使用如下方法选择获取对应实例

//获取default实例
android::sp<ICustomHardware> instance = ICustomHardware::getService();
//获取custom实例
android::sp<ICustomHardware> custom = ICustomHardware::getService("custom");

5.ICustomHardware的FCM

5.1 将FCM添加到Android编译系统

//file:test/flagstaffTest/device.mk
DEVICE_FRAMEWORK_COMPATIBILITY_MATRIX_FILE += \
        test/flagstaffTest/compatibility_matrix/framework_compatibility_matrix.xml

5.2 framework_compatibility_matrix.xml

//file:test/flagstaffTest/compatibility_matrix/framework_compatibility_matrix.xml
<compatibility-matrix version="2.0" type="framework">
    <hal format="hidl" optional="true">
        <name>flagstaff.hardware.custom_hardware</name>
        <version>1.0</version>
        <interface>
            <name>ICustomHardware</name>
            <instance>default</instance>
            <instance>custom</instance>
        </interface>
    </hal>
</compatibility-matrix>

  此处的FCM信息说明,framework需要版本为1.0接口ICustomHardware的两个实现,其分别是default和custom。但是此处的optional为true意味着该组件为可选,所以即使不提供对应的DM的话也不影响系统编译运行测试的。
  另外如果有问题,欢迎留言或者email(836190520@qq.com)我,技术升级在于交流~~

要实现 FCM 自定义通知横幅,你需要在客户端和服务器端分别进行以下配置: 客户端配置: 1. 在你的项目级 build.gradle 文件中添加以下依赖: ``` implementation 'com.google.firebase:firebase-messaging:22.0.0' ``` 2. 在你的应用级 build.gradle 文件中添加以下配置: ``` android { // ... defaultConfig { // ... // 设置通道 ID,用于兼容 Android 8.0 及以上版本的通知 notificationChannelId "my_channel_id" } } // 在 AndroidManifest.xml 文件中添加以下权限和服务声明 <uses-permission android:name="android.permission.INTERNET" /> <service android:name=".MyFirebaseMessagingService" android:exported="false"> <intent-filter> <action android:name="com.google.firebase.MESSAGING_EVENT" /> </intent-filter> </service> <service android:name=".MyFirebaseInstanceIDService" android:exported="false"> <intent-filter> <action android:name="com.google.firebase.INSTANCE_ID_EVENT" /> </intent-filter> </service> ``` 3. 创建一个继承自 FirebaseMessagingService 的服务类,并重写 onMessageReceived 方法,用于处理接收到的消息,如下所示: ``` public class MyFirebaseMessagingService extends FirebaseMessagingService { private static final String TAG = "MyFirebaseMessagingService"; @Override public void onMessageReceived(RemoteMessage remoteMessage) { Log.d(TAG, "From: " + remoteMessage.getFrom()); // Check if message contains a notification payload. if (remoteMessage.getNotification() != null) { Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody()); // 发送自定义横幅通知 sendCustomNotification(remoteMessage); } } private void sendCustomNotification(RemoteMessage remoteMessage) { // 创建一个 NotificationCompat.Builder 对象 NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "my_channel_id") .setSmallIcon(R.drawable.notification_icon) .setContentTitle(remoteMessage.getNotification().getTitle()) .setContentText(remoteMessage.getNotification().getBody()) .setPriority(NotificationCompat.PRIORITY_HIGH) .setAutoCancel(true); // 添加自定义横幅样式 NotificationCompat.BigTextStyle bigTextStyle = new NotificationCompat.BigTextStyle() .setBigContentTitle(remoteMessage.getNotification().getTitle()) .bigText(remoteMessage.getNotification().getBody()); builder.setStyle(bigTextStyle); // 显示通知 NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this); notificationManager.notify(1, builder.build()); } } ``` 服务器端配置: 1. 使用 Firebase 控制台创建一个新的 Firebase 项目,并在项目设置中获取到项目的 Server key。 2. 使用项目的 Server key 发送消息到 FCM 接口,消息格式如下: ``` { "to": "设备的 FCM token", "notification": { "title": "通知标题", "body": "通知内容" }, "data": { // 自定义数据 } } ``` 这样,当你的应用接收到消息时,就会发送一个自定义横幅通知。需要注意的是,自定义横幅样式只会在 Android 5.0 及以上版本的设备上生效。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值