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.PreferenceControllerMixin; 23 import com.android.settings.users.UserCapabilities; 24 import com.android.settingslib.RestrictedSwitchPreference; 25 import com.android.settingslib.core.AbstractPreferenceController; 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 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 AddUserWhenLockedPreferenceController(Context context)40 public AddUserWhenLockedPreferenceController(Context context) { 41 super(context); 42 mUserCaps = UserCapabilities.create(context); 43 } 44 45 @Override updateState(Preference preference)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 onPreferenceChange(Preference preference, Object newValue)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 onPause()64 public void onPause() { 65 mShouldUpdateUserList = true; 66 } 67 68 @Override onResume()69 public void onResume() { 70 if (mShouldUpdateUserList) { 71 mUserCaps.updateAddUserCapabilities(mContext); 72 } 73 } 74 75 @Override isAvailable()76 public boolean isAvailable() { 77 return mUserCaps.isAdmin() && 78 (!mUserCaps.disallowAddUser() || mUserCaps.disallowAddUserSetByAdmin()); 79 } 80 81 @Override getPreferenceKey()82 public String getPreferenceKey() { 83 return KEY_ADD_USER_WHEN_LOCKED; 84 } 85 } 86