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