From a16565fbac59982f290838cedee83c2f3df99a7c Mon Sep 17 00:00:00 2001 From: lucychang Date: Sat, 11 Sep 2021 15:58:20 +0800 Subject: [PATCH] Add accessibility uniqueId API to platform Add AccessibilityNodeInfo#setUniqueId and AccessibilityNodeInfo#getUniqueId API. Bug: 189055741 Test: atest android.view.accessibility.cts.AccessibilityNodeInfoTest Change-Id: I94948fcdea0bf997e8d41229bef4001aba557018 --- core/api/current.txt | 2 ++ .../accessibility/AccessibilityNodeInfo.java | 31 +++++++++++++++++++ .../AccessibilityNodeInfoTest.java | 2 +- 3 files changed, 34 insertions(+), 1 deletion(-) diff --git a/core/api/current.txt b/core/api/current.txt index 78c4cdfda40ca..9c0c9cea14176 100644 --- a/core/api/current.txt +++ b/core/api/current.txt @@ -50887,6 +50887,7 @@ package android.view.accessibility { method @Nullable public android.view.accessibility.AccessibilityNodeInfo.TouchDelegateInfo getTouchDelegateInfo(); method public android.view.accessibility.AccessibilityNodeInfo getTraversalAfter(); method public android.view.accessibility.AccessibilityNodeInfo getTraversalBefore(); + method @Nullable public String getUniqueId(); method public String getViewIdResourceName(); method public android.view.accessibility.AccessibilityWindowInfo getWindow(); method public int getWindowId(); @@ -50981,6 +50982,7 @@ package android.view.accessibility { method public void setTraversalAfter(android.view.View, int); method public void setTraversalBefore(android.view.View); method public void setTraversalBefore(android.view.View, int); + method public void setUniqueId(@Nullable String); method public void setViewIdResourceName(String); method public void setVisibleToUser(boolean); method public void writeToParcel(android.os.Parcel, int); diff --git a/core/java/android/view/accessibility/AccessibilityNodeInfo.java b/core/java/android/view/accessibility/AccessibilityNodeInfo.java index 587a27074544c..e9dec12360cbe 100644 --- a/core/java/android/view/accessibility/AccessibilityNodeInfo.java +++ b/core/java/android/view/accessibility/AccessibilityNodeInfo.java @@ -806,6 +806,7 @@ public class AccessibilityNodeInfo implements Parcelable { private CharSequence mContentDescription; private CharSequence mTooltipText; private String mViewIdResourceName; + private String mUniqueId; private ArrayList mExtraDataKeys; @UnsupportedAppUsage @@ -3462,6 +3463,28 @@ public class AccessibilityNodeInfo implements Parcelable { return mSourceNodeId; } + /** + * Sets the unique id to act as a key to identify the node. If the node instance is replaced + * after refreshing the layout, calling this API to assign the same unique id to the new + * alike node can help accessibility service to identify it. + * + * @param uniqueId The unique id that is associated with a visible node on the screen + */ + public void setUniqueId(@Nullable String uniqueId) { + enforceNotSealed(); + mUniqueId = uniqueId; + } + + /** + * Gets the unique id of the node. + * + * @return The unique id + */ + @Nullable + public String getUniqueId() { + return mUniqueId; + } + /** * Sets the token and node id of the leashed parent. * @@ -3764,6 +3787,10 @@ public class AccessibilityNodeInfo implements Parcelable { nonDefaultFields |= bitAt(fieldIndex); } fieldIndex++; + if (!Objects.equals(mUniqueId, DEFAULT.mUniqueId)) { + nonDefaultFields |= bitAt(fieldIndex); + } + fieldIndex++; if (mTextSelectionStart != DEFAULT.mTextSelectionStart) { nonDefaultFields |= bitAt(fieldIndex); } @@ -3903,6 +3930,7 @@ public class AccessibilityNodeInfo implements Parcelable { if (isBitSet(nonDefaultFields, fieldIndex++)) parcel.writeCharSequence(mTooltipText); if (isBitSet(nonDefaultFields, fieldIndex++)) parcel.writeString(mViewIdResourceName); + if (isBitSet(nonDefaultFields, fieldIndex++)) parcel.writeString(mUniqueId); if (isBitSet(nonDefaultFields, fieldIndex++)) parcel.writeInt(mTextSelectionStart); if (isBitSet(nonDefaultFields, fieldIndex++)) parcel.writeInt(mTextSelectionEnd); @@ -3982,6 +4010,7 @@ public class AccessibilityNodeInfo implements Parcelable { mTraversalAfter = other.mTraversalAfter; mWindowId = other.mWindowId; mConnectionId = other.mConnectionId; + mUniqueId = other.mUniqueId; mBoundsInParent.set(other.mBoundsInParent); mBoundsInScreen.set(other.mBoundsInScreen); mPackageName = other.mPackageName; @@ -4154,6 +4183,7 @@ public class AccessibilityNodeInfo implements Parcelable { if (isBitSet(nonDefaultFields, fieldIndex++)) mPaneTitle = parcel.readCharSequence(); if (isBitSet(nonDefaultFields, fieldIndex++)) mTooltipText = parcel.readCharSequence(); if (isBitSet(nonDefaultFields, fieldIndex++)) mViewIdResourceName = parcel.readString(); + if (isBitSet(nonDefaultFields, fieldIndex++)) mUniqueId = parcel.readString(); if (isBitSet(nonDefaultFields, fieldIndex++)) mTextSelectionStart = parcel.readInt(); if (isBitSet(nonDefaultFields, fieldIndex++)) mTextSelectionEnd = parcel.readInt(); @@ -4480,6 +4510,7 @@ public class AccessibilityNodeInfo implements Parcelable { builder.append("; contentDescription: ").append(mContentDescription); builder.append("; tooltipText: ").append(mTooltipText); builder.append("; viewIdResName: ").append(mViewIdResourceName); + builder.append("; uniqueId: ").append(mUniqueId); builder.append("; checkable: ").append(isCheckable()); builder.append("; checked: ").append(isChecked()); diff --git a/core/tests/coretests/src/android/view/accessibility/AccessibilityNodeInfoTest.java b/core/tests/coretests/src/android/view/accessibility/AccessibilityNodeInfoTest.java index 79e7c50e79871..201883689546a 100644 --- a/core/tests/coretests/src/android/view/accessibility/AccessibilityNodeInfoTest.java +++ b/core/tests/coretests/src/android/view/accessibility/AccessibilityNodeInfoTest.java @@ -46,7 +46,7 @@ public class AccessibilityNodeInfoTest { // The number of fields tested in the corresponding CTS AccessibilityNodeInfoTest: // See fullyPopulateAccessibilityNodeInfo, assertEqualsAccessibilityNodeInfo, // and assertAccessibilityNodeInfoCleared in that class. - private static final int NUM_MARSHALLED_PROPERTIES = 39; + private static final int NUM_MARSHALLED_PROPERTIES = 40; /** * The number of properties that are purposely not marshalled