14
14
import java .io .PrintWriter ;
15
15
import java .io .StringWriter ;
16
16
import java .nio .ByteBuffer ;
17
+ import java .util .HashMap ;
17
18
import java .util .List ;
18
19
import java .util .Map ;
19
20
@@ -64,6 +65,9 @@ public void onMethodCall(@NonNull MethodCall call, @NonNull MethodChannel.Result
64
65
case "resize" :
65
66
resize (call , result );
66
67
break ;
68
+ case "offset" :
69
+ offset (call , result );
70
+ break ;
67
71
case "touch" :
68
72
touch (call , result );
69
73
break ;
@@ -82,29 +86,40 @@ public void onMethodCall(@NonNull MethodCall call, @NonNull MethodChannel.Result
82
86
}
83
87
84
88
private void create (@ NonNull MethodCall call , @ NonNull MethodChannel .Result result ) {
85
- Map <String , Object > createArgs = call .arguments ();
86
- boolean usesHybridComposition =
89
+ final Map <String , Object > createArgs = call .arguments ();
90
+ // TODO(egarciad): Remove the "hybrid" case.
91
+ final boolean usesPlatformViewLayer =
87
92
createArgs .containsKey ("hybrid" ) && (boolean ) createArgs .get ("hybrid" );
88
- // In hybrid mode, the size of the view is determined by the size of the Flow layer.
89
- double width = (usesHybridComposition ) ? 0 : (double ) createArgs .get ("width" );
90
- double height = (usesHybridComposition ) ? 0 : (double ) createArgs .get ("height" );
91
-
92
- PlatformViewCreationRequest request =
93
- new PlatformViewCreationRequest (
94
- (int ) createArgs .get ("id" ),
95
- (String ) createArgs .get ("viewType" ),
96
- width ,
97
- height ,
98
- (int ) createArgs .get ("direction" ),
99
- createArgs .containsKey ("params" )
100
- ? ByteBuffer .wrap ((byte []) createArgs .get ("params" ))
101
- : null );
93
+ final ByteBuffer additionalParams =
94
+ createArgs .containsKey ("params" )
95
+ ? ByteBuffer .wrap ((byte []) createArgs .get ("params" ))
96
+ : null ;
102
97
try {
103
- if (usesHybridComposition ) {
104
- handler .createAndroidViewForPlatformView (request );
98
+ if (usesPlatformViewLayer ) {
99
+ final PlatformViewCreationRequest request =
100
+ new PlatformViewCreationRequest (
101
+ (int ) createArgs .get ("id" ),
102
+ (String ) createArgs .get ("viewType" ),
103
+ 0 ,
104
+ 0 ,
105
+ 0 ,
106
+ 0 ,
107
+ (int ) createArgs .get ("direction" ),
108
+ additionalParams );
109
+ handler .createForPlatformViewLayer (request );
105
110
result .success (null );
106
111
} else {
107
- long textureId = handler .createVirtualDisplayForPlatformView (request );
112
+ final PlatformViewCreationRequest request =
113
+ new PlatformViewCreationRequest (
114
+ (int ) createArgs .get ("id" ),
115
+ (String ) createArgs .get ("viewType" ),
116
+ createArgs .containsKey ("top" ) ? (double ) createArgs .get ("top" ) : 0.0 ,
117
+ createArgs .containsKey ("left" ) ? (double ) createArgs .get ("left" ) : 0.0 ,
118
+ (double ) createArgs .get ("width" ),
119
+ (double ) createArgs .get ("height" ),
120
+ (int ) createArgs .get ("direction" ),
121
+ additionalParams );
122
+ long textureId = handler .createForTextureLayer (request );
108
123
result .success (textureId );
109
124
}
110
125
} catch (IllegalStateException exception ) {
@@ -115,15 +130,9 @@ private void create(@NonNull MethodCall call, @NonNull MethodChannel.Result resu
115
130
private void dispose (@ NonNull MethodCall call , @ NonNull MethodChannel .Result result ) {
116
131
Map <String , Object > disposeArgs = call .arguments ();
117
132
int viewId = (int ) disposeArgs .get ("id" );
118
- boolean usesHybridComposition =
119
- disposeArgs .containsKey ("hybrid" ) && (boolean ) disposeArgs .get ("hybrid" );
120
133
121
134
try {
122
- if (usesHybridComposition ) {
123
- handler .disposeAndroidViewForPlatformView (viewId );
124
- } else {
125
- handler .disposeVirtualDisplayForPlatformView (viewId );
126
- }
135
+ handler .dispose (viewId );
127
136
result .success (null );
128
137
} catch (IllegalStateException exception ) {
129
138
result .error ("error" , detailedExceptionString (exception ), null );
@@ -138,14 +147,28 @@ private void resize(@NonNull MethodCall call, @NonNull MethodChannel.Result resu
138
147
(double ) resizeArgs .get ("width" ),
139
148
(double ) resizeArgs .get ("height" ));
140
149
try {
141
- handler .resizePlatformView (
142
- resizeRequest ,
143
- new Runnable () {
144
- @ Override
145
- public void run () {
146
- result .success (null );
147
- }
148
- });
150
+ final PlatformViewBufferSize sz = handler .resize (resizeRequest );
151
+ if (sz == null ) {
152
+ result .error ("error" , "Failed to resize the platform view" , null );
153
+ } else {
154
+ final Map <String , Object > response = new HashMap <>();
155
+ response .put ("width" , (double ) sz .width );
156
+ response .put ("height" , (double ) sz .height );
157
+ result .success (response );
158
+ }
159
+ } catch (IllegalStateException exception ) {
160
+ result .error ("error" , detailedExceptionString (exception ), null );
161
+ }
162
+ }
163
+
164
+ private void offset (@ NonNull MethodCall call , @ NonNull MethodChannel .Result result ) {
165
+ Map <String , Object > offsetArgs = call .arguments ();
166
+ try {
167
+ handler .offset (
168
+ (int ) offsetArgs .get ("id" ),
169
+ (double ) offsetArgs .get ("top" ),
170
+ (double ) offsetArgs .get ("left" ));
171
+ result .success (null );
149
172
} catch (IllegalStateException exception ) {
150
173
result .error ("error" , detailedExceptionString (exception ), null );
151
174
}
@@ -249,36 +272,40 @@ public interface PlatformViewsHandler {
249
272
* The Flutter application would like to display a new Android {@code View}, i.e., platform
250
273
* view.
251
274
*
252
- * <p>The Android {@code View} is added to the view hierarchy.
253
- */
254
- void createAndroidViewForPlatformView (@ NonNull PlatformViewCreationRequest request );
255
-
256
- /**
257
- * The Flutter application would like to dispose of an existing Android {@code View} rendered in
258
- * the view hierarchy.
275
+ * <p>The Android View is added to the view hierarchy. This view is rendered in the Flutter
276
+ * framework by a PlatformViewLayer.
277
+ *
278
+ * @param request The metadata sent from the framework.
259
279
*/
260
- void disposeAndroidViewForPlatformView ( int viewId );
280
+ void createForPlatformViewLayer ( @ NonNull PlatformViewCreationRequest request );
261
281
262
282
/**
263
- * The Flutter application would like to display a new Android {@code View}.
283
+ * The Flutter application would like to display a new Android {@code View}, i.e., platform
284
+ * view.
264
285
*
265
- * <p>{@code View} is added to a {@code VirtualDisplay}. The framework uses id returned by this
266
- * method to lookup the texture in the engine.
286
+ * <p>The Android View is added to the view hierarchy. This view is rendered in the Flutter
287
+ * framework by a TextureLayer.
288
+ *
289
+ * @param request The metadata sent from the framework.
290
+ * @return The texture ID.
267
291
*/
268
- long createVirtualDisplayForPlatformView (@ NonNull PlatformViewCreationRequest request );
292
+ long createForTextureLayer (@ NonNull PlatformViewCreationRequest request );
293
+
294
+ /** The Flutter application would like to dispose of an existing Android {@code View}. */
295
+ void dispose (int viewId );
269
296
270
297
/**
271
- * The Flutter application would like to dispose of an existing Android {@code View} rendered in
272
- * a virtual display.
298
+ * The Flutter application would like to resize an existing Android {@code View}.
299
+ *
300
+ * @param request The request to resize the platform view.
301
+ * @return The buffer size where the platform view pixels are written to.
273
302
*/
274
- void disposeVirtualDisplayForPlatformView ( int viewId );
303
+ PlatformViewBufferSize resize ( @ NonNull PlatformViewResizeRequest request );
275
304
276
305
/**
277
- * The Flutter application would like to resize an existing Android {@code View}, i.e., platform
278
- * view.
306
+ * The Flutter application would like to change the offset of an existing Android {@code View}.
279
307
*/
280
- void resizePlatformView (
281
- @ NonNull PlatformViewResizeRequest request , @ NonNull Runnable onComplete );
308
+ void offset (int viewId , double top , double left );
282
309
283
310
/**
284
311
* The user touched a platform view within Flutter.
@@ -321,6 +348,12 @@ public static class PlatformViewCreationRequest {
321
348
/** The density independent height to display the platform view. */
322
349
public final double logicalHeight ;
323
350
351
+ /** The density independent top position to display the platform view. */
352
+ public final double logicalTop ;
353
+
354
+ /** The density independent left position to display the platform view. */
355
+ public final double logicalLeft ;
356
+
324
357
/**
325
358
* The layout direction of the new platform view.
326
359
*
@@ -332,28 +365,28 @@ public static class PlatformViewCreationRequest {
332
365
/** Custom parameters that are unique to the desired platform view. */
333
366
@ Nullable public final ByteBuffer params ;
334
367
335
- /** Creates a request to construct a platform view that uses a virtual display . */
368
+ /** Creates a request to construct a platform view. */
336
369
public PlatformViewCreationRequest (
337
370
int viewId ,
338
371
@ NonNull String viewType ,
372
+ double logicalTop ,
373
+ double logicalLeft ,
339
374
double logicalWidth ,
340
375
double logicalHeight ,
341
376
int direction ,
342
377
@ Nullable ByteBuffer params ) {
343
378
this .viewId = viewId ;
344
379
this .viewType = viewType ;
380
+ this .logicalTop = logicalTop ;
381
+ this .logicalLeft = logicalLeft ;
345
382
this .logicalWidth = logicalWidth ;
346
383
this .logicalHeight = logicalHeight ;
347
384
this .direction = direction ;
348
385
this .params = params ;
349
386
}
350
387
}
351
388
352
- /**
353
- * Request sent from Flutter to resize a platform view.
354
- *
355
- * <p>This only applies to platform views that use virtual displays.
356
- */
389
+ /** Request sent from Flutter to resize a platform view. */
357
390
public static class PlatformViewResizeRequest {
358
391
/** The ID of the platform view as seen by the Flutter side. */
359
392
public final int viewId ;
@@ -371,6 +404,20 @@ public PlatformViewResizeRequest(int viewId, double newLogicalWidth, double newL
371
404
}
372
405
}
373
406
407
+ /** The platform view buffer size. */
408
+ public static class PlatformViewBufferSize {
409
+ /** The width of the screen buffer. */
410
+ public final int width ;
411
+
412
+ /** The height of the screen buffer. */
413
+ public final int height ;
414
+
415
+ public PlatformViewBufferSize (int width , int height ) {
416
+ this .width = width ;
417
+ this .height = height ;
418
+ }
419
+ }
420
+
374
421
/** The state of a touch event in Flutter within a platform view. */
375
422
public static class PlatformViewTouch {
376
423
/** The ID of the platform view as seen by the Flutter side. */
0 commit comments