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