public final class SimpleRegexValidator implements android.os.Parcelable {
ctor public SimpleRegexValidator(android.view.autofill.AutofillId, java.lang.String);
method public int describeContents();
+ method public boolean isValid(android.service.autofill.ValueFinder);
method public void writeToParcel(android.os.Parcel, int);
field public static final android.os.Parcelable.Creator<android.service.autofill.SimpleRegexValidator> CREATOR;
}
*
* @param id id of the screen field that will be used to evaluate whether the image should
* be used.
- * @param regex regular expression defining what should be matched to use this image.
+ * @param regex regular expression defining what should be matched to use this image. The
+ * pattern will be {@link Pattern#compile compiled} without setting any flags.
* @param resId resource id of the image (in the autofill service's package). The
* {@link RemoteViews presentation} must contain a {@link ImageView} child with that id.
*/
/**
* Adds an option to replace the child view with a different image when the regex matches.
*
- * @param regex regular expression defining what should be matched to use this image.
+ * @param regex regular expression defining what should be matched to use this image. The
+ * pattern will be {@link Pattern#compile compiled} without setting any flags.
* @param resId resource id of the image (in the autofill service's package). The
* {@link RemoteViews presentation} must contain a {@link ImageView} child with that id.
*
import static android.view.autofill.Helper.sDebug;
import android.annotation.NonNull;
+import android.annotation.TestApi;
import android.os.Parcel;
import android.os.Parcelable;
import android.util.Log;
import com.android.internal.util.Preconditions;
+import java.util.regex.Pattern;
+
/**
* Defines if a field is valid based on a regular expression (regex).
*
private static final String TAG = "SimpleRegexValidator";
private final AutofillId mId;
- private final String mRegex;
+ private final Pattern mRegex;
/**
- * Default constructor.
- *
- * @param id id of the field whose regex is applied to.
- * @param regex regular expression that defines the result
- * of the validator: if the regex matches the contents of
- * the field identified by {@code id}, it returns {@code true}; otherwise, it
- * returns {@code false}.
+ * Default constructor.
+ *
+ * @param id id of the field whose regex is applied to.
+ * @param regex regular expression that defines the result of the validator: if the regex
+ * matches the contents of the field identified by {@code id}, it returns {@code true};
+ * otherwise, it returns {@code false}. The pattern will be {@link Pattern#compile compiled}
+ * without setting any flags.
*/
public SimpleRegexValidator(@NonNull AutofillId id, @NonNull String regex) {
mId = Preconditions.checkNotNull(id);
- //TODO(b/62534917): throw exception if regex is invalid
- mRegex = Preconditions.checkNotNull(regex);
+ mRegex = Pattern.compile(regex);
}
/** @hide */
@Override
+ @TestApi
public boolean isValid(@NonNull ValueFinder finder) {
final String value = finder.findByAutofillId(mId);
if (value == null) {
Log.w(TAG, "No view for id " + mId);
return false;
}
- final boolean valid = value.matches(mRegex);
+
+ final boolean valid = mRegex.matcher(value).matches();
if (sDebug) Log.d(TAG, "isValid(): " + valid);
return valid;
}
@Override
public void writeToParcel(Parcel parcel, int flags) {
parcel.writeParcelable(mId, flags);
- parcel.writeString(mRegex);
+ parcel.writeString(mRegex.pattern());
}
public static final Parcelable.Creator<SimpleRegexValidator> CREATOR =