fix android input service

This commit is contained in:
csf 2022-04-10 23:05:04 +08:00
parent d054da3404
commit 62f5352263
2 changed files with 41 additions and 35 deletions

View File

@ -5,12 +5,9 @@ import android.accessibilityservice.GestureDescription
import android.content.Context import android.content.Context
import android.graphics.Path import android.graphics.Path
import android.os.Build import android.os.Build
import android.os.Handler
import android.os.Looper
import android.util.Log import android.util.Log
import android.view.accessibility.AccessibilityEvent import android.view.accessibility.AccessibilityEvent
import androidx.annotation.RequiresApi import androidx.annotation.RequiresApi
import kotlin.concurrent.thread
class InputService : AccessibilityService() { class InputService : AccessibilityService() {
@ -19,6 +16,13 @@ class InputService : AccessibilityService() {
val isOpen: Boolean val isOpen: Boolean
get() = ctx != null get() = ctx != null
} }
private external fun init(ctx: Context)
init {
System.loadLibrary("rustdesk")
}
private val logTag = "input service" private val logTag = "input service"
private var leftIsDown = false private var leftIsDown = false
private var mPath = Path() private var mPath = Path()
@ -28,10 +32,21 @@ class InputService : AccessibilityService() {
@RequiresApi(Build.VERSION_CODES.N) @RequiresApi(Build.VERSION_CODES.N)
fun rustMouseInput(mask: Int, _x: Int, _y: Int) { fun rustMouseInput(mask: Int, _x: Int, _y: Int) {
val x = if (_x < 0) {
0
} else {
_x
}
val y = if (_y < 0) {
0
} else {
_y
}
if (!(mask == 9 || mask == 10)) { if (!(mask == 9 || mask == 10)) {
mouseX = _x * INFO.scale mouseX = x * INFO.scale
mouseY = _y * INFO.scale mouseY = y * INFO.scale
} }
// left button down ,was up // left button down ,was up
@ -41,7 +56,7 @@ class InputService : AccessibilityService() {
} }
// left down ,was down // left down ,was down
if (mask == 9) { if (leftIsDown) {
continueGesture(mouseX, mouseY) continueGesture(mouseX, mouseY)
} }
@ -64,30 +79,24 @@ class InputService : AccessibilityService() {
@RequiresApi(Build.VERSION_CODES.N) @RequiresApi(Build.VERSION_CODES.N)
private fun endGesture(x: Int, y: Int) { private fun endGesture(x: Int, y: Int) {
try {
mPath.lineTo(x.toFloat(), y.toFloat()) mPath.lineTo(x.toFloat(), y.toFloat())
var duration = System.currentTimeMillis() - mLastGestureStartTime
if (duration <= 0) {
duration = 1
}
val stroke = GestureDescription.StrokeDescription( val stroke = GestureDescription.StrokeDescription(
mPath, mPath,
0, 0,
System.currentTimeMillis() - mLastGestureStartTime duration
) )
val builder = GestureDescription.Builder() val builder = GestureDescription.Builder()
builder.addStroke(stroke) builder.addStroke(stroke)
Log.d(logTag, "end gesture $x $y") Log.d(logTag, "end gesture x:$x y:$y time:$duration")
dispatchGesture(builder.build(), object : GestureResultCallback() { dispatchGesture(builder.build(), null, null)
override fun onCompleted(gestureDescription: GestureDescription) { } catch (e: Exception) {
super.onCompleted(gestureDescription) Log.e(logTag, "endGesture error:$e")
} }
override fun onCancelled(gestureDescription: GestureDescription) {
super.onCancelled(gestureDescription)
}
}, null)
}
private external fun init(ctx: Context)
init {
System.loadLibrary("rustdesk")
} }
@RequiresApi(Build.VERSION_CODES.O) @RequiresApi(Build.VERSION_CODES.O)

View File

@ -82,10 +82,7 @@ class ServerModel with ChangeNotifier {
toggleAudio() async { toggleAudio() async {
if(!_audioOk && !await PermissionManager.check("audio")){ if(!_audioOk && !await PermissionManager.check("audio")){
debugPrint("toggleAudio 无权限 开始获取权限");
final res = await PermissionManager.request("audio"); final res = await PermissionManager.request("audio");
debugPrint("权限请求结果:$res");
if(!res){ if(!res){
// TODO handle fail // TODO handle fail
return; return;