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.Context; |
9 import android.graphics.Bitmap; | |
10 import android.graphics.BitmapFactory; | |
9 import android.util.Pair; | 11 import android.util.Pair; |
10 import android.view.ContextMenu; | 12 import android.view.ContextMenu; |
11 import android.view.ContextMenu.ContextMenuInfo; | 13 import android.view.ContextMenu.ContextMenuInfo; |
12 import android.view.View; | 14 import android.view.View; |
13 import android.view.View.OnCreateContextMenuListener; | 15 import android.view.View.OnCreateContextMenuListener; |
14 | 16 |
15 import org.chromium.base.Callback; | 17 import org.chromium.base.Callback; |
16 import org.chromium.base.VisibleForTesting; | 18 import org.chromium.base.VisibleForTesting; |
17 import org.chromium.base.annotations.CalledByNative; | 19 import org.chromium.base.annotations.CalledByNative; |
18 import org.chromium.base.metrics.RecordHistogram; | 20 import org.chromium.base.metrics.RecordHistogram; |
(...skipping 11 matching lines...) Expand all Loading... | |
30 */ | 32 */ |
31 public class ContextMenuHelper implements OnCreateContextMenuListener { | 33 public class ContextMenuHelper implements OnCreateContextMenuListener { |
32 private long mNativeContextMenuHelper; | 34 private long mNativeContextMenuHelper; |
33 | 35 |
34 private ContextMenuPopulator mPopulator; | 36 private ContextMenuPopulator mPopulator; |
35 private ContextMenuParams mCurrentContextMenuParams; | 37 private ContextMenuParams mCurrentContextMenuParams; |
36 private Context mContext; | 38 private Context mContext; |
37 private Callback<Integer> mCallback; | 39 private Callback<Integer> mCallback; |
38 private Runnable mOnMenuShown; | 40 private Runnable mOnMenuShown; |
39 private Runnable mOnMenuClosed; | 41 private Runnable mOnMenuClosed; |
42 private OnThumbnailReceivedListener mOnThumbnailReceivedListener; | |
43 | |
44 /** | |
45 * This waits for a thumbnail to be retrieved by the native code. | |
46 */ | |
47 interface OnThumbnailReceivedListener { | |
48 /** | |
49 * When the thumbnail is received it will send the thumbnail via this me thod. This is | |
50 * activated after calling {@link #getThumbnail()}. | |
51 * @param bitmap The bitmap that is retrieved from native code. | |
52 */ | |
53 void onThumbnailReceived(Bitmap bitmap); | |
54 } | |
55 | |
56 @VisibleForTesting | |
57 ContextMenuHelper() {} | |
40 | 58 |
41 private ContextMenuHelper(long nativeContextMenuHelper) { | 59 private ContextMenuHelper(long nativeContextMenuHelper) { |
42 mNativeContextMenuHelper = nativeContextMenuHelper; | 60 mNativeContextMenuHelper = nativeContextMenuHelper; |
43 } | 61 } |
44 | 62 |
45 @CalledByNative | 63 @CalledByNative |
46 private static ContextMenuHelper create(long nativeContextMenuHelper) { | 64 private static ContextMenuHelper create(long nativeContextMenuHelper) { |
47 return new ContextMenuHelper(nativeContextMenuHelper); | 65 return new ContextMenuHelper(nativeContextMenuHelper); |
48 } | 66 } |
49 | 67 |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
101 public void run() { | 119 public void run() { |
102 if (mNativeContextMenuHelper == 0) return; | 120 if (mNativeContextMenuHelper == 0) return; |
103 nativeOnContextMenuClosed(mNativeContextMenuHelper); | 121 nativeOnContextMenuClosed(mNativeContextMenuHelper); |
104 } | 122 } |
105 }; | 123 }; |
106 | 124 |
107 if (ChromeFeatureList.isEnabled(ChromeFeatureList.CUSTOM_CONTEXT_MENU)) { | 125 if (ChromeFeatureList.isEnabled(ChromeFeatureList.CUSTOM_CONTEXT_MENU)) { |
108 List<Pair<Integer, List<ContextMenuItem>>> items = | 126 List<Pair<Integer, List<ContextMenuItem>>> items = |
109 mPopulator.buildContextMenu(null, mContext, mCurrentContextM enuParams); | 127 mPopulator.buildContextMenu(null, mContext, mCurrentContextM enuParams); |
110 | 128 |
111 ContextMenuUi menuUi = new TabularContextMenuUi(); | 129 ContextMenuUi menuUi = new TabularContextMenuUi(this); |
112 menuUi.displayMenu(mContext, mCurrentContextMenuParams, items, mCall back, mOnMenuShown, | 130 menuUi.displayMenu(mContext, mCurrentContextMenuParams, items, mCall back, mOnMenuShown, |
113 mOnMenuClosed); | 131 mOnMenuClosed); |
114 return; | 132 return; |
115 } | 133 } |
116 | 134 |
117 // The Platform Context Menu requires the listener within this hepler si nce this helper and | 135 // The Platform Context Menu requires the listener within this hepler si nce this helper and |
118 // provides context menu for us to show. | 136 // provides context menu for us to show. |
119 view.setOnCreateContextMenuListener(this); | 137 view.setOnCreateContextMenuListener(this); |
120 if (view.showContextMenu()) { | 138 if (view.showContextMenu()) { |
121 mOnMenuShown.run(); | 139 mOnMenuShown.run(); |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
157 | 175 |
158 @CalledByNative | 176 @CalledByNative |
159 private void onShareImageReceived( | 177 private void onShareImageReceived( |
160 WindowAndroid windowAndroid, byte[] jpegImageData) { | 178 WindowAndroid windowAndroid, byte[] jpegImageData) { |
161 Activity activity = windowAndroid.getActivity().get(); | 179 Activity activity = windowAndroid.getActivity().get(); |
162 if (activity == null) return; | 180 if (activity == null) return; |
163 | 181 |
164 ShareHelper.shareImage(activity, jpegImageData); | 182 ShareHelper.shareImage(activity, jpegImageData); |
165 } | 183 } |
166 | 184 |
185 /** | |
186 * Gets the thumbnail of the current image that triggered the context menu. | |
187 */ | |
188 public void getThumbnail() { | |
David Trainor- moved to gerrit
2017/03/30 05:03:14
I actually think this (and maybe shareImage() with
JJ
2017/03/30 16:00:25
You make an interesting point on that native can c
JJ
2017/03/30 17:09:00
Checked it out! It's not impossible, but there are
David Trainor- moved to gerrit
2017/03/30 23:45:14
You could probably just do it all with finals. Fo
JJ
2017/03/31 02:28:45
It's not the Java side that's the problem. That pa
| |
189 if (mNativeContextMenuHelper == 0) return; | |
190 nativeRetrieveHeaderThumbnail(mNativeContextMenuHelper); | |
191 } | |
192 | |
193 @CalledByNative | |
194 private void onHeaderThumbnailReceived(WindowAndroid windowAndroid, byte[] j pegImageData) { | |
195 Bitmap bitmap = BitmapFactory.decodeByteArray(jpegImageData, 0, jpegImag eData.length); | |
196 if (mOnThumbnailReceivedListener != null) { | |
197 mOnThumbnailReceivedListener.onThumbnailReceived(bitmap); | |
198 } | |
199 } | |
200 | |
167 @Override | 201 @Override |
168 public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo me nuInfo) { | 202 public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo me nuInfo) { |
169 List<Pair<Integer, List<ContextMenuItem>>> items = | 203 List<Pair<Integer, List<ContextMenuItem>>> items = |
170 mPopulator.buildContextMenu(menu, v.getContext(), mCurrentContex tMenuParams); | 204 mPopulator.buildContextMenu(menu, v.getContext(), mCurrentContex tMenuParams); |
171 ContextMenuUi menuUi = new PlatformContextMenuUi(menu); | 205 ContextMenuUi menuUi = new PlatformContextMenuUi(menu); |
172 menuUi.displayMenu( | 206 menuUi.displayMenu( |
173 mContext, mCurrentContextMenuParams, items, mCallback, mOnMenuSh own, mOnMenuClosed); | 207 mContext, mCurrentContextMenuParams, items, mCallback, mOnMenuSh own, mOnMenuClosed); |
174 } | 208 } |
175 | 209 |
176 /** | 210 /** |
211 * Sets the listener for retrieving a thumbnail when calling {@link #getThum bnail()}. | |
212 * @param listener The listener that will be called once a thumbnail is retr ieved. | |
213 */ | |
214 public void setOnThumbnailReceivedListener(OnThumbnailReceivedListener liste ner) { | |
215 mOnThumbnailReceivedListener = listener; | |
216 } | |
217 | |
218 /** | |
177 * @return The {@link ContextMenuPopulator} responsible for populating the c ontext menu. | 219 * @return The {@link ContextMenuPopulator} responsible for populating the c ontext menu. |
178 */ | 220 */ |
179 @VisibleForTesting | 221 @VisibleForTesting |
180 public ContextMenuPopulator getPopulator() { | 222 public ContextMenuPopulator getPopulator() { |
181 return mPopulator; | 223 return mPopulator; |
182 } | 224 } |
183 | 225 |
184 private native void nativeOnStartDownload( | 226 private native void nativeOnStartDownload( |
185 long nativeContextMenuHelper, boolean isLink, boolean isDataReductio nProxyEnabled); | 227 long nativeContextMenuHelper, boolean isLink, boolean isDataReductio nProxyEnabled); |
186 private native void nativeSearchForImage(long nativeContextMenuHelper); | 228 private native void nativeSearchForImage(long nativeContextMenuHelper); |
187 private native void nativeShareImage(long nativeContextMenuHelper); | 229 private native void nativeShareImage(long nativeContextMenuHelper); |
188 private native void nativeOnContextMenuClosed(long nativeContextMenuHelper); | 230 private native void nativeOnContextMenuClosed(long nativeContextMenuHelper); |
231 private native void nativeRetrieveHeaderThumbnail(long nativeContextMenuHelp er); | |
189 } | 232 } |
OLD | NEW |