新增锁屏全屏倒计时:计时中锁屏亮屏即全屏显示日期/时钟/倒计时/任务(v0.16)

This commit is contained in:
XIGE
2026-11-29 12:50:37 +08:00
parent a955318990
commit 0ddd1ec125
10 changed files with 298 additions and 1 deletions

View File

@@ -1,13 +1,17 @@
package com.focusbox.app.service
import android.app.KeyguardManager
import android.app.Service
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.os.Handler
import android.os.IBinder
import android.os.Looper
import android.os.PowerManager
import androidx.core.content.ContextCompat
import com.focusbox.app.util.Prefs
/**
* 计时前台服务。
@@ -55,6 +59,35 @@ class TimerService : Service() {
override fun onCreate() {
super.onCreate()
TimerCore.addListener(onStateChanged)
// 锁屏全屏倒计时:计时中亮屏(用户按电源键偷看)→ 发全屏意图把
// LockScreenActivity 盖在锁屏上;灭屏时撤回触发通知,不在抽屉里堆垃圾
ContextCompat.registerReceiver(
this,
screenReceiver,
IntentFilter().apply {
addAction(Intent.ACTION_SCREEN_ON)
addAction(Intent.ACTION_SCREEN_OFF)
},
ContextCompat.RECEIVER_NOT_EXPORTED,
)
}
private val screenReceiver = object : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
when (intent.action) {
Intent.ACTION_SCREEN_ON -> showLockPeek()
Intent.ACTION_SCREEN_OFF -> Notif.cancelFullscreen(context)
}
}
}
private fun showLockPeek() {
if (!Prefs.lockFullscreen) return
if (!TimerCore.running) return
val km = getSystemService(Context.KEYGUARD_SERVICE) as KeyguardManager
// 只有真在锁屏界面才拉全屏页;用户正在用手机时不打扰
if (!km.inKeyguardRestrictedInputMode()) return
Notif.updateFullscreen(this)
}
private val onStateChanged: () -> Unit = {
@@ -171,6 +204,8 @@ class TimerService : Service() {
override fun onDestroy() {
stopTicker()
releaseWakeLock()
runCatching { unregisterReceiver(screenReceiver) }
Notif.cancelFullscreen(this)
TimerCore.removeListener(onStateChanged)
super.onDestroy()
}