Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
@Keep
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
class SingleViewPresentation extends Presentation {
private static final String TAG = "PlatformViewsController";

/*
* When an embedded view is resized in Flutterverse we move the Android view to a new virtual display
Expand Down Expand Up @@ -184,10 +185,20 @@ protected void onCreate(Bundle savedInstanceState) {
MutableContextWrapper currentContext = (MutableContextWrapper) embeddedView.getContext();
currentContext.setBaseContext(baseContext);
} else {
throw new IllegalStateException(
"Unexpected platform view context. "
+ "When constructing a platform view in the factory, use the context from PlatformViewFactory#create, view id: "
+ viewId);
// In some cases, such as when using LayoutInflator, the original context
// may not be preserved. For backward compatibility with previous
// implementations of Virtual Display, which didn't validate the context,
// continue, but log a warning indicating that some functionality may not
// work as expected.
// See https://github.com/flutter/flutter/issues/110146 for context.
Log.w(
TAG,
"Unexpected platform view context for view ID "
+ viewId
+ "; some functionality may not work correctly. When constructing a platform view "
+ "in the factory, ensure that the view returned from PlatformViewFactory#create "
+ "returns the provided context from getContext(). If you are unable to associate "
+ "the view with that context, consider using Hybrid Composition instead.");
}

container.addView(embeddedView);
Expand Down
1 change: 1 addition & 0 deletions testing/scenario_app/android/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ _android_sources = [
"app/src/androidTest/java/dev/flutter/scenariosui/MemoryLeakTests.java",
"app/src/androidTest/java/dev/flutter/scenariosui/PlatformTextureUiTests.java",
"app/src/androidTest/java/dev/flutter/scenariosui/PlatformViewUiTests.java",
"app/src/androidTest/java/dev/flutter/scenariosui/PlatformViewWithSurfaceViewBadContextUiTest.java",
"app/src/androidTest/java/dev/flutter/scenariosui/PlatformViewWithSurfaceViewUiTest.java",
"app/src/androidTest/java/dev/flutter/scenariosui/PlatformViewWithTextureViewUiTest.java",
"app/src/androidTest/java/dev/flutter/scenariosui/ScreenshotUtil.java",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

package dev.flutter.scenariosui;

import android.content.Intent;
import androidx.annotation.NonNull;
import androidx.test.filters.LargeTest;
import androidx.test.rule.ActivityTestRule;
import androidx.test.runner.AndroidJUnit4;
import dev.flutter.scenarios.PlatformViewsActivity;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

@RunWith(AndroidJUnit4.class)
@LargeTest
public class PlatformViewWithSurfaceViewBadContextUiTest {
Intent intent;

@Rule @NonNull
public ActivityTestRule<PlatformViewsActivity> activityRule =
new ActivityTestRule<>(
PlatformViewsActivity.class, /*initialTouchMode=*/ false, /*launchActivity=*/ false);

private static String goldName(String suffix) {
return "PlatformViewWithSurfaceViewBadContextUiTest_" + suffix;
}

@Before
public void setUp() {
intent = new Intent(Intent.ACTION_MAIN);
// Render a texture.
intent.putExtra("use_android_view", false);
intent.putExtra("view_type", PlatformViewsActivity.SURFACE_VIEW_BAD_CONTEXT_PV);
}

@Test
public void testPlatformView() throws Exception {
intent.putExtra("scenario_name", "platform_view");
ScreenshotUtil.capture(activityRule.launchActivity(intent), goldName("testPlatformView"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,12 @@
import io.flutter.embedding.engine.FlutterEngine;

public class PlatformViewsActivity extends TestActivity {
// WARNING: These strings must all be exactly the same length to avoid
// breaking the 'create' method's manual encoding in the test. See the
// TODO(stuartmorgan) about encoding alignment in platform_view.dart
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I lost at least an hour to figuring out why my new factory wasn't working at all 😐 I didn't want to yak-shave right now since this is an important fix, and one that I want to scope-limit so that we can cherry-pick it to stable, so I just left comments so that at least the problem will be obvious to the next person.

public static final String TEXT_VIEW_PV = "scenarios/textPlatformView";
public static final String SURFACE_VIEW_PV = "scenarios/surfacePlatformV";
public static final String SURFACE_VIEW_BAD_CONTEXT_PV = "scenarios/surfaceVBadCntxt";
public static final String TEXTURE_VIEW_PV = "scenarios/texturePlatformV";

@Override
Expand All @@ -23,7 +27,12 @@ public void configureFlutterEngine(@NonNull FlutterEngine flutterEngine) {
flutterEngine
.getPlatformViewsController()
.getRegistry()
.registerViewFactory(SURFACE_VIEW_PV, new SurfacePlatformViewFactory());
.registerViewFactory(SURFACE_VIEW_PV, new SurfacePlatformViewFactory(true));

flutterEngine
.getPlatformViewsController()
.getRegistry()
.registerViewFactory(SURFACE_VIEW_BAD_CONTEXT_PV, new SurfacePlatformViewFactory(false));

flutterEngine
.getPlatformViewsController()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import android.annotation.TargetApi;
import android.content.Context;
import android.content.ContextWrapper;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
Expand All @@ -24,7 +25,9 @@

@TargetApi(23)
public final class SurfacePlatformViewFactory extends PlatformViewFactory {
SurfacePlatformViewFactory() {
private boolean preserveContext;

SurfacePlatformViewFactory(boolean preserveContext) {
super(
new MessageCodec<Object>() {
@Nullable
Expand All @@ -42,13 +45,19 @@ public Object decodeMessage(@Nullable ByteBuffer byteBuffer) {
return StringCodec.INSTANCE.decodeMessage(byteBuffer);
}
});
this.preserveContext = preserveContext;
}

@SuppressWarnings("unchecked")
@Override
@NonNull
public PlatformView create(@NonNull Context context, int id, @Nullable Object args) {
return new SurfacePlatformView(context);
if (preserveContext) {
return new SurfacePlatformView(context);
} else {
final Context differentContext = new ContextWrapper(context);
return new SurfacePlatformView(differentContext);
}
}

private static class SurfacePlatformView implements PlatformView {
Expand Down
5 changes: 5 additions & 0 deletions testing/scenario_app/lib/src/platform_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1144,6 +1144,11 @@ void addPlatformView(
valueString,
'width'.length,
...utf8.encode('width'),
// This is missing the 64-bit boundary alignment, making the entire
// message encoding fragile to changes before this point. Do not add new
// variable-length values such as strings before this point.
// TODO(stuartmorgan): Fix this to use the actual encoding logic,
// including alignment: https://github.com/flutter/flutter/issues/111188
valueFloat64,
..._to64(width),
valueString,
Expand Down