From 3d2b70670ae27d4329e9b96bc44b0ed19d76cba3 Mon Sep 17 00:00:00 2001 From: qua-non Date: Sat, 30 Mar 2013 22:31:52 +0530 Subject: [PATCH 1/4] SDLSurfaceView: add `OnKeyPreIme` listener closes github/kivy/kivy#1066 --- src/src/org/renpy/android/SDLSurfaceView.java | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/src/org/renpy/android/SDLSurfaceView.java b/src/src/org/renpy/android/SDLSurfaceView.java index 2d6ccfae4d..1a2c18425c 100644 --- a/src/src/org/renpy/android/SDLSurfaceView.java +++ b/src/src/org/renpy/android/SDLSurfaceView.java @@ -986,6 +986,16 @@ public boolean onKeyMultiple(int keyCode, int count, KeyEvent event){ return true; } + @Override + public boolean onKeyPreIme(int keyCode, final KeyEvent event){ + Log.i("python", String.format("key up %d", keyCode)); + if (mInputActivated && nativeKey(keyCode, 1, event.getUnicodeChar())) { + return false; + } else { + return super.onKeyDown(keyCode, event); + } + } + static void activateInput() { mInputActivated = true; } From 2da7d847e128894e7e8e5eb8c4f2f961587de9ed Mon Sep 17 00:00:00 2001 From: qua-non Date: Tue, 2 Apr 2013 19:32:54 +0530 Subject: [PATCH 2/4] initial attempt at swiping support --- src/src/org/renpy/android/SDLSurfaceView.java | 85 ++++++++++++++++--- 1 file changed, 71 insertions(+), 14 deletions(-) diff --git a/src/src/org/renpy/android/SDLSurfaceView.java b/src/src/org/renpy/android/SDLSurfaceView.java index 1a2c18425c..aafe156fc8 100644 --- a/src/src/org/renpy/android/SDLSurfaceView.java +++ b/src/src/org/renpy/android/SDLSurfaceView.java @@ -34,9 +34,13 @@ import android.util.Log; import android.view.SurfaceHolder; import android.view.SurfaceView; -import android.opengl.GLSurfaceView; import android.view.MotionEvent; import android.view.KeyEvent; +import android.view.inputmethod.EditorInfo; +import android.view.inputmethod.InputMethodManager; +import android.view.inputmethod.InputConnection; +import android.view.inputmethod.BaseInputConnection; +import android.opengl.GLSurfaceView; import android.net.Uri; import android.os.PowerManager; @@ -53,7 +57,7 @@ public class SDLSurfaceView extends SurfaceView implements SurfaceHolder.Callback, Runnable { - private static String TAG = "SDLSurface"; + private static String TAG = "SDLSurface"; private final String mVertexShader = "uniform mat4 uMVPMatrix;\n" + "attribute vec4 aPosition;\n" + @@ -272,6 +276,9 @@ private void printConfig(EGL10 egl, EGLDisplay display, // Is Python ready to receive input events? static boolean mInputActivated = false; + // Is Composing text being repeated? + static boolean mComposingText = false; + // The number of times we should clear the screen after swap. private int mClears = 2; @@ -463,6 +470,11 @@ public void onDestroy() { Log.d(TAG, "onDestroy() app already leaved."); return; } + + // close the IME overlay(keyboard) + InputMethodManager inputMethodManager = (InputMethodManager)getContext().getSystemService(Context.INPUT_METHOD_SERVICE); + inputMethodManager.hideSoftInputFromInputMethod(this.getWindowToken(), 0); + // application didn't leave, give 10s before closing. // hopefully, this could be enough for launching the on_stop() trigger within the app. @@ -865,6 +877,7 @@ public int swapBuffers() { private static final int INVALID_POINTER_ID = -1; private int mActivePointerId = INVALID_POINTER_ID; + private static String mCompText = ""; @Override public boolean onTouchEvent(final MotionEvent event) { @@ -959,7 +972,7 @@ public boolean onKeyUp(int keyCode, final KeyEvent event) { return super.onKeyUp(keyCode, event); } } - + @Override public boolean onKeyMultiple(int keyCode, int count, KeyEvent event){ String keys = event.getCharacters(); @@ -972,32 +985,76 @@ public boolean onKeyMultiple(int keyCode, int count, KeyEvent event){ // but it my cause some odd behaviour keyCode = 45; } - - if (mInputActivated){ + if (mInputActivated && event.getAction() == KeyEvent.ACTION_MULTIPLE){ keys.getChars(0, keys.length(), keysBuffer, 0); + if (mComposingText == true){ + mComposingText = false; + this.mCompText = keys; + }else if (this.mCompText.equals(keys)){ + // skip on composing text + this.mCompText = ""; + return true; + } + for(char c: keysBuffer){ //Log.i("python", "Char from multiply " + (int) c); // Calls both up/down events to emulate key pressing nativeKey(keyCode, 1, (int) c); nativeKey(keyCode, 0, (int) c); } + return true; + }else { + return super.onKeyMultiple(keyCode, count, event); } - - return true; } @Override public boolean onKeyPreIme(int keyCode, final KeyEvent event){ - Log.i("python", String.format("key up %d", keyCode)); - if (mInputActivated && nativeKey(keyCode, 1, event.getUnicodeChar())) { - return false; - } else { - return super.onKeyDown(keyCode, event); - } + //Log.i("python", String.format("key pre ime %d", keyCode)); + if (mInputActivated){ + 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 false; + } + return super.onKeyPreIme(keyCode, event); } - + + @Override + public InputConnection onCreateInputConnection(EditorInfo outAttrs) { + outAttrs.inputType = EditorInfo.TYPE_NULL; + return new BaseInputConnection(this, false) { + + @Override + public boolean setComposingText(CharSequence text, + int newCursorPosition) { + commitText(text, 0); + mComposingText = true; + sendKeyEvent( + new KeyEvent( + KeyEvent.ACTION_DOWN, + KeyEvent.KEYCODE_SPACE)); + sendKeyEvent( + new KeyEvent( + KeyEvent.ACTION_UP, + KeyEvent.KEYCODE_SPACE)); + //Log.i("Python:", String.format("set Composing Text %s", mComposingText)); + return true; + } + }; + } + static void activateInput() { mInputActivated = true; + mComposingText = false; } static void openUrl(String url) { From bda532de65b9e30ec567dc8915522ecd07f1e5b2 Mon Sep 17 00:00:00 2001 From: qua-non Date: Tue, 2 Apr 2013 23:14:13 +0530 Subject: [PATCH 3/4] swype input: simplify IME management, allow text predictionto work and ensure IME doesn't take up entire screen in landscape mode Tested with:: - Stock JellyBean keyboard with swiping and text prediction - SwiftKey with flow and text prediction - Swype Keyboard with swiping and text prediction --- src/src/org/renpy/android/SDLSurfaceView.java | 39 +++---------------- 1 file changed, 5 insertions(+), 34 deletions(-) diff --git a/src/src/org/renpy/android/SDLSurfaceView.java b/src/src/org/renpy/android/SDLSurfaceView.java index aafe156fc8..bd41a49211 100644 --- a/src/src/org/renpy/android/SDLSurfaceView.java +++ b/src/src/org/renpy/android/SDLSurfaceView.java @@ -276,9 +276,6 @@ private void printConfig(EGL10 egl, EGLDisplay display, // Is Python ready to receive input events? static boolean mInputActivated = false; - // Is Composing text being repeated? - static boolean mComposingText = false; - // The number of times we should clear the screen after swap. private int mClears = 2; @@ -475,7 +472,6 @@ public void onDestroy() { InputMethodManager inputMethodManager = (InputMethodManager)getContext().getSystemService(Context.INPUT_METHOD_SERVICE); inputMethodManager.hideSoftInputFromInputMethod(this.getWindowToken(), 0); - // application didn't leave, give 10s before closing. // hopefully, this could be enough for launching the on_stop() trigger within the app. mPause = PAUSE_STOP_REQUEST; @@ -877,7 +873,6 @@ public int swapBuffers() { private static final int INVALID_POINTER_ID = -1; private int mActivePointerId = INVALID_POINTER_ID; - private static String mCompText = ""; @Override public boolean onTouchEvent(final MotionEvent event) { @@ -987,14 +982,6 @@ public boolean onKeyMultiple(int keyCode, int count, KeyEvent event){ } if (mInputActivated && event.getAction() == KeyEvent.ACTION_MULTIPLE){ keys.getChars(0, keys.length(), keysBuffer, 0); - if (mComposingText == true){ - mComposingText = false; - this.mCompText = keys; - }else if (this.mCompText.equals(keys)){ - // skip on composing text - this.mCompText = ""; - return true; - } for(char c: keysBuffer){ //Log.i("python", "Char from multiply " + (int) c); @@ -1030,31 +1017,15 @@ public boolean onKeyPreIme(int keyCode, final KeyEvent event){ @Override public InputConnection onCreateInputConnection(EditorInfo outAttrs) { - outAttrs.inputType = EditorInfo.TYPE_NULL; - return new BaseInputConnection(this, false) { - - @Override - public boolean setComposingText(CharSequence text, - int newCursorPosition) { - commitText(text, 0); - mComposingText = true; - sendKeyEvent( - new KeyEvent( - KeyEvent.ACTION_DOWN, - KeyEvent.KEYCODE_SPACE)); - sendKeyEvent( - new KeyEvent( - KeyEvent.ACTION_UP, - KeyEvent.KEYCODE_SPACE)); - //Log.i("Python:", String.format("set Composing Text %s", mComposingText)); - return true; - } - }; + // setting inputtype to TYPE_CLASS_TEXT is necessary for swiftkey to enable + outAttrs.inputType = EditorInfo.TYPE_CLASS_TEXT; + // ask IME to avoid taking full screen on landscape mode + outAttrs.imeOptions = EditorInfo.IME_FLAG_NO_EXTRACT_UI; + return new BaseInputConnection(this, false); } static void activateInput() { mInputActivated = true; - mComposingText = false; } static void openUrl(String url) { From 4ec2d541976e72a3e3b3c5a2d6acdd0a1d893791 Mon Sep 17 00:00:00 2001 From: qua-non Date: Sun, 7 Apr 2013 00:01:24 +0530 Subject: [PATCH 4/4] SDLSurfaceView: add suport for forwarding keystrokes while text prediction is working not just at the end. --- src/src/org/renpy/android/SDLSurfaceView.java | 39 ++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/src/src/org/renpy/android/SDLSurfaceView.java b/src/src/org/renpy/android/SDLSurfaceView.java index bd41a49211..28d18e4fe1 100644 --- a/src/src/org/renpy/android/SDLSurfaceView.java +++ b/src/src/org/renpy/android/SDLSurfaceView.java @@ -1021,7 +1021,44 @@ public InputConnection onCreateInputConnection(EditorInfo outAttrs) { outAttrs.inputType = EditorInfo.TYPE_CLASS_TEXT; // ask IME to avoid taking full screen on landscape mode outAttrs.imeOptions = EditorInfo.IME_FLAG_NO_EXTRACT_UI; - return new BaseInputConnection(this, false); + return new BaseInputConnection(this, false){ + + private int mDelLen = 0; + + private void deleteLastText(){ + // send back space keys + for (int i = 0; i < this.mDelLen; i++){ + nativeKey(KeyEvent.KEYCODE_DEL, 1, 23); + nativeKey(KeyEvent.KEYCODE_DEL, 0, 23); + } + } + + @Override + public boolean setComposingText(CharSequence text, + int newCursorPosition){ + //Log.i("Python:", String.format("set Composing Text %s", text)); + this.deleteLastText(); + // send text + for(int i = 0; i < text.length(); i++){ + // Calls both up/down events to emulate key pressing + char c = text.charAt(i); + nativeKey(45, 1, (int) c); + nativeKey(45, 0, (int) c); + } + // store len to be deleted for next time + this.mDelLen = text.length(); + return super.setComposingText(text, newCursorPosition); + } + + @Override + public boolean commitText(CharSequence text, int newCursorPosition) { + // some code which takes the input and manipulates it and calls editText.getText().replace() afterwards + //Log.i("Python:", String.format("Commit Text %s", text)); + this.deleteLastText(); + this.mDelLen = 0; + return super.commitText(text, newCursorPosition); + } + }; } static void activateInput() {