blob: 040eb5fcdd5b6761f162195811543f3c0e3b061c [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;
Evan Charltona05805b2014-03-05 08:21:46 -080026import android.telecomm.TelecommConstants;
Santos Cordon8e8b8d22013-12-19 14:14:05 -080027
Sailesh Nepala439e1b2014-03-11 18:19:58 -070028import com.android.internal.telecomm.ICallServiceSelector;
29
Ben Gilad8e55d1d2014-02-26 16:25:56 -080030import java.util.Iterator;
Ben Gilad9f2bed32013-12-12 17:43:26 -080031import java.util.List;
Ben Gilad0407fb22014-01-09 16:18:41 -080032import java.util.Set;
Ben Gilad9f2bed32013-12-12 17:43:26 -080033
Santos Cordon8e8b8d22013-12-19 14:14:05 -080034/**
Santos Cordonc195e362014-02-11 17:05:31 -080035 * Switchboard is responsible for (1) gathering the {@link CallServiceWrapper}s and
36 * {@link ICallServiceSelector}s through which to place outgoing calls, (2) starting outgoing calls
37 * (via {@link OutgoingCallsManager} and (3) switching active calls between call services.
Santos Cordon8e8b8d22013-12-19 14:14:05 -080038 */
Ben Giladd17443c2014-01-06 11:04:15 -080039final class Switchboard {
Ben Gilad2495d572014-01-09 17:26:19 -080040
Santos Cordon681663d2014-01-30 04:32:15 -080041 private final CallsManager mCallsManager;
Ben Gilad2313e622014-02-06 12:02:25 -080042
Santos Cordon681663d2014-01-30 04:32:15 -080043 /** Used to place outgoing calls. */
44 private final OutgoingCallsManager mOutgoingCallsManager;
45
Santos Cordon493e8f22014-02-19 03:15:12 -080046 /** Used to retrieve incoming call details. */
Santos Cordon80d9bdc2014-02-13 18:28:46 -080047 private final IncomingCallsManager mIncomingCallsManager;
48
Santos Cordonc195e362014-02-11 17:05:31 -080049 private final CallServiceRepository mCallServiceRepository;
Santos Cordon681663d2014-01-30 04:32:15 -080050
Santos Cordonc195e362014-02-11 17:05:31 -080051 private final CallServiceSelectorRepository mSelectorRepository;
Ben Gilad2313e622014-02-06 12:02:25 -080052
Ben Gilad8e55d1d2014-02-26 16:25:56 -080053 private final BinderDeallocator mBinderDeallocator;
54
Ben Gilad2313e622014-02-06 12:02:25 -080055 /** Used to schedule tasks on the main (UI) thread. */
56 private final Handler mHandler = new Handler(Looper.getMainLooper());
57
58 /**
59 * Executes a single tick task and potentially schedules the next such that polling continues
60 * as long as necessary.
61 * NOTE(gilad): by design no two tick invocations should ever overlap.
62 */
63 private final Runnable mTicker = new Runnable() {
64 @Override
65 public void run() {
66 tick();
67 if (isTicking()) {
68 scheduleNextTick();
69 }
70 }
71 };
72
73 private final Set<Call> mNewOutgoingCalls = Sets.newLinkedHashSet();
74
75 private final Set<Call> mPendingOutgoingCalls = Sets.newLinkedHashSet();
Ben Giladb59769e2014-01-16 11:41:10 -080076
Ben Gilad03292d42014-01-16 15:06:16 -080077 /**
Ben Giladbb167cd2014-02-25 16:24:05 -080078 * The set of currently available call-service implementations, see
Ben Gilad37587232014-02-19 16:03:44 -080079 * {@link CallServiceRepository}. Populated during call-service lookup cycles as part of the
80 * {@link #placeOutgoingCall} flow and cleared upon zero-remaining new/pending outgoing calls.
Ben Gilad03292d42014-01-16 15:06:16 -080081 */
Evan Charltonf02e9882014-03-06 12:54:52 -080082 private final Set<CallServiceWrapper> mCallServices = Sets.newHashSet();
Ben Giladb59769e2014-01-16 11:41:10 -080083
Ben Gilad03292d42014-01-16 15:06:16 -080084 /**
85 * The set of currently available call-service-selector implementations,
Santos Cordonc195e362014-02-11 17:05:31 -080086 * see {@link CallServiceSelectorRepository}.
Evan Charltonf02e9882014-03-06 12:54:52 -080087 * TODO(gilad): Clear once the active-call count goes to zero.
Ben Gilad03292d42014-01-16 15:06:16 -080088 */
Evan Charltonf02e9882014-03-06 12:54:52 -080089 private final Set<ICallServiceSelector> mSelectors = Sets.newHashSet();
Ben Gilad0407fb22014-01-09 16:18:41 -080090
Santos Cordon681663d2014-01-30 04:32:15 -080091 /**
Santos Cordonc195e362014-02-11 17:05:31 -080092 * The current lookup-cycle ID used with the repositories. Incremented with each invocation
Ben Gilad37587232014-02-19 16:03:44 -080093 * of {@link #placeOutgoingCall} and passed to the repositories via initiateLookup().
Santos Cordonc195e362014-02-11 17:05:31 -080094 */
95 private int mLookupId = 0;
96
97 /**
Santos Cordon681663d2014-01-30 04:32:15 -080098 * Persists the specified parameters and initializes Switchboard.
99 */
Santos Cordon6242b132014-02-07 16:24:42 -0800100 Switchboard(CallsManager callsManager) {
101 mCallsManager = callsManager;
Santos Cordon681663d2014-01-30 04:32:15 -0800102 mOutgoingCallsManager = new OutgoingCallsManager(this);
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800103 mIncomingCallsManager = new IncomingCallsManager(this);
Santos Cordonc195e362014-02-11 17:05:31 -0800104 mSelectorRepository = new CallServiceSelectorRepository(this);
Santos Cordon493e8f22014-02-19 03:15:12 -0800105 mCallServiceRepository =
106 new CallServiceRepository(this, mOutgoingCallsManager, mIncomingCallsManager);
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800107
108 mBinderDeallocator = new BinderDeallocator();
Santos Cordon681663d2014-01-30 04:32:15 -0800109 }
Ben Giladd17443c2014-01-06 11:04:15 -0800110
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800111 /**
Santos Cordonc195e362014-02-11 17:05:31 -0800112 * Starts the process of placing an outgoing call by searching for available call services
113 * through which the call can be placed. After a lookup for those services completes, execution
114 * returns to {@link #setCallServices} where the process of placing the call continues.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800115 *
Ben Gilad13329fd2014-02-11 17:20:29 -0800116 * @param call The yet-to-be-connected outgoing-call object.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800117 */
Santos Cordonc195e362014-02-11 17:05:31 -0800118 void placeOutgoingCall(Call call) {
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800119 mBinderDeallocator.acquireUsePermit();
Ben Giladb59769e2014-01-16 11:41:10 -0800120
Ben Giladbb167cd2014-02-25 16:24:05 -0800121 // Reset prior to initiating the next lookup. One case to consider is (1) placeOutgoingCall
122 // is invoked with call A, (2) the call-service lookup completes, but the one for selectors
123 // does not, (3) placeOutgoingCall is invoked again with call B, (4) mCallServices below is
124 // reset, (5) the selector lookup completes but the call-services are missing. This should
125 // be okay since the call-service lookup completed. Specifically the already-available call
126 // services are cached and will be provided in response to the second lookup cycle.
Evan Charltonf02e9882014-03-06 12:54:52 -0800127 mCallServices.clear();
128 mSelectors.clear();
Ben Giladbb167cd2014-02-25 16:24:05 -0800129
Sailesh Nepalb6141ae2014-02-18 08:45:26 -0800130 mNewOutgoingCalls.add(call);
Ben Giladb59769e2014-01-16 11:41:10 -0800131
Ben Giladebd9b662014-02-19 16:03:44 -0800132 // Initiate a lookup every time to account for newly-installed apps and/or updated settings.
Sailesh Nepalb6141ae2014-02-18 08:45:26 -0800133 mLookupId++;
Santos Cordonc195e362014-02-11 17:05:31 -0800134 mCallServiceRepository.initiateLookup(mLookupId);
135 mSelectorRepository.initiateLookup(mLookupId);
Ben Gilad0407fb22014-01-09 16:18:41 -0800136 }
Ben Gilad9f2bed32013-12-12 17:43:26 -0800137
Ben Gilad0407fb22014-01-09 16:18:41 -0800138 /**
Santos Cordon493e8f22014-02-19 03:15:12 -0800139 * Retrieves details about the incoming call through the incoming call manager. The incoming
140 * call manager will invoke either {@link #handleSuccessfulIncomingCall} or
141 * {@link #handleFailedIncomingCall} depending on the result of the retrieval.
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800142 *
143 * @param call The call object.
Ben Giladc5b22692014-02-18 20:03:22 -0800144 * @param descriptor The relevant call-service descriptor.
Evan Charltona05805b2014-03-05 08:21:46 -0800145 * @param extras The optional extras passed via
146 * {@link TelecommConstants#EXTRA_INCOMING_CALL_EXTRAS}
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800147 */
Evan Charltona05805b2014-03-05 08:21:46 -0800148 void retrieveIncomingCall(Call call, CallServiceDescriptor descriptor, Bundle extras) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800149 Log.d(this, "retrieveIncomingCall");
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800150 mBinderDeallocator.acquireUsePermit();
151
Ben Giladc5b22692014-02-18 20:03:22 -0800152 CallServiceWrapper callService = mCallServiceRepository.getCallService(descriptor);
Santos Cordon493e8f22014-02-19 03:15:12 -0800153 call.setCallService(callService);
Evan Charltona05805b2014-03-05 08:21:46 -0800154 mIncomingCallsManager.retrieveIncomingCall(call, extras);
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800155 }
156
157 /**
Ben Gilad0bf5b912014-01-28 17:55:57 -0800158 * Persists the specified set of call services and attempts to place any pending outgoing
Santos Cordonc195e362014-02-11 17:05:31 -0800159 * calls. Intended to be invoked by {@link CallServiceRepository} exclusively.
Ben Gilad0407fb22014-01-09 16:18:41 -0800160 *
Ben Gilad03292d42014-01-16 15:06:16 -0800161 * @param callServices The potentially-partial set of call services. Partial since the lookup
162 * process is time-boxed, such that some providers/call-services may be slow to respond and
163 * hence effectively omitted from the specified list.
Ben Gilad0407fb22014-01-09 16:18:41 -0800164 */
Santos Cordonc195e362014-02-11 17:05:31 -0800165 void setCallServices(Set<CallServiceWrapper> callServices) {
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800166 mBinderDeallocator.updateBinders(mCallServices);
Ben Gilad0407fb22014-01-09 16:18:41 -0800167
Evan Charltonf02e9882014-03-06 12:54:52 -0800168 mCallServices.clear();
169 mCallServices.addAll(callServices);
Ben Gilad0bf5b912014-01-28 17:55:57 -0800170 processNewOutgoingCalls();
Ben Giladb59769e2014-01-16 11:41:10 -0800171 }
172
173 /**
174 * Persists the specified list of selectors and attempts to connect any pending outgoing
Santos Cordonc195e362014-02-11 17:05:31 -0800175 * calls. Intended to be invoked by {@link CallServiceSelectorRepository} exclusively.
Ben Giladb59769e2014-01-16 11:41:10 -0800176 *
Ben Gilad03292d42014-01-16 15:06:16 -0800177 * @param selectors The potentially-partial set of selectors. Partial since the lookup
178 * procedure is time-boxed such that some selectors may be slow to respond and hence
179 * effectively omitted from the specified set.
Ben Giladb59769e2014-01-16 11:41:10 -0800180 */
Ben Gilad03292d42014-01-16 15:06:16 -0800181 void setSelectors(Set<ICallServiceSelector> selectors) {
Santos Cordonc195e362014-02-11 17:05:31 -0800182 // TODO(santoscordon): This should take in CallServiceSelectorWrapper instead of the direct
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800183 // ICallServiceSelector implementation. Copy what we have for CallServiceWrapper. Also need
184 // to invoke updateBinders(selectors) once this to-do is addressed.
Ben Giladb59769e2014-01-16 11:41:10 -0800185 ThreadUtil.checkOnMainThread();
186
Ben Gilad134cf092014-01-16 18:26:12 -0800187 // TODO(gilad): Add logic to include the built-in selectors (e.g. for dealing with
188 // emergency calls) and order the entire set prior to the assignment below. If the
189 // built-in selectors can be implemented in a manner that does not require binding,
190 // that's probably preferred. May want to use a LinkedHashSet for the sorted set.
Evan Charltonf02e9882014-03-06 12:54:52 -0800191 mSelectors.clear();
192 mSelectors.addAll(selectors);
Ben Gilad0bf5b912014-01-28 17:55:57 -0800193 processNewOutgoingCalls();
Ben Giladb59769e2014-01-16 11:41:10 -0800194 }
195
196 /**
Ben Gilad0bf5b912014-01-28 17:55:57 -0800197 * Handles the case where an outgoing call has been successfully placed,
198 * see {@link OutgoingCallProcessor}.
Ben Giladb59769e2014-01-16 11:41:10 -0800199 */
Ben Gilad0bf5b912014-01-28 17:55:57 -0800200 void handleSuccessfulOutgoingCall(Call call) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800201 Log.d(this, "handleSuccessfulOutgoingCall");
Ben Gilad0bf5b912014-01-28 17:55:57 -0800202
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800203 mCallsManager.handleSuccessfulOutgoingCall(call);
204 finalizeOutgoingCall(call);
Ben Gilad0bf5b912014-01-28 17:55:57 -0800205 }
206
207 /**
208 * Handles the case where an outgoing call could not be proceed by any of the
209 * selector/call-service implementations, see {@link OutgoingCallProcessor}.
210 */
211 void handleFailedOutgoingCall(Call call) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800212 Log.d(this, "handleFailedOutgoingCall");
Ben Gilad0bf5b912014-01-28 17:55:57 -0800213
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800214 // TODO(gilad): Notify mCallsManager.
215
216 finalizeOutgoingCall(call);
Ben Gilad0bf5b912014-01-28 17:55:57 -0800217 }
218
219 /**
Santos Cordon493e8f22014-02-19 03:15:12 -0800220 * Handles the case where we successfully receive details of an incoming call. Hands the
221 * resulting call to {@link CallsManager} as the final step in the incoming sequence. At that
222 * point, {@link CallsManager} should bring up the incoming-call UI.
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800223 */
224 void handleSuccessfulIncomingCall(Call call) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800225 Log.d(this, "handleSuccessfulIncomingCall");
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800226
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800227 mCallsManager.handleSuccessfulIncomingCall(call);
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800228 mBinderDeallocator.releaseUsePermit();
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800229 }
230
231 /**
Santos Cordon493e8f22014-02-19 03:15:12 -0800232 * Handles the case where we failed to retrieve an incoming call after receiving an incoming-call
233 * intent via {@link CallActivity}.
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800234 *
235 * @param call The call.
236 */
237 void handleFailedIncomingCall(Call call) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800238 Log.d(this, "handleFailedIncomingCall");
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800239
Santos Cordon493e8f22014-02-19 03:15:12 -0800240 // Since we set the call service before calling into incoming-calls manager, we clear it for
241 // good measure if an error is reported.
242 call.clearCallService();
243
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800244 mBinderDeallocator.releaseUsePermit();
245
Santos Cordon493e8f22014-02-19 03:15:12 -0800246 // At the moment there is nothing more to do if an incoming call is not retrieved. We may at
247 // a future date bind to the in-call app optimistically during the incoming-call sequence
248 // and this method could tell {@link CallsManager} to unbind from the in-call app if the
249 // incoming call was not retrieved.
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800250 }
251
252 /**
Ben Gilad2313e622014-02-06 12:02:25 -0800253 * @return True if ticking should continue (or be resumed) and false otherwise.
254 */
255 private boolean isTicking() {
256 // TODO(gilad): return true every time at least one outgoing call is pending (i.e. waiting
257 // to be connected by a call service) and also when at least one active call is switch-able
258 // between call services, see {@link ICallServiceSelector#isSwitchable}.
259 return false;
260 }
261
262 /**
263 * Schedules the next tick invocation.
264 */
265 private void scheduleNextTick() {
Evan Charlton1063e282014-03-05 19:31:21 -0800266 mHandler.postDelayed(mTicker, Timeouts.getTickMs());
Ben Gilad2313e622014-02-06 12:02:25 -0800267 }
268
269 /**
Ben Gilad0bf5b912014-01-28 17:55:57 -0800270 * Attempts to process the next new outgoing calls that have not yet been expired.
271 */
272 private void processNewOutgoingCalls() {
Evan Charltonf02e9882014-03-06 12:54:52 -0800273 if (mCallServices.isEmpty() || mSelectors.isEmpty()) {
Ben Gilad0bf5b912014-01-28 17:55:57 -0800274 // At least one call service and one selector are required to process outgoing calls.
275 return;
276 }
277
278 if (!mNewOutgoingCalls.isEmpty()) {
279 Call call = mNewOutgoingCalls.iterator().next();
280 mNewOutgoingCalls.remove(call);
281 mPendingOutgoingCalls.add(call);
282
283 // Specifically only attempt to place one call at a time such that call services
284 // can be freed from needing to deal with concurrent requests.
285 processNewOutgoingCall(call);
Ben Gilad0407fb22014-01-09 16:18:41 -0800286 }
Ben Gilad9f2bed32013-12-12 17:43:26 -0800287 }
288
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800289 /**
Ben Giladb59769e2014-01-16 11:41:10 -0800290 * Attempts to place the specified call.
291 *
Ben Gilad0bf5b912014-01-28 17:55:57 -0800292 * @param call The call to place.
Ben Giladb59769e2014-01-16 11:41:10 -0800293 */
Ben Gilad0bf5b912014-01-28 17:55:57 -0800294 private void processNewOutgoingCall(Call call) {
Ben Gilad0bf5b912014-01-28 17:55:57 -0800295 // Convert to (duplicate-free) list to aid index-based iteration, see the comment under
296 // setSelectors regarding using LinkedHashSet instead.
297 List<ICallServiceSelector> selectors = Lists.newArrayList();
298 selectors.addAll(mSelectors);
299
Santos Cordon681663d2014-01-30 04:32:15 -0800300 mOutgoingCallsManager.placeCall(call, mCallServices, selectors);
Ben Giladb59769e2014-01-16 11:41:10 -0800301 }
302
303 /**
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800304 * Finalizes the outgoing-call sequence, regardless if it succeeded or failed.
305 */
306 private void finalizeOutgoingCall(Call call) {
307 mPendingOutgoingCalls.remove(call);
308
309 mBinderDeallocator.releaseUsePermit();
310 processNewOutgoingCalls(); // Process additional (new) calls, if any.
311 }
312
313 /**
314 * Performs the set of tasks that needs to be executed on polling basis.
315 */
316 private void tick() {
317 // TODO(gilad): Add the necessary logic to support switching.
318
319 expireStaleOutgoingCalls(mNewOutgoingCalls);
320 expireStaleOutgoingCalls(mPendingOutgoingCalls);
321 }
322
323 /**
324 * Identifies stale calls and takes the necessary steps to mark these as expired.
325 *
326 * @param calls The set of calls to iterate through.
327 */
328 private void expireStaleOutgoingCalls(Set<Call> calls) {
329 if (calls.isEmpty()) {
330 return;
331 }
332
Evan Charlton1063e282014-03-05 19:31:21 -0800333 final long newCallTimeoutMs = Timeouts.getNewOutgoingCallMs();
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800334 Iterator<Call> iterator = calls.iterator();
335 while (iterator.hasNext()) {
336 Call call = iterator.next();
Evan Charlton1063e282014-03-05 19:31:21 -0800337 if (call.getAgeInMilliseconds() >= newCallTimeoutMs) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800338 Log.d(this, "Call %s timed out.", call.getId());
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800339 mOutgoingCallsManager.abort(call);
340 calls.remove(call);
341
342 // TODO(gilad): We may also have expired calls that are not yet associated with an
343 // OutgoingCallProcessor (e.g. when newer calls are "blocked" on older-yet-expired
344 // ones), in which case call.abort may need to be invoked directly. Alternatively
345 // we can also create an OutgoingCallsManager instance for every new call at intent-
346 // processing time.
347
348 // TODO(gilad): Notify the user in the relevant cases (at least outgoing).
349
350 mBinderDeallocator.releaseUsePermit();
351 }
352 }
353 }
Ben Gilad9f2bed32013-12-12 17:43:26 -0800354}