1 /*
2  * Copyright (C) 2012 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.keyguard;
17 
18 import static com.android.systemui.DejankUtils.whitelistIpcs;
19 
20 import android.app.admin.DevicePolicyManager;
21 import android.content.Context;
22 import android.telephony.SubscriptionManager;
23 import android.telephony.TelephonyManager;
24 
25 import com.android.internal.widget.LockPatternUtils;
26 import com.android.systemui.Dependency;
27 
28 import javax.inject.Inject;
29 import javax.inject.Singleton;
30 
31 @Singleton
32 public class KeyguardSecurityModel {
33 
34     /**
35      * The different types of security available.
36      * @see KeyguardSecurityContainer#showSecurityScreen
37      */
38     public enum SecurityMode {
39         Invalid, // NULL state
40         None, // No security enabled
41         Pattern, // Unlock by drawing a pattern.
42         Password, // Unlock by entering an alphanumeric password
43         PIN, // Strictly numeric password
44         SimPin, // Unlock by entering a sim pin.
45         SimPuk // Unlock by entering a sim puk
46     }
47 
48     private final Context mContext;
49     private final boolean mIsPukScreenAvailable;
50 
51     private LockPatternUtils mLockPatternUtils;
52 
53     @Inject
KeyguardSecurityModel(Context context)54     KeyguardSecurityModel(Context context) {
55         mContext = context;
56         mLockPatternUtils = new LockPatternUtils(context);
57         mIsPukScreenAvailable = mContext.getResources().getBoolean(
58                 com.android.internal.R.bool.config_enable_puk_unlock_screen);
59     }
60 
setLockPatternUtils(LockPatternUtils utils)61     void setLockPatternUtils(LockPatternUtils utils) {
62         mLockPatternUtils = utils;
63     }
64 
getSecurityMode(int userId)65     public SecurityMode getSecurityMode(int userId) {
66         KeyguardUpdateMonitor monitor = Dependency.get(KeyguardUpdateMonitor.class);
67 
68         if (mIsPukScreenAvailable && SubscriptionManager.isValidSubscriptionId(
69                 monitor.getNextSubIdForState(TelephonyManager.SIM_STATE_PUK_REQUIRED))) {
70             return SecurityMode.SimPuk;
71         }
72 
73         if (SubscriptionManager.isValidSubscriptionId(
74                 monitor.getNextSubIdForState(TelephonyManager.SIM_STATE_PIN_REQUIRED))) {
75             return SecurityMode.SimPin;
76         }
77 
78         final int security = whitelistIpcs(() ->
79                 mLockPatternUtils.getActivePasswordQuality(userId));
80         switch (security) {
81             case DevicePolicyManager.PASSWORD_QUALITY_NUMERIC:
82             case DevicePolicyManager.PASSWORD_QUALITY_NUMERIC_COMPLEX:
83                 return SecurityMode.PIN;
84 
85             case DevicePolicyManager.PASSWORD_QUALITY_ALPHABETIC:
86             case DevicePolicyManager.PASSWORD_QUALITY_ALPHANUMERIC:
87             case DevicePolicyManager.PASSWORD_QUALITY_COMPLEX:
88             case DevicePolicyManager.PASSWORD_QUALITY_MANAGED:
89                 return SecurityMode.Password;
90 
91             case DevicePolicyManager.PASSWORD_QUALITY_SOMETHING:
92                 return SecurityMode.Pattern;
93             case DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED:
94                 return SecurityMode.None;
95 
96             default:
97                 throw new IllegalStateException("Unknown security quality:" + security);
98         }
99     }
100 }
101