Add support for physical keyboards on Android (flutter/engine#5324)

* Fixes flutter/flutter#7943
* Fixes flutter/flutter#9347
* Fixes flutter/flutter#11177
This commit is contained in:
Simon Lightfoot
2018-05-24 23:21:33 +01:00
committed by Chris Bracken
parent 0fdf193472
commit 1987911c7d
3 changed files with 33 additions and 9 deletions

View File

@@ -9,3 +9,4 @@ Ali Bitek <alibitek@protonmail.ch>
Jacob Greenfield <jacob.greenfield.256@gmail.com>
Dan Field <dfield@gmail.com>
Victor Choueiri <victor@ctrlanddev.com>
Simon Lightfoot <simon@devangels.london>

View File

@@ -128,36 +128,47 @@ class InputConnectionAdaptor extends BaseInputConnection {
@Override
public boolean sendKeyEvent(KeyEvent event) {
final boolean result = super.sendKeyEvent(event);
if (event.getAction() == KeyEvent.ACTION_UP) {
if (event.getAction() == KeyEvent.ACTION_DOWN) {
if (event.getKeyCode() == KeyEvent.KEYCODE_DEL) {
int selStart = Selection.getSelectionStart(mEditable);
int selEnd = Selection.getSelectionEnd(mEditable);
if (selEnd > selStart) {
// Delete the selection.
Selection.setSelection(mEditable, selStart);
deleteSurroundingText(0, selEnd - selStart);
mEditable.delete(selStart, selEnd);
updateEditingState();
return true;
} else if (selStart > 0) {
// Delete to the left of the cursor.
Selection.setSelection(mEditable, selStart - 1);
deleteSurroundingText(0, 1);
int newSel = Math.max(selStart - 1, 0);
Selection.setSelection(mEditable, newSel);
mEditable.delete(newSel, selStart);
updateEditingState();
return true;
}
} else if (event.getKeyCode() == KeyEvent.KEYCODE_DPAD_LEFT) {
int selStart = Selection.getSelectionStart(mEditable);
int newSel = Math.max(selStart - 1, 0);
setSelection(newSel, newSel);
return true;
} else if (event.getKeyCode() == KeyEvent.KEYCODE_DPAD_RIGHT) {
int selStart = Selection.getSelectionStart(mEditable);
int newSel = Math.min(selStart + 1, mEditable.length());
setSelection(newSel, newSel);
return true;
} else {
// Enter a character.
int character = event.getUnicodeChar();
if (character != 0)
commitText(String.valueOf((char) character), 1);
if (character != 0) {
int selStart = Math.max(0, Selection.getSelectionStart(mEditable));
mEditable.insert(selStart, String.valueOf((char) character));
setSelection(selStart + 1, selStart + 1);
updateEditingState();
}
return true;
}
}
return result;
return false;
}
@Override

View File

@@ -22,6 +22,7 @@ import android.text.format.DateFormat;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;
import android.view.KeyCharacterMap;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.Surface;
@@ -33,6 +34,7 @@ import android.view.accessibility.AccessibilityManager;
import android.view.accessibility.AccessibilityNodeProvider;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputConnection;
import android.view.inputmethod.InputMethodManager;
import io.flutter.app.FlutterActivity;
import io.flutter.app.FlutterPluginRegistry;
import io.flutter.plugin.common.*;
@@ -98,6 +100,7 @@ public class FlutterView extends SurfaceView
int physicalViewInsetLeft = 0;
}
private final InputMethodManager mImm;
private final TextInputPlugin mTextInputPlugin;
private final SurfaceHolder.Callback mSurfaceCallback;
private final ViewportMetrics mMetrics;
@@ -114,6 +117,7 @@ public class FlutterView extends SurfaceView
private final AtomicLong nextTextureId = new AtomicLong(0L);
private FlutterNativeView mNativeView;
private boolean mIsSoftwareRenderingEnabled = false; // using the software renderer or not
private InputConnection mLastInputConnection;
public FlutterView(Context context) {
this(context, null);
@@ -197,6 +201,7 @@ public class FlutterView extends SurfaceView
"flutter/platform", JSONMethodCodec.INSTANCE);
flutterPlatformChannel.setMethodCallHandler(platformPlugin);
addActivityLifecycleListener(platformPlugin);
mImm = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
mTextInputPlugin = new TextInputPlugin(this);
setLocale(getResources().getConfiguration().locale);
@@ -238,6 +243,12 @@ public class FlutterView extends SurfaceView
return super.onKeyDown(keyCode, event);
}
if (event.getDeviceId() != KeyCharacterMap.VIRTUAL_KEYBOARD) {
if (mLastInputConnection != null && mImm.isAcceptingText()) {
mLastInputConnection.sendKeyEvent(event);
}
}
Map<String, Object> message = new HashMap<>();
message.put("type", "keydown");
message.put("keymap", "android");
@@ -372,7 +383,8 @@ public class FlutterView extends SurfaceView
@Override
public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
try {
return mTextInputPlugin.createInputConnection(this, outAttrs);
mLastInputConnection = mTextInputPlugin.createInputConnection(this, outAttrs);
return mLastInputConnection;
} catch (JSONException e) {
Log.e(TAG, "Failed to create input connection", e);
return null;