feat: 音量快照

This commit is contained in:
lisonge 2023-10-08 11:43:32 +08:00
parent 2e454eb37d
commit 3b67d31a3e
3 changed files with 60 additions and 1 deletions

View File

@ -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 "服务已暂停"

View File

@ -222,6 +222,18 @@ fun DebugPage() {
}
}
Divider()
TextSwitch(
name = "音量快照",
desc = "当音量变化时,生成快照,如果悬浮窗按钮不工作,可以使用这个",
checked = store.captureVolumeChange
) {
updateStorage(
storeFlow, store.copy(
captureVolumeChange = it
)
)
}
Divider()
TextSwitch(
name = "隐藏快照状态栏",
desc = "当保存快照时,隐藏截图里的顶部状态栏高度区域",

View File

@ -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<Long, Int> = emptyMap(),
) : Parcelable
val recordStoreFlow by lazy {