blob: a592ee39b0c47f3d585a1237b35169a77cc97fd6 [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
Ben Giladc5b22692014-02-18 20:03:22 -080019import android.telecomm.CallServiceDescriptor;
Santos Cordon681663d2014-01-30 04:32:15 -080020import android.telecomm.CallState;
Santos Cordon681663d2014-01-30 04:32:15 -080021import android.util.Log;
Santos Cordon8e8b8d22013-12-19 14:14:05 -080022
Santos Cordon681663d2014-01-30 04:32:15 -080023import com.google.common.base.Preconditions;
24import com.google.common.base.Strings;
Santos Cordon8e8b8d22013-12-19 14:14:05 -080025import com.google.common.collect.Lists;
Santos Cordon681663d2014-01-30 04:32:15 -080026import com.google.common.collect.Maps;
Ben Gilad9f2bed32013-12-12 17:43:26 -080027
Ben Gilad9f2bed32013-12-12 17:43:26 -080028import java.util.List;
Santos Cordon681663d2014-01-30 04:32:15 -080029import java.util.Map;
Ben Gilad9f2bed32013-12-12 17:43:26 -080030
Ben Giladdd8c6082013-12-30 14:44:08 -080031/**
32 * Singleton.
33 *
34 * NOTE(gilad): by design most APIs are package private, use the relevant adapter/s to allow
35 * access from other packages specifically refraining from passing the CallsManager instance
36 * beyond the com.android.telecomm package boundary.
37 */
Santos Cordon8e8b8d22013-12-19 14:14:05 -080038public final class CallsManager {
Ben Gilada0d9f752014-02-26 11:49:03 -080039
Santos Cordon681663d2014-01-30 04:32:15 -080040 private static final String TAG = CallsManager.class.getSimpleName();
Ben Gilad9f2bed32013-12-12 17:43:26 -080041
Santos Cordon8e8b8d22013-12-19 14:14:05 -080042 private static final CallsManager INSTANCE = new CallsManager();
Ben Gilad9f2bed32013-12-12 17:43:26 -080043
Santos Cordone3d76ab2014-01-28 17:25:20 -080044 private final Switchboard mSwitchboard;
45
46 /** Used to control the in-call app. */
47 private final InCallController mInCallController;
48
Santos Cordon4e9fffe2014-03-04 18:13:41 -080049 private final Ringer mRinger;
50
Santos Cordon8e8b8d22013-12-19 14:14:05 -080051 /**
Santos Cordon681663d2014-01-30 04:32:15 -080052 * The main call repository. Keeps an instance of all live calls keyed by call ID. New incoming
53 * and outgoing calls are added to the map and removed when the calls move to the disconnected
54 * state.
55 * TODO(santoscordon): Add new CallId class and use it in place of String.
56 */
57 private final Map<String, Call> mCalls = Maps.newHashMap();
58
59 /**
Santos Cordon4e9fffe2014-03-04 18:13:41 -080060 * Used to keep ordering of unanswered incoming calls. The existence of multiple call services
61 * means that there can easily exist multiple incoming calls and explicit ordering is useful for
62 * maintaining the proper state of the ringer.
63 * TODO(santoscordon): May want to add comments about ITelephony.answerCall() method since
64 * ordering may also apply to that case.
65 */
66 private final List<Call> mUnansweredIncomingCalls = Lists.newLinkedList();
67
68 /**
Santos Cordon8e8b8d22013-12-19 14:14:05 -080069 * May be unnecessary per off-line discussions (between santoscordon and gilad) since the set
70 * of CallsManager APIs that need to be exposed to the dialer (or any application firing call
71 * intents) may be empty.
72 */
73 private DialerAdapter mDialerAdapter;
Ben Gilad9f2bed32013-12-12 17:43:26 -080074
Santos Cordon8e8b8d22013-12-19 14:14:05 -080075 private InCallAdapter mInCallAdapter;
Ben Gilad9f2bed32013-12-12 17:43:26 -080076
Santos Cordon8e8b8d22013-12-19 14:14:05 -080077 private CallLogManager mCallLogManager;
Ben Gilad9f2bed32013-12-12 17:43:26 -080078
Santos Cordon8e8b8d22013-12-19 14:14:05 -080079 private VoicemailManager mVoicemailManager;
Ben Gilad9f2bed32013-12-12 17:43:26 -080080
Ben Gilad8bdaa462014-02-05 12:53:19 -080081 private List<OutgoingCallValidator> mOutgoingCallValidators = Lists.newArrayList();
Ben Gilad9f2bed32013-12-12 17:43:26 -080082
Ben Gilad8bdaa462014-02-05 12:53:19 -080083 private List<IncomingCallValidator> mIncomingCallValidators = Lists.newArrayList();
Ben Gilad9f2bed32013-12-12 17:43:26 -080084
Santos Cordon8e8b8d22013-12-19 14:14:05 -080085 /**
Ben Gilad8bdaa462014-02-05 12:53:19 -080086 * Initializes the required Telecomm components.
Santos Cordon8e8b8d22013-12-19 14:14:05 -080087 */
88 private CallsManager() {
Santos Cordon6242b132014-02-07 16:24:42 -080089 mSwitchboard = new Switchboard(this);
90 mInCallController = new InCallController(this);
Santos Cordon4e9fffe2014-03-04 18:13:41 -080091 mRinger = new Ringer();
Santos Cordon8e8b8d22013-12-19 14:14:05 -080092 }
93
Ben Gilada0d9f752014-02-26 11:49:03 -080094 static CallsManager getInstance() {
95 return INSTANCE;
96 }
97
Santos Cordon8e8b8d22013-12-19 14:14:05 -080098 /**
Santos Cordon493e8f22014-02-19 03:15:12 -080099 * Starts the incoming call sequence by having switchboard gather more information about the
100 * specified call; using the specified call service descriptor. Upon success, execution returns
101 * to {@link #handleSuccessfulIncomingCall} to start the in-call UI.
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800102 *
Ben Giladc5b22692014-02-18 20:03:22 -0800103 * @param descriptor The descriptor of the call service to use for this incoming call.
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800104 */
Santos Cordon493e8f22014-02-19 03:15:12 -0800105 void processIncomingCallIntent(CallServiceDescriptor descriptor) {
Santos Cordon61d0f702014-02-19 02:52:23 -0800106 Log.d(TAG, "processIncomingCallIntent");
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800107 // Create a call with no handle. Eventually, switchboard will update the call with
108 // additional information from the call service, but for now we just need one to pass around
109 // with a unique call ID.
Santos Cordon493e8f22014-02-19 03:15:12 -0800110 Call call = new Call();
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800111
Santos Cordon493e8f22014-02-19 03:15:12 -0800112 mSwitchboard.retrieveIncomingCall(call, descriptor);
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800113 }
114
115 /**
Ben Gilada0d9f752014-02-26 11:49:03 -0800116 * Validates the specified call and, upon no objection to connect it, adds the new call to the
117 * list of live calls. Also notifies the in-call app so the user can answer or reject the call.
118 *
119 * @param call The new incoming call.
120 */
121 void handleSuccessfulIncomingCall(Call call) {
122 Log.d(TAG, "handleSuccessfulIncomingCall");
123 Preconditions.checkState(call.getState() == CallState.RINGING);
124
125 String handle = call.getHandle();
126 ContactInfo contactInfo = call.getContactInfo();
127 for (IncomingCallValidator validator : mIncomingCallValidators) {
128 if (!validator.isValid(handle, contactInfo)) {
129 // TODO(gilad): Consider displaying an error message.
130 Log.i(TAG, "Dropping restricted incoming call");
131 return;
132 }
133 }
134
135 // No objection to accept the incoming call, proceed with potentially connecting it (based
136 // on the user's action, or lack thereof).
137 addCall(call);
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800138
139 mUnansweredIncomingCalls.add(call);
140 if (mUnansweredIncomingCalls.size() == 1) {
141 // Start the ringer if we are the top-most incoming call (the only one in this case).
142 mRinger.startRinging();
143 }
Ben Gilada0d9f752014-02-26 11:49:03 -0800144 }
145
146 /**
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800147 * Attempts to issue/connect the specified call. From an (arbitrary) application standpoint,
148 * all that is required to initiate this flow is to fire either of the CALL, CALL_PRIVILEGED,
149 * and CALL_EMERGENCY intents. These are listened to by CallActivity.java which then invokes
150 * this method.
151 *
152 * @param handle The handle to dial.
153 * @param contactInfo Information about the entity being called.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800154 */
Ben Gilada0d9f752014-02-26 11:49:03 -0800155 void processOutgoingCallIntent(String handle, ContactInfo contactInfo) {
Ben Gilad8bdaa462014-02-05 12:53:19 -0800156 for (OutgoingCallValidator validator : mOutgoingCallValidators) {
Ben Gilada0d9f752014-02-26 11:49:03 -0800157 if (!validator.isValid(handle, contactInfo)) {
158 // TODO(gilad): Display an error message.
159 Log.i(TAG, "Dropping restricted outgoing call.");
160 return;
161 }
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800162 }
163
164 // No objection to issue the call, proceed with trying to put it through.
Ben Gilad13329fd2014-02-11 17:20:29 -0800165 Call call = new Call(handle, contactInfo);
Santos Cordonc195e362014-02-11 17:05:31 -0800166 mSwitchboard.placeOutgoingCall(call);
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800167 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800168
169 /**
Santos Cordon681663d2014-01-30 04:32:15 -0800170 * Adds a new outgoing call to the list of live calls and notifies the in-call app.
171 *
172 * @param call The new outgoing call.
173 */
174 void handleSuccessfulOutgoingCall(Call call) {
175 // OutgoingCallProcessor sets the call state to DIALING when it receives confirmation of the
176 // placed call from the call service so there is no need to set it here. Instead, check that
177 // the state is appropriate.
178 Preconditions.checkState(call.getState() == CallState.DIALING);
Santos Cordon681663d2014-01-30 04:32:15 -0800179 addCall(call);
Santos Cordon681663d2014-01-30 04:32:15 -0800180 }
181
182 /**
Santos Cordone3d76ab2014-01-28 17:25:20 -0800183 * Instructs Telecomm to answer the specified call. Intended to be invoked by the in-call
184 * app through {@link InCallAdapter} after Telecomm notifies it of an incoming call followed by
185 * the user opting to answer said call.
186 *
187 * @param callId The ID of the call.
188 */
189 void answerCall(String callId) {
Santos Cordon61d0f702014-02-19 02:52:23 -0800190 Call call = mCalls.get(callId);
191 if (call == null) {
192 Log.i(TAG, "Request to answer a non-existent call " + callId);
193 } else {
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800194 stopRinging(call);
195
Santos Cordon61d0f702014-02-19 02:52:23 -0800196 // We do not update the UI until we get confirmation of the answer() through
197 // {@link #markCallAsActive}. However, if we ever change that to look more responsive,
198 // then we need to make sure we add a timeout for the answer() in case the call never
199 // comes out of RINGING.
200 call.answer();
201 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800202 }
203
204 /**
205 * Instructs Telecomm to reject the specified call. Intended to be invoked by the in-call
206 * app through {@link InCallAdapter} after Telecomm notifies it of an incoming call followed by
207 * the user opting to reject said call.
208 *
209 * @param callId The ID of the call.
210 */
211 void rejectCall(String callId) {
Santos Cordon61d0f702014-02-19 02:52:23 -0800212 Call call = mCalls.get(callId);
213 if (call == null) {
214 Log.i(TAG, "Request to reject a non-existent call " + callId);
215 } else {
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800216 stopRinging(call);
Santos Cordon61d0f702014-02-19 02:52:23 -0800217 call.reject();
218 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800219 }
220
221 /**
222 * Instructs Telecomm to disconnect the specified call. Intended to be invoked by the
223 * in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered by
224 * the user hitting the end-call button.
225 *
226 * @param callId The ID of the call.
227 */
228 void disconnectCall(String callId) {
Santos Cordon049b7b62014-01-30 05:34:26 -0800229 Call call = mCalls.get(callId);
230 if (call == null) {
231 Log.e(TAG, "Unknown call (" + callId + ") asked to disconnect");
232 } else {
233 call.disconnect();
234 }
235
Santos Cordone3d76ab2014-01-28 17:25:20 -0800236 }
Santos Cordon681663d2014-01-30 04:32:15 -0800237
238 void markCallAsRinging(String callId) {
239 setCallState(callId, CallState.RINGING);
240 }
241
242 void markCallAsDialing(String callId) {
243 setCallState(callId, CallState.DIALING);
244 }
245
246 void markCallAsActive(String callId) {
247 setCallState(callId, CallState.ACTIVE);
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800248 removeFromUnansweredCalls(callId);
Sailesh Nepalfc511ca2014-02-20 18:48:53 -0800249 mInCallController.markCallAsActive(callId);
Santos Cordon681663d2014-01-30 04:32:15 -0800250 }
251
Santos Cordon049b7b62014-01-30 05:34:26 -0800252 /**
253 * Marks the specified call as DISCONNECTED and notifies the in-call app. If this was the last
254 * live call, then also disconnect from the in-call controller.
255 *
256 * @param callId The ID of the call.
257 */
Santos Cordon681663d2014-01-30 04:32:15 -0800258 void markCallAsDisconnected(String callId) {
259 setCallState(callId, CallState.DISCONNECTED);
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800260 removeFromUnansweredCalls(callId);
Santos Cordonc195e362014-02-11 17:05:31 -0800261
262 Call call = mCalls.remove(callId);
263 // At this point the call service has confirmed that the call is disconnected to it is
264 // safe to disassociate the call from its call service.
265 call.clearCallService();
Santos Cordon049b7b62014-01-30 05:34:26 -0800266
267 // Notify the in-call UI
268 mInCallController.markCallAsDisconnected(callId);
269 if (mCalls.isEmpty()) {
270 mInCallController.unbind();
271 }
Santos Cordon681663d2014-01-30 04:32:15 -0800272 }
273
274 /**
Ben Gilada0d9f752014-02-26 11:49:03 -0800275 * Sends all the live calls to the in-call app if any exist. If there are no live calls, then
276 * tells the in-call controller to unbind since it is not needed.
277 */
278 void updateInCall() {
279 if (mCalls.isEmpty()) {
280 mInCallController.unbind();
281 } else {
282 for (Call call : mCalls.values()) {
283 addInCallEntry(call);
284 }
285 }
286 }
287
288 /**
289 * Adds the specified call to the main list of live calls.
290 *
291 * @param call The call to add.
292 */
293 private void addCall(Call call) {
294 mCalls.put(call.getId(), call);
295 addInCallEntry(call);
296 }
297
298 /**
299 * Notifies the in-call app of the specified (new) call.
300 */
301 private void addInCallEntry(Call call) {
302 mInCallController.addCall(call.toCallInfo());
303 }
304
305 /**
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800306 * Sets the specified state on the specified call. Updates the ringer if the call is exiting
307 * the RINGING state.
Santos Cordon681663d2014-01-30 04:32:15 -0800308 *
309 * @param callId The ID of the call to update.
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800310 * @param newState The new state of the call.
Santos Cordon681663d2014-01-30 04:32:15 -0800311 */
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800312 private void setCallState(String callId, CallState newState) {
Santos Cordon681663d2014-01-30 04:32:15 -0800313 Preconditions.checkState(!Strings.isNullOrEmpty(callId));
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800314 Preconditions.checkNotNull(newState);
Santos Cordon681663d2014-01-30 04:32:15 -0800315
316 Call call = mCalls.get(callId);
317 if (call == null) {
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800318 Log.e(TAG, "Call " + callId + " was not found while attempting to update the state " +
319 "to " + newState + ".");
Santos Cordon681663d2014-01-30 04:32:15 -0800320 } else {
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800321 if (newState != call.getState()) {
322 // Unfortunately, in the telephony world the radio is king. So if the call notifies
323 // us that the call is in a particular state, we allow it even if it doesn't make
324 // sense (e.g., ACTIVE -> RINGING).
325 // TODO(santoscordon): Consider putting a stop to the above and turning CallState
326 // into a well-defined state machine.
327 // TODO(santoscordon): Define expected state transitions here, and log when an
328 // unexpected transition occurs.
329 call.setState(newState);
330 // TODO(santoscordon): Notify the in-call app whenever a call changes state.
331 }
332 }
333 }
334
335 /**
336 * Removes the specified call from the list of unanswered incoming calls and updates the ringer
337 * based on the new state of {@link #mUnansweredIncomingCalls}. Safe to call with a call ID that
338 * is not present in the list of incoming calls.
339 *
340 * @param callId The ID of the call.
341 */
342 private void removeFromUnansweredCalls(String callId) {
343 Call call = mCalls.get(callId);
344 if (call != null && mUnansweredIncomingCalls.remove(call)) {
345 if (mUnansweredIncomingCalls.isEmpty()) {
346 mRinger.stopRinging();
347 } else {
348 mRinger.startRinging();
349 }
350 }
351 }
352
353 /**
354 * Stops playing the ringer if the specified call is the top-most incoming call. This exists
355 * separately from {@link #removeIncomingCall} for cases where we would like to stop playing the
356 * ringer for a call, but that call may still exist in {@link #mUnansweredIncomingCalls} - See
357 * {@link #rejectCall}, {@link #answerCall}.
358 *
359 * @param call The call for which we should stop ringing.
360 */
361 private void stopRinging(Call call) {
362 // Only stop the ringer if this call is the top-most incoming call.
363 if (!mUnansweredIncomingCalls.isEmpty() && mUnansweredIncomingCalls.get(0) == call) {
364 mRinger.stopRinging();
Santos Cordon681663d2014-01-30 04:32:15 -0800365 }
366 }
Ben Gilad9f2bed32013-12-12 17:43:26 -0800367}