blob: 017a8d582e28a950341dd1b90bdfa4cb4f4208dc [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
Sailesh Nepalb6141ae2014-02-18 08:45:26 -0800117 mNewOutgoingCalls.add(call);
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.
Sailesh Nepalb6141ae2014-02-18 08:45:26 -0800121 mLookupId++;
Santos Cordonc195e362014-02-11 17:05:31 -0800122 mCallServiceRepository.initiateLookup(mLookupId);
123 mSelectorRepository.initiateLookup(mLookupId);
Ben Gilad0407fb22014-01-09 16:18:41 -0800124 }
Ben Gilad9f2bed32013-12-12 17:43:26 -0800125
Ben Gilad0407fb22014-01-09 16:18:41 -0800126 /**
Ben Gilad0bf5b912014-01-28 17:55:57 -0800127 * Persists the specified set of call services and attempts to place any pending outgoing
Santos Cordonc195e362014-02-11 17:05:31 -0800128 * calls. Intended to be invoked by {@link CallServiceRepository} exclusively.
Ben Gilad0407fb22014-01-09 16:18:41 -0800129 *
Ben Gilad03292d42014-01-16 15:06:16 -0800130 * @param callServices The potentially-partial set of call services. Partial since the lookup
131 * process is time-boxed, such that some providers/call-services may be slow to respond and
132 * hence effectively omitted from the specified list.
Ben Gilad0407fb22014-01-09 16:18:41 -0800133 */
Santos Cordonc195e362014-02-11 17:05:31 -0800134 void setCallServices(Set<CallServiceWrapper> callServices) {
Evan Charlton0958f532014-01-10 16:58:02 -0800135 ThreadUtil.checkOnMainThread();
Ben Gilad0407fb22014-01-09 16:18:41 -0800136
Ben Giladb59769e2014-01-16 11:41:10 -0800137 mCallServices = callServices;
Ben Gilad0bf5b912014-01-28 17:55:57 -0800138 processNewOutgoingCalls();
Ben Giladb59769e2014-01-16 11:41:10 -0800139 }
140
141 /**
142 * Persists the specified list of selectors and attempts to connect any pending outgoing
Santos Cordonc195e362014-02-11 17:05:31 -0800143 * calls. Intended to be invoked by {@link CallServiceSelectorRepository} exclusively.
Ben Giladb59769e2014-01-16 11:41:10 -0800144 *
Ben Gilad03292d42014-01-16 15:06:16 -0800145 * @param selectors The potentially-partial set of selectors. Partial since the lookup
146 * procedure is time-boxed such that some selectors may be slow to respond and hence
147 * effectively omitted from the specified set.
Ben Giladb59769e2014-01-16 11:41:10 -0800148 */
Ben Gilad03292d42014-01-16 15:06:16 -0800149 void setSelectors(Set<ICallServiceSelector> selectors) {
Santos Cordonc195e362014-02-11 17:05:31 -0800150 // TODO(santoscordon): This should take in CallServiceSelectorWrapper instead of the direct
151 // ICallServiceSelector implementation. Copy what we have for CallServiceWrapper.
Ben Giladb59769e2014-01-16 11:41:10 -0800152 ThreadUtil.checkOnMainThread();
153
Ben Gilad134cf092014-01-16 18:26:12 -0800154 // TODO(gilad): Add logic to include the built-in selectors (e.g. for dealing with
155 // emergency calls) and order the entire set prior to the assignment below. If the
156 // built-in selectors can be implemented in a manner that does not require binding,
157 // that's probably preferred. May want to use a LinkedHashSet for the sorted set.
Ben Giladb59769e2014-01-16 11:41:10 -0800158 mSelectors = selectors;
Ben Gilad0bf5b912014-01-28 17:55:57 -0800159 processNewOutgoingCalls();
Ben Giladb59769e2014-01-16 11:41:10 -0800160 }
161
162 /**
Ben Gilad0bf5b912014-01-28 17:55:57 -0800163 * Handles the case where an outgoing call has been successfully placed,
164 * see {@link OutgoingCallProcessor}.
Ben Giladb59769e2014-01-16 11:41:10 -0800165 */
Ben Gilad0bf5b912014-01-28 17:55:57 -0800166 void handleSuccessfulOutgoingCall(Call call) {
Santos Cordon681663d2014-01-30 04:32:15 -0800167 mCallsManager.handleSuccessfulOutgoingCall(call);
Ben Gilad0bf5b912014-01-28 17:55:57 -0800168
169 // Process additional (new) calls, if any.
170 processNewOutgoingCalls();
171 }
172
173 /**
174 * Handles the case where an outgoing call could not be proceed by any of the
175 * selector/call-service implementations, see {@link OutgoingCallProcessor}.
176 */
177 void handleFailedOutgoingCall(Call call) {
178 // TODO(gilad): More here.
179
180 // Process additional (new) calls, if any.
181 processNewOutgoingCalls();
182 }
183
184 /**
Ben Gilad2313e622014-02-06 12:02:25 -0800185 * @return True if ticking should continue (or be resumed) and false otherwise.
186 */
187 private boolean isTicking() {
188 // TODO(gilad): return true every time at least one outgoing call is pending (i.e. waiting
189 // to be connected by a call service) and also when at least one active call is switch-able
190 // between call services, see {@link ICallServiceSelector#isSwitchable}.
191 return false;
192 }
193
194 /**
195 * Schedules the next tick invocation.
196 */
197 private void scheduleNextTick() {
198 mHandler.postDelayed(mTicker, TICK_FREQUENCY);
199 }
200
201 /**
202 * Performs the set of tasks that needs to be executed on polling basis.
203 * TODO(gilad): Check for stale pending calls that may need to be terminated etc, see
204 * mNewOutgoingCalls and mPendingOutgoingCalls.
205 * TODO(gilad): Also intended to trigger the call switching/hand-off logic when applicable.
206 */
207 private void tick() {
208 // TODO(gilad): More here.
Santos Cordonc195e362014-02-11 17:05:31 -0800209 // TODO(santoscordon): Clear mCallServices if there exist no more new or pending outgoing
210 // calls.
Ben Gilad2313e622014-02-06 12:02:25 -0800211 }
212
213 /**
Ben Gilad0bf5b912014-01-28 17:55:57 -0800214 * Attempts to process the next new outgoing calls that have not yet been expired.
215 */
216 private void processNewOutgoingCalls() {
217 if (isNullOrEmpty(mCallServices) || isNullOrEmpty(mSelectors)) {
218 // At least one call service and one selector are required to process outgoing calls.
219 return;
220 }
221
222 if (!mNewOutgoingCalls.isEmpty()) {
223 Call call = mNewOutgoingCalls.iterator().next();
224 mNewOutgoingCalls.remove(call);
225 mPendingOutgoingCalls.add(call);
226
227 // Specifically only attempt to place one call at a time such that call services
228 // can be freed from needing to deal with concurrent requests.
229 processNewOutgoingCall(call);
Ben Gilad0407fb22014-01-09 16:18:41 -0800230 }
Ben Gilad9f2bed32013-12-12 17:43:26 -0800231 }
232
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800233 /**
Ben Giladb59769e2014-01-16 11:41:10 -0800234 * Attempts to place the specified call.
235 *
Ben Gilad0bf5b912014-01-28 17:55:57 -0800236 * @param call The call to place.
Ben Giladb59769e2014-01-16 11:41:10 -0800237 */
Ben Gilad0bf5b912014-01-28 17:55:57 -0800238 private void processNewOutgoingCall(Call call) {
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}