blob: 5c952110d0c84ff6de59d6413d6fbd6f73e9a071 [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());
Evan Charlton893f9e32014-04-09 08:51:52 -0700113 mListeners.add(new Ringer(mCallAudioManager, this, playerFactory, app));
Santos Cordonc7b8eba2014-04-01 15:26:28 -0700114 mListeners.add(new RingbackPlayer(this, playerFactory));
115 mListeners.add(new InCallToneMonitor(playerFactory, this));
Santos Cordona56f2762014-03-24 15:55:53 -0700116 mListeners.add(mCallAudioManager);
Santos Cordona0e5f1a2014-03-31 21:43:00 -0700117 mListeners.add(app.getMissedCallNotifier());
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800118 }
119
Ben Gilada0d9f752014-02-26 11:49:03 -0800120 static CallsManager getInstance() {
121 return INSTANCE;
122 }
123
Sailesh Nepal810735e2014-03-18 18:15:46 -0700124 ImmutableCollection<Call> getCalls() {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700125 return ImmutableList.copyOf(mCalls);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700126 }
127
128 Call getForegroundCall() {
129 return mForegroundCall;
130 }
131
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700132 boolean hasEmergencyCall() {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700133 for (Call call : mCalls) {
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700134 if (call.isEmergencyCall()) {
135 return true;
136 }
137 }
138 return false;
139 }
140
141 CallAudioState getAudioState() {
142 return mCallAudioManager.getAudioState();
143 }
144
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800145 /**
Santos Cordon493e8f22014-02-19 03:15:12 -0800146 * Starts the incoming call sequence by having switchboard gather more information about the
147 * specified call; using the specified call service descriptor. Upon success, execution returns
148 * to {@link #handleSuccessfulIncomingCall} to start the in-call UI.
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800149 *
Ben Giladc5b22692014-02-18 20:03:22 -0800150 * @param descriptor The descriptor of the call service to use for this incoming call.
Evan Charltona05805b2014-03-05 08:21:46 -0800151 * @param extras The optional extras Bundle passed with the intent used for the incoming call.
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800152 */
Evan Charltona05805b2014-03-05 08:21:46 -0800153 void processIncomingCallIntent(CallServiceDescriptor descriptor, Bundle extras) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800154 Log.d(this, "processIncomingCallIntent");
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800155 // Create a call with no handle. Eventually, switchboard will update the call with
Sailesh Nepale59bb192014-04-01 18:33:59 -0700156 // additional information from the call service, but for now we just need one to pass
157 // around.
Sailesh Nepal810735e2014-03-18 18:15:46 -0700158 Call call = new Call(true /* isIncoming */);
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800159
Evan Charltona05805b2014-03-05 08:21:46 -0800160 mSwitchboard.retrieveIncomingCall(call, descriptor, extras);
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800161 }
162
163 /**
Ben Gilada0d9f752014-02-26 11:49:03 -0800164 * Validates the specified call and, upon no objection to connect it, adds the new call to the
165 * list of live calls. Also notifies the in-call app so the user can answer or reject the call.
166 *
167 * @param call The new incoming call.
Sailesh Nepal810735e2014-03-18 18:15:46 -0700168 * @param callInfo The details of the call.
Ben Gilada0d9f752014-02-26 11:49:03 -0800169 */
Sailesh Nepal810735e2014-03-18 18:15:46 -0700170 void handleSuccessfulIncomingCall(Call call, CallInfo callInfo) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800171 Log.d(this, "handleSuccessfulIncomingCall");
Sailesh Nepal810735e2014-03-18 18:15:46 -0700172 Preconditions.checkState(callInfo.getState() == CallState.RINGING);
Ben Gilada0d9f752014-02-26 11:49:03 -0800173
Sailesh Nepalce704b92014-03-17 18:31:43 -0700174 Uri handle = call.getHandle();
Ben Gilada0d9f752014-02-26 11:49:03 -0800175 ContactInfo contactInfo = call.getContactInfo();
176 for (IncomingCallValidator validator : mIncomingCallValidators) {
177 if (!validator.isValid(handle, contactInfo)) {
178 // TODO(gilad): Consider displaying an error message.
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800179 Log.i(this, "Dropping restricted incoming call");
Ben Gilada0d9f752014-02-26 11:49:03 -0800180 return;
181 }
182 }
183
184 // No objection to accept the incoming call, proceed with potentially connecting it (based
185 // on the user's action, or lack thereof).
Sailesh Nepal810735e2014-03-18 18:15:46 -0700186 call.setHandle(callInfo.getHandle());
187 setCallState(call, callInfo.getState());
Ben Gilada0d9f752014-02-26 11:49:03 -0800188 addCall(call);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700189 }
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800190
Sailesh Nepal810735e2014-03-18 18:15:46 -0700191 /**
192 * Called when an incoming call was not connected.
193 *
194 * @param call The incoming call.
195 */
196 void handleUnsuccessfulIncomingCall(Call call) {
Santos Cordon79ff2bc2014-03-27 15:31:27 -0700197 // Incoming calls are not added unless they are successful. We set the state and disconnect
198 // cause just as a matter of good bookkeeping. We do not use the specific methods for
199 // setting those values so that we do not trigger CallsManagerListener events.
200 // TODO: Needs more specific disconnect error for this case.
201 call.setDisconnectCause(DisconnectCause.ERROR_UNSPECIFIED, null);
202 call.setState(CallState.DISCONNECTED);
Ben Gilada0d9f752014-02-26 11:49:03 -0800203 }
204
205 /**
Yorke Lee33501632014-03-17 19:24:12 -0700206 * Attempts to issue/connect the specified call.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800207 *
Yorke Lee33501632014-03-17 19:24:12 -0700208 * @param handle Handle to connect the call with.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800209 * @param contactInfo Information about the entity being called.
Yorke Lee33501632014-03-17 19:24:12 -0700210 * @param gatewayInfo Optional gateway information that can be used to route the call to the
Santos Cordon5b7b9b32014-03-26 14:00:22 -0700211 * actual dialed handle via a gateway provider. May be null.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800212 */
Yorke Lee33501632014-03-17 19:24:12 -0700213 void placeOutgoingCall(Uri handle, ContactInfo contactInfo, GatewayInfo gatewayInfo) {
Ben Gilad8bdaa462014-02-05 12:53:19 -0800214 for (OutgoingCallValidator validator : mOutgoingCallValidators) {
Ben Gilada0d9f752014-02-26 11:49:03 -0800215 if (!validator.isValid(handle, contactInfo)) {
216 // TODO(gilad): Display an error message.
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800217 Log.i(this, "Dropping restricted outgoing call.");
Ben Gilada0d9f752014-02-26 11:49:03 -0800218 return;
219 }
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800220 }
221
Yorke Lee33501632014-03-17 19:24:12 -0700222 final Uri uriHandle = (gatewayInfo == null) ? handle : gatewayInfo.getGatewayHandle();
223
224 if (gatewayInfo == null) {
225 Log.i(this, "Creating a new outgoing call with handle: %s", Log.pii(uriHandle));
226 } else {
227 Log.i(this, "Creating a new outgoing call with gateway handle: %s, original handle: %s",
228 Log.pii(uriHandle), Log.pii(handle));
229 }
Santos Cordon5b7b9b32014-03-26 14:00:22 -0700230 Call call = new Call(uriHandle, contactInfo, gatewayInfo, false /* isIncoming */);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700231 setCallState(call, CallState.DIALING);
232 addCall(call);
Santos Cordonc195e362014-02-11 17:05:31 -0800233 mSwitchboard.placeOutgoingCall(call);
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800234 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800235
236 /**
Sailesh Nepal810735e2014-03-18 18:15:46 -0700237 * Called when a call service acknowledges that it can place a call.
Santos Cordon681663d2014-01-30 04:32:15 -0800238 *
239 * @param call The new outgoing call.
240 */
241 void handleSuccessfulOutgoingCall(Call call) {
Sailesh Nepal810735e2014-03-18 18:15:46 -0700242 Log.v(this, "handleSuccessfulOutgoingCall, %s", call);
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700243 if (mCalls.contains(call)) {
244 // The call's CallService has been updated.
245 for (CallsManagerListener listener : mListeners) {
246 listener.onCallServiceChanged(call, null, call.getCallService());
247 }
248 } else if (mPendingHandoffCalls.contains(call)) {
Sailesh Nepal4857f472014-04-07 22:26:27 -0700249 updateHandoffCallServiceDescriptor(call.getOriginalCall(),
250 call.getCallService().getDescriptor());
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700251 }
Santos Cordon681663d2014-01-30 04:32:15 -0800252 }
253
254 /**
Sailesh Nepal810735e2014-03-18 18:15:46 -0700255 * Called when an outgoing call was not placed.
Yorke Leef98fb572014-03-05 10:56:55 -0800256 *
Sailesh Nepal810735e2014-03-18 18:15:46 -0700257 * @param call The outgoing call.
258 * @param isAborted True if the call was unsuccessful because it was aborted.
Yorke Leef98fb572014-03-05 10:56:55 -0800259 */
Sailesh Nepal810735e2014-03-18 18:15:46 -0700260 void handleUnsuccessfulOutgoingCall(Call call, boolean isAborted) {
261 Log.v(this, "handleAbortedOutgoingCall, call: %s, isAborted: %b", call, isAborted);
262 if (isAborted) {
263 call.abort();
264 setCallState(call, CallState.ABORTED);
Santos Cordon79ff2bc2014-03-27 15:31:27 -0700265 removeCall(call);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700266 } else {
Santos Cordon79ff2bc2014-03-27 15:31:27 -0700267 // TODO: Replace disconnect cause with more specific disconnect causes.
Sailesh Nepale59bb192014-04-01 18:33:59 -0700268 markCallAsDisconnected(call, DisconnectCause.ERROR_UNSPECIFIED, null);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700269 }
Yorke Leef98fb572014-03-05 10:56:55 -0800270 }
271
272 /**
Santos Cordone3d76ab2014-01-28 17:25:20 -0800273 * Instructs Telecomm to answer the specified call. Intended to be invoked by the in-call
274 * app through {@link InCallAdapter} after Telecomm notifies it of an incoming call followed by
275 * the user opting to answer said call.
Santos Cordone3d76ab2014-01-28 17:25:20 -0800276 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700277 void answerCall(Call call) {
278 if (!mCalls.contains(call)) {
279 Log.i(this, "Request to answer a non-existent call %s", call);
Santos Cordon61d0f702014-02-19 02:52:23 -0800280 } else {
Santos Cordon40f78c22014-04-07 02:11:42 -0700281 // If the foreground call is not the ringing call and it is currently isActive() or
282 // DIALING, put it on hold before answering the call.
283 if (mForegroundCall != null && mForegroundCall != call &&
284 (mForegroundCall.isActive() ||
285 mForegroundCall.getState() == CallState.DIALING)) {
286 Log.v(this, "Holding active/dialing call %s before answering incoming call %s.",
287 mForegroundCall, call);
288 mForegroundCall.hold();
289 // TODO(santoscordon): Wait until we get confirmation of the active call being
290 // on-hold before answering the new call.
291 // TODO(santoscordon): Import logic from CallManager.acceptCall()
292 }
293
Santos Cordona56f2762014-03-24 15:55:53 -0700294 for (CallsManagerListener listener : mListeners) {
295 listener.onIncomingCallAnswered(call);
296 }
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800297
Santos Cordon61d0f702014-02-19 02:52:23 -0800298 // We do not update the UI until we get confirmation of the answer() through
Sailesh Nepale59bb192014-04-01 18:33:59 -0700299 // {@link #markCallAsActive}.
Santos Cordon61d0f702014-02-19 02:52:23 -0800300 call.answer();
301 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800302 }
303
304 /**
305 * Instructs Telecomm to reject the specified call. Intended to be invoked by the in-call
306 * app through {@link InCallAdapter} after Telecomm notifies it of an incoming call followed by
307 * the user opting to reject said call.
Santos Cordone3d76ab2014-01-28 17:25:20 -0800308 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700309 void rejectCall(Call call) {
310 if (!mCalls.contains(call)) {
311 Log.i(this, "Request to reject a non-existent call %s", call);
Santos Cordon61d0f702014-02-19 02:52:23 -0800312 } else {
Santos Cordona56f2762014-03-24 15:55:53 -0700313 for (CallsManagerListener listener : mListeners) {
314 listener.onIncomingCallRejected(call);
315 }
Santos Cordon61d0f702014-02-19 02:52:23 -0800316 call.reject();
317 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800318 }
319
320 /**
Ihab Awad74549ec2014-03-10 15:33:25 -0700321 * Instructs Telecomm to play the specified DTMF tone within the specified call.
322 *
Ihab Awad74549ec2014-03-10 15:33:25 -0700323 * @param digit The DTMF digit to play.
324 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700325 void playDtmfTone(Call call, char digit) {
326 if (!mCalls.contains(call)) {
327 Log.i(this, "Request to play DTMF in a non-existent call %s", call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700328 } else {
329 call.playDtmfTone(digit);
330 }
331 }
332
333 /**
334 * Instructs Telecomm to stop the currently playing DTMF tone, if any.
Ihab Awad74549ec2014-03-10 15:33:25 -0700335 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700336 void stopDtmfTone(Call call) {
337 if (!mCalls.contains(call)) {
338 Log.i(this, "Request to stop DTMF in a non-existent call %s", call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700339 } else {
340 call.stopDtmfTone();
341 }
342 }
343
344 /**
345 * Instructs Telecomm to continue the current post-dial DTMF string, if any.
Ihab Awad74549ec2014-03-10 15:33:25 -0700346 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700347 void postDialContinue(Call call) {
348 if (!mCalls.contains(call)) {
349 Log.i(this, "Request to continue post-dial string in a non-existent call %s", call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700350 } else {
351 // TODO(ihab): Implement this from this level on downwards
352 // call.postDialContinue();
353 // Must play tones locally -- see DTMFTonePlayer.java in Telephony
354 }
355 }
356
357 /**
Santos Cordone3d76ab2014-01-28 17:25:20 -0800358 * Instructs Telecomm to disconnect the specified call. Intended to be invoked by the
359 * in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered by
360 * the user hitting the end-call button.
Santos Cordone3d76ab2014-01-28 17:25:20 -0800361 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700362 void disconnectCall(Call call) {
363 if (!mCalls.contains(call)) {
364 Log.w(this, "Unknown call (%s) asked to disconnect", call);
Santos Cordon049b7b62014-01-30 05:34:26 -0800365 } else {
366 call.disconnect();
367 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800368 }
Santos Cordon681663d2014-01-30 04:32:15 -0800369
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700370 /**
371 * Instructs Telecomm to put the specified call on hold. Intended to be invoked by the
372 * in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered by
373 * the user hitting the hold button during an active call.
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700374 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700375 void holdCall(Call call) {
376 if (!mCalls.contains(call)) {
377 Log.w(this, "Unknown call (%s) asked to be put on hold", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700378 } else {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700379 Log.d(this, "Putting call on hold: (%s)", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700380 call.hold();
381 }
382 }
383
384 /**
385 * Instructs Telecomm to release the specified call from hold. Intended to be invoked by
386 * the in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered
387 * by the user hitting the hold button during a held call.
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700388 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700389 void unholdCall(Call call) {
390 if (!mCalls.contains(call)) {
391 Log.w(this, "Unknown call (%s) asked to be removed from hold", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700392 } else {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700393 Log.d(this, "Removing call from hold: (%s)", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700394 call.unhold();
395 }
396 }
397
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700398 /** Called by the in-call UI to change the mute state. */
399 void mute(boolean shouldMute) {
400 mCallAudioManager.mute(shouldMute);
401 }
402
403 /**
404 * Called by the in-call UI to change the audio route, for example to change from earpiece to
405 * speaker phone.
406 */
407 void setAudioRoute(int route) {
408 mCallAudioManager.setAudioRoute(route);
409 }
410
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700411 void startHandoffForCall(Call originalCall) {
412 if (!mCalls.contains(originalCall)) {
413 Log.w(this, "Unknown call %s asked to be handed off", originalCall);
414 return;
415 }
416
417 for (Call handoffCall : mPendingHandoffCalls) {
418 if (handoffCall.getOriginalCall() == originalCall) {
419 Log.w(this, "Call %s is already being handed off, skipping", originalCall);
420 return;
421 }
422 }
423
424 // Create a new call to be placed in the background. If handoff is successful then the
425 // original call will live on but its state will be updated to the new call's state. In
426 // particular the original call's call service will be updated to the new call's call
427 // service.
428 Call tempCall = new Call(originalCall.getHandoffHandle(), originalCall.getContactInfo(),
429 originalCall.getGatewayInfo(), false);
430 tempCall.setOriginalCall(originalCall);
431 tempCall.setExtras(originalCall.getExtras());
432 tempCall.setCallServiceSelector(originalCall.getCallServiceSelector());
433 mPendingHandoffCalls.add(tempCall);
434 Log.d(this, "Placing handoff call");
435 mSwitchboard.placeOutgoingCall(tempCall);
436 }
437
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700438 /** Called when the audio state changes. */
439 void onAudioStateChanged(CallAudioState oldAudioState, CallAudioState newAudioState) {
440 Log.v(this, "onAudioStateChanged, audioState: %s -> %s", oldAudioState, newAudioState);
Santos Cordona56f2762014-03-24 15:55:53 -0700441 for (CallsManagerListener listener : mListeners) {
442 listener.onAudioStateChanged(oldAudioState, newAudioState);
443 }
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700444 }
445
Sailesh Nepale59bb192014-04-01 18:33:59 -0700446 void markCallAsRinging(Call call) {
447 setCallState(call, CallState.RINGING);
Santos Cordon681663d2014-01-30 04:32:15 -0800448 }
449
Sailesh Nepale59bb192014-04-01 18:33:59 -0700450 void markCallAsDialing(Call call) {
451 setCallState(call, CallState.DIALING);
Santos Cordon681663d2014-01-30 04:32:15 -0800452 }
453
Sailesh Nepale59bb192014-04-01 18:33:59 -0700454 void markCallAsActive(Call call) {
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700455 if (call.getConnectTimeMillis() == 0) {
456 call.setConnectTimeMillis(System.currentTimeMillis());
457 }
Sailesh Nepale59bb192014-04-01 18:33:59 -0700458 setCallState(call, CallState.ACTIVE);
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700459
460 if (mPendingHandoffCalls.contains(call)) {
Sailesh Nepal4857f472014-04-07 22:26:27 -0700461 completeHandoff(call, true);
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700462 }
Santos Cordon681663d2014-01-30 04:32:15 -0800463 }
464
Sailesh Nepale59bb192014-04-01 18:33:59 -0700465 void markCallAsOnHold(Call call) {
466 setCallState(call, CallState.ON_HOLD);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700467 }
468
Santos Cordon049b7b62014-01-30 05:34:26 -0800469 /**
470 * Marks the specified call as DISCONNECTED and notifies the in-call app. If this was the last
471 * live call, then also disconnect from the in-call controller.
472 *
Santos Cordon79ff2bc2014-03-27 15:31:27 -0700473 * @param disconnectCause The disconnect reason, see {@link android.telephony.DisconnectCause}.
474 * @param disconnectMessage Optional call-service-provided message about the disconnect.
Santos Cordon049b7b62014-01-30 05:34:26 -0800475 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700476 void markCallAsDisconnected(Call call, int disconnectCause, String disconnectMessage) {
477 call.setDisconnectCause(disconnectCause, disconnectMessage);
478 setCallState(call, CallState.DISCONNECTED);
Sailesh Nepal4857f472014-04-07 22:26:27 -0700479
480 // Only remove the call if handoff is not pending.
481 if (call.getHandoffCallServiceDescriptor() == null) {
482 removeCall(call);
483 }
Ben Gilada0d9f752014-02-26 11:49:03 -0800484 }
485
Sailesh Nepal6ab6fb72014-04-01 20:03:19 -0700486 void setHandoffInfo(Call call, Uri handle, Bundle extras) {
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700487 if (!mCalls.contains(call)) {
488 Log.w(this, "Unknown call (%s) asked to set handoff info", call);
489 return;
490 }
491
492 if (extras == null) {
493 call.setExtras(Bundle.EMPTY);
494 } else {
495 call.setExtras(extras);
496 }
497
498 Uri oldHandle = call.getHandoffHandle();
499 Log.v(this, "set handoff handle %s -> %s, for call: %s", oldHandle, handle, call);
500 if (!areUriEqual(oldHandle, handle)) {
501 call.setHandoffHandle(handle);
502 for (CallsManagerListener listener : mListeners) {
503 listener.onCallHandoffHandleChanged(call, oldHandle, handle);
504 }
505 }
Sailesh Nepal6ab6fb72014-04-01 20:03:19 -0700506 }
507
Ben Gilada0d9f752014-02-26 11:49:03 -0800508 /**
Santos Cordon4b2c1192014-03-19 18:15:38 -0700509 * Cleans up any calls currently associated with the specified call service when the
510 * call-service binder disconnects unexpectedly.
511 *
512 * @param callService The call service that disconnected.
513 */
514 void handleCallServiceDeath(CallServiceWrapper callService) {
515 Preconditions.checkNotNull(callService);
Sailesh Nepale59bb192014-04-01 18:33:59 -0700516 for (Call call : ImmutableList.copyOf(mCalls)) {
Santos Cordon4b2c1192014-03-19 18:15:38 -0700517 if (call.getCallService() == callService) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700518 markCallAsDisconnected(call, DisconnectCause.ERROR_UNSPECIFIED, null);
Santos Cordon4b2c1192014-03-19 18:15:38 -0700519 }
520 }
521 }
522
523 /**
Ben Gilada0d9f752014-02-26 11:49:03 -0800524 * Adds the specified call to the main list of live calls.
525 *
526 * @param call The call to add.
527 */
528 private void addCall(Call call) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700529 mCalls.add(call);
Santos Cordon40f78c22014-04-07 02:11:42 -0700530
531 // TODO(santoscordon): Update mForegroundCall prior to invoking
532 // onCallAdded for calls which immediately take the foreground (like the first call).
Santos Cordona56f2762014-03-24 15:55:53 -0700533 for (CallsManagerListener listener : mListeners) {
534 listener.onCallAdded(call);
535 }
Sailesh Nepal810735e2014-03-18 18:15:46 -0700536 updateForegroundCall();
Ben Gilada0d9f752014-02-26 11:49:03 -0800537 }
538
Sailesh Nepal810735e2014-03-18 18:15:46 -0700539 private void removeCall(Call call) {
Sailesh Nepal4857f472014-04-07 22:26:27 -0700540 // If a handoff is pending then the original call shouldn't be removed.
541 Preconditions.checkState(call.getHandoffCallServiceDescriptor() == null);
542
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);
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700636
Sailesh Nepal4857f472014-04-07 22:26:27 -0700637 if (wasSuccessful) {
638 // Disconnect.
639 originalCall.disconnect();
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700640
Sailesh Nepal4857f472014-04-07 22:26:27 -0700641 // Synchronize.
642 originalCall.setCallService(handoffCall.getCallService(), handoffCall);
643 setCallState(originalCall, handoffCall.getState());
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700644
Sailesh Nepal4857f472014-04-07 22:26:27 -0700645 // Force the foreground call changed notification to be sent.
646 for (CallsManagerListener listener : mListeners) {
647 listener.onForegroundCallChanged(mForegroundCall, mForegroundCall);
648 }
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700649
Sailesh Nepal4857f472014-04-07 22:26:27 -0700650 updateHandoffCallServiceDescriptor(originalCall, null);
651 } else {
652 updateHandoffCallServiceDescriptor(originalCall, null);
653 if (originalCall.getState() == CallState.DISCONNECTED ||
654 originalCall.getState() == CallState.ABORTED) {
655 removeCall(originalCall);
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700656 }
657 }
658 }
659
Sailesh Nepal4857f472014-04-07 22:26:27 -0700660 private void updateHandoffCallServiceDescriptor(
661 Call originalCall,
662 CallServiceDescriptor newDescriptor) {
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700663 CallServiceDescriptor oldDescriptor = originalCall.getHandoffCallServiceDescriptor();
Sailesh Nepal4857f472014-04-07 22:26:27 -0700664 Log.v(this, "updateHandoffCallServiceDescriptor, call: %s, pending descriptor: %s -> %s",
665 originalCall, oldDescriptor, newDescriptor);
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700666
667 if (!areDescriptorsEqual(oldDescriptor, newDescriptor)) {
668 originalCall.setHandoffCallServiceDescriptor(newDescriptor);
669 for (CallsManagerListener listener : mListeners) {
670 listener.onCallHandoffCallServiceDescriptorChanged(originalCall, oldDescriptor,
671 newDescriptor);
672 }
673 }
674 }
675
676 private static boolean areDescriptorsEqual(
677 CallServiceDescriptor descriptor1,
678 CallServiceDescriptor descriptor2) {
679 if (descriptor1 == null) {
680 return descriptor2 == null;
681 }
682 return descriptor1.equals(descriptor2);
683 }
684
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700685 private static boolean areUriEqual(Uri handle1, Uri handle2) {
686 if (handle1 == null) {
687 return handle2 == null;
688 }
689 return handle1.equals(handle2);
690 }
Ben Gilad9f2bed32013-12-12 17:43:26 -0800691}