blob: a297472b926cde0dbd7d3fc357b0a533a435ca96 [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;
37
38import 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 */
46public 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
224 /* package */ void addBluetoothIndicatorListener(BluetoothIndicatorListener listener) {
225 if (!mListeners.contains(listener)) {
226 mListeners.add(listener);
227 }
228 }
229
230 private void notifyListeners(boolean showBluetoothOn) {
231 for (int i = 0; i < mListeners.size(); i++) {
232 mListeners.get(i).onBluetoothIndicationChange(showBluetoothOn, this);
233 }
234 }
235
236 private void init(Context context) {
237 Preconditions.checkNotNull(context);
238
239 if (mBluetoothAdapter != null) {
240 mBluetoothAdapter.getProfileProxy(context, mBluetoothProfileServiceListener,
241 BluetoothProfile.HEADSET);
242 }
243
244 // Register for misc other intent broadcasts.
245 IntentFilter intentFilter =
246 new IntentFilter(BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED);
247 intentFilter.addAction(BluetoothHeadset.ACTION_AUDIO_STATE_CHANGED);
248 context.registerReceiver(mReceiver, intentFilter);
249 }
250
251 private void tearDown() {
252 if (mBluetoothHeadset != null) {
253 mBluetoothAdapter.closeProfileProxy(BluetoothProfile.HEADSET, mBluetoothHeadset);
254 mBluetoothHeadset = null;
255 }
256 }
257
258 private BluetoothProfile.ServiceListener mBluetoothProfileServiceListener =
259 new BluetoothProfile.ServiceListener() {
260 @Override
261 public void onServiceConnected(int profile, BluetoothProfile proxy) {
262 mBluetoothHeadset = (BluetoothHeadset) proxy;
263 if (VDBG) log("- Got BluetoothHeadset: " + mBluetoothHeadset);
264 }
265
266 @Override
267 public void onServiceDisconnected(int profile) {
268 mBluetoothHeadset = null;
269 }
270 };
271
272 /**
273 * UI policy helper function for the couple of places in the UI that
274 * have some way of indicating that "bluetooth is in use."
275 *
276 * @return true if the onscreen UI should indicate that "bluetooth is in use",
277 * based on the specified bluetooth headset state, and the
278 * current state of the phone.
279 * @see showBluetoothIndication()
280 */
281 private static boolean shouldShowBluetoothIndication(int bluetoothState,
282 int bluetoothAudioState,
283 CallManager cm) {
284 // We want the UI to indicate that "bluetooth is in use" in two
285 // slightly different cases:
286 //
287 // (a) The obvious case: if a bluetooth headset is currently in
288 // use for an ongoing call.
289 //
290 // (b) The not-so-obvious case: if an incoming call is ringing,
291 // and we expect that audio *will* be routed to a bluetooth
292 // headset once the call is answered.
293
294 switch (cm.getState()) {
295 case OFFHOOK:
296 // This covers normal active calls, and also the case if
297 // the foreground call is DIALING or ALERTING. In this
298 // case, bluetooth is considered "active" if a headset
299 // is connected *and* audio is being routed to it.
300 return ((bluetoothState == BluetoothHeadset.STATE_CONNECTED)
301 && (bluetoothAudioState == BluetoothHeadset.STATE_AUDIO_CONNECTED));
302
303 case RINGING:
304 // If an incoming call is ringing, we're *not* yet routing
305 // audio to the headset (since there's no in-call audio
306 // yet!) In this case, if a bluetooth headset is
307 // connected at all, we assume that it'll become active
308 // once the user answers the phone.
309 return (bluetoothState == BluetoothHeadset.STATE_CONNECTED);
310
311 default: // Presumably IDLE
312 return false;
313 }
314 }
315
316 private void dumpBluetoothState() {
317 log("============== dumpBluetoothState() =============");
318 log("= isBluetoothAvailable: " + isBluetoothAvailable());
319 log("= isBluetoothAudioConnected: " + isBluetoothAudioConnected());
320 log("= isBluetoothAudioConnectedOrPending: " + isBluetoothAudioConnectedOrPending());
321 log("= PhoneApp.showBluetoothIndication: "
322 + showBluetoothIndication());
323 log("=");
324 if (mBluetoothAdapter != null) {
325 if (mBluetoothHeadset != null) {
326 List<BluetoothDevice> deviceList = mBluetoothHeadset.getConnectedDevices();
327
328 if (deviceList.size() > 0) {
329 BluetoothDevice device = deviceList.get(0);
330 log("= BluetoothHeadset.getCurrentDevice: " + device);
331 log("= BluetoothHeadset.State: "
332 + mBluetoothHeadset.getConnectionState(device));
333 log("= BluetoothHeadset audio connected: " +
334 mBluetoothHeadset.isAudioConnected(device));
335 }
336 } else {
337 log("= mBluetoothHeadset is null");
338 }
339 } else {
340 log("= mBluetoothAdapter is null; device is not BT capable");
341 }
342 }
343
344 /* package */ void connectBluetoothAudio() {
345 if (VDBG) log("connectBluetoothAudio()...");
346 if (mBluetoothHeadset != null) {
347 // TODO(BT) check return
348 mBluetoothHeadset.connectAudio();
349 }
350
351 // Watch out: The bluetooth connection doesn't happen instantly;
352 // the connectAudio() call returns instantly but does its real
353 // work in another thread. The mBluetoothConnectionPending flag
354 // is just a little trickery to ensure that the onscreen UI updates
355 // instantly. (See isBluetoothAudioConnectedOrPending() above.)
356 mBluetoothConnectionPending = true;
357 mBluetoothConnectionRequestTime = SystemClock.elapsedRealtime();
358 }
359
360 /* package */ void disconnectBluetoothAudio() {
361 if (VDBG) log("disconnectBluetoothAudio()...");
362 if (mBluetoothHeadset != null) {
363 mBluetoothHeadset.disconnectAudio();
364 }
365 mBluetoothConnectionPending = false;
366 }
367
368 /**
369 * Receiver for misc intent broadcasts the BluetoothManager cares about.
370 */
371 private class BluetoothBroadcastReceiver extends BroadcastReceiver {
372 @Override
373 public void onReceive(Context context, Intent intent) {
374 String action = intent.getAction();
375
376 if (action.equals(BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED)) {
377 mBluetoothHeadsetState = intent.getIntExtra(BluetoothHeadset.EXTRA_STATE,
378 BluetoothHeadset.STATE_DISCONNECTED);
379 if (VDBG) Log.d(LOG_TAG, "mReceiver: HEADSET_STATE_CHANGED_ACTION");
380 if (VDBG) Log.d(LOG_TAG, "==> new state: " + mBluetoothHeadsetState);
381 // Also update any visible UI if necessary
382 updateBluetoothIndication();
383 } else if (action.equals(BluetoothHeadset.ACTION_AUDIO_STATE_CHANGED)) {
384 mBluetoothHeadsetAudioState =
385 intent.getIntExtra(BluetoothHeadset.EXTRA_STATE,
386 BluetoothHeadset.STATE_AUDIO_DISCONNECTED);
387 if (VDBG) Log.d(LOG_TAG, "mReceiver: HEADSET_AUDIO_STATE_CHANGED_ACTION");
388 if (VDBG) Log.d(LOG_TAG, "==> new state: " + mBluetoothHeadsetAudioState);
389 updateBluetoothIndication();
390 }
391 }
392 }
393
394 private void log(String msg) {
395 Log.d(LOG_TAG, msg);
396 }
397
398 /* package */ interface BluetoothIndicatorListener {
399 public void onBluetoothIndicationChange(boolean showAsConnected, BluetoothManager manager);
400 }
401}