blob: cc2e8c066630de3bb89fbdb63a6814296426a61b [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 Cordonafe59e52014-08-22 16:48:43 -070019import android.content.Context;
Santos Cordon72890ce2014-07-21 01:32:04 -070020import android.telecomm.ParcelableConnection;
Sailesh Nepal7957f9c2014-08-09 17:13:19 -070021import android.telecomm.PhoneAccount;
Evan Charlton89176372014-07-19 18:23:09 -070022import android.telecomm.PhoneAccountHandle;
Sailesh Nepal664837f2014-07-14 16:31:51 -070023import android.telephony.DisconnectCause;
Sailesh Nepal664837f2014-07-14 16:31:51 -070024
25import java.util.ArrayList;
26import java.util.Iterator;
27import java.util.List;
Ihab Awad293edf22014-07-24 17:52:29 -070028import java.util.Objects;
Sailesh Nepal664837f2014-07-14 16:31:51 -070029
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 */
37final class CreateConnectionProcessor {
Ihab Awad293edf22014-07-24 17:52:29 -070038
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 Awadb78b2762014-07-25 15:16:23 -070043 public final PhoneAccountHandle connectionManagerPhoneAccount;
Ihab Awad293edf22014-07-24 17:52:29 -070044 // The PhoneAccount which we will tell the target connection service to use
45 // for attempting to make the actual phone call
Ihab Awadb78b2762014-07-25 15:16:23 -070046 public final PhoneAccountHandle targetPhoneAccount;
Ihab Awad293edf22014-07-24 17:52:29 -070047
48 public CallAttemptRecord(
Ihab Awadb78b2762014-07-25 15:16:23 -070049 PhoneAccountHandle connectionManagerPhoneAccount,
50 PhoneAccountHandle targetPhoneAccount) {
51 this.connectionManagerPhoneAccount = connectionManagerPhoneAccount;
52 this.targetPhoneAccount = targetPhoneAccount;
Ihab Awad293edf22014-07-24 17:52:29 -070053 }
54
55 @Override
56 public String toString() {
57 return "CallAttemptRecord("
Ihab Awadb78b2762014-07-25 15:16:23 -070058 + Objects.toString(connectionManagerPhoneAccount) + ","
59 + Objects.toString(targetPhoneAccount) + ")";
Ihab Awad293edf22014-07-24 17:52:29 -070060 }
Tyler Gunn6e6f6d12014-08-20 15:22:18 -070061
62 /**
63 * Determines if this instance of {@code CallAttemptRecord} has the same underlying
64 * {@code PhoneAccountHandle}s as another instance.
65 *
66 * @param obj The other instance to compare against.
67 * @return {@code True} if the {@code CallAttemptRecord}s are equal.
68 */
69 @Override
70 public boolean equals(Object obj) {
71 if (obj instanceof CallAttemptRecord) {
72 CallAttemptRecord other = (CallAttemptRecord) obj;
73 return Objects.equals(connectionManagerPhoneAccount,
74 other.connectionManagerPhoneAccount) &&
75 Objects.equals(targetPhoneAccount, other.targetPhoneAccount);
76 }
77 return false;
78 }
Ihab Awad293edf22014-07-24 17:52:29 -070079 }
80
Sailesh Nepal664837f2014-07-14 16:31:51 -070081 private final Call mCall;
82 private final ConnectionServiceRepository mRepository;
Ihab Awad293edf22014-07-24 17:52:29 -070083 private List<CallAttemptRecord> mAttemptRecords;
84 private Iterator<CallAttemptRecord> mAttemptRecordIterator;
Sailesh Nepal664837f2014-07-14 16:31:51 -070085 private CreateConnectionResponse mResponse;
Santos Cordonfd6ca442014-07-24 15:34:01 -070086 private int mLastErrorCode = DisconnectCause.OUTGOING_FAILURE;
Sailesh Nepal664837f2014-07-14 16:31:51 -070087 private String mLastErrorMsg;
88
89 CreateConnectionProcessor(
90 Call call, ConnectionServiceRepository repository, CreateConnectionResponse response) {
91 mCall = call;
92 mRepository = repository;
93 mResponse = response;
94 }
95
96 void process() {
97 Log.v(this, "process");
Ihab Awad293edf22014-07-24 17:52:29 -070098 mAttemptRecords = new ArrayList<>();
Ihab Awadb78b2762014-07-25 15:16:23 -070099 if (mCall.getTargetPhoneAccount() != null) {
Sailesh Nepal7957f9c2014-08-09 17:13:19 -0700100 mAttemptRecords.add(new CallAttemptRecord(
101 mCall.getTargetPhoneAccount(), mCall.getTargetPhoneAccount()));
Sailesh Nepal664837f2014-07-14 16:31:51 -0700102 }
Sailesh Nepal7957f9c2014-08-09 17:13:19 -0700103 adjustAttemptsForConnectionManager();
Ihab Awad293edf22014-07-24 17:52:29 -0700104 adjustAttemptsForEmergency();
105 mAttemptRecordIterator = mAttemptRecords.iterator();
Ihab Awad69eb0f52014-07-18 11:20:37 -0700106 attemptNextPhoneAccount();
Sailesh Nepal664837f2014-07-14 16:31:51 -0700107 }
108
109 void abort() {
110 Log.v(this, "abort");
111
112 // Clear the response first to prevent attemptNextConnectionService from attempting any
113 // more services.
114 CreateConnectionResponse response = mResponse;
115 mResponse = null;
116
117 ConnectionServiceWrapper service = mCall.getConnectionService();
118 if (service != null) {
119 service.abort(mCall);
120 mCall.clearConnectionService();
121 }
122 if (response != null) {
Ihab Awadfb5560d2014-08-18 09:32:51 -0700123 response.handleCreateConnectionFailure(DisconnectCause.OUTGOING_CANCELED, null);
Sailesh Nepal664837f2014-07-14 16:31:51 -0700124 }
125 }
126
Ihab Awad69eb0f52014-07-18 11:20:37 -0700127 private void attemptNextPhoneAccount() {
128 Log.v(this, "attemptNextPhoneAccount");
Tyler Gunncb59b672014-08-20 09:02:11 -0700129 PhoneAccountRegistrar registrar = TelecommApp.getInstance().getPhoneAccountRegistrar();
130 CallAttemptRecord attempt = null;
131 if (mAttemptRecordIterator.hasNext()) {
132 attempt = mAttemptRecordIterator.next();
Sailesh Nepal664837f2014-07-14 16:31:51 -0700133
Tyler Gunncb59b672014-08-20 09:02:11 -0700134 if (!registrar.phoneAccountHasPermission(attempt.connectionManagerPhoneAccount)) {
135 Log.w(this,
136 "Connection mgr does not have BIND_CONNECTION_SERVICE for attempt: %s",
137 attempt);
138 attemptNextPhoneAccount();
139 return;
140 }
141
142 // If the target PhoneAccount differs from the ConnectionManager phone acount, ensure it
143 // also has BIND_CONNECTION_SERVICE permission.
144 if (!attempt.connectionManagerPhoneAccount.equals(attempt.targetPhoneAccount) &&
145 !registrar.phoneAccountHasPermission(attempt.targetPhoneAccount)) {
146 Log.w(this,
147 "Target PhoneAccount does not have BIND_CONNECTION_SERVICE for attempt: %s",
148 attempt);
149 attemptNextPhoneAccount();
150 return;
151 }
152 }
153
154 if (mResponse != null && attempt != null) {
Ihab Awad293edf22014-07-24 17:52:29 -0700155 Log.i(this, "Trying attempt %s", attempt);
Evan Charlton94d01622014-07-20 12:32:05 -0700156 ConnectionServiceWrapper service =
Ihab Awadb78b2762014-07-25 15:16:23 -0700157 mRepository.getService(
158 attempt.connectionManagerPhoneAccount.getComponentName());
Sailesh Nepal664837f2014-07-14 16:31:51 -0700159 if (service == null) {
Ihab Awad293edf22014-07-24 17:52:29 -0700160 Log.i(this, "Found no connection service for attempt %s", attempt);
Ihab Awad69eb0f52014-07-18 11:20:37 -0700161 attemptNextPhoneAccount();
Sailesh Nepal664837f2014-07-14 16:31:51 -0700162 } else {
Ihab Awadb78b2762014-07-25 15:16:23 -0700163 mCall.setConnectionManagerPhoneAccount(attempt.connectionManagerPhoneAccount);
164 mCall.setTargetPhoneAccount(attempt.targetPhoneAccount);
Sailesh Nepal664837f2014-07-14 16:31:51 -0700165 mCall.setConnectionService(service);
166 Log.i(this, "Attempting to call from %s", service.getComponentName());
167 service.createConnection(mCall, new Response(service));
168 }
169 } else {
Ihab Awad69eb0f52014-07-18 11:20:37 -0700170 Log.v(this, "attemptNextPhoneAccount, no more accounts, failing");
Sailesh Nepal664837f2014-07-14 16:31:51 -0700171 if (mResponse != null) {
Ihab Awadfb5560d2014-08-18 09:32:51 -0700172 mResponse.handleCreateConnectionFailure(mLastErrorCode, mLastErrorMsg);
Sailesh Nepal664837f2014-07-14 16:31:51 -0700173 mResponse = null;
174 mCall.clearConnectionService();
175 }
176 }
177 }
178
Sailesh Nepal7957f9c2014-08-09 17:13:19 -0700179 private boolean shouldSetConnectionManager() {
Santos Cordonafe59e52014-08-22 16:48:43 -0700180 Context context = TelecommApp.getInstance();
181 if (!context.getResources().getBoolean(R.bool.connection_manager_enabled)) {
182 // Connection Manager support has been turned off, disregard.
183 return false;
184 }
185
Sailesh Nepal7957f9c2014-08-09 17:13:19 -0700186 if (mAttemptRecords.size() == 0) {
187 return false;
Ihab Awad293edf22014-07-24 17:52:29 -0700188 }
Ihab Awadc17294c2014-08-04 19:23:37 -0700189
Sailesh Nepal7957f9c2014-08-09 17:13:19 -0700190 if (mAttemptRecords.size() > 1) {
191 Log.d(this, "shouldSetConnectionManager, error, mAttemptRecords should not have more "
192 + "than 1 record");
193 return false;
194 }
195
196 PhoneAccountRegistrar registrar = TelecommApp.getInstance().getPhoneAccountRegistrar();
197 PhoneAccountHandle connectionManager = registrar.getSimCallManager();
198 if (connectionManager == null) {
199 return false;
200 }
201
202 PhoneAccountHandle targetPhoneAccountHandle = mAttemptRecords.get(0).targetPhoneAccount;
203 if (Objects.equals(connectionManager, targetPhoneAccountHandle)) {
204 return false;
205 }
206
207 // Connection managers are only allowed to manage SIM subscriptions.
208 PhoneAccount targetPhoneAccount = registrar.getPhoneAccount(targetPhoneAccountHandle);
209 boolean isSimSubscription = (targetPhoneAccount.getCapabilities() &
210 PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION) != 0;
211 if (!isSimSubscription) {
212 return false;
213 }
214
215 return true;
216 }
217
218 // If there exists a registered connection manager then use it.
219 private void adjustAttemptsForConnectionManager() {
220 if (shouldSetConnectionManager()) {
221 CallAttemptRecord record = new CallAttemptRecord(
222 TelecommApp.getInstance().getPhoneAccountRegistrar().getSimCallManager(),
223 mAttemptRecords.get(0).targetPhoneAccount);
224 Log.v(this, "setConnectionManager, changing %s -> %s",
225 mAttemptRecords.get(0).targetPhoneAccount, record);
226 mAttemptRecords.set(0, record);
227 } else {
228 Log.v(this, "setConnectionManager, not changing");
Ihab Awad293edf22014-07-24 17:52:29 -0700229 }
230 }
231
Sailesh Nepal664837f2014-07-14 16:31:51 -0700232 // If we are possibly attempting to call a local emergency number, ensure that the
Ihab Awad69eb0f52014-07-18 11:20:37 -0700233 // plain PSTN connection services are listed, and nothing else.
Ihab Awad293edf22014-07-24 17:52:29 -0700234 private void adjustAttemptsForEmergency() {
Ihab Awad69eb0f52014-07-18 11:20:37 -0700235 if (TelephonyUtil.shouldProcessAsEmergency(TelecommApp.getInstance(), mCall.getHandle())) {
236 Log.i(this, "Emergency number detected");
Ihab Awad293edf22014-07-24 17:52:29 -0700237 mAttemptRecords.clear();
Evan Charlton94d01622014-07-20 12:32:05 -0700238 List<PhoneAccountHandle> allAccountHandles = TelecommApp.getInstance()
Ihab Awad6fb37c82014-08-07 19:48:57 -0700239 .getPhoneAccountRegistrar().getOutgoingPhoneAccounts();
Tyler Gunn6e6f6d12014-08-20 15:22:18 -0700240 // First, add the PSTN phone account
Evan Charlton94d01622014-07-20 12:32:05 -0700241 for (int i = 0; i < allAccountHandles.size(); i++) {
242 if (TelephonyUtil.isPstnComponentName(
243 allAccountHandles.get(i).getComponentName())) {
244 Log.i(this, "Will try PSTN account %s for emergency", allAccountHandles.get(i));
Ihab Awad293edf22014-07-24 17:52:29 -0700245 mAttemptRecords.add(
246 new CallAttemptRecord(
247 allAccountHandles.get(i),
248 allAccountHandles.get(i)));
Sailesh Nepal664837f2014-07-14 16:31:51 -0700249 }
250 }
Tyler Gunn6e6f6d12014-08-20 15:22:18 -0700251
252 // Next, add the connection manager account as a backup.
253 PhoneAccountHandle callManager = TelecommApp.getInstance()
254 .getPhoneAccountRegistrar().getSimCallManager();
255 CallAttemptRecord callAttemptRecord = new CallAttemptRecord(callManager,
256 TelecommApp.getInstance().getPhoneAccountRegistrar().
257 getDefaultOutgoingPhoneAccount());
258
259 if (callManager != null && !mAttemptRecords.contains(callAttemptRecord)) {
260 Log.i(this, "Will try Connection Manager account %s for emergency",
261 callManager);
262 mAttemptRecords.add(callAttemptRecord);
263 }
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
Ihab Awadfb5560d2014-08-18 09:32:51 -0700291 public void handleCreateConnectionFailure(int code, String msg) {
292 // Failure of some sort; record the reasons for failure and try again if possible
293 Log.d(CreateConnectionProcessor.this, "Connection failed: %d (%s)", code, msg);
Sailesh Nepal664837f2014-07-14 16:31:51 -0700294 mLastErrorCode = code;
295 mLastErrorMsg = msg;
Ihab Awad69eb0f52014-07-18 11:20:37 -0700296 attemptNextPhoneAccount();
Sailesh Nepal664837f2014-07-14 16:31:51 -0700297 }
Sailesh Nepal664837f2014-07-14 16:31:51 -0700298 }
299}