blob: 99221021e0518d4c2861ad52ab4080a1f26050ad [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 Cordon80d9bdc2014-02-13 18:28:46 -080087 * Starts the incoming call sequence by having switchboard confirm with the specified call
88 * service that an incoming call actually exists for the specified call token. Upon success,
89 * execution returns to {@link #handleSuccessfulIncomingCall} to start the in-call UI.
90 *
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 * @param callToken The token used by the call service to identify the incoming call.
93 */
Ben Giladc5b22692014-02-18 20:03:22 -080094 void processIncomingCallIntent(CallServiceDescriptor descriptor, String callToken) {
Santos Cordon61d0f702014-02-19 02:52:23 -080095 Log.d(TAG, "processIncomingCallIntent");
Santos Cordon80d9bdc2014-02-13 18:28:46 -080096 // Create a call with no handle. Eventually, switchboard will update the call with
97 // additional information from the call service, but for now we just need one to pass around
98 // with a unique call ID.
99 Call call = new Call(null, null);
100
Ben Giladc5b22692014-02-18 20:03:22 -0800101 mSwitchboard.confirmIncomingCall(call, descriptor, callToken);
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800102 }
103
104 /**
Ben Gilada0d9f752014-02-26 11:49:03 -0800105 * Validates the specified call and, upon no objection to connect it, adds the new call to the
106 * list of live calls. Also notifies the in-call app so the user can answer or reject the call.
107 *
108 * @param call The new incoming call.
109 */
110 void handleSuccessfulIncomingCall(Call call) {
111 Log.d(TAG, "handleSuccessfulIncomingCall");
112 Preconditions.checkState(call.getState() == CallState.RINGING);
113
114 String handle = call.getHandle();
115 ContactInfo contactInfo = call.getContactInfo();
116 for (IncomingCallValidator validator : mIncomingCallValidators) {
117 if (!validator.isValid(handle, contactInfo)) {
118 // TODO(gilad): Consider displaying an error message.
119 Log.i(TAG, "Dropping restricted incoming call");
120 return;
121 }
122 }
123
124 // No objection to accept the incoming call, proceed with potentially connecting it (based
125 // on the user's action, or lack thereof).
126 addCall(call);
127 }
128
129 /**
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800130 * Attempts to issue/connect the specified call. From an (arbitrary) application standpoint,
131 * all that is required to initiate this flow is to fire either of the CALL, CALL_PRIVILEGED,
132 * and CALL_EMERGENCY intents. These are listened to by CallActivity.java which then invokes
133 * this method.
134 *
135 * @param handle The handle to dial.
136 * @param contactInfo Information about the entity being called.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800137 */
Ben Gilada0d9f752014-02-26 11:49:03 -0800138 void processOutgoingCallIntent(String handle, ContactInfo contactInfo) {
Ben Gilad8bdaa462014-02-05 12:53:19 -0800139 for (OutgoingCallValidator validator : mOutgoingCallValidators) {
Ben Gilada0d9f752014-02-26 11:49:03 -0800140 if (!validator.isValid(handle, contactInfo)) {
141 // TODO(gilad): Display an error message.
142 Log.i(TAG, "Dropping restricted outgoing call.");
143 return;
144 }
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800145 }
146
147 // No objection to issue the call, proceed with trying to put it through.
Ben Gilad13329fd2014-02-11 17:20:29 -0800148 Call call = new Call(handle, contactInfo);
Santos Cordonc195e362014-02-11 17:05:31 -0800149 mSwitchboard.placeOutgoingCall(call);
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800150 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800151
152 /**
Santos Cordon681663d2014-01-30 04:32:15 -0800153 * Adds a new outgoing call to the list of live calls and notifies the in-call app.
154 *
155 * @param call The new outgoing call.
156 */
157 void handleSuccessfulOutgoingCall(Call call) {
158 // OutgoingCallProcessor sets the call state to DIALING when it receives confirmation of the
159 // placed call from the call service so there is no need to set it here. Instead, check that
160 // the state is appropriate.
161 Preconditions.checkState(call.getState() == CallState.DIALING);
Santos Cordon681663d2014-01-30 04:32:15 -0800162 addCall(call);
Santos Cordon681663d2014-01-30 04:32:15 -0800163 }
164
165 /**
Santos Cordone3d76ab2014-01-28 17:25:20 -0800166 * Instructs Telecomm to answer the specified call. Intended to be invoked by the in-call
167 * app through {@link InCallAdapter} after Telecomm notifies it of an incoming call followed by
168 * the user opting to answer said call.
169 *
170 * @param callId The ID of the call.
171 */
172 void answerCall(String callId) {
Santos Cordon61d0f702014-02-19 02:52:23 -0800173 Call call = mCalls.get(callId);
174 if (call == null) {
175 Log.i(TAG, "Request to answer a non-existent call " + callId);
176 } else {
177 // We do not update the UI until we get confirmation of the answer() through
178 // {@link #markCallAsActive}. However, if we ever change that to look more responsive,
179 // then we need to make sure we add a timeout for the answer() in case the call never
180 // comes out of RINGING.
181 call.answer();
182 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800183 }
184
185 /**
186 * Instructs Telecomm to reject the specified call. Intended to be invoked by the in-call
187 * app through {@link InCallAdapter} after Telecomm notifies it of an incoming call followed by
188 * the user opting to reject said call.
189 *
190 * @param callId The ID of the call.
191 */
192 void rejectCall(String callId) {
Santos Cordon61d0f702014-02-19 02:52:23 -0800193 Call call = mCalls.get(callId);
194 if (call == null) {
195 Log.i(TAG, "Request to reject a non-existent call " + callId);
196 } else {
197 call.reject();
198 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800199 }
200
201 /**
202 * Instructs Telecomm to disconnect the specified call. Intended to be invoked by the
203 * in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered by
204 * the user hitting the end-call button.
205 *
206 * @param callId The ID of the call.
207 */
208 void disconnectCall(String callId) {
Santos Cordon049b7b62014-01-30 05:34:26 -0800209 Call call = mCalls.get(callId);
210 if (call == null) {
211 Log.e(TAG, "Unknown call (" + callId + ") asked to disconnect");
212 } else {
213 call.disconnect();
214 }
215
Santos Cordone3d76ab2014-01-28 17:25:20 -0800216 }
Santos Cordon681663d2014-01-30 04:32:15 -0800217
218 void markCallAsRinging(String callId) {
219 setCallState(callId, CallState.RINGING);
220 }
221
222 void markCallAsDialing(String callId) {
223 setCallState(callId, CallState.DIALING);
224 }
225
226 void markCallAsActive(String callId) {
227 setCallState(callId, CallState.ACTIVE);
Sailesh Nepalfc511ca2014-02-20 18:48:53 -0800228 mInCallController.markCallAsActive(callId);
Santos Cordon681663d2014-01-30 04:32:15 -0800229 }
230
Santos Cordon049b7b62014-01-30 05:34:26 -0800231 /**
232 * Marks the specified call as DISCONNECTED and notifies the in-call app. If this was the last
233 * live call, then also disconnect from the in-call controller.
234 *
235 * @param callId The ID of the call.
236 */
Santos Cordon681663d2014-01-30 04:32:15 -0800237 void markCallAsDisconnected(String callId) {
238 setCallState(callId, CallState.DISCONNECTED);
Santos Cordonc195e362014-02-11 17:05:31 -0800239
240 Call call = mCalls.remove(callId);
241 // At this point the call service has confirmed that the call is disconnected to it is
242 // safe to disassociate the call from its call service.
243 call.clearCallService();
Santos Cordon049b7b62014-01-30 05:34:26 -0800244
245 // Notify the in-call UI
246 mInCallController.markCallAsDisconnected(callId);
247 if (mCalls.isEmpty()) {
248 mInCallController.unbind();
249 }
Santos Cordon681663d2014-01-30 04:32:15 -0800250 }
251
252 /**
Ben Gilada0d9f752014-02-26 11:49:03 -0800253 * Sends all the live calls to the in-call app if any exist. If there are no live calls, then
254 * tells the in-call controller to unbind since it is not needed.
255 */
256 void updateInCall() {
257 if (mCalls.isEmpty()) {
258 mInCallController.unbind();
259 } else {
260 for (Call call : mCalls.values()) {
261 addInCallEntry(call);
262 }
263 }
264 }
265
266 /**
267 * Adds the specified call to the main list of live calls.
268 *
269 * @param call The call to add.
270 */
271 private void addCall(Call call) {
272 mCalls.put(call.getId(), call);
273 addInCallEntry(call);
274 }
275
276 /**
277 * Notifies the in-call app of the specified (new) call.
278 */
279 private void addInCallEntry(Call call) {
280 mInCallController.addCall(call.toCallInfo());
281 }
282
283 /**
Santos Cordon681663d2014-01-30 04:32:15 -0800284 * Sets the specified state on the specified call.
285 *
286 * @param callId The ID of the call to update.
287 * @param state The new state of the call.
288 */
289 private void setCallState(String callId, CallState state) {
290 Preconditions.checkState(!Strings.isNullOrEmpty(callId));
291 Preconditions.checkNotNull(state);
292
293 Call call = mCalls.get(callId);
294 if (call == null) {
295 Log.e(TAG, "Call " + callId + " was not found while attempting to upda the state to " +
296 state + ".");
297 } else {
298 // Unfortunately, in the telephony world, the radio is king. So if the call notifies us
299 // that the call is in a particular state, we allow it even if it doesn't make sense
300 // (e.g., ACTIVE -> RINGING).
301 // TODO(santoscordon): Consider putting a stop to the above and turning CallState into
302 // a well-defined state machine.
303 // TODO(santoscordon): Define expected state transitions here, and log when an
304 // unexpected transition occurs.
305 call.setState(state);
306 // TODO(santoscordon): Notify the in-call app whenever a call changes state.
307 }
308 }
Ben Gilad9f2bed32013-12-12 17:43:26 -0800309}