blob: e8b49e50e8e32cd208798aa2cce4328b3492d1ef [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);
Ihab Awadcb387ac2014-05-28 16:49:38 -070063 void onRequestingRingback(Call call, boolean ringback);
Sailesh Nepal810735e2014-03-18 18:15:46 -070064 }
65
Santos Cordon8e8b8d22013-12-19 14:14:05 -080066 private static final CallsManager INSTANCE = new CallsManager();
Ben Gilad9f2bed32013-12-12 17:43:26 -080067
Santos Cordon8e8b8d22013-12-19 14:14:05 -080068 /**
Sailesh Nepale59bb192014-04-01 18:33:59 -070069 * The main call repository. Keeps an instance of all live calls. New incoming and outgoing
70 * calls are added to the map and removed when the calls move to the disconnected state.
Santos Cordon681663d2014-01-30 04:32:15 -080071 */
Santos Cordonf3671a62014-05-29 21:51:53 -070072 private final Set<Call> mCalls = new LinkedHashSet<>();
Santos Cordon681663d2014-01-30 04:32:15 -080073
74 /**
Sailesh Nepal84fa5f82014-04-02 11:01:11 -070075 * Set of new calls created to perform a handoff. The calls are added when handoff is initiated
76 * and removed when hadnoff is complete.
77 */
Santos Cordonf3671a62014-05-29 21:51:53 -070078 private final Set<Call> mPendingHandoffCalls = new LinkedHashSet<>();
79
80
81 private final DtmfLocalTonePlayer mDtmfLocalTonePlayer = new DtmfLocalTonePlayer();
82 private final InCallController mInCallController = new InCallController();
83 private final CallAudioManager mCallAudioManager;
84 private final Ringer mRinger;
85 private final Set<CallsManagerListener> mListeners = new HashSet<>();
Sailesh Nepal84fa5f82014-04-02 11:01:11 -070086
87 /**
Sailesh Nepal810735e2014-03-18 18:15:46 -070088 * The call the user is currently interacting with. This is the call that should have audio
89 * focus and be visible in the in-call UI.
Santos Cordon4e9fffe2014-03-04 18:13:41 -080090 */
Sailesh Nepal810735e2014-03-18 18:15:46 -070091 private Call mForegroundCall;
Santos Cordon4e9fffe2014-03-04 18:13:41 -080092
Santos Cordon766d04f2014-05-06 10:28:25 -070093 /** Singleton accessor. */
94 static CallsManager getInstance() {
95 return INSTANCE;
96 }
Ben Gilad9f2bed32013-12-12 17:43:26 -080097
Santos Cordon8e8b8d22013-12-19 14:14:05 -080098 /**
Ben Gilad8bdaa462014-02-05 12:53:19 -080099 * Initializes the required Telecomm components.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800100 */
101 private CallsManager() {
Santos Cordona0e5f1a2014-03-31 21:43:00 -0700102 TelecommApp app = TelecommApp.getInstance();
Santos Cordona56f2762014-03-24 15:55:53 -0700103
Sailesh Nepal810735e2014-03-18 18:15:46 -0700104 mCallAudioManager = new CallAudioManager();
Santos Cordonc7b8eba2014-04-01 15:26:28 -0700105 InCallTonePlayer.Factory playerFactory = new InCallTonePlayer.Factory(mCallAudioManager);
Santos Cordonae193062014-05-21 21:21:49 -0700106 mRinger = new Ringer(mCallAudioManager, this, playerFactory, app);
107
Santos Cordona0e5f1a2014-03-31 21:43:00 -0700108 mListeners.add(new CallLogManager(app));
Santos Cordona56f2762014-03-24 15:55:53 -0700109 mListeners.add(new PhoneStateBroadcaster());
Santos Cordonf3671a62014-05-29 21:51:53 -0700110 mListeners.add(mInCallController);
Santos Cordonae193062014-05-21 21:21:49 -0700111 mListeners.add(mRinger);
Santos Cordonc7b8eba2014-04-01 15:26:28 -0700112 mListeners.add(new RingbackPlayer(this, playerFactory));
113 mListeners.add(new InCallToneMonitor(playerFactory, this));
Santos Cordona56f2762014-03-24 15:55:53 -0700114 mListeners.add(mCallAudioManager);
Santos Cordona0e5f1a2014-03-31 21:43:00 -0700115 mListeners.add(app.getMissedCallNotifier());
Santos Cordon92a2d812014-04-30 15:19:01 -0700116 mListeners.add(mDtmfLocalTonePlayer);
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800117 }
118
Santos Cordon766d04f2014-05-06 10:28:25 -0700119 @Override
120 public void onSuccessfulOutgoingCall(Call call) {
121 Log.v(this, "onSuccessfulOutgoingCall, %s", call);
122 if (mCalls.contains(call)) {
123 // The call's CallService has been updated.
124 for (CallsManagerListener listener : mListeners) {
125 listener.onCallServiceChanged(call, null, call.getCallService());
126 }
127 } else if (mPendingHandoffCalls.contains(call)) {
128 updateHandoffCallServiceDescriptor(call.getOriginalCall(),
129 call.getCallService().getDescriptor());
Santos Cordon6cb7ba92014-05-23 14:09:32 -0700130 } else {
131 Log.wtf(this, "unexpected successful call notification: %s", call);
132 return;
Santos Cordon766d04f2014-05-06 10:28:25 -0700133 }
Santos Cordon6cb7ba92014-05-23 14:09:32 -0700134
135 markCallAsDialing(call);
Santos Cordon766d04f2014-05-06 10:28:25 -0700136 }
137
138 @Override
139 public void onFailedOutgoingCall(Call call, boolean isAborted) {
140 Log.v(this, "onFailedOutgoingCall, call: %s, isAborted: %b", call, isAborted);
141 if (isAborted) {
142 setCallState(call, CallState.ABORTED);
143 removeCall(call);
144 } else {
145 // TODO: Replace disconnect cause with more specific disconnect causes.
146 markCallAsDisconnected(call, DisconnectCause.ERROR_UNSPECIFIED, null);
147 }
148 }
149
150 @Override
151 public void onSuccessfulIncomingCall(Call call, CallInfo callInfo) {
152 Log.d(this, "onSuccessfulIncomingCall");
153 setCallState(call, callInfo.getState());
154 addCall(call);
155 }
156
157 @Override
158 public void onFailedIncomingCall(Call call) {
159 call.removeListener(this);
Ben Gilada0d9f752014-02-26 11:49:03 -0800160 }
161
Ihab Awadcb387ac2014-05-28 16:49:38 -0700162 @Override
163 public void onRequestingRingback(Call call, boolean ringback) {
164 for (CallsManagerListener listener : mListeners) {
165 listener.onRequestingRingback(call, ringback);
166 }
167 }
168
Sailesh Nepal810735e2014-03-18 18:15:46 -0700169 ImmutableCollection<Call> getCalls() {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700170 return ImmutableList.copyOf(mCalls);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700171 }
172
173 Call getForegroundCall() {
174 return mForegroundCall;
175 }
176
Santos Cordonae193062014-05-21 21:21:49 -0700177 Ringer getRinger() {
178 return mRinger;
179 }
180
Santos Cordonf3671a62014-05-29 21:51:53 -0700181 InCallController getInCallController() {
182 return mInCallController;
183 }
184
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700185 boolean hasEmergencyCall() {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700186 for (Call call : mCalls) {
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700187 if (call.isEmergencyCall()) {
188 return true;
189 }
190 }
191 return false;
192 }
193
194 CallAudioState getAudioState() {
195 return mCallAudioManager.getAudioState();
196 }
197
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800198 /**
Santos Cordon493e8f22014-02-19 03:15:12 -0800199 * Starts the incoming call sequence by having switchboard gather more information about the
200 * specified call; using the specified call service descriptor. Upon success, execution returns
Santos Cordon766d04f2014-05-06 10:28:25 -0700201 * to {@link #onSuccessfulIncomingCall} to start the in-call UI.
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800202 *
Ben Giladc5b22692014-02-18 20:03:22 -0800203 * @param descriptor The descriptor of the call service to use for this incoming call.
Evan Charltona05805b2014-03-05 08:21:46 -0800204 * @param extras The optional extras Bundle passed with the intent used for the incoming call.
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800205 */
Evan Charltona05805b2014-03-05 08:21:46 -0800206 void processIncomingCallIntent(CallServiceDescriptor descriptor, Bundle extras) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800207 Log.d(this, "processIncomingCallIntent");
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800208 // Create a call with no handle. Eventually, switchboard will update the call with
Sailesh Nepale59bb192014-04-01 18:33:59 -0700209 // additional information from the call service, but for now we just need one to pass
210 // around.
Sailesh Nepal810735e2014-03-18 18:15:46 -0700211 Call call = new Call(true /* isIncoming */);
Santos Cordon766d04f2014-05-06 10:28:25 -0700212 // TODO(santoscordon): Move this to be a part of addCall()
213 call.addListener(this);
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800214
Santos Cordon766d04f2014-05-06 10:28:25 -0700215 call.startIncoming(descriptor, extras);
Ben Gilada0d9f752014-02-26 11:49:03 -0800216 }
217
218 /**
Yorke Lee33501632014-03-17 19:24:12 -0700219 * Attempts to issue/connect the specified call.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800220 *
Yorke Lee33501632014-03-17 19:24:12 -0700221 * @param handle Handle to connect the call with.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800222 * @param contactInfo Information about the entity being called.
Yorke Lee33501632014-03-17 19:24:12 -0700223 * @param gatewayInfo Optional gateway information that can be used to route the call to the
Santos Cordon5b7b9b32014-03-26 14:00:22 -0700224 * actual dialed handle via a gateway provider. May be null.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800225 */
Yorke Lee33501632014-03-17 19:24:12 -0700226 void placeOutgoingCall(Uri handle, ContactInfo contactInfo, GatewayInfo gatewayInfo) {
Yorke Lee33501632014-03-17 19:24:12 -0700227 final Uri uriHandle = (gatewayInfo == null) ? handle : gatewayInfo.getGatewayHandle();
228
229 if (gatewayInfo == null) {
Santos Cordonb58f4532014-05-29 11:59:06 -0700230 Log.i(this, "Creating a new outgoing call with handle: %s", Log.piiHandle(uriHandle));
Yorke Lee33501632014-03-17 19:24:12 -0700231 } else {
232 Log.i(this, "Creating a new outgoing call with gateway handle: %s, original handle: %s",
233 Log.pii(uriHandle), Log.pii(handle));
234 }
Santos Cordon766d04f2014-05-06 10:28:25 -0700235
Santos Cordonfd71f4a2014-05-28 13:59:49 -0700236 Call call = new Call(uriHandle, gatewayInfo, false /* isIncoming */);
Santos Cordon766d04f2014-05-06 10:28:25 -0700237
238 // TODO(santoscordon): Move this to be a part of addCall()
239 call.addListener(this);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700240 addCall(call);
Santos Cordone3d76ab2014-01-28 17:25:20 -0800241
Santos Cordon766d04f2014-05-06 10:28:25 -0700242 call.startOutgoing();
Yorke Leef98fb572014-03-05 10:56:55 -0800243 }
244
245 /**
Santos Cordone3d76ab2014-01-28 17:25:20 -0800246 * Instructs Telecomm to answer the specified call. Intended to be invoked by the in-call
247 * app through {@link InCallAdapter} after Telecomm notifies it of an incoming call followed by
248 * the user opting to answer said call.
Santos Cordone3d76ab2014-01-28 17:25:20 -0800249 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700250 void answerCall(Call call) {
251 if (!mCalls.contains(call)) {
252 Log.i(this, "Request to answer a non-existent call %s", call);
Santos Cordon61d0f702014-02-19 02:52:23 -0800253 } else {
Santos Cordon40f78c22014-04-07 02:11:42 -0700254 // If the foreground call is not the ringing call and it is currently isActive() or
255 // DIALING, put it on hold before answering the call.
256 if (mForegroundCall != null && mForegroundCall != call &&
257 (mForegroundCall.isActive() ||
258 mForegroundCall.getState() == CallState.DIALING)) {
259 Log.v(this, "Holding active/dialing call %s before answering incoming call %s.",
260 mForegroundCall, call);
261 mForegroundCall.hold();
262 // TODO(santoscordon): Wait until we get confirmation of the active call being
263 // on-hold before answering the new call.
264 // TODO(santoscordon): Import logic from CallManager.acceptCall()
265 }
266
Santos Cordona56f2762014-03-24 15:55:53 -0700267 for (CallsManagerListener listener : mListeners) {
268 listener.onIncomingCallAnswered(call);
269 }
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800270
Santos Cordon61d0f702014-02-19 02:52:23 -0800271 // We do not update the UI until we get confirmation of the answer() through
Sailesh Nepale59bb192014-04-01 18:33:59 -0700272 // {@link #markCallAsActive}.
Santos Cordon61d0f702014-02-19 02:52:23 -0800273 call.answer();
274 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800275 }
276
277 /**
278 * Instructs Telecomm to reject the specified call. Intended to be invoked by the in-call
279 * app through {@link InCallAdapter} after Telecomm notifies it of an incoming call followed by
280 * the user opting to reject said call.
Santos Cordone3d76ab2014-01-28 17:25:20 -0800281 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700282 void rejectCall(Call call) {
283 if (!mCalls.contains(call)) {
284 Log.i(this, "Request to reject a non-existent call %s", call);
Santos Cordon61d0f702014-02-19 02:52:23 -0800285 } else {
Santos Cordona56f2762014-03-24 15:55:53 -0700286 for (CallsManagerListener listener : mListeners) {
287 listener.onIncomingCallRejected(call);
288 }
Santos Cordon61d0f702014-02-19 02:52:23 -0800289 call.reject();
290 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800291 }
292
293 /**
Ihab Awad74549ec2014-03-10 15:33:25 -0700294 * Instructs Telecomm to play the specified DTMF tone within the specified call.
295 *
Ihab Awad74549ec2014-03-10 15:33:25 -0700296 * @param digit The DTMF digit to play.
297 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700298 void playDtmfTone(Call call, char digit) {
299 if (!mCalls.contains(call)) {
300 Log.i(this, "Request to play DTMF in a non-existent call %s", call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700301 } else {
302 call.playDtmfTone(digit);
Santos Cordon92a2d812014-04-30 15:19:01 -0700303 mDtmfLocalTonePlayer.playTone(call, digit);
Ihab Awad74549ec2014-03-10 15:33:25 -0700304 }
305 }
306
307 /**
308 * Instructs Telecomm to stop the currently playing DTMF tone, if any.
Ihab Awad74549ec2014-03-10 15:33:25 -0700309 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700310 void stopDtmfTone(Call call) {
311 if (!mCalls.contains(call)) {
312 Log.i(this, "Request to stop DTMF in a non-existent call %s", call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700313 } else {
314 call.stopDtmfTone();
Santos Cordon92a2d812014-04-30 15:19:01 -0700315 mDtmfLocalTonePlayer.stopTone(call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700316 }
317 }
318
319 /**
320 * Instructs Telecomm to continue the current post-dial DTMF string, if any.
Ihab Awad74549ec2014-03-10 15:33:25 -0700321 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700322 void postDialContinue(Call call) {
323 if (!mCalls.contains(call)) {
324 Log.i(this, "Request to continue post-dial string in a non-existent call %s", call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700325 } else {
326 // TODO(ihab): Implement this from this level on downwards
327 // call.postDialContinue();
328 // Must play tones locally -- see DTMFTonePlayer.java in Telephony
329 }
330 }
331
332 /**
Santos Cordone3d76ab2014-01-28 17:25:20 -0800333 * Instructs Telecomm to disconnect the specified call. Intended to be invoked by the
334 * in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered by
335 * the user hitting the end-call button.
Santos Cordone3d76ab2014-01-28 17:25:20 -0800336 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700337 void disconnectCall(Call call) {
Santos Cordon682fe6b2014-05-20 08:56:39 -0700338 Log.v(this, "disconnectCall %s", call);
339
Sailesh Nepale59bb192014-04-01 18:33:59 -0700340 if (!mCalls.contains(call)) {
341 Log.w(this, "Unknown call (%s) asked to disconnect", call);
Santos Cordon049b7b62014-01-30 05:34:26 -0800342 } else {
343 call.disconnect();
344 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800345 }
Santos Cordon681663d2014-01-30 04:32:15 -0800346
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700347 /**
348 * Instructs Telecomm to put the specified call on hold. Intended to be invoked by the
349 * in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered by
350 * the user hitting the hold button during an active call.
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700351 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700352 void holdCall(Call call) {
353 if (!mCalls.contains(call)) {
354 Log.w(this, "Unknown call (%s) asked to be put on hold", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700355 } else {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700356 Log.d(this, "Putting call on hold: (%s)", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700357 call.hold();
358 }
359 }
360
361 /**
362 * Instructs Telecomm to release the specified call from hold. Intended to be invoked by
363 * the in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered
364 * by the user hitting the hold button during a held call.
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700365 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700366 void unholdCall(Call call) {
367 if (!mCalls.contains(call)) {
368 Log.w(this, "Unknown call (%s) asked to be removed from hold", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700369 } else {
Santos Cordonc7e85d42014-05-22 02:51:48 -0700370 Log.d(this, "unholding call: (%s)", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700371 call.unhold();
372 }
373 }
374
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700375 /** Called by the in-call UI to change the mute state. */
376 void mute(boolean shouldMute) {
377 mCallAudioManager.mute(shouldMute);
378 }
379
380 /**
381 * Called by the in-call UI to change the audio route, for example to change from earpiece to
382 * speaker phone.
383 */
384 void setAudioRoute(int route) {
385 mCallAudioManager.setAudioRoute(route);
386 }
387
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700388 void startHandoffForCall(Call originalCall) {
389 if (!mCalls.contains(originalCall)) {
390 Log.w(this, "Unknown call %s asked to be handed off", originalCall);
391 return;
392 }
393
394 for (Call handoffCall : mPendingHandoffCalls) {
395 if (handoffCall.getOriginalCall() == originalCall) {
396 Log.w(this, "Call %s is already being handed off, skipping", originalCall);
397 return;
398 }
399 }
400
401 // Create a new call to be placed in the background. If handoff is successful then the
402 // original call will live on but its state will be updated to the new call's state. In
403 // particular the original call's call service will be updated to the new call's call
404 // service.
Santos Cordonfd71f4a2014-05-28 13:59:49 -0700405 Call tempCall =
406 new Call(originalCall.getHandoffHandle(), originalCall.getGatewayInfo(), false);
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700407 tempCall.setOriginalCall(originalCall);
408 tempCall.setExtras(originalCall.getExtras());
409 tempCall.setCallServiceSelector(originalCall.getCallServiceSelector());
410 mPendingHandoffCalls.add(tempCall);
Santos Cordon6cb7ba92014-05-23 14:09:32 -0700411 tempCall.addListener(this);
412
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700413 Log.d(this, "Placing handoff call");
Santos Cordon766d04f2014-05-06 10:28:25 -0700414 tempCall.startOutgoing();
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700415 }
416
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700417 /** Called when the audio state changes. */
418 void onAudioStateChanged(CallAudioState oldAudioState, CallAudioState newAudioState) {
419 Log.v(this, "onAudioStateChanged, audioState: %s -> %s", oldAudioState, newAudioState);
Santos Cordona56f2762014-03-24 15:55:53 -0700420 for (CallsManagerListener listener : mListeners) {
421 listener.onAudioStateChanged(oldAudioState, newAudioState);
422 }
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700423 }
424
Sailesh Nepale59bb192014-04-01 18:33:59 -0700425 void markCallAsRinging(Call call) {
426 setCallState(call, CallState.RINGING);
Santos Cordon681663d2014-01-30 04:32:15 -0800427 }
428
Sailesh Nepale59bb192014-04-01 18:33:59 -0700429 void markCallAsDialing(Call call) {
430 setCallState(call, CallState.DIALING);
Santos Cordon681663d2014-01-30 04:32:15 -0800431 }
432
Sailesh Nepale59bb192014-04-01 18:33:59 -0700433 void markCallAsActive(Call call) {
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700434 if (call.getConnectTimeMillis() == 0) {
435 call.setConnectTimeMillis(System.currentTimeMillis());
436 }
Sailesh Nepale59bb192014-04-01 18:33:59 -0700437 setCallState(call, CallState.ACTIVE);
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700438
439 if (mPendingHandoffCalls.contains(call)) {
Sailesh Nepal4857f472014-04-07 22:26:27 -0700440 completeHandoff(call, true);
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700441 }
Santos Cordon681663d2014-01-30 04:32:15 -0800442 }
443
Sailesh Nepale59bb192014-04-01 18:33:59 -0700444 void markCallAsOnHold(Call call) {
445 setCallState(call, CallState.ON_HOLD);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700446 }
447
Santos Cordon049b7b62014-01-30 05:34:26 -0800448 /**
449 * Marks the specified call as DISCONNECTED and notifies the in-call app. If this was the last
450 * live call, then also disconnect from the in-call controller.
451 *
Santos Cordon79ff2bc2014-03-27 15:31:27 -0700452 * @param disconnectCause The disconnect reason, see {@link android.telephony.DisconnectCause}.
453 * @param disconnectMessage Optional call-service-provided message about the disconnect.
Santos Cordon049b7b62014-01-30 05:34:26 -0800454 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700455 void markCallAsDisconnected(Call call, int disconnectCause, String disconnectMessage) {
456 call.setDisconnectCause(disconnectCause, disconnectMessage);
457 setCallState(call, CallState.DISCONNECTED);
Sailesh Nepal4857f472014-04-07 22:26:27 -0700458
459 // Only remove the call if handoff is not pending.
460 if (call.getHandoffCallServiceDescriptor() == null) {
461 removeCall(call);
462 }
Ben Gilada0d9f752014-02-26 11:49:03 -0800463 }
464
Sailesh Nepal6ab6fb72014-04-01 20:03:19 -0700465 void setHandoffInfo(Call call, Uri handle, Bundle extras) {
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700466 if (!mCalls.contains(call)) {
467 Log.w(this, "Unknown call (%s) asked to set handoff info", call);
468 return;
469 }
470
471 if (extras == null) {
472 call.setExtras(Bundle.EMPTY);
473 } else {
474 call.setExtras(extras);
475 }
476
477 Uri oldHandle = call.getHandoffHandle();
478 Log.v(this, "set handoff handle %s -> %s, for call: %s", oldHandle, handle, call);
479 if (!areUriEqual(oldHandle, handle)) {
480 call.setHandoffHandle(handle);
481 for (CallsManagerListener listener : mListeners) {
482 listener.onCallHandoffHandleChanged(call, oldHandle, handle);
483 }
484 }
Sailesh Nepal6ab6fb72014-04-01 20:03:19 -0700485 }
486
Ben Gilada0d9f752014-02-26 11:49:03 -0800487 /**
Santos Cordon4b2c1192014-03-19 18:15:38 -0700488 * Cleans up any calls currently associated with the specified call service when the
489 * call-service binder disconnects unexpectedly.
490 *
491 * @param callService The call service that disconnected.
492 */
493 void handleCallServiceDeath(CallServiceWrapper callService) {
494 Preconditions.checkNotNull(callService);
Sailesh Nepale59bb192014-04-01 18:33:59 -0700495 for (Call call : ImmutableList.copyOf(mCalls)) {
Santos Cordon4b2c1192014-03-19 18:15:38 -0700496 if (call.getCallService() == callService) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700497 markCallAsDisconnected(call, DisconnectCause.ERROR_UNSPECIFIED, null);
Santos Cordon4b2c1192014-03-19 18:15:38 -0700498 }
499 }
500 }
501
Santos Cordonc7e85d42014-05-22 02:51:48 -0700502 boolean hasActiveOrHoldingCall() {
503 for (Call call : mCalls) {
504 CallState state = call.getState();
505 if (state == CallState.ACTIVE || state == CallState.ON_HOLD) {
506 return true;
507 }
508 }
509 return false;
510 }
511
512 boolean hasRingingCall() {
513 for (Call call : mCalls) {
514 if (call.getState() == CallState.RINGING) {
515 return true;
516 }
517 }
518 return false;
519 }
520
Santos Cordon4b2c1192014-03-19 18:15:38 -0700521 /**
Ben Gilada0d9f752014-02-26 11:49:03 -0800522 * Adds the specified call to the main list of live calls.
523 *
524 * @param call The call to add.
525 */
526 private void addCall(Call call) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700527 mCalls.add(call);
Santos Cordon40f78c22014-04-07 02:11:42 -0700528
529 // TODO(santoscordon): Update mForegroundCall prior to invoking
530 // onCallAdded for calls which immediately take the foreground (like the first call).
Santos Cordona56f2762014-03-24 15:55:53 -0700531 for (CallsManagerListener listener : mListeners) {
532 listener.onCallAdded(call);
533 }
Sailesh Nepal810735e2014-03-18 18:15:46 -0700534 updateForegroundCall();
Ben Gilada0d9f752014-02-26 11:49:03 -0800535 }
536
Sailesh Nepal810735e2014-03-18 18:15:46 -0700537 private void removeCall(Call call) {
Sailesh Nepal4857f472014-04-07 22:26:27 -0700538 // If a handoff is pending then the original call shouldn't be removed.
539 Preconditions.checkState(call.getHandoffCallServiceDescriptor() == null);
Santos Cordonc499c1c2014-04-14 17:13:14 -0700540 Log.v(this, "removeCall(%s)", call);
Sailesh Nepal4857f472014-04-07 22:26:27 -0700541
Santos Cordon766d04f2014-05-06 10:28:25 -0700542 call.removeListener(this);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700543 call.clearCallService();
Sailesh Nepale59bb192014-04-01 18:33:59 -0700544 call.clearCallServiceSelector();
545
546 boolean shouldNotify = false;
547 if (mCalls.contains(call)) {
548 mCalls.remove(call);
549 shouldNotify = true;
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700550 } else if (mPendingHandoffCalls.contains(call)) {
Sailesh Nepal4857f472014-04-07 22:26:27 -0700551 Log.v(this, "removeCall, marking handoff call as failed");
552 completeHandoff(call, false);
Santos Cordona56f2762014-03-24 15:55:53 -0700553 }
Ben Gilada0d9f752014-02-26 11:49:03 -0800554
Sailesh Nepale59bb192014-04-01 18:33:59 -0700555 // Only broadcast changes for calls that are being tracked.
556 if (shouldNotify) {
557 for (CallsManagerListener listener : mListeners) {
558 listener.onCallRemoved(call);
559 }
560 updateForegroundCall();
Sailesh Nepal810735e2014-03-18 18:15:46 -0700561 }
562 }
Evan Charlton5c670a92014-03-06 14:58:20 -0800563
Sailesh Nepal810735e2014-03-18 18:15:46 -0700564 /**
Santos Cordon1ae2b852014-03-19 03:03:10 -0700565 * Sets the specified state on the specified call.
Sailesh Nepal810735e2014-03-18 18:15:46 -0700566 *
567 * @param call The call.
568 * @param newState The new state of the call.
569 */
570 private void setCallState(Call call, CallState newState) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700571 Preconditions.checkNotNull(newState);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700572 CallState oldState = call.getState();
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700573 Log.i(this, "setCallState %s -> %s, call: %s", oldState, newState, call);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700574 if (newState != oldState) {
575 // Unfortunately, in the telephony world the radio is king. So if the call notifies
576 // us that the call is in a particular state, we allow it even if it doesn't make
577 // sense (e.g., ACTIVE -> RINGING).
578 // TODO(santoscordon): Consider putting a stop to the above and turning CallState
579 // into a well-defined state machine.
580 // TODO(santoscordon): Define expected state transitions here, and log when an
581 // unexpected transition occurs.
582 call.setState(newState);
583
584 // Only broadcast state change for calls that are being tracked.
Sailesh Nepale59bb192014-04-01 18:33:59 -0700585 if (mCalls.contains(call)) {
Santos Cordona56f2762014-03-24 15:55:53 -0700586 for (CallsManagerListener listener : mListeners) {
587 listener.onCallStateChanged(call, oldState, newState);
588 }
Sailesh Nepal810735e2014-03-18 18:15:46 -0700589 updateForegroundCall();
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800590 }
591 }
592 }
593
594 /**
Sailesh Nepal810735e2014-03-18 18:15:46 -0700595 * Checks which call should be visible to the user and have audio focus.
Evan Charlton5c670a92014-03-06 14:58:20 -0800596 */
Sailesh Nepal810735e2014-03-18 18:15:46 -0700597 private void updateForegroundCall() {
598 Call newForegroundCall = null;
Sailesh Nepale59bb192014-04-01 18:33:59 -0700599 for (Call call : mCalls) {
Santos Cordon40f78c22014-04-07 02:11:42 -0700600 // TODO(santoscordon): Foreground-ness needs to be explicitly set. No call, regardless
601 // of its state will be foreground by default and instead the call service should be
602 // notified when its calls enter and exit foreground state. Foreground will mean that
603 // the call should play audio and listen to microphone if it wants.
604
605 // Active calls have priority.
606 if (call.isActive()) {
Sailesh Nepal810735e2014-03-18 18:15:46 -0700607 newForegroundCall = call;
Evan Charlton5c670a92014-03-06 14:58:20 -0800608 break;
Sailesh Nepal810735e2014-03-18 18:15:46 -0700609 }
Santos Cordon40f78c22014-04-07 02:11:42 -0700610
611 if (call.isAlive() || call.getState() == CallState.RINGING) {
Sailesh Nepal810735e2014-03-18 18:15:46 -0700612 newForegroundCall = call;
Santos Cordon40f78c22014-04-07 02:11:42 -0700613 // Don't break in case there's an active call that has priority.
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800614 }
615 }
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800616
Sailesh Nepal810735e2014-03-18 18:15:46 -0700617 if (newForegroundCall != mForegroundCall) {
Santos Cordon40f78c22014-04-07 02:11:42 -0700618 Log.v(this, "Updating foreground call, %s -> %s.", mForegroundCall, newForegroundCall);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700619 Call oldForegroundCall = mForegroundCall;
620 mForegroundCall = newForegroundCall;
621
Santos Cordona56f2762014-03-24 15:55:53 -0700622 for (CallsManagerListener listener : mListeners) {
623 listener.onForegroundCallChanged(oldForegroundCall, mForegroundCall);
624 }
Santos Cordon681663d2014-01-30 04:32:15 -0800625 }
626 }
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700627
Sailesh Nepal4857f472014-04-07 22:26:27 -0700628 private void completeHandoff(Call handoffCall, boolean wasSuccessful) {
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700629 Call originalCall = handoffCall.getOriginalCall();
Sailesh Nepal4857f472014-04-07 22:26:27 -0700630 Log.v(this, "complete handoff, %s -> %s, wasSuccessful: %b", handoffCall, originalCall,
631 wasSuccessful);
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700632
Sailesh Nepal4857f472014-04-07 22:26:27 -0700633 // Remove the transient handoff call object (don't disconnect because the call could still
634 // be live).
635 mPendingHandoffCalls.remove(handoffCall);
Santos Cordon6cb7ba92014-05-23 14:09:32 -0700636 handoffCall.removeListener(this);
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700637
Sailesh Nepal4857f472014-04-07 22:26:27 -0700638 if (wasSuccessful) {
Sailesh Nepaldcd64262014-05-23 17:03:28 -0700639 if (TelephonyUtil.isCurrentlyPSTNCall(originalCall)) {
640 originalCall.disconnect();
641 }
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700642
Sailesh Nepal4857f472014-04-07 22:26:27 -0700643 // Synchronize.
644 originalCall.setCallService(handoffCall.getCallService(), handoffCall);
645 setCallState(originalCall, handoffCall.getState());
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700646
Sailesh Nepal4857f472014-04-07 22:26:27 -0700647 // Force the foreground call changed notification to be sent.
648 for (CallsManagerListener listener : mListeners) {
649 listener.onForegroundCallChanged(mForegroundCall, mForegroundCall);
650 }
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700651
Sailesh Nepal4857f472014-04-07 22:26:27 -0700652 updateHandoffCallServiceDescriptor(originalCall, null);
653 } else {
654 updateHandoffCallServiceDescriptor(originalCall, null);
655 if (originalCall.getState() == CallState.DISCONNECTED ||
656 originalCall.getState() == CallState.ABORTED) {
657 removeCall(originalCall);
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700658 }
659 }
660 }
661
Sailesh Nepal4857f472014-04-07 22:26:27 -0700662 private void updateHandoffCallServiceDescriptor(
663 Call originalCall,
664 CallServiceDescriptor newDescriptor) {
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700665 CallServiceDescriptor oldDescriptor = originalCall.getHandoffCallServiceDescriptor();
Sailesh Nepal4857f472014-04-07 22:26:27 -0700666 Log.v(this, "updateHandoffCallServiceDescriptor, call: %s, pending descriptor: %s -> %s",
667 originalCall, oldDescriptor, newDescriptor);
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700668
669 if (!areDescriptorsEqual(oldDescriptor, newDescriptor)) {
670 originalCall.setHandoffCallServiceDescriptor(newDescriptor);
671 for (CallsManagerListener listener : mListeners) {
672 listener.onCallHandoffCallServiceDescriptorChanged(originalCall, oldDescriptor,
673 newDescriptor);
674 }
675 }
676 }
677
678 private static boolean areDescriptorsEqual(
679 CallServiceDescriptor descriptor1,
680 CallServiceDescriptor descriptor2) {
681 if (descriptor1 == null) {
682 return descriptor2 == null;
683 }
684 return descriptor1.equals(descriptor2);
685 }
686
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700687 private static boolean areUriEqual(Uri handle1, Uri handle2) {
688 if (handle1 == null) {
689 return handle2 == null;
690 }
691 return handle1.equals(handle2);
692 }
Ben Gilad9f2bed32013-12-12 17:43:26 -0800693}