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 ContextMenuHelper mContextMenuHelper; |
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. |
| 35 * @param contextMenuHelper Used to directly share an image through an icon. |
32 */ | 36 */ |
33 TabularContextMenuListAdapter(List<ContextMenuItem> menuItems, Context conte
xt) { | 37 TabularContextMenuListAdapter(List<ContextMenuItem> menuItems, Activity acti
vity, |
| 38 ContextMenuHelper contextMenuHelper) { |
34 mMenuItems = menuItems; | 39 mMenuItems = menuItems; |
35 mContext = context; | 40 mActivity = activity; |
| 41 mContextMenuHelper = contextMenuHelper; |
36 } | 42 } |
37 | 43 |
38 @Override | 44 @Override |
39 public int getCount() { | 45 public int getCount() { |
40 return mMenuItems.size(); | 46 return mMenuItems.size(); |
41 } | 47 } |
42 | 48 |
43 @Override | 49 @Override |
44 public Object getItem(int position) { | 50 public Object getItem(int position) { |
45 return mMenuItems.get(position); | 51 return mMenuItems.get(position); |
46 } | 52 } |
47 | 53 |
48 @Override | 54 @Override |
49 public long getItemId(int position) { | 55 public long getItemId(int position) { |
50 return mMenuItems.get(position).menuId; | 56 return mMenuItems.get(position).menuId; |
51 } | 57 } |
52 | 58 |
53 @Override | 59 @Override |
54 public View getView(int position, View convertView, ViewGroup parent) { | 60 public View getView(int position, View convertView, ViewGroup parent) { |
55 ContextMenuItem menuItem = mMenuItems.get(position); | 61 ContextMenuItem menuItem = mMenuItems.get(position); |
56 ViewHolderItem viewHolder; | 62 ViewHolderItem viewHolder; |
57 | 63 |
58 if (convertView == null) { | 64 if (convertView == null) { |
59 LayoutInflater inflater = LayoutInflater.from(mContext); | 65 LayoutInflater inflater = LayoutInflater.from(mActivity); |
60 convertView = inflater.inflate(R.layout.tabular_context_menu_row, nu
ll); | 66 convertView = inflater.inflate(R.layout.tabular_context_menu_row, nu
ll); |
61 | 67 |
62 viewHolder = new ViewHolderItem(); | 68 viewHolder = new ViewHolderItem(); |
63 viewHolder.mIcon = (ImageView) convertView.findViewById(R.id.context
_menu_icon); | 69 viewHolder.mIcon = (ImageView) convertView.findViewById(R.id.context
_menu_icon); |
64 viewHolder.mText = (TextView) convertView.findViewById(R.id.context_
text); | 70 viewHolder.mText = (TextView) convertView.findViewById(R.id.context_
text); |
| 71 viewHolder.mShareIcon = |
| 72 (ImageView) convertView.findViewById(R.id.context_menu_share
_icon); |
65 | 73 |
66 convertView.setTag(viewHolder); | 74 convertView.setTag(viewHolder); |
67 } else { | 75 } else { |
68 viewHolder = (ViewHolderItem) convertView.getTag(); | 76 viewHolder = (ViewHolderItem) convertView.getTag(); |
69 } | 77 } |
70 | 78 |
71 viewHolder.mText.setText(menuItem.getString(mContext)); | 79 viewHolder.mText.setText(menuItem.getString(mActivity)); |
72 Drawable icon = menuItem.getDrawableAndDescription(mContext); | 80 Drawable icon = menuItem.getDrawableAndDescription(mActivity); |
73 viewHolder.mIcon.setImageDrawable(icon); | 81 viewHolder.mIcon.setImageDrawable(icon); |
74 viewHolder.mIcon.setVisibility(icon != null ? View.VISIBLE : View.INVISI
BLE); | 82 viewHolder.mIcon.setVisibility(icon != null ? View.VISIBLE : View.INVISI
BLE); |
75 | 83 |
| 84 if (menuItem == ContextMenuItem.SHARE_IMAGE) { |
| 85 final Pair<Drawable, CharSequence> shareInfo = |
| 86 ShareHelper.getShareableIconAndName(mActivity); |
| 87 if (shareInfo.first != null) { |
| 88 viewHolder.mShareIcon.setImageDrawable(shareInfo.first); |
| 89 viewHolder.mShareIcon.setVisibility(View.VISIBLE); |
| 90 viewHolder.mShareIcon.setContentDescription(mActivity.getString( |
| 91 R.string.accessibility_menu_share_via, shareInfo.second)
); |
| 92 viewHolder.mShareIcon.setOnClickListener(new View.OnClickListene
r() { |
| 93 @Override |
| 94 public void onClick(View view) { |
| 95 mContextMenuHelper.shareImageDirectly( |
| 96 ShareHelper.getLastShareComponentName()); |
| 97 } |
| 98 }); |
| 99 } |
| 100 } else { |
| 101 viewHolder.mShareIcon.setVisibility(View.GONE); |
| 102 } |
| 103 |
76 return convertView; | 104 return convertView; |
77 } | 105 } |
78 | 106 |
79 private static class ViewHolderItem { | 107 private static class ViewHolderItem { |
80 ImageView mIcon; | 108 ImageView mIcon; |
81 TextView mText; | 109 TextView mText; |
| 110 ImageView mShareIcon; |
82 } | 111 } |
83 } | 112 } |
OLD | NEW |