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
This commit is contained in:
Kaushik Iska 2019-04-25 13:44:45 -07:00 committed by GitHub
parent 8edfaea973
commit 7471ddea46
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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.
* <p>
@ -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);