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