CarService 中運行邏輯
在Android O中,Google在原有的Framework架構(gòu)中引入了Android Car的功能結(jié)構(gòu),這里著重對Android Service模塊進行相關(guān)的講解。由于Car模塊 Android O和P延續(xù)統(tǒng)一體系,這里就按照P的源碼,O/P架構(gòu)進行解析。Car的相關(guān)架構(gòu),請參照Android Automotive架構(gòu),不再做多的贅述、
Car service
CarService集中在一個App中。這個App需要非常高的權(quán)限,所以這是一個系統(tǒng)App。其Manifest開頭如下:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:androidprv="http://schemas.android.com/apk/prv/res/android"
package="com.android.car"
coreApp="true"
android:sharedUserId="android.uid.system">
android:sharedUserId屬性使得這個應(yīng)用具有系統(tǒng)權(quán)限。
CarService.java的onCreate()方法會new一個ICarImpl,并對一系列的服務(wù)進行初始化。
CarService并非一個服務(wù),而是一系列的服務(wù)。這些服務(wù)都在ICarImpl.java構(gòu)造函數(shù)中列了出來。
首先是CarService中onCreate()中關(guān)鍵代碼:
mICarImpl = new ICarImpl(this,
mVehicle,
SystemInterface.Builder.defaultSystemInterface(this).build(),
mCanBusErrorNotifier,
mVehicleInterfaceName);
mICarImpl.init();
再者是ICarImpl類中的構(gòu)造函數(shù):
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<CarServiceBase> 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();
}
閱讀源碼會發(fā)現(xiàn),CarService的onCreate()方法new完一個ICarImpl對象會執(zhí)行mICarImpl.init()方法初始化。
首先對VehicleHal執(zhí)行了init(),先是調(diào)用HalClient.getAllPropConfigs()從Vehicle HAL獲取所有的屬性,然后對各個service.takeSupportedProperties(properties)獲取CarService支持的屬性。
接著又對各個繼承了CarServiceBase的Service執(zhí)行service.init()方法。
比如PropertyHalServiceBase繼承了CarServiceBase,在init()方法里面對各個屬性執(zhí)行了mVehicleHal.subscribeProperty(this, prop),然后調(diào)用了VehicleHal.java的subscribeProperty(HalServiceBase service, int property),接著調(diào)用HalClient.subscribe(SubscribeOptions… options),這樣就把所有屬性都subscribe了,這樣CarService就能接收到Vehicle HAL發(fā)上來的所有屬性變化。