blob: 8e81752381baf97104ea18eaf011e1242ac1e620 [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;
Sailesh Nepal8c85dee2014-04-07 22:21:40 -070026import android.telecomm.InCallCall;
Santos Cordon79ff2bc2014-03-27 15:31:27 -070027import android.telephony.DisconnectCause;
Santos Cordon8e8b8d22013-12-19 14:14:05 -080028
Santos Cordon681663d2014-01-30 04:32:15 -080029import com.google.common.base.Preconditions;
30import com.google.common.base.Strings;
Sailesh Nepal810735e2014-03-18 18:15:46 -070031import com.google.common.collect.ImmutableCollection;
32import com.google.common.collect.ImmutableList;
Santos Cordon8e8b8d22013-12-19 14:14:05 -080033import com.google.common.collect.Lists;
Santos Cordon681663d2014-01-30 04:32:15 -080034import com.google.common.collect.Maps;
Santos Cordona56f2762014-03-24 15:55:53 -070035import com.google.common.collect.Sets;
Ben Gilad9f2bed32013-12-12 17:43:26 -080036
Ben Gilad9f2bed32013-12-12 17:43:26 -080037import java.util.List;
Santos Cordon681663d2014-01-30 04:32:15 -080038import java.util.Map;
Santos Cordona56f2762014-03-24 15:55:53 -070039import java.util.Set;
Ben Gilad9f2bed32013-12-12 17:43:26 -080040
Ben Giladdd8c6082013-12-30 14:44:08 -080041/**
42 * Singleton.
43 *
44 * NOTE(gilad): by design most APIs are package private, use the relevant adapter/s to allow
45 * access from other packages specifically refraining from passing the CallsManager instance
46 * beyond the com.android.telecomm package boundary.
47 */
Santos Cordon8e8b8d22013-12-19 14:14:05 -080048public final class CallsManager {
Ben Gilada0d9f752014-02-26 11:49:03 -080049
Santos Cordona56f2762014-03-24 15:55:53 -070050 // TODO(santoscordon): Consider renaming this CallsManagerPlugin.
Sailesh Nepal810735e2014-03-18 18:15:46 -070051 interface CallsManagerListener {
52 void onCallAdded(Call call);
53 void onCallRemoved(Call call);
54 void onCallStateChanged(Call call, CallState oldState, CallState newState);
Sailesh Nepal8c85dee2014-04-07 22:21:40 -070055 void onCallHandoffHandleChanged(Call call, Uri oldHandle, Uri newHandle);
56 void onCallServiceChanged(
57 Call call,
58 CallServiceWrapper oldCallService,
59 CallServiceWrapper newCallService);
60 void onCallHandoffCallServiceDescriptorChanged(
61 Call call,
62 CallServiceDescriptor oldDescriptor,
63 CallServiceDescriptor newDescriptor);
Sailesh Nepal810735e2014-03-18 18:15:46 -070064 void onIncomingCallAnswered(Call call);
65 void onIncomingCallRejected(Call call);
66 void onForegroundCallChanged(Call oldForegroundCall, Call newForegroundCall);
Sailesh Nepal6aca10a2014-03-24 16:11:02 -070067 void onAudioStateChanged(CallAudioState oldAudioState, CallAudioState newAudioState);
Sailesh Nepal810735e2014-03-18 18:15:46 -070068 }
69
Santos Cordon8e8b8d22013-12-19 14:14:05 -080070 private static final CallsManager INSTANCE = new CallsManager();
Ben Gilad9f2bed32013-12-12 17:43:26 -080071
Santos Cordone3d76ab2014-01-28 17:25:20 -080072 private final Switchboard mSwitchboard;
73
Santos Cordon8e8b8d22013-12-19 14:14:05 -080074 /**
Sailesh Nepale59bb192014-04-01 18:33:59 -070075 * The main call repository. Keeps an instance of all live calls. New incoming and outgoing
76 * calls are added to the map and removed when the calls move to the disconnected state.
Santos Cordon681663d2014-01-30 04:32:15 -080077 */
Sailesh Nepale59bb192014-04-01 18:33:59 -070078 private final Set<Call> mCalls = Sets.newLinkedHashSet();
Santos Cordon681663d2014-01-30 04:32:15 -080079
80 /**
Sailesh Nepal84fa5f82014-04-02 11:01:11 -070081 * Set of new calls created to perform a handoff. The calls are added when handoff is initiated
82 * and removed when hadnoff is complete.
83 */
84 private final Set<Call> mPendingHandoffCalls = Sets.newLinkedHashSet();
85
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
Sailesh Nepal6aca10a2014-03-24 16:11:02 -070092 private final CallAudioManager mCallAudioManager;
Sailesh Nepal810735e2014-03-18 18:15:46 -070093
Santos Cordona56f2762014-03-24 15:55:53 -070094 private final Set<CallsManagerListener> mListeners = Sets.newHashSet();
Sailesh Nepal6aca10a2014-03-24 16:11:02 -070095
Evan Charltonf02e9882014-03-06 12:54:52 -080096 private final List<OutgoingCallValidator> mOutgoingCallValidators = Lists.newArrayList();
Ben Gilad9f2bed32013-12-12 17:43:26 -080097
Evan Charltonf02e9882014-03-06 12:54:52 -080098 private final List<IncomingCallValidator> mIncomingCallValidators = Lists.newArrayList();
Ben Gilad9f2bed32013-12-12 17:43:26 -080099
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800100 /**
Ben Gilad8bdaa462014-02-05 12:53:19 -0800101 * Initializes the required Telecomm components.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800102 */
103 private CallsManager() {
Santos Cordona0e5f1a2014-03-31 21:43:00 -0700104 TelecommApp app = TelecommApp.getInstance();
Santos Cordona56f2762014-03-24 15:55:53 -0700105
Santos Cordona0e5f1a2014-03-31 21:43:00 -0700106 mSwitchboard = new Switchboard(this);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700107 mCallAudioManager = new CallAudioManager();
Santos Cordona56f2762014-03-24 15:55:53 -0700108
Santos Cordonc7b8eba2014-04-01 15:26:28 -0700109 InCallTonePlayer.Factory playerFactory = new InCallTonePlayer.Factory(mCallAudioManager);
Santos Cordona0e5f1a2014-03-31 21:43:00 -0700110 mListeners.add(new CallLogManager(app));
Santos Cordona56f2762014-03-24 15:55:53 -0700111 mListeners.add(new PhoneStateBroadcaster());
112 mListeners.add(new InCallController());
Santos Cordon40f78c22014-04-07 02:11:42 -0700113 mListeners.add(
114 new Ringer(mCallAudioManager, this, playerFactory, TelecommApp.getInstance()));
Santos Cordonc7b8eba2014-04-01 15:26:28 -0700115 mListeners.add(new RingbackPlayer(this, playerFactory));
116 mListeners.add(new InCallToneMonitor(playerFactory, this));
Santos Cordona56f2762014-03-24 15:55:53 -0700117 mListeners.add(mCallAudioManager);
Santos Cordona0e5f1a2014-03-31 21:43:00 -0700118 mListeners.add(app.getMissedCallNotifier());
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800119 }
120
Ben Gilada0d9f752014-02-26 11:49:03 -0800121 static CallsManager getInstance() {
122 return INSTANCE;
123 }
124
Sailesh Nepal810735e2014-03-18 18:15:46 -0700125 ImmutableCollection<Call> getCalls() {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700126 return ImmutableList.copyOf(mCalls);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700127 }
128
129 Call getForegroundCall() {
130 return mForegroundCall;
131 }
132
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700133 boolean hasEmergencyCall() {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700134 for (Call call : mCalls) {
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700135 if (call.isEmergencyCall()) {
136 return true;
137 }
138 }
139 return false;
140 }
141
142 CallAudioState getAudioState() {
143 return mCallAudioManager.getAudioState();
144 }
145
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800146 /**
Santos Cordon493e8f22014-02-19 03:15:12 -0800147 * Starts the incoming call sequence by having switchboard gather more information about the
148 * specified call; using the specified call service descriptor. Upon success, execution returns
149 * to {@link #handleSuccessfulIncomingCall} to start the in-call UI.
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800150 *
Ben Giladc5b22692014-02-18 20:03:22 -0800151 * @param descriptor The descriptor of the call service to use for this incoming call.
Evan Charltona05805b2014-03-05 08:21:46 -0800152 * @param extras The optional extras Bundle passed with the intent used for the incoming call.
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800153 */
Evan Charltona05805b2014-03-05 08:21:46 -0800154 void processIncomingCallIntent(CallServiceDescriptor descriptor, Bundle extras) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800155 Log.d(this, "processIncomingCallIntent");
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800156 // Create a call with no handle. Eventually, switchboard will update the call with
Sailesh Nepale59bb192014-04-01 18:33:59 -0700157 // additional information from the call service, but for now we just need one to pass
158 // around.
Sailesh Nepal810735e2014-03-18 18:15:46 -0700159 Call call = new Call(true /* isIncoming */);
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800160
Evan Charltona05805b2014-03-05 08:21:46 -0800161 mSwitchboard.retrieveIncomingCall(call, descriptor, extras);
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800162 }
163
164 /**
Ben Gilada0d9f752014-02-26 11:49:03 -0800165 * Validates the specified call and, upon no objection to connect it, adds the new call to the
166 * list of live calls. Also notifies the in-call app so the user can answer or reject the call.
167 *
168 * @param call The new incoming call.
Sailesh Nepal810735e2014-03-18 18:15:46 -0700169 * @param callInfo The details of the call.
Ben Gilada0d9f752014-02-26 11:49:03 -0800170 */
Sailesh Nepal810735e2014-03-18 18:15:46 -0700171 void handleSuccessfulIncomingCall(Call call, CallInfo callInfo) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800172 Log.d(this, "handleSuccessfulIncomingCall");
Sailesh Nepal810735e2014-03-18 18:15:46 -0700173 Preconditions.checkState(callInfo.getState() == CallState.RINGING);
Ben Gilada0d9f752014-02-26 11:49:03 -0800174
Sailesh Nepalce704b92014-03-17 18:31:43 -0700175 Uri handle = call.getHandle();
Ben Gilada0d9f752014-02-26 11:49:03 -0800176 ContactInfo contactInfo = call.getContactInfo();
177 for (IncomingCallValidator validator : mIncomingCallValidators) {
178 if (!validator.isValid(handle, contactInfo)) {
179 // TODO(gilad): Consider displaying an error message.
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800180 Log.i(this, "Dropping restricted incoming call");
Ben Gilada0d9f752014-02-26 11:49:03 -0800181 return;
182 }
183 }
184
185 // No objection to accept the incoming call, proceed with potentially connecting it (based
186 // on the user's action, or lack thereof).
Sailesh Nepal810735e2014-03-18 18:15:46 -0700187 call.setHandle(callInfo.getHandle());
188 setCallState(call, callInfo.getState());
Ben Gilada0d9f752014-02-26 11:49:03 -0800189 addCall(call);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700190 }
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800191
Sailesh Nepal810735e2014-03-18 18:15:46 -0700192 /**
193 * Called when an incoming call was not connected.
194 *
195 * @param call The incoming call.
196 */
197 void handleUnsuccessfulIncomingCall(Call call) {
Santos Cordon79ff2bc2014-03-27 15:31:27 -0700198 // Incoming calls are not added unless they are successful. We set the state and disconnect
199 // cause just as a matter of good bookkeeping. We do not use the specific methods for
200 // setting those values so that we do not trigger CallsManagerListener events.
201 // TODO: Needs more specific disconnect error for this case.
202 call.setDisconnectCause(DisconnectCause.ERROR_UNSPECIFIED, null);
203 call.setState(CallState.DISCONNECTED);
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) {
Ben Gilad8bdaa462014-02-05 12:53:19 -0800215 for (OutgoingCallValidator validator : mOutgoingCallValidators) {
Ben Gilada0d9f752014-02-26 11:49:03 -0800216 if (!validator.isValid(handle, contactInfo)) {
217 // TODO(gilad): Display an error message.
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800218 Log.i(this, "Dropping restricted outgoing call.");
Ben Gilada0d9f752014-02-26 11:49:03 -0800219 return;
220 }
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800221 }
222
Yorke Lee33501632014-03-17 19:24:12 -0700223 final Uri uriHandle = (gatewayInfo == null) ? handle : gatewayInfo.getGatewayHandle();
224
225 if (gatewayInfo == null) {
226 Log.i(this, "Creating a new outgoing call with handle: %s", Log.pii(uriHandle));
227 } else {
228 Log.i(this, "Creating a new outgoing call with gateway handle: %s, original handle: %s",
229 Log.pii(uriHandle), Log.pii(handle));
230 }
Santos Cordon5b7b9b32014-03-26 14:00:22 -0700231 Call call = new Call(uriHandle, contactInfo, gatewayInfo, false /* isIncoming */);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700232 setCallState(call, CallState.DIALING);
233 addCall(call);
Santos Cordonc195e362014-02-11 17:05:31 -0800234 mSwitchboard.placeOutgoingCall(call);
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800235 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800236
237 /**
Sailesh Nepal810735e2014-03-18 18:15:46 -0700238 * Called when a call service acknowledges that it can place a call.
Santos Cordon681663d2014-01-30 04:32:15 -0800239 *
240 * @param call The new outgoing call.
241 */
242 void handleSuccessfulOutgoingCall(Call call) {
Sailesh Nepal810735e2014-03-18 18:15:46 -0700243 Log.v(this, "handleSuccessfulOutgoingCall, %s", call);
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700244 if (mCalls.contains(call)) {
245 // The call's CallService has been updated.
246 for (CallsManagerListener listener : mListeners) {
247 listener.onCallServiceChanged(call, null, call.getCallService());
248 }
249 } else if (mPendingHandoffCalls.contains(call)) {
Sailesh Nepal4857f472014-04-07 22:26:27 -0700250 updateHandoffCallServiceDescriptor(call.getOriginalCall(),
251 call.getCallService().getDescriptor());
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700252 }
Santos Cordon681663d2014-01-30 04:32:15 -0800253 }
254
255 /**
Sailesh Nepal810735e2014-03-18 18:15:46 -0700256 * Called when an outgoing call was not placed.
Yorke Leef98fb572014-03-05 10:56:55 -0800257 *
Sailesh Nepal810735e2014-03-18 18:15:46 -0700258 * @param call The outgoing call.
259 * @param isAborted True if the call was unsuccessful because it was aborted.
Yorke Leef98fb572014-03-05 10:56:55 -0800260 */
Sailesh Nepal810735e2014-03-18 18:15:46 -0700261 void handleUnsuccessfulOutgoingCall(Call call, boolean isAborted) {
262 Log.v(this, "handleAbortedOutgoingCall, call: %s, isAborted: %b", call, isAborted);
263 if (isAborted) {
264 call.abort();
265 setCallState(call, CallState.ABORTED);
Santos Cordon79ff2bc2014-03-27 15:31:27 -0700266 removeCall(call);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700267 } else {
Santos Cordon79ff2bc2014-03-27 15:31:27 -0700268 // TODO: Replace disconnect cause with more specific disconnect causes.
Sailesh Nepale59bb192014-04-01 18:33:59 -0700269 markCallAsDisconnected(call, DisconnectCause.ERROR_UNSPECIFIED, null);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700270 }
Yorke Leef98fb572014-03-05 10:56:55 -0800271 }
272
273 /**
Santos Cordone3d76ab2014-01-28 17:25:20 -0800274 * Instructs Telecomm to answer the specified call. Intended to be invoked by the in-call
275 * app through {@link InCallAdapter} after Telecomm notifies it of an incoming call followed by
276 * the user opting to answer said call.
Santos Cordone3d76ab2014-01-28 17:25:20 -0800277 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700278 void answerCall(Call call) {
279 if (!mCalls.contains(call)) {
280 Log.i(this, "Request to answer a non-existent call %s", call);
Santos Cordon61d0f702014-02-19 02:52:23 -0800281 } else {
Santos Cordon40f78c22014-04-07 02:11:42 -0700282 // If the foreground call is not the ringing call and it is currently isActive() or
283 // DIALING, put it on hold before answering the call.
284 if (mForegroundCall != null && mForegroundCall != call &&
285 (mForegroundCall.isActive() ||
286 mForegroundCall.getState() == CallState.DIALING)) {
287 Log.v(this, "Holding active/dialing call %s before answering incoming call %s.",
288 mForegroundCall, call);
289 mForegroundCall.hold();
290 // TODO(santoscordon): Wait until we get confirmation of the active call being
291 // on-hold before answering the new call.
292 // TODO(santoscordon): Import logic from CallManager.acceptCall()
293 }
294
Santos Cordona56f2762014-03-24 15:55:53 -0700295 for (CallsManagerListener listener : mListeners) {
296 listener.onIncomingCallAnswered(call);
297 }
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800298
Santos Cordon61d0f702014-02-19 02:52:23 -0800299 // We do not update the UI until we get confirmation of the answer() through
Sailesh Nepale59bb192014-04-01 18:33:59 -0700300 // {@link #markCallAsActive}.
Santos Cordon61d0f702014-02-19 02:52:23 -0800301 call.answer();
302 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800303 }
304
305 /**
306 * Instructs Telecomm to reject the specified call. Intended to be invoked by the in-call
307 * app through {@link InCallAdapter} after Telecomm notifies it of an incoming call followed by
308 * the user opting to reject said call.
Santos Cordone3d76ab2014-01-28 17:25:20 -0800309 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700310 void rejectCall(Call call) {
311 if (!mCalls.contains(call)) {
312 Log.i(this, "Request to reject a non-existent call %s", call);
Santos Cordon61d0f702014-02-19 02:52:23 -0800313 } else {
Santos Cordona56f2762014-03-24 15:55:53 -0700314 for (CallsManagerListener listener : mListeners) {
315 listener.onIncomingCallRejected(call);
316 }
Santos Cordon61d0f702014-02-19 02:52:23 -0800317 call.reject();
318 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800319 }
320
321 /**
Ihab Awad74549ec2014-03-10 15:33:25 -0700322 * Instructs Telecomm to play the specified DTMF tone within the specified call.
323 *
Ihab Awad74549ec2014-03-10 15:33:25 -0700324 * @param digit The DTMF digit to play.
325 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700326 void playDtmfTone(Call call, char digit) {
327 if (!mCalls.contains(call)) {
328 Log.i(this, "Request to play DTMF in a non-existent call %s", call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700329 } else {
330 call.playDtmfTone(digit);
331 }
332 }
333
334 /**
335 * Instructs Telecomm to stop the currently playing DTMF tone, if any.
Ihab Awad74549ec2014-03-10 15:33:25 -0700336 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700337 void stopDtmfTone(Call call) {
338 if (!mCalls.contains(call)) {
339 Log.i(this, "Request to stop DTMF in a non-existent call %s", call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700340 } else {
341 call.stopDtmfTone();
342 }
343 }
344
345 /**
346 * Instructs Telecomm to continue the current post-dial DTMF string, if any.
Ihab Awad74549ec2014-03-10 15:33:25 -0700347 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700348 void postDialContinue(Call call) {
349 if (!mCalls.contains(call)) {
350 Log.i(this, "Request to continue post-dial string in a non-existent call %s", call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700351 } else {
352 // TODO(ihab): Implement this from this level on downwards
353 // call.postDialContinue();
354 // Must play tones locally -- see DTMFTonePlayer.java in Telephony
355 }
356 }
357
358 /**
Santos Cordone3d76ab2014-01-28 17:25:20 -0800359 * Instructs Telecomm to disconnect the specified call. Intended to be invoked by the
360 * in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered by
361 * the user hitting the end-call button.
Santos Cordone3d76ab2014-01-28 17:25:20 -0800362 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700363 void disconnectCall(Call call) {
364 if (!mCalls.contains(call)) {
365 Log.w(this, "Unknown call (%s) asked to disconnect", call);
Santos Cordon049b7b62014-01-30 05:34:26 -0800366 } else {
367 call.disconnect();
368 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800369 }
Santos Cordon681663d2014-01-30 04:32:15 -0800370
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700371 /**
372 * Instructs Telecomm to put the specified call on hold. Intended to be invoked by the
373 * in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered by
374 * the user hitting the hold button during an active call.
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700375 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700376 void holdCall(Call call) {
377 if (!mCalls.contains(call)) {
378 Log.w(this, "Unknown call (%s) asked to be put on hold", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700379 } else {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700380 Log.d(this, "Putting call on hold: (%s)", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700381 call.hold();
382 }
383 }
384
385 /**
386 * Instructs Telecomm to release the specified call from hold. Intended to be invoked by
387 * the in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered
388 * by the user hitting the hold button during a held call.
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700389 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700390 void unholdCall(Call call) {
391 if (!mCalls.contains(call)) {
392 Log.w(this, "Unknown call (%s) asked to be removed from hold", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700393 } else {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700394 Log.d(this, "Removing call from hold: (%s)", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700395 call.unhold();
396 }
397 }
398
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700399 /** Called by the in-call UI to change the mute state. */
400 void mute(boolean shouldMute) {
401 mCallAudioManager.mute(shouldMute);
402 }
403
404 /**
405 * Called by the in-call UI to change the audio route, for example to change from earpiece to
406 * speaker phone.
407 */
408 void setAudioRoute(int route) {
409 mCallAudioManager.setAudioRoute(route);
410 }
411
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700412 void startHandoffForCall(Call originalCall) {
413 if (!mCalls.contains(originalCall)) {
414 Log.w(this, "Unknown call %s asked to be handed off", originalCall);
415 return;
416 }
417
418 for (Call handoffCall : mPendingHandoffCalls) {
419 if (handoffCall.getOriginalCall() == originalCall) {
420 Log.w(this, "Call %s is already being handed off, skipping", originalCall);
421 return;
422 }
423 }
424
425 // Create a new call to be placed in the background. If handoff is successful then the
426 // original call will live on but its state will be updated to the new call's state. In
427 // particular the original call's call service will be updated to the new call's call
428 // service.
429 Call tempCall = new Call(originalCall.getHandoffHandle(), originalCall.getContactInfo(),
430 originalCall.getGatewayInfo(), false);
431 tempCall.setOriginalCall(originalCall);
432 tempCall.setExtras(originalCall.getExtras());
433 tempCall.setCallServiceSelector(originalCall.getCallServiceSelector());
434 mPendingHandoffCalls.add(tempCall);
435 Log.d(this, "Placing handoff call");
436 mSwitchboard.placeOutgoingCall(tempCall);
437 }
438
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700439 /** Called when the audio state changes. */
440 void onAudioStateChanged(CallAudioState oldAudioState, CallAudioState newAudioState) {
441 Log.v(this, "onAudioStateChanged, audioState: %s -> %s", oldAudioState, newAudioState);
Santos Cordona56f2762014-03-24 15:55:53 -0700442 for (CallsManagerListener listener : mListeners) {
443 listener.onAudioStateChanged(oldAudioState, newAudioState);
444 }
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700445 }
446
Sailesh Nepale59bb192014-04-01 18:33:59 -0700447 void markCallAsRinging(Call call) {
448 setCallState(call, CallState.RINGING);
Santos Cordon681663d2014-01-30 04:32:15 -0800449 }
450
Sailesh Nepale59bb192014-04-01 18:33:59 -0700451 void markCallAsDialing(Call call) {
452 setCallState(call, CallState.DIALING);
Santos Cordon681663d2014-01-30 04:32:15 -0800453 }
454
Sailesh Nepale59bb192014-04-01 18:33:59 -0700455 void markCallAsActive(Call call) {
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700456 if (call.getConnectTimeMillis() == 0) {
457 call.setConnectTimeMillis(System.currentTimeMillis());
458 }
Sailesh Nepale59bb192014-04-01 18:33:59 -0700459 setCallState(call, CallState.ACTIVE);
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700460
461 if (mPendingHandoffCalls.contains(call)) {
Sailesh Nepal4857f472014-04-07 22:26:27 -0700462 completeHandoff(call, true);
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700463 }
Santos Cordon681663d2014-01-30 04:32:15 -0800464 }
465
Sailesh Nepale59bb192014-04-01 18:33:59 -0700466 void markCallAsOnHold(Call call) {
467 setCallState(call, CallState.ON_HOLD);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700468 }
469
Santos Cordon049b7b62014-01-30 05:34:26 -0800470 /**
471 * Marks the specified call as DISCONNECTED and notifies the in-call app. If this was the last
472 * live call, then also disconnect from the in-call controller.
473 *
Santos Cordon79ff2bc2014-03-27 15:31:27 -0700474 * @param disconnectCause The disconnect reason, see {@link android.telephony.DisconnectCause}.
475 * @param disconnectMessage Optional call-service-provided message about the disconnect.
Santos Cordon049b7b62014-01-30 05:34:26 -0800476 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700477 void markCallAsDisconnected(Call call, int disconnectCause, String disconnectMessage) {
478 call.setDisconnectCause(disconnectCause, disconnectMessage);
479 setCallState(call, CallState.DISCONNECTED);
Sailesh Nepal4857f472014-04-07 22:26:27 -0700480
481 // Only remove the call if handoff is not pending.
482 if (call.getHandoffCallServiceDescriptor() == null) {
483 removeCall(call);
484 }
Ben Gilada0d9f752014-02-26 11:49:03 -0800485 }
486
Sailesh Nepal6ab6fb72014-04-01 20:03:19 -0700487 void setHandoffInfo(Call call, Uri handle, Bundle extras) {
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700488 if (!mCalls.contains(call)) {
489 Log.w(this, "Unknown call (%s) asked to set handoff info", call);
490 return;
491 }
492
493 if (extras == null) {
494 call.setExtras(Bundle.EMPTY);
495 } else {
496 call.setExtras(extras);
497 }
498
499 Uri oldHandle = call.getHandoffHandle();
500 Log.v(this, "set handoff handle %s -> %s, for call: %s", oldHandle, handle, call);
501 if (!areUriEqual(oldHandle, handle)) {
502 call.setHandoffHandle(handle);
503 for (CallsManagerListener listener : mListeners) {
504 listener.onCallHandoffHandleChanged(call, oldHandle, handle);
505 }
506 }
Sailesh Nepal6ab6fb72014-04-01 20:03:19 -0700507 }
508
Ben Gilada0d9f752014-02-26 11:49:03 -0800509 /**
Santos Cordon4b2c1192014-03-19 18:15:38 -0700510 * Cleans up any calls currently associated with the specified call service when the
511 * call-service binder disconnects unexpectedly.
512 *
513 * @param callService The call service that disconnected.
514 */
515 void handleCallServiceDeath(CallServiceWrapper callService) {
516 Preconditions.checkNotNull(callService);
Sailesh Nepale59bb192014-04-01 18:33:59 -0700517 for (Call call : ImmutableList.copyOf(mCalls)) {
Santos Cordon4b2c1192014-03-19 18:15:38 -0700518 if (call.getCallService() == callService) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700519 markCallAsDisconnected(call, DisconnectCause.ERROR_UNSPECIFIED, null);
Santos Cordon4b2c1192014-03-19 18:15:38 -0700520 }
521 }
522 }
523
524 /**
Ben Gilada0d9f752014-02-26 11:49:03 -0800525 * Adds the specified call to the main list of live calls.
526 *
527 * @param call The call to add.
528 */
529 private void addCall(Call call) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700530 mCalls.add(call);
Santos Cordon40f78c22014-04-07 02:11:42 -0700531
532 // TODO(santoscordon): Update mForegroundCall prior to invoking
533 // onCallAdded for calls which immediately take the foreground (like the first call).
Santos Cordona56f2762014-03-24 15:55:53 -0700534 for (CallsManagerListener listener : mListeners) {
535 listener.onCallAdded(call);
536 }
Sailesh Nepal810735e2014-03-18 18:15:46 -0700537 updateForegroundCall();
Ben Gilada0d9f752014-02-26 11:49:03 -0800538 }
539
Sailesh Nepal810735e2014-03-18 18:15:46 -0700540 private void removeCall(Call call) {
Sailesh Nepal4857f472014-04-07 22:26:27 -0700541 // If a handoff is pending then the original call shouldn't be removed.
542 Preconditions.checkState(call.getHandoffCallServiceDescriptor() == null);
543
Sailesh Nepal810735e2014-03-18 18:15:46 -0700544 call.clearCallService();
Sailesh Nepale59bb192014-04-01 18:33:59 -0700545 call.clearCallServiceSelector();
546
547 boolean shouldNotify = false;
548 if (mCalls.contains(call)) {
549 mCalls.remove(call);
550 shouldNotify = true;
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700551 } else if (mPendingHandoffCalls.contains(call)) {
Sailesh Nepal4857f472014-04-07 22:26:27 -0700552 Log.v(this, "removeCall, marking handoff call as failed");
553 completeHandoff(call, false);
Santos Cordona56f2762014-03-24 15:55:53 -0700554 }
Ben Gilada0d9f752014-02-26 11:49:03 -0800555
Sailesh Nepale59bb192014-04-01 18:33:59 -0700556 // Only broadcast changes for calls that are being tracked.
557 if (shouldNotify) {
558 for (CallsManagerListener listener : mListeners) {
559 listener.onCallRemoved(call);
560 }
561 updateForegroundCall();
Sailesh Nepal810735e2014-03-18 18:15:46 -0700562 }
563 }
Evan Charlton5c670a92014-03-06 14:58:20 -0800564
Sailesh Nepal810735e2014-03-18 18:15:46 -0700565 /**
Santos Cordon1ae2b852014-03-19 03:03:10 -0700566 * Sets the specified state on the specified call.
Sailesh Nepal810735e2014-03-18 18:15:46 -0700567 *
568 * @param call The call.
569 * @param newState The new state of the call.
570 */
571 private void setCallState(Call call, CallState newState) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700572 Preconditions.checkNotNull(newState);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700573 CallState oldState = call.getState();
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700574 Log.i(this, "setCallState %s -> %s, call: %s", oldState, newState, call);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700575 if (newState != oldState) {
576 // Unfortunately, in the telephony world the radio is king. So if the call notifies
577 // us that the call is in a particular state, we allow it even if it doesn't make
578 // sense (e.g., ACTIVE -> RINGING).
579 // TODO(santoscordon): Consider putting a stop to the above and turning CallState
580 // into a well-defined state machine.
581 // TODO(santoscordon): Define expected state transitions here, and log when an
582 // unexpected transition occurs.
583 call.setState(newState);
584
585 // Only broadcast state change for calls that are being tracked.
Sailesh Nepale59bb192014-04-01 18:33:59 -0700586 if (mCalls.contains(call)) {
Santos Cordona56f2762014-03-24 15:55:53 -0700587 for (CallsManagerListener listener : mListeners) {
588 listener.onCallStateChanged(call, oldState, newState);
589 }
Sailesh Nepal810735e2014-03-18 18:15:46 -0700590 updateForegroundCall();
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800591 }
592 }
593 }
594
595 /**
Sailesh Nepal810735e2014-03-18 18:15:46 -0700596 * Checks which call should be visible to the user and have audio focus.
Evan Charlton5c670a92014-03-06 14:58:20 -0800597 */
Sailesh Nepal810735e2014-03-18 18:15:46 -0700598 private void updateForegroundCall() {
599 Call newForegroundCall = null;
Sailesh Nepale59bb192014-04-01 18:33:59 -0700600 for (Call call : mCalls) {
Santos Cordon40f78c22014-04-07 02:11:42 -0700601 // TODO(santoscordon): Foreground-ness needs to be explicitly set. No call, regardless
602 // of its state will be foreground by default and instead the call service should be
603 // notified when its calls enter and exit foreground state. Foreground will mean that
604 // the call should play audio and listen to microphone if it wants.
605
606 // Active calls have priority.
607 if (call.isActive()) {
Sailesh Nepal810735e2014-03-18 18:15:46 -0700608 newForegroundCall = call;
Evan Charlton5c670a92014-03-06 14:58:20 -0800609 break;
Sailesh Nepal810735e2014-03-18 18:15:46 -0700610 }
Santos Cordon40f78c22014-04-07 02:11:42 -0700611
612 if (call.isAlive() || call.getState() == CallState.RINGING) {
Sailesh Nepal810735e2014-03-18 18:15:46 -0700613 newForegroundCall = call;
Santos Cordon40f78c22014-04-07 02:11:42 -0700614 // Don't break in case there's an active call that has priority.
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800615 }
616 }
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800617
Sailesh Nepal810735e2014-03-18 18:15:46 -0700618 if (newForegroundCall != mForegroundCall) {
Santos Cordon40f78c22014-04-07 02:11:42 -0700619 Log.v(this, "Updating foreground call, %s -> %s.", mForegroundCall, newForegroundCall);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700620 Call oldForegroundCall = mForegroundCall;
621 mForegroundCall = newForegroundCall;
622
Santos Cordona56f2762014-03-24 15:55:53 -0700623 for (CallsManagerListener listener : mListeners) {
624 listener.onForegroundCallChanged(oldForegroundCall, mForegroundCall);
625 }
Santos Cordon681663d2014-01-30 04:32:15 -0800626 }
627 }
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700628
Sailesh Nepal4857f472014-04-07 22:26:27 -0700629 private void completeHandoff(Call handoffCall, boolean wasSuccessful) {
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700630 Call originalCall = handoffCall.getOriginalCall();
Sailesh Nepal4857f472014-04-07 22:26:27 -0700631 Log.v(this, "complete handoff, %s -> %s, wasSuccessful: %b", handoffCall, originalCall,
632 wasSuccessful);
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700633
Sailesh Nepal4857f472014-04-07 22:26:27 -0700634 // Remove the transient handoff call object (don't disconnect because the call could still
635 // be live).
636 mPendingHandoffCalls.remove(handoffCall);
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700637
Sailesh Nepal4857f472014-04-07 22:26:27 -0700638 if (wasSuccessful) {
639 // Disconnect.
640 originalCall.disconnect();
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700641
Sailesh Nepal4857f472014-04-07 22:26:27 -0700642 // Synchronize.
643 originalCall.setCallService(handoffCall.getCallService(), handoffCall);
644 setCallState(originalCall, handoffCall.getState());
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700645
Sailesh Nepal4857f472014-04-07 22:26:27 -0700646 // Force the foreground call changed notification to be sent.
647 for (CallsManagerListener listener : mListeners) {
648 listener.onForegroundCallChanged(mForegroundCall, mForegroundCall);
649 }
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700650
Sailesh Nepal4857f472014-04-07 22:26:27 -0700651 updateHandoffCallServiceDescriptor(originalCall, null);
652 } else {
653 updateHandoffCallServiceDescriptor(originalCall, null);
654 if (originalCall.getState() == CallState.DISCONNECTED ||
655 originalCall.getState() == CallState.ABORTED) {
656 removeCall(originalCall);
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700657 }
658 }
659 }
660
Sailesh Nepal4857f472014-04-07 22:26:27 -0700661 private void updateHandoffCallServiceDescriptor(
662 Call originalCall,
663 CallServiceDescriptor newDescriptor) {
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700664 CallServiceDescriptor oldDescriptor = originalCall.getHandoffCallServiceDescriptor();
Sailesh Nepal4857f472014-04-07 22:26:27 -0700665 Log.v(this, "updateHandoffCallServiceDescriptor, call: %s, pending descriptor: %s -> %s",
666 originalCall, oldDescriptor, newDescriptor);
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700667
668 if (!areDescriptorsEqual(oldDescriptor, newDescriptor)) {
669 originalCall.setHandoffCallServiceDescriptor(newDescriptor);
670 for (CallsManagerListener listener : mListeners) {
671 listener.onCallHandoffCallServiceDescriptorChanged(originalCall, oldDescriptor,
672 newDescriptor);
673 }
674 }
675 }
676
677 private static boolean areDescriptorsEqual(
678 CallServiceDescriptor descriptor1,
679 CallServiceDescriptor descriptor2) {
680 if (descriptor1 == null) {
681 return descriptor2 == null;
682 }
683 return descriptor1.equals(descriptor2);
684 }
685
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700686 private static boolean areUriEqual(Uri handle1, Uri handle2) {
687 if (handle1 == null) {
688 return handle2 == null;
689 }
690 return handle1.equals(handle2);
691 }
Ben Gilad9f2bed32013-12-12 17:43:26 -0800692}