[Android] Fix mEditable NullPointerException in TextInputPlugin (flutter/engine#30145)
This commit is contained in:
@@ -9,6 +9,8 @@ import android.text.Selection;
|
||||
import android.text.SpannableStringBuilder;
|
||||
import android.view.View;
|
||||
import android.view.inputmethod.BaseInputConnection;
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import io.flutter.Log;
|
||||
import io.flutter.embedding.engine.systemchannels.TextInputChannel;
|
||||
import java.util.ArrayList;
|
||||
@@ -55,10 +57,11 @@ class ListenableEditingState extends SpannableStringBuilder {
|
||||
|
||||
// The View is only used for creating a dummy BaseInputConnection for setComposingRegion. The View
|
||||
// needs to have a non-null Context.
|
||||
public ListenableEditingState(TextInputChannel.TextEditState initalState, View view) {
|
||||
public ListenableEditingState(
|
||||
@Nullable TextInputChannel.TextEditState initialState, @NonNull View view) {
|
||||
super();
|
||||
if (initalState != null) {
|
||||
setEditingState(initalState);
|
||||
if (initialState != null) {
|
||||
setEditingState(initialState);
|
||||
}
|
||||
|
||||
Editable self = this;
|
||||
|
||||
@@ -44,7 +44,7 @@ public class TextInputPlugin implements ListenableEditingState.EditingStateWatch
|
||||
@NonNull private InputTarget inputTarget = new InputTarget(InputTarget.Type.NO_TARGET, 0);
|
||||
@Nullable private TextInputChannel.Configuration configuration;
|
||||
@Nullable private SparseArray<TextInputChannel.Configuration> mAutofillConfigurations;
|
||||
@Nullable private ListenableEditingState mEditable;
|
||||
@NonNull private ListenableEditingState mEditable;
|
||||
private boolean mRestartInputPending;
|
||||
@Nullable private InputConnection lastInputConnection;
|
||||
@NonNull private PlatformViewsController platformViewsController;
|
||||
@@ -66,6 +66,8 @@ public class TextInputPlugin implements ListenableEditingState.EditingStateWatch
|
||||
@NonNull TextInputChannel textInputChannel,
|
||||
@NonNull PlatformViewsController platformViewsController) {
|
||||
mView = view;
|
||||
// Create a default object.
|
||||
mEditable = new ListenableEditingState(null, mView);
|
||||
mImm = (InputMethodManager) view.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||
afm = view.getContext().getSystemService(AutofillManager.class);
|
||||
@@ -218,9 +220,7 @@ public class TextInputPlugin implements ListenableEditingState.EditingStateWatch
|
||||
platformViewsController.detachTextInputPlugin();
|
||||
textInputChannel.setTextInputMethodHandler(null);
|
||||
notifyViewExited();
|
||||
if (mEditable != null) {
|
||||
mEditable.removeEditingStateListener(this);
|
||||
}
|
||||
mEditable.removeEditingStateListener(this);
|
||||
if (imeSyncCallback != null) {
|
||||
imeSyncCallback.remove();
|
||||
}
|
||||
@@ -415,9 +415,7 @@ public class TextInputPlugin implements ListenableEditingState.EditingStateWatch
|
||||
inputTarget = new InputTarget(InputTarget.Type.NO_TARGET, client);
|
||||
}
|
||||
|
||||
if (mEditable != null) {
|
||||
mEditable.removeEditingStateListener(this);
|
||||
}
|
||||
mEditable.removeEditingStateListener(this);
|
||||
mEditable =
|
||||
new ListenableEditingState(
|
||||
configuration.autofill != null ? configuration.autofill.editState : null, mView);
|
||||
|
||||
@@ -183,6 +183,25 @@ public class TextInputPluginTest {
|
||||
.updateEditingState(anyInt(), any(), anyInt(), anyInt(), anyInt(), anyInt());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setTextInputEditingState_willNotThrowWithoutSetTextInputClient() {
|
||||
// Initialize a general TextInputPlugin.
|
||||
InputMethodSubtype inputMethodSubtype = mock(InputMethodSubtype.class);
|
||||
TestImm testImm =
|
||||
Shadow.extract(
|
||||
RuntimeEnvironment.application.getSystemService(Context.INPUT_METHOD_SERVICE));
|
||||
testImm.setCurrentInputMethodSubtype(inputMethodSubtype);
|
||||
View testView = new View(RuntimeEnvironment.application);
|
||||
TextInputChannel textInputChannel = spy(new TextInputChannel(mock(DartExecutor.class)));
|
||||
TextInputPlugin textInputPlugin =
|
||||
new TextInputPlugin(testView, textInputChannel, mock(PlatformViewsController.class));
|
||||
|
||||
// Here's no textInputPlugin.setTextInputClient()
|
||||
textInputPlugin.setTextInputEditingState(
|
||||
testView, new TextInputChannel.TextEditState("initial input from framework", 0, 0, -1, -1));
|
||||
assertTrue(textInputPlugin.getEditable().toString().equals("initial input from framework"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setTextInputEditingState_doesNotInvokeUpdateEditingStateWithDeltas() {
|
||||
// Initialize a general TextInputPlugin.
|
||||
|
||||
Reference in New Issue
Block a user