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(int userId)57     SecurityMode getSecurityMode(int userId) {
58         KeyguardUpdateMonitor monitor = KeyguardUpdateMonitor.getInstance(mContext);
59 
60         if (mIsPukScreenAvailable && SubscriptionManager.isValidSubscriptionId(
61                 monitor.getNextSubIdForState(IccCardConstants.State.PUK_REQUIRED))) {
62             return SecurityMode.SimPuk;
63         }
64 
65         if (SubscriptionManager.isValidSubscriptionId(
66                 monitor.getNextSubIdForState(IccCardConstants.State.PIN_REQUIRED))) {
67             return SecurityMode.SimPin;
68         }
69 
70         final int security = mLockPatternUtils.getActivePasswordQuality(userId);
71         switch (security) {
72             case DevicePolicyManager.PASSWORD_QUALITY_NUMERIC:
73             case DevicePolicyManager.PASSWORD_QUALITY_NUMERIC_COMPLEX:
74                 return SecurityMode.PIN;
75 
76             case DevicePolicyManager.PASSWORD_QUALITY_ALPHABETIC:
77             case DevicePolicyManager.PASSWORD_QUALITY_ALPHANUMERIC:
78             case DevicePolicyManager.PASSWORD_QUALITY_COMPLEX:
79             case DevicePolicyManager.PASSWORD_QUALITY_MANAGED:
80                 return SecurityMode.Password;
81 
82             case DevicePolicyManager.PASSWORD_QUALITY_SOMETHING:
83                 return SecurityMode.Pattern;
84             case DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED:
85                 return SecurityMode.None;
86 
87             default:
88                 throw new IllegalStateException("Unknown security quality:" + security);
89         }
90     }
91 }
92