1 /*
2  * Copyright (C) 2018 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 
17 package com.android.settings.security;
18 
19 import android.content.Context;
20 import android.os.UserHandle;
21 import android.provider.Settings;
22 
23 import com.android.internal.widget.LockPatternUtils;
24 import com.android.settings.R;
25 import com.android.settings.core.TogglePreferenceController;
26 import com.android.settings.overlay.FeatureFactory;
27 
28 public class ShowPasswordPreferenceController extends TogglePreferenceController {
29 
30     private static final String KEY_SHOW_PASSWORD = "show_password";
31     private static final int MY_USER_ID = UserHandle.myUserId();
32     private final LockPatternUtils mLockPatternUtils;
33 
ShowPasswordPreferenceController(Context context)34     public ShowPasswordPreferenceController(Context context) {
35         super(context, KEY_SHOW_PASSWORD);
36         mLockPatternUtils = FeatureFactory.getFactory(context)
37                 .getSecurityFeatureProvider()
38                 .getLockPatternUtils(context);
39     }
40 
41     @Override
isChecked()42     public boolean isChecked() {
43         return Settings.System.getInt(mContext.getContentResolver(),
44                 Settings.System.TEXT_SHOW_PASSWORD, 1) != 0;
45     }
46 
47     @Override
setChecked(boolean isChecked)48     public boolean setChecked(boolean isChecked) {
49         Settings.System.putInt(mContext.getContentResolver(), Settings.System.TEXT_SHOW_PASSWORD,
50                 isChecked ? 1 : 0);
51         mLockPatternUtils.setVisiblePasswordEnabled(isChecked, MY_USER_ID);
52         return true;
53     }
54 
55     @Override
getAvailabilityStatus()56     public int getAvailabilityStatus() {
57         return mContext.getResources().getBoolean(R.bool.config_show_show_password)
58                 ? AVAILABLE : UNSUPPORTED_ON_DEVICE;
59     }
60 
61 }
62 
63