Santos Cordon | 9b7bac7 | 2013-08-06 08:04:52 -0700 | [diff] [blame^] | 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.phone; |
| 18 | |
| 19 | import com.google.common.collect.Lists; |
| 20 | |
| 21 | import android.content.Context; |
| 22 | import android.media.AudioManager; |
| 23 | import android.os.SystemProperties; |
| 24 | import android.util.Log; |
| 25 | |
| 26 | import com.android.phone.BluetoothManager.BluetoothIndicatorListener; |
| 27 | import com.android.services.telephony.common.AudioMode; |
| 28 | |
| 29 | import java.util.List; |
| 30 | |
| 31 | /** |
| 32 | * Responsible for Routing in-call audio and maintaining routing state. |
| 33 | */ |
| 34 | /* package */ class AudioRouter implements BluetoothIndicatorListener { |
| 35 | |
| 36 | private static String LOG_TAG = AudioRouter.class.getSimpleName(); |
| 37 | private static final boolean DBG = |
| 38 | (PhoneGlobals.DBG_LEVEL >= 1) && (SystemProperties.getInt("ro.debuggable", 0) == 1); |
| 39 | private static final boolean VDBG = (PhoneGlobals.DBG_LEVEL >= 2); |
| 40 | |
| 41 | private final Context mContext; |
| 42 | private final BluetoothManager mBluetoothManager; |
| 43 | private final List<AudioModeListener> mListeners = Lists.newArrayList(); |
| 44 | private int mAudioMode = AudioMode.EARPIECE; |
| 45 | private int mPreviousMode = AudioMode.EARPIECE; |
| 46 | |
| 47 | public AudioRouter(Context context, BluetoothManager bluetoothManager) { |
| 48 | mContext = context; |
| 49 | mBluetoothManager = bluetoothManager; |
| 50 | |
| 51 | init(); |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Return the current audio mode. |
| 56 | */ |
| 57 | public int getAudioMode() { |
| 58 | return mAudioMode; |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Add a listener to audio mode changes. |
| 63 | */ |
| 64 | public void addAudioModeListener(AudioModeListener listener) { |
| 65 | if (!mListeners.contains(listener)) { |
| 66 | mListeners.add(listener); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Remove listener. |
| 72 | */ |
| 73 | public void removeAudioModeListener(AudioModeListener listener) { |
| 74 | if (mListeners.contains(listener)) { |
| 75 | mListeners.remove(listener); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Sets the audio mode to the mode that is passed in. |
| 81 | */ |
| 82 | public void setAudioMode(int mode) { |
| 83 | if (AudioMode.BLUETOOTH == mode) { |
| 84 | |
| 85 | // dont set mAudioMode because we will get a notificaiton through |
| 86 | // onBluetoothIndicationChange if successful |
| 87 | toggleBluetooth(true); |
| 88 | } else if (AudioMode.EARPIECE == mode) { |
| 89 | toggleBluetooth(false); |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Called when the bluetooth connection changes. |
| 95 | * We adjust the audio mode according to the state we receive. |
| 96 | */ |
| 97 | @Override |
| 98 | public void onBluetoothIndicationChange(boolean isConnected, BluetoothManager btManager) { |
| 99 | int newMode = mAudioMode; |
| 100 | |
| 101 | if (isConnected) { |
| 102 | newMode = AudioMode.BLUETOOTH; |
| 103 | } else { |
| 104 | newMode = AudioMode.EARPIECE; |
| 105 | } |
| 106 | |
| 107 | changeAudioModeTo(newMode); |
| 108 | } |
| 109 | |
| 110 | private void toggleBluetooth(boolean on) { |
| 111 | if (on) { |
| 112 | mBluetoothManager.connectBluetoothAudio(); |
| 113 | } else { |
| 114 | mBluetoothManager.disconnectBluetoothAudio(); |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | private void init() { |
| 119 | mBluetoothManager.addBluetoothIndicatorListener(this); |
| 120 | } |
| 121 | |
| 122 | private void changeAudioModeTo(int mode) { |
| 123 | if (mAudioMode != mode) { |
| 124 | Log.i(LOG_TAG, "Audio mode changing to " + AudioMode.toString(mode)); |
| 125 | |
| 126 | mPreviousMode = mAudioMode; |
| 127 | mAudioMode = mode; |
| 128 | |
| 129 | notifyListeners(); |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | private void notifyListeners() { |
| 134 | for (int i = 0; i < mListeners.size(); i++) { |
| 135 | mListeners.get(i).onAudioModeChange(mPreviousMode, mAudioMode); |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | public interface AudioModeListener { |
| 140 | void onAudioModeChange(int previousMode, int newMode); |
| 141 | } |
| 142 | } |