blob: dd1fd6e566d94e2555545e6ddff589700f63840e [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;
Evan Charlton105d9772014-11-25 14:08:53 -080026import android.content.pm.UserInfo;
Ihab Awadd9f54382014-10-24 11:44:47 -070027import android.graphics.Bitmap;
28import android.graphics.BitmapFactory;
Evan Charlton105d9772014-11-25 14:08:53 -080029import android.net.Uri;
Sailesh Nepal91fc8092015-02-14 15:44:55 -080030import android.os.Binder;
Evan Charlton105d9772014-11-25 14:08:53 -080031import android.os.Process;
32import android.os.UserHandle;
33import android.os.UserManager;
Tyler Gunn84253572014-09-02 14:50:05 -070034import android.provider.Settings;
Tyler Gunn7cc70b42014-09-12 22:17:27 -070035import android.telecom.ConnectionService;
36import android.telecom.PhoneAccount;
37import android.telecom.PhoneAccountHandle;
Nancy Chen140004a2014-10-15 15:48:38 -070038import android.telephony.PhoneNumberUtils;
Nancy Chen5a36b6e2014-10-23 17:42:42 -070039import android.telephony.SubscriptionManager;
Santos Cordon479b3022015-02-06 04:27:15 -080040import android.telephony.TelephonyManager;
Evan Charltonaf51ceb2014-07-30 11:56:36 -070041import android.text.TextUtils;
Ihab Awadb78b2762014-07-25 15:16:23 -070042import android.util.AtomicFile;
Ihab Awadd9f54382014-10-24 11:44:47 -070043import android.util.Base64;
Ihab Awadb78b2762014-07-25 15:16:23 -070044import android.util.Xml;
Santos Cordon176ae282014-07-14 02:02:14 -070045
Santos Cordon479b3022015-02-06 04:27:15 -080046
Tyler Gunn91d43cf2014-09-17 12:19:39 -070047// TODO: Needed for move to system service: import com.android.internal.R;
Sailesh Nepal0e1dc5a2014-07-30 11:08:54 -070048import com.android.internal.annotations.VisibleForTesting;
49import com.android.internal.util.FastXmlSerializer;
Tyler Gunn9787e0e2014-10-14 14:36:12 -070050import com.android.internal.util.IndentingPrintWriter;
Sailesh Nepal0e1dc5a2014-07-30 11:08:54 -070051import com.android.internal.util.XmlUtils;
52
Evan Charltonaf51ceb2014-07-30 11:56:36 -070053import org.xmlpull.v1.XmlPullParser;
54import org.xmlpull.v1.XmlPullParserException;
55import org.xmlpull.v1.XmlSerializer;
56
Ihab Awadb78b2762014-07-25 15:16:23 -070057import java.io.BufferedInputStream;
58import java.io.BufferedOutputStream;
Ihab Awadd9f54382014-10-24 11:44:47 -070059import java.io.ByteArrayOutputStream;
Ihab Awadb78b2762014-07-25 15:16:23 -070060import java.io.File;
61import java.io.FileNotFoundException;
62import java.io.FileOutputStream;
63import java.io.IOException;
64import java.io.InputStream;
Tyler Gunn84253572014-09-02 14:50:05 -070065import java.lang.Integer;
Tyler Gunncb59b672014-08-20 09:02:11 -070066import java.lang.SecurityException;
Tyler Gunnd900ce62014-08-13 11:40:59 -070067import java.lang.String;
Santos Cordon176ae282014-07-14 02:02:14 -070068import java.util.ArrayList;
Sailesh Nepal91fc8092015-02-14 15:44:55 -080069import java.util.Collections;
Tyler Gunnd900ce62014-08-13 11:40:59 -070070import java.util.Iterator;
Santos Cordon176ae282014-07-14 02:02:14 -070071import java.util.List;
72import java.util.Objects;
Ihab Awadb78b2762014-07-25 15:16:23 -070073import java.util.concurrent.CopyOnWriteArrayList;
Santos Cordon176ae282014-07-14 02:02:14 -070074
75/**
Evan Charlton89176372014-07-19 18:23:09 -070076 * Handles writing and reading PhoneAccountHandle registration entries. This is a simple verbatim
Santos Cordon6a212642015-05-08 16:35:23 -070077 * delegate for all the account handling methods on {@link android.telecom.TelecomManager} as
78 * implemented in {@link TelecomServiceImpl}, with the notable exception that
79 * {@link TelecomServiceImpl} is responsible for security checking to make sure that the caller has
80 * proper authority over the {@code ComponentName}s they are declaring in their
81 * {@code PhoneAccountHandle}s.
82 *
83 *
84 * -- About Users and Phone Accounts --
85 *
86 * When it comes to PhoneAccounts, we store all phone account in a single place,
87 * which means that there are three users that we deal with:
88 * 1) The Android User that is currently active on the device.
89 * 2) The user which owns/registers the phone account.
90 * 3) The user running the app that is requesting the phone account information.
91 *
92 * For example, I have a device with 2 users, primary (A) and secondary (B), and the secondary user
93 * has a work profile running as another user (B2). Lets say that user B opens the phone settings
94 * (not currently supported, but theoretically speaking), and phone settings queries for a phone
95 * account list. Lets also say that an app running in the work profile has registered a phone account.
96 * This means that:
97 *
98 * Since phone settings always runs as the primary user, We have the following situation:
99 * User A (settings) is requesting a list of phone accounts while the active user is User B, and that
100 * list contains a phone account for profile User B2.
101 *
102 * In practice, (2) is stored with the phone account handle and is part of the handle's ID. (1) is
103 * saved in {@link #mCurrentUserHandle} and (3) we get from Binder.getCallingUser(). We check these
104 * users for visibility before returning any phone accounts.
Santos Cordon176ae282014-07-14 02:02:14 -0700105 */
Ihab Awadb78b2762014-07-25 15:16:23 -0700106public final class PhoneAccountRegistrar {
Santos Cordon176ae282014-07-14 02:02:14 -0700107
Yorke Lee5e8836a2014-08-22 15:25:18 -0700108 public static final PhoneAccountHandle NO_ACCOUNT_SELECTED =
109 new PhoneAccountHandle(new ComponentName("null", "null"), "NO_ACCOUNT_SELECTED");
110
Ihab Awadb78b2762014-07-25 15:16:23 -0700111 public abstract static class Listener {
112 public void onAccountsChanged(PhoneAccountRegistrar registrar) {}
113 public void onDefaultOutgoingChanged(PhoneAccountRegistrar registrar) {}
114 public void onSimCallManagerChanged(PhoneAccountRegistrar registrar) {}
115 }
116
117 private static final String FILE_NAME = "phone-account-registrar-state.xml";
Tyler Gunn84253572014-09-02 14:50:05 -0700118 @VisibleForTesting
Ihab Awad7e2c7f32014-11-03 09:49:45 -0800119 public static final int EXPECTED_STATE_VERSION = 5;
Tyler Gunn84253572014-09-02 14:50:05 -0700120
121 /** Keep in sync with the same in SipSettings.java */
122 private static final String SIP_SHARED_PREFERENCES = "SIP_PREFERENCES";
Ihab Awadb78b2762014-07-25 15:16:23 -0700123
124 private final List<Listener> mListeners = new CopyOnWriteArrayList<>();
125 private final AtomicFile mAtomicFile;
Santos Cordonafe59e52014-08-22 16:48:43 -0700126 private final Context mContext;
Evan Charlton105d9772014-11-25 14:08:53 -0800127 private final UserManager mUserManager;
Wink Saville33c05d32014-11-20 13:04:17 -0800128 private final SubscriptionManager mSubscriptionManager;
Ihab Awadb78b2762014-07-25 15:16:23 -0700129 private State mState;
Evan Charlton105d9772014-11-25 14:08:53 -0800130 private UserHandle mCurrentUserHandle;
Santos Cordon176ae282014-07-14 02:02:14 -0700131
Nancy Chen06ce0622014-10-23 01:17:35 +0000132 @VisibleForTesting
Ihab Awad26923222014-07-30 10:54:35 -0700133 public PhoneAccountRegistrar(Context context) {
Ihab Awadb78b2762014-07-25 15:16:23 -0700134 this(context, FILE_NAME);
135 }
136
137 @VisibleForTesting
138 public PhoneAccountRegistrar(Context context, String fileName) {
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700139 // TODO: This file path is subject to change -- it is storing the phone account registry
140 // state file in the path /data/system/users/0/, which is likely not correct in a
141 // multi-user setting.
142 /** UNCOMMENT_FOR_MOVE_TO_SYSTEM_SERVICE
143 String filePath = Environment.getUserSystemDirectory(UserHandle.myUserId()).
144 getAbsolutePath();
145 mAtomicFile = new AtomicFile(new File(filePath, fileName));
146 UNCOMMENT_FOR_MOVE_TO_SYSTEM_SERVICE */
Ihab Awadb78b2762014-07-25 15:16:23 -0700147 mAtomicFile = new AtomicFile(new File(context.getFilesDir(), fileName));
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700148
Ihab Awadb78b2762014-07-25 15:16:23 -0700149 mState = new State();
Santos Cordonafe59e52014-08-22 16:48:43 -0700150 mContext = context;
Evan Charlton105d9772014-11-25 14:08:53 -0800151 mUserManager = UserManager.get(context);
Wink Saville33c05d32014-11-20 13:04:17 -0800152 mSubscriptionManager = SubscriptionManager.from(mContext);
Evan Charlton105d9772014-11-25 14:08:53 -0800153 mCurrentUserHandle = Process.myUserHandle();
Ihab Awadb78b2762014-07-25 15:16:23 -0700154 read();
Santos Cordon176ae282014-07-14 02:02:14 -0700155 }
156
Tyler Gunn84253572014-09-02 14:50:05 -0700157 /**
Nancy Chen140004a2014-10-15 15:48:38 -0700158 * Retrieves the subscription id for a given phone account if it exists. Subscription ids
159 * apply only to PSTN/SIM card phone accounts so all other accounts should not have a
160 * subscription id.
161 * @param accountHandle The handle for the phone account for which to retrieve the
162 * subscription id.
Wink Saville35850602014-10-23 15:57:21 -0700163 * @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 -0700164 */
Wink Saville35850602014-10-23 15:57:21 -0700165 public int getSubscriptionIdForPhoneAccount(PhoneAccountHandle accountHandle) {
Santos Cordon6a212642015-05-08 16:35:23 -0700166 PhoneAccount account = getPhoneAccountCheckCallingUser(accountHandle);
Santos Cordon479b3022015-02-06 04:27:15 -0800167
Santos Cordon6a212642015-05-08 16:35:23 -0700168 if (account != null && account.hasCapabilities(PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION)) {
Santos Cordon479b3022015-02-06 04:27:15 -0800169 TelephonyManager tm =
170 (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE);
171 return tm.getSubIdForPhoneAccount(account);
Nancy Chen140004a2014-10-15 15:48:38 -0700172 }
Santos Cordon479b3022015-02-06 04:27:15 -0800173 return SubscriptionManager.INVALID_SUBSCRIPTION_ID;
Nancy Chen140004a2014-10-15 15:48:38 -0700174 }
175
176 /**
Evan Charlton105d9772014-11-25 14:08:53 -0800177 * Retrieves the default outgoing phone account supporting the specified uriScheme. Note that if
178 * {@link #mCurrentUserHandle} does not have visibility into the current default, {@code null}
179 * will be returned.
180 *
Tyler Gunn84253572014-09-02 14:50:05 -0700181 * @param uriScheme The URI scheme for the outgoing call.
182 * @return The {@link PhoneAccountHandle} to use.
183 */
Santos Cordon6a212642015-05-08 16:35:23 -0700184 public PhoneAccountHandle getOutgoingPhoneAccountForScheme(String uriScheme) {
Yorke Lee5e8836a2014-08-22 15:25:18 -0700185 final PhoneAccountHandle userSelected = getUserSelectedOutgoingPhoneAccount();
Tyler Gunn84253572014-09-02 14:50:05 -0700186
Yorke Lee5e8836a2014-08-22 15:25:18 -0700187 if (userSelected != null) {
Tyler Gunn84253572014-09-02 14:50:05 -0700188 // If there is a default PhoneAccount, ensure it supports calls to handles with the
189 // specified uriScheme.
Santos Cordon6a212642015-05-08 16:35:23 -0700190 final PhoneAccount userSelectedAccount = getPhoneAccountCheckCallingUser(userSelected);
191 if (userSelectedAccount.supportsUriScheme(uriScheme)) {
Tyler Gunn84253572014-09-02 14:50:05 -0700192 return userSelected;
193 }
Ihab Awad293edf22014-07-24 17:52:29 -0700194 }
195
Nancy Chen309198e2014-09-15 18:02:49 -0700196 List<PhoneAccountHandle> outgoing = getCallCapablePhoneAccounts(uriScheme);
Ihab Awad6fb37c82014-08-07 19:48:57 -0700197 switch (outgoing.size()) {
Ihab Awad293edf22014-07-24 17:52:29 -0700198 case 0:
199 // There are no accounts, so there can be no default
200 return null;
201 case 1:
Evan Charlton105d9772014-11-25 14:08:53 -0800202 // There is only one account, which is by definition the default.
Santos Cordon6a212642015-05-08 16:35:23 -0700203 return outgoing.get(0);
Ihab Awad293edf22014-07-24 17:52:29 -0700204 default:
205 // There are multiple accounts with no selected default
206 return null;
Ihab Awadf2a84912014-07-22 21:09:25 -0700207 }
Ihab Awad104f8062014-07-17 11:29:35 -0700208 }
Santos Cordon176ae282014-07-14 02:02:14 -0700209
Evan Charlton105d9772014-11-25 14:08:53 -0800210 /**
211 * @return The user-selected outgoing {@link PhoneAccount}, or null if it hasn't been set (or
212 * if it was set by another user).
213 */
Yorke Lee5e8836a2014-08-22 15:25:18 -0700214 PhoneAccountHandle getUserSelectedOutgoingPhoneAccount() {
Santos Cordon6a212642015-05-08 16:35:23 -0700215 PhoneAccount account = getPhoneAccountCheckCallingUser(mState.defaultOutgoing);
216 if (account != null) {
217 return mState.defaultOutgoing;
Yorke Lee5e8836a2014-08-22 15:25:18 -0700218 }
219 return null;
220 }
221
Santos Cordon6a212642015-05-08 16:35:23 -0700222 /**
223 * Sets the phone account with which to place all calls by default. Set by the user
224 * within phone settings.
225 */
Andrew Leea51a3862014-09-03 14:58:45 -0700226 public void setUserSelectedOutgoingPhoneAccount(PhoneAccountHandle accountHandle) {
Evan Charlton89176372014-07-19 18:23:09 -0700227 if (accountHandle == null) {
Ihab Awad104f8062014-07-17 11:29:35 -0700228 // Asking to clear the default outgoing is a valid request
Ihab Awad293edf22014-07-24 17:52:29 -0700229 mState.defaultOutgoing = null;
Ihab Awad104f8062014-07-17 11:29:35 -0700230 } else {
Santos Cordon6a212642015-05-08 16:35:23 -0700231 // TODO: Do we really want to return for *any* user?
232 PhoneAccount account = getPhoneAccount(accountHandle);
233 if (account == null) {
Ihab Awadb78b2762014-07-25 15:16:23 -0700234 Log.w(this, "Trying to set nonexistent default outgoing %s",
235 accountHandle);
236 return;
237 }
238
Santos Cordon6a212642015-05-08 16:35:23 -0700239 if (!account.hasCapabilities(PhoneAccount.CAPABILITY_CALL_PROVIDER)) {
Ihab Awadb78b2762014-07-25 15:16:23 -0700240 Log.w(this, "Trying to set non-call-provider default outgoing %s",
Evan Charlton89176372014-07-19 18:23:09 -0700241 accountHandle);
Ihab Awad104f8062014-07-17 11:29:35 -0700242 return;
243 }
244
Santos Cordon6a212642015-05-08 16:35:23 -0700245 if (account.hasCapabilities(PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION)) {
Nancy Chen5a36b6e2014-10-23 17:42:42 -0700246 // If the account selected is a SIM account, propagate down to the subscription
247 // record.
Wink Saville7ce6e782014-10-27 10:56:46 -0700248 int subId = getSubscriptionIdForPhoneAccount(accountHandle);
Wink Saville33c05d32014-11-20 13:04:17 -0800249 mSubscriptionManager.setDefaultVoiceSubId(subId);
Nancy Chen5a36b6e2014-10-23 17:42:42 -0700250 }
251
Ihab Awad293edf22014-07-24 17:52:29 -0700252 mState.defaultOutgoing = accountHandle;
Santos Cordon176ae282014-07-14 02:02:14 -0700253 }
254
Ihab Awad293edf22014-07-24 17:52:29 -0700255 write();
Ihab Awadb78b2762014-07-25 15:16:23 -0700256 fireDefaultOutgoingChanged();
Santos Cordon176ae282014-07-14 02:02:14 -0700257 }
258
Nancy Chen668dee02014-11-19 15:31:31 -0800259 boolean isUserSelectedSmsPhoneAccount(PhoneAccountHandle accountHandle) {
260 return getSubscriptionIdForPhoneAccount(accountHandle) ==
261 SubscriptionManager.getDefaultSmsSubId();
262 }
263
Ihab Awad293edf22014-07-24 17:52:29 -0700264 public void setSimCallManager(PhoneAccountHandle callManager) {
265 if (callManager != null) {
Santos Cordon6a212642015-05-08 16:35:23 -0700266 // TODO: Do we really want to return for *any* user?
267 PhoneAccount callManagerAccount = getPhoneAccount(callManager);
Ihab Awad293edf22014-07-24 17:52:29 -0700268 if (callManagerAccount == null) {
269 Log.d(this, "setSimCallManager: Nonexistent call manager: %s", callManager);
270 return;
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700271 } else if (!callManagerAccount.hasCapabilities(
272 PhoneAccount.CAPABILITY_CONNECTION_MANAGER)) {
Ihab Awad293edf22014-07-24 17:52:29 -0700273 Log.d(this, "setSimCallManager: Not a call manager: %s", callManagerAccount);
274 return;
275 }
Yorke Lee5e8836a2014-08-22 15:25:18 -0700276 } else {
277 callManager = NO_ACCOUNT_SELECTED;
Ihab Awad293edf22014-07-24 17:52:29 -0700278 }
279 mState.simCallManager = callManager;
Yorke Lee5e8836a2014-08-22 15:25:18 -0700280
Ihab Awad293edf22014-07-24 17:52:29 -0700281 write();
Ihab Awadb78b2762014-07-25 15:16:23 -0700282 fireSimCallManagerChanged();
Ihab Awad293edf22014-07-24 17:52:29 -0700283 }
284
Evan Charlton105d9772014-11-25 14:08:53 -0800285 /**
286 * @return The {@link PhoneAccount}s which are visible to {@link #mCurrentUserHandle}.
287 */
Ihab Awad293edf22014-07-24 17:52:29 -0700288 public PhoneAccountHandle getSimCallManager() {
Santos Cordon6a212642015-05-08 16:35:23 -0700289 PhoneAccount account = getPhoneAccountCheckCallingUser(mState.simCallManager);
290
291 // Return the registered sim call manager iff it still exists (we keep a sticky
292 // setting to survive account deletion and re-addition)
293 if (account != null && !resolveComponent(mState.simCallManager).isEmpty()) {
294 return mState.simCallManager;
Ihab Awadb78b2762014-07-25 15:16:23 -0700295 }
Evan Charltonaf51ceb2014-07-30 11:56:36 -0700296
Santos Cordon6a212642015-05-08 16:35:23 -0700297 // We have no set call manager, but check to see if the OEM has specified a default one.
Evan Charltonaf51ceb2014-07-30 11:56:36 -0700298 String defaultConnectionMgr =
Santos Cordonafe59e52014-08-22 16:48:43 -0700299 mContext.getResources().getString(R.string.default_connection_manager_component);
Evan Charltonaf51ceb2014-07-30 11:56:36 -0700300 if (!TextUtils.isEmpty(defaultConnectionMgr)) {
Evan Charltonaf51ceb2014-07-30 11:56:36 -0700301 ComponentName componentName = ComponentName.unflattenFromString(defaultConnectionMgr);
Sailesh Nepal91fc8092015-02-14 15:44:55 -0800302 if (componentName == null) {
303 return null;
304 }
305
Evan Charltonaf51ceb2014-07-30 11:56:36 -0700306 // Make sure that the component can be resolved.
Evan Charlton105d9772014-11-25 14:08:53 -0800307 List<ResolveInfo> resolveInfos = resolveComponent(componentName, null);
Sailesh Nepal91fc8092015-02-14 15:44:55 -0800308 if (resolveInfos.isEmpty()) {
309 resolveInfos = resolveComponent(componentName, Binder.getCallingUserHandle());
310 }
311
Evan Charltonaf51ceb2014-07-30 11:56:36 -0700312 if (!resolveInfos.isEmpty()) {
313 // See if there is registered PhoneAccount by this component.
314 List<PhoneAccountHandle> handles = getAllPhoneAccountHandles();
315 for (PhoneAccountHandle handle : handles) {
Santos Cordon6a212642015-05-08 16:35:23 -0700316 if (componentName.equals(handle.getComponentName())) {
Evan Charltonaf51ceb2014-07-30 11:56:36 -0700317 return handle;
318 }
319 }
320 Log.d(this, "%s does not have a PhoneAccount; not using as default", componentName);
321 } else {
322 Log.d(this, "%s could not be resolved; not using as default", componentName);
323 }
324 } else {
325 Log.v(this, "No default connection manager specified");
326 }
327
Ihab Awadb78b2762014-07-25 15:16:23 -0700328 return null;
Ihab Awad293edf22014-07-24 17:52:29 -0700329 }
330
Evan Charlton105d9772014-11-25 14:08:53 -0800331 /**
Evan Charlton105d9772014-11-25 14:08:53 -0800332 * Update the current UserHandle to track when users are switched. This will allow the
333 * PhoneAccountRegistar to self-filter the PhoneAccounts to make sure we don't leak anything
334 * across users.
Santos Cordon6a212642015-05-08 16:35:23 -0700335 * We cannot simply check the calling user because that would always return the primary user for
336 * all invocations originating with the system process.
Evan Charlton105d9772014-11-25 14:08:53 -0800337 *
338 * @param userHandle The {@link UserHandle}, as delivered by
339 * {@link Intent#ACTION_USER_SWITCHED}.
340 */
341 public void setCurrentUserHandle(UserHandle userHandle) {
342 if (userHandle == null) {
343 Log.d(this, "setCurrentUserHandle, userHandle = null");
344 userHandle = Process.myUserHandle();
345 }
346 Log.d(this, "setCurrentUserHandle, %s", userHandle);
347 mCurrentUserHandle = userHandle;
348 }
349
Evan Charlton105d9772014-11-25 14:08:53 -0800350 private boolean isVisibleForUser(PhoneAccount account) {
351 if (account == null) {
352 return false;
353 }
354
355 // If this PhoneAccount has CAPABILITY_MULTI_USER, it should be visible to all users and
356 // all profiles. Only Telephony and SIP accounts should have this capability.
357 if (account.hasCapabilities(PhoneAccount.CAPABILITY_MULTI_USER)) {
358 return true;
359 }
360
361 UserHandle phoneAccountUserHandle = account.getAccountHandle().getUserHandle();
362 if (phoneAccountUserHandle == null) {
363 return false;
364 }
365
366 if (mCurrentUserHandle == null) {
367 Log.d(this, "Current user is null; assuming true");
368 return true;
369 }
370
Sailesh Nepal91fc8092015-02-14 15:44:55 -0800371 if (phoneAccountUserHandle.equals(Binder.getCallingUserHandle())) {
372 return true;
373 }
374
Santos Cordon6a212642015-05-08 16:35:23 -0700375 // Special check for work profiles.
Evan Charlton105d9772014-11-25 14:08:53 -0800376 // Unlike in TelecomServiceImpl, we only care about *profiles* here. We want to make sure
377 // that we don't resolve PhoneAccount across *users*, but resolving across *profiles* is
378 // fine.
Sailesh Nepal91fc8092015-02-14 15:44:55 -0800379 if (UserHandle.getCallingUserId() == UserHandle.USER_OWNER) {
380 List<UserInfo> profileUsers =
381 mUserManager.getProfiles(mCurrentUserHandle.getIdentifier());
382 for (UserInfo profileInfo : profileUsers) {
383 if (profileInfo.getUserHandle().equals(phoneAccountUserHandle)) {
384 return true;
385 }
Evan Charlton105d9772014-11-25 14:08:53 -0800386 }
387 }
Sailesh Nepal91fc8092015-02-14 15:44:55 -0800388
Evan Charlton105d9772014-11-25 14:08:53 -0800389 return false;
390 }
391
392 private List<ResolveInfo> resolveComponent(PhoneAccountHandle phoneAccountHandle) {
393 return resolveComponent(phoneAccountHandle.getComponentName(),
394 phoneAccountHandle.getUserHandle());
395 }
396
397 private List<ResolveInfo> resolveComponent(ComponentName componentName,
398 UserHandle userHandle) {
mike dooley10a58312014-11-06 13:46:19 -0800399 PackageManager pm = mContext.getPackageManager();
400 Intent intent = new Intent(ConnectionService.SERVICE_INTERFACE);
401 intent.setComponent(componentName);
Sailesh Nepal91fc8092015-02-14 15:44:55 -0800402 try {
403 if (userHandle != null) {
404 return pm.queryIntentServicesAsUser(intent, 0, userHandle.getIdentifier());
405 } else {
406 return pm.queryIntentServices(intent, 0);
407 }
408 } catch (SecurityException e) {
409 Log.v(this, "%s is not visible for the calling user", componentName);
410 return Collections.EMPTY_LIST;
Evan Charlton105d9772014-11-25 14:08:53 -0800411 }
mike dooley10a58312014-11-06 13:46:19 -0800412 }
413
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700414 /**
415 * Retrieves a list of all {@link PhoneAccountHandle}s registered.
416 *
417 * @return The list of {@link PhoneAccountHandle}s.
418 */
Ihab Awad293edf22014-07-24 17:52:29 -0700419 public List<PhoneAccountHandle> getAllPhoneAccountHandles() {
Santos Cordon6a212642015-05-08 16:35:23 -0700420 return getPhoneAccountHandles(0, null, null);
Ihab Awad293edf22014-07-24 17:52:29 -0700421 }
422
423 public List<PhoneAccount> getAllPhoneAccounts() {
Santos Cordon6a212642015-05-08 16:35:23 -0700424 return getPhoneAccounts(0, null, null);
Santos Cordonafe59e52014-08-22 16:48:43 -0700425 }
426
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700427 /**
Nancy Chen309198e2014-09-15 18:02:49 -0700428 * Retrieves a list of all phone account call provider phone accounts supporting the
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700429 * specified URI scheme.
430 *
431 * @param uriScheme The URI scheme.
432 * @return The phone account handles.
433 */
Nancy Chen309198e2014-09-15 18:02:49 -0700434 public List<PhoneAccountHandle> getCallCapablePhoneAccounts(String uriScheme) {
Santos Cordon6a212642015-05-08 16:35:23 -0700435 return getPhoneAccountHandles(PhoneAccount.CAPABILITY_CALL_PROVIDER, uriScheme, null);
436 }
437
438 /**
439 * Retrieves a list of all the SIM-based phone accounts.
440 */
441 public List<PhoneAccountHandle> getSimPhoneAccounts() {
442 return getPhoneAccountHandles(
443 PhoneAccount.CAPABILITY_CALL_PROVIDER | PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION,
444 null, null);
Tyler Gunn84253572014-09-02 14:50:05 -0700445 }
446
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700447 /**
Nancy Chen1c5926f2014-09-17 14:44:14 -0700448 * Retrieves a list of all phone accounts registered by a specified package.
449 *
450 * @param packageName The name of the package that registered the phone accounts.
451 * @return The phone account handles.
452 */
453 public List<PhoneAccountHandle> getPhoneAccountsForPackage(String packageName) {
Santos Cordon6a212642015-05-08 16:35:23 -0700454 return getPhoneAccountHandles(0, null, packageName);
Nancy Chen1c5926f2014-09-17 14:44:14 -0700455 }
456
457 /**
Nancy Chen309198e2014-09-15 18:02:49 -0700458 * Retrieves a list of all phone account handles with the connection manager capability.
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700459 *
460 * @return The phone account handles.
461 */
462 public List<PhoneAccountHandle> getConnectionManagerPhoneAccounts() {
Santos Cordon6a212642015-05-08 16:35:23 -0700463 return getPhoneAccountHandles(PhoneAccount.CAPABILITY_CONNECTION_MANAGER, null, null);
Santos Cordon176ae282014-07-14 02:02:14 -0700464 }
465
Ihab Awad104f8062014-07-17 11:29:35 -0700466 // TODO: Should we implement an artificial limit for # of accounts associated with a single
467 // ComponentName?
Ihab Awad293edf22014-07-24 17:52:29 -0700468 public void registerPhoneAccount(PhoneAccount account) {
Tyler Gunncb59b672014-08-20 09:02:11 -0700469 // Enforce the requirement that a connection service for a phone account has the correct
470 // permission.
Santos Cordon6a212642015-05-08 16:35:23 -0700471 if (!phoneAccountRequiresBindPermission(account.getAccountHandle())) {
Tyler Gunncb59b672014-08-20 09:02:11 -0700472 Log.w(this, "Phone account %s does not have BIND_CONNECTION_SERVICE permission.",
473 account.getAccountHandle());
474 throw new SecurityException(
475 "PhoneAccount connection service requires BIND_CONNECTION_SERVICE permission.");
476 }
477
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700478 addOrReplacePhoneAccount(account);
479 }
480
481 /**
482 * Adds a {@code PhoneAccount}, replacing an existing one if found.
483 *
484 * @param account The {@code PhoneAccount} to add or replace.
485 */
486 private void addOrReplacePhoneAccount(PhoneAccount account) {
Ihab Awad7e2c7f32014-11-03 09:49:45 -0800487 Log.d(this, "addOrReplacePhoneAccount(%s -> %s)",
488 account.getAccountHandle(), account);
489
Santos Cordon6a212642015-05-08 16:35:23 -0700490 PhoneAccount oldAccount = getPhoneAccount(account.getAccountHandle());
491 if (oldAccount != null) {
492 mState.accounts.remove(oldAccount);
Santos Cordon176ae282014-07-14 02:02:14 -0700493 }
Santos Cordon6a212642015-05-08 16:35:23 -0700494 mState.accounts.add(account);
Santos Cordon176ae282014-07-14 02:02:14 -0700495
Ihab Awad293edf22014-07-24 17:52:29 -0700496 write();
Ihab Awadb78b2762014-07-25 15:16:23 -0700497 fireAccountsChanged();
Ihab Awad293edf22014-07-24 17:52:29 -0700498 }
499
Evan Charlton89176372014-07-19 18:23:09 -0700500 public void unregisterPhoneAccount(PhoneAccountHandle accountHandle) {
Santos Cordon6a212642015-05-08 16:35:23 -0700501 PhoneAccount account = getPhoneAccount(accountHandle);
502 if (account != null) {
503 if (mState.accounts.remove(account)) {
504 write();
505 fireAccountsChanged();
Ihab Awad104f8062014-07-17 11:29:35 -0700506 }
507 }
Santos Cordon176ae282014-07-14 02:02:14 -0700508 }
509
Tyler Gunnd900ce62014-08-13 11:40:59 -0700510 /**
511 * Un-registers all phone accounts associated with a specified package.
512 *
513 * @param packageName The package for which phone accounts will be removed.
Evan Charlton105d9772014-11-25 14:08:53 -0800514 * @param userHandle The {@link UserHandle} the package is running under.
Tyler Gunnd900ce62014-08-13 11:40:59 -0700515 */
Evan Charlton105d9772014-11-25 14:08:53 -0800516 public void clearAccounts(String packageName, UserHandle userHandle) {
Tyler Gunnd900ce62014-08-13 11:40:59 -0700517 boolean accountsRemoved = false;
518 Iterator<PhoneAccount> it = mState.accounts.iterator();
519 while (it.hasNext()) {
520 PhoneAccount phoneAccount = it.next();
Evan Charlton105d9772014-11-25 14:08:53 -0800521 PhoneAccountHandle handle = phoneAccount.getAccountHandle();
522 if (Objects.equals(packageName, handle.getComponentName().getPackageName())
523 && Objects.equals(userHandle, handle.getUserHandle())) {
Tyler Gunnd900ce62014-08-13 11:40:59 -0700524 Log.i(this, "Removing phone account " + phoneAccount.getLabel());
525 it.remove();
526 accountsRemoved = true;
Ihab Awad104f8062014-07-17 11:29:35 -0700527 }
528 }
529
Tyler Gunnd900ce62014-08-13 11:40:59 -0700530 if (accountsRemoved) {
531 write();
532 fireAccountsChanged();
533 }
Ihab Awadb78b2762014-07-25 15:16:23 -0700534 }
535
Nancy Chen140004a2014-10-15 15:48:38 -0700536 public boolean isVoiceMailNumber(PhoneAccountHandle accountHandle, String number) {
Wink Saville35850602014-10-23 15:57:21 -0700537 int subId = getSubscriptionIdForPhoneAccount(accountHandle);
Nancy Chen140004a2014-10-15 15:48:38 -0700538 return PhoneNumberUtils.isVoiceMailNumber(subId, number);
539 }
540
Ihab Awadb78b2762014-07-25 15:16:23 -0700541 public void addListener(Listener l) {
542 mListeners.add(l);
543 }
544
545 public void removeListener(Listener l) {
Jay Shraunera82c8f72014-08-14 15:49:16 -0700546 if (l != null) {
547 mListeners.remove(l);
548 }
Ihab Awadb78b2762014-07-25 15:16:23 -0700549 }
550
551 private void fireAccountsChanged() {
552 for (Listener l : mListeners) {
553 l.onAccountsChanged(this);
554 }
555 }
556
557 private void fireDefaultOutgoingChanged() {
558 for (Listener l : mListeners) {
559 l.onDefaultOutgoingChanged(this);
560 }
561 }
562
563 private void fireSimCallManagerChanged() {
564 for (Listener l : mListeners) {
565 l.onSimCallManagerChanged(this);
566 }
Santos Cordon176ae282014-07-14 02:02:14 -0700567 }
568
Tyler Gunncb59b672014-08-20 09:02:11 -0700569 /**
Santos Cordon6a212642015-05-08 16:35:23 -0700570 * Determines if the connection service specified by a {@link PhoneAccountHandle} requires the
Tyler Gunncb59b672014-08-20 09:02:11 -0700571 * {@link Manifest.permission#BIND_CONNECTION_SERVICE} permission.
572 *
573 * @param phoneAccountHandle The phone account to check.
574 * @return {@code True} if the phone account has permission.
575 */
Santos Cordon6a212642015-05-08 16:35:23 -0700576 public boolean phoneAccountRequiresBindPermission(PhoneAccountHandle phoneAccountHandle) {
Sailesh Nepal91fc8092015-02-14 15:44:55 -0800577 List<ResolveInfo> resolveInfos = resolveComponent(phoneAccountHandle);
578 if (resolveInfos.isEmpty()) {
579 Log.w(this, "phoneAccount %s not found", phoneAccountHandle.getComponentName());
Tyler Gunncb59b672014-08-20 09:02:11 -0700580 return false;
581 }
Sailesh Nepal91fc8092015-02-14 15:44:55 -0800582 for (ResolveInfo resolveInfo : resolveInfos) {
583 ServiceInfo serviceInfo = resolveInfo.serviceInfo;
Santos Cordon6a212642015-05-08 16:35:23 -0700584 if (serviceInfo == null ||
585 !Manifest.permission.BIND_CONNECTION_SERVICE.equals(serviceInfo.permission)) {
Sailesh Nepal91fc8092015-02-14 15:44:55 -0800586 return false;
587 }
588 }
589 return true;
Tyler Gunncb59b672014-08-20 09:02:11 -0700590 }
591
Santos Cordon6a212642015-05-08 16:35:23 -0700592 //
593 // Methods for retrieving PhoneAccounts and PhoneAccountHandles
594 //
Ihab Awad293edf22014-07-24 17:52:29 -0700595
Santos Cordonafe59e52014-08-22 16:48:43 -0700596 /**
Santos Cordon6a212642015-05-08 16:35:23 -0700597 * Returns the PhoneAccount for the specified handle. Does no user checking.
Tyler Gunn84253572014-09-02 14:50:05 -0700598 *
Santos Cordon6a212642015-05-08 16:35:23 -0700599 * @param handle
600 * @return The corresponding phone account if one exists.
Santos Cordonafe59e52014-08-22 16:48:43 -0700601 */
Santos Cordon6a212642015-05-08 16:35:23 -0700602 PhoneAccount getPhoneAccount(PhoneAccountHandle handle) {
603 for (PhoneAccount m : mState.accounts) {
604 if (Objects.equals(handle, m.getAccountHandle())) {
605 return m;
606 }
607 }
608 return null;
609 }
610
611 /**
612 * Like getPhoneAccount, but checks to see if the current user is allowed to see the phone
613 * account before returning it. The current user is the active user on the actual android
614 * device.
615 */
616 public PhoneAccount getPhoneAccountCheckCallingUser(PhoneAccountHandle handle) {
617 PhoneAccount account = getPhoneAccount(handle);
618 if (account != null && isVisibleForUser(account)) {
619 return account;
620 }
621 return null;
622 }
623
624 /**
625 * Returns a list of phone account handles with the specified capabilities, uri scheme,
626 * and package name.
627 */
628 private List<PhoneAccountHandle> getPhoneAccountHandles(
629 int capabilities, String uriScheme, String packageName) {
630 List<PhoneAccountHandle> handles = new ArrayList<>();
631 for (PhoneAccount account : getPhoneAccounts(capabilities, uriScheme, packageName)) {
632 handles.add(account.getAccountHandle());
633 }
634 return handles;
Tyler Gunn84253572014-09-02 14:50:05 -0700635 }
636
637 /**
638 * Returns a list of phone account handles with the specified flag, supporting the specified
Santos Cordon6a212642015-05-08 16:35:23 -0700639 * URI scheme, within the specified package name.
Tyler Gunn84253572014-09-02 14:50:05 -0700640 *
Santos Cordon6a212642015-05-08 16:35:23 -0700641 * @param capabilities Capabilities which the {@code PhoneAccount} must have. Ignored if 0.
642 * @param uriScheme URI schemes the PhoneAccount must handle. {@code null} bypasses the
Tyler Gunn84253572014-09-02 14:50:05 -0700643 * URI scheme check.
Santos Cordon6a212642015-05-08 16:35:23 -0700644 * @param packageName Package name of the PhoneAccount. {@code null} bypasses packageName check.
Tyler Gunn84253572014-09-02 14:50:05 -0700645 */
Santos Cordon6a212642015-05-08 16:35:23 -0700646 private List<PhoneAccount> getPhoneAccounts(
647 int capabilities, String uriScheme, String packageName) {
648 List<PhoneAccount> accounts = new ArrayList<>(mState.accounts.size());
Ihab Awad293edf22014-07-24 17:52:29 -0700649 for (PhoneAccount m : mState.accounts) {
Santos Cordon6a212642015-05-08 16:35:23 -0700650 if (capabilities != 0 && !m.hasCapabilities(capabilities)) {
Evan Charlton105d9772014-11-25 14:08:53 -0800651 // Account doesn't have the right capabilities; skip this one.
652 continue;
Ihab Awadf2a84912014-07-22 21:09:25 -0700653 }
Evan Charlton105d9772014-11-25 14:08:53 -0800654 if (uriScheme != null && !m.supportsUriScheme(uriScheme)) {
655 // Account doesn't support this URI scheme; skip this one.
656 continue;
657 }
Santos Cordon6a212642015-05-08 16:35:23 -0700658 PhoneAccountHandle handle = m.getAccountHandle();
659
660 if (resolveComponent(handle).isEmpty()) {
Evan Charlton105d9772014-11-25 14:08:53 -0800661 // This component cannot be resolved anymore; skip this one.
662 continue;
663 }
Santos Cordon6a212642015-05-08 16:35:23 -0700664 if (packageName != null &&
665 !packageName.equals(handle.getComponentName().getPackageName())) {
666 // Not the right package name; skip this one.
667 continue;
668 }
Evan Charlton105d9772014-11-25 14:08:53 -0800669 if (!isVisibleForUser(m)) {
670 // Account is not visible for the current user; skip this one.
671 continue;
672 }
Santos Cordon6a212642015-05-08 16:35:23 -0700673 accounts.add(m);
Ihab Awad104f8062014-07-17 11:29:35 -0700674 }
Santos Cordon6a212642015-05-08 16:35:23 -0700675 return accounts;
Ihab Awad104f8062014-07-17 11:29:35 -0700676 }
677
Santos Cordon6a212642015-05-08 16:35:23 -0700678 //
679 // State Implementation for PhoneAccountRegistrar
680 //
681
Ihab Awad293edf22014-07-24 17:52:29 -0700682 /**
683 * The state of this {@code PhoneAccountRegistrar}.
684 */
Ihab Awadb78b2762014-07-25 15:16:23 -0700685 @VisibleForTesting
686 public static class State {
Ihab Awad293edf22014-07-24 17:52:29 -0700687 /**
688 * The account selected by the user to be employed by default for making outgoing calls.
689 * If the user has not made such a selection, then this is null.
690 */
691 public PhoneAccountHandle defaultOutgoing = null;
692
693 /**
Ihab Awadb78b2762014-07-25 15:16:23 -0700694 * A {@code PhoneAccount} having {@link PhoneAccount#CAPABILITY_CONNECTION_MANAGER} which
Ihab Awad293edf22014-07-24 17:52:29 -0700695 * manages and optimizes a user's PSTN SIM connections.
696 */
697 public PhoneAccountHandle simCallManager;
698
699 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700700 * The complete list of {@code PhoneAccount}s known to the Telecom subsystem.
Ihab Awad293edf22014-07-24 17:52:29 -0700701 */
702 public final List<PhoneAccount> accounts = new ArrayList<>();
Tyler Gunn84253572014-09-02 14:50:05 -0700703
704 /**
705 * The version number of the State data.
706 */
707 public int versionNumber;
Ihab Awad293edf22014-07-24 17:52:29 -0700708 }
709
Tyler Gunn9787e0e2014-10-14 14:36:12 -0700710 /**
711 * Dumps the state of the {@link CallsManager}.
712 *
713 * @param pw The {@code IndentingPrintWriter} to write the state to.
714 */
715 public void dump(IndentingPrintWriter pw) {
716 if (mState != null) {
717 pw.println("xmlVersion: " + mState.versionNumber);
718 pw.println("defaultOutgoing: " + (mState.defaultOutgoing == null ? "none" :
719 mState.defaultOutgoing));
720 pw.println("simCallManager: " + (mState.simCallManager == null ? "none" :
721 mState.simCallManager));
722 pw.println("phoneAccounts:");
723 pw.increaseIndent();
724 for (PhoneAccount phoneAccount : mState.accounts) {
725 pw.println(phoneAccount);
726 }
727 pw.decreaseIndent();
728 }
729 }
730
Ihab Awad293edf22014-07-24 17:52:29 -0700731 ////////////////////////////////////////////////////////////////////////////////////////////////
732 //
733 // State management
734 //
735
736 private void write() {
Ihab Awadb78b2762014-07-25 15:16:23 -0700737 final FileOutputStream os;
Ihab Awad104f8062014-07-17 11:29:35 -0700738 try {
Ihab Awadb78b2762014-07-25 15:16:23 -0700739 os = mAtomicFile.startWrite();
740 boolean success = false;
741 try {
742 XmlSerializer serializer = new FastXmlSerializer();
743 serializer.setOutput(new BufferedOutputStream(os), "utf-8");
Evan Charlton105d9772014-11-25 14:08:53 -0800744 writeToXml(mState, serializer, mContext);
Ihab Awadb78b2762014-07-25 15:16:23 -0700745 serializer.flush();
746 success = true;
747 } finally {
748 if (success) {
749 mAtomicFile.finishWrite(os);
750 } else {
751 mAtomicFile.failWrite(os);
752 }
753 }
754 } catch (IOException e) {
755 Log.e(this, e, "Writing state to XML file");
Ihab Awad104f8062014-07-17 11:29:35 -0700756 }
757 }
758
Ihab Awadb78b2762014-07-25 15:16:23 -0700759 private void read() {
760 final InputStream is;
Ihab Awad104f8062014-07-17 11:29:35 -0700761 try {
Ihab Awadb78b2762014-07-25 15:16:23 -0700762 is = mAtomicFile.openRead();
763 } catch (FileNotFoundException ex) {
764 return;
765 }
766
Tyler Gunn84253572014-09-02 14:50:05 -0700767 boolean versionChanged = false;
768
Ihab Awadb78b2762014-07-25 15:16:23 -0700769 XmlPullParser parser;
770 try {
771 parser = Xml.newPullParser();
772 parser.setInput(new BufferedInputStream(is), null);
773 parser.nextTag();
Tyler Gunn84253572014-09-02 14:50:05 -0700774 mState = readFromXml(parser, mContext);
775 versionChanged = mState.versionNumber < EXPECTED_STATE_VERSION;
776
Ihab Awadb78b2762014-07-25 15:16:23 -0700777 } catch (IOException | XmlPullParserException e) {
778 Log.e(this, e, "Reading state from XML file");
779 mState = new State();
780 } finally {
781 try {
782 is.close();
783 } catch (IOException e) {
784 Log.e(this, e, "Closing InputStream");
785 }
Ihab Awad104f8062014-07-17 11:29:35 -0700786 }
Tyler Gunn84253572014-09-02 14:50:05 -0700787
Evan Charlton105d9772014-11-25 14:08:53 -0800788 // Verify all of the UserHandles.
789 List<PhoneAccount> badAccounts = new ArrayList<>();
790 for (PhoneAccount phoneAccount : mState.accounts) {
791 UserHandle userHandle = phoneAccount.getAccountHandle().getUserHandle();
792 if (userHandle == null) {
793 Log.w(this, "Missing UserHandle for %s", phoneAccount);
794 badAccounts.add(phoneAccount);
795 } else if (mUserManager.getSerialNumberForUser(userHandle) == -1) {
796 Log.w(this, "User does not exist for %s", phoneAccount);
797 badAccounts.add(phoneAccount);
798 }
799 }
800 mState.accounts.removeAll(badAccounts);
801
Tyler Gunn84253572014-09-02 14:50:05 -0700802 // If an upgrade occurred, write out the changed data.
Evan Charlton105d9772014-11-25 14:08:53 -0800803 if (versionChanged || !badAccounts.isEmpty()) {
Tyler Gunn84253572014-09-02 14:50:05 -0700804 write();
805 }
Santos Cordon176ae282014-07-14 02:02:14 -0700806 }
807
Evan Charlton105d9772014-11-25 14:08:53 -0800808 private static void writeToXml(State state, XmlSerializer serializer, Context context)
Ihab Awadb78b2762014-07-25 15:16:23 -0700809 throws IOException {
Evan Charlton105d9772014-11-25 14:08:53 -0800810 sStateXml.writeToXml(state, serializer, context);
Santos Cordon176ae282014-07-14 02:02:14 -0700811 }
Ihab Awad104f8062014-07-17 11:29:35 -0700812
Tyler Gunn84253572014-09-02 14:50:05 -0700813 private static State readFromXml(XmlPullParser parser, Context context)
Ihab Awadb78b2762014-07-25 15:16:23 -0700814 throws IOException, XmlPullParserException {
Tyler Gunn84253572014-09-02 14:50:05 -0700815 State s = sStateXml.readFromXml(parser, 0, context);
Ihab Awadb78b2762014-07-25 15:16:23 -0700816 return s != null ? s : new State();
Ihab Awad104f8062014-07-17 11:29:35 -0700817 }
818
Ihab Awad293edf22014-07-24 17:52:29 -0700819 ////////////////////////////////////////////////////////////////////////////////////////////////
Ihab Awad104f8062014-07-17 11:29:35 -0700820 //
Ihab Awadb78b2762014-07-25 15:16:23 -0700821 // XML serialization
Ihab Awad104f8062014-07-17 11:29:35 -0700822 //
823
Ihab Awadb78b2762014-07-25 15:16:23 -0700824 @VisibleForTesting
Ihab Awad26923222014-07-30 10:54:35 -0700825 public abstract static class XmlSerialization<T> {
Tyler Gunn84253572014-09-02 14:50:05 -0700826 private static final String LENGTH_ATTRIBUTE = "length";
827 private static final String VALUE_TAG = "value";
828
Ihab Awadb78b2762014-07-25 15:16:23 -0700829 /**
830 * Write the supplied object to XML
831 */
Evan Charlton105d9772014-11-25 14:08:53 -0800832 public abstract void writeToXml(T o, XmlSerializer serializer, Context context)
Ihab Awad26923222014-07-30 10:54:35 -0700833 throws IOException;
Ihab Awadb78b2762014-07-25 15:16:23 -0700834
835 /**
836 * Read from the supplied XML into a new object, returning null in case of an
837 * unrecoverable schema mismatch or other data error. 'parser' must be already
838 * positioned at the first tag that is expected to have been emitted by this
839 * object's writeToXml(). This object tries to fail early without modifying
840 * 'parser' if it does not recognize the data it sees.
841 */
Tyler Gunn84253572014-09-02 14:50:05 -0700842 public abstract T readFromXml(XmlPullParser parser, int version, Context context)
Ihab Awad26923222014-07-30 10:54:35 -0700843 throws IOException, XmlPullParserException;
844
Ihab Awadd9f54382014-10-24 11:44:47 -0700845 protected void writeTextIfNonNull(String tagName, Object value, XmlSerializer serializer)
Ihab Awad26923222014-07-30 10:54:35 -0700846 throws IOException {
847 if (value != null) {
848 serializer.startTag(null, tagName);
849 serializer.text(Objects.toString(value));
850 serializer.endTag(null, tagName);
851 }
852 }
Tyler Gunn84253572014-09-02 14:50:05 -0700853
854 /**
855 * Serializes a string array.
856 *
857 * @param tagName The tag name for the string array.
858 * @param values The string values to serialize.
859 * @param serializer The serializer.
860 * @throws IOException
861 */
862 protected void writeStringList(String tagName, List<String> values,
863 XmlSerializer serializer)
864 throws IOException {
865
866 serializer.startTag(null, tagName);
867 if (values != null) {
868 serializer.attribute(null, LENGTH_ATTRIBUTE, Objects.toString(values.size()));
869 for (String toSerialize : values) {
870 serializer.startTag(null, VALUE_TAG);
871 if (toSerialize != null ){
872 serializer.text(toSerialize);
873 }
Tyler Gunn84253572014-09-02 14:50:05 -0700874 serializer.endTag(null, VALUE_TAG);
875 }
876 } else {
877 serializer.attribute(null, LENGTH_ATTRIBUTE, "0");
878 }
879 serializer.endTag(null, tagName);
Ihab Awadd9f54382014-10-24 11:44:47 -0700880 }
Tyler Gunn84253572014-09-02 14:50:05 -0700881
Ihab Awadd9f54382014-10-24 11:44:47 -0700882 protected void writeBitmapIfNonNull(String tagName, Bitmap value, XmlSerializer serializer)
883 throws IOException {
884 if (value != null && value.getByteCount() > 0) {
885 ByteArrayOutputStream stream = new ByteArrayOutputStream();
886 value.compress(Bitmap.CompressFormat.PNG, 100, stream);
887 byte[] imageByteArray = stream.toByteArray();
888 String text = Base64.encodeToString(imageByteArray, 0, imageByteArray.length, 0);
889
890 serializer.startTag(null, tagName);
891 serializer.text(text);
892 serializer.endTag(null, tagName);
893 }
Tyler Gunn84253572014-09-02 14:50:05 -0700894 }
895
Evan Charlton105d9772014-11-25 14:08:53 -0800896 protected void writeLong(String tagName, long value, XmlSerializer serializer)
897 throws IOException {
898 serializer.startTag(null, tagName);
899 serializer.text(Long.valueOf(value).toString());
900 serializer.endTag(null, tagName);
901 }
902
Tyler Gunn84253572014-09-02 14:50:05 -0700903 /**
904 * Reads a string array from the XML parser.
905 *
906 * @param parser The XML parser.
907 * @return String array containing the parsed values.
908 * @throws IOException Exception related to IO.
909 * @throws XmlPullParserException Exception related to parsing.
910 */
911 protected List<String> readStringList(XmlPullParser parser)
912 throws IOException, XmlPullParserException {
913
914 int length = Integer.parseInt(parser.getAttributeValue(null, LENGTH_ATTRIBUTE));
915 List<String> arrayEntries = new ArrayList<String>(length);
916 String value = null;
917
918 if (length == 0) {
919 return arrayEntries;
920 }
921
922 int outerDepth = parser.getDepth();
923 while (XmlUtils.nextElementWithin(parser, outerDepth)) {
924 if (parser.getName().equals(VALUE_TAG)) {
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700925 parser.next();
Tyler Gunn84253572014-09-02 14:50:05 -0700926 value = parser.getText();
927 arrayEntries.add(value);
928 }
929 }
930
931 return arrayEntries;
932 }
Ihab Awadd9f54382014-10-24 11:44:47 -0700933
934 protected Bitmap readBitmap(XmlPullParser parser)
935 throws IOException, XmlPullParserException {
936 byte[] imageByteArray = Base64.decode(parser.getText(), 0);
937 return BitmapFactory.decodeByteArray(imageByteArray, 0, imageByteArray.length);
938 }
Ihab Awad104f8062014-07-17 11:29:35 -0700939 }
940
Ihab Awadb78b2762014-07-25 15:16:23 -0700941 @VisibleForTesting
942 public static final XmlSerialization<State> sStateXml =
943 new XmlSerialization<State>() {
944 private static final String CLASS_STATE = "phone_account_registrar_state";
Ihab Awad104f8062014-07-17 11:29:35 -0700945 private static final String DEFAULT_OUTGOING = "default_outgoing";
Ihab Awad293edf22014-07-24 17:52:29 -0700946 private static final String SIM_CALL_MANAGER = "sim_call_manager";
Ihab Awad104f8062014-07-17 11:29:35 -0700947 private static final String ACCOUNTS = "accounts";
Tyler Gunn84253572014-09-02 14:50:05 -0700948 private static final String VERSION = "version";
Ihab Awad104f8062014-07-17 11:29:35 -0700949
950 @Override
Evan Charlton105d9772014-11-25 14:08:53 -0800951 public void writeToXml(State o, XmlSerializer serializer, Context context)
Ihab Awadb78b2762014-07-25 15:16:23 -0700952 throws IOException {
Ihab Awad26923222014-07-30 10:54:35 -0700953 if (o != null) {
954 serializer.startTag(null, CLASS_STATE);
Tyler Gunn84253572014-09-02 14:50:05 -0700955 serializer.attribute(null, VERSION, Objects.toString(EXPECTED_STATE_VERSION));
Ihab Awadb78b2762014-07-25 15:16:23 -0700956
Ihab Awad26923222014-07-30 10:54:35 -0700957 if (o.defaultOutgoing != null) {
958 serializer.startTag(null, DEFAULT_OUTGOING);
Evan Charlton105d9772014-11-25 14:08:53 -0800959 sPhoneAccountHandleXml.writeToXml(o.defaultOutgoing, serializer, context);
Ihab Awad26923222014-07-30 10:54:35 -0700960 serializer.endTag(null, DEFAULT_OUTGOING);
961 }
962
963 if (o.simCallManager != null) {
964 serializer.startTag(null, SIM_CALL_MANAGER);
Evan Charlton105d9772014-11-25 14:08:53 -0800965 sPhoneAccountHandleXml.writeToXml(o.simCallManager, serializer, context);
Ihab Awad26923222014-07-30 10:54:35 -0700966 serializer.endTag(null, SIM_CALL_MANAGER);
967 }
968
969 serializer.startTag(null, ACCOUNTS);
970 for (PhoneAccount m : o.accounts) {
Evan Charlton105d9772014-11-25 14:08:53 -0800971 sPhoneAccountXml.writeToXml(m, serializer, context);
Ihab Awad26923222014-07-30 10:54:35 -0700972 }
973 serializer.endTag(null, ACCOUNTS);
974
975 serializer.endTag(null, CLASS_STATE);
Ihab Awad293edf22014-07-24 17:52:29 -0700976 }
Ihab Awad104f8062014-07-17 11:29:35 -0700977 }
978
979 @Override
Tyler Gunn84253572014-09-02 14:50:05 -0700980 public State readFromXml(XmlPullParser parser, int version, Context context)
Ihab Awadb78b2762014-07-25 15:16:23 -0700981 throws IOException, XmlPullParserException {
982 if (parser.getName().equals(CLASS_STATE)) {
983 State s = new State();
Tyler Gunn84253572014-09-02 14:50:05 -0700984
985 String rawVersion = parser.getAttributeValue(null, VERSION);
986 s.versionNumber = TextUtils.isEmpty(rawVersion) ? 1 :
987 Integer.parseInt(rawVersion);
988
Ihab Awadb78b2762014-07-25 15:16:23 -0700989 int outerDepth = parser.getDepth();
990 while (XmlUtils.nextElementWithin(parser, outerDepth)) {
991 if (parser.getName().equals(DEFAULT_OUTGOING)) {
992 parser.nextTag();
Tyler Gunn84253572014-09-02 14:50:05 -0700993 s.defaultOutgoing = sPhoneAccountHandleXml.readFromXml(parser,
994 s.versionNumber, context);
Ihab Awadb78b2762014-07-25 15:16:23 -0700995 } else if (parser.getName().equals(SIM_CALL_MANAGER)) {
996 parser.nextTag();
Tyler Gunn84253572014-09-02 14:50:05 -0700997 s.simCallManager = sPhoneAccountHandleXml.readFromXml(parser,
998 s.versionNumber, context);
Evan Charlton105d9772014-11-25 14:08:53 -0800999 if (s.simCallManager.getUserHandle() == null) {
1000 // This should never happen, but handle the upgrade case.
1001 s.simCallManager = new PhoneAccountHandle(
1002 s.simCallManager.getComponentName(),
1003 s.simCallManager.getId(),
1004 Process.myUserHandle());
1005 }
Ihab Awadb78b2762014-07-25 15:16:23 -07001006 } else if (parser.getName().equals(ACCOUNTS)) {
1007 int accountsDepth = parser.getDepth();
1008 while (XmlUtils.nextElementWithin(parser, accountsDepth)) {
Tyler Gunn84253572014-09-02 14:50:05 -07001009 PhoneAccount account = sPhoneAccountXml.readFromXml(parser,
1010 s.versionNumber, context);
1011
1012 if (account != null && s.accounts != null) {
Ihab Awadb78b2762014-07-25 15:16:23 -07001013 s.accounts.add(account);
1014 }
1015 }
Ihab Awad104f8062014-07-17 11:29:35 -07001016 }
1017 }
Ihab Awadb78b2762014-07-25 15:16:23 -07001018 return s;
Ihab Awad104f8062014-07-17 11:29:35 -07001019 }
Ihab Awadb78b2762014-07-25 15:16:23 -07001020 return null;
Ihab Awad104f8062014-07-17 11:29:35 -07001021 }
1022 };
1023
Ihab Awadb78b2762014-07-25 15:16:23 -07001024 @VisibleForTesting
1025 public static final XmlSerialization<PhoneAccount> sPhoneAccountXml =
1026 new XmlSerialization<PhoneAccount>() {
1027 private static final String CLASS_PHONE_ACCOUNT = "phone_account";
1028 private static final String ACCOUNT_HANDLE = "account_handle";
Andrew Lee7129f1c2014-09-04 11:55:07 -07001029 private static final String ADDRESS = "handle";
1030 private static final String SUBSCRIPTION_ADDRESS = "subscription_number";
Ihab Awad104f8062014-07-17 11:29:35 -07001031 private static final String CAPABILITIES = "capabilities";
1032 private static final String ICON_RES_ID = "icon_res_id";
Ihab Awadd9f54382014-10-24 11:44:47 -07001033 private static final String ICON_PACKAGE_NAME = "icon_package_name";
1034 private static final String ICON_BITMAP = "icon_bitmap";
Ihab Awad7e2c7f32014-11-03 09:49:45 -08001035 private static final String ICON_TINT = "icon_tint";
1036 private static final String HIGHLIGHT_COLOR = "highlight_color";
Ihab Awad104f8062014-07-17 11:29:35 -07001037 private static final String LABEL = "label";
1038 private static final String SHORT_DESCRIPTION = "short_description";
Tyler Gunn84253572014-09-02 14:50:05 -07001039 private static final String SUPPORTED_URI_SCHEMES = "supported_uri_schemes";
Ihab Awad104f8062014-07-17 11:29:35 -07001040
1041 @Override
Evan Charlton105d9772014-11-25 14:08:53 -08001042 public void writeToXml(PhoneAccount o, XmlSerializer serializer, Context context)
Ihab Awadb78b2762014-07-25 15:16:23 -07001043 throws IOException {
Ihab Awad26923222014-07-30 10:54:35 -07001044 if (o != null) {
1045 serializer.startTag(null, CLASS_PHONE_ACCOUNT);
Ihab Awadb78b2762014-07-25 15:16:23 -07001046
Ihab Awad26923222014-07-30 10:54:35 -07001047 if (o.getAccountHandle() != null) {
1048 serializer.startTag(null, ACCOUNT_HANDLE);
Evan Charlton105d9772014-11-25 14:08:53 -08001049 sPhoneAccountHandleXml.writeToXml(o.getAccountHandle(), serializer, context);
Ihab Awad26923222014-07-30 10:54:35 -07001050 serializer.endTag(null, ACCOUNT_HANDLE);
1051 }
Ihab Awadb78b2762014-07-25 15:16:23 -07001052
Ihab Awadd9f54382014-10-24 11:44:47 -07001053 writeTextIfNonNull(ADDRESS, o.getAddress(), serializer);
1054 writeTextIfNonNull(SUBSCRIPTION_ADDRESS, o.getSubscriptionAddress(), serializer);
1055 writeTextIfNonNull(CAPABILITIES, Integer.toString(o.getCapabilities()), serializer);
1056 writeTextIfNonNull(ICON_RES_ID, Integer.toString(o.getIconResId()), serializer);
1057 writeTextIfNonNull(ICON_PACKAGE_NAME, o.getIconPackageName(), serializer);
1058 writeBitmapIfNonNull(ICON_BITMAP, o.getIconBitmap(), serializer);
Ihab Awad7e2c7f32014-11-03 09:49:45 -08001059 writeTextIfNonNull(ICON_TINT, Integer.toString(o.getIconTint()), serializer);
1060 writeTextIfNonNull(HIGHLIGHT_COLOR,
1061 Integer.toString(o.getHighlightColor()), serializer);
Ihab Awadd9f54382014-10-24 11:44:47 -07001062 writeTextIfNonNull(LABEL, o.getLabel(), serializer);
1063 writeTextIfNonNull(SHORT_DESCRIPTION, o.getShortDescription(), serializer);
Tyler Gunn84253572014-09-02 14:50:05 -07001064 writeStringList(SUPPORTED_URI_SCHEMES, o.getSupportedUriSchemes(), serializer);
Ihab Awadb78b2762014-07-25 15:16:23 -07001065
Ihab Awad26923222014-07-30 10:54:35 -07001066 serializer.endTag(null, CLASS_PHONE_ACCOUNT);
1067 }
Ihab Awad104f8062014-07-17 11:29:35 -07001068 }
1069
Tyler Gunn84253572014-09-02 14:50:05 -07001070 public PhoneAccount readFromXml(XmlPullParser parser, int version, Context context)
Ihab Awadb78b2762014-07-25 15:16:23 -07001071 throws IOException, XmlPullParserException {
1072 if (parser.getName().equals(CLASS_PHONE_ACCOUNT)) {
1073 int outerDepth = parser.getDepth();
1074 PhoneAccountHandle accountHandle = null;
Andrew Lee7129f1c2014-09-04 11:55:07 -07001075 Uri address = null;
1076 Uri subscriptionAddress = null;
Ihab Awadb78b2762014-07-25 15:16:23 -07001077 int capabilities = 0;
Ihab Awad7e2c7f32014-11-03 09:49:45 -08001078 int iconResId = PhoneAccount.NO_RESOURCE_ID;
Ihab Awadd9f54382014-10-24 11:44:47 -07001079 String iconPackageName = null;
Ihab Awad7e2c7f32014-11-03 09:49:45 -08001080 Bitmap iconBitmap = null;
Ihab Awad07bc5ee2014-11-12 13:42:52 -08001081 int iconTint = PhoneAccount.NO_ICON_TINT;
1082 int highlightColor = PhoneAccount.NO_HIGHLIGHT_COLOR;
Ihab Awadb78b2762014-07-25 15:16:23 -07001083 String label = null;
1084 String shortDescription = null;
Tyler Gunn84253572014-09-02 14:50:05 -07001085 List<String> supportedUriSchemes = null;
Ihab Awadb78b2762014-07-25 15:16:23 -07001086
1087 while (XmlUtils.nextElementWithin(parser, outerDepth)) {
1088 if (parser.getName().equals(ACCOUNT_HANDLE)) {
1089 parser.nextTag();
Tyler Gunn84253572014-09-02 14:50:05 -07001090 accountHandle = sPhoneAccountHandleXml.readFromXml(parser, version,
1091 context);
Andrew Lee7129f1c2014-09-04 11:55:07 -07001092 } else if (parser.getName().equals(ADDRESS)) {
Ihab Awadb78b2762014-07-25 15:16:23 -07001093 parser.next();
Andrew Lee7129f1c2014-09-04 11:55:07 -07001094 address = Uri.parse(parser.getText());
1095 } else if (parser.getName().equals(SUBSCRIPTION_ADDRESS)) {
Ihab Awadb78b2762014-07-25 15:16:23 -07001096 parser.next();
Andrew Lee7129f1c2014-09-04 11:55:07 -07001097 String nextText = parser.getText();
1098 subscriptionAddress = nextText == null ? null : Uri.parse(nextText);
Ihab Awadb78b2762014-07-25 15:16:23 -07001099 } else if (parser.getName().equals(CAPABILITIES)) {
1100 parser.next();
1101 capabilities = Integer.parseInt(parser.getText());
1102 } else if (parser.getName().equals(ICON_RES_ID)) {
1103 parser.next();
1104 iconResId = Integer.parseInt(parser.getText());
Ihab Awadd9f54382014-10-24 11:44:47 -07001105 } else if (parser.getName().equals(ICON_PACKAGE_NAME)) {
1106 parser.next();
1107 iconPackageName = parser.getText();
1108 } else if (parser.getName().equals(ICON_BITMAP)) {
1109 parser.next();
Ihab Awad7e2c7f32014-11-03 09:49:45 -08001110 iconBitmap = readBitmap(parser);
1111 } else if (parser.getName().equals(ICON_TINT)) {
Nancy Chen06ce0622014-10-23 01:17:35 +00001112 parser.next();
Ihab Awad7e2c7f32014-11-03 09:49:45 -08001113 iconTint = Integer.parseInt(parser.getText());
1114 } else if (parser.getName().equals(HIGHLIGHT_COLOR)) {
1115 parser.next();
1116 highlightColor = Integer.parseInt(parser.getText());
Ihab Awadb78b2762014-07-25 15:16:23 -07001117 } else if (parser.getName().equals(LABEL)) {
1118 parser.next();
1119 label = parser.getText();
1120 } else if (parser.getName().equals(SHORT_DESCRIPTION)) {
1121 parser.next();
1122 shortDescription = parser.getText();
Tyler Gunn84253572014-09-02 14:50:05 -07001123 } else if (parser.getName().equals(SUPPORTED_URI_SCHEMES)) {
1124 supportedUriSchemes = readStringList(parser);
Ihab Awadb78b2762014-07-25 15:16:23 -07001125 }
1126 }
Tyler Gunn84253572014-09-02 14:50:05 -07001127
1128 // Upgrade older phone accounts to specify the supported URI schemes.
1129 if (version < 2) {
1130 ComponentName sipComponentName = new ComponentName("com.android.phone",
1131 "com.android.services.telephony.sip.SipConnectionService");
1132
1133 supportedUriSchemes = new ArrayList<>();
1134
1135 // Handle the SIP connection service.
1136 // Check the system settings to see if it also should handle "tel" calls.
1137 if (accountHandle.getComponentName().equals(sipComponentName)) {
1138 boolean useSipForPstn = useSipForPstnCalls(context);
1139 supportedUriSchemes.add(PhoneAccount.SCHEME_SIP);
1140 if (useSipForPstn) {
1141 supportedUriSchemes.add(PhoneAccount.SCHEME_TEL);
1142 }
1143 } else {
1144 supportedUriSchemes.add(PhoneAccount.SCHEME_TEL);
1145 supportedUriSchemes.add(PhoneAccount.SCHEME_VOICEMAIL);
1146 }
1147 }
1148
Ihab Awad7e2c7f32014-11-03 09:49:45 -08001149 // Upgrade older phone accounts with explicit package name
1150 if (version < 5) {
1151 if (iconBitmap == null) {
1152 iconPackageName = accountHandle.getComponentName().getPackageName();
1153 }
1154 }
1155
1156 PhoneAccount.Builder builder = PhoneAccount.builder(accountHandle, label)
Andrew Lee7129f1c2014-09-04 11:55:07 -07001157 .setAddress(address)
1158 .setSubscriptionAddress(subscriptionAddress)
1159 .setCapabilities(capabilities)
Andrew Lee7129f1c2014-09-04 11:55:07 -07001160 .setShortDescription(shortDescription)
1161 .setSupportedUriSchemes(supportedUriSchemes)
Ihab Awad7e2c7f32014-11-03 09:49:45 -08001162 .setHighlightColor(highlightColor);
1163
1164 if (iconBitmap == null) {
1165 builder.setIcon(iconPackageName, iconResId, iconTint);
1166 } else {
1167 builder.setIcon(iconBitmap);
1168 }
1169
1170 return builder.build();
Ihab Awadb78b2762014-07-25 15:16:23 -07001171 }
1172 return null;
Ihab Awad104f8062014-07-17 11:29:35 -07001173 }
Tyler Gunn84253572014-09-02 14:50:05 -07001174
1175 /**
1176 * Determines if the SIP call settings specify to use SIP for all calls, including PSTN calls.
1177 *
1178 * @param context The context.
1179 * @return {@code True} if SIP should be used for all calls.
1180 */
1181 private boolean useSipForPstnCalls(Context context) {
1182 String option = Settings.System.getString(context.getContentResolver(),
1183 Settings.System.SIP_CALL_OPTIONS);
1184 option = (option != null) ? option : Settings.System.SIP_ADDRESS_ONLY;
1185 return option.equals(Settings.System.SIP_ALWAYS);
1186 }
Ihab Awad104f8062014-07-17 11:29:35 -07001187 };
1188
Ihab Awadb78b2762014-07-25 15:16:23 -07001189 @VisibleForTesting
1190 public static final XmlSerialization<PhoneAccountHandle> sPhoneAccountHandleXml =
1191 new XmlSerialization<PhoneAccountHandle>() {
1192 private static final String CLASS_PHONE_ACCOUNT_HANDLE = "phone_account_handle";
Ihab Awad104f8062014-07-17 11:29:35 -07001193 private static final String COMPONENT_NAME = "component_name";
1194 private static final String ID = "id";
Evan Charlton105d9772014-11-25 14:08:53 -08001195 private static final String USER_SERIAL_NUMBER = "user_serial_number";
Ihab Awad104f8062014-07-17 11:29:35 -07001196
1197 @Override
Evan Charlton105d9772014-11-25 14:08:53 -08001198 public void writeToXml(PhoneAccountHandle o, XmlSerializer serializer, Context context)
Ihab Awadb78b2762014-07-25 15:16:23 -07001199 throws IOException {
Ihab Awad26923222014-07-30 10:54:35 -07001200 if (o != null) {
1201 serializer.startTag(null, CLASS_PHONE_ACCOUNT_HANDLE);
Ihab Awadb78b2762014-07-25 15:16:23 -07001202
Ihab Awad26923222014-07-30 10:54:35 -07001203 if (o.getComponentName() != null) {
Ihab Awadd9f54382014-10-24 11:44:47 -07001204 writeTextIfNonNull(
Ihab Awad26923222014-07-30 10:54:35 -07001205 COMPONENT_NAME, o.getComponentName().flattenToString(), serializer);
1206 }
Ihab Awadb78b2762014-07-25 15:16:23 -07001207
Ihab Awadd9f54382014-10-24 11:44:47 -07001208 writeTextIfNonNull(ID, o.getId(), serializer);
Ihab Awadb78b2762014-07-25 15:16:23 -07001209
Evan Charlton105d9772014-11-25 14:08:53 -08001210 if (o.getUserHandle() != null && context != null) {
1211 UserManager userManager = UserManager.get(context);
1212 writeLong(USER_SERIAL_NUMBER,
1213 userManager.getSerialNumberForUser(o.getUserHandle()), serializer);
1214 }
1215
Ihab Awad26923222014-07-30 10:54:35 -07001216 serializer.endTag(null, CLASS_PHONE_ACCOUNT_HANDLE);
1217 }
Ihab Awad104f8062014-07-17 11:29:35 -07001218 }
1219
1220 @Override
Tyler Gunn84253572014-09-02 14:50:05 -07001221 public PhoneAccountHandle readFromXml(XmlPullParser parser, int version, Context context)
Ihab Awadb78b2762014-07-25 15:16:23 -07001222 throws IOException, XmlPullParserException {
1223 if (parser.getName().equals(CLASS_PHONE_ACCOUNT_HANDLE)) {
1224 String componentNameString = null;
1225 String idString = null;
Evan Charlton105d9772014-11-25 14:08:53 -08001226 String userSerialNumberString = null;
Ihab Awadb78b2762014-07-25 15:16:23 -07001227 int outerDepth = parser.getDepth();
Evan Charlton105d9772014-11-25 14:08:53 -08001228
1229 UserManager userManager = UserManager.get(context);
1230
Ihab Awadb78b2762014-07-25 15:16:23 -07001231 while (XmlUtils.nextElementWithin(parser, outerDepth)) {
1232 if (parser.getName().equals(COMPONENT_NAME)) {
1233 parser.next();
1234 componentNameString = parser.getText();
1235 } else if (parser.getName().equals(ID)) {
1236 parser.next();
1237 idString = parser.getText();
Evan Charlton105d9772014-11-25 14:08:53 -08001238 } else if (parser.getName().equals(USER_SERIAL_NUMBER)) {
1239 parser.next();
1240 userSerialNumberString = parser.getText();
Ihab Awadb78b2762014-07-25 15:16:23 -07001241 }
1242 }
Ihab Awad26923222014-07-30 10:54:35 -07001243 if (componentNameString != null) {
Evan Charlton105d9772014-11-25 14:08:53 -08001244 UserHandle userHandle = null;
1245 if (userSerialNumberString != null) {
1246 try {
1247 long serialNumber = Long.parseLong(userSerialNumberString);
1248 userHandle = userManager.getUserForSerialNumber(serialNumber);
1249 } catch (NumberFormatException e) {
1250 Log.e(this, e, "Could not parse UserHandle " + userSerialNumberString);
1251 }
1252 }
Ihab Awadb78b2762014-07-25 15:16:23 -07001253 return new PhoneAccountHandle(
1254 ComponentName.unflattenFromString(componentNameString),
Evan Charlton105d9772014-11-25 14:08:53 -08001255 idString,
1256 userHandle);
Ihab Awadb78b2762014-07-25 15:16:23 -07001257 }
1258 }
1259 return null;
Ihab Awad104f8062014-07-17 11:29:35 -07001260 }
1261 };
Santos Cordon176ae282014-07-14 02:02:14 -07001262}