blob: 5283e0bb54b02720ffa51aea9580300c590fc685 [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
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;
Santos Cordon8e8b8d22013-12-19 14:14:05 -080027
Ben Giladb59769e2014-01-16 11:41:10 -080028import java.util.Collection;
Ben Gilad9f2bed32013-12-12 17:43:26 -080029import java.util.List;
Ben Gilad0407fb22014-01-09 16:18:41 -080030import java.util.Set;
Ben Gilad9f2bed32013-12-12 17:43:26 -080031
Santos Cordon8e8b8d22013-12-19 14:14:05 -080032/**
Santos Cordonc195e362014-02-11 17:05:31 -080033 * Switchboard is responsible for (1) gathering the {@link CallServiceWrapper}s and
34 * {@link ICallServiceSelector}s through which to place outgoing calls, (2) starting outgoing calls
35 * (via {@link OutgoingCallsManager} and (3) switching active calls between call services.
Santos Cordon8e8b8d22013-12-19 14:14:05 -080036 */
Ben Giladd17443c2014-01-06 11:04:15 -080037final class Switchboard {
Ben Gilad2495d572014-01-09 17:26:19 -080038
Ben Gilad2313e622014-02-06 12:02:25 -080039 /**
40 * The frequency of invoking tick in milliseconds.
41 * TODO(gilad): May require tuning.
42 */
43 private final static int TICK_FREQUENCY = 250;
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 Cordon80d9bdc2014-02-13 18:28:46 -080050 /** Used to confirm incoming calls. */
51 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
57 /** Used to schedule tasks on the main (UI) thread. */
58 private final Handler mHandler = new Handler(Looper.getMainLooper());
59
60 /**
61 * Executes a single tick task and potentially schedules the next such that polling continues
62 * as long as necessary.
63 * NOTE(gilad): by design no two tick invocations should ever overlap.
64 */
65 private final Runnable mTicker = new Runnable() {
66 @Override
67 public void run() {
68 tick();
69 if (isTicking()) {
70 scheduleNextTick();
71 }
72 }
73 };
74
75 private final Set<Call> mNewOutgoingCalls = Sets.newLinkedHashSet();
76
77 private final Set<Call> mPendingOutgoingCalls = Sets.newLinkedHashSet();
Ben Giladb59769e2014-01-16 11:41:10 -080078
Ben Gilad03292d42014-01-16 15:06:16 -080079 /**
Ben Giladbb167cd2014-02-25 16:24:05 -080080 * The set of currently available call-service implementations, see
Ben Gilad37587232014-02-19 16:03:44 -080081 * {@link CallServiceRepository}. Populated during call-service lookup cycles as part of the
82 * {@link #placeOutgoingCall} flow and cleared upon zero-remaining new/pending outgoing calls.
Ben Gilad03292d42014-01-16 15:06:16 -080083 */
Santos Cordonc195e362014-02-11 17:05:31 -080084 private Set<CallServiceWrapper> mCallServices;
Ben Giladb59769e2014-01-16 11:41:10 -080085
Ben Gilad03292d42014-01-16 15:06:16 -080086 /**
87 * The set of currently available call-service-selector implementations,
Santos Cordonc195e362014-02-11 17:05:31 -080088 * see {@link CallServiceSelectorRepository}.
Ben Gilad03292d42014-01-16 15:06:16 -080089 * TODO(gilad): Null out once the active-call count goes to zero.
90 */
91 private Set<ICallServiceSelector> mSelectors;
Ben Gilad0407fb22014-01-09 16:18:41 -080092
Santos Cordon681663d2014-01-30 04:32:15 -080093 /**
Santos Cordonc195e362014-02-11 17:05:31 -080094 * The current lookup-cycle ID used with the repositories. Incremented with each invocation
Ben Gilad37587232014-02-19 16:03:44 -080095 * of {@link #placeOutgoingCall} and passed to the repositories via initiateLookup().
Santos Cordonc195e362014-02-11 17:05:31 -080096 */
97 private int mLookupId = 0;
98
99 /**
Santos Cordon681663d2014-01-30 04:32:15 -0800100 * Persists the specified parameters and initializes Switchboard.
101 */
Santos Cordon6242b132014-02-07 16:24:42 -0800102 Switchboard(CallsManager callsManager) {
103 mCallsManager = callsManager;
Santos Cordon681663d2014-01-30 04:32:15 -0800104 mOutgoingCallsManager = new OutgoingCallsManager(this);
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800105 mIncomingCallsManager = new IncomingCallsManager(this);
Santos Cordonc195e362014-02-11 17:05:31 -0800106 mCallServiceRepository = new CallServiceRepository(this, mOutgoingCallsManager);
107 mSelectorRepository = new CallServiceSelectorRepository(this);
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) {
Evan Charlton0958f532014-01-10 16:58:02 -0800118 ThreadUtil.checkOnMainThread();
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.
126 mCallServices = null;
127 mSelectors = null;
128
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 Cordon80d9bdc2014-02-13 18:28:46 -0800138 * Confirms with incoming call manager that an incoming call exists for the specified call
139 * service and call token. The incoming call manager will invoke either
140 * {@link #handleSuccessfulIncomingCall} or {@link #handleFailedIncomingCall} depending
141 * on the result.
142 *
143 * @param call The call object.
Ben Giladc5b22692014-02-18 20:03:22 -0800144 * @param descriptor The relevant call-service descriptor.
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800145 * @param callToken The token used by the call service to identify the incoming call.
146 */
Ben Giladc5b22692014-02-18 20:03:22 -0800147 void confirmIncomingCall(Call call, CallServiceDescriptor descriptor, String callToken) {
148 CallServiceWrapper callService = mCallServiceRepository.getCallService(descriptor);
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800149 mIncomingCallsManager.confirmIncomingCall(call, callService, callToken);
150 }
151
152 /**
Ben Gilad0bf5b912014-01-28 17:55:57 -0800153 * Persists the specified set of call services and attempts to place any pending outgoing
Santos Cordonc195e362014-02-11 17:05:31 -0800154 * calls. Intended to be invoked by {@link CallServiceRepository} exclusively.
Ben Gilad0407fb22014-01-09 16:18:41 -0800155 *
Ben Gilad03292d42014-01-16 15:06:16 -0800156 * @param callServices The potentially-partial set of call services. Partial since the lookup
157 * process is time-boxed, such that some providers/call-services may be slow to respond and
158 * hence effectively omitted from the specified list.
Ben Gilad0407fb22014-01-09 16:18:41 -0800159 */
Santos Cordonc195e362014-02-11 17:05:31 -0800160 void setCallServices(Set<CallServiceWrapper> callServices) {
Evan Charlton0958f532014-01-10 16:58:02 -0800161 ThreadUtil.checkOnMainThread();
Ben Gilad0407fb22014-01-09 16:18:41 -0800162
Ben Giladb59769e2014-01-16 11:41:10 -0800163 mCallServices = callServices;
Ben Gilad0bf5b912014-01-28 17:55:57 -0800164 processNewOutgoingCalls();
Ben Giladb59769e2014-01-16 11:41:10 -0800165 }
166
167 /**
168 * Persists the specified list of selectors and attempts to connect any pending outgoing
Santos Cordonc195e362014-02-11 17:05:31 -0800169 * calls. Intended to be invoked by {@link CallServiceSelectorRepository} exclusively.
Ben Giladb59769e2014-01-16 11:41:10 -0800170 *
Ben Gilad03292d42014-01-16 15:06:16 -0800171 * @param selectors The potentially-partial set of selectors. Partial since the lookup
172 * procedure is time-boxed such that some selectors may be slow to respond and hence
173 * effectively omitted from the specified set.
Ben Giladb59769e2014-01-16 11:41:10 -0800174 */
Ben Gilad03292d42014-01-16 15:06:16 -0800175 void setSelectors(Set<ICallServiceSelector> selectors) {
Santos Cordonc195e362014-02-11 17:05:31 -0800176 // TODO(santoscordon): This should take in CallServiceSelectorWrapper instead of the direct
177 // ICallServiceSelector implementation. Copy what we have for CallServiceWrapper.
Ben Giladb59769e2014-01-16 11:41:10 -0800178 ThreadUtil.checkOnMainThread();
179
Ben Gilad134cf092014-01-16 18:26:12 -0800180 // TODO(gilad): Add logic to include the built-in selectors (e.g. for dealing with
181 // emergency calls) and order the entire set prior to the assignment below. If the
182 // built-in selectors can be implemented in a manner that does not require binding,
183 // that's probably preferred. May want to use a LinkedHashSet for the sorted set.
Ben Giladb59769e2014-01-16 11:41:10 -0800184 mSelectors = selectors;
Ben Gilad0bf5b912014-01-28 17:55:57 -0800185 processNewOutgoingCalls();
Ben Giladb59769e2014-01-16 11:41:10 -0800186 }
187
188 /**
Ben Gilad0bf5b912014-01-28 17:55:57 -0800189 * Handles the case where an outgoing call has been successfully placed,
190 * see {@link OutgoingCallProcessor}.
Ben Giladb59769e2014-01-16 11:41:10 -0800191 */
Ben Gilad0bf5b912014-01-28 17:55:57 -0800192 void handleSuccessfulOutgoingCall(Call call) {
Santos Cordon681663d2014-01-30 04:32:15 -0800193 mCallsManager.handleSuccessfulOutgoingCall(call);
Ben Gilad0bf5b912014-01-28 17:55:57 -0800194
195 // Process additional (new) calls, if any.
196 processNewOutgoingCalls();
197 }
198
199 /**
200 * Handles the case where an outgoing call could not be proceed by any of the
201 * selector/call-service implementations, see {@link OutgoingCallProcessor}.
202 */
203 void handleFailedOutgoingCall(Call call) {
204 // TODO(gilad): More here.
205
206 // Process additional (new) calls, if any.
207 processNewOutgoingCalls();
208 }
209
210 /**
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800211 * Handles the case where we received confirmation of an incoming call. Hands the resulting
212 * call to {@link CallsManager} as the final step in the incoming sequence. At that point,
213 * {@link CallsManager} should bring up the incoming-call UI.
214 */
215 void handleSuccessfulIncomingCall(Call call) {
216 mCallsManager.handleSuccessfulIncomingCall(call);
217 }
218
219 /**
220 * Handles the case where we failed to confirm an incoming call after receiving an incoming-call
221 * intent via {@link TelecommReceiver}.
222 *
223 * @param call The call.
224 */
225 void handleFailedIncomingCall(Call call) {
226 // At the moment there is nothing to do if an incoming call is not confirmed. We may at a
227 // future date bind to the in-call app optimistically during the incoming-call sequence and
228 // this method could tell {@link CallsManager} to unbind from the in-call app if the
229 // incoming call was not confirmed. It's worth keeping this method for parity with the
230 // outgoing call sequence.
231 }
232
233 /**
Ben Gilad2313e622014-02-06 12:02:25 -0800234 * @return True if ticking should continue (or be resumed) and false otherwise.
235 */
236 private boolean isTicking() {
237 // TODO(gilad): return true every time at least one outgoing call is pending (i.e. waiting
238 // to be connected by a call service) and also when at least one active call is switch-able
239 // between call services, see {@link ICallServiceSelector#isSwitchable}.
240 return false;
241 }
242
243 /**
244 * Schedules the next tick invocation.
245 */
246 private void scheduleNextTick() {
247 mHandler.postDelayed(mTicker, TICK_FREQUENCY);
248 }
249
250 /**
251 * Performs the set of tasks that needs to be executed on polling basis.
252 * TODO(gilad): Check for stale pending calls that may need to be terminated etc, see
253 * mNewOutgoingCalls and mPendingOutgoingCalls.
254 * TODO(gilad): Also intended to trigger the call switching/hand-off logic when applicable.
255 */
256 private void tick() {
257 // TODO(gilad): More here.
Santos Cordonc195e362014-02-11 17:05:31 -0800258 // TODO(santoscordon): Clear mCallServices if there exist no more new or pending outgoing
259 // calls.
Ben Gilad2313e622014-02-06 12:02:25 -0800260 }
261
262 /**
Ben Gilad0bf5b912014-01-28 17:55:57 -0800263 * Attempts to process the next new outgoing calls that have not yet been expired.
264 */
265 private void processNewOutgoingCalls() {
266 if (isNullOrEmpty(mCallServices) || isNullOrEmpty(mSelectors)) {
267 // At least one call service and one selector are required to process outgoing calls.
268 return;
269 }
270
271 if (!mNewOutgoingCalls.isEmpty()) {
272 Call call = mNewOutgoingCalls.iterator().next();
273 mNewOutgoingCalls.remove(call);
274 mPendingOutgoingCalls.add(call);
275
276 // Specifically only attempt to place one call at a time such that call services
277 // can be freed from needing to deal with concurrent requests.
278 processNewOutgoingCall(call);
Ben Gilad0407fb22014-01-09 16:18:41 -0800279 }
Ben Gilad9f2bed32013-12-12 17:43:26 -0800280 }
281
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800282 /**
Ben Giladb59769e2014-01-16 11:41:10 -0800283 * Attempts to place the specified call.
284 *
Ben Gilad0bf5b912014-01-28 17:55:57 -0800285 * @param call The call to place.
Ben Giladb59769e2014-01-16 11:41:10 -0800286 */
Ben Gilad0bf5b912014-01-28 17:55:57 -0800287 private void processNewOutgoingCall(Call call) {
Ben Gilad0bf5b912014-01-28 17:55:57 -0800288 Preconditions.checkNotNull(mCallServices);
289 Preconditions.checkNotNull(mSelectors);
Ben Giladb59769e2014-01-16 11:41:10 -0800290
Ben Gilad0bf5b912014-01-28 17:55:57 -0800291 // Convert to (duplicate-free) list to aid index-based iteration, see the comment under
292 // setSelectors regarding using LinkedHashSet instead.
293 List<ICallServiceSelector> selectors = Lists.newArrayList();
294 selectors.addAll(mSelectors);
295
Santos Cordon681663d2014-01-30 04:32:15 -0800296 mOutgoingCallsManager.placeCall(call, mCallServices, selectors);
Ben Giladb59769e2014-01-16 11:41:10 -0800297 }
298
299 /**
300 * Determines whether or not the specified collection is either null or empty.
301 *
302 * @param collection Either null or the collection object to be evaluated.
303 * @return True if the collection is null or empty.
304 */
305 @SuppressWarnings("rawtypes")
306 private boolean isNullOrEmpty(Collection collection) {
307 return collection == null || collection.isEmpty();
308 }
Ben Gilad9f2bed32013-12-12 17:43:26 -0800309}