diff options
4 files changed, 44 insertions, 0 deletions
diff --git a/api/current.txt b/api/current.txt index 759c581c50d8..ae84d93de0a9 100644 --- a/api/current.txt +++ b/api/current.txt @@ -52675,6 +52675,7 @@ package android.view.contentcapture { method public final void notifyViewAppeared(android.view.ViewStructure); method public final void notifyViewDisappeared(android.view.autofill.AutofillId); method public final void notifyViewTextChanged(android.view.autofill.AutofillId, java.lang.CharSequence, int); + method public final void notifyViewsDisappeared(android.view.autofill.AutofillId, int[]); field public static final int FLAG_USER_INPUT = 1; // 0x1 } diff --git a/core/java/android/view/contentcapture/ChildContentCaptureSession.java b/core/java/android/view/contentcapture/ChildContentCaptureSession.java index cb4c4b4b6899..0b39c3ada0b1 100644 --- a/core/java/android/view/contentcapture/ChildContentCaptureSession.java +++ b/core/java/android/view/contentcapture/ChildContentCaptureSession.java @@ -92,6 +92,7 @@ final class ChildContentCaptureSession extends ContentCaptureSession { int flags) { getMainCaptureSession().notifyViewTextChanged(mId, id, text, flags); } + @Override boolean isContentCaptureEnabled() { return getMainCaptureSession().isContentCaptureEnabled(); diff --git a/core/java/android/view/contentcapture/ContentCaptureSession.java b/core/java/android/view/contentcapture/ContentCaptureSession.java index a9e7b5e5de70..7fb1fdd2d651 100644 --- a/core/java/android/view/contentcapture/ContentCaptureSession.java +++ b/core/java/android/view/contentcapture/ContentCaptureSession.java @@ -31,6 +31,7 @@ import android.view.contentcapture.ViewNode.ViewStructureImpl; import com.android.internal.annotations.GuardedBy; import com.android.internal.annotations.VisibleForTesting; +import com.android.internal.util.ArrayUtils; import com.android.internal.util.Preconditions; import dalvik.system.CloseGuard; @@ -345,6 +346,31 @@ public abstract class ContentCaptureSession implements AutoCloseable { abstract void internalNotifyViewDisappeared(@NonNull AutofillId id); /** + * Notifies the Content Capture Service that many nodes has been removed from a virtual view + * structure. + * + * <p>Should only be called by views that handle their own virtual view hierarchy. + * + * @param hostId id of the view hosting the virtual hierarchy. + * @param virtualIds ids of the virtual children. + * + * @throws IllegalArgumentException if the {@code hostId} is an autofill id for a virtual view. + * @throws IllegalArgumentException if {@code virtualIds} is empty + */ + public final void notifyViewsDisappeared(@NonNull AutofillId hostId, + @NonNull int[] virtualIds) { + Preconditions.checkArgument(!hostId.isVirtual(), "parent cannot be virtual"); + Preconditions.checkArgument(!ArrayUtils.isEmpty(virtualIds), "virtual ids cannot be empty"); + if (!isContentCaptureEnabled()) return; + + // TODO(b/121050915): create a new VIEWS_DISAPPEARED event type, which could also be used + // to batch delete non-virtual views + for (int id : virtualIds) { + internalNotifyViewDisappeared(new AutofillId(hostId, id, getIdAsInt())); + } + } + + /** * Notifies the Intelligence Service that the value of a text node has been changed. * * @param id of the node. diff --git a/core/tests/coretests/src/android/view/contentcapture/ContentCaptureSessionTest.java b/core/tests/coretests/src/android/view/contentcapture/ContentCaptureSessionTest.java index 6ecb63abf586..05c0df7c9a5f 100644 --- a/core/tests/coretests/src/android/view/contentcapture/ContentCaptureSessionTest.java +++ b/core/tests/coretests/src/android/view/contentcapture/ContentCaptureSessionTest.java @@ -97,6 +97,22 @@ public class ContentCaptureSessionTest { assertThat(structure.getAutofillId()).isEqualTo(childId); } + @Test + public void testNotifyViewsDisappeared_invalid() { + // Null parent + assertThrows(NullPointerException.class, + () -> mSession1.notifyViewsDisappeared(null, new int[] {42})); + // Null child + assertThrows(IllegalArgumentException.class, + () -> mSession1.notifyViewsDisappeared(new AutofillId(42), null)); + // Empty child + assertThrows(IllegalArgumentException.class, + () -> mSession1.notifyViewsDisappeared(new AutofillId(42), new int[] {})); + // Virtual parent + assertThrows(IllegalArgumentException.class, + () -> mSession1.notifyViewsDisappeared(new AutofillId(42, 108), new int[] {666})); + } + // Cannot use @Spy because we need to pass the session id on constructor private class MyContentCaptureSession extends ContentCaptureSession { |