blob: 35d744f11c9a8738ccdda64c62feb50d4e31cfff [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 Cordon681663d2014-01-30 04:32:15 -080027import android.util.Log;
Santos Cordon8e8b8d22013-12-19 14:14:05 -080028
Santos Cordon681663d2014-01-30 04:32:15 -080029import com.google.common.base.Preconditions;
30import com.google.common.base.Strings;
Santos Cordon8e8b8d22013-12-19 14:14:05 -080031import com.google.common.collect.Lists;
Santos Cordon681663d2014-01-30 04:32:15 -080032import com.google.common.collect.Maps;
Ben Gilad9f2bed32013-12-12 17:43:26 -080033
Ben Gilad9f2bed32013-12-12 17:43:26 -080034import java.util.List;
Santos Cordon681663d2014-01-30 04:32:15 -080035import java.util.Map;
Ben Gilad9f2bed32013-12-12 17:43:26 -080036
Ben Giladdd8c6082013-12-30 14:44:08 -080037/**
38 * Singleton.
39 *
40 * NOTE(gilad): by design most APIs are package private, use the relevant adapter/s to allow
41 * access from other packages specifically refraining from passing the CallsManager instance
42 * beyond the com.android.telecomm package boundary.
43 */
Santos Cordon8e8b8d22013-12-19 14:14:05 -080044public final class CallsManager {
Ben Gilada0d9f752014-02-26 11:49:03 -080045
Santos Cordon681663d2014-01-30 04:32:15 -080046 private static final String TAG = CallsManager.class.getSimpleName();
Ben Gilad9f2bed32013-12-12 17:43:26 -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();
Santos Cordon8e8b8d22013-12-19 14:14:05 -080098 }
99
Ben Gilada0d9f752014-02-26 11:49:03 -0800100 static CallsManager getInstance() {
101 return INSTANCE;
102 }
103
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800104 /**
Santos Cordon493e8f22014-02-19 03:15:12 -0800105 * Starts the incoming call sequence by having switchboard gather more information about the
106 * specified call; using the specified call service descriptor. Upon success, execution returns
107 * to {@link #handleSuccessfulIncomingCall} to start the in-call UI.
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800108 *
Ben Giladc5b22692014-02-18 20:03:22 -0800109 * @param descriptor The descriptor of the call service to use for this incoming call.
Evan Charltona05805b2014-03-05 08:21:46 -0800110 * @param extras The optional extras Bundle passed with the intent used for the incoming call.
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800111 */
Evan Charltona05805b2014-03-05 08:21:46 -0800112 void processIncomingCallIntent(CallServiceDescriptor descriptor, Bundle extras) {
Santos Cordon61d0f702014-02-19 02:52:23 -0800113 Log.d(TAG, "processIncomingCallIntent");
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800114 // Create a call with no handle. Eventually, switchboard will update the call with
115 // additional information from the call service, but for now we just need one to pass around
116 // with a unique call ID.
Santos Cordon493e8f22014-02-19 03:15:12 -0800117 Call call = new Call();
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800118
Evan Charltona05805b2014-03-05 08:21:46 -0800119 mSwitchboard.retrieveIncomingCall(call, descriptor, extras);
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800120 }
121
122 /**
Ben Gilada0d9f752014-02-26 11:49:03 -0800123 * Validates the specified call and, upon no objection to connect it, adds the new call to the
124 * list of live calls. Also notifies the in-call app so the user can answer or reject the call.
125 *
126 * @param call The new incoming call.
127 */
128 void handleSuccessfulIncomingCall(Call call) {
129 Log.d(TAG, "handleSuccessfulIncomingCall");
130 Preconditions.checkState(call.getState() == CallState.RINGING);
131
132 String handle = call.getHandle();
133 ContactInfo contactInfo = call.getContactInfo();
134 for (IncomingCallValidator validator : mIncomingCallValidators) {
135 if (!validator.isValid(handle, contactInfo)) {
136 // TODO(gilad): Consider displaying an error message.
137 Log.i(TAG, "Dropping restricted incoming call");
138 return;
139 }
140 }
141
142 // No objection to accept the incoming call, proceed with potentially connecting it (based
143 // on the user's action, or lack thereof).
144 addCall(call);
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800145
146 mUnansweredIncomingCalls.add(call);
147 if (mUnansweredIncomingCalls.size() == 1) {
148 // Start the ringer if we are the top-most incoming call (the only one in this case).
149 mRinger.startRinging();
150 }
Ben Gilada0d9f752014-02-26 11:49:03 -0800151 }
152
153 /**
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800154 * Attempts to issue/connect the specified call. From an (arbitrary) application standpoint,
155 * all that is required to initiate this flow is to fire either of the CALL, CALL_PRIVILEGED,
156 * and CALL_EMERGENCY intents. These are listened to by CallActivity.java which then invokes
157 * this method.
158 *
159 * @param handle The handle to dial.
160 * @param contactInfo Information about the entity being called.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800161 */
Ben Gilada0d9f752014-02-26 11:49:03 -0800162 void processOutgoingCallIntent(String handle, ContactInfo contactInfo) {
Ben Gilad8bdaa462014-02-05 12:53:19 -0800163 for (OutgoingCallValidator validator : mOutgoingCallValidators) {
Ben Gilada0d9f752014-02-26 11:49:03 -0800164 if (!validator.isValid(handle, contactInfo)) {
165 // TODO(gilad): Display an error message.
166 Log.i(TAG, "Dropping restricted outgoing call.");
167 return;
168 }
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800169 }
170
171 // No objection to issue the call, proceed with trying to put it through.
Ben Gilad13329fd2014-02-11 17:20:29 -0800172 Call call = new Call(handle, contactInfo);
Santos Cordonc195e362014-02-11 17:05:31 -0800173 mSwitchboard.placeOutgoingCall(call);
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800174 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800175
176 /**
Santos Cordon681663d2014-01-30 04:32:15 -0800177 * Adds a new outgoing call to the list of live calls and notifies the in-call app.
178 *
179 * @param call The new outgoing call.
180 */
181 void handleSuccessfulOutgoingCall(Call call) {
182 // OutgoingCallProcessor sets the call state to DIALING when it receives confirmation of the
183 // placed call from the call service so there is no need to set it here. Instead, check that
184 // the state is appropriate.
185 Preconditions.checkState(call.getState() == CallState.DIALING);
Santos Cordon681663d2014-01-30 04:32:15 -0800186 addCall(call);
Santos Cordon681663d2014-01-30 04:32:15 -0800187 }
188
189 /**
Santos Cordone3d76ab2014-01-28 17:25:20 -0800190 * Instructs Telecomm to answer the specified call. Intended to be invoked by the in-call
191 * app through {@link InCallAdapter} after Telecomm notifies it of an incoming call followed by
192 * the user opting to answer said call.
193 *
194 * @param callId The ID of the call.
195 */
196 void answerCall(String callId) {
Santos Cordon61d0f702014-02-19 02:52:23 -0800197 Call call = mCalls.get(callId);
198 if (call == null) {
199 Log.i(TAG, "Request to answer a non-existent call " + callId);
200 } else {
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800201 stopRinging(call);
202
Santos Cordon61d0f702014-02-19 02:52:23 -0800203 // We do not update the UI until we get confirmation of the answer() through
204 // {@link #markCallAsActive}. However, if we ever change that to look more responsive,
205 // then we need to make sure we add a timeout for the answer() in case the call never
206 // comes out of RINGING.
207 call.answer();
208 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800209 }
210
211 /**
212 * Instructs Telecomm to reject the specified call. Intended to be invoked by the in-call
213 * app through {@link InCallAdapter} after Telecomm notifies it of an incoming call followed by
214 * the user opting to reject said call.
215 *
216 * @param callId The ID of the call.
217 */
218 void rejectCall(String callId) {
Santos Cordon61d0f702014-02-19 02:52:23 -0800219 Call call = mCalls.get(callId);
220 if (call == null) {
221 Log.i(TAG, "Request to reject a non-existent call " + callId);
222 } else {
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800223 stopRinging(call);
Santos Cordon61d0f702014-02-19 02:52:23 -0800224 call.reject();
225 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800226 }
227
228 /**
229 * Instructs Telecomm to disconnect the specified call. Intended to be invoked by the
230 * in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered by
231 * the user hitting the end-call button.
232 *
233 * @param callId The ID of the call.
234 */
235 void disconnectCall(String callId) {
Santos Cordon049b7b62014-01-30 05:34:26 -0800236 Call call = mCalls.get(callId);
237 if (call == null) {
238 Log.e(TAG, "Unknown call (" + callId + ") asked to disconnect");
239 } else {
240 call.disconnect();
241 }
242
Santos Cordone3d76ab2014-01-28 17:25:20 -0800243 }
Santos Cordon681663d2014-01-30 04:32:15 -0800244
245 void markCallAsRinging(String callId) {
246 setCallState(callId, CallState.RINGING);
247 }
248
249 void markCallAsDialing(String callId) {
250 setCallState(callId, CallState.DIALING);
251 }
252
253 void markCallAsActive(String callId) {
254 setCallState(callId, CallState.ACTIVE);
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800255 removeFromUnansweredCalls(callId);
Sailesh Nepalfc511ca2014-02-20 18:48:53 -0800256 mInCallController.markCallAsActive(callId);
Santos Cordon681663d2014-01-30 04:32:15 -0800257 }
258
Santos Cordon049b7b62014-01-30 05:34:26 -0800259 /**
260 * Marks the specified call as DISCONNECTED and notifies the in-call app. If this was the last
261 * live call, then also disconnect from the in-call controller.
262 *
263 * @param callId The ID of the call.
264 */
Santos Cordon681663d2014-01-30 04:32:15 -0800265 void markCallAsDisconnected(String callId) {
266 setCallState(callId, CallState.DISCONNECTED);
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800267 removeFromUnansweredCalls(callId);
Santos Cordonc195e362014-02-11 17:05:31 -0800268
269 Call call = mCalls.remove(callId);
270 // At this point the call service has confirmed that the call is disconnected to it is
271 // safe to disassociate the call from its call service.
272 call.clearCallService();
Santos Cordon049b7b62014-01-30 05:34:26 -0800273
274 // Notify the in-call UI
275 mInCallController.markCallAsDisconnected(callId);
276 if (mCalls.isEmpty()) {
277 mInCallController.unbind();
278 }
Santos Cordon681663d2014-01-30 04:32:15 -0800279 }
280
281 /**
Ben Gilada0d9f752014-02-26 11:49:03 -0800282 * Sends all the live calls to the in-call app if any exist. If there are no live calls, then
283 * tells the in-call controller to unbind since it is not needed.
284 */
285 void updateInCall() {
286 if (mCalls.isEmpty()) {
287 mInCallController.unbind();
288 } else {
289 for (Call call : mCalls.values()) {
290 addInCallEntry(call);
291 }
292 }
293 }
294
295 /**
296 * Adds the specified call to the main list of live calls.
297 *
298 * @param call The call to add.
299 */
300 private void addCall(Call call) {
301 mCalls.put(call.getId(), call);
302 addInCallEntry(call);
303 }
304
305 /**
306 * Notifies the in-call app of the specified (new) call.
307 */
308 private void addInCallEntry(Call call) {
309 mInCallController.addCall(call.toCallInfo());
310 }
311
312 /**
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800313 * Sets the specified state on the specified call. Updates the ringer if the call is exiting
314 * the RINGING state.
Santos Cordon681663d2014-01-30 04:32:15 -0800315 *
316 * @param callId The ID of the call to update.
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800317 * @param newState The new state of the call.
Santos Cordon681663d2014-01-30 04:32:15 -0800318 */
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800319 private void setCallState(String callId, CallState newState) {
Santos Cordon681663d2014-01-30 04:32:15 -0800320 Preconditions.checkState(!Strings.isNullOrEmpty(callId));
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800321 Preconditions.checkNotNull(newState);
Santos Cordon681663d2014-01-30 04:32:15 -0800322
323 Call call = mCalls.get(callId);
324 if (call == null) {
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800325 Log.e(TAG, "Call " + callId + " was not found while attempting to update the state " +
326 "to " + newState + ".");
Santos Cordon681663d2014-01-30 04:32:15 -0800327 } else {
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800328 if (newState != call.getState()) {
329 // Unfortunately, in the telephony world the radio is king. So if the call notifies
330 // us that the call is in a particular state, we allow it even if it doesn't make
331 // sense (e.g., ACTIVE -> RINGING).
332 // TODO(santoscordon): Consider putting a stop to the above and turning CallState
333 // into a well-defined state machine.
334 // TODO(santoscordon): Define expected state transitions here, and log when an
335 // unexpected transition occurs.
336 call.setState(newState);
337 // TODO(santoscordon): Notify the in-call app whenever a call changes state.
Evan Charlton5c670a92014-03-06 14:58:20 -0800338
339 broadcastState(call);
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800340 }
341 }
342 }
343
344 /**
Evan Charlton5c670a92014-03-06 14:58:20 -0800345 * Send a {@link TelephonyManager#ACTION_PHONE_STATE_CHANGED} broadcast when the call state
346 * changes. TODO: Split this out into a separate class and do it properly; this is just a first
347 * pass over the functionality.
348 *
349 * @param call The {@link Call} being updated.
350 */
351 private void broadcastState(Call call) {
352 final String callState;
353 switch (call.getState()) {
354 case DIALING:
355 case ACTIVE:
356 callState = TelephonyManager.EXTRA_STATE_OFFHOOK;
357 break;
358
359 case RINGING:
360 callState = TelephonyManager.EXTRA_STATE_RINGING;
361 break;
362
363 case DISCONNECTED:
364 case ABORTED:
365 callState = TelephonyManager.EXTRA_STATE_IDLE;
366 break;
367
368 default:
369 Log.e(TAG, "Call is in an unknown state (" + call.getState() +
370 "), not broadcasting: " + call.getId());
371 return;
372 }
373
374 Intent updateIntent = new Intent(TelephonyManager.ACTION_PHONE_STATE_CHANGED);
375 updateIntent.putExtra(TelephonyManager.EXTRA_STATE, callState);
376
377 // Populate both, since the original API was needlessly complicated.
378 updateIntent.putExtra(TelephonyManager.EXTRA_INCOMING_NUMBER, call.getHandle());
379 // TODO: See if we can add this (the current API only sets this on NEW_OUTGOING_CALL).
380 updateIntent.putExtra(Intent.EXTRA_PHONE_NUMBER, call.getHandle());
381
382 // TODO: Replace these with real constants once this API has been vetted.
383 CallServiceWrapper callService = call.getCallService();
384 if (callService != null) {
385 updateIntent.putExtra(CallService.class.getName(), callService.getComponentName());
386 }
387 TelecommApp.getInstance().sendBroadcast(updateIntent, Manifest.permission.READ_PHONE_STATE);
388 Log.i(TAG, "Broadcasted state change: " + callState);
389 }
390
391 /**
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800392 * Removes the specified call from the list of unanswered incoming calls and updates the ringer
393 * based on the new state of {@link #mUnansweredIncomingCalls}. Safe to call with a call ID that
394 * is not present in the list of incoming calls.
395 *
396 * @param callId The ID of the call.
397 */
398 private void removeFromUnansweredCalls(String callId) {
399 Call call = mCalls.get(callId);
400 if (call != null && mUnansweredIncomingCalls.remove(call)) {
401 if (mUnansweredIncomingCalls.isEmpty()) {
402 mRinger.stopRinging();
403 } else {
404 mRinger.startRinging();
405 }
406 }
407 }
408
409 /**
410 * Stops playing the ringer if the specified call is the top-most incoming call. This exists
411 * separately from {@link #removeIncomingCall} for cases where we would like to stop playing the
412 * ringer for a call, but that call may still exist in {@link #mUnansweredIncomingCalls} - See
413 * {@link #rejectCall}, {@link #answerCall}.
414 *
415 * @param call The call for which we should stop ringing.
416 */
417 private void stopRinging(Call call) {
418 // Only stop the ringer if this call is the top-most incoming call.
419 if (!mUnansweredIncomingCalls.isEmpty() && mUnansweredIncomingCalls.get(0) == call) {
420 mRinger.stopRinging();
Santos Cordon681663d2014-01-30 04:32:15 -0800421 }
422 }
Ben Gilad9f2bed32013-12-12 17:43:26 -0800423}