blob: 7159b49d4d42da3d0507406a2a3d25841b66e7b0 [file] [log] [blame]
Santos Cordon68d1a6b2014-09-19 12:25:58 -07001/*
2 * Copyright (C) 2014 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.server.telecom;
18
Santos Cordon68d1a6b2014-09-19 12:25:58 -070019import android.bluetooth.BluetoothAdapter;
20import android.bluetooth.BluetoothHeadset;
21import android.bluetooth.BluetoothProfile;
22import android.bluetooth.IBluetoothHeadsetPhone;
23import android.content.BroadcastReceiver;
24import android.content.Context;
25import android.content.Intent;
26import android.content.IntentFilter;
27import android.net.Uri;
Santos Cordonebf2d0f2015-05-15 10:28:29 -070028import android.os.Binder;
Santos Cordon68d1a6b2014-09-19 12:25:58 -070029import android.os.IBinder;
Santos Cordon68d1a6b2014-09-19 12:25:58 -070030import android.os.RemoteException;
Ihab Awad07bc5ee2014-11-12 13:42:52 -080031import android.telecom.Connection;
Santos Cordon68d1a6b2014-09-19 12:25:58 -070032import android.telecom.PhoneAccount;
33import android.telephony.PhoneNumberUtils;
34import android.telephony.TelephonyManager;
35import android.text.TextUtils;
36
Brad Ebinger53855132015-10-30 10:58:19 -070037import com.android.internal.annotations.VisibleForTesting;
Santos Cordon68d1a6b2014-09-19 12:25:58 -070038import com.android.server.telecom.CallsManager.CallsManagerListener;
39
Santos Cordon33fff492014-09-25 14:57:01 -070040import java.util.Collection;
41import java.util.HashMap;
Santos Cordon68d1a6b2014-09-19 12:25:58 -070042import java.util.List;
Santos Cordon33fff492014-09-25 14:57:01 -070043import java.util.Map;
Santos Cordon68d1a6b2014-09-19 12:25:58 -070044
45/**
46 * Bluetooth headset manager for Telecom. This class shares the call state with the bluetooth device
47 * and accepts call-related commands to perform on behalf of the BT device.
48 */
Hall Liub3979ee2015-11-11 16:21:25 -080049public class BluetoothPhoneServiceImpl {
50
51 public interface BluetoothPhoneServiceImplFactory {
52 BluetoothPhoneServiceImpl makeBluetoothPhoneServiceImpl(Context context,
53 TelecomSystem.SyncRoot lock, CallsManager callsManager,
54 PhoneAccountRegistrar phoneAccountRegistrar);
55 }
Santos Cordon68d1a6b2014-09-19 12:25:58 -070056
57 private static final String TAG = "BluetoothPhoneService";
58
Santos Cordon68d1a6b2014-09-19 12:25:58 -070059 // match up with bthf_call_state_t of bt_hf.h
60 private static final int CALL_STATE_ACTIVE = 0;
61 private static final int CALL_STATE_HELD = 1;
62 private static final int CALL_STATE_DIALING = 2;
63 private static final int CALL_STATE_ALERTING = 3;
64 private static final int CALL_STATE_INCOMING = 4;
65 private static final int CALL_STATE_WAITING = 5;
66 private static final int CALL_STATE_IDLE = 6;
67
68 // match up with bthf_call_state_t of bt_hf.h
69 // Terminate all held or set UDUB("busy") to a waiting call
70 private static final int CHLD_TYPE_RELEASEHELD = 0;
71 // Terminate all active calls and accepts a waiting/held call
72 private static final int CHLD_TYPE_RELEASEACTIVE_ACCEPTHELD = 1;
73 // Hold all active calls and accepts a waiting/held call
74 private static final int CHLD_TYPE_HOLDACTIVE_ACCEPTHELD = 2;
75 // Add all held calls to a conference
76 private static final int CHLD_TYPE_ADDHELDTOCONF = 3;
77
Santos Cordona0b46122014-09-29 14:20:21 -070078 private int mNumActiveCalls = 0;
79 private int mNumHeldCalls = 0;
80 private int mBluetoothCallState = CALL_STATE_IDLE;
81 private String mRingingAddress = null;
82 private int mRingingAddressType = 0;
Santos Cordonc0ca76e2014-10-21 15:54:19 -070083 private Call mOldHeldCall = null;
Santos Cordona0b46122014-09-29 14:20:21 -070084
Santos Cordon68d1a6b2014-09-19 12:25:58 -070085 /**
86 * Binder implementation of IBluetoothHeadsetPhone. Implements the command interface that the
87 * bluetooth headset code uses to control call.
88 */
Brad Ebinger53855132015-10-30 10:58:19 -070089 @VisibleForTesting
90 public final IBluetoothHeadsetPhone.Stub mBinder = new IBluetoothHeadsetPhone.Stub() {
Santos Cordon68d1a6b2014-09-19 12:25:58 -070091 @Override
92 public boolean answerCall() throws RemoteException {
Ihab Awad8d5d9dd2015-03-12 11:11:06 -070093 synchronized (mLock) {
94 enforceModifyPermission();
Brad Ebinger3165d502015-12-15 17:22:29 -080095 Log.startSession("BPSI.aC");
Santos Cordonebf2d0f2015-05-15 10:28:29 -070096 long token = Binder.clearCallingIdentity();
97 try {
98 Log.i(TAG, "BT - answering call");
99 Call call = mCallsManager.getRingingCall();
100 if (call != null) {
Mallikarjuna GB8729c182015-06-04 18:50:53 +0530101 mCallsManager.answerCall(call, call.getVideoState());
Santos Cordonebf2d0f2015-05-15 10:28:29 -0700102 return true;
103 }
104 return false;
105 } finally {
106 Binder.restoreCallingIdentity(token);
Brad Ebinger3165d502015-12-15 17:22:29 -0800107 Log.endSession();
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700108 }
Santos Cordonebf2d0f2015-05-15 10:28:29 -0700109
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700110 }
Santos Cordon68d1a6b2014-09-19 12:25:58 -0700111 }
112
113 @Override
114 public boolean hangupCall() throws RemoteException {
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700115 synchronized (mLock) {
116 enforceModifyPermission();
Brad Ebinger3165d502015-12-15 17:22:29 -0800117 Log.startSession("BPSI.hC");
Santos Cordonebf2d0f2015-05-15 10:28:29 -0700118 long token = Binder.clearCallingIdentity();
119 try {
120 Log.i(TAG, "BT - hanging up call");
121 Call call = mCallsManager.getForegroundCall();
122 if (call != null) {
123 mCallsManager.disconnectCall(call);
124 return true;
125 }
126 return false;
127 } finally {
128 Binder.restoreCallingIdentity(token);
Brad Ebinger3165d502015-12-15 17:22:29 -0800129 Log.endSession();
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700130 }
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700131 }
Santos Cordon68d1a6b2014-09-19 12:25:58 -0700132 }
133
134 @Override
135 public boolean sendDtmf(int dtmf) throws RemoteException {
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700136 synchronized (mLock) {
137 enforceModifyPermission();
Brad Ebinger3165d502015-12-15 17:22:29 -0800138 Log.startSession("BPSI.sD");
Santos Cordonebf2d0f2015-05-15 10:28:29 -0700139 long token = Binder.clearCallingIdentity();
140 try {
141 Log.i(TAG, "BT - sendDtmf %c", Log.DEBUG ? dtmf : '.');
142 Call call = mCallsManager.getForegroundCall();
143 if (call != null) {
144 // TODO: Consider making this a queue instead of starting/stopping
145 // in quick succession.
146 mCallsManager.playDtmfTone(call, (char) dtmf);
147 mCallsManager.stopDtmfTone(call);
148 return true;
149 }
150 return false;
151 } finally {
152 Binder.restoreCallingIdentity(token);
Brad Ebinger3165d502015-12-15 17:22:29 -0800153 Log.endSession();
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700154 }
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700155 }
Santos Cordon68d1a6b2014-09-19 12:25:58 -0700156 }
157
158 @Override
159 public String getNetworkOperator() throws RemoteException {
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700160 synchronized (mLock) {
161 enforceModifyPermission();
Brad Ebinger3165d502015-12-15 17:22:29 -0800162 Log.startSession("BPSI.gNO");
Santos Cordonebf2d0f2015-05-15 10:28:29 -0700163 long token = Binder.clearCallingIdentity();
164 try {
165 Log.i(TAG, "getNetworkOperator");
166 PhoneAccount account = getBestPhoneAccount();
167 if (account != null) {
168 return account.getLabel().toString();
169 } else {
170 // Finally, just get the network name from telephony.
171 return TelephonyManager.from(mContext)
172 .getNetworkOperatorName();
173 }
174 } finally {
175 Binder.restoreCallingIdentity(token);
Brad Ebinger3165d502015-12-15 17:22:29 -0800176 Log.endSession();
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700177 }
178 }
Santos Cordon68d1a6b2014-09-19 12:25:58 -0700179 }
180
181 @Override
182 public String getSubscriberNumber() throws RemoteException {
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700183 synchronized (mLock) {
184 enforceModifyPermission();
Brad Ebinger3165d502015-12-15 17:22:29 -0800185 Log.startSession("BPSI.gSN");
Santos Cordonebf2d0f2015-05-15 10:28:29 -0700186 long token = Binder.clearCallingIdentity();
187 try {
188 Log.i(TAG, "getSubscriberNumber");
189 String address = null;
190 PhoneAccount account = getBestPhoneAccount();
191 if (account != null) {
192 Uri addressUri = account.getAddress();
193 if (addressUri != null) {
194 address = addressUri.getSchemeSpecificPart();
195 }
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700196 }
Santos Cordonebf2d0f2015-05-15 10:28:29 -0700197 if (TextUtils.isEmpty(address)) {
198 address = TelephonyManager.from(mContext).getLine1Number();
Andre Eisenbach31092622015-06-04 18:56:42 -0700199 if (address == null) address = "";
Santos Cordonebf2d0f2015-05-15 10:28:29 -0700200 }
201 return address;
202 } finally {
203 Binder.restoreCallingIdentity(token);
Brad Ebinger3165d502015-12-15 17:22:29 -0800204 Log.endSession();
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700205 }
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700206 }
Santos Cordon68d1a6b2014-09-19 12:25:58 -0700207 }
208
209 @Override
210 public boolean listCurrentCalls() throws RemoteException {
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700211 synchronized (mLock) {
212 enforceModifyPermission();
Brad Ebinger3165d502015-12-15 17:22:29 -0800213 Log.startSession("BPSI.lCC");
Santos Cordonebf2d0f2015-05-15 10:28:29 -0700214 long token = Binder.clearCallingIdentity();
215 try {
216 // only log if it is after we recently updated the headset state or else it can
217 // clog the android log since this can be queried every second.
218 boolean logQuery = mHeadsetUpdatedRecently;
219 mHeadsetUpdatedRecently = false;
220
221 if (logQuery) {
222 Log.i(TAG, "listcurrentCalls");
223 }
224
225 sendListOfCalls(logQuery);
226 return true;
227 } finally {
228 Binder.restoreCallingIdentity(token);
Brad Ebinger3165d502015-12-15 17:22:29 -0800229 Log.endSession();
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700230 }
Santos Cordon88a4a602014-09-29 19:32:21 -0700231 }
Santos Cordon68d1a6b2014-09-19 12:25:58 -0700232 }
233
234 @Override
235 public boolean queryPhoneState() throws RemoteException {
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700236 synchronized (mLock) {
237 enforceModifyPermission();
Brad Ebinger3165d502015-12-15 17:22:29 -0800238 Log.startSession("BPSI.qPS");
Santos Cordonebf2d0f2015-05-15 10:28:29 -0700239 long token = Binder.clearCallingIdentity();
240 try {
241 Log.i(TAG, "queryPhoneState");
242 updateHeadsetWithCallState(true /* force */);
243 return true;
244 } finally {
245 Binder.restoreCallingIdentity(token);
Brad Ebinger3165d502015-12-15 17:22:29 -0800246 Log.endSession();
Santos Cordonebf2d0f2015-05-15 10:28:29 -0700247 }
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700248 }
Santos Cordon68d1a6b2014-09-19 12:25:58 -0700249 }
250
251 @Override
252 public boolean processChld(int chld) throws RemoteException {
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700253 synchronized (mLock) {
254 enforceModifyPermission();
Brad Ebinger3165d502015-12-15 17:22:29 -0800255 Log.startSession("BPSI.pC");
Santos Cordonebf2d0f2015-05-15 10:28:29 -0700256 long token = Binder.clearCallingIdentity();
257 try {
258 Log.i(TAG, "processChld %d", chld);
259 return BluetoothPhoneServiceImpl.this.processChld(chld);
260 } finally {
261 Binder.restoreCallingIdentity(token);
Brad Ebinger3165d502015-12-15 17:22:29 -0800262 Log.endSession();
Santos Cordonebf2d0f2015-05-15 10:28:29 -0700263 }
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700264 }
Santos Cordon68d1a6b2014-09-19 12:25:58 -0700265 }
266
267 @Override
268 public void updateBtHandsfreeAfterRadioTechnologyChange() throws RemoteException {
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700269 Log.d(TAG, "RAT change - deprecated");
Santos Cordon68d1a6b2014-09-19 12:25:58 -0700270 // deprecated
271 }
272
273 @Override
274 public void cdmaSetSecondCallState(boolean state) throws RemoteException {
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700275 Log.d(TAG, "cdma 1 - deprecated");
Santos Cordon68d1a6b2014-09-19 12:25:58 -0700276 // deprecated
277 }
278
279 @Override
280 public void cdmaSwapSecondCallState() throws RemoteException {
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700281 Log.d(TAG, "cdma 2 - deprecated");
Santos Cordon68d1a6b2014-09-19 12:25:58 -0700282 // deprecated
283 }
284 };
285
286 /**
Santos Cordon68d1a6b2014-09-19 12:25:58 -0700287 * Listens to call changes from the CallsManager and calls into methods to update the bluetooth
288 * headset with the new states.
289 */
Brad Ebinger53855132015-10-30 10:58:19 -0700290 @VisibleForTesting
291 public CallsManagerListener mCallsManagerListener = new CallsManagerListenerBase() {
Santos Cordon68d1a6b2014-09-19 12:25:58 -0700292 @Override
293 public void onCallAdded(Call call) {
Tyler Gunncd685e52014-10-10 11:48:25 -0700294 updateHeadsetWithCallState(false /* force */);
Santos Cordon68d1a6b2014-09-19 12:25:58 -0700295 }
296
297 @Override
298 public void onCallRemoved(Call call) {
Santos Cordon33fff492014-09-25 14:57:01 -0700299 mClccIndexMap.remove(call);
Tyler Gunncd685e52014-10-10 11:48:25 -0700300 updateHeadsetWithCallState(false /* force */);
Santos Cordon68d1a6b2014-09-19 12:25:58 -0700301 }
302
303 @Override
304 public void onCallStateChanged(Call call, int oldState, int newState) {
Yorke Lee720bcbe2014-10-22 18:09:15 -0700305 // If a call is being put on hold because of a new connecting call, ignore the
306 // CONNECTING since the BT state update needs to send out the numHeld = 1 + dialing
307 // state atomically.
308 // When the call later transitions to DIALING/DISCONNECTED we will then send out the
309 // aggregated update.
310 if (oldState == CallState.ACTIVE && newState == CallState.ON_HOLD) {
Ihab Awad78a5e6b2015-02-06 10:13:05 -0800311 for (Call otherCall : mCallsManager.getCalls()) {
Yorke Lee720bcbe2014-10-22 18:09:15 -0700312 if (otherCall.getState() == CallState.CONNECTING) {
313 return;
314 }
315 }
316 }
317
318 // To have an active call and another dialing at the same time is an invalid BT
319 // state. We can assume that the active call will be automatically held which will
320 // send another update at which point we will be in the right state.
Ihab Awad78a5e6b2015-02-06 10:13:05 -0800321 if (mCallsManager.getActiveCall() != null
Yorke Lee720bcbe2014-10-22 18:09:15 -0700322 && oldState == CallState.CONNECTING && newState == CallState.DIALING) {
323 return;
324 }
Tyler Gunncd685e52014-10-10 11:48:25 -0700325 updateHeadsetWithCallState(false /* force */);
Santos Cordon68d1a6b2014-09-19 12:25:58 -0700326 }
327
328 @Override
329 public void onForegroundCallChanged(Call oldForegroundCall, Call newForegroundCall) {
Yorke Lee720bcbe2014-10-22 18:09:15 -0700330 // The BluetoothPhoneService does not need to respond to changes in foreground calls,
331 // which are always accompanied by call state changes anyway.
Santos Cordon68d1a6b2014-09-19 12:25:58 -0700332 }
333
334 @Override
335 public void onIsConferencedChanged(Call call) {
Yorke Lee720bcbe2014-10-22 18:09:15 -0700336 /*
337 * Filter certain onIsConferencedChanged callbacks. Unfortunately this needs to be done
338 * because conference change events are not atomic and multiple callbacks get fired
339 * when two calls are conferenced together. This confuses updateHeadsetWithCallState
340 * if it runs in the middle of two calls being conferenced and can cause spurious and
341 * incorrect headset state updates. One of the scenarios is described below for CDMA
342 * conference calls.
343 *
344 * 1) Call 1 and Call 2 are being merged into conference Call 3.
345 * 2) Call 1 has its parent set to Call 3, but Call 2 does not have a parent yet.
346 * 3) updateHeadsetWithCallState now thinks that there are two active calls (Call 2 and
347 * Call 3) when there is actually only one active call (Call 3).
348 */
349 if (call.getParentCall() != null) {
350 // If this call is newly conferenced, ignore the callback. We only care about the
351 // one sent for the parent conference call.
352 Log.d(this, "Ignoring onIsConferenceChanged from child call with new parent");
353 return;
354 }
355 if (call.getChildCalls().size() == 1) {
356 // If this is a parent call with only one child, ignore the callback as well since
357 // the minimum number of child calls to start a conference call is 2. We expect
358 // this to be called again when the parent call has another child call added.
359 Log.d(this, "Ignoring onIsConferenceChanged from parent with only one child call");
360 return;
361 }
Tyler Gunncd685e52014-10-10 11:48:25 -0700362 updateHeadsetWithCallState(false /* force */);
Santos Cordon68d1a6b2014-09-19 12:25:58 -0700363 }
364 };
365
366 /**
367 * Listens to connections and disconnections of bluetooth headsets. We need to save the current
368 * bluetooth headset so that we know where to send call updates.
369 */
Brad Ebinger53855132015-10-30 10:58:19 -0700370 @VisibleForTesting
371 public BluetoothProfile.ServiceListener mProfileListener =
Santos Cordon68d1a6b2014-09-19 12:25:58 -0700372 new BluetoothProfile.ServiceListener() {
Brad Ebinger53855132015-10-30 10:58:19 -0700373 @Override
374 public void onServiceConnected(int profile, BluetoothProfile proxy) {
375 synchronized (mLock) {
376 setBluetoothHeadset(new BluetoothHeadsetProxy((BluetoothHeadset) proxy));
377 }
378 }
Santos Cordon68d1a6b2014-09-19 12:25:58 -0700379
Brad Ebinger53855132015-10-30 10:58:19 -0700380 @Override
381 public void onServiceDisconnected(int profile) {
382 synchronized (mLock) {
383 mBluetoothHeadset = null;
384 }
385 }
386 };
Santos Cordon68d1a6b2014-09-19 12:25:58 -0700387
388 /**
389 * Receives events for global state changes of the bluetooth adapter.
390 */
Brad Ebinger71286022015-12-09 17:13:27 -0800391 @VisibleForTesting
392 public final BroadcastReceiver mBluetoothAdapterReceiver = new BroadcastReceiver() {
Santos Cordon68d1a6b2014-09-19 12:25:58 -0700393 @Override
394 public void onReceive(Context context, Intent intent) {
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700395 synchronized (mLock) {
396 int state = intent
397 .getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR);
398 Log.d(TAG, "Bluetooth Adapter state: %d", state);
399 if (state == BluetoothAdapter.STATE_ON) {
400 try {
401 mBinder.queryPhoneState();
402 } catch (RemoteException e) {
403 // Remote exception not expected
404 }
405 }
Santos Cordon68d1a6b2014-09-19 12:25:58 -0700406 }
407 }
408 };
409
410 private BluetoothAdapter mBluetoothAdapter;
Brad Ebinger53855132015-10-30 10:58:19 -0700411 private BluetoothHeadsetProxy mBluetoothHeadset;
Santos Cordon68d1a6b2014-09-19 12:25:58 -0700412
Santos Cordon33fff492014-09-25 14:57:01 -0700413 // A map from Calls to indexes used to identify calls for CLCC (C* List Current Calls).
414 private Map<Call, Integer> mClccIndexMap = new HashMap<>();
415
Santos Cordon88a4a602014-09-29 19:32:21 -0700416 private boolean mHeadsetUpdatedRecently = false;
417
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700418 private final Context mContext;
419 private final TelecomSystem.SyncRoot mLock;
420 private final CallsManager mCallsManager;
421 private final PhoneAccountRegistrar mPhoneAccountRegistrar;
Santos Cordon68d1a6b2014-09-19 12:25:58 -0700422
Ihab Awad78a5e6b2015-02-06 10:13:05 -0800423 public IBinder getBinder() {
Santos Cordon68d1a6b2014-09-19 12:25:58 -0700424 return mBinder;
425 }
426
Ihab Awad78a5e6b2015-02-06 10:13:05 -0800427 public BluetoothPhoneServiceImpl(
428 Context context,
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700429 TelecomSystem.SyncRoot lock,
Ihab Awad78a5e6b2015-02-06 10:13:05 -0800430 CallsManager callsManager,
431 PhoneAccountRegistrar phoneAccountRegistrar) {
432 Log.d(this, "onCreate");
433
434 mContext = context;
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700435 mLock = lock;
Ihab Awad78a5e6b2015-02-06 10:13:05 -0800436 mCallsManager = callsManager;
437 mPhoneAccountRegistrar = phoneAccountRegistrar;
Santos Cordon68d1a6b2014-09-19 12:25:58 -0700438
439 mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
440 if (mBluetoothAdapter == null) {
Ihab Awad78a5e6b2015-02-06 10:13:05 -0800441 Log.d(this, "BluetoothPhoneService shutting down, no BT Adapter found.");
Santos Cordon68d1a6b2014-09-19 12:25:58 -0700442 return;
443 }
Ihab Awad78a5e6b2015-02-06 10:13:05 -0800444 mBluetoothAdapter.getProfileProxy(context, mProfileListener, BluetoothProfile.HEADSET);
Santos Cordon68d1a6b2014-09-19 12:25:58 -0700445
446 IntentFilter intentFilter = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);
Ihab Awad78a5e6b2015-02-06 10:13:05 -0800447 context.registerReceiver(mBluetoothAdapterReceiver, intentFilter);
Santos Cordon68d1a6b2014-09-19 12:25:58 -0700448
Ihab Awad78a5e6b2015-02-06 10:13:05 -0800449 mCallsManager.addListener(mCallsManagerListener);
Tyler Gunncd685e52014-10-10 11:48:25 -0700450 updateHeadsetWithCallState(false /* force */);
Santos Cordon68d1a6b2014-09-19 12:25:58 -0700451 }
452
Brad Ebinger53855132015-10-30 10:58:19 -0700453 @VisibleForTesting
454 public void setBluetoothHeadset(BluetoothHeadsetProxy bluetoothHeadset) {
455 mBluetoothHeadset = bluetoothHeadset;
456 }
457
Santos Cordon68d1a6b2014-09-19 12:25:58 -0700458 private boolean processChld(int chld) {
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700459 Call activeCall = mCallsManager.getActiveCall();
460 Call ringingCall = mCallsManager.getRingingCall();
461 Call heldCall = mCallsManager.getHeldCall();
Santos Cordon68d1a6b2014-09-19 12:25:58 -0700462
Santos Cordon66fe8822014-10-10 16:10:58 -0700463 // TODO: Keeping as Log.i for now. Move to Log.d after L release if BT proves stable.
464 Log.i(TAG, "Active: %s\nRinging: %s\nHeld: %s", activeCall, ringingCall, heldCall);
Santos Cordon88a4a602014-09-29 19:32:21 -0700465
Santos Cordon68d1a6b2014-09-19 12:25:58 -0700466 if (chld == CHLD_TYPE_RELEASEHELD) {
467 if (ringingCall != null) {
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700468 mCallsManager.rejectCall(ringingCall, false, null);
Santos Cordon68d1a6b2014-09-19 12:25:58 -0700469 return true;
470 } else if (heldCall != null) {
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700471 mCallsManager.disconnectCall(heldCall);
Santos Cordon68d1a6b2014-09-19 12:25:58 -0700472 return true;
473 }
474 } else if (chld == CHLD_TYPE_RELEASEACTIVE_ACCEPTHELD) {
475 if (activeCall != null) {
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700476 mCallsManager.disconnectCall(activeCall);
Santos Cordon68d1a6b2014-09-19 12:25:58 -0700477 if (ringingCall != null) {
Mallikarjuna GB8729c182015-06-04 18:50:53 +0530478 mCallsManager.answerCall(ringingCall, ringingCall.getVideoState());
Santos Cordon68d1a6b2014-09-19 12:25:58 -0700479 } else if (heldCall != null) {
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700480 mCallsManager.unholdCall(heldCall);
Santos Cordon68d1a6b2014-09-19 12:25:58 -0700481 }
482 return true;
483 }
484 } else if (chld == CHLD_TYPE_HOLDACTIVE_ACCEPTHELD) {
Ihab Awad07bc5ee2014-11-12 13:42:52 -0800485 if (activeCall != null && activeCall.can(Connection.CAPABILITY_SWAP_CONFERENCE)) {
Santos Cordon88a4a602014-09-29 19:32:21 -0700486 activeCall.swapConference();
Mallikarjuna GB0ae2df82015-06-04 18:20:21 +0530487 Log.i(TAG, "CDMA calls in conference swapped, updating headset");
488 updateHeadsetWithCallState(true /* force */);
Santos Cordon88a4a602014-09-29 19:32:21 -0700489 return true;
490 } else if (ringingCall != null) {
Mallikarjuna GB8729c182015-06-04 18:50:53 +0530491 mCallsManager.answerCall(ringingCall, ringingCall.getVideoState());
Santos Cordon68d1a6b2014-09-19 12:25:58 -0700492 return true;
493 } else if (heldCall != null) {
494 // CallsManager will hold any active calls when unhold() is called on a
495 // currently-held call.
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700496 mCallsManager.unholdCall(heldCall);
Santos Cordon68d1a6b2014-09-19 12:25:58 -0700497 return true;
Ihab Awad07bc5ee2014-11-12 13:42:52 -0800498 } else if (activeCall != null && activeCall.can(Connection.CAPABILITY_HOLD)) {
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700499 mCallsManager.holdCall(activeCall);
Santos Cordon68d1a6b2014-09-19 12:25:58 -0700500 return true;
501 }
502 } else if (chld == CHLD_TYPE_ADDHELDTOCONF) {
503 if (activeCall != null) {
Ihab Awad07bc5ee2014-11-12 13:42:52 -0800504 if (activeCall.can(Connection.CAPABILITY_MERGE_CONFERENCE)) {
Santos Cordon88a4a602014-09-29 19:32:21 -0700505 activeCall.mergeConference();
Santos Cordon68d1a6b2014-09-19 12:25:58 -0700506 return true;
Santos Cordon88a4a602014-09-29 19:32:21 -0700507 } else {
508 List<Call> conferenceable = activeCall.getConferenceableCalls();
509 if (!conferenceable.isEmpty()) {
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700510 mCallsManager.conference(activeCall, conferenceable.get(0));
Santos Cordon88a4a602014-09-29 19:32:21 -0700511 return true;
Brad Ebinger53855132015-10-30 10:58:19 -0700512 }
Santos Cordon68d1a6b2014-09-19 12:25:58 -0700513 }
514 }
515 }
516 return false;
517 }
518
519 private void enforceModifyPermission() {
Ihab Awad78a5e6b2015-02-06 10:13:05 -0800520 mContext.enforceCallingOrSelfPermission(
521 android.Manifest.permission.MODIFY_PHONE_STATE, null);
Santos Cordon68d1a6b2014-09-19 12:25:58 -0700522 }
523
Santos Cordon88a4a602014-09-29 19:32:21 -0700524 private void sendListOfCalls(boolean shouldLog) {
Ihab Awad78a5e6b2015-02-06 10:13:05 -0800525 Collection<Call> mCalls = mCallsManager.getCalls();
Santos Cordon33fff492014-09-25 14:57:01 -0700526 for (Call call : mCalls) {
527 // We don't send the parent conference call to the bluetooth device.
Tyler Gunn9365c272015-06-29 09:18:31 -0700528 // We do, however want to send conferences that have no children to the bluetooth
529 // device (e.g. IMS Conference).
530 if (!call.isConference() ||
531 (call.isConference() && call
532 .can(Connection.CAPABILITY_CONFERENCE_HAS_NO_CHILDREN))) {
Santos Cordon88a4a602014-09-29 19:32:21 -0700533 sendClccForCall(call, shouldLog);
Santos Cordon33fff492014-09-25 14:57:01 -0700534 }
535 }
536 sendClccEndMarker();
537 }
538
539 /**
540 * Sends a single clcc (C* List Current Calls) event for the specified call.
541 */
Santos Cordon88a4a602014-09-29 19:32:21 -0700542 private void sendClccForCall(Call call, boolean shouldLog) {
Ihab Awad78a5e6b2015-02-06 10:13:05 -0800543 boolean isForeground = mCallsManager.getForegroundCall() == call;
Santos Cordon33fff492014-09-25 14:57:01 -0700544 int state = convertCallState(call.getState(), isForeground);
Santos Cordon88a4a602014-09-29 19:32:21 -0700545 boolean isPartOfConference = false;
Tyler Gunn9365c272015-06-29 09:18:31 -0700546 boolean isConferenceWithNoChildren = call.isConference() && call
547 .can(Connection.CAPABILITY_CONFERENCE_HAS_NO_CHILDREN);
Nancy Chen05a9e402014-09-26 14:14:32 -0700548
549 if (state == CALL_STATE_IDLE) {
550 return;
551 }
552
Santos Cordon88a4a602014-09-29 19:32:21 -0700553 Call conferenceCall = call.getParentCall();
554 if (conferenceCall != null) {
555 isPartOfConference = true;
556
557 // Run some alternative states for Conference-level merge/swap support.
558 // Basically, if call supports swapping or merging at the conference-level, then we need
Ihab Awad07bc5ee2014-11-12 13:42:52 -0800559 // to expose the calls as having distinct states (ACTIVE vs CAPABILITY_HOLD) or the
560 // functionality won't show up on the bluetooth device.
Santos Cordon88a4a602014-09-29 19:32:21 -0700561
562 // Before doing any special logic, ensure that we are dealing with an ACTIVE call and
563 // that the conference itself has a notion of the current "active" child call.
564 Call activeChild = conferenceCall.getConferenceLevelActiveCall();
565 if (state == CALL_STATE_ACTIVE && activeChild != null) {
566 // Reevaluate state if we can MERGE or if we can SWAP without previously having
567 // MERGED.
568 boolean shouldReevaluateState =
Ihab Awad07bc5ee2014-11-12 13:42:52 -0800569 conferenceCall.can(Connection.CAPABILITY_MERGE_CONFERENCE) ||
570 (conferenceCall.can(Connection.CAPABILITY_SWAP_CONFERENCE) &&
Brad Ebinger53855132015-10-30 10:58:19 -0700571 !conferenceCall.wasConferencePreviouslyMerged());
Santos Cordon88a4a602014-09-29 19:32:21 -0700572
573 if (shouldReevaluateState) {
574 isPartOfConference = false;
575 if (call == activeChild) {
576 state = CALL_STATE_ACTIVE;
577 } else {
578 // At this point we know there is an "active" child and we know that it is
579 // not this call, so set it to HELD instead.
580 state = CALL_STATE_HELD;
581 }
582 }
583 }
Tyler Gunn9365c272015-06-29 09:18:31 -0700584 } else if (isConferenceWithNoChildren) {
585 // Handle the special case of an IMS conference call without conference event package
586 // support. The call will be marked as a conference, but the conference will not have
587 // child calls where conference event packages are not used by the carrier.
588 isPartOfConference = true;
Santos Cordon88a4a602014-09-29 19:32:21 -0700589 }
590
Nancy Chen05a9e402014-09-26 14:14:32 -0700591 int index = getIndexForCall(call);
592 int direction = call.isIncoming() ? 1 : 0;
Yorke Leefb70e1e2014-09-29 14:22:53 -0700593 final Uri addressUri;
594 if (call.getGatewayInfo() != null) {
595 addressUri = call.getGatewayInfo().getOriginalAddress();
596 } else {
597 addressUri = call.getHandle();
598 }
Santos Cordon33fff492014-09-25 14:57:01 -0700599 String address = addressUri == null ? null : addressUri.getSchemeSpecificPart();
600 int addressType = address == null ? -1 : PhoneNumberUtils.toaFromString(address);
601
Santos Cordon88a4a602014-09-29 19:32:21 -0700602 if (shouldLog) {
603 Log.i(this, "sending clcc for call %d, %d, %d, %b, %s, %d",
604 index, direction, state, isPartOfConference, Log.piiHandle(address),
605 addressType);
606 }
Yorke Leee241cd62014-10-09 13:41:19 -0700607
608 if (mBluetoothHeadset != null) {
609 mBluetoothHeadset.clccResponse(
610 index, direction, state, 0, isPartOfConference, address, addressType);
611 }
Santos Cordon33fff492014-09-25 14:57:01 -0700612 }
613
614 private void sendClccEndMarker() {
615 // End marker is recognized with an index value of 0. All other parameters are ignored.
Yorke Leee241cd62014-10-09 13:41:19 -0700616 if (mBluetoothHeadset != null) {
617 mBluetoothHeadset.clccResponse(0 /* index */, 0, 0, 0, false, null, 0);
618 }
Santos Cordon33fff492014-09-25 14:57:01 -0700619 }
620
621 /**
622 * Returns the caches index for the specified call. If no such index exists, then an index is
623 * given (smallest number starting from 1 that isn't already taken).
624 */
625 private int getIndexForCall(Call call) {
626 if (mClccIndexMap.containsKey(call)) {
627 return mClccIndexMap.get(call);
628 }
629
630 int i = 1; // Indexes for bluetooth clcc are 1-based.
631 while (mClccIndexMap.containsValue(i)) {
632 i++;
633 }
634
635 // NOTE: Indexes are removed in {@link #onCallRemoved}.
636 mClccIndexMap.put(call, i);
637 return i;
638 }
639
Tyler Gunncd685e52014-10-10 11:48:25 -0700640 /**
641 * Sends an update of the current call state to the current Headset.
642 *
643 * @param force {@code true} if the headset state should be sent regardless if no changes to the
644 * state have occurred, {@code false} if the state should only be sent if the state has
645 * changed.
646 */
647 private void updateHeadsetWithCallState(boolean force) {
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700648 Call activeCall = mCallsManager.getActiveCall();
649 Call ringingCall = mCallsManager.getRingingCall();
650 Call heldCall = mCallsManager.getHeldCall();
Santos Cordon68d1a6b2014-09-19 12:25:58 -0700651
652 int bluetoothCallState = getBluetoothCallStateForUpdate();
653
654 String ringingAddress = null;
655 int ringingAddressType = 128;
Santos Cordonecaaeac2014-11-05 20:59:04 -0800656 if (ringingCall != null && ringingCall.getHandle() != null) {
Santos Cordon68d1a6b2014-09-19 12:25:58 -0700657 ringingAddress = ringingCall.getHandle().getSchemeSpecificPart();
658 if (ringingAddress != null) {
659 ringingAddressType = PhoneNumberUtils.toaFromString(ringingAddress);
660 }
661 }
662 if (ringingAddress == null) {
663 ringingAddress = "";
664 }
665
Santos Cordona0b46122014-09-29 14:20:21 -0700666 int numActiveCalls = activeCall == null ? 0 : 1;
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700667 int numHeldCalls = mCallsManager.getNumHeldCalls();
Roshan Pius7d7cf272015-08-28 11:10:56 -0700668 // Intermediate state for GSM calls which are in the process of being swapped.
669 // TODO: Should we be hardcoding this value to 2 or should we check if all top level calls
670 // are held?
671 boolean callsPendingSwitch = (numHeldCalls == 2);
Santos Cordon68d1a6b2014-09-19 12:25:58 -0700672
Santos Cordon88a4a602014-09-29 19:32:21 -0700673 // For conference calls which support swapping the active call within the conference
674 // (namely CDMA calls) we need to expose that as a held call in order for the BT device
675 // to show "swap" and "merge" functionality.
Yorke Lee720bcbe2014-10-22 18:09:15 -0700676 boolean ignoreHeldCallChange = false;
Tyler Gunn9365c272015-06-29 09:18:31 -0700677 if (activeCall != null && activeCall.isConference() &&
678 !activeCall.can(Connection.CAPABILITY_CONFERENCE_HAS_NO_CHILDREN)) {
Ihab Awad07bc5ee2014-11-12 13:42:52 -0800679 if (activeCall.can(Connection.CAPABILITY_SWAP_CONFERENCE)) {
Santos Cordon88a4a602014-09-29 19:32:21 -0700680 // Indicate that BT device should show SWAP command by indicating that there is a
681 // call on hold, but only if the conference wasn't previously merged.
682 numHeldCalls = activeCall.wasConferencePreviouslyMerged() ? 0 : 1;
Ihab Awad07bc5ee2014-11-12 13:42:52 -0800683 } else if (activeCall.can(Connection.CAPABILITY_MERGE_CONFERENCE)) {
Santos Cordon88a4a602014-09-29 19:32:21 -0700684 numHeldCalls = 1; // Merge is available, so expose via numHeldCalls.
685 }
Yorke Lee720bcbe2014-10-22 18:09:15 -0700686
687 for (Call childCall : activeCall.getChildCalls()) {
688 // Held call has changed due to it being combined into a CDMA conference. Keep
689 // track of this and ignore any future update since it doesn't really count as
690 // a call change.
691 if (mOldHeldCall == childCall) {
692 ignoreHeldCallChange = true;
693 break;
694 }
695 }
Santos Cordon88a4a602014-09-29 19:32:21 -0700696 }
697
Santos Cordona0b46122014-09-29 14:20:21 -0700698 if (mBluetoothHeadset != null &&
Roshan Pius7d7cf272015-08-28 11:10:56 -0700699 (force ||
700 (!callsPendingSwitch &&
701 (numActiveCalls != mNumActiveCalls ||
Brad Ebinger53855132015-10-30 10:58:19 -0700702 numHeldCalls != mNumHeldCalls ||
703 bluetoothCallState != mBluetoothCallState ||
704 !TextUtils.equals(ringingAddress, mRingingAddress) ||
705 ringingAddressType != mRingingAddressType ||
706 (heldCall != mOldHeldCall && !ignoreHeldCallChange))))) {
Santos Cordon68d1a6b2014-09-19 12:25:58 -0700707
Santos Cordona0b46122014-09-29 14:20:21 -0700708 // If the call is transitioning into the alerting state, send DIALING first.
709 // Some devices expect to see a DIALING state prior to seeing an ALERTING state
710 // so we need to send it first.
711 boolean sendDialingFirst = mBluetoothCallState != bluetoothCallState &&
712 bluetoothCallState == CALL_STATE_ALERTING;
713
Santos Cordonc0ca76e2014-10-21 15:54:19 -0700714 mOldHeldCall = heldCall;
Santos Cordona0b46122014-09-29 14:20:21 -0700715 mNumActiveCalls = numActiveCalls;
716 mNumHeldCalls = numHeldCalls;
717 mBluetoothCallState = bluetoothCallState;
718 mRingingAddress = ringingAddress;
719 mRingingAddressType = ringingAddressType;
720
721 if (sendDialingFirst) {
Yorke Lee720bcbe2014-10-22 18:09:15 -0700722 // Log in full to make logs easier to debug.
723 Log.i(TAG, "updateHeadsetWithCallState " +
724 "numActive %s, " +
725 "numHeld %s, " +
726 "callState %s, " +
727 "ringing number %s, " +
728 "ringing type %s",
729 mNumActiveCalls,
730 mNumHeldCalls,
731 CALL_STATE_DIALING,
732 Log.pii(mRingingAddress),
733 mRingingAddressType);
Santos Cordona0b46122014-09-29 14:20:21 -0700734 mBluetoothHeadset.phoneStateChanged(
735 mNumActiveCalls,
736 mNumHeldCalls,
737 CALL_STATE_DIALING,
738 mRingingAddress,
739 mRingingAddressType);
740 }
741
742 Log.i(TAG, "updateHeadsetWithCallState " +
743 "numActive %s, " +
744 "numHeld %s, " +
745 "callState %s, " +
746 "ringing number %s, " +
747 "ringing type %s",
748 mNumActiveCalls,
749 mNumHeldCalls,
750 mBluetoothCallState,
751 Log.pii(mRingingAddress),
752 mRingingAddressType);
753
Santos Cordon68d1a6b2014-09-19 12:25:58 -0700754 mBluetoothHeadset.phoneStateChanged(
Santos Cordona0b46122014-09-29 14:20:21 -0700755 mNumActiveCalls,
756 mNumHeldCalls,
757 mBluetoothCallState,
758 mRingingAddress,
759 mRingingAddressType);
Santos Cordon88a4a602014-09-29 19:32:21 -0700760
761 mHeadsetUpdatedRecently = true;
Santos Cordon68d1a6b2014-09-19 12:25:58 -0700762 }
763 }
764
765 private int getBluetoothCallStateForUpdate() {
Ihab Awad78a5e6b2015-02-06 10:13:05 -0800766 CallsManager callsManager = mCallsManager;
Ihab Awad8d5d9dd2015-03-12 11:11:06 -0700767 Call ringingCall = mCallsManager.getRingingCall();
Roshan Pius7d7cf272015-08-28 11:10:56 -0700768 Call dialingCall = mCallsManager.getOutgoingCall();
Santos Cordon68d1a6b2014-09-19 12:25:58 -0700769
770 //
771 // !! WARNING !!
772 // You will note that CALL_STATE_WAITING, CALL_STATE_HELD, and CALL_STATE_ACTIVE are not
773 // used in this version of the call state mappings. This is on purpose.
774 // phone_state_change() in btif_hf.c is not written to handle these states. Only with the
775 // listCalls*() method are WAITING and ACTIVE used.
776 // Using the unsupported states here caused problems with inconsistent state in some
777 // bluetooth devices (like not getting out of ringing state after answering a call).
778 //
779 int bluetoothCallState = CALL_STATE_IDLE;
780 if (ringingCall != null) {
781 bluetoothCallState = CALL_STATE_INCOMING;
Nancy Chen05a9e402014-09-26 14:14:32 -0700782 } else if (dialingCall != null) {
Santos Cordon68d1a6b2014-09-19 12:25:58 -0700783 bluetoothCallState = CALL_STATE_ALERTING;
784 }
785 return bluetoothCallState;
786 }
787
Santos Cordon33fff492014-09-25 14:57:01 -0700788 private int convertCallState(int callState, boolean isForegroundCall) {
789 switch (callState) {
790 case CallState.NEW:
791 case CallState.ABORTED:
792 case CallState.DISCONNECTED:
793 return CALL_STATE_IDLE;
794
795 case CallState.ACTIVE:
796 return CALL_STATE_ACTIVE;
797
Santos Cordond9f90062015-10-28 15:55:34 -0700798 case CallState.CONNECTING:
799 case CallState.SELECT_PHONE_ACCOUNT:
Santos Cordon33fff492014-09-25 14:57:01 -0700800 case CallState.DIALING:
801 // Yes, this is correctly returning ALERTING.
802 // "Dialing" for BT means that we have sent information to the service provider
803 // to place the call but there is no confirmation that the call is going through.
804 // When there finally is confirmation, the ringback is played which is referred to
805 // as an "alert" tone, thus, ALERTING.
806 // TODO: We should consider using the ALERTING terms in Telecom because that
807 // seems to be more industry-standard.
808 return CALL_STATE_ALERTING;
809
810 case CallState.ON_HOLD:
811 return CALL_STATE_HELD;
812
813 case CallState.RINGING:
814 if (isForegroundCall) {
815 return CALL_STATE_INCOMING;
816 } else {
817 return CALL_STATE_WAITING;
818 }
819 }
820 return CALL_STATE_IDLE;
821 }
822
Santos Cordon68d1a6b2014-09-19 12:25:58 -0700823 /**
824 * Returns the best phone account to use for the given state of all calls.
825 * First, tries to return the phone account for the foreground call, second the default
826 * phone account for PhoneAccount.SCHEME_TEL.
827 */
828 private PhoneAccount getBestPhoneAccount() {
Ihab Awad78a5e6b2015-02-06 10:13:05 -0800829 if (mPhoneAccountRegistrar == null) {
Santos Cordon0b5cb4d2014-12-02 02:40:10 -0800830 return null;
831 }
832
Ihab Awad78a5e6b2015-02-06 10:13:05 -0800833 Call call = mCallsManager.getForegroundCall();
Santos Cordon68d1a6b2014-09-19 12:25:58 -0700834
835 PhoneAccount account = null;
836 if (call != null) {
837 // First try to get the network name of the foreground call.
Tony Mak240656f2015-12-04 11:36:22 +0000838 account = mPhoneAccountRegistrar.getPhoneAccountOfCurrentUser(
Santos Cordon6a212642015-05-08 16:35:23 -0700839 call.getTargetPhoneAccount());
Santos Cordon68d1a6b2014-09-19 12:25:58 -0700840 }
841
842 if (account == null) {
843 // Second, Try to get the label for the default Phone Account.
Tony Mak240656f2015-12-04 11:36:22 +0000844 account = mPhoneAccountRegistrar.getPhoneAccountUnchecked(
845 mPhoneAccountRegistrar.getOutgoingPhoneAccountForSchemeOfCurrentUser(
Brad Ebinger53855132015-10-30 10:58:19 -0700846 PhoneAccount.SCHEME_TEL));
Santos Cordon68d1a6b2014-09-19 12:25:58 -0700847 }
848 return account;
849 }
850}