blob: c049e18458833946d29ef4c985539b43d9572fbc [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 Cordon8e8b8d22013-12-19 14:14:05 -080049 /**
Santos Cordon681663d2014-01-30 04:32:15 -080050 * The main call repository. Keeps an instance of all live calls keyed by call ID. New incoming
51 * and outgoing calls are added to the map and removed when the calls move to the disconnected
52 * state.
53 * TODO(santoscordon): Add new CallId class and use it in place of String.
54 */
55 private final Map<String, Call> mCalls = Maps.newHashMap();
56
57 /**
Santos Cordon8e8b8d22013-12-19 14:14:05 -080058 * May be unnecessary per off-line discussions (between santoscordon and gilad) since the set
59 * of CallsManager APIs that need to be exposed to the dialer (or any application firing call
60 * intents) may be empty.
61 */
62 private DialerAdapter mDialerAdapter;
Ben Gilad9f2bed32013-12-12 17:43:26 -080063
Santos Cordon8e8b8d22013-12-19 14:14:05 -080064 private InCallAdapter mInCallAdapter;
Ben Gilad9f2bed32013-12-12 17:43:26 -080065
Santos Cordon8e8b8d22013-12-19 14:14:05 -080066 private CallLogManager mCallLogManager;
Ben Gilad9f2bed32013-12-12 17:43:26 -080067
Santos Cordon8e8b8d22013-12-19 14:14:05 -080068 private VoicemailManager mVoicemailManager;
Ben Gilad9f2bed32013-12-12 17:43:26 -080069
Ben Gilad8bdaa462014-02-05 12:53:19 -080070 private List<OutgoingCallValidator> mOutgoingCallValidators = Lists.newArrayList();
Ben Gilad9f2bed32013-12-12 17:43:26 -080071
Ben Gilad8bdaa462014-02-05 12:53:19 -080072 private List<IncomingCallValidator> mIncomingCallValidators = Lists.newArrayList();
Ben Gilad9f2bed32013-12-12 17:43:26 -080073
Santos Cordon8e8b8d22013-12-19 14:14:05 -080074 /**
Ben Gilad8bdaa462014-02-05 12:53:19 -080075 * Initializes the required Telecomm components.
Santos Cordon8e8b8d22013-12-19 14:14:05 -080076 */
77 private CallsManager() {
Santos Cordon6242b132014-02-07 16:24:42 -080078 mSwitchboard = new Switchboard(this);
79 mInCallController = new InCallController(this);
Santos Cordon8e8b8d22013-12-19 14:14:05 -080080 }
81
Ben Gilada0d9f752014-02-26 11:49:03 -080082 static CallsManager getInstance() {
83 return INSTANCE;
84 }
85
Santos Cordon8e8b8d22013-12-19 14:14:05 -080086 /**
Santos Cordon493e8f22014-02-19 03:15:12 -080087 * Starts the incoming call sequence by having switchboard gather more information about the
88 * specified call; using the specified call service descriptor. Upon success, execution returns
89 * to {@link #handleSuccessfulIncomingCall} to start the in-call UI.
Santos Cordon80d9bdc2014-02-13 18:28:46 -080090 *
Ben Giladc5b22692014-02-18 20:03:22 -080091 * @param descriptor The descriptor of the call service to use for this incoming call.
Santos Cordon80d9bdc2014-02-13 18:28:46 -080092 */
Santos Cordon493e8f22014-02-19 03:15:12 -080093 void processIncomingCallIntent(CallServiceDescriptor descriptor) {
Santos Cordon61d0f702014-02-19 02:52:23 -080094 Log.d(TAG, "processIncomingCallIntent");
Santos Cordon80d9bdc2014-02-13 18:28:46 -080095 // Create a call with no handle. Eventually, switchboard will update the call with
96 // additional information from the call service, but for now we just need one to pass around
97 // with a unique call ID.
Santos Cordon493e8f22014-02-19 03:15:12 -080098 Call call = new Call();
Santos Cordon80d9bdc2014-02-13 18:28:46 -080099
Santos Cordon493e8f22014-02-19 03:15:12 -0800100 mSwitchboard.retrieveIncomingCall(call, descriptor);
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800101 }
102
103 /**
Ben Gilada0d9f752014-02-26 11:49:03 -0800104 * Validates the specified call and, upon no objection to connect it, adds the new call to the
105 * list of live calls. Also notifies the in-call app so the user can answer or reject the call.
106 *
107 * @param call The new incoming call.
108 */
109 void handleSuccessfulIncomingCall(Call call) {
110 Log.d(TAG, "handleSuccessfulIncomingCall");
111 Preconditions.checkState(call.getState() == CallState.RINGING);
112
113 String handle = call.getHandle();
114 ContactInfo contactInfo = call.getContactInfo();
115 for (IncomingCallValidator validator : mIncomingCallValidators) {
116 if (!validator.isValid(handle, contactInfo)) {
117 // TODO(gilad): Consider displaying an error message.
118 Log.i(TAG, "Dropping restricted incoming call");
119 return;
120 }
121 }
122
123 // No objection to accept the incoming call, proceed with potentially connecting it (based
124 // on the user's action, or lack thereof).
125 addCall(call);
126 }
127
128 /**
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800129 * Attempts to issue/connect the specified call. From an (arbitrary) application standpoint,
130 * all that is required to initiate this flow is to fire either of the CALL, CALL_PRIVILEGED,
131 * and CALL_EMERGENCY intents. These are listened to by CallActivity.java which then invokes
132 * this method.
133 *
134 * @param handle The handle to dial.
135 * @param contactInfo Information about the entity being called.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800136 */
Ben Gilada0d9f752014-02-26 11:49:03 -0800137 void processOutgoingCallIntent(String handle, ContactInfo contactInfo) {
Ben Gilad8bdaa462014-02-05 12:53:19 -0800138 for (OutgoingCallValidator validator : mOutgoingCallValidators) {
Ben Gilada0d9f752014-02-26 11:49:03 -0800139 if (!validator.isValid(handle, contactInfo)) {
140 // TODO(gilad): Display an error message.
141 Log.i(TAG, "Dropping restricted outgoing call.");
142 return;
143 }
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800144 }
145
146 // No objection to issue the call, proceed with trying to put it through.
Ben Gilad13329fd2014-02-11 17:20:29 -0800147 Call call = new Call(handle, contactInfo);
Santos Cordonc195e362014-02-11 17:05:31 -0800148 mSwitchboard.placeOutgoingCall(call);
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800149 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800150
151 /**
Santos Cordon681663d2014-01-30 04:32:15 -0800152 * Adds a new outgoing call to the list of live calls and notifies the in-call app.
153 *
154 * @param call The new outgoing call.
155 */
156 void handleSuccessfulOutgoingCall(Call call) {
157 // OutgoingCallProcessor sets the call state to DIALING when it receives confirmation of the
158 // placed call from the call service so there is no need to set it here. Instead, check that
159 // the state is appropriate.
160 Preconditions.checkState(call.getState() == CallState.DIALING);
Santos Cordon681663d2014-01-30 04:32:15 -0800161 addCall(call);
Santos Cordon681663d2014-01-30 04:32:15 -0800162 }
163
164 /**
Santos Cordone3d76ab2014-01-28 17:25:20 -0800165 * Instructs Telecomm to answer the specified call. Intended to be invoked by the in-call
166 * app through {@link InCallAdapter} after Telecomm notifies it of an incoming call followed by
167 * the user opting to answer said call.
168 *
169 * @param callId The ID of the call.
170 */
171 void answerCall(String callId) {
Santos Cordon61d0f702014-02-19 02:52:23 -0800172 Call call = mCalls.get(callId);
173 if (call == null) {
174 Log.i(TAG, "Request to answer a non-existent call " + callId);
175 } else {
176 // We do not update the UI until we get confirmation of the answer() through
177 // {@link #markCallAsActive}. However, if we ever change that to look more responsive,
178 // then we need to make sure we add a timeout for the answer() in case the call never
179 // comes out of RINGING.
180 call.answer();
181 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800182 }
183
184 /**
185 * Instructs Telecomm to reject 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 reject said call.
188 *
189 * @param callId The ID of the call.
190 */
191 void rejectCall(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 reject a non-existent call " + callId);
195 } else {
196 call.reject();
197 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800198 }
199
200 /**
201 * Instructs Telecomm to disconnect the specified call. Intended to be invoked by the
202 * in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered by
203 * the user hitting the end-call button.
204 *
205 * @param callId The ID of the call.
206 */
207 void disconnectCall(String callId) {
Santos Cordon049b7b62014-01-30 05:34:26 -0800208 Call call = mCalls.get(callId);
209 if (call == null) {
210 Log.e(TAG, "Unknown call (" + callId + ") asked to disconnect");
211 } else {
212 call.disconnect();
213 }
214
Santos Cordone3d76ab2014-01-28 17:25:20 -0800215 }
Santos Cordon681663d2014-01-30 04:32:15 -0800216
217 void markCallAsRinging(String callId) {
218 setCallState(callId, CallState.RINGING);
219 }
220
221 void markCallAsDialing(String callId) {
222 setCallState(callId, CallState.DIALING);
223 }
224
225 void markCallAsActive(String callId) {
226 setCallState(callId, CallState.ACTIVE);
Sailesh Nepalfc511ca2014-02-20 18:48:53 -0800227 mInCallController.markCallAsActive(callId);
Santos Cordon681663d2014-01-30 04:32:15 -0800228 }
229
Santos Cordon049b7b62014-01-30 05:34:26 -0800230 /**
231 * Marks the specified call as DISCONNECTED and notifies the in-call app. If this was the last
232 * live call, then also disconnect from the in-call controller.
233 *
234 * @param callId The ID of the call.
235 */
Santos Cordon681663d2014-01-30 04:32:15 -0800236 void markCallAsDisconnected(String callId) {
237 setCallState(callId, CallState.DISCONNECTED);
Santos Cordonc195e362014-02-11 17:05:31 -0800238
239 Call call = mCalls.remove(callId);
240 // At this point the call service has confirmed that the call is disconnected to it is
241 // safe to disassociate the call from its call service.
242 call.clearCallService();
Santos Cordon049b7b62014-01-30 05:34:26 -0800243
244 // Notify the in-call UI
245 mInCallController.markCallAsDisconnected(callId);
246 if (mCalls.isEmpty()) {
247 mInCallController.unbind();
248 }
Santos Cordon681663d2014-01-30 04:32:15 -0800249 }
250
251 /**
Ben Gilada0d9f752014-02-26 11:49:03 -0800252 * Sends all the live calls to the in-call app if any exist. If there are no live calls, then
253 * tells the in-call controller to unbind since it is not needed.
254 */
255 void updateInCall() {
256 if (mCalls.isEmpty()) {
257 mInCallController.unbind();
258 } else {
259 for (Call call : mCalls.values()) {
260 addInCallEntry(call);
261 }
262 }
263 }
264
265 /**
266 * Adds the specified call to the main list of live calls.
267 *
268 * @param call The call to add.
269 */
270 private void addCall(Call call) {
271 mCalls.put(call.getId(), call);
272 addInCallEntry(call);
273 }
274
275 /**
276 * Notifies the in-call app of the specified (new) call.
277 */
278 private void addInCallEntry(Call call) {
279 mInCallController.addCall(call.toCallInfo());
280 }
281
282 /**
Santos Cordon681663d2014-01-30 04:32:15 -0800283 * Sets the specified state on the specified call.
284 *
285 * @param callId The ID of the call to update.
286 * @param state The new state of the call.
287 */
288 private void setCallState(String callId, CallState state) {
289 Preconditions.checkState(!Strings.isNullOrEmpty(callId));
290 Preconditions.checkNotNull(state);
291
292 Call call = mCalls.get(callId);
293 if (call == null) {
294 Log.e(TAG, "Call " + callId + " was not found while attempting to upda the state to " +
295 state + ".");
296 } else {
297 // Unfortunately, in the telephony world, the radio is king. So if the call notifies us
298 // that the call is in a particular state, we allow it even if it doesn't make sense
299 // (e.g., ACTIVE -> RINGING).
300 // TODO(santoscordon): Consider putting a stop to the above and turning CallState into
301 // a well-defined state machine.
302 // TODO(santoscordon): Define expected state transitions here, and log when an
303 // unexpected transition occurs.
304 call.setState(state);
305 // TODO(santoscordon): Notify the in-call app whenever a call changes state.
306 }
307 }
Ben Gilad9f2bed32013-12-12 17:43:26 -0800308}