blob: 8903a96629fe42996f9f5335be84b8fd7c244f4b [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
Ben Gilad9f2bed32013-12-12 17:43:26 -080023import com.android.telecomm.exceptions.RestrictedCallException;
Ben Gilad13329fd2014-02-11 17:20:29 -080024
Santos Cordon681663d2014-01-30 04:32:15 -080025import com.google.common.base.Preconditions;
26import com.google.common.base.Strings;
Santos Cordon8e8b8d22013-12-19 14:14:05 -080027import com.google.common.collect.Lists;
Santos Cordon681663d2014-01-30 04:32:15 -080028import com.google.common.collect.Maps;
Ben Gilad9f2bed32013-12-12 17:43:26 -080029
Ben Gilad9f2bed32013-12-12 17:43:26 -080030import java.util.List;
Santos Cordon681663d2014-01-30 04:32:15 -080031import java.util.Map;
Ben Gilad9f2bed32013-12-12 17:43:26 -080032
Ben Giladdd8c6082013-12-30 14:44:08 -080033/**
34 * Singleton.
35 *
36 * NOTE(gilad): by design most APIs are package private, use the relevant adapter/s to allow
37 * access from other packages specifically refraining from passing the CallsManager instance
38 * beyond the com.android.telecomm package boundary.
39 */
Santos Cordon8e8b8d22013-12-19 14:14:05 -080040public final class CallsManager {
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 static CallsManager getInstance() {
76 return INSTANCE;
Ben Gilad9f2bed32013-12-12 17:43:26 -080077 }
78
Santos Cordon8e8b8d22013-12-19 14:14:05 -080079 /**
Ben Gilad8bdaa462014-02-05 12:53:19 -080080 * Initializes the required Telecomm components.
Santos Cordon8e8b8d22013-12-19 14:14:05 -080081 */
82 private CallsManager() {
Santos Cordon6242b132014-02-07 16:24:42 -080083 mSwitchboard = new Switchboard(this);
84 mInCallController = new InCallController(this);
Santos Cordon8e8b8d22013-12-19 14:14:05 -080085 }
86
87 /**
Santos Cordon80d9bdc2014-02-13 18:28:46 -080088 * Starts the incoming call sequence by having switchboard confirm with the specified call
89 * service that an incoming call actually exists for the specified call token. Upon success,
90 * execution returns to {@link #handleSuccessfulIncomingCall} to start the in-call UI.
91 *
Ben Giladc5b22692014-02-18 20:03:22 -080092 * @param descriptor The descriptor of the call service to use for this incoming call.
Santos Cordon80d9bdc2014-02-13 18:28:46 -080093 * @param callToken The token used by the call service to identify the incoming call.
94 */
Ben Giladc5b22692014-02-18 20:03:22 -080095 void processIncomingCallIntent(CallServiceDescriptor descriptor, String callToken) {
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 /**
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800105 * Attempts to issue/connect the specified call. From an (arbitrary) application standpoint,
106 * all that is required to initiate this flow is to fire either of the CALL, CALL_PRIVILEGED,
107 * and CALL_EMERGENCY intents. These are listened to by CallActivity.java which then invokes
108 * this method.
109 *
110 * @param handle The handle to dial.
111 * @param contactInfo Information about the entity being called.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800112 */
Ben Giladd239a032014-02-24 16:42:12 -0800113 void processOutgoingCallIntent(String handle, ContactInfo contactInfo)
Ben Gilad8bdaa462014-02-05 12:53:19 -0800114 throws RestrictedCallException {
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800115
Ben Gilad8bdaa462014-02-05 12:53:19 -0800116 for (OutgoingCallValidator validator : mOutgoingCallValidators) {
117 validator.validate(handle, contactInfo);
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800118 }
119
120 // No objection to issue the call, proceed with trying to put it through.
Ben Gilad13329fd2014-02-11 17:20:29 -0800121 Call call = new Call(handle, contactInfo);
Santos Cordonc195e362014-02-11 17:05:31 -0800122 mSwitchboard.placeOutgoingCall(call);
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800123 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800124
125 /**
Santos Cordon681663d2014-01-30 04:32:15 -0800126 * Adds a new outgoing call to the list of live calls and notifies the in-call app.
127 *
128 * @param call The new outgoing call.
129 */
130 void handleSuccessfulOutgoingCall(Call call) {
131 // OutgoingCallProcessor sets the call state to DIALING when it receives confirmation of the
132 // placed call from the call service so there is no need to set it here. Instead, check that
133 // the state is appropriate.
134 Preconditions.checkState(call.getState() == CallState.DIALING);
135
136 addCall(call);
Santos Cordon049b7b62014-01-30 05:34:26 -0800137
138 mInCallController.addCall(call.toCallInfo());
139 }
140
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800141 /**
142 * Adds a new incoming call to the list of live calls and notifies the in-call app.
143 *
144 * @param call The new incoming call.
145 */
146 void handleSuccessfulIncomingCall(Call call) {
147 Preconditions.checkState(call.getState() == CallState.RINGING);
148 addCall(call);
149 mInCallController.addCall(call.toCallInfo());
150 }
151
Santos Cordon049b7b62014-01-30 05:34:26 -0800152 /*
153 * Sends all the live calls to the in-call app if any exist. If there are no live calls, then
154 * tells the in-call controller to unbind since it is not needed.
155 */
156 void updateInCall() {
157 if (mCalls.isEmpty()) {
158 mInCallController.unbind();
159 return;
160 }
161
162 for (Call call : mCalls.values()) {
163 mInCallController.addCall(call.toCallInfo());
164 }
Santos Cordon681663d2014-01-30 04:32:15 -0800165 }
166
167 /**
Santos Cordone3d76ab2014-01-28 17:25:20 -0800168 * Instructs Telecomm to answer the specified call. Intended to be invoked by the in-call
169 * app through {@link InCallAdapter} after Telecomm notifies it of an incoming call followed by
170 * the user opting to answer said call.
171 *
172 * @param callId The ID of the call.
173 */
174 void answerCall(String callId) {
175 // TODO(santoscordon): fill in and check that it is in the ringing state.
176 }
177
178 /**
179 * Instructs Telecomm to reject the specified call. Intended to be invoked by the in-call
180 * app through {@link InCallAdapter} after Telecomm notifies it of an incoming call followed by
181 * the user opting to reject said call.
182 *
183 * @param callId The ID of the call.
184 */
185 void rejectCall(String callId) {
186 // TODO(santoscordon): fill in and check that it is in the ringing state.
187 }
188
189 /**
190 * Instructs Telecomm to disconnect the specified call. Intended to be invoked by the
191 * in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered by
192 * the user hitting the end-call button.
193 *
194 * @param callId The ID of the call.
195 */
196 void disconnectCall(String callId) {
Santos Cordon049b7b62014-01-30 05:34:26 -0800197 Call call = mCalls.get(callId);
198 if (call == null) {
199 Log.e(TAG, "Unknown call (" + callId + ") asked to disconnect");
200 } else {
201 call.disconnect();
202 }
203
Santos Cordone3d76ab2014-01-28 17:25:20 -0800204 }
Santos Cordon681663d2014-01-30 04:32:15 -0800205
206 void markCallAsRinging(String callId) {
207 setCallState(callId, CallState.RINGING);
208 }
209
210 void markCallAsDialing(String callId) {
211 setCallState(callId, CallState.DIALING);
212 }
213
214 void markCallAsActive(String callId) {
215 setCallState(callId, CallState.ACTIVE);
Sailesh Nepalfc511ca2014-02-20 18:48:53 -0800216 mInCallController.markCallAsActive(callId);
Santos Cordon681663d2014-01-30 04:32:15 -0800217 }
218
Santos Cordon049b7b62014-01-30 05:34:26 -0800219 /**
220 * Marks the specified call as DISCONNECTED and notifies the in-call app. If this was the last
221 * live call, then also disconnect from the in-call controller.
222 *
223 * @param callId The ID of the call.
224 */
Santos Cordon681663d2014-01-30 04:32:15 -0800225 void markCallAsDisconnected(String callId) {
226 setCallState(callId, CallState.DISCONNECTED);
Santos Cordonc195e362014-02-11 17:05:31 -0800227
228 Call call = mCalls.remove(callId);
229 // At this point the call service has confirmed that the call is disconnected to it is
230 // safe to disassociate the call from its call service.
231 call.clearCallService();
Santos Cordon049b7b62014-01-30 05:34:26 -0800232
233 // Notify the in-call UI
234 mInCallController.markCallAsDisconnected(callId);
235 if (mCalls.isEmpty()) {
236 mInCallController.unbind();
237 }
Santos Cordon681663d2014-01-30 04:32:15 -0800238 }
239
240 /**
241 * Sets the specified state on the specified call.
242 *
243 * @param callId The ID of the call to update.
244 * @param state The new state of the call.
245 */
246 private void setCallState(String callId, CallState state) {
247 Preconditions.checkState(!Strings.isNullOrEmpty(callId));
248 Preconditions.checkNotNull(state);
249
250 Call call = mCalls.get(callId);
251 if (call == null) {
252 Log.e(TAG, "Call " + callId + " was not found while attempting to upda the state to " +
253 state + ".");
254 } else {
255 // Unfortunately, in the telephony world, the radio is king. So if the call notifies us
256 // that the call is in a particular state, we allow it even if it doesn't make sense
257 // (e.g., ACTIVE -> RINGING).
258 // TODO(santoscordon): Consider putting a stop to the above and turning CallState into
259 // a well-defined state machine.
260 // TODO(santoscordon): Define expected state transitions here, and log when an
261 // unexpected transition occurs.
262 call.setState(state);
263 // TODO(santoscordon): Notify the in-call app whenever a call changes state.
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 }
Ben Gilad9f2bed32013-12-12 17:43:26 -0800275}