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