首汽抢单功能的实现基于AccessibilityService

package com.gl.accessibilityservice.service
import android.accessibilityservice.AccessibilityService
import android.accessibilityservice.GestureDescription
import android.accessibilityservice.GestureDescription.StrokeDescription
import android.annotation.SuppressLint
import android.graphics.Path
import android.util.Log
import android.view.accessibility.AccessibilityEvent
@SuppressLint("NewApi")
class Accessibility : AccessibilityService() {
private val TAG = "TAG"
var top_type = ""
var start_distance = ""
var end_distance = ""
var start_address = ""
var end_address = ""
var tapX0 = 0
var tapY0 = 0
var tapX1 = 0
var tapY1 = 0
var isClick = false
var isStart_distance = false
var isEnd_distance = false
override fun onServiceConnected() {
super.onServiceConnected()
}
override fun onAccessibilityEvent(event: AccessibilityEvent?) {
when (event?.eventType) {
AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED -> {
if (isClick) isClick = false
if (isStart_distance) isStart_distance = false
if (isEnd_distance) isEnd_distance = false
Log.d(TAG, "onAccessibilityEvent: ...........................")
val list =
event.source.findAccessibilityNodeInfosByViewId("com.ichinait.gbdriver:id/tv_scramble_order")
for (info in list) {
if (info.text == "抢") {
val boundsInScreen = info.toString().substring("boundsInScreen: Rect", ";")
tapX0 = boundsInScreen.substring("(", ",").toInt()
tapY0 = boundsInScreen.substring(", ", " -").toInt()
tapX1 = boundsInScreen.substring("- ", ",").toInt()
tapY1 = boundsInScreen.substring("- ", ")").substring(", ").toInt()
val list_top_type =
event.source.findAccessibilityNodeInfosByViewId("com.ichinait.gbdriver:id/tv_rob_top_type")
for (topType in list_top_type) {
top_type = topType.text.toString()
Log.d(TAG, top_type)
}
val list_start_address =
event.source.findAccessibilityNodeInfosByViewId("com.ichinait.gbdriver:id/tv_rob_start_address")
for (startAddress in list_start_address) {
start_address = startAddress.text.toString()
Log.d(TAG, start_address)
}
val list_end_address =
event.source.findAccessibilityNodeInfosByViewId("com.ichinait.gbdriver:id/tv_rob_end_address")
for (endAddress in list_end_address) {
end_address = endAddress.text.toString()
Log.d(TAG, end_address)
}
while (!isClick) {
if (!isStart_distance) {
val list_start_distance =
event.source.findAccessibilityNodeInfosByViewId("com.ichinait.gbdriver:id/tv_rob_start_distance")
for (startDistance in list_start_distance) {
if (startDistance.text != "···") {
start_distance = startDistance.text.toString()
isStart_distance = true
}
}
}
if (!isEnd_distance) {
val list_end_distance =
event.source.findAccessibilityNodeInfosByViewId("com.ichinait.gbdriver:id/tv_rob_end_distance")
for (endDistance in list_end_distance) {
if (endDistance.text != "···") {
end_distance = endDistance.text.toString()
isEnd_distance = true
}
}
}
if (isStart_distance && isEnd_distance) {
isClick = true
Log.d(TAG, start_distance)
Log.d(TAG, end_distance)
if (top_type == "即时用车") {
if (kmToM(start_distance) <= 2000 && kmToM(end_distance) >= 5000) {
Log.d(TAG, "抢")
tap((tapX0 + tapX1) / 2, (tapY0 + tapY1) / 2)
} else if (kmToM(start_distance) <= 3500 && kmToM(end_distance) >= 15000) {
Log.d(TAG, "行程远,抢")
tap((tapX0 + tapX1) / 2, (tapY0 + tapY1) / 2)
} else {
Log.d(TAG, "不抢")
tap(100, 100)
}
} else {
Log.d(TAG, "非即时用车不抢")
tap(100, 100)
}
}
}
}
}
}
}
}
private fun kmToM(s: String): Int {
var m = 0
if (s.substring(s.length - 2, s.length) == "km") {
val a = s.substring("", ".").length
for (i in 0 until a) {
m += stringToInt(s.substring(i, i + 1)) * aa(a - i)
}
if (s.substring(".", "km").length == 1) {
m += stringToInt(s.substring(".", "km")) * 100
}
}
return m
}
private fun aa(a: Int): Int {
if (a == 1)
return 1000
if (a == 2)
return 10000
if (a == 3)
return 100000
if (a == 4)
return 1000000
if (a == 5)
return 10000000
return 0
}
private fun stringToInt(s: String): Int {
if (s == "0") {
return 0
}
if (s == "1") {
return 1
}
if (s == "2") {
return 2
}
if (s == "3") {
return 3
}
if (s == "4") {
return 4
}
if (s == "5") {
return 5
}
if (s == "6") {
return 6
}
if (s == "7") {
return 7
}
if (s == "8") {
return 8
}
if (s == "9") {
return 9
}
return -1
}
fun String.substring(startStr: String = "", endStr: String = ""): String {
val start = if (startStr.isEmpty()) {
0
} else {
this.indexOf(startStr) + startStr.length
}
val str = this.substring(start)
val end = if (endStr.isEmpty()) {
str.length
} else {
str.indexOf(endStr)
}
return str.substring(0, end)
}
private fun tap(x: Int, y: Int) {
val builder = GestureDescription.Builder()
val p = Path()
p.moveTo(x.toFloat(), y.toFloat())
builder.addStroke(StrokeDescription(p, 0L, 500L))
val gesture = builder.build()
dispatchGesture(gesture, object : GestureResultCallback() {
override fun onCompleted(gestureDescription: GestureDescription) {
super.onCompleted(gestureDescription)
}
override fun onCancelled(gestureDescription: GestureDescription) {
super.onCancelled(gestureDescription)
}
}, null)
}
private fun swipe(
xStart: Int,
yStart: Int,
xEnd: Int,
yEnd: Int,
startTime: Long,
duration: Long
) {
val builder = GestureDescription.Builder()
val p = Path()
p.moveTo(xStart.toFloat(), yStart.toFloat())
p.lineTo(xEnd.toFloat(), yEnd.toFloat())
builder.addStroke(StrokeDescription(p, startTime, duration))
val gesture = builder.build()
dispatchGesture(gesture, object : GestureResultCallback() {
override fun onCompleted(gestureDescription: GestureDescription) {
super.onCompleted(gestureDescription)
}
override fun onCancelled(gestureDescription: GestureDescription) {
super.onCancelled(gestureDescription)
}
}, null)
}
override fun onInterrupt() {
}
}