blob: 22d9ce6de04d1cbddce754e7d6c48dba0073fec5 [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 Nepal810735e2014-03-18 18:15:46 -070021import android.telecomm.CallInfo;
Ben Giladc5b22692014-02-18 20:03:22 -080022import android.telecomm.CallServiceDescriptor;
Santos Cordon681663d2014-01-30 04:32:15 -080023import android.telecomm.CallState;
Yorke Lee33501632014-03-17 19:24:12 -070024import android.telecomm.GatewayInfo;
Santos Cordon8e8b8d22013-12-19 14:14:05 -080025
Santos Cordon681663d2014-01-30 04:32:15 -080026import com.google.common.base.Preconditions;
27import com.google.common.base.Strings;
Sailesh Nepal810735e2014-03-18 18:15:46 -070028import com.google.common.collect.ImmutableCollection;
29import com.google.common.collect.ImmutableList;
Santos Cordon8e8b8d22013-12-19 14:14:05 -080030import com.google.common.collect.Lists;
Santos Cordon681663d2014-01-30 04:32:15 -080031import com.google.common.collect.Maps;
Ben Gilad9f2bed32013-12-12 17:43:26 -080032
Ben Gilad9f2bed32013-12-12 17:43:26 -080033import java.util.List;
Santos Cordon681663d2014-01-30 04:32:15 -080034import java.util.Map;
Ben Gilad9f2bed32013-12-12 17:43:26 -080035
Ben Giladdd8c6082013-12-30 14:44:08 -080036/**
37 * Singleton.
38 *
39 * NOTE(gilad): by design most APIs are package private, use the relevant adapter/s to allow
40 * access from other packages specifically refraining from passing the CallsManager instance
41 * beyond the com.android.telecomm package boundary.
42 */
Santos Cordon8e8b8d22013-12-19 14:14:05 -080043public final class CallsManager {
Ben Gilada0d9f752014-02-26 11:49:03 -080044
Sailesh Nepal810735e2014-03-18 18:15:46 -070045 interface CallsManagerListener {
46 void onCallAdded(Call call);
47 void onCallRemoved(Call call);
48 void onCallStateChanged(Call call, CallState oldState, CallState newState);
49 void onIncomingCallAnswered(Call call);
50 void onIncomingCallRejected(Call call);
51 void onForegroundCallChanged(Call oldForegroundCall, Call newForegroundCall);
52 }
53
Santos Cordon8e8b8d22013-12-19 14:14:05 -080054 private static final CallsManager INSTANCE = new CallsManager();
Ben Gilad9f2bed32013-12-12 17:43:26 -080055
Santos Cordone3d76ab2014-01-28 17:25:20 -080056 private final Switchboard mSwitchboard;
57
Santos Cordon8e8b8d22013-12-19 14:14:05 -080058 /**
Santos Cordon681663d2014-01-30 04:32:15 -080059 * The main call repository. Keeps an instance of all live calls keyed by call ID. New incoming
60 * and outgoing calls are added to the map and removed when the calls move to the disconnected
61 * state.
62 * TODO(santoscordon): Add new CallId class and use it in place of String.
63 */
64 private final Map<String, Call> mCalls = Maps.newHashMap();
65
66 /**
Sailesh Nepal810735e2014-03-18 18:15:46 -070067 * The call the user is currently interacting with. This is the call that should have audio
68 * focus and be visible in the in-call UI.
Santos Cordon4e9fffe2014-03-04 18:13:41 -080069 */
Sailesh Nepal810735e2014-03-18 18:15:46 -070070 private Call mForegroundCall;
Santos Cordon4e9fffe2014-03-04 18:13:41 -080071
Sailesh Nepal810735e2014-03-18 18:15:46 -070072 private final CallsManagerListener mCallLogManager;
Ben Gilad9f2bed32013-12-12 17:43:26 -080073
Sailesh Nepal810735e2014-03-18 18:15:46 -070074 private final CallsManagerListener mPhoneStateBroadcaster;
Ben Gilad9f2bed32013-12-12 17:43:26 -080075
Sailesh Nepal810735e2014-03-18 18:15:46 -070076 private final CallsManagerListener mCallAudioManager;
77
78 private final CallsManagerListener mInCallController;
79
Evan Charltonf02e9882014-03-06 12:54:52 -080080 private final List<OutgoingCallValidator> mOutgoingCallValidators = Lists.newArrayList();
Ben Gilad9f2bed32013-12-12 17:43:26 -080081
Evan Charltonf02e9882014-03-06 12:54:52 -080082 private final List<IncomingCallValidator> mIncomingCallValidators = Lists.newArrayList();
Ben Gilad9f2bed32013-12-12 17:43:26 -080083
Santos Cordon8e8b8d22013-12-19 14:14:05 -080084 /**
Ben Gilad8bdaa462014-02-05 12:53:19 -080085 * Initializes the required Telecomm components.
Santos Cordon8e8b8d22013-12-19 14:14:05 -080086 */
87 private CallsManager() {
Santos Cordon6242b132014-02-07 16:24:42 -080088 mSwitchboard = new Switchboard(this);
Yorke Leef98fb572014-03-05 10:56:55 -080089 mCallLogManager = new CallLogManager(TelecommApp.getInstance());
Sailesh Nepal810735e2014-03-18 18:15:46 -070090 mPhoneStateBroadcaster = new PhoneStateBroadcaster();
91 mCallAudioManager = new CallAudioManager();
92 mInCallController = new InCallController();
Santos Cordon8e8b8d22013-12-19 14:14:05 -080093 }
94
Ben Gilada0d9f752014-02-26 11:49:03 -080095 static CallsManager getInstance() {
96 return INSTANCE;
97 }
98
Sailesh Nepal810735e2014-03-18 18:15:46 -070099 ImmutableCollection<Call> getCalls() {
100 return ImmutableList.copyOf(mCalls.values());
101 }
102
103 Call getForegroundCall() {
104 return mForegroundCall;
105 }
106
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800107 /**
Santos Cordon493e8f22014-02-19 03:15:12 -0800108 * Starts the incoming call sequence by having switchboard gather more information about the
109 * specified call; using the specified call service descriptor. Upon success, execution returns
110 * to {@link #handleSuccessfulIncomingCall} to start the in-call UI.
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800111 *
Ben Giladc5b22692014-02-18 20:03:22 -0800112 * @param descriptor The descriptor of the call service to use for this incoming call.
Evan Charltona05805b2014-03-05 08:21:46 -0800113 * @param extras The optional extras Bundle passed with the intent used for the incoming call.
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800114 */
Evan Charltona05805b2014-03-05 08:21:46 -0800115 void processIncomingCallIntent(CallServiceDescriptor descriptor, Bundle extras) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800116 Log.d(this, "processIncomingCallIntent");
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800117 // Create a call with no handle. Eventually, switchboard will update the call with
118 // additional information from the call service, but for now we just need one to pass around
119 // with a unique call ID.
Sailesh Nepal810735e2014-03-18 18:15:46 -0700120 Call call = new Call(true /* isIncoming */);
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800121
Evan Charltona05805b2014-03-05 08:21:46 -0800122 mSwitchboard.retrieveIncomingCall(call, descriptor, extras);
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800123 }
124
125 /**
Ben Gilada0d9f752014-02-26 11:49:03 -0800126 * Validates the specified call and, upon no objection to connect it, adds the new call to the
127 * list of live calls. Also notifies the in-call app so the user can answer or reject the call.
128 *
129 * @param call The new incoming call.
Sailesh Nepal810735e2014-03-18 18:15:46 -0700130 * @param callInfo The details of the call.
Ben Gilada0d9f752014-02-26 11:49:03 -0800131 */
Sailesh Nepal810735e2014-03-18 18:15:46 -0700132 void handleSuccessfulIncomingCall(Call call, CallInfo callInfo) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800133 Log.d(this, "handleSuccessfulIncomingCall");
Sailesh Nepal810735e2014-03-18 18:15:46 -0700134 Preconditions.checkState(callInfo.getState() == CallState.RINGING);
Ben Gilada0d9f752014-02-26 11:49:03 -0800135
Sailesh Nepalce704b92014-03-17 18:31:43 -0700136 Uri handle = call.getHandle();
Ben Gilada0d9f752014-02-26 11:49:03 -0800137 ContactInfo contactInfo = call.getContactInfo();
138 for (IncomingCallValidator validator : mIncomingCallValidators) {
139 if (!validator.isValid(handle, contactInfo)) {
140 // TODO(gilad): Consider displaying an error message.
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800141 Log.i(this, "Dropping restricted incoming call");
Ben Gilada0d9f752014-02-26 11:49:03 -0800142 return;
143 }
144 }
145
146 // No objection to accept the incoming call, proceed with potentially connecting it (based
147 // on the user's action, or lack thereof).
Sailesh Nepal810735e2014-03-18 18:15:46 -0700148 call.setHandle(callInfo.getHandle());
149 setCallState(call, callInfo.getState());
Ben Gilada0d9f752014-02-26 11:49:03 -0800150 addCall(call);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700151 }
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800152
Sailesh Nepal810735e2014-03-18 18:15:46 -0700153 /**
154 * Called when an incoming call was not connected.
155 *
156 * @param call The incoming call.
157 */
158 void handleUnsuccessfulIncomingCall(Call call) {
159 setCallState(call, CallState.DISCONNECTED);
Ben Gilada0d9f752014-02-26 11:49:03 -0800160 }
161
162 /**
Yorke Lee33501632014-03-17 19:24:12 -0700163 * Attempts to issue/connect the specified call.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800164 *
Yorke Lee33501632014-03-17 19:24:12 -0700165 * @param handle Handle to connect the call with.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800166 * @param contactInfo Information about the entity being called.
Yorke Lee33501632014-03-17 19:24:12 -0700167 * @param gatewayInfo Optional gateway information that can be used to route the call to the
168 * actual dialed handle via a gateway provider. May be null.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800169 */
Yorke Lee33501632014-03-17 19:24:12 -0700170 void placeOutgoingCall(Uri handle, ContactInfo contactInfo, GatewayInfo gatewayInfo) {
Ben Gilad8bdaa462014-02-05 12:53:19 -0800171 for (OutgoingCallValidator validator : mOutgoingCallValidators) {
Ben Gilada0d9f752014-02-26 11:49:03 -0800172 if (!validator.isValid(handle, contactInfo)) {
173 // TODO(gilad): Display an error message.
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800174 Log.i(this, "Dropping restricted outgoing call.");
Ben Gilada0d9f752014-02-26 11:49:03 -0800175 return;
176 }
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800177 }
178
Yorke Lee33501632014-03-17 19:24:12 -0700179 final Uri uriHandle = (gatewayInfo == null) ? handle : gatewayInfo.getGatewayHandle();
180
181 if (gatewayInfo == null) {
182 Log.i(this, "Creating a new outgoing call with handle: %s", Log.pii(uriHandle));
183 } else {
184 Log.i(this, "Creating a new outgoing call with gateway handle: %s, original handle: %s",
185 Log.pii(uriHandle), Log.pii(handle));
186 }
187 Call call = new Call(uriHandle, contactInfo, gatewayInfo, false /*isIncoming*/);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700188 setCallState(call, CallState.DIALING);
189 addCall(call);
Santos Cordonc195e362014-02-11 17:05:31 -0800190 mSwitchboard.placeOutgoingCall(call);
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800191 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800192
193 /**
Sailesh Nepal810735e2014-03-18 18:15:46 -0700194 * Called when a call service acknowledges that it can place a call.
Santos Cordon681663d2014-01-30 04:32:15 -0800195 *
196 * @param call The new outgoing call.
197 */
198 void handleSuccessfulOutgoingCall(Call call) {
Sailesh Nepal810735e2014-03-18 18:15:46 -0700199 Log.v(this, "handleSuccessfulOutgoingCall, %s", call);
Santos Cordon681663d2014-01-30 04:32:15 -0800200 }
201
202 /**
Sailesh Nepal810735e2014-03-18 18:15:46 -0700203 * Called when an outgoing call was not placed.
Yorke Leef98fb572014-03-05 10:56:55 -0800204 *
Sailesh Nepal810735e2014-03-18 18:15:46 -0700205 * @param call The outgoing call.
206 * @param isAborted True if the call was unsuccessful because it was aborted.
Yorke Leef98fb572014-03-05 10:56:55 -0800207 */
Sailesh Nepal810735e2014-03-18 18:15:46 -0700208 void handleUnsuccessfulOutgoingCall(Call call, boolean isAborted) {
209 Log.v(this, "handleAbortedOutgoingCall, call: %s, isAborted: %b", call, isAborted);
210 if (isAborted) {
211 call.abort();
212 setCallState(call, CallState.ABORTED);
213 } else {
214 setCallState(call, CallState.DISCONNECTED);
215 }
216 removeCall(call);
Yorke Leef98fb572014-03-05 10:56:55 -0800217 }
218
219 /**
Santos Cordone3d76ab2014-01-28 17:25:20 -0800220 * Instructs Telecomm to answer the specified call. Intended to be invoked by the in-call
221 * app through {@link InCallAdapter} after Telecomm notifies it of an incoming call followed by
222 * the user opting to answer said call.
223 *
224 * @param callId The ID of the call.
225 */
226 void answerCall(String callId) {
Santos Cordon61d0f702014-02-19 02:52:23 -0800227 Call call = mCalls.get(callId);
228 if (call == null) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800229 Log.i(this, "Request to answer a non-existent call %s", callId);
Santos Cordon61d0f702014-02-19 02:52:23 -0800230 } else {
Sailesh Nepal810735e2014-03-18 18:15:46 -0700231 mCallLogManager.onIncomingCallAnswered(call);
232 mPhoneStateBroadcaster.onIncomingCallAnswered(call);
233 mCallAudioManager.onIncomingCallAnswered(call);
234 mInCallController.onIncomingCallAnswered(call);
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800235
Santos Cordon61d0f702014-02-19 02:52:23 -0800236 // We do not update the UI until we get confirmation of the answer() through
237 // {@link #markCallAsActive}. However, if we ever change that to look more responsive,
238 // then we need to make sure we add a timeout for the answer() in case the call never
239 // comes out of RINGING.
240 call.answer();
241 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800242 }
243
244 /**
245 * Instructs Telecomm to reject the specified call. Intended to be invoked by the in-call
246 * app through {@link InCallAdapter} after Telecomm notifies it of an incoming call followed by
247 * the user opting to reject said call.
248 *
249 * @param callId The ID of the call.
250 */
251 void rejectCall(String callId) {
Santos Cordon61d0f702014-02-19 02:52:23 -0800252 Call call = mCalls.get(callId);
253 if (call == null) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800254 Log.i(this, "Request to reject a non-existent call %s", callId);
Santos Cordon61d0f702014-02-19 02:52:23 -0800255 } else {
Sailesh Nepal810735e2014-03-18 18:15:46 -0700256 mCallLogManager.onIncomingCallRejected(call);
257 mPhoneStateBroadcaster.onIncomingCallRejected(call);
258 mCallAudioManager.onIncomingCallRejected(call);
259 mInCallController.onIncomingCallRejected(call);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700260
Santos Cordon61d0f702014-02-19 02:52:23 -0800261 call.reject();
262 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800263 }
264
265 /**
266 * Instructs Telecomm to disconnect the specified call. Intended to be invoked by the
267 * in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered by
268 * the user hitting the end-call button.
269 *
270 * @param callId The ID of the call.
271 */
272 void disconnectCall(String callId) {
Santos Cordon049b7b62014-01-30 05:34:26 -0800273 Call call = mCalls.get(callId);
274 if (call == null) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800275 Log.w(this, "Unknown call (%s) asked to disconnect", callId);
Santos Cordon049b7b62014-01-30 05:34:26 -0800276 } else {
277 call.disconnect();
278 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800279 }
Santos Cordon681663d2014-01-30 04:32:15 -0800280
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700281 /**
282 * Instructs Telecomm to put the specified call on hold. Intended to be invoked by the
283 * in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered by
284 * the user hitting the hold button during an active call.
285 *
286 * @param callId The ID of the call.
287 */
288 void holdCall(String callId) {
289 Call call = mCalls.get(callId);
290 if (call == null) {
291 Log.w(this, "Unknown call (%s) asked to be put on hold", callId);
292 } else {
293 Log.d(this, "Putting call on hold: (%s)", callId);
294 call.hold();
295 }
296 }
297
298 /**
299 * Instructs Telecomm to release the specified call from hold. Intended to be invoked by
300 * the in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered
301 * by the user hitting the hold button during a held call.
302 *
303 * @param callId The ID of the call
304 */
305 void unholdCall(String callId) {
306 Call call = mCalls.get(callId);
307 if (call == null) {
308 Log.w(this, "Unknown call (%s) asked to be removed from hold", callId);
309 } else {
310 Log.d(this, "Removing call from hold: (%s)", callId);
311 call.unhold();
312 }
313 }
314
Santos Cordon681663d2014-01-30 04:32:15 -0800315 void markCallAsRinging(String callId) {
316 setCallState(callId, CallState.RINGING);
317 }
318
319 void markCallAsDialing(String callId) {
320 setCallState(callId, CallState.DIALING);
321 }
322
323 void markCallAsActive(String callId) {
324 setCallState(callId, CallState.ACTIVE);
325 }
326
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700327 void markCallAsOnHold(String callId) {
328 setCallState(callId, CallState.ON_HOLD);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700329 }
330
Santos Cordon049b7b62014-01-30 05:34:26 -0800331 /**
332 * Marks the specified call as DISCONNECTED and notifies the in-call app. If this was the last
333 * live call, then also disconnect from the in-call controller.
334 *
335 * @param callId The ID of the call.
336 */
Santos Cordon681663d2014-01-30 04:32:15 -0800337 void markCallAsDisconnected(String callId) {
338 setCallState(callId, CallState.DISCONNECTED);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700339 removeCall(mCalls.remove(callId));
Ben Gilada0d9f752014-02-26 11:49:03 -0800340 }
341
342 /**
Santos Cordon1ae2b852014-03-19 03:03:10 -0700343 * @return True if there exists a call with the specific state.
344 */
345 boolean hasCallWithState(CallState... states) {
346 for (Call call : mCalls.values()) {
347 for (CallState state : states) {
348 if (call.getState() == state) {
349 return true;
350 }
351 }
352 }
353
354 return false;
355 }
356
357 /**
Santos Cordon4b2c1192014-03-19 18:15:38 -0700358 * Cleans up any calls currently associated with the specified call service when the
359 * call-service binder disconnects unexpectedly.
360 *
361 * @param callService The call service that disconnected.
362 */
363 void handleCallServiceDeath(CallServiceWrapper callService) {
364 Preconditions.checkNotNull(callService);
365 for (Call call : ImmutableList.copyOf(mCalls.values())) {
366 if (call.getCallService() == callService) {
367 markCallAsDisconnected(call.getId());
368 }
369 }
370 }
371
372 /**
Ben Gilada0d9f752014-02-26 11:49:03 -0800373 * Adds the specified call to the main list of live calls.
374 *
375 * @param call The call to add.
376 */
377 private void addCall(Call call) {
378 mCalls.put(call.getId(), call);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700379
380 mCallLogManager.onCallAdded(call);
381 mPhoneStateBroadcaster.onCallAdded(call);
382 mCallAudioManager.onCallAdded(call);
383 mInCallController.onCallAdded(call);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700384 updateForegroundCall();
Ben Gilada0d9f752014-02-26 11:49:03 -0800385 }
386
Sailesh Nepal810735e2014-03-18 18:15:46 -0700387 private void removeCall(Call call) {
388 call.clearCallService();
389
390 mCallLogManager.onCallRemoved(call);
391 mPhoneStateBroadcaster.onCallRemoved(call);
392 mCallAudioManager.onCallRemoved(call);
393 mInCallController.onCallRemoved(call);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700394 updateForegroundCall();
Ben Gilada0d9f752014-02-26 11:49:03 -0800395 }
396
397 /**
Santos Cordon1ae2b852014-03-19 03:03:10 -0700398 * Sets the specified state on the specified call.
Santos Cordon681663d2014-01-30 04:32:15 -0800399 *
400 * @param callId The ID of the call to update.
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800401 * @param newState The new state of the call.
Santos Cordon681663d2014-01-30 04:32:15 -0800402 */
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800403 private void setCallState(String callId, CallState newState) {
Santos Cordon681663d2014-01-30 04:32:15 -0800404 Preconditions.checkState(!Strings.isNullOrEmpty(callId));
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800405 Preconditions.checkNotNull(newState);
Santos Cordon681663d2014-01-30 04:32:15 -0800406
407 Call call = mCalls.get(callId);
408 if (call == null) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800409 Log.w(this, "Call %s was not found while attempting to update the state to %s.",
410 callId, newState );
Santos Cordon681663d2014-01-30 04:32:15 -0800411 } else {
Sailesh Nepal810735e2014-03-18 18:15:46 -0700412 setCallState(call, newState);
413 }
414 }
Evan Charlton5c670a92014-03-06 14:58:20 -0800415
Sailesh Nepal810735e2014-03-18 18:15:46 -0700416 /**
Santos Cordon1ae2b852014-03-19 03:03:10 -0700417 * Sets the specified state on the specified call.
Sailesh Nepal810735e2014-03-18 18:15:46 -0700418 *
419 * @param call The call.
420 * @param newState The new state of the call.
421 */
422 private void setCallState(Call call, CallState newState) {
423 CallState oldState = call.getState();
424 if (newState != oldState) {
425 // Unfortunately, in the telephony world the radio is king. So if the call notifies
426 // us that the call is in a particular state, we allow it even if it doesn't make
427 // sense (e.g., ACTIVE -> RINGING).
428 // TODO(santoscordon): Consider putting a stop to the above and turning CallState
429 // into a well-defined state machine.
430 // TODO(santoscordon): Define expected state transitions here, and log when an
431 // unexpected transition occurs.
432 call.setState(newState);
433
434 // Only broadcast state change for calls that are being tracked.
435 if (mCalls.containsKey(call.getId())) {
436 mCallLogManager.onCallStateChanged(call, oldState, newState);
437 mPhoneStateBroadcaster.onCallStateChanged(call, oldState, newState);
438 mCallAudioManager.onCallStateChanged(call, oldState, newState);
439 mInCallController.onCallStateChanged(call, oldState, newState);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700440 updateForegroundCall();
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800441 }
442 }
443 }
444
445 /**
Sailesh Nepal810735e2014-03-18 18:15:46 -0700446 * Checks which call should be visible to the user and have audio focus.
Evan Charlton5c670a92014-03-06 14:58:20 -0800447 */
Sailesh Nepal810735e2014-03-18 18:15:46 -0700448 private void updateForegroundCall() {
449 Call newForegroundCall = null;
450 for (Call call : mCalls.values()) {
451 // Incoming ringing calls have priority.
452 if (call.getState() == CallState.RINGING) {
453 newForegroundCall = call;
Evan Charlton5c670a92014-03-06 14:58:20 -0800454 break;
Sailesh Nepal810735e2014-03-18 18:15:46 -0700455 }
456 if (call.getState() == CallState.ACTIVE) {
457 newForegroundCall = call;
458 // Don't break in case there's a ringing call that has priority.
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800459 }
460 }
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800461
Sailesh Nepal810735e2014-03-18 18:15:46 -0700462 if (newForegroundCall != mForegroundCall) {
463 Call oldForegroundCall = mForegroundCall;
464 mForegroundCall = newForegroundCall;
465
466 mCallLogManager.onForegroundCallChanged(oldForegroundCall, mForegroundCall);
467 mPhoneStateBroadcaster.onForegroundCallChanged(oldForegroundCall, mForegroundCall);
468 mCallAudioManager.onForegroundCallChanged(oldForegroundCall, mForegroundCall);
469 mInCallController.onForegroundCallChanged(oldForegroundCall, mForegroundCall);
Santos Cordon681663d2014-01-30 04:32:15 -0800470 }
471 }
Ben Gilad9f2bed32013-12-12 17:43:26 -0800472}