blob: 759a31dc6f9755bc0ffd18fb1672e993ee5fcdb1 [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
Tyler Gunn7cc70b42014-09-12 22:17:27 -070077 * delegate for all the account handling methods on {@link android.telecom.TelecomManager} as implemented in
78 * {@link TelecomServiceImpl}, with the notable exception that {@link TelecomServiceImpl} is
Ihab Awad104f8062014-07-17 11:29:35 -070079 * responsible for security checking to make sure that the caller has proper authority over
Evan Charlton89176372014-07-19 18:23:09 -070080 * the {@code ComponentName}s they are declaring in their {@code PhoneAccountHandle}s.
Santos Cordon176ae282014-07-14 02:02:14 -070081 */
Ihab Awadb78b2762014-07-25 15:16:23 -070082public final class PhoneAccountRegistrar {
Santos Cordon176ae282014-07-14 02:02:14 -070083
Yorke Lee5e8836a2014-08-22 15:25:18 -070084 public static final PhoneAccountHandle NO_ACCOUNT_SELECTED =
85 new PhoneAccountHandle(new ComponentName("null", "null"), "NO_ACCOUNT_SELECTED");
86
Ihab Awadb78b2762014-07-25 15:16:23 -070087 public abstract static class Listener {
88 public void onAccountsChanged(PhoneAccountRegistrar registrar) {}
89 public void onDefaultOutgoingChanged(PhoneAccountRegistrar registrar) {}
90 public void onSimCallManagerChanged(PhoneAccountRegistrar registrar) {}
91 }
92
93 private static final String FILE_NAME = "phone-account-registrar-state.xml";
Tyler Gunn84253572014-09-02 14:50:05 -070094 @VisibleForTesting
Ihab Awad7e2c7f32014-11-03 09:49:45 -080095 public static final int EXPECTED_STATE_VERSION = 5;
Tyler Gunn84253572014-09-02 14:50:05 -070096
97 /** Keep in sync with the same in SipSettings.java */
98 private static final String SIP_SHARED_PREFERENCES = "SIP_PREFERENCES";
Ihab Awadb78b2762014-07-25 15:16:23 -070099
100 private final List<Listener> mListeners = new CopyOnWriteArrayList<>();
101 private final AtomicFile mAtomicFile;
Santos Cordonafe59e52014-08-22 16:48:43 -0700102 private final Context mContext;
Evan Charlton105d9772014-11-25 14:08:53 -0800103 private final UserManager mUserManager;
Wink Saville33c05d32014-11-20 13:04:17 -0800104 private final SubscriptionManager mSubscriptionManager;
Ihab Awadb78b2762014-07-25 15:16:23 -0700105 private State mState;
Evan Charlton105d9772014-11-25 14:08:53 -0800106 private UserHandle mCurrentUserHandle;
Santos Cordon176ae282014-07-14 02:02:14 -0700107
Nancy Chen06ce0622014-10-23 01:17:35 +0000108 @VisibleForTesting
Ihab Awad26923222014-07-30 10:54:35 -0700109 public PhoneAccountRegistrar(Context context) {
Ihab Awadb78b2762014-07-25 15:16:23 -0700110 this(context, FILE_NAME);
111 }
112
113 @VisibleForTesting
114 public PhoneAccountRegistrar(Context context, String fileName) {
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700115 // TODO: This file path is subject to change -- it is storing the phone account registry
116 // state file in the path /data/system/users/0/, which is likely not correct in a
117 // multi-user setting.
118 /** UNCOMMENT_FOR_MOVE_TO_SYSTEM_SERVICE
119 String filePath = Environment.getUserSystemDirectory(UserHandle.myUserId()).
120 getAbsolutePath();
121 mAtomicFile = new AtomicFile(new File(filePath, fileName));
122 UNCOMMENT_FOR_MOVE_TO_SYSTEM_SERVICE */
Ihab Awadb78b2762014-07-25 15:16:23 -0700123 mAtomicFile = new AtomicFile(new File(context.getFilesDir(), fileName));
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700124
Ihab Awadb78b2762014-07-25 15:16:23 -0700125 mState = new State();
Santos Cordonafe59e52014-08-22 16:48:43 -0700126 mContext = context;
Evan Charlton105d9772014-11-25 14:08:53 -0800127 mUserManager = UserManager.get(context);
Wink Saville33c05d32014-11-20 13:04:17 -0800128 mSubscriptionManager = SubscriptionManager.from(mContext);
Evan Charlton105d9772014-11-25 14:08:53 -0800129 mCurrentUserHandle = Process.myUserHandle();
Ihab Awadb78b2762014-07-25 15:16:23 -0700130 read();
Santos Cordon176ae282014-07-14 02:02:14 -0700131 }
132
Tyler Gunn84253572014-09-02 14:50:05 -0700133 /**
Nancy Chen140004a2014-10-15 15:48:38 -0700134 * Retrieves the subscription id for a given phone account if it exists. Subscription ids
135 * apply only to PSTN/SIM card phone accounts so all other accounts should not have a
136 * subscription id.
137 * @param accountHandle The handle for the phone account for which to retrieve the
138 * subscription id.
Wink Saville35850602014-10-23 15:57:21 -0700139 * @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 -0700140 */
Wink Saville35850602014-10-23 15:57:21 -0700141 public int getSubscriptionIdForPhoneAccount(PhoneAccountHandle accountHandle) {
Evan Charlton105d9772014-11-25 14:08:53 -0800142 PhoneAccount account = getPhoneAccountInternal(accountHandle);
Santos Cordon479b3022015-02-06 04:27:15 -0800143
144 if (account != null
145 && account.hasCapabilities(PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION)
146 && isVisibleForUser(accountHandle)) {
147 TelephonyManager tm =
148 (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE);
149 return tm.getSubIdForPhoneAccount(account);
Nancy Chen140004a2014-10-15 15:48:38 -0700150 }
Santos Cordon479b3022015-02-06 04:27:15 -0800151 return SubscriptionManager.INVALID_SUBSCRIPTION_ID;
Nancy Chen140004a2014-10-15 15:48:38 -0700152 }
153
154 /**
Evan Charlton105d9772014-11-25 14:08:53 -0800155 * Retrieves the default outgoing phone account supporting the specified uriScheme. Note that if
156 * {@link #mCurrentUserHandle} does not have visibility into the current default, {@code null}
157 * will be returned.
158 *
Tyler Gunn84253572014-09-02 14:50:05 -0700159 * @param uriScheme The URI scheme for the outgoing call.
160 * @return The {@link PhoneAccountHandle} to use.
161 */
162 public PhoneAccountHandle getDefaultOutgoingPhoneAccount(String uriScheme) {
Yorke Lee5e8836a2014-08-22 15:25:18 -0700163 final PhoneAccountHandle userSelected = getUserSelectedOutgoingPhoneAccount();
Tyler Gunn84253572014-09-02 14:50:05 -0700164
Yorke Lee5e8836a2014-08-22 15:25:18 -0700165 if (userSelected != null) {
Tyler Gunn84253572014-09-02 14:50:05 -0700166 // If there is a default PhoneAccount, ensure it supports calls to handles with the
167 // specified uriScheme.
Evan Charlton105d9772014-11-25 14:08:53 -0800168 final PhoneAccount userSelectedAccount = getPhoneAccountInternal(userSelected);
169 if (userSelectedAccount.supportsUriScheme(uriScheme)
170 && isVisibleForUser(userSelected)) {
Tyler Gunn84253572014-09-02 14:50:05 -0700171 return userSelected;
172 }
Ihab Awad293edf22014-07-24 17:52:29 -0700173 }
174
Nancy Chen309198e2014-09-15 18:02:49 -0700175 List<PhoneAccountHandle> outgoing = getCallCapablePhoneAccounts(uriScheme);
Ihab Awad6fb37c82014-08-07 19:48:57 -0700176 switch (outgoing.size()) {
Ihab Awad293edf22014-07-24 17:52:29 -0700177 case 0:
178 // There are no accounts, so there can be no default
179 return null;
180 case 1:
Evan Charlton105d9772014-11-25 14:08:53 -0800181 // There is only one account, which is by definition the default.
182 PhoneAccountHandle onlyHandle = outgoing.get(0);
183 if (isVisibleForUser(onlyHandle)) {
184 return outgoing.get(0);
185 }
186 return null;
Ihab Awad293edf22014-07-24 17:52:29 -0700187 default:
188 // There are multiple accounts with no selected default
189 return null;
Ihab Awadf2a84912014-07-22 21:09:25 -0700190 }
Ihab Awad104f8062014-07-17 11:29:35 -0700191 }
Santos Cordon176ae282014-07-14 02:02:14 -0700192
Evan Charlton105d9772014-11-25 14:08:53 -0800193 /**
194 * @return The user-selected outgoing {@link PhoneAccount}, or null if it hasn't been set (or
195 * if it was set by another user).
196 */
Yorke Lee5e8836a2014-08-22 15:25:18 -0700197 PhoneAccountHandle getUserSelectedOutgoingPhoneAccount() {
198 if (mState.defaultOutgoing != null) {
199 // Return the registered outgoing default iff it still exists (we keep a sticky
200 // default to survive account deletion and re-addition)
201 for (int i = 0; i < mState.accounts.size(); i++) {
Evan Charlton105d9772014-11-25 14:08:53 -0800202 if (mState.accounts.get(i).getAccountHandle().equals(mState.defaultOutgoing)
203 && isVisibleForUser(mState.defaultOutgoing)) {
Yorke Lee5e8836a2014-08-22 15:25:18 -0700204 return mState.defaultOutgoing;
205 }
206 }
207 // At this point, there was a registered default but it has been deleted; proceed
208 // as though there were no default
209 }
210 return null;
211 }
212
Andrew Leea51a3862014-09-03 14:58:45 -0700213 public void setUserSelectedOutgoingPhoneAccount(PhoneAccountHandle accountHandle) {
Evan Charlton89176372014-07-19 18:23:09 -0700214 if (accountHandle == null) {
Ihab Awad104f8062014-07-17 11:29:35 -0700215 // Asking to clear the default outgoing is a valid request
Ihab Awad293edf22014-07-24 17:52:29 -0700216 mState.defaultOutgoing = null;
Ihab Awad104f8062014-07-17 11:29:35 -0700217 } else {
218 boolean found = false;
Ihab Awad293edf22014-07-24 17:52:29 -0700219 for (PhoneAccount m : mState.accounts) {
Evan Charlton94d01622014-07-20 12:32:05 -0700220 if (Objects.equals(accountHandle, m.getAccountHandle())) {
Ihab Awad104f8062014-07-17 11:29:35 -0700221 found = true;
222 break;
223 }
Santos Cordon176ae282014-07-14 02:02:14 -0700224 }
Ihab Awad104f8062014-07-17 11:29:35 -0700225
226 if (!found) {
Ihab Awadb78b2762014-07-25 15:16:23 -0700227 Log.w(this, "Trying to set nonexistent default outgoing %s",
228 accountHandle);
229 return;
230 }
231
Evan Charlton105d9772014-11-25 14:08:53 -0800232 if (!getPhoneAccountInternal(accountHandle).hasCapabilities(
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700233 PhoneAccount.CAPABILITY_CALL_PROVIDER)) {
Ihab Awadb78b2762014-07-25 15:16:23 -0700234 Log.w(this, "Trying to set non-call-provider default outgoing %s",
Evan Charlton89176372014-07-19 18:23:09 -0700235 accountHandle);
Ihab Awad104f8062014-07-17 11:29:35 -0700236 return;
237 }
238
Evan Charlton105d9772014-11-25 14:08:53 -0800239 if (getPhoneAccountInternal(accountHandle).hasCapabilities(
Nancy Chen5a36b6e2014-10-23 17:42:42 -0700240 PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION)) {
241 // If the account selected is a SIM account, propagate down to the subscription
242 // record.
Wink Saville7ce6e782014-10-27 10:56:46 -0700243 int subId = getSubscriptionIdForPhoneAccount(accountHandle);
Wink Saville33c05d32014-11-20 13:04:17 -0800244 mSubscriptionManager.setDefaultVoiceSubId(subId);
Nancy Chen5a36b6e2014-10-23 17:42:42 -0700245 }
246
Ihab Awad293edf22014-07-24 17:52:29 -0700247 mState.defaultOutgoing = accountHandle;
Santos Cordon176ae282014-07-14 02:02:14 -0700248 }
249
Ihab Awad293edf22014-07-24 17:52:29 -0700250 write();
Ihab Awadb78b2762014-07-25 15:16:23 -0700251 fireDefaultOutgoingChanged();
Santos Cordon176ae282014-07-14 02:02:14 -0700252 }
253
Nancy Chen668dee02014-11-19 15:31:31 -0800254 boolean isUserSelectedSmsPhoneAccount(PhoneAccountHandle accountHandle) {
255 return getSubscriptionIdForPhoneAccount(accountHandle) ==
256 SubscriptionManager.getDefaultSmsSubId();
257 }
258
Ihab Awad293edf22014-07-24 17:52:29 -0700259 public void setSimCallManager(PhoneAccountHandle callManager) {
260 if (callManager != null) {
Evan Charlton105d9772014-11-25 14:08:53 -0800261 PhoneAccount callManagerAccount = getPhoneAccountInternal(callManager);
Ihab Awad293edf22014-07-24 17:52:29 -0700262 if (callManagerAccount == null) {
263 Log.d(this, "setSimCallManager: Nonexistent call manager: %s", callManager);
264 return;
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700265 } else if (!callManagerAccount.hasCapabilities(
266 PhoneAccount.CAPABILITY_CONNECTION_MANAGER)) {
Ihab Awad293edf22014-07-24 17:52:29 -0700267 Log.d(this, "setSimCallManager: Not a call manager: %s", callManagerAccount);
268 return;
269 }
Yorke Lee5e8836a2014-08-22 15:25:18 -0700270 } else {
271 callManager = NO_ACCOUNT_SELECTED;
Ihab Awad293edf22014-07-24 17:52:29 -0700272 }
273 mState.simCallManager = callManager;
Yorke Lee5e8836a2014-08-22 15:25:18 -0700274
Ihab Awad293edf22014-07-24 17:52:29 -0700275 write();
Ihab Awadb78b2762014-07-25 15:16:23 -0700276 fireSimCallManagerChanged();
Ihab Awad293edf22014-07-24 17:52:29 -0700277 }
278
Evan Charlton105d9772014-11-25 14:08:53 -0800279 /**
280 * @return The {@link PhoneAccount}s which are visible to {@link #mCurrentUserHandle}.
281 */
Ihab Awad293edf22014-07-24 17:52:29 -0700282 public PhoneAccountHandle getSimCallManager() {
Ihab Awadb78b2762014-07-25 15:16:23 -0700283 if (mState.simCallManager != null) {
Yorke Lee5e8836a2014-08-22 15:25:18 -0700284 if (NO_ACCOUNT_SELECTED.equals(mState.simCallManager)) {
285 return null;
286 }
Ihab Awadb78b2762014-07-25 15:16:23 -0700287 // Return the registered sim call manager iff it still exists (we keep a sticky
288 // setting to survive account deletion and re-addition)
289 for (int i = 0; i < mState.accounts.size(); i++) {
mike dooley10a58312014-11-06 13:46:19 -0800290 if (mState.accounts.get(i).getAccountHandle().equals(mState.simCallManager)
Evan Charlton105d9772014-11-25 14:08:53 -0800291 && !resolveComponent(mState.simCallManager).isEmpty()
292 && isVisibleForUser(mState.simCallManager)) {
Ihab Awadb78b2762014-07-25 15:16:23 -0700293 return mState.simCallManager;
294 }
295 }
296 }
Evan Charltonaf51ceb2014-07-30 11:56:36 -0700297
298 // See if the OEM has specified a default one.
Evan Charltonaf51ceb2014-07-30 11:56:36 -0700299 String defaultConnectionMgr =
Santos Cordonafe59e52014-08-22 16:48:43 -0700300 mContext.getResources().getString(R.string.default_connection_manager_component);
Evan Charltonaf51ceb2014-07-30 11:56:36 -0700301 if (!TextUtils.isEmpty(defaultConnectionMgr)) {
Evan Charltonaf51ceb2014-07-30 11:56:36 -0700302 ComponentName componentName = ComponentName.unflattenFromString(defaultConnectionMgr);
Sailesh Nepal91fc8092015-02-14 15:44:55 -0800303 if (componentName == null) {
304 return null;
305 }
306
Evan Charltonaf51ceb2014-07-30 11:56:36 -0700307 // Make sure that the component can be resolved.
Evan Charlton105d9772014-11-25 14:08:53 -0800308 List<ResolveInfo> resolveInfos = resolveComponent(componentName, null);
Sailesh Nepal91fc8092015-02-14 15:44:55 -0800309 if (resolveInfos.isEmpty()) {
310 resolveInfos = resolveComponent(componentName, Binder.getCallingUserHandle());
311 }
312
Evan Charltonaf51ceb2014-07-30 11:56:36 -0700313 if (!resolveInfos.isEmpty()) {
314 // See if there is registered PhoneAccount by this component.
315 List<PhoneAccountHandle> handles = getAllPhoneAccountHandles();
316 for (PhoneAccountHandle handle : handles) {
Evan Charlton105d9772014-11-25 14:08:53 -0800317 if (componentName.equals(handle.getComponentName())
318 && isVisibleForUser(handle)) {
Evan Charltonaf51ceb2014-07-30 11:56:36 -0700319 return handle;
320 }
321 }
322 Log.d(this, "%s does not have a PhoneAccount; not using as default", componentName);
323 } else {
324 Log.d(this, "%s could not be resolved; not using as default", componentName);
325 }
326 } else {
327 Log.v(this, "No default connection manager specified");
328 }
329
Ihab Awadb78b2762014-07-25 15:16:23 -0700330 return null;
Ihab Awad293edf22014-07-24 17:52:29 -0700331 }
332
Evan Charlton105d9772014-11-25 14:08:53 -0800333 /**
334 * A version of {@link #getPhoneAccount} which does not guard for the current user.
335 *
336 * @param handle
337 * @return
338 */
339 PhoneAccount getPhoneAccountInternal(PhoneAccountHandle handle) {
340 for (PhoneAccount m : mState.accounts) {
341 if (Objects.equals(handle, m.getAccountHandle())) {
342 return m;
343 }
344 }
345 return null;
346 }
347
348 /**
349 * Update the current UserHandle to track when users are switched. This will allow the
350 * PhoneAccountRegistar to self-filter the PhoneAccounts to make sure we don't leak anything
351 * across users.
352 *
353 * @param userHandle The {@link UserHandle}, as delivered by
354 * {@link Intent#ACTION_USER_SWITCHED}.
355 */
356 public void setCurrentUserHandle(UserHandle userHandle) {
357 if (userHandle == null) {
358 Log.d(this, "setCurrentUserHandle, userHandle = null");
359 userHandle = Process.myUserHandle();
360 }
361 Log.d(this, "setCurrentUserHandle, %s", userHandle);
362 mCurrentUserHandle = userHandle;
363 }
364
365 private boolean isVisibleForUser(PhoneAccountHandle accountHandle) {
366 if (accountHandle == null) {
367 return false;
368 }
369
370 return isVisibleForUser(getPhoneAccountInternal(accountHandle));
371 }
372
373 private boolean isVisibleForUser(PhoneAccount account) {
374 if (account == null) {
375 return false;
376 }
377
378 // If this PhoneAccount has CAPABILITY_MULTI_USER, it should be visible to all users and
379 // all profiles. Only Telephony and SIP accounts should have this capability.
380 if (account.hasCapabilities(PhoneAccount.CAPABILITY_MULTI_USER)) {
381 return true;
382 }
383
384 UserHandle phoneAccountUserHandle = account.getAccountHandle().getUserHandle();
385 if (phoneAccountUserHandle == null) {
386 return false;
387 }
388
389 if (mCurrentUserHandle == null) {
390 Log.d(this, "Current user is null; assuming true");
391 return true;
392 }
393
Sailesh Nepal91fc8092015-02-14 15:44:55 -0800394 if (phoneAccountUserHandle.equals(Binder.getCallingUserHandle())) {
395 return true;
396 }
397
Evan Charlton105d9772014-11-25 14:08:53 -0800398 // Unlike in TelecomServiceImpl, we only care about *profiles* here. We want to make sure
399 // that we don't resolve PhoneAccount across *users*, but resolving across *profiles* is
400 // fine.
Sailesh Nepal91fc8092015-02-14 15:44:55 -0800401 if (UserHandle.getCallingUserId() == UserHandle.USER_OWNER) {
402 List<UserInfo> profileUsers =
403 mUserManager.getProfiles(mCurrentUserHandle.getIdentifier());
404 for (UserInfo profileInfo : profileUsers) {
405 if (profileInfo.getUserHandle().equals(phoneAccountUserHandle)) {
406 return true;
407 }
Evan Charlton105d9772014-11-25 14:08:53 -0800408 }
409 }
Sailesh Nepal91fc8092015-02-14 15:44:55 -0800410
Evan Charlton105d9772014-11-25 14:08:53 -0800411 return false;
412 }
413
414 private List<ResolveInfo> resolveComponent(PhoneAccountHandle phoneAccountHandle) {
415 return resolveComponent(phoneAccountHandle.getComponentName(),
416 phoneAccountHandle.getUserHandle());
417 }
418
419 private List<ResolveInfo> resolveComponent(ComponentName componentName,
420 UserHandle userHandle) {
mike dooley10a58312014-11-06 13:46:19 -0800421 PackageManager pm = mContext.getPackageManager();
422 Intent intent = new Intent(ConnectionService.SERVICE_INTERFACE);
423 intent.setComponent(componentName);
Sailesh Nepal91fc8092015-02-14 15:44:55 -0800424 try {
425 if (userHandle != null) {
426 return pm.queryIntentServicesAsUser(intent, 0, userHandle.getIdentifier());
427 } else {
428 return pm.queryIntentServices(intent, 0);
429 }
430 } catch (SecurityException e) {
431 Log.v(this, "%s is not visible for the calling user", componentName);
432 return Collections.EMPTY_LIST;
Evan Charlton105d9772014-11-25 14:08:53 -0800433 }
mike dooley10a58312014-11-06 13:46:19 -0800434 }
435
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700436 /**
437 * Retrieves a list of all {@link PhoneAccountHandle}s registered.
438 *
439 * @return The list of {@link PhoneAccountHandle}s.
440 */
Ihab Awad293edf22014-07-24 17:52:29 -0700441 public List<PhoneAccountHandle> getAllPhoneAccountHandles() {
442 List<PhoneAccountHandle> accountHandles = new ArrayList<>();
443 for (PhoneAccount m : mState.accounts) {
Evan Charlton105d9772014-11-25 14:08:53 -0800444 if (isVisibleForUser(m)) {
445 accountHandles.add(m.getAccountHandle());
446 }
Ihab Awad293edf22014-07-24 17:52:29 -0700447 }
448 return accountHandles;
449 }
450
451 public List<PhoneAccount> getAllPhoneAccounts() {
Evan Charlton105d9772014-11-25 14:08:53 -0800452 List<PhoneAccount> accounts = new ArrayList<>(mState.accounts.size());
453 for (PhoneAccount account : mState.accounts) {
454 if (isVisibleForUser(account)) {
455 accounts.add(account);
456 }
457 }
458 return accounts;
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700459 }
460
461 /**
Nancy Chen309198e2014-09-15 18:02:49 -0700462 * Retrieves a list of all call provider phone accounts.
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700463 *
464 * @return The phone account handles.
465 */
Nancy Chen309198e2014-09-15 18:02:49 -0700466 public List<PhoneAccountHandle> getCallCapablePhoneAccounts() {
Santos Cordonafe59e52014-08-22 16:48:43 -0700467 return getPhoneAccountHandles(PhoneAccount.CAPABILITY_CALL_PROVIDER);
468 }
469
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700470 /**
Nancy Chen309198e2014-09-15 18:02:49 -0700471 * Retrieves a list of all phone account call provider phone accounts supporting the
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700472 * specified URI scheme.
473 *
474 * @param uriScheme The URI scheme.
475 * @return The phone account handles.
476 */
Nancy Chen309198e2014-09-15 18:02:49 -0700477 public List<PhoneAccountHandle> getCallCapablePhoneAccounts(String uriScheme) {
478 return getPhoneAccountHandles(PhoneAccount.CAPABILITY_CALL_PROVIDER, uriScheme);
Tyler Gunn84253572014-09-02 14:50:05 -0700479 }
480
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700481 /**
Nancy Chen1c5926f2014-09-17 14:44:14 -0700482 * Retrieves a list of all phone accounts registered by a specified package.
483 *
484 * @param packageName The name of the package that registered the phone accounts.
485 * @return The phone account handles.
486 */
487 public List<PhoneAccountHandle> getPhoneAccountsForPackage(String packageName) {
488 List<PhoneAccountHandle> accountHandles = new ArrayList<>();
489 for (PhoneAccount m : mState.accounts) {
490 if (Objects.equals(
491 packageName,
Evan Charlton105d9772014-11-25 14:08:53 -0800492 m.getAccountHandle().getComponentName().getPackageName())
493 && isVisibleForUser(m)) {
Nancy Chen1c5926f2014-09-17 14:44:14 -0700494 accountHandles.add(m.getAccountHandle());
495 }
496 }
497 return accountHandles;
498 }
499
500 /**
Nancy Chen309198e2014-09-15 18:02:49 -0700501 * Retrieves a list of all phone account handles with the connection manager capability.
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700502 *
503 * @return The phone account handles.
504 */
505 public List<PhoneAccountHandle> getConnectionManagerPhoneAccounts() {
Santos Cordone3d82452014-09-15 13:44:29 -0700506 return getPhoneAccountHandles(PhoneAccount.CAPABILITY_CONNECTION_MANAGER,
Nancy Chen309198e2014-09-15 18:02:49 -0700507 null /* supportedUriScheme */);
Santos Cordon176ae282014-07-14 02:02:14 -0700508 }
509
Ihab Awad293edf22014-07-24 17:52:29 -0700510 public PhoneAccount getPhoneAccount(PhoneAccountHandle handle) {
511 for (PhoneAccount m : mState.accounts) {
Evan Charlton105d9772014-11-25 14:08:53 -0800512 if (Objects.equals(handle, m.getAccountHandle())
513 && isVisibleForUser(m)) {
Ihab Awad104f8062014-07-17 11:29:35 -0700514 return m;
Santos Cordon176ae282014-07-14 02:02:14 -0700515 }
516 }
517 return null;
518 }
519
Ihab Awad104f8062014-07-17 11:29:35 -0700520 // TODO: Should we implement an artificial limit for # of accounts associated with a single
521 // ComponentName?
Ihab Awad293edf22014-07-24 17:52:29 -0700522 public void registerPhoneAccount(PhoneAccount account) {
Tyler Gunncb59b672014-08-20 09:02:11 -0700523 // Enforce the requirement that a connection service for a phone account has the correct
524 // permission.
525 if (!phoneAccountHasPermission(account.getAccountHandle())) {
526 Log.w(this, "Phone account %s does not have BIND_CONNECTION_SERVICE permission.",
527 account.getAccountHandle());
528 throw new SecurityException(
529 "PhoneAccount connection service requires BIND_CONNECTION_SERVICE permission.");
530 }
531
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700532 addOrReplacePhoneAccount(account);
533 }
534
535 /**
536 * Adds a {@code PhoneAccount}, replacing an existing one if found.
537 *
538 * @param account The {@code PhoneAccount} to add or replace.
539 */
540 private void addOrReplacePhoneAccount(PhoneAccount account) {
Ihab Awad7e2c7f32014-11-03 09:49:45 -0800541 Log.d(this, "addOrReplacePhoneAccount(%s -> %s)",
542 account.getAccountHandle(), account);
543
Ihab Awad293edf22014-07-24 17:52:29 -0700544 mState.accounts.add(account);
Ihab Awad104f8062014-07-17 11:29:35 -0700545 // Search for duplicates and remove any that are found.
Ihab Awad293edf22014-07-24 17:52:29 -0700546 for (int i = 0; i < mState.accounts.size() - 1; i++) {
547 if (Objects.equals(
548 account.getAccountHandle(), mState.accounts.get(i).getAccountHandle())) {
Ihab Awad104f8062014-07-17 11:29:35 -0700549 // replace existing entry.
Ihab Awad293edf22014-07-24 17:52:29 -0700550 mState.accounts.remove(i);
Ihab Awad104f8062014-07-17 11:29:35 -0700551 break;
Santos Cordon176ae282014-07-14 02:02:14 -0700552 }
553 }
554
Ihab Awad293edf22014-07-24 17:52:29 -0700555 write();
Ihab Awadb78b2762014-07-25 15:16:23 -0700556 fireAccountsChanged();
Ihab Awad293edf22014-07-24 17:52:29 -0700557 }
558
Evan Charlton89176372014-07-19 18:23:09 -0700559 public void unregisterPhoneAccount(PhoneAccountHandle accountHandle) {
Ihab Awad293edf22014-07-24 17:52:29 -0700560 for (int i = 0; i < mState.accounts.size(); i++) {
Evan Charlton105d9772014-11-25 14:08:53 -0800561 PhoneAccountHandle handle = mState.accounts.get(i).getAccountHandle();
562 if (Objects.equals(accountHandle, handle)) {
Ihab Awad293edf22014-07-24 17:52:29 -0700563 mState.accounts.remove(i);
Ihab Awad104f8062014-07-17 11:29:35 -0700564 break;
565 }
566 }
567
Ihab Awad293edf22014-07-24 17:52:29 -0700568 write();
Ihab Awadb78b2762014-07-25 15:16:23 -0700569 fireAccountsChanged();
Santos Cordon176ae282014-07-14 02:02:14 -0700570 }
571
Tyler Gunnd900ce62014-08-13 11:40:59 -0700572 /**
573 * Un-registers all phone accounts associated with a specified package.
574 *
575 * @param packageName The package for which phone accounts will be removed.
Evan Charlton105d9772014-11-25 14:08:53 -0800576 * @param userHandle The {@link UserHandle} the package is running under.
Tyler Gunnd900ce62014-08-13 11:40:59 -0700577 */
Evan Charlton105d9772014-11-25 14:08:53 -0800578 public void clearAccounts(String packageName, UserHandle userHandle) {
Tyler Gunnd900ce62014-08-13 11:40:59 -0700579 boolean accountsRemoved = false;
580 Iterator<PhoneAccount> it = mState.accounts.iterator();
581 while (it.hasNext()) {
582 PhoneAccount phoneAccount = it.next();
Evan Charlton105d9772014-11-25 14:08:53 -0800583 PhoneAccountHandle handle = phoneAccount.getAccountHandle();
584 if (Objects.equals(packageName, handle.getComponentName().getPackageName())
585 && Objects.equals(userHandle, handle.getUserHandle())) {
Tyler Gunnd900ce62014-08-13 11:40:59 -0700586 Log.i(this, "Removing phone account " + phoneAccount.getLabel());
587 it.remove();
588 accountsRemoved = true;
Ihab Awad104f8062014-07-17 11:29:35 -0700589 }
590 }
591
Tyler Gunnd900ce62014-08-13 11:40:59 -0700592 if (accountsRemoved) {
593 write();
594 fireAccountsChanged();
595 }
Ihab Awadb78b2762014-07-25 15:16:23 -0700596 }
597
Nancy Chen140004a2014-10-15 15:48:38 -0700598 public boolean isVoiceMailNumber(PhoneAccountHandle accountHandle, String number) {
Wink Saville35850602014-10-23 15:57:21 -0700599 int subId = getSubscriptionIdForPhoneAccount(accountHandle);
Nancy Chen140004a2014-10-15 15:48:38 -0700600 return PhoneNumberUtils.isVoiceMailNumber(subId, number);
601 }
602
Ihab Awadb78b2762014-07-25 15:16:23 -0700603 public void addListener(Listener l) {
604 mListeners.add(l);
605 }
606
607 public void removeListener(Listener l) {
Jay Shraunera82c8f72014-08-14 15:49:16 -0700608 if (l != null) {
609 mListeners.remove(l);
610 }
Ihab Awadb78b2762014-07-25 15:16:23 -0700611 }
612
613 private void fireAccountsChanged() {
614 for (Listener l : mListeners) {
615 l.onAccountsChanged(this);
616 }
617 }
618
619 private void fireDefaultOutgoingChanged() {
620 for (Listener l : mListeners) {
621 l.onDefaultOutgoingChanged(this);
622 }
623 }
624
625 private void fireSimCallManagerChanged() {
626 for (Listener l : mListeners) {
627 l.onSimCallManagerChanged(this);
628 }
Santos Cordon176ae282014-07-14 02:02:14 -0700629 }
630
Tyler Gunncb59b672014-08-20 09:02:11 -0700631 /**
632 * Determines if the connection service specified by a {@link PhoneAccountHandle} has the
633 * {@link Manifest.permission#BIND_CONNECTION_SERVICE} permission.
634 *
635 * @param phoneAccountHandle The phone account to check.
636 * @return {@code True} if the phone account has permission.
637 */
638 public boolean phoneAccountHasPermission(PhoneAccountHandle phoneAccountHandle) {
Sailesh Nepal91fc8092015-02-14 15:44:55 -0800639 List<ResolveInfo> resolveInfos = resolveComponent(phoneAccountHandle);
640 if (resolveInfos.isEmpty()) {
641 Log.w(this, "phoneAccount %s not found", phoneAccountHandle.getComponentName());
Tyler Gunncb59b672014-08-20 09:02:11 -0700642 return false;
643 }
Sailesh Nepal91fc8092015-02-14 15:44:55 -0800644 for (ResolveInfo resolveInfo : resolveInfos) {
645 ServiceInfo serviceInfo = resolveInfo.serviceInfo;
646 if (serviceInfo == null || !Objects.equals(serviceInfo.permission,
647 Manifest.permission.BIND_CONNECTION_SERVICE)) {
648 return false;
649 }
650 }
651 return true;
Tyler Gunncb59b672014-08-20 09:02:11 -0700652 }
653
Ihab Awad293edf22014-07-24 17:52:29 -0700654 ////////////////////////////////////////////////////////////////////////////////////////////////
655
Santos Cordonafe59e52014-08-22 16:48:43 -0700656 /**
657 * Returns a list of phone account handles with the specified flag.
Tyler Gunn84253572014-09-02 14:50:05 -0700658 *
659 * @param flags Flags which the {@code PhoneAccount} must have.
Santos Cordonafe59e52014-08-22 16:48:43 -0700660 */
661 private List<PhoneAccountHandle> getPhoneAccountHandles(int flags) {
Nancy Chen309198e2014-09-15 18:02:49 -0700662 return getPhoneAccountHandles(flags, null);
Tyler Gunn84253572014-09-02 14:50:05 -0700663 }
664
665 /**
666 * Returns a list of phone account handles with the specified flag, supporting the specified
Nancy Chen309198e2014-09-15 18:02:49 -0700667 * URI scheme.
Tyler Gunn84253572014-09-02 14:50:05 -0700668 *
669 * @param flags Flags which the {@code PhoneAccount} must have.
670 * @param uriScheme URI schemes the PhoneAccount must handle. {@code Null} bypasses the
671 * URI scheme check.
672 */
Nancy Chen309198e2014-09-15 18:02:49 -0700673 private List<PhoneAccountHandle> getPhoneAccountHandles(int flags, String uriScheme) {
Evan Charlton94d01622014-07-20 12:32:05 -0700674 List<PhoneAccountHandle> accountHandles = new ArrayList<>();
Ihab Awad293edf22014-07-24 17:52:29 -0700675 for (PhoneAccount m : mState.accounts) {
Evan Charlton105d9772014-11-25 14:08:53 -0800676 if (!m.hasCapabilities(flags)) {
677 // Account doesn't have the right capabilities; skip this one.
678 continue;
Ihab Awadf2a84912014-07-22 21:09:25 -0700679 }
Evan Charlton105d9772014-11-25 14:08:53 -0800680 if (uriScheme != null && !m.supportsUriScheme(uriScheme)) {
681 // Account doesn't support this URI scheme; skip this one.
682 continue;
683 }
684 if (resolveComponent(m.getAccountHandle()).isEmpty()) {
685 // This component cannot be resolved anymore; skip this one.
686 continue;
687 }
688 if (!isVisibleForUser(m)) {
689 // Account is not visible for the current user; skip this one.
690 continue;
691 }
692 accountHandles.add(m.getAccountHandle());
Ihab Awad104f8062014-07-17 11:29:35 -0700693 }
Evan Charlton94d01622014-07-20 12:32:05 -0700694 return accountHandles;
Ihab Awad104f8062014-07-17 11:29:35 -0700695 }
696
Ihab Awad293edf22014-07-24 17:52:29 -0700697 /**
698 * The state of this {@code PhoneAccountRegistrar}.
699 */
Ihab Awadb78b2762014-07-25 15:16:23 -0700700 @VisibleForTesting
701 public static class State {
Ihab Awad293edf22014-07-24 17:52:29 -0700702 /**
703 * The account selected by the user to be employed by default for making outgoing calls.
704 * If the user has not made such a selection, then this is null.
705 */
706 public PhoneAccountHandle defaultOutgoing = null;
707
708 /**
Ihab Awadb78b2762014-07-25 15:16:23 -0700709 * A {@code PhoneAccount} having {@link PhoneAccount#CAPABILITY_CONNECTION_MANAGER} which
Ihab Awad293edf22014-07-24 17:52:29 -0700710 * manages and optimizes a user's PSTN SIM connections.
711 */
712 public PhoneAccountHandle simCallManager;
713
714 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700715 * The complete list of {@code PhoneAccount}s known to the Telecom subsystem.
Ihab Awad293edf22014-07-24 17:52:29 -0700716 */
717 public final List<PhoneAccount> accounts = new ArrayList<>();
Tyler Gunn84253572014-09-02 14:50:05 -0700718
719 /**
720 * The version number of the State data.
721 */
722 public int versionNumber;
Ihab Awad293edf22014-07-24 17:52:29 -0700723 }
724
Tyler Gunn9787e0e2014-10-14 14:36:12 -0700725 /**
726 * Dumps the state of the {@link CallsManager}.
727 *
728 * @param pw The {@code IndentingPrintWriter} to write the state to.
729 */
730 public void dump(IndentingPrintWriter pw) {
731 if (mState != null) {
732 pw.println("xmlVersion: " + mState.versionNumber);
733 pw.println("defaultOutgoing: " + (mState.defaultOutgoing == null ? "none" :
734 mState.defaultOutgoing));
735 pw.println("simCallManager: " + (mState.simCallManager == null ? "none" :
736 mState.simCallManager));
737 pw.println("phoneAccounts:");
738 pw.increaseIndent();
739 for (PhoneAccount phoneAccount : mState.accounts) {
740 pw.println(phoneAccount);
741 }
742 pw.decreaseIndent();
743 }
744 }
745
Ihab Awad293edf22014-07-24 17:52:29 -0700746 ////////////////////////////////////////////////////////////////////////////////////////////////
747 //
748 // State management
749 //
750
751 private void write() {
Ihab Awadb78b2762014-07-25 15:16:23 -0700752 final FileOutputStream os;
Ihab Awad104f8062014-07-17 11:29:35 -0700753 try {
Ihab Awadb78b2762014-07-25 15:16:23 -0700754 os = mAtomicFile.startWrite();
755 boolean success = false;
756 try {
757 XmlSerializer serializer = new FastXmlSerializer();
758 serializer.setOutput(new BufferedOutputStream(os), "utf-8");
Evan Charlton105d9772014-11-25 14:08:53 -0800759 writeToXml(mState, serializer, mContext);
Ihab Awadb78b2762014-07-25 15:16:23 -0700760 serializer.flush();
761 success = true;
762 } finally {
763 if (success) {
764 mAtomicFile.finishWrite(os);
765 } else {
766 mAtomicFile.failWrite(os);
767 }
768 }
769 } catch (IOException e) {
770 Log.e(this, e, "Writing state to XML file");
Ihab Awad104f8062014-07-17 11:29:35 -0700771 }
772 }
773
Ihab Awadb78b2762014-07-25 15:16:23 -0700774 private void read() {
775 final InputStream is;
Ihab Awad104f8062014-07-17 11:29:35 -0700776 try {
Ihab Awadb78b2762014-07-25 15:16:23 -0700777 is = mAtomicFile.openRead();
778 } catch (FileNotFoundException ex) {
779 return;
780 }
781
Tyler Gunn84253572014-09-02 14:50:05 -0700782 boolean versionChanged = false;
783
Ihab Awadb78b2762014-07-25 15:16:23 -0700784 XmlPullParser parser;
785 try {
786 parser = Xml.newPullParser();
787 parser.setInput(new BufferedInputStream(is), null);
788 parser.nextTag();
Tyler Gunn84253572014-09-02 14:50:05 -0700789 mState = readFromXml(parser, mContext);
790 versionChanged = mState.versionNumber < EXPECTED_STATE_VERSION;
791
Ihab Awadb78b2762014-07-25 15:16:23 -0700792 } catch (IOException | XmlPullParserException e) {
793 Log.e(this, e, "Reading state from XML file");
794 mState = new State();
795 } finally {
796 try {
797 is.close();
798 } catch (IOException e) {
799 Log.e(this, e, "Closing InputStream");
800 }
Ihab Awad104f8062014-07-17 11:29:35 -0700801 }
Tyler Gunn84253572014-09-02 14:50:05 -0700802
Evan Charlton105d9772014-11-25 14:08:53 -0800803 // Verify all of the UserHandles.
804 List<PhoneAccount> badAccounts = new ArrayList<>();
805 for (PhoneAccount phoneAccount : mState.accounts) {
806 UserHandle userHandle = phoneAccount.getAccountHandle().getUserHandle();
807 if (userHandle == null) {
808 Log.w(this, "Missing UserHandle for %s", phoneAccount);
809 badAccounts.add(phoneAccount);
810 } else if (mUserManager.getSerialNumberForUser(userHandle) == -1) {
811 Log.w(this, "User does not exist for %s", phoneAccount);
812 badAccounts.add(phoneAccount);
813 }
814 }
815 mState.accounts.removeAll(badAccounts);
816
Tyler Gunn84253572014-09-02 14:50:05 -0700817 // If an upgrade occurred, write out the changed data.
Evan Charlton105d9772014-11-25 14:08:53 -0800818 if (versionChanged || !badAccounts.isEmpty()) {
Tyler Gunn84253572014-09-02 14:50:05 -0700819 write();
820 }
Santos Cordon176ae282014-07-14 02:02:14 -0700821 }
822
Evan Charlton105d9772014-11-25 14:08:53 -0800823 private static void writeToXml(State state, XmlSerializer serializer, Context context)
Ihab Awadb78b2762014-07-25 15:16:23 -0700824 throws IOException {
Evan Charlton105d9772014-11-25 14:08:53 -0800825 sStateXml.writeToXml(state, serializer, context);
Santos Cordon176ae282014-07-14 02:02:14 -0700826 }
Ihab Awad104f8062014-07-17 11:29:35 -0700827
Tyler Gunn84253572014-09-02 14:50:05 -0700828 private static State readFromXml(XmlPullParser parser, Context context)
Ihab Awadb78b2762014-07-25 15:16:23 -0700829 throws IOException, XmlPullParserException {
Tyler Gunn84253572014-09-02 14:50:05 -0700830 State s = sStateXml.readFromXml(parser, 0, context);
Ihab Awadb78b2762014-07-25 15:16:23 -0700831 return s != null ? s : new State();
Ihab Awad104f8062014-07-17 11:29:35 -0700832 }
833
Ihab Awad293edf22014-07-24 17:52:29 -0700834 ////////////////////////////////////////////////////////////////////////////////////////////////
Ihab Awad104f8062014-07-17 11:29:35 -0700835 //
Ihab Awadb78b2762014-07-25 15:16:23 -0700836 // XML serialization
Ihab Awad104f8062014-07-17 11:29:35 -0700837 //
838
Ihab Awadb78b2762014-07-25 15:16:23 -0700839 @VisibleForTesting
Ihab Awad26923222014-07-30 10:54:35 -0700840 public abstract static class XmlSerialization<T> {
Tyler Gunn84253572014-09-02 14:50:05 -0700841 private static final String LENGTH_ATTRIBUTE = "length";
842 private static final String VALUE_TAG = "value";
843
Ihab Awadb78b2762014-07-25 15:16:23 -0700844 /**
845 * Write the supplied object to XML
846 */
Evan Charlton105d9772014-11-25 14:08:53 -0800847 public abstract void writeToXml(T o, XmlSerializer serializer, Context context)
Ihab Awad26923222014-07-30 10:54:35 -0700848 throws IOException;
Ihab Awadb78b2762014-07-25 15:16:23 -0700849
850 /**
851 * Read from the supplied XML into a new object, returning null in case of an
852 * unrecoverable schema mismatch or other data error. 'parser' must be already
853 * positioned at the first tag that is expected to have been emitted by this
854 * object's writeToXml(). This object tries to fail early without modifying
855 * 'parser' if it does not recognize the data it sees.
856 */
Tyler Gunn84253572014-09-02 14:50:05 -0700857 public abstract T readFromXml(XmlPullParser parser, int version, Context context)
Ihab Awad26923222014-07-30 10:54:35 -0700858 throws IOException, XmlPullParserException;
859
Ihab Awadd9f54382014-10-24 11:44:47 -0700860 protected void writeTextIfNonNull(String tagName, Object value, XmlSerializer serializer)
Ihab Awad26923222014-07-30 10:54:35 -0700861 throws IOException {
862 if (value != null) {
863 serializer.startTag(null, tagName);
864 serializer.text(Objects.toString(value));
865 serializer.endTag(null, tagName);
866 }
867 }
Tyler Gunn84253572014-09-02 14:50:05 -0700868
869 /**
870 * Serializes a string array.
871 *
872 * @param tagName The tag name for the string array.
873 * @param values The string values to serialize.
874 * @param serializer The serializer.
875 * @throws IOException
876 */
877 protected void writeStringList(String tagName, List<String> values,
878 XmlSerializer serializer)
879 throws IOException {
880
881 serializer.startTag(null, tagName);
882 if (values != null) {
883 serializer.attribute(null, LENGTH_ATTRIBUTE, Objects.toString(values.size()));
884 for (String toSerialize : values) {
885 serializer.startTag(null, VALUE_TAG);
886 if (toSerialize != null ){
887 serializer.text(toSerialize);
888 }
Tyler Gunn84253572014-09-02 14:50:05 -0700889 serializer.endTag(null, VALUE_TAG);
890 }
891 } else {
892 serializer.attribute(null, LENGTH_ATTRIBUTE, "0");
893 }
894 serializer.endTag(null, tagName);
Ihab Awadd9f54382014-10-24 11:44:47 -0700895 }
Tyler Gunn84253572014-09-02 14:50:05 -0700896
Ihab Awadd9f54382014-10-24 11:44:47 -0700897 protected void writeBitmapIfNonNull(String tagName, Bitmap value, XmlSerializer serializer)
898 throws IOException {
899 if (value != null && value.getByteCount() > 0) {
900 ByteArrayOutputStream stream = new ByteArrayOutputStream();
901 value.compress(Bitmap.CompressFormat.PNG, 100, stream);
902 byte[] imageByteArray = stream.toByteArray();
903 String text = Base64.encodeToString(imageByteArray, 0, imageByteArray.length, 0);
904
905 serializer.startTag(null, tagName);
906 serializer.text(text);
907 serializer.endTag(null, tagName);
908 }
Tyler Gunn84253572014-09-02 14:50:05 -0700909 }
910
Evan Charlton105d9772014-11-25 14:08:53 -0800911 protected void writeLong(String tagName, long value, XmlSerializer serializer)
912 throws IOException {
913 serializer.startTag(null, tagName);
914 serializer.text(Long.valueOf(value).toString());
915 serializer.endTag(null, tagName);
916 }
917
Tyler Gunn84253572014-09-02 14:50:05 -0700918 /**
919 * Reads a string array from the XML parser.
920 *
921 * @param parser The XML parser.
922 * @return String array containing the parsed values.
923 * @throws IOException Exception related to IO.
924 * @throws XmlPullParserException Exception related to parsing.
925 */
926 protected List<String> readStringList(XmlPullParser parser)
927 throws IOException, XmlPullParserException {
928
929 int length = Integer.parseInt(parser.getAttributeValue(null, LENGTH_ATTRIBUTE));
930 List<String> arrayEntries = new ArrayList<String>(length);
931 String value = null;
932
933 if (length == 0) {
934 return arrayEntries;
935 }
936
937 int outerDepth = parser.getDepth();
938 while (XmlUtils.nextElementWithin(parser, outerDepth)) {
939 if (parser.getName().equals(VALUE_TAG)) {
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700940 parser.next();
Tyler Gunn84253572014-09-02 14:50:05 -0700941 value = parser.getText();
942 arrayEntries.add(value);
943 }
944 }
945
946 return arrayEntries;
947 }
Ihab Awadd9f54382014-10-24 11:44:47 -0700948
949 protected Bitmap readBitmap(XmlPullParser parser)
950 throws IOException, XmlPullParserException {
951 byte[] imageByteArray = Base64.decode(parser.getText(), 0);
952 return BitmapFactory.decodeByteArray(imageByteArray, 0, imageByteArray.length);
953 }
Ihab Awad104f8062014-07-17 11:29:35 -0700954 }
955
Ihab Awadb78b2762014-07-25 15:16:23 -0700956 @VisibleForTesting
957 public static final XmlSerialization<State> sStateXml =
958 new XmlSerialization<State>() {
959 private static final String CLASS_STATE = "phone_account_registrar_state";
Ihab Awad104f8062014-07-17 11:29:35 -0700960 private static final String DEFAULT_OUTGOING = "default_outgoing";
Ihab Awad293edf22014-07-24 17:52:29 -0700961 private static final String SIM_CALL_MANAGER = "sim_call_manager";
Ihab Awad104f8062014-07-17 11:29:35 -0700962 private static final String ACCOUNTS = "accounts";
Tyler Gunn84253572014-09-02 14:50:05 -0700963 private static final String VERSION = "version";
Ihab Awad104f8062014-07-17 11:29:35 -0700964
965 @Override
Evan Charlton105d9772014-11-25 14:08:53 -0800966 public void writeToXml(State o, XmlSerializer serializer, Context context)
Ihab Awadb78b2762014-07-25 15:16:23 -0700967 throws IOException {
Ihab Awad26923222014-07-30 10:54:35 -0700968 if (o != null) {
969 serializer.startTag(null, CLASS_STATE);
Tyler Gunn84253572014-09-02 14:50:05 -0700970 serializer.attribute(null, VERSION, Objects.toString(EXPECTED_STATE_VERSION));
Ihab Awadb78b2762014-07-25 15:16:23 -0700971
Ihab Awad26923222014-07-30 10:54:35 -0700972 if (o.defaultOutgoing != null) {
973 serializer.startTag(null, DEFAULT_OUTGOING);
Evan Charlton105d9772014-11-25 14:08:53 -0800974 sPhoneAccountHandleXml.writeToXml(o.defaultOutgoing, serializer, context);
Ihab Awad26923222014-07-30 10:54:35 -0700975 serializer.endTag(null, DEFAULT_OUTGOING);
976 }
977
978 if (o.simCallManager != null) {
979 serializer.startTag(null, SIM_CALL_MANAGER);
Evan Charlton105d9772014-11-25 14:08:53 -0800980 sPhoneAccountHandleXml.writeToXml(o.simCallManager, serializer, context);
Ihab Awad26923222014-07-30 10:54:35 -0700981 serializer.endTag(null, SIM_CALL_MANAGER);
982 }
983
984 serializer.startTag(null, ACCOUNTS);
985 for (PhoneAccount m : o.accounts) {
Evan Charlton105d9772014-11-25 14:08:53 -0800986 sPhoneAccountXml.writeToXml(m, serializer, context);
Ihab Awad26923222014-07-30 10:54:35 -0700987 }
988 serializer.endTag(null, ACCOUNTS);
989
990 serializer.endTag(null, CLASS_STATE);
Ihab Awad293edf22014-07-24 17:52:29 -0700991 }
Ihab Awad104f8062014-07-17 11:29:35 -0700992 }
993
994 @Override
Tyler Gunn84253572014-09-02 14:50:05 -0700995 public State readFromXml(XmlPullParser parser, int version, Context context)
Ihab Awadb78b2762014-07-25 15:16:23 -0700996 throws IOException, XmlPullParserException {
997 if (parser.getName().equals(CLASS_STATE)) {
998 State s = new State();
Tyler Gunn84253572014-09-02 14:50:05 -0700999
1000 String rawVersion = parser.getAttributeValue(null, VERSION);
1001 s.versionNumber = TextUtils.isEmpty(rawVersion) ? 1 :
1002 Integer.parseInt(rawVersion);
1003
Ihab Awadb78b2762014-07-25 15:16:23 -07001004 int outerDepth = parser.getDepth();
1005 while (XmlUtils.nextElementWithin(parser, outerDepth)) {
1006 if (parser.getName().equals(DEFAULT_OUTGOING)) {
1007 parser.nextTag();
Tyler Gunn84253572014-09-02 14:50:05 -07001008 s.defaultOutgoing = sPhoneAccountHandleXml.readFromXml(parser,
1009 s.versionNumber, context);
Ihab Awadb78b2762014-07-25 15:16:23 -07001010 } else if (parser.getName().equals(SIM_CALL_MANAGER)) {
1011 parser.nextTag();
Tyler Gunn84253572014-09-02 14:50:05 -07001012 s.simCallManager = sPhoneAccountHandleXml.readFromXml(parser,
1013 s.versionNumber, context);
Evan Charlton105d9772014-11-25 14:08:53 -08001014 if (s.simCallManager.getUserHandle() == null) {
1015 // This should never happen, but handle the upgrade case.
1016 s.simCallManager = new PhoneAccountHandle(
1017 s.simCallManager.getComponentName(),
1018 s.simCallManager.getId(),
1019 Process.myUserHandle());
1020 }
Ihab Awadb78b2762014-07-25 15:16:23 -07001021 } else if (parser.getName().equals(ACCOUNTS)) {
1022 int accountsDepth = parser.getDepth();
1023 while (XmlUtils.nextElementWithin(parser, accountsDepth)) {
Tyler Gunn84253572014-09-02 14:50:05 -07001024 PhoneAccount account = sPhoneAccountXml.readFromXml(parser,
1025 s.versionNumber, context);
1026
1027 if (account != null && s.accounts != null) {
Ihab Awadb78b2762014-07-25 15:16:23 -07001028 s.accounts.add(account);
1029 }
1030 }
Ihab Awad104f8062014-07-17 11:29:35 -07001031 }
1032 }
Ihab Awadb78b2762014-07-25 15:16:23 -07001033 return s;
Ihab Awad104f8062014-07-17 11:29:35 -07001034 }
Ihab Awadb78b2762014-07-25 15:16:23 -07001035 return null;
Ihab Awad104f8062014-07-17 11:29:35 -07001036 }
1037 };
1038
Ihab Awadb78b2762014-07-25 15:16:23 -07001039 @VisibleForTesting
1040 public static final XmlSerialization<PhoneAccount> sPhoneAccountXml =
1041 new XmlSerialization<PhoneAccount>() {
1042 private static final String CLASS_PHONE_ACCOUNT = "phone_account";
1043 private static final String ACCOUNT_HANDLE = "account_handle";
Andrew Lee7129f1c2014-09-04 11:55:07 -07001044 private static final String ADDRESS = "handle";
1045 private static final String SUBSCRIPTION_ADDRESS = "subscription_number";
Ihab Awad104f8062014-07-17 11:29:35 -07001046 private static final String CAPABILITIES = "capabilities";
1047 private static final String ICON_RES_ID = "icon_res_id";
Ihab Awadd9f54382014-10-24 11:44:47 -07001048 private static final String ICON_PACKAGE_NAME = "icon_package_name";
1049 private static final String ICON_BITMAP = "icon_bitmap";
Ihab Awad7e2c7f32014-11-03 09:49:45 -08001050 private static final String ICON_TINT = "icon_tint";
1051 private static final String HIGHLIGHT_COLOR = "highlight_color";
Ihab Awad104f8062014-07-17 11:29:35 -07001052 private static final String LABEL = "label";
1053 private static final String SHORT_DESCRIPTION = "short_description";
Tyler Gunn84253572014-09-02 14:50:05 -07001054 private static final String SUPPORTED_URI_SCHEMES = "supported_uri_schemes";
Ihab Awad104f8062014-07-17 11:29:35 -07001055
1056 @Override
Evan Charlton105d9772014-11-25 14:08:53 -08001057 public void writeToXml(PhoneAccount o, XmlSerializer serializer, Context context)
Ihab Awadb78b2762014-07-25 15:16:23 -07001058 throws IOException {
Ihab Awad26923222014-07-30 10:54:35 -07001059 if (o != null) {
1060 serializer.startTag(null, CLASS_PHONE_ACCOUNT);
Ihab Awadb78b2762014-07-25 15:16:23 -07001061
Ihab Awad26923222014-07-30 10:54:35 -07001062 if (o.getAccountHandle() != null) {
1063 serializer.startTag(null, ACCOUNT_HANDLE);
Evan Charlton105d9772014-11-25 14:08:53 -08001064 sPhoneAccountHandleXml.writeToXml(o.getAccountHandle(), serializer, context);
Ihab Awad26923222014-07-30 10:54:35 -07001065 serializer.endTag(null, ACCOUNT_HANDLE);
1066 }
Ihab Awadb78b2762014-07-25 15:16:23 -07001067
Ihab Awadd9f54382014-10-24 11:44:47 -07001068 writeTextIfNonNull(ADDRESS, o.getAddress(), serializer);
1069 writeTextIfNonNull(SUBSCRIPTION_ADDRESS, o.getSubscriptionAddress(), serializer);
1070 writeTextIfNonNull(CAPABILITIES, Integer.toString(o.getCapabilities()), serializer);
1071 writeTextIfNonNull(ICON_RES_ID, Integer.toString(o.getIconResId()), serializer);
1072 writeTextIfNonNull(ICON_PACKAGE_NAME, o.getIconPackageName(), serializer);
1073 writeBitmapIfNonNull(ICON_BITMAP, o.getIconBitmap(), serializer);
Ihab Awad7e2c7f32014-11-03 09:49:45 -08001074 writeTextIfNonNull(ICON_TINT, Integer.toString(o.getIconTint()), serializer);
1075 writeTextIfNonNull(HIGHLIGHT_COLOR,
1076 Integer.toString(o.getHighlightColor()), serializer);
Ihab Awadd9f54382014-10-24 11:44:47 -07001077 writeTextIfNonNull(LABEL, o.getLabel(), serializer);
1078 writeTextIfNonNull(SHORT_DESCRIPTION, o.getShortDescription(), serializer);
Tyler Gunn84253572014-09-02 14:50:05 -07001079 writeStringList(SUPPORTED_URI_SCHEMES, o.getSupportedUriSchemes(), serializer);
Ihab Awadb78b2762014-07-25 15:16:23 -07001080
Ihab Awad26923222014-07-30 10:54:35 -07001081 serializer.endTag(null, CLASS_PHONE_ACCOUNT);
1082 }
Ihab Awad104f8062014-07-17 11:29:35 -07001083 }
1084
Tyler Gunn84253572014-09-02 14:50:05 -07001085 public PhoneAccount readFromXml(XmlPullParser parser, int version, Context context)
Ihab Awadb78b2762014-07-25 15:16:23 -07001086 throws IOException, XmlPullParserException {
1087 if (parser.getName().equals(CLASS_PHONE_ACCOUNT)) {
1088 int outerDepth = parser.getDepth();
1089 PhoneAccountHandle accountHandle = null;
Andrew Lee7129f1c2014-09-04 11:55:07 -07001090 Uri address = null;
1091 Uri subscriptionAddress = null;
Ihab Awadb78b2762014-07-25 15:16:23 -07001092 int capabilities = 0;
Ihab Awad7e2c7f32014-11-03 09:49:45 -08001093 int iconResId = PhoneAccount.NO_RESOURCE_ID;
Ihab Awadd9f54382014-10-24 11:44:47 -07001094 String iconPackageName = null;
Ihab Awad7e2c7f32014-11-03 09:49:45 -08001095 Bitmap iconBitmap = null;
Ihab Awad07bc5ee2014-11-12 13:42:52 -08001096 int iconTint = PhoneAccount.NO_ICON_TINT;
1097 int highlightColor = PhoneAccount.NO_HIGHLIGHT_COLOR;
Ihab Awadb78b2762014-07-25 15:16:23 -07001098 String label = null;
1099 String shortDescription = null;
Tyler Gunn84253572014-09-02 14:50:05 -07001100 List<String> supportedUriSchemes = null;
Ihab Awadb78b2762014-07-25 15:16:23 -07001101
1102 while (XmlUtils.nextElementWithin(parser, outerDepth)) {
1103 if (parser.getName().equals(ACCOUNT_HANDLE)) {
1104 parser.nextTag();
Tyler Gunn84253572014-09-02 14:50:05 -07001105 accountHandle = sPhoneAccountHandleXml.readFromXml(parser, version,
1106 context);
Andrew Lee7129f1c2014-09-04 11:55:07 -07001107 } else if (parser.getName().equals(ADDRESS)) {
Ihab Awadb78b2762014-07-25 15:16:23 -07001108 parser.next();
Andrew Lee7129f1c2014-09-04 11:55:07 -07001109 address = Uri.parse(parser.getText());
1110 } else if (parser.getName().equals(SUBSCRIPTION_ADDRESS)) {
Ihab Awadb78b2762014-07-25 15:16:23 -07001111 parser.next();
Andrew Lee7129f1c2014-09-04 11:55:07 -07001112 String nextText = parser.getText();
1113 subscriptionAddress = nextText == null ? null : Uri.parse(nextText);
Ihab Awadb78b2762014-07-25 15:16:23 -07001114 } else if (parser.getName().equals(CAPABILITIES)) {
1115 parser.next();
1116 capabilities = Integer.parseInt(parser.getText());
1117 } else if (parser.getName().equals(ICON_RES_ID)) {
1118 parser.next();
1119 iconResId = Integer.parseInt(parser.getText());
Ihab Awadd9f54382014-10-24 11:44:47 -07001120 } else if (parser.getName().equals(ICON_PACKAGE_NAME)) {
1121 parser.next();
1122 iconPackageName = parser.getText();
1123 } else if (parser.getName().equals(ICON_BITMAP)) {
1124 parser.next();
Ihab Awad7e2c7f32014-11-03 09:49:45 -08001125 iconBitmap = readBitmap(parser);
1126 } else if (parser.getName().equals(ICON_TINT)) {
Nancy Chen06ce0622014-10-23 01:17:35 +00001127 parser.next();
Ihab Awad7e2c7f32014-11-03 09:49:45 -08001128 iconTint = Integer.parseInt(parser.getText());
1129 } else if (parser.getName().equals(HIGHLIGHT_COLOR)) {
1130 parser.next();
1131 highlightColor = Integer.parseInt(parser.getText());
Ihab Awadb78b2762014-07-25 15:16:23 -07001132 } else if (parser.getName().equals(LABEL)) {
1133 parser.next();
1134 label = parser.getText();
1135 } else if (parser.getName().equals(SHORT_DESCRIPTION)) {
1136 parser.next();
1137 shortDescription = parser.getText();
Tyler Gunn84253572014-09-02 14:50:05 -07001138 } else if (parser.getName().equals(SUPPORTED_URI_SCHEMES)) {
1139 supportedUriSchemes = readStringList(parser);
Ihab Awadb78b2762014-07-25 15:16:23 -07001140 }
1141 }
Tyler Gunn84253572014-09-02 14:50:05 -07001142
1143 // Upgrade older phone accounts to specify the supported URI schemes.
1144 if (version < 2) {
1145 ComponentName sipComponentName = new ComponentName("com.android.phone",
1146 "com.android.services.telephony.sip.SipConnectionService");
1147
1148 supportedUriSchemes = new ArrayList<>();
1149
1150 // Handle the SIP connection service.
1151 // Check the system settings to see if it also should handle "tel" calls.
1152 if (accountHandle.getComponentName().equals(sipComponentName)) {
1153 boolean useSipForPstn = useSipForPstnCalls(context);
1154 supportedUriSchemes.add(PhoneAccount.SCHEME_SIP);
1155 if (useSipForPstn) {
1156 supportedUriSchemes.add(PhoneAccount.SCHEME_TEL);
1157 }
1158 } else {
1159 supportedUriSchemes.add(PhoneAccount.SCHEME_TEL);
1160 supportedUriSchemes.add(PhoneAccount.SCHEME_VOICEMAIL);
1161 }
1162 }
1163
Ihab Awad7e2c7f32014-11-03 09:49:45 -08001164 // Upgrade older phone accounts with explicit package name
1165 if (version < 5) {
1166 if (iconBitmap == null) {
1167 iconPackageName = accountHandle.getComponentName().getPackageName();
1168 }
1169 }
1170
1171 PhoneAccount.Builder builder = PhoneAccount.builder(accountHandle, label)
Andrew Lee7129f1c2014-09-04 11:55:07 -07001172 .setAddress(address)
1173 .setSubscriptionAddress(subscriptionAddress)
1174 .setCapabilities(capabilities)
Andrew Lee7129f1c2014-09-04 11:55:07 -07001175 .setShortDescription(shortDescription)
1176 .setSupportedUriSchemes(supportedUriSchemes)
Ihab Awad7e2c7f32014-11-03 09:49:45 -08001177 .setHighlightColor(highlightColor);
1178
1179 if (iconBitmap == null) {
1180 builder.setIcon(iconPackageName, iconResId, iconTint);
1181 } else {
1182 builder.setIcon(iconBitmap);
1183 }
1184
1185 return builder.build();
Ihab Awadb78b2762014-07-25 15:16:23 -07001186 }
1187 return null;
Ihab Awad104f8062014-07-17 11:29:35 -07001188 }
Tyler Gunn84253572014-09-02 14:50:05 -07001189
1190 /**
1191 * Determines if the SIP call settings specify to use SIP for all calls, including PSTN calls.
1192 *
1193 * @param context The context.
1194 * @return {@code True} if SIP should be used for all calls.
1195 */
1196 private boolean useSipForPstnCalls(Context context) {
1197 String option = Settings.System.getString(context.getContentResolver(),
1198 Settings.System.SIP_CALL_OPTIONS);
1199 option = (option != null) ? option : Settings.System.SIP_ADDRESS_ONLY;
1200 return option.equals(Settings.System.SIP_ALWAYS);
1201 }
Ihab Awad104f8062014-07-17 11:29:35 -07001202 };
1203
Ihab Awadb78b2762014-07-25 15:16:23 -07001204 @VisibleForTesting
1205 public static final XmlSerialization<PhoneAccountHandle> sPhoneAccountHandleXml =
1206 new XmlSerialization<PhoneAccountHandle>() {
1207 private static final String CLASS_PHONE_ACCOUNT_HANDLE = "phone_account_handle";
Ihab Awad104f8062014-07-17 11:29:35 -07001208 private static final String COMPONENT_NAME = "component_name";
1209 private static final String ID = "id";
Evan Charlton105d9772014-11-25 14:08:53 -08001210 private static final String USER_SERIAL_NUMBER = "user_serial_number";
Ihab Awad104f8062014-07-17 11:29:35 -07001211
1212 @Override
Evan Charlton105d9772014-11-25 14:08:53 -08001213 public void writeToXml(PhoneAccountHandle o, XmlSerializer serializer, Context context)
Ihab Awadb78b2762014-07-25 15:16:23 -07001214 throws IOException {
Ihab Awad26923222014-07-30 10:54:35 -07001215 if (o != null) {
1216 serializer.startTag(null, CLASS_PHONE_ACCOUNT_HANDLE);
Ihab Awadb78b2762014-07-25 15:16:23 -07001217
Ihab Awad26923222014-07-30 10:54:35 -07001218 if (o.getComponentName() != null) {
Ihab Awadd9f54382014-10-24 11:44:47 -07001219 writeTextIfNonNull(
Ihab Awad26923222014-07-30 10:54:35 -07001220 COMPONENT_NAME, o.getComponentName().flattenToString(), serializer);
1221 }
Ihab Awadb78b2762014-07-25 15:16:23 -07001222
Ihab Awadd9f54382014-10-24 11:44:47 -07001223 writeTextIfNonNull(ID, o.getId(), serializer);
Ihab Awadb78b2762014-07-25 15:16:23 -07001224
Evan Charlton105d9772014-11-25 14:08:53 -08001225 if (o.getUserHandle() != null && context != null) {
1226 UserManager userManager = UserManager.get(context);
1227 writeLong(USER_SERIAL_NUMBER,
1228 userManager.getSerialNumberForUser(o.getUserHandle()), serializer);
1229 }
1230
Ihab Awad26923222014-07-30 10:54:35 -07001231 serializer.endTag(null, CLASS_PHONE_ACCOUNT_HANDLE);
1232 }
Ihab Awad104f8062014-07-17 11:29:35 -07001233 }
1234
1235 @Override
Tyler Gunn84253572014-09-02 14:50:05 -07001236 public PhoneAccountHandle readFromXml(XmlPullParser parser, int version, Context context)
Ihab Awadb78b2762014-07-25 15:16:23 -07001237 throws IOException, XmlPullParserException {
1238 if (parser.getName().equals(CLASS_PHONE_ACCOUNT_HANDLE)) {
1239 String componentNameString = null;
1240 String idString = null;
Evan Charlton105d9772014-11-25 14:08:53 -08001241 String userSerialNumberString = null;
Ihab Awadb78b2762014-07-25 15:16:23 -07001242 int outerDepth = parser.getDepth();
Evan Charlton105d9772014-11-25 14:08:53 -08001243
1244 UserManager userManager = UserManager.get(context);
1245
Ihab Awadb78b2762014-07-25 15:16:23 -07001246 while (XmlUtils.nextElementWithin(parser, outerDepth)) {
1247 if (parser.getName().equals(COMPONENT_NAME)) {
1248 parser.next();
1249 componentNameString = parser.getText();
1250 } else if (parser.getName().equals(ID)) {
1251 parser.next();
1252 idString = parser.getText();
Evan Charlton105d9772014-11-25 14:08:53 -08001253 } else if (parser.getName().equals(USER_SERIAL_NUMBER)) {
1254 parser.next();
1255 userSerialNumberString = parser.getText();
Ihab Awadb78b2762014-07-25 15:16:23 -07001256 }
1257 }
Ihab Awad26923222014-07-30 10:54:35 -07001258 if (componentNameString != null) {
Evan Charlton105d9772014-11-25 14:08:53 -08001259 UserHandle userHandle = null;
1260 if (userSerialNumberString != null) {
1261 try {
1262 long serialNumber = Long.parseLong(userSerialNumberString);
1263 userHandle = userManager.getUserForSerialNumber(serialNumber);
1264 } catch (NumberFormatException e) {
1265 Log.e(this, e, "Could not parse UserHandle " + userSerialNumberString);
1266 }
1267 }
Ihab Awadb78b2762014-07-25 15:16:23 -07001268 return new PhoneAccountHandle(
1269 ComponentName.unflattenFromString(componentNameString),
Evan Charlton105d9772014-11-25 14:08:53 -08001270 idString,
1271 userHandle);
Ihab Awadb78b2762014-07-25 15:16:23 -07001272 }
1273 }
1274 return null;
Ihab Awad104f8062014-07-17 11:29:35 -07001275 }
1276 };
Santos Cordon176ae282014-07-14 02:02:14 -07001277}