• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.accounts;
17 
18 import android.content.Context;
19 import android.provider.Settings.Global;
20 import android.support.v7.preference.Preference;
21 
22 import com.android.settings.core.PreferenceController;
23 import com.android.settings.core.lifecycle.LifecycleObserver;
24 import com.android.settings.core.lifecycle.events.OnPause;
25 import com.android.settings.core.lifecycle.events.OnResume;
26 import com.android.settings.users.UserCapabilities;
27 import com.android.settingslib.RestrictedSwitchPreference;
28 
29 public class AddUserWhenLockedPreferenceController extends PreferenceController
30         implements Preference.OnPreferenceChangeListener, LifecycleObserver, OnPause, OnResume {
31 
32     private static final String KEY_ADD_USER_WHEN_LOCKED = "add_users_when_locked";
33 
34     private RestrictedSwitchPreference mAddUserWhenLocked;
35     private UserCapabilities mUserCaps;
36     private boolean mShouldUpdateUserList;
37 
AddUserWhenLockedPreferenceController(Context context)38     public AddUserWhenLockedPreferenceController(Context context) {
39         super(context);
40         mUserCaps = UserCapabilities.create(context);
41     }
42 
43     @Override
updateState(Preference preference)44     public void updateState(Preference preference) {
45         RestrictedSwitchPreference restrictedSwitchPreference =
46                 (RestrictedSwitchPreference) preference;
47         int value = Global.getInt(mContext.getContentResolver(), Global.ADD_USERS_WHEN_LOCKED, 0);
48         restrictedSwitchPreference.setChecked(value == 1);
49         restrictedSwitchPreference.setDisabledByAdmin(
50                 mUserCaps.disallowAddUser() ? mUserCaps.getEnforcedAdmin() : null);
51     }
52 
53     @Override
onPreferenceChange(Preference preference, Object newValue)54     public boolean onPreferenceChange(Preference preference, Object newValue) {
55         Boolean value = (Boolean) newValue;
56         Global.putInt(mContext.getContentResolver(),
57                 Global.ADD_USERS_WHEN_LOCKED, value != null && value ? 1 : 0);
58         return true;
59     }
60 
61     @Override
onPause()62     public void onPause() {
63         mShouldUpdateUserList = true;
64     }
65 
66     @Override
onResume()67     public void onResume() {
68         if (mShouldUpdateUserList) {
69             mUserCaps.updateAddUserCapabilities(mContext);
70         }
71     }
72 
73     @Override
isAvailable()74     public boolean isAvailable() {
75         return mUserCaps.isAdmin() &&
76                 (!mUserCaps.disallowAddUser() || mUserCaps.disallowAddUserSetByAdmin());
77     }
78 
79     @Override
getPreferenceKey()80     public String getPreferenceKey() {
81         return KEY_ADD_USER_WHEN_LOCKED;
82     }
83 }
84