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