blob: 2f1b76402bf0f358be7e3901ef6797d4edf9c917 [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;
Evan Charltona05805b2014-03-05 08:21:46 -080023import android.os.Bundle;
Evan Charlton5c670a92014-03-06 14:58:20 -080024import android.telecomm.CallService;
Ben Giladc5b22692014-02-18 20:03:22 -080025import android.telecomm.CallServiceDescriptor;
Santos Cordon681663d2014-01-30 04:32:15 -080026import android.telecomm.CallState;
Sailesh Nepal3c6f27e2014-03-13 12:34:52 -070027import android.telecomm.TelecommConstants;
Evan Charlton5c670a92014-03-06 14:58:20 -080028import android.telephony.TelephonyManager;
Santos Cordon8e8b8d22013-12-19 14:14:05 -080029
Sailesh Nepala439e1b2014-03-11 18:19:58 -070030import com.android.internal.telecomm.ICallService;
Santos Cordon681663d2014-01-30 04:32:15 -080031import com.google.common.base.Preconditions;
32import com.google.common.base.Strings;
Santos Cordon8e8b8d22013-12-19 14:14:05 -080033import com.google.common.collect.Lists;
Santos Cordon681663d2014-01-30 04:32:15 -080034import com.google.common.collect.Maps;
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;
Ben Gilad9f2bed32013-12-12 17:43:26 -080038
Ben Giladdd8c6082013-12-30 14:44:08 -080039/**
40 * Singleton.
41 *
42 * NOTE(gilad): by design most APIs are package private, use the relevant adapter/s to allow
43 * access from other packages specifically refraining from passing the CallsManager instance
44 * beyond the com.android.telecomm package boundary.
45 */
Santos Cordon8e8b8d22013-12-19 14:14:05 -080046public final class CallsManager {
Ben Gilada0d9f752014-02-26 11:49:03 -080047
Santos Cordon8e8b8d22013-12-19 14:14:05 -080048 private static final CallsManager INSTANCE = new CallsManager();
Ben Gilad9f2bed32013-12-12 17:43:26 -080049
Santos Cordone3d76ab2014-01-28 17:25:20 -080050 private final Switchboard mSwitchboard;
51
52 /** Used to control the in-call app. */
53 private final InCallController mInCallController;
54
Santos Cordon4e9fffe2014-03-04 18:13:41 -080055 private final Ringer mRinger;
56
Santos Cordon8e8b8d22013-12-19 14:14:05 -080057 /**
Santos Cordon681663d2014-01-30 04:32:15 -080058 * The main call repository. Keeps an instance of all live calls keyed by call ID. New incoming
59 * and outgoing calls are added to the map and removed when the calls move to the disconnected
60 * state.
61 * TODO(santoscordon): Add new CallId class and use it in place of String.
62 */
63 private final Map<String, Call> mCalls = Maps.newHashMap();
64
65 /**
Santos Cordon4e9fffe2014-03-04 18:13:41 -080066 * Used to keep ordering of unanswered incoming calls. The existence of multiple call services
67 * means that there can easily exist multiple incoming calls and explicit ordering is useful for
68 * maintaining the proper state of the ringer.
69 * TODO(santoscordon): May want to add comments about ITelephony.answerCall() method since
70 * ordering may also apply to that case.
71 */
72 private final List<Call> mUnansweredIncomingCalls = Lists.newLinkedList();
73
74 /**
Santos Cordon8e8b8d22013-12-19 14:14:05 -080075 * May be unnecessary per off-line discussions (between santoscordon and gilad) since the set
76 * of CallsManager APIs that need to be exposed to the dialer (or any application firing call
77 * intents) may be empty.
78 */
79 private DialerAdapter mDialerAdapter;
Ben Gilad9f2bed32013-12-12 17:43:26 -080080
Santos Cordon8e8b8d22013-12-19 14:14:05 -080081 private InCallAdapter mInCallAdapter;
Ben Gilad9f2bed32013-12-12 17:43:26 -080082
Santos Cordon8e8b8d22013-12-19 14:14:05 -080083 private CallLogManager mCallLogManager;
Ben Gilad9f2bed32013-12-12 17:43:26 -080084
Santos Cordon8e8b8d22013-12-19 14:14:05 -080085 private VoicemailManager mVoicemailManager;
Ben Gilad9f2bed32013-12-12 17:43:26 -080086
Evan Charltonf02e9882014-03-06 12:54:52 -080087 private final List<OutgoingCallValidator> mOutgoingCallValidators = Lists.newArrayList();
Ben Gilad9f2bed32013-12-12 17:43:26 -080088
Evan Charltonf02e9882014-03-06 12:54:52 -080089 private final List<IncomingCallValidator> mIncomingCallValidators = Lists.newArrayList();
Ben Gilad9f2bed32013-12-12 17:43:26 -080090
Santos Cordon8e8b8d22013-12-19 14:14:05 -080091 /**
Ben Gilad8bdaa462014-02-05 12:53:19 -080092 * Initializes the required Telecomm components.
Santos Cordon8e8b8d22013-12-19 14:14:05 -080093 */
94 private CallsManager() {
Santos Cordon6242b132014-02-07 16:24:42 -080095 mSwitchboard = new Switchboard(this);
96 mInCallController = new InCallController(this);
Santos Cordon4e9fffe2014-03-04 18:13:41 -080097 mRinger = new Ringer();
Yorke Leef98fb572014-03-05 10:56:55 -080098 mCallLogManager = new CallLogManager(TelecommApp.getInstance());
Santos Cordon8e8b8d22013-12-19 14:14:05 -080099 }
100
Ben Gilada0d9f752014-02-26 11:49:03 -0800101 static CallsManager getInstance() {
102 return INSTANCE;
103 }
104
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800105 /**
Santos Cordon493e8f22014-02-19 03:15:12 -0800106 * Starts the incoming call sequence by having switchboard gather more information about the
107 * specified call; using the specified call service descriptor. Upon success, execution returns
108 * to {@link #handleSuccessfulIncomingCall} to start the in-call UI.
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800109 *
Ben Giladc5b22692014-02-18 20:03:22 -0800110 * @param descriptor The descriptor of the call service to use for this incoming call.
Evan Charltona05805b2014-03-05 08:21:46 -0800111 * @param extras The optional extras Bundle passed with the intent used for the incoming call.
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800112 */
Evan Charltona05805b2014-03-05 08:21:46 -0800113 void processIncomingCallIntent(CallServiceDescriptor descriptor, Bundle extras) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800114 Log.d(this, "processIncomingCallIntent");
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800115 // Create a call with no handle. Eventually, switchboard will update the call with
116 // additional information from the call service, but for now we just need one to pass around
117 // with a unique call ID.
Santos Cordon493e8f22014-02-19 03:15:12 -0800118 Call call = new Call();
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800119
Evan Charltona05805b2014-03-05 08:21:46 -0800120 mSwitchboard.retrieveIncomingCall(call, descriptor, extras);
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800121 }
122
123 /**
Ben Gilada0d9f752014-02-26 11:49:03 -0800124 * Validates the specified call and, upon no objection to connect it, adds the new call to the
125 * list of live calls. Also notifies the in-call app so the user can answer or reject the call.
126 *
127 * @param call The new incoming call.
128 */
129 void handleSuccessfulIncomingCall(Call call) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800130 Log.d(this, "handleSuccessfulIncomingCall");
Ben Gilada0d9f752014-02-26 11:49:03 -0800131 Preconditions.checkState(call.getState() == CallState.RINGING);
132
133 String handle = call.getHandle();
134 ContactInfo contactInfo = call.getContactInfo();
135 for (IncomingCallValidator validator : mIncomingCallValidators) {
136 if (!validator.isValid(handle, contactInfo)) {
137 // TODO(gilad): Consider displaying an error message.
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800138 Log.i(this, "Dropping restricted incoming call");
Ben Gilada0d9f752014-02-26 11:49:03 -0800139 return;
140 }
141 }
142
143 // No objection to accept the incoming call, proceed with potentially connecting it (based
144 // on the user's action, or lack thereof).
145 addCall(call);
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800146
147 mUnansweredIncomingCalls.add(call);
148 if (mUnansweredIncomingCalls.size() == 1) {
149 // Start the ringer if we are the top-most incoming call (the only one in this case).
150 mRinger.startRinging();
151 }
Ben Gilada0d9f752014-02-26 11:49:03 -0800152 }
153
154 /**
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800155 * Attempts to issue/connect the specified call. From an (arbitrary) application standpoint,
156 * all that is required to initiate this flow is to fire either of the CALL, CALL_PRIVILEGED,
157 * and CALL_EMERGENCY intents. These are listened to by CallActivity.java which then invokes
158 * this method.
159 *
160 * @param handle The handle to dial.
161 * @param contactInfo Information about the entity being called.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800162 */
Ben Gilada0d9f752014-02-26 11:49:03 -0800163 void processOutgoingCallIntent(String handle, ContactInfo contactInfo) {
Ben Gilad8bdaa462014-02-05 12:53:19 -0800164 for (OutgoingCallValidator validator : mOutgoingCallValidators) {
Ben Gilada0d9f752014-02-26 11:49:03 -0800165 if (!validator.isValid(handle, contactInfo)) {
166 // TODO(gilad): Display an error message.
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800167 Log.i(this, "Dropping restricted outgoing call.");
Ben Gilada0d9f752014-02-26 11:49:03 -0800168 return;
169 }
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800170 }
171
172 // No objection to issue the call, proceed with trying to put it through.
Ben Gilad13329fd2014-02-11 17:20:29 -0800173 Call call = new Call(handle, contactInfo);
Santos Cordonc195e362014-02-11 17:05:31 -0800174 mSwitchboard.placeOutgoingCall(call);
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800175 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800176
177 /**
Santos Cordon681663d2014-01-30 04:32:15 -0800178 * Adds a new outgoing call to the list of live calls and notifies the in-call app.
179 *
180 * @param call The new outgoing call.
181 */
182 void handleSuccessfulOutgoingCall(Call call) {
183 // OutgoingCallProcessor sets the call state to DIALING when it receives confirmation of the
184 // placed call from the call service so there is no need to set it here. Instead, check that
185 // the state is appropriate.
186 Preconditions.checkState(call.getState() == CallState.DIALING);
Santos Cordon681663d2014-01-30 04:32:15 -0800187 addCall(call);
Santos Cordon681663d2014-01-30 04:32:15 -0800188 }
189
190 /**
Yorke Leef98fb572014-03-05 10:56:55 -0800191 * Informs mCallLogManager about the outgoing call that failed, so that it can be logged.
192 *
193 * @param call The failed outgoing call.
194 */
195 void handleFailedOutgoingCall(Call call) {
196 mCallLogManager.logFailedOutgoingCall(call);
197 }
198
199 /**
Santos Cordone3d76ab2014-01-28 17:25:20 -0800200 * Instructs Telecomm to answer the specified call. Intended to be invoked by the in-call
201 * app through {@link InCallAdapter} after Telecomm notifies it of an incoming call followed by
202 * the user opting to answer said call.
203 *
204 * @param callId The ID of the call.
205 */
206 void answerCall(String callId) {
Santos Cordon61d0f702014-02-19 02:52:23 -0800207 Call call = mCalls.get(callId);
208 if (call == null) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800209 Log.i(this, "Request to answer a non-existent call %s", callId);
Santos Cordon61d0f702014-02-19 02:52:23 -0800210 } else {
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800211 stopRinging(call);
212
Santos Cordon61d0f702014-02-19 02:52:23 -0800213 // We do not update the UI until we get confirmation of the answer() through
214 // {@link #markCallAsActive}. However, if we ever change that to look more responsive,
215 // then we need to make sure we add a timeout for the answer() in case the call never
216 // comes out of RINGING.
217 call.answer();
218 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800219 }
220
221 /**
222 * Instructs Telecomm to reject the specified call. Intended to be invoked by the in-call
223 * app through {@link InCallAdapter} after Telecomm notifies it of an incoming call followed by
224 * the user opting to reject said call.
225 *
226 * @param callId The ID of the call.
227 */
228 void rejectCall(String callId) {
Santos Cordon61d0f702014-02-19 02:52:23 -0800229 Call call = mCalls.get(callId);
230 if (call == null) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800231 Log.i(this, "Request to reject a non-existent call %s", callId);
Santos Cordon61d0f702014-02-19 02:52:23 -0800232 } else {
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800233 stopRinging(call);
Santos Cordon61d0f702014-02-19 02:52:23 -0800234 call.reject();
235 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800236 }
237
238 /**
239 * Instructs Telecomm to disconnect the specified call. Intended to be invoked by the
240 * in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered by
241 * the user hitting the end-call button.
242 *
243 * @param callId The ID of the call.
244 */
245 void disconnectCall(String callId) {
Santos Cordon049b7b62014-01-30 05:34:26 -0800246 Call call = mCalls.get(callId);
247 if (call == null) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800248 Log.w(this, "Unknown call (%s) asked to disconnect", callId);
Santos Cordon049b7b62014-01-30 05:34:26 -0800249 } else {
250 call.disconnect();
251 }
252
Sailesh Nepal1cecc7c2014-03-10 17:03:08 -0700253 // TODO(sail): Replace with CallAudioManager.
254 Log.v(this, "disconnectCall, abandoning audio focus");
255 Context context = TelecommApp.getInstance().getApplicationContext();
256 AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
257 audioManager.setMode(AudioManager.MODE_NORMAL);
258 audioManager.abandonAudioFocusForCall();
Santos Cordone3d76ab2014-01-28 17:25:20 -0800259 }
Santos Cordon681663d2014-01-30 04:32:15 -0800260
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700261 /**
262 * Instructs Telecomm to put the specified call on hold. Intended to be invoked by the
263 * in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered by
264 * the user hitting the hold button during an active call.
265 *
266 * @param callId The ID of the call.
267 */
268 void holdCall(String callId) {
269 Call call = mCalls.get(callId);
270 if (call == null) {
271 Log.w(this, "Unknown call (%s) asked to be put on hold", callId);
272 } else {
273 Log.d(this, "Putting call on hold: (%s)", callId);
274 call.hold();
275 }
276 }
277
278 /**
279 * Instructs Telecomm to release the specified call from hold. Intended to be invoked by
280 * the in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered
281 * by the user hitting the hold button during a held call.
282 *
283 * @param callId The ID of the call
284 */
285 void unholdCall(String callId) {
286 Call call = mCalls.get(callId);
287 if (call == null) {
288 Log.w(this, "Unknown call (%s) asked to be removed from hold", callId);
289 } else {
290 Log.d(this, "Removing call from hold: (%s)", callId);
291 call.unhold();
292 }
293 }
294
Santos Cordon681663d2014-01-30 04:32:15 -0800295 void markCallAsRinging(String callId) {
296 setCallState(callId, CallState.RINGING);
297 }
298
299 void markCallAsDialing(String callId) {
300 setCallState(callId, CallState.DIALING);
301 }
302
303 void markCallAsActive(String callId) {
304 setCallState(callId, CallState.ACTIVE);
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800305 removeFromUnansweredCalls(callId);
Sailesh Nepalfc511ca2014-02-20 18:48:53 -0800306 mInCallController.markCallAsActive(callId);
Sailesh Nepal1cecc7c2014-03-10 17:03:08 -0700307
308 // TODO(sail): Replace with CallAudioManager.
309 Log.v(this, "markCallAsActive, requesting audio focus");
310 Context context = TelecommApp.getInstance().getApplicationContext();
311 AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
312 audioManager.requestAudioFocusForCall(AudioManager.STREAM_VOICE_CALL,
313 AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);
314 audioManager.setMode(AudioManager.MODE_IN_CALL);
315 audioManager.setMicrophoneMute(false);
316 audioManager.setSpeakerphoneOn(false);
Santos Cordon681663d2014-01-30 04:32:15 -0800317 }
318
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700319 void markCallAsOnHold(String callId) {
320 setCallState(callId, CallState.ON_HOLD);
321
322 // Notify the in-call UI
323 mInCallController.markCallAsOnHold(callId);
324 }
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);
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800334 removeFromUnansweredCalls(callId);
Santos Cordonc195e362014-02-11 17:05:31 -0800335
336 Call call = mCalls.remove(callId);
337 // At this point the call service has confirmed that the call is disconnected to it is
338 // safe to disassociate the call from its call service.
339 call.clearCallService();
Santos Cordon049b7b62014-01-30 05:34:26 -0800340
341 // Notify the in-call UI
342 mInCallController.markCallAsDisconnected(callId);
343 if (mCalls.isEmpty()) {
344 mInCallController.unbind();
345 }
Yorke Leef98fb572014-03-05 10:56:55 -0800346
347 // Log the call in the call log.
348 mCallLogManager.logDisconnectedCall(call);
Santos Cordon681663d2014-01-30 04:32:15 -0800349 }
350
351 /**
Ben Gilada0d9f752014-02-26 11:49:03 -0800352 * Sends all the live calls to the in-call app if any exist. If there are no live calls, then
353 * tells the in-call controller to unbind since it is not needed.
354 */
355 void updateInCall() {
356 if (mCalls.isEmpty()) {
357 mInCallController.unbind();
358 } else {
359 for (Call call : mCalls.values()) {
360 addInCallEntry(call);
361 }
362 }
363 }
364
365 /**
366 * Adds the specified call to the main list of live calls.
367 *
368 * @param call The call to add.
369 */
370 private void addCall(Call call) {
371 mCalls.put(call.getId(), call);
372 addInCallEntry(call);
373 }
374
375 /**
376 * Notifies the in-call app of the specified (new) call.
377 */
378 private void addInCallEntry(Call call) {
379 mInCallController.addCall(call.toCallInfo());
380 }
381
382 /**
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800383 * Sets the specified state on the specified call. Updates the ringer if the call is exiting
384 * the RINGING state.
Santos Cordon681663d2014-01-30 04:32:15 -0800385 *
386 * @param callId The ID of the call to update.
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800387 * @param newState The new state of the call.
Santos Cordon681663d2014-01-30 04:32:15 -0800388 */
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800389 private void setCallState(String callId, CallState newState) {
Santos Cordon681663d2014-01-30 04:32:15 -0800390 Preconditions.checkState(!Strings.isNullOrEmpty(callId));
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800391 Preconditions.checkNotNull(newState);
Santos Cordon681663d2014-01-30 04:32:15 -0800392
393 Call call = mCalls.get(callId);
394 if (call == null) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800395 Log.w(this, "Call %s was not found while attempting to update the state to %s.",
396 callId, newState );
Santos Cordon681663d2014-01-30 04:32:15 -0800397 } else {
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800398 if (newState != call.getState()) {
399 // Unfortunately, in the telephony world the radio is king. So if the call notifies
400 // us that the call is in a particular state, we allow it even if it doesn't make
401 // sense (e.g., ACTIVE -> RINGING).
402 // TODO(santoscordon): Consider putting a stop to the above and turning CallState
403 // into a well-defined state machine.
404 // TODO(santoscordon): Define expected state transitions here, and log when an
405 // unexpected transition occurs.
406 call.setState(newState);
407 // TODO(santoscordon): Notify the in-call app whenever a call changes state.
Evan Charlton5c670a92014-03-06 14:58:20 -0800408
409 broadcastState(call);
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800410 }
411 }
412 }
413
414 /**
Evan Charlton5c670a92014-03-06 14:58:20 -0800415 * Send a {@link TelephonyManager#ACTION_PHONE_STATE_CHANGED} broadcast when the call state
416 * changes. TODO: Split this out into a separate class and do it properly; this is just a first
417 * pass over the functionality.
418 *
419 * @param call The {@link Call} being updated.
420 */
421 private void broadcastState(Call call) {
422 final String callState;
423 switch (call.getState()) {
424 case DIALING:
425 case ACTIVE:
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700426 case ON_HOLD:
Evan Charlton5c670a92014-03-06 14:58:20 -0800427 callState = TelephonyManager.EXTRA_STATE_OFFHOOK;
428 break;
429
430 case RINGING:
431 callState = TelephonyManager.EXTRA_STATE_RINGING;
432 break;
433
434 case DISCONNECTED:
435 case ABORTED:
436 callState = TelephonyManager.EXTRA_STATE_IDLE;
437 break;
438
439 default:
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800440 Log.w(this, "Call is in an unknown state (%s), not broadcasting: %s", call.getState(),
441 call.getId());
Evan Charlton5c670a92014-03-06 14:58:20 -0800442 return;
443 }
444
445 Intent updateIntent = new Intent(TelephonyManager.ACTION_PHONE_STATE_CHANGED);
446 updateIntent.putExtra(TelephonyManager.EXTRA_STATE, callState);
Sailesh Nepal85680bf2014-03-13 11:56:30 -0700447 // TODO: See if we can add this (the current API doesn't have a callId).
448 updateIntent.putExtra(TelecommConstants.EXTRA_CALL_ID, call.getId());
Evan Charlton5c670a92014-03-06 14:58:20 -0800449
450 // Populate both, since the original API was needlessly complicated.
451 updateIntent.putExtra(TelephonyManager.EXTRA_INCOMING_NUMBER, call.getHandle());
452 // TODO: See if we can add this (the current API only sets this on NEW_OUTGOING_CALL).
453 updateIntent.putExtra(Intent.EXTRA_PHONE_NUMBER, call.getHandle());
454
455 // TODO: Replace these with real constants once this API has been vetted.
456 CallServiceWrapper callService = call.getCallService();
457 if (callService != null) {
458 updateIntent.putExtra(CallService.class.getName(), callService.getComponentName());
459 }
460 TelecommApp.getInstance().sendBroadcast(updateIntent, Manifest.permission.READ_PHONE_STATE);
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800461 Log.i(this, "Broadcasted state change: %s", callState);
Evan Charlton5c670a92014-03-06 14:58:20 -0800462 }
463
464 /**
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800465 * Removes the specified call from the list of unanswered incoming calls and updates the ringer
466 * based on the new state of {@link #mUnansweredIncomingCalls}. Safe to call with a call ID that
467 * is not present in the list of incoming calls.
468 *
469 * @param callId The ID of the call.
470 */
471 private void removeFromUnansweredCalls(String callId) {
472 Call call = mCalls.get(callId);
473 if (call != null && mUnansweredIncomingCalls.remove(call)) {
474 if (mUnansweredIncomingCalls.isEmpty()) {
475 mRinger.stopRinging();
476 } else {
477 mRinger.startRinging();
478 }
479 }
480 }
481
482 /**
483 * Stops playing the ringer if the specified call is the top-most incoming call. This exists
Ben Gilad61925612014-03-11 19:06:36 -0700484 * separately from {@link #removeFromUnansweredCalls} to allow stopping the ringer for calls
485 * that should remain in {@link #mUnansweredIncomingCalls}, invoked from {@link #answerCall}
486 * and {@link #rejectCall}.
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800487 *
488 * @param call The call for which we should stop ringing.
489 */
490 private void stopRinging(Call call) {
491 // Only stop the ringer if this call is the top-most incoming call.
492 if (!mUnansweredIncomingCalls.isEmpty() && mUnansweredIncomingCalls.get(0) == call) {
493 mRinger.stopRinging();
Santos Cordon681663d2014-01-30 04:32:15 -0800494 }
495 }
Ben Gilad9f2bed32013-12-12 17:43:26 -0800496}