Fix to bottom bar behavior

- Add a new extra for the bottom bar buttons
- Use the above to distinguish between old and new API calls so that
  all permutations of support lib and chrome versions work
- Add accesibility support for bottom bar
- Have the bottom bar pending intents sent back with the current url

BUG=573683

Review URL: https://codereview.chromium.org/1591053003

Cr-Commit-Position: refs/heads/master@{#369907}
diff --git a/chrome/android/java/src/android/support/customtabs/CustomTabsIntent.java b/chrome/android/java/src/android/support/customtabs/CustomTabsIntent.java
index e8ad81d..952be0d 100644
--- a/chrome/android/java/src/android/support/customtabs/CustomTabsIntent.java
+++ b/chrome/android/java/src/android/support/customtabs/CustomTabsIntent.java
@@ -58,6 +58,14 @@
             "android.support.customtabs.extra.ACTION_BUTTON_BUNDLE";
 
     /**
+     * List<Bundle> 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.
      */
     public static final String KEY_ICON = "android.support.customtabs.customaction.ICON";
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 aa28292..c842698 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 @@
      * 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 @@
         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 @@
         Bundle singleBundle = IntentUtils.safeGetBundleExtra(intent,
                 CustomTabsIntent.EXTRA_ACTION_BUTTON_BUNDLE);
         ArrayList<Bundle> 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<Integer> 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 162e346..e3a49c7 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 @@
      * 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 @@
         List<CustomButtonParams> 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 04510b8..147955e 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 @@
             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);