This repository was archived by the owner on Feb 25, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6k
Implement onDisplayPlatformView #19344
Merged
fluttergithubbot
merged 6 commits into
flutter:master
from
blasten:on_display_platform_view
Jun 27, 2020
Merged
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
26fc606
Implement onDisplayPlatformView
2e6f501
Comment
8899323
Typos
f61aa8c
Linter
fb0b13d
Hide platform view if it is not in the current frame
4bb31e1
Merge remote-tracking branch 'upstream/master' into on_display_platfo…
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -44,10 +44,6 @@ | |
| public class PlatformViewsController implements PlatformViewsAccessibilityDelegate { | ||
| private static final String TAG = "PlatformViewsController"; | ||
|
|
||
| // API level 20 is required for VirtualDisplay#setSurface which we use when resizing a platform | ||
| // view. | ||
| private static final int MINIMAL_SDK = Build.VERSION_CODES.KITKAT_WATCH; | ||
|
|
||
| private final PlatformViewRegistryImpl registry; | ||
|
|
||
| // The context of the Activity or Fragment hosting the render target for the Flutter engine. | ||
|
|
@@ -80,6 +76,9 @@ public class PlatformViewsController implements PlatformViewsAccessibilityDelega | |
| // it is associated with(e.g if a platform view creates other views in the same virtual display. | ||
| private final HashMap<Context, View> contextToPlatformView; | ||
|
|
||
| private final SparseArray<PlatformViewsChannel.PlatformViewCreationRequest> platformViewRequests; | ||
| private final SparseArray<View> platformViews; | ||
|
|
||
| // Map of unique IDs to views that render overlay layers. | ||
| private final SparseArray<FlutterImageView> overlayLayerViews; | ||
|
|
||
|
|
@@ -94,12 +93,34 @@ public class PlatformViewsController implements PlatformViewsAccessibilityDelega | |
|
|
||
| private final PlatformViewsChannel.PlatformViewsHandler channelHandler = | ||
| new PlatformViewsChannel.PlatformViewsHandler() { | ||
| @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1) | ||
|
|
||
| @Override | ||
| public long createPlatformView( | ||
| public void createAndroidViewForPlatformView( | ||
| @NonNull PlatformViewsChannel.PlatformViewCreationRequest request) { | ||
| ensureValidAndroidVersion(); | ||
| // API level 19 is required for android.graphics.ImageReader. | ||
| ensureValidAndroidVersion(Build.VERSION_CODES.KITKAT); | ||
| platformViewRequests.put(request.viewId, request); | ||
| } | ||
|
|
||
| @Override | ||
| public void disposeAndroidViewForPlatformView(int viewId) { | ||
| // Hybrid view. | ||
| if (platformViewRequests.get(viewId) != null) { | ||
| platformViewRequests.remove(viewId); | ||
| } | ||
| if (platformViews.get(viewId) != null) { | ||
| ((FlutterView) flutterView).addView(platformViews.get(viewId)); | ||
| platformViews.remove(viewId); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public long createVirtualDisplayForPlatformView( | ||
| @NonNull PlatformViewsChannel.PlatformViewCreationRequest request) { | ||
| // API level 20 is required for VirtualDisplay#setSurface which we use when resizing a | ||
| // platform | ||
|
||
| // view. | ||
| ensureValidAndroidVersion(Build.VERSION_CODES.KITKAT_WATCH); | ||
| if (!validateDirection(request.direction)) { | ||
| throw new IllegalStateException( | ||
| "Trying to create a view with unknown direction value: " | ||
|
|
@@ -171,9 +192,8 @@ public long createPlatformView( | |
| } | ||
|
|
||
| @Override | ||
| public void disposePlatformView(int viewId) { | ||
| ensureValidAndroidVersion(); | ||
|
|
||
| public void disposeVirtualDisplayForPlatformView(int viewId) { | ||
| ensureValidAndroidVersion(Build.VERSION_CODES.KITKAT_WATCH); | ||
| VirtualDisplayController vdController = vdControllers.get(viewId); | ||
| if (vdController == null) { | ||
| throw new IllegalStateException( | ||
|
|
@@ -193,7 +213,7 @@ public void disposePlatformView(int viewId) { | |
| public void resizePlatformView( | ||
| @NonNull PlatformViewsChannel.PlatformViewResizeRequest request, | ||
| @NonNull Runnable onComplete) { | ||
| ensureValidAndroidVersion(); | ||
| ensureValidAndroidVersion(Build.VERSION_CODES.KITKAT_WATCH); | ||
|
|
||
| final VirtualDisplayController vdController = vdControllers.get(request.viewId); | ||
| if (vdController == null) { | ||
|
|
@@ -224,8 +244,6 @@ public void run() { | |
|
|
||
| @Override | ||
| public void onTouch(@NonNull PlatformViewsChannel.PlatformViewTouch touch) { | ||
| ensureValidAndroidVersion(); | ||
|
|
||
| float density = context.getResources().getDisplayMetrics().density; | ||
| PointerProperties[] pointerProperties = | ||
| parsePointerPropertiesList(touch.rawPointerPropertiesList) | ||
|
|
@@ -256,14 +274,12 @@ public void onTouch(@NonNull PlatformViewsChannel.PlatformViewTouch touch) { | |
| touch.source, | ||
| touch.flags); | ||
|
|
||
| ensureValidAndroidVersion(Build.VERSION_CODES.KITKAT_WATCH); | ||
| vdControllers.get(touch.viewId).dispatchTouchEvent(event); | ||
| } | ||
|
|
||
| @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1) | ||
| @Override | ||
| public void setDirection(int viewId, int direction) { | ||
| ensureValidAndroidVersion(); | ||
|
|
||
| if (!validateDirection(direction)) { | ||
| throw new IllegalStateException( | ||
| "Trying to set unknown direction value: " | ||
|
|
@@ -273,6 +289,7 @@ public void setDirection(int viewId, int direction) { | |
| + ")"); | ||
| } | ||
|
|
||
| ensureValidAndroidVersion(Build.VERSION_CODES.KITKAT_WATCH); | ||
| View view = vdControllers.get(viewId).getView(); | ||
| if (view == null) { | ||
| throw new IllegalStateException( | ||
|
|
@@ -284,17 +301,18 @@ public void setDirection(int viewId, int direction) { | |
|
|
||
| @Override | ||
| public void clearFocus(int viewId) { | ||
| ensureValidAndroidVersion(Build.VERSION_CODES.KITKAT_WATCH); | ||
| View view = vdControllers.get(viewId).getView(); | ||
| view.clearFocus(); | ||
| } | ||
|
|
||
| private void ensureValidAndroidVersion() { | ||
| if (Build.VERSION.SDK_INT < MINIMAL_SDK) { | ||
| private void ensureValidAndroidVersion(int minSdkVersion) { | ||
| if (Build.VERSION.SDK_INT < minSdkVersion) { | ||
| throw new IllegalStateException( | ||
| "Trying to use platform views with API " | ||
| + Build.VERSION.SDK_INT | ||
| + ", required API level is: " | ||
| + MINIMAL_SDK); | ||
| + minSdkVersion); | ||
| } | ||
| } | ||
| }; | ||
|
|
@@ -306,6 +324,9 @@ public PlatformViewsController() { | |
| contextToPlatformView = new HashMap<>(); | ||
| overlayLayerViews = new SparseArray<>(); | ||
| currentFrameUsedOverlayLayerIds = new HashSet<>(); | ||
|
|
||
| platformViewRequests = new SparseArray<>(); | ||
| platformViews = new SparseArray<>(); | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -565,13 +586,60 @@ private void initializeRootImageViewIfNeeded() { | |
| } | ||
| } | ||
|
|
||
| private void initializePlatformViewIfNeeded(int viewId) { | ||
| if (platformViews.get(viewId) != null) { | ||
| return; | ||
| } | ||
|
|
||
| PlatformViewsChannel.PlatformViewCreationRequest request = platformViewRequests.get(viewId); | ||
| if (request == null) { | ||
| throw new IllegalStateException( | ||
| "Platform view hasn't been initialized from the platform view channel."); | ||
| } | ||
|
|
||
| if (!validateDirection(request.direction)) { | ||
| throw new IllegalStateException( | ||
| "Trying to create a view with unknown direction value: " | ||
| + request.direction | ||
| + "(view id: " | ||
| + viewId | ||
| + ")"); | ||
| } | ||
|
|
||
| PlatformViewFactory factory = registry.getFactory(request.viewType); | ||
| if (factory == null) { | ||
| throw new IllegalStateException( | ||
| "Trying to create a platform view of unregistered type: " + request.viewType); | ||
| } | ||
|
|
||
| Object createParams = null; | ||
| if (request.params != null) { | ||
| createParams = factory.getCreateArgsCodec().decodeMessage(request.params); | ||
| } | ||
|
|
||
| PlatformView platformView = factory.create(context, viewId, createParams); | ||
| View view = platformView.getView(); | ||
| platformViews.put(viewId, view); | ||
|
|
||
| ((FlutterView) flutterView).addView(view); | ||
| } | ||
|
|
||
| public void onDisplayPlatformView(int viewId, int x, int y, int width, int height) { | ||
| initializeRootImageViewIfNeeded(); | ||
| // TODO: Implement this method. https://github.com/flutter/flutter/issues/58288 | ||
| initializePlatformViewIfNeeded(viewId); | ||
|
|
||
| View platformView = platformViews.get(viewId); | ||
| FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams((int) width, (int) height); | ||
| layoutParams.leftMargin = (int) x; | ||
| layoutParams.topMargin = (int) y; | ||
| platformView.setLayoutParams(layoutParams); | ||
| platformView.setVisibility(View.VISIBLE); | ||
| platformView.bringToFront(); | ||
| } | ||
|
|
||
| public void onDisplayOverlaySurface(int id, int x, int y, int width, int height) { | ||
| initializeRootImageViewIfNeeded(); | ||
|
|
||
| FlutterImageView overlayView = overlayLayerViews.get(id); | ||
| if (overlayView.getParent() == null) { | ||
| ((FlutterView) flutterView).addView(overlayView); | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
typo: "would like to dispose"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.