blob: 011e1acbf92092b0db57d84f549d65436332b6bf [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;
Makoto Onuki4f160732015-10-27 17:15:38 -070028import android.os.Binder;
Makoto Onukia4f11972015-10-01 13:19:58 -070029import android.os.Bundle;
Fyodor Kupolov9cbfc9e2015-10-07 15:52:33 -070030import android.os.RemoteException;
Makoto Onuki4f160732015-10-27 17:15:38 -070031import android.os.UserHandle;
Makoto Onukia4f11972015-10-01 13:19:58 -070032import android.os.UserManager;
Pavel Grafov6a40f092016-10-25 15:46:51 +010033import android.os.UserManagerInternal;
Mahaver Chopradea471e2015-12-17 11:02:37 +000034import android.telephony.SubscriptionInfo;
35import android.telephony.SubscriptionManager;
Makoto Onuki1a2cd742015-11-16 13:51:27 -080036import android.util.Log;
Makoto Onuki1f1ceef2016-01-28 11:32:32 -080037import android.util.Slog;
Pavel Grafov6a40f092016-10-25 15:46:51 +010038import android.util.SparseArray;
Makoto Onukia4f11972015-10-01 13:19:58 -070039
40import org.xmlpull.v1.XmlPullParser;
41import org.xmlpull.v1.XmlSerializer;
42
43import java.io.IOException;
44import java.io.PrintWriter;
Mahaver Chopradea471e2015-12-17 11:02:37 +000045import java.util.List;
Makoto Onukia4f11972015-10-01 13:19:58 -070046import java.util.Set;
47
Makoto Onukid45a4a22015-11-02 17:17:38 -080048/**
Mahaver Chopradea471e2015-12-17 11:02:37 +000049 * Utility methods for user restrictions.
Makoto Onukid45a4a22015-11-02 17:17:38 -080050 *
51 * <p>See {@link UserManagerService} for the method suffixes.
52 */
Makoto Onukia4f11972015-10-01 13:19:58 -070053public class UserRestrictionsUtils {
Makoto Onuki4f160732015-10-27 17:15:38 -070054 private static final String TAG = "UserRestrictionsUtils";
55
Makoto Onukia4f11972015-10-01 13:19:58 -070056 private UserRestrictionsUtils() {
57 }
58
Makoto Onuki1f1ceef2016-01-28 11:32:32 -080059 private static Set<String> newSetWithUniqueCheck(String[] strings) {
60 final Set<String> ret = Sets.newArraySet(strings);
61
62 // Make sure there's no overlap.
63 Preconditions.checkState(ret.size() == strings.length);
64 return ret;
65 }
66
67 public static final Set<String> USER_RESTRICTIONS = newSetWithUniqueCheck(new String[] {
Makoto Onukia4f11972015-10-01 13:19:58 -070068 UserManager.DISALLOW_CONFIG_WIFI,
69 UserManager.DISALLOW_MODIFY_ACCOUNTS,
70 UserManager.DISALLOW_INSTALL_APPS,
71 UserManager.DISALLOW_UNINSTALL_APPS,
72 UserManager.DISALLOW_SHARE_LOCATION,
73 UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES,
74 UserManager.DISALLOW_CONFIG_BLUETOOTH,
Lenka Trochtova63d5e4a72016-12-02 12:19:39 +010075 UserManager.DISALLOW_BLUETOOTH,
Pavel Grafov7f4ad752017-03-28 13:44:04 +010076 UserManager.DISALLOW_BLUETOOTH_SHARING,
Makoto Onukia4f11972015-10-01 13:19:58 -070077 UserManager.DISALLOW_USB_FILE_TRANSFER,
78 UserManager.DISALLOW_CONFIG_CREDENTIALS,
79 UserManager.DISALLOW_REMOVE_USER,
Esteban Talavera6c9116a2016-11-24 16:12:44 +000080 UserManager.DISALLOW_REMOVE_MANAGED_PROFILE,
Makoto Onukia4f11972015-10-01 13:19:58 -070081 UserManager.DISALLOW_DEBUGGING_FEATURES,
82 UserManager.DISALLOW_CONFIG_VPN,
yuemingwa9772f362017-10-23 18:34:35 +010083 UserManager.DISALLOW_CONFIG_DATE_TIME,
Makoto Onukia4f11972015-10-01 13:19:58 -070084 UserManager.DISALLOW_CONFIG_TETHERING,
85 UserManager.DISALLOW_NETWORK_RESET,
86 UserManager.DISALLOW_FACTORY_RESET,
87 UserManager.DISALLOW_ADD_USER,
Esteban Talavera6c9116a2016-11-24 16:12:44 +000088 UserManager.DISALLOW_ADD_MANAGED_PROFILE,
Makoto Onukia4f11972015-10-01 13:19:58 -070089 UserManager.ENSURE_VERIFY_APPS,
90 UserManager.DISALLOW_CONFIG_CELL_BROADCASTS,
91 UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS,
92 UserManager.DISALLOW_APPS_CONTROL,
93 UserManager.DISALLOW_MOUNT_PHYSICAL_MEDIA,
94 UserManager.DISALLOW_UNMUTE_MICROPHONE,
95 UserManager.DISALLOW_ADJUST_VOLUME,
96 UserManager.DISALLOW_OUTGOING_CALLS,
97 UserManager.DISALLOW_SMS,
98 UserManager.DISALLOW_FUN,
99 UserManager.DISALLOW_CREATE_WINDOWS,
Charles He22ff6f9d2017-10-05 21:28:55 +0100100 UserManager.DISALLOW_SYSTEM_ERROR_DIALOGS,
Makoto Onukia4f11972015-10-01 13:19:58 -0700101 UserManager.DISALLOW_CROSS_PROFILE_COPY_PASTE,
102 UserManager.DISALLOW_OUTGOING_BEAM,
103 UserManager.DISALLOW_WALLPAPER,
104 UserManager.DISALLOW_SAFE_BOOT,
105 UserManager.ALLOW_PARENT_PROFILE_APP_LINKING,
106 UserManager.DISALLOW_RECORD_AUDIO,
Fyodor Kupolov9cbfc9e2015-10-07 15:52:33 -0700107 UserManager.DISALLOW_CAMERA,
Mahaver Chopradea471e2015-12-17 11:02:37 +0000108 UserManager.DISALLOW_RUN_IN_BACKGROUND,
Oleksandr Peletskyi7f1f1df2016-01-18 15:40:21 +0100109 UserManager.DISALLOW_DATA_ROAMING,
Oleksandr Peletskyif2519812016-01-26 20:16:06 +0100110 UserManager.DISALLOW_SET_USER_ICON,
Mahaver Chopra3d9805d2016-07-07 16:25:05 +0100111 UserManager.DISALLOW_SET_WALLPAPER,
Tony Makc1205112016-07-22 16:02:59 +0100112 UserManager.DISALLOW_OEM_UNLOCK,
Esteban Talavera492b4722017-02-13 14:59:45 +0000113 UserManager.DISALLOW_UNMUTE_DEVICE,
Felipe Leme24d58932017-03-21 14:13:58 -0700114 UserManager.DISALLOW_AUTOFILL,
Benjamin Franzff66fa92017-08-10 10:39:44 +0100115 UserManager.DISALLOW_USER_SWITCH
Makoto Onuki1f1ceef2016-01-28 11:32:32 -0800116 });
Makoto Onukia4f11972015-10-01 13:19:58 -0700117
118 /**
119 * Set of user restriction which we don't want to persist.
120 */
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800121 private static final Set<String> NON_PERSIST_USER_RESTRICTIONS = Sets.newArraySet(
122 UserManager.DISALLOW_RECORD_AUDIO
123 );
Makoto Onukia4f11972015-10-01 13:19:58 -0700124
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800125 /**
Pavel Grafov6a40f092016-10-25 15:46:51 +0100126 * User restrictions that cannot be set by profile owners of secondary users. When set by DO
127 * they will be applied to all users.
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800128 */
Pavel Grafov6a40f092016-10-25 15:46:51 +0100129 private static final Set<String> PRIMARY_USER_ONLY_RESTRICTIONS = Sets.newArraySet(
Lenka Trochtova63d5e4a72016-12-02 12:19:39 +0100130 UserManager.DISALLOW_BLUETOOTH,
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800131 UserManager.DISALLOW_USB_FILE_TRANSFER,
132 UserManager.DISALLOW_CONFIG_TETHERING,
133 UserManager.DISALLOW_NETWORK_RESET,
134 UserManager.DISALLOW_FACTORY_RESET,
135 UserManager.DISALLOW_ADD_USER,
136 UserManager.DISALLOW_CONFIG_CELL_BROADCASTS,
137 UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS,
138 UserManager.DISALLOW_MOUNT_PHYSICAL_MEDIA,
139 UserManager.DISALLOW_SMS,
140 UserManager.DISALLOW_FUN,
141 UserManager.DISALLOW_SAFE_BOOT,
Mahaver Chopradea471e2015-12-17 11:02:37 +0000142 UserManager.DISALLOW_CREATE_WINDOWS,
143 UserManager.DISALLOW_DATA_ROAMING
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800144 );
145
146 /**
Benjamin Franzff66fa92017-08-10 10:39:44 +0100147 * User restrictions that cannot be set by profile owners. Applied to all users.
148 */
149 private static final Set<String> DEVICE_OWNER_ONLY_RESTRICTIONS = Sets.newArraySet(
150 UserManager.DISALLOW_USER_SWITCH
151 );
152
153 /**
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800154 * User restrictions that can't be changed by device owner or profile owner.
155 */
156 private static final Set<String> IMMUTABLE_BY_OWNERS = Sets.newArraySet(
157 UserManager.DISALLOW_RECORD_AUDIO,
Mahaver Chopra3d9805d2016-07-07 16:25:05 +0100158 UserManager.DISALLOW_WALLPAPER,
159 UserManager.DISALLOW_OEM_UNLOCK
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800160 );
161
162 /**
163 * Special user restrictions that can be applied to a user as well as to all users globally,
164 * depending on callers. When device owner sets them, they'll be applied to all users.
165 */
166 private static final Set<String> GLOBAL_RESTRICTIONS = Sets.newArraySet(
167 UserManager.DISALLOW_ADJUST_VOLUME,
Pavel Grafov7f4ad752017-03-28 13:44:04 +0100168 UserManager.DISALLOW_BLUETOOTH_SHARING,
yuemingwa9772f362017-10-23 18:34:35 +0100169 UserManager.DISALLOW_CONFIG_DATE_TIME,
Charles He22ff6f9d2017-10-05 21:28:55 +0100170 UserManager.DISALLOW_SYSTEM_ERROR_DIALOGS,
Fyodor Kupolov9cbfc9e2015-10-07 15:52:33 -0700171 UserManager.DISALLOW_RUN_IN_BACKGROUND,
Tony Makc1205112016-07-22 16:02:59 +0100172 UserManager.DISALLOW_UNMUTE_MICROPHONE,
Esteban Talavera492b4722017-02-13 14:59:45 +0000173 UserManager.DISALLOW_UNMUTE_DEVICE
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800174 );
175
Makoto Onuki1f1ceef2016-01-28 11:32:32 -0800176 /**
Esteban Talavera6c9116a2016-11-24 16:12:44 +0000177 * User restrictions that default to {@code true} for device owners.
178 */
179 private static final Set<String> DEFAULT_ENABLED_FOR_DEVICE_OWNERS = Sets.newArraySet(
180 UserManager.DISALLOW_ADD_MANAGED_PROFILE
181 );
182
Pavel Grafov7f4ad752017-03-28 13:44:04 +0100183 /**
184 * User restrictions that default to {@code true} for managed profile owners.
185 *
186 * NB: {@link UserManager#DISALLOW_INSTALL_UNKNOWN_SOURCES} is also set by default but it is
187 * not set to existing profile owners unless they used to have INSTALL_NON_MARKET_APPS disabled
188 * in settings. So it is handled separately.
189 */
190 private static final Set<String> DEFAULT_ENABLED_FOR_MANAGED_PROFILES = Sets.newArraySet(
191 UserManager.DISALLOW_BLUETOOTH_SHARING
192 );
193
Pavel Grafov6a40f092016-10-25 15:46:51 +0100194 /*
195 * Special user restrictions that are always applied to all users no matter who sets them.
196 */
197 private static final Set<String> PROFILE_GLOBAL_RESTRICTIONS = Sets.newArraySet(
198 UserManager.ENSURE_VERIFY_APPS
199 );
200
Esteban Talavera6c9116a2016-11-24 16:12:44 +0000201 /**
Makoto Onuki1f1ceef2016-01-28 11:32:32 -0800202 * Throws {@link IllegalArgumentException} if the given restriction name is invalid.
203 */
204 public static boolean isValidRestriction(@NonNull String restriction) {
205 if (!USER_RESTRICTIONS.contains(restriction)) {
Makoto Onukiad5619d2016-02-10 13:41:15 -0800206 Slog.e(TAG, "Unknown restriction: " + restriction);
Makoto Onuki1f1ceef2016-01-28 11:32:32 -0800207 return false;
208 }
209 return true;
210 }
211
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800212 public static void writeRestrictions(@NonNull XmlSerializer serializer,
213 @Nullable Bundle restrictions, @NonNull String tag) throws IOException {
214 if (restrictions == null) {
215 return;
216 }
217
Makoto Onukia4f11972015-10-01 13:19:58 -0700218 serializer.startTag(null, tag);
Makoto Onukiac65e1e2015-11-20 15:33:17 -0800219 for (String key : restrictions.keySet()) {
220 if (NON_PERSIST_USER_RESTRICTIONS.contains(key)) {
221 continue; // Don't persist.
Makoto Onukia4f11972015-10-01 13:19:58 -0700222 }
Makoto Onukiac65e1e2015-11-20 15:33:17 -0800223 if (USER_RESTRICTIONS.contains(key)) {
224 if (restrictions.getBoolean(key)) {
225 serializer.attribute(null, key, "true");
226 }
227 continue;
228 }
229 Log.w(TAG, "Unknown user restriction detected: " + key);
Makoto Onukia4f11972015-10-01 13:19:58 -0700230 }
231 serializer.endTag(null, tag);
232 }
233
Esteban Talaverac48b20f2016-08-11 11:23:40 +0100234 public static void readRestrictions(XmlPullParser parser, Bundle restrictions) {
Fyodor Kupoloveafee022017-03-15 17:09:04 -0700235 restrictions.clear();
Makoto Onukia4f11972015-10-01 13:19:58 -0700236 for (String key : USER_RESTRICTIONS) {
237 final String value = parser.getAttributeValue(null, key);
238 if (value != null) {
239 restrictions.putBoolean(key, Boolean.parseBoolean(value));
240 }
241 }
242 }
243
Pavel Grafov6a40f092016-10-25 15:46:51 +0100244 public static Bundle readRestrictions(XmlPullParser parser) {
245 final Bundle result = new Bundle();
246 readRestrictions(parser, result);
247 return result;
248 }
249
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800250 /**
251 * @return {@code in} itself when it's not null, or an empty bundle (which can writable).
252 */
253 public static Bundle nonNull(@Nullable Bundle in) {
254 return in != null ? in : new Bundle();
255 }
256
257 public static boolean isEmpty(@Nullable Bundle in) {
258 return (in == null) || (in.size() == 0);
259 }
260
261 /**
Pavel Grafov6a40f092016-10-25 15:46:51 +0100262 * Returns {@code true} if given bundle is not null and contains {@code true} for a given
263 * restriction.
264 */
265 public static boolean contains(@Nullable Bundle in, String restriction) {
266 return in != null && in.getBoolean(restriction);
267 }
268
269 /**
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800270 * Creates a copy of the {@code in} Bundle. If {@code in} is null, it'll return an empty
271 * bundle.
272 *
273 * <p>The resulting {@link Bundle} is always writable. (i.e. it won't return
274 * {@link Bundle#EMPTY})
275 */
276 public static @NonNull Bundle clone(@Nullable Bundle in) {
277 return (in != null) ? new Bundle(in) : new Bundle();
278 }
279
280 public static void merge(@NonNull Bundle dest, @Nullable Bundle in) {
281 Preconditions.checkNotNull(dest);
282 Preconditions.checkArgument(dest != in);
Makoto Onuki068c54a2015-10-13 14:34:03 -0700283 if (in == null) {
284 return;
285 }
286 for (String key : in.keySet()) {
287 if (in.getBoolean(key, false)) {
288 dest.putBoolean(key, true);
289 }
290 }
291 }
292
Makoto Onuki4f160732015-10-27 17:15:38 -0700293 /**
Pavel Grafov6a40f092016-10-25 15:46:51 +0100294 * Merges a sparse array of restrictions bundles into one.
295 */
296 @Nullable
297 public static Bundle mergeAll(SparseArray<Bundle> restrictions) {
298 if (restrictions.size() == 0) {
299 return null;
300 } else {
301 final Bundle result = new Bundle();
302 for (int i = 0; i < restrictions.size(); i++) {
303 merge(result, restrictions.valueAt(i));
304 }
305 return result;
306 }
307 }
308
309 /**
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800310 * @return true if a restriction is settable by device owner.
311 */
312 public static boolean canDeviceOwnerChange(String restriction) {
313 return !IMMUTABLE_BY_OWNERS.contains(restriction);
314 }
315
316 /**
Makoto Onuki5485ed42015-12-09 11:38:23 -0800317 * @return true if a restriction is settable by profile owner. Note it takes a user ID because
318 * some restrictions can be changed by PO only when it's running on the system user.
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800319 */
Makoto Onuki5485ed42015-12-09 11:38:23 -0800320 public static boolean canProfileOwnerChange(String restriction, int userId) {
321 return !IMMUTABLE_BY_OWNERS.contains(restriction)
Benjamin Franzff66fa92017-08-10 10:39:44 +0100322 && !DEVICE_OWNER_ONLY_RESTRICTIONS.contains(restriction)
Makoto Onuki5485ed42015-12-09 11:38:23 -0800323 && !(userId != UserHandle.USER_SYSTEM
Pavel Grafov6a40f092016-10-25 15:46:51 +0100324 && PRIMARY_USER_ONLY_RESTRICTIONS.contains(restriction));
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800325 }
326
327 /**
Esteban Talavera6c9116a2016-11-24 16:12:44 +0000328 * Returns the user restrictions that default to {@code true} for device owners.
Nicolas Prevot2ea46fe2017-01-05 10:29:34 +0000329 * These user restrictions are local, though. ie only for the device owner's user id.
Esteban Talavera6c9116a2016-11-24 16:12:44 +0000330 */
331 public static @NonNull Set<String> getDefaultEnabledForDeviceOwner() {
332 return DEFAULT_ENABLED_FOR_DEVICE_OWNERS;
333 }
334
335 /**
Pavel Grafov7f4ad752017-03-28 13:44:04 +0100336 * Returns the user restrictions that default to {@code true} for managed profile owners.
337 */
338 public static @NonNull Set<String> getDefaultEnabledForManagedProfiles() {
339 return DEFAULT_ENABLED_FOR_MANAGED_PROFILES;
340 }
341
342 /**
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800343 * Takes restrictions that can be set by device owner, and sort them into what should be applied
344 * globally and what should be applied only on the current user.
345 */
Pavel Grafov6a40f092016-10-25 15:46:51 +0100346 public static void sortToGlobalAndLocal(@Nullable Bundle in, boolean isDeviceOwner,
347 int cameraRestrictionScope,
348 @NonNull Bundle global, @NonNull Bundle local) {
349 // Camera restriction (as well as all others) goes to at most one bundle.
350 if (cameraRestrictionScope == UserManagerInternal.CAMERA_DISABLED_GLOBALLY) {
351 global.putBoolean(UserManager.DISALLOW_CAMERA, true);
352 } else if (cameraRestrictionScope == UserManagerInternal.CAMERA_DISABLED_LOCALLY) {
353 local.putBoolean(UserManager.DISALLOW_CAMERA, true);
354 }
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800355 if (in == null || in.size() == 0) {
356 return;
357 }
358 for (String key : in.keySet()) {
359 if (!in.getBoolean(key)) {
360 continue;
361 }
Pavel Grafov6a40f092016-10-25 15:46:51 +0100362 if (isGlobal(isDeviceOwner, key)) {
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800363 global.putBoolean(key, true);
364 } else {
365 local.putBoolean(key, true);
366 }
367 }
368 }
369
370 /**
Pavel Grafov6a40f092016-10-25 15:46:51 +0100371 * Whether given user restriction should be enforced globally.
372 */
373 private static boolean isGlobal(boolean isDeviceOwner, String key) {
374 return (isDeviceOwner &&
Benjamin Franzff66fa92017-08-10 10:39:44 +0100375 (PRIMARY_USER_ONLY_RESTRICTIONS.contains(key) || GLOBAL_RESTRICTIONS.contains(key)))
376 || PROFILE_GLOBAL_RESTRICTIONS.contains(key)
377 || DEVICE_OWNER_ONLY_RESTRICTIONS.contains(key);
Pavel Grafov6a40f092016-10-25 15:46:51 +0100378 }
379
380 /**
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800381 * @return true if two Bundles contain the same user restriction.
382 * A null bundle and an empty bundle are considered to be equal.
383 */
384 public static boolean areEqual(@Nullable Bundle a, @Nullable Bundle b) {
385 if (a == b) {
386 return true;
387 }
388 if (isEmpty(a)) {
389 return isEmpty(b);
390 }
391 if (isEmpty(b)) {
392 return false;
393 }
394 for (String key : a.keySet()) {
395 if (a.getBoolean(key) != b.getBoolean(key)) {
396 return false;
397 }
398 }
399 for (String key : b.keySet()) {
400 if (a.getBoolean(key) != b.getBoolean(key)) {
401 return false;
402 }
403 }
404 return true;
405 }
406
407 /**
Makoto Onuki4f160732015-10-27 17:15:38 -0700408 * Takes a new use restriction set and the previous set, and apply the restrictions that have
409 * changed.
Makoto Onukid45a4a22015-11-02 17:17:38 -0800410 *
Fyodor Kupolov9cbfc9e2015-10-07 15:52:33 -0700411 * <p>Note this method is called by {@link UserManagerService} without holding any locks.
Makoto Onuki4f160732015-10-27 17:15:38 -0700412 */
Fyodor Kupolov9cbfc9e2015-10-07 15:52:33 -0700413 public static void applyUserRestrictions(Context context, int userId,
Makoto Onukid45a4a22015-11-02 17:17:38 -0800414 Bundle newRestrictions, Bundle prevRestrictions) {
Makoto Onuki4f160732015-10-27 17:15:38 -0700415 for (String key : USER_RESTRICTIONS) {
416 final boolean newValue = newRestrictions.getBoolean(key);
417 final boolean prevValue = prevRestrictions.getBoolean(key);
418
419 if (newValue != prevValue) {
Fyodor Kupolov9cbfc9e2015-10-07 15:52:33 -0700420 applyUserRestriction(context, userId, key, newValue);
Makoto Onuki4f160732015-10-27 17:15:38 -0700421 }
422 }
423 }
Fyodor Kupolov9cbfc9e2015-10-07 15:52:33 -0700424
Makoto Onukid45a4a22015-11-02 17:17:38 -0800425 /**
426 * Apply each user restriction.
427 *
Makoto Onuki28da2e32015-11-20 11:30:44 -0800428 * <p>See also {@link
429 * com.android.providers.settings.SettingsProvider#isGlobalOrSecureSettingRestrictedForUser},
430 * which should be in sync with this method.
Makoto Onukid45a4a22015-11-02 17:17:38 -0800431 */
Fyodor Kupolov9cbfc9e2015-10-07 15:52:33 -0700432 private static void applyUserRestriction(Context context, int userId, String key,
Makoto Onuki4f160732015-10-27 17:15:38 -0700433 boolean newValue) {
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800434 if (UserManagerService.DBG) {
435 Log.d(TAG, "Applying user restriction: userId=" + userId
436 + " key=" + key + " value=" + newValue);
437 }
Makoto Onuki4f160732015-10-27 17:15:38 -0700438 // When certain restrictions are cleared, we don't update the system settings,
439 // because these settings are changeable on the Settings UI and we don't know the original
440 // value -- for example LOCATION_MODE might have been off already when the restriction was
441 // set, and in that case even if the restriction is lifted, changing it to ON would be
442 // wrong. So just don't do anything in such a case. If the user hopes to enable location
443 // later, they can do it on the Settings UI.
Benjamin Franz0ff13fc2016-07-12 13:42:21 +0100444 // WARNING: Remember that Settings.Global and Settings.Secure are changeable via adb.
445 // To prevent this from happening for a given user restriction, you have to add a check to
446 // SettingsProvider.isGlobalOrSecureSettingRestrictedForUser.
Makoto Onuki4f160732015-10-27 17:15:38 -0700447
448 final ContentResolver cr = context.getContentResolver();
449 final long id = Binder.clearCallingIdentity();
450 try {
451 switch (key) {
Mahaver Chopradea471e2015-12-17 11:02:37 +0000452 case UserManager.DISALLOW_DATA_ROAMING:
453 if (newValue) {
454 // DISALLOW_DATA_ROAMING user restriction is set.
455
456 // Multi sim device.
457 SubscriptionManager subscriptionManager = new SubscriptionManager(context);
458 final List<SubscriptionInfo> subscriptionInfoList =
459 subscriptionManager.getActiveSubscriptionInfoList();
460 if (subscriptionInfoList != null) {
461 for (SubscriptionInfo subInfo : subscriptionInfoList) {
462 android.provider.Settings.Global.putStringForUser(cr,
463 android.provider.Settings.Global.DATA_ROAMING
464 + subInfo.getSubscriptionId(), "0", userId);
465 }
466 }
467
468 // Single sim device.
469 android.provider.Settings.Global.putStringForUser(cr,
470 android.provider.Settings.Global.DATA_ROAMING, "0", userId);
471 }
472 break;
Makoto Onuki4f160732015-10-27 17:15:38 -0700473 case UserManager.DISALLOW_SHARE_LOCATION:
474 if (newValue) {
475 android.provider.Settings.Secure.putIntForUser(cr,
476 android.provider.Settings.Secure.LOCATION_MODE,
477 android.provider.Settings.Secure.LOCATION_MODE_OFF,
478 userId);
Makoto Onuki4f160732015-10-27 17:15:38 -0700479 }
Makoto Onuki4f160732015-10-27 17:15:38 -0700480 break;
481 case UserManager.DISALLOW_DEBUGGING_FEATURES:
482 if (newValue) {
483 // Only disable adb if changing for system user, since it is global
484 // TODO: should this be admin user?
485 if (userId == UserHandle.USER_SYSTEM) {
486 android.provider.Settings.Global.putStringForUser(cr,
487 android.provider.Settings.Global.ADB_ENABLED, "0",
488 userId);
489 }
490 }
491 break;
492 case UserManager.ENSURE_VERIFY_APPS:
493 if (newValue) {
494 android.provider.Settings.Global.putStringForUser(
495 context.getContentResolver(),
496 android.provider.Settings.Global.PACKAGE_VERIFIER_ENABLE, "1",
497 userId);
498 android.provider.Settings.Global.putStringForUser(
499 context.getContentResolver(),
500 android.provider.Settings.Global.PACKAGE_VERIFIER_INCLUDE_ADB, "1",
501 userId);
502 }
503 break;
504 case UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES:
Suprabh Shuklae3745ee2017-02-02 20:01:11 -0800505 // Since Android O, the secure setting is not available to be changed by the
506 // user. Hence, when the restriction is cleared, we need to reset the state of
507 // the setting to its default value which is now 1.
508 android.provider.Settings.Secure.putIntForUser(cr,
509 android.provider.Settings.Secure.INSTALL_NON_MARKET_APPS,
510 newValue ? 0 : 1, userId);
Makoto Onuki4f160732015-10-27 17:15:38 -0700511 break;
Fyodor Kupolov9cbfc9e2015-10-07 15:52:33 -0700512 case UserManager.DISALLOW_RUN_IN_BACKGROUND:
513 if (newValue) {
514 int currentUser = ActivityManager.getCurrentUser();
515 if (currentUser != userId && userId != UserHandle.USER_SYSTEM) {
516 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -0800517 ActivityManager.getService().stopUser(userId, false, null);
Fyodor Kupolov9cbfc9e2015-10-07 15:52:33 -0700518 } catch (RemoteException e) {
519 throw e.rethrowAsRuntimeException();
520 }
521 }
522 }
Lenka Trochtova6474f0e2016-03-24 16:43:10 +0100523 break;
524 case UserManager.DISALLOW_SAFE_BOOT:
525 // Unlike with the other restrictions, we want to propagate the new value to
526 // the system settings even if it is false. The other restrictions modify
527 // settings which could be manually changed by the user from the Settings app
528 // after the policies enforcing these restrictions have been revoked, so we
529 // leave re-setting of those settings to the user.
530 android.provider.Settings.Global.putInt(
531 context.getContentResolver(),
532 android.provider.Settings.Global.SAFE_BOOT_DISALLOWED,
533 newValue ? 1 : 0);
534 break;
Makoto Onuki4f160732015-10-27 17:15:38 -0700535 }
Makoto Onuki4f160732015-10-27 17:15:38 -0700536 } finally {
537 Binder.restoreCallingIdentity(id);
538 }
539 }
540
Makoto Onukia4f11972015-10-01 13:19:58 -0700541 public static void dumpRestrictions(PrintWriter pw, String prefix, Bundle restrictions) {
542 boolean noneSet = true;
543 if (restrictions != null) {
544 for (String key : restrictions.keySet()) {
545 if (restrictions.getBoolean(key, false)) {
546 pw.println(prefix + key);
547 noneSet = false;
548 }
549 }
Makoto Onuki068c54a2015-10-13 14:34:03 -0700550 if (noneSet) {
551 pw.println(prefix + "none");
552 }
553 } else {
554 pw.println(prefix + "null");
Makoto Onukia4f11972015-10-01 13:19:58 -0700555 }
556 }
Pavel Grafov6a40f092016-10-25 15:46:51 +0100557
558 /**
559 * Moves a particular restriction from one array of bundles to another, e.g. for all users.
560 */
561 public static void moveRestriction(String restrictionKey, SparseArray<Bundle> srcRestrictions,
562 SparseArray<Bundle> destRestrictions) {
563 for (int i = 0; i < srcRestrictions.size(); i++) {
Pavel Grafov7f4ad752017-03-28 13:44:04 +0100564 final int key = srcRestrictions.keyAt(i);
565 final Bundle from = srcRestrictions.valueAt(i);
Pavel Grafov6a40f092016-10-25 15:46:51 +0100566 if (contains(from, restrictionKey)) {
567 from.remove(restrictionKey);
568 Bundle to = destRestrictions.get(key);
569 if (to == null) {
570 to = new Bundle();
571 destRestrictions.append(key, to);
572 }
573 to.putBoolean(restrictionKey, true);
574 // Don't keep empty bundles.
575 if (from.isEmpty()) {
576 srcRestrictions.removeAt(i);
577 i--;
578 }
579 }
580 }
581 }
Pavel Grafov7f4ad752017-03-28 13:44:04 +0100582
583 /**
584 * Returns whether restrictions differ between two bundles.
585 * @param oldRestrictions old bundle of restrictions.
586 * @param newRestrictions new bundle of restrictions
587 * @param restrictions restrictions of interest, if empty, all restrictions are checked.
588 */
589 public static boolean restrictionsChanged(Bundle oldRestrictions, Bundle newRestrictions,
590 String... restrictions) {
591 if (restrictions.length == 0) {
592 return areEqual(oldRestrictions, newRestrictions);
593 }
594 for (final String restriction : restrictions) {
595 if (oldRestrictions.getBoolean(restriction, false) !=
596 newRestrictions.getBoolean(restriction, false)) {
597 return true;
598 }
599 }
600 return false;
601 }
Makoto Onukia4f11972015-10-01 13:19:58 -0700602}