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