blob: 753f284a15214f1cebd9be63449cf141993497ce [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 Gilad0bf5b912014-01-28 17:55:57 -080021import com.google.common.collect.Maps;
Ben Gilad0407fb22014-01-09 16:18:41 -080022import com.google.common.collect.Sets;
Santos Cordon8e8b8d22013-12-19 14:14:05 -080023
24import android.content.Context;
Ben Gilad2313e622014-02-06 12:02:25 -080025import android.os.Handler;
26import android.os.Looper;
Santos Cordon8e8b8d22013-12-19 14:14:05 -080027import android.telecomm.ICallService;
Ben Giladb59769e2014-01-16 11:41:10 -080028import android.telecomm.ICallServiceSelector;
Santos Cordon8e8b8d22013-12-19 14:14:05 -080029
Ben Giladb59769e2014-01-16 11:41:10 -080030import java.util.Collection;
Ben Gilad9f2bed32013-12-12 17:43:26 -080031import java.util.List;
Ben Gilad0bf5b912014-01-28 17:55:57 -080032import java.util.Map;
Ben Gilad0407fb22014-01-09 16:18:41 -080033import java.util.Set;
Ben Gilad9f2bed32013-12-12 17:43:26 -080034
Santos Cordon8e8b8d22013-12-19 14:14:05 -080035/**
36 * Switchboard is responsible for (1) selecting the {@link ICallService} through which to make
37 * outgoing calls and (2) switching active calls between transports (each ICallService is
38 * considered a different transport type).
Santos Cordon8e8b8d22013-12-19 14:14:05 -080039 */
Ben Giladd17443c2014-01-06 11:04:15 -080040final class Switchboard {
Ben Gilad2495d572014-01-09 17:26:19 -080041
Ben Gilad2313e622014-02-06 12:02:25 -080042 /**
43 * The frequency of invoking tick in milliseconds.
44 * TODO(gilad): May require tuning.
45 */
46 private final static int TICK_FREQUENCY = 250;
Ben Giladb59769e2014-01-16 11:41:10 -080047
Santos Cordon681663d2014-01-30 04:32:15 -080048 private final CallsManager mCallsManager;
Ben Gilad2313e622014-02-06 12:02:25 -080049
Santos Cordon681663d2014-01-30 04:32:15 -080050 /** Used to place outgoing calls. */
51 private final OutgoingCallsManager mOutgoingCallsManager;
52
53 private CallServiceFinder mCallServiceFinder;
54
55 private CallServiceSelectorFinder mSelectorFinder;
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 /**
80 * The set of currently available call-service implementations, see {@link CallServiceFinder}.
81 * TODO(gilad): Null out once the active-call count goes to zero.
82 */
83 private Set<ICallService> mCallServices;
Ben Giladb59769e2014-01-16 11:41:10 -080084
Ben Gilad03292d42014-01-16 15:06:16 -080085 /**
86 * The set of currently available call-service-selector implementations,
87 * see {@link CallServiceSelectorFinder}.
88 * TODO(gilad): Null out once the active-call count goes to zero.
89 */
90 private Set<ICallServiceSelector> mSelectors;
Ben Gilad0407fb22014-01-09 16:18:41 -080091
Santos Cordon681663d2014-01-30 04:32:15 -080092 /**
93 * Persists the specified parameters and initializes Switchboard.
94 */
Santos Cordon6242b132014-02-07 16:24:42 -080095 Switchboard(CallsManager callsManager) {
96 mCallsManager = callsManager;
Santos Cordon681663d2014-01-30 04:32:15 -080097 mOutgoingCallsManager = new OutgoingCallsManager(this);
98 mCallServiceFinder = new CallServiceFinder(this, mOutgoingCallsManager);
99 mSelectorFinder = new CallServiceSelectorFinder(this);
100 }
Ben Giladd17443c2014-01-06 11:04:15 -0800101
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800102 /**
Ben Gilad8bdaa462014-02-05 12:53:19 -0800103 * Attempts to place an outgoing call to the specified handle.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800104 *
Ben Gilad13329fd2014-02-11 17:20:29 -0800105 * @param call The yet-to-be-connected outgoing-call object.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800106 * @param context The application context.
107 */
Ben Gilad13329fd2014-02-11 17:20:29 -0800108 void placeOutgoingCall(Call call, Context context) {
Evan Charlton0958f532014-01-10 16:58:02 -0800109 ThreadUtil.checkOnMainThread();
Ben Giladb59769e2014-01-16 11:41:10 -0800110
Ben Giladb59769e2014-01-16 11:41:10 -0800111 boolean bailout = false;
112 if (isNullOrEmpty(mCallServices)) {
113 mCallServiceFinder.initiateLookup(context);
114 bailout = true;
115 }
116 if (isNullOrEmpty(mSelectors)) {
117 mSelectorFinder.initiateLookup(context);
118 bailout = true;
119 }
120
121 if (bailout) {
122 // Unable to process the call without either call service, selectors, or both.
123 // Store the call for deferred processing and bail out.
Ben Gilad0bf5b912014-01-28 17:55:57 -0800124 mNewOutgoingCalls.add(call);
Ben Giladb59769e2014-01-16 11:41:10 -0800125 return;
126 }
127
Ben Gilad0bf5b912014-01-28 17:55:57 -0800128 processNewOutgoingCall(call);
Ben Gilad0407fb22014-01-09 16:18:41 -0800129 }
Ben Gilad9f2bed32013-12-12 17:43:26 -0800130
Ben Gilad0407fb22014-01-09 16:18:41 -0800131 /**
Ben Gilad0bf5b912014-01-28 17:55:57 -0800132 * Persists the specified set of call services and attempts to place any pending outgoing
133 * calls. Intended to be invoked by {@link CallServiceFinder} exclusively.
Ben Gilad0407fb22014-01-09 16:18:41 -0800134 *
Ben Gilad03292d42014-01-16 15:06:16 -0800135 * @param callServices The potentially-partial set of call services. Partial since the lookup
136 * process is time-boxed, such that some providers/call-services may be slow to respond and
137 * hence effectively omitted from the specified list.
Ben Gilad0407fb22014-01-09 16:18:41 -0800138 */
Ben Gilad03292d42014-01-16 15:06:16 -0800139 void setCallServices(Set<ICallService> callServices) {
Evan Charlton0958f532014-01-10 16:58:02 -0800140 ThreadUtil.checkOnMainThread();
Ben Gilad0407fb22014-01-09 16:18:41 -0800141
Ben Giladb59769e2014-01-16 11:41:10 -0800142 mCallServices = callServices;
Ben Gilad0bf5b912014-01-28 17:55:57 -0800143 processNewOutgoingCalls();
Ben Giladb59769e2014-01-16 11:41:10 -0800144 }
145
146 /**
147 * Persists the specified list of selectors and attempts to connect any pending outgoing
Ben Gilad0bf5b912014-01-28 17:55:57 -0800148 * calls. Intended to be invoked by {@link CallServiceSelectorFinder} exclusively.
Ben Giladb59769e2014-01-16 11:41:10 -0800149 *
Ben Gilad03292d42014-01-16 15:06:16 -0800150 * @param selectors The potentially-partial set of selectors. Partial since the lookup
151 * procedure is time-boxed such that some selectors may be slow to respond and hence
152 * effectively omitted from the specified set.
Ben Giladb59769e2014-01-16 11:41:10 -0800153 */
Ben Gilad03292d42014-01-16 15:06:16 -0800154 void setSelectors(Set<ICallServiceSelector> selectors) {
Ben Giladb59769e2014-01-16 11:41:10 -0800155 ThreadUtil.checkOnMainThread();
156
Ben Gilad134cf092014-01-16 18:26:12 -0800157 // TODO(gilad): Add logic to include the built-in selectors (e.g. for dealing with
158 // emergency calls) and order the entire set prior to the assignment below. If the
159 // built-in selectors can be implemented in a manner that does not require binding,
160 // that's probably preferred. May want to use a LinkedHashSet for the sorted set.
Ben Giladb59769e2014-01-16 11:41:10 -0800161 mSelectors = selectors;
Ben Gilad0bf5b912014-01-28 17:55:57 -0800162 processNewOutgoingCalls();
Ben Giladb59769e2014-01-16 11:41:10 -0800163 }
164
165 /**
Ben Gilad0bf5b912014-01-28 17:55:57 -0800166 * Handles the case where an outgoing call has been successfully placed,
167 * see {@link OutgoingCallProcessor}.
Ben Giladb59769e2014-01-16 11:41:10 -0800168 */
Ben Gilad0bf5b912014-01-28 17:55:57 -0800169 void handleSuccessfulOutgoingCall(Call call) {
Santos Cordon681663d2014-01-30 04:32:15 -0800170 mCallsManager.handleSuccessfulOutgoingCall(call);
Ben Gilad0bf5b912014-01-28 17:55:57 -0800171
172 // Process additional (new) calls, if any.
173 processNewOutgoingCalls();
174 }
175
176 /**
177 * Handles the case where an outgoing call could not be proceed by any of the
178 * selector/call-service implementations, see {@link OutgoingCallProcessor}.
179 */
180 void handleFailedOutgoingCall(Call call) {
181 // TODO(gilad): More here.
182
183 // Process additional (new) calls, if any.
184 processNewOutgoingCalls();
185 }
186
187 /**
Ben Gilad2313e622014-02-06 12:02:25 -0800188 * @return True if ticking should continue (or be resumed) and false otherwise.
189 */
190 private boolean isTicking() {
191 // TODO(gilad): return true every time at least one outgoing call is pending (i.e. waiting
192 // to be connected by a call service) and also when at least one active call is switch-able
193 // between call services, see {@link ICallServiceSelector#isSwitchable}.
194 return false;
195 }
196
197 /**
198 * Schedules the next tick invocation.
199 */
200 private void scheduleNextTick() {
201 mHandler.postDelayed(mTicker, TICK_FREQUENCY);
202 }
203
204 /**
205 * Performs the set of tasks that needs to be executed on polling basis.
206 * TODO(gilad): Check for stale pending calls that may need to be terminated etc, see
207 * mNewOutgoingCalls and mPendingOutgoingCalls.
208 * TODO(gilad): Also intended to trigger the call switching/hand-off logic when applicable.
209 */
210 private void tick() {
211 // TODO(gilad): More here.
212 }
213
214 /**
Ben Gilad0bf5b912014-01-28 17:55:57 -0800215 * Attempts to process the next new outgoing calls that have not yet been expired.
216 */
217 private void processNewOutgoingCalls() {
218 if (isNullOrEmpty(mCallServices) || isNullOrEmpty(mSelectors)) {
219 // At least one call service and one selector are required to process outgoing calls.
220 return;
221 }
222
223 if (!mNewOutgoingCalls.isEmpty()) {
224 Call call = mNewOutgoingCalls.iterator().next();
225 mNewOutgoingCalls.remove(call);
226 mPendingOutgoingCalls.add(call);
227
228 // Specifically only attempt to place one call at a time such that call services
229 // can be freed from needing to deal with concurrent requests.
230 processNewOutgoingCall(call);
Ben Gilad0407fb22014-01-09 16:18:41 -0800231 }
Ben Gilad9f2bed32013-12-12 17:43:26 -0800232 }
233
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800234 /**
Ben Giladb59769e2014-01-16 11:41:10 -0800235 * Attempts to place the specified call.
236 *
Ben Gilad0bf5b912014-01-28 17:55:57 -0800237 * @param call The call to place.
Ben Giladb59769e2014-01-16 11:41:10 -0800238 */
Ben Gilad0bf5b912014-01-28 17:55:57 -0800239 private void processNewOutgoingCall(Call call) {
Ben Giladb59769e2014-01-16 11:41:10 -0800240
Ben Gilad0bf5b912014-01-28 17:55:57 -0800241 Preconditions.checkNotNull(mCallServices);
242 Preconditions.checkNotNull(mSelectors);
Ben Giladb59769e2014-01-16 11:41:10 -0800243
Ben Gilad0bf5b912014-01-28 17:55:57 -0800244 // Convert to (duplicate-free) list to aid index-based iteration, see the comment under
245 // setSelectors regarding using LinkedHashSet instead.
246 List<ICallServiceSelector> selectors = Lists.newArrayList();
247 selectors.addAll(mSelectors);
248
Santos Cordon681663d2014-01-30 04:32:15 -0800249 mOutgoingCallsManager.placeCall(call, mCallServices, selectors);
Ben Giladb59769e2014-01-16 11:41:10 -0800250 }
251
252 /**
253 * Determines whether or not the specified collection is either null or empty.
254 *
255 * @param collection Either null or the collection object to be evaluated.
256 * @return True if the collection is null or empty.
257 */
258 @SuppressWarnings("rawtypes")
259 private boolean isNullOrEmpty(Collection collection) {
260 return collection == null || collection.isEmpty();
261 }
Ben Gilad9f2bed32013-12-12 17:43:26 -0800262}