blob: 2ef58e3fc47e55297f21b7c2c47acace669dd2ad [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 */
Evan Charltonf02e9882014-03-06 12:54:52 -080097 private final Set<CallServiceWrapper> mCallServices = Sets.newHashSet();
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}.
Evan Charltonf02e9882014-03-06 12:54:52 -0800102 * TODO(gilad): Clear once the active-call count goes to zero.
Ben Gilad03292d42014-01-16 15:06:16 -0800103 */
Evan Charltonf02e9882014-03-06 12:54:52 -0800104 private final Set<ICallServiceSelector> mSelectors = Sets.newHashSet();
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.
Evan Charltonf02e9882014-03-06 12:54:52 -0800142 mCallServices.clear();
143 mSelectors.clear();
Ben Giladbb167cd2014-02-25 16:24:05 -0800144
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
Evan Charltonf02e9882014-03-06 12:54:52 -0800183 mCallServices.clear();
184 mCallServices.addAll(callServices);
Ben Gilad0bf5b912014-01-28 17:55:57 -0800185 processNewOutgoingCalls();
Ben Giladb59769e2014-01-16 11:41:10 -0800186 }
187
188 /**
189 * Persists the specified list of selectors and attempts to connect any pending outgoing
Santos Cordonc195e362014-02-11 17:05:31 -0800190 * calls. Intended to be invoked by {@link CallServiceSelectorRepository} exclusively.
Ben Giladb59769e2014-01-16 11:41:10 -0800191 *
Ben Gilad03292d42014-01-16 15:06:16 -0800192 * @param selectors The potentially-partial set of selectors. Partial since the lookup
193 * procedure is time-boxed such that some selectors may be slow to respond and hence
194 * effectively omitted from the specified set.
Ben Giladb59769e2014-01-16 11:41:10 -0800195 */
Ben Gilad03292d42014-01-16 15:06:16 -0800196 void setSelectors(Set<ICallServiceSelector> selectors) {
Santos Cordonc195e362014-02-11 17:05:31 -0800197 // TODO(santoscordon): This should take in CallServiceSelectorWrapper instead of the direct
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800198 // ICallServiceSelector implementation. Copy what we have for CallServiceWrapper. Also need
199 // to invoke updateBinders(selectors) once this to-do is addressed.
Ben Giladb59769e2014-01-16 11:41:10 -0800200 ThreadUtil.checkOnMainThread();
201
Ben Gilad134cf092014-01-16 18:26:12 -0800202 // TODO(gilad): Add logic to include the built-in selectors (e.g. for dealing with
203 // emergency calls) and order the entire set prior to the assignment below. If the
204 // built-in selectors can be implemented in a manner that does not require binding,
205 // that's probably preferred. May want to use a LinkedHashSet for the sorted set.
Evan Charltonf02e9882014-03-06 12:54:52 -0800206 mSelectors.clear();
207 mSelectors.addAll(selectors);
Ben Gilad0bf5b912014-01-28 17:55:57 -0800208 processNewOutgoingCalls();
Ben Giladb59769e2014-01-16 11:41:10 -0800209 }
210
211 /**
Ben Gilad0bf5b912014-01-28 17:55:57 -0800212 * Handles the case where an outgoing call has been successfully placed,
213 * see {@link OutgoingCallProcessor}.
Ben Giladb59769e2014-01-16 11:41:10 -0800214 */
Ben Gilad0bf5b912014-01-28 17:55:57 -0800215 void handleSuccessfulOutgoingCall(Call call) {
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800216 Log.d(TAG, "handleSuccessfulOutgoingCall");
Ben Gilad0bf5b912014-01-28 17:55:57 -0800217
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800218 mCallsManager.handleSuccessfulOutgoingCall(call);
219 finalizeOutgoingCall(call);
Ben Gilad0bf5b912014-01-28 17:55:57 -0800220 }
221
222 /**
223 * Handles the case where an outgoing call could not be proceed by any of the
224 * selector/call-service implementations, see {@link OutgoingCallProcessor}.
225 */
226 void handleFailedOutgoingCall(Call call) {
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800227 Log.d(TAG, "handleFailedOutgoingCall");
Ben Gilad0bf5b912014-01-28 17:55:57 -0800228
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800229 // TODO(gilad): Notify mCallsManager.
230
231 finalizeOutgoingCall(call);
Ben Gilad0bf5b912014-01-28 17:55:57 -0800232 }
233
234 /**
Santos Cordon493e8f22014-02-19 03:15:12 -0800235 * Handles the case where we successfully receive details of an incoming call. Hands the
236 * resulting call to {@link CallsManager} as the final step in the incoming sequence. At that
237 * point, {@link CallsManager} should bring up the incoming-call UI.
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800238 */
239 void handleSuccessfulIncomingCall(Call call) {
Santos Cordon493e8f22014-02-19 03:15:12 -0800240 Log.d(TAG, "handleSuccessfulIncomingCall");
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800241
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800242 mCallsManager.handleSuccessfulIncomingCall(call);
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800243 mBinderDeallocator.releaseUsePermit();
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800244 }
245
246 /**
Santos Cordon493e8f22014-02-19 03:15:12 -0800247 * Handles the case where we failed to retrieve an incoming call after receiving an incoming-call
248 * intent via {@link CallActivity}.
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800249 *
250 * @param call The call.
251 */
252 void handleFailedIncomingCall(Call call) {
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800253 Log.d(TAG, "handleFailedIncomingCall");
254
Santos Cordon493e8f22014-02-19 03:15:12 -0800255 // Since we set the call service before calling into incoming-calls manager, we clear it for
256 // good measure if an error is reported.
257 call.clearCallService();
258
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800259 mBinderDeallocator.releaseUsePermit();
260
Santos Cordon493e8f22014-02-19 03:15:12 -0800261 // At the moment there is nothing more to do if an incoming call is not retrieved. We may at
262 // a future date bind to the in-call app optimistically during the incoming-call sequence
263 // and this method could tell {@link CallsManager} to unbind from the in-call app if the
264 // incoming call was not retrieved.
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800265 }
266
267 /**
Ben Gilad2313e622014-02-06 12:02:25 -0800268 * @return True if ticking should continue (or be resumed) and false otherwise.
269 */
270 private boolean isTicking() {
271 // TODO(gilad): return true every time at least one outgoing call is pending (i.e. waiting
272 // to be connected by a call service) and also when at least one active call is switch-able
273 // between call services, see {@link ICallServiceSelector#isSwitchable}.
274 return false;
275 }
276
277 /**
278 * Schedules the next tick invocation.
279 */
280 private void scheduleNextTick() {
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800281 mHandler.postDelayed(mTicker, TICK_FREQUENCY_MS);
Ben Gilad2313e622014-02-06 12:02:25 -0800282 }
283
284 /**
Ben Gilad0bf5b912014-01-28 17:55:57 -0800285 * Attempts to process the next new outgoing calls that have not yet been expired.
286 */
287 private void processNewOutgoingCalls() {
Evan Charltonf02e9882014-03-06 12:54:52 -0800288 if (mCallServices.isEmpty() || mSelectors.isEmpty()) {
Ben Gilad0bf5b912014-01-28 17:55:57 -0800289 // At least one call service and one selector are required to process outgoing calls.
290 return;
291 }
292
293 if (!mNewOutgoingCalls.isEmpty()) {
294 Call call = mNewOutgoingCalls.iterator().next();
295 mNewOutgoingCalls.remove(call);
296 mPendingOutgoingCalls.add(call);
297
298 // Specifically only attempt to place one call at a time such that call services
299 // can be freed from needing to deal with concurrent requests.
300 processNewOutgoingCall(call);
Ben Gilad0407fb22014-01-09 16:18:41 -0800301 }
Ben Gilad9f2bed32013-12-12 17:43:26 -0800302 }
303
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800304 /**
Ben Giladb59769e2014-01-16 11:41:10 -0800305 * Attempts to place the specified call.
306 *
Ben Gilad0bf5b912014-01-28 17:55:57 -0800307 * @param call The call to place.
Ben Giladb59769e2014-01-16 11:41:10 -0800308 */
Ben Gilad0bf5b912014-01-28 17:55:57 -0800309 private void processNewOutgoingCall(Call call) {
Ben Gilad0bf5b912014-01-28 17:55:57 -0800310 // Convert to (duplicate-free) list to aid index-based iteration, see the comment under
311 // setSelectors regarding using LinkedHashSet instead.
312 List<ICallServiceSelector> selectors = Lists.newArrayList();
313 selectors.addAll(mSelectors);
314
Santos Cordon681663d2014-01-30 04:32:15 -0800315 mOutgoingCallsManager.placeCall(call, mCallServices, selectors);
Ben Giladb59769e2014-01-16 11:41:10 -0800316 }
317
318 /**
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800319 * Finalizes the outgoing-call sequence, regardless if it succeeded or failed.
320 */
321 private void finalizeOutgoingCall(Call call) {
322 mPendingOutgoingCalls.remove(call);
323
324 mBinderDeallocator.releaseUsePermit();
325 processNewOutgoingCalls(); // Process additional (new) calls, if any.
326 }
327
328 /**
329 * Performs the set of tasks that needs to be executed on polling basis.
330 */
331 private void tick() {
332 // TODO(gilad): Add the necessary logic to support switching.
333
334 expireStaleOutgoingCalls(mNewOutgoingCalls);
335 expireStaleOutgoingCalls(mPendingOutgoingCalls);
336 }
337
338 /**
339 * Identifies stale calls and takes the necessary steps to mark these as expired.
340 *
341 * @param calls The set of calls to iterate through.
342 */
343 private void expireStaleOutgoingCalls(Set<Call> calls) {
344 if (calls.isEmpty()) {
345 return;
346 }
347
348 Iterator<Call> iterator = calls.iterator();
349 while (iterator.hasNext()) {
350 Call call = iterator.next();
351 if (call.getAgeInMilliseconds() >= NEW_CALL_TIMEOUT_MS) {
352 mOutgoingCallsManager.abort(call);
353 calls.remove(call);
354
355 // TODO(gilad): We may also have expired calls that are not yet associated with an
356 // OutgoingCallProcessor (e.g. when newer calls are "blocked" on older-yet-expired
357 // ones), in which case call.abort may need to be invoked directly. Alternatively
358 // we can also create an OutgoingCallsManager instance for every new call at intent-
359 // processing time.
360
361 // TODO(gilad): Notify the user in the relevant cases (at least outgoing).
362
363 mBinderDeallocator.releaseUsePermit();
364 }
365 }
366 }
Ben Gilad9f2bed32013-12-12 17:43:26 -0800367}