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 | |
| 90 | private boolean mIsAborted = false; |
| 91 | |
| 92 | /** |
| 93 | * Persists the specified parameters and iterates through the prioritized list of selectors |
| 94 | * 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] | 95 | * service descriptors. Stops once a matching selector is found. Calls with no matching |
| 96 | * selectors will eventually be killed by the cleanup/monitor switchboard handler, which will |
| 97 | * in turn call the abort method of this processor via {@link OutgoingCallsManager}. |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 98 | * |
| 99 | * @param call The call to place. |
| 100 | * @param callServices The available call-service implementations. |
| 101 | * @param selectors The available call-service selector implementations. |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 102 | * @param outgoingCallsManager Manager of all outgoing call processors. |
| 103 | * @param switchboard The switchboard. |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 104 | */ |
| 105 | OutgoingCallProcessor( |
| 106 | Call call, |
Santos Cordon | c195e36 | 2014-02-11 17:05:31 -0800 | [diff] [blame] | 107 | Set<CallServiceWrapper> callServices, |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 108 | List<ICallServiceSelector> selectors, |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 109 | OutgoingCallsManager outgoingCallsManager, |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 110 | Switchboard switchboard) { |
| 111 | |
Ben Gilad | d88323b | 2014-01-29 19:03:19 -0800 | [diff] [blame] | 112 | ThreadUtil.checkOnMainThread(); |
| 113 | |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 114 | Preconditions.checkNotNull(callServices); |
| 115 | Preconditions.checkNotNull(selectors); |
| 116 | |
| 117 | mCall = call; |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 118 | mSelectors = selectors; |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 119 | mOutgoingCallsManager = outgoingCallsManager; |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 120 | mSwitchboard = switchboard; |
| 121 | |
Ben Gilad | c5b2269 | 2014-02-18 20:03:22 -0800 | [diff] [blame] | 122 | // 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] | 123 | // it's being passed down to selectors. |
Santos Cordon | c195e36 | 2014-02-11 17:05:31 -0800 | [diff] [blame] | 124 | for (CallServiceWrapper callService : callServices) { |
Ben Gilad | c5b2269 | 2014-02-18 20:03:22 -0800 | [diff] [blame] | 125 | CallServiceDescriptor descriptor = callService.getDescriptor(); |
| 126 | mCallServiceDescriptors.add(descriptor); |
| 127 | mCallServicesById.put(descriptor.getCallServiceId(), callService); |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 128 | } |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * Initiates the attempt to place the call. No-op beyond the first invocation. |
| 133 | */ |
| 134 | void process() { |
Ben Gilad | d88323b | 2014-01-29 19:03:19 -0800 | [diff] [blame] | 135 | ThreadUtil.checkOnMainThread(); |
| 136 | |
Ben Gilad | c5b2269 | 2014-02-18 20:03:22 -0800 | [diff] [blame] | 137 | if (mSelectors.isEmpty() || mCallServiceDescriptors.isEmpty()) { |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 138 | // TODO(gilad): Consider adding a failure message/type to differentiate the various |
| 139 | // cases, or potentially throw an exception in this case. |
Ben Gilad | 8e55d1d | 2014-02-26 16:25:56 -0800 | [diff] [blame^] | 140 | // TODO(gilad): Perform this check all the way up in switchboard to short-circuit |
| 141 | // the current detour. |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 142 | mOutgoingCallsManager.handleFailedOutgoingCall(mCall); |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 143 | } else if (mSelectorIterator == null) { |
| 144 | mSelectorIterator = mSelectors.iterator(); |
| 145 | attemptNextSelector(); |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * 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] | 151 | * switchboard through the outgoing-calls manager. |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 152 | */ |
| 153 | void abort() { |
Ben Gilad | 8e55d1d | 2014-02-26 16:25:56 -0800 | [diff] [blame^] | 154 | mCall.abort(); |
| 155 | |
| 156 | // TODO(gilad): Add logic to notify the relevant call service and/or selector. |
| 157 | |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 158 | mIsAborted = true; |
| 159 | } |
| 160 | |
| 161 | /** |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 162 | * Completes the outgoing call sequence by setting the call service on the call object. This is |
| 163 | * invoked when the call service adapter receives positive confirmation that the call service |
| 164 | * placed the call. |
| 165 | */ |
| 166 | void handleSuccessfulCallAttempt() { |
Ben Gilad | 8e55d1d | 2014-02-26 16:25:56 -0800 | [diff] [blame^] | 167 | ThreadUtil.checkOnMainThread(); |
| 168 | |
| 169 | if (mIsAborted) { |
| 170 | // TODO(gilad): Ask the call service to drop the call? |
| 171 | return; |
| 172 | } |
| 173 | |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 174 | mCall.setState(CallState.DIALING); |
Ben Gilad | 8e55d1d | 2014-02-26 16:25:56 -0800 | [diff] [blame^] | 175 | |
| 176 | // TODO(gilad): Seems better/clearer not to invoke "abort" on successfully-connected calls. |
Sailesh Nepal | 155ab51 | 2014-02-20 18:11:40 -0800 | [diff] [blame] | 177 | abort(); |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 178 | |
| 179 | mSwitchboard.handleSuccessfulOutgoingCall(mCall); |
| 180 | } |
| 181 | |
| 182 | /** |
| 183 | * Attempts the next call service if the specified call service is the one currently being |
| 184 | * attempted. |
| 185 | * |
| 186 | * @param reason The call-service supplied reason for the failed call attempt. |
| 187 | */ |
| 188 | void handleFailedCallAttempt(String reason) { |
Ben Gilad | 8e55d1d | 2014-02-26 16:25:56 -0800 | [diff] [blame^] | 189 | ThreadUtil.checkOnMainThread(); |
| 190 | |
| 191 | mCall.clearCallService(); |
| 192 | mCall.clearCallServiceSelector(); |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 193 | attemptNextCallService(); |
| 194 | } |
| 195 | |
| 196 | /** |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 197 | * Attempts to place the call using the next selector, no-op if no other selectors |
| 198 | * are available. |
| 199 | */ |
| 200 | private void attemptNextSelector() { |
| 201 | if (mIsAborted) { |
| 202 | return; |
| 203 | } |
| 204 | |
| 205 | if (mSelectorIterator.hasNext()) { |
| 206 | ICallServiceSelector selector = mSelectorIterator.next(); |
Ben Gilad | 8e55d1d | 2014-02-26 16:25:56 -0800 | [diff] [blame^] | 207 | mCall.setCallServiceSelector(selector); |
| 208 | |
Ben Gilad | d88323b | 2014-01-29 19:03:19 -0800 | [diff] [blame] | 209 | ICallServiceSelectionResponse.Stub response = createSelectionResponse(); |
| 210 | try { |
Ben Gilad | c5b2269 | 2014-02-18 20:03:22 -0800 | [diff] [blame] | 211 | selector.select(mCall.toCallInfo(), mCallServiceDescriptors, response); |
Ben Gilad | d88323b | 2014-01-29 19:03:19 -0800 | [diff] [blame] | 212 | } catch (RemoteException e) { |
| 213 | attemptNextSelector(); |
| 214 | } |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 215 | |
| 216 | } else { |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 217 | mOutgoingCallsManager.handleFailedOutgoingCall(mCall); |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 218 | } |
| 219 | } |
| 220 | |
| 221 | /** |
Ben Gilad | d88323b | 2014-01-29 19:03:19 -0800 | [diff] [blame] | 222 | * @return A new selection-response object that's wired to run on the main (UI) thread. |
| 223 | */ |
| 224 | private ICallServiceSelectionResponse.Stub createSelectionResponse() { |
| 225 | return new ICallServiceSelectionResponse.Stub() { |
Ben Gilad | c5b2269 | 2014-02-18 20:03:22 -0800 | [diff] [blame] | 226 | @Override public void setSelectedCallServiceDescriptors( |
| 227 | final List<CallServiceDescriptor> selectedCallServiceDescriptors) { |
Ben Gilad | d88323b | 2014-01-29 19:03:19 -0800 | [diff] [blame] | 228 | |
| 229 | Runnable runnable = new Runnable() { |
| 230 | @Override public void run() { |
Ben Gilad | c5b2269 | 2014-02-18 20:03:22 -0800 | [diff] [blame] | 231 | processSelectedCallServiceDescriptors(selectedCallServiceDescriptors); |
Ben Gilad | d88323b | 2014-01-29 19:03:19 -0800 | [diff] [blame] | 232 | } |
| 233 | }; |
| 234 | |
| 235 | mHandler.post(runnable); |
| 236 | } |
| 237 | }; |
| 238 | } |
| 239 | |
| 240 | /** |
Ben Gilad | c5b2269 | 2014-02-18 20:03:22 -0800 | [diff] [blame] | 241 | * Persists the ordered-list of call-service descriptor as selected by the current selector and |
| 242 | * starts iterating through the corresponding call services continuing the attempt to place the |
| 243 | * call. |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 244 | * |
Ben Gilad | c5b2269 | 2014-02-18 20:03:22 -0800 | [diff] [blame] | 245 | * @selectedCallServiceDescriptors The (ordered) list of call-service descriptor. |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 246 | */ |
Ben Gilad | c5b2269 | 2014-02-18 20:03:22 -0800 | [diff] [blame] | 247 | private void processSelectedCallServiceDescriptors( |
| 248 | List<CallServiceDescriptor> selectedCallServiceDescriptors) { |
| 249 | |
| 250 | if (selectedCallServiceDescriptors == null || selectedCallServiceDescriptors.isEmpty()) { |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 251 | attemptNextSelector(); |
Ben Gilad | c5b2269 | 2014-02-18 20:03:22 -0800 | [diff] [blame] | 252 | } else if (mCallServiceDescriptorIterator == null) { |
| 253 | mCallServiceDescriptorIterator = selectedCallServiceDescriptors.iterator(); |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 254 | attemptNextCallService(); |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | /** |
Ben Gilad | c5b2269 | 2014-02-18 20:03:22 -0800 | [diff] [blame] | 259 | * Attempts to place the call using the call service specified by the next call-service |
| 260 | * descriptor of mCallServiceDescriptorIterator. If there are no more call services to |
| 261 | * attempt, the process continues to the next call-service selector via |
| 262 | * {@link #attemptNextSelector}. |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 263 | */ |
| 264 | private void attemptNextCallService() { |
| 265 | if (mIsAborted) { |
| 266 | return; |
| 267 | } |
| 268 | |
Ben Gilad | c5b2269 | 2014-02-18 20:03:22 -0800 | [diff] [blame] | 269 | if (mCallServiceDescriptorIterator.hasNext()) { |
| 270 | CallServiceDescriptor descriptor = mCallServiceDescriptorIterator.next(); |
Ben Gilad | 8e55d1d | 2014-02-26 16:25:56 -0800 | [diff] [blame^] | 271 | final CallServiceWrapper callService = |
| 272 | mCallServicesById.get(descriptor.getCallServiceId()); |
| 273 | |
| 274 | if (callService == null) { |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 275 | attemptNextCallService(); |
| 276 | } else { |
Sailesh Nepal | b6141ae | 2014-02-18 08:45:26 -0800 | [diff] [blame] | 277 | BindCallback callback = new BindCallback() { |
| 278 | @Override public void onSuccess() { |
Ben Gilad | 8e55d1d | 2014-02-26 16:25:56 -0800 | [diff] [blame^] | 279 | callService.call(mCall.toCallInfo()); |
Sailesh Nepal | b6141ae | 2014-02-18 08:45:26 -0800 | [diff] [blame] | 280 | } |
| 281 | @Override public void onFailure() { |
Ben Gilad | b07a9d5 | 2014-02-24 12:12:25 -0800 | [diff] [blame] | 282 | attemptNextCallService(); |
Sailesh Nepal | b6141ae | 2014-02-18 08:45:26 -0800 | [diff] [blame] | 283 | } |
| 284 | }; |
Ben Gilad | 8e55d1d | 2014-02-26 16:25:56 -0800 | [diff] [blame^] | 285 | |
| 286 | mCall.setCallService(callService); |
| 287 | |
Santos Cordon | 493e8f2 | 2014-02-19 03:15:12 -0800 | [diff] [blame] | 288 | // TODO(santoscordon): Consider making bind private to CallServiceWrapper and having |
| 289 | // CSWrapper.call() do the bind automatically. |
Ben Gilad | 8e55d1d | 2014-02-26 16:25:56 -0800 | [diff] [blame^] | 290 | callService.bind(callback); |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 291 | } |
| 292 | } else { |
Ben Gilad | c5b2269 | 2014-02-18 20:03:22 -0800 | [diff] [blame] | 293 | mCallServiceDescriptorIterator = null; |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 294 | attemptNextSelector(); |
| 295 | } |
| 296 | } |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 297 | } |