summaryrefslogtreecommitdiff
path: root/services/core/java/com/android/server/pm/UserTypeFactory.java
blob: a814ca46fa5ebfdef36313cb579f17f5734b1652 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
/*
 * Copyright (C) 2019 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.server.pm;

import static android.content.pm.UserInfo.FLAG_ADMIN;
import static android.content.pm.UserInfo.FLAG_DEMO;
import static android.content.pm.UserInfo.FLAG_EPHEMERAL;
import static android.content.pm.UserInfo.FLAG_FULL;
import static android.content.pm.UserInfo.FLAG_GUEST;
import static android.content.pm.UserInfo.FLAG_MAIN;
import static android.content.pm.UserInfo.FLAG_MANAGED_PROFILE;
import static android.content.pm.UserInfo.FLAG_PRIMARY;
import static android.content.pm.UserInfo.FLAG_PROFILE;
import static android.content.pm.UserInfo.FLAG_RESTRICTED;
import static android.content.pm.UserInfo.FLAG_SYSTEM;
import static android.os.UserManager.USER_TYPE_FULL_DEMO;
import static android.os.UserManager.USER_TYPE_FULL_GUEST;
import static android.os.UserManager.USER_TYPE_FULL_RESTRICTED;
import static android.os.UserManager.USER_TYPE_FULL_SECONDARY;
import static android.os.UserManager.USER_TYPE_FULL_SYSTEM;
import static android.os.UserManager.USER_TYPE_PROFILE_CLONE;
import static android.os.UserManager.USER_TYPE_PROFILE_MANAGED;
import static android.os.UserManager.USER_TYPE_PROFILE_TEST;
import static android.os.UserManager.USER_TYPE_SYSTEM_HEADLESS;

import static com.android.server.pm.UserTypeDetails.UNLIMITED_NUMBER_OF_USERS;

import android.content.pm.UserInfo;
import android.content.pm.UserProperties;
import android.content.res.Resources;
import android.content.res.XmlResourceParser;
import android.os.Build;
import android.os.Bundle;
import android.os.UserManager;
import android.util.ArrayMap;
import android.util.Slog;

import com.android.internal.annotations.VisibleForTesting;
import com.android.internal.util.XmlUtils;

import org.xmlpull.v1.XmlPullParserException;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;

/**
 * Class for creating all {@link UserTypeDetails} on the device.
 *
 * This class is responsible both for defining the AOSP use types, as well as reading in customized
 * user types from {@link com.android.internal.R.xml#config_user_types}.
 *
 * Tests are located in {@link UserManagerServiceUserTypeTest}.
 * @hide
 */
public final class UserTypeFactory {

    private static final String LOG_TAG = "UserTypeFactory";

    /** This is a utility class, so no instantiable constructor. */
    private UserTypeFactory() {}

    /**
     * Obtains the user types (built-in and customized) for this device.
     *
     * @return mapping from the name of each user type to its {@link UserTypeDetails} object
     */
    public static ArrayMap<String, UserTypeDetails> getUserTypes() {
        final ArrayMap<String, UserTypeDetails.Builder> builders = getDefaultBuilders();

        try (XmlResourceParser parser =
                     Resources.getSystem().getXml(com.android.internal.R.xml.config_user_types)) {
            customizeBuilders(builders, parser);
        }

        final ArrayMap<String, UserTypeDetails> types = new ArrayMap<>(builders.size());
        for (int i = 0; i < builders.size(); i++) {
            types.put(builders.keyAt(i), builders.valueAt(i).createUserTypeDetails());
        }
        return types;
    }

    private static ArrayMap<String, UserTypeDetails.Builder> getDefaultBuilders() {
        final ArrayMap<String, UserTypeDetails.Builder> builders = new ArrayMap<>();

        builders.put(USER_TYPE_PROFILE_MANAGED, getDefaultTypeProfileManaged());
        builders.put(USER_TYPE_FULL_SYSTEM, getDefaultTypeFullSystem());
        builders.put(USER_TYPE_FULL_SECONDARY, getDefaultTypeFullSecondary());
        builders.put(USER_TYPE_FULL_GUEST, getDefaultTypeFullGuest());
        builders.put(USER_TYPE_FULL_DEMO, getDefaultTypeFullDemo());
        builders.put(USER_TYPE_FULL_RESTRICTED, getDefaultTypeFullRestricted());
        builders.put(USER_TYPE_SYSTEM_HEADLESS, getDefaultTypeSystemHeadless());
        builders.put(USER_TYPE_PROFILE_CLONE, getDefaultTypeProfileClone());
        if (Build.IS_DEBUGGABLE) {
            builders.put(USER_TYPE_PROFILE_TEST, getDefaultTypeProfileTest());
        }

        return builders;
    }

