From b7a941a3b76dbd12e6de201c547bc0563d92e505 Mon Sep 17 00:00:00 2001 From: SeungHoon Han Date: Tue, 2 Aug 2022 19:39:50 +0900 Subject: [PATCH] added fallback getting displayInfo when displayInfo is null --- .../main/java/com/genymobile/scrcpy/Command.java | 8 ++++---- .../scrcpy/wrappers/DisplayManager.java | 16 ++++++++++++++++ 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/server/src/main/java/com/genymobile/scrcpy/Command.java b/server/src/main/java/com/genymobile/scrcpy/Command.java index 0ef976a66c..8cf6c4b117 100644 --- a/server/src/main/java/com/genymobile/scrcpy/Command.java +++ b/server/src/main/java/com/genymobile/scrcpy/Command.java @@ -18,16 +18,16 @@ public static void exec(String... cmd) throws IOException, InterruptedException } public static String execReadLine(String... cmd) throws IOException, InterruptedException { - String result = null; + StringBuilder result = new StringBuilder(); Process process = Runtime.getRuntime().exec(cmd); Scanner scanner = new Scanner(process.getInputStream()); - if (scanner.hasNextLine()) { - result = scanner.nextLine(); + while (scanner.hasNextLine()) { + result.append(scanner.nextLine()).append("\n"); } int exitCode = process.waitFor(); if (exitCode != 0) { throw new IOException("Command " + Arrays.toString(cmd) + " returned with value " + exitCode); } - return result; + return result.toString(); } } diff --git a/server/src/main/java/com/genymobile/scrcpy/wrappers/DisplayManager.java b/server/src/main/java/com/genymobile/scrcpy/wrappers/DisplayManager.java index 3f4f897dfa..cd920d951b 100644 --- a/server/src/main/java/com/genymobile/scrcpy/wrappers/DisplayManager.java +++ b/server/src/main/java/com/genymobile/scrcpy/wrappers/DisplayManager.java @@ -1,8 +1,12 @@ package com.genymobile.scrcpy.wrappers; +import com.genymobile.scrcpy.Command; import com.genymobile.scrcpy.DisplayInfo; import com.genymobile.scrcpy.Size; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + public final class DisplayManager { private final Object manager; // instance of hidden class android.hardware.display.DisplayManagerGlobal @@ -14,6 +18,18 @@ public DisplayInfo getDisplayInfo(int displayId) { try { Object displayInfo = manager.getClass().getMethod("getDisplayInfo", int.class).invoke(manager, displayId); if (displayInfo == null) { + // fallback when displayInfo is null + String dumpDisplay = Command.execReadLine("dumpsys", "display"); + Pattern regex = Pattern.compile("(mBaseDisplayInfo=DisplayInfo\\{\".+?\", displayId " + displayId + + ")(.+)(, real )([0-9]+) x ([0-9]+)(.+)(, rotation )([0-9]+)(.+)(, layerStack )([0-9]+)(.+)"); + Matcher m = regex.matcher(dumpDisplay); + if (m.find()) { + int width = Integer.parseInt(m.group(4)); + int height = Integer.parseInt(m.group(5)); + int rotation = Integer.parseInt(m.group(8)); + int layerStack = Integer.parseInt(m.group(11)); + return new DisplayInfo(displayId, new Size(width, height), rotation, layerStack, 0); + } return null; } Class cls = displayInfo.getClass();