blob: 2a621aa3023713be47a74fadde5ad80385067245 [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 Cordon681663d2014-01-30 04:32:15 -080020import android.telecomm.CallState;
21import android.text.TextUtils;
22import android.util.Log;
Santos Cordon8e8b8d22013-12-19 14:14:05 -080023
Ben Gilad9f2bed32013-12-12 17:43:26 -080024import com.android.telecomm.exceptions.RestrictedCallException;
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() {
83 mSwitchboard = new Switchboard();
Santos Cordon681663d2014-01-30 04:32:15 -080084 mInCallController = new InCallController();
Santos Cordon8e8b8d22013-12-19 14:14:05 -080085 }
86
87 /**
88 * Attempts to issue/connect the specified call. From an (arbitrary) application standpoint,
89 * all that is required to initiate this flow is to fire either of the CALL, CALL_PRIVILEGED,
90 * and CALL_EMERGENCY intents. These are listened to by CallActivity.java which then invokes
91 * this method.
92 *
93 * @param handle The handle to dial.
94 * @param contactInfo Information about the entity being called.
95 * @param context The application context.
96 */
97 void processOutgoingCallIntent(String handle, ContactInfo contactInfo, Context context)
Ben Gilad8bdaa462014-02-05 12:53:19 -080098 throws RestrictedCallException {
Santos Cordon8e8b8d22013-12-19 14:14:05 -080099
Ben Gilad8bdaa462014-02-05 12:53:19 -0800100 for (OutgoingCallValidator validator : mOutgoingCallValidators) {
101 validator.validate(handle, contactInfo);
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800102 }
103
104 // No objection to issue the call, proceed with trying to put it through.
105 mSwitchboard.placeOutgoingCall(handle, contactInfo, context);
106 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800107
108 /**
Santos Cordon681663d2014-01-30 04:32:15 -0800109 * Adds a new outgoing call to the list of live calls and notifies the in-call app.
110 *
111 * @param call The new outgoing call.
112 */
113 void handleSuccessfulOutgoingCall(Call call) {
114 // OutgoingCallProcessor sets the call state to DIALING when it receives confirmation of the
115 // placed call from the call service so there is no need to set it here. Instead, check that
116 // the state is appropriate.
117 Preconditions.checkState(call.getState() == CallState.DIALING);
118
119 addCall(call);
Santos Cordon049b7b62014-01-30 05:34:26 -0800120
121 mInCallController.addCall(call.toCallInfo());
122 }
123
124 /*
125 * Sends all the live calls to the in-call app if any exist. If there are no live calls, then
126 * tells the in-call controller to unbind since it is not needed.
127 */
128 void updateInCall() {
129 if (mCalls.isEmpty()) {
130 mInCallController.unbind();
131 return;
132 }
133
134 for (Call call : mCalls.values()) {
135 mInCallController.addCall(call.toCallInfo());
136 }
Santos Cordon681663d2014-01-30 04:32:15 -0800137 }
138
139 /**
Santos Cordone3d76ab2014-01-28 17:25:20 -0800140 * Instructs Telecomm to answer the specified call. Intended to be invoked by the in-call
141 * app through {@link InCallAdapter} after Telecomm notifies it of an incoming call followed by
142 * the user opting to answer said call.
143 *
144 * @param callId The ID of the call.
145 */
146 void answerCall(String callId) {
147 // TODO(santoscordon): fill in and check that it is in the ringing state.
148 }
149
150 /**
151 * Instructs Telecomm to reject the specified call. Intended to be invoked by the in-call
152 * app through {@link InCallAdapter} after Telecomm notifies it of an incoming call followed by
153 * the user opting to reject said call.
154 *
155 * @param callId The ID of the call.
156 */
157 void rejectCall(String callId) {
158 // TODO(santoscordon): fill in and check that it is in the ringing state.
159 }
160
161 /**
162 * Instructs Telecomm to disconnect the specified call. Intended to be invoked by the
163 * in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered by
164 * the user hitting the end-call button.
165 *
166 * @param callId The ID of the call.
167 */
168 void disconnectCall(String callId) {
Santos Cordon049b7b62014-01-30 05:34:26 -0800169 Call call = mCalls.get(callId);
170 if (call == null) {
171 Log.e(TAG, "Unknown call (" + callId + ") asked to disconnect");
172 } else {
173 call.disconnect();
174 }
175
Santos Cordone3d76ab2014-01-28 17:25:20 -0800176 }
Santos Cordon681663d2014-01-30 04:32:15 -0800177
178 void markCallAsRinging(String callId) {
179 setCallState(callId, CallState.RINGING);
180 }
181
182 void markCallAsDialing(String callId) {
183 setCallState(callId, CallState.DIALING);
184 }
185
186 void markCallAsActive(String callId) {
187 setCallState(callId, CallState.ACTIVE);
188 }
189
Santos Cordon049b7b62014-01-30 05:34:26 -0800190 /**
191 * Marks the specified call as DISCONNECTED and notifies the in-call app. If this was the last
192 * live call, then also disconnect from the in-call controller.
193 *
194 * @param callId The ID of the call.
195 */
Santos Cordon681663d2014-01-30 04:32:15 -0800196 void markCallAsDisconnected(String callId) {
197 setCallState(callId, CallState.DISCONNECTED);
198 mCalls.remove(callId);
Santos Cordon049b7b62014-01-30 05:34:26 -0800199
200 // Notify the in-call UI
201 mInCallController.markCallAsDisconnected(callId);
202 if (mCalls.isEmpty()) {
203 mInCallController.unbind();
204 }
Santos Cordon681663d2014-01-30 04:32:15 -0800205 }
206
207 /**
208 * Sets the specified state on the specified call.
209 *
210 * @param callId The ID of the call to update.
211 * @param state The new state of the call.
212 */
213 private void setCallState(String callId, CallState state) {
214 Preconditions.checkState(!Strings.isNullOrEmpty(callId));
215 Preconditions.checkNotNull(state);
216
217 Call call = mCalls.get(callId);
218 if (call == null) {
219 Log.e(TAG, "Call " + callId + " was not found while attempting to upda the state to " +
220 state + ".");
221 } else {
222 // Unfortunately, in the telephony world, the radio is king. So if the call notifies us
223 // that the call is in a particular state, we allow it even if it doesn't make sense
224 // (e.g., ACTIVE -> RINGING).
225 // TODO(santoscordon): Consider putting a stop to the above and turning CallState into
226 // a well-defined state machine.
227 // TODO(santoscordon): Define expected state transitions here, and log when an
228 // unexpected transition occurs.
229 call.setState(state);
230 // TODO(santoscordon): Notify the in-call app whenever a call changes state.
231 }
232 }
233
234 /**
235 * Adds the specified call to the main list of live calls.
236 *
237 * @param call The call to add.
238 */
239 private void addCall(Call call) {
240 mCalls.put(call.getId(), call);
241 }
Ben Gilad9f2bed32013-12-12 17:43:26 -0800242}