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.Activity; |
8 import android.app.Dialog; | |
9 import android.content.DialogInterface; | 8 import android.content.DialogInterface; |
10 import android.content.res.Resources; | 9 import android.content.res.Resources; |
11 import android.graphics.Bitmap; | 10 import android.graphics.Bitmap; |
12 import android.graphics.Canvas; | 11 import android.graphics.Canvas; |
13 import android.graphics.Shader; | 12 import android.graphics.Shader; |
14 import android.graphics.drawable.BitmapDrawable; | 13 import android.graphics.drawable.BitmapDrawable; |
15 import android.graphics.drawable.Drawable; | 14 import android.graphics.drawable.Drawable; |
16 import android.support.design.widget.TabLayout; | 15 import android.support.design.widget.TabLayout; |
17 import android.support.v4.view.ViewPager; | |
18 import android.support.v7.app.AlertDialog; | 16 import android.support.v7.app.AlertDialog; |
19 import android.text.TextUtils; | 17 import android.text.TextUtils; |
20 import android.util.Pair; | 18 import android.util.Pair; |
21 import android.view.LayoutInflater; | 19 import android.view.LayoutInflater; |
22 import android.view.View; | 20 import android.view.View; |
23 import android.view.ViewGroup; | 21 import android.view.ViewGroup; |
24 import android.widget.AdapterView; | 22 import android.widget.AdapterView; |
25 import android.widget.BaseAdapter; | 23 import android.widget.BaseAdapter; |
26 import android.widget.ImageView; | 24 import android.widget.ImageView; |
27 import android.widget.ListView; | 25 import android.widget.ListView; |
28 import android.widget.TextView; | 26 import android.widget.TextView; |
29 | 27 |
30 import org.chromium.base.ApiCompatibilityUtils; | 28 import org.chromium.base.ApiCompatibilityUtils; |
31 import org.chromium.base.Callback; | 29 import org.chromium.base.Callback; |
32 import org.chromium.base.VisibleForTesting; | 30 import org.chromium.base.VisibleForTesting; |
33 import org.chromium.chrome.R; | 31 import org.chromium.chrome.R; |
32 import org.chromium.chrome.browser.widget.ContextMenuDialog; | |
33 import org.chromium.content.browser.RenderCoordinates; | |
34 | 34 |
35 import java.util.ArrayList; | 35 import java.util.ArrayList; |
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 static final double MAX_WIDTH_PROPORTION = 0.75; |
Ted C
2017/06/08 17:52:59
remove as this isn't used in this file anymore.
| |
43 | |
44 private ContextMenuDialog mContextMenuDialog; | |
43 private Callback<Integer> mCallback; | 45 private Callback<Integer> mCallback; |
44 private int mMenuItemHeight; | 46 private int mMenuItemHeight; |
45 private ImageView mHeaderImageView; | 47 private ImageView mHeaderImageView; |
46 private Runnable mOnShareItemClicked; | 48 private Runnable mOnShareItemClicked; |
49 private View mPagerView; | |
50 private RenderCoordinates mRenderCoordinates; | |
47 | 51 |
48 public TabularContextMenuUi(Runnable onShareItemClicked) { | 52 public TabularContextMenuUi(Runnable onShareItemClicked) { |
49 mOnShareItemClicked = onShareItemClicked; | 53 mOnShareItemClicked = onShareItemClicked; |
50 } | 54 } |
51 | 55 |
52 @Override | 56 @Override |
53 public void displayMenu(Activity activity, ContextMenuParams params, | 57 public void displayMenu(final Activity activity, ContextMenuParams params, |
54 List<Pair<Integer, List<ContextMenuItem>>> items, Callback<Integer> onItemClicked, | 58 List<Pair<Integer, List<ContextMenuItem>>> items, Callback<Integer> onItemClicked, |
55 final Runnable onMenuShown, final Runnable onMenuClosed) { | 59 final Runnable onMenuShown, final Runnable onMenuClosed) { |
56 mCallback = onItemClicked; | 60 mCallback = onItemClicked; |
57 mDialog = createDialog(activity, params, items); | |
58 | 61 |
59 mDialog.getWindow().setBackgroundDrawable(ApiCompatibilityUtils.getDrawa ble( | 62 float density = Resources.getSystem().getDisplayMetrics().density; |
60 activity.getResources(), R.drawable.white_with_rounded_corners)) ; | 63 final float touchPointXPx = params.getTriggeringTouchXDp() * density; |
64 final float touchPointYPx = params.getTriggeringTouchYDp() * density; | |
61 | 65 |
62 mDialog.setOnShowListener(new DialogInterface.OnShowListener() { | 66 mContextMenuDialog = |
67 createContextMenuDialog(activity, params, items, touchPointXPx, touchPointYPx); | |
68 | |
69 mContextMenuDialog.setOnShowListener(new DialogInterface.OnShowListener( ) { | |
63 @Override | 70 @Override |
64 public void onShow(DialogInterface dialogInterface) { | 71 public void onShow(DialogInterface dialogInterface) { |
65 onMenuShown.run(); | 72 onMenuShown.run(); |
66 } | 73 } |
67 }); | 74 }); |
68 | 75 |
69 mDialog.setOnDismissListener(new DialogInterface.OnDismissListener() { | 76 mContextMenuDialog.setOnDismissListener(new DialogInterface.OnDismissLis tener() { |
70 @Override | 77 @Override |
71 public void onDismiss(DialogInterface dialogInterface) { | 78 public void onDismiss(DialogInterface dialogInterface) { |
72 onMenuClosed.run(); | 79 onMenuClosed.run(); |
73 } | 80 } |
74 }); | 81 }); |
75 | 82 |
76 mDialog.show(); | 83 mContextMenuDialog.show(); |
77 } | 84 } |
78 | 85 |
79 /** | 86 /** |
80 * Returns the fully complete dialog based off the params and the itemGroups . | 87 * Returns the fully complete dialog based off the params and the itemGroups . |
88 * | |
81 * @param activity Used to inflate the dialog. | 89 * @param activity Used to inflate the dialog. |
82 * @param params Used to get the header title. | 90 * @param params Used to get the header title. |
83 * @param itemGroups If there is more than one group it will create a paged view. | 91 * @param itemGroups If there is more than one group it will create a paged view. |
92 * @param touchPointYPx The x-coordinate of the touch that triggered the con text menu. | |
93 * @param touchPointXPx The y-coordinate of the touch that triggered the con text menu. | |
84 * @return Returns a final dialog that does not have a background can be dis played using | 94 * @return Returns a final dialog that does not have a background can be dis played using |
85 * {@link AlertDialog#show()}. | 95 * {@link AlertDialog#show()}. |
86 */ | 96 */ |
87 private Dialog createDialog(Activity activity, ContextMenuParams params, | 97 private ContextMenuDialog createContextMenuDialog(Activity activity, Context MenuParams params, |
88 List<Pair<Integer, List<ContextMenuItem>>> itemGroups) { | 98 List<Pair<Integer, List<ContextMenuItem>>> itemGroups, float touchPo intXPx, |
89 Dialog dialog = new Dialog(activity); | 99 float touchPointYPx) { |
90 dialog.setContentView(createPagerView(activity, params, itemGroups)); | 100 View view = LayoutInflater.from(activity).inflate(R.layout.tabular_conte xt_menu, null); |
101 | |
102 mPagerView = initPagerView(activity, params, itemGroups, | |
103 (TabularContextMenuViewPager) view.findViewById(R.id.custom_page r)); | |
104 | |
105 final ContextMenuDialog dialog = new ContextMenuDialog(activity, R.style .DialogWhenLarge, | |
106 touchPointXPx, touchPointYPx, mPagerView, mRenderCoordinates); | |
107 dialog.setContentView(view); | |
108 | |
91 return dialog; | 109 return dialog; |
92 } | 110 } |
93 | 111 |
94 /** | 112 /** |
95 * Creates a ViewPageAdapter based off the given list of views. | 113 * Creates a ViewPageAdapter based off the given list of views. |
96 * @param activity Used to inflate the new ViewPager | 114 * |
115 * @param activity Used to inflate the new ViewPager. | |
97 * @param params Used to get the header text. | 116 * @param params Used to get the header text. |
98 * @param itemGroups The list of views to put into the ViewPager. The string is the title of the | 117 * @param itemGroups The list of views to put into the ViewPager. The string is the title of the |
99 * tab | 118 * tab. |
119 * @param viewPager The {@link TabularContextMenuViewPager} to initialize. | |
100 * @return Returns a complete tabular context menu view. | 120 * @return Returns a complete tabular context menu view. |
101 */ | 121 */ |
102 @VisibleForTesting | 122 @VisibleForTesting |
103 View createPagerView(Activity activity, ContextMenuParams params, | 123 View initPagerView(Activity activity, ContextMenuParams params, |
104 List<Pair<Integer, List<ContextMenuItem>>> itemGroups) { | 124 List<Pair<Integer, List<ContextMenuItem>>> itemGroups, |
105 View view = LayoutInflater.from(activity).inflate(R.layout.tabular_conte xt_menu, null); | 125 TabularContextMenuViewPager viewPager) { |
106 | |
107 List<Pair<String, ViewGroup>> viewGroups = new ArrayList<>(); | 126 List<Pair<String, ViewGroup>> viewGroups = new ArrayList<>(); |
108 int maxCount = 0; | 127 int maxCount = 0; |
109 for (int i = 0; i < itemGroups.size(); i++) { | 128 for (int i = 0; i < itemGroups.size(); i++) { |
110 Pair<Integer, List<ContextMenuItem>> itemGroup = itemGroups.get(i); | 129 Pair<Integer, List<ContextMenuItem>> itemGroup = itemGroups.get(i); |
111 maxCount = Math.max(maxCount, itemGroup.second.size()); | 130 maxCount = Math.max(maxCount, itemGroup.second.size()); |
112 } | 131 } |
113 for (int i = 0; i < itemGroups.size(); i++) { | 132 for (int i = 0; i < itemGroups.size(); i++) { |
114 Pair<Integer, List<ContextMenuItem>> itemGroup = itemGroups.get(i); | 133 Pair<Integer, List<ContextMenuItem>> itemGroup = itemGroups.get(i); |
115 // TODO(tedchoc): Pass the ContextMenuGroup identifier to determine if it's an image. | 134 // TODO(tedchoc): Pass the ContextMenuGroup identifier to determine if it's an image. |
116 boolean isImageTab = itemGroup.first == R.string.contextmenu_image_t itle; | 135 boolean isImageTab = itemGroup.first == R.string.contextmenu_image_t itle; |
117 viewGroups.add(new Pair<>(activity.getString(itemGroup.first), | 136 viewGroups.add(new Pair<>(activity.getString(itemGroup.first), |
118 createContextMenuPageUi( | 137 createContextMenuPageUi( |
119 activity, params, itemGroup.second, isImageTab, maxC ount))); | 138 activity, params, itemGroup.second, isImageTab, maxC ount))); |
120 } | 139 } |
121 if (itemGroups.size() == 1) { | |
122 viewGroups.get(0) | |
123 .second.getChildAt(0) | |
124 .findViewById(R.id.context_header_layout) | |
125 .setBackgroundResource(R.color.google_grey_100); | |
126 } | |
127 | 140 |
128 TabularContextMenuViewPager pager = | 141 viewPager.setAdapter(new TabularContextMenuPagerAdapter(viewGroups)); |
129 (TabularContextMenuViewPager) view.findViewById(R.id.custom_page r); | 142 TabLayout tabLayout = (TabLayout) viewPager.findViewById(R.id.tab_layout ); |
130 pager.setAdapter(new TabularContextMenuPagerAdapter(viewGroups)); | |
131 | |
132 TabLayout tabLayout = (TabLayout) view.findViewById(R.id.tab_layout); | |
133 if (itemGroups.size() <= 1) { | 143 if (itemGroups.size() <= 1) { |
134 tabLayout.setVisibility(View.GONE); | 144 tabLayout.setVisibility(View.GONE); |
135 } else { | 145 } else { |
136 tabLayout.setupWithViewPager((ViewPager) view.findViewById(R.id.cust om_pager)); | 146 tabLayout.setBackgroundResource(R.drawable.grey_with_top_rounded_cor ners); |
147 tabLayout.setupWithViewPager(viewPager); | |
137 } | 148 } |
138 | 149 |
139 return view; | 150 return viewPager; |
140 } | 151 } |
141 | 152 |
142 /** | 153 /** |
143 * Creates the view of a context menu. Based off the Context Type, it'll adj ust the list of | 154 * Creates the view of a context menu. Based off the Context Type, it'll adj ust the list of |
144 * items and display only the ones that'll be on that specific group. | 155 * items and display only the ones that'll be on that specific group. |
156 * | |
145 * @param activity Used to get the resources of an item. | 157 * @param activity Used to get the resources of an item. |
146 * @param params used to create the header text. | 158 * @param params used to create the header text. |
147 * @param items A set of Items to display in a context menu. Filtered based off the type. | 159 * @param items A set of Items to display in a context menu. Filtered based off the type. |
148 * @param isImage Whether or not the view should have an image layout or not . | 160 * @param isImage Whether or not the view should have an image layout or not . |
149 * @param maxCount The maximum amount of {@link ContextMenuItem}s that could exist in this view | 161 * @param maxCount The maximum amount of {@link ContextMenuItem}s that could exist in this view |
150 * or any other views calculated in the context menu. Used t o estimate the size | 162 * or any other views calculated in the context menu. Used t o estimate the size |
151 * of the list. | 163 * of the list. |
152 * @return Returns a filled LinearLayout with all the context menu items. | 164 * @return Returns a filled LinearLayout with all the context menu items. |
153 */ | 165 */ |
154 @VisibleForTesting | 166 @VisibleForTesting |
(...skipping 10 matching lines...) Expand all Loading... | |
165 baseLayout.findViewById(R.id.context_header_layout).setVisibility(Vi ew.VISIBLE); | 177 baseLayout.findViewById(R.id.context_header_layout).setVisibility(Vi ew.VISIBLE); |
166 baseLayout.findViewById(R.id.context_divider).setVisibility(View.VIS IBLE); | 178 baseLayout.findViewById(R.id.context_divider).setVisibility(View.VIS IBLE); |
167 displayImageHeader(baseLayout, params, activity.getResources()); | 179 displayImageHeader(baseLayout, params, activity.getResources()); |
168 } | 180 } |
169 | 181 |
170 // Set the list adapter and get the height to display it appropriately i n a dialog. | 182 // Set the list adapter and get the height to display it appropriately i n a dialog. |
171 Runnable onDirectShare = new Runnable() { | 183 Runnable onDirectShare = new Runnable() { |
172 @Override | 184 @Override |
173 public void run() { | 185 public void run() { |
174 mOnShareItemClicked.run(); | 186 mOnShareItemClicked.run(); |
175 mDialog.dismiss(); | 187 mContextMenuDialog.dismiss(); |
176 } | 188 } |
177 }; | 189 }; |
178 TabularContextMenuListAdapter listAdapter = | 190 TabularContextMenuListAdapter listAdapter = |
179 new TabularContextMenuListAdapter(items, activity, onDirectShare ); | 191 new TabularContextMenuListAdapter(items, activity, onDirectShare ); |
180 ViewGroup.LayoutParams layoutParams = listView.getLayoutParams(); | 192 ViewGroup.LayoutParams layoutParams = listView.getLayoutParams(); |
181 layoutParams.height = measureApproximateListViewHeight(listView, listAda pter, maxCount); | 193 layoutParams.height = measureApproximateListViewHeight(listView, listAda pter, maxCount); |
182 listView.setLayoutParams(layoutParams); | 194 listView.setLayoutParams(layoutParams); |
183 listView.setAdapter(listAdapter); | 195 listView.setAdapter(listAdapter); |
184 listView.setOnItemClickListener(this); | 196 listView.setOnItemClickListener(this); |
185 | 197 |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
238 BitmapDrawable bm = new BitmapDrawable(resources, bitmap); | 250 BitmapDrawable bm = new BitmapDrawable(resources, bitmap); |
239 bm.setTileModeXY(Shader.TileMode.REPEAT, Shader.TileMode.REPEAT); | 251 bm.setTileModeXY(Shader.TileMode.REPEAT, Shader.TileMode.REPEAT); |
240 imageView.setVisibility(View.VISIBLE); | 252 imageView.setVisibility(View.VISIBLE); |
241 imageView.setBackground(bm); | 253 imageView.setBackground(bm); |
242 } | 254 } |
243 | 255 |
244 /** | 256 /** |
245 * To save time measuring the height, this method gets an item if the height has not been | 257 * To save time measuring the height, this method gets an item if the height has not been |
246 * previous measured and multiplies it by count of the total amount of items . It is fine if the | 258 * previous measured and multiplies it by count of the total amount of items . It is fine if the |
247 * height too small as the ListView will scroll through the other values. | 259 * height too small as the ListView will scroll through the other values. |
260 * | |
248 * @param listView The ListView to measure the surrounding padding. | 261 * @param listView The ListView to measure the surrounding padding. |
249 * @param listAdapter The adapter which contains the items within the list. | 262 * @param listAdapter The adapter which contains the items within the list. |
250 * @return Returns the combined height of the padding of the ListView and th e approximate height | 263 * @return Returns the combined height of the padding of the ListView and th e approximate height |
251 * of the ListView based off the an item. | 264 * of the ListView based off the an item. |
252 */ | 265 */ |
253 private int measureApproximateListViewHeight( | 266 private int measureApproximateListViewHeight( |
254 ListView listView, BaseAdapter listAdapter, int maxCount) { | 267 ListView listView, BaseAdapter listAdapter, int maxCount) { |
255 int totalHeight = listView.getPaddingTop() + listView.getPaddingBottom() ; | 268 int totalHeight = listView.getPaddingTop() + listView.getPaddingBottom() ; |
256 if (mMenuItemHeight == 0 && !listAdapter.isEmpty()) { | 269 if (mMenuItemHeight == 0 && !listAdapter.isEmpty()) { |
257 View view = listAdapter.getView(0, null, listView); | 270 View view = listAdapter.getView(0, null, listView); |
258 view.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UN SPECIFIED), | 271 view.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UN SPECIFIED), |
259 View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECI FIED)); | 272 View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECI FIED)); |
260 mMenuItemHeight = view.getMeasuredHeight(); | 273 mMenuItemHeight = view.getMeasuredHeight(); |
261 } | 274 } |
262 return totalHeight + mMenuItemHeight * maxCount; | 275 return totalHeight + mMenuItemHeight * maxCount; |
263 } | 276 } |
264 | 277 |
265 /** | 278 /** |
266 * When an thumbnail is retrieved for the header of an image, this will set the header to | 279 * When an thumbnail is retrieved for the header of an image, this will set the header to that |
267 * that particular bitmap. | 280 * particular bitmap. |
268 */ | 281 */ |
269 public void onImageThumbnailRetrieved(Bitmap bitmap) { | 282 public void onImageThumbnailRetrieved(Bitmap bitmap) { |
270 if (mHeaderImageView != null) { | 283 if (mHeaderImageView != null) { |
271 mHeaderImageView.setImageBitmap(bitmap); | 284 mHeaderImageView.setImageBitmap(bitmap); |
272 } | 285 } |
273 } | 286 } |
274 | 287 |
275 @Override | 288 @Override |
276 public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) { | 289 public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) { |
277 mDialog.dismiss(); | 290 mContextMenuDialog.dismiss(); |
278 mCallback.onResult((int) id); | 291 mCallback.onResult((int) id); |
279 } | 292 } |
293 | |
294 /** | |
295 * Gives this class access to the render coordinates to allow access to the total size of the | |
296 * toolbar and tab strip. | |
297 */ | |
298 public void setRenderCoordinates(RenderCoordinates renderCoordinates) { | |
299 mRenderCoordinates = renderCoordinates; | |
300 } | |
280 } | 301 } |
OLD | NEW |