ContentCaptureSession


public abstract class ContentCaptureSession
extends Object implements AutoCloseable

java.lang.Object
   ↳ android.view.contentcapture.ContentCaptureSession


Session used when notifying the Android system about events associated with views.

Summary

Constants

int VERSION_1

Content Capture Version 1.

int VERSION_2

Content Capture Version 2.

Public methods

void close()

Closes this resource, relinquishing any underlying resources.

final ContentCaptureSession createContentCaptureSession(ContentCaptureContext context)

Creates a new ContentCaptureSession.

final void destroy()

Destroys this session, flushing out all pending notifications to the service.

void flush()

Flushes an internal buffer of UI events and signals System Intelligence (SI) that a semantically meaningful state has been reached.

final ContentCaptureContext getContentCaptureContext()

Gets the ContentCaptureContext associated with the session.

final ContentCaptureSessionId getContentCaptureSessionId()

Gets the id used to identify this session.

abstract int getContentCaptureVersion()

Gets the Content Capture version enabled for this session.

AutofillId newAutofillId(AutofillId hostId, long virtualChildId)

Creates a new AutofillId for a virtual child, so it can be used to uniquely identify the children in the session.

final ViewStructure newViewStructure(View view)

Creates a ViewStructure for a "standard" view.

final ViewStructure newVirtualViewStructure(AutofillId parentId, long virtualId)

Creates a ViewStructure for a "virtual" view, so it can be passed to notifyViewAppeared(ViewStructure) by the view managing the virtual view hierarchy.

void notifyContentInteractionEvent(AutofillId autofillId)

Notifies the Intelligence Service that a view has been interacted.

abstract void notifyNodeAppearedOrChanged(AutofillId id, ContentCaptureNodeProperties properties)

Creates a new node or updates an existing one according to the changes in properties.

abstract void notifyNodeDisappeared(AutofillId id)

Removes a node from the active content capture hierarchy.

final void notifySessionPaused()

Notifies the Content Capture Service that a session has paused.

final void notifySessionResumed()

Notifies the Content Capture Service that a session has resumed.

final void notifyViewAppeared(ViewStructure node)

Notifies the Content Capture Service that a node has been added to the view structure.

final void notifyViewDisappeared(AutofillId id)

Notifies the Content Capture Service that a node has been removed from the view structure.

final void notifyViewInsetsChanged(Insets viewInsets)

Notifies the Intelligence Service that the insets of a view have changed.

final void notifyViewTextChanged(AutofillId id, CharSequence text)

Notifies the Intelligence Service that the value of a text node has been changed.

final void notifyViewsAppeared(List<ViewStructure> appearedNodes)

Notifies the Content Capture Service that a list of nodes has appeared in the view structure.

final void notifyViewsDisappeared(AutofillId hostId, long[] virtualIds)

Notifies the Content Capture Service that many nodes has been removed from a virtual view structure.

final void setContentCaptureContext(ContentCaptureContext context)

Sets the ContentCaptureContext associated with the session.

String toString()

Returns a string representation of the object.

Inherited methods

Constants

VERSION_1

Added in API level 10000
public static final int VERSION_1

Content Capture Version 1.

The first Content Capture version is an information rich, event based system that attempts to transfer the current view hierarchy from the application process to a system intelligence service through periodic event dumps, e.g. flush(). However, this system can also be resource heavy due to the large number of events it generates.

When this version is returned by getContentCaptureVersion(), developers should use Version 1 APIs to notify the system of view hierarchy changes. Calling Version 2 APIs when the session is configured for Version 1 will result in no-ops.

A list of functions that belong explicitly to Content Capture Version 1:

Constant Value: 1 (0x00000001)

VERSION_2

Added in API level 10000
public static final int VERSION_2

Content Capture Version 2.

The second and newer Content Capture takes a more focused and leaner approach to passing the view hierarchy from the application to a system intelligence service. The required information is mostly limited to text on screen and bounding boxes of these text nodes. This version of Content Capture is also significantly more efficient at resource usage and recommended to be the default one for various system intelligence services to use.

