blob: c96b8da288dd0b559f27c4d8e81303b57bb9516b [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;
Ben Gilad13329fd2014-02-11 17:20:29 -080025
Santos Cordon681663d2014-01-30 04:32:15 -080026import com.google.common.base.Preconditions;
27import com.google.common.base.Strings;
Santos Cordon8e8b8d22013-12-19 14:14:05 -080028import com.google.common.collect.Lists;
Santos Cordon681663d2014-01-30 04:32:15 -080029import com.google.common.collect.Maps;
Ben Gilad9f2bed32013-12-12 17:43:26 -080030
Ben Gilad9f2bed32013-12-12 17:43:26 -080031import java.util.List;
Santos Cordon681663d2014-01-30 04:32:15 -080032import java.util.Map;
Ben Gilad9f2bed32013-12-12 17:43:26 -080033
Ben Giladdd8c6082013-12-30 14:44:08 -080034/**
35 * Singleton.
36 *
37 * NOTE(gilad): by design most APIs are package private, use the relevant adapter/s to allow
38 * access from other packages specifically refraining from passing the CallsManager instance
39 * beyond the com.android.telecomm package boundary.
40 */
Santos Cordon8e8b8d22013-12-19 14:14:05 -080041public final class CallsManager {
Santos Cordon681663d2014-01-30 04:32:15 -080042 private static final String TAG = CallsManager.class.getSimpleName();
Ben Gilad9f2bed32013-12-12 17:43:26 -080043
Santos Cordon8e8b8d22013-12-19 14:14:05 -080044 private static final CallsManager INSTANCE = new CallsManager();
Ben Gilad9f2bed32013-12-12 17:43:26 -080045
Santos Cordone3d76ab2014-01-28 17:25:20 -080046 private final Switchboard mSwitchboard;
47
48 /** Used to control the in-call app. */
49 private final InCallController mInCallController;
50
Santos Cordon8e8b8d22013-12-19 14:14:05 -080051 /**
Santos Cordon681663d2014-01-30 04:32:15 -080052 * The main call repository. Keeps an instance of all live calls keyed by call ID. New incoming
53 * and outgoing calls are added to the map and removed when the calls move to the disconnected
54 * state.
55 * TODO(santoscordon): Add new CallId class and use it in place of String.
56 */
57 private final Map<String, Call> mCalls = Maps.newHashMap();
58
59 /**
Santos Cordon8e8b8d22013-12-19 14:14:05 -080060 * May be unnecessary per off-line discussions (between santoscordon and gilad) since the set
61 * of CallsManager APIs that need to be exposed to the dialer (or any application firing call
62 * intents) may be empty.
63 */
64 private DialerAdapter mDialerAdapter;
Ben Gilad9f2bed32013-12-12 17:43:26 -080065
Santos Cordon8e8b8d22013-12-19 14:14:05 -080066 private InCallAdapter mInCallAdapter;
Ben Gilad9f2bed32013-12-12 17:43:26 -080067
Santos Cordon8e8b8d22013-12-19 14:14:05 -080068 private CallLogManager mCallLogManager;
Ben Gilad9f2bed32013-12-12 17:43:26 -080069
Santos Cordon8e8b8d22013-12-19 14:14:05 -080070 private VoicemailManager mVoicemailManager;
Ben Gilad9f2bed32013-12-12 17:43:26 -080071
Ben Gilad8bdaa462014-02-05 12:53:19 -080072 private List<OutgoingCallValidator> mOutgoingCallValidators = Lists.newArrayList();
Ben Gilad9f2bed32013-12-12 17:43:26 -080073
Ben Gilad8bdaa462014-02-05 12:53:19 -080074 private List<IncomingCallValidator> mIncomingCallValidators = Lists.newArrayList();
Ben Gilad9f2bed32013-12-12 17:43:26 -080075
Santos Cordon8e8b8d22013-12-19 14:14:05 -080076 static CallsManager getInstance() {
77 return INSTANCE;
Ben Gilad9f2bed32013-12-12 17:43:26 -080078 }
79
Santos Cordon8e8b8d22013-12-19 14:14:05 -080080 /**
Ben Gilad8bdaa462014-02-05 12:53:19 -080081 * Initializes the required Telecomm components.
Santos Cordon8e8b8d22013-12-19 14:14:05 -080082 */
83 private CallsManager() {
Santos Cordon6242b132014-02-07 16:24:42 -080084 mSwitchboard = new Switchboard(this);
85 mInCallController = new InCallController(this);
Santos Cordon8e8b8d22013-12-19 14:14:05 -080086 }
87
88 /**
89 * Attempts to issue/connect the specified call. From an (arbitrary) application standpoint,
90 * all that is required to initiate this flow is to fire either of the CALL, CALL_PRIVILEGED,
91 * and CALL_EMERGENCY intents. These are listened to by CallActivity.java which then invokes
92 * this method.
93 *
94 * @param handle The handle to dial.
95 * @param contactInfo Information about the entity being called.
96 * @param context The application context.
97 */
98 void processOutgoingCallIntent(String handle, ContactInfo contactInfo, Context context)
Ben Gilad8bdaa462014-02-05 12:53:19 -080099 throws RestrictedCallException {
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800100
Ben Gilad8bdaa462014-02-05 12:53:19 -0800101 for (OutgoingCallValidator validator : mOutgoingCallValidators) {
102 validator.validate(handle, contactInfo);
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800103 }
104
105 // No objection to issue the call, proceed with trying to put it through.
Ben Gilad13329fd2014-02-11 17:20:29 -0800106 Call call = new Call(handle, contactInfo);
107 mSwitchboard.placeOutgoingCall(call, context);
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800108 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800109
110 /**
Santos Cordon681663d2014-01-30 04:32:15 -0800111 * Adds a new outgoing call to the list of live calls and notifies the in-call app.
112 *
113 * @param call The new outgoing call.
114 */
115 void handleSuccessfulOutgoingCall(Call call) {
116 // OutgoingCallProcessor sets the call state to DIALING when it receives confirmation of the
117 // placed call from the call service so there is no need to set it here. Instead, check that
118 // the state is appropriate.
119 Preconditions.checkState(call.getState() == CallState.DIALING);
120
121 addCall(call);
Santos Cordon049b7b62014-01-30 05:34:26 -0800122
123 mInCallController.addCall(call.toCallInfo());
124 }
125
126 /*
127 * Sends all the live calls to the in-call app if any exist. If there are no live calls, then
128 * tells the in-call controller to unbind since it is not needed.
129 */
130 void updateInCall() {
131 if (mCalls.isEmpty()) {
132 mInCallController.unbind();
133 return;
134 }
135
136 for (Call call : mCalls.values()) {
137 mInCallController.addCall(call.toCallInfo());
138 }
Santos Cordon681663d2014-01-30 04:32:15 -0800139 }
140
141 /**
Santos Cordone3d76ab2014-01-28 17:25:20 -0800142 * Instructs Telecomm to answer the specified call. Intended to be invoked by the in-call
143 * app through {@link InCallAdapter} after Telecomm notifies it of an incoming call followed by
144 * the user opting to answer said call.
145 *
146 * @param callId The ID of the call.
147 */
148 void answerCall(String callId) {
149 // TODO(santoscordon): fill in and check that it is in the ringing state.
150 }
151
152 /**
153 * Instructs Telecomm to reject the specified call. Intended to be invoked by the in-call
154 * app through {@link InCallAdapter} after Telecomm notifies it of an incoming call followed by
155 * the user opting to reject said call.
156 *
157 * @param callId The ID of the call.
158 */
159 void rejectCall(String callId) {
160 // TODO(santoscordon): fill in and check that it is in the ringing state.
161 }
162
163 /**
164 * Instructs Telecomm to disconnect the specified call. Intended to be invoked by the
165 * in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered by
166 * the user hitting the end-call button.
167 *
168 * @param callId The ID of the call.
169 */
170 void disconnectCall(String callId) {
Santos Cordon049b7b62014-01-30 05:34:26 -0800171 Call call = mCalls.get(callId);
172 if (call == null) {
173 Log.e(TAG, "Unknown call (" + callId + ") asked to disconnect");
174 } else {
175 call.disconnect();
176 }
177
Santos Cordone3d76ab2014-01-28 17:25:20 -0800178 }
Santos Cordon681663d2014-01-30 04:32:15 -0800179
180 void markCallAsRinging(String callId) {
181 setCallState(callId, CallState.RINGING);
182 }
183
184 void markCallAsDialing(String callId) {
185 setCallState(callId, CallState.DIALING);
186 }
187
188 void markCallAsActive(String callId) {
189 setCallState(callId, CallState.ACTIVE);
190 }
191
Santos Cordon049b7b62014-01-30 05:34:26 -0800192 /**
193 * Marks the specified call as DISCONNECTED and notifies the in-call app. If this was the last
194 * live call, then also disconnect from the in-call controller.
195 *
196 * @param callId The ID of the call.
197 */
Santos Cordon681663d2014-01-30 04:32:15 -0800198 void markCallAsDisconnected(String callId) {
199 setCallState(callId, CallState.DISCONNECTED);
200 mCalls.remove(callId);
Santos Cordon049b7b62014-01-30 05:34:26 -0800201
202 // Notify the in-call UI
203 mInCallController.markCallAsDisconnected(callId);
204 if (mCalls.isEmpty()) {
205 mInCallController.unbind();
206 }
Santos Cordon681663d2014-01-30 04:32:15 -0800207 }
208
209 /**
210 * Sets the specified state on the specified call.
211 *
212 * @param callId The ID of the call to update.
213 * @param state The new state of the call.
214 */
215 private void setCallState(String callId, CallState state) {
216 Preconditions.checkState(!Strings.isNullOrEmpty(callId));
217 Preconditions.checkNotNull(state);
218
219 Call call = mCalls.get(callId);
220 if (call == null) {
221 Log.e(TAG, "Call " + callId + " was not found while attempting to upda the state to " +
222 state + ".");
223 } else {
224 // Unfortunately, in the telephony world, the radio is king. So if the call notifies us
225 // that the call is in a particular state, we allow it even if it doesn't make sense
226 // (e.g., ACTIVE -> RINGING).
227 // TODO(santoscordon): Consider putting a stop to the above and turning CallState into
228 // a well-defined state machine.
229 // TODO(santoscordon): Define expected state transitions here, and log when an
230 // unexpected transition occurs.
231 call.setState(state);
232 // TODO(santoscordon): Notify the in-call app whenever a call changes state.
233 }
234 }
235
236 /**
237 * Adds the specified call to the main list of live calls.
238 *
239 * @param call The call to add.
240 */
241 private void addCall(Call call) {
242 mCalls.put(call.getId(), call);
243 }
Ben Gilad9f2bed32013-12-12 17:43:26 -0800244}