blob: e1d30449a1a955fbc26a82b86dd3595bb7438210 [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;
Ben Gilad9f2bed32013-12-12 17:43:26 -080031
Santos Cordonf3671a62014-05-29 21:51:53 -070032import java.util.HashSet;
33import java.util.LinkedHashSet;
Santos Cordona56f2762014-03-24 15:55:53 -070034import java.util.Set;
Ben Gilad9f2bed32013-12-12 17:43:26 -080035
Ben Giladdd8c6082013-12-30 14:44:08 -080036/**
37 * Singleton.
38 *
39 * NOTE(gilad): by design most APIs are package private, use the relevant adapter/s to allow
40 * access from other packages specifically refraining from passing the CallsManager instance
41 * beyond the com.android.telecomm package boundary.
42 */
Santos Cordon766d04f2014-05-06 10:28:25 -070043public final class CallsManager implements Call.Listener {
Ben Gilada0d9f752014-02-26 11:49:03 -080044
Santos Cordon74d420b2014-05-07 14:38:47 -070045 // TODO(santoscordon): Consider renaming this CallsManagerPlugin.
Sailesh Nepal810735e2014-03-18 18:15:46 -070046 interface CallsManagerListener {
47 void onCallAdded(Call call);
48 void onCallRemoved(Call call);
49 void onCallStateChanged(Call call, CallState oldState, CallState newState);
Sailesh Nepal8c85dee2014-04-07 22:21:40 -070050 void onCallHandoffHandleChanged(Call call, Uri oldHandle, Uri newHandle);
51 void onCallServiceChanged(
52 Call call,
53 CallServiceWrapper oldCallService,
54 CallServiceWrapper newCallService);
55 void onCallHandoffCallServiceDescriptorChanged(
56 Call call,
57 CallServiceDescriptor oldDescriptor,
58 CallServiceDescriptor newDescriptor);
Sailesh Nepal810735e2014-03-18 18:15:46 -070059 void onIncomingCallAnswered(Call call);
60 void onIncomingCallRejected(Call call);
61 void onForegroundCallChanged(Call oldForegroundCall, Call newForegroundCall);
Sailesh Nepal6aca10a2014-03-24 16:11:02 -070062 void onAudioStateChanged(CallAudioState oldAudioState, CallAudioState newAudioState);
Sailesh Nepal810735e2014-03-18 18:15:46 -070063 }
64
Santos Cordon8e8b8d22013-12-19 14:14:05 -080065 private static final CallsManager INSTANCE = new CallsManager();
Ben Gilad9f2bed32013-12-12 17:43:26 -080066
Santos Cordon8e8b8d22013-12-19 14:14:05 -080067 /**
Sailesh Nepale59bb192014-04-01 18:33:59 -070068 * The main call repository. Keeps an instance of all live calls. New incoming and outgoing
69 * calls are added to the map and removed when the calls move to the disconnected state.
Santos Cordon681663d2014-01-30 04:32:15 -080070 */
Santos Cordonf3671a62014-05-29 21:51:53 -070071 private final Set<Call> mCalls = new LinkedHashSet<>();
Santos Cordon681663d2014-01-30 04:32:15 -080072
73 /**
Sailesh Nepal84fa5f82014-04-02 11:01:11 -070074 * Set of new calls created to perform a handoff. The calls are added when handoff is initiated
75 * and removed when hadnoff is complete.
76 */
Santos Cordonf3671a62014-05-29 21:51:53 -070077 private final Set<Call> mPendingHandoffCalls = new LinkedHashSet<>();
78
79
80 private final DtmfLocalTonePlayer mDtmfLocalTonePlayer = new DtmfLocalTonePlayer();
81 private final InCallController mInCallController = new InCallController();
82 private final CallAudioManager mCallAudioManager;
83 private final Ringer mRinger;
84 private final Set<CallsManagerListener> mListeners = new HashSet<>();
Sailesh Nepal84fa5f82014-04-02 11:01:11 -070085
86 /**
Sailesh Nepal810735e2014-03-18 18:15:46 -070087 * The call the user is currently interacting with. This is the call that should have audio
88 * focus and be visible in the in-call UI.
Santos Cordon4e9fffe2014-03-04 18:13:41 -080089 */
Sailesh Nepal810735e2014-03-18 18:15:46 -070090 private Call mForegroundCall;
Santos Cordon4e9fffe2014-03-04 18:13:41 -080091
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 Cordonae193062014-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());
Santos Cordonf3671a62014-05-29 21:51:53 -0700109 mListeners.add(mInCallController);
Santos Cordonae193062014-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 Cordon6cb7ba92014-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 Cordon6cb7ba92014-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 Cordonae193062014-05-21 21:21:49 -0700169 Ringer getRinger() {
170 return mRinger;
171 }
172
Santos Cordonf3671a62014-05-29 21:51:53 -0700173 InCallController getInCallController() {
174 return mInCallController;
175 }
176
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700177 boolean hasEmergencyCall() {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700178 for (Call call : mCalls) {
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700179 if (call.isEmergencyCall()) {
180 return true;
181 }
182 }
183 return false;
184 }
185
186 CallAudioState getAudioState() {
187 return mCallAudioManager.getAudioState();
188 }
189
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800190 /**
Santos Cordon493e8f22014-02-19 03:15:12 -0800191 * Starts the incoming call sequence by having switchboard gather more information about the
192 * specified call; using the specified call service descriptor. Upon success, execution returns
Santos Cordon766d04f2014-05-06 10:28:25 -0700193 * to {@link #onSuccessfulIncomingCall} to start the in-call UI.
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800194 *
Ben Giladc5b22692014-02-18 20:03:22 -0800195 * @param descriptor The descriptor of the call service to use for this incoming call.
Evan Charltona05805b2014-03-05 08:21:46 -0800196 * @param extras The optional extras Bundle passed with the intent used for the incoming call.
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800197 */
Evan Charltona05805b2014-03-05 08:21:46 -0800198 void processIncomingCallIntent(CallServiceDescriptor descriptor, Bundle extras) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800199 Log.d(this, "processIncomingCallIntent");
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800200 // Create a call with no handle. Eventually, switchboard will update the call with
Sailesh Nepale59bb192014-04-01 18:33:59 -0700201 // additional information from the call service, but for now we just need one to pass
202 // around.
Sailesh Nepal810735e2014-03-18 18:15:46 -0700203 Call call = new Call(true /* isIncoming */);
Santos Cordon766d04f2014-05-06 10:28:25 -0700204 // TODO(santoscordon): Move this to be a part of addCall()
205 call.addListener(this);
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800206
Santos Cordon766d04f2014-05-06 10:28:25 -0700207 call.startIncoming(descriptor, extras);
Ben Gilada0d9f752014-02-26 11:49:03 -0800208 }
209
210 /**
Yorke Lee33501632014-03-17 19:24:12 -0700211 * Attempts to issue/connect the specified call.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800212 *
Yorke Lee33501632014-03-17 19:24:12 -0700213 * @param handle Handle to connect the call with.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800214 * @param contactInfo Information about the entity being called.
Yorke Lee33501632014-03-17 19:24:12 -0700215 * @param gatewayInfo Optional gateway information that can be used to route the call to the
Santos Cordon5b7b9b32014-03-26 14:00:22 -0700216 * actual dialed handle via a gateway provider. May be null.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800217 */
Yorke Lee33501632014-03-17 19:24:12 -0700218 void placeOutgoingCall(Uri handle, ContactInfo contactInfo, GatewayInfo gatewayInfo) {
Yorke Lee33501632014-03-17 19:24:12 -0700219 final Uri uriHandle = (gatewayInfo == null) ? handle : gatewayInfo.getGatewayHandle();
220
221 if (gatewayInfo == null) {
Santos Cordonb58f4532014-05-29 11:59:06 -0700222 Log.i(this, "Creating a new outgoing call with handle: %s", Log.piiHandle(uriHandle));
Yorke Lee33501632014-03-17 19:24:12 -0700223 } else {
224 Log.i(this, "Creating a new outgoing call with gateway handle: %s, original handle: %s",
225 Log.pii(uriHandle), Log.pii(handle));
226 }
Santos Cordon766d04f2014-05-06 10:28:25 -0700227
Santos Cordonfd71f4a2014-05-28 13:59:49 -0700228 Call call = new Call(uriHandle, gatewayInfo, false /* isIncoming */);
Santos Cordon766d04f2014-05-06 10:28:25 -0700229
230 // TODO(santoscordon): Move this to be a part of addCall()
231 call.addListener(this);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700232 addCall(call);
Santos Cordone3d76ab2014-01-28 17:25:20 -0800233
Santos Cordon766d04f2014-05-06 10:28:25 -0700234 call.startOutgoing();
Yorke Leef98fb572014-03-05 10:56:55 -0800235 }
236
237 /**
Santos Cordone3d76ab2014-01-28 17:25:20 -0800238 * Instructs Telecomm to answer the specified call. Intended to be invoked by the in-call
239 * app through {@link InCallAdapter} after Telecomm notifies it of an incoming call followed by
240 * the user opting to answer said call.
Santos Cordone3d76ab2014-01-28 17:25:20 -0800241 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700242 void answerCall(Call call) {
243 if (!mCalls.contains(call)) {
244 Log.i(this, "Request to answer a non-existent call %s", call);
Santos Cordon61d0f702014-02-19 02:52:23 -0800245 } else {
Santos Cordon40f78c22014-04-07 02:11:42 -0700246 // If the foreground call is not the ringing call and it is currently isActive() or
247 // DIALING, put it on hold before answering the call.
248 if (mForegroundCall != null && mForegroundCall != call &&
249 (mForegroundCall.isActive() ||
250 mForegroundCall.getState() == CallState.DIALING)) {
251 Log.v(this, "Holding active/dialing call %s before answering incoming call %s.",
252 mForegroundCall, call);
253 mForegroundCall.hold();
254 // TODO(santoscordon): Wait until we get confirmation of the active call being
255 // on-hold before answering the new call.
256 // TODO(santoscordon): Import logic from CallManager.acceptCall()
257 }
258
Santos Cordona56f2762014-03-24 15:55:53 -0700259 for (CallsManagerListener listener : mListeners) {
260 listener.onIncomingCallAnswered(call);
261 }
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800262
Santos Cordon61d0f702014-02-19 02:52:23 -0800263 // We do not update the UI until we get confirmation of the answer() through
Sailesh Nepale59bb192014-04-01 18:33:59 -0700264 // {@link #markCallAsActive}.
Santos Cordon61d0f702014-02-19 02:52:23 -0800265 call.answer();
266 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800267 }
268
269 /**
270 * Instructs Telecomm to reject the specified call. Intended to be invoked by the in-call
271 * app through {@link InCallAdapter} after Telecomm notifies it of an incoming call followed by
272 * the user opting to reject said call.
Santos Cordone3d76ab2014-01-28 17:25:20 -0800273 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700274 void rejectCall(Call call) {
275 if (!mCalls.contains(call)) {
276 Log.i(this, "Request to reject a non-existent call %s", call);
Santos Cordon61d0f702014-02-19 02:52:23 -0800277 } else {
Santos Cordona56f2762014-03-24 15:55:53 -0700278 for (CallsManagerListener listener : mListeners) {
279 listener.onIncomingCallRejected(call);
280 }
Santos Cordon61d0f702014-02-19 02:52:23 -0800281 call.reject();
282 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800283 }
284
285 /**
Ihab Awad74549ec2014-03-10 15:33:25 -0700286 * Instructs Telecomm to play the specified DTMF tone within the specified call.
287 *
Ihab Awad74549ec2014-03-10 15:33:25 -0700288 * @param digit The DTMF digit to play.
289 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700290 void playDtmfTone(Call call, char digit) {
291 if (!mCalls.contains(call)) {
292 Log.i(this, "Request to play DTMF in a non-existent call %s", call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700293 } else {
294 call.playDtmfTone(digit);
Santos Cordon92a2d812014-04-30 15:19:01 -0700295 mDtmfLocalTonePlayer.playTone(call, digit);
Ihab Awad74549ec2014-03-10 15:33:25 -0700296 }
297 }
298
299 /**
300 * Instructs Telecomm to stop the currently playing DTMF tone, if any.
Ihab Awad74549ec2014-03-10 15:33:25 -0700301 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700302 void stopDtmfTone(Call call) {
303 if (!mCalls.contains(call)) {
304 Log.i(this, "Request to stop DTMF in a non-existent call %s", call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700305 } else {
306 call.stopDtmfTone();
Santos Cordon92a2d812014-04-30 15:19:01 -0700307 mDtmfLocalTonePlayer.stopTone(call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700308 }
309 }
310
311 /**
312 * Instructs Telecomm to continue the current post-dial DTMF string, if any.
Ihab Awad74549ec2014-03-10 15:33:25 -0700313 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700314 void postDialContinue(Call call) {
315 if (!mCalls.contains(call)) {
316 Log.i(this, "Request to continue post-dial string in a non-existent call %s", call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700317 } else {
318 // TODO(ihab): Implement this from this level on downwards
319 // call.postDialContinue();
320 // Must play tones locally -- see DTMFTonePlayer.java in Telephony
321 }
322 }
323
324 /**
Santos Cordone3d76ab2014-01-28 17:25:20 -0800325 * Instructs Telecomm to disconnect the specified call. Intended to be invoked by the
326 * in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered by
327 * the user hitting the end-call button.
Santos Cordone3d76ab2014-01-28 17:25:20 -0800328 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700329 void disconnectCall(Call call) {
Santos Cordon682fe6b2014-05-20 08:56:39 -0700330 Log.v(this, "disconnectCall %s", call);
331
Sailesh Nepale59bb192014-04-01 18:33:59 -0700332 if (!mCalls.contains(call)) {
333 Log.w(this, "Unknown call (%s) asked to disconnect", call);
Santos Cordon049b7b62014-01-30 05:34:26 -0800334 } else {
335 call.disconnect();
336 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800337 }
Santos Cordon681663d2014-01-30 04:32:15 -0800338
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700339 /**
340 * Instructs Telecomm to put the specified call on hold. Intended to be invoked by the
341 * in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered by
342 * the user hitting the hold button during an active call.
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700343 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700344 void holdCall(Call call) {
345 if (!mCalls.contains(call)) {
346 Log.w(this, "Unknown call (%s) asked to be put on hold", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700347 } else {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700348 Log.d(this, "Putting call on hold: (%s)", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700349 call.hold();
350 }
351 }
352
353 /**
354 * Instructs Telecomm to release the specified call from hold. Intended to be invoked by
355 * the in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered
356 * by the user hitting the hold button during a held call.
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700357 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700358 void unholdCall(Call call) {
359 if (!mCalls.contains(call)) {
360 Log.w(this, "Unknown call (%s) asked to be removed from hold", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700361 } else {
Santos Cordonc7e85d42014-05-22 02:51:48 -0700362 Log.d(this, "unholding call: (%s)", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700363 call.unhold();
364 }
365 }
366
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700367 /** Called by the in-call UI to change the mute state. */
368 void mute(boolean shouldMute) {
369 mCallAudioManager.mute(shouldMute);
370 }
371
372 /**
373 * Called by the in-call UI to change the audio route, for example to change from earpiece to
374 * speaker phone.
375 */
376 void setAudioRoute(int route) {
377 mCallAudioManager.setAudioRoute(route);
378 }
379
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700380 void startHandoffForCall(Call originalCall) {
381 if (!mCalls.contains(originalCall)) {
382 Log.w(this, "Unknown call %s asked to be handed off", originalCall);
383 return;
384 }
385
386 for (Call handoffCall : mPendingHandoffCalls) {
387 if (handoffCall.getOriginalCall() == originalCall) {
388 Log.w(this, "Call %s is already being handed off, skipping", originalCall);
389 return;
390 }
391 }
392
393 // Create a new call to be placed in the background. If handoff is successful then the
394 // original call will live on but its state will be updated to the new call's state. In
395 // particular the original call's call service will be updated to the new call's call
396 // service.
Santos Cordonfd71f4a2014-05-28 13:59:49 -0700397 Call tempCall =
398 new Call(originalCall.getHandoffHandle(), originalCall.getGatewayInfo(), false);
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700399 tempCall.setOriginalCall(originalCall);
400 tempCall.setExtras(originalCall.getExtras());
401 tempCall.setCallServiceSelector(originalCall.getCallServiceSelector());
402 mPendingHandoffCalls.add(tempCall);
Santos Cordon6cb7ba92014-05-23 14:09:32 -0700403 tempCall.addListener(this);
404
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700405 Log.d(this, "Placing handoff call");
Santos Cordon766d04f2014-05-06 10:28:25 -0700406 tempCall.startOutgoing();
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700407 }
408
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700409 /** Called when the audio state changes. */
410 void onAudioStateChanged(CallAudioState oldAudioState, CallAudioState newAudioState) {
411 Log.v(this, "onAudioStateChanged, audioState: %s -> %s", oldAudioState, newAudioState);
Santos Cordona56f2762014-03-24 15:55:53 -0700412 for (CallsManagerListener listener : mListeners) {
413 listener.onAudioStateChanged(oldAudioState, newAudioState);
414 }
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700415 }
416
Sailesh Nepale59bb192014-04-01 18:33:59 -0700417 void markCallAsRinging(Call call) {
418 setCallState(call, CallState.RINGING);
Santos Cordon681663d2014-01-30 04:32:15 -0800419 }
420
Sailesh Nepale59bb192014-04-01 18:33:59 -0700421 void markCallAsDialing(Call call) {
422 setCallState(call, CallState.DIALING);
Santos Cordon681663d2014-01-30 04:32:15 -0800423 }
424
Sailesh Nepale59bb192014-04-01 18:33:59 -0700425 void markCallAsActive(Call call) {
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700426 if (call.getConnectTimeMillis() == 0) {
427 call.setConnectTimeMillis(System.currentTimeMillis());
428 }
Sailesh Nepale59bb192014-04-01 18:33:59 -0700429 setCallState(call, CallState.ACTIVE);
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700430
431 if (mPendingHandoffCalls.contains(call)) {
Sailesh Nepal4857f472014-04-07 22:26:27 -0700432 completeHandoff(call, true);
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700433 }
Santos Cordon681663d2014-01-30 04:32:15 -0800434 }
435
Sailesh Nepale59bb192014-04-01 18:33:59 -0700436 void markCallAsOnHold(Call call) {
437 setCallState(call, CallState.ON_HOLD);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700438 }
439
Santos Cordon049b7b62014-01-30 05:34:26 -0800440 /**
441 * Marks the specified call as DISCONNECTED and notifies the in-call app. If this was the last
442 * live call, then also disconnect from the in-call controller.
443 *
Santos Cordon79ff2bc2014-03-27 15:31:27 -0700444 * @param disconnectCause The disconnect reason, see {@link android.telephony.DisconnectCause}.
445 * @param disconnectMessage Optional call-service-provided message about the disconnect.
Santos Cordon049b7b62014-01-30 05:34:26 -0800446 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700447 void markCallAsDisconnected(Call call, int disconnectCause, String disconnectMessage) {
448 call.setDisconnectCause(disconnectCause, disconnectMessage);
449 setCallState(call, CallState.DISCONNECTED);
Sailesh Nepal4857f472014-04-07 22:26:27 -0700450
451 // Only remove the call if handoff is not pending.
452 if (call.getHandoffCallServiceDescriptor() == null) {
453 removeCall(call);
454 }
Ben Gilada0d9f752014-02-26 11:49:03 -0800455 }
456
Sailesh Nepal6ab6fb72014-04-01 20:03:19 -0700457 void setHandoffInfo(Call call, Uri handle, Bundle extras) {
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700458 if (!mCalls.contains(call)) {
459 Log.w(this, "Unknown call (%s) asked to set handoff info", call);
460 return;
461 }
462
463 if (extras == null) {
464 call.setExtras(Bundle.EMPTY);
465 } else {
466 call.setExtras(extras);
467 }
468
469 Uri oldHandle = call.getHandoffHandle();
470 Log.v(this, "set handoff handle %s -> %s, for call: %s", oldHandle, handle, call);
471 if (!areUriEqual(oldHandle, handle)) {
472 call.setHandoffHandle(handle);
473 for (CallsManagerListener listener : mListeners) {
474 listener.onCallHandoffHandleChanged(call, oldHandle, handle);
475 }
476 }
Sailesh Nepal6ab6fb72014-04-01 20:03:19 -0700477 }
478
Ben Gilada0d9f752014-02-26 11:49:03 -0800479 /**
Santos Cordon4b2c1192014-03-19 18:15:38 -0700480 * Cleans up any calls currently associated with the specified call service when the
481 * call-service binder disconnects unexpectedly.
482 *
483 * @param callService The call service that disconnected.
484 */
485 void handleCallServiceDeath(CallServiceWrapper callService) {
486 Preconditions.checkNotNull(callService);
Sailesh Nepale59bb192014-04-01 18:33:59 -0700487 for (Call call : ImmutableList.copyOf(mCalls)) {
Santos Cordon4b2c1192014-03-19 18:15:38 -0700488 if (call.getCallService() == callService) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700489 markCallAsDisconnected(call, DisconnectCause.ERROR_UNSPECIFIED, null);
Santos Cordon4b2c1192014-03-19 18:15:38 -0700490 }
491 }
492 }
493
Santos Cordonc7e85d42014-05-22 02:51:48 -0700494 boolean hasActiveOrHoldingCall() {
495 for (Call call : mCalls) {
496 CallState state = call.getState();
497 if (state == CallState.ACTIVE || state == CallState.ON_HOLD) {
498 return true;
499 }
500 }
501 return false;
502 }
503
504 boolean hasRingingCall() {
505 for (Call call : mCalls) {
506 if (call.getState() == CallState.RINGING) {
507 return true;
508 }
509 }
510 return false;
511 }
512
Santos Cordon4b2c1192014-03-19 18:15:38 -0700513 /**
Ben Gilada0d9f752014-02-26 11:49:03 -0800514 * Adds the specified call to the main list of live calls.
515 *
516 * @param call The call to add.
517 */
518 private void addCall(Call call) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700519 mCalls.add(call);
Santos Cordon40f78c22014-04-07 02:11:42 -0700520
521 // TODO(santoscordon): Update mForegroundCall prior to invoking
522 // onCallAdded for calls which immediately take the foreground (like the first call).
Santos Cordona56f2762014-03-24 15:55:53 -0700523 for (CallsManagerListener listener : mListeners) {
524 listener.onCallAdded(call);
525 }
Sailesh Nepal810735e2014-03-18 18:15:46 -0700526 updateForegroundCall();
Ben Gilada0d9f752014-02-26 11:49:03 -0800527 }
528
Sailesh Nepal810735e2014-03-18 18:15:46 -0700529 private void removeCall(Call call) {
Sailesh Nepal4857f472014-04-07 22:26:27 -0700530 // If a handoff is pending then the original call shouldn't be removed.
531 Preconditions.checkState(call.getHandoffCallServiceDescriptor() == null);
Santos Cordonc499c1c2014-04-14 17:13:14 -0700532 Log.v(this, "removeCall(%s)", call);
Sailesh Nepal4857f472014-04-07 22:26:27 -0700533
Santos Cordon766d04f2014-05-06 10:28:25 -0700534 call.removeListener(this);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700535 call.clearCallService();
Sailesh Nepale59bb192014-04-01 18:33:59 -0700536 call.clearCallServiceSelector();
537
538 boolean shouldNotify = false;
539 if (mCalls.contains(call)) {
540 mCalls.remove(call);
541 shouldNotify = true;
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700542 } else if (mPendingHandoffCalls.contains(call)) {
Sailesh Nepal4857f472014-04-07 22:26:27 -0700543 Log.v(this, "removeCall, marking handoff call as failed");
544 completeHandoff(call, false);
Santos Cordona56f2762014-03-24 15:55:53 -0700545 }
Ben Gilada0d9f752014-02-26 11:49:03 -0800546
Sailesh Nepale59bb192014-04-01 18:33:59 -0700547 // Only broadcast changes for calls that are being tracked.
548 if (shouldNotify) {
549 for (CallsManagerListener listener : mListeners) {
550 listener.onCallRemoved(call);
551 }
552 updateForegroundCall();
Sailesh Nepal810735e2014-03-18 18:15:46 -0700553 }
554 }
Evan Charlton5c670a92014-03-06 14:58:20 -0800555
Sailesh Nepal810735e2014-03-18 18:15:46 -0700556 /**
Santos Cordon1ae2b852014-03-19 03:03:10 -0700557 * Sets the specified state on the specified call.
Sailesh Nepal810735e2014-03-18 18:15:46 -0700558 *
559 * @param call The call.
560 * @param newState The new state of the call.
561 */
562 private void setCallState(Call call, CallState newState) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700563 Preconditions.checkNotNull(newState);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700564 CallState oldState = call.getState();
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700565 Log.i(this, "setCallState %s -> %s, call: %s", oldState, newState, call);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700566 if (newState != oldState) {
567 // Unfortunately, in the telephony world the radio is king. So if the call notifies
568 // us that the call is in a particular state, we allow it even if it doesn't make
569 // sense (e.g., ACTIVE -> RINGING).
570 // TODO(santoscordon): Consider putting a stop to the above and turning CallState
571 // into a well-defined state machine.
572 // TODO(santoscordon): Define expected state transitions here, and log when an
573 // unexpected transition occurs.
574 call.setState(newState);
575
576 // Only broadcast state change for calls that are being tracked.
Sailesh Nepale59bb192014-04-01 18:33:59 -0700577 if (mCalls.contains(call)) {
Santos Cordona56f2762014-03-24 15:55:53 -0700578 for (CallsManagerListener listener : mListeners) {
579 listener.onCallStateChanged(call, oldState, newState);
580 }
Sailesh Nepal810735e2014-03-18 18:15:46 -0700581 updateForegroundCall();
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800582 }
583 }
584 }
585
586 /**
Sailesh Nepal810735e2014-03-18 18:15:46 -0700587 * Checks which call should be visible to the user and have audio focus.
Evan Charlton5c670a92014-03-06 14:58:20 -0800588 */
Sailesh Nepal810735e2014-03-18 18:15:46 -0700589 private void updateForegroundCall() {
590 Call newForegroundCall = null;
Sailesh Nepale59bb192014-04-01 18:33:59 -0700591 for (Call call : mCalls) {
Santos Cordon40f78c22014-04-07 02:11:42 -0700592 // TODO(santoscordon): Foreground-ness needs to be explicitly set. No call, regardless
593 // of its state will be foreground by default and instead the call service should be
594 // notified when its calls enter and exit foreground state. Foreground will mean that
595 // the call should play audio and listen to microphone if it wants.
596
597 // Active calls have priority.
598 if (call.isActive()) {
Sailesh Nepal810735e2014-03-18 18:15:46 -0700599 newForegroundCall = call;
Evan Charlton5c670a92014-03-06 14:58:20 -0800600 break;
Sailesh Nepal810735e2014-03-18 18:15:46 -0700601 }
Santos Cordon40f78c22014-04-07 02:11:42 -0700602
603 if (call.isAlive() || call.getState() == CallState.RINGING) {
Sailesh Nepal810735e2014-03-18 18:15:46 -0700604 newForegroundCall = call;
Santos Cordon40f78c22014-04-07 02:11:42 -0700605 // Don't break in case there's an active call that has priority.
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800606 }
607 }
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800608
Sailesh Nepal810735e2014-03-18 18:15:46 -0700609 if (newForegroundCall != mForegroundCall) {
Santos Cordon40f78c22014-04-07 02:11:42 -0700610 Log.v(this, "Updating foreground call, %s -> %s.", mForegroundCall, newForegroundCall);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700611 Call oldForegroundCall = mForegroundCall;
612 mForegroundCall = newForegroundCall;
613
Santos Cordona56f2762014-03-24 15:55:53 -0700614 for (CallsManagerListener listener : mListeners) {
615 listener.onForegroundCallChanged(oldForegroundCall, mForegroundCall);
616 }
Santos Cordon681663d2014-01-30 04:32:15 -0800617 }
618 }
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700619
Sailesh Nepal4857f472014-04-07 22:26:27 -0700620 private void completeHandoff(Call handoffCall, boolean wasSuccessful) {
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700621 Call originalCall = handoffCall.getOriginalCall();
Sailesh Nepal4857f472014-04-07 22:26:27 -0700622 Log.v(this, "complete handoff, %s -> %s, wasSuccessful: %b", handoffCall, originalCall,
623 wasSuccessful);
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700624
Sailesh Nepal4857f472014-04-07 22:26:27 -0700625 // Remove the transient handoff call object (don't disconnect because the call could still
626 // be live).
627 mPendingHandoffCalls.remove(handoffCall);
Santos Cordon6cb7ba92014-05-23 14:09:32 -0700628 handoffCall.removeListener(this);
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700629
Sailesh Nepal4857f472014-04-07 22:26:27 -0700630 if (wasSuccessful) {
Sailesh Nepaldcd64262014-05-23 17:03:28 -0700631 if (TelephonyUtil.isCurrentlyPSTNCall(originalCall)) {
632 originalCall.disconnect();
633 }
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700634
Sailesh Nepal4857f472014-04-07 22:26:27 -0700635 // Synchronize.
636 originalCall.setCallService(handoffCall.getCallService(), handoffCall);
637 setCallState(originalCall, handoffCall.getState());
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700638
Sailesh Nepal4857f472014-04-07 22:26:27 -0700639 // Force the foreground call changed notification to be sent.
640 for (CallsManagerListener listener : mListeners) {
641 listener.onForegroundCallChanged(mForegroundCall, mForegroundCall);
642 }
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700643
Sailesh Nepal4857f472014-04-07 22:26:27 -0700644 updateHandoffCallServiceDescriptor(originalCall, null);
645 } else {
646 updateHandoffCallServiceDescriptor(originalCall, null);
647 if (originalCall.getState() == CallState.DISCONNECTED ||
648 originalCall.getState() == CallState.ABORTED) {
649 removeCall(originalCall);
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700650 }
651 }
652 }
653
Sailesh Nepal4857f472014-04-07 22:26:27 -0700654 private void updateHandoffCallServiceDescriptor(
655 Call originalCall,
656 CallServiceDescriptor newDescriptor) {
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700657 CallServiceDescriptor oldDescriptor = originalCall.getHandoffCallServiceDescriptor();
Sailesh Nepal4857f472014-04-07 22:26:27 -0700658 Log.v(this, "updateHandoffCallServiceDescriptor, call: %s, pending descriptor: %s -> %s",
659 originalCall, oldDescriptor, newDescriptor);
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700660
661 if (!areDescriptorsEqual(oldDescriptor, newDescriptor)) {
662 originalCall.setHandoffCallServiceDescriptor(newDescriptor);
663 for (CallsManagerListener listener : mListeners) {
664 listener.onCallHandoffCallServiceDescriptorChanged(originalCall, oldDescriptor,
665 newDescriptor);
666 }
667 }
668 }
669
670 private static boolean areDescriptorsEqual(
671 CallServiceDescriptor descriptor1,
672 CallServiceDescriptor descriptor2) {
673 if (descriptor1 == null) {
674 return descriptor2 == null;
675 }
676 return descriptor1.equals(descriptor2);
677 }
678
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700679 private static boolean areUriEqual(Uri handle1, Uri handle2) {
680 if (handle1 == null) {
681 return handle2 == null;
682 }
683 return handle1.equals(handle2);
684 }
Ben Gilad9f2bed32013-12-12 17:43:26 -0800685}