blob: ac8974c4da4effad7262258e5acb5a21b6f6211c [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");
Sailesh Nepal664837f2014-07-14 16:31:51 -0700128
Ihab Awad293edf22014-07-24 17:52:29 -0700129 if (mResponse != null && mAttemptRecordIterator.hasNext()) {
130 CallAttemptRecord attempt = mAttemptRecordIterator.next();
131 Log.i(this, "Trying attempt %s", attempt);
Evan Charlton94d01622014-07-20 12:32:05 -0700132 ConnectionServiceWrapper service =
Ihab Awadb78b2762014-07-25 15:16:23 -0700133 mRepository.getService(
134 attempt.connectionManagerPhoneAccount.getComponentName());
Sailesh Nepal664837f2014-07-14 16:31:51 -0700135 if (service == null) {
Ihab Awad293edf22014-07-24 17:52:29 -0700136 Log.i(this, "Found no connection service for attempt %s", attempt);
Ihab Awad69eb0f52014-07-18 11:20:37 -0700137 attemptNextPhoneAccount();
Sailesh Nepal664837f2014-07-14 16:31:51 -0700138 } else {
Ihab Awadb78b2762014-07-25 15:16:23 -0700139 mCall.setConnectionManagerPhoneAccount(attempt.connectionManagerPhoneAccount);
140 mCall.setTargetPhoneAccount(attempt.targetPhoneAccount);
Sailesh Nepal664837f2014-07-14 16:31:51 -0700141 mCall.setConnectionService(service);
142 Log.i(this, "Attempting to call from %s", service.getComponentName());
143 service.createConnection(mCall, new Response(service));
144 }
145 } else {
Ihab Awad69eb0f52014-07-18 11:20:37 -0700146 Log.v(this, "attemptNextPhoneAccount, no more accounts, failing");
Sailesh Nepal664837f2014-07-14 16:31:51 -0700147 if (mResponse != null) {
Ihab Awadfb5560d2014-08-18 09:32:51 -0700148 mResponse.handleCreateConnectionFailure(mLastErrorCode, mLastErrorMsg);
Sailesh Nepal664837f2014-07-14 16:31:51 -0700149 mResponse = null;
150 mCall.clearConnectionService();
151 }
152 }
153 }
154
Sailesh Nepal7957f9c2014-08-09 17:13:19 -0700155 private boolean shouldSetConnectionManager() {
156 if (mAttemptRecords.size() == 0) {
157 return false;
Ihab Awad293edf22014-07-24 17:52:29 -0700158 }
Ihab Awadc17294c2014-08-04 19:23:37 -0700159
Sailesh Nepal7957f9c2014-08-09 17:13:19 -0700160 if (mAttemptRecords.size() > 1) {
161 Log.d(this, "shouldSetConnectionManager, error, mAttemptRecords should not have more "
162 + "than 1 record");
163 return false;
164 }
165
166 PhoneAccountRegistrar registrar = TelecommApp.getInstance().getPhoneAccountRegistrar();
167 PhoneAccountHandle connectionManager = registrar.getSimCallManager();
168 if (connectionManager == null) {
169 return false;
170 }
171
172 PhoneAccountHandle targetPhoneAccountHandle = mAttemptRecords.get(0).targetPhoneAccount;
173 if (Objects.equals(connectionManager, targetPhoneAccountHandle)) {
174 return false;
175 }
176
177 // Connection managers are only allowed to manage SIM subscriptions.
178 PhoneAccount targetPhoneAccount = registrar.getPhoneAccount(targetPhoneAccountHandle);
179 boolean isSimSubscription = (targetPhoneAccount.getCapabilities() &
180 PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION) != 0;
181 if (!isSimSubscription) {
182 return false;
183 }
184
185 return true;
186 }
187
188 // If there exists a registered connection manager then use it.
189 private void adjustAttemptsForConnectionManager() {
190 if (shouldSetConnectionManager()) {
191 CallAttemptRecord record = new CallAttemptRecord(
192 TelecommApp.getInstance().getPhoneAccountRegistrar().getSimCallManager(),
193 mAttemptRecords.get(0).targetPhoneAccount);
194 Log.v(this, "setConnectionManager, changing %s -> %s",
195 mAttemptRecords.get(0).targetPhoneAccount, record);
196 mAttemptRecords.set(0, record);
197 } else {
198 Log.v(this, "setConnectionManager, not changing");
Ihab Awad293edf22014-07-24 17:52:29 -0700199 }
200 }
201
Sailesh Nepal664837f2014-07-14 16:31:51 -0700202 // If we are possibly attempting to call a local emergency number, ensure that the
Ihab Awad69eb0f52014-07-18 11:20:37 -0700203 // plain PSTN connection services are listed, and nothing else.
Ihab Awad293edf22014-07-24 17:52:29 -0700204 private void adjustAttemptsForEmergency() {
Ihab Awad69eb0f52014-07-18 11:20:37 -0700205 if (TelephonyUtil.shouldProcessAsEmergency(TelecommApp.getInstance(), mCall.getHandle())) {
206 Log.i(this, "Emergency number detected");
Ihab Awad293edf22014-07-24 17:52:29 -0700207 mAttemptRecords.clear();
Evan Charlton94d01622014-07-20 12:32:05 -0700208 List<PhoneAccountHandle> allAccountHandles = TelecommApp.getInstance()
Ihab Awad6fb37c82014-08-07 19:48:57 -0700209 .getPhoneAccountRegistrar().getOutgoingPhoneAccounts();
Tyler Gunn6e6f6d12014-08-20 15:22:18 -0700210 // First, add the PSTN phone account
Evan Charlton94d01622014-07-20 12:32:05 -0700211 for (int i = 0; i < allAccountHandles.size(); i++) {
212 if (TelephonyUtil.isPstnComponentName(
213 allAccountHandles.get(i).getComponentName())) {
214 Log.i(this, "Will try PSTN account %s for emergency", allAccountHandles.get(i));
Ihab Awad293edf22014-07-24 17:52:29 -0700215 mAttemptRecords.add(
216 new CallAttemptRecord(
217 allAccountHandles.get(i),
218 allAccountHandles.get(i)));
Sailesh Nepal664837f2014-07-14 16:31:51 -0700219 }
220 }
Tyler Gunn6e6f6d12014-08-20 15:22:18 -0700221
222 // Next, add the connection manager account as a backup.
223 PhoneAccountHandle callManager = TelecommApp.getInstance()
224 .getPhoneAccountRegistrar().getSimCallManager();
225 CallAttemptRecord callAttemptRecord = new CallAttemptRecord(callManager,
226 TelecommApp.getInstance().getPhoneAccountRegistrar().
227 getDefaultOutgoingPhoneAccount());
228
229 if (callManager != null && !mAttemptRecords.contains(callAttemptRecord)) {
230 Log.i(this, "Will try Connection Manager account %s for emergency",
231 callManager);
232 mAttemptRecords.add(callAttemptRecord);
233 }
Sailesh Nepal664837f2014-07-14 16:31:51 -0700234 }
235 }
236
Sailesh Nepal664837f2014-07-14 16:31:51 -0700237 private class Response implements CreateConnectionResponse {
238 private final ConnectionServiceWrapper mService;
239
240 Response(ConnectionServiceWrapper service) {
241 mService = service;
242 }
243
244 @Override
Ihab Awadfb5560d2014-08-18 09:32:51 -0700245 public void handleCreateConnectionSuccess(ParcelableConnection connection) {
Sailesh Nepal664837f2014-07-14 16:31:51 -0700246 if (mResponse == null) {
Ihab Awadfb5560d2014-08-18 09:32:51 -0700247 // Nobody is listening for this connection attempt any longer; ask the responsible
248 // ConnectionService to tear down any resources associated with the call
Sailesh Nepal664837f2014-07-14 16:31:51 -0700249 mService.abort(mCall);
250 } else {
Ihab Awadfb5560d2014-08-18 09:32:51 -0700251 // Success -- share the good news and remember that we are no longer interested
252 // in hearing about any more attempts
253 mResponse.handleCreateConnectionSuccess(connection);
Santos Cordon72890ce2014-07-21 01:32:04 -0700254 mResponse = null;
Sailesh Nepal664837f2014-07-14 16:31:51 -0700255 }
256 }
257
258 @Override
Ihab Awadfb5560d2014-08-18 09:32:51 -0700259 public void handleCreateConnectionFailure(int code, String msg) {
260 // Failure of some sort; record the reasons for failure and try again if possible
261 Log.d(CreateConnectionProcessor.this, "Connection failed: %d (%s)", code, msg);
Sailesh Nepal664837f2014-07-14 16:31:51 -0700262 mLastErrorCode = code;
263 mLastErrorMsg = msg;
Ihab Awad69eb0f52014-07-18 11:20:37 -0700264 attemptNextPhoneAccount();
Sailesh Nepal664837f2014-07-14 16:31:51 -0700265 }
Sailesh Nepal664837f2014-07-14 16:31:51 -0700266 }
267}