Skip to content

Commit b7e0f52

Browse files
Add files via upload
1 parent 80431e6 commit b7e0f52

1 file changed

Lines changed: 76 additions & 16 deletions

File tree

app/src/main/kotlin/com/google/ai/sample/ScreenOperatorAccessibilityService.kt

Lines changed: 76 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1043,21 +1043,51 @@ class ScreenOperatorAccessibilityService : AccessibilityService() {
10431043
}
10441044

10451045
/**
1046-
* Scroll down on the screen
1046+
* Scroll down on the screen using gesture
10471047
*/
10481048
fun scrollDown() {
10491049
Log.d(TAG, "Scrolling down")
10501050
showToast("Scrolle nach unten...", false)
10511051

10521052
try {
1053-
// Use the global action to scroll down
1054-
val result = performGlobalAction(GLOBAL_ACTION_SCROLL_DOWN)
1053+
// Get display metrics to calculate swipe coordinates
1054+
val displayMetrics = resources.displayMetrics
1055+
val screenHeight = displayMetrics.heightPixels
1056+
val screenWidth = displayMetrics.widthPixels
10551057

1056-
if (result) {
1057-
Log.d(TAG, "Successfully scrolled down")
1058-
showToast("Erfolgreich nach unten gescrollt", false)
1059-
} else {
1060-
Log.e(TAG, "Failed to scroll down")
1058+
// Create a path for the gesture (swipe from middle-bottom to middle-top)
1059+
val swipePath = Path()
1060+
swipePath.moveTo(screenWidth / 2f, screenHeight * 0.7f) // Start from 70% down the screen
1061+
swipePath.lineTo(screenWidth / 2f, screenHeight * 0.3f) // Move to 30% down the screen
1062+
1063+
// Create a gesture builder and add the swipe
1064+
val gestureBuilder = GestureDescription.Builder()
1065+
val gesture = GestureDescription.StrokeDescription(
1066+
swipePath,
1067+
0, // start time
1068+
300 // duration in milliseconds
1069+
)
1070+
gestureBuilder.addStroke(gesture)
1071+
1072+
// Dispatch the gesture
1073+
val result = dispatchGesture(
1074+
gestureBuilder.build(),
1075+
object : GestureResultCallback() {
1076+
override fun onCompleted(gestureDescription: GestureDescription) {
1077+
Log.d(TAG, "Scroll down gesture completed")
1078+
showToast("Erfolgreich nach unten gescrollt", false)
1079+
}
1080+
1081+
override fun onCancelled(gestureDescription: GestureDescription) {
1082+
Log.e(TAG, "Scroll down gesture cancelled")
1083+
showToast("Scrollen nach unten abgebrochen", true)
1084+
}
1085+
},
1086+
null // handler
1087+
)
1088+
1089+
if (!result) {
1090+
Log.e(TAG, "Failed to dispatch scroll down gesture")
10611091
showToast("Fehler beim Scrollen nach unten", true)
10621092
}
10631093
} catch (e: Exception) {
@@ -1067,21 +1097,51 @@ class ScreenOperatorAccessibilityService : AccessibilityService() {
10671097
}
10681098

10691099
/**
1070-
* Scroll up on the screen
1100+
* Scroll up on the screen using gesture
10711101
*/
10721102
fun scrollUp() {
10731103
Log.d(TAG, "Scrolling up")
10741104
showToast("Scrolle nach oben...", false)
10751105

10761106
try {
1077-
// Use the global action to scroll up
1078-
val result = performGlobalAction(GLOBAL_ACTION_SCROLL_UP)
1107+
// Get display metrics to calculate swipe coordinates
1108+
val displayMetrics = resources.displayMetrics
1109+
val screenHeight = displayMetrics.heightPixels
1110+
val screenWidth = displayMetrics.widthPixels
10791111

1080-
if (result) {
1081-
Log.d(TAG, "Successfully scrolled up")
1082-
showToast("Erfolgreich nach oben gescrollt", false)
1083-
} else {
1084-
Log.e(TAG, "Failed to scroll up")
1112+
// Create a path for the gesture (swipe from middle-top to middle-bottom)
1113+
val swipePath = Path()
1114+
swipePath.moveTo(screenWidth / 2f, screenHeight * 0.3f) // Start from 30% down the screen
1115+
swipePath.lineTo(screenWidth / 2f, screenHeight * 0.7f) // Move to 70% down the screen
1116+
1117+
// Create a gesture builder and add the swipe
1118+
val gestureBuilder = GestureDescription.Builder()
1119+
val gesture = GestureDescription.StrokeDescription(
1120+
swipePath,
1121+
0, // start time
1122+
300 // duration in milliseconds
1123+
)
1124+
gestureBuilder.addStroke(gesture)
1125+
1126+
// Dispatch the gesture
1127+
val result = dispatchGesture(
1128+
gestureBuilder.build(),
1129+
object : GestureResultCallback() {
1130+
override fun onCompleted(gestureDescription: GestureDescription) {
1131+
Log.d(TAG, "Scroll up gesture completed")
1132+
showToast("Erfolgreich nach oben gescrollt", false)
1133+
}
1134+
1135+
override fun onCancelled(gestureDescription: GestureDescription) {
1136+
Log.e(TAG, "Scroll up gesture cancelled")
1137+
showToast("Scrollen nach oben abgebrochen", true)
1138+
}
1139+
},
1140+
null // handler
1141+
)
1142+
1143+
if (!result) {
1144+
Log.e(TAG, "Failed to dispatch scroll up gesture")
10851145
showToast("Fehler beim Scrollen nach oben", true)
10861146
}
10871147
} catch (e: Exception) {

0 commit comments

Comments
 (0)