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.
This commit is contained in:
Romain Vimont 2018-01-24 10:23:57 +01:00
parent 879941355d
commit 2c4ea6869e
4 changed files with 20 additions and 21 deletions

View file

@ -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;
}

View file

@ -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();

View file

@ -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;

View file

@ -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();
}