下面我们从代码角度分析setFadeTowardFront流程,首先是CarAudioManager的setFadeTowardFront方法:
//packages/services/Car/car-lib/src/android/car/media/CarAudioManager.java
public final class CarAudioManager extends CarManagerBase {
private final ICarAudio mService;
@SystemApi
@RequiresPermission(Car.PERMISSION_CAR_CONTROL_AUDIO_VOLUME)
@AddedInOrBefore(majorVersion = 33)
public void setFadeTowardFront(float value) {
try {
mService.setFadeTowardFront(value);
} catch (RemoteException e) {
handleRemoteExceptionFromCarService(e);
}
}
}
方法比较简单,直接调用ICarAudio的setFadeTowardFront方法,ICarAudio接口由CarAudioService实现,下面我们看CarAudioService的setFadeTowardFront方法:
//packages/services/Car/service/src/com/android/car/audioCarAudioService.java
public class CarAudioService extends ICarAudio.Stub implements CarServiceBase {
private AudioControlWrapper mAudioControlWrapper;
@Override
public void setFadeTowardFront(float value) {
synchronized (mImplLock) {
enforcePermission(Car.PERMISSION_CAR_CONTROL_AUDIO_VOLUME);
getAudioControlWrapperLocked().setFadeTowardFront(value); //调用getAudioControlWrapperLocked获取AudioControlWrapper对象,然后调用它的setFadeTowardFront方法
}
}
@GuardedBy("mImplLock")
private AudioControlWrapper getAudioControlWrapperLocked() {
if (mAudioControlWrapper == null) {
mAudioControlWrapper = AudioControlFactory.newAudioControl(); //调用AudioControlFactory的newAudioControl创建AudioControl
mAudioControlWrapper.linkToDeath(this::audioControlDied);
}
return mAudioControlWrapper; //返回AudioControlWrapper对象
}
}
这个函数主要步骤如下:
1、调用getAudioControlWrapperLocked获取AudioControlWrapper对象。
2、调用AudioControlWrapper的setFadeTowardFront方法,由于AudioControlWrapper是一个接口,而AudioControlWrapperAidl实现AudioControlWrapper接口,因此会调用到AudioControlWrapperAidl的AudioControlWrapper方法。
其中步骤1的代码:
//packages/services/Car/service/src/com/android/car/hal/AudioControlFactory.java
public final class AudioControlFactory {
@VisibleForTesting
static final String TAG = CarLog.tagFor(AudioControlFactory.class);
private AudioControlFactory() {
}
/**
* Generates {@link AudioControlWrapper} for interacting with IAudioControl HAL service. The HAL
* version priority is: Current AIDL, HIDL V2, HIDL V1. The wrapper will try to fetch the
* highest priority service, and then fall back to older versions if it's not available. The
* wrapper will throw if none is registered on the manifest.
*
* @return {@link AudioControlWrapper} for registered IAudioControl service.
*/
public static AudioControlWrapper newAudioControl() {
IBinder binder = AudioControlWrapperAidl.getService();
if (binder != null) {
return new AudioControlWrapperAidl(binder);
}
Slogf.i(TAG, "AIDL AudioControl HAL not in the manifest");
//根据audioControl版本返回对应的AudioControlWrapper
android.hardware.automotive.audiocontrol.V2_0.IAudioControl audioControlV2 =
AudioControlWrapperV2.getService();
if (audioControlV2 != null) {
return new AudioControlWrapperV2(audioControlV2);
}
Slogf.i(TAG, "HIDL AudioControl@V2.0 not in the manifest");
android.hardware.automotive.audiocontrol.V1_0.IAudioControl audioControlV1 =
AudioControlWrapperV1.getService();
if (audioControlV1 != null) {
Slogf.w(TAG, "HIDL AudioControl V1.0 is deprecated. Consider upgrading to AIDL");
return new AudioControlWrapperV1(audioControlV1);
}
throw new IllegalStateException("No version of AudioControl HAL in the manifest");
}
}
步骤2的代码:
//packages/services/Car/service/src/com/android/car/audio/hal/AudioControlWrapperAidl.java
public final class AudioControlWrapperAidl implements AudioControlWrapper, IBinder.DeathRecipient {
private IAudioControl mAudioControl;
@Override
public void setFadeTowardFront(float value) {
try {
mAudioControl.setFadeTowardFront(value); //调用IAudioControl的setFadeTowardFront方法
} catch (RemoteException e) {
Slogf.e(TAG, "setFadeTowardFront with " + value + " failed", e);
}
}
}
调用IAudioControl的setFadeTowardFront方法,IAudioControl是一个接口,实际调用的是AudioControl HAL