blob: ff224e5af9d8ea8e7c65c5b475a5299c380840b0 [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
Santos Cordon8e8b8d22013-12-19 14:14:05 -080019import android.content.Context;
Santos Cordon80d9bdc2014-02-13 18:28:46 -080020import android.telecomm.CallServiceInfo;
Santos Cordon681663d2014-01-30 04:32:15 -080021import android.telecomm.CallState;
22import android.text.TextUtils;
23import android.util.Log;
Santos Cordon8e8b8d22013-12-19 14:14:05 -080024
Ben Gilad9f2bed32013-12-12 17:43:26 -080025import com.android.telecomm.exceptions.RestrictedCallException;
Ben Gilad13329fd2014-02-11 17:20:29 -080026
Santos Cordon681663d2014-01-30 04:32:15 -080027import com.google.common.base.Preconditions;
28import com.google.common.base.Strings;
Santos Cordon8e8b8d22013-12-19 14:14:05 -080029import com.google.common.collect.Lists;
Santos Cordon681663d2014-01-30 04:32:15 -080030import com.google.common.collect.Maps;
Ben Gilad9f2bed32013-12-12 17:43:26 -080031
Ben Gilad9f2bed32013-12-12 17:43:26 -080032import java.util.List;
Santos Cordon681663d2014-01-30 04:32:15 -080033import java.util.Map;
Ben Gilad9f2bed32013-12-12 17:43:26 -080034
Ben Giladdd8c6082013-12-30 14:44:08 -080035/**
36 * Singleton.
37 *
38 * NOTE(gilad): by design most APIs are package private, use the relevant adapter/s to allow
39 * access from other packages specifically refraining from passing the CallsManager instance
40 * beyond the com.android.telecomm package boundary.
41 */
Santos Cordon8e8b8d22013-12-19 14:14:05 -080042public final class CallsManager {
Santos Cordon681663d2014-01-30 04:32:15 -080043 private static final String TAG = CallsManager.class.getSimpleName();
Ben Gilad9f2bed32013-12-12 17:43:26 -080044
Santos Cordon8e8b8d22013-12-19 14:14:05 -080045 private static final CallsManager INSTANCE = new CallsManager();
Ben Gilad9f2bed32013-12-12 17:43:26 -080046
Santos Cordone3d76ab2014-01-28 17:25:20 -080047 private final Switchboard mSwitchboard;
48
49 /** Used to control the in-call app. */
50 private final InCallController mInCallController;
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 Cordon8e8b8d22013-12-19 14:14:05 -080061 * May be unnecessary per off-line discussions (between santoscordon and gilad) since the set
62 * of CallsManager APIs that need to be exposed to the dialer (or any application firing call
63 * intents) may be empty.
64 */
65 private DialerAdapter mDialerAdapter;
Ben Gilad9f2bed32013-12-12 17:43:26 -080066
Santos Cordon8e8b8d22013-12-19 14:14:05 -080067 private InCallAdapter mInCallAdapter;
Ben Gilad9f2bed32013-12-12 17:43:26 -080068
Santos Cordon8e8b8d22013-12-19 14:14:05 -080069 private CallLogManager mCallLogManager;
Ben Gilad9f2bed32013-12-12 17:43:26 -080070
Santos Cordon8e8b8d22013-12-19 14:14:05 -080071 private VoicemailManager mVoicemailManager;
Ben Gilad9f2bed32013-12-12 17:43:26 -080072
Ben Gilad8bdaa462014-02-05 12:53:19 -080073 private List<OutgoingCallValidator> mOutgoingCallValidators = Lists.newArrayList();
Ben Gilad9f2bed32013-12-12 17:43:26 -080074
Ben Gilad8bdaa462014-02-05 12:53:19 -080075 private List<IncomingCallValidator> mIncomingCallValidators = Lists.newArrayList();
Ben Gilad9f2bed32013-12-12 17:43:26 -080076
Santos Cordon8e8b8d22013-12-19 14:14:05 -080077 static CallsManager getInstance() {
78 return INSTANCE;
Ben Gilad9f2bed32013-12-12 17:43:26 -080079 }
80
Santos Cordon8e8b8d22013-12-19 14:14:05 -080081 /**
Ben Gilad8bdaa462014-02-05 12:53:19 -080082 * Initializes the required Telecomm components.
Santos Cordon8e8b8d22013-12-19 14:14:05 -080083 */
84 private CallsManager() {
Santos Cordon6242b132014-02-07 16:24:42 -080085 mSwitchboard = new Switchboard(this);
86 mInCallController = new InCallController(this);
Santos Cordon8e8b8d22013-12-19 14:14:05 -080087 }
88
89 /**
Santos Cordon80d9bdc2014-02-13 18:28:46 -080090 * Starts the incoming call sequence by having switchboard confirm with the specified call
91 * service that an incoming call actually exists for the specified call token. Upon success,
92 * execution returns to {@link #handleSuccessfulIncomingCall} to start the in-call UI.
93 *
94 * @param callServiceInfo The details of the call service to use for this incoming call.
95 * @param callToken The token used by the call service to identify the incoming call.
96 */
97 void processIncomingCallIntent(CallServiceInfo callServiceInfo, String callToken) {
98 // Create a call with no handle. Eventually, switchboard will update the call with
99 // additional information from the call service, but for now we just need one to pass around
100 // with a unique call ID.
101 Call call = new Call(null, null);
102
103 mSwitchboard.confirmIncomingCall(call, callServiceInfo, callToken);
104 }
105
106 /**
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800107 * Attempts to issue/connect the specified call. From an (arbitrary) application standpoint,
108 * all that is required to initiate this flow is to fire either of the CALL, CALL_PRIVILEGED,
109 * and CALL_EMERGENCY intents. These are listened to by CallActivity.java which then invokes
110 * this method.
111 *
112 * @param handle The handle to dial.
113 * @param contactInfo Information about the entity being called.
114 * @param context The application context.
115 */
116 void processOutgoingCallIntent(String handle, ContactInfo contactInfo, Context context)
Ben Gilad8bdaa462014-02-05 12:53:19 -0800117 throws RestrictedCallException {
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800118
Ben Gilad8bdaa462014-02-05 12:53:19 -0800119 for (OutgoingCallValidator validator : mOutgoingCallValidators) {
120 validator.validate(handle, contactInfo);
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800121 }
122
123 // No objection to issue the call, proceed with trying to put it through.
Ben Gilad13329fd2014-02-11 17:20:29 -0800124 Call call = new Call(handle, contactInfo);
Santos Cordonc195e362014-02-11 17:05:31 -0800125 mSwitchboard.placeOutgoingCall(call);
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800126 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800127
128 /**
Santos Cordon681663d2014-01-30 04:32:15 -0800129 * Adds a new outgoing call to the list of live calls and notifies the in-call app.
130 *
131 * @param call The new outgoing call.
132 */
133 void handleSuccessfulOutgoingCall(Call call) {
134 // OutgoingCallProcessor sets the call state to DIALING when it receives confirmation of the
135 // placed call from the call service so there is no need to set it here. Instead, check that
136 // the state is appropriate.
137 Preconditions.checkState(call.getState() == CallState.DIALING);
138
139 addCall(call);
Santos Cordon049b7b62014-01-30 05:34:26 -0800140
141 mInCallController.addCall(call.toCallInfo());
142 }
143
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800144 /**
145 * Adds a new incoming call to the list of live calls and notifies the in-call app.
146 *
147 * @param call The new incoming call.
148 */
149 void handleSuccessfulIncomingCall(Call call) {
150 Preconditions.checkState(call.getState() == CallState.RINGING);
151 addCall(call);
152 mInCallController.addCall(call.toCallInfo());
153 }
154
Santos Cordon049b7b62014-01-30 05:34:26 -0800155 /*
156 * Sends all the live calls to the in-call app if any exist. If there are no live calls, then
157 * tells the in-call controller to unbind since it is not needed.
158 */
159 void updateInCall() {
160 if (mCalls.isEmpty()) {
161 mInCallController.unbind();
162 return;
163 }
164
165 for (Call call : mCalls.values()) {
166 mInCallController.addCall(call.toCallInfo());
167 }
Santos Cordon681663d2014-01-30 04:32:15 -0800168 }
169
170 /**
Santos Cordone3d76ab2014-01-28 17:25:20 -0800171 * Instructs Telecomm to answer the specified call. Intended to be invoked by the in-call
172 * app through {@link InCallAdapter} after Telecomm notifies it of an incoming call followed by
173 * the user opting to answer said call.
174 *
175 * @param callId The ID of the call.
176 */
177 void answerCall(String callId) {
178 // TODO(santoscordon): fill in and check that it is in the ringing state.
179 }
180
181 /**
182 * Instructs Telecomm to reject the specified call. Intended to be invoked by the in-call
183 * app through {@link InCallAdapter} after Telecomm notifies it of an incoming call followed by
184 * the user opting to reject said call.
185 *
186 * @param callId The ID of the call.
187 */
188 void rejectCall(String callId) {
189 // TODO(santoscordon): fill in and check that it is in the ringing state.
190 }
191
192 /**
193 * Instructs Telecomm to disconnect the specified call. Intended to be invoked by the
194 * in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered by
195 * the user hitting the end-call button.
196 *
197 * @param callId The ID of the call.
198 */
199 void disconnectCall(String callId) {
Santos Cordon049b7b62014-01-30 05:34:26 -0800200 Call call = mCalls.get(callId);
201 if (call == null) {
202 Log.e(TAG, "Unknown call (" + callId + ") asked to disconnect");
203 } else {
204 call.disconnect();
205 }
206
Santos Cordone3d76ab2014-01-28 17:25:20 -0800207 }
Santos Cordon681663d2014-01-30 04:32:15 -0800208
209 void markCallAsRinging(String callId) {
210 setCallState(callId, CallState.RINGING);
211 }
212
213 void markCallAsDialing(String callId) {
214 setCallState(callId, CallState.DIALING);
215 }
216
217 void markCallAsActive(String callId) {
218 setCallState(callId, CallState.ACTIVE);
219 }
220
Santos Cordon049b7b62014-01-30 05:34:26 -0800221 /**
222 * Marks the specified call as DISCONNECTED and notifies the in-call app. If this was the last
223 * live call, then also disconnect from the in-call controller.
224 *
225 * @param callId The ID of the call.
226 */
Santos Cordon681663d2014-01-30 04:32:15 -0800227 void markCallAsDisconnected(String callId) {
228 setCallState(callId, CallState.DISCONNECTED);
Santos Cordonc195e362014-02-11 17:05:31 -0800229
230 Call call = mCalls.remove(callId);
231 // At this point the call service has confirmed that the call is disconnected to it is
232 // safe to disassociate the call from its call service.
233 call.clearCallService();
Santos Cordon049b7b62014-01-30 05:34:26 -0800234
235 // Notify the in-call UI
236 mInCallController.markCallAsDisconnected(callId);
237 if (mCalls.isEmpty()) {
238 mInCallController.unbind();
239 }
Santos Cordon681663d2014-01-30 04:32:15 -0800240 }
241
242 /**
243 * Sets the specified state on the specified call.
244 *
245 * @param callId The ID of the call to update.
246 * @param state The new state of the call.
247 */
248 private void setCallState(String callId, CallState state) {
249 Preconditions.checkState(!Strings.isNullOrEmpty(callId));
250 Preconditions.checkNotNull(state);
251
252 Call call = mCalls.get(callId);
253 if (call == null) {
254 Log.e(TAG, "Call " + callId + " was not found while attempting to upda the state to " +
255 state + ".");
256 } else {
257 // Unfortunately, in the telephony world, the radio is king. So if the call notifies us
258 // that the call is in a particular state, we allow it even if it doesn't make sense
259 // (e.g., ACTIVE -> RINGING).
260 // TODO(santoscordon): Consider putting a stop to the above and turning CallState into
261 // a well-defined state machine.
262 // TODO(santoscordon): Define expected state transitions here, and log when an
263 // unexpected transition occurs.
264 call.setState(state);
265 // TODO(santoscordon): Notify the in-call app whenever a call changes state.
266 }
267 }
268
269 /**
270 * Adds the specified call to the main list of live calls.
271 *
272 * @param call The call to add.
273 */
274 private void addCall(Call call) {
275 mCalls.put(call.getId(), call);
276 }
Ben Gilad9f2bed32013-12-12 17:43:26 -0800277}