blob: 466f97f4de3d69d535f961248c5fdf0f83e58ffc [file] [log] [blame]
Santos Cordon8e8b8d22013-12-19 14:14:05 -08001/*
2 * Copyright 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 Gilad0bf5b912014-01-28 17:55:57 -080019import com.google.common.base.Preconditions;
Evan Charltonac1aa9e2014-01-03 13:47:12 -080020import com.google.common.collect.Lists;
Ben Gilad0bf5b912014-01-28 17:55:57 -080021import com.google.common.collect.Maps;
Ben Gilad0407fb22014-01-09 16:18:41 -080022import com.google.common.collect.Sets;
Santos Cordon8e8b8d22013-12-19 14:14:05 -080023
24import android.content.Context;
Ben Gilad2313e622014-02-06 12:02:25 -080025import android.os.Handler;
26import android.os.Looper;
Santos Cordon8e8b8d22013-12-19 14:14:05 -080027import android.telecomm.ICallService;
Ben Giladb59769e2014-01-16 11:41:10 -080028import android.telecomm.ICallServiceSelector;
Santos Cordon8e8b8d22013-12-19 14:14:05 -080029
Ben Giladb59769e2014-01-16 11:41:10 -080030import java.util.Collection;
Ben Gilad9f2bed32013-12-12 17:43:26 -080031import java.util.List;
Ben Gilad0bf5b912014-01-28 17:55:57 -080032import java.util.Map;
Ben Gilad0407fb22014-01-09 16:18:41 -080033import java.util.Set;
Ben Gilad9f2bed32013-12-12 17:43:26 -080034
Santos Cordon8e8b8d22013-12-19 14:14:05 -080035/**
36 * Switchboard is responsible for (1) selecting the {@link ICallService} through which to make
37 * outgoing calls and (2) switching active calls between transports (each ICallService is
38 * considered a different transport type).
Santos Cordon8e8b8d22013-12-19 14:14:05 -080039 */
Ben Giladd17443c2014-01-06 11:04:15 -080040final class Switchboard {
Ben Gilad2495d572014-01-09 17:26:19 -080041
Ben Gilad2313e622014-02-06 12:02:25 -080042 /**
43 * The frequency of invoking tick in milliseconds.
44 * TODO(gilad): May require tuning.
45 */
46 private final static int TICK_FREQUENCY = 250;
Ben Giladb59769e2014-01-16 11:41:10 -080047
Santos Cordon681663d2014-01-30 04:32:15 -080048 private final CallsManager mCallsManager;
Ben Gilad2313e622014-02-06 12:02:25 -080049
Santos Cordon681663d2014-01-30 04:32:15 -080050 /** Used to place outgoing calls. */
51 private final OutgoingCallsManager mOutgoingCallsManager;
52
53 private CallServiceFinder mCallServiceFinder;
54
55 private CallServiceSelectorFinder mSelectorFinder;
Ben Gilad2313e622014-02-06 12:02:25 -080056
57 /** Used to schedule tasks on the main (UI) thread. */
58 private final Handler mHandler = new Handler(Looper.getMainLooper());
59
60 /**
61 * Executes a single tick task and potentially schedules the next such that polling continues
62 * as long as necessary.
63 * NOTE(gilad): by design no two tick invocations should ever overlap.
64 */
65 private final Runnable mTicker = new Runnable() {
66 @Override
67 public void run() {
68 tick();
69 if (isTicking()) {
70 scheduleNextTick();
71 }
72 }
73 };
74
75 private final Set<Call> mNewOutgoingCalls = Sets.newLinkedHashSet();
76
77 private final Set<Call> mPendingOutgoingCalls = Sets.newLinkedHashSet();
Ben Giladb59769e2014-01-16 11:41:10 -080078
Ben Gilad03292d42014-01-16 15:06:16 -080079 /**
80 * The set of currently available call-service implementations, see {@link CallServiceFinder}.
81 * TODO(gilad): Null out once the active-call count goes to zero.
82 */
83 private Set<ICallService> mCallServices;
Ben Giladb59769e2014-01-16 11:41:10 -080084
Ben Gilad03292d42014-01-16 15:06:16 -080085 /**
86 * The set of currently available call-service-selector implementations,
87 * see {@link CallServiceSelectorFinder}.
88 * TODO(gilad): Null out once the active-call count goes to zero.
89 */
90 private Set<ICallServiceSelector> mSelectors;
Ben Gilad0407fb22014-01-09 16:18:41 -080091
Santos Cordon681663d2014-01-30 04:32:15 -080092 /**
93 * Persists the specified parameters and initializes Switchboard.
94 */
95 Switchboard() {
96 mCallsManager = CallsManager.getInstance();
97 mOutgoingCallsManager = new OutgoingCallsManager(this);
98 mCallServiceFinder = new CallServiceFinder(this, mOutgoingCallsManager);
99 mSelectorFinder = new CallServiceSelectorFinder(this);
100 }
Ben Giladd17443c2014-01-06 11:04:15 -0800101
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800102 /**
Ben Gilad8bdaa462014-02-05 12:53:19 -0800103 * Attempts to place an outgoing call to the specified handle.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800104 *
Ben Gilad0407fb22014-01-09 16:18:41 -0800105 * @param handle The handle to dial.
106 * @param contactInfo Information about the entity being called.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800107 * @param context The application context.
108 */
Ben Giladd17443c2014-01-06 11:04:15 -0800109 void placeOutgoingCall(String handle, ContactInfo contactInfo, Context context) {
Evan Charlton0958f532014-01-10 16:58:02 -0800110 ThreadUtil.checkOnMainThread();
Ben Giladb59769e2014-01-16 11:41:10 -0800111
112 // TODO(gilad): Consider creating the call object even earlier, e.g. in CallsManager.
113 Call call = new Call(handle, contactInfo);
114 boolean bailout = false;
115 if (isNullOrEmpty(mCallServices)) {
116 mCallServiceFinder.initiateLookup(context);
117 bailout = true;
118 }
119 if (isNullOrEmpty(mSelectors)) {
120 mSelectorFinder.initiateLookup(context);
121 bailout = true;
122 }
123
124 if (bailout) {
125 // Unable to process the call without either call service, selectors, or both.
126 // Store the call for deferred processing and bail out.
Ben Gilad0bf5b912014-01-28 17:55:57 -0800127 mNewOutgoingCalls.add(call);
Ben Giladb59769e2014-01-16 11:41:10 -0800128 return;
129 }
130
Ben Gilad0bf5b912014-01-28 17:55:57 -0800131 processNewOutgoingCall(call);
Ben Gilad0407fb22014-01-09 16:18:41 -0800132 }
Ben Gilad9f2bed32013-12-12 17:43:26 -0800133
Ben Gilad0407fb22014-01-09 16:18:41 -0800134 /**
Ben Gilad0bf5b912014-01-28 17:55:57 -0800135 * Persists the specified set of call services and attempts to place any pending outgoing
136 * calls. Intended to be invoked by {@link CallServiceFinder} exclusively.
Ben Gilad0407fb22014-01-09 16:18:41 -0800137 *
Ben Gilad03292d42014-01-16 15:06:16 -0800138 * @param callServices The potentially-partial set of call services. Partial since the lookup
139 * process is time-boxed, such that some providers/call-services may be slow to respond and
140 * hence effectively omitted from the specified list.
Ben Gilad0407fb22014-01-09 16:18:41 -0800141 */
Ben Gilad03292d42014-01-16 15:06:16 -0800142 void setCallServices(Set<ICallService> callServices) {
Evan Charlton0958f532014-01-10 16:58:02 -0800143 ThreadUtil.checkOnMainThread();
Ben Gilad0407fb22014-01-09 16:18:41 -0800144
Ben Giladb59769e2014-01-16 11:41:10 -0800145 mCallServices = callServices;
Ben Gilad0bf5b912014-01-28 17:55:57 -0800146 processNewOutgoingCalls();
Ben Giladb59769e2014-01-16 11:41:10 -0800147 }
148
149 /**
150 * Persists the specified list of selectors and attempts to connect any pending outgoing
Ben Gilad0bf5b912014-01-28 17:55:57 -0800151 * calls. Intended to be invoked by {@link CallServiceSelectorFinder} exclusively.
Ben Giladb59769e2014-01-16 11:41:10 -0800152 *
Ben Gilad03292d42014-01-16 15:06:16 -0800153 * @param selectors The potentially-partial set of selectors. Partial since the lookup
154 * procedure is time-boxed such that some selectors may be slow to respond and hence
155 * effectively omitted from the specified set.
Ben Giladb59769e2014-01-16 11:41:10 -0800156 */
Ben Gilad03292d42014-01-16 15:06:16 -0800157 void setSelectors(Set<ICallServiceSelector> selectors) {
Ben Giladb59769e2014-01-16 11:41:10 -0800158 ThreadUtil.checkOnMainThread();
159
Ben Gilad134cf092014-01-16 18:26:12 -0800160 // TODO(gilad): Add logic to include the built-in selectors (e.g. for dealing with
161 // emergency calls) and order the entire set prior to the assignment below. If the
162 // built-in selectors can be implemented in a manner that does not require binding,
163 // that's probably preferred. May want to use a LinkedHashSet for the sorted set.
Ben Giladb59769e2014-01-16 11:41:10 -0800164 mSelectors = selectors;
Ben Gilad0bf5b912014-01-28 17:55:57 -0800165 processNewOutgoingCalls();
Ben Giladb59769e2014-01-16 11:41:10 -0800166 }
167
168 /**
Ben Gilad0bf5b912014-01-28 17:55:57 -0800169 * Handles the case where an outgoing call has been successfully placed,
170 * see {@link OutgoingCallProcessor}.
Ben Giladb59769e2014-01-16 11:41:10 -0800171 */
Ben Gilad0bf5b912014-01-28 17:55:57 -0800172 void handleSuccessfulOutgoingCall(Call call) {
Santos Cordon681663d2014-01-30 04:32:15 -0800173 mCallsManager.handleSuccessfulOutgoingCall(call);
Ben Gilad0bf5b912014-01-28 17:55:57 -0800174
175 // Process additional (new) calls, if any.
176 processNewOutgoingCalls();
177 }
178
179 /**
180 * Handles the case where an outgoing call could not be proceed by any of the
181 * selector/call-service implementations, see {@link OutgoingCallProcessor}.
182 */
183 void handleFailedOutgoingCall(Call call) {
184 // TODO(gilad): More here.
185
186 // Process additional (new) calls, if any.
187 processNewOutgoingCalls();
188 }
189
190 /**
Ben Gilad2313e622014-02-06 12:02:25 -0800191 * @return True if ticking should continue (or be resumed) and false otherwise.
192 */
193 private boolean isTicking() {
194 // TODO(gilad): return true every time at least one outgoing call is pending (i.e. waiting
195 // to be connected by a call service) and also when at least one active call is switch-able
196 // between call services, see {@link ICallServiceSelector#isSwitchable}.
197 return false;
198 }
199
200 /**
201 * Schedules the next tick invocation.
202 */
203 private void scheduleNextTick() {
204 mHandler.postDelayed(mTicker, TICK_FREQUENCY);
205 }
206
207 /**
208 * Performs the set of tasks that needs to be executed on polling basis.
209 * TODO(gilad): Check for stale pending calls that may need to be terminated etc, see
210 * mNewOutgoingCalls and mPendingOutgoingCalls.
211 * TODO(gilad): Also intended to trigger the call switching/hand-off logic when applicable.
212 */
213 private void tick() {
214 // TODO(gilad): More here.
215 }
216
217 /**
Ben Gilad0bf5b912014-01-28 17:55:57 -0800218 * Attempts to process the next new outgoing calls that have not yet been expired.
219 */
220 private void processNewOutgoingCalls() {
221 if (isNullOrEmpty(mCallServices) || isNullOrEmpty(mSelectors)) {
222 // At least one call service and one selector are required to process outgoing calls.
223 return;
224 }
225
226 if (!mNewOutgoingCalls.isEmpty()) {
227 Call call = mNewOutgoingCalls.iterator().next();
228 mNewOutgoingCalls.remove(call);
229 mPendingOutgoingCalls.add(call);
230
231 // Specifically only attempt to place one call at a time such that call services
232 // can be freed from needing to deal with concurrent requests.
233 processNewOutgoingCall(call);
Ben Gilad0407fb22014-01-09 16:18:41 -0800234 }
Ben Gilad9f2bed32013-12-12 17:43:26 -0800235 }
236
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800237 /**
Ben Giladb59769e2014-01-16 11:41:10 -0800238 * Attempts to place the specified call.
239 *
Ben Gilad0bf5b912014-01-28 17:55:57 -0800240 * @param call The call to place.
Ben Giladb59769e2014-01-16 11:41:10 -0800241 */
Ben Gilad0bf5b912014-01-28 17:55:57 -0800242 private void processNewOutgoingCall(Call call) {
Ben Giladb59769e2014-01-16 11:41:10 -0800243
Ben Gilad0bf5b912014-01-28 17:55:57 -0800244 Preconditions.checkNotNull(mCallServices);
245 Preconditions.checkNotNull(mSelectors);
Ben Giladb59769e2014-01-16 11:41:10 -0800246
Ben Gilad0bf5b912014-01-28 17:55:57 -0800247 // Convert to (duplicate-free) list to aid index-based iteration, see the comment under
248 // setSelectors regarding using LinkedHashSet instead.
249 List<ICallServiceSelector> selectors = Lists.newArrayList();
250 selectors.addAll(mSelectors);
251
Santos Cordon681663d2014-01-30 04:32:15 -0800252 mOutgoingCallsManager.placeCall(call, mCallServices, selectors);
Ben Giladb59769e2014-01-16 11:41:10 -0800253 }
254
255 /**
256 * Determines whether or not the specified collection is either null or empty.
257 *
258 * @param collection Either null or the collection object to be evaluated.
259 * @return True if the collection is null or empty.
260 */
261 @SuppressWarnings("rawtypes")
262 private boolean isNullOrEmpty(Collection collection) {
263 return collection == null || collection.isEmpty();
264 }
265
266 /**
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800267 * Places an outgoing call to the handle passed in. Given a list of {@link ICallServices},
268 * select one and place a call to the handle.
269 * TODO(santoscordon): How does the CallService selection process work?
Ben Giladd17443c2014-01-06 11:04:15 -0800270 * TODO(gilad): Wire this logic from CallServiceFinder.updateSwitchboard.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800271 *
272 * @param handle The handle to dial.
273 * @param contactInfo Information about the entity being called.
274 * @param callServices The list of available {@link ICallService}s.
275 */
Ben Giladd17443c2014-01-06 11:04:15 -0800276// private void placeOutgoingCallInternal(
277// String handle,
278// ContactInfo contactInfo,
279// List<ICallService> callServices) throws CallServiceUnavailableException {
280//
281// Log.i(TAG, "Placing and outgoing call.");
282//
283// if (callServices.isEmpty()) {
284// // No call services, bail out.
285// // TODO(contacts-team): Add logging?
286// // TODO(santoscordon): Does this actually go anywhere considering this method is now
287// // asynchronous?
288// throw new CallServiceUnavailableException("No CallService found.");
289// }
290//
291// List<ICallService> compatibleCallServices = Lists.newArrayList();
292// for (ICallService service : callServices) {
293// // TODO(santoscordon): This code needs to be updated to an asynchronous response
294// // callback from isCompatibleWith().
295// /* if (service.isCompatibleWith(handle)) {
296// // NOTE(android-contacts): If we end up taking the liberty to issue
297// // calls not using the explicit user input (in case one is provided)
298// // and instead pull an alternative method of communication from the
299// // specified user-info object, it may be desirable to give precedence
300// // to services that can in fact respect the user's intent.
301// compatibleCallServices.add(service);
302// }
303// */
304// }
305//
306// if (compatibleCallServices.isEmpty()) {
307// // None of the available call services is suitable for making this call.
308// // TODO(contacts-team): Same here re logging.
309// throw new CallServiceUnavailableException("No compatible CallService found.");
310// }
311//
312// // NOTE(android-team): At this point we can also prompt the user for
313// // preference, i.e. instead of the logic just below.
314// if (compatibleCallServices.size() > 1) {
315// compatibleCallServices = sort(compatibleCallServices);
316// }
317// for (ICallService service : compatibleCallServices) {
318// try {
319// service.call(handle);
320// return;
321// } catch (RemoteException e) {
322// // TODO(santoscordon): Need some proxy for ICallService so that we don't have to
323// // avoid RemoteExceptionHandling everywhere. Take a look at how InputMethodService
324// // handles this.
325// }
326// // catch (OutgoingCallException ignored) {
327// // TODO(santoscordon): Figure out how OutgoingCallException falls into this. Should
328// // RemoteExceptions also be converted to OutgoingCallExceptions thrown by call()?
329// }
330// }
Ben Gilad9f2bed32013-12-12 17:43:26 -0800331}