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 | |
Evan Charlton | ac1aa9e | 2014-01-03 13:47:12 -0800 | [diff] [blame] | 19 | import com.google.common.collect.Lists; |
Ben Gilad | 0407fb2 | 2014-01-09 16:18:41 -0800 | [diff] [blame] | 20 | import com.google.common.collect.Sets; |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 21 | |
| 22 | import android.content.Context; |
| 23 | import android.os.RemoteException; |
| 24 | import android.telecomm.ICallService; |
Ben Gilad | b59769e | 2014-01-16 11:41:10 -0800 | [diff] [blame] | 25 | import android.telecomm.ICallServiceSelector; |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 26 | import android.util.Log; |
| 27 | |
Ben Gilad | 9f2bed3 | 2013-12-12 17:43:26 -0800 | [diff] [blame] | 28 | import com.android.telecomm.exceptions.CallServiceUnavailableException; |
| 29 | import com.android.telecomm.exceptions.OutgoingCallException; |
| 30 | |
Ben Gilad | b59769e | 2014-01-16 11:41:10 -0800 | [diff] [blame] | 31 | import java.util.Collection; |
Ben Gilad | 9f2bed3 | 2013-12-12 17:43:26 -0800 | [diff] [blame] | 32 | import java.util.List; |
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). |
| 39 | * 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] | 40 | * TODO(gilad): Add a monitor thread to wake up periodically and check for stale pending calls |
| 41 | * that may need to be terminated, see mPendingOutgoingCalls. |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 42 | */ |
Ben Gilad | d17443c | 2014-01-06 11:04:15 -0800 | [diff] [blame] | 43 | final class Switchboard { |
Ben Gilad | 2495d57 | 2014-01-09 17:26:19 -0800 | [diff] [blame] | 44 | |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 45 | private static final String TAG = Switchboard.class.getSimpleName(); |
Ben Gilad | 9f2bed3 | 2013-12-12 17:43:26 -0800 | [diff] [blame] | 46 | |
Ben Gilad | b59769e | 2014-01-16 11:41:10 -0800 | [diff] [blame] | 47 | private CallServiceFinder mCallServiceFinder = new CallServiceFinder(this); |
| 48 | |
| 49 | private CallServiceSelectorFinder mSelectorFinder = new CallServiceSelectorFinder(this); |
| 50 | |
Ben Gilad | 03292d4 | 2014-01-16 15:06:16 -0800 | [diff] [blame] | 51 | /** |
| 52 | * The set of currently available call-service implementations, see {@link CallServiceFinder}. |
| 53 | * TODO(gilad): Null out once the active-call count goes to zero. |
| 54 | */ |
| 55 | private Set<ICallService> mCallServices; |
Ben Gilad | b59769e | 2014-01-16 11:41:10 -0800 | [diff] [blame] | 56 | |
Ben Gilad | 03292d4 | 2014-01-16 15:06:16 -0800 | [diff] [blame] | 57 | /** |
| 58 | * The set of currently available call-service-selector implementations, |
| 59 | * see {@link CallServiceSelectorFinder}. |
| 60 | * TODO(gilad): Null out once the active-call count goes to zero. |
| 61 | */ |
| 62 | private Set<ICallServiceSelector> mSelectors; |
Ben Gilad | 0407fb2 | 2014-01-09 16:18:41 -0800 | [diff] [blame] | 63 | |
| 64 | private Set<Call> mPendingOutgoingCalls = Sets.newHashSet(); |
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 | /** |
| 67 | * Places an outgoing call to the handle passed in. Method asynchronously collects |
| 68 | * {@link ICallService} implementations and passes them along with the handle and contactInfo |
| 69 | * to {@link #placeOutgoingCallInternal} to actually place the call. |
Ben Gilad | b59769e | 2014-01-16 11:41:10 -0800 | [diff] [blame] | 70 | * TODO(gilad): Update. |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 71 | * |
Ben Gilad | 0407fb2 | 2014-01-09 16:18:41 -0800 | [diff] [blame] | 72 | * @param handle The handle to dial. |
| 73 | * @param contactInfo Information about the entity being called. |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 74 | * @param context The application context. |
| 75 | */ |
Ben Gilad | d17443c | 2014-01-06 11:04:15 -0800 | [diff] [blame] | 76 | void placeOutgoingCall(String handle, ContactInfo contactInfo, Context context) { |
Evan Charlton | 0958f53 | 2014-01-10 16:58:02 -0800 | [diff] [blame] | 77 | ThreadUtil.checkOnMainThread(); |
Ben Gilad | b59769e | 2014-01-16 11:41:10 -0800 | [diff] [blame] | 78 | |
| 79 | // TODO(gilad): Consider creating the call object even earlier, e.g. in CallsManager. |
| 80 | Call call = new Call(handle, contactInfo); |
| 81 | boolean bailout = false; |
| 82 | if (isNullOrEmpty(mCallServices)) { |
| 83 | mCallServiceFinder.initiateLookup(context); |
| 84 | bailout = true; |
| 85 | } |
| 86 | if (isNullOrEmpty(mSelectors)) { |
| 87 | mSelectorFinder.initiateLookup(context); |
| 88 | bailout = true; |
| 89 | } |
| 90 | |
| 91 | if (bailout) { |
| 92 | // Unable to process the call without either call service, selectors, or both. |
| 93 | // Store the call for deferred processing and bail out. |
| 94 | mPendingOutgoingCalls.add(call); |
| 95 | return; |
| 96 | } |
| 97 | |
| 98 | placeOutgoingCall(call); |
Ben Gilad | 0407fb2 | 2014-01-09 16:18:41 -0800 | [diff] [blame] | 99 | } |
Ben Gilad | 9f2bed3 | 2013-12-12 17:43:26 -0800 | [diff] [blame] | 100 | |
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 | * Persists the specified set of call services and attempts to connect any pending outgoing |
| 103 | * calls (still waiting for a matching call-service to be initiated). Intended to be called |
| 104 | * by {@link CallServiceFinder} exclusively. |
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 | * @param callServices The potentially-partial set of call services. Partial since the lookup |
| 107 | * process is time-boxed, such that some providers/call-services may be slow to respond and |
| 108 | * hence effectively omitted from the specified list. |
Ben Gilad | 0407fb2 | 2014-01-09 16:18:41 -0800 | [diff] [blame] | 109 | */ |
Ben Gilad | 03292d4 | 2014-01-16 15:06:16 -0800 | [diff] [blame] | 110 | void setCallServices(Set<ICallService> callServices) { |
Evan Charlton | 0958f53 | 2014-01-10 16:58:02 -0800 | [diff] [blame] | 111 | ThreadUtil.checkOnMainThread(); |
Ben Gilad | 0407fb2 | 2014-01-09 16:18:41 -0800 | [diff] [blame] | 112 | |
Ben Gilad | b59769e | 2014-01-16 11:41:10 -0800 | [diff] [blame] | 113 | mCallServices = callServices; |
| 114 | processPendingOutgoingCalls(); |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Persists the specified list of selectors and attempts to connect any pending outgoing |
| 119 | * calls. Intended to be called by {@link CallServiceSelectorFinder} exclusively. |
| 120 | * |
Ben Gilad | 03292d4 | 2014-01-16 15:06:16 -0800 | [diff] [blame] | 121 | * @param selectors The potentially-partial set of selectors. Partial since the lookup |
| 122 | * procedure is time-boxed such that some selectors may be slow to respond and hence |
| 123 | * effectively omitted from the specified set. |
Ben Gilad | b59769e | 2014-01-16 11:41:10 -0800 | [diff] [blame] | 124 | */ |
Ben Gilad | 03292d4 | 2014-01-16 15:06:16 -0800 | [diff] [blame] | 125 | void setSelectors(Set<ICallServiceSelector> selectors) { |
Ben Gilad | b59769e | 2014-01-16 11:41:10 -0800 | [diff] [blame] | 126 | ThreadUtil.checkOnMainThread(); |
| 127 | |
Ben Gilad | 134cf09 | 2014-01-16 18:26:12 -0800 | [diff] [blame] | 128 | // TODO(gilad): Add logic to include the built-in selectors (e.g. for dealing with |
| 129 | // emergency calls) and order the entire set prior to the assignment below. If the |
| 130 | // built-in selectors can be implemented in a manner that does not require binding, |
| 131 | // 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] | 132 | mSelectors = selectors; |
| 133 | processPendingOutgoingCalls(); |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Attempts to process any pending outgoing calls that have not yet been expired. |
| 138 | */ |
| 139 | void processPendingOutgoingCalls() { |
| 140 | for (Call call : mPendingOutgoingCalls) { |
| 141 | placeOutgoingCall(call); |
Ben Gilad | 0407fb2 | 2014-01-09 16:18:41 -0800 | [diff] [blame] | 142 | } |
Ben Gilad | 9f2bed3 | 2013-12-12 17:43:26 -0800 | [diff] [blame] | 143 | } |
| 144 | |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 145 | /** |
Ben Gilad | b59769e | 2014-01-16 11:41:10 -0800 | [diff] [blame] | 146 | * Attempts to place the specified call. |
| 147 | * |
| 148 | * @param call The call to put through. |
| 149 | */ |
| 150 | private void placeOutgoingCall(Call call) { |
| 151 | if (isNullOrEmpty(mCallServices) || isNullOrEmpty(mSelectors)) { |
| 152 | // At least one call service and one selector are required to process outgoing calls. |
| 153 | return; |
| 154 | } |
| 155 | |
| 156 | // TODO(gilad): Iterate through the prioritized list of selectors passing to each selector |
| 157 | // (read-only versions of) the call object and all available call services. Break out once |
| 158 | // a matching selector is found. Calls with no matching selectors will eventually be killed |
| 159 | // by the cleanup/monitor thread, see the "monitor" to-do at the top of the file. |
| 160 | |
| 161 | // Psuedo code (assuming connect to be a future switchboard method): |
| 162 | // |
| 163 | // FOR selector IN prioritizedSelectors: |
| 164 | // prioritizedCallServices = selector.select(mCallServices, call) |
| 165 | // IF notEmpty(prioritizedCallServices): |
| 166 | // FOR callService IN prioritizedCallServices: |
| 167 | // TRY |
| 168 | // connect(call, callService, selector) |
| 169 | // mPendingOutgoingCalls.remove(call) |
| 170 | // BREAK |
| 171 | } |
| 172 | |
| 173 | /** |
| 174 | * Determines whether or not the specified collection is either null or empty. |
| 175 | * |
| 176 | * @param collection Either null or the collection object to be evaluated. |
| 177 | * @return True if the collection is null or empty. |
| 178 | */ |
| 179 | @SuppressWarnings("rawtypes") |
| 180 | private boolean isNullOrEmpty(Collection collection) { |
| 181 | return collection == null || collection.isEmpty(); |
| 182 | } |
| 183 | |
| 184 | /** |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 185 | * Places an outgoing call to the handle passed in. Given a list of {@link ICallServices}, |
| 186 | * select one and place a call to the handle. |
| 187 | * TODO(santoscordon): How does the CallService selection process work? |
Ben Gilad | d17443c | 2014-01-06 11:04:15 -0800 | [diff] [blame] | 188 | * TODO(gilad): Wire this logic from CallServiceFinder.updateSwitchboard. |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 189 | * |
| 190 | * @param handle The handle to dial. |
| 191 | * @param contactInfo Information about the entity being called. |
| 192 | * @param callServices The list of available {@link ICallService}s. |
| 193 | */ |
Ben Gilad | d17443c | 2014-01-06 11:04:15 -0800 | [diff] [blame] | 194 | // private void placeOutgoingCallInternal( |
| 195 | // String handle, |
| 196 | // ContactInfo contactInfo, |
| 197 | // List<ICallService> callServices) throws CallServiceUnavailableException { |
| 198 | // |
| 199 | // Log.i(TAG, "Placing and outgoing call."); |
| 200 | // |
| 201 | // if (callServices.isEmpty()) { |
| 202 | // // No call services, bail out. |
| 203 | // // TODO(contacts-team): Add logging? |
| 204 | // // TODO(santoscordon): Does this actually go anywhere considering this method is now |
| 205 | // // asynchronous? |
| 206 | // throw new CallServiceUnavailableException("No CallService found."); |
| 207 | // } |
| 208 | // |
| 209 | // List<ICallService> compatibleCallServices = Lists.newArrayList(); |
| 210 | // for (ICallService service : callServices) { |
| 211 | // // TODO(santoscordon): This code needs to be updated to an asynchronous response |
| 212 | // // callback from isCompatibleWith(). |
| 213 | // /* if (service.isCompatibleWith(handle)) { |
| 214 | // // NOTE(android-contacts): If we end up taking the liberty to issue |
| 215 | // // calls not using the explicit user input (in case one is provided) |
| 216 | // // and instead pull an alternative method of communication from the |
| 217 | // // specified user-info object, it may be desirable to give precedence |
| 218 | // // to services that can in fact respect the user's intent. |
| 219 | // compatibleCallServices.add(service); |
| 220 | // } |
| 221 | // */ |
| 222 | // } |
| 223 | // |
| 224 | // if (compatibleCallServices.isEmpty()) { |
| 225 | // // None of the available call services is suitable for making this call. |
| 226 | // // TODO(contacts-team): Same here re logging. |
| 227 | // throw new CallServiceUnavailableException("No compatible CallService found."); |
| 228 | // } |
| 229 | // |
| 230 | // // NOTE(android-team): At this point we can also prompt the user for |
| 231 | // // preference, i.e. instead of the logic just below. |
| 232 | // if (compatibleCallServices.size() > 1) { |
| 233 | // compatibleCallServices = sort(compatibleCallServices); |
| 234 | // } |
| 235 | // for (ICallService service : compatibleCallServices) { |
| 236 | // try { |
| 237 | // service.call(handle); |
| 238 | // return; |
| 239 | // } catch (RemoteException e) { |
| 240 | // // TODO(santoscordon): Need some proxy for ICallService so that we don't have to |
| 241 | // // avoid RemoteExceptionHandling everywhere. Take a look at how InputMethodService |
| 242 | // // handles this. |
| 243 | // } |
| 244 | // // catch (OutgoingCallException ignored) { |
| 245 | // // TODO(santoscordon): Figure out how OutgoingCallException falls into this. Should |
| 246 | // // RemoteExceptions also be converted to OutgoingCallExceptions thrown by call()? |
| 247 | // } |
| 248 | // } |
Ben Gilad | 9f2bed3 | 2013-12-12 17:43:26 -0800 | [diff] [blame] | 249 | } |