摘要:
Android系统中与Automotive相关的代码会被编译成一个名为CarService的apk存在于车机中,CarService中包括了众多和Car功能相关的Service,其中就包括CarAudioService。CarAudioService在初始化时会获取系统的音频策略配置参数并试图构造并注册AudioPolicy类,AudioPolicy.java是Java层音频策略的总设计图,其描述了总体音频策略,其中音频焦点策略可以通过setAudioPolicyFocusListener设置
一、CarService的启动及获取服务
概述:
CarService服务在系统启动时被拉起,然后通过ICarImpl初始化并保存所有Service,用户想要获取Service的Manager时必须通过Car类提供的方法
关键类及说明:
关键类 | 说明 |
---|---|
CarService | 最先被拉起的服务,Auto模块最主要的服务 |
ICarImpl | Auto模块各种Servcie的管理者,CarService功能的实现者 |
Car | 用户获取Manager的门户,其存在起到隔离用户操作实现解耦的作用 |
整个Android Automotive部分的代码会被编译成一个apk,既然是apk,那么肯定有AndroidManifest文件:
/packages/services/Car/service/AndroidManifest.xml:
......
<application android:label="Car service"
android:directBootAware="true"
android:defaultToDeviceProtectedStorage="true"
android:allowBackup="false"
android:persistent="true">
<uses-library android:name="android.test.runner" />
<service android:name=".CarService"
android:singleUser="true">
<intent-filter>
<action android:name="android.car.ICar" />
</intent-filter>
</service>
<service android:name=".PerUserCarService" android:exported="false" />
<activity android:name="com.android.car.pm.ActivityBlockingActivity"
android:excludeFromRecents="true"
android:theme="@android:style/Theme.Translucent.NoTitleBar"
android:exported="false"
android:launchMode="singleTask">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
</intent-filter>
</activity>
......
可以看到这个APP中存在名为CarService的Service,这个Service在系统启动后会通过bindService的方式被拉起,流程如下:
在CarService.java的onCreate方法中,会构建一个ICarImpl对象,并调用其init方法,之后调用addService将ICarImpl对象添加到ServiceManager中:
public void onCreate() {
Log.i(CarLog.TAG_SERVICE, "Service onCreate");
while(true) {
try {
Thread.sleep(10);
if ("1".equals(SystemProperties.get("vendor.all.system_server.ready"))) {
Log.i(CarLog.TAG_SERVICE, "Android Auto UI showed, continue the Car Service.");
break;
}
} catch (Exception err) {
}
}
mCanBusErrorNotifier = new CanBusErrorNotifier(this /* context */);
mVehicle = getVehicle();
if (mVehicle == null) {
throw new IllegalStateException("Vehicle HAL service is not available.");
}
try {
mVehicleInterfaceName = mVehicle.interfaceDescriptor();
} catch (RemoteException e) {
throw new IllegalStateException("Unable to get Vehicle HAL interface descriptor", e);
}
Log.i(CarLog.TAG_SERVICE, "Connected to " + mVehicleInterfaceName);
mICarImpl = new ICarImpl(this,
mVehicle,
SystemInterface.Builder.defaultSystemInterface(this).build(),
mCanBusErrorNotifier,
mVehicleInterfaceName);
mICarImpl.init();
SystemProperties.set("boot.car_service_created", "1");
linkToDeath(mVehicle, mVehicleDeathRecipient);
ServiceManager.addService("car_service", mICarImpl);
super.onCreate();
}
ICarImpl.java的构造方法中new出了众多和Car相关的Service,并把它们全部添加到mAllServices容器中:
public ICarImpl