OSDN Git Service

Uses Pattern instead of String for new Autofill Save API.
authorFelipe Leme <felipeal@google.com>
Mon, 17 Jul 2017 21:56:17 +0000 (14:56 -0700)
committerFelipe Leme <felipeal@google.com>
Wed, 19 Jul 2017 00:40:45 +0000 (17:40 -0700)
Test: CtsAutoFillServiceTestCases pass
Bug: 62534917

Change-Id: Ic285838465c2aa55e4935cdd8f00c551faa6a972

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

index aff9938..b1d9391 100644 (file)
@@ -36961,8 +36961,8 @@ package android.service.autofill {
   }
 
   public static class CharSequenceTransformation.Builder {
-    ctor public CharSequenceTransformation.Builder(android.view.autofill.AutofillId, java.lang.String, java.lang.String);
-    method public android.service.autofill.CharSequenceTransformation.Builder addField(android.view.autofill.AutofillId, java.lang.String, java.lang.String);
+    ctor public CharSequenceTransformation.Builder(android.view.autofill.AutofillId, java.util.regex.Pattern, java.lang.String);
+    method public android.service.autofill.CharSequenceTransformation.Builder addField(android.view.autofill.AutofillId, java.util.regex.Pattern, java.lang.String);
     method public android.service.autofill.CharSequenceTransformation build();
   }
 
@@ -37058,8 +37058,8 @@ package android.service.autofill {
   }
 
   public static class ImageTransformation.Builder {
-    ctor public ImageTransformation.Builder(android.view.autofill.AutofillId, java.lang.String, int);
-    method public android.service.autofill.ImageTransformation.Builder addOption(java.lang.String, int);
+    ctor public ImageTransformation.Builder(android.view.autofill.AutofillId, java.util.regex.Pattern, int);
+    method public android.service.autofill.ImageTransformation.Builder addOption(java.util.regex.Pattern, int);
     method public android.service.autofill.ImageTransformation build();
   }
 
