CustomTabsIntent.java revision e063d4d36883c9f7ca100036b9344b665ab6fd1e
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.ActivityOptions;
2108889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysalimport android.app.PendingIntent;
2297ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lizeimport android.content.Context;
2308889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysalimport android.content.Intent;
2408889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysalimport android.graphics.Bitmap;
2508889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysalimport android.graphics.Color;
2608889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysalimport android.net.Uri;
27e063d4d36883c9f7ca100036b9344b665ab6fd1eYusuf Ozuysalimport android.os.Build;
2808889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysalimport android.os.Bundle;
29e063d4d36883c9f7ca100036b9344b665ab6fd1eYusuf Ozuysalimport android.os.IBinder;
3097ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lizeimport android.support.annotation.AnimRes;
31c502e639bdb79fa04454641310f8965d0ffe0191Yusuf Ozuysalimport android.support.annotation.NonNull;
32e063d4d36883c9f7ca100036b9344b665ab6fd1eYusuf Ozuysalimport android.support.annotation.ColorInt;
3397ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lizeimport android.support.annotation.Nullable;
3408889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal
35e063d4d36883c9f7ca100036b9344b665ab6fd1eYusuf Ozuysalimport java.lang.reflect.InvocationTargetException;
36e063d4d36883c9f7ca100036b9344b665ab6fd1eYusuf Ozuysalimport java.lang.reflect.Method;
3797ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lizeimport java.util.ArrayList;
3808889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal
3908889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal/**
4097ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize * Class holding the {@link Intent} and start bundle for a Custom Tabs Activity.
4197ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize *
4297ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize * <p>
4397ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize * <strong>Note:</strong> The constants below are public for the browser implementation's benefit.
4497ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize * You are strongly encouraged to use {@link CustomTabsIntent.Builder}.</p>
4508889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal */
4697ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lizepublic final class CustomTabsIntent {
4708889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal
4808889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal    /**
4908889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal     * Extra used to match the session. This has to be included in the intent to open in
5008889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal     * a custom tab. This is the same IBinder that gets passed to ICustomTabsService#newSession.
5108889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal     * Null if there is no need to match any service side sessions with the intent.
5208889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal     */
5308889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal    public static final String EXTRA_SESSION = "android.support.customtabs.extra.SESSION";
5408889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal
5508889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal    /**
5608889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal     * Extra that changes the background color for the toolbar. colorRes is an int that specifies a
5708889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal     * {@link Color}, not a resource id.
5808889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal     */
5908889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal    public static final String EXTRA_TOOLBAR_COLOR =
6008889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal            "android.support.customtabs.extra.TOOLBAR_COLOR";
6108889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal
6208889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal    /**
6397ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize     * Extra bitmap that specifies the icon of the back button on the toolbar. If the client chooses
6497ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize     * not to customize it, a default close button will be used.
6597ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize     */
6697ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize    public static final String EXTRA_CLOSE_BUTTON_ICON =
6797ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize            "android.support.customtabs.extra.CLOSE_BUTTON_ICON";
6897ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize
6997ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize    /**
7097ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize     * Extra (int) that specifies state for showing the page title. Default is {@link #NO_TITLE}.
7197ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize     */
7297ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize    public static final String EXTRA_TITLE_VISIBILITY_STATE =
7397ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize            "android.support.customtabs.extra.TITLE_VISIBILITY";
7497ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize
7597ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize    /**
7697ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize     * Don't show any title. Shows only the domain.
7797ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize     */
7897ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize    public static final int NO_TITLE = 0;
7997ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize
8097ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize    /**
8197ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize     * Shows the page title and the domain.
8297ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize     */
8397ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize    public static final int SHOW_PAGE_TITLE = 1;
8497ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize
8597ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize    /**
868a1ccd14081000d6501622de72a16da67c33030cYusuf Ozuysal     * Bundle used for adding a custom action button to the custom tab toolbar. The client should
878a1ccd14081000d6501622de72a16da67c33030cYusuf Ozuysal     * provide a description, an icon {@link Bitmap} and a {@link PendingIntent} for the button.
888a1ccd14081000d6501622de72a16da67c33030cYusuf Ozuysal     * All three keys must be present.
8908889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal     */
9008889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal    public static final String EXTRA_ACTION_BUTTON_BUNDLE =
9108889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal            "android.support.customtabs.extra.ACTION_BUTTON_BUNDLE";
9208889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal
9308889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal    /**
9408889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal     * Key that specifies the {@link Bitmap} to be used as the image source for the action button.
958a1ccd14081000d6501622de72a16da67c33030cYusuf Ozuysal     *  The icon should't be more than 24dp in height (No padding needed. The button itself will be
968a1ccd14081000d6501622de72a16da67c33030cYusuf Ozuysal     *  48dp in height) and have a width/height ratio of less than 2.
9708889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal     */
9808889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal    public static final String KEY_ICON = "android.support.customtabs.customaction.ICON";
9908889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal
10008889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal    /**
1018a1ccd14081000d6501622de72a16da67c33030cYusuf Ozuysal     * Key that specifies the content description for the custom action button.
1028a1ccd14081000d6501622de72a16da67c33030cYusuf Ozuysal     */
1038a1ccd14081000d6501622de72a16da67c33030cYusuf Ozuysal    public static final String KEY_DESCRIPTION =
1048a1ccd14081000d6501622de72a16da67c33030cYusuf Ozuysal            "android.support.customtabs.customaction.DESCRIPTION";
1058a1ccd14081000d6501622de72a16da67c33030cYusuf Ozuysal
1068a1ccd14081000d6501622de72a16da67c33030cYusuf Ozuysal    /**
10708889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal     * Key that specifies the PendingIntent to launch when the action button or menu item was
10808889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal     * clicked. The custom tab will be calling {@link PendingIntent#send()} on clicks after adding
10908889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal     * the url as data. The client app can call {@link Intent#getDataString()} to get the url.
11008889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal     */
11108889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal    public static final String KEY_PENDING_INTENT =
11208889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal            "android.support.customtabs.customaction.PENDING_INTENT";
11308889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal
11408889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal    /**
11508889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal     * Use an {@code ArrayList<Bundle>} for specifying menu related params. There should be a
11608889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal     * separate {@link Bundle} for each custom menu item.
11708889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal     */
11808889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal    public static final String EXTRA_MENU_ITEMS = "android.support.customtabs.extra.MENU_ITEMS";
11908889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal
12008889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal    /**
12108889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal     * Key for specifying the title of a menu item.
12208889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal     */
12308889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal    public static final String KEY_MENU_ITEM_TITLE =
12408889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal            "android.support.customtabs.customaction.MENU_ITEM_TITLE";
12508889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal
12608889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal    /**
12708889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal     * Bundle constructed out of {@link ActivityOptions} that will be running when the
12808889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal     * {@link Activity} that holds the custom tab gets finished. A similar ActivityOptions
12908889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal     * for creation should be constructed and given to the startActivity() call that
13008889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal     * launches the custom tab.
13108889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal     */
13208889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal    public static final String EXTRA_EXIT_ANIMATION_BUNDLE =
13308889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal            "android.support.customtabs.extra.EXIT_ANIMATION_BUNDLE";
13408889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal
13508889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal    /**
13697ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize     * An {@link Intent} used to start the Custom Tabs Activity.
13797ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize     */
13897ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize    @NonNull public final Intent intent;
13997ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize
14097ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize    /**
14197ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize     * A {@link Bundle} containing the start animation for the Custom Tabs Activity.
14208889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal     */
14397ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize    @Nullable public final Bundle startAnimationBundle;
14497ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize
14597ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize    /**
14697ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize     * Convenience method to launch a Custom Tabs Activity.
14797ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize     * @param context The source Activity.
14897ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize     * @param url The URL to load in the Custom Tab.
14997ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize     */
15097ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize    public void launchUrl(Activity context, Uri url) {
15197ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize        intent.setData(url);
15297ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize        if (startAnimationBundle != null){
15397ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize            context.startActivity(intent, startAnimationBundle);
15497ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize        } else {
15597ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize            context.startActivity(intent);
15697ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize        }
15797ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize    }
15897ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize
15997ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize    private CustomTabsIntent(Intent intent, Bundle startAnimationBundle) {
16097ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize        this.intent = intent;
16197ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize        this.startAnimationBundle = startAnimationBundle;
16208889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal    }
16308889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal
16408889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal    /**
16597ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize     * Builder class for {@link CustomTabsIntent} objects.
16697ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize     */
16797ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize    public static final class Builder {
16897ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize        private final Intent mIntent = new Intent(Intent.ACTION_VIEW);
16997ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize        private ArrayList<Bundle> mMenuItems = null;
17097ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize        private Bundle mStartAnimationBundle = null;
17197ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize
17297ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize        /**
17397ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize         * Creates a {@link CustomTabsIntent.Builder} object associated with no
17497ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize         * {@link CustomTabsSession}.
17597ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize         */
17697ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize        public Builder() {
17797ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize            this(null);
17897ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize        }
17997ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize
18097ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize        /**
18197ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize         * Creates a {@link CustomTabsIntent.Builder} object associated with a given
18297ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize         * {@link CustomTabsSession}.
18397ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize         *
18497ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize         * Guarantees that the {@link Intent} will be sent to the same component as the one the
18597ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize         * session is associated with.
18697ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize         *
18797ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize         * @param session The session to associate this Builder with.
18897ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize         */
18997ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize        public Builder(@Nullable CustomTabsSession session) {
19097ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize            if (session != null) mIntent.setPackage(session.getComponentName().getPackageName());
19197ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize            Bundle bundle = new Bundle();
192e063d4d36883c9f7ca100036b9344b665ab6fd1eYusuf Ozuysal            safePutBinder(bundle, EXTRA_SESSION, session == null ? null : session.getBinder());
19397ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize            mIntent.putExtras(bundle);
19497ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize        }
19597ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize
19697ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize        /**
19797ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize         * Sets the toolbar color.
19897ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize         *
19997ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize         * @param color {@link Color}
20097ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize         */
20197ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize        public Builder setToolbarColor(@ColorInt int color) {
20297ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize            mIntent.putExtra(EXTRA_TOOLBAR_COLOR, color);
20397ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize            return this;
20497ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize        }
20597ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize
20697ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize        /**
20797ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize         * Sets the Close button icon for the custom tab.
20897ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize         *
20997ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize         * @param icon The icon {@link Bitmap}
21097ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize         */
21197ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize        public Builder setCloseButtonIcon(@NonNull Bitmap icon) {
21297ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize            mIntent.putExtra(EXTRA_CLOSE_BUTTON_ICON, icon);
21397ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize            return this;
21497ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize        }
21597ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize
21697ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize        /**
21797ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize         * Sets whether the title should be shown in the custom tab.
21897ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize         *
21997ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize         * @param showTitle Whether the title should be shown.
22097ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize         */
22197ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize        public Builder setShowTitle(boolean showTitle) {
22297ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize            mIntent.putExtra(EXTRA_TITLE_VISIBILITY_STATE,
22397ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize                    showTitle ? SHOW_PAGE_TITLE : NO_TITLE);
22497ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize            return this;
22597ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize        }
22697ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize
22797ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize        /**
22897ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize         * Adds a menu item.
22997ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize         *
23097ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize         * @param label Menu label.
23197ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize         * @param pendingIntent Pending intent delivered when the menu item is clicked.
23297ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize         */
23397ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize        public Builder addMenuItem(@NonNull String label, @NonNull PendingIntent pendingIntent) {
23497ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize            if (mMenuItems == null) mMenuItems = new ArrayList<>();
23597ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize            Bundle bundle = new Bundle();
23697ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize            bundle.putString(KEY_MENU_ITEM_TITLE, label);
23797ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize            bundle.putParcelable(KEY_PENDING_INTENT, pendingIntent);
23897ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize            mMenuItems.add(bundle);
23997ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize            return this;
24097ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize        }
24197ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize
24297ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize        /**
24397ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize         * Set the action button.
24497ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize         *
24597ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize         * @param icon The icon.
2468a1ccd14081000d6501622de72a16da67c33030cYusuf Ozuysal         * @param description The description for the button. To be used for accessibility.
24797ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize         * @param pendingIntent pending intent delivered when the button is clicked.
24897ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize         */
2498a1ccd14081000d6501622de72a16da67c33030cYusuf Ozuysal        public Builder setActionButton(@NonNull Bitmap icon,
2508a1ccd14081000d6501622de72a16da67c33030cYusuf Ozuysal                @NonNull String description, @NonNull PendingIntent pendingIntent) {
25197ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize            Bundle bundle = new Bundle();
25297ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize            bundle.putParcelable(KEY_ICON, icon);
2538a1ccd14081000d6501622de72a16da67c33030cYusuf Ozuysal            bundle.putString(KEY_DESCRIPTION, description);
25497ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize            bundle.putParcelable(KEY_PENDING_INTENT, pendingIntent);
25597ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize            mIntent.putExtra(EXTRA_ACTION_BUTTON_BUNDLE, bundle);
25697ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize            return this;
25797ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize        }
25897ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize
25997ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize        /**
26097ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize         * Sets the start animations,
26197ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize         *
26297ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize         * @param context Application context.
26397ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize         * @param enterResId Resource ID of the "enter" animation for the browser.
26497ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize         * @param exitResId Resource ID of the "exit" animation for the application.
26597ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize         */
26697ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize        public Builder setStartAnimations(
26797ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize                @NonNull Context context, @AnimRes int enterResId, @AnimRes int exitResId) {
26897ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize            mStartAnimationBundle =
26997ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize                    ActivityOptions.makeCustomAnimation(context, enterResId, exitResId).toBundle();
27097ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize            return this;
27197ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize        }
27297ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize
27397ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize        /**
27497ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize         * Sets the exit animations,
27597ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize         *
27697ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize         * @param context Application context.
27797ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize         * @param enterResId Resource ID of the "enter" animation for the application.
27897ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize         * @param exitResId Resource ID of the "exit" animation for the browser.
27997ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize         */
28097ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize        public Builder setExitAnimations(
28197ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize                @NonNull Context context, @AnimRes int enterResId, @AnimRes int exitResId) {
28297ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize            Bundle bundle =
28397ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize                    ActivityOptions.makeCustomAnimation(context, enterResId, exitResId).toBundle();
28497ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize            mIntent.putExtra(EXTRA_EXIT_ANIMATION_BUNDLE, bundle);
28597ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize            return this;
28697ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize        }
28797ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize
28897ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize        /**
28997ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize         * Combines all the options that have been set and returns a new {@link CustomTabsIntent}
29097ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize         * object.
29197ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize         */
29297ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize        public CustomTabsIntent build() {
29397ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize            if (mMenuItems != null) {
29497ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize                mIntent.putParcelableArrayListExtra(CustomTabsIntent.EXTRA_MENU_ITEMS, mMenuItems);
29597ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize            }
29697ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize            return new CustomTabsIntent(mIntent, mStartAnimationBundle);
29797ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize        }
298e063d4d36883c9f7ca100036b9344b665ab6fd1eYusuf Ozuysal
299e063d4d36883c9f7ca100036b9344b665ab6fd1eYusuf Ozuysal        /**
300e063d4d36883c9f7ca100036b9344b665ab6fd1eYusuf Ozuysal         * A convenience method to handle putting an {@link IBinder} inside a {@link Bundle} for all
301e063d4d36883c9f7ca100036b9344b665ab6fd1eYusuf Ozuysal         * Android version.
302e063d4d36883c9f7ca100036b9344b665ab6fd1eYusuf Ozuysal         * @param bundle The bundle to insert the {@link IBinder}.
303e063d4d36883c9f7ca100036b9344b665ab6fd1eYusuf Ozuysal         * @param key    The key to use while putting the {@link IBinder}.
304e063d4d36883c9f7ca100036b9344b665ab6fd1eYusuf Ozuysal         * @param binder The {@link IBinder} to put.
305e063d4d36883c9f7ca100036b9344b665ab6fd1eYusuf Ozuysal         * @return       Whether the operation was successful.
306e063d4d36883c9f7ca100036b9344b665ab6fd1eYusuf Ozuysal         */
307e063d4d36883c9f7ca100036b9344b665ab6fd1eYusuf Ozuysal        private boolean safePutBinder(Bundle bundle, String key, IBinder binder) {
308e063d4d36883c9f7ca100036b9344b665ab6fd1eYusuf Ozuysal            try {
309e063d4d36883c9f7ca100036b9344b665ab6fd1eYusuf Ozuysal                // {@link Bundle#putBinder} exists since JB MR2, but we have
310e063d4d36883c9f7ca100036b9344b665ab6fd1eYusuf Ozuysal                // {@link Bundle#putIBinder} which is the same method since the dawn of time. Use
311e063d4d36883c9f7ca100036b9344b665ab6fd1eYusuf Ozuysal                // reflection when necessary to call it.
312e063d4d36883c9f7ca100036b9344b665ab6fd1eYusuf Ozuysal                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
313e063d4d36883c9f7ca100036b9344b665ab6fd1eYusuf Ozuysal                    bundle.putBinder(key, binder);
314e063d4d36883c9f7ca100036b9344b665ab6fd1eYusuf Ozuysal                } else {
315e063d4d36883c9f7ca100036b9344b665ab6fd1eYusuf Ozuysal                    Method putBinderMethod =
316e063d4d36883c9f7ca100036b9344b665ab6fd1eYusuf Ozuysal                            Bundle.class.getMethod("putIBinder", String.class, IBinder.class);
317e063d4d36883c9f7ca100036b9344b665ab6fd1eYusuf Ozuysal                    putBinderMethod.invoke(bundle, key, binder);
318e063d4d36883c9f7ca100036b9344b665ab6fd1eYusuf Ozuysal                }
319e063d4d36883c9f7ca100036b9344b665ab6fd1eYusuf Ozuysal            } catch (InvocationTargetException | IllegalAccessException
320e063d4d36883c9f7ca100036b9344b665ab6fd1eYusuf Ozuysal                    | IllegalArgumentException | NoSuchMethodException e) {
321e063d4d36883c9f7ca100036b9344b665ab6fd1eYusuf Ozuysal                return false;
322e063d4d36883c9f7ca100036b9344b665ab6fd1eYusuf Ozuysal            }
323e063d4d36883c9f7ca100036b9344b665ab6fd1eYusuf Ozuysal            return true;
324e063d4d36883c9f7ca100036b9344b665ab6fd1eYusuf Ozuysal        }
32508889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal    }
32608889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal}
327