blob: 26f7ac1b9fb6386e92f47125efe6a8529c935d13 [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
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700262 /**
263 * Instructs Telecomm to put the specified call on hold. Intended to be invoked by the
264 * in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered by
265 * the user hitting the hold button during an active call.
266 *
267 * @param callId The ID of the call.
268 */
269 void holdCall(String callId) {
270 Call call = mCalls.get(callId);
271 if (call == null) {
272 Log.w(this, "Unknown call (%s) asked to be put on hold", callId);
273 } else {
274 Log.d(this, "Putting call on hold: (%s)", callId);
275 call.hold();
276 }
277 }
278
279 /**
280 * Instructs Telecomm to release the specified call from hold. Intended to be invoked by
281 * the in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered
282 * by the user hitting the hold button during a held call.
283 *
284 * @param callId The ID of the call
285 */
286 void unholdCall(String callId) {
287 Call call = mCalls.get(callId);
288 if (call == null) {
289 Log.w(this, "Unknown call (%s) asked to be removed from hold", callId);
290 } else {
291 Log.d(this, "Removing call from hold: (%s)", callId);
292 call.unhold();
293 }
294 }
295
Santos Cordon681663d2014-01-30 04:32:15 -0800296 void markCallAsRinging(String callId) {
297 setCallState(callId, CallState.RINGING);
298 }
299
300 void markCallAsDialing(String callId) {
301 setCallState(callId, CallState.DIALING);
302 }
303
304 void markCallAsActive(String callId) {
305 setCallState(callId, CallState.ACTIVE);
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800306 removeFromUnansweredCalls(callId);
Sailesh Nepalfc511ca2014-02-20 18:48:53 -0800307 mInCallController.markCallAsActive(callId);
Sailesh Nepal1cecc7c2014-03-10 17:03:08 -0700308
309 // TODO(sail): Replace with CallAudioManager.
310 Log.v(this, "markCallAsActive, requesting audio focus");
311 Context context = TelecommApp.getInstance().getApplicationContext();
312 AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
313 audioManager.requestAudioFocusForCall(AudioManager.STREAM_VOICE_CALL,
314 AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);
315 audioManager.setMode(AudioManager.MODE_IN_CALL);
316 audioManager.setMicrophoneMute(false);
317 audioManager.setSpeakerphoneOn(false);
Santos Cordon681663d2014-01-30 04:32:15 -0800318 }
319
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700320 void markCallAsOnHold(String callId) {
321 setCallState(callId, CallState.ON_HOLD);
322
323 // Notify the in-call UI
324 mInCallController.markCallAsOnHold(callId);
325 }
326
Santos Cordon049b7b62014-01-30 05:34:26 -0800327 /**
328 * Marks the specified call as DISCONNECTED and notifies the in-call app. If this was the last
329 * live call, then also disconnect from the in-call controller.
330 *
331 * @param callId The ID of the call.
332 */
Santos Cordon681663d2014-01-30 04:32:15 -0800333 void markCallAsDisconnected(String callId) {
334 setCallState(callId, CallState.DISCONNECTED);
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800335 removeFromUnansweredCalls(callId);
Santos Cordonc195e362014-02-11 17:05:31 -0800336
337 Call call = mCalls.remove(callId);
338 // At this point the call service has confirmed that the call is disconnected to it is
339 // safe to disassociate the call from its call service.
340 call.clearCallService();
Santos Cordon049b7b62014-01-30 05:34:26 -0800341
342 // Notify the in-call UI
343 mInCallController.markCallAsDisconnected(callId);
344 if (mCalls.isEmpty()) {
345 mInCallController.unbind();
346 }
Yorke Leef98fb572014-03-05 10:56:55 -0800347
348 // Log the call in the call log.
349 mCallLogManager.logDisconnectedCall(call);
Santos Cordon681663d2014-01-30 04:32:15 -0800350 }
351
352 /**
Ben Gilada0d9f752014-02-26 11:49:03 -0800353 * Sends all the live calls to the in-call app if any exist. If there are no live calls, then
354 * tells the in-call controller to unbind since it is not needed.
355 */
356 void updateInCall() {
357 if (mCalls.isEmpty()) {
358 mInCallController.unbind();
359 } else {
360 for (Call call : mCalls.values()) {
361 addInCallEntry(call);
362 }
363 }
364 }
365
366 /**
367 * Adds the specified call to the main list of live calls.
368 *
369 * @param call The call to add.
370 */
371 private void addCall(Call call) {
372 mCalls.put(call.getId(), call);
373 addInCallEntry(call);
374 }
375
376 /**
377 * Notifies the in-call app of the specified (new) call.
378 */
379 private void addInCallEntry(Call call) {
380 mInCallController.addCall(call.toCallInfo());
381 }
382
383 /**
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800384 * Sets the specified state on the specified call. Updates the ringer if the call is exiting
385 * the RINGING state.
Santos Cordon681663d2014-01-30 04:32:15 -0800386 *
387 * @param callId The ID of the call to update.
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800388 * @param newState The new state of the call.
Santos Cordon681663d2014-01-30 04:32:15 -0800389 */
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800390 private void setCallState(String callId, CallState newState) {
Santos Cordon681663d2014-01-30 04:32:15 -0800391 Preconditions.checkState(!Strings.isNullOrEmpty(callId));
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800392 Preconditions.checkNotNull(newState);
Santos Cordon681663d2014-01-30 04:32:15 -0800393
394 Call call = mCalls.get(callId);
395 if (call == null) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800396 Log.w(this, "Call %s was not found while attempting to update the state to %s.",
397 callId, newState );
Santos Cordon681663d2014-01-30 04:32:15 -0800398 } else {
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800399 if (newState != call.getState()) {
400 // Unfortunately, in the telephony world the radio is king. So if the call notifies
401 // us that the call is in a particular state, we allow it even if it doesn't make
402 // sense (e.g., ACTIVE -> RINGING).
403 // TODO(santoscordon): Consider putting a stop to the above and turning CallState
404 // into a well-defined state machine.
405 // TODO(santoscordon): Define expected state transitions here, and log when an
406 // unexpected transition occurs.
407 call.setState(newState);
408 // TODO(santoscordon): Notify the in-call app whenever a call changes state.
Evan Charlton5c670a92014-03-06 14:58:20 -0800409
410 broadcastState(call);
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800411 }
412 }
413 }
414
415 /**
Evan Charlton5c670a92014-03-06 14:58:20 -0800416 * Send a {@link TelephonyManager#ACTION_PHONE_STATE_CHANGED} broadcast when the call state
417 * changes. TODO: Split this out into a separate class and do it properly; this is just a first
418 * pass over the functionality.
419 *
420 * @param call The {@link Call} being updated.
421 */
422 private void broadcastState(Call call) {
423 final String callState;
424 switch (call.getState()) {
425 case DIALING:
426 case ACTIVE:
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700427 case ON_HOLD:
Evan Charlton5c670a92014-03-06 14:58:20 -0800428 callState = TelephonyManager.EXTRA_STATE_OFFHOOK;
429 break;
430
431 case RINGING:
432 callState = TelephonyManager.EXTRA_STATE_RINGING;
433 break;
434
435 case DISCONNECTED:
436 case ABORTED:
437 callState = TelephonyManager.EXTRA_STATE_IDLE;
438 break;
439
440 default:
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800441 Log.w(this, "Call is in an unknown state (%s), not broadcasting: %s", call.getState(),
442 call.getId());
Evan Charlton5c670a92014-03-06 14:58:20 -0800443 return;
444 }
445
446 Intent updateIntent = new Intent(TelephonyManager.ACTION_PHONE_STATE_CHANGED);
447 updateIntent.putExtra(TelephonyManager.EXTRA_STATE, callState);
Sailesh Nepal85680bf2014-03-13 11:56:30 -0700448 // TODO: See if we can add this (the current API doesn't have a callId).
449 updateIntent.putExtra(TelecommConstants.EXTRA_CALL_ID, call.getId());
Evan Charlton5c670a92014-03-06 14:58:20 -0800450
451 // Populate both, since the original API was needlessly complicated.
452 updateIntent.putExtra(TelephonyManager.EXTRA_INCOMING_NUMBER, call.getHandle());
453 // TODO: See if we can add this (the current API only sets this on NEW_OUTGOING_CALL).
454 updateIntent.putExtra(Intent.EXTRA_PHONE_NUMBER, call.getHandle());
455
456 // TODO: Replace these with real constants once this API has been vetted.
457 CallServiceWrapper callService = call.getCallService();
458 if (callService != null) {
459 updateIntent.putExtra(CallService.class.getName(), callService.getComponentName());
460 }
461 TelecommApp.getInstance().sendBroadcast(updateIntent, Manifest.permission.READ_PHONE_STATE);
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800462 Log.i(this, "Broadcasted state change: %s", callState);
Evan Charlton5c670a92014-03-06 14:58:20 -0800463 }
464
465 /**
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800466 * Removes the specified call from the list of unanswered incoming calls and updates the ringer
467 * based on the new state of {@link #mUnansweredIncomingCalls}. Safe to call with a call ID that
468 * is not present in the list of incoming calls.
469 *
470 * @param callId The ID of the call.
471 */
472 private void removeFromUnansweredCalls(String callId) {
473 Call call = mCalls.get(callId);
474 if (call != null && mUnansweredIncomingCalls.remove(call)) {
475 if (mUnansweredIncomingCalls.isEmpty()) {
476 mRinger.stopRinging();
477 } else {
478 mRinger.startRinging();
479 }
480 }
481 }
482
483 /**
484 * Stops playing the ringer if the specified call is the top-most incoming call. This exists
Ben Gilad61925612014-03-11 19:06:36 -0700485 * separately from {@link #removeFromUnansweredCalls} to allow stopping the ringer for calls
486 * that should remain in {@link #mUnansweredIncomingCalls}, invoked from {@link #answerCall}
487 * and {@link #rejectCall}.
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800488 *
489 * @param call The call for which we should stop ringing.
490 */
491 private void stopRinging(Call call) {
492 // Only stop the ringer if this call is the top-most incoming call.
493 if (!mUnansweredIncomingCalls.isEmpty() && mUnansweredIncomingCalls.get(0) == call) {
494 mRinger.stopRinging();
Santos Cordon681663d2014-01-30 04:32:15 -0800495 }
496 }
Ben Gilad9f2bed32013-12-12 17:43:26 -0800497}