| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.service.autofill; |
| 18 |
|
| 19 |
import static android.view.autofill.Helper.sDebug; |
| 20 |
|
| 21 |
import android.annotation.NonNull; |
| 22 |
import android.annotation.TestApi; |
| 23 |
import android.os.Parcel; |
| 24 |
import android.os.Parcelable; |
| 25 |
import android.util.Log; |
| 26 |
import android.view.autofill.AutofillId; |
| 27 |
|
| 28 |
import java.util.Objects; |
| 29 |
import java.util.regex.Pattern; |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
public final class RegexValidator extends InternalValidator implements Validator, Parcelable { |
| 37 |
|
| 38 |
private static final String TAG = "RegexValidator"; |
| 39 |
|
| 40 |
private final AutofillId mId; |
| 41 |
private final Pattern mRegex; |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
public RegexValidator(@NonNull AutofillId id, @NonNull Pattern regex) { |
| 52 |
mId = Objects.requireNonNull(id); |
| 53 |
mRegex = Objects.requireNonNull(regex); |
| 54 |
} |
| 55 |
|
| 56 |
|
| 57 |
@Override |
| 58 |
@TestApi |
| 59 |
public boolean isValid(@NonNull ValueFinder finder) { |
| 60 |
final String value = finder.findByAutofillId(mId); |
| 61 |
if (value == null) { |
| 62 |
Log.w(TAG, "No view for id " + mId); |
| 63 |
return false; |
| 64 |
} |
| 65 |
|
| 66 |
final boolean valid = mRegex.matcher(value).matches(); |
| 67 |
if (sDebug) Log.d(TAG, "isValid(): " + valid); |
| 68 |
return valid; |
| 69 |
} |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
@Override |
| 75 |
public String toString() { |
| 76 |
if (!sDebug) return super.toString(); |
| 77 |
|
| 78 |
return "RegexValidator: [id=" + mId + ", regex=" + mRegex + "]"; |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
@Override |
| 85 |
public int describeContents() { |
| 86 |
return 0; |
| 87 |
} |
| 88 |
|
| 89 |
@Override |
| 90 |
public void writeToParcel(Parcel parcel, int flags) { |
| 91 |
parcel.writeParcelable(mId, flags); |
| 92 |
parcel.writeSerializable(mRegex); |
| 93 |
} |
| 94 |
|
| 95 |
public static final @android.annotation.NonNull Parcelable.Creator<RegexValidator> CREATOR = |
| 96 |
new Parcelable.Creator<RegexValidator>() { |
| 97 |
@Override |
| 98 |
public RegexValidator createFromParcel(Parcel parcel) { |
| 99 |
return new RegexValidator(parcel.readParcelable(null, android.view.autofill.AutofillId.class), |
| 100 |
(Pattern) parcel.readSerializable(java.util.regex.Pattern.class.getClassLoader(), java.util.regex.Pattern.class)); |
| 101 |
} |
| 102 |
|
| 103 |
@Override |
| 104 |
public RegexValidator[] newArray(int size) { |
| 105 |
return new RegexValidator[size]; |
| 106 |
} |
| 107 |
}; |
| 108 |
} |