Bookatz | 029832a | 2019-10-04 16:50:22 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 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 | |
| 17 | package com.android.server.pm; |
| 18 | |
| 19 | import static android.content.pm.UserInfo.FLAG_DEMO; |
| 20 | import static android.content.pm.UserInfo.FLAG_EPHEMERAL; |
| 21 | import static android.content.pm.UserInfo.FLAG_FULL; |
| 22 | import static android.content.pm.UserInfo.FLAG_GUEST; |
| 23 | import static android.content.pm.UserInfo.FLAG_MANAGED_PROFILE; |
| 24 | import static android.content.pm.UserInfo.FLAG_PROFILE; |
| 25 | import static android.content.pm.UserInfo.FLAG_RESTRICTED; |
| 26 | import static android.content.pm.UserInfo.FLAG_SYSTEM; |
| 27 | import static android.os.UserManager.USER_TYPE_FULL_DEMO; |
| 28 | import static android.os.UserManager.USER_TYPE_FULL_GUEST; |
| 29 | import static android.os.UserManager.USER_TYPE_FULL_RESTRICTED; |
| 30 | import static android.os.UserManager.USER_TYPE_FULL_SECONDARY; |
| 31 | import static android.os.UserManager.USER_TYPE_FULL_SYSTEM; |
| 32 | import static android.os.UserManager.USER_TYPE_PROFILE_MANAGED; |
| 33 | import static android.os.UserManager.USER_TYPE_SYSTEM_HEADLESS; |
| 34 | |
| 35 | import static com.android.server.pm.UserTypeDetails.UNLIMITED_NUMBER_OF_USERS; |
| 36 | |
Bookatz | d15b2f7 | 2019-11-26 16:58:57 -0800 | [diff] [blame] | 37 | import android.content.pm.UserInfo; |
Bookatz | 029832a | 2019-10-04 16:50:22 -0700 | [diff] [blame] | 38 | import android.content.res.Resources; |
Bookatz | 95f0270 | 2019-12-03 17:15:49 -0800 | [diff] [blame] | 39 | import android.content.res.XmlResourceParser; |
Bookatz | d15b2f7 | 2019-11-26 16:58:57 -0800 | [diff] [blame] | 40 | import android.os.Bundle; |
Bookatz | 029832a | 2019-10-04 16:50:22 -0700 | [diff] [blame] | 41 | import android.os.UserManager; |
| 42 | import android.util.ArrayMap; |
Bookatz | 95f0270 | 2019-12-03 17:15:49 -0800 | [diff] [blame] | 43 | import android.util.Slog; |
| 44 | |
| 45 | import com.android.internal.annotations.VisibleForTesting; |
| 46 | import com.android.internal.util.XmlUtils; |
| 47 | |
| 48 | import org.xmlpull.v1.XmlPullParserException; |
| 49 | |
| 50 | import java.io.IOException; |
| 51 | import java.util.ArrayList; |
| 52 | import java.util.function.Consumer; |
Bookatz | 029832a | 2019-10-04 16:50:22 -0700 | [diff] [blame] | 53 | |
| 54 | /** |
| 55 | * Class for creating all {@link UserTypeDetails} on the device. |
| 56 | * |
Bookatz | 95f0270 | 2019-12-03 17:15:49 -0800 | [diff] [blame] | 57 | * This class is responsible both for defining the AOSP use types, as well as reading in customized |
| 58 | * user types from {@link com.android.internal.R.xml#config_user_types}. |
| 59 | * |
Bookatz | 029832a | 2019-10-04 16:50:22 -0700 | [diff] [blame] | 60 | * Tests are located in UserManagerServiceUserTypeTest.java. |
| 61 | * @hide |
| 62 | */ |
| 63 | public final class UserTypeFactory { |
| 64 | |
Bookatz | 95f0270 | 2019-12-03 17:15:49 -0800 | [diff] [blame] | 65 | private static final String LOG_TAG = "UserTypeFactory"; |
| 66 | |
Bookatz | 029832a | 2019-10-04 16:50:22 -0700 | [diff] [blame] | 67 | /** This is a utility class, so no instantiable constructor. */ |
| 68 | private UserTypeFactory() {} |
| 69 | |
| 70 | /** |
| 71 | * Obtains the user types (built-in and customized) for this device. |
| 72 | * |
| 73 | * @return mapping from the name of each user type to its {@link UserTypeDetails} object |
| 74 | */ |
| 75 | public static ArrayMap<String, UserTypeDetails> getUserTypes() { |
Bookatz | 95f0270 | 2019-12-03 17:15:49 -0800 | [diff] [blame] | 76 | final ArrayMap<String, UserTypeDetails.Builder> builders = new ArrayMap<>(); |
| 77 | builders.put(USER_TYPE_PROFILE_MANAGED, getDefaultTypeProfileManaged()); |
| 78 | builders.put(USER_TYPE_FULL_SYSTEM, getDefaultTypeFullSystem()); |
| 79 | builders.put(USER_TYPE_FULL_SECONDARY, getDefaultTypeFullSecondary()); |
| 80 | builders.put(USER_TYPE_FULL_GUEST, getDefaultTypeFullGuest()); |
| 81 | builders.put(USER_TYPE_FULL_DEMO, getDefaultTypeFullDemo()); |
| 82 | builders.put(USER_TYPE_FULL_RESTRICTED, getDefaultTypeFullRestricted()); |
| 83 | builders.put(USER_TYPE_SYSTEM_HEADLESS, getDefaultTypeSystemHeadless()); |
| 84 | |
Bookatz | d15b2f7 | 2019-11-26 16:58:57 -0800 | [diff] [blame] | 85 | try (XmlResourceParser parser = |
| 86 | Resources.getSystem().getXml(com.android.internal.R.xml.config_user_types)) { |
Bookatz | 95f0270 | 2019-12-03 17:15:49 -0800 | [diff] [blame] | 87 | customizeBuilders(builders, parser); |
| 88 | } |
| 89 | |
| 90 | final ArrayMap<String, UserTypeDetails> types = new ArrayMap<>(builders.size()); |
| 91 | for (int i = 0; i < builders.size(); i++) { |
| 92 | types.put(builders.keyAt(i), builders.valueAt(i).createUserTypeDetails()); |
| 93 | } |
| 94 | return types; |
Bookatz | 029832a | 2019-10-04 16:50:22 -0700 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Returns the Builder for the default {@link UserManager#USER_TYPE_PROFILE_MANAGED} |
| 99 | * configuration. |
| 100 | */ |
| 101 | private static UserTypeDetails.Builder getDefaultTypeProfileManaged() { |
| 102 | return new UserTypeDetails.Builder() |
| 103 | .setName(USER_TYPE_PROFILE_MANAGED) |
| 104 | .setBaseType(FLAG_PROFILE) |
| 105 | .setDefaultUserInfoPropertyFlags(FLAG_MANAGED_PROFILE) |
| 106 | .setMaxAllowedPerParent(1) |
| 107 | .setLabel(0) |
| 108 | .setIconBadge(com.android.internal.R.drawable.ic_corp_icon_badge_case) |
| 109 | .setBadgePlain(com.android.internal.R.drawable.ic_corp_badge_case) |
| 110 | .setBadgeNoBackground(com.android.internal.R.drawable.ic_corp_badge_no_background) |
| 111 | .setBadgeLabels( |
| 112 | com.android.internal.R.string.managed_profile_label_badge, |
| 113 | com.android.internal.R.string.managed_profile_label_badge_2, |
| 114 | com.android.internal.R.string.managed_profile_label_badge_3) |
| 115 | .setBadgeColors( |
| 116 | com.android.internal.R.color.profile_badge_1, |
| 117 | com.android.internal.R.color.profile_badge_2, |
Bookatz | d15b2f7 | 2019-11-26 16:58:57 -0800 | [diff] [blame] | 118 | com.android.internal.R.color.profile_badge_3) |
| 119 | .setDefaultRestrictions(null); |
Bookatz | 029832a | 2019-10-04 16:50:22 -0700 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Returns the Builder for the default {@link UserManager#USER_TYPE_FULL_SECONDARY} |
| 124 | * configuration. |
| 125 | */ |
| 126 | private static UserTypeDetails.Builder getDefaultTypeFullSecondary() { |
| 127 | return new UserTypeDetails.Builder() |
| 128 | .setName(USER_TYPE_FULL_SECONDARY) |
| 129 | .setBaseType(FLAG_FULL) |
Bookatz | d15b2f7 | 2019-11-26 16:58:57 -0800 | [diff] [blame] | 130 | .setMaxAllowed(UNLIMITED_NUMBER_OF_USERS) |
| 131 | .setDefaultRestrictions(getDefaultSecondaryUserRestrictions()); |
Bookatz | 029832a | 2019-10-04 16:50:22 -0700 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | /** |
| 135 | * Returns the Builder for the default {@link UserManager#USER_TYPE_FULL_GUEST} configuration. |
| 136 | */ |
| 137 | private static UserTypeDetails.Builder getDefaultTypeFullGuest() { |
| 138 | final boolean ephemeralGuests = Resources.getSystem() |
| 139 | .getBoolean(com.android.internal.R.bool.config_guestUserEphemeral); |
| 140 | final int flags = FLAG_GUEST | (ephemeralGuests ? FLAG_EPHEMERAL : 0); |
| 141 | |
Bookatz | 029832a | 2019-10-04 16:50:22 -0700 | [diff] [blame] | 142 | return new UserTypeDetails.Builder() |
| 143 | .setName(USER_TYPE_FULL_GUEST) |
| 144 | .setBaseType(FLAG_FULL) |
| 145 | .setDefaultUserInfoPropertyFlags(flags) |
Bookatz | d15b2f7 | 2019-11-26 16:58:57 -0800 | [diff] [blame] | 146 | .setMaxAllowed(1) |
| 147 | .setDefaultRestrictions(getDefaultGuestUserRestrictions()); |
Bookatz | 029832a | 2019-10-04 16:50:22 -0700 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | /** |
| 151 | * Returns the Builder for the default {@link UserManager#USER_TYPE_FULL_DEMO} configuration. |
| 152 | */ |
| 153 | private static UserTypeDetails.Builder getDefaultTypeFullDemo() { |
| 154 | return new UserTypeDetails.Builder() |
| 155 | .setName(USER_TYPE_FULL_DEMO) |
| 156 | .setBaseType(FLAG_FULL) |
| 157 | .setDefaultUserInfoPropertyFlags(FLAG_DEMO) |
Bookatz | d15b2f7 | 2019-11-26 16:58:57 -0800 | [diff] [blame] | 158 | .setMaxAllowed(UNLIMITED_NUMBER_OF_USERS) |
| 159 | .setDefaultRestrictions(null); |
Bookatz | 029832a | 2019-10-04 16:50:22 -0700 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | /** |
| 163 | * Returns the Builder for the default {@link UserManager#USER_TYPE_FULL_RESTRICTED} |
| 164 | * configuration. |
| 165 | */ |
| 166 | private static UserTypeDetails.Builder getDefaultTypeFullRestricted() { |
| 167 | return new UserTypeDetails.Builder() |
| 168 | .setName(USER_TYPE_FULL_RESTRICTED) |
| 169 | .setBaseType(FLAG_FULL) |
| 170 | .setDefaultUserInfoPropertyFlags(FLAG_RESTRICTED) |
Bookatz | d15b2f7 | 2019-11-26 16:58:57 -0800 | [diff] [blame] | 171 | .setMaxAllowed(UNLIMITED_NUMBER_OF_USERS) |
| 172 | // NB: UserManagerService.createRestrictedProfile() applies hardcoded restrictions. |
| 173 | .setDefaultRestrictions(null); |
Bookatz | 029832a | 2019-10-04 16:50:22 -0700 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | /** |
| 177 | * Returns the Builder for the default {@link UserManager#USER_TYPE_FULL_SYSTEM} configuration. |
| 178 | */ |
Bookatz | 95f0270 | 2019-12-03 17:15:49 -0800 | [diff] [blame] | 179 | private static UserTypeDetails.Builder getDefaultTypeFullSystem() { |
Bookatz | 029832a | 2019-10-04 16:50:22 -0700 | [diff] [blame] | 180 | return new UserTypeDetails.Builder() |
| 181 | .setName(USER_TYPE_FULL_SYSTEM) |
| 182 | .setBaseType(FLAG_SYSTEM | FLAG_FULL); |
| 183 | } |
| 184 | |
| 185 | /** |
| 186 | * Returns the Builder for the default {@link UserManager#USER_TYPE_SYSTEM_HEADLESS} |
| 187 | * configuration. |
| 188 | */ |
| 189 | private static UserTypeDetails.Builder getDefaultTypeSystemHeadless() { |
| 190 | return new UserTypeDetails.Builder() |
| 191 | .setName(USER_TYPE_SYSTEM_HEADLESS) |
| 192 | .setBaseType(FLAG_SYSTEM); |
| 193 | } |
Bookatz | 95f0270 | 2019-12-03 17:15:49 -0800 | [diff] [blame] | 194 | |
Bookatz | d15b2f7 | 2019-11-26 16:58:57 -0800 | [diff] [blame] | 195 | private static Bundle getDefaultSecondaryUserRestrictions() { |
| 196 | final Bundle restrictions = new Bundle(); |
| 197 | restrictions.putBoolean(UserManager.DISALLOW_OUTGOING_CALLS, true); |
| 198 | restrictions.putBoolean(UserManager.DISALLOW_SMS, true); |
| 199 | return restrictions; |
| 200 | } |
| 201 | |
| 202 | private static Bundle getDefaultGuestUserRestrictions() { |
| 203 | // Guest inherits the secondary user's restrictions, plus has some extra ones. |
| 204 | final Bundle restrictions = getDefaultSecondaryUserRestrictions(); |
| 205 | restrictions.putBoolean(UserManager.DISALLOW_CONFIG_WIFI, true); |
| 206 | restrictions.putBoolean(UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES, true); |
| 207 | return restrictions; |
| 208 | } |
| 209 | |
Bookatz | 95f0270 | 2019-12-03 17:15:49 -0800 | [diff] [blame] | 210 | /** |
| 211 | * Reads the given xml parser to obtain device user-type customization, and updates the given |
| 212 | * map of {@link UserTypeDetails.Builder}s accordingly. |
| 213 | * <p> |
| 214 | * The xml file can specify the attributes according to the set... methods below. |
| 215 | */ |
| 216 | @VisibleForTesting |
| 217 | static void customizeBuilders(ArrayMap<String, UserTypeDetails.Builder> builders, |
| 218 | XmlResourceParser parser) { |
| 219 | try { |
| 220 | XmlUtils.beginDocument(parser, "user-types"); |
| 221 | for (XmlUtils.nextElement(parser); |
| 222 | parser.getEventType() != XmlResourceParser.END_DOCUMENT; |
| 223 | XmlUtils.nextElement(parser)) { |
Bookatz | d15b2f7 | 2019-11-26 16:58:57 -0800 | [diff] [blame] | 224 | final boolean isProfile; |
Bookatz | 95f0270 | 2019-12-03 17:15:49 -0800 | [diff] [blame] | 225 | final String elementName = parser.getName(); |
Bookatz | d15b2f7 | 2019-11-26 16:58:57 -0800 | [diff] [blame] | 226 | if ("profile-type".equals(elementName)) { |
| 227 | isProfile = true; |
| 228 | } else if ("full-type".equals(elementName)) { |
| 229 | isProfile = false; |
| 230 | } else { |
Bookatz | 95f0270 | 2019-12-03 17:15:49 -0800 | [diff] [blame] | 231 | Slog.w(LOG_TAG, "Skipping unknown element " + elementName + " in " |
| 232 | + parser.getPositionDescription()); |
| 233 | XmlUtils.skipCurrentTag(parser); |
| 234 | continue; |
| 235 | } |
| 236 | |
| 237 | String typeName = parser.getAttributeValue(null, "name"); |
| 238 | if (typeName == null) { |
Bookatz | d15b2f7 | 2019-11-26 16:58:57 -0800 | [diff] [blame] | 239 | Slog.w(LOG_TAG, "Skipping user type with no name in " |
Bookatz | 95f0270 | 2019-12-03 17:15:49 -0800 | [diff] [blame] | 240 | + parser.getPositionDescription()); |
| 241 | XmlUtils.skipCurrentTag(parser); |
| 242 | continue; |
| 243 | } |
| 244 | typeName.intern(); |
| 245 | |
| 246 | UserTypeDetails.Builder builder; |
| 247 | if (typeName.startsWith("android.")) { |
| 248 | // typeName refers to a AOSP-defined type which we are modifying. |
| 249 | Slog.i(LOG_TAG, "Customizing user type " + typeName); |
| 250 | builder = builders.get(typeName); |
| 251 | if (builder == null) { |
| 252 | throw new IllegalArgumentException("Illegal custom user type name " |
| 253 | + typeName + ": Non-AOSP user types cannot start with 'android.'"); |
| 254 | } |
Bookatz | d15b2f7 | 2019-11-26 16:58:57 -0800 | [diff] [blame] | 255 | final boolean isValid = |
| 256 | (isProfile && builder.getBaseType() == UserInfo.FLAG_PROFILE) |
| 257 | || (!isProfile && builder.getBaseType() == UserInfo.FLAG_FULL); |
| 258 | if (!isValid) { |
| 259 | throw new IllegalArgumentException("Wrong base type to customize user type " |
| 260 | + "(" + typeName + "), which is type " |
| 261 | + UserInfo.flagsToString(builder.getBaseType())); |
Bookatz | 95f0270 | 2019-12-03 17:15:49 -0800 | [diff] [blame] | 262 | } |
Bookatz | d15b2f7 | 2019-11-26 16:58:57 -0800 | [diff] [blame] | 263 | } else if (isProfile) { |
| 264 | // typeName refers to a new OEM-defined profile type which we are defining. |
Bookatz | 95f0270 | 2019-12-03 17:15:49 -0800 | [diff] [blame] | 265 | Slog.i(LOG_TAG, "Creating custom user type " + typeName); |
| 266 | builder = new UserTypeDetails.Builder(); |
| 267 | builder.setName(typeName); |
| 268 | builder.setBaseType(FLAG_PROFILE); |
| 269 | builders.put(typeName, builder); |
Bookatz | d15b2f7 | 2019-11-26 16:58:57 -0800 | [diff] [blame] | 270 | } else { |
| 271 | throw new IllegalArgumentException("Creation of non-profile user type " |
| 272 | + "(" + typeName + ") is not currently supported."); |
Bookatz | 95f0270 | 2019-12-03 17:15:49 -0800 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | // Process the attributes (other than name). |
Bookatz | d15b2f7 | 2019-11-26 16:58:57 -0800 | [diff] [blame] | 276 | if (isProfile) { |
| 277 | setIntAttribute(parser, "max-allowed-per-parent", |
| 278 | builder::setMaxAllowedPerParent); |
| 279 | setResAttribute(parser, "icon-badge", builder::setIconBadge); |
| 280 | setResAttribute(parser, "badge-plain", builder::setBadgePlain); |
| 281 | setResAttribute(parser, "badge-no-background", builder::setBadgeNoBackground); |
| 282 | } |
Bookatz | 95f0270 | 2019-12-03 17:15:49 -0800 | [diff] [blame] | 283 | |
| 284 | // Process child elements. |
| 285 | final int depth = parser.getDepth(); |
| 286 | while (XmlUtils.nextElementWithin(parser, depth)) { |
| 287 | final String childName = parser.getName(); |
Bookatz | d15b2f7 | 2019-11-26 16:58:57 -0800 | [diff] [blame] | 288 | if ("default-restrictions".equals(childName)) { |
| 289 | final Bundle restrictions = UserRestrictionsUtils.readRestrictions(parser); |
| 290 | builder.setDefaultRestrictions(restrictions); |
| 291 | } else if (isProfile && "badge-labels".equals(childName)) { |
Bookatz | 95f0270 | 2019-12-03 17:15:49 -0800 | [diff] [blame] | 292 | setResAttributeArray(parser, builder::setBadgeLabels); |
Bookatz | d15b2f7 | 2019-11-26 16:58:57 -0800 | [diff] [blame] | 293 | } else if (isProfile && "badge-colors".equals(childName)) { |
Bookatz | 95f0270 | 2019-12-03 17:15:49 -0800 | [diff] [blame] | 294 | setResAttributeArray(parser, builder::setBadgeColors); |
| 295 | } else { |
| 296 | Slog.w(LOG_TAG, "Unrecognized tag " + childName + " in " |
| 297 | + parser.getPositionDescription()); |
| 298 | } |
| 299 | } |
| 300 | } |
| 301 | } catch (XmlPullParserException | IOException e) { |
| 302 | Slog.w(LOG_TAG, "Cannot read user type configuration file.", e); |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | /** |
| 307 | * If the given attribute exists, gets the int stored in it and performs the given fcn using it. |
| 308 | * The stored value must be an int or NumberFormatException will be thrown. |
| 309 | * |
| 310 | * @param parser xml parser from which to read the attribute |
| 311 | * @param attributeName name of the attribute |
| 312 | * @param fcn one-int-argument function, |
| 313 | * like {@link UserTypeDetails.Builder#setMaxAllowedPerParent(int)} |
| 314 | */ |
| 315 | private static void setIntAttribute(XmlResourceParser parser, String attributeName, |
| 316 | Consumer<Integer> fcn) { |
| 317 | final String intValue = parser.getAttributeValue(null, attributeName); |
| 318 | if (intValue == null) { |
| 319 | return; |
| 320 | } |
| 321 | try { |
| 322 | fcn.accept(Integer.parseInt(intValue)); |
| 323 | } catch (NumberFormatException e) { |
Bookatz | d15b2f7 | 2019-11-26 16:58:57 -0800 | [diff] [blame] | 324 | Slog.e(LOG_TAG, "Cannot parse value of '" + intValue + "' for " + attributeName |
| 325 | + " in " + parser.getPositionDescription(), e); |
Bookatz | 95f0270 | 2019-12-03 17:15:49 -0800 | [diff] [blame] | 326 | throw e; |
| 327 | } |
| 328 | } |
| 329 | |
| 330 | /** |
| 331 | * If the given attribute exists, gets the resId stored in it (or 0 if it is not a valid resId) |
| 332 | * and performs the given fcn using it. |
| 333 | * |
| 334 | * @param parser xml parser from which to read the attribute |
| 335 | * @param attributeName name of the attribute |
| 336 | * @param fcn one-argument function, like {@link UserTypeDetails.Builder#setIconBadge(int)} |
| 337 | */ |
| 338 | private static void setResAttribute(XmlResourceParser parser, String attributeName, |
| 339 | Consumer<Integer> fcn) { |
| 340 | if (parser.getAttributeValue(null, attributeName) == null) { |
| 341 | // Attribute is not present, i.e. use the default value. |
| 342 | return; |
| 343 | } |
| 344 | final int resId = parser.getAttributeResourceValue(null, attributeName, Resources.ID_NULL); |
| 345 | fcn.accept(resId); |
| 346 | } |
| 347 | |
| 348 | /** |
| 349 | * Gets the resIds stored in "item" elements (in their "res" attribute) at the current depth. |
Bookatz | d15b2f7 | 2019-11-26 16:58:57 -0800 | [diff] [blame] | 350 | * Then performs the given fcn using the int[] array of these resIds. |
Bookatz | 95f0270 | 2019-12-03 17:15:49 -0800 | [diff] [blame] | 351 | * <p> |
| 352 | * Each xml element is expected to be of the form {@code <item res="someResValue" />}. |
| 353 | * |
| 354 | * @param parser xml parser from which to read the elements and their attributes |
| 355 | * @param fcn function, like {@link UserTypeDetails.Builder#setBadgeColors(int...)} |
| 356 | */ |
| 357 | private static void setResAttributeArray(XmlResourceParser parser, Consumer<int[]> fcn) |
| 358 | throws IOException, XmlPullParserException { |
| 359 | |
| 360 | ArrayList<Integer> resList = new ArrayList<>(); |
| 361 | final int depth = parser.getDepth(); |
| 362 | while (XmlUtils.nextElementWithin(parser, depth)) { |
| 363 | final String elementName = parser.getName(); |
| 364 | if (!"item".equals(elementName)) { |
| 365 | Slog.w(LOG_TAG, "Skipping unknown child element " + elementName + " in " |
| 366 | + parser.getPositionDescription()); |
| 367 | XmlUtils.skipCurrentTag(parser); |
| 368 | continue; |
| 369 | } |
| 370 | final int resId = parser.getAttributeResourceValue(null, "res", -1); |
| 371 | if (resId == -1) { |
| 372 | continue; |
| 373 | } |
| 374 | resList.add(resId); |
| 375 | } |
| 376 | |
| 377 | int[] result = new int[resList.size()]; |
| 378 | for (int i = 0; i < resList.size(); i++) { |
| 379 | result[i] = resList.get(i); |
| 380 | } |
| 381 | fcn.accept(result); |
| 382 | } |
Bookatz | 029832a | 2019-10-04 16:50:22 -0700 | [diff] [blame] | 383 | } |