blob: 92c247e53236d69a686b9a6e247ff41d65b28d56 [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 Nepal1cecc7c2014-03-10 17:03:08 -070019import android.content.Context;
Sailesh Nepalce704b92014-03-17 18:31:43 -070020import android.net.Uri;
Evan Charltona05805b2014-03-05 08:21:46 -080021import android.os.Bundle;
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;
Santos Cordon8e8b8d22013-12-19 14:14:05 -080025
Sailesh Nepala439e1b2014-03-11 18:19:58 -070026import com.android.internal.telecomm.ICallService;
Santos Cordon681663d2014-01-30 04:32:15 -080027import com.google.common.base.Preconditions;
28import com.google.common.base.Strings;
Sailesh Nepal810735e2014-03-18 18:15:46 -070029import com.google.common.collect.ImmutableCollection;
30import com.google.common.collect.ImmutableList;
Santos Cordon8e8b8d22013-12-19 14:14:05 -080031import com.google.common.collect.Lists;
Santos Cordon681663d2014-01-30 04:32:15 -080032import com.google.common.collect.Maps;
Ben Gilad9f2bed32013-12-12 17:43:26 -080033
Ben Gilad9f2bed32013-12-12 17:43:26 -080034import java.util.List;
Santos Cordon681663d2014-01-30 04:32:15 -080035import java.util.Map;
Ben Gilad9f2bed32013-12-12 17:43:26 -080036
Ben Giladdd8c6082013-12-30 14:44:08 -080037/**
38 * Singleton.
39 *
40 * NOTE(gilad): by design most APIs are package private, use the relevant adapter/s to allow
41 * access from other packages specifically refraining from passing the CallsManager instance
42 * beyond the com.android.telecomm package boundary.
43 */
Santos Cordon8e8b8d22013-12-19 14:14:05 -080044public final class CallsManager {
Ben Gilada0d9f752014-02-26 11:49:03 -080045
Sailesh Nepal810735e2014-03-18 18:15:46 -070046 interface CallsManagerListener {
47 void onCallAdded(Call call);
48 void onCallRemoved(Call call);
49 void onCallStateChanged(Call call, CallState oldState, CallState newState);
50 void onIncomingCallAnswered(Call call);
51 void onIncomingCallRejected(Call call);
52 void onForegroundCallChanged(Call oldForegroundCall, Call newForegroundCall);
53 }
54
Santos Cordon8e8b8d22013-12-19 14:14:05 -080055 private static final CallsManager INSTANCE = new CallsManager();
Ben Gilad9f2bed32013-12-12 17:43:26 -080056
Santos Cordone3d76ab2014-01-28 17:25:20 -080057 private final Switchboard mSwitchboard;
58
Santos Cordon8e8b8d22013-12-19 14:14:05 -080059 /**
Santos Cordon681663d2014-01-30 04:32:15 -080060 * The main call repository. Keeps an instance of all live calls keyed by call ID. New incoming
61 * and outgoing calls are added to the map and removed when the calls move to the disconnected
62 * state.
63 * TODO(santoscordon): Add new CallId class and use it in place of String.
64 */
65 private final Map<String, Call> mCalls = Maps.newHashMap();
66
67 /**
Sailesh Nepal810735e2014-03-18 18:15:46 -070068 * The call the user is currently interacting with. This is the call that should have audio
69 * focus and be visible in the in-call UI.
Santos Cordon4e9fffe2014-03-04 18:13:41 -080070 */
Sailesh Nepal810735e2014-03-18 18:15:46 -070071 private Call mForegroundCall;
Santos Cordon4e9fffe2014-03-04 18:13:41 -080072
Sailesh Nepal810735e2014-03-18 18:15:46 -070073 private final CallsManagerListener mCallLogManager;
Ben Gilad9f2bed32013-12-12 17:43:26 -080074
Sailesh Nepal810735e2014-03-18 18:15:46 -070075 private final CallsManagerListener mPhoneStateBroadcaster;
Ben Gilad9f2bed32013-12-12 17:43:26 -080076
Sailesh Nepal810735e2014-03-18 18:15:46 -070077 private final CallsManagerListener mCallAudioManager;
78
79 private final CallsManagerListener mInCallController;
80
Evan Charltonf02e9882014-03-06 12:54:52 -080081 private final List<OutgoingCallValidator> mOutgoingCallValidators = Lists.newArrayList();
Ben Gilad9f2bed32013-12-12 17:43:26 -080082
Evan Charltonf02e9882014-03-06 12:54:52 -080083 private final List<IncomingCallValidator> mIncomingCallValidators = Lists.newArrayList();
Ben Gilad9f2bed32013-12-12 17:43:26 -080084
Santos Cordon8e8b8d22013-12-19 14:14:05 -080085 /**
Ben Gilad8bdaa462014-02-05 12:53:19 -080086 * Initializes the required Telecomm components.
Santos Cordon8e8b8d22013-12-19 14:14:05 -080087 */
88 private CallsManager() {
Santos Cordon6242b132014-02-07 16:24:42 -080089 mSwitchboard = new Switchboard(this);
Yorke Leef98fb572014-03-05 10:56:55 -080090 mCallLogManager = new CallLogManager(TelecommApp.getInstance());
Sailesh Nepal810735e2014-03-18 18:15:46 -070091 mPhoneStateBroadcaster = new PhoneStateBroadcaster();
92 mCallAudioManager = new CallAudioManager();
93 mInCallController = new InCallController();
Santos Cordon8e8b8d22013-12-19 14:14:05 -080094 }
95
Ben Gilada0d9f752014-02-26 11:49:03 -080096 static CallsManager getInstance() {
97 return INSTANCE;
98 }
99
Sailesh Nepal810735e2014-03-18 18:15:46 -0700100 ImmutableCollection<Call> getCalls() {
101 return ImmutableList.copyOf(mCalls.values());
102 }
103
104 Call getForegroundCall() {
105 return mForegroundCall;
106 }
107
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800108 /**
Santos Cordon493e8f22014-02-19 03:15:12 -0800109 * Starts the incoming call sequence by having switchboard gather more information about the
110 * specified call; using the specified call service descriptor. Upon success, execution returns
111 * to {@link #handleSuccessfulIncomingCall} to start the in-call UI.
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800112 *
Ben Giladc5b22692014-02-18 20:03:22 -0800113 * @param descriptor The descriptor of the call service to use for this incoming call.
Evan Charltona05805b2014-03-05 08:21:46 -0800114 * @param extras The optional extras Bundle passed with the intent used for the incoming call.
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800115 */
Evan Charltona05805b2014-03-05 08:21:46 -0800116 void processIncomingCallIntent(CallServiceDescriptor descriptor, Bundle extras) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800117 Log.d(this, "processIncomingCallIntent");
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800118 // Create a call with no handle. Eventually, switchboard will update the call with
119 // additional information from the call service, but for now we just need one to pass around
120 // with a unique call ID.
Sailesh Nepal810735e2014-03-18 18:15:46 -0700121 Call call = new Call(true /* isIncoming */);
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800122
Evan Charltona05805b2014-03-05 08:21:46 -0800123 mSwitchboard.retrieveIncomingCall(call, descriptor, extras);
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800124 }
125
126 /**
Ben Gilada0d9f752014-02-26 11:49:03 -0800127 * Validates the specified call and, upon no objection to connect it, adds the new call to the
128 * list of live calls. Also notifies the in-call app so the user can answer or reject the call.
129 *
130 * @param call The new incoming call.
Sailesh Nepal810735e2014-03-18 18:15:46 -0700131 * @param callInfo The details of the call.
Ben Gilada0d9f752014-02-26 11:49:03 -0800132 */
Sailesh Nepal810735e2014-03-18 18:15:46 -0700133 void handleSuccessfulIncomingCall(Call call, CallInfo callInfo) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800134 Log.d(this, "handleSuccessfulIncomingCall");
Sailesh Nepal810735e2014-03-18 18:15:46 -0700135 Preconditions.checkState(callInfo.getState() == CallState.RINGING);
Ben Gilada0d9f752014-02-26 11:49:03 -0800136
Sailesh Nepalce704b92014-03-17 18:31:43 -0700137 Uri handle = call.getHandle();
Ben Gilada0d9f752014-02-26 11:49:03 -0800138 ContactInfo contactInfo = call.getContactInfo();
139 for (IncomingCallValidator validator : mIncomingCallValidators) {
140 if (!validator.isValid(handle, contactInfo)) {
141 // TODO(gilad): Consider displaying an error message.
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800142 Log.i(this, "Dropping restricted incoming call");
Ben Gilada0d9f752014-02-26 11:49:03 -0800143 return;
144 }
145 }
146
147 // No objection to accept the incoming call, proceed with potentially connecting it (based
148 // on the user's action, or lack thereof).
Sailesh Nepal810735e2014-03-18 18:15:46 -0700149 call.setHandle(callInfo.getHandle());
150 setCallState(call, callInfo.getState());
Ben Gilada0d9f752014-02-26 11:49:03 -0800151 addCall(call);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700152 }
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800153
Sailesh Nepal810735e2014-03-18 18:15:46 -0700154 /**
155 * Called when an incoming call was not connected.
156 *
157 * @param call The incoming call.
158 */
159 void handleUnsuccessfulIncomingCall(Call call) {
160 setCallState(call, CallState.DISCONNECTED);
Ben Gilada0d9f752014-02-26 11:49:03 -0800161 }
162
163 /**
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800164 * Attempts to issue/connect the specified call. From an (arbitrary) application standpoint,
165 * all that is required to initiate this flow is to fire either of the CALL, CALL_PRIVILEGED,
166 * and CALL_EMERGENCY intents. These are listened to by CallActivity.java which then invokes
167 * this method.
168 *
169 * @param handle The handle to dial.
170 * @param contactInfo Information about the entity being called.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800171 */
Sailesh Nepalce704b92014-03-17 18:31:43 -0700172 void processOutgoingCallIntent(Uri handle, ContactInfo contactInfo) {
Ben Gilad8bdaa462014-02-05 12:53:19 -0800173 for (OutgoingCallValidator validator : mOutgoingCallValidators) {
Ben Gilada0d9f752014-02-26 11:49:03 -0800174 if (!validator.isValid(handle, contactInfo)) {
175 // TODO(gilad): Display an error message.
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800176 Log.i(this, "Dropping restricted outgoing call.");
Ben Gilada0d9f752014-02-26 11:49:03 -0800177 return;
178 }
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800179 }
180
181 // No objection to issue the call, proceed with trying to put it through.
Sailesh Nepal810735e2014-03-18 18:15:46 -0700182 Call call = new Call(handle, contactInfo, false /* isIncoming */);
183 setCallState(call, CallState.DIALING);
184 addCall(call);
Santos Cordonc195e362014-02-11 17:05:31 -0800185 mSwitchboard.placeOutgoingCall(call);
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800186 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800187
188 /**
Sailesh Nepal810735e2014-03-18 18:15:46 -0700189 * Called when a call service acknowledges that it can place a call.
Santos Cordon681663d2014-01-30 04:32:15 -0800190 *
191 * @param call The new outgoing call.
192 */
193 void handleSuccessfulOutgoingCall(Call call) {
Sailesh Nepal810735e2014-03-18 18:15:46 -0700194 Log.v(this, "handleSuccessfulOutgoingCall, %s", call);
Santos Cordon681663d2014-01-30 04:32:15 -0800195 }
196
197 /**
Sailesh Nepal810735e2014-03-18 18:15:46 -0700198 * Called when an outgoing call was not placed.
Yorke Leef98fb572014-03-05 10:56:55 -0800199 *
Sailesh Nepal810735e2014-03-18 18:15:46 -0700200 * @param call The outgoing call.
201 * @param isAborted True if the call was unsuccessful because it was aborted.
Yorke Leef98fb572014-03-05 10:56:55 -0800202 */
Sailesh Nepal810735e2014-03-18 18:15:46 -0700203 void handleUnsuccessfulOutgoingCall(Call call, boolean isAborted) {
204 Log.v(this, "handleAbortedOutgoingCall, call: %s, isAborted: %b", call, isAborted);
205 if (isAborted) {
206 call.abort();
207 setCallState(call, CallState.ABORTED);
208 } else {
209 setCallState(call, CallState.DISCONNECTED);
210 }
211 removeCall(call);
Yorke Leef98fb572014-03-05 10:56:55 -0800212 }
213
214 /**
Santos Cordone3d76ab2014-01-28 17:25:20 -0800215 * Instructs Telecomm to answer the specified call. Intended to be invoked by the in-call
216 * app through {@link InCallAdapter} after Telecomm notifies it of an incoming call followed by
217 * the user opting to answer said call.
218 *
219 * @param callId The ID of the call.
220 */
221 void answerCall(String callId) {
Santos Cordon61d0f702014-02-19 02:52:23 -0800222 Call call = mCalls.get(callId);
223 if (call == null) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800224 Log.i(this, "Request to answer a non-existent call %s", callId);
Santos Cordon61d0f702014-02-19 02:52:23 -0800225 } else {
Sailesh Nepal810735e2014-03-18 18:15:46 -0700226 mCallLogManager.onIncomingCallAnswered(call);
227 mPhoneStateBroadcaster.onIncomingCallAnswered(call);
228 mCallAudioManager.onIncomingCallAnswered(call);
229 mInCallController.onIncomingCallAnswered(call);
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800230
Santos Cordon61d0f702014-02-19 02:52:23 -0800231 // We do not update the UI until we get confirmation of the answer() through
232 // {@link #markCallAsActive}. However, if we ever change that to look more responsive,
233 // then we need to make sure we add a timeout for the answer() in case the call never
234 // comes out of RINGING.
235 call.answer();
236 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800237 }
238
239 /**
240 * Instructs Telecomm to reject the specified call. Intended to be invoked by the in-call
241 * app through {@link InCallAdapter} after Telecomm notifies it of an incoming call followed by
242 * the user opting to reject said call.
243 *
244 * @param callId The ID of the call.
245 */
246 void rejectCall(String callId) {
Santos Cordon61d0f702014-02-19 02:52:23 -0800247 Call call = mCalls.get(callId);
248 if (call == null) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800249 Log.i(this, "Request to reject a non-existent call %s", callId);
Santos Cordon61d0f702014-02-19 02:52:23 -0800250 } else {
Sailesh Nepal810735e2014-03-18 18:15:46 -0700251 mCallLogManager.onIncomingCallRejected(call);
252 mPhoneStateBroadcaster.onIncomingCallRejected(call);
253 mCallAudioManager.onIncomingCallRejected(call);
254 mInCallController.onIncomingCallRejected(call);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700255
Santos Cordon61d0f702014-02-19 02:52:23 -0800256 call.reject();
257 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800258 }
259
260 /**
261 * Instructs Telecomm to disconnect the specified call. Intended to be invoked by the
262 * in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered by
263 * the user hitting the end-call button.
264 *
265 * @param callId The ID of the call.
266 */
267 void disconnectCall(String callId) {
Santos Cordon049b7b62014-01-30 05:34:26 -0800268 Call call = mCalls.get(callId);
269 if (call == null) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800270 Log.w(this, "Unknown call (%s) asked to disconnect", callId);
Santos Cordon049b7b62014-01-30 05:34:26 -0800271 } else {
272 call.disconnect();
273 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800274 }
Santos Cordon681663d2014-01-30 04:32:15 -0800275
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700276 /**
277 * Instructs Telecomm to put the specified call on hold. Intended to be invoked by the
278 * in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered by
279 * the user hitting the hold button during an active call.
280 *
281 * @param callId The ID of the call.
282 */
283 void holdCall(String callId) {
284 Call call = mCalls.get(callId);
285 if (call == null) {
286 Log.w(this, "Unknown call (%s) asked to be put on hold", callId);
287 } else {
288 Log.d(this, "Putting call on hold: (%s)", callId);
289 call.hold();
290 }
291 }
292
293 /**
294 * Instructs Telecomm to release the specified call from hold. Intended to be invoked by
295 * the in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered
296 * by the user hitting the hold button during a held call.
297 *
298 * @param callId The ID of the call
299 */
300 void unholdCall(String callId) {
301 Call call = mCalls.get(callId);
302 if (call == null) {
303 Log.w(this, "Unknown call (%s) asked to be removed from hold", callId);
304 } else {
305 Log.d(this, "Removing call from hold: (%s)", callId);
306 call.unhold();
307 }
308 }
309
Santos Cordon681663d2014-01-30 04:32:15 -0800310 void markCallAsRinging(String callId) {
311 setCallState(callId, CallState.RINGING);
312 }
313
314 void markCallAsDialing(String callId) {
315 setCallState(callId, CallState.DIALING);
316 }
317
318 void markCallAsActive(String callId) {
319 setCallState(callId, CallState.ACTIVE);
320 }
321
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700322 void markCallAsOnHold(String callId) {
323 setCallState(callId, CallState.ON_HOLD);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700324 }
325
Santos Cordon049b7b62014-01-30 05:34:26 -0800326 /**
327 * Marks the specified call as DISCONNECTED and notifies the in-call app. If this was the last
328 * live call, then also disconnect from the in-call controller.
329 *
330 * @param callId The ID of the call.
331 */
Santos Cordon681663d2014-01-30 04:32:15 -0800332 void markCallAsDisconnected(String callId) {
333 setCallState(callId, CallState.DISCONNECTED);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700334 removeCall(mCalls.remove(callId));
Ben Gilada0d9f752014-02-26 11:49:03 -0800335 }
336
337 /**
Santos Cordon1ae2b852014-03-19 03:03:10 -0700338 * @return True if there exists a call with the specific state.
339 */
340 boolean hasCallWithState(CallState... states) {
341 for (Call call : mCalls.values()) {
342 for (CallState state : states) {
343 if (call.getState() == state) {
344 return true;
345 }
346 }
347 }
348
349 return false;
350 }
351
352 /**
Ben Gilada0d9f752014-02-26 11:49:03 -0800353 * Adds the specified call to the main list of live calls.
354 *
355 * @param call The call to add.
356 */
357 private void addCall(Call call) {
358 mCalls.put(call.getId(), call);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700359
360 mCallLogManager.onCallAdded(call);
361 mPhoneStateBroadcaster.onCallAdded(call);
362 mCallAudioManager.onCallAdded(call);
363 mInCallController.onCallAdded(call);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700364 updateForegroundCall();
Ben Gilada0d9f752014-02-26 11:49:03 -0800365 }
366
Sailesh Nepal810735e2014-03-18 18:15:46 -0700367 private void removeCall(Call call) {
368 call.clearCallService();
369
370 mCallLogManager.onCallRemoved(call);
371 mPhoneStateBroadcaster.onCallRemoved(call);
372 mCallAudioManager.onCallRemoved(call);
373 mInCallController.onCallRemoved(call);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700374 updateForegroundCall();
Ben Gilada0d9f752014-02-26 11:49:03 -0800375 }
376
377 /**
Santos Cordon1ae2b852014-03-19 03:03:10 -0700378 * Sets the specified state on the specified call.
Santos Cordon681663d2014-01-30 04:32:15 -0800379 *
380 * @param callId The ID of the call to update.
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800381 * @param newState The new state of the call.
Santos Cordon681663d2014-01-30 04:32:15 -0800382 */
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800383 private void setCallState(String callId, CallState newState) {
Santos Cordon681663d2014-01-30 04:32:15 -0800384 Preconditions.checkState(!Strings.isNullOrEmpty(callId));
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800385 Preconditions.checkNotNull(newState);
Santos Cordon681663d2014-01-30 04:32:15 -0800386
387 Call call = mCalls.get(callId);
388 if (call == null) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800389 Log.w(this, "Call %s was not found while attempting to update the state to %s.",
390 callId, newState );
Santos Cordon681663d2014-01-30 04:32:15 -0800391 } else {
Sailesh Nepal810735e2014-03-18 18:15:46 -0700392 setCallState(call, newState);
393 }
394 }
Evan Charlton5c670a92014-03-06 14:58:20 -0800395
Sailesh Nepal810735e2014-03-18 18:15:46 -0700396 /**
Santos Cordon1ae2b852014-03-19 03:03:10 -0700397 * Sets the specified state on the specified call.
Sailesh Nepal810735e2014-03-18 18:15:46 -0700398 *
399 * @param call The call.
400 * @param newState The new state of the call.
401 */
402 private void setCallState(Call call, CallState newState) {
403 CallState oldState = call.getState();
404 if (newState != oldState) {
405 // Unfortunately, in the telephony world the radio is king. So if the call notifies
406 // us that the call is in a particular state, we allow it even if it doesn't make
407 // sense (e.g., ACTIVE -> RINGING).
408 // TODO(santoscordon): Consider putting a stop to the above and turning CallState
409 // into a well-defined state machine.
410 // TODO(santoscordon): Define expected state transitions here, and log when an
411 // unexpected transition occurs.
412 call.setState(newState);
413
414 // Only broadcast state change for calls that are being tracked.
415 if (mCalls.containsKey(call.getId())) {
416 mCallLogManager.onCallStateChanged(call, oldState, newState);
417 mPhoneStateBroadcaster.onCallStateChanged(call, oldState, newState);
418 mCallAudioManager.onCallStateChanged(call, oldState, newState);
419 mInCallController.onCallStateChanged(call, oldState, newState);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700420 updateForegroundCall();
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800421 }
422 }
423 }
424
425 /**
Sailesh Nepal810735e2014-03-18 18:15:46 -0700426 * Checks which call should be visible to the user and have audio focus.
Evan Charlton5c670a92014-03-06 14:58:20 -0800427 */
Sailesh Nepal810735e2014-03-18 18:15:46 -0700428 private void updateForegroundCall() {
429 Call newForegroundCall = null;
430 for (Call call : mCalls.values()) {
431 // Incoming ringing calls have priority.
432 if (call.getState() == CallState.RINGING) {
433 newForegroundCall = call;
Evan Charlton5c670a92014-03-06 14:58:20 -0800434 break;
Sailesh Nepal810735e2014-03-18 18:15:46 -0700435 }
436 if (call.getState() == CallState.ACTIVE) {
437 newForegroundCall = call;
438 // Don't break in case there's a ringing call that has priority.
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800439 }
440 }
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800441
Sailesh Nepal810735e2014-03-18 18:15:46 -0700442 if (newForegroundCall != mForegroundCall) {
443 Call oldForegroundCall = mForegroundCall;
444 mForegroundCall = newForegroundCall;
445
446 mCallLogManager.onForegroundCallChanged(oldForegroundCall, mForegroundCall);
447 mPhoneStateBroadcaster.onForegroundCallChanged(oldForegroundCall, mForegroundCall);
448 mCallAudioManager.onForegroundCallChanged(oldForegroundCall, mForegroundCall);
449 mInCallController.onForegroundCallChanged(oldForegroundCall, mForegroundCall);
Santos Cordon681663d2014-01-30 04:32:15 -0800450 }
451 }
Ben Gilad9f2bed32013-12-12 17:43:26 -0800452}