When this version is returned by getContentCaptureVersion(), developers should use Version 2 APIs to report node lifecycle and property updates. Calling Version 1 APIs when the session is configured for Version 2 will result in no-ops.

A list of functions that belong explicitly to Content Capture Version 2:

Constant Value: 2 (0x00000002)

Public methods

close

Added in API level 29
public void close ()

Closes this resource, relinquishing any underlying resources. This method is invoked automatically on objects managed by the try-with-resources statement.

createContentCaptureSession

Added in API level 29
public final ContentCaptureSession createContentCaptureSession (ContentCaptureContext context)

Creates a new ContentCaptureSession.

See View.setContentCaptureSession(ContentCaptureSession) for more info.

Parameters
context ContentCaptureContext: This value cannot be null.

Returns
ContentCaptureSession This value cannot be null.

destroy

Added in API level 29
public final void destroy ()

Destroys this session, flushing out all pending notifications to the service.

Once destroyed, any new notification will be dropped.

flush

Added in API level 36
public void flush ()

Flushes an internal buffer of UI events and signals System Intelligence (SI) that a semantically meaningful state has been reached. SI uses this signal to potentially rebuild the view hierarchy and understand the current state of the UI.

UI events are often batched together for performance reasons. A semantic batch represents a series of events that, when applied sequentially, result in a meaningful and complete UI state.

It is crucial to call flush() after completing a semantic batch to ensure SI can accurately reconstruct the view hierarchy.

Premature Flushing: Calling flush() within a semantic batch may lead to SI failing to rebuild the view hierarchy correctly. This could manifest as incorrect ordering of sibling nodes.

Delayed Flushing: While not immediately flushing after a semantic batch is generally safe, it's recommended to do so as soon as possible. In the worst-case scenario where a flush() is never called, SI will attempt to process the events after a short delay based on view appearance and disappearance events.

Note; This function belongs to Content Capture Version 1.

getContentCaptureContext

Added in API level 29
public final ContentCaptureContext getContentCaptureContext ()

Gets the ContentCaptureContext associated with the session.

Returns
ContentCaptureContext context set on constructor or by setContentCaptureContext(ContentCaptureContext), or null if never explicitly set.

getContentCaptureSessionId

Added in API level 29
public final ContentCaptureSessionId getContentCaptureSessionId ()

Gets the id used to identify this session.

Returns
ContentCaptureSessionId This value cannot be null.

getContentCaptureVersion

Added in API level 10000
public abstract int getContentCaptureVersion ()

Gets the Content Capture version enabled for this session.

ContentCaptureSession offers two versions: VERSION_1 (legacy event-based capture using ViewStructure) and VERSION_2 (leaner, buffer-backed capture focused on text and bounding boxes).

The returned version is decided by the system intelligence service that coordinates this ContentCaptureSession.

Returns
int the enabled version, either VERSION_1 or VERSION_2. Calls to Version 1 methods become no-ops if the returned version is not VERSION_1, and calls to Version 2 methods become no-ops if the returned version is not VERSION_2.
Value is one of the following:

See also:

newAutofillId

Added in API level 29
public AutofillId newAutofillId (AutofillId hostId, 
                long virtualChildId)

Creates a new AutofillId for a virtual child, so it can be used to uniquely identify the children in the session.

Parameters
hostId AutofillId: id of the non-virtual view hosting the virtual view hierarchy (it can be obtained by calling ViewStructure.getAutofillId()).
This value cannot be null.

virtualChildId long: id of the virtual child, relative to the parent.

Returns
AutofillId if for the virtual child.
This value cannot be null.

Throws
IllegalArgumentException if the parentId is a virtual child id.

newViewStructure

Added in API level 29
public final ViewStructure newViewStructure (View view)

Creates a ViewStructure for a "standard" view.

