SaveInfo


public final class SaveInfo
extends Object implements Parcelable

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


Information used to indicate that an AutofillService is interested on saving the user-inputed data for future use, through a AutofillService#onSaveRequest(SaveRequest, SaveCallback) call.

A SaveInfo is always associated with a FillResponse, and it contains at least two pieces of information:

  1. The type(s) of user data (like password or credit card info) that would be saved.
  2. The minimum set of views (represented by their AutofillId) that need to be changed to trigger a save request.

Typically, the SaveInfo contains the same ids as the Dataset:

   new FillResponse.Builder()
       .addDataset(new Dataset.Builder()
           .setValue(id1, AutofillValue.forText("homer"), createPresentation("homer")) // username
           .setValue(id2, AutofillValue.forText("D'OH!"), createPresentation("password for homer")) // password
           .build())
       .setSaveInfo(new SaveInfo.Builder(
           SaveInfo.SAVE_DATA_TYPE_USERNAME | SaveInfo.SAVE_DATA_TYPE_PASSWORD,
           new AutofillId[] { id1, id2 }).build())
       .build();
 

The save type flags are used to display the appropriate strings in the autofill save UI. You can pass multiple values, but try to keep it short if possible. In the above example, just SaveInfo.SAVE_DATA_TYPE_PASSWORD would be enough.

There might be cases where the AutofillService knows how to fill the screen, but the user has no data for it. In that case, the FillResponse should contain just the SaveInfo, but no Datasets:

   new FillResponse.Builder()
       .setSaveInfo(new SaveInfo.Builder(SaveInfo.SAVE_DATA_TYPE_PASSWORD,
           new AutofillId[] { id1, id2 }).build())
       .build();
 

There might be cases where the user data in the AutofillService is enough to populate some fields but not all, and the service would still be interested on saving the other fields. In that case, the service could set the SaveInfo.Builder#setOptionalIds(AutofillId[]) as well:

   new FillResponse.Builder()
       .addDataset(new Dataset.Builder()
           .setValue(id1, AutofillValue.forText("742 Evergreen Terrace"),
               createPresentation("742 Evergreen Terrace")) // street
           .setValue(id2, AutofillValue.forText("Springfield"),
               createPresentation("Springfield")) // city
           .build())
       .setSaveInfo(new SaveInfo.Builder(SaveInfo.SAVE_DATA_TYPE_ADDRESS,
           new AutofillId[] { id1, id2 }) // street and  city
           .setOptionalIds(new AutofillId[] { id3, id4 }) // state and zipcode
           .build())
       .build();
 

Triggering a save request

The AutofillService#onSaveRequest(SaveRequest, SaveCallback) can be triggered after any of the following events:

