CustomTabsService
public abstract class CustomTabsService extends Service
| java.lang.Object | ||||
| ↳ | android.content.Context | |||
| ↳ | android.content.ContextWrapper | |||
| ↳ | android.app.Service | |||
| ↳ | androidx.browser.customtabs.CustomTabsService |
Abstract service class for implementing Custom Tabs related functionality. The service should be responding to the action ACTION_CUSTOM_TABS_CONNECTION. This class should be used by implementers that want to provide Custom Tabs functionality, not by clients that want to launch Custom Tabs.
Summary
Nested types |
|---|
@Retention(value = RetentionPolicy.SOURCE) |
@Retention(value = RetentionPolicy.SOURCE) |
Constants |
|
|---|---|
static final String |
ACTION_CUSTOM_TABS_CONNECTION = "android.support.customtabs.action.CustomTabsService"The Intent action that a CustomTabsService must respond to. |
static final String |
CATEGORY_AUTH_TAB = "androidx.browser.auth.category.AuthTab"An Intent filter category to signify that the Custom Tabs provider supports Auth Tab. |
static final String |
CATEGORY_COLOR_SCHEME_CUSTOMIZATION = "androidx.browser.customtabs.category.ColorSchemeCustomization"An Intent filter category to signify that the Custom Tabs provider supports selecting and customizing color schemes via |
static final String |
CATEGORY_EPHEMERAL_BROWSING = "androidx.browser.customtabs.category.EphemeralBrowsing"An Intent filter category to signify that the Custom Tabs provider supports ephemeral browsing feature which opens Custom Tab that does not share cookies or other data with the browser that handles the Custom Tab. |
static final String |
CATEGORY_NAVBAR_COLOR_CUSTOMIZATION = "androidx.browser.customtabs.category.NavBarColorCustomization"An Intent filter category to signify that the Custom Tabs provider supports customizing the color of the navigation bar ( |
static final String |
CATEGORY_SET_NETWORK = "androidx.browser.customtabs.category.SetNetwork"An Intent filter category to signify that the Custom Tabs provider supports multi-network, bind a custom tab to a particular network via |
static final String |
CATEGORY_TRUSTED_WEB_ACTIVITY_IMMERSIVE_MODE = "androidx.browser.trusted.category.ImmersiveMode"An Intent filter category to signify that the Trusted Web Activity provider supports immersive mode. |
static final String |
CATEGORY_WEB_SHARE_TARGET_V2 = "androidx.browser.trusted.category.WebShareTargetV2"An Intent filter category to signify that the Trusted Web Activity provider supports sending shared data according to the Web Share Target v2 protocol defined in https://wicg.github.io/web-share-target/level-2/. |
static final int |
A constant to be used with |
static final String |
KEY_SUCCESS = "androidx.browser.customtabs.SUCCESS"The key to use to store a boolean in the returns bundle of |
static final String |
KEY_URL = "android.support.customtabs.otherurls.URL"For |
static final int |
Used for |
static final int |
Used for |
static final int |
Indicates that the postMessage request was not allowed due to a bad argument or requesting at a disallowed time like when in background. |
static final int |
Indicates that the postMessage request has failed due to an internal error on the browser message channel. |
static final int |
Indicates that the postMessage request has failed due to a |
static final int |
RESULT_SUCCESS = 0Indicates that the postMessage request was accepted. |
static final String |
TRUSTED_WEB_ACTIVITY_CATEGORY = "androidx.browser.trusted.category.TrustedWebActivities"An Intent filter category to signify that the Custom Tabs provider supports Trusted Web Activities (see |
Public constructors |
|---|
Protected methods |
|
|---|---|
boolean |
cleanUpSession(@NonNull AuthTabSessionToken sessionToken)Called when the client side |
boolean |
cleanUpSession(@NonNull CustomTabsSessionToken sessionToken)Called when the client side |
abstract @Nullable Bundle |
extraCommand(@NonNull String commandName, @Nullable Bundle args)Unsupported commands that may be provided by the implementation. |
boolean |
isEngagementSignalsApiAvailable(Returns whether the Engagement Signals API is available. |
abstract boolean |
mayLaunchUrl(Tells the browser of a likely future navigation to a URL. |
abstract boolean |
newSession(@NonNull CustomTabsSessionToken sessionToken)Creates a new session through an ICustomTabsService with the optional callback. |
abstract 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 page that will be used for future navigations. |
abstract boolean |
receiveFile(Receive a file from client by given Uri, e.g. in order to display a large bitmap in a Custom Tab. |
boolean |
registerAuthTabSession(@NonNull AuthTabSessionToken sessionToken)Creates a new Auth Tab session through an ICustomTabsService with the optional callback. |
abstract boolean |
requestPostMessageChannel(Sends a request to create a two way postMessage channel between the client and the browser linked with the given |
boolean |
requestPostMessageChannel(Same as above method with specifying the target origin to establish communication with. |
boolean |
setEngagementSignalsCallback(Sets an |
abstract boolean |
updateVisuals(Updates the visuals of custom tabs for the given session. |
abstract boolean |
validateRelationship(Request to validate a relationship between the application and an origin. |
abstract boolean |
warmup(long flags)Warms up the browser process asynchronously. |
Inherited Constants |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Inherited methods |
||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
||||||||||||||||||||||||||||||||||||||
|
Constants
ACTION_CUSTOM_TABS_CONNECTION
public static final String ACTION_CUSTOM_TABS_CONNECTION = "android.support.customtabs.action.CustomTabsService"
The Intent action that a CustomTabsService must respond to.
CATEGORY_AUTH_TAB
public static final String CATEGORY_AUTH_TAB = "androidx.browser.auth.category.AuthTab"
An Intent filter category to signify that the Custom Tabs provider supports Auth Tab.
CATEGORY_COLOR_SCHEME_CUSTOMIZATION
public static final String CATEGORY_COLOR_SCHEME_CUSTOMIZATION = "androidx.browser.customtabs.category.ColorSchemeCustomization"
An Intent filter category to signify that the Custom Tabs provider supports selecting and customizing color schemes via setColorScheme and setColorSchemeParams.
CATEGORY_EPHEMERAL_BROWSING
public static final String CATEGORY_EPHEMERAL_BROWSING = "androidx.browser.customtabs.category.EphemeralBrowsing"
An Intent filter category to signify that the Custom Tabs provider supports ephemeral browsing feature which opens Custom Tab that does not share cookies or other data with the browser that handles the Custom Tab.
CATEGORY_NAVBAR_COLOR_CUSTOMIZATION
public static final String CATEGORY_NAVBAR_COLOR_CUSTOMIZATION = "androidx.browser.customtabs.category.NavBarColorCustomization"
An Intent filter category to signify that the Custom Tabs provider supports customizing the color of the navigation bar (setNavigationBarColor).
CATEGORY_SET_NETWORK
public static final String CATEGORY_SET_NETWORK = "androidx.browser.customtabs.category.SetNetwork"
An Intent filter category to signify that the Custom Tabs provider supports multi-network, bind a custom tab to a particular network via setNetwork.
CATEGORY_TRUSTED_WEB_ACTIVITY_IMMERSIVE_MODE
public static final String CATEGORY_TRUSTED_WEB_ACTIVITY_IMMERSIVE_MODE = "androidx.browser.trusted.category.ImmersiveMode"
An Intent filter category to signify that the Trusted Web Activity provider supports immersive mode.
CATEGORY_WEB_SHARE_TARGET_V2
public static final String CATEGORY_WEB_SHARE_TARGET_V2 = "androidx.browser.trusted.category.WebShareTargetV2"
An Intent filter category to signify that the Trusted Web Activity provider supports sending shared data according to the Web Share Target v2 protocol defined in https://wicg.github.io/web-share-target/level-2/.
FILE_PURPOSE_TRUSTED_WEB_ACTIVITY_SPLASH_IMAGE
public static final int FILE_PURPOSE_TRUSTED_WEB_ACTIVITY_SPLASH_IMAGE = 1
A constant to be used with receiveFile indicating that the file is a splash image to be shown on top of a Trusted Web Activity while the web contents are loading.
KEY_SUCCESS
public static final String KEY_SUCCESS = "androidx.browser.customtabs.SUCCESS"
The key to use to store a boolean in the returns bundle of extraCommand method, to indicate the command is executed successfully.
KEY_URL
public static final String KEY_URL = "android.support.customtabs.otherurls.URL"
For mayLaunchUrl calls that wants to specify more than one url, this key can be used with putParcelable to insert a new url to each bundle inside list of bundles.
RELATION_HANDLE_ALL_URLS
public static final int RELATION_HANDLE_ALL_URLS = 2
Used for validateRelationship. Requests the ability to handle all URLs from a given origin.
RELATION_USE_AS_ORIGIN
public static final int RELATION_USE_AS_ORIGIN = 1
Used for validateRelationship. For App -> Web transitions, requests the app to use the declared origin to be used as origin for the client app in the web APIs context.
RESULT_FAILURE_DISALLOWED
public static final int RESULT_FAILURE_DISALLOWED = -1
Indicates that the postMessage request was not allowed due to a bad argument or requesting at a disallowed time like when in background.
RESULT_FAILURE_MESSAGING_ERROR
public static final int RESULT_FAILURE_MESSAGING_ERROR = -3
Indicates that the postMessage request has failed due to an internal error on the browser message channel.
RESULT_FAILURE_REMOTE_ERROR
public static final int RESULT_FAILURE_REMOTE_ERROR = -2
Indicates that the postMessage request has failed due to a RemoteException .
RESULT_SUCCESS
public static final int RESULT_SUCCESS = 0
Indicates that the postMessage request was accepted.
TRUSTED_WEB_ACTIVITY_CATEGORY
public static final String TRUSTED_WEB_ACTIVITY_CATEGORY = "androidx.browser.trusted.category.TrustedWebActivities"
An Intent filter category to signify that the Custom Tabs provider supports Trusted Web Activities (see TrustedWebUtils for more details).
Public constructors
Protected methods
cleanUpSession
protected boolean cleanUpSession(@NonNull AuthTabSessionToken sessionToken)
Called when the client side IBinder for this AuthTabSessionToken is dead. Can also be used to clean up DeathRecipient instances allocated for the given token.
| Parameters | |
|---|---|
@NonNull AuthTabSessionToken sessionToken |
The session token for which the |
| Returns | |
|---|---|
boolean |
True if the provided session was cleaned up as a result of this call, false if the session doesn't exist or has already been cleaned up. |
cleanUpSession
protected boolean cleanUpSession(@NonNull CustomTabsSessionToken sessionToken)
Called when the client side IBinder for this CustomTabsSessionToken is dead. Can also be used to clean up DeathRecipient instances allocated for the given token.
| Parameters | |
|---|---|
@NonNull CustomTabsSessionToken sessionToken |
The session token for which the |
| Returns | |
|---|---|
boolean |
Whether the clean up was successful. Multiple calls with two tokens holdings the same binder will return false. |
extraCommand
protected abstract @Nullable Bundle extraCommand(@NonNull String commandName, @Nullable Bundle args)
Unsupported commands that may be provided by the implementation.
Note:Clients should never rely on this method to have a defined behavior, as it is entirely implementation-defined and not supported.
This call can be used by implementations to add extra commands, for testing or experimental purposes. A return value of null will be used to signify that the client does not know how to handle the request. As optional best practices, KEY_SUCCESS could be use to identify that command was *successfully* handled. For example, when returning a message with result:
Bundle result = new Bundle();
result.putString("message", message);
if (success)
result.putBoolean(KEY_SUCCESS, true);
return result; Bundle result = service.extraCommand(commandName, args);
if (result.getBoolean(service.KEY_SUCCESS)) {
// Command was successfully handled
}isEngagementSignalsApiAvailable
protected boolean isEngagementSignalsApiAvailable(
@NonNull CustomTabsSessionToken sessionToken,
@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 CustomTabsSessionToken sessionToken |
The unique identifier for the session. |
@NonNull Bundle extras |
Reserved for future use. |
| Returns | |
|---|---|
boolean |
Whether the Engagement Signals API is available. A false value means |
mayLaunchUrl
protected abstract boolean mayLaunchUrl(
@NonNull CustomTabsSessionToken sessionToken,
@Nullable Uri url,
@Nullable Bundle extras,
@Nullable List<Bundle> otherLikelyBundles
)
Tells the browser of a likely future navigation to a URL.
The method warmup has to be called beforehand. The most likely URL has to be specified explicitly. 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 | |
|---|---|
@NonNull CustomTabsSessionToken sessionToken |
The unique identifier for the session. Can not be null. |
@Nullable Uri url |
Most likely URL. |
@Nullable Bundle extras |
Reserved for future use. |
@Nullable List<Bundle> otherLikelyBundles |
Other likely destinations, sorted in decreasing likelihood order. Each Bundle has to provide a url. |
| Returns | |
|---|---|
boolean |
Whether the call was successful. |
newSession
protected abstract boolean newSession(@NonNull CustomTabsSessionToken sessionToken)
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 | |
|---|---|
@NonNull CustomTabsSessionToken sessionToken |
Session token to be used as a unique identifier. This also has access to the |
| Returns | |
|---|---|
boolean |
Whether a new session was successfully created. |
postMessage
@CustomTabsService.Result
protected abstract int postMessage(
@NonNull CustomTabsSessionToken sessionToken,
@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 CustomTabsSessionToken sessionToken |
The unique identifier for the session. Can not be null. |
@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
protected void prefetch(
@NonNull CustomTabsSessionToken sessionToken,
@NonNull Uri url,
@NonNull PrefetchOptions options
)
Request the browser to start navigational prefetch to the page that will be used for future navigations. warmup is required to be called before using this method. TODO(crbug.com/40288091): Currently, there is no caller of this API and can be removed.
| Parameters | |
|---|---|
@NonNull CustomTabsSessionToken sessionToken |
The unique identifier for the session. |
@NonNull Uri url |
The url to be prefetched for future navigations. |
@NonNull PrefetchOptions options |
The option used for prefetch request. Please see |
prefetch
@ExperimentalPrefetch
protected void prefetch(
@NonNull CustomTabsSessionToken sessionToken,
@NonNull List<Uri> urls,
@NonNull PrefetchOptions options
)
Request the browser to start navigational prefetch to the page that will be used for future navigations. warmup is required to be called before using this method.
| Parameters | |
|---|---|
@NonNull CustomTabsSessionToken sessionToken |
The unique identifier for the session. |
@NonNull List<Uri> urls |
The urls to be prefetched for future navigations. |
@NonNull PrefetchOptions options |
The option used for prefetch request. Please see |
receiveFile
protected abstract boolean receiveFile(
@NonNull CustomTabsSessionToken sessionToken,
@NonNull Uri uri,
int purpose,
@Nullable Bundle extras
)
Receive a file from client by given Uri, e.g. in order to display a large bitmap in a Custom Tab. Prior to calling this method, the client grants a read permission to the target Custom Tabs provider via grantUriPermission. The file is read and processed (where applicable) synchronously.
| Parameters | |
|---|---|
@NonNull CustomTabsSessionToken sessionToken |
The unique identifier for the session. |
@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 |
|
registerAuthTabSession
protected boolean registerAuthTabSession(@NonNull AuthTabSessionToken sessionToken)
Creates a new Auth Tab 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 an Auth Tab. The client can then send later service calls or intents through the same session-intent-Auth Tab association.
| Parameters | |
|---|---|
@NonNull AuthTabSessionToken sessionToken |
Session token to be used as a unique identifier. This also has access to the AuthTabCallback passed from the client side through |
| Returns | |
|---|---|
boolean |
Whether a new session was successfully created. |
requestPostMessageChannel
protected abstract boolean requestPostMessageChannel(
@NonNull CustomTabsSessionToken sessionToken,
@NonNull Uri postMessageOrigin
)
Sends a request to create a two way postMessage channel between the client and the browser linked with the given CustomTabsSession.
| Parameters | |
|---|---|
@NonNull CustomTabsSessionToken sessionToken |
The unique identifier for the session. Can not be null. |
@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
protected boolean requestPostMessageChannel(
@NonNull CustomTabsSessionToken sessionToken,
@NonNull Uri postMessageOrigin,
@Nullable Uri postMessageTargetOrigin,
@NonNull Bundle extras
)
Same as above method with specifying the target origin to establish communication with.
| Parameters | |
|---|---|
@NonNull CustomTabsSessionToken sessionToken |
The unique identifier for the session. Can not be null. |
@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 PostMessageChannel with and send messages to. |
@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. |
setEngagementSignalsCallback
protected boolean setEngagementSignalsCallback(
@NonNull CustomTabsSessionToken sessionToken,
@NonNull EngagementSignalsCallback callback,
@NonNull Bundle extras
)
Sets an EngagementSignalsCallback to execute callbacks for events related to the user's engagement with the webpage within the tab.
| Parameters | |
|---|---|
@NonNull CustomTabsSessionToken sessionToken |
The unique identifier for the session. |
@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. |
updateVisuals
protected abstract boolean updateVisuals(
@NonNull CustomTabsSessionToken sessionToken,
@Nullable Bundle bundle
)
Updates the visuals of custom tabs for the given session. Will only succeed if the given session matches the currently active one.
| Parameters | |
|---|---|
@NonNull CustomTabsSessionToken sessionToken |
The currently active session that the custom tab belongs to. |
@Nullable Bundle bundle |
The action button configuration bundle. This bundle should be constructed with the same structure in |
| Returns | |
|---|---|
boolean |
Whether the operation was successful. |
validateRelationship
protected abstract boolean validateRelationship(
@NonNull CustomTabsSessionToken sessionToken,
@CustomTabsService.Relation int relation,
@NonNull Uri origin,
@Nullable Bundle extras
)
Request to validate a relationship between the application and an origin. 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 | |
|---|---|
@NonNull CustomTabsSessionToken sessionToken |
The unique identifier for the session. Can not be null. |
@CustomTabsService.Relation int relation |
Relation to check, must be one of the |
@NonNull Uri origin |
Origin for the relation query. |
@Nullable Bundle extras |
Reserved for future use. |
| Returns | |
|---|---|
boolean |
true if the request has been submitted successfully. |