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

FillResponse.Builder

public static final class FillResponse.Builder
extends Object

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


Builder for FillResponse objects. You must to provide at least one dataset or set an authentication intent with a presentation view.

Summary

Public constructors

FillResponse.Builder()

Public methods

FillResponse.Builder addDataset(Dataset dataset)

Adds a new Dataset to this response.

FillResponse build()

Builds a new FillResponse instance.

FillResponse.Builder disableAutofill(long duration)

Disables autofill for the app or activity.

FillResponse.Builder setAuthentication(AutofillId[] ids, IntentSender authentication, RemoteViews presentation)

Triggers a custom UI before before autofilling the screen with any data set in this response.

FillResponse.Builder setClientState(Bundle clientState)

Sets a bundle with state that is passed to subsequent APIs that manipulate this response.

FillResponse.Builder setFieldClassificationIds(AutofillId... ids)

Sets which fields are used for field classification

Note: This method automatically adds the FillResponse.FLAG_TRACK_CONTEXT_COMMITED to the flags.

FillResponse.Builder setFlags(int flags)

Sets flags changing the response behavior.

FillResponse.Builder setFooter(RemoteViews footer)

Sets a footer to be shown as the last element in the list of datasets.

FillResponse.Builder setHeader(RemoteViews header)

Sets a header to be shown as the first element in the list of datasets.

FillResponse.Builder setIgnoredIds(AutofillId... ids)

Specifies views that should not trigger new AutofillService.onFillRequest(FillRequest, android.os.CancellationSignal, FillCallback) requests.

FillResponse.Builder setSaveInfo(SaveInfo saveInfo)

Sets the SaveInfo associated with this response.

Inherited methods

Public constructors

FillResponse.Builder

added in API level 26
public FillResponse.Builder ()

Public methods

addDataset

added in API level 26
public FillResponse.Builder addDataset (Dataset dataset)

Adds a new Dataset to this response.

Note: on Android Build.VERSION_CODES.O, the total number of datasets is limited by the Binder transaction size, so it's recommended to keep it small (in the range of 10-20 at most) and use pagination by adding a fake authenticated dataset at the end with a presentation string like "Next 10" that would return a new FillResponse with the next 10 datasets, and so on. This limitation was lifted on Android Build.VERSION_CODES.O_MR1, although the Binder transaction size can still be reached if each dataset itself is too big.

Parameters
dataset Dataset

This value may be null.

Returns
FillResponse.Builder This builder.

This value will never be null.

build

added in API level 26
public FillResponse build ()

Builds a new FillResponse instance.

Returns
FillResponse A built response.

Throws
IllegalStateException if any of the following conditions occur:
  1. build() was already called.
  2. No call was made to addDataset(Dataset), setAuthentication(AutofillId[], IntentSender, RemoteViews), setSaveInfo(SaveInfo), disableAutofill(long), setClientState(Bundle), or setFieldClassificationIds(AutofillId).
  3. setHeader(RemoteViews) or setFooter(RemoteViews) is called without any previous calls to addDataset(Dataset).

disableAutofill

added in API level 28
public FillResponse.Builder disableAutofill (long duration)

Disables autofill for the app or activity.

This method is useful to optimize performance in cases where the service knows it can not autofill an app—for example, when the service has a list of "blacklisted" apps such as office suites.

By default, it disables autofill for all activities in the app, unless the response is flagged with FillResponse.FLAG_DISABLE_ACTIVITY_ONLY.

Autofill for the app or activity is automatically re-enabled after any of the following conditions:

  1. duration milliseconds have passed.
  2. The autofill service for the user has changed.
  3. The device has rebooted.

Note: Activities that are running when autofill is re-enabled remain disabled for autofill until they finish and restart.

Parameters
duration long: duration to disable autofill, in milliseconds.

Returns
FillResponse.Builder this builder

Throws
IllegalArgumentException if duration is not a positive number.
IllegalStateException if either addDataset(Dataset), setAuthentication(AutofillId[], IntentSender, RemoteViews), setSaveInfo(SaveInfo), setClientState(Bundle), or setFieldClassificationIds(AutofillId) was already called.

setAuthentication

added in API level 26
public FillResponse.Builder setAuthentication (AutofillId[] ids, 
                IntentSender authentication, 
                RemoteViews presentation)

Triggers a custom UI before before autofilling the screen with any data set in this response.

Note: Although the name of this method suggests that it should be used just for authentication flow, it can be used for other advanced flows; see AutofillService for examples.

This is typically useful when a user interaction is required to unlock their data vault if you encrypt the data set labels and data set data. It is recommended to encrypt only the sensitive data and not the data set labels which would allow auth on the data set level leading to a better user experience. Note that if you use sensitive data as a label, for example an email address, then it should also be encrypted. The provided intent must be an Activity which implements your authentication flow. Also if you provide an auth intent you also need to specify the presentation view to be shown in the fill UI for the user to trigger your authentication flow.

When a user triggers autofill, the system launches the provided intent whose extras will have the screen content and your client state. Once you complete your authentication flow you should set the Activity result to Activity.RESULT_OK and set the AutofillManager.EXTRA_AUTHENTICATION_RESULT extra with the fully populated response (or null if the screen cannot be autofilled).

