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