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 com.android.internal.widget; 18 19 /** 20 * Password validation error containing an error code and optional requirement. 21 */ 22 public class PasswordValidationError { 23 // Password validation error codes 24 public static final int WEAK_CREDENTIAL_TYPE = 1; 25 public static final int CONTAINS_INVALID_CHARACTERS = 2; 26 public static final int TOO_SHORT = 3; 27 public static final int TOO_LONG = 4; 28 public static final int CONTAINS_SEQUENCE = 5; 29 public static final int NOT_ENOUGH_LETTERS = 6; 30 public static final int NOT_ENOUGH_UPPER_CASE = 7; 31 public static final int NOT_ENOUGH_LOWER_CASE = 8; 32 public static final int NOT_ENOUGH_DIGITS = 9; 33 public static final int NOT_ENOUGH_SYMBOLS = 10; 34 public static final int NOT_ENOUGH_NON_LETTER = 11; 35 public static final int NOT_ENOUGH_NON_DIGITS = 12; 36 public static final int RECENTLY_USED = 13; 37 // WARNING: if you add a new error, make sure it is presented to the user correctly in Settings. 38 39 public final int errorCode; 40 public final int requirement; 41 PasswordValidationError(int errorCode)42 public PasswordValidationError(int errorCode) { 43 this(errorCode, 0); 44 } 45 PasswordValidationError(int errorCode, int requirement)46 public PasswordValidationError(int errorCode, int requirement) { 47 this.errorCode = errorCode; 48 this.requirement = requirement; 49 } 50 51 @Override toString()52 public String toString() { 53 return errorCodeToString(errorCode) + (requirement > 0 ? "; required: " + requirement : ""); 54 } 55 56 /** 57 * Returns textual representation of the error for logging purposes. 58 */ errorCodeToString(int error)59 private static String errorCodeToString(int error) { 60 switch (error) { 61 case WEAK_CREDENTIAL_TYPE: return "Weak credential type"; 62 case CONTAINS_INVALID_CHARACTERS: return "Contains an invalid character"; 63 case TOO_SHORT: return "Password too short"; 64 case TOO_LONG: return "Password too long"; 65 case CONTAINS_SEQUENCE: return "Sequence too long"; 66 case NOT_ENOUGH_LETTERS: return "Too few letters"; 67 case NOT_ENOUGH_UPPER_CASE: return "Too few upper case letters"; 68 case NOT_ENOUGH_LOWER_CASE: return "Too few lower case letters"; 69 case NOT_ENOUGH_DIGITS: return "Too few numeric characters"; 70 case NOT_ENOUGH_SYMBOLS: return "Too few symbols"; 71 case NOT_ENOUGH_NON_LETTER: return "Too few non-letter characters"; 72 case NOT_ENOUGH_NON_DIGITS: return "Too few non-numeric characters"; 73 case RECENTLY_USED: return "Pin or password was recently used"; 74 default: return "Unknown error " + error; 75 } 76 } 77 78 } 79