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