OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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.content.Context; | 8 import android.content.ComponentName; |
9 import android.graphics.Bitmap; | 9 import android.graphics.Bitmap; |
10 import android.graphics.BitmapFactory; | 10 import android.graphics.BitmapFactory; |
11 import android.util.Pair; | 11 import android.util.Pair; |
12 import android.view.ContextMenu; | 12 import android.view.ContextMenu; |
13 import android.view.ContextMenu.ContextMenuInfo; | 13 import android.view.ContextMenu.ContextMenuInfo; |
14 import android.view.View; | 14 import android.view.View; |
15 import android.view.View.OnCreateContextMenuListener; | 15 import android.view.View.OnCreateContextMenuListener; |
16 | 16 |
17 import org.chromium.base.Callback; | 17 import org.chromium.base.Callback; |
18 import org.chromium.base.VisibleForTesting; | 18 import org.chromium.base.VisibleForTesting; |
19 import org.chromium.base.annotations.CalledByNative; | 19 import org.chromium.base.annotations.CalledByNative; |
20 import org.chromium.base.metrics.RecordHistogram; | 20 import org.chromium.base.metrics.RecordHistogram; |
21 import org.chromium.chrome.browser.ChromeFeatureList; | 21 import org.chromium.chrome.browser.ChromeFeatureList; |
22 import org.chromium.chrome.browser.share.ShareHelper; | 22 import org.chromium.chrome.browser.share.ShareHelper; |
23 import org.chromium.content.browser.ContentViewCore; | 23 import org.chromium.content.browser.ContentViewCore; |
24 import org.chromium.content_public.browser.WebContents; | 24 import org.chromium.content_public.browser.WebContents; |
25 import org.chromium.ui.base.WindowAndroid; | 25 import org.chromium.ui.base.WindowAndroid; |
26 import org.chromium.ui.base.WindowAndroid.OnCloseContextMenuListener; | 26 import org.chromium.ui.base.WindowAndroid.OnCloseContextMenuListener; |
27 | 27 |
28 import java.util.List; | 28 import java.util.List; |
29 | 29 |
30 /** | 30 /** |
31 * A helper class that handles generating context menus for {@link ContentViewCo re}s. | 31 * A helper class that handles generating context menus for {@link ContentViewCo re}s. |
32 */ | 32 */ |
33 public class ContextMenuHelper implements OnCreateContextMenuListener { | 33 public class ContextMenuHelper implements OnCreateContextMenuListener { |
34 private long mNativeContextMenuHelper; | 34 private long mNativeContextMenuHelper; |
35 | 35 |
36 private ContextMenuPopulator mPopulator; | 36 private ContextMenuPopulator mPopulator; |
37 private ContextMenuParams mCurrentContextMenuParams; | 37 private ContextMenuParams mCurrentContextMenuParams; |
38 private Context mContext; | 38 private Activity mActivity; |
39 private Callback<Integer> mCallback; | 39 private Callback<Integer> mCallback; |
40 private Runnable mOnMenuShown; | 40 private Runnable mOnMenuShown; |
41 private Runnable mOnMenuClosed; | 41 private Runnable mOnMenuClosed; |
42 private OnThumbnailReceivedListener mOnThumbnailReceivedListener; | 42 private OnThumbnailReceivedListener mOnThumbnailReceivedListener; |
43 private ComponentName mComponentName; | |
43 | 44 |
44 /** | 45 /** |
45 * This waits for a thumbnail to be retrieved by the native code. | 46 * This waits for a thumbnail to be retrieved by the native code. |
46 */ | 47 */ |
47 interface OnThumbnailReceivedListener { | 48 interface OnThumbnailReceivedListener { |
48 /** | 49 /** |
49 * When the thumbnail is received it will send the thumbnail via this me thod. This is | 50 * When the thumbnail is received it will send the thumbnail via this me thod. This is |
50 * activated after calling {@link #getThumbnail()}. | 51 * activated after calling {@link #getThumbnail()}. |
51 * @param bitmap The bitmap that is retrieved from native code. | 52 * @param bitmap The bitmap that is retrieved from native code. |
52 */ | 53 */ |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
86 * @param contentViewCore The {@link ContentViewCore} to show the menu to. | 87 * @param contentViewCore The {@link ContentViewCore} to show the menu to. |
87 * @param params The {@link ContextMenuParams} that indicate what m enu items to show. | 88 * @param params The {@link ContextMenuParams} that indicate what m enu items to show. |
88 */ | 89 */ |
89 @CalledByNative | 90 @CalledByNative |
90 private void showContextMenu(final ContentViewCore contentViewCore, ContextM enuParams params) { | 91 private void showContextMenu(final ContentViewCore contentViewCore, ContextM enuParams params) { |
91 if (params.isFile()) return; | 92 if (params.isFile()) return; |
92 View view = contentViewCore.getContainerView(); | 93 View view = contentViewCore.getContainerView(); |
93 final WindowAndroid windowAndroid = contentViewCore.getWindowAndroid(); | 94 final WindowAndroid windowAndroid = contentViewCore.getWindowAndroid(); |
94 | 95 |
95 if (view == null || view.getVisibility() != View.VISIBLE || view.getPare nt() == null | 96 if (view == null || view.getVisibility() != View.VISIBLE || view.getPare nt() == null |
96 || windowAndroid == null || windowAndroid.getContext().get() == null | 97 || windowAndroid == null || windowAndroid.getActivity().get() == null |
97 || mPopulator == null) { | 98 || mPopulator == null) { |
98 return; | 99 return; |
99 } | 100 } |
100 | 101 |
101 mCurrentContextMenuParams = params; | 102 mCurrentContextMenuParams = params; |
102 mContext = windowAndroid.getContext().get(); | 103 mActivity = windowAndroid.getActivity().get(); |
103 mCallback = new Callback<Integer>() { | 104 mCallback = new Callback<Integer>() { |
104 @Override | 105 @Override |
105 public void onResult(Integer result) { | 106 public void onResult(Integer result) { |
106 mPopulator.onItemSelected( | 107 mPopulator.onItemSelected( |
107 ContextMenuHelper.this, mCurrentContextMenuParams, resul t); | 108 ContextMenuHelper.this, mCurrentContextMenuParams, resul t); |
108 } | 109 } |
109 }; | 110 }; |
110 mOnMenuShown = new Runnable() { | 111 mOnMenuShown = new Runnable() { |
111 @Override | 112 @Override |
112 public void run() { | 113 public void run() { |
113 WebContents webContents = contentViewCore.getWebContents(); | 114 WebContents webContents = contentViewCore.getWebContents(); |
114 RecordHistogram.recordBooleanHistogram("ContextMenu.Shown", webC ontents != null); | 115 RecordHistogram.recordBooleanHistogram("ContextMenu.Shown", webC ontents != null); |
115 } | 116 } |
116 }; | 117 }; |
117 mOnMenuClosed = new Runnable() { | 118 mOnMenuClosed = new Runnable() { |
118 @Override | 119 @Override |
119 public void run() { | 120 public void run() { |
120 if (mNativeContextMenuHelper == 0) return; | 121 if (mNativeContextMenuHelper == 0) return; |
121 nativeOnContextMenuClosed(mNativeContextMenuHelper); | 122 nativeOnContextMenuClosed(mNativeContextMenuHelper); |
122 } | 123 } |
123 }; | 124 }; |
124 | 125 |
125 if (ChromeFeatureList.isEnabled(ChromeFeatureList.CUSTOM_CONTEXT_MENU)) { | 126 if (ChromeFeatureList.isEnabled(ChromeFeatureList.CUSTOM_CONTEXT_MENU)) { |
126 List<Pair<Integer, List<ContextMenuItem>>> items = | 127 List<Pair<Integer, List<ContextMenuItem>>> items = |
127 mPopulator.buildContextMenu(null, mContext, mCurrentContextM enuParams); | 128 mPopulator.buildContextMenu(null, mActivity, mCurrentContext MenuParams); |
128 | 129 |
129 ContextMenuUi menuUi = new TabularContextMenuUi(this); | 130 ContextMenuUi menuUi = new TabularContextMenuUi(this); |
130 menuUi.displayMenu(mContext, mCurrentContextMenuParams, items, mCall back, mOnMenuShown, | 131 menuUi.displayMenu(mActivity, mCurrentContextMenuParams, items, mCal lback, mOnMenuShown, |
131 mOnMenuClosed); | 132 mOnMenuClosed); |
132 return; | 133 return; |
133 } | 134 } |
134 | 135 |
135 // The Platform Context Menu requires the listener within this hepler si nce this helper and | 136 // The Platform Context Menu requires the listener within this hepler si nce this helper and |
136 // provides context menu for us to show. | 137 // provides context menu for us to show. |
137 view.setOnCreateContextMenuListener(this); | 138 view.setOnCreateContextMenuListener(this); |
138 if (view.showContextMenu()) { | 139 if (view.showContextMenu()) { |
139 mOnMenuShown.run(); | 140 mOnMenuShown.run(); |
140 windowAndroid.addContextMenuCloseListener(new OnCloseContextMenuList ener() { | 141 windowAndroid.addContextMenuCloseListener(new OnCloseContextMenuList ener() { |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
172 if (mNativeContextMenuHelper == 0) return; | 173 if (mNativeContextMenuHelper == 0) return; |
173 nativeShareImage(mNativeContextMenuHelper); | 174 nativeShareImage(mNativeContextMenuHelper); |
174 } | 175 } |
175 | 176 |
176 @CalledByNative | 177 @CalledByNative |
177 private void onShareImageReceived( | 178 private void onShareImageReceived( |
178 WindowAndroid windowAndroid, byte[] jpegImageData) { | 179 WindowAndroid windowAndroid, byte[] jpegImageData) { |
179 Activity activity = windowAndroid.getActivity().get(); | 180 Activity activity = windowAndroid.getActivity().get(); |
180 if (activity == null) return; | 181 if (activity == null) return; |
181 | 182 |
182 ShareHelper.shareImage(activity, jpegImageData); | 183 ShareHelper.shareImage(activity, jpegImageData, mComponentName); |
184 // This needs to be reset to null after a share. This way the next time a user shares an | |
185 // image it won't share with the last shared app unless explicitly told. | |
186 mComponentName = null; | |
183 } | 187 } |
184 | 188 |
185 /** | 189 /** |
190 * Share image triggered with the current context menu directly with a speci fic app. | |
191 * @param name The {@link ComponentName} of the app to share the image direc tly with. | |
192 */ | |
193 public void shareImageDirectly(ComponentName name) { | |
194 mComponentName = name; | |
David Trainor- moved to gerrit
2017/03/30 23:50:22
This would be cleaner if we used final and magical
JJ
2017/03/31 20:58:54
Acknowledged.
| |
195 shareImage(); | |
196 } | |
197 | |
198 /** | |
186 * Gets the thumbnail of the current image that triggered the context menu. | 199 * Gets the thumbnail of the current image that triggered the context menu. |
187 * @param listener Called once the the thumbnail is received | 200 * @param listener Called once the the thumbnail is received |
188 */ | 201 */ |
189 public void getThumbnail(OnThumbnailReceivedListener listener) { | 202 public void getThumbnail(OnThumbnailReceivedListener listener) { |
190 mOnThumbnailReceivedListener = listener; | 203 mOnThumbnailReceivedListener = listener; |
191 if (mNativeContextMenuHelper == 0) return; | 204 if (mNativeContextMenuHelper == 0) return; |
192 nativeRetrieveHeaderThumbnail(mNativeContextMenuHelper); | 205 nativeRetrieveHeaderThumbnail(mNativeContextMenuHelper); |
193 } | 206 } |
194 | 207 |
195 @CalledByNative | 208 @CalledByNative |
196 private void onHeaderThumbnailReceived(WindowAndroid windowAndroid, byte[] j pegImageData) { | 209 private void onHeaderThumbnailReceived(WindowAndroid windowAndroid, byte[] j pegImageData) { |
197 Bitmap bitmap = BitmapFactory.decodeByteArray(jpegImageData, 0, jpegImag eData.length); | 210 Bitmap bitmap = BitmapFactory.decodeByteArray(jpegImageData, 0, jpegImag eData.length); |
198 if (mOnThumbnailReceivedListener != null) { | 211 if (mOnThumbnailReceivedListener != null) { |
199 mOnThumbnailReceivedListener.onThumbnailReceived(bitmap); | 212 mOnThumbnailReceivedListener.onThumbnailReceived(bitmap); |
200 } | 213 } |
201 } | 214 } |
202 | 215 |
203 @Override | 216 @Override |
204 public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo me nuInfo) { | 217 public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo me nuInfo) { |
205 List<Pair<Integer, List<ContextMenuItem>>> items = | 218 List<Pair<Integer, List<ContextMenuItem>>> items = |
206 mPopulator.buildContextMenu(menu, v.getContext(), mCurrentContex tMenuParams); | 219 mPopulator.buildContextMenu(menu, v.getContext(), mCurrentContex tMenuParams); |
207 ContextMenuUi menuUi = new PlatformContextMenuUi(menu); | 220 ContextMenuUi menuUi = new PlatformContextMenuUi(menu); |
208 menuUi.displayMenu( | 221 menuUi.displayMenu(mActivity, mCurrentContextMenuParams, items, mCallbac k, mOnMenuShown, |
209 mContext, mCurrentContextMenuParams, items, mCallback, mOnMenuSh own, mOnMenuClosed); | 222 mOnMenuClosed); |
210 } | 223 } |
211 | 224 |
212 /** | 225 /** |
213 * @return The {@link ContextMenuPopulator} responsible for populating the c ontext menu. | 226 * @return The {@link ContextMenuPopulator} responsible for populating the c ontext menu. |
214 */ | 227 */ |
215 @VisibleForTesting | 228 @VisibleForTesting |
216 public ContextMenuPopulator getPopulator() { | 229 public ContextMenuPopulator getPopulator() { |
217 return mPopulator; | 230 return mPopulator; |
218 } | 231 } |
219 | 232 |
220 private native void nativeOnStartDownload( | 233 private native void nativeOnStartDownload( |
221 long nativeContextMenuHelper, boolean isLink, boolean isDataReductio nProxyEnabled); | 234 long nativeContextMenuHelper, boolean isLink, boolean isDataReductio nProxyEnabled); |
222 private native void nativeSearchForImage(long nativeContextMenuHelper); | 235 private native void nativeSearchForImage(long nativeContextMenuHelper); |
223 private native void nativeShareImage(long nativeContextMenuHelper); | 236 private native void nativeShareImage(long nativeContextMenuHelper); |
224 private native void nativeOnContextMenuClosed(long nativeContextMenuHelper); | 237 private native void nativeOnContextMenuClosed(long nativeContextMenuHelper); |
225 private native void nativeRetrieveHeaderThumbnail(long nativeContextMenuHelp er); | 238 private native void nativeRetrieveHeaderThumbnail(long nativeContextMenuHelp er); |
226 } | 239 } |
OLD | NEW |