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