blob: 84381fe20accac772c98cd90a58a2c6f02ae618d [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 Chopra830e32c2016-05-17 18:53:09 +010034import android.service.persistentdata.PersistentDataBlockManager;
Mahaver Chopradea471e2015-12-17 11:02:37 +000035import android.telephony.SubscriptionInfo;
36import android.telephony.SubscriptionManager;
Makoto Onuki1a2cd742015-11-16 13:51:27 -080037import android.util.Log;
Makoto Onuki1f1ceef2016-01-28 11:32:32 -080038import android.util.Slog;
Pavel Grafov6a40f092016-10-25 15:46:51 +010039import android.util.SparseArray;
Makoto Onukia4f11972015-10-01 13:19:58 -070040
41import org.xmlpull.v1.XmlPullParser;
42import org.xmlpull.v1.XmlSerializer;
43
44import java.io.IOException;
45import java.io.PrintWriter;
Mahaver Chopradea471e2015-12-17 11:02:37 +000046import java.util.List;
Makoto Onukia4f11972015-10-01 13:19:58 -070047import java.util.Set;
48
Makoto Onukid45a4a22015-11-02 17:17:38 -080049/**
Mahaver Chopradea471e2015-12-17 11:02:37 +000050 * Utility methods for user restrictions.
Makoto Onukid45a4a22015-11-02 17:17:38 -080051 *
52 * <p>See {@link UserManagerService} for the method suffixes.
53 */
Makoto Onukia4f11972015-10-01 13:19:58 -070054public class UserRestrictionsUtils {
Makoto Onuki4f160732015-10-27 17:15:38 -070055 private static final String TAG = "UserRestrictionsUtils";
56
Makoto Onukia4f11972015-10-01 13:19:58 -070057 private UserRestrictionsUtils() {
58 }
59
Makoto Onuki1f1ceef2016-01-28 11:32:32 -080060 private static Set<String> newSetWithUniqueCheck(String[] strings) {
61 final Set<String> ret = Sets.newArraySet(strings);
62
63 // Make sure there's no overlap.
64 Preconditions.checkState(ret.size() == strings.length);
65 return ret;
66 }
67
68 public static final Set<String> USER_RESTRICTIONS = newSetWithUniqueCheck(new String[] {
Makoto Onukia4f11972015-10-01 13:19:58 -070069 UserManager.DISALLOW_CONFIG_WIFI,
70 UserManager.DISALLOW_MODIFY_ACCOUNTS,
71 UserManager.DISALLOW_INSTALL_APPS,
72 UserManager.DISALLOW_UNINSTALL_APPS,
73 UserManager.DISALLOW_SHARE_LOCATION,
74 UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES,
75 UserManager.DISALLOW_CONFIG_BLUETOOTH,
Lenka Trochtova63d5e4a72016-12-02 12:19:39 +010076 UserManager.DISALLOW_BLUETOOTH,
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,
99 UserManager.DISALLOW_CROSS_PROFILE_COPY_PASTE,
100 UserManager.DISALLOW_OUTGOING_BEAM,
101 UserManager.DISALLOW_WALLPAPER,
102 UserManager.DISALLOW_SAFE_BOOT,
103 UserManager.ALLOW_PARENT_PROFILE_APP_LINKING,
104 UserManager.DISALLOW_RECORD_AUDIO,
Fyodor Kupolov9cbfc9e2015-10-07 15:52:33 -0700105 UserManager.DISALLOW_CAMERA,
Mahaver Chopradea471e2015-12-17 11:02:37 +0000106 UserManager.DISALLOW_RUN_IN_BACKGROUND,
Oleksandr Peletskyi7f1f1df2016-01-18 15:40:21 +0100107 UserManager.DISALLOW_DATA_ROAMING,
Oleksandr Peletskyif2519812016-01-26 20:16:06 +0100108 UserManager.DISALLOW_SET_USER_ICON,
Mahaver Chopra3d9805d2016-07-07 16:25:05 +0100109 UserManager.DISALLOW_SET_WALLPAPER,
Tony Makc1205112016-07-22 16:02:59 +0100110 UserManager.DISALLOW_OEM_UNLOCK,
Esteban Talavera492b4722017-02-13 14:59:45 +0000111 UserManager.DISALLOW_UNMUTE_DEVICE,
Felipe Leme24d58932017-03-21 14:13:58 -0700112 UserManager.DISALLOW_AUTOFILL,
Makoto Onuki1f1ceef2016-01-28 11:32:32 -0800113 });
Makoto Onukia4f11972015-10-01 13:19:58 -0700114
115 /**
116 * Set of user restriction which we don't want to persist.
117 */
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800118 private static final Set<String> NON_PERSIST_USER_RESTRICTIONS = Sets.newArraySet(
119 UserManager.DISALLOW_RECORD_AUDIO
120 );
Makoto Onukia4f11972015-10-01 13:19:58 -0700121
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800122 /**
Pavel Grafov6a40f092016-10-25 15:46:51 +0100123 * User restrictions that cannot be set by profile owners of secondary users. When set by DO
124 * they will be applied to all users.
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800125 */
Pavel Grafov6a40f092016-10-25 15:46:51 +0100126 private static final Set<String> PRIMARY_USER_ONLY_RESTRICTIONS = Sets.newArraySet(
Lenka Trochtova63d5e4a72016-12-02 12:19:39 +0100127 UserManager.DISALLOW_BLUETOOTH,
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800128 UserManager.DISALLOW_USB_FILE_TRANSFER,
129 UserManager.DISALLOW_CONFIG_TETHERING,
130 UserManager.DISALLOW_NETWORK_RESET,
131 UserManager.DISALLOW_FACTORY_RESET,
132 UserManager.DISALLOW_ADD_USER,
133 UserManager.DISALLOW_CONFIG_CELL_BROADCASTS,
134 UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS,
135 UserManager.DISALLOW_MOUNT_PHYSICAL_MEDIA,
136 UserManager.DISALLOW_SMS,
137 UserManager.DISALLOW_FUN,
138 UserManager.DISALLOW_SAFE_BOOT,
Mahaver Chopradea471e2015-12-17 11:02:37 +0000139 UserManager.DISALLOW_CREATE_WINDOWS,
140 UserManager.DISALLOW_DATA_ROAMING
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800141 );
142
143 /**
144 * User restrictions that can't be changed by device owner or profile owner.
145 */
146 private static final Set<String> IMMUTABLE_BY_OWNERS = Sets.newArraySet(
147 UserManager.DISALLOW_RECORD_AUDIO,
Mahaver Chopra3d9805d2016-07-07 16:25:05 +0100148 UserManager.DISALLOW_WALLPAPER,
149 UserManager.DISALLOW_OEM_UNLOCK
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800150 );
151
152 /**
153 * Special user restrictions that can be applied to a user as well as to all users globally,
154 * depending on callers. When device owner sets them, they'll be applied to all users.
155 */
156 private static final Set<String> GLOBAL_RESTRICTIONS = Sets.newArraySet(
157 UserManager.DISALLOW_ADJUST_VOLUME,
Fyodor Kupolov9cbfc9e2015-10-07 15:52:33 -0700158 UserManager.DISALLOW_RUN_IN_BACKGROUND,
Tony Makc1205112016-07-22 16:02:59 +0100159 UserManager.DISALLOW_UNMUTE_MICROPHONE,
Esteban Talavera492b4722017-02-13 14:59:45 +0000160 UserManager.DISALLOW_UNMUTE_DEVICE
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800161 );
162
Makoto Onuki1f1ceef2016-01-28 11:32:32 -0800163 /**
Esteban Talavera6c9116a2016-11-24 16:12:44 +0000164 * User restrictions that default to {@code true} for device owners.
165 */
166 private static final Set<String> DEFAULT_ENABLED_FOR_DEVICE_OWNERS = Sets.newArraySet(
167 UserManager.DISALLOW_ADD_MANAGED_PROFILE
168 );
169
Pavel Grafov6a40f092016-10-25 15:46:51 +0100170 /*
171 * Special user restrictions that are always applied to all users no matter who sets them.
172 */
173 private static final Set<String> PROFILE_GLOBAL_RESTRICTIONS = Sets.newArraySet(
174 UserManager.ENSURE_VERIFY_APPS
175 );
176
Esteban Talavera6c9116a2016-11-24 16:12:44 +0000177 /**
Makoto Onuki1f1ceef2016-01-28 11:32:32 -0800178 * Throws {@link IllegalArgumentException} if the given restriction name is invalid.
179 */
180 public static boolean isValidRestriction(@NonNull String restriction) {
181 if (!USER_RESTRICTIONS.contains(restriction)) {
Makoto Onukiad5619d2016-02-10 13:41:15 -0800182 Slog.e(TAG, "Unknown restriction: " + restriction);
Makoto Onuki1f1ceef2016-01-28 11:32:32 -0800183 return false;
184 }
185 return true;
186 }
187
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800188 public static void writeRestrictions(@NonNull XmlSerializer serializer,
189 @Nullable Bundle restrictions, @NonNull String tag) throws IOException {
190 if (restrictions == null) {
191 return;
192 }
193
Makoto Onukia4f11972015-10-01 13:19:58 -0700194 serializer.startTag(null, tag);
Makoto Onukiac65e1e2015-11-20 15:33:17 -0800195 for (String key : restrictions.keySet()) {
196 if (NON_PERSIST_USER_RESTRICTIONS.contains(key)) {
197 continue; // Don't persist.
Makoto Onukia4f11972015-10-01 13:19:58 -0700198 }
Makoto Onukiac65e1e2015-11-20 15:33:17 -0800199 if (USER_RESTRICTIONS.contains(key)) {
200 if (restrictions.getBoolean(key)) {
201 serializer.attribute(null, key, "true");
202 }
203 continue;
204 }
205 Log.w(TAG, "Unknown user restriction detected: " + key);
Makoto Onukia4f11972015-10-01 13:19:58 -0700206 }
207 serializer.endTag(null, tag);
208 }
209
Esteban Talaverac48b20f2016-08-11 11:23:40 +0100210 public static void readRestrictions(XmlPullParser parser, Bundle restrictions) {
Fyodor Kupoloveafee022017-03-15 17:09:04 -0700211 restrictions.clear();
Makoto Onukia4f11972015-10-01 13:19:58 -0700212 for (String key : USER_RESTRICTIONS) {
213 final String value = parser.getAttributeValue(null, key);
214 if (value != null) {
215 restrictions.putBoolean(key, Boolean.parseBoolean(value));
216 }
217 }
218 }
219
Pavel Grafov6a40f092016-10-25 15:46:51 +0100220 public static Bundle readRestrictions(XmlPullParser parser) {
221 final Bundle result = new Bundle();
222 readRestrictions(parser, result);
223 return result;
224 }
225
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800226 /**
227 * @return {@code in} itself when it's not null, or an empty bundle (which can writable).
228 */
229 public static Bundle nonNull(@Nullable Bundle in) {
230 return in != null ? in : new Bundle();
231 }
232
233 public static boolean isEmpty(@Nullable Bundle in) {
234 return (in == null) || (in.size() == 0);
235 }
236
237 /**
Pavel Grafov6a40f092016-10-25 15:46:51 +0100238 * Returns {@code true} if given bundle is not null and contains {@code true} for a given
239 * restriction.
240 */
241 public static boolean contains(@Nullable Bundle in, String restriction) {
242 return in != null && in.getBoolean(restriction);
243 }
244
245 /**
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800246 * Creates a copy of the {@code in} Bundle. If {@code in} is null, it'll return an empty
247 * bundle.
248 *
249 * <p>The resulting {@link Bundle} is always writable. (i.e. it won't return
250 * {@link Bundle#EMPTY})
251 */
252 public static @NonNull Bundle clone(@Nullable Bundle in) {
253 return (in != null) ? new Bundle(in) : new Bundle();
254 }
255
256 public static void merge(@NonNull Bundle dest, @Nullable Bundle in) {
257 Preconditions.checkNotNull(dest);
258 Preconditions.checkArgument(dest != in);
Makoto Onuki068c54a2015-10-13 14:34:03 -0700259 if (in == null) {
260 return;
261 }
262 for (String key : in.keySet()) {
263 if (in.getBoolean(key, false)) {
264 dest.putBoolean(key, true);
265 }
266 }
267 }
268
Makoto Onuki4f160732015-10-27 17:15:38 -0700269 /**
Pavel Grafov6a40f092016-10-25 15:46:51 +0100270 * Merges a sparse array of restrictions bundles into one.
271 */
272 @Nullable
273 public static Bundle mergeAll(SparseArray<Bundle> restrictions) {
274 if (restrictions.size() == 0) {
275 return null;
276 } else {
277 final Bundle result = new Bundle();
278 for (int i = 0; i < restrictions.size(); i++) {
279 merge(result, restrictions.valueAt(i));
280 }
281 return result;
282 }
283 }
284
285 /**
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800286 * @return true if a restriction is settable by device owner.
287 */
288 public static boolean canDeviceOwnerChange(String restriction) {
289 return !IMMUTABLE_BY_OWNERS.contains(restriction);
290 }
291
292 /**
Makoto Onuki5485ed42015-12-09 11:38:23 -0800293 * @return true if a restriction is settable by profile owner. Note it takes a user ID because
294 * some restrictions can be changed by PO only when it's running on the system user.
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800295 */
Makoto Onuki5485ed42015-12-09 11:38:23 -0800296 public static boolean canProfileOwnerChange(String restriction, int userId) {
297 return !IMMUTABLE_BY_OWNERS.contains(restriction)
298 && !(userId != UserHandle.USER_SYSTEM
Pavel Grafov6a40f092016-10-25 15:46:51 +0100299 && PRIMARY_USER_ONLY_RESTRICTIONS.contains(restriction));
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800300 }
301
302 /**
Esteban Talavera6c9116a2016-11-24 16:12:44 +0000303 * Returns the user restrictions that default to {@code true} for device owners.
Nicolas Prevot2ea46fe2017-01-05 10:29:34 +0000304 * These user restrictions are local, though. ie only for the device owner's user id.
Esteban Talavera6c9116a2016-11-24 16:12:44 +0000305 */
306 public static @NonNull Set<String> getDefaultEnabledForDeviceOwner() {
307 return DEFAULT_ENABLED_FOR_DEVICE_OWNERS;
308 }
309
310 /**
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800311 * Takes restrictions that can be set by device owner, and sort them into what should be applied
312 * globally and what should be applied only on the current user.
313 */
Pavel Grafov6a40f092016-10-25 15:46:51 +0100314 public static void sortToGlobalAndLocal(@Nullable Bundle in, boolean isDeviceOwner,
315 int cameraRestrictionScope,
316 @NonNull Bundle global, @NonNull Bundle local) {
317 // Camera restriction (as well as all others) goes to at most one bundle.
318 if (cameraRestrictionScope == UserManagerInternal.CAMERA_DISABLED_GLOBALLY) {
319 global.putBoolean(UserManager.DISALLOW_CAMERA, true);
320 } else if (cameraRestrictionScope == UserManagerInternal.CAMERA_DISABLED_LOCALLY) {
321 local.putBoolean(UserManager.DISALLOW_CAMERA, true);
322 }
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800323 if (in == null || in.size() == 0) {
324 return;
325 }
326 for (String key : in.keySet()) {
327 if (!in.getBoolean(key)) {
328 continue;
329 }
Pavel Grafov6a40f092016-10-25 15:46:51 +0100330 if (isGlobal(isDeviceOwner, key)) {
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800331 global.putBoolean(key, true);
332 } else {
333 local.putBoolean(key, true);
334 }
335 }
336 }
337
338 /**
Pavel Grafov6a40f092016-10-25 15:46:51 +0100339 * Whether given user restriction should be enforced globally.
340 */
341 private static boolean isGlobal(boolean isDeviceOwner, String key) {
342 return (isDeviceOwner &&
343 (PRIMARY_USER_ONLY_RESTRICTIONS.contains(key)|| GLOBAL_RESTRICTIONS.contains(key)))
344 || PROFILE_GLOBAL_RESTRICTIONS.contains(key);
345 }
346
347 /**
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800348 * @return true if two Bundles contain the same user restriction.
349 * A null bundle and an empty bundle are considered to be equal.
350 */
351 public static boolean areEqual(@Nullable Bundle a, @Nullable Bundle b) {
352 if (a == b) {
353 return true;
354 }
355 if (isEmpty(a)) {
356 return isEmpty(b);
357 }
358 if (isEmpty(b)) {
359 return false;
360 }
361 for (String key : a.keySet()) {
362 if (a.getBoolean(key) != b.getBoolean(key)) {
363 return false;
364 }
365 }
366 for (String key : b.keySet()) {
367 if (a.getBoolean(key) != b.getBoolean(key)) {
368 return false;
369 }
370 }
371 return true;
372 }
373
374 /**
Makoto Onuki4f160732015-10-27 17:15:38 -0700375 * Takes a new use restriction set and the previous set, and apply the restrictions that have
376 * changed.
Makoto Onukid45a4a22015-11-02 17:17:38 -0800377 *
Fyodor Kupolov9cbfc9e2015-10-07 15:52:33 -0700378 * <p>Note this method is called by {@link UserManagerService} without holding any locks.
Makoto Onuki4f160732015-10-27 17:15:38 -0700379 */
Fyodor Kupolov9cbfc9e2015-10-07 15:52:33 -0700380 public static void applyUserRestrictions(Context context, int userId,
Makoto Onukid45a4a22015-11-02 17:17:38 -0800381 Bundle newRestrictions, Bundle prevRestrictions) {
Makoto Onuki4f160732015-10-27 17:15:38 -0700382 for (String key : USER_RESTRICTIONS) {
383 final boolean newValue = newRestrictions.getBoolean(key);
384 final boolean prevValue = prevRestrictions.getBoolean(key);
385
386 if (newValue != prevValue) {
Fyodor Kupolov9cbfc9e2015-10-07 15:52:33 -0700387 applyUserRestriction(context, userId, key, newValue);
Makoto Onuki4f160732015-10-27 17:15:38 -0700388 }
389 }
390 }
Fyodor Kupolov9cbfc9e2015-10-07 15:52:33 -0700391
Makoto Onukid45a4a22015-11-02 17:17:38 -0800392 /**
393 * Apply each user restriction.
394 *
Makoto Onuki28da2e32015-11-20 11:30:44 -0800395 * <p>See also {@link
396 * com.android.providers.settings.SettingsProvider#isGlobalOrSecureSettingRestrictedForUser},
397 * which should be in sync with this method.
Makoto Onukid45a4a22015-11-02 17:17:38 -0800398 */
Fyodor Kupolov9cbfc9e2015-10-07 15:52:33 -0700399 private static void applyUserRestriction(Context context, int userId, String key,
Makoto Onuki4f160732015-10-27 17:15:38 -0700400 boolean newValue) {
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800401 if (UserManagerService.DBG) {
402 Log.d(TAG, "Applying user restriction: userId=" + userId
403 + " key=" + key + " value=" + newValue);
404 }
Makoto Onuki4f160732015-10-27 17:15:38 -0700405 // When certain restrictions are cleared, we don't update the system settings,
406 // because these settings are changeable on the Settings UI and we don't know the original
407 // value -- for example LOCATION_MODE might have been off already when the restriction was
408 // set, and in that case even if the restriction is lifted, changing it to ON would be
409 // wrong. So just don't do anything in such a case. If the user hopes to enable location
410 // later, they can do it on the Settings UI.
Benjamin Franz0ff13fc2016-07-12 13:42:21 +0100411 // WARNING: Remember that Settings.Global and Settings.Secure are changeable via adb.
412 // To prevent this from happening for a given user restriction, you have to add a check to
413 // SettingsProvider.isGlobalOrSecureSettingRestrictedForUser.
Makoto Onuki4f160732015-10-27 17:15:38 -0700414
415 final ContentResolver cr = context.getContentResolver();
416 final long id = Binder.clearCallingIdentity();
417 try {
418 switch (key) {
Mahaver Chopradea471e2015-12-17 11:02:37 +0000419 case UserManager.DISALLOW_DATA_ROAMING:
420 if (newValue) {
421 // DISALLOW_DATA_ROAMING user restriction is set.
422
423 // Multi sim device.
424 SubscriptionManager subscriptionManager = new SubscriptionManager(context);
425 final List<SubscriptionInfo> subscriptionInfoList =
426 subscriptionManager.getActiveSubscriptionInfoList();
427 if (subscriptionInfoList != null) {
428 for (SubscriptionInfo subInfo : subscriptionInfoList) {
429 android.provider.Settings.Global.putStringForUser(cr,
430 android.provider.Settings.Global.DATA_ROAMING
431 + subInfo.getSubscriptionId(), "0", userId);
432 }
433 }
434
435 // Single sim device.
436 android.provider.Settings.Global.putStringForUser(cr,
437 android.provider.Settings.Global.DATA_ROAMING, "0", userId);
438 }
439 break;
Makoto Onuki4f160732015-10-27 17:15:38 -0700440 case UserManager.DISALLOW_SHARE_LOCATION:
441 if (newValue) {
442 android.provider.Settings.Secure.putIntForUser(cr,
443 android.provider.Settings.Secure.LOCATION_MODE,
444 android.provider.Settings.Secure.LOCATION_MODE_OFF,
445 userId);
Makoto Onuki4f160732015-10-27 17:15:38 -0700446 }
Makoto Onuki4f160732015-10-27 17:15:38 -0700447 break;
448 case UserManager.DISALLOW_DEBUGGING_FEATURES:
449 if (newValue) {
450 // Only disable adb if changing for system user, since it is global
451 // TODO: should this be admin user?
452 if (userId == UserHandle.USER_SYSTEM) {
453 android.provider.Settings.Global.putStringForUser(cr,
454 android.provider.Settings.Global.ADB_ENABLED, "0",
455 userId);
456 }
457 }
458 break;
459 case UserManager.ENSURE_VERIFY_APPS:
460 if (newValue) {
461 android.provider.Settings.Global.putStringForUser(
462 context.getContentResolver(),
463 android.provider.Settings.Global.PACKAGE_VERIFIER_ENABLE, "1",
464 userId);
465 android.provider.Settings.Global.putStringForUser(
466 context.getContentResolver(),
467 android.provider.Settings.Global.PACKAGE_VERIFIER_INCLUDE_ADB, "1",
468 userId);
469 }
470 break;
471 case UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES:
Suprabh Shuklae3745ee2017-02-02 20:01:11 -0800472 // Since Android O, the secure setting is not available to be changed by the
473 // user. Hence, when the restriction is cleared, we need to reset the state of
474 // the setting to its default value which is now 1.
475 android.provider.Settings.Secure.putIntForUser(cr,
476 android.provider.Settings.Secure.INSTALL_NON_MARKET_APPS,
477 newValue ? 0 : 1, userId);
Makoto Onuki4f160732015-10-27 17:15:38 -0700478 break;
Fyodor Kupolov9cbfc9e2015-10-07 15:52:33 -0700479 case UserManager.DISALLOW_RUN_IN_BACKGROUND:
480 if (newValue) {
481 int currentUser = ActivityManager.getCurrentUser();
482 if (currentUser != userId && userId != UserHandle.USER_SYSTEM) {
483 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -0800484 ActivityManager.getService().stopUser(userId, false, null);
Fyodor Kupolov9cbfc9e2015-10-07 15:52:33 -0700485 } catch (RemoteException e) {
486 throw e.rethrowAsRuntimeException();
487 }
488 }
489 }
Lenka Trochtova6474f0e2016-03-24 16:43:10 +0100490 break;
491 case UserManager.DISALLOW_SAFE_BOOT:
492 // Unlike with the other restrictions, we want to propagate the new value to
493 // the system settings even if it is false. The other restrictions modify
494 // settings which could be manually changed by the user from the Settings app
495 // after the policies enforcing these restrictions have been revoked, so we
496 // leave re-setting of those settings to the user.
497 android.provider.Settings.Global.putInt(
498 context.getContentResolver(),
499 android.provider.Settings.Global.SAFE_BOOT_DISALLOWED,
500 newValue ? 1 : 0);
501 break;
Mahaver Chopra830e32c2016-05-17 18:53:09 +0100502 case UserManager.DISALLOW_FACTORY_RESET:
Mahaver Chopra3d9805d2016-07-07 16:25:05 +0100503 case UserManager.DISALLOW_OEM_UNLOCK:
Mahaver Chopra830e32c2016-05-17 18:53:09 +0100504 if (newValue) {
505 PersistentDataBlockManager manager = (PersistentDataBlockManager) context
506 .getSystemService(Context.PERSISTENT_DATA_BLOCK_SERVICE);
Esteban Talaverac48b20f2016-08-11 11:23:40 +0100507 if (manager != null
508 && manager.getOemUnlockEnabled()
509 && manager.getFlashLockState()
510 != PersistentDataBlockManager.FLASH_LOCK_UNLOCKED) {
511 // Only disable OEM unlock if the bootloader is locked. If it's already
512 // unlocked, setting the OEM unlock enabled flag to false has no effect
513 // (the bootloader would remain unlocked).
Mahaver Chopra830e32c2016-05-17 18:53:09 +0100514 manager.setOemUnlockEnabled(false);
515 }
516 }
Tony Makc1205112016-07-22 16:02:59 +0100517 break;
Makoto Onuki4f160732015-10-27 17:15:38 -0700518 }
Makoto Onuki4f160732015-10-27 17:15:38 -0700519 } finally {
520 Binder.restoreCallingIdentity(id);
521 }
522 }
523
Makoto Onukia4f11972015-10-01 13:19:58 -0700524 public static void dumpRestrictions(PrintWriter pw, String prefix, Bundle restrictions) {
525 boolean noneSet = true;
526 if (restrictions != null) {
527 for (String key : restrictions.keySet()) {
528 if (restrictions.getBoolean(key, false)) {
529 pw.println(prefix + key);
530 noneSet = false;
531 }
532 }
Makoto Onuki068c54a2015-10-13 14:34:03 -0700533 if (noneSet) {
534 pw.println(prefix + "none");
535 }
536 } else {
537 pw.println(prefix + "null");
Makoto Onukia4f11972015-10-01 13:19:58 -0700538 }
539 }
Pavel Grafov6a40f092016-10-25 15:46:51 +0100540
541 /**
542 * Moves a particular restriction from one array of bundles to another, e.g. for all users.
543 */
544 public static void moveRestriction(String restrictionKey, SparseArray<Bundle> srcRestrictions,
545 SparseArray<Bundle> destRestrictions) {
546 for (int i = 0; i < srcRestrictions.size(); i++) {
547 int key = srcRestrictions.keyAt(i);
548 Bundle from = srcRestrictions.valueAt(i);
549 if (contains(from, restrictionKey)) {
550 from.remove(restrictionKey);
551 Bundle to = destRestrictions.get(key);
552 if (to == null) {
553 to = new Bundle();
554 destRestrictions.append(key, to);
555 }
556 to.putBoolean(restrictionKey, true);
557 // Don't keep empty bundles.
558 if (from.isEmpty()) {
559 srcRestrictions.removeAt(i);
560 i--;
561 }
562 }
563 }
564 }
Makoto Onukia4f11972015-10-01 13:19:58 -0700565}