Fixed bug 1614 - SDL for Android does not implement TextInput API
authorSam Lantinga <slouken@libsdl.org>
Wed, 03 Oct 2012 20:49:16 -0700
changeset 6555f2c03c06d987
parent 6554 5bcda034e678
child 6556 d5e854f0d9a9
Fixed bug 1614 - SDL for Android does not implement TextInput API

Andrey Isakov 2012-10-03 08:30:25 PDT

I've found out in the process of porting one OS project to Android/SDL2 that
there is no support for TextInput events/APIs on Android.
So I implemented some kind of initial support of that feature, and at the very
least it seems to work fine with latin chars input with soft and hardware
keyboards on my Moto Milestone2. I've also tried playing around with more
complex IMEs, like japanese, logging the process and it seemed to work too. I'm
not sure since the app itself I am working on does not have support for
non-latin input.

The main point of the patch is to place a fake input view in the region
specified by SDL_SetTextInputRect and create a custom InputConnection for it.
The reason to make it a separate view is to support Android's pan&scan on input
feature properly. For details please refer to
http://android-developers.blogspot.com/2009/04/updating-applications-for-on-screen.html
Even though the manual states that SetTextInputRect is used to determine the
IME variants position, I thought this would be a proper use for this too.
android-project/src/org/libsdl/app/SDLActivity.java
src/core/android/SDL_android.cpp
src/core/android/SDL_android.h
src/video/android/SDL_androidkeyboard.c
src/video/android/SDL_androidkeyboard.h
src/video/android/SDL_androidvideo.c
src/video/android/SDL_androidvideo.h
     1.1 --- a/android-project/src/org/libsdl/app/SDLActivity.java	Wed Oct 03 20:22:14 2012 -0700
     1.2 +++ b/android-project/src/org/libsdl/app/SDLActivity.java	Wed Oct 03 20:49:16 2012 -0700
     1.3 @@ -9,7 +9,11 @@
     1.4  import android.app.*;
     1.5  import android.content.*;
     1.6  import android.view.*;
     1.7 +import android.view.inputmethod.BaseInputConnection;
     1.8 +import android.view.inputmethod.EditorInfo;
     1.9 +import android.view.inputmethod.InputConnection;
    1.10  import android.view.inputmethod.InputMethodManager;
    1.11 +import android.widget.AbsoluteLayout;
    1.12  import android.os.*;
    1.13  import android.util.Log;
    1.14  import android.graphics.*;
    1.15 @@ -33,6 +37,8 @@
    1.16      // Main components
    1.17      private static SDLActivity mSingleton;
    1.18      private static SDLSurface mSurface;
    1.19 +    private static View mTextEdit;
    1.20 +    private static ViewGroup mLayout;
    1.21  
    1.22      // This is what SDL runs in. It invokes SDL_main(), eventually
    1.23      private static Thread mSDLThread;
    1.24 @@ -70,7 +76,12 @@
    1.25  
    1.26          // Set up the surface
    1.27          mSurface = new SDLSurface(getApplication());
    1.28 -        setContentView(mSurface);
    1.29 +
    1.30 +        mLayout = new AbsoluteLayout(this);
    1.31 +        mLayout.addView(mSurface);
    1.32 +
    1.33 +        setContentView(mLayout);
    1.34 +
    1.35          SurfaceHolder holder = mSurface.getHolder();
    1.36      }
    1.37  
    1.38 @@ -109,6 +120,7 @@
    1.39      // Messages from the SDLMain thread
    1.40      static final int COMMAND_CHANGE_TITLE = 1;
    1.41      static final int COMMAND_KEYBOARD_SHOW = 2;
    1.42 +    static final int COMMAND_TEXTEDIT_HIDE = 3;
    1.43  
    1.44      // Handler for the messages
    1.45      Handler commandHandler = new Handler() {
    1.46 @@ -134,6 +146,14 @@
    1.47                      }
    1.48                  }
    1.49                 break;
    1.50 +            case COMMAND_TEXTEDIT_HIDE:
    1.51 +                if (mTextEdit != null) {
    1.52 +                    mTextEdit.setVisibility(View.GONE);
    1.53 +
    1.54 +                    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
    1.55 +                    imm.hideSoftInputFromWindow(mTextEdit.getWindowToken(), 0);
    1.56 +                }
    1.57 +                break;
    1.58              }
    1.59          }
    1.60      };
    1.61 @@ -201,6 +221,50 @@
    1.62              }
    1.63          }
    1.64      }
    1.65 +    
    1.66 +    static class ShowTextInputHandler implements Runnable {
    1.67 +        /*
    1.68 +         * This is used to regulate the pan&scan method to have some offset from
    1.69 +         * the bottom edge of the input region and the top edge of an input
    1.70 +         * method (soft keyboard)
    1.71 +         */
    1.72 +        static final int HEIGHT_PADDING = 15;
    1.73 +
    1.74 +        public int x, y, w, h;
    1.75 +
    1.76 +        public ShowTextInputHandler(int x, int y, int w, int h) {
    1.77 +            this.x = x;
    1.78 +            this.y = y;
    1.79 +            this.w = w;
    1.80 +            this.h = h;
    1.81 +        }
    1.82 +
    1.83 +        public void run() {
    1.84 +            AbsoluteLayout.LayoutParams params = new AbsoluteLayout.LayoutParams(
    1.85 +                    w, h + HEIGHT_PADDING, x, y);
    1.86 +
    1.87 +            if (mTextEdit == null) {
    1.88 +                mTextEdit = new DummyEdit(getContext());
    1.89 +
    1.90 +                mLayout.addView(mTextEdit, params);
    1.91 +            } else {
    1.92 +                mTextEdit.setLayoutParams(params);
    1.93 +            }
    1.94 +
    1.95 +            mTextEdit.setVisibility(View.VISIBLE);
    1.96 +            mTextEdit.requestFocus();
    1.97 +
    1.98 +            InputMethodManager imm = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
    1.99 +            imm.showSoftInput(mTextEdit, 0);
   1.100 +        }
   1.101 +
   1.102 +    }
   1.103 +
   1.104 +    public static void showTextInput(int x, int y, int w, int h) {
   1.105 +        // Transfer the task to the main thread as a Runnable
   1.106 +        mSingleton.commandHandler.post(new ShowTextInputHandler(x, y, w, h));
   1.107 +    }
   1.108 +
   1.109  
   1.110      // EGL functions
   1.111      public static boolean initEGL(int majorVersion, int minorVersion) {
   1.112 @@ -623,5 +687,104 @@
   1.113                                        event.values[2] / SensorManager.GRAVITY_EARTH);
   1.114          }
   1.115      }
   1.116 +    
   1.117 +}
   1.118 +
   1.119 +/* This is a fake invisible editor view that receives the input and defines the
   1.120 + * pan&scan region
   1.121 + */
   1.122 +class DummyEdit extends View implements View.OnKeyListener {
   1.123 +    InputConnection ic;
   1.124 +
   1.125 +    public DummyEdit(Context context) {
   1.126 +        super(context);
   1.127 +        setFocusableInTouchMode(true);
   1.128 +        setFocusable(true);
   1.129 +        setOnKeyListener(this);
   1.130 +    }
   1.131 +
   1.132 +    @Override
   1.133 +    public boolean onCheckIsTextEditor() {
   1.134 +        return true;
   1.135 +    }
   1.136 +
   1.137 +    public boolean onKey(View v, int keyCode, KeyEvent event) {
   1.138 +
   1.139 +        // This handles the hardware keyboard input
   1.140 +        if (event.isPrintingKey()) {
   1.141 +            if (event.getAction() == KeyEvent.ACTION_DOWN) {
   1.142 +                ic.commitText(String.valueOf((char) event.getUnicodeChar()), 1);
   1.143 +            }
   1.144 +            return true;
   1.145 +        }
   1.146 +
   1.147 +        if (event.getAction() == KeyEvent.ACTION_DOWN) {
   1.148 +            SDLActivity.onNativeKeyDown(keyCode);
   1.149 +            return true;
   1.150 +        } else if (event.getAction() == KeyEvent.ACTION_UP) {
   1.151 +            SDLActivity.onNativeKeyUp(keyCode);
   1.152 +            return true;
   1.153 +        }
   1.154 +
   1.155 +        return false;
   1.156 +    }
   1.157 +
   1.158 +    @Override
   1.159 +    public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
   1.160 +        ic = new SDLInputConnection(this, true);
   1.161 +
   1.162 +        outAttrs.imeOptions = EditorInfo.IME_FLAG_NO_EXTRACT_UI
   1.163 +                | EditorInfo.IME_FLAG_NO_FULLSCREEN;
   1.164 +
   1.165 +        return ic;
   1.166 +    }
   1.167 +}
   1.168 +
   1.169 +class SDLInputConnection extends BaseInputConnection {
   1.170 +
   1.171 +    public SDLInputConnection(View targetView, boolean fullEditor) {
   1.172 +        super(targetView, fullEditor);
   1.173 +
   1.174 +    }
   1.175 +
   1.176 +    @Override
   1.177 +    public boolean sendKeyEvent(KeyEvent event) {
   1.178 +
   1.179 +        /*
   1.180 +         * This handles the keycodes from soft keyboard (and IME-translated
   1.181 +         * input from hardkeyboard)
   1.182 +         */
   1.183 +        int keyCode = event.getKeyCode();
   1.184 +        if (event.getAction() == KeyEvent.ACTION_DOWN) {
   1.185 +
   1.186 +            SDLActivity.onNativeKeyDown(keyCode);
   1.187 +            return true;
   1.188 +        } else if (event.getAction() == KeyEvent.ACTION_UP) {
   1.189 +
   1.190 +            SDLActivity.onNativeKeyUp(keyCode);
   1.191 +            return true;
   1.192 +        }
   1.193 +        return super.sendKeyEvent(event);
   1.194 +    }
   1.195 +
   1.196 +    @Override
   1.197 +    public boolean commitText(CharSequence text, int newCursorPosition) {
   1.198 +
   1.199 +        nativeCommitText(text.toString(), newCursorPosition);
   1.200 +
   1.201 +        return super.commitText(text, newCursorPosition);
   1.202 +    }
   1.203 +
   1.204 +    @Override
   1.205 +    public boolean setComposingText(CharSequence text, int newCursorPosition) {
   1.206 +
   1.207 +        nativeSetComposingText(text.toString(), newCursorPosition);
   1.208 +
   1.209 +        return super.setComposingText(text, newCursorPosition);
   1.210 +    }
   1.211 +
   1.212 +    public native void nativeCommitText(String text, int newCursorPosition);
   1.213 +
   1.214 +    public native void nativeSetComposingText(String text, int newCursorPosition);
   1.215  
   1.216  }
     2.1 --- a/src/core/android/SDL_android.cpp	Wed Oct 03 20:22:14 2012 -0700
     2.2 +++ b/src/core/android/SDL_android.cpp	Wed Oct 03 20:49:16 2012 -0700
     2.3 @@ -218,6 +218,30 @@
     2.4      Android_RunAudioThread();
     2.5  }
     2.6  
     2.7 +extern "C" void Java_org_libsdl_app_SDLInputConnection_nativeCommitText(
     2.8 +                                    JNIEnv* env, jclass cls,
     2.9 +                                    jstring text, jint newCursorPosition)
    2.10 +{
    2.11 +    const char *utftext = env->GetStringUTFChars(text, NULL);
    2.12 +
    2.13 +    SDL_SendKeyboardText(utftext);
    2.14 +
    2.15 +    env->ReleaseStringUTFChars(text, utftext);
    2.16 +}
    2.17 +
    2.18 +extern "C" void Java_org_libsdl_app_SDLInputConnection_nativeSetComposingText(
    2.19 +                                    JNIEnv* env, jclass cls,
    2.20 +                                    jstring text, jint newCursorPosition)
    2.21 +{
    2.22 +    const char *utftext = env->GetStringUTFChars(text, NULL);
    2.23 +
    2.24 +    SDL_SendEditingText(utftext, 0, 0);
    2.25 +
    2.26 +    env->ReleaseStringUTFChars(text, utftext);
    2.27 +}
    2.28 +
    2.29 +
    2.30 +
    2.31  
    2.32  /*******************************************************************************
    2.33               Functions called by SDL into Java
    2.34 @@ -918,6 +942,42 @@
    2.35      return 0;
    2.36  }
    2.37  
    2.38 +extern "C" int Android_JNI_ShowTextInput(SDL_Rect *inputRect)
    2.39 +{
    2.40 +    JNIEnv *env = Android_JNI_GetEnv();
    2.41 +    if (!env) {
    2.42 +        return -1;
    2.43 +    }
    2.44 +
    2.45 +    jmethodID mid = env->GetStaticMethodID(mActivityClass, "showTextInput", "(IIII)V");
    2.46 +    if (!mid) {
    2.47 +        return -1;
    2.48 +    }
    2.49 +    env->CallStaticVoidMethod( mActivityClass, mid,
    2.50 +                               inputRect->x,
    2.51 +                               inputRect->y,
    2.52 +                               inputRect->w,
    2.53 +                               inputRect->h );
    2.54 +    return 0;
    2.55 +}
    2.56 +
    2.57 +/*extern "C" int Android_JNI_HideTextInput()
    2.58 +{
    2.59 +
    2.60 +
    2.61 +    JNIEnv *env = Android_JNI_GetEnv();
    2.62 +    if (!env) {
    2.63 +        return -1;
    2.64 +    }
    2.65 +
    2.66 +    jmethodID mid = env->GetStaticMethodID(mActivityClass, "hideTextInput", "()V");
    2.67 +    if (!mid) {
    2.68 +        return -1;
    2.69 +    }
    2.70 +    env->CallStaticVoidMethod(mActivityClass, mid);
    2.71 +    return 0;
    2.72 +}*/
    2.73 +
    2.74  #endif /* __ANDROID__ */
    2.75  
    2.76  /* vi: set ts=4 sw=4 expandtab: */
     3.1 --- a/src/core/android/SDL_android.h	Wed Oct 03 20:22:14 2012 -0700
     3.2 +++ b/src/core/android/SDL_android.h	Wed Oct 03 20:49:16 2012 -0700
     3.3 @@ -27,11 +27,14 @@
     3.4  /* *INDENT-ON* */
     3.5  #endif
     3.6  
     3.7 +#include "SDL_rect.h"
     3.8 +
     3.9  /* Interface from the SDL library into the Android Java activity */
    3.10  extern SDL_bool Android_JNI_CreateContext(int majorVersion, int minorVersion);
    3.11  extern void Android_JNI_SwapWindow();
    3.12  extern void Android_JNI_SetActivityTitle(const char *title);
    3.13  extern SDL_bool Android_JNI_GetAccelerometerValues(float values[3]);
    3.14 +extern int Android_JNI_ShowTextInput(SDL_Rect *inputRect);
    3.15  
    3.16  // Audio support
    3.17  extern int Android_JNI_OpenAudioDevice(int sampleRate, int is16Bit, int channelCount, int desiredBufferFrames);
     4.1 --- a/src/video/android/SDL_androidkeyboard.c	Wed Oct 03 20:22:14 2012 -0700
     4.2 +++ b/src/video/android/SDL_androidkeyboard.c	Wed Oct 03 20:49:16 2012 -0700
     4.3 @@ -321,6 +321,27 @@
     4.4      return SDL_FALSE;
     4.5  }
     4.6  
     4.7 +void
     4.8 +Android_StartTextInput(_THIS)
     4.9 +{
    4.10 +    SDL_VideoData *videodata = (SDL_VideoData *)_this->driverdata;
    4.11 +    Android_JNI_ShowTextInput(&videodata->textRect);
    4.12 +}
    4.13 +
    4.14 +#define COMMAND_TEXTEDIT_HIDE 3
    4.15 +void
    4.16 +Android_StopTextInput(_THIS)
    4.17 +{
    4.18 +    Android_JNI_SendMessage(COMMAND_TEXTEDIT_HIDE, 0);
    4.19 +}
    4.20 +
    4.21 +void
    4.22 +Android_SetTextInputRect(_THIS, SDL_Rect *rect)
    4.23 +{
    4.24 +    SDL_VideoData *videodata = (SDL_VideoData *)_this->driverdata;
    4.25 +    videodata->textRect = *rect;
    4.26 +}
    4.27 +
    4.28  #endif /* SDL_VIDEO_DRIVER_ANDROID */
    4.29  
    4.30  /* vi: set ts=4 sw=4 expandtab: */
     5.1 --- a/src/video/android/SDL_androidkeyboard.h	Wed Oct 03 20:22:14 2012 -0700
     5.2 +++ b/src/video/android/SDL_androidkeyboard.h	Wed Oct 03 20:49:16 2012 -0700
     5.3 @@ -32,4 +32,8 @@
     5.4  extern int Android_ToggleScreenKeyboard(_THIS, SDL_Window * window);
     5.5  extern SDL_bool Android_IsScreenKeyboardShown(_THIS, SDL_Window * window);
     5.6  
     5.7 +extern void Android_StartTextInput(_THIS);
     5.8 +extern void Android_StopTextInput(_THIS);
     5.9 +extern void Android_SetTextInputRect(_THIS, SDL_Rect *rect);
    5.10 +
    5.11  /* vi: set ts=4 sw=4 expandtab: */
     6.1 --- a/src/video/android/SDL_androidvideo.c	Wed Oct 03 20:22:14 2012 -0700
     6.2 +++ b/src/video/android/SDL_androidvideo.c	Wed Oct 03 20:49:16 2012 -0700
     6.3 @@ -87,17 +87,24 @@
     6.4  {
     6.5      printf("Creating video device\n");
     6.6      SDL_VideoDevice *device;
     6.7 +    SDL_VideoData *data;
     6.8  
     6.9      /* Initialize all variables that we clean on shutdown */
    6.10      device = (SDL_VideoDevice *) SDL_calloc(1, sizeof(SDL_VideoDevice));
    6.11      if (!device) {
    6.12          SDL_OutOfMemory();
    6.13 -        if (device) {
    6.14 -            SDL_free(device);
    6.15 -        }
    6.16 -        return (0);
    6.17 +        return NULL;
    6.18      }
    6.19  
    6.20 +    data = (SDL_VideoData*) SDL_calloc(1, sizeof(SDL_VideoData));
    6.21 +    if (!data) {
    6.22 +        SDL_OutOfMemory();
    6.23 +        SDL_free(device);
    6.24 +        return NULL;
    6.25 +    }
    6.26 +
    6.27 +    device->driverdata = data;
    6.28 +
    6.29      /* Set the function pointers */
    6.30      device->VideoInit = Android_VideoInit;
    6.31      device->VideoQuit = Android_VideoQuit;
    6.32 @@ -132,6 +139,11 @@
    6.33      device->GetClipboardText = Android_GetClipboardText;
    6.34      device->HasClipboardText = Android_HasClipboardText;
    6.35  
    6.36 +    /* Text input */
    6.37 +    device->StartTextInput = Android_StartTextInput;
    6.38 +    device->StopTextInput = Android_StopTextInput;
    6.39 +    device->SetTextInputRect = Android_SetTextInputRect;
    6.40 +
    6.41      return device;
    6.42  }
    6.43  
     7.1 --- a/src/video/android/SDL_androidvideo.h	Wed Oct 03 20:22:14 2012 -0700
     7.2 +++ b/src/video/android/SDL_androidvideo.h	Wed Oct 03 20:49:16 2012 -0700
     7.3 @@ -24,6 +24,7 @@
     7.4  #define _SDL_androidvideo_h
     7.5  
     7.6  #include "SDL_mutex.h"
     7.7 +#include "SDL_rect.h"
     7.8  #include "../SDL_sysvideo.h"
     7.9  
    7.10  /* Called by the JNI layer when the screen changes size or format */
    7.11 @@ -31,6 +32,11 @@
    7.12  
    7.13  /* Private display data */
    7.14  
    7.15 +typedef struct SDL_VideoData
    7.16 +{
    7.17 +    SDL_Rect        textRect;
    7.18 +} SDL_VideoData;
    7.19 +
    7.20  extern int Android_ScreenWidth;
    7.21  extern int Android_ScreenHeight;
    7.22  extern Uint32 Android_ScreenFormat;