public class

CustomTabsCallback

extends java.lang.Object

 java.lang.Object

↳androidx.browser.customtabs.CustomTabsCallback

Gradle dependencies

compile group: 'androidx.browser', name: 'browser', version: '1.4.0'

  • groupId: androidx.browser
  • artifactId: browser
  • version: 1.4.0

Artifact androidx.browser:browser:1.4.0 it located at Google repository (https://maven.google.com/)

Androidx artifact mapping:

androidx.browser:browser com.android.support:customtabs

Androidx class mapping:

androidx.browser.customtabs.CustomTabsCallback android.support.customtabs.CustomTabsCallback

Overview

A callback class for custom tabs client to get messages regarding events in their custom tabs. In the implementation, all callbacks are sent to the UI thread for the client.

Summary

Fields
public static final intNAVIGATION_ABORTED

Sent when loading was aborted by a user action before it finishes like clicking on a link or refreshing the page.

public static final intNAVIGATION_FAILED

Sent when the tab couldn't finish loading due to a failure.

public static final intNAVIGATION_FINISHED

Sent when the tab has finished loading a page.

public static final intNAVIGATION_STARTED

Sent when the tab has started loading a page.

public static final java.lang.StringONLINE_EXTRAS_KEY

Key for the extra included in CustomTabsCallback.onRelationshipValidationResult(int, Uri, boolean, Bundle) extras containing whether the verification was performed while the device was online.

public static final intTAB_HIDDEN

Sent when the tab becomes hidden.

public static final intTAB_SHOWN

Sent when the tab becomes visible.

Constructors
publicCustomTabsCallback()

Methods
public voidextraCallback(java.lang.String callbackName, Bundle args)

Unsupported callbacks that may be provided by the implementation.

public BundleextraCallbackWithResult(java.lang.String callbackName, Bundle args)

The same as CustomTabsCallback.extraCallback(String, Bundle), except that this method allows the custom tabs provider to return a result.

public voidonMessageChannelReady(Bundle extras)

Called when CustomTabsSession has requested a postMessage channel through CustomTabsService and the channel is ready for sending and receiving messages on both ends.

public voidonNavigationEvent(int navigationEvent, Bundle extras)

To be called when a navigation event happens.

public voidonPostMessage(java.lang.String message, Bundle extras)

Called when a tab controlled by this CustomTabsSession has sent a postMessage.

public voidonRelationshipValidationResult(int relation, Uri requestedOrigin, boolean result, Bundle extras)

Called when a relationship validation result is available.

from java.lang.Objectclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

Fields

public static final int NAVIGATION_STARTED

Sent when the tab has started loading a page.

public static final int NAVIGATION_FINISHED

Sent when the tab has finished loading a page.

public static final int NAVIGATION_FAILED

Sent when the tab couldn't finish loading due to a failure.

public static final int NAVIGATION_ABORTED

Sent when loading was aborted by a user action before it finishes like clicking on a link or refreshing the page.

public static final int TAB_SHOWN

Sent when the tab becomes visible.

public static final int TAB_HIDDEN

Sent when the tab becomes hidden.

public static final java.lang.String ONLINE_EXTRAS_KEY

Key for the extra included in CustomTabsCallback.onRelationshipValidationResult(int, Uri, boolean, Bundle) extras containing whether the verification was performed while the device was online. This may be missing in cases verification was short cut.

Constructors

public CustomTabsCallback()

Methods

public void onNavigationEvent(int navigationEvent, Bundle extras)

To be called when a navigation event happens.

Parameters:

navigationEvent: The code corresponding to the navigation event.
extras: Reserved for future use.

public void extraCallback(java.lang.String callbackName, Bundle args)

Unsupported callbacks that may be provided by the implementation.

Note:Clients should never rely on this callback to be called and/or to have a defined behavior, as it is entirely implementation-defined and not supported.

This can be used by implementations to add extra callbacks, for testing or experimental purposes.

Parameters:

callbackName: Name of the extra callback.
args: Arguments for the callback

public Bundle extraCallbackWithResult(java.lang.String callbackName, Bundle args)

The same as CustomTabsCallback.extraCallback(String, Bundle), except that this method allows the custom tabs provider to return a result. A return value of null will be used to signify that the client does not know how to handle the callback. As optional best practices, CustomTabsService.KEY_SUCCESS could be use to identify that callback was *successfully* handled. For example, when returning a message with result:


     Bundle result = new Bundle();
     result.putString("message", message);
     if (success)
         result.putBoolean(CustomTabsService#KEY_SUCCESS, true);
     return result;
 
The caller side:

     Bundle result = extraCallbackWithResult(callbackName, args);
     if (result.getBoolean(CustomTabsService#KEY_SUCCESS)) {
         // callback was successfully handled
     }
 

public void onMessageChannelReady(Bundle extras)

Called when CustomTabsSession has requested a postMessage channel through CustomTabsService and the channel is ready for sending and receiving messages on both ends.

Parameters:

extras: Reserved for future use.

public void onPostMessage(java.lang.String message, Bundle extras)

Called when a tab controlled by this CustomTabsSession has sent a postMessage. If postMessage() is called from a single thread, then the messages will be posted in the same order. When received on the client side, it is the client's responsibility to preserve the ordering further.

Parameters:

message: The message sent.
extras: Reserved for future use.

public void onRelationshipValidationResult(int relation, Uri requestedOrigin, boolean result, Bundle extras)

Called when a relationship validation result is available.

Parameters:

relation: Relation for which the result is available. Value previously passed to CustomTabsSession.validateRelationship(int, Uri, Bundle). Must be one of the CustomTabsService#RELATION_* constants.
requestedOrigin: Origin requested. Value previously passed to CustomTabsSession.validateRelationship(int, Uri, Bundle).
result: Whether the relation was validated.
extras: Reserved for future use.

Source

/*
 * Copyright 2018 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package androidx.browser.customtabs;

import android.net.Uri;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RestrictTo;
import androidx.browser.customtabs.CustomTabsService.Relation;

/**
 * A callback class for custom tabs client to get messages regarding events in their custom tabs. In
 * the implementation, all callbacks are sent to the UI thread for the client.
 */
public class CustomTabsCallback {
    /**
     * Sent when the tab has started loading a page.
     */
    public static final int NAVIGATION_STARTED = 1;

    /**
     * Sent when the tab has finished loading a page.
     */
    public static final int NAVIGATION_FINISHED = 2;

    /**
     * Sent when the tab couldn't finish loading due to a failure.
     */
    public static final int NAVIGATION_FAILED = 3;

    /**
     * Sent when loading was aborted by a user action before it finishes like clicking on a link
     * or refreshing the page.
     */
    public static final int NAVIGATION_ABORTED = 4;

    /**
     * Sent when the tab becomes visible.
     */
    public static final int TAB_SHOWN = 5;

    /**
     * Sent when the tab becomes hidden.
     */
    public static final int TAB_HIDDEN = 6;

    /**
     * Key for the extra included in {@link #onRelationshipValidationResult} {@code extras}
     * containing whether the verification was performed while the device was online. This may be
     * missing in cases verification was short cut.
     * @hide
     */
    @RestrictTo(RestrictTo.Scope.LIBRARY)
    public static final String ONLINE_EXTRAS_KEY = "online";

    /**
     * To be called when a navigation event happens.
     *
     * @param navigationEvent The code corresponding to the navigation event.
     * @param extras Reserved for future use.
     */
    public void onNavigationEvent(int navigationEvent, @Nullable Bundle extras) {}

    /**
     * Unsupported callbacks that may be provided by the implementation.
     *
     * <p>
     * <strong>Note:</strong>Clients should <strong>never</strong> rely on this callback to be
     * called and/or to have a defined behavior, as it is entirely implementation-defined and not
     * supported.
     *
     * <p> This can be used by implementations to add extra callbacks, for testing or experimental
     * purposes.
     *
     * @param callbackName Name of the extra callback.
     * @param args Arguments for the callback
     */
    public void extraCallback(@NonNull String callbackName, @Nullable Bundle args) {}

    /**
     * The same as {@link #extraCallback}, except that this method allows the custom tabs provider
     * to return a result.
     *
     * A return value of {@code null} will be used to signify that the client does not know how to
     * handle the callback.
     *
     * As optional best practices, {@link CustomTabsService#KEY_SUCCESS} could be use to identify
     * that callback was *successfully* handled. For example, when returning a message with result:
     * <pre><code>
     *     Bundle result = new Bundle();
     *     result.putString("message", message);
     *     if (success)
     *         result.putBoolean(CustomTabsService#KEY_SUCCESS, true);
     *     return result;
     * </code></pre>
     * The caller side:
     * <pre><code>
     *     Bundle result = extraCallbackWithResult(callbackName, args);
     *     if (result.getBoolean(CustomTabsService#KEY_SUCCESS)) {
     *         // callback was successfully handled
     *     }
     * </code></pre>
     */
    @Nullable
    public Bundle extraCallbackWithResult(@NonNull String callbackName, @Nullable Bundle args) {
        return null;
    }

    /**
     * Called when {@link CustomTabsSession} has requested a postMessage channel through
     * {@link CustomTabsService#requestPostMessageChannel(
     * CustomTabsSessionToken, android.net.Uri)} and the channel
     * is ready for sending and receiving messages on both ends.
     *
     * @param extras Reserved for future use.
     */
    public void onMessageChannelReady(@Nullable Bundle extras) {}

    /**
     * Called when a tab controlled by this {@link CustomTabsSession} has sent a postMessage.
     * If postMessage() is called from a single thread, then the messages will be posted in the
     * same order. When received on the client side, it is the client's responsibility to preserve
     * the ordering further.
     *
     * @param message The message sent.
     * @param extras Reserved for future use.
     */
    public void onPostMessage(@NonNull String message, @Nullable Bundle extras) {}

    /**
     * Called when a relationship validation result is available.
     *
     * @param relation Relation for which the result is available. Value previously passed to
     *                 {@link CustomTabsSession#validateRelationship(int, Uri, Bundle)}. Must be one
     *                 of the {@code CustomTabsService#RELATION_* } constants.
     * @param requestedOrigin Origin requested. Value previously passed to
     *                        {@link CustomTabsSession#validateRelationship(int, Uri, Bundle)}.
     * @param result Whether the relation was validated.
     * @param extras Reserved for future use.
     */
    public void onRelationshipValidationResult(@Relation int relation, @NonNull Uri requestedOrigin,
            boolean result, @Nullable Bundle extras) {}
}