blob: 6adb72cb2fdab3f7adbf6ede22be4e04a288f79e [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
Tyler Gunn7cc70b42014-09-12 22:17:27 -070017package com.android.server.telecom;
Sailesh Nepal664837f2014-07-14 16:31:51 -070018
Tyler Gunn91d43cf2014-09-17 12:19:39 -070019import android.content.Context;
Andrew Lee701dc002014-09-11 21:29:12 -070020import android.telecom.DisconnectCause;
Tyler Gunn7cc70b42014-09-12 22:17:27 -070021import android.telecom.ParcelableConnection;
Tyler Gunn91d43cf2014-09-17 12:19:39 -070022import android.telecom.Phone;
Tyler Gunn7cc70b42014-09-12 22:17:27 -070023import android.telecom.PhoneAccount;
24import android.telecom.PhoneAccountHandle;
Sailesh Nepal664837f2014-07-14 16:31:51 -070025
Tyler Gunn91d43cf2014-09-17 12:19:39 -070026// TODO: Needed for move to system service: import com.android.internal.R;
27
Sailesh Nepal664837f2014-07-14 16:31:51 -070028import java.util.ArrayList;
29import java.util.Iterator;
30import java.util.List;
Ihab Awad293edf22014-07-24 17:52:29 -070031import java.util.Objects;
Sailesh Nepal664837f2014-07-14 16:31:51 -070032
33/**
34 * This class creates connections to place new outgoing calls to attached to an existing incoming
35 * call. In either case, this class cycles through a set of connection services until:
36 * - a connection service returns a newly created connection in which case the call is displayed
37 * to the user
38 * - a connection service cancels the process, in which case the call is aborted
39 */
40final class CreateConnectionProcessor {
Ihab Awad293edf22014-07-24 17:52:29 -070041
42 // Describes information required to attempt to make a phone call
43 private static class CallAttemptRecord {
44 // The PhoneAccount describing the target connection service which we will
45 // contact in order to process an attempt
Ihab Awadb78b2762014-07-25 15:16:23 -070046 public final PhoneAccountHandle connectionManagerPhoneAccount;
Ihab Awad293edf22014-07-24 17:52:29 -070047 // The PhoneAccount which we will tell the target connection service to use
48 // for attempting to make the actual phone call
Ihab Awadb78b2762014-07-25 15:16:23 -070049 public final PhoneAccountHandle targetPhoneAccount;
Ihab Awad293edf22014-07-24 17:52:29 -070050
51 public CallAttemptRecord(
Ihab Awadb78b2762014-07-25 15:16:23 -070052 PhoneAccountHandle connectionManagerPhoneAccount,
53 PhoneAccountHandle targetPhoneAccount) {
54 this.connectionManagerPhoneAccount = connectionManagerPhoneAccount;
55 this.targetPhoneAccount = targetPhoneAccount;
Ihab Awad293edf22014-07-24 17:52:29 -070056 }
57
58 @Override
59 public String toString() {
60 return "CallAttemptRecord("
Ihab Awadb78b2762014-07-25 15:16:23 -070061 + Objects.toString(connectionManagerPhoneAccount) + ","
62 + Objects.toString(targetPhoneAccount) + ")";
Ihab Awad293edf22014-07-24 17:52:29 -070063 }
Tyler Gunn6e6f6d12014-08-20 15:22:18 -070064
65 /**
66 * Determines if this instance of {@code CallAttemptRecord} has the same underlying
67 * {@code PhoneAccountHandle}s as another instance.
68 *
69 * @param obj The other instance to compare against.
70 * @return {@code True} if the {@code CallAttemptRecord}s are equal.
71 */
72 @Override
73 public boolean equals(Object obj) {
74 if (obj instanceof CallAttemptRecord) {
75 CallAttemptRecord other = (CallAttemptRecord) obj;
76 return Objects.equals(connectionManagerPhoneAccount,
77 other.connectionManagerPhoneAccount) &&
78 Objects.equals(targetPhoneAccount, other.targetPhoneAccount);
79 }
80 return false;
81 }
Ihab Awad293edf22014-07-24 17:52:29 -070082 }
83
Sailesh Nepal664837f2014-07-14 16:31:51 -070084 private final Call mCall;
85 private final ConnectionServiceRepository mRepository;
Ihab Awad293edf22014-07-24 17:52:29 -070086 private List<CallAttemptRecord> mAttemptRecords;
87 private Iterator<CallAttemptRecord> mAttemptRecordIterator;
Sailesh Nepal664837f2014-07-14 16:31:51 -070088 private CreateConnectionResponse mResponse;
Andrew Lee701dc002014-09-11 21:29:12 -070089 private DisconnectCause mLastErrorDisconnectCause;
Tyler Gunn91d43cf2014-09-17 12:19:39 -070090 private final PhoneAccountRegistrar mPhoneAccountRegistrar;
91 private final Context mContext;
Sailesh Nepal664837f2014-07-14 16:31:51 -070092
93 CreateConnectionProcessor(
Tyler Gunn91d43cf2014-09-17 12:19:39 -070094 Call call, ConnectionServiceRepository repository, CreateConnectionResponse response,
95 PhoneAccountRegistrar phoneAccountRegistrar, Context context) {
Sailesh Nepal664837f2014-07-14 16:31:51 -070096 mCall = call;
97 mRepository = repository;
98 mResponse = response;
Tyler Gunn91d43cf2014-09-17 12:19:39 -070099 mPhoneAccountRegistrar = phoneAccountRegistrar;
100 mContext = context;
Sailesh Nepal664837f2014-07-14 16:31:51 -0700101 }
102
103 void process() {
104 Log.v(this, "process");
Ihab Awad293edf22014-07-24 17:52:29 -0700105 mAttemptRecords = new ArrayList<>();
Ihab Awadb78b2762014-07-25 15:16:23 -0700106 if (mCall.getTargetPhoneAccount() != null) {
Sailesh Nepal7957f9c2014-08-09 17:13:19 -0700107 mAttemptRecords.add(new CallAttemptRecord(
108 mCall.getTargetPhoneAccount(), mCall.getTargetPhoneAccount()));
Sailesh Nepal664837f2014-07-14 16:31:51 -0700109 }
Sailesh Nepal7957f9c2014-08-09 17:13:19 -0700110 adjustAttemptsForConnectionManager();
Ihab Awad293edf22014-07-24 17:52:29 -0700111 adjustAttemptsForEmergency();
112 mAttemptRecordIterator = mAttemptRecords.iterator();
Ihab Awad69eb0f52014-07-18 11:20:37 -0700113 attemptNextPhoneAccount();
Sailesh Nepal664837f2014-07-14 16:31:51 -0700114 }
115
116 void abort() {
117 Log.v(this, "abort");
118
119 // Clear the response first to prevent attemptNextConnectionService from attempting any
120 // more services.
121 CreateConnectionResponse response = mResponse;
122 mResponse = null;
123
124 ConnectionServiceWrapper service = mCall.getConnectionService();
125 if (service != null) {
126 service.abort(mCall);
127 mCall.clearConnectionService();
128 }
129 if (response != null) {
Andrew Lee701dc002014-09-11 21:29:12 -0700130 response.handleCreateConnectionFailure(new DisconnectCause(DisconnectCause.LOCAL));
Sailesh Nepal664837f2014-07-14 16:31:51 -0700131 }
132 }
133
Ihab Awad69eb0f52014-07-18 11:20:37 -0700134 private void attemptNextPhoneAccount() {
135 Log.v(this, "attemptNextPhoneAccount");
Tyler Gunncb59b672014-08-20 09:02:11 -0700136 CallAttemptRecord attempt = null;
137 if (mAttemptRecordIterator.hasNext()) {
138 attempt = mAttemptRecordIterator.next();
Sailesh Nepal664837f2014-07-14 16:31:51 -0700139
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700140 if (!mPhoneAccountRegistrar.phoneAccountHasPermission(
141 attempt.connectionManagerPhoneAccount)) {
Tyler Gunncb59b672014-08-20 09:02:11 -0700142 Log.w(this,
143 "Connection mgr does not have BIND_CONNECTION_SERVICE for attempt: %s",
144 attempt);
145 attemptNextPhoneAccount();
146 return;
147 }
148
149 // If the target PhoneAccount differs from the ConnectionManager phone acount, ensure it
150 // also has BIND_CONNECTION_SERVICE permission.
151 if (!attempt.connectionManagerPhoneAccount.equals(attempt.targetPhoneAccount) &&
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700152 !mPhoneAccountRegistrar.phoneAccountHasPermission(attempt.targetPhoneAccount)) {
Tyler Gunncb59b672014-08-20 09:02:11 -0700153 Log.w(this,
154 "Target PhoneAccount does not have BIND_CONNECTION_SERVICE for attempt: %s",
155 attempt);
156 attemptNextPhoneAccount();
157 return;
158 }
159 }
160
161 if (mResponse != null && attempt != null) {
Ihab Awad293edf22014-07-24 17:52:29 -0700162 Log.i(this, "Trying attempt %s", attempt);
Evan Charlton94d01622014-07-20 12:32:05 -0700163 ConnectionServiceWrapper service =
Ihab Awadb78b2762014-07-25 15:16:23 -0700164 mRepository.getService(
165 attempt.connectionManagerPhoneAccount.getComponentName());
Sailesh Nepal664837f2014-07-14 16:31:51 -0700166 if (service == null) {
Ihab Awad293edf22014-07-24 17:52:29 -0700167 Log.i(this, "Found no connection service for attempt %s", attempt);
Ihab Awad69eb0f52014-07-18 11:20:37 -0700168 attemptNextPhoneAccount();
Sailesh Nepal664837f2014-07-14 16:31:51 -0700169 } else {
Ihab Awadb78b2762014-07-25 15:16:23 -0700170 mCall.setConnectionManagerPhoneAccount(attempt.connectionManagerPhoneAccount);
171 mCall.setTargetPhoneAccount(attempt.targetPhoneAccount);
Sailesh Nepal664837f2014-07-14 16:31:51 -0700172 mCall.setConnectionService(service);
173 Log.i(this, "Attempting to call from %s", service.getComponentName());
174 service.createConnection(mCall, new Response(service));
175 }
176 } else {
Ihab Awad69eb0f52014-07-18 11:20:37 -0700177 Log.v(this, "attemptNextPhoneAccount, no more accounts, failing");
Sailesh Nepal664837f2014-07-14 16:31:51 -0700178 if (mResponse != null) {
Andrew Lee701dc002014-09-11 21:29:12 -0700179 mResponse.handleCreateConnectionFailure(mLastErrorDisconnectCause);
Sailesh Nepal664837f2014-07-14 16:31:51 -0700180 mResponse = null;
181 mCall.clearConnectionService();
182 }
183 }
184 }
185
Sailesh Nepal7957f9c2014-08-09 17:13:19 -0700186 private boolean shouldSetConnectionManager() {
187 if (mAttemptRecords.size() == 0) {
188 return false;
Ihab Awad293edf22014-07-24 17:52:29 -0700189 }
Ihab Awadc17294c2014-08-04 19:23:37 -0700190
Sailesh Nepal7957f9c2014-08-09 17:13:19 -0700191 if (mAttemptRecords.size() > 1) {
192 Log.d(this, "shouldSetConnectionManager, error, mAttemptRecords should not have more "
193 + "than 1 record");
194 return false;
195 }
196
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700197 PhoneAccountHandle connectionManager = mPhoneAccountRegistrar.getSimCallManager();
Sailesh Nepal7957f9c2014-08-09 17:13:19 -0700198 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.
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700208 PhoneAccount targetPhoneAccount = mPhoneAccountRegistrar.getPhoneAccount(
209 targetPhoneAccountHandle);
Sailesh Nepal7957f9c2014-08-09 17:13:19 -0700210 boolean isSimSubscription = (targetPhoneAccount.getCapabilities() &
211 PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION) != 0;
212 if (!isSimSubscription) {
213 return false;
214 }
215
216 return true;
217 }
218
219 // If there exists a registered connection manager then use it.
220 private void adjustAttemptsForConnectionManager() {
221 if (shouldSetConnectionManager()) {
222 CallAttemptRecord record = new CallAttemptRecord(
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700223 mPhoneAccountRegistrar.getSimCallManager(),
Sailesh Nepal7957f9c2014-08-09 17:13:19 -0700224 mAttemptRecords.get(0).targetPhoneAccount);
225 Log.v(this, "setConnectionManager, changing %s -> %s",
226 mAttemptRecords.get(0).targetPhoneAccount, record);
227 mAttemptRecords.set(0, record);
228 } else {
229 Log.v(this, "setConnectionManager, not changing");
Ihab Awad293edf22014-07-24 17:52:29 -0700230 }
231 }
232
Sailesh Nepal664837f2014-07-14 16:31:51 -0700233 // If we are possibly attempting to call a local emergency number, ensure that the
Ihab Awad69eb0f52014-07-18 11:20:37 -0700234 // plain PSTN connection services are listed, and nothing else.
Ihab Awad293edf22014-07-24 17:52:29 -0700235 private void adjustAttemptsForEmergency() {
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700236 if (TelephonyUtil.shouldProcessAsEmergency(mContext, mCall.getHandle())) {
Ihab Awad69eb0f52014-07-18 11:20:37 -0700237 Log.i(this, "Emergency number detected");
Ihab Awad293edf22014-07-24 17:52:29 -0700238 mAttemptRecords.clear();
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700239 List<PhoneAccount> allAccounts = mPhoneAccountRegistrar.getAllPhoneAccounts();
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700240 // First, add SIM phone accounts which can place emergency calls.
241 for (PhoneAccount phoneAccount : allAccounts) {
242 if (phoneAccount.hasCapabilities(PhoneAccount.CAPABILITY_PLACE_EMERGENCY_CALLS) &&
243 phoneAccount.hasCapabilities(PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION)) {
244 Log.i(this, "Will try PSTN account %s for emergency",
245 phoneAccount.getAccountHandle());
Ihab Awad293edf22014-07-24 17:52:29 -0700246 mAttemptRecords.add(
247 new CallAttemptRecord(
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700248 phoneAccount.getAccountHandle(),
249 phoneAccount.getAccountHandle()));
Sailesh Nepal664837f2014-07-14 16:31:51 -0700250 }
251 }
Tyler Gunn6e6f6d12014-08-20 15:22:18 -0700252
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700253 // Next, add the connection manager account as a backup if it can place emergency calls.
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700254 PhoneAccountHandle callManagerHandle = mPhoneAccountRegistrar.getSimCallManager();
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700255 if (callManagerHandle != null) {
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700256 PhoneAccount callManager = mPhoneAccountRegistrar
257 .getPhoneAccount(callManagerHandle);
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700258 if (callManager.hasCapabilities(PhoneAccount.CAPABILITY_PLACE_EMERGENCY_CALLS)) {
259 CallAttemptRecord callAttemptRecord = new CallAttemptRecord(callManagerHandle,
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700260 mPhoneAccountRegistrar.
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700261 getDefaultOutgoingPhoneAccount(mCall.getHandle().getScheme())
262 );
Tyler Gunn6e6f6d12014-08-20 15:22:18 -0700263
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700264 if (!mAttemptRecords.contains(callAttemptRecord)) {
265 Log.i(this, "Will try Connection Manager account %s for emergency",
266 callManager);
267 mAttemptRecords.add(callAttemptRecord);
268 }
269 }
Tyler Gunn6e6f6d12014-08-20 15:22:18 -0700270 }
Sailesh Nepal664837f2014-07-14 16:31:51 -0700271 }
272 }
273
Sailesh Nepal664837f2014-07-14 16:31:51 -0700274 private class Response implements CreateConnectionResponse {
275 private final ConnectionServiceWrapper mService;
276
277 Response(ConnectionServiceWrapper service) {
278 mService = service;
279 }
280
281 @Override
Ihab Awad80008452014-08-23 20:35:44 -0700282 public void handleCreateConnectionSuccess(
283 CallIdMapper idMapper,
284 ParcelableConnection connection) {
Sailesh Nepal664837f2014-07-14 16:31:51 -0700285 if (mResponse == null) {
Ihab Awadfb5560d2014-08-18 09:32:51 -0700286 // Nobody is listening for this connection attempt any longer; ask the responsible
287 // ConnectionService to tear down any resources associated with the call
Sailesh Nepal664837f2014-07-14 16:31:51 -0700288 mService.abort(mCall);
289 } else {
Ihab Awadfb5560d2014-08-18 09:32:51 -0700290 // Success -- share the good news and remember that we are no longer interested
291 // in hearing about any more attempts
Ihab Awad80008452014-08-23 20:35:44 -0700292 mResponse.handleCreateConnectionSuccess(idMapper, connection);
Santos Cordon72890ce2014-07-21 01:32:04 -0700293 mResponse = null;
Sailesh Nepal664837f2014-07-14 16:31:51 -0700294 }
295 }
296
297 @Override
Andrew Lee701dc002014-09-11 21:29:12 -0700298 public void handleCreateConnectionFailure(DisconnectCause errorDisconnectCause) {
Ihab Awadfb5560d2014-08-18 09:32:51 -0700299 // Failure of some sort; record the reasons for failure and try again if possible
Andrew Lee701dc002014-09-11 21:29:12 -0700300 Log.d(CreateConnectionProcessor.this, "Connection failed: (%s)", errorDisconnectCause);
301 mLastErrorDisconnectCause = errorDisconnectCause;
Ihab Awad69eb0f52014-07-18 11:20:37 -0700302 attemptNextPhoneAccount();
Sailesh Nepal664837f2014-07-14 16:31:51 -0700303 }
Sailesh Nepal664837f2014-07-14 16:31:51 -0700304 }
305}