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