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