Makoto Onuki | a4f1197 | 2015-10-01 13:19:58 -0700 | [diff] [blame] | 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 | |
| 17 | package com.android.server.pm; |
| 18 | |
| 19 | import com.google.android.collect.Sets; |
| 20 | |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 21 | import com.android.internal.util.Preconditions; |
| 22 | |
| 23 | import android.annotation.NonNull; |
| 24 | import android.annotation.Nullable; |
Fyodor Kupolov | 9cbfc9e | 2015-10-07 15:52:33 -0700 | [diff] [blame] | 25 | import android.app.ActivityManager; |
Makoto Onuki | 4f16073 | 2015-10-27 17:15:38 -0700 | [diff] [blame] | 26 | import android.content.ContentResolver; |
| 27 | import android.content.Context; |
Makoto Onuki | 4f16073 | 2015-10-27 17:15:38 -0700 | [diff] [blame] | 28 | import android.os.Binder; |
Makoto Onuki | a4f1197 | 2015-10-01 13:19:58 -0700 | [diff] [blame] | 29 | import android.os.Bundle; |
Fyodor Kupolov | 9cbfc9e | 2015-10-07 15:52:33 -0700 | [diff] [blame] | 30 | import android.os.RemoteException; |
Makoto Onuki | 4f16073 | 2015-10-27 17:15:38 -0700 | [diff] [blame] | 31 | import android.os.UserHandle; |
Makoto Onuki | a4f1197 | 2015-10-01 13:19:58 -0700 | [diff] [blame] | 32 | import android.os.UserManager; |
Mahaver Chopra | 830e32c | 2016-05-17 18:53:09 +0100 | [diff] [blame] | 33 | import android.service.persistentdata.PersistentDataBlockManager; |
Mahaver Chopra | dea471e | 2015-12-17 11:02:37 +0000 | [diff] [blame] | 34 | import android.telephony.SubscriptionInfo; |
| 35 | import android.telephony.SubscriptionManager; |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 36 | import android.util.Log; |
Makoto Onuki | 1f1ceef | 2016-01-28 11:32:32 -0800 | [diff] [blame] | 37 | import android.util.Slog; |
Makoto Onuki | a4f1197 | 2015-10-01 13:19:58 -0700 | [diff] [blame] | 38 | |
| 39 | import org.xmlpull.v1.XmlPullParser; |
| 40 | import org.xmlpull.v1.XmlSerializer; |
| 41 | |
| 42 | import java.io.IOException; |
| 43 | import java.io.PrintWriter; |
Mahaver Chopra | dea471e | 2015-12-17 11:02:37 +0000 | [diff] [blame] | 44 | import java.util.List; |
Makoto Onuki | a4f1197 | 2015-10-01 13:19:58 -0700 | [diff] [blame] | 45 | import java.util.Set; |
| 46 | |
Makoto Onuki | d45a4a2 | 2015-11-02 17:17:38 -0800 | [diff] [blame] | 47 | /** |
Mahaver Chopra | dea471e | 2015-12-17 11:02:37 +0000 | [diff] [blame] | 48 | * Utility methods for user restrictions. |
Makoto Onuki | d45a4a2 | 2015-11-02 17:17:38 -0800 | [diff] [blame] | 49 | * |
| 50 | * <p>See {@link UserManagerService} for the method suffixes. |
| 51 | */ |
Makoto Onuki | a4f1197 | 2015-10-01 13:19:58 -0700 | [diff] [blame] | 52 | public class UserRestrictionsUtils { |
Makoto Onuki | 4f16073 | 2015-10-27 17:15:38 -0700 | [diff] [blame] | 53 | private static final String TAG = "UserRestrictionsUtils"; |
| 54 | |
Makoto Onuki | a4f1197 | 2015-10-01 13:19:58 -0700 | [diff] [blame] | 55 | private UserRestrictionsUtils() { |
| 56 | } |
| 57 | |
Makoto Onuki | 1f1ceef | 2016-01-28 11:32:32 -0800 | [diff] [blame] | 58 | private static Set<String> newSetWithUniqueCheck(String[] strings) { |
| 59 | final Set<String> ret = Sets.newArraySet(strings); |
| 60 | |
| 61 | // Make sure there's no overlap. |
| 62 | Preconditions.checkState(ret.size() == strings.length); |
| 63 | return ret; |
| 64 | } |
| 65 | |
| 66 | public static final Set<String> USER_RESTRICTIONS = newSetWithUniqueCheck(new String[] { |
Makoto Onuki | a4f1197 | 2015-10-01 13:19:58 -0700 | [diff] [blame] | 67 | UserManager.DISALLOW_CONFIG_WIFI, |
| 68 | UserManager.DISALLOW_MODIFY_ACCOUNTS, |
| 69 | UserManager.DISALLOW_INSTALL_APPS, |
| 70 | UserManager.DISALLOW_UNINSTALL_APPS, |
| 71 | UserManager.DISALLOW_SHARE_LOCATION, |
| 72 | UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES, |
| 73 | UserManager.DISALLOW_CONFIG_BLUETOOTH, |
Lenka Trochtova | 63d5e4a7 | 2016-12-02 12:19:39 +0100 | [diff] [blame] | 74 | UserManager.DISALLOW_BLUETOOTH, |
Makoto Onuki | a4f1197 | 2015-10-01 13:19:58 -0700 | [diff] [blame] | 75 | UserManager.DISALLOW_USB_FILE_TRANSFER, |
| 76 | UserManager.DISALLOW_CONFIG_CREDENTIALS, |
| 77 | UserManager.DISALLOW_REMOVE_USER, |
Esteban Talavera | 6c9116a | 2016-11-24 16:12:44 +0000 | [diff] [blame] | 78 | UserManager.DISALLOW_REMOVE_MANAGED_PROFILE, |
Makoto Onuki | a4f1197 | 2015-10-01 13:19:58 -0700 | [diff] [blame] | 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, |
Esteban Talavera | 6c9116a | 2016-11-24 16:12:44 +0000 | [diff] [blame] | 85 | UserManager.DISALLOW_ADD_MANAGED_PROFILE, |
Makoto Onuki | a4f1197 | 2015-10-01 13:19:58 -0700 | [diff] [blame] | 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 Kupolov | 9cbfc9e | 2015-10-07 15:52:33 -0700 | [diff] [blame] | 103 | UserManager.DISALLOW_CAMERA, |
Mahaver Chopra | dea471e | 2015-12-17 11:02:37 +0000 | [diff] [blame] | 104 | UserManager.DISALLOW_RUN_IN_BACKGROUND, |
Oleksandr Peletskyi | 7f1f1df | 2016-01-18 15:40:21 +0100 | [diff] [blame] | 105 | UserManager.DISALLOW_DATA_ROAMING, |
Oleksandr Peletskyi | f251981 | 2016-01-26 20:16:06 +0100 | [diff] [blame] | 106 | UserManager.DISALLOW_SET_USER_ICON, |
Mahaver Chopra | 3d9805d | 2016-07-07 16:25:05 +0100 | [diff] [blame] | 107 | UserManager.DISALLOW_SET_WALLPAPER, |
Tony Mak | c120511 | 2016-07-22 16:02:59 +0100 | [diff] [blame] | 108 | UserManager.DISALLOW_OEM_UNLOCK, |
| 109 | UserManager.DISALLLOW_UNMUTE_DEVICE, |
Makoto Onuki | 1f1ceef | 2016-01-28 11:32:32 -0800 | [diff] [blame] | 110 | }); |
Makoto Onuki | a4f1197 | 2015-10-01 13:19:58 -0700 | [diff] [blame] | 111 | |
| 112 | /** |
| 113 | * Set of user restriction which we don't want to persist. |
| 114 | */ |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 115 | private static final Set<String> NON_PERSIST_USER_RESTRICTIONS = Sets.newArraySet( |
| 116 | UserManager.DISALLOW_RECORD_AUDIO |
| 117 | ); |
Makoto Onuki | a4f1197 | 2015-10-01 13:19:58 -0700 | [diff] [blame] | 118 | |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 119 | /** |
| 120 | * User restrictions that can not be set by profile owners. |
| 121 | */ |
| 122 | private static final Set<String> DEVICE_OWNER_ONLY_RESTRICTIONS = Sets.newArraySet( |
Lenka Trochtova | 63d5e4a7 | 2016-12-02 12:19:39 +0100 | [diff] [blame] | 123 | UserManager.DISALLOW_BLUETOOTH, |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 124 | UserManager.DISALLOW_USB_FILE_TRANSFER, |
| 125 | UserManager.DISALLOW_CONFIG_TETHERING, |
| 126 | UserManager.DISALLOW_NETWORK_RESET, |
| 127 | UserManager.DISALLOW_FACTORY_RESET, |
| 128 | UserManager.DISALLOW_ADD_USER, |
| 129 | UserManager.DISALLOW_CONFIG_CELL_BROADCASTS, |
| 130 | UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS, |
| 131 | UserManager.DISALLOW_MOUNT_PHYSICAL_MEDIA, |
| 132 | UserManager.DISALLOW_SMS, |
| 133 | UserManager.DISALLOW_FUN, |
| 134 | UserManager.DISALLOW_SAFE_BOOT, |
Mahaver Chopra | dea471e | 2015-12-17 11:02:37 +0000 | [diff] [blame] | 135 | UserManager.DISALLOW_CREATE_WINDOWS, |
| 136 | UserManager.DISALLOW_DATA_ROAMING |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 137 | ); |
| 138 | |
| 139 | /** |
| 140 | * User restrictions that can't be changed by device owner or profile owner. |
| 141 | */ |
| 142 | private static final Set<String> IMMUTABLE_BY_OWNERS = Sets.newArraySet( |
| 143 | UserManager.DISALLOW_RECORD_AUDIO, |
Mahaver Chopra | 3d9805d | 2016-07-07 16:25:05 +0100 | [diff] [blame] | 144 | UserManager.DISALLOW_WALLPAPER, |
| 145 | UserManager.DISALLOW_OEM_UNLOCK |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 146 | ); |
| 147 | |
| 148 | /** |
| 149 | * Special user restrictions that can be applied to a user as well as to all users globally, |
| 150 | * depending on callers. When device owner sets them, they'll be applied to all users. |
| 151 | */ |
| 152 | private static final Set<String> GLOBAL_RESTRICTIONS = Sets.newArraySet( |
| 153 | UserManager.DISALLOW_ADJUST_VOLUME, |
Fyodor Kupolov | 9cbfc9e | 2015-10-07 15:52:33 -0700 | [diff] [blame] | 154 | UserManager.DISALLOW_RUN_IN_BACKGROUND, |
Tony Mak | c120511 | 2016-07-22 16:02:59 +0100 | [diff] [blame] | 155 | UserManager.DISALLOW_UNMUTE_MICROPHONE, |
| 156 | UserManager.DISALLLOW_UNMUTE_DEVICE |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 157 | ); |
| 158 | |
Makoto Onuki | 1f1ceef | 2016-01-28 11:32:32 -0800 | [diff] [blame] | 159 | /** |
Esteban Talavera | 6c9116a | 2016-11-24 16:12:44 +0000 | [diff] [blame] | 160 | * User restrictions that default to {@code true} for device owners. |
| 161 | */ |
| 162 | private static final Set<String> DEFAULT_ENABLED_FOR_DEVICE_OWNERS = Sets.newArraySet( |
| 163 | UserManager.DISALLOW_ADD_MANAGED_PROFILE |
| 164 | ); |
| 165 | |
| 166 | /** |
Makoto Onuki | 1f1ceef | 2016-01-28 11:32:32 -0800 | [diff] [blame] | 167 | * Throws {@link IllegalArgumentException} if the given restriction name is invalid. |
| 168 | */ |
| 169 | public static boolean isValidRestriction(@NonNull String restriction) { |
| 170 | if (!USER_RESTRICTIONS.contains(restriction)) { |
Makoto Onuki | ad5619d | 2016-02-10 13:41:15 -0800 | [diff] [blame] | 171 | Slog.e(TAG, "Unknown restriction: " + restriction); |
Makoto Onuki | 1f1ceef | 2016-01-28 11:32:32 -0800 | [diff] [blame] | 172 | return false; |
| 173 | } |
| 174 | return true; |
| 175 | } |
| 176 | |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 177 | public static void writeRestrictions(@NonNull XmlSerializer serializer, |
| 178 | @Nullable Bundle restrictions, @NonNull String tag) throws IOException { |
| 179 | if (restrictions == null) { |
| 180 | return; |
| 181 | } |
| 182 | |
Makoto Onuki | a4f1197 | 2015-10-01 13:19:58 -0700 | [diff] [blame] | 183 | serializer.startTag(null, tag); |
Makoto Onuki | ac65e1e | 2015-11-20 15:33:17 -0800 | [diff] [blame] | 184 | for (String key : restrictions.keySet()) { |
| 185 | if (NON_PERSIST_USER_RESTRICTIONS.contains(key)) { |
| 186 | continue; // Don't persist. |
Makoto Onuki | a4f1197 | 2015-10-01 13:19:58 -0700 | [diff] [blame] | 187 | } |
Makoto Onuki | ac65e1e | 2015-11-20 15:33:17 -0800 | [diff] [blame] | 188 | if (USER_RESTRICTIONS.contains(key)) { |
| 189 | if (restrictions.getBoolean(key)) { |
| 190 | serializer.attribute(null, key, "true"); |
| 191 | } |
| 192 | continue; |
| 193 | } |
| 194 | Log.w(TAG, "Unknown user restriction detected: " + key); |
Makoto Onuki | a4f1197 | 2015-10-01 13:19:58 -0700 | [diff] [blame] | 195 | } |
| 196 | serializer.endTag(null, tag); |
| 197 | } |
| 198 | |
Esteban Talavera | c48b20f | 2016-08-11 11:23:40 +0100 | [diff] [blame] | 199 | public static void readRestrictions(XmlPullParser parser, Bundle restrictions) { |
Makoto Onuki | a4f1197 | 2015-10-01 13:19:58 -0700 | [diff] [blame] | 200 | for (String key : USER_RESTRICTIONS) { |
| 201 | final String value = parser.getAttributeValue(null, key); |
| 202 | if (value != null) { |
| 203 | restrictions.putBoolean(key, Boolean.parseBoolean(value)); |
| 204 | } |
| 205 | } |
| 206 | } |
| 207 | |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 208 | /** |
| 209 | * @return {@code in} itself when it's not null, or an empty bundle (which can writable). |
| 210 | */ |
| 211 | public static Bundle nonNull(@Nullable Bundle in) { |
| 212 | return in != null ? in : new Bundle(); |
| 213 | } |
| 214 | |
| 215 | public static boolean isEmpty(@Nullable Bundle in) { |
| 216 | return (in == null) || (in.size() == 0); |
| 217 | } |
| 218 | |
| 219 | /** |
| 220 | * Creates a copy of the {@code in} Bundle. If {@code in} is null, it'll return an empty |
| 221 | * bundle. |
| 222 | * |
| 223 | * <p>The resulting {@link Bundle} is always writable. (i.e. it won't return |
| 224 | * {@link Bundle#EMPTY}) |
| 225 | */ |
| 226 | public static @NonNull Bundle clone(@Nullable Bundle in) { |
| 227 | return (in != null) ? new Bundle(in) : new Bundle(); |
| 228 | } |
| 229 | |
| 230 | public static void merge(@NonNull Bundle dest, @Nullable Bundle in) { |
| 231 | Preconditions.checkNotNull(dest); |
| 232 | Preconditions.checkArgument(dest != in); |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 233 | if (in == null) { |
| 234 | return; |
| 235 | } |
| 236 | for (String key : in.keySet()) { |
| 237 | if (in.getBoolean(key, false)) { |
| 238 | dest.putBoolean(key, true); |
| 239 | } |
| 240 | } |
| 241 | } |
| 242 | |
Makoto Onuki | 4f16073 | 2015-10-27 17:15:38 -0700 | [diff] [blame] | 243 | /** |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 244 | * @return true if a restriction is settable by device owner. |
| 245 | */ |
| 246 | public static boolean canDeviceOwnerChange(String restriction) { |
| 247 | return !IMMUTABLE_BY_OWNERS.contains(restriction); |
| 248 | } |
| 249 | |
| 250 | /** |
Makoto Onuki | 5485ed4 | 2015-12-09 11:38:23 -0800 | [diff] [blame] | 251 | * @return true if a restriction is settable by profile owner. Note it takes a user ID because |
| 252 | * some restrictions can be changed by PO only when it's running on the system user. |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 253 | */ |
Makoto Onuki | 5485ed4 | 2015-12-09 11:38:23 -0800 | [diff] [blame] | 254 | public static boolean canProfileOwnerChange(String restriction, int userId) { |
| 255 | return !IMMUTABLE_BY_OWNERS.contains(restriction) |
| 256 | && !(userId != UserHandle.USER_SYSTEM |
| 257 | && DEVICE_OWNER_ONLY_RESTRICTIONS.contains(restriction)); |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | /** |
Esteban Talavera | 6c9116a | 2016-11-24 16:12:44 +0000 | [diff] [blame] | 261 | * Returns the user restrictions that default to {@code true} for device owners. |
Nicolas Prevot | 2ea46fe | 2017-01-05 10:29:34 +0000 | [diff] [blame^] | 262 | * These user restrictions are local, though. ie only for the device owner's user id. |
Esteban Talavera | 6c9116a | 2016-11-24 16:12:44 +0000 | [diff] [blame] | 263 | */ |
| 264 | public static @NonNull Set<String> getDefaultEnabledForDeviceOwner() { |
| 265 | return DEFAULT_ENABLED_FOR_DEVICE_OWNERS; |
| 266 | } |
| 267 | |
| 268 | /** |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 269 | * Takes restrictions that can be set by device owner, and sort them into what should be applied |
| 270 | * globally and what should be applied only on the current user. |
| 271 | */ |
| 272 | public static void sortToGlobalAndLocal(@Nullable Bundle in, @NonNull Bundle global, |
| 273 | @NonNull Bundle local) { |
| 274 | if (in == null || in.size() == 0) { |
| 275 | return; |
| 276 | } |
| 277 | for (String key : in.keySet()) { |
| 278 | if (!in.getBoolean(key)) { |
| 279 | continue; |
| 280 | } |
| 281 | if (DEVICE_OWNER_ONLY_RESTRICTIONS.contains(key) || GLOBAL_RESTRICTIONS.contains(key)) { |
| 282 | global.putBoolean(key, true); |
| 283 | } else { |
| 284 | local.putBoolean(key, true); |
| 285 | } |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | /** |
| 290 | * @return true if two Bundles contain the same user restriction. |
| 291 | * A null bundle and an empty bundle are considered to be equal. |
| 292 | */ |
| 293 | public static boolean areEqual(@Nullable Bundle a, @Nullable Bundle b) { |
| 294 | if (a == b) { |
| 295 | return true; |
| 296 | } |
| 297 | if (isEmpty(a)) { |
| 298 | return isEmpty(b); |
| 299 | } |
| 300 | if (isEmpty(b)) { |
| 301 | return false; |
| 302 | } |
| 303 | for (String key : a.keySet()) { |
| 304 | if (a.getBoolean(key) != b.getBoolean(key)) { |
| 305 | return false; |
| 306 | } |
| 307 | } |
| 308 | for (String key : b.keySet()) { |
| 309 | if (a.getBoolean(key) != b.getBoolean(key)) { |
| 310 | return false; |
| 311 | } |
| 312 | } |
| 313 | return true; |
| 314 | } |
| 315 | |
| 316 | /** |
Makoto Onuki | 4f16073 | 2015-10-27 17:15:38 -0700 | [diff] [blame] | 317 | * Takes a new use restriction set and the previous set, and apply the restrictions that have |
| 318 | * changed. |
Makoto Onuki | d45a4a2 | 2015-11-02 17:17:38 -0800 | [diff] [blame] | 319 | * |
Fyodor Kupolov | 9cbfc9e | 2015-10-07 15:52:33 -0700 | [diff] [blame] | 320 | * <p>Note this method is called by {@link UserManagerService} without holding any locks. |
Makoto Onuki | 4f16073 | 2015-10-27 17:15:38 -0700 | [diff] [blame] | 321 | */ |
Fyodor Kupolov | 9cbfc9e | 2015-10-07 15:52:33 -0700 | [diff] [blame] | 322 | public static void applyUserRestrictions(Context context, int userId, |
Makoto Onuki | d45a4a2 | 2015-11-02 17:17:38 -0800 | [diff] [blame] | 323 | Bundle newRestrictions, Bundle prevRestrictions) { |
Makoto Onuki | 4f16073 | 2015-10-27 17:15:38 -0700 | [diff] [blame] | 324 | for (String key : USER_RESTRICTIONS) { |
| 325 | final boolean newValue = newRestrictions.getBoolean(key); |
| 326 | final boolean prevValue = prevRestrictions.getBoolean(key); |
| 327 | |
| 328 | if (newValue != prevValue) { |
Fyodor Kupolov | 9cbfc9e | 2015-10-07 15:52:33 -0700 | [diff] [blame] | 329 | applyUserRestriction(context, userId, key, newValue); |
Makoto Onuki | 4f16073 | 2015-10-27 17:15:38 -0700 | [diff] [blame] | 330 | } |
| 331 | } |
| 332 | } |
Fyodor Kupolov | 9cbfc9e | 2015-10-07 15:52:33 -0700 | [diff] [blame] | 333 | |
Makoto Onuki | d45a4a2 | 2015-11-02 17:17:38 -0800 | [diff] [blame] | 334 | /** |
| 335 | * Apply each user restriction. |
| 336 | * |
Makoto Onuki | 28da2e3 | 2015-11-20 11:30:44 -0800 | [diff] [blame] | 337 | * <p>See also {@link |
| 338 | * com.android.providers.settings.SettingsProvider#isGlobalOrSecureSettingRestrictedForUser}, |
| 339 | * which should be in sync with this method. |
Makoto Onuki | d45a4a2 | 2015-11-02 17:17:38 -0800 | [diff] [blame] | 340 | */ |
Fyodor Kupolov | 9cbfc9e | 2015-10-07 15:52:33 -0700 | [diff] [blame] | 341 | private static void applyUserRestriction(Context context, int userId, String key, |
Makoto Onuki | 4f16073 | 2015-10-27 17:15:38 -0700 | [diff] [blame] | 342 | boolean newValue) { |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 343 | if (UserManagerService.DBG) { |
| 344 | Log.d(TAG, "Applying user restriction: userId=" + userId |
| 345 | + " key=" + key + " value=" + newValue); |
| 346 | } |
Makoto Onuki | 4f16073 | 2015-10-27 17:15:38 -0700 | [diff] [blame] | 347 | // When certain restrictions are cleared, we don't update the system settings, |
| 348 | // because these settings are changeable on the Settings UI and we don't know the original |
| 349 | // value -- for example LOCATION_MODE might have been off already when the restriction was |
| 350 | // set, and in that case even if the restriction is lifted, changing it to ON would be |
| 351 | // wrong. So just don't do anything in such a case. If the user hopes to enable location |
| 352 | // later, they can do it on the Settings UI. |
Benjamin Franz | 0ff13fc | 2016-07-12 13:42:21 +0100 | [diff] [blame] | 353 | // WARNING: Remember that Settings.Global and Settings.Secure are changeable via adb. |
| 354 | // To prevent this from happening for a given user restriction, you have to add a check to |
| 355 | // SettingsProvider.isGlobalOrSecureSettingRestrictedForUser. |
Makoto Onuki | 4f16073 | 2015-10-27 17:15:38 -0700 | [diff] [blame] | 356 | |
| 357 | final ContentResolver cr = context.getContentResolver(); |
| 358 | final long id = Binder.clearCallingIdentity(); |
| 359 | try { |
| 360 | switch (key) { |
Makoto Onuki | 4f16073 | 2015-10-27 17:15:38 -0700 | [diff] [blame] | 361 | case UserManager.DISALLOW_CONFIG_WIFI: |
| 362 | if (newValue) { |
| 363 | android.provider.Settings.Secure.putIntForUser(cr, |
Makoto Onuki | 28da2e3 | 2015-11-20 11:30:44 -0800 | [diff] [blame] | 364 | android.provider.Settings.Global |
Makoto Onuki | 4f16073 | 2015-10-27 17:15:38 -0700 | [diff] [blame] | 365 | .WIFI_NETWORKS_AVAILABLE_NOTIFICATION_ON, 0, userId); |
| 366 | } |
| 367 | break; |
Mahaver Chopra | dea471e | 2015-12-17 11:02:37 +0000 | [diff] [blame] | 368 | case UserManager.DISALLOW_DATA_ROAMING: |
| 369 | if (newValue) { |
| 370 | // DISALLOW_DATA_ROAMING user restriction is set. |
| 371 | |
| 372 | // Multi sim device. |
| 373 | SubscriptionManager subscriptionManager = new SubscriptionManager(context); |
| 374 | final List<SubscriptionInfo> subscriptionInfoList = |
| 375 | subscriptionManager.getActiveSubscriptionInfoList(); |
| 376 | if (subscriptionInfoList != null) { |
| 377 | for (SubscriptionInfo subInfo : subscriptionInfoList) { |
| 378 | android.provider.Settings.Global.putStringForUser(cr, |
| 379 | android.provider.Settings.Global.DATA_ROAMING |
| 380 | + subInfo.getSubscriptionId(), "0", userId); |
| 381 | } |
| 382 | } |
| 383 | |
| 384 | // Single sim device. |
| 385 | android.provider.Settings.Global.putStringForUser(cr, |
| 386 | android.provider.Settings.Global.DATA_ROAMING, "0", userId); |
| 387 | } |
| 388 | break; |
Makoto Onuki | 4f16073 | 2015-10-27 17:15:38 -0700 | [diff] [blame] | 389 | case UserManager.DISALLOW_SHARE_LOCATION: |
| 390 | if (newValue) { |
| 391 | android.provider.Settings.Secure.putIntForUser(cr, |
| 392 | android.provider.Settings.Secure.LOCATION_MODE, |
| 393 | android.provider.Settings.Secure.LOCATION_MODE_OFF, |
| 394 | userId); |
Makoto Onuki | 4f16073 | 2015-10-27 17:15:38 -0700 | [diff] [blame] | 395 | } |
Makoto Onuki | 4f16073 | 2015-10-27 17:15:38 -0700 | [diff] [blame] | 396 | break; |
| 397 | case UserManager.DISALLOW_DEBUGGING_FEATURES: |
| 398 | if (newValue) { |
| 399 | // Only disable adb if changing for system user, since it is global |
| 400 | // TODO: should this be admin user? |
| 401 | if (userId == UserHandle.USER_SYSTEM) { |
| 402 | android.provider.Settings.Global.putStringForUser(cr, |
| 403 | android.provider.Settings.Global.ADB_ENABLED, "0", |
| 404 | userId); |
| 405 | } |
| 406 | } |
| 407 | break; |
| 408 | case UserManager.ENSURE_VERIFY_APPS: |
| 409 | if (newValue) { |
| 410 | android.provider.Settings.Global.putStringForUser( |
| 411 | context.getContentResolver(), |
| 412 | android.provider.Settings.Global.PACKAGE_VERIFIER_ENABLE, "1", |
| 413 | userId); |
| 414 | android.provider.Settings.Global.putStringForUser( |
| 415 | context.getContentResolver(), |
| 416 | android.provider.Settings.Global.PACKAGE_VERIFIER_INCLUDE_ADB, "1", |
| 417 | userId); |
| 418 | } |
| 419 | break; |
| 420 | case UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES: |
| 421 | if (newValue) { |
| 422 | android.provider.Settings.Secure.putIntForUser(cr, |
| 423 | android.provider.Settings.Secure.INSTALL_NON_MARKET_APPS, 0, |
| 424 | userId); |
| 425 | } |
| 426 | break; |
Fyodor Kupolov | 9cbfc9e | 2015-10-07 15:52:33 -0700 | [diff] [blame] | 427 | case UserManager.DISALLOW_RUN_IN_BACKGROUND: |
| 428 | if (newValue) { |
| 429 | int currentUser = ActivityManager.getCurrentUser(); |
| 430 | if (currentUser != userId && userId != UserHandle.USER_SYSTEM) { |
| 431 | try { |
Sudheer Shanka | dc589ac | 2016-11-10 15:30:17 -0800 | [diff] [blame] | 432 | ActivityManager.getService().stopUser(userId, false, null); |
Fyodor Kupolov | 9cbfc9e | 2015-10-07 15:52:33 -0700 | [diff] [blame] | 433 | } catch (RemoteException e) { |
| 434 | throw e.rethrowAsRuntimeException(); |
| 435 | } |
| 436 | } |
| 437 | } |
Lenka Trochtova | 6474f0e | 2016-03-24 16:43:10 +0100 | [diff] [blame] | 438 | break; |
| 439 | case UserManager.DISALLOW_SAFE_BOOT: |
| 440 | // Unlike with the other restrictions, we want to propagate the new value to |
| 441 | // the system settings even if it is false. The other restrictions modify |
| 442 | // settings which could be manually changed by the user from the Settings app |
| 443 | // after the policies enforcing these restrictions have been revoked, so we |
| 444 | // leave re-setting of those settings to the user. |
| 445 | android.provider.Settings.Global.putInt( |
| 446 | context.getContentResolver(), |
| 447 | android.provider.Settings.Global.SAFE_BOOT_DISALLOWED, |
| 448 | newValue ? 1 : 0); |
| 449 | break; |
Mahaver Chopra | 830e32c | 2016-05-17 18:53:09 +0100 | [diff] [blame] | 450 | case UserManager.DISALLOW_FACTORY_RESET: |
Mahaver Chopra | 3d9805d | 2016-07-07 16:25:05 +0100 | [diff] [blame] | 451 | case UserManager.DISALLOW_OEM_UNLOCK: |
Mahaver Chopra | 830e32c | 2016-05-17 18:53:09 +0100 | [diff] [blame] | 452 | if (newValue) { |
| 453 | PersistentDataBlockManager manager = (PersistentDataBlockManager) context |
| 454 | .getSystemService(Context.PERSISTENT_DATA_BLOCK_SERVICE); |
Esteban Talavera | c48b20f | 2016-08-11 11:23:40 +0100 | [diff] [blame] | 455 | if (manager != null |
| 456 | && manager.getOemUnlockEnabled() |
| 457 | && manager.getFlashLockState() |
| 458 | != PersistentDataBlockManager.FLASH_LOCK_UNLOCKED) { |
| 459 | // Only disable OEM unlock if the bootloader is locked. If it's already |
| 460 | // unlocked, setting the OEM unlock enabled flag to false has no effect |
| 461 | // (the bootloader would remain unlocked). |
Mahaver Chopra | 830e32c | 2016-05-17 18:53:09 +0100 | [diff] [blame] | 462 | manager.setOemUnlockEnabled(false); |
| 463 | } |
| 464 | } |
Tony Mak | c120511 | 2016-07-22 16:02:59 +0100 | [diff] [blame] | 465 | break; |
Makoto Onuki | 4f16073 | 2015-10-27 17:15:38 -0700 | [diff] [blame] | 466 | } |
Makoto Onuki | 4f16073 | 2015-10-27 17:15:38 -0700 | [diff] [blame] | 467 | } finally { |
| 468 | Binder.restoreCallingIdentity(id); |
| 469 | } |
| 470 | } |
| 471 | |
Makoto Onuki | a4f1197 | 2015-10-01 13:19:58 -0700 | [diff] [blame] | 472 | public static void dumpRestrictions(PrintWriter pw, String prefix, Bundle restrictions) { |
| 473 | boolean noneSet = true; |
| 474 | if (restrictions != null) { |
| 475 | for (String key : restrictions.keySet()) { |
| 476 | if (restrictions.getBoolean(key, false)) { |
| 477 | pw.println(prefix + key); |
| 478 | noneSet = false; |
| 479 | } |
| 480 | } |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 481 | if (noneSet) { |
| 482 | pw.println(prefix + "none"); |
| 483 | } |
| 484 | } else { |
| 485 | pw.println(prefix + "null"); |
Makoto Onuki | a4f1197 | 2015-10-01 13:19:58 -0700 | [diff] [blame] | 486 | } |
| 487 | } |
| 488 | } |