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; |
Ben Gilad | b59769e | 2014-01-16 11:41:10 -0800 | [diff] [blame] | 26 | import android.telecomm.ICallServiceSelector; |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 27 | |
Ben Gilad | b59769e | 2014-01-16 11:41:10 -0800 | [diff] [blame] | 28 | import java.util.Collection; |
Ben Gilad | 9f2bed3 | 2013-12-12 17:43:26 -0800 | [diff] [blame] | 29 | import java.util.List; |
Ben Gilad | 0407fb2 | 2014-01-09 16:18:41 -0800 | [diff] [blame] | 30 | import java.util.Set; |
Ben Gilad | 9f2bed3 | 2013-12-12 17:43:26 -0800 | [diff] [blame] | 31 | |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 32 | /** |
Santos Cordon | c195e36 | 2014-02-11 17:05:31 -0800 | [diff] [blame] | 33 | * 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 Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 36 | */ |
Ben Gilad | d17443c | 2014-01-06 11:04:15 -0800 | [diff] [blame] | 37 | final class Switchboard { |
Ben Gilad | 2495d57 | 2014-01-09 17:26:19 -0800 | [diff] [blame] | 38 | |
Ben Gilad | 2313e62 | 2014-02-06 12:02:25 -0800 | [diff] [blame] | 39 | /** |
| 40 | * The frequency of invoking tick in milliseconds. |
| 41 | * TODO(gilad): May require tuning. |
| 42 | */ |
| 43 | private final static int TICK_FREQUENCY = 250; |
Ben Gilad | b59769e | 2014-01-16 11:41:10 -0800 | [diff] [blame] | 44 | |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 45 | private final CallsManager mCallsManager; |
Ben Gilad | 2313e62 | 2014-02-06 12:02:25 -0800 | [diff] [blame] | 46 | |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 47 | /** Used to place outgoing calls. */ |
| 48 | private final OutgoingCallsManager mOutgoingCallsManager; |
| 49 | |
Santos Cordon | c195e36 | 2014-02-11 17:05:31 -0800 | [diff] [blame] | 50 | private final CallServiceRepository mCallServiceRepository; |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 51 | |
Santos Cordon | c195e36 | 2014-02-11 17:05:31 -0800 | [diff] [blame] | 52 | private final CallServiceSelectorRepository mSelectorRepository; |
Ben Gilad | 2313e62 | 2014-02-06 12:02:25 -0800 | [diff] [blame] | 53 | |
| 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 Gilad | b59769e | 2014-01-16 11:41:10 -0800 | [diff] [blame] | 75 | |
Ben Gilad | 03292d4 | 2014-01-16 15:06:16 -0800 | [diff] [blame] | 76 | /** |
Santos Cordon | c195e36 | 2014-02-11 17:05:31 -0800 | [diff] [blame] | 77 | * 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 Gilad | 03292d4 | 2014-01-16 15:06:16 -0800 | [diff] [blame] | 81 | */ |
Santos Cordon | c195e36 | 2014-02-11 17:05:31 -0800 | [diff] [blame] | 82 | private Set<CallServiceWrapper> mCallServices; |
Ben Gilad | b59769e | 2014-01-16 11:41:10 -0800 | [diff] [blame] | 83 | |
Ben Gilad | 03292d4 | 2014-01-16 15:06:16 -0800 | [diff] [blame] | 84 | /** |
| 85 | * The set of currently available call-service-selector implementations, |
Santos Cordon | c195e36 | 2014-02-11 17:05:31 -0800 | [diff] [blame] | 86 | * see {@link CallServiceSelectorRepository}. |
Ben Gilad | 03292d4 | 2014-01-16 15:06:16 -0800 | [diff] [blame] | 87 | * TODO(gilad): Null out once the active-call count goes to zero. |
| 88 | */ |
| 89 | private Set<ICallServiceSelector> mSelectors; |
Ben Gilad | 0407fb2 | 2014-01-09 16:18:41 -0800 | [diff] [blame] | 90 | |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 91 | /** |
Santos Cordon | c195e36 | 2014-02-11 17:05:31 -0800 | [diff] [blame] | 92 | * 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 Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 98 | * Persists the specified parameters and initializes Switchboard. |
| 99 | */ |
Santos Cordon | 6242b13 | 2014-02-07 16:24:42 -0800 | [diff] [blame] | 100 | Switchboard(CallsManager callsManager) { |
| 101 | mCallsManager = callsManager; |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 102 | mOutgoingCallsManager = new OutgoingCallsManager(this); |
Santos Cordon | c195e36 | 2014-02-11 17:05:31 -0800 | [diff] [blame] | 103 | mCallServiceRepository = new CallServiceRepository(this, mOutgoingCallsManager); |
| 104 | mSelectorRepository = new CallServiceSelectorRepository(this); |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 105 | } |
Ben Gilad | d17443c | 2014-01-06 11:04:15 -0800 | [diff] [blame] | 106 | |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 107 | /** |
Santos Cordon | c195e36 | 2014-02-11 17:05:31 -0800 | [diff] [blame] | 108 | * 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 Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 111 | * |
Ben Gilad | 13329fd | 2014-02-11 17:20:29 -0800 | [diff] [blame] | 112 | * @param call The yet-to-be-connected outgoing-call object. |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 113 | */ |
Santos Cordon | c195e36 | 2014-02-11 17:05:31 -0800 | [diff] [blame] | 114 | void placeOutgoingCall(Call call) { |
Evan Charlton | 0958f53 | 2014-01-10 16:58:02 -0800 | [diff] [blame] | 115 | ThreadUtil.checkOnMainThread(); |
Ben Gilad | b59769e | 2014-01-16 11:41:10 -0800 | [diff] [blame] | 116 | |
Sailesh Nepal | b6141ae | 2014-02-18 08:45:26 -0800 | [diff] [blame^] | 117 | mNewOutgoingCalls.add(call); |
Ben Gilad | b59769e | 2014-01-16 11:41:10 -0800 | [diff] [blame] | 118 | |
Santos Cordon | c195e36 | 2014-02-11 17:05:31 -0800 | [diff] [blame] | 119 | // We initialize a lookup every time because between calls the set of available call |
| 120 | // services can change between calls. |
Sailesh Nepal | b6141ae | 2014-02-18 08:45:26 -0800 | [diff] [blame^] | 121 | mLookupId++; |
Santos Cordon | c195e36 | 2014-02-11 17:05:31 -0800 | [diff] [blame] | 122 | mCallServiceRepository.initiateLookup(mLookupId); |
| 123 | mSelectorRepository.initiateLookup(mLookupId); |
Ben Gilad | 0407fb2 | 2014-01-09 16:18:41 -0800 | [diff] [blame] | 124 | } |
Ben Gilad | 9f2bed3 | 2013-12-12 17:43:26 -0800 | [diff] [blame] | 125 | |
Ben Gilad | 0407fb2 | 2014-01-09 16:18:41 -0800 | [diff] [blame] | 126 | /** |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 127 | * Persists the specified set of call services and attempts to place any pending outgoing |
Santos Cordon | c195e36 | 2014-02-11 17:05:31 -0800 | [diff] [blame] | 128 | * calls. Intended to be invoked by {@link CallServiceRepository} exclusively. |
Ben Gilad | 0407fb2 | 2014-01-09 16:18:41 -0800 | [diff] [blame] | 129 | * |
Ben Gilad | 03292d4 | 2014-01-16 15:06:16 -0800 | [diff] [blame] | 130 | * @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 Gilad | 0407fb2 | 2014-01-09 16:18:41 -0800 | [diff] [blame] | 133 | */ |
Santos Cordon | c195e36 | 2014-02-11 17:05:31 -0800 | [diff] [blame] | 134 | void setCallServices(Set<CallServiceWrapper> callServices) { |
Evan Charlton | 0958f53 | 2014-01-10 16:58:02 -0800 | [diff] [blame] | 135 | ThreadUtil.checkOnMainThread(); |
Ben Gilad | 0407fb2 | 2014-01-09 16:18:41 -0800 | [diff] [blame] | 136 | |
Ben Gilad | b59769e | 2014-01-16 11:41:10 -0800 | [diff] [blame] | 137 | mCallServices = callServices; |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 138 | processNewOutgoingCalls(); |
Ben Gilad | b59769e | 2014-01-16 11:41:10 -0800 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | /** |
| 142 | * Persists the specified list of selectors and attempts to connect any pending outgoing |
Santos Cordon | c195e36 | 2014-02-11 17:05:31 -0800 | [diff] [blame] | 143 | * calls. Intended to be invoked by {@link CallServiceSelectorRepository} exclusively. |
Ben Gilad | b59769e | 2014-01-16 11:41:10 -0800 | [diff] [blame] | 144 | * |
Ben Gilad | 03292d4 | 2014-01-16 15:06:16 -0800 | [diff] [blame] | 145 | * @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 Gilad | b59769e | 2014-01-16 11:41:10 -0800 | [diff] [blame] | 148 | */ |
Ben Gilad | 03292d4 | 2014-01-16 15:06:16 -0800 | [diff] [blame] | 149 | void setSelectors(Set<ICallServiceSelector> selectors) { |
Santos Cordon | c195e36 | 2014-02-11 17:05:31 -0800 | [diff] [blame] | 150 | // TODO(santoscordon): This should take in CallServiceSelectorWrapper instead of the direct |
| 151 | // ICallServiceSelector implementation. Copy what we have for CallServiceWrapper. |
Ben Gilad | b59769e | 2014-01-16 11:41:10 -0800 | [diff] [blame] | 152 | ThreadUtil.checkOnMainThread(); |
| 153 | |
Ben Gilad | 134cf09 | 2014-01-16 18:26:12 -0800 | [diff] [blame] | 154 | // 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 Gilad | b59769e | 2014-01-16 11:41:10 -0800 | [diff] [blame] | 158 | mSelectors = selectors; |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 159 | processNewOutgoingCalls(); |
Ben Gilad | b59769e | 2014-01-16 11:41:10 -0800 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | /** |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 163 | * Handles the case where an outgoing call has been successfully placed, |
| 164 | * see {@link OutgoingCallProcessor}. |
Ben Gilad | b59769e | 2014-01-16 11:41:10 -0800 | [diff] [blame] | 165 | */ |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 166 | void handleSuccessfulOutgoingCall(Call call) { |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 167 | mCallsManager.handleSuccessfulOutgoingCall(call); |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 168 | |
| 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 Gilad | 2313e62 | 2014-02-06 12:02:25 -0800 | [diff] [blame] | 185 | * @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 Cordon | c195e36 | 2014-02-11 17:05:31 -0800 | [diff] [blame] | 209 | // TODO(santoscordon): Clear mCallServices if there exist no more new or pending outgoing |
| 210 | // calls. |
Ben Gilad | 2313e62 | 2014-02-06 12:02:25 -0800 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | /** |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 214 | * 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 Gilad | 0407fb2 | 2014-01-09 16:18:41 -0800 | [diff] [blame] | 230 | } |
Ben Gilad | 9f2bed3 | 2013-12-12 17:43:26 -0800 | [diff] [blame] | 231 | } |
| 232 | |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 233 | /** |
Ben Gilad | b59769e | 2014-01-16 11:41:10 -0800 | [diff] [blame] | 234 | * Attempts to place the specified call. |
| 235 | * |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 236 | * @param call The call to place. |
Ben Gilad | b59769e | 2014-01-16 11:41:10 -0800 | [diff] [blame] | 237 | */ |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 238 | private void processNewOutgoingCall(Call call) { |
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 | } |