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.res.Resources; 22 import android.telephony.SubscriptionManager; 23 import android.telephony.TelephonyManager; 24 25 import com.android.internal.widget.LockPatternUtils; 26 import com.android.systemui.dagger.SysUISingleton; 27 import com.android.systemui.dagger.qualifiers.Main; 28 29 import javax.inject.Inject; 30 31 @SysUISingleton 32 public class KeyguardSecurityModel { 33 34 /** 35 * The different types of security available. 36 * @see KeyguardSecurityContainerController#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 boolean mIsPukScreenAvailable; 49 50 private final LockPatternUtils mLockPatternUtils; 51 private final KeyguardUpdateMonitor mKeyguardUpdateMonitor; 52 53 @Inject KeyguardSecurityModel(@ain Resources resources, LockPatternUtils lockPatternUtils, KeyguardUpdateMonitor keyguardUpdateMonitor)54 KeyguardSecurityModel(@Main Resources resources, LockPatternUtils lockPatternUtils, 55 KeyguardUpdateMonitor keyguardUpdateMonitor) { 56 mIsPukScreenAvailable = resources.getBoolean( 57 com.android.internal.R.bool.config_enable_puk_unlock_screen); 58 mLockPatternUtils = lockPatternUtils; 59 mKeyguardUpdateMonitor = keyguardUpdateMonitor; 60 } 61 getSecurityMode(int userId)62 public SecurityMode getSecurityMode(int userId) { 63 if (mIsPukScreenAvailable && SubscriptionManager.isValidSubscriptionId( 64 mKeyguardUpdateMonitor.getNextSubIdForState( 65 TelephonyManager.SIM_STATE_PUK_REQUIRED))) { 66 return SecurityMode.SimPuk; 67 } 68 69 if (SubscriptionManager.isValidSubscriptionId( 70 mKeyguardUpdateMonitor.getNextSubIdForState( 71 TelephonyManager.SIM_STATE_PIN_REQUIRED))) { 72 return SecurityMode.SimPin; 73 } 74 75 final int security = whitelistIpcs(() -> 76 mLockPatternUtils.getActivePasswordQuality(userId)); 77 switch (security) { 78 case DevicePolicyManager.PASSWORD_QUALITY_NUMERIC: 79 case DevicePolicyManager.PASSWORD_QUALITY_NUMERIC_COMPLEX: 80 return SecurityMode.PIN; 81 82 case DevicePolicyManager.PASSWORD_QUALITY_ALPHABETIC: 83 case DevicePolicyManager.PASSWORD_QUALITY_ALPHANUMERIC: 84 case DevicePolicyManager.PASSWORD_QUALITY_COMPLEX: 85 case DevicePolicyManager.PASSWORD_QUALITY_MANAGED: 86 return SecurityMode.Password; 87 88 case DevicePolicyManager.PASSWORD_QUALITY_SOMETHING: 89 return SecurityMode.Pattern; 90 case DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED: 91 return SecurityMode.None; 92 93 default: 94 throw new IllegalStateException("Unknown security quality:" + security); 95 } 96 } 97 98 /** 99 * Returns whether the given security view should be used in a "one handed" way. This can be 100 * used to change how the security view is drawn (e.g. take up less of the screen, and align to 101 * one side). 102 */ isSecurityViewOneHanded(SecurityMode securityMode)103 public static boolean isSecurityViewOneHanded(SecurityMode securityMode) { 104 return securityMode == SecurityMode.Pattern || securityMode == SecurityMode.PIN; 105 } 106 } 107