blob: 04997570b89e6d58eb9bc9f22b52211b0a5cdccf [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;
26import android.app.ActivityManagerNative;
Makoto Onuki4f160732015-10-27 17:15:38 -070027import android.content.ContentResolver;
28import android.content.Context;
Makoto Onuki4f160732015-10-27 17:15:38 -070029import android.net.Uri;
30import android.os.Binder;
Makoto Onukia4f11972015-10-01 13:19:58 -070031import android.os.Bundle;
Fyodor Kupolov9cbfc9e2015-10-07 15:52:33 -070032import android.os.RemoteException;
Makoto Onuki4f160732015-10-27 17:15:38 -070033import android.os.SystemProperties;
34import android.os.UserHandle;
Makoto Onukia4f11972015-10-01 13:19:58 -070035import android.os.UserManager;
Mahaver Chopra830e32c2016-05-17 18:53:09 +010036import android.service.persistentdata.PersistentDataBlockManager;
Mahaver Chopradea471e2015-12-17 11:02:37 +000037import android.telephony.SubscriptionInfo;
38import android.telephony.SubscriptionManager;
Makoto Onuki1a2cd742015-11-16 13:51:27 -080039import android.util.Log;
Makoto Onuki1f1ceef2016-01-28 11:32:32 -080040import android.util.Slog;
Makoto Onukia4f11972015-10-01 13:19:58 -070041
42import org.xmlpull.v1.XmlPullParser;
43import org.xmlpull.v1.XmlSerializer;
44
45import java.io.IOException;
46import java.io.PrintWriter;
Mahaver Chopradea471e2015-12-17 11:02:37 +000047import java.util.List;
Makoto Onukia4f11972015-10-01 13:19:58 -070048import java.util.Set;
49
Makoto Onukid45a4a22015-11-02 17:17:38 -080050/**
Mahaver Chopradea471e2015-12-17 11:02:37 +000051 * Utility methods for user restrictions.
Makoto Onukid45a4a22015-11-02 17:17:38 -080052 *
53 * <p>See {@link UserManagerService} for the method suffixes.
54 */
Makoto Onukia4f11972015-10-01 13:19:58 -070055public class UserRestrictionsUtils {
Makoto Onuki4f160732015-10-27 17:15:38 -070056 private static final String TAG = "UserRestrictionsUtils";
57
Makoto Onukia4f11972015-10-01 13:19:58 -070058 private UserRestrictionsUtils() {
59 }
60
Makoto Onuki1f1ceef2016-01-28 11:32:32 -080061 private static Set<String> newSetWithUniqueCheck(String[] strings) {
62 final Set<String> ret = Sets.newArraySet(strings);
63
64 // Make sure there's no overlap.
65 Preconditions.checkState(ret.size() == strings.length);
66 return ret;
67 }
68
69 public static final Set<String> USER_RESTRICTIONS = newSetWithUniqueCheck(new String[] {
Makoto Onukia4f11972015-10-01 13:19:58 -070070 UserManager.DISALLOW_CONFIG_WIFI,
71 UserManager.DISALLOW_MODIFY_ACCOUNTS,
72 UserManager.DISALLOW_INSTALL_APPS,
73 UserManager.DISALLOW_UNINSTALL_APPS,
74 UserManager.DISALLOW_SHARE_LOCATION,
75 UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES,
76 UserManager.DISALLOW_CONFIG_BLUETOOTH,
77 UserManager.DISALLOW_USB_FILE_TRANSFER,
78 UserManager.DISALLOW_CONFIG_CREDENTIALS,
79 UserManager.DISALLOW_REMOVE_USER,
80 UserManager.DISALLOW_DEBUGGING_FEATURES,
81 UserManager.DISALLOW_CONFIG_VPN,
82 UserManager.DISALLOW_CONFIG_TETHERING,
83 UserManager.DISALLOW_NETWORK_RESET,
84 UserManager.DISALLOW_FACTORY_RESET,
85 UserManager.DISALLOW_ADD_USER,
86 UserManager.ENSURE_VERIFY_APPS,
87 UserManager.DISALLOW_CONFIG_CELL_BROADCASTS,
88 UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS,
89 UserManager.DISALLOW_APPS_CONTROL,
90 UserManager.DISALLOW_MOUNT_PHYSICAL_MEDIA,
91 UserManager.DISALLOW_UNMUTE_MICROPHONE,
92 UserManager.DISALLOW_ADJUST_VOLUME,
93 UserManager.DISALLOW_OUTGOING_CALLS,
94 UserManager.DISALLOW_SMS,
95 UserManager.DISALLOW_FUN,
96 UserManager.DISALLOW_CREATE_WINDOWS,
97 UserManager.DISALLOW_CROSS_PROFILE_COPY_PASTE,
98 UserManager.DISALLOW_OUTGOING_BEAM,
99 UserManager.DISALLOW_WALLPAPER,
100 UserManager.DISALLOW_SAFE_BOOT,
101 UserManager.ALLOW_PARENT_PROFILE_APP_LINKING,
102 UserManager.DISALLOW_RECORD_AUDIO,
Fyodor Kupolov9cbfc9e2015-10-07 15:52:33 -0700103 UserManager.DISALLOW_CAMERA,
Mahaver Chopradea471e2015-12-17 11:02:37 +0000104 UserManager.DISALLOW_RUN_IN_BACKGROUND,
Oleksandr Peletskyi7f1f1df2016-01-18 15:40:21 +0100105 UserManager.DISALLOW_DATA_ROAMING,
Oleksandr Peletskyif2519812016-01-26 20:16:06 +0100106 UserManager.DISALLOW_SET_USER_ICON,
Mahaver Chopra3d9805d2016-07-07 16:25:05 +0100107 UserManager.DISALLOW_SET_WALLPAPER,
108 UserManager.DISALLOW_OEM_UNLOCK
Makoto Onuki1f1ceef2016-01-28 11:32:32 -0800109 });
Makoto Onukia4f11972015-10-01 13:19:58 -0700110
111 /**
112 * Set of user restriction which we don't want to persist.
113 */
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800114 private static final Set<String> NON_PERSIST_USER_RESTRICTIONS = Sets.newArraySet(
115 UserManager.DISALLOW_RECORD_AUDIO
116 );
Makoto Onukia4f11972015-10-01 13:19:58 -0700117
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800118 /**
119 * User restrictions that can not be set by profile owners.
120 */
121 private static final Set<String> DEVICE_OWNER_ONLY_RESTRICTIONS = Sets.newArraySet(
122 UserManager.DISALLOW_USB_FILE_TRANSFER,
123 UserManager.DISALLOW_CONFIG_TETHERING,
124 UserManager.DISALLOW_NETWORK_RESET,
125 UserManager.DISALLOW_FACTORY_RESET,
126 UserManager.DISALLOW_ADD_USER,
127 UserManager.DISALLOW_CONFIG_CELL_BROADCASTS,
128 UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS,
129 UserManager.DISALLOW_MOUNT_PHYSICAL_MEDIA,
130 UserManager.DISALLOW_SMS,
131 UserManager.DISALLOW_FUN,
132 UserManager.DISALLOW_SAFE_BOOT,
Mahaver Chopradea471e2015-12-17 11:02:37 +0000133 UserManager.DISALLOW_CREATE_WINDOWS,
134 UserManager.DISALLOW_DATA_ROAMING
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800135 );
136
137 /**
138 * User restrictions that can't be changed by device owner or profile owner.
139 */
140 private static final Set<String> IMMUTABLE_BY_OWNERS = Sets.newArraySet(
141 UserManager.DISALLOW_RECORD_AUDIO,
Mahaver Chopra3d9805d2016-07-07 16:25:05 +0100142 UserManager.DISALLOW_WALLPAPER,
143 UserManager.DISALLOW_OEM_UNLOCK
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800144 );
145
146 /**
147 * Special user restrictions that can be applied to a user as well as to all users globally,
148 * depending on callers. When device owner sets them, they'll be applied to all users.
149 */
150 private static final Set<String> GLOBAL_RESTRICTIONS = Sets.newArraySet(
151 UserManager.DISALLOW_ADJUST_VOLUME,
Fyodor Kupolov9cbfc9e2015-10-07 15:52:33 -0700152 UserManager.DISALLOW_RUN_IN_BACKGROUND,
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800153 UserManager.DISALLOW_UNMUTE_MICROPHONE
154 );
155
Makoto Onuki1f1ceef2016-01-28 11:32:32 -0800156 /**
157 * Throws {@link IllegalArgumentException} if the given restriction name is invalid.
158 */
159 public static boolean isValidRestriction(@NonNull String restriction) {
160 if (!USER_RESTRICTIONS.contains(restriction)) {
Makoto Onukiad5619d2016-02-10 13:41:15 -0800161 Slog.e(TAG, "Unknown restriction: " + restriction);
Makoto Onuki1f1ceef2016-01-28 11:32:32 -0800162 return false;
163 }
164 return true;
165 }
166
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800167 public static void writeRestrictions(@NonNull XmlSerializer serializer,
168 @Nullable Bundle restrictions, @NonNull String tag) throws IOException {
169 if (restrictions == null) {
170 return;
171 }
172
Makoto Onukia4f11972015-10-01 13:19:58 -0700173 serializer.startTag(null, tag);
Makoto Onukiac65e1e2015-11-20 15:33:17 -0800174 for (String key : restrictions.keySet()) {
175 if (NON_PERSIST_USER_RESTRICTIONS.contains(key)) {
176 continue; // Don't persist.
Makoto Onukia4f11972015-10-01 13:19:58 -0700177 }
Makoto Onukiac65e1e2015-11-20 15:33:17 -0800178 if (USER_RESTRICTIONS.contains(key)) {
179 if (restrictions.getBoolean(key)) {
180 serializer.attribute(null, key, "true");
181 }
182 continue;
183 }
184 Log.w(TAG, "Unknown user restriction detected: " + key);
Makoto Onukia4f11972015-10-01 13:19:58 -0700185 }
186 serializer.endTag(null, tag);
187 }
188
189 public static void readRestrictions(XmlPullParser parser, Bundle restrictions)
190 throws IOException {
191 for (String key : USER_RESTRICTIONS) {
192 final String value = parser.getAttributeValue(null, key);
193 if (value != null) {
194 restrictions.putBoolean(key, Boolean.parseBoolean(value));
195 }
196 }
197 }
198
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800199 /**
200 * @return {@code in} itself when it's not null, or an empty bundle (which can writable).
201 */
202 public static Bundle nonNull(@Nullable Bundle in) {
203 return in != null ? in : new Bundle();
204 }
205
206 public static boolean isEmpty(@Nullable Bundle in) {
207 return (in == null) || (in.size() == 0);
208 }
209
210 /**
211 * Creates a copy of the {@code in} Bundle. If {@code in} is null, it'll return an empty
212 * bundle.
213 *
214 * <p>The resulting {@link Bundle} is always writable. (i.e. it won't return
215 * {@link Bundle#EMPTY})
216 */
217 public static @NonNull Bundle clone(@Nullable Bundle in) {
218 return (in != null) ? new Bundle(in) : new Bundle();
219 }
220
221 public static void merge(@NonNull Bundle dest, @Nullable Bundle in) {
222 Preconditions.checkNotNull(dest);
223 Preconditions.checkArgument(dest != in);
Makoto Onuki068c54a2015-10-13 14:34:03 -0700224 if (in == null) {
225 return;
226 }
227 for (String key : in.keySet()) {
228 if (in.getBoolean(key, false)) {
229 dest.putBoolean(key, true);
230 }
231 }
232 }
233
Makoto Onuki4f160732015-10-27 17:15:38 -0700234 /**
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800235 * @return true if a restriction is settable by device owner.
236 */
237 public static boolean canDeviceOwnerChange(String restriction) {
238 return !IMMUTABLE_BY_OWNERS.contains(restriction);
239 }
240
241 /**
Makoto Onuki5485ed42015-12-09 11:38:23 -0800242 * @return true if a restriction is settable by profile owner. Note it takes a user ID because
243 * some restrictions can be changed by PO only when it's running on the system user.
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800244 */
Makoto Onuki5485ed42015-12-09 11:38:23 -0800245 public static boolean canProfileOwnerChange(String restriction, int userId) {
246 return !IMMUTABLE_BY_OWNERS.contains(restriction)
247 && !(userId != UserHandle.USER_SYSTEM
248 && DEVICE_OWNER_ONLY_RESTRICTIONS.contains(restriction));
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800249 }
250
251 /**
252 * Takes restrictions that can be set by device owner, and sort them into what should be applied
253 * globally and what should be applied only on the current user.
254 */
255 public static void sortToGlobalAndLocal(@Nullable Bundle in, @NonNull Bundle global,
256 @NonNull Bundle local) {
257 if (in == null || in.size() == 0) {
258 return;
259 }
260 for (String key : in.keySet()) {
261 if (!in.getBoolean(key)) {
262 continue;
263 }
264 if (DEVICE_OWNER_ONLY_RESTRICTIONS.contains(key) || GLOBAL_RESTRICTIONS.contains(key)) {
265 global.putBoolean(key, true);
266 } else {
267 local.putBoolean(key, true);
268 }
269 }
270 }
271
272 /**
273 * @return true if two Bundles contain the same user restriction.
274 * A null bundle and an empty bundle are considered to be equal.
275 */
276 public static boolean areEqual(@Nullable Bundle a, @Nullable Bundle b) {
277 if (a == b) {
278 return true;
279 }
280 if (isEmpty(a)) {
281 return isEmpty(b);
282 }
283 if (isEmpty(b)) {
284 return false;
285 }
286 for (String key : a.keySet()) {
287 if (a.getBoolean(key) != b.getBoolean(key)) {
288 return false;
289 }
290 }
291 for (String key : b.keySet()) {
292 if (a.getBoolean(key) != b.getBoolean(key)) {
293 return false;
294 }
295 }
296 return true;
297 }
298
299 /**
Makoto Onuki4f160732015-10-27 17:15:38 -0700300 * Takes a new use restriction set and the previous set, and apply the restrictions that have
301 * changed.
Makoto Onukid45a4a22015-11-02 17:17:38 -0800302 *
Fyodor Kupolov9cbfc9e2015-10-07 15:52:33 -0700303 * <p>Note this method is called by {@link UserManagerService} without holding any locks.
Makoto Onuki4f160732015-10-27 17:15:38 -0700304 */
Fyodor Kupolov9cbfc9e2015-10-07 15:52:33 -0700305 public static void applyUserRestrictions(Context context, int userId,
Makoto Onukid45a4a22015-11-02 17:17:38 -0800306 Bundle newRestrictions, Bundle prevRestrictions) {
Makoto Onuki4f160732015-10-27 17:15:38 -0700307 for (String key : USER_RESTRICTIONS) {
308 final boolean newValue = newRestrictions.getBoolean(key);
309 final boolean prevValue = prevRestrictions.getBoolean(key);
310
311 if (newValue != prevValue) {
Fyodor Kupolov9cbfc9e2015-10-07 15:52:33 -0700312 applyUserRestriction(context, userId, key, newValue);
Makoto Onuki4f160732015-10-27 17:15:38 -0700313 }
314 }
315 }
Fyodor Kupolov9cbfc9e2015-10-07 15:52:33 -0700316
Makoto Onukid45a4a22015-11-02 17:17:38 -0800317 /**
318 * Apply each user restriction.
319 *
Makoto Onuki28da2e32015-11-20 11:30:44 -0800320 * <p>See also {@link
321 * com.android.providers.settings.SettingsProvider#isGlobalOrSecureSettingRestrictedForUser},
322 * which should be in sync with this method.
Makoto Onukid45a4a22015-11-02 17:17:38 -0800323 */
Fyodor Kupolov9cbfc9e2015-10-07 15:52:33 -0700324 private static void applyUserRestriction(Context context, int userId, String key,
Makoto Onuki4f160732015-10-27 17:15:38 -0700325 boolean newValue) {
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800326 if (UserManagerService.DBG) {
327 Log.d(TAG, "Applying user restriction: userId=" + userId
328 + " key=" + key + " value=" + newValue);
329 }
Makoto Onuki4f160732015-10-27 17:15:38 -0700330 // When certain restrictions are cleared, we don't update the system settings,
331 // because these settings are changeable on the Settings UI and we don't know the original
332 // value -- for example LOCATION_MODE might have been off already when the restriction was
333 // set, and in that case even if the restriction is lifted, changing it to ON would be
334 // wrong. So just don't do anything in such a case. If the user hopes to enable location
335 // later, they can do it on the Settings UI.
336
337 final ContentResolver cr = context.getContentResolver();
338 final long id = Binder.clearCallingIdentity();
339 try {
340 switch (key) {
Makoto Onuki4f160732015-10-27 17:15:38 -0700341 case UserManager.DISALLOW_CONFIG_WIFI:
342 if (newValue) {
343 android.provider.Settings.Secure.putIntForUser(cr,
Makoto Onuki28da2e32015-11-20 11:30:44 -0800344 android.provider.Settings.Global
Makoto Onuki4f160732015-10-27 17:15:38 -0700345 .WIFI_NETWORKS_AVAILABLE_NOTIFICATION_ON, 0, userId);
346 }
347 break;
Mahaver Chopradea471e2015-12-17 11:02:37 +0000348 case UserManager.DISALLOW_DATA_ROAMING:
349 if (newValue) {
350 // DISALLOW_DATA_ROAMING user restriction is set.
351
352 // Multi sim device.
353 SubscriptionManager subscriptionManager = new SubscriptionManager(context);
354 final List<SubscriptionInfo> subscriptionInfoList =
355 subscriptionManager.getActiveSubscriptionInfoList();
356 if (subscriptionInfoList != null) {
357 for (SubscriptionInfo subInfo : subscriptionInfoList) {
358 android.provider.Settings.Global.putStringForUser(cr,
359 android.provider.Settings.Global.DATA_ROAMING
360 + subInfo.getSubscriptionId(), "0", userId);
361 }
362 }
363
364 // Single sim device.
365 android.provider.Settings.Global.putStringForUser(cr,
366 android.provider.Settings.Global.DATA_ROAMING, "0", userId);
367 }
368 break;
Makoto Onuki4f160732015-10-27 17:15:38 -0700369 case UserManager.DISALLOW_SHARE_LOCATION:
370 if (newValue) {
371 android.provider.Settings.Secure.putIntForUser(cr,
372 android.provider.Settings.Secure.LOCATION_MODE,
373 android.provider.Settings.Secure.LOCATION_MODE_OFF,
374 userId);
Makoto Onuki4f160732015-10-27 17:15:38 -0700375 }
Makoto Onuki4f160732015-10-27 17:15:38 -0700376 break;
377 case UserManager.DISALLOW_DEBUGGING_FEATURES:
378 if (newValue) {
379 // Only disable adb if changing for system user, since it is global
380 // TODO: should this be admin user?
381 if (userId == UserHandle.USER_SYSTEM) {
382 android.provider.Settings.Global.putStringForUser(cr,
383 android.provider.Settings.Global.ADB_ENABLED, "0",
384 userId);
385 }
386 }
387 break;
388 case UserManager.ENSURE_VERIFY_APPS:
389 if (newValue) {
390 android.provider.Settings.Global.putStringForUser(
391 context.getContentResolver(),
392 android.provider.Settings.Global.PACKAGE_VERIFIER_ENABLE, "1",
393 userId);
394 android.provider.Settings.Global.putStringForUser(
395 context.getContentResolver(),
396 android.provider.Settings.Global.PACKAGE_VERIFIER_INCLUDE_ADB, "1",
397 userId);
398 }
399 break;
400 case UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES:
401 if (newValue) {
402 android.provider.Settings.Secure.putIntForUser(cr,
403 android.provider.Settings.Secure.INSTALL_NON_MARKET_APPS, 0,
404 userId);
405 }
406 break;
Fyodor Kupolov9cbfc9e2015-10-07 15:52:33 -0700407 case UserManager.DISALLOW_RUN_IN_BACKGROUND:
408 if (newValue) {
409 int currentUser = ActivityManager.getCurrentUser();
410 if (currentUser != userId && userId != UserHandle.USER_SYSTEM) {
411 try {
412 ActivityManagerNative.getDefault().stopUser(userId, false, null);
413 } catch (RemoteException e) {
414 throw e.rethrowAsRuntimeException();
415 }
416 }
417 }
Lenka Trochtova6474f0e2016-03-24 16:43:10 +0100418 break;
419 case UserManager.DISALLOW_SAFE_BOOT:
420 // Unlike with the other restrictions, we want to propagate the new value to
421 // the system settings even if it is false. The other restrictions modify
422 // settings which could be manually changed by the user from the Settings app
423 // after the policies enforcing these restrictions have been revoked, so we
424 // leave re-setting of those settings to the user.
425 android.provider.Settings.Global.putInt(
426 context.getContentResolver(),
427 android.provider.Settings.Global.SAFE_BOOT_DISALLOWED,
428 newValue ? 1 : 0);
429 break;
Mahaver Chopra830e32c2016-05-17 18:53:09 +0100430 case UserManager.DISALLOW_FACTORY_RESET:
Mahaver Chopra3d9805d2016-07-07 16:25:05 +0100431 case UserManager.DISALLOW_OEM_UNLOCK:
Mahaver Chopra830e32c2016-05-17 18:53:09 +0100432 if (newValue) {
433 PersistentDataBlockManager manager = (PersistentDataBlockManager) context
434 .getSystemService(Context.PERSISTENT_DATA_BLOCK_SERVICE);
Mahaver Choprad444ff12016-06-16 14:05:58 +0100435 if (manager != null && manager.getOemUnlockEnabled()) {
Mahaver Chopra830e32c2016-05-17 18:53:09 +0100436 manager.setOemUnlockEnabled(false);
437 }
438 }
Makoto Onuki4f160732015-10-27 17:15:38 -0700439 }
Makoto Onuki4f160732015-10-27 17:15:38 -0700440 } finally {
441 Binder.restoreCallingIdentity(id);
442 }
443 }
444
Makoto Onukia4f11972015-10-01 13:19:58 -0700445 public static void dumpRestrictions(PrintWriter pw, String prefix, Bundle restrictions) {
446 boolean noneSet = true;
447 if (restrictions != null) {
448 for (String key : restrictions.keySet()) {
449 if (restrictions.getBoolean(key, false)) {
450 pw.println(prefix + key);
451 noneSet = false;
452 }
453 }
Makoto Onuki068c54a2015-10-13 14:34:03 -0700454 if (noneSet) {
455 pw.println(prefix + "none");
456 }
457 } else {
458 pw.println(prefix + "null");
Makoto Onukia4f11972015-10-01 13:19:58 -0700459 }
460 }
461}