Share via


ContentCaptureManager Class

Definition

Provides additional ways for apps to integrate with the content capture subsystem.

[Android.Runtime.Register("android/view/contentcapture/ContentCaptureManager", ApiSince=29, DoNotGenerateAcw=true)]
public sealed class ContentCaptureManager : Java.Lang.Object
[<Android.Runtime.Register("android/view/contentcapture/ContentCaptureManager", ApiSince=29, DoNotGenerateAcw=true)>]
type ContentCaptureManager = class
    inherit Object
Inheritance
ContentCaptureManager
Attributes

Remarks

Provides additional ways for apps to integrate with the content capture subsystem.

Content capture provides real-time, continuous capture of application activity, display and events to an intelligence service that is provided by the Android system. The intelligence service then uses that info to mediate and speed user journey through different apps. For example, when the user receives a restaurant address in a chat app and switches to a map app to search for that restaurant, the intelligence service could offer an autofill dialog to let the user automatically select its address.

Content capture was designed with two major concerns in mind: privacy and performance.

<ul> <li><b>Privacy:</b> the intelligence service is a trusted component provided that is provided by the device manufacturer and that cannot be changed by the user (although the user can globaly disable content capture using the Android Settings app). This service can only use the data for in-device machine learning, which is enforced both by process isolation and CDD requirements. <li><b>Performance:</b> content capture is highly optimized to minimize its impact in the app jankiness and overall device system health. For example, its only enabled on apps (or even specific activities from an app) that were explicitly allowlisted by the intelligence service, and it buffers the events so they are sent in a batch to the service (see #isContentCaptureEnabled() for other cases when its disabled). </ul>

In fact, before using this manager, the app developer should check if it's available. Example:

<code>
             ContentCaptureManager mgr = context.getSystemService(ContentCaptureManager.class);
             if (mgr != null && mgr.isContentCaptureEnabled()) {
               // ...
             }
</code>

App developers usually don't need to explicitly interact with content capture, except when the app:

<ul> <li>Can define a contextual android.content.LocusId to identify unique state (such as a conversation between 2 chat users). <li>Can have multiple view hierarchies with different contextual meaning (for example, a browser app with multiple tabs, each representing a different URL). <li>Contains custom views (that extend View directly and are not provided by the standard Android SDK. <li>Contains views that provide their own virtual hierarchy (like a web browser that render the HTML elements using a Canvas). </ul>

The main integration point with content capture is the ContentCaptureSession. A "main" session is automatically created by the Android System when content capture is enabled for the activity and its used by the standard Android views to notify the content capture service of events such as views being added, views been removed, and text changed by user input. The session could have a ContentCaptureContext to provide more contextual info about it, such as the locus associated with the view hierarchy (see android.content.LocusId for more info about locus). By default, the main session doesn't have a ContentCaptureContext, but you can change it after its created. Example:

<code>
            protected void onCreate(Bundle savedInstanceState) {
              // Initialize view structure
              ContentCaptureSession session = rootView.getContentCaptureSession();
              if (session != null) {
                session.setContentCaptureContext(ContentCaptureContext.forLocusId("chat_UserA_UserB"));
              }
            }
</code>

If your activity contains view hierarchies with a different contextual meaning, you should created child sessions for each view hierarchy root. For example, if your activity is a browser, you could use the main session for the main URL being rendered, then child sessions for each IFRAME:

<code>
            ContentCaptureSession mMainSession;

            protected void onCreate(Bundle savedInstanceState) {
               // Initialize view structure...
               mMainSession = rootView.getContentCaptureSession();
               if (mMainSession != null) {
                 mMainSession.setContentCaptureContext(
                     ContentCaptureContext.forLocusId("https://example.com"));
               }
            }

            private void loadIFrame(View iframeRootView, String url) {
              if (mMainSession != null) {
                 ContentCaptureSession iFrameSession = mMainSession.newChild(
                     ContentCaptureContext.forLocusId(url));
                 }
                 iframeRootView.setContentCaptureSession(iFrameSession);
              }
              // Load iframe...
            }
</code>

If your activity has custom views (i.e., views that extend View directly and provide just one logical view, not a virtual tree hiearchy) and it provides content that's relevant for content capture (as of android.os.Build.VERSION_CODES#Q Android Q, the only relevant content is text), then your view implementation should:

<ul> <li>Set it as important for content capture. <li>Fill ViewStructure used for content capture. <li>Notify the ContentCaptureSession when the text is changed by user input. </ul>

Here's an example of the relevant methods for an EditText-like view:

<code>
            public class MyEditText extends View {

            public MyEditText(...) {
              if (getImportantForContentCapture() == IMPORTANT_FOR_CONTENT_CAPTURE_AUTO) {
                setImportantForContentCapture(IMPORTANT_FOR_CONTENT_CAPTURE_YES);
              }
            }

            public void onProvideContentCaptureStructure(@NonNull ViewStructure structure, int flags) {
              super.onProvideContentCaptureStructure(structure, flags);

              structure.setText(getText(), getSelectionStart(), getSelectionEnd());
              structure.setHint(getHint());
              structure.setInputType(getInputType());
              // set other properties like setTextIdEntry(), setTextLines(), setTextStyle(),
              // setMinTextEms(), setMaxTextEms(), setMaxTextLength()
            }

            private void onTextChanged() {
              if (isLaidOut() && isImportantForContentCapture() && isTextEditable()) {
                ContentCaptureManager mgr = mContext.getSystemService(ContentCaptureManager.class);
                if (cm != null && cm.isContentCaptureEnabled()) {
                   ContentCaptureSession session = getContentCaptureSession();
                   if (session != null) {
                     session.notifyViewTextChanged(getAutofillId(), getText());
                   }
              }
            }
</code>

If your view provides its own virtual hierarchy (for example, if it's a browser that draws the HTML using Canvas or native libraries in a different render process), then the view is also responsible to notify the session when the virtual elements appear and disappear - see View#onProvideContentCaptureStructure(ViewStructure, int) for more info.

Java documentation for android.view.contentcapture.ContentCaptureManager.

Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.

Fields

DataShareErrorConcurrentRequest
Obsolete.

Request has been rejected, because a concurrent data share sessions is in progress.

DataShareErrorTimeoutInterrupted
Obsolete.

Request has been interrupted because of data share session timeout.

DataShareErrorUnknown
Obsolete.

Error happened during the data sharing session.

Properties

Class

Returns the runtime class of this Object.

(Inherited from Object)
ContentCaptureConditions

Gets the list of conditions for when content capture should be allowed.

ContentCaptureEnabled

Checks whether content capture is enabled for this activity. -or- Called by apps to explicitly enable or disable content capture.

Handle

The handle to the underlying Android instance.

(Inherited from Object)
JniIdentityHashCode (Inherited from Object)
JniPeerMembers
PeerReference (Inherited from Object)
ServiceComponentName

Returns the component name of the system service that is consuming the captured events for the current user.

ThresholdClass

This API supports the Mono for Android infrastructure and is not intended to be used directly from your code.

(Inherited from Object)
ThresholdType

This API supports the Mono for Android infrastructure and is not intended to be used directly from your code.

(Inherited from Object)

Methods

Clone()

Creates and returns a copy of this object.

(Inherited from Object)
Dispose() (Inherited from Object)
Dispose(Boolean) (Inherited from Object)
Equals(Object)

Indicates whether some other object is "equal to" this one.

(Inherited from Object)
GetHashCode()

Returns a hash code value for the object.

(Inherited from Object)
JavaFinalize()

Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.

(Inherited from Object)
Notify()

Wakes up a single thread that is waiting on this object's monitor.

(Inherited from Object)
NotifyAll()

Wakes up all threads that are waiting on this object's monitor.

(Inherited from Object)
RemoveData(DataRemovalRequest)

Called by the app to request the content capture service to remove content capture data associated with some context.

SetHandle(IntPtr, JniHandleOwnership)

Sets the Handle property.

(Inherited from Object)
ShareData(DataShareRequest, IExecutor, IDataShareWriteAdapter)

Called by the app to request data sharing via writing to a file.

ToArray<T>() (Inherited from Object)
ToString()

Returns a string representation of the object.

(Inherited from Object)
UnregisterFromRuntime() (Inherited from Object)
Wait()

Causes the current thread to wait until it is awakened, typically by being <em>notified</em> or <em>interrupted</em>.

(Inherited from Object)
Wait(Int64, Int32)

Causes the current thread to wait until it is awakened, typically by being <em>notified</em> or <em>interrupted</em>, or until a certain amount of real time has elapsed.

(Inherited from Object)
Wait(Int64)

Causes the current thread to wait until it is awakened, typically by being <em>notified</em> or <em>interrupted</em>, or until a certain amount of real time has elapsed.

(Inherited from Object)

Explicit Interface Implementations

IJavaPeerable.Disposed() (Inherited from Object)
IJavaPeerable.DisposeUnlessReferenced() (Inherited from Object)
IJavaPeerable.Finalized() (Inherited from Object)
IJavaPeerable.JniManagedPeerState (Inherited from Object)
IJavaPeerable.SetJniIdentityHashCode(Int32) (Inherited from Object)
IJavaPeerable.SetJniManagedPeerState(JniManagedPeerStates) (Inherited from Object)
IJavaPeerable.SetPeerReference(JniObjectReference) (Inherited from Object)

Extension Methods

JavaCast<TResult>(IJavaObject)

Performs an Android runtime-checked type conversion.

JavaCast<TResult>(IJavaObject)
GetJniTypeName(IJavaPeerable)

Applies to