Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Android] Fix mEditable NullPointerException in TextInputPlugin #30145

Merged
merged 1 commit into from Dec 9, 2021

Conversation

eggfly
Copy link
Member

@eggfly eggfly commented Dec 5, 2021

@flutter-dashboard
Copy link

It looks like this pull request may not have tests. Please make sure to add tests before merging. If you need an exemption to this rule, contact Hixie on the #hackers channel in Chat.

If you are not sure if you need tests, consider this rule of thumb: the purpose of a test is to make sure someone doesn't accidentally revert the fix. Ask yourself, is there anything in your PR that you feel it is important we not accidentally revert back to how it was before your fix?

Reviewers: Read the Tree Hygiene page and make sure this patch meets those guidelines before LGTMing.

@eggfly
Copy link
Member Author

eggfly commented Dec 7, 2021

I thought a lot about it, there are two MethodChannel invokes to Platform thread called: 'TextInput.setClient' and 'TextInput.setEditingState', the setEditingState is always called after _attach.

https://github.com/flutter/flutter/blob/ffcd32ebb68fbfe3c9788a3bf1a09d3ebe28887f/packages/flutter/lib/src/services/text_input.dart#L1495

  /// This method actually notifies the embedding of the client. It is utilized
  /// by [attach] and by [_handleTextInputInvocation] for the
  /// `TextInputClient.requestExistingInputState` method.
  void _attach(TextInputConnection connection, TextInputConfiguration configuration) {
    assert(connection != null);
    assert(connection._client != null);
    assert(configuration != null);
    assert(_debugEnsureInputActionWorksOnPlatform(configuration.inputAction));
    _channel.invokeMethod<void>(
      'TextInput.setClient',
      <dynamic>[ connection._id, configuration.toJson() ],
    );
    _currentConnection = connection;
    _currentConfiguration = configuration;
  }

https://github.com/flutter/flutter/blob/ffcd32ebb68fbfe3c9788a3bf1a09d3ebe28887f/packages/flutter/lib/src/services/text_input.dart#L1666

  void _setEditingState(TextEditingValue value) {
    assert(value != null);
    _channel.invokeMethod<void>(
      'TextInput.setEditingState',
      value.toJSON(),
    );
  }

void setTextInputClient(int client, TextInputChannel.Configuration configuration) {

  @VisibleForTesting
  void setTextInputClient(int client, TextInputChannel.Configuration configuration) {
    // Call notifyViewExited on the previous field.
    notifyViewExited();
    inputTarget = new InputTarget(InputTarget.Type.FRAMEWORK_CLIENT, client);

    if (mEditable != null) {
      mEditable.removeEditingStateListener(this);
    }
    mEditable =
        new ListenableEditingState(
            configuration.autofill != null ? configuration.autofill.editState : null, mView);   // <---- here 
    // more code ..
  }

So there's impossible to leave the mEditable member's value is null in TextInputPlugin class.

BUT UNLESS: there's another FlutterView object?

public void attachToFlutterEngine(@NonNull FlutterEngine flutterEngine) {

  public void attachToFlutterEngine(@NonNull FlutterEngine flutterEngine) {
    Log.v(TAG, "Attaching to a FlutterEngine: " + flutterEngine);
    if (isAttachedToFlutterEngine()) {
      if (flutterEngine == this.flutterEngine) {
        // We are already attached to this FlutterEngine
        Log.v(TAG, "Already attached to this engine. Doing nothing.");
        return;
      }

      // Detach from a previous FlutterEngine so we can attach to this new one.
      Log.v(
          TAG,
          "Currently attached to a different engine. Detaching and then attaching"
              + " to new engine.");
      detachFromFlutterEngine();
    }

    this.flutterEngine = flutterEngine;

    // Instruct our FlutterRenderer that we are now its designated RenderSurface.
    FlutterRenderer flutterRenderer = this.flutterEngine.getRenderer();
    isFlutterUiDisplayed = flutterRenderer.isDisplayingFlutterUi();
    renderSurface.attachToRenderer(flutterRenderer);
    flutterRenderer.addIsDisplayingFlutterUiListener(flutterUiDisplayListener);

    // Initialize various components that know how to process Android View I/O
    // in a way that Flutter understands.
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
      mouseCursorPlugin = new MouseCursorPlugin(this, this.flutterEngine.getMouseCursorChannel());
    }
    textInputPlugin = 
        new TextInputPlugin(
            this,
            this.flutterEngine.getTextInputChannel(),
            this.flutterEngine.getPlatformViewsController());   // <---- here new instance
  @VisibleForTesting
  void setTextInputClient(int client, TextInputChannel.Configuration configuration) {
    // Call notifyViewExited on the previous field.
    notifyViewExited();
    inputTarget = new InputTarget(InputTarget.Type.FRAMEWORK_CLIENT, client);

    if (mEditable != null) {
      mEditable.removeEditingStateListener(this);
    }
    mEditable =
        new ListenableEditingState(
            configuration.autofill != null ? configuration.autofill.editState : null, mView);
    this.configuration = configuration;
    updateAutofillConfigurationIfNeeded(configuration);

    // setTextInputClient will be followed by a call to setTextInputEditingState.
    // Do a restartInput at that time.
    mRestartInputPending = true;
    unlockPlatformViewInputConnection();
    lastClientRect = null;
    mEditable.addEditingStateListener(this);
  }

If we try to attach an old engine (reuse engine) to a new FlutterView instance and call setTextInputEditingState or call clearTextInputClient, the mEditable maybe null. It will crash.

  • So I think we can pre-create a mEditable default object in the constructor of TextInputPlugin, give it a default client_id and mView. @LongCatIsLooong @blasten

@eggfly eggfly changed the title [Android] Fix mEditable NullPointerException in TextInputPlugin WIP: [Android] Fix mEditable NullPointerException in TextInputPlugin Dec 7, 2021
@eggfly eggfly force-pushed the fix_android_text_input_null branch 5 times, most recently from 180f819 to 895b9c9 Compare December 8, 2021 09:34
@eggfly eggfly requested a review from blasten December 8, 2021 09:40
@eggfly eggfly removed the needs tests label Dec 8, 2021
@eggfly eggfly changed the title WIP: [Android] Fix mEditable NullPointerException in TextInputPlugin [Android] Fix mEditable NullPointerException in TextInputPlugin Dec 8, 2021
Copy link

@blasten blasten left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM % nits

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla: yes platform-android waiting for tree to go green This PR is approved and tested, but waiting for the tree to be green to land.
Projects
3 participants