CustomTabsSession

public final class CustomTabsSession
extends Object

java.lang.Object
   ↳ androidx.browser.customtabs.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 CustomTabsSession createMockSessionForTesting(ComponentName componentName)

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

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

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

int postMessage(String message, Bundle extras)

Sends a postMessage request using the origin communicated via CustomTabsService.requestPostMessageChannel(CustomTabsSessionToken, Uri).

boolean receiveFile(Uri uri, int purpose, Bundle extras)

Passes an URI of a file, e.g.

boolean requestPostMessageChannel(Uri postMessageOrigin)

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

boolean setActionButton(Bitmap icon, String description)

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

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

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

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

This method is deprecated. Use CustomTabsSession#setSecondaryToolbarViews(RemoteViews, int[], PendingIntent)

boolean validateRelationship(int relation, Uri origin, Bundle extras)

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

Inherited methods

Public methods

createMockSessionForTesting

public static CustomTabsSession createMockSessionForTesting (ComponentName componentName)

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

Parameters
componentName ComponentName: The component the session should be created for.

Returns
CustomTabsSession A mock session with no functionality.

mayLaunchUrl

public boolean mayLaunchUrl (Uri url, 
                Bundle extras, 
                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
url Uri: Most likely URL, may be null if otherLikelyBundles is provided.

extras Bundle: Reserved for future use.

otherLikelyBundles List: Other likely destinations, sorted in decreasing likelihood order. Inside each Bundle, the client should provide a Uri using CustomTabsService.KEY_URL with Bundle.putParcelable(String, android.os.Parcelable).

Returns
boolean true for success.

postMessage

public int postMessage (String message, 
                Bundle extras)

Sends a postMessage request using the origin communicated via CustomTabsService.requestPostMessageChannel(CustomTabsSessionToken, Uri). Fails when called before PostMessageServiceConnection.notifyMessageChannelReady(Bundle) is received on the client side.

Parameters
message String: The message that is being sent.

extras Bundle: Reserved for future use.

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

receiveFile

public boolean receiveFile (Uri uri, 
                int purpose, 
                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 Context.grantUriPermission(String, Uri, int). The file is read and processed (where applicable) synchronously, therefore it's recommended to call this method on a background thread.

Parameters
uri Uri: Uri of the file.

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

extras Bundle: Reserved for future use.

Returns
boolean true if the file was received successfully.

requestPostMessageChann