From f7b1ec98ca957c7bfb2dd560bdb39c6f6ce6be40 Mon Sep 17 00:00:00 2001 From: garyqian Date: Mon, 13 May 2019 15:49:37 +0800 Subject: [PATCH 1/2] Use built-in text selection shifting API to delete --- .../io/flutter/plugin/editing/InputConnectionAdaptor.java | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/shell/platform/android/io/flutter/plugin/editing/InputConnectionAdaptor.java b/shell/platform/android/io/flutter/plugin/editing/InputConnectionAdaptor.java index c613e82ff3a01..1178c343108d1 100644 --- a/shell/platform/android/io/flutter/plugin/editing/InputConnectionAdaptor.java +++ b/shell/platform/android/io/flutter/plugin/editing/InputConnectionAdaptor.java @@ -5,8 +5,11 @@ package io.flutter.plugin.editing; import android.content.Context; +import android.text.DynamicLayout; import android.text.Editable; +import android.text.Layout; import android.text.Selection; +import android.text.TextPaint; import android.view.KeyEvent; import android.view.View; import android.view.inputmethod.BaseInputConnection; @@ -24,6 +27,7 @@ class InputConnectionAdaptor extends BaseInputConnection { private final Editable mEditable; private int mBatchCount; private InputMethodManager mImm; + private final Layout mLayout; private static final MethodChannel.Result logger = new ErrorLogResult("FlutterTextInput"); @@ -40,6 +44,7 @@ public InputConnectionAdaptor( this.textInputChannel = textInputChannel; mEditable = editable; mBatchCount = 0; + mLayout = DynamicLayout.Builder.obtain(mEditable, new TextPaint(), Integer.MAX_VALUE).build(); mImm = (InputMethodManager) view.getContext().getSystemService(Context.INPUT_METHOD_SERVICE); } @@ -144,7 +149,8 @@ public boolean sendKeyEvent(KeyEvent event) { return true; } else if (selStart > 0) { // Delete to the left of the cursor. - int newSel = Math.max(selStart - 1, 0); + Selection.extendLeft(mEditable, mLayout); + int newSel = Selection.getSelectionEnd(mEditable); Selection.setSelection(mEditable, newSel); mEditable.delete(newSel, selStart); updateEditingState(); From 9d6e41c218b5f6642eb5f40a0848fe04b282624f Mon Sep 17 00:00:00 2001 From: garyqian Date: Wed, 15 May 2019 09:47:44 +0800 Subject: [PATCH 2/2] Docs, use deperecated API for backwards compat --- .../io/flutter/plugin/editing/InputConnectionAdaptor.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/shell/platform/android/io/flutter/plugin/editing/InputConnectionAdaptor.java b/shell/platform/android/io/flutter/plugin/editing/InputConnectionAdaptor.java index 1178c343108d1..02010b13c467b 100644 --- a/shell/platform/android/io/flutter/plugin/editing/InputConnectionAdaptor.java +++ b/shell/platform/android/io/flutter/plugin/editing/InputConnectionAdaptor.java @@ -32,6 +32,7 @@ class InputConnectionAdaptor extends BaseInputConnection { private static final MethodChannel.Result logger = new ErrorLogResult("FlutterTextInput"); + @SuppressWarnings("deprecation") public InputConnectionAdaptor( View view, int client, @@ -44,7 +45,9 @@ public InputConnectionAdaptor( this.textInputChannel = textInputChannel; mEditable = editable; mBatchCount = 0; - mLayout = DynamicLayout.Builder.obtain(mEditable, new TextPaint(), Integer.MAX_VALUE).build(); + // We create a dummy Layout with max width so that the selection + // shifting acts as if all text were in one line. + mLayout = new DynamicLayout(mEditable, new TextPaint(), Integer.MAX_VALUE, Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false); mImm = (InputMethodManager) view.getContext().getSystemService(Context.INPUT_METHOD_SERVICE); }