blob: 89030ed7c0758c5ae7717d814b3c0c50b64f1478 [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;
Makoto Onuki4f160732015-10-27 17:15:38 -070029import android.os.Binder;
Makoto Onukia4f11972015-10-01 13:19:58 -070030import android.os.Bundle;
yuemingw1d13eae2018-01-30 17:27:54 +000031import android.os.Process;
Fyodor Kupolov9cbfc9e2015-10-07 15:52:33 -070032import android.os.RemoteException;
Makoto Onuki4f160732015-10-27 17:15:38 -070033import android.os.UserHandle;
Makoto Onukia4f11972015-10-01 13:19:58 -070034import android.os.UserManager;
Pavel Grafov6a40f092016-10-25 15:46:51 +010035import android.os.UserManagerInternal;
yuemingw265c6922018-01-11 18:31:14 +000036import android.provider.Settings;
Makoto Onukiacc50462018-02-14 14:13:49 -080037import android.provider.Settings.Global;
Mahaver Chopradea471e2015-12-17 11:02:37 +000038import android.telephony.SubscriptionInfo;
39import android.telephony.SubscriptionManager;
Makoto Onuki1a2cd742015-11-16 13:51:27 -080040import android.util.Log;
Makoto Onuki1f1ceef2016-01-28 11:32:32 -080041import android.util.Slog;
Pavel Grafov6a40f092016-10-25 15:46:51 +010042import android.util.SparseArray;
Makoto Onukia4f11972015-10-01 13:19:58 -070043
Pavel Grafova462bcb2019-01-25 08:50:06 +000044import com.android.internal.util.Preconditions;
45
46import com.google.android.collect.Sets;
47
Makoto Onukia4f11972015-10-01 13:19:58 -070048import org.xmlpull.v1.XmlPullParser;
49import org.xmlpull.v1.XmlSerializer;
50
51import java.io.IOException;
52import java.io.PrintWriter;
Mahaver Chopradea471e2015-12-17 11:02:37 +000053import java.util.List;
Daulet Zhanguzin82adfcb2020-01-02 17:31:40 +000054import java.util.Objects;
Makoto Onukia4f11972015-10-01 13:19:58 -070055import java.util.Set;
56
Makoto Onukid45a4a22015-11-02 17:17:38 -080057/**
Mahaver Chopradea471e2015-12-17 11:02:37 +000058 * Utility methods for user restrictions.
Makoto Onukid45a4a22015-11-02 17:17:38 -080059 *
60 * <p>See {@link UserManagerService} for the method suffixes.
61 */
Makoto Onukia4f11972015-10-01 13:19:58 -070062public class UserRestrictionsUtils {
Makoto Onuki4f160732015-10-27 17:15:38 -070063 private static final String TAG = "UserRestrictionsUtils";
64
Makoto Onukia4f11972015-10-01 13:19:58 -070065 private UserRestrictionsUtils() {
66 }
67
Makoto Onuki1f1ceef2016-01-28 11:32:32 -080068 private static Set<String> newSetWithUniqueCheck(String[] strings) {
69 final Set<String> ret = Sets.newArraySet(strings);
70
71 // Make sure there's no overlap.
72 Preconditions.checkState(ret.size() == strings.length);
73 return ret;
74 }
75
76 public static final Set<String> USER_RESTRICTIONS = newSetWithUniqueCheck(new String[] {
Makoto Onukia4f11972015-10-01 13:19:58 -070077 UserManager.DISALLOW_CONFIG_WIFI,
Christine Franks1bade5d2017-10-10 15:41:50 -070078 UserManager.DISALLOW_CONFIG_LOCALE,
Makoto Onukia4f11972015-10-01 13:19:58 -070079 UserManager.DISALLOW_MODIFY_ACCOUNTS,
80 UserManager.DISALLOW_INSTALL_APPS,
81 UserManager.DISALLOW_UNINSTALL_APPS,
82 UserManager.DISALLOW_SHARE_LOCATION,
83 UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES,
Irina Dumitrescu4638edd2018-09-05 14:08:33 +010084 UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES_GLOBALLY,
Makoto Onukia4f11972015-10-01 13:19:58 -070085 UserManager.DISALLOW_CONFIG_BLUETOOTH,
Lenka Trochtova63d5e4a72016-12-02 12:19:39 +010086 UserManager.DISALLOW_BLUETOOTH,
Pavel Grafov7f4ad752017-03-28 13:44:04 +010087 UserManager.DISALLOW_BLUETOOTH_SHARING,
Makoto Onukia4f11972015-10-01 13:19:58 -070088 UserManager.DISALLOW_USB_FILE_TRANSFER,
89 UserManager.DISALLOW_CONFIG_CREDENTIALS,
90 UserManager.DISALLOW_REMOVE_USER,
Esteban Talavera6c9116a2016-11-24 16:12:44 +000091 UserManager.DISALLOW_REMOVE_MANAGED_PROFILE,
Makoto Onukia4f11972015-10-01 13:19:58 -070092 UserManager.DISALLOW_DEBUGGING_FEATURES,
93 UserManager.DISALLOW_CONFIG_VPN,
yuemingwa9772f362017-10-23 18:34:35 +010094 UserManager.DISALLOW_CONFIG_DATE_TIME,
Makoto Onukia4f11972015-10-01 13:19:58 -070095 UserManager.DISALLOW_CONFIG_TETHERING,
96 UserManager.DISALLOW_NETWORK_RESET,
97 UserManager.DISALLOW_FACTORY_RESET,
98 UserManager.DISALLOW_ADD_USER,
Esteban Talavera6c9116a2016-11-24 16:12:44 +000099 UserManager.DISALLOW_ADD_MANAGED_PROFILE,
Makoto Onukia4f11972015-10-01 13:19:58 -0700100 UserManager.ENSURE_VERIFY_APPS,
101 UserManager.DISALLOW_CONFIG_CELL_BROADCASTS,
102 UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS,
103 UserManager.DISALLOW_APPS_CONTROL,
104 UserManager.DISALLOW_MOUNT_PHYSICAL_MEDIA,
105 UserManager.DISALLOW_UNMUTE_MICROPHONE,
106 UserManager.DISALLOW_ADJUST_VOLUME,
107 UserManager.DISALLOW_OUTGOING_CALLS,
108 UserManager.DISALLOW_SMS,
109 UserManager.DISALLOW_FUN,
110 UserManager.DISALLOW_CREATE_WINDOWS,
Charles He22ff6f9d2017-10-05 21:28:55 +0100111 UserManager.DISALLOW_SYSTEM_ERROR_DIALOGS,
Makoto Onukia4f11972015-10-01 13:19:58 -0700112 UserManager.DISALLOW_CROSS_PROFILE_COPY_PASTE,
113 UserManager.DISALLOW_OUTGOING_BEAM,
114 UserManager.DISALLOW_WALLPAPER,
115 UserManager.DISALLOW_SAFE_BOOT,
116 UserManager.ALLOW_PARENT_PROFILE_APP_LINKING,
117 UserManager.DISALLOW_RECORD_AUDIO,
Fyodor Kupolov9cbfc9e2015-10-07 15:52:33 -0700118 UserManager.DISALLOW_CAMERA,
Mahaver Chopradea471e2015-12-17 11:02:37 +0000119 UserManager.DISALLOW_RUN_IN_BACKGROUND,
Oleksandr Peletskyi7f1f1df2016-01-18 15:40:21 +0100120 UserManager.DISALLOW_DATA_ROAMING,
Oleksandr Peletskyif2519812016-01-26 20:16:06 +0100121 UserManager.DISALLOW_SET_USER_ICON,
Mahaver Chopra3d9805d2016-07-07 16:25:05 +0100122 UserManager.DISALLOW_SET_WALLPAPER,
Tony Makc1205112016-07-22 16:02:59 +0100123 UserManager.DISALLOW_OEM_UNLOCK,
Esteban Talavera492b4722017-02-13 14:59:45 +0000124 UserManager.DISALLOW_UNMUTE_DEVICE,
Felipe Leme24d58932017-03-21 14:13:58 -0700125 UserManager.DISALLOW_AUTOFILL,
Felipe Leme08c92022019-02-08 14:19:55 -0800126 UserManager.DISALLOW_CONTENT_CAPTURE,
Zak Cohen3e12ac72019-03-05 16:50:39 -0800127 UserManager.DISALLOW_CONTENT_SUGGESTIONS,
Pavel Grafovc4f87e92017-10-26 16:34:25 +0100128 UserManager.DISALLOW_USER_SWITCH,
129 UserManager.DISALLOW_UNIFIED_PASSWORD,
yuemingw7810b8b2018-02-01 17:32:25 +0000130 UserManager.DISALLOW_CONFIG_LOCATION,
yuemingwc6ac29d2018-01-10 16:54:08 +0000131 UserManager.DISALLOW_AIRPLANE_MODE,
Rubin Xucc391c22018-01-02 20:37:35 +0000132 UserManager.DISALLOW_CONFIG_BRIGHTNESS,
133 UserManager.DISALLOW_SHARE_INTO_MANAGED_PROFILE,
yuemingw5cda3ae2018-01-15 10:26:19 +0000134 UserManager.DISALLOW_AMBIENT_DISPLAY,
Vladislav Kuzkokov622b9f92018-01-25 16:33:05 +0100135 UserManager.DISALLOW_CONFIG_SCREEN_TIMEOUT,
Eran Messeri09b122da2018-10-05 15:33:53 +0100136 UserManager.DISALLOW_PRINTING,
137 UserManager.DISALLOW_CONFIG_PRIVATE_DNS
Makoto Onuki1f1ceef2016-01-28 11:32:32 -0800138 });
Makoto Onukia4f11972015-10-01 13:19:58 -0700139
140 /**
141 * Set of user restriction which we don't want to persist.
142 */
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800143 private static final Set<String> NON_PERSIST_USER_RESTRICTIONS = Sets.newArraySet(
144 UserManager.DISALLOW_RECORD_AUDIO
145 );
Makoto Onukia4f11972015-10-01 13:19:58 -0700146
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800147 /**
Pavel Grafov6a40f092016-10-25 15:46:51 +0100148 * User restrictions that cannot be set by profile owners of secondary users. When set by DO
149 * they will be applied to all users.
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800150 */
Pavel Grafov6a40f092016-10-25 15:46:51 +0100151 private static final Set<String> PRIMARY_USER_ONLY_RESTRICTIONS = Sets.newArraySet(
Lenka Trochtova63d5e4a72016-12-02 12:19:39 +0100152 UserManager.DISALLOW_BLUETOOTH,
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800153 UserManager.DISALLOW_USB_FILE_TRANSFER,
154 UserManager.DISALLOW_CONFIG_TETHERING,
155 UserManager.DISALLOW_NETWORK_RESET,
156 UserManager.DISALLOW_FACTORY_RESET,
157 UserManager.DISALLOW_ADD_USER,
158 UserManager.DISALLOW_CONFIG_CELL_BROADCASTS,
159 UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS,
160 UserManager.DISALLOW_MOUNT_PHYSICAL_MEDIA,
161 UserManager.DISALLOW_SMS,
162 UserManager.DISALLOW_FUN,
163 UserManager.DISALLOW_SAFE_BOOT,
Mahaver Chopradea471e2015-12-17 11:02:37 +0000164 UserManager.DISALLOW_CREATE_WINDOWS,
yuemingw5fe75dc2017-11-29 15:52:56 +0000165 UserManager.DISALLOW_DATA_ROAMING,
166 UserManager.DISALLOW_AIRPLANE_MODE
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800167 );
168
169 /**
Benjamin Franzff66fa92017-08-10 10:39:44 +0100170 * User restrictions that cannot be set by profile owners. Applied to all users.
171 */
172 private static final Set<String> DEVICE_OWNER_ONLY_RESTRICTIONS = Sets.newArraySet(
Eran Messeri09b122da2018-10-05 15:33:53 +0100173 UserManager.DISALLOW_USER_SWITCH,
174 UserManager.DISALLOW_CONFIG_PRIVATE_DNS
Benjamin Franzff66fa92017-08-10 10:39:44 +0100175 );
176
177 /**
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800178 * User restrictions that can't be changed by device owner or profile owner.
179 */
180 private static final Set<String> IMMUTABLE_BY_OWNERS = Sets.newArraySet(
181 UserManager.DISALLOW_RECORD_AUDIO,
Mahaver Chopra3d9805d2016-07-07 16:25:05 +0100182 UserManager.DISALLOW_WALLPAPER,
183 UserManager.DISALLOW_OEM_UNLOCK
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800184 );
185
186 /**
187 * Special user restrictions that can be applied to a user as well as to all users globally,
188 * depending on callers. When device owner sets them, they'll be applied to all users.
189 */
190 private static final Set<String> GLOBAL_RESTRICTIONS = Sets.newArraySet(
191 UserManager.DISALLOW_ADJUST_VOLUME,
Pavel Grafov7f4ad752017-03-28 13:44:04 +0100192 UserManager.DISALLOW_BLUETOOTH_SHARING,
yuemingwa9772f362017-10-23 18:34:35 +0100193 UserManager.DISALLOW_CONFIG_DATE_TIME,
Charles He22ff6f9d2017-10-05 21:28:55 +0100194 UserManager.DISALLOW_SYSTEM_ERROR_DIALOGS,
Fyodor Kupolov9cbfc9e2015-10-07 15:52:33 -0700195 UserManager.DISALLOW_RUN_IN_BACKGROUND,
Tony Makc1205112016-07-22 16:02:59 +0100196 UserManager.DISALLOW_UNMUTE_MICROPHONE,
Alex Johnstonece5cdb2019-11-20 14:37:13 +0000197 UserManager.DISALLOW_UNMUTE_DEVICE,
198 UserManager.DISALLOW_CAMERA
199 );
200
201 /**
Alex Johnston7c3d7e22020-01-08 12:57:14 +0000202 * Special user restrictions that profile owner of an organization-owned managed profile can
203 * set on the parent profile instance to apply them globally.
Alex Johnstonece5cdb2019-11-20 14:37:13 +0000204 */
205 private static final Set<String> PROFILE_OWNER_ORGANIZATION_OWNED_GLOBAL_RESTRICTIONS =
206 Sets.newArraySet(
207 UserManager.DISALLOW_CONFIG_DATE_TIME,
Alex Johnston7c3d7e22020-01-08 12:57:14 +0000208 UserManager.DISALLOW_CAMERA,
209 UserManager.DISALLOW_ADD_USER,
210 UserManager.DISALLOW_BLUETOOTH,
211 UserManager.DISALLOW_BLUETOOTH_SHARING,
212 UserManager.DISALLOW_CONFIG_BLUETOOTH,
213 UserManager.DISALLOW_CONFIG_CELL_BROADCASTS,
214 UserManager.DISALLOW_CONFIG_LOCATION,
215 UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS,
216 UserManager.DISALLOW_CONFIG_PRIVATE_DNS,
217 UserManager.DISALLOW_CONFIG_TETHERING,
218 UserManager.DISALLOW_CONFIG_WIFI,
219 UserManager.DISALLOW_CONTENT_CAPTURE,
220 UserManager.DISALLOW_CONTENT_SUGGESTIONS,
221 UserManager.DISALLOW_DATA_ROAMING,
222 UserManager.DISALLOW_DEBUGGING_FEATURES,
223 UserManager.DISALLOW_SAFE_BOOT,
224 UserManager.DISALLOW_SHARE_LOCATION,
225 UserManager.DISALLOW_SMS,
226 UserManager.DISALLOW_USB_FILE_TRANSFER
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800227 );
228
Makoto Onuki1f1ceef2016-01-28 11:32:32 -0800229 /**
Pavel Grafov7f4ad752017-03-28 13:44:04 +0100230 * User restrictions that default to {@code true} for managed profile owners.
231 *
232 * NB: {@link UserManager#DISALLOW_INSTALL_UNKNOWN_SOURCES} is also set by default but it is
233 * not set to existing profile owners unless they used to have INSTALL_NON_MARKET_APPS disabled
234 * in settings. So it is handled separately.
235 */
236 private static final Set<String> DEFAULT_ENABLED_FOR_MANAGED_PROFILES = Sets.newArraySet(
237 UserManager.DISALLOW_BLUETOOTH_SHARING
238 );
239
Pavel Grafovc4f87e92017-10-26 16:34:25 +0100240 /**
Pavel Grafov6a40f092016-10-25 15:46:51 +0100241 * Special user restrictions that are always applied to all users no matter who sets them.
242 */
243 private static final Set<String> PROFILE_GLOBAL_RESTRICTIONS = Sets.newArraySet(
yuemingw5fe75dc2017-11-29 15:52:56 +0000244 UserManager.ENSURE_VERIFY_APPS,
Irina Dumitrescu4638edd2018-09-05 14:08:33 +0100245 UserManager.DISALLOW_AIRPLANE_MODE,
246 UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES_GLOBALLY
Pavel Grafov6a40f092016-10-25 15:46:51 +0100247 );
248
Esteban Talavera6c9116a2016-11-24 16:12:44 +0000249 /**
Bookatz1711b31d2019-04-02 11:11:20 -0700250 * Returns whether the given restriction name is valid (and logs it if it isn't).
Makoto Onuki1f1ceef2016-01-28 11:32:32 -0800251 */
252 public static boolean isValidRestriction(@NonNull String restriction) {
253 if (!USER_RESTRICTIONS.contains(restriction)) {
Bookatz1711b31d2019-04-02 11:11:20 -0700254 // Log this, with severity depending on the source.
255 final int uid = Binder.getCallingUid();
256 String[] pkgs = null;
257 try {
258 pkgs = AppGlobals.getPackageManager().getPackagesForUid(uid);
259 } catch (RemoteException e) {
260 // Ignore
261 }
262 StringBuilder msg = new StringBuilder("Unknown restriction queried by uid ");
263 msg.append(uid);
264 if (pkgs != null && pkgs.length > 0) {
265 msg.append(" (");
266 msg.append(pkgs[0]);
267 if (pkgs.length > 1) {
268 msg.append(" et al");
269 }
270 msg.append(")");
271 }
272 msg.append(": ");
273 msg.append(restriction);
274 if (restriction != null && isSystemApp(uid, pkgs)) {
275 Slog.wtf(TAG, msg.toString());
276 } else {
277 Slog.e(TAG, msg.toString());
278 }
Makoto Onuki1f1ceef2016-01-28 11:32:32 -0800279 return false;
280 }
281 return true;
282 }
283
Bookatz1711b31d2019-04-02 11:11:20 -0700284 /** Returns whether the given uid (or corresponding packageList) is for a System app. */
285 private static boolean isSystemApp(int uid, String[] packageList) {
286 if (UserHandle.isCore(uid)) {
287 return true;
288 }
289 if (packageList == null) {
290 return false;
291 }
292 final IPackageManager pm = AppGlobals.getPackageManager();
293 for (int i = 0; i < packageList.length; i++) {
294 try {
295 final int flags = PackageManager.MATCH_UNINSTALLED_PACKAGES
296 | PackageManager.MATCH_DIRECT_BOOT_AWARE
297 | PackageManager.MATCH_DIRECT_BOOT_UNAWARE;
298 final ApplicationInfo appInfo =
299 pm.getApplicationInfo(packageList[i], flags, UserHandle.getUserId(uid));
300 if (appInfo != null && appInfo.isSystemApp()) {
301 return true;
302 }
303 } catch (RemoteException e) {
304 // Ignore
305 }
306 }
307 return false;
308 }
309
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800310 public static void writeRestrictions(@NonNull XmlSerializer serializer,
311 @Nullable Bundle restrictions, @NonNull String tag) throws IOException {
312 if (restrictions == null) {
313 return;
314 }
315
Makoto Onukia4f11972015-10-01 13:19:58 -0700316 serializer.startTag(null, tag);
Makoto Onukiac65e1e2015-11-20 15:33:17 -0800317 for (String key : restrictions.keySet()) {
318 if (NON_PERSIST_USER_RESTRICTIONS.contains(key)) {
319 continue; // Don't persist.
Makoto Onukia4f11972015-10-01 13:19:58 -0700320 }
Makoto Onukiac65e1e2015-11-20 15:33:17 -0800321 if (USER_RESTRICTIONS.contains(key)) {
322 if (restrictions.getBoolean(key)) {
323 serializer.attribute(null, key, "true");
324 }
325 continue;
326 }
327 Log.w(TAG, "Unknown user restriction detected: " + key);
Makoto Onukia4f11972015-10-01 13:19:58 -0700328 }
329 serializer.endTag(null, tag);
330 }
331
Esteban Talaverac48b20f2016-08-11 11:23:40 +0100332 public static void readRestrictions(XmlPullParser parser, Bundle restrictions) {
Fyodor Kupoloveafee022017-03-15 17:09:04 -0700333 restrictions.clear();
Makoto Onukia4f11972015-10-01 13:19:58 -0700334 for (String key : USER_RESTRICTIONS) {
335 final String value = parser.getAttributeValue(null, key);
336 if (value != null) {
337 restrictions.putBoolean(key, Boolean.parseBoolean(value));
338 }
339 }
340 }
341
Pavel Grafov6a40f092016-10-25 15:46:51 +0100342 public static Bundle readRestrictions(XmlPullParser parser) {
343 final Bundle result = new Bundle();
344 readRestrictions(parser, result);
345 return result;
346 }
347
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800348 /**
349 * @return {@code in} itself when it's not null, or an empty bundle (which can writable).
350 */
351 public static Bundle nonNull(@Nullable Bundle in) {
352 return in != null ? in : new Bundle();
353 }
354
355 public static boolean isEmpty(@Nullable Bundle in) {
356 return (in == null) || (in.size() == 0);
357 }
358
359 /**
Pavel Grafov6a40f092016-10-25 15:46:51 +0100360 * Returns {@code true} if given bundle is not null and contains {@code true} for a given
361 * restriction.
362 */
363 public static boolean contains(@Nullable Bundle in, String restriction) {
364 return in != null && in.getBoolean(restriction);
365 }
366
367 /**
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800368 * Creates a copy of the {@code in} Bundle. If {@code in} is null, it'll return an empty
369 * bundle.
370 *
371 * <p>The resulting {@link Bundle} is always writable. (i.e. it won't return
372 * {@link Bundle#EMPTY})
373 */
374 public static @NonNull Bundle clone(@Nullable Bundle in) {
375 return (in != null) ? new Bundle(in) : new Bundle();
376 }
377
378 public static void merge(@NonNull Bundle dest, @Nullable Bundle in) {
Daulet Zhanguzin82adfcb2020-01-02 17:31:40 +0000379 Objects.requireNonNull(dest);
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800380 Preconditions.checkArgument(dest != in);
Makoto Onuki068c54a2015-10-13 14:34:03 -0700381 if (in == null) {
382 return;
383 }
384 for (String key : in.keySet()) {
385 if (in.getBoolean(key, false)) {
386 dest.putBoolean(key, true);
387 }
388 }
389 }
390
Makoto Onuki4f160732015-10-27 17:15:38 -0700391 /**
Pavel Grafov6a40f092016-10-25 15:46:51 +0100392 * Merges a sparse array of restrictions bundles into one.
393 */
394 @Nullable
395 public static Bundle mergeAll(SparseArray<Bundle> restrictions) {
396 if (restrictions.size() == 0) {
397 return null;
398 } else {
399 final Bundle result = new Bundle();
400 for (int i = 0; i < restrictions.size(); i++) {
401 merge(result, restrictions.valueAt(i));
402 }
403 return result;
404 }
405 }
406
407 /**
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800408 * @return true if a restriction is settable by device owner.
409 */
410 public static boolean canDeviceOwnerChange(String restriction) {
411 return !IMMUTABLE_BY_OWNERS.contains(restriction);
412 }
413
414 /**
Makoto Onuki5485ed42015-12-09 11:38:23 -0800415 * @return true if a restriction is settable by profile owner. Note it takes a user ID because
416 * some restrictions can be changed by PO only when it's running on the system user.
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800417 */
Makoto Onuki5485ed42015-12-09 11:38:23 -0800418 public static boolean canProfileOwnerChange(String restriction, int userId) {
419 return !IMMUTABLE_BY_OWNERS.contains(restriction)
Benjamin Franzff66fa92017-08-10 10:39:44 +0100420 && !DEVICE_OWNER_ONLY_RESTRICTIONS.contains(restriction)
Makoto Onuki5485ed42015-12-09 11:38:23 -0800421 && !(userId != UserHandle.USER_SYSTEM
Pavel Grafov6a40f092016-10-25 15:46:51 +0100422 && PRIMARY_USER_ONLY_RESTRICTIONS.contains(restriction));
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800423 }
424
425 /**
Alex Johnston51419382019-11-26 17:00:10 +0000426 * @return true if a restriction is settable by profile owner of an organization owned device.
427 */
428 public static boolean canProfileOwnerOfOrganizationOwnedDeviceChange(String restriction) {
429 return PROFILE_OWNER_ORGANIZATION_OWNED_GLOBAL_RESTRICTIONS.contains(restriction);
430 }
431
432 /**
Pavel Grafov7f4ad752017-03-28 13:44:04 +0100433 * Returns the user restrictions that default to {@code true} for managed profile owners.
434 */
435 public static @NonNull Set<String> getDefaultEnabledForManagedProfiles() {
436 return DEFAULT_ENABLED_FOR_MANAGED_PROFILES;
437 }
438
439 /**
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800440 * Takes restrictions that can be set by device owner, and sort them into what should be applied
441 * globally and what should be applied only on the current user.
442 */
Alex Johnstonece5cdb2019-11-20 14:37:13 +0000443 public static void sortToGlobalAndLocal(@Nullable Bundle in,
444 @UserManagerInternal.OwnerType int restrictionOwnerType, @NonNull Bundle global,
445 @NonNull Bundle local) {
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800446 if (in == null || in.size() == 0) {
447 return;
448 }
449 for (String key : in.keySet()) {
450 if (!in.getBoolean(key)) {
451 continue;
452 }
Alex Johnstonece5cdb2019-11-20 14:37:13 +0000453 if (isGlobal(restrictionOwnerType, key)) {
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800454 global.putBoolean(key, true);
455 } else {
456 local.putBoolean(key, true);
457 }
458 }
459 }
460
461 /**
Pavel Grafov6a40f092016-10-25 15:46:51 +0100462 * Whether given user restriction should be enforced globally.
463 */
Alex Johnstonece5cdb2019-11-20 14:37:13 +0000464 private static boolean isGlobal(@UserManagerInternal.OwnerType int restrictionOwnerType,
465 String key) {
466 return ((restrictionOwnerType == UserManagerInternal.OWNER_TYPE_DEVICE_OWNER) && (
467 PRIMARY_USER_ONLY_RESTRICTIONS.contains(key) || GLOBAL_RESTRICTIONS.contains(key)))
468 || ((restrictionOwnerType
469 == UserManagerInternal.OWNER_TYPE_PROFILE_OWNER_OF_ORGANIZATION_OWNED_DEVICE)
470 && PROFILE_OWNER_ORGANIZATION_OWNED_GLOBAL_RESTRICTIONS.contains(key))
Benjamin Franzff66fa92017-08-10 10:39:44 +0100471 || PROFILE_GLOBAL_RESTRICTIONS.contains(key)
472 || DEVICE_OWNER_ONLY_RESTRICTIONS.contains(key);
Pavel Grafov6a40f092016-10-25 15:46:51 +0100473 }
474
475 /**
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800476 * @return true if two Bundles contain the same user restriction.
477 * A null bundle and an empty bundle are considered to be equal.
478 */
479 public static boolean areEqual(@Nullable Bundle a, @Nullable Bundle b) {
480 if (a == b) {
481 return true;
482 }
483 if (isEmpty(a)) {
484 return isEmpty(b);
485 }
486 if (isEmpty(b)) {
487 return false;
488 }
489 for (String key : a.keySet()) {
490 if (a.getBoolean(key) != b.getBoolean(key)) {
491 return false;
492 }
493 }
494 for (String key : b.keySet()) {
495 if (a.getBoolean(key) != b.getBoolean(key)) {
496 return false;
497 }
498 }
499 return true;
500 }
501
502 /**
Makoto Onuki4f160732015-10-27 17:15:38 -0700503 * Takes a new use restriction set and the previous set, and apply the restrictions that have
504 * changed.
Makoto Onukid45a4a22015-11-02 17:17:38 -0800505 *
Fyodor Kupolov9cbfc9e2015-10-07 15:52:33 -0700506 * <p>Note this method is called by {@link UserManagerService} without holding any locks.
Makoto Onuki4f160732015-10-27 17:15:38 -0700507 */
Fyodor Kupolov9cbfc9e2015-10-07 15:52:33 -0700508 public static void applyUserRestrictions(Context context, int userId,
Makoto Onukid45a4a22015-11-02 17:17:38 -0800509 Bundle newRestrictions, Bundle prevRestrictions) {
Makoto Onuki4f160732015-10-27 17:15:38 -0700510 for (String key : USER_RESTRICTIONS) {
511 final boolean newValue = newRestrictions.getBoolean(key);
512 final boolean prevValue = prevRestrictions.getBoolean(key);
513
514 if (newValue != prevValue) {
Fyodor Kupolov9cbfc9e2015-10-07 15:52:33 -0700515 applyUserRestriction(context, userId, key, newValue);
Makoto Onuki4f160732015-10-27 17:15:38 -0700516 }
517 }
518 }
Fyodor Kupolov9cbfc9e2015-10-07 15:52:33 -0700519
Makoto Onukid45a4a22015-11-02 17:17:38 -0800520 /**
521 * Apply each user restriction.
522 *
yuemingw1d13eae2018-01-30 17:27:54 +0000523 * <p>See also {@link #isSettingRestrictedForUser()},
Makoto Onuki28da2e32015-11-20 11:30:44 -0800524 * which should be in sync with this method.
Makoto Onukid45a4a22015-11-02 17:17:38 -0800525 */
Fyodor Kupolov9cbfc9e2015-10-07 15:52:33 -0700526 private static void applyUserRestriction(Context context, int userId, String key,
Makoto Onuki4f160732015-10-27 17:15:38 -0700527 boolean newValue) {
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800528 if (UserManagerService.DBG) {
529 Log.d(TAG, "Applying user restriction: userId=" + userId
530 + " key=" + key + " value=" + newValue);
531 }
Makoto Onuki4f160732015-10-27 17:15:38 -0700532 // When certain restrictions are cleared, we don't update the system settings,
533 // because these settings are changeable on the Settings UI and we don't know the original
534 // value -- for example LOCATION_MODE might have been off already when the restriction was
535 // set, and in that case even if the restriction is lifted, changing it to ON would be
536 // wrong. So just don't do anything in such a case. If the user hopes to enable location
537 // later, they can do it on the Settings UI.
Benjamin Franz0ff13fc2016-07-12 13:42:21 +0100538 // WARNING: Remember that Settings.Global and Settings.Secure are changeable via adb.
539 // To prevent this from happening for a given user restriction, you have to add a check to
540 // SettingsProvider.isGlobalOrSecureSettingRestrictedForUser.
Makoto Onuki4f160732015-10-27 17:15:38 -0700541
542 final ContentResolver cr = context.getContentResolver();
543 final long id = Binder.clearCallingIdentity();
544 try {
545 switch (key) {
Mahaver Chopradea471e2015-12-17 11:02:37 +0000546 case UserManager.DISALLOW_DATA_ROAMING:
547 if (newValue) {
548 // DISALLOW_DATA_ROAMING user restriction is set.
549
550 // Multi sim device.
Jeff Sharkey717f52f2018-01-04 16:04:11 -0700551 SubscriptionManager subscriptionManager = context
552 .getSystemService(SubscriptionManager.class);
Mahaver Chopradea471e2015-12-17 11:02:37 +0000553 final List<SubscriptionInfo> subscriptionInfoList =
554 subscriptionManager.getActiveSubscriptionInfoList();
555 if (subscriptionInfoList != null) {
556 for (SubscriptionInfo subInfo : subscriptionInfoList) {
557 android.provider.Settings.Global.putStringForUser(cr,
558 android.provider.Settings.Global.DATA_ROAMING
559 + subInfo.getSubscriptionId(), "0", userId);
560 }
561 }
562
563 // Single sim device.
564 android.provider.Settings.Global.putStringForUser(cr,
565 android.provider.Settings.Global.DATA_ROAMING, "0", userId);
566 }
567 break;
Makoto Onuki4f160732015-10-27 17:15:38 -0700568 case UserManager.DISALLOW_SHARE_LOCATION:
569 if (newValue) {
570 android.provider.Settings.Secure.putIntForUser(cr,
571 android.provider.Settings.Secure.LOCATION_MODE,
572 android.provider.Settings.Secure.LOCATION_MODE_OFF,
573 userId);
Makoto Onuki4f160732015-10-27 17:15:38 -0700574 }
Makoto Onuki4f160732015-10-27 17:15:38 -0700575 break;
576 case UserManager.DISALLOW_DEBUGGING_FEATURES:
577 if (newValue) {
578 // Only disable adb if changing for system user, since it is global
579 // TODO: should this be admin user?
580 if (userId == UserHandle.USER_SYSTEM) {
581 android.provider.Settings.Global.putStringForUser(cr,
582 android.provider.Settings.Global.ADB_ENABLED, "0",
583 userId);
584 }
585 }
586 break;
587 case UserManager.ENSURE_VERIFY_APPS:
588 if (newValue) {
589 android.provider.Settings.Global.putStringForUser(
590 context.getContentResolver(),
Makoto Onuki4f160732015-10-27 17:15:38 -0700591 android.provider.Settings.Global.PACKAGE_VERIFIER_INCLUDE_ADB, "1",
592 userId);
593 }
594 break;
Irina Dumitrescu4638edd2018-09-05 14:08:33 +0100595 case UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES_GLOBALLY:
596 setInstallMarketAppsRestriction(cr, userId, getNewUserRestrictionSetting(
597 context, userId, UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES,
598 newValue));
599 break;
Makoto Onuki4f160732015-10-27 17:15:38 -0700600 case UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES:
Suprabh Shuklae3745ee2017-02-02 20:01:11 -0800601 // Since Android O, the secure setting is not available to be changed by the
602 // user. Hence, when the restriction is cleared, we need to reset the state of
603 // the setting to its default value which is now 1.
Irina Dumitrescu4638edd2018-09-05 14:08:33 +0100604 setInstallMarketAppsRestriction(cr, userId, getNewUserRestrictionSetting(
605 context, userId, UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES_GLOBALLY,
606 newValue));
Makoto Onuki4f160732015-10-27 17:15:38 -0700607 break;
Fyodor Kupolov9cbfc9e2015-10-07 15:52:33 -0700608 case UserManager.DISALLOW_RUN_IN_BACKGROUND:
609 if (newValue) {
610 int currentUser = ActivityManager.getCurrentUser();
611 if (currentUser != userId && userId != UserHandle.USER_SYSTEM) {
612 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -0800613 ActivityManager.getService().stopUser(userId, false, null);
Fyodor Kupolov9cbfc9e2015-10-07 15:52:33 -0700614 } catch (RemoteException e) {
615 throw e.rethrowAsRuntimeException();
616 }
617 }
618 }
Lenka Trochtova6474f0e2016-03-24 16:43:10 +0100619 break;
620 case UserManager.DISALLOW_SAFE_BOOT:
621 // Unlike with the other restrictions, we want to propagate the new value to
622 // the system settings even if it is false. The other restrictions modify
623 // settings which could be manually changed by the user from the Settings app
624 // after the policies enforcing these restrictions have been revoked, so we
625 // leave re-setting of those settings to the user.
626 android.provider.Settings.Global.putInt(
627 context.getContentResolver(),
628 android.provider.Settings.Global.SAFE_BOOT_DISALLOWED,
629 newValue ? 1 : 0);
630 break;
yuemingw265c6922018-01-11 18:31:14 +0000631 case UserManager.DISALLOW_AIRPLANE_MODE:
632 if (newValue) {
633 final boolean airplaneMode = Settings.Global.getInt(
634 context.getContentResolver(),
635 Settings.Global.AIRPLANE_MODE_ON, 0) == 1;
636 if (airplaneMode) {
637 android.provider.Settings.Global.putInt(
638 context.getContentResolver(),
639 android.provider.Settings.Global.AIRPLANE_MODE_ON, 0);
640 // Post the intent.
641 Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
yuemingwf3b97342018-03-28 15:35:40 +0100642 intent.putExtra("state", false);
yuemingw265c6922018-01-11 18:31:14 +0000643 context.sendBroadcastAsUser(intent, UserHandle.ALL);
644 }
645 }
646 break;
yuemingwc0109512018-01-21 19:21:27 +0000647 case UserManager.DISALLOW_AMBIENT_DISPLAY:
648 if (newValue) {
Alex Chau1e2b7312018-02-28 10:47:48 +0000649 android.provider.Settings.Secure.putIntForUser(
yuemingwc0109512018-01-21 19:21:27 +0000650 context.getContentResolver(),
Alex Chau1e2b7312018-02-28 10:47:48 +0000651 Settings.Secure.DOZE_ENABLED, 0, userId);
652 android.provider.Settings.Secure.putIntForUser(
yuemingwc0109512018-01-21 19:21:27 +0000653 context.getContentResolver(),
Alex Chau1e2b7312018-02-28 10:47:48 +0000654 Settings.Secure.DOZE_ALWAYS_ON, 0, userId);
655 android.provider.Settings.Secure.putIntForUser(
yuemingwc0109512018-01-21 19:21:27 +0000656 context.getContentResolver(),
Lucas Dupin4359b552018-08-09 15:07:54 -0700657 Settings.Secure.DOZE_PICK_UP_GESTURE, 0, userId);
Alex Chau1e2b7312018-02-28 10:47:48 +0000658 android.provider.Settings.Secure.putIntForUser(
yuemingwc0109512018-01-21 19:21:27 +0000659 context.getContentResolver(),
Alex Chau1e2b7312018-02-28 10:47:48 +0000660 Settings.Secure.DOZE_PULSE_ON_LONG_PRESS, 0, userId);
661 android.provider.Settings.Secure.putIntForUser(
yuemingwc0109512018-01-21 19:21:27 +0000662 context.getContentResolver(),
Lucas Dupin4359b552018-08-09 15:07:54 -0700663 Settings.Secure.DOZE_DOUBLE_TAP_GESTURE, 0, userId);
yuemingwc0109512018-01-21 19:21:27 +0000664 }
665 break;
Makoto Onukiacc50462018-02-14 14:13:49 -0800666 case UserManager.DISALLOW_CONFIG_LOCATION:
667 // When DISALLOW_CONFIG_LOCATION is set on any user, we undo the global
668 // kill switch.
669 if (newValue) {
670 android.provider.Settings.Global.putString(
671 context.getContentResolver(),
672 Global.LOCATION_GLOBAL_KILL_SWITCH, "0");
673 }
674 break;
Makoto Onuki4f160732015-10-27 17:15:38 -0700675 }
Makoto Onuki4f160732015-10-27 17:15:38 -0700676 } finally {
677 Binder.restoreCallingIdentity(id);
678 }
679 }
680
yuemingw1d13eae2018-01-30 17:27:54 +0000681 public static boolean isSettingRestrictedForUser(Context context, @NonNull String setting,
682 int userId, String value, int callingUid) {
Daulet Zhanguzin82adfcb2020-01-02 17:31:40 +0000683 Objects.requireNonNull(setting);
yuemingw1d13eae2018-01-30 17:27:54 +0000684 final UserManager mUserManager = context.getSystemService(UserManager.class);
685 String restriction;
686 boolean checkAllUser = false;
687 switch (setting) {
688 case android.provider.Settings.Secure.LOCATION_MODE:
689 if (mUserManager.hasUserRestriction(
690 UserManager.DISALLOW_CONFIG_LOCATION, UserHandle.of(userId))
691 && callingUid != Process.SYSTEM_UID) {
692 return true;
693 } else if (String.valueOf(Settings.Secure.LOCATION_MODE_OFF).equals(value)) {
yuemingw1d13eae2018-01-30 17:27:54 +0000694 return false;
695 }
696 restriction = UserManager.DISALLOW_SHARE_LOCATION;
697 break;
698
699 case android.provider.Settings.Secure.LOCATION_PROVIDERS_ALLOWED:
700 if (mUserManager.hasUserRestriction(
701 UserManager.DISALLOW_CONFIG_LOCATION, UserHandle.of(userId))
702 && callingUid != Process.SYSTEM_UID) {
703 return true;
704 } else if (value != null && value.startsWith("-")) {
705 // See SettingsProvider.updateLocationProvidersAllowedLocked. "-" is to disable
706 // a provider, which should be allowed even if the user restriction is set.
707 return false;
708 }
709 restriction = UserManager.DISALLOW_SHARE_LOCATION;
710 break;
711
712 case android.provider.Settings.Secure.INSTALL_NON_MARKET_APPS:
713 if ("0".equals(value)) {
714 return false;
715 }
716 restriction = UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES;
717 break;
718
719 case android.provider.Settings.Global.ADB_ENABLED:
720 if ("0".equals(value)) {
721 return false;
722 }
723 restriction = UserManager.DISALLOW_DEBUGGING_FEATURES;
724 break;
725
yuemingw1d13eae2018-01-30 17:27:54 +0000726 case android.provider.Settings.Global.PACKAGE_VERIFIER_INCLUDE_ADB:
727 if ("1".equals(value)) {
728 return false;
729 }
730 restriction = UserManager.ENSURE_VERIFY_APPS;
731 break;
732
733 case android.provider.Settings.Global.PREFERRED_NETWORK_MODE:
734 restriction = UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS;
735 break;
736
737 case android.provider.Settings.Secure.ALWAYS_ON_VPN_APP:
738 case android.provider.Settings.Secure.ALWAYS_ON_VPN_LOCKDOWN:
Pavel Grafova462bcb2019-01-25 08:50:06 +0000739 case android.provider.Settings.Secure.ALWAYS_ON_VPN_LOCKDOWN_WHITELIST:
yuemingw1d13eae2018-01-30 17:27:54 +0000740 // Whitelist system uid (ConnectivityService) and root uid to change always-on vpn
741 final int appId = UserHandle.getAppId(callingUid);
742 if (appId == Process.SYSTEM_UID || appId == Process.ROOT_UID) {
743 return false;
744 }
745 restriction = UserManager.DISALLOW_CONFIG_VPN;
746 break;
747
748 case android.provider.Settings.Global.SAFE_BOOT_DISALLOWED:
749 if ("1".equals(value)) {
750 return false;
751 }
752 restriction = UserManager.DISALLOW_SAFE_BOOT;
753 break;
754
755 case android.provider.Settings.Global.AIRPLANE_MODE_ON:
756 if ("0".equals(value)) {
757 return false;
758 }
759 restriction = UserManager.DISALLOW_AIRPLANE_MODE;
760 break;
761
762 case android.provider.Settings.Secure.DOZE_ENABLED:
763 case android.provider.Settings.Secure.DOZE_ALWAYS_ON:
Lucas Dupin4359b552018-08-09 15:07:54 -0700764 case android.provider.Settings.Secure.DOZE_PICK_UP_GESTURE:
yuemingw1d13eae2018-01-30 17:27:54 +0000765 case android.provider.Settings.Secure.DOZE_PULSE_ON_LONG_PRESS:
Lucas Dupin4359b552018-08-09 15:07:54 -0700766 case android.provider.Settings.Secure.DOZE_DOUBLE_TAP_GESTURE:
yuemingw1d13eae2018-01-30 17:27:54 +0000767 if ("0".equals(value)) {
768 return false;
769 }
770 restriction = UserManager.DISALLOW_AMBIENT_DISPLAY;
771 break;
772
773 case android.provider.Settings.Global.LOCATION_GLOBAL_KILL_SWITCH:
774 if ("0".equals(value)) {
775 return false;
776 }
777 restriction = UserManager.DISALLOW_CONFIG_LOCATION;
778 checkAllUser = true;
779 break;
780
781 case android.provider.Settings.System.SCREEN_BRIGHTNESS:
782 case android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE:
783 if (callingUid == Process.SYSTEM_UID) {
784 return false;
785 }
786 restriction = UserManager.DISALLOW_CONFIG_BRIGHTNESS;
787 break;
788
789 case android.provider.Settings.Global.AUTO_TIME:
yuemingw1d13eae2018-01-30 17:27:54 +0000790 case android.provider.Settings.Global.AUTO_TIME_ZONE:
791 if (callingUid == Process.SYSTEM_UID) {
792 return false;
793 }
794 restriction = UserManager.DISALLOW_CONFIG_DATE_TIME;
795 break;
796
797 case android.provider.Settings.System.SCREEN_OFF_TIMEOUT:
798 if (callingUid == Process.SYSTEM_UID) {
799 return false;
800 }
801 restriction = UserManager.DISALLOW_CONFIG_SCREEN_TIMEOUT;
802 break;
803
Eran Messeri09b122da2018-10-05 15:33:53 +0100804 case android.provider.Settings.Global.PRIVATE_DNS_MODE:
805 case android.provider.Settings.Global.PRIVATE_DNS_SPECIFIER:
Eran Messeric28f98a2019-02-07 15:19:23 +0000806 if (callingUid == Process.SYSTEM_UID) {
807 return false;
808 }
Eran Messeri09b122da2018-10-05 15:33:53 +0100809 restriction = UserManager.DISALLOW_CONFIG_PRIVATE_DNS;
810 break;
yuemingw1d13eae2018-01-30 17:27:54 +0000811 default:
812 if (setting.startsWith(Settings.Global.DATA_ROAMING)) {
813 if ("0".equals(value)) {
814 return false;
815 }
816 restriction = UserManager.DISALLOW_DATA_ROAMING;
817 break;
818 }
819 return false;
820 }
821
822 if (checkAllUser) {
823 return mUserManager.hasUserRestrictionOnAnyUser(restriction);
824 } else {
825 return mUserManager.hasUserRestriction(restriction, UserHandle.of(userId));
826 }
827 }
828
Makoto Onukia4f11972015-10-01 13:19:58 -0700829 public static void dumpRestrictions(PrintWriter pw, String prefix, Bundle restrictions) {
830 boolean noneSet = true;
831 if (restrictions != null) {
832 for (String key : restrictions.keySet()) {
833 if (restrictions.getBoolean(key, false)) {
834 pw.println(prefix + key);
835 noneSet = false;
836 }
837 }
Makoto Onuki068c54a2015-10-13 14:34:03 -0700838 if (noneSet) {
839 pw.println(prefix + "none");
840 }
841 } else {
842 pw.println(prefix + "null");
Makoto Onukia4f11972015-10-01 13:19:58 -0700843 }
844 }
Pavel Grafov6a40f092016-10-25 15:46:51 +0100845
846 /**
847 * Moves a particular restriction from one array of bundles to another, e.g. for all users.
848 */
849 public static void moveRestriction(String restrictionKey, SparseArray<Bundle> srcRestrictions,
850 SparseArray<Bundle> destRestrictions) {
851 for (int i = 0; i < srcRestrictions.size(); i++) {
Pavel Grafov7f4ad752017-03-28 13:44:04 +0100852 final int key = srcRestrictions.keyAt(i);
853 final Bundle from = srcRestrictions.valueAt(i);
Pavel Grafov6a40f092016-10-25 15:46:51 +0100854 if (contains(from, restrictionKey)) {
855 from.remove(restrictionKey);
856 Bundle to = destRestrictions.get(key);
857 if (to == null) {
858 to = new Bundle();
859 destRestrictions.append(key, to);
860 }
861 to.putBoolean(restrictionKey, true);
862 // Don't keep empty bundles.
863 if (from.isEmpty()) {
864 srcRestrictions.removeAt(i);
865 i--;
866 }
867 }
868 }
869 }
Pavel Grafov7f4ad752017-03-28 13:44:04 +0100870
871 /**
872 * Returns whether restrictions differ between two bundles.
873 * @param oldRestrictions old bundle of restrictions.
874 * @param newRestrictions new bundle of restrictions
875 * @param restrictions restrictions of interest, if empty, all restrictions are checked.
876 */
877 public static boolean restrictionsChanged(Bundle oldRestrictions, Bundle newRestrictions,
878 String... restrictions) {
879 if (restrictions.length == 0) {
880 return areEqual(oldRestrictions, newRestrictions);
881 }
882 for (final String restriction : restrictions) {
883 if (oldRestrictions.getBoolean(restriction, false) !=
884 newRestrictions.getBoolean(restriction, false)) {
885 return true;
886 }
887 }
888 return false;
889 }
Irina Dumitrescu4638edd2018-09-05 14:08:33 +0100890
891 private static void setInstallMarketAppsRestriction(ContentResolver cr, int userId,
892 int settingValue) {
893 android.provider.Settings.Secure.putIntForUser(
894 cr, android.provider.Settings.Secure.INSTALL_NON_MARKET_APPS, settingValue, userId);
895 }
896
897 private static int getNewUserRestrictionSetting(Context context, int userId,
898 String userRestriction, boolean newValue) {
899 return (newValue || UserManager.get(context).hasUserRestriction(userRestriction,
900 UserHandle.of(userId))) ? 0 : 1;
901 }
Makoto Onukia4f11972015-10-01 13:19:58 -0700902}