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