CustomTabsIntent.java revision 1b7158bbc0d0ba4569881f547d12f52e405e125e
108889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal/*
208889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal * Copyright (C) 2015 The Android Open Source Project
308889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal *
408889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal * Licensed under the Apache License, Version 2.0 (the "License");
508889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal * you may not use this file except in compliance with the License.
608889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal * You may obtain a copy of the License at
708889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal *
808889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal *      http://www.apache.org/licenses/LICENSE-2.0
908889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal *
1008889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal * Unless required by applicable law or agreed to in writing, software
1108889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal * distributed under the License is distributed on an "AS IS" BASIS,
1208889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1308889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal * See the License for the specific language governing permissions and
1408889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal * limitations under the License.
1508889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal */
1608889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal
1708889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysalpackage android.support.customtabs;
1808889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal
1908889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysalimport android.app.Activity;
2008889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysalimport android.app.PendingIntent;
2197ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lizeimport android.content.Context;
2208889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysalimport android.content.Intent;
2308889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysalimport android.graphics.Bitmap;
2408889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysalimport android.graphics.Color;
2508889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysalimport android.net.Uri;
2608889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysalimport android.os.Bundle;
2797ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lizeimport android.support.annotation.AnimRes;
28e063d4d36883c9f7ca100036b9344b665ab6fd1eYusuf Ozuysalimport android.support.annotation.ColorInt;
29c5c0045de7c8848144a570665e0d3c950816f314Yusuf Ozuysalimport android.support.annotation.NonNull;
3097ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lizeimport android.support.annotation.Nullable;
311b7158bbc0d0ba4569881f547d12f52e405e125eIan Lakeimport android.support.v4.app.ActivityCompat;
321b7158bbc0d0ba4569881f547d12f52e405e125eIan Lakeimport android.support.v4.app.ActivityOptionsCompat;
33c5c0045de7c8848144a570665e0d3c950816f314Yusuf Ozuysalimport android.support.v4.app.BundleCompat;
3408889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal
3597ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lizeimport java.util.ArrayList;
3608889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal
3708889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal/**
3897ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize * Class holding the {@link Intent} and start bundle for a Custom Tabs Activity.
3997ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize *
4097ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize * <p>
4197ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize * <strong>Note:</strong> The constants below are public for the browser implementation's benefit.
4297ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize * You are strongly encouraged to use {@link CustomTabsIntent.Builder}.</p>
4308889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal */
4497ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lizepublic final class CustomTabsIntent {
4508889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal
4608889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal    /**
4708889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal     * Extra used to match the session. This has to be included in the intent to open in
4808889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal     * a custom tab. This is the same IBinder that gets passed to ICustomTabsService#newSession.
4908889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal     * Null if there is no need to match any service side sessions with the intent.
5008889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal     */
5108889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal    public static final String EXTRA_SESSION = "android.support.customtabs.extra.SESSION";
5208889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal
5308889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal    /**
5408889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal     * Extra that changes the background color for the toolbar. colorRes is an int that specifies a
5508889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal     * {@link Color}, not a resource id.
5608889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal     */
5708889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal    public static final String EXTRA_TOOLBAR_COLOR =
5808889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal            "android.support.customtabs.extra.TOOLBAR_COLOR";
5908889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal
6008889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal    /**
619440f0b000fc2740382eb4ae5f1afec58c245c2cYusuf Ozuysal     * Boolean extra that enables the url bar to hide as the user scrolls down the page
629440f0b000fc2740382eb4ae5f1afec58c245c2cYusuf Ozuysal     */
639440f0b000fc2740382eb4ae5f1afec58c245c2cYusuf Ozuysal    public static final String EXTRA_ENABLE_URLBAR_HIDING =
649440f0b000fc2740382eb4ae5f1afec58c245c2cYusuf Ozuysal            "android.support.customtabs.extra.ENABLE_URLBAR_HIDING";
659440f0b000fc2740382eb4ae5f1afec58c245c2cYusuf Ozuysal
669440f0b000fc2740382eb4ae5f1afec58c245c2cYusuf Ozuysal    /**
6797ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize     * Extra bitmap that specifies the icon of the back button on the toolbar. If the client chooses
6897ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize     * not to customize it, a default close button will be used.
6997ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize     */
7097ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize    public static final String EXTRA_CLOSE_BUTTON_ICON =
7197ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize            "android.support.customtabs.extra.CLOSE_BUTTON_ICON";
7297ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize
7397ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize    /**
7497ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize     * Extra (int) that specifies state for showing the page title. Default is {@link #NO_TITLE}.
7597ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize     */
7697ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize    public static final String EXTRA_TITLE_VISIBILITY_STATE =
7797ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize            "android.support.customtabs.extra.TITLE_VISIBILITY";
7897ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize
7997ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize    /**
8097ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize     * Don't show any title. Shows only the domain.
8197ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize     */
8297ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize    public static final int NO_TITLE = 0;
8397ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize
8497ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize    /**
8597ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize     * Shows the page title and the domain.
8697ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize     */
8797ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize    public static final int SHOW_PAGE_TITLE = 1;
8897ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize
8997ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize    /**
908a1ccd14081000d6501622de72a16da67c33030cYusuf Ozuysal     * Bundle used for adding a custom action button to the custom tab toolbar. The client should
918a1ccd14081000d6501622de72a16da67c33030cYusuf Ozuysal     * provide a description, an icon {@link Bitmap} and a {@link PendingIntent} for the button.
928a1ccd14081000d6501622de72a16da67c33030cYusuf Ozuysal     * All three keys must be present.
9308889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal     */
9408889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal    public static final String EXTRA_ACTION_BUTTON_BUNDLE =
9508889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal            "android.support.customtabs.extra.ACTION_BUTTON_BUNDLE";
9608889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal
9708889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal    /**
9808889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal     * Key that specifies the {@link Bitmap} to be used as the image source for the action button.
998a1ccd14081000d6501622de72a16da67c33030cYusuf Ozuysal     *  The icon should't be more than 24dp in height (No padding needed. The button itself will be
1008a1ccd14081000d6501622de72a16da67c33030cYusuf Ozuysal     *  48dp in height) and have a width/height ratio of less than 2.
10108889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal     */
10208889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal    public static final String KEY_ICON = "android.support.customtabs.customaction.ICON";
10308889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal
10408889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal    /**
1058a1ccd14081000d6501622de72a16da67c33030cYusuf Ozuysal     * Key that specifies the content description for the custom action button.
1068a1ccd14081000d6501622de72a16da67c33030cYusuf Ozuysal     */
1078a1ccd14081000d6501622de72a16da67c33030cYusuf Ozuysal    public static final String KEY_DESCRIPTION =
1088a1ccd14081000d6501622de72a16da67c33030cYusuf Ozuysal            "android.support.customtabs.customaction.DESCRIPTION";
1098a1ccd14081000d6501622de72a16da67c33030cYusuf Ozuysal
1108a1ccd14081000d6501622de72a16da67c33030cYusuf Ozuysal    /**
11108889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal     * Key that specifies the PendingIntent to launch when the action button or menu item was
11208889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal     * clicked. The custom tab will be calling {@link PendingIntent#send()} on clicks after adding
11308889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal     * the url as data. The client app can call {@link Intent#getDataString()} to get the url.
11408889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal     */
11508889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal    public static final String KEY_PENDING_INTENT =
11608889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal            "android.support.customtabs.customaction.PENDING_INTENT";
11708889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal
11808889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal    /**
1199440f0b000fc2740382eb4ae5f1afec58c245c2cYusuf Ozuysal     * Extra boolean that specifies whether the custom action button should be tinted. Default is
1209440f0b000fc2740382eb4ae5f1afec58c245c2cYusuf Ozuysal     * false and the action button will not be tinted.
1219440f0b000fc2740382eb4ae5f1afec58c245c2cYusuf Ozuysal     */
1229440f0b000fc2740382eb4ae5f1afec58c245c2cYusuf Ozuysal    public static final String EXTRA_TINT_ACTION_BUTTON =
1239440f0b000fc2740382eb4ae5f1afec58c245c2cYusuf Ozuysal            "android.support.customtabs.extra.TINT_ACTION_BUTTON";
1249440f0b000fc2740382eb4ae5f1afec58c245c2cYusuf Ozuysal
1259440f0b000fc2740382eb4ae5f1afec58c245c2cYusuf Ozuysal    /**
12608889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal     * Use an {@code ArrayList<Bundle>} for specifying menu related params. There should be a
12708889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal     * separate {@link Bundle} for each custom menu item.
12808889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal     */
12908889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal    public static final String EXTRA_MENU_ITEMS = "android.support.customtabs.extra.MENU_ITEMS";
13008889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal
13108889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal    /**
13208889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal     * Key for specifying the title of a menu item.
13308889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal     */
13408889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal    public static final String KEY_MENU_ITEM_TITLE =
13508889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal            "android.support.customtabs.customaction.MENU_ITEM_TITLE";
13608889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal
13708889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal    /**
1381b7158bbc0d0ba4569881f547d12f52e405e125eIan Lake     * Bundle constructed out of {@link ActivityOptionsCompat} that will be running when the
13908889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal     * {@link Activity} that holds the custom tab gets finished. A similar ActivityOptions
14008889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal     * for creation should be constructed and given to the startActivity() call that
14108889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal     * launches the custom tab.
14208889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal     */
14308889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal    public static final String EXTRA_EXIT_ANIMATION_BUNDLE =
14408889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal            "android.support.customtabs.extra.EXIT_ANIMATION_BUNDLE";
14508889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal
14608889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal    /**
14797ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize     * An {@link Intent} used to start the Custom Tabs Activity.
14897ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize     */
14997ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize    @NonNull public final Intent intent;
15097ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize
15197ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize    /**
15297ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize     * A {@link Bundle} containing the start animation for the Custom Tabs Activity.
15308889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal     */
15497ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize    @Nullable public final Bundle startAnimationBundle;
15597ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize
15697ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize    /**
15797ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize     * Convenience method to launch a Custom Tabs Activity.
15897ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize     * @param context The source Activity.
15997ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize     * @param url The URL to load in the Custom Tab.
16097ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize     */
16197ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize    public void launchUrl(Activity context, Uri url) {
16297ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize        intent.setData(url);
1631b7158bbc0d0ba4569881f547d12f52e405e125eIan Lake        ActivityCompat.startActivity(context, intent, startAnimationBundle);
16497ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize    }
16597ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize
16697ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize    private CustomTabsIntent(Intent intent, Bundle startAnimationBundle) {
16797ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize        this.intent = intent;
16897ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize        this.startAnimationBundle = startAnimationBundle;
16908889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal    }
17008889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal
17108889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal    /**
17297ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize     * Builder class for {@link CustomTabsIntent} objects.
17397ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize     */
17497ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize    public static final class Builder {
17597ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize        private final Intent mIntent = new Intent(Intent.ACTION_VIEW);
17697ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize        private ArrayList<Bundle> mMenuItems = null;
17797ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize        private Bundle mStartAnimationBundle = null;
17897ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize
17997ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize        /**
18097ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize         * Creates a {@link CustomTabsIntent.Builder} object associated with no
18197ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize         * {@link CustomTabsSession}.
18297ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize         */
18397ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize        public Builder() {
18497ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize            this(null);
18597ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize        }
18697ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize
18797ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize        /**
18897ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize         * Creates a {@link CustomTabsIntent.Builder} object associated with a given
18997ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize         * {@link CustomTabsSession}.
19097ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize         *
19197ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize         * Guarantees that the {@link Intent} will be sent to the same component as the one the
19297ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize         * session is associated with.
19397ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize         *
19497ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize         * @param session The session to associate this Builder with.
19597ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize         */
19697ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize        public Builder(@Nullable CustomTabsSession session) {
19797ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize            if (session != null) mIntent.setPackage(session.getComponentName().getPackageName());
19897ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize            Bundle bundle = new Bundle();
199c5c0045de7c8848144a570665e0d3c950816f314Yusuf Ozuysal            BundleCompat.putBinder(
200c5c0045de7c8848144a570665e0d3c950816f314Yusuf Ozuysal                    bundle, EXTRA_SESSION, session == null ? null : session.getBinder());
20197ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize            mIntent.putExtras(bundle);
20297ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize        }
20397ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize
20497ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize        /**
20597ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize         * Sets the toolbar color.
20697ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize         *
20797ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize         * @param color {@link Color}
20897ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize         */
20997ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize        public Builder setToolbarColor(@ColorInt int color) {
21097ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize            mIntent.putExtra(EXTRA_TOOLBAR_COLOR, color);
21197ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize            return this;
21297ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize        }
21397ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize
21497ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize        /**
2159440f0b000fc2740382eb4ae5f1afec58c245c2cYusuf Ozuysal         * Enables the url bar to hide as the user scrolls down on the page.
2169440f0b000fc2740382eb4ae5f1afec58c245c2cYusuf Ozuysal         */
2179440f0b000fc2740382eb4ae5f1afec58c245c2cYusuf Ozuysal        public Builder enableUrlBarHiding() {
2189440f0b000fc2740382eb4ae5f1afec58c245c2cYusuf Ozuysal            mIntent.putExtra(EXTRA_ENABLE_URLBAR_HIDING, true);
2199440f0b000fc2740382eb4ae5f1afec58c245c2cYusuf Ozuysal            return this;
2209440f0b000fc2740382eb4ae5f1afec58c245c2cYusuf Ozuysal        }
2219440f0b000fc2740382eb4ae5f1afec58c245c2cYusuf Ozuysal
2229440f0b000fc2740382eb4ae5f1afec58c245c2cYusuf Ozuysal        /**
22397ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize         * Sets the Close button icon for the custom tab.
22497ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize         *
22597ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize         * @param icon The icon {@link Bitmap}
22697ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize         */
22797ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize        public Builder setCloseButtonIcon(@NonNull Bitmap icon) {
22897ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize            mIntent.putExtra(EXTRA_CLOSE_BUTTON_ICON, icon);
22997ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize            return this;
23097ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize        }
23197ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize
23297ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize        /**
23397ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize         * Sets whether the title should be shown in the custom tab.
23497ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize         *
23597ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize         * @param showTitle Whether the title should be shown.
23697ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize         */
23797ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize        public Builder setShowTitle(boolean showTitle) {
23897ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize            mIntent.putExtra(EXTRA_TITLE_VISIBILITY_STATE,
23997ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize                    showTitle ? SHOW_PAGE_TITLE : NO_TITLE);
24097ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize            return this;
24197ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize        }
24297ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize
24397ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize        /**
24497ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize         * Adds a menu item.
24597ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize         *
24697ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize         * @param label Menu label.
24797ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize         * @param pendingIntent Pending intent delivered when the menu item is clicked.
24897ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize         */
24997ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize        public Builder addMenuItem(@NonNull String label, @NonNull PendingIntent pendingIntent) {
25097ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize            if (mMenuItems == null) mMenuItems = new ArrayList<>();
25197ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize            Bundle bundle = new Bundle();
25297ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize            bundle.putString(KEY_MENU_ITEM_TITLE, label);
25397ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize            bundle.putParcelable(KEY_PENDING_INTENT, pendingIntent);
25497ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize            mMenuItems.add(bundle);
25597ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize            return this;
25697ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize        }
25797ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize
25897ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize        /**
25997ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize         * Set the action button.
26097ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize         *
26197ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize         * @param icon The icon.
2628a1ccd14081000d6501622de72a16da67c33030cYusuf Ozuysal         * @param description The description for the button. To be used for accessibility.
26397ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize         * @param pendingIntent pending intent delivered when the button is clicked.
2649440f0b000fc2740382eb4ae5f1afec58c245c2cYusuf Ozuysal         * @param shouldTint Whether the action button should be tinted.
26597ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize         */
2669440f0b000fc2740382eb4ae5f1afec58c245c2cYusuf Ozuysal        public Builder setActionButton(@NonNull Bitmap icon, @NonNull String description,
2679440f0b000fc2740382eb4ae5f1afec58c245c2cYusuf Ozuysal                @NonNull PendingIntent pendingIntent, boolean shouldTint) {
26897ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize            Bundle bundle = new Bundle();
26997ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize            bundle.putParcelable(KEY_ICON, icon);
2708a1ccd14081000d6501622de72a16da67c33030cYusuf Ozuysal            bundle.putString(KEY_DESCRIPTION, description);
27197ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize            bundle.putParcelable(KEY_PENDING_INTENT, pendingIntent);
27297ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize            mIntent.putExtra(EXTRA_ACTION_BUTTON_BUNDLE, bundle);
2739440f0b000fc2740382eb4ae5f1afec58c245c2cYusuf Ozuysal            mIntent.putExtra(EXTRA_TINT_ACTION_BUTTON, shouldTint);
27497ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize            return this;
27597ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize        }
27697ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize
27797ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize        /**
2789440f0b000fc2740382eb4ae5f1afec58c245c2cYusuf Ozuysal         * See {@link CustomTabsIntent.Builder#setActionButton(
2799440f0b000fc2740382eb4ae5f1afec58c245c2cYusuf Ozuysal         * Bitmap, String, PendingIntent, boolean)}
2809440f0b000fc2740382eb4ae5f1afec58c245c2cYusuf Ozuysal         */
2819440f0b000fc2740382eb4ae5f1afec58c245c2cYusuf Ozuysal        public Builder setActionButton(@NonNull Bitmap icon, @NonNull String description,
2829440f0b000fc2740382eb4ae5f1afec58c245c2cYusuf Ozuysal                @NonNull PendingIntent pendingIntent) {
2839440f0b000fc2740382eb4ae5f1afec58c245c2cYusuf Ozuysal            return setActionButton(icon, description, pendingIntent, false);
2849440f0b000fc2740382eb4ae5f1afec58c245c2cYusuf Ozuysal        }
2859440f0b000fc2740382eb4ae5f1afec58c245c2cYusuf Ozuysal
2869440f0b000fc2740382eb4ae5f1afec58c245c2cYusuf Ozuysal        /**
28797ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize         * Sets the start animations,
28897ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize         *
28997ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize         * @param context Application context.
29097ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize         * @param enterResId Resource ID of the "enter" animation for the browser.
29197ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize         * @param exitResId Resource ID of the "exit" animation for the application.
29297ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize         */
29397ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize        public Builder setStartAnimations(
29497ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize                @NonNull Context context, @AnimRes int enterResId, @AnimRes int exitResId) {
2951b7158bbc0d0ba4569881f547d12f52e405e125eIan Lake            mStartAnimationBundle = ActivityOptionsCompat.makeCustomAnimation(
2961b7158bbc0d0ba4569881f547d12f52e405e125eIan Lake                    context, enterResId, exitResId).toBundle();
29797ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize            return this;
29897ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize        }
29997ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize
30097ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize        /**
30197ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize         * Sets the exit animations,
30297ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize         *
30397ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize         * @param context Application context.
30497ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize         * @param enterResId Resource ID of the "enter" animation for the application.
30597ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize         * @param exitResId Resource ID of the "exit" animation for the browser.
30697ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize         */
30797ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize        public Builder setExitAnimations(
30897ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize                @NonNull Context context, @AnimRes int enterResId, @AnimRes int exitResId) {
3091b7158bbc0d0ba4569881f547d12f52e405e125eIan Lake            Bundle bundle = ActivityOptionsCompat.makeCustomAnimation(
3101b7158bbc0d0ba4569881f547d12f52e405e125eIan Lake                    context, enterResId, exitResId).toBundle();
31197ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize            mIntent.putExtra(EXTRA_EXIT_ANIMATION_BUNDLE, bundle);
31297ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize            return this;
31397ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize        }
31497ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize
31597ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize        /**
31697ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize         * Combines all the options that have been set and returns a new {@link CustomTabsIntent}
31797ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize         * object.
31897ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize         */
31997ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize        public CustomTabsIntent build() {
32097ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize            if (mMenuItems != null) {
32197ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize                mIntent.putParcelableArrayListExtra(CustomTabsIntent.EXTRA_MENU_ITEMS, mMenuItems);
32297ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize            }
32397ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize            return new CustomTabsIntent(mIntent, mStartAnimationBundle);
32497ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize        }
32508889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal    }
32608889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal}
327