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 | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 21 | import com.google.common.collect.Maps; |
Ben Gilad | 0407fb2 | 2014-01-09 16:18:41 -0800 | [diff] [blame] | 22 | import com.google.common.collect.Sets; |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 23 | |
| 24 | import android.content.Context; |
Ben Gilad | 2313e62 | 2014-02-06 12:02:25 -0800 | [diff] [blame] | 25 | import android.os.Handler; |
| 26 | import android.os.Looper; |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 27 | import android.telecomm.ICallService; |
Ben Gilad | b59769e | 2014-01-16 11:41:10 -0800 | [diff] [blame] | 28 | import android.telecomm.ICallServiceSelector; |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 29 | |
Ben Gilad | b59769e | 2014-01-16 11:41:10 -0800 | [diff] [blame] | 30 | import java.util.Collection; |
Ben Gilad | 9f2bed3 | 2013-12-12 17:43:26 -0800 | [diff] [blame] | 31 | import java.util.List; |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 32 | import java.util.Map; |
Ben Gilad | 0407fb2 | 2014-01-09 16:18:41 -0800 | [diff] [blame] | 33 | import java.util.Set; |
Ben Gilad | 9f2bed3 | 2013-12-12 17:43:26 -0800 | [diff] [blame] | 34 | |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 35 | /** |
| 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 Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 39 | */ |
Ben Gilad | d17443c | 2014-01-06 11:04:15 -0800 | [diff] [blame] | 40 | final class Switchboard { |
Ben Gilad | 2495d57 | 2014-01-09 17:26:19 -0800 | [diff] [blame] | 41 | |
Ben Gilad | 2313e62 | 2014-02-06 12:02:25 -0800 | [diff] [blame] | 42 | /** |
| 43 | * The frequency of invoking tick in milliseconds. |
| 44 | * TODO(gilad): May require tuning. |
| 45 | */ |
| 46 | private final static int TICK_FREQUENCY = 250; |
Ben Gilad | b59769e | 2014-01-16 11:41:10 -0800 | [diff] [blame] | 47 | |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 48 | private final CallsManager mCallsManager; |
Ben Gilad | 2313e62 | 2014-02-06 12:02:25 -0800 | [diff] [blame] | 49 | |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 50 | /** Used to place outgoing calls. */ |
| 51 | private final OutgoingCallsManager mOutgoingCallsManager; |
| 52 | |
| 53 | private CallServiceFinder mCallServiceFinder; |
| 54 | |
| 55 | private CallServiceSelectorFinder mSelectorFinder; |
Ben Gilad | 2313e62 | 2014-02-06 12:02:25 -0800 | [diff] [blame] | 56 | |
| 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 Gilad | b59769e | 2014-01-16 11:41:10 -0800 | [diff] [blame] | 78 | |
Ben Gilad | 03292d4 | 2014-01-16 15:06:16 -0800 | [diff] [blame] | 79 | /** |
| 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 Gilad | b59769e | 2014-01-16 11:41:10 -0800 | [diff] [blame] | 84 | |
Ben Gilad | 03292d4 | 2014-01-16 15:06:16 -0800 | [diff] [blame] | 85 | /** |
| 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 Gilad | 0407fb2 | 2014-01-09 16:18:41 -0800 | [diff] [blame] | 91 | |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 92 | /** |
| 93 | * Persists the specified parameters and initializes Switchboard. |
| 94 | */ |
Santos Cordon | 6242b13 | 2014-02-07 16:24:42 -0800 | [diff] [blame] | 95 | Switchboard(CallsManager callsManager) { |
| 96 | mCallsManager = callsManager; |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 97 | mOutgoingCallsManager = new OutgoingCallsManager(this); |
| 98 | mCallServiceFinder = new CallServiceFinder(this, mOutgoingCallsManager); |
| 99 | mSelectorFinder = new CallServiceSelectorFinder(this); |
| 100 | } |
Ben Gilad | d17443c | 2014-01-06 11:04:15 -0800 | [diff] [blame] | 101 | |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 102 | /** |
Ben Gilad | 8bdaa46 | 2014-02-05 12:53:19 -0800 | [diff] [blame] | 103 | * Attempts to place an outgoing call to the specified handle. |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 104 | * |
Ben Gilad | 13329fd | 2014-02-11 17:20:29 -0800 | [diff] [blame^] | 105 | * @param call The yet-to-be-connected outgoing-call object. |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 106 | * @param context The application context. |
| 107 | */ |
Ben Gilad | 13329fd | 2014-02-11 17:20:29 -0800 | [diff] [blame^] | 108 | void placeOutgoingCall(Call call, Context context) { |
Evan Charlton | 0958f53 | 2014-01-10 16:58:02 -0800 | [diff] [blame] | 109 | ThreadUtil.checkOnMainThread(); |
Ben Gilad | b59769e | 2014-01-16 11:41:10 -0800 | [diff] [blame] | 110 | |
Ben Gilad | b59769e | 2014-01-16 11:41:10 -0800 | [diff] [blame] | 111 | 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 Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 124 | mNewOutgoingCalls.add(call); |
Ben Gilad | b59769e | 2014-01-16 11:41:10 -0800 | [diff] [blame] | 125 | return; |
| 126 | } |
| 127 | |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 128 | processNewOutgoingCall(call); |
Ben Gilad | 0407fb2 | 2014-01-09 16:18:41 -0800 | [diff] [blame] | 129 | } |
Ben Gilad | 9f2bed3 | 2013-12-12 17:43:26 -0800 | [diff] [blame] | 130 | |
Ben Gilad | 0407fb2 | 2014-01-09 16:18:41 -0800 | [diff] [blame] | 131 | /** |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 132 | * 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 Gilad | 0407fb2 | 2014-01-09 16:18:41 -0800 | [diff] [blame] | 134 | * |
Ben Gilad | 03292d4 | 2014-01-16 15:06:16 -0800 | [diff] [blame] | 135 | * @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 Gilad | 0407fb2 | 2014-01-09 16:18:41 -0800 | [diff] [blame] | 138 | */ |
Ben Gilad | 03292d4 | 2014-01-16 15:06:16 -0800 | [diff] [blame] | 139 | void setCallServices(Set<ICallService> callServices) { |
Evan Charlton | 0958f53 | 2014-01-10 16:58:02 -0800 | [diff] [blame] | 140 | ThreadUtil.checkOnMainThread(); |
Ben Gilad | 0407fb2 | 2014-01-09 16:18:41 -0800 | [diff] [blame] | 141 | |
Ben Gilad | b59769e | 2014-01-16 11:41:10 -0800 | [diff] [blame] | 142 | mCallServices = callServices; |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 143 | processNewOutgoingCalls(); |
Ben Gilad | b59769e | 2014-01-16 11:41:10 -0800 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | /** |
| 147 | * 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] | 148 | * calls. Intended to be invoked by {@link CallServiceSelectorFinder} exclusively. |
Ben Gilad | b59769e | 2014-01-16 11:41:10 -0800 | [diff] [blame] | 149 | * |
Ben Gilad | 03292d4 | 2014-01-16 15:06:16 -0800 | [diff] [blame] | 150 | * @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 Gilad | b59769e | 2014-01-16 11:41:10 -0800 | [diff] [blame] | 153 | */ |
Ben Gilad | 03292d4 | 2014-01-16 15:06:16 -0800 | [diff] [blame] | 154 | void setSelectors(Set<ICallServiceSelector> selectors) { |
Ben Gilad | b59769e | 2014-01-16 11:41:10 -0800 | [diff] [blame] | 155 | ThreadUtil.checkOnMainThread(); |
| 156 | |
Ben Gilad | 134cf09 | 2014-01-16 18:26:12 -0800 | [diff] [blame] | 157 | // 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 Gilad | b59769e | 2014-01-16 11:41:10 -0800 | [diff] [blame] | 161 | mSelectors = selectors; |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 162 | processNewOutgoingCalls(); |
Ben Gilad | b59769e | 2014-01-16 11:41:10 -0800 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | /** |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 166 | * Handles the case where an outgoing call has been successfully placed, |
| 167 | * see {@link OutgoingCallProcessor}. |
Ben Gilad | b59769e | 2014-01-16 11:41:10 -0800 | [diff] [blame] | 168 | */ |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 169 | void handleSuccessfulOutgoingCall(Call call) { |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 170 | mCallsManager.handleSuccessfulOutgoingCall(call); |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 171 | |
| 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 Gilad | 2313e62 | 2014-02-06 12:02:25 -0800 | [diff] [blame] | 188 | * @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 Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 215 | * 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 Gilad | 0407fb2 | 2014-01-09 16:18:41 -0800 | [diff] [blame] | 231 | } |
Ben Gilad | 9f2bed3 | 2013-12-12 17:43:26 -0800 | [diff] [blame] | 232 | } |
| 233 | |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 234 | /** |
Ben Gilad | b59769e | 2014-01-16 11:41:10 -0800 | [diff] [blame] | 235 | * Attempts to place the specified call. |
| 236 | * |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 237 | * @param call The call to place. |
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 | private void processNewOutgoingCall(Call call) { |
Ben Gilad | b59769e | 2014-01-16 11:41:10 -0800 | [diff] [blame] | 240 | |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 241 | Preconditions.checkNotNull(mCallServices); |
| 242 | Preconditions.checkNotNull(mSelectors); |
Ben Gilad | b59769e | 2014-01-16 11:41:10 -0800 | [diff] [blame] | 243 | |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 244 | // 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 Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 249 | mOutgoingCallsManager.placeCall(call, mCallServices, selectors); |
Ben Gilad | b59769e | 2014-01-16 11:41:10 -0800 | [diff] [blame] | 250 | } |
| 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 Gilad | 9f2bed3 | 2013-12-12 17:43:26 -0800 | [diff] [blame] | 262 | } |