Fix: support to set layout direction for hybrid composition platform view (flutter/engine#30611)
This commit is contained in:
@@ -122,6 +122,7 @@ public class PlatformViewsController implements PlatformViewsAccessibilityDelega
|
||||
private final PlatformViewsChannel.PlatformViewsHandler channelHandler =
|
||||
new PlatformViewsChannel.PlatformViewsHandler() {
|
||||
|
||||
@TargetApi(Build.VERSION_CODES.KITKAT)
|
||||
@Override
|
||||
public void createAndroidViewForPlatformView(
|
||||
@NonNull PlatformViewsChannel.PlatformViewCreationRequest request) {
|
||||
@@ -149,6 +150,7 @@ public class PlatformViewsController implements PlatformViewsAccessibilityDelega
|
||||
}
|
||||
|
||||
final PlatformView platformView = factory.create(context, request.viewId, createParams);
|
||||
platformView.getView().setLayoutDirection(request.direction);
|
||||
platformViews.put(request.viewId, platformView);
|
||||
}
|
||||
|
||||
@@ -328,13 +330,20 @@ public class PlatformViewsController implements PlatformViewsAccessibilityDelega
|
||||
}
|
||||
|
||||
ensureValidAndroidVersion(Build.VERSION_CODES.KITKAT_WATCH);
|
||||
View view = vdControllers.get(viewId).getView();
|
||||
if (view == null) {
|
||||
throw new IllegalStateException(
|
||||
"Sending touch to an unknown view with id: " + direction);
|
||||
final PlatformView platformView = platformViews.get(viewId);
|
||||
if (platformView != null) {
|
||||
platformView.getView().setLayoutDirection(direction);
|
||||
return;
|
||||
}
|
||||
|
||||
view.setLayoutDirection(direction);
|
||||
VirtualDisplayController controller = vdControllers.get(viewId);
|
||||
if (controller == null) {
|
||||
throw new IllegalStateException(
|
||||
"Trying to set direction: "
|
||||
+ direction
|
||||
+ " to an unknown platform view with id: "
|
||||
+ viewId);
|
||||
}
|
||||
controller.getView().setLayoutDirection(direction);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -387,6 +387,40 @@ public class PlatformViewsControllerTest {
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
@Config(shadows = {ShadowFlutterJNI.class, ShadowPlatformTaskQueue.class})
|
||||
public void setPlatformViewDirection__throwIfPlatformViewNotFound() {
|
||||
PlatformViewsController platformViewsController = new PlatformViewsController();
|
||||
|
||||
int platformViewId = 0;
|
||||
assertNull(platformViewsController.getPlatformViewById(platformViewId));
|
||||
|
||||
PlatformViewFactory viewFactory = mock(PlatformViewFactory.class);
|
||||
PlatformView platformView = mock(PlatformView.class);
|
||||
final View androidView = mock(View.class);
|
||||
when(platformView.getView()).thenReturn(androidView);
|
||||
when(viewFactory.create(any(), eq(platformViewId), any())).thenReturn(platformView);
|
||||
platformViewsController.getRegistry().registerViewFactory("testType", viewFactory);
|
||||
|
||||
FlutterJNI jni = new FlutterJNI();
|
||||
attach(jni, platformViewsController);
|
||||
|
||||
verify(androidView, never()).setLayoutDirection(anyInt());
|
||||
|
||||
// Simulate create call from the framework.
|
||||
createPlatformView(jni, platformViewsController, platformViewId, "testType", /* hybrid=*/ true);
|
||||
assertEquals(ShadowFlutterJNI.getResponses().size(), 1);
|
||||
|
||||
// Simulate set direction call from the framework.
|
||||
setLayoutDirection(jni, platformViewsController, platformViewId, 1);
|
||||
verify(androidView, times(1)).setLayoutDirection(1);
|
||||
|
||||
// The limit value of reply message will be equal to 2 if the layout direction is set
|
||||
// successfully, otherwise it will be much more than 2 due to the reply message contains
|
||||
// an error message wrapped with exception detail information.
|
||||
assertEquals(ShadowFlutterJNI.getResponses().get(0).limit(), 2);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Config(shadows = {ShadowFlutterJNI.class, ShadowPlatformTaskQueue.class})
|
||||
public void disposeAndroidView__hybridComposition() {
|
||||
@@ -834,6 +868,25 @@ public class PlatformViewsControllerTest {
|
||||
/*messageData=*/ 0);
|
||||
}
|
||||
|
||||
private static void setLayoutDirection(
|
||||
FlutterJNI jni,
|
||||
PlatformViewsController platformViewsController,
|
||||
int platformViewId,
|
||||
int direction) {
|
||||
final Map<String, Object> platformViewCreateArguments = new HashMap<>();
|
||||
platformViewCreateArguments.put("id", platformViewId);
|
||||
platformViewCreateArguments.put("direction", direction);
|
||||
|
||||
final MethodCall platformCreateMethodCall =
|
||||
new MethodCall("setDirection", platformViewCreateArguments);
|
||||
|
||||
jni.handlePlatformMessage(
|
||||
"flutter/platform_views",
|
||||
encodeMethodCall(platformCreateMethodCall),
|
||||
/*replyId=*/ 0,
|
||||
/*messageData=*/ 0);
|
||||
}
|
||||
|
||||
private static void disposePlatformView(
|
||||
FlutterJNI jni, PlatformViewsController platformViewsController, int platformViewId) {
|
||||
|
||||
|
||||
Reference in New Issue
Block a user