CustomTabsClient
public class CustomTabsClient
Class to communicate with a CustomTabsService and create CustomTabsSession from it.
Summary
Public methods |
|
|---|---|
@Nullable AuthTabSession |
Associate |
@Nullable CustomTabsSession |
Associate |
static boolean |
bindCustomTabsService(Bind to a |
static boolean |
bindCustomTabsServicePreservePriority(Bind to a |
static boolean |
connectAndInitialize(@NonNull Context context, @NonNull String packageName)Connects to the Custom Tabs warmup service, and initializes the browser. |
static @NonNull AuthTabSession.PendingSession |
@ExperimentalPendingSessionCreates a new pending session without a callback. |
static @NonNull AuthTabSession.PendingSession |
@ExperimentalPendingSessionCreates a new pending session with a callback. |
@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 |
Returns the preferred package to use for Custom Tabs. |
static boolean |
isAuthTabSupported(@NonNull Context context, @NonNull String provider)Checks whether the Custom Tabs provider supports Auth Tab. |
static boolean |
isEphemeralBrowsingSupported(Checks whether the Custom Tabs provider supports Ephemeral Browsing. |
static boolean |
isSetNetworkSupported(@NonNull Context context, @NonNull String provider)Check whether the Custom Tabs provider supports multi-network feature |
@Nullable AuthTabSession |
newAuthTabSession(Creates a new session through an ICustomTabsService with the optional callback. |
@Nullable AuthTabSession |
newAuthTabSession(Creates a new session through an ICustomTabsService with the optional callback. |
static @NonNull CustomTabsSession.PendingSession |
newPendingSession(Creates a new pending session with an optional callback. |
@Nullable CustomTabsSession |
newSession(@Nullable CustomTabsCallback callback)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
attachAuthTabSession
@ExperimentalPendingSession
public @Nullable AuthTabSession attachAuthTabSession(@NonNull AuthTabSession.PendingSession session)
Associate AuthTabSession.PendingSession with the service and turn it into an AuthTabSession.
| Parameters | |
|---|---|
@NonNull AuthTabSession.PendingSession session |
The |
| Returns | |
|---|---|
@Nullable AuthTabSession |
The |
attachSession
public @Nullable CustomTabsSession attachSession(@NonNull CustomTabsSession.PendingSession session)
Associate CustomTabsSession.PendingSession with the service and turn it into a CustomTabsSession.
bindCustomTabsService
public static boolean bindCustomTabsService(
@NonNull Context context,
@NonNull String packageName,
@NonNull CustomTabsServiceConnection connection
)
Bind to a CustomTabsService using the given package name and ServiceConnection.
| Parameters | |
|---|---|
@NonNull Context context |
|
@NonNull String packageName |
Package name to set on the |
@NonNull CustomTabsServiceConnection connection |
|
| Returns | |
|---|---|
boolean |
Whether the binding was successful. |
bindCustomTabsServicePreservePriority
public static boolean bindCustomTabsServicePreservePriority(
@NonNull Context context,
@NonNull 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 |
|
@NonNull String packageName |
Package name to set on the |
@NonNull CustomTabsServiceConnection connection |
|
| Returns | |
|---|---|
boolean |
Whether the binding was successful. |
connectAndInitialize
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 |
|
@NonNull String packageName |
Package name of the target implementation. |
| Returns | |
|---|---|
boolean |
Whether the binding was successful. |
createPendingAuthTabSession
@ExperimentalPendingSession
public static @NonNull AuthTabSession.PendingSession createPendingAuthTabSession(@NonNull Context context, int id)
Creates a new pending session without a callback. This session can be converted to a standard session using attachSession after connection.
| Returns | |
|---|---|
@NonNull AuthTabSession.PendingSession |
The newly created |
createPendingAuthTabSession
@ExperimentalPendingSession
public static @NonNull AuthTabSession.PendingSession createPendingAuthTabSession(
@NonNull Context context,
int id,
@NonNull Executor executor,
@NonNull AuthTabCallback callback
)
Creates a new pending session with a callback. This session can be converted to a standard session using attachSession after connection.
| Parameters | |
|---|---|
@NonNull Context context |
The |
int id |
The session id. |
@NonNull Executor executor |
The |
@NonNull AuthTabCallback callback |
The callback through which the client will receive updates about the created session. |
| Returns | |
|---|---|
@NonNull AuthTabSession.PendingSession |
The newly created |
extraCommand
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
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
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>
isAuthTabSupported
public static boolean isAuthTabSupported(@NonNull Context context, @NonNull String provider)
Checks whether the Custom Tabs provider supports Auth Tab. See AuthTabIntent for more information on how to launch an Auth Tab.
| Parameters | |
|---|---|
@NonNull Context context |
The application |
@NonNull String provider |
The package name of the Custom Tabs provider. |
| Returns | |
|---|---|
boolean |
Whether the Custom Tabs provider supports Auth Tab. |
| See also | |
|---|---|
CATEGORY_AUTH_TAB |
isEphemeralBrowsingSupported
public static boolean isEphemeralBrowsingSupported(
@NonNull Context context,
@NonNull String provider
)
Checks whether the Custom Tabs provider supports Ephemeral Browsing.
| Parameters | |
|---|---|
@NonNull Context context |
The application |
@NonNull String provider |
The package name of the Custom Tabs provider. |
| Returns | |
|---|---|
boolean |
Whether the Custom Tabs provider supports Ephemeral Browsing. |
| See also | |
|---|---|
CATEGORY_EPHEMERAL_BROWSING |
isSetNetworkSupported
public static boolean isSetNetworkSupported(@NonNull Context context, @NonNull String provider)
Check whether the Custom Tabs provider supports multi-network feature setNetwork, i.e. be able to bind a custom tab to a particular network.
| Parameters | |
|---|---|
@NonNull Context context |
Application context. |
@NonNull String provider |
the package name of Custom Tabs provider. |
| Returns | |
|---|---|
boolean |
whether a Custom Tabs provider supports multi-network feature. |
| See also | |
|---|---|
setNetwork |
and CustomTabsService#CATEGORY_SET_NETWORK. |
newAuthTabSession
public @Nullable AuthTabSession newAuthTabSession(
@Nullable AuthTabCallback callback,
@Nullable Executor executor
)
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 AuthTabCallback callback |
The callback through which the client will receive updates about the created session. Can be null. |
@Nullable Executor executor |
The |
| Returns | |
|---|---|
@Nullable AuthTabSession |
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). |
newAuthTabSession
public @Nullable AuthTabSession newAuthTabSession(
@Nullable AuthTabCallback callback,
@Nullable Executor executor,
int id
)
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 AuthTabCallback callback |
The callback through which the client will receive updates about the created session. Can be null. |
@Nullable Executor executor |
The |
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 Auth Tab instead of launching a new one. |
| Returns | |
|---|---|
@Nullable AuthTabSession |
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). |
newPendingSession
public static @NonNull CustomTabsSession.PendingSession newPendingSession(
@NonNull Context context,
@Nullable CustomTabsCallback callback,
int id
)
Creates a new pending session with an optional callback. This session can be converted to a standard session using attachSession after connection. {@see PendingSession}
newSession
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
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 |
warmup
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. |