SaveInfo.Builder


public static final class SaveInfo.Builder
extends Object

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


A builder for SaveInfo objects.

Summary

Public constructors

Builder(int type, AutofillId[] requiredIds)

Creates a new builder.

Builder(int type)

Creates a new builder when no id is required.

Public methods

SaveInfo.Builder addSanitizer(Sanitizer sanitizer, AutofillId... ids)

Adds a sanitizer for one or more field.

SaveInfo build()

Builds a new SaveInfo instance.

SaveInfo.Builder setCustomDescription(CustomDescription customDescription)

Sets a custom description to be shown in the UI when the user is asked to save.

SaveInfo.Builder setDescription(CharSequence description)

Sets an optional description to be shown in the UI when the user is asked to save.

SaveInfo.Builder setFlags(int flags)

Sets flags changing the save behavior.

SaveInfo.Builder setNegativeAction(int style, IntentSender listener)

Sets the style and listener for the negative save action.

SaveInfo.Builder setOptionalIds(AutofillId[] ids)

Sets the ids of additional, optional views the service would be interested to save.

SaveInfo.Builder setPositiveAction(int style)

Sets the style for the positive save action.

SaveInfo.Builder setTriggerId(AutofillId id)

Explicitly defines the view that should commit the autofill context when clicked.

SaveInfo.Builder setValidator(Validator validator)

Sets an object used to validate the user input - if the input is not valid, the autofill save UI is not shown.

Inherited methods

Public constructors

Builder

Added in API level 26
public Builder (int type, 
                AutofillId[] requiredIds)

Creates a new builder.

Parameters
type int: the type of information the associated FillResponse represents. It can be any combination of SaveInfo#SAVE_DATA_TYPE_GENERIC, SaveInfo#SAVE_DATA_TYPE_PASSWORD, SaveInfo#SAVE_DATA_TYPE_ADDRESS, SaveInfo#SAVE_DATA_TYPE_CREDIT_CARD, SaveInfo#SAVE_DATA_TYPE_DEBIT_CARD, SaveInfo#SAVE_DATA_TYPE_PAYMENT_CARD, SaveInfo#SAVE_DATA_TYPE_GENERIC_CARD, SaveInfo#SAVE_DATA_TYPE_USERNAME, or SaveInfo#SAVE_DATA_TYPE_EMAIL_ADDRESS. Value is either 0 or a combination of SaveInfo.SAVE_DATA_TYPE_GENERIC, SaveInfo.SAVE_DATA_TYPE_PASSWORD, SaveInfo.SAVE_DATA_TYPE_ADDRESS, SaveInfo.SAVE_DATA_TYPE_CREDIT_CARD, SaveInfo.SAVE_DATA_TYPE_USERNAME, SaveInfo.SAVE_DATA_TYPE_EMAIL_ADDRESS, SaveInfo.SAVE_DATA_TYPE_DEBIT_CARD, SaveInfo.SAVE_DATA_TYPE_PAYMENT_CARD, and SaveInfo.SAVE_DATA_TYPE_GENERIC_CARD

requiredIds AutofillId: ids of all required views that will trigger a save request.

See SaveInfo for more info. This value cannot be null.

Throws
IllegalArgumentException if requiredIds is null or empty, or if it contains any null entry.

Builder

Added in API level 27
public Builder (int type)

Creates a new builder when no id is required.

When using this builder, caller must call setOptionalIds(android.view.autofill.AutofillId[]) before calling build().

Parameters
type int: the type of information the associated FillResponse represents. It can be any combination of SaveInfo#SAVE_DATA_TYPE_GENERIC, SaveInfo#SAVE_DATA_TYPE_PASSWORD, SaveInfo#SAVE_DATA_TYPE_ADDRESS, SaveInfo#SAVE_DATA_TYPE_CREDIT_CARD, SaveInfo#SAVE_DATA_TYPE_DEBIT_CARD, SaveInfo#SAVE_DATA_TYPE_PAYMENT_CARD, SaveInfo#SAVE_DATA_TYPE_GENERIC_CARD, SaveInfo#SAVE_DATA_TYPE_USERNAME, or SaveInfo#SAVE_DATA_TYPE_EMAIL_ADDRESS.

