Felipe Leme | 979013d | 2017-06-22 10:59:23 -0700 | [diff] [blame] | 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; |
Philip P. Moltmann | f28764d | 2017-07-11 10:43:43 -0700 | [diff] [blame] | 22 | import android.annotation.TestApi; |
Felipe Leme | 979013d | 2017-06-22 10:59:23 -0700 | [diff] [blame] | 23 | import android.os.Parcel; |
| 24 | import android.os.Parcelable; |
| 25 | import android.util.Log; |
| 26 | import android.view.autofill.AutofillId; |
| 27 | |
| 28 | import com.android.internal.util.Preconditions; |
| 29 | |
Philip P. Moltmann | f28764d | 2017-07-11 10:43:43 -0700 | [diff] [blame] | 30 | import java.util.regex.Pattern; |
| 31 | |
Felipe Leme | 979013d | 2017-06-22 10:59:23 -0700 | [diff] [blame] | 32 | /** |
| 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 Leme | c7cea5b | 2017-08-02 09:50:15 -0700 | [diff] [blame] | 37 | public final class RegexValidator extends InternalValidator implements Validator, Parcelable { |
Felipe Leme | 979013d | 2017-06-22 10:59:23 -0700 | [diff] [blame] | 38 | |
Felipe Leme | c7cea5b | 2017-08-02 09:50:15 -0700 | [diff] [blame] | 39 | private static final String TAG = "RegexValidator"; |
Felipe Leme | 979013d | 2017-06-22 10:59:23 -0700 | [diff] [blame] | 40 | |
| 41 | private final AutofillId mId; |
Philip P. Moltmann | f28764d | 2017-07-11 10:43:43 -0700 | [diff] [blame] | 42 | private final Pattern mRegex; |
Felipe Leme | 979013d | 2017-06-22 10:59:23 -0700 | [diff] [blame] | 43 | |
| 44 | /** |
Philip P. Moltmann | f28764d | 2017-07-11 10:43:43 -0700 | [diff] [blame] | 45 | * 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 Leme | 906b853 | 2017-07-17 14:56:17 -0700 | [diff] [blame] | 50 | * otherwise, it returns {@code false}. |
Felipe Leme | 979013d | 2017-06-22 10:59:23 -0700 | [diff] [blame] | 51 | */ |
Felipe Leme | c7cea5b | 2017-08-02 09:50:15 -0700 | [diff] [blame] | 52 | public RegexValidator(@NonNull AutofillId id, @NonNull Pattern regex) { |
Felipe Leme | 979013d | 2017-06-22 10:59:23 -0700 | [diff] [blame] | 53 | mId = Preconditions.checkNotNull(id); |
Felipe Leme | 906b853 | 2017-07-17 14:56:17 -0700 | [diff] [blame] | 54 | mRegex = Preconditions.checkNotNull(regex); |
Felipe Leme | 979013d | 2017-06-22 10:59:23 -0700 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | /** @hide */ |
| 58 | @Override |
Philip P. Moltmann | f28764d | 2017-07-11 10:43:43 -0700 | [diff] [blame] | 59 | @TestApi |
Felipe Leme | 979013d | 2017-06-22 10:59:23 -0700 | [diff] [blame] | 60 | 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. Moltmann | f28764d | 2017-07-11 10:43:43 -0700 | [diff] [blame] | 66 | |
| 67 | final boolean valid = mRegex.matcher(value).matches(); |
Felipe Leme | 979013d | 2017-06-22 10:59:23 -0700 | [diff] [blame] | 68 | 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 Leme | c7cea5b | 2017-08-02 09:50:15 -0700 | [diff] [blame] | 79 | return "RegexValidator: [id=" + mId + ", regex=" + mRegex + "]"; |
Felipe Leme | 979013d | 2017-06-22 10:59:23 -0700 | [diff] [blame] | 80 | } |
| 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 Leme | 906b853 | 2017-07-17 14:56:17 -0700 | [diff] [blame] | 93 | parcel.writeSerializable(mRegex); |
Felipe Leme | 979013d | 2017-06-22 10:59:23 -0700 | [diff] [blame] | 94 | } |
| 95 | |
Felipe Leme | c7cea5b | 2017-08-02 09:50:15 -0700 | [diff] [blame] | 96 | public static final Parcelable.Creator<RegexValidator> CREATOR = |
| 97 | new Parcelable.Creator<RegexValidator>() { |
Felipe Leme | 979013d | 2017-06-22 10:59:23 -0700 | [diff] [blame] | 98 | @Override |
Felipe Leme | c7cea5b | 2017-08-02 09:50:15 -0700 | [diff] [blame] | 99 | public RegexValidator createFromParcel(Parcel parcel) { |
| 100 | return new RegexValidator(parcel.readParcelable(null), |
Felipe Leme | 906b853 | 2017-07-17 14:56:17 -0700 | [diff] [blame] | 101 | (Pattern) parcel.readSerializable()); |
Felipe Leme | 979013d | 2017-06-22 10:59:23 -0700 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | @Override |
Felipe Leme | c7cea5b | 2017-08-02 09:50:15 -0700 | [diff] [blame] | 105 | public RegexValidator[] newArray(int size) { |
| 106 | return new RegexValidator[size]; |
Felipe Leme | 979013d | 2017-06-22 10:59:23 -0700 | [diff] [blame] | 107 | } |
| 108 | }; |
| 109 | } |