From 7330dc70f3705be0d8a6ce18236cec002b7dbc61 Mon Sep 17 00:00:00 2001 From: fufesou <13586388+fufesou@users.noreply.github.com> Date: Fri, 20 Jun 2025 17:50:28 +0800 Subject: [PATCH] fix: android 7.1, input, crash (#12129) Signed-off-by: fufesou --- .../com/carriez/flutter_hbb/InputService.kt | 56 +++++++++++++++---- .../com/carriez/flutter_hbb/MainActivity.kt | 2 +- 2 files changed, 46 insertions(+), 12 deletions(-) diff --git a/flutter/android/app/src/main/kotlin/com/carriez/flutter_hbb/InputService.kt b/flutter/android/app/src/main/kotlin/com/carriez/flutter_hbb/InputService.kt index 8ea67fe0b..3ca83fbac 100644 --- a/flutter/android/app/src/main/kotlin/com/carriez/flutter_hbb/InputService.kt +++ b/flutter/android/app/src/main/kotlin/com/carriez/flutter_hbb/InputService.kt @@ -70,7 +70,7 @@ class InputService : AccessibilityService() { private val logTag = "input service" private var leftIsDown = false - private val touchPath = Path() + private var touchPath = Path() private var stroke: GestureDescription.StrokeDescription? = null private var lastTouchGestureStartTime = 0L private var mouseX = 0 @@ -278,7 +278,11 @@ class InputService : AccessibilityService() { } private fun startGesture(x: Int, y: Int) { - touchPath.reset() + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + touchPath.reset() + } else { + touchPath = Path() + } touchPath.moveTo(x.toFloat(), y.toFloat()) lastTouchGestureStartTime = System.currentTimeMillis() lastX = x @@ -333,19 +337,49 @@ class InputService : AccessibilityService() { @RequiresApi(Build.VERSION_CODES.N) private fun continueGesture(x: Int, y: Int) { - doDispatchGesture(x, y, true) - touchPath.reset() - touchPath.moveTo(x.toFloat(), y.toFloat()) - lastTouchGestureStartTime = System.currentTimeMillis() - lastX = x - lastY = y + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + doDispatchGesture(x, y, true) + touchPath.reset() + touchPath.moveTo(x.toFloat(), y.toFloat()) + lastTouchGestureStartTime = System.currentTimeMillis() + lastX = x + lastY = y + } else { + touchPath.lineTo(x.toFloat(), y.toFloat()) + } + } + + @RequiresApi(Build.VERSION_CODES.N) + private fun endGestureBelowO(x: Int, y: Int) { + try { + touchPath.lineTo(x.toFloat(), y.toFloat()) + var duration = System.currentTimeMillis() - lastTouchGestureStartTime + if (duration <= 0) { + duration = 1 + } + val stroke = GestureDescription.StrokeDescription( + touchPath, + 0, + duration + ) + val builder = GestureDescription.Builder() + builder.addStroke(stroke) + Log.d(logTag, "end gesture x:$x y:$y time:$duration") + dispatchGesture(builder.build(), null, null) + } catch (e: Exception) { + Log.e(logTag, "endGesture error:$e") + } } @RequiresApi(Build.VERSION_CODES.N) private fun endGesture(x: Int, y: Int) { - doDispatchGesture(x, y, false) - touchPath.reset() - stroke = null + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + doDispatchGesture(x, y, false) + touchPath.reset() + stroke = null + } else { + endGestureBelowO(x, y) + } } @RequiresApi(Build.VERSION_CODES.N) diff --git a/flutter/android/app/src/main/kotlin/com/carriez/flutter_hbb/MainActivity.kt b/flutter/android/app/src/main/kotlin/com/carriez/flutter_hbb/MainActivity.kt index 5c54c18fb..a19c2ae9d 100644 --- a/flutter/android/app/src/main/kotlin/com/carriez/flutter_hbb/MainActivity.kt +++ b/flutter/android/app/src/main/kotlin/com/carriez/flutter_hbb/MainActivity.kt @@ -316,7 +316,7 @@ class MainActivity : FlutterActivity() { codecObject.put("mime_type", mime_type) val caps = codec.getCapabilitiesForType(mime_type) if (codec.isEncoder) { - // Encoder‘s max_height and max_width are interchangeable + // Encoder's max_height and max_width are interchangeable if (!caps.videoCapabilities.isSizeSupported(w,h) && !caps.videoCapabilities.isSizeSupported(h,w)) { return@forEach }