See SaveInfo for more info. Value is either 0 or a combination of SaveInfo.SAVE_DATA_TYPE_GENERIC, SaveInfo.SAVE_DATA_TYPE_PASSWORD, SaveInfo.SAVE_DATA_TYPE_ADDRESS, SaveInfo.SAVE_DATA_TYPE_CREDIT_CARD, SaveInfo.SAVE_DATA_TYPE_USERNAME, SaveInfo.SAVE_DATA_TYPE_EMAIL_ADDRESS, SaveInfo.SAVE_DATA_TYPE_DEBIT_CARD, SaveInfo.SAVE_DATA_TYPE_PAYMENT_CARD, and SaveInfo.SAVE_DATA_TYPE_GENERIC_CARD

Public methods

addSanitizer

Added in API level 28
public SaveInfo.Builder addSanitizer (Sanitizer sanitizer, 
                AutofillId... ids)

Adds a sanitizer for one or more field.

When a sanitizer is set for a field, the AutofillValue is sent to the sanitizer before a save request is triggered.

Typically used to avoid displaying the save UI for values that are autofilled but reformattedby the app. For example, to remove spaces between every 4 digits of a credit card number:

 builder.addSanitizer(new TextValueSanitizer(
     Pattern.compile("^(\\d{4})\\s?(\\d{4})\\s?(\\d{4})\\s?(\\d{4})$", "$1$2$3$4")),
     ccNumberId);
 

The same sanitizer can be reused to sanitize multiple fields. For example, to trim both the username and password fields:

 builder.addSanitizer(
     new TextValueSanitizer(Pattern.compile("^\\s*(.*)\\s*$"), "$1"),
         usernameId, passwordId);
 

The sanitizer can also be used as an alternative for a validator. If any of the ids is a required id and the sanitizer fails because of it, then the save UI is not shown.

Parameters
sanitizer Sanitizer: an implementation provided by the Android System. This value cannot be null.

ids AutofillId: id of fields whose value will be sanitized. This value cannot be null.

Returns
SaveInfo.Builder this builder. This value cannot be null.

Throws
IllegalArgumentException if a sanitizer for any of the ids has already been added or if ids is empty.

build

Added in API level 26
public SaveInfo build ()

Builds a new SaveInfo instance. If no required ids, or optional ids, or SaveInfo.FLAG_DELAY_SAVE were set, Save Dialog will only be triggered if platform detection is enabled, which is indicated when FillRequest#getHints() is not empty.

Returns
SaveInfo

setCustomDescription

Added in API level 27
public SaveInfo.Builder setCustomDescription (CustomDescription customDescription)

Sets a custom description to be shown in the UI when the user is asked to save.

Typically used when the service must show more info about the object being saved, like a credit card logo, masked number, and expiration date.

Parameters
customDescription CustomDescription: the custom description. This value cannot be null.

Returns
SaveInfo.Builder This Builder. This value cannot be null.

Throws
IllegalStateException if this call was made after calling setDescription(java.lang.CharSequence).

setDescription

Added in API level 26
public SaveInfo.Builder setDescription (CharSequence description)

Sets an optional description to be shown in the UI when the user is asked to save.

Typically, it describes how the data will be stored by the service, so it can help users to decide whether they can trust the service to save their data.

Parameters
description CharSequence: a succint description. This value may be null.

Returns
SaveInfo.Builder This Builder. This value cannot be null.

Throws
IllegalStateException if this call was made after calling setCustomDescription(android.service.autofill.CustomDescription).

setFlags

Added in API level 26
public SaveInfo.Builder setFlags (int flags)

Sets flags changing the save behavior.

Parameters
flags int: SaveInfo.FLAG_SAVE_ON_ALL_VIEWS_INVISIBLE, SaveInfo.FLAG_DONT_SAVE_ON_FINISH, SaveInfo.FLAG_DELAY_SAVE, or 0. Value is either 0 or a combination of SaveInfo.FLAG_SAVE_ON_ALL_VIEWS_INVISIBLE, SaveInfo.FLAG_DONT_SAVE_ON_FINISH, and SaveInfo.FLAG_DELAY_SAVE

Returns
SaveInfo.Builder This builder. This value cannot be null.

setNegativeAction

Added in API level 26
public SaveInfo.Builder setNegativeAction (int style, 
                IntentSender listener)

Sets the style and listener for the negative save action.

This allows an autofill service to customize the style and be notified when the user selects the negative action in the save UI. Note that selecting the negative action regardless of its style and listener being customized would dismiss the save UI and if a custom listener intent is provided then this intent is started. The default style is SaveInfo.NEGATIVE_BUTTON_STYLE_CANCEL

