Skip to content
This repository was archived by the owner on Apr 3, 2020. It is now read-only.

Commit 92d7753

Browse files
yusufoCommit bot
yusufo
authored and
Commit bot
committed
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}
1 parent 070dd8c commit 92d7753

File tree

4 files changed

+58
-20
lines changed

4 files changed

+58
-20
lines changed

chrome/android/java/src/android/support/customtabs/CustomTabsIntent.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,14 @@ public class CustomTabsIntent {
5757
public static final String EXTRA_ACTION_BUTTON_BUNDLE =
5858
"android.support.customtabs.extra.ACTION_BUTTON_BUNDLE";
5959

60+
/**
61+
* List<Bundle> used for adding items to the top and bottom action bars. The client should
62+
* provide an ID, a description, an icon {@link Bitmap} for each item. They may also provide a
63+
* {@link PendingIntent} if the item is a button.
64+
*/
65+
public static final String EXTRA_ACTION_BAR_ITEMS =
66+
"android.support.customtabs.extra.ACTION_BAR_ITEMS";
67+
6068
/**
6169
* Key that specifies the {@link Bitmap} to be used as the image source for the action button.
6270
*/

chrome/android/java/src/org/chromium/chrome/browser/customtabs/CustomButtonParams.java

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
package org.chromium.chrome.browser.customtabs;
66

77
import android.app.PendingIntent;
8-
import android.app.PendingIntent.CanceledException;
98
import android.content.Context;
109
import android.content.Intent;
1110
import android.content.res.Resources;
@@ -16,15 +15,19 @@
1615
import android.support.annotation.NonNull;
1716
import android.support.customtabs.CustomTabsIntent;
1817
import android.text.TextUtils;
18+
import android.view.Gravity;
1919
import android.view.LayoutInflater;
2020
import android.view.View;
21+
import android.view.View.OnClickListener;
22+
import android.view.View.OnLongClickListener;
2123
import android.view.ViewGroup;
2224
import android.widget.ImageButton;
2325

2426
import org.chromium.base.Log;
2527
import org.chromium.chrome.R;
2628
import org.chromium.chrome.browser.util.IntentUtils;
2729
import org.chromium.chrome.browser.widget.TintedDrawable;
30+
import org.chromium.ui.widget.Toast;
2831

2932
import java.util.ArrayList;
3033
import java.util.HashSet;
@@ -108,9 +111,10 @@ PendingIntent getPendingIntent() {
108111
* Builds an {@link ImageButton} from the data in this params. Generated buttons should be
109112
* placed on the bottom bar. The button's tag will be its id.
110113
* @param parent The parent that the inflated {@link ImageButton}.
114+
* @param listener {@link OnClickListener} that should be used with the button.
111115
* @return Parsed list of {@link CustomButtonParams}, which is empty if the input is invalid.
112116
*/
113-
ImageButton buildBottomBarButton(Context context, ViewGroup parent) {
117+
ImageButton buildBottomBarButton(Context context, ViewGroup parent, OnClickListener listener) {
114118
if (mIsOnToolbar) return null;
115119

116120
ImageButton button = (ImageButton) LayoutInflater.from(context)
@@ -121,18 +125,26 @@ ImageButton buildBottomBarButton(Context context, ViewGroup parent) {
121125
if (mPendingIntent == null) {
122126
button.setEnabled(false);
123127
} else {
124-
// TODO(ianwen): add UMA for button clicking.
125-
button.setOnClickListener(new View.OnClickListener() {
126-
@Override
127-
public void onClick(View v) {
128-
try {
129-
mPendingIntent.send();
130-
} catch (CanceledException e) {
131-
Log.e(TAG, "CanceledException while sending pending intent in custom tab");
132-
}
133-
}
134-
});
128+
button.setOnClickListener(listener);
135129
}
130+
button.setOnLongClickListener(new OnLongClickListener() {
131+
@Override
132+
public boolean onLongClick(View view) {
133+
final int screenWidth = view.getResources().getDisplayMetrics().widthPixels;
134+
final int[] screenPos = new int[2];
135+
view.getLocationOnScreen(screenPos);
136+
final int width = view.getWidth();
137+
138+
Toast toast = Toast.makeText(
139+
view.getContext(), view.getContentDescription(), Toast.LENGTH_SHORT);
140+
toast.setGravity(Gravity.BOTTOM | Gravity.END,
141+
screenWidth - screenPos[0] - width / 2,
142+
view.getResources().getDimensionPixelSize(
143+
R.dimen.toolbar_height_no_shadow));
144+
toast.show();
145+
return true;
146+
}
147+
});
136148
return button;
137149
}
138150

@@ -148,13 +160,11 @@ static List<CustomButtonParams> fromIntent(Context context, Intent intent) {
148160
Bundle singleBundle = IntentUtils.safeGetBundleExtra(intent,
149161
CustomTabsIntent.EXTRA_ACTION_BUTTON_BUNDLE);
150162
ArrayList<Bundle> bundleList = IntentUtils.getParcelableArrayListExtra(intent,
151-
CustomTabsIntent.EXTRA_ACTION_BUTTON_BUNDLE);
163+
CustomTabsIntent.EXTRA_ACTION_BAR_ITEMS);
152164
boolean tinted = IntentUtils.safeGetBooleanExtra(intent,
153165
CustomTabsIntent.EXTRA_TINT_ACTION_BUTTON, false);
154-
if (singleBundle != null) {
155-
CustomButtonParams params = fromBundle(context, singleBundle, tinted, false);
156-
paramsList.add(params);
157-
} else if (bundleList != null) {
166+
if (singleBundle != null) paramsList.add(fromBundle(context, singleBundle, tinted, false));
167+
if (bundleList != null) {
158168
Set<Integer> ids = new HashSet<>();
159169
for (Bundle bundle : bundleList) {
160170
CustomButtonParams params = fromBundle(context, bundle, tinted, true);

chrome/android/java/src/org/chromium/chrome/browser/customtabs/CustomTabActivity.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
package org.chromium.chrome.browser.customtabs;
66

7+
import android.app.PendingIntent;
8+
import android.app.PendingIntent.CanceledException;
79
import android.content.Intent;
810
import android.graphics.Bitmap;
911
import android.net.Uri;
@@ -482,6 +484,7 @@ private void updateBottomBarButton(CustomButtonParams params) {
482484
* Inflates the bottom bar {@link ViewStub} and its shadow, and populates it with items.
483485
*/
484486
private void showBottomBarIfNecessary() {
487+
// TODO (yusufo): Find a better place for the layout code here and in CustomButtonParams.
485488
// TODO (ianwen): if button icon is too wide, show them in overflow menu instead. If button
486489
// id is not specified, the overflow sequence should be toolbar -> bottom bar -> menu.
487490
if (!mIntentDataProvider.shouldShowBottomBar()) return;
@@ -501,7 +504,24 @@ private void showBottomBarIfNecessary() {
501504
List<CustomButtonParams> items = mIntentDataProvider.getCustomButtonsOnBottombar();
502505
for (CustomButtonParams params : items) {
503506
if (params.showOnToolbar()) continue;
504-
ImageButton button = params.buildBottomBarButton(this, bottomBar);
507+
final PendingIntent pendingIntent = params.getPendingIntent();
508+
OnClickListener clickListener = null;
509+
if (pendingIntent != null) {
510+
clickListener = new OnClickListener() {
511+
@Override
512+
public void onClick(View v) {
513+
Intent addedIntent = new Intent();
514+
addedIntent.setData(Uri.parse(getActivityTab().getUrl()));
515+
try {
516+
pendingIntent.send(CustomTabActivity.this, 0, addedIntent, null, null);
517+
} catch (CanceledException e) {
518+
Log.e(TAG,
519+
"CanceledException while sending pending intent in custom tab");
520+
}
521+
}
522+
};
523+
}
524+
ImageButton button = params.buildBottomBarButton(this, bottomBar, clickListener);
505525
bottomBar.addView(button);
506526
}
507527
}

chrome/android/javatests/src/org/chromium/chrome/browser/customtabs/CustomTabActivityTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -498,7 +498,7 @@ public void testBottomBar() throws InterruptedException {
498498
Bundle bundle = makeBottomBarBundle(i, expectedIcon, Integer.toString(i));
499499
bundles.add(bundle);
500500
}
501-
intent.putExtra(CustomTabsIntent.EXTRA_ACTION_BUTTON_BUNDLE, bundles);
501+
intent.putExtra(CustomTabsIntent.EXTRA_ACTION_BAR_ITEMS, bundles);
502502
startCustomTabActivityWithIntent(intent);
503503

504504
ViewGroup bottomBar = (ViewGroup) getActivity().findViewById(R.id.bottombar);

0 commit comments

Comments
 (0)