Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Ben Gilad | 9f2bed3 | 2013-12-12 17:43:26 -0800 | [diff] [blame] | 17 | package com.android.telecomm; |
| 18 | |
Sailesh Nepal | ce704b9 | 2014-03-17 18:31:43 -0700 | [diff] [blame] | 19 | import android.net.Uri; |
Evan Charlton | a05805b | 2014-03-05 08:21:46 -0800 | [diff] [blame] | 20 | import android.os.Bundle; |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 21 | import android.telecomm.CallAudioState; |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 22 | import android.telecomm.CallInfo; |
Ben Gilad | c5b2269 | 2014-02-18 20:03:22 -0800 | [diff] [blame] | 23 | import android.telecomm.CallServiceDescriptor; |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 24 | import android.telecomm.CallState; |
Yorke Lee | 3350163 | 2014-03-17 19:24:12 -0700 | [diff] [blame] | 25 | import android.telecomm.GatewayInfo; |
Nancy Chen | 77d2d0e | 2014-06-24 12:06:03 -0700 | [diff] [blame] | 26 | import android.telecomm.Subscription; |
Santos Cordon | 79ff2bc | 2014-03-27 15:31:27 -0700 | [diff] [blame] | 27 | import android.telephony.DisconnectCause; |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 28 | |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 29 | import com.google.common.base.Preconditions; |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 30 | import com.google.common.collect.ImmutableCollection; |
| 31 | import com.google.common.collect.ImmutableList; |
Ben Gilad | 9f2bed3 | 2013-12-12 17:43:26 -0800 | [diff] [blame] | 32 | |
Santos Cordon | 23baed3 | 2014-06-27 14:45:39 -0700 | [diff] [blame^] | 33 | import java.util.ArrayList; |
| 34 | import java.util.Arrays; |
| 35 | import java.util.Collections; |
Santos Cordon | f3671a6 | 2014-05-29 21:51:53 -0700 | [diff] [blame] | 36 | import java.util.HashSet; |
| 37 | import java.util.LinkedHashSet; |
Santos Cordon | a56f276 | 2014-03-24 15:55:53 -0700 | [diff] [blame] | 38 | import java.util.Set; |
Ben Gilad | 9f2bed3 | 2013-12-12 17:43:26 -0800 | [diff] [blame] | 39 | |
Ben Gilad | dd8c608 | 2013-12-30 14:44:08 -0800 | [diff] [blame] | 40 | /** |
| 41 | * Singleton. |
| 42 | * |
| 43 | * NOTE(gilad): by design most APIs are package private, use the relevant adapter/s to allow |
| 44 | * access from other packages specifically refraining from passing the CallsManager instance |
| 45 | * beyond the com.android.telecomm package boundary. |
| 46 | */ |
Santos Cordon | 766d04f | 2014-05-06 10:28:25 -0700 | [diff] [blame] | 47 | public final class CallsManager implements Call.Listener { |
Ben Gilad | a0d9f75 | 2014-02-26 11:49:03 -0800 | [diff] [blame] | 48 | |
Santos Cordon | 74d420b | 2014-05-07 14:38:47 -0700 | [diff] [blame] | 49 | // TODO(santoscordon): Consider renaming this CallsManagerPlugin. |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 50 | interface CallsManagerListener { |
| 51 | void onCallAdded(Call call); |
| 52 | void onCallRemoved(Call call); |
| 53 | void onCallStateChanged(Call call, CallState oldState, CallState newState); |
Sailesh Nepal | 8c85dee | 2014-04-07 22:21:40 -0700 | [diff] [blame] | 54 | void onCallHandoffHandleChanged(Call call, Uri oldHandle, Uri newHandle); |
| 55 | void onCallServiceChanged( |
| 56 | Call call, |
| 57 | CallServiceWrapper oldCallService, |
| 58 | CallServiceWrapper newCallService); |
| 59 | void onCallHandoffCallServiceDescriptorChanged( |
| 60 | Call call, |
| 61 | CallServiceDescriptor oldDescriptor, |
| 62 | CallServiceDescriptor newDescriptor); |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 63 | void onIncomingCallAnswered(Call call); |
Ihab Awad | ff7493a | 2014-06-10 13:47:44 -0700 | [diff] [blame] | 64 | void onIncomingCallRejected(Call call, boolean rejectWithMessage, String textMessage); |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 65 | void onForegroundCallChanged(Call oldForegroundCall, Call newForegroundCall); |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 66 | void onAudioStateChanged(CallAudioState oldAudioState, CallAudioState newAudioState); |
Ihab Awad | cb387ac | 2014-05-28 16:49:38 -0700 | [diff] [blame] | 67 | void onRequestingRingback(Call call, boolean ringback); |
Santos Cordon | a161070 | 2014-06-04 20:22:56 -0700 | [diff] [blame] | 68 | void onIsConferenceCapableChanged(Call call, boolean isConferenceCapable); |
| 69 | void onIsConferencedChanged(Call call); |
Ihab Awad | ff7493a | 2014-06-10 13:47:44 -0700 | [diff] [blame] | 70 | void onCannedSmsResponsesLoaded(Call call); |
Nancy Chen | a65d41f | 2014-06-24 12:06:03 -0700 | [diff] [blame] | 71 | void onCallVideoProviderChanged(Call call); |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 72 | } |
| 73 | |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 74 | private static final CallsManager INSTANCE = new CallsManager(); |
Ben Gilad | 9f2bed3 | 2013-12-12 17:43:26 -0800 | [diff] [blame] | 75 | |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 76 | /** |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 77 | * The main call repository. Keeps an instance of all live calls. New incoming and outgoing |
| 78 | * calls are added to the map and removed when the calls move to the disconnected state. |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 79 | */ |
Santos Cordon | f3671a6 | 2014-05-29 21:51:53 -0700 | [diff] [blame] | 80 | private final Set<Call> mCalls = new LinkedHashSet<>(); |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 81 | |
| 82 | /** |
Sailesh Nepal | 84fa5f8 | 2014-04-02 11:01:11 -0700 | [diff] [blame] | 83 | * Set of new calls created to perform a handoff. The calls are added when handoff is initiated |
| 84 | * and removed when hadnoff is complete. |
| 85 | */ |
Santos Cordon | f3671a6 | 2014-05-29 21:51:53 -0700 | [diff] [blame] | 86 | private final Set<Call> mPendingHandoffCalls = new LinkedHashSet<>(); |
| 87 | |
| 88 | |
| 89 | private final DtmfLocalTonePlayer mDtmfLocalTonePlayer = new DtmfLocalTonePlayer(); |
| 90 | private final InCallController mInCallController = new InCallController(); |
| 91 | private final CallAudioManager mCallAudioManager; |
| 92 | private final Ringer mRinger; |
| 93 | private final Set<CallsManagerListener> mListeners = new HashSet<>(); |
Santos Cordon | deb8c89 | 2014-05-30 01:38:03 -0700 | [diff] [blame] | 94 | private final HeadsetMediaButton mHeadsetMediaButton; |
Sailesh Nepal | 84fa5f8 | 2014-04-02 11:01:11 -0700 | [diff] [blame] | 95 | |
| 96 | /** |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 97 | * The call the user is currently interacting with. This is the call that should have audio |
| 98 | * focus and be visible in the in-call UI. |
Santos Cordon | 4e9fffe | 2014-03-04 18:13:41 -0800 | [diff] [blame] | 99 | */ |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 100 | private Call mForegroundCall; |
Santos Cordon | 4e9fffe | 2014-03-04 18:13:41 -0800 | [diff] [blame] | 101 | |
Santos Cordon | 766d04f | 2014-05-06 10:28:25 -0700 | [diff] [blame] | 102 | /** Singleton accessor. */ |
| 103 | static CallsManager getInstance() { |
| 104 | return INSTANCE; |
| 105 | } |
Ben Gilad | 9f2bed3 | 2013-12-12 17:43:26 -0800 | [diff] [blame] | 106 | |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 107 | /** |
Ben Gilad | 8bdaa46 | 2014-02-05 12:53:19 -0800 | [diff] [blame] | 108 | * Initializes the required Telecomm components. |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 109 | */ |
| 110 | private CallsManager() { |
Santos Cordon | a0e5f1a | 2014-03-31 21:43:00 -0700 | [diff] [blame] | 111 | TelecommApp app = TelecommApp.getInstance(); |
Santos Cordon | a56f276 | 2014-03-24 15:55:53 -0700 | [diff] [blame] | 112 | |
Santos Cordon | deb8c89 | 2014-05-30 01:38:03 -0700 | [diff] [blame] | 113 | StatusBarNotifier statusBarNotifier = new StatusBarNotifier(app, this); |
| 114 | mCallAudioManager = new CallAudioManager(app, statusBarNotifier); |
Santos Cordon | c7b8eba | 2014-04-01 15:26:28 -0700 | [diff] [blame] | 115 | InCallTonePlayer.Factory playerFactory = new InCallTonePlayer.Factory(mCallAudioManager); |
Santos Cordon | ae19306 | 2014-05-21 21:21:49 -0700 | [diff] [blame] | 116 | mRinger = new Ringer(mCallAudioManager, this, playerFactory, app); |
Santos Cordon | deb8c89 | 2014-05-30 01:38:03 -0700 | [diff] [blame] | 117 | mHeadsetMediaButton = new HeadsetMediaButton(app, this); |
Santos Cordon | ae19306 | 2014-05-21 21:21:49 -0700 | [diff] [blame] | 118 | |
Santos Cordon | deb8c89 | 2014-05-30 01:38:03 -0700 | [diff] [blame] | 119 | mListeners.add(statusBarNotifier); |
Santos Cordon | a0e5f1a | 2014-03-31 21:43:00 -0700 | [diff] [blame] | 120 | mListeners.add(new CallLogManager(app)); |
Santos Cordon | a56f276 | 2014-03-24 15:55:53 -0700 | [diff] [blame] | 121 | mListeners.add(new PhoneStateBroadcaster()); |
Santos Cordon | f3671a6 | 2014-05-29 21:51:53 -0700 | [diff] [blame] | 122 | mListeners.add(mInCallController); |
Santos Cordon | ae19306 | 2014-05-21 21:21:49 -0700 | [diff] [blame] | 123 | mListeners.add(mRinger); |
Santos Cordon | c7b8eba | 2014-04-01 15:26:28 -0700 | [diff] [blame] | 124 | mListeners.add(new RingbackPlayer(this, playerFactory)); |
| 125 | mListeners.add(new InCallToneMonitor(playerFactory, this)); |
Santos Cordon | a56f276 | 2014-03-24 15:55:53 -0700 | [diff] [blame] | 126 | mListeners.add(mCallAudioManager); |
Santos Cordon | a0e5f1a | 2014-03-31 21:43:00 -0700 | [diff] [blame] | 127 | mListeners.add(app.getMissedCallNotifier()); |
Santos Cordon | 92a2d81 | 2014-04-30 15:19:01 -0700 | [diff] [blame] | 128 | mListeners.add(mDtmfLocalTonePlayer); |
Santos Cordon | 8128998 | 2014-06-03 16:03:08 -0700 | [diff] [blame] | 129 | mListeners.add(mHeadsetMediaButton); |
Ihab Awad | ff7493a | 2014-06-10 13:47:44 -0700 | [diff] [blame] | 130 | mListeners.add(RespondViaSmsManager.getInstance()); |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 131 | } |
| 132 | |
Santos Cordon | 766d04f | 2014-05-06 10:28:25 -0700 | [diff] [blame] | 133 | @Override |
| 134 | public void onSuccessfulOutgoingCall(Call call) { |
| 135 | Log.v(this, "onSuccessfulOutgoingCall, %s", call); |
| 136 | if (mCalls.contains(call)) { |
| 137 | // The call's CallService has been updated. |
| 138 | for (CallsManagerListener listener : mListeners) { |
| 139 | listener.onCallServiceChanged(call, null, call.getCallService()); |
| 140 | } |
| 141 | } else if (mPendingHandoffCalls.contains(call)) { |
| 142 | updateHandoffCallServiceDescriptor(call.getOriginalCall(), |
| 143 | call.getCallService().getDescriptor()); |
Santos Cordon | 6cb7ba9 | 2014-05-23 14:09:32 -0700 | [diff] [blame] | 144 | } else { |
| 145 | Log.wtf(this, "unexpected successful call notification: %s", call); |
| 146 | return; |
Santos Cordon | 766d04f | 2014-05-06 10:28:25 -0700 | [diff] [blame] | 147 | } |
Santos Cordon | 6cb7ba9 | 2014-05-23 14:09:32 -0700 | [diff] [blame] | 148 | |
| 149 | markCallAsDialing(call); |
Santos Cordon | 766d04f | 2014-05-06 10:28:25 -0700 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | @Override |
Sailesh Nepal | 5a73b03 | 2014-06-25 15:53:21 -0700 | [diff] [blame] | 153 | public void onFailedOutgoingCall(Call call, int errorCode, String errorMsg) { |
| 154 | Log.v(this, "onFailedOutgoingCall, call: %s", call); |
| 155 | // TODO: Replace disconnect cause with more specific disconnect causes. |
| 156 | markCallAsDisconnected(call, errorCode, errorMsg); |
| 157 | } |
| 158 | |
| 159 | @Override |
| 160 | public void onCancelledOutgoingCall(Call call) { |
| 161 | Log.v(this, "onCancelledOutgoingCall, call: %s", call); |
| 162 | setCallState(call, CallState.ABORTED); |
| 163 | removeCall(call); |
Santos Cordon | 766d04f | 2014-05-06 10:28:25 -0700 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | @Override |
| 167 | public void onSuccessfulIncomingCall(Call call, CallInfo callInfo) { |
| 168 | Log.d(this, "onSuccessfulIncomingCall"); |
| 169 | setCallState(call, callInfo.getState()); |
| 170 | addCall(call); |
| 171 | } |
| 172 | |
| 173 | @Override |
| 174 | public void onFailedIncomingCall(Call call) { |
| 175 | call.removeListener(this); |
Ben Gilad | a0d9f75 | 2014-02-26 11:49:03 -0800 | [diff] [blame] | 176 | } |
| 177 | |
Ihab Awad | cb387ac | 2014-05-28 16:49:38 -0700 | [diff] [blame] | 178 | @Override |
Santos Cordon | a161070 | 2014-06-04 20:22:56 -0700 | [diff] [blame] | 179 | public void onIsConferenceCapableChanged(Call call, boolean isConferenceCapable) { |
| 180 | for (CallsManagerListener listener : mListeners) { |
| 181 | listener.onIsConferenceCapableChanged(call, isConferenceCapable); |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | @Override |
Ihab Awad | cb387ac | 2014-05-28 16:49:38 -0700 | [diff] [blame] | 186 | public void onRequestingRingback(Call call, boolean ringback) { |
| 187 | for (CallsManagerListener listener : mListeners) { |
| 188 | listener.onRequestingRingback(call, ringback); |
| 189 | } |
| 190 | } |
| 191 | |
Evan Charlton | 352105c | 2014-06-03 14:10:54 -0700 | [diff] [blame] | 192 | @Override |
| 193 | public void onPostDialWait(Call call, String remaining) { |
| 194 | mInCallController.onPostDialWait(call, remaining); |
| 195 | } |
| 196 | |
Santos Cordon | a161070 | 2014-06-04 20:22:56 -0700 | [diff] [blame] | 197 | @Override |
| 198 | public void onExpiredConferenceCall(Call call) { |
| 199 | call.removeListener(this); |
| 200 | } |
| 201 | |
| 202 | @Override |
| 203 | public void onConfirmedConferenceCall(Call call) { |
| 204 | addCall(call); |
| 205 | Log.v(this, "confirming Conf call %s", call); |
| 206 | for (CallsManagerListener listener : mListeners) { |
| 207 | listener.onIsConferencedChanged(call); |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | @Override |
| 212 | public void onParentChanged(Call call) { |
| 213 | for (CallsManagerListener listener : mListeners) { |
| 214 | listener.onIsConferencedChanged(call); |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | @Override |
| 219 | public void onChildrenChanged(Call call) { |
| 220 | for (CallsManagerListener listener : mListeners) { |
| 221 | listener.onIsConferencedChanged(call); |
| 222 | } |
| 223 | } |
| 224 | |
Ihab Awad | ff7493a | 2014-06-10 13:47:44 -0700 | [diff] [blame] | 225 | @Override |
| 226 | public void onCannedSmsResponsesLoaded(Call call) { |
| 227 | for (CallsManagerListener listener : mListeners) { |
| 228 | listener.onCannedSmsResponsesLoaded(call); |
| 229 | } |
| 230 | } |
| 231 | |
Nancy Chen | a65d41f | 2014-06-24 12:06:03 -0700 | [diff] [blame] | 232 | @Override |
| 233 | public void onCallVideoProviderChanged(Call call) { |
| 234 | for (CallsManagerListener listener : mListeners) { |
| 235 | listener.onCallVideoProviderChanged(call); |
| 236 | } |
| 237 | } |
| 238 | |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 239 | ImmutableCollection<Call> getCalls() { |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 240 | return ImmutableList.copyOf(mCalls); |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | Call getForegroundCall() { |
| 244 | return mForegroundCall; |
| 245 | } |
| 246 | |
Santos Cordon | ae19306 | 2014-05-21 21:21:49 -0700 | [diff] [blame] | 247 | Ringer getRinger() { |
| 248 | return mRinger; |
| 249 | } |
| 250 | |
Santos Cordon | f3671a6 | 2014-05-29 21:51:53 -0700 | [diff] [blame] | 251 | InCallController getInCallController() { |
| 252 | return mInCallController; |
| 253 | } |
| 254 | |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 255 | boolean hasEmergencyCall() { |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 256 | for (Call call : mCalls) { |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 257 | if (call.isEmergencyCall()) { |
| 258 | return true; |
| 259 | } |
| 260 | } |
| 261 | return false; |
| 262 | } |
| 263 | |
| 264 | CallAudioState getAudioState() { |
| 265 | return mCallAudioManager.getAudioState(); |
| 266 | } |
| 267 | |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 268 | /** |
Santos Cordon | 493e8f2 | 2014-02-19 03:15:12 -0800 | [diff] [blame] | 269 | * Starts the incoming call sequence by having switchboard gather more information about the |
| 270 | * specified call; using the specified call service descriptor. Upon success, execution returns |
Santos Cordon | 766d04f | 2014-05-06 10:28:25 -0700 | [diff] [blame] | 271 | * to {@link #onSuccessfulIncomingCall} to start the in-call UI. |
Santos Cordon | 80d9bdc | 2014-02-13 18:28:46 -0800 | [diff] [blame] | 272 | * |
Ben Gilad | c5b2269 | 2014-02-18 20:03:22 -0800 | [diff] [blame] | 273 | * @param descriptor The descriptor of the call service to use for this incoming call. |
Evan Charlton | a05805b | 2014-03-05 08:21:46 -0800 | [diff] [blame] | 274 | * @param extras The optional extras Bundle passed with the intent used for the incoming call. |
Santos Cordon | 80d9bdc | 2014-02-13 18:28:46 -0800 | [diff] [blame] | 275 | */ |
Evan Charlton | a05805b | 2014-03-05 08:21:46 -0800 | [diff] [blame] | 276 | void processIncomingCallIntent(CallServiceDescriptor descriptor, Bundle extras) { |
Sailesh Nepal | f1c191d | 2014-03-07 18:17:39 -0800 | [diff] [blame] | 277 | Log.d(this, "processIncomingCallIntent"); |
Santos Cordon | 80d9bdc | 2014-02-13 18:28:46 -0800 | [diff] [blame] | 278 | // Create a call with no handle. Eventually, switchboard will update the call with |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 279 | // additional information from the call service, but for now we just need one to pass |
| 280 | // around. |
Santos Cordon | a161070 | 2014-06-04 20:22:56 -0700 | [diff] [blame] | 281 | Call call = new Call(true /* isIncoming */, false /* isConference */); |
Santos Cordon | 766d04f | 2014-05-06 10:28:25 -0700 | [diff] [blame] | 282 | // TODO(santoscordon): Move this to be a part of addCall() |
| 283 | call.addListener(this); |
Santos Cordon | 80d9bdc | 2014-02-13 18:28:46 -0800 | [diff] [blame] | 284 | |
Santos Cordon | 766d04f | 2014-05-06 10:28:25 -0700 | [diff] [blame] | 285 | call.startIncoming(descriptor, extras); |
Ben Gilad | a0d9f75 | 2014-02-26 11:49:03 -0800 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | /** |
Yorke Lee | 3350163 | 2014-03-17 19:24:12 -0700 | [diff] [blame] | 289 | * Attempts to issue/connect the specified call. |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 290 | * |
Yorke Lee | 3350163 | 2014-03-17 19:24:12 -0700 | [diff] [blame] | 291 | * @param handle Handle to connect the call with. |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 292 | * @param contactInfo Information about the entity being called. |
Yorke Lee | 3350163 | 2014-03-17 19:24:12 -0700 | [diff] [blame] | 293 | * @param gatewayInfo Optional gateway information that can be used to route the call to the |
Santos Cordon | 5b7b9b3 | 2014-03-26 14:00:22 -0700 | [diff] [blame] | 294 | * actual dialed handle via a gateway provider. May be null. |
Sai Cheemalapati | b7157e9 | 2014-06-11 17:51:55 -0700 | [diff] [blame] | 295 | * @param speakerphoneOn Whether or not to turn the speakerphone on once the call connects. |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 296 | */ |
Sai Cheemalapati | b7157e9 | 2014-06-11 17:51:55 -0700 | [diff] [blame] | 297 | void placeOutgoingCall(Uri handle, ContactInfo contactInfo, GatewayInfo gatewayInfo, |
Nancy Chen | 77d2d0e | 2014-06-24 12:06:03 -0700 | [diff] [blame] | 298 | Subscription subscription, boolean speakerphoneOn) { |
Yorke Lee | 3350163 | 2014-03-17 19:24:12 -0700 | [diff] [blame] | 299 | final Uri uriHandle = (gatewayInfo == null) ? handle : gatewayInfo.getGatewayHandle(); |
| 300 | |
| 301 | if (gatewayInfo == null) { |
Santos Cordon | b58f453 | 2014-05-29 11:59:06 -0700 | [diff] [blame] | 302 | Log.i(this, "Creating a new outgoing call with handle: %s", Log.piiHandle(uriHandle)); |
Yorke Lee | 3350163 | 2014-03-17 19:24:12 -0700 | [diff] [blame] | 303 | } else { |
| 304 | Log.i(this, "Creating a new outgoing call with gateway handle: %s, original handle: %s", |
| 305 | Log.pii(uriHandle), Log.pii(handle)); |
| 306 | } |
Santos Cordon | 766d04f | 2014-05-06 10:28:25 -0700 | [diff] [blame] | 307 | |
Santos Cordon | a161070 | 2014-06-04 20:22:56 -0700 | [diff] [blame] | 308 | Call call = new Call( |
Nancy Chen | 77d2d0e | 2014-06-24 12:06:03 -0700 | [diff] [blame] | 309 | uriHandle, gatewayInfo, subscription, |
| 310 | false /* isIncoming */, false /* isConference */); |
Sai Cheemalapati | b7157e9 | 2014-06-11 17:51:55 -0700 | [diff] [blame] | 311 | call.setStartWithSpeakerphoneOn(speakerphoneOn); |
Santos Cordon | 766d04f | 2014-05-06 10:28:25 -0700 | [diff] [blame] | 312 | |
| 313 | // TODO(santoscordon): Move this to be a part of addCall() |
| 314 | call.addListener(this); |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 315 | addCall(call); |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 316 | |
Santos Cordon | 766d04f | 2014-05-06 10:28:25 -0700 | [diff] [blame] | 317 | call.startOutgoing(); |
Yorke Lee | f98fb57 | 2014-03-05 10:56:55 -0800 | [diff] [blame] | 318 | } |
| 319 | |
| 320 | /** |
Santos Cordon | a161070 | 2014-06-04 20:22:56 -0700 | [diff] [blame] | 321 | * Attempts to start a conference call for the specified call. |
| 322 | * |
| 323 | * @param call The call to conference with. |
| 324 | */ |
| 325 | void conference(Call call) { |
| 326 | Call conferenceCall = new Call(false, true); |
| 327 | conferenceCall.addListener(this); |
| 328 | call.conferenceInto(conferenceCall); |
| 329 | } |
| 330 | |
| 331 | /** |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 332 | * Instructs Telecomm to answer the specified call. Intended to be invoked by the in-call |
| 333 | * app through {@link InCallAdapter} after Telecomm notifies it of an incoming call followed by |
| 334 | * the user opting to answer said call. |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 335 | */ |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 336 | void answerCall(Call call) { |
| 337 | if (!mCalls.contains(call)) { |
| 338 | Log.i(this, "Request to answer a non-existent call %s", call); |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 339 | } else { |
Santos Cordon | 40f78c2 | 2014-04-07 02:11:42 -0700 | [diff] [blame] | 340 | // If the foreground call is not the ringing call and it is currently isActive() or |
| 341 | // DIALING, put it on hold before answering the call. |
| 342 | if (mForegroundCall != null && mForegroundCall != call && |
| 343 | (mForegroundCall.isActive() || |
| 344 | mForegroundCall.getState() == CallState.DIALING)) { |
| 345 | Log.v(this, "Holding active/dialing call %s before answering incoming call %s.", |
| 346 | mForegroundCall, call); |
| 347 | mForegroundCall.hold(); |
| 348 | // TODO(santoscordon): Wait until we get confirmation of the active call being |
| 349 | // on-hold before answering the new call. |
| 350 | // TODO(santoscordon): Import logic from CallManager.acceptCall() |
| 351 | } |
| 352 | |
Santos Cordon | a56f276 | 2014-03-24 15:55:53 -0700 | [diff] [blame] | 353 | for (CallsManagerListener listener : mListeners) { |
| 354 | listener.onIncomingCallAnswered(call); |
| 355 | } |
Santos Cordon | 4e9fffe | 2014-03-04 18:13:41 -0800 | [diff] [blame] | 356 | |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 357 | // We do not update the UI until we get confirmation of the answer() through |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 358 | // {@link #markCallAsActive}. |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 359 | call.answer(); |
| 360 | } |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 361 | } |
| 362 | |
| 363 | /** |
| 364 | * Instructs Telecomm to reject the specified call. Intended to be invoked by the in-call |
| 365 | * app through {@link InCallAdapter} after Telecomm notifies it of an incoming call followed by |
| 366 | * the user opting to reject said call. |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 367 | */ |
Ihab Awad | ff7493a | 2014-06-10 13:47:44 -0700 | [diff] [blame] | 368 | void rejectCall(Call call, boolean rejectWithMessage, String textMessage) { |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 369 | if (!mCalls.contains(call)) { |
| 370 | Log.i(this, "Request to reject a non-existent call %s", call); |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 371 | } else { |
Santos Cordon | a56f276 | 2014-03-24 15:55:53 -0700 | [diff] [blame] | 372 | for (CallsManagerListener listener : mListeners) { |
Ihab Awad | ff7493a | 2014-06-10 13:47:44 -0700 | [diff] [blame] | 373 | listener.onIncomingCallRejected(call, rejectWithMessage, textMessage); |
Santos Cordon | a56f276 | 2014-03-24 15:55:53 -0700 | [diff] [blame] | 374 | } |
Ihab Awad | ff7493a | 2014-06-10 13:47:44 -0700 | [diff] [blame] | 375 | call.reject(rejectWithMessage, textMessage); |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 376 | } |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 377 | } |
| 378 | |
| 379 | /** |
Ihab Awad | 74549ec | 2014-03-10 15:33:25 -0700 | [diff] [blame] | 380 | * Instructs Telecomm to play the specified DTMF tone within the specified call. |
| 381 | * |
Ihab Awad | 74549ec | 2014-03-10 15:33:25 -0700 | [diff] [blame] | 382 | * @param digit The DTMF digit to play. |
| 383 | */ |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 384 | void playDtmfTone(Call call, char digit) { |
| 385 | if (!mCalls.contains(call)) { |
| 386 | Log.i(this, "Request to play DTMF in a non-existent call %s", call); |
Ihab Awad | 74549ec | 2014-03-10 15:33:25 -0700 | [diff] [blame] | 387 | } else { |
| 388 | call.playDtmfTone(digit); |
Santos Cordon | 92a2d81 | 2014-04-30 15:19:01 -0700 | [diff] [blame] | 389 | mDtmfLocalTonePlayer.playTone(call, digit); |
Ihab Awad | 74549ec | 2014-03-10 15:33:25 -0700 | [diff] [blame] | 390 | } |
| 391 | } |
| 392 | |
| 393 | /** |
| 394 | * Instructs Telecomm to stop the currently playing DTMF tone, if any. |
Ihab Awad | 74549ec | 2014-03-10 15:33:25 -0700 | [diff] [blame] | 395 | */ |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 396 | void stopDtmfTone(Call call) { |
| 397 | if (!mCalls.contains(call)) { |
| 398 | Log.i(this, "Request to stop DTMF in a non-existent call %s", call); |
Ihab Awad | 74549ec | 2014-03-10 15:33:25 -0700 | [diff] [blame] | 399 | } else { |
| 400 | call.stopDtmfTone(); |
Santos Cordon | 92a2d81 | 2014-04-30 15:19:01 -0700 | [diff] [blame] | 401 | mDtmfLocalTonePlayer.stopTone(call); |
Ihab Awad | 74549ec | 2014-03-10 15:33:25 -0700 | [diff] [blame] | 402 | } |
| 403 | } |
| 404 | |
| 405 | /** |
Evan Charlton | 352105c | 2014-06-03 14:10:54 -0700 | [diff] [blame] | 406 | * Instructs Telecomm to continue (or not) the current post-dial DTMF string, if any. |
Ihab Awad | 74549ec | 2014-03-10 15:33:25 -0700 | [diff] [blame] | 407 | */ |
Evan Charlton | 352105c | 2014-06-03 14:10:54 -0700 | [diff] [blame] | 408 | void postDialContinue(Call call, boolean proceed) { |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 409 | if (!mCalls.contains(call)) { |
| 410 | Log.i(this, "Request to continue post-dial string in a non-existent call %s", call); |
Ihab Awad | 74549ec | 2014-03-10 15:33:25 -0700 | [diff] [blame] | 411 | } else { |
Evan Charlton | 352105c | 2014-06-03 14:10:54 -0700 | [diff] [blame] | 412 | call.postDialContinue(proceed); |
Ihab Awad | 74549ec | 2014-03-10 15:33:25 -0700 | [diff] [blame] | 413 | } |
| 414 | } |
| 415 | |
| 416 | /** |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 417 | * Instructs Telecomm to disconnect the specified call. Intended to be invoked by the |
| 418 | * in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered by |
| 419 | * the user hitting the end-call button. |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 420 | */ |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 421 | void disconnectCall(Call call) { |
Santos Cordon | 682fe6b | 2014-05-20 08:56:39 -0700 | [diff] [blame] | 422 | Log.v(this, "disconnectCall %s", call); |
| 423 | |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 424 | if (!mCalls.contains(call)) { |
| 425 | Log.w(this, "Unknown call (%s) asked to disconnect", call); |
Santos Cordon | 049b7b6 | 2014-01-30 05:34:26 -0800 | [diff] [blame] | 426 | } else { |
| 427 | call.disconnect(); |
| 428 | } |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 429 | } |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 430 | |
Yorke Lee | cdf3ebd | 2014-03-12 18:31:41 -0700 | [diff] [blame] | 431 | /** |
| 432 | * Instructs Telecomm to put the specified call on hold. Intended to be invoked by the |
| 433 | * in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered by |
| 434 | * the user hitting the hold button during an active call. |
Yorke Lee | cdf3ebd | 2014-03-12 18:31:41 -0700 | [diff] [blame] | 435 | */ |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 436 | void holdCall(Call call) { |
| 437 | if (!mCalls.contains(call)) { |
| 438 | Log.w(this, "Unknown call (%s) asked to be put on hold", call); |
Yorke Lee | cdf3ebd | 2014-03-12 18:31:41 -0700 | [diff] [blame] | 439 | } else { |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 440 | Log.d(this, "Putting call on hold: (%s)", call); |
Yorke Lee | cdf3ebd | 2014-03-12 18:31:41 -0700 | [diff] [blame] | 441 | call.hold(); |
| 442 | } |
| 443 | } |
| 444 | |
| 445 | /** |
| 446 | * Instructs Telecomm to release the specified call from hold. Intended to be invoked by |
| 447 | * the in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered |
| 448 | * by the user hitting the hold button during a held call. |
Yorke Lee | cdf3ebd | 2014-03-12 18:31:41 -0700 | [diff] [blame] | 449 | */ |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 450 | void unholdCall(Call call) { |
| 451 | if (!mCalls.contains(call)) { |
| 452 | Log.w(this, "Unknown call (%s) asked to be removed from hold", call); |
Yorke Lee | cdf3ebd | 2014-03-12 18:31:41 -0700 | [diff] [blame] | 453 | } else { |
Santos Cordon | c7e85d4 | 2014-05-22 02:51:48 -0700 | [diff] [blame] | 454 | Log.d(this, "unholding call: (%s)", call); |
Yorke Lee | cdf3ebd | 2014-03-12 18:31:41 -0700 | [diff] [blame] | 455 | call.unhold(); |
| 456 | } |
| 457 | } |
| 458 | |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 459 | /** Called by the in-call UI to change the mute state. */ |
| 460 | void mute(boolean shouldMute) { |
| 461 | mCallAudioManager.mute(shouldMute); |
| 462 | } |
| 463 | |
| 464 | /** |
| 465 | * Called by the in-call UI to change the audio route, for example to change from earpiece to |
| 466 | * speaker phone. |
| 467 | */ |
| 468 | void setAudioRoute(int route) { |
| 469 | mCallAudioManager.setAudioRoute(route); |
| 470 | } |
| 471 | |
Sailesh Nepal | 84fa5f8 | 2014-04-02 11:01:11 -0700 | [diff] [blame] | 472 | void startHandoffForCall(Call originalCall) { |
| 473 | if (!mCalls.contains(originalCall)) { |
| 474 | Log.w(this, "Unknown call %s asked to be handed off", originalCall); |
| 475 | return; |
| 476 | } |
| 477 | |
| 478 | for (Call handoffCall : mPendingHandoffCalls) { |
| 479 | if (handoffCall.getOriginalCall() == originalCall) { |
| 480 | Log.w(this, "Call %s is already being handed off, skipping", originalCall); |
| 481 | return; |
| 482 | } |
| 483 | } |
| 484 | |
| 485 | // Create a new call to be placed in the background. If handoff is successful then the |
| 486 | // original call will live on but its state will be updated to the new call's state. In |
| 487 | // particular the original call's call service will be updated to the new call's call |
| 488 | // service. |
Santos Cordon | a161070 | 2014-06-04 20:22:56 -0700 | [diff] [blame] | 489 | Call tempCall = new Call( |
Nancy Chen | 77d2d0e | 2014-06-24 12:06:03 -0700 | [diff] [blame] | 490 | originalCall.getHandoffHandle(), originalCall.getGatewayInfo(), |
| 491 | originalCall.getSubscription(), false, false); |
Sailesh Nepal | 84fa5f8 | 2014-04-02 11:01:11 -0700 | [diff] [blame] | 492 | tempCall.setOriginalCall(originalCall); |
| 493 | tempCall.setExtras(originalCall.getExtras()); |
Sailesh Nepal | 84fa5f8 | 2014-04-02 11:01:11 -0700 | [diff] [blame] | 494 | mPendingHandoffCalls.add(tempCall); |
Santos Cordon | 6cb7ba9 | 2014-05-23 14:09:32 -0700 | [diff] [blame] | 495 | tempCall.addListener(this); |
| 496 | |
Sailesh Nepal | 84fa5f8 | 2014-04-02 11:01:11 -0700 | [diff] [blame] | 497 | Log.d(this, "Placing handoff call"); |
Santos Cordon | 766d04f | 2014-05-06 10:28:25 -0700 | [diff] [blame] | 498 | tempCall.startOutgoing(); |
Sailesh Nepal | 84fa5f8 | 2014-04-02 11:01:11 -0700 | [diff] [blame] | 499 | } |
| 500 | |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 501 | /** Called when the audio state changes. */ |
| 502 | void onAudioStateChanged(CallAudioState oldAudioState, CallAudioState newAudioState) { |
| 503 | Log.v(this, "onAudioStateChanged, audioState: %s -> %s", oldAudioState, newAudioState); |
Santos Cordon | a56f276 | 2014-03-24 15:55:53 -0700 | [diff] [blame] | 504 | for (CallsManagerListener listener : mListeners) { |
| 505 | listener.onAudioStateChanged(oldAudioState, newAudioState); |
| 506 | } |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 507 | } |
| 508 | |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 509 | void markCallAsRinging(Call call) { |
| 510 | setCallState(call, CallState.RINGING); |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 511 | } |
| 512 | |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 513 | void markCallAsDialing(Call call) { |
| 514 | setCallState(call, CallState.DIALING); |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 515 | } |
| 516 | |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 517 | void markCallAsActive(Call call) { |
Sailesh Nepal | 8c85dee | 2014-04-07 22:21:40 -0700 | [diff] [blame] | 518 | if (call.getConnectTimeMillis() == 0) { |
| 519 | call.setConnectTimeMillis(System.currentTimeMillis()); |
| 520 | } |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 521 | setCallState(call, CallState.ACTIVE); |
Sailesh Nepal | 84fa5f8 | 2014-04-02 11:01:11 -0700 | [diff] [blame] | 522 | |
| 523 | if (mPendingHandoffCalls.contains(call)) { |
Sailesh Nepal | 4857f47 | 2014-04-07 22:26:27 -0700 | [diff] [blame] | 524 | completeHandoff(call, true); |
Sailesh Nepal | 84fa5f8 | 2014-04-02 11:01:11 -0700 | [diff] [blame] | 525 | } |
Sai Cheemalapati | b7157e9 | 2014-06-11 17:51:55 -0700 | [diff] [blame] | 526 | if (call.getStartWithSpeakerphoneOn()) { |
| 527 | setAudioRoute(CallAudioState.ROUTE_SPEAKER); |
| 528 | } |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 529 | } |
| 530 | |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 531 | void markCallAsOnHold(Call call) { |
| 532 | setCallState(call, CallState.ON_HOLD); |
Yorke Lee | cdf3ebd | 2014-03-12 18:31:41 -0700 | [diff] [blame] | 533 | } |
| 534 | |
Santos Cordon | 049b7b6 | 2014-01-30 05:34:26 -0800 | [diff] [blame] | 535 | /** |
| 536 | * Marks the specified call as DISCONNECTED and notifies the in-call app. If this was the last |
| 537 | * live call, then also disconnect from the in-call controller. |
| 538 | * |
Santos Cordon | 79ff2bc | 2014-03-27 15:31:27 -0700 | [diff] [blame] | 539 | * @param disconnectCause The disconnect reason, see {@link android.telephony.DisconnectCause}. |
| 540 | * @param disconnectMessage Optional call-service-provided message about the disconnect. |
Santos Cordon | 049b7b6 | 2014-01-30 05:34:26 -0800 | [diff] [blame] | 541 | */ |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 542 | void markCallAsDisconnected(Call call, int disconnectCause, String disconnectMessage) { |
| 543 | call.setDisconnectCause(disconnectCause, disconnectMessage); |
| 544 | setCallState(call, CallState.DISCONNECTED); |
Sailesh Nepal | 4857f47 | 2014-04-07 22:26:27 -0700 | [diff] [blame] | 545 | |
| 546 | // Only remove the call if handoff is not pending. |
| 547 | if (call.getHandoffCallServiceDescriptor() == null) { |
| 548 | removeCall(call); |
| 549 | } |
Ben Gilad | a0d9f75 | 2014-02-26 11:49:03 -0800 | [diff] [blame] | 550 | } |
| 551 | |
Sailesh Nepal | 6ab6fb7 | 2014-04-01 20:03:19 -0700 | [diff] [blame] | 552 | void setHandoffInfo(Call call, Uri handle, Bundle extras) { |
Sailesh Nepal | 84fa5f8 | 2014-04-02 11:01:11 -0700 | [diff] [blame] | 553 | if (!mCalls.contains(call)) { |
| 554 | Log.w(this, "Unknown call (%s) asked to set handoff info", call); |
| 555 | return; |
| 556 | } |
| 557 | |
| 558 | if (extras == null) { |
| 559 | call.setExtras(Bundle.EMPTY); |
| 560 | } else { |
| 561 | call.setExtras(extras); |
| 562 | } |
| 563 | |
| 564 | Uri oldHandle = call.getHandoffHandle(); |
| 565 | Log.v(this, "set handoff handle %s -> %s, for call: %s", oldHandle, handle, call); |
| 566 | if (!areUriEqual(oldHandle, handle)) { |
| 567 | call.setHandoffHandle(handle); |
| 568 | for (CallsManagerListener listener : mListeners) { |
| 569 | listener.onCallHandoffHandleChanged(call, oldHandle, handle); |
| 570 | } |
| 571 | } |
Sailesh Nepal | 6ab6fb7 | 2014-04-01 20:03:19 -0700 | [diff] [blame] | 572 | } |
| 573 | |
Ben Gilad | a0d9f75 | 2014-02-26 11:49:03 -0800 | [diff] [blame] | 574 | /** |
Santos Cordon | 4b2c119 | 2014-03-19 18:15:38 -0700 | [diff] [blame] | 575 | * Cleans up any calls currently associated with the specified call service when the |
| 576 | * call-service binder disconnects unexpectedly. |
| 577 | * |
| 578 | * @param callService The call service that disconnected. |
| 579 | */ |
| 580 | void handleCallServiceDeath(CallServiceWrapper callService) { |
| 581 | Preconditions.checkNotNull(callService); |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 582 | for (Call call : ImmutableList.copyOf(mCalls)) { |
Santos Cordon | 4b2c119 | 2014-03-19 18:15:38 -0700 | [diff] [blame] | 583 | if (call.getCallService() == callService) { |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 584 | markCallAsDisconnected(call, DisconnectCause.ERROR_UNSPECIFIED, null); |
Santos Cordon | 4b2c119 | 2014-03-19 18:15:38 -0700 | [diff] [blame] | 585 | } |
| 586 | } |
| 587 | } |
| 588 | |
Santos Cordon | deb8c89 | 2014-05-30 01:38:03 -0700 | [diff] [blame] | 589 | boolean hasAnyCalls() { |
| 590 | return !mCalls.isEmpty(); |
| 591 | } |
| 592 | |
Santos Cordon | c7e85d4 | 2014-05-22 02:51:48 -0700 | [diff] [blame] | 593 | boolean hasActiveOrHoldingCall() { |
Santos Cordon | 23baed3 | 2014-06-27 14:45:39 -0700 | [diff] [blame^] | 594 | return getFirstCallWithState(CallState.ACTIVE, CallState.ON_HOLD) != null; |
Santos Cordon | c7e85d4 | 2014-05-22 02:51:48 -0700 | [diff] [blame] | 595 | } |
| 596 | |
| 597 | boolean hasRingingCall() { |
Santos Cordon | 23baed3 | 2014-06-27 14:45:39 -0700 | [diff] [blame^] | 598 | return getFirstCallWithState(CallState.RINGING) != null; |
Santos Cordon | c7e85d4 | 2014-05-22 02:51:48 -0700 | [diff] [blame] | 599 | } |
| 600 | |
Santos Cordon | deb8c89 | 2014-05-30 01:38:03 -0700 | [diff] [blame] | 601 | boolean onMediaButton(int type) { |
| 602 | if (hasAnyCalls()) { |
| 603 | if (HeadsetMediaButton.SHORT_PRESS == type) { |
| 604 | Call ringingCall = getFirstCallWithState(CallState.RINGING); |
| 605 | if (ringingCall == null) { |
| 606 | mCallAudioManager.toggleMute(); |
| 607 | return true; |
| 608 | } else { |
| 609 | ringingCall.answer(); |
| 610 | return true; |
| 611 | } |
| 612 | } else if (HeadsetMediaButton.LONG_PRESS == type) { |
| 613 | Log.d(this, "handleHeadsetHook: longpress -> hangup"); |
| 614 | Call callToHangup = getFirstCallWithState( |
| 615 | CallState.RINGING, CallState.DIALING, CallState.ACTIVE, CallState.ON_HOLD); |
| 616 | if (callToHangup != null) { |
| 617 | callToHangup.disconnect(); |
| 618 | return true; |
| 619 | } |
| 620 | } |
| 621 | } |
| 622 | return false; |
| 623 | } |
| 624 | |
| 625 | /** |
Santos Cordon | 10838c2 | 2014-06-11 17:36:04 -0700 | [diff] [blame] | 626 | * Checks to see if the specified call is the only high-level call and if so, enable the |
| 627 | * "Add-call" button. We allow you to add a second call but not a third or beyond. |
| 628 | * |
| 629 | * @param call The call to test for add-call. |
| 630 | * @return Whether the add-call feature should be enabled for the call. |
| 631 | */ |
| 632 | protected boolean isAddCallCapable(Call call) { |
| 633 | if (call.getParentCall() != null) { |
| 634 | // Never true for child calls. |
| 635 | return false; |
| 636 | } |
| 637 | |
| 638 | // Loop through all the other calls and there exists a top level (has no parent) call |
| 639 | // that is not the specified call, return false. |
| 640 | for (Call otherCall : mCalls) { |
| 641 | if (call != otherCall && otherCall.getParentCall() == null) { |
| 642 | return false; |
| 643 | } |
| 644 | } |
| 645 | return true; |
| 646 | } |
| 647 | |
| 648 | /** |
Santos Cordon | deb8c89 | 2014-05-30 01:38:03 -0700 | [diff] [blame] | 649 | * Returns the first call that it finds with the given states. The states are treated as having |
| 650 | * priority order so that any call with the first state will be returned before any call with |
| 651 | * states listed later in the parameter list. |
| 652 | */ |
Santos Cordon | 23baed3 | 2014-06-27 14:45:39 -0700 | [diff] [blame^] | 653 | Call getFirstCallWithState(CallState... states) { |
Santos Cordon | deb8c89 | 2014-05-30 01:38:03 -0700 | [diff] [blame] | 654 | for (CallState currentState : states) { |
Santos Cordon | 23baed3 | 2014-06-27 14:45:39 -0700 | [diff] [blame^] | 655 | // check the foreground first |
| 656 | if (mForegroundCall != null && mForegroundCall.getState() == currentState) { |
| 657 | return mForegroundCall; |
| 658 | } |
| 659 | |
Santos Cordon | deb8c89 | 2014-05-30 01:38:03 -0700 | [diff] [blame] | 660 | for (Call call : mCalls) { |
| 661 | if (currentState == call.getState()) { |
| 662 | return call; |
| 663 | } |
| 664 | } |
| 665 | } |
| 666 | return null; |
| 667 | } |
| 668 | |
Santos Cordon | 4b2c119 | 2014-03-19 18:15:38 -0700 | [diff] [blame] | 669 | /** |
Ben Gilad | a0d9f75 | 2014-02-26 11:49:03 -0800 | [diff] [blame] | 670 | * Adds the specified call to the main list of live calls. |
| 671 | * |
| 672 | * @param call The call to add. |
| 673 | */ |
| 674 | private void addCall(Call call) { |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 675 | mCalls.add(call); |
Santos Cordon | 40f78c2 | 2014-04-07 02:11:42 -0700 | [diff] [blame] | 676 | |
| 677 | // TODO(santoscordon): Update mForegroundCall prior to invoking |
| 678 | // onCallAdded for calls which immediately take the foreground (like the first call). |
Santos Cordon | a56f276 | 2014-03-24 15:55:53 -0700 | [diff] [blame] | 679 | for (CallsManagerListener listener : mListeners) { |
| 680 | listener.onCallAdded(call); |
| 681 | } |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 682 | updateForegroundCall(); |
Ben Gilad | a0d9f75 | 2014-02-26 11:49:03 -0800 | [diff] [blame] | 683 | } |
| 684 | |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 685 | private void removeCall(Call call) { |
Sailesh Nepal | 4857f47 | 2014-04-07 22:26:27 -0700 | [diff] [blame] | 686 | // If a handoff is pending then the original call shouldn't be removed. |
| 687 | Preconditions.checkState(call.getHandoffCallServiceDescriptor() == null); |
Santos Cordon | c499c1c | 2014-04-14 17:13:14 -0700 | [diff] [blame] | 688 | Log.v(this, "removeCall(%s)", call); |
Sailesh Nepal | 4857f47 | 2014-04-07 22:26:27 -0700 | [diff] [blame] | 689 | |
Santos Cordon | 766d04f | 2014-05-06 10:28:25 -0700 | [diff] [blame] | 690 | call.removeListener(this); |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 691 | call.clearCallService(); |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 692 | |
| 693 | boolean shouldNotify = false; |
| 694 | if (mCalls.contains(call)) { |
| 695 | mCalls.remove(call); |
| 696 | shouldNotify = true; |
Sailesh Nepal | 84fa5f8 | 2014-04-02 11:01:11 -0700 | [diff] [blame] | 697 | } else if (mPendingHandoffCalls.contains(call)) { |
Sailesh Nepal | 4857f47 | 2014-04-07 22:26:27 -0700 | [diff] [blame] | 698 | Log.v(this, "removeCall, marking handoff call as failed"); |
| 699 | completeHandoff(call, false); |
Santos Cordon | a56f276 | 2014-03-24 15:55:53 -0700 | [diff] [blame] | 700 | } |
Ben Gilad | a0d9f75 | 2014-02-26 11:49:03 -0800 | [diff] [blame] | 701 | |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 702 | // Only broadcast changes for calls that are being tracked. |
| 703 | if (shouldNotify) { |
| 704 | for (CallsManagerListener listener : mListeners) { |
| 705 | listener.onCallRemoved(call); |
| 706 | } |
| 707 | updateForegroundCall(); |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 708 | } |
| 709 | } |
Evan Charlton | 5c670a9 | 2014-03-06 14:58:20 -0800 | [diff] [blame] | 710 | |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 711 | /** |
Santos Cordon | 1ae2b85 | 2014-03-19 03:03:10 -0700 | [diff] [blame] | 712 | * Sets the specified state on the specified call. |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 713 | * |
| 714 | * @param call The call. |
| 715 | * @param newState The new state of the call. |
| 716 | */ |
| 717 | private void setCallState(Call call, CallState newState) { |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 718 | Preconditions.checkNotNull(newState); |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 719 | CallState oldState = call.getState(); |
Sailesh Nepal | 8c85dee | 2014-04-07 22:21:40 -0700 | [diff] [blame] | 720 | Log.i(this, "setCallState %s -> %s, call: %s", oldState, newState, call); |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 721 | if (newState != oldState) { |
| 722 | // Unfortunately, in the telephony world the radio is king. So if the call notifies |
| 723 | // us that the call is in a particular state, we allow it even if it doesn't make |
| 724 | // sense (e.g., ACTIVE -> RINGING). |
| 725 | // TODO(santoscordon): Consider putting a stop to the above and turning CallState |
| 726 | // into a well-defined state machine. |
| 727 | // TODO(santoscordon): Define expected state transitions here, and log when an |
| 728 | // unexpected transition occurs. |
| 729 | call.setState(newState); |
| 730 | |
| 731 | // Only broadcast state change for calls that are being tracked. |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 732 | if (mCalls.contains(call)) { |
Santos Cordon | a56f276 | 2014-03-24 15:55:53 -0700 | [diff] [blame] | 733 | for (CallsManagerListener listener : mListeners) { |
| 734 | listener.onCallStateChanged(call, oldState, newState); |
| 735 | } |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 736 | updateForegroundCall(); |
Santos Cordon | 4e9fffe | 2014-03-04 18:13:41 -0800 | [diff] [blame] | 737 | } |
| 738 | } |
| 739 | } |
| 740 | |
| 741 | /** |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 742 | * Checks which call should be visible to the user and have audio focus. |
Evan Charlton | 5c670a9 | 2014-03-06 14:58:20 -0800 | [diff] [blame] | 743 | */ |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 744 | private void updateForegroundCall() { |
| 745 | Call newForegroundCall = null; |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 746 | for (Call call : mCalls) { |
Santos Cordon | 40f78c2 | 2014-04-07 02:11:42 -0700 | [diff] [blame] | 747 | // TODO(santoscordon): Foreground-ness needs to be explicitly set. No call, regardless |
| 748 | // of its state will be foreground by default and instead the call service should be |
| 749 | // notified when its calls enter and exit foreground state. Foreground will mean that |
| 750 | // the call should play audio and listen to microphone if it wants. |
| 751 | |
| 752 | // Active calls have priority. |
| 753 | if (call.isActive()) { |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 754 | newForegroundCall = call; |
Evan Charlton | 5c670a9 | 2014-03-06 14:58:20 -0800 | [diff] [blame] | 755 | break; |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 756 | } |
Santos Cordon | 40f78c2 | 2014-04-07 02:11:42 -0700 | [diff] [blame] | 757 | |
| 758 | if (call.isAlive() || call.getState() == CallState.RINGING) { |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 759 | newForegroundCall = call; |
Santos Cordon | 40f78c2 | 2014-04-07 02:11:42 -0700 | [diff] [blame] | 760 | // Don't break in case there's an active call that has priority. |
Santos Cordon | 4e9fffe | 2014-03-04 18:13:41 -0800 | [diff] [blame] | 761 | } |
| 762 | } |
Santos Cordon | 4e9fffe | 2014-03-04 18:13:41 -0800 | [diff] [blame] | 763 | |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 764 | if (newForegroundCall != mForegroundCall) { |
Santos Cordon | 40f78c2 | 2014-04-07 02:11:42 -0700 | [diff] [blame] | 765 | Log.v(this, "Updating foreground call, %s -> %s.", mForegroundCall, newForegroundCall); |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 766 | Call oldForegroundCall = mForegroundCall; |
| 767 | mForegroundCall = newForegroundCall; |
| 768 | |
Santos Cordon | a56f276 | 2014-03-24 15:55:53 -0700 | [diff] [blame] | 769 | for (CallsManagerListener listener : mListeners) { |
| 770 | listener.onForegroundCallChanged(oldForegroundCall, mForegroundCall); |
| 771 | } |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 772 | } |
| 773 | } |
Sailesh Nepal | 84fa5f8 | 2014-04-02 11:01:11 -0700 | [diff] [blame] | 774 | |
Sailesh Nepal | 4857f47 | 2014-04-07 22:26:27 -0700 | [diff] [blame] | 775 | private void completeHandoff(Call handoffCall, boolean wasSuccessful) { |
Sailesh Nepal | 84fa5f8 | 2014-04-02 11:01:11 -0700 | [diff] [blame] | 776 | Call originalCall = handoffCall.getOriginalCall(); |
Sailesh Nepal | 4857f47 | 2014-04-07 22:26:27 -0700 | [diff] [blame] | 777 | Log.v(this, "complete handoff, %s -> %s, wasSuccessful: %b", handoffCall, originalCall, |
| 778 | wasSuccessful); |
Sailesh Nepal | 84fa5f8 | 2014-04-02 11:01:11 -0700 | [diff] [blame] | 779 | |
Sailesh Nepal | 4857f47 | 2014-04-07 22:26:27 -0700 | [diff] [blame] | 780 | // Remove the transient handoff call object (don't disconnect because the call could still |
| 781 | // be live). |
| 782 | mPendingHandoffCalls.remove(handoffCall); |
Santos Cordon | 6cb7ba9 | 2014-05-23 14:09:32 -0700 | [diff] [blame] | 783 | handoffCall.removeListener(this); |
Sailesh Nepal | 84fa5f8 | 2014-04-02 11:01:11 -0700 | [diff] [blame] | 784 | |
Sailesh Nepal | 4857f47 | 2014-04-07 22:26:27 -0700 | [diff] [blame] | 785 | if (wasSuccessful) { |
Sailesh Nepal | dcd6426 | 2014-05-23 17:03:28 -0700 | [diff] [blame] | 786 | if (TelephonyUtil.isCurrentlyPSTNCall(originalCall)) { |
| 787 | originalCall.disconnect(); |
| 788 | } |
Sailesh Nepal | 84fa5f8 | 2014-04-02 11:01:11 -0700 | [diff] [blame] | 789 | |
Sailesh Nepal | 4857f47 | 2014-04-07 22:26:27 -0700 | [diff] [blame] | 790 | // Synchronize. |
| 791 | originalCall.setCallService(handoffCall.getCallService(), handoffCall); |
| 792 | setCallState(originalCall, handoffCall.getState()); |
Sailesh Nepal | 84fa5f8 | 2014-04-02 11:01:11 -0700 | [diff] [blame] | 793 | |
Sailesh Nepal | 4857f47 | 2014-04-07 22:26:27 -0700 | [diff] [blame] | 794 | // Force the foreground call changed notification to be sent. |
| 795 | for (CallsManagerListener listener : mListeners) { |
| 796 | listener.onForegroundCallChanged(mForegroundCall, mForegroundCall); |
| 797 | } |
Sailesh Nepal | 8c85dee | 2014-04-07 22:21:40 -0700 | [diff] [blame] | 798 | |
Sailesh Nepal | 4857f47 | 2014-04-07 22:26:27 -0700 | [diff] [blame] | 799 | updateHandoffCallServiceDescriptor(originalCall, null); |
| 800 | } else { |
| 801 | updateHandoffCallServiceDescriptor(originalCall, null); |
| 802 | if (originalCall.getState() == CallState.DISCONNECTED || |
| 803 | originalCall.getState() == CallState.ABORTED) { |
| 804 | removeCall(originalCall); |
Sailesh Nepal | 84fa5f8 | 2014-04-02 11:01:11 -0700 | [diff] [blame] | 805 | } |
| 806 | } |
| 807 | } |
| 808 | |
Sailesh Nepal | 4857f47 | 2014-04-07 22:26:27 -0700 | [diff] [blame] | 809 | private void updateHandoffCallServiceDescriptor( |
| 810 | Call originalCall, |
| 811 | CallServiceDescriptor newDescriptor) { |
Sailesh Nepal | 8c85dee | 2014-04-07 22:21:40 -0700 | [diff] [blame] | 812 | CallServiceDescriptor oldDescriptor = originalCall.getHandoffCallServiceDescriptor(); |
Sailesh Nepal | 4857f47 | 2014-04-07 22:26:27 -0700 | [diff] [blame] | 813 | Log.v(this, "updateHandoffCallServiceDescriptor, call: %s, pending descriptor: %s -> %s", |
| 814 | originalCall, oldDescriptor, newDescriptor); |
Sailesh Nepal | 8c85dee | 2014-04-07 22:21:40 -0700 | [diff] [blame] | 815 | |
| 816 | if (!areDescriptorsEqual(oldDescriptor, newDescriptor)) { |
| 817 | originalCall.setHandoffCallServiceDescriptor(newDescriptor); |
| 818 | for (CallsManagerListener listener : mListeners) { |
| 819 | listener.onCallHandoffCallServiceDescriptorChanged(originalCall, oldDescriptor, |
| 820 | newDescriptor); |
| 821 | } |
| 822 | } |
| 823 | } |
| 824 | |
| 825 | private static boolean areDescriptorsEqual( |
| 826 | CallServiceDescriptor descriptor1, |
| 827 | CallServiceDescriptor descriptor2) { |
| 828 | if (descriptor1 == null) { |
| 829 | return descriptor2 == null; |
| 830 | } |
| 831 | return descriptor1.equals(descriptor2); |
| 832 | } |
| 833 | |
Sailesh Nepal | 84fa5f8 | 2014-04-02 11:01:11 -0700 | [diff] [blame] | 834 | private static boolean areUriEqual(Uri handle1, Uri handle2) { |
| 835 | if (handle1 == null) { |
| 836 | return handle2 == null; |
| 837 | } |
| 838 | return handle1.equals(handle2); |
| 839 | } |
Ben Gilad | 9f2bed3 | 2013-12-12 17:43:26 -0800 | [diff] [blame] | 840 | } |