@@ -37111,7 +37111,7 @@ package android.service.autofill {
   }
 
   public final class SimpleRegexValidator implements android.os.Parcelable android.service.autofill.Validator {
-    ctor public SimpleRegexValidator(android.view.autofill.AutofillId, java.lang.String);
+    ctor public SimpleRegexValidator(android.view.autofill.AutofillId, java.util.regex.Pattern);
     method public int describeContents();
     method public void writeToParcel(android.os.Parcel, int);
     field public static final android.os.Parcelable.Creator<android.service.autofill.SimpleRegexValidator> CREATOR;
index 7539a58..8c0f9c1 100644 (file)
@@ -40145,8 +40145,8 @@ package android.service.autofill {
   }
 
   public static class CharSequenceTransformation.Builder {
-    ctor public CharSequenceTransformation.Builder(android.view.autofill.AutofillId, java.lang.String, java.lang.String);
-    method public android.service.autofill.CharSequenceTransformation.Builder addField(android.view.autofill.AutofillId, java.lang.String, java.lang.String);
+    ctor public CharSequenceTransformation.Builder(android.view.autofill.AutofillId, java.util.regex.Pattern, java.lang.String);
+    method public android.service.autofill.CharSequenceTransformation.Builder addField(android.view.autofill.AutofillId, java.util.regex.Pattern, java.lang.String);
     method public android.service.autofill.CharSequenceTransformation build();
   }
 
@@ -40242,8 +40242,8 @@ package android.service.autofill {
   }
 
   public static class ImageTransformation.Builder {
-    ctor public ImageTransformation.Builder(android.view.autofill.AutofillId, java.lang.String, int);
-    method public android.service.autofill.ImageTransformation.Builder addOption(java.lang.String, int);
+    ctor public ImageTransformation.Builder(android.view.autofill.AutofillId, java.util.regex.Pattern, int);
+    method public android.service.autofill.ImageTransformation.Builder addOption(java.util.regex.Pattern, int);
     method public android.service.autofill.ImageTransformation build();
   }
 
@@ -40295,7 +40295,7 @@ package android.service.autofill {
   }
 
   public final class SimpleRegexValidator implements android.os.Parcelable android.service.autofill.Validator {
-    ctor public SimpleRegexValidator(android.view.autofill.AutofillId, java.lang.String);
+    ctor public SimpleRegexValidator(android.view.autofill.AutofillId, java.util.regex.Pattern);
     method public int describeContents();
     method public void writeToParcel(android.os.Parcel, int);
     field public static final android.os.Parcelable.Creator<android.service.autofill.SimpleRegexValidator> CREATOR;
index 01652cb..da4f678 100644 (file)
@@ -37126,8 +37126,8 @@ package android.service.autofill {
   }
 
   public static class CharSequenceTransformation.Builder {
-    ctor public CharSequenceTransformation.Builder(android.view.autofill.AutofillId, java.lang.String, java.lang.String);
-    method public android.service.autofill.CharSequenceTransformation.Builder addField(android.view.autofill.AutofillId, java.lang.String, java.lang.String);
+    ctor public CharSequenceTransformation.Builder(android.view.autofill.AutofillId, java.util.regex.Pattern, java.lang.String);
+    method public android.service.autofill.CharSequenceTransformation.Builder addField(android.view.autofill.AutofillId, java.util.regex.Pattern, java.lang.String);
     method public android.service.autofill.CharSequenceTransformation build();
   }
 
@@ -37224,8 +37224,8 @@ package android.service.autofill {
   }
 
   public static class ImageTransformation.Builder {
-    ctor public ImageTransformation.Builder(android.view.autofill.AutofillId, java.lang.String, int);
-    method public android.service.autofill.ImageTransformation.Builder addOption(java.lang.String, int);
+    ctor public ImageTransformation.Builder(android.view.autofill.AutofillId, java.util.regex.Pattern, int);
+    method public android.service.autofill.ImageTransformation.Builder addOption(java.util.regex.Pattern, int);
     method public android.service.autofill.ImageTransformation build();
   }
 
@@ -37278,7 +37278,7 @@ package android.service.autofill {
   }
 
   public final class SimpleRegexValidator implements android.os.Parcelable android.service.autofill.Validator {
-    ctor public SimpleRegexValidator(android.view.autofill.AutofillId, java.lang.String);
+    ctor public SimpleRegexValidator(android.view.autofill.AutofillId, java.util.regex.Pattern);
     method public int describeContents();
     method public boolean isValid(android.service.autofill.ValueFinder);
     method public void writeToParcel(android.os.Parcel, int);
index 3ff36aa..77555fe 100644 (file)
@@ -44,15 +44,17 @@ import java.util.regex.Pattern;
  * be:
  *
  * <pre class="prettyprint">
- * new CharSequenceTransformation.Builder(ccNumberId, "^.*(\\d\\d\\d\\d)$", "...$1").build();
+ * new CharSequenceTransformation
+ *     .Builder(ccNumberId, Pattern.compile("^.*(\\d\\d\\d\\d)$"), "...$1")
+ *     .build();
  * </pre>
  *
  * <p>But a transformation that generates a {@code Exp: MM / YYYY} credit expiration date from two
  * fields (month and year) would be:
  *
  * <pre class="prettyprint">
- * new CharSequenceTransformation.Builder(ccExpMonthId, "^(\\d\\d)$", "Exp: $1")
- *   .addField(ccExpYearId, "^(\\d\\d\\d\\d)$", " / $1");
+ * new CharSequenceTransformation.Builder(ccExpMonthId, Pattern.compile("^(\\d\\d)$"), "Exp: $1")
+ *   .addField(ccExpYearId, Pattern.compile("^(\\d\\d\\d\\d)$"), " / $1");
  * </pre>
  */
 public final class CharSequenceTransformation extends InternalTransformation implements
@@ -107,12 +109,11 @@ public final class CharSequenceTransformation extends InternalTransformation imp
          *
          * @param id id of the screen field.
          * @param regex regular expression with groups (delimited by {@code (} and {@code (}) that
-         * are used to substitute parts of the value. The pattern will be {@link Pattern#compile
-         * compiled} without setting any flags.
+         * are used to substitute parts of the value.
          * @param subst the string that substitutes the matched regex, using {@code $} for
          * group substitution ({@code $1} for 1st group match, {@code $2} for 2nd, etc).
          */
-        public Builder(@NonNull AutofillId id, @NonNull String regex, @NonNull String subst) {
+        public Builder(@NonNull AutofillId id, @NonNull Pattern regex, @NonNull String subst) {
             addField(id, regex, subst);
         }
 
@@ -121,24 +122,20 @@ public final class CharSequenceTransformation extends InternalTransformation imp
          *
          * @param id id of the screen field.
          * @param regex regular expression with groups (delimited by {@code (} and {@code (}) that
-         * are used to substitute parts of the value. The pattern will be {@link Pattern#compile
-         * compiled} without setting any flags.
+         * are used to substitute parts of the value.
          * @param subst the string that substitutes the matched regex, using {@code $} for
          * group substitution ({@code $1} for 1st group match, {@code $2} for 2nd, etc).
          *
          * @return this builder.
          */
-        public Builder addField(@NonNull AutofillId id, @NonNull String regex,
+        public Builder addField(@NonNull AutofillId id, @NonNull Pattern regex,
                 @NonNull String subst) {
             throwIfDestroyed();
             Preconditions.checkNotNull(id);
             Preconditions.checkNotNull(regex);
             Preconditions.checkNotNull(subst);
 
-            // Check if the regex is valid
-            Pattern pattern = Pattern.compile(regex);
-
-            mFields.put(id, new Pair<>(pattern, subst));
+            mFields.put(id, new Pair<>(regex, subst));
             return this;
         }
 
@@ -178,17 +175,17 @@ public final class CharSequenceTransformation extends InternalTransformation imp
     public void writeToParcel(Parcel parcel, int flags) {
         final int size = mFields.size();
         final AutofillId[] ids = new AutofillId[size];
-        final String[] regexs = new String[size];
+        final Pattern[] regexs = new Pattern[size];
         final String[] substs = new String[size];
         Pair<Pattern, String> pair;
         for (int i = 0; i < size; i++) {
             ids[i] = mFields.keyAt(i);
             pair = mFields.valueAt(i);
-            regexs[i] = pair.first.pattern();
+            regexs[i] = pair.first;
             substs[i] = pair.second;
         }
         parcel.writeParcelableArray(ids, flags);
-        parcel.writeStringArray(regexs);
+        parcel.writeSerializable(regexs);
         parcel.writeStringArray(substs);
     }
 
@@ -197,7 +194,7 @@ public final class CharSequenceTransformation extends InternalTransformation imp
         @Override
         public CharSequenceTransformation createFromParcel(Parcel parcel) {
             final AutofillId[] ids = parcel.readParcelableArray(null, AutofillId.class);
-            final String[] regexs = parcel.createStringArray();
+            final Pattern[] regexs = (Pattern[]) parcel.readSerializable();
             final String[] substs = parcel.createStringArray();
 
             // Always go through the builder to ensure the data ingested by
index eebe02a..9eb52f6 100644 (file)
@@ -42,9 +42,10 @@ import java.util.regex.Pattern;
  * <p>Typically used to display credit card logos. Example:
  *
  * <pre class="prettyprint">
- *   new ImageTransformation.Builder(ccNumberId, "^4815.*$", R.drawable.ic_credit_card_logo1)
- *     .addOption("^1623.*$", R.drawable.ic_credit_card_logo2)
- *     .addOption("^42.*$", R.drawable.ic_credit_card_logo3)
+ *   new ImageTransformation.Builder(ccNumberId, Pattern.compile("^4815.*$"),
+ *                                   R.drawable.ic_credit_card_logo1)
+ *     .addOption(Pattern.compile("^1623.*$"), R.drawable.ic_credit_card_logo2)
+ *     .addOption(Pattern.compile("^42.*$"), R.drawable.ic_credit_card_logo3)
  *     .build();
  * </pre>
  *
@@ -105,12 +106,11 @@ 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. The
-         * pattern will be {@link Pattern#compile compiled} without setting any flags.
+         * @param regex regular expression defining what should be matched to use this image.
          * @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.
          */
-        public Builder(@NonNull AutofillId id, @NonNull String regex, @DrawableRes int resId) {
+        public Builder(@NonNull AutofillId id, @NonNull Pattern regex, @DrawableRes int resId) {
             mId = Preconditions.checkNotNull(id);
 
             addOption(regex, resId);
@@ -119,34 +119,27 @@ 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. The
-         * pattern will be {@link Pattern#compile compiled} without setting any flags.
+         * @param regex regular expression defining what should be matched to use this image.
          * @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.
          *
          * @return this build
          */
-        public Builder addOption(@NonNull String regex, @DrawableRes int resId) {
+        public Builder addOption(@NonNull Pattern regex, @DrawableRes int resId) {
             throwIfDestroyed();
 
+            Preconditions.checkNotNull(regex);
             Preconditions.checkArgument(resId != 0);
 
-            // Check regex
-            Pattern pattern = Pattern.compile(regex);
-
-            mOptions.add(new Pair<>(pattern, resId));
+            mOptions.add(new Pair<>(regex, resId));
             return this;
         }
 
         /**
          * Creates a new {@link ImageTransformation} instance.
-         *
-         * @throws IllegalStateException if no call to {@link #addOption(String, int)} was made.
          */
         public ImageTransformation build() {
             throwIfDestroyed();
-            Preconditions.checkState(mOptions != null && !mOptions.isEmpty(),
-                    "Must add at least one option");
             mDestroyed = true;
             return new ImageTransformation(this);
         }
@@ -178,14 +171,14 @@ public final class ImageTransformation extends InternalTransformation implements
         parcel.writeParcelable(mId, flags);
 
         final int size = mOptions.size();
-        final String[] regexs = new String[size];
+        final Pattern[] regexs = new Pattern[size];
         final int[] resIds = new int[size];
         for (int i = 0; i < size; i++) {
             Pair<Pattern, Integer> regex = mOptions.get(i);
-            regexs[i] = regex.first.pattern();
+            regexs[i] = regex.first;
             resIds[i] = regex.second;
         }
-        parcel.writeStringArray(regexs);
+        parcel.writeSerializable(regexs);
         parcel.writeIntArray(resIds);
     }
 
@@ -195,7 +188,7 @@ public final class ImageTransformation extends InternalTransformation implements
         public ImageTransformation createFromParcel(Parcel parcel) {
             final AutofillId id = parcel.readParcelable(null);
 
-            final String[] regexs = parcel.createStringArray();
+            final Pattern[] regexs = (Pattern[]) parcel.readSerializable();
             final int[] resIds = parcel.createIntArray();
 
             // Always go through the builder to ensure the data ingested by the system obeys the
index d56d700..ef8c52c 100644 (file)
@@ -47,12 +47,11 @@ public final class SimpleRegexValidator extends InternalValidator implements Val
      * @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.
+     * otherwise, it returns {@code false}.
       */
-    public SimpleRegexValidator(@NonNull AutofillId id, @NonNull String regex) {
+    public SimpleRegexValidator(@NonNull AutofillId id, @NonNull Pattern regex) {
         mId = Preconditions.checkNotNull(id);
-        mRegex = Pattern.compile(regex);
+        mRegex = Preconditions.checkNotNull(regex);
     }
 
     /** @hide */
@@ -91,14 +90,15 @@ public final class SimpleRegexValidator extends InternalValidator implements Val
     @Override
     public void writeToParcel(Parcel parcel, int flags) {
         parcel.writeParcelable(mId, flags);
-        parcel.writeString(mRegex.pattern());
+        parcel.writeSerializable(mRegex);
     }
 
     public static final Parcelable.Creator<SimpleRegexValidator> CREATOR =
             new Parcelable.Creator<SimpleRegexValidator>() {
         @Override
         public SimpleRegexValidator createFromParcel(Parcel parcel) {
-            return new SimpleRegexValidator(parcel.readParcelable(null), parcel.readString());
+            return new SimpleRegexValidator(parcel.readParcelable(null),
+                    (Pattern) parcel.readSerializable());
         }
 
         @Override