blob: d4afb76b9d56157e07b9cd09f1c8d6f68547afee [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
23import android.content.Context;
Ben Gilad2313e622014-02-06 12:02:25 -080024import android.os.Handler;
25import android.os.Looper;
Santos Cordon8e8b8d22013-12-19 14:14:05 -080026import android.telecomm.ICallService;
Ben Giladb59769e2014-01-16 11:41:10 -080027import android.telecomm.ICallServiceSelector;
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/**
34 * Switchboard is responsible for (1) selecting the {@link ICallService} through which to make
35 * outgoing calls and (2) switching active calls between transports (each ICallService is
36 * considered a different transport type).
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
Ben Gilad2313e622014-02-06 12:02:25 -080040 /**
41 * The frequency of invoking tick in milliseconds.
42 * TODO(gilad): May require tuning.
43 */
44 private final static int TICK_FREQUENCY = 250;
Ben Giladb59769e2014-01-16 11:41:10 -080045
Santos Cordon681663d2014-01-30 04:32:15 -080046 private final CallsManager mCallsManager;
Ben Gilad2313e622014-02-06 12:02:25 -080047
Santos Cordon681663d2014-01-30 04:32:15 -080048 /** Used to place outgoing calls. */
49 private final OutgoingCallsManager mOutgoingCallsManager;
50
51 private CallServiceFinder mCallServiceFinder;
52
53 private CallServiceSelectorFinder mSelectorFinder;
Ben Gilad2313e622014-02-06 12:02:25 -080054
55 /** Used to schedule tasks on the main (UI) thread. */
56 private final Handler mHandler = new Handler(Looper.getMainLooper());
57
58 /**
59 * Executes a single tick task and potentially schedules the next such that polling continues
60 * as long as necessary.
61 * NOTE(gilad): by design no two tick invocations should ever overlap.
62 */
63 private final Runnable mTicker = new Runnable() {
64 @Override
65 public void run() {
66 tick();
67 if (isTicking()) {
68 scheduleNextTick();
69 }
70 }
71 };
72
73 private final Set<Call> mNewOutgoingCalls = Sets.newLinkedHashSet();
74
75 private final Set<Call> mPendingOutgoingCalls = Sets.newLinkedHashSet();
Ben Giladb59769e2014-01-16 11:41:10 -080076
Ben Gilad03292d42014-01-16 15:06:16 -080077 /**
78 * The set of currently available call-service implementations, see {@link CallServiceFinder}.
79 * TODO(gilad): Null out once the active-call count goes to zero.
80 */
81 private Set<ICallService> mCallServices;
Ben Giladb59769e2014-01-16 11:41:10 -080082
Ben Gilad03292d42014-01-16 15:06:16 -080083 /**
84 * The set of currently available call-service-selector implementations,
85 * see {@link CallServiceSelectorFinder}.
86 * TODO(gilad): Null out once the active-call count goes to zero.
87 */
88 private Set<ICallServiceSelector> mSelectors;
Ben Gilad0407fb22014-01-09 16:18:41 -080089
Santos Cordon681663d2014-01-30 04:32:15 -080090 /**
91 * Persists the specified parameters and initializes Switchboard.
92 */
Santos Cordon6242b132014-02-07 16:24:42 -080093 Switchboard(CallsManager callsManager) {
94 mCallsManager = callsManager;
Santos Cordon681663d2014-01-30 04:32:15 -080095 mOutgoingCallsManager = new OutgoingCallsManager(this);
96 mCallServiceFinder = new CallServiceFinder(this, mOutgoingCallsManager);
97 mSelectorFinder = new CallServiceSelectorFinder(this);
98 }
Ben Giladd17443c2014-01-06 11:04:15 -080099
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800100 /**
Ben Gilad8bdaa462014-02-05 12:53:19 -0800101 * Attempts to place an outgoing call to the specified handle.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800102 *
Ben Gilad13329fd2014-02-11 17:20:29 -0800103 * @param call The yet-to-be-connected outgoing-call object.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800104 * @param context The application context.
105 */
Ben Gilad13329fd2014-02-11 17:20:29 -0800106 void placeOutgoingCall(Call call, Context context) {
Evan Charlton0958f532014-01-10 16:58:02 -0800107 ThreadUtil.checkOnMainThread();
Ben Giladb59769e2014-01-16 11:41:10 -0800108
Ben Giladb59769e2014-01-16 11:41:10 -0800109 boolean bailout = false;
110 if (isNullOrEmpty(mCallServices)) {
Santos Cordon63aeb162014-02-10 09:20:40 -0800111 mCallServiceFinder.initiateLookup();
Ben Giladb59769e2014-01-16 11:41:10 -0800112 bailout = true;
113 }
114 if (isNullOrEmpty(mSelectors)) {
115 mSelectorFinder.initiateLookup(context);
116 bailout = true;
117 }
118
119 if (bailout) {
120 // Unable to process the call without either call service, selectors, or both.
121 // Store the call for deferred processing and bail out.
Ben Gilad0bf5b912014-01-28 17:55:57 -0800122 mNewOutgoingCalls.add(call);
Ben Giladb59769e2014-01-16 11:41:10 -0800123 return;
124 }
125
Ben Gilad0bf5b912014-01-28 17:55:57 -0800126 processNewOutgoingCall(call);
Ben Gilad0407fb22014-01-09 16:18:41 -0800127 }
Ben Gilad9f2bed32013-12-12 17:43:26 -0800128
Ben Gilad0407fb22014-01-09 16:18:41 -0800129 /**
Ben Gilad0bf5b912014-01-28 17:55:57 -0800130 * Persists the specified set of call services and attempts to place any pending outgoing
131 * calls. Intended to be invoked by {@link CallServiceFinder} exclusively.
Ben Gilad0407fb22014-01-09 16:18:41 -0800132 *
Ben Gilad03292d42014-01-16 15:06:16 -0800133 * @param callServices The potentially-partial set of call services. Partial since the lookup
134 * process is time-boxed, such that some providers/call-services may be slow to respond and
135 * hence effectively omitted from the specified list.
Ben Gilad0407fb22014-01-09 16:18:41 -0800136 */
Ben Gilad03292d42014-01-16 15:06:16 -0800137 void setCallServices(Set<ICallService> callServices) {
Evan Charlton0958f532014-01-10 16:58:02 -0800138 ThreadUtil.checkOnMainThread();
Ben Gilad0407fb22014-01-09 16:18:41 -0800139
Ben Giladb59769e2014-01-16 11:41:10 -0800140 mCallServices = callServices;
Ben Gilad0bf5b912014-01-28 17:55:57 -0800141 processNewOutgoingCalls();
Ben Giladb59769e2014-01-16 11:41:10 -0800142 }
143
144 /**
145 * Persists the specified list of selectors and attempts to connect any pending outgoing
Ben Gilad0bf5b912014-01-28 17:55:57 -0800146 * calls. Intended to be invoked by {@link CallServiceSelectorFinder} exclusively.
Ben Giladb59769e2014-01-16 11:41:10 -0800147 *
Ben Gilad03292d42014-01-16 15:06:16 -0800148 * @param selectors The potentially-partial set of selectors. Partial since the lookup
149 * procedure is time-boxed such that some selectors may be slow to respond and hence
150 * effectively omitted from the specified set.
Ben Giladb59769e2014-01-16 11:41:10 -0800151 */
Ben Gilad03292d42014-01-16 15:06:16 -0800152 void setSelectors(Set<ICallServiceSelector> selectors) {
Ben Giladb59769e2014-01-16 11:41:10 -0800153 ThreadUtil.checkOnMainThread();
154
Ben Gilad134cf092014-01-16 18:26:12 -0800155 // TODO(gilad): Add logic to include the built-in selectors (e.g. for dealing with
156 // emergency calls) and order the entire set prior to the assignment below. If the
157 // built-in selectors can be implemented in a manner that does not require binding,
158 // that's probably preferred. May want to use a LinkedHashSet for the sorted set.
Ben Giladb59769e2014-01-16 11:41:10 -0800159 mSelectors = selectors;
Ben Gilad0bf5b912014-01-28 17:55:57 -0800160 processNewOutgoingCalls();
Ben Giladb59769e2014-01-16 11:41:10 -0800161 }
162
163 /**
Ben Gilad0bf5b912014-01-28 17:55:57 -0800164 * Handles the case where an outgoing call has been successfully placed,
165 * see {@link OutgoingCallProcessor}.
Ben Giladb59769e2014-01-16 11:41:10 -0800166 */
Ben Gilad0bf5b912014-01-28 17:55:57 -0800167 void handleSuccessfulOutgoingCall(Call call) {
Santos Cordon681663d2014-01-30 04:32:15 -0800168 mCallsManager.handleSuccessfulOutgoingCall(call);
Ben Gilad0bf5b912014-01-28 17:55:57 -0800169
170 // Process additional (new) calls, if any.
171 processNewOutgoingCalls();
172 }
173
174 /**
175 * Handles the case where an outgoing call could not be proceed by any of the
176 * selector/call-service implementations, see {@link OutgoingCallProcessor}.
177 */
178 void handleFailedOutgoingCall(Call call) {
179 // TODO(gilad): More here.
180
181 // Process additional (new) calls, if any.
182 processNewOutgoingCalls();
183 }
184
185 /**
Ben Gilad2313e622014-02-06 12:02:25 -0800186 * @return True if ticking should continue (or be resumed) and false otherwise.
187 */
188 private boolean isTicking() {
189 // TODO(gilad): return true every time at least one outgoing call is pending (i.e. waiting
190 // to be connected by a call service) and also when at least one active call is switch-able
191 // between call services, see {@link ICallServiceSelector#isSwitchable}.
192 return false;
193 }
194
195 /**
196 * Schedules the next tick invocation.
197 */
198 private void scheduleNextTick() {
199 mHandler.postDelayed(mTicker, TICK_FREQUENCY);
200 }
201
202 /**
203 * Performs the set of tasks that needs to be executed on polling basis.
204 * TODO(gilad): Check for stale pending calls that may need to be terminated etc, see
205 * mNewOutgoingCalls and mPendingOutgoingCalls.
206 * TODO(gilad): Also intended to trigger the call switching/hand-off logic when applicable.
207 */
208 private void tick() {
209 // TODO(gilad): More here.
210 }
211
212 /**
Ben Gilad0bf5b912014-01-28 17:55:57 -0800213 * Attempts to process the next new outgoing calls that have not yet been expired.
214 */
215 private void processNewOutgoingCalls() {
216 if (isNullOrEmpty(mCallServices) || isNullOrEmpty(mSelectors)) {
217 // At least one call service and one selector are required to process outgoing calls.
218 return;
219 }
220
221 if (!mNewOutgoingCalls.isEmpty()) {
222 Call call = mNewOutgoingCalls.iterator().next();
223 mNewOutgoingCalls.remove(call);
224 mPendingOutgoingCalls.add(call);
225
226 // Specifically only attempt to place one call at a time such that call services
227 // can be freed from needing to deal with concurrent requests.
228 processNewOutgoingCall(call);
Ben Gilad0407fb22014-01-09 16:18:41 -0800229 }
Ben Gilad9f2bed32013-12-12 17:43:26 -0800230 }
231
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800232 /**
Ben Giladb59769e2014-01-16 11:41:10 -0800233 * Attempts to place the specified call.
234 *
Ben Gilad0bf5b912014-01-28 17:55:57 -0800235 * @param call The call to place.
Ben Giladb59769e2014-01-16 11:41:10 -0800236 */
Ben Gilad0bf5b912014-01-28 17:55:57 -0800237 private void processNewOutgoingCall(Call call) {
Ben Giladb59769e2014-01-16 11:41:10 -0800238
Ben Gilad0bf5b912014-01-28 17:55:57 -0800239 Preconditions.checkNotNull(mCallServices);
240 Preconditions.checkNotNull(mSelectors);
Ben Giladb59769e2014-01-16 11:41:10 -0800241
Ben Gilad0bf5b912014-01-28 17:55:57 -0800242 // Convert to (duplicate-free) list to aid index-based iteration, see the comment under
243 // setSelectors regarding using LinkedHashSet instead.
244 List<ICallServiceSelector> selectors = Lists.newArrayList();
245 selectors.addAll(mSelectors);
246
Santos Cordon681663d2014-01-30 04:32:15 -0800247 mOutgoingCallsManager.placeCall(call, mCallServices, selectors);
Ben Giladb59769e2014-01-16 11:41:10 -0800248 }
249
250 /**
251 * Determines whether or not the specified collection is either null or empty.
252 *
253 * @param collection Either null or the collection object to be evaluated.
254 * @return True if the collection is null or empty.
255 */
256 @SuppressWarnings("rawtypes")
257 private boolean isNullOrEmpty(Collection collection) {
258 return collection == null || collection.isEmpty();
259 }
Ben Gilad9f2bed32013-12-12 17:43:26 -0800260}