ContentActionSelectedData


@ExperimentalCustomContentAction
public final class ContentActionSelectedData


Represents the data passed by a browser when a custom content action is triggered from a Custom Tab. When you add custom content actions using addCustomContentAction, the browser sends the android.app.PendingIntent associated with the selected action when the user triggers it. The browser includes specific extras in the Intent carried by the android.app.PendingIntent. This class provides a convenient way to access these extras, such as the current URL, the type of content interacted with, the ID of the triggered action, and potentially details about the specific element (like an image or link).

Summary

Public methods

static @Nullable ContentActionSelectedData

Creates a ContentActionSelectedData instance from the given Intent.

int

Gets the specific type of content that was interacted with (e.g., via long-press) to trigger the custom content action.

@Nullable String

Gets the alt text of the clicked element (e.g., an image or link), if available.

@Nullable Uri

Gets a Uri pointing to the data of the clicked image, if the interacted element was an image and the browser supports providing this information.

@Nullable String

Gets the URL of the clicked image, if the interacted element was an image and the browser supports providing this information.

@NonNull Intent

Returns the underlying Intent object that this data was parsed from.

@Nullable String

Gets the visible text content of the clicked link, if the interacted element was a link and the browser provides this information.

@Nullable String

Gets the URL of the clicked link, if the interacted element was a link (i.e., getClickedContentTargetType returns CONTENT_TARGET_TYPE_LINK) and the browser supports providing this information.

@NonNull Uri

Gets the URL of the current web page in the Custom Tab when the action was triggered.

int

Gets the ID of the specific CustomContentAction that was triggered by the user.

Public methods

fromIntent

Added in 1.9.0-rc01
public static @Nullable ContentActionSelectedData fromIntent(@Nullable Intent intent)

Creates a ContentActionSelectedData instance from the given Intent.

Parameters
@Nullable Intent intent

The intent received from the browser's custom content action. This intent is expected to contain the relevant extras.

Returns
@Nullable ContentActionSelectedData

A ContentActionSelectedData instance, or null if the provided intent is null.

getClickedContentTargetType

Added in 1.9.0-rc01
public int getClickedContentTargetType()

Gets the specific type of content that was interacted with (e.g., via long-press) to trigger the custom content action.

Returns
int

The CustomTabsIntent.ContentTargetType integer value (e.g., CONTENT_TARGET_TYPE_IMAGE, CONTENT_TARGET_TYPE_LINK). Returns 0 if the extra is not present, which is not a valid target type and indicates the data was not sent or is malformed.

getImageAltText

Added in 1.9.0-rc01
public @Nullable String getImageAltText()

Gets the alt text of the clicked element (e.g., an image or link), if available.

Note: The browser determines the length of the returned text, so it may be truncated. As such, the returned value is not guaranteed to be the complete, original text.

Returns
@Nullable String

The alt text as a String, or null if not present or not applicable.

getImageDataUri

Added in 1.9.0-rc01
public @Nullable Uri getImageDataUri()

Gets a Uri pointing to the data of the clicked image, if the interacted element was an image and the browser supports providing this information.

Note: The returned Uri points to a temporary file managed by the browser. Its contents should be copied to your app's persistent storage immediately if you need to access it later, as the file may be deleted. Do not store the Uri itself for long-term use. Please refer to https://developer.android .com/training/secure-file-sharing/share-file#GrantPermissions for more information.

Returns
@Nullable Uri

A Uri for the image data, or null if not present or Android version is below Tiramisu.

getImageUrl

Added in 1.9.0-rc01
public @Nullable String getImageUrl()

Gets the URL of the clicked image, if the interacted element was an image and the browser supports providing this information.

Returns
@Nullable String

The image URL as a String, or null if not present or not applicable.

getIntent

Added in 1.9.0-rc01
public @NonNull Intent getIntent()

Returns the underlying Intent object that this data was parsed from. This can be useful for accessing any additional, non-standard extras that might have been included by the browser.

Returns
@NonNull Intent

The original Intent.

getLinkText

Added in 1.9.0-rc01
public @Nullable String getLinkText()

Gets the visible text content of the clicked link, if the interacted element was a link and the browser provides this information.

Returns
@Nullable String

The link text as a String, or null if not present or not applicable.

getLinkUrl

Added in 1.9.0-rc01
public @Nullable String getLinkUrl()

Gets the URL of the clicked link, if the interacted element was a link (i.e., getClickedContentTargetType returns CONTENT_TARGET_TYPE_LINK) and the browser supports providing this information.

Returns
@Nullable String

The link URL as a String, or null if not present or not applicable.

getPageUrl

Added in 1.9.0-rc01
public @NonNull Uri getPageUrl()

Gets the URL of the current web page in the Custom Tab when the action was triggered. This is retrieved from getData.

Returns
@NonNull Uri

The current URL as a Uri.

getTriggeredActionId

Added in 1.9.0-rc01
public int getTriggeredActionId()

Gets the ID of the specific CustomContentAction that was triggered by the user.

Returns
int

The integer ID of the triggered action. Returns -1 if the extra is not present, indicating an issue or that the ID was not properly sent.