Added in API level 29

ContentCaptureSession


abstract class ContentCaptureSession : AutoCloseable
kotlin.Any
   ↳ android.view.contentcapture.ContentCaptureSession

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

Summary

Public methods
open Unit

Closes this resource, relinquishing any underlying resources.

ContentCaptureSession

Creates a new ContentCaptureSession.

Unit

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

open Unit

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

ContentCaptureContext?

Gets the ContentCaptureContext associated with the session.

ContentCaptureSessionId

Gets the id used to identify this session.

open AutofillId
newAutofillId(hostId: AutofillId, virtualChildId: Long)

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

ViewStructure

Creates a ViewStructure for a "standard" view.

ViewStructure
newVirtualViewStructure(parentId: AutofillId, virtualId: Long)

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

open Unit

Notifies the Intelligence Service that a view has been interacted.

Unit

Notifies the Content Capture Service that a session has paused.

Unit

Notifies the Content Capture Service that a session has resumed.

Unit

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

Unit

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

Unit

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

Unit

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

Unit

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

Unit

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

Unit

Sets the ContentCaptureContext associated with the session.

open String

Returns a string representation of the object.

Public methods

close

Added in API level 29
open fun close(): Unit

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

Exceptions
java.lang.Exception if this resource cannot be closed

createContentCaptureSession

Added in API level 29
fun createContentCaptureSession(context: ContentCaptureContext): ContentCaptureSession

Creates a new ContentCaptureSession.

See View.setContentCaptureSession(ContentCaptureSession) for more info.

Parameters
context ContentCaptureContext: This value cannot be null.
Return
ContentCaptureSession This value cannot be null.

destroy

Added in API level 29
fun destroy(): Unit

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
open fun flush(): Unit

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.

See Also

    getContentCaptureContext

    Added in API level 29
    fun getContentCaptureContext(): ContentCaptureContext?

    Gets the ContentCaptureContext associated with the session.

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

    getContentCaptureSessionId

    Added in API level 29
    fun getContentCaptureSessionId(): ContentCaptureSessionId

    Gets the id used to identify this session.

    Return
    ContentCaptureSessionId This value cannot be null.

    newAutofillId

    Added in API level 29
    open fun newAutofillId(
        hostId: AutofillId,
        virtualChildId: Long
    ): AutofillId

    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.
    Return
    AutofillId if for the virtual child.
    This value cannot be null.
    Exceptions
    java.lang.IllegalArgumentException if the parentId is a virtual child id.

    newViewStructure

    Added in API level 29
    fun newViewStructure(view: View): ViewStructure

    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.
    Return
    ViewStructure This value cannot be null.

    See Also

      newVirtualViewStructure

      Added in API level 29
      fun newVirtualViewStructure(
          parentId: AutofillId,
          virtualId: Long
      ): ViewStructure

      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.
      Return
      ViewStructure a new ViewStructure that can be used for Content Capture purposes.
      This value cannot be null.

      See Also

        notifyContentInteractionEvent

        Added in API level 37
        open fun notifyContentInteractionEvent(autofillId: AutofillId): Unit

        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.

        notifySessionPaused

        Added in API level 30
        fun notifySessionPaused(): Unit

        Notifies the Content Capture Service that a session has paused.

        notifySessionResumed

        Added in API level 30
        fun notifySessionResumed(): Unit

        Notifies the Content Capture Service that a session has resumed.

        notifyViewAppeared

        Added in API level 29
        fun notifyViewAppeared(node: ViewStructure): Unit

        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 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
        fun notifyViewDisappeared(id: AutofillId): Unit

        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 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.

        See Also

          notifyViewInsetsChanged

          Added in API level 30
          fun notifyViewInsetsChanged(viewInsets: Insets): Unit

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

          Note; This function belongs to Content Capture Version 1.

          Parameters
          viewInsets Insets: This value cannot be null.

          See Also

            notifyViewTextChanged

            Added in API level 29
            fun notifyViewTextChanged(
                id: AutofillId,
                text: CharSequence?
            ): Unit

            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.

            See Also

              notifyViewsAppeared

              Added in API level 34
              fun notifyViewsAppeared(appearedNodes: MutableList<ViewStructure!>): Unit

              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 MutableList<ViewStructure!>: 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.

              See Also

                notifyViewsDisappeared

                Added in API level 29
                fun notifyViewsDisappeared(
                    hostId: AutofillId,
                    virtualIds: LongArray
                ): Unit

                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 LongArray: ids of the virtual children.
                This value cannot be null.
                Exceptions
                java.lang.IllegalArgumentException if virtualIds is empty

                See Also

                  setContentCaptureContext

                  Added in API level 29
                  fun setContentCaptureContext(context: ContentCaptureContext?): Unit

                  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
                  open fun toString(): String

                  Returns a string representation of the object.

                  Return
                  String a string representation of the object.