1 /*
2  * Copyright (C) 2019 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 package com.android.car.audio;
17 
18 import android.car.media.ICarVolumeCallback;
19 import android.os.IBinder;
20 import android.os.RemoteException;
21 import android.util.Log;
22 
23 import androidx.annotation.NonNull;
24 
25 import com.android.car.BinderInterfaceContainer;
26 import com.android.car.CarLog;
27 
28 /**
29  * Manages callbacks for changes in car volume
30  */
31 class CarVolumeCallbackHandler {
32     private final BinderInterfaceContainer<ICarVolumeCallback> mVolumeCallbackContainer =
33             new BinderInterfaceContainer<>();
34 
release()35     void release() {
36         mVolumeCallbackContainer.clear();
37     }
38 
onVolumeGroupChange(int zoneId, int groupId, int flags)39     void onVolumeGroupChange(int zoneId, int groupId, int flags) {
40         for (BinderInterfaceContainer.BinderInterface<ICarVolumeCallback> callback :
41                 mVolumeCallbackContainer.getInterfaces()) {
42             try {
43                 callback.binderInterface.onGroupVolumeChanged(zoneId, groupId, flags);
44             } catch (RemoteException e) {
45                 Log.e(CarLog.TAG_AUDIO, "Failed to callback onGroupVolumeChanged", e);
46             }
47         }
48     }
49 
onMasterMuteChanged(int zoneId, int flags)50     void onMasterMuteChanged(int zoneId, int flags) {
51         for (BinderInterfaceContainer.BinderInterface<ICarVolumeCallback> callback :
52                 mVolumeCallbackContainer.getInterfaces()) {
53             try {
54                 callback.binderInterface.onMasterMuteChanged(zoneId, flags);
55             } catch (RemoteException e) {
56                 Log.e(CarLog.TAG_AUDIO, "Failed to callback onMasterMuteChanged", e);
57             }
58         }
59     }
60 
registerCallback(@onNull IBinder binder)61     public void registerCallback(@NonNull IBinder binder) {
62         mVolumeCallbackContainer.addBinder(ICarVolumeCallback.Stub.asInterface(binder));
63     }
64 
unregisterCallback(@onNull IBinder binder)65     public void unregisterCallback(@NonNull IBinder binder) {
66         mVolumeCallbackContainer.removeBinder(ICarVolumeCallback.Stub.asInterface(binder));
67     }
68 }
69