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