Display and control your Android device
Do not use device as a singleton
Romain Vimont2c4ea6869eb7
diff --git a/server/src/com/genymobile/scrcpy/DesktopConnection.java b/server/src/com/genymobile/scrcpy/DesktopConnection.javaindex 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.javaindex 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.javaindex 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 eventreturn 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 eventreturn false;diff --git a/server/src/com/genymobile/scrcpy/ScrCpyServer.java b/server/src/com/genymobile/scrcpy/ScrCpyServer.javaindex 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() {@Overridepublic 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() {@Overridepublic void run() {try {- new EventController(connection).control();+ new EventController(device, connection).control();} catch (IOException e) {e.printStackTrace();}