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 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(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 28 matching lines...) Expand all Loading... | |
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 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 * @return Returns a filled LinearLayout with all the context menu items |
Theresa
2017/03/28 17:23:08
nit: add @param isImage and @param maxCount to Jav
JJ
2017/03/28 23:19:20
Done.
| |
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 List<ContextMenuItem> items, boolean isImage, int maxCount) { |
91 ViewGroup baseLayout = (ViewGroup) LayoutInflater.from(activity).inflate ( | 108 ViewGroup baseLayout = (ViewGroup) LayoutInflater.from(activity).inflate ( |
92 R.layout.tabular_context_menu_page, null); | 109 R.layout.tabular_context_menu_page, null); |
93 ListView listView = (ListView) baseLayout.findViewById(R.id.selectable_i tems); | 110 ListView listView = (ListView) baseLayout.findViewById(R.id.selectable_i tems); |
94 displayHeaderIfVisibleItems(params, baseLayout); | 111 |
112 if (isImage) { | |
113 displayImageHeader(baseLayout, params, activity.getResources()); | |
114 } else { | |
115 displayHeaderIfVisibleItems(params, baseLayout); | |
116 } | |
95 | 117 |
96 // 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. |
97 TabularContextMenuListAdapter listAdapter = | 119 TabularContextMenuListAdapter listAdapter = |
98 new TabularContextMenuListAdapter(items, activity); | 120 new TabularContextMenuListAdapter(items, activity); |
99 ViewGroup.LayoutParams layoutParams = listView.getLayoutParams(); | 121 ViewGroup.LayoutParams layoutParams = listView.getLayoutParams(); |
100 layoutParams.height = measureApproximateListViewHeight(listView, listAda pter, maxCount); | 122 layoutParams.height = measureApproximateListViewHeight(listView, listAda pter, maxCount); |
101 listView.setLayoutParams(layoutParams); | 123 listView.setLayoutParams(layoutParams); |
102 listView.setAdapter(listAdapter); | 124 listView.setAdapter(listAdapter); |
103 listView.setOnItemClickListener(this); | 125 listView.setOnItemClickListener(this); |
104 | 126 |
105 return baseLayout; | 127 return baseLayout; |
106 } | 128 } |
107 | 129 |
108 private void displayHeaderIfVisibleItems(ContextMenuParams params, ViewGroup baseLayout) { | 130 private void displayHeaderIfVisibleItems(ContextMenuParams params, ViewGroup baseLayout) { |
109 String headerText = ChromeContextMenuPopulator.createHeaderText(params); | 131 String headerText = ChromeContextMenuPopulator.createHeaderText(params); |
110 TextView headerTextView = (TextView) baseLayout.findViewById(R.id.contex t_header_text); | 132 TextView headerTextView = (TextView) baseLayout.findViewById(R.id.contex t_header_text); |
111 if (TextUtils.isEmpty(headerText)) { | 133 if (TextUtils.isEmpty(headerText)) { |
134 baseLayout.findViewById(R.id.context_header_layout).setVisibility(Vi ew.GONE); | |
112 headerTextView.setVisibility(View.GONE); | 135 headerTextView.setVisibility(View.GONE); |
113 baseLayout.findViewById(R.id.context_divider).setVisibility(View.GON E); | 136 baseLayout.findViewById(R.id.context_divider).setVisibility(View.GON E); |
114 return; | 137 return; |
115 } | 138 } |
116 headerTextView.setVisibility(View.VISIBLE); | 139 headerTextView.setVisibility(View.VISIBLE); |
117 headerTextView.setText(headerText); | 140 headerTextView.setText(headerText); |
118 } | 141 } |
119 | 142 |
143 private void displayImageHeader( | |
144 ViewGroup baseLayout, ContextMenuParams params, Resources resources) { | |
145 final ImageView imageView = (ImageView) baseLayout.findViewById(R.id.con text_header_image); | |
146 TextView headerTextView = (TextView) baseLayout.findViewById(R.id.contex t_header_text); | |
147 String headerText = params.getTitleText(); | |
148 if (!TextUtils.isEmpty(headerText)) { | |
149 headerTextView.setText(headerText); | |
150 headerTextView.setVisibility(View.VISIBLE); | |
151 } else { | |
152 displayHeaderIfVisibleItems(params, baseLayout); | |
153 // #displayHeaderIfVisibleItems() sets these two views to GONE if th e header text is | |
154 // empty but they should still be visible because we have an image t o display. | |
155 baseLayout.findViewById(R.id.context_header_layout).setVisibility(Vi ew.VISIBLE); | |
156 baseLayout.findViewById(R.id.context_divider).setVisibility(View.VIS IBLE); | |
157 } | |
158 setBackgroundForImageView(imageView, resources); | |
159 | |
160 mContextMenuHelper.setOnThumbnailReceivedListener( | |
161 new ContextMenuHelper.OnThumbnailReceivedListener() { | |
162 @Override | |
163 public void onThumbnailReceived(Bitmap bitmap) { | |
164 imageView.setImageBitmap(bitmap); | |
165 } | |
166 }); | |
167 mContextMenuHelper.getThumbnail(); | |
168 } | |
169 | |
170 private void setBackgroundForImageView(ImageView imageView, Resources resour ces) { | |
171 Bitmap bitmap = null; | |
172 Drawable drawable = | |
173 ApiCompatibilityUtils.getDrawable(resources, R.drawable.checkerb oard_background); | |
174 bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getI ntrinsicHeight(), | |
175 Bitmap.Config.ARGB_8888); | |
176 Canvas canvas = new Canvas(bitmap); | |
177 drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight()); | |
178 drawable.draw(canvas); | |
179 BitmapDrawable bm = new BitmapDrawable(resources, bitmap); | |
Theresa
2017/03/28 17:23:08
I think this can be simplified to something like:
JJ
2017/03/28 23:19:20
Doesn't work :< Doesn't keep the repetition up, sa
| |
180 bm.setTileModeXY(Shader.TileMode.REPEAT, Shader.TileMode.REPEAT); | |
181 imageView.setVisibility(View.VISIBLE); | |
182 imageView.setBackground(bm); | |
183 } | |
184 | |
120 /** | 185 /** |
121 * To save time measuring the height, this method gets an item if the height has not been | 186 * 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 | 187 * 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. | 188 * height too small as the ListView will scroll through the other values. |
124 * @param listView The ListView to measure the surrounding padding. | 189 * @param listView The ListView to measure the surrounding padding. |
125 * @param listAdapter The adapter which contains the items within the list. | 190 * @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 | 191 * @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. | 192 * of the ListView based off the an item. |
128 */ | 193 */ |
129 private int measureApproximateListViewHeight( | 194 private int measureApproximateListViewHeight( |
(...skipping 18 matching lines...) Expand all Loading... | |
148 */ | 213 */ |
149 @VisibleForTesting | 214 @VisibleForTesting |
150 View createPagerView(Activity activity, ContextMenuParams params, | 215 View createPagerView(Activity activity, ContextMenuParams params, |
151 List<Pair<Integer, List<ContextMenuItem>>> itemGroups) { | 216 List<Pair<Integer, List<ContextMenuItem>>> itemGroups) { |
152 View view = LayoutInflater.from(activity).inflate(R.layout.tabular_conte xt_menu, null); | 217 View view = LayoutInflater.from(activity).inflate(R.layout.tabular_conte xt_menu, null); |
153 | 218 |
154 List<Pair<String, ViewGroup>> viewGroups = new ArrayList<>(); | 219 List<Pair<String, ViewGroup>> viewGroups = new ArrayList<>(); |
155 int maxCount = 0; | 220 int maxCount = 0; |
156 for (Pair<Integer, List<ContextMenuItem>> itemGroup : itemGroups) { | 221 for (Pair<Integer, List<ContextMenuItem>> itemGroup : itemGroups) { |
157 maxCount = Math.max(maxCount, itemGroup.second.size()); | 222 maxCount = Math.max(maxCount, itemGroup.second.size()); |
223 boolean isImage = itemGroup.first == R.string.contextmenu_image_titl e; | |
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( |
226 activity, params, itemGroup.second, isImage, maxCoun t))); | |
160 } | 227 } |
161 TabularContextMenuViewPager pager = | 228 TabularContextMenuViewPager pager = |
162 (TabularContextMenuViewPager) view.findViewById(R.id.custom_page r); | 229 (TabularContextMenuViewPager) view.findViewById(R.id.custom_page r); |
163 pager.setAdapter(new TabularContextMenuPagerAdapter(viewGroups)); | 230 pager.setAdapter(new TabularContextMenuPagerAdapter(viewGroups)); |
164 | 231 |
165 TabLayout tabLayout = (TabLayout) view.findViewById(R.id.tab_layout); | 232 TabLayout tabLayout = (TabLayout) view.findViewById(R.id.tab_layout); |
166 if (itemGroups.size() <= 1) { | 233 if (itemGroups.size() <= 1) { |
167 tabLayout.setVisibility(View.GONE); | 234 tabLayout.setVisibility(View.GONE); |
168 } | 235 } |
169 tabLayout.setupWithViewPager((ViewPager) view.findViewById(R.id.custom_p ager)); | 236 tabLayout.setupWithViewPager((ViewPager) view.findViewById(R.id.custom_p ager)); |
170 | 237 |
171 return view; | 238 return view; |
172 } | 239 } |
173 | 240 |
174 @Override | 241 @Override |
175 public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) { | 242 public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) { |
176 mDialog.dismiss(); | 243 mDialog.dismiss(); |
177 mCallback.onResult((int) id); | 244 mCallback.onResult((int) id); |
178 } | 245 } |
179 } | 246 } |
OLD | NEW |