blob: 816903ec4901abf0af0f15dd2e6c952f8e52b951 [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;
Makoto Onuki1a2cd742015-11-16 13:51:27 -080036import android.util.Log;
Makoto Onukia4f11972015-10-01 13:19:58 -070037
38import org.xmlpull.v1.XmlPullParser;
39import org.xmlpull.v1.XmlSerializer;
40
41import java.io.IOException;
42import java.io.PrintWriter;
43import java.util.Set;
44
Makoto Onukid45a4a22015-11-02 17:17:38 -080045/**
46 * Utility methods for uesr restrictions.
47 *
48 * <p>See {@link UserManagerService} for the method suffixes.
49 */
Makoto Onukia4f11972015-10-01 13:19:58 -070050public class UserRestrictionsUtils {
Makoto Onuki4f160732015-10-27 17:15:38 -070051 private static final String TAG = "UserRestrictionsUtils";
52
Makoto Onukia4f11972015-10-01 13:19:58 -070053 private UserRestrictionsUtils() {
54 }
55
Makoto Onukiac65e1e2015-11-20 15:33:17 -080056 public static final Set<String> USER_RESTRICTIONS = Sets.newArraySet(
Makoto Onukia4f11972015-10-01 13:19:58 -070057 UserManager.DISALLOW_CONFIG_WIFI,
58 UserManager.DISALLOW_MODIFY_ACCOUNTS,
59 UserManager.DISALLOW_INSTALL_APPS,
60 UserManager.DISALLOW_UNINSTALL_APPS,
61 UserManager.DISALLOW_SHARE_LOCATION,
62 UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES,
63 UserManager.DISALLOW_CONFIG_BLUETOOTH,
64 UserManager.DISALLOW_USB_FILE_TRANSFER,
65 UserManager.DISALLOW_CONFIG_CREDENTIALS,
66 UserManager.DISALLOW_REMOVE_USER,
67 UserManager.DISALLOW_DEBUGGING_FEATURES,
68 UserManager.DISALLOW_CONFIG_VPN,
69 UserManager.DISALLOW_CONFIG_TETHERING,
70 UserManager.DISALLOW_NETWORK_RESET,
71 UserManager.DISALLOW_FACTORY_RESET,
72 UserManager.DISALLOW_ADD_USER,
73 UserManager.ENSURE_VERIFY_APPS,
74 UserManager.DISALLOW_CONFIG_CELL_BROADCASTS,
75 UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS,
76 UserManager.DISALLOW_APPS_CONTROL,
77 UserManager.DISALLOW_MOUNT_PHYSICAL_MEDIA,
78 UserManager.DISALLOW_UNMUTE_MICROPHONE,
79 UserManager.DISALLOW_ADJUST_VOLUME,
80 UserManager.DISALLOW_OUTGOING_CALLS,
81 UserManager.DISALLOW_SMS,
82 UserManager.DISALLOW_FUN,
83 UserManager.DISALLOW_CREATE_WINDOWS,
84 UserManager.DISALLOW_CROSS_PROFILE_COPY_PASTE,
85 UserManager.DISALLOW_OUTGOING_BEAM,
86 UserManager.DISALLOW_WALLPAPER,
87 UserManager.DISALLOW_SAFE_BOOT,
88 UserManager.ALLOW_PARENT_PROFILE_APP_LINKING,
89 UserManager.DISALLOW_RECORD_AUDIO,
Fyodor Kupolov9cbfc9e2015-10-07 15:52:33 -070090 UserManager.DISALLOW_CAMERA,
91 UserManager.DISALLOW_RUN_IN_BACKGROUND
Makoto Onuki1a2cd742015-11-16 13:51:27 -080092 );
Makoto Onukia4f11972015-10-01 13:19:58 -070093
94 /**
95 * Set of user restriction which we don't want to persist.
96 */
Makoto Onuki1a2cd742015-11-16 13:51:27 -080097 private static final Set<String> NON_PERSIST_USER_RESTRICTIONS = Sets.newArraySet(
98 UserManager.DISALLOW_RECORD_AUDIO
99 );
Makoto Onukia4f11972015-10-01 13:19:58 -0700100
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800101 /**
102 * User restrictions that can not be set by profile owners.
103 */
104 private static final Set<String> DEVICE_OWNER_ONLY_RESTRICTIONS = Sets.newArraySet(
105 UserManager.DISALLOW_USB_FILE_TRANSFER,
106 UserManager.DISALLOW_CONFIG_TETHERING,
107 UserManager.DISALLOW_NETWORK_RESET,
108 UserManager.DISALLOW_FACTORY_RESET,
109 UserManager.DISALLOW_ADD_USER,
110 UserManager.DISALLOW_CONFIG_CELL_BROADCASTS,
111 UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS,
112 UserManager.DISALLOW_MOUNT_PHYSICAL_MEDIA,
113 UserManager.DISALLOW_SMS,
114 UserManager.DISALLOW_FUN,
115 UserManager.DISALLOW_SAFE_BOOT,
116 UserManager.DISALLOW_CREATE_WINDOWS
117 );
118
119 /**
120 * User restrictions that can't be changed by device owner or profile owner.
121 */
122 private static final Set<String> IMMUTABLE_BY_OWNERS = Sets.newArraySet(
123 UserManager.DISALLOW_RECORD_AUDIO,
124 UserManager.DISALLOW_WALLPAPER
125 );
126
127 /**
128 * Special user restrictions that can be applied to a user as well as to all users globally,
129 * depending on callers. When device owner sets them, they'll be applied to all users.
130 */
131 private static final Set<String> GLOBAL_RESTRICTIONS = Sets.newArraySet(
132 UserManager.DISALLOW_ADJUST_VOLUME,
Fyodor Kupolov9cbfc9e2015-10-07 15:52:33 -0700133 UserManager.DISALLOW_RUN_IN_BACKGROUND,
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800134 UserManager.DISALLOW_UNMUTE_MICROPHONE
135 );
136
137 public static void writeRestrictions(@NonNull XmlSerializer serializer,
138 @Nullable Bundle restrictions, @NonNull String tag) throws IOException {
139 if (restrictions == null) {
140 return;
141 }
142
Makoto Onukia4f11972015-10-01 13:19:58 -0700143 serializer.startTag(null, tag);
Makoto Onukiac65e1e2015-11-20 15:33:17 -0800144 for (String key : restrictions.keySet()) {
145 if (NON_PERSIST_USER_RESTRICTIONS.contains(key)) {
146 continue; // Don't persist.
Makoto Onukia4f11972015-10-01 13:19:58 -0700147 }
Makoto Onukiac65e1e2015-11-20 15:33:17 -0800148 if (USER_RESTRICTIONS.contains(key)) {
149 if (restrictions.getBoolean(key)) {
150 serializer.attribute(null, key, "true");
151 }
152 continue;
153 }
154 Log.w(TAG, "Unknown user restriction detected: " + key);
Makoto Onukia4f11972015-10-01 13:19:58 -0700155 }
156 serializer.endTag(null, tag);
157 }
158
159 public static void readRestrictions(XmlPullParser parser, Bundle restrictions)
160 throws IOException {
161 for (String key : USER_RESTRICTIONS) {
162 final String value = parser.getAttributeValue(null, key);
163 if (value != null) {
164 restrictions.putBoolean(key, Boolean.parseBoolean(value));
165 }
166 }
167 }
168
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800169 /**
170 * @return {@code in} itself when it's not null, or an empty bundle (which can writable).
171 */
172 public static Bundle nonNull(@Nullable Bundle in) {
173 return in != null ? in : new Bundle();
174 }
175
176 public static boolean isEmpty(@Nullable Bundle in) {
177 return (in == null) || (in.size() == 0);
178 }
179
180 /**
181 * Creates a copy of the {@code in} Bundle. If {@code in} is null, it'll return an empty
182 * bundle.
183 *
184 * <p>The resulting {@link Bundle} is always writable. (i.e. it won't return
185 * {@link Bundle#EMPTY})
186 */
187 public static @NonNull Bundle clone(@Nullable Bundle in) {
188 return (in != null) ? new Bundle(in) : new Bundle();
189 }
190
191 public static void merge(@NonNull Bundle dest, @Nullable Bundle in) {
192 Preconditions.checkNotNull(dest);
193 Preconditions.checkArgument(dest != in);
Makoto Onuki068c54a2015-10-13 14:34:03 -0700194 if (in == null) {
195 return;
196 }
197 for (String key : in.keySet()) {
198 if (in.getBoolean(key, false)) {
199 dest.putBoolean(key, true);
200 }
201 }
202 }
203
Makoto Onuki4f160732015-10-27 17:15:38 -0700204 /**
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800205 * @return true if a restriction is settable by device owner.
206 */
207 public static boolean canDeviceOwnerChange(String restriction) {
208 return !IMMUTABLE_BY_OWNERS.contains(restriction);
209 }
210
211 /**
212 * @return true if a restriction is settable by profile owner.
213 */
214 public static boolean canProfileOwnerChange(String restriction) {
215 return !(IMMUTABLE_BY_OWNERS.contains(restriction)
216 || DEVICE_OWNER_ONLY_RESTRICTIONS.contains(restriction));
217 }
218
219 /**
220 * Takes restrictions that can be set by device owner, and sort them into what should be applied
221 * globally and what should be applied only on the current user.
222 */
223 public static void sortToGlobalAndLocal(@Nullable Bundle in, @NonNull Bundle global,
224 @NonNull Bundle local) {
225 if (in == null || in.size() == 0) {
226 return;
227 }
228 for (String key : in.keySet()) {
229 if (!in.getBoolean(key)) {
230 continue;
231 }
232 if (DEVICE_OWNER_ONLY_RESTRICTIONS.contains(key) || GLOBAL_RESTRICTIONS.contains(key)) {
233 global.putBoolean(key, true);
234 } else {
235 local.putBoolean(key, true);
236 }
237 }
238 }
239
240 /**
241 * @return true if two Bundles contain the same user restriction.
242 * A null bundle and an empty bundle are considered to be equal.
243 */
244 public static boolean areEqual(@Nullable Bundle a, @Nullable Bundle b) {
245 if (a == b) {
246 return true;
247 }
248 if (isEmpty(a)) {
249 return isEmpty(b);
250 }
251 if (isEmpty(b)) {
252 return false;
253 }
254 for (String key : a.keySet()) {
255 if (a.getBoolean(key) != b.getBoolean(key)) {
256 return false;
257 }
258 }
259 for (String key : b.keySet()) {
260 if (a.getBoolean(key) != b.getBoolean(key)) {
261 return false;
262 }
263 }
264 return true;
265 }
266
267 /**
Makoto Onuki4f160732015-10-27 17:15:38 -0700268 * Takes a new use restriction set and the previous set, and apply the restrictions that have
269 * changed.
Makoto Onukid45a4a22015-11-02 17:17:38 -0800270 *
Fyodor Kupolov9cbfc9e2015-10-07 15:52:33 -0700271 * <p>Note this method is called by {@link UserManagerService} without holding any locks.
Makoto Onuki4f160732015-10-27 17:15:38 -0700272 */
Fyodor Kupolov9cbfc9e2015-10-07 15:52:33 -0700273 public static void applyUserRestrictions(Context context, int userId,
Makoto Onukid45a4a22015-11-02 17:17:38 -0800274 Bundle newRestrictions, Bundle prevRestrictions) {
Makoto Onuki4f160732015-10-27 17:15:38 -0700275 for (String key : USER_RESTRICTIONS) {
276 final boolean newValue = newRestrictions.getBoolean(key);
277 final boolean prevValue = prevRestrictions.getBoolean(key);
278
279 if (newValue != prevValue) {
Fyodor Kupolov9cbfc9e2015-10-07 15:52:33 -0700280 applyUserRestriction(context, userId, key, newValue);
Makoto Onuki4f160732015-10-27 17:15:38 -0700281 }
282 }
283 }
Fyodor Kupolov9cbfc9e2015-10-07 15:52:33 -0700284
Makoto Onukid45a4a22015-11-02 17:17:38 -0800285 /**
286 * Apply each user restriction.
287 *
Makoto Onuki28da2e32015-11-20 11:30:44 -0800288 * <p>See also {@link
289 * com.android.providers.settings.SettingsProvider#isGlobalOrSecureSettingRestrictedForUser},
290 * which should be in sync with this method.
Makoto Onukid45a4a22015-11-02 17:17:38 -0800291 */
Fyodor Kupolov9cbfc9e2015-10-07 15:52:33 -0700292 private static void applyUserRestriction(Context context, int userId, String key,
Makoto Onuki4f160732015-10-27 17:15:38 -0700293 boolean newValue) {
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800294 if (UserManagerService.DBG) {
295 Log.d(TAG, "Applying user restriction: userId=" + userId
296 + " key=" + key + " value=" + newValue);
297 }
Makoto Onuki4f160732015-10-27 17:15:38 -0700298 // When certain restrictions are cleared, we don't update the system settings,
299 // because these settings are changeable on the Settings UI and we don't know the original
300 // value -- for example LOCATION_MODE might have been off already when the restriction was
301 // set, and in that case even if the restriction is lifted, changing it to ON would be
302 // wrong. So just don't do anything in such a case. If the user hopes to enable location
303 // later, they can do it on the Settings UI.
304
305 final ContentResolver cr = context.getContentResolver();
306 final long id = Binder.clearCallingIdentity();
307 try {
308 switch (key) {
Makoto Onuki4f160732015-10-27 17:15:38 -0700309 case UserManager.DISALLOW_CONFIG_WIFI:
310 if (newValue) {
311 android.provider.Settings.Secure.putIntForUser(cr,
Makoto Onuki28da2e32015-11-20 11:30:44 -0800312 android.provider.Settings.Global
Makoto Onuki4f160732015-10-27 17:15:38 -0700313 .WIFI_NETWORKS_AVAILABLE_NOTIFICATION_ON, 0, userId);
314 }
315 break;
316 case UserManager.DISALLOW_SHARE_LOCATION:
317 if (newValue) {
318 android.provider.Settings.Secure.putIntForUser(cr,
319 android.provider.Settings.Secure.LOCATION_MODE,
320 android.provider.Settings.Secure.LOCATION_MODE_OFF,
321 userId);
Makoto Onuki4f160732015-10-27 17:15:38 -0700322 }
323 // Send out notifications as some clients may want to reread the
324 // value which actually changed due to a restriction having been
325 // applied.
326 final String property =
327 android.provider.Settings.Secure.SYS_PROP_SETTING_VERSION;
328 long version = SystemProperties.getLong(property, 0) + 1;
329 SystemProperties.set(property, Long.toString(version));
330
331 final String name = android.provider.Settings.Secure.LOCATION_PROVIDERS_ALLOWED;
332 final Uri url = Uri.withAppendedPath(
333 android.provider.Settings.Secure.CONTENT_URI, name);
334 context.getContentResolver().notifyChange(url, null, true, userId);
335
336 break;
337 case UserManager.DISALLOW_DEBUGGING_FEATURES:
338 if (newValue) {
339 // Only disable adb if changing for system user, since it is global
340 // TODO: should this be admin user?
341 if (userId == UserHandle.USER_SYSTEM) {
342 android.provider.Settings.Global.putStringForUser(cr,
343 android.provider.Settings.Global.ADB_ENABLED, "0",
344 userId);
345 }
346 }
347 break;
348 case UserManager.ENSURE_VERIFY_APPS:
349 if (newValue) {
350 android.provider.Settings.Global.putStringForUser(
351 context.getContentResolver(),
352 android.provider.Settings.Global.PACKAGE_VERIFIER_ENABLE, "1",
353 userId);
354 android.provider.Settings.Global.putStringForUser(
355 context.getContentResolver(),
356 android.provider.Settings.Global.PACKAGE_VERIFIER_INCLUDE_ADB, "1",
357 userId);
358 }
359 break;
360 case UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES:
361 if (newValue) {
362 android.provider.Settings.Secure.putIntForUser(cr,
363 android.provider.Settings.Secure.INSTALL_NON_MARKET_APPS, 0,
364 userId);
365 }
366 break;
Fyodor Kupolov9cbfc9e2015-10-07 15:52:33 -0700367 case UserManager.DISALLOW_RUN_IN_BACKGROUND:
368 if (newValue) {
369 int currentUser = ActivityManager.getCurrentUser();
370 if (currentUser != userId && userId != UserHandle.USER_SYSTEM) {
371 try {
372 ActivityManagerNative.getDefault().stopUser(userId, false, null);
373 } catch (RemoteException e) {
374 throw e.rethrowAsRuntimeException();
375 }
376 }
377 }
Makoto Onuki4f160732015-10-27 17:15:38 -0700378 }
Makoto Onuki4f160732015-10-27 17:15:38 -0700379 } finally {
380 Binder.restoreCallingIdentity(id);
381 }
382 }
383
Makoto Onukia4f11972015-10-01 13:19:58 -0700384 public static void dumpRestrictions(PrintWriter pw, String prefix, Bundle restrictions) {
385 boolean noneSet = true;
386 if (restrictions != null) {
387 for (String key : restrictions.keySet()) {
388 if (restrictions.getBoolean(key, false)) {
389 pw.println(prefix + key);
390 noneSet = false;
391 }
392 }
Makoto Onuki068c54a2015-10-13 14:34:03 -0700393 if (noneSet) {
394 pw.println(prefix + "none");
395 }
396 } else {
397 pw.println(prefix + "null");
Makoto Onukia4f11972015-10-01 13:19:58 -0700398 }
399 }
400}