From 2c4ea6869eb7975facd5cbceb4b4a2da580bb970 Mon Sep 17 00:00:00 2001 From: Romain Vimont Date: Wed, 24 Jan 2018 10:23:57 +0100 Subject: [PATCH] Do not use device as a singleton The device instance should be able to store a state (e.g. the maximum size requested by the user), so it should not be a singleton. --- .../com/genymobile/scrcpy/DesktopConnection.java | 9 +++++++-- server/src/com/genymobile/scrcpy/Device.java | 7 +------ .../com/genymobile/scrcpy/EventController.java | 10 ++++++---- .../src/com/genymobile/scrcpy/ScrCpyServer.java | 15 ++++++--------- 4 files changed, 20 insertions(+), 21 deletions(-) diff --git a/server/src/com/genymobile/scrcpy/DesktopConnection.java b/server/src/com/genymobile/scrcpy/DesktopConnection.java index f1a85beb..50b4b942 100644 --- a/server/src/com/genymobile/scrcpy/DesktopConnection.java +++ b/server/src/com/genymobile/scrcpy/DesktopConnection.java @@ -33,10 +33,15 @@ public class DesktopConnection implements Closeable { return localSocket; } - public static DesktopConnection open(String deviceName, int width, int height) throws IOException { + public static DesktopConnection open(Device device) throws IOException { LocalSocket socket = connect(SOCKET_NAME); + + ScreenInfo initialScreenInfo = device.getScreenInfo(); + int width = initialScreenInfo.getLogicalWidth(); + int height = initialScreenInfo.getLogicalHeight(); + DesktopConnection connection = new DesktopConnection(socket); - connection.send(deviceName, width, height); + connection.send(Device.getDeviceName(), width, height); return connection; } diff --git a/server/src/com/genymobile/scrcpy/Device.java b/server/src/com/genymobile/scrcpy/Device.java index 829089fe..ec5f7800 100644 --- a/server/src/com/genymobile/scrcpy/Device.java +++ b/server/src/com/genymobile/scrcpy/Device.java @@ -13,13 +13,12 @@ public class Device { 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() { + public Device() { screenInfo = readScreenInfo(); registerRotationWatcher(new IRotationWatcher.Stub() { @Override @@ -37,10 +36,6 @@ public class Device { }); } - public static Device getInstance() { - return INSTANCE; - } - public synchronized ScreenInfo getScreenInfo() { if (screenInfo == null) { screenInfo = readScreenInfo(); diff --git a/server/src/com/genymobile/scrcpy/EventController.java b/server/src/com/genymobile/scrcpy/EventController.java index 9fc07a0e..fd594dc4 100644 --- a/server/src/com/genymobile/scrcpy/EventController.java +++ b/server/src/com/genymobile/scrcpy/EventController.java @@ -13,6 +13,7 @@ import java.io.IOException; public class EventController { + private final Device device; private final InputManager inputManager; private final DesktopConnection connection; @@ -22,9 +23,10 @@ public class EventController { private final MotionEvent.PointerProperties[] pointerProperties = { new MotionEvent.PointerProperties() }; private final MotionEvent.PointerCoords[] pointerCoords = { new MotionEvent.PointerCoords() }; - public EventController(DesktopConnection connection) { + public EventController(Device device, DesktopConnection connection) { + this.device = device; this.connection = connection; - inputManager = Device.getInstance().getInputManager(); + inputManager = device.getInputManager(); initPointer(); } @@ -102,7 +104,7 @@ public class EventController { if (action == MotionEvent.ACTION_DOWN) { lastMouseDown = now; } - Point point = Device.getInstance().getPhysicalPoint(position); + Point point = device.getPhysicalPoint(position); if (point == null) { // ignore event return false; @@ -114,7 +116,7 @@ public class EventController { private boolean injectScroll(Position position, int hScroll, int vScroll) { long now = SystemClock.uptimeMillis(); - Point point = Device.getInstance().getPhysicalPoint(position); + Point point = device.getPhysicalPoint(position); if (point == null) { // ignore event return false; diff --git a/server/src/com/genymobile/scrcpy/ScrCpyServer.java b/server/src/com/genymobile/scrcpy/ScrCpyServer.java index 5b6258e5..c5ef3b9b 100644 --- a/server/src/com/genymobile/scrcpy/ScrCpyServer.java +++ b/server/src/com/genymobile/scrcpy/ScrCpyServer.java @@ -7,13 +7,10 @@ public class ScrCpyServer { private static final String TAG = "scrcpy"; private static void scrcpy() throws IOException { - 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 Device device = new Device(); + try (DesktopConnection connection = DesktopConnection.open(device)) { final ScreenStreamer streamer = new ScreenStreamer(connection); - Device.getInstance().setRotationListener(new Device.RotationListener() { + device.setRotationListener(new Device.RotationListener() { @Override public void onRotationChanged(int rotation) { streamer.reset(); @@ -21,7 +18,7 @@ public class ScrCpyServer { }); // asynchronous - startEventController(connection); + startEventController(device, connection); try { // synchronous @@ -32,12 +29,12 @@ public class ScrCpyServer { } } - private static void startEventController(final DesktopConnection connection) { + private static void startEventController(final Device device, final DesktopConnection connection) { new Thread(new Runnable() { @Override public void run() { try { - new EventController(connection).control(); + new EventController(device, connection).control(); } catch (IOException e) { e.printStackTrace(); }