1 /*
2  * Copyright (C) 2020 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.car.audio.hal;
18 
19 import android.car.builtin.util.Slogf;
20 import android.os.IBinder;
21 
22 import com.android.car.CarLog;
23 import com.android.internal.annotations.VisibleForTesting;
24 
25 /**
26  * Factory for constructing wrappers around IAudioControl HAL instances.
27  */
28 public final class AudioControlFactory {
29 
30     @VisibleForTesting
31     static final String TAG = CarLog.tagFor(AudioControlFactory.class);
32 
AudioControlFactory()33     private AudioControlFactory() {
34     }
35 
36     /**
37      * Generates {@link AudioControlWrapper} for interacting with IAudioControl HAL service. The HAL
38      * version priority is: Current AIDL, HIDL V2, HIDL V1. The wrapper will try to fetch the
39      * highest priority service, and then fall back to older versions if it's not available. The
40      * wrapper will throw if none is registered on the manifest.
41      *
42      * @return {@link AudioControlWrapper} for registered IAudioControl service.
43      */
newAudioControl()44     public static AudioControlWrapper newAudioControl() {
45         IBinder binder = AudioControlWrapperAidl.getService();
46         if (binder != null) {
47             return new AudioControlWrapperAidl(binder);
48         }
49         Slogf.i(TAG, "AIDL AudioControl HAL not in the manifest");
50 
51         android.hardware.automotive.audiocontrol.V2_0.IAudioControl audioControlV2 =
52                 AudioControlWrapperV2.getService();
53         if (audioControlV2 != null) {
54             return new AudioControlWrapperV2(audioControlV2);
55         }
56         Slogf.i(TAG, "HIDL AudioControl@V2.0 not in the manifest");
57 
58         android.hardware.automotive.audiocontrol.V1_0.IAudioControl audioControlV1 =
59                 AudioControlWrapperV1.getService();
60         if (audioControlV1 != null) {
61             Slogf.w(TAG, "HIDL AudioControl V1.0 is deprecated. Consider upgrading to AIDL");
62             return new AudioControlWrapperV1(audioControlV1);
63         }
64 
65         throw new IllegalStateException("No version of AudioControl HAL in the manifest");
66     }
67 }
68