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; |
yuemingw | 1d13eae | 2018-01-30 17:27:54 +0000 | [diff] [blame] | 26 | import android.app.admin.DevicePolicyManager; |
Makoto Onuki | 4f16073 | 2015-10-27 17:15:38 -0700 | [diff] [blame] | 27 | import android.content.ContentResolver; |
| 28 | import android.content.Context; |
yuemingw | 265c692 | 2018-01-11 18:31:14 +0000 | [diff] [blame] | 29 | import android.content.Intent; |
Makoto Onuki | 4f16073 | 2015-10-27 17:15:38 -0700 | [diff] [blame] | 30 | import android.os.Binder; |
Makoto Onuki | a4f1197 | 2015-10-01 13:19:58 -0700 | [diff] [blame] | 31 | import android.os.Bundle; |
yuemingw | 1d13eae | 2018-01-30 17:27:54 +0000 | [diff] [blame] | 32 | import android.os.Process; |
Fyodor Kupolov | 9cbfc9e | 2015-10-07 15:52:33 -0700 | [diff] [blame] | 33 | import android.os.RemoteException; |
Makoto Onuki | 4f16073 | 2015-10-27 17:15:38 -0700 | [diff] [blame] | 34 | import android.os.UserHandle; |
Makoto Onuki | a4f1197 | 2015-10-01 13:19:58 -0700 | [diff] [blame] | 35 | import android.os.UserManager; |
Pavel Grafov | 6a40f09 | 2016-10-25 15:46:51 +0100 | [diff] [blame] | 36 | import android.os.UserManagerInternal; |
yuemingw | 265c692 | 2018-01-11 18:31:14 +0000 | [diff] [blame] | 37 | import android.provider.Settings; |
Makoto Onuki | acc5046 | 2018-02-14 14:13:49 -0800 | [diff] [blame] | 38 | import android.provider.Settings.Global; |
Mahaver Chopra | dea471e | 2015-12-17 11:02:37 +0000 | [diff] [blame] | 39 | import android.telephony.SubscriptionInfo; |
| 40 | import android.telephony.SubscriptionManager; |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 41 | import android.util.Log; |
Makoto Onuki | 1f1ceef | 2016-01-28 11:32:32 -0800 | [diff] [blame] | 42 | import android.util.Slog; |
Pavel Grafov | 6a40f09 | 2016-10-25 15:46:51 +0100 | [diff] [blame] | 43 | import android.util.SparseArray; |
Makoto Onuki | a4f1197 | 2015-10-01 13:19:58 -0700 | [diff] [blame] | 44 | |
| 45 | import org.xmlpull.v1.XmlPullParser; |
| 46 | import org.xmlpull.v1.XmlSerializer; |
| 47 | |
| 48 | import java.io.IOException; |
| 49 | import java.io.PrintWriter; |
Mahaver Chopra | dea471e | 2015-12-17 11:02:37 +0000 | [diff] [blame] | 50 | import java.util.List; |
Makoto Onuki | a4f1197 | 2015-10-01 13:19:58 -0700 | [diff] [blame] | 51 | import java.util.Set; |
| 52 | |
Makoto Onuki | d45a4a2 | 2015-11-02 17:17:38 -0800 | [diff] [blame] | 53 | /** |
Mahaver Chopra | dea471e | 2015-12-17 11:02:37 +0000 | [diff] [blame] | 54 | * Utility methods for user restrictions. |
Makoto Onuki | d45a4a2 | 2015-11-02 17:17:38 -0800 | [diff] [blame] | 55 | * |
| 56 | * <p>See {@link UserManagerService} for the method suffixes. |
| 57 | */ |
Makoto Onuki | a4f1197 | 2015-10-01 13:19:58 -0700 | [diff] [blame] | 58 | public class UserRestrictionsUtils { |
Makoto Onuki | 4f16073 | 2015-10-27 17:15:38 -0700 | [diff] [blame] | 59 | private static final String TAG = "UserRestrictionsUtils"; |
| 60 | |
Makoto Onuki | a4f1197 | 2015-10-01 13:19:58 -0700 | [diff] [blame] | 61 | private UserRestrictionsUtils() { |
| 62 | } |
| 63 | |
Makoto Onuki | 1f1ceef | 2016-01-28 11:32:32 -0800 | [diff] [blame] | 64 | private static Set<String> newSetWithUniqueCheck(String[] strings) { |
| 65 | final Set<String> ret = Sets.newArraySet(strings); |
| 66 | |
| 67 | // Make sure there's no overlap. |
| 68 | Preconditions.checkState(ret.size() == strings.length); |
| 69 | return ret; |
| 70 | } |
| 71 | |
| 72 | public static final Set<String> USER_RESTRICTIONS = newSetWithUniqueCheck(new String[] { |
Makoto Onuki | a4f1197 | 2015-10-01 13:19:58 -0700 | [diff] [blame] | 73 | UserManager.DISALLOW_CONFIG_WIFI, |
Christine Franks | 1bade5d | 2017-10-10 15:41:50 -0700 | [diff] [blame] | 74 | UserManager.DISALLOW_CONFIG_LOCALE, |
Makoto Onuki | a4f1197 | 2015-10-01 13:19:58 -0700 | [diff] [blame] | 75 | UserManager.DISALLOW_MODIFY_ACCOUNTS, |
| 76 | UserManager.DISALLOW_INSTALL_APPS, |
| 77 | UserManager.DISALLOW_UNINSTALL_APPS, |
| 78 | UserManager.DISALLOW_SHARE_LOCATION, |
| 79 | UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES, |
| 80 | UserManager.DISALLOW_CONFIG_BLUETOOTH, |
Lenka Trochtova | 63d5e4a7 | 2016-12-02 12:19:39 +0100 | [diff] [blame] | 81 | UserManager.DISALLOW_BLUETOOTH, |
Pavel Grafov | 7f4ad75 | 2017-03-28 13:44:04 +0100 | [diff] [blame] | 82 | UserManager.DISALLOW_BLUETOOTH_SHARING, |
Makoto Onuki | a4f1197 | 2015-10-01 13:19:58 -0700 | [diff] [blame] | 83 | UserManager.DISALLOW_USB_FILE_TRANSFER, |
| 84 | UserManager.DISALLOW_CONFIG_CREDENTIALS, |
| 85 | UserManager.DISALLOW_REMOVE_USER, |
Esteban Talavera | 6c9116a | 2016-11-24 16:12:44 +0000 | [diff] [blame] | 86 | UserManager.DISALLOW_REMOVE_MANAGED_PROFILE, |
Makoto Onuki | a4f1197 | 2015-10-01 13:19:58 -0700 | [diff] [blame] | 87 | UserManager.DISALLOW_DEBUGGING_FEATURES, |
| 88 | UserManager.DISALLOW_CONFIG_VPN, |
yuemingw | a9772f36 | 2017-10-23 18:34:35 +0100 | [diff] [blame] | 89 | UserManager.DISALLOW_CONFIG_DATE_TIME, |
Makoto Onuki | a4f1197 | 2015-10-01 13:19:58 -0700 | [diff] [blame] | 90 | UserManager.DISALLOW_CONFIG_TETHERING, |
| 91 | UserManager.DISALLOW_NETWORK_RESET, |
| 92 | UserManager.DISALLOW_FACTORY_RESET, |
| 93 | UserManager.DISALLOW_ADD_USER, |
Esteban Talavera | 6c9116a | 2016-11-24 16:12:44 +0000 | [diff] [blame] | 94 | UserManager.DISALLOW_ADD_MANAGED_PROFILE, |
Makoto Onuki | a4f1197 | 2015-10-01 13:19:58 -0700 | [diff] [blame] | 95 | UserManager.ENSURE_VERIFY_APPS, |
| 96 | UserManager.DISALLOW_CONFIG_CELL_BROADCASTS, |
| 97 | UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS, |
| 98 | UserManager.DISALLOW_APPS_CONTROL, |
| 99 | UserManager.DISALLOW_MOUNT_PHYSICAL_MEDIA, |
| 100 | UserManager.DISALLOW_UNMUTE_MICROPHONE, |
| 101 | UserManager.DISALLOW_ADJUST_VOLUME, |
| 102 | UserManager.DISALLOW_OUTGOING_CALLS, |
| 103 | UserManager.DISALLOW_SMS, |
| 104 | UserManager.DISALLOW_FUN, |
| 105 | UserManager.DISALLOW_CREATE_WINDOWS, |
Charles He | 22ff6f9d | 2017-10-05 21:28:55 +0100 | [diff] [blame] | 106 | UserManager.DISALLOW_SYSTEM_ERROR_DIALOGS, |
Makoto Onuki | a4f1197 | 2015-10-01 13:19:58 -0700 | [diff] [blame] | 107 | UserManager.DISALLOW_CROSS_PROFILE_COPY_PASTE, |
| 108 | UserManager.DISALLOW_OUTGOING_BEAM, |
| 109 | UserManager.DISALLOW_WALLPAPER, |
| 110 | UserManager.DISALLOW_SAFE_BOOT, |
| 111 | UserManager.ALLOW_PARENT_PROFILE_APP_LINKING, |
| 112 | UserManager.DISALLOW_RECORD_AUDIO, |
Fyodor Kupolov | 9cbfc9e | 2015-10-07 15:52:33 -0700 | [diff] [blame] | 113 | UserManager.DISALLOW_CAMERA, |
Mahaver Chopra | dea471e | 2015-12-17 11:02:37 +0000 | [diff] [blame] | 114 | UserManager.DISALLOW_RUN_IN_BACKGROUND, |
Oleksandr Peletskyi | 7f1f1df | 2016-01-18 15:40:21 +0100 | [diff] [blame] | 115 | UserManager.DISALLOW_DATA_ROAMING, |
Oleksandr Peletskyi | f251981 | 2016-01-26 20:16:06 +0100 | [diff] [blame] | 116 | UserManager.DISALLOW_SET_USER_ICON, |
Mahaver Chopra | 3d9805d | 2016-07-07 16:25:05 +0100 | [diff] [blame] | 117 | UserManager.DISALLOW_SET_WALLPAPER, |
Tony Mak | c120511 | 2016-07-22 16:02:59 +0100 | [diff] [blame] | 118 | UserManager.DISALLOW_OEM_UNLOCK, |
Esteban Talavera | 492b472 | 2017-02-13 14:59:45 +0000 | [diff] [blame] | 119 | UserManager.DISALLOW_UNMUTE_DEVICE, |
Felipe Leme | 24d5893 | 2017-03-21 14:13:58 -0700 | [diff] [blame] | 120 | UserManager.DISALLOW_AUTOFILL, |
Pavel Grafov | c4f87e9 | 2017-10-26 16:34:25 +0100 | [diff] [blame] | 121 | UserManager.DISALLOW_USER_SWITCH, |
| 122 | UserManager.DISALLOW_UNIFIED_PASSWORD, |
yuemingw | 7810b8b | 2018-02-01 17:32:25 +0000 | [diff] [blame] | 123 | UserManager.DISALLOW_CONFIG_LOCATION, |
yuemingw | c6ac29d | 2018-01-10 16:54:08 +0000 | [diff] [blame] | 124 | UserManager.DISALLOW_AIRPLANE_MODE, |
Rubin Xu | cc391c2 | 2018-01-02 20:37:35 +0000 | [diff] [blame] | 125 | UserManager.DISALLOW_CONFIG_BRIGHTNESS, |
| 126 | UserManager.DISALLOW_SHARE_INTO_MANAGED_PROFILE, |
yuemingw | 5cda3ae | 2018-01-15 10:26:19 +0000 | [diff] [blame] | 127 | UserManager.DISALLOW_AMBIENT_DISPLAY, |
Vladislav Kuzkokov | 622b9f9 | 2018-01-25 16:33:05 +0100 | [diff] [blame] | 128 | UserManager.DISALLOW_CONFIG_SCREEN_TIMEOUT, |
| 129 | UserManager.DISALLOW_PRINTING |
Makoto Onuki | 1f1ceef | 2016-01-28 11:32:32 -0800 | [diff] [blame] | 130 | }); |
Makoto Onuki | a4f1197 | 2015-10-01 13:19:58 -0700 | [diff] [blame] | 131 | |
| 132 | /** |
| 133 | * Set of user restriction which we don't want to persist. |
| 134 | */ |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 135 | private static final Set<String> NON_PERSIST_USER_RESTRICTIONS = Sets.newArraySet( |
| 136 | UserManager.DISALLOW_RECORD_AUDIO |
| 137 | ); |
Makoto Onuki | a4f1197 | 2015-10-01 13:19:58 -0700 | [diff] [blame] | 138 | |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 139 | /** |
Pavel Grafov | 6a40f09 | 2016-10-25 15:46:51 +0100 | [diff] [blame] | 140 | * User restrictions that cannot be set by profile owners of secondary users. When set by DO |
| 141 | * they will be applied to all users. |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 142 | */ |
Pavel Grafov | 6a40f09 | 2016-10-25 15:46:51 +0100 | [diff] [blame] | 143 | private static final Set<String> PRIMARY_USER_ONLY_RESTRICTIONS = Sets.newArraySet( |
Lenka Trochtova | 63d5e4a7 | 2016-12-02 12:19:39 +0100 | [diff] [blame] | 144 | UserManager.DISALLOW_BLUETOOTH, |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 145 | UserManager.DISALLOW_USB_FILE_TRANSFER, |
| 146 | UserManager.DISALLOW_CONFIG_TETHERING, |
| 147 | UserManager.DISALLOW_NETWORK_RESET, |
| 148 | UserManager.DISALLOW_FACTORY_RESET, |
| 149 | UserManager.DISALLOW_ADD_USER, |
| 150 | UserManager.DISALLOW_CONFIG_CELL_BROADCASTS, |
| 151 | UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS, |
| 152 | UserManager.DISALLOW_MOUNT_PHYSICAL_MEDIA, |
| 153 | UserManager.DISALLOW_SMS, |
| 154 | UserManager.DISALLOW_FUN, |
| 155 | UserManager.DISALLOW_SAFE_BOOT, |
Mahaver Chopra | dea471e | 2015-12-17 11:02:37 +0000 | [diff] [blame] | 156 | UserManager.DISALLOW_CREATE_WINDOWS, |
yuemingw | 5fe75dc | 2017-11-29 15:52:56 +0000 | [diff] [blame] | 157 | UserManager.DISALLOW_DATA_ROAMING, |
| 158 | UserManager.DISALLOW_AIRPLANE_MODE |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 159 | ); |
| 160 | |
| 161 | /** |
Benjamin Franz | ff66fa9 | 2017-08-10 10:39:44 +0100 | [diff] [blame] | 162 | * User restrictions that cannot be set by profile owners. Applied to all users. |
| 163 | */ |
| 164 | private static final Set<String> DEVICE_OWNER_ONLY_RESTRICTIONS = Sets.newArraySet( |
| 165 | UserManager.DISALLOW_USER_SWITCH |
| 166 | ); |
| 167 | |
| 168 | /** |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 169 | * User restrictions that can't be changed by device owner or profile owner. |
| 170 | */ |
| 171 | private static final Set<String> IMMUTABLE_BY_OWNERS = Sets.newArraySet( |
| 172 | UserManager.DISALLOW_RECORD_AUDIO, |
Mahaver Chopra | 3d9805d | 2016-07-07 16:25:05 +0100 | [diff] [blame] | 173 | UserManager.DISALLOW_WALLPAPER, |
| 174 | UserManager.DISALLOW_OEM_UNLOCK |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 175 | ); |
| 176 | |
| 177 | /** |
| 178 | * Special user restrictions that can be applied to a user as well as to all users globally, |
| 179 | * depending on callers. When device owner sets them, they'll be applied to all users. |
| 180 | */ |
| 181 | private static final Set<String> GLOBAL_RESTRICTIONS = Sets.newArraySet( |
| 182 | UserManager.DISALLOW_ADJUST_VOLUME, |
Pavel Grafov | 7f4ad75 | 2017-03-28 13:44:04 +0100 | [diff] [blame] | 183 | UserManager.DISALLOW_BLUETOOTH_SHARING, |
yuemingw | a9772f36 | 2017-10-23 18:34:35 +0100 | [diff] [blame] | 184 | UserManager.DISALLOW_CONFIG_DATE_TIME, |
Charles He | 22ff6f9d | 2017-10-05 21:28:55 +0100 | [diff] [blame] | 185 | UserManager.DISALLOW_SYSTEM_ERROR_DIALOGS, |
Fyodor Kupolov | 9cbfc9e | 2015-10-07 15:52:33 -0700 | [diff] [blame] | 186 | UserManager.DISALLOW_RUN_IN_BACKGROUND, |
Tony Mak | c120511 | 2016-07-22 16:02:59 +0100 | [diff] [blame] | 187 | UserManager.DISALLOW_UNMUTE_MICROPHONE, |
Esteban Talavera | 492b472 | 2017-02-13 14:59:45 +0000 | [diff] [blame] | 188 | UserManager.DISALLOW_UNMUTE_DEVICE |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 189 | ); |
| 190 | |
Makoto Onuki | 1f1ceef | 2016-01-28 11:32:32 -0800 | [diff] [blame] | 191 | /** |
Esteban Talavera | 6c9116a | 2016-11-24 16:12:44 +0000 | [diff] [blame] | 192 | * User restrictions that default to {@code true} for device owners. |
| 193 | */ |
| 194 | private static final Set<String> DEFAULT_ENABLED_FOR_DEVICE_OWNERS = Sets.newArraySet( |
| 195 | UserManager.DISALLOW_ADD_MANAGED_PROFILE |
| 196 | ); |
| 197 | |
Pavel Grafov | 7f4ad75 | 2017-03-28 13:44:04 +0100 | [diff] [blame] | 198 | /** |
| 199 | * User restrictions that default to {@code true} for managed profile owners. |
| 200 | * |
| 201 | * NB: {@link UserManager#DISALLOW_INSTALL_UNKNOWN_SOURCES} is also set by default but it is |
| 202 | * not set to existing profile owners unless they used to have INSTALL_NON_MARKET_APPS disabled |
| 203 | * in settings. So it is handled separately. |
| 204 | */ |
| 205 | private static final Set<String> DEFAULT_ENABLED_FOR_MANAGED_PROFILES = Sets.newArraySet( |
| 206 | UserManager.DISALLOW_BLUETOOTH_SHARING |
| 207 | ); |
| 208 | |
Pavel Grafov | c4f87e9 | 2017-10-26 16:34:25 +0100 | [diff] [blame] | 209 | /** |
Pavel Grafov | 6a40f09 | 2016-10-25 15:46:51 +0100 | [diff] [blame] | 210 | * Special user restrictions that are always applied to all users no matter who sets them. |
| 211 | */ |
| 212 | private static final Set<String> PROFILE_GLOBAL_RESTRICTIONS = Sets.newArraySet( |
yuemingw | 5fe75dc | 2017-11-29 15:52:56 +0000 | [diff] [blame] | 213 | UserManager.ENSURE_VERIFY_APPS, |
| 214 | UserManager.DISALLOW_AIRPLANE_MODE |
Pavel Grafov | 6a40f09 | 2016-10-25 15:46:51 +0100 | [diff] [blame] | 215 | ); |
| 216 | |
Esteban Talavera | 6c9116a | 2016-11-24 16:12:44 +0000 | [diff] [blame] | 217 | /** |
Makoto Onuki | 1f1ceef | 2016-01-28 11:32:32 -0800 | [diff] [blame] | 218 | * Throws {@link IllegalArgumentException} if the given restriction name is invalid. |
| 219 | */ |
| 220 | public static boolean isValidRestriction(@NonNull String restriction) { |
| 221 | if (!USER_RESTRICTIONS.contains(restriction)) { |
Makoto Onuki | ad5619d | 2016-02-10 13:41:15 -0800 | [diff] [blame] | 222 | Slog.e(TAG, "Unknown restriction: " + restriction); |
Makoto Onuki | 1f1ceef | 2016-01-28 11:32:32 -0800 | [diff] [blame] | 223 | return false; |
| 224 | } |
| 225 | return true; |
| 226 | } |
| 227 | |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 228 | public static void writeRestrictions(@NonNull XmlSerializer serializer, |
| 229 | @Nullable Bundle restrictions, @NonNull String tag) throws IOException { |
| 230 | if (restrictions == null) { |
| 231 | return; |
| 232 | } |
| 233 | |
Makoto Onuki | a4f1197 | 2015-10-01 13:19:58 -0700 | [diff] [blame] | 234 | serializer.startTag(null, tag); |
Makoto Onuki | ac65e1e | 2015-11-20 15:33:17 -0800 | [diff] [blame] | 235 | for (String key : restrictions.keySet()) { |
| 236 | if (NON_PERSIST_USER_RESTRICTIONS.contains(key)) { |
| 237 | continue; // Don't persist. |
Makoto Onuki | a4f1197 | 2015-10-01 13:19:58 -0700 | [diff] [blame] | 238 | } |
Makoto Onuki | ac65e1e | 2015-11-20 15:33:17 -0800 | [diff] [blame] | 239 | if (USER_RESTRICTIONS.contains(key)) { |
| 240 | if (restrictions.getBoolean(key)) { |
| 241 | serializer.attribute(null, key, "true"); |
| 242 | } |
| 243 | continue; |
| 244 | } |
| 245 | Log.w(TAG, "Unknown user restriction detected: " + key); |
Makoto Onuki | a4f1197 | 2015-10-01 13:19:58 -0700 | [diff] [blame] | 246 | } |
| 247 | serializer.endTag(null, tag); |
| 248 | } |
| 249 | |
Esteban Talavera | c48b20f | 2016-08-11 11:23:40 +0100 | [diff] [blame] | 250 | public static void readRestrictions(XmlPullParser parser, Bundle restrictions) { |
Fyodor Kupolov | eafee02 | 2017-03-15 17:09:04 -0700 | [diff] [blame] | 251 | restrictions.clear(); |
Makoto Onuki | a4f1197 | 2015-10-01 13:19:58 -0700 | [diff] [blame] | 252 | for (String key : USER_RESTRICTIONS) { |
| 253 | final String value = parser.getAttributeValue(null, key); |
| 254 | if (value != null) { |
| 255 | restrictions.putBoolean(key, Boolean.parseBoolean(value)); |
| 256 | } |
| 257 | } |
| 258 | } |
| 259 | |
Pavel Grafov | 6a40f09 | 2016-10-25 15:46:51 +0100 | [diff] [blame] | 260 | public static Bundle readRestrictions(XmlPullParser parser) { |
| 261 | final Bundle result = new Bundle(); |
| 262 | readRestrictions(parser, result); |
| 263 | return result; |
| 264 | } |
| 265 | |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 266 | /** |
| 267 | * @return {@code in} itself when it's not null, or an empty bundle (which can writable). |
| 268 | */ |
| 269 | public static Bundle nonNull(@Nullable Bundle in) { |
| 270 | return in != null ? in : new Bundle(); |
| 271 | } |
| 272 | |
| 273 | public static boolean isEmpty(@Nullable Bundle in) { |
| 274 | return (in == null) || (in.size() == 0); |
| 275 | } |
| 276 | |
| 277 | /** |
Pavel Grafov | 6a40f09 | 2016-10-25 15:46:51 +0100 | [diff] [blame] | 278 | * Returns {@code true} if given bundle is not null and contains {@code true} for a given |
| 279 | * restriction. |
| 280 | */ |
| 281 | public static boolean contains(@Nullable Bundle in, String restriction) { |
| 282 | return in != null && in.getBoolean(restriction); |
| 283 | } |
| 284 | |
| 285 | /** |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 286 | * Creates a copy of the {@code in} Bundle. If {@code in} is null, it'll return an empty |
| 287 | * bundle. |
| 288 | * |
| 289 | * <p>The resulting {@link Bundle} is always writable. (i.e. it won't return |
| 290 | * {@link Bundle#EMPTY}) |
| 291 | */ |
| 292 | public static @NonNull Bundle clone(@Nullable Bundle in) { |
| 293 | return (in != null) ? new Bundle(in) : new Bundle(); |
| 294 | } |
| 295 | |
| 296 | public static void merge(@NonNull Bundle dest, @Nullable Bundle in) { |
| 297 | Preconditions.checkNotNull(dest); |
| 298 | Preconditions.checkArgument(dest != in); |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 299 | if (in == null) { |
| 300 | return; |
| 301 | } |
| 302 | for (String key : in.keySet()) { |
| 303 | if (in.getBoolean(key, false)) { |
| 304 | dest.putBoolean(key, true); |
| 305 | } |
| 306 | } |
| 307 | } |
| 308 | |
Makoto Onuki | 4f16073 | 2015-10-27 17:15:38 -0700 | [diff] [blame] | 309 | /** |
Pavel Grafov | 6a40f09 | 2016-10-25 15:46:51 +0100 | [diff] [blame] | 310 | * Merges a sparse array of restrictions bundles into one. |
| 311 | */ |
| 312 | @Nullable |
| 313 | public static Bundle mergeAll(SparseArray<Bundle> restrictions) { |
| 314 | if (restrictions.size() == 0) { |
| 315 | return null; |
| 316 | } else { |
| 317 | final Bundle result = new Bundle(); |
| 318 | for (int i = 0; i < restrictions.size(); i++) { |
| 319 | merge(result, restrictions.valueAt(i)); |
| 320 | } |
| 321 | return result; |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | /** |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 326 | * @return true if a restriction is settable by device owner. |
| 327 | */ |
| 328 | public static boolean canDeviceOwnerChange(String restriction) { |
| 329 | return !IMMUTABLE_BY_OWNERS.contains(restriction); |
| 330 | } |
| 331 | |
| 332 | /** |
Makoto Onuki | 5485ed4 | 2015-12-09 11:38:23 -0800 | [diff] [blame] | 333 | * @return true if a restriction is settable by profile owner. Note it takes a user ID because |
| 334 | * 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] | 335 | */ |
Makoto Onuki | 5485ed4 | 2015-12-09 11:38:23 -0800 | [diff] [blame] | 336 | public static boolean canProfileOwnerChange(String restriction, int userId) { |
| 337 | return !IMMUTABLE_BY_OWNERS.contains(restriction) |
Benjamin Franz | ff66fa9 | 2017-08-10 10:39:44 +0100 | [diff] [blame] | 338 | && !DEVICE_OWNER_ONLY_RESTRICTIONS.contains(restriction) |
Makoto Onuki | 5485ed4 | 2015-12-09 11:38:23 -0800 | [diff] [blame] | 339 | && !(userId != UserHandle.USER_SYSTEM |
Pavel Grafov | 6a40f09 | 2016-10-25 15:46:51 +0100 | [diff] [blame] | 340 | && PRIMARY_USER_ONLY_RESTRICTIONS.contains(restriction)); |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | /** |
Esteban Talavera | 6c9116a | 2016-11-24 16:12:44 +0000 | [diff] [blame] | 344 | * Returns the user restrictions that default to {@code true} for device owners. |
Nicolas Prevot | 2ea46fe | 2017-01-05 10:29:34 +0000 | [diff] [blame] | 345 | * 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] | 346 | */ |
| 347 | public static @NonNull Set<String> getDefaultEnabledForDeviceOwner() { |
| 348 | return DEFAULT_ENABLED_FOR_DEVICE_OWNERS; |
| 349 | } |
| 350 | |
| 351 | /** |
Pavel Grafov | 7f4ad75 | 2017-03-28 13:44:04 +0100 | [diff] [blame] | 352 | * Returns the user restrictions that default to {@code true} for managed profile owners. |
| 353 | */ |
| 354 | public static @NonNull Set<String> getDefaultEnabledForManagedProfiles() { |
| 355 | return DEFAULT_ENABLED_FOR_MANAGED_PROFILES; |
| 356 | } |
| 357 | |
| 358 | /** |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 359 | * Takes restrictions that can be set by device owner, and sort them into what should be applied |
| 360 | * globally and what should be applied only on the current user. |
| 361 | */ |
Pavel Grafov | 6a40f09 | 2016-10-25 15:46:51 +0100 | [diff] [blame] | 362 | public static void sortToGlobalAndLocal(@Nullable Bundle in, boolean isDeviceOwner, |
| 363 | int cameraRestrictionScope, |
| 364 | @NonNull Bundle global, @NonNull Bundle local) { |
| 365 | // Camera restriction (as well as all others) goes to at most one bundle. |
| 366 | if (cameraRestrictionScope == UserManagerInternal.CAMERA_DISABLED_GLOBALLY) { |
| 367 | global.putBoolean(UserManager.DISALLOW_CAMERA, true); |
| 368 | } else if (cameraRestrictionScope == UserManagerInternal.CAMERA_DISABLED_LOCALLY) { |
| 369 | local.putBoolean(UserManager.DISALLOW_CAMERA, true); |
| 370 | } |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 371 | if (in == null || in.size() == 0) { |
| 372 | return; |
| 373 | } |
| 374 | for (String key : in.keySet()) { |
| 375 | if (!in.getBoolean(key)) { |
| 376 | continue; |
| 377 | } |
Pavel Grafov | 6a40f09 | 2016-10-25 15:46:51 +0100 | [diff] [blame] | 378 | if (isGlobal(isDeviceOwner, key)) { |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 379 | global.putBoolean(key, true); |
| 380 | } else { |
| 381 | local.putBoolean(key, true); |
| 382 | } |
| 383 | } |
| 384 | } |
| 385 | |
| 386 | /** |
Pavel Grafov | 6a40f09 | 2016-10-25 15:46:51 +0100 | [diff] [blame] | 387 | * Whether given user restriction should be enforced globally. |
| 388 | */ |
| 389 | private static boolean isGlobal(boolean isDeviceOwner, String key) { |
| 390 | return (isDeviceOwner && |
Benjamin Franz | ff66fa9 | 2017-08-10 10:39:44 +0100 | [diff] [blame] | 391 | (PRIMARY_USER_ONLY_RESTRICTIONS.contains(key) || GLOBAL_RESTRICTIONS.contains(key))) |
| 392 | || PROFILE_GLOBAL_RESTRICTIONS.contains(key) |
| 393 | || DEVICE_OWNER_ONLY_RESTRICTIONS.contains(key); |
Pavel Grafov | 6a40f09 | 2016-10-25 15:46:51 +0100 | [diff] [blame] | 394 | } |
| 395 | |
| 396 | /** |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 397 | * @return true if two Bundles contain the same user restriction. |
| 398 | * A null bundle and an empty bundle are considered to be equal. |
| 399 | */ |
| 400 | public static boolean areEqual(@Nullable Bundle a, @Nullable Bundle b) { |
| 401 | if (a == b) { |
| 402 | return true; |
| 403 | } |
| 404 | if (isEmpty(a)) { |
| 405 | return isEmpty(b); |
| 406 | } |
| 407 | if (isEmpty(b)) { |
| 408 | return false; |
| 409 | } |
| 410 | for (String key : a.keySet()) { |
| 411 | if (a.getBoolean(key) != b.getBoolean(key)) { |
| 412 | return false; |
| 413 | } |
| 414 | } |
| 415 | for (String key : b.keySet()) { |
| 416 | if (a.getBoolean(key) != b.getBoolean(key)) { |
| 417 | return false; |
| 418 | } |
| 419 | } |
| 420 | return true; |
| 421 | } |
| 422 | |
| 423 | /** |
Makoto Onuki | 4f16073 | 2015-10-27 17:15:38 -0700 | [diff] [blame] | 424 | * Takes a new use restriction set and the previous set, and apply the restrictions that have |
| 425 | * changed. |
Makoto Onuki | d45a4a2 | 2015-11-02 17:17:38 -0800 | [diff] [blame] | 426 | * |
Fyodor Kupolov | 9cbfc9e | 2015-10-07 15:52:33 -0700 | [diff] [blame] | 427 | * <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] | 428 | */ |
Fyodor Kupolov | 9cbfc9e | 2015-10-07 15:52:33 -0700 | [diff] [blame] | 429 | public static void applyUserRestrictions(Context context, int userId, |
Makoto Onuki | d45a4a2 | 2015-11-02 17:17:38 -0800 | [diff] [blame] | 430 | Bundle newRestrictions, Bundle prevRestrictions) { |
Makoto Onuki | 4f16073 | 2015-10-27 17:15:38 -0700 | [diff] [blame] | 431 | for (String key : USER_RESTRICTIONS) { |
| 432 | final boolean newValue = newRestrictions.getBoolean(key); |
| 433 | final boolean prevValue = prevRestrictions.getBoolean(key); |
| 434 | |
| 435 | if (newValue != prevValue) { |
Fyodor Kupolov | 9cbfc9e | 2015-10-07 15:52:33 -0700 | [diff] [blame] | 436 | applyUserRestriction(context, userId, key, newValue); |
Makoto Onuki | 4f16073 | 2015-10-27 17:15:38 -0700 | [diff] [blame] | 437 | } |
| 438 | } |
| 439 | } |
Fyodor Kupolov | 9cbfc9e | 2015-10-07 15:52:33 -0700 | [diff] [blame] | 440 | |
Makoto Onuki | d45a4a2 | 2015-11-02 17:17:38 -0800 | [diff] [blame] | 441 | /** |
| 442 | * Apply each user restriction. |
| 443 | * |
yuemingw | 1d13eae | 2018-01-30 17:27:54 +0000 | [diff] [blame] | 444 | * <p>See also {@link #isSettingRestrictedForUser()}, |
Makoto Onuki | 28da2e3 | 2015-11-20 11:30:44 -0800 | [diff] [blame] | 445 | * which should be in sync with this method. |
Makoto Onuki | d45a4a2 | 2015-11-02 17:17:38 -0800 | [diff] [blame] | 446 | */ |
Fyodor Kupolov | 9cbfc9e | 2015-10-07 15:52:33 -0700 | [diff] [blame] | 447 | private static void applyUserRestriction(Context context, int userId, String key, |
Makoto Onuki | 4f16073 | 2015-10-27 17:15:38 -0700 | [diff] [blame] | 448 | boolean newValue) { |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 449 | if (UserManagerService.DBG) { |
| 450 | Log.d(TAG, "Applying user restriction: userId=" + userId |
| 451 | + " key=" + key + " value=" + newValue); |
| 452 | } |
Makoto Onuki | 4f16073 | 2015-10-27 17:15:38 -0700 | [diff] [blame] | 453 | // When certain restrictions are cleared, we don't update the system settings, |
| 454 | // because these settings are changeable on the Settings UI and we don't know the original |
| 455 | // value -- for example LOCATION_MODE might have been off already when the restriction was |
| 456 | // set, and in that case even if the restriction is lifted, changing it to ON would be |
| 457 | // wrong. So just don't do anything in such a case. If the user hopes to enable location |
| 458 | // later, they can do it on the Settings UI. |
Benjamin Franz | 0ff13fc | 2016-07-12 13:42:21 +0100 | [diff] [blame] | 459 | // WARNING: Remember that Settings.Global and Settings.Secure are changeable via adb. |
| 460 | // To prevent this from happening for a given user restriction, you have to add a check to |
| 461 | // SettingsProvider.isGlobalOrSecureSettingRestrictedForUser. |
Makoto Onuki | 4f16073 | 2015-10-27 17:15:38 -0700 | [diff] [blame] | 462 | |
| 463 | final ContentResolver cr = context.getContentResolver(); |
| 464 | final long id = Binder.clearCallingIdentity(); |
| 465 | try { |
| 466 | switch (key) { |
Mahaver Chopra | dea471e | 2015-12-17 11:02:37 +0000 | [diff] [blame] | 467 | case UserManager.DISALLOW_DATA_ROAMING: |
| 468 | if (newValue) { |
| 469 | // DISALLOW_DATA_ROAMING user restriction is set. |
| 470 | |
| 471 | // Multi sim device. |
Jeff Sharkey | 717f52f | 2018-01-04 16:04:11 -0700 | [diff] [blame] | 472 | SubscriptionManager subscriptionManager = context |
| 473 | .getSystemService(SubscriptionManager.class); |
Mahaver Chopra | dea471e | 2015-12-17 11:02:37 +0000 | [diff] [blame] | 474 | final List<SubscriptionInfo> subscriptionInfoList = |
| 475 | subscriptionManager.getActiveSubscriptionInfoList(); |
| 476 | if (subscriptionInfoList != null) { |
| 477 | for (SubscriptionInfo subInfo : subscriptionInfoList) { |
| 478 | android.provider.Settings.Global.putStringForUser(cr, |
| 479 | android.provider.Settings.Global.DATA_ROAMING |
| 480 | + subInfo.getSubscriptionId(), "0", userId); |
| 481 | } |
| 482 | } |
| 483 | |
| 484 | // Single sim device. |
| 485 | android.provider.Settings.Global.putStringForUser(cr, |
| 486 | android.provider.Settings.Global.DATA_ROAMING, "0", userId); |
| 487 | } |
| 488 | break; |
Makoto Onuki | 4f16073 | 2015-10-27 17:15:38 -0700 | [diff] [blame] | 489 | case UserManager.DISALLOW_SHARE_LOCATION: |
| 490 | if (newValue) { |
| 491 | android.provider.Settings.Secure.putIntForUser(cr, |
| 492 | android.provider.Settings.Secure.LOCATION_MODE, |
| 493 | android.provider.Settings.Secure.LOCATION_MODE_OFF, |
| 494 | userId); |
Makoto Onuki | 4f16073 | 2015-10-27 17:15:38 -0700 | [diff] [blame] | 495 | } |
Makoto Onuki | 4f16073 | 2015-10-27 17:15:38 -0700 | [diff] [blame] | 496 | break; |
| 497 | case UserManager.DISALLOW_DEBUGGING_FEATURES: |
| 498 | if (newValue) { |
| 499 | // Only disable adb if changing for system user, since it is global |
| 500 | // TODO: should this be admin user? |
| 501 | if (userId == UserHandle.USER_SYSTEM) { |
| 502 | android.provider.Settings.Global.putStringForUser(cr, |
| 503 | android.provider.Settings.Global.ADB_ENABLED, "0", |
| 504 | userId); |
| 505 | } |
| 506 | } |
| 507 | break; |
| 508 | case UserManager.ENSURE_VERIFY_APPS: |
| 509 | if (newValue) { |
| 510 | android.provider.Settings.Global.putStringForUser( |
| 511 | context.getContentResolver(), |
| 512 | android.provider.Settings.Global.PACKAGE_VERIFIER_ENABLE, "1", |
| 513 | userId); |
| 514 | android.provider.Settings.Global.putStringForUser( |
| 515 | context.getContentResolver(), |
| 516 | android.provider.Settings.Global.PACKAGE_VERIFIER_INCLUDE_ADB, "1", |
| 517 | userId); |
| 518 | } |
| 519 | break; |
| 520 | case UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES: |
Suprabh Shukla | e3745ee | 2017-02-02 20:01:11 -0800 | [diff] [blame] | 521 | // Since Android O, the secure setting is not available to be changed by the |
| 522 | // user. Hence, when the restriction is cleared, we need to reset the state of |
| 523 | // the setting to its default value which is now 1. |
| 524 | android.provider.Settings.Secure.putIntForUser(cr, |
| 525 | android.provider.Settings.Secure.INSTALL_NON_MARKET_APPS, |
| 526 | newValue ? 0 : 1, userId); |
Makoto Onuki | 4f16073 | 2015-10-27 17:15:38 -0700 | [diff] [blame] | 527 | break; |
Fyodor Kupolov | 9cbfc9e | 2015-10-07 15:52:33 -0700 | [diff] [blame] | 528 | case UserManager.DISALLOW_RUN_IN_BACKGROUND: |
| 529 | if (newValue) { |
| 530 | int currentUser = ActivityManager.getCurrentUser(); |
| 531 | if (currentUser != userId && userId != UserHandle.USER_SYSTEM) { |
| 532 | try { |
Sudheer Shanka | dc589ac | 2016-11-10 15:30:17 -0800 | [diff] [blame] | 533 | ActivityManager.getService().stopUser(userId, false, null); |
Fyodor Kupolov | 9cbfc9e | 2015-10-07 15:52:33 -0700 | [diff] [blame] | 534 | } catch (RemoteException e) { |
| 535 | throw e.rethrowAsRuntimeException(); |
| 536 | } |
| 537 | } |
| 538 | } |
Lenka Trochtova | 6474f0e | 2016-03-24 16:43:10 +0100 | [diff] [blame] | 539 | break; |
| 540 | case UserManager.DISALLOW_SAFE_BOOT: |
| 541 | // Unlike with the other restrictions, we want to propagate the new value to |
| 542 | // the system settings even if it is false. The other restrictions modify |
| 543 | // settings which could be manually changed by the user from the Settings app |
| 544 | // after the policies enforcing these restrictions have been revoked, so we |
| 545 | // leave re-setting of those settings to the user. |
| 546 | android.provider.Settings.Global.putInt( |
| 547 | context.getContentResolver(), |
| 548 | android.provider.Settings.Global.SAFE_BOOT_DISALLOWED, |
| 549 | newValue ? 1 : 0); |
| 550 | break; |
yuemingw | 265c692 | 2018-01-11 18:31:14 +0000 | [diff] [blame] | 551 | case UserManager.DISALLOW_AIRPLANE_MODE: |
| 552 | if (newValue) { |
| 553 | final boolean airplaneMode = Settings.Global.getInt( |
| 554 | context.getContentResolver(), |
| 555 | Settings.Global.AIRPLANE_MODE_ON, 0) == 1; |
| 556 | if (airplaneMode) { |
| 557 | android.provider.Settings.Global.putInt( |
| 558 | context.getContentResolver(), |
| 559 | android.provider.Settings.Global.AIRPLANE_MODE_ON, 0); |
| 560 | // Post the intent. |
| 561 | Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED); |
yuemingw | f3b9734 | 2018-03-28 15:35:40 +0100 | [diff] [blame] | 562 | intent.putExtra("state", false); |
yuemingw | 265c692 | 2018-01-11 18:31:14 +0000 | [diff] [blame] | 563 | context.sendBroadcastAsUser(intent, UserHandle.ALL); |
| 564 | } |
| 565 | } |
| 566 | break; |
yuemingw | c010951 | 2018-01-21 19:21:27 +0000 | [diff] [blame] | 567 | case UserManager.DISALLOW_AMBIENT_DISPLAY: |
| 568 | if (newValue) { |
Alex Chau | 1e2b731 | 2018-02-28 10:47:48 +0000 | [diff] [blame] | 569 | android.provider.Settings.Secure.putIntForUser( |
yuemingw | c010951 | 2018-01-21 19:21:27 +0000 | [diff] [blame] | 570 | context.getContentResolver(), |
Alex Chau | 1e2b731 | 2018-02-28 10:47:48 +0000 | [diff] [blame] | 571 | Settings.Secure.DOZE_ENABLED, 0, userId); |
| 572 | android.provider.Settings.Secure.putIntForUser( |
yuemingw | c010951 | 2018-01-21 19:21:27 +0000 | [diff] [blame] | 573 | context.getContentResolver(), |
Alex Chau | 1e2b731 | 2018-02-28 10:47:48 +0000 | [diff] [blame] | 574 | Settings.Secure.DOZE_ALWAYS_ON, 0, userId); |
| 575 | android.provider.Settings.Secure.putIntForUser( |
yuemingw | c010951 | 2018-01-21 19:21:27 +0000 | [diff] [blame] | 576 | context.getContentResolver(), |
Lucas Dupin | 4359b55 | 2018-08-09 15:07:54 -0700 | [diff] [blame^] | 577 | Settings.Secure.DOZE_PICK_UP_GESTURE, 0, userId); |
Alex Chau | 1e2b731 | 2018-02-28 10:47:48 +0000 | [diff] [blame] | 578 | android.provider.Settings.Secure.putIntForUser( |
yuemingw | c010951 | 2018-01-21 19:21:27 +0000 | [diff] [blame] | 579 | context.getContentResolver(), |
Alex Chau | 1e2b731 | 2018-02-28 10:47:48 +0000 | [diff] [blame] | 580 | Settings.Secure.DOZE_PULSE_ON_LONG_PRESS, 0, userId); |
| 581 | android.provider.Settings.Secure.putIntForUser( |
yuemingw | c010951 | 2018-01-21 19:21:27 +0000 | [diff] [blame] | 582 | context.getContentResolver(), |
Lucas Dupin | 4359b55 | 2018-08-09 15:07:54 -0700 | [diff] [blame^] | 583 | Settings.Secure.DOZE_DOUBLE_TAP_GESTURE, 0, userId); |
yuemingw | c010951 | 2018-01-21 19:21:27 +0000 | [diff] [blame] | 584 | } |
| 585 | break; |
Makoto Onuki | acc5046 | 2018-02-14 14:13:49 -0800 | [diff] [blame] | 586 | case UserManager.DISALLOW_CONFIG_LOCATION: |
| 587 | // When DISALLOW_CONFIG_LOCATION is set on any user, we undo the global |
| 588 | // kill switch. |
| 589 | if (newValue) { |
| 590 | android.provider.Settings.Global.putString( |
| 591 | context.getContentResolver(), |
| 592 | Global.LOCATION_GLOBAL_KILL_SWITCH, "0"); |
| 593 | } |
| 594 | break; |
Makoto Onuki | 4f16073 | 2015-10-27 17:15:38 -0700 | [diff] [blame] | 595 | } |
Makoto Onuki | 4f16073 | 2015-10-27 17:15:38 -0700 | [diff] [blame] | 596 | } finally { |
| 597 | Binder.restoreCallingIdentity(id); |
| 598 | } |
| 599 | } |
| 600 | |
yuemingw | 1d13eae | 2018-01-30 17:27:54 +0000 | [diff] [blame] | 601 | public static boolean isSettingRestrictedForUser(Context context, @NonNull String setting, |
| 602 | int userId, String value, int callingUid) { |
| 603 | Preconditions.checkNotNull(setting); |
| 604 | final UserManager mUserManager = context.getSystemService(UserManager.class); |
| 605 | String restriction; |
| 606 | boolean checkAllUser = false; |
| 607 | switch (setting) { |
| 608 | case android.provider.Settings.Secure.LOCATION_MODE: |
| 609 | if (mUserManager.hasUserRestriction( |
| 610 | UserManager.DISALLOW_CONFIG_LOCATION, UserHandle.of(userId)) |
| 611 | && callingUid != Process.SYSTEM_UID) { |
| 612 | return true; |
| 613 | } else if (String.valueOf(Settings.Secure.LOCATION_MODE_OFF).equals(value)) { |
| 614 | // Note LOCATION_MODE will be converted into LOCATION_PROVIDERS_ALLOWED |
| 615 | // in android.provider.Settings.Secure.putStringForUser(), so we shouldn't come |
| 616 | // here normally, but we still protect it here from a direct provider write. |
| 617 | return false; |
| 618 | } |
| 619 | restriction = UserManager.DISALLOW_SHARE_LOCATION; |
| 620 | break; |
| 621 | |
| 622 | case android.provider.Settings.Secure.LOCATION_PROVIDERS_ALLOWED: |
| 623 | if (mUserManager.hasUserRestriction( |
| 624 | UserManager.DISALLOW_CONFIG_LOCATION, UserHandle.of(userId)) |
| 625 | && callingUid != Process.SYSTEM_UID) { |
| 626 | return true; |
| 627 | } else if (value != null && value.startsWith("-")) { |
| 628 | // See SettingsProvider.updateLocationProvidersAllowedLocked. "-" is to disable |
| 629 | // a provider, which should be allowed even if the user restriction is set. |
| 630 | return false; |
| 631 | } |
| 632 | restriction = UserManager.DISALLOW_SHARE_LOCATION; |
| 633 | break; |
| 634 | |
| 635 | case android.provider.Settings.Secure.INSTALL_NON_MARKET_APPS: |
| 636 | if ("0".equals(value)) { |
| 637 | return false; |
| 638 | } |
| 639 | restriction = UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES; |
| 640 | break; |
| 641 | |
| 642 | case android.provider.Settings.Global.ADB_ENABLED: |
| 643 | if ("0".equals(value)) { |
| 644 | return false; |
| 645 | } |
| 646 | restriction = UserManager.DISALLOW_DEBUGGING_FEATURES; |
| 647 | break; |
| 648 | |
| 649 | case android.provider.Settings.Global.PACKAGE_VERIFIER_ENABLE: |
| 650 | case android.provider.Settings.Global.PACKAGE_VERIFIER_INCLUDE_ADB: |
| 651 | if ("1".equals(value)) { |
| 652 | return false; |
| 653 | } |
| 654 | restriction = UserManager.ENSURE_VERIFY_APPS; |
| 655 | break; |
| 656 | |
| 657 | case android.provider.Settings.Global.PREFERRED_NETWORK_MODE: |
| 658 | restriction = UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS; |
| 659 | break; |
| 660 | |
| 661 | case android.provider.Settings.Secure.ALWAYS_ON_VPN_APP: |
| 662 | case android.provider.Settings.Secure.ALWAYS_ON_VPN_LOCKDOWN: |
| 663 | // Whitelist system uid (ConnectivityService) and root uid to change always-on vpn |
| 664 | final int appId = UserHandle.getAppId(callingUid); |
| 665 | if (appId == Process.SYSTEM_UID || appId == Process.ROOT_UID) { |
| 666 | return false; |
| 667 | } |
| 668 | restriction = UserManager.DISALLOW_CONFIG_VPN; |
| 669 | break; |
| 670 | |
| 671 | case android.provider.Settings.Global.SAFE_BOOT_DISALLOWED: |
| 672 | if ("1".equals(value)) { |
| 673 | return false; |
| 674 | } |
| 675 | restriction = UserManager.DISALLOW_SAFE_BOOT; |
| 676 | break; |
| 677 | |
| 678 | case android.provider.Settings.Global.AIRPLANE_MODE_ON: |
| 679 | if ("0".equals(value)) { |
| 680 | return false; |
| 681 | } |
| 682 | restriction = UserManager.DISALLOW_AIRPLANE_MODE; |
| 683 | break; |
| 684 | |
| 685 | case android.provider.Settings.Secure.DOZE_ENABLED: |
| 686 | case android.provider.Settings.Secure.DOZE_ALWAYS_ON: |
Lucas Dupin | 4359b55 | 2018-08-09 15:07:54 -0700 | [diff] [blame^] | 687 | case android.provider.Settings.Secure.DOZE_PICK_UP_GESTURE: |
yuemingw | 1d13eae | 2018-01-30 17:27:54 +0000 | [diff] [blame] | 688 | case android.provider.Settings.Secure.DOZE_PULSE_ON_LONG_PRESS: |
Lucas Dupin | 4359b55 | 2018-08-09 15:07:54 -0700 | [diff] [blame^] | 689 | case android.provider.Settings.Secure.DOZE_DOUBLE_TAP_GESTURE: |
yuemingw | 1d13eae | 2018-01-30 17:27:54 +0000 | [diff] [blame] | 690 | if ("0".equals(value)) { |
| 691 | return false; |
| 692 | } |
| 693 | restriction = UserManager.DISALLOW_AMBIENT_DISPLAY; |
| 694 | break; |
| 695 | |
| 696 | case android.provider.Settings.Global.LOCATION_GLOBAL_KILL_SWITCH: |
| 697 | if ("0".equals(value)) { |
| 698 | return false; |
| 699 | } |
| 700 | restriction = UserManager.DISALLOW_CONFIG_LOCATION; |
| 701 | checkAllUser = true; |
| 702 | break; |
| 703 | |
| 704 | case android.provider.Settings.System.SCREEN_BRIGHTNESS: |
| 705 | case android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE: |
| 706 | if (callingUid == Process.SYSTEM_UID) { |
| 707 | return false; |
| 708 | } |
| 709 | restriction = UserManager.DISALLOW_CONFIG_BRIGHTNESS; |
| 710 | break; |
| 711 | |
| 712 | case android.provider.Settings.Global.AUTO_TIME: |
| 713 | DevicePolicyManager dpm = context.getSystemService(DevicePolicyManager.class); |
| 714 | if (dpm != null && dpm.getAutoTimeRequired() |
| 715 | && "0".equals(value)) { |
| 716 | return true; |
| 717 | } else if (callingUid == Process.SYSTEM_UID) { |
| 718 | return false; |
| 719 | } |
| 720 | restriction = UserManager.DISALLOW_CONFIG_DATE_TIME; |
| 721 | break; |
| 722 | |
| 723 | case android.provider.Settings.Global.AUTO_TIME_ZONE: |
| 724 | if (callingUid == Process.SYSTEM_UID) { |
| 725 | return false; |
| 726 | } |
| 727 | restriction = UserManager.DISALLOW_CONFIG_DATE_TIME; |
| 728 | break; |
| 729 | |
| 730 | case android.provider.Settings.System.SCREEN_OFF_TIMEOUT: |
| 731 | if (callingUid == Process.SYSTEM_UID) { |
| 732 | return false; |
| 733 | } |
| 734 | restriction = UserManager.DISALLOW_CONFIG_SCREEN_TIMEOUT; |
| 735 | break; |
| 736 | |
| 737 | default: |
| 738 | if (setting.startsWith(Settings.Global.DATA_ROAMING)) { |
| 739 | if ("0".equals(value)) { |
| 740 | return false; |
| 741 | } |
| 742 | restriction = UserManager.DISALLOW_DATA_ROAMING; |
| 743 | break; |
| 744 | } |
| 745 | return false; |
| 746 | } |
| 747 | |
| 748 | if (checkAllUser) { |
| 749 | return mUserManager.hasUserRestrictionOnAnyUser(restriction); |
| 750 | } else { |
| 751 | return mUserManager.hasUserRestriction(restriction, UserHandle.of(userId)); |
| 752 | } |
| 753 | } |
| 754 | |
Makoto Onuki | a4f1197 | 2015-10-01 13:19:58 -0700 | [diff] [blame] | 755 | public static void dumpRestrictions(PrintWriter pw, String prefix, Bundle restrictions) { |
| 756 | boolean noneSet = true; |
| 757 | if (restrictions != null) { |
| 758 | for (String key : restrictions.keySet()) { |
| 759 | if (restrictions.getBoolean(key, false)) { |
| 760 | pw.println(prefix + key); |
| 761 | noneSet = false; |
| 762 | } |
| 763 | } |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 764 | if (noneSet) { |
| 765 | pw.println(prefix + "none"); |
| 766 | } |
| 767 | } else { |
| 768 | pw.println(prefix + "null"); |
Makoto Onuki | a4f1197 | 2015-10-01 13:19:58 -0700 | [diff] [blame] | 769 | } |
| 770 | } |
Pavel Grafov | 6a40f09 | 2016-10-25 15:46:51 +0100 | [diff] [blame] | 771 | |
| 772 | /** |
| 773 | * Moves a particular restriction from one array of bundles to another, e.g. for all users. |
| 774 | */ |
| 775 | public static void moveRestriction(String restrictionKey, SparseArray<Bundle> srcRestrictions, |
| 776 | SparseArray<Bundle> destRestrictions) { |
| 777 | for (int i = 0; i < srcRestrictions.size(); i++) { |
Pavel Grafov | 7f4ad75 | 2017-03-28 13:44:04 +0100 | [diff] [blame] | 778 | final int key = srcRestrictions.keyAt(i); |
| 779 | final Bundle from = srcRestrictions.valueAt(i); |
Pavel Grafov | 6a40f09 | 2016-10-25 15:46:51 +0100 | [diff] [blame] | 780 | if (contains(from, restrictionKey)) { |
| 781 | from.remove(restrictionKey); |
| 782 | Bundle to = destRestrictions.get(key); |
| 783 | if (to == null) { |
| 784 | to = new Bundle(); |
| 785 | destRestrictions.append(key, to); |
| 786 | } |
| 787 | to.putBoolean(restrictionKey, true); |
| 788 | // Don't keep empty bundles. |
| 789 | if (from.isEmpty()) { |
| 790 | srcRestrictions.removeAt(i); |
| 791 | i--; |
| 792 | } |
| 793 | } |
| 794 | } |
| 795 | } |
Pavel Grafov | 7f4ad75 | 2017-03-28 13:44:04 +0100 | [diff] [blame] | 796 | |
| 797 | /** |
| 798 | * Returns whether restrictions differ between two bundles. |
| 799 | * @param oldRestrictions old bundle of restrictions. |
| 800 | * @param newRestrictions new bundle of restrictions |
| 801 | * @param restrictions restrictions of interest, if empty, all restrictions are checked. |
| 802 | */ |
| 803 | public static boolean restrictionsChanged(Bundle oldRestrictions, Bundle newRestrictions, |
| 804 | String... restrictions) { |
| 805 | if (restrictions.length == 0) { |
| 806 | return areEqual(oldRestrictions, newRestrictions); |
| 807 | } |
| 808 | for (final String restriction : restrictions) { |
| 809 | if (oldRestrictions.getBoolean(restriction, false) != |
| 810 | newRestrictions.getBoolean(restriction, false)) { |
| 811 | return true; |
| 812 | } |
| 813 | } |
| 814 | return false; |
| 815 | } |
Makoto Onuki | a4f1197 | 2015-10-01 13:19:58 -0700 | [diff] [blame] | 816 | } |