blob: c0198816df4190e60cf99d04c14545e053222a0d [file] [log] [blame]
Sailesh Nepal664837f2014-07-14 16:31:51 -07001/*
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
Tyler Gunn7cc70b42014-09-12 22:17:27 -070017package com.android.server.telecom;
Sailesh Nepal664837f2014-07-14 16:31:51 -070018
Andrew Lee701dc002014-09-11 21:29:12 -070019import android.telecom.DisconnectCause;
Tyler Gunn7cc70b42014-09-12 22:17:27 -070020import android.telecom.ParcelableConnection;
21import android.telecom.PhoneAccount;
22import android.telecom.PhoneAccountHandle;
Sailesh Nepal664837f2014-07-14 16:31:51 -070023
24import java.util.ArrayList;
25import java.util.Iterator;
26import java.util.List;
Ihab Awad293edf22014-07-24 17:52:29 -070027import java.util.Objects;
Sailesh Nepal664837f2014-07-14 16:31:51 -070028
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 */
36final class CreateConnectionProcessor {
Ihab Awad293edf22014-07-24 17:52:29 -070037
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 Awadb78b2762014-07-25 15:16:23 -070042 public final PhoneAccountHandle connectionManagerPhoneAccount;
Ihab Awad293edf22014-07-24 17:52:29 -070043 // The PhoneAccount which we will tell the target connection service to use
44 // for attempting to make the actual phone call
Ihab Awadb78b2762014-07-25 15:16:23 -070045 public final PhoneAccountHandle targetPhoneAccount;
Ihab Awad293edf22014-07-24 17:52:29 -070046
47 public CallAttemptRecord(
Ihab Awadb78b2762014-07-25 15:16:23 -070048 PhoneAccountHandle connectionManagerPhoneAccount,
49 PhoneAccountHandle targetPhoneAccount) {
50 this.connectionManagerPhoneAccount = connectionManagerPhoneAccount;
51 this.targetPhoneAccount = targetPhoneAccount;
Ihab Awad293edf22014-07-24 17:52:29 -070052 }
53
54 @Override
55 public String toString() {
56 return "CallAttemptRecord("
Ihab Awadb78b2762014-07-25 15:16:23 -070057 + Objects.toString(connectionManagerPhoneAccount) + ","
58 + Objects.toString(targetPhoneAccount) + ")";
Ihab Awad293edf22014-07-24 17:52:29 -070059 }
Tyler Gunn6e6f6d12014-08-20 15:22:18 -070060
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 Awad293edf22014-07-24 17:52:29 -070078 }
79
Sailesh Nepal664837f2014-07-14 16:31:51 -070080 private final Call mCall;
81 private final ConnectionServiceRepository mRepository;
Ihab Awad293edf22014-07-24 17:52:29 -070082 private List<CallAttemptRecord> mAttemptRecords;
83 private Iterator<CallAttemptRecord> mAttemptRecordIterator;
Sailesh Nepal664837f2014-07-14 16:31:51 -070084 private CreateConnectionResponse mResponse;
Andrew Lee701dc002014-09-11 21:29:12 -070085 private DisconnectCause mLastErrorDisconnectCause;
Sailesh Nepal664837f2014-07-14 16:31:51 -070086
87 CreateConnectionProcessor(
88 Call call, ConnectionServiceRepository repository, CreateConnectionResponse response) {
89 mCall = call;
90 mRepository = repository;
91 mResponse = response;
92 }
93
94 void process() {
95 Log.v(this, "process");
Ihab Awad293edf22014-07-24 17:52:29 -070096 mAttemptRecords = new ArrayList<>();
Ihab Awadb78b2762014-07-25 15:16:23 -070097 if (mCall.getTargetPhoneAccount() != null) {
Sailesh Nepal7957f9c2014-08-09 17:13:19 -070098 mAttemptRecords.add(new CallAttemptRecord(
99 mCall.getTargetPhoneAccount(), mCall.getTargetPhoneAccount()));
Sailesh Nepal664837f2014-07-14 16:31:51 -0700100 }
Sailesh Nepal7957f9c2014-08-09 17:13:19 -0700101 adjustAttemptsForConnectionManager();
Ihab Awad293edf22014-07-24 17:52:29 -0700102 adjustAttemptsForEmergency();
103 mAttemptRecordIterator = mAttemptRecords.iterator();
Ihab Awad69eb0f52014-07-18 11:20:37 -0700104 attemptNextPhoneAccount();
Sailesh Nepal664837f2014-07-14 16:31:51 -0700105 }
106
107 void abort() {
108 Log.v(this, "abort");
109
110 // Clear the response first to prevent attemptNextConnectionService from attempting any
111 // more services.
112 CreateConnectionResponse response = mResponse;
113 mResponse = null;
114
115 ConnectionServiceWrapper service = mCall.getConnectionService();
116 if (service != null) {
117 service.abort(mCall);
118 mCall.clearConnectionService();
119 }
120 if (response != null) {
Andrew Lee701dc002014-09-11 21:29:12 -0700121 response.handleCreateConnectionFailure(new DisconnectCause(DisconnectCause.LOCAL));
Sailesh Nepal664837f2014-07-14 16:31:51 -0700122 }
123 }
124
Ihab Awad69eb0f52014-07-18 11:20:37 -0700125 private void attemptNextPhoneAccount() {
126 Log.v(this, "attemptNextPhoneAccount");
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700127 PhoneAccountRegistrar registrar = TelecomApp.getInstance().getPhoneAccountRegistrar();
Tyler Gunncb59b672014-08-20 09:02:11 -0700128 CallAttemptRecord attempt = null;
129 if (mAttemptRecordIterator.hasNext()) {
130 attempt = mAttemptRecordIterator.next();
Sailesh Nepal664837f2014-07-14 16:31:51 -0700131
Tyler Gunncb59b672014-08-20 09:02:11 -0700132 if (!registrar.phoneAccountHasPermission(attempt.connectionManagerPhoneAccount)) {
133 Log.w(this,
134 "Connection mgr does not have BIND_CONNECTION_SERVICE for attempt: %s",
135 attempt);
136 attemptNextPhoneAccount();
137 return;
138 }
139
140 // If the target PhoneAccount differs from the ConnectionManager phone acount, ensure it
141 // also has BIND_CONNECTION_SERVICE permission.
142 if (!attempt.connectionManagerPhoneAccount.equals(attempt.targetPhoneAccount) &&
143 !registrar.phoneAccountHasPermission(attempt.targetPhoneAccount)) {
144 Log.w(this,
145 "Target PhoneAccount does not have BIND_CONNECTION_SERVICE for attempt: %s",
146 attempt);
147 attemptNextPhoneAccount();
148 return;
149 }
150 }
151
152 if (mResponse != null && attempt != null) {
Ihab Awad293edf22014-07-24 17:52:29 -0700153 Log.i(this, "Trying attempt %s", attempt);
Evan Charlton94d01622014-07-20 12:32:05 -0700154 ConnectionServiceWrapper service =
Ihab Awadb78b2762014-07-25 15:16:23 -0700155 mRepository.getService(
156 attempt.connectionManagerPhoneAccount.getComponentName());
Sailesh Nepal664837f2014-07-14 16:31:51 -0700157 if (service == null) {
Ihab Awad293edf22014-07-24 17:52:29 -0700158 Log.i(this, "Found no connection service for attempt %s", attempt);
Ihab Awad69eb0f52014-07-18 11:20:37 -0700159 attemptNextPhoneAccount();
Sailesh Nepal664837f2014-07-14 16:31:51 -0700160 } else {
Ihab Awadb78b2762014-07-25 15:16:23 -0700161 mCall.setConnectionManagerPhoneAccount(attempt.connectionManagerPhoneAccount);
162 mCall.setTargetPhoneAccount(attempt.targetPhoneAccount);
Sailesh Nepal664837f2014-07-14 16:31:51 -0700163 mCall.setConnectionService(service);
164 Log.i(this, "Attempting to call from %s", service.getComponentName());
165 service.createConnection(mCall, new Response(service));
166 }
167 } else {
Ihab Awad69eb0f52014-07-18 11:20:37 -0700168 Log.v(this, "attemptNextPhoneAccount, no more accounts, failing");
Sailesh Nepal664837f2014-07-14 16:31:51 -0700169 if (mResponse != null) {
Andrew Lee701dc002014-09-11 21:29:12 -0700170 mResponse.handleCreateConnectionFailure(mLastErrorDisconnectCause);
Sailesh Nepal664837f2014-07-14 16:31:51 -0700171 mResponse = null;
172 mCall.clearConnectionService();
173 }
174 }
175 }
176
Sailesh Nepal7957f9c2014-08-09 17:13:19 -0700177 private boolean shouldSetConnectionManager() {
178 if (mAttemptRecords.size() == 0) {
179 return false;
Ihab Awad293edf22014-07-24 17:52:29 -0700180 }
Ihab Awadc17294c2014-08-04 19:23:37 -0700181
Sailesh Nepal7957f9c2014-08-09 17:13:19 -0700182 if (mAttemptRecords.size() > 1) {
183 Log.d(this, "shouldSetConnectionManager, error, mAttemptRecords should not have more "
184 + "than 1 record");
185 return false;
186 }
187
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700188 PhoneAccountRegistrar registrar = TelecomApp.getInstance().getPhoneAccountRegistrar();
Sailesh Nepal7957f9c2014-08-09 17:13:19 -0700189 PhoneAccountHandle connectionManager = registrar.getSimCallManager();
190 if (connectionManager == null) {
191 return false;
192 }
193
194 PhoneAccountHandle targetPhoneAccountHandle = mAttemptRecords.get(0).targetPhoneAccount;
195 if (Objects.equals(connectionManager, targetPhoneAccountHandle)) {
196 return false;
197 }
198
199 // Connection managers are only allowed to manage SIM subscriptions.
200 PhoneAccount targetPhoneAccount = registrar.getPhoneAccount(targetPhoneAccountHandle);
201 boolean isSimSubscription = (targetPhoneAccount.getCapabilities() &
202 PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION) != 0;
203 if (!isSimSubscription) {
204 return false;
205 }
206
207 return true;
208 }
209
210 // If there exists a registered connection manager then use it.
211 private void adjustAttemptsForConnectionManager() {
212 if (shouldSetConnectionManager()) {
213 CallAttemptRecord record = new CallAttemptRecord(
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700214 TelecomApp.getInstance().getPhoneAccountRegistrar().getSimCallManager(),
Sailesh Nepal7957f9c2014-08-09 17:13:19 -0700215 mAttemptRecords.get(0).targetPhoneAccount);
216 Log.v(this, "setConnectionManager, changing %s -> %s",
217 mAttemptRecords.get(0).targetPhoneAccount, record);
218 mAttemptRecords.set(0, record);
219 } else {
220 Log.v(this, "setConnectionManager, not changing");
Ihab Awad293edf22014-07-24 17:52:29 -0700221 }
222 }
223
Sailesh Nepal664837f2014-07-14 16:31:51 -0700224 // If we are possibly attempting to call a local emergency number, ensure that the
Ihab Awad69eb0f52014-07-18 11:20:37 -0700225 // plain PSTN connection services are listed, and nothing else.
Ihab Awad293edf22014-07-24 17:52:29 -0700226 private void adjustAttemptsForEmergency() {
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700227 if (TelephonyUtil.shouldProcessAsEmergency(TelecomApp.getInstance(), mCall.getHandle())) {
Ihab Awad69eb0f52014-07-18 11:20:37 -0700228 Log.i(this, "Emergency number detected");
Ihab Awad293edf22014-07-24 17:52:29 -0700229 mAttemptRecords.clear();
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700230 List<PhoneAccount> allAccounts = TelecomApp.getInstance()
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700231 .getPhoneAccountRegistrar().getAllPhoneAccounts();
232 // First, add SIM phone accounts which can place emergency calls.
233 for (PhoneAccount phoneAccount : allAccounts) {
234 if (phoneAccount.hasCapabilities(PhoneAccount.CAPABILITY_PLACE_EMERGENCY_CALLS) &&
235 phoneAccount.hasCapabilities(PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION)) {
236 Log.i(this, "Will try PSTN account %s for emergency",
237 phoneAccount.getAccountHandle());
Ihab Awad293edf22014-07-24 17:52:29 -0700238 mAttemptRecords.add(
239 new CallAttemptRecord(
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700240 phoneAccount.getAccountHandle(),
241 phoneAccount.getAccountHandle()));
Sailesh Nepal664837f2014-07-14 16:31:51 -0700242 }
243 }
Tyler Gunn6e6f6d12014-08-20 15:22:18 -0700244
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700245 // Next, add the connection manager account as a backup if it can place emergency calls.
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700246 PhoneAccountHandle callManagerHandle = TelecomApp.getInstance()
Tyler Gunn6e6f6d12014-08-20 15:22:18 -0700247 .getPhoneAccountRegistrar().getSimCallManager();
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700248 if (callManagerHandle != null) {
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700249 PhoneAccount callManager = TelecomApp.getInstance()
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700250 .getPhoneAccountRegistrar().getPhoneAccount(callManagerHandle);
251 if (callManager.hasCapabilities(PhoneAccount.CAPABILITY_PLACE_EMERGENCY_CALLS)) {
252 CallAttemptRecord callAttemptRecord = new CallAttemptRecord(callManagerHandle,
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700253 TelecomApp.getInstance().getPhoneAccountRegistrar().
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700254 getDefaultOutgoingPhoneAccount(mCall.getHandle().getScheme())
255 );
Tyler Gunn6e6f6d12014-08-20 15:22:18 -0700256
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700257 if (!mAttemptRecords.contains(callAttemptRecord)) {
258 Log.i(this, "Will try Connection Manager account %s for emergency",
259 callManager);
260 mAttemptRecords.add(callAttemptRecord);
261 }
262 }
Tyler Gunn6e6f6d12014-08-20 15:22:18 -0700263 }
Sailesh Nepal664837f2014-07-14 16:31:51 -0700264 }
265 }
266
Sailesh Nepal664837f2014-07-14 16:31:51 -0700267 private class Response implements CreateConnectionResponse {
268 private final ConnectionServiceWrapper mService;
269
270 Response(ConnectionServiceWrapper service) {
271 mService = service;
272 }
273
274 @Override
Ihab Awad80008452014-08-23 20:35:44 -0700275 public void handleCreateConnectionSuccess(
276 CallIdMapper idMapper,
277 ParcelableConnection connection) {
Sailesh Nepal664837f2014-07-14 16:31:51 -0700278 if (mResponse == null) {
Ihab Awadfb5560d2014-08-18 09:32:51 -0700279 // Nobody is listening for this connection attempt any longer; ask the responsible
280 // ConnectionService to tear down any resources associated with the call
Sailesh Nepal664837f2014-07-14 16:31:51 -0700281 mService.abort(mCall);
282 } else {
Ihab Awadfb5560d2014-08-18 09:32:51 -0700283 // Success -- share the good news and remember that we are no longer interested
284 // in hearing about any more attempts
Ihab Awad80008452014-08-23 20:35:44 -0700285 mResponse.handleCreateConnectionSuccess(idMapper, connection);
Santos Cordon72890ce2014-07-21 01:32:04 -0700286 mResponse = null;
Sailesh Nepal664837f2014-07-14 16:31:51 -0700287 }
288 }
289
290 @Override
Andrew Lee701dc002014-09-11 21:29:12 -0700291 public void handleCreateConnectionFailure(DisconnectCause errorDisconnectCause) {
Ihab Awadfb5560d2014-08-18 09:32:51 -0700292 // Failure of some sort; record the reasons for failure and try again if possible
Andrew Lee701dc002014-09-11 21:29:12 -0700293 Log.d(CreateConnectionProcessor.this, "Connection failed: (%s)", errorDisconnectCause);
294 mLastErrorDisconnectCause = errorDisconnectCause;
Ihab Awad69eb0f52014-07-18 11:20:37 -0700295 attemptNextPhoneAccount();
Sailesh Nepal664837f2014-07-14 16:31:51 -0700296 }
Sailesh Nepal664837f2014-07-14 16:31:51 -0700297 }
298}