blob: 9dfe78d2b372b409f36566913e4a6ca8b3a93b3a [file] [log] [blame]
Felipe Leme979013d2017-06-22 10:59:23 -07001/*
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
17package android.service.autofill;
18
19import static android.view.autofill.Helper.sDebug;
20
21import android.annotation.NonNull;
Philip P. Moltmannf28764d2017-07-11 10:43:43 -070022import android.annotation.TestApi;
Felipe Leme979013d2017-06-22 10:59:23 -070023import android.os.Parcel;
24import android.os.Parcelable;
25import android.util.Log;
26import android.view.autofill.AutofillId;
27
28import com.android.internal.util.Preconditions;
29
Philip P. Moltmannf28764d2017-07-11 10:43:43 -070030import java.util.regex.Pattern;
31
Felipe Leme979013d2017-06-22 10:59:23 -070032/**
33 * Defines if a field is valid based on a regular expression (regex).
34 *
35 * <p>See {@link SaveInfo.Builder#setValidator(Validator)} for examples.
36 */
Felipe Lemec7cea5b2017-08-02 09:50:15 -070037public final class RegexValidator extends InternalValidator implements Validator, Parcelable {
Felipe Leme979013d2017-06-22 10:59:23 -070038
Felipe Lemec7cea5b2017-08-02 09:50:15 -070039 private static final String TAG = "RegexValidator";
Felipe Leme979013d2017-06-22 10:59:23 -070040
41 private final AutofillId mId;
Philip P. Moltmannf28764d2017-07-11 10:43:43 -070042 private final Pattern mRegex;
Felipe Leme979013d2017-06-22 10:59:23 -070043
44 /**
Philip P. Moltmannf28764d2017-07-11 10:43:43 -070045 * Default constructor.
46 *
47 * @param id id of the field whose regex is applied to.
48 * @param regex regular expression that defines the result of the validator: if the regex
49 * matches the contents of the field identified by {@code id}, it returns {@code true};
Felipe Leme906b8532017-07-17 14:56:17 -070050 * otherwise, it returns {@code false}.
Felipe Leme979013d2017-06-22 10:59:23 -070051 */
Felipe Lemec7cea5b2017-08-02 09:50:15 -070052 public RegexValidator(@NonNull AutofillId id, @NonNull Pattern regex) {
Felipe Leme979013d2017-06-22 10:59:23 -070053 mId = Preconditions.checkNotNull(id);
Felipe Leme906b8532017-07-17 14:56:17 -070054 mRegex = Preconditions.checkNotNull(regex);
Felipe Leme979013d2017-06-22 10:59:23 -070055 }
56
57 /** @hide */
58 @Override
Philip P. Moltmannf28764d2017-07-11 10:43:43 -070059 @TestApi
Felipe Leme979013d2017-06-22 10:59:23 -070060 public boolean isValid(@NonNull ValueFinder finder) {
61 final String value = finder.findByAutofillId(mId);
62 if (value == null) {
63 Log.w(TAG, "No view for id " + mId);
64 return false;
65 }
Philip P. Moltmannf28764d2017-07-11 10:43:43 -070066
67 final boolean valid = mRegex.matcher(value).matches();
Felipe Leme979013d2017-06-22 10:59:23 -070068 if (sDebug) Log.d(TAG, "isValid(): " + valid);
69 return valid;
70 }
71
72 /////////////////////////////////////
73 // Object "contract" methods. //
74 /////////////////////////////////////
75 @Override
76 public String toString() {
77 if (!sDebug) return super.toString();
78
Felipe Lemec7cea5b2017-08-02 09:50:15 -070079 return "RegexValidator: [id=" + mId + ", regex=" + mRegex + "]";
Felipe Leme979013d2017-06-22 10:59:23 -070080 }
81
82 /////////////////////////////////////
83 // Parcelable "contract" methods. //
84 /////////////////////////////////////
85 @Override
86 public int describeContents() {
87 return 0;
88 }
89
90 @Override
91 public void writeToParcel(Parcel parcel, int flags) {
92 parcel.writeParcelable(mId, flags);
Felipe Leme906b8532017-07-17 14:56:17 -070093 parcel.writeSerializable(mRegex);
Felipe Leme979013d2017-06-22 10:59:23 -070094 }
95
Felipe Lemec7cea5b2017-08-02 09:50:15 -070096 public static final Parcelable.Creator<RegexValidator> CREATOR =
97 new Parcelable.Creator<RegexValidator>() {
Felipe Leme979013d2017-06-22 10:59:23 -070098 @Override
Felipe Lemec7cea5b2017-08-02 09:50:15 -070099 public RegexValidator createFromParcel(Parcel parcel) {
100 return new RegexValidator(parcel.readParcelable(null),
Felipe Leme906b8532017-07-17 14:56:17 -0700101 (Pattern) parcel.readSerializable());
Felipe Leme979013d2017-06-22 10:59:23 -0700102 }
103
104 @Override
Felipe Lemec7cea5b2017-08-02 09:50:15 -0700105 public RegexValidator[] newArray(int size) {
106 return new RegexValidator[size];
Felipe Leme979013d2017-06-22 10:59:23 -0700107 }
108 };
109}