blob: d2c5bf9bc3edaa7ca1285673da20fad19182ebcf [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 /**
Santos Cordonc195e362014-02-11 17:05:31 -080080 * The set of currently available call service implementations, see
81 * {@link CallServiceRepository}. Populated after a lookup for call services as part of
82 * {@link #placeCall}. It is cleared periodically when there are no more new or pending outgoing
83 * calls.
Ben Gilad03292d42014-01-16 15:06:16 -080084 */
Santos Cordonc195e362014-02-11 17:05:31 -080085 private Set<CallServiceWrapper> mCallServices;
Ben Giladb59769e2014-01-16 11:41:10 -080086
Ben Gilad03292d42014-01-16 15:06:16 -080087 /**
88 * The set of currently available call-service-selector implementations,
Santos Cordonc195e362014-02-11 17:05:31 -080089 * see {@link CallServiceSelectorRepository}.
Ben Gilad03292d42014-01-16 15:06:16 -080090 * TODO(gilad): Null out once the active-call count goes to zero.
91 */
92 private Set<ICallServiceSelector> mSelectors;
Ben Gilad0407fb22014-01-09 16:18:41 -080093
Santos Cordon681663d2014-01-30 04:32:15 -080094 /**
Santos Cordonc195e362014-02-11 17:05:31 -080095 * The current lookup-cycle ID used with the repositories. Incremented with each invocation
96 * of {@link #placeCall} and passed to the repositories via initiateLookup().
97 */
98 private int mLookupId = 0;
99
100 /**
Santos Cordon681663d2014-01-30 04:32:15 -0800101 * Persists the specified parameters and initializes Switchboard.
102 */
Santos Cordon6242b132014-02-07 16:24:42 -0800103 Switchboard(CallsManager callsManager) {
104 mCallsManager = callsManager;
Santos Cordon681663d2014-01-30 04:32:15 -0800105 mOutgoingCallsManager = new OutgoingCallsManager(this);
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800106 mIncomingCallsManager = new IncomingCallsManager(this);
Santos Cordonc195e362014-02-11 17:05:31 -0800107 mCallServiceRepository = new CallServiceRepository(this, mOutgoingCallsManager);
108 mSelectorRepository = new CallServiceSelectorRepository(this);
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) {
Evan Charlton0958f532014-01-10 16:58:02 -0800119 ThreadUtil.checkOnMainThread();
Ben Giladb59769e2014-01-16 11:41:10 -0800120
Sailesh Nepalb6141ae2014-02-18 08:45:26 -0800121 mNewOutgoingCalls.add(call);
Ben Giladb59769e2014-01-16 11:41:10 -0800122
Santos Cordonc195e362014-02-11 17:05:31 -0800123 // We initialize a lookup every time because between calls the set of available call
124 // services can change between calls.
Sailesh Nepalb6141ae2014-02-18 08:45:26 -0800125 mLookupId++;
Santos Cordonc195e362014-02-11 17:05:31 -0800126 mCallServiceRepository.initiateLookup(mLookupId);
127 mSelectorRepository.initiateLookup(mLookupId);
Ben Gilad0407fb22014-01-09 16:18:41 -0800128 }
Ben Gilad9f2bed32013-12-12 17:43:26 -0800129
Ben Gilad0407fb22014-01-09 16:18:41 -0800130 /**
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800131 * Confirms with incoming call manager that an incoming call exists for the specified call
132 * service and call token. The incoming call manager will invoke either
133 * {@link #handleSuccessfulIncomingCall} or {@link #handleFailedIncomingCall} depending
134 * on the result.
135 *
136 * @param call The call object.
Ben Giladc5b22692014-02-18 20:03:22 -0800137 * @param descriptor The relevant call-service descriptor.
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800138 * @param callToken The token used by the call service to identify the incoming call.
139 */
Ben Giladc5b22692014-02-18 20:03:22 -0800140 void confirmIncomingCall(Call call, CallServiceDescriptor descriptor, String callToken) {
141 CallServiceWrapper callService = mCallServiceRepository.getCallService(descriptor);
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800142 mIncomingCallsManager.confirmIncomingCall(call, callService, callToken);
143 }
144
145 /**
Ben Gilad0bf5b912014-01-28 17:55:57 -0800146 * Persists the specified set of call services and attempts to place any pending outgoing
Santos Cordonc195e362014-02-11 17:05:31 -0800147 * calls. Intended to be invoked by {@link CallServiceRepository} exclusively.
Ben Gilad0407fb22014-01-09 16:18:41 -0800148 *
Ben Gilad03292d42014-01-16 15:06:16 -0800149 * @param callServices The potentially-partial set of call services. Partial since the lookup
150 * process is time-boxed, such that some providers/call-services may be slow to respond and
151 * hence effectively omitted from the specified list.
Ben Gilad0407fb22014-01-09 16:18:41 -0800152 */
Santos Cordonc195e362014-02-11 17:05:31 -0800153 void setCallServices(Set<CallServiceWrapper> callServices) {
Evan Charlton0958f532014-01-10 16:58:02 -0800154 ThreadUtil.checkOnMainThread();
Ben Gilad0407fb22014-01-09 16:18:41 -0800155
Ben Giladb59769e2014-01-16 11:41:10 -0800156 mCallServices = callServices;
Ben Gilad0bf5b912014-01-28 17:55:57 -0800157 processNewOutgoingCalls();
Ben Giladb59769e2014-01-16 11:41:10 -0800158 }
159
160 /**
161 * Persists the specified list of selectors and attempts to connect any pending outgoing
Santos Cordonc195e362014-02-11 17:05:31 -0800162 * calls. Intended to be invoked by {@link CallServiceSelectorRepository} exclusively.
Ben Giladb59769e2014-01-16 11:41:10 -0800163 *
Ben Gilad03292d42014-01-16 15:06:16 -0800164 * @param selectors The potentially-partial set of selectors. Partial since the lookup
165 * procedure is time-boxed such that some selectors may be slow to respond and hence
166 * effectively omitted from the specified set.
Ben Giladb59769e2014-01-16 11:41:10 -0800167 */
Ben Gilad03292d42014-01-16 15:06:16 -0800168 void setSelectors(Set<ICallServiceSelector> selectors) {
Santos Cordonc195e362014-02-11 17:05:31 -0800169 // TODO(santoscordon): This should take in CallServiceSelectorWrapper instead of the direct
170 // ICallServiceSelector implementation. Copy what we have for CallServiceWrapper.
Ben Giladb59769e2014-01-16 11:41:10 -0800171 ThreadUtil.checkOnMainThread();
172
Ben Gilad134cf092014-01-16 18:26:12 -0800173 // TODO(gilad): Add logic to include the built-in selectors (e.g. for dealing with
174 // emergency calls) and order the entire set prior to the assignment below. If the
175 // built-in selectors can be implemented in a manner that does not require binding,
176 // that's probably preferred. May want to use a LinkedHashSet for the sorted set.
Ben Giladb59769e2014-01-16 11:41:10 -0800177 mSelectors = selectors;
Ben Gilad0bf5b912014-01-28 17:55:57 -0800178 processNewOutgoingCalls();
Ben Giladb59769e2014-01-16 11:41:10 -0800179 }
180
181 /**
Ben Gilad0bf5b912014-01-28 17:55:57 -0800182 * Handles the case where an outgoing call has been successfully placed,
183 * see {@link OutgoingCallProcessor}.
Ben Giladb59769e2014-01-16 11:41:10 -0800184 */
Ben Gilad0bf5b912014-01-28 17:55:57 -0800185 void handleSuccessfulOutgoingCall(Call call) {
Santos Cordon681663d2014-01-30 04:32:15 -0800186 mCallsManager.handleSuccessfulOutgoingCall(call);
Ben Gilad0bf5b912014-01-28 17:55:57 -0800187
188 // Process additional (new) calls, if any.
189 processNewOutgoingCalls();
190 }
191
192 /**
193 * Handles the case where an outgoing call could not be proceed by any of the
194 * selector/call-service implementations, see {@link OutgoingCallProcessor}.
195 */
196 void handleFailedOutgoingCall(Call call) {
197 // TODO(gilad): More here.
198
199 // Process additional (new) calls, if any.
200 processNewOutgoingCalls();
201 }
202
203 /**
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800204 * Handles the case where we received confirmation of an incoming call. Hands the resulting
205 * call to {@link CallsManager} as the final step in the incoming sequence. At that point,
206 * {@link CallsManager} should bring up the incoming-call UI.
207 */
208 void handleSuccessfulIncomingCall(Call call) {
209 mCallsManager.handleSuccessfulIncomingCall(call);
210 }
211
212 /**
213 * Handles the case where we failed to confirm an incoming call after receiving an incoming-call
214 * intent via {@link TelecommReceiver}.
215 *
216 * @param call The call.
217 */
218 void handleFailedIncomingCall(Call call) {
219 // At the moment there is nothing to do if an incoming call is not confirmed. We may at a
220 // future date bind to the in-call app optimistically during the incoming-call sequence and
221 // this method could tell {@link CallsManager} to unbind from the in-call app if the
222 // incoming call was not confirmed. It's worth keeping this method for parity with the
223 // outgoing call sequence.
224 }
225
226 /**
Ben Gilad2313e622014-02-06 12:02:25 -0800227 * @return True if ticking should continue (or be resumed) and false otherwise.
228 */
229 private boolean isTicking() {
230 // TODO(gilad): return true every time at least one outgoing call is pending (i.e. waiting
231 // to be connected by a call service) and also when at least one active call is switch-able
232 // between call services, see {@link ICallServiceSelector#isSwitchable}.
233 return false;
234 }
235
236 /**
237 * Schedules the next tick invocation.
238 */
239 private void scheduleNextTick() {
240 mHandler.postDelayed(mTicker, TICK_FREQUENCY);
241 }
242
243 /**
244 * Performs the set of tasks that needs to be executed on polling basis.
245 * TODO(gilad): Check for stale pending calls that may need to be terminated etc, see
246 * mNewOutgoingCalls and mPendingOutgoingCalls.
247 * TODO(gilad): Also intended to trigger the call switching/hand-off logic when applicable.
248 */
249 private void tick() {
250 // TODO(gilad): More here.
Santos Cordonc195e362014-02-11 17:05:31 -0800251 // TODO(santoscordon): Clear mCallServices if there exist no more new or pending outgoing
252 // calls.
Ben Gilad2313e622014-02-06 12:02:25 -0800253 }
254
255 /**
Ben Gilad0bf5b912014-01-28 17:55:57 -0800256 * Attempts to process the next new outgoing calls that have not yet been expired.
257 */
258 private void processNewOutgoingCalls() {
259 if (isNullOrEmpty(mCallServices) || isNullOrEmpty(mSelectors)) {
260 // At least one call service and one selector are required to process outgoing calls.
261 return;
262 }
263
264 if (!mNewOutgoingCalls.isEmpty()) {
265 Call call = mNewOutgoingCalls.iterator().next();
266 mNewOutgoingCalls.remove(call);
267 mPendingOutgoingCalls.add(call);
268
269 // Specifically only attempt to place one call at a time such that call services
270 // can be freed from needing to deal with concurrent requests.
271 processNewOutgoingCall(call);
Ben Gilad0407fb22014-01-09 16:18:41 -0800272 }
Ben Gilad9f2bed32013-12-12 17:43:26 -0800273 }
274
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800275 /**
Ben Giladb59769e2014-01-16 11:41:10 -0800276 * Attempts to place the specified call.
277 *
Ben Gilad0bf5b912014-01-28 17:55:57 -0800278 * @param call The call to place.
Ben Giladb59769e2014-01-16 11:41:10 -0800279 */
Ben Gilad0bf5b912014-01-28 17:55:57 -0800280 private void processNewOutgoingCall(Call call) {
Ben Gilad0bf5b912014-01-28 17:55:57 -0800281 Preconditions.checkNotNull(mCallServices);
282 Preconditions.checkNotNull(mSelectors);
Ben Giladb59769e2014-01-16 11:41:10 -0800283
Ben Gilad0bf5b912014-01-28 17:55:57 -0800284 // Convert to (duplicate-free) list to aid index-based iteration, see the comment under
285 // setSelectors regarding using LinkedHashSet instead.
286 List<ICallServiceSelector> selectors = Lists.newArrayList();
287 selectors.addAll(mSelectors);
288
Santos Cordon681663d2014-01-30 04:32:15 -0800289 mOutgoingCallsManager.placeCall(call, mCallServices, selectors);
Ben Giladb59769e2014-01-16 11:41:10 -0800290 }
291
292 /**
293 * Determines whether or not the specified collection is either null or empty.
294 *
295 * @param collection Either null or the collection object to be evaluated.
296 * @return True if the collection is null or empty.
297 */
298 @SuppressWarnings("rawtypes")
299 private boolean isNullOrEmpty(Collection collection) {
300 return collection == null || collection.isEmpty();
301 }
Ben Gilad9f2bed32013-12-12 17:43:26 -0800302}