Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -16,16 +20,13 @@
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>
Expand Down Expand Up @@ -199,14 +200,18 @@ private void createPlatformView(MethodCall call, MethodChannel.Result result) {
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
);
Expand Down Expand Up @@ -261,9 +266,14 @@ private void resizePlatformView(MethodCall call, final MethodChannel.Result resu
);
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() {
Expand Down Expand Up @@ -406,6 +416,18 @@ private static PointerCoords parsePointerCoords(Object rawCoords, float density)
return coords;
}

private void validateVirtualDisplayDimensions(int width, int height) {
DisplayMetrics metrics = mContext.getResources().getDisplayMetrics();
if (height > metrics.heightPixels || width > metrics.widthPixels) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm missing context here, but wondering why do we have to be that strict? do we never want to allow VDs bigger than the main display? (if that is the case would be useful to add a comment explaining why we want to be that strict)

StringBuilder errorBuilder = new StringBuilder();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I think it's more common in out codebase to just use + for string concatenation (I believe it is compiled to a StringBuilder anyway).
This should also save the Local import, which initially made me wonder.

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);
Expand Down