blob: 7aeaed57ef53b4a46f93c7ec1404ddee0528d826 [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;
Santos Cordon79ff2bc2014-03-27 15:31:27 -070026import android.telephony.DisconnectCause;
Santos Cordon8e8b8d22013-12-19 14:14:05 -080027
Santos Cordon681663d2014-01-30 04:32:15 -080028import com.google.common.base.Preconditions;
Sailesh Nepal810735e2014-03-18 18:15:46 -070029import com.google.common.collect.ImmutableCollection;
30import com.google.common.collect.ImmutableList;
Santos Cordona56f2762014-03-24 15:55:53 -070031import com.google.common.collect.Sets;
Ben Gilad9f2bed32013-12-12 17:43:26 -080032
Santos Cordona56f2762014-03-24 15:55:53 -070033import java.util.Set;
Ben Gilad9f2bed32013-12-12 17:43:26 -080034
Ben Giladdd8c6082013-12-30 14:44:08 -080035/**
36 * Singleton.
37 *
38 * NOTE(gilad): by design most APIs are package private, use the relevant adapter/s to allow
39 * access from other packages specifically refraining from passing the CallsManager instance
40 * beyond the com.android.telecomm package boundary.
41 */
Santos Cordon766d04f2014-05-06 10:28:25 -070042public final class CallsManager implements Call.Listener {
Ben Gilada0d9f752014-02-26 11:49:03 -080043
Santos Cordon74d420b2014-05-07 14:38:47 -070044 // TODO(santoscordon): Consider renaming this CallsManagerPlugin.
Sailesh Nepal810735e2014-03-18 18:15:46 -070045 interface CallsManagerListener {
46 void onCallAdded(Call call);
47 void onCallRemoved(Call call);
48 void onCallStateChanged(Call call, CallState oldState, CallState newState);
Sailesh Nepal8c85dee2014-04-07 22:21:40 -070049 void onCallHandoffHandleChanged(Call call, Uri oldHandle, Uri newHandle);
50 void onCallServiceChanged(
51 Call call,
52 CallServiceWrapper oldCallService,
53 CallServiceWrapper newCallService);
54 void onCallHandoffCallServiceDescriptorChanged(
55 Call call,
56 CallServiceDescriptor oldDescriptor,
57 CallServiceDescriptor newDescriptor);
Sailesh Nepal810735e2014-03-18 18:15:46 -070058 void onIncomingCallAnswered(Call call);
59 void onIncomingCallRejected(Call call);
60 void onForegroundCallChanged(Call oldForegroundCall, Call newForegroundCall);
Sailesh Nepal6aca10a2014-03-24 16:11:02 -070061 void onAudioStateChanged(CallAudioState oldAudioState, CallAudioState newAudioState);
Sailesh Nepal810735e2014-03-18 18:15:46 -070062 }
63
Santos Cordon8e8b8d22013-12-19 14:14:05 -080064 private static final CallsManager INSTANCE = new CallsManager();
Ben Gilad9f2bed32013-12-12 17:43:26 -080065
Santos Cordon8e8b8d22013-12-19 14:14:05 -080066 /**
Sailesh Nepale59bb192014-04-01 18:33:59 -070067 * The main call repository. Keeps an instance of all live calls. New incoming and outgoing
68 * calls are added to the map and removed when the calls move to the disconnected state.
Santos Cordon681663d2014-01-30 04:32:15 -080069 */
Sailesh Nepale59bb192014-04-01 18:33:59 -070070 private final Set<Call> mCalls = Sets.newLinkedHashSet();
Santos Cordon681663d2014-01-30 04:32:15 -080071
72 /**
Sailesh Nepal84fa5f82014-04-02 11:01:11 -070073 * Set of new calls created to perform a handoff. The calls are added when handoff is initiated
74 * and removed when hadnoff is complete.
75 */
76 private final Set<Call> mPendingHandoffCalls = Sets.newLinkedHashSet();
77
78 /**
Sailesh Nepal810735e2014-03-18 18:15:46 -070079 * The call the user is currently interacting with. This is the call that should have audio
80 * focus and be visible in the in-call UI.
Santos Cordon4e9fffe2014-03-04 18:13:41 -080081 */
Sailesh Nepal810735e2014-03-18 18:15:46 -070082 private Call mForegroundCall;
Santos Cordon4e9fffe2014-03-04 18:13:41 -080083
Santos Cordon92a2d812014-04-30 15:19:01 -070084 private final DtmfLocalTonePlayer mDtmfLocalTonePlayer = new DtmfLocalTonePlayer();
85
Sailesh Nepal6aca10a2014-03-24 16:11:02 -070086 private final CallAudioManager mCallAudioManager;
Sailesh Nepal810735e2014-03-18 18:15:46 -070087
Santos Cordonb64c1502014-05-21 21:21:49 -070088 private final Ringer mRinger;
89
Santos Cordona56f2762014-03-24 15:55:53 -070090 private final Set<CallsManagerListener> mListeners = Sets.newHashSet();
Sailesh Nepal6aca10a2014-03-24 16:11:02 -070091
Santos Cordon766d04f2014-05-06 10:28:25 -070092 /** Singleton accessor. */
93 static CallsManager getInstance() {
94 return INSTANCE;
95 }
Ben Gilad9f2bed32013-12-12 17:43:26 -080096
Santos Cordon8e8b8d22013-12-19 14:14:05 -080097 /**
Ben Gilad8bdaa462014-02-05 12:53:19 -080098 * Initializes the required Telecomm components.
Santos Cordon8e8b8d22013-12-19 14:14:05 -080099 */
100 private CallsManager() {
Santos Cordona0e5f1a2014-03-31 21:43:00 -0700101 TelecommApp app = TelecommApp.getInstance();
Santos Cordona56f2762014-03-24 15:55:53 -0700102
Sailesh Nepal810735e2014-03-18 18:15:46 -0700103 mCallAudioManager = new CallAudioManager();
Santos Cordonc7b8eba2014-04-01 15:26:28 -0700104 InCallTonePlayer.Factory playerFactory = new InCallTonePlayer.Factory(mCallAudioManager);
Santos Cordonb64c1502014-05-21 21:21:49 -0700105 mRinger = new Ringer(mCallAudioManager, this, playerFactory, app);
106
Santos Cordona0e5f1a2014-03-31 21:43:00 -0700107 mListeners.add(new CallLogManager(app));
Santos Cordona56f2762014-03-24 15:55:53 -0700108 mListeners.add(new PhoneStateBroadcaster());
109 mListeners.add(new InCallController());
Santos Cordonb64c1502014-05-21 21:21:49 -0700110 mListeners.add(mRinger);
Santos Cordonc7b8eba2014-04-01 15:26:28 -0700111 mListeners.add(new RingbackPlayer(this, playerFactory));
112 mListeners.add(new InCallToneMonitor(playerFactory, this));
Santos Cordona56f2762014-03-24 15:55:53 -0700113 mListeners.add(mCallAudioManager);
Santos Cordona0e5f1a2014-03-31 21:43:00 -0700114 mListeners.add(app.getMissedCallNotifier());
Santos Cordon92a2d812014-04-30 15:19:01 -0700115 mListeners.add(mDtmfLocalTonePlayer);
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800116 }
117
Santos Cordon766d04f2014-05-06 10:28:25 -0700118 @Override
119 public void onSuccessfulOutgoingCall(Call call) {
120 Log.v(this, "onSuccessfulOutgoingCall, %s", call);
121 if (mCalls.contains(call)) {
122 // The call's CallService has been updated.
123 for (CallsManagerListener listener : mListeners) {
124 listener.onCallServiceChanged(call, null, call.getCallService());
125 }
126 } else if (mPendingHandoffCalls.contains(call)) {
127 updateHandoffCallServiceDescriptor(call.getOriginalCall(),
128 call.getCallService().getDescriptor());
Santos Cordon355083c2014-05-23 14:09:32 -0700129 } else {
130 Log.wtf(this, "unexpected successful call notification: %s", call);
131 return;
Santos Cordon766d04f2014-05-06 10:28:25 -0700132 }
Santos Cordon355083c2014-05-23 14:09:32 -0700133
134 markCallAsDialing(call);
Santos Cordon766d04f2014-05-06 10:28:25 -0700135 }
136
137 @Override
138 public void onFailedOutgoingCall(Call call, boolean isAborted) {
139 Log.v(this, "onFailedOutgoingCall, call: %s, isAborted: %b", call, isAborted);
140 if (isAborted) {
141 setCallState(call, CallState.ABORTED);
142 removeCall(call);
143 } else {
144 // TODO: Replace disconnect cause with more specific disconnect causes.
145 markCallAsDisconnected(call, DisconnectCause.ERROR_UNSPECIFIED, null);
146 }
147 }
148
149 @Override
150 public void onSuccessfulIncomingCall(Call call, CallInfo callInfo) {
151 Log.d(this, "onSuccessfulIncomingCall");
152 setCallState(call, callInfo.getState());
153 addCall(call);
154 }
155
156 @Override
157 public void onFailedIncomingCall(Call call) {
158 call.removeListener(this);
Ben Gilada0d9f752014-02-26 11:49:03 -0800159 }
160
Sailesh Nepal810735e2014-03-18 18:15:46 -0700161 ImmutableCollection<Call> getCalls() {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700162 return ImmutableList.copyOf(mCalls);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700163 }
164
165 Call getForegroundCall() {
166 return mForegroundCall;
167 }
168
Santos Cordonb64c1502014-05-21 21:21:49 -0700169 Ringer getRinger() {
170 return mRinger;
171 }
172
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700173 boolean hasEmergencyCall() {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700174 for (Call call : mCalls) {
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700175 if (call.isEmergencyCall()) {
176 return true;
177 }
178 }
179 return false;
180 }
181
182 CallAudioState getAudioState() {
183 return mCallAudioManager.getAudioState();
184 }
185
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800186 /**
Santos Cordon493e8f22014-02-19 03:15:12 -0800187 * Starts the incoming call sequence by having switchboard gather more information about the
188 * specified call; using the specified call service descriptor. Upon success, execution returns
Santos Cordon766d04f2014-05-06 10:28:25 -0700189 * to {@link #onSuccessfulIncomingCall} to start the in-call UI.
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800190 *
Ben Giladc5b22692014-02-18 20:03:22 -0800191 * @param descriptor The descriptor of the call service to use for this incoming call.
Evan Charltona05805b2014-03-05 08:21:46 -0800192 * @param extras The optional extras Bundle passed with the intent used for the incoming call.
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800193 */
Evan Charltona05805b2014-03-05 08:21:46 -0800194 void processIncomingCallIntent(CallServiceDescriptor descriptor, Bundle extras) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800195 Log.d(this, "processIncomingCallIntent");
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800196 // Create a call with no handle. Eventually, switchboard will update the call with
Sailesh Nepale59bb192014-04-01 18:33:59 -0700197 // additional information from the call service, but for now we just need one to pass
198 // around.
Sailesh Nepal810735e2014-03-18 18:15:46 -0700199 Call call = new Call(true /* isIncoming */);
Santos Cordon766d04f2014-05-06 10:28:25 -0700200 // TODO(santoscordon): Move this to be a part of addCall()
201 call.addListener(this);
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800202
Santos Cordon766d04f2014-05-06 10:28:25 -0700203 call.startIncoming(descriptor, extras);
Ben Gilada0d9f752014-02-26 11:49:03 -0800204 }
205
206 /**
Yorke Lee33501632014-03-17 19:24:12 -0700207 * Attempts to issue/connect the specified call.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800208 *
Yorke Lee33501632014-03-17 19:24:12 -0700209 * @param handle Handle to connect the call with.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800210 * @param contactInfo Information about the entity being called.
Yorke Lee33501632014-03-17 19:24:12 -0700211 * @param gatewayInfo Optional gateway information that can be used to route the call to the
Santos Cordon5b7b9b32014-03-26 14:00:22 -0700212 * actual dialed handle via a gateway provider. May be null.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800213 */
Yorke Lee33501632014-03-17 19:24:12 -0700214 void placeOutgoingCall(Uri handle, ContactInfo contactInfo, GatewayInfo gatewayInfo) {
Yorke Lee33501632014-03-17 19:24:12 -0700215 final Uri uriHandle = (gatewayInfo == null) ? handle : gatewayInfo.getGatewayHandle();
216
217 if (gatewayInfo == null) {
Santos Cordonb58f4532014-05-29 11:59:06 -0700218 Log.i(this, "Creating a new outgoing call with handle: %s", Log.piiHandle(uriHandle));
Yorke Lee33501632014-03-17 19:24:12 -0700219 } else {
220 Log.i(this, "Creating a new outgoing call with gateway handle: %s, original handle: %s",
221 Log.pii(uriHandle), Log.pii(handle));
222 }
Santos Cordon766d04f2014-05-06 10:28:25 -0700223
Santos Cordonfd71f4a2014-05-28 13:59:49 -0700224 Call call = new Call(uriHandle, gatewayInfo, false /* isIncoming */);
Santos Cordon766d04f2014-05-06 10:28:25 -0700225
226 // TODO(santoscordon): Move this to be a part of addCall()
227 call.addListener(this);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700228 addCall(call);
Santos Cordone3d76ab2014-01-28 17:25:20 -0800229
Santos Cordon766d04f2014-05-06 10:28:25 -0700230 call.startOutgoing();
Yorke Leef98fb572014-03-05 10:56:55 -0800231 }
232
233 /**
Santos Cordone3d76ab2014-01-28 17:25:20 -0800234 * Instructs Telecomm to answer the specified call. Intended to be invoked by the in-call
235 * app through {@link InCallAdapter} after Telecomm notifies it of an incoming call followed by
236 * the user opting to answer said call.
Santos Cordone3d76ab2014-01-28 17:25:20 -0800237 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700238 void answerCall(Call call) {
239 if (!mCalls.contains(call)) {
240 Log.i(this, "Request to answer a non-existent call %s", call);
Santos Cordon61d0f702014-02-19 02:52:23 -0800241 } else {
Santos Cordon40f78c22014-04-07 02:11:42 -0700242 // If the foreground call is not the ringing call and it is currently isActive() or
243 // DIALING, put it on hold before answering the call.
244 if (mForegroundCall != null && mForegroundCall != call &&
245 (mForegroundCall.isActive() ||
246 mForegroundCall.getState() == CallState.DIALING)) {
247 Log.v(this, "Holding active/dialing call %s before answering incoming call %s.",
248 mForegroundCall, call);
249 mForegroundCall.hold();
250 // TODO(santoscordon): Wait until we get confirmation of the active call being
251 // on-hold before answering the new call.
252 // TODO(santoscordon): Import logic from CallManager.acceptCall()
253 }
254
Santos Cordona56f2762014-03-24 15:55:53 -0700255 for (CallsManagerListener listener : mListeners) {
256 listener.onIncomingCallAnswered(call);
257 }
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800258
Santos Cordon61d0f702014-02-19 02:52:23 -0800259 // We do not update the UI until we get confirmation of the answer() through
Sailesh Nepale59bb192014-04-01 18:33:59 -0700260 // {@link #markCallAsActive}.
Santos Cordon61d0f702014-02-19 02:52:23 -0800261 call.answer();
262 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800263 }
264
265 /**
266 * Instructs Telecomm to reject the specified call. Intended to be invoked by the in-call
267 * app through {@link InCallAdapter} after Telecomm notifies it of an incoming call followed by
268 * the user opting to reject said call.
Santos Cordone3d76ab2014-01-28 17:25:20 -0800269 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700270 void rejectCall(Call call) {
271 if (!mCalls.contains(call)) {
272 Log.i(this, "Request to reject a non-existent call %s", call);
Santos Cordon61d0f702014-02-19 02:52:23 -0800273 } else {
Santos Cordona56f2762014-03-24 15:55:53 -0700274 for (CallsManagerListener listener : mListeners) {
275 listener.onIncomingCallRejected(call);
276 }
Santos Cordon61d0f702014-02-19 02:52:23 -0800277 call.reject();
278 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800279 }
280
281 /**
Ihab Awad74549ec2014-03-10 15:33:25 -0700282 * Instructs Telecomm to play the specified DTMF tone within the specified call.
283 *
Ihab Awad74549ec2014-03-10 15:33:25 -0700284 * @param digit The DTMF digit to play.
285 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700286 void playDtmfTone(Call call, char digit) {
287 if (!mCalls.contains(call)) {
288 Log.i(this, "Request to play DTMF in a non-existent call %s", call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700289 } else {
290 call.playDtmfTone(digit);
Santos Cordon92a2d812014-04-30 15:19:01 -0700291 mDtmfLocalTonePlayer.playTone(call, digit);
Ihab Awad74549ec2014-03-10 15:33:25 -0700292 }
293 }
294
295 /**
296 * Instructs Telecomm to stop the currently playing DTMF tone, if any.
Ihab Awad74549ec2014-03-10 15:33:25 -0700297 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700298 void stopDtmfTone(Call call) {
299 if (!mCalls.contains(call)) {
300 Log.i(this, "Request to stop DTMF in a non-existent call %s", call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700301 } else {
302 call.stopDtmfTone();
Santos Cordon92a2d812014-04-30 15:19:01 -0700303 mDtmfLocalTonePlayer.stopTone(call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700304 }
305 }
306
307 /**
308 * Instructs Telecomm to continue the current post-dial DTMF string, if any.
Ihab Awad74549ec2014-03-10 15:33:25 -0700309 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700310 void postDialContinue(Call call) {
311 if (!mCalls.contains(call)) {
312 Log.i(this, "Request to continue post-dial string in a non-existent call %s", call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700313 } else {
314 // TODO(ihab): Implement this from this level on downwards
315 // call.postDialContinue();
316 // Must play tones locally -- see DTMFTonePlayer.java in Telephony
317 }
318 }
319
320 /**
Santos Cordone3d76ab2014-01-28 17:25:20 -0800321 * Instructs Telecomm to disconnect the specified call. Intended to be invoked by the
322 * in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered by
323 * the user hitting the end-call button.
Santos Cordone3d76ab2014-01-28 17:25:20 -0800324 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700325 void disconnectCall(Call call) {
Santos Cordon682fe6b2014-05-20 08:56:39 -0700326 Log.v(this, "disconnectCall %s", call);
327
Sailesh Nepale59bb192014-04-01 18:33:59 -0700328 if (!mCalls.contains(call)) {
329 Log.w(this, "Unknown call (%s) asked to disconnect", call);
Santos Cordon049b7b62014-01-30 05:34:26 -0800330 } else {
331 call.disconnect();
332 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800333 }
Santos Cordon681663d2014-01-30 04:32:15 -0800334
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700335 /**
336 * Instructs Telecomm to put the specified call on hold. Intended to be invoked by the
337 * in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered by
338 * the user hitting the hold button during an active call.
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700339 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700340 void holdCall(Call call) {
341 if (!mCalls.contains(call)) {
342 Log.w(this, "Unknown call (%s) asked to be put on hold", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700343 } else {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700344 Log.d(this, "Putting call on hold: (%s)", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700345 call.hold();
346 }
347 }
348
349 /**
350 * Instructs Telecomm to release the specified call from hold. Intended to be invoked by
351 * the in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered
352 * by the user hitting the hold button during a held call.
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700353 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700354 void unholdCall(Call call) {
355 if (!mCalls.contains(call)) {
356 Log.w(this, "Unknown call (%s) asked to be removed from hold", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700357 } else {
Santos Cordonc7e85d42014-05-22 02:51:48 -0700358 Log.d(this, "unholding call: (%s)", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700359 call.unhold();
360 }
361 }
362
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700363 /** Called by the in-call UI to change the mute state. */
364 void mute(boolean shouldMute) {
365 mCallAudioManager.mute(shouldMute);
366 }
367
368 /**
369 * Called by the in-call UI to change the audio route, for example to change from earpiece to
370 * speaker phone.
371 */
372 void setAudioRoute(int route) {
373 mCallAudioManager.setAudioRoute(route);
374 }
375
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700376 void startHandoffForCall(Call originalCall) {
377 if (!mCalls.contains(originalCall)) {
378 Log.w(this, "Unknown call %s asked to be handed off", originalCall);
379 return;
380 }
381
382 for (Call handoffCall : mPendingHandoffCalls) {
383 if (handoffCall.getOriginalCall() == originalCall) {
384 Log.w(this, "Call %s is already being handed off, skipping", originalCall);
385 return;
386 }
387 }
388
389 // Create a new call to be placed in the background. If handoff is successful then the
390 // original call will live on but its state will be updated to the new call's state. In
391 // particular the original call's call service will be updated to the new call's call
392 // service.
Santos Cordonfd71f4a2014-05-28 13:59:49 -0700393 Call tempCall =
394 new Call(originalCall.getHandoffHandle(), originalCall.getGatewayInfo(), false);
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700395 tempCall.setOriginalCall(originalCall);
396 tempCall.setExtras(originalCall.getExtras());
397 tempCall.setCallServiceSelector(originalCall.getCallServiceSelector());
398 mPendingHandoffCalls.add(tempCall);
Santos Cordon355083c2014-05-23 14:09:32 -0700399 tempCall.addListener(this);
400
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700401 Log.d(this, "Placing handoff call");
Santos Cordon766d04f2014-05-06 10:28:25 -0700402 tempCall.startOutgoing();
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700403 }
404
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700405 /** Called when the audio state changes. */
406 void onAudioStateChanged(CallAudioState oldAudioState, CallAudioState newAudioState) {
407 Log.v(this, "onAudioStateChanged, audioState: %s -> %s", oldAudioState, newAudioState);
Santos Cordona56f2762014-03-24 15:55:53 -0700408 for (CallsManagerListener listener : mListeners) {
409 listener.onAudioStateChanged(oldAudioState, newAudioState);
410 }
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700411 }
412
Sailesh Nepale59bb192014-04-01 18:33:59 -0700413 void markCallAsRinging(Call call) {
414 setCallState(call, CallState.RINGING);
Santos Cordon681663d2014-01-30 04:32:15 -0800415 }
416
Sailesh Nepale59bb192014-04-01 18:33:59 -0700417 void markCallAsDialing(Call call) {
418 setCallState(call, CallState.DIALING);
Santos Cordon681663d2014-01-30 04:32:15 -0800419 }
420
Sailesh Nepale59bb192014-04-01 18:33:59 -0700421 void markCallAsActive(Call call) {
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700422 if (call.getConnectTimeMillis() == 0) {
423 call.setConnectTimeMillis(System.currentTimeMillis());
424 }
Sailesh Nepale59bb192014-04-01 18:33:59 -0700425 setCallState(call, CallState.ACTIVE);
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700426
427 if (mPendingHandoffCalls.contains(call)) {
Sailesh Nepal4857f472014-04-07 22:26:27 -0700428 completeHandoff(call, true);
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700429 }
Santos Cordon681663d2014-01-30 04:32:15 -0800430 }
431
Sailesh Nepale59bb192014-04-01 18:33:59 -0700432 void markCallAsOnHold(Call call) {
433 setCallState(call, CallState.ON_HOLD);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700434 }
435
Santos Cordon049b7b62014-01-30 05:34:26 -0800436 /**
437 * Marks the specified call as DISCONNECTED and notifies the in-call app. If this was the last
438 * live call, then also disconnect from the in-call controller.
439 *
Santos Cordon79ff2bc2014-03-27 15:31:27 -0700440 * @param disconnectCause The disconnect reason, see {@link android.telephony.DisconnectCause}.
441 * @param disconnectMessage Optional call-service-provided message about the disconnect.
Santos Cordon049b7b62014-01-30 05:34:26 -0800442 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700443 void markCallAsDisconnected(Call call, int disconnectCause, String disconnectMessage) {
444 call.setDisconnectCause(disconnectCause, disconnectMessage);
445 setCallState(call, CallState.DISCONNECTED);
Sailesh Nepal4857f472014-04-07 22:26:27 -0700446
447 // Only remove the call if handoff is not pending.
448 if (call.getHandoffCallServiceDescriptor() == null) {
449 removeCall(call);
450 }
Ben Gilada0d9f752014-02-26 11:49:03 -0800451 }
452
Sailesh Nepal6ab6fb72014-04-01 20:03:19 -0700453 void setHandoffInfo(Call call, Uri handle, Bundle extras) {
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700454 if (!mCalls.contains(call)) {
455 Log.w(this, "Unknown call (%s) asked to set handoff info", call);
456 return;
457 }
458
459 if (extras == null) {
460 call.setExtras(Bundle.EMPTY);
461 } else {
462 call.setExtras(extras);
463 }
464
465 Uri oldHandle = call.getHandoffHandle();
466 Log.v(this, "set handoff handle %s -> %s, for call: %s", oldHandle, handle, call);
467 if (!areUriEqual(oldHandle, handle)) {
468 call.setHandoffHandle(handle);
469 for (CallsManagerListener listener : mListeners) {
470 listener.onCallHandoffHandleChanged(call, oldHandle, handle);
471 }
472 }
Sailesh Nepal6ab6fb72014-04-01 20:03:19 -0700473 }
474
Ben Gilada0d9f752014-02-26 11:49:03 -0800475 /**
Santos Cordon4b2c1192014-03-19 18:15:38 -0700476 * Cleans up any calls currently associated with the specified call service when the
477 * call-service binder disconnects unexpectedly.
478 *
479 * @param callService The call service that disconnected.
480 */
481 void handleCallServiceDeath(CallServiceWrapper callService) {
482 Preconditions.checkNotNull(callService);
Sailesh Nepale59bb192014-04-01 18:33:59 -0700483 for (Call call : ImmutableList.copyOf(mCalls)) {
Santos Cordon4b2c1192014-03-19 18:15:38 -0700484 if (call.getCallService() == callService) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700485 markCallAsDisconnected(call, DisconnectCause.ERROR_UNSPECIFIED, null);
Santos Cordon4b2c1192014-03-19 18:15:38 -0700486 }
487 }
488 }
489
Santos Cordonc7e85d42014-05-22 02:51:48 -0700490 boolean hasActiveOrHoldingCall() {
491 for (Call call : mCalls) {
492 CallState state = call.getState();
493 if (state == CallState.ACTIVE || state == CallState.ON_HOLD) {
494 return true;
495 }
496 }
497 return false;
498 }
499
500 boolean hasRingingCall() {
501 for (Call call : mCalls) {
502 if (call.getState() == CallState.RINGING) {
503 return true;
504 }
505 }
506 return false;
507 }
508
Santos Cordon4b2c1192014-03-19 18:15:38 -0700509 /**
Ben Gilada0d9f752014-02-26 11:49:03 -0800510 * Adds the specified call to the main list of live calls.
511 *
512 * @param call The call to add.
513 */
514 private void addCall(Call call) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700515 mCalls.add(call);
Santos Cordon40f78c22014-04-07 02:11:42 -0700516
517 // TODO(santoscordon): Update mForegroundCall prior to invoking
518 // onCallAdded for calls which immediately take the foreground (like the first call).
Santos Cordona56f2762014-03-24 15:55:53 -0700519 for (CallsManagerListener listener : mListeners) {
520 listener.onCallAdded(call);
521 }
Sailesh Nepal810735e2014-03-18 18:15:46 -0700522 updateForegroundCall();
Ben Gilada0d9f752014-02-26 11:49:03 -0800523 }
524
Sailesh Nepal810735e2014-03-18 18:15:46 -0700525 private void removeCall(Call call) {
Sailesh Nepal4857f472014-04-07 22:26:27 -0700526 // If a handoff is pending then the original call shouldn't be removed.
527 Preconditions.checkState(call.getHandoffCallServiceDescriptor() == null);
Santos Cordonc499c1c2014-04-14 17:13:14 -0700528 Log.v(this, "removeCall(%s)", call);
Sailesh Nepal4857f472014-04-07 22:26:27 -0700529
Santos Cordon766d04f2014-05-06 10:28:25 -0700530 call.removeListener(this);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700531 call.clearCallService();
Sailesh Nepale59bb192014-04-01 18:33:59 -0700532 call.clearCallServiceSelector();
533
534 boolean shouldNotify = false;
535 if (mCalls.contains(call)) {
536 mCalls.remove(call);
537 shouldNotify = true;
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700538 } else if (mPendingHandoffCalls.contains(call)) {
Sailesh Nepal4857f472014-04-07 22:26:27 -0700539 Log.v(this, "removeCall, marking handoff call as failed");
540 completeHandoff(call, false);
Santos Cordona56f2762014-03-24 15:55:53 -0700541 }
Ben Gilada0d9f752014-02-26 11:49:03 -0800542
Sailesh Nepale59bb192014-04-01 18:33:59 -0700543 // Only broadcast changes for calls that are being tracked.
544 if (shouldNotify) {
545 for (CallsManagerListener listener : mListeners) {
546 listener.onCallRemoved(call);
547 }
548 updateForegroundCall();
Sailesh Nepal810735e2014-03-18 18:15:46 -0700549 }
550 }
Evan Charlton5c670a92014-03-06 14:58:20 -0800551
Sailesh Nepal810735e2014-03-18 18:15:46 -0700552 /**
Santos Cordon1ae2b852014-03-19 03:03:10 -0700553 * Sets the specified state on the specified call.
Sailesh Nepal810735e2014-03-18 18:15:46 -0700554 *
555 * @param call The call.
556 * @param newState The new state of the call.
557 */
558 private void setCallState(Call call, CallState newState) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700559 Preconditions.checkNotNull(newState);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700560 CallState oldState = call.getState();
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700561 Log.i(this, "setCallState %s -> %s, call: %s", oldState, newState, call);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700562 if (newState != oldState) {
563 // Unfortunately, in the telephony world the radio is king. So if the call notifies
564 // us that the call is in a particular state, we allow it even if it doesn't make
565 // sense (e.g., ACTIVE -> RINGING).
566 // TODO(santoscordon): Consider putting a stop to the above and turning CallState
567 // into a well-defined state machine.
568 // TODO(santoscordon): Define expected state transitions here, and log when an
569 // unexpected transition occurs.
570 call.setState(newState);
571
572 // Only broadcast state change for calls that are being tracked.
Sailesh Nepale59bb192014-04-01 18:33:59 -0700573 if (mCalls.contains(call)) {
Santos Cordona56f2762014-03-24 15:55:53 -0700574 for (CallsManagerListener listener : mListeners) {
575 listener.onCallStateChanged(call, oldState, newState);
576 }
Sailesh Nepal810735e2014-03-18 18:15:46 -0700577 updateForegroundCall();
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800578 }
579 }
580 }
581
582 /**
Sailesh Nepal810735e2014-03-18 18:15:46 -0700583 * Checks which call should be visible to the user and have audio focus.
Evan Charlton5c670a92014-03-06 14:58:20 -0800584 */
Sailesh Nepal810735e2014-03-18 18:15:46 -0700585 private void updateForegroundCall() {
586 Call newForegroundCall = null;
Sailesh Nepale59bb192014-04-01 18:33:59 -0700587 for (Call call : mCalls) {
Santos Cordon40f78c22014-04-07 02:11:42 -0700588 // TODO(santoscordon): Foreground-ness needs to be explicitly set. No call, regardless
589 // of its state will be foreground by default and instead the call service should be
590 // notified when its calls enter and exit foreground state. Foreground will mean that
591 // the call should play audio and listen to microphone if it wants.
592
593 // Active calls have priority.
594 if (call.isActive()) {
Sailesh Nepal810735e2014-03-18 18:15:46 -0700595 newForegroundCall = call;
Evan Charlton5c670a92014-03-06 14:58:20 -0800596 break;
Sailesh Nepal810735e2014-03-18 18:15:46 -0700597 }
Santos Cordon40f78c22014-04-07 02:11:42 -0700598
599 if (call.isAlive() || call.getState() == CallState.RINGING) {
Sailesh Nepal810735e2014-03-18 18:15:46 -0700600 newForegroundCall = call;
Santos Cordon40f78c22014-04-07 02:11:42 -0700601 // Don't break in case there's an active call that has priority.
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800602 }
603 }
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800604
Sailesh Nepal810735e2014-03-18 18:15:46 -0700605 if (newForegroundCall != mForegroundCall) {
Santos Cordon40f78c22014-04-07 02:11:42 -0700606 Log.v(this, "Updating foreground call, %s -> %s.", mForegroundCall, newForegroundCall);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700607 Call oldForegroundCall = mForegroundCall;
608 mForegroundCall = newForegroundCall;
609
Santos Cordona56f2762014-03-24 15:55:53 -0700610 for (CallsManagerListener listener : mListeners) {
611 listener.onForegroundCallChanged(oldForegroundCall, mForegroundCall);
612 }
Santos Cordon681663d2014-01-30 04:32:15 -0800613 }
614 }
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700615
Sailesh Nepal4857f472014-04-07 22:26:27 -0700616 private void completeHandoff(Call handoffCall, boolean wasSuccessful) {
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700617 Call originalCall = handoffCall.getOriginalCall();
Sailesh Nepal4857f472014-04-07 22:26:27 -0700618 Log.v(this, "complete handoff, %s -> %s, wasSuccessful: %b", handoffCall, originalCall,
619 wasSuccessful);
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700620
Sailesh Nepal4857f472014-04-07 22:26:27 -0700621 // Remove the transient handoff call object (don't disconnect because the call could still
622 // be live).
623 mPendingHandoffCalls.remove(handoffCall);
Santos Cordon355083c2014-05-23 14:09:32 -0700624 handoffCall.removeListener(this);
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700625
Sailesh Nepal4857f472014-04-07 22:26:27 -0700626 if (wasSuccessful) {
627 // Disconnect.
628 originalCall.disconnect();
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700629
Sailesh Nepal4857f472014-04-07 22:26:27 -0700630 // Synchronize.
631 originalCall.setCallService(handoffCall.getCallService(), handoffCall);
632 setCallState(originalCall, handoffCall.getState());
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700633
Sailesh Nepal4857f472014-04-07 22:26:27 -0700634 // Force the foreground call changed notification to be sent.
635 for (CallsManagerListener listener : mListeners) {
636 listener.onForegroundCallChanged(mForegroundCall, mForegroundCall);
637 }
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700638
Sailesh Nepal4857f472014-04-07 22:26:27 -0700639 updateHandoffCallServiceDescriptor(originalCall, null);
640 } else {
641 updateHandoffCallServiceDescriptor(originalCall, null);
642 if (originalCall.getState() == CallState.DISCONNECTED ||
643 originalCall.getState() == CallState.ABORTED) {
644 removeCall(originalCall);
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700645 }
646 }
647 }
648
Sailesh Nepal4857f472014-04-07 22:26:27 -0700649 private void updateHandoffCallServiceDescriptor(
650 Call originalCall,
651 CallServiceDescriptor newDescriptor) {
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700652 CallServiceDescriptor oldDescriptor = originalCall.getHandoffCallServiceDescriptor();
Sailesh Nepal4857f472014-04-07 22:26:27 -0700653 Log.v(this, "updateHandoffCallServiceDescriptor, call: %s, pending descriptor: %s -> %s",
654 originalCall, oldDescriptor, newDescriptor);
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700655
656 if (!areDescriptorsEqual(oldDescriptor, newDescriptor)) {
657 originalCall.setHandoffCallServiceDescriptor(newDescriptor);
658 for (CallsManagerListener listener : mListeners) {
659 listener.onCallHandoffCallServiceDescriptorChanged(originalCall, oldDescriptor,
660 newDescriptor);
661 }
662 }
663 }
664
665 private static boolean areDescriptorsEqual(
666 CallServiceDescriptor descriptor1,
667 CallServiceDescriptor descriptor2) {
668 if (descriptor1 == null) {
669 return descriptor2 == null;
670 }
671 return descriptor1.equals(descriptor2);
672 }
673
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700674 private static boolean areUriEqual(Uri handle1, Uri handle2) {
675 if (handle1 == null) {
676 return handle2 == null;
677 }
678 return handle1.equals(handle2);
679 }
Ben Gilad9f2bed32013-12-12 17:43:26 -0800680}