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.Dialog; | 7 import android.app.Dialog; |
8 import android.content.Context; | 8 import android.content.Context; |
9 import android.content.DialogInterface; | 9 import android.content.DialogInterface; |
10 import android.content.res.Resources; | |
11 import android.graphics.Bitmap; | |
12 import android.graphics.Canvas; | |
13 import android.graphics.Shader; | |
14 import android.graphics.drawable.BitmapDrawable; | |
15 import android.graphics.drawable.Drawable; | |
10 import android.support.design.widget.TabLayout; | 16 import android.support.design.widget.TabLayout; |
11 import android.support.v4.view.ViewPager; | 17 import android.support.v4.view.ViewPager; |
12 import android.support.v7.app.AlertDialog; | 18 import android.support.v7.app.AlertDialog; |
13 import android.text.TextUtils; | 19 import android.text.TextUtils; |
14 import android.util.Pair; | 20 import android.util.Pair; |
15 import android.view.LayoutInflater; | 21 import android.view.LayoutInflater; |
16 import android.view.View; | 22 import android.view.View; |
17 import android.view.ViewGroup; | 23 import android.view.ViewGroup; |
18 import android.widget.AdapterView; | 24 import android.widget.AdapterView; |
19 import android.widget.BaseAdapter; | 25 import android.widget.BaseAdapter; |
26 import android.widget.ImageView; | |
20 import android.widget.ListView; | 27 import android.widget.ListView; |
21 import android.widget.TextView; | 28 import android.widget.TextView; |
22 | 29 |
23 import org.chromium.base.ApiCompatibilityUtils; | 30 import org.chromium.base.ApiCompatibilityUtils; |
24 import org.chromium.base.Callback; | 31 import org.chromium.base.Callback; |
25 import org.chromium.base.VisibleForTesting; | 32 import org.chromium.base.VisibleForTesting; |
26 import org.chromium.chrome.R; | 33 import org.chromium.chrome.R; |
27 | 34 |
28 import java.util.ArrayList; | 35 import java.util.ArrayList; |
29 import java.util.List; | 36 import java.util.List; |
30 | 37 |
31 /** | 38 /** |
32 * 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. |
33 */ | 40 */ |
34 public class TabularContextMenuUi implements ContextMenuUi, AdapterView.OnItemCl ickListener { | 41 public class TabularContextMenuUi implements ContextMenuUi, AdapterView.OnItemCl ickListener { |
35 private Dialog mDialog; | 42 private Dialog mDialog; |
36 private Callback<Integer> mCallback; | 43 private Callback<Integer> mCallback; |
37 private int mMenuItemHeight; | 44 private int mMenuItemHeight; |
45 private ContextMenuHelper mContextMenuHelper; | |
David Trainor- moved to gerrit
2017/03/30 05:03:14
final?
JJ
2017/03/30 16:00:26
final!
| |
46 | |
47 /** | |
48 * A context menu that sperates types by tabs. | |
49 * @param contextMenuHelper The {@link ContextMenuHelper} is used to retriev e the thumbnail | |
50 * natively from the Context Menu Helper. | |
51 */ | |
52 TabularContextMenuUi(ContextMenuHelper contextMenuHelper) { | |
53 mContextMenuHelper = contextMenuHelper; | |
54 } | |
38 | 55 |
39 @Override | 56 @Override |
40 public void displayMenu(Context context, ContextMenuParams params, | 57 public void displayMenu(Context context, ContextMenuParams params, |
41 List<Pair<Integer, List<ContextMenuItem>>> items, Callback<Integer> onItemClicked, | 58 List<Pair<Integer, List<ContextMenuItem>>> items, Callback<Integer> onItemClicked, |
42 final Runnable onMenuShown, final Runnable onMenuClosed) { | 59 final Runnable onMenuShown, final Runnable onMenuClosed) { |
43 mCallback = onItemClicked; | 60 mCallback = onItemClicked; |
44 mDialog = createDialog(context, params, items); | 61 mDialog = createDialog(context, params, items); |
45 mDialog.getWindow().setBackgroundDrawable(ApiCompatibilityUtils.getDrawa ble( | 62 mDialog.getWindow().setBackgroundDrawable(ApiCompatibilityUtils.getDrawa ble( |
46 context.getResources(), R.drawable.bg_find_toolbar_popup)); | 63 context.getResources(), R.drawable.bg_find_toolbar_popup)); |
47 | 64 |
(...skipping 28 matching lines...) Expand all Loading... | |
76 dialog.setContentView(createPagerView(context, params, itemGroups)); | 93 dialog.setContentView(createPagerView(context, params, itemGroups)); |
77 return dialog; | 94 return dialog; |
78 } | 95 } |
79 | 96 |
80 /** | 97 /** |
81 * 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 |
82 * 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. |
83 * @param context Used to get the resources of an item. | 100 * @param context Used to get the resources of an item. |
84 * @param params used to create the header text. | 101 * @param params used to create the header text. |
85 * @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. |
86 * @return Returns a filled LinearLayout with all the context menu items | 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 | |
105 * or any other views calculated in the context menu. Used t o estimate the size | |
106 * of the list. | |
107 * @return Returns a filled LinearLayout with all the context menu items. | |
87 */ | 108 */ |
88 @VisibleForTesting | 109 @VisibleForTesting |
89 ViewGroup createContextMenuPageUi( | 110 ViewGroup createContextMenuPageUi(Context context, ContextMenuParams params, |
90 Context context, ContextMenuParams params, List<ContextMenuItem> ite ms, int maxCount) { | 111 List<ContextMenuItem> items, boolean isImage, int maxCount) { |
91 ViewGroup baseLayout = (ViewGroup) LayoutInflater.from(context).inflate( | 112 ViewGroup baseLayout = (ViewGroup) LayoutInflater.from(context).inflate( |
92 R.layout.tabular_context_menu_page, null); | 113 R.layout.tabular_context_menu_page, null); |
93 ListView listView = (ListView) baseLayout.findViewById(R.id.selectable_i tems); | 114 ListView listView = (ListView) baseLayout.findViewById(R.id.selectable_i tems); |
94 displayHeaderIfVisibleItems(params, baseLayout); | 115 |
116 if (isImage) { | |
117 displayImageHeader(baseLayout, params, context.getResources()); | |
118 } else { | |
119 displayHeaderIfVisibleItems(params, baseLayout); | |
120 } | |
95 | 121 |
96 // 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. |
97 TabularContextMenuListAdapter listAdapter = | 123 TabularContextMenuListAdapter listAdapter = |
98 new TabularContextMenuListAdapter(items, context); | 124 new TabularContextMenuListAdapter(items, context); |
99 ViewGroup.LayoutParams layoutParams = listView.getLayoutParams(); | 125 ViewGroup.LayoutParams layoutParams = listView.getLayoutParams(); |
100 layoutParams.height = measureApproximateListViewHeight(listView, listAda pter, maxCount); | 126 layoutParams.height = measureApproximateListViewHeight(listView, listAda pter, maxCount); |
101 listView.setLayoutParams(layoutParams); | 127 listView.setLayoutParams(layoutParams); |
102 listView.setAdapter(listAdapter); | 128 listView.setAdapter(listAdapter); |
103 listView.setOnItemClickListener(this); | 129 listView.setOnItemClickListener(this); |
104 | 130 |
105 return baseLayout; | 131 return baseLayout; |
106 } | 132 } |
107 | 133 |
108 private void displayHeaderIfVisibleItems(ContextMenuParams params, ViewGroup baseLayout) { | 134 private void displayHeaderIfVisibleItems(ContextMenuParams params, ViewGroup baseLayout) { |
109 String headerText = ChromeContextMenuPopulator.createHeaderText(params); | 135 String headerText = ChromeContextMenuPopulator.createHeaderText(params); |
110 TextView headerTextView = (TextView) baseLayout.findViewById(R.id.contex t_header_text); | 136 TextView headerTextView = (TextView) baseLayout.findViewById(R.id.contex t_header_text); |
111 if (TextUtils.isEmpty(headerText)) { | 137 if (TextUtils.isEmpty(headerText)) { |
138 baseLayout.findViewById(R.id.context_header_layout).setVisibility(Vi ew.GONE); | |
112 headerTextView.setVisibility(View.GONE); | 139 headerTextView.setVisibility(View.GONE); |
113 baseLayout.findViewById(R.id.context_divider).setVisibility(View.GON E); | 140 baseLayout.findViewById(R.id.context_divider).setVisibility(View.GON E); |
114 return; | 141 return; |
115 } | 142 } |
116 headerTextView.setVisibility(View.VISIBLE); | 143 headerTextView.setVisibility(View.VISIBLE); |
117 headerTextView.setText(headerText); | 144 headerTextView.setText(headerText); |
118 } | 145 } |
119 | 146 |
147 private void displayImageHeader( | |
148 ViewGroup baseLayout, ContextMenuParams params, Resources resources) { | |
149 displayHeaderIfVisibleItems(params, baseLayout); | |
150 // #displayHeaderIfVisibleItems() sets these two views to GONE if the he ader text is | |
151 // empty but they should still be visible because we have an image to di splay. | |
152 baseLayout.findViewById(R.id.context_header_layout).setVisibility(View.V ISIBLE); | |
153 baseLayout.findViewById(R.id.context_divider).setVisibility(View.VISIBLE ); | |
154 | |
155 final ImageView imageView = (ImageView) baseLayout.findViewById(R.id.con text_header_image); | |
156 TextView headerTextView = (TextView) baseLayout.findViewById(R.id.contex t_header_text); | |
157 String headerText = params.getTitleText(); | |
158 if (!TextUtils.isEmpty(headerText)) { | |
159 headerTextView.setText(headerText); | |
160 headerTextView.setVisibility(View.VISIBLE); | |
161 } | |
162 setBackgroundForImageView(imageView, resources); | |
163 | |
164 mContextMenuHelper.setOnThumbnailReceivedListener( | |
165 new ContextMenuHelper.OnThumbnailReceivedListener() { | |
166 @Override | |
167 public void onThumbnailReceived(Bitmap bitmap) { | |
168 imageView.setImageBitmap(bitmap); | |
169 } | |
170 }); | |
171 mContextMenuHelper.getThumbnail(); | |
172 } | |
173 | |
174 private void setBackgroundForImageView(ImageView imageView, Resources resour ces) { | |
David Trainor- moved to gerrit
2017/03/30 05:03:14
This still makes me sad! :P
JJ
2017/03/30 16:00:26
It makes me even sadder!
Theresa
2017/03/30 18:34:34
I concur, but I still haven't come up with a bette
David Trainor- moved to gerrit
2017/03/30 23:45:14
Custom drawable!? (future work...?) Might be slo
| |
175 Drawable drawable = | |
176 ApiCompatibilityUtils.getDrawable(resources, R.drawable.checkerb oard_background); | |
177 Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), | |
178 drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888); | |
179 Canvas canvas = new Canvas(bitmap); | |
180 drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight()); | |
181 drawable.draw(canvas); | |
182 BitmapDrawable bm = new BitmapDrawable(resources, bitmap); | |
183 bm.setTileModeXY(Shader.TileMode.REPEAT, Shader.TileMode.REPEAT); | |
184 imageView.setVisibility(View.VISIBLE); | |
185 imageView.setBackground(bm); | |
186 } | |
187 | |
120 /** | 188 /** |
121 * To save time measuring the height, this method gets an item if the height has not been | 189 * To save time measuring the height, this method gets an item if the height has not been |
122 * previous measured and multiplies it by count of the total amount of items . It is fine if the | 190 * previous measured and multiplies it by count of the total amount of items . It is fine if the |
123 * height too small as the ListView will scroll through the other values. | 191 * height too small as the ListView will scroll through the other values. |
124 * @param listView The ListView to measure the surrounding padding. | 192 * @param listView The ListView to measure the surrounding padding. |
125 * @param listAdapter The adapter which contains the items within the list. | 193 * @param listAdapter The adapter which contains the items within the list. |
126 * @return Returns the combined height of the padding of the ListView and th e approximate height | 194 * @return Returns the combined height of the padding of the ListView and th e approximate height |
127 * of the ListView based off the an item. | 195 * of the ListView based off the an item. |
128 */ | 196 */ |
129 private int measureApproximateListViewHeight( | 197 private int measureApproximateListViewHeight( |
(...skipping 22 matching lines...) Expand all Loading... | |
152 View view = LayoutInflater.from(context).inflate(R.layout.tabular_contex t_menu, null); | 220 View view = LayoutInflater.from(context).inflate(R.layout.tabular_contex t_menu, null); |
153 | 221 |
154 List<Pair<String, ViewGroup>> viewGroups = new ArrayList<>(); | 222 List<Pair<String, ViewGroup>> viewGroups = new ArrayList<>(); |
155 int maxCount = 0; | 223 int maxCount = 0; |
156 for (int i = 0; i < itemGroups.size(); i++) { | 224 for (int i = 0; i < itemGroups.size(); i++) { |
157 Pair<Integer, List<ContextMenuItem>> itemGroup = itemGroups.get(i); | 225 Pair<Integer, List<ContextMenuItem>> itemGroup = itemGroups.get(i); |
158 maxCount = Math.max(maxCount, itemGroup.second.size()); | 226 maxCount = Math.max(maxCount, itemGroup.second.size()); |
159 } | 227 } |
160 for (int i = 0; i < itemGroups.size(); i++) { | 228 for (int i = 0; i < itemGroups.size(); i++) { |
161 Pair<Integer, List<ContextMenuItem>> itemGroup = itemGroups.get(i); | 229 Pair<Integer, List<ContextMenuItem>> itemGroup = itemGroups.get(i); |
230 boolean isImage = itemGroup.first == R.string.contextmenu_image_titl e; | |
162 viewGroups.add(new Pair<>(context.getString(itemGroup.first), | 231 viewGroups.add(new Pair<>(context.getString(itemGroup.first), |
163 createContextMenuPageUi(context, params, itemGroup.second, m axCount))); | 232 createContextMenuPageUi(context, params, itemGroup.second, i sImage, maxCount))); |
164 } | 233 } |
165 TabularContextMenuViewPager pager = | 234 TabularContextMenuViewPager pager = |
166 (TabularContextMenuViewPager) view.findViewById(R.id.custom_page r); | 235 (TabularContextMenuViewPager) view.findViewById(R.id.custom_page r); |
167 pager.setAdapter(new TabularContextMenuPagerAdapter(viewGroups)); | 236 pager.setAdapter(new TabularContextMenuPagerAdapter(viewGroups)); |
168 | 237 |
169 TabLayout tabLayout = (TabLayout) view.findViewById(R.id.tab_layout); | 238 TabLayout tabLayout = (TabLayout) view.findViewById(R.id.tab_layout); |
170 if (itemGroups.size() <= 1) { | 239 if (itemGroups.size() <= 1) { |
171 tabLayout.setVisibility(View.GONE); | 240 tabLayout.setVisibility(View.GONE); |
172 } | 241 } |
173 tabLayout.setupWithViewPager((ViewPager) view.findViewById(R.id.custom_p ager)); | 242 tabLayout.setupWithViewPager((ViewPager) view.findViewById(R.id.custom_p ager)); |
174 | 243 |
175 return view; | 244 return view; |
176 } | 245 } |
177 | 246 |
178 @Override | 247 @Override |
179 public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) { | 248 public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) { |
180 mDialog.dismiss(); | 249 mDialog.dismiss(); |
181 mCallback.onResult((int) id); | 250 mCallback.onResult((int) id); |
182 } | 251 } |
183 } | 252 } |
OLD | NEW |