blob: 709c04454e499d10c30f724e79bbad0d1f01c35e [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;
Sailesh Nepalaecfa9b2015-06-16 14:55:27 -070032import android.os.PersistableBundle;
Evan Charlton105d9772014-11-25 14:08:53 -080033import android.os.Process;
34import android.os.UserHandle;
35import android.os.UserManager;
Tyler Gunn84253572014-09-02 14:50:05 -070036import android.provider.Settings;
Tyler Gunn7cc70b42014-09-12 22:17:27 -070037import android.telecom.ConnectionService;
Santos Cordon59c21a72015-06-11 10:11:21 -070038import android.telecom.DefaultDialerManager;
Tyler Gunn7cc70b42014-09-12 22:17:27 -070039import android.telecom.PhoneAccount;
40import android.telecom.PhoneAccountHandle;
Sailesh Nepalaecfa9b2015-06-16 14:55:27 -070041import android.telephony.CarrierConfigManager;
Nancy Chen140004a2014-10-15 15:48:38 -070042import android.telephony.PhoneNumberUtils;
Nancy Chen5a36b6e2014-10-23 17:42:42 -070043import android.telephony.SubscriptionManager;
Santos Cordon479b3022015-02-06 04:27:15 -080044import android.telephony.TelephonyManager;
Evan Charltonaf51ceb2014-07-30 11:56:36 -070045import android.text.TextUtils;
Ihab Awadb78b2762014-07-25 15:16:23 -070046import android.util.AtomicFile;
Ihab Awadd9f54382014-10-24 11:44:47 -070047import android.util.Base64;
Ihab Awadb78b2762014-07-25 15:16:23 -070048import android.util.Xml;
Santos Cordon176ae282014-07-14 02:02:14 -070049
Tyler Gunn91d43cf2014-09-17 12:19:39 -070050// TODO: Needed for move to system service: import com.android.internal.R;
Sailesh Nepal0e1dc5a2014-07-30 11:08:54 -070051import com.android.internal.annotations.VisibleForTesting;
52import com.android.internal.util.FastXmlSerializer;
Tyler Gunn9787e0e2014-10-14 14:36:12 -070053import com.android.internal.util.IndentingPrintWriter;
Sailesh Nepal0e1dc5a2014-07-30 11:08:54 -070054import com.android.internal.util.XmlUtils;
55
Evan Charltonaf51ceb2014-07-30 11:56:36 -070056import org.xmlpull.v1.XmlPullParser;
57import org.xmlpull.v1.XmlPullParserException;
58import org.xmlpull.v1.XmlSerializer;
59
Ihab Awadb78b2762014-07-25 15:16:23 -070060import java.io.BufferedInputStream;
61import java.io.BufferedOutputStream;
Santos Cordon9c30c282015-05-13 16:28:27 -070062import java.io.ByteArrayInputStream;
Ihab Awadd9f54382014-10-24 11:44:47 -070063import java.io.ByteArrayOutputStream;
Ihab Awadb78b2762014-07-25 15:16:23 -070064import java.io.File;
65import java.io.FileNotFoundException;
66import java.io.FileOutputStream;
67import java.io.IOException;
68import java.io.InputStream;
Tyler Gunn84253572014-09-02 14:50:05 -070069import java.lang.Integer;
Tyler Gunncb59b672014-08-20 09:02:11 -070070import java.lang.SecurityException;
Tyler Gunnd900ce62014-08-13 11:40:59 -070071import java.lang.String;
Santos Cordon176ae282014-07-14 02:02:14 -070072import java.util.ArrayList;
Sailesh Nepal91fc8092015-02-14 15:44:55 -080073import java.util.Collections;
Tyler Gunnd900ce62014-08-13 11:40:59 -070074import java.util.Iterator;
Santos Cordon176ae282014-07-14 02:02:14 -070075import java.util.List;
Tony Mak4a3e2fd2015-12-04 11:58:38 +000076import java.util.Map;
Santos Cordon176ae282014-07-14 02:02:14 -070077import java.util.Objects;
Tony Mak4a3e2fd2015-12-04 11:58:38 +000078import java.util.concurrent.ConcurrentHashMap;
Ihab Awadb78b2762014-07-25 15:16:23 -070079import java.util.concurrent.CopyOnWriteArrayList;
Santos Cordon176ae282014-07-14 02:02:14 -070080
81/**
Evan Charlton89176372014-07-19 18:23:09 -070082 * Handles writing and reading PhoneAccountHandle registration entries. This is a simple verbatim
Santos Cordon6a212642015-05-08 16:35:23 -070083 * delegate for all the account handling methods on {@link android.telecom.TelecomManager} as
84 * implemented in {@link TelecomServiceImpl}, with the notable exception that
85 * {@link TelecomServiceImpl} is responsible for security checking to make sure that the caller has
86 * proper authority over the {@code ComponentName}s they are declaring in their
87 * {@code PhoneAccountHandle}s.
88 *
89 *
90 * -- About Users and Phone Accounts --
91 *
Santos Cordon9c30c282015-05-13 16:28:27 -070092 * We store all phone accounts for all users in a single place, which means that there are three
93 * users that we have to deal with in code:
Santos Cordon6a212642015-05-08 16:35:23 -070094 * 1) The Android User that is currently active on the device.
95 * 2) The user which owns/registers the phone account.
96 * 3) The user running the app that is requesting the phone account information.
97 *
98 * 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 +000099 * has a work profile running as another user (B2). Each user/profile only have the visibility of
100 * phone accounts owned by them. Lets say, user B (settings) is requesting a list of phone accounts,
101 * and the list only contains phone accounts owned by user B and accounts with
102 * {@link PhoneAccount#CAPABILITY_MULTI_USER}.
Santos Cordon6a212642015-05-08 16:35:23 -0700103 *
104 * In practice, (2) is stored with the phone account handle and is part of the handle's ID. (1) is
105 * saved in {@link #mCurrentUserHandle} and (3) we get from Binder.getCallingUser(). We check these
106 * users for visibility before returning any phone accounts.
Santos Cordon176ae282014-07-14 02:02:14 -0700107 */
Brad Ebinger53855132015-10-30 10:58:19 -0700108public class PhoneAccountRegistrar {
Santos Cordon176ae282014-07-14 02:02:14 -0700109
Yorke Lee5e8836a2014-08-22 15:25:18 -0700110 public static final PhoneAccountHandle NO_ACCOUNT_SELECTED =
111 new PhoneAccountHandle(new ComponentName("null", "null"), "NO_ACCOUNT_SELECTED");
112
Ihab Awadb78b2762014-07-25 15:16:23 -0700113 public abstract static class Listener {
114 public void onAccountsChanged(PhoneAccountRegistrar registrar) {}
115 public void onDefaultOutgoingChanged(PhoneAccountRegistrar registrar) {}
116 public void onSimCallManagerChanged(PhoneAccountRegistrar registrar) {}
117 }
118
119 private static final String FILE_NAME = "phone-account-registrar-state.xml";
Tyler Gunn84253572014-09-02 14:50:05 -0700120 @VisibleForTesting
Tony Mak4a3e2fd2015-12-04 11:58:38 +0000121 public static final int EXPECTED_STATE_VERSION = 9;
Tyler Gunn84253572014-09-02 14:50:05 -0700122
123 /** Keep in sync with the same in SipSettings.java */
124 private static final String SIP_SHARED_PREFERENCES = "SIP_PREFERENCES";
Ihab Awadb78b2762014-07-25 15:16:23 -0700125
126 private final List<Listener> mListeners = new CopyOnWriteArrayList<>();
127 private final AtomicFile mAtomicFile;
Santos Cordonafe59e52014-08-22 16:48:43 -0700128 private final Context mContext;
Evan Charlton105d9772014-11-25 14:08:53 -0800129 private final UserManager mUserManager;
Wink Saville33c05d32014-11-20 13:04:17 -0800130 private final SubscriptionManager mSubscriptionManager;
Ihab Awadb78b2762014-07-25 15:16:23 -0700131 private State mState;
Evan Charlton105d9772014-11-25 14:08:53 -0800132 private UserHandle mCurrentUserHandle;
Santos Cordon176ae282014-07-14 02:02:14 -0700133
Nancy Chen06ce0622014-10-23 01:17:35 +0000134 @VisibleForTesting
Ihab Awad26923222014-07-30 10:54:35 -0700135 public PhoneAccountRegistrar(Context context) {
Ihab Awadb78b2762014-07-25 15:16:23 -0700136 this(context, FILE_NAME);
137 }
138
139 @VisibleForTesting
140 public PhoneAccountRegistrar(Context context, String fileName) {
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700141 // TODO: This file path is subject to change -- it is storing the phone account registry
142 // state file in the path /data/system/users/0/, which is likely not correct in a
143 // multi-user setting.
144 /** UNCOMMENT_FOR_MOVE_TO_SYSTEM_SERVICE
145 String filePath = Environment.getUserSystemDirectory(UserHandle.myUserId()).
146 getAbsolutePath();
147 mAtomicFile = new AtomicFile(new File(filePath, fileName));
148 UNCOMMENT_FOR_MOVE_TO_SYSTEM_SERVICE */
Ihab Awadb78b2762014-07-25 15:16:23 -0700149 mAtomicFile = new AtomicFile(new File(context.getFilesDir(), fileName));
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700150
Ihab Awadb78b2762014-07-25 15:16:23 -0700151 mState = new State();
Santos Cordonafe59e52014-08-22 16:48:43 -0700152 mContext = context;
Evan Charlton105d9772014-11-25 14:08:53 -0800153 mUserManager = UserManager.get(context);
Wink Saville33c05d32014-11-20 13:04:17 -0800154 mSubscriptionManager = SubscriptionManager.from(mContext);
Evan Charlton105d9772014-11-25 14:08:53 -0800155 mCurrentUserHandle = Process.myUserHandle();
Ihab Awadb78b2762014-07-25 15:16:23 -0700156 read();
Santos Cordon176ae282014-07-14 02:02:14 -0700157 }
158
Tyler Gunn84253572014-09-02 14:50:05 -0700159 /**
Nancy Chen140004a2014-10-15 15:48:38 -0700160 * Retrieves the subscription id for a given phone account if it exists. Subscription ids
161 * apply only to PSTN/SIM card phone accounts so all other accounts should not have a
162 * subscription id.
163 * @param accountHandle The handle for the phone account for which to retrieve the
164 * subscription id.
Wink Saville35850602014-10-23 15:57:21 -0700165 * @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 -0700166 */
Wink Saville35850602014-10-23 15:57:21 -0700167 public int getSubscriptionIdForPhoneAccount(PhoneAccountHandle accountHandle) {
Tony Mak240656f2015-12-04 11:36:22 +0000168 PhoneAccount account = getPhoneAccountUnchecked(accountHandle);
Santos Cordon479b3022015-02-06 04:27:15 -0800169
Santos Cordon6a212642015-05-08 16:35:23 -0700170 if (account != null && account.hasCapabilities(PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION)) {
Santos Cordon479b3022015-02-06 04:27:15 -0800171 TelephonyManager tm =
172 (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE);
173 return tm.getSubIdForPhoneAccount(account);
Nancy Chen140004a2014-10-15 15:48:38 -0700174 }
Santos Cordon479b3022015-02-06 04:27:15 -0800175 return SubscriptionManager.INVALID_SUBSCRIPTION_ID;
Nancy Chen140004a2014-10-15 15:48:38 -0700176 }
177
178 /**
Evan Charlton105d9772014-11-25 14:08:53 -0800179 * Retrieves the default outgoing phone account supporting the specified uriScheme. Note that if
180 * {@link #mCurrentUserHandle} does not have visibility into the current default, {@code null}
181 * will be returned.
182 *
Tyler Gunn84253572014-09-02 14:50:05 -0700183 * @param uriScheme The URI scheme for the outgoing call.
184 * @return The {@link PhoneAccountHandle} to use.
185 */
Tony Mak240656f2015-12-04 11:36:22 +0000186 public PhoneAccountHandle getOutgoingPhoneAccountForScheme(String uriScheme,
187 UserHandle userHandle) {
188 final PhoneAccountHandle userSelected = getUserSelectedOutgoingPhoneAccount(userHandle);
Tyler Gunn84253572014-09-02 14:50:05 -0700189
Yorke Lee5e8836a2014-08-22 15:25:18 -0700190 if (userSelected != null) {
Tyler Gunn84253572014-09-02 14:50:05 -0700191 // If there is a default PhoneAccount, ensure it supports calls to handles with the
192 // specified uriScheme.
Tony Mak240656f2015-12-04 11:36:22 +0000193 final PhoneAccount userSelectedAccount = getPhoneAccountUnchecked(userSelected);
Santos Cordon6a212642015-05-08 16:35:23 -0700194 if (userSelectedAccount.supportsUriScheme(uriScheme)) {
Tyler Gunn84253572014-09-02 14:50:05 -0700195 return userSelected;
196 }
Ihab Awad293edf22014-07-24 17:52:29 -0700197 }
198
Tony Mak240656f2015-12-04 11:36:22 +0000199 List<PhoneAccountHandle> outgoing = getCallCapablePhoneAccounts(uriScheme, false,
200 userHandle);
Ihab Awad6fb37c82014-08-07 19:48:57 -0700201 switch (outgoing.size()) {
Ihab Awad293edf22014-07-24 17:52:29 -0700202 case 0:
203 // There are no accounts, so there can be no default
204 return null;
205 case 1:
Evan Charlton105d9772014-11-25 14:08:53 -0800206 // There is only one account, which is by definition the default.
Santos Cordon6a212642015-05-08 16:35:23 -0700207 return outgoing.get(0);
Ihab Awad293edf22014-07-24 17:52:29 -0700208 default:
209 // There are multiple accounts with no selected default
210 return null;
Ihab Awadf2a84912014-07-22 21:09:25 -0700211 }
Ihab Awad104f8062014-07-17 11:29:35 -0700212 }
Santos Cordon176ae282014-07-14 02:02:14 -0700213
Tony Mak240656f2015-12-04 11:36:22 +0000214 public PhoneAccountHandle getOutgoingPhoneAccountForSchemeOfCurrentUser(String uriScheme) {
215 return getOutgoingPhoneAccountForScheme(uriScheme, mCurrentUserHandle);
216 }
217
Evan Charlton105d9772014-11-25 14:08:53 -0800218 /**
219 * @return The user-selected outgoing {@link PhoneAccount}, or null if it hasn't been set (or
220 * if it was set by another user).
221 */
Tony Mak240656f2015-12-04 11:36:22 +0000222 PhoneAccountHandle getUserSelectedOutgoingPhoneAccount(UserHandle userHandle) {
Tony Mak4a3e2fd2015-12-04 11:58:38 +0000223 if (userHandle == null) {
224 return null;
225 }
226 DefaultPhoneAccountHandle defaultPhoneAccountHandle = mState.defaultOutgoingAccountHandles
227 .get(userHandle);
228 if (defaultPhoneAccountHandle == null) {
229 return null;
230 }
231 // Make sure the account is still registered and owned by the user.
232 PhoneAccount account = getPhoneAccount(defaultPhoneAccountHandle.phoneAccountHandle,
233 userHandle);
Santos Cordon6a212642015-05-08 16:35:23 -0700234 if (account != null) {
Tony Mak4a3e2fd2015-12-04 11:58:38 +0000235 return defaultPhoneAccountHandle.phoneAccountHandle;
Yorke Lee5e8836a2014-08-22 15:25:18 -0700236 }
237 return null;
238 }
239
Santos Cordon6a212642015-05-08 16:35:23 -0700240 /**
241 * Sets the phone account with which to place all calls by default. Set by the user
242 * within phone settings.
243 */
Tony Mak4a3e2fd2015-12-04 11:58:38 +0000244 public void setUserSelectedOutgoingPhoneAccount(PhoneAccountHandle accountHandle,
245 UserHandle userHandle) {
246 if (userHandle == null) {
247 return;
248 }
Evan Charlton89176372014-07-19 18:23:09 -0700249 if (accountHandle == null) {
Ihab Awad104f8062014-07-17 11:29:35 -0700250 // Asking to clear the default outgoing is a valid request
Tony Mak4a3e2fd2015-12-04 11:58:38 +0000251 mState.defaultOutgoingAccountHandles.remove(userHandle);
Ihab Awad104f8062014-07-17 11:29:35 -0700252 } else {
Tony Mak4a3e2fd2015-12-04 11:58:38 +0000253 PhoneAccount account = getPhoneAccount(accountHandle, userHandle);
Santos Cordon6a212642015-05-08 16:35:23 -0700254 if (account == null) {
Ihab Awadb78b2762014-07-25 15:16:23 -0700255 Log.w(this, "Trying to set nonexistent default outgoing %s",
256 accountHandle);
257 return;
258 }
259
Santos Cordon6a212642015-05-08 16:35:23 -0700260 if (!account.hasCapabilities(PhoneAccount.CAPABILITY_CALL_PROVIDER)) {
Ihab Awadb78b2762014-07-25 15:16:23 -0700261 Log.w(this, "Trying to set non-call-provider default outgoing %s",
Evan Charlton89176372014-07-19 18:23:09 -0700262 accountHandle);
Ihab Awad104f8062014-07-17 11:29:35 -0700263 return;
264 }
265
Santos Cordon6a212642015-05-08 16:35:23 -0700266 if (account.hasCapabilities(PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION)) {
Nancy Chen5a36b6e2014-10-23 17:42:42 -0700267 // If the account selected is a SIM account, propagate down to the subscription
268 // record.
Wink Saville7ce6e782014-10-27 10:56:46 -0700269 int subId = getSubscriptionIdForPhoneAccount(accountHandle);
Wink Saville33c05d32014-11-20 13:04:17 -0800270 mSubscriptionManager.setDefaultVoiceSubId(subId);
Nancy Chen5a36b6e2014-10-23 17:42:42 -0700271 }
272
Tony Mak4a3e2fd2015-12-04 11:58:38 +0000273 mState.defaultOutgoingAccountHandles
274 .put(userHandle, new DefaultPhoneAccountHandle(userHandle, accountHandle));
Santos Cordon176ae282014-07-14 02:02:14 -0700275 }
276
Ihab Awad293edf22014-07-24 17:52:29 -0700277 write();
Ihab Awadb78b2762014-07-25 15:16:23 -0700278 fireDefaultOutgoingChanged();
Santos Cordon176ae282014-07-14 02:02:14 -0700279 }
280
Nancy Chen668dee02014-11-19 15:31:31 -0800281 boolean isUserSelectedSmsPhoneAccount(PhoneAccountHandle accountHandle) {
Tony Mak240656f2015-12-04 11:36:22 +0000282 return getSubscriptionIdForPhoneAccount(accountHandle) == SubscriptionManager
283 .getDefaultSmsSubId();
Sailesh Nepalfc43ea82015-07-28 19:30:28 -0700284 }
285
Sailesh Nepal8ed7eeb2015-09-08 12:30:13 -0700286 public ComponentName getSystemSimCallManagerComponent() {
287 String defaultSimCallManager = null;
288 CarrierConfigManager configManager = (CarrierConfigManager) mContext.getSystemService(
289 Context.CARRIER_CONFIG_SERVICE);
290 PersistableBundle configBundle = configManager.getConfig();
291 if (configBundle != null) {
292 defaultSimCallManager = configBundle.getString(
293 CarrierConfigManager.KEY_DEFAULT_SIM_CALL_MANAGER_STRING);
294 }
295 return TextUtils.isEmpty(defaultSimCallManager)
296 ? null : ComponentName.unflattenFromString(defaultSimCallManager);
297 }
298
Tony Mak240656f2015-12-04 11:36:22 +0000299 public PhoneAccountHandle getSimCallManagerOfCurrentUser() {
300 return getSimCallManager(mCurrentUserHandle);
301 }
302
Sailesh Nepalfc43ea82015-07-28 19:30:28 -0700303 /**
304 * Returns the {@link PhoneAccountHandle} corresponding to the currently active SIM Call
305 * Manager. SIM Call Manager returned corresponds to the following priority order:
306 * 1. If a SIM Call Manager {@link PhoneAccount} is registered for the same package as the
307 * default dialer, then that one is returned.
308 * 2. If there is a SIM Call Manager {@link PhoneAccount} registered which matches the
309 * carrier configuration's default, then that one is returned.
310 * 3. Otherwise, we return null.
311 */
Tony Mak240656f2015-12-04 11:36:22 +0000312 public PhoneAccountHandle getSimCallManager(UserHandle userHandle) {
Santos Cordon59c21a72015-06-11 10:11:21 -0700313 // Get the default dialer in case it has a connection manager associated with it.
Tony Mak240656f2015-12-04 11:36:22 +0000314 String dialerPackage = DefaultDialerManager
315 .getDefaultDialerApplication(mContext, userHandle.getIdentifier());
Evan Charltonaf51ceb2014-07-30 11:56:36 -0700316
Sailesh Nepalaecfa9b2015-06-16 14:55:27 -0700317 // Check carrier config.
Sailesh Nepal8ed7eeb2015-09-08 12:30:13 -0700318 ComponentName systemSimCallManagerComponent = getSystemSimCallManagerComponent();
Sailesh Nepal91fc8092015-02-14 15:44:55 -0800319
Santos Cordon59c21a72015-06-11 10:11:21 -0700320 PhoneAccountHandle dialerSimCallManager = null;
321 PhoneAccountHandle systemSimCallManager = null;
Sailesh Nepal91fc8092015-02-14 15:44:55 -0800322
Santos Cordon59c21a72015-06-11 10:11:21 -0700323 if (!TextUtils.isEmpty(dialerPackage) || systemSimCallManagerComponent != null) {
324 // loop through and look for any connection manager in the same package.
325 List<PhoneAccountHandle> allSimCallManagers = getPhoneAccountHandles(
326 PhoneAccount.CAPABILITY_CONNECTION_MANAGER, null, null,
Tony Mak240656f2015-12-04 11:36:22 +0000327 true /* includeDisabledAccounts */, userHandle);
Santos Cordon59c21a72015-06-11 10:11:21 -0700328 for (PhoneAccountHandle accountHandle : allSimCallManagers) {
329 ComponentName component = accountHandle.getComponentName();
330
331 // Store the system connection manager if found
332 if (systemSimCallManager == null
333 && Objects.equals(component, systemSimCallManagerComponent)
334 && !resolveComponent(accountHandle).isEmpty()) {
335 systemSimCallManager = accountHandle;
336
337 // Store the dialer connection manager if found
338 } else if (dialerSimCallManager == null
339 && Objects.equals(component.getPackageName(), dialerPackage)
340 && !resolveComponent(accountHandle).isEmpty()) {
341 dialerSimCallManager = accountHandle;
Evan Charltonaf51ceb2014-07-30 11:56:36 -0700342 }
Evan Charltonaf51ceb2014-07-30 11:56:36 -0700343 }
Evan Charltonaf51ceb2014-07-30 11:56:36 -0700344 }
345
Santos Cordon59c21a72015-06-11 10:11:21 -0700346 PhoneAccountHandle retval = dialerSimCallManager != null ?
347 dialerSimCallManager : systemSimCallManager;
348
349 Log.i(this, "SimCallManager queried, returning: %s", retval);
350
Sailesh Nepalb3ccd7e2015-06-26 18:16:18 -0700351 return retval;
Ihab Awad293edf22014-07-24 17:52:29 -0700352 }
353
Evan Charlton105d9772014-11-25 14:08:53 -0800354 /**
Tony Mak240656f2015-12-04 11:36:22 +0000355 * If it is a outgoing call, sim call manager of call-initiating user is returned.
356 * Otherwise, we return the sim call manager of the user associated with the
357 * target phone account.
358 * @return phone account handle of sim call manager based on the ongoing call.
359 */
360 public PhoneAccountHandle getSimCallManagerFromCall(Call call) {
361 if (call == null) {
362 return null;
363 }
364 UserHandle userHandle = call.getInitiatingUser();
365 if (userHandle == null) {
366 userHandle = call.getTargetPhoneAccount().getUserHandle();
367 }
368 return getSimCallManager(userHandle);
369 }
370
371 /**
Evan Charlton105d9772014-11-25 14:08:53 -0800372 * Update the current UserHandle to track when users are switched. This will allow the
373 * PhoneAccountRegistar to self-filter the PhoneAccounts to make sure we don't leak anything
374 * across users.
Santos Cordon6a212642015-05-08 16:35:23 -0700375 * We cannot simply check the calling user because that would always return the primary user for
376 * all invocations originating with the system process.
Evan Charlton105d9772014-11-25 14:08:53 -0800377 *
378 * @param userHandle The {@link UserHandle}, as delivered by
379 * {@link Intent#ACTION_USER_SWITCHED}.
380 */
381 public void setCurrentUserHandle(UserHandle userHandle) {
382 if (userHandle == null) {
383 Log.d(this, "setCurrentUserHandle, userHandle = null");
384 userHandle = Process.myUserHandle();
385 }
386 Log.d(this, "setCurrentUserHandle, %s", userHandle);
387 mCurrentUserHandle = userHandle;
388 }
389
Yorke Lee71734c22015-06-02 14:22:56 -0700390 /**
391 * @return {@code true} if the phone account was successfully enabled/disabled, {@code false}
392 * otherwise.
393 */
394 public boolean enablePhoneAccount(PhoneAccountHandle accountHandle, boolean isEnabled) {
Tony Mak98e6bdc2015-11-25 21:54:05 +0000395 PhoneAccount account = getPhoneAccountUnchecked(accountHandle);
Santos Cordon638f05c2015-05-19 17:12:33 -0700396 if (account == null) {
397 Log.w(this, "Could not find account to enable: " + accountHandle);
Yorke Lee71734c22015-06-02 14:22:56 -0700398 return false;
Santos Cordon638f05c2015-05-19 17:12:33 -0700399 } else if (account.hasCapabilities(PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION)) {
Santos Cordonea5cb932015-05-07 16:28:38 -0700400 // We never change the enabled state of SIM-based accounts.
Santos Cordon638f05c2015-05-19 17:12:33 -0700401 Log.w(this, "Could not change enable state of SIM account: " + accountHandle);
Yorke Lee71734c22015-06-02 14:22:56 -0700402 return false;
Santos Cordonea5cb932015-05-07 16:28:38 -0700403 }
404
Santos Cordon638f05c2015-05-19 17:12:33 -0700405 if (account.isEnabled() != isEnabled) {
Santos Cordonea5cb932015-05-07 16:28:38 -0700406 account.setIsEnabled(isEnabled);
407 write();
408 fireAccountsChanged();
409 }
Yorke Lee71734c22015-06-02 14:22:56 -0700410 return true;
Santos Cordonea5cb932015-05-07 16:28:38 -0700411 }
412
Tony Mak240656f2015-12-04 11:36:22 +0000413 private boolean isVisibleForUser(PhoneAccount account, UserHandle userHandle,
414 boolean acrossProfiles) {
Evan Charlton105d9772014-11-25 14:08:53 -0800415 if (account == null) {
416 return false;
417 }
418
Tony Mak240656f2015-12-04 11:36:22 +0000419 if (userHandle == null) {
420 Log.w(this, "userHandle is null in isVisibleForUser");
421 return false;
422 }
423
Evan Charlton105d9772014-11-25 14:08:53 -0800424 // If this PhoneAccount has CAPABILITY_MULTI_USER, it should be visible to all users and
425 // all profiles. Only Telephony and SIP accounts should have this capability.
426 if (account.hasCapabilities(PhoneAccount.CAPABILITY_MULTI_USER)) {
427 return true;
428 }
429
430 UserHandle phoneAccountUserHandle = account.getAccountHandle().getUserHandle();
431 if (phoneAccountUserHandle == null) {
432 return false;
433 }
434
435 if (mCurrentUserHandle == null) {
Tony Mak240656f2015-12-04 11:36:22 +0000436 // In case we need to have emergency phone calls from the lock screen.
Evan Charlton105d9772014-11-25 14:08:53 -0800437 Log.d(this, "Current user is null; assuming true");
438 return true;
439 }
440
Tony Mak240656f2015-12-04 11:36:22 +0000441 if (acrossProfiles) {
442 return UserManager.get(mContext).isSameProfileGroup(userHandle.getIdentifier(),
443 phoneAccountUserHandle.getIdentifier());
444 } else {
445 return phoneAccountUserHandle.equals(userHandle);
Sailesh Nepal91fc8092015-02-14 15:44:55 -0800446 }
Evan Charlton105d9772014-11-25 14:08:53 -0800447 }
448
449 private List<ResolveInfo> resolveComponent(PhoneAccountHandle phoneAccountHandle) {
450 return resolveComponent(phoneAccountHandle.getComponentName(),
Tony Mak240656f2015-12-04 11:36:22 +0000451 phoneAccountHandle.getUserHandle());
Evan Charlton105d9772014-11-25 14:08:53 -0800452 }
453
454 private List<ResolveInfo> resolveComponent(ComponentName componentName,
455 UserHandle userHandle) {
mike dooley10a58312014-11-06 13:46:19 -0800456 PackageManager pm = mContext.getPackageManager();
457 Intent intent = new Intent(ConnectionService.SERVICE_INTERFACE);
458 intent.setComponent(componentName);
Sailesh Nepal91fc8092015-02-14 15:44:55 -0800459 try {
460 if (userHandle != null) {
461 return pm.queryIntentServicesAsUser(intent, 0, userHandle.getIdentifier());
462 } else {
463 return pm.queryIntentServices(intent, 0);
464 }
465 } catch (SecurityException e) {
Santos Cordon3188b362015-05-22 13:01:10 -0700466 Log.e(this, e, "%s is not visible for the calling user", componentName);
Sailesh Nepal91fc8092015-02-14 15:44:55 -0800467 return Collections.EMPTY_LIST;
Evan Charlton105d9772014-11-25 14:08:53 -0800468 }
mike dooley10a58312014-11-06 13:46:19 -0800469 }
470
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700471 /**
472 * Retrieves a list of all {@link PhoneAccountHandle}s registered.
Santos Cordonea5cb932015-05-07 16:28:38 -0700473 * Only returns accounts which are enabled.
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700474 *
475 * @return The list of {@link PhoneAccountHandle}s.
476 */
Tony Mak240656f2015-12-04 11:36:22 +0000477 public List<PhoneAccountHandle> getAllPhoneAccountHandles(UserHandle userHandle) {
478 return getPhoneAccountHandles(0, null, null, false, userHandle);
Ihab Awad293edf22014-07-24 17:52:29 -0700479 }
480
Tony Mak240656f2015-12-04 11:36:22 +0000481 public List<PhoneAccount> getAllPhoneAccounts(UserHandle userHandle) {
482 return getPhoneAccounts(0, null, null, false, userHandle);
483 }
484
485 public List<PhoneAccount> getAllPhoneAccountsOfCurrentUser() {
486 return getAllPhoneAccounts(mCurrentUserHandle);
Santos Cordonafe59e52014-08-22 16:48:43 -0700487 }
488
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700489 /**
Nancy Chen309198e2014-09-15 18:02:49 -0700490 * Retrieves a list of all phone account call provider phone accounts supporting the
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700491 * specified URI scheme.
492 *
493 * @param uriScheme The URI scheme.
494 * @return The phone account handles.
495 */
Santos Cordonea5cb932015-05-07 16:28:38 -0700496 public List<PhoneAccountHandle> getCallCapablePhoneAccounts(
Tony Mak240656f2015-12-04 11:36:22 +0000497 String uriScheme, boolean includeDisabledAccounts, UserHandle userHandle) {
Santos Cordonea5cb932015-05-07 16:28:38 -0700498 return getPhoneAccountHandles(
Bryce Leea0bb7052015-10-16 13:31:40 -0700499 PhoneAccount.CAPABILITY_CALL_PROVIDER,
500 PhoneAccount.CAPABILITY_EMERGENCY_CALLS_ONLY /*excludedCapabilities*/,
Tony Mak240656f2015-12-04 11:36:22 +0000501 uriScheme, null, includeDisabledAccounts, userHandle);
502 }
503
504 public List<PhoneAccountHandle> getCallCapablePhoneAccountsOfCurrentUser(
505 String uriScheme, boolean includeDisabledAccounts) {
506 return getCallCapablePhoneAccounts(uriScheme, includeDisabledAccounts, mCurrentUserHandle);
Santos Cordon6a212642015-05-08 16:35:23 -0700507 }
508
509 /**
510 * Retrieves a list of all the SIM-based phone accounts.
511 */
Tony Mak240656f2015-12-04 11:36:22 +0000512 public List<PhoneAccountHandle> getSimPhoneAccounts(UserHandle userHandle) {
Santos Cordon6a212642015-05-08 16:35:23 -0700513 return getPhoneAccountHandles(
514 PhoneAccount.CAPABILITY_CALL_PROVIDER | PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION,
Tony Mak240656f2015-12-04 11:36:22 +0000515 null, null, false, userHandle);
Tyler Gunn84253572014-09-02 14:50:05 -0700516 }
517
Tony Mak240656f2015-12-04 11:36:22 +0000518 public List<PhoneAccountHandle> getSimPhoneAccountsOfCurrentUser() {
519 return getSimPhoneAccounts(mCurrentUserHandle);
520 }
521
522 /**
523 * Retrieves a list of all phone accounts registered by a specified package.
524 *
525 * @param packageName The name of the package that registered the phone accounts.
526 * @return The phone account handles.
527 */
528 public List<PhoneAccountHandle> getPhoneAccountsForPackage(String packageName,
529 UserHandle userHandle) {
530 return getPhoneAccountHandles(0, null, packageName, false, userHandle);
Nancy Chen1c5926f2014-09-17 14:44:14 -0700531 }
532
Ihab Awad104f8062014-07-17 11:29:35 -0700533 // TODO: Should we implement an artificial limit for # of accounts associated with a single
534 // ComponentName?
Ihab Awad293edf22014-07-24 17:52:29 -0700535 public void registerPhoneAccount(PhoneAccount account) {
Tyler Gunncb59b672014-08-20 09:02:11 -0700536 // Enforce the requirement that a connection service for a phone account has the correct
537 // permission.
Santos Cordon6a212642015-05-08 16:35:23 -0700538 if (!phoneAccountRequiresBindPermission(account.getAccountHandle())) {
Yorke Lee7bb8ce92015-05-13 16:28:29 -0700539 Log.w(this,
540 "Phone account %s does not have BIND_TELECOM_CONNECTION_SERVICE permission.",
Tyler Gunncb59b672014-08-20 09:02:11 -0700541 account.getAccountHandle());
Yorke Lee7bb8ce92015-05-13 16:28:29 -0700542 throw new SecurityException("PhoneAccount connection service requires "
543 + "BIND_TELECOM_CONNECTION_SERVICE permission.");
Tyler Gunncb59b672014-08-20 09:02:11 -0700544 }
545
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700546 addOrReplacePhoneAccount(account);
547 }
548
549 /**
550 * Adds a {@code PhoneAccount}, replacing an existing one if found.
551 *
552 * @param account The {@code PhoneAccount} to add or replace.
553 */
554 private void addOrReplacePhoneAccount(PhoneAccount account) {
Ihab Awad7e2c7f32014-11-03 09:49:45 -0800555 Log.d(this, "addOrReplacePhoneAccount(%s -> %s)",
556 account.getAccountHandle(), account);
557
Santos Cordonea5cb932015-05-07 16:28:38 -0700558 // Start _enabled_ property as false.
559 // !!! IMPORTANT !!! It is important that we do not read the enabled state that the
560 // source app provides or else an third party app could enable itself.
561 boolean isEnabled = false;
562
Tony Mak98e6bdc2015-11-25 21:54:05 +0000563 PhoneAccount oldAccount = getPhoneAccountUnchecked(account.getAccountHandle());
Santos Cordon6a212642015-05-08 16:35:23 -0700564 if (oldAccount != null) {
565 mState.accounts.remove(oldAccount);
Santos Cordonea5cb932015-05-07 16:28:38 -0700566 isEnabled = oldAccount.isEnabled();
Santos Cordon5fa4e4f2015-06-10 14:56:01 -0700567 Log.i(this, getAccountDiffString(account, oldAccount));
568 } else {
569 Log.i(this, "New phone account registered: " + account);
Santos Cordon176ae282014-07-14 02:02:14 -0700570 }
Santos Cordonea5cb932015-05-07 16:28:38 -0700571
Santos Cordon6a212642015-05-08 16:35:23 -0700572 mState.accounts.add(account);
Santos Cordonea5cb932015-05-07 16:28:38 -0700573 // Reset enabled state to whatever the value was if the account was already registered,
574 // or _true_ if this is a SIM-based account. All SIM-based accounts are always enabled.
575 account.setIsEnabled(
576 isEnabled || account.hasCapabilities(PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION));
Santos Cordon176ae282014-07-14 02:02:14 -0700577
Ihab Awad293edf22014-07-24 17:52:29 -0700578 write();
Ihab Awadb78b2762014-07-25 15:16:23 -0700579 fireAccountsChanged();
Ihab Awad293edf22014-07-24 17:52:29 -0700580 }
581
Evan Charlton89176372014-07-19 18:23:09 -0700582 public void unregisterPhoneAccount(PhoneAccountHandle accountHandle) {
Tony Mak98e6bdc2015-11-25 21:54:05 +0000583 PhoneAccount account = getPhoneAccountUnchecked(accountHandle);
Santos Cordon6a212642015-05-08 16:35:23 -0700584 if (account != null) {
585 if (mState.accounts.remove(account)) {
586 write();
587 fireAccountsChanged();
Ihab Awad104f8062014-07-17 11:29:35 -0700588 }
589 }
Santos Cordon176ae282014-07-14 02:02:14 -0700590 }
591
Tyler Gunnd900ce62014-08-13 11:40:59 -0700592 /**
593 * Un-registers all phone accounts associated with a specified package.
594 *
595 * @param packageName The package for which phone accounts will be removed.
Evan Charlton105d9772014-11-25 14:08:53 -0800596 * @param userHandle The {@link UserHandle} the package is running under.
Tyler Gunnd900ce62014-08-13 11:40:59 -0700597 */
Evan Charlton105d9772014-11-25 14:08:53 -0800598 public void clearAccounts(String packageName, UserHandle userHandle) {
Tyler Gunnd900ce62014-08-13 11:40:59 -0700599 boolean accountsRemoved = false;
600 Iterator<PhoneAccount> it = mState.accounts.iterator();
601 while (it.hasNext()) {
602 PhoneAccount phoneAccount = it.next();
Evan Charlton105d9772014-11-25 14:08:53 -0800603 PhoneAccountHandle handle = phoneAccount.getAccountHandle();
604 if (Objects.equals(packageName, handle.getComponentName().getPackageName())
605 && Objects.equals(userHandle, handle.getUserHandle())) {
Tyler Gunnd900ce62014-08-13 11:40:59 -0700606 Log.i(this, "Removing phone account " + phoneAccount.getLabel());
Shriram Ganesh8f89f8d2015-04-15 12:35:43 +0530607 mState.accounts.remove(phoneAccount);
Tyler Gunnd900ce62014-08-13 11:40:59 -0700608 accountsRemoved = true;
Ihab Awad104f8062014-07-17 11:29:35 -0700609 }
610 }
611
Tyler Gunnd900ce62014-08-13 11:40:59 -0700612 if (accountsRemoved) {
613 write();
614 fireAccountsChanged();
615 }
Ihab Awadb78b2762014-07-25 15:16:23 -0700616 }
617
Nancy Chen140004a2014-10-15 15:48:38 -0700618 public boolean isVoiceMailNumber(PhoneAccountHandle accountHandle, String number) {
Wink Saville35850602014-10-23 15:57:21 -0700619 int subId = getSubscriptionIdForPhoneAccount(accountHandle);
Yorke Leeb1957232015-06-30 10:09:54 -0700620 return PhoneNumberUtils.isVoiceMailNumber(mContext, subId, number);
Nancy Chen140004a2014-10-15 15:48:38 -0700621 }
622
Ihab Awadb78b2762014-07-25 15:16:23 -0700623 public void addListener(Listener l) {
624 mListeners.add(l);
625 }
626
627 public void removeListener(Listener l) {
Jay Shraunera82c8f72014-08-14 15:49:16 -0700628 if (l != null) {
629 mListeners.remove(l);
630 }
Ihab Awadb78b2762014-07-25 15:16:23 -0700631 }
632
633 private void fireAccountsChanged() {
634 for (Listener l : mListeners) {
635 l.onAccountsChanged(this);
636 }
637 }
638
639 private void fireDefaultOutgoingChanged() {
640 for (Listener l : mListeners) {
641 l.onDefaultOutgoingChanged(this);
642 }
643 }
644
645 private void fireSimCallManagerChanged() {
646 for (Listener l : mListeners) {
647 l.onSimCallManagerChanged(this);
648 }
Santos Cordon176ae282014-07-14 02:02:14 -0700649 }
650
Santos Cordon5fa4e4f2015-06-10 14:56:01 -0700651 private String getAccountDiffString(PhoneAccount account1, PhoneAccount account2) {
652 if (account1 == null || account2 == null) {
653 return "Diff: " + account1 + ", " + account2;
654 }
655
656 StringBuffer sb = new StringBuffer();
657 sb.append("[").append(account1.getAccountHandle());
Anthony Lee7c64a4e2015-08-25 09:27:47 -0700658 appendDiff(sb, "addr", Log.piiHandle(account1.getAddress()),
659 Log.piiHandle(account2.getAddress()));
Santos Cordon5fa4e4f2015-06-10 14:56:01 -0700660 appendDiff(sb, "cap", account1.getCapabilities(), account2.getCapabilities());
661 appendDiff(sb, "hl", account1.getHighlightColor(), account2.getHighlightColor());
662 appendDiff(sb, "icon", account1.getIcon(), account2.getIcon());
663 appendDiff(sb, "lbl", account1.getLabel(), account2.getLabel());
664 appendDiff(sb, "desc", account1.getShortDescription(), account2.getShortDescription());
Anthony Lee7c64a4e2015-08-25 09:27:47 -0700665 appendDiff(sb, "subAddr", Log.piiHandle(account1.getSubscriptionAddress()),
666 Log.piiHandle(account2.getSubscriptionAddress()));
Santos Cordon5fa4e4f2015-06-10 14:56:01 -0700667 appendDiff(sb, "uris", account1.getSupportedUriSchemes(),
668 account2.getSupportedUriSchemes());
669 sb.append("]");
670 return sb.toString();
671 }
672
673 private void appendDiff(StringBuffer sb, String attrName, Object obj1, Object obj2) {
674 if (!Objects.equals(obj1, obj2)) {
675 sb.append("(")
676 .append(attrName)
677 .append(": ")
678 .append(obj1)
679 .append(" -> ")
680 .append(obj2)
681 .append(")");
682 }
683 }
684
Tyler Gunncb59b672014-08-20 09:02:11 -0700685 /**
Santos Cordon6a212642015-05-08 16:35:23 -0700686 * Determines if the connection service specified by a {@link PhoneAccountHandle} requires the
Yorke Lee7bb8ce92015-05-13 16:28:29 -0700687 * {@link Manifest.permission#BIND_TELECOM_CONNECTION_SERVICE} permission.
Tyler Gunncb59b672014-08-20 09:02:11 -0700688 *
689 * @param phoneAccountHandle The phone account to check.
690 * @return {@code True} if the phone account has permission.
691 */
Santos Cordon6a212642015-05-08 16:35:23 -0700692 public boolean phoneAccountRequiresBindPermission(PhoneAccountHandle phoneAccountHandle) {
Sailesh Nepal91fc8092015-02-14 15:44:55 -0800693 List<ResolveInfo> resolveInfos = resolveComponent(phoneAccountHandle);
694 if (resolveInfos.isEmpty()) {
695 Log.w(this, "phoneAccount %s not found", phoneAccountHandle.getComponentName());
Tyler Gunncb59b672014-08-20 09:02:11 -0700696 return false;
697 }
Sailesh Nepal91fc8092015-02-14 15:44:55 -0800698 for (ResolveInfo resolveInfo : resolveInfos) {
699 ServiceInfo serviceInfo = resolveInfo.serviceInfo;
Yorke Lee7bb8ce92015-05-13 16:28:29 -0700700 if (serviceInfo == null) {
701 return false;
702 }
703
704 if (!Manifest.permission.BIND_CONNECTION_SERVICE.equals(serviceInfo.permission) &&
705 !Manifest.permission.BIND_TELECOM_CONNECTION_SERVICE.equals(
706 serviceInfo.permission)) {
707 // The ConnectionService must require either the deprecated BIND_CONNECTION_SERVICE,
708 // or the public BIND_TELECOM_CONNECTION_SERVICE permissions, both of which are
709 // system/signature only.
Sailesh Nepal91fc8092015-02-14 15:44:55 -0800710 return false;
711 }
712 }
713 return true;
Tyler Gunncb59b672014-08-20 09:02:11 -0700714 }
715
Santos Cordon6a212642015-05-08 16:35:23 -0700716 //
717 // Methods for retrieving PhoneAccounts and PhoneAccountHandles
718 //
Ihab Awad293edf22014-07-24 17:52:29 -0700719
Santos Cordonafe59e52014-08-22 16:48:43 -0700720 /**
Santos Cordon6a212642015-05-08 16:35:23 -0700721 * Returns the PhoneAccount for the specified handle. Does no user checking.
Tyler Gunn84253572014-09-02 14:50:05 -0700722 *
Santos Cordon6a212642015-05-08 16:35:23 -0700723 * @param handle
724 * @return The corresponding phone account if one exists.
Santos Cordonafe59e52014-08-22 16:48:43 -0700725 */
Tony Mak98e6bdc2015-11-25 21:54:05 +0000726 public PhoneAccount getPhoneAccountUnchecked(PhoneAccountHandle handle) {
Santos Cordon6a212642015-05-08 16:35:23 -0700727 for (PhoneAccount m : mState.accounts) {
728 if (Objects.equals(handle, m.getAccountHandle())) {
729 return m;
730 }
731 }
732 return null;
733 }
734
735 /**
736 * Like getPhoneAccount, but checks to see if the current user is allowed to see the phone
737 * account before returning it. The current user is the active user on the actual android
738 * device.
739 */
Tony Mak240656f2015-12-04 11:36:22 +0000740 public PhoneAccount getPhoneAccount(PhoneAccountHandle handle, UserHandle userHandle) {
741 return getPhoneAccount(handle, userHandle, /* acrossProfiles */ false);
742 }
743
744 public PhoneAccount getPhoneAccount(PhoneAccountHandle handle,
745 UserHandle userHandle, boolean acrossProfiles) {
Tony Mak98e6bdc2015-11-25 21:54:05 +0000746 PhoneAccount account = getPhoneAccountUnchecked(handle);
Tony Mak240656f2015-12-04 11:36:22 +0000747 if (account != null && (isVisibleForUser(account, userHandle, acrossProfiles))) {
Santos Cordon6a212642015-05-08 16:35:23 -0700748 return account;
749 }
750 return null;
751 }
752
Tony Mak240656f2015-12-04 11:36:22 +0000753 public PhoneAccount getPhoneAccountOfCurrentUser(PhoneAccountHandle handle) {
754 return getPhoneAccount(handle, mCurrentUserHandle);
755 }
756
Bryce Leea0bb7052015-10-16 13:31:40 -0700757 private List<PhoneAccountHandle> getPhoneAccountHandles(
758 int capabilities,
759 String uriScheme,
760 String packageName,
Tony Mak240656f2015-12-04 11:36:22 +0000761 boolean includeDisabledAccounts,
762 UserHandle userHandle) {
Bryce Leea0bb7052015-10-16 13:31:40 -0700763 return getPhoneAccountHandles(capabilities, 0 /*excludedCapabilities*/, uriScheme,
Tony Mak240656f2015-12-04 11:36:22 +0000764 packageName, includeDisabledAccounts, userHandle);
Bryce Leea0bb7052015-10-16 13:31:40 -0700765 }
766
Santos Cordon6a212642015-05-08 16:35:23 -0700767 /**
768 * Returns a list of phone account handles with the specified capabilities, uri scheme,
769 * and package name.
770 */
771 private List<PhoneAccountHandle> getPhoneAccountHandles(
Santos Cordonea5cb932015-05-07 16:28:38 -0700772 int capabilities,
Bryce Leea0bb7052015-10-16 13:31:40 -0700773 int excludedCapabilities,
Santos Cordonea5cb932015-05-07 16:28:38 -0700774 String uriScheme,
775 String packageName,
Tony Mak240656f2015-12-04 11:36:22 +0000776 boolean includeDisabledAccounts,
777 UserHandle userHandle) {
Santos Cordon6a212642015-05-08 16:35:23 -0700778 List<PhoneAccountHandle> handles = new ArrayList<>();
Santos Cordonea5cb932015-05-07 16:28:38 -0700779
780 for (PhoneAccount account : getPhoneAccounts(
Bryce Leea0bb7052015-10-16 13:31:40 -0700781 capabilities, excludedCapabilities, uriScheme, packageName,
Tony Mak240656f2015-12-04 11:36:22 +0000782 includeDisabledAccounts, userHandle)) {
Santos Cordon6a212642015-05-08 16:35:23 -0700783 handles.add(account.getAccountHandle());
784 }
785 return handles;
Tyler Gunn84253572014-09-02 14:50:05 -0700786 }
787
Bryce Leea0bb7052015-10-16 13:31:40 -0700788 private List<PhoneAccount> getPhoneAccounts(
789 int capabilities,
790 String uriScheme,
791 String packageName,
Tony Mak240656f2015-12-04 11:36:22 +0000792 boolean includeDisabledAccounts,
793 UserHandle userHandle) {
Bryce Leea0bb7052015-10-16 13:31:40 -0700794 return getPhoneAccounts(capabilities, 0 /*excludedCapabilities*/, uriScheme, packageName,
Tony Mak240656f2015-12-04 11:36:22 +0000795 includeDisabledAccounts, userHandle);
Bryce Leea0bb7052015-10-16 13:31:40 -0700796 }
797
Tyler Gunn84253572014-09-02 14:50:05 -0700798 /**
799 * Returns a list of phone account handles with the specified flag, supporting the specified
Santos Cordon6a212642015-05-08 16:35:23 -0700800 * URI scheme, within the specified package name.
Tyler Gunn84253572014-09-02 14:50:05 -0700801 *
Santos Cordon6a212642015-05-08 16:35:23 -0700802 * @param capabilities Capabilities which the {@code PhoneAccount} must have. Ignored if 0.
Bryce Leea0bb7052015-10-16 13:31:40 -0700803 * @param excludedCapabilities Capabilities which the {@code PhoneAccount} must not have.
804 * Ignored if 0.
Santos Cordon6a212642015-05-08 16:35:23 -0700805 * @param uriScheme URI schemes the PhoneAccount must handle. {@code null} bypasses the
Tyler Gunn84253572014-09-02 14:50:05 -0700806 * URI scheme check.
Santos Cordon6a212642015-05-08 16:35:23 -0700807 * @param packageName Package name of the PhoneAccount. {@code null} bypasses packageName check.
Tyler Gunn84253572014-09-02 14:50:05 -0700808 */
Santos Cordon6a212642015-05-08 16:35:23 -0700809 private List<PhoneAccount> getPhoneAccounts(
Santos Cordonea5cb932015-05-07 16:28:38 -0700810 int capabilities,
Bryce Leea0bb7052015-10-16 13:31:40 -0700811 int excludedCapabilities,
Santos Cordonea5cb932015-05-07 16:28:38 -0700812 String uriScheme,
813 String packageName,
Tony Mak240656f2015-12-04 11:36:22 +0000814 boolean includeDisabledAccounts,
815 UserHandle userHandle) {
Santos Cordon6a212642015-05-08 16:35:23 -0700816 List<PhoneAccount> accounts = new ArrayList<>(mState.accounts.size());
Ihab Awad293edf22014-07-24 17:52:29 -0700817 for (PhoneAccount m : mState.accounts) {
Santos Cordonea5cb932015-05-07 16:28:38 -0700818 if (!(m.isEnabled() || includeDisabledAccounts)) {
819 // Do not include disabled accounts.
820 continue;
821 }
822
Bryce Leea0bb7052015-10-16 13:31:40 -0700823 if ((m.getCapabilities() & excludedCapabilities) != 0) {
824 // If an excluded capability is present, skip.
825 continue;
826 }
827
Santos Cordon6a212642015-05-08 16:35:23 -0700828 if (capabilities != 0 && !m.hasCapabilities(capabilities)) {
Evan Charlton105d9772014-11-25 14:08:53 -0800829 // Account doesn't have the right capabilities; skip this one.
830 continue;
Ihab Awadf2a84912014-07-22 21:09:25 -0700831 }
Evan Charlton105d9772014-11-25 14:08:53 -0800832 if (uriScheme != null && !m.supportsUriScheme(uriScheme)) {
833 // Account doesn't support this URI scheme; skip this one.
834 continue;
835 }
Santos Cordon6a212642015-05-08 16:35:23 -0700836 PhoneAccountHandle handle = m.getAccountHandle();
837
838 if (resolveComponent(handle).isEmpty()) {
Evan Charlton105d9772014-11-25 14:08:53 -0800839 // This component cannot be resolved anymore; skip this one.
840 continue;
841 }
Santos Cordon6a212642015-05-08 16:35:23 -0700842 if (packageName != null &&
843 !packageName.equals(handle.getComponentName().getPackageName())) {
844 // Not the right package name; skip this one.
845 continue;
846 }
Tony Mak240656f2015-12-04 11:36:22 +0000847 if (!isVisibleForUser(m, userHandle, false)) {
Evan Charlton105d9772014-11-25 14:08:53 -0800848 // Account is not visible for the current user; skip this one.
849 continue;
850 }
Santos Cordon6a212642015-05-08 16:35:23 -0700851 accounts.add(m);
Ihab Awad104f8062014-07-17 11:29:35 -0700852 }
Santos Cordon6a212642015-05-08 16:35:23 -0700853 return accounts;
Ihab Awad104f8062014-07-17 11:29:35 -0700854 }
855
Santos Cordon6a212642015-05-08 16:35:23 -0700856 //
857 // State Implementation for PhoneAccountRegistrar
858 //
859
Ihab Awad293edf22014-07-24 17:52:29 -0700860 /**
861 * The state of this {@code PhoneAccountRegistrar}.
862 */
Ihab Awadb78b2762014-07-25 15:16:23 -0700863 @VisibleForTesting
864 public static class State {
Ihab Awad293edf22014-07-24 17:52:29 -0700865 /**
Tony Mak4a3e2fd2015-12-04 11:58:38 +0000866 * Store the default phone account handle of users. If no record of a user can be found in
867 * the map, it means that no default phone account handle is set in that user.
Ihab Awad293edf22014-07-24 17:52:29 -0700868 */
Tony Mak4a3e2fd2015-12-04 11:58:38 +0000869 public final Map<UserHandle, DefaultPhoneAccountHandle> defaultOutgoingAccountHandles
870 = new ConcurrentHashMap<>();
Ihab Awad293edf22014-07-24 17:52:29 -0700871
872 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700873 * The complete list of {@code PhoneAccount}s known to the Telecom subsystem.
Ihab Awad293edf22014-07-24 17:52:29 -0700874 */
Shriram Ganesh8f89f8d2015-04-15 12:35:43 +0530875 public final List<PhoneAccount> accounts = new CopyOnWriteArrayList<>();
Tyler Gunn84253572014-09-02 14:50:05 -0700876
877 /**
878 * The version number of the State data.
879 */
880 public int versionNumber;
Ihab Awad293edf22014-07-24 17:52:29 -0700881 }
882
Tyler Gunn9787e0e2014-10-14 14:36:12 -0700883 /**
Tony Mak4a3e2fd2015-12-04 11:58:38 +0000884 * The default {@link PhoneAccountHandle} of a user.
885 */
886 public static class DefaultPhoneAccountHandle {
887
888 public final UserHandle userHandle;
889
890 public final PhoneAccountHandle phoneAccountHandle;
891
892 public DefaultPhoneAccountHandle(UserHandle userHandle,
893 PhoneAccountHandle phoneAccountHandle) {
894 this.userHandle = userHandle;
895 this.phoneAccountHandle = phoneAccountHandle;
896 }
897 }
898
899 /**
Tyler Gunn9787e0e2014-10-14 14:36:12 -0700900 * Dumps the state of the {@link CallsManager}.
901 *
902 * @param pw The {@code IndentingPrintWriter} to write the state to.
903 */
904 public void dump(IndentingPrintWriter pw) {
905 if (mState != null) {
906 pw.println("xmlVersion: " + mState.versionNumber);
Tony Mak4a3e2fd2015-12-04 11:58:38 +0000907 DefaultPhoneAccountHandle defaultPhoneAccountHandle
908 = mState.defaultOutgoingAccountHandles.get(Process.myUserHandle());
909 pw.println("defaultOutgoing: " + (defaultPhoneAccountHandle == null ? "none" :
910 defaultPhoneAccountHandle.phoneAccountHandle));
Tony Mak240656f2015-12-04 11:36:22 +0000911 pw.println("simCallManager: " + getSimCallManager(mCurrentUserHandle));
Tyler Gunn9787e0e2014-10-14 14:36:12 -0700912 pw.println("phoneAccounts:");
913 pw.increaseIndent();
914 for (PhoneAccount phoneAccount : mState.accounts) {
915 pw.println(phoneAccount);
916 }
917 pw.decreaseIndent();
918 }
919 }
920
Ihab Awad293edf22014-07-24 17:52:29 -0700921 ////////////////////////////////////////////////////////////////////////////////////////////////
922 //
923 // State management
924 //
925
926 private void write() {
Ihab Awadb78b2762014-07-25 15:16:23 -0700927 final FileOutputStream os;
Ihab Awad104f8062014-07-17 11:29:35 -0700928 try {
Ihab Awadb78b2762014-07-25 15:16:23 -0700929 os = mAtomicFile.startWrite();
930 boolean success = false;
931 try {
932 XmlSerializer serializer = new FastXmlSerializer();
933 serializer.setOutput(new BufferedOutputStream(os), "utf-8");
Evan Charlton105d9772014-11-25 14:08:53 -0800934 writeToXml(mState, serializer, mContext);
Ihab Awadb78b2762014-07-25 15:16:23 -0700935 serializer.flush();
936 success = true;
937 } finally {
938 if (success) {
939 mAtomicFile.finishWrite(os);
940 } else {
941 mAtomicFile.failWrite(os);
942 }
943 }
944 } catch (IOException e) {
945 Log.e(this, e, "Writing state to XML file");
Ihab Awad104f8062014-07-17 11:29:35 -0700946 }
947 }
948
Ihab Awadb78b2762014-07-25 15:16:23 -0700949 private void read() {
950 final InputStream is;
Ihab Awad104f8062014-07-17 11:29:35 -0700951 try {
Ihab Awadb78b2762014-07-25 15:16:23 -0700952 is = mAtomicFile.openRead();
953 } catch (FileNotFoundException ex) {
954 return;
955 }
956
Tyler Gunn84253572014-09-02 14:50:05 -0700957 boolean versionChanged = false;
958
Ihab Awadb78b2762014-07-25 15:16:23 -0700959 XmlPullParser parser;
960 try {
961 parser = Xml.newPullParser();
962 parser.setInput(new BufferedInputStream(is), null);
963 parser.nextTag();
Tyler Gunn84253572014-09-02 14:50:05 -0700964 mState = readFromXml(parser, mContext);
965 versionChanged = mState.versionNumber < EXPECTED_STATE_VERSION;
966
Ihab Awadb78b2762014-07-25 15:16:23 -0700967 } catch (IOException | XmlPullParserException e) {
968 Log.e(this, e, "Reading state from XML file");
969 mState = new State();
970 } finally {
971 try {
972 is.close();
973 } catch (IOException e) {
974 Log.e(this, e, "Closing InputStream");
975 }
Ihab Awad104f8062014-07-17 11:29:35 -0700976 }
Tyler Gunn84253572014-09-02 14:50:05 -0700977
Evan Charlton105d9772014-11-25 14:08:53 -0800978 // Verify all of the UserHandles.
979 List<PhoneAccount> badAccounts = new ArrayList<>();
980 for (PhoneAccount phoneAccount : mState.accounts) {
981 UserHandle userHandle = phoneAccount.getAccountHandle().getUserHandle();
982 if (userHandle == null) {
983 Log.w(this, "Missing UserHandle for %s", phoneAccount);
984 badAccounts.add(phoneAccount);
985 } else if (mUserManager.getSerialNumberForUser(userHandle) == -1) {
986 Log.w(this, "User does not exist for %s", phoneAccount);
987 badAccounts.add(phoneAccount);
988 }
989 }
990 mState.accounts.removeAll(badAccounts);
991
Tyler Gunn84253572014-09-02 14:50:05 -0700992 // If an upgrade occurred, write out the changed data.
Evan Charlton105d9772014-11-25 14:08:53 -0800993 if (versionChanged || !badAccounts.isEmpty()) {
Tyler Gunn84253572014-09-02 14:50:05 -0700994 write();
995 }
Santos Cordon176ae282014-07-14 02:02:14 -0700996 }
997
Evan Charlton105d9772014-11-25 14:08:53 -0800998 private static void writeToXml(State state, XmlSerializer serializer, Context context)
Ihab Awadb78b2762014-07-25 15:16:23 -0700999 throws IOException {
Evan Charlton105d9772014-11-25 14:08:53 -08001000 sStateXml.writeToXml(state, serializer, context);
Santos Cordon176ae282014-07-14 02:02:14 -07001001 }
Ihab Awad104f8062014-07-17 11:29:35 -07001002
Tyler Gunn84253572014-09-02 14:50:05 -07001003 private static State readFromXml(XmlPullParser parser, Context context)
Ihab Awadb78b2762014-07-25 15:16:23 -07001004 throws IOException, XmlPullParserException {
Tyler Gunn84253572014-09-02 14:50:05 -07001005 State s = sStateXml.readFromXml(parser, 0, context);
Ihab Awadb78b2762014-07-25 15:16:23 -07001006 return s != null ? s : new State();
Ihab Awad104f8062014-07-17 11:29:35 -07001007 }
1008
Ihab Awad293edf22014-07-24 17:52:29 -07001009 ////////////////////////////////////////////////////////////////////////////////////////////////
Ihab Awad104f8062014-07-17 11:29:35 -07001010 //
Ihab Awadb78b2762014-07-25 15:16:23 -07001011 // XML serialization
Ihab Awad104f8062014-07-17 11:29:35 -07001012 //
1013
Ihab Awadb78b2762014-07-25 15:16:23 -07001014 @VisibleForTesting
Ihab Awad26923222014-07-30 10:54:35 -07001015 public abstract static class XmlSerialization<T> {
Tyler Gunn369c8742015-10-07 15:39:28 -07001016 private static final String TAG_VALUE = "value";
1017 private static final String ATTRIBUTE_LENGTH = "length";
1018 private static final String ATTRIBUTE_KEY = "key";
1019 private static final String ATTRIBUTE_VALUE_TYPE = "type";
1020 private static final String VALUE_TYPE_STRING = "string";
1021 private static final String VALUE_TYPE_INTEGER = "integer";
1022 private static final String VALUE_TYPE_BOOLEAN = "boolean";
Tyler Gunn84253572014-09-02 14:50:05 -07001023
Ihab Awadb78b2762014-07-25 15:16:23 -07001024 /**
1025 * Write the supplied object to XML
1026 */
Evan Charlton105d9772014-11-25 14:08:53 -08001027 public abstract void writeToXml(T o, XmlSerializer serializer, Context context)
Ihab Awad26923222014-07-30 10:54:35 -07001028 throws IOException;
Ihab Awadb78b2762014-07-25 15:16:23 -07001029
1030 /**
1031 * Read from the supplied XML into a new object, returning null in case of an
1032 * unrecoverable schema mismatch or other data error. 'parser' must be already
1033 * positioned at the first tag that is expected to have been emitted by this
1034 * object's writeToXml(). This object tries to fail early without modifying
1035 * 'parser' if it does not recognize the data it sees.
1036 */
Tyler Gunn84253572014-09-02 14:50:05 -07001037 public abstract T readFromXml(XmlPullParser parser, int version, Context context)
Ihab Awad26923222014-07-30 10:54:35 -07001038 throws IOException, XmlPullParserException;
1039
Ihab Awadd9f54382014-10-24 11:44:47 -07001040 protected void writeTextIfNonNull(String tagName, Object value, XmlSerializer serializer)
Ihab Awad26923222014-07-30 10:54:35 -07001041 throws IOException {
1042 if (value != null) {
1043 serializer.startTag(null, tagName);
1044 serializer.text(Objects.toString(value));
1045 serializer.endTag(null, tagName);
1046 }
1047 }
Tyler Gunn84253572014-09-02 14:50:05 -07001048
1049 /**
1050 * Serializes a string array.
1051 *
1052 * @param tagName The tag name for the string array.
1053 * @param values The string values to serialize.
1054 * @param serializer The serializer.
1055 * @throws IOException
1056 */
1057 protected void writeStringList(String tagName, List<String> values,
1058 XmlSerializer serializer)
1059 throws IOException {
1060
1061 serializer.startTag(null, tagName);
1062 if (values != null) {
Tyler Gunn369c8742015-10-07 15:39:28 -07001063 serializer.attribute(null, ATTRIBUTE_LENGTH, Objects.toString(values.size()));
Tyler Gunn84253572014-09-02 14:50:05 -07001064 for (String toSerialize : values) {
Tyler Gunn369c8742015-10-07 15:39:28 -07001065 serializer.startTag(null, TAG_VALUE);
Tyler Gunn84253572014-09-02 14:50:05 -07001066 if (toSerialize != null ){
1067 serializer.text(toSerialize);
1068 }
Tyler Gunn369c8742015-10-07 15:39:28 -07001069 serializer.endTag(null, TAG_VALUE);
Tyler Gunn84253572014-09-02 14:50:05 -07001070 }
1071 } else {
Tyler Gunn369c8742015-10-07 15:39:28 -07001072 serializer.attribute(null, ATTRIBUTE_LENGTH, "0");
1073 }
1074 serializer.endTag(null, tagName);
1075 }
1076
1077 protected void writeBundle(String tagName, Bundle values, XmlSerializer serializer)
1078 throws IOException {
1079
1080 serializer.startTag(null, tagName);
1081 if (values != null) {
1082 for (String key : values.keySet()) {
1083 Object value = values.get(key);
1084
1085 if (value == null) {
1086 continue;
1087 }
1088
1089 String valueType;
1090 if (value instanceof String) {
1091 valueType = VALUE_TYPE_STRING;
1092 } else if (value instanceof Integer) {
1093 valueType = VALUE_TYPE_INTEGER;
1094 } else if (value instanceof Boolean) {
1095 valueType = VALUE_TYPE_BOOLEAN;
1096 } else {
1097 Log.w(this,
1098 "PhoneAccounts support only string, integer and boolean extras TY.");
1099 continue;
1100 }
1101
1102 serializer.startTag(null, TAG_VALUE);
1103 serializer.attribute(null, ATTRIBUTE_KEY, key);
1104 serializer.attribute(null, ATTRIBUTE_VALUE_TYPE, valueType);
1105 serializer.text(Objects.toString(value));
1106 serializer.endTag(null, TAG_VALUE);
1107 }
Tyler Gunn84253572014-09-02 14:50:05 -07001108 }
1109 serializer.endTag(null, tagName);
Ihab Awadd9f54382014-10-24 11:44:47 -07001110 }
Tyler Gunn84253572014-09-02 14:50:05 -07001111
Santos Cordon9c30c282015-05-13 16:28:27 -07001112 protected void writeIconIfNonNull(String tagName, Icon value, XmlSerializer serializer)
Ihab Awadd9f54382014-10-24 11:44:47 -07001113 throws IOException {
Santos Cordon9c30c282015-05-13 16:28:27 -07001114 if (value != null) {
Ihab Awadd9f54382014-10-24 11:44:47 -07001115 ByteArrayOutputStream stream = new ByteArrayOutputStream();
Santos Cordon9c30c282015-05-13 16:28:27 -07001116 value.writeToStream(stream);
1117 byte[] iconByteArray = stream.toByteArray();
1118 String text = Base64.encodeToString(iconByteArray, 0, iconByteArray.length, 0);
Ihab Awadd9f54382014-10-24 11:44:47 -07001119
1120 serializer.startTag(null, tagName);
1121 serializer.text(text);
1122 serializer.endTag(null, tagName);
1123 }
Tyler Gunn84253572014-09-02 14:50:05 -07001124 }
1125
Evan Charlton105d9772014-11-25 14:08:53 -08001126 protected void writeLong(String tagName, long value, XmlSerializer serializer)
1127 throws IOException {
1128 serializer.startTag(null, tagName);
1129 serializer.text(Long.valueOf(value).toString());
1130 serializer.endTag(null, tagName);
1131 }
1132
Tyler Gunn84253572014-09-02 14:50:05 -07001133 /**
1134 * Reads a string array from the XML parser.
1135 *
1136 * @param parser The XML parser.
1137 * @return String array containing the parsed values.
1138 * @throws IOException Exception related to IO.
1139 * @throws XmlPullParserException Exception related to parsing.
1140 */
1141 protected List<String> readStringList(XmlPullParser parser)
1142 throws IOException, XmlPullParserException {
1143
Tyler Gunn369c8742015-10-07 15:39:28 -07001144 int length = Integer.parseInt(parser.getAttributeValue(null, ATTRIBUTE_LENGTH));
Tyler Gunn84253572014-09-02 14:50:05 -07001145 List<String> arrayEntries = new ArrayList<String>(length);
1146 String value = null;
1147
1148 if (length == 0) {
1149 return arrayEntries;
1150 }
1151
1152 int outerDepth = parser.getDepth();
1153 while (XmlUtils.nextElementWithin(parser, outerDepth)) {
Tyler Gunn369c8742015-10-07 15:39:28 -07001154 if (parser.getName().equals(TAG_VALUE)) {
Tyler Gunn8e0fef42014-09-08 18:34:44 -07001155 parser.next();
Tyler Gunn84253572014-09-02 14:50:05 -07001156 value = parser.getText();
1157 arrayEntries.add(value);
1158 }
1159 }
1160
1161 return arrayEntries;
1162 }
Ihab Awadd9f54382014-10-24 11:44:47 -07001163
Tyler Gunn369c8742015-10-07 15:39:28 -07001164 /**
1165 * Reads a bundle from the XML parser.
1166 *
1167 * @param parser The XML parser.
1168 * @return Bundle containing the parsed values.
1169 * @throws IOException Exception related to IO.
1170 * @throws XmlPullParserException Exception related to parsing.
1171 */
1172 protected Bundle readBundle(XmlPullParser parser)
1173 throws IOException, XmlPullParserException {
1174
1175 Bundle bundle = null;
1176 int outerDepth = parser.getDepth();
1177 while (XmlUtils.nextElementWithin(parser, outerDepth)) {
1178 if (parser.getName().equals(TAG_VALUE)) {
1179 String valueType = parser.getAttributeValue(null, ATTRIBUTE_VALUE_TYPE);
1180 String key = parser.getAttributeValue(null, ATTRIBUTE_KEY);
1181 parser.next();
1182 String value = parser.getText();
1183
1184 if (bundle == null) {
1185 bundle = new Bundle();
1186 }
1187
1188 // Do not write null values to the bundle.
1189 if (value == null) {
1190 continue;
1191 }
1192
1193 if (VALUE_TYPE_STRING.equals(valueType)) {
1194 bundle.putString(key, value);
1195 } else if (VALUE_TYPE_INTEGER.equals(valueType)) {
1196 try {
1197 int intValue = Integer.parseInt(value);
1198 bundle.putInt(key, intValue);
1199 } catch (NumberFormatException nfe) {
1200 Log.w(this, "Invalid integer PhoneAccount extra.");
1201 }
1202 } else if (VALUE_TYPE_BOOLEAN.equals(valueType)) {
1203 boolean boolValue = Boolean.parseBoolean(value);
1204 bundle.putBoolean(key, boolValue);
1205 } else {
1206 Log.w(this, "Invalid type " + valueType + " for PhoneAccount bundle.");
1207 }
1208 }
1209 }
1210 return bundle;
1211 }
1212
Santos Cordon9c30c282015-05-13 16:28:27 -07001213 protected Bitmap readBitmap(XmlPullParser parser) {
Ihab Awadd9f54382014-10-24 11:44:47 -07001214 byte[] imageByteArray = Base64.decode(parser.getText(), 0);
1215 return BitmapFactory.decodeByteArray(imageByteArray, 0, imageByteArray.length);
1216 }
Santos Cordon9c30c282015-05-13 16:28:27 -07001217
1218 protected Icon readIcon(XmlPullParser parser) throws IOException {
1219 byte[] iconByteArray = Base64.decode(parser.getText(), 0);
1220 ByteArrayInputStream stream = new ByteArrayInputStream(iconByteArray);
1221 return Icon.createFromStream(stream);
1222 }
Ihab Awad104f8062014-07-17 11:29:35 -07001223 }
1224
Ihab Awadb78b2762014-07-25 15:16:23 -07001225 @VisibleForTesting
1226 public static final XmlSerialization<State> sStateXml =
1227 new XmlSerialization<State>() {
1228 private static final String CLASS_STATE = "phone_account_registrar_state";
Ihab Awad104f8062014-07-17 11:29:35 -07001229 private static final String DEFAULT_OUTGOING = "default_outgoing";
1230 private static final String ACCOUNTS = "accounts";
Tyler Gunn84253572014-09-02 14:50:05 -07001231 private static final String VERSION = "version";
Ihab Awad104f8062014-07-17 11:29:35 -07001232
1233 @Override
Evan Charlton105d9772014-11-25 14:08:53 -08001234 public void writeToXml(State o, XmlSerializer serializer, Context context)
Ihab Awadb78b2762014-07-25 15:16:23 -07001235 throws IOException {
Ihab Awad26923222014-07-30 10:54:35 -07001236 if (o != null) {
1237 serializer.startTag(null, CLASS_STATE);
Tyler Gunn84253572014-09-02 14:50:05 -07001238 serializer.attribute(null, VERSION, Objects.toString(EXPECTED_STATE_VERSION));
Ihab Awadb78b2762014-07-25 15:16:23 -07001239
Tony Mak4a3e2fd2015-12-04 11:58:38 +00001240 serializer.startTag(null, DEFAULT_OUTGOING);
1241 for (DefaultPhoneAccountHandle defaultPhoneAccountHandle : o
1242 .defaultOutgoingAccountHandles.values()) {
1243 sDefaultPhoneAcountHandleXml
1244 .writeToXml(defaultPhoneAccountHandle, serializer, context);
Ihab Awad26923222014-07-30 10:54:35 -07001245 }
Tony Mak4a3e2fd2015-12-04 11:58:38 +00001246 serializer.endTag(null, DEFAULT_OUTGOING);
Ihab Awad26923222014-07-30 10:54:35 -07001247
Ihab Awad26923222014-07-30 10:54:35 -07001248 serializer.startTag(null, ACCOUNTS);
1249 for (PhoneAccount m : o.accounts) {
Evan Charlton105d9772014-11-25 14:08:53 -08001250 sPhoneAccountXml.writeToXml(m, serializer, context);
Ihab Awad26923222014-07-30 10:54:35 -07001251 }
1252 serializer.endTag(null, ACCOUNTS);
1253
1254 serializer.endTag(null, CLASS_STATE);
Ihab Awad293edf22014-07-24 17:52:29 -07001255 }
Ihab Awad104f8062014-07-17 11:29:35 -07001256 }
1257
1258 @Override
Tyler Gunn84253572014-09-02 14:50:05 -07001259 public State readFromXml(XmlPullParser parser, int version, Context context)
Ihab Awadb78b2762014-07-25 15:16:23 -07001260 throws IOException, XmlPullParserException {
1261 if (parser.getName().equals(CLASS_STATE)) {
1262 State s = new State();
Tyler Gunn84253572014-09-02 14:50:05 -07001263
1264 String rawVersion = parser.getAttributeValue(null, VERSION);
Tony Mak4a3e2fd2015-12-04 11:58:38 +00001265 s.versionNumber = TextUtils.isEmpty(rawVersion) ? 1 : Integer.parseInt(rawVersion);
Tyler Gunn84253572014-09-02 14:50:05 -07001266
Ihab Awadb78b2762014-07-25 15:16:23 -07001267 int outerDepth = parser.getDepth();
1268 while (XmlUtils.nextElementWithin(parser, outerDepth)) {
1269 if (parser.getName().equals(DEFAULT_OUTGOING)) {
Tony Mak4a3e2fd2015-12-04 11:58:38 +00001270 if (s.versionNumber < 9) {
1271 // Migration old default phone account handle here by assuming the
1272 // default phone account handle is belong to primary user.
1273 parser.nextTag();
1274 PhoneAccountHandle phoneAccountHandle = sPhoneAccountHandleXml
1275 .readFromXml(parser, s.versionNumber, context);
1276 UserManager userManager = UserManager.get(context);
1277 UserInfo primaryUser = userManager.getPrimaryUser();
1278 if (primaryUser != null) {
1279 UserHandle userHandle = primaryUser.getUserHandle();
1280 DefaultPhoneAccountHandle defaultPhoneAccountHandle
1281 = new DefaultPhoneAccountHandle(userHandle,
1282 phoneAccountHandle);
1283 s.defaultOutgoingAccountHandles
1284 .put(userHandle, defaultPhoneAccountHandle);
1285 }
1286 } else {
1287 int defaultAccountHandlesDepth = parser.getDepth();
1288 while (XmlUtils.nextElementWithin(parser, defaultAccountHandlesDepth)) {
1289 DefaultPhoneAccountHandle accountHandle
1290 = sDefaultPhoneAcountHandleXml
1291 .readFromXml(parser, s.versionNumber, context);
1292 if (accountHandle != null && s.accounts != null) {
1293 s.defaultOutgoingAccountHandles
1294 .put(accountHandle.userHandle, accountHandle);
1295 }
1296 }
1297 }
Ihab Awadb78b2762014-07-25 15:16:23 -07001298 } else if (parser.getName().equals(ACCOUNTS)) {
1299 int accountsDepth = parser.getDepth();
1300 while (XmlUtils.nextElementWithin(parser, accountsDepth)) {
Tyler Gunn84253572014-09-02 14:50:05 -07001301 PhoneAccount account = sPhoneAccountXml.readFromXml(parser,
1302 s.versionNumber, context);
1303
1304 if (account != null && s.accounts != null) {
Ihab Awadb78b2762014-07-25 15:16:23 -07001305 s.accounts.add(account);
1306 }
1307 }
Ihab Awad104f8062014-07-17 11:29:35 -07001308 }
1309 }
Ihab Awadb78b2762014-07-25 15:16:23 -07001310 return s;
Ihab Awad104f8062014-07-17 11:29:35 -07001311 }
Ihab Awadb78b2762014-07-25 15:16:23 -07001312 return null;
Ihab Awad104f8062014-07-17 11:29:35 -07001313 }
1314 };
1315
Ihab Awadb78b2762014-07-25 15:16:23 -07001316 @VisibleForTesting
Tony Mak4a3e2fd2015-12-04 11:58:38 +00001317 public static final XmlSerialization<DefaultPhoneAccountHandle> sDefaultPhoneAcountHandleXml =
1318 new XmlSerialization<DefaultPhoneAccountHandle>() {
1319 private static final String CLASS_DEFAULT_OUTGOING_PHONE_ACCOUNT_HANDLE
1320 = "default_outgoing_phone_account_handle";
1321 private static final String USER_SERIAL_NUMBER = "user_serial_number";
1322 private static final String ACCOUNT_HANDLE = "account_handle";
1323
1324 @Override
1325 public void writeToXml(DefaultPhoneAccountHandle o, XmlSerializer serializer,
1326 Context context) throws IOException {
1327 if (o != null) {
1328 final UserManager userManager = UserManager.get(context);
1329 final long serialNumber = userManager.getSerialNumberForUser(o.userHandle);
1330 if (serialNumber != -1) {
1331 serializer.startTag(null, CLASS_DEFAULT_OUTGOING_PHONE_ACCOUNT_HANDLE);
1332 writeLong(USER_SERIAL_NUMBER, serialNumber, serializer);
1333 serializer.startTag(null, ACCOUNT_HANDLE);
1334 sPhoneAccountHandleXml.writeToXml(o.phoneAccountHandle, serializer,
1335 context);
1336 serializer.endTag(null, ACCOUNT_HANDLE);
1337 serializer.endTag(null, CLASS_DEFAULT_OUTGOING_PHONE_ACCOUNT_HANDLE);
1338 }
1339 }
1340 }
1341
1342 @Override
1343 public DefaultPhoneAccountHandle readFromXml(XmlPullParser parser, int version,
1344 Context context)
1345 throws IOException, XmlPullParserException {
1346 if (parser.getName().equals(CLASS_DEFAULT_OUTGOING_PHONE_ACCOUNT_HANDLE)) {
1347 int outerDepth = parser.getDepth();
1348 PhoneAccountHandle accountHandle = null;
1349 String userSerialNumberString = null;
1350 while (XmlUtils.nextElementWithin(parser, outerDepth)) {
1351 if (parser.getName().equals(ACCOUNT_HANDLE)) {
1352 parser.nextTag();
1353 accountHandle = sPhoneAccountHandleXml.readFromXml(parser, version,
1354 context);
1355 } else if (parser.getName().equals(USER_SERIAL_NUMBER)) {
1356 parser.next();
1357 userSerialNumberString = parser.getText();
1358 }
1359 }
1360 UserHandle userHandle = null;
1361 if (userSerialNumberString != null) {
1362 try {
1363 long serialNumber = Long.parseLong(userSerialNumberString);
1364 userHandle = UserManager.get(context)
1365 .getUserForSerialNumber(serialNumber);
1366 } catch (NumberFormatException e) {
1367 Log.e(this, e,
1368 "Could not parse UserHandle " + userSerialNumberString);
1369 }
1370 }
1371 if (accountHandle != null && userHandle != null) {
1372 return new DefaultPhoneAccountHandle(userHandle, accountHandle);
1373 }
1374 }
1375 return null;
1376 }
1377 };
1378
1379
1380 @VisibleForTesting
Ihab Awadb78b2762014-07-25 15:16:23 -07001381 public static final XmlSerialization<PhoneAccount> sPhoneAccountXml =
1382 new XmlSerialization<PhoneAccount>() {
1383 private static final String CLASS_PHONE_ACCOUNT = "phone_account";
1384 private static final String ACCOUNT_HANDLE = "account_handle";
Andrew Lee7129f1c2014-09-04 11:55:07 -07001385 private static final String ADDRESS = "handle";
1386 private static final String SUBSCRIPTION_ADDRESS = "subscription_number";
Ihab Awad104f8062014-07-17 11:29:35 -07001387 private static final String CAPABILITIES = "capabilities";
1388 private static final String ICON_RES_ID = "icon_res_id";
Ihab Awadd9f54382014-10-24 11:44:47 -07001389 private static final String ICON_PACKAGE_NAME = "icon_package_name";
1390 private static final String ICON_BITMAP = "icon_bitmap";
Ihab Awad7e2c7f32014-11-03 09:49:45 -08001391 private static final String ICON_TINT = "icon_tint";
1392 private static final String HIGHLIGHT_COLOR = "highlight_color";
Ihab Awad104f8062014-07-17 11:29:35 -07001393 private static final String LABEL = "label";
1394 private static final String SHORT_DESCRIPTION = "short_description";
Tyler Gunn84253572014-09-02 14:50:05 -07001395 private static final String SUPPORTED_URI_SCHEMES = "supported_uri_schemes";
Santos Cordon9c30c282015-05-13 16:28:27 -07001396 private static final String ICON = "icon";
Tyler Gunn369c8742015-10-07 15:39:28 -07001397 private static final String EXTRAS = "extras";
Santos Cordonea5cb932015-05-07 16:28:38 -07001398 private static final String ENABLED = "enabled";
Ihab Awad104f8062014-07-17 11:29:35 -07001399
1400 @Override
Evan Charlton105d9772014-11-25 14:08:53 -08001401 public void writeToXml(PhoneAccount o, XmlSerializer serializer, Context context)
Ihab Awadb78b2762014-07-25 15:16:23 -07001402 throws IOException {
Ihab Awad26923222014-07-30 10:54:35 -07001403 if (o != null) {
1404 serializer.startTag(null, CLASS_PHONE_ACCOUNT);
Ihab Awadb78b2762014-07-25 15:16:23 -07001405
Ihab Awad26923222014-07-30 10:54:35 -07001406 if (o.getAccountHandle() != null) {
1407 serializer.startTag(null, ACCOUNT_HANDLE);
Evan Charlton105d9772014-11-25 14:08:53 -08001408 sPhoneAccountHandleXml.writeToXml(o.getAccountHandle(), serializer, context);
Ihab Awad26923222014-07-30 10:54:35 -07001409 serializer.endTag(null, ACCOUNT_HANDLE);
1410 }
Ihab Awadb78b2762014-07-25 15:16:23 -07001411
Ihab Awadd9f54382014-10-24 11:44:47 -07001412 writeTextIfNonNull(ADDRESS, o.getAddress(), serializer);
1413 writeTextIfNonNull(SUBSCRIPTION_ADDRESS, o.getSubscriptionAddress(), serializer);
1414 writeTextIfNonNull(CAPABILITIES, Integer.toString(o.getCapabilities()), serializer);
Santos Cordon9c30c282015-05-13 16:28:27 -07001415 writeIconIfNonNull(ICON, o.getIcon(), serializer);
Ihab Awad7e2c7f32014-11-03 09:49:45 -08001416 writeTextIfNonNull(HIGHLIGHT_COLOR,
1417 Integer.toString(o.getHighlightColor()), serializer);
Ihab Awadd9f54382014-10-24 11:44:47 -07001418 writeTextIfNonNull(LABEL, o.getLabel(), serializer);
1419 writeTextIfNonNull(SHORT_DESCRIPTION, o.getShortDescription(), serializer);
Tyler Gunn84253572014-09-02 14:50:05 -07001420 writeStringList(SUPPORTED_URI_SCHEMES, o.getSupportedUriSchemes(), serializer);
Tyler Gunn369c8742015-10-07 15:39:28 -07001421 writeBundle(EXTRAS, o.getExtras(), serializer);
Santos Cordonea5cb932015-05-07 16:28:38 -07001422 writeTextIfNonNull(ENABLED, o.isEnabled() ? "true" : "false" , serializer);
Ihab Awadb78b2762014-07-25 15:16:23 -07001423
Ihab Awad26923222014-07-30 10:54:35 -07001424 serializer.endTag(null, CLASS_PHONE_ACCOUNT);
1425 }
Ihab Awad104f8062014-07-17 11:29:35 -07001426 }
1427
Tyler Gunn84253572014-09-02 14:50:05 -07001428 public PhoneAccount readFromXml(XmlPullParser parser, int version, Context context)
Ihab Awadb78b2762014-07-25 15:16:23 -07001429 throws IOException, XmlPullParserException {
1430 if (parser.getName().equals(CLASS_PHONE_ACCOUNT)) {
1431 int outerDepth = parser.getDepth();
1432 PhoneAccountHandle accountHandle = null;
Andrew Lee7129f1c2014-09-04 11:55:07 -07001433 Uri address = null;
1434 Uri subscriptionAddress = null;
Ihab Awadb78b2762014-07-25 15:16:23 -07001435 int capabilities = 0;
Ihab Awad7e2c7f32014-11-03 09:49:45 -08001436 int iconResId = PhoneAccount.NO_RESOURCE_ID;
Ihab Awadd9f54382014-10-24 11:44:47 -07001437 String iconPackageName = null;
Ihab Awad7e2c7f32014-11-03 09:49:45 -08001438 Bitmap iconBitmap = null;
Ihab Awad07bc5ee2014-11-12 13:42:52 -08001439 int iconTint = PhoneAccount.NO_ICON_TINT;
1440 int highlightColor = PhoneAccount.NO_HIGHLIGHT_COLOR;
Ihab Awadb78b2762014-07-25 15:16:23 -07001441 String label = null;
1442 String shortDescription = null;
Tyler Gunn84253572014-09-02 14:50:05 -07001443 List<String> supportedUriSchemes = null;
Santos Cordon9c30c282015-05-13 16:28:27 -07001444 Icon icon = null;
Santos Cordonea5cb932015-05-07 16:28:38 -07001445 boolean enabled = false;
Tyler Gunn369c8742015-10-07 15:39:28 -07001446 Bundle extras = null;
Ihab Awadb78b2762014-07-25 15:16:23 -07001447
1448 while (XmlUtils.nextElementWithin(parser, outerDepth)) {
1449 if (parser.getName().equals(ACCOUNT_HANDLE)) {
1450 parser.nextTag();
Tyler Gunn84253572014-09-02 14:50:05 -07001451 accountHandle = sPhoneAccountHandleXml.readFromXml(parser, version,
1452 context);
Andrew Lee7129f1c2014-09-04 11:55:07 -07001453 } else if (parser.getName().equals(ADDRESS)) {
Ihab Awadb78b2762014-07-25 15:16:23 -07001454 parser.next();
Andrew Lee7129f1c2014-09-04 11:55:07 -07001455 address = Uri.parse(parser.getText());
1456 } else if (parser.getName().equals(SUBSCRIPTION_ADDRESS)) {
Ihab Awadb78b2762014-07-25 15:16:23 -07001457 parser.next();
Andrew Lee7129f1c2014-09-04 11:55:07 -07001458 String nextText = parser.getText();
1459 subscriptionAddress = nextText == null ? null : Uri.parse(nextText);
Ihab Awadb78b2762014-07-25 15:16:23 -07001460 } else if (parser.getName().equals(CAPABILITIES)) {
1461 parser.next();
1462 capabilities = Integer.parseInt(parser.getText());
1463 } else if (parser.getName().equals(ICON_RES_ID)) {
1464 parser.next();
1465 iconResId = Integer.parseInt(parser.getText());
Ihab Awadd9f54382014-10-24 11:44:47 -07001466 } else if (parser.getName().equals(ICON_PACKAGE_NAME)) {
1467 parser.next();
1468 iconPackageName = parser.getText();
1469 } else if (parser.getName().equals(ICON_BITMAP)) {
1470 parser.next();
Ihab Awad7e2c7f32014-11-03 09:49:45 -08001471 iconBitmap = readBitmap(parser);
1472 } else if (parser.getName().equals(ICON_TINT)) {
Nancy Chen06ce0622014-10-23 01:17:35 +00001473 parser.next();
Ihab Awad7e2c7f32014-11-03 09:49:45 -08001474 iconTint = Integer.parseInt(parser.getText());
1475 } else if (parser.getName().equals(HIGHLIGHT_COLOR)) {
1476 parser.next();
1477 highlightColor = Integer.parseInt(parser.getText());
Ihab Awadb78b2762014-07-25 15:16:23 -07001478 } else if (parser.getName().equals(LABEL)) {
1479 parser.next();
1480 label = parser.getText();
1481 } else if (parser.getName().equals(SHORT_DESCRIPTION)) {
1482 parser.next();
1483 shortDescription = parser.getText();
Tyler Gunn84253572014-09-02 14:50:05 -07001484 } else if (parser.getName().equals(SUPPORTED_URI_SCHEMES)) {
1485 supportedUriSchemes = readStringList(parser);
Santos Cordon9c30c282015-05-13 16:28:27 -07001486 } else if (parser.getName().equals(ICON)) {
1487 parser.next();
1488 icon = readIcon(parser);
Santos Cordonea5cb932015-05-07 16:28:38 -07001489 } else if (parser.getName().equals(ENABLED)) {
1490 parser.next();
1491 enabled = "true".equalsIgnoreCase(parser.getText());
Tyler Gunn369c8742015-10-07 15:39:28 -07001492 } else if (parser.getName().equals(EXTRAS)) {
1493 parser.next();
1494 extras = readBundle(parser);
Ihab Awadb78b2762014-07-25 15:16:23 -07001495 }
1496 }
Tyler Gunn84253572014-09-02 14:50:05 -07001497
Santos Cordona9eebe42015-06-11 14:07:44 -07001498 ComponentName pstnComponentName = new ComponentName("com.android.phone",
1499 "com.android.services.telephony.TelephonyConnectionService");
Santos Cordona82aed82015-05-26 10:43:56 -07001500 ComponentName sipComponentName = new ComponentName("com.android.phone",
1501 "com.android.services.telephony.sip.SipConnectionService");
1502
Tyler Gunn84253572014-09-02 14:50:05 -07001503 // Upgrade older phone accounts to specify the supported URI schemes.
1504 if (version < 2) {
Tyler Gunn84253572014-09-02 14:50:05 -07001505 supportedUriSchemes = new ArrayList<>();
1506
1507 // Handle the SIP connection service.
1508 // Check the system settings to see if it also should handle "tel" calls.
1509 if (accountHandle.getComponentName().equals(sipComponentName)) {
1510 boolean useSipForPstn = useSipForPstnCalls(context);
1511 supportedUriSchemes.add(PhoneAccount.SCHEME_SIP);
1512 if (useSipForPstn) {
1513 supportedUriSchemes.add(PhoneAccount.SCHEME_TEL);
1514 }
1515 } else {
1516 supportedUriSchemes.add(PhoneAccount.SCHEME_TEL);
1517 supportedUriSchemes.add(PhoneAccount.SCHEME_VOICEMAIL);
1518 }
1519 }
1520
Ihab Awad7e2c7f32014-11-03 09:49:45 -08001521 // Upgrade older phone accounts with explicit package name
1522 if (version < 5) {
1523 if (iconBitmap == null) {
1524 iconPackageName = accountHandle.getComponentName().getPackageName();
1525 }
1526 }
1527
Santos Cordona82aed82015-05-26 10:43:56 -07001528 if (version < 6) {
1529 // Always enable all SIP accounts on upgrade to version 6
1530 if (accountHandle.getComponentName().equals(sipComponentName)) {
1531 enabled = true;
1532 }
1533 }
Santos Cordona9eebe42015-06-11 14:07:44 -07001534 if (version < 7) {
1535 // Always enabled all PSTN acocunts on upgrade to version 7
1536 if (accountHandle.getComponentName().equals(pstnComponentName)) {
1537 enabled = true;
1538 }
1539 }
Roshan Pius6f752c82015-08-10 10:42:06 -07001540 if (version < 8) {
1541 // Migrate the SIP account handle ids to use SIP username instead of SIP URI.
1542 if (accountHandle.getComponentName().equals(sipComponentName)) {
1543 Uri accountUri = Uri.parse(accountHandle.getId());
1544 if (accountUri.getScheme() != null &&
1545 accountUri.getScheme().equals(PhoneAccount.SCHEME_SIP)) {
1546 accountHandle = new PhoneAccountHandle(accountHandle.getComponentName(),
1547 accountUri.getSchemeSpecificPart(),
1548 accountHandle.getUserHandle());
1549 }
1550 }
1551 }
Santos Cordona82aed82015-05-26 10:43:56 -07001552
Ihab Awad7e2c7f32014-11-03 09:49:45 -08001553 PhoneAccount.Builder builder = PhoneAccount.builder(accountHandle, label)
Andrew Lee7129f1c2014-09-04 11:55:07 -07001554 .setAddress(address)
1555 .setSubscriptionAddress(subscriptionAddress)
1556 .setCapabilities(capabilities)
Andrew Lee7129f1c2014-09-04 11:55:07 -07001557 .setShortDescription(shortDescription)
1558 .setSupportedUriSchemes(supportedUriSchemes)
Santos Cordonea5cb932015-05-07 16:28:38 -07001559 .setHighlightColor(highlightColor)
Tyler Gunn369c8742015-10-07 15:39:28 -07001560 .setExtras(extras)
Santos Cordonea5cb932015-05-07 16:28:38 -07001561 .setIsEnabled(enabled);
Ihab Awad7e2c7f32014-11-03 09:49:45 -08001562
Santos Cordon9c30c282015-05-13 16:28:27 -07001563 if (icon != null) {
1564 builder.setIcon(icon);
1565 } else if (iconBitmap != null) {
1566 builder.setIcon(Icon.createWithBitmap(iconBitmap));
1567 } else if (!TextUtils.isEmpty(iconPackageName)) {
1568 builder.setIcon(Icon.createWithResource(iconPackageName, iconResId));
1569 // TODO: Need to set tint.
Ihab Awad7e2c7f32014-11-03 09:49:45 -08001570 }
1571
Ihab Awad0a4b95f2015-05-18 10:15:38 -07001572 return builder.build();
Ihab Awadb78b2762014-07-25 15:16:23 -07001573 }
1574 return null;
Ihab Awad104f8062014-07-17 11:29:35 -07001575 }
Tyler Gunn84253572014-09-02 14:50:05 -07001576
1577 /**
Santos Cordon9c30c282015-05-13 16:28:27 -07001578 * Determines if the SIP call settings specify to use SIP for all calls, including PSTN
1579 * calls.
Tyler Gunn84253572014-09-02 14:50:05 -07001580 *
1581 * @param context The context.
1582 * @return {@code True} if SIP should be used for all calls.
1583 */
1584 private boolean useSipForPstnCalls(Context context) {
1585 String option = Settings.System.getString(context.getContentResolver(),
1586 Settings.System.SIP_CALL_OPTIONS);
1587 option = (option != null) ? option : Settings.System.SIP_ADDRESS_ONLY;
1588 return option.equals(Settings.System.SIP_ALWAYS);
1589 }
Ihab Awad104f8062014-07-17 11:29:35 -07001590 };
1591
Ihab Awadb78b2762014-07-25 15:16:23 -07001592 @VisibleForTesting
1593 public static final XmlSerialization<PhoneAccountHandle> sPhoneAccountHandleXml =
1594 new XmlSerialization<PhoneAccountHandle>() {
1595 private static final String CLASS_PHONE_ACCOUNT_HANDLE = "phone_account_handle";
Ihab Awad104f8062014-07-17 11:29:35 -07001596 private static final String COMPONENT_NAME = "component_name";
1597 private static final String ID = "id";
Evan Charlton105d9772014-11-25 14:08:53 -08001598 private static final String USER_SERIAL_NUMBER = "user_serial_number";
Ihab Awad104f8062014-07-17 11:29:35 -07001599
1600 @Override
Evan Charlton105d9772014-11-25 14:08:53 -08001601 public void writeToXml(PhoneAccountHandle o, XmlSerializer serializer, Context context)
Ihab Awadb78b2762014-07-25 15:16:23 -07001602 throws IOException {
Ihab Awad26923222014-07-30 10:54:35 -07001603 if (o != null) {
1604 serializer.startTag(null, CLASS_PHONE_ACCOUNT_HANDLE);
Ihab Awadb78b2762014-07-25 15:16:23 -07001605
Ihab Awad26923222014-07-30 10:54:35 -07001606 if (o.getComponentName() != null) {
Ihab Awadd9f54382014-10-24 11:44:47 -07001607 writeTextIfNonNull(
Ihab Awad26923222014-07-30 10:54:35 -07001608 COMPONENT_NAME, o.getComponentName().flattenToString(), serializer);
1609 }
Ihab Awadb78b2762014-07-25 15:16:23 -07001610
Ihab Awadd9f54382014-10-24 11:44:47 -07001611 writeTextIfNonNull(ID, o.getId(), serializer);
Ihab Awadb78b2762014-07-25 15:16:23 -07001612
Evan Charlton105d9772014-11-25 14:08:53 -08001613 if (o.getUserHandle() != null && context != null) {
1614 UserManager userManager = UserManager.get(context);
1615 writeLong(USER_SERIAL_NUMBER,
1616 userManager.getSerialNumberForUser(o.getUserHandle()), serializer);
1617 }
1618
Ihab Awad26923222014-07-30 10:54:35 -07001619 serializer.endTag(null, CLASS_PHONE_ACCOUNT_HANDLE);
1620 }
Ihab Awad104f8062014-07-17 11:29:35 -07001621 }
1622
1623 @Override
Tyler Gunn84253572014-09-02 14:50:05 -07001624 public PhoneAccountHandle readFromXml(XmlPullParser parser, int version, Context context)
Ihab Awadb78b2762014-07-25 15:16:23 -07001625 throws IOException, XmlPullParserException {
1626 if (parser.getName().equals(CLASS_PHONE_ACCOUNT_HANDLE)) {
1627 String componentNameString = null;
1628 String idString = null;
Evan Charlton105d9772014-11-25 14:08:53 -08001629 String userSerialNumberString = null;
Ihab Awadb78b2762014-07-25 15:16:23 -07001630 int outerDepth = parser.getDepth();
Evan Charlton105d9772014-11-25 14:08:53 -08001631
1632 UserManager userManager = UserManager.get(context);
1633
Ihab Awadb78b2762014-07-25 15:16:23 -07001634 while (XmlUtils.nextElementWithin(parser, outerDepth)) {
1635 if (parser.getName().equals(COMPONENT_NAME)) {
1636 parser.next();
1637 componentNameString = parser.getText();
1638 } else if (parser.getName().equals(ID)) {
1639 parser.next();
1640 idString = parser.getText();
Evan Charlton105d9772014-11-25 14:08:53 -08001641 } else if (parser.getName().equals(USER_SERIAL_NUMBER)) {
1642 parser.next();
1643 userSerialNumberString = parser.getText();
Ihab Awadb78b2762014-07-25 15:16:23 -07001644 }
1645 }
Ihab Awad26923222014-07-30 10:54:35 -07001646 if (componentNameString != null) {
Evan Charlton105d9772014-11-25 14:08:53 -08001647 UserHandle userHandle = null;
1648 if (userSerialNumberString != null) {
1649 try {
1650 long serialNumber = Long.parseLong(userSerialNumberString);
1651 userHandle = userManager.getUserForSerialNumber(serialNumber);
1652 } catch (NumberFormatException e) {
1653 Log.e(this, e, "Could not parse UserHandle " + userSerialNumberString);
1654 }
1655 }
Ihab Awadb78b2762014-07-25 15:16:23 -07001656 return new PhoneAccountHandle(
1657 ComponentName.unflattenFromString(componentNameString),
Evan Charlton105d9772014-11-25 14:08:53 -08001658 idString,
1659 userHandle);
Ihab Awadb78b2762014-07-25 15:16:23 -07001660 }
1661 }
1662 return null;
Ihab Awad104f8062014-07-17 11:29:35 -07001663 }
1664 };
Santos Cordon176ae282014-07-14 02:02:14 -07001665}