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