Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
SurfacePlatformViewFactory.java
Go to the documentation of this file.
1// Copyright 2013 The Flutter Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5package dev.flutter.scenarios;
6
7import static io.flutter.Build.API_LEVELS;
8
9import android.annotation.TargetApi;
10import android.content.Context;
11import android.content.ContextWrapper;
12import android.graphics.Canvas;
13import android.graphics.Color;
14import android.graphics.Paint;
15import android.view.Surface;
16import android.view.SurfaceHolder;
17import android.view.SurfaceView;
18import android.view.View;
19import androidx.annotation.NonNull;
20import androidx.annotation.Nullable;
21import io.flutter.Log;
22import io.flutter.plugin.common.MessageCodec;
23import io.flutter.plugin.common.StringCodec;
24import io.flutter.plugin.platform.PlatformView;
25import io.flutter.plugin.platform.PlatformViewFactory;
26import java.nio.ByteBuffer;
27
28@TargetApi(API_LEVELS.API_23)
29public final class SurfacePlatformViewFactory extends PlatformViewFactory {
30 private boolean preserveContext;
31
32 SurfacePlatformViewFactory(boolean preserveContext) {
33 super(
34 new MessageCodec<Object>() {
35 @Nullable
36 @Override
37 public ByteBuffer encodeMessage(@Nullable Object o) {
38 if (o instanceof String) {
39 return StringCodec.INSTANCE.encodeMessage((String) o);
40 }
41 return null;
42 }
43
44 @Nullable
45 @Override
46 public Object decodeMessage(@Nullable ByteBuffer byteBuffer) {
47 return StringCodec.INSTANCE.decodeMessage(byteBuffer);
48 }
49 });
50 this.preserveContext = preserveContext;
51 }
52
53 @SuppressWarnings("unchecked")
54 @Override
55 @NonNull
56 public PlatformView create(@NonNull Context context, int id, @Nullable Object args) {
57 if (preserveContext) {
58 return new SurfacePlatformView(context);
59 } else {
60 final Context differentContext = new ContextWrapper(context);
61 return new SurfacePlatformView(differentContext);
62 }
63 }
64
65 private static class SurfacePlatformView implements PlatformView {
66 static String TAG = "SurfacePlatformView";
67
68 final SurfaceView surfaceView;
69
70 @SuppressWarnings("unchecked")
71 SurfacePlatformView(@NonNull final Context context) {
72 surfaceView = new SurfaceView(context);
73 surfaceView
74 .getHolder()
75 .addCallback(
76 new SurfaceHolder.Callback() {
77 @Override
78 public void surfaceCreated(SurfaceHolder holder) {
79 Log.i(TAG, "surfaceCreated");
80 final Surface surface = holder.getSurface();
81 final Canvas canvas = surface.lockHardwareCanvas();
82 canvas.drawColor(Color.WHITE);
83
84 final Paint paint = new Paint();
85 paint.setColor(Color.RED);
86 canvas.drawCircle(canvas.getWidth() / 2, canvas.getHeight() / 2, 20, paint);
87 surface.unlockCanvasAndPost(canvas);
88 }
89
90 @Override
91 public void surfaceChanged(
92 SurfaceHolder holder, int format, int width, int height) {
93 Log.i(TAG, "surfaceChanged");
94 }
95
96 @Override
97 public void surfaceDestroyed(SurfaceHolder holder) {
98 Log.i(TAG, "surfaceDestroyed");
99 }
100 });
101 }
102
103 @Override
104 @NonNull
105 public View getView() {
106 return surfaceView;
107 }
108
109 @Override
110 public void dispose() {}
111 }
112}
PlatformView create(@NonNull Context context, int id, @Nullable Object args)
G_BEGIN_DECLS G_MODULE_EXPORT FlValue * args
uint32_t uint32_t * format
#define TAG()
int32_t height
int32_t width