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 | |
Tyler Gunn | cb59b67 | 2014-08-20 09:02:11 -0700 | [diff] [blame] | 19 | import android.Manifest; |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 20 | import android.content.ComponentName; |
| 21 | import android.content.Context; |
Evan Charlton | af51ceb | 2014-07-30 11:56:36 -0700 | [diff] [blame] | 22 | import android.content.Intent; |
| 23 | import android.content.pm.PackageManager; |
| 24 | import android.content.pm.ResolveInfo; |
Tyler Gunn | cb59b67 | 2014-08-20 09:02:11 -0700 | [diff] [blame] | 25 | import android.content.pm.ServiceInfo; |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 26 | import android.content.pm.UserInfo; |
Ihab Awad | d9f5438 | 2014-10-24 11:44:47 -0700 | [diff] [blame] | 27 | import android.graphics.Bitmap; |
| 28 | import android.graphics.BitmapFactory; |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 29 | import android.net.Uri; |
| 30 | import android.os.Process; |
| 31 | import android.os.UserHandle; |
| 32 | import android.os.UserManager; |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 33 | import android.provider.Settings; |
Tyler Gunn | 7cc70b4 | 2014-09-12 22:17:27 -0700 | [diff] [blame] | 34 | import android.telecom.ConnectionService; |
| 35 | import android.telecom.PhoneAccount; |
| 36 | import android.telecom.PhoneAccountHandle; |
Nancy Chen | 140004a | 2014-10-15 15:48:38 -0700 | [diff] [blame] | 37 | import android.telephony.PhoneNumberUtils; |
Nancy Chen | 5a36b6e | 2014-10-23 17:42:42 -0700 | [diff] [blame] | 38 | import android.telephony.SubscriptionManager; |
Evan Charlton | af51ceb | 2014-07-30 11:56:36 -0700 | [diff] [blame] | 39 | import android.text.TextUtils; |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 40 | import android.util.AtomicFile; |
Ihab Awad | d9f5438 | 2014-10-24 11:44:47 -0700 | [diff] [blame] | 41 | import android.util.Base64; |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 42 | import android.util.Xml; |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 43 | |
Tyler Gunn | 91d43cf | 2014-09-17 12:19:39 -0700 | [diff] [blame] | 44 | // TODO: Needed for move to system service: import com.android.internal.R; |
Sailesh Nepal | 0e1dc5a | 2014-07-30 11:08:54 -0700 | [diff] [blame] | 45 | import com.android.internal.annotations.VisibleForTesting; |
| 46 | import com.android.internal.util.FastXmlSerializer; |
Tyler Gunn | 9787e0e | 2014-10-14 14:36:12 -0700 | [diff] [blame] | 47 | import com.android.internal.util.IndentingPrintWriter; |
Sailesh Nepal | 0e1dc5a | 2014-07-30 11:08:54 -0700 | [diff] [blame] | 48 | import com.android.internal.util.XmlUtils; |
| 49 | |
Evan Charlton | af51ceb | 2014-07-30 11:56:36 -0700 | [diff] [blame] | 50 | import org.xmlpull.v1.XmlPullParser; |
| 51 | import org.xmlpull.v1.XmlPullParserException; |
| 52 | import org.xmlpull.v1.XmlSerializer; |
| 53 | |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 54 | import java.io.BufferedInputStream; |
| 55 | import java.io.BufferedOutputStream; |
Ihab Awad | d9f5438 | 2014-10-24 11:44:47 -0700 | [diff] [blame] | 56 | import java.io.ByteArrayOutputStream; |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 57 | import java.io.File; |
| 58 | import java.io.FileNotFoundException; |
| 59 | import java.io.FileOutputStream; |
| 60 | import java.io.IOException; |
| 61 | import java.io.InputStream; |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 62 | import java.lang.Integer; |
Tyler Gunn | cb59b67 | 2014-08-20 09:02:11 -0700 | [diff] [blame] | 63 | import java.lang.SecurityException; |
Tyler Gunn | d900ce6 | 2014-08-13 11:40:59 -0700 | [diff] [blame] | 64 | import java.lang.String; |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 65 | import java.util.ArrayList; |
Tyler Gunn | d900ce6 | 2014-08-13 11:40:59 -0700 | [diff] [blame] | 66 | import java.util.Iterator; |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 67 | import java.util.List; |
| 68 | import java.util.Objects; |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 69 | import java.util.concurrent.CopyOnWriteArrayList; |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 70 | |
| 71 | /** |
Evan Charlton | 8917637 | 2014-07-19 18:23:09 -0700 | [diff] [blame] | 72 | * Handles writing and reading PhoneAccountHandle registration entries. This is a simple verbatim |
Tyler Gunn | 7cc70b4 | 2014-09-12 22:17:27 -0700 | [diff] [blame] | 73 | * delegate for all the account handling methods on {@link android.telecom.TelecomManager} as implemented in |
| 74 | * {@link TelecomServiceImpl}, with the notable exception that {@link TelecomServiceImpl} is |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 75 | * responsible for security checking to make sure that the caller has proper authority over |
Evan Charlton | 8917637 | 2014-07-19 18:23:09 -0700 | [diff] [blame] | 76 | * the {@code ComponentName}s they are declaring in their {@code PhoneAccountHandle}s. |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 77 | */ |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 78 | public final class PhoneAccountRegistrar { |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 79 | |
Yorke Lee | 5e8836a | 2014-08-22 15:25:18 -0700 | [diff] [blame] | 80 | public static final PhoneAccountHandle NO_ACCOUNT_SELECTED = |
| 81 | new PhoneAccountHandle(new ComponentName("null", "null"), "NO_ACCOUNT_SELECTED"); |
| 82 | |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 83 | public abstract static class Listener { |
| 84 | public void onAccountsChanged(PhoneAccountRegistrar registrar) {} |
| 85 | public void onDefaultOutgoingChanged(PhoneAccountRegistrar registrar) {} |
| 86 | public void onSimCallManagerChanged(PhoneAccountRegistrar registrar) {} |
| 87 | } |
| 88 | |
| 89 | private static final String FILE_NAME = "phone-account-registrar-state.xml"; |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 90 | @VisibleForTesting |
Ihab Awad | 7e2c7f3 | 2014-11-03 09:49:45 -0800 | [diff] [blame] | 91 | public static final int EXPECTED_STATE_VERSION = 5; |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 92 | |
| 93 | /** Keep in sync with the same in SipSettings.java */ |
| 94 | private static final String SIP_SHARED_PREFERENCES = "SIP_PREFERENCES"; |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 95 | |
| 96 | private final List<Listener> mListeners = new CopyOnWriteArrayList<>(); |
| 97 | private final AtomicFile mAtomicFile; |
Santos Cordon | afe59e5 | 2014-08-22 16:48:43 -0700 | [diff] [blame] | 98 | private final Context mContext; |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 99 | private final UserManager mUserManager; |
Wink Saville | 33c05d3 | 2014-11-20 13:04:17 -0800 | [diff] [blame] | 100 | private final SubscriptionManager mSubscriptionManager; |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 101 | private State mState; |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 102 | private UserHandle mCurrentUserHandle; |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 103 | |
Nancy Chen | 06ce062 | 2014-10-23 01:17:35 +0000 | [diff] [blame] | 104 | @VisibleForTesting |
Ihab Awad | 2692322 | 2014-07-30 10:54:35 -0700 | [diff] [blame] | 105 | public PhoneAccountRegistrar(Context context) { |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 106 | this(context, FILE_NAME); |
| 107 | } |
| 108 | |
| 109 | @VisibleForTesting |
| 110 | public PhoneAccountRegistrar(Context context, String fileName) { |
Tyler Gunn | 91d43cf | 2014-09-17 12:19:39 -0700 | [diff] [blame] | 111 | // TODO: This file path is subject to change -- it is storing the phone account registry |
| 112 | // state file in the path /data/system/users/0/, which is likely not correct in a |
| 113 | // multi-user setting. |
| 114 | /** UNCOMMENT_FOR_MOVE_TO_SYSTEM_SERVICE |
| 115 | String filePath = Environment.getUserSystemDirectory(UserHandle.myUserId()). |
| 116 | getAbsolutePath(); |
| 117 | mAtomicFile = new AtomicFile(new File(filePath, fileName)); |
| 118 | UNCOMMENT_FOR_MOVE_TO_SYSTEM_SERVICE */ |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 119 | mAtomicFile = new AtomicFile(new File(context.getFilesDir(), fileName)); |
Tyler Gunn | 91d43cf | 2014-09-17 12:19:39 -0700 | [diff] [blame] | 120 | |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 121 | mState = new State(); |
Santos Cordon | afe59e5 | 2014-08-22 16:48:43 -0700 | [diff] [blame] | 122 | mContext = context; |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 123 | mUserManager = UserManager.get(context); |
Wink Saville | 33c05d3 | 2014-11-20 13:04:17 -0800 | [diff] [blame] | 124 | mSubscriptionManager = SubscriptionManager.from(mContext); |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 125 | mCurrentUserHandle = Process.myUserHandle(); |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 126 | read(); |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 127 | } |
| 128 | |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 129 | /** |
Nancy Chen | 140004a | 2014-10-15 15:48:38 -0700 | [diff] [blame] | 130 | * Retrieves the subscription id for a given phone account if it exists. Subscription ids |
| 131 | * apply only to PSTN/SIM card phone accounts so all other accounts should not have a |
| 132 | * subscription id. |
| 133 | * @param accountHandle The handle for the phone account for which to retrieve the |
| 134 | * subscription id. |
Wink Saville | 3585060 | 2014-10-23 15:57:21 -0700 | [diff] [blame] | 135 | * @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] | 136 | */ |
Wink Saville | 3585060 | 2014-10-23 15:57:21 -0700 | [diff] [blame] | 137 | public int getSubscriptionIdForPhoneAccount(PhoneAccountHandle accountHandle) { |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 138 | PhoneAccount account = getPhoneAccountInternal(accountHandle); |
| 139 | if (account == null |
| 140 | || !account.hasCapabilities(PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION) |
| 141 | || !TextUtils.isDigitsOnly(accountHandle.getId()) |
| 142 | || !isVisibleForUser(accountHandle)) { |
Nancy Chen | 140004a | 2014-10-15 15:48:38 -0700 | [diff] [blame] | 143 | // Since no decimals or negative numbers can be valid subscription ids, only a string of |
| 144 | // numbers can be subscription id |
Wink Saville | 33c05d3 | 2014-11-20 13:04:17 -0800 | [diff] [blame] | 145 | return SubscriptionManager.INVALID_SUBSCRIPTION_ID; |
Nancy Chen | 140004a | 2014-10-15 15:48:38 -0700 | [diff] [blame] | 146 | } |
Wink Saville | 3585060 | 2014-10-23 15:57:21 -0700 | [diff] [blame] | 147 | return Integer.parseInt(accountHandle.getId()); |
Nancy Chen | 140004a | 2014-10-15 15:48:38 -0700 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | /** |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 151 | * Retrieves the default outgoing phone account supporting the specified uriScheme. Note that if |
| 152 | * {@link #mCurrentUserHandle} does not have visibility into the current default, {@code null} |
| 153 | * will be returned. |
| 154 | * |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 155 | * @param uriScheme The URI scheme for the outgoing call. |
| 156 | * @return The {@link PhoneAccountHandle} to use. |
| 157 | */ |
| 158 | public PhoneAccountHandle getDefaultOutgoingPhoneAccount(String uriScheme) { |
Yorke Lee | 5e8836a | 2014-08-22 15:25:18 -0700 | [diff] [blame] | 159 | final PhoneAccountHandle userSelected = getUserSelectedOutgoingPhoneAccount(); |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 160 | |
Yorke Lee | 5e8836a | 2014-08-22 15:25:18 -0700 | [diff] [blame] | 161 | if (userSelected != null) { |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 162 | // If there is a default PhoneAccount, ensure it supports calls to handles with the |
| 163 | // specified uriScheme. |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 164 | final PhoneAccount userSelectedAccount = getPhoneAccountInternal(userSelected); |
| 165 | if (userSelectedAccount.supportsUriScheme(uriScheme) |
| 166 | && isVisibleForUser(userSelected)) { |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 167 | return userSelected; |
| 168 | } |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 169 | } |
| 170 | |
Nancy Chen | 309198e | 2014-09-15 18:02:49 -0700 | [diff] [blame] | 171 | List<PhoneAccountHandle> outgoing = getCallCapablePhoneAccounts(uriScheme); |
Ihab Awad | 6fb37c8 | 2014-08-07 19:48:57 -0700 | [diff] [blame] | 172 | switch (outgoing.size()) { |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 173 | case 0: |
| 174 | // There are no accounts, so there can be no default |
| 175 | return null; |
| 176 | case 1: |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 177 | // There is only one account, which is by definition the default. |
| 178 | PhoneAccountHandle onlyHandle = outgoing.get(0); |
| 179 | if (isVisibleForUser(onlyHandle)) { |
| 180 | return outgoing.get(0); |
| 181 | } |
| 182 | return null; |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 183 | default: |
| 184 | // There are multiple accounts with no selected default |
| 185 | return null; |
Ihab Awad | f2a8491 | 2014-07-22 21:09:25 -0700 | [diff] [blame] | 186 | } |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 187 | } |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 188 | |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 189 | /** |
| 190 | * @return The user-selected outgoing {@link PhoneAccount}, or null if it hasn't been set (or |
| 191 | * if it was set by another user). |
| 192 | */ |
Yorke Lee | 5e8836a | 2014-08-22 15:25:18 -0700 | [diff] [blame] | 193 | PhoneAccountHandle getUserSelectedOutgoingPhoneAccount() { |
| 194 | if (mState.defaultOutgoing != null) { |
| 195 | // Return the registered outgoing default iff it still exists (we keep a sticky |
| 196 | // default to survive account deletion and re-addition) |
| 197 | for (int i = 0; i < mState.accounts.size(); i++) { |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 198 | if (mState.accounts.get(i).getAccountHandle().equals(mState.defaultOutgoing) |
| 199 | && isVisibleForUser(mState.defaultOutgoing)) { |
Yorke Lee | 5e8836a | 2014-08-22 15:25:18 -0700 | [diff] [blame] | 200 | return mState.defaultOutgoing; |
| 201 | } |
| 202 | } |
| 203 | // At this point, there was a registered default but it has been deleted; proceed |
| 204 | // as though there were no default |
| 205 | } |
| 206 | return null; |
| 207 | } |
| 208 | |
Andrew Lee | a51a386 | 2014-09-03 14:58:45 -0700 | [diff] [blame] | 209 | public void setUserSelectedOutgoingPhoneAccount(PhoneAccountHandle accountHandle) { |
Evan Charlton | 8917637 | 2014-07-19 18:23:09 -0700 | [diff] [blame] | 210 | if (accountHandle == null) { |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 211 | // Asking to clear the default outgoing is a valid request |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 212 | mState.defaultOutgoing = null; |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 213 | } else { |
| 214 | boolean found = false; |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 215 | for (PhoneAccount m : mState.accounts) { |
Evan Charlton | 94d0162 | 2014-07-20 12:32:05 -0700 | [diff] [blame] | 216 | if (Objects.equals(accountHandle, m.getAccountHandle())) { |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 217 | found = true; |
| 218 | break; |
| 219 | } |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 220 | } |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 221 | |
| 222 | if (!found) { |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 223 | Log.w(this, "Trying to set nonexistent default outgoing %s", |
| 224 | accountHandle); |
| 225 | return; |
| 226 | } |
| 227 | |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 228 | if (!getPhoneAccountInternal(accountHandle).hasCapabilities( |
Tyler Gunn | 8e0fef4 | 2014-09-08 18:34:44 -0700 | [diff] [blame] | 229 | PhoneAccount.CAPABILITY_CALL_PROVIDER)) { |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 230 | Log.w(this, "Trying to set non-call-provider default outgoing %s", |
Evan Charlton | 8917637 | 2014-07-19 18:23:09 -0700 | [diff] [blame] | 231 | accountHandle); |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 232 | return; |
| 233 | } |
| 234 | |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 235 | if (getPhoneAccountInternal(accountHandle).hasCapabilities( |
Nancy Chen | 5a36b6e | 2014-10-23 17:42:42 -0700 | [diff] [blame] | 236 | PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION)) { |
| 237 | // If the account selected is a SIM account, propagate down to the subscription |
| 238 | // record. |
Wink Saville | 7ce6e78 | 2014-10-27 10:56:46 -0700 | [diff] [blame] | 239 | int subId = getSubscriptionIdForPhoneAccount(accountHandle); |
Wink Saville | 33c05d3 | 2014-11-20 13:04:17 -0800 | [diff] [blame] | 240 | mSubscriptionManager.setDefaultVoiceSubId(subId); |
Nancy Chen | 5a36b6e | 2014-10-23 17:42:42 -0700 | [diff] [blame] | 241 | } |
| 242 | |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 243 | mState.defaultOutgoing = accountHandle; |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 244 | } |
| 245 | |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 246 | write(); |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 247 | fireDefaultOutgoingChanged(); |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 248 | } |
| 249 | |
Nancy Chen | 668dee0 | 2014-11-19 15:31:31 -0800 | [diff] [blame] | 250 | boolean isUserSelectedSmsPhoneAccount(PhoneAccountHandle accountHandle) { |
| 251 | return getSubscriptionIdForPhoneAccount(accountHandle) == |
| 252 | SubscriptionManager.getDefaultSmsSubId(); |
| 253 | } |
| 254 | |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 255 | public void setSimCallManager(PhoneAccountHandle callManager) { |
| 256 | if (callManager != null) { |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 257 | PhoneAccount callManagerAccount = getPhoneAccountInternal(callManager); |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 258 | if (callManagerAccount == null) { |
| 259 | Log.d(this, "setSimCallManager: Nonexistent call manager: %s", callManager); |
| 260 | return; |
Tyler Gunn | 8e0fef4 | 2014-09-08 18:34:44 -0700 | [diff] [blame] | 261 | } else if (!callManagerAccount.hasCapabilities( |
| 262 | PhoneAccount.CAPABILITY_CONNECTION_MANAGER)) { |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 263 | Log.d(this, "setSimCallManager: Not a call manager: %s", callManagerAccount); |
| 264 | return; |
| 265 | } |
Yorke Lee | 5e8836a | 2014-08-22 15:25:18 -0700 | [diff] [blame] | 266 | } else { |
| 267 | callManager = NO_ACCOUNT_SELECTED; |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 268 | } |
| 269 | mState.simCallManager = callManager; |
Yorke Lee | 5e8836a | 2014-08-22 15:25:18 -0700 | [diff] [blame] | 270 | |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 271 | write(); |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 272 | fireSimCallManagerChanged(); |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 273 | } |
| 274 | |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 275 | /** |
| 276 | * @return The {@link PhoneAccount}s which are visible to {@link #mCurrentUserHandle}. |
| 277 | */ |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 278 | public PhoneAccountHandle getSimCallManager() { |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 279 | if (mState.simCallManager != null) { |
Yorke Lee | 5e8836a | 2014-08-22 15:25:18 -0700 | [diff] [blame] | 280 | if (NO_ACCOUNT_SELECTED.equals(mState.simCallManager)) { |
| 281 | return null; |
| 282 | } |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 283 | // Return the registered sim call manager iff it still exists (we keep a sticky |
| 284 | // setting to survive account deletion and re-addition) |
| 285 | for (int i = 0; i < mState.accounts.size(); i++) { |
mike dooley | 10a5831 | 2014-11-06 13:46:19 -0800 | [diff] [blame] | 286 | if (mState.accounts.get(i).getAccountHandle().equals(mState.simCallManager) |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 287 | && !resolveComponent(mState.simCallManager).isEmpty() |
| 288 | && isVisibleForUser(mState.simCallManager)) { |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 289 | return mState.simCallManager; |
| 290 | } |
| 291 | } |
| 292 | } |
Evan Charlton | af51ceb | 2014-07-30 11:56:36 -0700 | [diff] [blame] | 293 | |
| 294 | // See if the OEM has specified a default one. |
Evan Charlton | af51ceb | 2014-07-30 11:56:36 -0700 | [diff] [blame] | 295 | String defaultConnectionMgr = |
Santos Cordon | afe59e5 | 2014-08-22 16:48:43 -0700 | [diff] [blame] | 296 | mContext.getResources().getString(R.string.default_connection_manager_component); |
Evan Charlton | af51ceb | 2014-07-30 11:56:36 -0700 | [diff] [blame] | 297 | if (!TextUtils.isEmpty(defaultConnectionMgr)) { |
Evan Charlton | af51ceb | 2014-07-30 11:56:36 -0700 | [diff] [blame] | 298 | ComponentName componentName = ComponentName.unflattenFromString(defaultConnectionMgr); |
Evan Charlton | af51ceb | 2014-07-30 11:56:36 -0700 | [diff] [blame] | 299 | // Make sure that the component can be resolved. |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 300 | List<ResolveInfo> resolveInfos = resolveComponent(componentName, null); |
Evan Charlton | af51ceb | 2014-07-30 11:56:36 -0700 | [diff] [blame] | 301 | if (!resolveInfos.isEmpty()) { |
| 302 | // See if there is registered PhoneAccount by this component. |
| 303 | List<PhoneAccountHandle> handles = getAllPhoneAccountHandles(); |
| 304 | for (PhoneAccountHandle handle : handles) { |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 305 | if (componentName.equals(handle.getComponentName()) |
| 306 | && isVisibleForUser(handle)) { |
Evan Charlton | af51ceb | 2014-07-30 11:56:36 -0700 | [diff] [blame] | 307 | return handle; |
| 308 | } |
| 309 | } |
| 310 | Log.d(this, "%s does not have a PhoneAccount; not using as default", componentName); |
| 311 | } else { |
| 312 | Log.d(this, "%s could not be resolved; not using as default", componentName); |
| 313 | } |
| 314 | } else { |
| 315 | Log.v(this, "No default connection manager specified"); |
| 316 | } |
| 317 | |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 318 | return null; |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 319 | } |
| 320 | |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 321 | /** |
| 322 | * A version of {@link #getPhoneAccount} which does not guard for the current user. |
| 323 | * |
| 324 | * @param handle |
| 325 | * @return |
| 326 | */ |
| 327 | PhoneAccount getPhoneAccountInternal(PhoneAccountHandle handle) { |
| 328 | for (PhoneAccount m : mState.accounts) { |
| 329 | if (Objects.equals(handle, m.getAccountHandle())) { |
| 330 | return m; |
| 331 | } |
| 332 | } |
| 333 | return null; |
| 334 | } |
| 335 | |
| 336 | /** |
| 337 | * Update the current UserHandle to track when users are switched. This will allow the |
| 338 | * PhoneAccountRegistar to self-filter the PhoneAccounts to make sure we don't leak anything |
| 339 | * across users. |
| 340 | * |
| 341 | * @param userHandle The {@link UserHandle}, as delivered by |
| 342 | * {@link Intent#ACTION_USER_SWITCHED}. |
| 343 | */ |
| 344 | public void setCurrentUserHandle(UserHandle userHandle) { |
| 345 | if (userHandle == null) { |
| 346 | Log.d(this, "setCurrentUserHandle, userHandle = null"); |
| 347 | userHandle = Process.myUserHandle(); |
| 348 | } |
| 349 | Log.d(this, "setCurrentUserHandle, %s", userHandle); |
| 350 | mCurrentUserHandle = userHandle; |
| 351 | } |
| 352 | |
| 353 | private boolean isVisibleForUser(PhoneAccountHandle accountHandle) { |
| 354 | if (accountHandle == null) { |
| 355 | return false; |
| 356 | } |
| 357 | |
| 358 | return isVisibleForUser(getPhoneAccountInternal(accountHandle)); |
| 359 | } |
| 360 | |
| 361 | private boolean isVisibleForUser(PhoneAccount account) { |
| 362 | if (account == null) { |
| 363 | return false; |
| 364 | } |
| 365 | |
| 366 | // If this PhoneAccount has CAPABILITY_MULTI_USER, it should be visible to all users and |
| 367 | // all profiles. Only Telephony and SIP accounts should have this capability. |
| 368 | if (account.hasCapabilities(PhoneAccount.CAPABILITY_MULTI_USER)) { |
| 369 | return true; |
| 370 | } |
| 371 | |
| 372 | UserHandle phoneAccountUserHandle = account.getAccountHandle().getUserHandle(); |
| 373 | if (phoneAccountUserHandle == null) { |
| 374 | return false; |
| 375 | } |
| 376 | |
| 377 | if (mCurrentUserHandle == null) { |
| 378 | Log.d(this, "Current user is null; assuming true"); |
| 379 | return true; |
| 380 | } |
| 381 | |
| 382 | // Unlike in TelecomServiceImpl, we only care about *profiles* here. We want to make sure |
| 383 | // that we don't resolve PhoneAccount across *users*, but resolving across *profiles* is |
| 384 | // fine. |
| 385 | List<UserInfo> profileUsers = mUserManager.getProfiles(mCurrentUserHandle.getIdentifier()); |
| 386 | |
| 387 | for (UserInfo profileInfo : profileUsers) { |
| 388 | if (profileInfo.getUserHandle().equals(phoneAccountUserHandle)) { |
| 389 | return true; |
| 390 | } |
| 391 | } |
| 392 | return false; |
| 393 | } |
| 394 | |
| 395 | private List<ResolveInfo> resolveComponent(PhoneAccountHandle phoneAccountHandle) { |
| 396 | return resolveComponent(phoneAccountHandle.getComponentName(), |
| 397 | phoneAccountHandle.getUserHandle()); |
| 398 | } |
| 399 | |
| 400 | private List<ResolveInfo> resolveComponent(ComponentName componentName, |
| 401 | UserHandle userHandle) { |
mike dooley | 10a5831 | 2014-11-06 13:46:19 -0800 | [diff] [blame] | 402 | PackageManager pm = mContext.getPackageManager(); |
| 403 | Intent intent = new Intent(ConnectionService.SERVICE_INTERFACE); |
| 404 | intent.setComponent(componentName); |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 405 | if (userHandle != null) { |
| 406 | return pm.queryIntentServicesAsUser(intent, 0, userHandle.getIdentifier()); |
| 407 | } else { |
| 408 | return pm.queryIntentServices(intent, 0); |
| 409 | } |
mike dooley | 10a5831 | 2014-11-06 13:46:19 -0800 | [diff] [blame] | 410 | } |
| 411 | |
Tyler Gunn | 8e0fef4 | 2014-09-08 18:34:44 -0700 | [diff] [blame] | 412 | /** |
| 413 | * Retrieves a list of all {@link PhoneAccountHandle}s registered. |
| 414 | * |
| 415 | * @return The list of {@link PhoneAccountHandle}s. |
| 416 | */ |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 417 | public List<PhoneAccountHandle> getAllPhoneAccountHandles() { |
| 418 | List<PhoneAccountHandle> accountHandles = new ArrayList<>(); |
| 419 | for (PhoneAccount m : mState.accounts) { |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 420 | if (isVisibleForUser(m)) { |
| 421 | accountHandles.add(m.getAccountHandle()); |
| 422 | } |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 423 | } |
| 424 | return accountHandles; |
| 425 | } |
| 426 | |
| 427 | public List<PhoneAccount> getAllPhoneAccounts() { |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 428 | List<PhoneAccount> accounts = new ArrayList<>(mState.accounts.size()); |
| 429 | for (PhoneAccount account : mState.accounts) { |
| 430 | if (isVisibleForUser(account)) { |
| 431 | accounts.add(account); |
| 432 | } |
| 433 | } |
| 434 | return accounts; |
Tyler Gunn | 8e0fef4 | 2014-09-08 18:34:44 -0700 | [diff] [blame] | 435 | } |
| 436 | |
| 437 | /** |
Nancy Chen | 309198e | 2014-09-15 18:02:49 -0700 | [diff] [blame] | 438 | * Retrieves a list of all call provider phone accounts. |
Tyler Gunn | 8e0fef4 | 2014-09-08 18:34:44 -0700 | [diff] [blame] | 439 | * |
| 440 | * @return The phone account handles. |
| 441 | */ |
Nancy Chen | 309198e | 2014-09-15 18:02:49 -0700 | [diff] [blame] | 442 | public List<PhoneAccountHandle> getCallCapablePhoneAccounts() { |
Santos Cordon | afe59e5 | 2014-08-22 16:48:43 -0700 | [diff] [blame] | 443 | return getPhoneAccountHandles(PhoneAccount.CAPABILITY_CALL_PROVIDER); |
| 444 | } |
| 445 | |
Tyler Gunn | 8e0fef4 | 2014-09-08 18:34:44 -0700 | [diff] [blame] | 446 | /** |
Nancy Chen | 309198e | 2014-09-15 18:02:49 -0700 | [diff] [blame] | 447 | * 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] | 448 | * specified URI scheme. |
| 449 | * |
| 450 | * @param uriScheme The URI scheme. |
| 451 | * @return The phone account handles. |
| 452 | */ |
Nancy Chen | 309198e | 2014-09-15 18:02:49 -0700 | [diff] [blame] | 453 | public List<PhoneAccountHandle> getCallCapablePhoneAccounts(String uriScheme) { |
| 454 | return getPhoneAccountHandles(PhoneAccount.CAPABILITY_CALL_PROVIDER, uriScheme); |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 455 | } |
| 456 | |
Tyler Gunn | 8e0fef4 | 2014-09-08 18:34:44 -0700 | [diff] [blame] | 457 | /** |
Nancy Chen | 1c5926f | 2014-09-17 14:44:14 -0700 | [diff] [blame] | 458 | * Retrieves a list of all phone accounts registered by a specified package. |
| 459 | * |
| 460 | * @param packageName The name of the package that registered the phone accounts. |
| 461 | * @return The phone account handles. |
| 462 | */ |
| 463 | public List<PhoneAccountHandle> getPhoneAccountsForPackage(String packageName) { |
| 464 | List<PhoneAccountHandle> accountHandles = new ArrayList<>(); |
| 465 | for (PhoneAccount m : mState.accounts) { |
| 466 | if (Objects.equals( |
| 467 | packageName, |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 468 | m.getAccountHandle().getComponentName().getPackageName()) |
| 469 | && isVisibleForUser(m)) { |
Nancy Chen | 1c5926f | 2014-09-17 14:44:14 -0700 | [diff] [blame] | 470 | accountHandles.add(m.getAccountHandle()); |
| 471 | } |
| 472 | } |
| 473 | return accountHandles; |
| 474 | } |
| 475 | |
| 476 | /** |
Nancy Chen | 309198e | 2014-09-15 18:02:49 -0700 | [diff] [blame] | 477 | * Retrieves a list of all phone account handles with the connection manager capability. |
Tyler Gunn | 8e0fef4 | 2014-09-08 18:34:44 -0700 | [diff] [blame] | 478 | * |
| 479 | * @return The phone account handles. |
| 480 | */ |
| 481 | public List<PhoneAccountHandle> getConnectionManagerPhoneAccounts() { |
Santos Cordon | e3d8245 | 2014-09-15 13:44:29 -0700 | [diff] [blame] | 482 | return getPhoneAccountHandles(PhoneAccount.CAPABILITY_CONNECTION_MANAGER, |
Nancy Chen | 309198e | 2014-09-15 18:02:49 -0700 | [diff] [blame] | 483 | null /* supportedUriScheme */); |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 484 | } |
| 485 | |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 486 | public PhoneAccount getPhoneAccount(PhoneAccountHandle handle) { |
| 487 | for (PhoneAccount m : mState.accounts) { |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 488 | if (Objects.equals(handle, m.getAccountHandle()) |
| 489 | && isVisibleForUser(m)) { |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 490 | return m; |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 491 | } |
| 492 | } |
| 493 | return null; |
| 494 | } |
| 495 | |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 496 | // TODO: Should we implement an artificial limit for # of accounts associated with a single |
| 497 | // ComponentName? |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 498 | public void registerPhoneAccount(PhoneAccount account) { |
Tyler Gunn | cb59b67 | 2014-08-20 09:02:11 -0700 | [diff] [blame] | 499 | // Enforce the requirement that a connection service for a phone account has the correct |
| 500 | // permission. |
| 501 | if (!phoneAccountHasPermission(account.getAccountHandle())) { |
| 502 | Log.w(this, "Phone account %s does not have BIND_CONNECTION_SERVICE permission.", |
| 503 | account.getAccountHandle()); |
| 504 | throw new SecurityException( |
| 505 | "PhoneAccount connection service requires BIND_CONNECTION_SERVICE permission."); |
| 506 | } |
| 507 | |
Tyler Gunn | 8e0fef4 | 2014-09-08 18:34:44 -0700 | [diff] [blame] | 508 | addOrReplacePhoneAccount(account); |
| 509 | } |
| 510 | |
| 511 | /** |
| 512 | * Adds a {@code PhoneAccount}, replacing an existing one if found. |
| 513 | * |
| 514 | * @param account The {@code PhoneAccount} to add or replace. |
| 515 | */ |
| 516 | private void addOrReplacePhoneAccount(PhoneAccount account) { |
Ihab Awad | 7e2c7f3 | 2014-11-03 09:49:45 -0800 | [diff] [blame] | 517 | Log.d(this, "addOrReplacePhoneAccount(%s -> %s)", |
| 518 | account.getAccountHandle(), account); |
| 519 | |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 520 | mState.accounts.add(account); |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 521 | // Search for duplicates and remove any that are found. |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 522 | for (int i = 0; i < mState.accounts.size() - 1; i++) { |
| 523 | if (Objects.equals( |
| 524 | account.getAccountHandle(), mState.accounts.get(i).getAccountHandle())) { |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 525 | // replace existing entry. |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 526 | mState.accounts.remove(i); |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 527 | break; |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 528 | } |
| 529 | } |
| 530 | |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 531 | write(); |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 532 | fireAccountsChanged(); |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 533 | } |
| 534 | |
Evan Charlton | 8917637 | 2014-07-19 18:23:09 -0700 | [diff] [blame] | 535 | public void unregisterPhoneAccount(PhoneAccountHandle accountHandle) { |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 536 | for (int i = 0; i < mState.accounts.size(); i++) { |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 537 | PhoneAccountHandle handle = mState.accounts.get(i).getAccountHandle(); |
| 538 | if (Objects.equals(accountHandle, handle)) { |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 539 | mState.accounts.remove(i); |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 540 | break; |
| 541 | } |
| 542 | } |
| 543 | |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 544 | write(); |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 545 | fireAccountsChanged(); |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 546 | } |
| 547 | |
Tyler Gunn | d900ce6 | 2014-08-13 11:40:59 -0700 | [diff] [blame] | 548 | /** |
| 549 | * Un-registers all phone accounts associated with a specified package. |
| 550 | * |
| 551 | * @param packageName The package for which phone accounts will be removed. |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 552 | * @param userHandle The {@link UserHandle} the package is running under. |
Tyler Gunn | d900ce6 | 2014-08-13 11:40:59 -0700 | [diff] [blame] | 553 | */ |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 554 | public void clearAccounts(String packageName, UserHandle userHandle) { |
Tyler Gunn | d900ce6 | 2014-08-13 11:40:59 -0700 | [diff] [blame] | 555 | boolean accountsRemoved = false; |
| 556 | Iterator<PhoneAccount> it = mState.accounts.iterator(); |
| 557 | while (it.hasNext()) { |
| 558 | PhoneAccount phoneAccount = it.next(); |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 559 | PhoneAccountHandle handle = phoneAccount.getAccountHandle(); |
| 560 | if (Objects.equals(packageName, handle.getComponentName().getPackageName()) |
| 561 | && Objects.equals(userHandle, handle.getUserHandle())) { |
Tyler Gunn | d900ce6 | 2014-08-13 11:40:59 -0700 | [diff] [blame] | 562 | Log.i(this, "Removing phone account " + phoneAccount.getLabel()); |
| 563 | it.remove(); |
| 564 | accountsRemoved = true; |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 565 | } |
| 566 | } |
| 567 | |
Tyler Gunn | d900ce6 | 2014-08-13 11:40:59 -0700 | [diff] [blame] | 568 | if (accountsRemoved) { |
| 569 | write(); |
| 570 | fireAccountsChanged(); |
| 571 | } |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 572 | } |
| 573 | |
Nancy Chen | 140004a | 2014-10-15 15:48:38 -0700 | [diff] [blame] | 574 | public boolean isVoiceMailNumber(PhoneAccountHandle accountHandle, String number) { |
Wink Saville | 3585060 | 2014-10-23 15:57:21 -0700 | [diff] [blame] | 575 | int subId = getSubscriptionIdForPhoneAccount(accountHandle); |
Nancy Chen | 140004a | 2014-10-15 15:48:38 -0700 | [diff] [blame] | 576 | return PhoneNumberUtils.isVoiceMailNumber(subId, number); |
| 577 | } |
| 578 | |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 579 | public void addListener(Listener l) { |
| 580 | mListeners.add(l); |
| 581 | } |
| 582 | |
| 583 | public void removeListener(Listener l) { |
Jay Shrauner | a82c8f7 | 2014-08-14 15:49:16 -0700 | [diff] [blame] | 584 | if (l != null) { |
| 585 | mListeners.remove(l); |
| 586 | } |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 587 | } |
| 588 | |
| 589 | private void fireAccountsChanged() { |
| 590 | for (Listener l : mListeners) { |
| 591 | l.onAccountsChanged(this); |
| 592 | } |
| 593 | } |
| 594 | |
| 595 | private void fireDefaultOutgoingChanged() { |
| 596 | for (Listener l : mListeners) { |
| 597 | l.onDefaultOutgoingChanged(this); |
| 598 | } |
| 599 | } |
| 600 | |
| 601 | private void fireSimCallManagerChanged() { |
| 602 | for (Listener l : mListeners) { |
| 603 | l.onSimCallManagerChanged(this); |
| 604 | } |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 605 | } |
| 606 | |
Tyler Gunn | cb59b67 | 2014-08-20 09:02:11 -0700 | [diff] [blame] | 607 | /** |
| 608 | * Determines if the connection service specified by a {@link PhoneAccountHandle} has the |
| 609 | * {@link Manifest.permission#BIND_CONNECTION_SERVICE} permission. |
| 610 | * |
| 611 | * @param phoneAccountHandle The phone account to check. |
| 612 | * @return {@code True} if the phone account has permission. |
| 613 | */ |
| 614 | public boolean phoneAccountHasPermission(PhoneAccountHandle phoneAccountHandle) { |
Tyler Gunn | 91d43cf | 2014-09-17 12:19:39 -0700 | [diff] [blame] | 615 | PackageManager packageManager = mContext.getPackageManager(); |
Tyler Gunn | cb59b67 | 2014-08-20 09:02:11 -0700 | [diff] [blame] | 616 | try { |
| 617 | ServiceInfo serviceInfo = packageManager.getServiceInfo( |
| 618 | phoneAccountHandle.getComponentName(), 0); |
| 619 | |
| 620 | return serviceInfo.permission != null && |
| 621 | serviceInfo.permission.equals(Manifest.permission.BIND_CONNECTION_SERVICE); |
| 622 | } catch (PackageManager.NameNotFoundException e) { |
| 623 | Log.w(this, "Name not found %s", e); |
| 624 | return false; |
| 625 | } |
| 626 | } |
| 627 | |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 628 | //////////////////////////////////////////////////////////////////////////////////////////////// |
| 629 | |
Santos Cordon | afe59e5 | 2014-08-22 16:48:43 -0700 | [diff] [blame] | 630 | /** |
| 631 | * Returns a list of phone account handles with the specified flag. |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 632 | * |
| 633 | * @param flags Flags which the {@code PhoneAccount} must have. |
Santos Cordon | afe59e5 | 2014-08-22 16:48:43 -0700 | [diff] [blame] | 634 | */ |
| 635 | private List<PhoneAccountHandle> getPhoneAccountHandles(int flags) { |
Nancy Chen | 309198e | 2014-09-15 18:02:49 -0700 | [diff] [blame] | 636 | return getPhoneAccountHandles(flags, null); |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 637 | } |
| 638 | |
| 639 | /** |
| 640 | * Returns a list of phone account handles with the specified flag, supporting the specified |
Nancy Chen | 309198e | 2014-09-15 18:02:49 -0700 | [diff] [blame] | 641 | * URI scheme. |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 642 | * |
| 643 | * @param flags Flags which the {@code PhoneAccount} must have. |
| 644 | * @param uriScheme URI schemes the PhoneAccount must handle. {@code Null} bypasses the |
| 645 | * URI scheme check. |
| 646 | */ |
Nancy Chen | 309198e | 2014-09-15 18:02:49 -0700 | [diff] [blame] | 647 | private List<PhoneAccountHandle> getPhoneAccountHandles(int flags, String uriScheme) { |
Evan Charlton | 94d0162 | 2014-07-20 12:32:05 -0700 | [diff] [blame] | 648 | List<PhoneAccountHandle> accountHandles = new ArrayList<>(); |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 649 | for (PhoneAccount m : mState.accounts) { |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 650 | if (!m.hasCapabilities(flags)) { |
| 651 | // Account doesn't have the right capabilities; skip this one. |
| 652 | continue; |
Ihab Awad | f2a8491 | 2014-07-22 21:09:25 -0700 | [diff] [blame] | 653 | } |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 654 | if (uriScheme != null && !m.supportsUriScheme(uriScheme)) { |
| 655 | // Account doesn't support this URI scheme; skip this one. |
| 656 | continue; |
| 657 | } |
| 658 | if (resolveComponent(m.getAccountHandle()).isEmpty()) { |
| 659 | // This component cannot be resolved anymore; skip this one. |
| 660 | continue; |
| 661 | } |
| 662 | if (!isVisibleForUser(m)) { |
| 663 | // Account is not visible for the current user; skip this one. |
| 664 | continue; |
| 665 | } |
| 666 | accountHandles.add(m.getAccountHandle()); |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 667 | } |
Evan Charlton | 94d0162 | 2014-07-20 12:32:05 -0700 | [diff] [blame] | 668 | return accountHandles; |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 669 | } |
| 670 | |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 671 | /** |
| 672 | * The state of this {@code PhoneAccountRegistrar}. |
| 673 | */ |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 674 | @VisibleForTesting |
| 675 | public static class State { |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 676 | /** |
| 677 | * The account selected by the user to be employed by default for making outgoing calls. |
| 678 | * If the user has not made such a selection, then this is null. |
| 679 | */ |
| 680 | public PhoneAccountHandle defaultOutgoing = null; |
| 681 | |
| 682 | /** |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 683 | * A {@code PhoneAccount} having {@link PhoneAccount#CAPABILITY_CONNECTION_MANAGER} which |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 684 | * manages and optimizes a user's PSTN SIM connections. |
| 685 | */ |
| 686 | public PhoneAccountHandle simCallManager; |
| 687 | |
| 688 | /** |
Tyler Gunn | 7cc70b4 | 2014-09-12 22:17:27 -0700 | [diff] [blame] | 689 | * The complete list of {@code PhoneAccount}s known to the Telecom subsystem. |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 690 | */ |
| 691 | public final List<PhoneAccount> accounts = new ArrayList<>(); |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 692 | |
| 693 | /** |
| 694 | * The version number of the State data. |
| 695 | */ |
| 696 | public int versionNumber; |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 697 | } |
| 698 | |
Tyler Gunn | 9787e0e | 2014-10-14 14:36:12 -0700 | [diff] [blame] | 699 | /** |
| 700 | * Dumps the state of the {@link CallsManager}. |
| 701 | * |
| 702 | * @param pw The {@code IndentingPrintWriter} to write the state to. |
| 703 | */ |
| 704 | public void dump(IndentingPrintWriter pw) { |
| 705 | if (mState != null) { |
| 706 | pw.println("xmlVersion: " + mState.versionNumber); |
| 707 | pw.println("defaultOutgoing: " + (mState.defaultOutgoing == null ? "none" : |
| 708 | mState.defaultOutgoing)); |
| 709 | pw.println("simCallManager: " + (mState.simCallManager == null ? "none" : |
| 710 | mState.simCallManager)); |
| 711 | pw.println("phoneAccounts:"); |
| 712 | pw.increaseIndent(); |
| 713 | for (PhoneAccount phoneAccount : mState.accounts) { |
| 714 | pw.println(phoneAccount); |
| 715 | } |
| 716 | pw.decreaseIndent(); |
| 717 | } |
| 718 | } |
| 719 | |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 720 | //////////////////////////////////////////////////////////////////////////////////////////////// |
| 721 | // |
| 722 | // State management |
| 723 | // |
| 724 | |
| 725 | private void write() { |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 726 | final FileOutputStream os; |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 727 | try { |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 728 | os = mAtomicFile.startWrite(); |
| 729 | boolean success = false; |
| 730 | try { |
| 731 | XmlSerializer serializer = new FastXmlSerializer(); |
| 732 | serializer.setOutput(new BufferedOutputStream(os), "utf-8"); |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 733 | writeToXml(mState, serializer, mContext); |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 734 | serializer.flush(); |
| 735 | success = true; |
| 736 | } finally { |
| 737 | if (success) { |
| 738 | mAtomicFile.finishWrite(os); |
| 739 | } else { |
| 740 | mAtomicFile.failWrite(os); |
| 741 | } |
| 742 | } |
| 743 | } catch (IOException e) { |
| 744 | Log.e(this, e, "Writing state to XML file"); |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 745 | } |
| 746 | } |
| 747 | |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 748 | private void read() { |
| 749 | final InputStream is; |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 750 | try { |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 751 | is = mAtomicFile.openRead(); |
| 752 | } catch (FileNotFoundException ex) { |
| 753 | return; |
| 754 | } |
| 755 | |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 756 | boolean versionChanged = false; |
| 757 | |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 758 | XmlPullParser parser; |
| 759 | try { |
| 760 | parser = Xml.newPullParser(); |
| 761 | parser.setInput(new BufferedInputStream(is), null); |
| 762 | parser.nextTag(); |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 763 | mState = readFromXml(parser, mContext); |
| 764 | versionChanged = mState.versionNumber < EXPECTED_STATE_VERSION; |
| 765 | |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 766 | } catch (IOException | XmlPullParserException e) { |
| 767 | Log.e(this, e, "Reading state from XML file"); |
| 768 | mState = new State(); |
| 769 | } finally { |
| 770 | try { |
| 771 | is.close(); |
| 772 | } catch (IOException e) { |
| 773 | Log.e(this, e, "Closing InputStream"); |
| 774 | } |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 775 | } |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 776 | |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 777 | // Verify all of the UserHandles. |
| 778 | List<PhoneAccount> badAccounts = new ArrayList<>(); |
| 779 | for (PhoneAccount phoneAccount : mState.accounts) { |
| 780 | UserHandle userHandle = phoneAccount.getAccountHandle().getUserHandle(); |
| 781 | if (userHandle == null) { |
| 782 | Log.w(this, "Missing UserHandle for %s", phoneAccount); |
| 783 | badAccounts.add(phoneAccount); |
| 784 | } else if (mUserManager.getSerialNumberForUser(userHandle) == -1) { |
| 785 | Log.w(this, "User does not exist for %s", phoneAccount); |
| 786 | badAccounts.add(phoneAccount); |
| 787 | } |
| 788 | } |
| 789 | mState.accounts.removeAll(badAccounts); |
| 790 | |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 791 | // If an upgrade occurred, write out the changed data. |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 792 | if (versionChanged || !badAccounts.isEmpty()) { |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 793 | write(); |
| 794 | } |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 795 | } |
| 796 | |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 797 | private static void writeToXml(State state, XmlSerializer serializer, Context context) |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 798 | throws IOException { |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 799 | sStateXml.writeToXml(state, serializer, context); |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 800 | } |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 801 | |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 802 | private static State readFromXml(XmlPullParser parser, Context context) |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 803 | throws IOException, XmlPullParserException { |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 804 | State s = sStateXml.readFromXml(parser, 0, context); |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 805 | return s != null ? s : new State(); |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 806 | } |
| 807 | |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 808 | //////////////////////////////////////////////////////////////////////////////////////////////// |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 809 | // |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 810 | // XML serialization |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 811 | // |
| 812 | |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 813 | @VisibleForTesting |
Ihab Awad | 2692322 | 2014-07-30 10:54:35 -0700 | [diff] [blame] | 814 | public abstract static class XmlSerialization<T> { |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 815 | private static final String LENGTH_ATTRIBUTE = "length"; |
| 816 | private static final String VALUE_TAG = "value"; |
| 817 | |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 818 | /** |
| 819 | * Write the supplied object to XML |
| 820 | */ |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 821 | public abstract void writeToXml(T o, XmlSerializer serializer, Context context) |
Ihab Awad | 2692322 | 2014-07-30 10:54:35 -0700 | [diff] [blame] | 822 | throws IOException; |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 823 | |
| 824 | /** |
| 825 | * Read from the supplied XML into a new object, returning null in case of an |
| 826 | * unrecoverable schema mismatch or other data error. 'parser' must be already |
| 827 | * positioned at the first tag that is expected to have been emitted by this |
| 828 | * object's writeToXml(). This object tries to fail early without modifying |
| 829 | * 'parser' if it does not recognize the data it sees. |
| 830 | */ |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 831 | public abstract T readFromXml(XmlPullParser parser, int version, Context context) |
Ihab Awad | 2692322 | 2014-07-30 10:54:35 -0700 | [diff] [blame] | 832 | throws IOException, XmlPullParserException; |
| 833 | |
Ihab Awad | d9f5438 | 2014-10-24 11:44:47 -0700 | [diff] [blame] | 834 | protected void writeTextIfNonNull(String tagName, Object value, XmlSerializer serializer) |
Ihab Awad | 2692322 | 2014-07-30 10:54:35 -0700 | [diff] [blame] | 835 | throws IOException { |
| 836 | if (value != null) { |
| 837 | serializer.startTag(null, tagName); |
| 838 | serializer.text(Objects.toString(value)); |
| 839 | serializer.endTag(null, tagName); |
| 840 | } |
| 841 | } |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 842 | |
| 843 | /** |
| 844 | * Serializes a string array. |
| 845 | * |
| 846 | * @param tagName The tag name for the string array. |
| 847 | * @param values The string values to serialize. |
| 848 | * @param serializer The serializer. |
| 849 | * @throws IOException |
| 850 | */ |
| 851 | protected void writeStringList(String tagName, List<String> values, |
| 852 | XmlSerializer serializer) |
| 853 | throws IOException { |
| 854 | |
| 855 | serializer.startTag(null, tagName); |
| 856 | if (values != null) { |
| 857 | serializer.attribute(null, LENGTH_ATTRIBUTE, Objects.toString(values.size())); |
| 858 | for (String toSerialize : values) { |
| 859 | serializer.startTag(null, VALUE_TAG); |
| 860 | if (toSerialize != null ){ |
| 861 | serializer.text(toSerialize); |
| 862 | } |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 863 | serializer.endTag(null, VALUE_TAG); |
| 864 | } |
| 865 | } else { |
| 866 | serializer.attribute(null, LENGTH_ATTRIBUTE, "0"); |
| 867 | } |
| 868 | serializer.endTag(null, tagName); |
Ihab Awad | d9f5438 | 2014-10-24 11:44:47 -0700 | [diff] [blame] | 869 | } |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 870 | |
Ihab Awad | d9f5438 | 2014-10-24 11:44:47 -0700 | [diff] [blame] | 871 | protected void writeBitmapIfNonNull(String tagName, Bitmap value, XmlSerializer serializer) |
| 872 | throws IOException { |
| 873 | if (value != null && value.getByteCount() > 0) { |
| 874 | ByteArrayOutputStream stream = new ByteArrayOutputStream(); |
| 875 | value.compress(Bitmap.CompressFormat.PNG, 100, stream); |
| 876 | byte[] imageByteArray = stream.toByteArray(); |
| 877 | String text = Base64.encodeToString(imageByteArray, 0, imageByteArray.length, 0); |
| 878 | |
| 879 | serializer.startTag(null, tagName); |
| 880 | serializer.text(text); |
| 881 | serializer.endTag(null, tagName); |
| 882 | } |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 883 | } |
| 884 | |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 885 | protected void writeLong(String tagName, long value, XmlSerializer serializer) |
| 886 | throws IOException { |
| 887 | serializer.startTag(null, tagName); |
| 888 | serializer.text(Long.valueOf(value).toString()); |
| 889 | serializer.endTag(null, tagName); |
| 890 | } |
| 891 | |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 892 | /** |
| 893 | * Reads a string array from the XML parser. |
| 894 | * |
| 895 | * @param parser The XML parser. |
| 896 | * @return String array containing the parsed values. |
| 897 | * @throws IOException Exception related to IO. |
| 898 | * @throws XmlPullParserException Exception related to parsing. |
| 899 | */ |
| 900 | protected List<String> readStringList(XmlPullParser parser) |
| 901 | throws IOException, XmlPullParserException { |
| 902 | |
| 903 | int length = Integer.parseInt(parser.getAttributeValue(null, LENGTH_ATTRIBUTE)); |
| 904 | List<String> arrayEntries = new ArrayList<String>(length); |
| 905 | String value = null; |
| 906 | |
| 907 | if (length == 0) { |
| 908 | return arrayEntries; |
| 909 | } |
| 910 | |
| 911 | int outerDepth = parser.getDepth(); |
| 912 | while (XmlUtils.nextElementWithin(parser, outerDepth)) { |
| 913 | if (parser.getName().equals(VALUE_TAG)) { |
Tyler Gunn | 8e0fef4 | 2014-09-08 18:34:44 -0700 | [diff] [blame] | 914 | parser.next(); |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 915 | value = parser.getText(); |
| 916 | arrayEntries.add(value); |
| 917 | } |
| 918 | } |
| 919 | |
| 920 | return arrayEntries; |
| 921 | } |
Ihab Awad | d9f5438 | 2014-10-24 11:44:47 -0700 | [diff] [blame] | 922 | |
| 923 | protected Bitmap readBitmap(XmlPullParser parser) |
| 924 | throws IOException, XmlPullParserException { |
| 925 | byte[] imageByteArray = Base64.decode(parser.getText(), 0); |
| 926 | return BitmapFactory.decodeByteArray(imageByteArray, 0, imageByteArray.length); |
| 927 | } |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 928 | } |
| 929 | |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 930 | @VisibleForTesting |
| 931 | public static final XmlSerialization<State> sStateXml = |
| 932 | new XmlSerialization<State>() { |
| 933 | private static final String CLASS_STATE = "phone_account_registrar_state"; |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 934 | private static final String DEFAULT_OUTGOING = "default_outgoing"; |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 935 | private static final String SIM_CALL_MANAGER = "sim_call_manager"; |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 936 | private static final String ACCOUNTS = "accounts"; |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 937 | private static final String VERSION = "version"; |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 938 | |
| 939 | @Override |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 940 | public void writeToXml(State o, XmlSerializer serializer, Context context) |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 941 | throws IOException { |
Ihab Awad | 2692322 | 2014-07-30 10:54:35 -0700 | [diff] [blame] | 942 | if (o != null) { |
| 943 | serializer.startTag(null, CLASS_STATE); |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 944 | serializer.attribute(null, VERSION, Objects.toString(EXPECTED_STATE_VERSION)); |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 945 | |
Ihab Awad | 2692322 | 2014-07-30 10:54:35 -0700 | [diff] [blame] | 946 | if (o.defaultOutgoing != null) { |
| 947 | serializer.startTag(null, DEFAULT_OUTGOING); |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 948 | sPhoneAccountHandleXml.writeToXml(o.defaultOutgoing, serializer, context); |
Ihab Awad | 2692322 | 2014-07-30 10:54:35 -0700 | [diff] [blame] | 949 | serializer.endTag(null, DEFAULT_OUTGOING); |
| 950 | } |
| 951 | |
| 952 | if (o.simCallManager != null) { |
| 953 | serializer.startTag(null, SIM_CALL_MANAGER); |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 954 | sPhoneAccountHandleXml.writeToXml(o.simCallManager, serializer, context); |
Ihab Awad | 2692322 | 2014-07-30 10:54:35 -0700 | [diff] [blame] | 955 | serializer.endTag(null, SIM_CALL_MANAGER); |
| 956 | } |
| 957 | |
| 958 | serializer.startTag(null, ACCOUNTS); |
| 959 | for (PhoneAccount m : o.accounts) { |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 960 | sPhoneAccountXml.writeToXml(m, serializer, context); |
Ihab Awad | 2692322 | 2014-07-30 10:54:35 -0700 | [diff] [blame] | 961 | } |
| 962 | serializer.endTag(null, ACCOUNTS); |
| 963 | |
| 964 | serializer.endTag(null, CLASS_STATE); |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 965 | } |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 966 | } |
| 967 | |
| 968 | @Override |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 969 | public State readFromXml(XmlPullParser parser, int version, Context context) |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 970 | throws IOException, XmlPullParserException { |
| 971 | if (parser.getName().equals(CLASS_STATE)) { |
| 972 | State s = new State(); |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 973 | |
| 974 | String rawVersion = parser.getAttributeValue(null, VERSION); |
| 975 | s.versionNumber = TextUtils.isEmpty(rawVersion) ? 1 : |
| 976 | Integer.parseInt(rawVersion); |
| 977 | |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 978 | int outerDepth = parser.getDepth(); |
| 979 | while (XmlUtils.nextElementWithin(parser, outerDepth)) { |
| 980 | if (parser.getName().equals(DEFAULT_OUTGOING)) { |
| 981 | parser.nextTag(); |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 982 | s.defaultOutgoing = sPhoneAccountHandleXml.readFromXml(parser, |
| 983 | s.versionNumber, context); |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 984 | } else if (parser.getName().equals(SIM_CALL_MANAGER)) { |
| 985 | parser.nextTag(); |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 986 | s.simCallManager = sPhoneAccountHandleXml.readFromXml(parser, |
| 987 | s.versionNumber, context); |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 988 | if (s.simCallManager.getUserHandle() == null) { |
| 989 | // This should never happen, but handle the upgrade case. |
| 990 | s.simCallManager = new PhoneAccountHandle( |
| 991 | s.simCallManager.getComponentName(), |
| 992 | s.simCallManager.getId(), |
| 993 | Process.myUserHandle()); |
| 994 | } |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 995 | } else if (parser.getName().equals(ACCOUNTS)) { |
| 996 | int accountsDepth = parser.getDepth(); |
| 997 | while (XmlUtils.nextElementWithin(parser, accountsDepth)) { |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 998 | PhoneAccount account = sPhoneAccountXml.readFromXml(parser, |
| 999 | s.versionNumber, context); |
| 1000 | |
| 1001 | if (account != null && s.accounts != null) { |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1002 | s.accounts.add(account); |
| 1003 | } |
| 1004 | } |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 1005 | } |
| 1006 | } |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1007 | return s; |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 1008 | } |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1009 | return null; |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 1010 | } |
| 1011 | }; |
| 1012 | |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1013 | @VisibleForTesting |
| 1014 | public static final XmlSerialization<PhoneAccount> sPhoneAccountXml = |
| 1015 | new XmlSerialization<PhoneAccount>() { |
| 1016 | private static final String CLASS_PHONE_ACCOUNT = "phone_account"; |
| 1017 | private static final String ACCOUNT_HANDLE = "account_handle"; |
Andrew Lee | 7129f1c | 2014-09-04 11:55:07 -0700 | [diff] [blame] | 1018 | private static final String ADDRESS = "handle"; |
| 1019 | private static final String SUBSCRIPTION_ADDRESS = "subscription_number"; |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 1020 | private static final String CAPABILITIES = "capabilities"; |
| 1021 | private static final String ICON_RES_ID = "icon_res_id"; |
Ihab Awad | d9f5438 | 2014-10-24 11:44:47 -0700 | [diff] [blame] | 1022 | private static final String ICON_PACKAGE_NAME = "icon_package_name"; |
| 1023 | private static final String ICON_BITMAP = "icon_bitmap"; |
Ihab Awad | 7e2c7f3 | 2014-11-03 09:49:45 -0800 | [diff] [blame] | 1024 | private static final String ICON_TINT = "icon_tint"; |
| 1025 | private static final String HIGHLIGHT_COLOR = "highlight_color"; |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 1026 | private static final String LABEL = "label"; |
| 1027 | private static final String SHORT_DESCRIPTION = "short_description"; |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 1028 | private static final String SUPPORTED_URI_SCHEMES = "supported_uri_schemes"; |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 1029 | |
| 1030 | @Override |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 1031 | public void writeToXml(PhoneAccount o, XmlSerializer serializer, Context context) |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1032 | throws IOException { |
Ihab Awad | 2692322 | 2014-07-30 10:54:35 -0700 | [diff] [blame] | 1033 | if (o != null) { |
| 1034 | serializer.startTag(null, CLASS_PHONE_ACCOUNT); |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1035 | |
Ihab Awad | 2692322 | 2014-07-30 10:54:35 -0700 | [diff] [blame] | 1036 | if (o.getAccountHandle() != null) { |
| 1037 | serializer.startTag(null, ACCOUNT_HANDLE); |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 1038 | sPhoneAccountHandleXml.writeToXml(o.getAccountHandle(), serializer, context); |
Ihab Awad | 2692322 | 2014-07-30 10:54:35 -0700 | [diff] [blame] | 1039 | serializer.endTag(null, ACCOUNT_HANDLE); |
| 1040 | } |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1041 | |
Ihab Awad | d9f5438 | 2014-10-24 11:44:47 -0700 | [diff] [blame] | 1042 | writeTextIfNonNull(ADDRESS, o.getAddress(), serializer); |
| 1043 | writeTextIfNonNull(SUBSCRIPTION_ADDRESS, o.getSubscriptionAddress(), serializer); |
| 1044 | writeTextIfNonNull(CAPABILITIES, Integer.toString(o.getCapabilities()), serializer); |
| 1045 | writeTextIfNonNull(ICON_RES_ID, Integer.toString(o.getIconResId()), serializer); |
| 1046 | writeTextIfNonNull(ICON_PACKAGE_NAME, o.getIconPackageName(), serializer); |
| 1047 | writeBitmapIfNonNull(ICON_BITMAP, o.getIconBitmap(), serializer); |
Ihab Awad | 7e2c7f3 | 2014-11-03 09:49:45 -0800 | [diff] [blame] | 1048 | writeTextIfNonNull(ICON_TINT, Integer.toString(o.getIconTint()), serializer); |
| 1049 | writeTextIfNonNull(HIGHLIGHT_COLOR, |
| 1050 | Integer.toString(o.getHighlightColor()), serializer); |
Ihab Awad | d9f5438 | 2014-10-24 11:44:47 -0700 | [diff] [blame] | 1051 | writeTextIfNonNull(LABEL, o.getLabel(), serializer); |
| 1052 | writeTextIfNonNull(SHORT_DESCRIPTION, o.getShortDescription(), serializer); |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 1053 | writeStringList(SUPPORTED_URI_SCHEMES, o.getSupportedUriSchemes(), serializer); |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1054 | |
Ihab Awad | 2692322 | 2014-07-30 10:54:35 -0700 | [diff] [blame] | 1055 | serializer.endTag(null, CLASS_PHONE_ACCOUNT); |
| 1056 | } |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 1057 | } |
| 1058 | |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 1059 | public PhoneAccount readFromXml(XmlPullParser parser, int version, Context context) |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1060 | throws IOException, XmlPullParserException { |
| 1061 | if (parser.getName().equals(CLASS_PHONE_ACCOUNT)) { |
| 1062 | int outerDepth = parser.getDepth(); |
| 1063 | PhoneAccountHandle accountHandle = null; |
Andrew Lee | 7129f1c | 2014-09-04 11:55:07 -0700 | [diff] [blame] | 1064 | Uri address = null; |
| 1065 | Uri subscriptionAddress = null; |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1066 | int capabilities = 0; |
Ihab Awad | 7e2c7f3 | 2014-11-03 09:49:45 -0800 | [diff] [blame] | 1067 | int iconResId = PhoneAccount.NO_RESOURCE_ID; |
Ihab Awad | d9f5438 | 2014-10-24 11:44:47 -0700 | [diff] [blame] | 1068 | String iconPackageName = null; |
Ihab Awad | 7e2c7f3 | 2014-11-03 09:49:45 -0800 | [diff] [blame] | 1069 | Bitmap iconBitmap = null; |
Ihab Awad | 07bc5ee | 2014-11-12 13:42:52 -0800 | [diff] [blame] | 1070 | int iconTint = PhoneAccount.NO_ICON_TINT; |
| 1071 | int highlightColor = PhoneAccount.NO_HIGHLIGHT_COLOR; |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1072 | String label = null; |
| 1073 | String shortDescription = null; |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 1074 | List<String> supportedUriSchemes = null; |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1075 | |
| 1076 | while (XmlUtils.nextElementWithin(parser, outerDepth)) { |
| 1077 | if (parser.getName().equals(ACCOUNT_HANDLE)) { |
| 1078 | parser.nextTag(); |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 1079 | accountHandle = sPhoneAccountHandleXml.readFromXml(parser, version, |
| 1080 | context); |
Andrew Lee | 7129f1c | 2014-09-04 11:55:07 -0700 | [diff] [blame] | 1081 | } else if (parser.getName().equals(ADDRESS)) { |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1082 | parser.next(); |
Andrew Lee | 7129f1c | 2014-09-04 11:55:07 -0700 | [diff] [blame] | 1083 | address = Uri.parse(parser.getText()); |
| 1084 | } else if (parser.getName().equals(SUBSCRIPTION_ADDRESS)) { |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1085 | parser.next(); |
Andrew Lee | 7129f1c | 2014-09-04 11:55:07 -0700 | [diff] [blame] | 1086 | String nextText = parser.getText(); |
| 1087 | subscriptionAddress = nextText == null ? null : Uri.parse(nextText); |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1088 | } else if (parser.getName().equals(CAPABILITIES)) { |
| 1089 | parser.next(); |
| 1090 | capabilities = Integer.parseInt(parser.getText()); |
| 1091 | } else if (parser.getName().equals(ICON_RES_ID)) { |
| 1092 | parser.next(); |
| 1093 | iconResId = Integer.parseInt(parser.getText()); |
Ihab Awad | d9f5438 | 2014-10-24 11:44:47 -0700 | [diff] [blame] | 1094 | } else if (parser.getName().equals(ICON_PACKAGE_NAME)) { |
| 1095 | parser.next(); |
| 1096 | iconPackageName = parser.getText(); |
| 1097 | } else if (parser.getName().equals(ICON_BITMAP)) { |
| 1098 | parser.next(); |
Ihab Awad | 7e2c7f3 | 2014-11-03 09:49:45 -0800 | [diff] [blame] | 1099 | iconBitmap = readBitmap(parser); |
| 1100 | } else if (parser.getName().equals(ICON_TINT)) { |
Nancy Chen | 06ce062 | 2014-10-23 01:17:35 +0000 | [diff] [blame] | 1101 | parser.next(); |
Ihab Awad | 7e2c7f3 | 2014-11-03 09:49:45 -0800 | [diff] [blame] | 1102 | iconTint = Integer.parseInt(parser.getText()); |
| 1103 | } else if (parser.getName().equals(HIGHLIGHT_COLOR)) { |
| 1104 | parser.next(); |
| 1105 | highlightColor = Integer.parseInt(parser.getText()); |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1106 | } else if (parser.getName().equals(LABEL)) { |
| 1107 | parser.next(); |
| 1108 | label = parser.getText(); |
| 1109 | } else if (parser.getName().equals(SHORT_DESCRIPTION)) { |
| 1110 | parser.next(); |
| 1111 | shortDescription = parser.getText(); |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 1112 | } else if (parser.getName().equals(SUPPORTED_URI_SCHEMES)) { |
| 1113 | supportedUriSchemes = readStringList(parser); |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1114 | } |
| 1115 | } |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 1116 | |
| 1117 | // Upgrade older phone accounts to specify the supported URI schemes. |
| 1118 | if (version < 2) { |
| 1119 | ComponentName sipComponentName = new ComponentName("com.android.phone", |
| 1120 | "com.android.services.telephony.sip.SipConnectionService"); |
| 1121 | |
| 1122 | supportedUriSchemes = new ArrayList<>(); |
| 1123 | |
| 1124 | // Handle the SIP connection service. |
| 1125 | // Check the system settings to see if it also should handle "tel" calls. |
| 1126 | if (accountHandle.getComponentName().equals(sipComponentName)) { |
| 1127 | boolean useSipForPstn = useSipForPstnCalls(context); |
| 1128 | supportedUriSchemes.add(PhoneAccount.SCHEME_SIP); |
| 1129 | if (useSipForPstn) { |
| 1130 | supportedUriSchemes.add(PhoneAccount.SCHEME_TEL); |
| 1131 | } |
| 1132 | } else { |
| 1133 | supportedUriSchemes.add(PhoneAccount.SCHEME_TEL); |
| 1134 | supportedUriSchemes.add(PhoneAccount.SCHEME_VOICEMAIL); |
| 1135 | } |
| 1136 | } |
| 1137 | |
Ihab Awad | 7e2c7f3 | 2014-11-03 09:49:45 -0800 | [diff] [blame] | 1138 | // Upgrade older phone accounts with explicit package name |
| 1139 | if (version < 5) { |
| 1140 | if (iconBitmap == null) { |
| 1141 | iconPackageName = accountHandle.getComponentName().getPackageName(); |
| 1142 | } |
| 1143 | } |
| 1144 | |
| 1145 | PhoneAccount.Builder builder = PhoneAccount.builder(accountHandle, label) |
Andrew Lee | 7129f1c | 2014-09-04 11:55:07 -0700 | [diff] [blame] | 1146 | .setAddress(address) |
| 1147 | .setSubscriptionAddress(subscriptionAddress) |
| 1148 | .setCapabilities(capabilities) |
Andrew Lee | 7129f1c | 2014-09-04 11:55:07 -0700 | [diff] [blame] | 1149 | .setShortDescription(shortDescription) |
| 1150 | .setSupportedUriSchemes(supportedUriSchemes) |
Ihab Awad | 7e2c7f3 | 2014-11-03 09:49:45 -0800 | [diff] [blame] | 1151 | .setHighlightColor(highlightColor); |
| 1152 | |
| 1153 | if (iconBitmap == null) { |
| 1154 | builder.setIcon(iconPackageName, iconResId, iconTint); |
| 1155 | } else { |
| 1156 | builder.setIcon(iconBitmap); |
| 1157 | } |
| 1158 | |
| 1159 | return builder.build(); |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1160 | } |
| 1161 | return null; |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 1162 | } |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 1163 | |
| 1164 | /** |
| 1165 | * Determines if the SIP call settings specify to use SIP for all calls, including PSTN calls. |
| 1166 | * |
| 1167 | * @param context The context. |
| 1168 | * @return {@code True} if SIP should be used for all calls. |
| 1169 | */ |
| 1170 | private boolean useSipForPstnCalls(Context context) { |
| 1171 | String option = Settings.System.getString(context.getContentResolver(), |
| 1172 | Settings.System.SIP_CALL_OPTIONS); |
| 1173 | option = (option != null) ? option : Settings.System.SIP_ADDRESS_ONLY; |
| 1174 | return option.equals(Settings.System.SIP_ALWAYS); |
| 1175 | } |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 1176 | }; |
| 1177 | |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1178 | @VisibleForTesting |
| 1179 | public static final XmlSerialization<PhoneAccountHandle> sPhoneAccountHandleXml = |
| 1180 | new XmlSerialization<PhoneAccountHandle>() { |
| 1181 | private static final String CLASS_PHONE_ACCOUNT_HANDLE = "phone_account_handle"; |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 1182 | private static final String COMPONENT_NAME = "component_name"; |
| 1183 | private static final String ID = "id"; |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 1184 | private static final String USER_SERIAL_NUMBER = "user_serial_number"; |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 1185 | |
| 1186 | @Override |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 1187 | public void writeToXml(PhoneAccountHandle o, XmlSerializer serializer, Context context) |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1188 | throws IOException { |
Ihab Awad | 2692322 | 2014-07-30 10:54:35 -0700 | [diff] [blame] | 1189 | if (o != null) { |
| 1190 | serializer.startTag(null, CLASS_PHONE_ACCOUNT_HANDLE); |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1191 | |
Ihab Awad | 2692322 | 2014-07-30 10:54:35 -0700 | [diff] [blame] | 1192 | if (o.getComponentName() != null) { |
Ihab Awad | d9f5438 | 2014-10-24 11:44:47 -0700 | [diff] [blame] | 1193 | writeTextIfNonNull( |
Ihab Awad | 2692322 | 2014-07-30 10:54:35 -0700 | [diff] [blame] | 1194 | COMPONENT_NAME, o.getComponentName().flattenToString(), serializer); |
| 1195 | } |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1196 | |
Ihab Awad | d9f5438 | 2014-10-24 11:44:47 -0700 | [diff] [blame] | 1197 | writeTextIfNonNull(ID, o.getId(), serializer); |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1198 | |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 1199 | if (o.getUserHandle() != null && context != null) { |
| 1200 | UserManager userManager = UserManager.get(context); |
| 1201 | writeLong(USER_SERIAL_NUMBER, |
| 1202 | userManager.getSerialNumberForUser(o.getUserHandle()), serializer); |
| 1203 | } |
| 1204 | |
Ihab Awad | 2692322 | 2014-07-30 10:54:35 -0700 | [diff] [blame] | 1205 | serializer.endTag(null, CLASS_PHONE_ACCOUNT_HANDLE); |
| 1206 | } |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 1207 | } |
| 1208 | |
| 1209 | @Override |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 1210 | public PhoneAccountHandle readFromXml(XmlPullParser parser, int version, Context context) |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1211 | throws IOException, XmlPullParserException { |
| 1212 | if (parser.getName().equals(CLASS_PHONE_ACCOUNT_HANDLE)) { |
| 1213 | String componentNameString = null; |
| 1214 | String idString = null; |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 1215 | String userSerialNumberString = null; |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1216 | int outerDepth = parser.getDepth(); |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 1217 | |
| 1218 | UserManager userManager = UserManager.get(context); |
| 1219 | |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1220 | while (XmlUtils.nextElementWithin(parser, outerDepth)) { |
| 1221 | if (parser.getName().equals(COMPONENT_NAME)) { |
| 1222 | parser.next(); |
| 1223 | componentNameString = parser.getText(); |
| 1224 | } else if (parser.getName().equals(ID)) { |
| 1225 | parser.next(); |
| 1226 | idString = parser.getText(); |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 1227 | } else if (parser.getName().equals(USER_SERIAL_NUMBER)) { |
| 1228 | parser.next(); |
| 1229 | userSerialNumberString = parser.getText(); |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1230 | } |
| 1231 | } |
Ihab Awad | 2692322 | 2014-07-30 10:54:35 -0700 | [diff] [blame] | 1232 | if (componentNameString != null) { |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 1233 | UserHandle userHandle = null; |
| 1234 | if (userSerialNumberString != null) { |
| 1235 | try { |
| 1236 | long serialNumber = Long.parseLong(userSerialNumberString); |
| 1237 | userHandle = userManager.getUserForSerialNumber(serialNumber); |
| 1238 | } catch (NumberFormatException e) { |
| 1239 | Log.e(this, e, "Could not parse UserHandle " + userSerialNumberString); |
| 1240 | } |
| 1241 | } |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1242 | return new PhoneAccountHandle( |
| 1243 | ComponentName.unflattenFromString(componentNameString), |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 1244 | idString, |
| 1245 | userHandle); |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1246 | } |
| 1247 | } |
| 1248 | return null; |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 1249 | } |
| 1250 | }; |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 1251 | } |