CustomTabsIntent.java revision 97ee4f3f353309991efd3ceca369548b485e9a5f
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; 2708889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysalimport android.os.Build; 2808889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysalimport android.os.Bundle; 2908889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysalimport android.os.IBinder; 3097ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lizeimport android.support.annotation.AnimRes; 3197ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lizeimport android.support.annotation.NonNull; 3297ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lizeimport android.support.annotation.ColorInt; 3397ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lizeimport android.support.annotation.Nullable; 3408889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal 3508889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysalimport java.lang.reflect.InvocationTargetException; 3608889acbc842c73b64f94a761910154d9d42ee4cYusuf 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 /** 8608889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal * Bundle used for adding a custom action button to the custom tab toolbar. The client can 8708889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal * provide an icon {@link Bitmap} and a {@link PendingIntent} for the button. 8808889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal */ 8908889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal public static final String EXTRA_ACTION_BUTTON_BUNDLE = 9008889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal "android.support.customtabs.extra.ACTION_BUTTON_BUNDLE"; 9108889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal 9208889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal /** 9308889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal * Key that specifies the {@link Bitmap} to be used as the image source for the action button. 9408889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal */ 9508889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal public static final String KEY_ICON = "android.support.customtabs.customaction.ICON"; 9608889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal 9708889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal /** 9808889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal * Key that specifies the PendingIntent to launch when the action button or menu item was 9908889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal * clicked. The custom tab will be calling {@link PendingIntent#send()} on clicks after adding 10008889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal * the url as data. The client app can call {@link Intent#getDataString()} to get the url. 10108889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal */ 10208889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal public static final String KEY_PENDING_INTENT = 10308889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal "android.support.customtabs.customaction.PENDING_INTENT"; 10408889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal 10508889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal /** 10608889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal * Use an {@code ArrayList<Bundle>} for specifying menu related params. There should be a 10708889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal * separate {@link Bundle} for each custom menu item. 10808889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal */ 10908889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal public static final String EXTRA_MENU_ITEMS = "android.support.customtabs.extra.MENU_ITEMS"; 11008889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal 11108889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal /** 11208889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal * Key for specifying the title of a menu item. 11308889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal */ 11408889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal public static final String KEY_MENU_ITEM_TITLE = 11508889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal "android.support.customtabs.customaction.MENU_ITEM_TITLE"; 11608889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal 11708889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal /** 11808889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal * Bundle constructed out of {@link ActivityOptions} that will be running when the 11908889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal * {@link Activity} that holds the custom tab gets finished. A similar ActivityOptions 12008889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal * for creation should be constructed and given to the startActivity() call that 12108889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal * launches the custom tab. 12208889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal */ 12308889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal public static final String EXTRA_EXIT_ANIMATION_BUNDLE = 12408889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal "android.support.customtabs.extra.EXIT_ANIMATION_BUNDLE"; 12508889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal 12608889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal /** 12797ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize * An {@link Intent} used to start the Custom Tabs Activity. 12897ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize */ 12997ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize @NonNull public final Intent intent; 13097ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize 13197ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize /** 13297ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize * A {@link Bundle} containing the start animation for the Custom Tabs Activity. 13308889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal */ 13497ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize @Nullable public final Bundle startAnimationBundle; 13597ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize 13697ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize /** 13797ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize * Convenience method to launch a Custom Tabs Activity. 13897ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize * @param context The source Activity. 13997ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize * @param url The URL to load in the Custom Tab. 14097ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize */ 14197ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize public void launchUrl(Activity context, Uri url) { 14297ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize intent.setData(url); 14397ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize if (startAnimationBundle != null){ 14497ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize context.startActivity(intent, startAnimationBundle); 14597ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize } else { 14697ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize context.startActivity(intent); 14797ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize } 14897ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize } 14997ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize 15097ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize private CustomTabsIntent(Intent intent, Bundle startAnimationBundle) { 15197ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize this.intent = intent; 15297ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize this.startAnimationBundle = startAnimationBundle; 15308889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal } 15408889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal 15508889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal /** 15697ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize * Builder class for {@link CustomTabsIntent} objects. 15797ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize */ 15897ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize public static final class Builder { 15997ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize private final Intent mIntent = new Intent(Intent.ACTION_VIEW); 16097ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize private ArrayList<Bundle> mMenuItems = null; 16197ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize private Bundle mStartAnimationBundle = null; 16297ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize 16397ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize /** 16497ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize * Creates a {@link CustomTabsIntent.Builder} object associated with no 16597ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize * {@link CustomTabsSession}. 16697ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize */ 16797ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize public Builder() { 16897ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize this(null); 16997ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize } 17097ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize 17197ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize /** 17297ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize * Creates a {@link CustomTabsIntent.Builder} object associated with a given 17397ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize * {@link CustomTabsSession}. 17497ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize * 17597ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize * Guarantees that the {@link Intent} will be sent to the same component as the one the 17697ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize * session is associated with. 17797ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize * 17897ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize * @param session The session to associate this Builder with. 17997ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize */ 18097ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize public Builder(@Nullable CustomTabsSession session) { 18197ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize if (session != null) mIntent.setPackage(session.getComponentName().getPackageName()); 18297ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize Bundle bundle = new Bundle(); 18397ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize safePutBinder(bundle, EXTRA_SESSION, session == null ? null : session.getBinder()); 18497ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize mIntent.putExtras(bundle); 18597ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize } 18697ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize 18797ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize /** 18897ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize * Sets the toolbar color. 18997ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize * 19097ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize * @param color {@link Color} 19197ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize */ 19297ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize public Builder setToolbarColor(@ColorInt int color) { 19397ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize mIntent.putExtra(EXTRA_TOOLBAR_COLOR, color); 19497ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize return this; 19597ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize } 19697ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize 19797ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize /** 19897ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize * Sets the Close button icon for the custom tab. 19997ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize * 20097ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize * @param icon The icon {@link Bitmap} 20197ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize */ 20297ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize public Builder setCloseButtonIcon(@NonNull Bitmap icon) { 20397ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize mIntent.putExtra(EXTRA_CLOSE_BUTTON_ICON, icon); 20497ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize return this; 20597ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize } 20697ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize 20797ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize /** 20897ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize * Sets whether the title should be shown in the custom tab. 20997ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize * 21097ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize * @param showTitle Whether the title should be shown. 21197ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize */ 21297ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize public Builder setShowTitle(boolean showTitle) { 21397ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize mIntent.putExtra(EXTRA_TITLE_VISIBILITY_STATE, 21497ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize showTitle ? SHOW_PAGE_TITLE : NO_TITLE); 21597ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize return this; 21697ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize } 21797ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize 21897ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize /** 21997ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize * Adds a menu item. 22097ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize * 22197ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize * @param label Menu label. 22297ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize * @param pendingIntent Pending intent delivered when the menu item is clicked. 22397ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize */ 22497ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize public Builder addMenuItem(@NonNull String label, @NonNull PendingIntent pendingIntent) { 22597ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize if (mMenuItems == null) mMenuItems = new ArrayList<>(); 22697ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize Bundle bundle = new Bundle(); 22797ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize bundle.putString(KEY_MENU_ITEM_TITLE, label); 22897ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize bundle.putParcelable(KEY_PENDING_INTENT, pendingIntent); 22997ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize mMenuItems.add(bundle); 23097ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize return this; 23197ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize } 23297ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize 23397ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize /** 23497ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize * Set the action button. 23597ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize * 23697ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize * @param icon The icon. 23797ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize * @param pendingIntent pending intent delivered when the button is clicked. 23897ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize */ 23997ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize public Builder setActionButton(@NonNull Bitmap icon, @NonNull PendingIntent pendingIntent) { 24097ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize Bundle bundle = new Bundle(); 24197ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize bundle.putParcelable(KEY_ICON, icon); 24297ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize bundle.putParcelable(KEY_PENDING_INTENT, pendingIntent); 24397ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize mIntent.putExtra(EXTRA_ACTION_BUTTON_BUNDLE, bundle); 24497ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize return this; 24597ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize } 24697ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize 24797ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize /** 24897ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize * Sets the start animations, 24997ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize * 25097ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize * @param context Application context. 25197ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize * @param enterResId Resource ID of the "enter" animation for the browser. 25297ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize * @param exitResId Resource ID of the "exit" animation for the application. 25397ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize */ 25497ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize public Builder setStartAnimations( 25597ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize @NonNull Context context, @AnimRes int enterResId, @AnimRes int exitResId) { 25697ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize mStartAnimationBundle = 25797ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize ActivityOptions.makeCustomAnimation(context, enterResId, exitResId).toBundle(); 25897ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize return this; 25997ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize } 26097ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize 26197ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize /** 26297ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize * Sets the exit animations, 26397ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize * 26497ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize * @param context Application context. 26597ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize * @param enterResId Resource ID of the "enter" animation for the application. 26697ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize * @param exitResId Resource ID of the "exit" animation for the browser. 26797ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize */ 26897ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize public Builder setExitAnimations( 26997ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize @NonNull Context context, @AnimRes int enterResId, @AnimRes int exitResId) { 27097ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize Bundle bundle = 27197ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize ActivityOptions.makeCustomAnimation(context, enterResId, exitResId).toBundle(); 27297ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize mIntent.putExtra(EXTRA_EXIT_ANIMATION_BUNDLE, bundle); 27397ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize return this; 27497ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize } 27597ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize 27697ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize /** 27797ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize * Combines all the options that have been set and returns a new {@link CustomTabsIntent} 27897ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize * object. 27997ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize */ 28097ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize public CustomTabsIntent build() { 28197ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize if (mMenuItems != null) { 28297ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize mIntent.putParcelableArrayListExtra(CustomTabsIntent.EXTRA_MENU_ITEMS, mMenuItems); 28397ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize } 28497ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize return new CustomTabsIntent(mIntent, mStartAnimationBundle); 28597ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize } 28697ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize 28797ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize /** 28897ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize * A convenience method to handle putting an {@link IBinder} inside a {@link Bundle} for all 28997ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize * Android version. 29097ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize * @param bundle The bundle to insert the {@link IBinder}. 29197ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize * @param key The key to use while putting the {@link IBinder}. 29297ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize * @param binder The {@link IBinder} to put. 29397ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize * @return Whether the operation was successful. 29497ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize */ 29597ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize private boolean safePutBinder(Bundle bundle, String key, IBinder binder) { 29697ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize try { 29797ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize // {@link Bundle#putBinder} exists since JB MR2, but we have 29897ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize // {@link Bundle#putIBinder} which is the same method since the dawn of time. Use 29997ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize // reflection when necessary to call it. 30097ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) { 30197ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize bundle.putBinder(key, binder); 30297ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize } else { 30397ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize Method putBinderMethod = 30497ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize Bundle.class.getMethod("putIBinder", String.class, IBinder.class); 30597ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize putBinderMethod.invoke(bundle, key, binder); 30697ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize } 30797ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize } catch (InvocationTargetException | IllegalAccessException 30897ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize | IllegalArgumentException | NoSuchMethodException e) { 30997ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize return false; 31008889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal } 31197ee4f3f353309991efd3ceca369548b485e9a5fBenoit Lize return true; 31208889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal } 31308889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal } 31408889acbc842c73b64f94a761910154d9d42ee4cYusuf Ozuysal} 315