blob: 68ffc73870dff7033145543678606435c820d6a3 [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
Ben Gilad2313e622014-02-06 12:02:25 -080048 private final CallServiceFinder mCallServiceFinder = new CallServiceFinder(this);
49
50 private final CallServiceSelectorFinder mSelectorFinder = new CallServiceSelectorFinder(this);
51
52 /** Used to schedule tasks on the main (UI) thread. */
53 private final Handler mHandler = new Handler(Looper.getMainLooper());
54
55 /**
56 * Executes a single tick task and potentially schedules the next such that polling continues
57 * as long as necessary.
58 * NOTE(gilad): by design no two tick invocations should ever overlap.
59 */
60 private final Runnable mTicker = new Runnable() {
61 @Override
62 public void run() {
63 tick();
64 if (isTicking()) {
65 scheduleNextTick();
66 }
67 }
68 };
69
70 private final Set<Call> mNewOutgoingCalls = Sets.newLinkedHashSet();
71
72 private final Set<Call> mPendingOutgoingCalls = Sets.newLinkedHashSet();
Ben Giladb59769e2014-01-16 11:41:10 -080073
Ben Gilad03292d42014-01-16 15:06:16 -080074 /**
75 * The set of currently available call-service implementations, see {@link CallServiceFinder}.
76 * TODO(gilad): Null out once the active-call count goes to zero.
77 */
78 private Set<ICallService> mCallServices;
Ben Giladb59769e2014-01-16 11:41:10 -080079
Ben Gilad03292d42014-01-16 15:06:16 -080080 /**
81 * The set of currently available call-service-selector implementations,
82 * see {@link CallServiceSelectorFinder}.
83 * TODO(gilad): Null out once the active-call count goes to zero.
84 */
85 private Set<ICallServiceSelector> mSelectors;
Ben Gilad0407fb22014-01-09 16:18:41 -080086
Ben Gilad0bf5b912014-01-28 17:55:57 -080087 private Map<Call, OutgoingCallProcessor> outgoingCallProcessors = Maps.newHashMap();
Ben Giladd17443c2014-01-06 11:04:15 -080088
Santos Cordon8e8b8d22013-12-19 14:14:05 -080089 /**
Ben Gilad8bdaa462014-02-05 12:53:19 -080090 * Attempts to place an outgoing call to the specified handle.
Santos Cordon8e8b8d22013-12-19 14:14:05 -080091 *
Ben Gilad0407fb22014-01-09 16:18:41 -080092 * @param handle The handle to dial.
93 * @param contactInfo Information about the entity being called.
Santos Cordon8e8b8d22013-12-19 14:14:05 -080094 * @param context The application context.
95 */
Ben Giladd17443c2014-01-06 11:04:15 -080096 void placeOutgoingCall(String handle, ContactInfo contactInfo, Context context) {
Evan Charlton0958f532014-01-10 16:58:02 -080097 ThreadUtil.checkOnMainThread();
Ben Giladb59769e2014-01-16 11:41:10 -080098
99 // TODO(gilad): Consider creating the call object even earlier, e.g. in CallsManager.
100 Call call = new Call(handle, contactInfo);
101 boolean bailout = false;
102 if (isNullOrEmpty(mCallServices)) {
103 mCallServiceFinder.initiateLookup(context);
104 bailout = true;
105 }
106 if (isNullOrEmpty(mSelectors)) {
107 mSelectorFinder.initiateLookup(context);
108 bailout = true;
109 }
110
111 if (bailout) {
112 // Unable to process the call without either call service, selectors, or both.
113 // Store the call for deferred processing and bail out.
Ben Gilad0bf5b912014-01-28 17:55:57 -0800114 mNewOutgoingCalls.add(call);
Ben Giladb59769e2014-01-16 11:41:10 -0800115 return;
116 }
117
Ben Gilad0bf5b912014-01-28 17:55:57 -0800118 processNewOutgoingCall(call);
Ben Gilad0407fb22014-01-09 16:18:41 -0800119 }
Ben Gilad9f2bed32013-12-12 17:43:26 -0800120
Ben Gilad0407fb22014-01-09 16:18:41 -0800121 /**
Ben Gilad0bf5b912014-01-28 17:55:57 -0800122 * Persists the specified set of call services and attempts to place any pending outgoing
123 * calls. Intended to be invoked by {@link CallServiceFinder} exclusively.
Ben Gilad0407fb22014-01-09 16:18:41 -0800124 *
Ben Gilad03292d42014-01-16 15:06:16 -0800125 * @param callServices The potentially-partial set of call services. Partial since the lookup
126 * process is time-boxed, such that some providers/call-services may be slow to respond and
127 * hence effectively omitted from the specified list.
Ben Gilad0407fb22014-01-09 16:18:41 -0800128 */
Ben Gilad03292d42014-01-16 15:06:16 -0800129 void setCallServices(Set<ICallService> callServices) {
Evan Charlton0958f532014-01-10 16:58:02 -0800130 ThreadUtil.checkOnMainThread();
Ben Gilad0407fb22014-01-09 16:18:41 -0800131
Ben Giladb59769e2014-01-16 11:41:10 -0800132 mCallServices = callServices;
Ben Gilad0bf5b912014-01-28 17:55:57 -0800133 processNewOutgoingCalls();
Ben Giladb59769e2014-01-16 11:41:10 -0800134 }
135
136 /**
137 * Persists the specified list of selectors and attempts to connect any pending outgoing
Ben Gilad0bf5b912014-01-28 17:55:57 -0800138 * calls. Intended to be invoked by {@link CallServiceSelectorFinder} exclusively.
Ben Giladb59769e2014-01-16 11:41:10 -0800139 *
Ben Gilad03292d42014-01-16 15:06:16 -0800140 * @param selectors The potentially-partial set of selectors. Partial since the lookup
141 * procedure is time-boxed such that some selectors may be slow to respond and hence
142 * effectively omitted from the specified set.
Ben Giladb59769e2014-01-16 11:41:10 -0800143 */
Ben Gilad03292d42014-01-16 15:06:16 -0800144 void setSelectors(Set<ICallServiceSelector> selectors) {
Ben Giladb59769e2014-01-16 11:41:10 -0800145 ThreadUtil.checkOnMainThread();
146
Ben Gilad134cf092014-01-16 18:26:12 -0800147 // TODO(gilad): Add logic to include the built-in selectors (e.g. for dealing with
148 // emergency calls) and order the entire set prior to the assignment below. If the
149 // built-in selectors can be implemented in a manner that does not require binding,
150 // that's probably preferred. May want to use a LinkedHashSet for the sorted set.
Ben Giladb59769e2014-01-16 11:41:10 -0800151 mSelectors = selectors;
Ben Gilad0bf5b912014-01-28 17:55:57 -0800152 processNewOutgoingCalls();
Ben Giladb59769e2014-01-16 11:41:10 -0800153 }
154
155 /**
Ben Gilad0bf5b912014-01-28 17:55:57 -0800156 * Handles the case where an outgoing call has been successfully placed,
157 * see {@link OutgoingCallProcessor}.
Ben Giladb59769e2014-01-16 11:41:10 -0800158 */
Ben Gilad0bf5b912014-01-28 17:55:57 -0800159 void handleSuccessfulOutgoingCall(Call call) {
160 // TODO(gilad): More here.
161
162 // Process additional (new) calls, if any.
163 processNewOutgoingCalls();
164 }
165
166 /**
167 * Handles the case where an outgoing call could not be proceed by any of the
168 * selector/call-service implementations, see {@link OutgoingCallProcessor}.
169 */
170 void handleFailedOutgoingCall(Call call) {
171 // TODO(gilad): More here.
172
173 // Process additional (new) calls, if any.
174 processNewOutgoingCalls();
175 }
176
177 /**
Ben Gilad2313e622014-02-06 12:02:25 -0800178 * @return True if ticking should continue (or be resumed) and false otherwise.
179 */
180 private boolean isTicking() {
181 // TODO(gilad): return true every time at least one outgoing call is pending (i.e. waiting
182 // to be connected by a call service) and also when at least one active call is switch-able
183 // between call services, see {@link ICallServiceSelector#isSwitchable}.
184 return false;
185 }
186
187 /**
188 * Schedules the next tick invocation.
189 */
190 private void scheduleNextTick() {
191 mHandler.postDelayed(mTicker, TICK_FREQUENCY);
192 }
193
194 /**
195 * Performs the set of tasks that needs to be executed on polling basis.
196 * TODO(gilad): Check for stale pending calls that may need to be terminated etc, see
197 * mNewOutgoingCalls and mPendingOutgoingCalls.
198 * TODO(gilad): Also intended to trigger the call switching/hand-off logic when applicable.
199 */
200 private void tick() {
201 // TODO(gilad): More here.
202 }
203
204 /**
Ben Gilad0bf5b912014-01-28 17:55:57 -0800205 * Attempts to process the next new outgoing calls that have not yet been expired.
206 */
207 private void processNewOutgoingCalls() {
208 if (isNullOrEmpty(mCallServices) || isNullOrEmpty(mSelectors)) {
209 // At least one call service and one selector are required to process outgoing calls.
210 return;
211 }
212
213 if (!mNewOutgoingCalls.isEmpty()) {
214 Call call = mNewOutgoingCalls.iterator().next();
215 mNewOutgoingCalls.remove(call);
216 mPendingOutgoingCalls.add(call);
217
218 // Specifically only attempt to place one call at a time such that call services
219 // can be freed from needing to deal with concurrent requests.
220 processNewOutgoingCall(call);
Ben Gilad0407fb22014-01-09 16:18:41 -0800221 }
Ben Gilad9f2bed32013-12-12 17:43:26 -0800222 }
223
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800224 /**
Ben Giladb59769e2014-01-16 11:41:10 -0800225 * Attempts to place the specified call.
226 *
Ben Gilad0bf5b912014-01-28 17:55:57 -0800227 * @param call The call to place.
Ben Giladb59769e2014-01-16 11:41:10 -0800228 */
Ben Gilad0bf5b912014-01-28 17:55:57 -0800229 private void processNewOutgoingCall(Call call) {
Ben Giladb59769e2014-01-16 11:41:10 -0800230
Ben Gilad0bf5b912014-01-28 17:55:57 -0800231 Preconditions.checkNotNull(mCallServices);
232 Preconditions.checkNotNull(mSelectors);
Ben Giladb59769e2014-01-16 11:41:10 -0800233
Ben Gilad0bf5b912014-01-28 17:55:57 -0800234 // Convert to (duplicate-free) list to aid index-based iteration, see the comment under
235 // setSelectors regarding using LinkedHashSet instead.
236 List<ICallServiceSelector> selectors = Lists.newArrayList();
237 selectors.addAll(mSelectors);
238
239 // Create the processor for this (outgoing) call and store it in a map such that call
240 // attempts can be aborted etc.
241 // TODO(gilad): Consider passing mSelector as an immutable set.
242 OutgoingCallProcessor processor =
243 new OutgoingCallProcessor(call, mCallServices, selectors, this);
244
245 outgoingCallProcessors.put(call, processor);
246 processor.process();
Ben Giladb59769e2014-01-16 11:41:10 -0800247 }
248
249 /**
250 * Determines whether or not the specified collection is either null or empty.
251 *
252 * @param collection Either null or the collection object to be evaluated.
253 * @return True if the collection is null or empty.
254 */
255 @SuppressWarnings("rawtypes")
256 private boolean isNullOrEmpty(Collection collection) {
257 return collection == null || collection.isEmpty();
258 }
259
260 /**
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800261 * Places an outgoing call to the handle passed in. Given a list of {@link ICallServices},
262 * select one and place a call to the handle.
263 * TODO(santoscordon): How does the CallService selection process work?
Ben Giladd17443c2014-01-06 11:04:15 -0800264 * TODO(gilad): Wire this logic from CallServiceFinder.updateSwitchboard.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800265 *
266 * @param handle The handle to dial.
267 * @param contactInfo Information about the entity being called.
268 * @param callServices The list of available {@link ICallService}s.
269 */
Ben Giladd17443c2014-01-06 11:04:15 -0800270// private void placeOutgoingCallInternal(
271// String handle,
272// ContactInfo contactInfo,
273// List<ICallService> callServices) throws CallServiceUnavailableException {
274//
275// Log.i(TAG, "Placing and outgoing call.");
276//
277// if (callServices.isEmpty()) {
278// // No call services, bail out.
279// // TODO(contacts-team): Add logging?
280// // TODO(santoscordon): Does this actually go anywhere considering this method is now
281// // asynchronous?
282// throw new CallServiceUnavailableException("No CallService found.");
283// }
284//
285// List<ICallService> compatibleCallServices = Lists.newArrayList();
286// for (ICallService service : callServices) {
287// // TODO(santoscordon): This code needs to be updated to an asynchronous response
288// // callback from isCompatibleWith().
289// /* if (service.isCompatibleWith(handle)) {
290// // NOTE(android-contacts): If we end up taking the liberty to issue
291// // calls not using the explicit user input (in case one is provided)
292// // and instead pull an alternative method of communication from the
293// // specified user-info object, it may be desirable to give precedence
294// // to services that can in fact respect the user's intent.
295// compatibleCallServices.add(service);
296// }
297// */
298// }
299//
300// if (compatibleCallServices.isEmpty()) {
301// // None of the available call services is suitable for making this call.
302// // TODO(contacts-team): Same here re logging.
303// throw new CallServiceUnavailableException("No compatible CallService found.");
304// }
305//
306// // NOTE(android-team): At this point we can also prompt the user for
307// // preference, i.e. instead of the logic just below.
308// if (compatibleCallServices.size() > 1) {
309// compatibleCallServices = sort(compatibleCallServices);
310// }
311// for (ICallService service : compatibleCallServices) {
312// try {
313// service.call(handle);
314// return;
315// } catch (RemoteException e) {
316// // TODO(santoscordon): Need some proxy for ICallService so that we don't have to
317// // avoid RemoteExceptionHandling everywhere. Take a look at how InputMethodService
318// // handles this.
319// }
320// // catch (OutgoingCallException ignored) {
321// // TODO(santoscordon): Figure out how OutgoingCallException falls into this. Should
322// // RemoteExceptions also be converted to OutgoingCallExceptions thrown by call()?
323// }
324// }
Ben Gilad9f2bed32013-12-12 17:43:26 -0800325}