Bug workaround to create KEYCODE_DEL events.

This commit is contained in:
James Sanford 2012-10-17 21:13:34 -07:00
parent b039ca01c4
commit 92ad0afb3c
3 changed files with 69 additions and 2 deletions

View File

@ -0,0 +1,53 @@
package com.froop.app.kegs;
import android.os.SystemClock;
import android.util.Log;
import android.view.KeyCharacterMap;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.BaseInputConnection;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputConnection;
// This is a bug workaround so that we see KEYCODE_DEL on API 16 JELLY_BEAN
// and later. Let me know what the "right" way of doing this is!
class InputFix {
public static InputConnection getInputConnection(View view, EditorInfo attrs) {
attrs.inputType = EditorInfo.TYPE_NULL;
attrs.imeOptions |= (EditorInfo.IME_FLAG_NO_EXTRACT_UI |
EditorInfo.IME_FLAG_NO_ENTER_ACTION |
EditorInfo.IME_ACTION_NONE);
// Change defaults so that the IME makes the API calls.
attrs.initialSelStart = 0;
attrs.initialSelEnd = 0;
InputConnection ic = new BaseInputConnection(view, false) {
@Override
public boolean deleteSurroundingText(int before, int after) {
if (before == 1 && after == 0) {
// In API 16 JELLY_BEAN, the IME no longer sends KEYCODE_DEL
// for 'fake' editors like us. Force it to send the code
// instead of this "delete one character" request.
//
// There must be a better way to handle input rather than
// providing an entire InputConnection or hacking it like this.
final long eventTime = SystemClock.uptimeMillis();
final int keyCode = KeyEvent.KEYCODE_DEL;
sendKeyEvent(new KeyEvent(eventTime, eventTime, KeyEvent.ACTION_DOWN,
keyCode, 0, 0, KeyCharacterMap.VIRTUAL_KEYBOARD, 0,
KeyEvent.FLAG_SOFT_KEYBOARD | KeyEvent.FLAG_KEEP_TOUCH_MODE));
sendKeyEvent(new KeyEvent(eventTime, eventTime, KeyEvent.ACTION_UP,
keyCode, 0, 0, KeyCharacterMap.VIRTUAL_KEYBOARD, 0,
KeyEvent.FLAG_SOFT_KEYBOARD | KeyEvent.FLAG_KEEP_TOUCH_MODE));
return true;
} else {
return super.deleteSurroundingText(before, after);
}
}
};
return ic;
}
}

View File

@ -5,6 +5,8 @@ import android.graphics.Bitmap;
import android.os.Handler;
import android.util.AttributeSet;
import android.util.Log;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputConnection;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
@ -43,6 +45,12 @@ class KegsView extends SurfaceView implements KegsThread.UpdateScreen, SurfaceHo
setMeasuredDimension(mWidth, mHeight);
}
@Override
public InputConnection onCreateInputConnection(EditorInfo attrs) {
// Bug workaround to force KEYCODE_DEL.
return InputFix.getInputConnection(this, attrs);
}
private FpsCounter fpsCount = new FpsCounter("kegs", "native");
public Bitmap getBitmap() {

View File

@ -4,10 +4,10 @@ import android.content.Context;
import android.graphics.Bitmap;
import android.util.AttributeSet;
import android.util.Log;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputConnection;
import android.opengl.GLSurfaceView;
// FIXME: before use, make sure KegsMain calls our onPause and onResume funcs
class KegsViewGL extends GLSurfaceView implements KegsThread.UpdateScreen {
// Reported area of this view, see updateScreenSize()
private int mWidth = 0;
@ -37,6 +37,12 @@ class KegsViewGL extends GLSurfaceView implements KegsThread.UpdateScreen {
setMeasuredDimension(mWidth, mHeight);
}
@Override
public InputConnection onCreateInputConnection(EditorInfo attrs) {
// Bug workaround to force KEYCODE_DEL.
return InputFix.getInputConnection(this, attrs);
}
public Bitmap getBitmap() {
return mBitmap;
}