blob: 9e3b5ee216988ae1fb0f145300755e1f9e3c2b11 [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
17package com.android.telecomm;
18
Santos Cordon72890ce2014-07-21 01:32:04 -070019import android.telecomm.ParcelableConnection;
Sailesh Nepal7957f9c2014-08-09 17:13:19 -070020import android.telecomm.PhoneAccount;
Evan Charlton89176372014-07-19 18:23:09 -070021import android.telecomm.PhoneAccountHandle;
Sailesh Nepal664837f2014-07-14 16:31:51 -070022import android.telephony.DisconnectCause;
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;
Santos Cordonfd6ca442014-07-24 15:34:01 -070085 private int mLastErrorCode = DisconnectCause.OUTGOING_FAILURE;
Sailesh Nepal664837f2014-07-14 16:31:51 -070086 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 Awad293edf22014-07-24 17:52:29 -070097 mAttemptRecords = new ArrayList<>();
Ihab Awadb78b2762014-07-25 15:16:23 -070098 if (mCall.getTargetPhoneAccount() != null) {
Sailesh Nepal7957f9c2014-08-09 17:13:19 -070099 mAttemptRecords.add(new CallAttemptRecord(
100 mCall.getTargetPhoneAccount(), mCall.getTargetPhoneAccount()));
Sailesh Nepal664837f2014-07-14 16:31:51 -0700101 }
Sailesh Nepal7957f9c2014-08-09 17:13:19 -0700102 adjustAttemptsForConnectionManager();
Ihab Awad293edf22014-07-24 17:52:29 -0700103 adjustAttemptsForEmergency();
104 mAttemptRecordIterator = mAttemptRecords.iterator();
Ihab Awad69eb0f52014-07-18 11:20:37 -0700105 attemptNextPhoneAccount();
Sailesh Nepal664837f2014-07-14 16:31:51 -0700106 }
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 Awadfb5560d2014-08-18 09:32:51 -0700122 response.handleCreateConnectionFailure(DisconnectCause.OUTGOING_CANCELED, null);
Sailesh Nepal664837f2014-07-14 16:31:51 -0700123 }
124 }
125
Ihab Awad69eb0f52014-07-18 11:20:37 -0700126 private void attemptNextPhoneAccount() {
127 Log.v(this, "attemptNextPhoneAccount");
Tyler Gunncb59b672014-08-20 09:02:11 -0700128 PhoneAccountRegistrar registrar = TelecommApp.getInstance().getPhoneAccountRegistrar();
129 CallAttemptRecord attempt = null;
130 if (mAttemptRecordIterator.hasNext()) {
131 attempt = mAttemptRecordIterator.next();
Sailesh Nepal664837f2014-07-14 16:31:51 -0700132
Tyler Gunncb59b672014-08-20 09:02:11 -0700133 if (!registrar.phoneAccountHasPermission(attempt.connectionManagerPhoneAccount)) {
134 Log.w(this,
135 "Connection mgr does not have BIND_CONNECTION_SERVICE for attempt: %s",
136 attempt);
137 attemptNextPhoneAccount();
138 return;
139 }
140
141 // If the target PhoneAccount differs from the ConnectionManager phone acount, ensure it
142 // also has BIND_CONNECTION_SERVICE permission.
143 if (!attempt.connectionManagerPhoneAccount.equals(attempt.targetPhoneAccount) &&
144 !registrar.phoneAccountHasPermission(attempt.targetPhoneAccount)) {
145 Log.w(this,
146 "Target PhoneAccount does not have BIND_CONNECTION_SERVICE for attempt: %s",
147 attempt);
148 attemptNextPhoneAccount();
149 return;
150 }
151 }
152
153 if (mResponse != null && attempt != null) {
Ihab Awad293edf22014-07-24 17:52:29 -0700154 Log.i(this, "Trying attempt %s", attempt);
Evan Charlton94d01622014-07-20 12:32:05 -0700155 ConnectionServiceWrapper service =
Ihab Awadb78b2762014-07-25 15:16:23 -0700156 mRepository.getService(
157 attempt.connectionManagerPhoneAccount.getComponentName());
Sailesh Nepal664837f2014-07-14 16:31:51 -0700158 if (service == null) {
Ihab Awad293edf22014-07-24 17:52:29 -0700159 Log.i(this, "Found no connection service for attempt %s", attempt);
Ihab Awad69eb0f52014-07-18 11:20:37 -0700160 attemptNextPhoneAccount();
Sailesh Nepal664837f2014-07-14 16:31:51 -0700161 } else {
Ihab Awadb78b2762014-07-25 15:16:23 -0700162 mCall.setConnectionManagerPhoneAccount(attempt.connectionManagerPhoneAccount);
163 mCall.setTargetPhoneAccount(attempt.targetPhoneAccount);
Sailesh Nepal664837f2014-07-14 16:31:51 -0700164 mCall.setConnectionService(service);
165 Log.i(this, "Attempting to call from %s", service.getComponentName());
166 service.createConnection(mCall, new Response(service));
167 }
168 } else {
Ihab Awad69eb0f52014-07-18 11:20:37 -0700169 Log.v(this, "attemptNextPhoneAccount, no more accounts, failing");
Sailesh Nepal664837f2014-07-14 16:31:51 -0700170 if (mResponse != null) {
Ihab Awadfb5560d2014-08-18 09:32:51 -0700171 mResponse.handleCreateConnectionFailure(mLastErrorCode, mLastErrorMsg);
Sailesh Nepal664837f2014-07-14 16:31:51 -0700172 mResponse = null;
173 mCall.clearConnectionService();
174 }
175 }
176 }
177
Sailesh Nepal7957f9c2014-08-09 17:13:19 -0700178 private boolean shouldSetConnectionManager() {
179 if (mAttemptRecords.size() == 0) {
180 return false;
Ihab Awad293edf22014-07-24 17:52:29 -0700181 }
Ihab Awadc17294c2014-08-04 19:23:37 -0700182
Sailesh Nepal7957f9c2014-08-09 17:13:19 -0700183 if (mAttemptRecords.size() > 1) {
184 Log.d(this, "shouldSetConnectionManager, error, mAttemptRecords should not have more "
185 + "than 1 record");
186 return false;
187 }
188
189 PhoneAccountRegistrar registrar = TelecommApp.getInstance().getPhoneAccountRegistrar();
190 PhoneAccountHandle connectionManager = registrar.getSimCallManager();
191 if (connectionManager == null) {
192 return false;
193 }
194
195 PhoneAccountHandle targetPhoneAccountHandle = mAttemptRecords.get(0).targetPhoneAccount;
196 if (Objects.equals(connectionManager, targetPhoneAccountHandle)) {
197 return false;
198 }
199
200 // Connection managers are only allowed to manage SIM subscriptions.
201 PhoneAccount targetPhoneAccount = registrar.getPhoneAccount(targetPhoneAccountHandle);
202 boolean isSimSubscription = (targetPhoneAccount.getCapabilities() &
203 PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION) != 0;
204 if (!isSimSubscription) {
205 return false;
206 }
207
208 return true;
209 }
210
211 // If there exists a registered connection manager then use it.
212 private void adjustAttemptsForConnectionManager() {
213 if (shouldSetConnectionManager()) {
214 CallAttemptRecord record = new CallAttemptRecord(
215 TelecommApp.getInstance().getPhoneAccountRegistrar().getSimCallManager(),
216 mAttemptRecords.get(0).targetPhoneAccount);
217 Log.v(this, "setConnectionManager, changing %s -> %s",
218 mAttemptRecords.get(0).targetPhoneAccount, record);
219 mAttemptRecords.set(0, record);
220 } else {
221 Log.v(this, "setConnectionManager, not changing");
Ihab Awad293edf22014-07-24 17:52:29 -0700222 }
223 }
224
Sailesh Nepal664837f2014-07-14 16:31:51 -0700225 // If we are possibly attempting to call a local emergency number, ensure that the
Ihab Awad69eb0f52014-07-18 11:20:37 -0700226 // plain PSTN connection services are listed, and nothing else.
Ihab Awad293edf22014-07-24 17:52:29 -0700227 private void adjustAttemptsForEmergency() {
Ihab Awad69eb0f52014-07-18 11:20:37 -0700228 if (TelephonyUtil.shouldProcessAsEmergency(TelecommApp.getInstance(), mCall.getHandle())) {
229 Log.i(this, "Emergency number detected");
Ihab Awad293edf22014-07-24 17:52:29 -0700230 mAttemptRecords.clear();
Evan Charlton94d01622014-07-20 12:32:05 -0700231 List<PhoneAccountHandle> allAccountHandles = TelecommApp.getInstance()
Ihab Awad6fb37c82014-08-07 19:48:57 -0700232 .getPhoneAccountRegistrar().getOutgoingPhoneAccounts();
Tyler Gunn6e6f6d12014-08-20 15:22:18 -0700233 // First, add the PSTN phone account
Evan Charlton94d01622014-07-20 12:32:05 -0700234 for (int i = 0; i < allAccountHandles.size(); i++) {
235 if (TelephonyUtil.isPstnComponentName(
236 allAccountHandles.get(i).getComponentName())) {
237 Log.i(this, "Will try PSTN account %s for emergency", allAccountHandles.get(i));
Ihab Awad293edf22014-07-24 17:52:29 -0700238 mAttemptRecords.add(
239 new CallAttemptRecord(
240 allAccountHandles.get(i),
241 allAccountHandles.get(i)));
Sailesh Nepal664837f2014-07-14 16:31:51 -0700242 }
243 }
Tyler Gunn6e6f6d12014-08-20 15:22:18 -0700244
245 // Next, add the connection manager account as a backup.
246 PhoneAccountHandle callManager = TelecommApp.getInstance()
247 .getPhoneAccountRegistrar().getSimCallManager();
248 CallAttemptRecord callAttemptRecord = new CallAttemptRecord(callManager,
249 TelecommApp.getInstance().getPhoneAccountRegistrar().
250 getDefaultOutgoingPhoneAccount());
251
252 if (callManager != null && !mAttemptRecords.contains(callAttemptRecord)) {
253 Log.i(this, "Will try Connection Manager account %s for emergency",
254 callManager);
255 mAttemptRecords.add(callAttemptRecord);
256 }
Sailesh Nepal664837f2014-07-14 16:31:51 -0700257 }
258 }
259
Sailesh Nepal664837f2014-07-14 16:31:51 -0700260 private class Response implements CreateConnectionResponse {
261 private final ConnectionServiceWrapper mService;
262
263 Response(ConnectionServiceWrapper service) {
264 mService = service;
265 }
266
267 @Override
Ihab Awadfb5560d2014-08-18 09:32:51 -0700268 public void handleCreateConnectionSuccess(ParcelableConnection connection) {
Sailesh Nepal664837f2014-07-14 16:31:51 -0700269 if (mResponse == null) {
Ihab Awadfb5560d2014-08-18 09:32:51 -0700270 // Nobody is listening for this connection attempt any longer; ask the responsible
271 // ConnectionService to tear down any resources associated with the call
Sailesh Nepal664837f2014-07-14 16:31:51 -0700272 mService.abort(mCall);
273 } else {
Ihab Awadfb5560d2014-08-18 09:32:51 -0700274 // Success -- share the good news and remember that we are no longer interested
275 // in hearing about any more attempts
276 mResponse.handleCreateConnectionSuccess(connection);
Santos Cordon72890ce2014-07-21 01:32:04 -0700277 mResponse = null;
Sailesh Nepal664837f2014-07-14 16:31:51 -0700278 }
279 }
280
281 @Override
Ihab Awadfb5560d2014-08-18 09:32:51 -0700282 public void handleCreateConnectionFailure(int code, String msg) {
283 // Failure of some sort; record the reasons for failure and try again if possible
284 Log.d(CreateConnectionProcessor.this, "Connection failed: %d (%s)", code, msg);
Sailesh Nepal664837f2014-07-14 16:31:51 -0700285 mLastErrorCode = code;
286 mLastErrorMsg = msg;
Ihab Awad69eb0f52014-07-18 11:20:37 -0700287 attemptNextPhoneAccount();
Sailesh Nepal664837f2014-07-14 16:31:51 -0700288 }
Sailesh Nepal664837f2014-07-14 16:31:51 -0700289 }
290}