[Android] Fix KeyEmbedderResponder throws a NullPointerException (flutter/engine#49860)

## Description

This PR fixes a `NullPointerException` thrown from `KeyEmbedderResponder` when pressing a key during a hot restart (at this time the framework might not be ready and null reply is sent back).

## Related Issue

Fixes https://github.com/flutter/flutter/issues/141662.

## Tests

Adds 1 test.
This commit is contained in:
Bruno Leroux
2024-01-19 10:17:06 +01:00
committed by GitHub
parent f8b699a88b
commit 7001e5cc63
2 changed files with 42 additions and 5 deletions

View File

@@ -8,6 +8,7 @@ import android.view.InputDevice;
import android.view.KeyEvent;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import io.flutter.Log;
import io.flutter.embedding.android.KeyboardMap.PressingGoal;
import io.flutter.embedding.android.KeyboardMap.TogglingGoal;
import io.flutter.plugin.common.BinaryMessenger;
@@ -398,9 +399,13 @@ public class KeyEmbedderResponder implements KeyboardManager.Responder {
? null
: message -> {
Boolean handled = false;
message.rewind();
if (message.capacity() != 0) {
handled = message.get() != 0;
if (message != null) {
message.rewind();
if (message.capacity() != 0) {
handled = message.get() != 0;
}
} else {
Log.w(TAG, "A null reply was received when sending a key event to the framework.");
}
onKeyEventHandledCallback.onKeyEventHandled(handled);
};

View File

@@ -541,7 +541,7 @@ public class KeyboardManagerTest {
}
@Test
public void channelReponderHandlesEvents() {
public void channelResponderHandlesEvents() {
final KeyboardTester tester = new KeyboardTester();
final KeyEvent keyEvent = new FakeKeyEvent(ACTION_DOWN, 65);
final ArrayList<CallRecord> calls = new ArrayList<>();
@@ -567,7 +567,7 @@ public class KeyboardManagerTest {
}
@Test
public void embedderReponderHandlesEvents() {
public void embedderResponderHandlesEvents() {
final KeyboardTester tester = new KeyboardTester();
final KeyEvent keyEvent = new FakeKeyEvent(ACTION_DOWN, SCAN_KEY_A, KEYCODE_A, 0, 'a', 0);
final ArrayList<CallRecord> calls = new ArrayList<>();
@@ -599,6 +599,38 @@ public class KeyboardManagerTest {
verify(tester.mockView, times(0)).redispatch(any(KeyEvent.class));
}
@Test
public void embedderResponderHandlesNullReply() {
// Regression test for https://github.com/flutter/flutter/issues/141662.
final BinaryMessenger mockMessenger = mock(BinaryMessenger.class);
doAnswer(
invocation -> {
final BinaryMessenger.BinaryReply reply = invocation.getArgument(2);
// Simulate a null reply.
// In release mode, a null reply might happen when the engine sends a message
// before the framework has started.
reply.reply(null);
return null;
})
.when(mockMessenger)
.send(any(String.class), any(ByteBuffer.class), any(BinaryMessenger.BinaryReply.class));
final KeyboardManager.ViewDelegate mockView = mock(KeyboardManager.ViewDelegate.class);
doAnswer(invocation -> mockMessenger).when(mockView).getBinaryMessenger();
final KeyboardManager keyboardManager = new KeyboardManager(mockView);
final KeyEvent keyEvent = new FakeKeyEvent(ACTION_DOWN, SCAN_KEY_A, KEYCODE_A, 0, 'a', 0);
boolean exceptionThrown = false;
try {
final boolean result = keyboardManager.handleEvent(keyEvent);
} catch (Exception exception) {
exceptionThrown = true;
}
assertEquals(false, exceptionThrown);
}
@Test
public void bothRespondersHandlesEvents() {
final KeyboardTester tester = new KeyboardTester();