But it is only triggered when all conditions below are met:

  • The SaveInfo associated with the FillResponse is not null neither has the FLAG_DELAY_SAVE flag.
  • The AutofillValues of all required views (as set by the requiredIds passed to the SaveInfo.Builder constructor are not empty.
  • The AutofillValue of at least one view (be it required or optional) has changed (i.e., it's neither the same value passed in a Dataset, nor the initial value presented in the view).
  • There is no Dataset in the last FillResponse that completely matches the screen state (i.e., all required and optional fields in the dataset have the same value as the fields in the screen).
  • The user explicitly tapped the autofill save UI asking to save data for autofill.

Customizing the autofill save UI

The service can also customize some aspects of the autofill save UI:

Summary

Nested classes

class SaveInfo.Builder

A builder for SaveInfo objects. 

Constants

int FLAG_DELAY_SAVE

Postpone the autofill save UI.

int FLAG_DONT_SAVE_ON_FINISH

By default, a save request is automatically triggered once the Activity finishes.

int FLAG_SAVE_ON_ALL_VIEWS_INVISIBLE

Usually, a save request is only automatically triggered once the Activity finishes.

int NEGATIVE_BUTTON_STYLE_CANCEL

Style for the negative button of the save UI to cancel the save operation.

int NEGATIVE_BUTTON_STYLE_NEVER

Style for the negative button of the save UI to never do the save operation.

int NEGATIVE_BUTTON_STYLE_REJECT

Style for the negative button of the save UI to reject the save operation.

int POSITIVE_BUTTON_STYLE_CONTINUE

Style for the positive button of save UI to have next action before the save operation.

int POSITIVE_BUTTON_STYLE_SAVE

Style for the positive button of save UI to request the save operation.

int SAVE_DATA_TYPE_ADDRESS

Type used on when the FillResponse represents a physical address (such as street, city, state, etc).

int SAVE_DATA_TYPE_CREDIT_CARD

Type used when the FillResponse represents a credit card.

int SAVE_DATA_TYPE_DEBIT_CARD

Type used when the FillResponse represents a debit card.

int SAVE_DATA_TYPE_EMAIL_ADDRESS

Type used when the FillResponse represents just an email address, without a password.

int SAVE_DATA_TYPE_GENERIC

Type used when the service can save the contents of a screen, but cannot describe what the content is for.

int SAVE_DATA_TYPE_GENERIC_CARD

Type used when the FillResponse represents a card that does not a specified card or cannot identify what the card is for.

int SAVE_DATA_TYPE_PASSWORD

Type used when the FillResponse represents user credentials that have a password.

int SAVE_DATA_TYPE_PAYMENT_CARD

Type used when the FillResponse represents a payment card except for credit and debit cards.

int SAVE_DATA_TYPE_USERNAME

Type used when the FillResponse represents just an username, without a password.

Inherited constants

Fields

public static final Creator<SaveInfo> CREATOR

Public methods

int describeContents()

Describe the kinds of special objects contained in this Parcelable instance's marshaled representation.

String toString()

Returns a string representation of the object.

void writeToParcel(Parcel parcel, int flags)

Flatten this object in to a Parcel.

Inherited methods

Constants

FLAG_DELAY_SAVE

Added in API level 29
public static final int FLAG_DELAY_SAVE

Postpone the autofill save UI.

If flag is set, the autofill save UI is not triggered when the autofill context associated with the response associated with this SaveInfo is committed (with AutofillManager#commit()). Instead, the FillContext is delivered in future fill requests (with AutofillService.onFillRequest(android.service.autofill.FillRequest, android.os.CancellationSignal, android.service.autofill.FillCallback)) and save request (with AutofillService#onSaveRequest(SaveRequest, SaveCallback)) of an activity belonging to the same task.

This flag should be used when the service detects that the application uses multiple screens to implement an autofillable workflow (for example, one screen for the username field, another for password).

Constant Value: 4 (0x00000004)

FLAG_DONT_SAVE_ON_FINISH

Added in API level 28
public static final int FLAG_DONT_SAVE_ON_FINISH

By default, a save request is automatically triggered once the Activity finishes. If this flag is set, finishing the activity doesn't trigger a save request.

This flag is typically used in conjunction with Builder#setTriggerId(AutofillId).

Constant Value: 2 (0x00000002)

FLAG_SAVE_ON_ALL_VIEWS_INVISIBLE

Added in API level 26
public static final int FLAG_SAVE_ON_ALL_VIEWS_INVISIBLE

Usually, a save request is only automatically triggered once the Activity finishes. If this flag is set, it is triggered once all saved views become invisible.

Constant Value: 1 (0x00000001)

NEGATIVE_BUTTON_STYLE_CANCEL

Added in API level 26
public static final int NEGATIVE_BUTTON_STYLE_CANCEL

Style for the negative button of the save UI to cancel the save operation. In this case, the user tapping the negative button signals that they would prefer to not save the filled content.

Constant Value: 0 (0x00000000)

NEGATIVE_BUTTON_STYLE_NEVER

Added in API level 30
public static final int NEGATIVE_BUTTON_STYLE_NEVER

Style for the negative button of the save UI to never do the save operation. This means that the user does not need to save any data on this activity or application. Once the user tapping the negative button, the service should never trigger the save UI again. In addition to this, must consider providing restore options for the user.

Constant Value: 2 (0x00000002)

NEGATIVE_BUTTON_STYLE_REJECT

Added in API level 26
public static final int NEGATIVE_BUTTON_STYLE_REJECT

Style for the negative button of the save UI to reject the save operation. This could be useful if the user needs to opt-in your service and the save prompt is an advertisement of the potential value you can add to the user. In this case, the user tapping the negative button sends a strong signal that the feature may not be useful and you may consider some backoff strategy.

Constant Value: 1 (0x00000001)

POSITIVE_BUTTON_STYLE_CONTINUE

Added in API level 30
public static final int POSITIVE_BUTTON_STYLE_CONTINUE

Style for the positive button of save UI to have next action before the save operation. This could be useful if the filled content contains sensitive personally identifiable information and then requires user confirmation or verification. In this case, the user tapping the positive button signals that they would complete the next required action to save the filled content.

Constant Value: 1 (0x00000001)

POSITIVE_BUTTON_STYLE_SAVE

Added in API level 30
public static final int POSITIVE_BUTTON_STYLE_SAVE

Style for the positive button of save UI to request the save operation. In this case, the user tapping the positive button signals that they agrees to save the filled content.

Constant Value: 0 (0x00000000)

SAVE_DATA_TYPE_ADDRESS

Added in API level 26
public static final int SAVE_DATA_TYPE_ADDRESS

Type used on when the FillResponse represents a physical address (such as street, city, state, etc).

Constant Value: 2 (0x00000002)

SAVE_DATA_TYPE_CREDIT_CARD

Added in API level 26
public static final int SAVE_DATA_TYPE_CREDIT_CARD

Type used when the FillResponse represents a credit card.

Constant Value: 4 (0x00000004)

SAVE_DATA_TYPE_DEBIT_CARD

Added in API level 30
public static final int SAVE_DATA_TYPE_DEBIT_CARD

Type used when the FillResponse represents a debit card.

Constant Value: 32 (0x00000020)

SAVE_DATA_TYPE_EMAIL_ADDRESS

Added in API level 26
public static final int SAVE_DATA_TYPE_EMAIL_ADDRESS

Type used when the FillResponse represents just an email address, without a password.

Constant Value: 16 (0x00000010)

SAVE_DATA_TYPE_GENERIC

Added in API level 26
public static final int SAVE_DATA_TYPE_GENERIC

Type used when the service can save the contents of a screen, but cannot describe what the content is for.

Constant Value: 0 (0x00000000)

SAVE_DATA_TYPE_GENERIC_CARD

Added in API level 30
public static final int SAVE_DATA_TYPE_GENERIC_CARD

Type used when the FillResponse represents a card that does not a specified card or cannot identify what the card is for.

Constant Value: 128 (0x00000080)

SAVE_DATA_TYPE_PASSWORD

Added in API level 26
public static final int SAVE_DATA_TYPE_PASSWORD

Type used when the FillResponse represents user credentials that have a password.

Constant Value: 1 (0x00000001)

SAVE_DATA_TYPE_PAYMENT_CARD

Added in API level 30
public static final int SAVE_DATA_TYPE_PAYMENT_CARD

Type used when the FillResponse represents a payment card except for credit and debit cards.

Constant Value: 64 (0x00000040)

SAVE_DATA_TYPE_USERNAME

Added in API level 26
public static final int SAVE_DATA_TYPE_USERNAME

Type used when the FillResponse represents just an username, without a password.

Constant Value: 8 (0x00000008)

Fields

CREATOR

Added in API level 26
public static final Creator<SaveInfo> CREATOR

Public methods

describeContents

Added in API level 26
public int describeContents ()

Describe the kinds of special objects contained in this Parcelable instance's marshaled representation. For example, if the object will include a file descriptor in the output of writeToParcel(android.os.Parcel, int), the return value of this method must include the CONTENTS_FILE_DESCRIPTOR bit.

Returns
int a bitmask indicating the set of special object types marshaled by this Parcelable object instance. Value is either 0 or CONTENTS_FILE_DESCRIPTOR

toString

Added in API level 26
public String toString ()

Returns a string representation of the object.

Returns
String a string representation of the object.

writeToParcel

Added in API level 26
public void writeToParcel (Parcel parcel, 
                int flags)

Flatten this object in to a Parcel.

Parameters
parcel Parcel: The Parcel in which the object should be written. This value cannot be null.

flags int: Additional flags about how the object should be written. May be 0 or Parcelable.PARCELABLE_WRITE_RETURN_VALUE. Value is either 0 or a combination of Parcelable.PARCELABLE_WRITE_RETURN_VALUE, and android.os.Parcelable.PARCELABLE_ELIDE_DUPLICATES