diff --git a/chrome/android/java/src/android/support/customtabs/CustomTabsIntent.java b/chrome/android/java/src/android/support/customtabs/CustomTabsIntent.java index e8ad81d7b464a..952be0d5ec238 100644 --- a/chrome/android/java/src/android/support/customtabs/CustomTabsIntent.java +++ b/chrome/android/java/src/android/support/customtabs/CustomTabsIntent.java @@ -57,6 +57,14 @@ public class CustomTabsIntent { public static final String EXTRA_ACTION_BUTTON_BUNDLE = "android.support.customtabs.extra.ACTION_BUTTON_BUNDLE"; + /** + * List used for adding items to the top and bottom action bars. The client should + * provide an ID, a description, an icon {@link Bitmap} for each item. They may also provide a + * {@link PendingIntent} if the item is a button. + */ + public static final String EXTRA_ACTION_BAR_ITEMS = + "android.support.customtabs.extra.ACTION_BAR_ITEMS"; + /** * Key that specifies the {@link Bitmap} to be used as the image source for the action button. */ diff --git a/chrome/android/java/src/org/chromium/chrome/browser/customtabs/CustomButtonParams.java b/chrome/android/java/src/org/chromium/chrome/browser/customtabs/CustomButtonParams.java index aa28292dca9e3..c842698ba78b9 100644 --- a/chrome/android/java/src/org/chromium/chrome/browser/customtabs/CustomButtonParams.java +++ b/chrome/android/java/src/org/chromium/chrome/browser/customtabs/CustomButtonParams.java @@ -5,7 +5,6 @@ package org.chromium.chrome.browser.customtabs; import android.app.PendingIntent; -import android.app.PendingIntent.CanceledException; import android.content.Context; import android.content.Intent; import android.content.res.Resources; @@ -16,8 +15,11 @@ import android.support.annotation.NonNull; import android.support.customtabs.CustomTabsIntent; import android.text.TextUtils; +import android.view.Gravity; import android.view.LayoutInflater; import android.view.View; +import android.view.View.OnClickListener; +import android.view.View.OnLongClickListener; import android.view.ViewGroup; import android.widget.ImageButton; @@ -25,6 +27,7 @@ import org.chromium.chrome.R; import org.chromium.chrome.browser.util.IntentUtils; import org.chromium.chrome.browser.widget.TintedDrawable; +import org.chromium.ui.widget.Toast; import java.util.ArrayList; import java.util.HashSet; @@ -108,9 +111,10 @@ PendingIntent getPendingIntent() { * Builds an {@link ImageButton} from the data in this params. Generated buttons should be * placed on the bottom bar. The button's tag will be its id. * @param parent The parent that the inflated {@link ImageButton}. + * @param listener {@link OnClickListener} that should be used with the button. * @return Parsed list of {@link CustomButtonParams}, which is empty if the input is invalid. */ - ImageButton buildBottomBarButton(Context context, ViewGroup parent) { + ImageButton buildBottomBarButton(Context context, ViewGroup parent, OnClickListener listener) { if (mIsOnToolbar) return null; ImageButton button = (ImageButton) LayoutInflater.from(context) @@ -121,18 +125,26 @@ ImageButton buildBottomBarButton(Context context, ViewGroup parent) { if (mPendingIntent == null) { button.setEnabled(false); } else { - // TODO(ianwen): add UMA for button clicking. - button.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - try { - mPendingIntent.send(); - } catch (CanceledException e) { - Log.e(TAG, "CanceledException while sending pending intent in custom tab"); - } - } - }); + button.setOnClickListener(listener); } + button.setOnLongClickListener(new OnLongClickListener() { + @Override + public boolean onLongClick(View view) { + final int screenWidth = view.getResources().getDisplayMetrics().widthPixels; + final int[] screenPos = new int[2]; + view.getLocationOnScreen(screenPos); + final int width = view.getWidth(); + + Toast toast = Toast.makeText( + view.getContext(), view.getContentDescription(), Toast.LENGTH_SHORT); + toast.setGravity(Gravity.BOTTOM | Gravity.END, + screenWidth - screenPos[0] - width / 2, + view.getResources().getDimensionPixelSize( + R.dimen.toolbar_height_no_shadow)); + toast.show(); + return true; + } + }); return button; } @@ -148,13 +160,11 @@ static List fromIntent(Context context, Intent intent) { Bundle singleBundle = IntentUtils.safeGetBundleExtra(intent, CustomTabsIntent.EXTRA_ACTION_BUTTON_BUNDLE); ArrayList bundleList = IntentUtils.getParcelableArrayListExtra(intent, - CustomTabsIntent.EXTRA_ACTION_BUTTON_BUNDLE); + CustomTabsIntent.EXTRA_ACTION_BAR_ITEMS); boolean tinted = IntentUtils.safeGetBooleanExtra(intent, CustomTabsIntent.EXTRA_TINT_ACTION_BUTTON, false); - if (singleBundle != null) { - CustomButtonParams params = fromBundle(context, singleBundle, tinted, false); - paramsList.add(params); - } else if (bundleList != null) { + if (singleBundle != null) paramsList.add(fromBundle(context, singleBundle, tinted, false)); + if (bundleList != null) { Set ids = new HashSet<>(); for (Bundle bundle : bundleList) { CustomButtonParams params = fromBundle(context, bundle, tinted, true); diff --git a/chrome/android/java/src/org/chromium/chrome/browser/customtabs/CustomTabActivity.java b/chrome/android/java/src/org/chromium/chrome/browser/customtabs/CustomTabActivity.java index 162e346c6b096..e3a49c7d466e8 100644 --- a/chrome/android/java/src/org/chromium/chrome/browser/customtabs/CustomTabActivity.java +++ b/chrome/android/java/src/org/chromium/chrome/browser/customtabs/CustomTabActivity.java @@ -4,6 +4,8 @@ package org.chromium.chrome.browser.customtabs; +import android.app.PendingIntent; +import android.app.PendingIntent.CanceledException; import android.content.Intent; import android.graphics.Bitmap; import android.net.Uri; @@ -482,6 +484,7 @@ private void updateBottomBarButton(CustomButtonParams params) { * Inflates the bottom bar {@link ViewStub} and its shadow, and populates it with items. */ private void showBottomBarIfNecessary() { + // TODO (yusufo): Find a better place for the layout code here and in CustomButtonParams. // TODO (ianwen): if button icon is too wide, show them in overflow menu instead. If button // id is not specified, the overflow sequence should be toolbar -> bottom bar -> menu. if (!mIntentDataProvider.shouldShowBottomBar()) return; @@ -501,7 +504,24 @@ private void showBottomBarIfNecessary() { List items = mIntentDataProvider.getCustomButtonsOnBottombar(); for (CustomButtonParams params : items) { if (params.showOnToolbar()) continue; - ImageButton button = params.buildBottomBarButton(this, bottomBar); + final PendingIntent pendingIntent = params.getPendingIntent(); + OnClickListener clickListener = null; + if (pendingIntent != null) { + clickListener = new OnClickListener() { + @Override + public void onClick(View v) { + Intent addedIntent = new Intent(); + addedIntent.setData(Uri.parse(getActivityTab().getUrl())); + try { + pendingIntent.send(CustomTabActivity.this, 0, addedIntent, null, null); + } catch (CanceledException e) { + Log.e(TAG, + "CanceledException while sending pending intent in custom tab"); + } + } + }; + } + ImageButton button = params.buildBottomBarButton(this, bottomBar, clickListener); bottomBar.addView(button); } } diff --git a/chrome/android/javatests/src/org/chromium/chrome/browser/customtabs/CustomTabActivityTest.java b/chrome/android/javatests/src/org/chromium/chrome/browser/customtabs/CustomTabActivityTest.java index 04510b8087779..147955e536bb1 100644 --- a/chrome/android/javatests/src/org/chromium/chrome/browser/customtabs/CustomTabActivityTest.java +++ b/chrome/android/javatests/src/org/chromium/chrome/browser/customtabs/CustomTabActivityTest.java @@ -498,7 +498,7 @@ public void testBottomBar() throws InterruptedException { Bundle bundle = makeBottomBarBundle(i, expectedIcon, Integer.toString(i)); bundles.add(bundle); } - intent.putExtra(CustomTabsIntent.EXTRA_ACTION_BUTTON_BUNDLE, bundles); + intent.putExtra(CustomTabsIntent.EXTRA_ACTION_BAR_ITEMS, bundles); startCustomTabActivityWithIntent(intent); ViewGroup bottomBar = (ViewGroup) getActivity().findViewById(R.id.bottombar);