blob: 8cdec308fd6e461e072ab92c2ca279c763130d70 [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) {}
Tyler Gunnea4c6fb2017-04-14 13:46:21 -0700123 public void onPhoneAccountRegistered(PhoneAccountRegistrar registrar,
124 PhoneAccountHandle handle) {}
125 public void onPhoneAccountUnRegistered(PhoneAccountRegistrar registrar,
126 PhoneAccountHandle handle) {}
Ihab Awadb78b2762014-07-25 15:16:23 -0700127 }
128
Tyler Gunn2b17f232017-03-08 08:51:00 -0800129 /**
130 * Abstracts away dependency on the {@link PackageManager} required to fetch the label for an
131 * app.
132 */
133 public interface AppLabelProxy {
134 CharSequence getAppLabel(String packageName);
135 }
136
Ihab Awadb78b2762014-07-25 15:16:23 -0700137 private static final String FILE_NAME = "phone-account-registrar-state.xml";
Tyler Gunn84253572014-09-02 14:50:05 -0700138 @VisibleForTesting
Tony Mak4a3e2fd2015-12-04 11:58:38 +0000139 public static final int EXPECTED_STATE_VERSION = 9;
Tyler Gunn84253572014-09-02 14:50:05 -0700140
141 /** Keep in sync with the same in SipSettings.java */
142 private static final String SIP_SHARED_PREFERENCES = "SIP_PREFERENCES";
Ihab Awadb78b2762014-07-25 15:16:23 -0700143
144 private final List<Listener> mListeners = new CopyOnWriteArrayList<>();
145 private final AtomicFile mAtomicFile;
Santos Cordonafe59e52014-08-22 16:48:43 -0700146 private final Context mContext;
Evan Charlton105d9772014-11-25 14:08:53 -0800147 private final UserManager mUserManager;
Wink Saville33c05d32014-11-20 13:04:17 -0800148 private final SubscriptionManager mSubscriptionManager;
Hall Liu2b33df72016-12-06 18:15:39 -0800149 private final DefaultDialerCache mDefaultDialerCache;
Tyler Gunn2b17f232017-03-08 08:51:00 -0800150 private final AppLabelProxy mAppLabelProxy;
Ihab Awadb78b2762014-07-25 15:16:23 -0700151 private State mState;
Evan Charlton105d9772014-11-25 14:08:53 -0800152 private UserHandle mCurrentUserHandle;
Hall Liu3f7f64d2016-01-07 17:40:13 -0800153 private interface PhoneAccountRegistrarWriteLock {}
154 private final PhoneAccountRegistrarWriteLock mWriteLock =
155 new PhoneAccountRegistrarWriteLock() {};
Santos Cordon176ae282014-07-14 02:02:14 -0700156
Nancy Chen06ce0622014-10-23 01:17:35 +0000157 @VisibleForTesting
Tyler Gunn2b17f232017-03-08 08:51:00 -0800158 public PhoneAccountRegistrar(Context context, DefaultDialerCache defaultDialerCache,
159 AppLabelProxy appLabelProxy) {
160 this(context, FILE_NAME, defaultDialerCache, appLabelProxy);
Ihab Awadb78b2762014-07-25 15:16:23 -0700161 }
162
163 @VisibleForTesting
Hall Liu2b33df72016-12-06 18:15:39 -0800164 public PhoneAccountRegistrar(Context context, String fileName,
Tyler Gunn2b17f232017-03-08 08:51:00 -0800165 DefaultDialerCache defaultDialerCache, AppLabelProxy appLabelProxy) {
166
Ihab Awadb78b2762014-07-25 15:16:23 -0700167 mAtomicFile = new AtomicFile(new File(context.getFilesDir(), fileName));
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700168
Ihab Awadb78b2762014-07-25 15:16:23 -0700169 mState = new State();
Santos Cordonafe59e52014-08-22 16:48:43 -0700170 mContext = context;
Evan Charlton105d9772014-11-25 14:08:53 -0800171 mUserManager = UserManager.get(context);
Hall Liu2b33df72016-12-06 18:15:39 -0800172 mDefaultDialerCache = defaultDialerCache;
Wink Saville33c05d32014-11-20 13:04:17 -0800173 mSubscriptionManager = SubscriptionManager.from(mContext);
Tyler Gunn2b17f232017-03-08 08:51:00 -0800174 mAppLabelProxy = appLabelProxy;
Evan Charlton105d9772014-11-25 14:08:53 -0800175 mCurrentUserHandle = Process.myUserHandle();
Ihab Awadb78b2762014-07-25 15:16:23 -0700176 read();
Santos Cordon176ae282014-07-14 02:02:14 -0700177 }
178
Tyler Gunn84253572014-09-02 14:50:05 -0700179 /**
Nancy Chen140004a2014-10-15 15:48:38 -0700180 * Retrieves the subscription id for a given phone account if it exists. Subscription ids
181 * apply only to PSTN/SIM card phone accounts so all other accounts should not have a
182 * subscription id.
183 * @param accountHandle The handle for the phone account for which to retrieve the
184 * subscription id.
Wink Saville35850602014-10-23 15:57:21 -0700185 * @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 -0700186 */
Wink Saville35850602014-10-23 15:57:21 -0700187 public int getSubscriptionIdForPhoneAccount(PhoneAccountHandle accountHandle) {
Tony Mak240656f2015-12-04 11:36:22 +0000188 PhoneAccount account = getPhoneAccountUnchecked(accountHandle);
Santos Cordon479b3022015-02-06 04:27:15 -0800189
Santos Cordon6a212642015-05-08 16:35:23 -0700190 if (account != null && account.hasCapabilities(PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION)) {
Santos Cordon479b3022015-02-06 04:27:15 -0800191 TelephonyManager tm =
192 (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE);
193 return tm.getSubIdForPhoneAccount(account);
Nancy Chen140004a2014-10-15 15:48:38 -0700194 }
Santos Cordon479b3022015-02-06 04:27:15 -0800195 return SubscriptionManager.INVALID_SUBSCRIPTION_ID;
Nancy Chen140004a2014-10-15 15:48:38 -0700196 }
197
198 /**
Evan Charlton105d9772014-11-25 14:08:53 -0800199 * Retrieves the default outgoing phone account supporting the specified uriScheme. Note that if
200 * {@link #mCurrentUserHandle} does not have visibility into the current default, {@code null}
201 * will be returned.
202 *
Tyler Gunn84253572014-09-02 14:50:05 -0700203 * @param uriScheme The URI scheme for the outgoing call.
204 * @return The {@link PhoneAccountHandle} to use.
205 */
Tony Mak240656f2015-12-04 11:36:22 +0000206 public PhoneAccountHandle getOutgoingPhoneAccountForScheme(String uriScheme,
207 UserHandle userHandle) {
208 final PhoneAccountHandle userSelected = getUserSelectedOutgoingPhoneAccount(userHandle);
Tyler Gunn84253572014-09-02 14:50:05 -0700209
Yorke Lee5e8836a2014-08-22 15:25:18 -0700210 if (userSelected != null) {
Tyler Gunn84253572014-09-02 14:50:05 -0700211 // If there is a default PhoneAccount, ensure it supports calls to handles with the
212 // specified uriScheme.
Tony Mak240656f2015-12-04 11:36:22 +0000213 final PhoneAccount userSelectedAccount = getPhoneAccountUnchecked(userSelected);
Santos Cordon6a212642015-05-08 16:35:23 -0700214 if (userSelectedAccount.supportsUriScheme(uriScheme)) {
Tyler Gunn84253572014-09-02 14:50:05 -0700215 return userSelected;
216 }
Ihab Awad293edf22014-07-24 17:52:29 -0700217 }
218
Tony Mak240656f2015-12-04 11:36:22 +0000219 List<PhoneAccountHandle> outgoing = getCallCapablePhoneAccounts(uriScheme, false,
220 userHandle);
Ihab Awad6fb37c82014-08-07 19:48:57 -0700221 switch (outgoing.size()) {
Ihab Awad293edf22014-07-24 17:52:29 -0700222 case 0:
223 // There are no accounts, so there can be no default
224 return null;
225 case 1:
Evan Charlton105d9772014-11-25 14:08:53 -0800226 // There is only one account, which is by definition the default.
Santos Cordon6a212642015-05-08 16:35:23 -0700227 return outgoing.get(0);
Ihab Awad293edf22014-07-24 17:52:29 -0700228 default:
229 // There are multiple accounts with no selected default
230 return null;
Ihab Awadf2a84912014-07-22 21:09:25 -0700231 }
Ihab Awad104f8062014-07-17 11:29:35 -0700232 }
Santos Cordon176ae282014-07-14 02:02:14 -0700233
Tony Mak240656f2015-12-04 11:36:22 +0000234 public PhoneAccountHandle getOutgoingPhoneAccountForSchemeOfCurrentUser(String uriScheme) {
235 return getOutgoingPhoneAccountForScheme(uriScheme, mCurrentUserHandle);
236 }
237
Evan Charlton105d9772014-11-25 14:08:53 -0800238 /**
239 * @return The user-selected outgoing {@link PhoneAccount}, or null if it hasn't been set (or
240 * if it was set by another user).
241 */
Hall Liuecda5542015-12-04 11:31:31 -0800242 @VisibleForTesting
243 public PhoneAccountHandle getUserSelectedOutgoingPhoneAccount(UserHandle userHandle) {
Tony Mak4a3e2fd2015-12-04 11:58:38 +0000244 if (userHandle == null) {
245 return null;
246 }
247 DefaultPhoneAccountHandle defaultPhoneAccountHandle = mState.defaultOutgoingAccountHandles
248 .get(userHandle);
249 if (defaultPhoneAccountHandle == null) {
250 return null;
251 }
252 // Make sure the account is still registered and owned by the user.
253 PhoneAccount account = getPhoneAccount(defaultPhoneAccountHandle.phoneAccountHandle,
254 userHandle);
Hall Liuecda5542015-12-04 11:31:31 -0800255
Santos Cordon6a212642015-05-08 16:35:23 -0700256 if (account != null) {
Tony Mak4a3e2fd2015-12-04 11:58:38 +0000257 return defaultPhoneAccountHandle.phoneAccountHandle;
Yorke Lee5e8836a2014-08-22 15:25:18 -0700258 }
259 return null;
260 }
261
Santos Cordon6a212642015-05-08 16:35:23 -0700262 /**
Brad Ebinger0c87b3c2016-06-10 17:23:02 -0700263 * @return The {@link DefaultPhoneAccountHandle} containing the user-selected default calling
264 * account and group Id for the {@link UserHandle} specified.
265 */
266 private DefaultPhoneAccountHandle getUserSelectedDefaultPhoneAccount(UserHandle userHandle) {
267 if (userHandle == null) {
268 return null;
269 }
270 DefaultPhoneAccountHandle defaultPhoneAccountHandle = mState.defaultOutgoingAccountHandles
271 .get(userHandle);
272 if (defaultPhoneAccountHandle == null) {
273 return null;
274 }
275
276 return defaultPhoneAccountHandle;
277 }
278
279 /**
280 * @return The currently registered PhoneAccount in Telecom that has the same group Id.
281 */
282 private PhoneAccount getPhoneAccountByGroupId(String groupId, ComponentName groupComponentName,
283 UserHandle userHandle, PhoneAccountHandle excludePhoneAccountHandle) {
284 if (groupId == null || groupId.isEmpty() || userHandle == null) {
285 return null;
286 }
287 // Get the PhoneAccount with the same group Id (and same ComponentName) that is not the
288 // newAccount that was just added
289 List<PhoneAccount> accounts = getAllPhoneAccounts(userHandle).stream()
290 .filter(account -> groupId.equals(account.getGroupId()) &&
291 !account.getAccountHandle().equals(excludePhoneAccountHandle) &&
292 Objects.equals(account.getAccountHandle().getComponentName(),
293 groupComponentName))
294 .collect(Collectors.toList());
295 // There should be one or no PhoneAccounts with the same group Id
296 if (accounts.size() > 1) {
297 Log.w(this, "Found multiple PhoneAccounts registered to the same Group Id!");
298 }
299 return accounts.isEmpty() ? null : accounts.get(0);
300 }
301
302 /**
Santos Cordon6a212642015-05-08 16:35:23 -0700303 * Sets the phone account with which to place all calls by default. Set by the user
304 * within phone settings.
305 */
Tony Mak4a3e2fd2015-12-04 11:58:38 +0000306 public void setUserSelectedOutgoingPhoneAccount(PhoneAccountHandle accountHandle,
307 UserHandle userHandle) {
308 if (userHandle == null) {
309 return;
310 }
Evan Charlton89176372014-07-19 18:23:09 -0700311 if (accountHandle == null) {
Ihab Awad104f8062014-07-17 11:29:35 -0700312 // Asking to clear the default outgoing is a valid request
Tony Mak4a3e2fd2015-12-04 11:58:38 +0000313 mState.defaultOutgoingAccountHandles.remove(userHandle);
Ihab Awad104f8062014-07-17 11:29:35 -0700314 } else {
Tony Mak4a3e2fd2015-12-04 11:58:38 +0000315 PhoneAccount account = getPhoneAccount(accountHandle, userHandle);
Santos Cordon6a212642015-05-08 16:35:23 -0700316 if (account == null) {
Ihab Awadb78b2762014-07-25 15:16:23 -0700317 Log.w(this, "Trying to set nonexistent default outgoing %s",
318 accountHandle);
319 return;
320 }
321
Santos Cordon6a212642015-05-08 16:35:23 -0700322 if (!account.hasCapabilities(PhoneAccount.CAPABILITY_CALL_PROVIDER)) {
Ihab Awadb78b2762014-07-25 15:16:23 -0700323 Log.w(this, "Trying to set non-call-provider default outgoing %s",
Evan Charlton89176372014-07-19 18:23:09 -0700324 accountHandle);
Ihab Awad104f8062014-07-17 11:29:35 -0700325 return;
326 }
327
Santos Cordon6a212642015-05-08 16:35:23 -0700328 if (account.hasCapabilities(PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION)) {
Nancy Chen5a36b6e2014-10-23 17:42:42 -0700329 // If the account selected is a SIM account, propagate down to the subscription
330 // record.
Wink Saville7ce6e782014-10-27 10:56:46 -0700331 int subId = getSubscriptionIdForPhoneAccount(accountHandle);
Wink Saville33c05d32014-11-20 13:04:17 -0800332 mSubscriptionManager.setDefaultVoiceSubId(subId);
Nancy Chen5a36b6e2014-10-23 17:42:42 -0700333 }
334
Tony Mak4a3e2fd2015-12-04 11:58:38 +0000335 mState.defaultOutgoingAccountHandles
Brad Ebinger0c87b3c2016-06-10 17:23:02 -0700336 .put(userHandle, new DefaultPhoneAccountHandle(userHandle, accountHandle,
337 account.getGroupId()));
Santos Cordon176ae282014-07-14 02:02:14 -0700338 }
339
Ihab Awad293edf22014-07-24 17:52:29 -0700340 write();
Ihab Awadb78b2762014-07-25 15:16:23 -0700341 fireDefaultOutgoingChanged();
Santos Cordon176ae282014-07-14 02:02:14 -0700342 }
343
Nancy Chen668dee02014-11-19 15:31:31 -0800344 boolean isUserSelectedSmsPhoneAccount(PhoneAccountHandle accountHandle) {
Shishir Agrawalce688742016-01-25 15:02:21 -0800345 return getSubscriptionIdForPhoneAccount(accountHandle) ==
346 SubscriptionManager.getDefaultSmsSubscriptionId();
Sailesh Nepalfc43ea82015-07-28 19:30:28 -0700347 }
348
Sailesh Nepal8ed7eeb2015-09-08 12:30:13 -0700349 public ComponentName getSystemSimCallManagerComponent() {
350 String defaultSimCallManager = null;
351 CarrierConfigManager configManager = (CarrierConfigManager) mContext.getSystemService(
352 Context.CARRIER_CONFIG_SERVICE);
353 PersistableBundle configBundle = configManager.getConfig();
354 if (configBundle != null) {
355 defaultSimCallManager = configBundle.getString(
356 CarrierConfigManager.KEY_DEFAULT_SIM_CALL_MANAGER_STRING);
357 }
358 return TextUtils.isEmpty(defaultSimCallManager)
359 ? null : ComponentName.unflattenFromString(defaultSimCallManager);
360 }
361
Tony Mak240656f2015-12-04 11:36:22 +0000362 public PhoneAccountHandle getSimCallManagerOfCurrentUser() {
363 return getSimCallManager(mCurrentUserHandle);
364 }
365
Sailesh Nepalfc43ea82015-07-28 19:30:28 -0700366 /**
367 * Returns the {@link PhoneAccountHandle} corresponding to the currently active SIM Call
368 * Manager. SIM Call Manager returned corresponds to the following priority order:
369 * 1. If a SIM Call Manager {@link PhoneAccount} is registered for the same package as the
370 * default dialer, then that one is returned.
371 * 2. If there is a SIM Call Manager {@link PhoneAccount} registered which matches the
372 * carrier configuration's default, then that one is returned.
373 * 3. Otherwise, we return null.
374 */
Tony Mak240656f2015-12-04 11:36:22 +0000375 public PhoneAccountHandle getSimCallManager(UserHandle userHandle) {
Santos Cordon59c21a72015-06-11 10:11:21 -0700376 // Get the default dialer in case it has a connection manager associated with it.
Hall Liu2b33df72016-12-06 18:15:39 -0800377 String dialerPackage = mDefaultDialerCache
378 .getDefaultDialerApplication(userHandle.getIdentifier());
Evan Charltonaf51ceb2014-07-30 11:56:36 -0700379
Sailesh Nepalaecfa9b2015-06-16 14:55:27 -0700380 // Check carrier config.
Sailesh Nepal8ed7eeb2015-09-08 12:30:13 -0700381 ComponentName systemSimCallManagerComponent = getSystemSimCallManagerComponent();
Sailesh Nepal91fc8092015-02-14 15:44:55 -0800382
Santos Cordon59c21a72015-06-11 10:11:21 -0700383 PhoneAccountHandle dialerSimCallManager = null;
384 PhoneAccountHandle systemSimCallManager = null;
Sailesh Nepal91fc8092015-02-14 15:44:55 -0800385
Santos Cordon59c21a72015-06-11 10:11:21 -0700386 if (!TextUtils.isEmpty(dialerPackage) || systemSimCallManagerComponent != null) {
387 // loop through and look for any connection manager in the same package.
388 List<PhoneAccountHandle> allSimCallManagers = getPhoneAccountHandles(
389 PhoneAccount.CAPABILITY_CONNECTION_MANAGER, null, null,
Tony Mak240656f2015-12-04 11:36:22 +0000390 true /* includeDisabledAccounts */, userHandle);
Santos Cordon59c21a72015-06-11 10:11:21 -0700391 for (PhoneAccountHandle accountHandle : allSimCallManagers) {
392 ComponentName component = accountHandle.getComponentName();
393
394 // Store the system connection manager if found
395 if (systemSimCallManager == null
396 && Objects.equals(component, systemSimCallManagerComponent)
397 && !resolveComponent(accountHandle).isEmpty()) {
398 systemSimCallManager = accountHandle;
399
400 // Store the dialer connection manager if found
401 } else if (dialerSimCallManager == null
402 && Objects.equals(component.getPackageName(), dialerPackage)
403 && !resolveComponent(accountHandle).isEmpty()) {
404 dialerSimCallManager = accountHandle;
Evan Charltonaf51ceb2014-07-30 11:56:36 -0700405 }
Evan Charltonaf51ceb2014-07-30 11:56:36 -0700406 }
Evan Charltonaf51ceb2014-07-30 11:56:36 -0700407 }
408
Santos Cordon59c21a72015-06-11 10:11:21 -0700409 PhoneAccountHandle retval = dialerSimCallManager != null ?
410 dialerSimCallManager : systemSimCallManager;
411
412 Log.i(this, "SimCallManager queried, returning: %s", retval);
413
Sailesh Nepalb3ccd7e2015-06-26 18:16:18 -0700414 return retval;
Ihab Awad293edf22014-07-24 17:52:29 -0700415 }
416
Evan Charlton105d9772014-11-25 14:08:53 -0800417 /**
Tony Mak240656f2015-12-04 11:36:22 +0000418 * If it is a outgoing call, sim call manager of call-initiating user is returned.
419 * Otherwise, we return the sim call manager of the user associated with the
420 * target phone account.
421 * @return phone account handle of sim call manager based on the ongoing call.
422 */
423 public PhoneAccountHandle getSimCallManagerFromCall(Call call) {
424 if (call == null) {
425 return null;
426 }
427 UserHandle userHandle = call.getInitiatingUser();
428 if (userHandle == null) {
429 userHandle = call.getTargetPhoneAccount().getUserHandle();
430 }
431 return getSimCallManager(userHandle);
432 }
433
434 /**
Evan Charlton105d9772014-11-25 14:08:53 -0800435 * Update the current UserHandle to track when users are switched. This will allow the
436 * PhoneAccountRegistar to self-filter the PhoneAccounts to make sure we don't leak anything
437 * across users.
Santos Cordon6a212642015-05-08 16:35:23 -0700438 * We cannot simply check the calling user because that would always return the primary user for
439 * all invocations originating with the system process.
Evan Charlton105d9772014-11-25 14:08:53 -0800440 *
441 * @param userHandle The {@link UserHandle}, as delivered by
442 * {@link Intent#ACTION_USER_SWITCHED}.
443 */
444 public void setCurrentUserHandle(UserHandle userHandle) {
445 if (userHandle == null) {
446 Log.d(this, "setCurrentUserHandle, userHandle = null");
447 userHandle = Process.myUserHandle();
448 }
449 Log.d(this, "setCurrentUserHandle, %s", userHandle);
450 mCurrentUserHandle = userHandle;
451 }
452
Yorke Lee71734c22015-06-02 14:22:56 -0700453 /**
454 * @return {@code true} if the phone account was successfully enabled/disabled, {@code false}
455 * otherwise.
456 */
457 public boolean enablePhoneAccount(PhoneAccountHandle accountHandle, boolean isEnabled) {
Tony Mak98e6bdc2015-11-25 21:54:05 +0000458 PhoneAccount account = getPhoneAccountUnchecked(accountHandle);
Hall Liu518a7dd2017-01-26 14:20:04 -0800459 Log.i(this, "Phone account %s %s.", accountHandle, isEnabled ? "enabled" : "disabled");
Santos Cordon638f05c2015-05-19 17:12:33 -0700460 if (account == null) {
461 Log.w(this, "Could not find account to enable: " + accountHandle);
Yorke Lee71734c22015-06-02 14:22:56 -0700462 return false;
Santos Cordon638f05c2015-05-19 17:12:33 -0700463 } else if (account.hasCapabilities(PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION)) {
Santos Cordonea5cb932015-05-07 16:28:38 -0700464 // We never change the enabled state of SIM-based accounts.
Santos Cordon638f05c2015-05-19 17:12:33 -0700465 Log.w(this, "Could not change enable state of SIM account: " + accountHandle);
Yorke Lee71734c22015-06-02 14:22:56 -0700466 return false;
Santos Cordonea5cb932015-05-07 16:28:38 -0700467 }
468
Santos Cordon638f05c2015-05-19 17:12:33 -0700469 if (account.isEnabled() != isEnabled) {
Santos Cordonea5cb932015-05-07 16:28:38 -0700470 account.setIsEnabled(isEnabled);
Tony Mak48367162016-01-15 22:58:18 +0000471 if (!isEnabled) {
472 // If the disabled account is the default, remove it.
473 removeDefaultPhoneAccountHandle(accountHandle);
474 }
Santos Cordonea5cb932015-05-07 16:28:38 -0700475 write();
476 fireAccountsChanged();
477 }
Yorke Lee71734c22015-06-02 14:22:56 -0700478 return true;
Santos Cordonea5cb932015-05-07 16:28:38 -0700479 }
480
Tony Mak48367162016-01-15 22:58:18 +0000481 private void removeDefaultPhoneAccountHandle(PhoneAccountHandle phoneAccountHandle) {
482 Iterator<Map.Entry<UserHandle, DefaultPhoneAccountHandle>> iterator =
483 mState.defaultOutgoingAccountHandles.entrySet().iterator();
484 while (iterator.hasNext()) {
485 Map.Entry<UserHandle, DefaultPhoneAccountHandle> entry = iterator.next();
486 if (phoneAccountHandle.equals(entry.getValue().phoneAccountHandle)) {
487 iterator.remove();
488 }
489 }
490 }
491
Tony Mak240656f2015-12-04 11:36:22 +0000492 private boolean isVisibleForUser(PhoneAccount account, UserHandle userHandle,
493 boolean acrossProfiles) {
Evan Charlton105d9772014-11-25 14:08:53 -0800494 if (account == null) {
495 return false;
496 }
497
Tony Mak240656f2015-12-04 11:36:22 +0000498 if (userHandle == null) {
499 Log.w(this, "userHandle is null in isVisibleForUser");
500 return false;
501 }
502
Evan Charlton105d9772014-11-25 14:08:53 -0800503 // If this PhoneAccount has CAPABILITY_MULTI_USER, it should be visible to all users and
504 // all profiles. Only Telephony and SIP accounts should have this capability.
505 if (account.hasCapabilities(PhoneAccount.CAPABILITY_MULTI_USER)) {
506 return true;
507 }
508
509 UserHandle phoneAccountUserHandle = account.getAccountHandle().getUserHandle();
510 if (phoneAccountUserHandle == null) {
511 return false;
512 }
513
514 if (mCurrentUserHandle == null) {
Tony Mak240656f2015-12-04 11:36:22 +0000515 // In case we need to have emergency phone calls from the lock screen.
Evan Charlton105d9772014-11-25 14:08:53 -0800516 Log.d(this, "Current user is null; assuming true");
517 return true;
518 }
519
Tony Mak240656f2015-12-04 11:36:22 +0000520 if (acrossProfiles) {
521 return UserManager.get(mContext).isSameProfileGroup(userHandle.getIdentifier(),
522 phoneAccountUserHandle.getIdentifier());
523 } else {
524 return phoneAccountUserHandle.equals(userHandle);
Sailesh Nepal91fc8092015-02-14 15:44:55 -0800525 }
Evan Charlton105d9772014-11-25 14:08:53 -0800526 }
527
528 private List<ResolveInfo> resolveComponent(PhoneAccountHandle phoneAccountHandle) {
529 return resolveComponent(phoneAccountHandle.getComponentName(),
Tony Mak240656f2015-12-04 11:36:22 +0000530 phoneAccountHandle.getUserHandle());
Evan Charlton105d9772014-11-25 14:08:53 -0800531 }
532
533 private List<ResolveInfo> resolveComponent(ComponentName componentName,
534 UserHandle userHandle) {
mike dooley10a58312014-11-06 13:46:19 -0800535 PackageManager pm = mContext.getPackageManager();
536 Intent intent = new Intent(ConnectionService.SERVICE_INTERFACE);
537 intent.setComponent(componentName);
Sailesh Nepal91fc8092015-02-14 15:44:55 -0800538 try {
539 if (userHandle != null) {
540 return pm.queryIntentServicesAsUser(intent, 0, userHandle.getIdentifier());
541 } else {
542 return pm.queryIntentServices(intent, 0);
543 }
544 } catch (SecurityException e) {
Santos Cordon3188b362015-05-22 13:01:10 -0700545 Log.e(this, e, "%s is not visible for the calling user", componentName);
Sailesh Nepal91fc8092015-02-14 15:44:55 -0800546 return Collections.EMPTY_LIST;
Evan Charlton105d9772014-11-25 14:08:53 -0800547 }
mike dooley10a58312014-11-06 13:46:19 -0800548 }
549
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700550 /**
551 * Retrieves a list of all {@link PhoneAccountHandle}s registered.
Santos Cordonea5cb932015-05-07 16:28:38 -0700552 * Only returns accounts which are enabled.
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700553 *
554 * @return The list of {@link PhoneAccountHandle}s.
555 */
Tony Mak240656f2015-12-04 11:36:22 +0000556 public List<PhoneAccountHandle> getAllPhoneAccountHandles(UserHandle userHandle) {
557 return getPhoneAccountHandles(0, null, null, false, userHandle);
Ihab Awad293edf22014-07-24 17:52:29 -0700558 }
559
Tony Mak240656f2015-12-04 11:36:22 +0000560 public List<PhoneAccount> getAllPhoneAccounts(UserHandle userHandle) {
561 return getPhoneAccounts(0, null, null, false, userHandle);
562 }
563
564 public List<PhoneAccount> getAllPhoneAccountsOfCurrentUser() {
565 return getAllPhoneAccounts(mCurrentUserHandle);
Santos Cordonafe59e52014-08-22 16:48:43 -0700566 }
567
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700568 /**
Nancy Chen309198e2014-09-15 18:02:49 -0700569 * Retrieves a list of all phone account call provider phone accounts supporting the
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700570 * specified URI scheme.
571 *
572 * @param uriScheme The URI scheme.
573 * @return The phone account handles.
574 */
Santos Cordonea5cb932015-05-07 16:28:38 -0700575 public List<PhoneAccountHandle> getCallCapablePhoneAccounts(
Tony Mak240656f2015-12-04 11:36:22 +0000576 String uriScheme, boolean includeDisabledAccounts, UserHandle userHandle) {
Santos Cordonea5cb932015-05-07 16:28:38 -0700577 return getPhoneAccountHandles(
Bryce Leea0bb7052015-10-16 13:31:40 -0700578 PhoneAccount.CAPABILITY_CALL_PROVIDER,
579 PhoneAccount.CAPABILITY_EMERGENCY_CALLS_ONLY /*excludedCapabilities*/,
Tony Mak240656f2015-12-04 11:36:22 +0000580 uriScheme, null, includeDisabledAccounts, userHandle);
581 }
582
Tyler Gunn66ab9ae2017-04-07 15:00:49 -0700583 /**
584 * Retrieves a list of all phone accounts which have
585 * {@link PhoneAccount#CAPABILITY_SELF_MANAGED}.
586 * <p>
587 * Returns only the {@link PhoneAccount}s which are enabled as self-managed accounts are
588 * automatically enabled by default (see {@link #registerPhoneAccount(PhoneAccount)}).
589 *
590 * @param userHandle User handle of phone account owner.
591 * @return The phone account handles.
592 */
593 public List<PhoneAccountHandle> getSelfManagedPhoneAccounts(UserHandle userHandle) {
594 return getPhoneAccountHandles(
595 PhoneAccount.CAPABILITY_SELF_MANAGED,
596 PhoneAccount.CAPABILITY_EMERGENCY_CALLS_ONLY /* excludedCapabilities */,
597 null /* uriScheme */, null /* packageName */, false /* includeDisabledAccounts */,
598 userHandle);
599 }
600
Tony Mak240656f2015-12-04 11:36:22 +0000601 public List<PhoneAccountHandle> getCallCapablePhoneAccountsOfCurrentUser(
602 String uriScheme, boolean includeDisabledAccounts) {
603 return getCallCapablePhoneAccounts(uriScheme, includeDisabledAccounts, mCurrentUserHandle);
Santos Cordon6a212642015-05-08 16:35:23 -0700604 }
605
606 /**
607 * Retrieves a list of all the SIM-based phone accounts.
608 */
Tony Mak240656f2015-12-04 11:36:22 +0000609 public List<PhoneAccountHandle> getSimPhoneAccounts(UserHandle userHandle) {
Santos Cordon6a212642015-05-08 16:35:23 -0700610 return getPhoneAccountHandles(
611 PhoneAccount.CAPABILITY_CALL_PROVIDER | PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION,
Tony Mak240656f2015-12-04 11:36:22 +0000612 null, null, false, userHandle);
Tyler Gunn84253572014-09-02 14:50:05 -0700613 }
614
Tony Mak240656f2015-12-04 11:36:22 +0000615 public List<PhoneAccountHandle> getSimPhoneAccountsOfCurrentUser() {
616 return getSimPhoneAccounts(mCurrentUserHandle);
617 }
618
619 /**
620 * Retrieves a list of all phone accounts registered by a specified package.
621 *
622 * @param packageName The name of the package that registered the phone accounts.
623 * @return The phone account handles.
624 */
625 public List<PhoneAccountHandle> getPhoneAccountsForPackage(String packageName,
626 UserHandle userHandle) {
627 return getPhoneAccountHandles(0, null, packageName, false, userHandle);
Nancy Chen1c5926f2014-09-17 14:44:14 -0700628 }
629
Ihab Awad104f8062014-07-17 11:29:35 -0700630 // TODO: Should we implement an artificial limit for # of accounts associated with a single
631 // ComponentName?
Ihab Awad293edf22014-07-24 17:52:29 -0700632 public void registerPhoneAccount(PhoneAccount account) {
Tyler Gunncb59b672014-08-20 09:02:11 -0700633 // Enforce the requirement that a connection service for a phone account has the correct
634 // permission.
Santos Cordon6a212642015-05-08 16:35:23 -0700635 if (!phoneAccountRequiresBindPermission(account.getAccountHandle())) {
Yorke Lee7bb8ce92015-05-13 16:28:29 -0700636 Log.w(this,
637 "Phone account %s does not have BIND_TELECOM_CONNECTION_SERVICE permission.",
Tyler Gunncb59b672014-08-20 09:02:11 -0700638 account.getAccountHandle());
Yorke Lee7bb8ce92015-05-13 16:28:29 -0700639 throw new SecurityException("PhoneAccount connection service requires "
640 + "BIND_TELECOM_CONNECTION_SERVICE permission.");
Tyler Gunncb59b672014-08-20 09:02:11 -0700641 }
642
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700643 addOrReplacePhoneAccount(account);
644 }
645
646 /**
647 * Adds a {@code PhoneAccount}, replacing an existing one if found.
648 *
649 * @param account The {@code PhoneAccount} to add or replace.
650 */
651 private void addOrReplacePhoneAccount(PhoneAccount account) {
Ihab Awad7e2c7f32014-11-03 09:49:45 -0800652 Log.d(this, "addOrReplacePhoneAccount(%s -> %s)",
653 account.getAccountHandle(), account);
654
Santos Cordonea5cb932015-05-07 16:28:38 -0700655 // Start _enabled_ property as false.
656 // !!! IMPORTANT !!! It is important that we do not read the enabled state that the
657 // source app provides or else an third party app could enable itself.
658 boolean isEnabled = false;
Tyler Gunnea4c6fb2017-04-14 13:46:21 -0700659 boolean isNewAccount;
Santos Cordonea5cb932015-05-07 16:28:38 -0700660
Tony Mak98e6bdc2015-11-25 21:54:05 +0000661 PhoneAccount oldAccount = getPhoneAccountUnchecked(account.getAccountHandle());
Santos Cordon6a212642015-05-08 16:35:23 -0700662 if (oldAccount != null) {
663 mState.accounts.remove(oldAccount);
Santos Cordonea5cb932015-05-07 16:28:38 -0700664 isEnabled = oldAccount.isEnabled();
Tyler Gunnea4c6fb2017-04-14 13:46:21 -0700665 Log.i(this, "Modify account: %s", getAccountDiffString(account, oldAccount));
666 isNewAccount = false;
Santos Cordon5fa4e4f2015-06-10 14:56:01 -0700667 } else {
668 Log.i(this, "New phone account registered: " + account);
Tyler Gunnea4c6fb2017-04-14 13:46:21 -0700669 isNewAccount = true;
Santos Cordon176ae282014-07-14 02:02:14 -0700670 }
Santos Cordonea5cb932015-05-07 16:28:38 -0700671
Tyler Gunn2b17f232017-03-08 08:51:00 -0800672 // When registering a self-managed PhoneAccount we enforce the rule that the label that the
673 // app uses is also its phone account label. Also ensure it does not attempt to declare
674 // itself as a sim acct, call manager or call provider.
675 if (account.hasCapabilities(PhoneAccount.CAPABILITY_SELF_MANAGED)) {
676 // Turn off bits we don't want to be able to set (TelecomServiceImpl protects against
677 // this but we'll also prevent it from happening here, just to be safe).
678 int newCapabilities = account.getCapabilities() &
679 ~(PhoneAccount.CAPABILITY_CALL_PROVIDER |
680 PhoneAccount.CAPABILITY_CONNECTION_MANAGER |
681 PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION);
682
683 // Ensure name is correct.
684 CharSequence newLabel = mAppLabelProxy.getAppLabel(
685 account.getAccountHandle().getComponentName().getPackageName());
686
687 account = account.toBuilder()
688 .setLabel(newLabel)
689 .setCapabilities(newCapabilities)
690 .build();
691 }
692
Santos Cordon6a212642015-05-08 16:35:23 -0700693 mState.accounts.add(account);
Brad Ebinger0c87b3c2016-06-10 17:23:02 -0700694 // Set defaults and replace based on the group Id.
695 maybeReplaceOldAccount(account);
Santos Cordonea5cb932015-05-07 16:28:38 -0700696 // Reset enabled state to whatever the value was if the account was already registered,
Tyler Gunnacb3bc82017-01-09 09:43:56 -0800697 // or _true_ if this is a SIM-based account. All SIM-based accounts are always enabled,
698 // as are all self-managed phone accounts.
Santos Cordonea5cb932015-05-07 16:28:38 -0700699 account.setIsEnabled(
Tyler Gunnacb3bc82017-01-09 09:43:56 -0800700 isEnabled || account.hasCapabilities(PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION)
701 || account.hasCapabilities(PhoneAccount.CAPABILITY_SELF_MANAGED));
Santos Cordon176ae282014-07-14 02:02:14 -0700702
Ihab Awad293edf22014-07-24 17:52:29 -0700703 write();
Ihab Awadb78b2762014-07-25 15:16:23 -0700704 fireAccountsChanged();
Tyler Gunnea4c6fb2017-04-14 13:46:21 -0700705 if (isNewAccount) {
706 fireAccountRegistered(account.getAccountHandle());
707 }
Ihab Awad293edf22014-07-24 17:52:29 -0700708 }
709
Evan Charlton89176372014-07-19 18:23:09 -0700710 public void unregisterPhoneAccount(PhoneAccountHandle accountHandle) {
Tony Mak98e6bdc2015-11-25 21:54:05 +0000711 PhoneAccount account = getPhoneAccountUnchecked(accountHandle);
Santos Cordon6a212642015-05-08 16:35:23 -0700712 if (account != null) {
713 if (mState.accounts.remove(account)) {
714 write();
715 fireAccountsChanged();
Tyler Gunnea4c6fb2017-04-14 13:46:21 -0700716 fireAccountUnRegistered(accountHandle);
Ihab Awad104f8062014-07-17 11:29:35 -0700717 }
718 }
Santos Cordon176ae282014-07-14 02:02:14 -0700719 }
720
Tyler Gunnd900ce62014-08-13 11:40:59 -0700721 /**
722 * Un-registers all phone accounts associated with a specified package.
723 *
724 * @param packageName The package for which phone accounts will be removed.
Evan Charlton105d9772014-11-25 14:08:53 -0800725 * @param userHandle The {@link UserHandle} the package is running under.
Tyler Gunnd900ce62014-08-13 11:40:59 -0700726 */
Evan Charlton105d9772014-11-25 14:08:53 -0800727 public void clearAccounts(String packageName, UserHandle userHandle) {
Tyler Gunnd900ce62014-08-13 11:40:59 -0700728 boolean accountsRemoved = false;
729 Iterator<PhoneAccount> it = mState.accounts.iterator();
730 while (it.hasNext()) {
731 PhoneAccount phoneAccount = it.next();
Evan Charlton105d9772014-11-25 14:08:53 -0800732 PhoneAccountHandle handle = phoneAccount.getAccountHandle();
733 if (Objects.equals(packageName, handle.getComponentName().getPackageName())
734 && Objects.equals(userHandle, handle.getUserHandle())) {
Tyler Gunnd900ce62014-08-13 11:40:59 -0700735 Log.i(this, "Removing phone account " + phoneAccount.getLabel());
Shriram Ganesh8f89f8d2015-04-15 12:35:43 +0530736 mState.accounts.remove(phoneAccount);
Tyler Gunnd900ce62014-08-13 11:40:59 -0700737 accountsRemoved = true;
Ihab Awad104f8062014-07-17 11:29:35 -0700738 }
739 }
740
Tyler Gunnd900ce62014-08-13 11:40:59 -0700741 if (accountsRemoved) {
742 write();
743 fireAccountsChanged();
744 }
Ihab Awadb78b2762014-07-25 15:16:23 -0700745 }
746
Nancy Chen140004a2014-10-15 15:48:38 -0700747 public boolean isVoiceMailNumber(PhoneAccountHandle accountHandle, String number) {
Wink Saville35850602014-10-23 15:57:21 -0700748 int subId = getSubscriptionIdForPhoneAccount(accountHandle);
Yorke Leeb1957232015-06-30 10:09:54 -0700749 return PhoneNumberUtils.isVoiceMailNumber(mContext, subId, number);
Nancy Chen140004a2014-10-15 15:48:38 -0700750 }
751
Ihab Awadb78b2762014-07-25 15:16:23 -0700752 public void addListener(Listener l) {
753 mListeners.add(l);
754 }
755
756 public void removeListener(Listener l) {
Jay Shraunera82c8f72014-08-14 15:49:16 -0700757 if (l != null) {
758 mListeners.remove(l);
759 }
Ihab Awadb78b2762014-07-25 15:16:23 -0700760 }
761
Tyler Gunnea4c6fb2017-04-14 13:46:21 -0700762 private void fireAccountRegistered(PhoneAccountHandle handle) {
763 for (Listener l : mListeners) {
764 l.onPhoneAccountRegistered(this, handle);
765 }
766 }
767
768 private void fireAccountUnRegistered(PhoneAccountHandle handle) {
769 for (Listener l : mListeners) {
770 l.onPhoneAccountUnRegistered(this, handle);
771 }
772 }
773
Ihab Awadb78b2762014-07-25 15:16:23 -0700774 private void fireAccountsChanged() {
775 for (Listener l : mListeners) {
776 l.onAccountsChanged(this);
777 }
778 }
779
780 private void fireDefaultOutgoingChanged() {
781 for (Listener l : mListeners) {
782 l.onDefaultOutgoingChanged(this);
783 }
784 }
785
Santos Cordon5fa4e4f2015-06-10 14:56:01 -0700786 private String getAccountDiffString(PhoneAccount account1, PhoneAccount account2) {
787 if (account1 == null || account2 == null) {
788 return "Diff: " + account1 + ", " + account2;
789 }
790
791 StringBuffer sb = new StringBuffer();
792 sb.append("[").append(account1.getAccountHandle());
Anthony Lee7c64a4e2015-08-25 09:27:47 -0700793 appendDiff(sb, "addr", Log.piiHandle(account1.getAddress()),
794 Log.piiHandle(account2.getAddress()));
Santos Cordon5fa4e4f2015-06-10 14:56:01 -0700795 appendDiff(sb, "cap", account1.getCapabilities(), account2.getCapabilities());
796 appendDiff(sb, "hl", account1.getHighlightColor(), account2.getHighlightColor());
Santos Cordon5fa4e4f2015-06-10 14:56:01 -0700797 appendDiff(sb, "lbl", account1.getLabel(), account2.getLabel());
798 appendDiff(sb, "desc", account1.getShortDescription(), account2.getShortDescription());
Anthony Lee7c64a4e2015-08-25 09:27:47 -0700799 appendDiff(sb, "subAddr", Log.piiHandle(account1.getSubscriptionAddress()),
800 Log.piiHandle(account2.getSubscriptionAddress()));
Santos Cordon5fa4e4f2015-06-10 14:56:01 -0700801 appendDiff(sb, "uris", account1.getSupportedUriSchemes(),
802 account2.getSupportedUriSchemes());
803 sb.append("]");
804 return sb.toString();
805 }
806
807 private void appendDiff(StringBuffer sb, String attrName, Object obj1, Object obj2) {
808 if (!Objects.equals(obj1, obj2)) {
809 sb.append("(")
810 .append(attrName)
811 .append(": ")
812 .append(obj1)
813 .append(" -> ")
814 .append(obj2)
815 .append(")");
816 }
817 }
818
Brad Ebinger0c87b3c2016-06-10 17:23:02 -0700819 private void maybeReplaceOldAccount(PhoneAccount newAccount) {
820 UserHandle newAccountUserHandle = newAccount.getAccountHandle().getUserHandle();
821 DefaultPhoneAccountHandle defaultHandle =
822 getUserSelectedDefaultPhoneAccount(newAccountUserHandle);
823 if (defaultHandle == null || defaultHandle.groupId.isEmpty()) {
824 Log.v(this, "maybeReplaceOldAccount: Not replacing PhoneAccount, no group Id or " +
825 "default.");
826 return;
827 }
828 if (!defaultHandle.groupId.equals(newAccount.getGroupId())) {
829 Log.v(this, "maybeReplaceOldAccount: group Ids are not equal.");
830 return;
831 }
832 if (Objects.equals(newAccount.getAccountHandle().getComponentName(),
833 defaultHandle.phoneAccountHandle.getComponentName())) {
834 // Move default calling account over to new user, since the ComponentNames and Group Ids
835 // are the same.
836 setUserSelectedOutgoingPhoneAccount(newAccount.getAccountHandle(),
837 newAccountUserHandle);
838 } else {
839 Log.v(this, "maybeReplaceOldAccount: group Ids are equal, but ComponentName is not" +
840 " the same as the default. Not replacing default PhoneAccount.");
841 }
842 PhoneAccount replacementAccount = getPhoneAccountByGroupId(newAccount.getGroupId(),
843 newAccount.getAccountHandle().getComponentName(), newAccountUserHandle,
844 newAccount.getAccountHandle());
845 if (replacementAccount != null) {
846 // Unregister the old PhoneAccount.
847 Log.v(this, "maybeReplaceOldAccount: Unregistering old PhoneAccount: " +
848 replacementAccount.getAccountHandle());
849 unregisterPhoneAccount(replacementAccount.getAccountHandle());
850 }
851 }
852
Tyler Gunncb59b672014-08-20 09:02:11 -0700853 /**
Santos Cordon6a212642015-05-08 16:35:23 -0700854 * Determines if the connection service specified by a {@link PhoneAccountHandle} requires the
Yorke Lee7bb8ce92015-05-13 16:28:29 -0700855 * {@link Manifest.permission#BIND_TELECOM_CONNECTION_SERVICE} permission.
Tyler Gunncb59b672014-08-20 09:02:11 -0700856 *
857 * @param phoneAccountHandle The phone account to check.
858 * @return {@code True} if the phone account has permission.
859 */
Santos Cordon6a212642015-05-08 16:35:23 -0700860 public boolean phoneAccountRequiresBindPermission(PhoneAccountHandle phoneAccountHandle) {
Sailesh Nepal91fc8092015-02-14 15:44:55 -0800861 List<ResolveInfo> resolveInfos = resolveComponent(phoneAccountHandle);
862 if (resolveInfos.isEmpty()) {
863 Log.w(this, "phoneAccount %s not found", phoneAccountHandle.getComponentName());
Tyler Gunncb59b672014-08-20 09:02:11 -0700864 return false;
865 }
Sailesh Nepal91fc8092015-02-14 15:44:55 -0800866 for (ResolveInfo resolveInfo : resolveInfos) {
867 ServiceInfo serviceInfo = resolveInfo.serviceInfo;
Yorke Lee7bb8ce92015-05-13 16:28:29 -0700868 if (serviceInfo == null) {
869 return false;
870 }
871
872 if (!Manifest.permission.BIND_CONNECTION_SERVICE.equals(serviceInfo.permission) &&
873 !Manifest.permission.BIND_TELECOM_CONNECTION_SERVICE.equals(
874 serviceInfo.permission)) {
875 // The ConnectionService must require either the deprecated BIND_CONNECTION_SERVICE,
876 // or the public BIND_TELECOM_CONNECTION_SERVICE permissions, both of which are
877 // system/signature only.
Sailesh Nepal91fc8092015-02-14 15:44:55 -0800878 return false;
879 }
880 }
881 return true;
Tyler Gunncb59b672014-08-20 09:02:11 -0700882 }
883
Santos Cordon6a212642015-05-08 16:35:23 -0700884 //
885 // Methods for retrieving PhoneAccounts and PhoneAccountHandles
886 //
Ihab Awad293edf22014-07-24 17:52:29 -0700887
Santos Cordonafe59e52014-08-22 16:48:43 -0700888 /**
Santos Cordon6a212642015-05-08 16:35:23 -0700889 * Returns the PhoneAccount for the specified handle. Does no user checking.
Tyler Gunn84253572014-09-02 14:50:05 -0700890 *
Santos Cordon6a212642015-05-08 16:35:23 -0700891 * @param handle
892 * @return The corresponding phone account if one exists.
Santos Cordonafe59e52014-08-22 16:48:43 -0700893 */
Tony Mak98e6bdc2015-11-25 21:54:05 +0000894 public PhoneAccount getPhoneAccountUnchecked(PhoneAccountHandle handle) {
Santos Cordon6a212642015-05-08 16:35:23 -0700895 for (PhoneAccount m : mState.accounts) {
896 if (Objects.equals(handle, m.getAccountHandle())) {
897 return m;
898 }
899 }
900 return null;
901 }
902
903 /**
904 * Like getPhoneAccount, but checks to see if the current user is allowed to see the phone
905 * account before returning it. The current user is the active user on the actual android
906 * device.
907 */
Tony Mak240656f2015-12-04 11:36:22 +0000908 public PhoneAccount getPhoneAccount(PhoneAccountHandle handle, UserHandle userHandle) {
909 return getPhoneAccount(handle, userHandle, /* acrossProfiles */ false);
910 }
911
912 public PhoneAccount getPhoneAccount(PhoneAccountHandle handle,
913 UserHandle userHandle, boolean acrossProfiles) {
Tony Mak98e6bdc2015-11-25 21:54:05 +0000914 PhoneAccount account = getPhoneAccountUnchecked(handle);
Tony Mak240656f2015-12-04 11:36:22 +0000915 if (account != null && (isVisibleForUser(account, userHandle, acrossProfiles))) {
Santos Cordon6a212642015-05-08 16:35:23 -0700916 return account;
917 }
918 return null;
919 }
920
Tony Mak240656f2015-12-04 11:36:22 +0000921 public PhoneAccount getPhoneAccountOfCurrentUser(PhoneAccountHandle handle) {
922 return getPhoneAccount(handle, mCurrentUserHandle);
923 }
924
Bryce Leea0bb7052015-10-16 13:31:40 -0700925 private List<PhoneAccountHandle> getPhoneAccountHandles(
926 int capabilities,
927 String uriScheme,
928 String packageName,
Tony Mak240656f2015-12-04 11:36:22 +0000929 boolean includeDisabledAccounts,
930 UserHandle userHandle) {
Bryce Leea0bb7052015-10-16 13:31:40 -0700931 return getPhoneAccountHandles(capabilities, 0 /*excludedCapabilities*/, uriScheme,
Tony Mak240656f2015-12-04 11:36:22 +0000932 packageName, includeDisabledAccounts, userHandle);
Bryce Leea0bb7052015-10-16 13:31:40 -0700933 }
934
Santos Cordon6a212642015-05-08 16:35:23 -0700935 /**
936 * Returns a list of phone account handles with the specified capabilities, uri scheme,
937 * and package name.
938 */
939 private List<PhoneAccountHandle> getPhoneAccountHandles(
Santos Cordonea5cb932015-05-07 16:28:38 -0700940 int capabilities,
Bryce Leea0bb7052015-10-16 13:31:40 -0700941 int excludedCapabilities,
Santos Cordonea5cb932015-05-07 16:28:38 -0700942 String uriScheme,
943 String packageName,
Tony Mak240656f2015-12-04 11:36:22 +0000944 boolean includeDisabledAccounts,
945 UserHandle userHandle) {
Santos Cordon6a212642015-05-08 16:35:23 -0700946 List<PhoneAccountHandle> handles = new ArrayList<>();
Santos Cordonea5cb932015-05-07 16:28:38 -0700947
948 for (PhoneAccount account : getPhoneAccounts(
Bryce Leea0bb7052015-10-16 13:31:40 -0700949 capabilities, excludedCapabilities, uriScheme, packageName,
Tony Mak240656f2015-12-04 11:36:22 +0000950 includeDisabledAccounts, userHandle)) {
Santos Cordon6a212642015-05-08 16:35:23 -0700951 handles.add(account.getAccountHandle());
952 }
953 return handles;
Tyler Gunn84253572014-09-02 14:50:05 -0700954 }
955
Bryce Leea0bb7052015-10-16 13:31:40 -0700956 private List<PhoneAccount> getPhoneAccounts(
957 int capabilities,
958 String uriScheme,
959 String packageName,
Tony Mak240656f2015-12-04 11:36:22 +0000960 boolean includeDisabledAccounts,
961 UserHandle userHandle) {
Bryce Leea0bb7052015-10-16 13:31:40 -0700962 return getPhoneAccounts(capabilities, 0 /*excludedCapabilities*/, uriScheme, packageName,
Tony Mak240656f2015-12-04 11:36:22 +0000963 includeDisabledAccounts, userHandle);
Bryce Leea0bb7052015-10-16 13:31:40 -0700964 }
965
Tyler Gunn84253572014-09-02 14:50:05 -0700966 /**
967 * Returns a list of phone account handles with the specified flag, supporting the specified
Santos Cordon6a212642015-05-08 16:35:23 -0700968 * URI scheme, within the specified package name.
Tyler Gunn84253572014-09-02 14:50:05 -0700969 *
Santos Cordon6a212642015-05-08 16:35:23 -0700970 * @param capabilities Capabilities which the {@code PhoneAccount} must have. Ignored if 0.
Bryce Leea0bb7052015-10-16 13:31:40 -0700971 * @param excludedCapabilities Capabilities which the {@code PhoneAccount} must not have.
972 * Ignored if 0.
Santos Cordon6a212642015-05-08 16:35:23 -0700973 * @param uriScheme URI schemes the PhoneAccount must handle. {@code null} bypasses the
Tyler Gunn84253572014-09-02 14:50:05 -0700974 * URI scheme check.
Santos Cordon6a212642015-05-08 16:35:23 -0700975 * @param packageName Package name of the PhoneAccount. {@code null} bypasses packageName check.
Tyler Gunn84253572014-09-02 14:50:05 -0700976 */
Santos Cordon6a212642015-05-08 16:35:23 -0700977 private List<PhoneAccount> getPhoneAccounts(
Santos Cordonea5cb932015-05-07 16:28:38 -0700978 int capabilities,
Bryce Leea0bb7052015-10-16 13:31:40 -0700979 int excludedCapabilities,
Santos Cordonea5cb932015-05-07 16:28:38 -0700980 String uriScheme,
981 String packageName,
Tony Mak240656f2015-12-04 11:36:22 +0000982 boolean includeDisabledAccounts,
983 UserHandle userHandle) {
Santos Cordon6a212642015-05-08 16:35:23 -0700984 List<PhoneAccount> accounts = new ArrayList<>(mState.accounts.size());
Ihab Awad293edf22014-07-24 17:52:29 -0700985 for (PhoneAccount m : mState.accounts) {
Santos Cordonea5cb932015-05-07 16:28:38 -0700986 if (!(m.isEnabled() || includeDisabledAccounts)) {
987 // Do not include disabled accounts.
988 continue;
989 }
990
Bryce Leea0bb7052015-10-16 13:31:40 -0700991 if ((m.getCapabilities() & excludedCapabilities) != 0) {
992 // If an excluded capability is present, skip.
993 continue;
994 }
995
Santos Cordon6a212642015-05-08 16:35:23 -0700996 if (capabilities != 0 && !m.hasCapabilities(capabilities)) {
Evan Charlton105d9772014-11-25 14:08:53 -0800997 // Account doesn't have the right capabilities; skip this one.
998 continue;
Ihab Awadf2a84912014-07-22 21:09:25 -0700999 }
Evan Charlton105d9772014-11-25 14:08:53 -08001000 if (uriScheme != null && !m.supportsUriScheme(uriScheme)) {
1001 // Account doesn't support this URI scheme; skip this one.
1002 continue;
1003 }
Santos Cordon6a212642015-05-08 16:35:23 -07001004 PhoneAccountHandle handle = m.getAccountHandle();
1005
1006 if (resolveComponent(handle).isEmpty()) {
Evan Charlton105d9772014-11-25 14:08:53 -08001007 // This component cannot be resolved anymore; skip this one.
1008 continue;
1009 }
Santos Cordon6a212642015-05-08 16:35:23 -07001010 if (packageName != null &&
1011 !packageName.equals(handle.getComponentName().getPackageName())) {
1012 // Not the right package name; skip this one.
1013 continue;
1014 }
Tony Mak240656f2015-12-04 11:36:22 +00001015 if (!isVisibleForUser(m, userHandle, false)) {
Evan Charlton105d9772014-11-25 14:08:53 -08001016 // Account is not visible for the current user; skip this one.
1017 continue;
1018 }
Santos Cordon6a212642015-05-08 16:35:23 -07001019 accounts.add(m);
Ihab Awad104f8062014-07-17 11:29:35 -07001020 }
Santos Cordon6a212642015-05-08 16:35:23 -07001021 return accounts;
Ihab Awad104f8062014-07-17 11:29:35 -07001022 }
1023
Santos Cordon6a212642015-05-08 16:35:23 -07001024 //
1025 // State Implementation for PhoneAccountRegistrar
1026 //
1027
Ihab Awad293edf22014-07-24 17:52:29 -07001028 /**
1029 * The state of this {@code PhoneAccountRegistrar}.
1030 */
Ihab Awadb78b2762014-07-25 15:16:23 -07001031 @VisibleForTesting
1032 public static class State {
Ihab Awad293edf22014-07-24 17:52:29 -07001033 /**
Tony Mak4a3e2fd2015-12-04 11:58:38 +00001034 * Store the default phone account handle of users. If no record of a user can be found in
1035 * the map, it means that no default phone account handle is set in that user.
Ihab Awad293edf22014-07-24 17:52:29 -07001036 */
Tony Mak4a3e2fd2015-12-04 11:58:38 +00001037 public final Map<UserHandle, DefaultPhoneAccountHandle> defaultOutgoingAccountHandles
1038 = new ConcurrentHashMap<>();
Ihab Awad293edf22014-07-24 17:52:29 -07001039
1040 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -07001041 * The complete list of {@code PhoneAccount}s known to the Telecom subsystem.
Ihab Awad293edf22014-07-24 17:52:29 -07001042 */
Shriram Ganesh8f89f8d2015-04-15 12:35:43 +05301043 public final List<PhoneAccount> accounts = new CopyOnWriteArrayList<>();
Tyler Gunn84253572014-09-02 14:50:05 -07001044
1045 /**
1046 * The version number of the State data.
1047 */
1048 public int versionNumber;
Ihab Awad293edf22014-07-24 17:52:29 -07001049 }
1050
Tyler Gunn9787e0e2014-10-14 14:36:12 -07001051 /**
Tony Mak4a3e2fd2015-12-04 11:58:38 +00001052 * The default {@link PhoneAccountHandle} of a user.
1053 */
1054 public static class DefaultPhoneAccountHandle {
1055
1056 public final UserHandle userHandle;
1057
1058 public final PhoneAccountHandle phoneAccountHandle;
1059
Brad Ebinger0c87b3c2016-06-10 17:23:02 -07001060 public final String groupId;
1061
Tony Mak4a3e2fd2015-12-04 11:58:38 +00001062 public DefaultPhoneAccountHandle(UserHandle userHandle,
Brad Ebinger0c87b3c2016-06-10 17:23:02 -07001063 PhoneAccountHandle phoneAccountHandle, String groupId) {
Tony Mak4a3e2fd2015-12-04 11:58:38 +00001064 this.userHandle = userHandle;
1065 this.phoneAccountHandle = phoneAccountHandle;
Brad Ebinger0c87b3c2016-06-10 17:23:02 -07001066 this.groupId = groupId;
Tony Mak4a3e2fd2015-12-04 11:58:38 +00001067 }
1068 }
1069
1070 /**
Tyler Gunn9787e0e2014-10-14 14:36:12 -07001071 * Dumps the state of the {@link CallsManager}.
1072 *
1073 * @param pw The {@code IndentingPrintWriter} to write the state to.
1074 */
1075 public void dump(IndentingPrintWriter pw) {
1076 if (mState != null) {
1077 pw.println("xmlVersion: " + mState.versionNumber);
Tony Mak4a3e2fd2015-12-04 11:58:38 +00001078 DefaultPhoneAccountHandle defaultPhoneAccountHandle
1079 = mState.defaultOutgoingAccountHandles.get(Process.myUserHandle());
1080 pw.println("defaultOutgoing: " + (defaultPhoneAccountHandle == null ? "none" :
1081 defaultPhoneAccountHandle.phoneAccountHandle));
Tony Mak240656f2015-12-04 11:36:22 +00001082 pw.println("simCallManager: " + getSimCallManager(mCurrentUserHandle));
Tyler Gunn9787e0e2014-10-14 14:36:12 -07001083 pw.println("phoneAccounts:");
1084 pw.increaseIndent();
1085 for (PhoneAccount phoneAccount : mState.accounts) {
1086 pw.println(phoneAccount);
1087 }
1088 pw.decreaseIndent();
1089 }
1090 }
1091
Ihab Awad293edf22014-07-24 17:52:29 -07001092 ////////////////////////////////////////////////////////////////////////////////////////////////
1093 //
1094 // State management
1095 //
1096
Hall Liu3f7f64d2016-01-07 17:40:13 -08001097 private class AsyncXmlWriter extends AsyncTask<ByteArrayOutputStream, Void, Void> {
1098 @Override
1099 public Void doInBackground(ByteArrayOutputStream... args) {
1100 final ByteArrayOutputStream buffer = args[0];
1101 FileOutputStream fileOutput = null;
Ihab Awadb78b2762014-07-25 15:16:23 -07001102 try {
Hall Liu3f7f64d2016-01-07 17:40:13 -08001103 synchronized (mWriteLock) {
1104 fileOutput = mAtomicFile.startWrite();
1105 buffer.writeTo(fileOutput);
1106 mAtomicFile.finishWrite(fileOutput);
Ihab Awadb78b2762014-07-25 15:16:23 -07001107 }
Hall Liu3f7f64d2016-01-07 17:40:13 -08001108 } catch (IOException e) {
1109 Log.e(this, e, "Writing state to XML file");
1110 mAtomicFile.failWrite(fileOutput);
Ihab Awadb78b2762014-07-25 15:16:23 -07001111 }
Hall Liu3f7f64d2016-01-07 17:40:13 -08001112 return null;
1113 }
1114 }
1115
1116 private void write() {
1117 try {
1118 ByteArrayOutputStream os = new ByteArrayOutputStream();
1119 XmlSerializer serializer = new FastXmlSerializer();
1120 serializer.setOutput(os, "utf-8");
1121 writeToXml(mState, serializer, mContext);
1122 serializer.flush();
1123 new AsyncXmlWriter().execute(os);
Ihab Awadb78b2762014-07-25 15:16:23 -07001124 } catch (IOException e) {
Hall Liu3f7f64d2016-01-07 17:40:13 -08001125 Log.e(this, e, "Writing state to XML buffer");
Ihab Awad104f8062014-07-17 11:29:35 -07001126 }
1127 }
1128
Ihab Awadb78b2762014-07-25 15:16:23 -07001129 private void read() {
1130 final InputStream is;
Ihab Awad104f8062014-07-17 11:29:35 -07001131 try {
Ihab Awadb78b2762014-07-25 15:16:23 -07001132 is = mAtomicFile.openRead();
1133 } catch (FileNotFoundException ex) {
1134 return;
1135 }
1136
Tyler Gunn84253572014-09-02 14:50:05 -07001137 boolean versionChanged = false;
1138
Ihab Awadb78b2762014-07-25 15:16:23 -07001139 XmlPullParser parser;
1140 try {
1141 parser = Xml.newPullParser();
1142 parser.setInput(new BufferedInputStream(is), null);
1143 parser.nextTag();
Tyler Gunn84253572014-09-02 14:50:05 -07001144 mState = readFromXml(parser, mContext);
1145 versionChanged = mState.versionNumber < EXPECTED_STATE_VERSION;
1146
Ihab Awadb78b2762014-07-25 15:16:23 -07001147 } catch (IOException | XmlPullParserException e) {
1148 Log.e(this, e, "Reading state from XML file");
1149 mState = new State();
1150 } finally {
1151 try {
1152 is.close();
1153 } catch (IOException e) {
1154 Log.e(this, e, "Closing InputStream");
1155 }
Ihab Awad104f8062014-07-17 11:29:35 -07001156 }
Tyler Gunn84253572014-09-02 14:50:05 -07001157
Evan Charlton105d9772014-11-25 14:08:53 -08001158 // Verify all of the UserHandles.
1159 List<PhoneAccount> badAccounts = new ArrayList<>();
1160 for (PhoneAccount phoneAccount : mState.accounts) {
1161 UserHandle userHandle = phoneAccount.getAccountHandle().getUserHandle();
1162 if (userHandle == null) {
1163 Log.w(this, "Missing UserHandle for %s", phoneAccount);
1164 badAccounts.add(phoneAccount);
1165 } else if (mUserManager.getSerialNumberForUser(userHandle) == -1) {
1166 Log.w(this, "User does not exist for %s", phoneAccount);
1167 badAccounts.add(phoneAccount);
1168 }
1169 }
1170 mState.accounts.removeAll(badAccounts);
1171
Tyler Gunn84253572014-09-02 14:50:05 -07001172 // If an upgrade occurred, write out the changed data.
Evan Charlton105d9772014-11-25 14:08:53 -08001173 if (versionChanged || !badAccounts.isEmpty()) {
Tyler Gunn84253572014-09-02 14:50:05 -07001174 write();
1175 }
Santos Cordon176ae282014-07-14 02:02:14 -07001176 }
1177
Evan Charlton105d9772014-11-25 14:08:53 -08001178 private static void writeToXml(State state, XmlSerializer serializer, Context context)
Ihab Awadb78b2762014-07-25 15:16:23 -07001179 throws IOException {
Evan Charlton105d9772014-11-25 14:08:53 -08001180 sStateXml.writeToXml(state, serializer, context);
Santos Cordon176ae282014-07-14 02:02:14 -07001181 }
Ihab Awad104f8062014-07-17 11:29:35 -07001182
Tyler Gunn84253572014-09-02 14:50:05 -07001183 private static State readFromXml(XmlPullParser parser, Context context)
Ihab Awadb78b2762014-07-25 15:16:23 -07001184 throws IOException, XmlPullParserException {
Tyler Gunn84253572014-09-02 14:50:05 -07001185 State s = sStateXml.readFromXml(parser, 0, context);
Ihab Awadb78b2762014-07-25 15:16:23 -07001186 return s != null ? s : new State();
Ihab Awad104f8062014-07-17 11:29:35 -07001187 }
1188
Ihab Awad293edf22014-07-24 17:52:29 -07001189 ////////////////////////////////////////////////////////////////////////////////////////////////
Ihab Awad104f8062014-07-17 11:29:35 -07001190 //
Ihab Awadb78b2762014-07-25 15:16:23 -07001191 // XML serialization
Ihab Awad104f8062014-07-17 11:29:35 -07001192 //
1193
Ihab Awadb78b2762014-07-25 15:16:23 -07001194 @VisibleForTesting
Ihab Awad26923222014-07-30 10:54:35 -07001195 public abstract static class XmlSerialization<T> {
Tyler Gunn369c8742015-10-07 15:39:28 -07001196 private static final String TAG_VALUE = "value";
1197 private static final String ATTRIBUTE_LENGTH = "length";
1198 private static final String ATTRIBUTE_KEY = "key";
1199 private static final String ATTRIBUTE_VALUE_TYPE = "type";
1200 private static final String VALUE_TYPE_STRING = "string";
1201 private static final String VALUE_TYPE_INTEGER = "integer";
1202 private static final String VALUE_TYPE_BOOLEAN = "boolean";
Tyler Gunn84253572014-09-02 14:50:05 -07001203
Ihab Awadb78b2762014-07-25 15:16:23 -07001204 /**
1205 * Write the supplied object to XML
1206 */
Evan Charlton105d9772014-11-25 14:08:53 -08001207 public abstract void writeToXml(T o, XmlSerializer serializer, Context context)
Ihab Awad26923222014-07-30 10:54:35 -07001208 throws IOException;
Ihab Awadb78b2762014-07-25 15:16:23 -07001209
1210 /**
1211 * Read from the supplied XML into a new object, returning null in case of an
1212 * unrecoverable schema mismatch or other data error. 'parser' must be already
1213 * positioned at the first tag that is expected to have been emitted by this
1214 * object's writeToXml(). This object tries to fail early without modifying
1215 * 'parser' if it does not recognize the data it sees.
1216 */
Tyler Gunn84253572014-09-02 14:50:05 -07001217 public abstract T readFromXml(XmlPullParser parser, int version, Context context)
Ihab Awad26923222014-07-30 10:54:35 -07001218 throws IOException, XmlPullParserException;
1219
Ihab Awadd9f54382014-10-24 11:44:47 -07001220 protected void writeTextIfNonNull(String tagName, Object value, XmlSerializer serializer)
Ihab Awad26923222014-07-30 10:54:35 -07001221 throws IOException {
1222 if (value != null) {
1223 serializer.startTag(null, tagName);
1224 serializer.text(Objects.toString(value));
1225 serializer.endTag(null, tagName);
1226 }
1227 }
Tyler Gunn84253572014-09-02 14:50:05 -07001228
1229 /**
1230 * Serializes a string array.
1231 *
1232 * @param tagName The tag name for the string array.
1233 * @param values The string values to serialize.
1234 * @param serializer The serializer.
1235 * @throws IOException
1236 */
1237 protected void writeStringList(String tagName, List<String> values,
1238 XmlSerializer serializer)
1239 throws IOException {
1240
1241 serializer.startTag(null, tagName);
1242 if (values != null) {
Tyler Gunn369c8742015-10-07 15:39:28 -07001243 serializer.attribute(null, ATTRIBUTE_LENGTH, Objects.toString(values.size()));
Tyler Gunn84253572014-09-02 14:50:05 -07001244 for (String toSerialize : values) {
Tyler Gunn369c8742015-10-07 15:39:28 -07001245 serializer.startTag(null, TAG_VALUE);
Tyler Gunn84253572014-09-02 14:50:05 -07001246 if (toSerialize != null ){
1247 serializer.text(toSerialize);
1248 }
Tyler Gunn369c8742015-10-07 15:39:28 -07001249 serializer.endTag(null, TAG_VALUE);
Tyler Gunn84253572014-09-02 14:50:05 -07001250 }
1251 } else {
Tyler Gunn369c8742015-10-07 15:39:28 -07001252 serializer.attribute(null, ATTRIBUTE_LENGTH, "0");
1253 }
1254 serializer.endTag(null, tagName);
1255 }
1256
1257 protected void writeBundle(String tagName, Bundle values, XmlSerializer serializer)
1258 throws IOException {
1259
1260 serializer.startTag(null, tagName);
1261 if (values != null) {
1262 for (String key : values.keySet()) {
1263 Object value = values.get(key);
1264
1265 if (value == null) {
1266 continue;
1267 }
1268
1269 String valueType;
1270 if (value instanceof String) {
1271 valueType = VALUE_TYPE_STRING;
1272 } else if (value instanceof Integer) {
1273 valueType = VALUE_TYPE_INTEGER;
1274 } else if (value instanceof Boolean) {
1275 valueType = VALUE_TYPE_BOOLEAN;
1276 } else {
1277 Log.w(this,
1278 "PhoneAccounts support only string, integer and boolean extras TY.");
1279 continue;
1280 }
1281
1282 serializer.startTag(null, TAG_VALUE);
1283 serializer.attribute(null, ATTRIBUTE_KEY, key);
1284 serializer.attribute(null, ATTRIBUTE_VALUE_TYPE, valueType);
1285 serializer.text(Objects.toString(value));
1286 serializer.endTag(null, TAG_VALUE);
1287 }
Tyler Gunn84253572014-09-02 14:50:05 -07001288 }
1289 serializer.endTag(null, tagName);
Ihab Awadd9f54382014-10-24 11:44:47 -07001290 }
Tyler Gunn84253572014-09-02 14:50:05 -07001291
Santos Cordon9c30c282015-05-13 16:28:27 -07001292 protected void writeIconIfNonNull(String tagName, Icon value, XmlSerializer serializer)
Ihab Awadd9f54382014-10-24 11:44:47 -07001293 throws IOException {
Santos Cordon9c30c282015-05-13 16:28:27 -07001294 if (value != null) {
Ihab Awadd9f54382014-10-24 11:44:47 -07001295 ByteArrayOutputStream stream = new ByteArrayOutputStream();
Santos Cordon9c30c282015-05-13 16:28:27 -07001296 value.writeToStream(stream);
1297 byte[] iconByteArray = stream.toByteArray();
1298 String text = Base64.encodeToString(iconByteArray, 0, iconByteArray.length, 0);
Ihab Awadd9f54382014-10-24 11:44:47 -07001299
1300 serializer.startTag(null, tagName);
1301 serializer.text(text);
1302 serializer.endTag(null, tagName);
1303 }
Tyler Gunn84253572014-09-02 14:50:05 -07001304 }
1305
Evan Charlton105d9772014-11-25 14:08:53 -08001306 protected void writeLong(String tagName, long value, XmlSerializer serializer)
1307 throws IOException {
1308 serializer.startTag(null, tagName);
1309 serializer.text(Long.valueOf(value).toString());
1310 serializer.endTag(null, tagName);
1311 }
1312
Brad Ebinger0c87b3c2016-06-10 17:23:02 -07001313 protected void writeNonNullString(String tagName, String value, XmlSerializer serializer)
1314 throws IOException {
1315 serializer.startTag(null, tagName);
1316 serializer.text(value != null ? value : "");
1317 serializer.endTag(null, tagName);
1318 }
1319
Tyler Gunn84253572014-09-02 14:50:05 -07001320 /**
1321 * Reads a string array from the XML parser.
1322 *
1323 * @param parser The XML parser.
1324 * @return String array containing the parsed values.
1325 * @throws IOException Exception related to IO.
1326 * @throws XmlPullParserException Exception related to parsing.
1327 */
1328 protected List<String> readStringList(XmlPullParser parser)
1329 throws IOException, XmlPullParserException {
1330
Tyler Gunn369c8742015-10-07 15:39:28 -07001331 int length = Integer.parseInt(parser.getAttributeValue(null, ATTRIBUTE_LENGTH));
Tyler Gunn84253572014-09-02 14:50:05 -07001332 List<String> arrayEntries = new ArrayList<String>(length);
1333 String value = null;
1334
1335 if (length == 0) {
1336 return arrayEntries;
1337 }
1338
1339 int outerDepth = parser.getDepth();
1340 while (XmlUtils.nextElementWithin(parser, outerDepth)) {
Tyler Gunn369c8742015-10-07 15:39:28 -07001341 if (parser.getName().equals(TAG_VALUE)) {
Tyler Gunn8e0fef42014-09-08 18:34:44 -07001342 parser.next();
Tyler Gunn84253572014-09-02 14:50:05 -07001343 value = parser.getText();
1344 arrayEntries.add(value);
1345 }
1346 }
1347
1348 return arrayEntries;
1349 }
Ihab Awadd9f54382014-10-24 11:44:47 -07001350
Tyler Gunn369c8742015-10-07 15:39:28 -07001351 /**
1352 * Reads a bundle from the XML parser.
1353 *
1354 * @param parser The XML parser.
1355 * @return Bundle containing the parsed values.
1356 * @throws IOException Exception related to IO.
1357 * @throws XmlPullParserException Exception related to parsing.
1358 */
1359 protected Bundle readBundle(XmlPullParser parser)
1360 throws IOException, XmlPullParserException {
1361
1362 Bundle bundle = null;
1363 int outerDepth = parser.getDepth();
1364 while (XmlUtils.nextElementWithin(parser, outerDepth)) {
1365 if (parser.getName().equals(TAG_VALUE)) {
1366 String valueType = parser.getAttributeValue(null, ATTRIBUTE_VALUE_TYPE);
1367 String key = parser.getAttributeValue(null, ATTRIBUTE_KEY);
1368 parser.next();
1369 String value = parser.getText();
1370
1371 if (bundle == null) {
1372 bundle = new Bundle();
1373 }
1374
1375 // Do not write null values to the bundle.
1376 if (value == null) {
1377 continue;
1378 }
1379
1380 if (VALUE_TYPE_STRING.equals(valueType)) {
1381 bundle.putString(key, value);
1382 } else if (VALUE_TYPE_INTEGER.equals(valueType)) {
1383 try {
1384 int intValue = Integer.parseInt(value);
1385 bundle.putInt(key, intValue);
1386 } catch (NumberFormatException nfe) {
1387 Log.w(this, "Invalid integer PhoneAccount extra.");
1388 }
1389 } else if (VALUE_TYPE_BOOLEAN.equals(valueType)) {
1390 boolean boolValue = Boolean.parseBoolean(value);
1391 bundle.putBoolean(key, boolValue);
1392 } else {
1393 Log.w(this, "Invalid type " + valueType + " for PhoneAccount bundle.");
1394 }
1395 }
1396 }
1397 return bundle;
1398 }
1399
Santos Cordon9c30c282015-05-13 16:28:27 -07001400 protected Bitmap readBitmap(XmlPullParser parser) {
Ihab Awadd9f54382014-10-24 11:44:47 -07001401 byte[] imageByteArray = Base64.decode(parser.getText(), 0);
1402 return BitmapFactory.decodeByteArray(imageByteArray, 0, imageByteArray.length);
1403 }
Santos Cordon9c30c282015-05-13 16:28:27 -07001404
1405 protected Icon readIcon(XmlPullParser parser) throws IOException {
1406 byte[] iconByteArray = Base64.decode(parser.getText(), 0);
1407 ByteArrayInputStream stream = new ByteArrayInputStream(iconByteArray);
1408 return Icon.createFromStream(stream);
1409 }
Ihab Awad104f8062014-07-17 11:29:35 -07001410 }
1411
Ihab Awadb78b2762014-07-25 15:16:23 -07001412 @VisibleForTesting
1413 public static final XmlSerialization<State> sStateXml =
1414 new XmlSerialization<State>() {
1415 private static final String CLASS_STATE = "phone_account_registrar_state";
Ihab Awad104f8062014-07-17 11:29:35 -07001416 private static final String DEFAULT_OUTGOING = "default_outgoing";
1417 private static final String ACCOUNTS = "accounts";
Tyler Gunn84253572014-09-02 14:50:05 -07001418 private static final String VERSION = "version";
Ihab Awad104f8062014-07-17 11:29:35 -07001419
1420 @Override
Evan Charlton105d9772014-11-25 14:08:53 -08001421 public void writeToXml(State o, XmlSerializer serializer, Context context)
Ihab Awadb78b2762014-07-25 15:16:23 -07001422 throws IOException {
Ihab Awad26923222014-07-30 10:54:35 -07001423 if (o != null) {
1424 serializer.startTag(null, CLASS_STATE);
Tyler Gunn84253572014-09-02 14:50:05 -07001425 serializer.attribute(null, VERSION, Objects.toString(EXPECTED_STATE_VERSION));
Ihab Awadb78b2762014-07-25 15:16:23 -07001426
Tony Mak4a3e2fd2015-12-04 11:58:38 +00001427 serializer.startTag(null, DEFAULT_OUTGOING);
1428 for (DefaultPhoneAccountHandle defaultPhoneAccountHandle : o
1429 .defaultOutgoingAccountHandles.values()) {
1430 sDefaultPhoneAcountHandleXml
1431 .writeToXml(defaultPhoneAccountHandle, serializer, context);
Ihab Awad26923222014-07-30 10:54:35 -07001432 }
Tony Mak4a3e2fd2015-12-04 11:58:38 +00001433 serializer.endTag(null, DEFAULT_OUTGOING);
Ihab Awad26923222014-07-30 10:54:35 -07001434
Ihab Awad26923222014-07-30 10:54:35 -07001435 serializer.startTag(null, ACCOUNTS);
1436 for (PhoneAccount m : o.accounts) {
Evan Charlton105d9772014-11-25 14:08:53 -08001437 sPhoneAccountXml.writeToXml(m, serializer, context);
Ihab Awad26923222014-07-30 10:54:35 -07001438 }
1439 serializer.endTag(null, ACCOUNTS);
1440
1441 serializer.endTag(null, CLASS_STATE);
Ihab Awad293edf22014-07-24 17:52:29 -07001442 }
Ihab Awad104f8062014-07-17 11:29:35 -07001443 }
1444
1445 @Override
Tyler Gunn84253572014-09-02 14:50:05 -07001446 public State readFromXml(XmlPullParser parser, int version, Context context)
Ihab Awadb78b2762014-07-25 15:16:23 -07001447 throws IOException, XmlPullParserException {
1448 if (parser.getName().equals(CLASS_STATE)) {
1449 State s = new State();
Tyler Gunn84253572014-09-02 14:50:05 -07001450
1451 String rawVersion = parser.getAttributeValue(null, VERSION);
Tony Mak4a3e2fd2015-12-04 11:58:38 +00001452 s.versionNumber = TextUtils.isEmpty(rawVersion) ? 1 : Integer.parseInt(rawVersion);
Tyler Gunn84253572014-09-02 14:50:05 -07001453
Ihab Awadb78b2762014-07-25 15:16:23 -07001454 int outerDepth = parser.getDepth();
1455 while (XmlUtils.nextElementWithin(parser, outerDepth)) {
1456 if (parser.getName().equals(DEFAULT_OUTGOING)) {
Tony Mak4a3e2fd2015-12-04 11:58:38 +00001457 if (s.versionNumber < 9) {
Brad Ebinger0c87b3c2016-06-10 17:23:02 -07001458 // Migrate old default phone account handle here by assuming the
1459 // default phone account handle belongs to the primary user. Also,
1460 // assume there are no groups.
Tony Mak4a3e2fd2015-12-04 11:58:38 +00001461 parser.nextTag();
1462 PhoneAccountHandle phoneAccountHandle = sPhoneAccountHandleXml
1463 .readFromXml(parser, s.versionNumber, context);
1464 UserManager userManager = UserManager.get(context);
1465 UserInfo primaryUser = userManager.getPrimaryUser();
1466 if (primaryUser != null) {
1467 UserHandle userHandle = primaryUser.getUserHandle();
1468 DefaultPhoneAccountHandle defaultPhoneAccountHandle
1469 = new DefaultPhoneAccountHandle(userHandle,
Brad Ebinger0c87b3c2016-06-10 17:23:02 -07001470 phoneAccountHandle, "" /* groupId */);
Tony Mak4a3e2fd2015-12-04 11:58:38 +00001471 s.defaultOutgoingAccountHandles
1472 .put(userHandle, defaultPhoneAccountHandle);
1473 }
1474 } else {
1475 int defaultAccountHandlesDepth = parser.getDepth();
1476 while (XmlUtils.nextElementWithin(parser, defaultAccountHandlesDepth)) {
1477 DefaultPhoneAccountHandle accountHandle
1478 = sDefaultPhoneAcountHandleXml
1479 .readFromXml(parser, s.versionNumber, context);
1480 if (accountHandle != null && s.accounts != null) {
1481 s.defaultOutgoingAccountHandles
1482 .put(accountHandle.userHandle, accountHandle);
1483 }
1484 }
1485 }
Ihab Awadb78b2762014-07-25 15:16:23 -07001486 } else if (parser.getName().equals(ACCOUNTS)) {
1487 int accountsDepth = parser.getDepth();
1488 while (XmlUtils.nextElementWithin(parser, accountsDepth)) {
Tyler Gunn84253572014-09-02 14:50:05 -07001489 PhoneAccount account = sPhoneAccountXml.readFromXml(parser,
1490 s.versionNumber, context);
1491
1492 if (account != null && s.accounts != null) {
Ihab Awadb78b2762014-07-25 15:16:23 -07001493 s.accounts.add(account);
1494 }
1495 }
Ihab Awad104f8062014-07-17 11:29:35 -07001496 }
1497 }
Ihab Awadb78b2762014-07-25 15:16:23 -07001498 return s;
Ihab Awad104f8062014-07-17 11:29:35 -07001499 }
Ihab Awadb78b2762014-07-25 15:16:23 -07001500 return null;
Ihab Awad104f8062014-07-17 11:29:35 -07001501 }
1502 };
1503
Ihab Awadb78b2762014-07-25 15:16:23 -07001504 @VisibleForTesting
Tony Mak4a3e2fd2015-12-04 11:58:38 +00001505 public static final XmlSerialization<DefaultPhoneAccountHandle> sDefaultPhoneAcountHandleXml =
1506 new XmlSerialization<DefaultPhoneAccountHandle>() {
1507 private static final String CLASS_DEFAULT_OUTGOING_PHONE_ACCOUNT_HANDLE
1508 = "default_outgoing_phone_account_handle";
1509 private static final String USER_SERIAL_NUMBER = "user_serial_number";
Brad Ebinger0c87b3c2016-06-10 17:23:02 -07001510 private static final String GROUP_ID = "group_id";
Tony Mak4a3e2fd2015-12-04 11:58:38 +00001511 private static final String ACCOUNT_HANDLE = "account_handle";
1512
1513 @Override
1514 public void writeToXml(DefaultPhoneAccountHandle o, XmlSerializer serializer,
1515 Context context) throws IOException {
1516 if (o != null) {
1517 final UserManager userManager = UserManager.get(context);
1518 final long serialNumber = userManager.getSerialNumberForUser(o.userHandle);
1519 if (serialNumber != -1) {
1520 serializer.startTag(null, CLASS_DEFAULT_OUTGOING_PHONE_ACCOUNT_HANDLE);
1521 writeLong(USER_SERIAL_NUMBER, serialNumber, serializer);
Brad Ebinger0c87b3c2016-06-10 17:23:02 -07001522 writeNonNullString(GROUP_ID, o.groupId, serializer);
Tony Mak4a3e2fd2015-12-04 11:58:38 +00001523 serializer.startTag(null, ACCOUNT_HANDLE);
1524 sPhoneAccountHandleXml.writeToXml(o.phoneAccountHandle, serializer,
1525 context);
1526 serializer.endTag(null, ACCOUNT_HANDLE);
1527 serializer.endTag(null, CLASS_DEFAULT_OUTGOING_PHONE_ACCOUNT_HANDLE);
1528 }
1529 }
1530 }
1531
1532 @Override
1533 public DefaultPhoneAccountHandle readFromXml(XmlPullParser parser, int version,
1534 Context context)
1535 throws IOException, XmlPullParserException {
1536 if (parser.getName().equals(CLASS_DEFAULT_OUTGOING_PHONE_ACCOUNT_HANDLE)) {
1537 int outerDepth = parser.getDepth();
1538 PhoneAccountHandle accountHandle = null;
1539 String userSerialNumberString = null;
Brad Ebinger0c87b3c2016-06-10 17:23:02 -07001540 String groupId = "";
Tony Mak4a3e2fd2015-12-04 11:58:38 +00001541 while (XmlUtils.nextElementWithin(parser, outerDepth)) {
1542 if (parser.getName().equals(ACCOUNT_HANDLE)) {
1543 parser.nextTag();
1544 accountHandle = sPhoneAccountHandleXml.readFromXml(parser, version,
1545 context);
1546 } else if (parser.getName().equals(USER_SERIAL_NUMBER)) {
1547 parser.next();
1548 userSerialNumberString = parser.getText();
Brad Ebinger0c87b3c2016-06-10 17:23:02 -07001549 } else if (parser.getName().equals(GROUP_ID)) {
Hall Liuded880c2017-01-20 16:13:07 -08001550 if (parser.next() == XmlPullParser.TEXT) {
1551 groupId = parser.getText();
1552 }
Tony Mak4a3e2fd2015-12-04 11:58:38 +00001553 }
1554 }
1555 UserHandle userHandle = null;
1556 if (userSerialNumberString != null) {
1557 try {
1558 long serialNumber = Long.parseLong(userSerialNumberString);
1559 userHandle = UserManager.get(context)
1560 .getUserForSerialNumber(serialNumber);
1561 } catch (NumberFormatException e) {
1562 Log.e(this, e,
1563 "Could not parse UserHandle " + userSerialNumberString);
1564 }
1565 }
Brad Ebinger0c87b3c2016-06-10 17:23:02 -07001566 if (accountHandle != null && userHandle != null && groupId != null) {
1567 return new DefaultPhoneAccountHandle(userHandle, accountHandle,
1568 groupId);
Tony Mak4a3e2fd2015-12-04 11:58:38 +00001569 }
1570 }
1571 return null;
1572 }
1573 };
1574
1575
1576 @VisibleForTesting
Ihab Awadb78b2762014-07-25 15:16:23 -07001577 public static final XmlSerialization<PhoneAccount> sPhoneAccountXml =
1578 new XmlSerialization<PhoneAccount>() {
1579 private static final String CLASS_PHONE_ACCOUNT = "phone_account";
1580 private static final String ACCOUNT_HANDLE = "account_handle";
Andrew Lee7129f1c2014-09-04 11:55:07 -07001581 private static final String ADDRESS = "handle";
1582 private static final String SUBSCRIPTION_ADDRESS = "subscription_number";
Ihab Awad104f8062014-07-17 11:29:35 -07001583 private static final String CAPABILITIES = "capabilities";
Christine Hallstromffe558c2016-11-30 16:05:13 -08001584 private static final String SUPPORTED_AUDIO_ROUTES = "supported_audio_routes";
Ihab Awad104f8062014-07-17 11:29:35 -07001585 private static final String ICON_RES_ID = "icon_res_id";
Ihab Awadd9f54382014-10-24 11:44:47 -07001586 private static final String ICON_PACKAGE_NAME = "icon_package_name";
1587 private static final String ICON_BITMAP = "icon_bitmap";
Ihab Awad7e2c7f32014-11-03 09:49:45 -08001588 private static final String ICON_TINT = "icon_tint";
1589 private static final String HIGHLIGHT_COLOR = "highlight_color";
Ihab Awad104f8062014-07-17 11:29:35 -07001590 private static final String LABEL = "label";
1591 private static final String SHORT_DESCRIPTION = "short_description";
Tyler Gunn84253572014-09-02 14:50:05 -07001592 private static final String SUPPORTED_URI_SCHEMES = "supported_uri_schemes";
Santos Cordon9c30c282015-05-13 16:28:27 -07001593 private static final String ICON = "icon";
Tyler Gunn369c8742015-10-07 15:39:28 -07001594 private static final String EXTRAS = "extras";
Santos Cordonea5cb932015-05-07 16:28:38 -07001595 private static final String ENABLED = "enabled";
Ihab Awad104f8062014-07-17 11:29:35 -07001596
1597 @Override
Evan Charlton105d9772014-11-25 14:08:53 -08001598 public void writeToXml(PhoneAccount o, XmlSerializer serializer, Context context)
Ihab Awadb78b2762014-07-25 15:16:23 -07001599 throws IOException {
Ihab Awad26923222014-07-30 10:54:35 -07001600 if (o != null) {
1601 serializer.startTag(null, CLASS_PHONE_ACCOUNT);
Ihab Awadb78b2762014-07-25 15:16:23 -07001602
Ihab Awad26923222014-07-30 10:54:35 -07001603 if (o.getAccountHandle() != null) {
1604 serializer.startTag(null, ACCOUNT_HANDLE);
Evan Charlton105d9772014-11-25 14:08:53 -08001605 sPhoneAccountHandleXml.writeToXml(o.getAccountHandle(), serializer, context);
Ihab Awad26923222014-07-30 10:54:35 -07001606 serializer.endTag(null, ACCOUNT_HANDLE);
1607 }
Ihab Awadb78b2762014-07-25 15:16:23 -07001608
Ihab Awadd9f54382014-10-24 11:44:47 -07001609 writeTextIfNonNull(ADDRESS, o.getAddress(), serializer);
1610 writeTextIfNonNull(SUBSCRIPTION_ADDRESS, o.getSubscriptionAddress(), serializer);
1611 writeTextIfNonNull(CAPABILITIES, Integer.toString(o.getCapabilities()), serializer);
Santos Cordon9c30c282015-05-13 16:28:27 -07001612 writeIconIfNonNull(ICON, o.getIcon(), serializer);
Ihab Awad7e2c7f32014-11-03 09:49:45 -08001613 writeTextIfNonNull(HIGHLIGHT_COLOR,
1614 Integer.toString(o.getHighlightColor()), serializer);
Ihab Awadd9f54382014-10-24 11:44:47 -07001615 writeTextIfNonNull(LABEL, o.getLabel(), serializer);
1616 writeTextIfNonNull(SHORT_DESCRIPTION, o.getShortDescription(), serializer);
Tyler Gunn84253572014-09-02 14:50:05 -07001617 writeStringList(SUPPORTED_URI_SCHEMES, o.getSupportedUriSchemes(), serializer);
Tyler Gunn369c8742015-10-07 15:39:28 -07001618 writeBundle(EXTRAS, o.getExtras(), serializer);
Santos Cordonea5cb932015-05-07 16:28:38 -07001619 writeTextIfNonNull(ENABLED, o.isEnabled() ? "true" : "false" , serializer);
Christine Hallstromffe558c2016-11-30 16:05:13 -08001620 writeTextIfNonNull(SUPPORTED_AUDIO_ROUTES, Integer.toString(
1621 o.getSupportedAudioRoutes()), serializer);
Ihab Awadb78b2762014-07-25 15:16:23 -07001622
Ihab Awad26923222014-07-30 10:54:35 -07001623 serializer.endTag(null, CLASS_PHONE_ACCOUNT);
1624 }
Ihab Awad104f8062014-07-17 11:29:35 -07001625 }
1626
Tyler Gunn84253572014-09-02 14:50:05 -07001627 public PhoneAccount readFromXml(XmlPullParser parser, int version, Context context)
Ihab Awadb78b2762014-07-25 15:16:23 -07001628 throws IOException, XmlPullParserException {
1629 if (parser.getName().equals(CLASS_PHONE_ACCOUNT)) {
1630 int outerDepth = parser.getDepth();
1631 PhoneAccountHandle accountHandle = null;
Andrew Lee7129f1c2014-09-04 11:55:07 -07001632 Uri address = null;
1633 Uri subscriptionAddress = null;
Ihab Awadb78b2762014-07-25 15:16:23 -07001634 int capabilities = 0;
Christine Hallstromffe558c2016-11-30 16:05:13 -08001635 int supportedAudioRoutes = 0;
Ihab Awad7e2c7f32014-11-03 09:49:45 -08001636 int iconResId = PhoneAccount.NO_RESOURCE_ID;
Ihab Awadd9f54382014-10-24 11:44:47 -07001637 String iconPackageName = null;
Ihab Awad7e2c7f32014-11-03 09:49:45 -08001638 Bitmap iconBitmap = null;
Ihab Awad07bc5ee2014-11-12 13:42:52 -08001639 int iconTint = PhoneAccount.NO_ICON_TINT;
1640 int highlightColor = PhoneAccount.NO_HIGHLIGHT_COLOR;
Ihab Awadb78b2762014-07-25 15:16:23 -07001641 String label = null;
1642 String shortDescription = null;
Tyler Gunn84253572014-09-02 14:50:05 -07001643 List<String> supportedUriSchemes = null;
Santos Cordon9c30c282015-05-13 16:28:27 -07001644 Icon icon = null;
Santos Cordonea5cb932015-05-07 16:28:38 -07001645 boolean enabled = false;
Tyler Gunn369c8742015-10-07 15:39:28 -07001646 Bundle extras = null;
Ihab Awadb78b2762014-07-25 15:16:23 -07001647
1648 while (XmlUtils.nextElementWithin(parser, outerDepth)) {
1649 if (parser.getName().equals(ACCOUNT_HANDLE)) {
1650 parser.nextTag();
Tyler Gunn84253572014-09-02 14:50:05 -07001651 accountHandle = sPhoneAccountHandleXml.readFromXml(parser, version,
1652 context);
Andrew Lee7129f1c2014-09-04 11:55:07 -07001653 } else if (parser.getName().equals(ADDRESS)) {
Ihab Awadb78b2762014-07-25 15:16:23 -07001654 parser.next();
Andrew Lee7129f1c2014-09-04 11:55:07 -07001655 address = Uri.parse(parser.getText());
1656 } else if (parser.getName().equals(SUBSCRIPTION_ADDRESS)) {
Ihab Awadb78b2762014-07-25 15:16:23 -07001657 parser.next();
Andrew Lee7129f1c2014-09-04 11:55:07 -07001658 String nextText = parser.getText();
1659 subscriptionAddress = nextText == null ? null : Uri.parse(nextText);
Ihab Awadb78b2762014-07-25 15:16:23 -07001660 } else if (parser.getName().equals(CAPABILITIES)) {
1661 parser.next();
1662 capabilities = Integer.parseInt(parser.getText());
1663 } else if (parser.getName().equals(ICON_RES_ID)) {
1664 parser.next();
1665 iconResId = Integer.parseInt(parser.getText());
Ihab Awadd9f54382014-10-24 11:44:47 -07001666 } else if (parser.getName().equals(ICON_PACKAGE_NAME)) {
1667 parser.next();
1668 iconPackageName = parser.getText();
1669 } else if (parser.getName().equals(ICON_BITMAP)) {
1670 parser.next();
Ihab Awad7e2c7f32014-11-03 09:49:45 -08001671 iconBitmap = readBitmap(parser);
1672 } else if (parser.getName().equals(ICON_TINT)) {
Nancy Chen06ce0622014-10-23 01:17:35 +00001673 parser.next();
Ihab Awad7e2c7f32014-11-03 09:49:45 -08001674 iconTint = Integer.parseInt(parser.getText());
1675 } else if (parser.getName().equals(HIGHLIGHT_COLOR)) {
1676 parser.next();
1677 highlightColor = Integer.parseInt(parser.getText());
Ihab Awadb78b2762014-07-25 15:16:23 -07001678 } else if (parser.getName().equals(LABEL)) {
1679 parser.next();
1680 label = parser.getText();
1681 } else if (parser.getName().equals(SHORT_DESCRIPTION)) {
1682 parser.next();
1683 shortDescription = parser.getText();
Tyler Gunn84253572014-09-02 14:50:05 -07001684 } else if (parser.getName().equals(SUPPORTED_URI_SCHEMES)) {
1685 supportedUriSchemes = readStringList(parser);
Santos Cordon9c30c282015-05-13 16:28:27 -07001686 } else if (parser.getName().equals(ICON)) {
1687 parser.next();
1688 icon = readIcon(parser);
Santos Cordonea5cb932015-05-07 16:28:38 -07001689 } else if (parser.getName().equals(ENABLED)) {
1690 parser.next();
1691 enabled = "true".equalsIgnoreCase(parser.getText());
Tyler Gunn369c8742015-10-07 15:39:28 -07001692 } else if (parser.getName().equals(EXTRAS)) {
Tyler Gunn369c8742015-10-07 15:39:28 -07001693 extras = readBundle(parser);
Christine Hallstromffe558c2016-11-30 16:05:13 -08001694 } else if (parser.getName().equals(SUPPORTED_AUDIO_ROUTES)) {
1695 parser.next();
1696 supportedAudioRoutes = Integer.parseInt(parser.getText());
Ihab Awadb78b2762014-07-25 15:16:23 -07001697 }
1698 }
Tyler Gunn84253572014-09-02 14:50:05 -07001699
Santos Cordona9eebe42015-06-11 14:07:44 -07001700 ComponentName pstnComponentName = new ComponentName("com.android.phone",
1701 "com.android.services.telephony.TelephonyConnectionService");
Santos Cordona82aed82015-05-26 10:43:56 -07001702 ComponentName sipComponentName = new ComponentName("com.android.phone",
1703 "com.android.services.telephony.sip.SipConnectionService");
1704
Tyler Gunn84253572014-09-02 14:50:05 -07001705 // Upgrade older phone accounts to specify the supported URI schemes.
1706 if (version < 2) {
Tyler Gunn84253572014-09-02 14:50:05 -07001707 supportedUriSchemes = new ArrayList<>();
1708
1709 // Handle the SIP connection service.
1710 // Check the system settings to see if it also should handle "tel" calls.
1711 if (accountHandle.getComponentName().equals(sipComponentName)) {
1712 boolean useSipForPstn = useSipForPstnCalls(context);
1713 supportedUriSchemes.add(PhoneAccount.SCHEME_SIP);
1714 if (useSipForPstn) {
1715 supportedUriSchemes.add(PhoneAccount.SCHEME_TEL);
1716 }
1717 } else {
1718 supportedUriSchemes.add(PhoneAccount.SCHEME_TEL);
1719 supportedUriSchemes.add(PhoneAccount.SCHEME_VOICEMAIL);
1720 }
1721 }
1722
Ihab Awad7e2c7f32014-11-03 09:49:45 -08001723 // Upgrade older phone accounts with explicit package name
1724 if (version < 5) {
1725 if (iconBitmap == null) {
1726 iconPackageName = accountHandle.getComponentName().getPackageName();
1727 }
1728 }
1729
Santos Cordona82aed82015-05-26 10:43:56 -07001730 if (version < 6) {
1731 // Always enable all SIP accounts on upgrade to version 6
1732 if (accountHandle.getComponentName().equals(sipComponentName)) {
1733 enabled = true;
1734 }
1735 }
Santos Cordona9eebe42015-06-11 14:07:44 -07001736 if (version < 7) {
1737 // Always enabled all PSTN acocunts on upgrade to version 7
1738 if (accountHandle.getComponentName().equals(pstnComponentName)) {
1739 enabled = true;
1740 }
1741 }
Roshan Pius6f752c82015-08-10 10:42:06 -07001742 if (version < 8) {
1743 // Migrate the SIP account handle ids to use SIP username instead of SIP URI.
1744 if (accountHandle.getComponentName().equals(sipComponentName)) {
1745 Uri accountUri = Uri.parse(accountHandle.getId());
1746 if (accountUri.getScheme() != null &&
1747 accountUri.getScheme().equals(PhoneAccount.SCHEME_SIP)) {
1748 accountHandle = new PhoneAccountHandle(accountHandle.getComponentName(),
1749 accountUri.getSchemeSpecificPart(),
1750 accountHandle.getUserHandle());
1751 }
1752 }
1753 }
Santos Cordona82aed82015-05-26 10:43:56 -07001754
Christine Hallstromffe558c2016-11-30 16:05:13 -08001755 if (version < 9) {
1756 // Set supported audio routes to all by default
1757 supportedAudioRoutes = CallAudioState.ROUTE_ALL;
1758 }
1759
Ihab Awad7e2c7f32014-11-03 09:49:45 -08001760 PhoneAccount.Builder builder = PhoneAccount.builder(accountHandle, label)
Andrew Lee7129f1c2014-09-04 11:55:07 -07001761 .setAddress(address)
1762 .setSubscriptionAddress(subscriptionAddress)
1763 .setCapabilities(capabilities)
Christine Hallstromffe558c2016-11-30 16:05:13 -08001764 .setSupportedAudioRoutes(supportedAudioRoutes)
Andrew Lee7129f1c2014-09-04 11:55:07 -07001765 .setShortDescription(shortDescription)
1766 .setSupportedUriSchemes(supportedUriSchemes)
Santos Cordonea5cb932015-05-07 16:28:38 -07001767 .setHighlightColor(highlightColor)
Tyler Gunn369c8742015-10-07 15:39:28 -07001768 .setExtras(extras)
Santos Cordonea5cb932015-05-07 16:28:38 -07001769 .setIsEnabled(enabled);
Ihab Awad7e2c7f32014-11-03 09:49:45 -08001770
Santos Cordon9c30c282015-05-13 16:28:27 -07001771 if (icon != null) {
1772 builder.setIcon(icon);
1773 } else if (iconBitmap != null) {
1774 builder.setIcon(Icon.createWithBitmap(iconBitmap));
1775 } else if (!TextUtils.isEmpty(iconPackageName)) {
1776 builder.setIcon(Icon.createWithResource(iconPackageName, iconResId));
1777 // TODO: Need to set tint.
Ihab Awad7e2c7f32014-11-03 09:49:45 -08001778 }
1779
Ihab Awad0a4b95f2015-05-18 10:15:38 -07001780 return builder.build();
Ihab Awadb78b2762014-07-25 15:16:23 -07001781 }
1782 return null;
Ihab Awad104f8062014-07-17 11:29:35 -07001783 }
Tyler Gunn84253572014-09-02 14:50:05 -07001784
1785 /**
Santos Cordon9c30c282015-05-13 16:28:27 -07001786 * Determines if the SIP call settings specify to use SIP for all calls, including PSTN
1787 * calls.
Tyler Gunn84253572014-09-02 14:50:05 -07001788 *
1789 * @param context The context.
1790 * @return {@code True} if SIP should be used for all calls.
1791 */
1792 private boolean useSipForPstnCalls(Context context) {
1793 String option = Settings.System.getString(context.getContentResolver(),
1794 Settings.System.SIP_CALL_OPTIONS);
1795 option = (option != null) ? option : Settings.System.SIP_ADDRESS_ONLY;
1796 return option.equals(Settings.System.SIP_ALWAYS);
1797 }
Ihab Awad104f8062014-07-17 11:29:35 -07001798 };
1799
Ihab Awadb78b2762014-07-25 15:16:23 -07001800 @VisibleForTesting
1801 public static final XmlSerialization<PhoneAccountHandle> sPhoneAccountHandleXml =
1802 new XmlSerialization<PhoneAccountHandle>() {
1803 private static final String CLASS_PHONE_ACCOUNT_HANDLE = "phone_account_handle";
Ihab Awad104f8062014-07-17 11:29:35 -07001804 private static final String COMPONENT_NAME = "component_name";
1805 private static final String ID = "id";
Evan Charlton105d9772014-11-25 14:08:53 -08001806 private static final String USER_SERIAL_NUMBER = "user_serial_number";
Ihab Awad104f8062014-07-17 11:29:35 -07001807
1808 @Override
Evan Charlton105d9772014-11-25 14:08:53 -08001809 public void writeToXml(PhoneAccountHandle o, XmlSerializer serializer, Context context)
Ihab Awadb78b2762014-07-25 15:16:23 -07001810 throws IOException {
Ihab Awad26923222014-07-30 10:54:35 -07001811 if (o != null) {
1812 serializer.startTag(null, CLASS_PHONE_ACCOUNT_HANDLE);
Ihab Awadb78b2762014-07-25 15:16:23 -07001813
Ihab Awad26923222014-07-30 10:54:35 -07001814 if (o.getComponentName() != null) {
Ihab Awadd9f54382014-10-24 11:44:47 -07001815 writeTextIfNonNull(
Ihab Awad26923222014-07-30 10:54:35 -07001816 COMPONENT_NAME, o.getComponentName().flattenToString(), serializer);
1817 }
Ihab Awadb78b2762014-07-25 15:16:23 -07001818
Ihab Awadd9f54382014-10-24 11:44:47 -07001819 writeTextIfNonNull(ID, o.getId(), serializer);
Ihab Awadb78b2762014-07-25 15:16:23 -07001820
Evan Charlton105d9772014-11-25 14:08:53 -08001821 if (o.getUserHandle() != null && context != null) {
1822 UserManager userManager = UserManager.get(context);
1823 writeLong(USER_SERIAL_NUMBER,
1824 userManager.getSerialNumberForUser(o.getUserHandle()), serializer);
1825 }
1826
Ihab Awad26923222014-07-30 10:54:35 -07001827 serializer.endTag(null, CLASS_PHONE_ACCOUNT_HANDLE);
1828 }
Ihab Awad104f8062014-07-17 11:29:35 -07001829 }
1830
1831 @Override
Tyler Gunn84253572014-09-02 14:50:05 -07001832 public PhoneAccountHandle readFromXml(XmlPullParser parser, int version, Context context)
Ihab Awadb78b2762014-07-25 15:16:23 -07001833 throws IOException, XmlPullParserException {
1834 if (parser.getName().equals(CLASS_PHONE_ACCOUNT_HANDLE)) {
1835 String componentNameString = null;
1836 String idString = null;
Evan Charlton105d9772014-11-25 14:08:53 -08001837 String userSerialNumberString = null;
Ihab Awadb78b2762014-07-25 15:16:23 -07001838 int outerDepth = parser.getDepth();
Evan Charlton105d9772014-11-25 14:08:53 -08001839
1840 UserManager userManager = UserManager.get(context);
1841
Ihab Awadb78b2762014-07-25 15:16:23 -07001842 while (XmlUtils.nextElementWithin(parser, outerDepth)) {
1843 if (parser.getName().equals(COMPONENT_NAME)) {
1844 parser.next();
1845 componentNameString = parser.getText();
1846 } else if (parser.getName().equals(ID)) {
1847 parser.next();
1848 idString = parser.getText();
Evan Charlton105d9772014-11-25 14:08:53 -08001849 } else if (parser.getName().equals(USER_SERIAL_NUMBER)) {
1850 parser.next();
1851 userSerialNumberString = parser.getText();
Ihab Awadb78b2762014-07-25 15:16:23 -07001852 }
1853 }
Ihab Awad26923222014-07-30 10:54:35 -07001854 if (componentNameString != null) {
Evan Charlton105d9772014-11-25 14:08:53 -08001855 UserHandle userHandle = null;
1856 if (userSerialNumberString != null) {
1857 try {
1858 long serialNumber = Long.parseLong(userSerialNumberString);
1859 userHandle = userManager.getUserForSerialNumber(serialNumber);
1860 } catch (NumberFormatException e) {
1861 Log.e(this, e, "Could not parse UserHandle " + userSerialNumberString);
1862 }
1863 }
Ihab Awadb78b2762014-07-25 15:16:23 -07001864 return new PhoneAccountHandle(
1865 ComponentName.unflattenFromString(componentNameString),
Evan Charlton105d9772014-11-25 14:08:53 -08001866 idString,
1867 userHandle);
Ihab Awadb78b2762014-07-25 15:16:23 -07001868 }
1869 }
1870 return null;
Ihab Awad104f8062014-07-17 11:29:35 -07001871 }
1872 };
Santos Cordon176ae282014-07-14 02:02:14 -07001873}