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 com.android.internal.util.Preconditions; 29 30 import java.util.regex.Pattern; 31 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 */ 37 public final class RegexValidator extends InternalValidator implements Validator, Parcelable { 38 39 private static final String TAG = "RegexValidator"; 40 41 private final AutofillId mId; 42 private final Pattern mRegex; 43 44 /** 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}; 50 * otherwise, it returns {@code false}. 51 */ RegexValidator(@onNull AutofillId id, @NonNull Pattern regex)52 public RegexValidator(@NonNull AutofillId id, @NonNull Pattern regex) { 53 mId = Preconditions.checkNotNull(id); 54 mRegex = Preconditions.checkNotNull(regex); 55 } 56 57 /** @hide */ 58 @Override 59 @TestApi isValid(@onNull ValueFinder finder)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 } 66 67 final boolean valid = mRegex.matcher(value).matches(); 68 if (sDebug) Log.d(TAG, "isValid(): " + valid); 69 return valid; 70 } 71 72 ///////////////////////////////////// 73 // Object "contract" methods. // 74 ///////////////////////////////////// 75 @Override toString()76 public String toString() { 77 if (!sDebug) return super.toString(); 78 79 return "RegexValidator: [id=" + mId + ", regex=" + mRegex + "]"; 80 } 81 82 ///////////////////////////////////// 83 // Parcelable "contract" methods. // 84 ///////////////////////////////////// 85 @Override describeContents()86 public int describeContents() { 87 return 0; 88 } 89 90 @Override writeToParcel(Parcel parcel, int flags)91 public void writeToParcel(Parcel parcel, int flags) { 92 parcel.writeParcelable(mId, flags); 93 parcel.writeSerializable(mRegex); 94 } 95 96 public static final @android.annotation.NonNull Parcelable.Creator<RegexValidator> CREATOR = 97 new Parcelable.Creator<RegexValidator>() { 98 @Override 99 public RegexValidator createFromParcel(Parcel parcel) { 100 return new RegexValidator(parcel.readParcelable(null), 101 (Pattern) parcel.readSerializable()); 102 } 103 104 @Override 105 public RegexValidator[] newArray(int size) { 106 return new RegexValidator[size]; 107 } 108 }; 109 } 110