ContentCaptureManager
public
final
class
ContentCaptureManager
extends Object
| java.lang.Object | |
| ↳ | android.view.contentcapture.ContentCaptureManager |
Content capture is mechanism used to let apps notify the Android system of events associated with views.
Before using this manager, you should check if it's available. Example:
ContentCaptureManager mgr = context.getSystemService(ContentCaptureManager.class);
if (mgr != null && mgr.isContentCaptureEnabled()) {
// ...
}
To support content capture, you must notifiy the Android system of the following events:
- When a visible view is laid out, call
ContentCaptureSession#notifyViewAppeared(ViewStructure). - When a view becomes invisible or is removed from the view hierarchy, call
ContentCaptureSession#notifyViewDisappeared(android.view.autofill.AutofillId). - When the view represents text and the text value changed, call
ContentCaptureSession#notifyViewTextChanged(android.view.autofill.AutofillId, CharSequence).
You can get a blank content capture structure using
ContentCaptureSession#newViewStructure(View), then populate its relevant fields.
Here's an example of the relevant methods for an EditText-like view:
public class MyEditText extends View {
private void populateContentCaptureStructure(@NonNull ViewStructure structure) {
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() && isTextEditable()) {
ContentCaptureManager mgr = mContext.getSystemService(ContentCaptureManager.class);
if (cm != null && cm.isContentCaptureEnabled()) {
ContentCaptureSession session = getContentCaptureSession();
if (session != null) {
session.notifyViewTextChanged(getAutofillId(), getText());
}
}
}
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. The session could have a ContentCaptureContext to provide more contextual info
about it, such as the locus associated with the view hierarchy
(see 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:
protected void onCreate(Bundle savedInstanceState) {
// Initialize view structure
ContentCaptureSession session = rootView.getContentCaptureSession();
if (session != null) {
session.setContentCaptureContext(ContentCaptureContext.forLocusId("chat_UserA_UserB"));
}
}
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:
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...
}
Summary
Public methods | |
|---|---|
Set<ContentCaptureCondition>
|
getContentCaptureConditions()
Gets the list of conditions for when content capture should be allowed. |
ComponentName
|
getServiceComponentName()
Returns the component name of the system service that is consuming the captured events for the current user. |
boolean
|
isContentCaptureEnabled()
Checks whether content capture is enabled for this activity. |
void
|
removeData(DataRemovalRequest request)
Called by the app to remove content capture data associated with some context. |
void
|
setContentCaptureEnabled(boolean enabled)
Called by apps to explicitly enable or disable content capture. |
Inherited methods | |
|---|---|
Public methods
getContentCaptureConditions
public Set<ContentCaptureCondition> getContentCaptureConditions ()
Gets the list of conditions for when content capture should be allowed.
This method is typically used by web browsers so they don't generate unnecessary content capture events for some websites.
| Returns | |
|---|---|
Set<ContentCaptureCondition> |
list of conditions, or null if there isn't any restriction
(in which case content capture events should always be generated). If the list is empty,
then it should not generate any event at all. |
getServiceComponentName
public ComponentName getServiceComponentName ()
Returns the component name of the system service that is consuming the captured events for the current user.
| Returns | |
|---|---|
ComponentName |
This value may be null. |
isContentCaptureEnabled
public boolean isContentCaptureEnabled ()
Checks whether content capture is enabled for this activity.
| Returns | |
|---|---|
boolean |
|
removeData
public void removeData (DataRemovalRequest request)
Called by the app to remove content capture data associated with some context.
| Parameters | |
|---|---|
request |
DataRemovalRequest: object specifying what data should be removed.
This value must never be null. |
setContentCaptureEnabled
public void setContentCaptureEnabled (boolean enabled)
Called by apps to explicitly enable or disable content capture.
Note: this call is not persisted accross reboots, so apps should typically call
it on Activity.onCreate(android.os.Bundle, android.os.PersistableBundle).
| Parameters | |
|---|---|
enabled |
boolean |