diff options
author | Adam He <adamhe@google.com> | 2019-07-19 11:19:56 -0700 |
---|---|---|
committer | Adam He <adamhe@google.com> | 2019-07-19 15:33:51 -0700 |
commit | d55b16e73e16a1a4d154c40eb8d30dd489f71b6e (patch) | |
tree | 057e11296ee3c1089f97d5f8833d1c95d4a8272d | |
parent | 63bc5be960e2499366ec1084f8110a0d3164fcbb (diff) |
Implement AssistStructure.ViewNode.getHintIdEntry().
Bug: 135134274
Test: atest CtsAutoFillServiceTestCases
Test: atest android.contentcaptureservice.cts.unit.ViewNodeTest
Change-Id: I7154cf97a5236ad975bd774c29b30e055cfca8bd
-rw-r--r-- | api/current.txt | 2 | ||||
-rw-r--r-- | core/java/android/app/assist/AssistStructure.java | 52 | ||||
-rw-r--r-- | core/java/android/view/View.java | 2 | ||||
-rw-r--r-- | core/java/android/view/ViewStructure.java | 10 | ||||
-rw-r--r-- | core/java/android/view/contentcapture/ViewNode.java | 23 | ||||
-rw-r--r-- | core/java/android/widget/TextView.java | 14 |
6 files changed, 89 insertions, 14 deletions
diff --git a/api/current.txt b/api/current.txt index 0a8db4611043..71b1de034242 100644 --- a/api/current.txt +++ b/api/current.txt @@ -7165,6 +7165,7 @@ package android.app.assist { method public android.os.Bundle getExtras(); method public int getHeight(); method public String getHint(); + method @Nullable public String getHintIdEntry(); method @Nullable public android.view.ViewStructure.HtmlInfo getHtmlInfo(); method public int getId(); method public String getIdEntry(); @@ -51426,6 +51427,7 @@ package android.view { method public abstract void setFocusable(boolean); method public abstract void setFocused(boolean); method public abstract void setHint(CharSequence); + method public void setHintIdEntry(@NonNull String); method public abstract void setHtmlInfo(@NonNull android.view.ViewStructure.HtmlInfo); method public abstract void setId(int, String, String, String); method public void setImportantForAutofill(int); diff --git a/core/java/android/app/assist/AssistStructure.java b/core/java/android/app/assist/AssistStructure.java index 3fca3118c9a0..86e768db3c52 100644 --- a/core/java/android/app/assist/AssistStructure.java +++ b/core/java/android/app/assist/AssistStructure.java @@ -641,6 +641,7 @@ public class AssistStructure implements Parcelable { int mMaxEms = -1; int mMaxLength = -1; @Nullable String mTextIdEntry; + @Nullable String mHintIdEntry; @AutofillImportance int mImportantForAutofill; // POJO used to override some autofill-related values when the node is parcelized. @@ -688,18 +689,19 @@ public class AssistStructure implements Parcelable { static final int FLAGS_HAS_LOCALE_LIST = 0x00010000; static final int FLAGS_ALL_CONTROL = 0xfff00000; - static final int AUTOFILL_FLAGS_HAS_AUTOFILL_VIEW_ID = 0x001; - static final int AUTOFILL_FLAGS_HAS_AUTOFILL_VIRTUAL_VIEW_ID = 0x002; - static final int AUTOFILL_FLAGS_HAS_AUTOFILL_VALUE = 0x004; - static final int AUTOFILL_FLAGS_HAS_AUTOFILL_TYPE = 0x008; - static final int AUTOFILL_FLAGS_HAS_AUTOFILL_HINTS = 0x010; - static final int AUTOFILL_FLAGS_HAS_AUTOFILL_OPTIONS = 0x020; - static final int AUTOFILL_FLAGS_HAS_HTML_INFO = 0x040; - static final int AUTOFILL_FLAGS_HAS_TEXT_ID_ENTRY = 0x080; - static final int AUTOFILL_FLAGS_HAS_MIN_TEXT_EMS = 0x100; - static final int AUTOFILL_FLAGS_HAS_MAX_TEXT_EMS = 0x200; - static final int AUTOFILL_FLAGS_HAS_MAX_TEXT_LENGTH = 0x400; - static final int AUTOFILL_FLAGS_HAS_AUTOFILL_SESSION_ID = 0x800; + static final int AUTOFILL_FLAGS_HAS_AUTOFILL_VIEW_ID = 0x0001; + static final int AUTOFILL_FLAGS_HAS_AUTOFILL_VIRTUAL_VIEW_ID = 0x0002; + static final int AUTOFILL_FLAGS_HAS_AUTOFILL_VALUE = 0x0004; + static final int AUTOFILL_FLAGS_HAS_AUTOFILL_TYPE = 0x0008; + static final int AUTOFILL_FLAGS_HAS_AUTOFILL_HINTS = 0x0010; + static final int AUTOFILL_FLAGS_HAS_AUTOFILL_OPTIONS = 0x0020; + static final int AUTOFILL_FLAGS_HAS_HTML_INFO = 0x0040; + static final int AUTOFILL_FLAGS_HAS_TEXT_ID_ENTRY = 0x0080; + static final int AUTOFILL_FLAGS_HAS_MIN_TEXT_EMS = 0x0100; + static final int AUTOFILL_FLAGS_HAS_MAX_TEXT_EMS = 0x0200; + static final int AUTOFILL_FLAGS_HAS_MAX_TEXT_LENGTH = 0x0400; + static final int AUTOFILL_FLAGS_HAS_AUTOFILL_SESSION_ID = 0x0800; + static final int AUTOFILL_FLAGS_HAS_HINT_ID_ENTRY = 0x1000; int mFlags; int mAutofillFlags; @@ -786,6 +788,9 @@ public class AssistStructure implements Parcelable { if ((autofillFlags & AUTOFILL_FLAGS_HAS_TEXT_ID_ENTRY) != 0) { mTextIdEntry = preader.readString(); } + if ((autofillFlags & AUTOFILL_FLAGS_HAS_HINT_ID_ENTRY) != 0) { + mHintIdEntry = preader.readString(); + } } if ((flags&FLAGS_HAS_LARGE_COORDS) != 0) { mX = in.readInt(); @@ -934,6 +939,9 @@ public class AssistStructure implements Parcelable { if (mTextIdEntry != null) { autofillFlags |= AUTOFILL_FLAGS_HAS_TEXT_ID_ENTRY; } + if (mHintIdEntry != null) { + autofillFlags |= AUTOFILL_FLAGS_HAS_HINT_ID_ENTRY; + } pwriter.writeString(mClassName); @@ -1011,6 +1019,9 @@ public class AssistStructure implements Parcelable { if ((autofillFlags & AUTOFILL_FLAGS_HAS_TEXT_ID_ENTRY) != 0) { pwriter.writeString(mTextIdEntry); } + if ((autofillFlags & AUTOFILL_FLAGS_HAS_HINT_ID_ENTRY) != 0) { + pwriter.writeString(mHintIdEntry); + } } if ((flags&FLAGS_HAS_LARGE_COORDS) != 0) { out.writeInt(mX); @@ -1586,6 +1597,17 @@ public class AssistStructure implements Parcelable { } /** + * Gets the identifier used to set the hint associated with this view. + * + * <p>It's only relevant when the {@link AssistStructure} is used for autofill purposes, + * not for assist purposes. + */ + @Nullable + public String getHintIdEntry() { + return mHintIdEntry; + } + + /** * Return a Bundle containing optional vendor-specific extension information. */ public Bundle getExtras() { @@ -1853,6 +1875,11 @@ public class AssistStructure implements Parcelable { } @Override + public void setHintIdEntry(@NonNull String entryName) { + mNode.mHintIdEntry = Preconditions.checkNotNull(entryName); + } + + @Override public CharSequence getText() { return mNode.mText != null ? mNode.mText.mText : null; } @@ -2266,6 +2293,7 @@ public class AssistStructure implements Parcelable { String hint = node.getHint(); if (hint != null) { Log.i(TAG, prefix + " Hint: " + hint); + Log.i(TAG, prefix + " Resource id: " + node.getHintIdEntry()); } Bundle extras = node.getExtras(); if (extras != null) { diff --git a/core/java/android/view/View.java b/core/java/android/view/View.java index 69884dc62fb3..e8a9f22df9e5 100644 --- a/core/java/android/view/View.java +++ b/core/java/android/view/View.java @@ -8486,7 +8486,7 @@ public class View implements Drawable.Callback, KeyEvent.Callback, * </ul> * * <p>It's also recommended to set the following properties - the more properties the structure - * has, the higher the changes of an {@link android.service.autofill.AutofillService} properly + * has, the higher the chances of an {@link android.service.autofill.AutofillService} properly * using the structure: * * <ul> diff --git a/core/java/android/view/ViewStructure.java b/core/java/android/view/ViewStructure.java index 6efb6f38d118..5f3ce33db19d 100644 --- a/core/java/android/view/ViewStructure.java +++ b/core/java/android/view/ViewStructure.java @@ -228,6 +228,16 @@ public abstract class ViewStructure { public abstract void setHint(CharSequence hint); /** + * Sets the identifier used to set the hint associated with this view. + * + * <p>Should only be set when the node is used for autofill purposes - it will be ignored + * when used for Assist. + */ + public void setHintIdEntry(@NonNull String entryName) { + Preconditions.checkNotNull(entryName); + } + + /** * Retrieve the last {@link #setText(CharSequence)}. */ public abstract CharSequence getText(); diff --git a/core/java/android/view/contentcapture/ViewNode.java b/core/java/android/view/contentcapture/ViewNode.java index 8d6245458d62..0dd2587feed5 100644 --- a/core/java/android/view/contentcapture/ViewNode.java +++ b/core/java/android/view/contentcapture/ViewNode.java @@ -81,6 +81,7 @@ public final class ViewNode extends AssistStructure.ViewNode { private static final long FLAGS_HAS_AUTOFILL_VALUE = 1L << 32; private static final long FLAGS_HAS_AUTOFILL_HINTS = 1L << 33; private static final long FLAGS_HAS_AUTOFILL_OPTIONS = 1L << 34; + private static final long FLAGS_HAS_HINT_ID_ENTRY = 1L << 35; /** Flags used to optimize what's written to the parcel */ private long mFlags; @@ -108,6 +109,7 @@ public final class ViewNode extends AssistStructure.ViewNode { private int mMaxEms = -1; private int mMaxLength = -1; private String mTextIdEntry; + private String mHintIdEntry; private @View.AutofillType int mAutofillType = View.AUTOFILL_TYPE_NONE; private String[] mAutofillHints; private AutofillValue mAutofillValue; @@ -195,6 +197,9 @@ public final class ViewNode extends AssistStructure.ViewNode { if ((nodeFlags & FLAGS_HAS_AUTOFILL_OPTIONS) != 0) { mAutofillOptions = parcel.readCharSequenceArray(); } + if ((nodeFlags & FLAGS_HAS_HINT_ID_ENTRY) != 0) { + mHintIdEntry = parcel.readString(); + } } /** @@ -352,6 +357,11 @@ public final class ViewNode extends AssistStructure.ViewNode { } @Override + public String getHintIdEntry() { + return mHintIdEntry; + } + + @Override public int getTextSelectionStart() { return mText != null ? mText.mTextSelectionStart : -1; } @@ -512,6 +522,9 @@ public final class ViewNode extends AssistStructure.ViewNode { if (mAutofillOptions != null) { nodeFlags |= FLAGS_HAS_AUTOFILL_OPTIONS; } + if (mHintIdEntry != null) { + nodeFlags |= FLAGS_HAS_HINT_ID_ENTRY; + } parcel.writeLong(nodeFlags); if ((nodeFlags & FLAGS_HAS_AUTOFILL_ID) != 0) { @@ -585,6 +598,9 @@ public final class ViewNode extends AssistStructure.ViewNode { if ((nodeFlags & FLAGS_HAS_AUTOFILL_OPTIONS) != 0) { parcel.writeCharSequenceArray(mAutofillOptions); } + if ((nodeFlags & FLAGS_HAS_HINT_ID_ENTRY) != 0) { + parcel.writeString(mHintIdEntry); + } } /** @hide */ @@ -783,7 +799,7 @@ public final class ViewNode extends AssistStructure.ViewNode { } @Override - public void setTextIdEntry(String entryName) { + public void setTextIdEntry(@NonNull String entryName) { mNode.mTextIdEntry = Preconditions.checkNotNull(entryName); } @@ -793,6 +809,11 @@ public final class ViewNode extends AssistStructure.ViewNode { } @Override + public void setHintIdEntry(String entryName) { + mNode.mHintIdEntry = Preconditions.checkNotNull(entryName); + } + + @Override public CharSequence getText() { return mNode.getText(); } diff --git a/core/java/android/widget/TextView.java b/core/java/android/widget/TextView.java index a4844eaa612c..f997d6878a9c 100644 --- a/core/java/android/widget/TextView.java +++ b/core/java/android/widget/TextView.java @@ -918,6 +918,8 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener private boolean mTextSetFromXmlOrResourceId = false; // Resource id used to set the text. private @StringRes int mTextId = Resources.ID_NULL; + // Resource id used to set the hint. + private @StringRes int mHintId = Resources.ID_NULL; // // End of autofill-related attributes @@ -1210,6 +1212,7 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener break; case com.android.internal.R.styleable.TextView_hint: + mHintId = a.getResourceId(attr, Resources.ID_NULL); hint = a.getText(attr); break; @@ -6446,6 +6449,7 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener */ @android.view.RemotableViewMethod public final void setHint(@StringRes int resid) { + mHintId = resid; setHint(getContext().getResources().getText(resid)); } @@ -11598,6 +11602,16 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener structure.setMaxTextLength(maxLength); } } + if (mHintId != Resources.ID_NULL) { + try { + structure.setHintIdEntry(getResources().getResourceEntryName(mHintId)); + } catch (Resources.NotFoundException e) { + if (android.view.autofill.Helper.sVerbose) { + Log.v(LOG_TAG, "onProvideAutofillStructure(): cannot set name for hint id " + + mHintId + ": " + e.getMessage()); + } + } + } structure.setHint(getHint()); structure.setInputType(getInputType()); } |