-
Notifications
You must be signed in to change notification settings - Fork 6k
Create PlatformView instance right after method channel call from Dart #20500
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -79,7 +79,6 @@ 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; | ||
private final SparseArray<FlutterMutatorView> mutatorViews; | ||
|
||
|
@@ -107,18 +106,45 @@ public class PlatformViewsController implements PlatformViewsAccessibilityDelega | |
@Override | ||
public void createAndroidViewForPlatformView( | ||
@NonNull PlatformViewsChannel.PlatformViewCreationRequest request) { | ||
// API level 19 is required for android.graphics.ImageReader. | ||
// API level 19 is required for `android.graphics.ImageReader`. | ||
ensureValidAndroidVersion(Build.VERSION_CODES.KITKAT); | ||
platformViewRequests.put(request.viewId, request); | ||
|
||
if (!validateDirection(request.direction)) { | ||
throw new IllegalStateException( | ||
"Trying to create a view with unknown direction value: " | ||
+ request.direction | ||
+ "(view id: " | ||
+ request.viewId | ||
+ ")"); | ||
} | ||
|
||
final 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); | ||
} | ||
|
||
final PlatformView platformView = factory.create(context, request.viewId, createParams); | ||
final View view = platformView.getView(); | ||
if (view == null) { | ||
throw new IllegalStateException( | ||
"PlatformView#getView() returned null, but an Android view reference was expected."); | ||
} | ||
if (view.getParent() != null) { | ||
throw new IllegalStateException( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Who's responsible for handling all these exceptions? Will they propagate all the way up and crash the app? If so, is that the desired behavior, or should they translate to Dart "soft" exceptions? Can they ever happen in the normal operation of an app, or do they signal developer error? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It will be a It's something that should be addressed in the platform code by the developer before the API can be used in Dart. |
||
"The Android view returned from PlatformView#getView() was already added to a parent view."); | ||
} | ||
platformViews.put(request.viewId, view); | ||
} | ||
|
||
@Override | ||
public void disposeAndroidViewForPlatformView(int viewId) { | ||
// Hybrid view. | ||
if (platformViewRequests.get(viewId) != null) { | ||
platformViewRequests.remove(viewId); | ||
} | ||
|
||
final View platformView = platformViews.get(viewId); | ||
if (platformView != null) { | ||
final FlutterMutatorView mutatorView = mutatorViews.get(viewId); | ||
|
@@ -378,7 +404,6 @@ public PlatformViewsController() { | |
currentFrameUsedOverlayLayerIds = new HashSet<>(); | ||
currentFrameUsedPlatformViewIds = new HashSet<>(); | ||
|
||
platformViewRequests = new SparseArray<>(); | ||
platformViews = new SparseArray<>(); | ||
mutatorViews = new SparseArray<>(); | ||
|
||
|
@@ -651,50 +676,15 @@ private void initializeRootImageViewIfNeeded() { | |
|
||
@VisibleForTesting | ||
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(); | ||
|
||
final View view = platformViews.get(viewId); | ||
if (view == null) { | ||
throw new IllegalStateException( | ||
"PlatformView#getView() returned null, but an Android view reference was expected."); | ||
"Platform view hasn't been initialized from the platform view channel."); | ||
} | ||
if (view.getParent() != null) { | ||
throw new IllegalStateException( | ||
"The Android view returned from PlatformView#getView() was already added to a parent view."); | ||
if (mutatorViews.get(viewId) != null) { | ||
return; | ||
} | ||
platformViews.put(viewId, view); | ||
|
||
FlutterMutatorView mutatorView = | ||
final FlutterMutatorView mutatorView = | ||
new FlutterMutatorView( | ||
context, context.getResources().getDisplayMetrics().density, androidTouchProcessor); | ||
mutatorViews.put(viewId, mutatorView); | ||
|
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.
Here too?
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.
From the app developer perspective, this is an invalid state. It's also caught by https://github.com/flutter/engine/blob/master/shell/platform/android/io/flutter/embedding/engine/systemchannels/PlatformViewsChannel.java#L107 and propagated as an standard method channel result error.