    /**
     * Returns the Builder for the default {@link UserManager#USER_TYPE_PROFILE_CLONE}
     * configuration.
     */
    // TODO(b/182396009): Add default restrictions, if needed for clone user type.
    private static UserTypeDetails.Builder getDefaultTypeProfileClone() {
        return new UserTypeDetails.Builder()
                .setName(USER_TYPE_PROFILE_CLONE)
                .setBaseType(FLAG_PROFILE)
                .setMaxAllowedPerParent(1)
                .setLabel(0)
                .setIconBadge(com.android.internal.R.drawable.ic_clone_icon_badge)
                .setBadgePlain(com.android.internal.R.drawable.ic_clone_badge)
                // Clone doesn't use BadgeNoBackground, so just set to BadgePlain as a placeholder.
                .setBadgeNoBackground(com.android.internal.R.drawable.ic_clone_badge)
                .setBadgeLabels(
                        com.android.internal.R.string.clone_profile_label_badge)
                .setBadgeColors(
                        com.android.internal.R.color.system_neutral2_800)
                .setDarkThemeBadgeColors(
                        com.android.internal.R.color.system_neutral2_900)
                .setDefaultRestrictions(null)
                .setDefaultCrossProfileIntentFilters(getDefaultCloneCrossProfileIntentFilter())
                .setDefaultSecureSettings(getDefaultNonManagedProfileSecureSettings())
                .setDefaultUserProperties(new UserProperties.Builder()
                        .setStartWithParent(true)
                        .setShowInLauncher(UserProperties.SHOW_IN_LAUNCHER_WITH_PARENT)
                        .setShowInSettings(UserProperties.SHOW_IN_SETTINGS_WITH_PARENT)
                        .setInheritDevicePolicy(UserProperties.INHERIT_DEVICE_POLICY_FROM_PARENT)
                        .setUseParentsContacts(true)
                        .setUpdateCrossProfileIntentFiltersOnOTA(true)
                        .setCrossProfileIntentFilterAccessControl(
                                UserProperties.CROSS_PROFILE_INTENT_FILTER_ACCESS_LEVEL_SYSTEM)
                        .setCrossProfileIntentResolutionStrategy(UserProperties
                                .CROSS_PROFILE_INTENT_RESOLUTION_STRATEGY_NO_FILTERING)
                        .setMediaSharedWithParent(true)
                        .setCredentialShareableWithParent(true)
                        .setDeleteAppWithParent(true));
    }

    /**
     * Returns the Builder for the default {@link UserManager#USER_TYPE_PROFILE_MANAGED}
     * configuration.
     */
    private static UserTypeDetails.Builder getDefaultTypeProfileManaged() {
        return new UserTypeDetails.Builder()
                .setName(USER_TYPE_PROFILE_MANAGED)
                .setBaseType(FLAG_PROFILE)
                .setDefaultUserInfoPropertyFlags(FLAG_MANAGED_PROFILE)
                .setMaxAllowedPerParent(1)
                .setLabel(0)
                .setIconBadge(com.android.internal.R.drawable.ic_corp_icon_badge_case)
                .setBadgePlain(com.android.internal.R.drawable.ic_corp_badge_case)
                .setBadgeNoBackground(com.android.internal.R.drawable.ic_corp_badge_no_background)
                .setBadgeLabels(
                        com.android.internal.R.string.managed_profile_label_badge,
                        com.android.internal.R.string.managed_profile_label_badge_2,
                        com.android.internal.R.string.managed_profile_label_badge_3)
                .setBadgeColors(
                        com.android.internal.R.color.profile_badge_1,
                        com.android.internal.R.color.profile_badge_2,
                        com.android.internal.R.color.profile_badge_3)
                .setDarkThemeBadgeColors(
                        com.android.internal.R.color.profile_badge_1_dark,
                        com.android.internal.R.color.profile_badge_2_dark,
                        com.android.internal.R.color.profile_badge_3_dark)
                .setDefaultRestrictions(getDefaultManagedProfileRestrictions())
                .setDefaultSecureSettings(getDefaultManagedProfileSecureSettings())
                .setDefaultCrossProfileIntentFilters(getDefaultManagedCrossProfileIntentFilter())
                .setDefaultUserProperties(new UserProperties.Builder()
                        .setStartWithParent(true)
                        .setShowInLauncher(UserProperties.SHOW_IN_LAUNCHER_SEPARATE)
                        .setShowInSettings(UserProperties.SHOW_IN_SETTINGS_SEPARATE)
                        .setCredentialShareableWithParent(true));
    }

