From 1987911c7d2777fd50e0bb2952d091920d6012f5 Mon Sep 17 00:00:00 2001 From: Simon Lightfoot Date: Thu, 24 May 2018 23:21:33 +0100 Subject: [PATCH] Add support for physical keyboards on Android (flutter/engine#5324) * Fixes flutter/flutter#7943 * Fixes flutter/flutter#9347 * Fixes flutter/flutter#11177 --- engine/src/flutter/AUTHORS | 1 + .../editing/InputConnectionAdaptor.java | 27 +++++++++++++------ .../android/io/flutter/view/FlutterView.java | 14 +++++++++- 3 files changed, 33 insertions(+), 9 deletions(-) diff --git a/engine/src/flutter/AUTHORS b/engine/src/flutter/AUTHORS index cfae1d6367..3d412bd08c 100644 --- a/engine/src/flutter/AUTHORS +++ b/engine/src/flutter/AUTHORS @@ -9,3 +9,4 @@ Ali Bitek Jacob Greenfield Dan Field Victor Choueiri +Simon Lightfoot diff --git a/engine/src/flutter/shell/platform/android/io/flutter/plugin/editing/InputConnectionAdaptor.java b/engine/src/flutter/shell/platform/android/io/flutter/plugin/editing/InputConnectionAdaptor.java index e3d8acc000..af7da838a3 100644 --- a/engine/src/flutter/shell/platform/android/io/flutter/plugin/editing/InputConnectionAdaptor.java +++ b/engine/src/flutter/shell/platform/android/io/flutter/plugin/editing/InputConnectionAdaptor.java @@ -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 diff --git a/engine/src/flutter/shell/platform/android/io/flutter/view/FlutterView.java b/engine/src/flutter/shell/platform/android/io/flutter/view/FlutterView.java index 7734c29a0e..e80ccc0ab1 100644 --- a/engine/src/flutter/shell/platform/android/io/flutter/view/FlutterView.java +++ b/engine/src/flutter/shell/platform/android/io/flutter/view/FlutterView.java @@ -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 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;