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

Commit

Permalink
Fix to bottom bar behavior
Browse files Browse the repository at this point in the history
- 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}
  • Loading branch information
yusufo authored and Commit bot committed Jan 16, 2016
1 parent 070dd8c commit 92d7753
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,14 @@ public class CustomTabsIntent {
public static final String EXTRA_ACTION_BUTTON_BUNDLE =
"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.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -16,15 +15,19 @@
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;

import org.chromium.base.Log;
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;
Expand Down Expand Up @@ -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)
Expand All @@ -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;
}

Expand All @@ -148,13 +160,11 @@ static List<CustomButtonParams> fromIntent(Context context, Intent intent) {
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -501,7 +504,24 @@ private void showBottomBarIfNecessary() {
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);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 92d7753

Please sign in to comment.