Parameters
style int: The action style. Value is SaveInfo.NEGATIVE_BUTTON_STYLE_CANCEL, SaveInfo.NEGATIVE_BUTTON_STYLE_REJECT, or SaveInfo.NEGATIVE_BUTTON_STYLE_NEVER

listener IntentSender: The action listener. This value may be null.

Returns
SaveInfo.Builder This builder. This value cannot be null.

Throws
IllegalArgumentException If the style is invalid

setOptionalIds

Added in API level 26
public SaveInfo.Builder setOptionalIds (AutofillId[] ids)

Sets the ids of additional, optional views the service would be interested to save.

See SaveInfo for more info.

Parameters
ids AutofillId: The ids of the optional views. This value cannot be null.

Returns
SaveInfo.Builder This builder. This value cannot be null.

Throws
IllegalArgumentException if ids is null or empty, or if it contains any null entry.

setPositiveAction

Added in API level 30
public SaveInfo.Builder setPositiveAction (int style)

Sets the style for the positive save action.

This allows an autofill service to customize the style of the positive action in the save UI. Note that selecting the positive action regardless of its style would dismiss the save UI and calling into the save request. The service should take the next action if selecting style SaveInfo.POSITIVE_BUTTON_STYLE_CONTINUE. The default style is SaveInfo.POSITIVE_BUTTON_STYLE_SAVE

Parameters
style int: The action style. Value is SaveInfo.POSITIVE_BUTTON_STYLE_SAVE, or SaveInfo.POSITIVE_BUTTON_STYLE_CONTINUE

Returns
SaveInfo.Builder This builder. This value cannot be null.

Throws
IllegalArgumentException If the style is invalid

setTriggerId

Added in API level 28
public SaveInfo.Builder setTriggerId (AutofillId id)

Explicitly defines the view that should commit the autofill context when clicked.

Usually, the save request is only automatically triggered after the activity is finished or all relevant views become invisible, but there are scenarios where the autofill context is automatically commited too late —for example, when the activity manually clears the autofillable views when a button is tapped. This method can be used to trigger the autofill save UI earlier in these scenarios.

Note: This method should only be used in scenarios where the automatic workflow is not enough, otherwise it could trigger the autofill save UI when it should not— for example, when the user entered invalid credentials for the autofillable views.

Parameters
id AutofillId: This value cannot be null.

Returns
SaveInfo.Builder This value cannot be null.

setValidator

Added in API level 27
public SaveInfo.Builder setValidator (Validator validator)

Sets an object used to validate the user input - if the input is not valid, the autofill save UI is not shown.

Typically used to validate credit card numbers. Examples:

Validator for a credit number that must have exactly 16 digits:

 Validator validator = new RegexValidator(ccNumberId, Pattern.compile(""^\\d{16}$"))
 

Validator for a credit number that must pass a Luhn checksum and either have 16 digits, or 15 digits starting with 108:

 import static android.service.autofill.Validators.and;
 import static android.service.autofill.Validators.or;

 Validator validator =
   and(
     new LuhnChecksumValidator(ccNumberId),
     or(
       new RegexValidator(ccNumberId, Pattern.compile("^\\d{16}$")),
       new RegexValidator(ccNumberId, Pattern.compile("^108\\d{12}$"))
     )
   );
 

Note: the example above is just for illustrative purposes; the same validator could be created using a single regex for the OR part:

 Validator validator =
   and(
     new LuhnChecksumValidator(ccNumberId),
     new RegexValidator(ccNumberId, Pattern.compile(""^(\\d{16}|108\\d{12})$"))
   );
 

Validator for a credit number contained in just 4 fields and that must have exactly 4 digits on each field:

 import static android.service.autofill.Validators.and;

 Validator validator =
   and(
     new RegexValidator(ccNumberId1, Pattern.compile("^\\d{4}$")),
     new RegexValidator(ccNumberId2, Pattern.compile("^\\d{4}$")),
     new RegexValidator(ccNumberId3, Pattern.compile("^\\d{4}$")),
     new RegexValidator(ccNumberId4, Pattern.compile("^\\d{4}$"))
   );
 

Parameters
validator Validator: an implementation provided by the Android System. This value cannot be null.

Returns
SaveInfo.Builder this builder. This value cannot be null.

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