[flutter_releases] Flutter 1.23.0-18.1.pre Engine Cherrypicks (#21838)

* Update 1.23 engine to use Dart 2.11.0-213.1.beta

* Call PlatformView.dispose when removing hybrid composition platform views (#21790)

Also force disposal of all hybrid platform views when shutting down the
engine.

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

Co-authored-by: Jason Simmons <jason-simmons@users.noreply.github.com>
diff --git a/DEPS b/DEPS
index cf8f5fe..fa51088 100644
--- a/DEPS
+++ b/DEPS
@@ -34,7 +34,7 @@
   # Dart is: https://github.com/dart-lang/sdk/blob/master/DEPS.
   # You can use //tools/dart/create_updated_flutter_deps.py to produce
   # updated revision list of existing dependencies.
-  'dart_revision': 'e256855d07ba9cad5048f012f38a05446ca2fe09',
+  'dart_revision': '7cdfa7be71c9530e2ce12a095df1775aa0b9ab63',
 
   # WARNING: DO NOT EDIT MANUALLY
   # The lines between blank lines above and below are generated by a script. See create_updated_flutter_deps.py
diff --git a/shell/platform/android/io/flutter/plugin/platform/PlatformViewsController.java b/shell/platform/android/io/flutter/plugin/platform/PlatformViewsController.java
index 05a6240..b000a90 100644
--- a/shell/platform/android/io/flutter/plugin/platform/PlatformViewsController.java
+++ b/shell/platform/android/io/flutter/plugin/platform/PlatformViewsController.java
@@ -14,6 +14,7 @@
 import android.util.SparseArray;
 import android.view.MotionEvent;
 import android.view.View;
+import android.view.ViewGroup;
 import android.widget.FrameLayout;
 import androidx.annotation.NonNull;
 import androidx.annotation.Nullable;
@@ -83,7 +84,7 @@
   // The views returned by `PlatformView#getView()`.
   //
   // This only applies to hybrid composition.
-  private final SparseArray<View> platformViews;
+  private final SparseArray<PlatformView> platformViews;
 
   // The platform view parents that are appended to `FlutterView`.
   // If an entry in `platformViews` doesn't have an entry in this array, the platform view isn't
@@ -143,32 +144,24 @@
           }
 
           final PlatformView platformView = factory.create(context, request.viewId, createParams);
-          final View view = platformView.getView();
-          if (view == null) {
-            throw new IllegalStateException(
-                "PlatformView#getView() returned null, but an Android view reference was expected.");
-          }
-          if (view.getParent() != null) {
-            throw new IllegalStateException(
-                "The Android view returned from PlatformView#getView() was already added to a parent view.");
-          }
-          platformViews.put(request.viewId, view);
+          platformViews.put(request.viewId, platformView);
         }
 
         @Override
         public void disposeAndroidViewForPlatformView(int viewId) {
           // Hybrid view.
-          final View platformView = platformViews.get(viewId);
+          final PlatformView platformView = platformViews.get(viewId);
           final FlutterMutatorView parentView = platformViewParent.get(viewId);
           if (platformView != null) {
             if (parentView != null) {
-              parentView.removeView(platformView);
+              parentView.removeView(platformView.getView());
             }
             platformViews.remove(viewId);
+            platformView.dispose();
           }
 
           if (parentView != null) {
-            ((FlutterView) flutterView).removeView(parentView);
+            ((ViewGroup) parentView.getParent()).removeView(parentView);
             platformViewParent.remove(viewId);
           }
         }
@@ -311,8 +304,10 @@
             vdControllers.get(touch.viewId).dispatchTouchEvent(event);
           } else if (platformViews.get(viewId) != null) {
             final MotionEvent event = toMotionEvent(density, touch, /*usingVirtualDiplays=*/ false);
-            View view = platformViews.get(touch.viewId);
-            view.dispatchTouchEvent(event);
+            View view = platformViews.get(touch.viewId).getView();
+            if (view != null) {
+              view.dispatchTouchEvent(event);
+            }
           } else {
             throw new IllegalStateException("Sending touch to an unknown view with id: " + viewId);
           }
