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
Nested types |
|---|
public class CustomTabsSession.PendingSessionA class to be used instead of |
Public methods |
|
|---|---|
static @NonNull CustomTabsSession |
@VisibleForTestingProvides browsers a way to generate a mock |
boolean |
isEngagementSignalsApiAvailable(@NonNull Bundle extras)Returns whether the Engagement Signals API is available. |
boolean |
mayLaunchUrl(Tells the browser of a likely future navigation to a URL. |
int |
@CustomTabsService.ResultSends a postMessage request using the origin communicated via |
void |
@ExperimentalPrefetchRequest the browser to start navigational prefetch to the page that will be used for future navigations. |
void |
@ExperimentalPrefetchRequest the browser to start navigational prefetch to the pages that will be used for future navigations. |
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 |
requestPostMessageChannel(@NonNull Uri postMessageOrigin)Sends a request to create a two way postMessage channel between the client and the browser. |
boolean |
requestPostMessageChannel(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 |
boolean |
@RequiresFeature(name = CustomTabsFeatures.ENGAGEMENT_SIGNALS, enforcement = "androidx.browser.customtabs.CustomTabsSession#isEngagementSignalsApiAvailable")Sets an |
boolean |
@RequiresFeature(name = CustomTabsFeatures.ENGAGEMENT_SIGNALS, enforcement = "androidx.browser.customtabs.CustomTabsSession#isEngagementSignalsApiAvailable")Sets an |
boolean |
setSecondaryToolbarSwipeUpGesture(@Nullable PendingIntent pendingIntent)Sets a |
boolean |
setSecondaryToolbarViews(Updates the |
boolean |
This method is deprecated. Use CustomTabsSession#setSecondaryToolbarViews(RemoteViews, int[], PendingIntent) |
boolean |
validateRelationship(Requests to validate a relationship between the application and an origin. |
Public methods
createMockSessionForTesting
@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
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.
| Returns | |
|---|---|
boolean |
Whether the Engagement Signals API is available. A false value means |
| 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
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 |
@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 |
| Returns | |
|---|---|
boolean |
true for success. |
postMessage
@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 |
prefetch
@ExperimentalPrefetch
public void prefetch(@NonNull Uri url, @NonNull PrefetchOptions options)
Request the browser to start navigational prefetch to the page that will be used for future navigations.
| Parameters | |
|---|---|
@NonNull Uri url |
The url to be prefetched for upcoming navigations. |
@NonNull PrefetchOptions options |
The option used for prefetch request. Please see |
prefetch
@ExperimentalPrefetch
public void prefetch(@NonNull List<Uri> urls, @NonNull PrefetchOptions options)
Request the browser to start navigational prefetch to the pages that will be used for future navigations.
| Parameters | |
|---|---|
@NonNull List<Uri> urls |
The urls to be prefetched for upcoming navigations. |
@NonNull PrefetchOptions options |
The option used for prefetch request. Please see |
receiveFile
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 |
|
int purpose |
Purpose of transferring this file, one of the constants enumerated in |
@Nullable Bundle extras |
Reserved for future use. |
| Returns | |
|---|---|
boolean |
|
requestPostMessageChannel
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
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
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
@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 |
@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
@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 |
@NonNull EngagementSignalsCallback callback |
The |
@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
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 |
|
| Returns | |
|---|---|
boolean |
Whether the update succeeded. |
setSecondaryToolbarViews
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 |
@Nullable int[] clickableIDs |
The ids of clickable views. The onClick event of these views will be handled by custom tabs. |
@Nullable PendingIntent pendingIntent |
The |
public booleansetToolbarItem(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
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 |
@NonNull Uri origin |
Origin. |
@Nullable Bundle extras |
Reserved for future use. |
| Returns | |
|---|---|
boolean |
|