Chromium Code Reviews| Index: chrome/android/java/src/org/chromium/chrome/browser/infobar/translate/TranslateMenuHelper.java |
| diff --git a/chrome/android/java/src/org/chromium/chrome/browser/infobar/translate/TranslateMenuHelper.java b/chrome/android/java/src/org/chromium/chrome/browser/infobar/translate/TranslateMenuHelper.java |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..1ec72c2d61cbb3caadd78969868d575fc968f2bb |
| --- /dev/null |
| +++ b/chrome/android/java/src/org/chromium/chrome/browser/infobar/translate/TranslateMenuHelper.java |
| @@ -0,0 +1,212 @@ |
| +// Copyright 2017 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +package org.chromium.chrome.browser.infobar.translate; |
| + |
| +import android.content.Context; |
| +import android.support.v4.content.ContextCompat; |
| +import android.view.ContextThemeWrapper; |
| +import android.view.LayoutInflater; |
| +import android.view.View; |
| +import android.view.ViewGroup; |
| +import android.widget.AdapterView; |
| +import android.widget.ArrayAdapter; |
| +import android.widget.ListPopupWindow; |
| +import android.widget.PopupWindow; |
| +import android.widget.TextView; |
| + |
| +import org.chromium.chrome.R; |
| + |
| +import java.util.List; |
| + |
| +/** |
| + * A Helper class for managing the Translate Overflow Menu. |
| + */ |
| +public class TranslateMenuHelper implements AdapterView.OnItemClickListener { |
| + public static final String EMPTY_STRING = ""; |
|
Marti Wong
2017/04/11 14:46:18
It seems I might better to set this to private.
gone
2017/04/11 20:35:22
Yeah, no reason this should be exposed.
Marti Wong
2017/04/12 02:13:42
Done.
|
| + private final TranslateMenuItemListener mItemListener; |
| + private ContextThemeWrapper mWrapper; |
| + private View mAnchorView; |
| + private TranslateMenuAdapter mAdapter; |
| + private ListPopupWindow mPopup; |
| + |
| + /** |
| + * Add a divider menu item to list |
|
gone
2017/04/11 20:35:22
Periods at the end everywhere.
Marti Wong
2017/04/12 02:13:42
Done.
|
| + * @param list The list of items to show in the menu. |
| + */ |
| + public static void addDivider(List<TranslateMenuElement> list) { |
| + list.add(new TranslateMenuElement( |
| + EMPTY_STRING, EMPTY_STRING, TranslateMenuAdapter.MENU_DIVIDER)); |
| + } |
| + |
| + /** |
| + * Add an overflow menu item to list |
| + * @param title Title of the menu item |
| + * @param id ID of the menu item |
| + * @param list The list of items to show in the menu. |
| + */ |
| + public static void addOverflowMenuItem( |
| + String title, String id, List<TranslateMenuElement> list) { |
| + list.add(new TranslateMenuElement(title, id, TranslateMenuAdapter.OVERFLOW_MENU_ITEM)); |
| + } |
| + |
| + /** |
| + * Add a language menu item to list |
| + * @param title Title of the language |
| + * @param id ISO code of the language |
| + * @param list The list of items to show in the menu. |
| + */ |
| + public static void addLanguageMenuItem( |
| + String title, String id, List<TranslateMenuElement> list) { |
| + list.add(new TranslateMenuElement(title, id, TranslateMenuAdapter.LANGUAGE_MENU_ITEM)); |
| + } |
| + |
| + /** |
| + * Interface for receiving the click event of menu item. |
| + */ |
| + public interface TranslateMenuItemListener { |
| + boolean onTranslateMenuItemClicked(String id, int type); |
| + } |
| + |
| + public TranslateMenuHelper( |
| + Context context, View anchorView, TranslateMenuItemListener itemListener) { |
| + mWrapper = new ContextThemeWrapper(context, R.style.OverflowMenuTheme); |
| + mAnchorView = anchorView; |
| + mItemListener = itemListener; |
| + } |
| + |
| + public void show(List<TranslateMenuElement> list) { |
| + if (mPopup == null) { |
| + mPopup = new ListPopupWindow(mWrapper, null, android.R.attr.popupMenuStyle); |
| + mPopup.setModal(true); |
| + mPopup.setAnchorView(mAnchorView); |
| + mPopup.setInputMethodMode(PopupWindow.INPUT_METHOD_NOT_NEEDED); |
| + |
| + // Need to explicitly set the background here. Relying on it being set in the style |
| + // caused an incorrectly drawn background. |
| + mPopup.setBackgroundDrawable( |
| + ContextCompat.getDrawable(mWrapper, R.drawable.edge_menu_bg)); |
| + |
| + int popupWidth = mWrapper.getResources().getDimensionPixelSize(R.dimen.menu_width); |
| + mPopup.setWidth(popupWidth); |
| + |
| + mAdapter = new TranslateMenuAdapter( |
| + mWrapper, R.id.menu_item_text, list, LayoutInflater.from(mWrapper)); |
| + mPopup.setAdapter(mAdapter); |
| + |
| + mPopup.setOnItemClickListener(this); |
| + } else { |
| + mAdapter.clear(); |
| + mAdapter.addAll(list); |
| + } |
| + |
| + if (!mPopup.isShowing()) { |
| + mPopup.show(); |
| + mPopup.getListView().setItemsCanFocus(true); |
| + } |
| + } |
| + |
| + @Override |
| + public void onItemClick(AdapterView<?> parent, View view, int position, long id) { |
| + if (mItemListener.onTranslateMenuItemClicked( |
| + mAdapter.getItem(position).getId(), mAdapter.getItemViewType(position))) { |
| + dismiss(); |
| + } |
| + } |
| + |
| + /** |
| + * Dismisses the app menu and cancels the drag-to-scroll if it is taking place. |
| + */ |
| + void dismiss() { |
| + if (isShowing()) { |
| + mPopup.dismiss(); |
| + } |
| + } |
| + |
| + /** |
| + * @return Whether the app menu is currently showing. |
| + */ |
| + boolean isShowing() { |
| + if (mPopup == null) { |
| + return false; |
| + } |
| + return mPopup.isShowing(); |
| + } |
| + |
| + /** |
| + * The provides the views of the menu items and dividers. |
| + */ |
| + public class TranslateMenuAdapter extends ArrayAdapter<TranslateMenuElement> { |
|
gone
2017/04/11 22:45:05
can this be static final?
Marti Wong
2017/04/12 02:13:42
Done.
|
| + public static final int MENU_DIVIDER = 0; |
| + public static final int OVERFLOW_MENU_ITEM = 1; |
| + public static final int LANGUAGE_MENU_ITEM = 2; |
| + |
| + LayoutInflater mInflater; |
| + |
| + public TranslateMenuAdapter(Context context, int textViewResourceId, |
| + List<TranslateMenuElement> items, LayoutInflater inflater) { |
| + super(context, textViewResourceId, items); |
| + mInflater = inflater; |
| + } |
| + |
| + @Override |
| + public int getItemViewType(int position) { |
| + return getItem(position).getType(); |
| + } |
| + |
| + @Override |
| + public boolean isEnabled(int position) { |
| + return getItemViewType(position) != MENU_DIVIDER; |
| + } |
| + |
| + @Override |
| + public View getView(int position, View convertView, ViewGroup parent) { |
| + switch (getItemViewType(position)) { |
| + case MENU_DIVIDER: |
| + convertView = mInflater.inflate(R.layout.translate_menu_divider, parent, false); |
| + break; |
| + case OVERFLOW_MENU_ITEM: |
| + case LANGUAGE_MENU_ITEM: |
| + convertView = mInflater.inflate(R.layout.translate_menu_item, parent, false); |
| + ((TextView) convertView.findViewById(R.id.menu_item_text)) |
| + .setText(getItem(position).toString()); |
| + break; |
| + default: |
| + assert false : "Unexpected MenuItem type"; |
| + } |
| + return convertView; |
| + } |
| + } |
| + |
| + /** |
| + * The element that goes inside the menu. |
| + */ |
| + public static class TranslateMenuElement { |
|
gone
2017/04/11 22:45:05
can this be final?
Marti Wong
2017/04/12 02:13:41
Done.
|
| + private final String mTitle; |
| + private final String mId; |
| + private final int mType; |
| + |
| + public TranslateMenuElement(String title, String id, int type) { |
| + mTitle = title; |
| + mId = id; |
| + mType = type; |
| + } |
| + |
| + public String getId() { |
| + return mId; |
| + } |
| + |
| + public int getType() { |
| + return mType; |
| + } |
| + |
| + /** |
| + * This is the text displayed in the menu item. |
| + */ |
| + @Override |
| + public String toString() { |
| + return mTitle; |
| + } |
| + } |
| +} |