mCarHvacManager.registerCallback(mHvacCallback);
private CarHvacEventCallback mHvacCallback = new CarHvacEventCallback() {
@Override
public void onChangeEvent(CarPropertyValue carPropertyValue) {
//Log.d(TAG, "onChangeEvent id = " + toHexString(carPropertyValue.getPropertyId()));
int value = -1;
int areaId;
switch (carPropertyValue.getPropertyId()) {
case ID_HVAC_POWER_ON:
value = (Integer)carPropertyValue.getValue();
areaId = carPropertyValue.getAreaId();
break;
default:
break;
}
}
@Override
public void onErrorEvent(int i, int i1) { }
};
SET 和 GET,以HVAC空调为例:
mCarHvacManager.setIntProperty(ID_HVAC_LOCK_VENTILATION_ON, VehicleAreaType.VEHICLE_AREA_TYPE_GLOBAL, HVAC_ON);
float temp = mCarHvacManager.getFloatProperty(ID_HVAC_TEMPERATURE_SET, SEAT_ROW_1_LEFT);
VehicleAreaType分为GLOBAL(全局)、WINDOW(车窗)、MIRROR(镜子)、SEAT(座椅)、DOOR(车门)、WHEEL(车轮)等:
enum VehicleArea : int32_t {
GLOBAL = 0x01000000,
/** WINDOW maps to enum VehicleAreaWindow */
WINDOW = 0x03000000,
/** MIRROR maps to enum VehicleAreaMirror */
MIRROR = 0x04000000,
/** SEAT maps to enum VehicleAreaSeat */
SEAT = 0x05000000,
/** DOOR maps to enum VehicleAreaDoor */
DOOR = 0x06000000,
/** WHEEL maps to enum VehicleAreaWheel */
WHEEL = 0x07000000,
/** IN_OUT_CAR maps to enum VehicleAreaInOutCar,车内外 */
IN_OUT_CAR = 0x08000000,
MASK = 0x0f000000,
};
CarAPI (car-lib:android.car.jar)
1.createCar:
public static Car createCar(Context context, ServiceConnection serviceConnectionListener, @Nullable Handler handler) {
if (!context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_AUTOMOTIVE)) {
Log.e(CarLibLog.TAG_CAR, “FEATURE_AUTOMOTIVE not declared while android.car is used”);
return null;
}
try {
return new Car(context, serviceConnectionListener, handler);
} catch (IllegalArgumentException e) { // Expected when car service loader is not available.
}
return null;
}
public Object getCarManager(String serviceName) throws CarNotConnectedException {
CarManagerBase manager;
ICar service = getICarOrThrow();
synchronized (mCarManagerLock) {
manager = mServiceMap.get(serviceName);
if (manager == null) {
try {
if(TBOX_SERVICE.equals(serviceName)){
manager = createCarManager(serviceName, null);
return manager;
}
IBinder binder = service.getCarService(serviceName);
if (binder == null) {
Log.w(CarLibLog.TAG_CAR, “could not get binder for service:” + serviceName);
return null;
}
manager = createCarManager(serviceName, binder);
if (manager == null) {
Log.w(CarLibLog.TAG_CAR, “could not create manager for service:” + serviceName);
return null;
}
mServiceMap.put(serviceName, manager);
} catch (RemoteException e) {
handleRemoteException(e);
}
}
}
return manager;
}
private CarManagerBase createCarManager(String serviceName, IBinder binder) throws CarNotConnectedException {
CarManagerBase manager = null;
Log.d(“liueg”, "------------------createCarManager()serviceName "+serviceName);
switch (serviceName) {
case AUDIO_SERVICE: manager = new CarAudioManager(binder, mContext, mEventHandler);break;
case SENSOR_SERVICE: manager = new CarSensorManager(binder, mContext, mEventHandler);break;
case DYSENSOR_SERVICE: manager = new CarDYSensorManager(binder, mContext, mEventHandler);break;
case INFO_SERVICE: manager = new CarInfoManager(binder);break;
case APP_FOCUS_SERVICE: manager = new CarAppFocusManager(binder, mEventHandler);break;
case PACKAGE_SERVICE: manager = new CarPackageManager(binder, mContext);break;
case CAR_NAVIGATION_SERVICE: manager = new CarNavigationStatusManager(binder);break;
case CABIN_SERVICE: manager = new CarCabinManager(binder, mContext, mEventHandler);break;
case DIAGNOSTIC_SERVICE: manager = new CarDiagnosticManager(binder, mContext, mEventHandler);break;
case HVAC_SERVICE: manager = new CarHvacManager(binder, mContext, mEventHandler);break;
case DYHVAC_SERVICE: manager = new CarDYHvacManager(binder, mContext, mEventHandler);break;
case POWER_SERVICE: manager = new CarPowerManager(binder, mContext, mEventHandler);break;
case PROJECTION_SERVICE:manager = new CarProjectionManager(binder, mEventHandler);break;
case PROPERTY_SERVICE:
manager = new CarPropertyManager(binder, mEventHandler, false, “CarPropertyManager”);break;
case VENDOR_EXTENSION_SERVICE: manager = new CarVendorExtensionManager(binder, mEventHandler);break;
case CAR_INSTRUMENT_CLUSTER_SERVICE:
manager = new CarInstrumentClusterManager(binder, mEventHandler);break;
case TEST_SERVICE:
/* CarTestManager exist in static library. So instead of constructing it here,
only pass binder wrapper so that CarTestManager can be constructed outside. */
manager = new CarTestManagerBinderWrapper(binder);break;
case VMS_SUBSCRIBER_SERVICE: manager = new VmsSubscriberManager(binder); break;
case BLUETOOTH_SERVICE: manager = new CarBluetoothManager(binder, mContext);break;
case TBOX_SERVICE: manager = TboxManager.getInstance(); break;
case STORAGE_MONITORING_SERVICE: manager = new CarStorageMonitoringManager(binder, mEventHandler); break;
case CAR_DRIVING_STATE_SERVICE:
manager = new CarDrivingStateManager(binder, mContext, mEventHandler);break;
case CAR_UX_RESTRICTION_SERVICE:
manager = new CarUxRestrictionsManager(binder, mContext, mEventHandler);break;
case CAR_CONFIGURATION_SERVICE: manager = new CarConfigurationManager(binder);break;
case CAR_MCU_SERVICE: manager = new CarMcuManager(binder, mContext, mEventHandler);break;
case DYCABIN_SERVICE: manager = new CarDYCabinManager(binder, mContext, mEventHandler);break;
default:break;
}
return manager;
}
具体的Manager -> 注册/反注册回调, set/get 属性CarProperty:
public final class CarHvacManager implements CarManagerBase {
private void handleOnChangeEvent(CarPropertyValue value) {
Collection callbacks;
synchronized (this) { callbacks = new ArraySet<>(mCallbacks); }
if (!callbacks.isEmpty()) {
for (CarHvacEventCallback l: callbacks) {
if (DBG) Log.d(TAG, “onChangeEvent value=” + value.toString());
l.onChangeEvent(value);
}
}
}
private void handleOnErrorEvent(int propertyId, int zone) {
Collection callbacks;
synchronized (this) { callbacks = new ArraySet<>(mCallbacks); }
if (!callbacks.isEmpty()) {
for (CarHvacEventCallback l: callbacks) {
l.onErrorEvent(propertyId, zone);
}
}
}
public CarHvacManager(IBinder service, Context context, Handler handler) {
mCarPropertyMgr = new CarPropertyManager(service, handler, DBG, TAG);
}
public synchronized void registerCallback(CarHvacEventCallback callback)
throws CarNotConnectedException {
if (mCallbacks.isEmpty()) mListenerToBase = new CarPropertyEventListenerToBase(this);
List configs = getPropertyList();
for (CarPropertyConfig c : configs) { // Register each individual propertyId
mCarPropertyMgr.registerListener(mListenerToBase, c.getPropertyId(), 0);
}
mCallbacks.add(callback);
}
public synchronized void unregisterCallback(CarHvacEventCallback callback) {
mCallbacks.remove(callback);
try {
List configs = getPropertyList();
for (CarPropertyConfig c : configs) { // Register each individual propertyId
mCarPropertyMgr.unregisterListener(mListenerToBase, c.getPropertyId());
}
} catch (Exception e) { Log.e(TAG, "getPropertyList exception ", e); }
if (mCallbacks.isEmpty()) {
mCarPropertyMgr.unregisterListener(mListenerToBase);
mListenerToBase = null;
}
}
public List getPropertyList() throws CarNotConnectedException {
return mCarPropertyMgr.getPropertyList(mHvacPropertyIds);
}
public boolean isPropertyAvailable(@PropertyId int propertyId, int area) throws CarNotConnectedException {
return mCarPropertyMgr.isPropertyAvailable(propertyId, area);
}
// GET
// getBooleanProperty -> mCarPropertyMgr.getBooleanProperty(propertyId, area);
// getFloatProperty -> mCarPropertyMgr.getFloatProperty(propertyId, area);
// getIntProperty -> mCarPropertyMgr.getIntProperty(propertyId, area);
public boolean getBooleanProperty(@PropertyId int propertyId, int area)
throws CarNotConnectedException {
return mCarPropertyMgr.getBooleanProperty(propertyId, area);
}
// SET
// setBooleanProperty -> mCarPropertyMgr.setBooleanProperty(propertyId, area, val);
// setFloatProperty -> mCarPropertyMgr.setFloatProperty(propertyId, area, val);
// setIntProperty -> mCarPropertyMgr.setIntProperty(propertyId, area, val);
public void setBooleanProperty(@PropertyId int propertyId, int area, boolean val)
throws CarNotConnectedException {
if (mHvacPropertyIds.contains(propertyId)) {
mCarPropertyMgr.setBooleanProperty(propertyId, area, val);
}
}
}
CarService
ICarImpl初始化多个Service,可通过getCarService返回对应的Service
public class CarService extends Service {
IVehicle mVehicle = android.hardware.automotive.vehicle.V2_0.IVehicle.getService();
ICarImpl mICarImpl = new ICarImpl(this,
mVehicle,
SystemInterface.Builder.defaultSystemInterface(this).build(),
mCanBusErrorNotifier,
mVehicleInterfaceName);
mICarImpl.init();
}
public class ICarImpl extends ICar.Stub {
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, mCarPropertyService);
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);
addTboxService();
addUpgradeService();
// 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); // ★ CarPropertyService
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()]);
}
@MainThread
void init() {
traceBegin(“VehicleHal.init”);
mHal.init();
traceEnd();
traceBegin(“CarService.initAllServices”);
for (CarServiceBase service : mAllServices) {
service.init();
}
traceEnd();
}
@Override
public IBinder getCarService(String serviceName) {
switch (serviceName) {
case Car.AUDIO_SERVICE: return mCarAudioService;
case Car.APP_FOCUS_SERVICE: return mAppFocusService;
case Car.PACKAGE_SERVICE: return mCarPackageManagerService;
case Car.DIAGNOSTIC_SERVICE:
assertAnyDiagnosticPermission(mContext);
return mCarDiagnosticService;
case Car.POWER_SERVICE:
assertPowerPermission(mContext);
return mCarPowerManagementService;
case Car.CABIN_SERVICE:
case Car.HVAC_SERVICE:
case Car.INFO_SERVICE:
case Car.PROPERTY_SERVICE:
case Car.SENSOR_SERVICE:
case Car.VENDOR_EXTENSION_SERVICE:
case Car.DYHVAC_SERVICE:
case Car.CAR_MCU_SERVICE:
case Car.DYCABIN_SERVICE:
case Car.DYSENSOR_SERVICE:
return mCarPropertyService; // ★ CarPropertyService
… …
}
}
}
public class VehicleHal extends IVehicleCallback.Stub {
public VehicleHal(IVehicle vehicle) {
final HandlerThread mHandlerThread = new HandlerThread(“VEHICLE-HAL”);
mHandlerThread.start();
// passing this should be safe as long as it is just kept and not used in constructor
final PowerHalService mPowerHal = new PowerHalService(this);
final PropertyHalService mPropertyHal = new PropertyHalService(this); // ★ PropertyHalService
final InputHalService mInputHal = new InputHalService(this);
final VmsHalService mVmsHal = new VmsHalService(this);
DiagnosticHalService mDiagnosticHal = new DiagnosticHalService(this);
final ArrayList mAllServices = new ArrayList<>();
mAllServices.addAll(Arrays.asList(mPowerHal,
mInputHal,
mPropertyHal,
mDiagnosticHal,
mVmsHal));
volatile HalClient mHalClient = new HalClient(vehicle, mHandlerThread.getLooper(), this /IVehicleCallback/);
}
}