安卓无障碍API封装库: Android-Accessibility-Api (2.0)

接上篇:安卓无障碍API封装库: Android-Accessibility-Api

本次2.0更新带来了:

  1. 添加SmartFinder
  2. 支持多条件随意组合 搜索
  3. 支持条件扩展
  4. 协程支持

SmartFinder 介绍

1. 自定义条件搜索

搜索 AccessibilityNodeInfo.isChecked 为 true 的

//SF 为 SmartFinder缩写
SF.where { node ->
     node.isChecked
}.find()
2. 使用封装条件

搜索 id 为 text1 或者 text 为 “123” 的 Node

SF.id("text1").or().text("123").find()
3. 复杂条件搜索

(text=="111" && desc=="111") || (text=="222" && desc=="222")

SF.where(SF.text("111").desc("111"))
    .or(SF.text("222").desc("222"))
    .find()
4. 其他搜索方式

提供了中缀表达式,支持属性 _text, _desc, _id

SF.and(_text contains "Smart").clickable().findFirst()

SF.where(_id eq "view_id").findFirst()
//等效
SF.id("view_id").findFirst()
//使用中缀表达式
(SF where text("1111") or text("2222")
        and id("111") or longClickable()).findAll()
5. 协程支持

支持协程主要是为了在等待搜索时,能够及时响应 Job.cancel()

//主动附加协程上下文,可在搜索时感知取消事件
SF.attachCoroutine()
    .id("view_id")
    .waitFor(10_000)

扩展搜索条件

封装自定义搜索条件,使调用起来更简洁
库中搜索条件全部实现位于 SmartFinderConditions.kt

例如 定义使用正则匹配Node文本

Step 1.
class TextMatcherCondition(private val regex: String) : MatchCondition {
	//此处注意直接创建Regex,防止在搜索时重复创建;另外可直接检查正则表达式有效性
    private val reg = regex.toRegex()
    override fun invoke(node: AcsNode) =
        node.text?.toString()?.let {
            reg.matches(it)
        } ?: false
}

此时,已经可以这样使用:

SF.where(TextMatcherCondition("[0-9]+")).find()

追究简洁,可进行扩展方法:

Step 2.
fun ConditionGroup.matchText(reg: String) = link(TextMatcherCondition(reg))

之后可简化调用

SF.matchText("[0-9]+").find()

Github =>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值