Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable contextual selection menu in crosswalk (cordova)

I wish to disable the native contextual menu that is shown when you select some text, the one with the select all, copy, share and search buttons. I do not however want to disable selections themselves. Ideally I would wish to extend the menu actually, but honestly, I am more than perfectly fine with just disabling it. With textfields and the like it tends to be relatively simple from the documentation I found, but I just can't figure out a way to make this work with XWalkView/CordovaWebView. Might be that I am just searching in entirely the wrong corner though.

like image 360
David Mulder Avatar asked Nov 27 '22 07:11

David Mulder


1 Answers

I have a workaround.

For WebView there is a solution, but it doesn't work for XWalkView:

WebView selection menu workaround

My gradle includes compile 'org.xwalk:xwalk_core_library:14.43.343.17'

My solution, the main idea in the onAttachedToWindow method:

public class XWalkWebView extends XWalkView {

  public XWalkWebView(Context context, AttributeSet attrs) {
    super(context, attrs);
  }

  private ActionMode.Callback mOriginalCallback;

  @Override
  protected void onAttachedToWindow() {
    super.onAttachedToWindow();
    try {
        View innerChild = ((ViewGroup) getChildAt(0)).getChildAt(0);
        Field contentViewField = innerChild.getClass().getDeclaredField("mContentView");
        contentViewField.setAccessible(true);
        XWalkContentView xWalkContentView = (XWalkContentView) contentViewField.get(innerChild);
        Field contentViewCoreField = xWalkContentView.getClass().getSuperclass().getDeclaredField("mContentViewCore");
        contentViewCoreField.setAccessible(true);
        ContentViewCore viewCore = (ContentViewCore) contentViewCoreField.get(xWalkContentView);
        viewCore.setContainerView(this);
    } catch (NoSuchFieldException | IllegalAccessException e) {
        e.printStackTrace();
    }
  }

  @Override
  public ActionMode startActionMode(ActionMode.Callback callback) {
    mOriginalCallback = callback;
    ActionMode.Callback c = new // your callback...
    return super.startActionMode(c);
  }

}
like image 170
Warabei Avatar answered Dec 09 '22 20:12

Warabei