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