blob: dd04652a29b30fa176a6b2463f0a20fcba6f7ccc [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
Makoto Onuki1a2cd742015-11-16 13:51:27 -080019import android.annotation.NonNull;
20import android.annotation.Nullable;
Fyodor Kupolov9cbfc9e2015-10-07 15:52:33 -070021import android.app.ActivityManager;
yuemingw1d13eae2018-01-30 17:27:54 +000022import android.app.admin.DevicePolicyManager;
Makoto Onuki4f160732015-10-27 17:15:38 -070023import android.content.ContentResolver;
24import android.content.Context;
yuemingw265c6922018-01-11 18:31:14 +000025import android.content.Intent;
Makoto Onuki4f160732015-10-27 17:15:38 -070026import android.os.Binder;
Makoto Onukia4f11972015-10-01 13:19:58 -070027import android.os.Bundle;
yuemingw1d13eae2018-01-30 17:27:54 +000028import android.os.Process;
Fyodor Kupolov9cbfc9e2015-10-07 15:52:33 -070029import android.os.RemoteException;
Makoto Onuki4f160732015-10-27 17:15:38 -070030import android.os.UserHandle;
Makoto Onukia4f11972015-10-01 13:19:58 -070031import android.os.UserManager;
Pavel Grafov6a40f092016-10-25 15:46:51 +010032import android.os.UserManagerInternal;
yuemingw265c6922018-01-11 18:31:14 +000033import android.provider.Settings;
Makoto Onukiacc50462018-02-14 14:13:49 -080034import android.provider.Settings.Global;
Mahaver Chopradea471e2015-12-17 11:02:37 +000035import android.telephony.SubscriptionInfo;
36import android.telephony.SubscriptionManager;
Makoto Onuki1a2cd742015-11-16 13:51:27 -080037import android.util.Log;
Makoto Onuki1f1ceef2016-01-28 11:32:32 -080038import android.util.Slog;
Pavel Grafov6a40f092016-10-25 15:46:51 +010039import android.util.SparseArray;
Makoto Onukia4f11972015-10-01 13:19:58 -070040
Irina Dumitrescu4638edd2018-09-05 14:08:33 +010041import com.android.internal.util.Preconditions;
42
43import com.google.android.collect.Sets;
44
Makoto Onukia4f11972015-10-01 13:19:58 -070045import org.xmlpull.v1.XmlPullParser;
46import org.xmlpull.v1.XmlSerializer;
47
48import java.io.IOException;
49import java.io.PrintWriter;
Mahaver Chopradea471e2015-12-17 11:02:37 +000050import java.util.List;
Makoto Onukia4f11972015-10-01 13:19:58 -070051import java.util.Set;
52
Makoto Onukid45a4a22015-11-02 17:17:38 -080053/**
Mahaver Chopradea471e2015-12-17 11:02:37 +000054 * Utility methods for user restrictions.
Makoto Onukid45a4a22015-11-02 17:17:38 -080055 *
56 * <p>See {@link UserManagerService} for the method suffixes.
57 */
Makoto Onukia4f11972015-10-01 13:19:58 -070058public class UserRestrictionsUtils {
Makoto Onuki4f160732015-10-27 17:15:38 -070059 private static final String TAG = "UserRestrictionsUtils";
60
Makoto Onukia4f11972015-10-01 13:19:58 -070061 private UserRestrictionsUtils() {
62 }
63
Makoto Onuki1f1ceef2016-01-28 11:32:32 -080064 private static Set<String> newSetWithUniqueCheck(String[] strings) {
65 final Set<String> ret = Sets.newArraySet(strings);
66
67 // Make sure there's no overlap.
68 Preconditions.checkState(ret.size() == strings.length);
69 return ret;
70 }
71
72 public static final Set<String> USER_RESTRICTIONS = newSetWithUniqueCheck(new String[] {
Makoto Onukia4f11972015-10-01 13:19:58 -070073 UserManager.DISALLOW_CONFIG_WIFI,
Christine Franks1bade5d2017-10-10 15:41:50 -070074 UserManager.DISALLOW_CONFIG_LOCALE,
Makoto Onukia4f11972015-10-01 13:19:58 -070075 UserManager.DISALLOW_MODIFY_ACCOUNTS,
76 UserManager.DISALLOW_INSTALL_APPS,
77 UserManager.DISALLOW_UNINSTALL_APPS,
78 UserManager.DISALLOW_SHARE_LOCATION,
79 UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES,
Irina Dumitrescu4638edd2018-09-05 14:08:33 +010080 UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES_GLOBALLY,
Makoto Onukia4f11972015-10-01 13:19:58 -070081 UserManager.DISALLOW_CONFIG_BLUETOOTH,
Lenka Trochtova63d5e4a72016-12-02 12:19:39 +010082 UserManager.DISALLOW_BLUETOOTH,
Pavel Grafov7f4ad752017-03-28 13:44:04 +010083 UserManager.DISALLOW_BLUETOOTH_SHARING,
Makoto Onukia4f11972015-10-01 13:19:58 -070084 UserManager.DISALLOW_USB_FILE_TRANSFER,
85 UserManager.DISALLOW_CONFIG_CREDENTIALS,
86 UserManager.DISALLOW_REMOVE_USER,
Esteban Talavera6c9116a2016-11-24 16:12:44 +000087 UserManager.DISALLOW_REMOVE_MANAGED_PROFILE,
Makoto Onukia4f11972015-10-01 13:19:58 -070088 UserManager.DISALLOW_DEBUGGING_FEATURES,
89 UserManager.DISALLOW_CONFIG_VPN,
yuemingwa9772f362017-10-23 18:34:35 +010090 UserManager.DISALLOW_CONFIG_DATE_TIME,
Makoto Onukia4f11972015-10-01 13:19:58 -070091 UserManager.DISALLOW_CONFIG_TETHERING,
92 UserManager.DISALLOW_NETWORK_RESET,
93 UserManager.DISALLOW_FACTORY_RESET,
94 UserManager.DISALLOW_ADD_USER,
Esteban Talavera6c9116a2016-11-24 16:12:44 +000095 UserManager.DISALLOW_ADD_MANAGED_PROFILE,
Makoto Onukia4f11972015-10-01 13:19:58 -070096 UserManager.ENSURE_VERIFY_APPS,
97 UserManager.DISALLOW_CONFIG_CELL_BROADCASTS,
98 UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS,
99 UserManager.DISALLOW_APPS_CONTROL,
100 UserManager.DISALLOW_MOUNT_PHYSICAL_MEDIA,
101 UserManager.DISALLOW_UNMUTE_MICROPHONE,
102 UserManager.DISALLOW_ADJUST_VOLUME,
103 UserManager.DISALLOW_OUTGOING_CALLS,
104 UserManager.DISALLOW_SMS,
105 UserManager.DISALLOW_FUN,
106 UserManager.DISALLOW_CREATE_WINDOWS,
Charles He22ff6f9d2017-10-05 21:28:55 +0100107 UserManager.DISALLOW_SYSTEM_ERROR_DIALOGS,
Makoto Onukia4f11972015-10-01 13:19:58 -0700108 UserManager.DISALLOW_CROSS_PROFILE_COPY_PASTE,
109 UserManager.DISALLOW_OUTGOING_BEAM,
110 UserManager.DISALLOW_WALLPAPER,
111 UserManager.DISALLOW_SAFE_BOOT,
112 UserManager.ALLOW_PARENT_PROFILE_APP_LINKING,
113 UserManager.DISALLOW_RECORD_AUDIO,
Fyodor Kupolov9cbfc9e2015-10-07 15:52:33 -0700114 UserManager.DISALLOW_CAMERA,
Mahaver Chopradea471e2015-12-17 11:02:37 +0000115 UserManager.DISALLOW_RUN_IN_BACKGROUND,
Oleksandr Peletskyi7f1f1df2016-01-18 15:40:21 +0100116 UserManager.DISALLOW_DATA_ROAMING,
Oleksandr Peletskyif2519812016-01-26 20:16:06 +0100117 UserManager.DISALLOW_SET_USER_ICON,
Mahaver Chopra3d9805d2016-07-07 16:25:05 +0100118 UserManager.DISALLOW_SET_WALLPAPER,
Tony Makc1205112016-07-22 16:02:59 +0100119 UserManager.DISALLOW_OEM_UNLOCK,
Esteban Talavera492b4722017-02-13 14:59:45 +0000120 UserManager.DISALLOW_UNMUTE_DEVICE,
Felipe Leme24d58932017-03-21 14:13:58 -0700121 UserManager.DISALLOW_AUTOFILL,
Pavel Grafovc4f87e92017-10-26 16:34:25 +0100122 UserManager.DISALLOW_USER_SWITCH,
123 UserManager.DISALLOW_UNIFIED_PASSWORD,
yuemingw7810b8b2018-02-01 17:32:25 +0000124 UserManager.DISALLOW_CONFIG_LOCATION,
yuemingwc6ac29d2018-01-10 16:54:08 +0000125 UserManager.DISALLOW_AIRPLANE_MODE,
Rubin Xucc391c22018-01-02 20:37:35 +0000126 UserManager.DISALLOW_CONFIG_BRIGHTNESS,
127 UserManager.DISALLOW_SHARE_INTO_MANAGED_PROFILE,
yuemingw5cda3ae2018-01-15 10:26:19 +0000128 UserManager.DISALLOW_AMBIENT_DISPLAY,
Vladislav Kuzkokov622b9f92018-01-25 16:33:05 +0100129 UserManager.DISALLOW_CONFIG_SCREEN_TIMEOUT,
Eran Messeri09b122da2018-10-05 15:33:53 +0100130 UserManager.DISALLOW_PRINTING,
131 UserManager.DISALLOW_CONFIG_PRIVATE_DNS
Makoto Onuki1f1ceef2016-01-28 11:32:32 -0800132 });
Makoto Onukia4f11972015-10-01 13:19:58 -0700133
134 /**
135 * Set of user restriction which we don't want to persist.
136 */
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800137 private static final Set<String> NON_PERSIST_USER_RESTRICTIONS = Sets.newArraySet(
138 UserManager.DISALLOW_RECORD_AUDIO
139 );
Makoto Onukia4f11972015-10-01 13:19:58 -0700140
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800141 /**
Pavel Grafov6a40f092016-10-25 15:46:51 +0100142 * User restrictions that cannot be set by profile owners of secondary users. When set by DO
143 * they will be applied to all users.
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800144 */
Pavel Grafov6a40f092016-10-25 15:46:51 +0100145 private static final Set<String> PRIMARY_USER_ONLY_RESTRICTIONS = Sets.newArraySet(
Lenka Trochtova63d5e4a72016-12-02 12:19:39 +0100146 UserManager.DISALLOW_BLUETOOTH,
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800147 UserManager.DISALLOW_USB_FILE_TRANSFER,
148 UserManager.DISALLOW_CONFIG_TETHERING,
149 UserManager.DISALLOW_NETWORK_RESET,
150 UserManager.DISALLOW_FACTORY_RESET,
151 UserManager.DISALLOW_ADD_USER,
152 UserManager.DISALLOW_CONFIG_CELL_BROADCASTS,
153 UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS,
154 UserManager.DISALLOW_MOUNT_PHYSICAL_MEDIA,
155 UserManager.DISALLOW_SMS,
156 UserManager.DISALLOW_FUN,
157 UserManager.DISALLOW_SAFE_BOOT,
Mahaver Chopradea471e2015-12-17 11:02:37 +0000158 UserManager.DISALLOW_CREATE_WINDOWS,
yuemingw5fe75dc2017-11-29 15:52:56 +0000159 UserManager.DISALLOW_DATA_ROAMING,
160 UserManager.DISALLOW_AIRPLANE_MODE
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800161 );
162
163 /**
Benjamin Franzff66fa92017-08-10 10:39:44 +0100164 * User restrictions that cannot be set by profile owners. Applied to all users.
165 */
166 private static final Set<String> DEVICE_OWNER_ONLY_RESTRICTIONS = Sets.newArraySet(
Eran Messeri09b122da2018-10-05 15:33:53 +0100167 UserManager.DISALLOW_USER_SWITCH,
168 UserManager.DISALLOW_CONFIG_PRIVATE_DNS
Benjamin Franzff66fa92017-08-10 10:39:44 +0100169 );
170
171 /**
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800172 * User restrictions that can't be changed by device owner or profile owner.
173 */
174 private static final Set<String> IMMUTABLE_BY_OWNERS = Sets.newArraySet(
175 UserManager.DISALLOW_RECORD_AUDIO,
Mahaver Chopra3d9805d2016-07-07 16:25:05 +0100176 UserManager.DISALLOW_WALLPAPER,
177 UserManager.DISALLOW_OEM_UNLOCK
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800178 );
179
180 /**
181 * Special user restrictions that can be applied to a user as well as to all users globally,
182 * depending on callers. When device owner sets them, they'll be applied to all users.
183 */
184 private static final Set<String> GLOBAL_RESTRICTIONS = Sets.newArraySet(
185 UserManager.DISALLOW_ADJUST_VOLUME,
Pavel Grafov7f4ad752017-03-28 13:44:04 +0100186 UserManager.DISALLOW_BLUETOOTH_SHARING,
yuemingwa9772f362017-10-23 18:34:35 +0100187 UserManager.DISALLOW_CONFIG_DATE_TIME,
Charles He22ff6f9d2017-10-05 21:28:55 +0100188 UserManager.DISALLOW_SYSTEM_ERROR_DIALOGS,
Fyodor Kupolov9cbfc9e2015-10-07 15:52:33 -0700189 UserManager.DISALLOW_RUN_IN_BACKGROUND,
Tony Makc1205112016-07-22 16:02:59 +0100190 UserManager.DISALLOW_UNMUTE_MICROPHONE,
Esteban Talavera492b4722017-02-13 14:59:45 +0000191 UserManager.DISALLOW_UNMUTE_DEVICE
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800192 );
193
Makoto Onuki1f1ceef2016-01-28 11:32:32 -0800194 /**
Esteban Talavera6c9116a2016-11-24 16:12:44 +0000195 * User restrictions that default to {@code true} for device owners.
196 */
197 private static final Set<String> DEFAULT_ENABLED_FOR_DEVICE_OWNERS = Sets.newArraySet(
198 UserManager.DISALLOW_ADD_MANAGED_PROFILE
199 );
200
Pavel Grafov7f4ad752017-03-28 13:44:04 +0100201 /**
202 * User restrictions that default to {@code true} for managed profile owners.
203 *
204 * NB: {@link UserManager#DISALLOW_INSTALL_UNKNOWN_SOURCES} is also set by default but it is
205 * not set to existing profile owners unless they used to have INSTALL_NON_MARKET_APPS disabled
206 * in settings. So it is handled separately.
207 */
208 private static final Set<String> DEFAULT_ENABLED_FOR_MANAGED_PROFILES = Sets.newArraySet(
209 UserManager.DISALLOW_BLUETOOTH_SHARING
210 );
211
Pavel Grafovc4f87e92017-10-26 16:34:25 +0100212 /**
Pavel Grafov6a40f092016-10-25 15:46:51 +0100213 * Special user restrictions that are always applied to all users no matter who sets them.
214 */
215 private static final Set<String> PROFILE_GLOBAL_RESTRICTIONS = Sets.newArraySet(
yuemingw5fe75dc2017-11-29 15:52:56 +0000216 UserManager.ENSURE_VERIFY_APPS,
Irina Dumitrescu4638edd2018-09-05 14:08:33 +0100217 UserManager.DISALLOW_AIRPLANE_MODE,
218 UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES_GLOBALLY
Pavel Grafov6a40f092016-10-25 15:46:51 +0100219 );
220
Esteban Talavera6c9116a2016-11-24 16:12:44 +0000221 /**
Makoto Onuki1f1ceef2016-01-28 11:32:32 -0800222 * Throws {@link IllegalArgumentException} if the given restriction name is invalid.
223 */
224 public static boolean isValidRestriction(@NonNull String restriction) {
225 if (!USER_RESTRICTIONS.contains(restriction)) {
Makoto Onukiad5619d2016-02-10 13:41:15 -0800226 Slog.e(TAG, "Unknown restriction: " + restriction);
Makoto Onuki1f1ceef2016-01-28 11:32:32 -0800227 return false;
228 }
229 return true;
230 }
231
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800232 public static void writeRestrictions(@NonNull XmlSerializer serializer,
233 @Nullable Bundle restrictions, @NonNull String tag) throws IOException {
234 if (restrictions == null) {
235 return;
236 }
237
Makoto Onukia4f11972015-10-01 13:19:58 -0700238 serializer.startTag(null, tag);
Makoto Onukiac65e1e2015-11-20 15:33:17 -0800239 for (String key : restrictions.keySet()) {
240 if (NON_PERSIST_USER_RESTRICTIONS.contains(key)) {
241 continue; // Don't persist.
Makoto Onukia4f11972015-10-01 13:19:58 -0700242 }
Makoto Onukiac65e1e2015-11-20 15:33:17 -0800243 if (USER_RESTRICTIONS.contains(key)) {
244 if (restrictions.getBoolean(key)) {
245 serializer.attribute(null, key, "true");
246 }
247 continue;
248 }
249 Log.w(TAG, "Unknown user restriction detected: " + key);
Makoto Onukia4f11972015-10-01 13:19:58 -0700250 }
251 serializer.endTag(null, tag);
252 }
253
Esteban Talaverac48b20f2016-08-11 11:23:40 +0100254 public static void readRestrictions(XmlPullParser parser, Bundle restrictions) {
Fyodor Kupoloveafee022017-03-15 17:09:04 -0700255 restrictions.clear();
Makoto Onukia4f11972015-10-01 13:19:58 -0700256 for (String key : USER_RESTRICTIONS) {
257 final String value = parser.getAttributeValue(null, key);
258 if (value != null) {
259 restrictions.putBoolean(key, Boolean.parseBoolean(value));
260 }
261 }
262 }
263
Pavel Grafov6a40f092016-10-25 15:46:51 +0100264 public static Bundle readRestrictions(XmlPullParser parser) {
265 final Bundle result = new Bundle();
266 readRestrictions(parser, result);
267 return result;
268 }
269
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800270 /**
271 * @return {@code in} itself when it's not null, or an empty bundle (which can writable).
272 */
273 public static Bundle nonNull(@Nullable Bundle in) {
274 return in != null ? in : new Bundle();
275 }
276
277 public static boolean isEmpty(@Nullable Bundle in) {
278 return (in == null) || (in.size() == 0);
279 }
280
281 /**
Pavel Grafov6a40f092016-10-25 15:46:51 +0100282 * Returns {@code true} if given bundle is not null and contains {@code true} for a given
283 * restriction.
284 */
285 public static boolean contains(@Nullable Bundle in, String restriction) {
286 return in != null && in.getBoolean(restriction);
287 }
288
289 /**
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800290 * Creates a copy of the {@code in} Bundle. If {@code in} is null, it'll return an empty
291 * bundle.
292 *
293 * <p>The resulting {@link Bundle} is always writable. (i.e. it won't return
294 * {@link Bundle#EMPTY})
295 */
296 public static @NonNull Bundle clone(@Nullable Bundle in) {
297 return (in != null) ? new Bundle(in) : new Bundle();
298 }
299
300 public static void merge(@NonNull Bundle dest, @Nullable Bundle in) {
301 Preconditions.checkNotNull(dest);
302 Preconditions.checkArgument(dest != in);
Makoto Onuki068c54a2015-10-13 14:34:03 -0700303 if (in == null) {
304 return;
305 }
306 for (String key : in.keySet()) {
307 if (in.getBoolean(key, false)) {
308 dest.putBoolean(key, true);
309 }
310 }
311 }
312
Makoto Onuki4f160732015-10-27 17:15:38 -0700313 /**
Pavel Grafov6a40f092016-10-25 15:46:51 +0100314 * Merges a sparse array of restrictions bundles into one.
315 */
316 @Nullable
317 public static Bundle mergeAll(SparseArray<Bundle> restrictions) {
318 if (restrictions.size() == 0) {
319 return null;
320 } else {
321 final Bundle result = new Bundle();
322 for (int i = 0; i < restrictions.size(); i++) {
323 merge(result, restrictions.valueAt(i));
324 }
325 return result;
326 }
327 }
328
329 /**
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800330 * @return true if a restriction is settable by device owner.
331 */
332 public static boolean canDeviceOwnerChange(String restriction) {
333 return !IMMUTABLE_BY_OWNERS.contains(restriction);
334 }
335
336 /**
Makoto Onuki5485ed42015-12-09 11:38:23 -0800337 * @return true if a restriction is settable by profile owner. Note it takes a user ID because
338 * some restrictions can be changed by PO only when it's running on the system user.
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800339 */
Makoto Onuki5485ed42015-12-09 11:38:23 -0800340 public static boolean canProfileOwnerChange(String restriction, int userId) {
341 return !IMMUTABLE_BY_OWNERS.contains(restriction)
Benjamin Franzff66fa92017-08-10 10:39:44 +0100342 && !DEVICE_OWNER_ONLY_RESTRICTIONS.contains(restriction)
Makoto Onuki5485ed42015-12-09 11:38:23 -0800343 && !(userId != UserHandle.USER_SYSTEM
Pavel Grafov6a40f092016-10-25 15:46:51 +0100344 && PRIMARY_USER_ONLY_RESTRICTIONS.contains(restriction));
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800345 }
346
347 /**
Esteban Talavera6c9116a2016-11-24 16:12:44 +0000348 * Returns the user restrictions that default to {@code true} for device owners.
Nicolas Prevot2ea46fe2017-01-05 10:29:34 +0000349 * These user restrictions are local, though. ie only for the device owner's user id.
Esteban Talavera6c9116a2016-11-24 16:12:44 +0000350 */
351 public static @NonNull Set<String> getDefaultEnabledForDeviceOwner() {
352 return DEFAULT_ENABLED_FOR_DEVICE_OWNERS;
353 }
354
355 /**
Pavel Grafov7f4ad752017-03-28 13:44:04 +0100356 * Returns the user restrictions that default to {@code true} for managed profile owners.
357 */
358 public static @NonNull Set<String> getDefaultEnabledForManagedProfiles() {
359 return DEFAULT_ENABLED_FOR_MANAGED_PROFILES;
360 }
361
362 /**
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800363 * Takes restrictions that can be set by device owner, and sort them into what should be applied
364 * globally and what should be applied only on the current user.
365 */
Pavel Grafov6a40f092016-10-25 15:46:51 +0100366 public static void sortToGlobalAndLocal(@Nullable Bundle in, boolean isDeviceOwner,
367 int cameraRestrictionScope,
368 @NonNull Bundle global, @NonNull Bundle local) {
369 // Camera restriction (as well as all others) goes to at most one bundle.
370 if (cameraRestrictionScope == UserManagerInternal.CAMERA_DISABLED_GLOBALLY) {
371 global.putBoolean(UserManager.DISALLOW_CAMERA, true);
372 } else if (cameraRestrictionScope == UserManagerInternal.CAMERA_DISABLED_LOCALLY) {
373 local.putBoolean(UserManager.DISALLOW_CAMERA, true);
374 }
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800375 if (in == null || in.size() == 0) {
376 return;
377 }
378 for (String key : in.keySet()) {
379 if (!in.getBoolean(key)) {
380 continue;
381 }
Pavel Grafov6a40f092016-10-25 15:46:51 +0100382 if (isGlobal(isDeviceOwner, key)) {
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800383 global.putBoolean(key, true);
384 } else {
385 local.putBoolean(key, true);
386 }
387 }
388 }
389
390 /**
Pavel Grafov6a40f092016-10-25 15:46:51 +0100391 * Whether given user restriction should be enforced globally.
392 */
393 private static boolean isGlobal(boolean isDeviceOwner, String key) {
394 return (isDeviceOwner &&
Benjamin Franzff66fa92017-08-10 10:39:44 +0100395 (PRIMARY_USER_ONLY_RESTRICTIONS.contains(key) || GLOBAL_RESTRICTIONS.contains(key)))
396 || PROFILE_GLOBAL_RESTRICTIONS.contains(key)
397 || DEVICE_OWNER_ONLY_RESTRICTIONS.contains(key);
Pavel Grafov6a40f092016-10-25 15:46:51 +0100398 }
399
400 /**
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800401 * @return true if two Bundles contain the same user restriction.
402 * A null bundle and an empty bundle are considered to be equal.
403 */
404 public static boolean areEqual(@Nullable Bundle a, @Nullable Bundle b) {
405 if (a == b) {
406 return true;
407 }
408 if (isEmpty(a)) {
409 return isEmpty(b);
410 }
411 if (isEmpty(b)) {
412 return false;
413 }
414 for (String key : a.keySet()) {
415 if (a.getBoolean(key) != b.getBoolean(key)) {
416 return false;
417 }
418 }
419 for (String key : b.keySet()) {
420 if (a.getBoolean(key) != b.getBoolean(key)) {
421 return false;
422 }
423 }
424 return true;
425 }
426
427 /**
Makoto Onuki4f160732015-10-27 17:15:38 -0700428 * Takes a new use restriction set and the previous set, and apply the restrictions that have
429 * changed.
Makoto Onukid45a4a22015-11-02 17:17:38 -0800430 *
Fyodor Kupolov9cbfc9e2015-10-07 15:52:33 -0700431 * <p>Note this method is called by {@link UserManagerService} without holding any locks.
Makoto Onuki4f160732015-10-27 17:15:38 -0700432 */
Fyodor Kupolov9cbfc9e2015-10-07 15:52:33 -0700433 public static void applyUserRestrictions(Context context, int userId,
Makoto Onukid45a4a22015-11-02 17:17:38 -0800434 Bundle newRestrictions, Bundle prevRestrictions) {
Makoto Onuki4f160732015-10-27 17:15:38 -0700435 for (String key : USER_RESTRICTIONS) {
436 final boolean newValue = newRestrictions.getBoolean(key);
437 final boolean prevValue = prevRestrictions.getBoolean(key);
438
439 if (newValue != prevValue) {
Fyodor Kupolov9cbfc9e2015-10-07 15:52:33 -0700440 applyUserRestriction(context, userId, key, newValue);
Makoto Onuki4f160732015-10-27 17:15:38 -0700441 }
442 }
443 }
Fyodor Kupolov9cbfc9e2015-10-07 15:52:33 -0700444
Makoto Onukid45a4a22015-11-02 17:17:38 -0800445 /**
446 * Apply each user restriction.
447 *
yuemingw1d13eae2018-01-30 17:27:54 +0000448 * <p>See also {@link #isSettingRestrictedForUser()},
Makoto Onuki28da2e32015-11-20 11:30:44 -0800449 * which should be in sync with this method.
Makoto Onukid45a4a22015-11-02 17:17:38 -0800450 */
Fyodor Kupolov9cbfc9e2015-10-07 15:52:33 -0700451 private static void applyUserRestriction(Context context, int userId, String key,
Makoto Onuki4f160732015-10-27 17:15:38 -0700452 boolean newValue) {
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800453 if (UserManagerService.DBG) {
454 Log.d(TAG, "Applying user restriction: userId=" + userId
455 + " key=" + key + " value=" + newValue);
456 }
Makoto Onuki4f160732015-10-27 17:15:38 -0700457 // When certain restrictions are cleared, we don't update the system settings,
458 // because these settings are changeable on the Settings UI and we don't know the original
459 // value -- for example LOCATION_MODE might have been off already when the restriction was
460 // set, and in that case even if the restriction is lifted, changing it to ON would be
461 // wrong. So just don't do anything in such a case. If the user hopes to enable location
462 // later, they can do it on the Settings UI.
Benjamin Franz0ff13fc2016-07-12 13:42:21 +0100463 // WARNING: Remember that Settings.Global and Settings.Secure are changeable via adb.
464 // To prevent this from happening for a given user restriction, you have to add a check to
465 // SettingsProvider.isGlobalOrSecureSettingRestrictedForUser.
Makoto Onuki4f160732015-10-27 17:15:38 -0700466
467 final ContentResolver cr = context.getContentResolver();
468 final long id = Binder.clearCallingIdentity();
469 try {
470 switch (key) {
Mahaver Chopradea471e2015-12-17 11:02:37 +0000471 case UserManager.DISALLOW_DATA_ROAMING:
472 if (newValue) {
473 // DISALLOW_DATA_ROAMING user restriction is set.
474
475 // Multi sim device.
Jeff Sharkey717f52f2018-01-04 16:04:11 -0700476 SubscriptionManager subscriptionManager = context
477 .getSystemService(SubscriptionManager.class);
Mahaver Chopradea471e2015-12-17 11:02:37 +0000478 final List<SubscriptionInfo> subscriptionInfoList =
479 subscriptionManager.getActiveSubscriptionInfoList();
480 if (subscriptionInfoList != null) {
481 for (SubscriptionInfo subInfo : subscriptionInfoList) {
482 android.provider.Settings.Global.putStringForUser(cr,
483 android.provider.Settings.Global.DATA_ROAMING
484 + subInfo.getSubscriptionId(), "0", userId);
485 }
486 }
487
488 // Single sim device.
489 android.provider.Settings.Global.putStringForUser(cr,
490 android.provider.Settings.Global.DATA_ROAMING, "0", userId);
491 }
492 break;
Makoto Onuki4f160732015-10-27 17:15:38 -0700493 case UserManager.DISALLOW_SHARE_LOCATION:
494 if (newValue) {
495 android.provider.Settings.Secure.putIntForUser(cr,
496 android.provider.Settings.Secure.LOCATION_MODE,
497 android.provider.Settings.Secure.LOCATION_MODE_OFF,
498 userId);
Makoto Onuki4f160732015-10-27 17:15:38 -0700499 }
Makoto Onuki4f160732015-10-27 17:15:38 -0700500 break;
501 case UserManager.DISALLOW_DEBUGGING_FEATURES:
502 if (newValue) {
503 // Only disable adb if changing for system user, since it is global
504 // TODO: should this be admin user?
505 if (userId == UserHandle.USER_SYSTEM) {
506 android.provider.Settings.Global.putStringForUser(cr,
507 android.provider.Settings.Global.ADB_ENABLED, "0",
508 userId);
509 }
510 }
511 break;
512 case UserManager.ENSURE_VERIFY_APPS:
513 if (newValue) {
514 android.provider.Settings.Global.putStringForUser(
515 context.getContentResolver(),
516 android.provider.Settings.Global.PACKAGE_VERIFIER_ENABLE, "1",
517 userId);
518 android.provider.Settings.Global.putStringForUser(
519 context.getContentResolver(),
520 android.provider.Settings.Global.PACKAGE_VERIFIER_INCLUDE_ADB, "1",
521 userId);
522 }
523 break;
Irina Dumitrescu4638edd2018-09-05 14:08:33 +0100524 case UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES_GLOBALLY:
525 setInstallMarketAppsRestriction(cr, userId, getNewUserRestrictionSetting(
526 context, userId, UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES,
527 newValue));
528 break;
Makoto Onuki4f160732015-10-27 17:15:38 -0700529 case UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES:
Suprabh Shuklae3745ee2017-02-02 20:01:11 -0800530 // Since Android O, the secure setting is not available to be changed by the
531 // user. Hence, when the restriction is cleared, we need to reset the state of
532 // the setting to its default value which is now 1.
Irina Dumitrescu4638edd2018-09-05 14:08:33 +0100533 setInstallMarketAppsRestriction(cr, userId, getNewUserRestrictionSetting(
534 context, userId, UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES_GLOBALLY,
535 newValue));
Makoto Onuki4f160732015-10-27 17:15:38 -0700536 break;
Fyodor Kupolov9cbfc9e2015-10-07 15:52:33 -0700537 case UserManager.DISALLOW_RUN_IN_BACKGROUND:
538 if (newValue) {
539 int currentUser = ActivityManager.getCurrentUser();
540 if (currentUser != userId && userId != UserHandle.USER_SYSTEM) {
541 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -0800542 ActivityManager.getService().stopUser(userId, false, null);
Fyodor Kupolov9cbfc9e2015-10-07 15:52:33 -0700543 } catch (RemoteException e) {
544 throw e.rethrowAsRuntimeException();
545 }
546 }
547 }
Lenka Trochtova6474f0e2016-03-24 16:43:10 +0100548 break;
549 case UserManager.DISALLOW_SAFE_BOOT:
550 // Unlike with the other restrictions, we want to propagate the new value to
551 // the system settings even if it is false. The other restrictions modify
552 // settings which could be manually changed by the user from the Settings app
553 // after the policies enforcing these restrictions have been revoked, so we
554 // leave re-setting of those settings to the user.
555 android.provider.Settings.Global.putInt(
556 context.getContentResolver(),
557 android.provider.Settings.Global.SAFE_BOOT_DISALLOWED,
558 newValue ? 1 : 0);
559 break;
yuemingw265c6922018-01-11 18:31:14 +0000560 case UserManager.DISALLOW_AIRPLANE_MODE:
561 if (newValue) {
562 final boolean airplaneMode = Settings.Global.getInt(
563 context.getContentResolver(),
564 Settings.Global.AIRPLANE_MODE_ON, 0) == 1;
565 if (airplaneMode) {
566 android.provider.Settings.Global.putInt(
567 context.getContentResolver(),
568 android.provider.Settings.Global.AIRPLANE_MODE_ON, 0);
569 // Post the intent.
570 Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
yuemingwf3b97342018-03-28 15:35:40 +0100571 intent.putExtra("state", false);
yuemingw265c6922018-01-11 18:31:14 +0000572 context.sendBroadcastAsUser(intent, UserHandle.ALL);
573 }
574 }
575 break;
yuemingwc0109512018-01-21 19:21:27 +0000576 case UserManager.DISALLOW_AMBIENT_DISPLAY:
577 if (newValue) {
Alex Chau1e2b7312018-02-28 10:47:48 +0000578 android.provider.Settings.Secure.putIntForUser(
yuemingwc0109512018-01-21 19:21:27 +0000579 context.getContentResolver(),
Alex Chau1e2b7312018-02-28 10:47:48 +0000580 Settings.Secure.DOZE_ENABLED, 0, userId);
581 android.provider.Settings.Secure.putIntForUser(
yuemingwc0109512018-01-21 19:21:27 +0000582 context.getContentResolver(),
Alex Chau1e2b7312018-02-28 10:47:48 +0000583 Settings.Secure.DOZE_ALWAYS_ON, 0, userId);
584 android.provider.Settings.Secure.putIntForUser(
yuemingwc0109512018-01-21 19:21:27 +0000585 context.getContentResolver(),
Lucas Dupin4359b552018-08-09 15:07:54 -0700586 Settings.Secure.DOZE_PICK_UP_GESTURE, 0, userId);
Alex Chau1e2b7312018-02-28 10:47:48 +0000587 android.provider.Settings.Secure.putIntForUser(
yuemingwc0109512018-01-21 19:21:27 +0000588 context.getContentResolver(),
Alex Chau1e2b7312018-02-28 10:47:48 +0000589 Settings.Secure.DOZE_PULSE_ON_LONG_PRESS, 0, userId);
590 android.provider.Settings.Secure.putIntForUser(
yuemingwc0109512018-01-21 19:21:27 +0000591 context.getContentResolver(),
Lucas Dupin4359b552018-08-09 15:07:54 -0700592 Settings.Secure.DOZE_DOUBLE_TAP_GESTURE, 0, userId);
yuemingwc0109512018-01-21 19:21:27 +0000593 }
594 break;
Makoto Onukiacc50462018-02-14 14:13:49 -0800595 case UserManager.DISALLOW_CONFIG_LOCATION:
596 // When DISALLOW_CONFIG_LOCATION is set on any user, we undo the global
597 // kill switch.
598 if (newValue) {
599 android.provider.Settings.Global.putString(
600 context.getContentResolver(),
601 Global.LOCATION_GLOBAL_KILL_SWITCH, "0");
602 }
603 break;
Makoto Onuki4f160732015-10-27 17:15:38 -0700604 }
Makoto Onuki4f160732015-10-27 17:15:38 -0700605 } finally {
606 Binder.restoreCallingIdentity(id);
607 }
608 }
609
yuemingw1d13eae2018-01-30 17:27:54 +0000610 public static boolean isSettingRestrictedForUser(Context context, @NonNull String setting,
611 int userId, String value, int callingUid) {
612 Preconditions.checkNotNull(setting);
613 final UserManager mUserManager = context.getSystemService(UserManager.class);
614 String restriction;
615 boolean checkAllUser = false;
616 switch (setting) {
617 case android.provider.Settings.Secure.LOCATION_MODE:
618 if (mUserManager.hasUserRestriction(
619 UserManager.DISALLOW_CONFIG_LOCATION, UserHandle.of(userId))
620 && callingUid != Process.SYSTEM_UID) {
621 return true;
622 } else if (String.valueOf(Settings.Secure.LOCATION_MODE_OFF).equals(value)) {
623 // Note LOCATION_MODE will be converted into LOCATION_PROVIDERS_ALLOWED
624 // in android.provider.Settings.Secure.putStringForUser(), so we shouldn't come
625 // here normally, but we still protect it here from a direct provider write.
626 return false;
627 }
628 restriction = UserManager.DISALLOW_SHARE_LOCATION;
629 break;
630
631 case android.provider.Settings.Secure.LOCATION_PROVIDERS_ALLOWED:
632 if (mUserManager.hasUserRestriction(
633 UserManager.DISALLOW_CONFIG_LOCATION, UserHandle.of(userId))
634 && callingUid != Process.SYSTEM_UID) {
635 return true;
636 } else if (value != null && value.startsWith("-")) {
637 // See SettingsProvider.updateLocationProvidersAllowedLocked. "-" is to disable
638 // a provider, which should be allowed even if the user restriction is set.
639 return false;
640 }
641 restriction = UserManager.DISALLOW_SHARE_LOCATION;
642 break;
643
644 case android.provider.Settings.Secure.INSTALL_NON_MARKET_APPS:
645 if ("0".equals(value)) {
646 return false;
647 }
648 restriction = UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES;
649 break;
650
651 case android.provider.Settings.Global.ADB_ENABLED:
652 if ("0".equals(value)) {
653 return false;
654 }
655 restriction = UserManager.DISALLOW_DEBUGGING_FEATURES;
656 break;
657
658 case android.provider.Settings.Global.PACKAGE_VERIFIER_ENABLE:
659 case android.provider.Settings.Global.PACKAGE_VERIFIER_INCLUDE_ADB:
660 if ("1".equals(value)) {
661 return false;
662 }
663 restriction = UserManager.ENSURE_VERIFY_APPS;
664 break;
665
666 case android.provider.Settings.Global.PREFERRED_NETWORK_MODE:
667 restriction = UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS;
668 break;
669
670 case android.provider.Settings.Secure.ALWAYS_ON_VPN_APP:
671 case android.provider.Settings.Secure.ALWAYS_ON_VPN_LOCKDOWN:
672 // Whitelist system uid (ConnectivityService) and root uid to change always-on vpn
673 final int appId = UserHandle.getAppId(callingUid);
674 if (appId == Process.SYSTEM_UID || appId == Process.ROOT_UID) {
675 return false;
676 }
677 restriction = UserManager.DISALLOW_CONFIG_VPN;
678 break;
679
680 case android.provider.Settings.Global.SAFE_BOOT_DISALLOWED:
681 if ("1".equals(value)) {
682 return false;
683 }
684 restriction = UserManager.DISALLOW_SAFE_BOOT;
685 break;
686
687 case android.provider.Settings.Global.AIRPLANE_MODE_ON:
688 if ("0".equals(value)) {
689 return false;
690 }
691 restriction = UserManager.DISALLOW_AIRPLANE_MODE;
692 break;
693
694 case android.provider.Settings.Secure.DOZE_ENABLED:
695 case android.provider.Settings.Secure.DOZE_ALWAYS_ON:
Lucas Dupin4359b552018-08-09 15:07:54 -0700696 case android.provider.Settings.Secure.DOZE_PICK_UP_GESTURE:
yuemingw1d13eae2018-01-30 17:27:54 +0000697 case android.provider.Settings.Secure.DOZE_PULSE_ON_LONG_PRESS:
Lucas Dupin4359b552018-08-09 15:07:54 -0700698 case android.provider.Settings.Secure.DOZE_DOUBLE_TAP_GESTURE:
yuemingw1d13eae2018-01-30 17:27:54 +0000699 if ("0".equals(value)) {
700 return false;
701 }
702 restriction = UserManager.DISALLOW_AMBIENT_DISPLAY;
703 break;
704
705 case android.provider.Settings.Global.LOCATION_GLOBAL_KILL_SWITCH:
706 if ("0".equals(value)) {
707 return false;
708 }
709 restriction = UserManager.DISALLOW_CONFIG_LOCATION;
710 checkAllUser = true;
711 break;
712
713 case android.provider.Settings.System.SCREEN_BRIGHTNESS:
714 case android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE:
715 if (callingUid == Process.SYSTEM_UID) {
716 return false;
717 }
718 restriction = UserManager.DISALLOW_CONFIG_BRIGHTNESS;
719 break;
720
721 case android.provider.Settings.Global.AUTO_TIME:
722 DevicePolicyManager dpm = context.getSystemService(DevicePolicyManager.class);
723 if (dpm != null && dpm.getAutoTimeRequired()
724 && "0".equals(value)) {
725 return true;
726 } else if (callingUid == Process.SYSTEM_UID) {
727 return false;
728 }
729 restriction = UserManager.DISALLOW_CONFIG_DATE_TIME;
730 break;
731
732 case android.provider.Settings.Global.AUTO_TIME_ZONE:
733 if (callingUid == Process.SYSTEM_UID) {
734 return false;
735 }
736 restriction = UserManager.DISALLOW_CONFIG_DATE_TIME;
737 break;
738
739 case android.provider.Settings.System.SCREEN_OFF_TIMEOUT:
740 if (callingUid == Process.SYSTEM_UID) {
741 return false;
742 }
743 restriction = UserManager.DISALLOW_CONFIG_SCREEN_TIMEOUT;
744 break;
745
Eran Messeri09b122da2018-10-05 15:33:53 +0100746 case android.provider.Settings.Global.PRIVATE_DNS_MODE:
747 case android.provider.Settings.Global.PRIVATE_DNS_SPECIFIER:
748 restriction = UserManager.DISALLOW_CONFIG_PRIVATE_DNS;
749 break;
yuemingw1d13eae2018-01-30 17:27:54 +0000750 default:
751 if (setting.startsWith(Settings.Global.DATA_ROAMING)) {
752 if ("0".equals(value)) {
753 return false;
754 }
755 restriction = UserManager.DISALLOW_DATA_ROAMING;
756 break;
757 }
758 return false;
759 }
760
761 if (checkAllUser) {
762 return mUserManager.hasUserRestrictionOnAnyUser(restriction);
763 } else {
764 return mUserManager.hasUserRestriction(restriction, UserHandle.of(userId));
765 }
766 }
767
Makoto Onukia4f11972015-10-01 13:19:58 -0700768 public static void dumpRestrictions(PrintWriter pw, String prefix, Bundle restrictions) {
769 boolean noneSet = true;
770 if (restrictions != null) {
771 for (String key : restrictions.keySet()) {
772 if (restrictions.getBoolean(key, false)) {
773 pw.println(prefix + key);
774 noneSet = false;
775 }
776 }
Makoto Onuki068c54a2015-10-13 14:34:03 -0700777 if (noneSet) {
778 pw.println(prefix + "none");
779 }
780 } else {
781 pw.println(prefix + "null");
Makoto Onukia4f11972015-10-01 13:19:58 -0700782 }
783 }
Pavel Grafov6a40f092016-10-25 15:46:51 +0100784
785 /**
786 * Moves a particular restriction from one array of bundles to another, e.g. for all users.
787 */
788 public static void moveRestriction(String restrictionKey, SparseArray<Bundle> srcRestrictions,
789 SparseArray<Bundle> destRestrictions) {
790 for (int i = 0; i < srcRestrictions.size(); i++) {
Pavel Grafov7f4ad752017-03-28 13:44:04 +0100791 final int key = srcRestrictions.keyAt(i);
792 final Bundle from = srcRestrictions.valueAt(i);
Pavel Grafov6a40f092016-10-25 15:46:51 +0100793 if (contains(from, restrictionKey)) {
794 from.remove(restrictionKey);
795 Bundle to = destRestrictions.get(key);
796 if (to == null) {
797 to = new Bundle();
798 destRestrictions.append(key, to);
799 }
800 to.putBoolean(restrictionKey, true);
801 // Don't keep empty bundles.
802 if (from.isEmpty()) {
803 srcRestrictions.removeAt(i);
804 i--;
805 }
806 }
807 }
808 }
Pavel Grafov7f4ad752017-03-28 13:44:04 +0100809
810 /**
811 * Returns whether restrictions differ between two bundles.
812 * @param oldRestrictions old bundle of restrictions.
813 * @param newRestrictions new bundle of restrictions
814 * @param restrictions restrictions of interest, if empty, all restrictions are checked.
815 */
816 public static boolean restrictionsChanged(Bundle oldRestrictions, Bundle newRestrictions,
817 String... restrictions) {
818 if (restrictions.length == 0) {
819 return areEqual(oldRestrictions, newRestrictions);
820 }
821 for (final String restriction : restrictions) {
822 if (oldRestrictions.getBoolean(restriction, false) !=
823 newRestrictions.getBoolean(restriction, false)) {
824 return true;
825 }
826 }
827 return false;
828 }
Irina Dumitrescu4638edd2018-09-05 14:08:33 +0100829
830 private static void setInstallMarketAppsRestriction(ContentResolver cr, int userId,
831 int settingValue) {
832 android.provider.Settings.Secure.putIntForUser(
833 cr, android.provider.Settings.Secure.INSTALL_NON_MARKET_APPS, settingValue, userId);
834 }
835
836 private static int getNewUserRestrictionSetting(Context context, int userId,
837 String userRestriction, boolean newValue) {
838 return (newValue || UserManager.get(context).hasUserRestriction(userRestriction,
839 UserHandle.of(userId))) ? 0 : 1;
840 }
Makoto Onukia4f11972015-10-01 13:19:58 -0700841}