blob: 0b6024a84f784c112efe40546b97ffcf0b8f547c [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;
Bookatz1711b31d2019-04-02 11:11:20 -070022import android.app.AppGlobals;
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;
Bookatz1711b31d2019-04-02 11:11:20 -070026import android.content.pm.ApplicationInfo;
27import android.content.pm.IPackageManager;
28import android.content.pm.PackageManager;
Suprabh Shuklaf0f84c82020-02-02 17:52:20 -080029import android.content.pm.PackageManagerInternal;
Makoto Onuki4f160732015-10-27 17:15:38 -070030import android.os.Binder;
Makoto Onukia4f11972015-10-01 13:19:58 -070031import android.os.Bundle;
yuemingw1d13eae2018-01-30 17:27:54 +000032import android.os.Process;
Fyodor Kupolov9cbfc9e2015-10-07 15:52:33 -070033import android.os.RemoteException;
Makoto Onuki4f160732015-10-27 17:15:38 -070034import android.os.UserHandle;
Makoto Onukia4f11972015-10-01 13:19:58 -070035import android.os.UserManager;
Pavel Grafov6a40f092016-10-25 15:46:51 +010036import android.os.UserManagerInternal;
yuemingw265c6922018-01-11 18:31:14 +000037import android.provider.Settings;
Makoto Onukiacc50462018-02-14 14:13:49 -080038import android.provider.Settings.Global;
Mahaver Chopradea471e2015-12-17 11:02:37 +000039import android.telephony.SubscriptionInfo;
40import android.telephony.SubscriptionManager;
Makoto Onuki1a2cd742015-11-16 13:51:27 -080041import android.util.Log;
Makoto Onuki1f1ceef2016-01-28 11:32:32 -080042import android.util.Slog;
Pavel Grafov6a40f092016-10-25 15:46:51 +010043import android.util.SparseArray;
Makoto Onukia4f11972015-10-01 13:19:58 -070044
Pavel Grafova462bcb2019-01-25 08:50:06 +000045import com.android.internal.util.Preconditions;
Suprabh Shuklaf0f84c82020-02-02 17:52:20 -080046import com.android.server.LocalServices;
Pavel Grafova462bcb2019-01-25 08:50:06 +000047
48import com.google.android.collect.Sets;
49
Makoto Onukia4f11972015-10-01 13:19:58 -070050import org.xmlpull.v1.XmlPullParser;
51import org.xmlpull.v1.XmlSerializer;
52
53import java.io.IOException;
54import java.io.PrintWriter;
Mahaver Chopradea471e2015-12-17 11:02:37 +000055import java.util.List;
Daulet Zhanguzin82adfcb2020-01-02 17:31:40 +000056import java.util.Objects;
Makoto Onukia4f11972015-10-01 13:19:58 -070057import java.util.Set;
58
Makoto Onukid45a4a22015-11-02 17:17:38 -080059/**
Mahaver Chopradea471e2015-12-17 11:02:37 +000060 * Utility methods for user restrictions.
Makoto Onukid45a4a22015-11-02 17:17:38 -080061 *
62 * <p>See {@link UserManagerService} for the method suffixes.
63 */
Makoto Onukia4f11972015-10-01 13:19:58 -070064public class UserRestrictionsUtils {
Makoto Onuki4f160732015-10-27 17:15:38 -070065 private static final String TAG = "UserRestrictionsUtils";
66
Makoto Onukia4f11972015-10-01 13:19:58 -070067 private UserRestrictionsUtils() {
68 }
69
Makoto Onuki1f1ceef2016-01-28 11:32:32 -080070 private static Set<String> newSetWithUniqueCheck(String[] strings) {
71 final Set<String> ret = Sets.newArraySet(strings);
72
73 // Make sure there's no overlap.
74 Preconditions.checkState(ret.size() == strings.length);
75 return ret;
76 }
77
78 public static final Set<String> USER_RESTRICTIONS = newSetWithUniqueCheck(new String[] {
Makoto Onukia4f11972015-10-01 13:19:58 -070079 UserManager.DISALLOW_CONFIG_WIFI,
Christine Franks1bade5d2017-10-10 15:41:50 -070080 UserManager.DISALLOW_CONFIG_LOCALE,
Makoto Onukia4f11972015-10-01 13:19:58 -070081 UserManager.DISALLOW_MODIFY_ACCOUNTS,
82 UserManager.DISALLOW_INSTALL_APPS,
83 UserManager.DISALLOW_UNINSTALL_APPS,
84 UserManager.DISALLOW_SHARE_LOCATION,
85 UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES,
Irina Dumitrescu4638edd2018-09-05 14:08:33 +010086 UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES_GLOBALLY,
Makoto Onukia4f11972015-10-01 13:19:58 -070087 UserManager.DISALLOW_CONFIG_BLUETOOTH,
Lenka Trochtova63d5e4a72016-12-02 12:19:39 +010088 UserManager.DISALLOW_BLUETOOTH,
Pavel Grafov7f4ad752017-03-28 13:44:04 +010089 UserManager.DISALLOW_BLUETOOTH_SHARING,
Makoto Onukia4f11972015-10-01 13:19:58 -070090 UserManager.DISALLOW_USB_FILE_TRANSFER,
91 UserManager.DISALLOW_CONFIG_CREDENTIALS,
92 UserManager.DISALLOW_REMOVE_USER,
Esteban Talavera6c9116a2016-11-24 16:12:44 +000093 UserManager.DISALLOW_REMOVE_MANAGED_PROFILE,
Makoto Onukia4f11972015-10-01 13:19:58 -070094 UserManager.DISALLOW_DEBUGGING_FEATURES,
95 UserManager.DISALLOW_CONFIG_VPN,
yuemingwa9772f362017-10-23 18:34:35 +010096 UserManager.DISALLOW_CONFIG_DATE_TIME,
Makoto Onukia4f11972015-10-01 13:19:58 -070097 UserManager.DISALLOW_CONFIG_TETHERING,
98 UserManager.DISALLOW_NETWORK_RESET,
99 UserManager.DISALLOW_FACTORY_RESET,
100 UserManager.DISALLOW_ADD_USER,
Esteban Talavera6c9116a2016-11-24 16:12:44 +0000101 UserManager.DISALLOW_ADD_MANAGED_PROFILE,
Makoto Onukia4f11972015-10-01 13:19:58 -0700102 UserManager.ENSURE_VERIFY_APPS,
103 UserManager.DISALLOW_CONFIG_CELL_BROADCASTS,
104 UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS,
105 UserManager.DISALLOW_APPS_CONTROL,
106 UserManager.DISALLOW_MOUNT_PHYSICAL_MEDIA,
107 UserManager.DISALLOW_UNMUTE_MICROPHONE,
108 UserManager.DISALLOW_ADJUST_VOLUME,
109 UserManager.DISALLOW_OUTGOING_CALLS,
110 UserManager.DISALLOW_SMS,
111 UserManager.DISALLOW_FUN,
112 UserManager.DISALLOW_CREATE_WINDOWS,
Charles He22ff6f9d2017-10-05 21:28:55 +0100113 UserManager.DISALLOW_SYSTEM_ERROR_DIALOGS,
Makoto Onukia4f11972015-10-01 13:19:58 -0700114 UserManager.DISALLOW_CROSS_PROFILE_COPY_PASTE,
115 UserManager.DISALLOW_OUTGOING_BEAM,
116 UserManager.DISALLOW_WALLPAPER,
117 UserManager.DISALLOW_SAFE_BOOT,
118 UserManager.ALLOW_PARENT_PROFILE_APP_LINKING,
119 UserManager.DISALLOW_RECORD_AUDIO,
Fyodor Kupolov9cbfc9e2015-10-07 15:52:33 -0700120 UserManager.DISALLOW_CAMERA,
Mahaver Chopradea471e2015-12-17 11:02:37 +0000121 UserManager.DISALLOW_RUN_IN_BACKGROUND,
Oleksandr Peletskyi7f1f1df2016-01-18 15:40:21 +0100122 UserManager.DISALLOW_DATA_ROAMING,
Oleksandr Peletskyif2519812016-01-26 20:16:06 +0100123 UserManager.DISALLOW_SET_USER_ICON,
Mahaver Chopra3d9805d2016-07-07 16:25:05 +0100124 UserManager.DISALLOW_SET_WALLPAPER,
Tony Makc1205112016-07-22 16:02:59 +0100125 UserManager.DISALLOW_OEM_UNLOCK,
Esteban Talavera492b4722017-02-13 14:59:45 +0000126 UserManager.DISALLOW_UNMUTE_DEVICE,
Felipe Leme24d58932017-03-21 14:13:58 -0700127 UserManager.DISALLOW_AUTOFILL,
Felipe Leme08c92022019-02-08 14:19:55 -0800128 UserManager.DISALLOW_CONTENT_CAPTURE,
Zak Cohen3e12ac72019-03-05 16:50:39 -0800129 UserManager.DISALLOW_CONTENT_SUGGESTIONS,
Pavel Grafovc4f87e92017-10-26 16:34:25 +0100130 UserManager.DISALLOW_USER_SWITCH,
131 UserManager.DISALLOW_UNIFIED_PASSWORD,
yuemingw7810b8b2018-02-01 17:32:25 +0000132 UserManager.DISALLOW_CONFIG_LOCATION,
yuemingwc6ac29d2018-01-10 16:54:08 +0000133 UserManager.DISALLOW_AIRPLANE_MODE,
Rubin Xucc391c22018-01-02 20:37:35 +0000134 UserManager.DISALLOW_CONFIG_BRIGHTNESS,
135 UserManager.DISALLOW_SHARE_INTO_MANAGED_PROFILE,
yuemingw5cda3ae2018-01-15 10:26:19 +0000136 UserManager.DISALLOW_AMBIENT_DISPLAY,
Vladislav Kuzkokov622b9f92018-01-25 16:33:05 +0100137 UserManager.DISALLOW_CONFIG_SCREEN_TIMEOUT,
Eran Messeri09b122da2018-10-05 15:33:53 +0100138 UserManager.DISALLOW_PRINTING,
139 UserManager.DISALLOW_CONFIG_PRIVATE_DNS
Makoto Onuki1f1ceef2016-01-28 11:32:32 -0800140 });
Makoto Onukia4f11972015-10-01 13:19:58 -0700141
142 /**
143 * Set of user restriction which we don't want to persist.
144 */
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800145 private static final Set<String> NON_PERSIST_USER_RESTRICTIONS = Sets.newArraySet(
146 UserManager.DISALLOW_RECORD_AUDIO
147 );
Makoto Onukia4f11972015-10-01 13:19:58 -0700148
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800149 /**
Pavel Grafov6a40f092016-10-25 15:46:51 +0100150 * User restrictions that cannot be set by profile owners of secondary users. When set by DO
151 * they will be applied to all users.
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800152 */
Pavel Grafov6a40f092016-10-25 15:46:51 +0100153 private static final Set<String> PRIMARY_USER_ONLY_RESTRICTIONS = Sets.newArraySet(
Lenka Trochtova63d5e4a72016-12-02 12:19:39 +0100154 UserManager.DISALLOW_BLUETOOTH,
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800155 UserManager.DISALLOW_USB_FILE_TRANSFER,
156 UserManager.DISALLOW_CONFIG_TETHERING,
157 UserManager.DISALLOW_NETWORK_RESET,
158 UserManager.DISALLOW_FACTORY_RESET,
159 UserManager.DISALLOW_ADD_USER,
160 UserManager.DISALLOW_CONFIG_CELL_BROADCASTS,
161 UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS,
162 UserManager.DISALLOW_MOUNT_PHYSICAL_MEDIA,
163 UserManager.DISALLOW_SMS,
164 UserManager.DISALLOW_FUN,
165 UserManager.DISALLOW_SAFE_BOOT,
Mahaver Chopradea471e2015-12-17 11:02:37 +0000166 UserManager.DISALLOW_CREATE_WINDOWS,
yuemingw5fe75dc2017-11-29 15:52:56 +0000167 UserManager.DISALLOW_DATA_ROAMING,
168 UserManager.DISALLOW_AIRPLANE_MODE
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800169 );
170
171 /**
Benjamin Franzff66fa92017-08-10 10:39:44 +0100172 * User restrictions that cannot be set by profile owners. Applied to all users.
173 */
174 private static final Set<String> DEVICE_OWNER_ONLY_RESTRICTIONS = Sets.newArraySet(
Eran Messeri09b122da2018-10-05 15:33:53 +0100175 UserManager.DISALLOW_USER_SWITCH,
176 UserManager.DISALLOW_CONFIG_PRIVATE_DNS
Benjamin Franzff66fa92017-08-10 10:39:44 +0100177 );
178
179 /**
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800180 * User restrictions that can't be changed by device owner or profile owner.
181 */
182 private static final Set<String> IMMUTABLE_BY_OWNERS = Sets.newArraySet(
183 UserManager.DISALLOW_RECORD_AUDIO,
Mahaver Chopra3d9805d2016-07-07 16:25:05 +0100184 UserManager.DISALLOW_WALLPAPER,
185 UserManager.DISALLOW_OEM_UNLOCK
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800186 );
187
188 /**
189 * Special user restrictions that can be applied to a user as well as to all users globally,
190 * depending on callers. When device owner sets them, they'll be applied to all users.
191 */
192 private static final Set<String> GLOBAL_RESTRICTIONS = Sets.newArraySet(
193 UserManager.DISALLOW_ADJUST_VOLUME,
Pavel Grafov7f4ad752017-03-28 13:44:04 +0100194 UserManager.DISALLOW_BLUETOOTH_SHARING,
yuemingwa9772f362017-10-23 18:34:35 +0100195 UserManager.DISALLOW_CONFIG_DATE_TIME,
Charles He22ff6f9d2017-10-05 21:28:55 +0100196 UserManager.DISALLOW_SYSTEM_ERROR_DIALOGS,
Fyodor Kupolov9cbfc9e2015-10-07 15:52:33 -0700197 UserManager.DISALLOW_RUN_IN_BACKGROUND,
Tony Makc1205112016-07-22 16:02:59 +0100198 UserManager.DISALLOW_UNMUTE_MICROPHONE,
Alex Johnstonece5cdb2019-11-20 14:37:13 +0000199 UserManager.DISALLOW_UNMUTE_DEVICE,
200 UserManager.DISALLOW_CAMERA
201 );
202
203 /**
Alex Johnston7c3d7e22020-01-08 12:57:14 +0000204 * Special user restrictions that profile owner of an organization-owned managed profile can
205 * set on the parent profile instance to apply them globally.
Alex Johnstonece5cdb2019-11-20 14:37:13 +0000206 */
207 private static final Set<String> PROFILE_OWNER_ORGANIZATION_OWNED_GLOBAL_RESTRICTIONS =
208 Sets.newArraySet(
209 UserManager.DISALLOW_CONFIG_DATE_TIME,
Alex Johnston7c3d7e22020-01-08 12:57:14 +0000210 UserManager.DISALLOW_CAMERA,
211 UserManager.DISALLOW_ADD_USER,
212 UserManager.DISALLOW_BLUETOOTH,
213 UserManager.DISALLOW_BLUETOOTH_SHARING,
Alex Johnston7c3d7e22020-01-08 12:57:14 +0000214 UserManager.DISALLOW_CONFIG_CELL_BROADCASTS,
Alex Johnston7c3d7e22020-01-08 12:57:14 +0000215 UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS,
216 UserManager.DISALLOW_CONFIG_PRIVATE_DNS,
217 UserManager.DISALLOW_CONFIG_TETHERING,
Alex Johnston7c3d7e22020-01-08 12:57:14 +0000218 UserManager.DISALLOW_DATA_ROAMING,
Alex Johnston7c3d7e22020-01-08 12:57:14 +0000219 UserManager.DISALLOW_SAFE_BOOT,
Alex Johnston7c3d7e22020-01-08 12:57:14 +0000220 UserManager.DISALLOW_SMS,
Alex Johnston9722abe2020-01-28 16:15:55 +0000221 UserManager.DISALLOW_USB_FILE_TRANSFER,
222 UserManager.DISALLOW_AIRPLANE_MODE,
223 UserManager.DISALLOW_MOUNT_PHYSICAL_MEDIA,
Alex Johnston9722abe2020-01-28 16:15:55 +0000224 UserManager.DISALLOW_UNMUTE_MICROPHONE
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800225 );
226
Makoto Onuki1f1ceef2016-01-28 11:32:32 -0800227 /**
Alex Johnstonec6c3d32020-03-09 16:19:36 +0000228 * Special user restrictions that profile owner of an organization-owned managed profile can
229 * set on the parent profile instance to apply them on the personal profile.
230 */
231 private static final Set<String> PROFILE_OWNER_ORGANIZATION_OWNED_LOCAL_RESTRICTIONS =
Alex Johnstonac42b162020-04-15 17:05:51 +0100232 Sets.newArraySet(
233 UserManager.DISALLOW_CONFIG_BLUETOOTH,
234 UserManager.DISALLOW_CONFIG_LOCATION,
235 UserManager.DISALLOW_CONFIG_WIFI,
236 UserManager.DISALLOW_CONTENT_CAPTURE,
237 UserManager.DISALLOW_CONTENT_SUGGESTIONS,
238 UserManager.DISALLOW_DEBUGGING_FEATURES,
239 UserManager.DISALLOW_SHARE_LOCATION,
240 UserManager.DISALLOW_OUTGOING_CALLS
241 );
Alex Johnstonec6c3d32020-03-09 16:19:36 +0000242
243 /**
Pavel Grafov7f4ad752017-03-28 13:44:04 +0100244 * User restrictions that default to {@code true} for managed profile owners.
245 *
246 * NB: {@link UserManager#DISALLOW_INSTALL_UNKNOWN_SOURCES} is also set by default but it is
247 * not set to existing profile owners unless they used to have INSTALL_NON_MARKET_APPS disabled
248 * in settings. So it is handled separately.
249 */
250 private static final Set<String> DEFAULT_ENABLED_FOR_MANAGED_PROFILES = Sets.newArraySet(
251 UserManager.DISALLOW_BLUETOOTH_SHARING
252 );
253
Pavel Grafovc4f87e92017-10-26 16:34:25 +0100254 /**
Pavel Grafov6a40f092016-10-25 15:46:51 +0100255 * Special user restrictions that are always applied to all users no matter who sets them.
256 */
257 private static final Set<String> PROFILE_GLOBAL_RESTRICTIONS = Sets.newArraySet(
yuemingw5fe75dc2017-11-29 15:52:56 +0000258 UserManager.ENSURE_VERIFY_APPS,
Irina Dumitrescu4638edd2018-09-05 14:08:33 +0100259 UserManager.DISALLOW_AIRPLANE_MODE,
260 UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES_GLOBALLY
Pavel Grafov6a40f092016-10-25 15:46:51 +0100261 );
262
Esteban Talavera6c9116a2016-11-24 16:12:44 +0000263 /**
Bookatz1711b31d2019-04-02 11:11:20 -0700264 * Returns whether the given restriction name is valid (and logs it if it isn't).
Makoto Onuki1f1ceef2016-01-28 11:32:32 -0800265 */
266 public static boolean isValidRestriction(@NonNull String restriction) {
267 if (!USER_RESTRICTIONS.contains(restriction)) {
Bookatz1711b31d2019-04-02 11:11:20 -0700268 // Log this, with severity depending on the source.
269 final int uid = Binder.getCallingUid();
270 String[] pkgs = null;
271 try {
272 pkgs = AppGlobals.getPackageManager().getPackagesForUid(uid);
273 } catch (RemoteException e) {
274 // Ignore
275 }
276 StringBuilder msg = new StringBuilder("Unknown restriction queried by uid ");
277 msg.append(uid);
278 if (pkgs != null && pkgs.length > 0) {
279 msg.append(" (");
280 msg.append(pkgs[0]);
281 if (pkgs.length > 1) {
282 msg.append(" et al");
283 }
284 msg.append(")");
285 }
286 msg.append(": ");
287 msg.append(restriction);
288 if (restriction != null && isSystemApp(uid, pkgs)) {
289 Slog.wtf(TAG, msg.toString());
290 } else {
291 Slog.e(TAG, msg.toString());
292 }
Makoto Onuki1f1ceef2016-01-28 11:32:32 -0800293 return false;
294 }
295 return true;
296 }
297
Bookatz1711b31d2019-04-02 11:11:20 -0700298 /** Returns whether the given uid (or corresponding packageList) is for a System app. */
299 private static boolean isSystemApp(int uid, String[] packageList) {
300 if (UserHandle.isCore(uid)) {
301 return true;
302 }
303 if (packageList == null) {
304 return false;
305 }
306 final IPackageManager pm = AppGlobals.getPackageManager();
307 for (int i = 0; i < packageList.length; i++) {
308 try {
309 final int flags = PackageManager.MATCH_UNINSTALLED_PACKAGES
310 | PackageManager.MATCH_DIRECT_BOOT_AWARE
311 | PackageManager.MATCH_DIRECT_BOOT_UNAWARE;
312 final ApplicationInfo appInfo =
313 pm.getApplicationInfo(packageList[i], flags, UserHandle.getUserId(uid));
314 if (appInfo != null && appInfo.isSystemApp()) {
315 return true;
316 }
317 } catch (RemoteException e) {
318 // Ignore
319 }
320 }
321 return false;
322 }
323
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800324 public static void writeRestrictions(@NonNull XmlSerializer serializer,
325 @Nullable Bundle restrictions, @NonNull String tag) throws IOException {
326 if (restrictions == null) {
327 return;
328 }
329
Makoto Onukia4f11972015-10-01 13:19:58 -0700330 serializer.startTag(null, tag);
Makoto Onukiac65e1e2015-11-20 15:33:17 -0800331 for (String key : restrictions.keySet()) {
332 if (NON_PERSIST_USER_RESTRICTIONS.contains(key)) {
333 continue; // Don't persist.
Makoto Onukia4f11972015-10-01 13:19:58 -0700334 }
Makoto Onukiac65e1e2015-11-20 15:33:17 -0800335 if (USER_RESTRICTIONS.contains(key)) {
336 if (restrictions.getBoolean(key)) {
337 serializer.attribute(null, key, "true");
338 }
339 continue;
340 }
341 Log.w(TAG, "Unknown user restriction detected: " + key);
Makoto Onukia4f11972015-10-01 13:19:58 -0700342 }
343 serializer.endTag(null, tag);
344 }
345
Esteban Talaverac48b20f2016-08-11 11:23:40 +0100346 public static void readRestrictions(XmlPullParser parser, Bundle restrictions) {
Fyodor Kupoloveafee022017-03-15 17:09:04 -0700347 restrictions.clear();
Makoto Onukia4f11972015-10-01 13:19:58 -0700348 for (String key : USER_RESTRICTIONS) {
349 final String value = parser.getAttributeValue(null, key);
350 if (value != null) {
351 restrictions.putBoolean(key, Boolean.parseBoolean(value));
352 }
353 }
354 }
355
Pavel Grafov6a40f092016-10-25 15:46:51 +0100356 public static Bundle readRestrictions(XmlPullParser parser) {
357 final Bundle result = new Bundle();
358 readRestrictions(parser, result);
359 return result;
360 }
361
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800362 /**
363 * @return {@code in} itself when it's not null, or an empty bundle (which can writable).
364 */
365 public static Bundle nonNull(@Nullable Bundle in) {
366 return in != null ? in : new Bundle();
367 }
368
369 public static boolean isEmpty(@Nullable Bundle in) {
370 return (in == null) || (in.size() == 0);
371 }
372
373 /**
Pavel Grafov6a40f092016-10-25 15:46:51 +0100374 * Returns {@code true} if given bundle is not null and contains {@code true} for a given
375 * restriction.
376 */
377 public static boolean contains(@Nullable Bundle in, String restriction) {
378 return in != null && in.getBoolean(restriction);
379 }
380
381 /**
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800382 * Creates a copy of the {@code in} Bundle. If {@code in} is null, it'll return an empty
383 * bundle.
384 *
385 * <p>The resulting {@link Bundle} is always writable. (i.e. it won't return
386 * {@link Bundle#EMPTY})
387 */
388 public static @NonNull Bundle clone(@Nullable Bundle in) {
389 return (in != null) ? new Bundle(in) : new Bundle();
390 }
391
392 public static void merge(@NonNull Bundle dest, @Nullable Bundle in) {
Daulet Zhanguzin82adfcb2020-01-02 17:31:40 +0000393 Objects.requireNonNull(dest);
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800394 Preconditions.checkArgument(dest != in);
Makoto Onuki068c54a2015-10-13 14:34:03 -0700395 if (in == null) {
396 return;
397 }
398 for (String key : in.keySet()) {
399 if (in.getBoolean(key, false)) {
400 dest.putBoolean(key, true);
401 }
402 }
403 }
404
Makoto Onuki4f160732015-10-27 17:15:38 -0700405 /**
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800406 * @return true if a restriction is settable by device owner.
407 */
408 public static boolean canDeviceOwnerChange(String restriction) {
409 return !IMMUTABLE_BY_OWNERS.contains(restriction);
410 }
411
412 /**
Makoto Onuki5485ed42015-12-09 11:38:23 -0800413 * @return true if a restriction is settable by profile owner. Note it takes a user ID because
414 * some restrictions can be changed by PO only when it's running on the system user.
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800415 */
Makoto Onuki5485ed42015-12-09 11:38:23 -0800416 public static boolean canProfileOwnerChange(String restriction, int userId) {
417 return !IMMUTABLE_BY_OWNERS.contains(restriction)
Benjamin Franzff66fa92017-08-10 10:39:44 +0100418 && !DEVICE_OWNER_ONLY_RESTRICTIONS.contains(restriction)
Makoto Onuki5485ed42015-12-09 11:38:23 -0800419 && !(userId != UserHandle.USER_SYSTEM
Pavel Grafov6a40f092016-10-25 15:46:51 +0100420 && PRIMARY_USER_ONLY_RESTRICTIONS.contains(restriction));
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800421 }
422
423 /**
Alex Johnston51419382019-11-26 17:00:10 +0000424 * @return true if a restriction is settable by profile owner of an organization owned device.
425 */
426 public static boolean canProfileOwnerOfOrganizationOwnedDeviceChange(String restriction) {
Alex Johnstonec6c3d32020-03-09 16:19:36 +0000427 return PROFILE_OWNER_ORGANIZATION_OWNED_GLOBAL_RESTRICTIONS.contains(restriction)
428 || PROFILE_OWNER_ORGANIZATION_OWNED_LOCAL_RESTRICTIONS.contains(restriction);
Alex Johnston51419382019-11-26 17:00:10 +0000429 }
430
431 /**
Pavel Grafov7f4ad752017-03-28 13:44:04 +0100432 * Returns the user restrictions that default to {@code true} for managed profile owners.
433 */
434 public static @NonNull Set<String> getDefaultEnabledForManagedProfiles() {
435 return DEFAULT_ENABLED_FOR_MANAGED_PROFILES;
436 }
437
438 /**
Pavel Grafov6a40f092016-10-25 15:46:51 +0100439 * Whether given user restriction should be enforced globally.
440 */
Alex Johnstonec6c3d32020-03-09 16:19:36 +0000441 public static boolean isGlobal(@UserManagerInternal.OwnerType int restrictionOwnerType,
Alex Johnstonece5cdb2019-11-20 14:37:13 +0000442 String key) {
443 return ((restrictionOwnerType == UserManagerInternal.OWNER_TYPE_DEVICE_OWNER) && (
444 PRIMARY_USER_ONLY_RESTRICTIONS.contains(key) || GLOBAL_RESTRICTIONS.contains(key)))
445 || ((restrictionOwnerType
446 == UserManagerInternal.OWNER_TYPE_PROFILE_OWNER_OF_ORGANIZATION_OWNED_DEVICE)
447 && PROFILE_OWNER_ORGANIZATION_OWNED_GLOBAL_RESTRICTIONS.contains(key))
Benjamin Franzff66fa92017-08-10 10:39:44 +0100448 || PROFILE_GLOBAL_RESTRICTIONS.contains(key)
449 || DEVICE_OWNER_ONLY_RESTRICTIONS.contains(key);
Pavel Grafov6a40f092016-10-25 15:46:51 +0100450 }
451
452 /**
Alex Johnstonec6c3d32020-03-09 16:19:36 +0000453 * Whether given user restriction should be enforced locally.
454 */
455 public static boolean isLocal(@UserManagerInternal.OwnerType int restrictionOwnerType,
456 String key) {
457 return !isGlobal(restrictionOwnerType, key);
458 }
459
460 /**
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800461 * @return true if two Bundles contain the same user restriction.
462 * A null bundle and an empty bundle are considered to be equal.
463 */
464 public static boolean areEqual(@Nullable Bundle a, @Nullable Bundle b) {
465 if (a == b) {
466 return true;
467 }
468 if (isEmpty(a)) {
469 return isEmpty(b);
470 }
471 if (isEmpty(b)) {
472 return false;
473 }
474 for (String key : a.keySet()) {
475 if (a.getBoolean(key) != b.getBoolean(key)) {
476 return false;
477 }
478 }
479 for (String key : b.keySet()) {
480 if (a.getBoolean(key) != b.getBoolean(key)) {
481 return false;
482 }
483 }
484 return true;
485 }
486
487 /**
Makoto Onuki4f160732015-10-27 17:15:38 -0700488 * Takes a new use restriction set and the previous set, and apply the restrictions that have
489 * changed.
Makoto Onukid45a4a22015-11-02 17:17:38 -0800490 *
Fyodor Kupolov9cbfc9e2015-10-07 15:52:33 -0700491 * <p>Note this method is called by {@link UserManagerService} without holding any locks.
Makoto Onuki4f160732015-10-27 17:15:38 -0700492 */
Fyodor Kupolov9cbfc9e2015-10-07 15:52:33 -0700493 public static void applyUserRestrictions(Context context, int userId,
Makoto Onukid45a4a22015-11-02 17:17:38 -0800494 Bundle newRestrictions, Bundle prevRestrictions) {
Makoto Onuki4f160732015-10-27 17:15:38 -0700495 for (String key : USER_RESTRICTIONS) {
496 final boolean newValue = newRestrictions.getBoolean(key);
497 final boolean prevValue = prevRestrictions.getBoolean(key);
498
499 if (newValue != prevValue) {
Fyodor Kupolov9cbfc9e2015-10-07 15:52:33 -0700500 applyUserRestriction(context, userId, key, newValue);
Makoto Onuki4f160732015-10-27 17:15:38 -0700501 }
502 }
503 }
Fyodor Kupolov9cbfc9e2015-10-07 15:52:33 -0700504
Makoto Onukid45a4a22015-11-02 17:17:38 -0800505 /**
506 * Apply each user restriction.
507 *
yuemingw1d13eae2018-01-30 17:27:54 +0000508 * <p>See also {@link #isSettingRestrictedForUser()},
Makoto Onuki28da2e32015-11-20 11:30:44 -0800509 * which should be in sync with this method.
Makoto Onukid45a4a22015-11-02 17:17:38 -0800510 */
Fyodor Kupolov9cbfc9e2015-10-07 15:52:33 -0700511 private static void applyUserRestriction(Context context, int userId, String key,
Makoto Onuki4f160732015-10-27 17:15:38 -0700512 boolean newValue) {
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800513 if (UserManagerService.DBG) {
514 Log.d(TAG, "Applying user restriction: userId=" + userId
515 + " key=" + key + " value=" + newValue);
516 }
Makoto Onuki4f160732015-10-27 17:15:38 -0700517 // When certain restrictions are cleared, we don't update the system settings,
518 // because these settings are changeable on the Settings UI and we don't know the original
519 // value -- for example LOCATION_MODE might have been off already when the restriction was
520 // set, and in that case even if the restriction is lifted, changing it to ON would be
521 // wrong. So just don't do anything in such a case. If the user hopes to enable location
522 // later, they can do it on the Settings UI.
Benjamin Franz0ff13fc2016-07-12 13:42:21 +0100523 // WARNING: Remember that Settings.Global and Settings.Secure are changeable via adb.
524 // To prevent this from happening for a given user restriction, you have to add a check to
525 // SettingsProvider.isGlobalOrSecureSettingRestrictedForUser.
Makoto Onuki4f160732015-10-27 17:15:38 -0700526
527 final ContentResolver cr = context.getContentResolver();
528 final long id = Binder.clearCallingIdentity();
529 try {
530 switch (key) {
Mahaver Chopradea471e2015-12-17 11:02:37 +0000531 case UserManager.DISALLOW_DATA_ROAMING:
532 if (newValue) {
533 // DISALLOW_DATA_ROAMING user restriction is set.
534
535 // Multi sim device.
Jeff Sharkey717f52f2018-01-04 16:04:11 -0700536 SubscriptionManager subscriptionManager = context
537 .getSystemService(SubscriptionManager.class);
Mahaver Chopradea471e2015-12-17 11:02:37 +0000538 final List<SubscriptionInfo> subscriptionInfoList =
539 subscriptionManager.getActiveSubscriptionInfoList();
540 if (subscriptionInfoList != null) {
541 for (SubscriptionInfo subInfo : subscriptionInfoList) {
542 android.provider.Settings.Global.putStringForUser(cr,
543 android.provider.Settings.Global.DATA_ROAMING
544 + subInfo.getSubscriptionId(), "0", userId);
545 }
546 }
547
548 // Single sim device.
549 android.provider.Settings.Global.putStringForUser(cr,
550 android.provider.Settings.Global.DATA_ROAMING, "0", userId);
551 }
552 break;
Makoto Onuki4f160732015-10-27 17:15:38 -0700553 case UserManager.DISALLOW_SHARE_LOCATION:
554 if (newValue) {
555 android.provider.Settings.Secure.putIntForUser(cr,
556 android.provider.Settings.Secure.LOCATION_MODE,
557 android.provider.Settings.Secure.LOCATION_MODE_OFF,
558 userId);
Makoto Onuki4f160732015-10-27 17:15:38 -0700559 }
Makoto Onuki4f160732015-10-27 17:15:38 -0700560 break;
561 case UserManager.DISALLOW_DEBUGGING_FEATURES:
562 if (newValue) {
563 // Only disable adb if changing for system user, since it is global
564 // TODO: should this be admin user?
565 if (userId == UserHandle.USER_SYSTEM) {
566 android.provider.Settings.Global.putStringForUser(cr,
567 android.provider.Settings.Global.ADB_ENABLED, "0",
568 userId);
Joshua Duong2076c042020-01-02 14:53:32 -0800569 android.provider.Settings.Global.putStringForUser(cr,
570 android.provider.Settings.Global.ADB_WIFI_ENABLED, "0",
571 userId);
Makoto Onuki4f160732015-10-27 17:15:38 -0700572 }
573 }
574 break;
575 case UserManager.ENSURE_VERIFY_APPS:
576 if (newValue) {
577 android.provider.Settings.Global.putStringForUser(
578 context.getContentResolver(),
Makoto Onuki4f160732015-10-27 17:15:38 -0700579 android.provider.Settings.Global.PACKAGE_VERIFIER_INCLUDE_ADB, "1",
580 userId);
581 }
582 break;
Irina Dumitrescu4638edd2018-09-05 14:08:33 +0100583 case UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES_GLOBALLY:
584 setInstallMarketAppsRestriction(cr, userId, getNewUserRestrictionSetting(
585 context, userId, UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES,
586 newValue));
587 break;
Makoto Onuki4f160732015-10-27 17:15:38 -0700588 case UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES:
Suprabh Shuklae3745ee2017-02-02 20:01:11 -0800589 // Since Android O, the secure setting is not available to be changed by the
590 // user. Hence, when the restriction is cleared, we need to reset the state of
591 // the setting to its default value which is now 1.
Irina Dumitrescu4638edd2018-09-05 14:08:33 +0100592 setInstallMarketAppsRestriction(cr, userId, getNewUserRestrictionSetting(
593 context, userId, UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES_GLOBALLY,
594 newValue));
Makoto Onuki4f160732015-10-27 17:15:38 -0700595 break;
Fyodor Kupolov9cbfc9e2015-10-07 15:52:33 -0700596 case UserManager.DISALLOW_RUN_IN_BACKGROUND:
597 if (newValue) {
598 int currentUser = ActivityManager.getCurrentUser();
599 if (currentUser != userId && userId != UserHandle.USER_SYSTEM) {
600 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -0800601 ActivityManager.getService().stopUser(userId, false, null);
Fyodor Kupolov9cbfc9e2015-10-07 15:52:33 -0700602 } catch (RemoteException e) {
603 throw e.rethrowAsRuntimeException();
604 }
605 }
606 }
Lenka Trochtova6474f0e2016-03-24 16:43:10 +0100607 break;
608 case UserManager.DISALLOW_SAFE_BOOT:
609 // Unlike with the other restrictions, we want to propagate the new value to
610 // the system settings even if it is false. The other restrictions modify
611 // settings which could be manually changed by the user from the Settings app
612 // after the policies enforcing these restrictions have been revoked, so we
613 // leave re-setting of those settings to the user.
614 android.provider.Settings.Global.putInt(
615 context.getContentResolver(),
616 android.provider.Settings.Global.SAFE_BOOT_DISALLOWED,
617 newValue ? 1 : 0);
618 break;
yuemingw265c6922018-01-11 18:31:14 +0000619 case UserManager.DISALLOW_AIRPLANE_MODE:
620 if (newValue) {
621 final boolean airplaneMode = Settings.Global.getInt(
622 context.getContentResolver(),
623 Settings.Global.AIRPLANE_MODE_ON, 0) == 1;
624 if (airplaneMode) {
625 android.provider.Settings.Global.putInt(
626 context.getContentResolver(),
627 android.provider.Settings.Global.AIRPLANE_MODE_ON, 0);
628 // Post the intent.
629 Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
yuemingwf3b97342018-03-28 15:35:40 +0100630 intent.putExtra("state", false);
yuemingw265c6922018-01-11 18:31:14 +0000631 context.sendBroadcastAsUser(intent, UserHandle.ALL);
632 }
633 }
634 break;
yuemingwc0109512018-01-21 19:21:27 +0000635 case UserManager.DISALLOW_AMBIENT_DISPLAY:
636 if (newValue) {
Alex Chau1e2b7312018-02-28 10:47:48 +0000637 android.provider.Settings.Secure.putIntForUser(
yuemingwc0109512018-01-21 19:21:27 +0000638 context.getContentResolver(),
Alex Chau1e2b7312018-02-28 10:47:48 +0000639 Settings.Secure.DOZE_ENABLED, 0, userId);
640 android.provider.Settings.Secure.putIntForUser(
yuemingwc0109512018-01-21 19:21:27 +0000641 context.getContentResolver(),
Alex Chau1e2b7312018-02-28 10:47:48 +0000642 Settings.Secure.DOZE_ALWAYS_ON, 0, userId);
643 android.provider.Settings.Secure.putIntForUser(
yuemingwc0109512018-01-21 19:21:27 +0000644 context.getContentResolver(),
Lucas Dupin4359b552018-08-09 15:07:54 -0700645 Settings.Secure.DOZE_PICK_UP_GESTURE, 0, userId);
Alex Chau1e2b7312018-02-28 10:47:48 +0000646 android.provider.Settings.Secure.putIntForUser(
yuemingwc0109512018-01-21 19:21:27 +0000647 context.getContentResolver(),
Alex Chau1e2b7312018-02-28 10:47:48 +0000648 Settings.Secure.DOZE_PULSE_ON_LONG_PRESS, 0, userId);
649 android.provider.Settings.Secure.putIntForUser(
yuemingwc0109512018-01-21 19:21:27 +0000650 context.getContentResolver(),
Lucas Dupin4359b552018-08-09 15:07:54 -0700651 Settings.Secure.DOZE_DOUBLE_TAP_GESTURE, 0, userId);
yuemingwc0109512018-01-21 19:21:27 +0000652 }
653 break;
Makoto Onukiacc50462018-02-14 14:13:49 -0800654 case UserManager.DISALLOW_CONFIG_LOCATION:
655 // When DISALLOW_CONFIG_LOCATION is set on any user, we undo the global
656 // kill switch.
657 if (newValue) {
658 android.provider.Settings.Global.putString(
659 context.getContentResolver(),
660 Global.LOCATION_GLOBAL_KILL_SWITCH, "0");
661 }
662 break;
Suprabh Shuklaf0f84c82020-02-02 17:52:20 -0800663 case UserManager.DISALLOW_APPS_CONTROL:
664 // Intentional fall-through
665 case UserManager.DISALLOW_UNINSTALL_APPS:
666 final PackageManagerInternal pmi = LocalServices.getService(
667 PackageManagerInternal.class);
668 pmi.removeAllNonSystemPackageSuspensions(userId);
669 pmi.removeAllDistractingPackageRestrictions(userId);
670 pmi.flushPackageRestrictions(userId);
671 break;
Makoto Onuki4f160732015-10-27 17:15:38 -0700672 }
Makoto Onuki4f160732015-10-27 17:15:38 -0700673 } finally {
674 Binder.restoreCallingIdentity(id);
675 }
676 }
677
yuemingw1d13eae2018-01-30 17:27:54 +0000678 public static boolean isSettingRestrictedForUser(Context context, @NonNull String setting,
679 int userId, String value, int callingUid) {
Daulet Zhanguzin82adfcb2020-01-02 17:31:40 +0000680 Objects.requireNonNull(setting);
yuemingw1d13eae2018-01-30 17:27:54 +0000681 final UserManager mUserManager = context.getSystemService(UserManager.class);
682 String restriction;
683 boolean checkAllUser = false;
684 switch (setting) {
685 case android.provider.Settings.Secure.LOCATION_MODE:
686 if (mUserManager.hasUserRestriction(
687 UserManager.DISALLOW_CONFIG_LOCATION, UserHandle.of(userId))
688 && callingUid != Process.SYSTEM_UID) {
689 return true;
690 } else if (String.valueOf(Settings.Secure.LOCATION_MODE_OFF).equals(value)) {
yuemingw1d13eae2018-01-30 17:27:54 +0000691 return false;
692 }
693 restriction = UserManager.DISALLOW_SHARE_LOCATION;
694 break;
695
696 case android.provider.Settings.Secure.LOCATION_PROVIDERS_ALLOWED:
697 if (mUserManager.hasUserRestriction(
698 UserManager.DISALLOW_CONFIG_LOCATION, UserHandle.of(userId))
699 && callingUid != Process.SYSTEM_UID) {
700 return true;
701 } else if (value != null && value.startsWith("-")) {
702 // See SettingsProvider.updateLocationProvidersAllowedLocked. "-" is to disable
703 // a provider, which should be allowed even if the user restriction is set.
704 return false;
705 }
706 restriction = UserManager.DISALLOW_SHARE_LOCATION;
707 break;
708
709 case android.provider.Settings.Secure.INSTALL_NON_MARKET_APPS:
710 if ("0".equals(value)) {
711 return false;
712 }
713 restriction = UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES;
714 break;
715
716 case android.provider.Settings.Global.ADB_ENABLED:
Joshua Duong2076c042020-01-02 14:53:32 -0800717 case android.provider.Settings.Global.ADB_WIFI_ENABLED:
yuemingw1d13eae2018-01-30 17:27:54 +0000718 if ("0".equals(value)) {
719 return false;
720 }
721 restriction = UserManager.DISALLOW_DEBUGGING_FEATURES;
722 break;
723
yuemingw1d13eae2018-01-30 17:27:54 +0000724 case android.provider.Settings.Global.PACKAGE_VERIFIER_INCLUDE_ADB:
725 if ("1".equals(value)) {
726 return false;
727 }
728 restriction = UserManager.ENSURE_VERIFY_APPS;
729 break;
730
731 case android.provider.Settings.Global.PREFERRED_NETWORK_MODE:
732 restriction = UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS;
733 break;
734
735 case android.provider.Settings.Secure.ALWAYS_ON_VPN_APP:
736 case android.provider.Settings.Secure.ALWAYS_ON_VPN_LOCKDOWN:
Pavel Grafova462bcb2019-01-25 08:50:06 +0000737 case android.provider.Settings.Secure.ALWAYS_ON_VPN_LOCKDOWN_WHITELIST:
yuemingw1d13eae2018-01-30 17:27:54 +0000738 // Whitelist system uid (ConnectivityService) and root uid to change always-on vpn
739 final int appId = UserHandle.getAppId(callingUid);
740 if (appId == Process.SYSTEM_UID || appId == Process.ROOT_UID) {
741 return false;
742 }
743 restriction = UserManager.DISALLOW_CONFIG_VPN;
744 break;
745
746 case android.provider.Settings.Global.SAFE_BOOT_DISALLOWED:
747 if ("1".equals(value)) {
748 return false;
749 }
750 restriction = UserManager.DISALLOW_SAFE_BOOT;
751 break;
752
753 case android.provider.Settings.Global.AIRPLANE_MODE_ON:
754 if ("0".equals(value)) {
755 return false;
756 }
757 restriction = UserManager.DISALLOW_AIRPLANE_MODE;
758 break;
759
760 case android.provider.Settings.Secure.DOZE_ENABLED:
761 case android.provider.Settings.Secure.DOZE_ALWAYS_ON:
Lucas Dupin4359b552018-08-09 15:07:54 -0700762 case android.provider.Settings.Secure.DOZE_PICK_UP_GESTURE:
yuemingw1d13eae2018-01-30 17:27:54 +0000763 case android.provider.Settings.Secure.DOZE_PULSE_ON_LONG_PRESS:
Lucas Dupin4359b552018-08-09 15:07:54 -0700764 case android.provider.Settings.Secure.DOZE_DOUBLE_TAP_GESTURE:
yuemingw1d13eae2018-01-30 17:27:54 +0000765 if ("0".equals(value)) {
766 return false;
767 }
768 restriction = UserManager.DISALLOW_AMBIENT_DISPLAY;
769 break;
770
771 case android.provider.Settings.Global.LOCATION_GLOBAL_KILL_SWITCH:
772 if ("0".equals(value)) {
773 return false;
774 }
775 restriction = UserManager.DISALLOW_CONFIG_LOCATION;
776 checkAllUser = true;
777 break;
778
779 case android.provider.Settings.System.SCREEN_BRIGHTNESS:
Fiona Campbelld4eb2952019-11-04 17:19:56 +0000780 case android.provider.Settings.System.SCREEN_BRIGHTNESS_FLOAT:
yuemingw1d13eae2018-01-30 17:27:54 +0000781 case android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE:
782 if (callingUid == Process.SYSTEM_UID) {
783 return false;
784 }
785 restriction = UserManager.DISALLOW_CONFIG_BRIGHTNESS;
786 break;
787
788 case android.provider.Settings.Global.AUTO_TIME:
yuemingw1d13eae2018-01-30 17:27:54 +0000789 case android.provider.Settings.Global.AUTO_TIME_ZONE:
790 if (callingUid == Process.SYSTEM_UID) {
791 return false;
792 }
793 restriction = UserManager.DISALLOW_CONFIG_DATE_TIME;
794 break;
795
796 case android.provider.Settings.System.SCREEN_OFF_TIMEOUT:
797 if (callingUid == Process.SYSTEM_UID) {
798 return false;
799 }
800 restriction = UserManager.DISALLOW_CONFIG_SCREEN_TIMEOUT;
801 break;
802
Eran Messeri09b122da2018-10-05 15:33:53 +0100803 case android.provider.Settings.Global.PRIVATE_DNS_MODE:
804 case android.provider.Settings.Global.PRIVATE_DNS_SPECIFIER:
Eran Messeric28f98a2019-02-07 15:19:23 +0000805 if (callingUid == Process.SYSTEM_UID) {
806 return false;
807 }
Eran Messeri09b122da2018-10-05 15:33:53 +0100808 restriction = UserManager.DISALLOW_CONFIG_PRIVATE_DNS;
809 break;
yuemingw1d13eae2018-01-30 17:27:54 +0000810 default:
811 if (setting.startsWith(Settings.Global.DATA_ROAMING)) {
812 if ("0".equals(value)) {
813 return false;
814 }
815 restriction = UserManager.DISALLOW_DATA_ROAMING;
816 break;
817 }
818 return false;
819 }
820
821 if (checkAllUser) {
822 return mUserManager.hasUserRestrictionOnAnyUser(restriction);
823 } else {
824 return mUserManager.hasUserRestriction(restriction, UserHandle.of(userId));
825 }
826 }
827
Makoto Onukia4f11972015-10-01 13:19:58 -0700828 public static void dumpRestrictions(PrintWriter pw, String prefix, Bundle restrictions) {
829 boolean noneSet = true;
830 if (restrictions != null) {
831 for (String key : restrictions.keySet()) {
832 if (restrictions.getBoolean(key, false)) {
833 pw.println(prefix + key);
834 noneSet = false;
835 }
836 }
Makoto Onuki068c54a2015-10-13 14:34:03 -0700837 if (noneSet) {
838 pw.println(prefix + "none");
839 }
840 } else {
841 pw.println(prefix + "null");
Makoto Onukia4f11972015-10-01 13:19:58 -0700842 }
843 }
Pavel Grafov6a40f092016-10-25 15:46:51 +0100844
845 /**
Alex Johnstond4c416f2020-02-19 16:29:38 +0000846 * Moves a particular restriction from one array of restrictions sets to a restriction set,
847 * e.g. for all users.
Pavel Grafov6a40f092016-10-25 15:46:51 +0100848 */
Alex Johnstond4c416f2020-02-19 16:29:38 +0000849 public static void moveRestriction(String restrictionKey,
850 SparseArray<RestrictionsSet> sourceRestrictionsSets,
851 RestrictionsSet destRestrictionSet) {
852 for (int i = 0; i < sourceRestrictionsSets.size(); i++) {
853 final RestrictionsSet sourceRestrictionsSet = sourceRestrictionsSets.valueAt(i);
854 sourceRestrictionsSet.moveRestriction(destRestrictionSet, restrictionKey);
Pavel Grafov6a40f092016-10-25 15:46:51 +0100855 }
856 }
Pavel Grafov7f4ad752017-03-28 13:44:04 +0100857
858 /**
859 * Returns whether restrictions differ between two bundles.
860 * @param oldRestrictions old bundle of restrictions.
861 * @param newRestrictions new bundle of restrictions
862 * @param restrictions restrictions of interest, if empty, all restrictions are checked.
863 */
864 public static boolean restrictionsChanged(Bundle oldRestrictions, Bundle newRestrictions,
865 String... restrictions) {
866 if (restrictions.length == 0) {
867 return areEqual(oldRestrictions, newRestrictions);
868 }
869 for (final String restriction : restrictions) {
870 if (oldRestrictions.getBoolean(restriction, false) !=
871 newRestrictions.getBoolean(restriction, false)) {
872 return true;
873 }
874 }
875 return false;
876 }
Irina Dumitrescu4638edd2018-09-05 14:08:33 +0100877
878 private static void setInstallMarketAppsRestriction(ContentResolver cr, int userId,
879 int settingValue) {
880 android.provider.Settings.Secure.putIntForUser(
881 cr, android.provider.Settings.Secure.INSTALL_NON_MARKET_APPS, settingValue, userId);
882 }
883
884 private static int getNewUserRestrictionSetting(Context context, int userId,
885 String userRestriction, boolean newValue) {
886 return (newValue || UserManager.get(context).hasUserRestriction(userRestriction,
887 UserHandle.of(userId))) ? 0 : 1;
888 }
Makoto Onukia4f11972015-10-01 13:19:58 -0700889}