This method should be called after a visible view is laid out; the view then must populate the structure and pass it to notifyViewAppeared(ViewStructure). Note: views that manage a virtual structure under this view must populate just the node representing this view and return right away, then asynchronously report (not necessarily in the UI thread) when the children nodes appear, disappear or have their text changed by calling ContentCaptureSession.notifyViewAppeared(ViewStructure), ContentCaptureSession.notifyViewDisappeared(AutofillId), and ContentCaptureSession.notifyViewTextChanged(AutofillId,CharSequence) respectively. The structure for the a child must be created using ContentCaptureSession.newVirtualViewStructure(AutofillId,long), and the autofillId for a child can be obtained either through childStructure.getAutofillId() or ContentCaptureSession.newAutofillId(AutofillId,long).

When the virtual view hierarchy represents a web page, you should also:

Note: the following methods of the structure will be ignored:

Note: This function belongs to Content Capture Version 1.

Parameters
view View: This value cannot be null.

Returns
ViewStructure This value cannot be null.

newVirtualViewStructure

Added in API level 29
public final ViewStructure newVirtualViewStructure (AutofillId parentId, 
                long virtualId)

Creates a ViewStructure for a "virtual" view, so it can be passed to notifyViewAppeared(ViewStructure) by the view managing the virtual view hierarchy.

Note: This function belongs to Content Capture Version 1.

Parameters
parentId AutofillId: id of the virtual view parent (it can be obtained by calling ViewStructure.getAutofillId() on the parent).
This value cannot be null.

virtualId long: id of the virtual child, relative to the parent.

Returns
ViewStructure a new ViewStructure that can be used for Content Capture purposes.
This value cannot be null.

notifyContentInteractionEvent

Added in API level 37
public void notifyContentInteractionEvent (AutofillId autofillId)

Notifies the Intelligence Service that a view has been interacted.

The view must have appeared before sending the interaction event.

Parameters
autofillId AutofillId: id of the node.
This value cannot be null.

notifyNodeAppearedOrChanged

Added in API level 10000
public abstract void notifyNodeAppearedOrChanged (AutofillId id, 
                ContentCaptureNodeProperties properties)

Creates a new node or updates an existing one according to the changes in properties.

This is the primary method to construct and report the UI hierarchy in Content Capture Version 2. It unifies the reporting of both standard Views and virtual views (e.g., inside a Webview) into a single, cohesive API. Call this method when a node is attached, laid out, and ready to be drawn in the active UI hierarchy, or when its properties (like text or bounds) change.

For standard views, the id can be obtained via View.getAutofillId(). For virtual views, the id must be created using newAutofillId(AutofillId,long).

To minimize memory allocations, applications should reuse a single ContentCaptureNodeProperties instance, calling ContentCaptureNodeProperties.clear() before populating it for a new node.

If the node already exists, only the properties explicitly set in the ContentCaptureNodeProperties payload will be updated. The parent-child relationship is modified based on the parent ID configuration:

  • Setting parent ID to null: Guarantees that this node is treated as a root node with no parent.
  • Not setting parent ID at all: Leaves the existing parent unchanged. If the node is appearing for the first time, it defaults to a root node.

Note: This function belongs to Content Capture Version 2.

.
This method must be called on the thread that originally created this UI element. This is typically the main thread of your app.

Parameters
id AutofillId: The AutofillId of the node.
This value cannot be null.

properties ContentCaptureNodeProperties: The ContentCaptureNodeProperties holding the attributes for this node.
This value may be null.

notifyNodeDisappeared

Added in API level 10000
public abstract void notifyNodeDisappeared (AutofillId id)

Removes a node from the active content capture hierarchy.

This is the primary method to report the removal of UI components in Content Capture Version 2. It applies to both standard Views and virtual views, signaling that the node is no longer part of the visible hierarchy.

When a node is deleted:

  • Its internal resources (such as text and content description allocations) are freed.
  • Its children are orphaned and promoted to root nodes (their parent pointer is cleared). If you want to remove an entire subtree, you must recursively call this method for all descendant nodes in the subtree. Re-adding this node will not automatically relink previous children.

