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