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