This source file includes following definitions.
- create
- destroy
- setPopulator
- showContextMenu
- startContextMenuDownload
- onCreateContextMenu
- onMenuItemClick
- getPopulator
- shouldShowMenu
- nativeOnStartDownload
- nativeOnCustomItemSelected
package org.chromium.chrome.browser.contextmenu;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.HapticFeedbackConstants;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MenuItem.OnMenuItemClickListener;
import android.view.View;
import android.view.View.OnCreateContextMenuListener;
import com.google.common.annotations.VisibleForTesting;
import org.chromium.base.CalledByNative;
import org.chromium.content.browser.ContentView;
import org.chromium.content.browser.ContentViewCore;
public class ContextMenuHelper implements OnCreateContextMenuListener, OnMenuItemClickListener {
private long mNativeContextMenuHelper;
private ContextMenuPopulator mPopulator;
private ContextMenuParams mCurrentContextMenuParams;
private ContextMenuHelper(long nativeContextMenuHelper) {
mNativeContextMenuHelper = nativeContextMenuHelper;
}
@CalledByNative
private static ContextMenuHelper create(long nativeContextMenuHelper) {
return new ContextMenuHelper(nativeContextMenuHelper);
}
@CalledByNative
private void destroy() {
mNativeContextMenuHelper = 0;
}
@CalledByNative
private void setPopulator(ContextMenuPopulator populator) {
mPopulator = populator;
}
@CalledByNative
private void showContextMenu(ContentViewCore contentViewCore, ContextMenuParams params) {
final View view = contentViewCore.getContainerView();
if (!shouldShowMenu(params)
|| view == null
|| view.getVisibility() != View.VISIBLE
|| view.getParent() == null) {
return;
}
mCurrentContextMenuParams = params;
view.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
contentViewCore.setIgnoreRemainingTouchEvents();
view.setOnCreateContextMenuListener(this);
view.showContextMenu();
}
public void startContextMenuDownload(boolean isLink) {
if (mNativeContextMenuHelper != 0) nativeOnStartDownload(mNativeContextMenuHelper, isLink);
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
if (!shouldShowMenu(mCurrentContextMenuParams)) return;
if (mCurrentContextMenuParams.isCustomMenu()) {
for (int i = 0; i < mCurrentContextMenuParams.getCustomMenuSize(); i++) {
menu.add(Menu.NONE, i, Menu.NONE, mCurrentContextMenuParams.getCustomLabelAt(i));
}
} else {
assert mPopulator != null;
mPopulator.buildContextMenu(menu, v.getContext(), mCurrentContextMenuParams);
}
for (int i = 0; i < menu.size(); i++) {
menu.getItem(i).setOnMenuItemClickListener(this);
}
}
@Override
public boolean onMenuItemClick(MenuItem item) {
if (mCurrentContextMenuParams.isCustomMenu()) {
if (mNativeContextMenuHelper != 0) {
final int action = mCurrentContextMenuParams.getCustomActionAt(item.getItemId());
nativeOnCustomItemSelected(mNativeContextMenuHelper, action);
}
return true;
} else {
return mPopulator.onItemSelected(this, mCurrentContextMenuParams, item.getItemId());
}
}
@VisibleForTesting
public ContextMenuPopulator getPopulator() {
return mPopulator;
}
private boolean shouldShowMenu(ContextMenuParams params) {
return params.isCustomMenu() ||
(mPopulator != null && mPopulator.shouldShowContextMenu(params));
}
private native void nativeOnStartDownload(long nativeContextMenuHelper, boolean isLink);
private native void nativeOnCustomItemSelected(long nativeContextMenuHelper, int action);