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