blob: bb40ef63a04981031c6f3e06e0d139e62923189e [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 Giladb59769e2014-01-16 11:41:10 -080044
Santos Cordon681663d2014-01-30 04:32:15 -080045 private final CallsManager mCallsManager;
Ben Gilad2313e622014-02-06 12:02:25 -080046
Santos Cordon681663d2014-01-30 04:32:15 -080047 /** Used to place outgoing calls. */
48 private final OutgoingCallsManager mOutgoingCallsManager;
49
Santos Cordon493e8f22014-02-19 03:15:12 -080050 /** Used to retrieve incoming call details. */
Santos Cordon80d9bdc2014-02-13 18:28:46 -080051 private final IncomingCallsManager mIncomingCallsManager;
52
Santos Cordonc195e362014-02-11 17:05:31 -080053 private final CallServiceRepository mCallServiceRepository;
Santos Cordon681663d2014-01-30 04:32:15 -080054
Santos Cordonc195e362014-02-11 17:05:31 -080055 private final CallServiceSelectorRepository mSelectorRepository;
Ben Gilad2313e622014-02-06 12:02:25 -080056
Ben Gilad8e55d1d2014-02-26 16:25:56 -080057 private final BinderDeallocator mBinderDeallocator;
58
Ben Gilad2313e622014-02-06 12:02:25 -080059 /** Used to schedule tasks on the main (UI) thread. */
60 private final Handler mHandler = new Handler(Looper.getMainLooper());
61
62 /**
63 * Executes a single tick task and potentially schedules the next such that polling continues
64 * as long as necessary.
65 * NOTE(gilad): by design no two tick invocations should ever overlap.
66 */
67 private final Runnable mTicker = new Runnable() {
68 @Override
69 public void run() {
70 tick();
71 if (isTicking()) {
72 scheduleNextTick();
73 }
74 }
75 };
76
77 private final Set<Call> mNewOutgoingCalls = Sets.newLinkedHashSet();
78
79 private final Set<Call> mPendingOutgoingCalls = Sets.newLinkedHashSet();
Ben Giladb59769e2014-01-16 11:41:10 -080080
Ben Gilad03292d42014-01-16 15:06:16 -080081 /**
Ben Giladbb167cd2014-02-25 16:24:05 -080082 * The set of currently available call-service implementations, see
Ben Gilad37587232014-02-19 16:03:44 -080083 * {@link CallServiceRepository}. Populated during call-service lookup cycles as part of the
84 * {@link #placeOutgoingCall} flow and cleared upon zero-remaining new/pending outgoing calls.
Ben Gilad03292d42014-01-16 15:06:16 -080085 */
Santos Cordonc195e362014-02-11 17:05:31 -080086 private Set<CallServiceWrapper> mCallServices;
Ben Giladb59769e2014-01-16 11:41:10 -080087
Ben Gilad03292d42014-01-16 15:06:16 -080088 /**
89 * The set of currently available call-service-selector implementations,
Santos Cordonc195e362014-02-11 17:05:31 -080090 * see {@link CallServiceSelectorRepository}.
Ben Gilad03292d42014-01-16 15:06:16 -080091 * TODO(gilad): Null out once the active-call count goes to zero.
92 */
93 private Set<ICallServiceSelector> mSelectors;
Ben Gilad0407fb22014-01-09 16:18:41 -080094
Santos Cordon681663d2014-01-30 04:32:15 -080095 /**
Santos Cordonc195e362014-02-11 17:05:31 -080096 * The current lookup-cycle ID used with the repositories. Incremented with each invocation
Ben Gilad37587232014-02-19 16:03:44 -080097 * of {@link #placeOutgoingCall} and passed to the repositories via initiateLookup().
Santos Cordonc195e362014-02-11 17:05:31 -080098 */
99 private int mLookupId = 0;
100
101 /**
Santos Cordon681663d2014-01-30 04:32:15 -0800102 * Persists the specified parameters and initializes Switchboard.
103 */
Santos Cordon6242b132014-02-07 16:24:42 -0800104 Switchboard(CallsManager callsManager) {
105 mCallsManager = callsManager;
Santos Cordon681663d2014-01-30 04:32:15 -0800106 mOutgoingCallsManager = new OutgoingCallsManager(this);
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800107 mIncomingCallsManager = new IncomingCallsManager(this);
Santos Cordonc195e362014-02-11 17:05:31 -0800108 mSelectorRepository = new CallServiceSelectorRepository(this);
Santos Cordon493e8f22014-02-19 03:15:12 -0800109 mCallServiceRepository =
110 new CallServiceRepository(this, mOutgoingCallsManager, mIncomingCallsManager);
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800111
112 mBinderDeallocator = new BinderDeallocator();
Santos Cordon681663d2014-01-30 04:32:15 -0800113 }
Ben Giladd17443c2014-01-06 11:04:15 -0800114
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800115 /**
Santos Cordonc195e362014-02-11 17:05:31 -0800116 * Starts the process of placing an outgoing call by searching for available call services
117 * through which the call can be placed. After a lookup for those services completes, execution
118 * returns to {@link #setCallServices} where the process of placing the call continues.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800119 *
Ben Gilad13329fd2014-02-11 17:20:29 -0800120 * @param call The yet-to-be-connected outgoing-call object.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800121 */
Santos Cordonc195e362014-02-11 17:05:31 -0800122 void placeOutgoingCall(Call call) {
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800123 mBinderDeallocator.acquireUsePermit();
Ben Giladb59769e2014-01-16 11:41:10 -0800124
Ben Giladbb167cd2014-02-25 16:24:05 -0800125 // Reset prior to initiating the next lookup. One case to consider is (1) placeOutgoingCall
126 // is invoked with call A, (2) the call-service lookup completes, but the one for selectors
127 // does not, (3) placeOutgoingCall is invoked again with call B, (4) mCallServices below is
128 // reset, (5) the selector lookup completes but the call-services are missing. This should
129 // be okay since the call-service lookup completed. Specifically the already-available call
130 // services are cached and will be provided in response to the second lookup cycle.
131 mCallServices = null;
132 mSelectors = null;
133
Sailesh Nepalb6141ae2014-02-18 08:45:26 -0800134 mNewOutgoingCalls.add(call);
Ben Giladb59769e2014-01-16 11:41:10 -0800135
Ben Giladebd9b662014-02-19 16:03:44 -0800136 // Initiate a lookup every time to account for newly-installed apps and/or updated settings.
Sailesh Nepalb6141ae2014-02-18 08:45:26 -0800137 mLookupId++;
Santos Cordonc195e362014-02-11 17:05:31 -0800138 mCallServiceRepository.initiateLookup(mLookupId);
139 mSelectorRepository.initiateLookup(mLookupId);
Ben Gilad0407fb22014-01-09 16:18:41 -0800140 }
Ben Gilad9f2bed32013-12-12 17:43:26 -0800141
Ben Gilad0407fb22014-01-09 16:18:41 -0800142 /**
Santos Cordon493e8f22014-02-19 03:15:12 -0800143 * Retrieves details about the incoming call through the incoming call manager. The incoming
144 * call manager will invoke either {@link #handleSuccessfulIncomingCall} or
145 * {@link #handleFailedIncomingCall} depending on the result of the retrieval.
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800146 *
147 * @param call The call object.
Ben Giladc5b22692014-02-18 20:03:22 -0800148 * @param descriptor The relevant call-service descriptor.
Evan Charltona05805b2014-03-05 08:21:46 -0800149 * @param extras The optional extras passed via
150 * {@link TelecommConstants#EXTRA_INCOMING_CALL_EXTRAS}
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800151 */
Evan Charltona05805b2014-03-05 08:21:46 -0800152 void retrieveIncomingCall(Call call, CallServiceDescriptor descriptor, Bundle extras) {
Santos Cordon493e8f22014-02-19 03:15:12 -0800153 Log.d(TAG, "retrieveIncomingCall");
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800154 mBinderDeallocator.acquireUsePermit();
155
Ben Giladc5b22692014-02-18 20:03:22 -0800156 CallServiceWrapper callService = mCallServiceRepository.getCallService(descriptor);
Santos Cordon493e8f22014-02-19 03:15:12 -0800157 call.setCallService(callService);
Evan Charltona05805b2014-03-05 08:21:46 -0800158 mIncomingCallsManager.retrieveIncomingCall(call, extras);
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800159 }
160
161 /**
Ben Gilad0bf5b912014-01-28 17:55:57 -0800162 * Persists the specified set of call services and attempts to place any pending outgoing
Santos Cordonc195e362014-02-11 17:05:31 -0800163 * calls. Intended to be invoked by {@link CallServiceRepository} exclusively.
Ben Gilad0407fb22014-01-09 16:18:41 -0800164 *
Ben Gilad03292d42014-01-16 15:06:16 -0800165 * @param callServices The potentially-partial set of call services. Partial since the lookup
166 * process is time-boxed, such that some providers/call-services may be slow to respond and
167 * hence effectively omitted from the specified list.
Ben Gilad0407fb22014-01-09 16:18:41 -0800168 */
Santos Cordonc195e362014-02-11 17:05:31 -0800169 void setCallServices(Set<CallServiceWrapper> callServices) {
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800170 mBinderDeallocator.updateBinders(mCallServices);
Ben Gilad0407fb22014-01-09 16:18:41 -0800171
Ben Giladb59769e2014-01-16 11:41:10 -0800172 mCallServices = callServices;
Ben Gilad0bf5b912014-01-28 17:55:57 -0800173 processNewOutgoingCalls();
Ben Giladb59769e2014-01-16 11:41:10 -0800174 }
175
176 /**
177 * Persists the specified list of selectors and attempts to connect any pending outgoing
Santos Cordonc195e362014-02-11 17:05:31 -0800178 * calls. Intended to be invoked by {@link CallServiceSelectorRepository} exclusively.
Ben Giladb59769e2014-01-16 11:41:10 -0800179 *
Ben Gilad03292d42014-01-16 15:06:16 -0800180 * @param selectors The potentially-partial set of selectors. Partial since the lookup
181 * procedure is time-boxed such that some selectors may be slow to respond and hence
182 * effectively omitted from the specified set.
Ben Giladb59769e2014-01-16 11:41:10 -0800183 */
Ben Gilad03292d42014-01-16 15:06:16 -0800184 void setSelectors(Set<ICallServiceSelector> selectors) {
Santos Cordonc195e362014-02-11 17:05:31 -0800185 // TODO(santoscordon): This should take in CallServiceSelectorWrapper instead of the direct
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800186 // ICallServiceSelector implementation. Copy what we have for CallServiceWrapper. Also need
187 // to invoke updateBinders(selectors) once this to-do is addressed.
Ben Giladb59769e2014-01-16 11:41:10 -0800188 ThreadUtil.checkOnMainThread();
189
Ben Gilad134cf092014-01-16 18:26:12 -0800190 // TODO(gilad): Add logic to include the built-in selectors (e.g. for dealing with
191 // emergency calls) and order the entire set prior to the assignment below. If the
192 // built-in selectors can be implemented in a manner that does not require binding,
193 // that's probably preferred. May want to use a LinkedHashSet for the sorted set.
Ben Giladb59769e2014-01-16 11:41:10 -0800194 mSelectors = selectors;
Ben Gilad0bf5b912014-01-28 17:55:57 -0800195 processNewOutgoingCalls();
Ben Giladb59769e2014-01-16 11:41:10 -0800196 }
197
198 /**
Ben Gilad0bf5b912014-01-28 17:55:57 -0800199 * Handles the case where an outgoing call has been successfully placed,
200 * see {@link OutgoingCallProcessor}.
Ben Giladb59769e2014-01-16 11:41:10 -0800201 */
Ben Gilad0bf5b912014-01-28 17:55:57 -0800202 void handleSuccessfulOutgoingCall(Call call) {
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800203 Log.d(TAG, "handleSuccessfulOutgoingCall");
Ben Gilad0bf5b912014-01-28 17:55:57 -0800204
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800205 mCallsManager.handleSuccessfulOutgoingCall(call);
206 finalizeOutgoingCall(call);
Ben Gilad0bf5b912014-01-28 17:55:57 -0800207 }
208
209 /**
210 * Handles the case where an outgoing call could not be proceed by any of the
211 * selector/call-service implementations, see {@link OutgoingCallProcessor}.
212 */
213 void handleFailedOutgoingCall(Call call) {
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800214 Log.d(TAG, "handleFailedOutgoingCall");
Ben Gilad0bf5b912014-01-28 17:55:57 -0800215
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800216 // TODO(gilad): Notify mCallsManager.
217
218 finalizeOutgoingCall(call);
Ben Gilad0bf5b912014-01-28 17:55:57 -0800219 }
220
221 /**
Santos Cordon493e8f22014-02-19 03:15:12 -0800222 * Handles the case where we successfully receive details of an incoming call. Hands the
223 * resulting call to {@link CallsManager} as the final step in the incoming sequence. At that
224 * point, {@link CallsManager} should bring up the incoming-call UI.
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800225 */
226 void handleSuccessfulIncomingCall(Call call) {
Santos Cordon493e8f22014-02-19 03:15:12 -0800227 Log.d(TAG, "handleSuccessfulIncomingCall");
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800228
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800229 mCallsManager.handleSuccessfulIncomingCall(call);
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800230 mBinderDeallocator.releaseUsePermit();
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800231 }
232
233 /**
Santos Cordon493e8f22014-02-19 03:15:12 -0800234 * Handles the case where we failed to retrieve an incoming call after receiving an incoming-call
235 * intent via {@link CallActivity}.
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800236 *
237 * @param call The call.
238 */
239 void handleFailedIncomingCall(Call call) {
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800240 Log.d(TAG, "handleFailedIncomingCall");
241
Santos Cordon493e8f22014-02-19 03:15:12 -0800242 // Since we set the call service before calling into incoming-calls manager, we clear it for
243 // good measure if an error is reported.
244 call.clearCallService();
245
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800246 mBinderDeallocator.releaseUsePermit();
247
Santos Cordon493e8f22014-02-19 03:15:12 -0800248 // At the moment there is nothing more to do if an incoming call is not retrieved. We may at
249 // a future date bind to the in-call app optimistically during the incoming-call sequence
250 // and this method could tell {@link CallsManager} to unbind from the in-call app if the
251 // incoming call was not retrieved.
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800252 }
253
254 /**
Ben Gilad2313e622014-02-06 12:02:25 -0800255 * @return True if ticking should continue (or be resumed) and false otherwise.
256 */
257 private boolean isTicking() {
258 // TODO(gilad): return true every time at least one outgoing call is pending (i.e. waiting
259 // to be connected by a call service) and also when at least one active call is switch-able
260 // between call services, see {@link ICallServiceSelector#isSwitchable}.
261 return false;
262 }
263
264 /**
265 * Schedules the next tick invocation.
266 */
267 private void scheduleNextTick() {
Evan Charlton1063e282014-03-05 19:31:21 -0800268 mHandler.postDelayed(mTicker, Timeouts.getTickMs());
Ben Gilad2313e622014-02-06 12:02:25 -0800269 }
270
271 /**
Ben Gilad0bf5b912014-01-28 17:55:57 -0800272 * Attempts to process the next new outgoing calls that have not yet been expired.
273 */
274 private void processNewOutgoingCalls() {
275 if (isNullOrEmpty(mCallServices) || isNullOrEmpty(mSelectors)) {
276 // At least one call service and one selector are required to process outgoing calls.
277 return;
278 }
279
280 if (!mNewOutgoingCalls.isEmpty()) {
281 Call call = mNewOutgoingCalls.iterator().next();
282 mNewOutgoingCalls.remove(call);
283 mPendingOutgoingCalls.add(call);
284
285 // Specifically only attempt to place one call at a time such that call services
286 // can be freed from needing to deal with concurrent requests.
287 processNewOutgoingCall(call);
Ben Gilad0407fb22014-01-09 16:18:41 -0800288 }
Ben Gilad9f2bed32013-12-12 17:43:26 -0800289 }
290
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800291 /**
Ben Giladb59769e2014-01-16 11:41:10 -0800292 * Attempts to place the specified call.
293 *
Ben Gilad0bf5b912014-01-28 17:55:57 -0800294 * @param call The call to place.
Ben Giladb59769e2014-01-16 11:41:10 -0800295 */
Ben Gilad0bf5b912014-01-28 17:55:57 -0800296 private void processNewOutgoingCall(Call call) {
Ben Gilad0bf5b912014-01-28 17:55:57 -0800297 Preconditions.checkNotNull(mCallServices);
298 Preconditions.checkNotNull(mSelectors);
Ben Giladb59769e2014-01-16 11:41:10 -0800299
Ben Gilad0bf5b912014-01-28 17:55:57 -0800300 // Convert to (duplicate-free) list to aid index-based iteration, see the comment under
301 // setSelectors regarding using LinkedHashSet instead.
302 List<ICallServiceSelector> selectors = Lists.newArrayList();
303 selectors.addAll(mSelectors);
304
Santos Cordon681663d2014-01-30 04:32:15 -0800305 mOutgoingCallsManager.placeCall(call, mCallServices, selectors);
Ben Giladb59769e2014-01-16 11:41:10 -0800306 }
307
308 /**
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800309 * Finalizes the outgoing-call sequence, regardless if it succeeded or failed.
310 */
311 private void finalizeOutgoingCall(Call call) {
312 mPendingOutgoingCalls.remove(call);
313
314 mBinderDeallocator.releaseUsePermit();
315 processNewOutgoingCalls(); // Process additional (new) calls, if any.
316 }
317
318 /**
319 * Performs the set of tasks that needs to be executed on polling basis.
320 */
321 private void tick() {
322 // TODO(gilad): Add the necessary logic to support switching.
323
324 expireStaleOutgoingCalls(mNewOutgoingCalls);
325 expireStaleOutgoingCalls(mPendingOutgoingCalls);
326 }
327
328 /**
329 * Identifies stale calls and takes the necessary steps to mark these as expired.
330 *
331 * @param calls The set of calls to iterate through.
332 */
333 private void expireStaleOutgoingCalls(Set<Call> calls) {
334 if (calls.isEmpty()) {
335 return;
336 }
337
Evan Charlton1063e282014-03-05 19:31:21 -0800338 final long newCallTimeoutMs = Timeouts.getNewOutgoingCallMs();
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800339 Iterator<Call> iterator = calls.iterator();
340 while (iterator.hasNext()) {
341 Call call = iterator.next();
Evan Charlton1063e282014-03-05 19:31:21 -0800342 if (call.getAgeInMilliseconds() >= newCallTimeoutMs) {
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800343 mOutgoingCallsManager.abort(call);
344 calls.remove(call);
345
346 // TODO(gilad): We may also have expired calls that are not yet associated with an
347 // OutgoingCallProcessor (e.g. when newer calls are "blocked" on older-yet-expired
348 // ones), in which case call.abort may need to be invoked directly. Alternatively
349 // we can also create an OutgoingCallsManager instance for every new call at intent-
350 // processing time.
351
352 // TODO(gilad): Notify the user in the relevant cases (at least outgoing).
353
354 mBinderDeallocator.releaseUsePermit();
355 }
356 }
357 }
358
359 /**
Ben Giladb59769e2014-01-16 11:41:10 -0800360 * Determines whether or not the specified collection is either null or empty.
361 *
362 * @param collection Either null or the collection object to be evaluated.
363 * @return True if the collection is null or empty.
364 */
365 @SuppressWarnings("rawtypes")
366 private boolean isNullOrEmpty(Collection collection) {
367 return collection == null || collection.isEmpty();
368 }
Ben Gilad9f2bed32013-12-12 17:43:26 -0800369}