CustomTabsSession

public final class CustomTabsSession


A class to be used for Custom Tabs related communication. Clients that want to launch Custom Tabs can use this class exclusively to handle all related communication.

Summary

Public methods

static @NonNull CustomTabsSession

Provides browsers a way to generate a mock CustomTabsSession for testing purposes.

boolean

Returns whether the Engagement Signals API is available.

boolean
mayLaunchUrl(
    @Nullable Uri url,
    @Nullable Bundle extras,
    @Nullable List<Bundle> otherLikelyBundles
)

Tells the browser of a likely future navigation to a URL.

int

Sends a postMessage request using the origin communicated via requestPostMessageChannel.

boolean
receiveFile(@NonNull Uri uri, int purpose, @Nullable Bundle extras)

Passes an URI of a file, e.g. in order to pass a large bitmap to be displayed in the Custom Tabs provider.

boolean

Sends a request to create a two way postMessage channel between the client and the browser.

boolean
requestPostMessageChannel(
    @NonNull Uri postMessageOrigin,
    @Nullable Uri postMessageTargetOrigin,
    @NonNull Bundle extras
)

Sends a request to create a two way postMessage channel between the client and the browser with specifying the target origin to communicate with.

boolean
setActionButton(@NonNull Bitmap icon, @NonNull String description)

This sets the action button on the toolbar with ID TOOLBAR_ACTION_BUTTON_ID.

boolean
@RequiresFeature(name = CustomTabsFeatures.ENGAGEMENT_SIGNALS, enforcement = "androidx.browser.customtabs.CustomTabsSession#isEngagementSignalsApiAvailable")
setEngagementSignalsCallback(
    @NonNull EngagementSignalsCallback callback,
    @NonNull Bundle extras
)

Sets an EngagementSignalsCallback to receive callbacks for events related to the user's engagement with webpage within the tab.

boolean
@RequiresFeature(name = CustomTabsFeatures.ENGAGEMENT_SIGNALS, enforcement = "androidx.browser.customtabs.CustomTabsSession#isEngagementSignalsApiAvailable")
setEngagementSignalsCallback(
    @NonNull Executor executor,
    @NonNull EngagementSignalsCallback callback,
    @NonNull Bundle extras
)

Sets an EngagementSignalsCallback to receive callbacks for events related to the user's engagement with webpage within the tab.

boolean

Sets a PendingIntent object to be sent when the user swipes up from the secondary (bottom) toolbar.

boolean
setSecondaryToolbarViews(
    @Nullable RemoteViews remoteViews,
    @Nullable int[] clickableIDs,
    @Nullable PendingIntent pendingIntent
)

Updates the RemoteViews of the secondary toolbar in an existing custom tab session.

boolean
setToolbarItem(int id, @NonNull Bitmap icon, @NonNull String description)

This method is deprecated.

Use CustomTabsSession#setSecondaryToolbarViews(RemoteViews, int[], PendingIntent)

boolean
validateRelationship(
    @CustomTabsService.Relation int relation,
    @NonNull Uri origin,
    @Nullable Bundle extras
)

Requests to validate a relationship between the application and an origin.

Public methods

createMockSessionForTesting

Added in 1.2.0
@VisibleForTesting
public static @NonNull CustomTabsSession createMockSessionForTesting(@NonNull ComponentName componentName)

Provides browsers a way to generate a mock CustomTabsSession for testing purposes.

Parameters
@NonNull ComponentName componentName

The component the session should be created for.

Returns
@NonNull CustomTabsSession

A mock session with no functionality.

isEngagementSignalsApiAvailable

Added in 1.6.0
public boolean isEngagementSignalsApiAvailable(@NonNull Bundle extras)

Returns whether the Engagement Signals API is available. The availability of the Engagement Signals API may change at runtime. If an EngagementSignalsCallback has been set, an onSessionEnded signal will be sent if the API becomes unavailable later.

Parameters
@NonNull Bundle extras

Reserved for future use.

Returns
boolean

Whether the Engagement Signals API is available. A false value means setEngagementSignalsCallback will return false and not set the callback.

Throws
android.os.RemoteException

