blob: 8661e51aa33c129157d9287efcf1c297cfeb3c8d [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 Cordon493e8f22014-02-19 03:15:12 -080027import android.util.Log;
Santos Cordon8e8b8d22013-12-19 14:14:05 -080028
Ben Giladb59769e2014-01-16 11:41:10 -080029import java.util.Collection;
Ben Gilad9f2bed32013-12-12 17:43:26 -080030import java.util.List;
Ben Gilad0407fb22014-01-09 16:18:41 -080031import java.util.Set;
Ben Gilad9f2bed32013-12-12 17:43:26 -080032
Santos Cordon8e8b8d22013-12-19 14:14:05 -080033/**
Santos Cordonc195e362014-02-11 17:05:31 -080034 * Switchboard is responsible for (1) gathering the {@link CallServiceWrapper}s and
35 * {@link ICallServiceSelector}s through which to place outgoing calls, (2) starting outgoing calls
36 * (via {@link OutgoingCallsManager} and (3) switching active calls between call services.
Santos Cordon8e8b8d22013-12-19 14:14:05 -080037 */
Ben Giladd17443c2014-01-06 11:04:15 -080038final class Switchboard {
Ben Gilad2495d572014-01-09 17:26:19 -080039
Santos Cordon493e8f22014-02-19 03:15:12 -080040 private final static String TAG = Switchboard.class.getSimpleName();
Ben Gilad2313e622014-02-06 12:02:25 -080041 /**
42 * The frequency of invoking tick in milliseconds.
43 * TODO(gilad): May require tuning.
44 */
45 private final static int TICK_FREQUENCY = 250;
Ben Giladb59769e2014-01-16 11:41:10 -080046
Santos Cordon681663d2014-01-30 04:32:15 -080047 private final CallsManager mCallsManager;
Ben Gilad2313e622014-02-06 12:02:25 -080048
Santos Cordon681663d2014-01-30 04:32:15 -080049 /** Used to place outgoing calls. */
50 private final OutgoingCallsManager mOutgoingCallsManager;
51
Santos Cordon493e8f22014-02-19 03:15:12 -080052 /** Used to retrieve incoming call details. */
Santos Cordon80d9bdc2014-02-13 18:28:46 -080053 private final IncomingCallsManager mIncomingCallsManager;
54
Santos Cordonc195e362014-02-11 17:05:31 -080055 private final CallServiceRepository mCallServiceRepository;
Santos Cordon681663d2014-01-30 04:32:15 -080056
Santos Cordonc195e362014-02-11 17:05:31 -080057 private final CallServiceSelectorRepository mSelectorRepository;
Ben Gilad2313e622014-02-06 12:02:25 -080058
59 /** Used to schedule tasks on the main (UI) thread. */
60 private final Handler mHandler = new Handler(Looper.getMainLooper());
61
62 /**
63 * Executes a single tick task and potentially schedules the next such that polling continues
64 * as long as necessary.
65 * NOTE(gilad): by design no two tick invocations should ever overlap.
66 */
67 private final Runnable mTicker = new Runnable() {
68 @Override
69 public void run() {
70 tick();
71 if (isTicking()) {
72 scheduleNextTick();
73 }
74 }
75 };
76
77 private final Set<Call> mNewOutgoingCalls = Sets.newLinkedHashSet();
78
79 private final Set<Call> mPendingOutgoingCalls = Sets.newLinkedHashSet();
Ben Giladb59769e2014-01-16 11:41:10 -080080
Ben Gilad03292d42014-01-16 15:06:16 -080081 /**
Ben Giladbb167cd2014-02-25 16:24:05 -080082 * The set of currently available call-service implementations, see
Ben Gilad37587232014-02-19 16:03:44 -080083 * {@link CallServiceRepository}. Populated during call-service lookup cycles as part of the
84 * {@link #placeOutgoingCall} flow and cleared upon zero-remaining new/pending outgoing calls.
Ben Gilad03292d42014-01-16 15:06:16 -080085 */
Santos Cordonc195e362014-02-11 17:05:31 -080086 private Set<CallServiceWrapper> mCallServices;
Ben Giladb59769e2014-01-16 11:41:10 -080087
Ben Gilad03292d42014-01-16 15:06:16 -080088 /**
89 * The set of currently available call-service-selector implementations,
Santos Cordonc195e362014-02-11 17:05:31 -080090 * see {@link CallServiceSelectorRepository}.
Ben Gilad03292d42014-01-16 15:06:16 -080091 * TODO(gilad): Null out once the active-call count goes to zero.
92 */
93 private Set<ICallServiceSelector> mSelectors;
Ben Gilad0407fb22014-01-09 16:18:41 -080094
Santos Cordon681663d2014-01-30 04:32:15 -080095 /**
Santos Cordonc195e362014-02-11 17:05:31 -080096 * The current lookup-cycle ID used with the repositories. Incremented with each invocation
Ben Gilad37587232014-02-19 16:03:44 -080097 * of {@link #placeOutgoingCall} and passed to the repositories via initiateLookup().
Santos Cordonc195e362014-02-11 17:05:31 -080098 */
99 private int mLookupId = 0;
100
101 /**
Santos Cordon681663d2014-01-30 04:32:15 -0800102 * Persists the specified parameters and initializes Switchboard.
103 */
Santos Cordon6242b132014-02-07 16:24:42 -0800104 Switchboard(CallsManager callsManager) {
105 mCallsManager = callsManager;
Santos Cordon681663d2014-01-30 04:32:15 -0800106 mOutgoingCallsManager = new OutgoingCallsManager(this);
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800107 mIncomingCallsManager = new IncomingCallsManager(this);
Santos Cordonc195e362014-02-11 17:05:31 -0800108 mSelectorRepository = new CallServiceSelectorRepository(this);
Santos Cordon493e8f22014-02-19 03:15:12 -0800109 mCallServiceRepository =
110 new CallServiceRepository(this, mOutgoingCallsManager, mIncomingCallsManager);
Santos Cordon681663d2014-01-30 04:32:15 -0800111 }
Ben Giladd17443c2014-01-06 11:04:15 -0800112
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800113 /**
Santos Cordonc195e362014-02-11 17:05:31 -0800114 * Starts the process of placing an outgoing call by searching for available call services
115 * through which the call can be placed. After a lookup for those services completes, execution
116 * returns to {@link #setCallServices} where the process of placing the call continues.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800117 *
Ben Gilad13329fd2014-02-11 17:20:29 -0800118 * @param call The yet-to-be-connected outgoing-call object.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800119 */
Santos Cordonc195e362014-02-11 17:05:31 -0800120 void placeOutgoingCall(Call call) {
Evan Charlton0958f532014-01-10 16:58:02 -0800121 ThreadUtil.checkOnMainThread();
Ben Giladb59769e2014-01-16 11:41:10 -0800122
Ben Giladbb167cd2014-02-25 16:24:05 -0800123 // Reset prior to initiating the next lookup. One case to consider is (1) placeOutgoingCall
124 // is invoked with call A, (2) the call-service lookup completes, but the one for selectors
125 // does not, (3) placeOutgoingCall is invoked again with call B, (4) mCallServices below is
126 // reset, (5) the selector lookup completes but the call-services are missing. This should
127 // be okay since the call-service lookup completed. Specifically the already-available call
128 // services are cached and will be provided in response to the second lookup cycle.
129 mCallServices = null;
130 mSelectors = null;
131
Sailesh Nepalb6141ae2014-02-18 08:45:26 -0800132 mNewOutgoingCalls.add(call);
Ben Giladb59769e2014-01-16 11:41:10 -0800133
Ben Giladebd9b662014-02-19 16:03:44 -0800134 // Initiate a lookup every time to account for newly-installed apps and/or updated settings.
Sailesh Nepalb6141ae2014-02-18 08:45:26 -0800135 mLookupId++;
Santos Cordonc195e362014-02-11 17:05:31 -0800136 mCallServiceRepository.initiateLookup(mLookupId);
137 mSelectorRepository.initiateLookup(mLookupId);
Ben Gilad0407fb22014-01-09 16:18:41 -0800138 }
Ben Gilad9f2bed32013-12-12 17:43:26 -0800139
Ben Gilad0407fb22014-01-09 16:18:41 -0800140 /**
Santos Cordon493e8f22014-02-19 03:15:12 -0800141 * Retrieves details about the incoming call through the incoming call manager. The incoming
142 * call manager will invoke either {@link #handleSuccessfulIncomingCall} or
143 * {@link #handleFailedIncomingCall} depending on the result of the retrieval.
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800144 *
145 * @param call The call object.
Ben Giladc5b22692014-02-18 20:03:22 -0800146 * @param descriptor The relevant call-service descriptor.
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800147 */
Santos Cordon493e8f22014-02-19 03:15:12 -0800148 void retrieveIncomingCall(Call call, CallServiceDescriptor descriptor) {
149 Log.d(TAG, "retrieveIncomingCall");
Ben Giladc5b22692014-02-18 20:03:22 -0800150 CallServiceWrapper callService = mCallServiceRepository.getCallService(descriptor);
Santos Cordon493e8f22014-02-19 03:15:12 -0800151 call.setCallService(callService);
152 mIncomingCallsManager.retrieveIncomingCall(call);
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800153 }
154
155 /**
Ben Gilad0bf5b912014-01-28 17:55:57 -0800156 * Persists the specified set of call services and attempts to place any pending outgoing
Santos Cordonc195e362014-02-11 17:05:31 -0800157 * calls. Intended to be invoked by {@link CallServiceRepository} exclusively.
Ben Gilad0407fb22014-01-09 16:18:41 -0800158 *
Ben Gilad03292d42014-01-16 15:06:16 -0800159 * @param callServices The potentially-partial set of call services. Partial since the lookup
160 * process is time-boxed, such that some providers/call-services may be slow to respond and
161 * hence effectively omitted from the specified list.
Ben Gilad0407fb22014-01-09 16:18:41 -0800162 */
Santos Cordonc195e362014-02-11 17:05:31 -0800163 void setCallServices(Set<CallServiceWrapper> callServices) {
Evan Charlton0958f532014-01-10 16:58:02 -0800164 ThreadUtil.checkOnMainThread();
Ben Gilad0407fb22014-01-09 16:18:41 -0800165
Ben Giladb59769e2014-01-16 11:41:10 -0800166 mCallServices = callServices;
Ben Gilad0bf5b912014-01-28 17:55:57 -0800167 processNewOutgoingCalls();
Ben Giladb59769e2014-01-16 11:41:10 -0800168 }
169
170 /**
171 * Persists the specified list of selectors and attempts to connect any pending outgoing
Santos Cordonc195e362014-02-11 17:05:31 -0800172 * calls. Intended to be invoked by {@link CallServiceSelectorRepository} exclusively.
Ben Giladb59769e2014-01-16 11:41:10 -0800173 *
Ben Gilad03292d42014-01-16 15:06:16 -0800174 * @param selectors The potentially-partial set of selectors. Partial since the lookup
175 * procedure is time-boxed such that some selectors may be slow to respond and hence
176 * effectively omitted from the specified set.
Ben Giladb59769e2014-01-16 11:41:10 -0800177 */
Ben Gilad03292d42014-01-16 15:06:16 -0800178 void setSelectors(Set<ICallServiceSelector> selectors) {
Santos Cordonc195e362014-02-11 17:05:31 -0800179 // TODO(santoscordon): This should take in CallServiceSelectorWrapper instead of the direct
180 // ICallServiceSelector implementation. Copy what we have for CallServiceWrapper.
Ben Giladb59769e2014-01-16 11:41:10 -0800181 ThreadUtil.checkOnMainThread();
182
Ben Gilad134cf092014-01-16 18:26:12 -0800183 // TODO(gilad): Add logic to include the built-in selectors (e.g. for dealing with
184 // emergency calls) and order the entire set prior to the assignment below. If the
185 // built-in selectors can be implemented in a manner that does not require binding,
186 // that's probably preferred. May want to use a LinkedHashSet for the sorted set.
Ben Giladb59769e2014-01-16 11:41:10 -0800187 mSelectors = selectors;
Ben Gilad0bf5b912014-01-28 17:55:57 -0800188 processNewOutgoingCalls();
Ben Giladb59769e2014-01-16 11:41:10 -0800189 }
190
191 /**
Ben Gilad0bf5b912014-01-28 17:55:57 -0800192 * Handles the case where an outgoing call has been successfully placed,
193 * see {@link OutgoingCallProcessor}.
Ben Giladb59769e2014-01-16 11:41:10 -0800194 */
Ben Gilad0bf5b912014-01-28 17:55:57 -0800195 void handleSuccessfulOutgoingCall(Call call) {
Santos Cordon681663d2014-01-30 04:32:15 -0800196 mCallsManager.handleSuccessfulOutgoingCall(call);
Ben Gilad0bf5b912014-01-28 17:55:57 -0800197
198 // Process additional (new) calls, if any.
199 processNewOutgoingCalls();
200 }
201
202 /**
203 * Handles the case where an outgoing call could not be proceed by any of the
204 * selector/call-service implementations, see {@link OutgoingCallProcessor}.
205 */
206 void handleFailedOutgoingCall(Call call) {
207 // TODO(gilad): More here.
208
209 // Process additional (new) calls, if any.
210 processNewOutgoingCalls();
211 }
212
213 /**
Santos Cordon493e8f22014-02-19 03:15:12 -0800214 * Handles the case where we successfully receive details of an incoming call. Hands the
215 * resulting call to {@link CallsManager} as the final step in the incoming sequence. At that
216 * point, {@link CallsManager} should bring up the incoming-call UI.
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800217 */
218 void handleSuccessfulIncomingCall(Call call) {
Santos Cordon493e8f22014-02-19 03:15:12 -0800219 Log.d(TAG, "handleSuccessfulIncomingCall");
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800220 mCallsManager.handleSuccessfulIncomingCall(call);
221 }
222
223 /**
Santos Cordon493e8f22014-02-19 03:15:12 -0800224 * Handles the case where we failed to retrieve an incoming call after receiving an incoming-call
225 * intent via {@link CallActivity}.
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800226 *
227 * @param call The call.
228 */
229 void handleFailedIncomingCall(Call call) {
Santos Cordon493e8f22014-02-19 03:15:12 -0800230 // Since we set the call service before calling into incoming-calls manager, we clear it for
231 // good measure if an error is reported.
232 call.clearCallService();
233
234 // At the moment there is nothing more to do if an incoming call is not retrieved. We may at
235 // a future date bind to the in-call app optimistically during the incoming-call sequence
236 // and this method could tell {@link CallsManager} to unbind from the in-call app if the
237 // incoming call was not retrieved.
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800238 }
239
240 /**
Ben Gilad2313e622014-02-06 12:02:25 -0800241 * @return True if ticking should continue (or be resumed) and false otherwise.
242 */
243 private boolean isTicking() {
244 // TODO(gilad): return true every time at least one outgoing call is pending (i.e. waiting
245 // to be connected by a call service) and also when at least one active call is switch-able
246 // between call services, see {@link ICallServiceSelector#isSwitchable}.
247 return false;
248 }
249
250 /**
251 * Schedules the next tick invocation.
252 */
253 private void scheduleNextTick() {
254 mHandler.postDelayed(mTicker, TICK_FREQUENCY);
255 }
256
257 /**
258 * Performs the set of tasks that needs to be executed on polling basis.
259 * TODO(gilad): Check for stale pending calls that may need to be terminated etc, see
260 * mNewOutgoingCalls and mPendingOutgoingCalls.
261 * TODO(gilad): Also intended to trigger the call switching/hand-off logic when applicable.
262 */
263 private void tick() {
264 // TODO(gilad): More here.
Santos Cordonc195e362014-02-11 17:05:31 -0800265 // TODO(santoscordon): Clear mCallServices if there exist no more new or pending outgoing
266 // calls.
Ben Gilad2313e622014-02-06 12:02:25 -0800267 }
268
269 /**
Ben Gilad0bf5b912014-01-28 17:55:57 -0800270 * Attempts to process the next new outgoing calls that have not yet been expired.
271 */
272 private void processNewOutgoingCalls() {
273 if (isNullOrEmpty(mCallServices) || isNullOrEmpty(mSelectors)) {
274 // At least one call service and one selector are required to process outgoing calls.
275 return;
276 }
277
278 if (!mNewOutgoingCalls.isEmpty()) {
279 Call call = mNewOutgoingCalls.iterator().next();
280 mNewOutgoingCalls.remove(call);
281 mPendingOutgoingCalls.add(call);
282
283 // Specifically only attempt to place one call at a time such that call services
284 // can be freed from needing to deal with concurrent requests.
285 processNewOutgoingCall(call);
Ben Gilad0407fb22014-01-09 16:18:41 -0800286 }
Ben Gilad9f2bed32013-12-12 17:43:26 -0800287 }
288
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800289 /**
Ben Giladb59769e2014-01-16 11:41:10 -0800290 * Attempts to place the specified call.
291 *
Ben Gilad0bf5b912014-01-28 17:55:57 -0800292 * @param call The call to place.
Ben Giladb59769e2014-01-16 11:41:10 -0800293 */
Ben Gilad0bf5b912014-01-28 17:55:57 -0800294 private void processNewOutgoingCall(Call call) {
Ben Gilad0bf5b912014-01-28 17:55:57 -0800295 Preconditions.checkNotNull(mCallServices);
296 Preconditions.checkNotNull(mSelectors);
Ben Giladb59769e2014-01-16 11:41:10 -0800297
Ben Gilad0bf5b912014-01-28 17:55:57 -0800298 // Convert to (duplicate-free) list to aid index-based iteration, see the comment under
299 // setSelectors regarding using LinkedHashSet instead.
300 List<ICallServiceSelector> selectors = Lists.newArrayList();
301 selectors.addAll(mSelectors);
302
Santos Cordon681663d2014-01-30 04:32:15 -0800303 mOutgoingCallsManager.placeCall(call, mCallServices, selectors);
Ben Giladb59769e2014-01-16 11:41:10 -0800304 }
305
306 /**
307 * Determines whether or not the specified collection is either null or empty.
308 *
309 * @param collection Either null or the collection object to be evaluated.
310 * @return True if the collection is null or empty.
311 */
312 @SuppressWarnings("rawtypes")
313 private boolean isNullOrEmpty(Collection collection) {
314 return collection == null || collection.isEmpty();
315 }
Ben Gilad9f2bed32013-12-12 17:43:26 -0800316}