blob: 41570c48c47456addbfab2d383cbc531a9f6926b [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;
Makoto Onukiacc50462018-02-14 14:13:49 -080036import android.provider.Settings.Global;
Mahaver Chopradea471e2015-12-17 11:02:37 +000037import android.telephony.SubscriptionInfo;
38import android.telephony.SubscriptionManager;
Makoto Onuki1a2cd742015-11-16 13:51:27 -080039import android.util.Log;
Makoto Onuki1f1ceef2016-01-28 11:32:32 -080040import android.util.Slog;
Pavel Grafov6a40f092016-10-25 15:46:51 +010041import android.util.SparseArray;
Makoto Onukia4f11972015-10-01 13:19:58 -070042
43import org.xmlpull.v1.XmlPullParser;
44import org.xmlpull.v1.XmlSerializer;
45
46import java.io.IOException;
47import java.io.PrintWriter;
Mahaver Chopradea471e2015-12-17 11:02:37 +000048import java.util.List;
Makoto Onukia4f11972015-10-01 13:19:58 -070049import java.util.Set;
50
Makoto Onukid45a4a22015-11-02 17:17:38 -080051/**
Mahaver Chopradea471e2015-12-17 11:02:37 +000052 * Utility methods for user restrictions.
Makoto Onukid45a4a22015-11-02 17:17:38 -080053 *
54 * <p>See {@link UserManagerService} for the method suffixes.
55 */
Makoto Onukia4f11972015-10-01 13:19:58 -070056public class UserRestrictionsUtils {
Makoto Onuki4f160732015-10-27 17:15:38 -070057 private static final String TAG = "UserRestrictionsUtils";
58
Makoto Onukia4f11972015-10-01 13:19:58 -070059 private UserRestrictionsUtils() {
60 }
61
Makoto Onuki1f1ceef2016-01-28 11:32:32 -080062 private static Set<String> newSetWithUniqueCheck(String[] strings) {
63 final Set<String> ret = Sets.newArraySet(strings);
64
65 // Make sure there's no overlap.
66 Preconditions.checkState(ret.size() == strings.length);
67 return ret;
68 }
69
70 public static final Set<String> USER_RESTRICTIONS = newSetWithUniqueCheck(new String[] {
Makoto Onukia4f11972015-10-01 13:19:58 -070071 UserManager.DISALLOW_CONFIG_WIFI,
Christine Franks1bade5d2017-10-10 15:41:50 -070072 UserManager.DISALLOW_CONFIG_LOCALE,
Makoto Onukia4f11972015-10-01 13:19:58 -070073 UserManager.DISALLOW_MODIFY_ACCOUNTS,
74 UserManager.DISALLOW_INSTALL_APPS,
75 UserManager.DISALLOW_UNINSTALL_APPS,
76 UserManager.DISALLOW_SHARE_LOCATION,
77 UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES,
78 UserManager.DISALLOW_CONFIG_BLUETOOTH,
Lenka Trochtova63d5e4a72016-12-02 12:19:39 +010079 UserManager.DISALLOW_BLUETOOTH,
Pavel Grafov7f4ad752017-03-28 13:44:04 +010080 UserManager.DISALLOW_BLUETOOTH_SHARING,
Makoto Onukia4f11972015-10-01 13:19:58 -070081 UserManager.DISALLOW_USB_FILE_TRANSFER,
82 UserManager.DISALLOW_CONFIG_CREDENTIALS,
83 UserManager.DISALLOW_REMOVE_USER,
Esteban Talavera6c9116a2016-11-24 16:12:44 +000084 UserManager.DISALLOW_REMOVE_MANAGED_PROFILE,
Makoto Onukia4f11972015-10-01 13:19:58 -070085 UserManager.DISALLOW_DEBUGGING_FEATURES,
86 UserManager.DISALLOW_CONFIG_VPN,
yuemingwa9772f362017-10-23 18:34:35 +010087 UserManager.DISALLOW_CONFIG_DATE_TIME,
Makoto Onukia4f11972015-10-01 13:19:58 -070088 UserManager.DISALLOW_CONFIG_TETHERING,
89 UserManager.DISALLOW_NETWORK_RESET,
90 UserManager.DISALLOW_FACTORY_RESET,
91 UserManager.DISALLOW_ADD_USER,
Esteban Talavera6c9116a2016-11-24 16:12:44 +000092 UserManager.DISALLOW_ADD_MANAGED_PROFILE,
Makoto Onukia4f11972015-10-01 13:19:58 -070093 UserManager.ENSURE_VERIFY_APPS,
94 UserManager.DISALLOW_CONFIG_CELL_BROADCASTS,
95 UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS,
96 UserManager.DISALLOW_APPS_CONTROL,
97 UserManager.DISALLOW_MOUNT_PHYSICAL_MEDIA,
98 UserManager.DISALLOW_UNMUTE_MICROPHONE,
99 UserManager.DISALLOW_ADJUST_VOLUME,
100 UserManager.DISALLOW_OUTGOING_CALLS,
101 UserManager.DISALLOW_SMS,
102 UserManager.DISALLOW_FUN,
103 UserManager.DISALLOW_CREATE_WINDOWS,
Charles He22ff6f9d2017-10-05 21:28:55 +0100104 UserManager.DISALLOW_SYSTEM_ERROR_DIALOGS,
Makoto Onukia4f11972015-10-01 13:19:58 -0700105 UserManager.DISALLOW_CROSS_PROFILE_COPY_PASTE,
106 UserManager.DISALLOW_OUTGOING_BEAM,
107 UserManager.DISALLOW_WALLPAPER,
108 UserManager.DISALLOW_SAFE_BOOT,
109 UserManager.ALLOW_PARENT_PROFILE_APP_LINKING,
110 UserManager.DISALLOW_RECORD_AUDIO,
Fyodor Kupolov9cbfc9e2015-10-07 15:52:33 -0700111 UserManager.DISALLOW_CAMERA,
Mahaver Chopradea471e2015-12-17 11:02:37 +0000112 UserManager.DISALLOW_RUN_IN_BACKGROUND,
Oleksandr Peletskyi7f1f1df2016-01-18 15:40:21 +0100113 UserManager.DISALLOW_DATA_ROAMING,
Oleksandr Peletskyif2519812016-01-26 20:16:06 +0100114 UserManager.DISALLOW_SET_USER_ICON,
Mahaver Chopra3d9805d2016-07-07 16:25:05 +0100115 UserManager.DISALLOW_SET_WALLPAPER,
Tony Makc1205112016-07-22 16:02:59 +0100116 UserManager.DISALLOW_OEM_UNLOCK,
Esteban Talavera492b4722017-02-13 14:59:45 +0000117 UserManager.DISALLOW_UNMUTE_DEVICE,
Felipe Leme24d58932017-03-21 14:13:58 -0700118 UserManager.DISALLOW_AUTOFILL,
Pavel Grafovc4f87e92017-10-26 16:34:25 +0100119 UserManager.DISALLOW_USER_SWITCH,
120 UserManager.DISALLOW_UNIFIED_PASSWORD,
yuemingw7810b8b2018-02-01 17:32:25 +0000121 UserManager.DISALLOW_CONFIG_LOCATION,
yuemingwc6ac29d2018-01-10 16:54:08 +0000122 UserManager.DISALLOW_AIRPLANE_MODE,
Rubin Xucc391c22018-01-02 20:37:35 +0000123 UserManager.DISALLOW_CONFIG_BRIGHTNESS,
124 UserManager.DISALLOW_SHARE_INTO_MANAGED_PROFILE,
yuemingw5cda3ae2018-01-15 10:26:19 +0000125 UserManager.DISALLOW_AMBIENT_DISPLAY,
Vladislav Kuzkokov622b9f92018-01-25 16:33:05 +0100126 UserManager.DISALLOW_CONFIG_SCREEN_TIMEOUT,
127 UserManager.DISALLOW_PRINTING
Makoto Onuki1f1ceef2016-01-28 11:32:32 -0800128 });
Makoto Onukia4f11972015-10-01 13:19:58 -0700129
130 /**
131 * Set of user restriction which we don't want to persist.
132 */
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800133 private static final Set<String> NON_PERSIST_USER_RESTRICTIONS = Sets.newArraySet(
134 UserManager.DISALLOW_RECORD_AUDIO
135 );
Makoto Onukia4f11972015-10-01 13:19:58 -0700136
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800137 /**
Pavel Grafov6a40f092016-10-25 15:46:51 +0100138 * User restrictions that cannot be set by profile owners of secondary users. When set by DO
139 * they will be applied to all users.
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800140 */
Pavel Grafov6a40f092016-10-25 15:46:51 +0100141 private static final Set<String> PRIMARY_USER_ONLY_RESTRICTIONS = Sets.newArraySet(
Lenka Trochtova63d5e4a72016-12-02 12:19:39 +0100142 UserManager.DISALLOW_BLUETOOTH,
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800143 UserManager.DISALLOW_USB_FILE_TRANSFER,
144 UserManager.DISALLOW_CONFIG_TETHERING,
145 UserManager.DISALLOW_NETWORK_RESET,
146 UserManager.DISALLOW_FACTORY_RESET,
147 UserManager.DISALLOW_ADD_USER,
148 UserManager.DISALLOW_CONFIG_CELL_BROADCASTS,
149 UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS,
150 UserManager.DISALLOW_MOUNT_PHYSICAL_MEDIA,
151 UserManager.DISALLOW_SMS,
152 UserManager.DISALLOW_FUN,
153 UserManager.DISALLOW_SAFE_BOOT,
Mahaver Chopradea471e2015-12-17 11:02:37 +0000154 UserManager.DISALLOW_CREATE_WINDOWS,
yuemingw5fe75dc2017-11-29 15:52:56 +0000155 UserManager.DISALLOW_DATA_ROAMING,
156 UserManager.DISALLOW_AIRPLANE_MODE
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800157 );
158
159 /**
Benjamin Franzff66fa92017-08-10 10:39:44 +0100160 * User restrictions that cannot be set by profile owners. Applied to all users.
161 */
162 private static final Set<String> DEVICE_OWNER_ONLY_RESTRICTIONS = Sets.newArraySet(
163 UserManager.DISALLOW_USER_SWITCH
164 );
165
166 /**
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800167 * User restrictions that can't be changed by device owner or profile owner.
168 */
169 private static final Set<String> IMMUTABLE_BY_OWNERS = Sets.newArraySet(
170 UserManager.DISALLOW_RECORD_AUDIO,
Mahaver Chopra3d9805d2016-07-07 16:25:05 +0100171 UserManager.DISALLOW_WALLPAPER,
172 UserManager.DISALLOW_OEM_UNLOCK
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800173 );
174
175 /**
176 * Special user restrictions that can be applied to a user as well as to all users globally,
177 * depending on callers. When device owner sets them, they'll be applied to all users.
178 */
179 private static final Set<String> GLOBAL_RESTRICTIONS = Sets.newArraySet(
180 UserManager.DISALLOW_ADJUST_VOLUME,
Pavel Grafov7f4ad752017-03-28 13:44:04 +0100181 UserManager.DISALLOW_BLUETOOTH_SHARING,
yuemingwa9772f362017-10-23 18:34:35 +0100182 UserManager.DISALLOW_CONFIG_DATE_TIME,
Charles He22ff6f9d2017-10-05 21:28:55 +0100183 UserManager.DISALLOW_SYSTEM_ERROR_DIALOGS,
Fyodor Kupolov9cbfc9e2015-10-07 15:52:33 -0700184 UserManager.DISALLOW_RUN_IN_BACKGROUND,
Tony Makc1205112016-07-22 16:02:59 +0100185 UserManager.DISALLOW_UNMUTE_MICROPHONE,
Esteban Talavera492b4722017-02-13 14:59:45 +0000186 UserManager.DISALLOW_UNMUTE_DEVICE
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800187 );
188
Makoto Onuki1f1ceef2016-01-28 11:32:32 -0800189 /**
Esteban Talavera6c9116a2016-11-24 16:12:44 +0000190 * User restrictions that default to {@code true} for device owners.
191 */
192 private static final Set<String> DEFAULT_ENABLED_FOR_DEVICE_OWNERS = Sets.newArraySet(
193 UserManager.DISALLOW_ADD_MANAGED_PROFILE
194 );
195
Pavel Grafov7f4ad752017-03-28 13:44:04 +0100196 /**
197 * User restrictions that default to {@code true} for managed profile owners.
198 *
199 * NB: {@link UserManager#DISALLOW_INSTALL_UNKNOWN_SOURCES} is also set by default but it is
200 * not set to existing profile owners unless they used to have INSTALL_NON_MARKET_APPS disabled
201 * in settings. So it is handled separately.
202 */
203 private static final Set<String> DEFAULT_ENABLED_FOR_MANAGED_PROFILES = Sets.newArraySet(
204 UserManager.DISALLOW_BLUETOOTH_SHARING
205 );
206
Pavel Grafovc4f87e92017-10-26 16:34:25 +0100207 /**
Pavel Grafov6a40f092016-10-25 15:46:51 +0100208 * Special user restrictions that are always applied to all users no matter who sets them.
209 */
210 private static final Set<String> PROFILE_GLOBAL_RESTRICTIONS = Sets.newArraySet(
yuemingw5fe75dc2017-11-29 15:52:56 +0000211 UserManager.ENSURE_VERIFY_APPS,
212 UserManager.DISALLOW_AIRPLANE_MODE
Pavel Grafov6a40f092016-10-25 15:46:51 +0100213 );
214
Esteban Talavera6c9116a2016-11-24 16:12:44 +0000215 /**
Makoto Onuki1f1ceef2016-01-28 11:32:32 -0800216 * Throws {@link IllegalArgumentException} if the given restriction name is invalid.
217 */
218 public static boolean isValidRestriction(@NonNull String restriction) {
219 if (!USER_RESTRICTIONS.contains(restriction)) {
Makoto Onukiad5619d2016-02-10 13:41:15 -0800220 Slog.e(TAG, "Unknown restriction: " + restriction);
Makoto Onuki1f1ceef2016-01-28 11:32:32 -0800221 return false;
222 }
223 return true;
224 }
225
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800226 public static void writeRestrictions(@NonNull XmlSerializer serializer,
227 @Nullable Bundle restrictions, @NonNull String tag) throws IOException {
228 if (restrictions == null) {
229 return;
230 }
231
Makoto Onukia4f11972015-10-01 13:19:58 -0700232 serializer.startTag(null, tag);
Makoto Onukiac65e1e2015-11-20 15:33:17 -0800233 for (String key : restrictions.keySet()) {
234 if (NON_PERSIST_USER_RESTRICTIONS.contains(key)) {
235 continue; // Don't persist.
Makoto Onukia4f11972015-10-01 13:19:58 -0700236 }
Makoto Onukiac65e1e2015-11-20 15:33:17 -0800237 if (USER_RESTRICTIONS.contains(key)) {
238 if (restrictions.getBoolean(key)) {
239 serializer.attribute(null, key, "true");
240 }
241 continue;
242 }
243 Log.w(TAG, "Unknown user restriction detected: " + key);
Makoto Onukia4f11972015-10-01 13:19:58 -0700244 }
245 serializer.endTag(null, tag);
246 }
247
Esteban Talaverac48b20f2016-08-11 11:23:40 +0100248 public static void readRestrictions(XmlPullParser parser, Bundle restrictions) {
Fyodor Kupoloveafee022017-03-15 17:09:04 -0700249 restrictions.clear();
Makoto Onukia4f11972015-10-01 13:19:58 -0700250 for (String key : USER_RESTRICTIONS) {
251 final String value = parser.getAttributeValue(null, key);
252 if (value != null) {
253 restrictions.putBoolean(key, Boolean.parseBoolean(value));
254 }
255 }
256 }
257
Pavel Grafov6a40f092016-10-25 15:46:51 +0100258 public static Bundle readRestrictions(XmlPullParser parser) {
259 final Bundle result = new Bundle();
260 readRestrictions(parser, result);
261 return result;
262 }
263
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800264 /**
265 * @return {@code in} itself when it's not null, or an empty bundle (which can writable).
266 */
267 public static Bundle nonNull(@Nullable Bundle in) {
268 return in != null ? in : new Bundle();
269 }
270
271 public static boolean isEmpty(@Nullable Bundle in) {
272 return (in == null) || (in.size() == 0);
273 }
274
275 /**
Pavel Grafov6a40f092016-10-25 15:46:51 +0100276 * Returns {@code true} if given bundle is not null and contains {@code true} for a given
277 * restriction.
278 */
279 public static boolean contains(@Nullable Bundle in, String restriction) {
280 return in != null && in.getBoolean(restriction);
281 }
282
283 /**
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800284 * Creates a copy of the {@code in} Bundle. If {@code in} is null, it'll return an empty
285 * bundle.
286 *
287 * <p>The resulting {@link Bundle} is always writable. (i.e. it won't return
288 * {@link Bundle#EMPTY})
289 */
290 public static @NonNull Bundle clone(@Nullable Bundle in) {
291 return (in != null) ? new Bundle(in) : new Bundle();
292 }
293
294 public static void merge(@NonNull Bundle dest, @Nullable Bundle in) {
295 Preconditions.checkNotNull(dest);
296 Preconditions.checkArgument(dest != in);
Makoto Onuki068c54a2015-10-13 14:34:03 -0700297 if (in == null) {
298 return;
299 }
300 for (String key : in.keySet()) {
301 if (in.getBoolean(key, false)) {
302 dest.putBoolean(key, true);
303 }
304 }
305 }
306
Makoto Onuki4f160732015-10-27 17:15:38 -0700307 /**
Pavel Grafov6a40f092016-10-25 15:46:51 +0100308 * Merges a sparse array of restrictions bundles into one.
309 */
310 @Nullable
311 public static Bundle mergeAll(SparseArray<Bundle> restrictions) {
312 if (restrictions.size() == 0) {
313 return null;
314 } else {
315 final Bundle result = new Bundle();
316 for (int i = 0; i < restrictions.size(); i++) {
317 merge(result, restrictions.valueAt(i));
318 }
319 return result;
320 }
321 }
322
323 /**
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800324 * @return true if a restriction is settable by device owner.
325 */
326 public static boolean canDeviceOwnerChange(String restriction) {
327 return !IMMUTABLE_BY_OWNERS.contains(restriction);
328 }
329
330 /**
Makoto Onuki5485ed42015-12-09 11:38:23 -0800331 * @return true if a restriction is settable by profile owner. Note it takes a user ID because
332 * some restrictions can be changed by PO only when it's running on the system user.
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800333 */
Makoto Onuki5485ed42015-12-09 11:38:23 -0800334 public static boolean canProfileOwnerChange(String restriction, int userId) {
335 return !IMMUTABLE_BY_OWNERS.contains(restriction)
Benjamin Franzff66fa92017-08-10 10:39:44 +0100336 && !DEVICE_OWNER_ONLY_RESTRICTIONS.contains(restriction)
Makoto Onuki5485ed42015-12-09 11:38:23 -0800337 && !(userId != UserHandle.USER_SYSTEM
Pavel Grafov6a40f092016-10-25 15:46:51 +0100338 && PRIMARY_USER_ONLY_RESTRICTIONS.contains(restriction));
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800339 }
340
341 /**
Esteban Talavera6c9116a2016-11-24 16:12:44 +0000342 * Returns the user restrictions that default to {@code true} for device owners.
Nicolas Prevot2ea46fe2017-01-05 10:29:34 +0000343 * These user restrictions are local, though. ie only for the device owner's user id.
Esteban Talavera6c9116a2016-11-24 16:12:44 +0000344 */
345 public static @NonNull Set<String> getDefaultEnabledForDeviceOwner() {
346 return DEFAULT_ENABLED_FOR_DEVICE_OWNERS;
347 }
348
349 /**
Pavel Grafov7f4ad752017-03-28 13:44:04 +0100350 * Returns the user restrictions that default to {@code true} for managed profile owners.
351 */
352 public static @NonNull Set<String> getDefaultEnabledForManagedProfiles() {
353 return DEFAULT_ENABLED_FOR_MANAGED_PROFILES;
354 }
355
356 /**
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800357 * Takes restrictions that can be set by device owner, and sort them into what should be applied
358 * globally and what should be applied only on the current user.
359 */
Pavel Grafov6a40f092016-10-25 15:46:51 +0100360 public static void sortToGlobalAndLocal(@Nullable Bundle in, boolean isDeviceOwner,
361 int cameraRestrictionScope,
362 @NonNull Bundle global, @NonNull Bundle local) {
363 // Camera restriction (as well as all others) goes to at most one bundle.
364 if (cameraRestrictionScope == UserManagerInternal.CAMERA_DISABLED_GLOBALLY) {
365 global.putBoolean(UserManager.DISALLOW_CAMERA, true);
366 } else if (cameraRestrictionScope == UserManagerInternal.CAMERA_DISABLED_LOCALLY) {
367 local.putBoolean(UserManager.DISALLOW_CAMERA, true);
368 }
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800369 if (in == null || in.size() == 0) {
370 return;
371 }
372 for (String key : in.keySet()) {
373 if (!in.getBoolean(key)) {
374 continue;
375 }
Pavel Grafov6a40f092016-10-25 15:46:51 +0100376 if (isGlobal(isDeviceOwner, key)) {
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800377 global.putBoolean(key, true);
378 } else {
379 local.putBoolean(key, true);
380 }
381 }
382 }
383
384 /**
Pavel Grafov6a40f092016-10-25 15:46:51 +0100385 * Whether given user restriction should be enforced globally.
386 */
387 private static boolean isGlobal(boolean isDeviceOwner, String key) {
388 return (isDeviceOwner &&
Benjamin Franzff66fa92017-08-10 10:39:44 +0100389 (PRIMARY_USER_ONLY_RESTRICTIONS.contains(key) || GLOBAL_RESTRICTIONS.contains(key)))
390 || PROFILE_GLOBAL_RESTRICTIONS.contains(key)
391 || DEVICE_OWNER_ONLY_RESTRICTIONS.contains(key);
Pavel Grafov6a40f092016-10-25 15:46:51 +0100392 }
393
394 /**
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800395 * @return true if two Bundles contain the same user restriction.
396 * A null bundle and an empty bundle are considered to be equal.
397 */
398 public static boolean areEqual(@Nullable Bundle a, @Nullable Bundle b) {
399 if (a == b) {
400 return true;
401 }
402 if (isEmpty(a)) {
403 return isEmpty(b);
404 }
405 if (isEmpty(b)) {
406 return false;
407 }
408 for (String key : a.keySet()) {
409 if (a.getBoolean(key) != b.getBoolean(key)) {
410 return false;
411 }
412 }
413 for (String key : b.keySet()) {
414 if (a.getBoolean(key) != b.getBoolean(key)) {
415 return false;
416 }
417 }
418 return true;
419 }
420
421 /**
Makoto Onuki4f160732015-10-27 17:15:38 -0700422 * Takes a new use restriction set and the previous set, and apply the restrictions that have
423 * changed.
Makoto Onukid45a4a22015-11-02 17:17:38 -0800424 *
Fyodor Kupolov9cbfc9e2015-10-07 15:52:33 -0700425 * <p>Note this method is called by {@link UserManagerService} without holding any locks.
Makoto Onuki4f160732015-10-27 17:15:38 -0700426 */
Fyodor Kupolov9cbfc9e2015-10-07 15:52:33 -0700427 public static void applyUserRestrictions(Context context, int userId,
Makoto Onukid45a4a22015-11-02 17:17:38 -0800428 Bundle newRestrictions, Bundle prevRestrictions) {
Makoto Onuki4f160732015-10-27 17:15:38 -0700429 for (String key : USER_RESTRICTIONS) {
430 final boolean newValue = newRestrictions.getBoolean(key);
431 final boolean prevValue = prevRestrictions.getBoolean(key);
432
433 if (newValue != prevValue) {
Fyodor Kupolov9cbfc9e2015-10-07 15:52:33 -0700434 applyUserRestriction(context, userId, key, newValue);
Makoto Onuki4f160732015-10-27 17:15:38 -0700435 }
436 }
437 }
Fyodor Kupolov9cbfc9e2015-10-07 15:52:33 -0700438
Makoto Onukid45a4a22015-11-02 17:17:38 -0800439 /**
440 * Apply each user restriction.
441 *
Makoto Onuki28da2e32015-11-20 11:30:44 -0800442 * <p>See also {@link
443 * com.android.providers.settings.SettingsProvider#isGlobalOrSecureSettingRestrictedForUser},
444 * which should be in sync with this method.
Makoto Onukid45a4a22015-11-02 17:17:38 -0800445 */
Fyodor Kupolov9cbfc9e2015-10-07 15:52:33 -0700446 private static void applyUserRestriction(Context context, int userId, String key,
Makoto Onuki4f160732015-10-27 17:15:38 -0700447 boolean newValue) {
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800448 if (UserManagerService.DBG) {
449 Log.d(TAG, "Applying user restriction: userId=" + userId
450 + " key=" + key + " value=" + newValue);
451 }
Makoto Onuki4f160732015-10-27 17:15:38 -0700452 // When certain restrictions are cleared, we don't update the system settings,
453 // because these settings are changeable on the Settings UI and we don't know the original
454 // value -- for example LOCATION_MODE might have been off already when the restriction was
455 // set, and in that case even if the restriction is lifted, changing it to ON would be
456 // wrong. So just don't do anything in such a case. If the user hopes to enable location
457 // later, they can do it on the Settings UI.
Benjamin Franz0ff13fc2016-07-12 13:42:21 +0100458 // WARNING: Remember that Settings.Global and Settings.Secure are changeable via adb.
459 // To prevent this from happening for a given user restriction, you have to add a check to
460 // SettingsProvider.isGlobalOrSecureSettingRestrictedForUser.
Makoto Onuki4f160732015-10-27 17:15:38 -0700461
462 final ContentResolver cr = context.getContentResolver();
463 final long id = Binder.clearCallingIdentity();
464 try {
465 switch (key) {
Mahaver Chopradea471e2015-12-17 11:02:37 +0000466 case UserManager.DISALLOW_DATA_ROAMING:
467 if (newValue) {
468 // DISALLOW_DATA_ROAMING user restriction is set.
469
470 // Multi sim device.
Jeff Sharkey717f52f2018-01-04 16:04:11 -0700471 SubscriptionManager subscriptionManager = context
472 .getSystemService(SubscriptionManager.class);
Mahaver Chopradea471e2015-12-17 11:02:37 +0000473 final List<SubscriptionInfo> subscriptionInfoList =
474 subscriptionManager.getActiveSubscriptionInfoList();
475 if (subscriptionInfoList != null) {
476 for (SubscriptionInfo subInfo : subscriptionInfoList) {
477 android.provider.Settings.Global.putStringForUser(cr,
478 android.provider.Settings.Global.DATA_ROAMING
479 + subInfo.getSubscriptionId(), "0", userId);
480 }
481 }
482
483 // Single sim device.
484 android.provider.Settings.Global.putStringForUser(cr,
485 android.provider.Settings.Global.DATA_ROAMING, "0", userId);
486 }
487 break;
Makoto Onuki4f160732015-10-27 17:15:38 -0700488 case UserManager.DISALLOW_SHARE_LOCATION:
489 if (newValue) {
490 android.provider.Settings.Secure.putIntForUser(cr,
491 android.provider.Settings.Secure.LOCATION_MODE,
492 android.provider.Settings.Secure.LOCATION_MODE_OFF,
493 userId);
Makoto Onuki4f160732015-10-27 17:15:38 -0700494 }
Makoto Onuki4f160732015-10-27 17:15:38 -0700495 break;
496 case UserManager.DISALLOW_DEBUGGING_FEATURES:
497 if (newValue) {
498 // Only disable adb if changing for system user, since it is global
499 // TODO: should this be admin user?
500 if (userId == UserHandle.USER_SYSTEM) {
501 android.provider.Settings.Global.putStringForUser(cr,
502 android.provider.Settings.Global.ADB_ENABLED, "0",
503 userId);
504 }
505 }
506 break;
507 case UserManager.ENSURE_VERIFY_APPS:
508 if (newValue) {
509 android.provider.Settings.Global.putStringForUser(
510 context.getContentResolver(),
511 android.provider.Settings.Global.PACKAGE_VERIFIER_ENABLE, "1",
512 userId);
513 android.provider.Settings.Global.putStringForUser(
514 context.getContentResolver(),
515 android.provider.Settings.Global.PACKAGE_VERIFIER_INCLUDE_ADB, "1",
516 userId);
517 }
518 break;
519 case UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES:
Suprabh Shuklae3745ee2017-02-02 20:01:11 -0800520 // Since Android O, the secure setting is not available to be changed by the
521 // user. Hence, when the restriction is cleared, we need to reset the state of
522 // the setting to its default value which is now 1.
523 android.provider.Settings.Secure.putIntForUser(cr,
524 android.provider.Settings.Secure.INSTALL_NON_MARKET_APPS,
525 newValue ? 0 : 1, userId);
Makoto Onuki4f160732015-10-27 17:15:38 -0700526 break;
Fyodor Kupolov9cbfc9e2015-10-07 15:52:33 -0700527 case UserManager.DISALLOW_RUN_IN_BACKGROUND:
528 if (newValue) {
529 int currentUser = ActivityManager.getCurrentUser();
530 if (currentUser != userId && userId != UserHandle.USER_SYSTEM) {
531 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -0800532 ActivityManager.getService().stopUser(userId, false, null);
Fyodor Kupolov9cbfc9e2015-10-07 15:52:33 -0700533 } catch (RemoteException e) {
534 throw e.rethrowAsRuntimeException();
535 }
536 }
537 }
Lenka Trochtova6474f0e2016-03-24 16:43:10 +0100538 break;
539 case UserManager.DISALLOW_SAFE_BOOT:
540 // Unlike with the other restrictions, we want to propagate the new value to
541 // the system settings even if it is false. The other restrictions modify
542 // settings which could be manually changed by the user from the Settings app
543 // after the policies enforcing these restrictions have been revoked, so we
544 // leave re-setting of those settings to the user.
545 android.provider.Settings.Global.putInt(
546 context.getContentResolver(),
547 android.provider.Settings.Global.SAFE_BOOT_DISALLOWED,
548 newValue ? 1 : 0);
549 break;
yuemingw265c6922018-01-11 18:31:14 +0000550 case UserManager.DISALLOW_AIRPLANE_MODE:
551 if (newValue) {
552 final boolean airplaneMode = Settings.Global.getInt(
553 context.getContentResolver(),
554 Settings.Global.AIRPLANE_MODE_ON, 0) == 1;
555 if (airplaneMode) {
556 android.provider.Settings.Global.putInt(
557 context.getContentResolver(),
558 android.provider.Settings.Global.AIRPLANE_MODE_ON, 0);
559 // Post the intent.
560 Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
561 intent.putExtra("state", 0);
562 context.sendBroadcastAsUser(intent, UserHandle.ALL);
563 }
564 }
565 break;
yuemingwc0109512018-01-21 19:21:27 +0000566 case UserManager.DISALLOW_AMBIENT_DISPLAY:
567 if (newValue) {
568 android.provider.Settings.Secure.putString(
569 context.getContentResolver(),
570 Settings.Secure.DOZE_ENABLED, "0");
571 android.provider.Settings.Secure.putString(
572 context.getContentResolver(),
573 Settings.Secure.DOZE_ALWAYS_ON, "0");
574 android.provider.Settings.Secure.putString(
575 context.getContentResolver(),
576 Settings.Secure.DOZE_PULSE_ON_PICK_UP, "0");
577 android.provider.Settings.Secure.putString(
578 context.getContentResolver(),
579 Settings.Secure.DOZE_PULSE_ON_LONG_PRESS, "0");
580 android.provider.Settings.Secure.putString(
581 context.getContentResolver(),
582 Settings.Secure.DOZE_PULSE_ON_DOUBLE_TAP, "0");
583 }
584 break;
Makoto Onukiacc50462018-02-14 14:13:49 -0800585 case UserManager.DISALLOW_CONFIG_LOCATION:
586 // When DISALLOW_CONFIG_LOCATION is set on any user, we undo the global
587 // kill switch.
588 if (newValue) {
589 android.provider.Settings.Global.putString(
590 context.getContentResolver(),
591 Global.LOCATION_GLOBAL_KILL_SWITCH, "0");
592 }
593 break;
Makoto Onuki4f160732015-10-27 17:15:38 -0700594 }
Makoto Onuki4f160732015-10-27 17:15:38 -0700595 } finally {
596 Binder.restoreCallingIdentity(id);
597 }
598 }
599
Makoto Onukia4f11972015-10-01 13:19:58 -0700600 public static void dumpRestrictions(PrintWriter pw, String prefix, Bundle restrictions) {
601 boolean noneSet = true;
602 if (restrictions != null) {
603 for (String key : restrictions.keySet()) {
604 if (restrictions.getBoolean(key, false)) {
605 pw.println(prefix + key);
606 noneSet = false;
607 }
608 }
Makoto Onuki068c54a2015-10-13 14:34:03 -0700609 if (noneSet) {
610 pw.println(prefix + "none");
611 }
612 } else {
613 pw.println(prefix + "null");
Makoto Onukia4f11972015-10-01 13:19:58 -0700614 }
615 }
Pavel Grafov6a40f092016-10-25 15:46:51 +0100616
617 /**
618 * Moves a particular restriction from one array of bundles to another, e.g. for all users.
619 */
620 public static void moveRestriction(String restrictionKey, SparseArray<Bundle> srcRestrictions,
621 SparseArray<Bundle> destRestrictions) {
622 for (int i = 0; i < srcRestrictions.size(); i++) {
Pavel Grafov7f4ad752017-03-28 13:44:04 +0100623 final int key = srcRestrictions.keyAt(i);
624 final Bundle from = srcRestrictions.valueAt(i);
Pavel Grafov6a40f092016-10-25 15:46:51 +0100625 if (contains(from, restrictionKey)) {
626 from.remove(restrictionKey);
627 Bundle to = destRestrictions.get(key);
628 if (to == null) {
629 to = new Bundle();
630 destRestrictions.append(key, to);
631 }
632 to.putBoolean(restrictionKey, true);
633 // Don't keep empty bundles.
634 if (from.isEmpty()) {
635 srcRestrictions.removeAt(i);
636 i--;
637 }
638 }
639 }
640 }
Pavel Grafov7f4ad752017-03-28 13:44:04 +0100641
642 /**
643 * Returns whether restrictions differ between two bundles.
644 * @param oldRestrictions old bundle of restrictions.
645 * @param newRestrictions new bundle of restrictions
646 * @param restrictions restrictions of interest, if empty, all restrictions are checked.
647 */
648 public static boolean restrictionsChanged(Bundle oldRestrictions, Bundle newRestrictions,
649 String... restrictions) {
650 if (restrictions.length == 0) {
651 return areEqual(oldRestrictions, newRestrictions);
652 }
653 for (final String restriction : restrictions) {
654 if (oldRestrictions.getBoolean(restriction, false) !=
655 newRestrictions.getBoolean(restriction, false)) {
656 return true;
657 }
658 }
659 return false;
660 }
Makoto Onukia4f11972015-10-01 13:19:58 -0700661}