5

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.

David Mulder
  • 26,123
  • 9
  • 51
  • 114

4 Answers4

1

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);
  }

}
Community
  • 1
  • 1
Warabei
  • 143
  • 1
  • 8
0

I try Warabei's solution but it not work on 15.44.384.13. I improve to find ContentViewCore cross versions:

public class XWalkWebView extends XWalkView {
    ...
    private Field getFields(Class clazz){
        for(Field field:clazz.getDeclaredFields()){
            if(ContentViewCore.class == field.getType()){
                return field;
            }
        }
        clazz = clazz.getSuperclass();
        if(clazz!=null && clazz!=Object.class){
            Field field = getFields(clazz);
            if(field!=null)return field;
        }
        return null;
    }
    private void inject(View view){
        Field field = getFields(view.getClass());
        if(field!=null){
            field.setAccessible(true);
            try {
                ContentViewCore viewCore = (ContentViewCore) field.get(view);
                viewCore.setContainerView(this);
                return;
            }catch(Exception e){

            }
        }
        if(view instanceof ViewGroup){
            ViewGroup viewGroup = (ViewGroup)view;
            int count = viewGroup.getChildCount();
            for(int i = 0;i<count;i++){
                inject(viewGroup.getChildAt(i));
            }
        }
    }
    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        inject(this);
    }
    ...

To disable contextual selection menu:

@Override
public ActionMode startActionMode(ActionMode.Callback callback) {
    return new ActionMode() {
        @Override
        public void setTitle(CharSequence charSequence) {

        }

        @Override
        public void setTitle(int i) {

        }

        @Override
        public void setSubtitle(CharSequence charSequence) {

        }

        @Override
        public void setSubtitle(int i) {

        }

        @Override
        public void setCustomView(View view) {

        }

        @Override
        public void invalidate() {

        }

        @Override
        public void finish() {

        }

        @Override
        public Menu getMenu() {
            return null;
        }

        @Override
        public CharSequence getTitle() {
            return null;
        }

        @Override
        public CharSequence getSubtitle() {
            return null;
        }

        @Override
        public View getCustomView() {
            return null;
        }

        @Override
        public MenuInflater getMenuInflater() {
            return null;
        }
    };
 }
0

It is an old post but I haven't been able to find another solution.

A simple workaround to disable context options in crosswalk view..

  1. Go to your crosswalk project into res/menu/select_action_menu.xml
  2. Delete or comment on the item you don't want to show
  3. Save, build and run
-2

This CSS should prevent context menus in both Android and IOS, as given in the cordova template

* {
-webkit-tap-highlight-color: rgba(0,0,0,0); /* make transparent link selection, adjust last value opacity 0 to 1.0 */
}

body {
-webkit-touch-callout: none;    /* prevent callout to copy image, etc when tap to hold */
-webkit-text-size-adjust: none; /* prevent webkit from resizing text to fit */
-webkit-user-select: none;      /* prevent copy paste, to allow, change 'none' to 'text' */
}
cforcloud
  • 589
  • 2
  • 8