From 15a831d358f45f5d4102be69146cf426b58f7faa Mon Sep 17 00:00:00 2001 From: Michael Klimushyn Date: Thu, 11 Jul 2019 09:47:35 -0700 Subject: [PATCH] Add hooks for InputConnection lock and unlocking (flutter/engine#9727) With this, plugins can know whether or not their input connection should be cached. In very rare cases this can be used by plugins to know how to more properly handle their own input connections, in cases where they're overriding typical input behavior themselves. --- .../flutter/plugin/platform/PlatformView.java | 23 +++++++++++++ .../platform/PlatformViewsController.java | 34 +++++++++++++------ .../platform/VirtualDisplayController.java | 14 ++++++++ 3 files changed, 60 insertions(+), 11 deletions(-) diff --git a/engine/src/flutter/shell/platform/android/io/flutter/plugin/platform/PlatformView.java b/engine/src/flutter/shell/platform/android/io/flutter/plugin/platform/PlatformView.java index 36240f0cc3..ea62ae2476 100644 --- a/engine/src/flutter/shell/platform/android/io/flutter/plugin/platform/PlatformView.java +++ b/engine/src/flutter/shell/platform/android/io/flutter/plugin/platform/PlatformView.java @@ -4,6 +4,7 @@ package io.flutter.plugin.platform; +import android.annotation.SuppressLint; import android.view.View; /** @@ -24,4 +25,26 @@ public interface PlatformView { * after this method is called. Failing to do so will result in a memory leak. */ void dispose(); + + /** + * Callback fired when the platform's input connection is locked, or should be used. See also + * {@link TextInputPlugin#lockPlatformViewInputConnection}. + * + *

This hook only exists for rare cases where the plugin relies on the state of the input + * connection. This probably doesn't need to be implemented. + */ + // Default interface methods are supported on all min SDK versions of Android. + @SuppressLint("NewApi") + default void onInputConnectionLocked() {}; + + /** + * Callback fired when the platform input connection has been unlocked. See also + * {@link TextInputPlugin#lockPlatformViewInputConnection}. + * + *

This hook only exists for rare cases where the plugin relies on the state of the input + * connection. This probably doesn't need to be implemented. + */ + // Default interface methods are supported on all min SDK versions of Android. + @SuppressLint("NewApi") + default void onInputConnectionUnlocked() {}; } diff --git a/engine/src/flutter/shell/platform/android/io/flutter/plugin/platform/PlatformViewsController.java b/engine/src/flutter/shell/platform/android/io/flutter/plugin/platform/PlatformViewsController.java index 201cfcb631..37effad458 100644 --- a/engine/src/flutter/shell/platform/android/io/flutter/plugin/platform/PlatformViewsController.java +++ b/engine/src/flutter/shell/platform/android/io/flutter/plugin/platform/PlatformViewsController.java @@ -148,7 +148,7 @@ public class PlatformViewsController implements PlatformViewsAccessibilityDelega public void resizePlatformView(@NonNull PlatformViewsChannel.PlatformViewResizeRequest request, @NonNull Runnable onComplete) { ensureValidAndroidVersion(); - VirtualDisplayController vdController = vdControllers.get(request.viewId); + final VirtualDisplayController vdController = vdControllers.get(request.viewId); if (vdController == null) { throw new IllegalStateException("Trying to resize a platform view with unknown id: " + request.viewId); @@ -158,22 +158,18 @@ public class PlatformViewsController implements PlatformViewsAccessibilityDelega int physicalHeight = toPhysicalPixels(request.newLogicalHeight); validateVirtualDisplayDimensions(physicalWidth, physicalHeight); - if (textInputPlugin != null) { - // Resizing involved moving the platform view to a new virtual display. - // Doing so potentially results in losing an active input connection. - // To make sure we preserve the input connection when resizing we lock it here - // and unlock after the resize is complete. - textInputPlugin.lockPlatformViewInputConnection(); - } + // Resizing involved moving the platform view to a new virtual display. Doing so + // potentially results in losing an active input connection. To make sure we preserve + // the input connection when resizing we lock it here and unlock after the resize is + // complete. + lockInputConnection(vdController); vdController.resize( physicalWidth, physicalHeight, new Runnable() { @Override public void run() { - if (textInputPlugin != null) { - textInputPlugin.unlockPlatformViewInputConnection(); - } + unlockInputConnection(vdController); onComplete.run(); } } @@ -366,6 +362,22 @@ public class PlatformViewsController implements PlatformViewsAccessibilityDelega return controller.getView(); } + private void lockInputConnection(@NonNull VirtualDisplayController controller) { + if (textInputPlugin == null) { + return; + } + textInputPlugin.lockPlatformViewInputConnection(); + controller.onInputConnectionLocked(); + } + + private void unlockInputConnection(@NonNull VirtualDisplayController controller) { + if (textInputPlugin == null) { + return; + } + textInputPlugin.unlockPlatformViewInputConnection(); + controller.onInputConnectionUnlocked(); + } + private static boolean validateDirection(int direction) { return direction == View.LAYOUT_DIRECTION_LTR || direction == View.LAYOUT_DIRECTION_RTL; } diff --git a/engine/src/flutter/shell/platform/android/io/flutter/plugin/platform/VirtualDisplayController.java b/engine/src/flutter/shell/platform/android/io/flutter/plugin/platform/VirtualDisplayController.java index 54ffc1ff3b..5d1f3155ff 100644 --- a/engine/src/flutter/shell/platform/android/io/flutter/plugin/platform/VirtualDisplayController.java +++ b/engine/src/flutter/shell/platform/android/io/flutter/plugin/platform/VirtualDisplayController.java @@ -159,6 +159,20 @@ class VirtualDisplayController { textureEntry.release(); } + /*package*/ void onInputConnectionLocked() { + if (presentation == null || presentation.getView() == null) { + return; + } + presentation.getView().onInputConnectionLocked(); + } + + /*package*/ void onInputConnectionUnlocked() { + if (presentation == null || presentation.getView() == null) { + return; + } + presentation.getView().onInputConnectionUnlocked(); + } + public View getView() { if (presentation == null) return null;