Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 1 | /* |
| 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 Gunn | 7cc70b4 | 2014-09-12 22:17:27 -0700 | [diff] [blame] | 17 | package com.android.server.telecom; |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 18 | |
Sailesh Nepal | fc43ea8 | 2015-07-28 19:30:28 -0700 | [diff] [blame] | 19 | import android.app.ActivityManager; |
Tyler Gunn | cb59b67 | 2014-08-20 09:02:11 -0700 | [diff] [blame] | 20 | import android.Manifest; |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 21 | import android.content.ComponentName; |
| 22 | import android.content.Context; |
Evan Charlton | af51ceb | 2014-07-30 11:56:36 -0700 | [diff] [blame] | 23 | import android.content.Intent; |
| 24 | import android.content.pm.PackageManager; |
| 25 | import android.content.pm.ResolveInfo; |
Tyler Gunn | cb59b67 | 2014-08-20 09:02:11 -0700 | [diff] [blame] | 26 | import android.content.pm.ServiceInfo; |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 27 | import android.content.pm.UserInfo; |
Ihab Awad | d9f5438 | 2014-10-24 11:44:47 -0700 | [diff] [blame] | 28 | import android.graphics.Bitmap; |
| 29 | import android.graphics.BitmapFactory; |
Santos Cordon | 9c30c28 | 2015-05-13 16:28:27 -0700 | [diff] [blame] | 30 | import android.graphics.drawable.Icon; |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 31 | import android.net.Uri; |
Sailesh Nepal | 91fc809 | 2015-02-14 15:44:55 -0800 | [diff] [blame] | 32 | import android.os.Binder; |
Tyler Gunn | 369c874 | 2015-10-07 15:39:28 -0700 | [diff] [blame^] | 33 | import android.os.Bundle; |
Sailesh Nepal | aecfa9b | 2015-06-16 14:55:27 -0700 | [diff] [blame] | 34 | import android.os.PersistableBundle; |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 35 | import android.os.Process; |
| 36 | import android.os.UserHandle; |
| 37 | import android.os.UserManager; |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 38 | import android.provider.Settings; |
Tyler Gunn | 7cc70b4 | 2014-09-12 22:17:27 -0700 | [diff] [blame] | 39 | import android.telecom.ConnectionService; |
Santos Cordon | 59c21a7 | 2015-06-11 10:11:21 -0700 | [diff] [blame] | 40 | import android.telecom.DefaultDialerManager; |
Tyler Gunn | 7cc70b4 | 2014-09-12 22:17:27 -0700 | [diff] [blame] | 41 | import android.telecom.PhoneAccount; |
| 42 | import android.telecom.PhoneAccountHandle; |
Sailesh Nepal | aecfa9b | 2015-06-16 14:55:27 -0700 | [diff] [blame] | 43 | import android.telephony.CarrierConfigManager; |
Nancy Chen | 140004a | 2014-10-15 15:48:38 -0700 | [diff] [blame] | 44 | import android.telephony.PhoneNumberUtils; |
Nancy Chen | 5a36b6e | 2014-10-23 17:42:42 -0700 | [diff] [blame] | 45 | import android.telephony.SubscriptionManager; |
Santos Cordon | 479b302 | 2015-02-06 04:27:15 -0800 | [diff] [blame] | 46 | import android.telephony.TelephonyManager; |
Evan Charlton | af51ceb | 2014-07-30 11:56:36 -0700 | [diff] [blame] | 47 | import android.text.TextUtils; |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 48 | import android.util.AtomicFile; |
Ihab Awad | d9f5438 | 2014-10-24 11:44:47 -0700 | [diff] [blame] | 49 | import android.util.Base64; |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 50 | import android.util.Xml; |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 51 | |
Tyler Gunn | 91d43cf | 2014-09-17 12:19:39 -0700 | [diff] [blame] | 52 | // TODO: Needed for move to system service: import com.android.internal.R; |
Sailesh Nepal | 0e1dc5a | 2014-07-30 11:08:54 -0700 | [diff] [blame] | 53 | import com.android.internal.annotations.VisibleForTesting; |
| 54 | import com.android.internal.util.FastXmlSerializer; |
Tyler Gunn | 9787e0e | 2014-10-14 14:36:12 -0700 | [diff] [blame] | 55 | import com.android.internal.util.IndentingPrintWriter; |
Sailesh Nepal | 0e1dc5a | 2014-07-30 11:08:54 -0700 | [diff] [blame] | 56 | import com.android.internal.util.XmlUtils; |
| 57 | |
Evan Charlton | af51ceb | 2014-07-30 11:56:36 -0700 | [diff] [blame] | 58 | import org.xmlpull.v1.XmlPullParser; |
| 59 | import org.xmlpull.v1.XmlPullParserException; |
| 60 | import org.xmlpull.v1.XmlSerializer; |
| 61 | |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 62 | import java.io.BufferedInputStream; |
| 63 | import java.io.BufferedOutputStream; |
Santos Cordon | 9c30c28 | 2015-05-13 16:28:27 -0700 | [diff] [blame] | 64 | import java.io.ByteArrayInputStream; |
Ihab Awad | d9f5438 | 2014-10-24 11:44:47 -0700 | [diff] [blame] | 65 | import java.io.ByteArrayOutputStream; |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 66 | import java.io.File; |
| 67 | import java.io.FileNotFoundException; |
| 68 | import java.io.FileOutputStream; |
| 69 | import java.io.IOException; |
| 70 | import java.io.InputStream; |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 71 | import java.lang.Integer; |
Tyler Gunn | cb59b67 | 2014-08-20 09:02:11 -0700 | [diff] [blame] | 72 | import java.lang.SecurityException; |
Tyler Gunn | d900ce6 | 2014-08-13 11:40:59 -0700 | [diff] [blame] | 73 | import java.lang.String; |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 74 | import java.util.ArrayList; |
Sailesh Nepal | 91fc809 | 2015-02-14 15:44:55 -0800 | [diff] [blame] | 75 | import java.util.Collections; |
Tyler Gunn | d900ce6 | 2014-08-13 11:40:59 -0700 | [diff] [blame] | 76 | import java.util.Iterator; |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 77 | import java.util.List; |
| 78 | import java.util.Objects; |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 79 | import java.util.concurrent.CopyOnWriteArrayList; |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 80 | |
| 81 | /** |
Evan Charlton | 8917637 | 2014-07-19 18:23:09 -0700 | [diff] [blame] | 82 | * Handles writing and reading PhoneAccountHandle registration entries. This is a simple verbatim |
Santos Cordon | 6a21264 | 2015-05-08 16:35:23 -0700 | [diff] [blame] | 83 | * delegate for all the account handling methods on {@link android.telecom.TelecomManager} as |
| 84 | * implemented in {@link TelecomServiceImpl}, with the notable exception that |
| 85 | * {@link TelecomServiceImpl} is responsible for security checking to make sure that the caller has |
| 86 | * proper authority over the {@code ComponentName}s they are declaring in their |
| 87 | * {@code PhoneAccountHandle}s. |
| 88 | * |
| 89 | * |
| 90 | * -- About Users and Phone Accounts -- |
| 91 | * |
Santos Cordon | 9c30c28 | 2015-05-13 16:28:27 -0700 | [diff] [blame] | 92 | * We store all phone accounts for all users in a single place, which means that there are three |
| 93 | * users that we have to deal with in code: |
Santos Cordon | 6a21264 | 2015-05-08 16:35:23 -0700 | [diff] [blame] | 94 | * 1) The Android User that is currently active on the device. |
| 95 | * 2) The user which owns/registers the phone account. |
| 96 | * 3) The user running the app that is requesting the phone account information. |
| 97 | * |
| 98 | * For example, I have a device with 2 users, primary (A) and secondary (B), and the secondary user |
| 99 | * has a work profile running as another user (B2). Lets say that user B opens the phone settings |
| 100 | * (not currently supported, but theoretically speaking), and phone settings queries for a phone |
Santos Cordon | 9c30c28 | 2015-05-13 16:28:27 -0700 | [diff] [blame] | 101 | * account list. Lets also say that an app running in the work profile has registered a phone |
| 102 | * account. This means that: |
Santos Cordon | 6a21264 | 2015-05-08 16:35:23 -0700 | [diff] [blame] | 103 | * |
| 104 | * Since phone settings always runs as the primary user, We have the following situation: |
Santos Cordon | 9c30c28 | 2015-05-13 16:28:27 -0700 | [diff] [blame] | 105 | * User A (settings) is requesting a list of phone accounts while the active user is User B, and |
| 106 | * that list contains a phone account for profile User B2. |
Santos Cordon | 6a21264 | 2015-05-08 16:35:23 -0700 | [diff] [blame] | 107 | * |
| 108 | * In practice, (2) is stored with the phone account handle and is part of the handle's ID. (1) is |
| 109 | * saved in {@link #mCurrentUserHandle} and (3) we get from Binder.getCallingUser(). We check these |
| 110 | * users for visibility before returning any phone accounts. |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 111 | */ |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 112 | public final class PhoneAccountRegistrar { |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 113 | |
Yorke Lee | 5e8836a | 2014-08-22 15:25:18 -0700 | [diff] [blame] | 114 | public static final PhoneAccountHandle NO_ACCOUNT_SELECTED = |
| 115 | new PhoneAccountHandle(new ComponentName("null", "null"), "NO_ACCOUNT_SELECTED"); |
| 116 | |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 117 | public abstract static class Listener { |
| 118 | public void onAccountsChanged(PhoneAccountRegistrar registrar) {} |
| 119 | public void onDefaultOutgoingChanged(PhoneAccountRegistrar registrar) {} |
| 120 | public void onSimCallManagerChanged(PhoneAccountRegistrar registrar) {} |
| 121 | } |
| 122 | |
| 123 | private static final String FILE_NAME = "phone-account-registrar-state.xml"; |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 124 | @VisibleForTesting |
Roshan Pius | 6f752c8 | 2015-08-10 10:42:06 -0700 | [diff] [blame] | 125 | public static final int EXPECTED_STATE_VERSION = 8; |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 126 | |
| 127 | /** Keep in sync with the same in SipSettings.java */ |
| 128 | private static final String SIP_SHARED_PREFERENCES = "SIP_PREFERENCES"; |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 129 | |
| 130 | private final List<Listener> mListeners = new CopyOnWriteArrayList<>(); |
| 131 | private final AtomicFile mAtomicFile; |
Santos Cordon | afe59e5 | 2014-08-22 16:48:43 -0700 | [diff] [blame] | 132 | private final Context mContext; |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 133 | private final UserManager mUserManager; |
Wink Saville | 33c05d3 | 2014-11-20 13:04:17 -0800 | [diff] [blame] | 134 | private final SubscriptionManager mSubscriptionManager; |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 135 | private State mState; |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 136 | private UserHandle mCurrentUserHandle; |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 137 | |
Nancy Chen | 06ce062 | 2014-10-23 01:17:35 +0000 | [diff] [blame] | 138 | @VisibleForTesting |
Ihab Awad | 2692322 | 2014-07-30 10:54:35 -0700 | [diff] [blame] | 139 | public PhoneAccountRegistrar(Context context) { |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 140 | this(context, FILE_NAME); |
| 141 | } |
| 142 | |
| 143 | @VisibleForTesting |
| 144 | public PhoneAccountRegistrar(Context context, String fileName) { |
Tyler Gunn | 91d43cf | 2014-09-17 12:19:39 -0700 | [diff] [blame] | 145 | // TODO: This file path is subject to change -- it is storing the phone account registry |
| 146 | // state file in the path /data/system/users/0/, which is likely not correct in a |
| 147 | // multi-user setting. |
| 148 | /** UNCOMMENT_FOR_MOVE_TO_SYSTEM_SERVICE |
| 149 | String filePath = Environment.getUserSystemDirectory(UserHandle.myUserId()). |
| 150 | getAbsolutePath(); |
| 151 | mAtomicFile = new AtomicFile(new File(filePath, fileName)); |
| 152 | UNCOMMENT_FOR_MOVE_TO_SYSTEM_SERVICE */ |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 153 | mAtomicFile = new AtomicFile(new File(context.getFilesDir(), fileName)); |
Tyler Gunn | 91d43cf | 2014-09-17 12:19:39 -0700 | [diff] [blame] | 154 | |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 155 | mState = new State(); |
Santos Cordon | afe59e5 | 2014-08-22 16:48:43 -0700 | [diff] [blame] | 156 | mContext = context; |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 157 | mUserManager = UserManager.get(context); |
Wink Saville | 33c05d3 | 2014-11-20 13:04:17 -0800 | [diff] [blame] | 158 | mSubscriptionManager = SubscriptionManager.from(mContext); |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 159 | mCurrentUserHandle = Process.myUserHandle(); |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 160 | read(); |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 161 | } |
| 162 | |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 163 | /** |
Nancy Chen | 140004a | 2014-10-15 15:48:38 -0700 | [diff] [blame] | 164 | * Retrieves the subscription id for a given phone account if it exists. Subscription ids |
| 165 | * apply only to PSTN/SIM card phone accounts so all other accounts should not have a |
| 166 | * subscription id. |
| 167 | * @param accountHandle The handle for the phone account for which to retrieve the |
| 168 | * subscription id. |
Wink Saville | 3585060 | 2014-10-23 15:57:21 -0700 | [diff] [blame] | 169 | * @return The value of the subscription id or -1 if it does not exist or is not valid. |
Nancy Chen | 140004a | 2014-10-15 15:48:38 -0700 | [diff] [blame] | 170 | */ |
Wink Saville | 3585060 | 2014-10-23 15:57:21 -0700 | [diff] [blame] | 171 | public int getSubscriptionIdForPhoneAccount(PhoneAccountHandle accountHandle) { |
Santos Cordon | 6a21264 | 2015-05-08 16:35:23 -0700 | [diff] [blame] | 172 | PhoneAccount account = getPhoneAccountCheckCallingUser(accountHandle); |
Santos Cordon | 479b302 | 2015-02-06 04:27:15 -0800 | [diff] [blame] | 173 | |
Santos Cordon | 6a21264 | 2015-05-08 16:35:23 -0700 | [diff] [blame] | 174 | if (account != null && account.hasCapabilities(PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION)) { |
Santos Cordon | 479b302 | 2015-02-06 04:27:15 -0800 | [diff] [blame] | 175 | TelephonyManager tm = |
| 176 | (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE); |
| 177 | return tm.getSubIdForPhoneAccount(account); |
Nancy Chen | 140004a | 2014-10-15 15:48:38 -0700 | [diff] [blame] | 178 | } |
Santos Cordon | 479b302 | 2015-02-06 04:27:15 -0800 | [diff] [blame] | 179 | return SubscriptionManager.INVALID_SUBSCRIPTION_ID; |
Nancy Chen | 140004a | 2014-10-15 15:48:38 -0700 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | /** |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 183 | * Retrieves the default outgoing phone account supporting the specified uriScheme. Note that if |
| 184 | * {@link #mCurrentUserHandle} does not have visibility into the current default, {@code null} |
| 185 | * will be returned. |
| 186 | * |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 187 | * @param uriScheme The URI scheme for the outgoing call. |
| 188 | * @return The {@link PhoneAccountHandle} to use. |
| 189 | */ |
Santos Cordon | 6a21264 | 2015-05-08 16:35:23 -0700 | [diff] [blame] | 190 | public PhoneAccountHandle getOutgoingPhoneAccountForScheme(String uriScheme) { |
Yorke Lee | 5e8836a | 2014-08-22 15:25:18 -0700 | [diff] [blame] | 191 | final PhoneAccountHandle userSelected = getUserSelectedOutgoingPhoneAccount(); |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 192 | |
Yorke Lee | 5e8836a | 2014-08-22 15:25:18 -0700 | [diff] [blame] | 193 | if (userSelected != null) { |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 194 | // If there is a default PhoneAccount, ensure it supports calls to handles with the |
| 195 | // specified uriScheme. |
Santos Cordon | 6a21264 | 2015-05-08 16:35:23 -0700 | [diff] [blame] | 196 | final PhoneAccount userSelectedAccount = getPhoneAccountCheckCallingUser(userSelected); |
| 197 | if (userSelectedAccount.supportsUriScheme(uriScheme)) { |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 198 | return userSelected; |
| 199 | } |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 200 | } |
| 201 | |
Santos Cordon | ea5cb93 | 2015-05-07 16:28:38 -0700 | [diff] [blame] | 202 | List<PhoneAccountHandle> outgoing = getCallCapablePhoneAccounts(uriScheme, false); |
Ihab Awad | 6fb37c8 | 2014-08-07 19:48:57 -0700 | [diff] [blame] | 203 | switch (outgoing.size()) { |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 204 | case 0: |
| 205 | // There are no accounts, so there can be no default |
| 206 | return null; |
| 207 | case 1: |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 208 | // There is only one account, which is by definition the default. |
Santos Cordon | 6a21264 | 2015-05-08 16:35:23 -0700 | [diff] [blame] | 209 | return outgoing.get(0); |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 210 | default: |
| 211 | // There are multiple accounts with no selected default |
| 212 | return null; |
Ihab Awad | f2a8491 | 2014-07-22 21:09:25 -0700 | [diff] [blame] | 213 | } |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 214 | } |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 215 | |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 216 | /** |
| 217 | * @return The user-selected outgoing {@link PhoneAccount}, or null if it hasn't been set (or |
| 218 | * if it was set by another user). |
| 219 | */ |
Yorke Lee | 5e8836a | 2014-08-22 15:25:18 -0700 | [diff] [blame] | 220 | PhoneAccountHandle getUserSelectedOutgoingPhoneAccount() { |
Santos Cordon | 6a21264 | 2015-05-08 16:35:23 -0700 | [diff] [blame] | 221 | PhoneAccount account = getPhoneAccountCheckCallingUser(mState.defaultOutgoing); |
| 222 | if (account != null) { |
| 223 | return mState.defaultOutgoing; |
Yorke Lee | 5e8836a | 2014-08-22 15:25:18 -0700 | [diff] [blame] | 224 | } |
| 225 | return null; |
| 226 | } |
| 227 | |
Santos Cordon | 6a21264 | 2015-05-08 16:35:23 -0700 | [diff] [blame] | 228 | /** |
| 229 | * Sets the phone account with which to place all calls by default. Set by the user |
| 230 | * within phone settings. |
| 231 | */ |
Andrew Lee | a51a386 | 2014-09-03 14:58:45 -0700 | [diff] [blame] | 232 | public void setUserSelectedOutgoingPhoneAccount(PhoneAccountHandle accountHandle) { |
Evan Charlton | 8917637 | 2014-07-19 18:23:09 -0700 | [diff] [blame] | 233 | if (accountHandle == null) { |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 234 | // Asking to clear the default outgoing is a valid request |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 235 | mState.defaultOutgoing = null; |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 236 | } else { |
Santos Cordon | 6a21264 | 2015-05-08 16:35:23 -0700 | [diff] [blame] | 237 | // TODO: Do we really want to return for *any* user? |
| 238 | PhoneAccount account = getPhoneAccount(accountHandle); |
| 239 | if (account == null) { |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 240 | Log.w(this, "Trying to set nonexistent default outgoing %s", |
| 241 | accountHandle); |
| 242 | return; |
| 243 | } |
| 244 | |
Santos Cordon | 6a21264 | 2015-05-08 16:35:23 -0700 | [diff] [blame] | 245 | if (!account.hasCapabilities(PhoneAccount.CAPABILITY_CALL_PROVIDER)) { |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 246 | Log.w(this, "Trying to set non-call-provider default outgoing %s", |
Evan Charlton | 8917637 | 2014-07-19 18:23:09 -0700 | [diff] [blame] | 247 | accountHandle); |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 248 | return; |
| 249 | } |
| 250 | |
Santos Cordon | 6a21264 | 2015-05-08 16:35:23 -0700 | [diff] [blame] | 251 | if (account.hasCapabilities(PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION)) { |
Nancy Chen | 5a36b6e | 2014-10-23 17:42:42 -0700 | [diff] [blame] | 252 | // If the account selected is a SIM account, propagate down to the subscription |
| 253 | // record. |
Wink Saville | 7ce6e78 | 2014-10-27 10:56:46 -0700 | [diff] [blame] | 254 | int subId = getSubscriptionIdForPhoneAccount(accountHandle); |
Wink Saville | 33c05d3 | 2014-11-20 13:04:17 -0800 | [diff] [blame] | 255 | mSubscriptionManager.setDefaultVoiceSubId(subId); |
Nancy Chen | 5a36b6e | 2014-10-23 17:42:42 -0700 | [diff] [blame] | 256 | } |
| 257 | |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 258 | mState.defaultOutgoing = accountHandle; |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 259 | } |
| 260 | |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 261 | write(); |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 262 | fireDefaultOutgoingChanged(); |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 263 | } |
| 264 | |
Nancy Chen | 668dee0 | 2014-11-19 15:31:31 -0800 | [diff] [blame] | 265 | boolean isUserSelectedSmsPhoneAccount(PhoneAccountHandle accountHandle) { |
| 266 | return getSubscriptionIdForPhoneAccount(accountHandle) == |
| 267 | SubscriptionManager.getDefaultSmsSubId(); |
| 268 | } |
| 269 | |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 270 | /** |
Santos Cordon | 59c21a7 | 2015-06-11 10:11:21 -0700 | [diff] [blame] | 271 | * Returns the {@link PhoneAccountHandle} corresponding to the currently active SIM Call |
| 272 | * Manager. SIM Call Manager returned corresponds to the following priority order: |
| 273 | * 1. If a SIM Call Manager {@link PhoneAccount} is registered for the same package as the |
| 274 | * default dialer, then that one is returned. |
| 275 | * 2. If there is a SIM Call Manager {@link PhoneAccount} registered which matches the |
| 276 | * carrier configuration's default, then that one is returned. |
| 277 | * 3. Otherwise, we return null. |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 278 | */ |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 279 | public PhoneAccountHandle getSimCallManager() { |
Sailesh Nepal | fc43ea8 | 2015-07-28 19:30:28 -0700 | [diff] [blame] | 280 | long token = Binder.clearCallingIdentity(); |
| 281 | int user; |
| 282 | try { |
| 283 | user = ActivityManager.getCurrentUser(); |
| 284 | } finally { |
| 285 | Binder.restoreCallingIdentity(token); |
| 286 | } |
| 287 | return getSimCallManager(user); |
| 288 | } |
| 289 | |
Sailesh Nepal | 8ed7eeb | 2015-09-08 12:30:13 -0700 | [diff] [blame] | 290 | public ComponentName getSystemSimCallManagerComponent() { |
| 291 | String defaultSimCallManager = null; |
| 292 | CarrierConfigManager configManager = (CarrierConfigManager) mContext.getSystemService( |
| 293 | Context.CARRIER_CONFIG_SERVICE); |
| 294 | PersistableBundle configBundle = configManager.getConfig(); |
| 295 | if (configBundle != null) { |
| 296 | defaultSimCallManager = configBundle.getString( |
| 297 | CarrierConfigManager.KEY_DEFAULT_SIM_CALL_MANAGER_STRING); |
| 298 | } |
| 299 | return TextUtils.isEmpty(defaultSimCallManager) |
| 300 | ? null : ComponentName.unflattenFromString(defaultSimCallManager); |
| 301 | } |
| 302 | |
Sailesh Nepal | fc43ea8 | 2015-07-28 19:30:28 -0700 | [diff] [blame] | 303 | /** |
| 304 | * Returns the {@link PhoneAccountHandle} corresponding to the currently active SIM Call |
| 305 | * Manager. SIM Call Manager returned corresponds to the following priority order: |
| 306 | * 1. If a SIM Call Manager {@link PhoneAccount} is registered for the same package as the |
| 307 | * default dialer, then that one is returned. |
| 308 | * 2. If there is a SIM Call Manager {@link PhoneAccount} registered which matches the |
| 309 | * carrier configuration's default, then that one is returned. |
| 310 | * 3. Otherwise, we return null. |
| 311 | */ |
| 312 | public PhoneAccountHandle getSimCallManager(int user) { |
Santos Cordon | 59c21a7 | 2015-06-11 10:11:21 -0700 | [diff] [blame] | 313 | // Get the default dialer in case it has a connection manager associated with it. |
Sailesh Nepal | fc43ea8 | 2015-07-28 19:30:28 -0700 | [diff] [blame] | 314 | String dialerPackage = DefaultDialerManager.getDefaultDialerApplication(mContext, user); |
Evan Charlton | af51ceb | 2014-07-30 11:56:36 -0700 | [diff] [blame] | 315 | |
Sailesh Nepal | aecfa9b | 2015-06-16 14:55:27 -0700 | [diff] [blame] | 316 | // Check carrier config. |
Sailesh Nepal | 8ed7eeb | 2015-09-08 12:30:13 -0700 | [diff] [blame] | 317 | ComponentName systemSimCallManagerComponent = getSystemSimCallManagerComponent(); |
Sailesh Nepal | 91fc809 | 2015-02-14 15:44:55 -0800 | [diff] [blame] | 318 | |
Santos Cordon | 59c21a7 | 2015-06-11 10:11:21 -0700 | [diff] [blame] | 319 | PhoneAccountHandle dialerSimCallManager = null; |
| 320 | PhoneAccountHandle systemSimCallManager = null; |
Sailesh Nepal | 91fc809 | 2015-02-14 15:44:55 -0800 | [diff] [blame] | 321 | |
Santos Cordon | 59c21a7 | 2015-06-11 10:11:21 -0700 | [diff] [blame] | 322 | if (!TextUtils.isEmpty(dialerPackage) || systemSimCallManagerComponent != null) { |
| 323 | // loop through and look for any connection manager in the same package. |
| 324 | List<PhoneAccountHandle> allSimCallManagers = getPhoneAccountHandles( |
| 325 | PhoneAccount.CAPABILITY_CONNECTION_MANAGER, null, null, |
| 326 | true /* includeDisabledAccounts */); |
| 327 | for (PhoneAccountHandle accountHandle : allSimCallManagers) { |
| 328 | ComponentName component = accountHandle.getComponentName(); |
| 329 | |
| 330 | // Store the system connection manager if found |
| 331 | if (systemSimCallManager == null |
| 332 | && Objects.equals(component, systemSimCallManagerComponent) |
| 333 | && !resolveComponent(accountHandle).isEmpty()) { |
| 334 | systemSimCallManager = accountHandle; |
| 335 | |
| 336 | // Store the dialer connection manager if found |
| 337 | } else if (dialerSimCallManager == null |
| 338 | && Objects.equals(component.getPackageName(), dialerPackage) |
| 339 | && !resolveComponent(accountHandle).isEmpty()) { |
| 340 | dialerSimCallManager = accountHandle; |
Evan Charlton | af51ceb | 2014-07-30 11:56:36 -0700 | [diff] [blame] | 341 | } |
Evan Charlton | af51ceb | 2014-07-30 11:56:36 -0700 | [diff] [blame] | 342 | } |
Evan Charlton | af51ceb | 2014-07-30 11:56:36 -0700 | [diff] [blame] | 343 | } |
| 344 | |
Santos Cordon | 59c21a7 | 2015-06-11 10:11:21 -0700 | [diff] [blame] | 345 | PhoneAccountHandle retval = dialerSimCallManager != null ? |
| 346 | dialerSimCallManager : systemSimCallManager; |
| 347 | |
| 348 | Log.i(this, "SimCallManager queried, returning: %s", retval); |
| 349 | |
Sailesh Nepal | b3ccd7e | 2015-06-26 18:16:18 -0700 | [diff] [blame] | 350 | return retval; |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 351 | } |
| 352 | |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 353 | /** |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 354 | * Update the current UserHandle to track when users are switched. This will allow the |
| 355 | * PhoneAccountRegistar to self-filter the PhoneAccounts to make sure we don't leak anything |
| 356 | * across users. |
Santos Cordon | 6a21264 | 2015-05-08 16:35:23 -0700 | [diff] [blame] | 357 | * We cannot simply check the calling user because that would always return the primary user for |
| 358 | * all invocations originating with the system process. |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 359 | * |
| 360 | * @param userHandle The {@link UserHandle}, as delivered by |
| 361 | * {@link Intent#ACTION_USER_SWITCHED}. |
| 362 | */ |
| 363 | public void setCurrentUserHandle(UserHandle userHandle) { |
| 364 | if (userHandle == null) { |
| 365 | Log.d(this, "setCurrentUserHandle, userHandle = null"); |
| 366 | userHandle = Process.myUserHandle(); |
| 367 | } |
| 368 | Log.d(this, "setCurrentUserHandle, %s", userHandle); |
| 369 | mCurrentUserHandle = userHandle; |
| 370 | } |
| 371 | |
Yorke Lee | 71734c2 | 2015-06-02 14:22:56 -0700 | [diff] [blame] | 372 | /** |
| 373 | * @return {@code true} if the phone account was successfully enabled/disabled, {@code false} |
| 374 | * otherwise. |
| 375 | */ |
| 376 | public boolean enablePhoneAccount(PhoneAccountHandle accountHandle, boolean isEnabled) { |
Santos Cordon | ea5cb93 | 2015-05-07 16:28:38 -0700 | [diff] [blame] | 377 | PhoneAccount account = getPhoneAccount(accountHandle); |
Santos Cordon | 638f05c | 2015-05-19 17:12:33 -0700 | [diff] [blame] | 378 | if (account == null) { |
| 379 | Log.w(this, "Could not find account to enable: " + accountHandle); |
Yorke Lee | 71734c2 | 2015-06-02 14:22:56 -0700 | [diff] [blame] | 380 | return false; |
Santos Cordon | 638f05c | 2015-05-19 17:12:33 -0700 | [diff] [blame] | 381 | } else if (account.hasCapabilities(PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION)) { |
Santos Cordon | ea5cb93 | 2015-05-07 16:28:38 -0700 | [diff] [blame] | 382 | // We never change the enabled state of SIM-based accounts. |
Santos Cordon | 638f05c | 2015-05-19 17:12:33 -0700 | [diff] [blame] | 383 | Log.w(this, "Could not change enable state of SIM account: " + accountHandle); |
Yorke Lee | 71734c2 | 2015-06-02 14:22:56 -0700 | [diff] [blame] | 384 | return false; |
Santos Cordon | ea5cb93 | 2015-05-07 16:28:38 -0700 | [diff] [blame] | 385 | } |
| 386 | |
Santos Cordon | 638f05c | 2015-05-19 17:12:33 -0700 | [diff] [blame] | 387 | if (account.isEnabled() != isEnabled) { |
Santos Cordon | ea5cb93 | 2015-05-07 16:28:38 -0700 | [diff] [blame] | 388 | account.setIsEnabled(isEnabled); |
| 389 | write(); |
| 390 | fireAccountsChanged(); |
| 391 | } |
Yorke Lee | 71734c2 | 2015-06-02 14:22:56 -0700 | [diff] [blame] | 392 | return true; |
Santos Cordon | ea5cb93 | 2015-05-07 16:28:38 -0700 | [diff] [blame] | 393 | } |
| 394 | |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 395 | private boolean isVisibleForUser(PhoneAccount account) { |
| 396 | if (account == null) { |
| 397 | return false; |
| 398 | } |
| 399 | |
| 400 | // If this PhoneAccount has CAPABILITY_MULTI_USER, it should be visible to all users and |
| 401 | // all profiles. Only Telephony and SIP accounts should have this capability. |
| 402 | if (account.hasCapabilities(PhoneAccount.CAPABILITY_MULTI_USER)) { |
| 403 | return true; |
| 404 | } |
| 405 | |
| 406 | UserHandle phoneAccountUserHandle = account.getAccountHandle().getUserHandle(); |
| 407 | if (phoneAccountUserHandle == null) { |
| 408 | return false; |
| 409 | } |
| 410 | |
| 411 | if (mCurrentUserHandle == null) { |
| 412 | Log.d(this, "Current user is null; assuming true"); |
| 413 | return true; |
| 414 | } |
| 415 | |
Sailesh Nepal | 91fc809 | 2015-02-14 15:44:55 -0800 | [diff] [blame] | 416 | if (phoneAccountUserHandle.equals(Binder.getCallingUserHandle())) { |
| 417 | return true; |
| 418 | } |
| 419 | |
Santos Cordon | 6a21264 | 2015-05-08 16:35:23 -0700 | [diff] [blame] | 420 | // Special check for work profiles. |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 421 | // Unlike in TelecomServiceImpl, we only care about *profiles* here. We want to make sure |
| 422 | // that we don't resolve PhoneAccount across *users*, but resolving across *profiles* is |
| 423 | // fine. |
Sailesh Nepal | 91fc809 | 2015-02-14 15:44:55 -0800 | [diff] [blame] | 424 | if (UserHandle.getCallingUserId() == UserHandle.USER_OWNER) { |
| 425 | List<UserInfo> profileUsers = |
| 426 | mUserManager.getProfiles(mCurrentUserHandle.getIdentifier()); |
| 427 | for (UserInfo profileInfo : profileUsers) { |
| 428 | if (profileInfo.getUserHandle().equals(phoneAccountUserHandle)) { |
| 429 | return true; |
| 430 | } |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 431 | } |
| 432 | } |
Sailesh Nepal | 91fc809 | 2015-02-14 15:44:55 -0800 | [diff] [blame] | 433 | |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 434 | return false; |
| 435 | } |
| 436 | |
| 437 | private List<ResolveInfo> resolveComponent(PhoneAccountHandle phoneAccountHandle) { |
| 438 | return resolveComponent(phoneAccountHandle.getComponentName(), |
| 439 | phoneAccountHandle.getUserHandle()); |
| 440 | } |
| 441 | |
| 442 | private List<ResolveInfo> resolveComponent(ComponentName componentName, |
| 443 | UserHandle userHandle) { |
mike dooley | 10a5831 | 2014-11-06 13:46:19 -0800 | [diff] [blame] | 444 | PackageManager pm = mContext.getPackageManager(); |
| 445 | Intent intent = new Intent(ConnectionService.SERVICE_INTERFACE); |
| 446 | intent.setComponent(componentName); |
Sailesh Nepal | 91fc809 | 2015-02-14 15:44:55 -0800 | [diff] [blame] | 447 | try { |
| 448 | if (userHandle != null) { |
| 449 | return pm.queryIntentServicesAsUser(intent, 0, userHandle.getIdentifier()); |
| 450 | } else { |
| 451 | return pm.queryIntentServices(intent, 0); |
| 452 | } |
| 453 | } catch (SecurityException e) { |
Santos Cordon | 3188b36 | 2015-05-22 13:01:10 -0700 | [diff] [blame] | 454 | Log.e(this, e, "%s is not visible for the calling user", componentName); |
Sailesh Nepal | 91fc809 | 2015-02-14 15:44:55 -0800 | [diff] [blame] | 455 | return Collections.EMPTY_LIST; |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 456 | } |
mike dooley | 10a5831 | 2014-11-06 13:46:19 -0800 | [diff] [blame] | 457 | } |
| 458 | |
Tyler Gunn | 8e0fef4 | 2014-09-08 18:34:44 -0700 | [diff] [blame] | 459 | /** |
| 460 | * Retrieves a list of all {@link PhoneAccountHandle}s registered. |
Santos Cordon | ea5cb93 | 2015-05-07 16:28:38 -0700 | [diff] [blame] | 461 | * Only returns accounts which are enabled. |
Tyler Gunn | 8e0fef4 | 2014-09-08 18:34:44 -0700 | [diff] [blame] | 462 | * |
| 463 | * @return The list of {@link PhoneAccountHandle}s. |
| 464 | */ |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 465 | public List<PhoneAccountHandle> getAllPhoneAccountHandles() { |
Santos Cordon | ea5cb93 | 2015-05-07 16:28:38 -0700 | [diff] [blame] | 466 | return getPhoneAccountHandles(0, null, null, false); |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 467 | } |
| 468 | |
| 469 | public List<PhoneAccount> getAllPhoneAccounts() { |
Santos Cordon | ea5cb93 | 2015-05-07 16:28:38 -0700 | [diff] [blame] | 470 | return getPhoneAccounts(0, null, null, false); |
Santos Cordon | afe59e5 | 2014-08-22 16:48:43 -0700 | [diff] [blame] | 471 | } |
| 472 | |
Tyler Gunn | 8e0fef4 | 2014-09-08 18:34:44 -0700 | [diff] [blame] | 473 | /** |
Nancy Chen | 309198e | 2014-09-15 18:02:49 -0700 | [diff] [blame] | 474 | * Retrieves a list of all phone account call provider phone accounts supporting the |
Tyler Gunn | 8e0fef4 | 2014-09-08 18:34:44 -0700 | [diff] [blame] | 475 | * specified URI scheme. |
| 476 | * |
| 477 | * @param uriScheme The URI scheme. |
| 478 | * @return The phone account handles. |
| 479 | */ |
Santos Cordon | ea5cb93 | 2015-05-07 16:28:38 -0700 | [diff] [blame] | 480 | public List<PhoneAccountHandle> getCallCapablePhoneAccounts( |
| 481 | String uriScheme, boolean includeDisabledAccounts) { |
| 482 | return getPhoneAccountHandles( |
| 483 | PhoneAccount.CAPABILITY_CALL_PROVIDER, uriScheme, null, includeDisabledAccounts); |
Santos Cordon | 6a21264 | 2015-05-08 16:35:23 -0700 | [diff] [blame] | 484 | } |
| 485 | |
| 486 | /** |
| 487 | * Retrieves a list of all the SIM-based phone accounts. |
| 488 | */ |
| 489 | public List<PhoneAccountHandle> getSimPhoneAccounts() { |
| 490 | return getPhoneAccountHandles( |
| 491 | PhoneAccount.CAPABILITY_CALL_PROVIDER | PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION, |
Santos Cordon | ea5cb93 | 2015-05-07 16:28:38 -0700 | [diff] [blame] | 492 | null, null, false); |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 493 | } |
| 494 | |
Tyler Gunn | 8e0fef4 | 2014-09-08 18:34:44 -0700 | [diff] [blame] | 495 | /** |
Nancy Chen | 1c5926f | 2014-09-17 14:44:14 -0700 | [diff] [blame] | 496 | * Retrieves a list of all phone accounts registered by a specified package. |
| 497 | * |
| 498 | * @param packageName The name of the package that registered the phone accounts. |
| 499 | * @return The phone account handles. |
| 500 | */ |
| 501 | public List<PhoneAccountHandle> getPhoneAccountsForPackage(String packageName) { |
Santos Cordon | ea5cb93 | 2015-05-07 16:28:38 -0700 | [diff] [blame] | 502 | return getPhoneAccountHandles(0, null, packageName, false); |
Nancy Chen | 1c5926f | 2014-09-17 14:44:14 -0700 | [diff] [blame] | 503 | } |
| 504 | |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 505 | // TODO: Should we implement an artificial limit for # of accounts associated with a single |
| 506 | // ComponentName? |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 507 | public void registerPhoneAccount(PhoneAccount account) { |
Tyler Gunn | cb59b67 | 2014-08-20 09:02:11 -0700 | [diff] [blame] | 508 | // Enforce the requirement that a connection service for a phone account has the correct |
| 509 | // permission. |
Santos Cordon | 6a21264 | 2015-05-08 16:35:23 -0700 | [diff] [blame] | 510 | if (!phoneAccountRequiresBindPermission(account.getAccountHandle())) { |
Yorke Lee | 7bb8ce9 | 2015-05-13 16:28:29 -0700 | [diff] [blame] | 511 | Log.w(this, |
| 512 | "Phone account %s does not have BIND_TELECOM_CONNECTION_SERVICE permission.", |
Tyler Gunn | cb59b67 | 2014-08-20 09:02:11 -0700 | [diff] [blame] | 513 | account.getAccountHandle()); |
Yorke Lee | 7bb8ce9 | 2015-05-13 16:28:29 -0700 | [diff] [blame] | 514 | throw new SecurityException("PhoneAccount connection service requires " |
| 515 | + "BIND_TELECOM_CONNECTION_SERVICE permission."); |
Tyler Gunn | cb59b67 | 2014-08-20 09:02:11 -0700 | [diff] [blame] | 516 | } |
| 517 | |
Tyler Gunn | 8e0fef4 | 2014-09-08 18:34:44 -0700 | [diff] [blame] | 518 | addOrReplacePhoneAccount(account); |
| 519 | } |
| 520 | |
| 521 | /** |
| 522 | * Adds a {@code PhoneAccount}, replacing an existing one if found. |
| 523 | * |
| 524 | * @param account The {@code PhoneAccount} to add or replace. |
| 525 | */ |
| 526 | private void addOrReplacePhoneAccount(PhoneAccount account) { |
Ihab Awad | 7e2c7f3 | 2014-11-03 09:49:45 -0800 | [diff] [blame] | 527 | Log.d(this, "addOrReplacePhoneAccount(%s -> %s)", |
| 528 | account.getAccountHandle(), account); |
| 529 | |
Santos Cordon | ea5cb93 | 2015-05-07 16:28:38 -0700 | [diff] [blame] | 530 | // Start _enabled_ property as false. |
| 531 | // !!! IMPORTANT !!! It is important that we do not read the enabled state that the |
| 532 | // source app provides or else an third party app could enable itself. |
| 533 | boolean isEnabled = false; |
| 534 | |
Santos Cordon | 6a21264 | 2015-05-08 16:35:23 -0700 | [diff] [blame] | 535 | PhoneAccount oldAccount = getPhoneAccount(account.getAccountHandle()); |
| 536 | if (oldAccount != null) { |
| 537 | mState.accounts.remove(oldAccount); |
Santos Cordon | ea5cb93 | 2015-05-07 16:28:38 -0700 | [diff] [blame] | 538 | isEnabled = oldAccount.isEnabled(); |
Santos Cordon | 5fa4e4f | 2015-06-10 14:56:01 -0700 | [diff] [blame] | 539 | Log.i(this, getAccountDiffString(account, oldAccount)); |
| 540 | } else { |
| 541 | Log.i(this, "New phone account registered: " + account); |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 542 | } |
Santos Cordon | ea5cb93 | 2015-05-07 16:28:38 -0700 | [diff] [blame] | 543 | |
Santos Cordon | 6a21264 | 2015-05-08 16:35:23 -0700 | [diff] [blame] | 544 | mState.accounts.add(account); |
Santos Cordon | ea5cb93 | 2015-05-07 16:28:38 -0700 | [diff] [blame] | 545 | // Reset enabled state to whatever the value was if the account was already registered, |
| 546 | // or _true_ if this is a SIM-based account. All SIM-based accounts are always enabled. |
| 547 | account.setIsEnabled( |
| 548 | isEnabled || account.hasCapabilities(PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION)); |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 549 | |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 550 | write(); |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 551 | fireAccountsChanged(); |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 552 | } |
| 553 | |
Evan Charlton | 8917637 | 2014-07-19 18:23:09 -0700 | [diff] [blame] | 554 | public void unregisterPhoneAccount(PhoneAccountHandle accountHandle) { |
Santos Cordon | 6a21264 | 2015-05-08 16:35:23 -0700 | [diff] [blame] | 555 | PhoneAccount account = getPhoneAccount(accountHandle); |
| 556 | if (account != null) { |
| 557 | if (mState.accounts.remove(account)) { |
| 558 | write(); |
| 559 | fireAccountsChanged(); |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 560 | } |
| 561 | } |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 562 | } |
| 563 | |
Tyler Gunn | d900ce6 | 2014-08-13 11:40:59 -0700 | [diff] [blame] | 564 | /** |
| 565 | * Un-registers all phone accounts associated with a specified package. |
| 566 | * |
| 567 | * @param packageName The package for which phone accounts will be removed. |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 568 | * @param userHandle The {@link UserHandle} the package is running under. |
Tyler Gunn | d900ce6 | 2014-08-13 11:40:59 -0700 | [diff] [blame] | 569 | */ |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 570 | public void clearAccounts(String packageName, UserHandle userHandle) { |
Tyler Gunn | d900ce6 | 2014-08-13 11:40:59 -0700 | [diff] [blame] | 571 | boolean accountsRemoved = false; |
| 572 | Iterator<PhoneAccount> it = mState.accounts.iterator(); |
| 573 | while (it.hasNext()) { |
| 574 | PhoneAccount phoneAccount = it.next(); |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 575 | PhoneAccountHandle handle = phoneAccount.getAccountHandle(); |
| 576 | if (Objects.equals(packageName, handle.getComponentName().getPackageName()) |
| 577 | && Objects.equals(userHandle, handle.getUserHandle())) { |
Tyler Gunn | d900ce6 | 2014-08-13 11:40:59 -0700 | [diff] [blame] | 578 | Log.i(this, "Removing phone account " + phoneAccount.getLabel()); |
Shriram Ganesh | 8f89f8d | 2015-04-15 12:35:43 +0530 | [diff] [blame] | 579 | mState.accounts.remove(phoneAccount); |
Tyler Gunn | d900ce6 | 2014-08-13 11:40:59 -0700 | [diff] [blame] | 580 | accountsRemoved = true; |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 581 | } |
| 582 | } |
| 583 | |
Tyler Gunn | d900ce6 | 2014-08-13 11:40:59 -0700 | [diff] [blame] | 584 | if (accountsRemoved) { |
| 585 | write(); |
| 586 | fireAccountsChanged(); |
| 587 | } |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 588 | } |
| 589 | |
Nancy Chen | 140004a | 2014-10-15 15:48:38 -0700 | [diff] [blame] | 590 | public boolean isVoiceMailNumber(PhoneAccountHandle accountHandle, String number) { |
Wink Saville | 3585060 | 2014-10-23 15:57:21 -0700 | [diff] [blame] | 591 | int subId = getSubscriptionIdForPhoneAccount(accountHandle); |
Yorke Lee | b195723 | 2015-06-30 10:09:54 -0700 | [diff] [blame] | 592 | return PhoneNumberUtils.isVoiceMailNumber(mContext, subId, number); |
Nancy Chen | 140004a | 2014-10-15 15:48:38 -0700 | [diff] [blame] | 593 | } |
| 594 | |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 595 | public void addListener(Listener l) { |
| 596 | mListeners.add(l); |
| 597 | } |
| 598 | |
| 599 | public void removeListener(Listener l) { |
Jay Shrauner | a82c8f7 | 2014-08-14 15:49:16 -0700 | [diff] [blame] | 600 | if (l != null) { |
| 601 | mListeners.remove(l); |
| 602 | } |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 603 | } |
| 604 | |
| 605 | private void fireAccountsChanged() { |
| 606 | for (Listener l : mListeners) { |
| 607 | l.onAccountsChanged(this); |
| 608 | } |
| 609 | } |
| 610 | |
| 611 | private void fireDefaultOutgoingChanged() { |
| 612 | for (Listener l : mListeners) { |
| 613 | l.onDefaultOutgoingChanged(this); |
| 614 | } |
| 615 | } |
| 616 | |
| 617 | private void fireSimCallManagerChanged() { |
| 618 | for (Listener l : mListeners) { |
| 619 | l.onSimCallManagerChanged(this); |
| 620 | } |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 621 | } |
| 622 | |
Santos Cordon | 5fa4e4f | 2015-06-10 14:56:01 -0700 | [diff] [blame] | 623 | private String getAccountDiffString(PhoneAccount account1, PhoneAccount account2) { |
| 624 | if (account1 == null || account2 == null) { |
| 625 | return "Diff: " + account1 + ", " + account2; |
| 626 | } |
| 627 | |
| 628 | StringBuffer sb = new StringBuffer(); |
| 629 | sb.append("[").append(account1.getAccountHandle()); |
Anthony Lee | 7c64a4e | 2015-08-25 09:27:47 -0700 | [diff] [blame] | 630 | appendDiff(sb, "addr", Log.piiHandle(account1.getAddress()), |
| 631 | Log.piiHandle(account2.getAddress())); |
Santos Cordon | 5fa4e4f | 2015-06-10 14:56:01 -0700 | [diff] [blame] | 632 | appendDiff(sb, "cap", account1.getCapabilities(), account2.getCapabilities()); |
| 633 | appendDiff(sb, "hl", account1.getHighlightColor(), account2.getHighlightColor()); |
| 634 | appendDiff(sb, "icon", account1.getIcon(), account2.getIcon()); |
| 635 | appendDiff(sb, "lbl", account1.getLabel(), account2.getLabel()); |
| 636 | appendDiff(sb, "desc", account1.getShortDescription(), account2.getShortDescription()); |
Anthony Lee | 7c64a4e | 2015-08-25 09:27:47 -0700 | [diff] [blame] | 637 | appendDiff(sb, "subAddr", Log.piiHandle(account1.getSubscriptionAddress()), |
| 638 | Log.piiHandle(account2.getSubscriptionAddress())); |
Santos Cordon | 5fa4e4f | 2015-06-10 14:56:01 -0700 | [diff] [blame] | 639 | appendDiff(sb, "uris", account1.getSupportedUriSchemes(), |
| 640 | account2.getSupportedUriSchemes()); |
| 641 | sb.append("]"); |
| 642 | return sb.toString(); |
| 643 | } |
| 644 | |
| 645 | private void appendDiff(StringBuffer sb, String attrName, Object obj1, Object obj2) { |
| 646 | if (!Objects.equals(obj1, obj2)) { |
| 647 | sb.append("(") |
| 648 | .append(attrName) |
| 649 | .append(": ") |
| 650 | .append(obj1) |
| 651 | .append(" -> ") |
| 652 | .append(obj2) |
| 653 | .append(")"); |
| 654 | } |
| 655 | } |
| 656 | |
Tyler Gunn | cb59b67 | 2014-08-20 09:02:11 -0700 | [diff] [blame] | 657 | /** |
Santos Cordon | 6a21264 | 2015-05-08 16:35:23 -0700 | [diff] [blame] | 658 | * Determines if the connection service specified by a {@link PhoneAccountHandle} requires the |
Yorke Lee | 7bb8ce9 | 2015-05-13 16:28:29 -0700 | [diff] [blame] | 659 | * {@link Manifest.permission#BIND_TELECOM_CONNECTION_SERVICE} permission. |
Tyler Gunn | cb59b67 | 2014-08-20 09:02:11 -0700 | [diff] [blame] | 660 | * |
| 661 | * @param phoneAccountHandle The phone account to check. |
| 662 | * @return {@code True} if the phone account has permission. |
| 663 | */ |
Santos Cordon | 6a21264 | 2015-05-08 16:35:23 -0700 | [diff] [blame] | 664 | public boolean phoneAccountRequiresBindPermission(PhoneAccountHandle phoneAccountHandle) { |
Sailesh Nepal | 91fc809 | 2015-02-14 15:44:55 -0800 | [diff] [blame] | 665 | List<ResolveInfo> resolveInfos = resolveComponent(phoneAccountHandle); |
| 666 | if (resolveInfos.isEmpty()) { |
| 667 | Log.w(this, "phoneAccount %s not found", phoneAccountHandle.getComponentName()); |
Tyler Gunn | cb59b67 | 2014-08-20 09:02:11 -0700 | [diff] [blame] | 668 | return false; |
| 669 | } |
Sailesh Nepal | 91fc809 | 2015-02-14 15:44:55 -0800 | [diff] [blame] | 670 | for (ResolveInfo resolveInfo : resolveInfos) { |
| 671 | ServiceInfo serviceInfo = resolveInfo.serviceInfo; |
Yorke Lee | 7bb8ce9 | 2015-05-13 16:28:29 -0700 | [diff] [blame] | 672 | if (serviceInfo == null) { |
| 673 | return false; |
| 674 | } |
| 675 | |
| 676 | if (!Manifest.permission.BIND_CONNECTION_SERVICE.equals(serviceInfo.permission) && |
| 677 | !Manifest.permission.BIND_TELECOM_CONNECTION_SERVICE.equals( |
| 678 | serviceInfo.permission)) { |
| 679 | // The ConnectionService must require either the deprecated BIND_CONNECTION_SERVICE, |
| 680 | // or the public BIND_TELECOM_CONNECTION_SERVICE permissions, both of which are |
| 681 | // system/signature only. |
Sailesh Nepal | 91fc809 | 2015-02-14 15:44:55 -0800 | [diff] [blame] | 682 | return false; |
| 683 | } |
| 684 | } |
| 685 | return true; |
Tyler Gunn | cb59b67 | 2014-08-20 09:02:11 -0700 | [diff] [blame] | 686 | } |
| 687 | |
Santos Cordon | 6a21264 | 2015-05-08 16:35:23 -0700 | [diff] [blame] | 688 | // |
| 689 | // Methods for retrieving PhoneAccounts and PhoneAccountHandles |
| 690 | // |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 691 | |
Santos Cordon | afe59e5 | 2014-08-22 16:48:43 -0700 | [diff] [blame] | 692 | /** |
Santos Cordon | 6a21264 | 2015-05-08 16:35:23 -0700 | [diff] [blame] | 693 | * Returns the PhoneAccount for the specified handle. Does no user checking. |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 694 | * |
Santos Cordon | 6a21264 | 2015-05-08 16:35:23 -0700 | [diff] [blame] | 695 | * @param handle |
| 696 | * @return The corresponding phone account if one exists. |
Santos Cordon | afe59e5 | 2014-08-22 16:48:43 -0700 | [diff] [blame] | 697 | */ |
Santos Cordon | 6a21264 | 2015-05-08 16:35:23 -0700 | [diff] [blame] | 698 | PhoneAccount getPhoneAccount(PhoneAccountHandle handle) { |
| 699 | for (PhoneAccount m : mState.accounts) { |
| 700 | if (Objects.equals(handle, m.getAccountHandle())) { |
| 701 | return m; |
| 702 | } |
| 703 | } |
| 704 | return null; |
| 705 | } |
| 706 | |
| 707 | /** |
| 708 | * Like getPhoneAccount, but checks to see if the current user is allowed to see the phone |
| 709 | * account before returning it. The current user is the active user on the actual android |
| 710 | * device. |
| 711 | */ |
| 712 | public PhoneAccount getPhoneAccountCheckCallingUser(PhoneAccountHandle handle) { |
| 713 | PhoneAccount account = getPhoneAccount(handle); |
| 714 | if (account != null && isVisibleForUser(account)) { |
| 715 | return account; |
| 716 | } |
| 717 | return null; |
| 718 | } |
| 719 | |
| 720 | /** |
| 721 | * Returns a list of phone account handles with the specified capabilities, uri scheme, |
| 722 | * and package name. |
| 723 | */ |
| 724 | private List<PhoneAccountHandle> getPhoneAccountHandles( |
Santos Cordon | ea5cb93 | 2015-05-07 16:28:38 -0700 | [diff] [blame] | 725 | int capabilities, |
| 726 | String uriScheme, |
| 727 | String packageName, |
| 728 | boolean includeDisabledAccounts) { |
Santos Cordon | 6a21264 | 2015-05-08 16:35:23 -0700 | [diff] [blame] | 729 | List<PhoneAccountHandle> handles = new ArrayList<>(); |
Santos Cordon | ea5cb93 | 2015-05-07 16:28:38 -0700 | [diff] [blame] | 730 | |
| 731 | for (PhoneAccount account : getPhoneAccounts( |
| 732 | capabilities, uriScheme, packageName, includeDisabledAccounts)) { |
Santos Cordon | 6a21264 | 2015-05-08 16:35:23 -0700 | [diff] [blame] | 733 | handles.add(account.getAccountHandle()); |
| 734 | } |
| 735 | return handles; |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 736 | } |
| 737 | |
| 738 | /** |
| 739 | * Returns a list of phone account handles with the specified flag, supporting the specified |
Santos Cordon | 6a21264 | 2015-05-08 16:35:23 -0700 | [diff] [blame] | 740 | * URI scheme, within the specified package name. |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 741 | * |
Santos Cordon | 6a21264 | 2015-05-08 16:35:23 -0700 | [diff] [blame] | 742 | * @param capabilities Capabilities which the {@code PhoneAccount} must have. Ignored if 0. |
| 743 | * @param uriScheme URI schemes the PhoneAccount must handle. {@code null} bypasses the |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 744 | * URI scheme check. |
Santos Cordon | 6a21264 | 2015-05-08 16:35:23 -0700 | [diff] [blame] | 745 | * @param packageName Package name of the PhoneAccount. {@code null} bypasses packageName check. |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 746 | */ |
Santos Cordon | 6a21264 | 2015-05-08 16:35:23 -0700 | [diff] [blame] | 747 | private List<PhoneAccount> getPhoneAccounts( |
Santos Cordon | ea5cb93 | 2015-05-07 16:28:38 -0700 | [diff] [blame] | 748 | int capabilities, |
| 749 | String uriScheme, |
| 750 | String packageName, |
| 751 | boolean includeDisabledAccounts) { |
Santos Cordon | 6a21264 | 2015-05-08 16:35:23 -0700 | [diff] [blame] | 752 | List<PhoneAccount> accounts = new ArrayList<>(mState.accounts.size()); |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 753 | for (PhoneAccount m : mState.accounts) { |
Santos Cordon | ea5cb93 | 2015-05-07 16:28:38 -0700 | [diff] [blame] | 754 | if (!(m.isEnabled() || includeDisabledAccounts)) { |
| 755 | // Do not include disabled accounts. |
| 756 | continue; |
| 757 | } |
| 758 | |
Santos Cordon | 6a21264 | 2015-05-08 16:35:23 -0700 | [diff] [blame] | 759 | if (capabilities != 0 && !m.hasCapabilities(capabilities)) { |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 760 | // Account doesn't have the right capabilities; skip this one. |
| 761 | continue; |
Ihab Awad | f2a8491 | 2014-07-22 21:09:25 -0700 | [diff] [blame] | 762 | } |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 763 | if (uriScheme != null && !m.supportsUriScheme(uriScheme)) { |
| 764 | // Account doesn't support this URI scheme; skip this one. |
| 765 | continue; |
| 766 | } |
Santos Cordon | 6a21264 | 2015-05-08 16:35:23 -0700 | [diff] [blame] | 767 | PhoneAccountHandle handle = m.getAccountHandle(); |
| 768 | |
| 769 | if (resolveComponent(handle).isEmpty()) { |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 770 | // This component cannot be resolved anymore; skip this one. |
| 771 | continue; |
| 772 | } |
Santos Cordon | 6a21264 | 2015-05-08 16:35:23 -0700 | [diff] [blame] | 773 | if (packageName != null && |
| 774 | !packageName.equals(handle.getComponentName().getPackageName())) { |
| 775 | // Not the right package name; skip this one. |
| 776 | continue; |
| 777 | } |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 778 | if (!isVisibleForUser(m)) { |
| 779 | // Account is not visible for the current user; skip this one. |
| 780 | continue; |
| 781 | } |
Santos Cordon | 6a21264 | 2015-05-08 16:35:23 -0700 | [diff] [blame] | 782 | accounts.add(m); |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 783 | } |
Santos Cordon | 6a21264 | 2015-05-08 16:35:23 -0700 | [diff] [blame] | 784 | return accounts; |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 785 | } |
| 786 | |
Santos Cordon | 6a21264 | 2015-05-08 16:35:23 -0700 | [diff] [blame] | 787 | // |
| 788 | // State Implementation for PhoneAccountRegistrar |
| 789 | // |
| 790 | |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 791 | /** |
| 792 | * The state of this {@code PhoneAccountRegistrar}. |
| 793 | */ |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 794 | @VisibleForTesting |
| 795 | public static class State { |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 796 | /** |
| 797 | * The account selected by the user to be employed by default for making outgoing calls. |
| 798 | * If the user has not made such a selection, then this is null. |
| 799 | */ |
| 800 | public PhoneAccountHandle defaultOutgoing = null; |
| 801 | |
| 802 | /** |
Tyler Gunn | 7cc70b4 | 2014-09-12 22:17:27 -0700 | [diff] [blame] | 803 | * The complete list of {@code PhoneAccount}s known to the Telecom subsystem. |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 804 | */ |
Shriram Ganesh | 8f89f8d | 2015-04-15 12:35:43 +0530 | [diff] [blame] | 805 | public final List<PhoneAccount> accounts = new CopyOnWriteArrayList<>(); |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 806 | |
| 807 | /** |
| 808 | * The version number of the State data. |
| 809 | */ |
| 810 | public int versionNumber; |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 811 | } |
| 812 | |
Tyler Gunn | 9787e0e | 2014-10-14 14:36:12 -0700 | [diff] [blame] | 813 | /** |
| 814 | * Dumps the state of the {@link CallsManager}. |
| 815 | * |
| 816 | * @param pw The {@code IndentingPrintWriter} to write the state to. |
| 817 | */ |
| 818 | public void dump(IndentingPrintWriter pw) { |
| 819 | if (mState != null) { |
| 820 | pw.println("xmlVersion: " + mState.versionNumber); |
| 821 | pw.println("defaultOutgoing: " + (mState.defaultOutgoing == null ? "none" : |
| 822 | mState.defaultOutgoing)); |
Santos Cordon | 59c21a7 | 2015-06-11 10:11:21 -0700 | [diff] [blame] | 823 | pw.println("simCallManager: " + getSimCallManager()); |
Tyler Gunn | 9787e0e | 2014-10-14 14:36:12 -0700 | [diff] [blame] | 824 | pw.println("phoneAccounts:"); |
| 825 | pw.increaseIndent(); |
| 826 | for (PhoneAccount phoneAccount : mState.accounts) { |
| 827 | pw.println(phoneAccount); |
| 828 | } |
| 829 | pw.decreaseIndent(); |
| 830 | } |
| 831 | } |
| 832 | |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 833 | //////////////////////////////////////////////////////////////////////////////////////////////// |
| 834 | // |
| 835 | // State management |
| 836 | // |
| 837 | |
| 838 | private void write() { |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 839 | final FileOutputStream os; |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 840 | try { |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 841 | os = mAtomicFile.startWrite(); |
| 842 | boolean success = false; |
| 843 | try { |
| 844 | XmlSerializer serializer = new FastXmlSerializer(); |
| 845 | serializer.setOutput(new BufferedOutputStream(os), "utf-8"); |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 846 | writeToXml(mState, serializer, mContext); |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 847 | serializer.flush(); |
| 848 | success = true; |
| 849 | } finally { |
| 850 | if (success) { |
| 851 | mAtomicFile.finishWrite(os); |
| 852 | } else { |
| 853 | mAtomicFile.failWrite(os); |
| 854 | } |
| 855 | } |
| 856 | } catch (IOException e) { |
| 857 | Log.e(this, e, "Writing state to XML file"); |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 858 | } |
| 859 | } |
| 860 | |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 861 | private void read() { |
| 862 | final InputStream is; |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 863 | try { |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 864 | is = mAtomicFile.openRead(); |
| 865 | } catch (FileNotFoundException ex) { |
| 866 | return; |
| 867 | } |
| 868 | |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 869 | boolean versionChanged = false; |
| 870 | |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 871 | XmlPullParser parser; |
| 872 | try { |
| 873 | parser = Xml.newPullParser(); |
| 874 | parser.setInput(new BufferedInputStream(is), null); |
| 875 | parser.nextTag(); |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 876 | mState = readFromXml(parser, mContext); |
| 877 | versionChanged = mState.versionNumber < EXPECTED_STATE_VERSION; |
| 878 | |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 879 | } catch (IOException | XmlPullParserException e) { |
| 880 | Log.e(this, e, "Reading state from XML file"); |
| 881 | mState = new State(); |
| 882 | } finally { |
| 883 | try { |
| 884 | is.close(); |
| 885 | } catch (IOException e) { |
| 886 | Log.e(this, e, "Closing InputStream"); |
| 887 | } |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 888 | } |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 889 | |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 890 | // Verify all of the UserHandles. |
| 891 | List<PhoneAccount> badAccounts = new ArrayList<>(); |
| 892 | for (PhoneAccount phoneAccount : mState.accounts) { |
| 893 | UserHandle userHandle = phoneAccount.getAccountHandle().getUserHandle(); |
| 894 | if (userHandle == null) { |
| 895 | Log.w(this, "Missing UserHandle for %s", phoneAccount); |
| 896 | badAccounts.add(phoneAccount); |
| 897 | } else if (mUserManager.getSerialNumberForUser(userHandle) == -1) { |
| 898 | Log.w(this, "User does not exist for %s", phoneAccount); |
| 899 | badAccounts.add(phoneAccount); |
| 900 | } |
| 901 | } |
| 902 | mState.accounts.removeAll(badAccounts); |
| 903 | |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 904 | // If an upgrade occurred, write out the changed data. |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 905 | if (versionChanged || !badAccounts.isEmpty()) { |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 906 | write(); |
| 907 | } |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 908 | } |
| 909 | |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 910 | private static void writeToXml(State state, XmlSerializer serializer, Context context) |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 911 | throws IOException { |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 912 | sStateXml.writeToXml(state, serializer, context); |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 913 | } |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 914 | |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 915 | private static State readFromXml(XmlPullParser parser, Context context) |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 916 | throws IOException, XmlPullParserException { |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 917 | State s = sStateXml.readFromXml(parser, 0, context); |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 918 | return s != null ? s : new State(); |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 919 | } |
| 920 | |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 921 | //////////////////////////////////////////////////////////////////////////////////////////////// |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 922 | // |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 923 | // XML serialization |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 924 | // |
| 925 | |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 926 | @VisibleForTesting |
Ihab Awad | 2692322 | 2014-07-30 10:54:35 -0700 | [diff] [blame] | 927 | public abstract static class XmlSerialization<T> { |
Tyler Gunn | 369c874 | 2015-10-07 15:39:28 -0700 | [diff] [blame^] | 928 | private static final String TAG_VALUE = "value"; |
| 929 | private static final String ATTRIBUTE_LENGTH = "length"; |
| 930 | private static final String ATTRIBUTE_KEY = "key"; |
| 931 | private static final String ATTRIBUTE_VALUE_TYPE = "type"; |
| 932 | private static final String VALUE_TYPE_STRING = "string"; |
| 933 | private static final String VALUE_TYPE_INTEGER = "integer"; |
| 934 | private static final String VALUE_TYPE_BOOLEAN = "boolean"; |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 935 | |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 936 | /** |
| 937 | * Write the supplied object to XML |
| 938 | */ |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 939 | public abstract void writeToXml(T o, XmlSerializer serializer, Context context) |
Ihab Awad | 2692322 | 2014-07-30 10:54:35 -0700 | [diff] [blame] | 940 | throws IOException; |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 941 | |
| 942 | /** |
| 943 | * Read from the supplied XML into a new object, returning null in case of an |
| 944 | * unrecoverable schema mismatch or other data error. 'parser' must be already |
| 945 | * positioned at the first tag that is expected to have been emitted by this |
| 946 | * object's writeToXml(). This object tries to fail early without modifying |
| 947 | * 'parser' if it does not recognize the data it sees. |
| 948 | */ |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 949 | public abstract T readFromXml(XmlPullParser parser, int version, Context context) |
Ihab Awad | 2692322 | 2014-07-30 10:54:35 -0700 | [diff] [blame] | 950 | throws IOException, XmlPullParserException; |
| 951 | |
Ihab Awad | d9f5438 | 2014-10-24 11:44:47 -0700 | [diff] [blame] | 952 | protected void writeTextIfNonNull(String tagName, Object value, XmlSerializer serializer) |
Ihab Awad | 2692322 | 2014-07-30 10:54:35 -0700 | [diff] [blame] | 953 | throws IOException { |
| 954 | if (value != null) { |
| 955 | serializer.startTag(null, tagName); |
| 956 | serializer.text(Objects.toString(value)); |
| 957 | serializer.endTag(null, tagName); |
| 958 | } |
| 959 | } |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 960 | |
| 961 | /** |
| 962 | * Serializes a string array. |
| 963 | * |
| 964 | * @param tagName The tag name for the string array. |
| 965 | * @param values The string values to serialize. |
| 966 | * @param serializer The serializer. |
| 967 | * @throws IOException |
| 968 | */ |
| 969 | protected void writeStringList(String tagName, List<String> values, |
| 970 | XmlSerializer serializer) |
| 971 | throws IOException { |
| 972 | |
| 973 | serializer.startTag(null, tagName); |
| 974 | if (values != null) { |
Tyler Gunn | 369c874 | 2015-10-07 15:39:28 -0700 | [diff] [blame^] | 975 | serializer.attribute(null, ATTRIBUTE_LENGTH, Objects.toString(values.size())); |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 976 | for (String toSerialize : values) { |
Tyler Gunn | 369c874 | 2015-10-07 15:39:28 -0700 | [diff] [blame^] | 977 | serializer.startTag(null, TAG_VALUE); |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 978 | if (toSerialize != null ){ |
| 979 | serializer.text(toSerialize); |
| 980 | } |
Tyler Gunn | 369c874 | 2015-10-07 15:39:28 -0700 | [diff] [blame^] | 981 | serializer.endTag(null, TAG_VALUE); |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 982 | } |
| 983 | } else { |
Tyler Gunn | 369c874 | 2015-10-07 15:39:28 -0700 | [diff] [blame^] | 984 | serializer.attribute(null, ATTRIBUTE_LENGTH, "0"); |
| 985 | } |
| 986 | serializer.endTag(null, tagName); |
| 987 | } |
| 988 | |
| 989 | protected void writeBundle(String tagName, Bundle values, XmlSerializer serializer) |
| 990 | throws IOException { |
| 991 | |
| 992 | serializer.startTag(null, tagName); |
| 993 | if (values != null) { |
| 994 | for (String key : values.keySet()) { |
| 995 | Object value = values.get(key); |
| 996 | |
| 997 | if (value == null) { |
| 998 | continue; |
| 999 | } |
| 1000 | |
| 1001 | String valueType; |
| 1002 | if (value instanceof String) { |
| 1003 | valueType = VALUE_TYPE_STRING; |
| 1004 | } else if (value instanceof Integer) { |
| 1005 | valueType = VALUE_TYPE_INTEGER; |
| 1006 | } else if (value instanceof Boolean) { |
| 1007 | valueType = VALUE_TYPE_BOOLEAN; |
| 1008 | } else { |
| 1009 | Log.w(this, |
| 1010 | "PhoneAccounts support only string, integer and boolean extras TY."); |
| 1011 | continue; |
| 1012 | } |
| 1013 | |
| 1014 | serializer.startTag(null, TAG_VALUE); |
| 1015 | serializer.attribute(null, ATTRIBUTE_KEY, key); |
| 1016 | serializer.attribute(null, ATTRIBUTE_VALUE_TYPE, valueType); |
| 1017 | serializer.text(Objects.toString(value)); |
| 1018 | serializer.endTag(null, TAG_VALUE); |
| 1019 | } |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 1020 | } |
| 1021 | serializer.endTag(null, tagName); |
Ihab Awad | d9f5438 | 2014-10-24 11:44:47 -0700 | [diff] [blame] | 1022 | } |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 1023 | |
Santos Cordon | 9c30c28 | 2015-05-13 16:28:27 -0700 | [diff] [blame] | 1024 | protected void writeIconIfNonNull(String tagName, Icon value, XmlSerializer serializer) |
Ihab Awad | d9f5438 | 2014-10-24 11:44:47 -0700 | [diff] [blame] | 1025 | throws IOException { |
Santos Cordon | 9c30c28 | 2015-05-13 16:28:27 -0700 | [diff] [blame] | 1026 | if (value != null) { |
Ihab Awad | d9f5438 | 2014-10-24 11:44:47 -0700 | [diff] [blame] | 1027 | ByteArrayOutputStream stream = new ByteArrayOutputStream(); |
Santos Cordon | 9c30c28 | 2015-05-13 16:28:27 -0700 | [diff] [blame] | 1028 | value.writeToStream(stream); |
| 1029 | byte[] iconByteArray = stream.toByteArray(); |
| 1030 | String text = Base64.encodeToString(iconByteArray, 0, iconByteArray.length, 0); |
Ihab Awad | d9f5438 | 2014-10-24 11:44:47 -0700 | [diff] [blame] | 1031 | |
| 1032 | serializer.startTag(null, tagName); |
| 1033 | serializer.text(text); |
| 1034 | serializer.endTag(null, tagName); |
| 1035 | } |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 1036 | } |
| 1037 | |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 1038 | protected void writeLong(String tagName, long value, XmlSerializer serializer) |
| 1039 | throws IOException { |
| 1040 | serializer.startTag(null, tagName); |
| 1041 | serializer.text(Long.valueOf(value).toString()); |
| 1042 | serializer.endTag(null, tagName); |
| 1043 | } |
| 1044 | |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 1045 | /** |
| 1046 | * Reads a string array from the XML parser. |
| 1047 | * |
| 1048 | * @param parser The XML parser. |
| 1049 | * @return String array containing the parsed values. |
| 1050 | * @throws IOException Exception related to IO. |
| 1051 | * @throws XmlPullParserException Exception related to parsing. |
| 1052 | */ |
| 1053 | protected List<String> readStringList(XmlPullParser parser) |
| 1054 | throws IOException, XmlPullParserException { |
| 1055 | |
Tyler Gunn | 369c874 | 2015-10-07 15:39:28 -0700 | [diff] [blame^] | 1056 | int length = Integer.parseInt(parser.getAttributeValue(null, ATTRIBUTE_LENGTH)); |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 1057 | List<String> arrayEntries = new ArrayList<String>(length); |
| 1058 | String value = null; |
| 1059 | |
| 1060 | if (length == 0) { |
| 1061 | return arrayEntries; |
| 1062 | } |
| 1063 | |
| 1064 | int outerDepth = parser.getDepth(); |
| 1065 | while (XmlUtils.nextElementWithin(parser, outerDepth)) { |
Tyler Gunn | 369c874 | 2015-10-07 15:39:28 -0700 | [diff] [blame^] | 1066 | if (parser.getName().equals(TAG_VALUE)) { |
Tyler Gunn | 8e0fef4 | 2014-09-08 18:34:44 -0700 | [diff] [blame] | 1067 | parser.next(); |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 1068 | value = parser.getText(); |
| 1069 | arrayEntries.add(value); |
| 1070 | } |
| 1071 | } |
| 1072 | |
| 1073 | return arrayEntries; |
| 1074 | } |
Ihab Awad | d9f5438 | 2014-10-24 11:44:47 -0700 | [diff] [blame] | 1075 | |
Tyler Gunn | 369c874 | 2015-10-07 15:39:28 -0700 | [diff] [blame^] | 1076 | /** |
| 1077 | * Reads a bundle from the XML parser. |
| 1078 | * |
| 1079 | * @param parser The XML parser. |
| 1080 | * @return Bundle containing the parsed values. |
| 1081 | * @throws IOException Exception related to IO. |
| 1082 | * @throws XmlPullParserException Exception related to parsing. |
| 1083 | */ |
| 1084 | protected Bundle readBundle(XmlPullParser parser) |
| 1085 | throws IOException, XmlPullParserException { |
| 1086 | |
| 1087 | Bundle bundle = null; |
| 1088 | int outerDepth = parser.getDepth(); |
| 1089 | while (XmlUtils.nextElementWithin(parser, outerDepth)) { |
| 1090 | if (parser.getName().equals(TAG_VALUE)) { |
| 1091 | String valueType = parser.getAttributeValue(null, ATTRIBUTE_VALUE_TYPE); |
| 1092 | String key = parser.getAttributeValue(null, ATTRIBUTE_KEY); |
| 1093 | parser.next(); |
| 1094 | String value = parser.getText(); |
| 1095 | |
| 1096 | if (bundle == null) { |
| 1097 | bundle = new Bundle(); |
| 1098 | } |
| 1099 | |
| 1100 | // Do not write null values to the bundle. |
| 1101 | if (value == null) { |
| 1102 | continue; |
| 1103 | } |
| 1104 | |
| 1105 | if (VALUE_TYPE_STRING.equals(valueType)) { |
| 1106 | bundle.putString(key, value); |
| 1107 | } else if (VALUE_TYPE_INTEGER.equals(valueType)) { |
| 1108 | try { |
| 1109 | int intValue = Integer.parseInt(value); |
| 1110 | bundle.putInt(key, intValue); |
| 1111 | } catch (NumberFormatException nfe) { |
| 1112 | Log.w(this, "Invalid integer PhoneAccount extra."); |
| 1113 | } |
| 1114 | } else if (VALUE_TYPE_BOOLEAN.equals(valueType)) { |
| 1115 | boolean boolValue = Boolean.parseBoolean(value); |
| 1116 | bundle.putBoolean(key, boolValue); |
| 1117 | } else { |
| 1118 | Log.w(this, "Invalid type " + valueType + " for PhoneAccount bundle."); |
| 1119 | } |
| 1120 | } |
| 1121 | } |
| 1122 | return bundle; |
| 1123 | } |
| 1124 | |
Santos Cordon | 9c30c28 | 2015-05-13 16:28:27 -0700 | [diff] [blame] | 1125 | protected Bitmap readBitmap(XmlPullParser parser) { |
Ihab Awad | d9f5438 | 2014-10-24 11:44:47 -0700 | [diff] [blame] | 1126 | byte[] imageByteArray = Base64.decode(parser.getText(), 0); |
| 1127 | return BitmapFactory.decodeByteArray(imageByteArray, 0, imageByteArray.length); |
| 1128 | } |
Santos Cordon | 9c30c28 | 2015-05-13 16:28:27 -0700 | [diff] [blame] | 1129 | |
| 1130 | protected Icon readIcon(XmlPullParser parser) throws IOException { |
| 1131 | byte[] iconByteArray = Base64.decode(parser.getText(), 0); |
| 1132 | ByteArrayInputStream stream = new ByteArrayInputStream(iconByteArray); |
| 1133 | return Icon.createFromStream(stream); |
| 1134 | } |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 1135 | } |
| 1136 | |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1137 | @VisibleForTesting |
| 1138 | public static final XmlSerialization<State> sStateXml = |
| 1139 | new XmlSerialization<State>() { |
| 1140 | private static final String CLASS_STATE = "phone_account_registrar_state"; |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 1141 | private static final String DEFAULT_OUTGOING = "default_outgoing"; |
| 1142 | private static final String ACCOUNTS = "accounts"; |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 1143 | private static final String VERSION = "version"; |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 1144 | |
| 1145 | @Override |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 1146 | public void writeToXml(State o, XmlSerializer serializer, Context context) |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1147 | throws IOException { |
Ihab Awad | 2692322 | 2014-07-30 10:54:35 -0700 | [diff] [blame] | 1148 | if (o != null) { |
| 1149 | serializer.startTag(null, CLASS_STATE); |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 1150 | serializer.attribute(null, VERSION, Objects.toString(EXPECTED_STATE_VERSION)); |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1151 | |
Ihab Awad | 2692322 | 2014-07-30 10:54:35 -0700 | [diff] [blame] | 1152 | if (o.defaultOutgoing != null) { |
| 1153 | serializer.startTag(null, DEFAULT_OUTGOING); |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 1154 | sPhoneAccountHandleXml.writeToXml(o.defaultOutgoing, serializer, context); |
Ihab Awad | 2692322 | 2014-07-30 10:54:35 -0700 | [diff] [blame] | 1155 | serializer.endTag(null, DEFAULT_OUTGOING); |
| 1156 | } |
| 1157 | |
Ihab Awad | 2692322 | 2014-07-30 10:54:35 -0700 | [diff] [blame] | 1158 | serializer.startTag(null, ACCOUNTS); |
| 1159 | for (PhoneAccount m : o.accounts) { |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 1160 | sPhoneAccountXml.writeToXml(m, serializer, context); |
Ihab Awad | 2692322 | 2014-07-30 10:54:35 -0700 | [diff] [blame] | 1161 | } |
| 1162 | serializer.endTag(null, ACCOUNTS); |
| 1163 | |
| 1164 | serializer.endTag(null, CLASS_STATE); |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 1165 | } |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 1166 | } |
| 1167 | |
| 1168 | @Override |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 1169 | public State readFromXml(XmlPullParser parser, int version, Context context) |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1170 | throws IOException, XmlPullParserException { |
| 1171 | if (parser.getName().equals(CLASS_STATE)) { |
| 1172 | State s = new State(); |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 1173 | |
| 1174 | String rawVersion = parser.getAttributeValue(null, VERSION); |
| 1175 | s.versionNumber = TextUtils.isEmpty(rawVersion) ? 1 : |
| 1176 | Integer.parseInt(rawVersion); |
| 1177 | |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1178 | int outerDepth = parser.getDepth(); |
| 1179 | while (XmlUtils.nextElementWithin(parser, outerDepth)) { |
| 1180 | if (parser.getName().equals(DEFAULT_OUTGOING)) { |
| 1181 | parser.nextTag(); |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 1182 | s.defaultOutgoing = sPhoneAccountHandleXml.readFromXml(parser, |
| 1183 | s.versionNumber, context); |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1184 | } else if (parser.getName().equals(ACCOUNTS)) { |
| 1185 | int accountsDepth = parser.getDepth(); |
| 1186 | while (XmlUtils.nextElementWithin(parser, accountsDepth)) { |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 1187 | PhoneAccount account = sPhoneAccountXml.readFromXml(parser, |
| 1188 | s.versionNumber, context); |
| 1189 | |
| 1190 | if (account != null && s.accounts != null) { |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1191 | s.accounts.add(account); |
| 1192 | } |
| 1193 | } |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 1194 | } |
| 1195 | } |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1196 | return s; |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 1197 | } |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1198 | return null; |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 1199 | } |
| 1200 | }; |
| 1201 | |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1202 | @VisibleForTesting |
| 1203 | public static final XmlSerialization<PhoneAccount> sPhoneAccountXml = |
| 1204 | new XmlSerialization<PhoneAccount>() { |
| 1205 | private static final String CLASS_PHONE_ACCOUNT = "phone_account"; |
| 1206 | private static final String ACCOUNT_HANDLE = "account_handle"; |
Andrew Lee | 7129f1c | 2014-09-04 11:55:07 -0700 | [diff] [blame] | 1207 | private static final String ADDRESS = "handle"; |
| 1208 | private static final String SUBSCRIPTION_ADDRESS = "subscription_number"; |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 1209 | private static final String CAPABILITIES = "capabilities"; |
| 1210 | private static final String ICON_RES_ID = "icon_res_id"; |
Ihab Awad | d9f5438 | 2014-10-24 11:44:47 -0700 | [diff] [blame] | 1211 | private static final String ICON_PACKAGE_NAME = "icon_package_name"; |
| 1212 | private static final String ICON_BITMAP = "icon_bitmap"; |
Ihab Awad | 7e2c7f3 | 2014-11-03 09:49:45 -0800 | [diff] [blame] | 1213 | private static final String ICON_TINT = "icon_tint"; |
| 1214 | private static final String HIGHLIGHT_COLOR = "highlight_color"; |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 1215 | private static final String LABEL = "label"; |
| 1216 | private static final String SHORT_DESCRIPTION = "short_description"; |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 1217 | private static final String SUPPORTED_URI_SCHEMES = "supported_uri_schemes"; |
Santos Cordon | 9c30c28 | 2015-05-13 16:28:27 -0700 | [diff] [blame] | 1218 | private static final String ICON = "icon"; |
Tyler Gunn | 369c874 | 2015-10-07 15:39:28 -0700 | [diff] [blame^] | 1219 | private static final String EXTRAS = "extras"; |
Santos Cordon | ea5cb93 | 2015-05-07 16:28:38 -0700 | [diff] [blame] | 1220 | private static final String ENABLED = "enabled"; |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 1221 | |
| 1222 | @Override |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 1223 | public void writeToXml(PhoneAccount o, XmlSerializer serializer, Context context) |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1224 | throws IOException { |
Ihab Awad | 2692322 | 2014-07-30 10:54:35 -0700 | [diff] [blame] | 1225 | if (o != null) { |
| 1226 | serializer.startTag(null, CLASS_PHONE_ACCOUNT); |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1227 | |
Ihab Awad | 2692322 | 2014-07-30 10:54:35 -0700 | [diff] [blame] | 1228 | if (o.getAccountHandle() != null) { |
| 1229 | serializer.startTag(null, ACCOUNT_HANDLE); |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 1230 | sPhoneAccountHandleXml.writeToXml(o.getAccountHandle(), serializer, context); |
Ihab Awad | 2692322 | 2014-07-30 10:54:35 -0700 | [diff] [blame] | 1231 | serializer.endTag(null, ACCOUNT_HANDLE); |
| 1232 | } |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1233 | |
Ihab Awad | d9f5438 | 2014-10-24 11:44:47 -0700 | [diff] [blame] | 1234 | writeTextIfNonNull(ADDRESS, o.getAddress(), serializer); |
| 1235 | writeTextIfNonNull(SUBSCRIPTION_ADDRESS, o.getSubscriptionAddress(), serializer); |
| 1236 | writeTextIfNonNull(CAPABILITIES, Integer.toString(o.getCapabilities()), serializer); |
Santos Cordon | 9c30c28 | 2015-05-13 16:28:27 -0700 | [diff] [blame] | 1237 | writeIconIfNonNull(ICON, o.getIcon(), serializer); |
Ihab Awad | 7e2c7f3 | 2014-11-03 09:49:45 -0800 | [diff] [blame] | 1238 | writeTextIfNonNull(HIGHLIGHT_COLOR, |
| 1239 | Integer.toString(o.getHighlightColor()), serializer); |
Ihab Awad | d9f5438 | 2014-10-24 11:44:47 -0700 | [diff] [blame] | 1240 | writeTextIfNonNull(LABEL, o.getLabel(), serializer); |
| 1241 | writeTextIfNonNull(SHORT_DESCRIPTION, o.getShortDescription(), serializer); |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 1242 | writeStringList(SUPPORTED_URI_SCHEMES, o.getSupportedUriSchemes(), serializer); |
Tyler Gunn | 369c874 | 2015-10-07 15:39:28 -0700 | [diff] [blame^] | 1243 | writeBundle(EXTRAS, o.getExtras(), serializer); |
Santos Cordon | ea5cb93 | 2015-05-07 16:28:38 -0700 | [diff] [blame] | 1244 | writeTextIfNonNull(ENABLED, o.isEnabled() ? "true" : "false" , serializer); |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1245 | |
Ihab Awad | 2692322 | 2014-07-30 10:54:35 -0700 | [diff] [blame] | 1246 | serializer.endTag(null, CLASS_PHONE_ACCOUNT); |
| 1247 | } |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 1248 | } |
| 1249 | |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 1250 | public PhoneAccount readFromXml(XmlPullParser parser, int version, Context context) |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1251 | throws IOException, XmlPullParserException { |
| 1252 | if (parser.getName().equals(CLASS_PHONE_ACCOUNT)) { |
| 1253 | int outerDepth = parser.getDepth(); |
| 1254 | PhoneAccountHandle accountHandle = null; |
Andrew Lee | 7129f1c | 2014-09-04 11:55:07 -0700 | [diff] [blame] | 1255 | Uri address = null; |
| 1256 | Uri subscriptionAddress = null; |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1257 | int capabilities = 0; |
Ihab Awad | 7e2c7f3 | 2014-11-03 09:49:45 -0800 | [diff] [blame] | 1258 | int iconResId = PhoneAccount.NO_RESOURCE_ID; |
Ihab Awad | d9f5438 | 2014-10-24 11:44:47 -0700 | [diff] [blame] | 1259 | String iconPackageName = null; |
Ihab Awad | 7e2c7f3 | 2014-11-03 09:49:45 -0800 | [diff] [blame] | 1260 | Bitmap iconBitmap = null; |
Ihab Awad | 07bc5ee | 2014-11-12 13:42:52 -0800 | [diff] [blame] | 1261 | int iconTint = PhoneAccount.NO_ICON_TINT; |
| 1262 | int highlightColor = PhoneAccount.NO_HIGHLIGHT_COLOR; |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1263 | String label = null; |
| 1264 | String shortDescription = null; |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 1265 | List<String> supportedUriSchemes = null; |
Santos Cordon | 9c30c28 | 2015-05-13 16:28:27 -0700 | [diff] [blame] | 1266 | Icon icon = null; |
Santos Cordon | ea5cb93 | 2015-05-07 16:28:38 -0700 | [diff] [blame] | 1267 | boolean enabled = false; |
Tyler Gunn | 369c874 | 2015-10-07 15:39:28 -0700 | [diff] [blame^] | 1268 | Bundle extras = null; |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1269 | |
| 1270 | while (XmlUtils.nextElementWithin(parser, outerDepth)) { |
| 1271 | if (parser.getName().equals(ACCOUNT_HANDLE)) { |
| 1272 | parser.nextTag(); |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 1273 | accountHandle = sPhoneAccountHandleXml.readFromXml(parser, version, |
| 1274 | context); |
Andrew Lee | 7129f1c | 2014-09-04 11:55:07 -0700 | [diff] [blame] | 1275 | } else if (parser.getName().equals(ADDRESS)) { |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1276 | parser.next(); |
Andrew Lee | 7129f1c | 2014-09-04 11:55:07 -0700 | [diff] [blame] | 1277 | address = Uri.parse(parser.getText()); |
| 1278 | } else if (parser.getName().equals(SUBSCRIPTION_ADDRESS)) { |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1279 | parser.next(); |
Andrew Lee | 7129f1c | 2014-09-04 11:55:07 -0700 | [diff] [blame] | 1280 | String nextText = parser.getText(); |
| 1281 | subscriptionAddress = nextText == null ? null : Uri.parse(nextText); |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1282 | } else if (parser.getName().equals(CAPABILITIES)) { |
| 1283 | parser.next(); |
| 1284 | capabilities = Integer.parseInt(parser.getText()); |
| 1285 | } else if (parser.getName().equals(ICON_RES_ID)) { |
| 1286 | parser.next(); |
| 1287 | iconResId = Integer.parseInt(parser.getText()); |
Ihab Awad | d9f5438 | 2014-10-24 11:44:47 -0700 | [diff] [blame] | 1288 | } else if (parser.getName().equals(ICON_PACKAGE_NAME)) { |
| 1289 | parser.next(); |
| 1290 | iconPackageName = parser.getText(); |
| 1291 | } else if (parser.getName().equals(ICON_BITMAP)) { |
| 1292 | parser.next(); |
Ihab Awad | 7e2c7f3 | 2014-11-03 09:49:45 -0800 | [diff] [blame] | 1293 | iconBitmap = readBitmap(parser); |
| 1294 | } else if (parser.getName().equals(ICON_TINT)) { |
Nancy Chen | 06ce062 | 2014-10-23 01:17:35 +0000 | [diff] [blame] | 1295 | parser.next(); |
Ihab Awad | 7e2c7f3 | 2014-11-03 09:49:45 -0800 | [diff] [blame] | 1296 | iconTint = Integer.parseInt(parser.getText()); |
| 1297 | } else if (parser.getName().equals(HIGHLIGHT_COLOR)) { |
| 1298 | parser.next(); |
| 1299 | highlightColor = Integer.parseInt(parser.getText()); |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1300 | } else if (parser.getName().equals(LABEL)) { |
| 1301 | parser.next(); |
| 1302 | label = parser.getText(); |
| 1303 | } else if (parser.getName().equals(SHORT_DESCRIPTION)) { |
| 1304 | parser.next(); |
| 1305 | shortDescription = parser.getText(); |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 1306 | } else if (parser.getName().equals(SUPPORTED_URI_SCHEMES)) { |
| 1307 | supportedUriSchemes = readStringList(parser); |
Santos Cordon | 9c30c28 | 2015-05-13 16:28:27 -0700 | [diff] [blame] | 1308 | } else if (parser.getName().equals(ICON)) { |
| 1309 | parser.next(); |
| 1310 | icon = readIcon(parser); |
Santos Cordon | ea5cb93 | 2015-05-07 16:28:38 -0700 | [diff] [blame] | 1311 | } else if (parser.getName().equals(ENABLED)) { |
| 1312 | parser.next(); |
| 1313 | enabled = "true".equalsIgnoreCase(parser.getText()); |
Tyler Gunn | 369c874 | 2015-10-07 15:39:28 -0700 | [diff] [blame^] | 1314 | } else if (parser.getName().equals(EXTRAS)) { |
| 1315 | parser.next(); |
| 1316 | extras = readBundle(parser); |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1317 | } |
| 1318 | } |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 1319 | |
Santos Cordon | a9eebe4 | 2015-06-11 14:07:44 -0700 | [diff] [blame] | 1320 | ComponentName pstnComponentName = new ComponentName("com.android.phone", |
| 1321 | "com.android.services.telephony.TelephonyConnectionService"); |
Santos Cordon | a82aed8 | 2015-05-26 10:43:56 -0700 | [diff] [blame] | 1322 | ComponentName sipComponentName = new ComponentName("com.android.phone", |
| 1323 | "com.android.services.telephony.sip.SipConnectionService"); |
| 1324 | |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 1325 | // Upgrade older phone accounts to specify the supported URI schemes. |
| 1326 | if (version < 2) { |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 1327 | supportedUriSchemes = new ArrayList<>(); |
| 1328 | |
| 1329 | // Handle the SIP connection service. |
| 1330 | // Check the system settings to see if it also should handle "tel" calls. |
| 1331 | if (accountHandle.getComponentName().equals(sipComponentName)) { |
| 1332 | boolean useSipForPstn = useSipForPstnCalls(context); |
| 1333 | supportedUriSchemes.add(PhoneAccount.SCHEME_SIP); |
| 1334 | if (useSipForPstn) { |
| 1335 | supportedUriSchemes.add(PhoneAccount.SCHEME_TEL); |
| 1336 | } |
| 1337 | } else { |
| 1338 | supportedUriSchemes.add(PhoneAccount.SCHEME_TEL); |
| 1339 | supportedUriSchemes.add(PhoneAccount.SCHEME_VOICEMAIL); |
| 1340 | } |
| 1341 | } |
| 1342 | |
Ihab Awad | 7e2c7f3 | 2014-11-03 09:49:45 -0800 | [diff] [blame] | 1343 | // Upgrade older phone accounts with explicit package name |
| 1344 | if (version < 5) { |
| 1345 | if (iconBitmap == null) { |
| 1346 | iconPackageName = accountHandle.getComponentName().getPackageName(); |
| 1347 | } |
| 1348 | } |
| 1349 | |
Santos Cordon | a82aed8 | 2015-05-26 10:43:56 -0700 | [diff] [blame] | 1350 | if (version < 6) { |
| 1351 | // Always enable all SIP accounts on upgrade to version 6 |
| 1352 | if (accountHandle.getComponentName().equals(sipComponentName)) { |
| 1353 | enabled = true; |
| 1354 | } |
| 1355 | } |
Santos Cordon | a9eebe4 | 2015-06-11 14:07:44 -0700 | [diff] [blame] | 1356 | if (version < 7) { |
| 1357 | // Always enabled all PSTN acocunts on upgrade to version 7 |
| 1358 | if (accountHandle.getComponentName().equals(pstnComponentName)) { |
| 1359 | enabled = true; |
| 1360 | } |
| 1361 | } |
Roshan Pius | 6f752c8 | 2015-08-10 10:42:06 -0700 | [diff] [blame] | 1362 | if (version < 8) { |
| 1363 | // Migrate the SIP account handle ids to use SIP username instead of SIP URI. |
| 1364 | if (accountHandle.getComponentName().equals(sipComponentName)) { |
| 1365 | Uri accountUri = Uri.parse(accountHandle.getId()); |
| 1366 | if (accountUri.getScheme() != null && |
| 1367 | accountUri.getScheme().equals(PhoneAccount.SCHEME_SIP)) { |
| 1368 | accountHandle = new PhoneAccountHandle(accountHandle.getComponentName(), |
| 1369 | accountUri.getSchemeSpecificPart(), |
| 1370 | accountHandle.getUserHandle()); |
| 1371 | } |
| 1372 | } |
| 1373 | } |
Santos Cordon | a82aed8 | 2015-05-26 10:43:56 -0700 | [diff] [blame] | 1374 | |
Ihab Awad | 7e2c7f3 | 2014-11-03 09:49:45 -0800 | [diff] [blame] | 1375 | PhoneAccount.Builder builder = PhoneAccount.builder(accountHandle, label) |
Andrew Lee | 7129f1c | 2014-09-04 11:55:07 -0700 | [diff] [blame] | 1376 | .setAddress(address) |
| 1377 | .setSubscriptionAddress(subscriptionAddress) |
| 1378 | .setCapabilities(capabilities) |
Andrew Lee | 7129f1c | 2014-09-04 11:55:07 -0700 | [diff] [blame] | 1379 | .setShortDescription(shortDescription) |
| 1380 | .setSupportedUriSchemes(supportedUriSchemes) |
Santos Cordon | ea5cb93 | 2015-05-07 16:28:38 -0700 | [diff] [blame] | 1381 | .setHighlightColor(highlightColor) |
Tyler Gunn | 369c874 | 2015-10-07 15:39:28 -0700 | [diff] [blame^] | 1382 | .setExtras(extras) |
Santos Cordon | ea5cb93 | 2015-05-07 16:28:38 -0700 | [diff] [blame] | 1383 | .setIsEnabled(enabled); |
Ihab Awad | 7e2c7f3 | 2014-11-03 09:49:45 -0800 | [diff] [blame] | 1384 | |
Santos Cordon | 9c30c28 | 2015-05-13 16:28:27 -0700 | [diff] [blame] | 1385 | if (icon != null) { |
| 1386 | builder.setIcon(icon); |
| 1387 | } else if (iconBitmap != null) { |
| 1388 | builder.setIcon(Icon.createWithBitmap(iconBitmap)); |
| 1389 | } else if (!TextUtils.isEmpty(iconPackageName)) { |
| 1390 | builder.setIcon(Icon.createWithResource(iconPackageName, iconResId)); |
| 1391 | // TODO: Need to set tint. |
Ihab Awad | 7e2c7f3 | 2014-11-03 09:49:45 -0800 | [diff] [blame] | 1392 | } |
| 1393 | |
Ihab Awad | 0a4b95f | 2015-05-18 10:15:38 -0700 | [diff] [blame] | 1394 | return builder.build(); |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1395 | } |
| 1396 | return null; |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 1397 | } |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 1398 | |
| 1399 | /** |
Santos Cordon | 9c30c28 | 2015-05-13 16:28:27 -0700 | [diff] [blame] | 1400 | * Determines if the SIP call settings specify to use SIP for all calls, including PSTN |
| 1401 | * calls. |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 1402 | * |
| 1403 | * @param context The context. |
| 1404 | * @return {@code True} if SIP should be used for all calls. |
| 1405 | */ |
| 1406 | private boolean useSipForPstnCalls(Context context) { |
| 1407 | String option = Settings.System.getString(context.getContentResolver(), |
| 1408 | Settings.System.SIP_CALL_OPTIONS); |
| 1409 | option = (option != null) ? option : Settings.System.SIP_ADDRESS_ONLY; |
| 1410 | return option.equals(Settings.System.SIP_ALWAYS); |
| 1411 | } |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 1412 | }; |
| 1413 | |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1414 | @VisibleForTesting |
| 1415 | public static final XmlSerialization<PhoneAccountHandle> sPhoneAccountHandleXml = |
| 1416 | new XmlSerialization<PhoneAccountHandle>() { |
| 1417 | private static final String CLASS_PHONE_ACCOUNT_HANDLE = "phone_account_handle"; |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 1418 | private static final String COMPONENT_NAME = "component_name"; |
| 1419 | private static final String ID = "id"; |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 1420 | private static final String USER_SERIAL_NUMBER = "user_serial_number"; |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 1421 | |
| 1422 | @Override |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 1423 | public void writeToXml(PhoneAccountHandle o, XmlSerializer serializer, Context context) |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1424 | throws IOException { |
Ihab Awad | 2692322 | 2014-07-30 10:54:35 -0700 | [diff] [blame] | 1425 | if (o != null) { |
| 1426 | serializer.startTag(null, CLASS_PHONE_ACCOUNT_HANDLE); |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1427 | |
Ihab Awad | 2692322 | 2014-07-30 10:54:35 -0700 | [diff] [blame] | 1428 | if (o.getComponentName() != null) { |
Ihab Awad | d9f5438 | 2014-10-24 11:44:47 -0700 | [diff] [blame] | 1429 | writeTextIfNonNull( |
Ihab Awad | 2692322 | 2014-07-30 10:54:35 -0700 | [diff] [blame] | 1430 | COMPONENT_NAME, o.getComponentName().flattenToString(), serializer); |
| 1431 | } |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1432 | |
Ihab Awad | d9f5438 | 2014-10-24 11:44:47 -0700 | [diff] [blame] | 1433 | writeTextIfNonNull(ID, o.getId(), serializer); |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1434 | |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 1435 | if (o.getUserHandle() != null && context != null) { |
| 1436 | UserManager userManager = UserManager.get(context); |
| 1437 | writeLong(USER_SERIAL_NUMBER, |
| 1438 | userManager.getSerialNumberForUser(o.getUserHandle()), serializer); |
| 1439 | } |
| 1440 | |
Ihab Awad | 2692322 | 2014-07-30 10:54:35 -0700 | [diff] [blame] | 1441 | serializer.endTag(null, CLASS_PHONE_ACCOUNT_HANDLE); |
| 1442 | } |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 1443 | } |
| 1444 | |
| 1445 | @Override |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 1446 | public PhoneAccountHandle readFromXml(XmlPullParser parser, int version, Context context) |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1447 | throws IOException, XmlPullParserException { |
| 1448 | if (parser.getName().equals(CLASS_PHONE_ACCOUNT_HANDLE)) { |
| 1449 | String componentNameString = null; |
| 1450 | String idString = null; |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 1451 | String userSerialNumberString = null; |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1452 | int outerDepth = parser.getDepth(); |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 1453 | |
| 1454 | UserManager userManager = UserManager.get(context); |
| 1455 | |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1456 | while (XmlUtils.nextElementWithin(parser, outerDepth)) { |
| 1457 | if (parser.getName().equals(COMPONENT_NAME)) { |
| 1458 | parser.next(); |
| 1459 | componentNameString = parser.getText(); |
| 1460 | } else if (parser.getName().equals(ID)) { |
| 1461 | parser.next(); |
| 1462 | idString = parser.getText(); |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 1463 | } else if (parser.getName().equals(USER_SERIAL_NUMBER)) { |
| 1464 | parser.next(); |
| 1465 | userSerialNumberString = parser.getText(); |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1466 | } |
| 1467 | } |
Ihab Awad | 2692322 | 2014-07-30 10:54:35 -0700 | [diff] [blame] | 1468 | if (componentNameString != null) { |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 1469 | UserHandle userHandle = null; |
| 1470 | if (userSerialNumberString != null) { |
| 1471 | try { |
| 1472 | long serialNumber = Long.parseLong(userSerialNumberString); |
| 1473 | userHandle = userManager.getUserForSerialNumber(serialNumber); |
| 1474 | } catch (NumberFormatException e) { |
| 1475 | Log.e(this, e, "Could not parse UserHandle " + userSerialNumberString); |
| 1476 | } |
| 1477 | } |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1478 | return new PhoneAccountHandle( |
| 1479 | ComponentName.unflattenFromString(componentNameString), |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 1480 | idString, |
| 1481 | userHandle); |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1482 | } |
| 1483 | } |
| 1484 | return null; |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 1485 | } |
| 1486 | }; |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 1487 | } |