From 1f5c147eb5959a7e4fd03b751679cb5e00984c9c Mon Sep 17 00:00:00 2001 From: TYM Tsai Date: Thu, 14 Apr 2022 15:53:32 +0800 Subject: [PATCH] Auto append password hint for TextView Some TextViews are made as password via input type but does not contain autofill password hint. This change auto append a hint for autofill to identify. Bug: 219844915 Test: atest android.autofillservice.cts.dialog.LoginActivityTest Change-Id: Ifd18c56ffe788a601821d178ace41413f9e0454e --- core/java/android/view/View.java | 11 +++++++++++ core/java/android/widget/TextView.java | 12 ++++++++++++ 2 files changed, 23 insertions(+) diff --git a/core/java/android/view/View.java b/core/java/android/view/View.java index e87f47bc35447..91fb16166ba1b 100644 --- a/core/java/android/view/View.java +++ b/core/java/android/view/View.java @@ -1280,6 +1280,17 @@ public class View implements Drawable.Callback, KeyEvent.Callback, */ public static final String AUTOFILL_HINT_CREDIT_CARD_EXPIRATION_DAY = "creditCardExpirationDay"; + /** + * A hint indicating that this view can be autofilled with a password. + * + * This is a heuristic-based hint that is meant to be used by UI Toolkit developers when a + * view is a password field but doesn't specify a + * {@value View#AUTOFILL_HINT_PASSWORD}. + * @hide + */ + // TODO(229765029): unhide this for UI toolkit + public static final String AUTOFILL_HINT_PASSWORD_AUTO = "passwordAuto"; + /** * Hints for the autofill services that describes the content of the view. */ diff --git a/core/java/android/widget/TextView.java b/core/java/android/widget/TextView.java index f9c964158e2e9..572d977401879 100644 --- a/core/java/android/widget/TextView.java +++ b/core/java/android/widget/TextView.java @@ -6974,6 +6974,18 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener mEditor.mInputType = type; } + @Override + public String[] getAutofillHints() { + String[] hints = super.getAutofillHints(); + if (isAnyPasswordInputType()) { + if (!ArrayUtils.contains(hints, AUTOFILL_HINT_PASSWORD_AUTO)) { + hints = ArrayUtils.appendElement(String.class, hints, + AUTOFILL_HINT_PASSWORD_AUTO); + } + } + return hints; + } + /** * @return {@code null} if the key listener should use pre-O (locale-independent). Otherwise * a {@code Locale} object that can be used to customize key various listeners.