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