From 11befe419e3c3bcb83c52de1a6570ec6ef10d8dc Mon Sep 17 00:00:00 2001 From: Jason Simmons Date: Mon, 21 Sep 2020 17:42:02 -0700 Subject: [PATCH] Disconnect the view's AndroidKeyProcessor when detaching from the engine (#21307) --- .../embedding/android/AndroidKeyProcessor.java | 9 +++++++++ .../flutter/embedding/android/FlutterView.java | 2 ++ .../android/AndroidKeyProcessorTest.java | 18 ++++++++++++++++++ 3 files changed, 29 insertions(+) diff --git a/shell/platform/android/io/flutter/embedding/android/AndroidKeyProcessor.java b/shell/platform/android/io/flutter/embedding/android/AndroidKeyProcessor.java index 094b8ef1c53..14d7effe56c 100644 --- a/shell/platform/android/io/flutter/embedding/android/AndroidKeyProcessor.java +++ b/shell/platform/android/io/flutter/embedding/android/AndroidKeyProcessor.java @@ -70,6 +70,15 @@ public class AndroidKeyProcessor { this.keyEventChannel.setEventResponseHandler(eventResponder); } + /** + * Detaches the key processor from the Flutter engine. + * + *

The AndroidKeyProcessor instance should not be used after calling this. + */ + public void destroy() { + keyEventChannel.setEventResponseHandler(null); + } + /** * Called when a key up event is received by the {@link FlutterView}. * diff --git a/shell/platform/android/io/flutter/embedding/android/FlutterView.java b/shell/platform/android/io/flutter/embedding/android/FlutterView.java index 2f1942034a7..c5c92a05d9d 100644 --- a/shell/platform/android/io/flutter/embedding/android/FlutterView.java +++ b/shell/platform/android/io/flutter/embedding/android/FlutterView.java @@ -992,6 +992,8 @@ public class FlutterView extends FrameLayout implements MouseCursorPlugin.MouseC textInputPlugin.getInputMethodManager().restartInput(this); textInputPlugin.destroy(); + androidKeyProcessor.destroy(); + if (mouseCursorPlugin != null) { mouseCursorPlugin.destroy(); } diff --git a/shell/platform/android/test/io/flutter/embedding/android/AndroidKeyProcessorTest.java b/shell/platform/android/test/io/flutter/embedding/android/AndroidKeyProcessorTest.java index 23369a2d310..8eddb009dc8 100644 --- a/shell/platform/android/test/io/flutter/embedding/android/AndroidKeyProcessorTest.java +++ b/shell/platform/android/test/io/flutter/embedding/android/AndroidKeyProcessorTest.java @@ -2,7 +2,9 @@ package io.flutter.embedding.android; import static junit.framework.TestCase.assertEquals; import static org.mockito.Mockito.any; +import static org.mockito.Mockito.isNull; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.notNull; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; @@ -56,6 +58,22 @@ public class AndroidKeyProcessorTest { verify(fakeView, times(0)).dispatchKeyEvent(any(KeyEvent.class)); } + @Test + public void destroyTest() { + FlutterEngine flutterEngine = mockFlutterEngine(); + KeyEventChannel fakeKeyEventChannel = flutterEngine.getKeyEventChannel(); + View fakeView = mock(View.class); + + AndroidKeyProcessor processor = + new AndroidKeyProcessor(fakeView, fakeKeyEventChannel, mock(TextInputPlugin.class)); + + verify(fakeKeyEventChannel, times(1)) + .setEventResponseHandler(notNull(KeyEventChannel.EventResponseHandler.class)); + processor.destroy(); + verify(fakeKeyEventChannel, times(1)) + .setEventResponseHandler(isNull(KeyEventChannel.EventResponseHandler.class)); + } + public void synthesizesEventsWhenKeyDownNotHandled() { FlutterEngine flutterEngine = mockFlutterEngine(); KeyEventChannel fakeKeyEventChannel = flutterEngine.getKeyEventChannel();