| 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.util.Pair; | 9 import android.util.Pair; |
| 9 import android.view.ContextMenu; | 10 import android.view.ContextMenu; |
| 10 import android.view.ContextMenu.ContextMenuInfo; | 11 import android.view.ContextMenu.ContextMenuInfo; |
| 11 import android.view.View; | 12 import android.view.View; |
| 12 import android.view.View.OnCreateContextMenuListener; | 13 import android.view.View.OnCreateContextMenuListener; |
| 13 | 14 |
| 14 import org.chromium.base.Callback; | 15 import org.chromium.base.Callback; |
| 15 import org.chromium.base.VisibleForTesting; | 16 import org.chromium.base.VisibleForTesting; |
| 16 import org.chromium.base.annotations.CalledByNative; | 17 import org.chromium.base.annotations.CalledByNative; |
| 17 import org.chromium.base.metrics.RecordHistogram; | 18 import org.chromium.base.metrics.RecordHistogram; |
| 19 import org.chromium.chrome.browser.ChromeFeatureList; |
| 18 import org.chromium.chrome.browser.share.ShareHelper; | 20 import org.chromium.chrome.browser.share.ShareHelper; |
| 19 import org.chromium.content.browser.ContentViewCore; | 21 import org.chromium.content.browser.ContentViewCore; |
| 20 import org.chromium.content_public.browser.WebContents; | 22 import org.chromium.content_public.browser.WebContents; |
| 21 import org.chromium.ui.base.WindowAndroid; | 23 import org.chromium.ui.base.WindowAndroid; |
| 22 import org.chromium.ui.base.WindowAndroid.OnCloseContextMenuListener; | 24 import org.chromium.ui.base.WindowAndroid.OnCloseContextMenuListener; |
| 23 | 25 |
| 24 import java.util.List; | 26 import java.util.List; |
| 25 | 27 |
| 26 /** | 28 /** |
| 27 * A helper class that handles generating context menus for {@link ContentViewCo
re}s. | 29 * A helper class that handles generating context menus for {@link ContentViewCo
re}s. |
| 28 */ | 30 */ |
| 29 public class ContextMenuHelper implements OnCreateContextMenuListener { | 31 public class ContextMenuHelper implements OnCreateContextMenuListener { |
| 30 private long mNativeContextMenuHelper; | 32 private long mNativeContextMenuHelper; |
| 31 | 33 |
| 32 private ContextMenuPopulator mPopulator; | 34 private ContextMenuPopulator mPopulator; |
| 33 private ContextMenuParams mCurrentContextMenuParams; | 35 private ContextMenuParams mCurrentContextMenuParams; |
| 34 private Activity mActivity; | 36 private Context mContext; |
| 37 private Callback<Integer> mCallback; |
| 38 private Runnable mOnMenuShown; |
| 39 private Runnable mOnMenuClosed; |
| 35 | 40 |
| 36 private ContextMenuHelper(long nativeContextMenuHelper) { | 41 private ContextMenuHelper(long nativeContextMenuHelper) { |
| 37 mNativeContextMenuHelper = nativeContextMenuHelper; | 42 mNativeContextMenuHelper = nativeContextMenuHelper; |
| 38 } | 43 } |
| 39 | 44 |
| 40 @CalledByNative | 45 @CalledByNative |
| 41 private static ContextMenuHelper create(long nativeContextMenuHelper) { | 46 private static ContextMenuHelper create(long nativeContextMenuHelper) { |
| 42 return new ContextMenuHelper(nativeContextMenuHelper); | 47 return new ContextMenuHelper(nativeContextMenuHelper); |
| 43 } | 48 } |
| 44 | 49 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 57 if (mPopulator != null) mPopulator.onDestroy(); | 62 if (mPopulator != null) mPopulator.onDestroy(); |
| 58 mPopulator = populator; | 63 mPopulator = populator; |
| 59 } | 64 } |
| 60 | 65 |
| 61 /** | 66 /** |
| 62 * Starts showing a context menu for {@code view} based on {@code params}. | 67 * Starts showing a context menu for {@code view} based on {@code params}. |
| 63 * @param contentViewCore The {@link ContentViewCore} to show the menu to. | 68 * @param contentViewCore The {@link ContentViewCore} to show the menu to. |
| 64 * @param params The {@link ContextMenuParams} that indicate what m
enu items to show. | 69 * @param params The {@link ContextMenuParams} that indicate what m
enu items to show. |
| 65 */ | 70 */ |
| 66 @CalledByNative | 71 @CalledByNative |
| 67 private void showContextMenu(ContentViewCore contentViewCore, ContextMenuPar
ams params) { | 72 private void showContextMenu(final ContentViewCore contentViewCore, ContextM
enuParams params) { |
| 68 if (params.isFile()) return; | 73 if (params.isFile()) return; |
| 69 View view = contentViewCore.getContainerView(); | 74 View view = contentViewCore.getContainerView(); |
| 70 final WindowAndroid windowAndroid = contentViewCore.getWindowAndroid(); | 75 final WindowAndroid windowAndroid = contentViewCore.getWindowAndroid(); |
| 71 | 76 |
| 72 if (view == null || view.getVisibility() != View.VISIBLE || view.getPare
nt() == null | 77 if (view == null || view.getVisibility() != View.VISIBLE || view.getPare
nt() == null |
| 73 || windowAndroid == null || windowAndroid.getActivity().get() ==
null) { | 78 || windowAndroid == null || windowAndroid.getContext().get() ==
null |
| 79 || mPopulator == null) { |
| 74 return; | 80 return; |
| 75 } | 81 } |
| 76 | 82 |
| 77 mCurrentContextMenuParams = params; | 83 mCurrentContextMenuParams = params; |
| 78 mActivity = windowAndroid.getActivity().get(); | 84 mContext = windowAndroid.getContext().get(); |
| 85 mCallback = new Callback<Integer>() { |
| 86 @Override |
| 87 public void onResult(Integer result) { |
| 88 mPopulator.onItemSelected( |
| 89 ContextMenuHelper.this, mCurrentContextMenuParams, resul
t); |
| 90 } |
| 91 }; |
| 92 mOnMenuShown = new Runnable() { |
| 93 @Override |
| 94 public void run() { |
| 95 WebContents webContents = contentViewCore.getWebContents(); |
| 96 RecordHistogram.recordBooleanHistogram("ContextMenu.Shown", webC
ontents != null); |
| 97 } |
| 98 }; |
| 99 mOnMenuClosed = new Runnable() { |
| 100 @Override |
| 101 public void run() { |
| 102 if (mNativeContextMenuHelper == 0) return; |
| 103 nativeOnContextMenuClosed(mNativeContextMenuHelper); |
| 104 } |
| 105 }; |
| 79 | 106 |
| 107 if (ChromeFeatureList.isEnabled(ChromeFeatureList.CUSTOM_CONTEXT_MENU))
{ |
| 108 List<Pair<Integer, List<ContextMenuItem>>> items = |
| 109 mPopulator.buildContextMenu(null, mContext, mCurrentContextM
enuParams); |
| 110 |
| 111 ContextMenuUi menuUi = new TabularContextMenuUi(); |
| 112 menuUi.displayMenu(mContext, mCurrentContextMenuParams, items, mCall
back, mOnMenuShown, |
| 113 mOnMenuClosed); |
| 114 return; |
| 115 } |
| 116 |
| 117 // The Platform Context Menu requires the listener within this hepler si
nce this helper and |
| 118 // provides context menu for us to show. |
| 80 view.setOnCreateContextMenuListener(this); | 119 view.setOnCreateContextMenuListener(this); |
| 81 if (view.showContextMenu()) { | 120 if (view.showContextMenu()) { |
| 82 WebContents webContents = contentViewCore.getWebContents(); | 121 mOnMenuShown.run(); |
| 83 RecordHistogram.recordBooleanHistogram( | |
| 84 "ContextMenu.Shown", webContents != null); | |
| 85 | |
| 86 windowAndroid.addContextMenuCloseListener(new OnCloseContextMenuList
ener() { | 122 windowAndroid.addContextMenuCloseListener(new OnCloseContextMenuList
ener() { |
| 87 @Override | 123 @Override |
| 88 public void onContextMenuClosed() { | 124 public void onContextMenuClosed() { |
| 89 if (mNativeContextMenuHelper == 0) return; | 125 mOnMenuClosed.run(); |
| 90 | |
| 91 nativeOnContextMenuClosed(mNativeContextMenuHelper); | |
| 92 windowAndroid.removeContextMenuCloseListener(this); | 126 windowAndroid.removeContextMenuCloseListener(this); |
| 93 } | 127 } |
| 94 }); | 128 }); |
| 95 } | 129 } |
| 96 } | 130 } |
| 97 | 131 |
| 98 /** | 132 /** |
| 99 * Starts a download based on the current {@link ContextMenuParams}. | 133 * Starts a download based on the current {@link ContextMenuParams}. |
| 100 * @param isLink Whether or not the download target is a link. | 134 * @param isLink Whether or not the download target is a link. |
| 101 */ | 135 */ |
| (...skipping 23 matching lines...) Expand all Loading... |
| 125 private void onShareImageReceived( | 159 private void onShareImageReceived( |
| 126 WindowAndroid windowAndroid, byte[] jpegImageData) { | 160 WindowAndroid windowAndroid, byte[] jpegImageData) { |
| 127 Activity activity = windowAndroid.getActivity().get(); | 161 Activity activity = windowAndroid.getActivity().get(); |
| 128 if (activity == null) return; | 162 if (activity == null) return; |
| 129 | 163 |
| 130 ShareHelper.shareImage(activity, jpegImageData); | 164 ShareHelper.shareImage(activity, jpegImageData); |
| 131 } | 165 } |
| 132 | 166 |
| 133 @Override | 167 @Override |
| 134 public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo me
nuInfo) { | 168 public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo me
nuInfo) { |
| 135 assert mPopulator != null; | |
| 136 | |
| 137 List<Pair<Integer, List<ContextMenuItem>>> items = | 169 List<Pair<Integer, List<ContextMenuItem>>> items = |
| 138 mPopulator.buildContextMenu(menu, v.getContext(), mCurrentContex
tMenuParams); | 170 mPopulator.buildContextMenu(menu, v.getContext(), mCurrentContex
tMenuParams); |
| 139 ContextMenuUi menuUi = new PlatformContextMenuUi(menu); | 171 ContextMenuUi menuUi = new PlatformContextMenuUi(menu); |
| 140 menuUi.displayMenu(mActivity, mCurrentContextMenuParams, items, new Call
back<Integer>() { | 172 menuUi.displayMenu( |
| 141 @Override | 173 mContext, mCurrentContextMenuParams, items, mCallback, mOnMenuSh
own, mOnMenuClosed); |
| 142 public void onResult(Integer result) { | |
| 143 mPopulator.onItemSelected( | |
| 144 ContextMenuHelper.this, mCurrentContextMenuParams, resul
t); | |
| 145 } | |
| 146 }); | |
| 147 } | 174 } |
| 148 | 175 |
| 149 /** | 176 /** |
| 150 * @return The {@link ContextMenuPopulator} responsible for populating the c
ontext menu. | 177 * @return The {@link ContextMenuPopulator} responsible for populating the c
ontext menu. |
| 151 */ | 178 */ |
| 152 @VisibleForTesting | 179 @VisibleForTesting |
| 153 public ContextMenuPopulator getPopulator() { | 180 public ContextMenuPopulator getPopulator() { |
| 154 return mPopulator; | 181 return mPopulator; |
| 155 } | 182 } |
| 156 | 183 |
| 157 private native void nativeOnStartDownload( | 184 private native void nativeOnStartDownload( |
| 158 long nativeContextMenuHelper, boolean isLink, boolean isDataReductio
nProxyEnabled); | 185 long nativeContextMenuHelper, boolean isLink, boolean isDataReductio
nProxyEnabled); |
| 159 private native void nativeSearchForImage(long nativeContextMenuHelper); | 186 private native void nativeSearchForImage(long nativeContextMenuHelper); |
| 160 private native void nativeShareImage(long nativeContextMenuHelper); | 187 private native void nativeShareImage(long nativeContextMenuHelper); |
| 161 private native void nativeOnContextMenuClosed(long nativeContextMenuHelper); | 188 private native void nativeOnContextMenuClosed(long nativeContextMenuHelper); |
| 162 } | 189 } |
| OLD | NEW |