CustomTabsClient


public class CustomTabsClient


Class to communicate with a CustomTabsService and create CustomTabsSession from it.

Summary

Public methods

static boolean
bindCustomTabsService(
    @NonNull Context context,
    @Nullable String packageName,
    @NonNull CustomTabsServiceConnection connection
)

Bind to a CustomTabsService using the given package name and ServiceConnection.

static boolean
bindCustomTabsServicePreservePriority(
    @NonNull Context context,
    @Nullable String packageName,
    @NonNull CustomTabsServiceConnection connection
)

Bind to a CustomTabsService using the given package name and ServiceConnection.

static boolean

Connects to the Custom Tabs warmup service, and initializes the browser.

@Nullable Bundle
extraCommand(@NonNull String commandName, @Nullable Bundle args)

Can be used as a channel between the Custom Tabs client and the provider to do something that is not part of the API yet.

static @Nullable String

Returns the preferred package to use for Custom Tabs, preferring the default VIEW handler.

static @Nullable String
getPackageName(
    @NonNull Context context,
    @Nullable List<String> packages,
    boolean ignoreDefault
)

Returns the preferred package to use for Custom Tabs.

@Nullable CustomTabsSession

Creates a new session through an ICustomTabsService with the optional callback.

@Nullable CustomTabsSession
newSession(@Nullable CustomTabsCallback callback, int id)

Creates a new session or updates a callback for the existing session through an ICustomTabsService.

boolean
warmup(long flags)

Warm up the browser process.

Public methods

bindCustomTabsService

Added in 1.2.0
public static boolean bindCustomTabsService(
    @NonNull Context context,
    @Nullable String packageName,
    @NonNull CustomTabsServiceConnection connection
)

Bind to a CustomTabsService using the given package name and ServiceConnection.

Parameters
@NonNull Context context

Context to use while calling bindService

@Nullable String packageName

Package name to set on the Intent for binding.

@NonNull CustomTabsServiceConnection connection

CustomTabsServiceConnection to use when binding. This will return a CustomTabsClient on #onCustomTabsServiceConnected(ComponentName, CustomTabsClient)

Returns
boolean

Whether the binding was successful.

bindCustomTabsServicePreservePriority

Added in 1.3.0
public static boolean bindCustomTabsServicePreservePriority(
    @NonNull Context context,
    @Nullable String packageName,
    @NonNull CustomTabsServiceConnection connection
)

Bind to a CustomTabsService using the given package name and ServiceConnection. This is similar to bindCustomTabsService but does not use BIND_WAIVE_PRIORITY, making it suitable for use cases where the browser is immediately going to be launched and breaking the connection would be unrecoverable.

Parameters
@NonNull Context context

Context to use while calling bindService

@Nullable String packageName

Package name to set on the Intent for binding.

@NonNull CustomTabsServiceConnection connection

CustomTabsServiceConnection to use when binding. This will return a CustomTabsClient on #onCustomTabsServiceConnected(ComponentName, CustomTabsClient)

Returns
boolean

Whether the binding was successful.

connectAndInitialize

Added in 1.2.0
public static boolean connectAndInitialize(@NonNull Context context, @NonNull String packageName)

Connects to the Custom Tabs warmup service, and initializes the browser. This convenience method connects to the service, and immediately warms up the Custom Tabs implementation. Since service connection is asynchronous, the return code is not the return code of warmup. This call is optional, and clients are encouraged to connect to the service, call warmup() and create a session. In this case, calling this method is not necessary.

Parameters
@NonNull Context context

Context to use to connect to the remote service.

@NonNull String packageName

Package name of the target implementation.

Returns
boolean

Whether the binding was successful.

extraCommand

Added in 1.2.0
public @Nullable Bundle extraCommand(@NonNull String commandName, @Nullable Bundle args)

Can be used as a channel between the Custom Tabs client and the provider to do something that is not part of the API yet.

getPackageName

Added in 1.2.0
public static @Nullable String getPackageName(@NonNull Context context, @Nullable List<String> packages)

Returns the preferred package to use for Custom Tabs, preferring the default VIEW handler.

getPackageName

Added in 1.2.0
public static @Nullable String getPackageName(
    @NonNull Context context,
    @Nullable List<String> packages,
    boolean ignoreDefault
)

Returns the preferred package to use for Custom Tabs. The preferred package name is the default VIEW intent handler as long as it supports Custom Tabs. To modify this preferred behavior, set ignoreDefault to true and give a non empty list of package names in packages. This method queries the PackageManager to determine which packages support the Custom Tabs API. On apps that target Android 11 and above, this requires adding the following package visibility elements to your manifest.

<!-- Place inside the <queries> element. -->
<intent>
  <action android:name="android.support.customtabs.action.CustomTabsService" />
</intent>
Parameters
@NonNull Context context

Context to use for querying the packages.

@Nullable List<String> packages

Ordered list of packages to test for Custom Tabs support, in decreasing order of priority.

boolean ignoreDefault

If set, the default VIEW handler won't get priority over other browsers.

Returns
@Nullable String

The preferred package name for handling Custom Tabs, or null.

newSession

Added in 1.2.0
public @Nullable CustomTabsSession newSession(@Nullable CustomTabsCallback callback)

Creates a new session through an ICustomTabsService with the optional callback. This session can be used to associate any related communication through the service with an intent and then later with a Custom Tab. The client can then send later service calls or intents to through same session-intent-Custom Tab association.

Parameters
@Nullable CustomTabsCallback callback

The callback through which the client will receive updates about the created session. Can be null. All the callbacks will be received on the UI thread.

Returns
@Nullable CustomTabsSession

The session object that was created as a result of the transaction. The client can use this to relay session specific calls. Null if the service failed to respond (threw a RemoteException).

newSession

Added in 1.2.0
public @Nullable CustomTabsSession newSession(@Nullable CustomTabsCallback callback, int id)

Creates a new session or updates a callback for the existing session through an ICustomTabsService. This session can be used to associate any related communication through the service with an intent and then later with a Custom Tab. The client can then send later service calls or intents to through same session-intent-Custom Tab association.

Parameters
@Nullable CustomTabsCallback callback

The callback through which the client will receive updates about the created session. Can be null. All the callbacks will be received on the UI thread.

int id

The session id. If the session with the specified id already exists for the given client application, the new callback is supplied to that session and further attempts to launch URLs using that session will update the existing Custom Tab instead of launching a new one.

Returns
@Nullable CustomTabsSession

The session object that was created as a result of the transaction. The client can use this to relay session specific calls. Null if the service failed to respond (threw a RemoteException). If null is returned, attempt using newSession which is supported with older browsers.

warmup

Added in 1.2.0
public boolean warmup(long flags)

Warm up the browser process. Allows the browser application to pre-initialize itself in the background. Significantly speeds up URL opening in the browser. This is asynchronous and can be called several times.

Parameters
long flags

Reserved for future use.

Returns
boolean

Whether the warmup was successful.