    /**
     * Returns the Builder for the default {@link UserManager#USER_TYPE_PROFILE_TEST}
     * configuration (for userdebug builds). For now it just uses managed profile badges.
     */
    private static UserTypeDetails.Builder getDefaultTypeProfileTest() {
        final Bundle restrictions = new Bundle();
        restrictions.putBoolean(UserManager.DISALLOW_FUN, true);

        return new UserTypeDetails.Builder()
                .setName(USER_TYPE_PROFILE_TEST)
                .setBaseType(FLAG_PROFILE)
                .setMaxAllowedPerParent(2)
                .setLabel(0)
                .setIconBadge(com.android.internal.R.drawable.ic_test_icon_badge_experiment)
                .setBadgePlain(com.android.internal.R.drawable.ic_test_badge_experiment)
                .setBadgeNoBackground(com.android.internal.R.drawable.ic_test_badge_no_background)
                .setBadgeLabels(
                        com.android.internal.R.string.managed_profile_label_badge,
                        com.android.internal.R.string.managed_profile_label_badge_2,
                        com.android.internal.R.string.managed_profile_label_badge_3)
                .setBadgeColors(
                        com.android.internal.R.color.profile_badge_1,
                        com.android.internal.R.color.profile_badge_2,
                        com.android.internal.R.color.profile_badge_3)
                .setDarkThemeBadgeColors(
                        com.android.internal.R.color.profile_badge_1_dark,
                        com.android.internal.R.color.profile_badge_2_dark,
                        com.android.internal.R.color.profile_badge_3_dark)
                .setDefaultRestrictions(restrictions)
                .setDefaultSecureSettings(getDefaultNonManagedProfileSecureSettings());
    }

    /**
     * Returns the Builder for the default {@link UserManager#USER_TYPE_FULL_SECONDARY}
     * configuration.
     */
    private static UserTypeDetails.Builder getDefaultTypeFullSecondary() {
        return new UserTypeDetails.Builder()
                .setName(USER_TYPE_FULL_SECONDARY)
                .setBaseType(FLAG_FULL)
                .setMaxAllowed(UNLIMITED_NUMBER_OF_USERS)
                .setDefaultRestrictions(getDefaultSecondaryUserRestrictions());
    }

    /**
     * Returns the Builder for the default {@link UserManager#USER_TYPE_FULL_GUEST} configuration.
     */
    private static UserTypeDetails.Builder getDefaultTypeFullGuest() {
        final boolean ephemeralGuests = Resources.getSystem()
                .getBoolean(com.android.internal.R.bool.config_guestUserEphemeral);
        final int flags = FLAG_GUEST | (ephemeralGuests ? FLAG_EPHEMERAL : 0);

        return new UserTypeDetails.Builder()
                .setName(USER_TYPE_FULL_GUEST)
                .setBaseType(FLAG_FULL)
                .setDefaultUserInfoPropertyFlags(flags)
                .setMaxAllowed(1)
                .setDefaultRestrictions(getDefaultGuestUserRestrictions());
    }

