CarService 中运行逻辑
在Android O中,Google在原有的Framework架构中引入了Android Car的功能结构,这里着重对Android Service模块进行相关的讲解。由于Car模块 Android O和P延续统一体系,这里就按照P的源码,O/P架构进行解析。Car的相关架构,请参照Android Automotive架构,不再做多的赘述、
Car service
CarService集中在一个App中。这个App需要非常高的权限,所以这是一个系统App。其Manifest开头如下:
android:sharedUserId属性使得这个应用具有系统权限。
CarService.java的onCreate()方法会new一个ICarImpl,并对一系列的服务进行初始化。
CarService并非一个服务,而是一系列的服务。这些服务都在ICarImpl.java构造函数中列了出来。
首先是CarService中onCreate()中关键代码:
mICarImpl = new ICarImpl(this,
mVehicle,
SystemInterface.Builder.defaultSystemInterface(this).build(),
mCanBusErrorNotifier,
mVehicleInterfaceName);
mICarImpl.init();
再者是ICarImpl类中的构造函数:
public ICarImpl(Context serviceContext, IVehicle vehicle, SystemInterface systemInterface,
CanBusErrorNotifier errorNotifier, String vehicleInterfaceName) {
mContext = serviceContext;
mSystemInterface = systemInterface;
mHal = new VehicleHal(vehicle);
mVehicleInterfaceName = vehicleInterfaceName;
mSystemActivityMonitoringService = new SystemActivityMonitoringService(serviceContext);
mCarPowerManagementService = new CarPowerManagementService(mContext, mHal.getPowerHal(),
systemInterface);
mCarPropertyService = new CarPropertyService(serviceContext, mHal.getPropertyHal());
mCarDrivingStateService = new CarDrivingStateService(serviceContext, mCarPropertyService);
mCarUXRestrictionsService = new CarUxRestrictionsManagerService(serviceContext,
mCarDrivingStateService, mCarPropertyService);
mCarPackageManagerService = new CarPackageManagerService(serviceContext,
mCarUXRestrictionsService,
mSystemActivityMonitoringService);
mCarInputService = new CarInputService(serviceContext, mHal.getInputHal());
mCarProjectionService = new CarProjectionService(serviceContext, mCarInputService);
mGarageModeService = new GarageModeService(mContext, mCarPowerManagementService);
mAppFocusService = new AppFocusService(serviceContext, mSystemActivityMonitoringService);
mCarAudioService = new CarAudioService(serviceContext);
mCarNightService = new CarNightService(serviceContext, mCarPropertyService);
mInstrumentClusterService = new InstrumentClusterService(serviceContext,
mAppFocusService, mCarInputService);
mSystemStateControllerService = new SystemStateControllerService(serviceContext,
mCarPowerManagementService, mCarAudioService, this);
mPerUserCarServiceHelper = new PerUserCarServiceHelper(serviceContext);
mCarBluetoothService = new CarBluetoothService(serviceContext, mCarPropertyService,
mPerUserCarServiceHelper, mCarUXRestrictionsService);
mVmsSubscriberService = new VmsSubscriberService(serviceContext, mHal.getVmsHal());
mVmsPublisherService = new VmsPublisherService(serviceContext, mHal.getVmsHal());
mCarDiagnosticService = new CarDiagnosticService(serviceContext, mHal.getDiagnosticHal());
mCarStorageMonitoringService = new CarStorageMonitoringService(serviceContext,
systemInterface);
mCarConfigurationService =
new CarConfigurationService(serviceContext, new JsonReaderImpl());
mUserManagerHelper = new CarUserManagerHelper(serviceContext);
mCarLocationService = new CarLocationService(mContext, mCarPowerManagementService,
mCarPropertyService, mUserManagerHelper);
// Be careful with order. Service depending on other service should be inited later.
List allServices = new ArrayList<>();
allServices.add(mSystemActivityMonitoringService);
allServices.add(mCarPowerManagementService);
allServices.add(mCarPropertyService);
allServices.add(mCarDrivingStateService);
allServices.add(mCarUXRestrictionsService);
allServices.add(mCarPackageManagerService);
allServices.add(mCarInputService);
allServices.add(mGarageModeService);
allServices.add(mAppFocusService);
allServices.add(mCarAudioService);
allServices.add(mCarNightService);
allServices.add(mInstrumentClusterService);
allServices.add(mCarProjectionService);
allServices.add(mSystemStateControllerService);
allServices.add(mCarBluetoothService);
allServices.add(mCarDiagnosticService);
allServices.add(mPerUserCarServiceHelper);
allServices.add(mCarStorageMonitoringService);
allServices.add(mCarConfigurationService);
allServices.add(mVmsSubscriberService);
allServices.add(mVmsPublisherService);
if (mUserManagerHelper.isHeadlessSystemUser()) {
mCarUserService = new CarUserService(serviceContext, mUserManagerHelper);
allServices.add(mCarUserService);
}
allServices.add(mCarLocationService);
mAllServices = allServices.toArray(new CarServiceBase[allServices.size()]);
}
再是ICarImpl中init()方法:
void init() {
traceBegin("VehicleHal.init");
mHal.init();
traceEnd();
traceBegin("CarService.initAllServices");
for (CarServiceBase service : mAllServices) {
service.init();
}
traceEnd();
}
阅读源码会发现,CarService的onCreate()方法new完一个ICarImpl对象会执行mICarImpl.init()方法初始化。
首先对VehicleHal执行了init(),先是调用HalClient.getAllPropConfigs()从Vehicle HAL获取所有的属性,然后对各个service.takeSupportedProperties(properties)获取CarService支持的属性。
接着又对各个继承了CarServiceBase的Service执行service.init()方法。
比如PropertyHalServiceBase继承了CarServiceBase,在init()方法里面对各个属性执行了mVehicleHal.subscribeProperty(this, prop),然后调用了VehicleHal.java的subscribeProperty(HalServiceBase service, int property),接着调用HalClient.subscribe(SubscribeOptions… options),这样就把所有属性都subscribe了,这样CarService就能接收到Vehicle HAL发上来的所有属性变化。