Debian Codegraph @

core/java/android/service/autofill/RegexValidator.java

raw symbols SCIP
1 /*
2 * Copyright (C) 2017 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.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 * Defines if a field is valid based on a regular expression (regex).
33 *
34 * <p>See {@link SaveInfo.Builder#setValidator(Validator)} for examples.
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 * Default constructor.
45 *
46 * @param id id of the field whose regex is applied to.
47 * @param regex regular expression that defines the result of the validator: if the regex
48 * matches the contents of the field identified by {@code id}, it returns {@code true};
49 * otherwise, it returns {@code false}.
50 */
51 public RegexValidator(@NonNull AutofillId id, @NonNull Pattern regex) {
52 mId = Objects.requireNonNull(id);
53 mRegex = Objects.requireNonNull(regex);
54 }
55
56 /** @hide */
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 // Object "contract" methods. //
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 // Parcelable "contract" methods. //
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 }