blob: d8c57fb9339fe738b9b61d10d01740671126c759 [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
Sailesh Nepal18386a82014-03-19 10:22:40 -070019import com.google.common.base.Preconditions;
20import com.google.common.collect.ImmutableCollection;
21import com.google.common.collect.ImmutableList;
Evan Charltonac1aa9e2014-01-03 13:47:12 -080022import com.google.common.collect.Lists;
Ben Gilad0407fb22014-01-09 16:18:41 -080023import com.google.common.collect.Sets;
Santos Cordon8e8b8d22013-12-19 14:14:05 -080024
Evan Charltona05805b2014-03-05 08:21:46 -080025import android.os.Bundle;
Ben Gilad2313e622014-02-06 12:02:25 -080026import android.os.Handler;
27import android.os.Looper;
Sailesh Nepal810735e2014-03-18 18:15:46 -070028import android.telecomm.CallInfo;
Ben Giladc5b22692014-02-18 20:03:22 -080029import android.telecomm.CallServiceDescriptor;
Evan Charltona05805b2014-03-05 08:21:46 -080030import android.telecomm.TelecommConstants;
Santos Cordon8e8b8d22013-12-19 14:14:05 -080031
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/**
Sailesh Nepal18386a82014-03-19 10:22:40 -070037 * Switchboard is responsible for:
38 * <ul>
39 * <li> gathering the {@link CallServiceWrapper}s and {@link CallServiceSelectorWrapper}s through which to place
40 * outgoing calls
41 * <li> starting outgoing calls (via {@link OutgoingCallsManager}
42 * <li> switching active calls between call services.
43 * </ul>
Santos Cordon8e8b8d22013-12-19 14:14:05 -080044 */
Ben Giladd17443c2014-01-06 11:04:15 -080045final class Switchboard {
Ben Gilad2495d572014-01-09 17:26:19 -080046
Santos Cordon681663d2014-01-30 04:32:15 -080047 private final CallsManager mCallsManager;
Ben Gilad2313e622014-02-06 12:02:25 -080048
Santos Cordon681663d2014-01-30 04:32:15 -080049 /** Used to place outgoing calls. */
50 private final OutgoingCallsManager mOutgoingCallsManager;
51
Santos Cordon493e8f22014-02-19 03:15:12 -080052 /** Used to retrieve incoming call details. */
Santos Cordon80d9bdc2014-02-13 18:28:46 -080053 private final IncomingCallsManager mIncomingCallsManager;
54
Santos Cordonc195e362014-02-11 17:05:31 -080055 private final CallServiceRepository mCallServiceRepository;
Santos Cordon681663d2014-01-30 04:32:15 -080056
Santos Cordonc195e362014-02-11 17:05:31 -080057 private final CallServiceSelectorRepository mSelectorRepository;
Ben Gilad2313e622014-02-06 12:02:25 -080058
Ben Gilad8e55d1d2014-02-26 16:25:56 -080059 private final BinderDeallocator mBinderDeallocator;
60
Ben Gilad2313e622014-02-06 12:02:25 -080061 /** Used to schedule tasks on the main (UI) thread. */
62 private final Handler mHandler = new Handler(Looper.getMainLooper());
63
64 /**
65 * Executes a single tick task and potentially schedules the next such that polling continues
66 * as long as necessary.
67 * NOTE(gilad): by design no two tick invocations should ever overlap.
68 */
69 private final Runnable mTicker = new Runnable() {
70 @Override
71 public void run() {
72 tick();
73 if (isTicking()) {
74 scheduleNextTick();
75 }
76 }
77 };
78
79 private final Set<Call> mNewOutgoingCalls = Sets.newLinkedHashSet();
80
81 private final Set<Call> mPendingOutgoingCalls = Sets.newLinkedHashSet();
Ben Giladb59769e2014-01-16 11:41:10 -080082
Ben Gilad03292d42014-01-16 15:06:16 -080083 /**
Ben Giladbb167cd2014-02-25 16:24:05 -080084 * The set of currently available call-service implementations, see
Ben Gilad37587232014-02-19 16:03:44 -080085 * {@link CallServiceRepository}. Populated during call-service lookup cycles as part of the
86 * {@link #placeOutgoingCall} flow and cleared upon zero-remaining new/pending outgoing calls.
Ben Gilad03292d42014-01-16 15:06:16 -080087 */
Evan Charltonf02e9882014-03-06 12:54:52 -080088 private final Set<CallServiceWrapper> mCallServices = Sets.newHashSet();
Ben Giladb59769e2014-01-16 11:41:10 -080089
Ben Gilad03292d42014-01-16 15:06:16 -080090 /**
91 * The set of currently available call-service-selector implementations,
Santos Cordonc195e362014-02-11 17:05:31 -080092 * see {@link CallServiceSelectorRepository}.
Evan Charltonf02e9882014-03-06 12:54:52 -080093 * TODO(gilad): Clear once the active-call count goes to zero.
Ben Gilad03292d42014-01-16 15:06:16 -080094 */
Sailesh Nepal18386a82014-03-19 10:22:40 -070095 private ImmutableCollection<CallServiceSelectorWrapper> mSelectors = ImmutableList.of();
Ben Gilad0407fb22014-01-09 16:18:41 -080096
Santos Cordon681663d2014-01-30 04:32:15 -080097 /**
Santos Cordonc195e362014-02-11 17:05:31 -080098 * The current lookup-cycle ID used with the repositories. Incremented with each invocation
Ben Gilad37587232014-02-19 16:03:44 -080099 * of {@link #placeOutgoingCall} and passed to the repositories via initiateLookup().
Santos Cordonc195e362014-02-11 17:05:31 -0800100 */
101 private int mLookupId = 0;
102
103 /**
Santos Cordon681663d2014-01-30 04:32:15 -0800104 * Persists the specified parameters and initializes Switchboard.
105 */
Santos Cordon6242b132014-02-07 16:24:42 -0800106 Switchboard(CallsManager callsManager) {
107 mCallsManager = callsManager;
Santos Cordon681663d2014-01-30 04:32:15 -0800108 mOutgoingCallsManager = new OutgoingCallsManager(this);
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800109 mIncomingCallsManager = new IncomingCallsManager(this);
Santos Cordonc195e362014-02-11 17:05:31 -0800110 mSelectorRepository = new CallServiceSelectorRepository(this);
Santos Cordon493e8f22014-02-19 03:15:12 -0800111 mCallServiceRepository =
112 new CallServiceRepository(this, mOutgoingCallsManager, mIncomingCallsManager);
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800113
114 mBinderDeallocator = new BinderDeallocator();
Santos Cordon681663d2014-01-30 04:32:15 -0800115 }
Ben Giladd17443c2014-01-06 11:04:15 -0800116
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800117 /**
Santos Cordonc195e362014-02-11 17:05:31 -0800118 * Starts the process of placing an outgoing call by searching for available call services
119 * through which the call can be placed. After a lookup for those services completes, execution
120 * returns to {@link #setCallServices} where the process of placing the call continues.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800121 *
Ben Gilad13329fd2014-02-11 17:20:29 -0800122 * @param call The yet-to-be-connected outgoing-call object.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800123 */
Santos Cordonc195e362014-02-11 17:05:31 -0800124 void placeOutgoingCall(Call call) {
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800125 mBinderDeallocator.acquireUsePermit();
Ben Giladb59769e2014-01-16 11:41:10 -0800126
Ben Giladbb167cd2014-02-25 16:24:05 -0800127 // Reset prior to initiating the next lookup. One case to consider is (1) placeOutgoingCall
128 // is invoked with call A, (2) the call-service lookup completes, but the one for selectors
129 // does not, (3) placeOutgoingCall is invoked again with call B, (4) mCallServices below is
130 // reset, (5) the selector lookup completes but the call-services are missing. This should
131 // be okay since the call-service lookup completed. Specifically the already-available call
132 // services are cached and will be provided in response to the second lookup cycle.
Evan Charltonf02e9882014-03-06 12:54:52 -0800133 mCallServices.clear();
Sailesh Nepal18386a82014-03-19 10:22:40 -0700134 mSelectors = ImmutableList.of();
Ben Giladbb167cd2014-02-25 16:24:05 -0800135
Sailesh Nepalb6141ae2014-02-18 08:45:26 -0800136 mNewOutgoingCalls.add(call);
Ben Giladb59769e2014-01-16 11:41:10 -0800137
Ben Giladebd9b662014-02-19 16:03:44 -0800138 // Initiate a lookup every time to account for newly-installed apps and/or updated settings.
Sailesh Nepalb6141ae2014-02-18 08:45:26 -0800139 mLookupId++;
Santos Cordonc195e362014-02-11 17:05:31 -0800140 mCallServiceRepository.initiateLookup(mLookupId);
141 mSelectorRepository.initiateLookup(mLookupId);
Ben Gilad0407fb22014-01-09 16:18:41 -0800142 }
Ben Gilad9f2bed32013-12-12 17:43:26 -0800143
Ben Gilad0407fb22014-01-09 16:18:41 -0800144 /**
Santos Cordon493e8f22014-02-19 03:15:12 -0800145 * Retrieves details about the incoming call through the incoming call manager. The incoming
146 * call manager will invoke either {@link #handleSuccessfulIncomingCall} or
147 * {@link #handleFailedIncomingCall} depending on the result of the retrieval.
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800148 *
149 * @param call The call object.
Ben Giladc5b22692014-02-18 20:03:22 -0800150 * @param descriptor The relevant call-service descriptor.
Evan Charltona05805b2014-03-05 08:21:46 -0800151 * @param extras The optional extras passed via
152 * {@link TelecommConstants#EXTRA_INCOMING_CALL_EXTRAS}
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800153 */
Evan Charltona05805b2014-03-05 08:21:46 -0800154 void retrieveIncomingCall(Call call, CallServiceDescriptor descriptor, Bundle extras) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800155 Log.d(this, "retrieveIncomingCall");
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800156 mBinderDeallocator.acquireUsePermit();
157
Ben Giladc5b22692014-02-18 20:03:22 -0800158 CallServiceWrapper callService = mCallServiceRepository.getCallService(descriptor);
Santos Cordon493e8f22014-02-19 03:15:12 -0800159 call.setCallService(callService);
Evan Charltona05805b2014-03-05 08:21:46 -0800160 mIncomingCallsManager.retrieveIncomingCall(call, extras);
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800161 }
162
163 /**
Ben Gilad0bf5b912014-01-28 17:55:57 -0800164 * Persists the specified set of call services and attempts to place any pending outgoing
Santos Cordonc195e362014-02-11 17:05:31 -0800165 * calls. Intended to be invoked by {@link CallServiceRepository} exclusively.
Ben Gilad0407fb22014-01-09 16:18:41 -0800166 *
Ben Gilad03292d42014-01-16 15:06:16 -0800167 * @param callServices The potentially-partial set of call services. Partial since the lookup
168 * process is time-boxed, such that some providers/call-services may be slow to respond and
169 * hence effectively omitted from the specified list.
Ben Gilad0407fb22014-01-09 16:18:41 -0800170 */
Santos Cordonc195e362014-02-11 17:05:31 -0800171 void setCallServices(Set<CallServiceWrapper> callServices) {
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800172 mBinderDeallocator.updateBinders(mCallServices);
Ben Gilad0407fb22014-01-09 16:18:41 -0800173
Evan Charltonf02e9882014-03-06 12:54:52 -0800174 mCallServices.clear();
175 mCallServices.addAll(callServices);
Ben Gilad0bf5b912014-01-28 17:55:57 -0800176 processNewOutgoingCalls();
Ben Giladb59769e2014-01-16 11:41:10 -0800177 }
178
179 /**
180 * Persists the specified list of selectors and attempts to connect any pending outgoing
Santos Cordonc195e362014-02-11 17:05:31 -0800181 * calls. Intended to be invoked by {@link CallServiceSelectorRepository} exclusively.
Ben Giladb59769e2014-01-16 11:41:10 -0800182 *
Sailesh Nepal18386a82014-03-19 10:22:40 -0700183 * @param selectors Collection of selectors. The order of the collection determines the order in
184 * which the selectors are tried.
Ben Giladb59769e2014-01-16 11:41:10 -0800185 */
Sailesh Nepal18386a82014-03-19 10:22:40 -0700186 void setSelectors(ImmutableCollection<CallServiceSelectorWrapper> selectors) {
187 // TODO(santoscordon):: Need to invoke updateBinders(selectors).
Ben Giladb59769e2014-01-16 11:41:10 -0800188 ThreadUtil.checkOnMainThread();
Sailesh Nepal18386a82014-03-19 10:22:40 -0700189 Preconditions.checkNotNull(selectors);
Ben Giladb59769e2014-01-16 11:41:10 -0800190
Ben Gilad134cf092014-01-16 18:26:12 -0800191 // TODO(gilad): Add logic to include the built-in selectors (e.g. for dealing with
192 // emergency calls) and order the entire set prior to the assignment below. If the
193 // built-in selectors can be implemented in a manner that does not require binding,
Sailesh Nepal18386a82014-03-19 10:22:40 -0700194 // that's probably preferred.
195 mSelectors = selectors;
Ben Gilad0bf5b912014-01-28 17:55:57 -0800196 processNewOutgoingCalls();
Ben Giladb59769e2014-01-16 11:41:10 -0800197 }
198
199 /**
Ben Gilad0bf5b912014-01-28 17:55:57 -0800200 * Handles the case where an outgoing call has been successfully placed,
201 * see {@link OutgoingCallProcessor}.
Ben Giladb59769e2014-01-16 11:41:10 -0800202 */
Ben Gilad0bf5b912014-01-28 17:55:57 -0800203 void handleSuccessfulOutgoingCall(Call call) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800204 Log.d(this, "handleSuccessfulOutgoingCall");
Ben Gilad0bf5b912014-01-28 17:55:57 -0800205
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800206 mCallsManager.handleSuccessfulOutgoingCall(call);
207 finalizeOutgoingCall(call);
Ben Gilad0bf5b912014-01-28 17:55:57 -0800208 }
209
210 /**
211 * Handles the case where an outgoing call could not be proceed by any of the
212 * selector/call-service implementations, see {@link OutgoingCallProcessor}.
213 */
Sailesh Nepal810735e2014-03-18 18:15:46 -0700214 void handleFailedOutgoingCall(Call call, boolean isAborted) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800215 Log.d(this, "handleFailedOutgoingCall");
Ben Gilad0bf5b912014-01-28 17:55:57 -0800216
Sailesh Nepal810735e2014-03-18 18:15:46 -0700217 mCallsManager.handleUnsuccessfulOutgoingCall(call, isAborted);
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800218 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 */
Sailesh Nepal810735e2014-03-18 18:15:46 -0700226 void handleSuccessfulIncomingCall(Call call, CallInfo callInfo) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800227 Log.d(this, "handleSuccessfulIncomingCall");
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800228
Sailesh Nepal810735e2014-03-18 18:15:46 -0700229 mCallsManager.handleSuccessfulIncomingCall(call, callInfo);
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) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800240 Log.d(this, "handleFailedIncomingCall");
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800241
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
Sailesh Nepal810735e2014-03-18 18:15:46 -0700248 mCallsManager.handleUnsuccessfulIncomingCall(call);
249
Santos Cordon493e8f22014-02-19 03:15:12 -0800250 // At the moment there is nothing more to do if an incoming call is not retrieved. We may at
251 // a future date bind to the in-call app optimistically during the incoming-call sequence
252 // and this method could tell {@link CallsManager} to unbind from the in-call app if the
253 // incoming call was not retrieved.
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800254 }
255
256 /**
Ben Gilad2313e622014-02-06 12:02:25 -0800257 * @return True if ticking should continue (or be resumed) and false otherwise.
258 */
259 private boolean isTicking() {
260 // TODO(gilad): return true every time at least one outgoing call is pending (i.e. waiting
261 // to be connected by a call service) and also when at least one active call is switch-able
262 // between call services, see {@link ICallServiceSelector#isSwitchable}.
263 return false;
264 }
265
266 /**
267 * Schedules the next tick invocation.
268 */
269 private void scheduleNextTick() {
Evan Charlton1063e282014-03-05 19:31:21 -0800270 mHandler.postDelayed(mTicker, Timeouts.getTickMs());
Ben Gilad2313e622014-02-06 12:02:25 -0800271 }
272
273 /**
Ben Gilad0bf5b912014-01-28 17:55:57 -0800274 * Attempts to process the next new outgoing calls that have not yet been expired.
275 */
276 private void processNewOutgoingCalls() {
Evan Charltonf02e9882014-03-06 12:54:52 -0800277 if (mCallServices.isEmpty() || mSelectors.isEmpty()) {
Ben Gilad0bf5b912014-01-28 17:55:57 -0800278 // At least one call service and one selector are required to process outgoing calls.
279 return;
280 }
281
282 if (!mNewOutgoingCalls.isEmpty()) {
283 Call call = mNewOutgoingCalls.iterator().next();
284 mNewOutgoingCalls.remove(call);
285 mPendingOutgoingCalls.add(call);
286
287 // Specifically only attempt to place one call at a time such that call services
288 // can be freed from needing to deal with concurrent requests.
289 processNewOutgoingCall(call);
Ben Gilad0407fb22014-01-09 16:18:41 -0800290 }
Ben Gilad9f2bed32013-12-12 17:43:26 -0800291 }
292
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800293 /**
Ben Giladb59769e2014-01-16 11:41:10 -0800294 * Attempts to place the specified call.
295 *
Ben Gilad0bf5b912014-01-28 17:55:57 -0800296 * @param call The call to place.
Ben Giladb59769e2014-01-16 11:41:10 -0800297 */
Ben Gilad0bf5b912014-01-28 17:55:57 -0800298 private void processNewOutgoingCall(Call call) {
Sailesh Nepal18386a82014-03-19 10:22:40 -0700299 mOutgoingCallsManager.placeCall(call, mCallServices, mSelectors);
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}