feat: captureVolumeChange (#718)
Some checks failed
Build-Apk / build (push) Has been cancelled

This commit is contained in:
lisonge 2024-08-31 17:41:44 +08:00
parent 85ba7e67ee
commit 751898532f
3 changed files with 60 additions and 0 deletions

View File

@ -1,6 +1,10 @@
package li.songe.gkd.service
import android.content.BroadcastReceiver
import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.graphics.Bitmap
import android.graphics.PixelFormat
import android.os.Build
@ -12,6 +16,7 @@ import android.view.WindowManager
import android.view.accessibility.AccessibilityEvent
import android.view.accessibility.AccessibilityNodeInfo
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.ScreenUtils
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.asCoroutineDispatcher
@ -427,6 +432,50 @@ class GkdAbService : CompositionAbService({
}
}
val volumeChangedAction = "android.media.VOLUME_CHANGED_ACTION"
fun createVolumeReceiver() = object : BroadcastReceiver() {
var lastTriggerTime = -1L
override fun onReceive(context: Context?, intent: Intent?) {
if (intent?.action == volumeChangedAction) {
val t = System.currentTimeMillis()
if (t - lastTriggerTime > 3000 && !ScreenUtils.isScreenLock()) {
lastTriggerTime = t
scope.launchTry(Dispatchers.IO) {
SnapshotExt.captureSnapshot()
toast("快照成功")
}
}
}
}
}
var captureVolumeReceiver: BroadcastReceiver? = null
scope.launch {
storeFlow.map(scope) { s -> s.captureVolumeChange }.collect {
if (captureVolumeReceiver != null) {
context.unregisterReceiver(captureVolumeReceiver)
}
captureVolumeReceiver = if (it) {
createVolumeReceiver().apply {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
context.registerReceiver(
this, IntentFilter(volumeChangedAction), Context.RECEIVER_EXPORTED
)
} else {
context.registerReceiver(this, IntentFilter(volumeChangedAction))
}
}
} else {
null
}
}
}
onDestroy {
if (captureVolumeReceiver != null) {
context.unregisterReceiver(captureVolumeReceiver)
}
}
onAccessibilityEvent { e ->
if (!storeFlow.value.captureScreenshot) return@onAccessibilityEvent
val appId = e.packageName ?: return@onAccessibilityEvent

View File

@ -471,6 +471,16 @@ fun AdvancedPage() {
}
)
TextSwitch(
title = "音量快照",
subtitle = "音量变化时保存快照",
checked = store.captureVolumeChange
) {
storeFlow.value = store.copy(
captureVolumeChange = it
)
}
TextSwitch(
title = "截屏快照",
subtitle = "触发截屏时保存快照",

View File

@ -47,6 +47,7 @@ data class Store(
val captureScreenshot: Boolean = false,
val httpServerPort: Int = 8888,
val updateSubsInterval: Long = UpdateTimeOption.Everyday.value,
val captureVolumeChange: Boolean = false,
val autoCheckAppUpdate: Boolean = BuildConfig.ENABLED_UPDATE,
val toastWhenClick: Boolean = true,
val clickToast: String = "GKD",