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