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