Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(884)

Side by Side Diff: chrome/android/java/src/org/chromium/chrome/browser/contextmenu/ContextMenuHelper.java

Issue 2804913003: Remove redundant code from Android context menu native. (Closed)
Patch Set: Address dtrainor@ comments Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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.ComponentName; 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;
(...skipping 10 matching lines...) Expand all
21 import org.chromium.chrome.R; 21 import org.chromium.chrome.R;
22 import org.chromium.chrome.browser.ChromeFeatureList; 22 import org.chromium.chrome.browser.ChromeFeatureList;
23 import org.chromium.chrome.browser.share.ShareHelper; 23 import org.chromium.chrome.browser.share.ShareHelper;
24 import org.chromium.content.browser.ContentViewCore; 24 import org.chromium.content.browser.ContentViewCore;
25 import org.chromium.content_public.browser.WebContents; 25 import org.chromium.content_public.browser.WebContents;
26 import org.chromium.ui.base.WindowAndroid; 26 import org.chromium.ui.base.WindowAndroid;
27 import org.chromium.ui.base.WindowAndroid.OnCloseContextMenuListener; 27 import org.chromium.ui.base.WindowAndroid.OnCloseContextMenuListener;
28 28
29 import java.util.List; 29 import java.util.List;
30 30
31 import javax.annotation.Nullable;
32
31 /** 33 /**
32 * A helper class that handles generating context menus for {@link ContentViewCo re}s. 34 * A helper class that handles generating context menus for {@link ContentViewCo re}s.
33 */ 35 */
34 public class ContextMenuHelper implements OnCreateContextMenuListener { 36 public class ContextMenuHelper implements OnCreateContextMenuListener {
37 private static final int MAX_SHARE_DIMEN_PX = 2048;
38
35 private long mNativeContextMenuHelper; 39 private long mNativeContextMenuHelper;
36 40
37 private ContextMenuPopulator mPopulator; 41 private ContextMenuPopulator mPopulator;
38 private ContextMenuParams mCurrentContextMenuParams; 42 private ContextMenuParams mCurrentContextMenuParams;
39 private Activity mActivity; 43 private Activity mActivity;
40 private Callback<Integer> mCallback; 44 private Callback<Integer> mCallback;
41 private Runnable mOnMenuShown; 45 private Runnable mOnMenuShown;
42 private Runnable mOnMenuClosed; 46 private Runnable mOnMenuClosed;
43 private Callback<Bitmap> mOnThumbnailReceivedCallback;
44 private ComponentName mComponentName;
45 47
46 private ContextMenuHelper(long nativeContextMenuHelper) { 48 private ContextMenuHelper(long nativeContextMenuHelper) {
47 mNativeContextMenuHelper = nativeContextMenuHelper; 49 mNativeContextMenuHelper = nativeContextMenuHelper;
48 } 50 }
49 51
50 @CalledByNative 52 @CalledByNative
51 private static ContextMenuHelper create(long nativeContextMenuHelper) { 53 private static ContextMenuHelper create(long nativeContextMenuHelper) {
52 return new ContextMenuHelper(nativeContextMenuHelper); 54 return new ContextMenuHelper(nativeContextMenuHelper);
53 } 55 }
54 56
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after
162 */ 164 */
163 public void searchForImage() { 165 public void searchForImage() {
164 if (mNativeContextMenuHelper == 0) return; 166 if (mNativeContextMenuHelper == 0) return;
165 nativeSearchForImage(mNativeContextMenuHelper); 167 nativeSearchForImage(mNativeContextMenuHelper);
166 } 168 }
167 169
168 /** 170 /**
169 * Share the image that triggered the current context menu. 171 * Share the image that triggered the current context menu.
170 */ 172 */
171 public void shareImage() { 173 public void shareImage() {
172 if (mNativeContextMenuHelper == 0) return; 174 shareImageDirectly(null);
173 nativeShareImage(mNativeContextMenuHelper);
174 }
175
176 @CalledByNative
177 private void onShareImageReceived(
178 WindowAndroid windowAndroid, byte[] jpegImageData) {
179 Activity activity = windowAndroid.getActivity().get();
180 if (activity == null) return;
181
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;
186 } 175 }
187 176
188 /** 177 /**
189 * Share image triggered with the current context menu directly with a speci fic app. 178 * 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. 179 * @param name The {@link ComponentName} of the app to share the image direc tly with.
191 */ 180 */
192 public void shareImageDirectly(ComponentName name) { 181 public void shareImageDirectly(@Nullable final ComponentName name) {
193 mComponentName = name; 182 if (mNativeContextMenuHelper == 0) return;
194 shareImage(); 183 Callback<byte[]> callback = new Callback<byte[]>() {
184 @Override
185 public void onResult(byte[] result) {
186 WebContents webContents = nativeGetJavaWebContents(mNativeContex tMenuHelper);
187 WindowAndroid windowAndroid = webContents.getTopLevelNativeWindo w();
188
189 Activity activity = windowAndroid.getActivity().get();
190 if (activity == null) return;
191
192 ShareHelper.shareImage(activity, result, name);
193 }
194 };
195 nativeRetrieveImage(mNativeContextMenuHelper, callback, MAX_SHARE_DIMEN_ PX);
195 } 196 }
196 197
197 /** 198 /**
198 * 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.
199 * @param callback Called once the the thumbnail is received. 200 * @param callback Called once the the thumbnail is received.
200 */ 201 */
201 private void getThumbnail(Callback<Bitmap> callback) { 202 private void getThumbnail(final Callback<Bitmap> callback) {
202 mOnThumbnailReceivedCallback = callback;
203 if (mNativeContextMenuHelper == 0) return; 203 if (mNativeContextMenuHelper == 0) return;
204 int maxSizePx = mActivity.getResources().getDimensionPixelSize( 204 int maxSizePx = mActivity.getResources().getDimensionPixelSize(
205 R.dimen.context_menu_header_image_max_size); 205 R.dimen.context_menu_header_image_max_size);
206 nativeRetrieveHeaderThumbnail(mNativeContextMenuHelper, maxSizePx); 206 Callback<byte[]> rawDataCallback = new Callback<byte[]>() {
207 } 207 @Override
208 208 public void onResult(byte[] result) {
209 @CalledByNative 209 // TODO(tedchoc): Decode in separate process before launch.
210 private void onHeaderThumbnailReceived(WindowAndroid windowAndroid, byte[] j pegImageData) { 210 Bitmap bitmap = BitmapFactory.decodeByteArray(result, 0, result. length);
211 // TODO(tedchoc): Decode in separate process before launch. 211 callback.onResult(bitmap);
212 Bitmap bitmap = BitmapFactory.decodeByteArray(jpegImageData, 0, jpegImag eData.length); 212 }
213 if (mOnThumbnailReceivedCallback != null) { 213 };
214 mOnThumbnailReceivedCallback.onResult(bitmap); 214 nativeRetrieveImage(mNativeContextMenuHelper, rawDataCallback, maxSizePx );
215 }
216 } 215 }
217 216
218 @Override 217 @Override
219 public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo me nuInfo) { 218 public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo me nuInfo) {
220 List<Pair<Integer, List<ContextMenuItem>>> items = 219 List<Pair<Integer, List<ContextMenuItem>>> items =
221 mPopulator.buildContextMenu(menu, v.getContext(), mCurrentContex tMenuParams); 220 mPopulator.buildContextMenu(menu, v.getContext(), mCurrentContex tMenuParams);
222 ContextMenuUi menuUi = new PlatformContextMenuUi(menu); 221 ContextMenuUi menuUi = new PlatformContextMenuUi(menu);
223 menuUi.displayMenu(mActivity, mCurrentContextMenuParams, items, mCallbac k, mOnMenuShown, 222 menuUi.displayMenu(mActivity, mCurrentContextMenuParams, items, mCallbac k, mOnMenuShown,
224 mOnMenuClosed); 223 mOnMenuClosed);
225 } 224 }
226 225
227 /** 226 /**
228 * @return The {@link ContextMenuPopulator} responsible for populating the c ontext menu. 227 * @return The {@link ContextMenuPopulator} responsible for populating the c ontext menu.
229 */ 228 */
230 @VisibleForTesting 229 @VisibleForTesting
231 public ContextMenuPopulator getPopulator() { 230 public ContextMenuPopulator getPopulator() {
232 return mPopulator; 231 return mPopulator;
233 } 232 }
234 233
234 private native WebContents nativeGetJavaWebContents(long nativeContextMenuHe lper);
235 private native void nativeOnStartDownload( 235 private native void nativeOnStartDownload(
236 long nativeContextMenuHelper, boolean isLink, boolean isDataReductio nProxyEnabled); 236 long nativeContextMenuHelper, boolean isLink, boolean isDataReductio nProxyEnabled);
237 private native void nativeSearchForImage(long nativeContextMenuHelper); 237 private native void nativeSearchForImage(long nativeContextMenuHelper);
238 private native void nativeShareImage(long nativeContextMenuHelper); 238 private native void nativeRetrieveImage(
239 long nativeContextMenuHelper, Callback<byte[]> callback, int maxSize Px);
239 private native void nativeOnContextMenuClosed(long nativeContextMenuHelper); 240 private native void nativeOnContextMenuClosed(long nativeContextMenuHelper);
240 private native void nativeRetrieveHeaderThumbnail(long nativeContextMenuHelp er, int maxSizePx);
241 } 241 }
OLDNEW
« no previous file with comments | « base/android/java/src/org/chromium/base/Callback.java ('k') | chrome/browser/ui/android/context_menu_helper.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698