diff --git a/server/Makefile b/server/Makefile index 60dbbeac..a3f61e03 100644 --- a/server/Makefile +++ b/server/Makefile @@ -23,7 +23,7 @@ SRC := com/genymobile/scrcpy/ScrCpyServer.java \ com/genymobile/scrcpy/ControlEvent.java \ com/genymobile/scrcpy/ControlEventReader.java \ com/genymobile/scrcpy/DesktopConnection.java \ - com/genymobile/scrcpy/DeviceUtil.java \ + com/genymobile/scrcpy/Device.java \ com/genymobile/scrcpy/EventController.java \ com/genymobile/scrcpy/ScreenInfo.java \ com/genymobile/scrcpy/ScreenStreamer.java \ diff --git a/server/src/com/genymobile/scrcpy/Device.java b/server/src/com/genymobile/scrcpy/Device.java new file mode 100644 index 00000000..88d1f30d --- /dev/null +++ b/server/src/com/genymobile/scrcpy/Device.java @@ -0,0 +1,70 @@ +package com.genymobile.scrcpy; + +import android.os.Build; +import android.os.RemoteException; +import android.view.IRotationWatcher; + +import com.genymobile.scrcpy.wrappers.InputManager; +import com.genymobile.scrcpy.wrappers.ServiceManager; + +public class Device { + + public interface RotationListener { + void onRotationChanged(int rotation); + } + + private static final Device INSTANCE = new Device(); + private final ServiceManager serviceManager = new ServiceManager(); + + private ScreenInfo screenInfo; + private RotationListener rotationListener; + + private Device() { + screenInfo = readScreenInfo(); + registerRotationWatcher(new IRotationWatcher.Stub() { + @Override + public void onRotationChanged(int rotation) throws RemoteException { + synchronized (Device.this) { + // update screenInfo cache + screenInfo = screenInfo.withRotation(rotation); + + // notify + if (rotationListener != null) { + rotationListener.onRotationChanged(rotation); + } + } + } + }); + } + + public static Device getInstance() { + return INSTANCE; + } + + public synchronized ScreenInfo getScreenInfo() { + if (screenInfo == null) { + screenInfo = readScreenInfo(); + } + return screenInfo; + } + + private ScreenInfo readScreenInfo() { + return serviceManager.getDisplayManager().getScreenInfo(); + } + + public static String getDeviceName() { + return Build.MODEL; + } + + public InputManager getInputManager() { + return serviceManager.getInputManager(); + } + + public void registerRotationWatcher(IRotationWatcher rotationWatcher) { + serviceManager.getWindowManager().registerRotationWatcher(rotationWatcher); + } + + public synchronized void setRotationListener(RotationListener rotationListener) { + this.rotationListener = rotationListener; + } +} diff --git a/server/src/com/genymobile/scrcpy/DeviceUtil.java b/server/src/com/genymobile/scrcpy/DeviceUtil.java deleted file mode 100644 index 1c8f78ec..00000000 --- a/server/src/com/genymobile/scrcpy/DeviceUtil.java +++ /dev/null @@ -1,28 +0,0 @@ -package com.genymobile.scrcpy; - -import android.os.Build; -import android.view.IRotationWatcher; - -import com.genymobile.scrcpy.wrappers.InputManager; -import com.genymobile.scrcpy.wrappers.ServiceManager; - -public class DeviceUtil { - - private static final ServiceManager serviceManager = new ServiceManager(); - - public static ScreenInfo getScreenInfo() { - return serviceManager.getDisplayManager().getScreenInfo(); - } - - public static void registerRotationWatcher(IRotationWatcher rotationWatcher) { - serviceManager.getWindowManager().registerRotationWatcher(rotationWatcher); - } - - public static String getDeviceName() { - return Build.MODEL; - } - - public static InputManager getInputManager() { - return serviceManager.getInputManager(); - } -} diff --git a/server/src/com/genymobile/scrcpy/EventController.java b/server/src/com/genymobile/scrcpy/EventController.java index af65821c..3e4bbe76 100644 --- a/server/src/com/genymobile/scrcpy/EventController.java +++ b/server/src/com/genymobile/scrcpy/EventController.java @@ -24,7 +24,7 @@ public class EventController { public EventController(DesktopConnection connection) { this.connection = connection; - inputManager = DeviceUtil.getInputManager(); + inputManager = Device.getInstance().getInputManager(); initPointer(); } diff --git a/server/src/com/genymobile/scrcpy/ScrCpyServer.java b/server/src/com/genymobile/scrcpy/ScrCpyServer.java index 5991495c..5b6258e5 100644 --- a/server/src/com/genymobile/scrcpy/ScrCpyServer.java +++ b/server/src/com/genymobile/scrcpy/ScrCpyServer.java @@ -7,16 +7,25 @@ public class ScrCpyServer { private static final String TAG = "scrcpy"; private static void scrcpy() throws IOException { - String deviceName = DeviceUtil.getDeviceName(); - ScreenInfo initialScreenInfo = DeviceUtil.getScreenInfo(); + String deviceName = Device.getDeviceName(); + ScreenInfo initialScreenInfo = Device.getInstance().getScreenInfo(); int width = initialScreenInfo.getLogicalWidth(); int height = initialScreenInfo.getLogicalHeight(); try (DesktopConnection connection = DesktopConnection.open(deviceName, width, height)) { + final ScreenStreamer streamer = new ScreenStreamer(connection); + Device.getInstance().setRotationListener(new Device.RotationListener() { + @Override + public void onRotationChanged(int rotation) { + streamer.reset(); + } + }); + + // asynchronous + startEventController(connection); + try { - // asynchronous - startEventController(connection); // synchronous - new ScreenStreamer(connection).streamScreen(); + streamer.streamScreen(); } catch (IOException e) { Ln.e("Screen streaming interrupted", e); } diff --git a/server/src/com/genymobile/scrcpy/ScreenStreamer.java b/server/src/com/genymobile/scrcpy/ScreenStreamer.java index 4da18b75..a48f41bb 100644 --- a/server/src/com/genymobile/scrcpy/ScreenStreamer.java +++ b/server/src/com/genymobile/scrcpy/ScreenStreamer.java @@ -1,8 +1,5 @@ package com.genymobile.scrcpy; -import android.os.RemoteException; -import android.view.IRotationWatcher; - import java.io.IOException; import java.io.InterruptedIOException; @@ -13,12 +10,6 @@ public class ScreenStreamer { public ScreenStreamer(DesktopConnection connection) { this.connection = connection; - DeviceUtil.registerRotationWatcher(new IRotationWatcher.Stub() { - @Override - public void onRotationChanged(int rotation) throws RemoteException { - reset(); - } - }); } private synchronized ScreenStreamerSession newScreenStreamerSession() {