Chromium Code Reviews| Index: chrome/android/java/src/org/chromium/chrome/browser/appmenu/AppMenuAdapter.java |
| diff --git a/chrome/android/java/src/org/chromium/chrome/browser/appmenu/AppMenuAdapter.java b/chrome/android/java/src/org/chromium/chrome/browser/appmenu/AppMenuAdapter.java |
| index 2518f4b0163b2ba0a8b05f7a02046d2bd50bf110..29169e7c61409d816c9c6503e0f72d4f43f2ef8e 100644 |
| --- a/chrome/android/java/src/org/chromium/chrome/browser/appmenu/AppMenuAdapter.java |
| +++ b/chrome/android/java/src/org/chromium/chrome/browser/appmenu/AppMenuAdapter.java |
| @@ -9,6 +9,7 @@ import android.animation.AnimatorListenerAdapter; |
| import android.animation.AnimatorSet; |
| import android.animation.ObjectAnimator; |
| import android.graphics.drawable.Drawable; |
| +import android.graphics.drawable.LayerDrawable; |
| import android.text.TextUtils; |
| import android.view.LayoutInflater; |
| import android.view.MenuItem; |
| @@ -97,12 +98,23 @@ class AppMenuAdapter extends BaseAdapter { |
| private final LayoutInflater mInflater; |
| private final List<MenuItem> mMenuItems; |
| private final int mNumMenuItems; |
| + private final int mHighlightedItemId; |
| + private final int mHighlightedColor; |
| private final float mDpToPx; |
| - public AppMenuAdapter(AppMenu appMenu, List<MenuItem> menuItems, LayoutInflater inflater) { |
| + // Cached drawables to avoid animation glitches if views get rebuilt. |
| + private Drawable mCachedStandardBackgroundDrawable; |
|
David Trainor- moved to gerrit
2017/03/27 21:58:57
Remove.
|
| + private Drawable mCachedHighlightIconDrawable; |
| + private Drawable mCachedHighlightRowDrawable; |
| + |
| + public AppMenuAdapter(AppMenu appMenu, List<MenuItem> menuItems, LayoutInflater inflater, |
| + int highlightedItemId) { |
| mAppMenu = appMenu; |
| mMenuItems = menuItems; |
| mInflater = inflater; |
| + mHighlightedItemId = highlightedItemId; |
| + mHighlightedColor = ApiCompatibilityUtils.getColor( |
| + mInflater.getContext().getResources(), R.color.app_menu_highlight_color_light); |
| mNumMenuItems = menuItems.size(); |
| mDpToPx = inflater.getContext().getResources().getDisplayMetrics().density; |
| } |
| @@ -161,6 +173,7 @@ class AppMenuAdapter extends BaseAdapter { |
| convertView = mInflater.inflate(R.layout.menu_item, parent, false); |
| holder.text = (TextView) convertView.findViewById(R.id.menu_item_text); |
| holder.image = (AppMenuItemIcon) convertView.findViewById(R.id.menu_item_icon); |
| + holder.background = convertView.getBackground(); |
| convertView.setTag(holder); |
| convertView.setTag(R.id.menu_item_enter_anim_id, |
| buildStandardItemEnterAnimator(convertView, position)); |
| @@ -180,6 +193,7 @@ class AppMenuAdapter extends BaseAdapter { |
| holder.text = (TextView) convertView.findViewById(R.id.menu_item_text); |
| holder.image = (AppMenuItemIcon) convertView.findViewById(R.id.menu_item_icon); |
| holder.summary = (TextView) convertView.findViewById(R.id.menu_item_summary); |
| + holder.background = convertView.getBackground(); |
| convertView.setTag(holder); |
| convertView.setTag(R.id.menu_item_enter_anim_id, |
| buildStandardItemEnterAnimator(convertView, position)); |
| @@ -217,7 +231,9 @@ class AppMenuAdapter extends BaseAdapter { |
| holder = new TitleButtonMenuItemViewHolder(); |
| convertView = mInflater.inflate(R.layout.title_button_menu_item, parent, false); |
| holder.title = (TextView) convertView.findViewById(R.id.title); |
| - holder.button = (TintedImageButton) convertView.findViewById(R.id.button); |
| + holder.background = convertView.getBackground(); |
| + holder.button.view = (TintedImageButton) convertView.findViewById(R.id.button); |
| + holder.button.background = holder.button.view.getBackground(); |
| View animatedView = convertView; |
| @@ -239,10 +255,10 @@ class AppMenuAdapter extends BaseAdapter { |
| }); |
| if (item.getSubMenu().getItem(1).getIcon() != null) { |
| - holder.button.setVisibility(View.VISIBLE); |
| + holder.button.view.setVisibility(View.VISIBLE); |
| setupImageButton(holder.button, item.getSubMenu().getItem(1)); |
| } else { |
| - holder.button.setVisibility(View.GONE); |
| + holder.button.view.setVisibility(View.GONE); |
| } |
| convertView.setFocusable(false); |
| convertView.setEnabled(false); |
| @@ -251,32 +267,39 @@ class AppMenuAdapter extends BaseAdapter { |
| default: |
| assert false : "Unexpected MenuItem type"; |
| } |
| + |
| + checkRowHighlight(item.getItemId(), convertView); |
| + |
| return convertView; |
| } |
| - private void setupImageButton(TintedImageButton button, final MenuItem item) { |
| + private void setupImageButton(RowItemButton button, final MenuItem item) { |
| // Store and recover the level of image as button.setimageDrawable |
| // resets drawable to default level. |
| + TintedImageButton view = button.view; |
| + |
| int currentLevel = item.getIcon().getLevel(); |
| - button.setImageDrawable(item.getIcon()); |
| + view.setImageDrawable(item.getIcon()); |
| item.getIcon().setLevel(currentLevel); |
| if (item.isChecked()) { |
| - button.setTint(ApiCompatibilityUtils.getColorStateList( |
| - button.getResources(), R.color.blue_mode_tint)); |
| + view.setTint(ApiCompatibilityUtils.getColorStateList( |
| + view.getResources(), R.color.blue_mode_tint)); |
| } |
| - button.setEnabled(item.isEnabled()); |
| - button.setFocusable(item.isEnabled()); |
| - button.setContentDescription(item.getTitleCondensed()); |
| + view.setEnabled(item.isEnabled()); |
| + view.setFocusable(item.isEnabled()); |
| + view.setContentDescription(item.getTitleCondensed()); |
| - button.setOnClickListener(new OnClickListener() { |
| + view.setOnClickListener(new OnClickListener() { |
| @Override |
| public void onClick(View v) { |
| mAppMenu.onItemClick(item); |
| } |
| }); |
| + checkItemHighlight(item.getItemId(), view, button.background, true); |
| + |
| // Menu items may be hidden by command line flags before they get to this point. |
| - button.setVisibility(item.isVisible() ? View.VISIBLE : View.GONE); |
| + view.setVisibility(item.isVisible() ? View.VISIBLE : View.GONE); |
| } |
| private void setupStandardMenuItemViewHolder(StandardMenuItemViewHolder holder, |
| @@ -336,21 +359,22 @@ class AppMenuAdapter extends BaseAdapter { |
| * This builds an {@link Animator} for the enter animation of icon row menu items. This means |
| * it will animate the alpha from 0 to 1 and translate the views from 10dp to 0dp on the x axis. |
| * |
| - * @param views The list if icons in the menu item that should be animated. |
| - * @return The {@link Animator}. |
| + * @param buttons The list of icons in the menu item that should be animated. |
| + * @return The {@link Animator}. |
| */ |
| - private Animator buildIconItemEnterAnimator(final ImageView[] views) { |
| + private Animator buildIconItemEnterAnimator(final RowItemButton[] buttons) { |
| final boolean rtl = LocalizationUtils.isLayoutRtl(); |
| final float offsetXPx = ENTER_STANDARD_ITEM_OFFSET_X_DP * mDpToPx * (rtl ? -1.f : 1.f); |
| - final int maxViewsToAnimate = views.length; |
| + final int maxViewsToAnimate = buttons.length; |
| AnimatorSet animation = new AnimatorSet(); |
| AnimatorSet.Builder builder = null; |
| for (int i = 0; i < maxViewsToAnimate; i++) { |
| final int startDelay = ENTER_ITEM_ADDL_DELAY_MS * i; |
| - Animator alpha = ObjectAnimator.ofFloat(views[i], View.ALPHA, 0.f, 1.f); |
| - Animator translate = ObjectAnimator.ofFloat(views[i], View.TRANSLATION_X, offsetXPx, 0); |
| + ImageView view = buttons[i].view; |
| + Animator alpha = ObjectAnimator.ofFloat(view, View.ALPHA, 0.f, 1.f); |
| + Animator translate = ObjectAnimator.ofFloat(view, View.TRANSLATION_X, offsetXPx, 0); |
| alpha.setStartDelay(startDelay); |
| translate.setStartDelay(startDelay); |
| alpha.setDuration(ENTER_ITEM_DURATION_MS); |
| @@ -370,7 +394,7 @@ class AppMenuAdapter extends BaseAdapter { |
| @Override |
| public void onAnimationStart(Animator animation) { |
| for (int i = 0; i < maxViewsToAnimate; i++) { |
| - views[i].setAlpha(0.f); |
| + buttons[i].view.setAlpha(0.f); |
| } |
| } |
| }); |
| @@ -385,11 +409,14 @@ class AppMenuAdapter extends BaseAdapter { |
| || ((RowItemViewHolder) convertView.getTag()).buttons.length != numItems) { |
| holder = new RowItemViewHolder(numItems); |
| convertView = mInflater.inflate(R.layout.icon_row_menu_item, parent, false); |
| + holder.background = convertView.getBackground(); |
| // Save references to all the buttons. |
| for (int i = 0; i < numItems; i++) { |
| - holder.buttons[i] = |
| + TintedImageButton view = |
| (TintedImageButton) convertView.findViewById(BUTTON_IDS[i]); |
| + holder.buttons[i].view = view; |
| + holder.buttons[i].background = view.getBackground(); |
| } |
| // Remove unused menu items. |
| @@ -412,7 +439,46 @@ class AppMenuAdapter extends BaseAdapter { |
| return convertView; |
| } |
| - static class StandardMenuItemViewHolder { |
| + private void checkRowHighlight(int itemId, View view) { |
| + if (mHighlightedItemId == 0) return; |
| + |
| + Object tag = view.getTag(); |
| + if (tag == null || !(tag instanceof MenuItemViewHolder)) return; |
| + |
| + Drawable background = ((MenuItemViewHolder) tag).background; |
| + |
| + checkItemHighlight(itemId, view, background, false); |
| + } |
| + |
| + private void checkItemHighlight( |
| + int itemId, View view, Drawable viewBackground, boolean isIcon) { |
| + if (mHighlightedItemId == 0) return; |
| + |
| + if (mHighlightedItemId == itemId) { |
| + view.setBackground(new LayerDrawable( |
| + new Drawable[] {getHighlightDrawable(isIcon), viewBackground})); |
| + } else { |
| + view.setBackground(viewBackground); |
| + } |
| + } |
| + |
| + private Drawable getHighlightDrawable(boolean isIcon) { |
| + if (isIcon) { |
| + if (mCachedHighlightIconDrawable == null) { |
| + mCachedHighlightIconDrawable = new IconHighlightDrawable(mHighlightedColor); |
| + } |
| + return mCachedHighlightIconDrawable; |
| + } else { |
| + if (mCachedHighlightRowDrawable == null) { |
| + mCachedHighlightRowDrawable = new RowHighlightDrawable(mHighlightedColor); |
| + } |
| + return mCachedHighlightRowDrawable; |
| + } |
| + } |
| + |
| + static class MenuItemViewHolder { public Drawable background; } |
| + |
| + static class StandardMenuItemViewHolder extends MenuItemViewHolder { |
| public TextView text; |
| public AppMenuItemIcon image; |
| } |
| @@ -421,16 +487,24 @@ class AppMenuAdapter extends BaseAdapter { |
| public TextView summary; |
| } |
| - private static class RowItemViewHolder { |
| - public TintedImageButton[] buttons; |
| + static class RowItemButton { |
| + public TintedImageButton view; |
| + public Drawable background; |
| + } |
| + |
| + private static class RowItemViewHolder extends MenuItemViewHolder { |
| + public RowItemButton[] buttons; |
| RowItemViewHolder(int numButtons) { |
| - buttons = new TintedImageButton[numButtons]; |
| + buttons = new RowItemButton[numButtons]; |
| + for (int i = 0; i < numButtons; i++) { |
| + buttons[i] = new RowItemButton(); |
| + } |
| } |
| } |
| - static class TitleButtonMenuItemViewHolder { |
| + static class TitleButtonMenuItemViewHolder extends MenuItemViewHolder { |
| public TextView title; |
| - public TintedImageButton button; |
| + public RowItemButton button = new RowItemButton(); |
| } |
| } |