blob: 5cb879f05e0867a645834d5a0b6607a9142a832e [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;
20import android.content.Intent;
Evan Charltona05805b2014-03-05 08:21:46 -080021import android.os.Bundle;
Evan Charlton5c670a92014-03-06 14:58:20 -080022import android.telecomm.CallService;
Ben Giladc5b22692014-02-18 20:03:22 -080023import android.telecomm.CallServiceDescriptor;
Santos Cordon681663d2014-01-30 04:32:15 -080024import android.telecomm.CallState;
Evan Charlton5c670a92014-03-06 14:58:20 -080025import android.telecomm.ICallService;
26import android.telephony.TelephonyManager;
Santos Cordon8e8b8d22013-12-19 14:14:05 -080027
Santos Cordon681663d2014-01-30 04:32:15 -080028import com.google.common.base.Preconditions;
29import com.google.common.base.Strings;
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
Santos Cordon8e8b8d22013-12-19 14:14:05 -080045 private static final CallsManager INSTANCE = new CallsManager();
Ben Gilad9f2bed32013-12-12 17:43:26 -080046
Santos Cordone3d76ab2014-01-28 17:25:20 -080047 private final Switchboard mSwitchboard;
48
49 /** Used to control the in-call app. */
50 private final InCallController mInCallController;
51
Santos Cordon4e9fffe2014-03-04 18:13:41 -080052 private final Ringer mRinger;
53
Santos Cordon8e8b8d22013-12-19 14:14:05 -080054 /**
Santos Cordon681663d2014-01-30 04:32:15 -080055 * The main call repository. Keeps an instance of all live calls keyed by call ID. New incoming
56 * and outgoing calls are added to the map and removed when the calls move to the disconnected
57 * state.
58 * TODO(santoscordon): Add new CallId class and use it in place of String.
59 */
60 private final Map<String, Call> mCalls = Maps.newHashMap();
61
62 /**
Santos Cordon4e9fffe2014-03-04 18:13:41 -080063 * Used to keep ordering of unanswered incoming calls. The existence of multiple call services
64 * means that there can easily exist multiple incoming calls and explicit ordering is useful for
65 * maintaining the proper state of the ringer.
66 * TODO(santoscordon): May want to add comments about ITelephony.answerCall() method since
67 * ordering may also apply to that case.
68 */
69 private final List<Call> mUnansweredIncomingCalls = Lists.newLinkedList();
70
71 /**
Santos Cordon8e8b8d22013-12-19 14:14:05 -080072 * May be unnecessary per off-line discussions (between santoscordon and gilad) since the set
73 * of CallsManager APIs that need to be exposed to the dialer (or any application firing call
74 * intents) may be empty.
75 */
76 private DialerAdapter mDialerAdapter;
Ben Gilad9f2bed32013-12-12 17:43:26 -080077
Santos Cordon8e8b8d22013-12-19 14:14:05 -080078 private InCallAdapter mInCallAdapter;
Ben Gilad9f2bed32013-12-12 17:43:26 -080079
Santos Cordon8e8b8d22013-12-19 14:14:05 -080080 private CallLogManager mCallLogManager;
Ben Gilad9f2bed32013-12-12 17:43:26 -080081
Santos Cordon8e8b8d22013-12-19 14:14:05 -080082 private VoicemailManager mVoicemailManager;
Ben Gilad9f2bed32013-12-12 17:43:26 -080083
Evan Charltonf02e9882014-03-06 12:54:52 -080084 private final List<OutgoingCallValidator> mOutgoingCallValidators = Lists.newArrayList();
Ben Gilad9f2bed32013-12-12 17:43:26 -080085
Evan Charltonf02e9882014-03-06 12:54:52 -080086 private final List<IncomingCallValidator> mIncomingCallValidators = Lists.newArrayList();
Ben Gilad9f2bed32013-12-12 17:43:26 -080087
Santos Cordon8e8b8d22013-12-19 14:14:05 -080088 /**
Ben Gilad8bdaa462014-02-05 12:53:19 -080089 * Initializes the required Telecomm components.
Santos Cordon8e8b8d22013-12-19 14:14:05 -080090 */
91 private CallsManager() {
Santos Cordon6242b132014-02-07 16:24:42 -080092 mSwitchboard = new Switchboard(this);
93 mInCallController = new InCallController(this);
Santos Cordon4e9fffe2014-03-04 18:13:41 -080094 mRinger = new Ringer();
Santos Cordon8e8b8d22013-12-19 14:14:05 -080095 }
96
Ben Gilada0d9f752014-02-26 11:49:03 -080097 static CallsManager getInstance() {
98 return INSTANCE;
99 }
100
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800101 /**
Santos Cordon493e8f22014-02-19 03:15:12 -0800102 * Starts the incoming call sequence by having switchboard gather more information about the
103 * specified call; using the specified call service descriptor. Upon success, execution returns
104 * to {@link #handleSuccessfulIncomingCall} to start the in-call UI.
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800105 *
Ben Giladc5b22692014-02-18 20:03:22 -0800106 * @param descriptor The descriptor of the call service to use for this incoming call.
Evan Charltona05805b2014-03-05 08:21:46 -0800107 * @param extras The optional extras Bundle passed with the intent used for the incoming call.
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800108 */
Evan Charltona05805b2014-03-05 08:21:46 -0800109 void processIncomingCallIntent(CallServiceDescriptor descriptor, Bundle extras) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800110 Log.d(this, "processIncomingCallIntent");
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800111 // Create a call with no handle. Eventually, switchboard will update the call with
112 // additional information from the call service, but for now we just need one to pass around
113 // with a unique call ID.
Santos Cordon493e8f22014-02-19 03:15:12 -0800114 Call call = new Call();
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800115
Evan Charltona05805b2014-03-05 08:21:46 -0800116 mSwitchboard.retrieveIncomingCall(call, descriptor, extras);
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800117 }
118
119 /**
Ben Gilada0d9f752014-02-26 11:49:03 -0800120 * Validates the specified call and, upon no objection to connect it, adds the new call to the
121 * list of live calls. Also notifies the in-call app so the user can answer or reject the call.
122 *
123 * @param call The new incoming call.
124 */
125 void handleSuccessfulIncomingCall(Call call) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800126 Log.d(this, "handleSuccessfulIncomingCall");
Ben Gilada0d9f752014-02-26 11:49:03 -0800127 Preconditions.checkState(call.getState() == CallState.RINGING);
128
129 String handle = call.getHandle();
130 ContactInfo contactInfo = call.getContactInfo();
131 for (IncomingCallValidator validator : mIncomingCallValidators) {
132 if (!validator.isValid(handle, contactInfo)) {
133 // TODO(gilad): Consider displaying an error message.
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800134 Log.i(this, "Dropping restricted incoming call");
Ben Gilada0d9f752014-02-26 11:49:03 -0800135 return;
136 }
137 }
138
139 // No objection to accept the incoming call, proceed with potentially connecting it (based
140 // on the user's action, or lack thereof).
141 addCall(call);
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800142
143 mUnansweredIncomingCalls.add(call);
144 if (mUnansweredIncomingCalls.size() == 1) {
145 // Start the ringer if we are the top-most incoming call (the only one in this case).
146 mRinger.startRinging();
147 }
Ben Gilada0d9f752014-02-26 11:49:03 -0800148 }
149
150 /**
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800151 * Attempts to issue/connect the specified call. From an (arbitrary) application standpoint,
152 * all that is required to initiate this flow is to fire either of the CALL, CALL_PRIVILEGED,
153 * and CALL_EMERGENCY intents. These are listened to by CallActivity.java which then invokes
154 * this method.
155 *
156 * @param handle The handle to dial.
157 * @param contactInfo Information about the entity being called.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800158 */
Ben Gilada0d9f752014-02-26 11:49:03 -0800159 void processOutgoingCallIntent(String handle, ContactInfo contactInfo) {
Ben Gilad8bdaa462014-02-05 12:53:19 -0800160 for (OutgoingCallValidator validator : mOutgoingCallValidators) {
Ben Gilada0d9f752014-02-26 11:49:03 -0800161 if (!validator.isValid(handle, contactInfo)) {
162 // TODO(gilad): Display an error message.
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800163 Log.i(this, "Dropping restricted outgoing call.");
Ben Gilada0d9f752014-02-26 11:49:03 -0800164 return;
165 }
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800166 }
167
168 // No objection to issue the call, proceed with trying to put it through.
Ben Gilad13329fd2014-02-11 17:20:29 -0800169 Call call = new Call(handle, contactInfo);
Santos Cordonc195e362014-02-11 17:05:31 -0800170 mSwitchboard.placeOutgoingCall(call);
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800171 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800172
173 /**
Santos Cordon681663d2014-01-30 04:32:15 -0800174 * Adds a new outgoing call to the list of live calls and notifies the in-call app.
175 *
176 * @param call The new outgoing call.
177 */
178 void handleSuccessfulOutgoingCall(Call call) {
179 // OutgoingCallProcessor sets the call state to DIALING when it receives confirmation of the
180 // placed call from the call service so there is no need to set it here. Instead, check that
181 // the state is appropriate.
182 Preconditions.checkState(call.getState() == CallState.DIALING);
Santos Cordon681663d2014-01-30 04:32:15 -0800183 addCall(call);
Santos Cordon681663d2014-01-30 04:32:15 -0800184 }
185
186 /**
Santos Cordone3d76ab2014-01-28 17:25:20 -0800187 * Instructs Telecomm to answer the specified call. Intended to be invoked by the in-call
188 * app through {@link InCallAdapter} after Telecomm notifies it of an incoming call followed by
189 * the user opting to answer said call.
190 *
191 * @param callId The ID of the call.
192 */
193 void answerCall(String callId) {
Santos Cordon61d0f702014-02-19 02:52:23 -0800194 Call call = mCalls.get(callId);
195 if (call == null) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800196 Log.i(this, "Request to answer a non-existent call %s", callId);
Santos Cordon61d0f702014-02-19 02:52:23 -0800197 } else {
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800198 stopRinging(call);
199
Santos Cordon61d0f702014-02-19 02:52:23 -0800200 // We do not update the UI until we get confirmation of the answer() through
201 // {@link #markCallAsActive}. However, if we ever change that to look more responsive,
202 // then we need to make sure we add a timeout for the answer() in case the call never
203 // comes out of RINGING.
204 call.answer();
205 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800206 }
207
208 /**
209 * Instructs Telecomm to reject the specified call. Intended to be invoked by the in-call
210 * app through {@link InCallAdapter} after Telecomm notifies it of an incoming call followed by
211 * the user opting to reject said call.
212 *
213 * @param callId The ID of the call.
214 */
215 void rejectCall(String callId) {
Santos Cordon61d0f702014-02-19 02:52:23 -0800216 Call call = mCalls.get(callId);
217 if (call == null) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800218 Log.i(this, "Request to reject a non-existent call %s", callId);
Santos Cordon61d0f702014-02-19 02:52:23 -0800219 } else {
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800220 stopRinging(call);
Santos Cordon61d0f702014-02-19 02:52:23 -0800221 call.reject();
222 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800223 }
224
225 /**
226 * Instructs Telecomm to disconnect the specified call. Intended to be invoked by the
227 * in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered by
228 * the user hitting the end-call button.
229 *
230 * @param callId The ID of the call.
231 */
232 void disconnectCall(String callId) {
Santos Cordon049b7b62014-01-30 05:34:26 -0800233 Call call = mCalls.get(callId);
234 if (call == null) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800235 Log.w(this, "Unknown call (%s) asked to disconnect", callId);
Santos Cordon049b7b62014-01-30 05:34:26 -0800236 } else {
237 call.disconnect();
238 }
239
Santos Cordone3d76ab2014-01-28 17:25:20 -0800240 }
Santos Cordon681663d2014-01-30 04:32:15 -0800241
242 void markCallAsRinging(String callId) {
243 setCallState(callId, CallState.RINGING);
244 }
245
246 void markCallAsDialing(String callId) {
247 setCallState(callId, CallState.DIALING);
248 }
249
250 void markCallAsActive(String callId) {
251 setCallState(callId, CallState.ACTIVE);
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800252 removeFromUnansweredCalls(callId);
Sailesh Nepalfc511ca2014-02-20 18:48:53 -0800253 mInCallController.markCallAsActive(callId);
Santos Cordon681663d2014-01-30 04:32:15 -0800254 }
255
Santos Cordon049b7b62014-01-30 05:34:26 -0800256 /**
257 * Marks the specified call as DISCONNECTED and notifies the in-call app. If this was the last
258 * live call, then also disconnect from the in-call controller.
259 *
260 * @param callId The ID of the call.
261 */
Santos Cordon681663d2014-01-30 04:32:15 -0800262 void markCallAsDisconnected(String callId) {
263 setCallState(callId, CallState.DISCONNECTED);
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800264 removeFromUnansweredCalls(callId);
Santos Cordonc195e362014-02-11 17:05:31 -0800265
266 Call call = mCalls.remove(callId);
267 // At this point the call service has confirmed that the call is disconnected to it is
268 // safe to disassociate the call from its call service.
269 call.clearCallService();
Santos Cordon049b7b62014-01-30 05:34:26 -0800270
271 // Notify the in-call UI
272 mInCallController.markCallAsDisconnected(callId);
273 if (mCalls.isEmpty()) {
274 mInCallController.unbind();
275 }
Santos Cordon681663d2014-01-30 04:32:15 -0800276 }
277
278 /**
Ben Gilada0d9f752014-02-26 11:49:03 -0800279 * Sends all the live calls to the in-call app if any exist. If there are no live calls, then
280 * tells the in-call controller to unbind since it is not needed.
281 */
282 void updateInCall() {
283 if (mCalls.isEmpty()) {
284 mInCallController.unbind();
285 } else {
286 for (Call call : mCalls.values()) {
287 addInCallEntry(call);
288 }
289 }
290 }
291
292 /**
293 * Adds the specified call to the main list of live calls.
294 *
295 * @param call The call to add.
296 */
297 private void addCall(Call call) {
298 mCalls.put(call.getId(), call);
299 addInCallEntry(call);
300 }
301
302 /**
303 * Notifies the in-call app of the specified (new) call.
304 */
305 private void addInCallEntry(Call call) {
306 mInCallController.addCall(call.toCallInfo());
307 }
308
309 /**
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800310 * Sets the specified state on the specified call. Updates the ringer if the call is exiting
311 * the RINGING state.
Santos Cordon681663d2014-01-30 04:32:15 -0800312 *
313 * @param callId The ID of the call to update.
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800314 * @param newState The new state of the call.
Santos Cordon681663d2014-01-30 04:32:15 -0800315 */
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800316 private void setCallState(String callId, CallState newState) {
Santos Cordon681663d2014-01-30 04:32:15 -0800317 Preconditions.checkState(!Strings.isNullOrEmpty(callId));
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800318 Preconditions.checkNotNull(newState);
Santos Cordon681663d2014-01-30 04:32:15 -0800319
320 Call call = mCalls.get(callId);
321 if (call == null) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800322 Log.w(this, "Call %s was not found while attempting to update the state to %s.",
323 callId, newState );
Santos Cordon681663d2014-01-30 04:32:15 -0800324 } else {
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800325 if (newState != call.getState()) {
326 // Unfortunately, in the telephony world the radio is king. So if the call notifies
327 // us that the call is in a particular state, we allow it even if it doesn't make
328 // sense (e.g., ACTIVE -> RINGING).
329 // TODO(santoscordon): Consider putting a stop to the above and turning CallState
330 // into a well-defined state machine.
331 // TODO(santoscordon): Define expected state transitions here, and log when an
332 // unexpected transition occurs.
333 call.setState(newState);
334 // TODO(santoscordon): Notify the in-call app whenever a call changes state.
Evan Charlton5c670a92014-03-06 14:58:20 -0800335
336 broadcastState(call);
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800337 }
338 }
339 }
340
341 /**
Evan Charlton5c670a92014-03-06 14:58:20 -0800342 * Send a {@link TelephonyManager#ACTION_PHONE_STATE_CHANGED} broadcast when the call state
343 * changes. TODO: Split this out into a separate class and do it properly; this is just a first
344 * pass over the functionality.
345 *
346 * @param call The {@link Call} being updated.
347 */
348 private void broadcastState(Call call) {
349 final String callState;
350 switch (call.getState()) {
351 case DIALING:
352 case ACTIVE:
353 callState = TelephonyManager.EXTRA_STATE_OFFHOOK;
354 break;
355
356 case RINGING:
357 callState = TelephonyManager.EXTRA_STATE_RINGING;
358 break;
359
360 case DISCONNECTED:
361 case ABORTED:
362 callState = TelephonyManager.EXTRA_STATE_IDLE;
363 break;
364
365 default:
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800366 Log.w(this, "Call is in an unknown state (%s), not broadcasting: %s", call.getState(),
367 call.getId());
Evan Charlton5c670a92014-03-06 14:58:20 -0800368 return;
369 }
370
371 Intent updateIntent = new Intent(TelephonyManager.ACTION_PHONE_STATE_CHANGED);
372 updateIntent.putExtra(TelephonyManager.EXTRA_STATE, callState);
373
374 // Populate both, since the original API was needlessly complicated.
375 updateIntent.putExtra(TelephonyManager.EXTRA_INCOMING_NUMBER, call.getHandle());
376 // TODO: See if we can add this (the current API only sets this on NEW_OUTGOING_CALL).
377 updateIntent.putExtra(Intent.EXTRA_PHONE_NUMBER, call.getHandle());
378
379 // TODO: Replace these with real constants once this API has been vetted.
380 CallServiceWrapper callService = call.getCallService();
381 if (callService != null) {
382 updateIntent.putExtra(CallService.class.getName(), callService.getComponentName());
383 }
384 TelecommApp.getInstance().sendBroadcast(updateIntent, Manifest.permission.READ_PHONE_STATE);
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800385 Log.i(this, "Broadcasted state change: %s", callState);
Evan Charlton5c670a92014-03-06 14:58:20 -0800386 }
387
388 /**
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800389 * Removes the specified call from the list of unanswered incoming calls and updates the ringer
390 * based on the new state of {@link #mUnansweredIncomingCalls}. Safe to call with a call ID that
391 * is not present in the list of incoming calls.
392 *
393 * @param callId The ID of the call.
394 */
395 private void removeFromUnansweredCalls(String callId) {
396 Call call = mCalls.get(callId);
397 if (call != null && mUnansweredIncomingCalls.remove(call)) {
398 if (mUnansweredIncomingCalls.isEmpty()) {
399 mRinger.stopRinging();
400 } else {
401 mRinger.startRinging();
402 }
403 }
404 }
405
406 /**
407 * Stops playing the ringer if the specified call is the top-most incoming call. This exists
408 * separately from {@link #removeIncomingCall} for cases where we would like to stop playing the
409 * ringer for a call, but that call may still exist in {@link #mUnansweredIncomingCalls} - See
410 * {@link #rejectCall}, {@link #answerCall}.
411 *
412 * @param call The call for which we should stop ringing.
413 */
414 private void stopRinging(Call call) {
415 // Only stop the ringer if this call is the top-most incoming call.
416 if (!mUnansweredIncomingCalls.isEmpty() && mUnansweredIncomingCalls.get(0) == call) {
417 mRinger.stopRinging();
Santos Cordon681663d2014-01-30 04:32:15 -0800418 }
419 }
Ben Gilad9f2bed32013-12-12 17:43:26 -0800420}