1 /* 2 * Copyright (C) 2016 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 package com.android.settings.users; 17 18 import android.content.Context; 19 import android.provider.Settings; 20 21 import androidx.preference.Preference; 22 23 import com.android.settings.core.TogglePreferenceController; 24 import com.android.settingslib.RestrictedSwitchPreference; 25 26 public class AddUserWhenLockedPreferenceController extends TogglePreferenceController { 27 28 private final UserCapabilities mUserCaps; 29 AddUserWhenLockedPreferenceController(Context context, String key)30 public AddUserWhenLockedPreferenceController(Context context, String key) { 31 super(context, key); 32 mUserCaps = UserCapabilities.create(context); 33 } 34 35 @Override updateState(Preference preference)36 public void updateState(Preference preference) { 37 super.updateState(preference); 38 mUserCaps.updateAddUserCapabilities(mContext); 39 final RestrictedSwitchPreference restrictedSwitchPreference = 40 (RestrictedSwitchPreference) preference; 41 if (!isAvailable()) { 42 restrictedSwitchPreference.setVisible(false); 43 } else { 44 restrictedSwitchPreference.setDisabledByAdmin( 45 mUserCaps.disallowAddUser() ? mUserCaps.getEnforcedAdmin() : null); 46 restrictedSwitchPreference.setVisible(mUserCaps.mUserSwitcherEnabled); 47 } 48 } 49 50 @Override getAvailabilityStatus()51 public int getAvailabilityStatus() { 52 if (!mUserCaps.isAdmin()) { 53 return DISABLED_FOR_USER; 54 } else if (mUserCaps.disallowAddUser() || mUserCaps.disallowAddUserSetByAdmin()) { 55 return DISABLED_FOR_USER; 56 } else { 57 return mUserCaps.mUserSwitcherEnabled ? AVAILABLE : CONDITIONALLY_UNAVAILABLE; 58 } 59 } 60 61 @Override isChecked()62 public boolean isChecked() { 63 return Settings.Global.getInt(mContext.getContentResolver(), 64 Settings.Global.ADD_USERS_WHEN_LOCKED, 0) == 1; 65 } 66 67 @Override setChecked(boolean isChecked)68 public boolean setChecked(boolean isChecked) { 69 return Settings.Global.putInt(mContext.getContentResolver(), 70 Settings.Global.ADD_USERS_WHEN_LOCKED, isChecked ? 1 : 0); 71 } 72 } 73