Chromium Code Reviews| Index: chrome/android/java/src/org/chromium/chrome/browser/contextmenu/TabularContextMenuUi.java |
| diff --git a/chrome/android/java/src/org/chromium/chrome/browser/contextmenu/TabularContextMenuUi.java b/chrome/android/java/src/org/chromium/chrome/browser/contextmenu/TabularContextMenuUi.java |
| index 09428c07607022ac3b53855c70553f103e64cdfe..2b4e25889a84dd63e6704aa2316952a93cb41c9b 100644 |
| --- a/chrome/android/java/src/org/chromium/chrome/browser/contextmenu/TabularContextMenuUi.java |
| +++ b/chrome/android/java/src/org/chromium/chrome/browser/contextmenu/TabularContextMenuUi.java |
| @@ -5,22 +5,24 @@ |
| package org.chromium.chrome.browser.contextmenu; |
| import android.app.Activity; |
| -import android.app.Dialog; |
| import android.content.DialogInterface; |
| import android.content.res.Resources; |
| import android.graphics.Bitmap; |
| import android.graphics.Canvas; |
| +import android.graphics.Color; |
| import android.graphics.Shader; |
| import android.graphics.drawable.BitmapDrawable; |
| +import android.graphics.drawable.ColorDrawable; |
| import android.graphics.drawable.Drawable; |
| import android.support.design.widget.TabLayout; |
| -import android.support.v4.view.ViewPager; |
| import android.support.v7.app.AlertDialog; |
| import android.text.TextUtils; |
| import android.util.Pair; |
| import android.view.LayoutInflater; |
| import android.view.View; |
| import android.view.ViewGroup; |
| +import android.view.ViewGroup.LayoutParams; |
| +import android.view.Window; |
| import android.widget.AdapterView; |
| import android.widget.BaseAdapter; |
| import android.widget.ImageView; |
| @@ -31,6 +33,8 @@ import org.chromium.base.ApiCompatibilityUtils; |
| import org.chromium.base.Callback; |
| import org.chromium.base.VisibleForTesting; |
| import org.chromium.chrome.R; |
| +import org.chromium.chrome.browser.widget.ContextMenuDialog; |
| +import org.chromium.content.browser.RenderCoordinates; |
| import java.util.ArrayList; |
| import java.util.List; |
| @@ -39,71 +43,98 @@ import java.util.List; |
| * A custom dialog that separates each group into separate tabs. It uses a dialog instead. |
| */ |
| public class TabularContextMenuUi implements ContextMenuUi, AdapterView.OnItemClickListener { |
| - private Dialog mDialog; |
| + private static final double MAX_WIDTH_PROPORTION = 0.75; |
| + |
| + private ContextMenuDialog mContextMenuDialog; |
| private Callback<Integer> mCallback; |
| private int mMenuItemHeight; |
| private ImageView mHeaderImageView; |
| private Runnable mOnShareItemClicked; |
| + private View mPagerView; |
| + private RenderCoordinates mRenderCoordinates; |
| public TabularContextMenuUi(Runnable onShareItemClicked) { |
| mOnShareItemClicked = onShareItemClicked; |
| } |
| @Override |
| - public void displayMenu(Activity activity, ContextMenuParams params, |
| + public void displayMenu(final Activity activity, ContextMenuParams params, |
| List<Pair<Integer, List<ContextMenuItem>>> items, Callback<Integer> onItemClicked, |
| final Runnable onMenuShown, final Runnable onMenuClosed) { |
| mCallback = onItemClicked; |
| - mDialog = createDialog(activity, params, items); |
| - |
| - mDialog.getWindow().setBackgroundDrawable(ApiCompatibilityUtils.getDrawable( |
| - activity.getResources(), R.drawable.white_with_rounded_corners)); |
| + mContextMenuDialog = createContextMenuDialog(activity, params, items); |
| - mDialog.setOnShowListener(new DialogInterface.OnShowListener() { |
| + mContextMenuDialog.setOnShowListener(new DialogInterface.OnShowListener() { |
| @Override |
| public void onShow(DialogInterface dialogInterface) { |
| onMenuShown.run(); |
| } |
| }); |
| - mDialog.setOnDismissListener(new DialogInterface.OnDismissListener() { |
| + mContextMenuDialog.setOnDismissListener(new DialogInterface.OnDismissListener() { |
| @Override |
| public void onDismiss(DialogInterface dialogInterface) { |
| onMenuClosed.run(); |
| } |
| }); |
| - mDialog.show(); |
| + Window dialogWindow = mContextMenuDialog.getWindow(); |
| + dialogWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); |
| + dialogWindow.setLayout(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); |
|
Theresa
2017/05/24 22:50:50
Can contextMenuDialog be responsible for this as w
Daniel Park
2017/05/25 17:12:36
Done.
|
| + |
| + float density = Resources.getSystem().getDisplayMetrics().density; |
| + final float touchPointX = params.getTriggeringTouchX() * density; |
| + final float touchPointY = params.getTriggeringTouchY() * density; |
| + |
| + mPagerView.setVisibility(View.INVISIBLE); |
|
Theresa
2017/05/24 22:50:50
nit: let's set the view to invisible in ContextMen
Daniel Park
2017/05/25 17:12:36
Done.
|
| + mContextMenuDialog.setupEnterAnimationAndShow( |
| + mPagerView, touchPointX, touchPointY, activity, mRenderCoordinates); |
| } |
| /** |
| * Returns the fully complete dialog based off the params and the itemGroups. |
| + * |
| * @param activity Used to inflate the dialog. |
| * @param params Used to get the header title. |
| * @param itemGroups If there is more than one group it will create a paged view. |
| * @return Returns a final dialog that does not have a background can be displayed using |
| * {@link AlertDialog#show()}. |
| */ |
| - private Dialog createDialog(Activity activity, ContextMenuParams params, |
| + private ContextMenuDialog createContextMenuDialog(Activity activity, ContextMenuParams params, |
| List<Pair<Integer, List<ContextMenuItem>>> itemGroups) { |
| - Dialog dialog = new Dialog(activity); |
| - dialog.setContentView(createPagerView(activity, params, itemGroups)); |
| + final ContextMenuDialog dialog = new ContextMenuDialog(activity, R.style.DialogWhenLarge); |
| + |
| + View view = LayoutInflater.from(activity).inflate(R.layout.tabular_context_menu, null); |
| + |
| + Resources resources = activity.getResources(); |
| + |
| + int contextMenuWidth = |
| + (int) Math.min(resources.getDisplayMetrics().widthPixels * MAX_WIDTH_PROPORTION, |
| + resources.getDimensionPixelSize(R.dimen.context_menu_max_width)); |
| + |
| + mPagerView = initPagerView(activity, params, itemGroups, |
| + (TabularContextMenuViewPager) view.findViewById(R.id.custom_pager)); |
| + mPagerView.getLayoutParams().width = contextMenuWidth; |
| + |
| + dialog.setContentView(view); |
| + |
| return dialog; |
| } |
| /** |
| * Creates a ViewPageAdapter based off the given list of views. |
| - * @param activity Used to inflate the new ViewPager |
| + * |
| + * @param activity Used to inflate the new ViewPager. |
| * @param params Used to get the header text. |
| * @param itemGroups The list of views to put into the ViewPager. The string is the title of the |
| - * tab |
| + * tab. |
| + * @param viewPager The viewpager to initialize. |
| * @return Returns a complete tabular context menu view. |
| */ |
| @VisibleForTesting |
| - View createPagerView(Activity activity, ContextMenuParams params, |
| - List<Pair<Integer, List<ContextMenuItem>>> itemGroups) { |
| - View view = LayoutInflater.from(activity).inflate(R.layout.tabular_context_menu, null); |
| - |
| + View initPagerView(Activity activity, ContextMenuParams params, |
| + List<Pair<Integer, List<ContextMenuItem>>> itemGroups, |
| + TabularContextMenuViewPager viewPager) { |
| List<Pair<String, ViewGroup>> viewGroups = new ArrayList<>(); |
| int maxCount = 0; |
| for (int i = 0; i < itemGroups.size(); i++) { |
| @@ -118,30 +149,23 @@ public class TabularContextMenuUi implements ContextMenuUi, AdapterView.OnItemCl |
| createContextMenuPageUi( |
| activity, params, itemGroup.second, isImageTab, maxCount))); |
| } |
| - if (itemGroups.size() == 1) { |
| - viewGroups.get(0) |
| - .second.getChildAt(0) |
| - .findViewById(R.id.context_header_layout) |
| - .setBackgroundResource(R.color.google_grey_100); |
| - } |
| - TabularContextMenuViewPager pager = |
| - (TabularContextMenuViewPager) view.findViewById(R.id.custom_pager); |
| - pager.setAdapter(new TabularContextMenuPagerAdapter(viewGroups)); |
| - |
| - TabLayout tabLayout = (TabLayout) view.findViewById(R.id.tab_layout); |
| + viewPager.setAdapter(new TabularContextMenuPagerAdapter(viewGroups)); |
| + TabLayout tabLayout = (TabLayout) viewPager.findViewById(R.id.tab_layout); |
| if (itemGroups.size() <= 1) { |
| tabLayout.setVisibility(View.GONE); |
| } else { |
| - tabLayout.setupWithViewPager((ViewPager) view.findViewById(R.id.custom_pager)); |
| + tabLayout.setBackgroundResource(R.drawable.grey_with_top_rounded_corners); |
| + tabLayout.setupWithViewPager(viewPager); |
| } |
| - return view; |
| + return viewPager; |
| } |
| /** |
| * Creates the view of a context menu. Based off the Context Type, it'll adjust the list of |
| * items and display only the ones that'll be on that specific group. |
| + * |
| * @param activity Used to get the resources of an item. |
| * @param params used to create the header text. |
| * @param items A set of Items to display in a context menu. Filtered based off the type. |
| @@ -172,7 +196,7 @@ public class TabularContextMenuUi implements ContextMenuUi, AdapterView.OnItemCl |
| @Override |
| public void run() { |
| mOnShareItemClicked.run(); |
| - mDialog.dismiss(); |
| + mContextMenuDialog.dismiss(); |
| } |
| }; |
| TabularContextMenuListAdapter listAdapter = |
| @@ -245,6 +269,7 @@ public class TabularContextMenuUi implements ContextMenuUi, AdapterView.OnItemCl |
| * To save time measuring the height, this method gets an item if the height has not been |
| * previous measured and multiplies it by count of the total amount of items. It is fine if the |
| * height too small as the ListView will scroll through the other values. |
| + * |
| * @param listView The ListView to measure the surrounding padding. |
| * @param listAdapter The adapter which contains the items within the list. |
| * @return Returns the combined height of the padding of the ListView and the approximate height |
| @@ -263,8 +288,8 @@ public class TabularContextMenuUi implements ContextMenuUi, AdapterView.OnItemCl |
| } |
| /** |
| - * When an thumbnail is retrieved for the header of an image, this will set the header to |
| - * that particular bitmap. |
| + * When an thumbnail is retrieved for the header of an image, this will set the header to that |
| + * particular bitmap. |
| */ |
| public void onImageThumbnailRetrieved(Bitmap bitmap) { |
| if (mHeaderImageView != null) { |
| @@ -274,7 +299,15 @@ public class TabularContextMenuUi implements ContextMenuUi, AdapterView.OnItemCl |
| @Override |
| public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) { |
| - mDialog.dismiss(); |
| + mContextMenuDialog.dismiss(); |
| mCallback.onResult((int) id); |
| } |
| + |
| + /** |
| + * Gives this class access to the render coordinates to allow access to the total size of the |
| + * toolbar and tab strip. |
| + */ |
| + public void setRenderCoordinates(RenderCoordinates renderCoordinates) { |
| + mRenderCoordinates = renderCoordinates; |
| + } |
| } |