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 19 matching lines...) Expand all Loading... |
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 final ContextMenuHelper mContextMenuHelper; | 45 private final ContextMenuHelper mContextMenuHelper; |
46 | 46 |
47 /** | 47 /** |
48 * A context menu that sperates types by tabs. | 48 * A context menu that separates types by tabs. |
49 * @param contextMenuHelper The {@link ContextMenuHelper} is used to retriev
e the thumbnail | 49 * @param contextMenuHelper The {@link ContextMenuHelper} is used to retriev
e the thumbnail |
50 * natively from the Context Menu Helper. | 50 * natively from the Context Menu Helper. |
51 */ | 51 */ |
52 TabularContextMenuUi(ContextMenuHelper contextMenuHelper) { | 52 TabularContextMenuUi(ContextMenuHelper contextMenuHelper) { |
53 mContextMenuHelper = contextMenuHelper; | 53 mContextMenuHelper = contextMenuHelper; |
54 } | 54 } |
55 | 55 |
56 @Override | 56 @Override |
57 public void displayMenu(Context context, ContextMenuParams params, | 57 public void displayMenu(Activity activity, ContextMenuParams params, |
58 List<Pair<Integer, List<ContextMenuItem>>> items, Callback<Integer>
onItemClicked, | 58 List<Pair<Integer, List<ContextMenuItem>>> items, Callback<Integer>
onItemClicked, |
59 final Runnable onMenuShown, final Runnable onMenuClosed) { | 59 final Runnable onMenuShown, final Runnable onMenuClosed) { |
60 mCallback = onItemClicked; | 60 mCallback = onItemClicked; |
61 mDialog = createDialog(context, params, items); | 61 mDialog = createDialog(activity, params, items); |
62 mDialog.getWindow().setBackgroundDrawable(ApiCompatibilityUtils.getDrawa
ble( | 62 mDialog.getWindow().setBackgroundDrawable(ApiCompatibilityUtils.getDrawa
ble( |
63 context.getResources(), R.drawable.bg_find_toolbar_popup)); | 63 activity.getResources(), R.drawable.bg_find_toolbar_popup)); |
64 | 64 |
65 mDialog.setOnShowListener(new DialogInterface.OnShowListener() { | 65 mDialog.setOnShowListener(new DialogInterface.OnShowListener() { |
66 @Override | 66 @Override |
67 public void onShow(DialogInterface dialogInterface) { | 67 public void onShow(DialogInterface dialogInterface) { |
68 onMenuShown.run(); | 68 onMenuShown.run(); |
69 } | 69 } |
70 }); | 70 }); |
71 | 71 |
72 mDialog.setOnDismissListener(new DialogInterface.OnDismissListener() { | 72 mDialog.setOnDismissListener(new DialogInterface.OnDismissListener() { |
73 @Override | 73 @Override |
74 public void onDismiss(DialogInterface dialogInterface) { | 74 public void onDismiss(DialogInterface dialogInterface) { |
75 onMenuClosed.run(); | 75 onMenuClosed.run(); |
76 } | 76 } |
77 }); | 77 }); |
78 | 78 |
79 mDialog.show(); | 79 mDialog.show(); |
80 } | 80 } |
81 | 81 |
82 /** | 82 /** |
83 * Returns the fully complete dialog based off the params and the itemGroups
. | 83 * Returns the fully complete dialog based off the params and the itemGroups
. |
84 * @param context Used to inflate the dialog. | 84 * @param activity Used to inflate the dialog. |
85 * @param params Used to get the header title. | 85 * @param params Used to get the header title. |
86 * @param itemGroups If there is more than one group it will create a paged
view. | 86 * @param itemGroups If there is more than one group it will create a paged
view. |
87 * @return Returns a final dialog that does not have a background can be dis
played using | 87 * @return Returns a final dialog that does not have a background can be dis
played using |
88 * {@link AlertDialog#show()}. | 88 * {@link AlertDialog#show()}. |
89 */ | 89 */ |
90 private Dialog createDialog(Context context, ContextMenuParams params, | 90 private Dialog createDialog(Activity activity, ContextMenuParams params, |
91 List<Pair<Integer, List<ContextMenuItem>>> itemGroups) { | 91 List<Pair<Integer, List<ContextMenuItem>>> itemGroups) { |
92 Dialog dialog = new Dialog(context); | 92 Dialog dialog = new Dialog(activity); |
93 dialog.setContentView(createPagerView(context, params, itemGroups)); | 93 dialog.setContentView(createPagerView(activity, params, itemGroups)); |
94 return dialog; | 94 return dialog; |
95 } | 95 } |
96 | 96 |
97 /** | 97 /** |
98 * Creates the view of a context menu. Based off the Context Type, it'll adj
ust the list of | 98 * Creates the view of a context menu. Based off the Context Type, it'll adj
ust the list of |
99 * items and display only the ones that'll be on that specific group. | 99 * items and display only the ones that'll be on that specific group. |
100 * @param context Used to get the resources of an item. | 100 * @param activity Used to get the resources of an item. |
101 * @param params used to create the header text. | 101 * @param params used to create the header text. |
102 * @param items A set of Items to display in a context menu. Filtered based
off the type. | 102 * @param items A set of Items to display in a context menu. Filtered based
off the type. |
103 * @param isImage Whether or not the view should have an image layout or not
. | 103 * @param isImage Whether or not the view should have an image layout or not
. |
104 * @param maxCount The maximum amount of {@link ContextMenuItem}s that could
exist in this view | 104 * @param maxCount The maximum amount of {@link ContextMenuItem}s that could
exist in this view |
105 * or any other views calculated in the context menu. Used t
o estimate the size | 105 * or any other views calculated in the context menu. Used t
o estimate the size |
106 * of the list. | 106 * of the list. |
107 * @return Returns a filled LinearLayout with all the context menu items. | 107 * @return Returns a filled LinearLayout with all the context menu items. |
108 */ | 108 */ |
109 @VisibleForTesting | 109 @VisibleForTesting |
110 ViewGroup createContextMenuPageUi(Context context, ContextMenuParams params, | 110 ViewGroup createContextMenuPageUi(Activity activity, ContextMenuParams param
s, |
111 List<ContextMenuItem> items, boolean isImage, int maxCount) { | 111 List<ContextMenuItem> items, boolean isImage, int maxCount) { |
112 ViewGroup baseLayout = (ViewGroup) LayoutInflater.from(context).inflate( | 112 ViewGroup baseLayout = (ViewGroup) LayoutInflater.from(activity).inflate
( |
113 R.layout.tabular_context_menu_page, null); | 113 R.layout.tabular_context_menu_page, null); |
114 ListView listView = (ListView) baseLayout.findViewById(R.id.selectable_i
tems); | 114 ListView listView = (ListView) baseLayout.findViewById(R.id.selectable_i
tems); |
115 | 115 |
116 if (isImage) { | 116 if (isImage) { |
117 displayImageHeader(baseLayout, params, context.getResources()); | 117 displayImageHeader(baseLayout, params, activity.getResources()); |
118 } else { | 118 } else { |
119 displayHeaderIfVisibleItems(params, baseLayout); | 119 displayHeaderIfVisibleItems(params, baseLayout); |
120 } | 120 } |
121 | 121 |
122 // Set the list adapter and get the height to display it appropriately i
n a dialog. | 122 // Set the list adapter and get the height to display it appropriately i
n a dialog. |
123 TabularContextMenuListAdapter listAdapter = | 123 TabularContextMenuListAdapter listAdapter = |
124 new TabularContextMenuListAdapter(items, context); | 124 new TabularContextMenuListAdapter(items, activity, mContextMenuH
elper); |
125 ViewGroup.LayoutParams layoutParams = listView.getLayoutParams(); | 125 ViewGroup.LayoutParams layoutParams = listView.getLayoutParams(); |
126 layoutParams.height = measureApproximateListViewHeight(listView, listAda
pter, maxCount); | 126 layoutParams.height = measureApproximateListViewHeight(listView, listAda
pter, maxCount); |
127 listView.setLayoutParams(layoutParams); | 127 listView.setLayoutParams(layoutParams); |
128 listView.setAdapter(listAdapter); | 128 listView.setAdapter(listAdapter); |
129 listView.setOnItemClickListener(this); | 129 listView.setOnItemClickListener(this); |
130 | 130 |
131 return baseLayout; | 131 return baseLayout; |
132 } | 132 } |
133 | 133 |
134 private void displayHeaderIfVisibleItems(ContextMenuParams params, ViewGroup
baseLayout) { | 134 private void displayHeaderIfVisibleItems(ContextMenuParams params, ViewGroup
baseLayout) { |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
214 View view = listAdapter.getView(0, null, listView); | 214 View view = listAdapter.getView(0, null, listView); |
215 view.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UN
SPECIFIED), | 215 view.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UN
SPECIFIED), |
216 View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECI
FIED)); | 216 View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECI
FIED)); |
217 mMenuItemHeight = view.getMeasuredHeight(); | 217 mMenuItemHeight = view.getMeasuredHeight(); |
218 } | 218 } |
219 return totalHeight + mMenuItemHeight * maxCount; | 219 return totalHeight + mMenuItemHeight * maxCount; |
220 } | 220 } |
221 | 221 |
222 /** | 222 /** |
223 * Creates a ViewPageAdapter based off the given list of views. | 223 * Creates a ViewPageAdapter based off the given list of views. |
224 * @param context Used to inflate the new ViewPager | 224 * @param activity Used to inflate the new ViewPager |
225 * @param params Used to get the header text. | 225 * @param params Used to get the header text. |
226 * @param itemGroups The list of views to put into the ViewPager. The string
is the title of the | 226 * @param itemGroups The list of views to put into the ViewPager. The string
is the title of the |
227 * tab | 227 * tab |
228 * @return Returns a complete tabular context menu view. | 228 * @return Returns a complete tabular context menu view. |
229 */ | 229 */ |
230 @VisibleForTesting | 230 @VisibleForTesting |
231 View createPagerView(Context context, ContextMenuParams params, | 231 View createPagerView(Activity activity, ContextMenuParams params, |
232 List<Pair<Integer, List<ContextMenuItem>>> itemGroups) { | 232 List<Pair<Integer, List<ContextMenuItem>>> itemGroups) { |
233 View view = LayoutInflater.from(context).inflate(R.layout.tabular_contex
t_menu, null); | 233 View view = LayoutInflater.from(activity).inflate(R.layout.tabular_conte
xt_menu, null); |
234 | 234 |
235 List<Pair<String, ViewGroup>> viewGroups = new ArrayList<>(); | 235 List<Pair<String, ViewGroup>> viewGroups = new ArrayList<>(); |
236 int maxCount = 0; | 236 int maxCount = 0; |
237 for (int i = 0; i < itemGroups.size(); i++) { | 237 for (int i = 0; i < itemGroups.size(); i++) { |
238 Pair<Integer, List<ContextMenuItem>> itemGroup = itemGroups.get(i); | 238 Pair<Integer, List<ContextMenuItem>> itemGroup = itemGroups.get(i); |
239 maxCount = Math.max(maxCount, itemGroup.second.size()); | 239 maxCount = Math.max(maxCount, itemGroup.second.size()); |
240 } | 240 } |
241 for (int i = 0; i < itemGroups.size(); i++) { | 241 for (int i = 0; i < itemGroups.size(); i++) { |
242 Pair<Integer, List<ContextMenuItem>> itemGroup = itemGroups.get(i); | 242 Pair<Integer, List<ContextMenuItem>> itemGroup = itemGroups.get(i); |
243 boolean isImage = itemGroup.first == R.string.contextmenu_image_titl
e; | 243 boolean isImage = itemGroup.first == R.string.contextmenu_image_titl
e; |
244 viewGroups.add(new Pair<>(context.getString(itemGroup.first), | 244 viewGroups.add(new Pair<>(activity.getString(itemGroup.first), |
245 createContextMenuPageUi(context, params, itemGroup.second, i
sImage, maxCount))); | 245 createContextMenuPageUi( |
| 246 activity, params, itemGroup.second, isImage, maxCoun
t))); |
246 } | 247 } |
247 TabularContextMenuViewPager pager = | 248 TabularContextMenuViewPager pager = |
248 (TabularContextMenuViewPager) view.findViewById(R.id.custom_page
r); | 249 (TabularContextMenuViewPager) view.findViewById(R.id.custom_page
r); |
249 pager.setAdapter(new TabularContextMenuPagerAdapter(viewGroups)); | 250 pager.setAdapter(new TabularContextMenuPagerAdapter(viewGroups)); |
250 | 251 |
251 TabLayout tabLayout = (TabLayout) view.findViewById(R.id.tab_layout); | 252 TabLayout tabLayout = (TabLayout) view.findViewById(R.id.tab_layout); |
252 if (itemGroups.size() <= 1) { | 253 if (itemGroups.size() <= 1) { |
253 tabLayout.setVisibility(View.GONE); | 254 tabLayout.setVisibility(View.GONE); |
254 } | 255 } |
255 tabLayout.setupWithViewPager((ViewPager) view.findViewById(R.id.custom_p
ager)); | 256 tabLayout.setupWithViewPager((ViewPager) view.findViewById(R.id.custom_p
ager)); |
256 | 257 |
257 return view; | 258 return view; |
258 } | 259 } |
259 | 260 |
260 @Override | 261 @Override |
261 public void onItemClick(AdapterView<?> adapterView, View view, int position,
long id) { | 262 public void onItemClick(AdapterView<?> adapterView, View view, int position,
long id) { |
262 mDialog.dismiss(); | 263 mDialog.dismiss(); |
263 mCallback.onResult((int) id); | 264 mCallback.onResult((int) id); |
264 } | 265 } |
265 } | 266 } |
OLD | NEW |