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