blob: 23185d7fb5ab1ee542c9cae65a36f3ce92c534ff [file] [log] [blame]
Makoto Onukia4f11972015-10-01 13:19:58 -07001/*
2 * Copyright (C) 2015 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 com.google.android.collect.Sets;
20
Makoto Onuki1a2cd742015-11-16 13:51:27 -080021import com.android.internal.util.Preconditions;
22
23import android.annotation.NonNull;
24import android.annotation.Nullable;
Fyodor Kupolov9cbfc9e2015-10-07 15:52:33 -070025import android.app.ActivityManager;
Makoto Onuki4f160732015-10-27 17:15:38 -070026import android.content.ContentResolver;
27import android.content.Context;
yuemingw265c6922018-01-11 18:31:14 +000028import android.content.Intent;
Makoto Onuki4f160732015-10-27 17:15:38 -070029import android.os.Binder;
Makoto Onukia4f11972015-10-01 13:19:58 -070030import android.os.Bundle;
Fyodor Kupolov9cbfc9e2015-10-07 15:52:33 -070031import android.os.RemoteException;
Makoto Onuki4f160732015-10-27 17:15:38 -070032import android.os.UserHandle;
Makoto Onukia4f11972015-10-01 13:19:58 -070033import android.os.UserManager;
Pavel Grafov6a40f092016-10-25 15:46:51 +010034import android.os.UserManagerInternal;
yuemingw265c6922018-01-11 18:31:14 +000035import android.provider.Settings;
Mahaver Chopradea471e2015-12-17 11:02:37 +000036import android.telephony.SubscriptionInfo;
37import android.telephony.SubscriptionManager;
Makoto Onuki1a2cd742015-11-16 13:51:27 -080038import android.util.Log;
Makoto Onuki1f1ceef2016-01-28 11:32:32 -080039import android.util.Slog;
Pavel Grafov6a40f092016-10-25 15:46:51 +010040import android.util.SparseArray;
Makoto Onukia4f11972015-10-01 13:19:58 -070041
42import org.xmlpull.v1.XmlPullParser;
43import org.xmlpull.v1.XmlSerializer;
44
45import java.io.IOException;
46import java.io.PrintWriter;
Mahaver Chopradea471e2015-12-17 11:02:37 +000047import java.util.List;
Makoto Onukia4f11972015-10-01 13:19:58 -070048import java.util.Set;
49
Makoto Onukid45a4a22015-11-02 17:17:38 -080050/**
Mahaver Chopradea471e2015-12-17 11:02:37 +000051 * Utility methods for user restrictions.
Makoto Onukid45a4a22015-11-02 17:17:38 -080052 *
53 * <p>See {@link UserManagerService} for the method suffixes.
54 */
Makoto Onukia4f11972015-10-01 13:19:58 -070055public class UserRestrictionsUtils {
Makoto Onuki4f160732015-10-27 17:15:38 -070056 private static final String TAG = "UserRestrictionsUtils";
57
Makoto Onukia4f11972015-10-01 13:19:58 -070058 private UserRestrictionsUtils() {
59 }
60
Makoto Onuki1f1ceef2016-01-28 11:32:32 -080061 private static Set<String> newSetWithUniqueCheck(String[] strings) {
62 final Set<String> ret = Sets.newArraySet(strings);
63
64 // Make sure there's no overlap.
65 Preconditions.checkState(ret.size() == strings.length);
66 return ret;
67 }
68
69 public static final Set<String> USER_RESTRICTIONS = newSetWithUniqueCheck(new String[] {
Makoto Onukia4f11972015-10-01 13:19:58 -070070 UserManager.DISALLOW_CONFIG_WIFI,
Christine Franks1bade5d2017-10-10 15:41:50 -070071 UserManager.DISALLOW_CONFIG_LOCALE,
Makoto Onukia4f11972015-10-01 13:19:58 -070072 UserManager.DISALLOW_MODIFY_ACCOUNTS,
73 UserManager.DISALLOW_INSTALL_APPS,
74 UserManager.DISALLOW_UNINSTALL_APPS,
75 UserManager.DISALLOW_SHARE_LOCATION,
76 UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES,
77 UserManager.DISALLOW_CONFIG_BLUETOOTH,
Lenka Trochtova63d5e4a72016-12-02 12:19:39 +010078 UserManager.DISALLOW_BLUETOOTH,
Pavel Grafov7f4ad752017-03-28 13:44:04 +010079 UserManager.DISALLOW_BLUETOOTH_SHARING,
Makoto Onukia4f11972015-10-01 13:19:58 -070080 UserManager.DISALLOW_USB_FILE_TRANSFER,
81 UserManager.DISALLOW_CONFIG_CREDENTIALS,
82 UserManager.DISALLOW_REMOVE_USER,
Esteban Talavera6c9116a2016-11-24 16:12:44 +000083 UserManager.DISALLOW_REMOVE_MANAGED_PROFILE,
Makoto Onukia4f11972015-10-01 13:19:58 -070084 UserManager.DISALLOW_DEBUGGING_FEATURES,
85 UserManager.DISALLOW_CONFIG_VPN,
yuemingwa9772f362017-10-23 18:34:35 +010086 UserManager.DISALLOW_CONFIG_DATE_TIME,
Makoto Onukia4f11972015-10-01 13:19:58 -070087 UserManager.DISALLOW_CONFIG_TETHERING,
88 UserManager.DISALLOW_NETWORK_RESET,
89 UserManager.DISALLOW_FACTORY_RESET,
90 UserManager.DISALLOW_ADD_USER,
Esteban Talavera6c9116a2016-11-24 16:12:44 +000091 UserManager.DISALLOW_ADD_MANAGED_PROFILE,
Makoto Onukia4f11972015-10-01 13:19:58 -070092 UserManager.ENSURE_VERIFY_APPS,
93 UserManager.DISALLOW_CONFIG_CELL_BROADCASTS,
94 UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS,
95 UserManager.DISALLOW_APPS_CONTROL,
96 UserManager.DISALLOW_MOUNT_PHYSICAL_MEDIA,
97 UserManager.DISALLOW_UNMUTE_MICROPHONE,
98 UserManager.DISALLOW_ADJUST_VOLUME,
99 UserManager.DISALLOW_OUTGOING_CALLS,
100 UserManager.DISALLOW_SMS,
101 UserManager.DISALLOW_FUN,
102 UserManager.DISALLOW_CREATE_WINDOWS,
Charles He22ff6f9d2017-10-05 21:28:55 +0100103 UserManager.DISALLOW_SYSTEM_ERROR_DIALOGS,
Makoto Onukia4f11972015-10-01 13:19:58 -0700104 UserManager.DISALLOW_CROSS_PROFILE_COPY_PASTE,
105 UserManager.DISALLOW_OUTGOING_BEAM,
106 UserManager.DISALLOW_WALLPAPER,
107 UserManager.DISALLOW_SAFE_BOOT,
108 UserManager.ALLOW_PARENT_PROFILE_APP_LINKING,
109 UserManager.DISALLOW_RECORD_AUDIO,
Fyodor Kupolov9cbfc9e2015-10-07 15:52:33 -0700110 UserManager.DISALLOW_CAMERA,
Mahaver Chopradea471e2015-12-17 11:02:37 +0000111 UserManager.DISALLOW_RUN_IN_BACKGROUND,
Oleksandr Peletskyi7f1f1df2016-01-18 15:40:21 +0100112 UserManager.DISALLOW_DATA_ROAMING,
Oleksandr Peletskyif2519812016-01-26 20:16:06 +0100113 UserManager.DISALLOW_SET_USER_ICON,
Mahaver Chopra3d9805d2016-07-07 16:25:05 +0100114 UserManager.DISALLOW_SET_WALLPAPER,
Tony Makc1205112016-07-22 16:02:59 +0100115 UserManager.DISALLOW_OEM_UNLOCK,
Esteban Talavera492b4722017-02-13 14:59:45 +0000116 UserManager.DISALLOW_UNMUTE_DEVICE,
Felipe Leme24d58932017-03-21 14:13:58 -0700117 UserManager.DISALLOW_AUTOFILL,
Pavel Grafovc4f87e92017-10-26 16:34:25 +0100118 UserManager.DISALLOW_USER_SWITCH,
119 UserManager.DISALLOW_UNIFIED_PASSWORD,
yuemingw7810b8b2018-02-01 17:32:25 +0000120 UserManager.DISALLOW_CONFIG_LOCATION,
yuemingwc6ac29d2018-01-10 16:54:08 +0000121 UserManager.DISALLOW_AIRPLANE_MODE,
Rubin Xucc391c22018-01-02 20:37:35 +0000122 UserManager.DISALLOW_CONFIG_BRIGHTNESS,
123 UserManager.DISALLOW_SHARE_INTO_MANAGED_PROFILE,
yuemingw5cda3ae2018-01-15 10:26:19 +0000124 UserManager.DISALLOW_AMBIENT_DISPLAY,
Vladislav Kuzkokov622b9f92018-01-25 16:33:05 +0100125 UserManager.DISALLOW_CONFIG_SCREEN_TIMEOUT,
126 UserManager.DISALLOW_PRINTING
Makoto Onuki1f1ceef2016-01-28 11:32:32 -0800127 });
Makoto Onukia4f11972015-10-01 13:19:58 -0700128
129 /**
130 * Set of user restriction which we don't want to persist.
131 */
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800132 private static final Set<String> NON_PERSIST_USER_RESTRICTIONS = Sets.newArraySet(
133 UserManager.DISALLOW_RECORD_AUDIO
134 );
Makoto Onukia4f11972015-10-01 13:19:58 -0700135
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800136 /**
Pavel Grafov6a40f092016-10-25 15:46:51 +0100137 * User restrictions that cannot be set by profile owners of secondary users. When set by DO
138 * they will be applied to all users.
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800139 */
Pavel Grafov6a40f092016-10-25 15:46:51 +0100140 private static final Set<String> PRIMARY_USER_ONLY_RESTRICTIONS = Sets.newArraySet(
Lenka Trochtova63d5e4a72016-12-02 12:19:39 +0100141 UserManager.DISALLOW_BLUETOOTH,
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800142 UserManager.DISALLOW_USB_FILE_TRANSFER,
143 UserManager.DISALLOW_CONFIG_TETHERING,
144 UserManager.DISALLOW_NETWORK_RESET,
145 UserManager.DISALLOW_FACTORY_RESET,
146 UserManager.DISALLOW_ADD_USER,
147 UserManager.DISALLOW_CONFIG_CELL_BROADCASTS,
148 UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS,
149 UserManager.DISALLOW_MOUNT_PHYSICAL_MEDIA,
150 UserManager.DISALLOW_SMS,
151 UserManager.DISALLOW_FUN,
152 UserManager.DISALLOW_SAFE_BOOT,
Mahaver Chopradea471e2015-12-17 11:02:37 +0000153 UserManager.DISALLOW_CREATE_WINDOWS,
yuemingw5fe75dc2017-11-29 15:52:56 +0000154 UserManager.DISALLOW_DATA_ROAMING,
155 UserManager.DISALLOW_AIRPLANE_MODE
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800156 );
157
158 /**
Benjamin Franzff66fa92017-08-10 10:39:44 +0100159 * User restrictions that cannot be set by profile owners. Applied to all users.
160 */
161 private static final Set<String> DEVICE_OWNER_ONLY_RESTRICTIONS = Sets.newArraySet(
162 UserManager.DISALLOW_USER_SWITCH
163 );
164
165 /**
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800166 * User restrictions that can't be changed by device owner or profile owner.
167 */
168 private static final Set<String> IMMUTABLE_BY_OWNERS = Sets.newArraySet(
169 UserManager.DISALLOW_RECORD_AUDIO,
Mahaver Chopra3d9805d2016-07-07 16:25:05 +0100170 UserManager.DISALLOW_WALLPAPER,
171 UserManager.DISALLOW_OEM_UNLOCK
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800172 );
173
174 /**
175 * Special user restrictions that can be applied to a user as well as to all users globally,
176 * depending on callers. When device owner sets them, they'll be applied to all users.
177 */
178 private static final Set<String> GLOBAL_RESTRICTIONS = Sets.newArraySet(
179 UserManager.DISALLOW_ADJUST_VOLUME,
Pavel Grafov7f4ad752017-03-28 13:44:04 +0100180 UserManager.DISALLOW_BLUETOOTH_SHARING,
yuemingwa9772f362017-10-23 18:34:35 +0100181 UserManager.DISALLOW_CONFIG_DATE_TIME,
Charles He22ff6f9d2017-10-05 21:28:55 +0100182 UserManager.DISALLOW_SYSTEM_ERROR_DIALOGS,
Fyodor Kupolov9cbfc9e2015-10-07 15:52:33 -0700183 UserManager.DISALLOW_RUN_IN_BACKGROUND,
Tony Makc1205112016-07-22 16:02:59 +0100184 UserManager.DISALLOW_UNMUTE_MICROPHONE,
Esteban Talavera492b4722017-02-13 14:59:45 +0000185 UserManager.DISALLOW_UNMUTE_DEVICE
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800186 );
187
Makoto Onuki1f1ceef2016-01-28 11:32:32 -0800188 /**
Esteban Talavera6c9116a2016-11-24 16:12:44 +0000189 * User restrictions that default to {@code true} for device owners.
190 */
191 private static final Set<String> DEFAULT_ENABLED_FOR_DEVICE_OWNERS = Sets.newArraySet(
192 UserManager.DISALLOW_ADD_MANAGED_PROFILE
193 );
194
Pavel Grafov7f4ad752017-03-28 13:44:04 +0100195 /**
196 * User restrictions that default to {@code true} for managed profile owners.
197 *
198 * NB: {@link UserManager#DISALLOW_INSTALL_UNKNOWN_SOURCES} is also set by default but it is
199 * not set to existing profile owners unless they used to have INSTALL_NON_MARKET_APPS disabled
200 * in settings. So it is handled separately.
201 */
202 private static final Set<String> DEFAULT_ENABLED_FOR_MANAGED_PROFILES = Sets.newArraySet(
203 UserManager.DISALLOW_BLUETOOTH_SHARING
204 );
205
Pavel Grafovc4f87e92017-10-26 16:34:25 +0100206 /**
Pavel Grafov6a40f092016-10-25 15:46:51 +0100207 * Special user restrictions that are always applied to all users no matter who sets them.
208 */
209 private static final Set<String> PROFILE_GLOBAL_RESTRICTIONS = Sets.newArraySet(
yuemingw5fe75dc2017-11-29 15:52:56 +0000210 UserManager.ENSURE_VERIFY_APPS,
211 UserManager.DISALLOW_AIRPLANE_MODE
Pavel Grafov6a40f092016-10-25 15:46:51 +0100212 );
213
Esteban Talavera6c9116a2016-11-24 16:12:44 +0000214 /**
Makoto Onuki1f1ceef2016-01-28 11:32:32 -0800215 * Throws {@link IllegalArgumentException} if the given restriction name is invalid.
216 */
217 public static boolean isValidRestriction(@NonNull String restriction) {
218 if (!USER_RESTRICTIONS.contains(restriction)) {
Makoto Onukiad5619d2016-02-10 13:41:15 -0800219 Slog.e(TAG, "Unknown restriction: " + restriction);
Makoto Onuki1f1ceef2016-01-28 11:32:32 -0800220 return false;
221 }
222 return true;
223 }
224
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800225 public static void writeRestrictions(@NonNull XmlSerializer serializer,
226 @Nullable Bundle restrictions, @NonNull String tag) throws IOException {
227 if (restrictions == null) {
228 return;
229 }
230
Makoto Onukia4f11972015-10-01 13:19:58 -0700231 serializer.startTag(null, tag);
Makoto Onukiac65e1e2015-11-20 15:33:17 -0800232 for (String key : restrictions.keySet()) {
233 if (NON_PERSIST_USER_RESTRICTIONS.contains(key)) {
234 continue; // Don't persist.
Makoto Onukia4f11972015-10-01 13:19:58 -0700235 }
Makoto Onukiac65e1e2015-11-20 15:33:17 -0800236 if (USER_RESTRICTIONS.contains(key)) {
237 if (restrictions.getBoolean(key)) {
238 serializer.attribute(null, key, "true");
239 }
240 continue;
241 }
242 Log.w(TAG, "Unknown user restriction detected: " + key);
Makoto Onukia4f11972015-10-01 13:19:58 -0700243 }
244 serializer.endTag(null, tag);
245 }
246
Esteban Talaverac48b20f2016-08-11 11:23:40 +0100247 public static void readRestrictions(XmlPullParser parser, Bundle restrictions) {
Fyodor Kupoloveafee022017-03-15 17:09:04 -0700248 restrictions.clear();
Makoto Onukia4f11972015-10-01 13:19:58 -0700249 for (String key : USER_RESTRICTIONS) {
250 final String value = parser.getAttributeValue(null, key);
251 if (value != null) {
252 restrictions.putBoolean(key, Boolean.parseBoolean(value));
253 }
254 }
255 }
256
Pavel Grafov6a40f092016-10-25 15:46:51 +0100257 public static Bundle readRestrictions(XmlPullParser parser) {
258 final Bundle result = new Bundle();
259 readRestrictions(parser, result);
260 return result;
261 }
262
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800263 /**
264 * @return {@code in} itself when it's not null, or an empty bundle (which can writable).
265 */
266 public static Bundle nonNull(@Nullable Bundle in) {
267 return in != null ? in : new Bundle();
268 }
269
270 public static boolean isEmpty(@Nullable Bundle in) {
271 return (in == null) || (in.size() == 0);
272 }
273
274 /**
Pavel Grafov6a40f092016-10-25 15:46:51 +0100275 * Returns {@code true} if given bundle is not null and contains {@code true} for a given
276 * restriction.
277 */
278 public static boolean contains(@Nullable Bundle in, String restriction) {
279 return in != null && in.getBoolean(restriction);
280 }
281
282 /**
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800283 * Creates a copy of the {@code in} Bundle. If {@code in} is null, it'll return an empty
284 * bundle.
285 *
286 * <p>The resulting {@link Bundle} is always writable. (i.e. it won't return
287 * {@link Bundle#EMPTY})
288 */
289 public static @NonNull Bundle clone(@Nullable Bundle in) {
290 return (in != null) ? new Bundle(in) : new Bundle();
291 }
292
293 public static void merge(@NonNull Bundle dest, @Nullable Bundle in) {
294 Preconditions.checkNotNull(dest);
295 Preconditions.checkArgument(dest != in);
Makoto Onuki068c54a2015-10-13 14:34:03 -0700296 if (in == null) {
297 return;
298 }
299 for (String key : in.keySet()) {
300 if (in.getBoolean(key, false)) {
301 dest.putBoolean(key, true);
302 }
303 }
304 }
305
Makoto Onuki4f160732015-10-27 17:15:38 -0700306 /**
Pavel Grafov6a40f092016-10-25 15:46:51 +0100307 * Merges a sparse array of restrictions bundles into one.
308 */
309 @Nullable
310 public static Bundle mergeAll(SparseArray<Bundle> restrictions) {
311 if (restrictions.size() == 0) {
312 return null;
313 } else {
314 final Bundle result = new Bundle();
315 for (int i = 0; i < restrictions.size(); i++) {
316 merge(result, restrictions.valueAt(i));
317 }
318 return result;
319 }
320 }
321
322 /**
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800323 * @return true if a restriction is settable by device owner.
324 */
325 public static boolean canDeviceOwnerChange(String restriction) {
326 return !IMMUTABLE_BY_OWNERS.contains(restriction);
327 }
328
329 /**
Makoto Onuki5485ed42015-12-09 11:38:23 -0800330 * @return true if a restriction is settable by profile owner. Note it takes a user ID because
331 * some restrictions can be changed by PO only when it's running on the system user.
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800332 */
Makoto Onuki5485ed42015-12-09 11:38:23 -0800333 public static boolean canProfileOwnerChange(String restriction, int userId) {
334 return !IMMUTABLE_BY_OWNERS.contains(restriction)
Benjamin Franzff66fa92017-08-10 10:39:44 +0100335 && !DEVICE_OWNER_ONLY_RESTRICTIONS.contains(restriction)
Makoto Onuki5485ed42015-12-09 11:38:23 -0800336 && !(userId != UserHandle.USER_SYSTEM
Pavel Grafov6a40f092016-10-25 15:46:51 +0100337 && PRIMARY_USER_ONLY_RESTRICTIONS.contains(restriction));
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800338 }
339
340 /**
Esteban Talavera6c9116a2016-11-24 16:12:44 +0000341 * Returns the user restrictions that default to {@code true} for device owners.
Nicolas Prevot2ea46fe2017-01-05 10:29:34 +0000342 * These user restrictions are local, though. ie only for the device owner's user id.
Esteban Talavera6c9116a2016-11-24 16:12:44 +0000343 */
344 public static @NonNull Set<String> getDefaultEnabledForDeviceOwner() {
345 return DEFAULT_ENABLED_FOR_DEVICE_OWNERS;
346 }
347
348 /**
Pavel Grafov7f4ad752017-03-28 13:44:04 +0100349 * Returns the user restrictions that default to {@code true} for managed profile owners.
350 */
351 public static @NonNull Set<String> getDefaultEnabledForManagedProfiles() {
352 return DEFAULT_ENABLED_FOR_MANAGED_PROFILES;
353 }
354
355 /**
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800356 * Takes restrictions that can be set by device owner, and sort them into what should be applied
357 * globally and what should be applied only on the current user.
358 */
Pavel Grafov6a40f092016-10-25 15:46:51 +0100359 public static void sortToGlobalAndLocal(@Nullable Bundle in, boolean isDeviceOwner,
360 int cameraRestrictionScope,
361 @NonNull Bundle global, @NonNull Bundle local) {
362 // Camera restriction (as well as all others) goes to at most one bundle.
363 if (cameraRestrictionScope == UserManagerInternal.CAMERA_DISABLED_GLOBALLY) {
364 global.putBoolean(UserManager.DISALLOW_CAMERA, true);
365 } else if (cameraRestrictionScope == UserManagerInternal.CAMERA_DISABLED_LOCALLY) {
366 local.putBoolean(UserManager.DISALLOW_CAMERA, true);
367 }
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800368 if (in == null || in.size() == 0) {
369 return;
370 }
371 for (String key : in.keySet()) {
372 if (!in.getBoolean(key)) {
373 continue;
374 }
Pavel Grafov6a40f092016-10-25 15:46:51 +0100375 if (isGlobal(isDeviceOwner, key)) {
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800376 global.putBoolean(key, true);
377 } else {
378 local.putBoolean(key, true);
379 }
380 }
381 }
382
383 /**
Pavel Grafov6a40f092016-10-25 15:46:51 +0100384 * Whether given user restriction should be enforced globally.
385 */
386 private static boolean isGlobal(boolean isDeviceOwner, String key) {
387 return (isDeviceOwner &&
Benjamin Franzff66fa92017-08-10 10:39:44 +0100388 (PRIMARY_USER_ONLY_RESTRICTIONS.contains(key) || GLOBAL_RESTRICTIONS.contains(key)))
389 || PROFILE_GLOBAL_RESTRICTIONS.contains(key)
390 || DEVICE_OWNER_ONLY_RESTRICTIONS.contains(key);
Pavel Grafov6a40f092016-10-25 15:46:51 +0100391 }
392
393 /**
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800394 * @return true if two Bundles contain the same user restriction.
395 * A null bundle and an empty bundle are considered to be equal.
396 */
397 public static boolean areEqual(@Nullable Bundle a, @Nullable Bundle b) {
398 if (a == b) {
399 return true;
400 }
401 if (isEmpty(a)) {
402 return isEmpty(b);
403 }
404 if (isEmpty(b)) {
405 return false;
406 }
407 for (String key : a.keySet()) {
408 if (a.getBoolean(key) != b.getBoolean(key)) {
409 return false;
410 }
411 }
412 for (String key : b.keySet()) {
413 if (a.getBoolean(key) != b.getBoolean(key)) {
414 return false;
415 }
416 }
417 return true;
418 }
419
420 /**
Makoto Onuki4f160732015-10-27 17:15:38 -0700421 * Takes a new use restriction set and the previous set, and apply the restrictions that have
422 * changed.
Makoto Onukid45a4a22015-11-02 17:17:38 -0800423 *
Fyodor Kupolov9cbfc9e2015-10-07 15:52:33 -0700424 * <p>Note this method is called by {@link UserManagerService} without holding any locks.
Makoto Onuki4f160732015-10-27 17:15:38 -0700425 */
Fyodor Kupolov9cbfc9e2015-10-07 15:52:33 -0700426 public static void applyUserRestrictions(Context context, int userId,
Makoto Onukid45a4a22015-11-02 17:17:38 -0800427 Bundle newRestrictions, Bundle prevRestrictions) {
Makoto Onuki4f160732015-10-27 17:15:38 -0700428 for (String key : USER_RESTRICTIONS) {
429 final boolean newValue = newRestrictions.getBoolean(key);
430 final boolean prevValue = prevRestrictions.getBoolean(key);
431
432 if (newValue != prevValue) {
Fyodor Kupolov9cbfc9e2015-10-07 15:52:33 -0700433 applyUserRestriction(context, userId, key, newValue);
Makoto Onuki4f160732015-10-27 17:15:38 -0700434 }
435 }
436 }
Fyodor Kupolov9cbfc9e2015-10-07 15:52:33 -0700437
Makoto Onukid45a4a22015-11-02 17:17:38 -0800438 /**
439 * Apply each user restriction.
440 *
Makoto Onuki28da2e32015-11-20 11:30:44 -0800441 * <p>See also {@link
442 * com.android.providers.settings.SettingsProvider#isGlobalOrSecureSettingRestrictedForUser},
443 * which should be in sync with this method.
Makoto Onukid45a4a22015-11-02 17:17:38 -0800444 */
Fyodor Kupolov9cbfc9e2015-10-07 15:52:33 -0700445 private static void applyUserRestriction(Context context, int userId, String key,
Makoto Onuki4f160732015-10-27 17:15:38 -0700446 boolean newValue) {
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800447 if (UserManagerService.DBG) {
448 Log.d(TAG, "Applying user restriction: userId=" + userId
449 + " key=" + key + " value=" + newValue);
450 }
Makoto Onuki4f160732015-10-27 17:15:38 -0700451 // When certain restrictions are cleared, we don't update the system settings,
452 // because these settings are changeable on the Settings UI and we don't know the original
453 // value -- for example LOCATION_MODE might have been off already when the restriction was
454 // set, and in that case even if the restriction is lifted, changing it to ON would be
455 // wrong. So just don't do anything in such a case. If the user hopes to enable location
456 // later, they can do it on the Settings UI.
Benjamin Franz0ff13fc2016-07-12 13:42:21 +0100457 // WARNING: Remember that Settings.Global and Settings.Secure are changeable via adb.
458 // To prevent this from happening for a given user restriction, you have to add a check to
459 // SettingsProvider.isGlobalOrSecureSettingRestrictedForUser.
Makoto Onuki4f160732015-10-27 17:15:38 -0700460
461 final ContentResolver cr = context.getContentResolver();
462 final long id = Binder.clearCallingIdentity();
463 try {
464 switch (key) {
Mahaver Chopradea471e2015-12-17 11:02:37 +0000465 case UserManager.DISALLOW_DATA_ROAMING:
466 if (newValue) {
467 // DISALLOW_DATA_ROAMING user restriction is set.
468
469 // Multi sim device.
Jeff Sharkey717f52f2018-01-04 16:04:11 -0700470 SubscriptionManager subscriptionManager = context
471 .getSystemService(SubscriptionManager.class);
Mahaver Chopradea471e2015-12-17 11:02:37 +0000472 final List<SubscriptionInfo> subscriptionInfoList =
473 subscriptionManager.getActiveSubscriptionInfoList();
474 if (subscriptionInfoList != null) {
475 for (SubscriptionInfo subInfo : subscriptionInfoList) {
476 android.provider.Settings.Global.putStringForUser(cr,
477 android.provider.Settings.Global.DATA_ROAMING
478 + subInfo.getSubscriptionId(), "0", userId);
479 }
480 }
481
482 // Single sim device.
483 android.provider.Settings.Global.putStringForUser(cr,
484 android.provider.Settings.Global.DATA_ROAMING, "0", userId);
485 }
486 break;
Makoto Onuki4f160732015-10-27 17:15:38 -0700487 case UserManager.DISALLOW_SHARE_LOCATION:
488 if (newValue) {
489 android.provider.Settings.Secure.putIntForUser(cr,
490 android.provider.Settings.Secure.LOCATION_MODE,
491 android.provider.Settings.Secure.LOCATION_MODE_OFF,
492 userId);
Makoto Onuki4f160732015-10-27 17:15:38 -0700493 }
Makoto Onuki4f160732015-10-27 17:15:38 -0700494 break;
495 case UserManager.DISALLOW_DEBUGGING_FEATURES:
496 if (newValue) {
497 // Only disable adb if changing for system user, since it is global
498 // TODO: should this be admin user?
499 if (userId == UserHandle.USER_SYSTEM) {
500 android.provider.Settings.Global.putStringForUser(cr,
501 android.provider.Settings.Global.ADB_ENABLED, "0",
502 userId);
503 }
504 }
505 break;
506 case UserManager.ENSURE_VERIFY_APPS:
507 if (newValue) {
508 android.provider.Settings.Global.putStringForUser(
509 context.getContentResolver(),
510 android.provider.Settings.Global.PACKAGE_VERIFIER_ENABLE, "1",
511 userId);
512 android.provider.Settings.Global.putStringForUser(
513 context.getContentResolver(),
514 android.provider.Settings.Global.PACKAGE_VERIFIER_INCLUDE_ADB, "1",
515 userId);
516 }
517 break;
518 case UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES:
Suprabh Shuklae3745ee2017-02-02 20:01:11 -0800519 // Since Android O, the secure setting is not available to be changed by the
520 // user. Hence, when the restriction is cleared, we need to reset the state of
521 // the setting to its default value which is now 1.
522 android.provider.Settings.Secure.putIntForUser(cr,
523 android.provider.Settings.Secure.INSTALL_NON_MARKET_APPS,
524 newValue ? 0 : 1, userId);
Makoto Onuki4f160732015-10-27 17:15:38 -0700525 break;
Fyodor Kupolov9cbfc9e2015-10-07 15:52:33 -0700526 case UserManager.DISALLOW_RUN_IN_BACKGROUND:
527 if (newValue) {
528 int currentUser = ActivityManager.getCurrentUser();
529 if (currentUser != userId && userId != UserHandle.USER_SYSTEM) {
530 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -0800531 ActivityManager.getService().stopUser(userId, false, null);
Fyodor Kupolov9cbfc9e2015-10-07 15:52:33 -0700532 } catch (RemoteException e) {
533 throw e.rethrowAsRuntimeException();
534 }
535 }
536 }
Lenka Trochtova6474f0e2016-03-24 16:43:10 +0100537 break;
538 case UserManager.DISALLOW_SAFE_BOOT:
539 // Unlike with the other restrictions, we want to propagate the new value to
540 // the system settings even if it is false. The other restrictions modify
541 // settings which could be manually changed by the user from the Settings app
542 // after the policies enforcing these restrictions have been revoked, so we
543 // leave re-setting of those settings to the user.
544 android.provider.Settings.Global.putInt(
545 context.getContentResolver(),
546 android.provider.Settings.Global.SAFE_BOOT_DISALLOWED,
547 newValue ? 1 : 0);
548 break;
yuemingw265c6922018-01-11 18:31:14 +0000549 case UserManager.DISALLOW_AIRPLANE_MODE:
550 if (newValue) {
551 final boolean airplaneMode = Settings.Global.getInt(
552 context.getContentResolver(),
553 Settings.Global.AIRPLANE_MODE_ON, 0) == 1;
554 if (airplaneMode) {
555 android.provider.Settings.Global.putInt(
556 context.getContentResolver(),
557 android.provider.Settings.Global.AIRPLANE_MODE_ON, 0);
558 // Post the intent.
559 Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
560 intent.putExtra("state", 0);
561 context.sendBroadcastAsUser(intent, UserHandle.ALL);
562 }
563 }
564 break;
yuemingwc0109512018-01-21 19:21:27 +0000565 case UserManager.DISALLOW_AMBIENT_DISPLAY:
566 if (newValue) {
567 android.provider.Settings.Secure.putString(
568 context.getContentResolver(),
569 Settings.Secure.DOZE_ENABLED, "0");
570 android.provider.Settings.Secure.putString(
571 context.getContentResolver(),
572 Settings.Secure.DOZE_ALWAYS_ON, "0");
573 android.provider.Settings.Secure.putString(
574 context.getContentResolver(),
575 Settings.Secure.DOZE_PULSE_ON_PICK_UP, "0");
576 android.provider.Settings.Secure.putString(
577 context.getContentResolver(),
578 Settings.Secure.DOZE_PULSE_ON_LONG_PRESS, "0");
579 android.provider.Settings.Secure.putString(
580 context.getContentResolver(),
581 Settings.Secure.DOZE_PULSE_ON_DOUBLE_TAP, "0");
582 }
583 break;
Makoto Onuki4f160732015-10-27 17:15:38 -0700584 }
Makoto Onuki4f160732015-10-27 17:15:38 -0700585 } finally {
586 Binder.restoreCallingIdentity(id);
587 }
588 }
589
Makoto Onukia4f11972015-10-01 13:19:58 -0700590 public static void dumpRestrictions(PrintWriter pw, String prefix, Bundle restrictions) {
591 boolean noneSet = true;
592 if (restrictions != null) {
593 for (String key : restrictions.keySet()) {
594 if (restrictions.getBoolean(key, false)) {
595 pw.println(prefix + key);
596 noneSet = false;
597 }
598 }
Makoto Onuki068c54a2015-10-13 14:34:03 -0700599 if (noneSet) {
600 pw.println(prefix + "none");
601 }
602 } else {
603 pw.println(prefix + "null");
Makoto Onukia4f11972015-10-01 13:19:58 -0700604 }
605 }
Pavel Grafov6a40f092016-10-25 15:46:51 +0100606
607 /**
608 * Moves a particular restriction from one array of bundles to another, e.g. for all users.
609 */
610 public static void moveRestriction(String restrictionKey, SparseArray<Bundle> srcRestrictions,
611 SparseArray<Bundle> destRestrictions) {
612 for (int i = 0; i < srcRestrictions.size(); i++) {
Pavel Grafov7f4ad752017-03-28 13:44:04 +0100613 final int key = srcRestrictions.keyAt(i);
614 final Bundle from = srcRestrictions.valueAt(i);
Pavel Grafov6a40f092016-10-25 15:46:51 +0100615 if (contains(from, restrictionKey)) {
616 from.remove(restrictionKey);
617 Bundle to = destRestrictions.get(key);
618 if (to == null) {
619 to = new Bundle();
620 destRestrictions.append(key, to);
621 }
622 to.putBoolean(restrictionKey, true);
623 // Don't keep empty bundles.
624 if (from.isEmpty()) {
625 srcRestrictions.removeAt(i);
626 i--;
627 }
628 }
629 }
630 }
Pavel Grafov7f4ad752017-03-28 13:44:04 +0100631
632 /**
633 * Returns whether restrictions differ between two bundles.
634 * @param oldRestrictions old bundle of restrictions.
635 * @param newRestrictions new bundle of restrictions
636 * @param restrictions restrictions of interest, if empty, all restrictions are checked.
637 */
638 public static boolean restrictionsChanged(Bundle oldRestrictions, Bundle newRestrictions,
639 String... restrictions) {
640 if (restrictions.length == 0) {
641 return areEqual(oldRestrictions, newRestrictions);
642 }
643 for (final String restriction : restrictions) {
644 if (oldRestrictions.getBoolean(restriction, false) !=
645 newRestrictions.getBoolean(restriction, false)) {
646 return true;
647 }
648 }
649 return false;
650 }
Makoto Onukia4f11972015-10-01 13:19:58 -0700651}