blob: 13155027a3875e2e2547fb855c312eba30fa9c2b [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,
130 UserManager.DISALLOW_PRINTING
Makoto Onuki1f1ceef2016-01-28 11:32:32 -0800131 });
Makoto Onukia4f11972015-10-01 13:19:58 -0700132
133 /**
134 * Set of user restriction which we don't want to persist.
135 */
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800136 private static final Set<String> NON_PERSIST_USER_RESTRICTIONS = Sets.newArraySet(
137 UserManager.DISALLOW_RECORD_AUDIO
138 );
Makoto Onukia4f11972015-10-01 13:19:58 -0700139
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800140 /**
Pavel Grafov6a40f092016-10-25 15:46:51 +0100141 * User restrictions that cannot be set by profile owners of secondary users. When set by DO
142 * they will be applied to all users.
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800143 */
Pavel Grafov6a40f092016-10-25 15:46:51 +0100144 private static final Set<String> PRIMARY_USER_ONLY_RESTRICTIONS = Sets.newArraySet(
Lenka Trochtova63d5e4a72016-12-02 12:19:39 +0100145 UserManager.DISALLOW_BLUETOOTH,
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800146 UserManager.DISALLOW_USB_FILE_TRANSFER,
147 UserManager.DISALLOW_CONFIG_TETHERING,
148 UserManager.DISALLOW_NETWORK_RESET,
149 UserManager.DISALLOW_FACTORY_RESET,
150 UserManager.DISALLOW_ADD_USER,
151 UserManager.DISALLOW_CONFIG_CELL_BROADCASTS,
152 UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS,
153 UserManager.DISALLOW_MOUNT_PHYSICAL_MEDIA,
154 UserManager.DISALLOW_SMS,
155 UserManager.DISALLOW_FUN,
156 UserManager.DISALLOW_SAFE_BOOT,
Mahaver Chopradea471e2015-12-17 11:02:37 +0000157 UserManager.DISALLOW_CREATE_WINDOWS,
yuemingw5fe75dc2017-11-29 15:52:56 +0000158 UserManager.DISALLOW_DATA_ROAMING,
159 UserManager.DISALLOW_AIRPLANE_MODE
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800160 );
161
162 /**
Benjamin Franzff66fa92017-08-10 10:39:44 +0100163 * User restrictions that cannot be set by profile owners. Applied to all users.
164 */
165 private static final Set<String> DEVICE_OWNER_ONLY_RESTRICTIONS = Sets.newArraySet(
166 UserManager.DISALLOW_USER_SWITCH
167 );
168
169 /**
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800170 * User restrictions that can't be changed by device owner or profile owner.
171 */
172 private static final Set<String> IMMUTABLE_BY_OWNERS = Sets.newArraySet(
173 UserManager.DISALLOW_RECORD_AUDIO,
Mahaver Chopra3d9805d2016-07-07 16:25:05 +0100174 UserManager.DISALLOW_WALLPAPER,
175 UserManager.DISALLOW_OEM_UNLOCK
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800176 );
177
178 /**
179 * Special user restrictions that can be applied to a user as well as to all users globally,
180 * depending on callers. When device owner sets them, they'll be applied to all users.
181 */
182 private static final Set<String> GLOBAL_RESTRICTIONS = Sets.newArraySet(
183 UserManager.DISALLOW_ADJUST_VOLUME,
Pavel Grafov7f4ad752017-03-28 13:44:04 +0100184 UserManager.DISALLOW_BLUETOOTH_SHARING,
yuemingwa9772f362017-10-23 18:34:35 +0100185 UserManager.DISALLOW_CONFIG_DATE_TIME,
Charles He22ff6f9d2017-10-05 21:28:55 +0100186 UserManager.DISALLOW_SYSTEM_ERROR_DIALOGS,
Fyodor Kupolov9cbfc9e2015-10-07 15:52:33 -0700187 UserManager.DISALLOW_RUN_IN_BACKGROUND,
Tony Makc1205112016-07-22 16:02:59 +0100188 UserManager.DISALLOW_UNMUTE_MICROPHONE,
Esteban Talavera492b4722017-02-13 14:59:45 +0000189 UserManager.DISALLOW_UNMUTE_DEVICE
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800190 );
191
Makoto Onuki1f1ceef2016-01-28 11:32:32 -0800192 /**
Esteban Talavera6c9116a2016-11-24 16:12:44 +0000193 * User restrictions that default to {@code true} for device owners.
194 */
195 private static final Set<String> DEFAULT_ENABLED_FOR_DEVICE_OWNERS = Sets.newArraySet(
196 UserManager.DISALLOW_ADD_MANAGED_PROFILE
197 );
198
Pavel Grafov7f4ad752017-03-28 13:44:04 +0100199 /**
200 * User restrictions that default to {@code true} for managed profile owners.
201 *
202 * NB: {@link UserManager#DISALLOW_INSTALL_UNKNOWN_SOURCES} is also set by default but it is
203 * not set to existing profile owners unless they used to have INSTALL_NON_MARKET_APPS disabled
204 * in settings. So it is handled separately.
205 */
206 private static final Set<String> DEFAULT_ENABLED_FOR_MANAGED_PROFILES = Sets.newArraySet(
207 UserManager.DISALLOW_BLUETOOTH_SHARING
208 );
209
Pavel Grafovc4f87e92017-10-26 16:34:25 +0100210 /**
Pavel Grafov6a40f092016-10-25 15:46:51 +0100211 * Special user restrictions that are always applied to all users no matter who sets them.
212 */
213 private static final Set<String> PROFILE_GLOBAL_RESTRICTIONS = Sets.newArraySet(
yuemingw5fe75dc2017-11-29 15:52:56 +0000214 UserManager.ENSURE_VERIFY_APPS,
Irina Dumitrescu4638edd2018-09-05 14:08:33 +0100215 UserManager.DISALLOW_AIRPLANE_MODE,
216 UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES_GLOBALLY
Pavel Grafov6a40f092016-10-25 15:46:51 +0100217 );
218
Esteban Talavera6c9116a2016-11-24 16:12:44 +0000219 /**
Makoto Onuki1f1ceef2016-01-28 11:32:32 -0800220 * Throws {@link IllegalArgumentException} if the given restriction name is invalid.
221 */
222 public static boolean isValidRestriction(@NonNull String restriction) {
223 if (!USER_RESTRICTIONS.contains(restriction)) {
Makoto Onukiad5619d2016-02-10 13:41:15 -0800224 Slog.e(TAG, "Unknown restriction: " + restriction);
Makoto Onuki1f1ceef2016-01-28 11:32:32 -0800225 return false;
226 }
227 return true;
228 }
229
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800230 public static void writeRestrictions(@NonNull XmlSerializer serializer,
231 @Nullable Bundle restrictions, @NonNull String tag) throws IOException {
232 if (restrictions == null) {
233 return;
234 }
235
Makoto Onukia4f11972015-10-01 13:19:58 -0700236 serializer.startTag(null, tag);
Makoto Onukiac65e1e2015-11-20 15:33:17 -0800237 for (String key : restrictions.keySet()) {
238 if (NON_PERSIST_USER_RESTRICTIONS.contains(key)) {
239 continue; // Don't persist.
Makoto Onukia4f11972015-10-01 13:19:58 -0700240 }
Makoto Onukiac65e1e2015-11-20 15:33:17 -0800241 if (USER_RESTRICTIONS.contains(key)) {
242 if (restrictions.getBoolean(key)) {
243 serializer.attribute(null, key, "true");
244 }
245 continue;
246 }
247 Log.w(TAG, "Unknown user restriction detected: " + key);
Makoto Onukia4f11972015-10-01 13:19:58 -0700248 }
249 serializer.endTag(null, tag);
250 }
251
Esteban Talaverac48b20f2016-08-11 11:23:40 +0100252 public static void readRestrictions(XmlPullParser parser, Bundle restrictions) {
Fyodor Kupoloveafee022017-03-15 17:09:04 -0700253 restrictions.clear();
Makoto Onukia4f11972015-10-01 13:19:58 -0700254 for (String key : USER_RESTRICTIONS) {
255 final String value = parser.getAttributeValue(null, key);
256 if (value != null) {
257 restrictions.putBoolean(key, Boolean.parseBoolean(value));
258 }
259 }
260 }
261
Pavel Grafov6a40f092016-10-25 15:46:51 +0100262 public static Bundle readRestrictions(XmlPullParser parser) {
263 final Bundle result = new Bundle();
264 readRestrictions(parser, result);
265 return result;
266 }
267
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800268 /**
269 * @return {@code in} itself when it's not null, or an empty bundle (which can writable).
270 */
271 public static Bundle nonNull(@Nullable Bundle in) {
272 return in != null ? in : new Bundle();
273 }
274
275 public static boolean isEmpty(@Nullable Bundle in) {
276 return (in == null) || (in.size() == 0);
277 }
278
279 /**
Pavel Grafov6a40f092016-10-25 15:46:51 +0100280 * Returns {@code true} if given bundle is not null and contains {@code true} for a given
281 * restriction.
282 */
283 public static boolean contains(@Nullable Bundle in, String restriction) {
284 return in != null && in.getBoolean(restriction);
285 }
286
287 /**
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800288 * Creates a copy of the {@code in} Bundle. If {@code in} is null, it'll return an empty
289 * bundle.
290 *
291 * <p>The resulting {@link Bundle} is always writable. (i.e. it won't return
292 * {@link Bundle#EMPTY})
293 */
294 public static @NonNull Bundle clone(@Nullable Bundle in) {
295 return (in != null) ? new Bundle(in) : new Bundle();
296 }
297
298 public static void merge(@NonNull Bundle dest, @Nullable Bundle in) {
299 Preconditions.checkNotNull(dest);
300 Preconditions.checkArgument(dest != in);
Makoto Onuki068c54a2015-10-13 14:34:03 -0700301 if (in == null) {
302 return;
303 }
304 for (String key : in.keySet()) {
305 if (in.getBoolean(key, false)) {
306 dest.putBoolean(key, true);
307 }
308 }
309 }
310
Makoto Onuki4f160732015-10-27 17:15:38 -0700311 /**
Pavel Grafov6a40f092016-10-25 15:46:51 +0100312 * Merges a sparse array of restrictions bundles into one.
313 */
314 @Nullable
315 public static Bundle mergeAll(SparseArray<Bundle> restrictions) {
316 if (restrictions.size() == 0) {
317 return null;
318 } else {
319 final Bundle result = new Bundle();
320 for (int i = 0; i < restrictions.size(); i++) {
321 merge(result, restrictions.valueAt(i));
322 }
323 return result;
324 }
325 }
326
327 /**
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800328 * @return true if a restriction is settable by device owner.
329 */
330 public static boolean canDeviceOwnerChange(String restriction) {
331 return !IMMUTABLE_BY_OWNERS.contains(restriction);
332 }
333
334 /**
Makoto Onuki5485ed42015-12-09 11:38:23 -0800335 * @return true if a restriction is settable by profile owner. Note it takes a user ID because
336 * some restrictions can be changed by PO only when it's running on the system user.
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800337 */
Makoto Onuki5485ed42015-12-09 11:38:23 -0800338 public static boolean canProfileOwnerChange(String restriction, int userId) {
339 return !IMMUTABLE_BY_OWNERS.contains(restriction)
Benjamin Franzff66fa92017-08-10 10:39:44 +0100340 && !DEVICE_OWNER_ONLY_RESTRICTIONS.contains(restriction)
Makoto Onuki5485ed42015-12-09 11:38:23 -0800341 && !(userId != UserHandle.USER_SYSTEM
Pavel Grafov6a40f092016-10-25 15:46:51 +0100342 && PRIMARY_USER_ONLY_RESTRICTIONS.contains(restriction));
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800343 }
344
345 /**
Esteban Talavera6c9116a2016-11-24 16:12:44 +0000346 * Returns the user restrictions that default to {@code true} for device owners.
Nicolas Prevot2ea46fe2017-01-05 10:29:34 +0000347 * These user restrictions are local, though. ie only for the device owner's user id.
Esteban Talavera6c9116a2016-11-24 16:12:44 +0000348 */
349 public static @NonNull Set<String> getDefaultEnabledForDeviceOwner() {
350 return DEFAULT_ENABLED_FOR_DEVICE_OWNERS;
351 }
352
353 /**
Pavel Grafov7f4ad752017-03-28 13:44:04 +0100354 * Returns the user restrictions that default to {@code true} for managed profile owners.
355 */
356 public static @NonNull Set<String> getDefaultEnabledForManagedProfiles() {
357 return DEFAULT_ENABLED_FOR_MANAGED_PROFILES;
358 }
359
360 /**
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800361 * Takes restrictions that can be set by device owner, and sort them into what should be applied
362 * globally and what should be applied only on the current user.
363 */
Pavel Grafov6a40f092016-10-25 15:46:51 +0100364 public static void sortToGlobalAndLocal(@Nullable Bundle in, boolean isDeviceOwner,
365 int cameraRestrictionScope,
366 @NonNull Bundle global, @NonNull Bundle local) {
367 // Camera restriction (as well as all others) goes to at most one bundle.
368 if (cameraRestrictionScope == UserManagerInternal.CAMERA_DISABLED_GLOBALLY) {
369 global.putBoolean(UserManager.DISALLOW_CAMERA, true);
370 } else if (cameraRestrictionScope == UserManagerInternal.CAMERA_DISABLED_LOCALLY) {
371 local.putBoolean(UserManager.DISALLOW_CAMERA, true);
372 }
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800373 if (in == null || in.size() == 0) {
374 return;
375 }
376 for (String key : in.keySet()) {
377 if (!in.getBoolean(key)) {
378 continue;
379 }
Pavel Grafov6a40f092016-10-25 15:46:51 +0100380 if (isGlobal(isDeviceOwner, key)) {
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800381 global.putBoolean(key, true);
382 } else {
383 local.putBoolean(key, true);
384 }
385 }
386 }
387
388 /**
Pavel Grafov6a40f092016-10-25 15:46:51 +0100389 * Whether given user restriction should be enforced globally.
390 */
391 private static boolean isGlobal(boolean isDeviceOwner, String key) {
392 return (isDeviceOwner &&
Benjamin Franzff66fa92017-08-10 10:39:44 +0100393 (PRIMARY_USER_ONLY_RESTRICTIONS.contains(key) || GLOBAL_RESTRICTIONS.contains(key)))
394 || PROFILE_GLOBAL_RESTRICTIONS.contains(key)
395 || DEVICE_OWNER_ONLY_RESTRICTIONS.contains(key);
Pavel Grafov6a40f092016-10-25 15:46:51 +0100396 }
397
398 /**
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800399 * @return true if two Bundles contain the same user restriction.
400 * A null bundle and an empty bundle are considered to be equal.
401 */
402 public static boolean areEqual(@Nullable Bundle a, @Nullable Bundle b) {
403 if (a == b) {
404 return true;
405 }
406 if (isEmpty(a)) {
407 return isEmpty(b);
408 }
409 if (isEmpty(b)) {
410 return false;
411 }
412 for (String key : a.keySet()) {
413 if (a.getBoolean(key) != b.getBoolean(key)) {
414 return false;
415 }
416 }
417 for (String key : b.keySet()) {
418 if (a.getBoolean(key) != b.getBoolean(key)) {
419 return false;
420 }
421 }
422 return true;
423 }
424
425 /**
Makoto Onuki4f160732015-10-27 17:15:38 -0700426 * Takes a new use restriction set and the previous set, and apply the restrictions that have
427 * changed.
Makoto Onukid45a4a22015-11-02 17:17:38 -0800428 *
Fyodor Kupolov9cbfc9e2015-10-07 15:52:33 -0700429 * <p>Note this method is called by {@link UserManagerService} without holding any locks.
Makoto Onuki4f160732015-10-27 17:15:38 -0700430 */
Fyodor Kupolov9cbfc9e2015-10-07 15:52:33 -0700431 public static void applyUserRestrictions(Context context, int userId,
Makoto Onukid45a4a22015-11-02 17:17:38 -0800432 Bundle newRestrictions, Bundle prevRestrictions) {
Makoto Onuki4f160732015-10-27 17:15:38 -0700433 for (String key : USER_RESTRICTIONS) {
434 final boolean newValue = newRestrictions.getBoolean(key);
435 final boolean prevValue = prevRestrictions.getBoolean(key);
436
437 if (newValue != prevValue) {
Fyodor Kupolov9cbfc9e2015-10-07 15:52:33 -0700438 applyUserRestriction(context, userId, key, newValue);
Makoto Onuki4f160732015-10-27 17:15:38 -0700439 }
440 }
441 }
Fyodor Kupolov9cbfc9e2015-10-07 15:52:33 -0700442
Makoto Onukid45a4a22015-11-02 17:17:38 -0800443 /**
444 * Apply each user restriction.
445 *
yuemingw1d13eae2018-01-30 17:27:54 +0000446 * <p>See also {@link #isSettingRestrictedForUser()},
Makoto Onuki28da2e32015-11-20 11:30:44 -0800447 * which should be in sync with this method.
Makoto Onukid45a4a22015-11-02 17:17:38 -0800448 */
Fyodor Kupolov9cbfc9e2015-10-07 15:52:33 -0700449 private static void applyUserRestriction(Context context, int userId, String key,
Makoto Onuki4f160732015-10-27 17:15:38 -0700450 boolean newValue) {
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800451 if (UserManagerService.DBG) {
452 Log.d(TAG, "Applying user restriction: userId=" + userId
453 + " key=" + key + " value=" + newValue);
454 }
Makoto Onuki4f160732015-10-27 17:15:38 -0700455 // When certain restrictions are cleared, we don't update the system settings,
456 // because these settings are changeable on the Settings UI and we don't know the original
457 // value -- for example LOCATION_MODE might have been off already when the restriction was
458 // set, and in that case even if the restriction is lifted, changing it to ON would be
459 // wrong. So just don't do anything in such a case. If the user hopes to enable location
460 // later, they can do it on the Settings UI.
Benjamin Franz0ff13fc2016-07-12 13:42:21 +0100461 // WARNING: Remember that Settings.Global and Settings.Secure are changeable via adb.
462 // To prevent this from happening for a given user restriction, you have to add a check to
463 // SettingsProvider.isGlobalOrSecureSettingRestrictedForUser.
Makoto Onuki4f160732015-10-27 17:15:38 -0700464
465 final ContentResolver cr = context.getContentResolver();
466 final long id = Binder.clearCallingIdentity();
467 try {
468 switch (key) {
Mahaver Chopradea471e2015-12-17 11:02:37 +0000469 case UserManager.DISALLOW_DATA_ROAMING:
470 if (newValue) {
471 // DISALLOW_DATA_ROAMING user restriction is set.
472
473 // Multi sim device.
Jeff Sharkey717f52f2018-01-04 16:04:11 -0700474 SubscriptionManager subscriptionManager = context
475 .getSystemService(SubscriptionManager.class);
Mahaver Chopradea471e2015-12-17 11:02:37 +0000476 final List<SubscriptionInfo> subscriptionInfoList =
477 subscriptionManager.getActiveSubscriptionInfoList();
478 if (subscriptionInfoList != null) {
479 for (SubscriptionInfo subInfo : subscriptionInfoList) {
480 android.provider.Settings.Global.putStringForUser(cr,
481 android.provider.Settings.Global.DATA_ROAMING
482 + subInfo.getSubscriptionId(), "0", userId);
483 }
484 }
485
486 // Single sim device.
487 android.provider.Settings.Global.putStringForUser(cr,
488 android.provider.Settings.Global.DATA_ROAMING, "0", userId);
489 }
490 break;
Makoto Onuki4f160732015-10-27 17:15:38 -0700491 case UserManager.DISALLOW_SHARE_LOCATION:
492 if (newValue) {
493 android.provider.Settings.Secure.putIntForUser(cr,
494 android.provider.Settings.Secure.LOCATION_MODE,
495 android.provider.Settings.Secure.LOCATION_MODE_OFF,
496 userId);
Makoto Onuki4f160732015-10-27 17:15:38 -0700497 }
Makoto Onuki4f160732015-10-27 17:15:38 -0700498 break;
499 case UserManager.DISALLOW_DEBUGGING_FEATURES:
500 if (newValue) {
501 // Only disable adb if changing for system user, since it is global
502 // TODO: should this be admin user?
503 if (userId == UserHandle.USER_SYSTEM) {
504 android.provider.Settings.Global.putStringForUser(cr,
505 android.provider.Settings.Global.ADB_ENABLED, "0",
506 userId);
507 }
508 }
509 break;
510 case UserManager.ENSURE_VERIFY_APPS:
511 if (newValue) {
512 android.provider.Settings.Global.putStringForUser(
513 context.getContentResolver(),
514 android.provider.Settings.Global.PACKAGE_VERIFIER_ENABLE, "1",
515 userId);
516 android.provider.Settings.Global.putStringForUser(
517 context.getContentResolver(),
518 android.provider.Settings.Global.PACKAGE_VERIFIER_INCLUDE_ADB, "1",
519 userId);
520 }
521 break;
Irina Dumitrescu4638edd2018-09-05 14:08:33 +0100522 case UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES_GLOBALLY:
523 setInstallMarketAppsRestriction(cr, userId, getNewUserRestrictionSetting(
524 context, userId, UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES,
525 newValue));
526 break;
Makoto Onuki4f160732015-10-27 17:15:38 -0700527 case UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES:
Suprabh Shuklae3745ee2017-02-02 20:01:11 -0800528 // Since Android O, the secure setting is not available to be changed by the
529 // user. Hence, when the restriction is cleared, we need to reset the state of
530 // the setting to its default value which is now 1.
Irina Dumitrescu4638edd2018-09-05 14:08:33 +0100531 setInstallMarketAppsRestriction(cr, userId, getNewUserRestrictionSetting(
532 context, userId, UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES_GLOBALLY,
533 newValue));
Makoto Onuki4f160732015-10-27 17:15:38 -0700534 break;
Fyodor Kupolov9cbfc9e2015-10-07 15:52:33 -0700535 case UserManager.DISALLOW_RUN_IN_BACKGROUND:
536 if (newValue) {
537 int currentUser = ActivityManager.getCurrentUser();
538 if (currentUser != userId && userId != UserHandle.USER_SYSTEM) {
539 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -0800540 ActivityManager.getService().stopUser(userId, false, null);
Fyodor Kupolov9cbfc9e2015-10-07 15:52:33 -0700541 } catch (RemoteException e) {
542 throw e.rethrowAsRuntimeException();
543 }
544 }
545 }
Lenka Trochtova6474f0e2016-03-24 16:43:10 +0100546 break;
547 case UserManager.DISALLOW_SAFE_BOOT:
548 // Unlike with the other restrictions, we want to propagate the new value to
549 // the system settings even if it is false. The other restrictions modify
550 // settings which could be manually changed by the user from the Settings app
551 // after the policies enforcing these restrictions have been revoked, so we
552 // leave re-setting of those settings to the user.
553 android.provider.Settings.Global.putInt(
554 context.getContentResolver(),
555 android.provider.Settings.Global.SAFE_BOOT_DISALLOWED,
556 newValue ? 1 : 0);
557 break;
yuemingw265c6922018-01-11 18:31:14 +0000558 case UserManager.DISALLOW_AIRPLANE_MODE:
559 if (newValue) {
560 final boolean airplaneMode = Settings.Global.getInt(
561 context.getContentResolver(),
562 Settings.Global.AIRPLANE_MODE_ON, 0) == 1;
563 if (airplaneMode) {
564 android.provider.Settings.Global.putInt(
565 context.getContentResolver(),
566 android.provider.Settings.Global.AIRPLANE_MODE_ON, 0);
567 // Post the intent.
568 Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
yuemingwf3b97342018-03-28 15:35:40 +0100569 intent.putExtra("state", false);
yuemingw265c6922018-01-11 18:31:14 +0000570 context.sendBroadcastAsUser(intent, UserHandle.ALL);
571 }
572 }
573 break;
yuemingwc0109512018-01-21 19:21:27 +0000574 case UserManager.DISALLOW_AMBIENT_DISPLAY:
575 if (newValue) {
Alex Chau1e2b7312018-02-28 10:47:48 +0000576 android.provider.Settings.Secure.putIntForUser(
yuemingwc0109512018-01-21 19:21:27 +0000577 context.getContentResolver(),
Alex Chau1e2b7312018-02-28 10:47:48 +0000578 Settings.Secure.DOZE_ENABLED, 0, userId);
579 android.provider.Settings.Secure.putIntForUser(
yuemingwc0109512018-01-21 19:21:27 +0000580 context.getContentResolver(),
Alex Chau1e2b7312018-02-28 10:47:48 +0000581 Settings.Secure.DOZE_ALWAYS_ON, 0, userId);
582 android.provider.Settings.Secure.putIntForUser(
yuemingwc0109512018-01-21 19:21:27 +0000583 context.getContentResolver(),
Lucas Dupin4359b552018-08-09 15:07:54 -0700584 Settings.Secure.DOZE_PICK_UP_GESTURE, 0, userId);
Alex Chau1e2b7312018-02-28 10:47:48 +0000585 android.provider.Settings.Secure.putIntForUser(
yuemingwc0109512018-01-21 19:21:27 +0000586 context.getContentResolver(),
Alex Chau1e2b7312018-02-28 10:47:48 +0000587 Settings.Secure.DOZE_PULSE_ON_LONG_PRESS, 0, userId);
588 android.provider.Settings.Secure.putIntForUser(
yuemingwc0109512018-01-21 19:21:27 +0000589 context.getContentResolver(),
Lucas Dupin4359b552018-08-09 15:07:54 -0700590 Settings.Secure.DOZE_DOUBLE_TAP_GESTURE, 0, userId);
yuemingwc0109512018-01-21 19:21:27 +0000591 }
592 break;
Makoto Onukiacc50462018-02-14 14:13:49 -0800593 case UserManager.DISALLOW_CONFIG_LOCATION:
594 // When DISALLOW_CONFIG_LOCATION is set on any user, we undo the global
595 // kill switch.
596 if (newValue) {
597 android.provider.Settings.Global.putString(
598 context.getContentResolver(),
599 Global.LOCATION_GLOBAL_KILL_SWITCH, "0");
600 }
601 break;
Makoto Onuki4f160732015-10-27 17:15:38 -0700602 }
Makoto Onuki4f160732015-10-27 17:15:38 -0700603 } finally {
604 Binder.restoreCallingIdentity(id);
605 }
606 }
607
yuemingw1d13eae2018-01-30 17:27:54 +0000608 public static boolean isSettingRestrictedForUser(Context context, @NonNull String setting,
609 int userId, String value, int callingUid) {
610 Preconditions.checkNotNull(setting);
611 final UserManager mUserManager = context.getSystemService(UserManager.class);
612 String restriction;
613 boolean checkAllUser = false;
614 switch (setting) {
615 case android.provider.Settings.Secure.LOCATION_MODE:
616 if (mUserManager.hasUserRestriction(
617 UserManager.DISALLOW_CONFIG_LOCATION, UserHandle.of(userId))
618 && callingUid != Process.SYSTEM_UID) {
619 return true;
620 } else if (String.valueOf(Settings.Secure.LOCATION_MODE_OFF).equals(value)) {
621 // Note LOCATION_MODE will be converted into LOCATION_PROVIDERS_ALLOWED
622 // in android.provider.Settings.Secure.putStringForUser(), so we shouldn't come
623 // here normally, but we still protect it here from a direct provider write.
624 return false;
625 }
626 restriction = UserManager.DISALLOW_SHARE_LOCATION;
627 break;
628
629 case android.provider.Settings.Secure.LOCATION_PROVIDERS_ALLOWED:
630 if (mUserManager.hasUserRestriction(
631 UserManager.DISALLOW_CONFIG_LOCATION, UserHandle.of(userId))
632 && callingUid != Process.SYSTEM_UID) {
633 return true;
634 } else if (value != null && value.startsWith("-")) {
635 // See SettingsProvider.updateLocationProvidersAllowedLocked. "-" is to disable
636 // a provider, which should be allowed even if the user restriction is set.
637 return false;
638 }
639 restriction = UserManager.DISALLOW_SHARE_LOCATION;
640 break;
641
642 case android.provider.Settings.Secure.INSTALL_NON_MARKET_APPS:
643 if ("0".equals(value)) {
644 return false;
645 }
646 restriction = UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES;
647 break;
648
649 case android.provider.Settings.Global.ADB_ENABLED:
650 if ("0".equals(value)) {
651 return false;
652 }
653 restriction = UserManager.DISALLOW_DEBUGGING_FEATURES;
654 break;
655
656 case android.provider.Settings.Global.PACKAGE_VERIFIER_ENABLE:
657 case android.provider.Settings.Global.PACKAGE_VERIFIER_INCLUDE_ADB:
658 if ("1".equals(value)) {
659 return false;
660 }
661 restriction = UserManager.ENSURE_VERIFY_APPS;
662 break;
663
664 case android.provider.Settings.Global.PREFERRED_NETWORK_MODE:
665 restriction = UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS;
666 break;
667
668 case android.provider.Settings.Secure.ALWAYS_ON_VPN_APP:
669 case android.provider.Settings.Secure.ALWAYS_ON_VPN_LOCKDOWN:
670 // Whitelist system uid (ConnectivityService) and root uid to change always-on vpn
671 final int appId = UserHandle.getAppId(callingUid);
672 if (appId == Process.SYSTEM_UID || appId == Process.ROOT_UID) {
673 return false;
674 }
675 restriction = UserManager.DISALLOW_CONFIG_VPN;
676 break;
677
678 case android.provider.Settings.Global.SAFE_BOOT_DISALLOWED:
679 if ("1".equals(value)) {
680 return false;
681 }
682 restriction = UserManager.DISALLOW_SAFE_BOOT;
683 break;
684
685 case android.provider.Settings.Global.AIRPLANE_MODE_ON:
686 if ("0".equals(value)) {
687 return false;
688 }
689 restriction = UserManager.DISALLOW_AIRPLANE_MODE;
690 break;
691
692 case android.provider.Settings.Secure.DOZE_ENABLED:
693 case android.provider.Settings.Secure.DOZE_ALWAYS_ON:
Lucas Dupin4359b552018-08-09 15:07:54 -0700694 case android.provider.Settings.Secure.DOZE_PICK_UP_GESTURE:
yuemingw1d13eae2018-01-30 17:27:54 +0000695 case android.provider.Settings.Secure.DOZE_PULSE_ON_LONG_PRESS:
Lucas Dupin4359b552018-08-09 15:07:54 -0700696 case android.provider.Settings.Secure.DOZE_DOUBLE_TAP_GESTURE:
yuemingw1d13eae2018-01-30 17:27:54 +0000697 if ("0".equals(value)) {
698 return false;
699 }
700 restriction = UserManager.DISALLOW_AMBIENT_DISPLAY;
701 break;
702
703 case android.provider.Settings.Global.LOCATION_GLOBAL_KILL_SWITCH:
704 if ("0".equals(value)) {
705 return false;
706 }
707 restriction = UserManager.DISALLOW_CONFIG_LOCATION;
708 checkAllUser = true;
709 break;
710
711 case android.provider.Settings.System.SCREEN_BRIGHTNESS:
712 case android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE:
713 if (callingUid == Process.SYSTEM_UID) {
714 return false;
715 }
716 restriction = UserManager.DISALLOW_CONFIG_BRIGHTNESS;
717 break;
718
719 case android.provider.Settings.Global.AUTO_TIME:
720 DevicePolicyManager dpm = context.getSystemService(DevicePolicyManager.class);
721 if (dpm != null && dpm.getAutoTimeRequired()
722 && "0".equals(value)) {
723 return true;
724 } else if (callingUid == Process.SYSTEM_UID) {
725 return false;
726 }
727 restriction = UserManager.DISALLOW_CONFIG_DATE_TIME;
728 break;
729
730 case android.provider.Settings.Global.AUTO_TIME_ZONE:
731 if (callingUid == Process.SYSTEM_UID) {
732 return false;
733 }
734 restriction = UserManager.DISALLOW_CONFIG_DATE_TIME;
735 break;
736
737 case android.provider.Settings.System.SCREEN_OFF_TIMEOUT:
738 if (callingUid == Process.SYSTEM_UID) {
739 return false;
740 }
741 restriction = UserManager.DISALLOW_CONFIG_SCREEN_TIMEOUT;
742 break;
743
744 default:
745 if (setting.startsWith(Settings.Global.DATA_ROAMING)) {
746 if ("0".equals(value)) {
747 return false;
748 }
749 restriction = UserManager.DISALLOW_DATA_ROAMING;
750 break;
751 }
752 return false;
753 }
754
755 if (checkAllUser) {
756 return mUserManager.hasUserRestrictionOnAnyUser(restriction);
757 } else {
758 return mUserManager.hasUserRestriction(restriction, UserHandle.of(userId));
759 }
760 }
761
Makoto Onukia4f11972015-10-01 13:19:58 -0700762 public static void dumpRestrictions(PrintWriter pw, String prefix, Bundle restrictions) {
763 boolean noneSet = true;
764 if (restrictions != null) {
765 for (String key : restrictions.keySet()) {
766 if (restrictions.getBoolean(key, false)) {
767 pw.println(prefix + key);
768 noneSet = false;
769 }
770 }
Makoto Onuki068c54a2015-10-13 14:34:03 -0700771 if (noneSet) {
772 pw.println(prefix + "none");
773 }
774 } else {
775 pw.println(prefix + "null");
Makoto Onukia4f11972015-10-01 13:19:58 -0700776 }
777 }
Pavel Grafov6a40f092016-10-25 15:46:51 +0100778
779 /**
780 * Moves a particular restriction from one array of bundles to another, e.g. for all users.
781 */
782 public static void moveRestriction(String restrictionKey, SparseArray<Bundle> srcRestrictions,
783 SparseArray<Bundle> destRestrictions) {
784 for (int i = 0; i < srcRestrictions.size(); i++) {
Pavel Grafov7f4ad752017-03-28 13:44:04 +0100785 final int key = srcRestrictions.keyAt(i);
786 final Bundle from = srcRestrictions.valueAt(i);
Pavel Grafov6a40f092016-10-25 15:46:51 +0100787 if (contains(from, restrictionKey)) {
788 from.remove(restrictionKey);
789 Bundle to = destRestrictions.get(key);
790 if (to == null) {
791 to = new Bundle();
792 destRestrictions.append(key, to);
793 }
794 to.putBoolean(restrictionKey, true);
795 // Don't keep empty bundles.
796 if (from.isEmpty()) {
797 srcRestrictions.removeAt(i);
798 i--;
799 }
800 }
801 }
802 }
Pavel Grafov7f4ad752017-03-28 13:44:04 +0100803
804 /**
805 * Returns whether restrictions differ between two bundles.
806 * @param oldRestrictions old bundle of restrictions.
807 * @param newRestrictions new bundle of restrictions
808 * @param restrictions restrictions of interest, if empty, all restrictions are checked.
809 */
810 public static boolean restrictionsChanged(Bundle oldRestrictions, Bundle newRestrictions,
811 String... restrictions) {
812 if (restrictions.length == 0) {
813 return areEqual(oldRestrictions, newRestrictions);
814 }
815 for (final String restriction : restrictions) {
816 if (oldRestrictions.getBoolean(restriction, false) !=
817 newRestrictions.getBoolean(restriction, false)) {
818 return true;
819 }
820 }
821 return false;
822 }
Irina Dumitrescu4638edd2018-09-05 14:08:33 +0100823
824 private static void setInstallMarketAppsRestriction(ContentResolver cr, int userId,
825 int settingValue) {
826 android.provider.Settings.Secure.putIntForUser(
827 cr, android.provider.Settings.Secure.INSTALL_NON_MARKET_APPS, settingValue, userId);
828 }
829
830 private static int getNewUserRestrictionSetting(Context context, int userId,
831 String userRestriction, boolean newValue) {
832 return (newValue || UserManager.get(context).hasUserRestriction(userRestriction,
833 UserHandle.of(userId))) ? 0 : 1;
834 }
Makoto Onukia4f11972015-10-01 13:19:58 -0700835}