Skip to content

Display and control your Android device

Implement framework wrappers separately

Romain Vimont1a71c4ab1d83
diff --git a/server/Makefile b/server/Makefile
index a648405e..2bc0384b 100644
--- a/server/Makefile
+++ b/server/Makefile
@@ -22,6 +22,9 @@ SRC := com/genymobile/scrcpy/ScrCpyServer.java \
com/genymobile/scrcpy/ScreenInfo.java \
com/genymobile/scrcpy/ScreenStreamer.java \
com/genymobile/scrcpy/ScreenStreamerSession.java \
+ com/genymobile/scrcpy/wrappers/DisplayManager.java \
+ com/genymobile/scrcpy/wrappers/ServiceManager.java \
+ com/genymobile/scrcpy/wrappers/WindowManager.java \
JAR := scrcpy-server.jar
MAIN := com.genymobile.scrcpy.ScrCpyServer
diff --git a/server/src/com/genymobile/scrcpy/DeviceUtil.java b/server/src/com/genymobile/scrcpy/DeviceUtil.java
index 03e83126..f4ee98be 100644
--- a/server/src/com/genymobile/scrcpy/DeviceUtil.java
+++ b/server/src/com/genymobile/scrcpy/DeviceUtil.java
@@ -1,11 +1,9 @@
package com.genymobile.scrcpy;
import android.os.Build;
-import android.os.IBinder;
-import android.os.IInterface;
import android.view.IRotationWatcher;
-import java.lang.reflect.Method;
+import com.genymobile.scrcpy.wrappers.ServiceManager;
public class DeviceUtil {
@@ -22,94 +20,4 @@ public class DeviceUtil {
public static String getDeviceName() {
return Build.MODEL;
}
-
- private static class ServiceManager {
- private Method getServiceMethod;
-
- public ServiceManager() {
- try {
- getServiceMethod = Class.forName("android.os.ServiceManager").getDeclaredMethod("getService", String.class);
- } catch (Exception e) {
- throw new AssertionError(e);
- }
- }
-
- private IInterface getService(String service, String type) {
- try {
- IBinder binder = (IBinder) getServiceMethod.invoke(null, service);
- Method asInterfaceMethod = Class.forName(type + "$Stub").getMethod("asInterface", IBinder.class);
- return (IInterface) asInterfaceMethod.invoke(null, binder);
- } catch (Exception e) {
- throw new AssertionError(e);
- }
- }
-
- public WindowManager getWindowManager() {
- return new WindowManager(getService("window", "android.view.IWindowManager"));
- }
-
- public DisplayManager getDisplayManager() {
- return new DisplayManager(getService("display", "android.hardware.display.IDisplayManager"));
- }
- }
-
- private static class WindowManager {
- private IInterface manager;
-
- public WindowManager(IInterface manager) {
- this.manager = manager;
- }
-
- public int getRotation() {
- try {
- Class<?> cls = manager.getClass();
- try {
- return (Integer) manager.getClass().getMethod("getRotation").invoke(manager);
- } catch (NoSuchMethodException e) {
- // method changed since this commit:
- // https://android.googlesource.com/platform/frameworks/base/+/8ee7285128c3843401d4c4d0412cd66e86ba49e3%5E%21/#F2
- return (Integer) cls.getMethod("getDefaultDisplayRotation").invoke(manager);
- }
- } catch (Exception e) {
- throw new AssertionError(e);
- }
- }
-
- public void registerRotationWatcher(IRotationWatcher rotationWatcher) {
- try {
- Class<?> cls = manager.getClass();
- try {
- cls.getMethod("watchRotation", IRotationWatcher.class).invoke(manager, rotationWatcher);
- } catch (NoSuchMethodException e) {
- // display parameter added since this commit:
- // https://android.googlesource.com/platform/frameworks/base/+/35fa3c26adcb5f6577849fd0df5228b1f67cf2c6%5E%21/#F1
- cls.getMethod("watchRotation", IRotationWatcher.class, int.class).invoke(manager, rotationWatcher, 0);
- }
- } catch (Exception e) {
- throw new AssertionError(e);
- }
- }
- }
-
- private static class DisplayManager {
- private IInterface manager;
-
- public DisplayManager(IInterface manager) {
- this.manager = manager;
- }
-
- public ScreenInfo getScreenInfo() {
- try {
- Object displayInfo = manager.getClass().getMethod("getDisplayInfo", int.class).invoke(manager, 0);
- Class<?> cls = displayInfo.getClass();
- // width and height do not depend on the rotation
- int width = (Integer) cls.getMethod("getNaturalWidth").invoke(displayInfo);
- int height = (Integer) cls.getMethod("getNaturalHeight").invoke(displayInfo);
- int rotation = cls.getDeclaredField("rotation").getInt(displayInfo);
- return new ScreenInfo(width, height, rotation);
- } catch (Exception e) {
- throw new AssertionError(e);
- }
- }
- }
}
diff --git a/server/src/com/genymobile/scrcpy/wrappers/DisplayManager.java b/server/src/com/genymobile/scrcpy/wrappers/DisplayManager.java
new file mode 100644
index 00000000..4fddde23
--- /dev/null
+++ b/server/src/com/genymobile/scrcpy/wrappers/DisplayManager.java
@@ -0,0 +1,27 @@
+package com.genymobile.scrcpy.wrappers;
+
+import android.os.IInterface;
+
+import com.genymobile.scrcpy.ScreenInfo;
+
+public class DisplayManager {
+ private final IInterface manager;
+
+ public DisplayManager(IInterface manager) {
+ this.manager = manager;
+ }
+
+ public ScreenInfo getScreenInfo() {
+ try {
+ Object displayInfo = manager.getClass().getMethod("getDisplayInfo", int.class).invoke(manager, 0);
+ Class<?> cls = displayInfo.getClass();
+ // width and height do not depend on the rotation
+ int width = (Integer) cls.getMethod("getNaturalWidth").invoke(displayInfo);
+ int height = (Integer) cls.getMethod("getNaturalHeight").invoke(displayInfo);
+ int rotation = cls.getDeclaredField("rotation").getInt(displayInfo);
+ return new ScreenInfo(width, height, rotation);
+ } catch (Exception e) {
+ throw new AssertionError(e);
+ }
+ }
+}
diff --git a/server/src/com/genymobile/scrcpy/wrappers/ServiceManager.java b/server/src/com/genymobile/scrcpy/wrappers/ServiceManager.java
new file mode 100644
index 00000000..e034a0d8
--- /dev/null
+++ b/server/src/com/genymobile/scrcpy/wrappers/ServiceManager.java
@@ -0,0 +1,36 @@
+package com.genymobile.scrcpy.wrappers;
+
+import android.os.IBinder;
+import android.os.IInterface;
+
+import java.lang.reflect.Method;
+
+public class ServiceManager {
+ private final Method getServiceMethod;
+
+ public ServiceManager() {
+ try {
+ getServiceMethod = Class.forName("android.os.ServiceManager").getDeclaredMethod("getService", String.class);
+ } catch (Exception e) {
+ throw new AssertionError(e);
+ }
+ }
+
+ private IInterface getService(String service, String type) {
+ try {
+ IBinder binder = (IBinder) getServiceMethod.invoke(null, service);
+ Method asInterfaceMethod = Class.forName(type + "$Stub").getMethod("asInterface", IBinder.class);
+ return (IInterface) asInterfaceMethod.invoke(null, binder);
+ } catch (Exception e) {
+ throw new AssertionError(e);
+ }
+ }
+
+ public WindowManager getWindowManager() {
+ return new WindowManager(getService("window", "android.view.IWindowManager"));
+ }
+
+ public DisplayManager getDisplayManager() {
+ return new DisplayManager(getService("display", "android.hardware.display.IDisplayManager"));
+ }
+}
diff --git a/server/src/com/genymobile/scrcpy/wrappers/WindowManager.java b/server/src/com/genymobile/scrcpy/wrappers/WindowManager.java
new file mode 100644
index 00000000..78e50c3b
--- /dev/null
+++ b/server/src/com/genymobile/scrcpy/wrappers/WindowManager.java
@@ -0,0 +1,42 @@
+package com.genymobile.scrcpy.wrappers;
+
+import android.os.IInterface;
+import android.view.IRotationWatcher;
+
+public class WindowManager {
+ private final IInterface manager;
+
+ public WindowManager(IInterface manager) {
+ this.manager = manager;
+ }
+
+ public int getRotation() {
+ try {
+ Class<?> cls = manager.getClass();
+ try {
+ return (Integer) manager.getClass().getMethod("getRotation").invoke(manager);
+ } catch (NoSuchMethodException e) {
+ // method changed since this commit:
+ // https://android.googlesource.com/platform/frameworks/base/+/8ee7285128c3843401d4c4d0412cd66e86ba49e3%5E%21/#F2
+ return (Integer) cls.getMethod("getDefaultDisplayRotation").invoke(manager);
+ }
+ } catch (Exception e) {
+ throw new AssertionError(e);
+ }
+ }
+
+ public void registerRotationWatcher(IRotationWatcher rotationWatcher) {
+ try {
+ Class<?> cls = manager.getClass();
+ try {
+ cls.getMethod("watchRotation", IRotationWatcher.class).invoke(manager, rotationWatcher);
+ } catch (NoSuchMethodException e) {
+ // display parameter added since this commit:
+ // https://android.googlesource.com/platform/frameworks/base/+/35fa3c26adcb5f6577849fd0df5228b1f67cf2c6%5E%21/#F1
+ cls.getMethod("watchRotation", IRotationWatcher.class, int.class).invoke(manager, rotationWatcher, 0);
+ }
+ } catch (Exception e) {
+ throw new AssertionError(e);
+ }
+ }
+}