blob: a9889c59d5fa9cbfd4a2384164d2a76d43fef3eb [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 Gilad0407fb22014-01-09 16:18:41 -080021import com.google.common.collect.Sets;
Santos Cordon8e8b8d22013-12-19 14:14:05 -080022
Evan Charltona05805b2014-03-05 08:21:46 -080023import android.os.Bundle;
Ben Gilad2313e622014-02-06 12:02:25 -080024import android.os.Handler;
25import android.os.Looper;
Ben Giladc5b22692014-02-18 20:03:22 -080026import android.telecomm.CallServiceDescriptor;
Ben Giladb59769e2014-01-16 11:41:10 -080027import android.telecomm.ICallServiceSelector;
Evan Charltona05805b2014-03-05 08:21:46 -080028import android.telecomm.TelecommConstants;
Santos Cordon493e8f22014-02-19 03:15:12 -080029import android.util.Log;
Santos Cordon8e8b8d22013-12-19 14:14:05 -080030
Ben Giladb59769e2014-01-16 11:41:10 -080031import java.util.Collection;
Ben Gilad8e55d1d2014-02-26 16:25:56 -080032import java.util.Iterator;
Ben Gilad9f2bed32013-12-12 17:43:26 -080033import java.util.List;
Ben Gilad0407fb22014-01-09 16:18:41 -080034import java.util.Set;
Ben Gilad9f2bed32013-12-12 17:43:26 -080035
Santos Cordon8e8b8d22013-12-19 14:14:05 -080036/**
Santos Cordonc195e362014-02-11 17:05:31 -080037 * Switchboard is responsible for (1) gathering the {@link CallServiceWrapper}s and
38 * {@link ICallServiceSelector}s through which to place outgoing calls, (2) starting outgoing calls
39 * (via {@link OutgoingCallsManager} and (3) switching active calls between call services.
Santos Cordon8e8b8d22013-12-19 14:14:05 -080040 */
Ben Giladd17443c2014-01-06 11:04:15 -080041final class Switchboard {
Ben Gilad2495d572014-01-09 17:26:19 -080042
Santos Cordon493e8f22014-02-19 03:15:12 -080043 private final static String TAG = Switchboard.class.getSimpleName();
Ben Gilad2313e622014-02-06 12:02:25 -080044 /**
45 * The frequency of invoking tick in milliseconds.
46 * TODO(gilad): May require tuning.
47 */
Ben Gilad8e55d1d2014-02-26 16:25:56 -080048 private final static int TICK_FREQUENCY_MS = 250;
49
50 /**
51 * The timeout beyond which to drop ongoing attempts to place/receive calls.
52 * TODO(gilad): May require tuning.
53 */
54 private final static int NEW_CALL_TIMEOUT_MS = 5000;
Ben Giladb59769e2014-01-16 11:41:10 -080055
Santos Cordon681663d2014-01-30 04:32:15 -080056 private final CallsManager mCallsManager;
Ben Gilad2313e622014-02-06 12:02:25 -080057
Santos Cordon681663d2014-01-30 04:32:15 -080058 /** Used to place outgoing calls. */
59 private final OutgoingCallsManager mOutgoingCallsManager;
60
Santos Cordon493e8f22014-02-19 03:15:12 -080061 /** Used to retrieve incoming call details. */
Santos Cordon80d9bdc2014-02-13 18:28:46 -080062 private final IncomingCallsManager mIncomingCallsManager;
63
Santos Cordonc195e362014-02-11 17:05:31 -080064 private final CallServiceRepository mCallServiceRepository;
Santos Cordon681663d2014-01-30 04:32:15 -080065
Santos Cordonc195e362014-02-11 17:05:31 -080066 private final CallServiceSelectorRepository mSelectorRepository;
Ben Gilad2313e622014-02-06 12:02:25 -080067
Ben Gilad8e55d1d2014-02-26 16:25:56 -080068 private final BinderDeallocator mBinderDeallocator;
69
Ben Gilad2313e622014-02-06 12:02:25 -080070 /** Used to schedule tasks on the main (UI) thread. */
71 private final Handler mHandler = new Handler(Looper.getMainLooper());
72
73 /**
74 * Executes a single tick task and potentially schedules the next such that polling continues
75 * as long as necessary.
76 * NOTE(gilad): by design no two tick invocations should ever overlap.
77 */
78 private final Runnable mTicker = new Runnable() {
79 @Override
80 public void run() {
81 tick();
82 if (isTicking()) {
83 scheduleNextTick();
84 }
85 }
86 };
87
88 private final Set<Call> mNewOutgoingCalls = Sets.newLinkedHashSet();
89
90 private final Set<Call> mPendingOutgoingCalls = Sets.newLinkedHashSet();
Ben Giladb59769e2014-01-16 11:41:10 -080091
Ben Gilad03292d42014-01-16 15:06:16 -080092 /**
Ben Giladbb167cd2014-02-25 16:24:05 -080093 * The set of currently available call-service implementations, see
Ben Gilad37587232014-02-19 16:03:44 -080094 * {@link CallServiceRepository}. Populated during call-service lookup cycles as part of the
95 * {@link #placeOutgoingCall} flow and cleared upon zero-remaining new/pending outgoing calls.
Ben Gilad03292d42014-01-16 15:06:16 -080096 */
Santos Cordonc195e362014-02-11 17:05:31 -080097 private Set<CallServiceWrapper> mCallServices;
Ben Giladb59769e2014-01-16 11:41:10 -080098
Ben Gilad03292d42014-01-16 15:06:16 -080099 /**
100 * The set of currently available call-service-selector implementations,
Santos Cordonc195e362014-02-11 17:05:31 -0800101 * see {@link CallServiceSelectorRepository}.
Ben Gilad03292d42014-01-16 15:06:16 -0800102 * TODO(gilad): Null out once the active-call count goes to zero.
103 */
104 private Set<ICallServiceSelector> mSelectors;
Ben Gilad0407fb22014-01-09 16:18:41 -0800105
Santos Cordon681663d2014-01-30 04:32:15 -0800106 /**
Santos Cordonc195e362014-02-11 17:05:31 -0800107 * The current lookup-cycle ID used with the repositories. Incremented with each invocation
Ben Gilad37587232014-02-19 16:03:44 -0800108 * of {@link #placeOutgoingCall} and passed to the repositories via initiateLookup().
Santos Cordonc195e362014-02-11 17:05:31 -0800109 */
110 private int mLookupId = 0;
111
112 /**
Santos Cordon681663d2014-01-30 04:32:15 -0800113 * Persists the specified parameters and initializes Switchboard.
114 */
Santos Cordon6242b132014-02-07 16:24:42 -0800115 Switchboard(CallsManager callsManager) {
116 mCallsManager = callsManager;
Santos Cordon681663d2014-01-30 04:32:15 -0800117 mOutgoingCallsManager = new OutgoingCallsManager(this);
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800118 mIncomingCallsManager = new IncomingCallsManager(this);
Santos Cordonc195e362014-02-11 17:05:31 -0800119 mSelectorRepository = new CallServiceSelectorRepository(this);
Santos Cordon493e8f22014-02-19 03:15:12 -0800120 mCallServiceRepository =
121 new CallServiceRepository(this, mOutgoingCallsManager, mIncomingCallsManager);
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800122
123 mBinderDeallocator = new BinderDeallocator();
Santos Cordon681663d2014-01-30 04:32:15 -0800124 }
Ben Giladd17443c2014-01-06 11:04:15 -0800125
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800126 /**
Santos Cordonc195e362014-02-11 17:05:31 -0800127 * Starts the process of placing an outgoing call by searching for available call services
128 * through which the call can be placed. After a lookup for those services completes, execution
129 * returns to {@link #setCallServices} where the process of placing the call continues.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800130 *
Ben Gilad13329fd2014-02-11 17:20:29 -0800131 * @param call The yet-to-be-connected outgoing-call object.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800132 */
Santos Cordonc195e362014-02-11 17:05:31 -0800133 void placeOutgoingCall(Call call) {
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800134 mBinderDeallocator.acquireUsePermit();
Ben Giladb59769e2014-01-16 11:41:10 -0800135
Ben Giladbb167cd2014-02-25 16:24:05 -0800136 // Reset prior to initiating the next lookup. One case to consider is (1) placeOutgoingCall
137 // is invoked with call A, (2) the call-service lookup completes, but the one for selectors
138 // does not, (3) placeOutgoingCall is invoked again with call B, (4) mCallServices below is
139 // reset, (5) the selector lookup completes but the call-services are missing. This should
140 // be okay since the call-service lookup completed. Specifically the already-available call
141 // services are cached and will be provided in response to the second lookup cycle.
142 mCallServices = null;
143 mSelectors = null;
144
Sailesh Nepalb6141ae2014-02-18 08:45:26 -0800145 mNewOutgoingCalls.add(call);
Ben Giladb59769e2014-01-16 11:41:10 -0800146
Ben Giladebd9b662014-02-19 16:03:44 -0800147 // Initiate a lookup every time to account for newly-installed apps and/or updated settings.
Sailesh Nepalb6141ae2014-02-18 08:45:26 -0800148 mLookupId++;
Santos Cordonc195e362014-02-11 17:05:31 -0800149 mCallServiceRepository.initiateLookup(mLookupId);
150 mSelectorRepository.initiateLookup(mLookupId);
Ben Gilad0407fb22014-01-09 16:18:41 -0800151 }
Ben Gilad9f2bed32013-12-12 17:43:26 -0800152
Ben Gilad0407fb22014-01-09 16:18:41 -0800153 /**
Santos Cordon493e8f22014-02-19 03:15:12 -0800154 * Retrieves details about the incoming call through the incoming call manager. The incoming
155 * call manager will invoke either {@link #handleSuccessfulIncomingCall} or
156 * {@link #handleFailedIncomingCall} depending on the result of the retrieval.
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800157 *
158 * @param call The call object.
Ben Giladc5b22692014-02-18 20:03:22 -0800159 * @param descriptor The relevant call-service descriptor.
Evan Charltona05805b2014-03-05 08:21:46 -0800160 * @param extras The optional extras passed via
161 * {@link TelecommConstants#EXTRA_INCOMING_CALL_EXTRAS}
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800162 */
Evan Charltona05805b2014-03-05 08:21:46 -0800163 void retrieveIncomingCall(Call call, CallServiceDescriptor descriptor, Bundle extras) {
Santos Cordon493e8f22014-02-19 03:15:12 -0800164 Log.d(TAG, "retrieveIncomingCall");
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800165 mBinderDeallocator.acquireUsePermit();
166
Ben Giladc5b22692014-02-18 20:03:22 -0800167 CallServiceWrapper callService = mCallServiceRepository.getCallService(descriptor);
Santos Cordon493e8f22014-02-19 03:15:12 -0800168 call.setCallService(callService);
Evan Charltona05805b2014-03-05 08:21:46 -0800169 mIncomingCallsManager.retrieveIncomingCall(call, extras);
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800170 }
171
172 /**
Ben Gilad0bf5b912014-01-28 17:55:57 -0800173 * Persists the specified set of call services and attempts to place any pending outgoing
Santos Cordonc195e362014-02-11 17:05:31 -0800174 * calls. Intended to be invoked by {@link CallServiceRepository} exclusively.
Ben Gilad0407fb22014-01-09 16:18:41 -0800175 *
Ben Gilad03292d42014-01-16 15:06:16 -0800176 * @param callServices The potentially-partial set of call services. Partial since the lookup
177 * process is time-boxed, such that some providers/call-services may be slow to respond and
178 * hence effectively omitted from the specified list.
Ben Gilad0407fb22014-01-09 16:18:41 -0800179 */
Santos Cordonc195e362014-02-11 17:05:31 -0800180 void setCallServices(Set<CallServiceWrapper> callServices) {
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800181 mBinderDeallocator.updateBinders(mCallServices);
Ben Gilad0407fb22014-01-09 16:18:41 -0800182
Ben Giladb59769e2014-01-16 11:41:10 -0800183 mCallServices = callServices;
Ben Gilad0bf5b912014-01-28 17:55:57 -0800184 processNewOutgoingCalls();
Ben Giladb59769e2014-01-16 11:41:10 -0800185 }
186
187 /**
188 * Persists the specified list of selectors and attempts to connect any pending outgoing
Santos Cordonc195e362014-02-11 17:05:31 -0800189 * calls. Intended to be invoked by {@link CallServiceSelectorRepository} exclusively.
Ben Giladb59769e2014-01-16 11:41:10 -0800190 *
Ben Gilad03292d42014-01-16 15:06:16 -0800191 * @param selectors The potentially-partial set of selectors. Partial since the lookup
192 * procedure is time-boxed such that some selectors may be slow to respond and hence
193 * effectively omitted from the specified set.
Ben Giladb59769e2014-01-16 11:41:10 -0800194 */
Ben Gilad03292d42014-01-16 15:06:16 -0800195 void setSelectors(Set<ICallServiceSelector> selectors) {
Santos Cordonc195e362014-02-11 17:05:31 -0800196 // TODO(santoscordon): This should take in CallServiceSelectorWrapper instead of the direct
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800197 // ICallServiceSelector implementation. Copy what we have for CallServiceWrapper. Also need
198 // to invoke updateBinders(selectors) once this to-do is addressed.
Ben Giladb59769e2014-01-16 11:41:10 -0800199 ThreadUtil.checkOnMainThread();
200
Ben Gilad134cf092014-01-16 18:26:12 -0800201 // TODO(gilad): Add logic to include the built-in selectors (e.g. for dealing with
202 // emergency calls) and order the entire set prior to the assignment below. If the
203 // built-in selectors can be implemented in a manner that does not require binding,
204 // that's probably preferred. May want to use a LinkedHashSet for the sorted set.
Ben Giladb59769e2014-01-16 11:41:10 -0800205 mSelectors = selectors;
Ben Gilad0bf5b912014-01-28 17:55:57 -0800206 processNewOutgoingCalls();
Ben Giladb59769e2014-01-16 11:41:10 -0800207 }
208
209 /**
Ben Gilad0bf5b912014-01-28 17:55:57 -0800210 * Handles the case where an outgoing call has been successfully placed,
211 * see {@link OutgoingCallProcessor}.
Ben Giladb59769e2014-01-16 11:41:10 -0800212 */
Ben Gilad0bf5b912014-01-28 17:55:57 -0800213 void handleSuccessfulOutgoingCall(Call call) {
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800214 Log.d(TAG, "handleSuccessfulOutgoingCall");
Ben Gilad0bf5b912014-01-28 17:55:57 -0800215
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800216 mCallsManager.handleSuccessfulOutgoingCall(call);
217 finalizeOutgoingCall(call);
Ben Gilad0bf5b912014-01-28 17:55:57 -0800218 }
219
220 /**
221 * Handles the case where an outgoing call could not be proceed by any of the
222 * selector/call-service implementations, see {@link OutgoingCallProcessor}.
223 */
224 void handleFailedOutgoingCall(Call call) {
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800225 Log.d(TAG, "handleFailedOutgoingCall");
Ben Gilad0bf5b912014-01-28 17:55:57 -0800226
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800227 // TODO(gilad): Notify mCallsManager.
228
229 finalizeOutgoingCall(call);
Ben Gilad0bf5b912014-01-28 17:55:57 -0800230 }
231
232 /**
Santos Cordon493e8f22014-02-19 03:15:12 -0800233 * Handles the case where we successfully receive details of an incoming call. Hands the
234 * resulting call to {@link CallsManager} as the final step in the incoming sequence. At that
235 * point, {@link CallsManager} should bring up the incoming-call UI.
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800236 */
237 void handleSuccessfulIncomingCall(Call call) {
Santos Cordon493e8f22014-02-19 03:15:12 -0800238 Log.d(TAG, "handleSuccessfulIncomingCall");
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800239
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800240 mCallsManager.handleSuccessfulIncomingCall(call);
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800241 mBinderDeallocator.releaseUsePermit();
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800242 }
243
244 /**
Santos Cordon493e8f22014-02-19 03:15:12 -0800245 * Handles the case where we failed to retrieve an incoming call after receiving an incoming-call
246 * intent via {@link CallActivity}.
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800247 *
248 * @param call The call.
249 */
250 void handleFailedIncomingCall(Call call) {
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800251 Log.d(TAG, "handleFailedIncomingCall");
252
Santos Cordon493e8f22014-02-19 03:15:12 -0800253 // Since we set the call service before calling into incoming-calls manager, we clear it for
254 // good measure if an error is reported.
255 call.clearCallService();
256
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800257 mBinderDeallocator.releaseUsePermit();
258
Santos Cordon493e8f22014-02-19 03:15:12 -0800259 // At the moment there is nothing more to do if an incoming call is not retrieved. We may at
260 // a future date bind to the in-call app optimistically during the incoming-call sequence
261 // and this method could tell {@link CallsManager} to unbind from the in-call app if the
262 // incoming call was not retrieved.
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800263 }
264
265 /**
Ben Gilad2313e622014-02-06 12:02:25 -0800266 * @return True if ticking should continue (or be resumed) and false otherwise.
267 */
268 private boolean isTicking() {
269 // TODO(gilad): return true every time at least one outgoing call is pending (i.e. waiting
270 // to be connected by a call service) and also when at least one active call is switch-able
271 // between call services, see {@link ICallServiceSelector#isSwitchable}.
272 return false;
273 }
274
275 /**
276 * Schedules the next tick invocation.
277 */
278 private void scheduleNextTick() {
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800279 mHandler.postDelayed(mTicker, TICK_FREQUENCY_MS);
Ben Gilad2313e622014-02-06 12:02:25 -0800280 }
281
282 /**
Ben Gilad0bf5b912014-01-28 17:55:57 -0800283 * Attempts to process the next new outgoing calls that have not yet been expired.
284 */
285 private void processNewOutgoingCalls() {
286 if (isNullOrEmpty(mCallServices) || isNullOrEmpty(mSelectors)) {
287 // At least one call service and one selector are required to process outgoing calls.
288 return;
289 }
290
291 if (!mNewOutgoingCalls.isEmpty()) {
292 Call call = mNewOutgoingCalls.iterator().next();
293 mNewOutgoingCalls.remove(call);
294 mPendingOutgoingCalls.add(call);
295
296 // Specifically only attempt to place one call at a time such that call services
297 // can be freed from needing to deal with concurrent requests.
298 processNewOutgoingCall(call);
Ben Gilad0407fb22014-01-09 16:18:41 -0800299 }
Ben Gilad9f2bed32013-12-12 17:43:26 -0800300 }
301
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800302 /**
Ben Giladb59769e2014-01-16 11:41:10 -0800303 * Attempts to place the specified call.
304 *
Ben Gilad0bf5b912014-01-28 17:55:57 -0800305 * @param call The call to place.
Ben Giladb59769e2014-01-16 11:41:10 -0800306 */
Ben Gilad0bf5b912014-01-28 17:55:57 -0800307 private void processNewOutgoingCall(Call call) {
Ben Gilad0bf5b912014-01-28 17:55:57 -0800308 Preconditions.checkNotNull(mCallServices);
309 Preconditions.checkNotNull(mSelectors);
Ben Giladb59769e2014-01-16 11:41:10 -0800310
Ben Gilad0bf5b912014-01-28 17:55:57 -0800311 // Convert to (duplicate-free) list to aid index-based iteration, see the comment under
312 // setSelectors regarding using LinkedHashSet instead.
313 List<ICallServiceSelector> selectors = Lists.newArrayList();
314 selectors.addAll(mSelectors);
315
Santos Cordon681663d2014-01-30 04:32:15 -0800316 mOutgoingCallsManager.placeCall(call, mCallServices, selectors);
Ben Giladb59769e2014-01-16 11:41:10 -0800317 }
318
319 /**
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800320 * Finalizes the outgoing-call sequence, regardless if it succeeded or failed.
321 */
322 private void finalizeOutgoingCall(Call call) {
323 mPendingOutgoingCalls.remove(call);
324
325 mBinderDeallocator.releaseUsePermit();
326 processNewOutgoingCalls(); // Process additional (new) calls, if any.
327 }
328
329 /**
330 * Performs the set of tasks that needs to be executed on polling basis.
331 */
332 private void tick() {
333 // TODO(gilad): Add the necessary logic to support switching.
334
335 expireStaleOutgoingCalls(mNewOutgoingCalls);
336 expireStaleOutgoingCalls(mPendingOutgoingCalls);
337 }
338
339 /**
340 * Identifies stale calls and takes the necessary steps to mark these as expired.
341 *
342 * @param calls The set of calls to iterate through.
343 */
344 private void expireStaleOutgoingCalls(Set<Call> calls) {
345 if (calls.isEmpty()) {
346 return;
347 }
348
349 Iterator<Call> iterator = calls.iterator();
350 while (iterator.hasNext()) {
351 Call call = iterator.next();
352 if (call.getAgeInMilliseconds() >= NEW_CALL_TIMEOUT_MS) {
353 mOutgoingCallsManager.abort(call);
354 calls.remove(call);
355
356 // TODO(gilad): We may also have expired calls that are not yet associated with an
357 // OutgoingCallProcessor (e.g. when newer calls are "blocked" on older-yet-expired
358 // ones), in which case call.abort may need to be invoked directly. Alternatively
359 // we can also create an OutgoingCallsManager instance for every new call at intent-
360 // processing time.
361
362 // TODO(gilad): Notify the user in the relevant cases (at least outgoing).
363
364 mBinderDeallocator.releaseUsePermit();
365 }
366 }
367 }
368
369 /**
Ben Giladb59769e2014-01-16 11:41:10 -0800370 * Determines whether or not the specified collection is either null or empty.
371 *
372 * @param collection Either null or the collection object to be evaluated.
373 * @return True if the collection is null or empty.
374 */
375 @SuppressWarnings("rawtypes")
376 private boolean isNullOrEmpty(Collection collection) {
377 return collection == null || collection.isEmpty();
378 }
Ben Gilad9f2bed32013-12-12 17:43:26 -0800379}