lightbulb_outline Help shape the future of the Google Play Console, Android Studio, and Firebase. Start survey

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

SaveInfo.Builder(int type, AutofillId[] requiredIds)

Creates a new builder.

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

SaveInfo.Builder

added in API level 26
public SaveInfo.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_USERNAME, or SaveInfo.SAVE_DATA_TYPE_EMAIL_ADDRESS.

Value is either 0 or combination of SAVE_DATA_TYPE_GENERIC, SAVE_DATA_TYPE_PASSWORD, SAVE_DATA_TYPE_ADDRESS, SAVE_DATA_TYPE_CREDIT_CARD, SAVE_DATA_TYPE_USERNAME or SAVE_DATA_TYPE_EMAIL_ADDRESS.

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

See SaveInfo for more info.

This value must never be null.

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

SaveInfo.Builder

added in API level 27
public SaveInfo.Builder (int type)

Creates a new builder when no id is required.

When using this builder, caller must call setOptionalIds(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_USERNAME, or SaveInfo.SAVE_DATA_TYPE_EMAIL_ADDRESS.

See SaveInfo for more info.

Value is either 0 or combination of SAVE_DATA_TYPE_GENERIC, SAVE_DATA_TYPE_PASSWORD, SAVE_DATA_TYPE_ADDRESS, SAVE_DATA_TYPE_CREDIT_CARD, SAVE_DATA_TYPE_USERNAME or SAVE_DATA_TYPE_EMAIL_ADDRESS.

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 must never be null.

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

This value must never be null.

Returns
SaveInfo.Builder this builder.

This value will never 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.

Returns
SaveInfo

Throws
IllegalStateException if no required ids or optional ids were set

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 must never be null.

Returns
SaveInfo.Builder This Builder.

This value will never be null.

Throws
IllegalStateException if this call was made after calling setDescription(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 will never be null.

Throws
IllegalStateException if this call was made after calling setCustomDescription(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, or 0.

Value is either 0 or combination of FLAG_SAVE_ON_ALL_VIEWS_INVISIBLE or FLAG_DONT_SAVE_ON_FINISH.

Returns
SaveInfo.Builder This builder.

This value will never 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 NEGATIVE_BUTTON_STYLE_CANCEL or NEGATIVE_BUTTON_STYLE_REJECT.

listener IntentSender: The action listener.

This value may be null.

Returns
SaveInfo.Builder This builder.

This value will never 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 must never be null.

Returns
SaveInfo.Builder This builder.

This value will never be null.

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

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 must never be null.

Returns
SaveInfo.Builder

This value will never 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 must never be null.

Returns
SaveInfo.Builder this builder.

This value will never be null.

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