blob: 6afadb8e1d39133cec755857ac9177a49d15e8cb [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
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
Sailesh Nepalb6141ae2014-02-18 08:45:26 -0800120 mNewOutgoingCalls.add(call);
Ben Giladb59769e2014-01-16 11:41:10 -0800121
Santos Cordonc195e362014-02-11 17:05:31 -0800122 // We initialize a lookup every time because between calls the set of available call
123 // services can change between calls.
Sailesh Nepalb6141ae2014-02-18 08:45:26 -0800124 mLookupId++;
Santos Cordonc195e362014-02-11 17:05:31 -0800125 mCallServiceRepository.initiateLookup(mLookupId);
126 mSelectorRepository.initiateLookup(mLookupId);
Ben Gilad0407fb22014-01-09 16:18:41 -0800127 }
Ben Gilad9f2bed32013-12-12 17:43:26 -0800128
Ben Gilad0407fb22014-01-09 16:18:41 -0800129 /**
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800130 * Confirms with incoming call manager that an incoming call exists for the specified call
131 * service and call token. The incoming call manager will invoke either
132 * {@link #handleSuccessfulIncomingCall} or {@link #handleFailedIncomingCall} depending
133 * on the result.
134 *
135 * @param call The call object.
Ben Giladc5b22692014-02-18 20:03:22 -0800136 * @param descriptor The relevant call-service descriptor.
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800137 * @param callToken The token used by the call service to identify the incoming call.
138 */
Ben Giladc5b22692014-02-18 20:03:22 -0800139 void confirmIncomingCall(Call call, CallServiceDescriptor descriptor, String callToken) {
140 CallServiceWrapper callService = mCallServiceRepository.getCallService(descriptor);
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800141 mIncomingCallsManager.confirmIncomingCall(call, callService, callToken);
142 }
143
144 /**
Ben Gilad0bf5b912014-01-28 17:55:57 -0800145 * Persists the specified set of call services and attempts to place any pending outgoing
Santos Cordonc195e362014-02-11 17:05:31 -0800146 * calls. Intended to be invoked by {@link CallServiceRepository} exclusively.
Ben Gilad0407fb22014-01-09 16:18:41 -0800147 *
Ben Gilad03292d42014-01-16 15:06:16 -0800148 * @param callServices The potentially-partial set of call services. Partial since the lookup
149 * process is time-boxed, such that some providers/call-services may be slow to respond and
150 * hence effectively omitted from the specified list.
Ben Gilad0407fb22014-01-09 16:18:41 -0800151 */
Santos Cordonc195e362014-02-11 17:05:31 -0800152 void setCallServices(Set<CallServiceWrapper> callServices) {
Evan Charlton0958f532014-01-10 16:58:02 -0800153 ThreadUtil.checkOnMainThread();
Ben Gilad0407fb22014-01-09 16:18:41 -0800154
Ben Giladb59769e2014-01-16 11:41:10 -0800155 mCallServices = callServices;
Ben Gilad0bf5b912014-01-28 17:55:57 -0800156 processNewOutgoingCalls();
Ben Giladb59769e2014-01-16 11:41:10 -0800157 }
158
159 /**
160 * Persists the specified list of selectors and attempts to connect any pending outgoing
Santos Cordonc195e362014-02-11 17:05:31 -0800161 * calls. Intended to be invoked by {@link CallServiceSelectorRepository} exclusively.
Ben Giladb59769e2014-01-16 11:41:10 -0800162 *
Ben Gilad03292d42014-01-16 15:06:16 -0800163 * @param selectors The potentially-partial set of selectors. Partial since the lookup
164 * procedure is time-boxed such that some selectors may be slow to respond and hence
165 * effectively omitted from the specified set.
Ben Giladb59769e2014-01-16 11:41:10 -0800166 */
Ben Gilad03292d42014-01-16 15:06:16 -0800167 void setSelectors(Set<ICallServiceSelector> selectors) {
Santos Cordonc195e362014-02-11 17:05:31 -0800168 // TODO(santoscordon): This should take in CallServiceSelectorWrapper instead of the direct
169 // ICallServiceSelector implementation. Copy what we have for CallServiceWrapper.
Ben Giladb59769e2014-01-16 11:41:10 -0800170 ThreadUtil.checkOnMainThread();
171
Ben Gilad134cf092014-01-16 18:26:12 -0800172 // TODO(gilad): Add logic to include the built-in selectors (e.g. for dealing with
173 // emergency calls) and order the entire set prior to the assignment below. If the
174 // built-in selectors can be implemented in a manner that does not require binding,
175 // that's probably preferred. May want to use a LinkedHashSet for the sorted set.
Ben Giladb59769e2014-01-16 11:41:10 -0800176 mSelectors = selectors;
Ben Gilad0bf5b912014-01-28 17:55:57 -0800177 processNewOutgoingCalls();
Ben Giladb59769e2014-01-16 11:41:10 -0800178 }
179
180 /**
Ben Gilad0bf5b912014-01-28 17:55:57 -0800181 * Handles the case where an outgoing call has been successfully placed,
182 * see {@link OutgoingCallProcessor}.
Ben Giladb59769e2014-01-16 11:41:10 -0800183 */
Ben Gilad0bf5b912014-01-28 17:55:57 -0800184 void handleSuccessfulOutgoingCall(Call call) {
Santos Cordon681663d2014-01-30 04:32:15 -0800185 mCallsManager.handleSuccessfulOutgoingCall(call);
Ben Gilad0bf5b912014-01-28 17:55:57 -0800186
187 // Process additional (new) calls, if any.
188 processNewOutgoingCalls();
189 }
190
191 /**
192 * Handles the case where an outgoing call could not be proceed by any of the
193 * selector/call-service implementations, see {@link OutgoingCallProcessor}.
194 */
195 void handleFailedOutgoingCall(Call call) {
196 // TODO(gilad): More here.
197
198 // Process additional (new) calls, if any.
199 processNewOutgoingCalls();
200 }
201
202 /**
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800203 * Handles the case where we received confirmation of an incoming call. Hands the resulting
204 * call to {@link CallsManager} as the final step in the incoming sequence. At that point,
205 * {@link CallsManager} should bring up the incoming-call UI.
206 */
207 void handleSuccessfulIncomingCall(Call call) {
208 mCallsManager.handleSuccessfulIncomingCall(call);
209 }
210
211 /**
212 * Handles the case where we failed to confirm an incoming call after receiving an incoming-call
213 * intent via {@link TelecommReceiver}.
214 *
215 * @param call The call.
216 */
217 void handleFailedIncomingCall(Call call) {
218 // At the moment there is nothing to do if an incoming call is not confirmed. We may at a
219 // future date bind to the in-call app optimistically during the incoming-call sequence and
220 // this method could tell {@link CallsManager} to unbind from the in-call app if the
221 // incoming call was not confirmed. It's worth keeping this method for parity with the
222 // outgoing call sequence.
223 }
224
225 /**
Ben Gilad2313e622014-02-06 12:02:25 -0800226 * @return True if ticking should continue (or be resumed) and false otherwise.
227 */
228 private boolean isTicking() {
229 // TODO(gilad): return true every time at least one outgoing call is pending (i.e. waiting
230 // to be connected by a call service) and also when at least one active call is switch-able
231 // between call services, see {@link ICallServiceSelector#isSwitchable}.
232 return false;
233 }
234
235 /**
236 * Schedules the next tick invocation.
237 */
238 private void scheduleNextTick() {
239 mHandler.postDelayed(mTicker, TICK_FREQUENCY);
240 }
241
242 /**
243 * Performs the set of tasks that needs to be executed on polling basis.
244 * TODO(gilad): Check for stale pending calls that may need to be terminated etc, see
245 * mNewOutgoingCalls and mPendingOutgoingCalls.
246 * TODO(gilad): Also intended to trigger the call switching/hand-off logic when applicable.
247 */
248 private void tick() {
249 // TODO(gilad): More here.
Santos Cordonc195e362014-02-11 17:05:31 -0800250 // TODO(santoscordon): Clear mCallServices if there exist no more new or pending outgoing
251 // calls.
Ben Gilad2313e622014-02-06 12:02:25 -0800252 }
253
254 /**
Ben Gilad0bf5b912014-01-28 17:55:57 -0800255 * Attempts to process the next new outgoing calls that have not yet been expired.
256 */
257 private void processNewOutgoingCalls() {
258 if (isNullOrEmpty(mCallServices) || isNullOrEmpty(mSelectors)) {
259 // At least one call service and one selector are required to process outgoing calls.
260 return;
261 }
262
263 if (!mNewOutgoingCalls.isEmpty()) {
264 Call call = mNewOutgoingCalls.iterator().next();
265 mNewOutgoingCalls.remove(call);
266 mPendingOutgoingCalls.add(call);
267
268 // Specifically only attempt to place one call at a time such that call services
269 // can be freed from needing to deal with concurrent requests.
270 processNewOutgoingCall(call);
Ben Gilad0407fb22014-01-09 16:18:41 -0800271 }
Ben Gilad9f2bed32013-12-12 17:43:26 -0800272 }
273
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800274 /**
Ben Giladb59769e2014-01-16 11:41:10 -0800275 * Attempts to place the specified call.
276 *
Ben Gilad0bf5b912014-01-28 17:55:57 -0800277 * @param call The call to place.
Ben Giladb59769e2014-01-16 11:41:10 -0800278 */
Ben Gilad0bf5b912014-01-28 17:55:57 -0800279 private void processNewOutgoingCall(Call call) {
Ben Gilad0bf5b912014-01-28 17:55:57 -0800280 Preconditions.checkNotNull(mCallServices);
281 Preconditions.checkNotNull(mSelectors);
Ben Giladb59769e2014-01-16 11:41:10 -0800282
Ben Gilad0bf5b912014-01-28 17:55:57 -0800283 // Convert to (duplicate-free) list to aid index-based iteration, see the comment under
284 // setSelectors regarding using LinkedHashSet instead.
285 List<ICallServiceSelector> selectors = Lists.newArrayList();
286 selectors.addAll(mSelectors);
287
Santos Cordon681663d2014-01-30 04:32:15 -0800288 mOutgoingCallsManager.placeCall(call, mCallServices, selectors);
Ben Giladb59769e2014-01-16 11:41:10 -0800289 }
290
291 /**
292 * Determines whether or not the specified collection is either null or empty.
293 *
294 * @param collection Either null or the collection object to be evaluated.
295 * @return True if the collection is null or empty.
296 */
297 @SuppressWarnings("rawtypes")
298 private boolean isNullOrEmpty(Collection collection) {
299 return collection == null || collection.isEmpty();
300 }
Ben Gilad9f2bed32013-12-12 17:43:26 -0800301}