blob: 556c77085d70a45e937ba5226fb60572f618aba0 [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
Evan Charlton5c670a92014-03-06 14:58:20 -080019import android.Manifest;
Sailesh Nepal1cecc7c2014-03-10 17:03:08 -070020import android.content.Context;
Evan Charlton5c670a92014-03-06 14:58:20 -080021import android.content.Intent;
Sailesh Nepal1cecc7c2014-03-10 17:03:08 -070022import android.media.AudioManager;
Sailesh Nepalce704b92014-03-17 18:31:43 -070023import android.net.Uri;
Evan Charltona05805b2014-03-05 08:21:46 -080024import android.os.Bundle;
Evan Charlton5c670a92014-03-06 14:58:20 -080025import android.telecomm.CallService;
Ben Giladc5b22692014-02-18 20:03:22 -080026import android.telecomm.CallServiceDescriptor;
Santos Cordon681663d2014-01-30 04:32:15 -080027import android.telecomm.CallState;
Sailesh Nepal3c6f27e2014-03-13 12:34:52 -070028import android.telecomm.TelecommConstants;
Evan Charlton5c670a92014-03-06 14:58:20 -080029import android.telephony.TelephonyManager;
Santos Cordon8e8b8d22013-12-19 14:14:05 -080030
Sailesh Nepala439e1b2014-03-11 18:19:58 -070031import com.android.internal.telecomm.ICallService;
Santos Cordon681663d2014-01-30 04:32:15 -080032import com.google.common.base.Preconditions;
33import com.google.common.base.Strings;
Santos Cordon8e8b8d22013-12-19 14:14:05 -080034import com.google.common.collect.Lists;
Santos Cordon681663d2014-01-30 04:32:15 -080035import com.google.common.collect.Maps;
Ben Gilad9f2bed32013-12-12 17:43:26 -080036
Ben Gilad9f2bed32013-12-12 17:43:26 -080037import java.util.List;
Santos Cordon681663d2014-01-30 04:32:15 -080038import java.util.Map;
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 Cordon8e8b8d22013-12-19 14:14:05 -080049 private static final CallsManager INSTANCE = new CallsManager();
Ben Gilad9f2bed32013-12-12 17:43:26 -080050
Santos Cordone3d76ab2014-01-28 17:25:20 -080051 private final Switchboard mSwitchboard;
52
53 /** Used to control the in-call app. */
54 private final InCallController mInCallController;
55
Santos Cordon4e9fffe2014-03-04 18:13:41 -080056 private final Ringer mRinger;
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 /**
Santos Cordon4e9fffe2014-03-04 18:13:41 -080067 * Used to keep ordering of unanswered incoming calls. The existence of multiple call services
68 * means that there can easily exist multiple incoming calls and explicit ordering is useful for
69 * maintaining the proper state of the ringer.
70 * TODO(santoscordon): May want to add comments about ITelephony.answerCall() method since
71 * ordering may also apply to that case.
72 */
73 private final List<Call> mUnansweredIncomingCalls = Lists.newLinkedList();
74
75 /**
Santos Cordon8e8b8d22013-12-19 14:14:05 -080076 * May be unnecessary per off-line discussions (between santoscordon and gilad) since the set
77 * of CallsManager APIs that need to be exposed to the dialer (or any application firing call
78 * intents) may be empty.
79 */
80 private DialerAdapter mDialerAdapter;
Ben Gilad9f2bed32013-12-12 17:43:26 -080081
Santos Cordon8e8b8d22013-12-19 14:14:05 -080082 private InCallAdapter mInCallAdapter;
Ben Gilad9f2bed32013-12-12 17:43:26 -080083
Santos Cordon8e8b8d22013-12-19 14:14:05 -080084 private CallLogManager mCallLogManager;
Ben Gilad9f2bed32013-12-12 17:43:26 -080085
Santos Cordon8e8b8d22013-12-19 14:14:05 -080086 private VoicemailManager mVoicemailManager;
Ben Gilad9f2bed32013-12-12 17:43:26 -080087
Evan Charltonf02e9882014-03-06 12:54:52 -080088 private final List<OutgoingCallValidator> mOutgoingCallValidators = Lists.newArrayList();
Ben Gilad9f2bed32013-12-12 17:43:26 -080089
Evan Charltonf02e9882014-03-06 12:54:52 -080090 private final List<IncomingCallValidator> mIncomingCallValidators = Lists.newArrayList();
Ben Gilad9f2bed32013-12-12 17:43:26 -080091
Santos Cordon8e8b8d22013-12-19 14:14:05 -080092 /**
Ben Gilad8bdaa462014-02-05 12:53:19 -080093 * Initializes the required Telecomm components.
Santos Cordon8e8b8d22013-12-19 14:14:05 -080094 */
95 private CallsManager() {
Santos Cordon6242b132014-02-07 16:24:42 -080096 mSwitchboard = new Switchboard(this);
97 mInCallController = new InCallController(this);
Santos Cordon4e9fffe2014-03-04 18:13:41 -080098 mRinger = new Ringer();
Yorke Leef98fb572014-03-05 10:56:55 -080099 mCallLogManager = new CallLogManager(TelecommApp.getInstance());
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
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800106 /**
Santos Cordon493e8f22014-02-19 03:15:12 -0800107 * Starts the incoming call sequence by having switchboard gather more information about the
108 * specified call; using the specified call service descriptor. Upon success, execution returns
109 * to {@link #handleSuccessfulIncomingCall} to start the in-call UI.
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800110 *
Ben Giladc5b22692014-02-18 20:03:22 -0800111 * @param descriptor The descriptor of the call service to use for this incoming call.
Evan Charltona05805b2014-03-05 08:21:46 -0800112 * @param extras The optional extras Bundle passed with the intent used for the incoming call.
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800113 */
Evan Charltona05805b2014-03-05 08:21:46 -0800114 void processIncomingCallIntent(CallServiceDescriptor descriptor, Bundle extras) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800115 Log.d(this, "processIncomingCallIntent");
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800116 // Create a call with no handle. Eventually, switchboard will update the call with
117 // additional information from the call service, but for now we just need one to pass around
118 // with a unique call ID.
Santos Cordon493e8f22014-02-19 03:15:12 -0800119 Call call = new Call();
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800120
Evan Charltona05805b2014-03-05 08:21:46 -0800121 mSwitchboard.retrieveIncomingCall(call, descriptor, extras);
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800122 }
123
124 /**
Ben Gilada0d9f752014-02-26 11:49:03 -0800125 * Validates the specified call and, upon no objection to connect it, adds the new call to the
126 * list of live calls. Also notifies the in-call app so the user can answer or reject the call.
127 *
128 * @param call The new incoming call.
129 */
130 void handleSuccessfulIncomingCall(Call call) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800131 Log.d(this, "handleSuccessfulIncomingCall");
Ben Gilada0d9f752014-02-26 11:49:03 -0800132 Preconditions.checkState(call.getState() == CallState.RINGING);
133
Sailesh Nepalce704b92014-03-17 18:31:43 -0700134 Uri handle = call.getHandle();
Ben Gilada0d9f752014-02-26 11:49:03 -0800135 ContactInfo contactInfo = call.getContactInfo();
136 for (IncomingCallValidator validator : mIncomingCallValidators) {
137 if (!validator.isValid(handle, contactInfo)) {
138 // TODO(gilad): Consider displaying an error message.
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800139 Log.i(this, "Dropping restricted incoming call");
Ben Gilada0d9f752014-02-26 11:49:03 -0800140 return;
141 }
142 }
143
144 // No objection to accept the incoming call, proceed with potentially connecting it (based
145 // on the user's action, or lack thereof).
146 addCall(call);
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800147
148 mUnansweredIncomingCalls.add(call);
149 if (mUnansweredIncomingCalls.size() == 1) {
150 // Start the ringer if we are the top-most incoming call (the only one in this case).
151 mRinger.startRinging();
152 }
Ben Gilada0d9f752014-02-26 11:49:03 -0800153 }
154
155 /**
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800156 * Attempts to issue/connect the specified call. From an (arbitrary) application standpoint,
157 * all that is required to initiate this flow is to fire either of the CALL, CALL_PRIVILEGED,
158 * and CALL_EMERGENCY intents. These are listened to by CallActivity.java which then invokes
159 * this method.
160 *
161 * @param handle The handle to dial.
162 * @param contactInfo Information about the entity being called.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800163 */
Sailesh Nepalce704b92014-03-17 18:31:43 -0700164 void processOutgoingCallIntent(Uri handle, ContactInfo contactInfo) {
Ben Gilad8bdaa462014-02-05 12:53:19 -0800165 for (OutgoingCallValidator validator : mOutgoingCallValidators) {
Ben Gilada0d9f752014-02-26 11:49:03 -0800166 if (!validator.isValid(handle, contactInfo)) {
167 // TODO(gilad): Display an error message.
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800168 Log.i(this, "Dropping restricted outgoing call.");
Ben Gilada0d9f752014-02-26 11:49:03 -0800169 return;
170 }
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800171 }
172
173 // No objection to issue the call, proceed with trying to put it through.
Ben Gilad13329fd2014-02-11 17:20:29 -0800174 Call call = new Call(handle, contactInfo);
Santos Cordonc195e362014-02-11 17:05:31 -0800175 mSwitchboard.placeOutgoingCall(call);
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800176 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800177
178 /**
Santos Cordon681663d2014-01-30 04:32:15 -0800179 * Adds a new outgoing call to the list of live calls and notifies the in-call app.
180 *
181 * @param call The new outgoing call.
182 */
183 void handleSuccessfulOutgoingCall(Call call) {
184 // OutgoingCallProcessor sets the call state to DIALING when it receives confirmation of the
185 // placed call from the call service so there is no need to set it here. Instead, check that
186 // the state is appropriate.
187 Preconditions.checkState(call.getState() == CallState.DIALING);
Santos Cordon681663d2014-01-30 04:32:15 -0800188 addCall(call);
Santos Cordon681663d2014-01-30 04:32:15 -0800189 }
190
191 /**
Yorke Leef98fb572014-03-05 10:56:55 -0800192 * Informs mCallLogManager about the outgoing call that failed, so that it can be logged.
193 *
194 * @param call The failed outgoing call.
195 */
196 void handleFailedOutgoingCall(Call call) {
197 mCallLogManager.logFailedOutgoingCall(call);
198 }
199
200 /**
Santos Cordone3d76ab2014-01-28 17:25:20 -0800201 * Instructs Telecomm to answer the specified call. Intended to be invoked by the in-call
202 * app through {@link InCallAdapter} after Telecomm notifies it of an incoming call followed by
203 * the user opting to answer said call.
204 *
205 * @param callId The ID of the call.
206 */
207 void answerCall(String callId) {
Santos Cordon61d0f702014-02-19 02:52:23 -0800208 Call call = mCalls.get(callId);
209 if (call == null) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800210 Log.i(this, "Request to answer a non-existent call %s", callId);
Santos Cordon61d0f702014-02-19 02:52:23 -0800211 } else {
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800212 stopRinging(call);
213
Santos Cordon61d0f702014-02-19 02:52:23 -0800214 // We do not update the UI until we get confirmation of the answer() through
215 // {@link #markCallAsActive}. However, if we ever change that to look more responsive,
216 // then we need to make sure we add a timeout for the answer() in case the call never
217 // comes out of RINGING.
218 call.answer();
219 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800220 }
221
222 /**
223 * Instructs Telecomm to reject the specified call. Intended to be invoked by the in-call
224 * app through {@link InCallAdapter} after Telecomm notifies it of an incoming call followed by
225 * the user opting to reject said call.
226 *
227 * @param callId The ID of the call.
228 */
229 void rejectCall(String callId) {
Santos Cordon61d0f702014-02-19 02:52:23 -0800230 Call call = mCalls.get(callId);
231 if (call == null) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800232 Log.i(this, "Request to reject a non-existent call %s", callId);
Santos Cordon61d0f702014-02-19 02:52:23 -0800233 } else {
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800234 stopRinging(call);
Santos Cordon61d0f702014-02-19 02:52:23 -0800235 call.reject();
236 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800237 }
238
239 /**
240 * Instructs Telecomm to disconnect the specified call. Intended to be invoked by the
241 * in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered by
242 * the user hitting the end-call button.
243 *
244 * @param callId The ID of the call.
245 */
246 void disconnectCall(String callId) {
Santos Cordon049b7b62014-01-30 05:34:26 -0800247 Call call = mCalls.get(callId);
248 if (call == null) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800249 Log.w(this, "Unknown call (%s) asked to disconnect", callId);
Santos Cordon049b7b62014-01-30 05:34:26 -0800250 } else {
251 call.disconnect();
252 }
253
Sailesh Nepal1cecc7c2014-03-10 17:03:08 -0700254 // TODO(sail): Replace with CallAudioManager.
255 Log.v(this, "disconnectCall, abandoning audio focus");
256 Context context = TelecommApp.getInstance().getApplicationContext();
257 AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
258 audioManager.setMode(AudioManager.MODE_NORMAL);
259 audioManager.abandonAudioFocusForCall();
Santos Cordone3d76ab2014-01-28 17:25:20 -0800260 }
Santos Cordon681663d2014-01-30 04:32:15 -0800261
262 void markCallAsRinging(String callId) {
263 setCallState(callId, CallState.RINGING);
264 }
265
266 void markCallAsDialing(String callId) {
267 setCallState(callId, CallState.DIALING);
268 }
269
270 void markCallAsActive(String callId) {
271 setCallState(callId, CallState.ACTIVE);
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800272 removeFromUnansweredCalls(callId);
Sailesh Nepalfc511ca2014-02-20 18:48:53 -0800273 mInCallController.markCallAsActive(callId);
Sailesh Nepal1cecc7c2014-03-10 17:03:08 -0700274
275 // TODO(sail): Replace with CallAudioManager.
276 Log.v(this, "markCallAsActive, requesting audio focus");
277 Context context = TelecommApp.getInstance().getApplicationContext();
278 AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
279 audioManager.requestAudioFocusForCall(AudioManager.STREAM_VOICE_CALL,
280 AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);
281 audioManager.setMode(AudioManager.MODE_IN_CALL);
282 audioManager.setMicrophoneMute(false);
283 audioManager.setSpeakerphoneOn(false);
Santos Cordon681663d2014-01-30 04:32:15 -0800284 }
285
Santos Cordon049b7b62014-01-30 05:34:26 -0800286 /**
287 * Marks the specified call as DISCONNECTED and notifies the in-call app. If this was the last
288 * live call, then also disconnect from the in-call controller.
289 *
290 * @param callId The ID of the call.
291 */
Santos Cordon681663d2014-01-30 04:32:15 -0800292 void markCallAsDisconnected(String callId) {
293 setCallState(callId, CallState.DISCONNECTED);
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800294 removeFromUnansweredCalls(callId);
Santos Cordonc195e362014-02-11 17:05:31 -0800295
296 Call call = mCalls.remove(callId);
297 // At this point the call service has confirmed that the call is disconnected to it is
298 // safe to disassociate the call from its call service.
299 call.clearCallService();
Santos Cordon049b7b62014-01-30 05:34:26 -0800300
301 // Notify the in-call UI
302 mInCallController.markCallAsDisconnected(callId);
303 if (mCalls.isEmpty()) {
304 mInCallController.unbind();
305 }
Yorke Leef98fb572014-03-05 10:56:55 -0800306
307 // Log the call in the call log.
308 mCallLogManager.logDisconnectedCall(call);
Santos Cordon681663d2014-01-30 04:32:15 -0800309 }
310
311 /**
Ben Gilada0d9f752014-02-26 11:49:03 -0800312 * Sends all the live calls to the in-call app if any exist. If there are no live calls, then
313 * tells the in-call controller to unbind since it is not needed.
314 */
315 void updateInCall() {
316 if (mCalls.isEmpty()) {
317 mInCallController.unbind();
318 } else {
319 for (Call call : mCalls.values()) {
320 addInCallEntry(call);
321 }
322 }
323 }
324
325 /**
326 * Adds the specified call to the main list of live calls.
327 *
328 * @param call The call to add.
329 */
330 private void addCall(Call call) {
331 mCalls.put(call.getId(), call);
332 addInCallEntry(call);
333 }
334
335 /**
336 * Notifies the in-call app of the specified (new) call.
337 */
338 private void addInCallEntry(Call call) {
339 mInCallController.addCall(call.toCallInfo());
340 }
341
342 /**
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800343 * Sets the specified state on the specified call. Updates the ringer if the call is exiting
344 * the RINGING state.
Santos Cordon681663d2014-01-30 04:32:15 -0800345 *
346 * @param callId The ID of the call to update.
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800347 * @param newState The new state of the call.
Santos Cordon681663d2014-01-30 04:32:15 -0800348 */
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800349 private void setCallState(String callId, CallState newState) {
Santos Cordon681663d2014-01-30 04:32:15 -0800350 Preconditions.checkState(!Strings.isNullOrEmpty(callId));
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800351 Preconditions.checkNotNull(newState);
Santos Cordon681663d2014-01-30 04:32:15 -0800352
353 Call call = mCalls.get(callId);
354 if (call == null) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800355 Log.w(this, "Call %s was not found while attempting to update the state to %s.",
356 callId, newState );
Santos Cordon681663d2014-01-30 04:32:15 -0800357 } else {
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800358 if (newState != call.getState()) {
359 // Unfortunately, in the telephony world the radio is king. So if the call notifies
360 // us that the call is in a particular state, we allow it even if it doesn't make
361 // sense (e.g., ACTIVE -> RINGING).
362 // TODO(santoscordon): Consider putting a stop to the above and turning CallState
363 // into a well-defined state machine.
364 // TODO(santoscordon): Define expected state transitions here, and log when an
365 // unexpected transition occurs.
366 call.setState(newState);
367 // TODO(santoscordon): Notify the in-call app whenever a call changes state.
Evan Charlton5c670a92014-03-06 14:58:20 -0800368
369 broadcastState(call);
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800370 }
371 }
372 }
373
374 /**
Evan Charlton5c670a92014-03-06 14:58:20 -0800375 * Send a {@link TelephonyManager#ACTION_PHONE_STATE_CHANGED} broadcast when the call state
376 * changes. TODO: Split this out into a separate class and do it properly; this is just a first
377 * pass over the functionality.
378 *
379 * @param call The {@link Call} being updated.
380 */
381 private void broadcastState(Call call) {
382 final String callState;
383 switch (call.getState()) {
384 case DIALING:
385 case ACTIVE:
386 callState = TelephonyManager.EXTRA_STATE_OFFHOOK;
387 break;
388
389 case RINGING:
390 callState = TelephonyManager.EXTRA_STATE_RINGING;
391 break;
392
393 case DISCONNECTED:
394 case ABORTED:
395 callState = TelephonyManager.EXTRA_STATE_IDLE;
396 break;
397
398 default:
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800399 Log.w(this, "Call is in an unknown state (%s), not broadcasting: %s", call.getState(),
400 call.getId());
Evan Charlton5c670a92014-03-06 14:58:20 -0800401 return;
402 }
403
404 Intent updateIntent = new Intent(TelephonyManager.ACTION_PHONE_STATE_CHANGED);
405 updateIntent.putExtra(TelephonyManager.EXTRA_STATE, callState);
Sailesh Nepal85680bf2014-03-13 11:56:30 -0700406 // TODO: See if we can add this (the current API doesn't have a callId).
407 updateIntent.putExtra(TelecommConstants.EXTRA_CALL_ID, call.getId());
Evan Charlton5c670a92014-03-06 14:58:20 -0800408
409 // Populate both, since the original API was needlessly complicated.
410 updateIntent.putExtra(TelephonyManager.EXTRA_INCOMING_NUMBER, call.getHandle());
411 // TODO: See if we can add this (the current API only sets this on NEW_OUTGOING_CALL).
412 updateIntent.putExtra(Intent.EXTRA_PHONE_NUMBER, call.getHandle());
413
414 // TODO: Replace these with real constants once this API has been vetted.
415 CallServiceWrapper callService = call.getCallService();
416 if (callService != null) {
417 updateIntent.putExtra(CallService.class.getName(), callService.getComponentName());
418 }
419 TelecommApp.getInstance().sendBroadcast(updateIntent, Manifest.permission.READ_PHONE_STATE);
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800420 Log.i(this, "Broadcasted state change: %s", callState);
Evan Charlton5c670a92014-03-06 14:58:20 -0800421 }
422
423 /**
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800424 * Removes the specified call from the list of unanswered incoming calls and updates the ringer
425 * based on the new state of {@link #mUnansweredIncomingCalls}. Safe to call with a call ID that
426 * is not present in the list of incoming calls.
427 *
428 * @param callId The ID of the call.
429 */
430 private void removeFromUnansweredCalls(String callId) {
431 Call call = mCalls.get(callId);
432 if (call != null && mUnansweredIncomingCalls.remove(call)) {
433 if (mUnansweredIncomingCalls.isEmpty()) {
434 mRinger.stopRinging();
435 } else {
436 mRinger.startRinging();
437 }
438 }
439 }
440
441 /**
442 * Stops playing the ringer if the specified call is the top-most incoming call. This exists
Ben Gilad61925612014-03-11 19:06:36 -0700443 * separately from {@link #removeFromUnansweredCalls} to allow stopping the ringer for calls
444 * that should remain in {@link #mUnansweredIncomingCalls}, invoked from {@link #answerCall}
445 * and {@link #rejectCall}.
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800446 *
447 * @param call The call for which we should stop ringing.
448 */
449 private void stopRinging(Call call) {
450 // Only stop the ringer if this call is the top-most incoming call.
451 if (!mUnansweredIncomingCalls.isEmpty() && mUnansweredIncomingCalls.get(0) == call) {
452 mRinger.stopRinging();
Santos Cordon681663d2014-01-30 04:32:15 -0800453 }
454 }
Ben Gilad9f2bed32013-12-12 17:43:26 -0800455}