From 3b67d31a3e3629f40a0c520724022a164b198eaf Mon Sep 17 00:00:00 2001 From: lisonge Date: Sun, 8 Oct 2023 11:43:32 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E9=9F=B3=E9=87=8F=E5=BF=AB=E7=85=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../li/songe/gkd/service/ManageService.kt | 46 +++++++++++++++++++ .../main/java/li/songe/gkd/ui/DebugPage.kt | 12 +++++ app/src/main/java/li/songe/gkd/util/Store.kt | 3 +- 3 files changed, 60 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/li/songe/gkd/service/ManageService.kt b/app/src/main/java/li/songe/gkd/service/ManageService.kt index 3c6eca3..96dc2cf 100644 --- a/app/src/main/java/li/songe/gkd/service/ManageService.kt +++ b/app/src/main/java/li/songe/gkd/service/ManageService.kt @@ -1,18 +1,28 @@ package li.songe.gkd.service +import android.content.BroadcastReceiver import android.content.Context import android.content.Intent +import android.content.IntentFilter +import android.os.Build +import com.blankj.utilcode.util.ScreenUtils +import com.blankj.utilcode.util.ToastUtils +import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.flow.combine +import kotlinx.coroutines.flow.map import kotlinx.coroutines.launch import li.songe.gkd.app import li.songe.gkd.composition.CompositionExt.useLifeCycleLog import li.songe.gkd.composition.CompositionExt.useScope import li.songe.gkd.composition.CompositionService +import li.songe.gkd.debug.SnapshotExt.captureSnapshot import li.songe.gkd.notif.abNotif import li.songe.gkd.notif.createNotif import li.songe.gkd.notif.defaultChannel +import li.songe.gkd.util.VOLUME_CHANGED_ACTION import li.songe.gkd.util.appIdToRulesFlow import li.songe.gkd.util.clickCountFlow +import li.songe.gkd.util.launchTry import li.songe.gkd.util.storeFlow class ManageService : CompositionService({ @@ -21,6 +31,42 @@ class ManageService : CompositionService({ createNotif(context, defaultChannel.id, abNotif) val scope = useScope() + val receiver = object : BroadcastReceiver() { + var lastTriggerTime = -1L + override fun onReceive(context: Context?, intent: Intent?) { + if (intent?.action == VOLUME_CHANGED_ACTION) { + val t = System.currentTimeMillis() + if (t - lastTriggerTime > 3000 && !ScreenUtils.isScreenLock()) { + lastTriggerTime = t + scope.launchTry(Dispatchers.IO) { + captureSnapshot() + ToastUtils.showShort("快照成功") + } + } + } + } + } + scope.launchTry(Dispatchers.IO) { + storeFlow.map { s -> s.captureVolumeChange }.collect { + if (it) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { + context.registerReceiver( + receiver, IntentFilter(VOLUME_CHANGED_ACTION), Context.RECEIVER_EXPORTED + ) + } else { + context.registerReceiver(receiver, IntentFilter(VOLUME_CHANGED_ACTION)) + } + } else { + context.unregisterReceiver(receiver) + } + } + } + onDestroy { + if (storeFlow.value.captureVolumeChange) { + context.unregisterReceiver(receiver) + } + } + scope.launch { combine(appIdToRulesFlow, clickCountFlow, storeFlow) { appIdToRules, clickCount, store -> if (!store.enableService) return@combine "服务已暂停" diff --git a/app/src/main/java/li/songe/gkd/ui/DebugPage.kt b/app/src/main/java/li/songe/gkd/ui/DebugPage.kt index 79ad488..2b67ad8 100644 --- a/app/src/main/java/li/songe/gkd/ui/DebugPage.kt +++ b/app/src/main/java/li/songe/gkd/ui/DebugPage.kt @@ -222,6 +222,18 @@ fun DebugPage() { } } Divider() + TextSwitch( + name = "音量快照", + desc = "当音量变化时,生成快照,如果悬浮窗按钮不工作,可以使用这个", + checked = store.captureVolumeChange + ) { + updateStorage( + storeFlow, store.copy( + captureVolumeChange = it + ) + ) + } + Divider() TextSwitch( name = "隐藏快照状态栏", desc = "当保存快照时,隐藏截图里的顶部状态栏高度区域", diff --git a/app/src/main/java/li/songe/gkd/util/Store.kt b/app/src/main/java/li/songe/gkd/util/Store.kt index 300546b..03a346d 100644 --- a/app/src/main/java/li/songe/gkd/util/Store.kt +++ b/app/src/main/java/li/songe/gkd/util/Store.kt @@ -80,7 +80,7 @@ data class Store( val captureScreenshot: Boolean = false, val httpServerPort: Int = 8888, val updateSubsInterval: Long = 6 * 60 * 60_000L, - @Deprecated("https://github.com/gkd-kit/gkd/issues/26") val captureVolumeKey: Boolean = false, + val captureVolumeChange: Boolean = false, val autoCheckAppUpdate: Boolean = true, val toastWhenClick: Boolean = true, val clickToast: String = "跳过", @@ -96,6 +96,7 @@ val storeFlow by lazy { @Parcelize data class RecordStore( val clickCount: Int = 0, + val snapshotIdMap: Map = emptyMap(), ) : Parcelable val recordStoreFlow by lazy {