优秀案例

Android Automotive navigation(谷

2019-06-03  本文已影响69人  PenguinMan

前言

随着时代的发展,科技的进步,越来越多的车载系统添加了对液晶仪表系统的支持。不同的车载主机厂商与不同的仪表提供厂商之间对中控仪表互联的协议也不尽相同,由于大家对传输协议都有各自的理解,所以目前市场上出现了多种中控仪与导航应用的互联协议,比较常见的做法是中控系统与导航应用及仪表系统定制AIDL/Broadcast传输协议。


meter.png

但由于这一协议只是使用三方协定的,更换设备提供商之后还需要重新协定,无法复用。谷歌也正式看出了市场上目前存在问题,所以在这一次的Automotive Library中新增了navigation模块,专门解决导航与仪表之间的信息交互。


google_navigation.png

源代码分析

在navigation下有两个文件,一个是CarNavigationInstrumentCluster数据载体,一个是控制中心CarNavigationStatusManager。


目录结构.png

我们先通过一个表格来展现一下CarNavigationInstrumentCluster这个数据载体中所包含的内容。

序号 类型 定义 意义
1 int mMinIntervalMillis 仪表信息最小更新时间
2 @IntDef mType 转向标取值类型
3 int mImageWidth 图片的像素宽度
4 int mImageHeight 图片的像素高度
5 int mImageColorDepthBits 图片的素材度(8,16或者32)
6 Bundle mExtra 额外信息

转向标取值类型是通过Android中提供的注解代替枚举,它的取值一共有两个CLUSTER_TYPE_CUSTOM_IMAGES_SUPPORTED 和 CLUSTER_TYPE_IMAGE_CODES_ONLY分别代表导航转向标信息,可以是图片或枚举和只能是枚举类型的转向标信息。CarNavigationInstrumentCluster的初始化有两种方式:

    /**
     * 创建只支持枚举类型
     * */
    public static CarNavigationInstrumentCluster createCluster(int minIntervalMillis) {
        return new CarNavigationInstrumentCluster(minIntervalMillis, CLUSTER_TYPE_IMAGE_CODES_ONLY,
                0, 0, 0);
    }

    /**
     * 创建支持枚举与图片类型
     * */
    public static CarNavigationInstrumentCluster createCustomImageCluster(int minIntervalMs,
            int imageWidth, int imageHeight, int imageColorDepthBits) {
        return new CarNavigationInstrumentCluster(minIntervalMs,
                CLUSTER_TYPE_CUSTOM_IMAGES_SUPPORTED,
                imageWidth, imageHeight, imageColorDepthBits);
    }

接下来我们来看一下navigation模块的控制中心CarNavigationStatusManager,在CarNavigationStatusManager中有两个主要的方法sendEvent和getInstrumentClusterInfo即数据的发送和接收,我们首先来看一下数据发送:

    private final IInstrumentClusterNavigation mService;
    /**
     * Only for CarServiceLoader
     * @hide
     */
    public CarNavigationStatusManager(IBinder service) {
        mService = IInstrumentClusterNavigation.Stub.asInterface(service);
    }
    /**
     * Sends events from navigation app to instrument cluster.
     * 导航向仪表发送信息
     * <p>The event type and bundle can be populated by
     * {@link android.support.car.navigation.CarNavigationStatusEvent}.
     *
     * @param eventType event type 自定义的事件类型
     * @param bundle object that holds data about the event 自定义事件类型携带的数据
     * @throws CarNotConnectedException if the connection to the car service has been lost.抛出    
     *  车辆未连接异常
     */
    public void sendEvent(int eventType, Bundle bundle) throws CarNotConnectedException {
        try {
            mService.onEvent(eventType, bundle);
        } catch (IllegalStateException e) {
            CarApiUtil.checkCarNotConnectedExceptionFromCarService(e);
        } catch (RemoteException e) {
            handleCarServiceRemoteExceptionAndThrow(e);
        }
    }

在车辆连接状态情况下,调用sendEvent函数会触发IInstrumentClusterNavigation这个AIDL的onEvent,通知仪表导航数据已发生更新,此时仪表可以通过getInstrumentClusterInfo获取CarNavigationInstrumentCluster系统定义的转向类型信息,也可以通过eventType获取导航应用的自定义信息Bundle 。

package com.yanghaoyi.aosp.car.cluster.renderer;
import com.yanghaoyi.aosp.car.navigation.CarNavigationInstrumentCluster;
import android.graphics.Bitmap;
import android.os.Bundle;

interface IInstrumentClusterNavigation {
    void onEvent(int eventType, in Bundle bundle);
    CarNavigationInstrumentCluster getInstrumentClusterInfo();
}

那么导航应用是怎样实现信息流的发送呢?通过对AOSP(Android Open Source Project)的查阅,我们找到了CarNavigationStatusManager的初始化方法,在Car.Java文件中有一个getCarManager方法,这个方法的作用就是通过服务的名称获取对应的Manager,例如我们之前提到的CarNavigationStatusManager,我们就可以通过定义的"car_navigation_service"在Car中获取到CarNavigationStatusManager对象。

    /** Service name for {@link CarNavigationStatusManager} */
    public static final String CAR_NAVIGATION_SERVICE = "car_navigation_service";

    /**
     * Get car specific service as in {@link Context#getSystemService(String)}. Returned
     * {@link Object} should be type-casted to the desired service.
     * For example, to get sensor service,
     * SensorManagerService sensorManagerService = car.getCarManager(Car.SENSOR_SERVICE);
     * @param serviceName Name of service that should be created like {@link #SENSOR_SERVICE}.
     * @return Matching service manager or null if there is no such service.
     * @throws CarNotConnectedException if the connection to the car service has been lost.
     */
    public Object getCarManager(String serviceName) throws CarNotConnectedException {
        CarManagerBase manager;
        ICar service = getICarOrThrow();
        synchronized (mCarManagerLock) {
            manager = mServiceMap.get(serviceName);
            if (manager == null) {
                try {
                    IBinder binder = service.getCarService(serviceName);
                    if (binder == null) {
                        Log.w(CarLibLog.TAG_CAR, "getCarManager could not get binder for service:" +
                                serviceName);
                        return null;
                    }
                    manager = createCarManager(serviceName, binder);
                    if (manager == null) {
                        Log.w(CarLibLog.TAG_CAR,
                                "getCarManager could not create manager for service:" +
                                        serviceName);
                        return null;
                    }
                    mServiceMap.put(serviceName, manager);
                } catch (RemoteException e) {
                    handleRemoteException(e);
                }
            }
        }
        return manager;
    }
  
  //通过服务名创建manager实例
    private CarManagerBase createCarManager(String serviceName, IBinder binder)
            throws CarNotConnectedException {
        CarManagerBase manager = null;
        switch (serviceName) {
            case CAR_NAVIGATION_SERVICE:
                manager = new CarNavigationStatusManager(binder);
                break;
          //...省略其他与示例无关的case
            default:
                break;
        }
        return manager;
    }

信息流发送实例(出自谷歌/car/kitchensink/cluster目录下的InstrumentClusterFragment):

    private CarNavigationStatusManager mCarNavigationStatusManager;
    
        private final CarConnectionCallback mCarConnectionCallback = new CarConnectionCallback() {
            @Override
            public void onConnected(Car car) {
                Log.d(TAG, "Connected to Car Service");
                try {
              //车辆连接情况下,获取CarNavigationStatusManager对象
                    mCarNavigationStatusManager =
                            (CarNavigationStatusManager) mCarApi.getCarManager(CAR_NAVIGATION_SERVICE);
                    mCarAppFocusManager = (CarAppFocusManager) mCarApi.getCarManager(APP_FOCUS_SERVICE);
                } catch (CarNotConnectedException e) {
                    Log.e(TAG, "Car is not connected!", e);
                }
            }

            @Override
            public void onDisconnected(Car car) {
            Log.d(TAG, "Disconnect from Car Service");
        }
    };
    
  //发送导航信息流
    private void sendTurn() {
        // TODO(deanh): Make this actually meaningful.
        Bundle bundle = new Bundle();
        bundle.putString("someName", "someValue time=" + System.currentTimeMillis());
        try {
            mCarNavigationStatusManager.sendEvent(1, bundle);
        } catch(CarNotConnectedException e) {
            Log.e(TAG, "Failed to send turn information.", e);
        }
    }

看到这里你可能会产生一个疑问,导航信息流所发送的数据并没有包含我们一开始提到的CarNavigationInstrumentCluster,那么CarNavigationInstrumentCluster又是在哪里赋值并传递的呢?让我们继续深入源码。通过对CarNavigationInstrumentCluster createCluster的追溯,我们发现在android/car/cluster/sample目录下的SampleClusterServiceImpl类中有对createCluster的调用:

    //初始化CarNavigationInstrumentCluster仪表转向标信息
    @Override
    protected NavigationRenderer getNavigationRenderer() {
        NavigationRenderer navigationRenderer = new NavigationRenderer() {
            @Override
            public CarNavigationInstrumentCluster getNavigationProperties() {
                Log.i(TAG, "getNavigationProperties");
                //创建只支持枚举类型的仪表信息 更新时间1000毫秒
                CarNavigationInstrumentCluster config =
                        CarNavigationInstrumentCluster.createCluster(1000);
                Log.i(TAG, "getNavigationProperties, returns: " + config);
                return config;
            }

            @Override
            public void onEvent(int eventType, Bundle bundle) {
                StringBuilder bundleSummary = new StringBuilder();
                for (String key : bundle.keySet()) {
                    bundleSummary.append(key);
                    bundleSummary.append("=");
                    bundleSummary.append(bundle.get(key));
                    bundleSummary.append(" ");
                }
                Log.i(TAG, "onEvent(" + eventType + ", " + bundleSummary + ")");
            }
        };

        Log.i(TAG, "createNavigationRenderer, returns: " + navigationRenderer);
        return navigationRenderer;
    }

SampleClusterServiceImpl是客户端的实现,它继承自car/cluster/renderer目录下的InstrumentClusterRenderingService,谷歌对这个类的解释是用于导航与仪表之间的通信服务,CarService可以为导航应用提供内部绑定接口。导航应用实现与中控系统的通信还需要一个绑定操作,是通过AIDL文件IInstrumentCluster的setNavigationContextOwner来实现的,通过设置应用唯一标识UID与进程PID建立供应关系。

interface IInstrumentCluster {
    /** Returns {@link IInstrumentClusterNavigation} that will be passed to the Nav app
     * 返回IInstrumentClusterNavigation 将会被传递给导航应用
      * */
    IInstrumentClusterNavigation getNavigationService();

    /**
    * Supplies Instrument Cluster Renderer with current owner of Navigation app context
    * 为仪表渲染提供支持的导航应用
    * @param uid 应用唯一标识
    * @param pid 进程ID
    * */
    oneway void setNavigationContextOwner(int uid, int pid);

    /** Called when key event that was addressed to instrument cluster display has been received. */
    oneway void onKeyEvent(in KeyEvent keyEvent);
}

总结

最后,我们来梳理一下android automotive navigation帮助我们实现的传输协议,首先导航应用需要通过继承InstrumentClusterRenderingService实现一个自己的仪表渲染服务,在这个服务中,导航应用会通过UID与PID向IInstrumentCluster 绑定与仪表的通信关系。在服务的onBind方法中,设置与仪表通信数据的刷新时间,与导航信息流事件。导航也可以使用自定义事件sendEvent向仪表发送自定义信息流数据。仪表通过IInstrumentClusterNavigation的getInstrumentClusterInfo,onEvent获取方向信息数据与导航自定义数据,当然navigation中的实现远不止这么简单,automotive通过CarConnectionCallback帮助我们实现了对车辆连接状态的监听,以及通过Car.Java实现对多个服务控制中心的初始化操作,以及对渲染操作的线程保护等等。


传输示意图 .png

代码

由于谷歌服务是外网环境,为了方便阅读,笔者将文中所提及相关AOSP源代码制作成了模块,已经上传至了GitHub,有兴趣的朋友可以从Github上下载以便对文章的理解。
GitHub代码连接

source_github.png
上一篇 下一篇

猜你喜欢

热点阅读