From 7471ddea463dd0a95a2b6d8e3a9028df70c56cee Mon Sep 17 00:00:00 2001 From: Kaushik Iska Date: Thu, 25 Apr 2019 13:44:45 -0700 Subject: [PATCH] Limit the size of VirtualDisplay we create in android (#8704) - This can cause phones to restart as documented in https://github.com/flutter/flutter/issues/28978 --- .../platform/PlatformViewsController.java | 38 +++++++++++++++---- 1 file changed, 30 insertions(+), 8 deletions(-) diff --git a/shell/platform/android/io/flutter/plugin/platform/PlatformViewsController.java b/shell/platform/android/io/flutter/plugin/platform/PlatformViewsController.java index 5470e03a9f2..2561897bd85 100644 --- a/shell/platform/android/io/flutter/plugin/platform/PlatformViewsController.java +++ b/shell/platform/android/io/flutter/plugin/platform/PlatformViewsController.java @@ -4,9 +4,13 @@ package io.flutter.plugin.platform; +import static android.view.MotionEvent.PointerCoords; +import static android.view.MotionEvent.PointerProperties; + import android.annotation.TargetApi; import android.content.Context; import android.os.Build; +import android.util.DisplayMetrics; import android.util.Log; import android.view.MotionEvent; import android.view.View; @@ -16,16 +20,13 @@ import io.flutter.plugin.common.MethodChannel; import io.flutter.plugin.common.StandardMethodCodec; import io.flutter.view.AccessibilityBridge; import io.flutter.view.TextureRegistry; - import java.nio.ByteBuffer; import java.util.ArrayList; import java.util.HashMap; import java.util.List; +import java.util.Locale; import java.util.Map; -import static android.view.MotionEvent.PointerCoords; -import static android.view.MotionEvent.PointerProperties; - /** * Manages platform views. *

@@ -199,14 +200,18 @@ public class PlatformViewsController implements MethodChannel.MethodCallHandler, createParams = viewFactory.getCreateArgsCodec().decodeMessage(ByteBuffer.wrap((byte[]) args.get("params"))); } + int physicalWidth = toPhysicalPixels(logicalWidth); + int physicalHeight = toPhysicalPixels(logicalHeight); + validateVirtualDisplayDimensions(physicalWidth, physicalHeight); + TextureRegistry.SurfaceTextureEntry textureEntry = mTextureRegistry.createSurfaceTexture(); VirtualDisplayController vdController = VirtualDisplayController.create( mContext, mAccessibilityEventsDelegate, viewFactory, textureEntry, - toPhysicalPixels(logicalWidth), - toPhysicalPixels(logicalHeight), + physicalWidth, + physicalHeight, id, createParams ); @@ -261,9 +266,14 @@ public class PlatformViewsController implements MethodChannel.MethodCallHandler, ); return; } + + int physicalWidth = toPhysicalPixels(width); + int physicalHeight = toPhysicalPixels(height); + validateVirtualDisplayDimensions(physicalWidth, physicalHeight); + vdController.resize( - toPhysicalPixels(width), - toPhysicalPixels(height), + physicalWidth, + physicalHeight, new Runnable() { @Override public void run() { @@ -406,6 +416,18 @@ public class PlatformViewsController implements MethodChannel.MethodCallHandler, return coords; } + private void validateVirtualDisplayDimensions(int width, int height) { + DisplayMetrics metrics = mContext.getResources().getDisplayMetrics(); + if (height > metrics.heightPixels || width > metrics.widthPixels) { + StringBuilder errorBuilder = new StringBuilder(); + errorBuilder.append("Creating a virtual display of size: "); + errorBuilder.append(String.format(Locale.ENGLISH, "[%d, %d], ", width, height)); + errorBuilder.append(" is not supported. It is larger than the device screen size: "); + errorBuilder.append(String.format(Locale.ENGLISH, "[%d, %d].", metrics.widthPixels, metrics.heightPixels)); + throw new IllegalArgumentException(errorBuilder.toString()); + } + } + private int toPhysicalPixels(double logicalPixels) { float density = mContext.getResources().getDisplayMetrics().density; return (int) Math.round(logicalPixels * density);