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