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.screenlock;
18 
19 import static android.app.admin.DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED;
20 
21 import android.content.Context;
22 import android.os.UserHandle;
23 
24 import androidx.preference.Preference;
25 import androidx.preference.PreferenceScreen;
26 
27 import com.android.internal.widget.LockPatternUtils;
28 import com.android.settings.core.BasePreferenceController;
29 import com.android.settings.notification.LockScreenNotificationPreferenceController;
30 import com.android.settings.overlay.FeatureFactory;
31 import com.android.settingslib.core.lifecycle.LifecycleObserver;
32 import com.android.settingslib.core.lifecycle.events.OnResume;
33 
34 public class LockScreenPreferenceController extends BasePreferenceController implements
35         LifecycleObserver, OnResume {
36 
37     private static final int MY_USER_ID = UserHandle.myUserId();
38     private final LockPatternUtils mLockPatternUtils;
39     private Preference mPreference;
40 
LockScreenPreferenceController(Context context, String key)41     public LockScreenPreferenceController(Context context, String key) {
42         super(context, key);
43         mLockPatternUtils = FeatureFactory.getFactory(context)
44                 .getSecurityFeatureProvider().getLockPatternUtils(context);
45     }
46 
47     @Override
displayPreference(PreferenceScreen screen)48     public void displayPreference(PreferenceScreen screen) {
49         super.displayPreference(screen);
50         mPreference = screen.findPreference(getPreferenceKey());
51     }
52 
53     @Override
getAvailabilityStatus()54     public int getAvailabilityStatus() {
55         if (!mLockPatternUtils.isSecure(MY_USER_ID)) {
56             return mLockPatternUtils.isLockScreenDisabled(MY_USER_ID)
57                     ? DISABLED_FOR_USER : AVAILABLE_UNSEARCHABLE;
58         } else {
59             return mLockPatternUtils.getKeyguardStoredPasswordQuality(MY_USER_ID)
60                     == PASSWORD_QUALITY_UNSPECIFIED
61                     ? DISABLED_FOR_USER : AVAILABLE_UNSEARCHABLE;
62         }
63     }
64 
65     @Override
updateState(Preference preference)66     public void updateState(Preference preference) {
67         preference.setSummary(
68                 LockScreenNotificationPreferenceController.getSummaryResource(mContext));
69     }
70 
71     @Override
onResume()72     public void onResume() {
73         mPreference.setVisible(isAvailable());
74     }
75 }
76