OSDN Git Service

Verify regex for SimpleRegexVerifier
authorPhilip P. Moltmann <moltmann@google.com>
Tue, 11 Jul 2017 17:43:43 +0000 (10:43 -0700)
committerPhilip P. Moltmann <moltmann@google.com>
Wed, 12 Jul 2017 15:38:41 +0000 (08:38 -0700)
Test: cts-tradefed run cts-dev -m CtsAutoFillServiceTestCases
          --test=android.autofillservice.cts.SimpleRegexValidatorTest
Bug: 62534917
Change-Id: I63a887643bff0f8d584069ee47f091b0edb8684c

api/test-current.txt
core/java/android/service/autofill/ImageTransformation.java
core/java/android/service/autofill/SimpleRegexValidator.java

index 6d16e35..59c906b 100644 (file)
@@ -37284,6 +37284,7 @@ package android.service.autofill {
   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;
   }
index 5407ad0..52412cd 100644 (file)
@@ -102,7 +102,8 @@ public final class ImageTransformation extends InternalTransformation implements
          *
          * @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.
          */
@@ -115,7 +116,8 @@ public final class ImageTransformation extends InternalTransformation implements
         /**
          * 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.
          *
index ffe0076..225757a 100644 (file)
@@ -19,6 +19,7 @@ package android.service.autofill;
 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;
@@ -26,6 +27,8 @@ import android.view.autofill.AutofillId;
 
 import com.android.internal.util.Preconditions;
 
+import java.util.regex.Pattern;
+
 /**
  * Defines if a field is valid based on a regular expression (regex).
  *
@@ -36,32 +39,33 @@ public final class SimpleRegexValidator extends InternalValidator implements Par
     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;
     }
@@ -87,7 +91,7 @@ public final class SimpleRegexValidator extends InternalValidator implements Par
     @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 =