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.
hidpi
Romain Vimont 6 years ago
parent 879941355d
commit 2c4ea6869e

@ -33,10 +33,15 @@ public class DesktopConnection implements Closeable {
return localSocket; 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); LocalSocket socket = connect(SOCKET_NAME);
ScreenInfo initialScreenInfo = device.getScreenInfo();
int width = initialScreenInfo.getLogicalWidth();
int height = initialScreenInfo.getLogicalHeight();
DesktopConnection connection = new DesktopConnection(socket); DesktopConnection connection = new DesktopConnection(socket);
connection.send(deviceName, width, height); connection.send(Device.getDeviceName(), width, height);
return connection; return connection;
} }

@ -13,13 +13,12 @@ public class Device {
void onRotationChanged(int rotation); void onRotationChanged(int rotation);
} }
private static final Device INSTANCE = new Device();
private final ServiceManager serviceManager = new ServiceManager(); private final ServiceManager serviceManager = new ServiceManager();
private ScreenInfo screenInfo; private ScreenInfo screenInfo;
private RotationListener rotationListener; private RotationListener rotationListener;
private Device() { public Device() {
screenInfo = readScreenInfo(); screenInfo = readScreenInfo();
registerRotationWatcher(new IRotationWatcher.Stub() { registerRotationWatcher(new IRotationWatcher.Stub() {
@Override @Override
@ -37,10 +36,6 @@ public class Device {
}); });
} }
public static Device getInstance() {
return INSTANCE;
}
public synchronized ScreenInfo getScreenInfo() { public synchronized ScreenInfo getScreenInfo() {
if (screenInfo == null) { if (screenInfo == null) {
screenInfo = readScreenInfo(); screenInfo = readScreenInfo();

@ -13,6 +13,7 @@ import java.io.IOException;
public class EventController { public class EventController {
private final Device device;
private final InputManager inputManager; private final InputManager inputManager;
private final DesktopConnection connection; private final DesktopConnection connection;
@ -22,9 +23,10 @@ public class EventController {
private final MotionEvent.PointerProperties[] pointerProperties = { new MotionEvent.PointerProperties() }; private final MotionEvent.PointerProperties[] pointerProperties = { new MotionEvent.PointerProperties() };
private final MotionEvent.PointerCoords[] pointerCoords = { new MotionEvent.PointerCoords() }; private final MotionEvent.PointerCoords[] pointerCoords = { new MotionEvent.PointerCoords() };
public EventController(DesktopConnection connection) { public EventController(Device device, DesktopConnection connection) {
this.device = device;
this.connection = connection; this.connection = connection;
inputManager = Device.getInstance().getInputManager(); inputManager = device.getInputManager();
initPointer(); initPointer();
} }
@ -102,7 +104,7 @@ public class EventController {
if (action == MotionEvent.ACTION_DOWN) { if (action == MotionEvent.ACTION_DOWN) {
lastMouseDown = now; lastMouseDown = now;
} }
Point point = Device.getInstance().getPhysicalPoint(position); Point point = device.getPhysicalPoint(position);
if (point == null) { if (point == null) {
// ignore event // ignore event
return false; return false;
@ -114,7 +116,7 @@ public class EventController {
private boolean injectScroll(Position position, int hScroll, int vScroll) { private boolean injectScroll(Position position, int hScroll, int vScroll) {
long now = SystemClock.uptimeMillis(); long now = SystemClock.uptimeMillis();
Point point = Device.getInstance().getPhysicalPoint(position); Point point = device.getPhysicalPoint(position);
if (point == null) { if (point == null) {
// ignore event // ignore event
return false; return false;

@ -7,13 +7,10 @@ public class ScrCpyServer {
private static final String TAG = "scrcpy"; private static final String TAG = "scrcpy";
private static void scrcpy() throws IOException { private static void scrcpy() throws IOException {
String deviceName = Device.getDeviceName(); final Device device = new Device();
ScreenInfo initialScreenInfo = Device.getInstance().getScreenInfo(); try (DesktopConnection connection = DesktopConnection.open(device)) {
int width = initialScreenInfo.getLogicalWidth();
int height = initialScreenInfo.getLogicalHeight();
try (DesktopConnection connection = DesktopConnection.open(deviceName, width, height)) {
final ScreenStreamer streamer = new ScreenStreamer(connection); final ScreenStreamer streamer = new ScreenStreamer(connection);
Device.getInstance().setRotationListener(new Device.RotationListener() { device.setRotationListener(new Device.RotationListener() {
@Override @Override
public void onRotationChanged(int rotation) { public void onRotationChanged(int rotation) {
streamer.reset(); streamer.reset();
@ -21,7 +18,7 @@ public class ScrCpyServer {
}); });
// asynchronous // asynchronous
startEventController(connection); startEventController(device, connection);
try { try {
// synchronous // 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() { new Thread(new Runnable() {
@Override @Override
public void run() { public void run() {
try { try {
new EventController(connection).control(); new EventController(device, connection).control();
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }

Loading…
Cancel
Save