blob: c18a71d380e993c0881436134671e3a84e725e1b [file] [log] [blame]
Makoto Onukia4f11972015-10-01 13:19:58 -07001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server.pm;
18
19import com.google.android.collect.Sets;
20
Makoto Onuki1a2cd742015-11-16 13:51:27 -080021import com.android.internal.util.Preconditions;
22
23import android.annotation.NonNull;
24import android.annotation.Nullable;
Fyodor Kupolov9cbfc9e2015-10-07 15:52:33 -070025import android.app.ActivityManager;
Makoto Onuki4f160732015-10-27 17:15:38 -070026import android.content.ContentResolver;
27import android.content.Context;
Makoto Onuki4f160732015-10-27 17:15:38 -070028import android.os.Binder;
Makoto Onukia4f11972015-10-01 13:19:58 -070029import android.os.Bundle;
Fyodor Kupolov9cbfc9e2015-10-07 15:52:33 -070030import android.os.RemoteException;
Makoto Onuki4f160732015-10-27 17:15:38 -070031import android.os.UserHandle;
Makoto Onukia4f11972015-10-01 13:19:58 -070032import android.os.UserManager;
Pavel Grafov6a40f092016-10-25 15:46:51 +010033import android.os.UserManagerInternal;
Mahaver Chopradea471e2015-12-17 11:02:37 +000034import android.telephony.SubscriptionInfo;
35import android.telephony.SubscriptionManager;
Makoto Onuki1a2cd742015-11-16 13:51:27 -080036import android.util.Log;
Makoto Onuki1f1ceef2016-01-28 11:32:32 -080037import android.util.Slog;
Pavel Grafov6a40f092016-10-25 15:46:51 +010038import android.util.SparseArray;
Makoto Onukia4f11972015-10-01 13:19:58 -070039
40import org.xmlpull.v1.XmlPullParser;
41import org.xmlpull.v1.XmlSerializer;
42
43import java.io.IOException;
44import java.io.PrintWriter;
Mahaver Chopradea471e2015-12-17 11:02:37 +000045import java.util.List;
Makoto Onukia4f11972015-10-01 13:19:58 -070046import java.util.Set;
47
Makoto Onukid45a4a22015-11-02 17:17:38 -080048/**
Mahaver Chopradea471e2015-12-17 11:02:37 +000049 * Utility methods for user restrictions.
Makoto Onukid45a4a22015-11-02 17:17:38 -080050 *
51 * <p>See {@link UserManagerService} for the method suffixes.
52 */
Makoto Onukia4f11972015-10-01 13:19:58 -070053public class UserRestrictionsUtils {
Makoto Onuki4f160732015-10-27 17:15:38 -070054 private static final String TAG = "UserRestrictionsUtils";
55
Makoto Onukia4f11972015-10-01 13:19:58 -070056 private UserRestrictionsUtils() {
57 }
58
Makoto Onuki1f1ceef2016-01-28 11:32:32 -080059 private static Set<String> newSetWithUniqueCheck(String[] strings) {
60 final Set<String> ret = Sets.newArraySet(strings);
61
62 // Make sure there's no overlap.
63 Preconditions.checkState(ret.size() == strings.length);
64 return ret;
65 }
66
67 public static final Set<String> USER_RESTRICTIONS = newSetWithUniqueCheck(new String[] {
Makoto Onukia4f11972015-10-01 13:19:58 -070068 UserManager.DISALLOW_CONFIG_WIFI,
69 UserManager.DISALLOW_MODIFY_ACCOUNTS,
70 UserManager.DISALLOW_INSTALL_APPS,
71 UserManager.DISALLOW_UNINSTALL_APPS,
72 UserManager.DISALLOW_SHARE_LOCATION,
73 UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES,
74 UserManager.DISALLOW_CONFIG_BLUETOOTH,
Lenka Trochtova63d5e4a72016-12-02 12:19:39 +010075 UserManager.DISALLOW_BLUETOOTH,
Pavel Grafov7f4ad752017-03-28 13:44:04 +010076 UserManager.DISALLOW_BLUETOOTH_SHARING,
Makoto Onukia4f11972015-10-01 13:19:58 -070077 UserManager.DISALLOW_USB_FILE_TRANSFER,
78 UserManager.DISALLOW_CONFIG_CREDENTIALS,
79 UserManager.DISALLOW_REMOVE_USER,
Esteban Talavera6c9116a2016-11-24 16:12:44 +000080 UserManager.DISALLOW_REMOVE_MANAGED_PROFILE,
Makoto Onukia4f11972015-10-01 13:19:58 -070081 UserManager.DISALLOW_DEBUGGING_FEATURES,
82 UserManager.DISALLOW_CONFIG_VPN,
83 UserManager.DISALLOW_CONFIG_TETHERING,
84 UserManager.DISALLOW_NETWORK_RESET,
85 UserManager.DISALLOW_FACTORY_RESET,
86 UserManager.DISALLOW_ADD_USER,
Esteban Talavera6c9116a2016-11-24 16:12:44 +000087 UserManager.DISALLOW_ADD_MANAGED_PROFILE,
Makoto Onukia4f11972015-10-01 13:19:58 -070088 UserManager.ENSURE_VERIFY_APPS,
89 UserManager.DISALLOW_CONFIG_CELL_BROADCASTS,
90 UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS,
91 UserManager.DISALLOW_APPS_CONTROL,
92 UserManager.DISALLOW_MOUNT_PHYSICAL_MEDIA,
93 UserManager.DISALLOW_UNMUTE_MICROPHONE,
94 UserManager.DISALLOW_ADJUST_VOLUME,
95 UserManager.DISALLOW_OUTGOING_CALLS,
96 UserManager.DISALLOW_SMS,
97 UserManager.DISALLOW_FUN,
98 UserManager.DISALLOW_CREATE_WINDOWS,
Charles He22ff6f9d2017-10-05 21:28:55 +010099 UserManager.DISALLOW_SYSTEM_ERROR_DIALOGS,
Makoto Onukia4f11972015-10-01 13:19:58 -0700100 UserManager.DISALLOW_CROSS_PROFILE_COPY_PASTE,
101 UserManager.DISALLOW_OUTGOING_BEAM,
102 UserManager.DISALLOW_WALLPAPER,
103 UserManager.DISALLOW_SAFE_BOOT,
104 UserManager.ALLOW_PARENT_PROFILE_APP_LINKING,
105 UserManager.DISALLOW_RECORD_AUDIO,
Fyodor Kupolov9cbfc9e2015-10-07 15:52:33 -0700106 UserManager.DISALLOW_CAMERA,
Mahaver Chopradea471e2015-12-17 11:02:37 +0000107 UserManager.DISALLOW_RUN_IN_BACKGROUND,
Oleksandr Peletskyi7f1f1df2016-01-18 15:40:21 +0100108 UserManager.DISALLOW_DATA_ROAMING,
Oleksandr Peletskyif2519812016-01-26 20:16:06 +0100109 UserManager.DISALLOW_SET_USER_ICON,
Mahaver Chopra3d9805d2016-07-07 16:25:05 +0100110 UserManager.DISALLOW_SET_WALLPAPER,
Tony Makc1205112016-07-22 16:02:59 +0100111 UserManager.DISALLOW_OEM_UNLOCK,
Esteban Talavera492b4722017-02-13 14:59:45 +0000112 UserManager.DISALLOW_UNMUTE_DEVICE,
Felipe Leme24d58932017-03-21 14:13:58 -0700113 UserManager.DISALLOW_AUTOFILL,
Makoto Onuki1f1ceef2016-01-28 11:32:32 -0800114 });
Makoto Onukia4f11972015-10-01 13:19:58 -0700115
116 /**
117 * Set of user restriction which we don't want to persist.
118 */
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800119 private static final Set<String> NON_PERSIST_USER_RESTRICTIONS = Sets.newArraySet(
120 UserManager.DISALLOW_RECORD_AUDIO
121 );
Makoto Onukia4f11972015-10-01 13:19:58 -0700122
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800123 /**
Pavel Grafov6a40f092016-10-25 15:46:51 +0100124 * User restrictions that cannot be set by profile owners of secondary users. When set by DO
125 * they will be applied to all users.
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800126 */
Pavel Grafov6a40f092016-10-25 15:46:51 +0100127 private static final Set<String> PRIMARY_USER_ONLY_RESTRICTIONS = Sets.newArraySet(
Lenka Trochtova63d5e4a72016-12-02 12:19:39 +0100128 UserManager.DISALLOW_BLUETOOTH,
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800129 UserManager.DISALLOW_USB_FILE_TRANSFER,
130 UserManager.DISALLOW_CONFIG_TETHERING,
131 UserManager.DISALLOW_NETWORK_RESET,
132 UserManager.DISALLOW_FACTORY_RESET,
133 UserManager.DISALLOW_ADD_USER,
134 UserManager.DISALLOW_CONFIG_CELL_BROADCASTS,
135 UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS,
136 UserManager.DISALLOW_MOUNT_PHYSICAL_MEDIA,
137 UserManager.DISALLOW_SMS,
138 UserManager.DISALLOW_FUN,
139 UserManager.DISALLOW_SAFE_BOOT,
Mahaver Chopradea471e2015-12-17 11:02:37 +0000140 UserManager.DISALLOW_CREATE_WINDOWS,
141 UserManager.DISALLOW_DATA_ROAMING
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800142 );
143
144 /**
145 * User restrictions that can't be changed by device owner or profile owner.
146 */
147 private static final Set<String> IMMUTABLE_BY_OWNERS = Sets.newArraySet(
148 UserManager.DISALLOW_RECORD_AUDIO,
Mahaver Chopra3d9805d2016-07-07 16:25:05 +0100149 UserManager.DISALLOW_WALLPAPER,
150 UserManager.DISALLOW_OEM_UNLOCK
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800151 );
152
153 /**
154 * Special user restrictions that can be applied to a user as well as to all users globally,
155 * depending on callers. When device owner sets them, they'll be applied to all users.
156 */
157 private static final Set<String> GLOBAL_RESTRICTIONS = Sets.newArraySet(
158 UserManager.DISALLOW_ADJUST_VOLUME,
Pavel Grafov7f4ad752017-03-28 13:44:04 +0100159 UserManager.DISALLOW_BLUETOOTH_SHARING,
Charles He22ff6f9d2017-10-05 21:28:55 +0100160 UserManager.DISALLOW_SYSTEM_ERROR_DIALOGS,
Fyodor Kupolov9cbfc9e2015-10-07 15:52:33 -0700161 UserManager.DISALLOW_RUN_IN_BACKGROUND,
Tony Makc1205112016-07-22 16:02:59 +0100162 UserManager.DISALLOW_UNMUTE_MICROPHONE,
Esteban Talavera492b4722017-02-13 14:59:45 +0000163 UserManager.DISALLOW_UNMUTE_DEVICE
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800164 );
165
Makoto Onuki1f1ceef2016-01-28 11:32:32 -0800166 /**
Esteban Talavera6c9116a2016-11-24 16:12:44 +0000167 * User restrictions that default to {@code true} for device owners.
168 */
169 private static final Set<String> DEFAULT_ENABLED_FOR_DEVICE_OWNERS = Sets.newArraySet(
170 UserManager.DISALLOW_ADD_MANAGED_PROFILE
171 );
172
Pavel Grafov7f4ad752017-03-28 13:44:04 +0100173 /**
174 * User restrictions that default to {@code true} for managed profile owners.
175 *
176 * NB: {@link UserManager#DISALLOW_INSTALL_UNKNOWN_SOURCES} is also set by default but it is
177 * not set to existing profile owners unless they used to have INSTALL_NON_MARKET_APPS disabled
178 * in settings. So it is handled separately.
179 */
180 private static final Set<String> DEFAULT_ENABLED_FOR_MANAGED_PROFILES = Sets.newArraySet(
181 UserManager.DISALLOW_BLUETOOTH_SHARING
182 );
183
Pavel Grafov6a40f092016-10-25 15:46:51 +0100184 /*
185 * Special user restrictions that are always applied to all users no matter who sets them.
186 */
187 private static final Set<String> PROFILE_GLOBAL_RESTRICTIONS = Sets.newArraySet(
188 UserManager.ENSURE_VERIFY_APPS
189 );
190
Esteban Talavera6c9116a2016-11-24 16:12:44 +0000191 /**
Makoto Onuki1f1ceef2016-01-28 11:32:32 -0800192 * Throws {@link IllegalArgumentException} if the given restriction name is invalid.
193 */
194 public static boolean isValidRestriction(@NonNull String restriction) {
195 if (!USER_RESTRICTIONS.contains(restriction)) {
Makoto Onukiad5619d2016-02-10 13:41:15 -0800196 Slog.e(TAG, "Unknown restriction: " + restriction);
Makoto Onuki1f1ceef2016-01-28 11:32:32 -0800197 return false;
198 }
199 return true;
200 }
201
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800202 public static void writeRestrictions(@NonNull XmlSerializer serializer,
203 @Nullable Bundle restrictions, @NonNull String tag) throws IOException {
204 if (restrictions == null) {
205 return;
206 }
207
Makoto Onukia4f11972015-10-01 13:19:58 -0700208 serializer.startTag(null, tag);
Makoto Onukiac65e1e2015-11-20 15:33:17 -0800209 for (String key : restrictions.keySet()) {
210 if (NON_PERSIST_USER_RESTRICTIONS.contains(key)) {
211 continue; // Don't persist.
Makoto Onukia4f11972015-10-01 13:19:58 -0700212 }
Makoto Onukiac65e1e2015-11-20 15:33:17 -0800213 if (USER_RESTRICTIONS.contains(key)) {
214 if (restrictions.getBoolean(key)) {
215 serializer.attribute(null, key, "true");
216 }
217 continue;
218 }
219 Log.w(TAG, "Unknown user restriction detected: " + key);
Makoto Onukia4f11972015-10-01 13:19:58 -0700220 }
221 serializer.endTag(null, tag);
222 }
223
Esteban Talaverac48b20f2016-08-11 11:23:40 +0100224 public static void readRestrictions(XmlPullParser parser, Bundle restrictions) {
Fyodor Kupoloveafee022017-03-15 17:09:04 -0700225 restrictions.clear();
Makoto Onukia4f11972015-10-01 13:19:58 -0700226 for (String key : USER_RESTRICTIONS) {
227 final String value = parser.getAttributeValue(null, key);
228 if (value != null) {
229 restrictions.putBoolean(key, Boolean.parseBoolean(value));
230 }
231 }
232 }
233
Pavel Grafov6a40f092016-10-25 15:46:51 +0100234 public static Bundle readRestrictions(XmlPullParser parser) {
235 final Bundle result = new Bundle();
236 readRestrictions(parser, result);
237 return result;
238 }
239
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800240 /**
241 * @return {@code in} itself when it's not null, or an empty bundle (which can writable).
242 */
243 public static Bundle nonNull(@Nullable Bundle in) {
244 return in != null ? in : new Bundle();
245 }
246
247 public static boolean isEmpty(@Nullable Bundle in) {
248 return (in == null) || (in.size() == 0);
249 }
250
251 /**
Pavel Grafov6a40f092016-10-25 15:46:51 +0100252 * Returns {@code true} if given bundle is not null and contains {@code true} for a given
253 * restriction.
254 */
255 public static boolean contains(@Nullable Bundle in, String restriction) {
256 return in != null && in.getBoolean(restriction);
257 }
258
259 /**
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800260 * Creates a copy of the {@code in} Bundle. If {@code in} is null, it'll return an empty
261 * bundle.
262 *
263 * <p>The resulting {@link Bundle} is always writable. (i.e. it won't return
264 * {@link Bundle#EMPTY})
265 */
266 public static @NonNull Bundle clone(@Nullable Bundle in) {
267 return (in != null) ? new Bundle(in) : new Bundle();
268 }
269
270 public static void merge(@NonNull Bundle dest, @Nullable Bundle in) {
271 Preconditions.checkNotNull(dest);
272 Preconditions.checkArgument(dest != in);
Makoto Onuki068c54a2015-10-13 14:34:03 -0700273 if (in == null) {
274 return;
275 }
276 for (String key : in.keySet()) {
277 if (in.getBoolean(key, false)) {
278 dest.putBoolean(key, true);
279 }
280 }
281 }
282
Makoto Onuki4f160732015-10-27 17:15:38 -0700283 /**
Pavel Grafov6a40f092016-10-25 15:46:51 +0100284 * Merges a sparse array of restrictions bundles into one.
285 */
286 @Nullable
287 public static Bundle mergeAll(SparseArray<Bundle> restrictions) {
288 if (restrictions.size() == 0) {
289 return null;
290 } else {
291 final Bundle result = new Bundle();
292 for (int i = 0; i < restrictions.size(); i++) {
293 merge(result, restrictions.valueAt(i));
294 }
295 return result;
296 }
297 }
298
299 /**
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800300 * @return true if a restriction is settable by device owner.
301 */
302 public static boolean canDeviceOwnerChange(String restriction) {
303 return !IMMUTABLE_BY_OWNERS.contains(restriction);
304 }
305
306 /**
Makoto Onuki5485ed42015-12-09 11:38:23 -0800307 * @return true if a restriction is settable by profile owner. Note it takes a user ID because
308 * some restrictions can be changed by PO only when it's running on the system user.
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800309 */
Makoto Onuki5485ed42015-12-09 11:38:23 -0800310 public static boolean canProfileOwnerChange(String restriction, int userId) {
311 return !IMMUTABLE_BY_OWNERS.contains(restriction)
312 && !(userId != UserHandle.USER_SYSTEM
Pavel Grafov6a40f092016-10-25 15:46:51 +0100313 && PRIMARY_USER_ONLY_RESTRICTIONS.contains(restriction));
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800314 }
315
316 /**
Esteban Talavera6c9116a2016-11-24 16:12:44 +0000317 * Returns the user restrictions that default to {@code true} for device owners.
Nicolas Prevot2ea46fe2017-01-05 10:29:34 +0000318 * These user restrictions are local, though. ie only for the device owner's user id.
Esteban Talavera6c9116a2016-11-24 16:12:44 +0000319 */
320 public static @NonNull Set<String> getDefaultEnabledForDeviceOwner() {
321 return DEFAULT_ENABLED_FOR_DEVICE_OWNERS;
322 }
323
324 /**
Pavel Grafov7f4ad752017-03-28 13:44:04 +0100325 * Returns the user restrictions that default to {@code true} for managed profile owners.
326 */
327 public static @NonNull Set<String> getDefaultEnabledForManagedProfiles() {
328 return DEFAULT_ENABLED_FOR_MANAGED_PROFILES;
329 }
330
331 /**
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800332 * Takes restrictions that can be set by device owner, and sort them into what should be applied
333 * globally and what should be applied only on the current user.
334 */
Pavel Grafov6a40f092016-10-25 15:46:51 +0100335 public static void sortToGlobalAndLocal(@Nullable Bundle in, boolean isDeviceOwner,
336 int cameraRestrictionScope,
337 @NonNull Bundle global, @NonNull Bundle local) {
338 // Camera restriction (as well as all others) goes to at most one bundle.
339 if (cameraRestrictionScope == UserManagerInternal.CAMERA_DISABLED_GLOBALLY) {
340 global.putBoolean(UserManager.DISALLOW_CAMERA, true);
341 } else if (cameraRestrictionScope == UserManagerInternal.CAMERA_DISABLED_LOCALLY) {
342 local.putBoolean(UserManager.DISALLOW_CAMERA, true);
343 }
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800344 if (in == null || in.size() == 0) {
345 return;
346 }
347 for (String key : in.keySet()) {
348 if (!in.getBoolean(key)) {
349 continue;
350 }
Pavel Grafov6a40f092016-10-25 15:46:51 +0100351 if (isGlobal(isDeviceOwner, key)) {
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800352 global.putBoolean(key, true);
353 } else {
354 local.putBoolean(key, true);
355 }
356 }
357 }
358
359 /**
Pavel Grafov6a40f092016-10-25 15:46:51 +0100360 * Whether given user restriction should be enforced globally.
361 */
362 private static boolean isGlobal(boolean isDeviceOwner, String key) {
363 return (isDeviceOwner &&
364 (PRIMARY_USER_ONLY_RESTRICTIONS.contains(key)|| GLOBAL_RESTRICTIONS.contains(key)))
365 || PROFILE_GLOBAL_RESTRICTIONS.contains(key);
366 }
367
368 /**
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800369 * @return true if two Bundles contain the same user restriction.
370 * A null bundle and an empty bundle are considered to be equal.
371 */
372 public static boolean areEqual(@Nullable Bundle a, @Nullable Bundle b) {
373 if (a == b) {
374 return true;
375 }
376 if (isEmpty(a)) {
377 return isEmpty(b);
378 }
379 if (isEmpty(b)) {
380 return false;
381 }
382 for (String key : a.keySet()) {
383 if (a.getBoolean(key) != b.getBoolean(key)) {
384 return false;
385 }
386 }
387 for (String key : b.keySet()) {
388 if (a.getBoolean(key) != b.getBoolean(key)) {
389 return false;
390 }
391 }
392 return true;
393 }
394
395 /**
Makoto Onuki4f160732015-10-27 17:15:38 -0700396 * Takes a new use restriction set and the previous set, and apply the restrictions that have
397 * changed.
Makoto Onukid45a4a22015-11-02 17:17:38 -0800398 *
Fyodor Kupolov9cbfc9e2015-10-07 15:52:33 -0700399 * <p>Note this method is called by {@link UserManagerService} without holding any locks.
Makoto Onuki4f160732015-10-27 17:15:38 -0700400 */
Fyodor Kupolov9cbfc9e2015-10-07 15:52:33 -0700401 public static void applyUserRestrictions(Context context, int userId,
Makoto Onukid45a4a22015-11-02 17:17:38 -0800402 Bundle newRestrictions, Bundle prevRestrictions) {
Makoto Onuki4f160732015-10-27 17:15:38 -0700403 for (String key : USER_RESTRICTIONS) {
404 final boolean newValue = newRestrictions.getBoolean(key);
405 final boolean prevValue = prevRestrictions.getBoolean(key);
406
407 if (newValue != prevValue) {
Fyodor Kupolov9cbfc9e2015-10-07 15:52:33 -0700408 applyUserRestriction(context, userId, key, newValue);
Makoto Onuki4f160732015-10-27 17:15:38 -0700409 }
410 }
411 }
Fyodor Kupolov9cbfc9e2015-10-07 15:52:33 -0700412
Makoto Onukid45a4a22015-11-02 17:17:38 -0800413 /**
414 * Apply each user restriction.
415 *
Makoto Onuki28da2e32015-11-20 11:30:44 -0800416 * <p>See also {@link
417 * com.android.providers.settings.SettingsProvider#isGlobalOrSecureSettingRestrictedForUser},
418 * which should be in sync with this method.
Makoto Onukid45a4a22015-11-02 17:17:38 -0800419 */
Fyodor Kupolov9cbfc9e2015-10-07 15:52:33 -0700420 private static void applyUserRestriction(Context context, int userId, String key,
Makoto Onuki4f160732015-10-27 17:15:38 -0700421 boolean newValue) {
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800422 if (UserManagerService.DBG) {
423 Log.d(TAG, "Applying user restriction: userId=" + userId
424 + " key=" + key + " value=" + newValue);
425 }
Makoto Onuki4f160732015-10-27 17:15:38 -0700426 // When certain restrictions are cleared, we don't update the system settings,
427 // because these settings are changeable on the Settings UI and we don't know the original
428 // value -- for example LOCATION_MODE might have been off already when the restriction was
429 // set, and in that case even if the restriction is lifted, changing it to ON would be
430 // wrong. So just don't do anything in such a case. If the user hopes to enable location
431 // later, they can do it on the Settings UI.
Benjamin Franz0ff13fc2016-07-12 13:42:21 +0100432 // WARNING: Remember that Settings.Global and Settings.Secure are changeable via adb.
433 // To prevent this from happening for a given user restriction, you have to add a check to
434 // SettingsProvider.isGlobalOrSecureSettingRestrictedForUser.
Makoto Onuki4f160732015-10-27 17:15:38 -0700435
436 final ContentResolver cr = context.getContentResolver();
437 final long id = Binder.clearCallingIdentity();
438 try {
439 switch (key) {
Mahaver Chopradea471e2015-12-17 11:02:37 +0000440 case UserManager.DISALLOW_DATA_ROAMING:
441 if (newValue) {
442 // DISALLOW_DATA_ROAMING user restriction is set.
443
444 // Multi sim device.
445 SubscriptionManager subscriptionManager = new SubscriptionManager(context);
446 final List<SubscriptionInfo> subscriptionInfoList =
447 subscriptionManager.getActiveSubscriptionInfoList();
448 if (subscriptionInfoList != null) {
449 for (SubscriptionInfo subInfo : subscriptionInfoList) {
450 android.provider.Settings.Global.putStringForUser(cr,
451 android.provider.Settings.Global.DATA_ROAMING
452 + subInfo.getSubscriptionId(), "0", userId);
453 }
454 }
455
456 // Single sim device.
457 android.provider.Settings.Global.putStringForUser(cr,
458 android.provider.Settings.Global.DATA_ROAMING, "0", userId);
459 }
460 break;
Makoto Onuki4f160732015-10-27 17:15:38 -0700461 case UserManager.DISALLOW_SHARE_LOCATION:
462 if (newValue) {
463 android.provider.Settings.Secure.putIntForUser(cr,
464 android.provider.Settings.Secure.LOCATION_MODE,
465 android.provider.Settings.Secure.LOCATION_MODE_OFF,
466 userId);
Makoto Onuki4f160732015-10-27 17:15:38 -0700467 }
Makoto Onuki4f160732015-10-27 17:15:38 -0700468 break;
469 case UserManager.DISALLOW_DEBUGGING_FEATURES:
470 if (newValue) {
471 // Only disable adb if changing for system user, since it is global
472 // TODO: should this be admin user?
473 if (userId == UserHandle.USER_SYSTEM) {
474 android.provider.Settings.Global.putStringForUser(cr,
475 android.provider.Settings.Global.ADB_ENABLED, "0",
476 userId);
477 }
478 }
479 break;
480 case UserManager.ENSURE_VERIFY_APPS:
481 if (newValue) {
482 android.provider.Settings.Global.putStringForUser(
483 context.getContentResolver(),
484 android.provider.Settings.Global.PACKAGE_VERIFIER_ENABLE, "1",
485 userId);
486 android.provider.Settings.Global.putStringForUser(
487 context.getContentResolver(),
488 android.provider.Settings.Global.PACKAGE_VERIFIER_INCLUDE_ADB, "1",
489 userId);
490 }
491 break;
492 case UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES:
Suprabh Shuklae3745ee2017-02-02 20:01:11 -0800493 // Since Android O, the secure setting is not available to be changed by the
494 // user. Hence, when the restriction is cleared, we need to reset the state of
495 // the setting to its default value which is now 1.
496 android.provider.Settings.Secure.putIntForUser(cr,
497 android.provider.Settings.Secure.INSTALL_NON_MARKET_APPS,
498 newValue ? 0 : 1, userId);
Makoto Onuki4f160732015-10-27 17:15:38 -0700499 break;
Fyodor Kupolov9cbfc9e2015-10-07 15:52:33 -0700500 case UserManager.DISALLOW_RUN_IN_BACKGROUND:
501 if (newValue) {
502 int currentUser = ActivityManager.getCurrentUser();
503 if (currentUser != userId && userId != UserHandle.USER_SYSTEM) {
504 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -0800505 ActivityManager.getService().stopUser(userId, false, null);
Fyodor Kupolov9cbfc9e2015-10-07 15:52:33 -0700506 } catch (RemoteException e) {
507 throw e.rethrowAsRuntimeException();
508 }
509 }
510 }
Lenka Trochtova6474f0e2016-03-24 16:43:10 +0100511 break;
512 case UserManager.DISALLOW_SAFE_BOOT:
513 // Unlike with the other restrictions, we want to propagate the new value to
514 // the system settings even if it is false. The other restrictions modify
515 // settings which could be manually changed by the user from the Settings app
516 // after the policies enforcing these restrictions have been revoked, so we
517 // leave re-setting of those settings to the user.
518 android.provider.Settings.Global.putInt(
519 context.getContentResolver(),
520 android.provider.Settings.Global.SAFE_BOOT_DISALLOWED,
521 newValue ? 1 : 0);
522 break;
Makoto Onuki4f160732015-10-27 17:15:38 -0700523 }
Makoto Onuki4f160732015-10-27 17:15:38 -0700524 } finally {
525 Binder.restoreCallingIdentity(id);
526 }
527 }
528
Makoto Onukia4f11972015-10-01 13:19:58 -0700529 public static void dumpRestrictions(PrintWriter pw, String prefix, Bundle restrictions) {
530 boolean noneSet = true;
531 if (restrictions != null) {
532 for (String key : restrictions.keySet()) {
533 if (restrictions.getBoolean(key, false)) {
534 pw.println(prefix + key);
535 noneSet = false;
536 }
537 }
Makoto Onuki068c54a2015-10-13 14:34:03 -0700538 if (noneSet) {
539 pw.println(prefix + "none");
540 }
541 } else {
542 pw.println(prefix + "null");
Makoto Onukia4f11972015-10-01 13:19:58 -0700543 }
544 }
Pavel Grafov6a40f092016-10-25 15:46:51 +0100545
546 /**
547 * Moves a particular restriction from one array of bundles to another, e.g. for all users.
548 */
549 public static void moveRestriction(String restrictionKey, SparseArray<Bundle> srcRestrictions,
550 SparseArray<Bundle> destRestrictions) {
551 for (int i = 0; i < srcRestrictions.size(); i++) {
Pavel Grafov7f4ad752017-03-28 13:44:04 +0100552 final int key = srcRestrictions.keyAt(i);
553 final Bundle from = srcRestrictions.valueAt(i);
Pavel Grafov6a40f092016-10-25 15:46:51 +0100554 if (contains(from, restrictionKey)) {
555 from.remove(restrictionKey);
556 Bundle to = destRestrictions.get(key);
557 if (to == null) {
558 to = new Bundle();
559 destRestrictions.append(key, to);
560 }
561 to.putBoolean(restrictionKey, true);
562 // Don't keep empty bundles.
563 if (from.isEmpty()) {
564 srcRestrictions.removeAt(i);
565 i--;
566 }
567 }
568 }
569 }
Pavel Grafov7f4ad752017-03-28 13:44:04 +0100570
571 /**
572 * Returns whether restrictions differ between two bundles.
573 * @param oldRestrictions old bundle of restrictions.
574 * @param newRestrictions new bundle of restrictions
575 * @param restrictions restrictions of interest, if empty, all restrictions are checked.
576 */
577 public static boolean restrictionsChanged(Bundle oldRestrictions, Bundle newRestrictions,
578 String... restrictions) {
579 if (restrictions.length == 0) {
580 return areEqual(oldRestrictions, newRestrictions);
581 }
582 for (final String restriction : restrictions) {
583 if (oldRestrictions.getBoolean(restriction, false) !=
584 newRestrictions.getBoolean(restriction, false)) {
585 return true;
586 }
587 }
588 return false;
589 }
Makoto Onukia4f11972015-10-01 13:19:58 -0700590}