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