    /**
     * Returns the Builder for the default {@link UserManager#USER_TYPE_FULL_DEMO} configuration.
     */
    private static UserTypeDetails.Builder getDefaultTypeFullDemo() {
        return new UserTypeDetails.Builder()
                .setName(USER_TYPE_FULL_DEMO)
                .setBaseType(FLAG_FULL)
                .setDefaultUserInfoPropertyFlags(FLAG_DEMO)
                .setMaxAllowed(UNLIMITED_NUMBER_OF_USERS)
                .setDefaultRestrictions(null);
    }

    /**
     * Returns the Builder for the default {@link UserManager#USER_TYPE_FULL_RESTRICTED}
     * configuration.
     */
    private static UserTypeDetails.Builder getDefaultTypeFullRestricted() {
        return new UserTypeDetails.Builder()
                .setName(USER_TYPE_FULL_RESTRICTED)
                .setBaseType(FLAG_FULL)
                .setDefaultUserInfoPropertyFlags(FLAG_RESTRICTED)
                .setMaxAllowed(UNLIMITED_NUMBER_OF_USERS)
                // NB: UserManagerService.createRestrictedProfile() applies hardcoded restrictions.
                .setDefaultRestrictions(null);
    }

    /**
     * Returns the Builder for the default {@link UserManager#USER_TYPE_FULL_SYSTEM} configuration.
     */
    private static UserTypeDetails.Builder getDefaultTypeFullSystem() {
        return new UserTypeDetails.Builder()
                .setName(USER_TYPE_FULL_SYSTEM)
                .setBaseType(FLAG_SYSTEM | FLAG_FULL)
                .setDefaultUserInfoPropertyFlags(FLAG_PRIMARY | FLAG_ADMIN | FLAG_MAIN)
                .setMaxAllowed(1);
    }

    /**
     * Returns the Builder for the default {@link UserManager#USER_TYPE_SYSTEM_HEADLESS}
     * configuration.
     */
    private static UserTypeDetails.Builder getDefaultTypeSystemHeadless() {
        return new UserTypeDetails.Builder()
                .setName(USER_TYPE_SYSTEM_HEADLESS)
                .setBaseType(FLAG_SYSTEM)
                .setDefaultUserInfoPropertyFlags(FLAG_PRIMARY | FLAG_ADMIN)
                .setMaxAllowed(1);
    }

    private static Bundle getDefaultSecondaryUserRestrictions() {
        final Bundle restrictions = new Bundle();
        restrictions.putBoolean(UserManager.DISALLOW_OUTGOING_CALLS, true);
        restrictions.putBoolean(UserManager.DISALLOW_SMS, true);
        return restrictions;
    }

    private static Bundle getDefaultGuestUserRestrictions() {
        // Guest inherits the secondary user's restrictions, plus has some extra ones.
        final Bundle restrictions = getDefaultSecondaryUserRestrictions();
        restrictions.putBoolean(UserManager.DISALLOW_CONFIG_WIFI, true);
        restrictions.putBoolean(UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES, true);
        restrictions.putBoolean(UserManager.DISALLOW_CONFIG_CREDENTIALS, true);
        return restrictions;
    }

    private static Bundle getDefaultManagedProfileRestrictions() {
        final Bundle restrictions = new Bundle();
        restrictions.putBoolean(UserManager.DISALLOW_WALLPAPER, true);
        return restrictions;
    }

    private static Bundle getDefaultManagedProfileSecureSettings() {
        // Only add String values to the bundle, settings are written as Strings eventually
        final Bundle settings = new Bundle();
        settings.putString(
                android.provider.Settings.Secure.MANAGED_PROFILE_CONTACT_REMOTE_SEARCH, "1");
        settings.putString(
                android.provider.Settings.Secure.CROSS_PROFILE_CALENDAR_ENABLED, "1");
        return settings;
    }

    private static List<DefaultCrossProfileIntentFilter>
            getDefaultManagedCrossProfileIntentFilter() {
        return DefaultCrossProfileIntentFiltersUtils.getDefaultManagedProfileFilters();
    }

    private static List<DefaultCrossProfileIntentFilter> getDefaultCloneCrossProfileIntentFilter() {
        return DefaultCrossProfileIntentFiltersUtils.getDefaultCloneProfileFilters();
    }

