blob: 8ba73bf38b9265ae52b90ad6bf43bfbda25e6d62 [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.ConnectionRequest;
20import 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 }
61 }
62
Sailesh Nepal664837f2014-07-14 16:31:51 -070063 private final Call mCall;
64 private final ConnectionServiceRepository mRepository;
Ihab Awad293edf22014-07-24 17:52:29 -070065 private List<CallAttemptRecord> mAttemptRecords;
66 private Iterator<CallAttemptRecord> mAttemptRecordIterator;
Sailesh Nepal664837f2014-07-14 16:31:51 -070067 private CreateConnectionResponse mResponse;
Santos Cordonfd6ca442014-07-24 15:34:01 -070068 private int mLastErrorCode = DisconnectCause.OUTGOING_FAILURE;
Sailesh Nepal664837f2014-07-14 16:31:51 -070069 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 Awad293edf22014-07-24 17:52:29 -070080 mAttemptRecords = new ArrayList<>();
Ihab Awadb78b2762014-07-25 15:16:23 -070081 if (mCall.getTargetPhoneAccount() != null) {
Sailesh Nepal7957f9c2014-08-09 17:13:19 -070082 mAttemptRecords.add(new CallAttemptRecord(
83 mCall.getTargetPhoneAccount(), mCall.getTargetPhoneAccount()));
Sailesh Nepal664837f2014-07-14 16:31:51 -070084 }
Sailesh Nepal7957f9c2014-08-09 17:13:19 -070085 adjustAttemptsForConnectionManager();
Ihab Awad293edf22014-07-24 17:52:29 -070086 adjustAttemptsForEmergency();
87 mAttemptRecordIterator = mAttemptRecords.iterator();
Ihab Awad69eb0f52014-07-18 11:20:37 -070088 attemptNextPhoneAccount();
Sailesh Nepal664837f2014-07-14 16:31:51 -070089 }
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 Awad69eb0f52014-07-18 11:20:37 -0700109 private void attemptNextPhoneAccount() {
110 Log.v(this, "attemptNextPhoneAccount");
Sailesh Nepal664837f2014-07-14 16:31:51 -0700111
Ihab Awad293edf22014-07-24 17:52:29 -0700112 if (mResponse != null && mAttemptRecordIterator.hasNext()) {
113 CallAttemptRecord attempt = mAttemptRecordIterator.next();
114 Log.i(this, "Trying attempt %s", attempt);
Evan Charlton94d01622014-07-20 12:32:05 -0700115 ConnectionServiceWrapper service =
Ihab Awadb78b2762014-07-25 15:16:23 -0700116 mRepository.getService(
117 attempt.connectionManagerPhoneAccount.getComponentName());
Sailesh Nepal664837f2014-07-14 16:31:51 -0700118 if (service == null) {
Ihab Awad293edf22014-07-24 17:52:29 -0700119 Log.i(this, "Found no connection service for attempt %s", attempt);
Ihab Awad69eb0f52014-07-18 11:20:37 -0700120 attemptNextPhoneAccount();
Sailesh Nepal664837f2014-07-14 16:31:51 -0700121 } else {
Ihab Awadb78b2762014-07-25 15:16:23 -0700122 mCall.setConnectionManagerPhoneAccount(attempt.connectionManagerPhoneAccount);
123 mCall.setTargetPhoneAccount(attempt.targetPhoneAccount);
Sailesh Nepal664837f2014-07-14 16:31:51 -0700124 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 Awad69eb0f52014-07-18 11:20:37 -0700129 Log.v(this, "attemptNextPhoneAccount, no more accounts, failing");
Sailesh Nepal664837f2014-07-14 16:31:51 -0700130 if (mResponse != null) {
131 mResponse.handleCreateConnectionFailed(mLastErrorCode, mLastErrorMsg);
132 mResponse = null;
133 mCall.clearConnectionService();
134 }
135 }
136 }
137
Sailesh Nepal7957f9c2014-08-09 17:13:19 -0700138 private boolean shouldSetConnectionManager() {
139 if (mAttemptRecords.size() == 0) {
140 return false;
Ihab Awad293edf22014-07-24 17:52:29 -0700141 }
Ihab Awadc17294c2014-08-04 19:23:37 -0700142
Sailesh Nepal7957f9c2014-08-09 17:13:19 -0700143 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 Awad293edf22014-07-24 17:52:29 -0700182 }
183 }
184
Sailesh Nepal664837f2014-07-14 16:31:51 -0700185 // If we are possibly attempting to call a local emergency number, ensure that the
Ihab Awad69eb0f52014-07-18 11:20:37 -0700186 // plain PSTN connection services are listed, and nothing else.
Ihab Awad293edf22014-07-24 17:52:29 -0700187 private void adjustAttemptsForEmergency() {
Ihab Awad69eb0f52014-07-18 11:20:37 -0700188 if (TelephonyUtil.shouldProcessAsEmergency(TelecommApp.getInstance(), mCall.getHandle())) {
189 Log.i(this, "Emergency number detected");
Ihab Awad293edf22014-07-24 17:52:29 -0700190 mAttemptRecords.clear();
Evan Charlton94d01622014-07-20 12:32:05 -0700191 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 Awad293edf22014-07-24 17:52:29 -0700197 mAttemptRecords.add(
198 new CallAttemptRecord(
199 allAccountHandles.get(i),
200 allAccountHandles.get(i)));
Sailesh Nepal664837f2014-07-14 16:31:51 -0700201 }
202 }
203 }
204 }
205
Sailesh Nepal664837f2014-07-14 16:31:51 -0700206 private class Response implements CreateConnectionResponse {
207 private final ConnectionServiceWrapper mService;
208
209 Response(ConnectionServiceWrapper service) {
210 mService = service;
211 }
212
213 @Override
Santos Cordon72890ce2014-07-21 01:32:04 -0700214 public void handleCreateConnectionSuccessful(
215 ConnectionRequest request, ParcelableConnection connection) {
Sailesh Nepal664837f2014-07-14 16:31:51 -0700216 if (mResponse == null) {
217 mService.abort(mCall);
218 } else {
Santos Cordon72890ce2014-07-21 01:32:04 -0700219 mResponse.handleCreateConnectionSuccessful(request, connection);
220 mResponse = null;
Sailesh Nepal664837f2014-07-14 16:31:51 -0700221 }
222 }
223
224 @Override
225 public void handleCreateConnectionFailed(int code, String msg) {
226 mLastErrorCode = code;
227 mLastErrorMsg = msg;
Ihab Awad69eb0f52014-07-18 11:20:37 -0700228 Log.d(CreateConnectionProcessor.this, "Connection failed: %d (%s)", code, msg);
229 attemptNextPhoneAccount();
Sailesh Nepal664837f2014-07-14 16:31:51 -0700230 }
231
232 @Override
233 public void handleCreateConnectionCancelled() {
234 if (mResponse != null) {
235 mResponse.handleCreateConnectionCancelled();
236 mResponse = null;
237 }
238 }
239 }
240}