在CarServiceImpl的onCreate方法里面会new一个ICarImpl实例,在ICarImpl构造函数中会创建CarAudioService:
//packages/services/Car/service/src/com/android/car/ICarImpl.java
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;
mUserManagerHelper = new CarUserManagerHelper(serviceContext);
final Resources res = mContext.getResources();
final int maxRunningUsers = res.getInteger(
com.android.internal.R.integer.config_multiuserMaxRunningUsers);
mCarUserService = new CarUserService(serviceContext, mUserManagerHelper,
ActivityManager.getService(), maxRunningUsers);
mSystemActivityMonitoringService = new SystemActivityMonitoringService(serviceContext);
mCarPowerManagementService = new CarPowerManagementService(mContext, mHal.getPowerHal(),
systemInterface, mUserManagerHelper);
mCarPropertyService = new CarPropertyService(serviceContext, mHal.getPropertyHal());
mCarDrivingStateService = new CarDrivingStateService(serviceContext, mCarPropertyService);
mCarUXRestrictionsService = new CarUxRestrictionsManagerService(serviceContext,
mCarDrivingStateService, mCarPropertyService);
mCarPackageManagerService = new CarPackageManagerService(serviceContext,
mCarUXRestrictionsService,
mSystemActivityMonitoringService,
mUserManagerHelper);
mPerUserCarServiceHelper = new PerUserCarServiceHelper(serviceContext);
mCarBluetoothService = new CarBluetoothService(serviceContext, mPerUserCarServiceHelper);
mCarInputService = new CarInputService(serviceContext, mHal.getInputHal());
mCarProjectionService = new CarProjectionService(
serviceContext, null /* handler */, mCarInputService, mCarBluetoothService);
mGarageModeService = new GarageModeService(mContext);
mAppFocusService = new AppFocusService(serviceContext, mSystemActivityMonitoringService);
mCarAudioService = new CarAudioService(serviceContext); //创建CarAudioService实例
mCarNightService = new CarNightService(serviceContext, mCarPropertyService);
mInstrumentClusterService = new InstrumentClusterService(serviceContext,
mAppFocusService, mCarInputService);
mSystemStateControllerService = new SystemStateControllerService(
serviceContext, mCarAudioService, this);
mVmsBrokerService = new VmsBrokerService(mContext.getPackageManager());
mVmsClientManager = new VmsClientManager(
serviceContext, mCarUserService, mUserManagerHelper, mHal.getVmsHal());
mVmsSubscriberService = new VmsSubscriberService(
serviceContext, mVmsBrokerService, mHal.getVmsHal());
mVmsPublisherService = new VmsPublisherService(
serviceContext, mVmsBrokerService, mVmsClientManager);
mCarDiagnosticService = new CarDiagnosticService(serviceContext, mHal.getDiagnosticHal());
mCarStorageMonitoringService = new CarStorageMonitoringService(serviceContext,
systemInterface);
mCarConfigurationService =
new CarConfigurationService(serviceContext, new JsonReaderImpl());
mCarLocationService = new CarLocationService(mContext, mUserManagerHelper);
mCarTrustedDeviceService = new CarTrustedDeviceService(serviceContext);
mCarMediaService = new CarMediaService(serviceContext);
mCarBugreportManagerService = new CarBugreportManagerService(serviceContext);
CarLocalServices.addService(CarPowerManagementService.class, mCarPowerManagementService);
CarLocalServices.addService(CarUserService.class, mCarUserService);
CarLocalServices.addService(CarTrustedDeviceService.class, mCarTrustedDeviceService);
CarLocalServices.addService(SystemInterface.class, mSystemInterface);
CarLocalServices.addService(CarDrivingStateService.class, mCarDrivingStateService);
CarLocalServices.addService(PerUserCarServiceHelper.class, mPerUserCarServiceHelper);
// Be careful with order. Service depending on other service should be inited later.
List<CarServiceBase> allServices = new ArrayList<>();
allServices.add(mCarUserService);
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); //将CarAudioService加入到allServices中
allServices.add(mCarNightService);
allServices.add(mInstrumentClusterService);
allServices.add(mSystemStateControllerService);
allServices.add(mPerUserCarServiceHelper);
allServices.add(mCarBluetoothService);
allServices.add(mCarProjectionService);
allServices.add(mCarDiagnosticService);
allServices.add(mCarStorageMonitoringService);
allServices.add(mCarConfigurationService);
allServices.add(mVmsClientManager);
allServices.add(mVmsSubscriberService);
allServices.add(mVmsPublisherService);
allServices.add(mCarTrustedDeviceService);
allServices.add(mCarMediaService);
allServices.add(mCarLocationService);
allServices.add(mCarBugreportManagerService);
mAllServices = allServices.toArray(new CarServiceBase[allServices.size()]);
}
}
CarAudioService的构造函数如下:
//packages/services/Car/service/src/com/android/car/audio/audioCarAudioService.java
public class CarAudioService extends ICarAudio.Stub implements CarServiceBase {
CarAudioService(Context context, @Nullable String audioConfigurationPath) {
mContext = Objects.requireNonNull(context,
"Context to create car audio service can not be null");
mCarAudioConfigurationPath = audioConfigurationPath;
mTelephonyManager = (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE);
mAudioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
mUseDynamicRouting = mContext.getResources().getBoolean(R.bool.audioUseDynamicRouting);
mKeyEventTimeoutMs =
mContext.getResources().getInteger(R.integer.audioVolumeKeyEventTimeoutMs);
mUseHalDuckingSignals = mContext.getResources().getBoolean(
R.bool.audioUseHalDuckingSignals);
mUidToZoneMap = new HashMap<>();
mCarVolumeCallbackHandler = new CarVolumeCallbackHandler(); //创建CarVolumeCallbackHandler对象
mCarAudioSettings = new CarAudioSettings(mContext); //创建CarAudioSettings对象
mAudioZoneIdToUserIdMapping = new SparseIntArray();
mAudioVolumeAdjustmentContextsVersion =
mContext.getResources().getInteger(R.integer.audioVolumeAdjustmentContextsVersion);
mCarVolume = new CarVolume(mClock,
mAudioVolumeAdjustmentContextsVersion, mKeyEventTimeoutMs); //创建CarVolume对象
boolean useCarVolumeGroupMuting = mUseDynamicRouting && mContext.getResources().getBoolean(
R.bool.audioUseCarVolumeGroupMuting);
if (mAudioVolumeAdjustmentContextsVersion != VERSION_TWO && useCarVolumeGroupMuting) {
throw new IllegalArgumentException("audioUseCarVolumeGroupMuting is enabled but "
+ "this requires audioVolumeAdjustmentContextsVersion 2,"
+ " instead version " + mAudioVolumeAdjustmentContextsVersion + " was found");
}
mUseCarVolumeGroupMuting = useCarVolumeGroupMuting;
mPersistMasterMuteState = !mUseCarVolumeGroupMuting && mContext.getResources().getBoolean(
R.bool.audioPersistMasterMuteState);
}
}