Handle down/up actions for BACK or POWER ON
Pressing right-click generates either BACK or POWER ON, depending on the current screen state. Forward DOWN and UP events separately to the device, to generate key events accordingly. Ref #1062 <https://github.com/Genymobile/scrcpy/issues/1062>
This commit is contained in:
@@ -85,6 +85,13 @@ public final class ControlMessage {
|
||||
return msg;
|
||||
}
|
||||
|
||||
public static ControlMessage createBackOrScreenOn(int action) {
|
||||
ControlMessage msg = new ControlMessage();
|
||||
msg.type = TYPE_BACK_OR_SCREEN_ON;
|
||||
msg.action = action;
|
||||
return msg;
|
||||
}
|
||||
|
||||
public static ControlMessage createEmpty(int type) {
|
||||
ControlMessage msg = new ControlMessage();
|
||||
msg.type = type;
|
||||
|
||||
@@ -13,6 +13,7 @@ public class ControlMessageReader {
|
||||
private static final int INJECT_TOUCH_EVENT_PAYLOAD_LENGTH = 21;
|
||||
private static final int INJECT_SCROLL_EVENT_PAYLOAD_LENGTH = 20;
|
||||
private static final int SET_SCREEN_POWER_MODE_PAYLOAD_LENGTH = 1;
|
||||
private static final int BACK_OR_SCREEN_ON_PAYLOAD_LENGTH = 1;
|
||||
|
||||
public static final int TEXT_MAX_LENGTH = 300;
|
||||
public static final int CLIPBOARD_TEXT_MAX_LENGTH = 4093;
|
||||
@@ -73,6 +74,8 @@ public class ControlMessageReader {
|
||||
msg = parseSetScreenPowerMode();
|
||||
break;
|
||||
case ControlMessage.TYPE_BACK_OR_SCREEN_ON:
|
||||
msg = parseBackOrScreenOn();
|
||||
break;
|
||||
case ControlMessage.TYPE_EXPAND_NOTIFICATION_PANEL:
|
||||
case ControlMessage.TYPE_COLLAPSE_NOTIFICATION_PANEL:
|
||||
case ControlMessage.TYPE_GET_CLIPBOARD:
|
||||
@@ -164,6 +167,14 @@ public class ControlMessageReader {
|
||||
return ControlMessage.createSetScreenPowerMode(mode);
|
||||
}
|
||||
|
||||
private ControlMessage parseBackOrScreenOn() {
|
||||
if (buffer.remaining() < BACK_OR_SCREEN_ON_PAYLOAD_LENGTH) {
|
||||
return null;
|
||||
}
|
||||
int action = toUnsigned(buffer.get());
|
||||
return ControlMessage.createBackOrScreenOn(action);
|
||||
}
|
||||
|
||||
private static Position readPosition(ByteBuffer buffer) {
|
||||
int x = buffer.getInt();
|
||||
int y = buffer.getInt();
|
||||
|
||||
@@ -26,6 +26,9 @@ public class Controller {
|
||||
private final MotionEvent.PointerProperties[] pointerProperties = new MotionEvent.PointerProperties[PointersState.MAX_POINTERS];
|
||||
private final MotionEvent.PointerCoords[] pointerCoords = new MotionEvent.PointerCoords[PointersState.MAX_POINTERS];
|
||||
|
||||
private boolean backPressed;
|
||||
private boolean powerPressed;
|
||||
|
||||
public Controller(Device device, DesktopConnection connection) {
|
||||
this.device = device;
|
||||
this.connection = connection;
|
||||
@@ -88,7 +91,7 @@ public class Controller {
|
||||
injectScroll(msg.getPosition(), msg.getHScroll(), msg.getVScroll());
|
||||
break;
|
||||
case ControlMessage.TYPE_BACK_OR_SCREEN_ON:
|
||||
pressBackOrTurnScreenOn();
|
||||
pressBackOrTurnScreenOn(msg.getAction());
|
||||
break;
|
||||
case ControlMessage.TYPE_EXPAND_NOTIFICATION_PANEL:
|
||||
device.expandNotificationPanel();
|
||||
@@ -223,8 +226,33 @@ public class Controller {
|
||||
return device.injectInputEvent(event, InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);
|
||||
}
|
||||
|
||||
private boolean pressBackOrTurnScreenOn() {
|
||||
int keycode = device.isScreenOn() ? KeyEvent.KEYCODE_BACK : KeyEvent.KEYCODE_POWER;
|
||||
return injectKeycode(keycode);
|
||||
private boolean pressBackOrTurnScreenOn(int action) {
|
||||
int keycode = -1;
|
||||
if (action == KeyEvent.ACTION_UP) {
|
||||
// if BACK or POWER were pressed, this action is the corresponding release event (regardless of the screen state)
|
||||
if (backPressed) {
|
||||
keycode = KeyEvent.KEYCODE_BACK;
|
||||
backPressed = false;
|
||||
} else if (powerPressed) {
|
||||
keycode = KeyEvent.KEYCODE_POWER;
|
||||
powerPressed = false;
|
||||
}
|
||||
}
|
||||
|
||||
if (keycode == -1) {
|
||||
keycode = device.isScreenOn() ? KeyEvent.KEYCODE_BACK : KeyEvent.KEYCODE_POWER;
|
||||
}
|
||||
|
||||
if (action == KeyEvent.ACTION_DOWN) {
|
||||
if (keycode == KeyEvent.KEYCODE_BACK) {
|
||||
backPressed = true;
|
||||
powerPressed = false;
|
||||
} else {
|
||||
backPressed = false;
|
||||
powerPressed = true;
|
||||
}
|
||||
}
|
||||
|
||||
return injectKeyEvent(action, keycode, 0, 0);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -145,6 +145,7 @@ public class ControlMessageReaderTest {
|
||||
ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
||||
DataOutputStream dos = new DataOutputStream(bos);
|
||||
dos.writeByte(ControlMessage.TYPE_BACK_OR_SCREEN_ON);
|
||||
dos.writeByte(KeyEvent.ACTION_UP);
|
||||
|
||||
byte[] packet = bos.toByteArray();
|
||||
|
||||
@@ -152,6 +153,7 @@ public class ControlMessageReaderTest {
|
||||
ControlMessage event = reader.next();
|
||||
|
||||
Assert.assertEquals(ControlMessage.TYPE_BACK_OR_SCREEN_ON, event.getType());
|
||||
Assert.assertEquals(KeyEvent.ACTION_UP, event.getAction());
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
Reference in New Issue
Block a user