| Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 1 | /* |
| 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 Gilad | 9f2bed3 | 2013-12-12 17:43:26 -0800 | [diff] [blame] | 17 | package com.android.telecomm; |
| 18 | |
| Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 19 | import com.google.common.base.Preconditions; |
| Evan Charlton | ac1aa9e | 2014-01-03 13:47:12 -0800 | [diff] [blame] | 20 | import com.google.common.collect.Lists; |
| Ben Gilad | 0407fb2 | 2014-01-09 16:18:41 -0800 | [diff] [blame] | 21 | import com.google.common.collect.Sets; |
| Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 22 | |
| 23 | import android.content.Context; |
| Ben Gilad | 2313e62 | 2014-02-06 12:02:25 -0800 | [diff] [blame] | 24 | import android.os.Handler; |
| 25 | import android.os.Looper; |
| Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 26 | import android.telecomm.ICallService; |
| Ben Gilad | b59769e | 2014-01-16 11:41:10 -0800 | [diff] [blame] | 27 | import android.telecomm.ICallServiceSelector; |
| Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 28 | |
| Ben Gilad | b59769e | 2014-01-16 11:41:10 -0800 | [diff] [blame] | 29 | import java.util.Collection; |
| Ben Gilad | 9f2bed3 | 2013-12-12 17:43:26 -0800 | [diff] [blame] | 30 | import java.util.List; |
| Ben Gilad | 0407fb2 | 2014-01-09 16:18:41 -0800 | [diff] [blame] | 31 | import java.util.Set; |
| Ben Gilad | 9f2bed3 | 2013-12-12 17:43:26 -0800 | [diff] [blame] | 32 | |
| Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 33 | /** |
| 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 Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 37 | */ |
| Ben Gilad | d17443c | 2014-01-06 11:04:15 -0800 | [diff] [blame] | 38 | final class Switchboard { |
| Ben Gilad | 2495d57 | 2014-01-09 17:26:19 -0800 | [diff] [blame] | 39 | |
| Ben Gilad | 2313e62 | 2014-02-06 12:02:25 -0800 | [diff] [blame] | 40 | /** |
| 41 | * The frequency of invoking tick in milliseconds. |
| 42 | * TODO(gilad): May require tuning. |
| 43 | */ |
| 44 | private final static int TICK_FREQUENCY = 250; |
| Ben Gilad | b59769e | 2014-01-16 11:41:10 -0800 | [diff] [blame] | 45 | |
| Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 46 | private final CallsManager mCallsManager; |
| Ben Gilad | 2313e62 | 2014-02-06 12:02:25 -0800 | [diff] [blame] | 47 | |
| Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 48 | /** Used to place outgoing calls. */ |
| 49 | private final OutgoingCallsManager mOutgoingCallsManager; |
| 50 | |
| 51 | private CallServiceFinder mCallServiceFinder; |
| 52 | |
| 53 | private CallServiceSelectorFinder mSelectorFinder; |
| Ben Gilad | 2313e62 | 2014-02-06 12:02:25 -0800 | [diff] [blame] | 54 | |
| 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 Gilad | b59769e | 2014-01-16 11:41:10 -0800 | [diff] [blame] | 76 | |
| Ben Gilad | 03292d4 | 2014-01-16 15:06:16 -0800 | [diff] [blame] | 77 | /** |
| 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 Gilad | b59769e | 2014-01-16 11:41:10 -0800 | [diff] [blame] | 82 | |
| Ben Gilad | 03292d4 | 2014-01-16 15:06:16 -0800 | [diff] [blame] | 83 | /** |
| 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 Gilad | 0407fb2 | 2014-01-09 16:18:41 -0800 | [diff] [blame] | 89 | |
| Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 90 | /** |
| 91 | * Persists the specified parameters and initializes Switchboard. |
| 92 | */ |
| Santos Cordon | 6242b13 | 2014-02-07 16:24:42 -0800 | [diff] [blame] | 93 | Switchboard(CallsManager callsManager) { |
| 94 | mCallsManager = callsManager; |
| Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 95 | mOutgoingCallsManager = new OutgoingCallsManager(this); |
| 96 | mCallServiceFinder = new CallServiceFinder(this, mOutgoingCallsManager); |
| 97 | mSelectorFinder = new CallServiceSelectorFinder(this); |
| 98 | } |
| Ben Gilad | d17443c | 2014-01-06 11:04:15 -0800 | [diff] [blame] | 99 | |
| Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 100 | /** |
| Ben Gilad | 8bdaa46 | 2014-02-05 12:53:19 -0800 | [diff] [blame] | 101 | * Attempts to place an outgoing call to the specified handle. |
| Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 102 | * |
| Ben Gilad | 13329fd | 2014-02-11 17:20:29 -0800 | [diff] [blame] | 103 | * @param call The yet-to-be-connected outgoing-call object. |
| Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 104 | * @param context The application context. |
| 105 | */ |
| Ben Gilad | 13329fd | 2014-02-11 17:20:29 -0800 | [diff] [blame] | 106 | void placeOutgoingCall(Call call, Context context) { |
| Evan Charlton | 0958f53 | 2014-01-10 16:58:02 -0800 | [diff] [blame] | 107 | ThreadUtil.checkOnMainThread(); |
| Ben Gilad | b59769e | 2014-01-16 11:41:10 -0800 | [diff] [blame] | 108 | |
| Ben Gilad | b59769e | 2014-01-16 11:41:10 -0800 | [diff] [blame] | 109 | boolean bailout = false; |
| 110 | if (isNullOrEmpty(mCallServices)) { |
| Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 111 | mCallServiceFinder.initiateLookup(); |
| Ben Gilad | b59769e | 2014-01-16 11:41:10 -0800 | [diff] [blame] | 112 | 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 Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 122 | mNewOutgoingCalls.add(call); |
| Ben Gilad | b59769e | 2014-01-16 11:41:10 -0800 | [diff] [blame] | 123 | return; |
| 124 | } |
| 125 | |
| Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 126 | processNewOutgoingCall(call); |
| Ben Gilad | 0407fb2 | 2014-01-09 16:18:41 -0800 | [diff] [blame] | 127 | } |
| Ben Gilad | 9f2bed3 | 2013-12-12 17:43:26 -0800 | [diff] [blame] | 128 | |
| Ben Gilad | 0407fb2 | 2014-01-09 16:18:41 -0800 | [diff] [blame] | 129 | /** |
| Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 130 | * 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 Gilad | 0407fb2 | 2014-01-09 16:18:41 -0800 | [diff] [blame] | 132 | * |
| Ben Gilad | 03292d4 | 2014-01-16 15:06:16 -0800 | [diff] [blame] | 133 | * @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 Gilad | 0407fb2 | 2014-01-09 16:18:41 -0800 | [diff] [blame] | 136 | */ |
| Ben Gilad | 03292d4 | 2014-01-16 15:06:16 -0800 | [diff] [blame] | 137 | void setCallServices(Set<ICallService> callServices) { |
| Evan Charlton | 0958f53 | 2014-01-10 16:58:02 -0800 | [diff] [blame] | 138 | ThreadUtil.checkOnMainThread(); |
| Ben Gilad | 0407fb2 | 2014-01-09 16:18:41 -0800 | [diff] [blame] | 139 | |
| Ben Gilad | b59769e | 2014-01-16 11:41:10 -0800 | [diff] [blame] | 140 | mCallServices = callServices; |
| Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 141 | processNewOutgoingCalls(); |
| Ben Gilad | b59769e | 2014-01-16 11:41:10 -0800 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | /** |
| 145 | * Persists the specified list of selectors and attempts to connect any pending outgoing |
| Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 146 | * calls. Intended to be invoked by {@link CallServiceSelectorFinder} exclusively. |
| Ben Gilad | b59769e | 2014-01-16 11:41:10 -0800 | [diff] [blame] | 147 | * |
| Ben Gilad | 03292d4 | 2014-01-16 15:06:16 -0800 | [diff] [blame] | 148 | * @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 Gilad | b59769e | 2014-01-16 11:41:10 -0800 | [diff] [blame] | 151 | */ |
| Ben Gilad | 03292d4 | 2014-01-16 15:06:16 -0800 | [diff] [blame] | 152 | void setSelectors(Set<ICallServiceSelector> selectors) { |
| Ben Gilad | b59769e | 2014-01-16 11:41:10 -0800 | [diff] [blame] | 153 | ThreadUtil.checkOnMainThread(); |
| 154 | |
| Ben Gilad | 134cf09 | 2014-01-16 18:26:12 -0800 | [diff] [blame] | 155 | // 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 Gilad | b59769e | 2014-01-16 11:41:10 -0800 | [diff] [blame] | 159 | mSelectors = selectors; |
| Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 160 | processNewOutgoingCalls(); |
| Ben Gilad | b59769e | 2014-01-16 11:41:10 -0800 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | /** |
| Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 164 | * Handles the case where an outgoing call has been successfully placed, |
| 165 | * see {@link OutgoingCallProcessor}. |
| Ben Gilad | b59769e | 2014-01-16 11:41:10 -0800 | [diff] [blame] | 166 | */ |
| Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 167 | void handleSuccessfulOutgoingCall(Call call) { |
| Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 168 | mCallsManager.handleSuccessfulOutgoingCall(call); |
| Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 169 | |
| 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 Gilad | 2313e62 | 2014-02-06 12:02:25 -0800 | [diff] [blame] | 186 | * @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 Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 213 | * 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 Gilad | 0407fb2 | 2014-01-09 16:18:41 -0800 | [diff] [blame] | 229 | } |
| Ben Gilad | 9f2bed3 | 2013-12-12 17:43:26 -0800 | [diff] [blame] | 230 | } |
| 231 | |
| Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 232 | /** |
| Ben Gilad | b59769e | 2014-01-16 11:41:10 -0800 | [diff] [blame] | 233 | * Attempts to place the specified call. |
| 234 | * |
| Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 235 | * @param call The call to place. |
| Ben Gilad | b59769e | 2014-01-16 11:41:10 -0800 | [diff] [blame] | 236 | */ |
| Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 237 | private void processNewOutgoingCall(Call call) { |
| Ben Gilad | b59769e | 2014-01-16 11:41:10 -0800 | [diff] [blame] | 238 | |
| Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 239 | Preconditions.checkNotNull(mCallServices); |
| 240 | Preconditions.checkNotNull(mSelectors); |
| Ben Gilad | b59769e | 2014-01-16 11:41:10 -0800 | [diff] [blame] | 241 | |
| Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 242 | // 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 Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 247 | mOutgoingCallsManager.placeCall(call, mCallServices, selectors); |
| Ben Gilad | b59769e | 2014-01-16 11:41:10 -0800 | [diff] [blame] | 248 | } |
| 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 Gilad | 9f2bed3 | 2013-12-12 17:43:26 -0800 | [diff] [blame] | 260 | } |