Issue with setOnKeyListener using crosswalk

401 Views Asked by At

I'm trying to start using crosswalk in a cordova app, the problem is that the app uses a custom plugin for a barcode reader in a specific device.

We basically have a plugin class that calls custom View which extends EditText, in the view we create a OnKeyListener to check if the specific key is press.

During the initialization of the plugin class we use the webView.getView().setOnKeyListener(view.getScanKeyListener()); to create the OnKeyListener to check the keypress.

public class ScannerInputPlugin extends CordovaPlugin implements ScannerInputView.ScannerInputListener {
    private static CallbackContext callback = null;

    @Override
    public void pluginInitialize() {
        cordova.getActivity().runOnUiThread(new java.lang.Runnable() {
            public void run() {
                ScannerInputView view = new ScannerInputView(cordova.getActivity(), ScannerInputPlugin.this);
                ((ViewGroup) webView.getView()).addView(view);
                webView.getView().setOnKeyListener(view.getScanKeyListener());
            }
        });
    }

    @Override
    public boolean execute(String action, JSONArray data, CallbackContext callbackContext) throws JSONException {
        if (action.equals("register")) {
            callback = callbackContext;
            return true;
        } else {
            return false;
        }
    }

    @Override
    public void onScannerInput(CharSequence input) {
        if (callback != null) {
            PluginResult result = new PluginResult(PluginResult.Status.OK, input != null ? input.toString() : null);
            result.setKeepCallback(true);
            callback.sendPluginResult(result);
        }
    }
}

However in the view the OnKeyListener is never called, this only happens when using crosswalk, the only major change I found when debugging the app is that the webview.engine changes to a XWalkWebViewEngine.

public class ScannerInputView extends EditText {
    public static interface ScannerInputListener {
        void onScannerInput(CharSequence input);
    }

    public static final int KEYCODE_SCAN = 220;

    public ScannerInputView(Context context, final ScannerInputListener listener) {
        super(context);
        setLayoutParams(new ViewGroup.LayoutParams(0, 0));
        setInputType(InputType.TYPE_NULL);
        addTextChangedListener(new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (listener != null) {
                    if (count > 0 && s.charAt(start) != 0)
                        s = s.subSequence(start, start + count);
                    else
                        s = null;
                    listener.onScannerInput(s);
                }
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void afterTextChanged(Editable s) {
            }
        });
    }

    public View.OnKeyListener getScanKeyListener() {
        return new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (keyCode == KEYCODE_SCAN && event.getAction() == KeyEvent.ACTION_DOWN) {
                    requestFocus();
                    return true;
                }
                return false;
            }
        };
    }
}

I think this has something to do with the crosswalk engine but I have yet to find the cause, does anyone have any tips about the matter?

1

There are 1 best solutions below

0
On

As an update, I did manage to fix the issue by flowing the stackoverflow solution, I did not know about the XWalkView not being focusable by default, for future reference the solution for the issue was to setFocusable(true), setFocusableInTouchMode(true) and requestFocus() before the setOnKeyListener.

public void pluginInitialize() {
    cordova.getActivity().runOnUiThread(new java.lang.Runnable() {
        public void run() {
            ScannerInputView view = new ScannerInputView(cordova.getActivity(), ScannerInputPlugin.this);
            ((ViewGroup) webView.getView()).addView(view);
            webView.getView().setFocusable(true);
            webView.getView().setFocusableInTouchMode(true);
            webView.getView().requestFocus();
            webView.getView().setOnKeyListener(view.getScanKeyListener());
        }
    });
}