blob: a612a884fb2d343bb56b9f24a3247c9a3c2f7aab [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
Evan Charltonac1aa9e2014-01-03 13:47:12 -080019import com.google.common.collect.Lists;
Ben Gilad0407fb22014-01-09 16:18:41 -080020import com.google.common.collect.Sets;
Santos Cordon8e8b8d22013-12-19 14:14:05 -080021
Evan Charltona05805b2014-03-05 08:21:46 -080022import android.os.Bundle;
Ben Gilad2313e622014-02-06 12:02:25 -080023import android.os.Handler;
24import android.os.Looper;
Ben Giladc5b22692014-02-18 20:03:22 -080025import android.telecomm.CallServiceDescriptor;
Ben Giladb59769e2014-01-16 11:41:10 -080026import android.telecomm.ICallServiceSelector;
Evan Charltona05805b2014-03-05 08:21:46 -080027import android.telecomm.TelecommConstants;
Santos Cordon8e8b8d22013-12-19 14:14:05 -080028
Ben Gilad8e55d1d2014-02-26 16:25:56 -080029import java.util.Iterator;
Ben Gilad9f2bed32013-12-12 17:43:26 -080030import java.util.List;
Ben Gilad0407fb22014-01-09 16:18:41 -080031import java.util.Set;
Ben Gilad9f2bed32013-12-12 17:43:26 -080032
Santos Cordon8e8b8d22013-12-19 14:14:05 -080033/**
Santos Cordonc195e362014-02-11 17:05:31 -080034 * Switchboard is responsible for (1) gathering the {@link CallServiceWrapper}s and
35 * {@link ICallServiceSelector}s through which to place outgoing calls, (2) starting outgoing calls
36 * (via {@link OutgoingCallsManager} and (3) switching active calls between call services.
Santos Cordon8e8b8d22013-12-19 14:14:05 -080037 */
Ben Giladd17443c2014-01-06 11:04:15 -080038final class Switchboard {
Ben Gilad2495d572014-01-09 17:26:19 -080039
Santos Cordon681663d2014-01-30 04:32:15 -080040 private final CallsManager mCallsManager;
Ben Gilad2313e622014-02-06 12:02:25 -080041
Santos Cordon681663d2014-01-30 04:32:15 -080042 /** Used to place outgoing calls. */
43 private final OutgoingCallsManager mOutgoingCallsManager;
44
Santos Cordon493e8f22014-02-19 03:15:12 -080045 /** Used to retrieve incoming call details. */
Santos Cordon80d9bdc2014-02-13 18:28:46 -080046 private final IncomingCallsManager mIncomingCallsManager;
47
Santos Cordonc195e362014-02-11 17:05:31 -080048 private final CallServiceRepository mCallServiceRepository;
Santos Cordon681663d2014-01-30 04:32:15 -080049
Santos Cordonc195e362014-02-11 17:05:31 -080050 private final CallServiceSelectorRepository mSelectorRepository;
Ben Gilad2313e622014-02-06 12:02:25 -080051
Ben Gilad8e55d1d2014-02-26 16:25:56 -080052 private final BinderDeallocator mBinderDeallocator;
53
Ben Gilad2313e622014-02-06 12:02:25 -080054 /** Used to schedule tasks on the main (UI) thread. */
55 private final Handler mHandler = new Handler(Looper.getMainLooper());
56
57 /**
58 * Executes a single tick task and potentially schedules the next such that polling continues
59 * as long as necessary.
60 * NOTE(gilad): by design no two tick invocations should ever overlap.
61 */
62 private final Runnable mTicker = new Runnable() {
63 @Override
64 public void run() {
65 tick();
66 if (isTicking()) {
67 scheduleNextTick();
68 }
69 }
70 };
71
72 private final Set<Call> mNewOutgoingCalls = Sets.newLinkedHashSet();
73
74 private final Set<Call> mPendingOutgoingCalls = Sets.newLinkedHashSet();
Ben Giladb59769e2014-01-16 11:41:10 -080075
Ben Gilad03292d42014-01-16 15:06:16 -080076 /**
Ben Giladbb167cd2014-02-25 16:24:05 -080077 * The set of currently available call-service implementations, see
Ben Gilad37587232014-02-19 16:03:44 -080078 * {@link CallServiceRepository}. Populated during call-service lookup cycles as part of the
79 * {@link #placeOutgoingCall} flow and cleared upon zero-remaining new/pending outgoing calls.
Ben Gilad03292d42014-01-16 15:06:16 -080080 */
Evan Charltonf02e9882014-03-06 12:54:52 -080081 private final Set<CallServiceWrapper> mCallServices = Sets.newHashSet();
Ben Giladb59769e2014-01-16 11:41:10 -080082
Ben Gilad03292d42014-01-16 15:06:16 -080083 /**
84 * The set of currently available call-service-selector implementations,
Santos Cordonc195e362014-02-11 17:05:31 -080085 * see {@link CallServiceSelectorRepository}.
Evan Charltonf02e9882014-03-06 12:54:52 -080086 * TODO(gilad): Clear once the active-call count goes to zero.
Ben Gilad03292d42014-01-16 15:06:16 -080087 */
Evan Charltonf02e9882014-03-06 12:54:52 -080088 private final Set<ICallServiceSelector> mSelectors = Sets.newHashSet();
Ben Gilad0407fb22014-01-09 16:18:41 -080089
Santos Cordon681663d2014-01-30 04:32:15 -080090 /**
Santos Cordonc195e362014-02-11 17:05:31 -080091 * The current lookup-cycle ID used with the repositories. Incremented with each invocation
Ben Gilad37587232014-02-19 16:03:44 -080092 * of {@link #placeOutgoingCall} and passed to the repositories via initiateLookup().
Santos Cordonc195e362014-02-11 17:05:31 -080093 */
94 private int mLookupId = 0;
95
96 /**
Santos Cordon681663d2014-01-30 04:32:15 -080097 * Persists the specified parameters and initializes Switchboard.
98 */
Santos Cordon6242b132014-02-07 16:24:42 -080099 Switchboard(CallsManager callsManager) {
100 mCallsManager = callsManager;
Santos Cordon681663d2014-01-30 04:32:15 -0800101 mOutgoingCallsManager = new OutgoingCallsManager(this);
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800102 mIncomingCallsManager = new IncomingCallsManager(this);
Santos Cordonc195e362014-02-11 17:05:31 -0800103 mSelectorRepository = new CallServiceSelectorRepository(this);
Santos Cordon493e8f22014-02-19 03:15:12 -0800104 mCallServiceRepository =
105 new CallServiceRepository(this, mOutgoingCallsManager, mIncomingCallsManager);
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800106
107 mBinderDeallocator = new BinderDeallocator();
Santos Cordon681663d2014-01-30 04:32:15 -0800108 }
Ben Giladd17443c2014-01-06 11:04:15 -0800109
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800110 /**
Santos Cordonc195e362014-02-11 17:05:31 -0800111 * Starts the process of placing an outgoing call by searching for available call services
112 * through which the call can be placed. After a lookup for those services completes, execution
113 * returns to {@link #setCallServices} where the process of placing the call continues.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800114 *
Ben Gilad13329fd2014-02-11 17:20:29 -0800115 * @param call The yet-to-be-connected outgoing-call object.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800116 */
Santos Cordonc195e362014-02-11 17:05:31 -0800117 void placeOutgoingCall(Call call) {
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800118 mBinderDeallocator.acquireUsePermit();
Ben Giladb59769e2014-01-16 11:41:10 -0800119
Ben Giladbb167cd2014-02-25 16:24:05 -0800120 // Reset prior to initiating the next lookup. One case to consider is (1) placeOutgoingCall
121 // is invoked with call A, (2) the call-service lookup completes, but the one for selectors
122 // does not, (3) placeOutgoingCall is invoked again with call B, (4) mCallServices below is
123 // reset, (5) the selector lookup completes but the call-services are missing. This should
124 // be okay since the call-service lookup completed. Specifically the already-available call
125 // services are cached and will be provided in response to the second lookup cycle.
Evan Charltonf02e9882014-03-06 12:54:52 -0800126 mCallServices.clear();
127 mSelectors.clear();
Ben Giladbb167cd2014-02-25 16:24:05 -0800128
Sailesh Nepalb6141ae2014-02-18 08:45:26 -0800129 mNewOutgoingCalls.add(call);
Ben Giladb59769e2014-01-16 11:41:10 -0800130
Ben Giladebd9b662014-02-19 16:03:44 -0800131 // Initiate a lookup every time to account for newly-installed apps and/or updated settings.
Sailesh Nepalb6141ae2014-02-18 08:45:26 -0800132 mLookupId++;
Santos Cordonc195e362014-02-11 17:05:31 -0800133 mCallServiceRepository.initiateLookup(mLookupId);
134 mSelectorRepository.initiateLookup(mLookupId);
Ben Gilad0407fb22014-01-09 16:18:41 -0800135 }
Ben Gilad9f2bed32013-12-12 17:43:26 -0800136
Ben Gilad0407fb22014-01-09 16:18:41 -0800137 /**
Santos Cordon493e8f22014-02-19 03:15:12 -0800138 * Retrieves details about the incoming call through the incoming call manager. The incoming
139 * call manager will invoke either {@link #handleSuccessfulIncomingCall} or
140 * {@link #handleFailedIncomingCall} depending on the result of the retrieval.
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800141 *
142 * @param call The call object.
Ben Giladc5b22692014-02-18 20:03:22 -0800143 * @param descriptor The relevant call-service descriptor.
Evan Charltona05805b2014-03-05 08:21:46 -0800144 * @param extras The optional extras passed via
145 * {@link TelecommConstants#EXTRA_INCOMING_CALL_EXTRAS}
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800146 */
Evan Charltona05805b2014-03-05 08:21:46 -0800147 void retrieveIncomingCall(Call call, CallServiceDescriptor descriptor, Bundle extras) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800148 Log.d(this, "retrieveIncomingCall");
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800149 mBinderDeallocator.acquireUsePermit();
150
Ben Giladc5b22692014-02-18 20:03:22 -0800151 CallServiceWrapper callService = mCallServiceRepository.getCallService(descriptor);
Santos Cordon493e8f22014-02-19 03:15:12 -0800152 call.setCallService(callService);
Evan Charltona05805b2014-03-05 08:21:46 -0800153 mIncomingCallsManager.retrieveIncomingCall(call, extras);
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800154 }
155
156 /**
Ben Gilad0bf5b912014-01-28 17:55:57 -0800157 * Persists the specified set of call services and attempts to place any pending outgoing
Santos Cordonc195e362014-02-11 17:05:31 -0800158 * calls. Intended to be invoked by {@link CallServiceRepository} exclusively.
Ben Gilad0407fb22014-01-09 16:18:41 -0800159 *
Ben Gilad03292d42014-01-16 15:06:16 -0800160 * @param callServices The potentially-partial set of call services. Partial since the lookup
161 * process is time-boxed, such that some providers/call-services may be slow to respond and
162 * hence effectively omitted from the specified list.
Ben Gilad0407fb22014-01-09 16:18:41 -0800163 */
Santos Cordonc195e362014-02-11 17:05:31 -0800164 void setCallServices(Set<CallServiceWrapper> callServices) {
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800165 mBinderDeallocator.updateBinders(mCallServices);
Ben Gilad0407fb22014-01-09 16:18:41 -0800166
Evan Charltonf02e9882014-03-06 12:54:52 -0800167 mCallServices.clear();
168 mCallServices.addAll(callServices);
Ben Gilad0bf5b912014-01-28 17:55:57 -0800169 processNewOutgoingCalls();
Ben Giladb59769e2014-01-16 11:41:10 -0800170 }
171
172 /**
173 * Persists the specified list of selectors and attempts to connect any pending outgoing
Santos Cordonc195e362014-02-11 17:05:31 -0800174 * calls. Intended to be invoked by {@link CallServiceSelectorRepository} exclusively.
Ben Giladb59769e2014-01-16 11:41:10 -0800175 *
Ben Gilad03292d42014-01-16 15:06:16 -0800176 * @param selectors The potentially-partial set of selectors. Partial since the lookup
177 * procedure is time-boxed such that some selectors may be slow to respond and hence
178 * effectively omitted from the specified set.
Ben Giladb59769e2014-01-16 11:41:10 -0800179 */
Ben Gilad03292d42014-01-16 15:06:16 -0800180 void setSelectors(Set<ICallServiceSelector> selectors) {
Santos Cordonc195e362014-02-11 17:05:31 -0800181 // TODO(santoscordon): This should take in CallServiceSelectorWrapper instead of the direct
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800182 // ICallServiceSelector implementation. Copy what we have for CallServiceWrapper. Also need
183 // to invoke updateBinders(selectors) once this to-do is addressed.
Ben Giladb59769e2014-01-16 11:41:10 -0800184 ThreadUtil.checkOnMainThread();
185
Ben Gilad134cf092014-01-16 18:26:12 -0800186 // TODO(gilad): Add logic to include the built-in selectors (e.g. for dealing with
187 // emergency calls) and order the entire set prior to the assignment below. If the
188 // built-in selectors can be implemented in a manner that does not require binding,
189 // that's probably preferred. May want to use a LinkedHashSet for the sorted set.
Evan Charltonf02e9882014-03-06 12:54:52 -0800190 mSelectors.clear();
191 mSelectors.addAll(selectors);
Ben Gilad0bf5b912014-01-28 17:55:57 -0800192 processNewOutgoingCalls();
Ben Giladb59769e2014-01-16 11:41:10 -0800193 }
194
195 /**
Ben Gilad0bf5b912014-01-28 17:55:57 -0800196 * Handles the case where an outgoing call has been successfully placed,
197 * see {@link OutgoingCallProcessor}.
Ben Giladb59769e2014-01-16 11:41:10 -0800198 */
Ben Gilad0bf5b912014-01-28 17:55:57 -0800199 void handleSuccessfulOutgoingCall(Call call) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800200 Log.d(this, "handleSuccessfulOutgoingCall");
Ben Gilad0bf5b912014-01-28 17:55:57 -0800201
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800202 mCallsManager.handleSuccessfulOutgoingCall(call);
203 finalizeOutgoingCall(call);
Ben Gilad0bf5b912014-01-28 17:55:57 -0800204 }
205
206 /**
207 * Handles the case where an outgoing call could not be proceed by any of the
208 * selector/call-service implementations, see {@link OutgoingCallProcessor}.
209 */
210 void handleFailedOutgoingCall(Call call) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800211 Log.d(this, "handleFailedOutgoingCall");
Ben Gilad0bf5b912014-01-28 17:55:57 -0800212
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800213 // TODO(gilad): Notify mCallsManager.
214
215 finalizeOutgoingCall(call);
Ben Gilad0bf5b912014-01-28 17:55:57 -0800216 }
217
218 /**
Santos Cordon493e8f22014-02-19 03:15:12 -0800219 * Handles the case where we successfully receive details of an incoming call. Hands the
220 * resulting call to {@link CallsManager} as the final step in the incoming sequence. At that
221 * point, {@link CallsManager} should bring up the incoming-call UI.
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800222 */
223 void handleSuccessfulIncomingCall(Call call) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800224 Log.d(this, "handleSuccessfulIncomingCall");
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800225
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800226 mCallsManager.handleSuccessfulIncomingCall(call);
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800227 mBinderDeallocator.releaseUsePermit();
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800228 }
229
230 /**
Santos Cordon493e8f22014-02-19 03:15:12 -0800231 * Handles the case where we failed to retrieve an incoming call after receiving an incoming-call
232 * intent via {@link CallActivity}.
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800233 *
234 * @param call The call.
235 */
236 void handleFailedIncomingCall(Call call) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800237 Log.d(this, "handleFailedIncomingCall");
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800238
Santos Cordon493e8f22014-02-19 03:15:12 -0800239 // Since we set the call service before calling into incoming-calls manager, we clear it for
240 // good measure if an error is reported.
241 call.clearCallService();
242
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800243 mBinderDeallocator.releaseUsePermit();
244
Santos Cordon493e8f22014-02-19 03:15:12 -0800245 // At the moment there is nothing more to do if an incoming call is not retrieved. We may at
246 // a future date bind to the in-call app optimistically during the incoming-call sequence
247 // and this method could tell {@link CallsManager} to unbind from the in-call app if the
248 // incoming call was not retrieved.
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800249 }
250
251 /**
Ben Gilad2313e622014-02-06 12:02:25 -0800252 * @return True if ticking should continue (or be resumed) and false otherwise.
253 */
254 private boolean isTicking() {
255 // TODO(gilad): return true every time at least one outgoing call is pending (i.e. waiting
256 // to be connected by a call service) and also when at least one active call is switch-able
257 // between call services, see {@link ICallServiceSelector#isSwitchable}.
258 return false;
259 }
260
261 /**
262 * Schedules the next tick invocation.
263 */
264 private void scheduleNextTick() {
Evan Charlton1063e282014-03-05 19:31:21 -0800265 mHandler.postDelayed(mTicker, Timeouts.getTickMs());
Ben Gilad2313e622014-02-06 12:02:25 -0800266 }
267
268 /**
Ben Gilad0bf5b912014-01-28 17:55:57 -0800269 * Attempts to process the next new outgoing calls that have not yet been expired.
270 */
271 private void processNewOutgoingCalls() {
Evan Charltonf02e9882014-03-06 12:54:52 -0800272 if (mCallServices.isEmpty() || mSelectors.isEmpty()) {
Ben Gilad0bf5b912014-01-28 17:55:57 -0800273 // At least one call service and one selector are required to process outgoing calls.
274 return;
275 }
276
277 if (!mNewOutgoingCalls.isEmpty()) {
278 Call call = mNewOutgoingCalls.iterator().next();
279 mNewOutgoingCalls.remove(call);
280 mPendingOutgoingCalls.add(call);
281
282 // Specifically only attempt to place one call at a time such that call services
283 // can be freed from needing to deal with concurrent requests.
284 processNewOutgoingCall(call);
Ben Gilad0407fb22014-01-09 16:18:41 -0800285 }
Ben Gilad9f2bed32013-12-12 17:43:26 -0800286 }
287
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800288 /**
Ben Giladb59769e2014-01-16 11:41:10 -0800289 * Attempts to place the specified call.
290 *
Ben Gilad0bf5b912014-01-28 17:55:57 -0800291 * @param call The call to place.
Ben Giladb59769e2014-01-16 11:41:10 -0800292 */
Ben Gilad0bf5b912014-01-28 17:55:57 -0800293 private void processNewOutgoingCall(Call call) {
Ben Gilad0bf5b912014-01-28 17:55:57 -0800294 // Convert to (duplicate-free) list to aid index-based iteration, see the comment under
295 // setSelectors regarding using LinkedHashSet instead.
296 List<ICallServiceSelector> selectors = Lists.newArrayList();
297 selectors.addAll(mSelectors);
298
Santos Cordon681663d2014-01-30 04:32:15 -0800299 mOutgoingCallsManager.placeCall(call, mCallServices, selectors);
Ben Giladb59769e2014-01-16 11:41:10 -0800300 }
301
302 /**
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800303 * Finalizes the outgoing-call sequence, regardless if it succeeded or failed.
304 */
305 private void finalizeOutgoingCall(Call call) {
306 mPendingOutgoingCalls.remove(call);
307
308 mBinderDeallocator.releaseUsePermit();
309 processNewOutgoingCalls(); // Process additional (new) calls, if any.
310 }
311
312 /**
313 * Performs the set of tasks that needs to be executed on polling basis.
314 */
315 private void tick() {
316 // TODO(gilad): Add the necessary logic to support switching.
317
318 expireStaleOutgoingCalls(mNewOutgoingCalls);
319 expireStaleOutgoingCalls(mPendingOutgoingCalls);
320 }
321
322 /**
323 * Identifies stale calls and takes the necessary steps to mark these as expired.
324 *
325 * @param calls The set of calls to iterate through.
326 */
327 private void expireStaleOutgoingCalls(Set<Call> calls) {
328 if (calls.isEmpty()) {
329 return;
330 }
331
Evan Charlton1063e282014-03-05 19:31:21 -0800332 final long newCallTimeoutMs = Timeouts.getNewOutgoingCallMs();
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800333 Iterator<Call> iterator = calls.iterator();
334 while (iterator.hasNext()) {
335 Call call = iterator.next();
Evan Charlton1063e282014-03-05 19:31:21 -0800336 if (call.getAgeInMilliseconds() >= newCallTimeoutMs) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800337 Log.d(this, "Call %s timed out.", call.getId());
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800338 mOutgoingCallsManager.abort(call);
339 calls.remove(call);
340
341 // TODO(gilad): We may also have expired calls that are not yet associated with an
342 // OutgoingCallProcessor (e.g. when newer calls are "blocked" on older-yet-expired
343 // ones), in which case call.abort may need to be invoked directly. Alternatively
344 // we can also create an OutgoingCallsManager instance for every new call at intent-
345 // processing time.
346
347 // TODO(gilad): Notify the user in the relevant cases (at least outgoing).
348
349 mBinderDeallocator.releaseUsePermit();
350 }
351 }
352 }
Ben Gilad9f2bed32013-12-12 17:43:26 -0800353}