Santos Cordon | 68d1a6b | 2014-09-19 12:25:58 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.server.telecom; |
| 18 | |
Santos Cordon | 68d1a6b | 2014-09-19 12:25:58 -0700 | [diff] [blame] | 19 | import android.bluetooth.BluetoothAdapter; |
| 20 | import android.bluetooth.BluetoothHeadset; |
| 21 | import android.bluetooth.BluetoothProfile; |
| 22 | import android.bluetooth.IBluetoothHeadsetPhone; |
| 23 | import android.content.BroadcastReceiver; |
| 24 | import android.content.Context; |
| 25 | import android.content.Intent; |
| 26 | import android.content.IntentFilter; |
| 27 | import android.net.Uri; |
Santos Cordon | ebf2d0f | 2015-05-15 10:28:29 -0700 | [diff] [blame] | 28 | import android.os.Binder; |
Santos Cordon | 68d1a6b | 2014-09-19 12:25:58 -0700 | [diff] [blame] | 29 | import android.os.IBinder; |
Santos Cordon | 68d1a6b | 2014-09-19 12:25:58 -0700 | [diff] [blame] | 30 | import android.os.RemoteException; |
Ihab Awad | 07bc5ee | 2014-11-12 13:42:52 -0800 | [diff] [blame] | 31 | import android.telecom.Connection; |
Brad Ebinger | 953e1af | 2016-10-05 15:45:22 -0700 | [diff] [blame] | 32 | import android.telecom.Log; |
Santos Cordon | 68d1a6b | 2014-09-19 12:25:58 -0700 | [diff] [blame] | 33 | import android.telecom.PhoneAccount; |
Hall Liu | 3cab92a | 2016-07-19 14:00:05 -0700 | [diff] [blame] | 34 | import android.telecom.VideoProfile; |
Santos Cordon | 68d1a6b | 2014-09-19 12:25:58 -0700 | [diff] [blame] | 35 | import android.telephony.PhoneNumberUtils; |
| 36 | import android.telephony.TelephonyManager; |
| 37 | import android.text.TextUtils; |
| 38 | |
Brad Ebinger | 5385513 | 2015-10-30 10:58:19 -0700 | [diff] [blame] | 39 | import com.android.internal.annotations.VisibleForTesting; |
Santos Cordon | 68d1a6b | 2014-09-19 12:25:58 -0700 | [diff] [blame] | 40 | import com.android.server.telecom.CallsManager.CallsManagerListener; |
| 41 | |
Santos Cordon | 33fff49 | 2014-09-25 14:57:01 -0700 | [diff] [blame] | 42 | import java.util.Collection; |
| 43 | import java.util.HashMap; |
Santos Cordon | 68d1a6b | 2014-09-19 12:25:58 -0700 | [diff] [blame] | 44 | import java.util.List; |
Santos Cordon | 33fff49 | 2014-09-25 14:57:01 -0700 | [diff] [blame] | 45 | import java.util.Map; |
Santos Cordon | 68d1a6b | 2014-09-19 12:25:58 -0700 | [diff] [blame] | 46 | |
| 47 | /** |
| 48 | * Bluetooth headset manager for Telecom. This class shares the call state with the bluetooth device |
| 49 | * and accepts call-related commands to perform on behalf of the BT device. |
| 50 | */ |
Hall Liu | b3979ee | 2015-11-11 16:21:25 -0800 | [diff] [blame] | 51 | public class BluetoothPhoneServiceImpl { |
| 52 | |
| 53 | public interface BluetoothPhoneServiceImplFactory { |
| 54 | BluetoothPhoneServiceImpl makeBluetoothPhoneServiceImpl(Context context, |
| 55 | TelecomSystem.SyncRoot lock, CallsManager callsManager, |
| 56 | PhoneAccountRegistrar phoneAccountRegistrar); |
| 57 | } |
Santos Cordon | 68d1a6b | 2014-09-19 12:25:58 -0700 | [diff] [blame] | 58 | |
| 59 | private static final String TAG = "BluetoothPhoneService"; |
| 60 | |
Santos Cordon | 68d1a6b | 2014-09-19 12:25:58 -0700 | [diff] [blame] | 61 | // match up with bthf_call_state_t of bt_hf.h |
| 62 | private static final int CALL_STATE_ACTIVE = 0; |
| 63 | private static final int CALL_STATE_HELD = 1; |
| 64 | private static final int CALL_STATE_DIALING = 2; |
| 65 | private static final int CALL_STATE_ALERTING = 3; |
| 66 | private static final int CALL_STATE_INCOMING = 4; |
| 67 | private static final int CALL_STATE_WAITING = 5; |
| 68 | private static final int CALL_STATE_IDLE = 6; |
| 69 | |
| 70 | // match up with bthf_call_state_t of bt_hf.h |
| 71 | // Terminate all held or set UDUB("busy") to a waiting call |
| 72 | private static final int CHLD_TYPE_RELEASEHELD = 0; |
| 73 | // Terminate all active calls and accepts a waiting/held call |
| 74 | private static final int CHLD_TYPE_RELEASEACTIVE_ACCEPTHELD = 1; |
| 75 | // Hold all active calls and accepts a waiting/held call |
| 76 | private static final int CHLD_TYPE_HOLDACTIVE_ACCEPTHELD = 2; |
| 77 | // Add all held calls to a conference |
| 78 | private static final int CHLD_TYPE_ADDHELDTOCONF = 3; |
| 79 | |
Santos Cordon | a0b4612 | 2014-09-29 14:20:21 -0700 | [diff] [blame] | 80 | private int mNumActiveCalls = 0; |
| 81 | private int mNumHeldCalls = 0; |
Hall Liu | 6a89ba2 | 2017-07-31 19:04:48 -0700 | [diff] [blame^] | 82 | private int mNumChildrenOfActiveCall = 0; |
Santos Cordon | a0b4612 | 2014-09-29 14:20:21 -0700 | [diff] [blame] | 83 | private int mBluetoothCallState = CALL_STATE_IDLE; |
| 84 | private String mRingingAddress = null; |
| 85 | private int mRingingAddressType = 0; |
Santos Cordon | c0ca76e | 2014-10-21 15:54:19 -0700 | [diff] [blame] | 86 | private Call mOldHeldCall = null; |
Santos Cordon | a0b4612 | 2014-09-29 14:20:21 -0700 | [diff] [blame] | 87 | |
Santos Cordon | 68d1a6b | 2014-09-19 12:25:58 -0700 | [diff] [blame] | 88 | /** |
| 89 | * Binder implementation of IBluetoothHeadsetPhone. Implements the command interface that the |
| 90 | * bluetooth headset code uses to control call. |
| 91 | */ |
Brad Ebinger | 5385513 | 2015-10-30 10:58:19 -0700 | [diff] [blame] | 92 | @VisibleForTesting |
| 93 | public final IBluetoothHeadsetPhone.Stub mBinder = new IBluetoothHeadsetPhone.Stub() { |
Santos Cordon | 68d1a6b | 2014-09-19 12:25:58 -0700 | [diff] [blame] | 94 | @Override |
| 95 | public boolean answerCall() throws RemoteException { |
Ihab Awad | 8d5d9dd | 2015-03-12 11:11:06 -0700 | [diff] [blame] | 96 | synchronized (mLock) { |
| 97 | enforceModifyPermission(); |
Brad Ebinger | 3165d50 | 2015-12-15 17:22:29 -0800 | [diff] [blame] | 98 | Log.startSession("BPSI.aC"); |
Santos Cordon | ebf2d0f | 2015-05-15 10:28:29 -0700 | [diff] [blame] | 99 | long token = Binder.clearCallingIdentity(); |
| 100 | try { |
| 101 | Log.i(TAG, "BT - answering call"); |
| 102 | Call call = mCallsManager.getRingingCall(); |
| 103 | if (call != null) { |
Hall Liu | 3cab92a | 2016-07-19 14:00:05 -0700 | [diff] [blame] | 104 | mCallsManager.answerCall(call, VideoProfile.STATE_AUDIO_ONLY); |
Santos Cordon | ebf2d0f | 2015-05-15 10:28:29 -0700 | [diff] [blame] | 105 | return true; |
| 106 | } |
| 107 | return false; |
| 108 | } finally { |
| 109 | Binder.restoreCallingIdentity(token); |
Brad Ebinger | 3165d50 | 2015-12-15 17:22:29 -0800 | [diff] [blame] | 110 | Log.endSession(); |
Ihab Awad | 8d5d9dd | 2015-03-12 11:11:06 -0700 | [diff] [blame] | 111 | } |
Santos Cordon | ebf2d0f | 2015-05-15 10:28:29 -0700 | [diff] [blame] | 112 | |
Ihab Awad | 8d5d9dd | 2015-03-12 11:11:06 -0700 | [diff] [blame] | 113 | } |
Santos Cordon | 68d1a6b | 2014-09-19 12:25:58 -0700 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | @Override |
| 117 | public boolean hangupCall() throws RemoteException { |
Ihab Awad | 8d5d9dd | 2015-03-12 11:11:06 -0700 | [diff] [blame] | 118 | synchronized (mLock) { |
| 119 | enforceModifyPermission(); |
Brad Ebinger | 3165d50 | 2015-12-15 17:22:29 -0800 | [diff] [blame] | 120 | Log.startSession("BPSI.hC"); |
Santos Cordon | ebf2d0f | 2015-05-15 10:28:29 -0700 | [diff] [blame] | 121 | long token = Binder.clearCallingIdentity(); |
| 122 | try { |
| 123 | Log.i(TAG, "BT - hanging up call"); |
| 124 | Call call = mCallsManager.getForegroundCall(); |
| 125 | if (call != null) { |
| 126 | mCallsManager.disconnectCall(call); |
| 127 | return true; |
| 128 | } |
| 129 | return false; |
| 130 | } finally { |
| 131 | Binder.restoreCallingIdentity(token); |
Brad Ebinger | 3165d50 | 2015-12-15 17:22:29 -0800 | [diff] [blame] | 132 | Log.endSession(); |
Ihab Awad | 8d5d9dd | 2015-03-12 11:11:06 -0700 | [diff] [blame] | 133 | } |
Ihab Awad | 8d5d9dd | 2015-03-12 11:11:06 -0700 | [diff] [blame] | 134 | } |
Santos Cordon | 68d1a6b | 2014-09-19 12:25:58 -0700 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | @Override |
| 138 | public boolean sendDtmf(int dtmf) throws RemoteException { |
Ihab Awad | 8d5d9dd | 2015-03-12 11:11:06 -0700 | [diff] [blame] | 139 | synchronized (mLock) { |
| 140 | enforceModifyPermission(); |
Brad Ebinger | 3165d50 | 2015-12-15 17:22:29 -0800 | [diff] [blame] | 141 | Log.startSession("BPSI.sD"); |
Santos Cordon | ebf2d0f | 2015-05-15 10:28:29 -0700 | [diff] [blame] | 142 | long token = Binder.clearCallingIdentity(); |
| 143 | try { |
| 144 | Log.i(TAG, "BT - sendDtmf %c", Log.DEBUG ? dtmf : '.'); |
| 145 | Call call = mCallsManager.getForegroundCall(); |
| 146 | if (call != null) { |
| 147 | // TODO: Consider making this a queue instead of starting/stopping |
| 148 | // in quick succession. |
| 149 | mCallsManager.playDtmfTone(call, (char) dtmf); |
| 150 | mCallsManager.stopDtmfTone(call); |
| 151 | return true; |
| 152 | } |
| 153 | return false; |
| 154 | } finally { |
| 155 | Binder.restoreCallingIdentity(token); |
Brad Ebinger | 3165d50 | 2015-12-15 17:22:29 -0800 | [diff] [blame] | 156 | Log.endSession(); |
Ihab Awad | 8d5d9dd | 2015-03-12 11:11:06 -0700 | [diff] [blame] | 157 | } |
Ihab Awad | 8d5d9dd | 2015-03-12 11:11:06 -0700 | [diff] [blame] | 158 | } |
Santos Cordon | 68d1a6b | 2014-09-19 12:25:58 -0700 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | @Override |
| 162 | public String getNetworkOperator() throws RemoteException { |
Ihab Awad | 8d5d9dd | 2015-03-12 11:11:06 -0700 | [diff] [blame] | 163 | synchronized (mLock) { |
| 164 | enforceModifyPermission(); |
Brad Ebinger | 3165d50 | 2015-12-15 17:22:29 -0800 | [diff] [blame] | 165 | Log.startSession("BPSI.gNO"); |
Santos Cordon | ebf2d0f | 2015-05-15 10:28:29 -0700 | [diff] [blame] | 166 | long token = Binder.clearCallingIdentity(); |
| 167 | try { |
| 168 | Log.i(TAG, "getNetworkOperator"); |
| 169 | PhoneAccount account = getBestPhoneAccount(); |
Santos Cordon | 22403a7 | 2016-04-12 16:51:55 -0700 | [diff] [blame] | 170 | if (account != null && account.getLabel() != null) { |
Santos Cordon | ebf2d0f | 2015-05-15 10:28:29 -0700 | [diff] [blame] | 171 | return account.getLabel().toString(); |
| 172 | } else { |
| 173 | // Finally, just get the network name from telephony. |
| 174 | return TelephonyManager.from(mContext) |
| 175 | .getNetworkOperatorName(); |
| 176 | } |
| 177 | } finally { |
| 178 | Binder.restoreCallingIdentity(token); |
Brad Ebinger | 3165d50 | 2015-12-15 17:22:29 -0800 | [diff] [blame] | 179 | Log.endSession(); |
Ihab Awad | 8d5d9dd | 2015-03-12 11:11:06 -0700 | [diff] [blame] | 180 | } |
| 181 | } |
Santos Cordon | 68d1a6b | 2014-09-19 12:25:58 -0700 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | @Override |
| 185 | public String getSubscriberNumber() throws RemoteException { |
Ihab Awad | 8d5d9dd | 2015-03-12 11:11:06 -0700 | [diff] [blame] | 186 | synchronized (mLock) { |
| 187 | enforceModifyPermission(); |
Brad Ebinger | 3165d50 | 2015-12-15 17:22:29 -0800 | [diff] [blame] | 188 | Log.startSession("BPSI.gSN"); |
Santos Cordon | ebf2d0f | 2015-05-15 10:28:29 -0700 | [diff] [blame] | 189 | long token = Binder.clearCallingIdentity(); |
| 190 | try { |
| 191 | Log.i(TAG, "getSubscriberNumber"); |
| 192 | String address = null; |
| 193 | PhoneAccount account = getBestPhoneAccount(); |
| 194 | if (account != null) { |
| 195 | Uri addressUri = account.getAddress(); |
| 196 | if (addressUri != null) { |
| 197 | address = addressUri.getSchemeSpecificPart(); |
| 198 | } |
Ihab Awad | 8d5d9dd | 2015-03-12 11:11:06 -0700 | [diff] [blame] | 199 | } |
Santos Cordon | ebf2d0f | 2015-05-15 10:28:29 -0700 | [diff] [blame] | 200 | if (TextUtils.isEmpty(address)) { |
| 201 | address = TelephonyManager.from(mContext).getLine1Number(); |
Andre Eisenbach | 3109262 | 2015-06-04 18:56:42 -0700 | [diff] [blame] | 202 | if (address == null) address = ""; |
Santos Cordon | ebf2d0f | 2015-05-15 10:28:29 -0700 | [diff] [blame] | 203 | } |
| 204 | return address; |
| 205 | } finally { |
| 206 | Binder.restoreCallingIdentity(token); |
Brad Ebinger | 3165d50 | 2015-12-15 17:22:29 -0800 | [diff] [blame] | 207 | Log.endSession(); |
Ihab Awad | 8d5d9dd | 2015-03-12 11:11:06 -0700 | [diff] [blame] | 208 | } |
Ihab Awad | 8d5d9dd | 2015-03-12 11:11:06 -0700 | [diff] [blame] | 209 | } |
Santos Cordon | 68d1a6b | 2014-09-19 12:25:58 -0700 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | @Override |
| 213 | public boolean listCurrentCalls() throws RemoteException { |
Ihab Awad | 8d5d9dd | 2015-03-12 11:11:06 -0700 | [diff] [blame] | 214 | synchronized (mLock) { |
| 215 | enforceModifyPermission(); |
Brad Ebinger | 3165d50 | 2015-12-15 17:22:29 -0800 | [diff] [blame] | 216 | Log.startSession("BPSI.lCC"); |
Santos Cordon | ebf2d0f | 2015-05-15 10:28:29 -0700 | [diff] [blame] | 217 | long token = Binder.clearCallingIdentity(); |
| 218 | try { |
| 219 | // only log if it is after we recently updated the headset state or else it can |
| 220 | // clog the android log since this can be queried every second. |
| 221 | boolean logQuery = mHeadsetUpdatedRecently; |
| 222 | mHeadsetUpdatedRecently = false; |
| 223 | |
| 224 | if (logQuery) { |
| 225 | Log.i(TAG, "listcurrentCalls"); |
| 226 | } |
| 227 | |
| 228 | sendListOfCalls(logQuery); |
| 229 | return true; |
| 230 | } finally { |
| 231 | Binder.restoreCallingIdentity(token); |
Brad Ebinger | 3165d50 | 2015-12-15 17:22:29 -0800 | [diff] [blame] | 232 | Log.endSession(); |
Ihab Awad | 8d5d9dd | 2015-03-12 11:11:06 -0700 | [diff] [blame] | 233 | } |
Santos Cordon | 88a4a60 | 2014-09-29 19:32:21 -0700 | [diff] [blame] | 234 | } |
Santos Cordon | 68d1a6b | 2014-09-19 12:25:58 -0700 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | @Override |
| 238 | public boolean queryPhoneState() throws RemoteException { |
Ihab Awad | 8d5d9dd | 2015-03-12 11:11:06 -0700 | [diff] [blame] | 239 | synchronized (mLock) { |
| 240 | enforceModifyPermission(); |
Brad Ebinger | 3165d50 | 2015-12-15 17:22:29 -0800 | [diff] [blame] | 241 | Log.startSession("BPSI.qPS"); |
Santos Cordon | ebf2d0f | 2015-05-15 10:28:29 -0700 | [diff] [blame] | 242 | long token = Binder.clearCallingIdentity(); |
| 243 | try { |
| 244 | Log.i(TAG, "queryPhoneState"); |
| 245 | updateHeadsetWithCallState(true /* force */); |
| 246 | return true; |
| 247 | } finally { |
| 248 | Binder.restoreCallingIdentity(token); |
Brad Ebinger | 3165d50 | 2015-12-15 17:22:29 -0800 | [diff] [blame] | 249 | Log.endSession(); |
Santos Cordon | ebf2d0f | 2015-05-15 10:28:29 -0700 | [diff] [blame] | 250 | } |
Ihab Awad | 8d5d9dd | 2015-03-12 11:11:06 -0700 | [diff] [blame] | 251 | } |
Santos Cordon | 68d1a6b | 2014-09-19 12:25:58 -0700 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | @Override |
| 255 | public boolean processChld(int chld) throws RemoteException { |
Ihab Awad | 8d5d9dd | 2015-03-12 11:11:06 -0700 | [diff] [blame] | 256 | synchronized (mLock) { |
| 257 | enforceModifyPermission(); |
Brad Ebinger | 3165d50 | 2015-12-15 17:22:29 -0800 | [diff] [blame] | 258 | Log.startSession("BPSI.pC"); |
Santos Cordon | ebf2d0f | 2015-05-15 10:28:29 -0700 | [diff] [blame] | 259 | long token = Binder.clearCallingIdentity(); |
| 260 | try { |
| 261 | Log.i(TAG, "processChld %d", chld); |
| 262 | return BluetoothPhoneServiceImpl.this.processChld(chld); |
| 263 | } finally { |
| 264 | Binder.restoreCallingIdentity(token); |
Brad Ebinger | 3165d50 | 2015-12-15 17:22:29 -0800 | [diff] [blame] | 265 | Log.endSession(); |
Santos Cordon | ebf2d0f | 2015-05-15 10:28:29 -0700 | [diff] [blame] | 266 | } |
Ihab Awad | 8d5d9dd | 2015-03-12 11:11:06 -0700 | [diff] [blame] | 267 | } |
Santos Cordon | 68d1a6b | 2014-09-19 12:25:58 -0700 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | @Override |
| 271 | public void updateBtHandsfreeAfterRadioTechnologyChange() throws RemoteException { |
Ihab Awad | 8d5d9dd | 2015-03-12 11:11:06 -0700 | [diff] [blame] | 272 | Log.d(TAG, "RAT change - deprecated"); |
Santos Cordon | 68d1a6b | 2014-09-19 12:25:58 -0700 | [diff] [blame] | 273 | // deprecated |
| 274 | } |
| 275 | |
| 276 | @Override |
| 277 | public void cdmaSetSecondCallState(boolean state) throws RemoteException { |
Ihab Awad | 8d5d9dd | 2015-03-12 11:11:06 -0700 | [diff] [blame] | 278 | Log.d(TAG, "cdma 1 - deprecated"); |
Santos Cordon | 68d1a6b | 2014-09-19 12:25:58 -0700 | [diff] [blame] | 279 | // deprecated |
| 280 | } |
| 281 | |
| 282 | @Override |
| 283 | public void cdmaSwapSecondCallState() throws RemoteException { |
Ihab Awad | 8d5d9dd | 2015-03-12 11:11:06 -0700 | [diff] [blame] | 284 | Log.d(TAG, "cdma 2 - deprecated"); |
Santos Cordon | 68d1a6b | 2014-09-19 12:25:58 -0700 | [diff] [blame] | 285 | // deprecated |
| 286 | } |
| 287 | }; |
| 288 | |
| 289 | /** |
Santos Cordon | 68d1a6b | 2014-09-19 12:25:58 -0700 | [diff] [blame] | 290 | * Listens to call changes from the CallsManager and calls into methods to update the bluetooth |
| 291 | * headset with the new states. |
| 292 | */ |
Brad Ebinger | 5385513 | 2015-10-30 10:58:19 -0700 | [diff] [blame] | 293 | @VisibleForTesting |
| 294 | public CallsManagerListener mCallsManagerListener = new CallsManagerListenerBase() { |
Santos Cordon | 68d1a6b | 2014-09-19 12:25:58 -0700 | [diff] [blame] | 295 | @Override |
| 296 | public void onCallAdded(Call call) { |
Tyler Gunn | f15dc33 | 2016-06-07 16:01:41 -0700 | [diff] [blame] | 297 | if (call.isExternalCall()) { |
| 298 | return; |
| 299 | } |
Tyler Gunn | cd685e5 | 2014-10-10 11:48:25 -0700 | [diff] [blame] | 300 | updateHeadsetWithCallState(false /* force */); |
Santos Cordon | 68d1a6b | 2014-09-19 12:25:58 -0700 | [diff] [blame] | 301 | } |
| 302 | |
| 303 | @Override |
| 304 | public void onCallRemoved(Call call) { |
Tyler Gunn | f15dc33 | 2016-06-07 16:01:41 -0700 | [diff] [blame] | 305 | if (call.isExternalCall()) { |
| 306 | return; |
| 307 | } |
Santos Cordon | 33fff49 | 2014-09-25 14:57:01 -0700 | [diff] [blame] | 308 | mClccIndexMap.remove(call); |
Tyler Gunn | cd685e5 | 2014-10-10 11:48:25 -0700 | [diff] [blame] | 309 | updateHeadsetWithCallState(false /* force */); |
Santos Cordon | 68d1a6b | 2014-09-19 12:25:58 -0700 | [diff] [blame] | 310 | } |
| 311 | |
Tyler Gunn | 1a40c4f | 2016-04-14 14:29:45 -0700 | [diff] [blame] | 312 | /** |
| 313 | * Where a call which was external becomes a regular call, or a regular call becomes |
| 314 | * external, treat as an add or remove, respectively. |
| 315 | * |
| 316 | * @param call The call. |
| 317 | * @param isExternalCall {@code True} if the call became external, {@code false} otherwise. |
| 318 | */ |
| 319 | @Override |
| 320 | public void onExternalCallChanged(Call call, boolean isExternalCall) { |
| 321 | if (isExternalCall) { |
| 322 | onCallRemoved(call); |
| 323 | } else { |
| 324 | onCallAdded(call); |
| 325 | } |
| 326 | } |
| 327 | |
Santos Cordon | 68d1a6b | 2014-09-19 12:25:58 -0700 | [diff] [blame] | 328 | @Override |
| 329 | public void onCallStateChanged(Call call, int oldState, int newState) { |
Tyler Gunn | f15dc33 | 2016-06-07 16:01:41 -0700 | [diff] [blame] | 330 | if (call.isExternalCall()) { |
| 331 | return; |
| 332 | } |
Yorke Lee | 720bcbe | 2014-10-22 18:09:15 -0700 | [diff] [blame] | 333 | // If a call is being put on hold because of a new connecting call, ignore the |
| 334 | // CONNECTING since the BT state update needs to send out the numHeld = 1 + dialing |
| 335 | // state atomically. |
| 336 | // When the call later transitions to DIALING/DISCONNECTED we will then send out the |
| 337 | // aggregated update. |
| 338 | if (oldState == CallState.ACTIVE && newState == CallState.ON_HOLD) { |
Ihab Awad | 78a5e6b | 2015-02-06 10:13:05 -0800 | [diff] [blame] | 339 | for (Call otherCall : mCallsManager.getCalls()) { |
Yorke Lee | 720bcbe | 2014-10-22 18:09:15 -0700 | [diff] [blame] | 340 | if (otherCall.getState() == CallState.CONNECTING) { |
| 341 | return; |
| 342 | } |
| 343 | } |
| 344 | } |
| 345 | |
| 346 | // To have an active call and another dialing at the same time is an invalid BT |
| 347 | // state. We can assume that the active call will be automatically held which will |
| 348 | // send another update at which point we will be in the right state. |
Ihab Awad | 78a5e6b | 2015-02-06 10:13:05 -0800 | [diff] [blame] | 349 | if (mCallsManager.getActiveCall() != null |
Tyler Gunn | 1e37be5 | 2016-07-11 08:54:23 -0700 | [diff] [blame] | 350 | && oldState == CallState.CONNECTING && |
| 351 | (newState == CallState.DIALING || newState == CallState.PULLING)) { |
Yorke Lee | 720bcbe | 2014-10-22 18:09:15 -0700 | [diff] [blame] | 352 | return; |
| 353 | } |
Tyler Gunn | cd685e5 | 2014-10-10 11:48:25 -0700 | [diff] [blame] | 354 | updateHeadsetWithCallState(false /* force */); |
Santos Cordon | 68d1a6b | 2014-09-19 12:25:58 -0700 | [diff] [blame] | 355 | } |
| 356 | |
| 357 | @Override |
Santos Cordon | 68d1a6b | 2014-09-19 12:25:58 -0700 | [diff] [blame] | 358 | public void onIsConferencedChanged(Call call) { |
Tyler Gunn | f15dc33 | 2016-06-07 16:01:41 -0700 | [diff] [blame] | 359 | if (call.isExternalCall()) { |
| 360 | return; |
| 361 | } |
Yorke Lee | 720bcbe | 2014-10-22 18:09:15 -0700 | [diff] [blame] | 362 | /* |
| 363 | * Filter certain onIsConferencedChanged callbacks. Unfortunately this needs to be done |
| 364 | * because conference change events are not atomic and multiple callbacks get fired |
| 365 | * when two calls are conferenced together. This confuses updateHeadsetWithCallState |
| 366 | * if it runs in the middle of two calls being conferenced and can cause spurious and |
| 367 | * incorrect headset state updates. One of the scenarios is described below for CDMA |
| 368 | * conference calls. |
| 369 | * |
| 370 | * 1) Call 1 and Call 2 are being merged into conference Call 3. |
| 371 | * 2) Call 1 has its parent set to Call 3, but Call 2 does not have a parent yet. |
| 372 | * 3) updateHeadsetWithCallState now thinks that there are two active calls (Call 2 and |
| 373 | * Call 3) when there is actually only one active call (Call 3). |
| 374 | */ |
| 375 | if (call.getParentCall() != null) { |
| 376 | // If this call is newly conferenced, ignore the callback. We only care about the |
| 377 | // one sent for the parent conference call. |
| 378 | Log.d(this, "Ignoring onIsConferenceChanged from child call with new parent"); |
| 379 | return; |
| 380 | } |
| 381 | if (call.getChildCalls().size() == 1) { |
| 382 | // If this is a parent call with only one child, ignore the callback as well since |
| 383 | // the minimum number of child calls to start a conference call is 2. We expect |
| 384 | // this to be called again when the parent call has another child call added. |
| 385 | Log.d(this, "Ignoring onIsConferenceChanged from parent with only one child call"); |
| 386 | return; |
| 387 | } |
Tyler Gunn | cd685e5 | 2014-10-10 11:48:25 -0700 | [diff] [blame] | 388 | updateHeadsetWithCallState(false /* force */); |
Santos Cordon | 68d1a6b | 2014-09-19 12:25:58 -0700 | [diff] [blame] | 389 | } |
| 390 | }; |
| 391 | |
| 392 | /** |
| 393 | * Listens to connections and disconnections of bluetooth headsets. We need to save the current |
| 394 | * bluetooth headset so that we know where to send call updates. |
| 395 | */ |
Brad Ebinger | 5385513 | 2015-10-30 10:58:19 -0700 | [diff] [blame] | 396 | @VisibleForTesting |
| 397 | public BluetoothProfile.ServiceListener mProfileListener = |
Santos Cordon | 68d1a6b | 2014-09-19 12:25:58 -0700 | [diff] [blame] | 398 | new BluetoothProfile.ServiceListener() { |
Brad Ebinger | 5385513 | 2015-10-30 10:58:19 -0700 | [diff] [blame] | 399 | @Override |
| 400 | public void onServiceConnected(int profile, BluetoothProfile proxy) { |
| 401 | synchronized (mLock) { |
| 402 | setBluetoothHeadset(new BluetoothHeadsetProxy((BluetoothHeadset) proxy)); |
| 403 | } |
| 404 | } |
Santos Cordon | 68d1a6b | 2014-09-19 12:25:58 -0700 | [diff] [blame] | 405 | |
Brad Ebinger | 5385513 | 2015-10-30 10:58:19 -0700 | [diff] [blame] | 406 | @Override |
| 407 | public void onServiceDisconnected(int profile) { |
| 408 | synchronized (mLock) { |
| 409 | mBluetoothHeadset = null; |
| 410 | } |
| 411 | } |
| 412 | }; |
Santos Cordon | 68d1a6b | 2014-09-19 12:25:58 -0700 | [diff] [blame] | 413 | |
| 414 | /** |
| 415 | * Receives events for global state changes of the bluetooth adapter. |
| 416 | */ |
Brad Ebinger | 7128602 | 2015-12-09 17:13:27 -0800 | [diff] [blame] | 417 | @VisibleForTesting |
| 418 | public final BroadcastReceiver mBluetoothAdapterReceiver = new BroadcastReceiver() { |
Santos Cordon | 68d1a6b | 2014-09-19 12:25:58 -0700 | [diff] [blame] | 419 | @Override |
| 420 | public void onReceive(Context context, Intent intent) { |
Ihab Awad | 8d5d9dd | 2015-03-12 11:11:06 -0700 | [diff] [blame] | 421 | synchronized (mLock) { |
| 422 | int state = intent |
| 423 | .getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR); |
| 424 | Log.d(TAG, "Bluetooth Adapter state: %d", state); |
| 425 | if (state == BluetoothAdapter.STATE_ON) { |
| 426 | try { |
| 427 | mBinder.queryPhoneState(); |
| 428 | } catch (RemoteException e) { |
| 429 | // Remote exception not expected |
| 430 | } |
| 431 | } |
Santos Cordon | 68d1a6b | 2014-09-19 12:25:58 -0700 | [diff] [blame] | 432 | } |
| 433 | } |
| 434 | }; |
| 435 | |
Hall Liu | 7948f5b | 2016-03-15 17:39:28 -0700 | [diff] [blame] | 436 | private BluetoothAdapterProxy mBluetoothAdapter; |
Brad Ebinger | 5385513 | 2015-10-30 10:58:19 -0700 | [diff] [blame] | 437 | private BluetoothHeadsetProxy mBluetoothHeadset; |
Santos Cordon | 68d1a6b | 2014-09-19 12:25:58 -0700 | [diff] [blame] | 438 | |
Santos Cordon | 33fff49 | 2014-09-25 14:57:01 -0700 | [diff] [blame] | 439 | // A map from Calls to indexes used to identify calls for CLCC (C* List Current Calls). |
| 440 | private Map<Call, Integer> mClccIndexMap = new HashMap<>(); |
| 441 | |
Santos Cordon | 88a4a60 | 2014-09-29 19:32:21 -0700 | [diff] [blame] | 442 | private boolean mHeadsetUpdatedRecently = false; |
| 443 | |
Ihab Awad | 8d5d9dd | 2015-03-12 11:11:06 -0700 | [diff] [blame] | 444 | private final Context mContext; |
| 445 | private final TelecomSystem.SyncRoot mLock; |
| 446 | private final CallsManager mCallsManager; |
| 447 | private final PhoneAccountRegistrar mPhoneAccountRegistrar; |
Santos Cordon | 68d1a6b | 2014-09-19 12:25:58 -0700 | [diff] [blame] | 448 | |
Ihab Awad | 78a5e6b | 2015-02-06 10:13:05 -0800 | [diff] [blame] | 449 | public IBinder getBinder() { |
Santos Cordon | 68d1a6b | 2014-09-19 12:25:58 -0700 | [diff] [blame] | 450 | return mBinder; |
| 451 | } |
| 452 | |
Ihab Awad | 78a5e6b | 2015-02-06 10:13:05 -0800 | [diff] [blame] | 453 | public BluetoothPhoneServiceImpl( |
| 454 | Context context, |
Ihab Awad | 8d5d9dd | 2015-03-12 11:11:06 -0700 | [diff] [blame] | 455 | TelecomSystem.SyncRoot lock, |
Ihab Awad | 78a5e6b | 2015-02-06 10:13:05 -0800 | [diff] [blame] | 456 | CallsManager callsManager, |
Hall Liu | 7948f5b | 2016-03-15 17:39:28 -0700 | [diff] [blame] | 457 | BluetoothAdapterProxy bluetoothAdapter, |
Ihab Awad | 78a5e6b | 2015-02-06 10:13:05 -0800 | [diff] [blame] | 458 | PhoneAccountRegistrar phoneAccountRegistrar) { |
| 459 | Log.d(this, "onCreate"); |
| 460 | |
| 461 | mContext = context; |
Ihab Awad | 8d5d9dd | 2015-03-12 11:11:06 -0700 | [diff] [blame] | 462 | mLock = lock; |
Ihab Awad | 78a5e6b | 2015-02-06 10:13:05 -0800 | [diff] [blame] | 463 | mCallsManager = callsManager; |
| 464 | mPhoneAccountRegistrar = phoneAccountRegistrar; |
Santos Cordon | 68d1a6b | 2014-09-19 12:25:58 -0700 | [diff] [blame] | 465 | |
Hall Liu | 7948f5b | 2016-03-15 17:39:28 -0700 | [diff] [blame] | 466 | mBluetoothAdapter = bluetoothAdapter; |
Santos Cordon | 68d1a6b | 2014-09-19 12:25:58 -0700 | [diff] [blame] | 467 | if (mBluetoothAdapter == null) { |
Ihab Awad | 78a5e6b | 2015-02-06 10:13:05 -0800 | [diff] [blame] | 468 | Log.d(this, "BluetoothPhoneService shutting down, no BT Adapter found."); |
Santos Cordon | 68d1a6b | 2014-09-19 12:25:58 -0700 | [diff] [blame] | 469 | return; |
| 470 | } |
Ihab Awad | 78a5e6b | 2015-02-06 10:13:05 -0800 | [diff] [blame] | 471 | mBluetoothAdapter.getProfileProxy(context, mProfileListener, BluetoothProfile.HEADSET); |
Santos Cordon | 68d1a6b | 2014-09-19 12:25:58 -0700 | [diff] [blame] | 472 | |
| 473 | IntentFilter intentFilter = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED); |
Ihab Awad | 78a5e6b | 2015-02-06 10:13:05 -0800 | [diff] [blame] | 474 | context.registerReceiver(mBluetoothAdapterReceiver, intentFilter); |
Santos Cordon | 68d1a6b | 2014-09-19 12:25:58 -0700 | [diff] [blame] | 475 | |
Ihab Awad | 78a5e6b | 2015-02-06 10:13:05 -0800 | [diff] [blame] | 476 | mCallsManager.addListener(mCallsManagerListener); |
Tyler Gunn | cd685e5 | 2014-10-10 11:48:25 -0700 | [diff] [blame] | 477 | updateHeadsetWithCallState(false /* force */); |
Santos Cordon | 68d1a6b | 2014-09-19 12:25:58 -0700 | [diff] [blame] | 478 | } |
| 479 | |
Brad Ebinger | 5385513 | 2015-10-30 10:58:19 -0700 | [diff] [blame] | 480 | @VisibleForTesting |
| 481 | public void setBluetoothHeadset(BluetoothHeadsetProxy bluetoothHeadset) { |
| 482 | mBluetoothHeadset = bluetoothHeadset; |
| 483 | } |
| 484 | |
Santos Cordon | 68d1a6b | 2014-09-19 12:25:58 -0700 | [diff] [blame] | 485 | private boolean processChld(int chld) { |
Ihab Awad | 8d5d9dd | 2015-03-12 11:11:06 -0700 | [diff] [blame] | 486 | Call activeCall = mCallsManager.getActiveCall(); |
| 487 | Call ringingCall = mCallsManager.getRingingCall(); |
| 488 | Call heldCall = mCallsManager.getHeldCall(); |
Santos Cordon | 68d1a6b | 2014-09-19 12:25:58 -0700 | [diff] [blame] | 489 | |
Santos Cordon | 66fe882 | 2014-10-10 16:10:58 -0700 | [diff] [blame] | 490 | // TODO: Keeping as Log.i for now. Move to Log.d after L release if BT proves stable. |
| 491 | Log.i(TAG, "Active: %s\nRinging: %s\nHeld: %s", activeCall, ringingCall, heldCall); |
Santos Cordon | 88a4a60 | 2014-09-29 19:32:21 -0700 | [diff] [blame] | 492 | |
Santos Cordon | 68d1a6b | 2014-09-19 12:25:58 -0700 | [diff] [blame] | 493 | if (chld == CHLD_TYPE_RELEASEHELD) { |
| 494 | if (ringingCall != null) { |
Ihab Awad | 8d5d9dd | 2015-03-12 11:11:06 -0700 | [diff] [blame] | 495 | mCallsManager.rejectCall(ringingCall, false, null); |
Santos Cordon | 68d1a6b | 2014-09-19 12:25:58 -0700 | [diff] [blame] | 496 | return true; |
| 497 | } else if (heldCall != null) { |
Ihab Awad | 8d5d9dd | 2015-03-12 11:11:06 -0700 | [diff] [blame] | 498 | mCallsManager.disconnectCall(heldCall); |
Santos Cordon | 68d1a6b | 2014-09-19 12:25:58 -0700 | [diff] [blame] | 499 | return true; |
| 500 | } |
| 501 | } else if (chld == CHLD_TYPE_RELEASEACTIVE_ACCEPTHELD) { |
Satish Kodishala | f5bdfb5 | 2015-12-30 14:17:28 +0530 | [diff] [blame] | 502 | if (activeCall == null && ringingCall == null && heldCall == null) |
| 503 | return false; |
Santos Cordon | 68d1a6b | 2014-09-19 12:25:58 -0700 | [diff] [blame] | 504 | if (activeCall != null) { |
Ihab Awad | 8d5d9dd | 2015-03-12 11:11:06 -0700 | [diff] [blame] | 505 | mCallsManager.disconnectCall(activeCall); |
Santos Cordon | 68d1a6b | 2014-09-19 12:25:58 -0700 | [diff] [blame] | 506 | if (ringingCall != null) { |
Hall Liu | 3cab92a | 2016-07-19 14:00:05 -0700 | [diff] [blame] | 507 | mCallsManager.answerCall(ringingCall, VideoProfile.STATE_AUDIO_ONLY); |
Santos Cordon | 68d1a6b | 2014-09-19 12:25:58 -0700 | [diff] [blame] | 508 | } else if (heldCall != null) { |
Ihab Awad | 8d5d9dd | 2015-03-12 11:11:06 -0700 | [diff] [blame] | 509 | mCallsManager.unholdCall(heldCall); |
Santos Cordon | 68d1a6b | 2014-09-19 12:25:58 -0700 | [diff] [blame] | 510 | } |
| 511 | return true; |
| 512 | } |
Satish Kodishala | f5bdfb5 | 2015-12-30 14:17:28 +0530 | [diff] [blame] | 513 | if (ringingCall != null) { |
| 514 | mCallsManager.answerCall(ringingCall, ringingCall.getVideoState()); |
| 515 | } else if (heldCall != null) { |
| 516 | mCallsManager.unholdCall(heldCall); |
| 517 | } |
| 518 | return true; |
Santos Cordon | 68d1a6b | 2014-09-19 12:25:58 -0700 | [diff] [blame] | 519 | } else if (chld == CHLD_TYPE_HOLDACTIVE_ACCEPTHELD) { |
Ihab Awad | 07bc5ee | 2014-11-12 13:42:52 -0800 | [diff] [blame] | 520 | if (activeCall != null && activeCall.can(Connection.CAPABILITY_SWAP_CONFERENCE)) { |
Santos Cordon | 88a4a60 | 2014-09-29 19:32:21 -0700 | [diff] [blame] | 521 | activeCall.swapConference(); |
Mallikarjuna GB | 0ae2df8 | 2015-06-04 18:20:21 +0530 | [diff] [blame] | 522 | Log.i(TAG, "CDMA calls in conference swapped, updating headset"); |
| 523 | updateHeadsetWithCallState(true /* force */); |
Santos Cordon | 88a4a60 | 2014-09-29 19:32:21 -0700 | [diff] [blame] | 524 | return true; |
| 525 | } else if (ringingCall != null) { |
Hall Liu | 3cab92a | 2016-07-19 14:00:05 -0700 | [diff] [blame] | 526 | mCallsManager.answerCall(ringingCall, VideoProfile.STATE_AUDIO_ONLY); |
Santos Cordon | 68d1a6b | 2014-09-19 12:25:58 -0700 | [diff] [blame] | 527 | return true; |
| 528 | } else if (heldCall != null) { |
| 529 | // CallsManager will hold any active calls when unhold() is called on a |
| 530 | // currently-held call. |
Ihab Awad | 8d5d9dd | 2015-03-12 11:11:06 -0700 | [diff] [blame] | 531 | mCallsManager.unholdCall(heldCall); |
Santos Cordon | 68d1a6b | 2014-09-19 12:25:58 -0700 | [diff] [blame] | 532 | return true; |
Ihab Awad | 07bc5ee | 2014-11-12 13:42:52 -0800 | [diff] [blame] | 533 | } else if (activeCall != null && activeCall.can(Connection.CAPABILITY_HOLD)) { |
Ihab Awad | 8d5d9dd | 2015-03-12 11:11:06 -0700 | [diff] [blame] | 534 | mCallsManager.holdCall(activeCall); |
Santos Cordon | 68d1a6b | 2014-09-19 12:25:58 -0700 | [diff] [blame] | 535 | return true; |
| 536 | } |
| 537 | } else if (chld == CHLD_TYPE_ADDHELDTOCONF) { |
| 538 | if (activeCall != null) { |
Ihab Awad | 07bc5ee | 2014-11-12 13:42:52 -0800 | [diff] [blame] | 539 | if (activeCall.can(Connection.CAPABILITY_MERGE_CONFERENCE)) { |
Santos Cordon | 88a4a60 | 2014-09-29 19:32:21 -0700 | [diff] [blame] | 540 | activeCall.mergeConference(); |
Santos Cordon | 68d1a6b | 2014-09-19 12:25:58 -0700 | [diff] [blame] | 541 | return true; |
Santos Cordon | 88a4a60 | 2014-09-29 19:32:21 -0700 | [diff] [blame] | 542 | } else { |
| 543 | List<Call> conferenceable = activeCall.getConferenceableCalls(); |
| 544 | if (!conferenceable.isEmpty()) { |
Ihab Awad | 8d5d9dd | 2015-03-12 11:11:06 -0700 | [diff] [blame] | 545 | mCallsManager.conference(activeCall, conferenceable.get(0)); |
Santos Cordon | 88a4a60 | 2014-09-29 19:32:21 -0700 | [diff] [blame] | 546 | return true; |
Brad Ebinger | 5385513 | 2015-10-30 10:58:19 -0700 | [diff] [blame] | 547 | } |
Santos Cordon | 68d1a6b | 2014-09-19 12:25:58 -0700 | [diff] [blame] | 548 | } |
| 549 | } |
| 550 | } |
| 551 | return false; |
| 552 | } |
| 553 | |
| 554 | private void enforceModifyPermission() { |
Ihab Awad | 78a5e6b | 2015-02-06 10:13:05 -0800 | [diff] [blame] | 555 | mContext.enforceCallingOrSelfPermission( |
| 556 | android.Manifest.permission.MODIFY_PHONE_STATE, null); |
Santos Cordon | 68d1a6b | 2014-09-19 12:25:58 -0700 | [diff] [blame] | 557 | } |
| 558 | |
Santos Cordon | 88a4a60 | 2014-09-29 19:32:21 -0700 | [diff] [blame] | 559 | private void sendListOfCalls(boolean shouldLog) { |
Ihab Awad | 78a5e6b | 2015-02-06 10:13:05 -0800 | [diff] [blame] | 560 | Collection<Call> mCalls = mCallsManager.getCalls(); |
Santos Cordon | 33fff49 | 2014-09-25 14:57:01 -0700 | [diff] [blame] | 561 | for (Call call : mCalls) { |
| 562 | // We don't send the parent conference call to the bluetooth device. |
Tyler Gunn | 9365c27 | 2015-06-29 09:18:31 -0700 | [diff] [blame] | 563 | // We do, however want to send conferences that have no children to the bluetooth |
| 564 | // device (e.g. IMS Conference). |
| 565 | if (!call.isConference() || |
| 566 | (call.isConference() && call |
| 567 | .can(Connection.CAPABILITY_CONFERENCE_HAS_NO_CHILDREN))) { |
Santos Cordon | 88a4a60 | 2014-09-29 19:32:21 -0700 | [diff] [blame] | 568 | sendClccForCall(call, shouldLog); |
Santos Cordon | 33fff49 | 2014-09-25 14:57:01 -0700 | [diff] [blame] | 569 | } |
| 570 | } |
| 571 | sendClccEndMarker(); |
| 572 | } |
| 573 | |
| 574 | /** |
| 575 | * Sends a single clcc (C* List Current Calls) event for the specified call. |
| 576 | */ |
Santos Cordon | 88a4a60 | 2014-09-29 19:32:21 -0700 | [diff] [blame] | 577 | private void sendClccForCall(Call call, boolean shouldLog) { |
Ihab Awad | 78a5e6b | 2015-02-06 10:13:05 -0800 | [diff] [blame] | 578 | boolean isForeground = mCallsManager.getForegroundCall() == call; |
Santos Cordon | 33fff49 | 2014-09-25 14:57:01 -0700 | [diff] [blame] | 579 | int state = convertCallState(call.getState(), isForeground); |
Santos Cordon | 88a4a60 | 2014-09-29 19:32:21 -0700 | [diff] [blame] | 580 | boolean isPartOfConference = false; |
Tyler Gunn | 9365c27 | 2015-06-29 09:18:31 -0700 | [diff] [blame] | 581 | boolean isConferenceWithNoChildren = call.isConference() && call |
| 582 | .can(Connection.CAPABILITY_CONFERENCE_HAS_NO_CHILDREN); |
Nancy Chen | 05a9e40 | 2014-09-26 14:14:32 -0700 | [diff] [blame] | 583 | |
| 584 | if (state == CALL_STATE_IDLE) { |
| 585 | return; |
| 586 | } |
| 587 | |
Santos Cordon | 88a4a60 | 2014-09-29 19:32:21 -0700 | [diff] [blame] | 588 | Call conferenceCall = call.getParentCall(); |
| 589 | if (conferenceCall != null) { |
| 590 | isPartOfConference = true; |
| 591 | |
| 592 | // Run some alternative states for Conference-level merge/swap support. |
| 593 | // Basically, if call supports swapping or merging at the conference-level, then we need |
Ihab Awad | 07bc5ee | 2014-11-12 13:42:52 -0800 | [diff] [blame] | 594 | // to expose the calls as having distinct states (ACTIVE vs CAPABILITY_HOLD) or the |
| 595 | // functionality won't show up on the bluetooth device. |
Santos Cordon | 88a4a60 | 2014-09-29 19:32:21 -0700 | [diff] [blame] | 596 | |
| 597 | // Before doing any special logic, ensure that we are dealing with an ACTIVE call and |
| 598 | // that the conference itself has a notion of the current "active" child call. |
| 599 | Call activeChild = conferenceCall.getConferenceLevelActiveCall(); |
| 600 | if (state == CALL_STATE_ACTIVE && activeChild != null) { |
| 601 | // Reevaluate state if we can MERGE or if we can SWAP without previously having |
| 602 | // MERGED. |
| 603 | boolean shouldReevaluateState = |
Ihab Awad | 07bc5ee | 2014-11-12 13:42:52 -0800 | [diff] [blame] | 604 | conferenceCall.can(Connection.CAPABILITY_MERGE_CONFERENCE) || |
| 605 | (conferenceCall.can(Connection.CAPABILITY_SWAP_CONFERENCE) && |
Brad Ebinger | 5385513 | 2015-10-30 10:58:19 -0700 | [diff] [blame] | 606 | !conferenceCall.wasConferencePreviouslyMerged()); |
Santos Cordon | 88a4a60 | 2014-09-29 19:32:21 -0700 | [diff] [blame] | 607 | |
| 608 | if (shouldReevaluateState) { |
| 609 | isPartOfConference = false; |
| 610 | if (call == activeChild) { |
| 611 | state = CALL_STATE_ACTIVE; |
| 612 | } else { |
| 613 | // At this point we know there is an "active" child and we know that it is |
| 614 | // not this call, so set it to HELD instead. |
| 615 | state = CALL_STATE_HELD; |
| 616 | } |
| 617 | } |
| 618 | } |
Hall Liu | 6fccadb | 2017-03-07 18:01:10 -0800 | [diff] [blame] | 619 | if (conferenceCall.getState() == CallState.ON_HOLD && |
| 620 | conferenceCall.can(Connection.CAPABILITY_MANAGE_CONFERENCE)) { |
| 621 | // If the parent IMS CEP conference call is on hold, we should mark this call as |
| 622 | // being on hold regardless of what the other children are doing. |
| 623 | state = CALL_STATE_HELD; |
| 624 | } |
Tyler Gunn | 9365c27 | 2015-06-29 09:18:31 -0700 | [diff] [blame] | 625 | } else if (isConferenceWithNoChildren) { |
| 626 | // Handle the special case of an IMS conference call without conference event package |
| 627 | // support. The call will be marked as a conference, but the conference will not have |
| 628 | // child calls where conference event packages are not used by the carrier. |
| 629 | isPartOfConference = true; |
Santos Cordon | 88a4a60 | 2014-09-29 19:32:21 -0700 | [diff] [blame] | 630 | } |
| 631 | |
Nancy Chen | 05a9e40 | 2014-09-26 14:14:32 -0700 | [diff] [blame] | 632 | int index = getIndexForCall(call); |
| 633 | int direction = call.isIncoming() ? 1 : 0; |
Yorke Lee | fb70e1e | 2014-09-29 14:22:53 -0700 | [diff] [blame] | 634 | final Uri addressUri; |
| 635 | if (call.getGatewayInfo() != null) { |
| 636 | addressUri = call.getGatewayInfo().getOriginalAddress(); |
| 637 | } else { |
| 638 | addressUri = call.getHandle(); |
| 639 | } |
Satish Kodishala | f4bd7c2 | 2016-11-18 12:01:51 +0530 | [diff] [blame] | 640 | |
Santos Cordon | 33fff49 | 2014-09-25 14:57:01 -0700 | [diff] [blame] | 641 | String address = addressUri == null ? null : addressUri.getSchemeSpecificPart(); |
Satish Kodishala | f4bd7c2 | 2016-11-18 12:01:51 +0530 | [diff] [blame] | 642 | if (address != null) { |
| 643 | address = PhoneNumberUtils.stripSeparators(address); |
| 644 | } |
| 645 | |
Santos Cordon | 33fff49 | 2014-09-25 14:57:01 -0700 | [diff] [blame] | 646 | int addressType = address == null ? -1 : PhoneNumberUtils.toaFromString(address); |
| 647 | |
Santos Cordon | 88a4a60 | 2014-09-29 19:32:21 -0700 | [diff] [blame] | 648 | if (shouldLog) { |
| 649 | Log.i(this, "sending clcc for call %d, %d, %d, %b, %s, %d", |
| 650 | index, direction, state, isPartOfConference, Log.piiHandle(address), |
| 651 | addressType); |
| 652 | } |
Yorke Lee | e241cd6 | 2014-10-09 13:41:19 -0700 | [diff] [blame] | 653 | |
| 654 | if (mBluetoothHeadset != null) { |
| 655 | mBluetoothHeadset.clccResponse( |
| 656 | index, direction, state, 0, isPartOfConference, address, addressType); |
| 657 | } |
Santos Cordon | 33fff49 | 2014-09-25 14:57:01 -0700 | [diff] [blame] | 658 | } |
| 659 | |
| 660 | private void sendClccEndMarker() { |
| 661 | // End marker is recognized with an index value of 0. All other parameters are ignored. |
Yorke Lee | e241cd6 | 2014-10-09 13:41:19 -0700 | [diff] [blame] | 662 | if (mBluetoothHeadset != null) { |
| 663 | mBluetoothHeadset.clccResponse(0 /* index */, 0, 0, 0, false, null, 0); |
| 664 | } |
Santos Cordon | 33fff49 | 2014-09-25 14:57:01 -0700 | [diff] [blame] | 665 | } |
| 666 | |
| 667 | /** |
| 668 | * Returns the caches index for the specified call. If no such index exists, then an index is |
| 669 | * given (smallest number starting from 1 that isn't already taken). |
| 670 | */ |
| 671 | private int getIndexForCall(Call call) { |
| 672 | if (mClccIndexMap.containsKey(call)) { |
| 673 | return mClccIndexMap.get(call); |
| 674 | } |
| 675 | |
| 676 | int i = 1; // Indexes for bluetooth clcc are 1-based. |
| 677 | while (mClccIndexMap.containsValue(i)) { |
| 678 | i++; |
| 679 | } |
| 680 | |
| 681 | // NOTE: Indexes are removed in {@link #onCallRemoved}. |
| 682 | mClccIndexMap.put(call, i); |
| 683 | return i; |
| 684 | } |
| 685 | |
Tyler Gunn | cd685e5 | 2014-10-10 11:48:25 -0700 | [diff] [blame] | 686 | /** |
| 687 | * Sends an update of the current call state to the current Headset. |
| 688 | * |
| 689 | * @param force {@code true} if the headset state should be sent regardless if no changes to the |
| 690 | * state have occurred, {@code false} if the state should only be sent if the state has |
| 691 | * changed. |
| 692 | */ |
| 693 | private void updateHeadsetWithCallState(boolean force) { |
Ihab Awad | 8d5d9dd | 2015-03-12 11:11:06 -0700 | [diff] [blame] | 694 | Call activeCall = mCallsManager.getActiveCall(); |
| 695 | Call ringingCall = mCallsManager.getRingingCall(); |
| 696 | Call heldCall = mCallsManager.getHeldCall(); |
Santos Cordon | 68d1a6b | 2014-09-19 12:25:58 -0700 | [diff] [blame] | 697 | |
| 698 | int bluetoothCallState = getBluetoothCallStateForUpdate(); |
| 699 | |
| 700 | String ringingAddress = null; |
| 701 | int ringingAddressType = 128; |
Santos Cordon | ecaaeac | 2014-11-05 20:59:04 -0800 | [diff] [blame] | 702 | if (ringingCall != null && ringingCall.getHandle() != null) { |
Santos Cordon | 68d1a6b | 2014-09-19 12:25:58 -0700 | [diff] [blame] | 703 | ringingAddress = ringingCall.getHandle().getSchemeSpecificPart(); |
| 704 | if (ringingAddress != null) { |
| 705 | ringingAddressType = PhoneNumberUtils.toaFromString(ringingAddress); |
| 706 | } |
| 707 | } |
| 708 | if (ringingAddress == null) { |
| 709 | ringingAddress = ""; |
| 710 | } |
| 711 | |
Santos Cordon | a0b4612 | 2014-09-29 14:20:21 -0700 | [diff] [blame] | 712 | int numActiveCalls = activeCall == null ? 0 : 1; |
Ihab Awad | 8d5d9dd | 2015-03-12 11:11:06 -0700 | [diff] [blame] | 713 | int numHeldCalls = mCallsManager.getNumHeldCalls(); |
Hall Liu | 6a89ba2 | 2017-07-31 19:04:48 -0700 | [diff] [blame^] | 714 | int numChildrenOfActiveCall = activeCall == null ? 0 : activeCall.getChildCalls().size(); |
| 715 | |
Roshan Pius | 7d7cf27 | 2015-08-28 11:10:56 -0700 | [diff] [blame] | 716 | // Intermediate state for GSM calls which are in the process of being swapped. |
| 717 | // TODO: Should we be hardcoding this value to 2 or should we check if all top level calls |
| 718 | // are held? |
| 719 | boolean callsPendingSwitch = (numHeldCalls == 2); |
Santos Cordon | 68d1a6b | 2014-09-19 12:25:58 -0700 | [diff] [blame] | 720 | |
Santos Cordon | 88a4a60 | 2014-09-29 19:32:21 -0700 | [diff] [blame] | 721 | // For conference calls which support swapping the active call within the conference |
| 722 | // (namely CDMA calls) we need to expose that as a held call in order for the BT device |
| 723 | // to show "swap" and "merge" functionality. |
Yorke Lee | 720bcbe | 2014-10-22 18:09:15 -0700 | [diff] [blame] | 724 | boolean ignoreHeldCallChange = false; |
Tyler Gunn | 9365c27 | 2015-06-29 09:18:31 -0700 | [diff] [blame] | 725 | if (activeCall != null && activeCall.isConference() && |
| 726 | !activeCall.can(Connection.CAPABILITY_CONFERENCE_HAS_NO_CHILDREN)) { |
Ihab Awad | 07bc5ee | 2014-11-12 13:42:52 -0800 | [diff] [blame] | 727 | if (activeCall.can(Connection.CAPABILITY_SWAP_CONFERENCE)) { |
Santos Cordon | 88a4a60 | 2014-09-29 19:32:21 -0700 | [diff] [blame] | 728 | // Indicate that BT device should show SWAP command by indicating that there is a |
| 729 | // call on hold, but only if the conference wasn't previously merged. |
| 730 | numHeldCalls = activeCall.wasConferencePreviouslyMerged() ? 0 : 1; |
Ihab Awad | 07bc5ee | 2014-11-12 13:42:52 -0800 | [diff] [blame] | 731 | } else if (activeCall.can(Connection.CAPABILITY_MERGE_CONFERENCE)) { |
Santos Cordon | 88a4a60 | 2014-09-29 19:32:21 -0700 | [diff] [blame] | 732 | numHeldCalls = 1; // Merge is available, so expose via numHeldCalls. |
| 733 | } |
Yorke Lee | 720bcbe | 2014-10-22 18:09:15 -0700 | [diff] [blame] | 734 | |
| 735 | for (Call childCall : activeCall.getChildCalls()) { |
| 736 | // Held call has changed due to it being combined into a CDMA conference. Keep |
| 737 | // track of this and ignore any future update since it doesn't really count as |
| 738 | // a call change. |
| 739 | if (mOldHeldCall == childCall) { |
| 740 | ignoreHeldCallChange = true; |
| 741 | break; |
| 742 | } |
| 743 | } |
Santos Cordon | 88a4a60 | 2014-09-29 19:32:21 -0700 | [diff] [blame] | 744 | } |
| 745 | |
Santos Cordon | a0b4612 | 2014-09-29 14:20:21 -0700 | [diff] [blame] | 746 | if (mBluetoothHeadset != null && |
Roshan Pius | 7d7cf27 | 2015-08-28 11:10:56 -0700 | [diff] [blame] | 747 | (force || |
| 748 | (!callsPendingSwitch && |
| 749 | (numActiveCalls != mNumActiveCalls || |
Hall Liu | 6a89ba2 | 2017-07-31 19:04:48 -0700 | [diff] [blame^] | 750 | numChildrenOfActiveCall != mNumChildrenOfActiveCall || |
| 751 | numHeldCalls != mNumHeldCalls || |
| 752 | bluetoothCallState != mBluetoothCallState || |
| 753 | !TextUtils.equals(ringingAddress, mRingingAddress) || |
| 754 | ringingAddressType != mRingingAddressType || |
Brad Ebinger | 5385513 | 2015-10-30 10:58:19 -0700 | [diff] [blame] | 755 | (heldCall != mOldHeldCall && !ignoreHeldCallChange))))) { |
Santos Cordon | 68d1a6b | 2014-09-19 12:25:58 -0700 | [diff] [blame] | 756 | |
Santos Cordon | a0b4612 | 2014-09-29 14:20:21 -0700 | [diff] [blame] | 757 | // If the call is transitioning into the alerting state, send DIALING first. |
| 758 | // Some devices expect to see a DIALING state prior to seeing an ALERTING state |
| 759 | // so we need to send it first. |
| 760 | boolean sendDialingFirst = mBluetoothCallState != bluetoothCallState && |
| 761 | bluetoothCallState == CALL_STATE_ALERTING; |
| 762 | |
Santos Cordon | c0ca76e | 2014-10-21 15:54:19 -0700 | [diff] [blame] | 763 | mOldHeldCall = heldCall; |
Santos Cordon | a0b4612 | 2014-09-29 14:20:21 -0700 | [diff] [blame] | 764 | mNumActiveCalls = numActiveCalls; |
Hall Liu | 6a89ba2 | 2017-07-31 19:04:48 -0700 | [diff] [blame^] | 765 | mNumChildrenOfActiveCall = numChildrenOfActiveCall; |
Santos Cordon | a0b4612 | 2014-09-29 14:20:21 -0700 | [diff] [blame] | 766 | mNumHeldCalls = numHeldCalls; |
| 767 | mBluetoothCallState = bluetoothCallState; |
| 768 | mRingingAddress = ringingAddress; |
| 769 | mRingingAddressType = ringingAddressType; |
| 770 | |
| 771 | if (sendDialingFirst) { |
Yorke Lee | 720bcbe | 2014-10-22 18:09:15 -0700 | [diff] [blame] | 772 | // Log in full to make logs easier to debug. |
| 773 | Log.i(TAG, "updateHeadsetWithCallState " + |
| 774 | "numActive %s, " + |
| 775 | "numHeld %s, " + |
| 776 | "callState %s, " + |
| 777 | "ringing number %s, " + |
| 778 | "ringing type %s", |
| 779 | mNumActiveCalls, |
| 780 | mNumHeldCalls, |
| 781 | CALL_STATE_DIALING, |
| 782 | Log.pii(mRingingAddress), |
| 783 | mRingingAddressType); |
Santos Cordon | a0b4612 | 2014-09-29 14:20:21 -0700 | [diff] [blame] | 784 | mBluetoothHeadset.phoneStateChanged( |
| 785 | mNumActiveCalls, |
| 786 | mNumHeldCalls, |
| 787 | CALL_STATE_DIALING, |
| 788 | mRingingAddress, |
| 789 | mRingingAddressType); |
| 790 | } |
| 791 | |
| 792 | Log.i(TAG, "updateHeadsetWithCallState " + |
| 793 | "numActive %s, " + |
| 794 | "numHeld %s, " + |
| 795 | "callState %s, " + |
| 796 | "ringing number %s, " + |
| 797 | "ringing type %s", |
| 798 | mNumActiveCalls, |
| 799 | mNumHeldCalls, |
| 800 | mBluetoothCallState, |
| 801 | Log.pii(mRingingAddress), |
| 802 | mRingingAddressType); |
| 803 | |
Santos Cordon | 68d1a6b | 2014-09-19 12:25:58 -0700 | [diff] [blame] | 804 | mBluetoothHeadset.phoneStateChanged( |
Santos Cordon | a0b4612 | 2014-09-29 14:20:21 -0700 | [diff] [blame] | 805 | mNumActiveCalls, |
| 806 | mNumHeldCalls, |
| 807 | mBluetoothCallState, |
| 808 | mRingingAddress, |
| 809 | mRingingAddressType); |
Santos Cordon | 88a4a60 | 2014-09-29 19:32:21 -0700 | [diff] [blame] | 810 | |
| 811 | mHeadsetUpdatedRecently = true; |
Santos Cordon | 68d1a6b | 2014-09-19 12:25:58 -0700 | [diff] [blame] | 812 | } |
| 813 | } |
| 814 | |
| 815 | private int getBluetoothCallStateForUpdate() { |
Ihab Awad | 78a5e6b | 2015-02-06 10:13:05 -0800 | [diff] [blame] | 816 | CallsManager callsManager = mCallsManager; |
Ihab Awad | 8d5d9dd | 2015-03-12 11:11:06 -0700 | [diff] [blame] | 817 | Call ringingCall = mCallsManager.getRingingCall(); |
Roshan Pius | 7d7cf27 | 2015-08-28 11:10:56 -0700 | [diff] [blame] | 818 | Call dialingCall = mCallsManager.getOutgoingCall(); |
Santos Cordon | 68d1a6b | 2014-09-19 12:25:58 -0700 | [diff] [blame] | 819 | |
| 820 | // |
| 821 | // !! WARNING !! |
| 822 | // You will note that CALL_STATE_WAITING, CALL_STATE_HELD, and CALL_STATE_ACTIVE are not |
| 823 | // used in this version of the call state mappings. This is on purpose. |
| 824 | // phone_state_change() in btif_hf.c is not written to handle these states. Only with the |
| 825 | // listCalls*() method are WAITING and ACTIVE used. |
| 826 | // Using the unsupported states here caused problems with inconsistent state in some |
| 827 | // bluetooth devices (like not getting out of ringing state after answering a call). |
| 828 | // |
| 829 | int bluetoothCallState = CALL_STATE_IDLE; |
| 830 | if (ringingCall != null) { |
| 831 | bluetoothCallState = CALL_STATE_INCOMING; |
Nancy Chen | 05a9e40 | 2014-09-26 14:14:32 -0700 | [diff] [blame] | 832 | } else if (dialingCall != null) { |
Santos Cordon | 68d1a6b | 2014-09-19 12:25:58 -0700 | [diff] [blame] | 833 | bluetoothCallState = CALL_STATE_ALERTING; |
| 834 | } |
| 835 | return bluetoothCallState; |
| 836 | } |
| 837 | |
Santos Cordon | 33fff49 | 2014-09-25 14:57:01 -0700 | [diff] [blame] | 838 | private int convertCallState(int callState, boolean isForegroundCall) { |
| 839 | switch (callState) { |
| 840 | case CallState.NEW: |
| 841 | case CallState.ABORTED: |
| 842 | case CallState.DISCONNECTED: |
| 843 | return CALL_STATE_IDLE; |
| 844 | |
| 845 | case CallState.ACTIVE: |
| 846 | return CALL_STATE_ACTIVE; |
| 847 | |
Santos Cordon | d9f9006 | 2015-10-28 15:55:34 -0700 | [diff] [blame] | 848 | case CallState.CONNECTING: |
| 849 | case CallState.SELECT_PHONE_ACCOUNT: |
Santos Cordon | 33fff49 | 2014-09-25 14:57:01 -0700 | [diff] [blame] | 850 | case CallState.DIALING: |
Tyler Gunn | 1e37be5 | 2016-07-11 08:54:23 -0700 | [diff] [blame] | 851 | case CallState.PULLING: |
Santos Cordon | 33fff49 | 2014-09-25 14:57:01 -0700 | [diff] [blame] | 852 | // Yes, this is correctly returning ALERTING. |
| 853 | // "Dialing" for BT means that we have sent information to the service provider |
| 854 | // to place the call but there is no confirmation that the call is going through. |
| 855 | // When there finally is confirmation, the ringback is played which is referred to |
| 856 | // as an "alert" tone, thus, ALERTING. |
| 857 | // TODO: We should consider using the ALERTING terms in Telecom because that |
| 858 | // seems to be more industry-standard. |
| 859 | return CALL_STATE_ALERTING; |
| 860 | |
| 861 | case CallState.ON_HOLD: |
| 862 | return CALL_STATE_HELD; |
| 863 | |
| 864 | case CallState.RINGING: |
| 865 | if (isForegroundCall) { |
| 866 | return CALL_STATE_INCOMING; |
| 867 | } else { |
| 868 | return CALL_STATE_WAITING; |
| 869 | } |
| 870 | } |
| 871 | return CALL_STATE_IDLE; |
| 872 | } |
| 873 | |
Santos Cordon | 68d1a6b | 2014-09-19 12:25:58 -0700 | [diff] [blame] | 874 | /** |
| 875 | * Returns the best phone account to use for the given state of all calls. |
| 876 | * First, tries to return the phone account for the foreground call, second the default |
| 877 | * phone account for PhoneAccount.SCHEME_TEL. |
| 878 | */ |
| 879 | private PhoneAccount getBestPhoneAccount() { |
Ihab Awad | 78a5e6b | 2015-02-06 10:13:05 -0800 | [diff] [blame] | 880 | if (mPhoneAccountRegistrar == null) { |
Santos Cordon | 0b5cb4d | 2014-12-02 02:40:10 -0800 | [diff] [blame] | 881 | return null; |
| 882 | } |
| 883 | |
Ihab Awad | 78a5e6b | 2015-02-06 10:13:05 -0800 | [diff] [blame] | 884 | Call call = mCallsManager.getForegroundCall(); |
Santos Cordon | 68d1a6b | 2014-09-19 12:25:58 -0700 | [diff] [blame] | 885 | |
| 886 | PhoneAccount account = null; |
| 887 | if (call != null) { |
| 888 | // First try to get the network name of the foreground call. |
Tony Mak | 240656f | 2015-12-04 11:36:22 +0000 | [diff] [blame] | 889 | account = mPhoneAccountRegistrar.getPhoneAccountOfCurrentUser( |
Santos Cordon | 6a21264 | 2015-05-08 16:35:23 -0700 | [diff] [blame] | 890 | call.getTargetPhoneAccount()); |
Santos Cordon | 68d1a6b | 2014-09-19 12:25:58 -0700 | [diff] [blame] | 891 | } |
| 892 | |
| 893 | if (account == null) { |
| 894 | // Second, Try to get the label for the default Phone Account. |
Tony Mak | 240656f | 2015-12-04 11:36:22 +0000 | [diff] [blame] | 895 | account = mPhoneAccountRegistrar.getPhoneAccountUnchecked( |
| 896 | mPhoneAccountRegistrar.getOutgoingPhoneAccountForSchemeOfCurrentUser( |
Brad Ebinger | 5385513 | 2015-10-30 10:58:19 -0700 | [diff] [blame] | 897 | PhoneAccount.SCHEME_TEL)); |
Santos Cordon | 68d1a6b | 2014-09-19 12:25:58 -0700 | [diff] [blame] | 898 | } |
| 899 | return account; |
| 900 | } |
| 901 | } |