For example, if you provided an empty response because the user's data was locked and marked that the response needs an authentication then in the response returned if authentication succeeds you need to provide all available data sets some of which may need to be further authenticated, for example a credit card whose CVV needs to be entered.

If you provide an authentication intent you must also provide a presentation which is used to visualize visualize the response for triggering the authentication flow.

Note: Do not make the provided pending intent immutable by using PendingIntent.FLAG_IMMUTABLE as the platform needs to fill in the authentication arguments.

Theme does not work with RemoteViews layout. Avoid hardcoded text color or background color: Autofill on different platforms may have different themes.

Parameters
ids AutofillId: id of Views that when focused will display the authentication UI.

This value must never be null.

authentication IntentSender: Intent to an activity with your authentication flow.

This value may be null.

presentation RemoteViews: The presentation to visualize the response.

This value may be null.

Returns
FillResponse.Builder This builder.

This value will never be null.

Throws
IllegalArgumentException if any of the following occurs:
  • ids is null
  • ids is empty
  • ids contains a null element
  • both authentication and presentation are null
  • both authentication and presentation are non-null
IllegalStateException if a header or a footer are already set for this builder.

setClientState

added in API level 26
public FillResponse.Builder setClientState (Bundle clientState)

Sets a bundle with state that is passed to subsequent APIs that manipulate this response.

You can use this bundle to store intermediate state that is passed to subsequent calls to AutofillService.onFillRequest(FillRequest, android.os.CancellationSignal, FillCallback) and AutofillService.onSaveRequest(SaveRequest, SaveCallback), and you can also retrieve it by calling FillEventHistory.Event.getClientState().

If this method is called on multiple FillResponse objects for the same screen, just the latest bundle is passed back to the service.

Parameters
clientState Bundle: The custom client state.

This value may be null.

Returns
FillResponse.Builder This builder.

setFieldClassificationIds

added in API level 28
public FillResponse.Builder setFieldClassificationIds (AutofillId... ids)

Sets which fields are used for field classification

Note: This method automatically adds the FillResponse.FLAG_TRACK_CONTEXT_COMMITED to the flags.

Parameters
ids AutofillId

This value must never be null.

Returns
FillResponse.Builder

Throws
IllegalArgumentException is length of ids args is more than UserData.getMaxFieldClassificationIdsSize().
IllegalStateException if build() or disableAutofill(long) was already called.
NullPointerException if ids or any element on it is null.

setFlags

added in API level 28
public FillResponse.Builder setFlags (int flags)

Sets flags changing the response behavior.

Parameters
flags int: a combination of FillResponse.FLAG_TRACK_CONTEXT_COMMITED and FillResponse.FLAG_DISABLE_ACTIVITY_ONLY, or 0.

Value is either 0 or combination of FLAG_TRACK_CONTEXT_COMMITED or FLAG_DISABLE_ACTIVITY_ONLY.

Returns
FillResponse.Builder This builder.

setFooter

added in API level 28
public FillResponse.Builder setFooter (RemoteViews footer)

Sets a footer to be shown as the last element in the list of datasets.

When this method is called, you must also add a dataset, otherwise build() throws an IllegalStateException. Similarly, this method should only be used on FillResponses that do not require authentication (as the footer could have been set directly in the main presentation in these cases).

Theme does not work with RemoteViews layout. Avoid hardcoded text color or background color: Autofill on different platforms may have different themes.

Parameters
footer RemoteViews: a presentation to represent the footer. This presentation is not clickable —calling RemoteViews.setOnClickPendingIntent(int, android.app.PendingIntent) on it would have no effect.

This value must never be null.

Returns
FillResponse.Builder this builder

Throws
IllegalStateException if the FillResponse requires authentication.

setHeader

added in API level 28
public FillResponse.Builder setHeader (RemoteViews header)

Sets a header to be shown as the first element in the list of datasets.

When this method is called, you must also add a dataset, otherwise build() throws an IllegalStateException. Similarly, this method should only be used on FillResponses that do not require authentication (as the header could have been set directly in the main presentation in these cases).

Theme does not work with RemoteViews layout. Avoid hardcoded text color or background color: Autofill on different platforms may have different themes.

Parameters
header RemoteViews: a presentation to represent the header. This presentation is not clickable —calling RemoteViews.setOnClickPendingIntent(int, android.app.PendingIntent) on it would have no effect.

This value must never be null.

Returns
FillResponse.Builder this builder

Throws
IllegalStateException if an authentication was already set for this builder.

setIgnoredIds

added in API level 26
public FillResponse.Builder setIgnoredIds (AutofillId... ids)

Specifies views that should not trigger new AutofillService.onFillRequest(FillRequest, android.os.CancellationSignal, FillCallback) requests.

This is typically used when the service cannot autofill the view; for example, a text field representing the result of a Captcha challenge.

Parameters
ids AutofillId

Returns
FillResponse.Builder

setSaveInfo

added in API level 26
public FillResponse.Builder setSaveInfo (SaveInfo saveInfo)

Sets the SaveInfo associated with this response.

Parameters
saveInfo SaveInfo

This value must never be null.

Returns
FillResponse.Builder This builder.

This value will never be null.