blob: ea82d5216702d3d6a504e01b9ea5529634884451 [file] [log] [blame]
Santos Cordon176ae282014-07-14 02:02:14 -07001/*
2 * Copyright (C) 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;
Santos Cordon176ae282014-07-14 02:02:14 -070018
Tyler Gunncb59b672014-08-20 09:02:11 -070019import android.Manifest;
Evan Charlton105d9772014-11-25 14:08:53 -080020import android.content.ComponentName;
21import android.content.Context;
Evan Charltonaf51ceb2014-07-30 11:56:36 -070022import android.content.Intent;
23import android.content.pm.PackageManager;
24import android.content.pm.ResolveInfo;
Tyler Gunncb59b672014-08-20 09:02:11 -070025import android.content.pm.ServiceInfo;
Tony Mak4a3e2fd2015-12-04 11:58:38 +000026import android.content.pm.UserInfo;
Ihab Awadd9f54382014-10-24 11:44:47 -070027import android.graphics.Bitmap;
28import android.graphics.BitmapFactory;
Santos Cordon9c30c282015-05-13 16:28:27 -070029import android.graphics.drawable.Icon;
Evan Charlton105d9772014-11-25 14:08:53 -080030import android.net.Uri;
Tyler Gunn369c8742015-10-07 15:39:28 -070031import android.os.Bundle;
Hall Liu3f7f64d2016-01-07 17:40:13 -080032import android.os.AsyncTask;
Sailesh Nepalaecfa9b2015-06-16 14:55:27 -070033import android.os.PersistableBundle;
Evan Charlton105d9772014-11-25 14:08:53 -080034import android.os.Process;
35import android.os.UserHandle;
36import android.os.UserManager;
Tyler Gunn84253572014-09-02 14:50:05 -070037import android.provider.Settings;
Christine Hallstromffe558c2016-11-30 16:05:13 -080038import android.telecom.CallAudioState;
Tyler Gunn7cc70b42014-09-12 22:17:27 -070039import android.telecom.ConnectionService;
Santos Cordon59c21a72015-06-11 10:11:21 -070040import android.telecom.DefaultDialerManager;
Brad Ebinger953e1af2016-10-05 15:45:22 -070041import android.telecom.Log;
Tyler Gunn7cc70b42014-09-12 22:17:27 -070042import android.telecom.PhoneAccount;
43import android.telecom.PhoneAccountHandle;
Sailesh Nepalaecfa9b2015-06-16 14:55:27 -070044import android.telephony.CarrierConfigManager;
Nancy Chen140004a2014-10-15 15:48:38 -070045import android.telephony.PhoneNumberUtils;
Nancy Chen5a36b6e2014-10-23 17:42:42 -070046import android.telephony.SubscriptionManager;
Santos Cordon479b3022015-02-06 04:27:15 -080047import android.telephony.TelephonyManager;
Evan Charltonaf51ceb2014-07-30 11:56:36 -070048import android.text.TextUtils;
Ihab Awadb78b2762014-07-25 15:16:23 -070049import android.util.AtomicFile;
Ihab Awadd9f54382014-10-24 11:44:47 -070050import android.util.Base64;
Ihab Awadb78b2762014-07-25 15:16:23 -070051import android.util.Xml;
Santos Cordon176ae282014-07-14 02:02:14 -070052
Tyler Gunn91d43cf2014-09-17 12:19:39 -070053// TODO: Needed for move to system service: import com.android.internal.R;
Sailesh Nepal0e1dc5a2014-07-30 11:08:54 -070054import com.android.internal.annotations.VisibleForTesting;
55import com.android.internal.util.FastXmlSerializer;
Tyler Gunn9787e0e2014-10-14 14:36:12 -070056import com.android.internal.util.IndentingPrintWriter;
Sailesh Nepal0e1dc5a2014-07-30 11:08:54 -070057import com.android.internal.util.XmlUtils;
58
Evan Charltonaf51ceb2014-07-30 11:56:36 -070059import org.xmlpull.v1.XmlPullParser;
60import org.xmlpull.v1.XmlPullParserException;
61import org.xmlpull.v1.XmlSerializer;
62
Ihab Awadb78b2762014-07-25 15:16:23 -070063import java.io.BufferedInputStream;
Santos Cordon9c30c282015-05-13 16:28:27 -070064import java.io.ByteArrayInputStream;
Ihab Awadd9f54382014-10-24 11:44:47 -070065import java.io.ByteArrayOutputStream;
Ihab Awadb78b2762014-07-25 15:16:23 -070066import java.io.File;
67import java.io.FileNotFoundException;
68import java.io.FileOutputStream;
69import java.io.IOException;
70import java.io.InputStream;
Tyler Gunn84253572014-09-02 14:50:05 -070071import java.lang.Integer;
Tyler Gunncb59b672014-08-20 09:02:11 -070072import java.lang.SecurityException;
Tyler Gunnd900ce62014-08-13 11:40:59 -070073import java.lang.String;
Santos Cordon176ae282014-07-14 02:02:14 -070074import java.util.ArrayList;
Sailesh Nepal91fc8092015-02-14 15:44:55 -080075import java.util.Collections;
Tyler Gunnd900ce62014-08-13 11:40:59 -070076import java.util.Iterator;
Santos Cordon176ae282014-07-14 02:02:14 -070077import java.util.List;
Tony Mak4a3e2fd2015-12-04 11:58:38 +000078import java.util.Map;
Santos Cordon176ae282014-07-14 02:02:14 -070079import java.util.Objects;
Brad Ebinger0c87b3c2016-06-10 17:23:02 -070080import java.util.Optional;
Tony Mak4a3e2fd2015-12-04 11:58:38 +000081import java.util.concurrent.ConcurrentHashMap;
Ihab Awadb78b2762014-07-25 15:16:23 -070082import java.util.concurrent.CopyOnWriteArrayList;
Brad Ebinger0c87b3c2016-06-10 17:23:02 -070083import java.util.stream.Collector;
84import java.util.stream.Collectors;
85import java.util.stream.Stream;
Santos Cordon176ae282014-07-14 02:02:14 -070086
87/**
Evan Charlton89176372014-07-19 18:23:09 -070088 * Handles writing and reading PhoneAccountHandle registration entries. This is a simple verbatim
Santos Cordon6a212642015-05-08 16:35:23 -070089 * delegate for all the account handling methods on {@link android.telecom.TelecomManager} as
90 * implemented in {@link TelecomServiceImpl}, with the notable exception that
91 * {@link TelecomServiceImpl} is responsible for security checking to make sure that the caller has
92 * proper authority over the {@code ComponentName}s they are declaring in their
93 * {@code PhoneAccountHandle}s.
94 *
95 *
96 * -- About Users and Phone Accounts --
97 *
Santos Cordon9c30c282015-05-13 16:28:27 -070098 * We store all phone accounts for all users in a single place, which means that there are three
99 * users that we have to deal with in code:
Santos Cordon6a212642015-05-08 16:35:23 -0700100 * 1) The Android User that is currently active on the device.
101 * 2) The user which owns/registers the phone account.
102 * 3) The user running the app that is requesting the phone account information.
103 *
104 * For example, I have a device with 2 users, primary (A) and secondary (B), and the secondary user
Tony Mak240656f2015-12-04 11:36:22 +0000105 * has a work profile running as another user (B2). Each user/profile only have the visibility of
106 * phone accounts owned by them. Lets say, user B (settings) is requesting a list of phone accounts,
107 * and the list only contains phone accounts owned by user B and accounts with
108 * {@link PhoneAccount#CAPABILITY_MULTI_USER}.
Santos Cordon6a212642015-05-08 16:35:23 -0700109 *
110 * In practice, (2) is stored with the phone account handle and is part of the handle's ID. (1) is
111 * saved in {@link #mCurrentUserHandle} and (3) we get from Binder.getCallingUser(). We check these
112 * users for visibility before returning any phone accounts.
Santos Cordon176ae282014-07-14 02:02:14 -0700113 */
Brad Ebinger53855132015-10-30 10:58:19 -0700114public class PhoneAccountRegistrar {
Santos Cordon176ae282014-07-14 02:02:14 -0700115
Yorke Lee5e8836a2014-08-22 15:25:18 -0700116 public static final PhoneAccountHandle NO_ACCOUNT_SELECTED =
117 new PhoneAccountHandle(new ComponentName("null", "null"), "NO_ACCOUNT_SELECTED");
118
Ihab Awadb78b2762014-07-25 15:16:23 -0700119 public abstract static class Listener {
120 public void onAccountsChanged(PhoneAccountRegistrar registrar) {}
121 public void onDefaultOutgoingChanged(PhoneAccountRegistrar registrar) {}
122 public void onSimCallManagerChanged(PhoneAccountRegistrar registrar) {}
123 }
124
Tyler Gunn2b17f232017-03-08 08:51:00 -0800125 /**
126 * Abstracts away dependency on the {@link PackageManager} required to fetch the label for an
127 * app.
128 */
129 public interface AppLabelProxy {
130 CharSequence getAppLabel(String packageName);
131 }
132
Ihab Awadb78b2762014-07-25 15:16:23 -0700133 private static final String FILE_NAME = "phone-account-registrar-state.xml";
Tyler Gunn84253572014-09-02 14:50:05 -0700134 @VisibleForTesting
Tony Mak4a3e2fd2015-12-04 11:58:38 +0000135 public static final int EXPECTED_STATE_VERSION = 9;
Tyler Gunn84253572014-09-02 14:50:05 -0700136
137 /** Keep in sync with the same in SipSettings.java */
138 private static final String SIP_SHARED_PREFERENCES = "SIP_PREFERENCES";
Ihab Awadb78b2762014-07-25 15:16:23 -0700139
140 private final List<Listener> mListeners = new CopyOnWriteArrayList<>();
141 private final AtomicFile mAtomicFile;
Santos Cordonafe59e52014-08-22 16:48:43 -0700142 private final Context mContext;
Evan Charlton105d9772014-11-25 14:08:53 -0800143 private final UserManager mUserManager;
Wink Saville33c05d32014-11-20 13:04:17 -0800144 private final SubscriptionManager mSubscriptionManager;
Hall Liu2b33df72016-12-06 18:15:39 -0800145 private final DefaultDialerCache mDefaultDialerCache;
Tyler Gunn2b17f232017-03-08 08:51:00 -0800146 private final AppLabelProxy mAppLabelProxy;
Ihab Awadb78b2762014-07-25 15:16:23 -0700147 private State mState;
Evan Charlton105d9772014-11-25 14:08:53 -0800148 private UserHandle mCurrentUserHandle;
Hall Liu3f7f64d2016-01-07 17:40:13 -0800149 private interface PhoneAccountRegistrarWriteLock {}
150 private final PhoneAccountRegistrarWriteLock mWriteLock =
151 new PhoneAccountRegistrarWriteLock() {};
Santos Cordon176ae282014-07-14 02:02:14 -0700152
Nancy Chen06ce0622014-10-23 01:17:35 +0000153 @VisibleForTesting
Tyler Gunn2b17f232017-03-08 08:51:00 -0800154 public PhoneAccountRegistrar(Context context, DefaultDialerCache defaultDialerCache,
155 AppLabelProxy appLabelProxy) {
156 this(context, FILE_NAME, defaultDialerCache, appLabelProxy);
Ihab Awadb78b2762014-07-25 15:16:23 -0700157 }
158
159 @VisibleForTesting
Hall Liu2b33df72016-12-06 18:15:39 -0800160 public PhoneAccountRegistrar(Context context, String fileName,
Tyler Gunn2b17f232017-03-08 08:51:00 -0800161 DefaultDialerCache defaultDialerCache, AppLabelProxy appLabelProxy) {
162
Ihab Awadb78b2762014-07-25 15:16:23 -0700163 mAtomicFile = new AtomicFile(new File(context.getFilesDir(), fileName));
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700164
Ihab Awadb78b2762014-07-25 15:16:23 -0700165 mState = new State();
Santos Cordonafe59e52014-08-22 16:48:43 -0700166 mContext = context;
Evan Charlton105d9772014-11-25 14:08:53 -0800167 mUserManager = UserManager.get(context);
Hall Liu2b33df72016-12-06 18:15:39 -0800168 mDefaultDialerCache = defaultDialerCache;
Wink Saville33c05d32014-11-20 13:04:17 -0800169 mSubscriptionManager = SubscriptionManager.from(mContext);
Tyler Gunn2b17f232017-03-08 08:51:00 -0800170 mAppLabelProxy = appLabelProxy;
Evan Charlton105d9772014-11-25 14:08:53 -0800171 mCurrentUserHandle = Process.myUserHandle();
Ihab Awadb78b2762014-07-25 15:16:23 -0700172 read();
Santos Cordon176ae282014-07-14 02:02:14 -0700173 }
174
Tyler Gunn84253572014-09-02 14:50:05 -0700175 /**
Nancy Chen140004a2014-10-15 15:48:38 -0700176 * Retrieves the subscription id for a given phone account if it exists. Subscription ids
177 * apply only to PSTN/SIM card phone accounts so all other accounts should not have a
178 * subscription id.
179 * @param accountHandle The handle for the phone account for which to retrieve the
180 * subscription id.
Wink Saville35850602014-10-23 15:57:21 -0700181 * @return The value of the subscription id or -1 if it does not exist or is not valid.
Nancy Chen140004a2014-10-15 15:48:38 -0700182 */
Wink Saville35850602014-10-23 15:57:21 -0700183 public int getSubscriptionIdForPhoneAccount(PhoneAccountHandle accountHandle) {
Tony Mak240656f2015-12-04 11:36:22 +0000184 PhoneAccount account = getPhoneAccountUnchecked(accountHandle);
Santos Cordon479b3022015-02-06 04:27:15 -0800185
Santos Cordon6a212642015-05-08 16:35:23 -0700186 if (account != null && account.hasCapabilities(PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION)) {
Santos Cordon479b3022015-02-06 04:27:15 -0800187 TelephonyManager tm =
188 (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE);
189 return tm.getSubIdForPhoneAccount(account);
Nancy Chen140004a2014-10-15 15:48:38 -0700190 }
Santos Cordon479b3022015-02-06 04:27:15 -0800191 return SubscriptionManager.INVALID_SUBSCRIPTION_ID;
Nancy Chen140004a2014-10-15 15:48:38 -0700192 }
193
194 /**
Evan Charlton105d9772014-11-25 14:08:53 -0800195 * Retrieves the default outgoing phone account supporting the specified uriScheme. Note that if
196 * {@link #mCurrentUserHandle} does not have visibility into the current default, {@code null}
197 * will be returned.
198 *
Tyler Gunn84253572014-09-02 14:50:05 -0700199 * @param uriScheme The URI scheme for the outgoing call.
200 * @return The {@link PhoneAccountHandle} to use.
201 */
Tony Mak240656f2015-12-04 11:36:22 +0000202 public PhoneAccountHandle getOutgoingPhoneAccountForScheme(String uriScheme,
203 UserHandle userHandle) {
204 final PhoneAccountHandle userSelected = getUserSelectedOutgoingPhoneAccount(userHandle);
Tyler Gunn84253572014-09-02 14:50:05 -0700205
Yorke Lee5e8836a2014-08-22 15:25:18 -0700206 if (userSelected != null) {
Tyler Gunn84253572014-09-02 14:50:05 -0700207 // If there is a default PhoneAccount, ensure it supports calls to handles with the
208 // specified uriScheme.
Tony Mak240656f2015-12-04 11:36:22 +0000209 final PhoneAccount userSelectedAccount = getPhoneAccountUnchecked(userSelected);
Santos Cordon6a212642015-05-08 16:35:23 -0700210 if (userSelectedAccount.supportsUriScheme(uriScheme)) {
Tyler Gunn84253572014-09-02 14:50:05 -0700211 return userSelected;
212 }
Ihab Awad293edf22014-07-24 17:52:29 -0700213 }
214
Tony Mak240656f2015-12-04 11:36:22 +0000215 List<PhoneAccountHandle> outgoing = getCallCapablePhoneAccounts(uriScheme, false,
216 userHandle);
Ihab Awad6fb37c82014-08-07 19:48:57 -0700217 switch (outgoing.size()) {
Ihab Awad293edf22014-07-24 17:52:29 -0700218 case 0:
219 // There are no accounts, so there can be no default
220 return null;
221 case 1:
Evan Charlton105d9772014-11-25 14:08:53 -0800222 // There is only one account, which is by definition the default.
Santos Cordon6a212642015-05-08 16:35:23 -0700223 return outgoing.get(0);
Ihab Awad293edf22014-07-24 17:52:29 -0700224 default:
225 // There are multiple accounts with no selected default
226 return null;
Ihab Awadf2a84912014-07-22 21:09:25 -0700227 }
Ihab Awad104f8062014-07-17 11:29:35 -0700228 }
Santos Cordon176ae282014-07-14 02:02:14 -0700229
Tony Mak240656f2015-12-04 11:36:22 +0000230 public PhoneAccountHandle getOutgoingPhoneAccountForSchemeOfCurrentUser(String uriScheme) {
231 return getOutgoingPhoneAccountForScheme(uriScheme, mCurrentUserHandle);
232 }
233
Evan Charlton105d9772014-11-25 14:08:53 -0800234 /**
235 * @return The user-selected outgoing {@link PhoneAccount}, or null if it hasn't been set (or
236 * if it was set by another user).
237 */
Hall Liuecda5542015-12-04 11:31:31 -0800238 @VisibleForTesting
239 public PhoneAccountHandle getUserSelectedOutgoingPhoneAccount(UserHandle userHandle) {
Tony Mak4a3e2fd2015-12-04 11:58:38 +0000240 if (userHandle == null) {
241 return null;
242 }
243 DefaultPhoneAccountHandle defaultPhoneAccountHandle = mState.defaultOutgoingAccountHandles
244 .get(userHandle);
245 if (defaultPhoneAccountHandle == null) {
246 return null;
247 }
248 // Make sure the account is still registered and owned by the user.
249 PhoneAccount account = getPhoneAccount(defaultPhoneAccountHandle.phoneAccountHandle,
250 userHandle);
Hall Liuecda5542015-12-04 11:31:31 -0800251
Santos Cordon6a212642015-05-08 16:35:23 -0700252 if (account != null) {
Tony Mak4a3e2fd2015-12-04 11:58:38 +0000253 return defaultPhoneAccountHandle.phoneAccountHandle;
Yorke Lee5e8836a2014-08-22 15:25:18 -0700254 }
255 return null;
256 }
257
Santos Cordon6a212642015-05-08 16:35:23 -0700258 /**
Brad Ebinger0c87b3c2016-06-10 17:23:02 -0700259 * @return The {@link DefaultPhoneAccountHandle} containing the user-selected default calling
260 * account and group Id for the {@link UserHandle} specified.
261 */
262 private DefaultPhoneAccountHandle getUserSelectedDefaultPhoneAccount(UserHandle userHandle) {
263 if (userHandle == null) {
264 return null;
265 }
266 DefaultPhoneAccountHandle defaultPhoneAccountHandle = mState.defaultOutgoingAccountHandles
267 .get(userHandle);
268 if (defaultPhoneAccountHandle == null) {
269 return null;
270 }
271
272 return defaultPhoneAccountHandle;
273 }
274
275 /**
276 * @return The currently registered PhoneAccount in Telecom that has the same group Id.
277 */
278 private PhoneAccount getPhoneAccountByGroupId(String groupId, ComponentName groupComponentName,
279 UserHandle userHandle, PhoneAccountHandle excludePhoneAccountHandle) {
280 if (groupId == null || groupId.isEmpty() || userHandle == null) {
281 return null;
282 }
283 // Get the PhoneAccount with the same group Id (and same ComponentName) that is not the
284 // newAccount that was just added
285 List<PhoneAccount> accounts = getAllPhoneAccounts(userHandle).stream()
286 .filter(account -> groupId.equals(account.getGroupId()) &&
287 !account.getAccountHandle().equals(excludePhoneAccountHandle) &&
288 Objects.equals(account.getAccountHandle().getComponentName(),
289 groupComponentName))
290 .collect(Collectors.toList());
291 // There should be one or no PhoneAccounts with the same group Id
292 if (accounts.size() > 1) {
293 Log.w(this, "Found multiple PhoneAccounts registered to the same Group Id!");
294 }
295 return accounts.isEmpty() ? null : accounts.get(0);
296 }
297
298 /**
Santos Cordon6a212642015-05-08 16:35:23 -0700299 * Sets the phone account with which to place all calls by default. Set by the user
300 * within phone settings.
301 */
Tony Mak4a3e2fd2015-12-04 11:58:38 +0000302 public void setUserSelectedOutgoingPhoneAccount(PhoneAccountHandle accountHandle,
303 UserHandle userHandle) {
304 if (userHandle == null) {
305 return;
306 }
Evan Charlton89176372014-07-19 18:23:09 -0700307 if (accountHandle == null) {
Ihab Awad104f8062014-07-17 11:29:35 -0700308 // Asking to clear the default outgoing is a valid request
Tony Mak4a3e2fd2015-12-04 11:58:38 +0000309 mState.defaultOutgoingAccountHandles.remove(userHandle);
Ihab Awad104f8062014-07-17 11:29:35 -0700310 } else {
Tony Mak4a3e2fd2015-12-04 11:58:38 +0000311 PhoneAccount account = getPhoneAccount(accountHandle, userHandle);
Santos Cordon6a212642015-05-08 16:35:23 -0700312 if (account == null) {
Ihab Awadb78b2762014-07-25 15:16:23 -0700313 Log.w(this, "Trying to set nonexistent default outgoing %s",
314 accountHandle);
315 return;
316 }
317
Santos Cordon6a212642015-05-08 16:35:23 -0700318 if (!account.hasCapabilities(PhoneAccount.CAPABILITY_CALL_PROVIDER)) {
Ihab Awadb78b2762014-07-25 15:16:23 -0700319 Log.w(this, "Trying to set non-call-provider default outgoing %s",
Evan Charlton89176372014-07-19 18:23:09 -0700320 accountHandle);
Ihab Awad104f8062014-07-17 11:29:35 -0700321 return;
322 }
323
Santos Cordon6a212642015-05-08 16:35:23 -0700324 if (account.hasCapabilities(PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION)) {
Nancy Chen5a36b6e2014-10-23 17:42:42 -0700325 // If the account selected is a SIM account, propagate down to the subscription
326 // record.
Wink Saville7ce6e782014-10-27 10:56:46 -0700327 int subId = getSubscriptionIdForPhoneAccount(accountHandle);
Wink Saville33c05d32014-11-20 13:04:17 -0800328 mSubscriptionManager.setDefaultVoiceSubId(subId);
Nancy Chen5a36b6e2014-10-23 17:42:42 -0700329 }
330
Tony Mak4a3e2fd2015-12-04 11:58:38 +0000331 mState.defaultOutgoingAccountHandles
Brad Ebinger0c87b3c2016-06-10 17:23:02 -0700332 .put(userHandle, new DefaultPhoneAccountHandle(userHandle, accountHandle,
333 account.getGroupId()));
Santos Cordon176ae282014-07-14 02:02:14 -0700334 }
335
Ihab Awad293edf22014-07-24 17:52:29 -0700336 write();
Ihab Awadb78b2762014-07-25 15:16:23 -0700337 fireDefaultOutgoingChanged();
Santos Cordon176ae282014-07-14 02:02:14 -0700338 }
339
Nancy Chen668dee02014-11-19 15:31:31 -0800340 boolean isUserSelectedSmsPhoneAccount(PhoneAccountHandle accountHandle) {
Shishir Agrawalce688742016-01-25 15:02:21 -0800341 return getSubscriptionIdForPhoneAccount(accountHandle) ==
342 SubscriptionManager.getDefaultSmsSubscriptionId();
Sailesh Nepalfc43ea82015-07-28 19:30:28 -0700343 }
344
Sailesh Nepal8ed7eeb2015-09-08 12:30:13 -0700345 public ComponentName getSystemSimCallManagerComponent() {
346 String defaultSimCallManager = null;
347 CarrierConfigManager configManager = (CarrierConfigManager) mContext.getSystemService(
348 Context.CARRIER_CONFIG_SERVICE);
349 PersistableBundle configBundle = configManager.getConfig();
350 if (configBundle != null) {
351 defaultSimCallManager = configBundle.getString(
352 CarrierConfigManager.KEY_DEFAULT_SIM_CALL_MANAGER_STRING);
353 }
354 return TextUtils.isEmpty(defaultSimCallManager)
355 ? null : ComponentName.unflattenFromString(defaultSimCallManager);
356 }
357
Tony Mak240656f2015-12-04 11:36:22 +0000358 public PhoneAccountHandle getSimCallManagerOfCurrentUser() {
359 return getSimCallManager(mCurrentUserHandle);
360 }
361
Sailesh Nepalfc43ea82015-07-28 19:30:28 -0700362 /**
363 * Returns the {@link PhoneAccountHandle} corresponding to the currently active SIM Call
364 * Manager. SIM Call Manager returned corresponds to the following priority order:
365 * 1. If a SIM Call Manager {@link PhoneAccount} is registered for the same package as the
366 * default dialer, then that one is returned.
367 * 2. If there is a SIM Call Manager {@link PhoneAccount} registered which matches the
368 * carrier configuration's default, then that one is returned.
369 * 3. Otherwise, we return null.
370 */
Tony Mak240656f2015-12-04 11:36:22 +0000371 public PhoneAccountHandle getSimCallManager(UserHandle userHandle) {
Santos Cordon59c21a72015-06-11 10:11:21 -0700372 // Get the default dialer in case it has a connection manager associated with it.
Hall Liu2b33df72016-12-06 18:15:39 -0800373 String dialerPackage = mDefaultDialerCache
374 .getDefaultDialerApplication(userHandle.getIdentifier());
Evan Charltonaf51ceb2014-07-30 11:56:36 -0700375
Sailesh Nepalaecfa9b2015-06-16 14:55:27 -0700376 // Check carrier config.
Sailesh Nepal8ed7eeb2015-09-08 12:30:13 -0700377 ComponentName systemSimCallManagerComponent = getSystemSimCallManagerComponent();
Sailesh Nepal91fc8092015-02-14 15:44:55 -0800378
Santos Cordon59c21a72015-06-11 10:11:21 -0700379 PhoneAccountHandle dialerSimCallManager = null;
380 PhoneAccountHandle systemSimCallManager = null;
Sailesh Nepal91fc8092015-02-14 15:44:55 -0800381
Santos Cordon59c21a72015-06-11 10:11:21 -0700382 if (!TextUtils.isEmpty(dialerPackage) || systemSimCallManagerComponent != null) {
383 // loop through and look for any connection manager in the same package.
384 List<PhoneAccountHandle> allSimCallManagers = getPhoneAccountHandles(
385 PhoneAccount.CAPABILITY_CONNECTION_MANAGER, null, null,
Tony Mak240656f2015-12-04 11:36:22 +0000386 true /* includeDisabledAccounts */, userHandle);
Santos Cordon59c21a72015-06-11 10:11:21 -0700387 for (PhoneAccountHandle accountHandle : allSimCallManagers) {
388 ComponentName component = accountHandle.getComponentName();
389
390 // Store the system connection manager if found
391 if (systemSimCallManager == null
392 && Objects.equals(component, systemSimCallManagerComponent)
393 && !resolveComponent(accountHandle).isEmpty()) {
394 systemSimCallManager = accountHandle;
395
396 // Store the dialer connection manager if found
397 } else if (dialerSimCallManager == null
398 && Objects.equals(component.getPackageName(), dialerPackage)
399 && !resolveComponent(accountHandle).isEmpty()) {
400 dialerSimCallManager = accountHandle;
Evan Charltonaf51ceb2014-07-30 11:56:36 -0700401 }
Evan Charltonaf51ceb2014-07-30 11:56:36 -0700402 }
Evan Charltonaf51ceb2014-07-30 11:56:36 -0700403 }
404
Santos Cordon59c21a72015-06-11 10:11:21 -0700405 PhoneAccountHandle retval = dialerSimCallManager != null ?
406 dialerSimCallManager : systemSimCallManager;
407
408 Log.i(this, "SimCallManager queried, returning: %s", retval);
409
Sailesh Nepalb3ccd7e2015-06-26 18:16:18 -0700410 return retval;
Ihab Awad293edf22014-07-24 17:52:29 -0700411 }
412
Evan Charlton105d9772014-11-25 14:08:53 -0800413 /**
Tony Mak240656f2015-12-04 11:36:22 +0000414 * If it is a outgoing call, sim call manager of call-initiating user is returned.
415 * Otherwise, we return the sim call manager of the user associated with the
416 * target phone account.
417 * @return phone account handle of sim call manager based on the ongoing call.
418 */
419 public PhoneAccountHandle getSimCallManagerFromCall(Call call) {
420 if (call == null) {
421 return null;
422 }
423 UserHandle userHandle = call.getInitiatingUser();
424 if (userHandle == null) {
425 userHandle = call.getTargetPhoneAccount().getUserHandle();
426 }
427 return getSimCallManager(userHandle);
428 }
429
430 /**
Evan Charlton105d9772014-11-25 14:08:53 -0800431 * Update the current UserHandle to track when users are switched. This will allow the
432 * PhoneAccountRegistar to self-filter the PhoneAccounts to make sure we don't leak anything
433 * across users.
Santos Cordon6a212642015-05-08 16:35:23 -0700434 * We cannot simply check the calling user because that would always return the primary user for
435 * all invocations originating with the system process.
Evan Charlton105d9772014-11-25 14:08:53 -0800436 *
437 * @param userHandle The {@link UserHandle}, as delivered by
438 * {@link Intent#ACTION_USER_SWITCHED}.
439 */
440 public void setCurrentUserHandle(UserHandle userHandle) {
441 if (userHandle == null) {
442 Log.d(this, "setCurrentUserHandle, userHandle = null");
443 userHandle = Process.myUserHandle();
444 }
445 Log.d(this, "setCurrentUserHandle, %s", userHandle);
446 mCurrentUserHandle = userHandle;
447 }
448
Yorke Lee71734c22015-06-02 14:22:56 -0700449 /**
450 * @return {@code true} if the phone account was successfully enabled/disabled, {@code false}
451 * otherwise.
452 */
453 public boolean enablePhoneAccount(PhoneAccountHandle accountHandle, boolean isEnabled) {
Tony Mak98e6bdc2015-11-25 21:54:05 +0000454 PhoneAccount account = getPhoneAccountUnchecked(accountHandle);
Hall Liu518a7dd2017-01-26 14:20:04 -0800455 Log.i(this, "Phone account %s %s.", accountHandle, isEnabled ? "enabled" : "disabled");
Santos Cordon638f05c2015-05-19 17:12:33 -0700456 if (account == null) {
457 Log.w(this, "Could not find account to enable: " + accountHandle);
Yorke Lee71734c22015-06-02 14:22:56 -0700458 return false;
Santos Cordon638f05c2015-05-19 17:12:33 -0700459 } else if (account.hasCapabilities(PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION)) {
Santos Cordonea5cb932015-05-07 16:28:38 -0700460 // We never change the enabled state of SIM-based accounts.
Santos Cordon638f05c2015-05-19 17:12:33 -0700461 Log.w(this, "Could not change enable state of SIM account: " + accountHandle);
Yorke Lee71734c22015-06-02 14:22:56 -0700462 return false;
Santos Cordonea5cb932015-05-07 16:28:38 -0700463 }
464
Santos Cordon638f05c2015-05-19 17:12:33 -0700465 if (account.isEnabled() != isEnabled) {
Santos Cordonea5cb932015-05-07 16:28:38 -0700466 account.setIsEnabled(isEnabled);
Tony Mak48367162016-01-15 22:58:18 +0000467 if (!isEnabled) {
468 // If the disabled account is the default, remove it.
469 removeDefaultPhoneAccountHandle(accountHandle);
470 }
Santos Cordonea5cb932015-05-07 16:28:38 -0700471 write();
472 fireAccountsChanged();
473 }
Yorke Lee71734c22015-06-02 14:22:56 -0700474 return true;
Santos Cordonea5cb932015-05-07 16:28:38 -0700475 }
476
Tony Mak48367162016-01-15 22:58:18 +0000477 private void removeDefaultPhoneAccountHandle(PhoneAccountHandle phoneAccountHandle) {
478 Iterator<Map.Entry<UserHandle, DefaultPhoneAccountHandle>> iterator =
479 mState.defaultOutgoingAccountHandles.entrySet().iterator();
480 while (iterator.hasNext()) {
481 Map.Entry<UserHandle, DefaultPhoneAccountHandle> entry = iterator.next();
482 if (phoneAccountHandle.equals(entry.getValue().phoneAccountHandle)) {
483 iterator.remove();
484 }
485 }
486 }
487
Tony Mak240656f2015-12-04 11:36:22 +0000488 private boolean isVisibleForUser(PhoneAccount account, UserHandle userHandle,
489 boolean acrossProfiles) {
Evan Charlton105d9772014-11-25 14:08:53 -0800490 if (account == null) {
491 return false;
492 }
493
Tony Mak240656f2015-12-04 11:36:22 +0000494 if (userHandle == null) {
495 Log.w(this, "userHandle is null in isVisibleForUser");
496 return false;
497 }
498
Evan Charlton105d9772014-11-25 14:08:53 -0800499 // If this PhoneAccount has CAPABILITY_MULTI_USER, it should be visible to all users and
500 // all profiles. Only Telephony and SIP accounts should have this capability.
501 if (account.hasCapabilities(PhoneAccount.CAPABILITY_MULTI_USER)) {
502 return true;
503 }
504
505 UserHandle phoneAccountUserHandle = account.getAccountHandle().getUserHandle();
506 if (phoneAccountUserHandle == null) {
507 return false;
508 }
509
510 if (mCurrentUserHandle == null) {
Tony Mak240656f2015-12-04 11:36:22 +0000511 // In case we need to have emergency phone calls from the lock screen.
Evan Charlton105d9772014-11-25 14:08:53 -0800512 Log.d(this, "Current user is null; assuming true");
513 return true;
514 }
515
Tony Mak240656f2015-12-04 11:36:22 +0000516 if (acrossProfiles) {
517 return UserManager.get(mContext).isSameProfileGroup(userHandle.getIdentifier(),
518 phoneAccountUserHandle.getIdentifier());
519 } else {
520 return phoneAccountUserHandle.equals(userHandle);
Sailesh Nepal91fc8092015-02-14 15:44:55 -0800521 }
Evan Charlton105d9772014-11-25 14:08:53 -0800522 }
523
524 private List<ResolveInfo> resolveComponent(PhoneAccountHandle phoneAccountHandle) {
525 return resolveComponent(phoneAccountHandle.getComponentName(),
Tony Mak240656f2015-12-04 11:36:22 +0000526 phoneAccountHandle.getUserHandle());
Evan Charlton105d9772014-11-25 14:08:53 -0800527 }
528
529 private List<ResolveInfo> resolveComponent(ComponentName componentName,
530 UserHandle userHandle) {
mike dooley10a58312014-11-06 13:46:19 -0800531 PackageManager pm = mContext.getPackageManager();
532 Intent intent = new Intent(ConnectionService.SERVICE_INTERFACE);
533 intent.setComponent(componentName);
Sailesh Nepal91fc8092015-02-14 15:44:55 -0800534 try {
535 if (userHandle != null) {
536 return pm.queryIntentServicesAsUser(intent, 0, userHandle.getIdentifier());
537 } else {
538 return pm.queryIntentServices(intent, 0);
539 }
540 } catch (SecurityException e) {
Santos Cordon3188b362015-05-22 13:01:10 -0700541 Log.e(this, e, "%s is not visible for the calling user", componentName);
Sailesh Nepal91fc8092015-02-14 15:44:55 -0800542 return Collections.EMPTY_LIST;
Evan Charlton105d9772014-11-25 14:08:53 -0800543 }
mike dooley10a58312014-11-06 13:46:19 -0800544 }
545
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700546 /**
547 * Retrieves a list of all {@link PhoneAccountHandle}s registered.
Santos Cordonea5cb932015-05-07 16:28:38 -0700548 * Only returns accounts which are enabled.
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700549 *
550 * @return The list of {@link PhoneAccountHandle}s.
551 */
Tony Mak240656f2015-12-04 11:36:22 +0000552 public List<PhoneAccountHandle> getAllPhoneAccountHandles(UserHandle userHandle) {
553 return getPhoneAccountHandles(0, null, null, false, userHandle);
Ihab Awad293edf22014-07-24 17:52:29 -0700554 }
555
Tony Mak240656f2015-12-04 11:36:22 +0000556 public List<PhoneAccount> getAllPhoneAccounts(UserHandle userHandle) {
557 return getPhoneAccounts(0, null, null, false, userHandle);
558 }
559
560 public List<PhoneAccount> getAllPhoneAccountsOfCurrentUser() {
561 return getAllPhoneAccounts(mCurrentUserHandle);
Santos Cordonafe59e52014-08-22 16:48:43 -0700562 }
563
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700564 /**
Nancy Chen309198e2014-09-15 18:02:49 -0700565 * Retrieves a list of all phone account call provider phone accounts supporting the
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700566 * specified URI scheme.
567 *
568 * @param uriScheme The URI scheme.
569 * @return The phone account handles.
570 */
Santos Cordonea5cb932015-05-07 16:28:38 -0700571 public List<PhoneAccountHandle> getCallCapablePhoneAccounts(
Tony Mak240656f2015-12-04 11:36:22 +0000572 String uriScheme, boolean includeDisabledAccounts, UserHandle userHandle) {
Santos Cordonea5cb932015-05-07 16:28:38 -0700573 return getPhoneAccountHandles(
Bryce Leea0bb7052015-10-16 13:31:40 -0700574 PhoneAccount.CAPABILITY_CALL_PROVIDER,
575 PhoneAccount.CAPABILITY_EMERGENCY_CALLS_ONLY /*excludedCapabilities*/,
Tony Mak240656f2015-12-04 11:36:22 +0000576 uriScheme, null, includeDisabledAccounts, userHandle);
577 }
578
579 public List<PhoneAccountHandle> getCallCapablePhoneAccountsOfCurrentUser(
580 String uriScheme, boolean includeDisabledAccounts) {
581 return getCallCapablePhoneAccounts(uriScheme, includeDisabledAccounts, mCurrentUserHandle);
Santos Cordon6a212642015-05-08 16:35:23 -0700582 }
583
584 /**
585 * Retrieves a list of all the SIM-based phone accounts.
586 */
Tony Mak240656f2015-12-04 11:36:22 +0000587 public List<PhoneAccountHandle> getSimPhoneAccounts(UserHandle userHandle) {
Santos Cordon6a212642015-05-08 16:35:23 -0700588 return getPhoneAccountHandles(
589 PhoneAccount.CAPABILITY_CALL_PROVIDER | PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION,
Tony Mak240656f2015-12-04 11:36:22 +0000590 null, null, false, userHandle);
Tyler Gunn84253572014-09-02 14:50:05 -0700591 }
592
Tony Mak240656f2015-12-04 11:36:22 +0000593 public List<PhoneAccountHandle> getSimPhoneAccountsOfCurrentUser() {
594 return getSimPhoneAccounts(mCurrentUserHandle);
595 }
596
597 /**
598 * Retrieves a list of all phone accounts registered by a specified package.
599 *
600 * @param packageName The name of the package that registered the phone accounts.
601 * @return The phone account handles.
602 */
603 public List<PhoneAccountHandle> getPhoneAccountsForPackage(String packageName,
604 UserHandle userHandle) {
605 return getPhoneAccountHandles(0, null, packageName, false, userHandle);
Nancy Chen1c5926f2014-09-17 14:44:14 -0700606 }
607
Ihab Awad104f8062014-07-17 11:29:35 -0700608 // TODO: Should we implement an artificial limit for # of accounts associated with a single
609 // ComponentName?
Ihab Awad293edf22014-07-24 17:52:29 -0700610 public void registerPhoneAccount(PhoneAccount account) {
Tyler Gunncb59b672014-08-20 09:02:11 -0700611 // Enforce the requirement that a connection service for a phone account has the correct
612 // permission.
Santos Cordon6a212642015-05-08 16:35:23 -0700613 if (!phoneAccountRequiresBindPermission(account.getAccountHandle())) {
Yorke Lee7bb8ce92015-05-13 16:28:29 -0700614 Log.w(this,
615 "Phone account %s does not have BIND_TELECOM_CONNECTION_SERVICE permission.",
Tyler Gunncb59b672014-08-20 09:02:11 -0700616 account.getAccountHandle());
Yorke Lee7bb8ce92015-05-13 16:28:29 -0700617 throw new SecurityException("PhoneAccount connection service requires "
618 + "BIND_TELECOM_CONNECTION_SERVICE permission.");
Tyler Gunncb59b672014-08-20 09:02:11 -0700619 }
620
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700621 addOrReplacePhoneAccount(account);
622 }
623
624 /**
625 * Adds a {@code PhoneAccount}, replacing an existing one if found.
626 *
627 * @param account The {@code PhoneAccount} to add or replace.
628 */
629 private void addOrReplacePhoneAccount(PhoneAccount account) {
Ihab Awad7e2c7f32014-11-03 09:49:45 -0800630 Log.d(this, "addOrReplacePhoneAccount(%s -> %s)",
631 account.getAccountHandle(), account);
632
Santos Cordonea5cb932015-05-07 16:28:38 -0700633 // Start _enabled_ property as false.
634 // !!! IMPORTANT !!! It is important that we do not read the enabled state that the
635 // source app provides or else an third party app could enable itself.
636 boolean isEnabled = false;
637
Tony Mak98e6bdc2015-11-25 21:54:05 +0000638 PhoneAccount oldAccount = getPhoneAccountUnchecked(account.getAccountHandle());
Santos Cordon6a212642015-05-08 16:35:23 -0700639 if (oldAccount != null) {
640 mState.accounts.remove(oldAccount);
Santos Cordonea5cb932015-05-07 16:28:38 -0700641 isEnabled = oldAccount.isEnabled();
Santos Cordon5fa4e4f2015-06-10 14:56:01 -0700642 Log.i(this, getAccountDiffString(account, oldAccount));
643 } else {
644 Log.i(this, "New phone account registered: " + account);
Santos Cordon176ae282014-07-14 02:02:14 -0700645 }
Santos Cordonea5cb932015-05-07 16:28:38 -0700646
Tyler Gunn2b17f232017-03-08 08:51:00 -0800647 // When registering a self-managed PhoneAccount we enforce the rule that the label that the
648 // app uses is also its phone account label. Also ensure it does not attempt to declare
649 // itself as a sim acct, call manager or call provider.
650 if (account.hasCapabilities(PhoneAccount.CAPABILITY_SELF_MANAGED)) {
651 // Turn off bits we don't want to be able to set (TelecomServiceImpl protects against
652 // this but we'll also prevent it from happening here, just to be safe).
653 int newCapabilities = account.getCapabilities() &
654 ~(PhoneAccount.CAPABILITY_CALL_PROVIDER |
655 PhoneAccount.CAPABILITY_CONNECTION_MANAGER |
656 PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION);
657
658 // Ensure name is correct.
659 CharSequence newLabel = mAppLabelProxy.getAppLabel(
660 account.getAccountHandle().getComponentName().getPackageName());
661
662 account = account.toBuilder()
663 .setLabel(newLabel)
664 .setCapabilities(newCapabilities)
665 .build();
666 }
667
Santos Cordon6a212642015-05-08 16:35:23 -0700668 mState.accounts.add(account);
Brad Ebinger0c87b3c2016-06-10 17:23:02 -0700669 // Set defaults and replace based on the group Id.
670 maybeReplaceOldAccount(account);
Santos Cordonea5cb932015-05-07 16:28:38 -0700671 // Reset enabled state to whatever the value was if the account was already registered,
Tyler Gunnacb3bc82017-01-09 09:43:56 -0800672 // or _true_ if this is a SIM-based account. All SIM-based accounts are always enabled,
673 // as are all self-managed phone accounts.
Santos Cordonea5cb932015-05-07 16:28:38 -0700674 account.setIsEnabled(
Tyler Gunnacb3bc82017-01-09 09:43:56 -0800675 isEnabled || account.hasCapabilities(PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION)
676 || account.hasCapabilities(PhoneAccount.CAPABILITY_SELF_MANAGED));
Santos Cordon176ae282014-07-14 02:02:14 -0700677
Ihab Awad293edf22014-07-24 17:52:29 -0700678 write();
Ihab Awadb78b2762014-07-25 15:16:23 -0700679 fireAccountsChanged();
Ihab Awad293edf22014-07-24 17:52:29 -0700680 }
681
Evan Charlton89176372014-07-19 18:23:09 -0700682 public void unregisterPhoneAccount(PhoneAccountHandle accountHandle) {
Tony Mak98e6bdc2015-11-25 21:54:05 +0000683 PhoneAccount account = getPhoneAccountUnchecked(accountHandle);
Santos Cordon6a212642015-05-08 16:35:23 -0700684 if (account != null) {
685 if (mState.accounts.remove(account)) {
686 write();
687 fireAccountsChanged();
Ihab Awad104f8062014-07-17 11:29:35 -0700688 }
689 }
Santos Cordon176ae282014-07-14 02:02:14 -0700690 }
691
Tyler Gunnd900ce62014-08-13 11:40:59 -0700692 /**
693 * Un-registers all phone accounts associated with a specified package.
694 *
695 * @param packageName The package for which phone accounts will be removed.
Evan Charlton105d9772014-11-25 14:08:53 -0800696 * @param userHandle The {@link UserHandle} the package is running under.
Tyler Gunnd900ce62014-08-13 11:40:59 -0700697 */
Evan Charlton105d9772014-11-25 14:08:53 -0800698 public void clearAccounts(String packageName, UserHandle userHandle) {
Tyler Gunnd900ce62014-08-13 11:40:59 -0700699 boolean accountsRemoved = false;
700 Iterator<PhoneAccount> it = mState.accounts.iterator();
701 while (it.hasNext()) {
702 PhoneAccount phoneAccount = it.next();
Evan Charlton105d9772014-11-25 14:08:53 -0800703 PhoneAccountHandle handle = phoneAccount.getAccountHandle();
704 if (Objects.equals(packageName, handle.getComponentName().getPackageName())
705 && Objects.equals(userHandle, handle.getUserHandle())) {
Tyler Gunnd900ce62014-08-13 11:40:59 -0700706 Log.i(this, "Removing phone account " + phoneAccount.getLabel());
Shriram Ganesh8f89f8d2015-04-15 12:35:43 +0530707 mState.accounts.remove(phoneAccount);
Tyler Gunnd900ce62014-08-13 11:40:59 -0700708 accountsRemoved = true;
Ihab Awad104f8062014-07-17 11:29:35 -0700709 }
710 }
711
Tyler Gunnd900ce62014-08-13 11:40:59 -0700712 if (accountsRemoved) {
713 write();
714 fireAccountsChanged();
715 }
Ihab Awadb78b2762014-07-25 15:16:23 -0700716 }
717
Nancy Chen140004a2014-10-15 15:48:38 -0700718 public boolean isVoiceMailNumber(PhoneAccountHandle accountHandle, String number) {
Wink Saville35850602014-10-23 15:57:21 -0700719 int subId = getSubscriptionIdForPhoneAccount(accountHandle);
Yorke Leeb1957232015-06-30 10:09:54 -0700720 return PhoneNumberUtils.isVoiceMailNumber(mContext, subId, number);
Nancy Chen140004a2014-10-15 15:48:38 -0700721 }
722
Ihab Awadb78b2762014-07-25 15:16:23 -0700723 public void addListener(Listener l) {
724 mListeners.add(l);
725 }
726
727 public void removeListener(Listener l) {
Jay Shraunera82c8f72014-08-14 15:49:16 -0700728 if (l != null) {
729 mListeners.remove(l);
730 }
Ihab Awadb78b2762014-07-25 15:16:23 -0700731 }
732
733 private void fireAccountsChanged() {
734 for (Listener l : mListeners) {
735 l.onAccountsChanged(this);
736 }
737 }
738
739 private void fireDefaultOutgoingChanged() {
740 for (Listener l : mListeners) {
741 l.onDefaultOutgoingChanged(this);
742 }
743 }
744
Santos Cordon5fa4e4f2015-06-10 14:56:01 -0700745 private String getAccountDiffString(PhoneAccount account1, PhoneAccount account2) {
746 if (account1 == null || account2 == null) {
747 return "Diff: " + account1 + ", " + account2;
748 }
749
750 StringBuffer sb = new StringBuffer();
751 sb.append("[").append(account1.getAccountHandle());
Anthony Lee7c64a4e2015-08-25 09:27:47 -0700752 appendDiff(sb, "addr", Log.piiHandle(account1.getAddress()),
753 Log.piiHandle(account2.getAddress()));
Santos Cordon5fa4e4f2015-06-10 14:56:01 -0700754 appendDiff(sb, "cap", account1.getCapabilities(), account2.getCapabilities());
755 appendDiff(sb, "hl", account1.getHighlightColor(), account2.getHighlightColor());
756 appendDiff(sb, "icon", account1.getIcon(), account2.getIcon());
757 appendDiff(sb, "lbl", account1.getLabel(), account2.getLabel());
758 appendDiff(sb, "desc", account1.getShortDescription(), account2.getShortDescription());
Anthony Lee7c64a4e2015-08-25 09:27:47 -0700759 appendDiff(sb, "subAddr", Log.piiHandle(account1.getSubscriptionAddress()),
760 Log.piiHandle(account2.getSubscriptionAddress()));
Santos Cordon5fa4e4f2015-06-10 14:56:01 -0700761 appendDiff(sb, "uris", account1.getSupportedUriSchemes(),
762 account2.getSupportedUriSchemes());
763 sb.append("]");
764 return sb.toString();
765 }
766
767 private void appendDiff(StringBuffer sb, String attrName, Object obj1, Object obj2) {
768 if (!Objects.equals(obj1, obj2)) {
769 sb.append("(")
770 .append(attrName)
771 .append(": ")
772 .append(obj1)
773 .append(" -> ")
774 .append(obj2)
775 .append(")");
776 }
777 }
778
Brad Ebinger0c87b3c2016-06-10 17:23:02 -0700779 private void maybeReplaceOldAccount(PhoneAccount newAccount) {
780 UserHandle newAccountUserHandle = newAccount.getAccountHandle().getUserHandle();
781 DefaultPhoneAccountHandle defaultHandle =
782 getUserSelectedDefaultPhoneAccount(newAccountUserHandle);
783 if (defaultHandle == null || defaultHandle.groupId.isEmpty()) {
784 Log.v(this, "maybeReplaceOldAccount: Not replacing PhoneAccount, no group Id or " +
785 "default.");
786 return;
787 }
788 if (!defaultHandle.groupId.equals(newAccount.getGroupId())) {
789 Log.v(this, "maybeReplaceOldAccount: group Ids are not equal.");
790 return;
791 }
792 if (Objects.equals(newAccount.getAccountHandle().getComponentName(),
793 defaultHandle.phoneAccountHandle.getComponentName())) {
794 // Move default calling account over to new user, since the ComponentNames and Group Ids
795 // are the same.
796 setUserSelectedOutgoingPhoneAccount(newAccount.getAccountHandle(),
797 newAccountUserHandle);
798 } else {
799 Log.v(this, "maybeReplaceOldAccount: group Ids are equal, but ComponentName is not" +
800 " the same as the default. Not replacing default PhoneAccount.");
801 }
802 PhoneAccount replacementAccount = getPhoneAccountByGroupId(newAccount.getGroupId(),
803 newAccount.getAccountHandle().getComponentName(), newAccountUserHandle,
804 newAccount.getAccountHandle());
805 if (replacementAccount != null) {
806 // Unregister the old PhoneAccount.
807 Log.v(this, "maybeReplaceOldAccount: Unregistering old PhoneAccount: " +
808 replacementAccount.getAccountHandle());
809 unregisterPhoneAccount(replacementAccount.getAccountHandle());
810 }
811 }
812
Tyler Gunncb59b672014-08-20 09:02:11 -0700813 /**
Santos Cordon6a212642015-05-08 16:35:23 -0700814 * Determines if the connection service specified by a {@link PhoneAccountHandle} requires the
Yorke Lee7bb8ce92015-05-13 16:28:29 -0700815 * {@link Manifest.permission#BIND_TELECOM_CONNECTION_SERVICE} permission.
Tyler Gunncb59b672014-08-20 09:02:11 -0700816 *
817 * @param phoneAccountHandle The phone account to check.
818 * @return {@code True} if the phone account has permission.
819 */
Santos Cordon6a212642015-05-08 16:35:23 -0700820 public boolean phoneAccountRequiresBindPermission(PhoneAccountHandle phoneAccountHandle) {
Sailesh Nepal91fc8092015-02-14 15:44:55 -0800821 List<ResolveInfo> resolveInfos = resolveComponent(phoneAccountHandle);
822 if (resolveInfos.isEmpty()) {
823 Log.w(this, "phoneAccount %s not found", phoneAccountHandle.getComponentName());
Tyler Gunncb59b672014-08-20 09:02:11 -0700824 return false;
825 }
Sailesh Nepal91fc8092015-02-14 15:44:55 -0800826 for (ResolveInfo resolveInfo : resolveInfos) {
827 ServiceInfo serviceInfo = resolveInfo.serviceInfo;
Yorke Lee7bb8ce92015-05-13 16:28:29 -0700828 if (serviceInfo == null) {
829 return false;
830 }
831
832 if (!Manifest.permission.BIND_CONNECTION_SERVICE.equals(serviceInfo.permission) &&
833 !Manifest.permission.BIND_TELECOM_CONNECTION_SERVICE.equals(
834 serviceInfo.permission)) {
835 // The ConnectionService must require either the deprecated BIND_CONNECTION_SERVICE,
836 // or the public BIND_TELECOM_CONNECTION_SERVICE permissions, both of which are
837 // system/signature only.
Sailesh Nepal91fc8092015-02-14 15:44:55 -0800838 return false;
839 }
840 }
841 return true;
Tyler Gunncb59b672014-08-20 09:02:11 -0700842 }
843
Santos Cordon6a212642015-05-08 16:35:23 -0700844 //
845 // Methods for retrieving PhoneAccounts and PhoneAccountHandles
846 //
Ihab Awad293edf22014-07-24 17:52:29 -0700847
Santos Cordonafe59e52014-08-22 16:48:43 -0700848 /**
Santos Cordon6a212642015-05-08 16:35:23 -0700849 * Returns the PhoneAccount for the specified handle. Does no user checking.
Tyler Gunn84253572014-09-02 14:50:05 -0700850 *
Santos Cordon6a212642015-05-08 16:35:23 -0700851 * @param handle
852 * @return The corresponding phone account if one exists.
Santos Cordonafe59e52014-08-22 16:48:43 -0700853 */
Tony Mak98e6bdc2015-11-25 21:54:05 +0000854 public PhoneAccount getPhoneAccountUnchecked(PhoneAccountHandle handle) {
Santos Cordon6a212642015-05-08 16:35:23 -0700855 for (PhoneAccount m : mState.accounts) {
856 if (Objects.equals(handle, m.getAccountHandle())) {
857 return m;
858 }
859 }
860 return null;
861 }
862
863 /**
864 * Like getPhoneAccount, but checks to see if the current user is allowed to see the phone
865 * account before returning it. The current user is the active user on the actual android
866 * device.
867 */
Tony Mak240656f2015-12-04 11:36:22 +0000868 public PhoneAccount getPhoneAccount(PhoneAccountHandle handle, UserHandle userHandle) {
869 return getPhoneAccount(handle, userHandle, /* acrossProfiles */ false);
870 }
871
872 public PhoneAccount getPhoneAccount(PhoneAccountHandle handle,
873 UserHandle userHandle, boolean acrossProfiles) {
Tony Mak98e6bdc2015-11-25 21:54:05 +0000874 PhoneAccount account = getPhoneAccountUnchecked(handle);
Tony Mak240656f2015-12-04 11:36:22 +0000875 if (account != null && (isVisibleForUser(account, userHandle, acrossProfiles))) {
Santos Cordon6a212642015-05-08 16:35:23 -0700876 return account;
877 }
878 return null;
879 }
880
Tony Mak240656f2015-12-04 11:36:22 +0000881 public PhoneAccount getPhoneAccountOfCurrentUser(PhoneAccountHandle handle) {
882 return getPhoneAccount(handle, mCurrentUserHandle);
883 }
884
Bryce Leea0bb7052015-10-16 13:31:40 -0700885 private List<PhoneAccountHandle> getPhoneAccountHandles(
886 int capabilities,
887 String uriScheme,
888 String packageName,
Tony Mak240656f2015-12-04 11:36:22 +0000889 boolean includeDisabledAccounts,
890 UserHandle userHandle) {
Bryce Leea0bb7052015-10-16 13:31:40 -0700891 return getPhoneAccountHandles(capabilities, 0 /*excludedCapabilities*/, uriScheme,
Tony Mak240656f2015-12-04 11:36:22 +0000892 packageName, includeDisabledAccounts, userHandle);
Bryce Leea0bb7052015-10-16 13:31:40 -0700893 }
894
Santos Cordon6a212642015-05-08 16:35:23 -0700895 /**
896 * Returns a list of phone account handles with the specified capabilities, uri scheme,
897 * and package name.
898 */
899 private List<PhoneAccountHandle> getPhoneAccountHandles(
Santos Cordonea5cb932015-05-07 16:28:38 -0700900 int capabilities,
Bryce Leea0bb7052015-10-16 13:31:40 -0700901 int excludedCapabilities,
Santos Cordonea5cb932015-05-07 16:28:38 -0700902 String uriScheme,
903 String packageName,
Tony Mak240656f2015-12-04 11:36:22 +0000904 boolean includeDisabledAccounts,
905 UserHandle userHandle) {
Santos Cordon6a212642015-05-08 16:35:23 -0700906 List<PhoneAccountHandle> handles = new ArrayList<>();
Santos Cordonea5cb932015-05-07 16:28:38 -0700907
908 for (PhoneAccount account : getPhoneAccounts(
Bryce Leea0bb7052015-10-16 13:31:40 -0700909 capabilities, excludedCapabilities, uriScheme, packageName,
Tony Mak240656f2015-12-04 11:36:22 +0000910 includeDisabledAccounts, userHandle)) {
Santos Cordon6a212642015-05-08 16:35:23 -0700911 handles.add(account.getAccountHandle());
912 }
913 return handles;
Tyler Gunn84253572014-09-02 14:50:05 -0700914 }
915
Bryce Leea0bb7052015-10-16 13:31:40 -0700916 private List<PhoneAccount> getPhoneAccounts(
917 int capabilities,
918 String uriScheme,
919 String packageName,
Tony Mak240656f2015-12-04 11:36:22 +0000920 boolean includeDisabledAccounts,
921 UserHandle userHandle) {
Bryce Leea0bb7052015-10-16 13:31:40 -0700922 return getPhoneAccounts(capabilities, 0 /*excludedCapabilities*/, uriScheme, packageName,
Tony Mak240656f2015-12-04 11:36:22 +0000923 includeDisabledAccounts, userHandle);
Bryce Leea0bb7052015-10-16 13:31:40 -0700924 }
925
Tyler Gunn84253572014-09-02 14:50:05 -0700926 /**
927 * Returns a list of phone account handles with the specified flag, supporting the specified
Santos Cordon6a212642015-05-08 16:35:23 -0700928 * URI scheme, within the specified package name.
Tyler Gunn84253572014-09-02 14:50:05 -0700929 *
Santos Cordon6a212642015-05-08 16:35:23 -0700930 * @param capabilities Capabilities which the {@code PhoneAccount} must have. Ignored if 0.
Bryce Leea0bb7052015-10-16 13:31:40 -0700931 * @param excludedCapabilities Capabilities which the {@code PhoneAccount} must not have.
932 * Ignored if 0.
Santos Cordon6a212642015-05-08 16:35:23 -0700933 * @param uriScheme URI schemes the PhoneAccount must handle. {@code null} bypasses the
Tyler Gunn84253572014-09-02 14:50:05 -0700934 * URI scheme check.
Santos Cordon6a212642015-05-08 16:35:23 -0700935 * @param packageName Package name of the PhoneAccount. {@code null} bypasses packageName check.
Tyler Gunn84253572014-09-02 14:50:05 -0700936 */
Santos Cordon6a212642015-05-08 16:35:23 -0700937 private List<PhoneAccount> getPhoneAccounts(
Santos Cordonea5cb932015-05-07 16:28:38 -0700938 int capabilities,
Bryce Leea0bb7052015-10-16 13:31:40 -0700939 int excludedCapabilities,
Santos Cordonea5cb932015-05-07 16:28:38 -0700940 String uriScheme,
941 String packageName,
Tony Mak240656f2015-12-04 11:36:22 +0000942 boolean includeDisabledAccounts,
943 UserHandle userHandle) {
Santos Cordon6a212642015-05-08 16:35:23 -0700944 List<PhoneAccount> accounts = new ArrayList<>(mState.accounts.size());
Ihab Awad293edf22014-07-24 17:52:29 -0700945 for (PhoneAccount m : mState.accounts) {
Santos Cordonea5cb932015-05-07 16:28:38 -0700946 if (!(m.isEnabled() || includeDisabledAccounts)) {
947 // Do not include disabled accounts.
948 continue;
949 }
950
Bryce Leea0bb7052015-10-16 13:31:40 -0700951 if ((m.getCapabilities() & excludedCapabilities) != 0) {
952 // If an excluded capability is present, skip.
953 continue;
954 }
955
Santos Cordon6a212642015-05-08 16:35:23 -0700956 if (capabilities != 0 && !m.hasCapabilities(capabilities)) {
Evan Charlton105d9772014-11-25 14:08:53 -0800957 // Account doesn't have the right capabilities; skip this one.
958 continue;
Ihab Awadf2a84912014-07-22 21:09:25 -0700959 }
Evan Charlton105d9772014-11-25 14:08:53 -0800960 if (uriScheme != null && !m.supportsUriScheme(uriScheme)) {
961 // Account doesn't support this URI scheme; skip this one.
962 continue;
963 }
Santos Cordon6a212642015-05-08 16:35:23 -0700964 PhoneAccountHandle handle = m.getAccountHandle();
965
966 if (resolveComponent(handle).isEmpty()) {
Evan Charlton105d9772014-11-25 14:08:53 -0800967 // This component cannot be resolved anymore; skip this one.
968 continue;
969 }
Santos Cordon6a212642015-05-08 16:35:23 -0700970 if (packageName != null &&
971 !packageName.equals(handle.getComponentName().getPackageName())) {
972 // Not the right package name; skip this one.
973 continue;
974 }
Tony Mak240656f2015-12-04 11:36:22 +0000975 if (!isVisibleForUser(m, userHandle, false)) {
Evan Charlton105d9772014-11-25 14:08:53 -0800976 // Account is not visible for the current user; skip this one.
977 continue;
978 }
Santos Cordon6a212642015-05-08 16:35:23 -0700979 accounts.add(m);
Ihab Awad104f8062014-07-17 11:29:35 -0700980 }
Santos Cordon6a212642015-05-08 16:35:23 -0700981 return accounts;
Ihab Awad104f8062014-07-17 11:29:35 -0700982 }
983
Santos Cordon6a212642015-05-08 16:35:23 -0700984 //
985 // State Implementation for PhoneAccountRegistrar
986 //
987
Ihab Awad293edf22014-07-24 17:52:29 -0700988 /**
989 * The state of this {@code PhoneAccountRegistrar}.
990 */
Ihab Awadb78b2762014-07-25 15:16:23 -0700991 @VisibleForTesting
992 public static class State {
Ihab Awad293edf22014-07-24 17:52:29 -0700993 /**
Tony Mak4a3e2fd2015-12-04 11:58:38 +0000994 * Store the default phone account handle of users. If no record of a user can be found in
995 * the map, it means that no default phone account handle is set in that user.
Ihab Awad293edf22014-07-24 17:52:29 -0700996 */
Tony Mak4a3e2fd2015-12-04 11:58:38 +0000997 public final Map<UserHandle, DefaultPhoneAccountHandle> defaultOutgoingAccountHandles
998 = new ConcurrentHashMap<>();
Ihab Awad293edf22014-07-24 17:52:29 -0700999
1000 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -07001001 * The complete list of {@code PhoneAccount}s known to the Telecom subsystem.
Ihab Awad293edf22014-07-24 17:52:29 -07001002 */
Shriram Ganesh8f89f8d2015-04-15 12:35:43 +05301003 public final List<PhoneAccount> accounts = new CopyOnWriteArrayList<>();
Tyler Gunn84253572014-09-02 14:50:05 -07001004
1005 /**
1006 * The version number of the State data.
1007 */
1008 public int versionNumber;
Ihab Awad293edf22014-07-24 17:52:29 -07001009 }
1010
Tyler Gunn9787e0e2014-10-14 14:36:12 -07001011 /**
Tony Mak4a3e2fd2015-12-04 11:58:38 +00001012 * The default {@link PhoneAccountHandle} of a user.
1013 */
1014 public static class DefaultPhoneAccountHandle {
1015
1016 public final UserHandle userHandle;
1017
1018 public final PhoneAccountHandle phoneAccountHandle;
1019
Brad Ebinger0c87b3c2016-06-10 17:23:02 -07001020 public final String groupId;
1021
Tony Mak4a3e2fd2015-12-04 11:58:38 +00001022 public DefaultPhoneAccountHandle(UserHandle userHandle,
Brad Ebinger0c87b3c2016-06-10 17:23:02 -07001023 PhoneAccountHandle phoneAccountHandle, String groupId) {
Tony Mak4a3e2fd2015-12-04 11:58:38 +00001024 this.userHandle = userHandle;
1025 this.phoneAccountHandle = phoneAccountHandle;
Brad Ebinger0c87b3c2016-06-10 17:23:02 -07001026 this.groupId = groupId;
Tony Mak4a3e2fd2015-12-04 11:58:38 +00001027 }
1028 }
1029
1030 /**
Tyler Gunn9787e0e2014-10-14 14:36:12 -07001031 * Dumps the state of the {@link CallsManager}.
1032 *
1033 * @param pw The {@code IndentingPrintWriter} to write the state to.
1034 */
1035 public void dump(IndentingPrintWriter pw) {
1036 if (mState != null) {
1037 pw.println("xmlVersion: " + mState.versionNumber);
Tony Mak4a3e2fd2015-12-04 11:58:38 +00001038 DefaultPhoneAccountHandle defaultPhoneAccountHandle
1039 = mState.defaultOutgoingAccountHandles.get(Process.myUserHandle());
1040 pw.println("defaultOutgoing: " + (defaultPhoneAccountHandle == null ? "none" :
1041 defaultPhoneAccountHandle.phoneAccountHandle));
Tony Mak240656f2015-12-04 11:36:22 +00001042 pw.println("simCallManager: " + getSimCallManager(mCurrentUserHandle));
Tyler Gunn9787e0e2014-10-14 14:36:12 -07001043 pw.println("phoneAccounts:");
1044 pw.increaseIndent();
1045 for (PhoneAccount phoneAccount : mState.accounts) {
1046 pw.println(phoneAccount);
1047 }
1048 pw.decreaseIndent();
1049 }
1050 }
1051
Ihab Awad293edf22014-07-24 17:52:29 -07001052 ////////////////////////////////////////////////////////////////////////////////////////////////
1053 //
1054 // State management
1055 //
1056
Hall Liu3f7f64d2016-01-07 17:40:13 -08001057 private class AsyncXmlWriter extends AsyncTask<ByteArrayOutputStream, Void, Void> {
1058 @Override
1059 public Void doInBackground(ByteArrayOutputStream... args) {
1060 final ByteArrayOutputStream buffer = args[0];
1061 FileOutputStream fileOutput = null;
Ihab Awadb78b2762014-07-25 15:16:23 -07001062 try {
Hall Liu3f7f64d2016-01-07 17:40:13 -08001063 synchronized (mWriteLock) {
1064 fileOutput = mAtomicFile.startWrite();
1065 buffer.writeTo(fileOutput);
1066 mAtomicFile.finishWrite(fileOutput);
Ihab Awadb78b2762014-07-25 15:16:23 -07001067 }
Hall Liu3f7f64d2016-01-07 17:40:13 -08001068 } catch (IOException e) {
1069 Log.e(this, e, "Writing state to XML file");
1070 mAtomicFile.failWrite(fileOutput);
Ihab Awadb78b2762014-07-25 15:16:23 -07001071 }
Hall Liu3f7f64d2016-01-07 17:40:13 -08001072 return null;
1073 }
1074 }
1075
1076 private void write() {
1077 try {
1078 ByteArrayOutputStream os = new ByteArrayOutputStream();
1079 XmlSerializer serializer = new FastXmlSerializer();
1080 serializer.setOutput(os, "utf-8");
1081 writeToXml(mState, serializer, mContext);
1082 serializer.flush();
1083 new AsyncXmlWriter().execute(os);
Ihab Awadb78b2762014-07-25 15:16:23 -07001084 } catch (IOException e) {
Hall Liu3f7f64d2016-01-07 17:40:13 -08001085 Log.e(this, e, "Writing state to XML buffer");
Ihab Awad104f8062014-07-17 11:29:35 -07001086 }
1087 }
1088
Ihab Awadb78b2762014-07-25 15:16:23 -07001089 private void read() {
1090 final InputStream is;
Ihab Awad104f8062014-07-17 11:29:35 -07001091 try {
Ihab Awadb78b2762014-07-25 15:16:23 -07001092 is = mAtomicFile.openRead();
1093 } catch (FileNotFoundException ex) {
1094 return;
1095 }
1096
Tyler Gunn84253572014-09-02 14:50:05 -07001097 boolean versionChanged = false;
1098
Ihab Awadb78b2762014-07-25 15:16:23 -07001099 XmlPullParser parser;
1100 try {
1101 parser = Xml.newPullParser();
1102 parser.setInput(new BufferedInputStream(is), null);
1103 parser.nextTag();
Tyler Gunn84253572014-09-02 14:50:05 -07001104 mState = readFromXml(parser, mContext);
1105 versionChanged = mState.versionNumber < EXPECTED_STATE_VERSION;
1106
Ihab Awadb78b2762014-07-25 15:16:23 -07001107 } catch (IOException | XmlPullParserException e) {
1108 Log.e(this, e, "Reading state from XML file");
1109 mState = new State();
1110 } finally {
1111 try {
1112 is.close();
1113 } catch (IOException e) {
1114 Log.e(this, e, "Closing InputStream");
1115 }
Ihab Awad104f8062014-07-17 11:29:35 -07001116 }
Tyler Gunn84253572014-09-02 14:50:05 -07001117
Evan Charlton105d9772014-11-25 14:08:53 -08001118 // Verify all of the UserHandles.
1119 List<PhoneAccount> badAccounts = new ArrayList<>();
1120 for (PhoneAccount phoneAccount : mState.accounts) {
1121 UserHandle userHandle = phoneAccount.getAccountHandle().getUserHandle();
1122 if (userHandle == null) {
1123 Log.w(this, "Missing UserHandle for %s", phoneAccount);
1124 badAccounts.add(phoneAccount);
1125 } else if (mUserManager.getSerialNumberForUser(userHandle) == -1) {
1126 Log.w(this, "User does not exist for %s", phoneAccount);
1127 badAccounts.add(phoneAccount);
1128 }
1129 }
1130 mState.accounts.removeAll(badAccounts);
1131
Tyler Gunn84253572014-09-02 14:50:05 -07001132 // If an upgrade occurred, write out the changed data.
Evan Charlton105d9772014-11-25 14:08:53 -08001133 if (versionChanged || !badAccounts.isEmpty()) {
Tyler Gunn84253572014-09-02 14:50:05 -07001134 write();
1135 }
Santos Cordon176ae282014-07-14 02:02:14 -07001136 }
1137
Evan Charlton105d9772014-11-25 14:08:53 -08001138 private static void writeToXml(State state, XmlSerializer serializer, Context context)
Ihab Awadb78b2762014-07-25 15:16:23 -07001139 throws IOException {
Evan Charlton105d9772014-11-25 14:08:53 -08001140 sStateXml.writeToXml(state, serializer, context);
Santos Cordon176ae282014-07-14 02:02:14 -07001141 }
Ihab Awad104f8062014-07-17 11:29:35 -07001142
Tyler Gunn84253572014-09-02 14:50:05 -07001143 private static State readFromXml(XmlPullParser parser, Context context)
Ihab Awadb78b2762014-07-25 15:16:23 -07001144 throws IOException, XmlPullParserException {
Tyler Gunn84253572014-09-02 14:50:05 -07001145 State s = sStateXml.readFromXml(parser, 0, context);
Ihab Awadb78b2762014-07-25 15:16:23 -07001146 return s != null ? s : new State();
Ihab Awad104f8062014-07-17 11:29:35 -07001147 }
1148
Ihab Awad293edf22014-07-24 17:52:29 -07001149 ////////////////////////////////////////////////////////////////////////////////////////////////
Ihab Awad104f8062014-07-17 11:29:35 -07001150 //
Ihab Awadb78b2762014-07-25 15:16:23 -07001151 // XML serialization
Ihab Awad104f8062014-07-17 11:29:35 -07001152 //
1153
Ihab Awadb78b2762014-07-25 15:16:23 -07001154 @VisibleForTesting
Ihab Awad26923222014-07-30 10:54:35 -07001155 public abstract static class XmlSerialization<T> {
Tyler Gunn369c8742015-10-07 15:39:28 -07001156 private static final String TAG_VALUE = "value";
1157 private static final String ATTRIBUTE_LENGTH = "length";
1158 private static final String ATTRIBUTE_KEY = "key";
1159 private static final String ATTRIBUTE_VALUE_TYPE = "type";
1160 private static final String VALUE_TYPE_STRING = "string";
1161 private static final String VALUE_TYPE_INTEGER = "integer";
1162 private static final String VALUE_TYPE_BOOLEAN = "boolean";
Tyler Gunn84253572014-09-02 14:50:05 -07001163
Ihab Awadb78b2762014-07-25 15:16:23 -07001164 /**
1165 * Write the supplied object to XML
1166 */
Evan Charlton105d9772014-11-25 14:08:53 -08001167 public abstract void writeToXml(T o, XmlSerializer serializer, Context context)
Ihab Awad26923222014-07-30 10:54:35 -07001168 throws IOException;
Ihab Awadb78b2762014-07-25 15:16:23 -07001169
1170 /**
1171 * Read from the supplied XML into a new object, returning null in case of an
1172 * unrecoverable schema mismatch or other data error. 'parser' must be already
1173 * positioned at the first tag that is expected to have been emitted by this
1174 * object's writeToXml(). This object tries to fail early without modifying
1175 * 'parser' if it does not recognize the data it sees.
1176 */
Tyler Gunn84253572014-09-02 14:50:05 -07001177 public abstract T readFromXml(XmlPullParser parser, int version, Context context)
Ihab Awad26923222014-07-30 10:54:35 -07001178 throws IOException, XmlPullParserException;
1179
Ihab Awadd9f54382014-10-24 11:44:47 -07001180 protected void writeTextIfNonNull(String tagName, Object value, XmlSerializer serializer)
Ihab Awad26923222014-07-30 10:54:35 -07001181 throws IOException {
1182 if (value != null) {
1183 serializer.startTag(null, tagName);
1184 serializer.text(Objects.toString(value));
1185 serializer.endTag(null, tagName);
1186 }
1187 }
Tyler Gunn84253572014-09-02 14:50:05 -07001188
1189 /**
1190 * Serializes a string array.
1191 *
1192 * @param tagName The tag name for the string array.
1193 * @param values The string values to serialize.
1194 * @param serializer The serializer.
1195 * @throws IOException
1196 */
1197 protected void writeStringList(String tagName, List<String> values,
1198 XmlSerializer serializer)
1199 throws IOException {
1200
1201 serializer.startTag(null, tagName);
1202 if (values != null) {
Tyler Gunn369c8742015-10-07 15:39:28 -07001203 serializer.attribute(null, ATTRIBUTE_LENGTH, Objects.toString(values.size()));
Tyler Gunn84253572014-09-02 14:50:05 -07001204 for (String toSerialize : values) {
Tyler Gunn369c8742015-10-07 15:39:28 -07001205 serializer.startTag(null, TAG_VALUE);
Tyler Gunn84253572014-09-02 14:50:05 -07001206 if (toSerialize != null ){
1207 serializer.text(toSerialize);
1208 }
Tyler Gunn369c8742015-10-07 15:39:28 -07001209 serializer.endTag(null, TAG_VALUE);
Tyler Gunn84253572014-09-02 14:50:05 -07001210 }
1211 } else {
Tyler Gunn369c8742015-10-07 15:39:28 -07001212 serializer.attribute(null, ATTRIBUTE_LENGTH, "0");
1213 }
1214 serializer.endTag(null, tagName);
1215 }
1216
1217 protected void writeBundle(String tagName, Bundle values, XmlSerializer serializer)
1218 throws IOException {
1219
1220 serializer.startTag(null, tagName);
1221 if (values != null) {
1222 for (String key : values.keySet()) {
1223 Object value = values.get(key);
1224
1225 if (value == null) {
1226 continue;
1227 }
1228
1229 String valueType;
1230 if (value instanceof String) {
1231 valueType = VALUE_TYPE_STRING;
1232 } else if (value instanceof Integer) {
1233 valueType = VALUE_TYPE_INTEGER;
1234 } else if (value instanceof Boolean) {
1235 valueType = VALUE_TYPE_BOOLEAN;
1236 } else {
1237 Log.w(this,
1238 "PhoneAccounts support only string, integer and boolean extras TY.");
1239 continue;
1240 }
1241
1242 serializer.startTag(null, TAG_VALUE);
1243 serializer.attribute(null, ATTRIBUTE_KEY, key);
1244 serializer.attribute(null, ATTRIBUTE_VALUE_TYPE, valueType);
1245 serializer.text(Objects.toString(value));
1246 serializer.endTag(null, TAG_VALUE);
1247 }
Tyler Gunn84253572014-09-02 14:50:05 -07001248 }
1249 serializer.endTag(null, tagName);
Ihab Awadd9f54382014-10-24 11:44:47 -07001250 }
Tyler Gunn84253572014-09-02 14:50:05 -07001251
Santos Cordon9c30c282015-05-13 16:28:27 -07001252 protected void writeIconIfNonNull(String tagName, Icon value, XmlSerializer serializer)
Ihab Awadd9f54382014-10-24 11:44:47 -07001253 throws IOException {
Santos Cordon9c30c282015-05-13 16:28:27 -07001254 if (value != null) {
Ihab Awadd9f54382014-10-24 11:44:47 -07001255 ByteArrayOutputStream stream = new ByteArrayOutputStream();
Santos Cordon9c30c282015-05-13 16:28:27 -07001256 value.writeToStream(stream);
1257 byte[] iconByteArray = stream.toByteArray();
1258 String text = Base64.encodeToString(iconByteArray, 0, iconByteArray.length, 0);
Ihab Awadd9f54382014-10-24 11:44:47 -07001259
1260 serializer.startTag(null, tagName);
1261 serializer.text(text);
1262 serializer.endTag(null, tagName);
1263 }
Tyler Gunn84253572014-09-02 14:50:05 -07001264 }
1265
Evan Charlton105d9772014-11-25 14:08:53 -08001266 protected void writeLong(String tagName, long value, XmlSerializer serializer)
1267 throws IOException {
1268 serializer.startTag(null, tagName);
1269 serializer.text(Long.valueOf(value).toString());
1270 serializer.endTag(null, tagName);
1271 }
1272
Brad Ebinger0c87b3c2016-06-10 17:23:02 -07001273 protected void writeNonNullString(String tagName, String value, XmlSerializer serializer)
1274 throws IOException {
1275 serializer.startTag(null, tagName);
1276 serializer.text(value != null ? value : "");
1277 serializer.endTag(null, tagName);
1278 }
1279
Tyler Gunn84253572014-09-02 14:50:05 -07001280 /**
1281 * Reads a string array from the XML parser.
1282 *
1283 * @param parser The XML parser.
1284 * @return String array containing the parsed values.
1285 * @throws IOException Exception related to IO.
1286 * @throws XmlPullParserException Exception related to parsing.
1287 */
1288 protected List<String> readStringList(XmlPullParser parser)
1289 throws IOException, XmlPullParserException {
1290
Tyler Gunn369c8742015-10-07 15:39:28 -07001291 int length = Integer.parseInt(parser.getAttributeValue(null, ATTRIBUTE_LENGTH));
Tyler Gunn84253572014-09-02 14:50:05 -07001292 List<String> arrayEntries = new ArrayList<String>(length);
1293 String value = null;
1294
1295 if (length == 0) {
1296 return arrayEntries;
1297 }
1298
1299 int outerDepth = parser.getDepth();
1300 while (XmlUtils.nextElementWithin(parser, outerDepth)) {
Tyler Gunn369c8742015-10-07 15:39:28 -07001301 if (parser.getName().equals(TAG_VALUE)) {
Tyler Gunn8e0fef42014-09-08 18:34:44 -07001302 parser.next();
Tyler Gunn84253572014-09-02 14:50:05 -07001303 value = parser.getText();
1304 arrayEntries.add(value);
1305 }
1306 }
1307
1308 return arrayEntries;
1309 }
Ihab Awadd9f54382014-10-24 11:44:47 -07001310
Tyler Gunn369c8742015-10-07 15:39:28 -07001311 /**
1312 * Reads a bundle from the XML parser.
1313 *
1314 * @param parser The XML parser.
1315 * @return Bundle containing the parsed values.
1316 * @throws IOException Exception related to IO.
1317 * @throws XmlPullParserException Exception related to parsing.
1318 */
1319 protected Bundle readBundle(XmlPullParser parser)
1320 throws IOException, XmlPullParserException {
1321
1322 Bundle bundle = null;
1323 int outerDepth = parser.getDepth();
1324 while (XmlUtils.nextElementWithin(parser, outerDepth)) {
1325 if (parser.getName().equals(TAG_VALUE)) {
1326 String valueType = parser.getAttributeValue(null, ATTRIBUTE_VALUE_TYPE);
1327 String key = parser.getAttributeValue(null, ATTRIBUTE_KEY);
1328 parser.next();
1329 String value = parser.getText();
1330
1331 if (bundle == null) {
1332 bundle = new Bundle();
1333 }
1334
1335 // Do not write null values to the bundle.
1336 if (value == null) {
1337 continue;
1338 }
1339
1340 if (VALUE_TYPE_STRING.equals(valueType)) {
1341 bundle.putString(key, value);
1342 } else if (VALUE_TYPE_INTEGER.equals(valueType)) {
1343 try {
1344 int intValue = Integer.parseInt(value);
1345 bundle.putInt(key, intValue);
1346 } catch (NumberFormatException nfe) {
1347 Log.w(this, "Invalid integer PhoneAccount extra.");
1348 }
1349 } else if (VALUE_TYPE_BOOLEAN.equals(valueType)) {
1350 boolean boolValue = Boolean.parseBoolean(value);
1351 bundle.putBoolean(key, boolValue);
1352 } else {
1353 Log.w(this, "Invalid type " + valueType + " for PhoneAccount bundle.");
1354 }
1355 }
1356 }
1357 return bundle;
1358 }
1359
Santos Cordon9c30c282015-05-13 16:28:27 -07001360 protected Bitmap readBitmap(XmlPullParser parser) {
Ihab Awadd9f54382014-10-24 11:44:47 -07001361 byte[] imageByteArray = Base64.decode(parser.getText(), 0);
1362 return BitmapFactory.decodeByteArray(imageByteArray, 0, imageByteArray.length);
1363 }
Santos Cordon9c30c282015-05-13 16:28:27 -07001364
1365 protected Icon readIcon(XmlPullParser parser) throws IOException {
1366 byte[] iconByteArray = Base64.decode(parser.getText(), 0);
1367 ByteArrayInputStream stream = new ByteArrayInputStream(iconByteArray);
1368 return Icon.createFromStream(stream);
1369 }
Ihab Awad104f8062014-07-17 11:29:35 -07001370 }
1371
Ihab Awadb78b2762014-07-25 15:16:23 -07001372 @VisibleForTesting
1373 public static final XmlSerialization<State> sStateXml =
1374 new XmlSerialization<State>() {
1375 private static final String CLASS_STATE = "phone_account_registrar_state";
Ihab Awad104f8062014-07-17 11:29:35 -07001376 private static final String DEFAULT_OUTGOING = "default_outgoing";
1377 private static final String ACCOUNTS = "accounts";
Tyler Gunn84253572014-09-02 14:50:05 -07001378 private static final String VERSION = "version";
Ihab Awad104f8062014-07-17 11:29:35 -07001379
1380 @Override
Evan Charlton105d9772014-11-25 14:08:53 -08001381 public void writeToXml(State o, XmlSerializer serializer, Context context)
Ihab Awadb78b2762014-07-25 15:16:23 -07001382 throws IOException {
Ihab Awad26923222014-07-30 10:54:35 -07001383 if (o != null) {
1384 serializer.startTag(null, CLASS_STATE);
Tyler Gunn84253572014-09-02 14:50:05 -07001385 serializer.attribute(null, VERSION, Objects.toString(EXPECTED_STATE_VERSION));
Ihab Awadb78b2762014-07-25 15:16:23 -07001386
Tony Mak4a3e2fd2015-12-04 11:58:38 +00001387 serializer.startTag(null, DEFAULT_OUTGOING);
1388 for (DefaultPhoneAccountHandle defaultPhoneAccountHandle : o
1389 .defaultOutgoingAccountHandles.values()) {
1390 sDefaultPhoneAcountHandleXml
1391 .writeToXml(defaultPhoneAccountHandle, serializer, context);
Ihab Awad26923222014-07-30 10:54:35 -07001392 }
Tony Mak4a3e2fd2015-12-04 11:58:38 +00001393 serializer.endTag(null, DEFAULT_OUTGOING);
Ihab Awad26923222014-07-30 10:54:35 -07001394
Ihab Awad26923222014-07-30 10:54:35 -07001395 serializer.startTag(null, ACCOUNTS);
1396 for (PhoneAccount m : o.accounts) {
Evan Charlton105d9772014-11-25 14:08:53 -08001397 sPhoneAccountXml.writeToXml(m, serializer, context);
Ihab Awad26923222014-07-30 10:54:35 -07001398 }
1399 serializer.endTag(null, ACCOUNTS);
1400
1401 serializer.endTag(null, CLASS_STATE);
Ihab Awad293edf22014-07-24 17:52:29 -07001402 }
Ihab Awad104f8062014-07-17 11:29:35 -07001403 }
1404
1405 @Override
Tyler Gunn84253572014-09-02 14:50:05 -07001406 public State readFromXml(XmlPullParser parser, int version, Context context)
Ihab Awadb78b2762014-07-25 15:16:23 -07001407 throws IOException, XmlPullParserException {
1408 if (parser.getName().equals(CLASS_STATE)) {
1409 State s = new State();
Tyler Gunn84253572014-09-02 14:50:05 -07001410
1411 String rawVersion = parser.getAttributeValue(null, VERSION);
Tony Mak4a3e2fd2015-12-04 11:58:38 +00001412 s.versionNumber = TextUtils.isEmpty(rawVersion) ? 1 : Integer.parseInt(rawVersion);
Tyler Gunn84253572014-09-02 14:50:05 -07001413
Ihab Awadb78b2762014-07-25 15:16:23 -07001414 int outerDepth = parser.getDepth();
1415 while (XmlUtils.nextElementWithin(parser, outerDepth)) {
1416 if (parser.getName().equals(DEFAULT_OUTGOING)) {
Tony Mak4a3e2fd2015-12-04 11:58:38 +00001417 if (s.versionNumber < 9) {
Brad Ebinger0c87b3c2016-06-10 17:23:02 -07001418 // Migrate old default phone account handle here by assuming the
1419 // default phone account handle belongs to the primary user. Also,
1420 // assume there are no groups.
Tony Mak4a3e2fd2015-12-04 11:58:38 +00001421 parser.nextTag();
1422 PhoneAccountHandle phoneAccountHandle = sPhoneAccountHandleXml
1423 .readFromXml(parser, s.versionNumber, context);
1424 UserManager userManager = UserManager.get(context);
1425 UserInfo primaryUser = userManager.getPrimaryUser();
1426 if (primaryUser != null) {
1427 UserHandle userHandle = primaryUser.getUserHandle();
1428 DefaultPhoneAccountHandle defaultPhoneAccountHandle
1429 = new DefaultPhoneAccountHandle(userHandle,
Brad Ebinger0c87b3c2016-06-10 17:23:02 -07001430 phoneAccountHandle, "" /* groupId */);
Tony Mak4a3e2fd2015-12-04 11:58:38 +00001431 s.defaultOutgoingAccountHandles
1432 .put(userHandle, defaultPhoneAccountHandle);
1433 }
1434 } else {
1435 int defaultAccountHandlesDepth = parser.getDepth();
1436 while (XmlUtils.nextElementWithin(parser, defaultAccountHandlesDepth)) {
1437 DefaultPhoneAccountHandle accountHandle
1438 = sDefaultPhoneAcountHandleXml
1439 .readFromXml(parser, s.versionNumber, context);
1440 if (accountHandle != null && s.accounts != null) {
1441 s.defaultOutgoingAccountHandles
1442 .put(accountHandle.userHandle, accountHandle);
1443 }
1444 }
1445 }
Ihab Awadb78b2762014-07-25 15:16:23 -07001446 } else if (parser.getName().equals(ACCOUNTS)) {
1447 int accountsDepth = parser.getDepth();
1448 while (XmlUtils.nextElementWithin(parser, accountsDepth)) {
Tyler Gunn84253572014-09-02 14:50:05 -07001449 PhoneAccount account = sPhoneAccountXml.readFromXml(parser,
1450 s.versionNumber, context);
1451
1452 if (account != null && s.accounts != null) {
Ihab Awadb78b2762014-07-25 15:16:23 -07001453 s.accounts.add(account);
1454 }
1455 }
Ihab Awad104f8062014-07-17 11:29:35 -07001456 }
1457 }
Ihab Awadb78b2762014-07-25 15:16:23 -07001458 return s;
Ihab Awad104f8062014-07-17 11:29:35 -07001459 }
Ihab Awadb78b2762014-07-25 15:16:23 -07001460 return null;
Ihab Awad104f8062014-07-17 11:29:35 -07001461 }
1462 };
1463
Ihab Awadb78b2762014-07-25 15:16:23 -07001464 @VisibleForTesting
Tony Mak4a3e2fd2015-12-04 11:58:38 +00001465 public static final XmlSerialization<DefaultPhoneAccountHandle> sDefaultPhoneAcountHandleXml =
1466 new XmlSerialization<DefaultPhoneAccountHandle>() {
1467 private static final String CLASS_DEFAULT_OUTGOING_PHONE_ACCOUNT_HANDLE
1468 = "default_outgoing_phone_account_handle";
1469 private static final String USER_SERIAL_NUMBER = "user_serial_number";
Brad Ebinger0c87b3c2016-06-10 17:23:02 -07001470 private static final String GROUP_ID = "group_id";
Tony Mak4a3e2fd2015-12-04 11:58:38 +00001471 private static final String ACCOUNT_HANDLE = "account_handle";
1472
1473 @Override
1474 public void writeToXml(DefaultPhoneAccountHandle o, XmlSerializer serializer,
1475 Context context) throws IOException {
1476 if (o != null) {
1477 final UserManager userManager = UserManager.get(context);
1478 final long serialNumber = userManager.getSerialNumberForUser(o.userHandle);
1479 if (serialNumber != -1) {
1480 serializer.startTag(null, CLASS_DEFAULT_OUTGOING_PHONE_ACCOUNT_HANDLE);
1481 writeLong(USER_SERIAL_NUMBER, serialNumber, serializer);
Brad Ebinger0c87b3c2016-06-10 17:23:02 -07001482 writeNonNullString(GROUP_ID, o.groupId, serializer);
Tony Mak4a3e2fd2015-12-04 11:58:38 +00001483 serializer.startTag(null, ACCOUNT_HANDLE);
1484 sPhoneAccountHandleXml.writeToXml(o.phoneAccountHandle, serializer,
1485 context);
1486 serializer.endTag(null, ACCOUNT_HANDLE);
1487 serializer.endTag(null, CLASS_DEFAULT_OUTGOING_PHONE_ACCOUNT_HANDLE);
1488 }
1489 }
1490 }
1491
1492 @Override
1493 public DefaultPhoneAccountHandle readFromXml(XmlPullParser parser, int version,
1494 Context context)
1495 throws IOException, XmlPullParserException {
1496 if (parser.getName().equals(CLASS_DEFAULT_OUTGOING_PHONE_ACCOUNT_HANDLE)) {
1497 int outerDepth = parser.getDepth();
1498 PhoneAccountHandle accountHandle = null;
1499 String userSerialNumberString = null;
Brad Ebinger0c87b3c2016-06-10 17:23:02 -07001500 String groupId = "";
Tony Mak4a3e2fd2015-12-04 11:58:38 +00001501 while (XmlUtils.nextElementWithin(parser, outerDepth)) {
1502 if (parser.getName().equals(ACCOUNT_HANDLE)) {
1503 parser.nextTag();
1504 accountHandle = sPhoneAccountHandleXml.readFromXml(parser, version,
1505 context);
1506 } else if (parser.getName().equals(USER_SERIAL_NUMBER)) {
1507 parser.next();
1508 userSerialNumberString = parser.getText();
Brad Ebinger0c87b3c2016-06-10 17:23:02 -07001509 } else if (parser.getName().equals(GROUP_ID)) {
Hall Liuded880c2017-01-20 16:13:07 -08001510 if (parser.next() == XmlPullParser.TEXT) {
1511 groupId = parser.getText();
1512 }
Tony Mak4a3e2fd2015-12-04 11:58:38 +00001513 }
1514 }
1515 UserHandle userHandle = null;
1516 if (userSerialNumberString != null) {
1517 try {
1518 long serialNumber = Long.parseLong(userSerialNumberString);
1519 userHandle = UserManager.get(context)
1520 .getUserForSerialNumber(serialNumber);
1521 } catch (NumberFormatException e) {
1522 Log.e(this, e,
1523 "Could not parse UserHandle " + userSerialNumberString);
1524 }
1525 }
Brad Ebinger0c87b3c2016-06-10 17:23:02 -07001526 if (accountHandle != null && userHandle != null && groupId != null) {
1527 return new DefaultPhoneAccountHandle(userHandle, accountHandle,
1528 groupId);
Tony Mak4a3e2fd2015-12-04 11:58:38 +00001529 }
1530 }
1531 return null;
1532 }
1533 };
1534
1535
1536 @VisibleForTesting
Ihab Awadb78b2762014-07-25 15:16:23 -07001537 public static final XmlSerialization<PhoneAccount> sPhoneAccountXml =
1538 new XmlSerialization<PhoneAccount>() {
1539 private static final String CLASS_PHONE_ACCOUNT = "phone_account";
1540 private static final String ACCOUNT_HANDLE = "account_handle";
Andrew Lee7129f1c2014-09-04 11:55:07 -07001541 private static final String ADDRESS = "handle";
1542 private static final String SUBSCRIPTION_ADDRESS = "subscription_number";
Ihab Awad104f8062014-07-17 11:29:35 -07001543 private static final String CAPABILITIES = "capabilities";
Christine Hallstromffe558c2016-11-30 16:05:13 -08001544 private static final String SUPPORTED_AUDIO_ROUTES = "supported_audio_routes";
Ihab Awad104f8062014-07-17 11:29:35 -07001545 private static final String ICON_RES_ID = "icon_res_id";
Ihab Awadd9f54382014-10-24 11:44:47 -07001546 private static final String ICON_PACKAGE_NAME = "icon_package_name";
1547 private static final String ICON_BITMAP = "icon_bitmap";
Ihab Awad7e2c7f32014-11-03 09:49:45 -08001548 private static final String ICON_TINT = "icon_tint";
1549 private static final String HIGHLIGHT_COLOR = "highlight_color";
Ihab Awad104f8062014-07-17 11:29:35 -07001550 private static final String LABEL = "label";
1551 private static final String SHORT_DESCRIPTION = "short_description";
Tyler Gunn84253572014-09-02 14:50:05 -07001552 private static final String SUPPORTED_URI_SCHEMES = "supported_uri_schemes";
Santos Cordon9c30c282015-05-13 16:28:27 -07001553 private static final String ICON = "icon";
Tyler Gunn369c8742015-10-07 15:39:28 -07001554 private static final String EXTRAS = "extras";
Santos Cordonea5cb932015-05-07 16:28:38 -07001555 private static final String ENABLED = "enabled";
Ihab Awad104f8062014-07-17 11:29:35 -07001556
1557 @Override
Evan Charlton105d9772014-11-25 14:08:53 -08001558 public void writeToXml(PhoneAccount o, XmlSerializer serializer, Context context)
Ihab Awadb78b2762014-07-25 15:16:23 -07001559 throws IOException {
Ihab Awad26923222014-07-30 10:54:35 -07001560 if (o != null) {
1561 serializer.startTag(null, CLASS_PHONE_ACCOUNT);
Ihab Awadb78b2762014-07-25 15:16:23 -07001562
Ihab Awad26923222014-07-30 10:54:35 -07001563 if (o.getAccountHandle() != null) {
1564 serializer.startTag(null, ACCOUNT_HANDLE);
Evan Charlton105d9772014-11-25 14:08:53 -08001565 sPhoneAccountHandleXml.writeToXml(o.getAccountHandle(), serializer, context);
Ihab Awad26923222014-07-30 10:54:35 -07001566 serializer.endTag(null, ACCOUNT_HANDLE);
1567 }
Ihab Awadb78b2762014-07-25 15:16:23 -07001568
Ihab Awadd9f54382014-10-24 11:44:47 -07001569 writeTextIfNonNull(ADDRESS, o.getAddress(), serializer);
1570 writeTextIfNonNull(SUBSCRIPTION_ADDRESS, o.getSubscriptionAddress(), serializer);
1571 writeTextIfNonNull(CAPABILITIES, Integer.toString(o.getCapabilities()), serializer);
Santos Cordon9c30c282015-05-13 16:28:27 -07001572 writeIconIfNonNull(ICON, o.getIcon(), serializer);
Ihab Awad7e2c7f32014-11-03 09:49:45 -08001573 writeTextIfNonNull(HIGHLIGHT_COLOR,
1574 Integer.toString(o.getHighlightColor()), serializer);
Ihab Awadd9f54382014-10-24 11:44:47 -07001575 writeTextIfNonNull(LABEL, o.getLabel(), serializer);
1576 writeTextIfNonNull(SHORT_DESCRIPTION, o.getShortDescription(), serializer);
Tyler Gunn84253572014-09-02 14:50:05 -07001577 writeStringList(SUPPORTED_URI_SCHEMES, o.getSupportedUriSchemes(), serializer);
Tyler Gunn369c8742015-10-07 15:39:28 -07001578 writeBundle(EXTRAS, o.getExtras(), serializer);
Santos Cordonea5cb932015-05-07 16:28:38 -07001579 writeTextIfNonNull(ENABLED, o.isEnabled() ? "true" : "false" , serializer);
Christine Hallstromffe558c2016-11-30 16:05:13 -08001580 writeTextIfNonNull(SUPPORTED_AUDIO_ROUTES, Integer.toString(
1581 o.getSupportedAudioRoutes()), serializer);
Ihab Awadb78b2762014-07-25 15:16:23 -07001582
Ihab Awad26923222014-07-30 10:54:35 -07001583 serializer.endTag(null, CLASS_PHONE_ACCOUNT);
1584 }
Ihab Awad104f8062014-07-17 11:29:35 -07001585 }
1586
Tyler Gunn84253572014-09-02 14:50:05 -07001587 public PhoneAccount readFromXml(XmlPullParser parser, int version, Context context)
Ihab Awadb78b2762014-07-25 15:16:23 -07001588 throws IOException, XmlPullParserException {
1589 if (parser.getName().equals(CLASS_PHONE_ACCOUNT)) {
1590 int outerDepth = parser.getDepth();
1591 PhoneAccountHandle accountHandle = null;
Andrew Lee7129f1c2014-09-04 11:55:07 -07001592 Uri address = null;
1593 Uri subscriptionAddress = null;
Ihab Awadb78b2762014-07-25 15:16:23 -07001594 int capabilities = 0;
Christine Hallstromffe558c2016-11-30 16:05:13 -08001595 int supportedAudioRoutes = 0;
Ihab Awad7e2c7f32014-11-03 09:49:45 -08001596 int iconResId = PhoneAccount.NO_RESOURCE_ID;
Ihab Awadd9f54382014-10-24 11:44:47 -07001597 String iconPackageName = null;
Ihab Awad7e2c7f32014-11-03 09:49:45 -08001598 Bitmap iconBitmap = null;
Ihab Awad07bc5ee2014-11-12 13:42:52 -08001599 int iconTint = PhoneAccount.NO_ICON_TINT;
1600 int highlightColor = PhoneAccount.NO_HIGHLIGHT_COLOR;
Ihab Awadb78b2762014-07-25 15:16:23 -07001601 String label = null;
1602 String shortDescription = null;
Tyler Gunn84253572014-09-02 14:50:05 -07001603 List<String> supportedUriSchemes = null;
Santos Cordon9c30c282015-05-13 16:28:27 -07001604 Icon icon = null;
Santos Cordonea5cb932015-05-07 16:28:38 -07001605 boolean enabled = false;
Tyler Gunn369c8742015-10-07 15:39:28 -07001606 Bundle extras = null;
Ihab Awadb78b2762014-07-25 15:16:23 -07001607
1608 while (XmlUtils.nextElementWithin(parser, outerDepth)) {
1609 if (parser.getName().equals(ACCOUNT_HANDLE)) {
1610 parser.nextTag();
Tyler Gunn84253572014-09-02 14:50:05 -07001611 accountHandle = sPhoneAccountHandleXml.readFromXml(parser, version,
1612 context);
Andrew Lee7129f1c2014-09-04 11:55:07 -07001613 } else if (parser.getName().equals(ADDRESS)) {
Ihab Awadb78b2762014-07-25 15:16:23 -07001614 parser.next();
Andrew Lee7129f1c2014-09-04 11:55:07 -07001615 address = Uri.parse(parser.getText());
1616 } else if (parser.getName().equals(SUBSCRIPTION_ADDRESS)) {
Ihab Awadb78b2762014-07-25 15:16:23 -07001617 parser.next();
Andrew Lee7129f1c2014-09-04 11:55:07 -07001618 String nextText = parser.getText();
1619 subscriptionAddress = nextText == null ? null : Uri.parse(nextText);
Ihab Awadb78b2762014-07-25 15:16:23 -07001620 } else if (parser.getName().equals(CAPABILITIES)) {
1621 parser.next();
1622 capabilities = Integer.parseInt(parser.getText());
1623 } else if (parser.getName().equals(ICON_RES_ID)) {
1624 parser.next();
1625 iconResId = Integer.parseInt(parser.getText());
Ihab Awadd9f54382014-10-24 11:44:47 -07001626 } else if (parser.getName().equals(ICON_PACKAGE_NAME)) {
1627 parser.next();
1628 iconPackageName = parser.getText();
1629 } else if (parser.getName().equals(ICON_BITMAP)) {
1630 parser.next();
Ihab Awad7e2c7f32014-11-03 09:49:45 -08001631 iconBitmap = readBitmap(parser);
1632 } else if (parser.getName().equals(ICON_TINT)) {
Nancy Chen06ce0622014-10-23 01:17:35 +00001633 parser.next();
Ihab Awad7e2c7f32014-11-03 09:49:45 -08001634 iconTint = Integer.parseInt(parser.getText());
1635 } else if (parser.getName().equals(HIGHLIGHT_COLOR)) {
1636 parser.next();
1637 highlightColor = Integer.parseInt(parser.getText());
Ihab Awadb78b2762014-07-25 15:16:23 -07001638 } else if (parser.getName().equals(LABEL)) {
1639 parser.next();
1640 label = parser.getText();
1641 } else if (parser.getName().equals(SHORT_DESCRIPTION)) {
1642 parser.next();
1643 shortDescription = parser.getText();
Tyler Gunn84253572014-09-02 14:50:05 -07001644 } else if (parser.getName().equals(SUPPORTED_URI_SCHEMES)) {
1645 supportedUriSchemes = readStringList(parser);
Santos Cordon9c30c282015-05-13 16:28:27 -07001646 } else if (parser.getName().equals(ICON)) {
1647 parser.next();
1648 icon = readIcon(parser);
Santos Cordonea5cb932015-05-07 16:28:38 -07001649 } else if (parser.getName().equals(ENABLED)) {
1650 parser.next();
1651 enabled = "true".equalsIgnoreCase(parser.getText());
Tyler Gunn369c8742015-10-07 15:39:28 -07001652 } else if (parser.getName().equals(EXTRAS)) {
Tyler Gunn369c8742015-10-07 15:39:28 -07001653 extras = readBundle(parser);
Christine Hallstromffe558c2016-11-30 16:05:13 -08001654 } else if (parser.getName().equals(SUPPORTED_AUDIO_ROUTES)) {
1655 parser.next();
1656 supportedAudioRoutes = Integer.parseInt(parser.getText());
Ihab Awadb78b2762014-07-25 15:16:23 -07001657 }
1658 }
Tyler Gunn84253572014-09-02 14:50:05 -07001659
Santos Cordona9eebe42015-06-11 14:07:44 -07001660 ComponentName pstnComponentName = new ComponentName("com.android.phone",
1661 "com.android.services.telephony.TelephonyConnectionService");
Santos Cordona82aed82015-05-26 10:43:56 -07001662 ComponentName sipComponentName = new ComponentName("com.android.phone",
1663 "com.android.services.telephony.sip.SipConnectionService");
1664
Tyler Gunn84253572014-09-02 14:50:05 -07001665 // Upgrade older phone accounts to specify the supported URI schemes.
1666 if (version < 2) {
Tyler Gunn84253572014-09-02 14:50:05 -07001667 supportedUriSchemes = new ArrayList<>();
1668
1669 // Handle the SIP connection service.
1670 // Check the system settings to see if it also should handle "tel" calls.
1671 if (accountHandle.getComponentName().equals(sipComponentName)) {
1672 boolean useSipForPstn = useSipForPstnCalls(context);
1673 supportedUriSchemes.add(PhoneAccount.SCHEME_SIP);
1674 if (useSipForPstn) {
1675 supportedUriSchemes.add(PhoneAccount.SCHEME_TEL);
1676 }
1677 } else {
1678 supportedUriSchemes.add(PhoneAccount.SCHEME_TEL);
1679 supportedUriSchemes.add(PhoneAccount.SCHEME_VOICEMAIL);
1680 }
1681 }
1682
Ihab Awad7e2c7f32014-11-03 09:49:45 -08001683 // Upgrade older phone accounts with explicit package name
1684 if (version < 5) {
1685 if (iconBitmap == null) {
1686 iconPackageName = accountHandle.getComponentName().getPackageName();
1687 }
1688 }
1689
Santos Cordona82aed82015-05-26 10:43:56 -07001690 if (version < 6) {
1691 // Always enable all SIP accounts on upgrade to version 6
1692 if (accountHandle.getComponentName().equals(sipComponentName)) {
1693 enabled = true;
1694 }
1695 }
Santos Cordona9eebe42015-06-11 14:07:44 -07001696 if (version < 7) {
1697 // Always enabled all PSTN acocunts on upgrade to version 7
1698 if (accountHandle.getComponentName().equals(pstnComponentName)) {
1699 enabled = true;
1700 }
1701 }
Roshan Pius6f752c82015-08-10 10:42:06 -07001702 if (version < 8) {
1703 // Migrate the SIP account handle ids to use SIP username instead of SIP URI.
1704 if (accountHandle.getComponentName().equals(sipComponentName)) {
1705 Uri accountUri = Uri.parse(accountHandle.getId());
1706 if (accountUri.getScheme() != null &&
1707 accountUri.getScheme().equals(PhoneAccount.SCHEME_SIP)) {
1708 accountHandle = new PhoneAccountHandle(accountHandle.getComponentName(),
1709 accountUri.getSchemeSpecificPart(),
1710 accountHandle.getUserHandle());
1711 }
1712 }
1713 }
Santos Cordona82aed82015-05-26 10:43:56 -07001714
Christine Hallstromffe558c2016-11-30 16:05:13 -08001715 if (version < 9) {
1716 // Set supported audio routes to all by default
1717 supportedAudioRoutes = CallAudioState.ROUTE_ALL;
1718 }
1719
Ihab Awad7e2c7f32014-11-03 09:49:45 -08001720 PhoneAccount.Builder builder = PhoneAccount.builder(accountHandle, label)
Andrew Lee7129f1c2014-09-04 11:55:07 -07001721 .setAddress(address)
1722 .setSubscriptionAddress(subscriptionAddress)
1723 .setCapabilities(capabilities)
Christine Hallstromffe558c2016-11-30 16:05:13 -08001724 .setSupportedAudioRoutes(supportedAudioRoutes)
Andrew Lee7129f1c2014-09-04 11:55:07 -07001725 .setShortDescription(shortDescription)
1726 .setSupportedUriSchemes(supportedUriSchemes)
Santos Cordonea5cb932015-05-07 16:28:38 -07001727 .setHighlightColor(highlightColor)
Tyler Gunn369c8742015-10-07 15:39:28 -07001728 .setExtras(extras)
Santos Cordonea5cb932015-05-07 16:28:38 -07001729 .setIsEnabled(enabled);
Ihab Awad7e2c7f32014-11-03 09:49:45 -08001730
Santos Cordon9c30c282015-05-13 16:28:27 -07001731 if (icon != null) {
1732 builder.setIcon(icon);
1733 } else if (iconBitmap != null) {
1734 builder.setIcon(Icon.createWithBitmap(iconBitmap));
1735 } else if (!TextUtils.isEmpty(iconPackageName)) {
1736 builder.setIcon(Icon.createWithResource(iconPackageName, iconResId));
1737 // TODO: Need to set tint.
Ihab Awad7e2c7f32014-11-03 09:49:45 -08001738 }
1739
Ihab Awad0a4b95f2015-05-18 10:15:38 -07001740 return builder.build();
Ihab Awadb78b2762014-07-25 15:16:23 -07001741 }
1742 return null;
Ihab Awad104f8062014-07-17 11:29:35 -07001743 }
Tyler Gunn84253572014-09-02 14:50:05 -07001744
1745 /**
Santos Cordon9c30c282015-05-13 16:28:27 -07001746 * Determines if the SIP call settings specify to use SIP for all calls, including PSTN
1747 * calls.
Tyler Gunn84253572014-09-02 14:50:05 -07001748 *
1749 * @param context The context.
1750 * @return {@code True} if SIP should be used for all calls.
1751 */
1752 private boolean useSipForPstnCalls(Context context) {
1753 String option = Settings.System.getString(context.getContentResolver(),
1754 Settings.System.SIP_CALL_OPTIONS);
1755 option = (option != null) ? option : Settings.System.SIP_ADDRESS_ONLY;
1756 return option.equals(Settings.System.SIP_ALWAYS);
1757 }
Ihab Awad104f8062014-07-17 11:29:35 -07001758 };
1759
Ihab Awadb78b2762014-07-25 15:16:23 -07001760 @VisibleForTesting
1761 public static final XmlSerialization<PhoneAccountHandle> sPhoneAccountHandleXml =
1762 new XmlSerialization<PhoneAccountHandle>() {
1763 private static final String CLASS_PHONE_ACCOUNT_HANDLE = "phone_account_handle";
Ihab Awad104f8062014-07-17 11:29:35 -07001764 private static final String COMPONENT_NAME = "component_name";
1765 private static final String ID = "id";
Evan Charlton105d9772014-11-25 14:08:53 -08001766 private static final String USER_SERIAL_NUMBER = "user_serial_number";
Ihab Awad104f8062014-07-17 11:29:35 -07001767
1768 @Override
Evan Charlton105d9772014-11-25 14:08:53 -08001769 public void writeToXml(PhoneAccountHandle o, XmlSerializer serializer, Context context)
Ihab Awadb78b2762014-07-25 15:16:23 -07001770 throws IOException {
Ihab Awad26923222014-07-30 10:54:35 -07001771 if (o != null) {
1772 serializer.startTag(null, CLASS_PHONE_ACCOUNT_HANDLE);
Ihab Awadb78b2762014-07-25 15:16:23 -07001773
Ihab Awad26923222014-07-30 10:54:35 -07001774 if (o.getComponentName() != null) {
Ihab Awadd9f54382014-10-24 11:44:47 -07001775 writeTextIfNonNull(
Ihab Awad26923222014-07-30 10:54:35 -07001776 COMPONENT_NAME, o.getComponentName().flattenToString(), serializer);
1777 }
Ihab Awadb78b2762014-07-25 15:16:23 -07001778
Ihab Awadd9f54382014-10-24 11:44:47 -07001779 writeTextIfNonNull(ID, o.getId(), serializer);
Ihab Awadb78b2762014-07-25 15:16:23 -07001780
Evan Charlton105d9772014-11-25 14:08:53 -08001781 if (o.getUserHandle() != null && context != null) {
1782 UserManager userManager = UserManager.get(context);
1783 writeLong(USER_SERIAL_NUMBER,
1784 userManager.getSerialNumberForUser(o.getUserHandle()), serializer);
1785 }
1786
Ihab Awad26923222014-07-30 10:54:35 -07001787 serializer.endTag(null, CLASS_PHONE_ACCOUNT_HANDLE);
1788 }
Ihab Awad104f8062014-07-17 11:29:35 -07001789 }
1790
1791 @Override
Tyler Gunn84253572014-09-02 14:50:05 -07001792 public PhoneAccountHandle readFromXml(XmlPullParser parser, int version, Context context)
Ihab Awadb78b2762014-07-25 15:16:23 -07001793 throws IOException, XmlPullParserException {
1794 if (parser.getName().equals(CLASS_PHONE_ACCOUNT_HANDLE)) {
1795 String componentNameString = null;
1796 String idString = null;
Evan Charlton105d9772014-11-25 14:08:53 -08001797 String userSerialNumberString = null;
Ihab Awadb78b2762014-07-25 15:16:23 -07001798 int outerDepth = parser.getDepth();
Evan Charlton105d9772014-11-25 14:08:53 -08001799
1800 UserManager userManager = UserManager.get(context);
1801
Ihab Awadb78b2762014-07-25 15:16:23 -07001802 while (XmlUtils.nextElementWithin(parser, outerDepth)) {
1803 if (parser.getName().equals(COMPONENT_NAME)) {
1804 parser.next();
1805 componentNameString = parser.getText();
1806 } else if (parser.getName().equals(ID)) {
1807 parser.next();
1808 idString = parser.getText();
Evan Charlton105d9772014-11-25 14:08:53 -08001809 } else if (parser.getName().equals(USER_SERIAL_NUMBER)) {
1810 parser.next();
1811 userSerialNumberString = parser.getText();
Ihab Awadb78b2762014-07-25 15:16:23 -07001812 }
1813 }
Ihab Awad26923222014-07-30 10:54:35 -07001814 if (componentNameString != null) {
Evan Charlton105d9772014-11-25 14:08:53 -08001815 UserHandle userHandle = null;
1816 if (userSerialNumberString != null) {
1817 try {
1818 long serialNumber = Long.parseLong(userSerialNumberString);
1819 userHandle = userManager.getUserForSerialNumber(serialNumber);
1820 } catch (NumberFormatException e) {
1821 Log.e(this, e, "Could not parse UserHandle " + userSerialNumberString);
1822 }
1823 }
Ihab Awadb78b2762014-07-25 15:16:23 -07001824 return new PhoneAccountHandle(
1825 ComponentName.unflattenFromString(componentNameString),
Evan Charlton105d9772014-11-25 14:08:53 -08001826 idString,
1827 userHandle);
Ihab Awadb78b2762014-07-25 15:16:23 -07001828 }
1829 }
1830 return null;
Ihab Awad104f8062014-07-17 11:29:35 -07001831 }
1832 };
Santos Cordon176ae282014-07-14 02:02:14 -07001833}