CustomDescription.Builder

public static class CustomDescription.Builder
extends Object

java.lang.Object
   ↳ android.service.autofill.CustomDescription.Builder


Builder for CustomDescription objects.

Summary

Public constructors

CustomDescription.Builder(RemoteViews parentPresentation)

Default constructor.

Public methods

CustomDescription.Builder addChild(int id, Transformation transformation)

Adds a transformation to replace the value of a child view with the fields in the screen.

CustomDescription.Builder batchUpdate(Validator condition, BatchUpdates updates)

Updates the presentation template when a condition is satisfied by applying a series of remote view operations.

CustomDescription build()

Creates a new CustomDescription instance.

Inherited methods

Public constructors

CustomDescription.Builder

added in API level 27
CustomDescription.Builder (RemoteViews parentPresentation)

Default constructor.

Note: If any child view of presentation triggers a pending intent on click, such PendingIntent must follow the restrictions below, otherwise it might not be triggered or the autofill save UI might not be shown when its activity is finished:

  • It cannot be created with the FLAG_IMMUTABLE flag.
  • It must be a PendingIntent for an Activity.
  • The activity must call finish() when done.
  • The activity should not launch other activities.

Parameters
parentPresentation RemoteViews: template presentation with (optional) children views.

This value must never be null.

Throws
NullPointerException if parentPresentation is null (on Android P or higher).

Public methods

addChild

added in API level 27
CustomDescription.Builder addChild (int id, 
                Transformation transformation)

Adds a transformation to replace the value of a child view with the fields in the screen.

When multiple transformations are added for the same child view, they will be applied in the same order as added.

Parameters
id int: view id of the children view.

transformation Transformation: an implementation provided by the Android System.

This value must never be null.

Returns
CustomDescription.Builder this builder.

Throws
IllegalArgumentException if transformation is not a class provided by the Android System.

batchUpdate

CustomDescription.Builder batchUpdate (Validator condition, 
                BatchUpdates updates)

Updates the presentation template when a condition is satisfied by applying a series of remote view operations. This allows dynamic customization of the portion of the save UI that is controlled by the autofill service. Such dynamic customization is based on the content of target views.

The updates are applied in the sequence they are added, after the transformations are applied to the children views.

For example, to make children views visible when fields are not empty:

 RemoteViews template = new RemoteViews(pgkName, R.layout.my_full_template);

 Pattern notEmptyPattern = Pattern.compile(".+");
 Validator hasAddress = new RegexValidator(addressAutofillId, notEmptyPattern);
 Validator hasCcNumber = new RegexValidator(ccNumberAutofillId, notEmptyPattern);

 RemoteViews addressUpdates = new RemoteViews(pgkName, R.layout.my_full_template)
 addressUpdates.setViewVisibility(R.id.address, View.VISIBLE);

 // Make address visible
 BatchUpdates addressBatchUpdates = new BatchUpdates.Builder()
     .updateTemplate(addressUpdates)
     .build();

 RemoteViews ccUpdates = new RemoteViews(pgkName, R.layout.my_full_template)
 ccUpdates.setViewVisibility(R.id.cc_number, View.VISIBLE);

 // Mask credit card number (as .....LAST_4_DIGITS) and make it visible
 BatchUpdates ccBatchUpdates = new BatchUpdates.Builder()
     .updateTemplate(ccUpdates)
     .transformChild(R.id.templateCcNumber, new CharSequenceTransformation
                     .Builder(ccNumberId, Pattern.compile("^.*(\\d\\d\\d\\d)$"), "...$1")
                     .build())
     .build();

 CustomDescription customDescription = new CustomDescription.Builder(template)
     .batchUpdate(hasAddress, addressBatchUpdates)
     .batchUpdate(hasCcNumber, ccBatchUpdates)
     .build();
 

Another approach is to add a child first, then apply the transformations. Example:

 RemoteViews template = new RemoteViews(pgkName, R.layout.my_base_template);

 RemoteViews addressPresentation = new RemoteViews(pgkName, R.layout.address)
 RemoteViews addressUpdates = new RemoteViews(pgkName, R.layout.my_template)
 addressUpdates.addView(R.id.parentId, addressPresentation);
 BatchUpdates addressBatchUpdates = new BatchUpdates.Builder()
     .updateTemplate(addressUpdates)
     .build();

 RemoteViews ccPresentation = new RemoteViews(pgkName, R.layout.cc)
 RemoteViews ccUpdates = new RemoteViews(pgkName, R.layout.my_template)
 ccUpdates.addView(R.id.parentId, ccPresentation);
 BatchUpdates ccBatchUpdates = new BatchUpdates.Builder()
     .updateTemplate(ccUpdates)
     .transformChild(R.id.templateCcNumber, new CharSequenceTransformation
                     .Builder(ccNumberId, Pattern.compile("^.*(\\d\\d\\d\\d)$"), "...$1")
                     .build())
     .build();

 CustomDescription customDescription = new CustomDescription.Builder(template)
     .batchUpdate(hasAddress, addressBatchUpdates)
     .batchUpdate(hasCcNumber, ccBatchUpdates)
     .build();
 

Parameters
condition Validator: condition used to trigger the updates.

This value must never be null.

updates BatchUpdates: actions to be applied to the template presentation when the condition is satisfied.

This value must never be null.

Returns
CustomDescription.Builder this builder

Throws
IllegalArgumentException if condition is not a class provided by the Android System.

build

added in API level 27
CustomDescription build ()

Creates a new CustomDescription instance.

Returns
CustomDescription