From e023418f3fdcfd544bc2a6041234fa5b23b835ac Mon Sep 17 00:00:00 2001 From: Jason Simmons Date: Tue, 1 Oct 2019 09:03:47 -0700 Subject: [PATCH] Check for a null input method subtype (flutter/engine#12706) Fixes https://github.com/flutter/flutter/issues/41557 --- .../io/flutter/plugin/editing/TextInputPlugin.java | 10 ++++++++-- .../plugin/editing/TextInputPluginTest.java | 14 +++++++++++++- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/engine/src/flutter/shell/platform/android/io/flutter/plugin/editing/TextInputPlugin.java b/engine/src/flutter/shell/platform/android/io/flutter/plugin/editing/TextInputPlugin.java index 36669ffdd0..9da7c58768 100644 --- a/engine/src/flutter/shell/platform/android/io/flutter/plugin/editing/TextInputPlugin.java +++ b/engine/src/flutter/shell/platform/android/io/flutter/plugin/editing/TextInputPlugin.java @@ -18,6 +18,7 @@ import android.view.inputmethod.BaseInputConnection; import android.view.inputmethod.EditorInfo; import android.view.inputmethod.InputConnection; import android.view.inputmethod.InputMethodManager; +import android.view.inputmethod.InputMethodSubtype; import io.flutter.embedding.engine.dart.DartExecutor; import io.flutter.embedding.engine.systemchannels.TextInputChannel; @@ -320,10 +321,15 @@ public class TextInputPlugin { // and stops it from trying to incorrectly combine characters. However this also has some // negative performance implications, so we don't want to apply this workaround in every case. @SuppressLint("NewApi") // New API guard is inline, the linter can't see it. + @SuppressWarnings("deprecation") private boolean isRestartAlwaysRequired() { + InputMethodSubtype subtype = mImm.getCurrentInputMethodSubtype(); + if (subtype == null) { + return false; + } String language = (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) - ? mImm.getCurrentInputMethodSubtype().getLanguageTag() - : mImm.getCurrentInputMethodSubtype().getLocale(); + ? subtype.getLanguageTag() + : subtype.getLocale(); return Build.MANUFACTURER.equals("samsung") && language.equals("ko"); } diff --git a/engine/src/flutter/shell/platform/android/test/io/flutter/plugin/editing/TextInputPluginTest.java b/engine/src/flutter/shell/platform/android/test/io/flutter/plugin/editing/TextInputPluginTest.java index dffc6a6dc0..a0598380b7 100644 --- a/engine/src/flutter/shell/platform/android/test/io/flutter/plugin/editing/TextInputPluginTest.java +++ b/engine/src/flutter/shell/platform/android/test/io/flutter/plugin/editing/TextInputPluginTest.java @@ -69,6 +69,19 @@ public class TextInputPluginTest { assertEquals(2, testImm.getRestartCount(testView)); } + @Test + public void setTextInputEditingState_nullInputMethodSubtype() { + TestImm testImm = Shadow.extract(RuntimeEnvironment.application.getSystemService(Context.INPUT_METHOD_SERVICE)); + testImm.setCurrentInputMethodSubtype(null); + + View testView = new View(RuntimeEnvironment.application); + TextInputPlugin textInputPlugin = new TextInputPlugin(testView, mock(DartExecutor.class), mock(PlatformViewsController.class)); + textInputPlugin.setTextInputClient(0, new TextInputChannel.Configuration(false, false, TextInputChannel.TextCapitalization.NONE, null, null, null)); + // There's a pending restart since we initialized the text input client. Flush that now. + textInputPlugin.setTextInputEditingState(testView, new TextInputChannel.TextEditState("", 0, 0)); + assertEquals(1, testImm.getRestartCount(testView)); + } + @Implements(InputMethodManager.class) public static class TestImm extends ShadowInputMethodManager { private InputMethodSubtype currentInputMethodSubtype; @@ -97,4 +110,3 @@ public class TextInputPluginTest { } } } -