    /** Gets a default bundle, keyed by Settings.Secure String names, for non-managed profiles. */
    private static Bundle getDefaultNonManagedProfileSecureSettings() {
        final Bundle settings = new Bundle();
        // Non-managed profiles go through neither SetupWizard nor DPC flows, so we automatically
        // mark them as setup.
        settings.putString(android.provider.Settings.Secure.USER_SETUP_COMPLETE, "1");
        return settings;
    }

    /**
     * Reads the given xml parser to obtain device user-type customization, and updates the given
     * map of {@link UserTypeDetails.Builder}s accordingly.
     * <p>
     * The xml file can specify the attributes according to the set... methods below.
     */
    // TODO(b/176973369): Add parsing logic to support custom settings/filters
    //  in config_user_types.xml
    @VisibleForTesting
    static void customizeBuilders(ArrayMap<String, UserTypeDetails.Builder> builders,
            XmlResourceParser parser) {
        try {
            XmlUtils.beginDocument(parser, "user-types");
            for (XmlUtils.nextElement(parser);
                    parser.getEventType() != XmlResourceParser.END_DOCUMENT;
                    XmlUtils.nextElement(parser)) {
                final boolean isProfile;
                final String elementName = parser.getName();
                if ("profile-type".equals(elementName)) {
                    isProfile = true;
                } else if ("full-type".equals(elementName)) {
                    isProfile = false;
                } else if ("change-user-type".equals(elementName)) {
                    // parsed in parseUserUpgrades
                    XmlUtils.skipCurrentTag(parser);
                    continue;
                } else {
                    Slog.w(LOG_TAG, "Skipping unknown element " + elementName + " in "
                                + parser.getPositionDescription());
                    XmlUtils.skipCurrentTag(parser);
                    continue;
                }

                String typeName = parser.getAttributeValue(null, "name");
                if (typeName == null || typeName.equals("")) {
                    Slog.w(LOG_TAG, "Skipping user type with no name in "
                            + parser.getPositionDescription());
                    XmlUtils.skipCurrentTag(parser);
                    continue;
                }
                typeName = typeName.intern();

                UserTypeDetails.Builder builder;
                if (typeName.startsWith("android.")) {
                    // typeName refers to a AOSP-defined type which we are modifying.
                    Slog.i(LOG_TAG, "Customizing user type " + typeName);
                    builder = builders.get(typeName);
                    if (builder == null) {
                        throw new IllegalArgumentException("Illegal custom user type name "
                                + typeName + ": Non-AOSP user types cannot start with 'android.'");
                    }
                    final boolean isValid =
                            (isProfile && builder.getBaseType() == UserInfo.FLAG_PROFILE)
                            || (!isProfile && builder.getBaseType() == UserInfo.FLAG_FULL);
                    if (!isValid) {
                        throw new IllegalArgumentException("Wrong base type to customize user type "
                                + "(" + typeName + "), which is type "
                                + UserInfo.flagsToString(builder.getBaseType()));
                    }
                } else if (isProfile) {
                    // typeName refers to a new OEM-defined profile type which we are defining.
                    Slog.i(LOG_TAG, "Creating custom user type " + typeName);
                    builder = new UserTypeDetails.Builder();
                    builder.setName(typeName);
                    builder.setBaseType(FLAG_PROFILE);
                    builders.put(typeName, builder);
                } else {
                    throw new IllegalArgumentException("Creation of non-profile user type "
                            + "(" + typeName + ") is not currently supported.");
                }

                // Process the attributes (other than name).
                if (isProfile) {
                    setIntAttribute(parser, "max-allowed-per-parent",
                            builder::setMaxAllowedPerParent);
                    setResAttribute(parser, "icon-badge", builder::setIconBadge);
                    setResAttribute(parser, "badge-plain", builder::setBadgePlain);
                    setResAttribute(parser, "badge-no-background", builder::setBadgeNoBackground);
                }

                setIntAttribute(parser, "enabled", builder::setEnabled);
                setIntAttribute(parser, "max-allowed", builder::setMaxAllowed);

                // Process child elements.
                final int depth = parser.getDepth();
                while (XmlUtils.nextElementWithin(parser, depth)) {
                    final String childName = parser.getName();
                    if ("default-restrictions".equals(childName)) {
                        final Bundle restrictions = UserRestrictionsUtils
                                .readRestrictions(XmlUtils.makeTyped(parser));
                        builder.setDefaultRestrictions(restrictions);
                    } else if (isProfile && "badge-labels".equals(childName)) {
                        setResAttributeArray(parser, builder::setBadgeLabels);
                    } else if (isProfile && "badge-colors".equals(childName)) {
                        setResAttributeArray(parser, builder::setBadgeColors);
                    } else if (isProfile && "badge-colors-dark".equals(childName)) {
                        setResAttributeArray(parser, builder::setDarkThemeBadgeColors);
                    } else if ("user-properties".equals(childName)) {
                        builder.getDefaultUserProperties()
                                .updateFromXml(XmlUtils.makeTyped(parser));
                    } else {
                        Slog.w(LOG_TAG, "Unrecognized tag " + childName + " in "
                                + parser.getPositionDescription());
                    }
                }
            }
        } catch (XmlPullParserException | IOException e) {
            Slog.w(LOG_TAG, "Cannot read user type configuration file.", e);
        }
    }

