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