If the Service dies while responding to the request.

java.lang.UnsupportedOperationException

If this method isn't supported by the Custom Tabs implementation.

mayLaunchUrl

Added in 1.2.0
public boolean mayLaunchUrl(
    @Nullable Uri url,
    @Nullable Bundle extras,
    @Nullable List<Bundle> otherLikelyBundles
)

Tells the browser of a likely future navigation to a URL. The most likely URL has to be specified first. Optionally, a list of other likely URLs can be provided. They are treated as less likely than the first one, and have to be sorted in decreasing priority order. These additional URLs may be ignored. All previous calls to this method will be deprioritized.

Parameters
@Nullable Uri url

Most likely URL, may be null if otherLikelyBundles is provided.

@Nullable Bundle extras

Reserved for future use.

@Nullable List<Bundle> otherLikelyBundles

Other likely destinations, sorted in decreasing likelihood order. Inside each Bundle, the client should provide a Uri using KEY_URL with putParcelable.

Returns
boolean

true for success.

postMessage

Added in 1.2.0
@CustomTabsService.Result
public int postMessage(@NonNull String message, @Nullable Bundle extras)

Sends a postMessage request using the origin communicated via requestPostMessageChannel. Fails when called before notifyMessageChannelReady is received on the client side.

Parameters
@NonNull String message

The message that is being sent.

@Nullable Bundle extras

Reserved for future use.

Returns
int

An integer constant about the postMessage request result. Will return RESULT_SUCCESS if successful.

receiveFile

Added in 1.2.0
public boolean receiveFile(@NonNull Uri uri, int purpose, @Nullable Bundle extras)

Passes an URI of a file, e.g. in order to pass a large bitmap to be displayed in the Custom Tabs provider. Prior to calling this method, the client needs to grant a read permission to the target Custom Tabs provider via grantUriPermission. The file is read and processed (where applicable) synchronously, therefore it's recommended to call this method on a background thread.

Parameters
@NonNull Uri uri

Uri of the file.

int purpose

Purpose of transferring this file, one of the constants enumerated in CustomTabsService#FilePurpose.

@Nullable Bundle extras

Reserved for future use.

Returns
boolean

true if the file was received successfully.

requestPostMessageChannel

Added in 1.2.0
public boolean requestPostMessageChannel(@NonNull Uri postMessageOrigin)

Sends a request to create a two way postMessage channel between the client and the browser.

Parameters
@NonNull Uri postMessageOrigin

A origin that the client is requesting to be identified as during the postMessage communication.

Returns
boolean

Whether the implementation accepted the request. Note that returning true here doesn't mean an origin has already been assigned as the validation is asynchronous.

requestPostMessageChannel

Added in 1.6.0
public boolean requestPostMessageChannel(
    @NonNull Uri postMessageOrigin,
    @Nullable Uri postMessageTargetOrigin,
    @NonNull Bundle extras
)

Sends a request to create a two way postMessage channel between the client and the browser with specifying the target origin to communicate with.

Parameters
@NonNull Uri postMessageOrigin

A origin that the client is requesting to be identified as during the postMessage communication.

@Nullable Uri postMessageTargetOrigin

The target Origin to establish the postMessage communication with.

@NonNull Bundle extras

Reserved for future use.

Returns
boolean

Whether the implementation accepted the request. Note that returning true here doesn't mean an origin has already been assigned as the validation is asynchronous.

setActionButton

Added in 1.2.0
public boolean setActionButton(@NonNull Bitmap icon, @NonNull String description)

This sets the action button on the toolbar with ID TOOLBAR_ACTION_BUTTON_ID.

Parameters
@NonNull Bitmap icon

The new icon of the action button.

@NonNull String description

Content description of the action button.

See also
setToolbarItem

setEngagementSignalsCallback

Added in 1.6.0
@RequiresFeature(name = CustomTabsFeatures.ENGAGEMENT_SIGNALS, enforcement = "androidx.browser.customtabs.CustomTabsSession#isEngagementSignalsApiAvailable")
public boolean setEngagementSignalsCallback(
    @NonNull EngagementSignalsCallback callback,
    @NonNull Bundle extras
)

