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