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; |
(...skipping 10 matching lines...) Expand all Loading... |
29 import java.util.List; | 29 import java.util.List; |
30 | 30 |
31 /** | 31 /** |
32 * A helper class that handles generating context menus for {@link ContentViewCo
re}s. | 32 * A helper class that handles generating context menus for {@link ContentViewCo
re}s. |
33 */ | 33 */ |
34 public class ContextMenuHelper implements OnCreateContextMenuListener { | 34 public class ContextMenuHelper implements OnCreateContextMenuListener { |
35 private long mNativeContextMenuHelper; | 35 private long mNativeContextMenuHelper; |
36 | 36 |
37 private ContextMenuPopulator mPopulator; | 37 private ContextMenuPopulator mPopulator; |
38 private ContextMenuParams mCurrentContextMenuParams; | 38 private ContextMenuParams mCurrentContextMenuParams; |
39 private Context mContext; | 39 private Activity mActivity; |
40 private Callback<Integer> mCallback; | 40 private Callback<Integer> mCallback; |
41 private Runnable mOnMenuShown; | 41 private Runnable mOnMenuShown; |
42 private Runnable mOnMenuClosed; | 42 private Runnable mOnMenuClosed; |
43 private Callback<Bitmap> mOnThumbnailReceivedCallback; | 43 private Callback<Bitmap> mOnThumbnailReceivedCallback; |
| 44 private ComponentName mComponentName; |
44 | 45 |
45 private ContextMenuHelper(long nativeContextMenuHelper) { | 46 private ContextMenuHelper(long nativeContextMenuHelper) { |
46 mNativeContextMenuHelper = nativeContextMenuHelper; | 47 mNativeContextMenuHelper = nativeContextMenuHelper; |
47 } | 48 } |
48 | 49 |
49 @CalledByNative | 50 @CalledByNative |
50 private static ContextMenuHelper create(long nativeContextMenuHelper) { | 51 private static ContextMenuHelper create(long nativeContextMenuHelper) { |
51 return new ContextMenuHelper(nativeContextMenuHelper); | 52 return new ContextMenuHelper(nativeContextMenuHelper); |
52 } | 53 } |
53 | 54 |
(...skipping 18 matching lines...) Expand all Loading... |
72 * @param contentViewCore The {@link ContentViewCore} to show the menu to. | 73 * @param contentViewCore The {@link ContentViewCore} to show the menu to. |
73 * @param params The {@link ContextMenuParams} that indicate what m
enu items to show. | 74 * @param params The {@link ContextMenuParams} that indicate what m
enu items to show. |
74 */ | 75 */ |
75 @CalledByNative | 76 @CalledByNative |
76 private void showContextMenu(final ContentViewCore contentViewCore, ContextM
enuParams params) { | 77 private void showContextMenu(final ContentViewCore contentViewCore, ContextM
enuParams params) { |
77 if (params.isFile()) return; | 78 if (params.isFile()) return; |
78 View view = contentViewCore.getContainerView(); | 79 View view = contentViewCore.getContainerView(); |
79 final WindowAndroid windowAndroid = contentViewCore.getWindowAndroid(); | 80 final WindowAndroid windowAndroid = contentViewCore.getWindowAndroid(); |
80 | 81 |
81 if (view == null || view.getVisibility() != View.VISIBLE || view.getPare
nt() == null | 82 if (view == null || view.getVisibility() != View.VISIBLE || view.getPare
nt() == null |
82 || windowAndroid == null || windowAndroid.getContext().get() ==
null | 83 || windowAndroid == null || windowAndroid.getActivity().get() ==
null |
83 || mPopulator == null) { | 84 || mPopulator == null) { |
84 return; | 85 return; |
85 } | 86 } |
86 | 87 |
87 mCurrentContextMenuParams = params; | 88 mCurrentContextMenuParams = params; |
88 mContext = windowAndroid.getContext().get(); | 89 mActivity = windowAndroid.getActivity().get(); |
89 mCallback = new Callback<Integer>() { | 90 mCallback = new Callback<Integer>() { |
90 @Override | 91 @Override |
91 public void onResult(Integer result) { | 92 public void onResult(Integer result) { |
92 mPopulator.onItemSelected( | 93 mPopulator.onItemSelected( |
93 ContextMenuHelper.this, mCurrentContextMenuParams, resul
t); | 94 ContextMenuHelper.this, mCurrentContextMenuParams, resul
t); |
94 } | 95 } |
95 }; | 96 }; |
96 mOnMenuShown = new Runnable() { | 97 mOnMenuShown = new Runnable() { |
97 @Override | 98 @Override |
98 public void run() { | 99 public void run() { |
99 WebContents webContents = contentViewCore.getWebContents(); | 100 WebContents webContents = contentViewCore.getWebContents(); |
100 RecordHistogram.recordBooleanHistogram("ContextMenu.Shown", webC
ontents != null); | 101 RecordHistogram.recordBooleanHistogram("ContextMenu.Shown", webC
ontents != null); |
101 } | 102 } |
102 }; | 103 }; |
103 mOnMenuClosed = new Runnable() { | 104 mOnMenuClosed = new Runnable() { |
104 @Override | 105 @Override |
105 public void run() { | 106 public void run() { |
106 if (mNativeContextMenuHelper == 0) return; | 107 if (mNativeContextMenuHelper == 0) return; |
107 nativeOnContextMenuClosed(mNativeContextMenuHelper); | 108 nativeOnContextMenuClosed(mNativeContextMenuHelper); |
108 } | 109 } |
109 }; | 110 }; |
110 | 111 |
111 if (ChromeFeatureList.isEnabled(ChromeFeatureList.CUSTOM_CONTEXT_MENU))
{ | 112 if (ChromeFeatureList.isEnabled(ChromeFeatureList.CUSTOM_CONTEXT_MENU))
{ |
112 List<Pair<Integer, List<ContextMenuItem>>> items = | 113 List<Pair<Integer, List<ContextMenuItem>>> items = |
113 mPopulator.buildContextMenu(null, mContext, mCurrentContextM
enuParams); | 114 mPopulator.buildContextMenu(null, mActivity, mCurrentContext
MenuParams); |
114 | 115 |
115 final ContextMenuUi menuUi = new TabularContextMenuUi(); | 116 final ContextMenuUi menuUi = new TabularContextMenuUi(new Runnable()
{ |
116 menuUi.displayMenu(mContext, mCurrentContextMenuParams, items, mCall
back, mOnMenuShown, | 117 @Override |
| 118 public void run() { |
| 119 shareImageDirectly(ShareHelper.getLastShareComponentName()); |
| 120 } |
| 121 }); |
| 122 menuUi.displayMenu(mActivity, mCurrentContextMenuParams, items, mCal
lback, mOnMenuShown, |
117 mOnMenuClosed); | 123 mOnMenuClosed); |
118 if (mCurrentContextMenuParams.isImage()) { | 124 if (mCurrentContextMenuParams.isImage()) { |
119 getThumbnail(new Callback<Bitmap>() { | 125 getThumbnail(new Callback<Bitmap>() { |
120 @Override | 126 @Override |
121 public void onResult(Bitmap result) { | 127 public void onResult(Bitmap result) { |
122 ((TabularContextMenuUi) menuUi).onImageThumbnailRetrieve
d(result); | 128 ((TabularContextMenuUi) menuUi).onImageThumbnailRetrieve
d(result); |
123 } | 129 } |
124 }); | 130 }); |
125 } | 131 } |
126 return; | 132 return; |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
166 if (mNativeContextMenuHelper == 0) return; | 172 if (mNativeContextMenuHelper == 0) return; |
167 nativeShareImage(mNativeContextMenuHelper); | 173 nativeShareImage(mNativeContextMenuHelper); |
168 } | 174 } |
169 | 175 |
170 @CalledByNative | 176 @CalledByNative |
171 private void onShareImageReceived( | 177 private void onShareImageReceived( |
172 WindowAndroid windowAndroid, byte[] jpegImageData) { | 178 WindowAndroid windowAndroid, byte[] jpegImageData) { |
173 Activity activity = windowAndroid.getActivity().get(); | 179 Activity activity = windowAndroid.getActivity().get(); |
174 if (activity == null) return; | 180 if (activity == null) return; |
175 | 181 |
176 ShareHelper.shareImage(activity, jpegImageData); | 182 ShareHelper.shareImage(activity, jpegImageData, mComponentName); |
| 183 // This needs to be reset to null after a share. This way the next time
a user shares an |
| 184 // image it won't share with the last shared app unless explicitly told. |
| 185 mComponentName = null; |
177 } | 186 } |
178 | 187 |
179 /** | 188 /** |
| 189 * Share image triggered with the current context menu directly with a speci
fic app. |
| 190 * @param name The {@link ComponentName} of the app to share the image direc
tly with. |
| 191 */ |
| 192 public void shareImageDirectly(ComponentName name) { |
| 193 mComponentName = name; |
| 194 shareImage(); |
| 195 } |
| 196 |
| 197 /** |
180 * Gets the thumbnail of the current image that triggered the context menu. | 198 * Gets the thumbnail of the current image that triggered the context menu. |
181 * @param callback Called once the the thumbnail is received. | 199 * @param callback Called once the the thumbnail is received. |
182 */ | 200 */ |
183 private void getThumbnail(Callback<Bitmap> callback) { | 201 private void getThumbnail(Callback<Bitmap> callback) { |
184 mOnThumbnailReceivedCallback = callback; | 202 mOnThumbnailReceivedCallback = callback; |
185 if (mNativeContextMenuHelper == 0) return; | 203 if (mNativeContextMenuHelper == 0) return; |
186 int maxSizePx = mContext.getResources().getDimensionPixelSize( | 204 int maxSizePx = mActivity.getResources().getDimensionPixelSize( |
187 R.dimen.context_menu_header_image_max_size); | 205 R.dimen.context_menu_header_image_max_size); |
188 nativeRetrieveHeaderThumbnail(mNativeContextMenuHelper, maxSizePx); | 206 nativeRetrieveHeaderThumbnail(mNativeContextMenuHelper, maxSizePx); |
189 } | 207 } |
190 | 208 |
191 @CalledByNative | 209 @CalledByNative |
192 private void onHeaderThumbnailReceived(WindowAndroid windowAndroid, byte[] j
pegImageData) { | 210 private void onHeaderThumbnailReceived(WindowAndroid windowAndroid, byte[] j
pegImageData) { |
193 // TODO(tedchoc): Decode in separate process before launch. | 211 // TODO(tedchoc): Decode in separate process before launch. |
194 Bitmap bitmap = BitmapFactory.decodeByteArray(jpegImageData, 0, jpegImag
eData.length); | 212 Bitmap bitmap = BitmapFactory.decodeByteArray(jpegImageData, 0, jpegImag
eData.length); |
195 if (mOnThumbnailReceivedCallback != null) { | 213 if (mOnThumbnailReceivedCallback != null) { |
196 mOnThumbnailReceivedCallback.onResult(bitmap); | 214 mOnThumbnailReceivedCallback.onResult(bitmap); |
197 } | 215 } |
198 } | 216 } |
199 | 217 |
200 @Override | 218 @Override |
201 public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo me
nuInfo) { | 219 public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo me
nuInfo) { |
202 List<Pair<Integer, List<ContextMenuItem>>> items = | 220 List<Pair<Integer, List<ContextMenuItem>>> items = |
203 mPopulator.buildContextMenu(menu, v.getContext(), mCurrentContex
tMenuParams); | 221 mPopulator.buildContextMenu(menu, v.getContext(), mCurrentContex
tMenuParams); |
204 ContextMenuUi menuUi = new PlatformContextMenuUi(menu); | 222 ContextMenuUi menuUi = new PlatformContextMenuUi(menu); |
205 menuUi.displayMenu( | 223 menuUi.displayMenu(mActivity, mCurrentContextMenuParams, items, mCallbac
k, mOnMenuShown, |
206 mContext, mCurrentContextMenuParams, items, mCallback, mOnMenuSh
own, mOnMenuClosed); | 224 mOnMenuClosed); |
207 } | 225 } |
208 | 226 |
209 /** | 227 /** |
210 * @return The {@link ContextMenuPopulator} responsible for populating the c
ontext menu. | 228 * @return The {@link ContextMenuPopulator} responsible for populating the c
ontext menu. |
211 */ | 229 */ |
212 @VisibleForTesting | 230 @VisibleForTesting |
213 public ContextMenuPopulator getPopulator() { | 231 public ContextMenuPopulator getPopulator() { |
214 return mPopulator; | 232 return mPopulator; |
215 } | 233 } |
216 | 234 |
217 private native void nativeOnStartDownload( | 235 private native void nativeOnStartDownload( |
218 long nativeContextMenuHelper, boolean isLink, boolean isDataReductio
nProxyEnabled); | 236 long nativeContextMenuHelper, boolean isLink, boolean isDataReductio
nProxyEnabled); |
219 private native void nativeSearchForImage(long nativeContextMenuHelper); | 237 private native void nativeSearchForImage(long nativeContextMenuHelper); |
220 private native void nativeShareImage(long nativeContextMenuHelper); | 238 private native void nativeShareImage(long nativeContextMenuHelper); |
221 private native void nativeOnContextMenuClosed(long nativeContextMenuHelper); | 239 private native void nativeOnContextMenuClosed(long nativeContextMenuHelper); |
222 private native void nativeRetrieveHeaderThumbnail(long nativeContextMenuHelp
er, int maxSizePx); | 240 private native void nativeRetrieveHeaderThumbnail(long nativeContextMenuHelp
er, int maxSizePx); |
223 } | 241 } |
OLD | NEW |