blob: cb2ed6e0d00f0b9a54409539df633bcc623684e2 [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,
Makoto Onuki1f1ceef2016-01-28 11:32:32 -0800112 });
Makoto Onukia4f11972015-10-01 13:19:58 -0700113
114 /**
115 * Set of user restriction which we don't want to persist.
116 */
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800117 private static final Set<String> NON_PERSIST_USER_RESTRICTIONS = Sets.newArraySet(
118 UserManager.DISALLOW_RECORD_AUDIO
119 );
Makoto Onukia4f11972015-10-01 13:19:58 -0700120
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800121 /**
Pavel Grafov6a40f092016-10-25 15:46:51 +0100122 * User restrictions that cannot be set by profile owners of secondary users. When set by DO
123 * they will be applied to all users.
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800124 */
Pavel Grafov6a40f092016-10-25 15:46:51 +0100125 private static final Set<String> PRIMARY_USER_ONLY_RESTRICTIONS = Sets.newArraySet(
Lenka Trochtova63d5e4a72016-12-02 12:19:39 +0100126 UserManager.DISALLOW_BLUETOOTH,
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800127 UserManager.DISALLOW_USB_FILE_TRANSFER,
128 UserManager.DISALLOW_CONFIG_TETHERING,
129 UserManager.DISALLOW_NETWORK_RESET,
130 UserManager.DISALLOW_FACTORY_RESET,
131 UserManager.DISALLOW_ADD_USER,
132 UserManager.DISALLOW_CONFIG_CELL_BROADCASTS,
133 UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS,
134 UserManager.DISALLOW_MOUNT_PHYSICAL_MEDIA,
135 UserManager.DISALLOW_SMS,
136 UserManager.DISALLOW_FUN,
137 UserManager.DISALLOW_SAFE_BOOT,
Mahaver Chopradea471e2015-12-17 11:02:37 +0000138 UserManager.DISALLOW_CREATE_WINDOWS,
139 UserManager.DISALLOW_DATA_ROAMING
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800140 );
141
142 /**
143 * User restrictions that can't be changed by device owner or profile owner.
144 */
145 private static final Set<String> IMMUTABLE_BY_OWNERS = Sets.newArraySet(
146 UserManager.DISALLOW_RECORD_AUDIO,
Mahaver Chopra3d9805d2016-07-07 16:25:05 +0100147 UserManager.DISALLOW_WALLPAPER,
148 UserManager.DISALLOW_OEM_UNLOCK
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800149 );
150
151 /**
152 * Special user restrictions that can be applied to a user as well as to all users globally,
153 * depending on callers. When device owner sets them, they'll be applied to all users.
154 */
155 private static final Set<String> GLOBAL_RESTRICTIONS = Sets.newArraySet(
156 UserManager.DISALLOW_ADJUST_VOLUME,
Fyodor Kupolov9cbfc9e2015-10-07 15:52:33 -0700157 UserManager.DISALLOW_RUN_IN_BACKGROUND,
Tony Makc1205112016-07-22 16:02:59 +0100158 UserManager.DISALLOW_UNMUTE_MICROPHONE,
Esteban Talavera492b4722017-02-13 14:59:45 +0000159 UserManager.DISALLOW_UNMUTE_DEVICE
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800160 );
161
Makoto Onuki1f1ceef2016-01-28 11:32:32 -0800162 /**
Esteban Talavera6c9116a2016-11-24 16:12:44 +0000163 * User restrictions that default to {@code true} for device owners.
164 */
165 private static final Set<String> DEFAULT_ENABLED_FOR_DEVICE_OWNERS = Sets.newArraySet(
166 UserManager.DISALLOW_ADD_MANAGED_PROFILE
167 );
168
Pavel Grafov6a40f092016-10-25 15:46:51 +0100169 /*
170 * Special user restrictions that are always applied to all users no matter who sets them.
171 */
172 private static final Set<String> PROFILE_GLOBAL_RESTRICTIONS = Sets.newArraySet(
173 UserManager.ENSURE_VERIFY_APPS
174 );
175
Esteban Talavera6c9116a2016-11-24 16:12:44 +0000176 /**
Makoto Onuki1f1ceef2016-01-28 11:32:32 -0800177 * Throws {@link IllegalArgumentException} if the given restriction name is invalid.
178 */
179 public static boolean isValidRestriction(@NonNull String restriction) {
180 if (!USER_RESTRICTIONS.contains(restriction)) {
Makoto Onukiad5619d2016-02-10 13:41:15 -0800181 Slog.e(TAG, "Unknown restriction: " + restriction);
Makoto Onuki1f1ceef2016-01-28 11:32:32 -0800182 return false;
183 }
184 return true;
185 }
186
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800187 public static void writeRestrictions(@NonNull XmlSerializer serializer,
188 @Nullable Bundle restrictions, @NonNull String tag) throws IOException {
189 if (restrictions == null) {
190 return;
191 }
192
Makoto Onukia4f11972015-10-01 13:19:58 -0700193 serializer.startTag(null, tag);
Makoto Onukiac65e1e2015-11-20 15:33:17 -0800194 for (String key : restrictions.keySet()) {
195 if (NON_PERSIST_USER_RESTRICTIONS.contains(key)) {
196 continue; // Don't persist.
Makoto Onukia4f11972015-10-01 13:19:58 -0700197 }
Makoto Onukiac65e1e2015-11-20 15:33:17 -0800198 if (USER_RESTRICTIONS.contains(key)) {
199 if (restrictions.getBoolean(key)) {
200 serializer.attribute(null, key, "true");
201 }
202 continue;
203 }
204 Log.w(TAG, "Unknown user restriction detected: " + key);
Makoto Onukia4f11972015-10-01 13:19:58 -0700205 }
206 serializer.endTag(null, tag);
207 }
208
Esteban Talaverac48b20f2016-08-11 11:23:40 +0100209 public static void readRestrictions(XmlPullParser parser, Bundle restrictions) {
Fyodor Kupoloveafee022017-03-15 17:09:04 -0700210 restrictions.clear();
Makoto Onukia4f11972015-10-01 13:19:58 -0700211 for (String key : USER_RESTRICTIONS) {
212 final String value = parser.getAttributeValue(null, key);
213 if (value != null) {
214 restrictions.putBoolean(key, Boolean.parseBoolean(value));
215 }
216 }
217 }
218
Pavel Grafov6a40f092016-10-25 15:46:51 +0100219 public static Bundle readRestrictions(XmlPullParser parser) {
220 final Bundle result = new Bundle();
221 readRestrictions(parser, result);
222 return result;
223 }
224
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800225 /**
226 * @return {@code in} itself when it's not null, or an empty bundle (which can writable).
227 */
228 public static Bundle nonNull(@Nullable Bundle in) {
229 return in != null ? in : new Bundle();
230 }
231
232 public static boolean isEmpty(@Nullable Bundle in) {
233 return (in == null) || (in.size() == 0);
234 }
235
236 /**
Pavel Grafov6a40f092016-10-25 15:46:51 +0100237 * Returns {@code true} if given bundle is not null and contains {@code true} for a given
238 * restriction.
239 */
240 public static boolean contains(@Nullable Bundle in, String restriction) {
241 return in != null && in.getBoolean(restriction);
242 }
243
244 /**
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800245 * Creates a copy of the {@code in} Bundle. If {@code in} is null, it'll return an empty
246 * bundle.
247 *
248 * <p>The resulting {@link Bundle} is always writable. (i.e. it won't return
249 * {@link Bundle#EMPTY})
250 */
251 public static @NonNull Bundle clone(@Nullable Bundle in) {
252 return (in != null) ? new Bundle(in) : new Bundle();
253 }
254
255 public static void merge(@NonNull Bundle dest, @Nullable Bundle in) {
256 Preconditions.checkNotNull(dest);
257 Preconditions.checkArgument(dest != in);
Makoto Onuki068c54a2015-10-13 14:34:03 -0700258 if (in == null) {
259 return;
260 }
261 for (String key : in.keySet()) {
262 if (in.getBoolean(key, false)) {
263 dest.putBoolean(key, true);
264 }
265 }
266 }
267
Makoto Onuki4f160732015-10-27 17:15:38 -0700268 /**
Pavel Grafov6a40f092016-10-25 15:46:51 +0100269 * Merges a sparse array of restrictions bundles into one.
270 */
271 @Nullable
272 public static Bundle mergeAll(SparseArray<Bundle> restrictions) {
273 if (restrictions.size() == 0) {
274 return null;
275 } else {
276 final Bundle result = new Bundle();
277 for (int i = 0; i < restrictions.size(); i++) {
278 merge(result, restrictions.valueAt(i));
279 }
280 return result;
281 }
282 }
283
284 /**
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800285 * @return true if a restriction is settable by device owner.
286 */
287 public static boolean canDeviceOwnerChange(String restriction) {
288 return !IMMUTABLE_BY_OWNERS.contains(restriction);
289 }
290
291 /**
Makoto Onuki5485ed42015-12-09 11:38:23 -0800292 * @return true if a restriction is settable by profile owner. Note it takes a user ID because
293 * some restrictions can be changed by PO only when it's running on the system user.
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800294 */
Makoto Onuki5485ed42015-12-09 11:38:23 -0800295 public static boolean canProfileOwnerChange(String restriction, int userId) {
296 return !IMMUTABLE_BY_OWNERS.contains(restriction)
297 && !(userId != UserHandle.USER_SYSTEM
Pavel Grafov6a40f092016-10-25 15:46:51 +0100298 && PRIMARY_USER_ONLY_RESTRICTIONS.contains(restriction));
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800299 }
300
301 /**
Esteban Talavera6c9116a2016-11-24 16:12:44 +0000302 * Returns the user restrictions that default to {@code true} for device owners.
Nicolas Prevot2ea46fe2017-01-05 10:29:34 +0000303 * These user restrictions are local, though. ie only for the device owner's user id.
Esteban Talavera6c9116a2016-11-24 16:12:44 +0000304 */
305 public static @NonNull Set<String> getDefaultEnabledForDeviceOwner() {
306 return DEFAULT_ENABLED_FOR_DEVICE_OWNERS;
307 }
308
309 /**
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800310 * Takes restrictions that can be set by device owner, and sort them into what should be applied
311 * globally and what should be applied only on the current user.
312 */
Pavel Grafov6a40f092016-10-25 15:46:51 +0100313 public static void sortToGlobalAndLocal(@Nullable Bundle in, boolean isDeviceOwner,
314 int cameraRestrictionScope,
315 @NonNull Bundle global, @NonNull Bundle local) {
316 // Camera restriction (as well as all others) goes to at most one bundle.
317 if (cameraRestrictionScope == UserManagerInternal.CAMERA_DISABLED_GLOBALLY) {
318 global.putBoolean(UserManager.DISALLOW_CAMERA, true);
319 } else if (cameraRestrictionScope == UserManagerInternal.CAMERA_DISABLED_LOCALLY) {
320 local.putBoolean(UserManager.DISALLOW_CAMERA, true);
321 }
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800322 if (in == null || in.size() == 0) {
323 return;
324 }
325 for (String key : in.keySet()) {
326 if (!in.getBoolean(key)) {
327 continue;
328 }
Pavel Grafov6a40f092016-10-25 15:46:51 +0100329 if (isGlobal(isDeviceOwner, key)) {
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800330 global.putBoolean(key, true);
331 } else {
332 local.putBoolean(key, true);
333 }
334 }
335 }
336
337 /**
Pavel Grafov6a40f092016-10-25 15:46:51 +0100338 * Whether given user restriction should be enforced globally.
339 */
340 private static boolean isGlobal(boolean isDeviceOwner, String key) {
341 return (isDeviceOwner &&
342 (PRIMARY_USER_ONLY_RESTRICTIONS.contains(key)|| GLOBAL_RESTRICTIONS.contains(key)))
343 || PROFILE_GLOBAL_RESTRICTIONS.contains(key);
344 }
345
346 /**
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800347 * @return true if two Bundles contain the same user restriction.
348 * A null bundle and an empty bundle are considered to be equal.
349 */
350 public static boolean areEqual(@Nullable Bundle a, @Nullable Bundle b) {
351 if (a == b) {
352 return true;
353 }
354 if (isEmpty(a)) {
355 return isEmpty(b);
356 }
357 if (isEmpty(b)) {
358 return false;
359 }
360 for (String key : a.keySet()) {
361 if (a.getBoolean(key) != b.getBoolean(key)) {
362 return false;
363 }
364 }
365 for (String key : b.keySet()) {
366 if (a.getBoolean(key) != b.getBoolean(key)) {
367 return false;
368 }
369 }
370 return true;
371 }
372
373 /**
Makoto Onuki4f160732015-10-27 17:15:38 -0700374 * Takes a new use restriction set and the previous set, and apply the restrictions that have
375 * changed.
Makoto Onukid45a4a22015-11-02 17:17:38 -0800376 *
Fyodor Kupolov9cbfc9e2015-10-07 15:52:33 -0700377 * <p>Note this method is called by {@link UserManagerService} without holding any locks.
Makoto Onuki4f160732015-10-27 17:15:38 -0700378 */
Fyodor Kupolov9cbfc9e2015-10-07 15:52:33 -0700379 public static void applyUserRestrictions(Context context, int userId,
Makoto Onukid45a4a22015-11-02 17:17:38 -0800380 Bundle newRestrictions, Bundle prevRestrictions) {
Makoto Onuki4f160732015-10-27 17:15:38 -0700381 for (String key : USER_RESTRICTIONS) {
382 final boolean newValue = newRestrictions.getBoolean(key);
383 final boolean prevValue = prevRestrictions.getBoolean(key);
384
385 if (newValue != prevValue) {
Fyodor Kupolov9cbfc9e2015-10-07 15:52:33 -0700386 applyUserRestriction(context, userId, key, newValue);
Makoto Onuki4f160732015-10-27 17:15:38 -0700387 }
388 }
389 }
Fyodor Kupolov9cbfc9e2015-10-07 15:52:33 -0700390
Makoto Onukid45a4a22015-11-02 17:17:38 -0800391 /**
392 * Apply each user restriction.
393 *
Makoto Onuki28da2e32015-11-20 11:30:44 -0800394 * <p>See also {@link
395 * com.android.providers.settings.SettingsProvider#isGlobalOrSecureSettingRestrictedForUser},
396 * which should be in sync with this method.
Makoto Onukid45a4a22015-11-02 17:17:38 -0800397 */
Fyodor Kupolov9cbfc9e2015-10-07 15:52:33 -0700398 private static void applyUserRestriction(Context context, int userId, String key,
Makoto Onuki4f160732015-10-27 17:15:38 -0700399 boolean newValue) {
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800400 if (UserManagerService.DBG) {
401 Log.d(TAG, "Applying user restriction: userId=" + userId
402 + " key=" + key + " value=" + newValue);
403 }
Makoto Onuki4f160732015-10-27 17:15:38 -0700404 // When certain restrictions are cleared, we don't update the system settings,
405 // because these settings are changeable on the Settings UI and we don't know the original
406 // value -- for example LOCATION_MODE might have been off already when the restriction was
407 // set, and in that case even if the restriction is lifted, changing it to ON would be
408 // wrong. So just don't do anything in such a case. If the user hopes to enable location
409 // later, they can do it on the Settings UI.
Benjamin Franz0ff13fc2016-07-12 13:42:21 +0100410 // WARNING: Remember that Settings.Global and Settings.Secure are changeable via adb.
411 // To prevent this from happening for a given user restriction, you have to add a check to
412 // SettingsProvider.isGlobalOrSecureSettingRestrictedForUser.
Makoto Onuki4f160732015-10-27 17:15:38 -0700413
414 final ContentResolver cr = context.getContentResolver();
415 final long id = Binder.clearCallingIdentity();
416 try {
417 switch (key) {
Mahaver Chopradea471e2015-12-17 11:02:37 +0000418 case UserManager.DISALLOW_DATA_ROAMING:
419 if (newValue) {
420 // DISALLOW_DATA_ROAMING user restriction is set.
421
422 // Multi sim device.
423 SubscriptionManager subscriptionManager = new SubscriptionManager(context);
424 final List<SubscriptionInfo> subscriptionInfoList =
425 subscriptionManager.getActiveSubscriptionInfoList();
426 if (subscriptionInfoList != null) {
427 for (SubscriptionInfo subInfo : subscriptionInfoList) {
428 android.provider.Settings.Global.putStringForUser(cr,
429 android.provider.Settings.Global.DATA_ROAMING
430 + subInfo.getSubscriptionId(), "0", userId);
431 }
432 }
433
434 // Single sim device.
435 android.provider.Settings.Global.putStringForUser(cr,
436 android.provider.Settings.Global.DATA_ROAMING, "0", userId);
437 }
438 break;
Makoto Onuki4f160732015-10-27 17:15:38 -0700439 case UserManager.DISALLOW_SHARE_LOCATION:
440 if (newValue) {
441 android.provider.Settings.Secure.putIntForUser(cr,
442 android.provider.Settings.Secure.LOCATION_MODE,
443 android.provider.Settings.Secure.LOCATION_MODE_OFF,
444 userId);
Makoto Onuki4f160732015-10-27 17:15:38 -0700445 }
Makoto Onuki4f160732015-10-27 17:15:38 -0700446 break;
447 case UserManager.DISALLOW_DEBUGGING_FEATURES:
448 if (newValue) {
449 // Only disable adb if changing for system user, since it is global
450 // TODO: should this be admin user?
451 if (userId == UserHandle.USER_SYSTEM) {
452 android.provider.Settings.Global.putStringForUser(cr,
453 android.provider.Settings.Global.ADB_ENABLED, "0",
454 userId);
455 }
456 }
457 break;
458 case UserManager.ENSURE_VERIFY_APPS:
459 if (newValue) {
460 android.provider.Settings.Global.putStringForUser(
461 context.getContentResolver(),
462 android.provider.Settings.Global.PACKAGE_VERIFIER_ENABLE, "1",
463 userId);
464 android.provider.Settings.Global.putStringForUser(
465 context.getContentResolver(),
466 android.provider.Settings.Global.PACKAGE_VERIFIER_INCLUDE_ADB, "1",
467 userId);
468 }
469 break;
470 case UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES:
Suprabh Shuklae3745ee2017-02-02 20:01:11 -0800471 // Since Android O, the secure setting is not available to be changed by the
472 // user. Hence, when the restriction is cleared, we need to reset the state of
473 // the setting to its default value which is now 1.
474 android.provider.Settings.Secure.putIntForUser(cr,
475 android.provider.Settings.Secure.INSTALL_NON_MARKET_APPS,
476 newValue ? 0 : 1, userId);
Makoto Onuki4f160732015-10-27 17:15:38 -0700477 break;
Fyodor Kupolov9cbfc9e2015-10-07 15:52:33 -0700478 case UserManager.DISALLOW_RUN_IN_BACKGROUND:
479 if (newValue) {
480 int currentUser = ActivityManager.getCurrentUser();
481 if (currentUser != userId && userId != UserHandle.USER_SYSTEM) {
482 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -0800483 ActivityManager.getService().stopUser(userId, false, null);
Fyodor Kupolov9cbfc9e2015-10-07 15:52:33 -0700484 } catch (RemoteException e) {
485 throw e.rethrowAsRuntimeException();
486 }
487 }
488 }
Lenka Trochtova6474f0e2016-03-24 16:43:10 +0100489 break;
490 case UserManager.DISALLOW_SAFE_BOOT:
491 // Unlike with the other restrictions, we want to propagate the new value to
492 // the system settings even if it is false. The other restrictions modify
493 // settings which could be manually changed by the user from the Settings app
494 // after the policies enforcing these restrictions have been revoked, so we
495 // leave re-setting of those settings to the user.
496 android.provider.Settings.Global.putInt(
497 context.getContentResolver(),
498 android.provider.Settings.Global.SAFE_BOOT_DISALLOWED,
499 newValue ? 1 : 0);
500 break;
Mahaver Chopra830e32c2016-05-17 18:53:09 +0100501 case UserManager.DISALLOW_FACTORY_RESET:
Mahaver Chopra3d9805d2016-07-07 16:25:05 +0100502 case UserManager.DISALLOW_OEM_UNLOCK:
Mahaver Chopra830e32c2016-05-17 18:53:09 +0100503 if (newValue) {
504 PersistentDataBlockManager manager = (PersistentDataBlockManager) context
505 .getSystemService(Context.PERSISTENT_DATA_BLOCK_SERVICE);
Esteban Talaverac48b20f2016-08-11 11:23:40 +0100506 if (manager != null
507 && manager.getOemUnlockEnabled()
508 && manager.getFlashLockState()
509 != PersistentDataBlockManager.FLASH_LOCK_UNLOCKED) {
510 // Only disable OEM unlock if the bootloader is locked. If it's already
511 // unlocked, setting the OEM unlock enabled flag to false has no effect
512 // (the bootloader would remain unlocked).
Mahaver Chopra830e32c2016-05-17 18:53:09 +0100513 manager.setOemUnlockEnabled(false);
514 }
515 }
Tony Makc1205112016-07-22 16:02:59 +0100516 break;
Makoto Onuki4f160732015-10-27 17:15:38 -0700517 }
Makoto Onuki4f160732015-10-27 17:15:38 -0700518 } finally {
519 Binder.restoreCallingIdentity(id);
520 }
521 }
522
Makoto Onukia4f11972015-10-01 13:19:58 -0700523 public static void dumpRestrictions(PrintWriter pw, String prefix, Bundle restrictions) {
524 boolean noneSet = true;
525 if (restrictions != null) {
526 for (String key : restrictions.keySet()) {
527 if (restrictions.getBoolean(key, false)) {
528 pw.println(prefix + key);
529 noneSet = false;
530 }
531 }
Makoto Onuki068c54a2015-10-13 14:34:03 -0700532 if (noneSet) {
533 pw.println(prefix + "none");
534 }
535 } else {
536 pw.println(prefix + "null");
Makoto Onukia4f11972015-10-01 13:19:58 -0700537 }
538 }
Pavel Grafov6a40f092016-10-25 15:46:51 +0100539
540 /**
541 * Moves a particular restriction from one array of bundles to another, e.g. for all users.
542 */
543 public static void moveRestriction(String restrictionKey, SparseArray<Bundle> srcRestrictions,
544 SparseArray<Bundle> destRestrictions) {
545 for (int i = 0; i < srcRestrictions.size(); i++) {
546 int key = srcRestrictions.keyAt(i);
547 Bundle from = srcRestrictions.valueAt(i);
548 if (contains(from, restrictionKey)) {
549 from.remove(restrictionKey);
550 Bundle to = destRestrictions.get(key);
551 if (to == null) {
552 to = new Bundle();
553 destRestrictions.append(key, to);
554 }
555 to.putBoolean(restrictionKey, true);
556 // Don't keep empty bundles.
557 if (from.isEmpty()) {
558 srcRestrictions.removeAt(i);
559 i--;
560 }
561 }
562 }
563 }
Makoto Onukia4f11972015-10-01 13:19:58 -0700564}