blob: b7f7aeceafc16224049228abe368c797261dee66 [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;
29import com.google.common.base.Strings;
Sailesh Nepal810735e2014-03-18 18:15:46 -070030import com.google.common.collect.ImmutableCollection;
31import com.google.common.collect.ImmutableList;
Santos Cordon8e8b8d22013-12-19 14:14:05 -080032import com.google.common.collect.Lists;
Santos Cordon681663d2014-01-30 04:32:15 -080033import com.google.common.collect.Maps;
Santos Cordona56f2762014-03-24 15:55:53 -070034import com.google.common.collect.Sets;
Ben Gilad9f2bed32013-12-12 17:43:26 -080035
Ben Gilad9f2bed32013-12-12 17:43:26 -080036import java.util.List;
Santos Cordon681663d2014-01-30 04:32:15 -080037import java.util.Map;
Santos Cordona56f2762014-03-24 15:55:53 -070038import java.util.Set;
Ben Gilad9f2bed32013-12-12 17:43:26 -080039
Ben Giladdd8c6082013-12-30 14:44:08 -080040/**
41 * Singleton.
42 *
43 * NOTE(gilad): by design most APIs are package private, use the relevant adapter/s to allow
44 * access from other packages specifically refraining from passing the CallsManager instance
45 * beyond the com.android.telecomm package boundary.
46 */
Santos Cordon8e8b8d22013-12-19 14:14:05 -080047public final class CallsManager {
Ben Gilada0d9f752014-02-26 11:49:03 -080048
Santos Cordona56f2762014-03-24 15:55:53 -070049 // TODO(santoscordon): Consider renaming this CallsManagerPlugin.
Sailesh Nepal810735e2014-03-18 18:15:46 -070050 interface CallsManagerListener {
51 void onCallAdded(Call call);
52 void onCallRemoved(Call call);
53 void onCallStateChanged(Call call, CallState oldState, CallState newState);
54 void onIncomingCallAnswered(Call call);
55 void onIncomingCallRejected(Call call);
Sailesh Nepal84fa5f82014-04-02 11:01:11 -070056 void onCallHandoffHandleChanged(Call call, Uri oldHandle, Uri newHandle);
Sailesh Nepal810735e2014-03-18 18:15:46 -070057 void onForegroundCallChanged(Call oldForegroundCall, Call newForegroundCall);
Sailesh Nepal6aca10a2014-03-24 16:11:02 -070058 void onAudioStateChanged(CallAudioState oldAudioState, CallAudioState newAudioState);
Sailesh Nepal810735e2014-03-18 18:15:46 -070059 }
60
Santos Cordon8e8b8d22013-12-19 14:14:05 -080061 private static final CallsManager INSTANCE = new CallsManager();
Ben Gilad9f2bed32013-12-12 17:43:26 -080062
Santos Cordone3d76ab2014-01-28 17:25:20 -080063 private final Switchboard mSwitchboard;
64
Santos Cordon8e8b8d22013-12-19 14:14:05 -080065 /**
Sailesh Nepale59bb192014-04-01 18:33:59 -070066 * The main call repository. Keeps an instance of all live calls. New incoming and outgoing
67 * calls are added to the map and removed when the calls move to the disconnected state.
Santos Cordon681663d2014-01-30 04:32:15 -080068 */
Sailesh Nepale59bb192014-04-01 18:33:59 -070069 private final Set<Call> mCalls = Sets.newLinkedHashSet();
Santos Cordon681663d2014-01-30 04:32:15 -080070
71 /**
Sailesh Nepal84fa5f82014-04-02 11:01:11 -070072 * Set of new calls created to perform a handoff. The calls are added when handoff is initiated
73 * and removed when hadnoff is complete.
74 */
75 private final Set<Call> mPendingHandoffCalls = Sets.newLinkedHashSet();
76
77 /**
Sailesh Nepal810735e2014-03-18 18:15:46 -070078 * The call the user is currently interacting with. This is the call that should have audio
79 * focus and be visible in the in-call UI.
Santos Cordon4e9fffe2014-03-04 18:13:41 -080080 */
Sailesh Nepal810735e2014-03-18 18:15:46 -070081 private Call mForegroundCall;
Santos Cordon4e9fffe2014-03-04 18:13:41 -080082
Sailesh Nepal6aca10a2014-03-24 16:11:02 -070083 private final CallAudioManager mCallAudioManager;
Sailesh Nepal810735e2014-03-18 18:15:46 -070084
Santos Cordona56f2762014-03-24 15:55:53 -070085 private final Set<CallsManagerListener> mListeners = Sets.newHashSet();
Sailesh Nepal6aca10a2014-03-24 16:11:02 -070086
Evan Charltonf02e9882014-03-06 12:54:52 -080087 private final List<OutgoingCallValidator> mOutgoingCallValidators = Lists.newArrayList();
Ben Gilad9f2bed32013-12-12 17:43:26 -080088
Evan Charltonf02e9882014-03-06 12:54:52 -080089 private final List<IncomingCallValidator> mIncomingCallValidators = Lists.newArrayList();
Ben Gilad9f2bed32013-12-12 17:43:26 -080090
Santos Cordon8e8b8d22013-12-19 14:14:05 -080091 /**
Ben Gilad8bdaa462014-02-05 12:53:19 -080092 * Initializes the required Telecomm components.
Santos Cordon8e8b8d22013-12-19 14:14:05 -080093 */
94 private CallsManager() {
Santos Cordon6242b132014-02-07 16:24:42 -080095 mSwitchboard = new Switchboard(this);
Santos Cordona56f2762014-03-24 15:55:53 -070096
Sailesh Nepal810735e2014-03-18 18:15:46 -070097 mCallAudioManager = new CallAudioManager();
Santos Cordona56f2762014-03-24 15:55:53 -070098
Santos Cordonc7b8eba2014-04-01 15:26:28 -070099 InCallTonePlayer.Factory playerFactory = new InCallTonePlayer.Factory(mCallAudioManager);
100
Santos Cordona56f2762014-03-24 15:55:53 -0700101 mListeners.add(new CallLogManager(TelecommApp.getInstance()));
102 mListeners.add(new PhoneStateBroadcaster());
103 mListeners.add(new InCallController());
104 mListeners.add(new Ringer(mCallAudioManager));
Santos Cordonc7b8eba2014-04-01 15:26:28 -0700105 mListeners.add(new RingbackPlayer(this, playerFactory));
106 mListeners.add(new InCallToneMonitor(playerFactory, this));
Santos Cordona56f2762014-03-24 15:55:53 -0700107 mListeners.add(mCallAudioManager);
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800108 }
109
Ben Gilada0d9f752014-02-26 11:49:03 -0800110 static CallsManager getInstance() {
111 return INSTANCE;
112 }
113
Sailesh Nepal810735e2014-03-18 18:15:46 -0700114 ImmutableCollection<Call> getCalls() {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700115 return ImmutableList.copyOf(mCalls);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700116 }
117
118 Call getForegroundCall() {
119 return mForegroundCall;
120 }
121
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700122 boolean hasEmergencyCall() {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700123 for (Call call : mCalls) {
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700124 if (call.isEmergencyCall()) {
125 return true;
126 }
127 }
128 return false;
129 }
130
131 CallAudioState getAudioState() {
132 return mCallAudioManager.getAudioState();
133 }
134
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800135 /**
Santos Cordon493e8f22014-02-19 03:15:12 -0800136 * Starts the incoming call sequence by having switchboard gather more information about the
137 * specified call; using the specified call service descriptor. Upon success, execution returns
138 * to {@link #handleSuccessfulIncomingCall} to start the in-call UI.
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800139 *
Ben Giladc5b22692014-02-18 20:03:22 -0800140 * @param descriptor The descriptor of the call service to use for this incoming call.
Evan Charltona05805b2014-03-05 08:21:46 -0800141 * @param extras The optional extras Bundle passed with the intent used for the incoming call.
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800142 */
Evan Charltona05805b2014-03-05 08:21:46 -0800143 void processIncomingCallIntent(CallServiceDescriptor descriptor, Bundle extras) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800144 Log.d(this, "processIncomingCallIntent");
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800145 // Create a call with no handle. Eventually, switchboard will update the call with
Sailesh Nepale59bb192014-04-01 18:33:59 -0700146 // additional information from the call service, but for now we just need one to pass
147 // around.
Sailesh Nepal810735e2014-03-18 18:15:46 -0700148 Call call = new Call(true /* isIncoming */);
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800149
Evan Charltona05805b2014-03-05 08:21:46 -0800150 mSwitchboard.retrieveIncomingCall(call, descriptor, extras);
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800151 }
152
153 /**
Ben Gilada0d9f752014-02-26 11:49:03 -0800154 * Validates the specified call and, upon no objection to connect it, adds the new call to the
155 * list of live calls. Also notifies the in-call app so the user can answer or reject the call.
156 *
157 * @param call The new incoming call.
Sailesh Nepal810735e2014-03-18 18:15:46 -0700158 * @param callInfo The details of the call.
Ben Gilada0d9f752014-02-26 11:49:03 -0800159 */
Sailesh Nepal810735e2014-03-18 18:15:46 -0700160 void handleSuccessfulIncomingCall(Call call, CallInfo callInfo) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800161 Log.d(this, "handleSuccessfulIncomingCall");
Sailesh Nepal810735e2014-03-18 18:15:46 -0700162 Preconditions.checkState(callInfo.getState() == CallState.RINGING);
Ben Gilada0d9f752014-02-26 11:49:03 -0800163
Sailesh Nepalce704b92014-03-17 18:31:43 -0700164 Uri handle = call.getHandle();
Ben Gilada0d9f752014-02-26 11:49:03 -0800165 ContactInfo contactInfo = call.getContactInfo();
166 for (IncomingCallValidator validator : mIncomingCallValidators) {
167 if (!validator.isValid(handle, contactInfo)) {
168 // TODO(gilad): Consider displaying an error message.
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800169 Log.i(this, "Dropping restricted incoming call");
Ben Gilada0d9f752014-02-26 11:49:03 -0800170 return;
171 }
172 }
173
174 // No objection to accept the incoming call, proceed with potentially connecting it (based
175 // on the user's action, or lack thereof).
Sailesh Nepal810735e2014-03-18 18:15:46 -0700176 call.setHandle(callInfo.getHandle());
177 setCallState(call, callInfo.getState());
Ben Gilada0d9f752014-02-26 11:49:03 -0800178 addCall(call);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700179 }
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800180
Sailesh Nepal810735e2014-03-18 18:15:46 -0700181 /**
182 * Called when an incoming call was not connected.
183 *
184 * @param call The incoming call.
185 */
186 void handleUnsuccessfulIncomingCall(Call call) {
Santos Cordon79ff2bc2014-03-27 15:31:27 -0700187 // Incoming calls are not added unless they are successful. We set the state and disconnect
188 // cause just as a matter of good bookkeeping. We do not use the specific methods for
189 // setting those values so that we do not trigger CallsManagerListener events.
190 // TODO: Needs more specific disconnect error for this case.
191 call.setDisconnectCause(DisconnectCause.ERROR_UNSPECIFIED, null);
192 call.setState(CallState.DISCONNECTED);
Ben Gilada0d9f752014-02-26 11:49:03 -0800193 }
194
195 /**
Yorke Lee33501632014-03-17 19:24:12 -0700196 * Attempts to issue/connect the specified call.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800197 *
Yorke Lee33501632014-03-17 19:24:12 -0700198 * @param handle Handle to connect the call with.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800199 * @param contactInfo Information about the entity being called.
Yorke Lee33501632014-03-17 19:24:12 -0700200 * @param gatewayInfo Optional gateway information that can be used to route the call to the
Santos Cordon5b7b9b32014-03-26 14:00:22 -0700201 * actual dialed handle via a gateway provider. May be null.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800202 */
Yorke Lee33501632014-03-17 19:24:12 -0700203 void placeOutgoingCall(Uri handle, ContactInfo contactInfo, GatewayInfo gatewayInfo) {
Ben Gilad8bdaa462014-02-05 12:53:19 -0800204 for (OutgoingCallValidator validator : mOutgoingCallValidators) {
Ben Gilada0d9f752014-02-26 11:49:03 -0800205 if (!validator.isValid(handle, contactInfo)) {
206 // TODO(gilad): Display an error message.
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800207 Log.i(this, "Dropping restricted outgoing call.");
Ben Gilada0d9f752014-02-26 11:49:03 -0800208 return;
209 }
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800210 }
211
Yorke Lee33501632014-03-17 19:24:12 -0700212 final Uri uriHandle = (gatewayInfo == null) ? handle : gatewayInfo.getGatewayHandle();
213
214 if (gatewayInfo == null) {
215 Log.i(this, "Creating a new outgoing call with handle: %s", Log.pii(uriHandle));
216 } else {
217 Log.i(this, "Creating a new outgoing call with gateway handle: %s, original handle: %s",
218 Log.pii(uriHandle), Log.pii(handle));
219 }
Santos Cordon5b7b9b32014-03-26 14:00:22 -0700220 Call call = new Call(uriHandle, contactInfo, gatewayInfo, false /* isIncoming */);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700221 setCallState(call, CallState.DIALING);
222 addCall(call);
Santos Cordonc195e362014-02-11 17:05:31 -0800223 mSwitchboard.placeOutgoingCall(call);
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800224 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800225
226 /**
Sailesh Nepal810735e2014-03-18 18:15:46 -0700227 * Called when a call service acknowledges that it can place a call.
Santos Cordon681663d2014-01-30 04:32:15 -0800228 *
229 * @param call The new outgoing call.
230 */
231 void handleSuccessfulOutgoingCall(Call call) {
Sailesh Nepal810735e2014-03-18 18:15:46 -0700232 Log.v(this, "handleSuccessfulOutgoingCall, %s", call);
Santos Cordon681663d2014-01-30 04:32:15 -0800233 }
234
235 /**
Sailesh Nepal810735e2014-03-18 18:15:46 -0700236 * Called when an outgoing call was not placed.
Yorke Leef98fb572014-03-05 10:56:55 -0800237 *
Sailesh Nepal810735e2014-03-18 18:15:46 -0700238 * @param call The outgoing call.
239 * @param isAborted True if the call was unsuccessful because it was aborted.
Yorke Leef98fb572014-03-05 10:56:55 -0800240 */
Sailesh Nepal810735e2014-03-18 18:15:46 -0700241 void handleUnsuccessfulOutgoingCall(Call call, boolean isAborted) {
242 Log.v(this, "handleAbortedOutgoingCall, call: %s, isAborted: %b", call, isAborted);
243 if (isAborted) {
244 call.abort();
245 setCallState(call, CallState.ABORTED);
Santos Cordon79ff2bc2014-03-27 15:31:27 -0700246 removeCall(call);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700247 } else {
Santos Cordon79ff2bc2014-03-27 15:31:27 -0700248 // TODO: Replace disconnect cause with more specific disconnect causes.
Sailesh Nepale59bb192014-04-01 18:33:59 -0700249 markCallAsDisconnected(call, DisconnectCause.ERROR_UNSPECIFIED, null);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700250 }
Yorke Leef98fb572014-03-05 10:56:55 -0800251 }
252
253 /**
Santos Cordone3d76ab2014-01-28 17:25:20 -0800254 * Instructs Telecomm to answer the specified call. Intended to be invoked by the in-call
255 * app through {@link InCallAdapter} after Telecomm notifies it of an incoming call followed by
256 * the user opting to answer said call.
Santos Cordone3d76ab2014-01-28 17:25:20 -0800257 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700258 void answerCall(Call call) {
259 if (!mCalls.contains(call)) {
260 Log.i(this, "Request to answer a non-existent call %s", call);
Santos Cordon61d0f702014-02-19 02:52:23 -0800261 } else {
Santos Cordona56f2762014-03-24 15:55:53 -0700262 for (CallsManagerListener listener : mListeners) {
263 listener.onIncomingCallAnswered(call);
264 }
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800265
Santos Cordon61d0f702014-02-19 02:52:23 -0800266 // We do not update the UI until we get confirmation of the answer() through
Sailesh Nepale59bb192014-04-01 18:33:59 -0700267 // {@link #markCallAsActive}.
Santos Cordon61d0f702014-02-19 02:52:23 -0800268 call.answer();
269 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800270 }
271
272 /**
273 * Instructs Telecomm to reject 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 reject said call.
Santos Cordone3d76ab2014-01-28 17:25:20 -0800276 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700277 void rejectCall(Call call) {
278 if (!mCalls.contains(call)) {
279 Log.i(this, "Request to reject a non-existent call %s", call);
Santos Cordon61d0f702014-02-19 02:52:23 -0800280 } else {
Santos Cordona56f2762014-03-24 15:55:53 -0700281 for (CallsManagerListener listener : mListeners) {
282 listener.onIncomingCallRejected(call);
283 }
Santos Cordon61d0f702014-02-19 02:52:23 -0800284 call.reject();
285 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800286 }
287
288 /**
Ihab Awad74549ec2014-03-10 15:33:25 -0700289 * Instructs Telecomm to play the specified DTMF tone within the specified call.
290 *
Ihab Awad74549ec2014-03-10 15:33:25 -0700291 * @param digit The DTMF digit to play.
292 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700293 void playDtmfTone(Call call, char digit) {
294 if (!mCalls.contains(call)) {
295 Log.i(this, "Request to play DTMF in a non-existent call %s", call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700296 } else {
297 call.playDtmfTone(digit);
298 }
299 }
300
301 /**
302 * Instructs Telecomm to stop the currently playing DTMF tone, if any.
Ihab Awad74549ec2014-03-10 15:33:25 -0700303 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700304 void stopDtmfTone(Call call) {
305 if (!mCalls.contains(call)) {
306 Log.i(this, "Request to stop DTMF in a non-existent call %s", call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700307 } else {
308 call.stopDtmfTone();
309 }
310 }
311
312 /**
313 * Instructs Telecomm to continue the current post-dial DTMF string, if any.
Ihab Awad74549ec2014-03-10 15:33:25 -0700314 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700315 void postDialContinue(Call call) {
316 if (!mCalls.contains(call)) {
317 Log.i(this, "Request to continue post-dial string in a non-existent call %s", call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700318 } else {
319 // TODO(ihab): Implement this from this level on downwards
320 // call.postDialContinue();
321 // Must play tones locally -- see DTMFTonePlayer.java in Telephony
322 }
323 }
324
325 /**
Santos Cordone3d76ab2014-01-28 17:25:20 -0800326 * Instructs Telecomm to disconnect the specified call. Intended to be invoked by the
327 * in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered by
328 * the user hitting the end-call button.
Santos Cordone3d76ab2014-01-28 17:25:20 -0800329 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700330 void disconnectCall(Call call) {
331 if (!mCalls.contains(call)) {
332 Log.w(this, "Unknown call (%s) asked to disconnect", call);
Santos Cordon049b7b62014-01-30 05:34:26 -0800333 } else {
334 call.disconnect();
335 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800336 }
Santos Cordon681663d2014-01-30 04:32:15 -0800337
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700338 /**
339 * Instructs Telecomm to put the specified call on hold. Intended to be invoked by the
340 * in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered by
341 * the user hitting the hold button during an active call.
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700342 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700343 void holdCall(Call call) {
344 if (!mCalls.contains(call)) {
345 Log.w(this, "Unknown call (%s) asked to be put on hold", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700346 } else {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700347 Log.d(this, "Putting call on hold: (%s)", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700348 call.hold();
349 }
350 }
351
352 /**
353 * Instructs Telecomm to release the specified call from hold. Intended to be invoked by
354 * the in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered
355 * by the user hitting the hold button during a held call.
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700356 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700357 void unholdCall(Call call) {
358 if (!mCalls.contains(call)) {
359 Log.w(this, "Unknown call (%s) asked to be removed from hold", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700360 } else {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700361 Log.d(this, "Removing call from hold: (%s)", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700362 call.unhold();
363 }
364 }
365
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700366 /** Called by the in-call UI to change the mute state. */
367 void mute(boolean shouldMute) {
368 mCallAudioManager.mute(shouldMute);
369 }
370
371 /**
372 * Called by the in-call UI to change the audio route, for example to change from earpiece to
373 * speaker phone.
374 */
375 void setAudioRoute(int route) {
376 mCallAudioManager.setAudioRoute(route);
377 }
378
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700379 void startHandoffForCall(Call originalCall) {
380 if (!mCalls.contains(originalCall)) {
381 Log.w(this, "Unknown call %s asked to be handed off", originalCall);
382 return;
383 }
384
385 for (Call handoffCall : mPendingHandoffCalls) {
386 if (handoffCall.getOriginalCall() == originalCall) {
387 Log.w(this, "Call %s is already being handed off, skipping", originalCall);
388 return;
389 }
390 }
391
392 // Create a new call to be placed in the background. If handoff is successful then the
393 // original call will live on but its state will be updated to the new call's state. In
394 // particular the original call's call service will be updated to the new call's call
395 // service.
396 Call tempCall = new Call(originalCall.getHandoffHandle(), originalCall.getContactInfo(),
397 originalCall.getGatewayInfo(), false);
398 tempCall.setOriginalCall(originalCall);
399 tempCall.setExtras(originalCall.getExtras());
400 tempCall.setCallServiceSelector(originalCall.getCallServiceSelector());
401 mPendingHandoffCalls.add(tempCall);
402 Log.d(this, "Placing handoff call");
403 mSwitchboard.placeOutgoingCall(tempCall);
404 }
405
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700406 /** Called when the audio state changes. */
407 void onAudioStateChanged(CallAudioState oldAudioState, CallAudioState newAudioState) {
408 Log.v(this, "onAudioStateChanged, audioState: %s -> %s", oldAudioState, newAudioState);
Santos Cordona56f2762014-03-24 15:55:53 -0700409 for (CallsManagerListener listener : mListeners) {
410 listener.onAudioStateChanged(oldAudioState, newAudioState);
411 }
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700412 }
413
Sailesh Nepale59bb192014-04-01 18:33:59 -0700414 void markCallAsRinging(Call call) {
415 setCallState(call, CallState.RINGING);
Santos Cordon681663d2014-01-30 04:32:15 -0800416 }
417
Sailesh Nepale59bb192014-04-01 18:33:59 -0700418 void markCallAsDialing(Call call) {
419 setCallState(call, CallState.DIALING);
Santos Cordon681663d2014-01-30 04:32:15 -0800420 }
421
Sailesh Nepale59bb192014-04-01 18:33:59 -0700422 void markCallAsActive(Call call) {
423 setCallState(call, CallState.ACTIVE);
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700424
425 if (mPendingHandoffCalls.contains(call)) {
426 completeHandoff(call);
427 }
Santos Cordon681663d2014-01-30 04:32:15 -0800428 }
429
Sailesh Nepale59bb192014-04-01 18:33:59 -0700430 void markCallAsOnHold(Call call) {
431 setCallState(call, CallState.ON_HOLD);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700432 }
433
Santos Cordon049b7b62014-01-30 05:34:26 -0800434 /**
435 * Marks the specified call as DISCONNECTED and notifies the in-call app. If this was the last
436 * live call, then also disconnect from the in-call controller.
437 *
Santos Cordon79ff2bc2014-03-27 15:31:27 -0700438 * @param disconnectCause The disconnect reason, see {@link android.telephony.DisconnectCause}.
439 * @param disconnectMessage Optional call-service-provided message about the disconnect.
Santos Cordon049b7b62014-01-30 05:34:26 -0800440 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700441 void markCallAsDisconnected(Call call, int disconnectCause, String disconnectMessage) {
442 call.setDisconnectCause(disconnectCause, disconnectMessage);
443 setCallState(call, CallState.DISCONNECTED);
444 removeCall(call);
Ben Gilada0d9f752014-02-26 11:49:03 -0800445 }
446
Sailesh Nepal6ab6fb72014-04-01 20:03:19 -0700447 void setHandoffInfo(Call call, Uri handle, Bundle extras) {
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700448 if (!mCalls.contains(call)) {
449 Log.w(this, "Unknown call (%s) asked to set handoff info", call);
450 return;
451 }
452
453 if (extras == null) {
454 call.setExtras(Bundle.EMPTY);
455 } else {
456 call.setExtras(extras);
457 }
458
459 Uri oldHandle = call.getHandoffHandle();
460 Log.v(this, "set handoff handle %s -> %s, for call: %s", oldHandle, handle, call);
461 if (!areUriEqual(oldHandle, handle)) {
462 call.setHandoffHandle(handle);
463 for (CallsManagerListener listener : mListeners) {
464 listener.onCallHandoffHandleChanged(call, oldHandle, handle);
465 }
466 }
Sailesh Nepal6ab6fb72014-04-01 20:03:19 -0700467 }
468
Ben Gilada0d9f752014-02-26 11:49:03 -0800469 /**
Santos Cordon4b2c1192014-03-19 18:15:38 -0700470 * Cleans up any calls currently associated with the specified call service when the
471 * call-service binder disconnects unexpectedly.
472 *
473 * @param callService The call service that disconnected.
474 */
475 void handleCallServiceDeath(CallServiceWrapper callService) {
476 Preconditions.checkNotNull(callService);
Sailesh Nepale59bb192014-04-01 18:33:59 -0700477 for (Call call : ImmutableList.copyOf(mCalls)) {
Santos Cordon4b2c1192014-03-19 18:15:38 -0700478 if (call.getCallService() == callService) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700479 markCallAsDisconnected(call, DisconnectCause.ERROR_UNSPECIFIED, null);
Santos Cordon4b2c1192014-03-19 18:15:38 -0700480 }
481 }
482 }
483
484 /**
Ben Gilada0d9f752014-02-26 11:49:03 -0800485 * Adds the specified call to the main list of live calls.
486 *
487 * @param call The call to add.
488 */
489 private void addCall(Call call) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700490 mCalls.add(call);
Santos Cordona56f2762014-03-24 15:55:53 -0700491 for (CallsManagerListener listener : mListeners) {
492 listener.onCallAdded(call);
493 }
Sailesh Nepal810735e2014-03-18 18:15:46 -0700494 updateForegroundCall();
Ben Gilada0d9f752014-02-26 11:49:03 -0800495 }
496
Sailesh Nepal810735e2014-03-18 18:15:46 -0700497 private void removeCall(Call call) {
498 call.clearCallService();
Sailesh Nepale59bb192014-04-01 18:33:59 -0700499 call.clearCallServiceSelector();
500
501 boolean shouldNotify = false;
502 if (mCalls.contains(call)) {
503 mCalls.remove(call);
504 shouldNotify = true;
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700505 cleanUpHandoffCallsForOriginalCall(call);
506 } else if (mPendingHandoffCalls.contains(call)) {
507 Log.v(this, "silently removing handoff call %s", call);
508 mPendingHandoffCalls.remove(call);
Santos Cordona56f2762014-03-24 15:55:53 -0700509 }
Ben Gilada0d9f752014-02-26 11:49:03 -0800510
Sailesh Nepale59bb192014-04-01 18:33:59 -0700511 // Only broadcast changes for calls that are being tracked.
512 if (shouldNotify) {
513 for (CallsManagerListener listener : mListeners) {
514 listener.onCallRemoved(call);
515 }
516 updateForegroundCall();
Sailesh Nepal810735e2014-03-18 18:15:46 -0700517 }
518 }
Evan Charlton5c670a92014-03-06 14:58:20 -0800519
Sailesh Nepal810735e2014-03-18 18:15:46 -0700520 /**
Santos Cordon1ae2b852014-03-19 03:03:10 -0700521 * Sets the specified state on the specified call.
Sailesh Nepal810735e2014-03-18 18:15:46 -0700522 *
523 * @param call The call.
524 * @param newState The new state of the call.
525 */
526 private void setCallState(Call call, CallState newState) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700527 Preconditions.checkNotNull(newState);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700528 CallState oldState = call.getState();
529 if (newState != oldState) {
530 // Unfortunately, in the telephony world the radio is king. So if the call notifies
531 // us that the call is in a particular state, we allow it even if it doesn't make
532 // sense (e.g., ACTIVE -> RINGING).
533 // TODO(santoscordon): Consider putting a stop to the above and turning CallState
534 // into a well-defined state machine.
535 // TODO(santoscordon): Define expected state transitions here, and log when an
536 // unexpected transition occurs.
537 call.setState(newState);
538
539 // Only broadcast state change for calls that are being tracked.
Sailesh Nepale59bb192014-04-01 18:33:59 -0700540 if (mCalls.contains(call)) {
Santos Cordona56f2762014-03-24 15:55:53 -0700541 for (CallsManagerListener listener : mListeners) {
542 listener.onCallStateChanged(call, oldState, newState);
543 }
Sailesh Nepal810735e2014-03-18 18:15:46 -0700544 updateForegroundCall();
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800545 }
546 }
547 }
548
549 /**
Sailesh Nepal810735e2014-03-18 18:15:46 -0700550 * Checks which call should be visible to the user and have audio focus.
Evan Charlton5c670a92014-03-06 14:58:20 -0800551 */
Sailesh Nepal810735e2014-03-18 18:15:46 -0700552 private void updateForegroundCall() {
553 Call newForegroundCall = null;
Sailesh Nepale59bb192014-04-01 18:33:59 -0700554 for (Call call : mCalls) {
Sailesh Nepal810735e2014-03-18 18:15:46 -0700555 // Incoming ringing calls have priority.
556 if (call.getState() == CallState.RINGING) {
557 newForegroundCall = call;
Evan Charlton5c670a92014-03-06 14:58:20 -0800558 break;
Sailesh Nepal810735e2014-03-18 18:15:46 -0700559 }
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700560 if (call.isAlive()) {
Sailesh Nepal810735e2014-03-18 18:15:46 -0700561 newForegroundCall = call;
562 // Don't break in case there's a ringing call that has priority.
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800563 }
564 }
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800565
Sailesh Nepal810735e2014-03-18 18:15:46 -0700566 if (newForegroundCall != mForegroundCall) {
567 Call oldForegroundCall = mForegroundCall;
568 mForegroundCall = newForegroundCall;
569
Santos Cordona56f2762014-03-24 15:55:53 -0700570 for (CallsManagerListener listener : mListeners) {
571 listener.onForegroundCallChanged(oldForegroundCall, mForegroundCall);
572 }
Santos Cordon681663d2014-01-30 04:32:15 -0800573 }
574 }
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700575
576 private void completeHandoff(Call handoffCall) {
577 Call originalCall = handoffCall.getOriginalCall();
578 Log.v(this, "complete handoff, %s -> %s", handoffCall, originalCall);
579
580 // Disconnect.
581 originalCall.disconnect();
582
583 // Synchronize.
584 originalCall.setCallService(handoffCall.getCallService());
585 setCallState(originalCall, handoffCall.getState());
586
587 // Remove the transient handoff call object (don't disconnect because the call is still
588 // live).
589 removeCall(handoffCall);
590
591 // Force the foreground call changed notification to be sent.
592 for (CallsManagerListener listener : mListeners) {
593 listener.onForegroundCallChanged(mForegroundCall, mForegroundCall);
594 }
595 }
596
597 /** Makes sure there are no dangling handoff calls. */
598 private void cleanUpHandoffCallsForOriginalCall(Call originalCall) {
599 for (Call handoffCall : ImmutableList.copyOf((mPendingHandoffCalls))) {
600 if (handoffCall.getOriginalCall() == originalCall) {
601 Log.d(this, "cancelling handoff call %s for originalCall: %s", handoffCall,
602 originalCall);
603 if (handoffCall.getState() == CallState.NEW) {
604 handoffCall.abort();
605 handoffCall.setState(CallState.ABORTED);
606 } else {
607 handoffCall.disconnect();
608 handoffCall.setState(CallState.DISCONNECTED);
609 }
610 removeCall(handoffCall);
611 }
612 }
613 }
614
615 private static boolean areUriEqual(Uri handle1, Uri handle2) {
616 if (handle1 == null) {
617 return handle2 == null;
618 }
619 return handle1.equals(handle2);
620 }
Ben Gilad9f2bed32013-12-12 17:43:26 -0800621}