blob: d84fca5f1649a7f6c1bec61644005ede76371b25 [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;
Srikanth Chintalad5d2dd92016-06-23 16:02:33 +053076import java.util.Comparator;
Tyler Gunnd900ce62014-08-13 11:40:59 -070077import java.util.Iterator;
Santos Cordon176ae282014-07-14 02:02:14 -070078import java.util.List;
Tony Mak4a3e2fd2015-12-04 11:58:38 +000079import java.util.Map;
Santos Cordon176ae282014-07-14 02:02:14 -070080import java.util.Objects;
Brad Ebinger0c87b3c2016-06-10 17:23:02 -070081import java.util.Optional;
Tony Mak4a3e2fd2015-12-04 11:58:38 +000082import java.util.concurrent.ConcurrentHashMap;
Ihab Awadb78b2762014-07-25 15:16:23 -070083import java.util.concurrent.CopyOnWriteArrayList;
Brad Ebinger0c87b3c2016-06-10 17:23:02 -070084import java.util.stream.Collector;
85import java.util.stream.Collectors;
86import java.util.stream.Stream;
Santos Cordon176ae282014-07-14 02:02:14 -070087
88/**
Evan Charlton89176372014-07-19 18:23:09 -070089 * Handles writing and reading PhoneAccountHandle registration entries. This is a simple verbatim
Santos Cordon6a212642015-05-08 16:35:23 -070090 * delegate for all the account handling methods on {@link android.telecom.TelecomManager} as
91 * implemented in {@link TelecomServiceImpl}, with the notable exception that
92 * {@link TelecomServiceImpl} is responsible for security checking to make sure that the caller has
93 * proper authority over the {@code ComponentName}s they are declaring in their
94 * {@code PhoneAccountHandle}s.
95 *
96 *
97 * -- About Users and Phone Accounts --
98 *
Santos Cordon9c30c282015-05-13 16:28:27 -070099 * We store all phone accounts for all users in a single place, which means that there are three
100 * users that we have to deal with in code:
Santos Cordon6a212642015-05-08 16:35:23 -0700101 * 1) The Android User that is currently active on the device.
102 * 2) The user which owns/registers the phone account.
103 * 3) The user running the app that is requesting the phone account information.
104 *
105 * 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 +0000106 * has a work profile running as another user (B2). Each user/profile only have the visibility of
107 * phone accounts owned by them. Lets say, user B (settings) is requesting a list of phone accounts,
108 * and the list only contains phone accounts owned by user B and accounts with
109 * {@link PhoneAccount#CAPABILITY_MULTI_USER}.
Santos Cordon6a212642015-05-08 16:35:23 -0700110 *
111 * In practice, (2) is stored with the phone account handle and is part of the handle's ID. (1) is
112 * saved in {@link #mCurrentUserHandle} and (3) we get from Binder.getCallingUser(). We check these
113 * users for visibility before returning any phone accounts.
Santos Cordon176ae282014-07-14 02:02:14 -0700114 */
Brad Ebinger53855132015-10-30 10:58:19 -0700115public class PhoneAccountRegistrar {
Santos Cordon176ae282014-07-14 02:02:14 -0700116
Yorke Lee5e8836a2014-08-22 15:25:18 -0700117 public static final PhoneAccountHandle NO_ACCOUNT_SELECTED =
118 new PhoneAccountHandle(new ComponentName("null", "null"), "NO_ACCOUNT_SELECTED");
119
Ihab Awadb78b2762014-07-25 15:16:23 -0700120 public abstract static class Listener {
121 public void onAccountsChanged(PhoneAccountRegistrar registrar) {}
122 public void onDefaultOutgoingChanged(PhoneAccountRegistrar registrar) {}
123 public void onSimCallManagerChanged(PhoneAccountRegistrar registrar) {}
Tyler Gunnea4c6fb2017-04-14 13:46:21 -0700124 public void onPhoneAccountRegistered(PhoneAccountRegistrar registrar,
125 PhoneAccountHandle handle) {}
126 public void onPhoneAccountUnRegistered(PhoneAccountRegistrar registrar,
127 PhoneAccountHandle handle) {}
Ihab Awadb78b2762014-07-25 15:16:23 -0700128 }
129
Tyler Gunn2b17f232017-03-08 08:51:00 -0800130 /**
131 * Abstracts away dependency on the {@link PackageManager} required to fetch the label for an
132 * app.
133 */
134 public interface AppLabelProxy {
135 CharSequence getAppLabel(String packageName);
136 }
137
Ihab Awadb78b2762014-07-25 15:16:23 -0700138 private static final String FILE_NAME = "phone-account-registrar-state.xml";
Tyler Gunn84253572014-09-02 14:50:05 -0700139 @VisibleForTesting
Tony Mak4a3e2fd2015-12-04 11:58:38 +0000140 public static final int EXPECTED_STATE_VERSION = 9;
Tyler Gunn84253572014-09-02 14:50:05 -0700141
142 /** Keep in sync with the same in SipSettings.java */
143 private static final String SIP_SHARED_PREFERENCES = "SIP_PREFERENCES";
Ihab Awadb78b2762014-07-25 15:16:23 -0700144
145 private final List<Listener> mListeners = new CopyOnWriteArrayList<>();
146 private final AtomicFile mAtomicFile;
Santos Cordonafe59e52014-08-22 16:48:43 -0700147 private final Context mContext;
Evan Charlton105d9772014-11-25 14:08:53 -0800148 private final UserManager mUserManager;
Wink Saville33c05d32014-11-20 13:04:17 -0800149 private final SubscriptionManager mSubscriptionManager;
Hall Liu2b33df72016-12-06 18:15:39 -0800150 private final DefaultDialerCache mDefaultDialerCache;
Tyler Gunn2b17f232017-03-08 08:51:00 -0800151 private final AppLabelProxy mAppLabelProxy;
Ihab Awadb78b2762014-07-25 15:16:23 -0700152 private State mState;
Evan Charlton105d9772014-11-25 14:08:53 -0800153 private UserHandle mCurrentUserHandle;
Hall Liu3f7f64d2016-01-07 17:40:13 -0800154 private interface PhoneAccountRegistrarWriteLock {}
155 private final PhoneAccountRegistrarWriteLock mWriteLock =
156 new PhoneAccountRegistrarWriteLock() {};
Santos Cordon176ae282014-07-14 02:02:14 -0700157
Nancy Chen06ce0622014-10-23 01:17:35 +0000158 @VisibleForTesting
Tyler Gunn2b17f232017-03-08 08:51:00 -0800159 public PhoneAccountRegistrar(Context context, DefaultDialerCache defaultDialerCache,
160 AppLabelProxy appLabelProxy) {
161 this(context, FILE_NAME, defaultDialerCache, appLabelProxy);
Ihab Awadb78b2762014-07-25 15:16:23 -0700162 }
163
164 @VisibleForTesting
Hall Liu2b33df72016-12-06 18:15:39 -0800165 public PhoneAccountRegistrar(Context context, String fileName,
Tyler Gunn2b17f232017-03-08 08:51:00 -0800166 DefaultDialerCache defaultDialerCache, AppLabelProxy appLabelProxy) {
167
Ihab Awadb78b2762014-07-25 15:16:23 -0700168 mAtomicFile = new AtomicFile(new File(context.getFilesDir(), fileName));
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700169
Ihab Awadb78b2762014-07-25 15:16:23 -0700170 mState = new State();
Santos Cordonafe59e52014-08-22 16:48:43 -0700171 mContext = context;
Evan Charlton105d9772014-11-25 14:08:53 -0800172 mUserManager = UserManager.get(context);
Hall Liu2b33df72016-12-06 18:15:39 -0800173 mDefaultDialerCache = defaultDialerCache;
Wink Saville33c05d32014-11-20 13:04:17 -0800174 mSubscriptionManager = SubscriptionManager.from(mContext);
Tyler Gunn2b17f232017-03-08 08:51:00 -0800175 mAppLabelProxy = appLabelProxy;
Evan Charlton105d9772014-11-25 14:08:53 -0800176 mCurrentUserHandle = Process.myUserHandle();
Ihab Awadb78b2762014-07-25 15:16:23 -0700177 read();
Santos Cordon176ae282014-07-14 02:02:14 -0700178 }
179
Tyler Gunn84253572014-09-02 14:50:05 -0700180 /**
Nancy Chen140004a2014-10-15 15:48:38 -0700181 * Retrieves the subscription id for a given phone account if it exists. Subscription ids
182 * apply only to PSTN/SIM card phone accounts so all other accounts should not have a
183 * subscription id.
184 * @param accountHandle The handle for the phone account for which to retrieve the
185 * subscription id.
Wink Saville35850602014-10-23 15:57:21 -0700186 * @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 -0700187 */
Wink Saville35850602014-10-23 15:57:21 -0700188 public int getSubscriptionIdForPhoneAccount(PhoneAccountHandle accountHandle) {
Tony Mak240656f2015-12-04 11:36:22 +0000189 PhoneAccount account = getPhoneAccountUnchecked(accountHandle);
Santos Cordon479b3022015-02-06 04:27:15 -0800190
Santos Cordon6a212642015-05-08 16:35:23 -0700191 if (account != null && account.hasCapabilities(PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION)) {
Santos Cordon479b3022015-02-06 04:27:15 -0800192 TelephonyManager tm =
193 (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE);
194 return tm.getSubIdForPhoneAccount(account);
Nancy Chen140004a2014-10-15 15:48:38 -0700195 }
Santos Cordon479b3022015-02-06 04:27:15 -0800196 return SubscriptionManager.INVALID_SUBSCRIPTION_ID;
Nancy Chen140004a2014-10-15 15:48:38 -0700197 }
198
199 /**
Evan Charlton105d9772014-11-25 14:08:53 -0800200 * Retrieves the default outgoing phone account supporting the specified uriScheme. Note that if
201 * {@link #mCurrentUserHandle} does not have visibility into the current default, {@code null}
202 * will be returned.
203 *
Tyler Gunn84253572014-09-02 14:50:05 -0700204 * @param uriScheme The URI scheme for the outgoing call.
205 * @return The {@link PhoneAccountHandle} to use.
206 */
Tony Mak240656f2015-12-04 11:36:22 +0000207 public PhoneAccountHandle getOutgoingPhoneAccountForScheme(String uriScheme,
208 UserHandle userHandle) {
209 final PhoneAccountHandle userSelected = getUserSelectedOutgoingPhoneAccount(userHandle);
Tyler Gunn84253572014-09-02 14:50:05 -0700210
Yorke Lee5e8836a2014-08-22 15:25:18 -0700211 if (userSelected != null) {
Tyler Gunn84253572014-09-02 14:50:05 -0700212 // If there is a default PhoneAccount, ensure it supports calls to handles with the
213 // specified uriScheme.
Tony Mak240656f2015-12-04 11:36:22 +0000214 final PhoneAccount userSelectedAccount = getPhoneAccountUnchecked(userSelected);
Santos Cordon6a212642015-05-08 16:35:23 -0700215 if (userSelectedAccount.supportsUriScheme(uriScheme)) {
Tyler Gunn84253572014-09-02 14:50:05 -0700216 return userSelected;
217 }
Ihab Awad293edf22014-07-24 17:52:29 -0700218 }
219
Tony Mak240656f2015-12-04 11:36:22 +0000220 List<PhoneAccountHandle> outgoing = getCallCapablePhoneAccounts(uriScheme, false,
221 userHandle);
Ihab Awad6fb37c82014-08-07 19:48:57 -0700222 switch (outgoing.size()) {
Ihab Awad293edf22014-07-24 17:52:29 -0700223 case 0:
224 // There are no accounts, so there can be no default
225 return null;
226 case 1:
Evan Charlton105d9772014-11-25 14:08:53 -0800227 // There is only one account, which is by definition the default.
Santos Cordon6a212642015-05-08 16:35:23 -0700228 return outgoing.get(0);
Ihab Awad293edf22014-07-24 17:52:29 -0700229 default:
230 // There are multiple accounts with no selected default
231 return null;
Ihab Awadf2a84912014-07-22 21:09:25 -0700232 }
Ihab Awad104f8062014-07-17 11:29:35 -0700233 }
Santos Cordon176ae282014-07-14 02:02:14 -0700234
Tony Mak240656f2015-12-04 11:36:22 +0000235 public PhoneAccountHandle getOutgoingPhoneAccountForSchemeOfCurrentUser(String uriScheme) {
236 return getOutgoingPhoneAccountForScheme(uriScheme, mCurrentUserHandle);
237 }
238
Evan Charlton105d9772014-11-25 14:08:53 -0800239 /**
240 * @return The user-selected outgoing {@link PhoneAccount}, or null if it hasn't been set (or
241 * if it was set by another user).
242 */
Hall Liuecda5542015-12-04 11:31:31 -0800243 @VisibleForTesting
244 public PhoneAccountHandle getUserSelectedOutgoingPhoneAccount(UserHandle userHandle) {
Tony Mak4a3e2fd2015-12-04 11:58:38 +0000245 if (userHandle == null) {
246 return null;
247 }
248 DefaultPhoneAccountHandle defaultPhoneAccountHandle = mState.defaultOutgoingAccountHandles
249 .get(userHandle);
250 if (defaultPhoneAccountHandle == null) {
251 return null;
252 }
253 // Make sure the account is still registered and owned by the user.
254 PhoneAccount account = getPhoneAccount(defaultPhoneAccountHandle.phoneAccountHandle,
255 userHandle);
Hall Liuecda5542015-12-04 11:31:31 -0800256
Santos Cordon6a212642015-05-08 16:35:23 -0700257 if (account != null) {
Tony Mak4a3e2fd2015-12-04 11:58:38 +0000258 return defaultPhoneAccountHandle.phoneAccountHandle;
Yorke Lee5e8836a2014-08-22 15:25:18 -0700259 }
260 return null;
261 }
262
Santos Cordon6a212642015-05-08 16:35:23 -0700263 /**
Brad Ebinger0c87b3c2016-06-10 17:23:02 -0700264 * @return The {@link DefaultPhoneAccountHandle} containing the user-selected default calling
265 * account and group Id for the {@link UserHandle} specified.
266 */
267 private DefaultPhoneAccountHandle getUserSelectedDefaultPhoneAccount(UserHandle userHandle) {
268 if (userHandle == null) {
269 return null;
270 }
271 DefaultPhoneAccountHandle defaultPhoneAccountHandle = mState.defaultOutgoingAccountHandles
272 .get(userHandle);
273 if (defaultPhoneAccountHandle == null) {
274 return null;
275 }
276
277 return defaultPhoneAccountHandle;
278 }
279
280 /**
281 * @return The currently registered PhoneAccount in Telecom that has the same group Id.
282 */
283 private PhoneAccount getPhoneAccountByGroupId(String groupId, ComponentName groupComponentName,
284 UserHandle userHandle, PhoneAccountHandle excludePhoneAccountHandle) {
285 if (groupId == null || groupId.isEmpty() || userHandle == null) {
286 return null;
287 }
288 // Get the PhoneAccount with the same group Id (and same ComponentName) that is not the
289 // newAccount that was just added
290 List<PhoneAccount> accounts = getAllPhoneAccounts(userHandle).stream()
291 .filter(account -> groupId.equals(account.getGroupId()) &&
292 !account.getAccountHandle().equals(excludePhoneAccountHandle) &&
293 Objects.equals(account.getAccountHandle().getComponentName(),
294 groupComponentName))
295 .collect(Collectors.toList());
296 // There should be one or no PhoneAccounts with the same group Id
297 if (accounts.size() > 1) {
298 Log.w(this, "Found multiple PhoneAccounts registered to the same Group Id!");
299 }
300 return accounts.isEmpty() ? null : accounts.get(0);
301 }
302
303 /**
Santos Cordon6a212642015-05-08 16:35:23 -0700304 * Sets the phone account with which to place all calls by default. Set by the user
305 * within phone settings.
306 */
Tony Mak4a3e2fd2015-12-04 11:58:38 +0000307 public void setUserSelectedOutgoingPhoneAccount(PhoneAccountHandle accountHandle,
308 UserHandle userHandle) {
309 if (userHandle == null) {
310 return;
311 }
Evan Charlton89176372014-07-19 18:23:09 -0700312 if (accountHandle == null) {
Ihab Awad104f8062014-07-17 11:29:35 -0700313 // Asking to clear the default outgoing is a valid request
Tony Mak4a3e2fd2015-12-04 11:58:38 +0000314 mState.defaultOutgoingAccountHandles.remove(userHandle);
Ihab Awad104f8062014-07-17 11:29:35 -0700315 } else {
Tony Mak4a3e2fd2015-12-04 11:58:38 +0000316 PhoneAccount account = getPhoneAccount(accountHandle, userHandle);
Santos Cordon6a212642015-05-08 16:35:23 -0700317 if (account == null) {
Ihab Awadb78b2762014-07-25 15:16:23 -0700318 Log.w(this, "Trying to set nonexistent default outgoing %s",
319 accountHandle);
320 return;
321 }
322
Santos Cordon6a212642015-05-08 16:35:23 -0700323 if (!account.hasCapabilities(PhoneAccount.CAPABILITY_CALL_PROVIDER)) {
Ihab Awadb78b2762014-07-25 15:16:23 -0700324 Log.w(this, "Trying to set non-call-provider default outgoing %s",
Evan Charlton89176372014-07-19 18:23:09 -0700325 accountHandle);
Ihab Awad104f8062014-07-17 11:29:35 -0700326 return;
327 }
328
Santos Cordon6a212642015-05-08 16:35:23 -0700329 if (account.hasCapabilities(PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION)) {
Nancy Chen5a36b6e2014-10-23 17:42:42 -0700330 // If the account selected is a SIM account, propagate down to the subscription
331 // record.
Wink Saville7ce6e782014-10-27 10:56:46 -0700332 int subId = getSubscriptionIdForPhoneAccount(accountHandle);
Wink Saville33c05d32014-11-20 13:04:17 -0800333 mSubscriptionManager.setDefaultVoiceSubId(subId);
Nancy Chen5a36b6e2014-10-23 17:42:42 -0700334 }
335
Tony Mak4a3e2fd2015-12-04 11:58:38 +0000336 mState.defaultOutgoingAccountHandles
Brad Ebinger0c87b3c2016-06-10 17:23:02 -0700337 .put(userHandle, new DefaultPhoneAccountHandle(userHandle, accountHandle,
338 account.getGroupId()));
Santos Cordon176ae282014-07-14 02:02:14 -0700339 }
340
Ihab Awad293edf22014-07-24 17:52:29 -0700341 write();
Ihab Awadb78b2762014-07-25 15:16:23 -0700342 fireDefaultOutgoingChanged();
Santos Cordon176ae282014-07-14 02:02:14 -0700343 }
344
Nancy Chen668dee02014-11-19 15:31:31 -0800345 boolean isUserSelectedSmsPhoneAccount(PhoneAccountHandle accountHandle) {
Shishir Agrawalce688742016-01-25 15:02:21 -0800346 return getSubscriptionIdForPhoneAccount(accountHandle) ==
347 SubscriptionManager.getDefaultSmsSubscriptionId();
Sailesh Nepalfc43ea82015-07-28 19:30:28 -0700348 }
349
Sailesh Nepal8ed7eeb2015-09-08 12:30:13 -0700350 public ComponentName getSystemSimCallManagerComponent() {
351 String defaultSimCallManager = null;
352 CarrierConfigManager configManager = (CarrierConfigManager) mContext.getSystemService(
353 Context.CARRIER_CONFIG_SERVICE);
354 PersistableBundle configBundle = configManager.getConfig();
355 if (configBundle != null) {
356 defaultSimCallManager = configBundle.getString(
357 CarrierConfigManager.KEY_DEFAULT_SIM_CALL_MANAGER_STRING);
358 }
359 return TextUtils.isEmpty(defaultSimCallManager)
360 ? null : ComponentName.unflattenFromString(defaultSimCallManager);
361 }
362
Tony Mak240656f2015-12-04 11:36:22 +0000363 public PhoneAccountHandle getSimCallManagerOfCurrentUser() {
364 return getSimCallManager(mCurrentUserHandle);
365 }
366
Sailesh Nepalfc43ea82015-07-28 19:30:28 -0700367 /**
368 * Returns the {@link PhoneAccountHandle} corresponding to the currently active SIM Call
369 * Manager. SIM Call Manager returned corresponds to the following priority order:
370 * 1. If a SIM Call Manager {@link PhoneAccount} is registered for the same package as the
371 * default dialer, then that one is returned.
372 * 2. If there is a SIM Call Manager {@link PhoneAccount} registered which matches the
373 * carrier configuration's default, then that one is returned.
374 * 3. Otherwise, we return null.
375 */
Tony Mak240656f2015-12-04 11:36:22 +0000376 public PhoneAccountHandle getSimCallManager(UserHandle userHandle) {
Santos Cordon59c21a72015-06-11 10:11:21 -0700377 // Get the default dialer in case it has a connection manager associated with it.
Hall Liu2b33df72016-12-06 18:15:39 -0800378 String dialerPackage = mDefaultDialerCache
379 .getDefaultDialerApplication(userHandle.getIdentifier());
Evan Charltonaf51ceb2014-07-30 11:56:36 -0700380
Sailesh Nepalaecfa9b2015-06-16 14:55:27 -0700381 // Check carrier config.
Sailesh Nepal8ed7eeb2015-09-08 12:30:13 -0700382 ComponentName systemSimCallManagerComponent = getSystemSimCallManagerComponent();
Sailesh Nepal91fc8092015-02-14 15:44:55 -0800383
Santos Cordon59c21a72015-06-11 10:11:21 -0700384 PhoneAccountHandle dialerSimCallManager = null;
385 PhoneAccountHandle systemSimCallManager = null;
Sailesh Nepal91fc8092015-02-14 15:44:55 -0800386
Santos Cordon59c21a72015-06-11 10:11:21 -0700387 if (!TextUtils.isEmpty(dialerPackage) || systemSimCallManagerComponent != null) {
388 // loop through and look for any connection manager in the same package.
389 List<PhoneAccountHandle> allSimCallManagers = getPhoneAccountHandles(
390 PhoneAccount.CAPABILITY_CONNECTION_MANAGER, null, null,
Tony Mak240656f2015-12-04 11:36:22 +0000391 true /* includeDisabledAccounts */, userHandle);
Santos Cordon59c21a72015-06-11 10:11:21 -0700392 for (PhoneAccountHandle accountHandle : allSimCallManagers) {
393 ComponentName component = accountHandle.getComponentName();
394
395 // Store the system connection manager if found
396 if (systemSimCallManager == null
397 && Objects.equals(component, systemSimCallManagerComponent)
398 && !resolveComponent(accountHandle).isEmpty()) {
399 systemSimCallManager = accountHandle;
400
401 // Store the dialer connection manager if found
402 } else if (dialerSimCallManager == null
403 && Objects.equals(component.getPackageName(), dialerPackage)
404 && !resolveComponent(accountHandle).isEmpty()) {
405 dialerSimCallManager = accountHandle;
Evan Charltonaf51ceb2014-07-30 11:56:36 -0700406 }
Evan Charltonaf51ceb2014-07-30 11:56:36 -0700407 }
Evan Charltonaf51ceb2014-07-30 11:56:36 -0700408 }
409
Santos Cordon59c21a72015-06-11 10:11:21 -0700410 PhoneAccountHandle retval = dialerSimCallManager != null ?
411 dialerSimCallManager : systemSimCallManager;
412
413 Log.i(this, "SimCallManager queried, returning: %s", retval);
414
Sailesh Nepalb3ccd7e2015-06-26 18:16:18 -0700415 return retval;
Ihab Awad293edf22014-07-24 17:52:29 -0700416 }
417
Evan Charlton105d9772014-11-25 14:08:53 -0800418 /**
Tony Mak240656f2015-12-04 11:36:22 +0000419 * If it is a outgoing call, sim call manager of call-initiating user is returned.
420 * Otherwise, we return the sim call manager of the user associated with the
421 * target phone account.
422 * @return phone account handle of sim call manager based on the ongoing call.
423 */
424 public PhoneAccountHandle getSimCallManagerFromCall(Call call) {
425 if (call == null) {
426 return null;
427 }
428 UserHandle userHandle = call.getInitiatingUser();
429 if (userHandle == null) {
430 userHandle = call.getTargetPhoneAccount().getUserHandle();
431 }
432 return getSimCallManager(userHandle);
433 }
434
435 /**
Evan Charlton105d9772014-11-25 14:08:53 -0800436 * Update the current UserHandle to track when users are switched. This will allow the
437 * PhoneAccountRegistar to self-filter the PhoneAccounts to make sure we don't leak anything
438 * across users.
Santos Cordon6a212642015-05-08 16:35:23 -0700439 * We cannot simply check the calling user because that would always return the primary user for
440 * all invocations originating with the system process.
Evan Charlton105d9772014-11-25 14:08:53 -0800441 *
442 * @param userHandle The {@link UserHandle}, as delivered by
443 * {@link Intent#ACTION_USER_SWITCHED}.
444 */
445 public void setCurrentUserHandle(UserHandle userHandle) {
446 if (userHandle == null) {
447 Log.d(this, "setCurrentUserHandle, userHandle = null");
448 userHandle = Process.myUserHandle();
449 }
450 Log.d(this, "setCurrentUserHandle, %s", userHandle);
451 mCurrentUserHandle = userHandle;
452 }
453
Yorke Lee71734c22015-06-02 14:22:56 -0700454 /**
455 * @return {@code true} if the phone account was successfully enabled/disabled, {@code false}
456 * otherwise.
457 */
458 public boolean enablePhoneAccount(PhoneAccountHandle accountHandle, boolean isEnabled) {
Tony Mak98e6bdc2015-11-25 21:54:05 +0000459 PhoneAccount account = getPhoneAccountUnchecked(accountHandle);
Hall Liu518a7dd2017-01-26 14:20:04 -0800460 Log.i(this, "Phone account %s %s.", accountHandle, isEnabled ? "enabled" : "disabled");
Santos Cordon638f05c2015-05-19 17:12:33 -0700461 if (account == null) {
462 Log.w(this, "Could not find account to enable: " + accountHandle);
Yorke Lee71734c22015-06-02 14:22:56 -0700463 return false;
Santos Cordon638f05c2015-05-19 17:12:33 -0700464 } else if (account.hasCapabilities(PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION)) {
Santos Cordonea5cb932015-05-07 16:28:38 -0700465 // We never change the enabled state of SIM-based accounts.
Santos Cordon638f05c2015-05-19 17:12:33 -0700466 Log.w(this, "Could not change enable state of SIM account: " + accountHandle);
Yorke Lee71734c22015-06-02 14:22:56 -0700467 return false;
Santos Cordonea5cb932015-05-07 16:28:38 -0700468 }
469
Santos Cordon638f05c2015-05-19 17:12:33 -0700470 if (account.isEnabled() != isEnabled) {
Santos Cordonea5cb932015-05-07 16:28:38 -0700471 account.setIsEnabled(isEnabled);
Tony Mak48367162016-01-15 22:58:18 +0000472 if (!isEnabled) {
473 // If the disabled account is the default, remove it.
474 removeDefaultPhoneAccountHandle(accountHandle);
475 }
Santos Cordonea5cb932015-05-07 16:28:38 -0700476 write();
477 fireAccountsChanged();
478 }
Yorke Lee71734c22015-06-02 14:22:56 -0700479 return true;
Santos Cordonea5cb932015-05-07 16:28:38 -0700480 }
481
Tony Mak48367162016-01-15 22:58:18 +0000482 private void removeDefaultPhoneAccountHandle(PhoneAccountHandle phoneAccountHandle) {
483 Iterator<Map.Entry<UserHandle, DefaultPhoneAccountHandle>> iterator =
484 mState.defaultOutgoingAccountHandles.entrySet().iterator();
485 while (iterator.hasNext()) {
486 Map.Entry<UserHandle, DefaultPhoneAccountHandle> entry = iterator.next();
487 if (phoneAccountHandle.equals(entry.getValue().phoneAccountHandle)) {
488 iterator.remove();
489 }
490 }
491 }
492
Tony Mak240656f2015-12-04 11:36:22 +0000493 private boolean isVisibleForUser(PhoneAccount account, UserHandle userHandle,
494 boolean acrossProfiles) {
Evan Charlton105d9772014-11-25 14:08:53 -0800495 if (account == null) {
496 return false;
497 }
498
Tony Mak240656f2015-12-04 11:36:22 +0000499 if (userHandle == null) {
500 Log.w(this, "userHandle is null in isVisibleForUser");
501 return false;
502 }
503
Evan Charlton105d9772014-11-25 14:08:53 -0800504 // If this PhoneAccount has CAPABILITY_MULTI_USER, it should be visible to all users and
505 // all profiles. Only Telephony and SIP accounts should have this capability.
506 if (account.hasCapabilities(PhoneAccount.CAPABILITY_MULTI_USER)) {
507 return true;
508 }
509
510 UserHandle phoneAccountUserHandle = account.getAccountHandle().getUserHandle();
511 if (phoneAccountUserHandle == null) {
512 return false;
513 }
514
515 if (mCurrentUserHandle == null) {
Tony Mak240656f2015-12-04 11:36:22 +0000516 // In case we need to have emergency phone calls from the lock screen.
Evan Charlton105d9772014-11-25 14:08:53 -0800517 Log.d(this, "Current user is null; assuming true");
518 return true;
519 }
520
Tony Mak240656f2015-12-04 11:36:22 +0000521 if (acrossProfiles) {
522 return UserManager.get(mContext).isSameProfileGroup(userHandle.getIdentifier(),
523 phoneAccountUserHandle.getIdentifier());
524 } else {
525 return phoneAccountUserHandle.equals(userHandle);
Sailesh Nepal91fc8092015-02-14 15:44:55 -0800526 }
Evan Charlton105d9772014-11-25 14:08:53 -0800527 }
528
529 private List<ResolveInfo> resolveComponent(PhoneAccountHandle phoneAccountHandle) {
530 return resolveComponent(phoneAccountHandle.getComponentName(),
Tony Mak240656f2015-12-04 11:36:22 +0000531 phoneAccountHandle.getUserHandle());
Evan Charlton105d9772014-11-25 14:08:53 -0800532 }
533
534 private List<ResolveInfo> resolveComponent(ComponentName componentName,
535 UserHandle userHandle) {
mike dooley10a58312014-11-06 13:46:19 -0800536 PackageManager pm = mContext.getPackageManager();
537 Intent intent = new Intent(ConnectionService.SERVICE_INTERFACE);
538 intent.setComponent(componentName);
Sailesh Nepal91fc8092015-02-14 15:44:55 -0800539 try {
540 if (userHandle != null) {
541 return pm.queryIntentServicesAsUser(intent, 0, userHandle.getIdentifier());
542 } else {
543 return pm.queryIntentServices(intent, 0);
544 }
545 } catch (SecurityException e) {
Santos Cordon3188b362015-05-22 13:01:10 -0700546 Log.e(this, e, "%s is not visible for the calling user", componentName);
Sailesh Nepal91fc8092015-02-14 15:44:55 -0800547 return Collections.EMPTY_LIST;
Evan Charlton105d9772014-11-25 14:08:53 -0800548 }
mike dooley10a58312014-11-06 13:46:19 -0800549 }
550
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700551 /**
552 * Retrieves a list of all {@link PhoneAccountHandle}s registered.
Santos Cordonea5cb932015-05-07 16:28:38 -0700553 * Only returns accounts which are enabled.
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700554 *
555 * @return The list of {@link PhoneAccountHandle}s.
556 */
Tony Mak240656f2015-12-04 11:36:22 +0000557 public List<PhoneAccountHandle> getAllPhoneAccountHandles(UserHandle userHandle) {
558 return getPhoneAccountHandles(0, null, null, false, userHandle);
Ihab Awad293edf22014-07-24 17:52:29 -0700559 }
560
Tony Mak240656f2015-12-04 11:36:22 +0000561 public List<PhoneAccount> getAllPhoneAccounts(UserHandle userHandle) {
562 return getPhoneAccounts(0, null, null, false, userHandle);
563 }
564
565 public List<PhoneAccount> getAllPhoneAccountsOfCurrentUser() {
566 return getAllPhoneAccounts(mCurrentUserHandle);
Santos Cordonafe59e52014-08-22 16:48:43 -0700567 }
568
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700569 /**
Nancy Chen309198e2014-09-15 18:02:49 -0700570 * Retrieves a list of all phone account call provider phone accounts supporting the
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700571 * specified URI scheme.
572 *
573 * @param uriScheme The URI scheme.
Tyler Gunnc74b3e22017-11-07 15:03:24 -0800574 * @param includeDisabledAccounts {@code} if disabled {@link PhoneAccount}s should be included
575 * in the results.
576 * @param userHandle The {@link UserHandle} to retrieve the {@link PhoneAccount}s for.
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700577 * @return The phone account handles.
578 */
Santos Cordonea5cb932015-05-07 16:28:38 -0700579 public List<PhoneAccountHandle> getCallCapablePhoneAccounts(
Tony Mak240656f2015-12-04 11:36:22 +0000580 String uriScheme, boolean includeDisabledAccounts, UserHandle userHandle) {
Tyler Gunnc74b3e22017-11-07 15:03:24 -0800581 return getCallCapablePhoneAccounts(uriScheme, includeDisabledAccounts, userHandle,
582 0 /* capabilities */);
583 }
584
585 /**
586 * Retrieves a list of all phone account call provider phone accounts supporting the
587 * specified URI scheme.
588 *
589 * @param uriScheme The URI scheme.
590 * @param includeDisabledAccounts {@code} if disabled {@link PhoneAccount}s should be included
591 * in the results.
592 * @param userHandle The {@link UserHandle} to retrieve the {@link PhoneAccount}s for.
593 * @param capabilities Extra {@link PhoneAccount} capabilities which matching
594 * {@link PhoneAccount}s must have.
595 * @return The phone account handles.
596 */
597 public List<PhoneAccountHandle> getCallCapablePhoneAccounts(
598 String uriScheme, boolean includeDisabledAccounts, UserHandle userHandle,
599 int capabilities) {
Santos Cordonea5cb932015-05-07 16:28:38 -0700600 return getPhoneAccountHandles(
Tyler Gunnc74b3e22017-11-07 15:03:24 -0800601 PhoneAccount.CAPABILITY_CALL_PROVIDER | capabilities,
Bryce Leea0bb7052015-10-16 13:31:40 -0700602 PhoneAccount.CAPABILITY_EMERGENCY_CALLS_ONLY /*excludedCapabilities*/,
Tony Mak240656f2015-12-04 11:36:22 +0000603 uriScheme, null, includeDisabledAccounts, userHandle);
604 }
605
Tyler Gunn66ab9ae2017-04-07 15:00:49 -0700606 /**
607 * Retrieves a list of all phone accounts which have
608 * {@link PhoneAccount#CAPABILITY_SELF_MANAGED}.
609 * <p>
610 * Returns only the {@link PhoneAccount}s which are enabled as self-managed accounts are
611 * automatically enabled by default (see {@link #registerPhoneAccount(PhoneAccount)}).
612 *
613 * @param userHandle User handle of phone account owner.
614 * @return The phone account handles.
615 */
616 public List<PhoneAccountHandle> getSelfManagedPhoneAccounts(UserHandle userHandle) {
617 return getPhoneAccountHandles(
618 PhoneAccount.CAPABILITY_SELF_MANAGED,
619 PhoneAccount.CAPABILITY_EMERGENCY_CALLS_ONLY /* excludedCapabilities */,
620 null /* uriScheme */, null /* packageName */, false /* includeDisabledAccounts */,
621 userHandle);
622 }
623
Tony Mak240656f2015-12-04 11:36:22 +0000624 public List<PhoneAccountHandle> getCallCapablePhoneAccountsOfCurrentUser(
625 String uriScheme, boolean includeDisabledAccounts) {
626 return getCallCapablePhoneAccounts(uriScheme, includeDisabledAccounts, mCurrentUserHandle);
Santos Cordon6a212642015-05-08 16:35:23 -0700627 }
628
629 /**
630 * Retrieves a list of all the SIM-based phone accounts.
631 */
Tony Mak240656f2015-12-04 11:36:22 +0000632 public List<PhoneAccountHandle> getSimPhoneAccounts(UserHandle userHandle) {
Santos Cordon6a212642015-05-08 16:35:23 -0700633 return getPhoneAccountHandles(
634 PhoneAccount.CAPABILITY_CALL_PROVIDER | PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION,
Tony Mak240656f2015-12-04 11:36:22 +0000635 null, null, false, userHandle);
Tyler Gunn84253572014-09-02 14:50:05 -0700636 }
637
Tony Mak240656f2015-12-04 11:36:22 +0000638 public List<PhoneAccountHandle> getSimPhoneAccountsOfCurrentUser() {
639 return getSimPhoneAccounts(mCurrentUserHandle);
640 }
641
642 /**
643 * Retrieves a list of all phone accounts registered by a specified package.
644 *
645 * @param packageName The name of the package that registered the phone accounts.
646 * @return The phone account handles.
647 */
648 public List<PhoneAccountHandle> getPhoneAccountsForPackage(String packageName,
649 UserHandle userHandle) {
650 return getPhoneAccountHandles(0, null, packageName, false, userHandle);
Nancy Chen1c5926f2014-09-17 14:44:14 -0700651 }
652
Ihab Awad104f8062014-07-17 11:29:35 -0700653 // TODO: Should we implement an artificial limit for # of accounts associated with a single
654 // ComponentName?
Ihab Awad293edf22014-07-24 17:52:29 -0700655 public void registerPhoneAccount(PhoneAccount account) {
Tyler Gunncb59b672014-08-20 09:02:11 -0700656 // Enforce the requirement that a connection service for a phone account has the correct
657 // permission.
Santos Cordon6a212642015-05-08 16:35:23 -0700658 if (!phoneAccountRequiresBindPermission(account.getAccountHandle())) {
Yorke Lee7bb8ce92015-05-13 16:28:29 -0700659 Log.w(this,
660 "Phone account %s does not have BIND_TELECOM_CONNECTION_SERVICE permission.",
Tyler Gunncb59b672014-08-20 09:02:11 -0700661 account.getAccountHandle());
Yorke Lee7bb8ce92015-05-13 16:28:29 -0700662 throw new SecurityException("PhoneAccount connection service requires "
663 + "BIND_TELECOM_CONNECTION_SERVICE permission.");
Tyler Gunncb59b672014-08-20 09:02:11 -0700664 }
665
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700666 addOrReplacePhoneAccount(account);
667 }
668
669 /**
670 * Adds a {@code PhoneAccount}, replacing an existing one if found.
671 *
672 * @param account The {@code PhoneAccount} to add or replace.
673 */
674 private void addOrReplacePhoneAccount(PhoneAccount account) {
Ihab Awad7e2c7f32014-11-03 09:49:45 -0800675 Log.d(this, "addOrReplacePhoneAccount(%s -> %s)",
676 account.getAccountHandle(), account);
677
Santos Cordonea5cb932015-05-07 16:28:38 -0700678 // Start _enabled_ property as false.
679 // !!! IMPORTANT !!! It is important that we do not read the enabled state that the
680 // source app provides or else an third party app could enable itself.
681 boolean isEnabled = false;
Tyler Gunnea4c6fb2017-04-14 13:46:21 -0700682 boolean isNewAccount;
Santos Cordonea5cb932015-05-07 16:28:38 -0700683
Tony Mak98e6bdc2015-11-25 21:54:05 +0000684 PhoneAccount oldAccount = getPhoneAccountUnchecked(account.getAccountHandle());
Santos Cordon6a212642015-05-08 16:35:23 -0700685 if (oldAccount != null) {
686 mState.accounts.remove(oldAccount);
Santos Cordonea5cb932015-05-07 16:28:38 -0700687 isEnabled = oldAccount.isEnabled();
Tyler Gunnea4c6fb2017-04-14 13:46:21 -0700688 Log.i(this, "Modify account: %s", getAccountDiffString(account, oldAccount));
689 isNewAccount = false;
Santos Cordon5fa4e4f2015-06-10 14:56:01 -0700690 } else {
691 Log.i(this, "New phone account registered: " + account);
Tyler Gunnea4c6fb2017-04-14 13:46:21 -0700692 isNewAccount = true;
Santos Cordon176ae282014-07-14 02:02:14 -0700693 }
Santos Cordonea5cb932015-05-07 16:28:38 -0700694
Tyler Gunn2b17f232017-03-08 08:51:00 -0800695 // When registering a self-managed PhoneAccount we enforce the rule that the label that the
696 // app uses is also its phone account label. Also ensure it does not attempt to declare
697 // itself as a sim acct, call manager or call provider.
698 if (account.hasCapabilities(PhoneAccount.CAPABILITY_SELF_MANAGED)) {
699 // Turn off bits we don't want to be able to set (TelecomServiceImpl protects against
700 // this but we'll also prevent it from happening here, just to be safe).
701 int newCapabilities = account.getCapabilities() &
702 ~(PhoneAccount.CAPABILITY_CALL_PROVIDER |
703 PhoneAccount.CAPABILITY_CONNECTION_MANAGER |
704 PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION);
705
706 // Ensure name is correct.
707 CharSequence newLabel = mAppLabelProxy.getAppLabel(
708 account.getAccountHandle().getComponentName().getPackageName());
709
710 account = account.toBuilder()
711 .setLabel(newLabel)
712 .setCapabilities(newCapabilities)
713 .build();
714 }
715
Santos Cordon6a212642015-05-08 16:35:23 -0700716 mState.accounts.add(account);
Brad Ebinger0c87b3c2016-06-10 17:23:02 -0700717 // Set defaults and replace based on the group Id.
718 maybeReplaceOldAccount(account);
Santos Cordonea5cb932015-05-07 16:28:38 -0700719 // Reset enabled state to whatever the value was if the account was already registered,
Tyler Gunnacb3bc82017-01-09 09:43:56 -0800720 // or _true_ if this is a SIM-based account. All SIM-based accounts are always enabled,
721 // as are all self-managed phone accounts.
Santos Cordonea5cb932015-05-07 16:28:38 -0700722 account.setIsEnabled(
Tyler Gunnacb3bc82017-01-09 09:43:56 -0800723 isEnabled || account.hasCapabilities(PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION)
724 || account.hasCapabilities(PhoneAccount.CAPABILITY_SELF_MANAGED));
Santos Cordon176ae282014-07-14 02:02:14 -0700725
Ihab Awad293edf22014-07-24 17:52:29 -0700726 write();
Ihab Awadb78b2762014-07-25 15:16:23 -0700727 fireAccountsChanged();
Tyler Gunnea4c6fb2017-04-14 13:46:21 -0700728 if (isNewAccount) {
729 fireAccountRegistered(account.getAccountHandle());
730 }
Ihab Awad293edf22014-07-24 17:52:29 -0700731 }
732
Evan Charlton89176372014-07-19 18:23:09 -0700733 public void unregisterPhoneAccount(PhoneAccountHandle accountHandle) {
Tony Mak98e6bdc2015-11-25 21:54:05 +0000734 PhoneAccount account = getPhoneAccountUnchecked(accountHandle);
Santos Cordon6a212642015-05-08 16:35:23 -0700735 if (account != null) {
736 if (mState.accounts.remove(account)) {
737 write();
738 fireAccountsChanged();
Tyler Gunnea4c6fb2017-04-14 13:46:21 -0700739 fireAccountUnRegistered(accountHandle);
Ihab Awad104f8062014-07-17 11:29:35 -0700740 }
741 }
Santos Cordon176ae282014-07-14 02:02:14 -0700742 }
743
Tyler Gunnd900ce62014-08-13 11:40:59 -0700744 /**
745 * Un-registers all phone accounts associated with a specified package.
746 *
747 * @param packageName The package for which phone accounts will be removed.
Evan Charlton105d9772014-11-25 14:08:53 -0800748 * @param userHandle The {@link UserHandle} the package is running under.
Tyler Gunnd900ce62014-08-13 11:40:59 -0700749 */
Evan Charlton105d9772014-11-25 14:08:53 -0800750 public void clearAccounts(String packageName, UserHandle userHandle) {
Tyler Gunnd900ce62014-08-13 11:40:59 -0700751 boolean accountsRemoved = false;
752 Iterator<PhoneAccount> it = mState.accounts.iterator();
753 while (it.hasNext()) {
754 PhoneAccount phoneAccount = it.next();
Evan Charlton105d9772014-11-25 14:08:53 -0800755 PhoneAccountHandle handle = phoneAccount.getAccountHandle();
756 if (Objects.equals(packageName, handle.getComponentName().getPackageName())
757 && Objects.equals(userHandle, handle.getUserHandle())) {
Tyler Gunnd900ce62014-08-13 11:40:59 -0700758 Log.i(this, "Removing phone account " + phoneAccount.getLabel());
Shriram Ganesh8f89f8d2015-04-15 12:35:43 +0530759 mState.accounts.remove(phoneAccount);
Tyler Gunnd900ce62014-08-13 11:40:59 -0700760 accountsRemoved = true;
Ihab Awad104f8062014-07-17 11:29:35 -0700761 }
762 }
763
Tyler Gunnd900ce62014-08-13 11:40:59 -0700764 if (accountsRemoved) {
765 write();
766 fireAccountsChanged();
767 }
Ihab Awadb78b2762014-07-25 15:16:23 -0700768 }
769
Nancy Chen140004a2014-10-15 15:48:38 -0700770 public boolean isVoiceMailNumber(PhoneAccountHandle accountHandle, String number) {
Wink Saville35850602014-10-23 15:57:21 -0700771 int subId = getSubscriptionIdForPhoneAccount(accountHandle);
Yorke Leeb1957232015-06-30 10:09:54 -0700772 return PhoneNumberUtils.isVoiceMailNumber(mContext, subId, number);
Nancy Chen140004a2014-10-15 15:48:38 -0700773 }
774
Ihab Awadb78b2762014-07-25 15:16:23 -0700775 public void addListener(Listener l) {
776 mListeners.add(l);
777 }
778
779 public void removeListener(Listener l) {
Jay Shraunera82c8f72014-08-14 15:49:16 -0700780 if (l != null) {
781 mListeners.remove(l);
782 }
Ihab Awadb78b2762014-07-25 15:16:23 -0700783 }
784
Tyler Gunnea4c6fb2017-04-14 13:46:21 -0700785 private void fireAccountRegistered(PhoneAccountHandle handle) {
786 for (Listener l : mListeners) {
787 l.onPhoneAccountRegistered(this, handle);
788 }
789 }
790
791 private void fireAccountUnRegistered(PhoneAccountHandle handle) {
792 for (Listener l : mListeners) {
793 l.onPhoneAccountUnRegistered(this, handle);
794 }
795 }
796
Ihab Awadb78b2762014-07-25 15:16:23 -0700797 private void fireAccountsChanged() {
798 for (Listener l : mListeners) {
799 l.onAccountsChanged(this);
800 }
801 }
802
803 private void fireDefaultOutgoingChanged() {
804 for (Listener l : mListeners) {
805 l.onDefaultOutgoingChanged(this);
806 }
807 }
808
Santos Cordon5fa4e4f2015-06-10 14:56:01 -0700809 private String getAccountDiffString(PhoneAccount account1, PhoneAccount account2) {
810 if (account1 == null || account2 == null) {
811 return "Diff: " + account1 + ", " + account2;
812 }
813
814 StringBuffer sb = new StringBuffer();
815 sb.append("[").append(account1.getAccountHandle());
Anthony Lee7c64a4e2015-08-25 09:27:47 -0700816 appendDiff(sb, "addr", Log.piiHandle(account1.getAddress()),
817 Log.piiHandle(account2.getAddress()));
Santos Cordon5fa4e4f2015-06-10 14:56:01 -0700818 appendDiff(sb, "cap", account1.getCapabilities(), account2.getCapabilities());
819 appendDiff(sb, "hl", account1.getHighlightColor(), account2.getHighlightColor());
Santos Cordon5fa4e4f2015-06-10 14:56:01 -0700820 appendDiff(sb, "lbl", account1.getLabel(), account2.getLabel());
821 appendDiff(sb, "desc", account1.getShortDescription(), account2.getShortDescription());
Anthony Lee7c64a4e2015-08-25 09:27:47 -0700822 appendDiff(sb, "subAddr", Log.piiHandle(account1.getSubscriptionAddress()),
823 Log.piiHandle(account2.getSubscriptionAddress()));
Santos Cordon5fa4e4f2015-06-10 14:56:01 -0700824 appendDiff(sb, "uris", account1.getSupportedUriSchemes(),
825 account2.getSupportedUriSchemes());
826 sb.append("]");
827 return sb.toString();
828 }
829
830 private void appendDiff(StringBuffer sb, String attrName, Object obj1, Object obj2) {
831 if (!Objects.equals(obj1, obj2)) {
832 sb.append("(")
833 .append(attrName)
834 .append(": ")
835 .append(obj1)
836 .append(" -> ")
837 .append(obj2)
838 .append(")");
839 }
840 }
841
Brad Ebinger0c87b3c2016-06-10 17:23:02 -0700842 private void maybeReplaceOldAccount(PhoneAccount newAccount) {
843 UserHandle newAccountUserHandle = newAccount.getAccountHandle().getUserHandle();
844 DefaultPhoneAccountHandle defaultHandle =
845 getUserSelectedDefaultPhoneAccount(newAccountUserHandle);
846 if (defaultHandle == null || defaultHandle.groupId.isEmpty()) {
847 Log.v(this, "maybeReplaceOldAccount: Not replacing PhoneAccount, no group Id or " +
848 "default.");
849 return;
850 }
851 if (!defaultHandle.groupId.equals(newAccount.getGroupId())) {
852 Log.v(this, "maybeReplaceOldAccount: group Ids are not equal.");
853 return;
854 }
855 if (Objects.equals(newAccount.getAccountHandle().getComponentName(),
856 defaultHandle.phoneAccountHandle.getComponentName())) {
857 // Move default calling account over to new user, since the ComponentNames and Group Ids
858 // are the same.
859 setUserSelectedOutgoingPhoneAccount(newAccount.getAccountHandle(),
860 newAccountUserHandle);
861 } else {
862 Log.v(this, "maybeReplaceOldAccount: group Ids are equal, but ComponentName is not" +
863 " the same as the default. Not replacing default PhoneAccount.");
864 }
865 PhoneAccount replacementAccount = getPhoneAccountByGroupId(newAccount.getGroupId(),
866 newAccount.getAccountHandle().getComponentName(), newAccountUserHandle,
867 newAccount.getAccountHandle());
868 if (replacementAccount != null) {
869 // Unregister the old PhoneAccount.
870 Log.v(this, "maybeReplaceOldAccount: Unregistering old PhoneAccount: " +
871 replacementAccount.getAccountHandle());
872 unregisterPhoneAccount(replacementAccount.getAccountHandle());
873 }
874 }
875
Tyler Gunncb59b672014-08-20 09:02:11 -0700876 /**
Santos Cordon6a212642015-05-08 16:35:23 -0700877 * Determines if the connection service specified by a {@link PhoneAccountHandle} requires the
Yorke Lee7bb8ce92015-05-13 16:28:29 -0700878 * {@link Manifest.permission#BIND_TELECOM_CONNECTION_SERVICE} permission.
Tyler Gunncb59b672014-08-20 09:02:11 -0700879 *
880 * @param phoneAccountHandle The phone account to check.
881 * @return {@code True} if the phone account has permission.
882 */
Santos Cordon6a212642015-05-08 16:35:23 -0700883 public boolean phoneAccountRequiresBindPermission(PhoneAccountHandle phoneAccountHandle) {
Sailesh Nepal91fc8092015-02-14 15:44:55 -0800884 List<ResolveInfo> resolveInfos = resolveComponent(phoneAccountHandle);
885 if (resolveInfos.isEmpty()) {
886 Log.w(this, "phoneAccount %s not found", phoneAccountHandle.getComponentName());
Tyler Gunncb59b672014-08-20 09:02:11 -0700887 return false;
888 }
Sailesh Nepal91fc8092015-02-14 15:44:55 -0800889 for (ResolveInfo resolveInfo : resolveInfos) {
890 ServiceInfo serviceInfo = resolveInfo.serviceInfo;
Yorke Lee7bb8ce92015-05-13 16:28:29 -0700891 if (serviceInfo == null) {
892 return false;
893 }
894
895 if (!Manifest.permission.BIND_CONNECTION_SERVICE.equals(serviceInfo.permission) &&
896 !Manifest.permission.BIND_TELECOM_CONNECTION_SERVICE.equals(
897 serviceInfo.permission)) {
898 // The ConnectionService must require either the deprecated BIND_CONNECTION_SERVICE,
899 // or the public BIND_TELECOM_CONNECTION_SERVICE permissions, both of which are
900 // system/signature only.
Sailesh Nepal91fc8092015-02-14 15:44:55 -0800901 return false;
902 }
903 }
904 return true;
Tyler Gunncb59b672014-08-20 09:02:11 -0700905 }
906
Santos Cordon6a212642015-05-08 16:35:23 -0700907 //
908 // Methods for retrieving PhoneAccounts and PhoneAccountHandles
909 //
Ihab Awad293edf22014-07-24 17:52:29 -0700910
Santos Cordonafe59e52014-08-22 16:48:43 -0700911 /**
Santos Cordon6a212642015-05-08 16:35:23 -0700912 * Returns the PhoneAccount for the specified handle. Does no user checking.
Tyler Gunn84253572014-09-02 14:50:05 -0700913 *
Santos Cordon6a212642015-05-08 16:35:23 -0700914 * @param handle
915 * @return The corresponding phone account if one exists.
Santos Cordonafe59e52014-08-22 16:48:43 -0700916 */
Tony Mak98e6bdc2015-11-25 21:54:05 +0000917 public PhoneAccount getPhoneAccountUnchecked(PhoneAccountHandle handle) {
Santos Cordon6a212642015-05-08 16:35:23 -0700918 for (PhoneAccount m : mState.accounts) {
919 if (Objects.equals(handle, m.getAccountHandle())) {
920 return m;
921 }
922 }
923 return null;
924 }
925
926 /**
927 * Like getPhoneAccount, but checks to see if the current user is allowed to see the phone
928 * account before returning it. The current user is the active user on the actual android
929 * device.
930 */
Tony Mak240656f2015-12-04 11:36:22 +0000931 public PhoneAccount getPhoneAccount(PhoneAccountHandle handle, UserHandle userHandle) {
932 return getPhoneAccount(handle, userHandle, /* acrossProfiles */ false);
933 }
934
935 public PhoneAccount getPhoneAccount(PhoneAccountHandle handle,
936 UserHandle userHandle, boolean acrossProfiles) {
Tony Mak98e6bdc2015-11-25 21:54:05 +0000937 PhoneAccount account = getPhoneAccountUnchecked(handle);
Tony Mak240656f2015-12-04 11:36:22 +0000938 if (account != null && (isVisibleForUser(account, userHandle, acrossProfiles))) {
Santos Cordon6a212642015-05-08 16:35:23 -0700939 return account;
940 }
941 return null;
942 }
943
Tony Mak240656f2015-12-04 11:36:22 +0000944 public PhoneAccount getPhoneAccountOfCurrentUser(PhoneAccountHandle handle) {
945 return getPhoneAccount(handle, mCurrentUserHandle);
946 }
947
Bryce Leea0bb7052015-10-16 13:31:40 -0700948 private List<PhoneAccountHandle> getPhoneAccountHandles(
949 int capabilities,
950 String uriScheme,
951 String packageName,
Tony Mak240656f2015-12-04 11:36:22 +0000952 boolean includeDisabledAccounts,
953 UserHandle userHandle) {
Bryce Leea0bb7052015-10-16 13:31:40 -0700954 return getPhoneAccountHandles(capabilities, 0 /*excludedCapabilities*/, uriScheme,
Tony Mak240656f2015-12-04 11:36:22 +0000955 packageName, includeDisabledAccounts, userHandle);
Bryce Leea0bb7052015-10-16 13:31:40 -0700956 }
957
Santos Cordon6a212642015-05-08 16:35:23 -0700958 /**
959 * Returns a list of phone account handles with the specified capabilities, uri scheme,
960 * and package name.
961 */
962 private List<PhoneAccountHandle> getPhoneAccountHandles(
Santos Cordonea5cb932015-05-07 16:28:38 -0700963 int capabilities,
Bryce Leea0bb7052015-10-16 13:31:40 -0700964 int excludedCapabilities,
Santos Cordonea5cb932015-05-07 16:28:38 -0700965 String uriScheme,
966 String packageName,
Tony Mak240656f2015-12-04 11:36:22 +0000967 boolean includeDisabledAccounts,
968 UserHandle userHandle) {
Santos Cordon6a212642015-05-08 16:35:23 -0700969 List<PhoneAccountHandle> handles = new ArrayList<>();
Santos Cordonea5cb932015-05-07 16:28:38 -0700970
971 for (PhoneAccount account : getPhoneAccounts(
Bryce Leea0bb7052015-10-16 13:31:40 -0700972 capabilities, excludedCapabilities, uriScheme, packageName,
Tony Mak240656f2015-12-04 11:36:22 +0000973 includeDisabledAccounts, userHandle)) {
Santos Cordon6a212642015-05-08 16:35:23 -0700974 handles.add(account.getAccountHandle());
975 }
976 return handles;
Tyler Gunn84253572014-09-02 14:50:05 -0700977 }
978
Bryce Leea0bb7052015-10-16 13:31:40 -0700979 private List<PhoneAccount> getPhoneAccounts(
980 int capabilities,
981 String uriScheme,
982 String packageName,
Tony Mak240656f2015-12-04 11:36:22 +0000983 boolean includeDisabledAccounts,
984 UserHandle userHandle) {
Bryce Leea0bb7052015-10-16 13:31:40 -0700985 return getPhoneAccounts(capabilities, 0 /*excludedCapabilities*/, uriScheme, packageName,
Tony Mak240656f2015-12-04 11:36:22 +0000986 includeDisabledAccounts, userHandle);
Bryce Leea0bb7052015-10-16 13:31:40 -0700987 }
988
Tyler Gunn84253572014-09-02 14:50:05 -0700989 /**
990 * Returns a list of phone account handles with the specified flag, supporting the specified
Santos Cordon6a212642015-05-08 16:35:23 -0700991 * URI scheme, within the specified package name.
Tyler Gunn84253572014-09-02 14:50:05 -0700992 *
Santos Cordon6a212642015-05-08 16:35:23 -0700993 * @param capabilities Capabilities which the {@code PhoneAccount} must have. Ignored if 0.
Bryce Leea0bb7052015-10-16 13:31:40 -0700994 * @param excludedCapabilities Capabilities which the {@code PhoneAccount} must not have.
995 * Ignored if 0.
Santos Cordon6a212642015-05-08 16:35:23 -0700996 * @param uriScheme URI schemes the PhoneAccount must handle. {@code null} bypasses the
Tyler Gunn84253572014-09-02 14:50:05 -0700997 * URI scheme check.
Santos Cordon6a212642015-05-08 16:35:23 -0700998 * @param packageName Package name of the PhoneAccount. {@code null} bypasses packageName check.
Tyler Gunn84253572014-09-02 14:50:05 -0700999 */
Santos Cordon6a212642015-05-08 16:35:23 -07001000 private List<PhoneAccount> getPhoneAccounts(
Santos Cordonea5cb932015-05-07 16:28:38 -07001001 int capabilities,
Bryce Leea0bb7052015-10-16 13:31:40 -07001002 int excludedCapabilities,
Santos Cordonea5cb932015-05-07 16:28:38 -07001003 String uriScheme,
1004 String packageName,
Tony Mak240656f2015-12-04 11:36:22 +00001005 boolean includeDisabledAccounts,
1006 UserHandle userHandle) {
Santos Cordon6a212642015-05-08 16:35:23 -07001007 List<PhoneAccount> accounts = new ArrayList<>(mState.accounts.size());
Ihab Awad293edf22014-07-24 17:52:29 -07001008 for (PhoneAccount m : mState.accounts) {
Santos Cordonea5cb932015-05-07 16:28:38 -07001009 if (!(m.isEnabled() || includeDisabledAccounts)) {
1010 // Do not include disabled accounts.
1011 continue;
1012 }
1013
Bryce Leea0bb7052015-10-16 13:31:40 -07001014 if ((m.getCapabilities() & excludedCapabilities) != 0) {
1015 // If an excluded capability is present, skip.
1016 continue;
1017 }
1018
Santos Cordon6a212642015-05-08 16:35:23 -07001019 if (capabilities != 0 && !m.hasCapabilities(capabilities)) {
Evan Charlton105d9772014-11-25 14:08:53 -08001020 // Account doesn't have the right capabilities; skip this one.
1021 continue;
Ihab Awadf2a84912014-07-22 21:09:25 -07001022 }
Evan Charlton105d9772014-11-25 14:08:53 -08001023 if (uriScheme != null && !m.supportsUriScheme(uriScheme)) {
1024 // Account doesn't support this URI scheme; skip this one.
1025 continue;
1026 }
Santos Cordon6a212642015-05-08 16:35:23 -07001027 PhoneAccountHandle handle = m.getAccountHandle();
1028
1029 if (resolveComponent(handle).isEmpty()) {
Evan Charlton105d9772014-11-25 14:08:53 -08001030 // This component cannot be resolved anymore; skip this one.
1031 continue;
1032 }
Santos Cordon6a212642015-05-08 16:35:23 -07001033 if (packageName != null &&
1034 !packageName.equals(handle.getComponentName().getPackageName())) {
1035 // Not the right package name; skip this one.
1036 continue;
1037 }
Tony Mak240656f2015-12-04 11:36:22 +00001038 if (!isVisibleForUser(m, userHandle, false)) {
Evan Charlton105d9772014-11-25 14:08:53 -08001039 // Account is not visible for the current user; skip this one.
1040 continue;
1041 }
Santos Cordon6a212642015-05-08 16:35:23 -07001042 accounts.add(m);
Ihab Awad104f8062014-07-17 11:29:35 -07001043 }
Santos Cordon6a212642015-05-08 16:35:23 -07001044 return accounts;
Ihab Awad104f8062014-07-17 11:29:35 -07001045 }
1046
Santos Cordon6a212642015-05-08 16:35:23 -07001047 //
1048 // State Implementation for PhoneAccountRegistrar
1049 //
1050
Ihab Awad293edf22014-07-24 17:52:29 -07001051 /**
1052 * The state of this {@code PhoneAccountRegistrar}.
1053 */
Ihab Awadb78b2762014-07-25 15:16:23 -07001054 @VisibleForTesting
1055 public static class State {
Ihab Awad293edf22014-07-24 17:52:29 -07001056 /**
Tony Mak4a3e2fd2015-12-04 11:58:38 +00001057 * Store the default phone account handle of users. If no record of a user can be found in
1058 * the map, it means that no default phone account handle is set in that user.
Ihab Awad293edf22014-07-24 17:52:29 -07001059 */
Tony Mak4a3e2fd2015-12-04 11:58:38 +00001060 public final Map<UserHandle, DefaultPhoneAccountHandle> defaultOutgoingAccountHandles
1061 = new ConcurrentHashMap<>();
Ihab Awad293edf22014-07-24 17:52:29 -07001062
1063 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -07001064 * The complete list of {@code PhoneAccount}s known to the Telecom subsystem.
Ihab Awad293edf22014-07-24 17:52:29 -07001065 */
Shriram Ganesh8f89f8d2015-04-15 12:35:43 +05301066 public final List<PhoneAccount> accounts = new CopyOnWriteArrayList<>();
Tyler Gunn84253572014-09-02 14:50:05 -07001067
1068 /**
1069 * The version number of the State data.
1070 */
1071 public int versionNumber;
Ihab Awad293edf22014-07-24 17:52:29 -07001072 }
1073
Tyler Gunn9787e0e2014-10-14 14:36:12 -07001074 /**
Tony Mak4a3e2fd2015-12-04 11:58:38 +00001075 * The default {@link PhoneAccountHandle} of a user.
1076 */
1077 public static class DefaultPhoneAccountHandle {
1078
1079 public final UserHandle userHandle;
1080
1081 public final PhoneAccountHandle phoneAccountHandle;
1082
Brad Ebinger0c87b3c2016-06-10 17:23:02 -07001083 public final String groupId;
1084
Tony Mak4a3e2fd2015-12-04 11:58:38 +00001085 public DefaultPhoneAccountHandle(UserHandle userHandle,
Brad Ebinger0c87b3c2016-06-10 17:23:02 -07001086 PhoneAccountHandle phoneAccountHandle, String groupId) {
Tony Mak4a3e2fd2015-12-04 11:58:38 +00001087 this.userHandle = userHandle;
1088 this.phoneAccountHandle = phoneAccountHandle;
Brad Ebinger0c87b3c2016-06-10 17:23:02 -07001089 this.groupId = groupId;
Tony Mak4a3e2fd2015-12-04 11:58:38 +00001090 }
1091 }
1092
1093 /**
Tyler Gunn9787e0e2014-10-14 14:36:12 -07001094 * Dumps the state of the {@link CallsManager}.
1095 *
1096 * @param pw The {@code IndentingPrintWriter} to write the state to.
1097 */
1098 public void dump(IndentingPrintWriter pw) {
1099 if (mState != null) {
1100 pw.println("xmlVersion: " + mState.versionNumber);
Tony Mak4a3e2fd2015-12-04 11:58:38 +00001101 DefaultPhoneAccountHandle defaultPhoneAccountHandle
1102 = mState.defaultOutgoingAccountHandles.get(Process.myUserHandle());
1103 pw.println("defaultOutgoing: " + (defaultPhoneAccountHandle == null ? "none" :
1104 defaultPhoneAccountHandle.phoneAccountHandle));
Tony Mak240656f2015-12-04 11:36:22 +00001105 pw.println("simCallManager: " + getSimCallManager(mCurrentUserHandle));
Tyler Gunn9787e0e2014-10-14 14:36:12 -07001106 pw.println("phoneAccounts:");
1107 pw.increaseIndent();
1108 for (PhoneAccount phoneAccount : mState.accounts) {
1109 pw.println(phoneAccount);
1110 }
1111 pw.decreaseIndent();
1112 }
1113 }
1114
Tyler Gunnea921dc2017-04-17 20:45:26 -07001115 private void sortPhoneAccounts() {
1116 if (mState.accounts.size() > 1) {
1117 // Sort the phone accounts using sort order:
1118 // 1) SIM accounts first, followed by non-sim accounts
1119 // 2) Sort order, with those specifying no sort order last.
1120 // 3) Label
1121
1122 // Comparator to sort SIM subscriptions before non-sim subscriptions.
1123 Comparator<PhoneAccount> bySimCapability = (p1, p2) -> {
1124 if (p1.hasCapabilities(PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION)
1125 && !p2.hasCapabilities(PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION)) {
1126 return -1;
1127 } else if (!p1.hasCapabilities(PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION)
1128 && p2.hasCapabilities(PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION)) {
1129 return 1;
1130 } else {
1131 return 0;
1132 }
1133 };
1134
1135 // Create a string comparator which will sort strings, placing nulls last.
1136 Comparator<String> nullSafeStringComparator = Comparator.nullsLast(
1137 String::compareTo);
1138
1139 // Comparator which places PhoneAccounts with a specified sort order first, followed by
1140 // those with no sort order.
1141 Comparator<PhoneAccount> bySortOrder = (p1, p2) -> {
1142 String sort1 = p1.getExtras() == null ? null :
1143 p1.getExtras().getString(PhoneAccount.EXTRA_SORT_ORDER, null);
1144 String sort2 = p2.getExtras() == null ? null :
1145 p2.getExtras().getString(PhoneAccount.EXTRA_SORT_ORDER, null);
1146 return nullSafeStringComparator.compare(sort1, sort2);
1147 };
1148
1149 // Comparator which sorts PhoneAccounts by label.
1150 Comparator<PhoneAccount> byLabel = (p1, p2) -> {
1151 String s1 = p1.getLabel() == null ? null : p1.getLabel().toString();
1152 String s2 = p2.getLabel() == null ? null : p2.getLabel().toString();
1153 return nullSafeStringComparator.compare(s1, s2);
1154 };
1155
1156 // Sort the phone accounts.
1157 mState.accounts.sort(bySimCapability.thenComparing(bySortOrder.thenComparing(byLabel)));
1158 }
1159 }
1160
Ihab Awad293edf22014-07-24 17:52:29 -07001161 ////////////////////////////////////////////////////////////////////////////////////////////////
1162 //
1163 // State management
1164 //
1165
Hall Liu3f7f64d2016-01-07 17:40:13 -08001166 private class AsyncXmlWriter extends AsyncTask<ByteArrayOutputStream, Void, Void> {
1167 @Override
1168 public Void doInBackground(ByteArrayOutputStream... args) {
1169 final ByteArrayOutputStream buffer = args[0];
1170 FileOutputStream fileOutput = null;
Ihab Awadb78b2762014-07-25 15:16:23 -07001171 try {
Hall Liu3f7f64d2016-01-07 17:40:13 -08001172 synchronized (mWriteLock) {
1173 fileOutput = mAtomicFile.startWrite();
1174 buffer.writeTo(fileOutput);
1175 mAtomicFile.finishWrite(fileOutput);
Ihab Awadb78b2762014-07-25 15:16:23 -07001176 }
Hall Liu3f7f64d2016-01-07 17:40:13 -08001177 } catch (IOException e) {
1178 Log.e(this, e, "Writing state to XML file");
1179 mAtomicFile.failWrite(fileOutput);
Ihab Awadb78b2762014-07-25 15:16:23 -07001180 }
Hall Liu3f7f64d2016-01-07 17:40:13 -08001181 return null;
1182 }
1183 }
1184
1185 private void write() {
1186 try {
Tyler Gunnea921dc2017-04-17 20:45:26 -07001187 sortPhoneAccounts();
Hall Liu3f7f64d2016-01-07 17:40:13 -08001188 ByteArrayOutputStream os = new ByteArrayOutputStream();
1189 XmlSerializer serializer = new FastXmlSerializer();
1190 serializer.setOutput(os, "utf-8");
1191 writeToXml(mState, serializer, mContext);
1192 serializer.flush();
1193 new AsyncXmlWriter().execute(os);
Ihab Awadb78b2762014-07-25 15:16:23 -07001194 } catch (IOException e) {
Hall Liu3f7f64d2016-01-07 17:40:13 -08001195 Log.e(this, e, "Writing state to XML buffer");
Ihab Awad104f8062014-07-17 11:29:35 -07001196 }
1197 }
1198
Ihab Awadb78b2762014-07-25 15:16:23 -07001199 private void read() {
1200 final InputStream is;
Ihab Awad104f8062014-07-17 11:29:35 -07001201 try {
Ihab Awadb78b2762014-07-25 15:16:23 -07001202 is = mAtomicFile.openRead();
1203 } catch (FileNotFoundException ex) {
1204 return;
1205 }
1206
Tyler Gunn84253572014-09-02 14:50:05 -07001207 boolean versionChanged = false;
1208
Ihab Awadb78b2762014-07-25 15:16:23 -07001209 XmlPullParser parser;
1210 try {
1211 parser = Xml.newPullParser();
1212 parser.setInput(new BufferedInputStream(is), null);
1213 parser.nextTag();
Tyler Gunn84253572014-09-02 14:50:05 -07001214 mState = readFromXml(parser, mContext);
1215 versionChanged = mState.versionNumber < EXPECTED_STATE_VERSION;
1216
Ihab Awadb78b2762014-07-25 15:16:23 -07001217 } catch (IOException | XmlPullParserException e) {
1218 Log.e(this, e, "Reading state from XML file");
1219 mState = new State();
1220 } finally {
1221 try {
1222 is.close();
1223 } catch (IOException e) {
1224 Log.e(this, e, "Closing InputStream");
1225 }
Ihab Awad104f8062014-07-17 11:29:35 -07001226 }
Tyler Gunn84253572014-09-02 14:50:05 -07001227
Evan Charlton105d9772014-11-25 14:08:53 -08001228 // Verify all of the UserHandles.
1229 List<PhoneAccount> badAccounts = new ArrayList<>();
1230 for (PhoneAccount phoneAccount : mState.accounts) {
1231 UserHandle userHandle = phoneAccount.getAccountHandle().getUserHandle();
1232 if (userHandle == null) {
1233 Log.w(this, "Missing UserHandle for %s", phoneAccount);
1234 badAccounts.add(phoneAccount);
1235 } else if (mUserManager.getSerialNumberForUser(userHandle) == -1) {
1236 Log.w(this, "User does not exist for %s", phoneAccount);
1237 badAccounts.add(phoneAccount);
1238 }
1239 }
1240 mState.accounts.removeAll(badAccounts);
1241
Tyler Gunn84253572014-09-02 14:50:05 -07001242 // If an upgrade occurred, write out the changed data.
Evan Charlton105d9772014-11-25 14:08:53 -08001243 if (versionChanged || !badAccounts.isEmpty()) {
Tyler Gunn84253572014-09-02 14:50:05 -07001244 write();
1245 }
Santos Cordon176ae282014-07-14 02:02:14 -07001246 }
1247
Evan Charlton105d9772014-11-25 14:08:53 -08001248 private static void writeToXml(State state, XmlSerializer serializer, Context context)
Ihab Awadb78b2762014-07-25 15:16:23 -07001249 throws IOException {
Evan Charlton105d9772014-11-25 14:08:53 -08001250 sStateXml.writeToXml(state, serializer, context);
Santos Cordon176ae282014-07-14 02:02:14 -07001251 }
Ihab Awad104f8062014-07-17 11:29:35 -07001252
Tyler Gunn84253572014-09-02 14:50:05 -07001253 private static State readFromXml(XmlPullParser parser, Context context)
Ihab Awadb78b2762014-07-25 15:16:23 -07001254 throws IOException, XmlPullParserException {
Tyler Gunn84253572014-09-02 14:50:05 -07001255 State s = sStateXml.readFromXml(parser, 0, context);
Ihab Awadb78b2762014-07-25 15:16:23 -07001256 return s != null ? s : new State();
Ihab Awad104f8062014-07-17 11:29:35 -07001257 }
1258
Ihab Awad293edf22014-07-24 17:52:29 -07001259 ////////////////////////////////////////////////////////////////////////////////////////////////
Ihab Awad104f8062014-07-17 11:29:35 -07001260 //
Ihab Awadb78b2762014-07-25 15:16:23 -07001261 // XML serialization
Ihab Awad104f8062014-07-17 11:29:35 -07001262 //
1263
Ihab Awadb78b2762014-07-25 15:16:23 -07001264 @VisibleForTesting
Ihab Awad26923222014-07-30 10:54:35 -07001265 public abstract static class XmlSerialization<T> {
Tyler Gunn369c8742015-10-07 15:39:28 -07001266 private static final String TAG_VALUE = "value";
1267 private static final String ATTRIBUTE_LENGTH = "length";
1268 private static final String ATTRIBUTE_KEY = "key";
1269 private static final String ATTRIBUTE_VALUE_TYPE = "type";
1270 private static final String VALUE_TYPE_STRING = "string";
1271 private static final String VALUE_TYPE_INTEGER = "integer";
1272 private static final String VALUE_TYPE_BOOLEAN = "boolean";
Tyler Gunn84253572014-09-02 14:50:05 -07001273
Ihab Awadb78b2762014-07-25 15:16:23 -07001274 /**
1275 * Write the supplied object to XML
1276 */
Evan Charlton105d9772014-11-25 14:08:53 -08001277 public abstract void writeToXml(T o, XmlSerializer serializer, Context context)
Ihab Awad26923222014-07-30 10:54:35 -07001278 throws IOException;
Ihab Awadb78b2762014-07-25 15:16:23 -07001279
1280 /**
1281 * Read from the supplied XML into a new object, returning null in case of an
1282 * unrecoverable schema mismatch or other data error. 'parser' must be already
1283 * positioned at the first tag that is expected to have been emitted by this
1284 * object's writeToXml(). This object tries to fail early without modifying
1285 * 'parser' if it does not recognize the data it sees.
1286 */
Tyler Gunn84253572014-09-02 14:50:05 -07001287 public abstract T readFromXml(XmlPullParser parser, int version, Context context)
Ihab Awad26923222014-07-30 10:54:35 -07001288 throws IOException, XmlPullParserException;
1289
Ihab Awadd9f54382014-10-24 11:44:47 -07001290 protected void writeTextIfNonNull(String tagName, Object value, XmlSerializer serializer)
Ihab Awad26923222014-07-30 10:54:35 -07001291 throws IOException {
1292 if (value != null) {
1293 serializer.startTag(null, tagName);
1294 serializer.text(Objects.toString(value));
1295 serializer.endTag(null, tagName);
1296 }
1297 }
Tyler Gunn84253572014-09-02 14:50:05 -07001298
1299 /**
1300 * Serializes a string array.
1301 *
1302 * @param tagName The tag name for the string array.
1303 * @param values The string values to serialize.
1304 * @param serializer The serializer.
1305 * @throws IOException
1306 */
1307 protected void writeStringList(String tagName, List<String> values,
1308 XmlSerializer serializer)
1309 throws IOException {
1310
1311 serializer.startTag(null, tagName);
1312 if (values != null) {
Tyler Gunn369c8742015-10-07 15:39:28 -07001313 serializer.attribute(null, ATTRIBUTE_LENGTH, Objects.toString(values.size()));
Tyler Gunn84253572014-09-02 14:50:05 -07001314 for (String toSerialize : values) {
Tyler Gunn369c8742015-10-07 15:39:28 -07001315 serializer.startTag(null, TAG_VALUE);
Tyler Gunn84253572014-09-02 14:50:05 -07001316 if (toSerialize != null ){
1317 serializer.text(toSerialize);
1318 }
Tyler Gunn369c8742015-10-07 15:39:28 -07001319 serializer.endTag(null, TAG_VALUE);
Tyler Gunn84253572014-09-02 14:50:05 -07001320 }
1321 } else {
Tyler Gunn369c8742015-10-07 15:39:28 -07001322 serializer.attribute(null, ATTRIBUTE_LENGTH, "0");
1323 }
1324 serializer.endTag(null, tagName);
1325 }
1326
1327 protected void writeBundle(String tagName, Bundle values, XmlSerializer serializer)
1328 throws IOException {
1329
1330 serializer.startTag(null, tagName);
1331 if (values != null) {
1332 for (String key : values.keySet()) {
1333 Object value = values.get(key);
1334
1335 if (value == null) {
1336 continue;
1337 }
1338
1339 String valueType;
1340 if (value instanceof String) {
1341 valueType = VALUE_TYPE_STRING;
1342 } else if (value instanceof Integer) {
1343 valueType = VALUE_TYPE_INTEGER;
1344 } else if (value instanceof Boolean) {
1345 valueType = VALUE_TYPE_BOOLEAN;
1346 } else {
1347 Log.w(this,
1348 "PhoneAccounts support only string, integer and boolean extras TY.");
1349 continue;
1350 }
1351
1352 serializer.startTag(null, TAG_VALUE);
1353 serializer.attribute(null, ATTRIBUTE_KEY, key);
1354 serializer.attribute(null, ATTRIBUTE_VALUE_TYPE, valueType);
1355 serializer.text(Objects.toString(value));
1356 serializer.endTag(null, TAG_VALUE);
1357 }
Tyler Gunn84253572014-09-02 14:50:05 -07001358 }
1359 serializer.endTag(null, tagName);
Ihab Awadd9f54382014-10-24 11:44:47 -07001360 }
Tyler Gunn84253572014-09-02 14:50:05 -07001361
Santos Cordon9c30c282015-05-13 16:28:27 -07001362 protected void writeIconIfNonNull(String tagName, Icon value, XmlSerializer serializer)
Ihab Awadd9f54382014-10-24 11:44:47 -07001363 throws IOException {
Santos Cordon9c30c282015-05-13 16:28:27 -07001364 if (value != null) {
Ihab Awadd9f54382014-10-24 11:44:47 -07001365 ByteArrayOutputStream stream = new ByteArrayOutputStream();
Santos Cordon9c30c282015-05-13 16:28:27 -07001366 value.writeToStream(stream);
1367 byte[] iconByteArray = stream.toByteArray();
1368 String text = Base64.encodeToString(iconByteArray, 0, iconByteArray.length, 0);
Ihab Awadd9f54382014-10-24 11:44:47 -07001369
1370 serializer.startTag(null, tagName);
1371 serializer.text(text);
1372 serializer.endTag(null, tagName);
1373 }
Tyler Gunn84253572014-09-02 14:50:05 -07001374 }
1375
Evan Charlton105d9772014-11-25 14:08:53 -08001376 protected void writeLong(String tagName, long value, XmlSerializer serializer)
1377 throws IOException {
1378 serializer.startTag(null, tagName);
1379 serializer.text(Long.valueOf(value).toString());
1380 serializer.endTag(null, tagName);
1381 }
1382
Brad Ebinger0c87b3c2016-06-10 17:23:02 -07001383 protected void writeNonNullString(String tagName, String value, XmlSerializer serializer)
1384 throws IOException {
1385 serializer.startTag(null, tagName);
1386 serializer.text(value != null ? value : "");
1387 serializer.endTag(null, tagName);
1388 }
1389
Tyler Gunn84253572014-09-02 14:50:05 -07001390 /**
1391 * Reads a string array from the XML parser.
1392 *
1393 * @param parser The XML parser.
1394 * @return String array containing the parsed values.
1395 * @throws IOException Exception related to IO.
1396 * @throws XmlPullParserException Exception related to parsing.
1397 */
1398 protected List<String> readStringList(XmlPullParser parser)
1399 throws IOException, XmlPullParserException {
1400
Tyler Gunn369c8742015-10-07 15:39:28 -07001401 int length = Integer.parseInt(parser.getAttributeValue(null, ATTRIBUTE_LENGTH));
Tyler Gunn84253572014-09-02 14:50:05 -07001402 List<String> arrayEntries = new ArrayList<String>(length);
1403 String value = null;
1404
1405 if (length == 0) {
1406 return arrayEntries;
1407 }
1408
1409 int outerDepth = parser.getDepth();
1410 while (XmlUtils.nextElementWithin(parser, outerDepth)) {
Tyler Gunn369c8742015-10-07 15:39:28 -07001411 if (parser.getName().equals(TAG_VALUE)) {
Tyler Gunn8e0fef42014-09-08 18:34:44 -07001412 parser.next();
Tyler Gunn84253572014-09-02 14:50:05 -07001413 value = parser.getText();
1414 arrayEntries.add(value);
1415 }
1416 }
1417
1418 return arrayEntries;
1419 }
Ihab Awadd9f54382014-10-24 11:44:47 -07001420
Tyler Gunn369c8742015-10-07 15:39:28 -07001421 /**
1422 * Reads a bundle from the XML parser.
1423 *
1424 * @param parser The XML parser.
1425 * @return Bundle containing the parsed values.
1426 * @throws IOException Exception related to IO.
1427 * @throws XmlPullParserException Exception related to parsing.
1428 */
1429 protected Bundle readBundle(XmlPullParser parser)
1430 throws IOException, XmlPullParserException {
1431
1432 Bundle bundle = null;
1433 int outerDepth = parser.getDepth();
1434 while (XmlUtils.nextElementWithin(parser, outerDepth)) {
1435 if (parser.getName().equals(TAG_VALUE)) {
1436 String valueType = parser.getAttributeValue(null, ATTRIBUTE_VALUE_TYPE);
1437 String key = parser.getAttributeValue(null, ATTRIBUTE_KEY);
1438 parser.next();
1439 String value = parser.getText();
1440
1441 if (bundle == null) {
1442 bundle = new Bundle();
1443 }
1444
1445 // Do not write null values to the bundle.
1446 if (value == null) {
1447 continue;
1448 }
1449
1450 if (VALUE_TYPE_STRING.equals(valueType)) {
1451 bundle.putString(key, value);
1452 } else if (VALUE_TYPE_INTEGER.equals(valueType)) {
1453 try {
1454 int intValue = Integer.parseInt(value);
1455 bundle.putInt(key, intValue);
1456 } catch (NumberFormatException nfe) {
1457 Log.w(this, "Invalid integer PhoneAccount extra.");
1458 }
1459 } else if (VALUE_TYPE_BOOLEAN.equals(valueType)) {
1460 boolean boolValue = Boolean.parseBoolean(value);
1461 bundle.putBoolean(key, boolValue);
1462 } else {
1463 Log.w(this, "Invalid type " + valueType + " for PhoneAccount bundle.");
1464 }
1465 }
1466 }
1467 return bundle;
1468 }
1469
Santos Cordon9c30c282015-05-13 16:28:27 -07001470 protected Bitmap readBitmap(XmlPullParser parser) {
Ihab Awadd9f54382014-10-24 11:44:47 -07001471 byte[] imageByteArray = Base64.decode(parser.getText(), 0);
1472 return BitmapFactory.decodeByteArray(imageByteArray, 0, imageByteArray.length);
1473 }
Santos Cordon9c30c282015-05-13 16:28:27 -07001474
1475 protected Icon readIcon(XmlPullParser parser) throws IOException {
1476 byte[] iconByteArray = Base64.decode(parser.getText(), 0);
1477 ByteArrayInputStream stream = new ByteArrayInputStream(iconByteArray);
1478 return Icon.createFromStream(stream);
1479 }
Ihab Awad104f8062014-07-17 11:29:35 -07001480 }
1481
Ihab Awadb78b2762014-07-25 15:16:23 -07001482 @VisibleForTesting
1483 public static final XmlSerialization<State> sStateXml =
1484 new XmlSerialization<State>() {
1485 private static final String CLASS_STATE = "phone_account_registrar_state";
Ihab Awad104f8062014-07-17 11:29:35 -07001486 private static final String DEFAULT_OUTGOING = "default_outgoing";
1487 private static final String ACCOUNTS = "accounts";
Tyler Gunn84253572014-09-02 14:50:05 -07001488 private static final String VERSION = "version";
Ihab Awad104f8062014-07-17 11:29:35 -07001489
1490 @Override
Evan Charlton105d9772014-11-25 14:08:53 -08001491 public void writeToXml(State o, XmlSerializer serializer, Context context)
Ihab Awadb78b2762014-07-25 15:16:23 -07001492 throws IOException {
Ihab Awad26923222014-07-30 10:54:35 -07001493 if (o != null) {
1494 serializer.startTag(null, CLASS_STATE);
Tyler Gunn84253572014-09-02 14:50:05 -07001495 serializer.attribute(null, VERSION, Objects.toString(EXPECTED_STATE_VERSION));
Ihab Awadb78b2762014-07-25 15:16:23 -07001496
Tony Mak4a3e2fd2015-12-04 11:58:38 +00001497 serializer.startTag(null, DEFAULT_OUTGOING);
1498 for (DefaultPhoneAccountHandle defaultPhoneAccountHandle : o
1499 .defaultOutgoingAccountHandles.values()) {
1500 sDefaultPhoneAcountHandleXml
1501 .writeToXml(defaultPhoneAccountHandle, serializer, context);
Ihab Awad26923222014-07-30 10:54:35 -07001502 }
Tony Mak4a3e2fd2015-12-04 11:58:38 +00001503 serializer.endTag(null, DEFAULT_OUTGOING);
Ihab Awad26923222014-07-30 10:54:35 -07001504
Ihab Awad26923222014-07-30 10:54:35 -07001505 serializer.startTag(null, ACCOUNTS);
1506 for (PhoneAccount m : o.accounts) {
Evan Charlton105d9772014-11-25 14:08:53 -08001507 sPhoneAccountXml.writeToXml(m, serializer, context);
Ihab Awad26923222014-07-30 10:54:35 -07001508 }
1509 serializer.endTag(null, ACCOUNTS);
1510
1511 serializer.endTag(null, CLASS_STATE);
Ihab Awad293edf22014-07-24 17:52:29 -07001512 }
Ihab Awad104f8062014-07-17 11:29:35 -07001513 }
1514
1515 @Override
Tyler Gunn84253572014-09-02 14:50:05 -07001516 public State readFromXml(XmlPullParser parser, int version, Context context)
Ihab Awadb78b2762014-07-25 15:16:23 -07001517 throws IOException, XmlPullParserException {
1518 if (parser.getName().equals(CLASS_STATE)) {
1519 State s = new State();
Tyler Gunn84253572014-09-02 14:50:05 -07001520
1521 String rawVersion = parser.getAttributeValue(null, VERSION);
Tony Mak4a3e2fd2015-12-04 11:58:38 +00001522 s.versionNumber = TextUtils.isEmpty(rawVersion) ? 1 : Integer.parseInt(rawVersion);
Tyler Gunn84253572014-09-02 14:50:05 -07001523
Ihab Awadb78b2762014-07-25 15:16:23 -07001524 int outerDepth = parser.getDepth();
1525 while (XmlUtils.nextElementWithin(parser, outerDepth)) {
1526 if (parser.getName().equals(DEFAULT_OUTGOING)) {
Tony Mak4a3e2fd2015-12-04 11:58:38 +00001527 if (s.versionNumber < 9) {
Brad Ebinger0c87b3c2016-06-10 17:23:02 -07001528 // Migrate old default phone account handle here by assuming the
1529 // default phone account handle belongs to the primary user. Also,
1530 // assume there are no groups.
Tony Mak4a3e2fd2015-12-04 11:58:38 +00001531 parser.nextTag();
1532 PhoneAccountHandle phoneAccountHandle = sPhoneAccountHandleXml
1533 .readFromXml(parser, s.versionNumber, context);
1534 UserManager userManager = UserManager.get(context);
1535 UserInfo primaryUser = userManager.getPrimaryUser();
1536 if (primaryUser != null) {
1537 UserHandle userHandle = primaryUser.getUserHandle();
1538 DefaultPhoneAccountHandle defaultPhoneAccountHandle
1539 = new DefaultPhoneAccountHandle(userHandle,
Brad Ebinger0c87b3c2016-06-10 17:23:02 -07001540 phoneAccountHandle, "" /* groupId */);
Tony Mak4a3e2fd2015-12-04 11:58:38 +00001541 s.defaultOutgoingAccountHandles
1542 .put(userHandle, defaultPhoneAccountHandle);
1543 }
1544 } else {
1545 int defaultAccountHandlesDepth = parser.getDepth();
1546 while (XmlUtils.nextElementWithin(parser, defaultAccountHandlesDepth)) {
1547 DefaultPhoneAccountHandle accountHandle
1548 = sDefaultPhoneAcountHandleXml
1549 .readFromXml(parser, s.versionNumber, context);
1550 if (accountHandle != null && s.accounts != null) {
1551 s.defaultOutgoingAccountHandles
1552 .put(accountHandle.userHandle, accountHandle);
1553 }
1554 }
1555 }
Ihab Awadb78b2762014-07-25 15:16:23 -07001556 } else if (parser.getName().equals(ACCOUNTS)) {
1557 int accountsDepth = parser.getDepth();
1558 while (XmlUtils.nextElementWithin(parser, accountsDepth)) {
Tyler Gunn84253572014-09-02 14:50:05 -07001559 PhoneAccount account = sPhoneAccountXml.readFromXml(parser,
1560 s.versionNumber, context);
1561
1562 if (account != null && s.accounts != null) {
Ihab Awadb78b2762014-07-25 15:16:23 -07001563 s.accounts.add(account);
1564 }
1565 }
Ihab Awad104f8062014-07-17 11:29:35 -07001566 }
1567 }
Ihab Awadb78b2762014-07-25 15:16:23 -07001568 return s;
Ihab Awad104f8062014-07-17 11:29:35 -07001569 }
Ihab Awadb78b2762014-07-25 15:16:23 -07001570 return null;
Ihab Awad104f8062014-07-17 11:29:35 -07001571 }
1572 };
1573
Ihab Awadb78b2762014-07-25 15:16:23 -07001574 @VisibleForTesting
Tony Mak4a3e2fd2015-12-04 11:58:38 +00001575 public static final XmlSerialization<DefaultPhoneAccountHandle> sDefaultPhoneAcountHandleXml =
1576 new XmlSerialization<DefaultPhoneAccountHandle>() {
1577 private static final String CLASS_DEFAULT_OUTGOING_PHONE_ACCOUNT_HANDLE
1578 = "default_outgoing_phone_account_handle";
1579 private static final String USER_SERIAL_NUMBER = "user_serial_number";
Brad Ebinger0c87b3c2016-06-10 17:23:02 -07001580 private static final String GROUP_ID = "group_id";
Tony Mak4a3e2fd2015-12-04 11:58:38 +00001581 private static final String ACCOUNT_HANDLE = "account_handle";
1582
1583 @Override
1584 public void writeToXml(DefaultPhoneAccountHandle o, XmlSerializer serializer,
1585 Context context) throws IOException {
1586 if (o != null) {
1587 final UserManager userManager = UserManager.get(context);
1588 final long serialNumber = userManager.getSerialNumberForUser(o.userHandle);
1589 if (serialNumber != -1) {
1590 serializer.startTag(null, CLASS_DEFAULT_OUTGOING_PHONE_ACCOUNT_HANDLE);
1591 writeLong(USER_SERIAL_NUMBER, serialNumber, serializer);
Brad Ebinger0c87b3c2016-06-10 17:23:02 -07001592 writeNonNullString(GROUP_ID, o.groupId, serializer);
Tony Mak4a3e2fd2015-12-04 11:58:38 +00001593 serializer.startTag(null, ACCOUNT_HANDLE);
1594 sPhoneAccountHandleXml.writeToXml(o.phoneAccountHandle, serializer,
1595 context);
1596 serializer.endTag(null, ACCOUNT_HANDLE);
1597 serializer.endTag(null, CLASS_DEFAULT_OUTGOING_PHONE_ACCOUNT_HANDLE);
1598 }
1599 }
1600 }
1601
1602 @Override
1603 public DefaultPhoneAccountHandle readFromXml(XmlPullParser parser, int version,
1604 Context context)
1605 throws IOException, XmlPullParserException {
1606 if (parser.getName().equals(CLASS_DEFAULT_OUTGOING_PHONE_ACCOUNT_HANDLE)) {
1607 int outerDepth = parser.getDepth();
1608 PhoneAccountHandle accountHandle = null;
1609 String userSerialNumberString = null;
Brad Ebinger0c87b3c2016-06-10 17:23:02 -07001610 String groupId = "";
Tony Mak4a3e2fd2015-12-04 11:58:38 +00001611 while (XmlUtils.nextElementWithin(parser, outerDepth)) {
1612 if (parser.getName().equals(ACCOUNT_HANDLE)) {
1613 parser.nextTag();
1614 accountHandle = sPhoneAccountHandleXml.readFromXml(parser, version,
1615 context);
1616 } else if (parser.getName().equals(USER_SERIAL_NUMBER)) {
1617 parser.next();
1618 userSerialNumberString = parser.getText();
Brad Ebinger0c87b3c2016-06-10 17:23:02 -07001619 } else if (parser.getName().equals(GROUP_ID)) {
Hall Liuded880c2017-01-20 16:13:07 -08001620 if (parser.next() == XmlPullParser.TEXT) {
1621 groupId = parser.getText();
1622 }
Tony Mak4a3e2fd2015-12-04 11:58:38 +00001623 }
1624 }
1625 UserHandle userHandle = null;
1626 if (userSerialNumberString != null) {
1627 try {
1628 long serialNumber = Long.parseLong(userSerialNumberString);
1629 userHandle = UserManager.get(context)
1630 .getUserForSerialNumber(serialNumber);
1631 } catch (NumberFormatException e) {
1632 Log.e(this, e,
1633 "Could not parse UserHandle " + userSerialNumberString);
1634 }
1635 }
Brad Ebinger0c87b3c2016-06-10 17:23:02 -07001636 if (accountHandle != null && userHandle != null && groupId != null) {
1637 return new DefaultPhoneAccountHandle(userHandle, accountHandle,
1638 groupId);
Tony Mak4a3e2fd2015-12-04 11:58:38 +00001639 }
1640 }
1641 return null;
1642 }
1643 };
1644
1645
1646 @VisibleForTesting
Ihab Awadb78b2762014-07-25 15:16:23 -07001647 public static final XmlSerialization<PhoneAccount> sPhoneAccountXml =
1648 new XmlSerialization<PhoneAccount>() {
1649 private static final String CLASS_PHONE_ACCOUNT = "phone_account";
1650 private static final String ACCOUNT_HANDLE = "account_handle";
Andrew Lee7129f1c2014-09-04 11:55:07 -07001651 private static final String ADDRESS = "handle";
1652 private static final String SUBSCRIPTION_ADDRESS = "subscription_number";
Ihab Awad104f8062014-07-17 11:29:35 -07001653 private static final String CAPABILITIES = "capabilities";
Christine Hallstromffe558c2016-11-30 16:05:13 -08001654 private static final String SUPPORTED_AUDIO_ROUTES = "supported_audio_routes";
Ihab Awad104f8062014-07-17 11:29:35 -07001655 private static final String ICON_RES_ID = "icon_res_id";
Ihab Awadd9f54382014-10-24 11:44:47 -07001656 private static final String ICON_PACKAGE_NAME = "icon_package_name";
1657 private static final String ICON_BITMAP = "icon_bitmap";
Ihab Awad7e2c7f32014-11-03 09:49:45 -08001658 private static final String ICON_TINT = "icon_tint";
1659 private static final String HIGHLIGHT_COLOR = "highlight_color";
Ihab Awad104f8062014-07-17 11:29:35 -07001660 private static final String LABEL = "label";
1661 private static final String SHORT_DESCRIPTION = "short_description";
Tyler Gunn84253572014-09-02 14:50:05 -07001662 private static final String SUPPORTED_URI_SCHEMES = "supported_uri_schemes";
Santos Cordon9c30c282015-05-13 16:28:27 -07001663 private static final String ICON = "icon";
Tyler Gunn369c8742015-10-07 15:39:28 -07001664 private static final String EXTRAS = "extras";
Santos Cordonea5cb932015-05-07 16:28:38 -07001665 private static final String ENABLED = "enabled";
Ihab Awad104f8062014-07-17 11:29:35 -07001666
1667 @Override
Evan Charlton105d9772014-11-25 14:08:53 -08001668 public void writeToXml(PhoneAccount o, XmlSerializer serializer, Context context)
Ihab Awadb78b2762014-07-25 15:16:23 -07001669 throws IOException {
Ihab Awad26923222014-07-30 10:54:35 -07001670 if (o != null) {
1671 serializer.startTag(null, CLASS_PHONE_ACCOUNT);
Ihab Awadb78b2762014-07-25 15:16:23 -07001672
Ihab Awad26923222014-07-30 10:54:35 -07001673 if (o.getAccountHandle() != null) {
1674 serializer.startTag(null, ACCOUNT_HANDLE);
Evan Charlton105d9772014-11-25 14:08:53 -08001675 sPhoneAccountHandleXml.writeToXml(o.getAccountHandle(), serializer, context);
Ihab Awad26923222014-07-30 10:54:35 -07001676 serializer.endTag(null, ACCOUNT_HANDLE);
1677 }
Ihab Awadb78b2762014-07-25 15:16:23 -07001678
Ihab Awadd9f54382014-10-24 11:44:47 -07001679 writeTextIfNonNull(ADDRESS, o.getAddress(), serializer);
1680 writeTextIfNonNull(SUBSCRIPTION_ADDRESS, o.getSubscriptionAddress(), serializer);
1681 writeTextIfNonNull(CAPABILITIES, Integer.toString(o.getCapabilities()), serializer);
Santos Cordon9c30c282015-05-13 16:28:27 -07001682 writeIconIfNonNull(ICON, o.getIcon(), serializer);
Ihab Awad7e2c7f32014-11-03 09:49:45 -08001683 writeTextIfNonNull(HIGHLIGHT_COLOR,
1684 Integer.toString(o.getHighlightColor()), serializer);
Ihab Awadd9f54382014-10-24 11:44:47 -07001685 writeTextIfNonNull(LABEL, o.getLabel(), serializer);
1686 writeTextIfNonNull(SHORT_DESCRIPTION, o.getShortDescription(), serializer);
Tyler Gunn84253572014-09-02 14:50:05 -07001687 writeStringList(SUPPORTED_URI_SCHEMES, o.getSupportedUriSchemes(), serializer);
Tyler Gunn369c8742015-10-07 15:39:28 -07001688 writeBundle(EXTRAS, o.getExtras(), serializer);
Santos Cordonea5cb932015-05-07 16:28:38 -07001689 writeTextIfNonNull(ENABLED, o.isEnabled() ? "true" : "false" , serializer);
Christine Hallstromffe558c2016-11-30 16:05:13 -08001690 writeTextIfNonNull(SUPPORTED_AUDIO_ROUTES, Integer.toString(
1691 o.getSupportedAudioRoutes()), serializer);
Ihab Awadb78b2762014-07-25 15:16:23 -07001692
Ihab Awad26923222014-07-30 10:54:35 -07001693 serializer.endTag(null, CLASS_PHONE_ACCOUNT);
1694 }
Ihab Awad104f8062014-07-17 11:29:35 -07001695 }
1696
Tyler Gunn84253572014-09-02 14:50:05 -07001697 public PhoneAccount readFromXml(XmlPullParser parser, int version, Context context)
Ihab Awadb78b2762014-07-25 15:16:23 -07001698 throws IOException, XmlPullParserException {
1699 if (parser.getName().equals(CLASS_PHONE_ACCOUNT)) {
1700 int outerDepth = parser.getDepth();
1701 PhoneAccountHandle accountHandle = null;
Andrew Lee7129f1c2014-09-04 11:55:07 -07001702 Uri address = null;
1703 Uri subscriptionAddress = null;
Ihab Awadb78b2762014-07-25 15:16:23 -07001704 int capabilities = 0;
Christine Hallstromffe558c2016-11-30 16:05:13 -08001705 int supportedAudioRoutes = 0;
Ihab Awad7e2c7f32014-11-03 09:49:45 -08001706 int iconResId = PhoneAccount.NO_RESOURCE_ID;
Ihab Awadd9f54382014-10-24 11:44:47 -07001707 String iconPackageName = null;
Ihab Awad7e2c7f32014-11-03 09:49:45 -08001708 Bitmap iconBitmap = null;
Ihab Awad07bc5ee2014-11-12 13:42:52 -08001709 int iconTint = PhoneAccount.NO_ICON_TINT;
1710 int highlightColor = PhoneAccount.NO_HIGHLIGHT_COLOR;
Ihab Awadb78b2762014-07-25 15:16:23 -07001711 String label = null;
1712 String shortDescription = null;
Tyler Gunn84253572014-09-02 14:50:05 -07001713 List<String> supportedUriSchemes = null;
Santos Cordon9c30c282015-05-13 16:28:27 -07001714 Icon icon = null;
Santos Cordonea5cb932015-05-07 16:28:38 -07001715 boolean enabled = false;
Tyler Gunn369c8742015-10-07 15:39:28 -07001716 Bundle extras = null;
Ihab Awadb78b2762014-07-25 15:16:23 -07001717
1718 while (XmlUtils.nextElementWithin(parser, outerDepth)) {
1719 if (parser.getName().equals(ACCOUNT_HANDLE)) {
1720 parser.nextTag();
Tyler Gunn84253572014-09-02 14:50:05 -07001721 accountHandle = sPhoneAccountHandleXml.readFromXml(parser, version,
1722 context);
Andrew Lee7129f1c2014-09-04 11:55:07 -07001723 } else if (parser.getName().equals(ADDRESS)) {
Ihab Awadb78b2762014-07-25 15:16:23 -07001724 parser.next();
Andrew Lee7129f1c2014-09-04 11:55:07 -07001725 address = Uri.parse(parser.getText());
1726 } else if (parser.getName().equals(SUBSCRIPTION_ADDRESS)) {
Ihab Awadb78b2762014-07-25 15:16:23 -07001727 parser.next();
Andrew Lee7129f1c2014-09-04 11:55:07 -07001728 String nextText = parser.getText();
1729 subscriptionAddress = nextText == null ? null : Uri.parse(nextText);
Ihab Awadb78b2762014-07-25 15:16:23 -07001730 } else if (parser.getName().equals(CAPABILITIES)) {
1731 parser.next();
1732 capabilities = Integer.parseInt(parser.getText());
1733 } else if (parser.getName().equals(ICON_RES_ID)) {
1734 parser.next();
1735 iconResId = Integer.parseInt(parser.getText());
Ihab Awadd9f54382014-10-24 11:44:47 -07001736 } else if (parser.getName().equals(ICON_PACKAGE_NAME)) {
1737 parser.next();
1738 iconPackageName = parser.getText();
1739 } else if (parser.getName().equals(ICON_BITMAP)) {
1740 parser.next();
Ihab Awad7e2c7f32014-11-03 09:49:45 -08001741 iconBitmap = readBitmap(parser);
1742 } else if (parser.getName().equals(ICON_TINT)) {
Nancy Chen06ce0622014-10-23 01:17:35 +00001743 parser.next();
Ihab Awad7e2c7f32014-11-03 09:49:45 -08001744 iconTint = Integer.parseInt(parser.getText());
1745 } else if (parser.getName().equals(HIGHLIGHT_COLOR)) {
1746 parser.next();
1747 highlightColor = Integer.parseInt(parser.getText());
Ihab Awadb78b2762014-07-25 15:16:23 -07001748 } else if (parser.getName().equals(LABEL)) {
1749 parser.next();
1750 label = parser.getText();
1751 } else if (parser.getName().equals(SHORT_DESCRIPTION)) {
1752 parser.next();
1753 shortDescription = parser.getText();
Tyler Gunn84253572014-09-02 14:50:05 -07001754 } else if (parser.getName().equals(SUPPORTED_URI_SCHEMES)) {
1755 supportedUriSchemes = readStringList(parser);
Santos Cordon9c30c282015-05-13 16:28:27 -07001756 } else if (parser.getName().equals(ICON)) {
1757 parser.next();
1758 icon = readIcon(parser);
Santos Cordonea5cb932015-05-07 16:28:38 -07001759 } else if (parser.getName().equals(ENABLED)) {
1760 parser.next();
1761 enabled = "true".equalsIgnoreCase(parser.getText());
Tyler Gunn369c8742015-10-07 15:39:28 -07001762 } else if (parser.getName().equals(EXTRAS)) {
Tyler Gunn369c8742015-10-07 15:39:28 -07001763 extras = readBundle(parser);
Christine Hallstromffe558c2016-11-30 16:05:13 -08001764 } else if (parser.getName().equals(SUPPORTED_AUDIO_ROUTES)) {
1765 parser.next();
1766 supportedAudioRoutes = Integer.parseInt(parser.getText());
Ihab Awadb78b2762014-07-25 15:16:23 -07001767 }
1768 }
Tyler Gunn84253572014-09-02 14:50:05 -07001769
Santos Cordona9eebe42015-06-11 14:07:44 -07001770 ComponentName pstnComponentName = new ComponentName("com.android.phone",
1771 "com.android.services.telephony.TelephonyConnectionService");
Santos Cordona82aed82015-05-26 10:43:56 -07001772 ComponentName sipComponentName = new ComponentName("com.android.phone",
1773 "com.android.services.telephony.sip.SipConnectionService");
1774
Tyler Gunn84253572014-09-02 14:50:05 -07001775 // Upgrade older phone accounts to specify the supported URI schemes.
1776 if (version < 2) {
Tyler Gunn84253572014-09-02 14:50:05 -07001777 supportedUriSchemes = new ArrayList<>();
1778
1779 // Handle the SIP connection service.
1780 // Check the system settings to see if it also should handle "tel" calls.
1781 if (accountHandle.getComponentName().equals(sipComponentName)) {
1782 boolean useSipForPstn = useSipForPstnCalls(context);
1783 supportedUriSchemes.add(PhoneAccount.SCHEME_SIP);
1784 if (useSipForPstn) {
1785 supportedUriSchemes.add(PhoneAccount.SCHEME_TEL);
1786 }
1787 } else {
1788 supportedUriSchemes.add(PhoneAccount.SCHEME_TEL);
1789 supportedUriSchemes.add(PhoneAccount.SCHEME_VOICEMAIL);
1790 }
1791 }
1792
Ihab Awad7e2c7f32014-11-03 09:49:45 -08001793 // Upgrade older phone accounts with explicit package name
1794 if (version < 5) {
1795 if (iconBitmap == null) {
1796 iconPackageName = accountHandle.getComponentName().getPackageName();
1797 }
1798 }
1799
Santos Cordona82aed82015-05-26 10:43:56 -07001800 if (version < 6) {
1801 // Always enable all SIP accounts on upgrade to version 6
1802 if (accountHandle.getComponentName().equals(sipComponentName)) {
1803 enabled = true;
1804 }
1805 }
Santos Cordona9eebe42015-06-11 14:07:44 -07001806 if (version < 7) {
1807 // Always enabled all PSTN acocunts on upgrade to version 7
1808 if (accountHandle.getComponentName().equals(pstnComponentName)) {
1809 enabled = true;
1810 }
1811 }
Roshan Pius6f752c82015-08-10 10:42:06 -07001812 if (version < 8) {
1813 // Migrate the SIP account handle ids to use SIP username instead of SIP URI.
1814 if (accountHandle.getComponentName().equals(sipComponentName)) {
1815 Uri accountUri = Uri.parse(accountHandle.getId());
1816 if (accountUri.getScheme() != null &&
1817 accountUri.getScheme().equals(PhoneAccount.SCHEME_SIP)) {
1818 accountHandle = new PhoneAccountHandle(accountHandle.getComponentName(),
1819 accountUri.getSchemeSpecificPart(),
1820 accountHandle.getUserHandle());
1821 }
1822 }
1823 }
Santos Cordona82aed82015-05-26 10:43:56 -07001824
Christine Hallstromffe558c2016-11-30 16:05:13 -08001825 if (version < 9) {
1826 // Set supported audio routes to all by default
1827 supportedAudioRoutes = CallAudioState.ROUTE_ALL;
1828 }
1829
Ihab Awad7e2c7f32014-11-03 09:49:45 -08001830 PhoneAccount.Builder builder = PhoneAccount.builder(accountHandle, label)
Andrew Lee7129f1c2014-09-04 11:55:07 -07001831 .setAddress(address)
1832 .setSubscriptionAddress(subscriptionAddress)
1833 .setCapabilities(capabilities)
Christine Hallstromffe558c2016-11-30 16:05:13 -08001834 .setSupportedAudioRoutes(supportedAudioRoutes)
Andrew Lee7129f1c2014-09-04 11:55:07 -07001835 .setShortDescription(shortDescription)
1836 .setSupportedUriSchemes(supportedUriSchemes)
Santos Cordonea5cb932015-05-07 16:28:38 -07001837 .setHighlightColor(highlightColor)
Tyler Gunn369c8742015-10-07 15:39:28 -07001838 .setExtras(extras)
Santos Cordonea5cb932015-05-07 16:28:38 -07001839 .setIsEnabled(enabled);
Ihab Awad7e2c7f32014-11-03 09:49:45 -08001840
Santos Cordon9c30c282015-05-13 16:28:27 -07001841 if (icon != null) {
1842 builder.setIcon(icon);
1843 } else if (iconBitmap != null) {
1844 builder.setIcon(Icon.createWithBitmap(iconBitmap));
1845 } else if (!TextUtils.isEmpty(iconPackageName)) {
1846 builder.setIcon(Icon.createWithResource(iconPackageName, iconResId));
1847 // TODO: Need to set tint.
Ihab Awad7e2c7f32014-11-03 09:49:45 -08001848 }
1849
Ihab Awad0a4b95f2015-05-18 10:15:38 -07001850 return builder.build();
Ihab Awadb78b2762014-07-25 15:16:23 -07001851 }
1852 return null;
Ihab Awad104f8062014-07-17 11:29:35 -07001853 }
Tyler Gunn84253572014-09-02 14:50:05 -07001854
1855 /**
Santos Cordon9c30c282015-05-13 16:28:27 -07001856 * Determines if the SIP call settings specify to use SIP for all calls, including PSTN
1857 * calls.
Tyler Gunn84253572014-09-02 14:50:05 -07001858 *
1859 * @param context The context.
1860 * @return {@code True} if SIP should be used for all calls.
1861 */
1862 private boolean useSipForPstnCalls(Context context) {
1863 String option = Settings.System.getString(context.getContentResolver(),
1864 Settings.System.SIP_CALL_OPTIONS);
1865 option = (option != null) ? option : Settings.System.SIP_ADDRESS_ONLY;
1866 return option.equals(Settings.System.SIP_ALWAYS);
1867 }
Ihab Awad104f8062014-07-17 11:29:35 -07001868 };
1869
Ihab Awadb78b2762014-07-25 15:16:23 -07001870 @VisibleForTesting
1871 public static final XmlSerialization<PhoneAccountHandle> sPhoneAccountHandleXml =
1872 new XmlSerialization<PhoneAccountHandle>() {
1873 private static final String CLASS_PHONE_ACCOUNT_HANDLE = "phone_account_handle";
Ihab Awad104f8062014-07-17 11:29:35 -07001874 private static final String COMPONENT_NAME = "component_name";
1875 private static final String ID = "id";
Evan Charlton105d9772014-11-25 14:08:53 -08001876 private static final String USER_SERIAL_NUMBER = "user_serial_number";
Ihab Awad104f8062014-07-17 11:29:35 -07001877
1878 @Override
Evan Charlton105d9772014-11-25 14:08:53 -08001879 public void writeToXml(PhoneAccountHandle o, XmlSerializer serializer, Context context)
Ihab Awadb78b2762014-07-25 15:16:23 -07001880 throws IOException {
Ihab Awad26923222014-07-30 10:54:35 -07001881 if (o != null) {
1882 serializer.startTag(null, CLASS_PHONE_ACCOUNT_HANDLE);
Ihab Awadb78b2762014-07-25 15:16:23 -07001883
Ihab Awad26923222014-07-30 10:54:35 -07001884 if (o.getComponentName() != null) {
Ihab Awadd9f54382014-10-24 11:44:47 -07001885 writeTextIfNonNull(
Ihab Awad26923222014-07-30 10:54:35 -07001886 COMPONENT_NAME, o.getComponentName().flattenToString(), serializer);
1887 }
Ihab Awadb78b2762014-07-25 15:16:23 -07001888
Ihab Awadd9f54382014-10-24 11:44:47 -07001889 writeTextIfNonNull(ID, o.getId(), serializer);
Ihab Awadb78b2762014-07-25 15:16:23 -07001890
Evan Charlton105d9772014-11-25 14:08:53 -08001891 if (o.getUserHandle() != null && context != null) {
1892 UserManager userManager = UserManager.get(context);
1893 writeLong(USER_SERIAL_NUMBER,
1894 userManager.getSerialNumberForUser(o.getUserHandle()), serializer);
1895 }
1896
Ihab Awad26923222014-07-30 10:54:35 -07001897 serializer.endTag(null, CLASS_PHONE_ACCOUNT_HANDLE);
1898 }
Ihab Awad104f8062014-07-17 11:29:35 -07001899 }
1900
1901 @Override
Tyler Gunn84253572014-09-02 14:50:05 -07001902 public PhoneAccountHandle readFromXml(XmlPullParser parser, int version, Context context)
Ihab Awadb78b2762014-07-25 15:16:23 -07001903 throws IOException, XmlPullParserException {
1904 if (parser.getName().equals(CLASS_PHONE_ACCOUNT_HANDLE)) {
1905 String componentNameString = null;
1906 String idString = null;
Evan Charlton105d9772014-11-25 14:08:53 -08001907 String userSerialNumberString = null;
Ihab Awadb78b2762014-07-25 15:16:23 -07001908 int outerDepth = parser.getDepth();
Evan Charlton105d9772014-11-25 14:08:53 -08001909
1910 UserManager userManager = UserManager.get(context);
1911
Ihab Awadb78b2762014-07-25 15:16:23 -07001912 while (XmlUtils.nextElementWithin(parser, outerDepth)) {
1913 if (parser.getName().equals(COMPONENT_NAME)) {
1914 parser.next();
1915 componentNameString = parser.getText();
1916 } else if (parser.getName().equals(ID)) {
1917 parser.next();
1918 idString = parser.getText();
Evan Charlton105d9772014-11-25 14:08:53 -08001919 } else if (parser.getName().equals(USER_SERIAL_NUMBER)) {
1920 parser.next();
1921 userSerialNumberString = parser.getText();
Ihab Awadb78b2762014-07-25 15:16:23 -07001922 }
1923 }
Ihab Awad26923222014-07-30 10:54:35 -07001924 if (componentNameString != null) {
Evan Charlton105d9772014-11-25 14:08:53 -08001925 UserHandle userHandle = null;
1926 if (userSerialNumberString != null) {
1927 try {
1928 long serialNumber = Long.parseLong(userSerialNumberString);
1929 userHandle = userManager.getUserForSerialNumber(serialNumber);
1930 } catch (NumberFormatException e) {
1931 Log.e(this, e, "Could not parse UserHandle " + userSerialNumberString);
1932 }
1933 }
Ihab Awadb78b2762014-07-25 15:16:23 -07001934 return new PhoneAccountHandle(
1935 ComponentName.unflattenFromString(componentNameString),
Evan Charlton105d9772014-11-25 14:08:53 -08001936 idString,
1937 userHandle);
Ihab Awadb78b2762014-07-25 15:16:23 -07001938 }
1939 }
1940 return null;
Ihab Awad104f8062014-07-17 11:29:35 -07001941 }
1942 };
Srikanth Chintalad5d2dd92016-06-23 16:02:33 +05301943
1944 private String nullToEmpty(String str) {
1945 return str == null ? "" : str;
1946 }
Santos Cordon176ae282014-07-14 02:02:14 -07001947}