blob: 3e6e5440769d4cfa218350e628c847d2bbe293e7 [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 }
60 }
61
Sailesh Nepal664837f2014-07-14 16:31:51 -070062 private final Call mCall;
63 private final ConnectionServiceRepository mRepository;
Ihab Awad293edf22014-07-24 17:52:29 -070064 private List<CallAttemptRecord> mAttemptRecords;
65 private Iterator<CallAttemptRecord> mAttemptRecordIterator;
Sailesh Nepal664837f2014-07-14 16:31:51 -070066 private CreateConnectionResponse mResponse;
Santos Cordonfd6ca442014-07-24 15:34:01 -070067 private int mLastErrorCode = DisconnectCause.OUTGOING_FAILURE;
Sailesh Nepal664837f2014-07-14 16:31:51 -070068 private String mLastErrorMsg;
69
70 CreateConnectionProcessor(
71 Call call, ConnectionServiceRepository repository, CreateConnectionResponse response) {
72 mCall = call;
73 mRepository = repository;
74 mResponse = response;
75 }
76
77 void process() {
78 Log.v(this, "process");
Ihab Awad293edf22014-07-24 17:52:29 -070079 mAttemptRecords = new ArrayList<>();
Ihab Awadb78b2762014-07-25 15:16:23 -070080 if (mCall.getTargetPhoneAccount() != null) {
Sailesh Nepal7957f9c2014-08-09 17:13:19 -070081 mAttemptRecords.add(new CallAttemptRecord(
82 mCall.getTargetPhoneAccount(), mCall.getTargetPhoneAccount()));
Sailesh Nepal664837f2014-07-14 16:31:51 -070083 }
Sailesh Nepal7957f9c2014-08-09 17:13:19 -070084 adjustAttemptsForConnectionManager();
Ihab Awad293edf22014-07-24 17:52:29 -070085 adjustAttemptsForEmergency();
86 mAttemptRecordIterator = mAttemptRecords.iterator();
Ihab Awad69eb0f52014-07-18 11:20:37 -070087 attemptNextPhoneAccount();
Sailesh Nepal664837f2014-07-14 16:31:51 -070088 }
89
90 void abort() {
91 Log.v(this, "abort");
92
93 // Clear the response first to prevent attemptNextConnectionService from attempting any
94 // more services.
95 CreateConnectionResponse response = mResponse;
96 mResponse = null;
97
98 ConnectionServiceWrapper service = mCall.getConnectionService();
99 if (service != null) {
100 service.abort(mCall);
101 mCall.clearConnectionService();
102 }
103 if (response != null) {
Ihab Awadfb5560d2014-08-18 09:32:51 -0700104 response.handleCreateConnectionFailure(DisconnectCause.OUTGOING_CANCELED, null);
Sailesh Nepal664837f2014-07-14 16:31:51 -0700105 }
106 }
107
Ihab Awad69eb0f52014-07-18 11:20:37 -0700108 private void attemptNextPhoneAccount() {
109 Log.v(this, "attemptNextPhoneAccount");
Sailesh Nepal664837f2014-07-14 16:31:51 -0700110
Ihab Awad293edf22014-07-24 17:52:29 -0700111 if (mResponse != null && mAttemptRecordIterator.hasNext()) {
112 CallAttemptRecord attempt = mAttemptRecordIterator.next();
113 Log.i(this, "Trying attempt %s", attempt);
Evan Charlton94d01622014-07-20 12:32:05 -0700114 ConnectionServiceWrapper service =
Ihab Awadb78b2762014-07-25 15:16:23 -0700115 mRepository.getService(
116 attempt.connectionManagerPhoneAccount.getComponentName());
Sailesh Nepal664837f2014-07-14 16:31:51 -0700117 if (service == null) {
Ihab Awad293edf22014-07-24 17:52:29 -0700118 Log.i(this, "Found no connection service for attempt %s", attempt);
Ihab Awad69eb0f52014-07-18 11:20:37 -0700119 attemptNextPhoneAccount();
Sailesh Nepal664837f2014-07-14 16:31:51 -0700120 } else {
Ihab Awadb78b2762014-07-25 15:16:23 -0700121 mCall.setConnectionManagerPhoneAccount(attempt.connectionManagerPhoneAccount);
122 mCall.setTargetPhoneAccount(attempt.targetPhoneAccount);
Sailesh Nepal664837f2014-07-14 16:31:51 -0700123 mCall.setConnectionService(service);
124 Log.i(this, "Attempting to call from %s", service.getComponentName());
125 service.createConnection(mCall, new Response(service));
126 }
127 } else {
Ihab Awad69eb0f52014-07-18 11:20:37 -0700128 Log.v(this, "attemptNextPhoneAccount, no more accounts, failing");
Sailesh Nepal664837f2014-07-14 16:31:51 -0700129 if (mResponse != null) {
Ihab Awadfb5560d2014-08-18 09:32:51 -0700130 mResponse.handleCreateConnectionFailure(mLastErrorCode, mLastErrorMsg);
Sailesh Nepal664837f2014-07-14 16:31:51 -0700131 mResponse = null;
132 mCall.clearConnectionService();
133 }
134 }
135 }
136
Sailesh Nepal7957f9c2014-08-09 17:13:19 -0700137 private boolean shouldSetConnectionManager() {
138 if (mAttemptRecords.size() == 0) {
139 return false;
Ihab Awad293edf22014-07-24 17:52:29 -0700140 }
Ihab Awadc17294c2014-08-04 19:23:37 -0700141
Sailesh Nepal7957f9c2014-08-09 17:13:19 -0700142 if (mAttemptRecords.size() > 1) {
143 Log.d(this, "shouldSetConnectionManager, error, mAttemptRecords should not have more "
144 + "than 1 record");
145 return false;
146 }
147
148 PhoneAccountRegistrar registrar = TelecommApp.getInstance().getPhoneAccountRegistrar();
149 PhoneAccountHandle connectionManager = registrar.getSimCallManager();
150 if (connectionManager == null) {
151 return false;
152 }
153
154 PhoneAccountHandle targetPhoneAccountHandle = mAttemptRecords.get(0).targetPhoneAccount;
155 if (Objects.equals(connectionManager, targetPhoneAccountHandle)) {
156 return false;
157 }
158
159 // Connection managers are only allowed to manage SIM subscriptions.
160 PhoneAccount targetPhoneAccount = registrar.getPhoneAccount(targetPhoneAccountHandle);
161 boolean isSimSubscription = (targetPhoneAccount.getCapabilities() &
162 PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION) != 0;
163 if (!isSimSubscription) {
164 return false;
165 }
166
167 return true;
168 }
169
170 // If there exists a registered connection manager then use it.
171 private void adjustAttemptsForConnectionManager() {
172 if (shouldSetConnectionManager()) {
173 CallAttemptRecord record = new CallAttemptRecord(
174 TelecommApp.getInstance().getPhoneAccountRegistrar().getSimCallManager(),
175 mAttemptRecords.get(0).targetPhoneAccount);
176 Log.v(this, "setConnectionManager, changing %s -> %s",
177 mAttemptRecords.get(0).targetPhoneAccount, record);
178 mAttemptRecords.set(0, record);
179 } else {
180 Log.v(this, "setConnectionManager, not changing");
Ihab Awad293edf22014-07-24 17:52:29 -0700181 }
182 }
183
Sailesh Nepal664837f2014-07-14 16:31:51 -0700184 // If we are possibly attempting to call a local emergency number, ensure that the
Ihab Awad69eb0f52014-07-18 11:20:37 -0700185 // plain PSTN connection services are listed, and nothing else.
Ihab Awad293edf22014-07-24 17:52:29 -0700186 private void adjustAttemptsForEmergency() {
Ihab Awad69eb0f52014-07-18 11:20:37 -0700187 if (TelephonyUtil.shouldProcessAsEmergency(TelecommApp.getInstance(), mCall.getHandle())) {
188 Log.i(this, "Emergency number detected");
Ihab Awad293edf22014-07-24 17:52:29 -0700189 mAttemptRecords.clear();
Evan Charlton94d01622014-07-20 12:32:05 -0700190 List<PhoneAccountHandle> allAccountHandles = TelecommApp.getInstance()
Ihab Awad6fb37c82014-08-07 19:48:57 -0700191 .getPhoneAccountRegistrar().getOutgoingPhoneAccounts();
Evan Charlton94d01622014-07-20 12:32:05 -0700192 for (int i = 0; i < allAccountHandles.size(); i++) {
193 if (TelephonyUtil.isPstnComponentName(
194 allAccountHandles.get(i).getComponentName())) {
195 Log.i(this, "Will try PSTN account %s for emergency", allAccountHandles.get(i));
Ihab Awad293edf22014-07-24 17:52:29 -0700196 mAttemptRecords.add(
197 new CallAttemptRecord(
198 allAccountHandles.get(i),
199 allAccountHandles.get(i)));
Sailesh Nepal664837f2014-07-14 16:31:51 -0700200 }
201 }
202 }
203 }
204
Sailesh Nepal664837f2014-07-14 16:31:51 -0700205 private class Response implements CreateConnectionResponse {
206 private final ConnectionServiceWrapper mService;
207
208 Response(ConnectionServiceWrapper service) {
209 mService = service;
210 }
211
212 @Override
Ihab Awadfb5560d2014-08-18 09:32:51 -0700213 public void handleCreateConnectionSuccess(ParcelableConnection connection) {
Sailesh Nepal664837f2014-07-14 16:31:51 -0700214 if (mResponse == null) {
Ihab Awadfb5560d2014-08-18 09:32:51 -0700215 // Nobody is listening for this connection attempt any longer; ask the responsible
216 // ConnectionService to tear down any resources associated with the call
Sailesh Nepal664837f2014-07-14 16:31:51 -0700217 mService.abort(mCall);
218 } else {
Ihab Awadfb5560d2014-08-18 09:32:51 -0700219 // Success -- share the good news and remember that we are no longer interested
220 // in hearing about any more attempts
221 mResponse.handleCreateConnectionSuccess(connection);
Santos Cordon72890ce2014-07-21 01:32:04 -0700222 mResponse = null;
Sailesh Nepal664837f2014-07-14 16:31:51 -0700223 }
224 }
225
226 @Override
Ihab Awadfb5560d2014-08-18 09:32:51 -0700227 public void handleCreateConnectionFailure(int code, String msg) {
228 // Failure of some sort; record the reasons for failure and try again if possible
229 Log.d(CreateConnectionProcessor.this, "Connection failed: %d (%s)", code, msg);
Sailesh Nepal664837f2014-07-14 16:31:51 -0700230 mLastErrorCode = code;
231 mLastErrorMsg = msg;
Ihab Awad69eb0f52014-07-18 11:20:37 -0700232 attemptNextPhoneAccount();
Sailesh Nepal664837f2014-07-14 16:31:51 -0700233 }
Sailesh Nepal664837f2014-07-14 16:31:51 -0700234 }
235}