1 /*
2  * Copyright (C) 2017 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.UserManager;
21 import android.security.KeyStore;
22 
23 import androidx.preference.PreferenceScreen;
24 
25 import com.android.settingslib.RestrictedPreference;
26 import com.android.settingslib.core.lifecycle.Lifecycle;
27 import com.android.settingslib.core.lifecycle.LifecycleObserver;
28 import com.android.settingslib.core.lifecycle.events.OnResume;
29 
30 public class ResetCredentialsPreferenceController extends RestrictedEncryptionPreferenceController
31         implements LifecycleObserver, OnResume {
32 
33     private static final String KEY_RESET_CREDENTIALS = "credentials_reset";
34 
35     private final KeyStore mKeyStore;
36 
37     private RestrictedPreference mPreference;
38 
ResetCredentialsPreferenceController(Context context, Lifecycle lifecycle)39     public ResetCredentialsPreferenceController(Context context, Lifecycle lifecycle) {
40         super(context, UserManager.DISALLOW_CONFIG_CREDENTIALS);
41         mKeyStore = KeyStore.getInstance();
42         if (lifecycle != null) {
43             lifecycle.addObserver(this);
44         }
45     }
46 
47     @Override
getPreferenceKey()48     public String getPreferenceKey() {
49         return KEY_RESET_CREDENTIALS;
50     }
51 
52     @Override
displayPreference(PreferenceScreen screen)53     public void displayPreference(PreferenceScreen screen) {
54         super.displayPreference(screen);
55         mPreference = screen.findPreference(getPreferenceKey());
56     }
57 
58     @Override
onResume()59     public void onResume() {
60         if (mPreference != null && !mPreference.isDisabledByAdmin()) {
61             mPreference.setEnabled(!mKeyStore.isEmpty());
62         }
63     }
64 }
65