From c20740cbb352e4755c0f8ca0ff2bdae26cf19187 Mon Sep 17 00:00:00 2001 From: Michael Wu Date: Wed, 15 Dec 2010 12:37:54 -0800 Subject: [PATCH] Bug 611764 - Move key handling to GeckoSurfaceView and handling onKeyPreIme, r=blassey a=blocking-fennec --HG-- extra : rebase_source : 34229313342b93c7e78d51818b308a307dde8056 --- embedding/android/GeckoApp.java | 85 ------------------- embedding/android/GeckoSurfaceView.java | 103 ++++++++++++++++++++++++ widget/src/android/nsWindow.cpp | 2 - 3 files changed, 103 insertions(+), 87 deletions(-) diff --git a/embedding/android/GeckoApp.java b/embedding/android/GeckoApp.java index 460f8d3694e5..7154bc1fd4a8 100644 --- a/embedding/android/GeckoApp.java +++ b/embedding/android/GeckoApp.java @@ -324,91 +324,6 @@ abstract public class GeckoApp super.onLowMemory(); } - public boolean onKeyDown(int keyCode, KeyEvent event) { - switch (keyCode) { - case KeyEvent.KEYCODE_BACK: - if (event.getRepeatCount() == 0) { - event.startTracking(); - return true; - } else { - return false; - } - case KeyEvent.KEYCODE_MENU: - if (event.getRepeatCount() == 0) { - event.startTracking(); - break; - } else if ((event.getFlags() & KeyEvent.FLAG_LONG_PRESS) != 0) { - break; - } - // Ignore repeats for KEYCODE_MENU; they confuse the widget code. - return false; - case KeyEvent.KEYCODE_VOLUME_UP: - case KeyEvent.KEYCODE_VOLUME_DOWN: - case KeyEvent.KEYCODE_SEARCH: - return false; - case KeyEvent.KEYCODE_DEL: - // See comments in GeckoInputConnection.onKeyDel - if (surfaceView != null && - surfaceView.inputConnection != null && - surfaceView.inputConnection.onKeyDel()) { - return true; - } - break; - case KeyEvent.KEYCODE_ENTER: - if ((event.getFlags() & KeyEvent.FLAG_EDITOR_ACTION) != 0 && - surfaceView.mIMEActionHint.equalsIgnoreCase("next")) - event = new KeyEvent(event.getAction(), KeyEvent.KEYCODE_TAB); - break; - default: - break; - } - // KeyListener returns true if it handled the event for us. - if (GeckoApp.surfaceView.mIMEState == GeckoSurfaceView.IME_STATE_DISABLED || - keyCode == KeyEvent.KEYCODE_ENTER || - !GeckoApp.surfaceView.mKeyListener.onKeyDown(GeckoApp.surfaceView, GeckoApp.surfaceView.mEditable, keyCode, event)) - GeckoAppShell.sendEventToGecko(new GeckoEvent(event)); - return true; - } - - public boolean onKeyUp(int keyCode, KeyEvent event) { - switch (keyCode) { - case KeyEvent.KEYCODE_BACK: - if (!event.isTracking() || event.isCanceled()) - return false; - break; - default: - break; - } - if (GeckoApp.surfaceView.mIMEState == GeckoSurfaceView.IME_STATE_DISABLED || - keyCode == KeyEvent.KEYCODE_ENTER || - !GeckoApp.surfaceView.mKeyListener.onKeyUp(GeckoApp.surfaceView, GeckoApp.surfaceView.mEditable, keyCode, event)) - GeckoAppShell.sendEventToGecko(new GeckoEvent(event)); - return true; - } - - public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) { - GeckoAppShell.sendEventToGecko(new GeckoEvent(event)); - return true; - } - - public boolean onKeyLongPress(int keyCode, KeyEvent event) { - switch (keyCode) { - case KeyEvent.KEYCODE_BACK: - GeckoAppShell.sendEventToGecko(new GeckoEvent(event)); - return true; - case KeyEvent.KEYCODE_MENU: - InputMethodManager imm = (InputMethodManager) - surfaceView.getContext().getSystemService( - Context.INPUT_METHOD_SERVICE); - imm.toggleSoftInputFromWindow(surfaceView.getWindowToken(), - imm.SHOW_FORCED, 0); - return true; - default: - break; - } - return false; - } - abstract public String getAppName(); abstract public String getContentProcessName(); diff --git a/embedding/android/GeckoSurfaceView.java b/embedding/android/GeckoSurfaceView.java index cccdcc25264d..3bfb8ce3cd88 100644 --- a/embedding/android/GeckoSurfaceView.java +++ b/embedding/android/GeckoSurfaceView.java @@ -388,6 +388,109 @@ class GeckoSurfaceView return true; } + @Override + public boolean onKeyPreIme(int keyCode, KeyEvent event) { + if (mIMEState != IME_STATE_DISABLED || event.isSystem()) + return super.onKeyPreIme(keyCode, event); + + switch (event.getAction()) { + case KeyEvent.ACTION_DOWN: + return onKeyDown(keyCode, event); + case KeyEvent.ACTION_UP: + return onKeyUp(keyCode, event); + case KeyEvent.ACTION_MULTIPLE: + return onKeyMultiple(keyCode, event.getRepeatCount(), event); + } + return super.onKeyPreIme(keyCode, event); + } + + @Override + public boolean onKeyDown(int keyCode, KeyEvent event) { + switch (keyCode) { + case KeyEvent.KEYCODE_BACK: + if (event.getRepeatCount() == 0) { + event.startTracking(); + return true; + } else { + return false; + } + case KeyEvent.KEYCODE_MENU: + if (event.getRepeatCount() == 0) { + event.startTracking(); + break; + } else if ((event.getFlags() & KeyEvent.FLAG_LONG_PRESS) != 0) { + break; + } + // Ignore repeats for KEYCODE_MENU; they confuse the widget code. + return false; + case KeyEvent.KEYCODE_VOLUME_UP: + case KeyEvent.KEYCODE_VOLUME_DOWN: + case KeyEvent.KEYCODE_SEARCH: + return false; + case KeyEvent.KEYCODE_DEL: + // See comments in GeckoInputConnection.onKeyDel + if (inputConnection != null && + inputConnection.onKeyDel()) { + return true; + } + break; + case KeyEvent.KEYCODE_ENTER: + if ((event.getFlags() & KeyEvent.FLAG_EDITOR_ACTION) != 0 && + mIMEActionHint.equalsIgnoreCase("next")) + event = new KeyEvent(event.getAction(), KeyEvent.KEYCODE_TAB); + break; + default: + break; + } + // KeyListener returns true if it handled the event for us. + if (mIMEState == IME_STATE_DISABLED || + keyCode == KeyEvent.KEYCODE_ENTER || + !mKeyListener.onKeyDown(this, mEditable, keyCode, event)) + GeckoAppShell.sendEventToGecko(new GeckoEvent(event)); + return true; + } + + @Override + public boolean onKeyUp(int keyCode, KeyEvent event) { + switch (keyCode) { + case KeyEvent.KEYCODE_BACK: + if (!event.isTracking() || event.isCanceled()) + return false; + break; + default: + break; + } + if (mIMEState == IME_STATE_DISABLED || + keyCode == KeyEvent.KEYCODE_ENTER || + !mKeyListener.onKeyUp(this, mEditable, keyCode, event)) + GeckoAppShell.sendEventToGecko(new GeckoEvent(event)); + return true; + } + + @Override + public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) { + GeckoAppShell.sendEventToGecko(new GeckoEvent(event)); + return true; + } + + @Override + public boolean onKeyLongPress(int keyCode, KeyEvent event) { + switch (keyCode) { + case KeyEvent.KEYCODE_BACK: + GeckoAppShell.sendEventToGecko(new GeckoEvent(event)); + return true; + case KeyEvent.KEYCODE_MENU: + InputMethodManager imm = (InputMethodManager) + getContext().getSystemService(Context.INPUT_METHOD_SERVICE); + imm.toggleSoftInputFromWindow(getWindowToken(), + imm.SHOW_FORCED, 0); + return true; + default: + break; + } + return false; + } + // Is this surface valid for drawing into? boolean mSurfaceValid; diff --git a/widget/src/android/nsWindow.cpp b/widget/src/android/nsWindow.cpp index c8976b0f499f..e169e855c935 100644 --- a/widget/src/android/nsWindow.cpp +++ b/widget/src/android/nsWindow.cpp @@ -1517,8 +1517,6 @@ nsWindow::OnKeyEvent(AndroidGeckoEvent *ae) nsKeyEvent event(PR_TRUE, msg, this); InitKeyEvent(event, *ae); - if (event.charCode) - event.keyCode = 0; DispatchEvent(&event); if (isDown) {