Sailesh Nepal | 664837f | 2014-07-14 16:31:51 -0700 | [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 | |
Santos Cordon | 72890ce | 2014-07-21 01:32:04 -0700 | [diff] [blame] | 19 | import android.telecomm.ParcelableConnection; |
Sailesh Nepal | 7957f9c | 2014-08-09 17:13:19 -0700 | [diff] [blame] | 20 | import android.telecomm.PhoneAccount; |
Evan Charlton | 8917637 | 2014-07-19 18:23:09 -0700 | [diff] [blame] | 21 | import android.telecomm.PhoneAccountHandle; |
Sailesh Nepal | 664837f | 2014-07-14 16:31:51 -0700 | [diff] [blame] | 22 | import android.telephony.DisconnectCause; |
Sailesh Nepal | 664837f | 2014-07-14 16:31:51 -0700 | [diff] [blame] | 23 | |
| 24 | import java.util.ArrayList; |
| 25 | import java.util.Iterator; |
| 26 | import java.util.List; |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 27 | import java.util.Objects; |
Sailesh Nepal | 664837f | 2014-07-14 16:31:51 -0700 | [diff] [blame] | 28 | |
| 29 | /** |
| 30 | * This class creates connections to place new outgoing calls to attached to an existing incoming |
| 31 | * call. In either case, this class cycles through a set of connection services until: |
| 32 | * - a connection service returns a newly created connection in which case the call is displayed |
| 33 | * to the user |
| 34 | * - a connection service cancels the process, in which case the call is aborted |
| 35 | */ |
| 36 | final class CreateConnectionProcessor { |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 37 | |
| 38 | // Describes information required to attempt to make a phone call |
| 39 | private static class CallAttemptRecord { |
| 40 | // The PhoneAccount describing the target connection service which we will |
| 41 | // contact in order to process an attempt |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 42 | public final PhoneAccountHandle connectionManagerPhoneAccount; |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 43 | // The PhoneAccount which we will tell the target connection service to use |
| 44 | // for attempting to make the actual phone call |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 45 | public final PhoneAccountHandle targetPhoneAccount; |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 46 | |
| 47 | public CallAttemptRecord( |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 48 | PhoneAccountHandle connectionManagerPhoneAccount, |
| 49 | PhoneAccountHandle targetPhoneAccount) { |
| 50 | this.connectionManagerPhoneAccount = connectionManagerPhoneAccount; |
| 51 | this.targetPhoneAccount = targetPhoneAccount; |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | @Override |
| 55 | public String toString() { |
| 56 | return "CallAttemptRecord(" |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 57 | + Objects.toString(connectionManagerPhoneAccount) + "," |
| 58 | + Objects.toString(targetPhoneAccount) + ")"; |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 59 | } |
Tyler Gunn | 6e6f6d1 | 2014-08-20 15:22:18 -0700 | [diff] [blame] | 60 | |
| 61 | /** |
| 62 | * Determines if this instance of {@code CallAttemptRecord} has the same underlying |
| 63 | * {@code PhoneAccountHandle}s as another instance. |
| 64 | * |
| 65 | * @param obj The other instance to compare against. |
| 66 | * @return {@code True} if the {@code CallAttemptRecord}s are equal. |
| 67 | */ |
| 68 | @Override |
| 69 | public boolean equals(Object obj) { |
| 70 | if (obj instanceof CallAttemptRecord) { |
| 71 | CallAttemptRecord other = (CallAttemptRecord) obj; |
| 72 | return Objects.equals(connectionManagerPhoneAccount, |
| 73 | other.connectionManagerPhoneAccount) && |
| 74 | Objects.equals(targetPhoneAccount, other.targetPhoneAccount); |
| 75 | } |
| 76 | return false; |
| 77 | } |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 78 | } |
| 79 | |
Sailesh Nepal | 664837f | 2014-07-14 16:31:51 -0700 | [diff] [blame] | 80 | private final Call mCall; |
| 81 | private final ConnectionServiceRepository mRepository; |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 82 | private List<CallAttemptRecord> mAttemptRecords; |
| 83 | private Iterator<CallAttemptRecord> mAttemptRecordIterator; |
Sailesh Nepal | 664837f | 2014-07-14 16:31:51 -0700 | [diff] [blame] | 84 | private CreateConnectionResponse mResponse; |
Santos Cordon | fd6ca44 | 2014-07-24 15:34:01 -0700 | [diff] [blame] | 85 | private int mLastErrorCode = DisconnectCause.OUTGOING_FAILURE; |
Sailesh Nepal | 664837f | 2014-07-14 16:31:51 -0700 | [diff] [blame] | 86 | private String mLastErrorMsg; |
| 87 | |
| 88 | CreateConnectionProcessor( |
| 89 | Call call, ConnectionServiceRepository repository, CreateConnectionResponse response) { |
| 90 | mCall = call; |
| 91 | mRepository = repository; |
| 92 | mResponse = response; |
| 93 | } |
| 94 | |
| 95 | void process() { |
| 96 | Log.v(this, "process"); |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 97 | mAttemptRecords = new ArrayList<>(); |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 98 | if (mCall.getTargetPhoneAccount() != null) { |
Sailesh Nepal | 7957f9c | 2014-08-09 17:13:19 -0700 | [diff] [blame] | 99 | mAttemptRecords.add(new CallAttemptRecord( |
| 100 | mCall.getTargetPhoneAccount(), mCall.getTargetPhoneAccount())); |
Sailesh Nepal | 664837f | 2014-07-14 16:31:51 -0700 | [diff] [blame] | 101 | } |
Sailesh Nepal | 7957f9c | 2014-08-09 17:13:19 -0700 | [diff] [blame] | 102 | adjustAttemptsForConnectionManager(); |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 103 | adjustAttemptsForEmergency(); |
| 104 | mAttemptRecordIterator = mAttemptRecords.iterator(); |
Ihab Awad | 69eb0f5 | 2014-07-18 11:20:37 -0700 | [diff] [blame] | 105 | attemptNextPhoneAccount(); |
Sailesh Nepal | 664837f | 2014-07-14 16:31:51 -0700 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | void abort() { |
| 109 | Log.v(this, "abort"); |
| 110 | |
| 111 | // Clear the response first to prevent attemptNextConnectionService from attempting any |
| 112 | // more services. |
| 113 | CreateConnectionResponse response = mResponse; |
| 114 | mResponse = null; |
| 115 | |
| 116 | ConnectionServiceWrapper service = mCall.getConnectionService(); |
| 117 | if (service != null) { |
| 118 | service.abort(mCall); |
| 119 | mCall.clearConnectionService(); |
| 120 | } |
| 121 | if (response != null) { |
Ihab Awad | fb5560d | 2014-08-18 09:32:51 -0700 | [diff] [blame] | 122 | response.handleCreateConnectionFailure(DisconnectCause.OUTGOING_CANCELED, null); |
Sailesh Nepal | 664837f | 2014-07-14 16:31:51 -0700 | [diff] [blame] | 123 | } |
| 124 | } |
| 125 | |
Ihab Awad | 69eb0f5 | 2014-07-18 11:20:37 -0700 | [diff] [blame] | 126 | private void attemptNextPhoneAccount() { |
| 127 | Log.v(this, "attemptNextPhoneAccount"); |
Tyler Gunn | cb59b67 | 2014-08-20 09:02:11 -0700 | [diff] [blame] | 128 | PhoneAccountRegistrar registrar = TelecommApp.getInstance().getPhoneAccountRegistrar(); |
| 129 | CallAttemptRecord attempt = null; |
| 130 | if (mAttemptRecordIterator.hasNext()) { |
| 131 | attempt = mAttemptRecordIterator.next(); |
Sailesh Nepal | 664837f | 2014-07-14 16:31:51 -0700 | [diff] [blame] | 132 | |
Tyler Gunn | cb59b67 | 2014-08-20 09:02:11 -0700 | [diff] [blame] | 133 | if (!registrar.phoneAccountHasPermission(attempt.connectionManagerPhoneAccount)) { |
| 134 | Log.w(this, |
| 135 | "Connection mgr does not have BIND_CONNECTION_SERVICE for attempt: %s", |
| 136 | attempt); |
| 137 | attemptNextPhoneAccount(); |
| 138 | return; |
| 139 | } |
| 140 | |
| 141 | // If the target PhoneAccount differs from the ConnectionManager phone acount, ensure it |
| 142 | // also has BIND_CONNECTION_SERVICE permission. |
| 143 | if (!attempt.connectionManagerPhoneAccount.equals(attempt.targetPhoneAccount) && |
| 144 | !registrar.phoneAccountHasPermission(attempt.targetPhoneAccount)) { |
| 145 | Log.w(this, |
| 146 | "Target PhoneAccount does not have BIND_CONNECTION_SERVICE for attempt: %s", |
| 147 | attempt); |
| 148 | attemptNextPhoneAccount(); |
| 149 | return; |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | if (mResponse != null && attempt != null) { |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 154 | Log.i(this, "Trying attempt %s", attempt); |
Evan Charlton | 94d0162 | 2014-07-20 12:32:05 -0700 | [diff] [blame] | 155 | ConnectionServiceWrapper service = |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 156 | mRepository.getService( |
| 157 | attempt.connectionManagerPhoneAccount.getComponentName()); |
Sailesh Nepal | 664837f | 2014-07-14 16:31:51 -0700 | [diff] [blame] | 158 | if (service == null) { |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 159 | Log.i(this, "Found no connection service for attempt %s", attempt); |
Ihab Awad | 69eb0f5 | 2014-07-18 11:20:37 -0700 | [diff] [blame] | 160 | attemptNextPhoneAccount(); |
Sailesh Nepal | 664837f | 2014-07-14 16:31:51 -0700 | [diff] [blame] | 161 | } else { |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 162 | mCall.setConnectionManagerPhoneAccount(attempt.connectionManagerPhoneAccount); |
| 163 | mCall.setTargetPhoneAccount(attempt.targetPhoneAccount); |
Sailesh Nepal | 664837f | 2014-07-14 16:31:51 -0700 | [diff] [blame] | 164 | mCall.setConnectionService(service); |
| 165 | Log.i(this, "Attempting to call from %s", service.getComponentName()); |
| 166 | service.createConnection(mCall, new Response(service)); |
| 167 | } |
| 168 | } else { |
Ihab Awad | 69eb0f5 | 2014-07-18 11:20:37 -0700 | [diff] [blame] | 169 | Log.v(this, "attemptNextPhoneAccount, no more accounts, failing"); |
Sailesh Nepal | 664837f | 2014-07-14 16:31:51 -0700 | [diff] [blame] | 170 | if (mResponse != null) { |
Ihab Awad | fb5560d | 2014-08-18 09:32:51 -0700 | [diff] [blame] | 171 | mResponse.handleCreateConnectionFailure(mLastErrorCode, mLastErrorMsg); |
Sailesh Nepal | 664837f | 2014-07-14 16:31:51 -0700 | [diff] [blame] | 172 | mResponse = null; |
| 173 | mCall.clearConnectionService(); |
| 174 | } |
| 175 | } |
| 176 | } |
| 177 | |
Sailesh Nepal | 7957f9c | 2014-08-09 17:13:19 -0700 | [diff] [blame] | 178 | private boolean shouldSetConnectionManager() { |
| 179 | if (mAttemptRecords.size() == 0) { |
| 180 | return false; |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 181 | } |
Ihab Awad | c17294c | 2014-08-04 19:23:37 -0700 | [diff] [blame] | 182 | |
Sailesh Nepal | 7957f9c | 2014-08-09 17:13:19 -0700 | [diff] [blame] | 183 | if (mAttemptRecords.size() > 1) { |
| 184 | Log.d(this, "shouldSetConnectionManager, error, mAttemptRecords should not have more " |
| 185 | + "than 1 record"); |
| 186 | return false; |
| 187 | } |
| 188 | |
| 189 | PhoneAccountRegistrar registrar = TelecommApp.getInstance().getPhoneAccountRegistrar(); |
| 190 | PhoneAccountHandle connectionManager = registrar.getSimCallManager(); |
| 191 | if (connectionManager == null) { |
| 192 | return false; |
| 193 | } |
| 194 | |
| 195 | PhoneAccountHandle targetPhoneAccountHandle = mAttemptRecords.get(0).targetPhoneAccount; |
| 196 | if (Objects.equals(connectionManager, targetPhoneAccountHandle)) { |
| 197 | return false; |
| 198 | } |
| 199 | |
| 200 | // Connection managers are only allowed to manage SIM subscriptions. |
| 201 | PhoneAccount targetPhoneAccount = registrar.getPhoneAccount(targetPhoneAccountHandle); |
| 202 | boolean isSimSubscription = (targetPhoneAccount.getCapabilities() & |
| 203 | PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION) != 0; |
| 204 | if (!isSimSubscription) { |
| 205 | return false; |
| 206 | } |
| 207 | |
| 208 | return true; |
| 209 | } |
| 210 | |
| 211 | // If there exists a registered connection manager then use it. |
| 212 | private void adjustAttemptsForConnectionManager() { |
| 213 | if (shouldSetConnectionManager()) { |
| 214 | CallAttemptRecord record = new CallAttemptRecord( |
| 215 | TelecommApp.getInstance().getPhoneAccountRegistrar().getSimCallManager(), |
| 216 | mAttemptRecords.get(0).targetPhoneAccount); |
| 217 | Log.v(this, "setConnectionManager, changing %s -> %s", |
| 218 | mAttemptRecords.get(0).targetPhoneAccount, record); |
| 219 | mAttemptRecords.set(0, record); |
| 220 | } else { |
| 221 | Log.v(this, "setConnectionManager, not changing"); |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 222 | } |
| 223 | } |
| 224 | |
Sailesh Nepal | 664837f | 2014-07-14 16:31:51 -0700 | [diff] [blame] | 225 | // If we are possibly attempting to call a local emergency number, ensure that the |
Ihab Awad | 69eb0f5 | 2014-07-18 11:20:37 -0700 | [diff] [blame] | 226 | // plain PSTN connection services are listed, and nothing else. |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 227 | private void adjustAttemptsForEmergency() { |
Ihab Awad | 69eb0f5 | 2014-07-18 11:20:37 -0700 | [diff] [blame] | 228 | if (TelephonyUtil.shouldProcessAsEmergency(TelecommApp.getInstance(), mCall.getHandle())) { |
| 229 | Log.i(this, "Emergency number detected"); |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 230 | mAttemptRecords.clear(); |
Evan Charlton | 94d0162 | 2014-07-20 12:32:05 -0700 | [diff] [blame] | 231 | List<PhoneAccountHandle> allAccountHandles = TelecommApp.getInstance() |
Ihab Awad | 6fb37c8 | 2014-08-07 19:48:57 -0700 | [diff] [blame] | 232 | .getPhoneAccountRegistrar().getOutgoingPhoneAccounts(); |
Tyler Gunn | 6e6f6d1 | 2014-08-20 15:22:18 -0700 | [diff] [blame] | 233 | // First, add the PSTN phone account |
Evan Charlton | 94d0162 | 2014-07-20 12:32:05 -0700 | [diff] [blame] | 234 | for (int i = 0; i < allAccountHandles.size(); i++) { |
| 235 | if (TelephonyUtil.isPstnComponentName( |
| 236 | allAccountHandles.get(i).getComponentName())) { |
| 237 | Log.i(this, "Will try PSTN account %s for emergency", allAccountHandles.get(i)); |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 238 | mAttemptRecords.add( |
| 239 | new CallAttemptRecord( |
| 240 | allAccountHandles.get(i), |
| 241 | allAccountHandles.get(i))); |
Sailesh Nepal | 664837f | 2014-07-14 16:31:51 -0700 | [diff] [blame] | 242 | } |
| 243 | } |
Tyler Gunn | 6e6f6d1 | 2014-08-20 15:22:18 -0700 | [diff] [blame] | 244 | |
| 245 | // Next, add the connection manager account as a backup. |
| 246 | PhoneAccountHandle callManager = TelecommApp.getInstance() |
| 247 | .getPhoneAccountRegistrar().getSimCallManager(); |
| 248 | CallAttemptRecord callAttemptRecord = new CallAttemptRecord(callManager, |
| 249 | TelecommApp.getInstance().getPhoneAccountRegistrar(). |
| 250 | getDefaultOutgoingPhoneAccount()); |
| 251 | |
| 252 | if (callManager != null && !mAttemptRecords.contains(callAttemptRecord)) { |
| 253 | Log.i(this, "Will try Connection Manager account %s for emergency", |
| 254 | callManager); |
| 255 | mAttemptRecords.add(callAttemptRecord); |
| 256 | } |
Sailesh Nepal | 664837f | 2014-07-14 16:31:51 -0700 | [diff] [blame] | 257 | } |
| 258 | } |
| 259 | |
Sailesh Nepal | 664837f | 2014-07-14 16:31:51 -0700 | [diff] [blame] | 260 | private class Response implements CreateConnectionResponse { |
| 261 | private final ConnectionServiceWrapper mService; |
| 262 | |
| 263 | Response(ConnectionServiceWrapper service) { |
| 264 | mService = service; |
| 265 | } |
| 266 | |
| 267 | @Override |
Ihab Awad | fb5560d | 2014-08-18 09:32:51 -0700 | [diff] [blame] | 268 | public void handleCreateConnectionSuccess(ParcelableConnection connection) { |
Sailesh Nepal | 664837f | 2014-07-14 16:31:51 -0700 | [diff] [blame] | 269 | if (mResponse == null) { |
Ihab Awad | fb5560d | 2014-08-18 09:32:51 -0700 | [diff] [blame] | 270 | // Nobody is listening for this connection attempt any longer; ask the responsible |
| 271 | // ConnectionService to tear down any resources associated with the call |
Sailesh Nepal | 664837f | 2014-07-14 16:31:51 -0700 | [diff] [blame] | 272 | mService.abort(mCall); |
| 273 | } else { |
Ihab Awad | fb5560d | 2014-08-18 09:32:51 -0700 | [diff] [blame] | 274 | // Success -- share the good news and remember that we are no longer interested |
| 275 | // in hearing about any more attempts |
| 276 | mResponse.handleCreateConnectionSuccess(connection); |
Santos Cordon | 72890ce | 2014-07-21 01:32:04 -0700 | [diff] [blame] | 277 | mResponse = null; |
Sailesh Nepal | 664837f | 2014-07-14 16:31:51 -0700 | [diff] [blame] | 278 | } |
| 279 | } |
| 280 | |
| 281 | @Override |
Ihab Awad | fb5560d | 2014-08-18 09:32:51 -0700 | [diff] [blame] | 282 | public void handleCreateConnectionFailure(int code, String msg) { |
| 283 | // Failure of some sort; record the reasons for failure and try again if possible |
| 284 | Log.d(CreateConnectionProcessor.this, "Connection failed: %d (%s)", code, msg); |
Sailesh Nepal | 664837f | 2014-07-14 16:31:51 -0700 | [diff] [blame] | 285 | mLastErrorCode = code; |
| 286 | mLastErrorMsg = msg; |
Ihab Awad | 69eb0f5 | 2014-07-18 11:20:37 -0700 | [diff] [blame] | 287 | attemptNextPhoneAccount(); |
Sailesh Nepal | 664837f | 2014-07-14 16:31:51 -0700 | [diff] [blame] | 288 | } |
Sailesh Nepal | 664837f | 2014-07-14 16:31:51 -0700 | [diff] [blame] | 289 | } |
| 290 | } |