diff --git a/app/src/main/kotlin/li/songe/gkd/debug/SnapshotExt.kt b/app/src/main/kotlin/li/songe/gkd/debug/SnapshotExt.kt index 96b549e28..00eaf9b45 100644 --- a/app/src/main/kotlin/li/songe/gkd/debug/SnapshotExt.kt +++ b/app/src/main/kotlin/li/songe/gkd/debug/SnapshotExt.kt @@ -66,32 +66,40 @@ object SnapshotExt { private val captureLoading = MutableStateFlow(false) - suspend fun captureSnapshot(): ComplexSnapshot { + suspend fun captureSnapshot(skipScreenshot: Boolean = false): ComplexSnapshot { if (!GkdAbService.isRunning()) { throw RpcError("无障碍不可用") } if (captureLoading.value) { - throw RpcError("正在截屏,不可重复截屏") + throw RpcError("正在保存快照,不可重复操作") } captureLoading.value = true - ToastUtils.showShort("正在捕获快照...") + ToastUtils.showShort("正在保存快照...") try { - val snapshotDef = coroutineScope { async(Dispatchers.IO) { createComplexSnapshot() } } - val bitmapDef = coroutineScope { + val bitmapDef = coroutineScope {// TODO 也许在分屏模式下可能需要处理 async(Dispatchers.IO) { - GkdAbService.currentScreenshot() ?: withTimeoutOrNull(3_000) { - if (!ScreenshotService.isRunning()) { - return@withTimeoutOrNull null + if (skipScreenshot) { + LogUtils.d("跳过截屏,即将使用空白图片") + Bitmap.createBitmap( + ScreenUtils.getScreenWidth(), + ScreenUtils.getScreenHeight(), + Bitmap.Config.ARGB_8888 + ) + } else { + GkdAbService.currentScreenshot() ?: withTimeoutOrNull(3_000) { + if (!ScreenshotService.isRunning()) { + return@withTimeoutOrNull null + } + ScreenshotService.screenshot() + } ?: Bitmap.createBitmap( + ScreenUtils.getScreenWidth(), + ScreenUtils.getScreenHeight(), + Bitmap.Config.ARGB_8888 + ).apply { + LogUtils.d("截屏不可用,即将使用空白图片") } - ScreenshotService.screenshot() - } ?: Bitmap.createBitmap( - ScreenUtils.getScreenWidth(), - ScreenUtils.getScreenHeight(), - Bitmap.Config.ARGB_8888 - ).apply { - LogUtils.d("截屏不可用,即将使用空白图片") } } } diff --git a/app/src/main/kotlin/li/songe/gkd/service/GkdAbService.kt b/app/src/main/kotlin/li/songe/gkd/service/GkdAbService.kt index 7004fe157..d25d78ee8 100644 --- a/app/src/main/kotlin/li/songe/gkd/service/GkdAbService.kt +++ b/app/src/main/kotlin/li/songe/gkd/service/GkdAbService.kt @@ -38,6 +38,7 @@ import li.songe.gkd.data.SubsVersion import li.songe.gkd.data.SubscriptionRaw import li.songe.gkd.data.getActionFc import li.songe.gkd.db.DbSet +import li.songe.gkd.debug.SnapshotExt import li.songe.gkd.shizuku.useSafeGetTasksFc import li.songe.gkd.util.client import li.songe.gkd.util.launchTry @@ -148,10 +149,13 @@ class GkdAbService : CompositionAbService({ newQueryTask() } } + val skipAppIds = setOf("com.android.systemui") onAccessibilityEvent { event -> - if (event == null) return@onAccessibilityEvent + if (event == null || event.packageName == null) return@onAccessibilityEvent if (!(event.eventType == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED || event.eventType == AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED)) return@onAccessibilityEvent + if (skipAppIds.any { id -> id.contentEquals(event.packageName) }) return@onAccessibilityEvent + if (event.eventType == AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED) { if (event.eventTime - appChangeTime > 5_000) { // app 启动 5s 内关闭限制 if (event.eventTime - lastContentEventTime < 100) { @@ -346,6 +350,24 @@ class GkdAbService : CompositionAbService({ wm.removeView(aliveView) } } + + onAccessibilityEvent { e -> + if (!storeFlow.value.captureScreenshot) return@onAccessibilityEvent + e ?: return@onAccessibilityEvent + val appId = e.packageName ?: return@onAccessibilityEvent + val appCls = e.className ?: return@onAccessibilityEvent + if (appId.contentEquals("com.miui.screenshot") && + e.eventType == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED && + !e.isFullScreen && + appCls.contentEquals("android.widget.RelativeLayout") && + e.text.firstOrNull()?.contentEquals("截屏缩略图") == true // [截屏缩略图, 截长屏, 发送] + ) { + LogUtils.d("captureScreenshot", e) + scope.launchTry(Dispatchers.IO) { + SnapshotExt.captureSnapshot(skipScreenshot = true) + } + } + } }) { companion object { diff --git a/app/src/main/kotlin/li/songe/gkd/ui/DebugPage.kt b/app/src/main/kotlin/li/songe/gkd/ui/DebugPage.kt index 2c5abf54c..275c65b47 100644 --- a/app/src/main/kotlin/li/songe/gkd/ui/DebugPage.kt +++ b/app/src/main/kotlin/li/songe/gkd/ui/DebugPage.kt @@ -260,6 +260,20 @@ fun DebugPage() { ) ) } + + Divider() + TextSwitch( + name = "截屏快照", + desc = "当用户截屏时保存快照(需手动替换快照图片),仅支持MIUI14", + checked = store.captureScreenshot + ) { + updateStorage( + storeFlow, store.copy( + captureScreenshot = it + ) + ) + } + Divider() TextSwitch( name = "隐藏快照状态栏",