Skip to content

Commit

Permalink
Improve checks for calls into native code made after the platform vie…
Browse files Browse the repository at this point in the history
…w has been detached (flutter#3926)
  • Loading branch information
jason-simmons authored Aug 17, 2017
1 parent c80860a commit 0223e29
Showing 1 changed file with 23 additions and 4 deletions.
27 changes: 23 additions & 4 deletions shell/platform/android/io/flutter/view/FlutterView.java
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ public FlutterView(Context context, AttributeSet attrs) {
setFocusableInTouchMode(true);

attach();
assert mNativePlatformView != 0;
assertAttached();

int color = 0xFF000000;
TypedValue typedValue = new TypedValue();
Expand All @@ -135,19 +135,19 @@ public FlutterView(Context context, AttributeSet attrs) {
mSurfaceCallback = new SurfaceHolder.Callback() {
@Override
public void surfaceCreated(SurfaceHolder holder) {
assert mNativePlatformView != 0;
assertAttached();
nativeSurfaceCreated(mNativePlatformView, holder.getSurface(), backgroundColor);
}

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
assert mNativePlatformView != 0;
assertAttached();
nativeSurfaceChanged(mNativePlatformView, width, height);
}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {
assert mNativePlatformView != 0;
assertAttached();
nativeSurfaceDestroyed(mNativePlatformView);
}
};
Expand Down Expand Up @@ -292,6 +292,9 @@ float getDevicePixelRatio() {
}

public void destroy() {
if (!isAttached())
return;

if (mDiscoveryReceiver != null) {
getContext().unregisterReceiver(mDiscoveryReceiver);
}
Expand Down Expand Up @@ -529,6 +532,11 @@ private boolean isAttached() {
return mNativePlatformView != 0;
}

void assertAttached() {
if (!isAttached())
throw new AssertionError("Platform view is not attached");
}

private void preRun() {
resetAccessibilityTree();
}
Expand All @@ -537,6 +545,7 @@ private void postRun() {
}

public void runFromBundle(String bundlePath, String snapshotOverride) {
assertAttached();
preRun();
nativeRunBundleAndSnapshot(mNativePlatformView, bundlePath, snapshotOverride);
postRun();
Expand All @@ -547,6 +556,7 @@ private void runFromSource(final String assetsDirectory,
final String packages) {
Runnable runnable = new Runnable() {
public void run() {
assertAttached();
preRun();
nativeRunBundleAndSource(mNativePlatformView, assetsDirectory, main, packages);
postRun();
Expand Down Expand Up @@ -574,6 +584,7 @@ public void run() {
* @return A bitmap.
*/
public Bitmap getBitmap() {
assertAttached();
return nativeGetBitmap(mNativePlatformView);
}

Expand Down Expand Up @@ -641,6 +652,8 @@ private static native void nativeInvokePlatformMessageEmptyResponseCallback(
private static native boolean nativeGetIsSoftwareRenderingEnabled();

private void updateViewportMetrics() {
if (!isAttached())
return;
nativeSetViewportMetrics(mNativePlatformView,
mMetrics.devicePixelRatio,
mMetrics.physicalWidth,
Expand All @@ -653,6 +666,7 @@ private void updateViewportMetrics() {

// Called by native to send us a platform message.
private void handlePlatformMessage(String channel, byte[] message, final int replyId) {
assertAttached();
BinaryMessageHandler handler = mMessageHandlers.get(channel);
if (handler != null) {
try {
Expand All @@ -662,6 +676,7 @@ private void handlePlatformMessage(String channel, byte[] message, final int rep
private final AtomicBoolean done = new AtomicBoolean(false);
@Override
public void reply(ByteBuffer reply) {
assertAttached();
if (done.getAndSet(true)) {
throw new IllegalStateException("Reply already submitted");
}
Expand Down Expand Up @@ -724,6 +739,8 @@ private void onFirstFrame() {
private TouchExplorationListener mTouchExplorationListener;

protected void dispatchSemanticsAction(int id, int action) {
if (!isAttached())
return;
nativeDispatchSemanticsAction(mNativePlatformView, id, action);
}

Expand Down Expand Up @@ -803,6 +820,8 @@ public AccessibilityNodeProvider getAccessibilityNodeProvider() {
private AccessibilityBridge mAccessibilityNodeProvider;

void ensureAccessibilityEnabled() {
if (!isAttached())
return;
mAccessibilityEnabled = true;
if (mAccessibilityNodeProvider == null) {
mAccessibilityNodeProvider = new AccessibilityBridge(this);
Expand Down

0 comments on commit 0223e29

Please sign in to comment.