Added in API level 26

Builder


class Builder
kotlin.Any
   ↳ android.service.autofill.SaveInfo.Builder

A builder for SaveInfo objects.

Summary

Public constructors
Builder(type: Int, requiredIds: Array<AutofillId!>)

Creates a new builder.

Builder(type: Int)

Creates a new builder when no id is required.

Public methods
SaveInfo.Builder
addSanitizer(sanitizer: Sanitizer, vararg ids: AutofillId!)

Adds a sanitizer for one or more field.

SaveInfo!

Builds a new SaveInfo instance.

SaveInfo.Builder

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

SaveInfo.Builder

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

SaveInfo.Builder
setFlags(flags: Int)

Sets flags changing the save behavior.

SaveInfo.Builder
setNegativeAction(style: Int, listener: IntentSender?)

Sets the style and listener for the negative save action.

SaveInfo.Builder

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

SaveInfo.Builder

Sets the style for the positive save action.

SaveInfo.Builder

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.

Public constructors

Public methods

addSanitizer

Added in API level 28
fun addSanitizer(
    sanitizer: Sanitizer,
    vararg ids: AutofillId!
): SaveInfo.Builder

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.
Return
SaveInfo.Builder this builder. This value cannot be null.
Exceptions
java.lang.IllegalArgumentException if a sanitizer for any of the ids has already been added or if ids is empty.

build

Added in API level 26
fun build(): SaveInfo!

Builds a new SaveInfo instance. If no required ids, or optional ids, or 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.

setCustomDescription

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

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.
Return
SaveInfo.Builder This Builder. This value cannot be null.
Exceptions
java.lang.IllegalStateException if this call was made after calling setDescription(java.lang.CharSequence).

setDescription

Added in API level 26
fun setDescription(description: CharSequence?): SaveInfo.Builder

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.
Return
SaveInfo.Builder This Builder. This value cannot be null.
Exceptions
java.lang.IllegalStateException if this call was made after calling setCustomDescription(android.service.autofill.CustomDescription).

setFlags

Added in API level 26
fun setFlags(flags: Int): SaveInfo.Builder

Sets flags changing the save behavior.

Parameters
flags Int: FLAG_SAVE_ON_ALL_VIEWS_INVISIBLE, FLAG_DONT_SAVE_ON_FINISH, FLAG_DELAY_SAVE, or 0. Value is either 0 or a combination of android.service.autofill.SaveInfo#FLAG_SAVE_ON_ALL_VIEWS_INVISIBLE, android.service.autofill.SaveInfo#FLAG_DONT_SAVE_ON_FINISH, and android.service.autofill.SaveInfo#FLAG_DELAY_SAVE
Return
SaveInfo.Builder This builder. This value cannot be null.

setNegativeAction

Added in API level 26
fun setNegativeAction(
    style: Int,
    listener: IntentSender?
): SaveInfo.Builder

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 NEGATIVE_BUTTON_STYLE_CANCEL

Parameters
style Int: The action style. Value is android.service.autofill.SaveInfo#NEGATIVE_BUTTON_STYLE_CANCEL, android.service.autofill.SaveInfo#NEGATIVE_BUTTON_STYLE_REJECT, or android.service.autofill.SaveInfo#NEGATIVE_BUTTON_STYLE_NEVER
listener IntentSender?: The action listener. This value may be null.
Return
SaveInfo.Builder This builder. This value cannot be null.
Exceptions
java.lang.IllegalArgumentException If the style is invalid

setOptionalIds

Added in API level 26
fun setOptionalIds(ids: Array<AutofillId!>): SaveInfo.Builder

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

See SaveInfo for more info.

Parameters
ids Array<AutofillId!>: The ids of the optional views. This value cannot be null.
Return
SaveInfo.Builder This builder. This value cannot be null.
Exceptions
java.lang.IllegalArgumentException if ids is null or empty, or if it contains any null entry.

setPositiveAction

Added in API level 30
fun setPositiveAction(style: Int): SaveInfo.Builder

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 POSITIVE_BUTTON_STYLE_CONTINUE. The default style is POSITIVE_BUTTON_STYLE_SAVE

Parameters
style Int: The action style. Value is android.service.autofill.SaveInfo#POSITIVE_BUTTON_STYLE_SAVE, or android.service.autofill.SaveInfo#POSITIVE_BUTTON_STYLE_CONTINUE
Return
SaveInfo.Builder This builder. This value cannot be null.
Exceptions
java.lang.IllegalArgumentException If the style is invalid

setTriggerId

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

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.
Return
SaveInfo.Builder This value cannot be null.

setValidator

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

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.
Return
SaveInfo.Builder this builder. This value cannot be null.
Exceptions
java.lang.IllegalArgumentException if validator is not a class provided by the Android System.