blob: 7d45516bdefcb578e721cca17c1d9471533e6c9c [file] [log] [blame]
Bookatz029832a2019-10-04 16:50:22 -07001/*
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
17package com.android.server.pm;
18
19import static android.content.pm.UserInfo.FLAG_DEMO;
20import static android.content.pm.UserInfo.FLAG_EPHEMERAL;
21import static android.content.pm.UserInfo.FLAG_FULL;
22import static android.content.pm.UserInfo.FLAG_GUEST;
23import static android.content.pm.UserInfo.FLAG_MANAGED_PROFILE;
24import static android.content.pm.UserInfo.FLAG_PROFILE;
25import static android.content.pm.UserInfo.FLAG_RESTRICTED;
26import static android.content.pm.UserInfo.FLAG_SYSTEM;
27import static android.os.UserManager.USER_TYPE_FULL_DEMO;
28import static android.os.UserManager.USER_TYPE_FULL_GUEST;
29import static android.os.UserManager.USER_TYPE_FULL_RESTRICTED;
30import static android.os.UserManager.USER_TYPE_FULL_SECONDARY;
31import static android.os.UserManager.USER_TYPE_FULL_SYSTEM;
32import static android.os.UserManager.USER_TYPE_PROFILE_MANAGED;
33import static android.os.UserManager.USER_TYPE_SYSTEM_HEADLESS;
34
35import static com.android.server.pm.UserTypeDetails.UNLIMITED_NUMBER_OF_USERS;
36
Bookatzd15b2f72019-11-26 16:58:57 -080037import android.content.pm.UserInfo;
Bookatz029832a2019-10-04 16:50:22 -070038import android.content.res.Resources;
Bookatz95f02702019-12-03 17:15:49 -080039import android.content.res.XmlResourceParser;
Bookatzd15b2f72019-11-26 16:58:57 -080040import android.os.Bundle;
Bookatz029832a2019-10-04 16:50:22 -070041import android.os.UserManager;
42import android.util.ArrayMap;
Bookatz95f02702019-12-03 17:15:49 -080043import android.util.Slog;
44
45import com.android.internal.annotations.VisibleForTesting;
46import com.android.internal.util.XmlUtils;
47
48import org.xmlpull.v1.XmlPullParserException;
49
50import java.io.IOException;
51import java.util.ArrayList;
52import java.util.function.Consumer;
Bookatz029832a2019-10-04 16:50:22 -070053
54/**
55 * Class for creating all {@link UserTypeDetails} on the device.
56 *
Bookatz95f02702019-12-03 17:15:49 -080057 * 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 *
Bookatz029832a2019-10-04 16:50:22 -070060 * Tests are located in UserManagerServiceUserTypeTest.java.
61 * @hide
62 */
63public final class UserTypeFactory {
64
Bookatz95f02702019-12-03 17:15:49 -080065 private static final String LOG_TAG = "UserTypeFactory";
66
Bookatz029832a2019-10-04 16:50:22 -070067 /** 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() {
Bookatz95f02702019-12-03 17:15:49 -080076 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
Bookatzd15b2f72019-11-26 16:58:57 -080085 try (XmlResourceParser parser =
86 Resources.getSystem().getXml(com.android.internal.R.xml.config_user_types)) {
Bookatz95f02702019-12-03 17:15:49 -080087 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;
Bookatz029832a2019-10-04 16:50:22 -070095 }
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,
Bookatzd15b2f72019-11-26 16:58:57 -0800118 com.android.internal.R.color.profile_badge_3)
119 .setDefaultRestrictions(null);
Bookatz029832a2019-10-04 16:50:22 -0700120 }
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)
Bookatzd15b2f72019-11-26 16:58:57 -0800130 .setMaxAllowed(UNLIMITED_NUMBER_OF_USERS)
131 .setDefaultRestrictions(getDefaultSecondaryUserRestrictions());
Bookatz029832a2019-10-04 16:50:22 -0700132 }
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
Bookatz029832a2019-10-04 16:50:22 -0700142 return new UserTypeDetails.Builder()
143 .setName(USER_TYPE_FULL_GUEST)
144 .setBaseType(FLAG_FULL)
145 .setDefaultUserInfoPropertyFlags(flags)
Bookatzd15b2f72019-11-26 16:58:57 -0800146 .setMaxAllowed(1)
147 .setDefaultRestrictions(getDefaultGuestUserRestrictions());
Bookatz029832a2019-10-04 16:50:22 -0700148 }
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)
Bookatzd15b2f72019-11-26 16:58:57 -0800158 .setMaxAllowed(UNLIMITED_NUMBER_OF_USERS)
159 .setDefaultRestrictions(null);
Bookatz029832a2019-10-04 16:50:22 -0700160 }
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)
Bookatzd15b2f72019-11-26 16:58:57 -0800171 .setMaxAllowed(UNLIMITED_NUMBER_OF_USERS)
172 // NB: UserManagerService.createRestrictedProfile() applies hardcoded restrictions.
173 .setDefaultRestrictions(null);
Bookatz029832a2019-10-04 16:50:22 -0700174 }
175
176 /**
177 * Returns the Builder for the default {@link UserManager#USER_TYPE_FULL_SYSTEM} configuration.
178 */
Bookatz95f02702019-12-03 17:15:49 -0800179 private static UserTypeDetails.Builder getDefaultTypeFullSystem() {
Bookatz029832a2019-10-04 16:50:22 -0700180 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 }
Bookatz95f02702019-12-03 17:15:49 -0800194
Bookatzd15b2f72019-11-26 16:58:57 -0800195 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
Bookatz95f02702019-12-03 17:15:49 -0800210 /**
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)) {
Bookatzd15b2f72019-11-26 16:58:57 -0800224 final boolean isProfile;
Bookatz95f02702019-12-03 17:15:49 -0800225 final String elementName = parser.getName();
Bookatzd15b2f72019-11-26 16:58:57 -0800226 if ("profile-type".equals(elementName)) {
227 isProfile = true;
228 } else if ("full-type".equals(elementName)) {
229 isProfile = false;
230 } else {
Bookatz95f02702019-12-03 17:15:49 -0800231 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) {
Bookatzd15b2f72019-11-26 16:58:57 -0800239 Slog.w(LOG_TAG, "Skipping user type with no name in "
Bookatz95f02702019-12-03 17:15:49 -0800240 + 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 }
Bookatzd15b2f72019-11-26 16:58:57 -0800255 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()));
Bookatz95f02702019-12-03 17:15:49 -0800262 }
Bookatzd15b2f72019-11-26 16:58:57 -0800263 } else if (isProfile) {
264 // typeName refers to a new OEM-defined profile type which we are defining.
Bookatz95f02702019-12-03 17:15:49 -0800265 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);
Bookatzd15b2f72019-11-26 16:58:57 -0800270 } else {
271 throw new IllegalArgumentException("Creation of non-profile user type "
272 + "(" + typeName + ") is not currently supported.");
Bookatz95f02702019-12-03 17:15:49 -0800273 }
274
275 // Process the attributes (other than name).
Bookatzd15b2f72019-11-26 16:58:57 -0800276 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 }
Bookatz95f02702019-12-03 17:15:49 -0800283
284 // Process child elements.
285 final int depth = parser.getDepth();
286 while (XmlUtils.nextElementWithin(parser, depth)) {
287 final String childName = parser.getName();
Bookatzd15b2f72019-11-26 16:58:57 -0800288 if ("default-restrictions".equals(childName)) {
289 final Bundle restrictions = UserRestrictionsUtils.readRestrictions(parser);
290 builder.setDefaultRestrictions(restrictions);
291 } else if (isProfile && "badge-labels".equals(childName)) {
Bookatz95f02702019-12-03 17:15:49 -0800292 setResAttributeArray(parser, builder::setBadgeLabels);
Bookatzd15b2f72019-11-26 16:58:57 -0800293 } else if (isProfile && "badge-colors".equals(childName)) {
Bookatz95f02702019-12-03 17:15:49 -0800294 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) {
Bookatzd15b2f72019-11-26 16:58:57 -0800324 Slog.e(LOG_TAG, "Cannot parse value of '" + intValue + "' for " + attributeName
325 + " in " + parser.getPositionDescription(), e);
Bookatz95f02702019-12-03 17:15:49 -0800326 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.
Bookatzd15b2f72019-11-26 16:58:57 -0800350 * Then performs the given fcn using the int[] array of these resIds.
Bookatz95f02702019-12-03 17:15:49 -0800351 * <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 }
Bookatz029832a2019-10-04 16:50:22 -0700383}