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"); |
Sailesh Nepal | 664837f | 2014-07-14 16:31:51 -0700 | [diff] [blame] | 128 | |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 129 | if (mResponse != null && mAttemptRecordIterator.hasNext()) { |
| 130 | CallAttemptRecord attempt = mAttemptRecordIterator.next(); |
| 131 | Log.i(this, "Trying attempt %s", attempt); |
Evan Charlton | 94d0162 | 2014-07-20 12:32:05 -0700 | [diff] [blame] | 132 | ConnectionServiceWrapper service = |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 133 | mRepository.getService( |
| 134 | attempt.connectionManagerPhoneAccount.getComponentName()); |
Sailesh Nepal | 664837f | 2014-07-14 16:31:51 -0700 | [diff] [blame] | 135 | if (service == null) { |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 136 | Log.i(this, "Found no connection service for attempt %s", attempt); |
Ihab Awad | 69eb0f5 | 2014-07-18 11:20:37 -0700 | [diff] [blame] | 137 | attemptNextPhoneAccount(); |
Sailesh Nepal | 664837f | 2014-07-14 16:31:51 -0700 | [diff] [blame] | 138 | } else { |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 139 | mCall.setConnectionManagerPhoneAccount(attempt.connectionManagerPhoneAccount); |
| 140 | mCall.setTargetPhoneAccount(attempt.targetPhoneAccount); |
Sailesh Nepal | 664837f | 2014-07-14 16:31:51 -0700 | [diff] [blame] | 141 | mCall.setConnectionService(service); |
| 142 | Log.i(this, "Attempting to call from %s", service.getComponentName()); |
| 143 | service.createConnection(mCall, new Response(service)); |
| 144 | } |
| 145 | } else { |
Ihab Awad | 69eb0f5 | 2014-07-18 11:20:37 -0700 | [diff] [blame] | 146 | Log.v(this, "attemptNextPhoneAccount, no more accounts, failing"); |
Sailesh Nepal | 664837f | 2014-07-14 16:31:51 -0700 | [diff] [blame] | 147 | if (mResponse != null) { |
Ihab Awad | fb5560d | 2014-08-18 09:32:51 -0700 | [diff] [blame] | 148 | mResponse.handleCreateConnectionFailure(mLastErrorCode, mLastErrorMsg); |
Sailesh Nepal | 664837f | 2014-07-14 16:31:51 -0700 | [diff] [blame] | 149 | mResponse = null; |
| 150 | mCall.clearConnectionService(); |
| 151 | } |
| 152 | } |
| 153 | } |
| 154 | |
Sailesh Nepal | 7957f9c | 2014-08-09 17:13:19 -0700 | [diff] [blame] | 155 | private boolean shouldSetConnectionManager() { |
| 156 | if (mAttemptRecords.size() == 0) { |
| 157 | return false; |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 158 | } |
Ihab Awad | c17294c | 2014-08-04 19:23:37 -0700 | [diff] [blame] | 159 | |
Sailesh Nepal | 7957f9c | 2014-08-09 17:13:19 -0700 | [diff] [blame] | 160 | if (mAttemptRecords.size() > 1) { |
| 161 | Log.d(this, "shouldSetConnectionManager, error, mAttemptRecords should not have more " |
| 162 | + "than 1 record"); |
| 163 | return false; |
| 164 | } |
| 165 | |
| 166 | PhoneAccountRegistrar registrar = TelecommApp.getInstance().getPhoneAccountRegistrar(); |
| 167 | PhoneAccountHandle connectionManager = registrar.getSimCallManager(); |
| 168 | if (connectionManager == null) { |
| 169 | return false; |
| 170 | } |
| 171 | |
| 172 | PhoneAccountHandle targetPhoneAccountHandle = mAttemptRecords.get(0).targetPhoneAccount; |
| 173 | if (Objects.equals(connectionManager, targetPhoneAccountHandle)) { |
| 174 | return false; |
| 175 | } |
| 176 | |
| 177 | // Connection managers are only allowed to manage SIM subscriptions. |
| 178 | PhoneAccount targetPhoneAccount = registrar.getPhoneAccount(targetPhoneAccountHandle); |
| 179 | boolean isSimSubscription = (targetPhoneAccount.getCapabilities() & |
| 180 | PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION) != 0; |
| 181 | if (!isSimSubscription) { |
| 182 | return false; |
| 183 | } |
| 184 | |
| 185 | return true; |
| 186 | } |
| 187 | |
| 188 | // If there exists a registered connection manager then use it. |
| 189 | private void adjustAttemptsForConnectionManager() { |
| 190 | if (shouldSetConnectionManager()) { |
| 191 | CallAttemptRecord record = new CallAttemptRecord( |
| 192 | TelecommApp.getInstance().getPhoneAccountRegistrar().getSimCallManager(), |
| 193 | mAttemptRecords.get(0).targetPhoneAccount); |
| 194 | Log.v(this, "setConnectionManager, changing %s -> %s", |
| 195 | mAttemptRecords.get(0).targetPhoneAccount, record); |
| 196 | mAttemptRecords.set(0, record); |
| 197 | } else { |
| 198 | Log.v(this, "setConnectionManager, not changing"); |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 199 | } |
| 200 | } |
| 201 | |
Sailesh Nepal | 664837f | 2014-07-14 16:31:51 -0700 | [diff] [blame] | 202 | // 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] | 203 | // plain PSTN connection services are listed, and nothing else. |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 204 | private void adjustAttemptsForEmergency() { |
Ihab Awad | 69eb0f5 | 2014-07-18 11:20:37 -0700 | [diff] [blame] | 205 | if (TelephonyUtil.shouldProcessAsEmergency(TelecommApp.getInstance(), mCall.getHandle())) { |
| 206 | Log.i(this, "Emergency number detected"); |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 207 | mAttemptRecords.clear(); |
Evan Charlton | 94d0162 | 2014-07-20 12:32:05 -0700 | [diff] [blame] | 208 | List<PhoneAccountHandle> allAccountHandles = TelecommApp.getInstance() |
Ihab Awad | 6fb37c8 | 2014-08-07 19:48:57 -0700 | [diff] [blame] | 209 | .getPhoneAccountRegistrar().getOutgoingPhoneAccounts(); |
Tyler Gunn | 6e6f6d1 | 2014-08-20 15:22:18 -0700 | [diff] [blame^] | 210 | // First, add the PSTN phone account |
Evan Charlton | 94d0162 | 2014-07-20 12:32:05 -0700 | [diff] [blame] | 211 | for (int i = 0; i < allAccountHandles.size(); i++) { |
| 212 | if (TelephonyUtil.isPstnComponentName( |
| 213 | allAccountHandles.get(i).getComponentName())) { |
| 214 | 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] | 215 | mAttemptRecords.add( |
| 216 | new CallAttemptRecord( |
| 217 | allAccountHandles.get(i), |
| 218 | allAccountHandles.get(i))); |
Sailesh Nepal | 664837f | 2014-07-14 16:31:51 -0700 | [diff] [blame] | 219 | } |
| 220 | } |
Tyler Gunn | 6e6f6d1 | 2014-08-20 15:22:18 -0700 | [diff] [blame^] | 221 | |
| 222 | // Next, add the connection manager account as a backup. |
| 223 | PhoneAccountHandle callManager = TelecommApp.getInstance() |
| 224 | .getPhoneAccountRegistrar().getSimCallManager(); |
| 225 | CallAttemptRecord callAttemptRecord = new CallAttemptRecord(callManager, |
| 226 | TelecommApp.getInstance().getPhoneAccountRegistrar(). |
| 227 | getDefaultOutgoingPhoneAccount()); |
| 228 | |
| 229 | if (callManager != null && !mAttemptRecords.contains(callAttemptRecord)) { |
| 230 | Log.i(this, "Will try Connection Manager account %s for emergency", |
| 231 | callManager); |
| 232 | mAttemptRecords.add(callAttemptRecord); |
| 233 | } |
Sailesh Nepal | 664837f | 2014-07-14 16:31:51 -0700 | [diff] [blame] | 234 | } |
| 235 | } |
| 236 | |
Sailesh Nepal | 664837f | 2014-07-14 16:31:51 -0700 | [diff] [blame] | 237 | private class Response implements CreateConnectionResponse { |
| 238 | private final ConnectionServiceWrapper mService; |
| 239 | |
| 240 | Response(ConnectionServiceWrapper service) { |
| 241 | mService = service; |
| 242 | } |
| 243 | |
| 244 | @Override |
Ihab Awad | fb5560d | 2014-08-18 09:32:51 -0700 | [diff] [blame] | 245 | public void handleCreateConnectionSuccess(ParcelableConnection connection) { |
Sailesh Nepal | 664837f | 2014-07-14 16:31:51 -0700 | [diff] [blame] | 246 | if (mResponse == null) { |
Ihab Awad | fb5560d | 2014-08-18 09:32:51 -0700 | [diff] [blame] | 247 | // Nobody is listening for this connection attempt any longer; ask the responsible |
| 248 | // ConnectionService to tear down any resources associated with the call |
Sailesh Nepal | 664837f | 2014-07-14 16:31:51 -0700 | [diff] [blame] | 249 | mService.abort(mCall); |
| 250 | } else { |
Ihab Awad | fb5560d | 2014-08-18 09:32:51 -0700 | [diff] [blame] | 251 | // Success -- share the good news and remember that we are no longer interested |
| 252 | // in hearing about any more attempts |
| 253 | mResponse.handleCreateConnectionSuccess(connection); |
Santos Cordon | 72890ce | 2014-07-21 01:32:04 -0700 | [diff] [blame] | 254 | mResponse = null; |
Sailesh Nepal | 664837f | 2014-07-14 16:31:51 -0700 | [diff] [blame] | 255 | } |
| 256 | } |
| 257 | |
| 258 | @Override |
Ihab Awad | fb5560d | 2014-08-18 09:32:51 -0700 | [diff] [blame] | 259 | public void handleCreateConnectionFailure(int code, String msg) { |
| 260 | // Failure of some sort; record the reasons for failure and try again if possible |
| 261 | Log.d(CreateConnectionProcessor.this, "Connection failed: %d (%s)", code, msg); |
Sailesh Nepal | 664837f | 2014-07-14 16:31:51 -0700 | [diff] [blame] | 262 | mLastErrorCode = code; |
| 263 | mLastErrorMsg = msg; |
Ihab Awad | 69eb0f5 | 2014-07-18 11:20:37 -0700 | [diff] [blame] | 264 | attemptNextPhoneAccount(); |
Sailesh Nepal | 664837f | 2014-07-14 16:31:51 -0700 | [diff] [blame] | 265 | } |
Sailesh Nepal | 664837f | 2014-07-14 16:31:51 -0700 | [diff] [blame] | 266 | } |
| 267 | } |