Sets an EngagementSignalsCallback to receive callbacks for events related to the user's engagement with webpage within the tab. Note that the callback will be executed on the main thread using getMainLooper. To specify the execution thread, use setEngagementSignalsCallback.

Parameters
@NonNull EngagementSignalsCallback callback

The EngagementSignalsCallback to receive the user engagement signals.

@NonNull Bundle extras

Reserved for future use.

Returns
boolean

Whether the callback connection is allowed. If false, no callbacks will be called for this session.

Throws
android.os.RemoteException

If the Service dies while responding to the request.

java.lang.UnsupportedOperationException

If this method isn't supported by the Custom Tabs implementation.

setEngagementSignalsCallback

Added in 1.6.0
@RequiresFeature(name = CustomTabsFeatures.ENGAGEMENT_SIGNALS, enforcement = "androidx.browser.customtabs.CustomTabsSession#isEngagementSignalsApiAvailable")
public boolean setEngagementSignalsCallback(
    @NonNull Executor executor,
    @NonNull EngagementSignalsCallback callback,
    @NonNull Bundle extras
)

Sets an EngagementSignalsCallback to receive callbacks for events related to the user's engagement with webpage within the tab.

Parameters
@NonNull Executor executor

The Executor to be used to execute the callbacks.

@NonNull EngagementSignalsCallback callback

The EngagementSignalsCallback to receive the user engagement signals.

@NonNull Bundle extras

Reserved for future use.

Returns
boolean

Whether the callback connection is allowed. If false, no callbacks will be called for this session.

Throws
android.os.RemoteException

If the Service dies while responding to the request.

java.lang.UnsupportedOperationException

If this method isn't supported by the Custom Tabs implementation.

setSecondaryToolbarSwipeUpGesture

Added in 1.7.0
public boolean setSecondaryToolbarSwipeUpGesture(@Nullable PendingIntent pendingIntent)

Sets a PendingIntent object to be sent when the user swipes up from the secondary (bottom) toolbar.

Parameters
@Nullable PendingIntent pendingIntent

PendingIntent to send.

Returns
boolean

Whether the update succeeded.

setSecondaryToolbarViews

Added in 1.2.0
public boolean setSecondaryToolbarViews(
    @Nullable RemoteViews remoteViews,
    @Nullable int[] clickableIDs,
    @Nullable PendingIntent pendingIntent
)

Updates the RemoteViews of the secondary toolbar in an existing custom tab session.

Parameters
@Nullable RemoteViews remoteViews

The updated RemoteViews that will be shown in secondary toolbar. If null, the current secondary toolbar will be dismissed.

@Nullable int[] clickableIDs

The ids of clickable views. The onClick event of these views will be handled by custom tabs.

@Nullable PendingIntent pendingIntent

The PendingIntent that will be sent when the user clicks on one of the Views in clickableIDs.

setToolbarItem

Added in 1.2.0
Deprecated in 1.2.0
public boolean setToolbarItem(int id, @NonNull Bitmap icon, @NonNull String description)

Updates the visuals for toolbar items. Will only succeed if a custom tab created using this session is in the foreground in browser and the given id is valid.

Parameters
int id

The id for the item to update.

@NonNull Bitmap icon

The new icon of the toolbar item.

@NonNull String description

Content description of the toolbar item.

Returns
boolean

Whether the update succeeded.

validateRelationship

Added in 1.2.0
public boolean validateRelationship(
    @CustomTabsService.Relation int relation,
    @NonNull Uri origin,
    @Nullable Bundle extras
)

Requests to validate a relationship between the application and an origin.

See here for documentation about Digital Asset Links. This methods requests the browser to verify a relation with the calling application, to grant the associated rights.

If this method returns true, the validation result will be provided through onRelationshipValidationResult. Otherwise the request didn't succeed. The client must call warmup before this.

Parameters
@CustomTabsService.Relation int relation

Relation to check, must be one of the CustomTabsService#RELATION_* constants.

@NonNull Uri origin

Origin.

@Nullable Bundle extras

Reserved for future use.

Returns
boolean

true if the request has been submitted successfully.