Note; This function belongs to Content Capture Version 2.

.
This method must be called on the thread that originally created this UI element. This is typically the main thread of your app.

Parameters
id AutofillId: The AutofillId of the node to delete.
This value cannot be null.

notifySessionPaused

Added in API level 30
public final void notifySessionPaused ()

Notifies the Content Capture Service that a session has paused.

notifySessionResumed

Added in API level 30
public final void notifySessionResumed ()

Notifies the Content Capture Service that a session has resumed.

notifyViewAppeared

Added in API level 29
public final void notifyViewAppeared (ViewStructure node)

Notifies the Content Capture Service that a node has been added to the view structure.

Typically called "manually" by views that handle their own virtual view hierarchy, or automatically by the Android System for views that return true on View.onProvideContentCaptureStructure(ViewStructure,int).

Consider use notifyViewsAppeared(List) which has a better performance when notifying a list of nodes has appeared.

Note; This function belongs to Content Capture Version 1.

Parameters
node ViewStructure: node that has been added.
This value cannot be null.

notifyViewDisappeared

Added in API level 29
public final void notifyViewDisappeared (AutofillId id)

Notifies the Content Capture Service that a node has been removed from the view structure.

Typically called "manually" by views that handle their own virtual view hierarchy, or automatically by the Android System for standard views.

Consider use notifyViewsDisappeared(AutofillId, long) which has a better performance when notifying a list of nodes has disappeared.

Note; This function belongs to Content Capture Version 1.

Parameters
id AutofillId: id of the node that has been removed.
This value cannot be null.

notifyViewInsetsChanged

Added in API level 30
public final void notifyViewInsetsChanged (Insets viewInsets)

Notifies the Intelligence Service that the insets of a view have changed.

Parameters
viewInsets Insets: This value cannot be null.

notifyViewTextChanged

Added in API level 29
public final void notifyViewTextChanged (AutofillId id, 
                CharSequence text)

Notifies the Intelligence Service that the value of a text node has been changed.

Note; This function belongs to Content Capture Version 1.

Parameters
id AutofillId: of the node.
This value cannot be null.

text CharSequence: new text.
This value may be null.

notifyViewsAppeared

Added in API level 34
public final void notifyViewsAppeared (List<ViewStructure> appearedNodes)

Notifies the Content Capture Service that a list of nodes has appeared in the view structure.

Typically called manually by views that handle their own virtual view hierarchy.

Note; This function belongs to Content Capture Version 1.

Parameters
appearedNodes List: nodes that have appeared. Each element represents a view node that has been added to the view structure. The order of the elements is important, which should be preserved as the attached order of when the node is attached to the virtual view hierarchy.
This value cannot be null.

notifyViewsDisappeared

Added in API level 29
public final void notifyViewsDisappeared (AutofillId hostId, 
                long[] virtualIds)

Notifies the Content Capture Service that many nodes has been removed from a virtual view structure.

Should only be called by views that handle their own virtual view hierarchy.

After UPSIDE_DOWN_CAKE, this method wraps the virtual children with a pair of view tree appearing and view tree appeared events.

Note; This function belongs to Content Capture Version 1.

Parameters
hostId AutofillId: id of the non-virtual view hosting the virtual view hierarchy (it can be obtained by calling ViewStructure.getAutofillId()).
This value cannot be null.

virtualIds long: ids of the virtual children.
This value cannot be null.

Throws
IllegalArgumentException if the hostId is an autofill id for a virtual view.
IllegalArgumentException if virtualIds is empty

setContentCaptureContext

Added in API level 29
public final void setContentCaptureContext (ContentCaptureContext context)

Sets the ContentCaptureContext associated with the session.

Typically used to change the context associated with the default session from an activity.

Parameters
context ContentCaptureContext: This value may be null.

toString

Added in API level 29
public String toString ()

Returns a string representation of the object.

Returns
String a string representation of the object.