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