1 /*
2  * Copyright (C) 2013 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.incallui;
18 
19 import android.telecom.CallAudioState;
20 
21 import com.google.common.collect.Lists;
22 
23 import java.util.List;
24 
25 /**
26  * Proxy class for getting and setting the audio mode.
27  */
28 public class AudioModeProvider {
29 
30     static final int AUDIO_MODE_INVALID = 0;
31 
32     private static AudioModeProvider sAudioModeProvider = new AudioModeProvider();
33     private int mAudioMode = CallAudioState.ROUTE_EARPIECE;
34     private boolean mMuted = false;
35     private int mSupportedModes = CallAudioState.ROUTE_EARPIECE
36             | CallAudioState.ROUTE_BLUETOOTH | CallAudioState.ROUTE_WIRED_HEADSET
37             | CallAudioState.ROUTE_SPEAKER;
38     private final List<AudioModeListener> mListeners = Lists.newArrayList();
39 
getInstance()40     public static AudioModeProvider getInstance() {
41         return sAudioModeProvider;
42     }
43 
onAudioStateChanged(boolean isMuted, int route, int supportedRouteMask)44     public void onAudioStateChanged(boolean isMuted, int route, int supportedRouteMask) {
45         onAudioModeChange(route, isMuted);
46         onSupportedAudioModeChange(supportedRouteMask);
47     }
48 
onAudioModeChange(int newMode, boolean muted)49     public void onAudioModeChange(int newMode, boolean muted) {
50         if (mAudioMode != newMode) {
51             mAudioMode = newMode;
52             for (AudioModeListener l : mListeners) {
53                 l.onAudioMode(mAudioMode);
54             }
55         }
56 
57         if (mMuted != muted) {
58             mMuted = muted;
59             for (AudioModeListener l : mListeners) {
60                 l.onMute(mMuted);
61             }
62         }
63     }
64 
onSupportedAudioModeChange(int newModeMask)65     public void onSupportedAudioModeChange(int newModeMask) {
66         mSupportedModes = newModeMask;
67 
68         for (AudioModeListener l : mListeners) {
69             l.onSupportedAudioMode(mSupportedModes);
70         }
71     }
72 
addListener(AudioModeListener listener)73     public void addListener(AudioModeListener listener) {
74         if (!mListeners.contains(listener)) {
75             mListeners.add(listener);
76             listener.onSupportedAudioMode(mSupportedModes);
77             listener.onAudioMode(mAudioMode);
78             listener.onMute(mMuted);
79         }
80     }
81 
removeListener(AudioModeListener listener)82     public void removeListener(AudioModeListener listener) {
83         if (mListeners.contains(listener)) {
84             mListeners.remove(listener);
85         }
86     }
87 
getSupportedModes()88     public int getSupportedModes() {
89         return mSupportedModes;
90     }
91 
getAudioMode()92     public int getAudioMode() {
93         return mAudioMode;
94     }
95 
getMute()96     public boolean getMute() {
97         return mMuted;
98     }
99 
100     /* package */ interface AudioModeListener {
onAudioMode(int newMode)101         void onAudioMode(int newMode);
onMute(boolean muted)102         void onMute(boolean muted);
onSupportedAudioMode(int modeMask)103         void onSupportedAudioMode(int modeMask);
104     }
105 }
106