From 824956cbd644006e705594e8029dddb0ff0f620b Mon Sep 17 00:00:00 2001 From: eggfly Date: Fri, 7 Jan 2022 16:22:00 +0800 Subject: [PATCH] Fix the FlutterView casting, add a NonNull and fix code style --- .../embedding/android/FlutterView.java | 2 +- .../flutter/plugin/platform/PlatformView.java | 4 +- .../PlatformViewsAccessibilityDelegate.java | 2 +- .../platform/PlatformViewsController.java | 41 ++++++++----------- .../platform/SingleViewPresentation.java | 3 +- .../platform/VirtualDisplayController.java | 2 +- .../io/flutter/view/AccessibilityBridge.java | 31 +++----------- .../platform/PlatformViewsControllerTest.java | 2 +- 8 files changed, 30 insertions(+), 57 deletions(-) diff --git a/shell/platform/android/io/flutter/embedding/android/FlutterView.java b/shell/platform/android/io/flutter/embedding/android/FlutterView.java index aa78142ebb3d3..6e858204d9dd4 100644 --- a/shell/platform/android/io/flutter/embedding/android/FlutterView.java +++ b/shell/platform/android/io/flutter/embedding/android/FlutterView.java @@ -1193,7 +1193,7 @@ public void detachFromFlutterEngine() { flutterEngine.getPlatformViewsController().detachFromView(); // Disconnect the FlutterEngine's PlatformViewsController from the AccessibilityBridge. - flutterEngine.getPlatformViewsController().detachAccessibiltyBridge(); + flutterEngine.getPlatformViewsController().detachAccessibilityBridge(); // Disconnect and clean up the AccessibilityBridge. accessibilityBridge.release(); diff --git a/shell/platform/android/io/flutter/plugin/platform/PlatformView.java b/shell/platform/android/io/flutter/plugin/platform/PlatformView.java index 0de5c2651b98e..92f034d840d11 100644 --- a/shell/platform/android/io/flutter/plugin/platform/PlatformView.java +++ b/shell/platform/android/io/flutter/plugin/platform/PlatformView.java @@ -68,7 +68,7 @@ default void onFlutterViewDetached() {} */ // Default interface methods are supported on all min SDK versions of Android. @SuppressLint("NewApi") - default void onInputConnectionLocked() {}; + default void onInputConnectionLocked() {} /** * Callback fired when the platform input connection has been unlocked. See also {@link @@ -79,5 +79,5 @@ default void onFlutterViewDetached() {} */ // Default interface methods are supported on all min SDK versions of Android. @SuppressLint("NewApi") - default void onInputConnectionUnlocked() {}; + default void onInputConnectionUnlocked() {} } diff --git a/shell/platform/android/io/flutter/plugin/platform/PlatformViewsAccessibilityDelegate.java b/shell/platform/android/io/flutter/plugin/platform/PlatformViewsAccessibilityDelegate.java index 503e9c8d661e7..ad693a8724880 100644 --- a/shell/platform/android/io/flutter/plugin/platform/PlatformViewsAccessibilityDelegate.java +++ b/shell/platform/android/io/flutter/plugin/platform/PlatformViewsAccessibilityDelegate.java @@ -32,5 +32,5 @@ public interface PlatformViewsAccessibilityDelegate { *

Any accessibility events sent by platform views belonging to this delegate will be ignored * until a new accessibility bridge is attached. */ - void detachAccessibiltyBridge(); + void detachAccessibilityBridge(); } diff --git a/shell/platform/android/io/flutter/plugin/platform/PlatformViewsController.java b/shell/platform/android/io/flutter/plugin/platform/PlatformViewsController.java index 2e9a9c2e54c26..54916f31e9784 100644 --- a/shell/platform/android/io/flutter/plugin/platform/PlatformViewsController.java +++ b/shell/platform/android/io/flutter/plugin/platform/PlatformViewsController.java @@ -41,8 +41,9 @@ /** * Manages platform views. * - *

Each {@link io.flutter.app.FlutterPluginRegistry} has a single platform views controller. A - * platform views controller can be attached to at most one Flutter view. + *

Each {@link io.flutter.embedding.engine.FlutterEngine} or {@link + * io.flutter.app.FlutterPluginRegistry} has a single platform views controller. A platform views + * controller can be attached to at most one Flutter view. */ public class PlatformViewsController implements PlatformViewsAccessibilityDelegate { private static final String TAG = "PlatformViewsController"; @@ -55,8 +56,7 @@ public class PlatformViewsController implements PlatformViewsAccessibilityDelega private Context context; // The View currently rendering the Flutter UI associated with these platform views. - // TODO(egarciad): Investigate if this can be downcasted to `FlutterView`. - private View flutterView; + private FlutterView flutterView; // The texture registry maintaining the textures into which the embedded views will be rendered. @Nullable private TextureRegistry textureRegistry; @@ -111,10 +111,10 @@ public class PlatformViewsController implements PlatformViewsAccessibilityDelega private boolean synchronizeToNativeViewHierarchy = true; // Overlay layer IDs that were displayed since the start of the current frame. - private HashSet currentFrameUsedOverlayLayerIds; + private final HashSet currentFrameUsedOverlayLayerIds; // Platform view IDs that were displayed since the start of the current frame. - private HashSet currentFrameUsedPlatformViewIds; + private final HashSet currentFrameUsedPlatformViewIds; // Used to acquire the original motion events using the motionEventIds. private final MotionEventTracker motionEventTracker; @@ -290,12 +290,9 @@ public void resizePlatformView( vdController.resize( physicalWidth, physicalHeight, - new Runnable() { - @Override - public void run() { - unlockInputConnection(vdController); - onComplete.run(); - } + () -> { + unlockInputConnection(vdController); + onComplete.run(); }); } @@ -483,7 +480,7 @@ public void detach() { * This {@code PlatformViewsController} and its {@code FlutterEngine} is now attached to an * Android {@code View} that renders a Flutter UI. */ - public void attachToView(@NonNull View flutterView) { + public void attachToView(@NonNull FlutterView flutterView) { this.flutterView = flutterView; // Inform all existing platform views that they are now associated with @@ -518,7 +515,7 @@ public void attachAccessibilityBridge(AccessibilityBridge accessibilityBridge) { } @Override - public void detachAccessibiltyBridge() { + public void detachAccessibilityBridge() { accessibilityEventsDelegate.setAccessibilityBridge(null); } @@ -720,7 +717,7 @@ private void flushAllViews() { private void initializeRootImageViewIfNeeded() { if (synchronizeToNativeViewHierarchy && !flutterViewConvertedToImageView) { - ((FlutterView) flutterView).convertToImageView(); + flutterView.convertToImageView(); flutterViewConvertedToImageView = true; } } @@ -764,7 +761,7 @@ void initializePlatformViewIfNeeded(int viewId) { platformViewParent.put(viewId, parentView); parentView.addView(platformView.getView()); - ((FlutterView) flutterView).addView(parentView); + flutterView.addView(parentView); } public void attachToFlutterRenderer(FlutterRenderer flutterRenderer) { @@ -829,7 +826,7 @@ public void onDisplayOverlaySurface(int id, int x, int y, int width, int height) final FlutterImageView overlayView = overlayLayerViews.get(id); if (overlayView.getParent() == null) { - ((FlutterView) flutterView).addView(overlayView); + flutterView.addView(overlayView); } FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams((int) width, (int) height); @@ -852,14 +849,13 @@ public void onBeginFrame() { *

This member is not intended for public use, and is only visible for testing. */ public void onEndFrame() { - final FlutterView view = (FlutterView) flutterView; // If there are no platform views in the current frame, // then revert the image view surface and use the previous surface. // // Otherwise, acquire the latest image. if (flutterViewConvertedToImageView && currentFrameUsedPlatformViewIds.isEmpty()) { flutterViewConvertedToImageView = false; - view.revertImageView( + flutterView.revertImageView( () -> { // Destroy overlay surfaces once the surface reversion is completed. finishFrame(false); @@ -876,7 +872,7 @@ public void onEndFrame() { // dropped. // For example, a toolbar widget painted by Flutter may not be rendered. final boolean isFrameRenderedUsingImageReaders = - flutterViewConvertedToImageView && view.acquireLatestImageViewFrame(); + flutterViewConvertedToImageView && flutterView.acquireLatestImageViewFrame(); finishFrame(isFrameRenderedUsingImageReaders); } @@ -886,7 +882,7 @@ private void finishFrame(boolean isFrameRenderedUsingImageReaders) { final FlutterImageView overlayView = overlayLayerViews.valueAt(i); if (currentFrameUsedOverlayLayerIds.contains(overlayId)) { - ((FlutterView) flutterView).attachOverlaySurfaceToRender(overlayView); + flutterView.attachOverlaySurfaceToRender(overlayView); final boolean didAcquireOverlaySurfaceImage = overlayView.acquireLatestImage(); isFrameRenderedUsingImageReaders &= didAcquireOverlaySurfaceImage; } else { @@ -970,12 +966,11 @@ public FlutterOverlaySurface createOverlaySurface() { */ public void destroyOverlaySurfaces() { for (int i = 0; i < overlayLayerViews.size(); i++) { - int overlayId = overlayLayerViews.keyAt(i); FlutterImageView overlayView = overlayLayerViews.valueAt(i); overlayView.detachFromRenderer(); overlayView.closeImageReader(); if (flutterView != null) { - ((FlutterView) flutterView).removeView(overlayView); + flutterView.removeView(overlayView); } } overlayLayerViews.clear(); diff --git a/shell/platform/android/io/flutter/plugin/platform/SingleViewPresentation.java b/shell/platform/android/io/flutter/plugin/platform/SingleViewPresentation.java index b40ef87e5ccc7..a561f73868d86 100644 --- a/shell/platform/android/io/flutter/plugin/platform/SingleViewPresentation.java +++ b/shell/platform/android/io/flutter/plugin/platform/SingleViewPresentation.java @@ -4,7 +4,6 @@ package io.flutter.plugin.platform; -import static android.content.Context.INPUT_METHOD_SERVICE; import static android.content.Context.WINDOW_SERVICE; import static android.view.View.OnFocusChangeListener; @@ -99,7 +98,7 @@ static class PresentationState { // presentation. private FrameLayout container; - private PresentationState state; + private final PresentationState state; private boolean startFocused = false; diff --git a/shell/platform/android/io/flutter/plugin/platform/VirtualDisplayController.java b/shell/platform/android/io/flutter/plugin/platform/VirtualDisplayController.java index ec037ee680176..fec53e89a6d9b 100644 --- a/shell/platform/android/io/flutter/plugin/platform/VirtualDisplayController.java +++ b/shell/platform/android/io/flutter/plugin/platform/VirtualDisplayController.java @@ -64,7 +64,7 @@ public static VirtualDisplayController create( private final OnFocusChangeListener focusChangeListener; private VirtualDisplay virtualDisplay; @VisibleForTesting SingleViewPresentation presentation; - private Surface surface; + private final Surface surface; private VirtualDisplayController( Context context, diff --git a/shell/platform/android/io/flutter/view/AccessibilityBridge.java b/shell/platform/android/io/flutter/view/AccessibilityBridge.java index 42a2857f18f4e..62faf47ef6bc1 100644 --- a/shell/platform/android/io/flutter/view/AccessibilityBridge.java +++ b/shell/platform/android/io/flutter/view/AccessibilityBridge.java @@ -137,8 +137,7 @@ public class AccessibilityBridge extends AccessibilityNodeProvider { @NonNull private final AccessibilityViewEmbedder accessibilityViewEmbedder; // The delegate for interacting with embedded platform views. Used to embed accessibility data for - // an embedded - // view in the accessibility tree. + // an embedded view in the accessibility tree. @NonNull private final PlatformViewsAccessibilityDelegate platformViewsAccessibilityDelegate; // Android's {@link ContentResolver}, which is used to observe the global @@ -386,11 +385,7 @@ public AccessibilityBridge( @NonNull AccessibilityChannel accessibilityChannel, @NonNull AccessibilityManager accessibilityManager, @NonNull ContentResolver contentResolver, - // This should be @NonNull once the plumbing for - // io.flutter.embedding.engine.android.FlutterView is done. - // TODO(mattcarrol): Add the annotation once the plumbing is done. - // https://github.com/flutter/flutter/issues/29618 - PlatformViewsAccessibilityDelegate platformViewsAccessibilityDelegate) { + @NonNull PlatformViewsAccessibilityDelegate platformViewsAccessibilityDelegate) { this( rootAccessibilityView, accessibilityChannel, @@ -407,11 +402,7 @@ public AccessibilityBridge( @NonNull AccessibilityManager accessibilityManager, @NonNull ContentResolver contentResolver, @NonNull AccessibilityViewEmbedder accessibilityViewEmbedder, - // This should be @NonNull once the plumbing for - // io.flutter.embedding.engine.android.FlutterView is done. - // TODO(mattcarrol): Add the annotation once the plumbing is done. - // https://github.com/flutter/flutter/issues/29618 - PlatformViewsAccessibilityDelegate platformViewsAccessibilityDelegate) { + @NonNull PlatformViewsAccessibilityDelegate platformViewsAccessibilityDelegate) { this.rootAccessibilityView = rootAccessibilityView; this.accessibilityChannel = accessibilityChannel; this.accessibilityManager = accessibilityManager; @@ -464,13 +455,7 @@ public void onTouchExplorationStateChanged(boolean isTouchExplorationEnabled) { this.contentResolver.registerContentObserver(transitionUri, false, animationScaleObserver); } - // platformViewsAccessibilityDelegate should be @NonNull once the plumbing - // for io.flutter.embedding.engine.android.FlutterView is done. - // TODO(mattcarrol): Remove the null check once the plumbing is done. - // https://github.com/flutter/flutter/issues/29618 - if (platformViewsAccessibilityDelegate != null) { - platformViewsAccessibilityDelegate.attachAccessibilityBridge(this); - } + platformViewsAccessibilityDelegate.attachAccessibilityBridge(this); } /** @@ -482,13 +467,7 @@ public void onTouchExplorationStateChanged(boolean isTouchExplorationEnabled) { */ public void release() { isReleased = true; - // platformViewsAccessibilityDelegate should be @NonNull once the plumbing - // for io.flutter.embedding.engine.android.FlutterView is done. - // TODO(mattcarrol): Remove the null check once the plumbing is done. - // https://github.com/flutter/flutter/issues/29618 - if (platformViewsAccessibilityDelegate != null) { - platformViewsAccessibilityDelegate.detachAccessibiltyBridge(); - } + platformViewsAccessibilityDelegate.detachAccessibilityBridge(); setOnAccessibilityChangeListener(null); accessibilityManager.removeAccessibilityStateChangeListener(accessibilityStateChangeListener); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { diff --git a/shell/platform/android/test/io/flutter/plugin/platform/PlatformViewsControllerTest.java b/shell/platform/android/test/io/flutter/plugin/platform/PlatformViewsControllerTest.java index 346807172073d..0c030989d9475 100644 --- a/shell/platform/android/test/io/flutter/plugin/platform/PlatformViewsControllerTest.java +++ b/shell/platform/android/test/io/flutter/plugin/platform/PlatformViewsControllerTest.java @@ -59,7 +59,7 @@ public class PlatformViewsControllerTest { public void itNotifiesVirtualDisplayControllersOfViewAttachmentAndDetachment() { // Setup test structure. // Create a fake View that represents the View that renders a Flutter UI. - View fakeFlutterView = new View(RuntimeEnvironment.systemContext); + FlutterView fakeFlutterView = new FlutterView(RuntimeEnvironment.systemContext); // Create fake VirtualDisplayControllers. This requires internal knowledge of // PlatformViewsController. We know that all PlatformViewsController does is