Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inject UTF-8 text #1426

Closed
wants to merge 3 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Move injection methods to Device
Only the main injection method was exposed on Device, the convenience
methods were implemented in Controller.

For consistency, move them all to the Device class.
rom1v committed May 25, 2020
commit 4bbabfb4ef5f1105215dc2bb8a282ffce221bbcc
30 changes: 6 additions & 24 deletions server/src/main/java/com/genymobile/scrcpy/Controller.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package com.genymobile.scrcpy;

import com.genymobile.scrcpy.wrappers.InputManager;

import android.os.SystemClock;
import android.view.InputDevice;
import android.view.InputEvent;
import android.view.KeyCharacterMap;
import android.view.KeyEvent;
import android.view.MotionEvent;
@@ -50,7 +47,7 @@ private void initPointers() {
public void control() throws IOException {
// on start, power on the device
if (!device.isScreenOn()) {
injectKeycode(KeyEvent.KEYCODE_POWER);
device.injectKeycode(KeyEvent.KEYCODE_POWER);

// dirty hack
// After POWER is injected, the device is powered on asynchronously.
@@ -133,7 +130,7 @@ private void handleEvent() throws IOException {
}

private boolean injectKeycode(int action, int keycode, int metaState) {
return injectKeyEvent(action, keycode, 0, metaState);
return device.injectKeyEvent(action, keycode, 0, metaState);
}

private boolean injectChar(char c) {
@@ -144,7 +141,7 @@ private boolean injectChar(char c) {
return false;
}
for (KeyEvent event : events) {
if (!injectEvent(event)) {
if (!device.injectEvent(event)) {
return false;
}
}
@@ -200,7 +197,7 @@ private boolean injectTouch(int action, long pointerId, Position position, float
MotionEvent event = MotionEvent
.obtain(lastTouchDown, now, action, pointerCount, pointerProperties, pointerCoords, 0, buttons, 1f, 1f, DEVICE_ID_VIRTUAL, 0,
InputDevice.SOURCE_TOUCHSCREEN, 0);
return injectEvent(event);
return device.injectEvent(event);
}

private boolean injectScroll(Position position, int hScroll, int vScroll) {
@@ -223,26 +220,11 @@ private boolean injectScroll(Position position, int hScroll, int vScroll) {
MotionEvent event = MotionEvent
.obtain(lastTouchDown, now, MotionEvent.ACTION_SCROLL, 1, pointerProperties, pointerCoords, 0, 0, 1f, 1f, DEVICE_ID_VIRTUAL, 0,
InputDevice.SOURCE_TOUCHSCREEN, 0);
return injectEvent(event);
}

private boolean injectKeyEvent(int action, int keyCode, int repeat, int metaState) {
long now = SystemClock.uptimeMillis();
KeyEvent event = new KeyEvent(now, now, action, keyCode, repeat, metaState, KeyCharacterMap.VIRTUAL_KEYBOARD, 0, 0,
InputDevice.SOURCE_KEYBOARD);
return injectEvent(event);
}

private boolean injectKeycode(int keyCode) {
return injectKeyEvent(KeyEvent.ACTION_DOWN, keyCode, 0, 0) && injectKeyEvent(KeyEvent.ACTION_UP, keyCode, 0, 0);
}

private boolean injectEvent(InputEvent event) {
return device.injectInputEvent(event, InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);
return device.injectEvent(event);
}

private boolean pressBackOrTurnScreenOn() {
int keycode = device.isScreenOn() ? KeyEvent.KEYCODE_BACK : KeyEvent.KEYCODE_POWER;
return injectKeycode(keycode);
return device.injectKeycode(keycode);
}
}
21 changes: 20 additions & 1 deletion server/src/main/java/com/genymobile/scrcpy/Device.java
Original file line number Diff line number Diff line change
@@ -10,8 +10,12 @@
import android.graphics.Rect;
import android.os.Build;
import android.os.IBinder;
import android.os.SystemClock;
import android.view.IRotationWatcher;
import android.view.InputDevice;
import android.view.InputEvent;
import android.view.KeyCharacterMap;
import android.view.KeyEvent;

import java.util.concurrent.atomic.AtomicBoolean;

@@ -147,7 +151,7 @@ public boolean supportsInputEvents() {
return supportsInputEvents;
}

public boolean injectInputEvent(InputEvent inputEvent, int mode) {
public boolean injectEvent(InputEvent inputEvent, int mode) {
if (!supportsInputEvents()) {
throw new AssertionError("Could not inject input event if !supportsInputEvents()");
}
@@ -159,6 +163,21 @@ public boolean injectInputEvent(InputEvent inputEvent, int mode) {
return serviceManager.getInputManager().injectInputEvent(inputEvent, mode);
}

public boolean injectEvent(InputEvent event) {
return injectEvent(event, InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);
}

public boolean injectKeyEvent(int action, int keyCode, int repeat, int metaState) {
long now = SystemClock.uptimeMillis();
KeyEvent event = new KeyEvent(now, now, action, keyCode, repeat, metaState, KeyCharacterMap.VIRTUAL_KEYBOARD, 0, 0,
InputDevice.SOURCE_KEYBOARD);
return injectEvent(event);
}

public boolean injectKeycode(int keyCode) {
return injectKeyEvent(KeyEvent.ACTION_DOWN, keyCode, 0, 0) && injectKeyEvent(KeyEvent.ACTION_UP, keyCode, 0, 0);
}

public boolean isScreenOn() {
return serviceManager.getPowerManager().isScreenOn();
}