Santos Cordon | 27a3c1f | 2013-08-06 07:49:27 -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.android.collect.Lists; |
| 20 | import com.google.common.base.Preconditions; |
| 21 | |
| 22 | import android.bluetooth.BluetoothAdapter; |
| 23 | import android.bluetooth.BluetoothDevice; |
| 24 | import android.bluetooth.BluetoothHeadset; |
| 25 | import android.bluetooth.BluetoothProfile; |
| 26 | import android.content.BroadcastReceiver; |
| 27 | import android.content.Context; |
| 28 | import android.content.Intent; |
| 29 | import android.content.IntentFilter; |
| 30 | import android.os.Handler; |
| 31 | import android.os.Message; |
| 32 | import android.os.SystemClock; |
| 33 | import android.os.SystemProperties; |
| 34 | import android.util.Log; |
| 35 | |
| 36 | import com.android.internal.telephony.CallManager; |
| 37 | |
| 38 | import java.util.List; |
| 39 | |
| 40 | /** |
| 41 | * Listens to and caches bluetooth headset state. Used By the CallHandlerServiceProxy |
| 42 | * for sending this state to the UI layer. Also provides method for connection the bluetooth |
| 43 | * headset to the phone call. This is used by the CallCommandService to allow the UI to use the |
| 44 | * bluetooth headset if connected. |
| 45 | */ |
| 46 | public class BluetoothManager { |
| 47 | private static final String LOG_TAG = "InCallScreen"; |
| 48 | private static final boolean DBG = |
| 49 | (PhoneGlobals.DBG_LEVEL >= 1) && (SystemProperties.getInt("ro.debuggable", 0) == 1); |
| 50 | private static final boolean VDBG = (PhoneGlobals.DBG_LEVEL >= 2); |
| 51 | |
| 52 | private final BluetoothAdapter mBluetoothAdapter; |
| 53 | private final CallManager mCallManager; |
| 54 | private final Context mContext; |
| 55 | |
| 56 | private BluetoothHeadset mBluetoothHeadset; |
| 57 | private int mBluetoothHeadsetState = BluetoothProfile.STATE_DISCONNECTED; |
| 58 | private int mBluetoothHeadsetAudioState = BluetoothHeadset.STATE_AUDIO_DISCONNECTED; |
| 59 | private boolean mShowBluetoothIndication = false; |
| 60 | private boolean mBluetoothConnectionPending = false; |
| 61 | private long mBluetoothConnectionRequestTime; |
| 62 | |
| 63 | // Broadcast receiver for various intent broadcasts (see onCreate()) |
| 64 | private final BroadcastReceiver mReceiver = new BluetoothBroadcastReceiver(); |
| 65 | |
| 66 | private final List<BluetoothIndicatorListener> mListeners = Lists.newArrayList(); |
| 67 | |
| 68 | public BluetoothManager(Context context, CallManager callManager) { |
| 69 | mContext = context; |
| 70 | mCallManager = callManager; |
| 71 | mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); |
| 72 | |
| 73 | init(mContext); |
| 74 | // TODO(klp): Listen for changes to the call list/state. |
| 75 | } |
| 76 | |
| 77 | /* package */ boolean isBluetoothHeadsetAudioOn() { |
| 78 | return (mBluetoothHeadsetAudioState != BluetoothHeadset.STATE_AUDIO_DISCONNECTED); |
| 79 | } |
| 80 | |
| 81 | // |
| 82 | // Bluetooth helper methods. |
| 83 | // |
| 84 | // - BluetoothAdapter is the Bluetooth system service. If |
| 85 | // getDefaultAdapter() returns null |
| 86 | // then the device is not BT capable. Use BluetoothDevice.isEnabled() |
| 87 | // to see if BT is enabled on the device. |
| 88 | // |
| 89 | // - BluetoothHeadset is the API for the control connection to a |
| 90 | // Bluetooth Headset. This lets you completely connect/disconnect a |
| 91 | // headset (which we don't do from the Phone UI!) but also lets you |
| 92 | // get the address of the currently active headset and see whether |
| 93 | // it's currently connected. |
| 94 | |
| 95 | /** |
| 96 | * @return true if the Bluetooth on/off switch in the UI should be |
| 97 | * available to the user (i.e. if the device is BT-capable |
| 98 | * and a headset is connected.) |
| 99 | */ |
| 100 | /* package */ boolean isBluetoothAvailable() { |
| 101 | if (VDBG) log("isBluetoothAvailable()..."); |
| 102 | |
| 103 | // There's no need to ask the Bluetooth system service if BT is enabled: |
| 104 | // |
| 105 | // BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter(); |
| 106 | // if ((adapter == null) || !adapter.isEnabled()) { |
| 107 | // if (DBG) log(" ==> FALSE (BT not enabled)"); |
| 108 | // return false; |
| 109 | // } |
| 110 | // if (DBG) log(" - BT enabled! device name " + adapter.getName() |
| 111 | // + ", address " + adapter.getAddress()); |
| 112 | // |
| 113 | // ...since we already have a BluetoothHeadset instance. We can just |
| 114 | // call isConnected() on that, and assume it'll be false if BT isn't |
| 115 | // enabled at all. |
| 116 | |
| 117 | // Check if there's a connected headset, using the BluetoothHeadset API. |
| 118 | boolean isConnected = false; |
| 119 | if (mBluetoothHeadset != null) { |
| 120 | List<BluetoothDevice> deviceList = mBluetoothHeadset.getConnectedDevices(); |
| 121 | |
| 122 | if (deviceList.size() > 0) { |
| 123 | BluetoothDevice device = deviceList.get(0); |
| 124 | isConnected = true; |
| 125 | |
| 126 | if (VDBG) log(" - headset state = " + |
| 127 | mBluetoothHeadset.getConnectionState(device)); |
| 128 | if (VDBG) log(" - headset address: " + device); |
| 129 | if (VDBG) log(" - isConnected: " + isConnected); |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | if (VDBG) log(" ==> " + isConnected); |
| 134 | return isConnected; |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * @return true if a BT Headset is available, and its audio is currently connected. |
| 139 | */ |
| 140 | /* package */ boolean isBluetoothAudioConnected() { |
| 141 | if (mBluetoothHeadset == null) { |
| 142 | if (VDBG) log("isBluetoothAudioConnected: ==> FALSE (null mBluetoothHeadset)"); |
| 143 | return false; |
| 144 | } |
| 145 | List<BluetoothDevice> deviceList = mBluetoothHeadset.getConnectedDevices(); |
| 146 | |
| 147 | if (deviceList.isEmpty()) { |
| 148 | return false; |
| 149 | } |
| 150 | BluetoothDevice device = deviceList.get(0); |
| 151 | boolean isAudioOn = mBluetoothHeadset.isAudioConnected(device); |
| 152 | if (VDBG) log("isBluetoothAudioConnected: ==> isAudioOn = " + isAudioOn); |
| 153 | return isAudioOn; |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * Helper method used to control the onscreen "Bluetooth" indication; |
| 158 | * see InCallControlState.bluetoothIndicatorOn. |
| 159 | * |
| 160 | * @return true if a BT device is available and its audio is currently connected, |
| 161 | * <b>or</b> if we issued a BluetoothHeadset.connectAudio() |
| 162 | * call within the last 5 seconds (which presumably means |
| 163 | * that the BT audio connection is currently being set |
| 164 | * up, and will be connected soon.) |
| 165 | */ |
| 166 | /* package */ boolean isBluetoothAudioConnectedOrPending() { |
| 167 | if (isBluetoothAudioConnected()) { |
| 168 | if (VDBG) log("isBluetoothAudioConnectedOrPending: ==> TRUE (really connected)"); |
| 169 | return true; |
| 170 | } |
| 171 | |
| 172 | // If we issued a connectAudio() call "recently enough", even |
| 173 | // if BT isn't actually connected yet, let's still pretend BT is |
| 174 | // on. This makes the onscreen indication more responsive. |
| 175 | if (mBluetoothConnectionPending) { |
| 176 | long timeSinceRequest = |
| 177 | SystemClock.elapsedRealtime() - mBluetoothConnectionRequestTime; |
| 178 | if (timeSinceRequest < 5000 /* 5 seconds */) { |
| 179 | if (VDBG) log("isBluetoothAudioConnectedOrPending: ==> TRUE (requested " |
| 180 | + timeSinceRequest + " msec ago)"); |
| 181 | return true; |
| 182 | } else { |
| 183 | if (VDBG) log("isBluetoothAudioConnectedOrPending: ==> FALSE (request too old: " |
| 184 | + timeSinceRequest + " msec ago)"); |
| 185 | mBluetoothConnectionPending = false; |
| 186 | return false; |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | if (VDBG) log("isBluetoothAudioConnectedOrPending: ==> FALSE"); |
| 191 | return false; |
| 192 | } |
| 193 | |
| 194 | /** |
| 195 | * @return true if the onscreen UI should currently be showing the |
| 196 | * special "bluetooth is active" indication in a couple of places (in |
| 197 | * which UI elements turn blue and/or show the bluetooth logo.) |
| 198 | * |
| 199 | * This depends on the BluetoothHeadset state *and* the current |
| 200 | * telephony state; see shouldShowBluetoothIndication(). |
| 201 | * |
| 202 | * @see CallCard |
| 203 | * @see NotificationMgr.updateInCallNotification |
| 204 | */ |
| 205 | /* package */ boolean showBluetoothIndication() { |
| 206 | return mShowBluetoothIndication; |
| 207 | } |
| 208 | |
| 209 | /** |
| 210 | * Recomputes the mShowBluetoothIndication flag based on the current |
| 211 | * bluetooth state and current telephony state. |
| 212 | * |
| 213 | * This needs to be called any time the bluetooth headset state or the |
| 214 | * telephony state changes. |
| 215 | */ |
| 216 | /* package */ void updateBluetoothIndication() { |
| 217 | mShowBluetoothIndication = shouldShowBluetoothIndication(mBluetoothHeadsetState, |
| 218 | mBluetoothHeadsetAudioState, |
| 219 | mCallManager); |
| 220 | |
| 221 | notifyListeners(mShowBluetoothIndication); |
| 222 | } |
| 223 | |
Santos Cordon | 9b7bac7 | 2013-08-06 08:04:52 -0700 | [diff] [blame^] | 224 | public void addBluetoothIndicatorListener(BluetoothIndicatorListener listener) { |
Santos Cordon | 27a3c1f | 2013-08-06 07:49:27 -0700 | [diff] [blame] | 225 | if (!mListeners.contains(listener)) { |
| 226 | mListeners.add(listener); |
| 227 | } |
| 228 | } |
| 229 | |
Santos Cordon | 9b7bac7 | 2013-08-06 08:04:52 -0700 | [diff] [blame^] | 230 | public void removeBluetoothIndicatorListener(BluetoothIndicatorListener listener) { |
| 231 | if (mListeners.contains(listener)) { |
| 232 | mListeners.remove(listener); |
| 233 | } |
| 234 | } |
| 235 | |
Santos Cordon | 27a3c1f | 2013-08-06 07:49:27 -0700 | [diff] [blame] | 236 | private void notifyListeners(boolean showBluetoothOn) { |
| 237 | for (int i = 0; i < mListeners.size(); i++) { |
| 238 | mListeners.get(i).onBluetoothIndicationChange(showBluetoothOn, this); |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | private void init(Context context) { |
| 243 | Preconditions.checkNotNull(context); |
| 244 | |
| 245 | if (mBluetoothAdapter != null) { |
| 246 | mBluetoothAdapter.getProfileProxy(context, mBluetoothProfileServiceListener, |
| 247 | BluetoothProfile.HEADSET); |
| 248 | } |
| 249 | |
| 250 | // Register for misc other intent broadcasts. |
| 251 | IntentFilter intentFilter = |
| 252 | new IntentFilter(BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED); |
| 253 | intentFilter.addAction(BluetoothHeadset.ACTION_AUDIO_STATE_CHANGED); |
| 254 | context.registerReceiver(mReceiver, intentFilter); |
| 255 | } |
| 256 | |
| 257 | private void tearDown() { |
| 258 | if (mBluetoothHeadset != null) { |
| 259 | mBluetoothAdapter.closeProfileProxy(BluetoothProfile.HEADSET, mBluetoothHeadset); |
| 260 | mBluetoothHeadset = null; |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | private BluetoothProfile.ServiceListener mBluetoothProfileServiceListener = |
| 265 | new BluetoothProfile.ServiceListener() { |
| 266 | @Override |
| 267 | public void onServiceConnected(int profile, BluetoothProfile proxy) { |
| 268 | mBluetoothHeadset = (BluetoothHeadset) proxy; |
| 269 | if (VDBG) log("- Got BluetoothHeadset: " + mBluetoothHeadset); |
| 270 | } |
| 271 | |
| 272 | @Override |
| 273 | public void onServiceDisconnected(int profile) { |
| 274 | mBluetoothHeadset = null; |
| 275 | } |
| 276 | }; |
| 277 | |
| 278 | /** |
| 279 | * UI policy helper function for the couple of places in the UI that |
| 280 | * have some way of indicating that "bluetooth is in use." |
| 281 | * |
| 282 | * @return true if the onscreen UI should indicate that "bluetooth is in use", |
| 283 | * based on the specified bluetooth headset state, and the |
| 284 | * current state of the phone. |
| 285 | * @see showBluetoothIndication() |
| 286 | */ |
| 287 | private static boolean shouldShowBluetoothIndication(int bluetoothState, |
| 288 | int bluetoothAudioState, |
| 289 | CallManager cm) { |
| 290 | // We want the UI to indicate that "bluetooth is in use" in two |
| 291 | // slightly different cases: |
| 292 | // |
| 293 | // (a) The obvious case: if a bluetooth headset is currently in |
| 294 | // use for an ongoing call. |
| 295 | // |
| 296 | // (b) The not-so-obvious case: if an incoming call is ringing, |
| 297 | // and we expect that audio *will* be routed to a bluetooth |
| 298 | // headset once the call is answered. |
| 299 | |
| 300 | switch (cm.getState()) { |
| 301 | case OFFHOOK: |
| 302 | // This covers normal active calls, and also the case if |
| 303 | // the foreground call is DIALING or ALERTING. In this |
| 304 | // case, bluetooth is considered "active" if a headset |
| 305 | // is connected *and* audio is being routed to it. |
| 306 | return ((bluetoothState == BluetoothHeadset.STATE_CONNECTED) |
| 307 | && (bluetoothAudioState == BluetoothHeadset.STATE_AUDIO_CONNECTED)); |
| 308 | |
| 309 | case RINGING: |
| 310 | // If an incoming call is ringing, we're *not* yet routing |
| 311 | // audio to the headset (since there's no in-call audio |
| 312 | // yet!) In this case, if a bluetooth headset is |
| 313 | // connected at all, we assume that it'll become active |
| 314 | // once the user answers the phone. |
| 315 | return (bluetoothState == BluetoothHeadset.STATE_CONNECTED); |
| 316 | |
| 317 | default: // Presumably IDLE |
| 318 | return false; |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | private void dumpBluetoothState() { |
| 323 | log("============== dumpBluetoothState() ============="); |
| 324 | log("= isBluetoothAvailable: " + isBluetoothAvailable()); |
| 325 | log("= isBluetoothAudioConnected: " + isBluetoothAudioConnected()); |
| 326 | log("= isBluetoothAudioConnectedOrPending: " + isBluetoothAudioConnectedOrPending()); |
| 327 | log("= PhoneApp.showBluetoothIndication: " |
| 328 | + showBluetoothIndication()); |
| 329 | log("="); |
| 330 | if (mBluetoothAdapter != null) { |
| 331 | if (mBluetoothHeadset != null) { |
| 332 | List<BluetoothDevice> deviceList = mBluetoothHeadset.getConnectedDevices(); |
| 333 | |
| 334 | if (deviceList.size() > 0) { |
| 335 | BluetoothDevice device = deviceList.get(0); |
| 336 | log("= BluetoothHeadset.getCurrentDevice: " + device); |
| 337 | log("= BluetoothHeadset.State: " |
| 338 | + mBluetoothHeadset.getConnectionState(device)); |
| 339 | log("= BluetoothHeadset audio connected: " + |
| 340 | mBluetoothHeadset.isAudioConnected(device)); |
| 341 | } |
| 342 | } else { |
| 343 | log("= mBluetoothHeadset is null"); |
| 344 | } |
| 345 | } else { |
| 346 | log("= mBluetoothAdapter is null; device is not BT capable"); |
| 347 | } |
| 348 | } |
| 349 | |
| 350 | /* package */ void connectBluetoothAudio() { |
| 351 | if (VDBG) log("connectBluetoothAudio()..."); |
| 352 | if (mBluetoothHeadset != null) { |
| 353 | // TODO(BT) check return |
| 354 | mBluetoothHeadset.connectAudio(); |
| 355 | } |
| 356 | |
| 357 | // Watch out: The bluetooth connection doesn't happen instantly; |
| 358 | // the connectAudio() call returns instantly but does its real |
| 359 | // work in another thread. The mBluetoothConnectionPending flag |
| 360 | // is just a little trickery to ensure that the onscreen UI updates |
| 361 | // instantly. (See isBluetoothAudioConnectedOrPending() above.) |
| 362 | mBluetoothConnectionPending = true; |
| 363 | mBluetoothConnectionRequestTime = SystemClock.elapsedRealtime(); |
| 364 | } |
| 365 | |
| 366 | /* package */ void disconnectBluetoothAudio() { |
| 367 | if (VDBG) log("disconnectBluetoothAudio()..."); |
| 368 | if (mBluetoothHeadset != null) { |
| 369 | mBluetoothHeadset.disconnectAudio(); |
| 370 | } |
| 371 | mBluetoothConnectionPending = false; |
| 372 | } |
| 373 | |
| 374 | /** |
| 375 | * Receiver for misc intent broadcasts the BluetoothManager cares about. |
| 376 | */ |
| 377 | private class BluetoothBroadcastReceiver extends BroadcastReceiver { |
| 378 | @Override |
| 379 | public void onReceive(Context context, Intent intent) { |
| 380 | String action = intent.getAction(); |
| 381 | |
| 382 | if (action.equals(BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED)) { |
| 383 | mBluetoothHeadsetState = intent.getIntExtra(BluetoothHeadset.EXTRA_STATE, |
| 384 | BluetoothHeadset.STATE_DISCONNECTED); |
| 385 | if (VDBG) Log.d(LOG_TAG, "mReceiver: HEADSET_STATE_CHANGED_ACTION"); |
| 386 | if (VDBG) Log.d(LOG_TAG, "==> new state: " + mBluetoothHeadsetState); |
| 387 | // Also update any visible UI if necessary |
| 388 | updateBluetoothIndication(); |
| 389 | } else if (action.equals(BluetoothHeadset.ACTION_AUDIO_STATE_CHANGED)) { |
| 390 | mBluetoothHeadsetAudioState = |
| 391 | intent.getIntExtra(BluetoothHeadset.EXTRA_STATE, |
| 392 | BluetoothHeadset.STATE_AUDIO_DISCONNECTED); |
| 393 | if (VDBG) Log.d(LOG_TAG, "mReceiver: HEADSET_AUDIO_STATE_CHANGED_ACTION"); |
| 394 | if (VDBG) Log.d(LOG_TAG, "==> new state: " + mBluetoothHeadsetAudioState); |
| 395 | updateBluetoothIndication(); |
| 396 | } |
| 397 | } |
| 398 | } |
| 399 | |
| 400 | private void log(String msg) { |
| 401 | Log.d(LOG_TAG, msg); |
| 402 | } |
| 403 | |
| 404 | /* package */ interface BluetoothIndicatorListener { |
Santos Cordon | 9b7bac7 | 2013-08-06 08:04:52 -0700 | [diff] [blame^] | 405 | public void onBluetoothIndicationChange(boolean isConnected, BluetoothManager manager); |
Santos Cordon | 27a3c1f | 2013-08-06 07:49:27 -0700 | [diff] [blame] | 406 | } |
| 407 | } |