blob: f084f6b435d5c1c99cdfc76f845f68186b1b8b74 [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 Cordon8e8b8d22013-12-19 14:14:05 -080050 /**
Santos Cordon681663d2014-01-30 04:32:15 -080051 * The main call repository. Keeps an instance of all live calls keyed by call ID. New incoming
52 * and outgoing calls are added to the map and removed when the calls move to the disconnected
53 * state.
54 * TODO(santoscordon): Add new CallId class and use it in place of String.
55 */
56 private final Map<String, Call> mCalls = Maps.newHashMap();
57
58 /**
Santos Cordon8e8b8d22013-12-19 14:14:05 -080059 * May be unnecessary per off-line discussions (between santoscordon and gilad) since the set
60 * of CallsManager APIs that need to be exposed to the dialer (or any application firing call
61 * intents) may be empty.
62 */
63 private DialerAdapter mDialerAdapter;
Ben Gilad9f2bed32013-12-12 17:43:26 -080064
Santos Cordon8e8b8d22013-12-19 14:14:05 -080065 private InCallAdapter mInCallAdapter;
Ben Gilad9f2bed32013-12-12 17:43:26 -080066
Santos Cordon8e8b8d22013-12-19 14:14:05 -080067 private CallLogManager mCallLogManager;
Ben Gilad9f2bed32013-12-12 17:43:26 -080068
Santos Cordon8e8b8d22013-12-19 14:14:05 -080069 private VoicemailManager mVoicemailManager;
Ben Gilad9f2bed32013-12-12 17:43:26 -080070
Ben Gilad8bdaa462014-02-05 12:53:19 -080071 private List<OutgoingCallValidator> mOutgoingCallValidators = Lists.newArrayList();
Ben Gilad9f2bed32013-12-12 17:43:26 -080072
Ben Gilad8bdaa462014-02-05 12:53:19 -080073 private List<IncomingCallValidator> mIncomingCallValidators = Lists.newArrayList();
Ben Gilad9f2bed32013-12-12 17:43:26 -080074
Santos Cordon8e8b8d22013-12-19 14:14:05 -080075 /**
Ben Gilad8bdaa462014-02-05 12:53:19 -080076 * Initializes the required Telecomm components.
Santos Cordon8e8b8d22013-12-19 14:14:05 -080077 */
78 private CallsManager() {
Santos Cordon6242b132014-02-07 16:24:42 -080079 mSwitchboard = new Switchboard(this);
80 mInCallController = new InCallController(this);
Santos Cordon8e8b8d22013-12-19 14:14:05 -080081 }
82
Ben Gilada0d9f752014-02-26 11:49:03 -080083 static CallsManager getInstance() {
84 return INSTANCE;
85 }
86
Santos Cordon8e8b8d22013-12-19 14:14:05 -080087 /**
Santos Cordon493e8f22014-02-19 03:15:12 -080088 * Starts the incoming call sequence by having switchboard gather more information about the
89 * specified call; using the specified call service descriptor. Upon success, execution returns
90 * to {@link #handleSuccessfulIncomingCall} to start the in-call UI.
Santos Cordon80d9bdc2014-02-13 18:28:46 -080091 *
Ben Giladc5b22692014-02-18 20:03:22 -080092 * @param descriptor The descriptor of the call service to use for this incoming call.
Evan Charltona05805b2014-03-05 08:21:46 -080093 * @param extras The optional extras Bundle passed with the intent used for the incoming call.
Santos Cordon80d9bdc2014-02-13 18:28:46 -080094 */
Evan Charltona05805b2014-03-05 08:21:46 -080095 void processIncomingCallIntent(CallServiceDescriptor descriptor, Bundle extras) {
Santos Cordon61d0f702014-02-19 02:52:23 -080096 Log.d(TAG, "processIncomingCallIntent");
Santos Cordon80d9bdc2014-02-13 18:28:46 -080097 // Create a call with no handle. Eventually, switchboard will update the call with
98 // additional information from the call service, but for now we just need one to pass around
99 // with a unique call ID.
Santos Cordon493e8f22014-02-19 03:15:12 -0800100 Call call = new Call();
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800101
Evan Charltona05805b2014-03-05 08:21:46 -0800102 mSwitchboard.retrieveIncomingCall(call, descriptor, extras);
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800103 }
104
105 /**
Ben Gilada0d9f752014-02-26 11:49:03 -0800106 * Validates the specified call and, upon no objection to connect it, adds the new call to the
107 * list of live calls. Also notifies the in-call app so the user can answer or reject the call.
108 *
109 * @param call The new incoming call.
110 */
111 void handleSuccessfulIncomingCall(Call call) {
112 Log.d(TAG, "handleSuccessfulIncomingCall");
113 Preconditions.checkState(call.getState() == CallState.RINGING);
114
115 String handle = call.getHandle();
116 ContactInfo contactInfo = call.getContactInfo();
117 for (IncomingCallValidator validator : mIncomingCallValidators) {
118 if (!validator.isValid(handle, contactInfo)) {
119 // TODO(gilad): Consider displaying an error message.
120 Log.i(TAG, "Dropping restricted incoming call");
121 return;
122 }
123 }
124
125 // No objection to accept the incoming call, proceed with potentially connecting it (based
126 // on the user's action, or lack thereof).
127 addCall(call);
128 }
129
130 /**
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800131 * Attempts to issue/connect the specified call. From an (arbitrary) application standpoint,
132 * all that is required to initiate this flow is to fire either of the CALL, CALL_PRIVILEGED,
133 * and CALL_EMERGENCY intents. These are listened to by CallActivity.java which then invokes
134 * this method.
135 *
136 * @param handle The handle to dial.
137 * @param contactInfo Information about the entity being called.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800138 */
Ben Gilada0d9f752014-02-26 11:49:03 -0800139 void processOutgoingCallIntent(String handle, ContactInfo contactInfo) {
Ben Gilad8bdaa462014-02-05 12:53:19 -0800140 for (OutgoingCallValidator validator : mOutgoingCallValidators) {
Ben Gilada0d9f752014-02-26 11:49:03 -0800141 if (!validator.isValid(handle, contactInfo)) {
142 // TODO(gilad): Display an error message.
143 Log.i(TAG, "Dropping restricted outgoing call.");
144 return;
145 }
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800146 }
147
148 // No objection to issue the call, proceed with trying to put it through.
Ben Gilad13329fd2014-02-11 17:20:29 -0800149 Call call = new Call(handle, contactInfo);
Santos Cordonc195e362014-02-11 17:05:31 -0800150 mSwitchboard.placeOutgoingCall(call);
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800151 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800152
153 /**
Santos Cordon681663d2014-01-30 04:32:15 -0800154 * Adds a new outgoing call to the list of live calls and notifies the in-call app.
155 *
156 * @param call The new outgoing call.
157 */
158 void handleSuccessfulOutgoingCall(Call call) {
159 // OutgoingCallProcessor sets the call state to DIALING when it receives confirmation of the
160 // placed call from the call service so there is no need to set it here. Instead, check that
161 // the state is appropriate.
162 Preconditions.checkState(call.getState() == CallState.DIALING);
Santos Cordon681663d2014-01-30 04:32:15 -0800163 addCall(call);
Santos Cordon681663d2014-01-30 04:32:15 -0800164 }
165
166 /**
Santos Cordone3d76ab2014-01-28 17:25:20 -0800167 * Instructs Telecomm to answer the specified call. Intended to be invoked by the in-call
168 * app through {@link InCallAdapter} after Telecomm notifies it of an incoming call followed by
169 * the user opting to answer said call.
170 *
171 * @param callId The ID of the call.
172 */
173 void answerCall(String callId) {
Santos Cordon61d0f702014-02-19 02:52:23 -0800174 Call call = mCalls.get(callId);
175 if (call == null) {
176 Log.i(TAG, "Request to answer a non-existent call " + callId);
177 } else {
178 // We do not update the UI until we get confirmation of the answer() through
179 // {@link #markCallAsActive}. However, if we ever change that to look more responsive,
180 // then we need to make sure we add a timeout for the answer() in case the call never
181 // comes out of RINGING.
182 call.answer();
183 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800184 }
185
186 /**
187 * Instructs Telecomm to reject 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 reject said call.
190 *
191 * @param callId The ID of the call.
192 */
193 void rejectCall(String callId) {
Santos Cordon61d0f702014-02-19 02:52:23 -0800194 Call call = mCalls.get(callId);
195 if (call == null) {
196 Log.i(TAG, "Request to reject a non-existent call " + callId);
197 } else {
198 call.reject();
199 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800200 }
201
202 /**
203 * Instructs Telecomm to disconnect the specified call. Intended to be invoked by the
204 * in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered by
205 * the user hitting the end-call button.
206 *
207 * @param callId The ID of the call.
208 */
209 void disconnectCall(String callId) {
Santos Cordon049b7b62014-01-30 05:34:26 -0800210 Call call = mCalls.get(callId);
211 if (call == null) {
212 Log.e(TAG, "Unknown call (" + callId + ") asked to disconnect");
213 } else {
214 call.disconnect();
215 }
216
Santos Cordone3d76ab2014-01-28 17:25:20 -0800217 }
Santos Cordon681663d2014-01-30 04:32:15 -0800218
219 void markCallAsRinging(String callId) {
220 setCallState(callId, CallState.RINGING);
221 }
222
223 void markCallAsDialing(String callId) {
224 setCallState(callId, CallState.DIALING);
225 }
226
227 void markCallAsActive(String callId) {
228 setCallState(callId, CallState.ACTIVE);
Sailesh Nepalfc511ca2014-02-20 18:48:53 -0800229 mInCallController.markCallAsActive(callId);
Santos Cordon681663d2014-01-30 04:32:15 -0800230 }
231
Santos Cordon049b7b62014-01-30 05:34:26 -0800232 /**
233 * Marks the specified call as DISCONNECTED and notifies the in-call app. If this was the last
234 * live call, then also disconnect from the in-call controller.
235 *
236 * @param callId The ID of the call.
237 */
Santos Cordon681663d2014-01-30 04:32:15 -0800238 void markCallAsDisconnected(String callId) {
239 setCallState(callId, CallState.DISCONNECTED);
Santos Cordonc195e362014-02-11 17:05:31 -0800240
241 Call call = mCalls.remove(callId);
242 // At this point the call service has confirmed that the call is disconnected to it is
243 // safe to disassociate the call from its call service.
244 call.clearCallService();
Santos Cordon049b7b62014-01-30 05:34:26 -0800245
246 // Notify the in-call UI
247 mInCallController.markCallAsDisconnected(callId);
248 if (mCalls.isEmpty()) {
249 mInCallController.unbind();
250 }
Santos Cordon681663d2014-01-30 04:32:15 -0800251 }
252
253 /**
Ben Gilada0d9f752014-02-26 11:49:03 -0800254 * Sends all the live calls to the in-call app if any exist. If there are no live calls, then
255 * tells the in-call controller to unbind since it is not needed.
256 */
257 void updateInCall() {
258 if (mCalls.isEmpty()) {
259 mInCallController.unbind();
260 } else {
261 for (Call call : mCalls.values()) {
262 addInCallEntry(call);
263 }
264 }
265 }
266
267 /**
268 * Adds the specified call to the main list of live calls.
269 *
270 * @param call The call to add.
271 */
272 private void addCall(Call call) {
273 mCalls.put(call.getId(), call);
274 addInCallEntry(call);
275 }
276
277 /**
278 * Notifies the in-call app of the specified (new) call.
279 */
280 private void addInCallEntry(Call call) {
281 mInCallController.addCall(call.toCallInfo());
282 }
283
284 /**
Santos Cordon681663d2014-01-30 04:32:15 -0800285 * Sets the specified state on the specified call.
286 *
287 * @param callId The ID of the call to update.
288 * @param state The new state of the call.
289 */
290 private void setCallState(String callId, CallState state) {
291 Preconditions.checkState(!Strings.isNullOrEmpty(callId));
292 Preconditions.checkNotNull(state);
293
294 Call call = mCalls.get(callId);
295 if (call == null) {
296 Log.e(TAG, "Call " + callId + " was not found while attempting to upda the state to " +
297 state + ".");
298 } else {
299 // Unfortunately, in the telephony world, the radio is king. So if the call notifies us
300 // that the call is in a particular state, we allow it even if it doesn't make sense
301 // (e.g., ACTIVE -> RINGING).
302 // TODO(santoscordon): Consider putting a stop to the above and turning CallState into
303 // a well-defined state machine.
304 // TODO(santoscordon): Define expected state transitions here, and log when an
305 // unexpected transition occurs.
306 call.setState(state);
307 // TODO(santoscordon): Notify the in-call app whenever a call changes state.
308 }
309 }
Ben Gilad9f2bed32013-12-12 17:43:26 -0800310}