blob: 7102cf488343bcfd5205ab228f90cba9a6901079 [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 Charltona05805b2014-03-05 08:21:46 -080019import android.os.Bundle;
Ben Giladc5b22692014-02-18 20:03:22 -080020import android.telecomm.CallServiceDescriptor;
Santos Cordon681663d2014-01-30 04:32:15 -080021import android.telecomm.CallState;
Santos Cordon681663d2014-01-30 04:32:15 -080022import android.util.Log;
Santos Cordon8e8b8d22013-12-19 14:14:05 -080023
Santos Cordon681663d2014-01-30 04:32:15 -080024import com.google.common.base.Preconditions;
25import com.google.common.base.Strings;
Santos Cordon8e8b8d22013-12-19 14:14:05 -080026import com.google.common.collect.Lists;
Santos Cordon681663d2014-01-30 04:32:15 -080027import com.google.common.collect.Maps;
Ben Gilad9f2bed32013-12-12 17:43:26 -080028
Ben Gilad9f2bed32013-12-12 17:43:26 -080029import java.util.List;
Santos Cordon681663d2014-01-30 04:32:15 -080030import java.util.Map;
Ben Gilad9f2bed32013-12-12 17:43:26 -080031
Ben Giladdd8c6082013-12-30 14:44:08 -080032/**
33 * Singleton.
34 *
35 * NOTE(gilad): by design most APIs are package private, use the relevant adapter/s to allow
36 * access from other packages specifically refraining from passing the CallsManager instance
37 * beyond the com.android.telecomm package boundary.
38 */
Santos Cordon8e8b8d22013-12-19 14:14:05 -080039public final class CallsManager {
Ben Gilada0d9f752014-02-26 11:49:03 -080040
Santos Cordon681663d2014-01-30 04:32:15 -080041 private static final String TAG = CallsManager.class.getSimpleName();
Ben Gilad9f2bed32013-12-12 17:43:26 -080042
Santos Cordon8e8b8d22013-12-19 14:14:05 -080043 private static final CallsManager INSTANCE = new CallsManager();
Ben Gilad9f2bed32013-12-12 17:43:26 -080044
Santos Cordone3d76ab2014-01-28 17:25:20 -080045 private final Switchboard mSwitchboard;
46
47 /** Used to control the in-call app. */
48 private final InCallController mInCallController;
49
Santos Cordon4e9fffe2014-03-04 18:13:41 -080050 private final Ringer mRinger;
51
Santos Cordon8e8b8d22013-12-19 14:14:05 -080052 /**
Santos Cordon681663d2014-01-30 04:32:15 -080053 * The main call repository. Keeps an instance of all live calls keyed by call ID. New incoming
54 * and outgoing calls are added to the map and removed when the calls move to the disconnected
55 * state.
56 * TODO(santoscordon): Add new CallId class and use it in place of String.
57 */
58 private final Map<String, Call> mCalls = Maps.newHashMap();
59
60 /**
Santos Cordon4e9fffe2014-03-04 18:13:41 -080061 * Used to keep ordering of unanswered incoming calls. The existence of multiple call services
62 * means that there can easily exist multiple incoming calls and explicit ordering is useful for
63 * maintaining the proper state of the ringer.
64 * TODO(santoscordon): May want to add comments about ITelephony.answerCall() method since
65 * ordering may also apply to that case.
66 */
67 private final List<Call> mUnansweredIncomingCalls = Lists.newLinkedList();
68
69 /**
Santos Cordon8e8b8d22013-12-19 14:14:05 -080070 * May be unnecessary per off-line discussions (between santoscordon and gilad) since the set
71 * of CallsManager APIs that need to be exposed to the dialer (or any application firing call
72 * intents) may be empty.
73 */
74 private DialerAdapter mDialerAdapter;
Ben Gilad9f2bed32013-12-12 17:43:26 -080075
Santos Cordon8e8b8d22013-12-19 14:14:05 -080076 private InCallAdapter mInCallAdapter;
Ben Gilad9f2bed32013-12-12 17:43:26 -080077
Santos Cordon8e8b8d22013-12-19 14:14:05 -080078 private CallLogManager mCallLogManager;
Ben Gilad9f2bed32013-12-12 17:43:26 -080079
Santos Cordon8e8b8d22013-12-19 14:14:05 -080080 private VoicemailManager mVoicemailManager;
Ben Gilad9f2bed32013-12-12 17:43:26 -080081
Evan Charltonf02e9882014-03-06 12:54:52 -080082 private final List<OutgoingCallValidator> mOutgoingCallValidators = Lists.newArrayList();
Ben Gilad9f2bed32013-12-12 17:43:26 -080083
Evan Charltonf02e9882014-03-06 12:54:52 -080084 private final List<IncomingCallValidator> mIncomingCallValidators = Lists.newArrayList();
Ben Gilad9f2bed32013-12-12 17:43:26 -080085
Santos Cordon8e8b8d22013-12-19 14:14:05 -080086 /**
Ben Gilad8bdaa462014-02-05 12:53:19 -080087 * Initializes the required Telecomm components.
Santos Cordon8e8b8d22013-12-19 14:14:05 -080088 */
89 private CallsManager() {
Santos Cordon6242b132014-02-07 16:24:42 -080090 mSwitchboard = new Switchboard(this);
91 mInCallController = new InCallController(this);
Santos Cordon4e9fffe2014-03-04 18:13:41 -080092 mRinger = new Ringer();
Santos Cordon8e8b8d22013-12-19 14:14:05 -080093 }
94
Ben Gilada0d9f752014-02-26 11:49:03 -080095 static CallsManager getInstance() {
96 return INSTANCE;
97 }
98
Santos Cordon8e8b8d22013-12-19 14:14:05 -080099 /**
Santos Cordon493e8f22014-02-19 03:15:12 -0800100 * Starts the incoming call sequence by having switchboard gather more information about the
101 * specified call; using the specified call service descriptor. Upon success, execution returns
102 * to {@link #handleSuccessfulIncomingCall} to start the in-call UI.
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800103 *
Ben Giladc5b22692014-02-18 20:03:22 -0800104 * @param descriptor The descriptor of the call service to use for this incoming call.
Evan Charltona05805b2014-03-05 08:21:46 -0800105 * @param extras The optional extras Bundle passed with the intent used for the incoming call.
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800106 */
Evan Charltona05805b2014-03-05 08:21:46 -0800107 void processIncomingCallIntent(CallServiceDescriptor descriptor, Bundle extras) {
Santos Cordon61d0f702014-02-19 02:52:23 -0800108 Log.d(TAG, "processIncomingCallIntent");
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800109 // Create a call with no handle. Eventually, switchboard will update the call with
110 // additional information from the call service, but for now we just need one to pass around
111 // with a unique call ID.
Santos Cordon493e8f22014-02-19 03:15:12 -0800112 Call call = new Call();
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800113
Evan Charltona05805b2014-03-05 08:21:46 -0800114 mSwitchboard.retrieveIncomingCall(call, descriptor, extras);
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800115 }
116
117 /**
Ben Gilada0d9f752014-02-26 11:49:03 -0800118 * Validates the specified call and, upon no objection to connect it, adds the new call to the
119 * list of live calls. Also notifies the in-call app so the user can answer or reject the call.
120 *
121 * @param call The new incoming call.
122 */
123 void handleSuccessfulIncomingCall(Call call) {
124 Log.d(TAG, "handleSuccessfulIncomingCall");
125 Preconditions.checkState(call.getState() == CallState.RINGING);
126
127 String handle = call.getHandle();
128 ContactInfo contactInfo = call.getContactInfo();
129 for (IncomingCallValidator validator : mIncomingCallValidators) {
130 if (!validator.isValid(handle, contactInfo)) {
131 // TODO(gilad): Consider displaying an error message.
132 Log.i(TAG, "Dropping restricted incoming call");
133 return;
134 }
135 }
136
137 // No objection to accept the incoming call, proceed with potentially connecting it (based
138 // on the user's action, or lack thereof).
139 addCall(call);
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800140
141 mUnansweredIncomingCalls.add(call);
142 if (mUnansweredIncomingCalls.size() == 1) {
143 // Start the ringer if we are the top-most incoming call (the only one in this case).
144 mRinger.startRinging();
145 }
Ben Gilada0d9f752014-02-26 11:49:03 -0800146 }
147
148 /**
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800149 * Attempts to issue/connect the specified call. From an (arbitrary) application standpoint,
150 * all that is required to initiate this flow is to fire either of the CALL, CALL_PRIVILEGED,
151 * and CALL_EMERGENCY intents. These are listened to by CallActivity.java which then invokes
152 * this method.
153 *
154 * @param handle The handle to dial.
155 * @param contactInfo Information about the entity being called.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800156 */
Ben Gilada0d9f752014-02-26 11:49:03 -0800157 void processOutgoingCallIntent(String handle, ContactInfo contactInfo) {
Ben Gilad8bdaa462014-02-05 12:53:19 -0800158 for (OutgoingCallValidator validator : mOutgoingCallValidators) {
Ben Gilada0d9f752014-02-26 11:49:03 -0800159 if (!validator.isValid(handle, contactInfo)) {
160 // TODO(gilad): Display an error message.
161 Log.i(TAG, "Dropping restricted outgoing call.");
162 return;
163 }
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800164 }
165
166 // No objection to issue the call, proceed with trying to put it through.
Ben Gilad13329fd2014-02-11 17:20:29 -0800167 Call call = new Call(handle, contactInfo);
Santos Cordonc195e362014-02-11 17:05:31 -0800168 mSwitchboard.placeOutgoingCall(call);
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800169 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800170
171 /**
Santos Cordon681663d2014-01-30 04:32:15 -0800172 * Adds a new outgoing call to the list of live calls and notifies the in-call app.
173 *
174 * @param call The new outgoing call.
175 */
176 void handleSuccessfulOutgoingCall(Call call) {
177 // OutgoingCallProcessor sets the call state to DIALING when it receives confirmation of the
178 // placed call from the call service so there is no need to set it here. Instead, check that
179 // the state is appropriate.
180 Preconditions.checkState(call.getState() == CallState.DIALING);
Santos Cordon681663d2014-01-30 04:32:15 -0800181 addCall(call);
Santos Cordon681663d2014-01-30 04:32:15 -0800182 }
183
184 /**
Santos Cordone3d76ab2014-01-28 17:25:20 -0800185 * Instructs Telecomm to answer the specified call. Intended to be invoked by the in-call
186 * app through {@link InCallAdapter} after Telecomm notifies it of an incoming call followed by
187 * the user opting to answer said call.
188 *
189 * @param callId The ID of the call.
190 */
191 void answerCall(String callId) {
Santos Cordon61d0f702014-02-19 02:52:23 -0800192 Call call = mCalls.get(callId);
193 if (call == null) {
194 Log.i(TAG, "Request to answer a non-existent call " + callId);
195 } else {
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800196 stopRinging(call);
197
Santos Cordon61d0f702014-02-19 02:52:23 -0800198 // We do not update the UI until we get confirmation of the answer() through
199 // {@link #markCallAsActive}. However, if we ever change that to look more responsive,
200 // then we need to make sure we add a timeout for the answer() in case the call never
201 // comes out of RINGING.
202 call.answer();
203 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800204 }
205
206 /**
207 * Instructs Telecomm to reject the specified call. Intended to be invoked by the in-call
208 * app through {@link InCallAdapter} after Telecomm notifies it of an incoming call followed by
209 * the user opting to reject said call.
210 *
211 * @param callId The ID of the call.
212 */
213 void rejectCall(String callId) {
Santos Cordon61d0f702014-02-19 02:52:23 -0800214 Call call = mCalls.get(callId);
215 if (call == null) {
216 Log.i(TAG, "Request to reject a non-existent call " + callId);
217 } else {
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800218 stopRinging(call);
Santos Cordon61d0f702014-02-19 02:52:23 -0800219 call.reject();
220 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800221 }
222
223 /**
224 * Instructs Telecomm to disconnect the specified call. Intended to be invoked by the
225 * in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered by
226 * the user hitting the end-call button.
227 *
228 * @param callId The ID of the call.
229 */
230 void disconnectCall(String callId) {
Santos Cordon049b7b62014-01-30 05:34:26 -0800231 Call call = mCalls.get(callId);
232 if (call == null) {
233 Log.e(TAG, "Unknown call (" + callId + ") asked to disconnect");
234 } else {
235 call.disconnect();
236 }
237
Santos Cordone3d76ab2014-01-28 17:25:20 -0800238 }
Santos Cordon681663d2014-01-30 04:32:15 -0800239
240 void markCallAsRinging(String callId) {
241 setCallState(callId, CallState.RINGING);
242 }
243
244 void markCallAsDialing(String callId) {
245 setCallState(callId, CallState.DIALING);
246 }
247
248 void markCallAsActive(String callId) {
249 setCallState(callId, CallState.ACTIVE);
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800250 removeFromUnansweredCalls(callId);
Sailesh Nepalfc511ca2014-02-20 18:48:53 -0800251 mInCallController.markCallAsActive(callId);
Santos Cordon681663d2014-01-30 04:32:15 -0800252 }
253
Santos Cordon049b7b62014-01-30 05:34:26 -0800254 /**
255 * Marks the specified call as DISCONNECTED and notifies the in-call app. If this was the last
256 * live call, then also disconnect from the in-call controller.
257 *
258 * @param callId The ID of the call.
259 */
Santos Cordon681663d2014-01-30 04:32:15 -0800260 void markCallAsDisconnected(String callId) {
261 setCallState(callId, CallState.DISCONNECTED);
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800262 removeFromUnansweredCalls(callId);
Santos Cordonc195e362014-02-11 17:05:31 -0800263
264 Call call = mCalls.remove(callId);
265 // At this point the call service has confirmed that the call is disconnected to it is
266 // safe to disassociate the call from its call service.
267 call.clearCallService();
Santos Cordon049b7b62014-01-30 05:34:26 -0800268
269 // Notify the in-call UI
270 mInCallController.markCallAsDisconnected(callId);
271 if (mCalls.isEmpty()) {
272 mInCallController.unbind();
273 }
Santos Cordon681663d2014-01-30 04:32:15 -0800274 }
275
276 /**
Ben Gilada0d9f752014-02-26 11:49:03 -0800277 * Sends all the live calls to the in-call app if any exist. If there are no live calls, then
278 * tells the in-call controller to unbind since it is not needed.
279 */
280 void updateInCall() {
281 if (mCalls.isEmpty()) {
282 mInCallController.unbind();
283 } else {
284 for (Call call : mCalls.values()) {
285 addInCallEntry(call);
286 }
287 }
288 }
289
290 /**
291 * Adds the specified call to the main list of live calls.
292 *
293 * @param call The call to add.
294 */
295 private void addCall(Call call) {
296 mCalls.put(call.getId(), call);
297 addInCallEntry(call);
298 }
299
300 /**
301 * Notifies the in-call app of the specified (new) call.
302 */
303 private void addInCallEntry(Call call) {
304 mInCallController.addCall(call.toCallInfo());
305 }
306
307 /**
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800308 * Sets the specified state on the specified call. Updates the ringer if the call is exiting
309 * the RINGING state.
Santos Cordon681663d2014-01-30 04:32:15 -0800310 *
311 * @param callId The ID of the call to update.
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800312 * @param newState The new state of the call.
Santos Cordon681663d2014-01-30 04:32:15 -0800313 */
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800314 private void setCallState(String callId, CallState newState) {
Santos Cordon681663d2014-01-30 04:32:15 -0800315 Preconditions.checkState(!Strings.isNullOrEmpty(callId));
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800316 Preconditions.checkNotNull(newState);
Santos Cordon681663d2014-01-30 04:32:15 -0800317
318 Call call = mCalls.get(callId);
319 if (call == null) {
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800320 Log.e(TAG, "Call " + callId + " was not found while attempting to update the state " +
321 "to " + newState + ".");
Santos Cordon681663d2014-01-30 04:32:15 -0800322 } else {
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800323 if (newState != call.getState()) {
324 // Unfortunately, in the telephony world the radio is king. So if the call notifies
325 // us that the call is in a particular state, we allow it even if it doesn't make
326 // sense (e.g., ACTIVE -> RINGING).
327 // TODO(santoscordon): Consider putting a stop to the above and turning CallState
328 // into a well-defined state machine.
329 // TODO(santoscordon): Define expected state transitions here, and log when an
330 // unexpected transition occurs.
331 call.setState(newState);
332 // TODO(santoscordon): Notify the in-call app whenever a call changes state.
333 }
334 }
335 }
336
337 /**
338 * Removes the specified call from the list of unanswered incoming calls and updates the ringer
339 * based on the new state of {@link #mUnansweredIncomingCalls}. Safe to call with a call ID that
340 * is not present in the list of incoming calls.
341 *
342 * @param callId The ID of the call.
343 */
344 private void removeFromUnansweredCalls(String callId) {
345 Call call = mCalls.get(callId);
346 if (call != null && mUnansweredIncomingCalls.remove(call)) {
347 if (mUnansweredIncomingCalls.isEmpty()) {
348 mRinger.stopRinging();
349 } else {
350 mRinger.startRinging();
351 }
352 }
353 }
354
355 /**
356 * Stops playing the ringer if the specified call is the top-most incoming call. This exists
357 * separately from {@link #removeIncomingCall} for cases where we would like to stop playing the
358 * ringer for a call, but that call may still exist in {@link #mUnansweredIncomingCalls} - See
359 * {@link #rejectCall}, {@link #answerCall}.
360 *
361 * @param call The call for which we should stop ringing.
362 */
363 private void stopRinging(Call call) {
364 // Only stop the ringer if this call is the top-most incoming call.
365 if (!mUnansweredIncomingCalls.isEmpty() && mUnansweredIncomingCalls.get(0) == call) {
366 mRinger.stopRinging();
Santos Cordon681663d2014-01-30 04:32:15 -0800367 }
368 }
Ben Gilad9f2bed32013-12-12 17:43:26 -0800369}