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; |
| 26 | import android.app.ActivityManagerNative; |
Makoto Onuki | 4f16073 | 2015-10-27 17:15:38 -0700 | [diff] [blame] | 27 | import android.content.ContentResolver; |
| 28 | import android.content.Context; |
Makoto Onuki | 4f16073 | 2015-10-27 17:15:38 -0700 | [diff] [blame] | 29 | import android.net.Uri; |
| 30 | import android.os.Binder; |
Makoto Onuki | a4f1197 | 2015-10-01 13:19:58 -0700 | [diff] [blame] | 31 | import android.os.Bundle; |
Fyodor Kupolov | 9cbfc9e | 2015-10-07 15:52:33 -0700 | [diff] [blame] | 32 | import android.os.RemoteException; |
Makoto Onuki | 4f16073 | 2015-10-27 17:15:38 -0700 | [diff] [blame] | 33 | import android.os.SystemProperties; |
| 34 | import android.os.UserHandle; |
Makoto Onuki | a4f1197 | 2015-10-01 13:19:58 -0700 | [diff] [blame] | 35 | import android.os.UserManager; |
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; |
Makoto Onuki | a4f1197 | 2015-10-01 13:19:58 -0700 | [diff] [blame] | 40 | |
| 41 | import org.xmlpull.v1.XmlPullParser; |
| 42 | import org.xmlpull.v1.XmlSerializer; |
| 43 | |
| 44 | import java.io.IOException; |
| 45 | import java.io.PrintWriter; |
Mahaver Chopra | dea471e | 2015-12-17 11:02:37 +0000 | [diff] [blame] | 46 | import java.util.List; |
Makoto Onuki | a4f1197 | 2015-10-01 13:19:58 -0700 | [diff] [blame] | 47 | import java.util.Set; |
| 48 | |
Makoto Onuki | d45a4a2 | 2015-11-02 17:17:38 -0800 | [diff] [blame] | 49 | /** |
Mahaver Chopra | dea471e | 2015-12-17 11:02:37 +0000 | [diff] [blame] | 50 | * Utility methods for user restrictions. |
Makoto Onuki | d45a4a2 | 2015-11-02 17:17:38 -0800 | [diff] [blame] | 51 | * |
| 52 | * <p>See {@link UserManagerService} for the method suffixes. |
| 53 | */ |
Makoto Onuki | a4f1197 | 2015-10-01 13:19:58 -0700 | [diff] [blame] | 54 | public class UserRestrictionsUtils { |
Makoto Onuki | 4f16073 | 2015-10-27 17:15:38 -0700 | [diff] [blame] | 55 | private static final String TAG = "UserRestrictionsUtils"; |
| 56 | |
Makoto Onuki | a4f1197 | 2015-10-01 13:19:58 -0700 | [diff] [blame] | 57 | private UserRestrictionsUtils() { |
| 58 | } |
| 59 | |
Makoto Onuki | 1f1ceef | 2016-01-28 11:32:32 -0800 | [diff] [blame] | 60 | private static Set<String> newSetWithUniqueCheck(String[] strings) { |
| 61 | final Set<String> ret = Sets.newArraySet(strings); |
| 62 | |
| 63 | // Make sure there's no overlap. |
| 64 | Preconditions.checkState(ret.size() == strings.length); |
| 65 | return ret; |
| 66 | } |
| 67 | |
| 68 | public static final Set<String> USER_RESTRICTIONS = newSetWithUniqueCheck(new String[] { |
Makoto Onuki | a4f1197 | 2015-10-01 13:19:58 -0700 | [diff] [blame] | 69 | UserManager.DISALLOW_CONFIG_WIFI, |
| 70 | UserManager.DISALLOW_MODIFY_ACCOUNTS, |
| 71 | UserManager.DISALLOW_INSTALL_APPS, |
| 72 | UserManager.DISALLOW_UNINSTALL_APPS, |
| 73 | UserManager.DISALLOW_SHARE_LOCATION, |
| 74 | UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES, |
| 75 | UserManager.DISALLOW_CONFIG_BLUETOOTH, |
| 76 | UserManager.DISALLOW_USB_FILE_TRANSFER, |
| 77 | UserManager.DISALLOW_CONFIG_CREDENTIALS, |
| 78 | UserManager.DISALLOW_REMOVE_USER, |
| 79 | UserManager.DISALLOW_DEBUGGING_FEATURES, |
| 80 | UserManager.DISALLOW_CONFIG_VPN, |
| 81 | UserManager.DISALLOW_CONFIG_TETHERING, |
| 82 | UserManager.DISALLOW_NETWORK_RESET, |
| 83 | UserManager.DISALLOW_FACTORY_RESET, |
| 84 | UserManager.DISALLOW_ADD_USER, |
| 85 | UserManager.ENSURE_VERIFY_APPS, |
| 86 | UserManager.DISALLOW_CONFIG_CELL_BROADCASTS, |
| 87 | UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS, |
| 88 | UserManager.DISALLOW_APPS_CONTROL, |
| 89 | UserManager.DISALLOW_MOUNT_PHYSICAL_MEDIA, |
| 90 | UserManager.DISALLOW_UNMUTE_MICROPHONE, |
| 91 | UserManager.DISALLOW_ADJUST_VOLUME, |
| 92 | UserManager.DISALLOW_OUTGOING_CALLS, |
| 93 | UserManager.DISALLOW_SMS, |
| 94 | UserManager.DISALLOW_FUN, |
| 95 | UserManager.DISALLOW_CREATE_WINDOWS, |
| 96 | UserManager.DISALLOW_CROSS_PROFILE_COPY_PASTE, |
| 97 | UserManager.DISALLOW_OUTGOING_BEAM, |
| 98 | UserManager.DISALLOW_WALLPAPER, |
| 99 | UserManager.DISALLOW_SAFE_BOOT, |
| 100 | UserManager.ALLOW_PARENT_PROFILE_APP_LINKING, |
| 101 | UserManager.DISALLOW_RECORD_AUDIO, |
Fyodor Kupolov | 9cbfc9e | 2015-10-07 15:52:33 -0700 | [diff] [blame] | 102 | UserManager.DISALLOW_CAMERA, |
Mahaver Chopra | dea471e | 2015-12-17 11:02:37 +0000 | [diff] [blame] | 103 | UserManager.DISALLOW_RUN_IN_BACKGROUND, |
Oleksandr Peletskyi | 7f1f1df | 2016-01-18 15:40:21 +0100 | [diff] [blame] | 104 | UserManager.DISALLOW_DATA_ROAMING, |
Oleksandr Peletskyi | f251981 | 2016-01-26 20:16:06 +0100 | [diff] [blame] | 105 | UserManager.DISALLOW_SET_USER_ICON, |
| 106 | UserManager.DISALLOW_SET_WALLPAPER |
Makoto Onuki | 1f1ceef | 2016-01-28 11:32:32 -0800 | [diff] [blame] | 107 | }); |
Makoto Onuki | a4f1197 | 2015-10-01 13:19:58 -0700 | [diff] [blame] | 108 | |
| 109 | /** |
| 110 | * Set of user restriction which we don't want to persist. |
| 111 | */ |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 112 | private static final Set<String> NON_PERSIST_USER_RESTRICTIONS = Sets.newArraySet( |
| 113 | UserManager.DISALLOW_RECORD_AUDIO |
| 114 | ); |
Makoto Onuki | a4f1197 | 2015-10-01 13:19:58 -0700 | [diff] [blame] | 115 | |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 116 | /** |
| 117 | * User restrictions that can not be set by profile owners. |
| 118 | */ |
| 119 | private static final Set<String> DEVICE_OWNER_ONLY_RESTRICTIONS = Sets.newArraySet( |
| 120 | UserManager.DISALLOW_USB_FILE_TRANSFER, |
| 121 | UserManager.DISALLOW_CONFIG_TETHERING, |
| 122 | UserManager.DISALLOW_NETWORK_RESET, |
| 123 | UserManager.DISALLOW_FACTORY_RESET, |
| 124 | UserManager.DISALLOW_ADD_USER, |
| 125 | UserManager.DISALLOW_CONFIG_CELL_BROADCASTS, |
| 126 | UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS, |
| 127 | UserManager.DISALLOW_MOUNT_PHYSICAL_MEDIA, |
| 128 | UserManager.DISALLOW_SMS, |
| 129 | UserManager.DISALLOW_FUN, |
| 130 | UserManager.DISALLOW_SAFE_BOOT, |
Mahaver Chopra | dea471e | 2015-12-17 11:02:37 +0000 | [diff] [blame] | 131 | UserManager.DISALLOW_CREATE_WINDOWS, |
| 132 | UserManager.DISALLOW_DATA_ROAMING |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 133 | ); |
| 134 | |
| 135 | /** |
| 136 | * User restrictions that can't be changed by device owner or profile owner. |
| 137 | */ |
| 138 | private static final Set<String> IMMUTABLE_BY_OWNERS = Sets.newArraySet( |
| 139 | UserManager.DISALLOW_RECORD_AUDIO, |
| 140 | UserManager.DISALLOW_WALLPAPER |
| 141 | ); |
| 142 | |
| 143 | /** |
| 144 | * Special user restrictions that can be applied to a user as well as to all users globally, |
| 145 | * depending on callers. When device owner sets them, they'll be applied to all users. |
| 146 | */ |
| 147 | private static final Set<String> GLOBAL_RESTRICTIONS = Sets.newArraySet( |
| 148 | UserManager.DISALLOW_ADJUST_VOLUME, |
Fyodor Kupolov | 9cbfc9e | 2015-10-07 15:52:33 -0700 | [diff] [blame] | 149 | UserManager.DISALLOW_RUN_IN_BACKGROUND, |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 150 | UserManager.DISALLOW_UNMUTE_MICROPHONE |
| 151 | ); |
| 152 | |
Makoto Onuki | 1f1ceef | 2016-01-28 11:32:32 -0800 | [diff] [blame] | 153 | /** |
| 154 | * Throws {@link IllegalArgumentException} if the given restriction name is invalid. |
| 155 | */ |
| 156 | public static boolean isValidRestriction(@NonNull String restriction) { |
| 157 | if (!USER_RESTRICTIONS.contains(restriction)) { |
Makoto Onuki | ad5619d | 2016-02-10 13:41:15 -0800 | [diff] [blame] | 158 | Slog.e(TAG, "Unknown restriction: " + restriction); |
Makoto Onuki | 1f1ceef | 2016-01-28 11:32:32 -0800 | [diff] [blame] | 159 | return false; |
| 160 | } |
| 161 | return true; |
| 162 | } |
| 163 | |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 164 | public static void writeRestrictions(@NonNull XmlSerializer serializer, |
| 165 | @Nullable Bundle restrictions, @NonNull String tag) throws IOException { |
| 166 | if (restrictions == null) { |
| 167 | return; |
| 168 | } |
| 169 | |
Makoto Onuki | a4f1197 | 2015-10-01 13:19:58 -0700 | [diff] [blame] | 170 | serializer.startTag(null, tag); |
Makoto Onuki | ac65e1e | 2015-11-20 15:33:17 -0800 | [diff] [blame] | 171 | for (String key : restrictions.keySet()) { |
| 172 | if (NON_PERSIST_USER_RESTRICTIONS.contains(key)) { |
| 173 | continue; // Don't persist. |
Makoto Onuki | a4f1197 | 2015-10-01 13:19:58 -0700 | [diff] [blame] | 174 | } |
Makoto Onuki | ac65e1e | 2015-11-20 15:33:17 -0800 | [diff] [blame] | 175 | if (USER_RESTRICTIONS.contains(key)) { |
| 176 | if (restrictions.getBoolean(key)) { |
| 177 | serializer.attribute(null, key, "true"); |
| 178 | } |
| 179 | continue; |
| 180 | } |
| 181 | Log.w(TAG, "Unknown user restriction detected: " + key); |
Makoto Onuki | a4f1197 | 2015-10-01 13:19:58 -0700 | [diff] [blame] | 182 | } |
| 183 | serializer.endTag(null, tag); |
| 184 | } |
| 185 | |
| 186 | public static void readRestrictions(XmlPullParser parser, Bundle restrictions) |
| 187 | throws IOException { |
| 188 | for (String key : USER_RESTRICTIONS) { |
| 189 | final String value = parser.getAttributeValue(null, key); |
| 190 | if (value != null) { |
| 191 | restrictions.putBoolean(key, Boolean.parseBoolean(value)); |
| 192 | } |
| 193 | } |
| 194 | } |
| 195 | |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 196 | /** |
| 197 | * @return {@code in} itself when it's not null, or an empty bundle (which can writable). |
| 198 | */ |
| 199 | public static Bundle nonNull(@Nullable Bundle in) { |
| 200 | return in != null ? in : new Bundle(); |
| 201 | } |
| 202 | |
| 203 | public static boolean isEmpty(@Nullable Bundle in) { |
| 204 | return (in == null) || (in.size() == 0); |
| 205 | } |
| 206 | |
| 207 | /** |
| 208 | * Creates a copy of the {@code in} Bundle. If {@code in} is null, it'll return an empty |
| 209 | * bundle. |
| 210 | * |
| 211 | * <p>The resulting {@link Bundle} is always writable. (i.e. it won't return |
| 212 | * {@link Bundle#EMPTY}) |
| 213 | */ |
| 214 | public static @NonNull Bundle clone(@Nullable Bundle in) { |
| 215 | return (in != null) ? new Bundle(in) : new Bundle(); |
| 216 | } |
| 217 | |
| 218 | public static void merge(@NonNull Bundle dest, @Nullable Bundle in) { |
| 219 | Preconditions.checkNotNull(dest); |
| 220 | Preconditions.checkArgument(dest != in); |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 221 | if (in == null) { |
| 222 | return; |
| 223 | } |
| 224 | for (String key : in.keySet()) { |
| 225 | if (in.getBoolean(key, false)) { |
| 226 | dest.putBoolean(key, true); |
| 227 | } |
| 228 | } |
| 229 | } |
| 230 | |
Makoto Onuki | 4f16073 | 2015-10-27 17:15:38 -0700 | [diff] [blame] | 231 | /** |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 232 | * @return true if a restriction is settable by device owner. |
| 233 | */ |
| 234 | public static boolean canDeviceOwnerChange(String restriction) { |
| 235 | return !IMMUTABLE_BY_OWNERS.contains(restriction); |
| 236 | } |
| 237 | |
| 238 | /** |
Makoto Onuki | 5485ed4 | 2015-12-09 11:38:23 -0800 | [diff] [blame] | 239 | * @return true if a restriction is settable by profile owner. Note it takes a user ID because |
| 240 | * 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] | 241 | */ |
Makoto Onuki | 5485ed4 | 2015-12-09 11:38:23 -0800 | [diff] [blame] | 242 | public static boolean canProfileOwnerChange(String restriction, int userId) { |
| 243 | return !IMMUTABLE_BY_OWNERS.contains(restriction) |
| 244 | && !(userId != UserHandle.USER_SYSTEM |
| 245 | && DEVICE_OWNER_ONLY_RESTRICTIONS.contains(restriction)); |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | /** |
| 249 | * Takes restrictions that can be set by device owner, and sort them into what should be applied |
| 250 | * globally and what should be applied only on the current user. |
| 251 | */ |
| 252 | public static void sortToGlobalAndLocal(@Nullable Bundle in, @NonNull Bundle global, |
| 253 | @NonNull Bundle local) { |
| 254 | if (in == null || in.size() == 0) { |
| 255 | return; |
| 256 | } |
| 257 | for (String key : in.keySet()) { |
| 258 | if (!in.getBoolean(key)) { |
| 259 | continue; |
| 260 | } |
| 261 | if (DEVICE_OWNER_ONLY_RESTRICTIONS.contains(key) || GLOBAL_RESTRICTIONS.contains(key)) { |
| 262 | global.putBoolean(key, true); |
| 263 | } else { |
| 264 | local.putBoolean(key, true); |
| 265 | } |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | /** |
| 270 | * @return true if two Bundles contain the same user restriction. |
| 271 | * A null bundle and an empty bundle are considered to be equal. |
| 272 | */ |
| 273 | public static boolean areEqual(@Nullable Bundle a, @Nullable Bundle b) { |
| 274 | if (a == b) { |
| 275 | return true; |
| 276 | } |
| 277 | if (isEmpty(a)) { |
| 278 | return isEmpty(b); |
| 279 | } |
| 280 | if (isEmpty(b)) { |
| 281 | return false; |
| 282 | } |
| 283 | for (String key : a.keySet()) { |
| 284 | if (a.getBoolean(key) != b.getBoolean(key)) { |
| 285 | return false; |
| 286 | } |
| 287 | } |
| 288 | for (String key : b.keySet()) { |
| 289 | if (a.getBoolean(key) != b.getBoolean(key)) { |
| 290 | return false; |
| 291 | } |
| 292 | } |
| 293 | return true; |
| 294 | } |
| 295 | |
| 296 | /** |
Makoto Onuki | 4f16073 | 2015-10-27 17:15:38 -0700 | [diff] [blame] | 297 | * Takes a new use restriction set and the previous set, and apply the restrictions that have |
| 298 | * changed. |
Makoto Onuki | d45a4a2 | 2015-11-02 17:17:38 -0800 | [diff] [blame] | 299 | * |
Fyodor Kupolov | 9cbfc9e | 2015-10-07 15:52:33 -0700 | [diff] [blame] | 300 | * <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] | 301 | */ |
Fyodor Kupolov | 9cbfc9e | 2015-10-07 15:52:33 -0700 | [diff] [blame] | 302 | public static void applyUserRestrictions(Context context, int userId, |
Makoto Onuki | d45a4a2 | 2015-11-02 17:17:38 -0800 | [diff] [blame] | 303 | Bundle newRestrictions, Bundle prevRestrictions) { |
Makoto Onuki | 4f16073 | 2015-10-27 17:15:38 -0700 | [diff] [blame] | 304 | for (String key : USER_RESTRICTIONS) { |
| 305 | final boolean newValue = newRestrictions.getBoolean(key); |
| 306 | final boolean prevValue = prevRestrictions.getBoolean(key); |
| 307 | |
| 308 | if (newValue != prevValue) { |
Fyodor Kupolov | 9cbfc9e | 2015-10-07 15:52:33 -0700 | [diff] [blame] | 309 | applyUserRestriction(context, userId, key, newValue); |
Makoto Onuki | 4f16073 | 2015-10-27 17:15:38 -0700 | [diff] [blame] | 310 | } |
| 311 | } |
| 312 | } |
Fyodor Kupolov | 9cbfc9e | 2015-10-07 15:52:33 -0700 | [diff] [blame] | 313 | |
Makoto Onuki | d45a4a2 | 2015-11-02 17:17:38 -0800 | [diff] [blame] | 314 | /** |
| 315 | * Apply each user restriction. |
| 316 | * |
Makoto Onuki | 28da2e3 | 2015-11-20 11:30:44 -0800 | [diff] [blame] | 317 | * <p>See also {@link |
| 318 | * com.android.providers.settings.SettingsProvider#isGlobalOrSecureSettingRestrictedForUser}, |
| 319 | * which should be in sync with this method. |
Makoto Onuki | d45a4a2 | 2015-11-02 17:17:38 -0800 | [diff] [blame] | 320 | */ |
Fyodor Kupolov | 9cbfc9e | 2015-10-07 15:52:33 -0700 | [diff] [blame] | 321 | private static void applyUserRestriction(Context context, int userId, String key, |
Makoto Onuki | 4f16073 | 2015-10-27 17:15:38 -0700 | [diff] [blame] | 322 | boolean newValue) { |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 323 | if (UserManagerService.DBG) { |
| 324 | Log.d(TAG, "Applying user restriction: userId=" + userId |
| 325 | + " key=" + key + " value=" + newValue); |
| 326 | } |
Makoto Onuki | 4f16073 | 2015-10-27 17:15:38 -0700 | [diff] [blame] | 327 | // When certain restrictions are cleared, we don't update the system settings, |
| 328 | // because these settings are changeable on the Settings UI and we don't know the original |
| 329 | // value -- for example LOCATION_MODE might have been off already when the restriction was |
| 330 | // set, and in that case even if the restriction is lifted, changing it to ON would be |
| 331 | // wrong. So just don't do anything in such a case. If the user hopes to enable location |
| 332 | // later, they can do it on the Settings UI. |
Benjamin Franz | 0ff13fc | 2016-07-12 13:42:21 +0100 | [diff] [blame^] | 333 | // WARNING: Remember that Settings.Global and Settings.Secure are changeable via adb. |
| 334 | // To prevent this from happening for a given user restriction, you have to add a check to |
| 335 | // SettingsProvider.isGlobalOrSecureSettingRestrictedForUser. |
Makoto Onuki | 4f16073 | 2015-10-27 17:15:38 -0700 | [diff] [blame] | 336 | |
| 337 | final ContentResolver cr = context.getContentResolver(); |
| 338 | final long id = Binder.clearCallingIdentity(); |
| 339 | try { |
| 340 | switch (key) { |
Makoto Onuki | 4f16073 | 2015-10-27 17:15:38 -0700 | [diff] [blame] | 341 | case UserManager.DISALLOW_CONFIG_WIFI: |
| 342 | if (newValue) { |
| 343 | android.provider.Settings.Secure.putIntForUser(cr, |
Makoto Onuki | 28da2e3 | 2015-11-20 11:30:44 -0800 | [diff] [blame] | 344 | android.provider.Settings.Global |
Makoto Onuki | 4f16073 | 2015-10-27 17:15:38 -0700 | [diff] [blame] | 345 | .WIFI_NETWORKS_AVAILABLE_NOTIFICATION_ON, 0, userId); |
| 346 | } |
| 347 | break; |
Mahaver Chopra | dea471e | 2015-12-17 11:02:37 +0000 | [diff] [blame] | 348 | case UserManager.DISALLOW_DATA_ROAMING: |
| 349 | if (newValue) { |
| 350 | // DISALLOW_DATA_ROAMING user restriction is set. |
| 351 | |
| 352 | // Multi sim device. |
| 353 | SubscriptionManager subscriptionManager = new SubscriptionManager(context); |
| 354 | final List<SubscriptionInfo> subscriptionInfoList = |
| 355 | subscriptionManager.getActiveSubscriptionInfoList(); |
| 356 | if (subscriptionInfoList != null) { |
| 357 | for (SubscriptionInfo subInfo : subscriptionInfoList) { |
| 358 | android.provider.Settings.Global.putStringForUser(cr, |
| 359 | android.provider.Settings.Global.DATA_ROAMING |
| 360 | + subInfo.getSubscriptionId(), "0", userId); |
| 361 | } |
| 362 | } |
| 363 | |
| 364 | // Single sim device. |
| 365 | android.provider.Settings.Global.putStringForUser(cr, |
| 366 | android.provider.Settings.Global.DATA_ROAMING, "0", userId); |
| 367 | } |
| 368 | break; |
Makoto Onuki | 4f16073 | 2015-10-27 17:15:38 -0700 | [diff] [blame] | 369 | case UserManager.DISALLOW_SHARE_LOCATION: |
| 370 | if (newValue) { |
| 371 | android.provider.Settings.Secure.putIntForUser(cr, |
| 372 | android.provider.Settings.Secure.LOCATION_MODE, |
| 373 | android.provider.Settings.Secure.LOCATION_MODE_OFF, |
| 374 | userId); |
Makoto Onuki | 4f16073 | 2015-10-27 17:15:38 -0700 | [diff] [blame] | 375 | } |
Makoto Onuki | 4f16073 | 2015-10-27 17:15:38 -0700 | [diff] [blame] | 376 | break; |
| 377 | case UserManager.DISALLOW_DEBUGGING_FEATURES: |
| 378 | if (newValue) { |
| 379 | // Only disable adb if changing for system user, since it is global |
| 380 | // TODO: should this be admin user? |
| 381 | if (userId == UserHandle.USER_SYSTEM) { |
| 382 | android.provider.Settings.Global.putStringForUser(cr, |
| 383 | android.provider.Settings.Global.ADB_ENABLED, "0", |
| 384 | userId); |
| 385 | } |
| 386 | } |
| 387 | break; |
| 388 | case UserManager.ENSURE_VERIFY_APPS: |
| 389 | if (newValue) { |
| 390 | android.provider.Settings.Global.putStringForUser( |
| 391 | context.getContentResolver(), |
| 392 | android.provider.Settings.Global.PACKAGE_VERIFIER_ENABLE, "1", |
| 393 | userId); |
| 394 | android.provider.Settings.Global.putStringForUser( |
| 395 | context.getContentResolver(), |
| 396 | android.provider.Settings.Global.PACKAGE_VERIFIER_INCLUDE_ADB, "1", |
| 397 | userId); |
| 398 | } |
| 399 | break; |
| 400 | case UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES: |
| 401 | if (newValue) { |
| 402 | android.provider.Settings.Secure.putIntForUser(cr, |
| 403 | android.provider.Settings.Secure.INSTALL_NON_MARKET_APPS, 0, |
| 404 | userId); |
| 405 | } |
| 406 | break; |
Fyodor Kupolov | 9cbfc9e | 2015-10-07 15:52:33 -0700 | [diff] [blame] | 407 | case UserManager.DISALLOW_RUN_IN_BACKGROUND: |
| 408 | if (newValue) { |
| 409 | int currentUser = ActivityManager.getCurrentUser(); |
| 410 | if (currentUser != userId && userId != UserHandle.USER_SYSTEM) { |
| 411 | try { |
| 412 | ActivityManagerNative.getDefault().stopUser(userId, false, null); |
| 413 | } catch (RemoteException e) { |
| 414 | throw e.rethrowAsRuntimeException(); |
| 415 | } |
| 416 | } |
| 417 | } |
Lenka Trochtova | 6474f0e | 2016-03-24 16:43:10 +0100 | [diff] [blame] | 418 | break; |
| 419 | case UserManager.DISALLOW_SAFE_BOOT: |
| 420 | // Unlike with the other restrictions, we want to propagate the new value to |
| 421 | // the system settings even if it is false. The other restrictions modify |
| 422 | // settings which could be manually changed by the user from the Settings app |
| 423 | // after the policies enforcing these restrictions have been revoked, so we |
| 424 | // leave re-setting of those settings to the user. |
| 425 | android.provider.Settings.Global.putInt( |
| 426 | context.getContentResolver(), |
| 427 | android.provider.Settings.Global.SAFE_BOOT_DISALLOWED, |
| 428 | newValue ? 1 : 0); |
| 429 | break; |
Makoto Onuki | 4f16073 | 2015-10-27 17:15:38 -0700 | [diff] [blame] | 430 | } |
Makoto Onuki | 4f16073 | 2015-10-27 17:15:38 -0700 | [diff] [blame] | 431 | } finally { |
| 432 | Binder.restoreCallingIdentity(id); |
| 433 | } |
| 434 | } |
| 435 | |
Makoto Onuki | a4f1197 | 2015-10-01 13:19:58 -0700 | [diff] [blame] | 436 | public static void dumpRestrictions(PrintWriter pw, String prefix, Bundle restrictions) { |
| 437 | boolean noneSet = true; |
| 438 | if (restrictions != null) { |
| 439 | for (String key : restrictions.keySet()) { |
| 440 | if (restrictions.getBoolean(key, false)) { |
| 441 | pw.println(prefix + key); |
| 442 | noneSet = false; |
| 443 | } |
| 444 | } |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 445 | if (noneSet) { |
| 446 | pw.println(prefix + "none"); |
| 447 | } |
| 448 | } else { |
| 449 | pw.println(prefix + "null"); |
Makoto Onuki | a4f1197 | 2015-10-01 13:19:58 -0700 | [diff] [blame] | 450 | } |
| 451 | } |
| 452 | } |