OSDN Git Service

Don't show a custom description if any transformation fail.
authorFelipe Leme <felipeal@google.com>
Tue, 18 Jul 2017 15:58:50 +0000 (08:58 -0700)
committerFelipe Leme <felipeal@google.com>
Wed, 19 Jul 2017 00:40:45 +0000 (17:40 -0700)
That could result in an inconsistent, confusing UI. For example, if the
successfully description for a credit card was "Exp. date: 04/20", one with a
failure could be "Exp. date: /20" - it's better to not show a custom description
in these cases.

Test: CtsAutoFillServiceTestCases pass
Test: CustomDescriptionTest.failFirstFailAll
Test: CustomDescriptionTest.failSecondFailAll

Bug: 62534917

Change-Id: I6554e3470ead2f84d7ef8715192d863c01ab1190

api/test-current.txt
core/java/android/service/autofill/CharSequenceTransformation.java
core/java/android/service/autofill/CustomDescription.java
core/java/android/service/autofill/ImageTransformation.java
core/java/android/service/autofill/InternalTransformation.java

index da4f678..d95f5fc 100644 (file)
@@ -37119,7 +37119,7 @@ package android.service.autofill {
   }
 
   public final class CharSequenceTransformation implements android.os.Parcelable android.service.autofill.Transformation {
-    method public void apply(android.service.autofill.ValueFinder, android.widget.RemoteViews, int);
+    method public void apply(android.service.autofill.ValueFinder, android.widget.RemoteViews, int) throws java.lang.Exception;
     method public int describeContents();
     method public void writeToParcel(android.os.Parcel, int);
     field public static final android.os.Parcelable.Creator<android.service.autofill.CharSequenceTransformation> CREATOR;
@@ -37217,7 +37217,7 @@ package android.service.autofill {
   }
 
   public final class ImageTransformation implements android.os.Parcelable android.service.autofill.Transformation {
-    method public void apply(android.service.autofill.ValueFinder, android.widget.RemoteViews, int);
+    method public void apply(android.service.autofill.ValueFinder, android.widget.RemoteViews, int) throws java.lang.Exception;
     method public int describeContents();
     method public void writeToParcel(android.os.Parcel, int);
     field public static final android.os.Parcelable.Creator<android.service.autofill.ImageTransformation> CREATOR;
index 77555fe..dfb30b9 100644 (file)
@@ -70,13 +70,13 @@ public final class CharSequenceTransformation extends InternalTransformation imp
     @Override
     @TestApi
     public void apply(@NonNull ValueFinder finder, @NonNull RemoteViews parentTemplate,
-            int childViewId) {
+            int childViewId) throws Exception {
         final StringBuilder converted = new StringBuilder();
         final int size = mFields.size();
         if (sDebug) Log.d(TAG, size + " multiple fields on id " + childViewId);
         for (int i = 0; i < size; i++) {
             final AutofillId id = mFields.keyAt(i);
-            final Pair<Pattern, String> regex = mFields.valueAt(i);
+            final Pair<Pattern, String> field = mFields.valueAt(i);
             final String value = finder.findByAutofillId(id);
             if (value == null) {
                 Log.w(TAG, "No value for id " + id);
@@ -84,12 +84,13 @@ public final class CharSequenceTransformation extends InternalTransformation imp
             }
             try {
                 // replaceAll throws an exception if the subst is invalid
-                final String convertedValue = regex.first.matcher(value).replaceAll(regex.second);
+                final String convertedValue = field.first.matcher(value).replaceAll(field.second);
                 converted.append(convertedValue);
             } catch (Exception e) {
-                // Do not log full exception to avoid leaks
-                Log.w(TAG, "Cannot apply " + regex.first.pattern() + "->" + regex.second + " to "
+                // Do not log full exception to avoid PII leaking
+                Log.w(TAG, "Cannot apply " + field.first.pattern() + "->" + field.second + " to "
                         + "field with autofill id" + id + ": " + e.getClass());
+                throw e;
             }
         }
         parentTemplate.setCharSequence(childViewId, "setText", converted);
index e97c364..0edb154 100644 (file)
@@ -103,8 +103,10 @@ public final class CustomDescription implements Parcelable {
                 try {
                     transformation.apply(finder, mPresentation, id);
                 } catch (Exception e) {
-                    Log.e(TAG, "Could not apply transformation " + transformation + ". "
+                    // Do not log full exception to avoid PII leaking
+                    Log.e(TAG, "Could not apply transformation " + transformation + ": "
                             + e.getClass());
+                    return null;
                 }
             }
         }
index 9eb52f6..3627189 100644 (file)
@@ -70,7 +70,7 @@ public final class ImageTransformation extends InternalTransformation implements
     @TestApi
     @Override
     public void apply(@NonNull ValueFinder finder, @NonNull RemoteViews parentTemplate,
-            int childViewId) {
+            int childViewId) throws Exception {
         final String value = finder.findByAutofillId(mId);
         if (value == null) {
             Log.w(TAG, "No view for id " + mId);
@@ -83,14 +83,22 @@ public final class ImageTransformation extends InternalTransformation implements
         }
 
         for (int i = 0; i < size; i++) {
-            Pair<Pattern, Integer> regex = mOptions.get(i);
-            if (regex.first.matcher(value).matches()) {
-                Log.d(TAG, "Found match at " + i + ": " + regex);
-                parentTemplate.setImageViewResource(childViewId, regex.second);
-                return;
+            final Pair<Pattern, Integer> option = mOptions.get(i);
+            try {
+                if (option.first.matcher(value).matches()) {
+                    Log.d(TAG, "Found match at " + i + ": " + option);
+                    parentTemplate.setImageViewResource(childViewId, option.second);
+                    return;
+                }
+            } catch (Exception e) {
+                // Do not log full exception to avoid PII leaking
+                Log.w(TAG, "Error matching regex #" + i + "(" + option.first.pattern() + ") on id "
+                        + option.second + ": " + e.getClass());
+                throw e;
+
             }
         }
-        Log.w(TAG, "No match for " + value);
+        if (sDebug) Log.d(TAG, "No match for " + value);
     }
 
     /**
index e8ac14a..974397b 100644 (file)
@@ -38,5 +38,5 @@ abstract class InternalTransformation implements Transformation, Parcelable {
      * @hide
      */
     abstract void apply(@NonNull ValueFinder finder, @NonNull RemoteViews template,
-            int childViewId);
+            int childViewId) throws Exception;
 }