blob: 56e8e511ddd0998482f73ec365aebd1d91bfcdc1 [file] [log] [blame]
Santos Cordon8e8b8d22013-12-19 14:14:05 -08001/*
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 Gilad9f2bed32013-12-12 17:43:26 -080017package com.android.telecomm;
18
Sailesh Nepalce704b92014-03-17 18:31:43 -070019import android.net.Uri;
Evan Charltona05805b2014-03-05 08:21:46 -080020import android.os.Bundle;
Sailesh Nepal6aca10a2014-03-24 16:11:02 -070021import android.telecomm.CallAudioState;
Sailesh Nepal810735e2014-03-18 18:15:46 -070022import android.telecomm.CallInfo;
Ben Giladc5b22692014-02-18 20:03:22 -080023import android.telecomm.CallServiceDescriptor;
Santos Cordon681663d2014-01-30 04:32:15 -080024import android.telecomm.CallState;
Yorke Lee33501632014-03-17 19:24:12 -070025import android.telecomm.GatewayInfo;
Nancy Chen77d2d0e2014-06-24 12:06:03 -070026import android.telecomm.Subscription;
Santos Cordon79ff2bc2014-03-27 15:31:27 -070027import android.telephony.DisconnectCause;
Santos Cordon8e8b8d22013-12-19 14:14:05 -080028
Santos Cordon681663d2014-01-30 04:32:15 -080029import com.google.common.base.Preconditions;
Sailesh Nepal810735e2014-03-18 18:15:46 -070030import com.google.common.collect.ImmutableCollection;
31import com.google.common.collect.ImmutableList;
Ben Gilad9f2bed32013-12-12 17:43:26 -080032
Santos Cordon23baed32014-06-27 14:45:39 -070033import java.util.ArrayList;
34import java.util.Arrays;
35import java.util.Collections;
Santos Cordonf3671a62014-05-29 21:51:53 -070036import java.util.HashSet;
37import java.util.LinkedHashSet;
Santos Cordona56f2762014-03-24 15:55:53 -070038import java.util.Set;
Ben Gilad9f2bed32013-12-12 17:43:26 -080039
Ben Giladdd8c6082013-12-30 14:44:08 -080040/**
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 Cordon64c7e962014-07-02 15:15:27 -070047public final class CallsManager extends Call.ListenerBase {
Ben Gilada0d9f752014-02-26 11:49:03 -080048
Santos Cordon74d420b2014-05-07 14:38:47 -070049 // TODO(santoscordon): Consider renaming this CallsManagerPlugin.
Sailesh Nepal810735e2014-03-18 18:15:46 -070050 interface CallsManagerListener {
51 void onCallAdded(Call call);
52 void onCallRemoved(Call call);
53 void onCallStateChanged(Call call, CallState oldState, CallState newState);
Sailesh Nepal8c85dee2014-04-07 22:21:40 -070054 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 Nepal810735e2014-03-18 18:15:46 -070063 void onIncomingCallAnswered(Call call);
Ihab Awadff7493a2014-06-10 13:47:44 -070064 void onIncomingCallRejected(Call call, boolean rejectWithMessage, String textMessage);
Sailesh Nepal810735e2014-03-18 18:15:46 -070065 void onForegroundCallChanged(Call oldForegroundCall, Call newForegroundCall);
Sailesh Nepal6aca10a2014-03-24 16:11:02 -070066 void onAudioStateChanged(CallAudioState oldAudioState, CallAudioState newAudioState);
Ihab Awadcb387ac2014-05-28 16:49:38 -070067 void onRequestingRingback(Call call, boolean ringback);
Santos Cordona1610702014-06-04 20:22:56 -070068 void onIsConferenceCapableChanged(Call call, boolean isConferenceCapable);
69 void onIsConferencedChanged(Call call);
Ihab Awadff7493a2014-06-10 13:47:44 -070070 void onCannedSmsResponsesLoaded(Call call);
Nancy Chena65d41f2014-06-24 12:06:03 -070071 void onCallVideoProviderChanged(Call call);
Tyler Gunne19cc002014-07-01 11:32:53 -070072 void onFeaturesChanged(Call call);
Sailesh Nepal810735e2014-03-18 18:15:46 -070073 }
74
Santos Cordon8e8b8d22013-12-19 14:14:05 -080075 private static final CallsManager INSTANCE = new CallsManager();
Ben Gilad9f2bed32013-12-12 17:43:26 -080076
Santos Cordon8e8b8d22013-12-19 14:14:05 -080077 /**
Sailesh Nepale59bb192014-04-01 18:33:59 -070078 * 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 Cordon681663d2014-01-30 04:32:15 -080080 */
Santos Cordonf3671a62014-05-29 21:51:53 -070081 private final Set<Call> mCalls = new LinkedHashSet<>();
Santos Cordon681663d2014-01-30 04:32:15 -080082
83 /**
Sailesh Nepal84fa5f82014-04-02 11:01:11 -070084 * 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 Cordonf3671a62014-05-29 21:51:53 -070087 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 Cordondeb8c892014-05-30 01:38:03 -070095 private final HeadsetMediaButton mHeadsetMediaButton;
Sailesh Nepal84fa5f82014-04-02 11:01:11 -070096
97 /**
Sailesh Nepal810735e2014-03-18 18:15:46 -070098 * 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 Cordon4e9fffe2014-03-04 18:13:41 -0800100 */
Sailesh Nepal810735e2014-03-18 18:15:46 -0700101 private Call mForegroundCall;
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800102
Santos Cordon766d04f2014-05-06 10:28:25 -0700103 /** Singleton accessor. */
104 static CallsManager getInstance() {
105 return INSTANCE;
106 }
Ben Gilad9f2bed32013-12-12 17:43:26 -0800107
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800108 /**
Ben Gilad8bdaa462014-02-05 12:53:19 -0800109 * Initializes the required Telecomm components.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800110 */
111 private CallsManager() {
Santos Cordona0e5f1a2014-03-31 21:43:00 -0700112 TelecommApp app = TelecommApp.getInstance();
Santos Cordona56f2762014-03-24 15:55:53 -0700113
Santos Cordondeb8c892014-05-30 01:38:03 -0700114 StatusBarNotifier statusBarNotifier = new StatusBarNotifier(app, this);
115 mCallAudioManager = new CallAudioManager(app, statusBarNotifier);
Santos Cordonc7b8eba2014-04-01 15:26:28 -0700116 InCallTonePlayer.Factory playerFactory = new InCallTonePlayer.Factory(mCallAudioManager);
Santos Cordonae193062014-05-21 21:21:49 -0700117 mRinger = new Ringer(mCallAudioManager, this, playerFactory, app);
Santos Cordondeb8c892014-05-30 01:38:03 -0700118 mHeadsetMediaButton = new HeadsetMediaButton(app, this);
Santos Cordonae193062014-05-21 21:21:49 -0700119
Santos Cordondeb8c892014-05-30 01:38:03 -0700120 mListeners.add(statusBarNotifier);
Santos Cordona0e5f1a2014-03-31 21:43:00 -0700121 mListeners.add(new CallLogManager(app));
Santos Cordona56f2762014-03-24 15:55:53 -0700122 mListeners.add(new PhoneStateBroadcaster());
Santos Cordonf3671a62014-05-29 21:51:53 -0700123 mListeners.add(mInCallController);
Santos Cordonae193062014-05-21 21:21:49 -0700124 mListeners.add(mRinger);
Santos Cordonc7b8eba2014-04-01 15:26:28 -0700125 mListeners.add(new RingbackPlayer(this, playerFactory));
126 mListeners.add(new InCallToneMonitor(playerFactory, this));
Santos Cordona56f2762014-03-24 15:55:53 -0700127 mListeners.add(mCallAudioManager);
Santos Cordona0e5f1a2014-03-31 21:43:00 -0700128 mListeners.add(app.getMissedCallNotifier());
Santos Cordon92a2d812014-04-30 15:19:01 -0700129 mListeners.add(mDtmfLocalTonePlayer);
Santos Cordon81289982014-06-03 16:03:08 -0700130 mListeners.add(mHeadsetMediaButton);
Ihab Awadff7493a2014-06-10 13:47:44 -0700131 mListeners.add(RespondViaSmsManager.getInstance());
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800132 }
133
Santos Cordon766d04f2014-05-06 10:28:25 -0700134 @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 Cordon6cb7ba92014-05-23 14:09:32 -0700145 } else {
146 Log.wtf(this, "unexpected successful call notification: %s", call);
147 return;
Santos Cordon766d04f2014-05-06 10:28:25 -0700148 }
Santos Cordon6cb7ba92014-05-23 14:09:32 -0700149
150 markCallAsDialing(call);
Santos Cordon766d04f2014-05-06 10:28:25 -0700151 }
152
153 @Override
Sailesh Nepal5a73b032014-06-25 15:53:21 -0700154 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 Cordon766d04f2014-05-06 10:28:25 -0700165 }
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 Gilada0d9f752014-02-26 11:49:03 -0800177 }
178
Ihab Awadcb387ac2014-05-28 16:49:38 -0700179 @Override
Santos Cordona1610702014-06-04 20:22:56 -0700180 public void onIsConferenceCapableChanged(Call call, boolean isConferenceCapable) {
181 for (CallsManagerListener listener : mListeners) {
182 listener.onIsConferenceCapableChanged(call, isConferenceCapable);
183 }
184 }
185
186 @Override
Ihab Awadcb387ac2014-05-28 16:49:38 -0700187 public void onRequestingRingback(Call call, boolean ringback) {
188 for (CallsManagerListener listener : mListeners) {
189 listener.onRequestingRingback(call, ringback);
190 }
191 }
192
Evan Charlton352105c2014-06-03 14:10:54 -0700193 @Override
194 public void onPostDialWait(Call call, String remaining) {
195 mInCallController.onPostDialWait(call, remaining);
196 }
197
Santos Cordona1610702014-06-04 20:22:56 -0700198 @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 Awadff7493a2014-06-10 13:47:44 -0700226 @Override
227 public void onCannedSmsResponsesLoaded(Call call) {
228 for (CallsManagerListener listener : mListeners) {
229 listener.onCannedSmsResponsesLoaded(call);
230 }
231 }
232
Nancy Chena65d41f2014-06-24 12:06:03 -0700233 @Override
234 public void onCallVideoProviderChanged(Call call) {
235 for (CallsManagerListener listener : mListeners) {
236 listener.onCallVideoProviderChanged(call);
237 }
238 }
239
Tyler Gunne19cc002014-07-01 11:32:53 -0700240 @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 Nepal810735e2014-03-18 18:15:46 -0700248 ImmutableCollection<Call> getCalls() {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700249 return ImmutableList.copyOf(mCalls);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700250 }
251
252 Call getForegroundCall() {
253 return mForegroundCall;
254 }
255
Santos Cordonae193062014-05-21 21:21:49 -0700256 Ringer getRinger() {
257 return mRinger;
258 }
259
Santos Cordonf3671a62014-05-29 21:51:53 -0700260 InCallController getInCallController() {
261 return mInCallController;
262 }
263
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700264 boolean hasEmergencyCall() {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700265 for (Call call : mCalls) {
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700266 if (call.isEmergencyCall()) {
267 return true;
268 }
269 }
270 return false;
271 }
272
273 CallAudioState getAudioState() {
274 return mCallAudioManager.getAudioState();
275 }
276
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800277 /**
Santos Cordon493e8f22014-02-19 03:15:12 -0800278 * 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 Cordon766d04f2014-05-06 10:28:25 -0700280 * to {@link #onSuccessfulIncomingCall} to start the in-call UI.
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800281 *
Ben Giladc5b22692014-02-18 20:03:22 -0800282 * @param descriptor The descriptor of the call service to use for this incoming call.
Evan Charltona05805b2014-03-05 08:21:46 -0800283 * @param extras The optional extras Bundle passed with the intent used for the incoming call.
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800284 */
Evan Charltona05805b2014-03-05 08:21:46 -0800285 void processIncomingCallIntent(CallServiceDescriptor descriptor, Bundle extras) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800286 Log.d(this, "processIncomingCallIntent");
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800287 // Create a call with no handle. Eventually, switchboard will update the call with
Sailesh Nepale59bb192014-04-01 18:33:59 -0700288 // additional information from the call service, but for now we just need one to pass
289 // around.
Santos Cordona1610702014-06-04 20:22:56 -0700290 Call call = new Call(true /* isIncoming */, false /* isConference */);
Santos Cordon766d04f2014-05-06 10:28:25 -0700291 // TODO(santoscordon): Move this to be a part of addCall()
292 call.addListener(this);
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800293
Santos Cordon766d04f2014-05-06 10:28:25 -0700294 call.startIncoming(descriptor, extras);
Ben Gilada0d9f752014-02-26 11:49:03 -0800295 }
296
297 /**
Yorke Lee33501632014-03-17 19:24:12 -0700298 * Attempts to issue/connect the specified call.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800299 *
Yorke Lee33501632014-03-17 19:24:12 -0700300 * @param handle Handle to connect the call with.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800301 * @param contactInfo Information about the entity being called.
Yorke Lee33501632014-03-17 19:24:12 -0700302 * @param gatewayInfo Optional gateway information that can be used to route the call to the
Santos Cordon5b7b9b32014-03-26 14:00:22 -0700303 * actual dialed handle via a gateway provider. May be null.
Sai Cheemalapatib7157e92014-06-11 17:51:55 -0700304 * @param speakerphoneOn Whether or not to turn the speakerphone on once the call connects.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800305 */
Sai Cheemalapatib7157e92014-06-11 17:51:55 -0700306 void placeOutgoingCall(Uri handle, ContactInfo contactInfo, GatewayInfo gatewayInfo,
Nancy Chen77d2d0e2014-06-24 12:06:03 -0700307 Subscription subscription, boolean speakerphoneOn) {
Yorke Lee33501632014-03-17 19:24:12 -0700308 final Uri uriHandle = (gatewayInfo == null) ? handle : gatewayInfo.getGatewayHandle();
309
310 if (gatewayInfo == null) {
Santos Cordonb58f4532014-05-29 11:59:06 -0700311 Log.i(this, "Creating a new outgoing call with handle: %s", Log.piiHandle(uriHandle));
Yorke Lee33501632014-03-17 19:24:12 -0700312 } 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 Cordon766d04f2014-05-06 10:28:25 -0700316
Santos Cordona1610702014-06-04 20:22:56 -0700317 Call call = new Call(
Nancy Chen77d2d0e2014-06-24 12:06:03 -0700318 uriHandle, gatewayInfo, subscription,
319 false /* isIncoming */, false /* isConference */);
Sai Cheemalapatib7157e92014-06-11 17:51:55 -0700320 call.setStartWithSpeakerphoneOn(speakerphoneOn);
Santos Cordon766d04f2014-05-06 10:28:25 -0700321
322 // TODO(santoscordon): Move this to be a part of addCall()
323 call.addListener(this);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700324 addCall(call);
Santos Cordone3d76ab2014-01-28 17:25:20 -0800325
Santos Cordon766d04f2014-05-06 10:28:25 -0700326 call.startOutgoing();
Yorke Leef98fb572014-03-05 10:56:55 -0800327 }
328
329 /**
Santos Cordona1610702014-06-04 20:22:56 -0700330 * 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 Cordone3d76ab2014-01-28 17:25:20 -0800341 * 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 Cordone3d76ab2014-01-28 17:25:20 -0800344 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700345 void answerCall(Call call) {
346 if (!mCalls.contains(call)) {
347 Log.i(this, "Request to answer a non-existent call %s", call);
Santos Cordon61d0f702014-02-19 02:52:23 -0800348 } else {
Santos Cordon40f78c22014-04-07 02:11:42 -0700349 // 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 Cordona56f2762014-03-24 15:55:53 -0700362 for (CallsManagerListener listener : mListeners) {
363 listener.onIncomingCallAnswered(call);
364 }
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800365
Santos Cordon61d0f702014-02-19 02:52:23 -0800366 // We do not update the UI until we get confirmation of the answer() through
Sailesh Nepale59bb192014-04-01 18:33:59 -0700367 // {@link #markCallAsActive}.
Santos Cordon61d0f702014-02-19 02:52:23 -0800368 call.answer();
369 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800370 }
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 Cordone3d76ab2014-01-28 17:25:20 -0800376 */
Ihab Awadff7493a2014-06-10 13:47:44 -0700377 void rejectCall(Call call, boolean rejectWithMessage, String textMessage) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700378 if (!mCalls.contains(call)) {
379 Log.i(this, "Request to reject a non-existent call %s", call);
Santos Cordon61d0f702014-02-19 02:52:23 -0800380 } else {
Santos Cordona56f2762014-03-24 15:55:53 -0700381 for (CallsManagerListener listener : mListeners) {
Ihab Awadff7493a2014-06-10 13:47:44 -0700382 listener.onIncomingCallRejected(call, rejectWithMessage, textMessage);
Santos Cordona56f2762014-03-24 15:55:53 -0700383 }
Ihab Awadff7493a2014-06-10 13:47:44 -0700384 call.reject(rejectWithMessage, textMessage);
Santos Cordon61d0f702014-02-19 02:52:23 -0800385 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800386 }
387
388 /**
Ihab Awad74549ec2014-03-10 15:33:25 -0700389 * Instructs Telecomm to play the specified DTMF tone within the specified call.
390 *
Ihab Awad74549ec2014-03-10 15:33:25 -0700391 * @param digit The DTMF digit to play.
392 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700393 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 Awad74549ec2014-03-10 15:33:25 -0700396 } else {
397 call.playDtmfTone(digit);
Santos Cordon92a2d812014-04-30 15:19:01 -0700398 mDtmfLocalTonePlayer.playTone(call, digit);
Ihab Awad74549ec2014-03-10 15:33:25 -0700399 }
400 }
401
402 /**
403 * Instructs Telecomm to stop the currently playing DTMF tone, if any.
Ihab Awad74549ec2014-03-10 15:33:25 -0700404 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700405 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 Awad74549ec2014-03-10 15:33:25 -0700408 } else {
409 call.stopDtmfTone();
Santos Cordon92a2d812014-04-30 15:19:01 -0700410 mDtmfLocalTonePlayer.stopTone(call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700411 }
412 }
413
414 /**
Evan Charlton352105c2014-06-03 14:10:54 -0700415 * Instructs Telecomm to continue (or not) the current post-dial DTMF string, if any.
Ihab Awad74549ec2014-03-10 15:33:25 -0700416 */
Evan Charlton352105c2014-06-03 14:10:54 -0700417 void postDialContinue(Call call, boolean proceed) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700418 if (!mCalls.contains(call)) {
419 Log.i(this, "Request to continue post-dial string in a non-existent call %s", call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700420 } else {
Evan Charlton352105c2014-06-03 14:10:54 -0700421 call.postDialContinue(proceed);
Ihab Awad74549ec2014-03-10 15:33:25 -0700422 }
423 }
424
425 /**
Santos Cordone3d76ab2014-01-28 17:25:20 -0800426 * 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 Cordone3d76ab2014-01-28 17:25:20 -0800429 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700430 void disconnectCall(Call call) {
Santos Cordon682fe6b2014-05-20 08:56:39 -0700431 Log.v(this, "disconnectCall %s", call);
432
Sailesh Nepale59bb192014-04-01 18:33:59 -0700433 if (!mCalls.contains(call)) {
434 Log.w(this, "Unknown call (%s) asked to disconnect", call);
Santos Cordon049b7b62014-01-30 05:34:26 -0800435 } else {
436 call.disconnect();
437 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800438 }
Santos Cordon681663d2014-01-30 04:32:15 -0800439
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700440 /**
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 Leecdf3ebd2014-03-12 18:31:41 -0700444 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700445 void holdCall(Call call) {
446 if (!mCalls.contains(call)) {
447 Log.w(this, "Unknown call (%s) asked to be put on hold", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700448 } else {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700449 Log.d(this, "Putting call on hold: (%s)", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700450 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 Leecdf3ebd2014-03-12 18:31:41 -0700458 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700459 void unholdCall(Call call) {
460 if (!mCalls.contains(call)) {
461 Log.w(this, "Unknown call (%s) asked to be removed from hold", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700462 } else {
Santos Cordonc7e85d42014-05-22 02:51:48 -0700463 Log.d(this, "unholding call: (%s)", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700464 call.unhold();
465 }
466 }
467
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700468 /** 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 Nepal84fa5f82014-04-02 11:01:11 -0700481 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 Cordona1610702014-06-04 20:22:56 -0700498 Call tempCall = new Call(
Nancy Chen77d2d0e2014-06-24 12:06:03 -0700499 originalCall.getHandoffHandle(), originalCall.getGatewayInfo(),
500 originalCall.getSubscription(), false, false);
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700501 tempCall.setOriginalCall(originalCall);
502 tempCall.setExtras(originalCall.getExtras());
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700503 mPendingHandoffCalls.add(tempCall);
Santos Cordon6cb7ba92014-05-23 14:09:32 -0700504 tempCall.addListener(this);
505
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700506 Log.d(this, "Placing handoff call");
Santos Cordon766d04f2014-05-06 10:28:25 -0700507 tempCall.startOutgoing();
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700508 }
509
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700510 /** Called when the audio state changes. */
511 void onAudioStateChanged(CallAudioState oldAudioState, CallAudioState newAudioState) {
512 Log.v(this, "onAudioStateChanged, audioState: %s -> %s", oldAudioState, newAudioState);
Santos Cordona56f2762014-03-24 15:55:53 -0700513 for (CallsManagerListener listener : mListeners) {
514 listener.onAudioStateChanged(oldAudioState, newAudioState);
515 }
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700516 }
517
Sailesh Nepale59bb192014-04-01 18:33:59 -0700518 void markCallAsRinging(Call call) {
519 setCallState(call, CallState.RINGING);
Santos Cordon681663d2014-01-30 04:32:15 -0800520 }
521
Sailesh Nepale59bb192014-04-01 18:33:59 -0700522 void markCallAsDialing(Call call) {
523 setCallState(call, CallState.DIALING);
Santos Cordon681663d2014-01-30 04:32:15 -0800524 }
525
Sailesh Nepale59bb192014-04-01 18:33:59 -0700526 void markCallAsActive(Call call) {
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700527 if (call.getConnectTimeMillis() == 0) {
528 call.setConnectTimeMillis(System.currentTimeMillis());
529 }
Sailesh Nepale59bb192014-04-01 18:33:59 -0700530 setCallState(call, CallState.ACTIVE);
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700531
532 if (mPendingHandoffCalls.contains(call)) {
Sailesh Nepal4857f472014-04-07 22:26:27 -0700533 completeHandoff(call, true);
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700534 }
Sai Cheemalapatib7157e92014-06-11 17:51:55 -0700535 if (call.getStartWithSpeakerphoneOn()) {
536 setAudioRoute(CallAudioState.ROUTE_SPEAKER);
537 }
Santos Cordon681663d2014-01-30 04:32:15 -0800538 }
539
Sailesh Nepale59bb192014-04-01 18:33:59 -0700540 void markCallAsOnHold(Call call) {
541 setCallState(call, CallState.ON_HOLD);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700542 }
543
Santos Cordon049b7b62014-01-30 05:34:26 -0800544 /**
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 Cordon79ff2bc2014-03-27 15:31:27 -0700548 * @param disconnectCause The disconnect reason, see {@link android.telephony.DisconnectCause}.
549 * @param disconnectMessage Optional call-service-provided message about the disconnect.
Santos Cordon049b7b62014-01-30 05:34:26 -0800550 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700551 void markCallAsDisconnected(Call call, int disconnectCause, String disconnectMessage) {
552 call.setDisconnectCause(disconnectCause, disconnectMessage);
553 setCallState(call, CallState.DISCONNECTED);
Sailesh Nepal4857f472014-04-07 22:26:27 -0700554
555 // Only remove the call if handoff is not pending.
556 if (call.getHandoffCallServiceDescriptor() == null) {
557 removeCall(call);
558 }
Ben Gilada0d9f752014-02-26 11:49:03 -0800559 }
560
Sailesh Nepal6ab6fb72014-04-01 20:03:19 -0700561 void setHandoffInfo(Call call, Uri handle, Bundle extras) {
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700562 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 Nepal6ab6fb72014-04-01 20:03:19 -0700581 }
582
Ben Gilada0d9f752014-02-26 11:49:03 -0800583 /**
Santos Cordon4b2c1192014-03-19 18:15:38 -0700584 * 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 Nepale59bb192014-04-01 18:33:59 -0700591 for (Call call : ImmutableList.copyOf(mCalls)) {
Santos Cordon4b2c1192014-03-19 18:15:38 -0700592 if (call.getCallService() == callService) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700593 markCallAsDisconnected(call, DisconnectCause.ERROR_UNSPECIFIED, null);
Santos Cordon4b2c1192014-03-19 18:15:38 -0700594 }
595 }
596 }
597
Santos Cordondeb8c892014-05-30 01:38:03 -0700598 boolean hasAnyCalls() {
599 return !mCalls.isEmpty();
600 }
601
Santos Cordonc7e85d42014-05-22 02:51:48 -0700602 boolean hasActiveOrHoldingCall() {
Santos Cordon23baed32014-06-27 14:45:39 -0700603 return getFirstCallWithState(CallState.ACTIVE, CallState.ON_HOLD) != null;
Santos Cordonc7e85d42014-05-22 02:51:48 -0700604 }
605
606 boolean hasRingingCall() {
Santos Cordon23baed32014-06-27 14:45:39 -0700607 return getFirstCallWithState(CallState.RINGING) != null;
Santos Cordonc7e85d42014-05-22 02:51:48 -0700608 }
609
Santos Cordondeb8c892014-05-30 01:38:03 -0700610 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 Cordon10838c22014-06-11 17:36:04 -0700635 * 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 Cordondeb8c892014-05-30 01:38:03 -0700658 * 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 Cordon23baed32014-06-27 14:45:39 -0700662 Call getFirstCallWithState(CallState... states) {
Santos Cordondeb8c892014-05-30 01:38:03 -0700663 for (CallState currentState : states) {
Santos Cordon23baed32014-06-27 14:45:39 -0700664 // check the foreground first
665 if (mForegroundCall != null && mForegroundCall.getState() == currentState) {
666 return mForegroundCall;
667 }
668
Santos Cordondeb8c892014-05-30 01:38:03 -0700669 for (Call call : mCalls) {
670 if (currentState == call.getState()) {
671 return call;
672 }
673 }
674 }
675 return null;
676 }
677
Santos Cordon4b2c1192014-03-19 18:15:38 -0700678 /**
Ben Gilada0d9f752014-02-26 11:49:03 -0800679 * 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 Nepale59bb192014-04-01 18:33:59 -0700684 mCalls.add(call);
Santos Cordon40f78c22014-04-07 02:11:42 -0700685
686 // TODO(santoscordon): Update mForegroundCall prior to invoking
687 // onCallAdded for calls which immediately take the foreground (like the first call).
Santos Cordona56f2762014-03-24 15:55:53 -0700688 for (CallsManagerListener listener : mListeners) {
689 listener.onCallAdded(call);
690 }
Sailesh Nepal810735e2014-03-18 18:15:46 -0700691 updateForegroundCall();
Ben Gilada0d9f752014-02-26 11:49:03 -0800692 }
693
Sailesh Nepal810735e2014-03-18 18:15:46 -0700694 private void removeCall(Call call) {
Sailesh Nepal4857f472014-04-07 22:26:27 -0700695 // If a handoff is pending then the original call shouldn't be removed.
696 Preconditions.checkState(call.getHandoffCallServiceDescriptor() == null);
Santos Cordonc499c1c2014-04-14 17:13:14 -0700697 Log.v(this, "removeCall(%s)", call);
Sailesh Nepal4857f472014-04-07 22:26:27 -0700698
Santos Cordon766d04f2014-05-06 10:28:25 -0700699 call.removeListener(this);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700700 call.clearCallService();
Sailesh Nepale59bb192014-04-01 18:33:59 -0700701
702 boolean shouldNotify = false;
703 if (mCalls.contains(call)) {
704 mCalls.remove(call);
705 shouldNotify = true;
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700706 } else if (mPendingHandoffCalls.contains(call)) {
Sailesh Nepal4857f472014-04-07 22:26:27 -0700707 Log.v(this, "removeCall, marking handoff call as failed");
708 completeHandoff(call, false);
Santos Cordona56f2762014-03-24 15:55:53 -0700709 }
Ben Gilada0d9f752014-02-26 11:49:03 -0800710
Sailesh Nepale59bb192014-04-01 18:33:59 -0700711 // 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 Nepal810735e2014-03-18 18:15:46 -0700717 }
718 }
Evan Charlton5c670a92014-03-06 14:58:20 -0800719
Sailesh Nepal810735e2014-03-18 18:15:46 -0700720 /**
Santos Cordon1ae2b852014-03-19 03:03:10 -0700721 * Sets the specified state on the specified call.
Sailesh Nepal810735e2014-03-18 18:15:46 -0700722 *
723 * @param call The call.
724 * @param newState The new state of the call.
725 */
726 private void setCallState(Call call, CallState newState) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700727 Preconditions.checkNotNull(newState);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700728 CallState oldState = call.getState();
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700729 Log.i(this, "setCallState %s -> %s, call: %s", oldState, newState, call);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700730 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 Nepale59bb192014-04-01 18:33:59 -0700741 if (mCalls.contains(call)) {
Santos Cordona56f2762014-03-24 15:55:53 -0700742 for (CallsManagerListener listener : mListeners) {
743 listener.onCallStateChanged(call, oldState, newState);
744 }
Sailesh Nepal810735e2014-03-18 18:15:46 -0700745 updateForegroundCall();
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800746 }
747 }
748 }
749
750 /**
Sailesh Nepal810735e2014-03-18 18:15:46 -0700751 * Checks which call should be visible to the user and have audio focus.
Evan Charlton5c670a92014-03-06 14:58:20 -0800752 */
Sailesh Nepal810735e2014-03-18 18:15:46 -0700753 private void updateForegroundCall() {
754 Call newForegroundCall = null;
Sailesh Nepale59bb192014-04-01 18:33:59 -0700755 for (Call call : mCalls) {
Santos Cordon40f78c22014-04-07 02:11:42 -0700756 // 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 Nepal810735e2014-03-18 18:15:46 -0700763 newForegroundCall = call;
Evan Charlton5c670a92014-03-06 14:58:20 -0800764 break;
Sailesh Nepal810735e2014-03-18 18:15:46 -0700765 }
Santos Cordon40f78c22014-04-07 02:11:42 -0700766
767 if (call.isAlive() || call.getState() == CallState.RINGING) {
Sailesh Nepal810735e2014-03-18 18:15:46 -0700768 newForegroundCall = call;
Santos Cordon40f78c22014-04-07 02:11:42 -0700769 // Don't break in case there's an active call that has priority.
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800770 }
771 }
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800772
Sailesh Nepal810735e2014-03-18 18:15:46 -0700773 if (newForegroundCall != mForegroundCall) {
Santos Cordon40f78c22014-04-07 02:11:42 -0700774 Log.v(this, "Updating foreground call, %s -> %s.", mForegroundCall, newForegroundCall);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700775 Call oldForegroundCall = mForegroundCall;
776 mForegroundCall = newForegroundCall;
777
Santos Cordona56f2762014-03-24 15:55:53 -0700778 for (CallsManagerListener listener : mListeners) {
779 listener.onForegroundCallChanged(oldForegroundCall, mForegroundCall);
780 }
Santos Cordon681663d2014-01-30 04:32:15 -0800781 }
782 }
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700783
Sailesh Nepal4857f472014-04-07 22:26:27 -0700784 private void completeHandoff(Call handoffCall, boolean wasSuccessful) {
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700785 Call originalCall = handoffCall.getOriginalCall();
Sailesh Nepal4857f472014-04-07 22:26:27 -0700786 Log.v(this, "complete handoff, %s -> %s, wasSuccessful: %b", handoffCall, originalCall,
787 wasSuccessful);
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700788
Sailesh Nepal4857f472014-04-07 22:26:27 -0700789 // Remove the transient handoff call object (don't disconnect because the call could still
790 // be live).
791 mPendingHandoffCalls.remove(handoffCall);
Santos Cordon6cb7ba92014-05-23 14:09:32 -0700792 handoffCall.removeListener(this);
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700793
Sailesh Nepal4857f472014-04-07 22:26:27 -0700794 if (wasSuccessful) {
Sailesh Nepaldcd64262014-05-23 17:03:28 -0700795 if (TelephonyUtil.isCurrentlyPSTNCall(originalCall)) {
796 originalCall.disconnect();
797 }
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700798
Sailesh Nepal4857f472014-04-07 22:26:27 -0700799 // Synchronize.
800 originalCall.setCallService(handoffCall.getCallService(), handoffCall);
801 setCallState(originalCall, handoffCall.getState());
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700802
Sailesh Nepal4857f472014-04-07 22:26:27 -0700803 // Force the foreground call changed notification to be sent.
804 for (CallsManagerListener listener : mListeners) {
805 listener.onForegroundCallChanged(mForegroundCall, mForegroundCall);
806 }
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700807
Sailesh Nepal4857f472014-04-07 22:26:27 -0700808 updateHandoffCallServiceDescriptor(originalCall, null);
809 } else {
810 updateHandoffCallServiceDescriptor(originalCall, null);
811 if (originalCall.getState() == CallState.DISCONNECTED ||
812 originalCall.getState() == CallState.ABORTED) {
813 removeCall(originalCall);
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700814 }
815 }
816 }
817
Sailesh Nepal4857f472014-04-07 22:26:27 -0700818 private void updateHandoffCallServiceDescriptor(
819 Call originalCall,
820 CallServiceDescriptor newDescriptor) {
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700821 CallServiceDescriptor oldDescriptor = originalCall.getHandoffCallServiceDescriptor();
Sailesh Nepal4857f472014-04-07 22:26:27 -0700822 Log.v(this, "updateHandoffCallServiceDescriptor, call: %s, pending descriptor: %s -> %s",
823 originalCall, oldDescriptor, newDescriptor);
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700824
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 Nepal84fa5f82014-04-02 11:01:11 -0700843 private static boolean areUriEqual(Uri handle1, Uri handle2) {
844 if (handle1 == null) {
845 return handle2 == null;
846 }
847 return handle1.equals(handle2);
848 }
Ben Gilad9f2bed32013-12-12 17:43:26 -0800849}