blob: 6daaba01d2cba2521d4363788ffa67b1b27810bb [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);
56 void onForegroundCallChanged(Call oldForegroundCall, Call newForegroundCall);
Sailesh Nepal6aca10a2014-03-24 16:11:02 -070057 void onAudioStateChanged(CallAudioState oldAudioState, CallAudioState newAudioState);
Sailesh Nepal810735e2014-03-18 18:15:46 -070058 }
59
Santos Cordon8e8b8d22013-12-19 14:14:05 -080060 private static final CallsManager INSTANCE = new CallsManager();
Ben Gilad9f2bed32013-12-12 17:43:26 -080061
Santos Cordone3d76ab2014-01-28 17:25:20 -080062 private final Switchboard mSwitchboard;
63
Santos Cordon8e8b8d22013-12-19 14:14:05 -080064 /**
Santos Cordon681663d2014-01-30 04:32:15 -080065 * The main call repository. Keeps an instance of all live calls keyed by call ID. New incoming
66 * and outgoing calls are added to the map and removed when the calls move to the disconnected
67 * state.
68 * TODO(santoscordon): Add new CallId class and use it in place of String.
69 */
70 private final Map<String, Call> mCalls = Maps.newHashMap();
71
72 /**
Sailesh Nepal810735e2014-03-18 18:15:46 -070073 * The call the user is currently interacting with. This is the call that should have audio
74 * focus and be visible in the in-call UI.
Santos Cordon4e9fffe2014-03-04 18:13:41 -080075 */
Sailesh Nepal810735e2014-03-18 18:15:46 -070076 private Call mForegroundCall;
Santos Cordon4e9fffe2014-03-04 18:13:41 -080077
Sailesh Nepal6aca10a2014-03-24 16:11:02 -070078 private final CallAudioManager mCallAudioManager;
Sailesh Nepal810735e2014-03-18 18:15:46 -070079
Santos Cordona56f2762014-03-24 15:55:53 -070080 private final Set<CallsManagerListener> mListeners = Sets.newHashSet();
Sailesh Nepal6aca10a2014-03-24 16:11:02 -070081
Evan Charltonf02e9882014-03-06 12:54:52 -080082 private final List<OutgoingCallValidator> mOutgoingCallValidators = Lists.newArrayList();
Ben Gilad9f2bed32013-12-12 17:43:26 -080083
Evan Charltonf02e9882014-03-06 12:54:52 -080084 private final List<IncomingCallValidator> mIncomingCallValidators = Lists.newArrayList();
Ben Gilad9f2bed32013-12-12 17:43:26 -080085
Santos Cordon8e8b8d22013-12-19 14:14:05 -080086 /**
Ben Gilad8bdaa462014-02-05 12:53:19 -080087 * Initializes the required Telecomm components.
Santos Cordon8e8b8d22013-12-19 14:14:05 -080088 */
89 private CallsManager() {
Santos Cordon6242b132014-02-07 16:24:42 -080090 mSwitchboard = new Switchboard(this);
Santos Cordona56f2762014-03-24 15:55:53 -070091
Sailesh Nepal810735e2014-03-18 18:15:46 -070092 mCallAudioManager = new CallAudioManager();
Santos Cordona56f2762014-03-24 15:55:53 -070093
94 mListeners.add(new CallLogManager(TelecommApp.getInstance()));
95 mListeners.add(new PhoneStateBroadcaster());
96 mListeners.add(new InCallController());
97 mListeners.add(new Ringer(mCallAudioManager));
98 mListeners.add(new RingbackPlayer(this, new InCallTonePlayer.Factory(mCallAudioManager)));
99 mListeners.add(mCallAudioManager);
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800100 }
101
Ben Gilada0d9f752014-02-26 11:49:03 -0800102 static CallsManager getInstance() {
103 return INSTANCE;
104 }
105
Sailesh Nepal810735e2014-03-18 18:15:46 -0700106 ImmutableCollection<Call> getCalls() {
107 return ImmutableList.copyOf(mCalls.values());
108 }
109
110 Call getForegroundCall() {
111 return mForegroundCall;
112 }
113
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700114 boolean hasEmergencyCall() {
115 for (Call call : mCalls.values()) {
116 if (call.isEmergencyCall()) {
117 return true;
118 }
119 }
120 return false;
121 }
122
123 CallAudioState getAudioState() {
124 return mCallAudioManager.getAudioState();
125 }
126
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800127 /**
Santos Cordon493e8f22014-02-19 03:15:12 -0800128 * Starts the incoming call sequence by having switchboard gather more information about the
129 * specified call; using the specified call service descriptor. Upon success, execution returns
130 * to {@link #handleSuccessfulIncomingCall} to start the in-call UI.
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800131 *
Ben Giladc5b22692014-02-18 20:03:22 -0800132 * @param descriptor The descriptor of the call service to use for this incoming call.
Evan Charltona05805b2014-03-05 08:21:46 -0800133 * @param extras The optional extras Bundle passed with the intent used for the incoming call.
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800134 */
Evan Charltona05805b2014-03-05 08:21:46 -0800135 void processIncomingCallIntent(CallServiceDescriptor descriptor, Bundle extras) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800136 Log.d(this, "processIncomingCallIntent");
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800137 // Create a call with no handle. Eventually, switchboard will update the call with
138 // additional information from the call service, but for now we just need one to pass around
139 // with a unique call ID.
Sailesh Nepal810735e2014-03-18 18:15:46 -0700140 Call call = new Call(true /* isIncoming */);
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800141
Evan Charltona05805b2014-03-05 08:21:46 -0800142 mSwitchboard.retrieveIncomingCall(call, descriptor, extras);
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800143 }
144
145 /**
Ben Gilada0d9f752014-02-26 11:49:03 -0800146 * Validates the specified call and, upon no objection to connect it, adds the new call to the
147 * list of live calls. Also notifies the in-call app so the user can answer or reject the call.
148 *
149 * @param call The new incoming call.
Sailesh Nepal810735e2014-03-18 18:15:46 -0700150 * @param callInfo The details of the call.
Ben Gilada0d9f752014-02-26 11:49:03 -0800151 */
Sailesh Nepal810735e2014-03-18 18:15:46 -0700152 void handleSuccessfulIncomingCall(Call call, CallInfo callInfo) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800153 Log.d(this, "handleSuccessfulIncomingCall");
Sailesh Nepal810735e2014-03-18 18:15:46 -0700154 Preconditions.checkState(callInfo.getState() == CallState.RINGING);
Ben Gilada0d9f752014-02-26 11:49:03 -0800155
Sailesh Nepalce704b92014-03-17 18:31:43 -0700156 Uri handle = call.getHandle();
Ben Gilada0d9f752014-02-26 11:49:03 -0800157 ContactInfo contactInfo = call.getContactInfo();
158 for (IncomingCallValidator validator : mIncomingCallValidators) {
159 if (!validator.isValid(handle, contactInfo)) {
160 // TODO(gilad): Consider displaying an error message.
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800161 Log.i(this, "Dropping restricted incoming call");
Ben Gilada0d9f752014-02-26 11:49:03 -0800162 return;
163 }
164 }
165
166 // No objection to accept the incoming call, proceed with potentially connecting it (based
167 // on the user's action, or lack thereof).
Sailesh Nepal810735e2014-03-18 18:15:46 -0700168 call.setHandle(callInfo.getHandle());
169 setCallState(call, callInfo.getState());
Ben Gilada0d9f752014-02-26 11:49:03 -0800170 addCall(call);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700171 }
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800172
Sailesh Nepal810735e2014-03-18 18:15:46 -0700173 /**
174 * Called when an incoming call was not connected.
175 *
176 * @param call The incoming call.
177 */
178 void handleUnsuccessfulIncomingCall(Call call) {
Santos Cordon79ff2bc2014-03-27 15:31:27 -0700179 // Incoming calls are not added unless they are successful. We set the state and disconnect
180 // cause just as a matter of good bookkeeping. We do not use the specific methods for
181 // setting those values so that we do not trigger CallsManagerListener events.
182 // TODO: Needs more specific disconnect error for this case.
183 call.setDisconnectCause(DisconnectCause.ERROR_UNSPECIFIED, null);
184 call.setState(CallState.DISCONNECTED);
Ben Gilada0d9f752014-02-26 11:49:03 -0800185 }
186
187 /**
Yorke Lee33501632014-03-17 19:24:12 -0700188 * Attempts to issue/connect the specified call.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800189 *
Yorke Lee33501632014-03-17 19:24:12 -0700190 * @param handle Handle to connect the call with.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800191 * @param contactInfo Information about the entity being called.
Yorke Lee33501632014-03-17 19:24:12 -0700192 * @param gatewayInfo Optional gateway information that can be used to route the call to the
193 * actual dialed handle via a gateway provider. May be null.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800194 */
Yorke Lee33501632014-03-17 19:24:12 -0700195 void placeOutgoingCall(Uri handle, ContactInfo contactInfo, GatewayInfo gatewayInfo) {
Ben Gilad8bdaa462014-02-05 12:53:19 -0800196 for (OutgoingCallValidator validator : mOutgoingCallValidators) {
Ben Gilada0d9f752014-02-26 11:49:03 -0800197 if (!validator.isValid(handle, contactInfo)) {
198 // TODO(gilad): Display an error message.
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800199 Log.i(this, "Dropping restricted outgoing call.");
Ben Gilada0d9f752014-02-26 11:49:03 -0800200 return;
201 }
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800202 }
203
Yorke Lee33501632014-03-17 19:24:12 -0700204 final Uri uriHandle = (gatewayInfo == null) ? handle : gatewayInfo.getGatewayHandle();
205
206 if (gatewayInfo == null) {
207 Log.i(this, "Creating a new outgoing call with handle: %s", Log.pii(uriHandle));
208 } else {
209 Log.i(this, "Creating a new outgoing call with gateway handle: %s, original handle: %s",
210 Log.pii(uriHandle), Log.pii(handle));
211 }
212 Call call = new Call(uriHandle, contactInfo, gatewayInfo, false /*isIncoming*/);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700213 setCallState(call, CallState.DIALING);
214 addCall(call);
Santos Cordonc195e362014-02-11 17:05:31 -0800215 mSwitchboard.placeOutgoingCall(call);
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800216 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800217
218 /**
Sailesh Nepal810735e2014-03-18 18:15:46 -0700219 * Called when a call service acknowledges that it can place a call.
Santos Cordon681663d2014-01-30 04:32:15 -0800220 *
221 * @param call The new outgoing call.
222 */
223 void handleSuccessfulOutgoingCall(Call call) {
Sailesh Nepal810735e2014-03-18 18:15:46 -0700224 Log.v(this, "handleSuccessfulOutgoingCall, %s", call);
Santos Cordon681663d2014-01-30 04:32:15 -0800225 }
226
227 /**
Sailesh Nepal810735e2014-03-18 18:15:46 -0700228 * Called when an outgoing call was not placed.
Yorke Leef98fb572014-03-05 10:56:55 -0800229 *
Sailesh Nepal810735e2014-03-18 18:15:46 -0700230 * @param call The outgoing call.
231 * @param isAborted True if the call was unsuccessful because it was aborted.
Yorke Leef98fb572014-03-05 10:56:55 -0800232 */
Sailesh Nepal810735e2014-03-18 18:15:46 -0700233 void handleUnsuccessfulOutgoingCall(Call call, boolean isAborted) {
234 Log.v(this, "handleAbortedOutgoingCall, call: %s, isAborted: %b", call, isAborted);
235 if (isAborted) {
236 call.abort();
237 setCallState(call, CallState.ABORTED);
Santos Cordon79ff2bc2014-03-27 15:31:27 -0700238 removeCall(call);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700239 } else {
Santos Cordon79ff2bc2014-03-27 15:31:27 -0700240 // TODO: Replace disconnect cause with more specific disconnect causes.
241 markCallAsDisconnected(call.getId(), DisconnectCause.ERROR_UNSPECIFIED, null);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700242 }
Yorke Leef98fb572014-03-05 10:56:55 -0800243 }
244
245 /**
Santos Cordone3d76ab2014-01-28 17:25:20 -0800246 * Instructs Telecomm to answer the specified call. Intended to be invoked by the in-call
247 * app through {@link InCallAdapter} after Telecomm notifies it of an incoming call followed by
248 * the user opting to answer said call.
249 *
250 * @param callId The ID of the call.
251 */
252 void answerCall(String callId) {
Santos Cordon61d0f702014-02-19 02:52:23 -0800253 Call call = mCalls.get(callId);
254 if (call == null) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800255 Log.i(this, "Request to answer a non-existent call %s", callId);
Santos Cordon61d0f702014-02-19 02:52:23 -0800256 } else {
Santos Cordona56f2762014-03-24 15:55:53 -0700257 for (CallsManagerListener listener : mListeners) {
258 listener.onIncomingCallAnswered(call);
259 }
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800260
Santos Cordon61d0f702014-02-19 02:52:23 -0800261 // We do not update the UI until we get confirmation of the answer() through
262 // {@link #markCallAsActive}. However, if we ever change that to look more responsive,
263 // then we need to make sure we add a timeout for the answer() in case the call never
264 // comes out of RINGING.
265 call.answer();
266 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800267 }
268
269 /**
270 * Instructs Telecomm to reject the specified call. Intended to be invoked by the in-call
271 * app through {@link InCallAdapter} after Telecomm notifies it of an incoming call followed by
272 * the user opting to reject said call.
273 *
274 * @param callId The ID of the call.
275 */
276 void rejectCall(String callId) {
Santos Cordon61d0f702014-02-19 02:52:23 -0800277 Call call = mCalls.get(callId);
278 if (call == null) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800279 Log.i(this, "Request to reject a non-existent call %s", callId);
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 }
Sailesh Nepal810735e2014-03-18 18:15:46 -0700284
Santos Cordon61d0f702014-02-19 02:52:23 -0800285 call.reject();
286 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800287 }
288
289 /**
290 * Instructs Telecomm to disconnect the specified call. Intended to be invoked by the
291 * in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered by
292 * the user hitting the end-call button.
293 *
294 * @param callId The ID of the call.
295 */
296 void disconnectCall(String callId) {
Santos Cordon049b7b62014-01-30 05:34:26 -0800297 Call call = mCalls.get(callId);
298 if (call == null) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800299 Log.w(this, "Unknown call (%s) asked to disconnect", callId);
Santos Cordon049b7b62014-01-30 05:34:26 -0800300 } else {
301 call.disconnect();
302 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800303 }
Santos Cordon681663d2014-01-30 04:32:15 -0800304
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700305 /**
306 * Instructs Telecomm to put the specified call on hold. Intended to be invoked by the
307 * in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered by
308 * the user hitting the hold button during an active call.
309 *
310 * @param callId The ID of the call.
311 */
312 void holdCall(String callId) {
313 Call call = mCalls.get(callId);
314 if (call == null) {
315 Log.w(this, "Unknown call (%s) asked to be put on hold", callId);
316 } else {
317 Log.d(this, "Putting call on hold: (%s)", callId);
318 call.hold();
319 }
320 }
321
322 /**
323 * Instructs Telecomm to release the specified call from hold. Intended to be invoked by
324 * the in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered
325 * by the user hitting the hold button during a held call.
326 *
327 * @param callId The ID of the call
328 */
329 void unholdCall(String callId) {
330 Call call = mCalls.get(callId);
331 if (call == null) {
332 Log.w(this, "Unknown call (%s) asked to be removed from hold", callId);
333 } else {
334 Log.d(this, "Removing call from hold: (%s)", callId);
335 call.unhold();
336 }
337 }
338
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700339 /** Called by the in-call UI to change the mute state. */
340 void mute(boolean shouldMute) {
341 mCallAudioManager.mute(shouldMute);
342 }
343
344 /**
345 * Called by the in-call UI to change the audio route, for example to change from earpiece to
346 * speaker phone.
347 */
348 void setAudioRoute(int route) {
349 mCallAudioManager.setAudioRoute(route);
350 }
351
352 /** Called when the audio state changes. */
353 void onAudioStateChanged(CallAudioState oldAudioState, CallAudioState newAudioState) {
354 Log.v(this, "onAudioStateChanged, audioState: %s -> %s", oldAudioState, newAudioState);
Santos Cordona56f2762014-03-24 15:55:53 -0700355 for (CallsManagerListener listener : mListeners) {
356 listener.onAudioStateChanged(oldAudioState, newAudioState);
357 }
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700358 }
359
Santos Cordon681663d2014-01-30 04:32:15 -0800360 void markCallAsRinging(String callId) {
361 setCallState(callId, CallState.RINGING);
362 }
363
364 void markCallAsDialing(String callId) {
365 setCallState(callId, CallState.DIALING);
366 }
367
368 void markCallAsActive(String callId) {
369 setCallState(callId, CallState.ACTIVE);
370 }
371
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700372 void markCallAsOnHold(String callId) {
373 setCallState(callId, CallState.ON_HOLD);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700374 }
375
Santos Cordon049b7b62014-01-30 05:34:26 -0800376 /**
377 * Marks the specified call as DISCONNECTED and notifies the in-call app. If this was the last
378 * live call, then also disconnect from the in-call controller.
379 *
380 * @param callId The ID of the call.
Santos Cordon79ff2bc2014-03-27 15:31:27 -0700381 * @param disconnectCause The disconnect reason, see {@link android.telephony.DisconnectCause}.
382 * @param disconnectMessage Optional call-service-provided message about the disconnect.
Santos Cordon049b7b62014-01-30 05:34:26 -0800383 */
Santos Cordon79ff2bc2014-03-27 15:31:27 -0700384 void markCallAsDisconnected(String callId, int disconnectCause, String disconnectMessage) {
385 Call call = mCalls.get(callId);
386 if (call != null) {
387 call.setDisconnectCause(disconnectCause, disconnectMessage);
388 setCallState(callId, CallState.DISCONNECTED);
389 removeCall(call);
390 }
Ben Gilada0d9f752014-02-26 11:49:03 -0800391 }
392
393 /**
Santos Cordon4b2c1192014-03-19 18:15:38 -0700394 * Cleans up any calls currently associated with the specified call service when the
395 * call-service binder disconnects unexpectedly.
396 *
397 * @param callService The call service that disconnected.
398 */
399 void handleCallServiceDeath(CallServiceWrapper callService) {
400 Preconditions.checkNotNull(callService);
401 for (Call call : ImmutableList.copyOf(mCalls.values())) {
402 if (call.getCallService() == callService) {
Santos Cordon79ff2bc2014-03-27 15:31:27 -0700403 markCallAsDisconnected(call.getId(), DisconnectCause.ERROR_UNSPECIFIED, null);
Santos Cordon4b2c1192014-03-19 18:15:38 -0700404 }
405 }
406 }
407
408 /**
Ben Gilada0d9f752014-02-26 11:49:03 -0800409 * Adds the specified call to the main list of live calls.
410 *
411 * @param call The call to add.
412 */
413 private void addCall(Call call) {
414 mCalls.put(call.getId(), call);
Santos Cordona56f2762014-03-24 15:55:53 -0700415 for (CallsManagerListener listener : mListeners) {
416 listener.onCallAdded(call);
417 }
Sailesh Nepal810735e2014-03-18 18:15:46 -0700418 updateForegroundCall();
Ben Gilada0d9f752014-02-26 11:49:03 -0800419 }
420
Sailesh Nepal810735e2014-03-18 18:15:46 -0700421 private void removeCall(Call call) {
Santos Cordon79ff2bc2014-03-27 15:31:27 -0700422 mCalls.remove(call.getId());
Sailesh Nepal810735e2014-03-18 18:15:46 -0700423 call.clearCallService();
Santos Cordona56f2762014-03-24 15:55:53 -0700424 for (CallsManagerListener listener : mListeners) {
425 listener.onCallRemoved(call);
426 }
Sailesh Nepal810735e2014-03-18 18:15:46 -0700427 updateForegroundCall();
Ben Gilada0d9f752014-02-26 11:49:03 -0800428 }
429
430 /**
Santos Cordon1ae2b852014-03-19 03:03:10 -0700431 * Sets the specified state on the specified call.
Santos Cordon681663d2014-01-30 04:32:15 -0800432 *
433 * @param callId The ID of the call to update.
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800434 * @param newState The new state of the call.
Santos Cordon681663d2014-01-30 04:32:15 -0800435 */
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800436 private void setCallState(String callId, CallState newState) {
Santos Cordon681663d2014-01-30 04:32:15 -0800437 Preconditions.checkState(!Strings.isNullOrEmpty(callId));
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800438 Preconditions.checkNotNull(newState);
Santos Cordon681663d2014-01-30 04:32:15 -0800439
440 Call call = mCalls.get(callId);
441 if (call == null) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800442 Log.w(this, "Call %s was not found while attempting to update the state to %s.",
443 callId, newState );
Santos Cordon681663d2014-01-30 04:32:15 -0800444 } else {
Sailesh Nepal810735e2014-03-18 18:15:46 -0700445 setCallState(call, newState);
446 }
447 }
Evan Charlton5c670a92014-03-06 14:58:20 -0800448
Sailesh Nepal810735e2014-03-18 18:15:46 -0700449 /**
Santos Cordon1ae2b852014-03-19 03:03:10 -0700450 * Sets the specified state on the specified call.
Sailesh Nepal810735e2014-03-18 18:15:46 -0700451 *
452 * @param call The call.
453 * @param newState The new state of the call.
454 */
455 private void setCallState(Call call, CallState newState) {
456 CallState oldState = call.getState();
457 if (newState != oldState) {
458 // Unfortunately, in the telephony world the radio is king. So if the call notifies
459 // us that the call is in a particular state, we allow it even if it doesn't make
460 // sense (e.g., ACTIVE -> RINGING).
461 // TODO(santoscordon): Consider putting a stop to the above and turning CallState
462 // into a well-defined state machine.
463 // TODO(santoscordon): Define expected state transitions here, and log when an
464 // unexpected transition occurs.
465 call.setState(newState);
466
467 // Only broadcast state change for calls that are being tracked.
468 if (mCalls.containsKey(call.getId())) {
Santos Cordona56f2762014-03-24 15:55:53 -0700469 for (CallsManagerListener listener : mListeners) {
470 listener.onCallStateChanged(call, oldState, newState);
471 }
Sailesh Nepal810735e2014-03-18 18:15:46 -0700472 updateForegroundCall();
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800473 }
474 }
475 }
476
477 /**
Sailesh Nepal810735e2014-03-18 18:15:46 -0700478 * Checks which call should be visible to the user and have audio focus.
Evan Charlton5c670a92014-03-06 14:58:20 -0800479 */
Sailesh Nepal810735e2014-03-18 18:15:46 -0700480 private void updateForegroundCall() {
481 Call newForegroundCall = null;
482 for (Call call : mCalls.values()) {
483 // Incoming ringing calls have priority.
484 if (call.getState() == CallState.RINGING) {
485 newForegroundCall = call;
Evan Charlton5c670a92014-03-06 14:58:20 -0800486 break;
Sailesh Nepal810735e2014-03-18 18:15:46 -0700487 }
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700488 if (call.isAlive()) {
Sailesh Nepal810735e2014-03-18 18:15:46 -0700489 newForegroundCall = call;
490 // Don't break in case there's a ringing call that has priority.
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800491 }
492 }
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800493
Sailesh Nepal810735e2014-03-18 18:15:46 -0700494 if (newForegroundCall != mForegroundCall) {
495 Call oldForegroundCall = mForegroundCall;
496 mForegroundCall = newForegroundCall;
497
Santos Cordona56f2762014-03-24 15:55:53 -0700498 for (CallsManagerListener listener : mListeners) {
499 listener.onForegroundCallChanged(oldForegroundCall, mForegroundCall);
500 }
Santos Cordon681663d2014-01-30 04:32:15 -0800501 }
502 }
Ben Gilad9f2bed32013-12-12 17:43:26 -0800503}