blob: 32ef914093b09343e623221f08d30731053c7e41 [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;
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 Cordonc195e362014-02-11 17:05:31 -080050 private final CallServiceRepository mCallServiceRepository;
Santos Cordon681663d2014-01-30 04:32:15 -080051
Santos Cordonc195e362014-02-11 17:05:31 -080052 private final CallServiceSelectorRepository mSelectorRepository;
Ben Gilad2313e622014-02-06 12:02:25 -080053
54 /** Used to schedule tasks on the main (UI) thread. */
55 private final Handler mHandler = new Handler(Looper.getMainLooper());
56
57 /**
58 * Executes a single tick task and potentially schedules the next such that polling continues
59 * as long as necessary.
60 * NOTE(gilad): by design no two tick invocations should ever overlap.
61 */
62 private final Runnable mTicker = new Runnable() {
63 @Override
64 public void run() {
65 tick();
66 if (isTicking()) {
67 scheduleNextTick();
68 }
69 }
70 };
71
72 private final Set<Call> mNewOutgoingCalls = Sets.newLinkedHashSet();
73
74 private final Set<Call> mPendingOutgoingCalls = Sets.newLinkedHashSet();
Ben Giladb59769e2014-01-16 11:41:10 -080075
Ben Gilad03292d42014-01-16 15:06:16 -080076 /**
Santos Cordonc195e362014-02-11 17:05:31 -080077 * The set of currently available call service implementations, see
78 * {@link CallServiceRepository}. Populated after a lookup for call services as part of
79 * {@link #placeCall}. It is cleared periodically when there are no more new or pending outgoing
80 * calls.
Ben Gilad03292d42014-01-16 15:06:16 -080081 */
Santos Cordonc195e362014-02-11 17:05:31 -080082 private Set<CallServiceWrapper> mCallServices;
Ben Giladb59769e2014-01-16 11:41:10 -080083
Ben Gilad03292d42014-01-16 15:06:16 -080084 /**
85 * The set of currently available call-service-selector implementations,
Santos Cordonc195e362014-02-11 17:05:31 -080086 * see {@link CallServiceSelectorRepository}.
Ben Gilad03292d42014-01-16 15:06:16 -080087 * TODO(gilad): Null out once the active-call count goes to zero.
88 */
89 private Set<ICallServiceSelector> mSelectors;
Ben Gilad0407fb22014-01-09 16:18:41 -080090
Santos Cordon681663d2014-01-30 04:32:15 -080091 /**
Santos Cordonc195e362014-02-11 17:05:31 -080092 * The current lookup-cycle ID used with the repositories. Incremented with each invocation
93 * of {@link #placeCall} and passed to the repositories via initiateLookup().
94 */
95 private int mLookupId = 0;
96
97 /**
Santos Cordon681663d2014-01-30 04:32:15 -080098 * Persists the specified parameters and initializes Switchboard.
99 */
Santos Cordon6242b132014-02-07 16:24:42 -0800100 Switchboard(CallsManager callsManager) {
101 mCallsManager = callsManager;
Santos Cordon681663d2014-01-30 04:32:15 -0800102 mOutgoingCallsManager = new OutgoingCallsManager(this);
Santos Cordonc195e362014-02-11 17:05:31 -0800103 mCallServiceRepository = new CallServiceRepository(this, mOutgoingCallsManager);
104 mSelectorRepository = new CallServiceSelectorRepository(this);
Santos Cordon681663d2014-01-30 04:32:15 -0800105 }
Ben Giladd17443c2014-01-06 11:04:15 -0800106
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800107 /**
Santos Cordonc195e362014-02-11 17:05:31 -0800108 * Starts the process of placing an outgoing call by searching for available call services
109 * through which the call can be placed. After a lookup for those services completes, execution
110 * returns to {@link #setCallServices} where the process of placing the call continues.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800111 *
Ben Gilad13329fd2014-02-11 17:20:29 -0800112 * @param call The yet-to-be-connected outgoing-call object.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800113 */
Santos Cordonc195e362014-02-11 17:05:31 -0800114 void placeOutgoingCall(Call call) {
Evan Charlton0958f532014-01-10 16:58:02 -0800115 ThreadUtil.checkOnMainThread();
Ben Giladb59769e2014-01-16 11:41:10 -0800116
Santos Cordonc195e362014-02-11 17:05:31 -0800117 mLookupId++;
Ben Giladb59769e2014-01-16 11:41:10 -0800118
Santos Cordonc195e362014-02-11 17:05:31 -0800119 // We initialize a lookup every time because between calls the set of available call
120 // services can change between calls.
121 mCallServiceRepository.initiateLookup(mLookupId);
122 mSelectorRepository.initiateLookup(mLookupId);
Ben Gilad0407fb22014-01-09 16:18:41 -0800123 }
Ben Gilad9f2bed32013-12-12 17:43:26 -0800124
Ben Gilad0407fb22014-01-09 16:18:41 -0800125 /**
Ben Gilad0bf5b912014-01-28 17:55:57 -0800126 * Persists the specified set of call services and attempts to place any pending outgoing
Santos Cordonc195e362014-02-11 17:05:31 -0800127 * calls. Intended to be invoked by {@link CallServiceRepository} exclusively.
Ben Gilad0407fb22014-01-09 16:18:41 -0800128 *
Ben Gilad03292d42014-01-16 15:06:16 -0800129 * @param callServices The potentially-partial set of call services. Partial since the lookup
130 * process is time-boxed, such that some providers/call-services may be slow to respond and
131 * hence effectively omitted from the specified list.
Ben Gilad0407fb22014-01-09 16:18:41 -0800132 */
Santos Cordonc195e362014-02-11 17:05:31 -0800133 void setCallServices(Set<CallServiceWrapper> callServices) {
Evan Charlton0958f532014-01-10 16:58:02 -0800134 ThreadUtil.checkOnMainThread();
Ben Gilad0407fb22014-01-09 16:18:41 -0800135
Ben Giladb59769e2014-01-16 11:41:10 -0800136 mCallServices = callServices;
Ben Gilad0bf5b912014-01-28 17:55:57 -0800137 processNewOutgoingCalls();
Ben Giladb59769e2014-01-16 11:41:10 -0800138 }
139
140 /**
141 * Persists the specified list of selectors and attempts to connect any pending outgoing
Santos Cordonc195e362014-02-11 17:05:31 -0800142 * calls. Intended to be invoked by {@link CallServiceSelectorRepository} exclusively.
Ben Giladb59769e2014-01-16 11:41:10 -0800143 *
Ben Gilad03292d42014-01-16 15:06:16 -0800144 * @param selectors The potentially-partial set of selectors. Partial since the lookup
145 * procedure is time-boxed such that some selectors may be slow to respond and hence
146 * effectively omitted from the specified set.
Ben Giladb59769e2014-01-16 11:41:10 -0800147 */
Ben Gilad03292d42014-01-16 15:06:16 -0800148 void setSelectors(Set<ICallServiceSelector> selectors) {
Santos Cordonc195e362014-02-11 17:05:31 -0800149 // TODO(santoscordon): This should take in CallServiceSelectorWrapper instead of the direct
150 // ICallServiceSelector implementation. Copy what we have for CallServiceWrapper.
Ben Giladb59769e2014-01-16 11:41:10 -0800151 ThreadUtil.checkOnMainThread();
152
Ben Gilad134cf092014-01-16 18:26:12 -0800153 // TODO(gilad): Add logic to include the built-in selectors (e.g. for dealing with
154 // emergency calls) and order the entire set prior to the assignment below. If the
155 // built-in selectors can be implemented in a manner that does not require binding,
156 // that's probably preferred. May want to use a LinkedHashSet for the sorted set.
Ben Giladb59769e2014-01-16 11:41:10 -0800157 mSelectors = selectors;
Ben Gilad0bf5b912014-01-28 17:55:57 -0800158 processNewOutgoingCalls();
Ben Giladb59769e2014-01-16 11:41:10 -0800159 }
160
161 /**
Ben Gilad0bf5b912014-01-28 17:55:57 -0800162 * Handles the case where an outgoing call has been successfully placed,
163 * see {@link OutgoingCallProcessor}.
Ben Giladb59769e2014-01-16 11:41:10 -0800164 */
Ben Gilad0bf5b912014-01-28 17:55:57 -0800165 void handleSuccessfulOutgoingCall(Call call) {
Santos Cordon681663d2014-01-30 04:32:15 -0800166 mCallsManager.handleSuccessfulOutgoingCall(call);
Ben Gilad0bf5b912014-01-28 17:55:57 -0800167
168 // Process additional (new) calls, if any.
169 processNewOutgoingCalls();
170 }
171
172 /**
173 * Handles the case where an outgoing call could not be proceed by any of the
174 * selector/call-service implementations, see {@link OutgoingCallProcessor}.
175 */
176 void handleFailedOutgoingCall(Call call) {
177 // TODO(gilad): More here.
178
179 // Process additional (new) calls, if any.
180 processNewOutgoingCalls();
181 }
182
183 /**
Ben Gilad2313e622014-02-06 12:02:25 -0800184 * @return True if ticking should continue (or be resumed) and false otherwise.
185 */
186 private boolean isTicking() {
187 // TODO(gilad): return true every time at least one outgoing call is pending (i.e. waiting
188 // to be connected by a call service) and also when at least one active call is switch-able
189 // between call services, see {@link ICallServiceSelector#isSwitchable}.
190 return false;
191 }
192
193 /**
194 * Schedules the next tick invocation.
195 */
196 private void scheduleNextTick() {
197 mHandler.postDelayed(mTicker, TICK_FREQUENCY);
198 }
199
200 /**
201 * Performs the set of tasks that needs to be executed on polling basis.
202 * TODO(gilad): Check for stale pending calls that may need to be terminated etc, see
203 * mNewOutgoingCalls and mPendingOutgoingCalls.
204 * TODO(gilad): Also intended to trigger the call switching/hand-off logic when applicable.
205 */
206 private void tick() {
207 // TODO(gilad): More here.
Santos Cordonc195e362014-02-11 17:05:31 -0800208 // TODO(santoscordon): Clear mCallServices if there exist no more new or pending outgoing
209 // calls.
Ben Gilad2313e622014-02-06 12:02:25 -0800210 }
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 Gilad0bf5b912014-01-28 17:55:57 -0800238 Preconditions.checkNotNull(mCallServices);
239 Preconditions.checkNotNull(mSelectors);
Ben Giladb59769e2014-01-16 11:41:10 -0800240
Ben Gilad0bf5b912014-01-28 17:55:57 -0800241 // Convert to (duplicate-free) list to aid index-based iteration, see the comment under
242 // setSelectors regarding using LinkedHashSet instead.
243 List<ICallServiceSelector> selectors = Lists.newArrayList();
244 selectors.addAll(mSelectors);
245
Santos Cordon681663d2014-01-30 04:32:15 -0800246 mOutgoingCallsManager.placeCall(call, mCallServices, selectors);
Ben Giladb59769e2014-01-16 11:41:10 -0800247 }
248
249 /**
250 * Determines whether or not the specified collection is either null or empty.
251 *
252 * @param collection Either null or the collection object to be evaluated.
253 * @return True if the collection is null or empty.
254 */
255 @SuppressWarnings("rawtypes")
256 private boolean isNullOrEmpty(Collection collection) {
257 return collection == null || collection.isEmpty();
258 }
Ben Gilad9f2bed32013-12-12 17:43:26 -0800259}