1 /* 2 * Copyright (C) 2019 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 android.app.admin; 18 19 import static android.app.admin.DevicePolicyManager.PASSWORD_QUALITY_ALPHABETIC; 20 import static android.app.admin.DevicePolicyManager.PASSWORD_QUALITY_ALPHANUMERIC; 21 import static android.app.admin.DevicePolicyManager.PASSWORD_QUALITY_BIOMETRIC_WEAK; 22 import static android.app.admin.DevicePolicyManager.PASSWORD_QUALITY_COMPLEX; 23 import static android.app.admin.DevicePolicyManager.PASSWORD_QUALITY_NUMERIC; 24 import static android.app.admin.DevicePolicyManager.PASSWORD_QUALITY_NUMERIC_COMPLEX; 25 import static android.app.admin.DevicePolicyManager.PASSWORD_QUALITY_SOMETHING; 26 import static android.app.admin.DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED; 27 28 import static com.android.internal.widget.LockPatternUtils.CREDENTIAL_TYPE_NONE; 29 import static com.android.internal.widget.LockPatternUtils.CREDENTIAL_TYPE_PASSWORD; 30 import static com.android.internal.widget.LockPatternUtils.CREDENTIAL_TYPE_PATTERN; 31 import static com.android.internal.widget.LockPatternUtils.CREDENTIAL_TYPE_PIN; 32 33 /** 34 * {@hide} 35 */ 36 public class PasswordPolicy { 37 public static final int DEF_MINIMUM_LENGTH = 0; 38 public static final int DEF_MINIMUM_LETTERS = 1; 39 public static final int DEF_MINIMUM_UPPER_CASE = 0; 40 public static final int DEF_MINIMUM_LOWER_CASE = 0; 41 public static final int DEF_MINIMUM_NUMERIC = 1; 42 public static final int DEF_MINIMUM_SYMBOLS = 1; 43 public static final int DEF_MINIMUM_NON_LETTER = 0; 44 45 public int quality = PASSWORD_QUALITY_UNSPECIFIED; 46 public int length = DEF_MINIMUM_LENGTH; 47 public int letters = DEF_MINIMUM_LETTERS; 48 public int upperCase = DEF_MINIMUM_UPPER_CASE; 49 public int lowerCase = DEF_MINIMUM_LOWER_CASE; 50 public int numeric = DEF_MINIMUM_NUMERIC; 51 public int symbols = DEF_MINIMUM_SYMBOLS; 52 public int nonLetter = DEF_MINIMUM_NON_LETTER; 53 54 /** 55 * Returns a minimum password metrics that the password should have to satisfy current policy. 56 */ getMinMetrics()57 public PasswordMetrics getMinMetrics() { 58 if (quality == PASSWORD_QUALITY_UNSPECIFIED) { 59 return new PasswordMetrics(CREDENTIAL_TYPE_NONE); 60 } else if (quality == PASSWORD_QUALITY_BIOMETRIC_WEAK 61 || quality == PASSWORD_QUALITY_SOMETHING) { 62 return new PasswordMetrics(CREDENTIAL_TYPE_PATTERN); 63 } else if (quality == PASSWORD_QUALITY_NUMERIC 64 || quality == PASSWORD_QUALITY_NUMERIC_COMPLEX) { 65 PasswordMetrics result = new PasswordMetrics(CREDENTIAL_TYPE_PIN); 66 result.length = length; 67 if (quality == PASSWORD_QUALITY_NUMERIC_COMPLEX) { 68 result.seqLength = PasswordMetrics.MAX_ALLOWED_SEQUENCE; 69 } 70 return result; 71 } // quality is ALPHABETIC or stronger. 72 73 PasswordMetrics result = new PasswordMetrics(CREDENTIAL_TYPE_PASSWORD); 74 result.length = length; 75 76 if (quality == PASSWORD_QUALITY_ALPHABETIC) { 77 result.nonNumeric = 1; 78 } else if (quality == PASSWORD_QUALITY_ALPHANUMERIC) { 79 result.numeric = 1; 80 result.nonNumeric = 1; 81 } else if (quality == PASSWORD_QUALITY_COMPLEX) { 82 result.numeric = numeric; 83 result.letters = letters; 84 result.upperCase = upperCase; 85 result.lowerCase = lowerCase; 86 result.nonLetter = nonLetter; 87 result.symbols = symbols; 88 } 89 return result; 90 } 91 } 92