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.content.Context; | 7 import android.app.Activity; |
8 import android.graphics.drawable.Drawable; | 8 import android.graphics.drawable.Drawable; |
| 9 import android.util.Pair; |
9 import android.view.LayoutInflater; | 10 import android.view.LayoutInflater; |
10 import android.view.View; | 11 import android.view.View; |
11 import android.view.ViewGroup; | 12 import android.view.ViewGroup; |
12 import android.widget.BaseAdapter; | 13 import android.widget.BaseAdapter; |
13 import android.widget.ImageView; | 14 import android.widget.ImageView; |
14 import android.widget.TextView; | 15 import android.widget.TextView; |
15 | 16 |
16 import org.chromium.chrome.R; | 17 import org.chromium.chrome.R; |
| 18 import org.chromium.chrome.browser.share.ShareHelper; |
17 | 19 |
18 import java.util.List; | 20 import java.util.List; |
19 | 21 |
20 /** | 22 /** |
21 * Takes a list of {@link ContextMenuItem} and puts them in an adapter meant to
be used within a | 23 * Takes a list of {@link ContextMenuItem} and puts them in an adapter meant to
be used within a |
22 * list view. | 24 * list view. |
23 */ | 25 */ |
24 class TabularContextMenuListAdapter extends BaseAdapter { | 26 class TabularContextMenuListAdapter extends BaseAdapter { |
25 private final List<ContextMenuItem> mMenuItems; | 27 private final List<ContextMenuItem> mMenuItems; |
26 private final Context mContext; | 28 private final Activity mActivity; |
| 29 private final Runnable mOnShareItemClicked; |
27 | 30 |
28 /** | 31 /** |
29 * Adapter for the tabular context menu UI | 32 * Adapter for the tabular context menu UI |
30 * @param menuItems The list of items to display in the view. | 33 * @param menuItems The list of items to display in the view. |
31 * @param context Used to inflate the layout. | 34 * @param activity Used to inflate the layout. |
32 */ | 35 */ |
33 TabularContextMenuListAdapter(List<ContextMenuItem> menuItems, Context conte
xt) { | 36 TabularContextMenuListAdapter( |
| 37 List<ContextMenuItem> menuItems, Activity activity, Runnable onShare
ItemClicked) { |
34 mMenuItems = menuItems; | 38 mMenuItems = menuItems; |
35 mContext = context; | 39 mActivity = activity; |
| 40 mOnShareItemClicked = onShareItemClicked; |
36 } | 41 } |
37 | 42 |
38 @Override | 43 @Override |
39 public int getCount() { | 44 public int getCount() { |
40 return mMenuItems.size(); | 45 return mMenuItems.size(); |
41 } | 46 } |
42 | 47 |
43 @Override | 48 @Override |
44 public Object getItem(int position) { | 49 public Object getItem(int position) { |
45 return mMenuItems.get(position); | 50 return mMenuItems.get(position); |
46 } | 51 } |
47 | 52 |
48 @Override | 53 @Override |
49 public long getItemId(int position) { | 54 public long getItemId(int position) { |
50 return mMenuItems.get(position).menuId; | 55 return mMenuItems.get(position).menuId; |
51 } | 56 } |
52 | 57 |
53 @Override | 58 @Override |
54 public View getView(int position, View convertView, ViewGroup parent) { | 59 public View getView(int position, View convertView, ViewGroup parent) { |
55 ContextMenuItem menuItem = mMenuItems.get(position); | 60 ContextMenuItem menuItem = mMenuItems.get(position); |
56 ViewHolderItem viewHolder; | 61 ViewHolderItem viewHolder; |
57 | 62 |
58 if (convertView == null) { | 63 if (convertView == null) { |
59 LayoutInflater inflater = LayoutInflater.from(mContext); | 64 LayoutInflater inflater = LayoutInflater.from(mActivity); |
60 convertView = inflater.inflate(R.layout.tabular_context_menu_row, nu
ll); | 65 convertView = inflater.inflate(R.layout.tabular_context_menu_row, nu
ll); |
61 | 66 |
62 viewHolder = new ViewHolderItem(); | 67 viewHolder = new ViewHolderItem(); |
63 viewHolder.mIcon = (ImageView) convertView.findViewById(R.id.context
_menu_icon); | 68 viewHolder.mIcon = (ImageView) convertView.findViewById(R.id.context
_menu_icon); |
64 viewHolder.mText = (TextView) convertView.findViewById(R.id.context_
text); | 69 viewHolder.mText = (TextView) convertView.findViewById(R.id.context_
text); |
| 70 viewHolder.mShareIcon = |
| 71 (ImageView) convertView.findViewById(R.id.context_menu_share
_icon); |
65 | 72 |
66 convertView.setTag(viewHolder); | 73 convertView.setTag(viewHolder); |
67 } else { | 74 } else { |
68 viewHolder = (ViewHolderItem) convertView.getTag(); | 75 viewHolder = (ViewHolderItem) convertView.getTag(); |
69 } | 76 } |
70 | 77 |
71 viewHolder.mText.setText(menuItem.getString(mContext)); | 78 viewHolder.mText.setText(menuItem.getString(mActivity)); |
72 Drawable icon = menuItem.getDrawableAndDescription(mContext); | 79 Drawable icon = menuItem.getDrawableAndDescription(mActivity); |
73 viewHolder.mIcon.setImageDrawable(icon); | 80 viewHolder.mIcon.setImageDrawable(icon); |
74 viewHolder.mIcon.setVisibility(icon != null ? View.VISIBLE : View.INVISI
BLE); | 81 viewHolder.mIcon.setVisibility(icon != null ? View.VISIBLE : View.INVISI
BLE); |
| 82 |
| 83 if (menuItem == ContextMenuItem.SHARE_IMAGE) { |
| 84 final Pair<Drawable, CharSequence> shareInfo = |
| 85 ShareHelper.getShareableIconAndName(mActivity); |
| 86 if (shareInfo.first != null) { |
| 87 viewHolder.mShareIcon.setImageDrawable(shareInfo.first); |
| 88 viewHolder.mShareIcon.setVisibility(View.VISIBLE); |
| 89 viewHolder.mShareIcon.setContentDescription(mActivity.getString( |
| 90 R.string.accessibility_menu_share_via, shareInfo.second)
); |
| 91 viewHolder.mShareIcon.setOnClickListener(new View.OnClickListene
r() { |
| 92 @Override |
| 93 public void onClick(View view) { |
| 94 mOnShareItemClicked.run(); |
| 95 } |
| 96 }); |
| 97 } |
| 98 } else { |
| 99 viewHolder.mShareIcon.setVisibility(View.GONE); |
| 100 } |
| 101 |
75 return convertView; | 102 return convertView; |
76 } | 103 } |
77 | 104 |
78 private static class ViewHolderItem { | 105 private static class ViewHolderItem { |
79 ImageView mIcon; | 106 ImageView mIcon; |
80 TextView mText; | 107 TextView mText; |
| 108 ImageView mShareIcon; |
81 } | 109 } |
82 } | 110 } |
OLD | NEW |