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 | |
Ihab Awad | 55a3428 | 2014-06-18 10:31:09 -0700 | [diff] [blame] | 19 | import android.net.Uri; |
Santos Cordon | 682fe6b | 2014-05-20 08:56:39 -0700 | [diff] [blame] | 20 | import android.os.Handler; |
| 21 | import android.os.Message; |
Ben Gilad | c5b2269 | 2014-02-18 20:03:22 -0800 | [diff] [blame] | 22 | import android.telecomm.CallServiceDescriptor; |
Ihab Awad | 0fea3f2 | 2014-06-03 18:45:05 -0700 | [diff] [blame] | 23 | import android.telephony.DisconnectCause; |
Ihab Awad | 55a3428 | 2014-06-18 10:31:09 -0700 | [diff] [blame] | 24 | import android.telephony.PhoneNumberUtils; |
Sailesh Nepal | b6141ae | 2014-02-18 08:45:26 -0800 | [diff] [blame] | 25 | |
Santos Cordon | 682fe6b | 2014-05-20 08:56:39 -0700 | [diff] [blame] | 26 | import com.android.telecomm.BaseRepository.LookupCallback; |
Ben Gilad | 6192561 | 2014-03-11 19:06:36 -0700 | [diff] [blame] | 27 | import com.google.android.collect.Sets; |
Santos Cordon | 766d04f | 2014-05-06 10:28:25 -0700 | [diff] [blame] | 28 | import com.google.common.collect.Maps; |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 29 | |
Santos Cordon | 682fe6b | 2014-05-20 08:56:39 -0700 | [diff] [blame] | 30 | import java.util.ArrayList; |
Sailesh Nepal | 18386a8 | 2014-03-19 10:22:40 -0700 | [diff] [blame] | 31 | import java.util.Collection; |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 32 | import java.util.Iterator; |
| 33 | import java.util.List; |
| 34 | import java.util.Map; |
| 35 | import java.util.Set; |
| 36 | |
| 37 | /** |
| 38 | * 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] | 39 | * Iterates through the selectors and gets a sorted list of supported call-service descriptors |
| 40 | * for each selector. Upon receiving each sorted list (one list per selector), each of the |
| 41 | * corresponding call services is then attempted until either the outgoing call is placed, the |
Santos Cordon | 74d420b | 2014-05-07 14:38:47 -0700 | [diff] [blame] | 42 | * attempted call is aborted, or the list is exhausted -- whichever occurs first. |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 43 | * |
Santos Cordon | 74d420b | 2014-05-07 14:38:47 -0700 | [diff] [blame] | 44 | * Except for the abort case, all other scenarios should terminate with the call notified |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 45 | * of the result. |
Ben Gilad | d88323b | 2014-01-29 19:03:19 -0800 | [diff] [blame] | 46 | * |
| 47 | * NOTE(gilad): Currently operating under the assumption that we'll have one timeout per (outgoing) |
| 48 | * call attempt. If we (also) like to timeout individual selectors and/or call services, the code |
| 49 | * 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] | 50 | */ |
| 51 | final class OutgoingCallProcessor { |
| 52 | |
Santos Cordon | 682fe6b | 2014-05-20 08:56:39 -0700 | [diff] [blame] | 53 | private final static int MSG_EXPIRE = 1; |
| 54 | |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 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 | /** |
Sailesh Nepal | b6141ae | 2014-02-18 08:45:26 -0800 | [diff] [blame] | 61 | * 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] | 62 | */ |
Sailesh Nepal | b6141ae | 2014-02-18 08:45:26 -0800 | [diff] [blame] | 63 | private final Map<String, CallServiceWrapper> mCallServicesById = Maps.newHashMap(); |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 64 | |
| 65 | /** |
Ben Gilad | 6192561 | 2014-03-11 19:06:36 -0700 | [diff] [blame] | 66 | * The set of attempted call services, used to ensure services are attempted at most once per |
| 67 | * outgoing-call attempt. |
| 68 | */ |
| 69 | private final Set<CallServiceWrapper> mAttemptedCallServices = Sets.newHashSet(); |
| 70 | |
Santos Cordon | 682fe6b | 2014-05-20 08:56:39 -0700 | [diff] [blame] | 71 | private final CallServiceRepository mCallServiceRepository; |
Ben Gilad | 6192561 | 2014-03-11 19:06:36 -0700 | [diff] [blame] | 72 | |
Santos Cordon | 682fe6b | 2014-05-20 08:56:39 -0700 | [diff] [blame] | 73 | private final Handler mHandler = new Handler() { |
| 74 | @Override |
| 75 | public void handleMessage(Message msg) { |
| 76 | switch (msg.what) { |
| 77 | case MSG_EXPIRE: |
| 78 | abort(); |
| 79 | break; |
| 80 | } |
Ben Gilad | 6192561 | 2014-03-11 19:06:36 -0700 | [diff] [blame] | 81 | } |
| 82 | }; |
| 83 | |
Santos Cordon | 682fe6b | 2014-05-20 08:56:39 -0700 | [diff] [blame] | 84 | /** |
| 85 | * The duplicate-free list of currently-available call-service descriptors. |
| 86 | */ |
| 87 | private List<CallServiceDescriptor> mCallServiceDescriptors; |
Sailesh Nepal | 18386a8 | 2014-03-19 10:22:40 -0700 | [diff] [blame] | 88 | |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 89 | /** |
Ben Gilad | c5b2269 | 2014-02-18 20:03:22 -0800 | [diff] [blame] | 90 | * The iterator over the currently-selected ordered list of call-service descriptors. |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 91 | */ |
Ben Gilad | c5b2269 | 2014-02-18 20:03:22 -0800 | [diff] [blame] | 92 | private Iterator<CallServiceDescriptor> mCallServiceDescriptorIterator; |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 93 | |
Santos Cordon | 682fe6b | 2014-05-20 08:56:39 -0700 | [diff] [blame] | 94 | private AsyncResultCallback<Boolean> mResultCallback; |
| 95 | |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 96 | private boolean mIsAborted = false; |
| 97 | |
Ihab Awad | 0fea3f2 | 2014-06-03 18:45:05 -0700 | [diff] [blame] | 98 | private int mLastErrorCode = 0; |
| 99 | |
| 100 | private String mLastErrorMsg = null; |
| 101 | |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 102 | /** |
Ihab Awad | 55a3428 | 2014-06-18 10:31:09 -0700 | [diff] [blame] | 103 | * Persists the specified parameters and iterates through the prioritized list of call |
| 104 | * services. Stops once a matching call service is found. Calls with no matching |
| 105 | * call service will eventually be killed by the cleanup/monitor switchboard handler. |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 106 | * |
| 107 | * @param call The call to place. |
Santos Cordon | 682fe6b | 2014-05-20 08:56:39 -0700 | [diff] [blame] | 108 | * @param callServiceRepository |
Santos Cordon | 682fe6b | 2014-05-20 08:56:39 -0700 | [diff] [blame] | 109 | * @param resultCallback The callback on which to return the result. |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 110 | */ |
| 111 | OutgoingCallProcessor( |
| 112 | Call call, |
Santos Cordon | 682fe6b | 2014-05-20 08:56:39 -0700 | [diff] [blame] | 113 | CallServiceRepository callServiceRepository, |
Santos Cordon | 682fe6b | 2014-05-20 08:56:39 -0700 | [diff] [blame] | 114 | AsyncResultCallback<Boolean> resultCallback) { |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 115 | |
Ben Gilad | d88323b | 2014-01-29 19:03:19 -0800 | [diff] [blame] | 116 | ThreadUtil.checkOnMainThread(); |
| 117 | |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 118 | mCall = call; |
Santos Cordon | 682fe6b | 2014-05-20 08:56:39 -0700 | [diff] [blame] | 119 | mResultCallback = resultCallback; |
| 120 | mCallServiceRepository = callServiceRepository; |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Initiates the attempt to place the call. No-op beyond the first invocation. |
| 125 | */ |
| 126 | void process() { |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 127 | Log.v(this, "process, mIsAborted: %b", mIsAborted); |
Ben Gilad | 28e8ad6 | 2014-03-06 17:01:54 -0800 | [diff] [blame] | 128 | if (!mIsAborted) { |
Santos Cordon | 682fe6b | 2014-05-20 08:56:39 -0700 | [diff] [blame] | 129 | // Start the expiration timeout. |
| 130 | mHandler.sendEmptyMessageDelayed(MSG_EXPIRE, Timeouts.getNewOutgoingCallMillis()); |
Ben Gilad | d88323b | 2014-01-29 19:03:19 -0800 | [diff] [blame] | 131 | |
Santos Cordon | 682fe6b | 2014-05-20 08:56:39 -0700 | [diff] [blame] | 132 | // Lookup call services |
| 133 | mCallServiceRepository.lookupServices(new LookupCallback<CallServiceWrapper>() { |
| 134 | @Override |
| 135 | public void onComplete(Collection<CallServiceWrapper> services) { |
| 136 | setCallServices(services); |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 137 | } |
Santos Cordon | 682fe6b | 2014-05-20 08:56:39 -0700 | [diff] [blame] | 138 | }); |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 139 | } |
Ben Gilad | 6192561 | 2014-03-11 19:06:36 -0700 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | /** |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 143 | * 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] | 144 | * switchboard through the outgoing-calls manager. |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 145 | */ |
| 146 | void abort() { |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 147 | Log.v(this, "abort"); |
Ben Gilad | 9c23411 | 2014-03-04 16:07:33 -0800 | [diff] [blame] | 148 | ThreadUtil.checkOnMainThread(); |
Santos Cordon | 682fe6b | 2014-05-20 08:56:39 -0700 | [diff] [blame] | 149 | if (!mIsAborted && mResultCallback != null) { |
Ben Gilad | 9c23411 | 2014-03-04 16:07:33 -0800 | [diff] [blame] | 150 | mIsAborted = true; |
Santos Cordon | 682fe6b | 2014-05-20 08:56:39 -0700 | [diff] [blame] | 151 | |
| 152 | // On an abort, we need to check if we already told the call service to place the |
| 153 | // call. If so, we need to tell it to abort. |
| 154 | // TODO(santoscordon): The call service is saved with the call and so we have to query |
| 155 | // the call to get it, which is a bit backwards. Ideally, the call service would be |
| 156 | // saved inside this class until the whole thing is complete and then set on the call. |
| 157 | CallServiceWrapper callService = mCall.getCallService(); |
| 158 | if (callService != null) { |
| 159 | callService.abort(mCall); |
| 160 | } |
| 161 | |
Ihab Awad | 0fea3f2 | 2014-06-03 18:45:05 -0700 | [diff] [blame] | 162 | // We consider a deliberate abort to be a "normal" disconnect, not |
| 163 | // requiring special reporting. |
| 164 | sendResult(false, DisconnectCause.LOCAL, null); |
Ben Gilad | 9c23411 | 2014-03-04 16:07:33 -0800 | [diff] [blame] | 165 | } |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 166 | } |
| 167 | |
Santos Cordon | 682fe6b | 2014-05-20 08:56:39 -0700 | [diff] [blame] | 168 | boolean isAborted() { |
| 169 | return mIsAborted; |
| 170 | } |
| 171 | |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 172 | /** |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 173 | * Completes the outgoing call sequence by setting the call service on the call object. This is |
| 174 | * invoked when the call service adapter receives positive confirmation that the call service |
| 175 | * placed the call. |
| 176 | */ |
Santos Cordon | 682fe6b | 2014-05-20 08:56:39 -0700 | [diff] [blame] | 177 | void handleSuccessfulCallAttempt(CallServiceWrapper callService) { |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 178 | Log.v(this, "handleSuccessfulCallAttempt"); |
Ben Gilad | 8e55d1d | 2014-02-26 16:25:56 -0800 | [diff] [blame] | 179 | ThreadUtil.checkOnMainThread(); |
| 180 | |
| 181 | if (mIsAborted) { |
| 182 | // TODO(gilad): Ask the call service to drop the call? |
Santos Cordon | 682fe6b | 2014-05-20 08:56:39 -0700 | [diff] [blame] | 183 | callService.abort(mCall); |
Ben Gilad | 8e55d1d | 2014-02-26 16:25:56 -0800 | [diff] [blame] | 184 | return; |
| 185 | } |
| 186 | |
Ihab Awad | 0fea3f2 | 2014-06-03 18:45:05 -0700 | [diff] [blame] | 187 | sendResult(true, DisconnectCause.NOT_DISCONNECTED, null); |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | /** |
| 191 | * Attempts the next call service if the specified call service is the one currently being |
| 192 | * attempted. |
| 193 | * |
Ihab Awad | 0fea3f2 | 2014-06-03 18:45:05 -0700 | [diff] [blame] | 194 | * @param errorCode The reason for the failure, one of {@link DisconnectCause}. |
| 195 | * @param errorMsg Optional text reason for the failure. |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 196 | */ |
Ihab Awad | 0fea3f2 | 2014-06-03 18:45:05 -0700 | [diff] [blame] | 197 | void handleFailedCallAttempt(int errorCode, String errorMsg) { |
| 198 | Log.v(this, "handleFailedCallAttempt %s %s", DisconnectCause.toString(errorCode), errorMsg); |
| 199 | // Store latest error code and message. If this is our last available attempt at placing |
| 200 | // a call, these error details will be considered "the" cause of the failure. |
| 201 | mLastErrorCode = errorCode; |
| 202 | mLastErrorMsg = errorMsg; |
Ben Gilad | 28e8ad6 | 2014-03-06 17:01:54 -0800 | [diff] [blame] | 203 | if (!mIsAborted) { |
| 204 | ThreadUtil.checkOnMainThread(); |
Ben Gilad | 28e8ad6 | 2014-03-06 17:01:54 -0800 | [diff] [blame] | 205 | attemptNextCallService(); |
| 206 | } |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | /** |
Santos Cordon | 682fe6b | 2014-05-20 08:56:39 -0700 | [diff] [blame] | 210 | * Sets the call services to attempt for this outgoing call. |
| 211 | * |
| 212 | * @param callServices The call services. |
| 213 | */ |
| 214 | private void setCallServices(Collection<CallServiceWrapper> callServices) { |
| 215 | mCallServiceDescriptors = new ArrayList<>(); |
| 216 | |
| 217 | // Populate the list and map of call-service descriptors. The list is needed since |
| 218 | // it's being passed down to selectors. |
| 219 | for (CallServiceWrapper callService : callServices) { |
| 220 | CallServiceDescriptor descriptor = callService.getDescriptor(); |
Sailesh Nepal | a1b3bca | 2014-06-24 15:17:44 -0700 | [diff] [blame^] | 221 | // TODO(sail): Remove once there's a way to pick the service. |
| 222 | if (descriptor.getServiceComponent().getPackageName().equals( |
| 223 | "com.google.android.talk")) { |
| 224 | Log.i(this, "Moving call service %s to top of list", descriptor); |
| 225 | mCallServiceDescriptors.add(0, descriptor); |
| 226 | } else { |
| 227 | mCallServiceDescriptors.add(descriptor); |
| 228 | } |
Santos Cordon | 682fe6b | 2014-05-20 08:56:39 -0700 | [diff] [blame] | 229 | mCallServicesById.put(descriptor.getCallServiceId(), callService); |
| 230 | } |
| 231 | |
Ihab Awad | 55a3428 | 2014-06-18 10:31:09 -0700 | [diff] [blame] | 232 | adjustCallServiceDescriptorsForEmergency(); |
Santos Cordon | 682fe6b | 2014-05-20 08:56:39 -0700 | [diff] [blame] | 233 | |
Ihab Awad | 55a3428 | 2014-06-18 10:31:09 -0700 | [diff] [blame] | 234 | mCallServiceDescriptorIterator = mCallServiceDescriptors.iterator(); |
| 235 | attemptNextCallService(); |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | /** |
Ben Gilad | c5b2269 | 2014-02-18 20:03:22 -0800 | [diff] [blame] | 239 | * Attempts to place the call using the call service specified by the next call-service |
Ihab Awad | 55a3428 | 2014-06-18 10:31:09 -0700 | [diff] [blame] | 240 | * descriptor of mCallServiceDescriptorIterator. |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 241 | */ |
| 242 | private void attemptNextCallService() { |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 243 | Log.v(this, "attemptNextCallService, mIsAborted: %b", mIsAborted); |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 244 | if (mIsAborted) { |
| 245 | return; |
| 246 | } |
| 247 | |
Evan Charlton | f02e988 | 2014-03-06 12:54:52 -0800 | [diff] [blame] | 248 | if (mCallServiceDescriptorIterator != null && mCallServiceDescriptorIterator.hasNext()) { |
Ben Gilad | c5b2269 | 2014-02-18 20:03:22 -0800 | [diff] [blame] | 249 | CallServiceDescriptor descriptor = mCallServiceDescriptorIterator.next(); |
Ben Gilad | 8e55d1d | 2014-02-26 16:25:56 -0800 | [diff] [blame] | 250 | final CallServiceWrapper callService = |
| 251 | mCallServicesById.get(descriptor.getCallServiceId()); |
| 252 | |
Ben Gilad | 6192561 | 2014-03-11 19:06:36 -0700 | [diff] [blame] | 253 | if (callService == null || mAttemptedCallServices.contains(callService)) { |
| 254 | // The next call service is either null or has already been attempted, fast forward |
| 255 | // to the next. |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 256 | attemptNextCallService(); |
| 257 | } else { |
Ben Gilad | 6192561 | 2014-03-11 19:06:36 -0700 | [diff] [blame] | 258 | mAttemptedCallServices.add(callService); |
Ben Gilad | 8e55d1d | 2014-02-26 16:25:56 -0800 | [diff] [blame] | 259 | mCall.setCallService(callService); |
Santos Cordon | 682fe6b | 2014-05-20 08:56:39 -0700 | [diff] [blame] | 260 | |
| 261 | // Increment the associated call count until we get a result. This prevents the call |
| 262 | // service from unbinding while we are using it. |
| 263 | callService.incrementAssociatedCallCount(); |
| 264 | |
| 265 | callService.call(mCall, new AsyncResultCallback<Boolean>() { |
| 266 | @Override |
Ihab Awad | 0fea3f2 | 2014-06-03 18:45:05 -0700 | [diff] [blame] | 267 | public void onResult(Boolean wasCallPlaced, int errorCode, String errorMsg) { |
Santos Cordon | 682fe6b | 2014-05-20 08:56:39 -0700 | [diff] [blame] | 268 | if (wasCallPlaced) { |
| 269 | handleSuccessfulCallAttempt(callService); |
| 270 | } else { |
Ihab Awad | 0fea3f2 | 2014-06-03 18:45:05 -0700 | [diff] [blame] | 271 | handleFailedCallAttempt(errorCode, errorMsg); |
Santos Cordon | 682fe6b | 2014-05-20 08:56:39 -0700 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | // If successful, the call should not have it's own association to keep |
| 275 | // the call service bound. |
| 276 | callService.decrementAssociatedCallCount(); |
| 277 | } |
| 278 | }); |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 279 | } |
| 280 | } else { |
Ihab Awad | 55a3428 | 2014-06-18 10:31:09 -0700 | [diff] [blame] | 281 | Log.v(this, "attemptNextCallService, no more service descriptors, failing"); |
Ben Gilad | c5b2269 | 2014-02-18 20:03:22 -0800 | [diff] [blame] | 282 | mCallServiceDescriptorIterator = null; |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 283 | mCall.clearCallService(); |
Ihab Awad | 55a3428 | 2014-06-18 10:31:09 -0700 | [diff] [blame] | 284 | sendResult(false, mLastErrorCode, mLastErrorMsg); |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 285 | } |
| 286 | } |
Santos Cordon | 682fe6b | 2014-05-20 08:56:39 -0700 | [diff] [blame] | 287 | |
Ihab Awad | 0fea3f2 | 2014-06-03 18:45:05 -0700 | [diff] [blame] | 288 | private void sendResult(boolean wasCallPlaced, int errorCode, String errorMsg) { |
Santos Cordon | 682fe6b | 2014-05-20 08:56:39 -0700 | [diff] [blame] | 289 | if (mResultCallback != null) { |
Ihab Awad | 0fea3f2 | 2014-06-03 18:45:05 -0700 | [diff] [blame] | 290 | mResultCallback.onResult(wasCallPlaced, errorCode, errorMsg); |
Santos Cordon | 682fe6b | 2014-05-20 08:56:39 -0700 | [diff] [blame] | 291 | mResultCallback = null; |
| 292 | |
| 293 | mHandler.removeMessages(MSG_EXPIRE); |
| 294 | } else { |
| 295 | Log.wtf(this, "Attempting to return outgoing result twice for call %s", mCall); |
| 296 | } |
| 297 | } |
Ihab Awad | 55a3428 | 2014-06-18 10:31:09 -0700 | [diff] [blame] | 298 | |
| 299 | // If we are possibly attempting to call a local emergency number, ensure that the |
| 300 | // plain PSTN call service, if it exists, is attempted first. |
| 301 | private void adjustCallServiceDescriptorsForEmergency() { |
| 302 | if (shouldProcessAsEmergency(mCall.getHandle())) { |
| 303 | for (int i = 0; i < mCallServiceDescriptors.size(); i++) { |
| 304 | if (TelephonyUtil.isPstnCallService(mCallServiceDescriptors.get(i))) { |
| 305 | mCallServiceDescriptors.add(0, mCallServiceDescriptors.remove(i)); |
| 306 | return; |
| 307 | } |
| 308 | } |
| 309 | } |
| 310 | } |
| 311 | |
| 312 | private boolean shouldProcessAsEmergency(Uri handle) { |
| 313 | return PhoneNumberUtils.isPotentialLocalEmergencyNumber( |
| 314 | TelecommApp.getInstance(), handle.getSchemeSpecificPart()); |
| 315 | } |
Ben Gilad | 0bf5b91 | 2014-01-28 17:55:57 -0800 | [diff] [blame] | 316 | } |