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