blob: 364e9fa6719c2cd1c5e57afb8a1187f88e701733 [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 Chopradea471e2015-12-17 11:02:37 +000036import android.telephony.SubscriptionInfo;
37import android.telephony.SubscriptionManager;
Makoto Onuki1a2cd742015-11-16 13:51:27 -080038import android.util.Log;
Makoto Onuki1f1ceef2016-01-28 11:32:32 -080039import android.util.Slog;
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,
76 UserManager.DISALLOW_USB_FILE_TRANSFER,
77 UserManager.DISALLOW_CONFIG_CREDENTIALS,
78 UserManager.DISALLOW_REMOVE_USER,
79 UserManager.DISALLOW_DEBUGGING_FEATURES,
80 UserManager.DISALLOW_CONFIG_VPN,
81 UserManager.DISALLOW_CONFIG_TETHERING,
82 UserManager.DISALLOW_NETWORK_RESET,
83 UserManager.DISALLOW_FACTORY_RESET,
84 UserManager.DISALLOW_ADD_USER,
85 UserManager.ENSURE_VERIFY_APPS,
86 UserManager.DISALLOW_CONFIG_CELL_BROADCASTS,
87 UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS,
88 UserManager.DISALLOW_APPS_CONTROL,
89 UserManager.DISALLOW_MOUNT_PHYSICAL_MEDIA,
90 UserManager.DISALLOW_UNMUTE_MICROPHONE,
91 UserManager.DISALLOW_ADJUST_VOLUME,
92 UserManager.DISALLOW_OUTGOING_CALLS,
93 UserManager.DISALLOW_SMS,
94 UserManager.DISALLOW_FUN,
95 UserManager.DISALLOW_CREATE_WINDOWS,
96 UserManager.DISALLOW_CROSS_PROFILE_COPY_PASTE,
97 UserManager.DISALLOW_OUTGOING_BEAM,
98 UserManager.DISALLOW_WALLPAPER,
99 UserManager.DISALLOW_SAFE_BOOT,
100 UserManager.ALLOW_PARENT_PROFILE_APP_LINKING,
101 UserManager.DISALLOW_RECORD_AUDIO,
Fyodor Kupolov9cbfc9e2015-10-07 15:52:33 -0700102 UserManager.DISALLOW_CAMERA,
Mahaver Chopradea471e2015-12-17 11:02:37 +0000103 UserManager.DISALLOW_RUN_IN_BACKGROUND,
Oleksandr Peletskyi7f1f1df2016-01-18 15:40:21 +0100104 UserManager.DISALLOW_DATA_ROAMING,
Oleksandr Peletskyif2519812016-01-26 20:16:06 +0100105 UserManager.DISALLOW_SET_USER_ICON,
106 UserManager.DISALLOW_SET_WALLPAPER
Makoto Onuki1f1ceef2016-01-28 11:32:32 -0800107 });
Makoto Onukia4f11972015-10-01 13:19:58 -0700108
109 /**
110 * Set of user restriction which we don't want to persist.
111 */
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800112 private static final Set<String> NON_PERSIST_USER_RESTRICTIONS = Sets.newArraySet(
113 UserManager.DISALLOW_RECORD_AUDIO
114 );
Makoto Onukia4f11972015-10-01 13:19:58 -0700115
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800116 /**
117 * User restrictions that can not be set by profile owners.
118 */
119 private static final Set<String> DEVICE_OWNER_ONLY_RESTRICTIONS = Sets.newArraySet(
120 UserManager.DISALLOW_USB_FILE_TRANSFER,
121 UserManager.DISALLOW_CONFIG_TETHERING,
122 UserManager.DISALLOW_NETWORK_RESET,
123 UserManager.DISALLOW_FACTORY_RESET,
124 UserManager.DISALLOW_ADD_USER,
125 UserManager.DISALLOW_CONFIG_CELL_BROADCASTS,
126 UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS,
127 UserManager.DISALLOW_MOUNT_PHYSICAL_MEDIA,
128 UserManager.DISALLOW_SMS,
129 UserManager.DISALLOW_FUN,
130 UserManager.DISALLOW_SAFE_BOOT,
Mahaver Chopradea471e2015-12-17 11:02:37 +0000131 UserManager.DISALLOW_CREATE_WINDOWS,
132 UserManager.DISALLOW_DATA_ROAMING
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800133 );
134
135 /**
136 * User restrictions that can't be changed by device owner or profile owner.
137 */
138 private static final Set<String> IMMUTABLE_BY_OWNERS = Sets.newArraySet(
139 UserManager.DISALLOW_RECORD_AUDIO,
140 UserManager.DISALLOW_WALLPAPER
141 );
142
143 /**
144 * Special user restrictions that can be applied to a user as well as to all users globally,
145 * depending on callers. When device owner sets them, they'll be applied to all users.
146 */
147 private static final Set<String> GLOBAL_RESTRICTIONS = Sets.newArraySet(
148 UserManager.DISALLOW_ADJUST_VOLUME,
Fyodor Kupolov9cbfc9e2015-10-07 15:52:33 -0700149 UserManager.DISALLOW_RUN_IN_BACKGROUND,
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800150 UserManager.DISALLOW_UNMUTE_MICROPHONE
151 );
152
Makoto Onuki1f1ceef2016-01-28 11:32:32 -0800153 /**
154 * Throws {@link IllegalArgumentException} if the given restriction name is invalid.
155 */
156 public static boolean isValidRestriction(@NonNull String restriction) {
157 if (!USER_RESTRICTIONS.contains(restriction)) {
Makoto Onukiad5619d2016-02-10 13:41:15 -0800158 Slog.e(TAG, "Unknown restriction: " + restriction);
Makoto Onuki1f1ceef2016-01-28 11:32:32 -0800159 return false;
160 }
161 return true;
162 }
163
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800164 public static void writeRestrictions(@NonNull XmlSerializer serializer,
165 @Nullable Bundle restrictions, @NonNull String tag) throws IOException {
166 if (restrictions == null) {
167 return;
168 }
169
Makoto Onukia4f11972015-10-01 13:19:58 -0700170 serializer.startTag(null, tag);
Makoto Onukiac65e1e2015-11-20 15:33:17 -0800171 for (String key : restrictions.keySet()) {
172 if (NON_PERSIST_USER_RESTRICTIONS.contains(key)) {
173 continue; // Don't persist.
Makoto Onukia4f11972015-10-01 13:19:58 -0700174 }
Makoto Onukiac65e1e2015-11-20 15:33:17 -0800175 if (USER_RESTRICTIONS.contains(key)) {
176 if (restrictions.getBoolean(key)) {
177 serializer.attribute(null, key, "true");
178 }
179 continue;
180 }
181 Log.w(TAG, "Unknown user restriction detected: " + key);
Makoto Onukia4f11972015-10-01 13:19:58 -0700182 }
183 serializer.endTag(null, tag);
184 }
185
186 public static void readRestrictions(XmlPullParser parser, Bundle restrictions)
187 throws IOException {
188 for (String key : USER_RESTRICTIONS) {
189 final String value = parser.getAttributeValue(null, key);
190 if (value != null) {
191 restrictions.putBoolean(key, Boolean.parseBoolean(value));
192 }
193 }
194 }
195
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800196 /**
197 * @return {@code in} itself when it's not null, or an empty bundle (which can writable).
198 */
199 public static Bundle nonNull(@Nullable Bundle in) {
200 return in != null ? in : new Bundle();
201 }
202
203 public static boolean isEmpty(@Nullable Bundle in) {
204 return (in == null) || (in.size() == 0);
205 }
206
207 /**
208 * Creates a copy of the {@code in} Bundle. If {@code in} is null, it'll return an empty
209 * bundle.
210 *
211 * <p>The resulting {@link Bundle} is always writable. (i.e. it won't return
212 * {@link Bundle#EMPTY})
213 */
214 public static @NonNull Bundle clone(@Nullable Bundle in) {
215 return (in != null) ? new Bundle(in) : new Bundle();
216 }
217
218 public static void merge(@NonNull Bundle dest, @Nullable Bundle in) {
219 Preconditions.checkNotNull(dest);
220 Preconditions.checkArgument(dest != in);
Makoto Onuki068c54a2015-10-13 14:34:03 -0700221 if (in == null) {
222 return;
223 }
224 for (String key : in.keySet()) {
225 if (in.getBoolean(key, false)) {
226 dest.putBoolean(key, true);
227 }
228 }
229 }
230
Makoto Onuki4f160732015-10-27 17:15:38 -0700231 /**
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800232 * @return true if a restriction is settable by device owner.
233 */
234 public static boolean canDeviceOwnerChange(String restriction) {
235 return !IMMUTABLE_BY_OWNERS.contains(restriction);
236 }
237
238 /**
Makoto Onuki5485ed42015-12-09 11:38:23 -0800239 * @return true if a restriction is settable by profile owner. Note it takes a user ID because
240 * some restrictions can be changed by PO only when it's running on the system user.
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800241 */
Makoto Onuki5485ed42015-12-09 11:38:23 -0800242 public static boolean canProfileOwnerChange(String restriction, int userId) {
243 return !IMMUTABLE_BY_OWNERS.contains(restriction)
244 && !(userId != UserHandle.USER_SYSTEM
245 && DEVICE_OWNER_ONLY_RESTRICTIONS.contains(restriction));
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800246 }
247
248 /**
249 * Takes restrictions that can be set by device owner, and sort them into what should be applied
250 * globally and what should be applied only on the current user.
251 */
252 public static void sortToGlobalAndLocal(@Nullable Bundle in, @NonNull Bundle global,
253 @NonNull Bundle local) {
254 if (in == null || in.size() == 0) {
255 return;
256 }
257 for (String key : in.keySet()) {
258 if (!in.getBoolean(key)) {
259 continue;
260 }
261 if (DEVICE_OWNER_ONLY_RESTRICTIONS.contains(key) || GLOBAL_RESTRICTIONS.contains(key)) {
262 global.putBoolean(key, true);
263 } else {
264 local.putBoolean(key, true);
265 }
266 }
267 }
268
269 /**
270 * @return true if two Bundles contain the same user restriction.
271 * A null bundle and an empty bundle are considered to be equal.
272 */
273 public static boolean areEqual(@Nullable Bundle a, @Nullable Bundle b) {
274 if (a == b) {
275 return true;
276 }
277 if (isEmpty(a)) {
278 return isEmpty(b);
279 }
280 if (isEmpty(b)) {
281 return false;
282 }
283 for (String key : a.keySet()) {
284 if (a.getBoolean(key) != b.getBoolean(key)) {
285 return false;
286 }
287 }
288 for (String key : b.keySet()) {
289 if (a.getBoolean(key) != b.getBoolean(key)) {
290 return false;
291 }
292 }
293 return true;
294 }
295
296 /**
Makoto Onuki4f160732015-10-27 17:15:38 -0700297 * Takes a new use restriction set and the previous set, and apply the restrictions that have
298 * changed.
Makoto Onukid45a4a22015-11-02 17:17:38 -0800299 *
Fyodor Kupolov9cbfc9e2015-10-07 15:52:33 -0700300 * <p>Note this method is called by {@link UserManagerService} without holding any locks.
Makoto Onuki4f160732015-10-27 17:15:38 -0700301 */
Fyodor Kupolov9cbfc9e2015-10-07 15:52:33 -0700302 public static void applyUserRestrictions(Context context, int userId,
Makoto Onukid45a4a22015-11-02 17:17:38 -0800303 Bundle newRestrictions, Bundle prevRestrictions) {
Makoto Onuki4f160732015-10-27 17:15:38 -0700304 for (String key : USER_RESTRICTIONS) {
305 final boolean newValue = newRestrictions.getBoolean(key);
306 final boolean prevValue = prevRestrictions.getBoolean(key);
307
308 if (newValue != prevValue) {
Fyodor Kupolov9cbfc9e2015-10-07 15:52:33 -0700309 applyUserRestriction(context, userId, key, newValue);
Makoto Onuki4f160732015-10-27 17:15:38 -0700310 }
311 }
312 }
Fyodor Kupolov9cbfc9e2015-10-07 15:52:33 -0700313
Makoto Onukid45a4a22015-11-02 17:17:38 -0800314 /**
315 * Apply each user restriction.
316 *
Makoto Onuki28da2e32015-11-20 11:30:44 -0800317 * <p>See also {@link
318 * com.android.providers.settings.SettingsProvider#isGlobalOrSecureSettingRestrictedForUser},
319 * which should be in sync with this method.
Makoto Onukid45a4a22015-11-02 17:17:38 -0800320 */
Fyodor Kupolov9cbfc9e2015-10-07 15:52:33 -0700321 private static void applyUserRestriction(Context context, int userId, String key,
Makoto Onuki4f160732015-10-27 17:15:38 -0700322 boolean newValue) {
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800323 if (UserManagerService.DBG) {
324 Log.d(TAG, "Applying user restriction: userId=" + userId
325 + " key=" + key + " value=" + newValue);
326 }
Makoto Onuki4f160732015-10-27 17:15:38 -0700327 // When certain restrictions are cleared, we don't update the system settings,
328 // because these settings are changeable on the Settings UI and we don't know the original
329 // value -- for example LOCATION_MODE might have been off already when the restriction was
330 // set, and in that case even if the restriction is lifted, changing it to ON would be
331 // wrong. So just don't do anything in such a case. If the user hopes to enable location
332 // later, they can do it on the Settings UI.
333
334 final ContentResolver cr = context.getContentResolver();
335 final long id = Binder.clearCallingIdentity();
336 try {
337 switch (key) {
Makoto Onuki4f160732015-10-27 17:15:38 -0700338 case UserManager.DISALLOW_CONFIG_WIFI:
339 if (newValue) {
340 android.provider.Settings.Secure.putIntForUser(cr,
Makoto Onuki28da2e32015-11-20 11:30:44 -0800341 android.provider.Settings.Global
Makoto Onuki4f160732015-10-27 17:15:38 -0700342 .WIFI_NETWORKS_AVAILABLE_NOTIFICATION_ON, 0, userId);
343 }
344 break;
Mahaver Chopradea471e2015-12-17 11:02:37 +0000345 case UserManager.DISALLOW_DATA_ROAMING:
346 if (newValue) {
347 // DISALLOW_DATA_ROAMING user restriction is set.
348
349 // Multi sim device.
350 SubscriptionManager subscriptionManager = new SubscriptionManager(context);
351 final List<SubscriptionInfo> subscriptionInfoList =
352 subscriptionManager.getActiveSubscriptionInfoList();
353 if (subscriptionInfoList != null) {
354 for (SubscriptionInfo subInfo : subscriptionInfoList) {
355 android.provider.Settings.Global.putStringForUser(cr,
356 android.provider.Settings.Global.DATA_ROAMING
357 + subInfo.getSubscriptionId(), "0", userId);
358 }
359 }
360
361 // Single sim device.
362 android.provider.Settings.Global.putStringForUser(cr,
363 android.provider.Settings.Global.DATA_ROAMING, "0", userId);
364 }
365 break;
Makoto Onuki4f160732015-10-27 17:15:38 -0700366 case UserManager.DISALLOW_SHARE_LOCATION:
367 if (newValue) {
368 android.provider.Settings.Secure.putIntForUser(cr,
369 android.provider.Settings.Secure.LOCATION_MODE,
370 android.provider.Settings.Secure.LOCATION_MODE_OFF,
371 userId);
Makoto Onuki4f160732015-10-27 17:15:38 -0700372 }
373 // Send out notifications as some clients may want to reread the
374 // value which actually changed due to a restriction having been
375 // applied.
376 final String property =
377 android.provider.Settings.Secure.SYS_PROP_SETTING_VERSION;
378 long version = SystemProperties.getLong(property, 0) + 1;
379 SystemProperties.set(property, Long.toString(version));
380
381 final String name = android.provider.Settings.Secure.LOCATION_PROVIDERS_ALLOWED;
382 final Uri url = Uri.withAppendedPath(
383 android.provider.Settings.Secure.CONTENT_URI, name);
384 context.getContentResolver().notifyChange(url, null, true, userId);
385
386 break;
387 case UserManager.DISALLOW_DEBUGGING_FEATURES:
388 if (newValue) {
389 // Only disable adb if changing for system user, since it is global
390 // TODO: should this be admin user?
391 if (userId == UserHandle.USER_SYSTEM) {
392 android.provider.Settings.Global.putStringForUser(cr,
393 android.provider.Settings.Global.ADB_ENABLED, "0",
394 userId);
395 }
396 }
397 break;
398 case UserManager.ENSURE_VERIFY_APPS:
399 if (newValue) {
400 android.provider.Settings.Global.putStringForUser(
401 context.getContentResolver(),
402 android.provider.Settings.Global.PACKAGE_VERIFIER_ENABLE, "1",
403 userId);
404 android.provider.Settings.Global.putStringForUser(
405 context.getContentResolver(),
406 android.provider.Settings.Global.PACKAGE_VERIFIER_INCLUDE_ADB, "1",
407 userId);
408 }
409 break;
410 case UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES:
411 if (newValue) {
412 android.provider.Settings.Secure.putIntForUser(cr,
413 android.provider.Settings.Secure.INSTALL_NON_MARKET_APPS, 0,
414 userId);
415 }
416 break;
Fyodor Kupolov9cbfc9e2015-10-07 15:52:33 -0700417 case UserManager.DISALLOW_RUN_IN_BACKGROUND:
418 if (newValue) {
419 int currentUser = ActivityManager.getCurrentUser();
420 if (currentUser != userId && userId != UserHandle.USER_SYSTEM) {
421 try {
422 ActivityManagerNative.getDefault().stopUser(userId, false, null);
423 } catch (RemoteException e) {
424 throw e.rethrowAsRuntimeException();
425 }
426 }
427 }
Lenka Trochtova6474f0e2016-03-24 16:43:10 +0100428 break;
429 case UserManager.DISALLOW_SAFE_BOOT:
430 // Unlike with the other restrictions, we want to propagate the new value to
431 // the system settings even if it is false. The other restrictions modify
432 // settings which could be manually changed by the user from the Settings app
433 // after the policies enforcing these restrictions have been revoked, so we
434 // leave re-setting of those settings to the user.
435 android.provider.Settings.Global.putInt(
436 context.getContentResolver(),
437 android.provider.Settings.Global.SAFE_BOOT_DISALLOWED,
438 newValue ? 1 : 0);
439 break;
Makoto Onuki4f160732015-10-27 17:15:38 -0700440 }
Makoto Onuki4f160732015-10-27 17:15:38 -0700441 } finally {
442 Binder.restoreCallingIdentity(id);
443 }
444 }
445
Makoto Onukia4f11972015-10-01 13:19:58 -0700446 public static void dumpRestrictions(PrintWriter pw, String prefix, Bundle restrictions) {
447 boolean noneSet = true;
448 if (restrictions != null) {
449 for (String key : restrictions.keySet()) {
450 if (restrictions.getBoolean(key, false)) {
451 pw.println(prefix + key);
452 noneSet = false;
453 }
454 }
Makoto Onuki068c54a2015-10-13 14:34:03 -0700455 if (noneSet) {
456 pw.println(prefix + "none");
457 }
458 } else {
459 pw.println(prefix + "null");
Makoto Onukia4f11972015-10-01 13:19:58 -0700460 }
461 }
462}