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