    /**
     * If the given attribute exists, gets the int stored in it and performs the given fcn using it.
     * The stored value must be an int or NumberFormatException will be thrown.
     *
     * @param parser xml parser from which to read the attribute
     * @param attributeName name of the attribute
     * @param fcn one-int-argument function,
     *            like {@link UserTypeDetails.Builder#setMaxAllowedPerParent(int)}
     */
    private static void setIntAttribute(XmlResourceParser parser, String attributeName,
            Consumer<Integer> fcn) {
        final String intValue = parser.getAttributeValue(null, attributeName);
        if (intValue == null) {
            return;
        }
        try {
            fcn.accept(Integer.parseInt(intValue));
        } catch (NumberFormatException e) {
            Slog.e(LOG_TAG, "Cannot parse value of '" + intValue + "' for " + attributeName
                    + " in " + parser.getPositionDescription(), e);
            throw e;
        }
    }

    /**
     * If the given attribute exists, gets the resId stored in it (or 0 if it is not a valid resId)
     * and performs the given fcn using it.
     *
     * @param parser xml parser from which to read the attribute
     * @param attributeName name of the attribute
     * @param fcn one-argument function, like {@link UserTypeDetails.Builder#setIconBadge(int)}
     */
    private static void setResAttribute(XmlResourceParser parser, String attributeName,
            Consumer<Integer> fcn) {
        if (parser.getAttributeValue(null, attributeName) == null) {
            // Attribute is not present, i.e. use the default value.
            return;
        }
        final int resId = parser.getAttributeResourceValue(null, attributeName, Resources.ID_NULL);
        fcn.accept(resId);
    }

    /**
     * Gets the resIds stored in "item" elements (in their "res" attribute) at the current depth.
     * Then performs the given fcn using the int[] array of these resIds.
     * <p>
     * Each xml element is expected to be of the form {@code <item res="someResValue" />}.
     *
     * @param parser xml parser from which to read the elements and their attributes
     * @param fcn function, like {@link UserTypeDetails.Builder#setBadgeColors(int...)}
     */
    private static void setResAttributeArray(XmlResourceParser parser, Consumer<int[]> fcn)
            throws IOException, XmlPullParserException {

        ArrayList<Integer> resList = new ArrayList<>();
        final int depth = parser.getDepth();
        while (XmlUtils.nextElementWithin(parser, depth)) {
            final String elementName = parser.getName();
            if (!"item".equals(elementName)) {
                Slog.w(LOG_TAG, "Skipping unknown child element " + elementName + " in "
                        + parser.getPositionDescription());
                XmlUtils.skipCurrentTag(parser);
                continue;
            }
            final int resId = parser.getAttributeResourceValue(null, "res", -1);
            if (resId == -1) {
                continue;
            }
            resList.add(resId);
        }

        int[] result = new int[resList.size()];
        for (int i = 0; i < resList.size(); i++) {
            result[i] = resList.get(i);
        }
        fcn.accept(result);
    }

