Skip to content

Commit

Permalink
update finder api with coroutine
Browse files Browse the repository at this point in the history
  • Loading branch information
Krosxx committed Dec 19, 2022
1 parent 27154e4 commit 8b2d6d2
Show file tree
Hide file tree
Showing 11 changed files with 79 additions and 90 deletions.
2 changes: 1 addition & 1 deletion .idea/codeStyles/codeStyleConfig.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ abstract class AccessibilityApi : AccessibilityService(), BaseServiceApi {
@JvmOverloads
@JvmStatic
@Throws(NeedAccessibilityException::class)
fun waitAccessibility(waitMillis: Long = 30000, cls: Class<*>): Boolean {
suspend fun waitAccessibility(waitMillis: Long = 30000, cls: Class<*>): Boolean {

val se = if (cls == BASE_SERVICE_CLS) isBaseServiceEnable
else isGestureServiceEnable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import androidx.annotation.RequiresApi
import cn.vove7.andro_accessibility_api.AccessibilityApi
import cn.vove7.andro_accessibility_api.AppScope
import cn.vove7.andro_accessibility_api.utils.whileWaitTime
import kotlinx.coroutines.delay
import java.lang.Thread.sleep
import kotlin.math.min

Expand Down Expand Up @@ -37,18 +38,14 @@ fun screenShot(): Boolean = AccessibilityApi.requireBase.screenShot()
@RequiresApi(Build.VERSION_CODES.N)
fun splitScreen(): Boolean = AccessibilityApi.requireBase.splitScreen()

fun waitForApp(pkg: String, waitTime: Long = 30000): Boolean {
if (!AccessibilityApi.isBaseServiceEnable) {
return false
}
suspend fun waitForApp(pkg: String, waitTime: Long = 30000): Boolean {
return waitForPage(AppScope(pkg, ""), waitTime)
}

fun waitForPage(scope: AppScope, waitTime: Long = 30000): Boolean {
return whileWaitTime(min(waitTime, 30000)) {
if (AccessibilityApi.currentScope == scope) true else {
sleep(100)
null
}
suspend fun waitForPage(scope: AppScope, waitTime: Long = 30000): Boolean {
requireBaseAccessibility()
return whileWaitTime(min(waitTime, 30000), 100) {
if (AccessibilityApi.currentScope == scope) true
else null
} ?: false
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,19 @@ fun requireBaseAccessibility(autoJump: Boolean = false) {
AccessibilityApi.requireBaseAccessibility(autoJump)
}

fun waitBaseAccessibility(waitMillis: Long = 30000) {
suspend fun waitBaseAccessibility(waitMillis: Long = 30000) {
AccessibilityApi.waitAccessibility(waitMillis, AccessibilityApi.BASE_SERVICE_CLS)
}

fun requireGestureAccessibility(autoJump: Boolean = false) {
AccessibilityApi.requireGestureAccessibility(autoJump)
}

fun waitGestureAccessibility(waitMillis: Long = 30000) {
suspend fun waitGestureAccessibility(waitMillis: Long = 30000) {
AccessibilityApi.waitAccessibility(waitMillis, AccessibilityApi.GESTURE_SERVICE_CLS)
}

fun waitAccessibility(waitMillis: Long = 30000, cls: Class<*>): Boolean {
suspend fun waitAccessibility(waitMillis: Long = 30000, cls: Class<*>): Boolean {
return AccessibilityApi.waitAccessibility(waitMillis, cls)
}

Expand Down Expand Up @@ -90,7 +90,7 @@ fun editor(): ConditionGroup {
* @param depths Array<Int>
* @return ViewFindBuilder
*/
fun withDepths(vararg depths: Int): ViewNode? {
suspend fun withDepths(vararg depths: Int): ViewNode? {
return ViewFinder.findByDepths(*depths)
}

Expand Down Expand Up @@ -139,28 +139,28 @@ private fun ViewNode.printWithChild(
}
}

fun findWith(
suspend fun findWith(
includeInvisible: Boolean = false,
predicate: (AccessibilityNodeInfo) -> Boolean
): ViewNode? {
return SF.where(predicate).findFirst(includeInvisible)
}

fun findAllWith(
suspend fun findAllWith(
includeInvisible: Boolean = false,
predicate: (AccessibilityNodeInfo) -> Boolean
): Array<ViewNode> {
return SF.where(predicate).findAll(includeInvisible)
}

fun ViewNode.findWith(
suspend fun ViewNode.findWith(
includeInvisible: Boolean = false,
predicate: (AccessibilityNodeInfo) -> Boolean
): ViewNode? {
return SmartFinder(this).where(predicate).findFirst(includeInvisible)
}

fun ViewNode.findAllWith(
suspend fun ViewNode.findAllWith(
includeInvisible: Boolean = false,
predicate: (AccessibilityNodeInfo) -> Boolean
): Array<ViewNode> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ package cn.vove7.andro_accessibility_api.utils
import android.content.ComponentName
import android.content.Intent
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.os.SystemClock
import android.provider.Settings
import android.widget.Toast
import cn.vove7.andro_accessibility_api.InitCp
import java.util.*
import cn.vove7.andro_accessibility_api.viewfinder.ViewFinder
import kotlinx.coroutines.delay
import kotlinx.coroutines.ensureActive
import java.util.Locale
import kotlin.coroutines.coroutineContext
import kotlin.math.max

/**
Expand All @@ -27,19 +28,28 @@ import kotlin.math.max
* @param run () -> T 返回空时,重新执行,直到超时
* @return T
*/
fun <T> whileWaitTime(waitMillis: Long, run: () -> T?): T? {
suspend fun <T> whileWaitTime(
waitMillis: Long,
interval: Long = 0L, run: suspend () -> T?
): T? {
val begin = SystemClock.elapsedRealtime()
val ct = Thread.currentThread()
do {
run.invoke()?.also {
//if 耗时操作
return it
}
} while (SystemClock.elapsedRealtime() - begin < waitMillis && !ct.isInterrupted)
if (interval > 0) delay(interval)
else ensureActive()
} while (SystemClock.elapsedRealtime() - begin < waitMillis)
return null
}


internal suspend inline fun ensureActive() {
coroutineContext.ensureActive()
}


fun jumpAccessibilityServiceSettings(cls: Class<*>) {
val intent = Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS)
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cn.vove7.andro_accessibility_api.viewfinder
import android.os.Build
import androidx.annotation.RequiresApi
import cn.vove7.andro_accessibility_api.viewnode.ViewOperation
import kotlinx.coroutines.runBlocking

/**
* # FindBuilderWithOperation
Expand All @@ -19,7 +20,7 @@ interface FinderBuilderWithOperation : ViewOperation {

val finder: ViewFinder<*>

private val node get() = finder.require()
private val node get() = runBlocking { finder.require() }

override val id get() = node.id
override val className get() = node.className
Expand Down
Loading

0 comments on commit 8b2d6d2

Please sign in to comment.