| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package org.chromium.chrome.browser.contextmenu; | 5 package org.chromium.chrome.browser.contextmenu; |
| 6 | 6 |
| 7 import android.app.Activity; |
| 7 import android.app.Dialog; | 8 import android.app.Dialog; |
| 8 import android.content.Context; | |
| 9 import android.content.DialogInterface; | 9 import android.content.DialogInterface; |
| 10 import android.content.res.Resources; | 10 import android.content.res.Resources; |
| 11 import android.graphics.Bitmap; | 11 import android.graphics.Bitmap; |
| 12 import android.graphics.Canvas; | 12 import android.graphics.Canvas; |
| 13 import android.graphics.Shader; | 13 import android.graphics.Shader; |
| 14 import android.graphics.drawable.BitmapDrawable; | 14 import android.graphics.drawable.BitmapDrawable; |
| 15 import android.graphics.drawable.Drawable; | 15 import android.graphics.drawable.Drawable; |
| 16 import android.support.design.widget.TabLayout; | 16 import android.support.design.widget.TabLayout; |
| 17 import android.support.v4.view.ViewPager; | 17 import android.support.v4.view.ViewPager; |
| 18 import android.support.v7.app.AlertDialog; | 18 import android.support.v7.app.AlertDialog; |
| (...skipping 17 matching lines...) Expand all Loading... |
| 36 import java.util.List; | 36 import java.util.List; |
| 37 | 37 |
| 38 /** | 38 /** |
| 39 * A custom dialog that separates each group into separate tabs. It uses a dialo
g instead. | 39 * A custom dialog that separates each group into separate tabs. It uses a dialo
g instead. |
| 40 */ | 40 */ |
| 41 public class TabularContextMenuUi implements ContextMenuUi, AdapterView.OnItemCl
ickListener { | 41 public class TabularContextMenuUi implements ContextMenuUi, AdapterView.OnItemCl
ickListener { |
| 42 private Dialog mDialog; | 42 private Dialog mDialog; |
| 43 private Callback<Integer> mCallback; | 43 private Callback<Integer> mCallback; |
| 44 private int mMenuItemHeight; | 44 private int mMenuItemHeight; |
| 45 private ImageView mHeaderImageView; | 45 private ImageView mHeaderImageView; |
| 46 private Runnable mOnShareItemClicked; |
| 47 |
| 48 public TabularContextMenuUi(Runnable onShareItemClicked) { |
| 49 mOnShareItemClicked = onShareItemClicked; |
| 50 } |
| 46 | 51 |
| 47 @Override | 52 @Override |
| 48 public void displayMenu(Context context, ContextMenuParams params, | 53 public void displayMenu(Activity activity, ContextMenuParams params, |
| 49 List<Pair<Integer, List<ContextMenuItem>>> items, Callback<Integer>
onItemClicked, | 54 List<Pair<Integer, List<ContextMenuItem>>> items, Callback<Integer>
onItemClicked, |
| 50 final Runnable onMenuShown, final Runnable onMenuClosed) { | 55 final Runnable onMenuShown, final Runnable onMenuClosed) { |
| 51 mCallback = onItemClicked; | 56 mCallback = onItemClicked; |
| 52 mDialog = createDialog(context, params, items); | 57 mDialog = createDialog(activity, params, items); |
| 53 mDialog.getWindow().setBackgroundDrawable(ApiCompatibilityUtils.getDrawa
ble( | 58 mDialog.getWindow().setBackgroundDrawable(ApiCompatibilityUtils.getDrawa
ble( |
| 54 context.getResources(), R.drawable.bg_find_toolbar_popup)); | 59 activity.getResources(), R.drawable.bg_find_toolbar_popup)); |
| 55 | 60 |
| 56 mDialog.setOnShowListener(new DialogInterface.OnShowListener() { | 61 mDialog.setOnShowListener(new DialogInterface.OnShowListener() { |
| 57 @Override | 62 @Override |
| 58 public void onShow(DialogInterface dialogInterface) { | 63 public void onShow(DialogInterface dialogInterface) { |
| 59 onMenuShown.run(); | 64 onMenuShown.run(); |
| 60 } | 65 } |
| 61 }); | 66 }); |
| 62 | 67 |
| 63 mDialog.setOnDismissListener(new DialogInterface.OnDismissListener() { | 68 mDialog.setOnDismissListener(new DialogInterface.OnDismissListener() { |
| 64 @Override | 69 @Override |
| 65 public void onDismiss(DialogInterface dialogInterface) { | 70 public void onDismiss(DialogInterface dialogInterface) { |
| 66 onMenuClosed.run(); | 71 onMenuClosed.run(); |
| 67 } | 72 } |
| 68 }); | 73 }); |
| 69 | 74 |
| 70 mDialog.show(); | 75 mDialog.show(); |
| 71 } | 76 } |
| 72 | 77 |
| 73 /** | 78 /** |
| 74 * Returns the fully complete dialog based off the params and the itemGroups
. | 79 * Returns the fully complete dialog based off the params and the itemGroups
. |
| 75 * @param context Used to inflate the dialog. | 80 * @param activity Used to inflate the dialog. |
| 76 * @param params Used to get the header title. | 81 * @param params Used to get the header title. |
| 77 * @param itemGroups If there is more than one group it will create a paged
view. | 82 * @param itemGroups If there is more than one group it will create a paged
view. |
| 78 * @return Returns a final dialog that does not have a background can be dis
played using | 83 * @return Returns a final dialog that does not have a background can be dis
played using |
| 79 * {@link AlertDialog#show()}. | 84 * {@link AlertDialog#show()}. |
| 80 */ | 85 */ |
| 81 private Dialog createDialog(Context context, ContextMenuParams params, | 86 private Dialog createDialog(Activity activity, ContextMenuParams params, |
| 82 List<Pair<Integer, List<ContextMenuItem>>> itemGroups) { | 87 List<Pair<Integer, List<ContextMenuItem>>> itemGroups) { |
| 83 Dialog dialog = new Dialog(context); | 88 Dialog dialog = new Dialog(activity); |
| 84 dialog.setContentView(createPagerView(context, params, itemGroups)); | 89 dialog.setContentView(createPagerView(activity, params, itemGroups)); |
| 85 return dialog; | 90 return dialog; |
| 86 } | 91 } |
| 87 | 92 |
| 88 /** | 93 /** |
| 89 * Creates the view of a context menu. Based off the Context Type, it'll adj
ust the list of | 94 * Creates the view of a context menu. Based off the Context Type, it'll adj
ust the list of |
| 90 * items and display only the ones that'll be on that specific group. | 95 * items and display only the ones that'll be on that specific group. |
| 91 * @param context Used to get the resources of an item. | 96 * @param activity Used to get the resources of an item. |
| 92 * @param params used to create the header text. | 97 * @param params used to create the header text. |
| 93 * @param items A set of Items to display in a context menu. Filtered based
off the type. | 98 * @param items A set of Items to display in a context menu. Filtered based
off the type. |
| 94 * @param isImage Whether or not the view should have an image layout or not
. | 99 * @param isImage Whether or not the view should have an image layout or not
. |
| 95 * @param maxCount The maximum amount of {@link ContextMenuItem}s that could
exist in this view | 100 * @param maxCount The maximum amount of {@link ContextMenuItem}s that could
exist in this view |
| 96 * or any other views calculated in the context menu. Used t
o estimate the size | 101 * or any other views calculated in the context menu. Used t
o estimate the size |
| 97 * of the list. | 102 * of the list. |
| 98 * @return Returns a filled LinearLayout with all the context menu items. | 103 * @return Returns a filled LinearLayout with all the context menu items. |
| 99 */ | 104 */ |
| 100 @VisibleForTesting | 105 @VisibleForTesting |
| 101 ViewGroup createContextMenuPageUi(Context context, ContextMenuParams params, | 106 ViewGroup createContextMenuPageUi(Activity activity, ContextMenuParams param
s, |
| 102 List<ContextMenuItem> items, boolean isImage, int maxCount) { | 107 List<ContextMenuItem> items, boolean isImage, int maxCount) { |
| 103 ViewGroup baseLayout = (ViewGroup) LayoutInflater.from(context).inflate( | 108 ViewGroup baseLayout = (ViewGroup) LayoutInflater.from(activity).inflate
( |
| 104 R.layout.tabular_context_menu_page, null); | 109 R.layout.tabular_context_menu_page, null); |
| 105 ListView listView = (ListView) baseLayout.findViewById(R.id.selectable_i
tems); | 110 ListView listView = (ListView) baseLayout.findViewById(R.id.selectable_i
tems); |
| 106 | 111 |
| 107 if (isImage) { | 112 if (isImage) { |
| 108 displayImageHeader(baseLayout, params, context.getResources()); | 113 displayImageHeader(baseLayout, params, activity.getResources()); |
| 109 } else { | 114 } else { |
| 110 displayHeaderIfVisibleItems(params, baseLayout); | 115 displayHeaderIfVisibleItems(params, baseLayout); |
| 111 } | 116 } |
| 112 | 117 |
| 113 // Set the list adapter and get the height to display it appropriately i
n a dialog. | 118 // Set the list adapter and get the height to display it appropriately i
n a dialog. |
| 114 TabularContextMenuListAdapter listAdapter = | 119 TabularContextMenuListAdapter listAdapter = |
| 115 new TabularContextMenuListAdapter(items, context); | 120 new TabularContextMenuListAdapter(items, activity, mOnShareItemC
licked); |
| 116 ViewGroup.LayoutParams layoutParams = listView.getLayoutParams(); | 121 ViewGroup.LayoutParams layoutParams = listView.getLayoutParams(); |
| 117 layoutParams.height = measureApproximateListViewHeight(listView, listAda
pter, maxCount); | 122 layoutParams.height = measureApproximateListViewHeight(listView, listAda
pter, maxCount); |
| 118 listView.setLayoutParams(layoutParams); | 123 listView.setLayoutParams(layoutParams); |
| 119 listView.setAdapter(listAdapter); | 124 listView.setAdapter(listAdapter); |
| 120 listView.setOnItemClickListener(this); | 125 listView.setOnItemClickListener(this); |
| 121 | 126 |
| 122 return baseLayout; | 127 return baseLayout; |
| 123 } | 128 } |
| 124 | 129 |
| 125 private void displayHeaderIfVisibleItems(ContextMenuParams params, ViewGroup
baseLayout) { | 130 private void displayHeaderIfVisibleItems(ContextMenuParams params, ViewGroup
baseLayout) { |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 199 View view = listAdapter.getView(0, null, listView); | 204 View view = listAdapter.getView(0, null, listView); |
| 200 view.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UN
SPECIFIED), | 205 view.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UN
SPECIFIED), |
| 201 View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECI
FIED)); | 206 View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECI
FIED)); |
| 202 mMenuItemHeight = view.getMeasuredHeight(); | 207 mMenuItemHeight = view.getMeasuredHeight(); |
| 203 } | 208 } |
| 204 return totalHeight + mMenuItemHeight * maxCount; | 209 return totalHeight + mMenuItemHeight * maxCount; |
| 205 } | 210 } |
| 206 | 211 |
| 207 /** | 212 /** |
| 208 * Creates a ViewPageAdapter based off the given list of views. | 213 * Creates a ViewPageAdapter based off the given list of views. |
| 209 * @param context Used to inflate the new ViewPager | 214 * @param activity Used to inflate the new ViewPager |
| 210 * @param params Used to get the header text. | 215 * @param params Used to get the header text. |
| 211 * @param itemGroups The list of views to put into the ViewPager. The string
is the title of the | 216 * @param itemGroups The list of views to put into the ViewPager. The string
is the title of the |
| 212 * tab | 217 * tab |
| 213 * @return Returns a complete tabular context menu view. | 218 * @return Returns a complete tabular context menu view. |
| 214 */ | 219 */ |
| 215 @VisibleForTesting | 220 @VisibleForTesting |
| 216 View createPagerView(Context context, ContextMenuParams params, | 221 View createPagerView(Activity activity, ContextMenuParams params, |
| 217 List<Pair<Integer, List<ContextMenuItem>>> itemGroups) { | 222 List<Pair<Integer, List<ContextMenuItem>>> itemGroups) { |
| 218 View view = LayoutInflater.from(context).inflate(R.layout.tabular_contex
t_menu, null); | 223 View view = LayoutInflater.from(activity).inflate(R.layout.tabular_conte
xt_menu, null); |
| 219 | 224 |
| 220 List<Pair<String, ViewGroup>> viewGroups = new ArrayList<>(); | 225 List<Pair<String, ViewGroup>> viewGroups = new ArrayList<>(); |
| 221 int maxCount = 0; | 226 int maxCount = 0; |
| 222 for (int i = 0; i < itemGroups.size(); i++) { | 227 for (int i = 0; i < itemGroups.size(); i++) { |
| 223 Pair<Integer, List<ContextMenuItem>> itemGroup = itemGroups.get(i); | 228 Pair<Integer, List<ContextMenuItem>> itemGroup = itemGroups.get(i); |
| 224 maxCount = Math.max(maxCount, itemGroup.second.size()); | 229 maxCount = Math.max(maxCount, itemGroup.second.size()); |
| 225 } | 230 } |
| 226 for (int i = 0; i < itemGroups.size(); i++) { | 231 for (int i = 0; i < itemGroups.size(); i++) { |
| 227 Pair<Integer, List<ContextMenuItem>> itemGroup = itemGroups.get(i); | 232 Pair<Integer, List<ContextMenuItem>> itemGroup = itemGroups.get(i); |
| 228 // TODO(tedchoc): Pass the ContextMenuGroup identifier to determine
if it's an image. | 233 // TODO(tedchoc): Pass the ContextMenuGroup identifier to determine
if it's an image. |
| 229 boolean isImageTab = itemGroup.first == R.string.contextmenu_image_t
itle; | 234 boolean isImageTab = itemGroup.first == R.string.contextmenu_image_t
itle; |
| 230 viewGroups.add(new Pair<>(context.getString(itemGroup.first), | 235 viewGroups.add(new Pair<>(activity.getString(itemGroup.first), |
| 231 createContextMenuPageUi( | 236 createContextMenuPageUi( |
| 232 context, params, itemGroup.second, isImageTab, maxCo
unt))); | 237 activity, params, itemGroup.second, isImageTab, maxC
ount))); |
| 233 } | 238 } |
| 234 TabularContextMenuViewPager pager = | 239 TabularContextMenuViewPager pager = |
| 235 (TabularContextMenuViewPager) view.findViewById(R.id.custom_page
r); | 240 (TabularContextMenuViewPager) view.findViewById(R.id.custom_page
r); |
| 236 pager.setAdapter(new TabularContextMenuPagerAdapter(viewGroups)); | 241 pager.setAdapter(new TabularContextMenuPagerAdapter(viewGroups)); |
| 237 | 242 |
| 238 TabLayout tabLayout = (TabLayout) view.findViewById(R.id.tab_layout); | 243 TabLayout tabLayout = (TabLayout) view.findViewById(R.id.tab_layout); |
| 239 if (itemGroups.size() <= 1) { | 244 if (itemGroups.size() <= 1) { |
| 240 tabLayout.setVisibility(View.GONE); | 245 tabLayout.setVisibility(View.GONE); |
| 241 } | 246 } |
| 242 tabLayout.setupWithViewPager((ViewPager) view.findViewById(R.id.custom_p
ager)); | 247 tabLayout.setupWithViewPager((ViewPager) view.findViewById(R.id.custom_p
ager)); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 253 mHeaderImageView.setImageBitmap(bitmap); | 258 mHeaderImageView.setImageBitmap(bitmap); |
| 254 } | 259 } |
| 255 } | 260 } |
| 256 | 261 |
| 257 @Override | 262 @Override |
| 258 public void onItemClick(AdapterView<?> adapterView, View view, int position,
long id) { | 263 public void onItemClick(AdapterView<?> adapterView, View view, int position,
long id) { |
| 259 mDialog.dismiss(); | 264 mDialog.dismiss(); |
| 260 mCallback.onResult((int) id); | 265 mCallback.onResult((int) id); |
| 261 } | 266 } |
| 262 } | 267 } |
| OLD | NEW |