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