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; | 8 import android.app.Dialog; |
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; | |
46 | |
47 /** | |
48 * A context menu that sperates types by tabs. | |
49 * @param contextMenuHelper This is used to retrieve the thumbnail natively from the Context | |
Theresa
2017/03/27 18:28:26
nit: The {@link ContextMenuHelper} used to retriev
JJ
2017/03/27 20:47:12
Done.
| |
50 * Menu Helper. | |
51 */ | |
52 TabularContextMenuUi(ContextMenuHelper contextMenuHelper) { | |
53 mContextMenuHelper = contextMenuHelper; | |
54 } | |
38 | 55 |
39 @Override | 56 @Override |
40 public void displayMenu(Activity activity, ContextMenuParams params, | 57 public void displayMenu(Activity activity, 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(activity, params, items); | 61 mDialog = createDialog(activity, params, items); |
45 mDialog.getWindow().setBackgroundDrawable(ApiCompatibilityUtils.getDrawa ble( | 62 mDialog.getWindow().setBackgroundDrawable(ApiCompatibilityUtils.getDrawa ble( |
46 activity.getResources(), R.drawable.context_menu_bg)); | 63 activity.getResources(), R.drawable.context_menu_bg)); |
47 | 64 |
(...skipping 27 matching lines...) Expand all Loading... | |
75 Dialog dialog = new Dialog(activity); | 92 Dialog dialog = new Dialog(activity); |
76 dialog.setContentView(createPagerView(activity, params, itemGroups)); | 93 dialog.setContentView(createPagerView(activity, 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 activity Used to get the resources of an item. | 100 * @param activity 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 itemGroup A set of Items to display in a context menu. Filtered ba sed off the type. |
86 * @return Returns a filled LinearLayout with all the context menu items | 103 * @return Returns a filled LinearLayout with all the context menu items |
87 */ | 104 */ |
88 @VisibleForTesting | 105 @VisibleForTesting |
89 ViewGroup createSingleContextView(Activity activity, ContextMenuParams param s, | 106 ViewGroup createSingleContextView(Activity activity, ContextMenuParams param s, |
90 List<ContextMenuItem> items, int maxCount) { | 107 Pair<Integer, List<ContextMenuItem>> itemGroup, int maxCount) { |
Theresa
2017/03/27 18:28:26
Can the title string id and list of items be separ
JJ
2017/03/27 20:47:12
Yeah we can do that.
| |
108 List<ContextMenuItem> items = itemGroup.second; | |
109 int title = itemGroup.first; | |
91 ViewGroup baseLayout = (ViewGroup) LayoutInflater.from(activity).inflate ( | 110 ViewGroup baseLayout = (ViewGroup) LayoutInflater.from(activity).inflate ( |
92 R.layout.tabular_context_menu_page, null); | 111 R.layout.tabular_context_menu_page, null); |
93 ListView listView = (ListView) baseLayout.findViewById(R.id.selectable_i tems); | 112 ListView listView = (ListView) baseLayout.findViewById(R.id.selectable_i tems); |
94 displayHeaderIfVisibleItems(params, baseLayout); | 113 |
114 if (title == R.string.contextmenu_image_title) { | |
115 displayImageHeader(baseLayout, params, activity.getResources()); | |
116 } else { | |
117 displayHeaderIfVisibleItems(params, baseLayout); | |
118 } | |
95 | 119 |
96 // Set the list adapter and get the height to display it appropriately i n a dialog. | 120 // Set the list adapter and get the height to display it appropriately i n a dialog. |
97 TabularContextMenuListAdapter listAdapter = | 121 TabularContextMenuListAdapter listAdapter = |
98 new TabularContextMenuListAdapter(items, activity); | 122 new TabularContextMenuListAdapter(items, activity); |
99 ViewGroup.LayoutParams layoutParams = listView.getLayoutParams(); | 123 ViewGroup.LayoutParams layoutParams = listView.getLayoutParams(); |
100 layoutParams.height = measureApproximateListViewHeight(listView, listAda pter, maxCount); | 124 layoutParams.height = measureApproximateListViewHeight(listView, listAda pter, maxCount); |
101 listView.setLayoutParams(layoutParams); | 125 listView.setLayoutParams(layoutParams); |
102 listView.setAdapter(listAdapter); | 126 listView.setAdapter(listAdapter); |
103 listView.setOnItemClickListener(this); | 127 listView.setOnItemClickListener(this); |
104 | 128 |
105 return baseLayout; | 129 return baseLayout; |
106 } | 130 } |
107 | 131 |
108 private void displayHeaderIfVisibleItems(ContextMenuParams params, ViewGroup baseLayout) { | 132 private void displayHeaderIfVisibleItems(ContextMenuParams params, ViewGroup baseLayout) { |
109 String headerText = ChromeContextMenuPopulator.createHeaderText(params); | 133 String headerText = ChromeContextMenuPopulator.createHeaderText(params); |
110 TextView headerTextView = (TextView) baseLayout.findViewById(R.id.contex t_header_text); | 134 TextView headerTextView = (TextView) baseLayout.findViewById(R.id.contex t_header_text); |
111 if (TextUtils.isEmpty(headerText)) { | 135 if (TextUtils.isEmpty(headerText)) { |
136 baseLayout.findViewById(R.id.context_header_layout).setVisibility(Vi ew.GONE); | |
112 headerTextView.setVisibility(View.GONE); | 137 headerTextView.setVisibility(View.GONE); |
113 baseLayout.findViewById(R.id.context_divider).setVisibility(View.GON E); | 138 baseLayout.findViewById(R.id.context_divider).setVisibility(View.GON E); |
114 return; | 139 return; |
115 } | 140 } |
116 headerTextView.setVisibility(View.VISIBLE); | 141 headerTextView.setVisibility(View.VISIBLE); |
117 headerTextView.setText(headerText); | 142 headerTextView.setText(headerText); |
118 } | 143 } |
119 | 144 |
145 private void displayImageHeader( | |
146 ViewGroup baseLayout, ContextMenuParams params, Resources resources) { | |
147 final ImageView imageView = (ImageView) baseLayout.findViewById(R.id.con text_header_image); | |
148 TextView headerTextView = (TextView) baseLayout.findViewById(R.id.contex t_header_text); | |
149 String headerText = params.getTitleText(); | |
150 if (!TextUtils.isEmpty(headerText)) { | |
151 headerTextView.setText(headerText); | |
152 headerTextView.setVisibility(View.VISIBLE); | |
153 } else { | |
154 displayHeaderIfVisibleItems(params, baseLayout); | |
155 // The image is still around so disabling the header is uneccessary | |
156 baseLayout.findViewById(R.id.context_header_layout).setVisibility(Vi ew.VISIBLE); | |
Theresa
2017/03/27 18:28:26
I think this could be simplified:
displayHeaderIf
JJ
2017/03/27 20:47:12
Done.
Theresa
2017/03/28 17:23:08
I meant the whole top of the method
private displ
JJ
2017/03/28 23:19:20
Oh whoops, apologies.
| |
157 baseLayout.findViewById(R.id.context_divider).setVisibility(View.VIS IBLE); | |
158 } | |
159 setBackgroundForImageView(imageView, resources); | |
160 | |
161 mContextMenuHelper.setOnThumbnailReceivedListener( | |
162 new ContextMenuHelper.OnThumbnailReceivedListener() { | |
163 @Override | |
164 public void onThumbnailReceived(Bitmap bitmap) { | |
165 imageView.setImageBitmap(bitmap); | |
166 } | |
167 }); | |
168 mContextMenuHelper.getThumbnail(); | |
169 } | |
170 | |
171 private void setBackgroundForImageView(ImageView imageView, Resources resour ces) { | |
172 Bitmap bitmap = null; | |
173 Drawable drawable = | |
174 ApiCompatibilityUtils.getDrawable(resources, R.drawable.checkerb oard_background); | |
175 bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getI ntrinsicHeight(), | |
176 Bitmap.Config.ARGB_8888); | |
177 Canvas canvas = new Canvas(bitmap); | |
178 drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight()); | |
179 drawable.draw(canvas); | |
180 BitmapDrawable bm = new BitmapDrawable(resources, bitmap); | |
181 bm.setTileModeXY(Shader.TileMode.REPEAT, Shader.TileMode.REPEAT); | |
182 imageView.setVisibility(View.VISIBLE); | |
183 imageView.setBackground(bm); | |
Theresa
2017/03/27 18:28:26
This method seems more complex than needed. I thin
JJ
2017/03/27 20:47:12
haha we tried. However it was deemed impossible if
| |
184 } | |
185 | |
120 /** | 186 /** |
121 * To save time measuring the height, this method gets an item if the height has not been | 187 * 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 | 188 * 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. | 189 * height too small as the ListView will scroll through the other values. |
124 * @param listView The ListView to measure the surrounding padding. | 190 * @param listView The ListView to measure the surrounding padding. |
125 * @param listAdapter The adapter which contains the items within the list. | 191 * @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 | 192 * @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. | 193 * of the ListView based off the an item. |
128 */ | 194 */ |
129 private int measureApproximateListViewHeight( | 195 private int measureApproximateListViewHeight( |
(...skipping 19 matching lines...) Expand all Loading... | |
149 @VisibleForTesting | 215 @VisibleForTesting |
150 View createPagerView(Activity activity, ContextMenuParams params, | 216 View createPagerView(Activity activity, ContextMenuParams params, |
151 List<Pair<Integer, List<ContextMenuItem>>> itemGroups) { | 217 List<Pair<Integer, List<ContextMenuItem>>> itemGroups) { |
152 View view = LayoutInflater.from(activity).inflate(R.layout.tabular_conte xt_menu, null); | 218 View view = LayoutInflater.from(activity).inflate(R.layout.tabular_conte xt_menu, null); |
153 | 219 |
154 List<Pair<String, ViewGroup>> viewGroups = new ArrayList<>(); | 220 List<Pair<String, ViewGroup>> viewGroups = new ArrayList<>(); |
155 int maxCount = 0; | 221 int maxCount = 0; |
156 for (Pair<Integer, List<ContextMenuItem>> itemGroup : itemGroups) { | 222 for (Pair<Integer, List<ContextMenuItem>> itemGroup : itemGroups) { |
157 maxCount = Math.max(maxCount, itemGroup.second.size()); | 223 maxCount = Math.max(maxCount, itemGroup.second.size()); |
158 viewGroups.add(new Pair<>(activity.getString(itemGroup.first), | 224 viewGroups.add(new Pair<>(activity.getString(itemGroup.first), |
159 createSingleContextView(activity, params, itemGroup.second, maxCount))); | 225 createSingleContextView(activity, params, itemGroup, maxCoun t))); |
160 } | 226 } |
161 TabularContextMenuViewPager pager = | 227 TabularContextMenuViewPager pager = |
162 (TabularContextMenuViewPager) view.findViewById(R.id.custom_page r); | 228 (TabularContextMenuViewPager) view.findViewById(R.id.custom_page r); |
163 pager.setAdapter(new TabularContextMenuPagerAdapter(viewGroups)); | 229 pager.setAdapter(new TabularContextMenuPagerAdapter(viewGroups)); |
164 | 230 |
165 TabLayout tabLayout = (TabLayout) view.findViewById(R.id.tab_layout); | 231 TabLayout tabLayout = (TabLayout) view.findViewById(R.id.tab_layout); |
166 if (itemGroups.size() <= 1) { | 232 if (itemGroups.size() <= 1) { |
167 tabLayout.setVisibility(View.GONE); | 233 tabLayout.setVisibility(View.GONE); |
168 } | 234 } |
169 tabLayout.setupWithViewPager((ViewPager) view.findViewById(R.id.custom_p ager)); | 235 tabLayout.setupWithViewPager((ViewPager) view.findViewById(R.id.custom_p ager)); |
170 | 236 |
171 return view; | 237 return view; |
172 } | 238 } |
173 | 239 |
174 @Override | 240 @Override |
175 public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) { | 241 public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) { |
176 mDialog.dismiss(); | 242 mDialog.dismiss(); |
177 mCallback.onResult((int) id); | 243 mCallback.onResult((int) id); |
178 } | 244 } |
179 } | 245 } |
OLD | NEW |