UserRestrictionsUtils.java revision dea471ef548f09e04e178c5ec2d71a4b79bdb8f8
1/* 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 21import com.android.internal.util.Preconditions; 22 23import android.annotation.NonNull; 24import android.annotation.Nullable; 25import android.app.ActivityManager; 26import android.app.ActivityManagerNative; 27import android.content.ContentResolver; 28import android.content.Context; 29import android.net.Uri; 30import android.os.Binder; 31import android.os.Bundle; 32import android.os.RemoteException; 33import android.os.SystemProperties; 34import android.os.UserHandle; 35import android.os.UserManager; 36import android.telephony.SubscriptionInfo; 37import android.telephony.SubscriptionManager; 38import android.util.Log; 39 40import org.xmlpull.v1.XmlPullParser; 41import org.xmlpull.v1.XmlSerializer; 42 43import java.io.IOException; 44import java.io.PrintWriter; 45import java.util.List; 46import java.util.Set; 47 48/** 49 * Utility methods for user restrictions. 50 * 51 * <p>See {@link UserManagerService} for the method suffixes. 52 */ 53public class UserRestrictionsUtils { 54 private static final String TAG = "UserRestrictionsUtils"; 55 56 private UserRestrictionsUtils() { 57 } 58 59 public static final Set<String> USER_RESTRICTIONS = Sets.newArraySet( 60 UserManager.DISALLOW_CONFIG_WIFI, 61 UserManager.DISALLOW_MODIFY_ACCOUNTS, 62 UserManager.DISALLOW_INSTALL_APPS, 63 UserManager.DISALLOW_UNINSTALL_APPS, 64 UserManager.DISALLOW_SHARE_LOCATION, 65 UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES, 66 UserManager.DISALLOW_CONFIG_BLUETOOTH, 67 UserManager.DISALLOW_USB_FILE_TRANSFER, 68 UserManager.DISALLOW_CONFIG_CREDENTIALS, 69 UserManager.DISALLOW_REMOVE_USER, 70 UserManager.DISALLOW_DEBUGGING_FEATURES, 71 UserManager.DISALLOW_CONFIG_VPN, 72 UserManager.DISALLOW_CONFIG_TETHERING, 73 UserManager.DISALLOW_NETWORK_RESET, 74 UserManager.DISALLOW_FACTORY_RESET, 75 UserManager.DISALLOW_ADD_USER, 76 UserManager.ENSURE_VERIFY_APPS, 77 UserManager.DISALLOW_CONFIG_CELL_BROADCASTS, 78 UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS, 79 UserManager.DISALLOW_APPS_CONTROL, 80 UserManager.DISALLOW_MOUNT_PHYSICAL_MEDIA, 81 UserManager.DISALLOW_UNMUTE_MICROPHONE, 82 UserManager.DISALLOW_ADJUST_VOLUME, 83 UserManager.DISALLOW_OUTGOING_CALLS, 84 UserManager.DISALLOW_SMS, 85 UserManager.DISALLOW_FUN, 86 UserManager.DISALLOW_CREATE_WINDOWS, 87 UserManager.DISALLOW_CROSS_PROFILE_COPY_PASTE, 88 UserManager.DISALLOW_OUTGOING_BEAM, 89 UserManager.DISALLOW_WALLPAPER, 90 UserManager.DISALLOW_SAFE_BOOT, 91 UserManager.ALLOW_PARENT_PROFILE_APP_LINKING, 92 UserManager.DISALLOW_RECORD_AUDIO, 93 UserManager.DISALLOW_CAMERA, 94 UserManager.DISALLOW_RUN_IN_BACKGROUND, 95 UserManager.DISALLOW_DATA_ROAMING 96 ); 97 98 /** 99 * Set of user restriction which we don't want to persist. 100 */ 101 private static final Set<String> NON_PERSIST_USER_RESTRICTIONS = Sets.newArraySet( 102 UserManager.DISALLOW_RECORD_AUDIO 103 ); 104 105 /** 106 * User restrictions that can not be set by profile owners. 107 */ 108 private static final Set<String> DEVICE_OWNER_ONLY_RESTRICTIONS = Sets.newArraySet( 109 UserManager.DISALLOW_USB_FILE_TRANSFER, 110 UserManager.DISALLOW_CONFIG_TETHERING, 111 UserManager.DISALLOW_NETWORK_RESET, 112 UserManager.DISALLOW_FACTORY_RESET, 113 UserManager.DISALLOW_ADD_USER, 114 UserManager.DISALLOW_CONFIG_CELL_BROADCASTS, 115 UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS, 116 UserManager.DISALLOW_MOUNT_PHYSICAL_MEDIA, 117 UserManager.DISALLOW_SMS, 118 UserManager.DISALLOW_FUN, 119 UserManager.DISALLOW_SAFE_BOOT, 120 UserManager.DISALLOW_CREATE_WINDOWS, 121 UserManager.DISALLOW_DATA_ROAMING 122 ); 123 124 /** 125 * User restrictions that can't be changed by device owner or profile owner. 126 */ 127 private static final Set<String> IMMUTABLE_BY_OWNERS = Sets.newArraySet( 128 UserManager.DISALLOW_RECORD_AUDIO, 129 UserManager.DISALLOW_WALLPAPER 130 ); 131 132 /** 133 * Special user restrictions that can be applied to a user as well as to all users globally, 134 * depending on callers. When device owner sets them, they'll be applied to all users. 135 */ 136 private static final Set<String> GLOBAL_RESTRICTIONS = Sets.newArraySet( 137 UserManager.DISALLOW_ADJUST_VOLUME, 138 UserManager.DISALLOW_RUN_IN_BACKGROUND, 139 UserManager.DISALLOW_UNMUTE_MICROPHONE 140 ); 141 142 public static void writeRestrictions(@NonNull XmlSerializer serializer, 143 @Nullable Bundle restrictions, @NonNull String tag) throws IOException { 144 if (restrictions == null) { 145 return; 146 } 147 148 serializer.startTag(null, tag); 149 for (String key : restrictions.keySet()) { 150 if (NON_PERSIST_USER_RESTRICTIONS.contains(key)) { 151 continue; // Don't persist. 152 } 153 if (USER_RESTRICTIONS.contains(key)) { 154 if (restrictions.getBoolean(key)) { 155 serializer.attribute(null, key, "true"); 156 } 157 continue; 158 } 159 Log.w(TAG, "Unknown user restriction detected: " + key); 160 } 161 serializer.endTag(null, tag); 162 } 163 164 public static void readRestrictions(XmlPullParser parser, Bundle restrictions) 165 throws IOException { 166 for (String key : USER_RESTRICTIONS) { 167 final String value = parser.getAttributeValue(null, key); 168 if (value != null) { 169 restrictions.putBoolean(key, Boolean.parseBoolean(value)); 170 } 171 } 172 } 173 174 /** 175 * @return {@code in} itself when it's not null, or an empty bundle (which can writable). 176 */ 177 public static Bundle nonNull(@Nullable Bundle in) { 178 return in != null ? in : new Bundle(); 179 } 180 181 public static boolean isEmpty(@Nullable Bundle in) { 182 return (in == null) || (in.size() == 0); 183 } 184 185 /** 186 * Creates a copy of the {@code in} Bundle. If {@code in} is null, it'll return an empty 187 * bundle. 188 * 189 * <p>The resulting {@link Bundle} is always writable. (i.e. it won't return 190 * {@link Bundle#EMPTY}) 191 */ 192 public static @NonNull Bundle clone(@Nullable Bundle in) { 193 return (in != null) ? new Bundle(in) : new Bundle(); 194 } 195 196 public static void merge(@NonNull Bundle dest, @Nullable Bundle in) { 197 Preconditions.checkNotNull(dest); 198 Preconditions.checkArgument(dest != in); 199 if (in == null) { 200 return; 201 } 202 for (String key : in.keySet()) { 203 if (in.getBoolean(key, false)) { 204 dest.putBoolean(key, true); 205 } 206 } 207 } 208 209 /** 210 * @return true if a restriction is settable by device owner. 211 */ 212 public static boolean canDeviceOwnerChange(String restriction) { 213 return !IMMUTABLE_BY_OWNERS.contains(restriction); 214 } 215 216 /** 217 * @return true if a restriction is settable by profile owner. Note it takes a user ID because 218 * some restrictions can be changed by PO only when it's running on the system user. 219 */ 220 public static boolean canProfileOwnerChange(String restriction, int userId) { 221 return !IMMUTABLE_BY_OWNERS.contains(restriction) 222 && !(userId != UserHandle.USER_SYSTEM 223 && DEVICE_OWNER_ONLY_RESTRICTIONS.contains(restriction)); 224 } 225 226 /** 227 * Takes restrictions that can be set by device owner, and sort them into what should be applied 228 * globally and what should be applied only on the current user. 229 */ 230 public static void sortToGlobalAndLocal(@Nullable Bundle in, @NonNull Bundle global, 231 @NonNull Bundle local) { 232 if (in == null || in.size() == 0) { 233 return; 234 } 235 for (String key : in.keySet()) { 236 if (!in.getBoolean(key)) { 237 continue; 238 } 239 if (DEVICE_OWNER_ONLY_RESTRICTIONS.contains(key) || GLOBAL_RESTRICTIONS.contains(key)) { 240 global.putBoolean(key, true); 241 } else { 242 local.putBoolean(key, true); 243 } 244 } 245 } 246 247 /** 248 * @return true if two Bundles contain the same user restriction. 249 * A null bundle and an empty bundle are considered to be equal. 250 */ 251 public static boolean areEqual(@Nullable Bundle a, @Nullable Bundle b) { 252 if (a == b) { 253 return true; 254 } 255 if (isEmpty(a)) { 256 return isEmpty(b); 257 } 258 if (isEmpty(b)) { 259 return false; 260 } 261 for (String key : a.keySet()) { 262 if (a.getBoolean(key) != b.getBoolean(key)) { 263 return false; 264 } 265 } 266 for (String key : b.keySet()) { 267 if (a.getBoolean(key) != b.getBoolean(key)) { 268 return false; 269 } 270 } 271 return true; 272 } 273 274 /** 275 * Takes a new use restriction set and the previous set, and apply the restrictions that have 276 * changed. 277 * 278 * <p>Note this method is called by {@link UserManagerService} without holding any locks. 279 */ 280 public static void applyUserRestrictions(Context context, int userId, 281 Bundle newRestrictions, Bundle prevRestrictions) { 282 for (String key : USER_RESTRICTIONS) { 283 final boolean newValue = newRestrictions.getBoolean(key); 284 final boolean prevValue = prevRestrictions.getBoolean(key); 285 286 if (newValue != prevValue) { 287 applyUserRestriction(context, userId, key, newValue); 288 } 289 } 290 } 291 292 /** 293 * Apply each user restriction. 294 * 295 * <p>See also {@link 296 * com.android.providers.settings.SettingsProvider#isGlobalOrSecureSettingRestrictedForUser}, 297 * which should be in sync with this method. 298 */ 299 private static void applyUserRestriction(Context context, int userId, String key, 300 boolean newValue) { 301 if (UserManagerService.DBG) { 302 Log.d(TAG, "Applying user restriction: userId=" + userId 303 + " key=" + key + " value=" + newValue); 304 } 305 // When certain restrictions are cleared, we don't update the system settings, 306 // because these settings are changeable on the Settings UI and we don't know the original 307 // value -- for example LOCATION_MODE might have been off already when the restriction was 308 // set, and in that case even if the restriction is lifted, changing it to ON would be 309 // wrong. So just don't do anything in such a case. If the user hopes to enable location 310 // later, they can do it on the Settings UI. 311 312 final ContentResolver cr = context.getContentResolver(); 313 final long id = Binder.clearCallingIdentity(); 314 try { 315 switch (key) { 316 case UserManager.DISALLOW_CONFIG_WIFI: 317 if (newValue) { 318 android.provider.Settings.Secure.putIntForUser(cr, 319 android.provider.Settings.Global 320 .WIFI_NETWORKS_AVAILABLE_NOTIFICATION_ON, 0, userId); 321 } 322 break; 323 case UserManager.DISALLOW_DATA_ROAMING: 324 if (newValue) { 325 // DISALLOW_DATA_ROAMING user restriction is set. 326 327 // Multi sim device. 328 SubscriptionManager subscriptionManager = new SubscriptionManager(context); 329 final List<SubscriptionInfo> subscriptionInfoList = 330 subscriptionManager.getActiveSubscriptionInfoList(); 331 if (subscriptionInfoList != null) { 332 for (SubscriptionInfo subInfo : subscriptionInfoList) { 333 android.provider.Settings.Global.putStringForUser(cr, 334 android.provider.Settings.Global.DATA_ROAMING 335 + subInfo.getSubscriptionId(), "0", userId); 336 } 337 } 338 339 // Single sim device. 340 android.provider.Settings.Global.putStringForUser(cr, 341 android.provider.Settings.Global.DATA_ROAMING, "0", userId); 342 } 343 break; 344 case UserManager.DISALLOW_SHARE_LOCATION: 345 if (newValue) { 346 android.provider.Settings.Secure.putIntForUser(cr, 347 android.provider.Settings.Secure.LOCATION_MODE, 348 android.provider.Settings.Secure.LOCATION_MODE_OFF, 349 userId); 350 } 351 // Send out notifications as some clients may want to reread the 352 // value which actually changed due to a restriction having been 353 // applied. 354 final String property = 355 android.provider.Settings.Secure.SYS_PROP_SETTING_VERSION; 356 long version = SystemProperties.getLong(property, 0) + 1; 357 SystemProperties.set(property, Long.toString(version)); 358 359 final String name = android.provider.Settings.Secure.LOCATION_PROVIDERS_ALLOWED; 360 final Uri url = Uri.withAppendedPath( 361 android.provider.Settings.Secure.CONTENT_URI, name); 362 context.getContentResolver().notifyChange(url, null, true, userId); 363 364 break; 365 case UserManager.DISALLOW_DEBUGGING_FEATURES: 366 if (newValue) { 367 // Only disable adb if changing for system user, since it is global 368 // TODO: should this be admin user? 369 if (userId == UserHandle.USER_SYSTEM) { 370 android.provider.Settings.Global.putStringForUser(cr, 371 android.provider.Settings.Global.ADB_ENABLED, "0", 372 userId); 373 } 374 } 375 break; 376 case UserManager.ENSURE_VERIFY_APPS: 377 if (newValue) { 378 android.provider.Settings.Global.putStringForUser( 379 context.getContentResolver(), 380 android.provider.Settings.Global.PACKAGE_VERIFIER_ENABLE, "1", 381 userId); 382 android.provider.Settings.Global.putStringForUser( 383 context.getContentResolver(), 384 android.provider.Settings.Global.PACKAGE_VERIFIER_INCLUDE_ADB, "1", 385 userId); 386 } 387 break; 388 case UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES: 389 if (newValue) { 390 android.provider.Settings.Secure.putIntForUser(cr, 391 android.provider.Settings.Secure.INSTALL_NON_MARKET_APPS, 0, 392 userId); 393 } 394 break; 395 case UserManager.DISALLOW_RUN_IN_BACKGROUND: 396 if (newValue) { 397 int currentUser = ActivityManager.getCurrentUser(); 398 if (currentUser != userId && userId != UserHandle.USER_SYSTEM) { 399 try { 400 ActivityManagerNative.getDefault().stopUser(userId, false, null); 401 } catch (RemoteException e) { 402 throw e.rethrowAsRuntimeException(); 403 } 404 } 405 } 406 } 407 } finally { 408 Binder.restoreCallingIdentity(id); 409 } 410 } 411 412 public static void dumpRestrictions(PrintWriter pw, String prefix, Bundle restrictions) { 413 boolean noneSet = true; 414 if (restrictions != null) { 415 for (String key : restrictions.keySet()) { 416 if (restrictions.getBoolean(key, false)) { 417 pw.println(prefix + key); 418 noneSet = false; 419 } 420 } 421 if (noneSet) { 422 pw.println(prefix + "none"); 423 } 424 } else { 425 pw.println(prefix + "null"); 426 } 427 } 428} 429