Skip to content

Add keyboard input type customization for SDL2 #2057

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import android.os.Bundle;
import android.os.PowerManager;
import android.util.Log;
import android.view.inputmethod.InputMethodManager;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
Expand Down Expand Up @@ -421,7 +422,7 @@ public static void stop_service() {
/** Whether main routine/actual app has started yet **/
protected boolean mAppConfirmedActive = false;
/** Timer for delayed loading screen removal. **/
protected Timer loadingScreenRemovalTimer = null;
protected Timer loadingScreenRemovalTimer = null;

// Overridden since it's called often, to check whether to remove the
// loading screen:
Expand All @@ -431,7 +432,7 @@ protected boolean sendCommand(int command, Object data) {
considerLoadingScreenRemoval();
return result;
}

/** Confirm that the app's main routine has been launched.
**/
@Override
Expand Down Expand Up @@ -484,7 +485,7 @@ public void run() {
public void removeLoadingScreen() {
runOnUiThread(new Runnable() {
public void run() {
if (PythonActivity.mImageView != null &&
if (PythonActivity.mImageView != null &&
PythonActivity.mImageView.getParent() != null) {
((ViewGroup)PythonActivity.mImageView.getParent()).removeView(
PythonActivity.mImageView);
Expand Down Expand Up @@ -548,7 +549,7 @@ protected void showLoadingScreen() {
try {
mImageView.setBackgroundColor(Color.parseColor(backgroundColor));
} catch (IllegalArgumentException e) {}
}
}
mImageView.setLayoutParams(new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT,
ViewGroup.LayoutParams.FILL_PARENT));
Expand All @@ -568,7 +569,7 @@ protected void showLoadingScreen() {
// You must call removeView() on the child's parent first.")
}
}

@Override
protected void onPause() {
if (this.mWakeLock != null && mWakeLock.isHeld()) {
Expand Down Expand Up @@ -649,7 +650,7 @@ public boolean checkCurrentPermission(String permission) {
Activity.class.getMethod("checkSelfPermission", java.lang.String.class);
Object resultObj = methodCheckPermission.invoke(this, permission);
int result = Integer.parseInt(resultObj.toString());
if (result == PackageManager.PERMISSION_GRANTED)
if (result == PackageManager.PERMISSION_GRANTED)
return true;
} catch (IllegalAccessException | NoSuchMethodException |
InvocationTargetException e) {
Expand All @@ -676,4 +677,12 @@ public void requestPermissionsWithRequestCode(String[] permissions, int requestC
public void requestPermissions(String[] permissions) {
requestPermissionsWithRequestCode(permissions, 1);
}

public static void changeKeyboard(int inputType) {
if (SDLActivity.keyboardInputType != inputType){
SDLActivity.keyboardInputType = inputType;
InputMethodManager imm = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.restartInput(mTextEdit);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,13 @@ public enum NativeState {
// This is what SDL runs in. It invokes SDL_main(), eventually
protected static Thread mSDLThread;

public static int keyboardInputType = InputType.TYPE_CLASS_TEXT;

protected static SDLGenericMotionListener_API12 getMotionListener() {
if (mMotionListener == null) {
if (Build.VERSION.SDK_INT >= 26) {
mMotionListener = new SDLGenericMotionListener_API26();
} else
} else
if (Build.VERSION.SDK_INT >= 24) {
mMotionListener = new SDLGenericMotionListener_API24();
} else {
Expand Down Expand Up @@ -326,15 +328,15 @@ public static int getCurrentOrientation() {
case Surface.ROTATION_0:
result = SDL_ORIENTATION_PORTRAIT;
break;

case Surface.ROTATION_90:
result = SDL_ORIENTATION_LANDSCAPE;
break;

case Surface.ROTATION_180:
result = SDL_ORIENTATION_PORTRAIT_FLIPPED;
break;

case Surface.ROTATION_270:
result = SDL_ORIENTATION_LANDSCAPE_FLIPPED;
break;
Expand Down Expand Up @@ -584,7 +586,7 @@ public void handleMessage(Message msg) {
View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN |
View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION |
View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.INVISIBLE;
window.getDecorView().setSystemUiVisibility(flags);
window.getDecorView().setSystemUiVisibility(flags);
window.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
window.clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
SDLActivity.mFullscreenModeActive = true;
Expand All @@ -609,7 +611,7 @@ public void handleMessage(Message msg) {

InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(mTextEdit.getWindowToken(), 0);

mScreenKeyboardShown = false;
}
break;
Expand Down Expand Up @@ -650,14 +652,14 @@ boolean sendCommand(int command, Object data) {
// or 500ms have passed.

boolean bShouldWait = false;

if (data instanceof Integer) {
// Let's figure out if we're already laid out fullscreen or not.
Display display = ((WindowManager)getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
android.util.DisplayMetrics realMetrics = new android.util.DisplayMetrics();
display.getRealMetrics( realMetrics );
boolean bFullscreenLayout = ((realMetrics.widthPixels == mSurface.getWidth()) &&

boolean bFullscreenLayout = ((realMetrics.widthPixels == mSurface.getWidth()) &&
(realMetrics.heightPixels == mSurface.getHeight()));

if (((Integer)data).intValue() == 1) {
Expand All @@ -682,7 +684,7 @@ boolean sendCommand(int command, Object data) {
// size we need, instead of grabbing a size that's still got
// the navigation and/or status bars before they're hidden.
//
// We'll wait for up to half a second, because some devices
// We'll wait for up to half a second, because some devices
// take a surprisingly long time for the surface resize, but
// then we'll just give up and return.
//
Expand Down Expand Up @@ -743,19 +745,19 @@ public static void setWindowStyle(boolean fullscreen) {
/**
* This method is called by SDL using JNI.
* This is a static method for JNI convenience, it calls a non-static method
* so that is can be overridden
* so that is can be overridden
*/
public static void setOrientation(int w, int h, boolean resizable, String hint)
{
if (mSingleton != null) {
mSingleton.setOrientationBis(w, h, resizable, hint);
}
}

/**
* This can be overridden
*/
public void setOrientationBis(int w, int h, boolean resizable, String hint)
public void setOrientationBis(int w, int h, boolean resizable, String hint)
{
int orientation = -1;

Expand Down Expand Up @@ -795,7 +797,7 @@ public void setOrientationBis(int w, int h, boolean resizable, String hint)
/**
* This method is called by SDL using JNI.
*/
public static boolean isScreenKeyboardShown()
public static boolean isScreenKeyboardShown()
{
if (mTextEdit == null) {
return false;
Expand All @@ -820,7 +822,7 @@ public static boolean supportsRelativeMouse()
return false;
}

// DeX mode in Samsung Experience 9.0 and earlier doesn't support relative mice properly under
// DeX mode in Samsung Experience 9.0 and earlier doesn't support relative mice properly under
// Android 7 APIs, and simply returns no data under Android 8 APIs.
//
// This is fixed in Samsung Experience 9.5, which corresponds to Android 8.1.0, and
Expand Down Expand Up @@ -902,7 +904,7 @@ public static boolean isTablet() {
*/
public static boolean isChromebook() {
return getContext().getPackageManager().hasSystemFeature("org.chromium.arc.device_management");
}
}

/**
* This method is called by SDL using JNI.
Expand Down Expand Up @@ -948,15 +950,15 @@ public static boolean getManifestEnvironmentVariables() {
}
}
/* environment variables set! */
return true;
return true;
} catch (Exception e) {
Log.v("SDL", "exception " + e.toString());
}
return false;
}

// This method is called by SDLControllerManager's API 26 Generic Motion Handler.
public static View getContentView()
public static View getContentView()
{
return mSingleton.mLayout;
}
Expand Down Expand Up @@ -1011,12 +1013,12 @@ public static boolean showTextInput(int x, int y, int w, int h) {
}

public static boolean isTextInputEvent(KeyEvent event) {

// Key pressed with Ctrl should be sent as SDL_KEYDOWN/SDL_KEYUP and not SDL_TEXTINPUT
if (Build.VERSION.SDK_INT >= 11) {
if (event.isCtrlPressed()) {
return false;
}
}
}

return event.isPrintingKey() || event.getKeyCode() == KeyEvent.KEYCODE_SPACE;
Expand Down Expand Up @@ -1350,7 +1352,7 @@ public void onSystemUiVisibilityChange(int visibility) {
}

}
}
}

/**
* This method is called by SDL using JNI.
Expand Down Expand Up @@ -1862,7 +1864,7 @@ public void onSensorChanged(SensorEvent event) {
// Since we may have an orientation set, we won't receive onConfigurationChanged events.
// We thus should check here.
int newOrientation = SDLActivity.SDL_ORIENTATION_UNKNOWN;

float x, y;
switch (mDisplay.getRotation()) {
case Surface.ROTATION_90:
Expand Down Expand Up @@ -1896,7 +1898,7 @@ public void onSensorChanged(SensorEvent event) {
y / SensorManager.GRAVITY_EARTH,
event.values[2] / SensorManager.GRAVITY_EARTH);


}
}

Expand Down Expand Up @@ -1964,7 +1966,7 @@ public boolean onCheckIsTextEditor() {

@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
/*
/*
* This handles the hardware keyboard input
*/
if (event.getAction() == KeyEvent.ACTION_DOWN) {
Expand Down Expand Up @@ -2002,7 +2004,7 @@ public boolean onKeyPreIme (int keyCode, KeyEvent event) {
public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
ic = new SDLInputConnection(this, true);

outAttrs.inputType = InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD;
outAttrs.inputType = SDLActivity.keyboardInputType;
outAttrs.imeOptions = EditorInfo.IME_FLAG_NO_EXTRACT_UI
| EditorInfo.IME_FLAG_NO_FULLSCREEN /* API 11 */;

Expand Down Expand Up @@ -2084,7 +2086,7 @@ public boolean deleteSurroundingText(int beforeLength, int afterLength) {
while (beforeLength-- > 0) {
boolean ret_key = sendKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DEL))
&& sendKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_DEL));
ret = ret && ret_key;
ret = ret && ret_key;
}
return ret;
}
Expand All @@ -2103,7 +2105,7 @@ interface SDLClipboardHandler {


class SDLClipboardHandler_API11 implements
SDLClipboardHandler,
SDLClipboardHandler,
android.content.ClipboardManager.OnPrimaryClipChangedListener {

protected android.content.ClipboardManager mClipMgr;
Expand Down Expand Up @@ -2134,7 +2136,7 @@ public void clipboardSetText(String string) {
mClipMgr.setText(string);
mClipMgr.addPrimaryClipChangedListener(this);
}

@Override
public void onPrimaryClipChanged() {
SDLActivity.onNativeClipboardChanged();
Expand All @@ -2144,9 +2146,9 @@ public void onPrimaryClipChanged() {

class SDLClipboardHandler_Old implements
SDLClipboardHandler {

protected android.text.ClipboardManager mClipMgrOld;

SDLClipboardHandler_Old() {
mClipMgrOld = (android.text.ClipboardManager) SDL.getContext().getSystemService(Context.CLIPBOARD_SERVICE);
}
Expand All @@ -2171,4 +2173,3 @@ public void clipboardSetText(String string) {
mClipMgrOld.setText(string);
}
}