Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014, 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 | |
| 17 | package com.android.telecomm; |
| 18 | |
| 19 | import com.google.common.base.Preconditions; |
| 20 | import com.google.common.collect.Maps; |
Ben Gilad | d88323b | 2014-01-29 19:03:19 -0800 | [diff] [blame] | 21 | import com.google.common.collect.Lists; |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 22 | |
Ben Gilad | d88323b | 2014-01-29 19:03:19 -0800 | [diff] [blame] | 23 | import android.os.Handler; |
| 24 | import android.os.Looper; |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 25 | import android.os.RemoteException; |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 26 | import android.telecomm.CallState; |
Ben Gilad | c5b2269 | 2014-02-18 20:03:22 -0800 | [diff] [blame^] | 27 | import android.telecomm.CallServiceDescriptor; |
Ben Gilad | d88323b | 2014-01-29 19:03:19 -0800 | [diff] [blame] | 28 | import android.telecomm.ICallServiceSelectionResponse; |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 29 | import android.telecomm.ICallServiceSelector; |
Sailesh Nepal | b6141ae | 2014-02-18 08:45:26 -0800 | [diff] [blame] | 30 | |
| 31 | import com.android.telecomm.ServiceBinder.BindCallback; |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 32 | |
| 33 | import java.util.Iterator; |
| 34 | import java.util.List; |
| 35 | import java.util.Map; |
| 36 | import java.util.Set; |
| 37 | |
| 38 | /** |
| 39 | * Utility class to place a call using the specified set of call-services and ordered selectors. |
Ben Gilad | c5b2269 | 2014-02-18 20:03:22 -0800 | [diff] [blame^] | 40 | * Iterates through the selectors and gets a sorted list of supported call-service descriptors |
| 41 | * for each selector. Upon receiving each sorted list (one list per selector), each of the |
| 42 | * corresponding call services is then attempted until either the outgoing call is placed, the |
| 43 | * attempted call is aborted (by the switchboard), or the list is exhausted -- whichever occurs |
| 44 | * first. |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 45 | * |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 46 | * Except for the abort case, all other scenarios should terminate with the switchboard notified |
| 47 | * of the result. |
Ben Gilad | d88323b | 2014-01-29 19:03:19 -0800 | [diff] [blame] | 48 | * |
| 49 | * NOTE(gilad): Currently operating under the assumption that we'll have one timeout per (outgoing) |
| 50 | * call attempt. If we (also) like to timeout individual selectors and/or call services, the code |
| 51 | * here will need to be re-factored (quite a bit) to support that. |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 52 | */ |
| 53 | final class OutgoingCallProcessor { |
| 54 | |
| 55 | /** |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 56 | * The outgoing call this processor is tasked with placing. |
| 57 | */ |
| 58 | private final Call mCall; |
| 59 | |
| 60 | /** |
Ben Gilad | c5b2269 | 2014-02-18 20:03:22 -0800 | [diff] [blame^] | 61 | * The duplicate-free list of currently-available call-service descriptors. |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 62 | */ |
Ben Gilad | c5b2269 | 2014-02-18 20:03:22 -0800 | [diff] [blame^] | 63 | private final List<CallServiceDescriptor> mCallServiceDescriptors = Lists.newArrayList(); |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 64 | |
| 65 | /** |
Sailesh Nepal | b6141ae | 2014-02-18 08:45:26 -0800 | [diff] [blame] | 66 | * The map of currently-available call-service implementations keyed by call-service ID. |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 67 | */ |
Sailesh Nepal | b6141ae | 2014-02-18 08:45:26 -0800 | [diff] [blame] | 68 | private final Map<String, CallServiceWrapper> mCallServicesById = Maps.newHashMap(); |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 69 | |
| 70 | /** |
| 71 | * The set of currently-available call-service selector implementations. |
| 72 | */ |
| 73 | private final List<ICallServiceSelector> mSelectors; |
| 74 | |
Ben Gilad | d88323b | 2014-01-29 19:03:19 -0800 | [diff] [blame] | 75 | /** Used to run code (e.g. messages, Runnables) on the main (UI) thread. */ |
| 76 | private final Handler mHandler = new Handler(Looper.getMainLooper()); |
| 77 | |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 78 | /** Manages all outgoing call processors. */ |
| 79 | private final OutgoingCallsManager mOutgoingCallsManager; |
| 80 | |
| 81 | private final Switchboard mSwitchboard; |
| 82 | |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 83 | /** |
Ben Gilad | c5b2269 | 2014-02-18 20:03:22 -0800 | [diff] [blame^] | 84 | * The iterator over the currently-selected ordered list of call-service descriptors. |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 85 | */ |
Ben Gilad | c5b2269 | 2014-02-18 20:03:22 -0800 | [diff] [blame^] | 86 | private Iterator<CallServiceDescriptor> mCallServiceDescriptorIterator; |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 87 | |
| 88 | private Iterator<ICallServiceSelector> mSelectorIterator; |
| 89 | |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 90 | /** |
| 91 | * The last call service which we asked to place the call. If null, it indicates that there |
| 92 | * exists no call service that we expect to place this call. |
| 93 | */ |
Santos Cordon | c195e36 | 2014-02-11 17:05:31 -0800 | [diff] [blame] | 94 | private CallServiceWrapper mCallService; |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 95 | |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 96 | private boolean mIsAborted = false; |
| 97 | |
| 98 | /** |
| 99 | * Persists the specified parameters and iterates through the prioritized list of selectors |
| 100 | * passing to each selector (read-only versions of) the call object and all available call- |
Ben Gilad | c5b2269 | 2014-02-18 20:03:22 -0800 | [diff] [blame^] | 101 | * service descriptors. Stops once a matching selector is found. Calls with no matching |
| 102 | * selectors will eventually be killed by the cleanup/monitor switchboard handler, which will |
| 103 | * in turn call the abort method of this processor via {@link OutgoingCallsManager}. |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 104 | * |
| 105 | * @param call The call to place. |
| 106 | * @param callServices The available call-service implementations. |
| 107 | * @param selectors The available call-service selector implementations. |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 108 | * @param outgoingCallsManager Manager of all outgoing call processors. |
| 109 | * @param switchboard The switchboard. |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 110 | */ |
| 111 | OutgoingCallProcessor( |
| 112 | Call call, |
Santos Cordon | c195e36 | 2014-02-11 17:05:31 -0800 | [diff] [blame] | 113 | Set<CallServiceWrapper> callServices, |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 114 | List<ICallServiceSelector> selectors, |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 115 | OutgoingCallsManager outgoingCallsManager, |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 116 | Switchboard switchboard) { |
| 117 | |
Ben Gilad | d88323b | 2014-01-29 19:03:19 -0800 | [diff] [blame] | 118 | ThreadUtil.checkOnMainThread(); |
| 119 | |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 120 | Preconditions.checkNotNull(callServices); |
| 121 | Preconditions.checkNotNull(selectors); |
| 122 | |
| 123 | mCall = call; |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 124 | mSelectors = selectors; |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 125 | mOutgoingCallsManager = outgoingCallsManager; |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 126 | mSwitchboard = switchboard; |
| 127 | |
Ben Gilad | c5b2269 | 2014-02-18 20:03:22 -0800 | [diff] [blame^] | 128 | // Populate the list and map of call-service descriptors. The list is needed since |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 129 | // it's being passed down to selectors. |
Santos Cordon | c195e36 | 2014-02-11 17:05:31 -0800 | [diff] [blame] | 130 | for (CallServiceWrapper callService : callServices) { |
Ben Gilad | c5b2269 | 2014-02-18 20:03:22 -0800 | [diff] [blame^] | 131 | CallServiceDescriptor descriptor = callService.getDescriptor(); |
| 132 | mCallServiceDescriptors.add(descriptor); |
| 133 | mCallServicesById.put(descriptor.getCallServiceId(), callService); |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 134 | } |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * Initiates the attempt to place the call. No-op beyond the first invocation. |
| 139 | */ |
| 140 | void process() { |
Ben Gilad | d88323b | 2014-01-29 19:03:19 -0800 | [diff] [blame] | 141 | ThreadUtil.checkOnMainThread(); |
| 142 | |
Ben Gilad | c5b2269 | 2014-02-18 20:03:22 -0800 | [diff] [blame^] | 143 | if (mSelectors.isEmpty() || mCallServiceDescriptors.isEmpty()) { |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 144 | // TODO(gilad): Consider adding a failure message/type to differentiate the various |
| 145 | // cases, or potentially throw an exception in this case. |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 146 | mOutgoingCallsManager.handleFailedOutgoingCall(mCall); |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 147 | } else if (mSelectorIterator == null) { |
| 148 | mSelectorIterator = mSelectors.iterator(); |
| 149 | attemptNextSelector(); |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | /** |
| 154 | * Aborts the attempt to place the relevant call. Intended to be invoked by |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 155 | * switchboard through the outgoing-calls manager. |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 156 | */ |
| 157 | void abort() { |
Ben Gilad | d88323b | 2014-01-29 19:03:19 -0800 | [diff] [blame] | 158 | ThreadUtil.checkOnMainThread(); |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 159 | resetCallService(); |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 160 | mIsAborted = true; |
| 161 | } |
| 162 | |
| 163 | /** |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 164 | * Completes the outgoing call sequence by setting the call service on the call object. This is |
| 165 | * invoked when the call service adapter receives positive confirmation that the call service |
| 166 | * placed the call. |
| 167 | */ |
| 168 | void handleSuccessfulCallAttempt() { |
| 169 | abort(); // Technically not needed but playing it safe. |
| 170 | mCall.setCallService(mCallService); |
| 171 | mCall.setState(CallState.DIALING); |
| 172 | resetCallService(); |
| 173 | |
| 174 | mSwitchboard.handleSuccessfulOutgoingCall(mCall); |
| 175 | } |
| 176 | |
| 177 | /** |
| 178 | * Attempts the next call service if the specified call service is the one currently being |
| 179 | * attempted. |
| 180 | * |
| 181 | * @param reason The call-service supplied reason for the failed call attempt. |
| 182 | */ |
| 183 | void handleFailedCallAttempt(String reason) { |
| 184 | attemptNextCallService(); |
| 185 | } |
| 186 | |
| 187 | /** |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 188 | * Attempts to place the call using the next selector, no-op if no other selectors |
| 189 | * are available. |
| 190 | */ |
| 191 | private void attemptNextSelector() { |
| 192 | if (mIsAborted) { |
| 193 | return; |
| 194 | } |
| 195 | |
| 196 | if (mSelectorIterator.hasNext()) { |
| 197 | ICallServiceSelector selector = mSelectorIterator.next(); |
Ben Gilad | d88323b | 2014-01-29 19:03:19 -0800 | [diff] [blame] | 198 | ICallServiceSelectionResponse.Stub response = createSelectionResponse(); |
| 199 | try { |
Ben Gilad | c5b2269 | 2014-02-18 20:03:22 -0800 | [diff] [blame^] | 200 | selector.select(mCall.toCallInfo(), mCallServiceDescriptors, response); |
Ben Gilad | d88323b | 2014-01-29 19:03:19 -0800 | [diff] [blame] | 201 | } catch (RemoteException e) { |
| 202 | attemptNextSelector(); |
| 203 | } |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 204 | |
| 205 | } else { |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 206 | mOutgoingCallsManager.handleFailedOutgoingCall(mCall); |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 207 | } |
| 208 | } |
| 209 | |
| 210 | /** |
Ben Gilad | d88323b | 2014-01-29 19:03:19 -0800 | [diff] [blame] | 211 | * @return A new selection-response object that's wired to run on the main (UI) thread. |
| 212 | */ |
| 213 | private ICallServiceSelectionResponse.Stub createSelectionResponse() { |
| 214 | return new ICallServiceSelectionResponse.Stub() { |
Ben Gilad | c5b2269 | 2014-02-18 20:03:22 -0800 | [diff] [blame^] | 215 | @Override public void setSelectedCallServiceDescriptors( |
| 216 | final List<CallServiceDescriptor> selectedCallServiceDescriptors) { |
Ben Gilad | d88323b | 2014-01-29 19:03:19 -0800 | [diff] [blame] | 217 | |
| 218 | Runnable runnable = new Runnable() { |
| 219 | @Override public void run() { |
Ben Gilad | c5b2269 | 2014-02-18 20:03:22 -0800 | [diff] [blame^] | 220 | processSelectedCallServiceDescriptors(selectedCallServiceDescriptors); |
Ben Gilad | d88323b | 2014-01-29 19:03:19 -0800 | [diff] [blame] | 221 | } |
| 222 | }; |
| 223 | |
| 224 | mHandler.post(runnable); |
| 225 | } |
| 226 | }; |
| 227 | } |
| 228 | |
| 229 | /** |
Ben Gilad | c5b2269 | 2014-02-18 20:03:22 -0800 | [diff] [blame^] | 230 | * Persists the ordered-list of call-service descriptor as selected by the current selector and |
| 231 | * starts iterating through the corresponding call services continuing the attempt to place the |
| 232 | * call. |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 233 | * |
Ben Gilad | c5b2269 | 2014-02-18 20:03:22 -0800 | [diff] [blame^] | 234 | * @selectedCallServiceDescriptors The (ordered) list of call-service descriptor. |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 235 | */ |
Ben Gilad | c5b2269 | 2014-02-18 20:03:22 -0800 | [diff] [blame^] | 236 | private void processSelectedCallServiceDescriptors( |
| 237 | List<CallServiceDescriptor> selectedCallServiceDescriptors) { |
| 238 | |
| 239 | if (selectedCallServiceDescriptors == null || selectedCallServiceDescriptors.isEmpty()) { |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 240 | attemptNextSelector(); |
Ben Gilad | c5b2269 | 2014-02-18 20:03:22 -0800 | [diff] [blame^] | 241 | } else if (mCallServiceDescriptorIterator == null) { |
| 242 | mCallServiceDescriptorIterator = selectedCallServiceDescriptors.iterator(); |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 243 | attemptNextCallService(); |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | /** |
Ben Gilad | c5b2269 | 2014-02-18 20:03:22 -0800 | [diff] [blame^] | 248 | * Attempts to place the call using the call service specified by the next call-service |
| 249 | * descriptor of mCallServiceDescriptorIterator. If there are no more call services to |
| 250 | * attempt, the process continues to the next call-service selector via |
| 251 | * {@link #attemptNextSelector}. |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 252 | */ |
| 253 | private void attemptNextCallService() { |
| 254 | if (mIsAborted) { |
| 255 | return; |
| 256 | } |
| 257 | |
Ben Gilad | c5b2269 | 2014-02-18 20:03:22 -0800 | [diff] [blame^] | 258 | if (mCallServiceDescriptorIterator.hasNext()) { |
| 259 | CallServiceDescriptor descriptor = mCallServiceDescriptorIterator.next(); |
| 260 | mCallService = mCallServicesById.get(descriptor.getCallServiceId()); |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 261 | if (mCallService == null) { |
| 262 | attemptNextCallService(); |
| 263 | } else { |
Sailesh Nepal | b6141ae | 2014-02-18 08:45:26 -0800 | [diff] [blame] | 264 | BindCallback callback = new BindCallback() { |
| 265 | @Override public void onSuccess() { |
| 266 | mCallService.call(mCall.toCallInfo()); |
| 267 | } |
| 268 | @Override public void onFailure() { |
| 269 | attemptNextSelector(); |
| 270 | } |
| 271 | }; |
| 272 | mCallService.bind(callback); |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 273 | } |
| 274 | } else { |
Ben Gilad | c5b2269 | 2014-02-18 20:03:22 -0800 | [diff] [blame^] | 275 | mCallServiceDescriptorIterator = null; |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 276 | resetCallService(); |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 277 | attemptNextSelector(); |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | /** |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 282 | * Nulls out the reference to the current call service. Invoked when the call service is no longer |
| 283 | * expected to place the outgoing call. |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 284 | */ |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 285 | private void resetCallService() { |
| 286 | mCallService = null; |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 287 | } |
| 288 | } |