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 | |
Ben Gilad | 2313e62 | 2014-02-06 12:02:25 -0800 | [diff] [blame^] | 48 | private final CallServiceFinder mCallServiceFinder = new CallServiceFinder(this); |
| 49 | |
| 50 | private final CallServiceSelectorFinder mSelectorFinder = new CallServiceSelectorFinder(this); |
| 51 | |
| 52 | /** Used to schedule tasks on the main (UI) thread. */ |
| 53 | private final Handler mHandler = new Handler(Looper.getMainLooper()); |
| 54 | |
| 55 | /** |
| 56 | * Executes a single tick task and potentially schedules the next such that polling continues |
| 57 | * as long as necessary. |
| 58 | * NOTE(gilad): by design no two tick invocations should ever overlap. |
| 59 | */ |
| 60 | private final Runnable mTicker = new Runnable() { |
| 61 | @Override |
| 62 | public void run() { |
| 63 | tick(); |
| 64 | if (isTicking()) { |
| 65 | scheduleNextTick(); |
| 66 | } |
| 67 | } |
| 68 | }; |
| 69 | |
| 70 | private final Set<Call> mNewOutgoingCalls = Sets.newLinkedHashSet(); |
| 71 | |
| 72 | private final Set<Call> mPendingOutgoingCalls = Sets.newLinkedHashSet(); |
Ben Gilad | b59769e | 2014-01-16 11:41:10 -0800 | [diff] [blame] | 73 | |
Ben Gilad | 03292d4 | 2014-01-16 15:06:16 -0800 | [diff] [blame] | 74 | /** |
| 75 | * The set of currently available call-service implementations, see {@link CallServiceFinder}. |
| 76 | * TODO(gilad): Null out once the active-call count goes to zero. |
| 77 | */ |
| 78 | private Set<ICallService> mCallServices; |
Ben Gilad | b59769e | 2014-01-16 11:41:10 -0800 | [diff] [blame] | 79 | |
Ben Gilad | 03292d4 | 2014-01-16 15:06:16 -0800 | [diff] [blame] | 80 | /** |
| 81 | * The set of currently available call-service-selector implementations, |
| 82 | * see {@link CallServiceSelectorFinder}. |
| 83 | * TODO(gilad): Null out once the active-call count goes to zero. |
| 84 | */ |
| 85 | private Set<ICallServiceSelector> mSelectors; |
Ben Gilad | 0407fb2 | 2014-01-09 16:18:41 -0800 | [diff] [blame] | 86 | |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 87 | private Map<Call, OutgoingCallProcessor> outgoingCallProcessors = Maps.newHashMap(); |
Ben Gilad | d17443c | 2014-01-06 11:04:15 -0800 | [diff] [blame] | 88 | |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 89 | /** |
Ben Gilad | 8bdaa46 | 2014-02-05 12:53:19 -0800 | [diff] [blame] | 90 | * Attempts to place an outgoing call to the specified handle. |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 91 | * |
Ben Gilad | 0407fb2 | 2014-01-09 16:18:41 -0800 | [diff] [blame] | 92 | * @param handle The handle to dial. |
| 93 | * @param contactInfo Information about the entity being called. |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 94 | * @param context The application context. |
| 95 | */ |
Ben Gilad | d17443c | 2014-01-06 11:04:15 -0800 | [diff] [blame] | 96 | void placeOutgoingCall(String handle, ContactInfo contactInfo, Context context) { |
Evan Charlton | 0958f53 | 2014-01-10 16:58:02 -0800 | [diff] [blame] | 97 | ThreadUtil.checkOnMainThread(); |
Ben Gilad | b59769e | 2014-01-16 11:41:10 -0800 | [diff] [blame] | 98 | |
| 99 | // TODO(gilad): Consider creating the call object even earlier, e.g. in CallsManager. |
| 100 | Call call = new Call(handle, contactInfo); |
| 101 | boolean bailout = false; |
| 102 | if (isNullOrEmpty(mCallServices)) { |
| 103 | mCallServiceFinder.initiateLookup(context); |
| 104 | bailout = true; |
| 105 | } |
| 106 | if (isNullOrEmpty(mSelectors)) { |
| 107 | mSelectorFinder.initiateLookup(context); |
| 108 | bailout = true; |
| 109 | } |
| 110 | |
| 111 | if (bailout) { |
| 112 | // Unable to process the call without either call service, selectors, or both. |
| 113 | // Store the call for deferred processing and bail out. |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 114 | mNewOutgoingCalls.add(call); |
Ben Gilad | b59769e | 2014-01-16 11:41:10 -0800 | [diff] [blame] | 115 | return; |
| 116 | } |
| 117 | |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 118 | processNewOutgoingCall(call); |
Ben Gilad | 0407fb2 | 2014-01-09 16:18:41 -0800 | [diff] [blame] | 119 | } |
Ben Gilad | 9f2bed3 | 2013-12-12 17:43:26 -0800 | [diff] [blame] | 120 | |
Ben Gilad | 0407fb2 | 2014-01-09 16:18:41 -0800 | [diff] [blame] | 121 | /** |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 122 | * Persists the specified set of call services and attempts to place any pending outgoing |
| 123 | * calls. Intended to be invoked by {@link CallServiceFinder} exclusively. |
Ben Gilad | 0407fb2 | 2014-01-09 16:18:41 -0800 | [diff] [blame] | 124 | * |
Ben Gilad | 03292d4 | 2014-01-16 15:06:16 -0800 | [diff] [blame] | 125 | * @param callServices The potentially-partial set of call services. Partial since the lookup |
| 126 | * process is time-boxed, such that some providers/call-services may be slow to respond and |
| 127 | * hence effectively omitted from the specified list. |
Ben Gilad | 0407fb2 | 2014-01-09 16:18:41 -0800 | [diff] [blame] | 128 | */ |
Ben Gilad | 03292d4 | 2014-01-16 15:06:16 -0800 | [diff] [blame] | 129 | void setCallServices(Set<ICallService> callServices) { |
Evan Charlton | 0958f53 | 2014-01-10 16:58:02 -0800 | [diff] [blame] | 130 | ThreadUtil.checkOnMainThread(); |
Ben Gilad | 0407fb2 | 2014-01-09 16:18:41 -0800 | [diff] [blame] | 131 | |
Ben Gilad | b59769e | 2014-01-16 11:41:10 -0800 | [diff] [blame] | 132 | mCallServices = callServices; |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 133 | processNewOutgoingCalls(); |
Ben Gilad | b59769e | 2014-01-16 11:41:10 -0800 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | /** |
| 137 | * 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] | 138 | * calls. Intended to be invoked by {@link CallServiceSelectorFinder} exclusively. |
Ben Gilad | b59769e | 2014-01-16 11:41:10 -0800 | [diff] [blame] | 139 | * |
Ben Gilad | 03292d4 | 2014-01-16 15:06:16 -0800 | [diff] [blame] | 140 | * @param selectors The potentially-partial set of selectors. Partial since the lookup |
| 141 | * procedure is time-boxed such that some selectors may be slow to respond and hence |
| 142 | * effectively omitted from the specified set. |
Ben Gilad | b59769e | 2014-01-16 11:41:10 -0800 | [diff] [blame] | 143 | */ |
Ben Gilad | 03292d4 | 2014-01-16 15:06:16 -0800 | [diff] [blame] | 144 | void setSelectors(Set<ICallServiceSelector> selectors) { |
Ben Gilad | b59769e | 2014-01-16 11:41:10 -0800 | [diff] [blame] | 145 | ThreadUtil.checkOnMainThread(); |
| 146 | |
Ben Gilad | 134cf09 | 2014-01-16 18:26:12 -0800 | [diff] [blame] | 147 | // TODO(gilad): Add logic to include the built-in selectors (e.g. for dealing with |
| 148 | // emergency calls) and order the entire set prior to the assignment below. If the |
| 149 | // built-in selectors can be implemented in a manner that does not require binding, |
| 150 | // 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] | 151 | mSelectors = selectors; |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 152 | processNewOutgoingCalls(); |
Ben Gilad | b59769e | 2014-01-16 11:41:10 -0800 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | /** |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 156 | * Handles the case where an outgoing call has been successfully placed, |
| 157 | * see {@link OutgoingCallProcessor}. |
Ben Gilad | b59769e | 2014-01-16 11:41:10 -0800 | [diff] [blame] | 158 | */ |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 159 | void handleSuccessfulOutgoingCall(Call call) { |
| 160 | // TODO(gilad): More here. |
| 161 | |
| 162 | // Process additional (new) calls, if any. |
| 163 | processNewOutgoingCalls(); |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * Handles the case where an outgoing call could not be proceed by any of the |
| 168 | * selector/call-service implementations, see {@link OutgoingCallProcessor}. |
| 169 | */ |
| 170 | void handleFailedOutgoingCall(Call call) { |
| 171 | // TODO(gilad): More here. |
| 172 | |
| 173 | // Process additional (new) calls, if any. |
| 174 | processNewOutgoingCalls(); |
| 175 | } |
| 176 | |
| 177 | /** |
Ben Gilad | 2313e62 | 2014-02-06 12:02:25 -0800 | [diff] [blame^] | 178 | * @return True if ticking should continue (or be resumed) and false otherwise. |
| 179 | */ |
| 180 | private boolean isTicking() { |
| 181 | // TODO(gilad): return true every time at least one outgoing call is pending (i.e. waiting |
| 182 | // to be connected by a call service) and also when at least one active call is switch-able |
| 183 | // between call services, see {@link ICallServiceSelector#isSwitchable}. |
| 184 | return false; |
| 185 | } |
| 186 | |
| 187 | /** |
| 188 | * Schedules the next tick invocation. |
| 189 | */ |
| 190 | private void scheduleNextTick() { |
| 191 | mHandler.postDelayed(mTicker, TICK_FREQUENCY); |
| 192 | } |
| 193 | |
| 194 | /** |
| 195 | * Performs the set of tasks that needs to be executed on polling basis. |
| 196 | * TODO(gilad): Check for stale pending calls that may need to be terminated etc, see |
| 197 | * mNewOutgoingCalls and mPendingOutgoingCalls. |
| 198 | * TODO(gilad): Also intended to trigger the call switching/hand-off logic when applicable. |
| 199 | */ |
| 200 | private void tick() { |
| 201 | // TODO(gilad): More here. |
| 202 | } |
| 203 | |
| 204 | /** |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 205 | * Attempts to process the next new outgoing calls that have not yet been expired. |
| 206 | */ |
| 207 | private void processNewOutgoingCalls() { |
| 208 | if (isNullOrEmpty(mCallServices) || isNullOrEmpty(mSelectors)) { |
| 209 | // At least one call service and one selector are required to process outgoing calls. |
| 210 | return; |
| 211 | } |
| 212 | |
| 213 | if (!mNewOutgoingCalls.isEmpty()) { |
| 214 | Call call = mNewOutgoingCalls.iterator().next(); |
| 215 | mNewOutgoingCalls.remove(call); |
| 216 | mPendingOutgoingCalls.add(call); |
| 217 | |
| 218 | // Specifically only attempt to place one call at a time such that call services |
| 219 | // can be freed from needing to deal with concurrent requests. |
| 220 | processNewOutgoingCall(call); |
Ben Gilad | 0407fb2 | 2014-01-09 16:18:41 -0800 | [diff] [blame] | 221 | } |
Ben Gilad | 9f2bed3 | 2013-12-12 17:43:26 -0800 | [diff] [blame] | 222 | } |
| 223 | |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 224 | /** |
Ben Gilad | b59769e | 2014-01-16 11:41:10 -0800 | [diff] [blame] | 225 | * Attempts to place the specified call. |
| 226 | * |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 227 | * @param call The call to place. |
Ben Gilad | b59769e | 2014-01-16 11:41:10 -0800 | [diff] [blame] | 228 | */ |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 229 | private void processNewOutgoingCall(Call call) { |
Ben Gilad | b59769e | 2014-01-16 11:41:10 -0800 | [diff] [blame] | 230 | |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 231 | Preconditions.checkNotNull(mCallServices); |
| 232 | Preconditions.checkNotNull(mSelectors); |
Ben Gilad | b59769e | 2014-01-16 11:41:10 -0800 | [diff] [blame] | 233 | |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 234 | // Convert to (duplicate-free) list to aid index-based iteration, see the comment under |
| 235 | // setSelectors regarding using LinkedHashSet instead. |
| 236 | List<ICallServiceSelector> selectors = Lists.newArrayList(); |
| 237 | selectors.addAll(mSelectors); |
| 238 | |
| 239 | // Create the processor for this (outgoing) call and store it in a map such that call |
| 240 | // attempts can be aborted etc. |
| 241 | // TODO(gilad): Consider passing mSelector as an immutable set. |
| 242 | OutgoingCallProcessor processor = |
| 243 | new OutgoingCallProcessor(call, mCallServices, selectors, this); |
| 244 | |
| 245 | outgoingCallProcessors.put(call, processor); |
| 246 | processor.process(); |
Ben Gilad | b59769e | 2014-01-16 11:41:10 -0800 | [diff] [blame] | 247 | } |
| 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 | } |
| 259 | |
| 260 | /** |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 261 | * Places an outgoing call to the handle passed in. Given a list of {@link ICallServices}, |
| 262 | * select one and place a call to the handle. |
| 263 | * TODO(santoscordon): How does the CallService selection process work? |
Ben Gilad | d17443c | 2014-01-06 11:04:15 -0800 | [diff] [blame] | 264 | * TODO(gilad): Wire this logic from CallServiceFinder.updateSwitchboard. |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 265 | * |
| 266 | * @param handle The handle to dial. |
| 267 | * @param contactInfo Information about the entity being called. |
| 268 | * @param callServices The list of available {@link ICallService}s. |
| 269 | */ |
Ben Gilad | d17443c | 2014-01-06 11:04:15 -0800 | [diff] [blame] | 270 | // private void placeOutgoingCallInternal( |
| 271 | // String handle, |
| 272 | // ContactInfo contactInfo, |
| 273 | // List<ICallService> callServices) throws CallServiceUnavailableException { |
| 274 | // |
| 275 | // Log.i(TAG, "Placing and outgoing call."); |
| 276 | // |
| 277 | // if (callServices.isEmpty()) { |
| 278 | // // No call services, bail out. |
| 279 | // // TODO(contacts-team): Add logging? |
| 280 | // // TODO(santoscordon): Does this actually go anywhere considering this method is now |
| 281 | // // asynchronous? |
| 282 | // throw new CallServiceUnavailableException("No CallService found."); |
| 283 | // } |
| 284 | // |
| 285 | // List<ICallService> compatibleCallServices = Lists.newArrayList(); |
| 286 | // for (ICallService service : callServices) { |
| 287 | // // TODO(santoscordon): This code needs to be updated to an asynchronous response |
| 288 | // // callback from isCompatibleWith(). |
| 289 | // /* if (service.isCompatibleWith(handle)) { |
| 290 | // // NOTE(android-contacts): If we end up taking the liberty to issue |
| 291 | // // calls not using the explicit user input (in case one is provided) |
| 292 | // // and instead pull an alternative method of communication from the |
| 293 | // // specified user-info object, it may be desirable to give precedence |
| 294 | // // to services that can in fact respect the user's intent. |
| 295 | // compatibleCallServices.add(service); |
| 296 | // } |
| 297 | // */ |
| 298 | // } |
| 299 | // |
| 300 | // if (compatibleCallServices.isEmpty()) { |
| 301 | // // None of the available call services is suitable for making this call. |
| 302 | // // TODO(contacts-team): Same here re logging. |
| 303 | // throw new CallServiceUnavailableException("No compatible CallService found."); |
| 304 | // } |
| 305 | // |
| 306 | // // NOTE(android-team): At this point we can also prompt the user for |
| 307 | // // preference, i.e. instead of the logic just below. |
| 308 | // if (compatibleCallServices.size() > 1) { |
| 309 | // compatibleCallServices = sort(compatibleCallServices); |
| 310 | // } |
| 311 | // for (ICallService service : compatibleCallServices) { |
| 312 | // try { |
| 313 | // service.call(handle); |
| 314 | // return; |
| 315 | // } catch (RemoteException e) { |
| 316 | // // TODO(santoscordon): Need some proxy for ICallService so that we don't have to |
| 317 | // // avoid RemoteExceptionHandling everywhere. Take a look at how InputMethodService |
| 318 | // // handles this. |
| 319 | // } |
| 320 | // // catch (OutgoingCallException ignored) { |
| 321 | // // TODO(santoscordon): Figure out how OutgoingCallException falls into this. Should |
| 322 | // // RemoteExceptions also be converted to OutgoingCallExceptions thrown by call()? |
| 323 | // } |
| 324 | // } |
Ben Gilad | 9f2bed3 | 2013-12-12 17:43:26 -0800 | [diff] [blame] | 325 | } |