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; |
| 25 | import android.os.RemoteException; |
| 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 | import android.util.Log; |
| 29 | |
Ben Gilad | 9f2bed3 | 2013-12-12 17:43:26 -0800 | [diff] [blame] | 30 | import com.android.telecomm.exceptions.CallServiceUnavailableException; |
| 31 | import com.android.telecomm.exceptions.OutgoingCallException; |
| 32 | |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame^] | 33 | import java.util.ArrayList; |
Ben Gilad | b59769e | 2014-01-16 11:41:10 -0800 | [diff] [blame] | 34 | import java.util.Collection; |
Ben Gilad | 9f2bed3 | 2013-12-12 17:43:26 -0800 | [diff] [blame] | 35 | import java.util.List; |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame^] | 36 | import java.util.Map; |
Ben Gilad | 0407fb2 | 2014-01-09 16:18:41 -0800 | [diff] [blame] | 37 | import java.util.Set; |
Ben Gilad | 9f2bed3 | 2013-12-12 17:43:26 -0800 | [diff] [blame] | 38 | |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 39 | /** |
| 40 | * Switchboard is responsible for (1) selecting the {@link ICallService} through which to make |
| 41 | * outgoing calls and (2) switching active calls between transports (each ICallService is |
| 42 | * considered a different transport type). |
| 43 | * TODO(santoscordon): Need to add comments on the switchboard optimizer once that it is place. |
Ben Gilad | 0407fb2 | 2014-01-09 16:18:41 -0800 | [diff] [blame] | 44 | * TODO(gilad): Add a monitor thread to wake up periodically and check for stale pending calls |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame^] | 45 | * that may need to be terminated, see mNewOutgoingCalls and mPendingOutgoingCalls. |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 46 | */ |
Ben Gilad | d17443c | 2014-01-06 11:04:15 -0800 | [diff] [blame] | 47 | final class Switchboard { |
Ben Gilad | 2495d57 | 2014-01-09 17:26:19 -0800 | [diff] [blame] | 48 | |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 49 | private static final String TAG = Switchboard.class.getSimpleName(); |
Ben Gilad | 9f2bed3 | 2013-12-12 17:43:26 -0800 | [diff] [blame] | 50 | |
Ben Gilad | b59769e | 2014-01-16 11:41:10 -0800 | [diff] [blame] | 51 | private CallServiceFinder mCallServiceFinder = new CallServiceFinder(this); |
| 52 | |
| 53 | private CallServiceSelectorFinder mSelectorFinder = new CallServiceSelectorFinder(this); |
| 54 | |
Ben Gilad | 03292d4 | 2014-01-16 15:06:16 -0800 | [diff] [blame] | 55 | /** |
| 56 | * The set of currently available call-service implementations, see {@link CallServiceFinder}. |
| 57 | * TODO(gilad): Null out once the active-call count goes to zero. |
| 58 | */ |
| 59 | private Set<ICallService> mCallServices; |
Ben Gilad | b59769e | 2014-01-16 11:41:10 -0800 | [diff] [blame] | 60 | |
Ben Gilad | 03292d4 | 2014-01-16 15:06:16 -0800 | [diff] [blame] | 61 | /** |
| 62 | * The set of currently available call-service-selector implementations, |
| 63 | * see {@link CallServiceSelectorFinder}. |
| 64 | * TODO(gilad): Null out once the active-call count goes to zero. |
| 65 | */ |
| 66 | private Set<ICallServiceSelector> mSelectors; |
Ben Gilad | 0407fb2 | 2014-01-09 16:18:41 -0800 | [diff] [blame] | 67 | |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame^] | 68 | private Set<Call> mNewOutgoingCalls = Sets.newLinkedHashSet(); |
| 69 | |
| 70 | private Set<Call> mPendingOutgoingCalls = Sets.newLinkedHashSet(); |
| 71 | |
| 72 | private Map<Call, OutgoingCallProcessor> outgoingCallProcessors = Maps.newHashMap(); |
Ben Gilad | d17443c | 2014-01-06 11:04:15 -0800 | [diff] [blame] | 73 | |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 74 | /** |
| 75 | * Places an outgoing call to the handle passed in. Method asynchronously collects |
| 76 | * {@link ICallService} implementations and passes them along with the handle and contactInfo |
| 77 | * to {@link #placeOutgoingCallInternal} to actually place the call. |
Ben Gilad | b59769e | 2014-01-16 11:41:10 -0800 | [diff] [blame] | 78 | * TODO(gilad): Update. |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 79 | * |
Ben Gilad | 0407fb2 | 2014-01-09 16:18:41 -0800 | [diff] [blame] | 80 | * @param handle The handle to dial. |
| 81 | * @param contactInfo Information about the entity being called. |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 82 | * @param context The application context. |
| 83 | */ |
Ben Gilad | d17443c | 2014-01-06 11:04:15 -0800 | [diff] [blame] | 84 | void placeOutgoingCall(String handle, ContactInfo contactInfo, Context context) { |
Evan Charlton | 0958f53 | 2014-01-10 16:58:02 -0800 | [diff] [blame] | 85 | ThreadUtil.checkOnMainThread(); |
Ben Gilad | b59769e | 2014-01-16 11:41:10 -0800 | [diff] [blame] | 86 | |
| 87 | // TODO(gilad): Consider creating the call object even earlier, e.g. in CallsManager. |
| 88 | Call call = new Call(handle, contactInfo); |
| 89 | boolean bailout = false; |
| 90 | if (isNullOrEmpty(mCallServices)) { |
| 91 | mCallServiceFinder.initiateLookup(context); |
| 92 | bailout = true; |
| 93 | } |
| 94 | if (isNullOrEmpty(mSelectors)) { |
| 95 | mSelectorFinder.initiateLookup(context); |
| 96 | bailout = true; |
| 97 | } |
| 98 | |
| 99 | if (bailout) { |
| 100 | // Unable to process the call without either call service, selectors, or both. |
| 101 | // Store the call for deferred processing and bail out. |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame^] | 102 | mNewOutgoingCalls.add(call); |
Ben Gilad | b59769e | 2014-01-16 11:41:10 -0800 | [diff] [blame] | 103 | return; |
| 104 | } |
| 105 | |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame^] | 106 | processNewOutgoingCall(call); |
Ben Gilad | 0407fb2 | 2014-01-09 16:18:41 -0800 | [diff] [blame] | 107 | } |
Ben Gilad | 9f2bed3 | 2013-12-12 17:43:26 -0800 | [diff] [blame] | 108 | |
Ben Gilad | 0407fb2 | 2014-01-09 16:18:41 -0800 | [diff] [blame] | 109 | /** |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame^] | 110 | * Persists the specified set of call services and attempts to place any pending outgoing |
| 111 | * calls. Intended to be invoked by {@link CallServiceFinder} exclusively. |
Ben Gilad | 0407fb2 | 2014-01-09 16:18:41 -0800 | [diff] [blame] | 112 | * |
Ben Gilad | 03292d4 | 2014-01-16 15:06:16 -0800 | [diff] [blame] | 113 | * @param callServices The potentially-partial set of call services. Partial since the lookup |
| 114 | * process is time-boxed, such that some providers/call-services may be slow to respond and |
| 115 | * hence effectively omitted from the specified list. |
Ben Gilad | 0407fb2 | 2014-01-09 16:18:41 -0800 | [diff] [blame] | 116 | */ |
Ben Gilad | 03292d4 | 2014-01-16 15:06:16 -0800 | [diff] [blame] | 117 | void setCallServices(Set<ICallService> callServices) { |
Evan Charlton | 0958f53 | 2014-01-10 16:58:02 -0800 | [diff] [blame] | 118 | ThreadUtil.checkOnMainThread(); |
Ben Gilad | 0407fb2 | 2014-01-09 16:18:41 -0800 | [diff] [blame] | 119 | |
Ben Gilad | b59769e | 2014-01-16 11:41:10 -0800 | [diff] [blame] | 120 | mCallServices = callServices; |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame^] | 121 | processNewOutgoingCalls(); |
Ben Gilad | b59769e | 2014-01-16 11:41:10 -0800 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | /** |
| 125 | * 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^] | 126 | * calls. Intended to be invoked by {@link CallServiceSelectorFinder} exclusively. |
Ben Gilad | b59769e | 2014-01-16 11:41:10 -0800 | [diff] [blame] | 127 | * |
Ben Gilad | 03292d4 | 2014-01-16 15:06:16 -0800 | [diff] [blame] | 128 | * @param selectors The potentially-partial set of selectors. Partial since the lookup |
| 129 | * procedure is time-boxed such that some selectors may be slow to respond and hence |
| 130 | * effectively omitted from the specified set. |
Ben Gilad | b59769e | 2014-01-16 11:41:10 -0800 | [diff] [blame] | 131 | */ |
Ben Gilad | 03292d4 | 2014-01-16 15:06:16 -0800 | [diff] [blame] | 132 | void setSelectors(Set<ICallServiceSelector> selectors) { |
Ben Gilad | b59769e | 2014-01-16 11:41:10 -0800 | [diff] [blame] | 133 | ThreadUtil.checkOnMainThread(); |
| 134 | |
Ben Gilad | 134cf09 | 2014-01-16 18:26:12 -0800 | [diff] [blame] | 135 | // TODO(gilad): Add logic to include the built-in selectors (e.g. for dealing with |
| 136 | // emergency calls) and order the entire set prior to the assignment below. If the |
| 137 | // built-in selectors can be implemented in a manner that does not require binding, |
| 138 | // 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] | 139 | mSelectors = selectors; |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame^] | 140 | processNewOutgoingCalls(); |
Ben Gilad | b59769e | 2014-01-16 11:41:10 -0800 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | /** |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame^] | 144 | * Handles the case where an outgoing call has been successfully placed, |
| 145 | * see {@link OutgoingCallProcessor}. |
Ben Gilad | b59769e | 2014-01-16 11:41:10 -0800 | [diff] [blame] | 146 | */ |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame^] | 147 | void handleSuccessfulOutgoingCall(Call call) { |
| 148 | // TODO(gilad): More here. |
| 149 | |
| 150 | // Process additional (new) calls, if any. |
| 151 | processNewOutgoingCalls(); |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * Handles the case where an outgoing call could not be proceed by any of the |
| 156 | * selector/call-service implementations, see {@link OutgoingCallProcessor}. |
| 157 | */ |
| 158 | void handleFailedOutgoingCall(Call call) { |
| 159 | // TODO(gilad): More here. |
| 160 | |
| 161 | // Process additional (new) calls, if any. |
| 162 | processNewOutgoingCalls(); |
| 163 | } |
| 164 | |
| 165 | /** |
| 166 | * Attempts to process the next new outgoing calls that have not yet been expired. |
| 167 | */ |
| 168 | private void processNewOutgoingCalls() { |
| 169 | if (isNullOrEmpty(mCallServices) || isNullOrEmpty(mSelectors)) { |
| 170 | // At least one call service and one selector are required to process outgoing calls. |
| 171 | return; |
| 172 | } |
| 173 | |
| 174 | if (!mNewOutgoingCalls.isEmpty()) { |
| 175 | Call call = mNewOutgoingCalls.iterator().next(); |
| 176 | mNewOutgoingCalls.remove(call); |
| 177 | mPendingOutgoingCalls.add(call); |
| 178 | |
| 179 | // Specifically only attempt to place one call at a time such that call services |
| 180 | // can be freed from needing to deal with concurrent requests. |
| 181 | processNewOutgoingCall(call); |
Ben Gilad | 0407fb2 | 2014-01-09 16:18:41 -0800 | [diff] [blame] | 182 | } |
Ben Gilad | 9f2bed3 | 2013-12-12 17:43:26 -0800 | [diff] [blame] | 183 | } |
| 184 | |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 185 | /** |
Ben Gilad | b59769e | 2014-01-16 11:41:10 -0800 | [diff] [blame] | 186 | * Attempts to place the specified call. |
| 187 | * |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame^] | 188 | * @param call The call to place. |
Ben Gilad | b59769e | 2014-01-16 11:41:10 -0800 | [diff] [blame] | 189 | */ |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame^] | 190 | private void processNewOutgoingCall(Call call) { |
Ben Gilad | b59769e | 2014-01-16 11:41:10 -0800 | [diff] [blame] | 191 | |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame^] | 192 | Preconditions.checkNotNull(mCallServices); |
| 193 | Preconditions.checkNotNull(mSelectors); |
Ben Gilad | b59769e | 2014-01-16 11:41:10 -0800 | [diff] [blame] | 194 | |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame^] | 195 | // Convert to (duplicate-free) list to aid index-based iteration, see the comment under |
| 196 | // setSelectors regarding using LinkedHashSet instead. |
| 197 | List<ICallServiceSelector> selectors = Lists.newArrayList(); |
| 198 | selectors.addAll(mSelectors); |
| 199 | |
| 200 | // Create the processor for this (outgoing) call and store it in a map such that call |
| 201 | // attempts can be aborted etc. |
| 202 | // TODO(gilad): Consider passing mSelector as an immutable set. |
| 203 | OutgoingCallProcessor processor = |
| 204 | new OutgoingCallProcessor(call, mCallServices, selectors, this); |
| 205 | |
| 206 | outgoingCallProcessors.put(call, processor); |
| 207 | processor.process(); |
Ben Gilad | b59769e | 2014-01-16 11:41:10 -0800 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | /** |
| 211 | * Determines whether or not the specified collection is either null or empty. |
| 212 | * |
| 213 | * @param collection Either null or the collection object to be evaluated. |
| 214 | * @return True if the collection is null or empty. |
| 215 | */ |
| 216 | @SuppressWarnings("rawtypes") |
| 217 | private boolean isNullOrEmpty(Collection collection) { |
| 218 | return collection == null || collection.isEmpty(); |
| 219 | } |
| 220 | |
| 221 | /** |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 222 | * Places an outgoing call to the handle passed in. Given a list of {@link ICallServices}, |
| 223 | * select one and place a call to the handle. |
| 224 | * TODO(santoscordon): How does the CallService selection process work? |
Ben Gilad | d17443c | 2014-01-06 11:04:15 -0800 | [diff] [blame] | 225 | * TODO(gilad): Wire this logic from CallServiceFinder.updateSwitchboard. |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 226 | * |
| 227 | * @param handle The handle to dial. |
| 228 | * @param contactInfo Information about the entity being called. |
| 229 | * @param callServices The list of available {@link ICallService}s. |
| 230 | */ |
Ben Gilad | d17443c | 2014-01-06 11:04:15 -0800 | [diff] [blame] | 231 | // private void placeOutgoingCallInternal( |
| 232 | // String handle, |
| 233 | // ContactInfo contactInfo, |
| 234 | // List<ICallService> callServices) throws CallServiceUnavailableException { |
| 235 | // |
| 236 | // Log.i(TAG, "Placing and outgoing call."); |
| 237 | // |
| 238 | // if (callServices.isEmpty()) { |
| 239 | // // No call services, bail out. |
| 240 | // // TODO(contacts-team): Add logging? |
| 241 | // // TODO(santoscordon): Does this actually go anywhere considering this method is now |
| 242 | // // asynchronous? |
| 243 | // throw new CallServiceUnavailableException("No CallService found."); |
| 244 | // } |
| 245 | // |
| 246 | // List<ICallService> compatibleCallServices = Lists.newArrayList(); |
| 247 | // for (ICallService service : callServices) { |
| 248 | // // TODO(santoscordon): This code needs to be updated to an asynchronous response |
| 249 | // // callback from isCompatibleWith(). |
| 250 | // /* if (service.isCompatibleWith(handle)) { |
| 251 | // // NOTE(android-contacts): If we end up taking the liberty to issue |
| 252 | // // calls not using the explicit user input (in case one is provided) |
| 253 | // // and instead pull an alternative method of communication from the |
| 254 | // // specified user-info object, it may be desirable to give precedence |
| 255 | // // to services that can in fact respect the user's intent. |
| 256 | // compatibleCallServices.add(service); |
| 257 | // } |
| 258 | // */ |
| 259 | // } |
| 260 | // |
| 261 | // if (compatibleCallServices.isEmpty()) { |
| 262 | // // None of the available call services is suitable for making this call. |
| 263 | // // TODO(contacts-team): Same here re logging. |
| 264 | // throw new CallServiceUnavailableException("No compatible CallService found."); |
| 265 | // } |
| 266 | // |
| 267 | // // NOTE(android-team): At this point we can also prompt the user for |
| 268 | // // preference, i.e. instead of the logic just below. |
| 269 | // if (compatibleCallServices.size() > 1) { |
| 270 | // compatibleCallServices = sort(compatibleCallServices); |
| 271 | // } |
| 272 | // for (ICallService service : compatibleCallServices) { |
| 273 | // try { |
| 274 | // service.call(handle); |
| 275 | // return; |
| 276 | // } catch (RemoteException e) { |
| 277 | // // TODO(santoscordon): Need some proxy for ICallService so that we don't have to |
| 278 | // // avoid RemoteExceptionHandling everywhere. Take a look at how InputMethodService |
| 279 | // // handles this. |
| 280 | // } |
| 281 | // // catch (OutgoingCallException ignored) { |
| 282 | // // TODO(santoscordon): Figure out how OutgoingCallException falls into this. Should |
| 283 | // // RemoteExceptions also be converted to OutgoingCallExceptions thrown by call()? |
| 284 | // } |
| 285 | // } |
Ben Gilad | 9f2bed3 | 2013-12-12 17:43:26 -0800 | [diff] [blame] | 286 | } |