Skip to content

Commit

Permalink
added fallback getting displayInfo when displayInfo is null
Browse files Browse the repository at this point in the history
  • Loading branch information
issess committed Sep 6, 2022
1 parent 761fb35 commit b7a941a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
8 changes: 4 additions & 4 deletions server/src/main/java/com/genymobile/scrcpy/Command.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -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

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

0 comments on commit b7a941a

Please sign in to comment.