feat: action 支持 back

This commit is contained in:
lisonge 2023-10-23 16:08:51 +08:00
parent 5cd30e45e0
commit 9c458e245c
4 changed files with 51 additions and 24 deletions

View File

@ -83,11 +83,7 @@ data class Rule(
return activityIds.any { activityId.startsWith(it) } return activityIds.any { activityId.startsWith(it) }
} }
val performAction: ActionFc = when (rule.action) { val performAction = getActionFc(rule.action)
"clickNode" -> clickNode
"clickCenter" -> clickCenter
else -> click
}
companion object { companion object {
const val defaultMiniCd = 1000L const val defaultMiniCd = 1000L
@ -98,7 +94,7 @@ typealias ActionFc = (context: AccessibilityService, node: AccessibilityNodeInfo
@Serializable @Serializable
data class ClickAction( data class GkdAction(
val selector: String, val selector: String,
val quickFind: Boolean = false, val quickFind: Boolean = false,
val action: String? = null, val action: String? = null,
@ -145,3 +141,19 @@ val clickCenter: ActionFc = { context, node ->
) )
} }
val backFc: ActionFc = { context, _ ->
ActionResult(
action = "back",
result = context.performGlobalAction(AccessibilityService.GLOBAL_ACTION_BACK)
)
}
fun getActionFc(action: String?): ActionFc {
return when (action) {
"clickNode" -> clickNode
"clickCenter" -> clickCenter
"back" -> backFc
else -> click
}
}

View File

@ -31,7 +31,7 @@ import kotlinx.serialization.Serializable
import li.songe.gkd.app import li.songe.gkd.app
import li.songe.gkd.appScope import li.songe.gkd.appScope
import li.songe.gkd.composition.CompositionService import li.songe.gkd.composition.CompositionService
import li.songe.gkd.data.ClickAction import li.songe.gkd.data.GkdAction
import li.songe.gkd.data.DeviceInfo import li.songe.gkd.data.DeviceInfo
import li.songe.gkd.data.RpcError import li.songe.gkd.data.RpcError
import li.songe.gkd.data.SubsItem import li.songe.gkd.data.SubsItem
@ -120,9 +120,9 @@ class HttpService : CompositionService({
if (!GkdAbService.isRunning()) { if (!GkdAbService.isRunning()) {
throw RpcError("无障碍没有运行") throw RpcError("无障碍没有运行")
} }
val clickAction = call.receive<ClickAction>() val gkdAction = call.receive<GkdAction>()
LogUtils.d(clickAction) LogUtils.d(gkdAction)
call.respond(GkdAbService.execClickAction(clickAction)) call.respond(GkdAbService.execAction(gkdAction))
} }
} }
} }

View File

@ -29,14 +29,12 @@ import li.songe.gkd.composition.CompositionAbService
import li.songe.gkd.composition.CompositionExt.useLifeCycleLog import li.songe.gkd.composition.CompositionExt.useLifeCycleLog
import li.songe.gkd.composition.CompositionExt.useScope import li.songe.gkd.composition.CompositionExt.useScope
import li.songe.gkd.data.ActionResult import li.songe.gkd.data.ActionResult
import li.songe.gkd.data.ClickAction
import li.songe.gkd.data.ClickLog import li.songe.gkd.data.ClickLog
import li.songe.gkd.data.GkdAction
import li.songe.gkd.data.NodeInfo import li.songe.gkd.data.NodeInfo
import li.songe.gkd.data.RpcError import li.songe.gkd.data.RpcError
import li.songe.gkd.data.SubscriptionRaw import li.songe.gkd.data.SubscriptionRaw
import li.songe.gkd.data.click import li.songe.gkd.data.getActionFc
import li.songe.gkd.data.clickCenter
import li.songe.gkd.data.clickNode
import li.songe.gkd.db.DbSet import li.songe.gkd.db.DbSet
import li.songe.gkd.shizuku.useSafeGetTasksFc import li.songe.gkd.shizuku.useSafeGetTasksFc
import li.songe.gkd.util.Singleton import li.songe.gkd.util.Singleton
@ -123,7 +121,8 @@ class GkdAbService : CompositionAbService({
) )
} else { } else {
topActivityFlow.value = topActivityFlow.value?.copy( topActivityFlow.value = topActivityFlow.value?.copy(
appId = rightAppId, activityId = shizukuActivityId, appId = rightAppId,
activityId = shizukuActivityId,
sourceId = newActivityId sourceId = newActivityId
) )
} }
@ -311,23 +310,19 @@ class GkdAbService : CompositionAbService({
var service: GkdAbService? = null var service: GkdAbService? = null
fun isRunning() = ServiceUtils.isServiceRunning(GkdAbService::class.java) fun isRunning() = ServiceUtils.isServiceRunning(GkdAbService::class.java)
fun execClickAction(clickAction: ClickAction): ActionResult { fun execAction(gkdAction: GkdAction): ActionResult {
val serviceVal = service ?: throw RpcError("无障碍没有运行") val serviceVal = service ?: throw RpcError("无障碍没有运行")
val selector = try { val selector = try {
Selector.parse(clickAction.selector) Selector.parse(gkdAction.selector)
} catch (e: Exception) { } catch (e: Exception) {
throw RpcError("非法选择器") throw RpcError("非法选择器")
} }
val targetNode = val targetNode =
serviceVal.safeActiveWindow?.querySelector(selector, clickAction.quickFind) serviceVal.safeActiveWindow?.querySelector(selector, gkdAction.quickFind)
?: throw RpcError("没有选择到节点") ?: throw RpcError("没有选择到节点")
return when (clickAction.action) { return getActionFc(gkdAction.action)(serviceVal, targetNode)
"clickNode" -> clickNode(serviceVal, targetNode)
"clickCenter" -> clickCenter(serviceVal, targetNode)
else -> click(serviceVal, targetNode)
}
} }

View File

@ -204,15 +204,35 @@ fun SnapshotPage() {
) )
Divider() Divider()
if (recordStore.snapshotIdMap.containsKey(snapshotVal.id)) { if (recordStore.snapshotIdMap.containsKey(snapshotVal.id)) {
val url =
"https://i.gkd.li/import/" + recordStore.snapshotIdMap[snapshotVal.id]
Text( Text(
text = "复制链接", modifier = Modifier text = "复制链接", modifier = Modifier
.clickable(onClick = { .clickable(onClick = {
selectedSnapshot = null selectedSnapshot = null
ClipboardUtils.copyText("https://gkd-kit.gitee.io/import/" + recordStore.snapshotIdMap[snapshotVal.id]) ClipboardUtils.copyText(url)
ToastUtils.showShort("复制成功") ToastUtils.showShort("复制成功")
}) })
.then(modifier) .then(modifier)
) )
Divider()
Text(
text = "分享链接", modifier = Modifier
.clickable(onClick = {
selectedSnapshot = null
val intent = Intent().apply {
action = Intent.ACTION_SEND
putExtra(Intent.EXTRA_TEXT, url)
type = "text/plain"
}
context.startActivity(
Intent.createChooser(
intent, "分享链接"
)
)
})
.then(modifier)
)
} else { } else {
Text( Text(
text = "生成链接(需科学上网)", modifier = Modifier text = "生成链接(需科学上网)", modifier = Modifier