@@ -580,7 +575,7 @@
   public View getPlatformViewById(Integer id) {
     // Hybrid composition.
     if (platformViews.get(id) != null) {
-      return platformViews.get(id);
+      return platformViews.get(id).getView();
     }
     VirtualDisplayController controller = vdControllers.get(id);
     if (controller == null) {
@@ -690,6 +685,10 @@
       controller.dispose();
     }
     vdControllers.clear();
+
+    while (platformViews.size() > 0) {
+      channelHandler.disposeAndroidViewForPlatformView(platformViews.keyAt(0));
+    }
   }
 
   private void initializeRootImageViewIfNeeded() {
@@ -701,19 +700,27 @@
 
   @VisibleForTesting
   void initializePlatformViewIfNeeded(int viewId) {
-    final View view = platformViews.get(viewId);
-    if (view == null) {
+    final PlatformView platformView = platformViews.get(viewId);
+    if (platformView == null) {
       throw new IllegalStateException(
           "Platform view hasn't been initialized from the platform view channel.");
     }
     if (platformViewParent.get(viewId) != null) {
       return;
     }
+    if (platformView.getView() == null) {
+      throw new IllegalStateException(
+          "PlatformView#getView() returned null, but an Android view reference was expected.");
+    }
+    if (platformView.getView().getParent() != null) {
+      throw new IllegalStateException(
+          "The Android view returned from PlatformView#getView() was already added to a parent view.");
+    }
     final FlutterMutatorView parentView =
         new FlutterMutatorView(
             context, context.getResources().getDisplayMetrics().density, androidTouchProcessor);
     platformViewParent.put(viewId, parentView);
-    parentView.addView(view);
+    parentView.addView(platformView.getView());
     ((FlutterView) flutterView).addView(parentView);
   }
 
@@ -740,9 +747,11 @@
 
     final FrameLayout.LayoutParams layoutParams =
         new FrameLayout.LayoutParams(viewWidth, viewHeight);
-    final View platformView = platformViews.get(viewId);
-    platformView.setLayoutParams(layoutParams);
-    platformView.bringToFront();
+    final View view = platformViews.get(viewId).getView();
+    if (view != null) {
+      view.setLayoutParams(layoutParams);
+      view.bringToFront();
+    }
     currentFrameUsedPlatformViewIds.add(viewId);
   }
 
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 0e585b3..6280603 100644
--- a/shell/platform/android/test/io/flutter/plugin/platform/PlatformViewsControllerTest.java
+++ b/shell/platform/android/test/io/flutter/plugin/platform/PlatformViewsControllerTest.java
@@ -29,9 +29,7 @@
 import io.flutter.embedding.engine.systemchannels.MouseCursorChannel;
 import io.flutter.embedding.engine.systemchannels.SettingsChannel;
 import io.flutter.embedding.engine.systemchannels.TextInputChannel;
-import io.flutter.plugin.common.FlutterException;
 import io.flutter.plugin.common.MethodCall;
-import io.flutter.plugin.common.StandardMessageCodec;
 import io.flutter.plugin.common.StandardMethodCodec;
 import io.flutter.plugin.localization.LocalizationPlugin;
 import java.nio.ByteBuffer;
@@ -262,7 +260,7 @@
 
     // Simulate create call from the framework.
     createPlatformView(jni, platformViewsController, platformViewId, "testType");
-    verify(platformView, times(1)).getView();
+    verify(viewFactory, times(1)).create(any(), eq(platformViewId), any());
   }
 
   @Test
@@ -286,21 +284,11 @@
     createPlatformView(jni, platformViewsController, platformViewId, "testType");
     assertEquals(ShadowFlutterJNI.getResponses().size(), 1);
 
-    final ByteBuffer responseBuffer = ShadowFlutterJNI.getResponses().get(0);
-    responseBuffer.rewind();
-
-    StandardMethodCodec methodCodec = new StandardMethodCodec(new StandardMessageCodec());
-    try {
-      methodCodec.decodeEnvelope(responseBuffer);
-    } catch (FlutterException exception) {
-      assertTrue(
-          exception
-              .getMessage()
-              .contains(
-                  "PlatformView#getView() returned null, but an Android view reference was expected."));
-      return;
-    }
-    assertFalse(true);
+    assertThrows(
+        IllegalStateException.class,
+        () -> {
+          platformViewsController.initializePlatformViewIfNeeded(platformViewId);
+        });
   }
 
   @Test
@@ -326,21 +314,11 @@
     createPlatformView(jni, platformViewsController, platformViewId, "testType");
     assertEquals(ShadowFlutterJNI.getResponses().size(), 1);
 
-    final ByteBuffer responseBuffer = ShadowFlutterJNI.getResponses().get(0);
-    responseBuffer.rewind();
-
-    StandardMethodCodec methodCodec = new StandardMethodCodec(new StandardMessageCodec());
-    try {
-      methodCodec.decodeEnvelope(responseBuffer);
-    } catch (FlutterException exception) {
-      assertTrue(
-          exception
-              .getMessage()
-              .contains(
-                  "The Android view returned from PlatformView#getView() was already added to a parent view."));
-      return;
-    }
-    assertFalse(true);
+    assertThrows(
+        IllegalStateException.class,
+        () -> {
+          platformViewsController.initializePlatformViewIfNeeded(platformViewId);
+        });
   }
 
   @Test
@@ -381,6 +359,7 @@
 
     assertNotNull(androidView.getParent());
     assertTrue(androidView.getParent() instanceof FlutterMutatorView);
+    verify(platformView, times(1)).dispose();
   }
 
   @Test