Merge pull request #735 from UnknownShadow200/AndroidInputTypeTake2

Proper keyboard input for android version
This commit is contained in:
UnknownShadow200 2020-10-20 19:53:45 +11:00 committed by GitHub
commit 1093db2191
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 136 additions and 15 deletions

View File

@ -20,6 +20,10 @@ import android.content.res.Configuration;
import android.graphics.PixelFormat; import android.graphics.PixelFormat;
import android.net.Uri; import android.net.Uri;
import android.os.Bundle; import android.os.Bundle;
import android.text.Editable;
import android.text.InputType;
import android.text.Selection;
import android.text.SpannableStringBuilder;
import android.util.DisplayMetrics; import android.util.DisplayMetrics;
import android.util.Log; import android.util.Log;
import android.view.Display; import android.view.Display;
@ -33,6 +37,10 @@ import android.view.WindowManager;
import android.view.View; import android.view.View;
import android.view.ViewTreeObserver.OnGlobalLayoutListener; import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.view.Window; import android.view.Window;
import android.view.inputmethod.BaseInputConnection;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputConnection;
import android.view.inputmethod.InputMethod;
import android.view.inputmethod.InputMethodManager; import android.view.inputmethod.InputMethodManager;
// implements InputQueue.Callback // implements InputQueue.Callback
@ -42,7 +50,7 @@ public class MainActivity extends Activity implements SurfaceHolder.Callback2 {
// ====================================== // ======================================
// -------------- COMMANDS -------------- // -------------- COMMANDS --------------
// ====================================== // ======================================
class NativeCmdArgs { public int cmd, arg1, arg2, arg3; public Surface sur; } class NativeCmdArgs { public int cmd, arg1, arg2, arg3; public String str; public Surface sur; }
Queue<NativeCmdArgs> nativeCmds = new ConcurrentLinkedQueue<NativeCmdArgs>(); Queue<NativeCmdArgs> nativeCmds = new ConcurrentLinkedQueue<NativeCmdArgs>();
Queue<NativeCmdArgs> freeCmds = new ConcurrentLinkedQueue<NativeCmdArgs>(); Queue<NativeCmdArgs> freeCmds = new ConcurrentLinkedQueue<NativeCmdArgs>();
@ -73,6 +81,13 @@ public class MainActivity extends Activity implements SurfaceHolder.Callback2 {
nativeCmds.add(args); nativeCmds.add(args);
} }
void pushCmd(int cmd, String text) {
NativeCmdArgs args = getCmdArgs();
args.cmd = cmd;
args.str = text;
nativeCmds.add(args);
}
void pushCmd(int cmd, Surface surface) { void pushCmd(int cmd, Surface surface) {
NativeCmdArgs args = getCmdArgs(); NativeCmdArgs args = getCmdArgs();
args.cmd = cmd; args.cmd = cmd;
@ -83,6 +98,7 @@ public class MainActivity extends Activity implements SurfaceHolder.Callback2 {
final static int CMD_KEY_DOWN = 0; final static int CMD_KEY_DOWN = 0;
final static int CMD_KEY_UP = 1; final static int CMD_KEY_UP = 1;
final static int CMD_KEY_CHAR = 2; final static int CMD_KEY_CHAR = 2;
final static int CMD_KEY_TEXT = 19;
final static int CMD_MOUSE_DOWN = 3; final static int CMD_MOUSE_DOWN = 3;
final static int CMD_MOUSE_UP = 4; final static int CMD_MOUSE_UP = 4;
@ -108,9 +124,11 @@ public class MainActivity extends Activity implements SurfaceHolder.Callback2 {
// --------------- EVENTS --------------- // --------------- EVENTS ---------------
// ====================================== // ======================================
static boolean gameHooked; static boolean gameHooked;
InputMethodManager input;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
input = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
Log.i("CC_WIN", "CREATE EVENT"); Log.i("CC_WIN", "CREATE EVENT");
Window window = getWindow(); Window window = getWindow();
Log.i("CC_WIN", "GAME RUNNING?" + gameHooked); Log.i("CC_WIN", "GAME RUNNING?" + gameHooked);
@ -248,6 +266,7 @@ public class MainActivity extends Activity implements SurfaceHolder.Callback2 {
case CMD_KEY_DOWN: processKeyDown(c.arg1); break; case CMD_KEY_DOWN: processKeyDown(c.arg1); break;
case CMD_KEY_UP: processKeyUp(c.arg1); break; case CMD_KEY_UP: processKeyUp(c.arg1); break;
case CMD_KEY_CHAR: processKeyChar(c.arg1); break; case CMD_KEY_CHAR: processKeyChar(c.arg1); break;
case CMD_KEY_TEXT: processKeyText(c.str); break;
case CMD_MOUSE_DOWN: processMouseDown(c.arg1, c.arg2, c.arg3); break; case CMD_MOUSE_DOWN: processMouseDown(c.arg1, c.arg2, c.arg3); break;
case CMD_MOUSE_UP: processMouseUp(c.arg1, c.arg2, c.arg3); break; case CMD_MOUSE_UP: processMouseUp(c.arg1, c.arg2, c.arg3); break;
@ -270,6 +289,7 @@ public class MainActivity extends Activity implements SurfaceHolder.Callback2 {
case CMD_LOW_MEMORY: processOnLowMemory(); break; case CMD_LOW_MEMORY: processOnLowMemory(); break;
} }
c.str = null;
c.sur = null; // don't keep a reference to it c.sur = null; // don't keep a reference to it
freeCmds.add(c); freeCmds.add(c);
} }
@ -278,6 +298,7 @@ public class MainActivity extends Activity implements SurfaceHolder.Callback2 {
native void processKeyDown(int code); native void processKeyDown(int code);
native void processKeyUp(int code); native void processKeyUp(int code);
native void processKeyChar(int code); native void processKeyChar(int code);
native void processKeyText(String str);
native void processMouseDown(int id, int x, int y); native void processMouseDown(int id, int x, int y);
native void processMouseUp(int id, int x, int y); native void processMouseUp(int id, int x, int y);
@ -353,13 +374,64 @@ public class MainActivity extends Activity implements SurfaceHolder.Callback2 {
} }
class LauncherView extends SurfaceView { class LauncherView extends SurfaceView {
public LauncherView(Context context) {
public LauncherView(Context context) { super(context); } super(context);
setFocusable(true);
setFocusableInTouchMode(true);
}
@Override @Override
public boolean dispatchTouchEvent(MotionEvent ev) { public boolean dispatchTouchEvent(MotionEvent ev) {
return MainActivity.this.handleTouchEvent(ev) || super.dispatchTouchEvent(ev); return MainActivity.this.handleTouchEvent(ev) || super.dispatchTouchEvent(ev);
} }
@Override
public InputConnection onCreateInputConnection(EditorInfo attrs) {
attrs.actionLabel = null;
attrs.inputType = MainActivity.this.getKeyboardType();
attrs.imeOptions = EditorInfo.IME_ACTION_GO;
InputConnection ic = new BaseInputConnection(this, true) {
SpannableStringBuilder kbText = new SpannableStringBuilder(MainActivity.this.keyboardText);
boolean inited;
void updateText() { MainActivity.this.pushCmd(CMD_KEY_TEXT, kbText.toString()); }
@Override
public Editable getEditable() {
if (!inited) {
// needed to set selection, otherwise random crashes later with backspacing
// set selection to end, so backspacing after opening keyboard with text still works
Selection.setSelection(kbText, kbText.toString().length());
inited = true;
}
return kbText;
}
@Override
public boolean setComposingText(CharSequence text, int newCursorPosition) {
boolean success = super.setComposingText(text, newCursorPosition);
updateText();
return success;
}
@Override
public boolean deleteSurroundingText(int beforeLength, int afterLength) {
boolean success = super.deleteSurroundingText(beforeLength, afterLength);
updateText();
return success;
}
@Override
public boolean commitText(CharSequence text, int newCursorPosition) {
boolean success = super.commitText(text, newCursorPosition);
updateText();
return success;
}
};
//String text = MainActivity.this.keyboardText;
//if (text != null) ic.setComposingText(text, 0);
return ic;
}
} }
// ====================================== // ======================================
@ -404,16 +476,42 @@ public class MainActivity extends Activity implements SurfaceHolder.Callback2 {
// ====================================== // ======================================
public void setWindowTitle(String str) { setTitle(str); } public void setWindowTitle(String str) { setTitle(str); }
public void openKeyboard() { volatile int keyboardType;
InputMethodManager input = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); volatile String keyboardText = "";
View view = getWindow().getDecorView(); public void openKeyboard(String text, int type) {
input.showSoftInput(view, 0); keyboardType = type;
keyboardText = text;
//runOnUiThread(new Runnable() {
//public void run() {
// Restart view so it uses the right INPUT_TYPE
if (curView != null) input.restartInput(curView);
if (curView != null) input.showSoftInput(curView, 0);
//}
//});
} }
public void closeKeyboard() { public void closeKeyboard() {
InputMethodManager input = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); InputMethodManager input = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
View view = getWindow().getDecorView(); View view = getWindow().getDecorView();
input.hideSoftInputFromWindow(view.getWindowToken(), 0); input.hideSoftInputFromWindow(view.getWindowToken(), 0);
keyboardText = "";
//runOnUiThread(new Runnable() {
//public void run() {
if (curView != null) input.hideSoftInputFromWindow(curView.getWindowToken(), 0);
//}
//});
}
public void setKeyboardText(String text) {
keyboardText = text;
// Restart view because text changed externally
if (curView != null) input.restartInput(curView);
}
public int getKeyboardType() {
if (keyboardType == 2) return InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD;
if (keyboardType == 1) return InputType.TYPE_CLASS_NUMBER;
return InputType.TYPE_CLASS_TEXT; /* TODO: This still adds a . after space */
} }
public String getClipboardText() { public String getClipboardText() {

View File

@ -3684,6 +3684,14 @@ static void JNICALL java_processKeyChar(JNIEnv* env, jobject o, jint code) {
} }
} }
static void JNICALL java_processKeyText(JNIEnv* env, jobject o, jstring str) {
cc_string text = JavaGetString(env, str);
Platform_Log1("KEY - TEXT %s", &text);
Event_RaiseString(&InputEvents.TextChanged, &text);
(*env)->ReleaseStringUTFChars(env, str, text.buffer);
}
static void JNICALL java_processMouseDown(JNIEnv* env, jobject o, jint id, jint x, jint y) { static void JNICALL java_processMouseDown(JNIEnv* env, jobject o, jint id, jint x, jint y) {
Platform_Log3("MOUSE %i - DOWN %i,%i", &id, &x, &y); Platform_Log3("MOUSE %i - DOWN %i,%i", &id, &x, &y);
Input_AddTouch(id, x, y); Input_AddTouch(id, x, y);
@ -3773,10 +3781,11 @@ static void JNICALL java_onLowMemory(JNIEnv* env, jobject o) {
/* TODO: Low memory */ /* TODO: Low memory */
} }
static const JNINativeMethod methods[18] = { static const JNINativeMethod methods[19] = {
{ "processKeyDown", "(I)V", java_processKeyDown }, { "processKeyDown", "(I)V", java_processKeyDown },
{ "processKeyUp", "(I)V", java_processKeyUp }, { "processKeyUp", "(I)V", java_processKeyUp },
{ "processKeyChar", "(I)V", java_processKeyChar }, { "processKeyChar", "(I)V", java_processKeyChar },
{ "processKeyText", "(Ljava/lang/String;)V", java_processKeyText },
{ "processMouseDown", "(III)V", java_processMouseDown }, { "processMouseDown", "(III)V", java_processMouseDown },
{ "processMouseUp", "(III)V", java_processMouseUp }, { "processMouseUp", "(III)V", java_processMouseUp },
@ -3958,10 +3967,24 @@ void Window_FreeFramebuffer(struct Bitmap* bmp) {
void Window_OpenKeyboard(const cc_string* text, int type) { void Window_OpenKeyboard(const cc_string* text, int type) {
JNIEnv* env; JNIEnv* env;
jvalue args[2];
JavaGetCurrentEnv(env); JavaGetCurrentEnv(env);
JavaCallVoid(env, "openKeyboard", "()V", NULL);
args[0].l = JavaMakeString(env, text);
args[1].i = type;
JavaCallVoid(env, "openKeyboard", "(Ljava/lang/String;I)V", args);
(*env)->DeleteLocalRef(env, args[0].l);
}
void Window_SetKeyboardText(const cc_string* text) {
JNIEnv* env;
jvalue args[1];
JavaGetCurrentEnv(env);
args[0].l = JavaMakeString(env, text);
JavaCallVoid(env, "setKeyboardText", "(Ljava/lang/String;)V", args);
(*env)->DeleteLocalRef(env, args[0].l);
} }
void Window_SetKeyboardText(const cc_string* text) { }
void Window_CloseKeyboard(void) { void Window_CloseKeyboard(void) {
JNIEnv* env; JNIEnv* env;