Chromium Code Reviews| 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; |
| 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 touchPointX = params.getTriggeringTouchX() * density; |
| 64 final float touchPointY = params.getTriggeringTouchY() * density; | |
| 61 | 65 |
| 62 mDialog.setOnShowListener(new DialogInterface.OnShowListener() { | 66 mContextMenuDialog = |
| 67 createContextMenuDialog(activity, params, items, touchPointX, to uchPointY); | |
| 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 touchPointY The x-coordinate of the touch that triggered the conte xt menu in pixels. | |
|
Ted C
2017/05/30 23:04:04
I would just put a suffix of Px and then you can d
Daniel Park
2017/05/31 18:19:39
Done.
| |
| 93 * @param touchPointX The y-coordinate of the touch that triggered the conte xt menu in pixels. | |
| 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 intX, |
| 89 Dialog dialog = new Dialog(activity); | 99 float touchPointY) { |
| 90 dialog.setContentView(createPagerView(activity, params, itemGroups)); | 100 View view = LayoutInflater.from(activity).inflate(R.layout.tabular_conte xt_menu, null); |
| 101 | |
| 102 Resources resources = activity.getResources(); | |
| 103 | |
| 104 int contextMenuWidth = | |
| 105 (int) Math.min(resources.getDisplayMetrics().widthPixels * MAX_W IDTH_PROPORTION, | |
|
Ted C
2017/05/30 23:04:04
Does widthPixels change if you rotate the phone?
Daniel Park
2017/05/31 18:19:39
Acknowledged.
Daniel Park
2017/05/31 18:19:39
There's an issue where rotating the screen from la
Ted C
2017/06/02 22:51:40
As discussed offline, that is ok in this context a
| |
| 106 resources.getDimensionPixelSize(R.dimen.context_menu_max _width)); | |
| 107 | |
| 108 mPagerView = initPagerView(activity, params, itemGroups, | |
| 109 (TabularContextMenuViewPager) view.findViewById(R.id.custom_page r)); | |
| 110 mPagerView.getLayoutParams().width = contextMenuWidth; | |
| 111 | |
| 112 final ContextMenuDialog dialog = new ContextMenuDialog(activity, R.style .DialogWhenLarge, | |
| 113 touchPointX, touchPointY, mPagerView, mRenderCoordinates); | |
| 114 dialog.setContentView(view); | |
| 115 | |
| 91 return dialog; | 116 return dialog; |
| 92 } | 117 } |
| 93 | 118 |
| 94 /** | 119 /** |
| 95 * Creates a ViewPageAdapter based off the given list of views. | 120 * Creates a ViewPageAdapter based off the given list of views. |
| 96 * @param activity Used to inflate the new ViewPager | 121 * |
| 122 * @param activity Used to inflate the new ViewPager. | |
| 97 * @param params Used to get the header text. | 123 * @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 | 124 * @param itemGroups The list of views to put into the ViewPager. The string is the title of the |
| 99 * tab | 125 * tab. |
| 126 * @param viewPager The {@link TabularContextMenuViewPager} to initialize. | |
| 100 * @return Returns a complete tabular context menu view. | 127 * @return Returns a complete tabular context menu view. |
| 101 */ | 128 */ |
| 102 @VisibleForTesting | 129 @VisibleForTesting |
| 103 View createPagerView(Activity activity, ContextMenuParams params, | 130 View initPagerView(Activity activity, ContextMenuParams params, |
| 104 List<Pair<Integer, List<ContextMenuItem>>> itemGroups) { | 131 List<Pair<Integer, List<ContextMenuItem>>> itemGroups, |
| 105 View view = LayoutInflater.from(activity).inflate(R.layout.tabular_conte xt_menu, null); | 132 TabularContextMenuViewPager viewPager) { |
| 106 | |
| 107 List<Pair<String, ViewGroup>> viewGroups = new ArrayList<>(); | 133 List<Pair<String, ViewGroup>> viewGroups = new ArrayList<>(); |
| 108 int maxCount = 0; | 134 int maxCount = 0; |
| 109 for (int i = 0; i < itemGroups.size(); i++) { | 135 for (int i = 0; i < itemGroups.size(); i++) { |
| 110 Pair<Integer, List<ContextMenuItem>> itemGroup = itemGroups.get(i); | 136 Pair<Integer, List<ContextMenuItem>> itemGroup = itemGroups.get(i); |
| 111 maxCount = Math.max(maxCount, itemGroup.second.size()); | 137 maxCount = Math.max(maxCount, itemGroup.second.size()); |
| 112 } | 138 } |
| 113 for (int i = 0; i < itemGroups.size(); i++) { | 139 for (int i = 0; i < itemGroups.size(); i++) { |
| 114 Pair<Integer, List<ContextMenuItem>> itemGroup = itemGroups.get(i); | 140 Pair<Integer, List<ContextMenuItem>> itemGroup = itemGroups.get(i); |
| 115 // TODO(tedchoc): Pass the ContextMenuGroup identifier to determine if it's an image. | 141 // TODO(tedchoc): Pass the ContextMenuGroup identifier to determine if it's an image. |
| 116 boolean isImageTab = itemGroup.first == R.string.contextmenu_image_t itle; | 142 boolean isImageTab = itemGroup.first == R.string.contextmenu_image_t itle; |
| 117 viewGroups.add(new Pair<>(activity.getString(itemGroup.first), | 143 viewGroups.add(new Pair<>(activity.getString(itemGroup.first), |
| 118 createContextMenuPageUi( | 144 createContextMenuPageUi( |
| 119 activity, params, itemGroup.second, isImageTab, maxC ount))); | 145 activity, params, itemGroup.second, isImageTab, maxC ount))); |
| 120 } | 146 } |
| 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 | 147 |
| 128 TabularContextMenuViewPager pager = | 148 viewPager.setAdapter(new TabularContextMenuPagerAdapter(viewGroups)); |
| 129 (TabularContextMenuViewPager) view.findViewById(R.id.custom_page r); | 149 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) { | 150 if (itemGroups.size() <= 1) { |
| 134 tabLayout.setVisibility(View.GONE); | 151 tabLayout.setVisibility(View.GONE); |
| 135 } else { | 152 } else { |
| 136 tabLayout.setupWithViewPager((ViewPager) view.findViewById(R.id.cust om_pager)); | 153 tabLayout.setBackgroundResource(R.drawable.grey_with_top_rounded_cor ners); |
| 154 tabLayout.setupWithViewPager(viewPager); | |
| 137 } | 155 } |
| 138 | 156 |
| 139 return view; | 157 return viewPager; |
| 140 } | 158 } |
| 141 | 159 |
| 142 /** | 160 /** |
| 143 * Creates the view of a context menu. Based off the Context Type, it'll adj ust the list of | 161 * 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. | 162 * items and display only the ones that'll be on that specific group. |
| 163 * | |
| 145 * @param activity Used to get the resources of an item. | 164 * @param activity Used to get the resources of an item. |
| 146 * @param params used to create the header text. | 165 * @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. | 166 * @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 . | 167 * @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 | 168 * @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 | 169 * or any other views calculated in the context menu. Used t o estimate the size |
| 151 * of the list. | 170 * of the list. |
| 152 * @return Returns a filled LinearLayout with all the context menu items. | 171 * @return Returns a filled LinearLayout with all the context menu items. |
| 153 */ | 172 */ |
| 154 @VisibleForTesting | 173 @VisibleForTesting |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 165 baseLayout.findViewById(R.id.context_header_layout).setVisibility(Vi ew.VISIBLE); | 184 baseLayout.findViewById(R.id.context_header_layout).setVisibility(Vi ew.VISIBLE); |
| 166 baseLayout.findViewById(R.id.context_divider).setVisibility(View.VIS IBLE); | 185 baseLayout.findViewById(R.id.context_divider).setVisibility(View.VIS IBLE); |
| 167 displayImageHeader(baseLayout, params, activity.getResources()); | 186 displayImageHeader(baseLayout, params, activity.getResources()); |
| 168 } | 187 } |
| 169 | 188 |
| 170 // Set the list adapter and get the height to display it appropriately i n a dialog. | 189 // Set the list adapter and get the height to display it appropriately i n a dialog. |
| 171 Runnable onDirectShare = new Runnable() { | 190 Runnable onDirectShare = new Runnable() { |
| 172 @Override | 191 @Override |
| 173 public void run() { | 192 public void run() { |
| 174 mOnShareItemClicked.run(); | 193 mOnShareItemClicked.run(); |
| 175 mDialog.dismiss(); | 194 mContextMenuDialog.dismiss(); |
| 176 } | 195 } |
| 177 }; | 196 }; |
| 178 TabularContextMenuListAdapter listAdapter = | 197 TabularContextMenuListAdapter listAdapter = |
| 179 new TabularContextMenuListAdapter(items, activity, onDirectShare ); | 198 new TabularContextMenuListAdapter(items, activity, onDirectShare ); |
| 180 ViewGroup.LayoutParams layoutParams = listView.getLayoutParams(); | 199 ViewGroup.LayoutParams layoutParams = listView.getLayoutParams(); |
| 181 layoutParams.height = measureApproximateListViewHeight(listView, listAda pter, maxCount); | 200 layoutParams.height = measureApproximateListViewHeight(listView, listAda pter, maxCount); |
| 182 listView.setLayoutParams(layoutParams); | 201 listView.setLayoutParams(layoutParams); |
| 183 listView.setAdapter(listAdapter); | 202 listView.setAdapter(listAdapter); |
| 184 listView.setOnItemClickListener(this); | 203 listView.setOnItemClickListener(this); |
| 185 | 204 |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 238 BitmapDrawable bm = new BitmapDrawable(resources, bitmap); | 257 BitmapDrawable bm = new BitmapDrawable(resources, bitmap); |
| 239 bm.setTileModeXY(Shader.TileMode.REPEAT, Shader.TileMode.REPEAT); | 258 bm.setTileModeXY(Shader.TileMode.REPEAT, Shader.TileMode.REPEAT); |
| 240 imageView.setVisibility(View.VISIBLE); | 259 imageView.setVisibility(View.VISIBLE); |
| 241 imageView.setBackground(bm); | 260 imageView.setBackground(bm); |
| 242 } | 261 } |
| 243 | 262 |
| 244 /** | 263 /** |
| 245 * To save time measuring the height, this method gets an item if the height has not been | 264 * 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 | 265 * 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. | 266 * height too small as the ListView will scroll through the other values. |
| 267 * | |
| 248 * @param listView The ListView to measure the surrounding padding. | 268 * @param listView The ListView to measure the surrounding padding. |
| 249 * @param listAdapter The adapter which contains the items within the list. | 269 * @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 | 270 * @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. | 271 * of the ListView based off the an item. |
| 252 */ | 272 */ |
| 253 private int measureApproximateListViewHeight( | 273 private int measureApproximateListViewHeight( |
| 254 ListView listView, BaseAdapter listAdapter, int maxCount) { | 274 ListView listView, BaseAdapter listAdapter, int maxCount) { |
| 255 int totalHeight = listView.getPaddingTop() + listView.getPaddingBottom() ; | 275 int totalHeight = listView.getPaddingTop() + listView.getPaddingBottom() ; |
| 256 if (mMenuItemHeight == 0 && !listAdapter.isEmpty()) { | 276 if (mMenuItemHeight == 0 && !listAdapter.isEmpty()) { |
| 257 View view = listAdapter.getView(0, null, listView); | 277 View view = listAdapter.getView(0, null, listView); |
| 258 view.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UN SPECIFIED), | 278 view.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UN SPECIFIED), |
| 259 View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECI FIED)); | 279 View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECI FIED)); |
| 260 mMenuItemHeight = view.getMeasuredHeight(); | 280 mMenuItemHeight = view.getMeasuredHeight(); |
| 261 } | 281 } |
| 262 return totalHeight + mMenuItemHeight * maxCount; | 282 return totalHeight + mMenuItemHeight * maxCount; |
| 263 } | 283 } |
| 264 | 284 |
| 265 /** | 285 /** |
| 266 * When an thumbnail is retrieved for the header of an image, this will set the header to | 286 * When an thumbnail is retrieved for the header of an image, this will set the header to that |
| 267 * that particular bitmap. | 287 * particular bitmap. |
| 268 */ | 288 */ |
| 269 public void onImageThumbnailRetrieved(Bitmap bitmap) { | 289 public void onImageThumbnailRetrieved(Bitmap bitmap) { |
| 270 if (mHeaderImageView != null) { | 290 if (mHeaderImageView != null) { |
| 271 mHeaderImageView.setImageBitmap(bitmap); | 291 mHeaderImageView.setImageBitmap(bitmap); |
| 272 } | 292 } |
| 273 } | 293 } |
| 274 | 294 |
| 275 @Override | 295 @Override |
| 276 public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) { | 296 public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) { |
| 277 mDialog.dismiss(); | 297 mContextMenuDialog.dismiss(); |
| 278 mCallback.onResult((int) id); | 298 mCallback.onResult((int) id); |
| 279 } | 299 } |
| 300 | |
| 301 /** | |
| 302 * Gives this class access to the render coordinates to allow access to the total size of the | |
| 303 * toolbar and tab strip. | |
| 304 */ | |
| 305 public void setRenderCoordinates(RenderCoordinates renderCoordinates) { | |
| 306 mRenderCoordinates = renderCoordinates; | |
| 307 } | |
| 280 } | 308 } |
| OLD | NEW |