    /**
     * Returns the user type version of the config XML file.
     * @return user type version defined in XML file, 0 if none.
     */
    public static int getUserTypeVersion() {
        try (XmlResourceParser parser =
                     Resources.getSystem().getXml(com.android.internal.R.xml.config_user_types)) {
            return getUserTypeVersion(parser);
        }
    }

    @VisibleForTesting
    static int getUserTypeVersion(XmlResourceParser parser) {
        int version = 0;

        try {
            XmlUtils.beginDocument(parser, "user-types");
            String versionValue = parser.getAttributeValue(null, "version");
            if (versionValue != null) {
                try {
                    version = Integer.parseInt(versionValue);
                } catch (NumberFormatException e) {
                    Slog.e(LOG_TAG, "Cannot parse value of '" + versionValue + "' for version in "
                            + parser.getPositionDescription(), e);
                    throw e;
                }
            }
        } catch (XmlPullParserException | IOException e) {
            Slog.w(LOG_TAG, "Cannot read user type configuration file.", e);
        }

        return version;
    }

    /**
     * Obtains the user type upgrades for this device.
     * @return The list of user type upgrades.
     */
    public static List<UserTypeUpgrade> getUserTypeUpgrades() {
        final List<UserTypeUpgrade> userUpgrades;
        try (XmlResourceParser parser =
                     Resources.getSystem().getXml(com.android.internal.R.xml.config_user_types)) {
            userUpgrades = parseUserUpgrades(getDefaultBuilders(), parser);
        }
        return userUpgrades;
    }

    @VisibleForTesting
    static List<UserTypeUpgrade> parseUserUpgrades(
            ArrayMap<String, UserTypeDetails.Builder> builders, XmlResourceParser parser) {
        final List<UserTypeUpgrade> userUpgrades = new ArrayList<>();

        try {
            XmlUtils.beginDocument(parser, "user-types");
            for (XmlUtils.nextElement(parser);
                    parser.getEventType() != XmlResourceParser.END_DOCUMENT;
                    XmlUtils.nextElement(parser)) {
                final String elementName = parser.getName();
                if ("change-user-type".equals(elementName)) {
                    final String fromType = parser.getAttributeValue(null, "from");
                    final String toType = parser.getAttributeValue(null, "to");
                    // Check that the base type doesn't change.
                    // Currently, only the base type of PROFILE is supported.
                    validateUserTypeIsProfile(fromType, builders);
                    validateUserTypeIsProfile(toType, builders);

                    final int maxVersionToConvert;
                    try {
                        maxVersionToConvert = Integer.parseInt(
                                parser.getAttributeValue(null, "whenVersionLeq"));
                    } catch (NumberFormatException e) {
                        Slog.e(LOG_TAG, "Cannot parse value of whenVersionLeq in "
                                + parser.getPositionDescription(), e);
                        throw e;
                    }

                    UserTypeUpgrade userTypeUpgrade = new UserTypeUpgrade(fromType, toType,
                            maxVersionToConvert);
                    userUpgrades.add(userTypeUpgrade);
                    continue;
                } else {
                    XmlUtils.skipCurrentTag(parser);
                    continue;
                }
            }
        } catch (XmlPullParserException | IOException e) {
            Slog.w(LOG_TAG, "Cannot read user type configuration file.", e);
        }

        return userUpgrades;
    }

    private static void validateUserTypeIsProfile(String userType,
            ArrayMap<String, UserTypeDetails.Builder> builders) {
        UserTypeDetails.Builder builder = builders.get(userType);
        if (builder != null && builder.getBaseType() != FLAG_PROFILE) {
            throw new IllegalArgumentException("Illegal upgrade of user type " + userType
                    + " : Can only upgrade profiles user types");
        }
    }

    /**
     * Contains details required for an upgrade operation for {@link UserTypeDetails};
     */
    public static class UserTypeUpgrade {
        private final String mFromType;
        private final String mToType;
        private final int mUpToVersion;

        public UserTypeUpgrade(String fromType, String toType, int upToVersion) {
            mFromType = fromType;
            mToType = toType;
            mUpToVersion = upToVersion;
        }

        public String getFromType() {
            return mFromType;
        }

        public String getToType() {
            return mToType;
        }

        public int getUpToVersion() {
            return mUpToVersion;
        }
    }
}