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 | } |
| 60 | } |
| 61 | |
Sailesh Nepal | 664837f | 2014-07-14 16:31:51 -0700 | [diff] [blame] | 62 | private final Call mCall; |
| 63 | private final ConnectionServiceRepository mRepository; |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 64 | private List<CallAttemptRecord> mAttemptRecords; |
| 65 | private Iterator<CallAttemptRecord> mAttemptRecordIterator; |
Sailesh Nepal | 664837f | 2014-07-14 16:31:51 -0700 | [diff] [blame] | 66 | private CreateConnectionResponse mResponse; |
Santos Cordon | fd6ca44 | 2014-07-24 15:34:01 -0700 | [diff] [blame] | 67 | private int mLastErrorCode = DisconnectCause.OUTGOING_FAILURE; |
Sailesh Nepal | 664837f | 2014-07-14 16:31:51 -0700 | [diff] [blame] | 68 | private String mLastErrorMsg; |
| 69 | |
| 70 | CreateConnectionProcessor( |
| 71 | Call call, ConnectionServiceRepository repository, CreateConnectionResponse response) { |
| 72 | mCall = call; |
| 73 | mRepository = repository; |
| 74 | mResponse = response; |
| 75 | } |
| 76 | |
| 77 | void process() { |
| 78 | Log.v(this, "process"); |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 79 | mAttemptRecords = new ArrayList<>(); |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 80 | if (mCall.getTargetPhoneAccount() != null) { |
Sailesh Nepal | 7957f9c | 2014-08-09 17:13:19 -0700 | [diff] [blame] | 81 | mAttemptRecords.add(new CallAttemptRecord( |
| 82 | mCall.getTargetPhoneAccount(), mCall.getTargetPhoneAccount())); |
Sailesh Nepal | 664837f | 2014-07-14 16:31:51 -0700 | [diff] [blame] | 83 | } |
Sailesh Nepal | 7957f9c | 2014-08-09 17:13:19 -0700 | [diff] [blame] | 84 | adjustAttemptsForConnectionManager(); |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 85 | adjustAttemptsForEmergency(); |
| 86 | mAttemptRecordIterator = mAttemptRecords.iterator(); |
Ihab Awad | 69eb0f5 | 2014-07-18 11:20:37 -0700 | [diff] [blame] | 87 | attemptNextPhoneAccount(); |
Sailesh Nepal | 664837f | 2014-07-14 16:31:51 -0700 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | void abort() { |
| 91 | Log.v(this, "abort"); |
| 92 | |
| 93 | // Clear the response first to prevent attemptNextConnectionService from attempting any |
| 94 | // more services. |
| 95 | CreateConnectionResponse response = mResponse; |
| 96 | mResponse = null; |
| 97 | |
| 98 | ConnectionServiceWrapper service = mCall.getConnectionService(); |
| 99 | if (service != null) { |
| 100 | service.abort(mCall); |
| 101 | mCall.clearConnectionService(); |
| 102 | } |
| 103 | if (response != null) { |
Ihab Awad | fb5560d | 2014-08-18 09:32:51 -0700 | [diff] [blame^] | 104 | response.handleCreateConnectionFailure(DisconnectCause.OUTGOING_CANCELED, null); |
Sailesh Nepal | 664837f | 2014-07-14 16:31:51 -0700 | [diff] [blame] | 105 | } |
| 106 | } |
| 107 | |
Ihab Awad | 69eb0f5 | 2014-07-18 11:20:37 -0700 | [diff] [blame] | 108 | private void attemptNextPhoneAccount() { |
| 109 | Log.v(this, "attemptNextPhoneAccount"); |
Sailesh Nepal | 664837f | 2014-07-14 16:31:51 -0700 | [diff] [blame] | 110 | |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 111 | if (mResponse != null && mAttemptRecordIterator.hasNext()) { |
| 112 | CallAttemptRecord attempt = mAttemptRecordIterator.next(); |
| 113 | Log.i(this, "Trying attempt %s", attempt); |
Evan Charlton | 94d0162 | 2014-07-20 12:32:05 -0700 | [diff] [blame] | 114 | ConnectionServiceWrapper service = |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 115 | mRepository.getService( |
| 116 | attempt.connectionManagerPhoneAccount.getComponentName()); |
Sailesh Nepal | 664837f | 2014-07-14 16:31:51 -0700 | [diff] [blame] | 117 | if (service == null) { |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 118 | Log.i(this, "Found no connection service for attempt %s", attempt); |
Ihab Awad | 69eb0f5 | 2014-07-18 11:20:37 -0700 | [diff] [blame] | 119 | attemptNextPhoneAccount(); |
Sailesh Nepal | 664837f | 2014-07-14 16:31:51 -0700 | [diff] [blame] | 120 | } else { |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 121 | mCall.setConnectionManagerPhoneAccount(attempt.connectionManagerPhoneAccount); |
| 122 | mCall.setTargetPhoneAccount(attempt.targetPhoneAccount); |
Sailesh Nepal | 664837f | 2014-07-14 16:31:51 -0700 | [diff] [blame] | 123 | mCall.setConnectionService(service); |
| 124 | Log.i(this, "Attempting to call from %s", service.getComponentName()); |
| 125 | service.createConnection(mCall, new Response(service)); |
| 126 | } |
| 127 | } else { |
Ihab Awad | 69eb0f5 | 2014-07-18 11:20:37 -0700 | [diff] [blame] | 128 | Log.v(this, "attemptNextPhoneAccount, no more accounts, failing"); |
Sailesh Nepal | 664837f | 2014-07-14 16:31:51 -0700 | [diff] [blame] | 129 | if (mResponse != null) { |
Ihab Awad | fb5560d | 2014-08-18 09:32:51 -0700 | [diff] [blame^] | 130 | mResponse.handleCreateConnectionFailure(mLastErrorCode, mLastErrorMsg); |
Sailesh Nepal | 664837f | 2014-07-14 16:31:51 -0700 | [diff] [blame] | 131 | mResponse = null; |
| 132 | mCall.clearConnectionService(); |
| 133 | } |
| 134 | } |
| 135 | } |
| 136 | |
Sailesh Nepal | 7957f9c | 2014-08-09 17:13:19 -0700 | [diff] [blame] | 137 | private boolean shouldSetConnectionManager() { |
| 138 | if (mAttemptRecords.size() == 0) { |
| 139 | return false; |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 140 | } |
Ihab Awad | c17294c | 2014-08-04 19:23:37 -0700 | [diff] [blame] | 141 | |
Sailesh Nepal | 7957f9c | 2014-08-09 17:13:19 -0700 | [diff] [blame] | 142 | if (mAttemptRecords.size() > 1) { |
| 143 | Log.d(this, "shouldSetConnectionManager, error, mAttemptRecords should not have more " |
| 144 | + "than 1 record"); |
| 145 | return false; |
| 146 | } |
| 147 | |
| 148 | PhoneAccountRegistrar registrar = TelecommApp.getInstance().getPhoneAccountRegistrar(); |
| 149 | PhoneAccountHandle connectionManager = registrar.getSimCallManager(); |
| 150 | if (connectionManager == null) { |
| 151 | return false; |
| 152 | } |
| 153 | |
| 154 | PhoneAccountHandle targetPhoneAccountHandle = mAttemptRecords.get(0).targetPhoneAccount; |
| 155 | if (Objects.equals(connectionManager, targetPhoneAccountHandle)) { |
| 156 | return false; |
| 157 | } |
| 158 | |
| 159 | // Connection managers are only allowed to manage SIM subscriptions. |
| 160 | PhoneAccount targetPhoneAccount = registrar.getPhoneAccount(targetPhoneAccountHandle); |
| 161 | boolean isSimSubscription = (targetPhoneAccount.getCapabilities() & |
| 162 | PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION) != 0; |
| 163 | if (!isSimSubscription) { |
| 164 | return false; |
| 165 | } |
| 166 | |
| 167 | return true; |
| 168 | } |
| 169 | |
| 170 | // If there exists a registered connection manager then use it. |
| 171 | private void adjustAttemptsForConnectionManager() { |
| 172 | if (shouldSetConnectionManager()) { |
| 173 | CallAttemptRecord record = new CallAttemptRecord( |
| 174 | TelecommApp.getInstance().getPhoneAccountRegistrar().getSimCallManager(), |
| 175 | mAttemptRecords.get(0).targetPhoneAccount); |
| 176 | Log.v(this, "setConnectionManager, changing %s -> %s", |
| 177 | mAttemptRecords.get(0).targetPhoneAccount, record); |
| 178 | mAttemptRecords.set(0, record); |
| 179 | } else { |
| 180 | Log.v(this, "setConnectionManager, not changing"); |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 181 | } |
| 182 | } |
| 183 | |
Sailesh Nepal | 664837f | 2014-07-14 16:31:51 -0700 | [diff] [blame] | 184 | // 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] | 185 | // plain PSTN connection services are listed, and nothing else. |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 186 | private void adjustAttemptsForEmergency() { |
Ihab Awad | 69eb0f5 | 2014-07-18 11:20:37 -0700 | [diff] [blame] | 187 | if (TelephonyUtil.shouldProcessAsEmergency(TelecommApp.getInstance(), mCall.getHandle())) { |
| 188 | Log.i(this, "Emergency number detected"); |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 189 | mAttemptRecords.clear(); |
Evan Charlton | 94d0162 | 2014-07-20 12:32:05 -0700 | [diff] [blame] | 190 | List<PhoneAccountHandle> allAccountHandles = TelecommApp.getInstance() |
Ihab Awad | 6fb37c8 | 2014-08-07 19:48:57 -0700 | [diff] [blame] | 191 | .getPhoneAccountRegistrar().getOutgoingPhoneAccounts(); |
Evan Charlton | 94d0162 | 2014-07-20 12:32:05 -0700 | [diff] [blame] | 192 | for (int i = 0; i < allAccountHandles.size(); i++) { |
| 193 | if (TelephonyUtil.isPstnComponentName( |
| 194 | allAccountHandles.get(i).getComponentName())) { |
| 195 | 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] | 196 | mAttemptRecords.add( |
| 197 | new CallAttemptRecord( |
| 198 | allAccountHandles.get(i), |
| 199 | allAccountHandles.get(i))); |
Sailesh Nepal | 664837f | 2014-07-14 16:31:51 -0700 | [diff] [blame] | 200 | } |
| 201 | } |
| 202 | } |
| 203 | } |
| 204 | |
Sailesh Nepal | 664837f | 2014-07-14 16:31:51 -0700 | [diff] [blame] | 205 | private class Response implements CreateConnectionResponse { |
| 206 | private final ConnectionServiceWrapper mService; |
| 207 | |
| 208 | Response(ConnectionServiceWrapper service) { |
| 209 | mService = service; |
| 210 | } |
| 211 | |
| 212 | @Override |
Ihab Awad | fb5560d | 2014-08-18 09:32:51 -0700 | [diff] [blame^] | 213 | public void handleCreateConnectionSuccess(ParcelableConnection connection) { |
Sailesh Nepal | 664837f | 2014-07-14 16:31:51 -0700 | [diff] [blame] | 214 | if (mResponse == null) { |
Ihab Awad | fb5560d | 2014-08-18 09:32:51 -0700 | [diff] [blame^] | 215 | // Nobody is listening for this connection attempt any longer; ask the responsible |
| 216 | // ConnectionService to tear down any resources associated with the call |
Sailesh Nepal | 664837f | 2014-07-14 16:31:51 -0700 | [diff] [blame] | 217 | mService.abort(mCall); |
| 218 | } else { |
Ihab Awad | fb5560d | 2014-08-18 09:32:51 -0700 | [diff] [blame^] | 219 | // Success -- share the good news and remember that we are no longer interested |
| 220 | // in hearing about any more attempts |
| 221 | mResponse.handleCreateConnectionSuccess(connection); |
Santos Cordon | 72890ce | 2014-07-21 01:32:04 -0700 | [diff] [blame] | 222 | mResponse = null; |
Sailesh Nepal | 664837f | 2014-07-14 16:31:51 -0700 | [diff] [blame] | 223 | } |
| 224 | } |
| 225 | |
| 226 | @Override |
Ihab Awad | fb5560d | 2014-08-18 09:32:51 -0700 | [diff] [blame^] | 227 | public void handleCreateConnectionFailure(int code, String msg) { |
| 228 | // Failure of some sort; record the reasons for failure and try again if possible |
| 229 | Log.d(CreateConnectionProcessor.this, "Connection failed: %d (%s)", code, msg); |
Sailesh Nepal | 664837f | 2014-07-14 16:31:51 -0700 | [diff] [blame] | 230 | mLastErrorCode = code; |
| 231 | mLastErrorMsg = msg; |
Ihab Awad | 69eb0f5 | 2014-07-18 11:20:37 -0700 | [diff] [blame] | 232 | attemptNextPhoneAccount(); |
Sailesh Nepal | 664837f | 2014-07-14 16:31:51 -0700 | [diff] [blame] | 233 | } |
Sailesh Nepal | 664837f | 2014-07-14 16:31:51 -0700 | [diff] [blame] | 234 | } |
| 235 | } |