2026-11-28 00:25:22 +08:00
|
|
|
|
package com.focusbox.app.service
|
|
|
|
|
|
|
2026-11-29 12:50:37 +08:00
|
|
|
|
import android.app.KeyguardManager
|
2026-11-28 00:25:22 +08:00
|
|
|
|
import android.app.Service
|
2026-11-29 12:50:37 +08:00
|
|
|
|
import android.content.BroadcastReceiver
|
2026-11-28 00:25:22 +08:00
|
|
|
|
import android.content.Context
|
|
|
|
|
|
import android.content.Intent
|
2026-11-29 12:50:37 +08:00
|
|
|
|
import android.content.IntentFilter
|
2026-11-28 00:25:22 +08:00
|
|
|
|
import android.os.Handler
|
|
|
|
|
|
import android.os.IBinder
|
|
|
|
|
|
import android.os.Looper
|
|
|
|
|
|
import android.os.PowerManager
|
|
|
|
|
|
import androidx.core.content.ContextCompat
|
2026-11-29 13:54:10 +08:00
|
|
|
|
import com.focusbox.app.ui.lockscreen.LockScreenActivity
|
2026-11-29 12:50:37 +08:00
|
|
|
|
import com.focusbox.app.util.Prefs
|
2026-11-28 00:25:22 +08:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 计时前台服务。
|
|
|
|
|
|
*
|
|
|
|
|
|
* 之所以必须用前台服务而不是页面里的 Handler:息屏后 Activity 会被暂停、
|
|
|
|
|
|
* 进程随时可能被回收,只有带常驻通知的前台服务能稳定维持计时与到时提醒。
|
|
|
|
|
|
*
|
|
|
|
|
|
* 真正的计时逻辑在 [TimerCore],这里只做三件事:撑住进程、每秒推进、刷新通知。
|
|
|
|
|
|
*/
|
|
|
|
|
|
class TimerService : Service() {
|
|
|
|
|
|
|
|
|
|
|
|
companion object {
|
|
|
|
|
|
const val ACTION_SYNC = "com.focusbox.app.SYNC"
|
|
|
|
|
|
const val ACTION_PAUSE = "com.focusbox.app.PAUSE"
|
|
|
|
|
|
const val ACTION_RESUME = "com.focusbox.app.RESUME"
|
|
|
|
|
|
const val ACTION_STOP = "com.focusbox.app.STOP"
|
|
|
|
|
|
|
|
|
|
|
|
fun ensureRunning(context: Context) {
|
|
|
|
|
|
val intent = Intent(context, TimerService::class.java).setAction(ACTION_SYNC)
|
|
|
|
|
|
runCatching { ContextCompat.startForegroundService(context, intent) }
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fun refresh(context: Context) {
|
|
|
|
|
|
Notif.updateOngoing(context)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fun stop(context: Context) {
|
|
|
|
|
|
runCatching { context.stopService(Intent(context, TimerService::class.java)) }
|
|
|
|
|
|
Notif.cancelOngoing(context)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private val handler = Handler(Looper.getMainLooper())
|
|
|
|
|
|
private var wakeLock: PowerManager.WakeLock? = null
|
|
|
|
|
|
|
|
|
|
|
|
private val ticker = object : Runnable {
|
|
|
|
|
|
override fun run() {
|
|
|
|
|
|
// 只推进状态,不再每秒重发通知:通知里的时间是系统的 Chronometer 在走,
|
|
|
|
|
|
// 每秒 notify 反而会让锁屏上的计时被系统限流后"冻住"。
|
|
|
|
|
|
TimerCore.tick()
|
2026-11-29 20:34:23 +08:00
|
|
|
|
// 锁屏悬浮窗的读数刷新(窗口存在时每秒重画日期/时钟/倒计时)
|
|
|
|
|
|
LockOverlay.update(this@TimerService)
|
2026-11-28 00:25:22 +08:00
|
|
|
|
handler.postDelayed(this, 1000L)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
override fun onCreate() {
|
|
|
|
|
|
super.onCreate()
|
|
|
|
|
|
TimerCore.addListener(onStateChanged)
|
2026-11-29 20:34:23 +08:00
|
|
|
|
// 锁屏倒计时主链路:悬浮窗(WindowManager 直接画在锁屏上,
|
|
|
|
|
|
// 不受"后台弹出界面"限制);灭屏/亮屏/解锁三个时机同步显隐
|
2026-11-29 12:50:37 +08:00
|
|
|
|
ContextCompat.registerReceiver(
|
|
|
|
|
|
this,
|
|
|
|
|
|
screenReceiver,
|
|
|
|
|
|
IntentFilter().apply {
|
|
|
|
|
|
addAction(Intent.ACTION_SCREEN_ON)
|
|
|
|
|
|
addAction(Intent.ACTION_SCREEN_OFF)
|
|
|
|
|
|
},
|
|
|
|
|
|
ContextCompat.RECEIVER_NOT_EXPORTED,
|
|
|
|
|
|
)
|
2026-11-29 20:34:23 +08:00
|
|
|
|
// 用户解锁(输密码/指纹进入系统)后悬浮窗没有存在意义,立即关闭
|
|
|
|
|
|
ContextCompat.registerReceiver(
|
|
|
|
|
|
this,
|
|
|
|
|
|
unlockReceiver,
|
|
|
|
|
|
IntentFilter(Intent.ACTION_USER_PRESENT),
|
|
|
|
|
|
ContextCompat.RECEIVER_NOT_EXPORTED,
|
|
|
|
|
|
)
|
2026-11-29 12:50:37 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private val screenReceiver = object : BroadcastReceiver() {
|
|
|
|
|
|
override fun onReceive(context: Context, intent: Intent) {
|
|
|
|
|
|
when (intent.action) {
|
2026-11-29 13:54:10 +08:00
|
|
|
|
Intent.ACTION_SCREEN_ON -> onScreenOn()
|
|
|
|
|
|
Intent.ACTION_SCREEN_OFF -> onScreenOff()
|
2026-11-29 12:50:37 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-11-29 20:34:23 +08:00
|
|
|
|
private val unlockReceiver = object : BroadcastReceiver() {
|
|
|
|
|
|
override fun onReceive(context: Context, intent: Intent) {
|
|
|
|
|
|
if (intent.action == Intent.ACTION_USER_PRESENT) {
|
|
|
|
|
|
LockOverlay.hide()
|
|
|
|
|
|
Notif.cancelFullscreen(this@TimerService)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-11-29 13:54:10 +08:00
|
|
|
|
/**
|
2026-11-29 20:34:23 +08:00
|
|
|
|
* 悬浮窗显隐的总开关:锁屏中 + 计时未清零 + 开关打开 + 有悬浮窗权限。
|
|
|
|
|
|
* 显示成功时顺手清掉抽屉里的全屏意图触发通知(不再需要)。
|
2026-11-29 13:54:10 +08:00
|
|
|
|
*/
|
2026-11-29 20:34:23 +08:00
|
|
|
|
private fun syncLockOverlay() {
|
|
|
|
|
|
val km = getSystemService(Context.KEYGUARD_SERVICE) as KeyguardManager
|
|
|
|
|
|
val want = Prefs.lockFullscreen
|
|
|
|
|
|
&& LockOverlay.canShow(this)
|
|
|
|
|
|
&& km.isKeyguardLocked()
|
|
|
|
|
|
&& (TimerCore.running || TimerCore.displayNow() > 0)
|
|
|
|
|
|
if (want) LockOverlay.show(this) else LockOverlay.hide()
|
|
|
|
|
|
if (LockOverlay.isShowing()) Notif.cancelFullscreen(this)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** 灭屏:先补结算,悬浮窗可用就直接盖到锁屏上(保持熄屏,按电源键即见);不可用走全屏意图兜底 */
|
2026-11-29 13:54:10 +08:00
|
|
|
|
private fun onScreenOff() {
|
|
|
|
|
|
TimerCore.tick() // 灭屏一瞬先补结算,避免到点边界上拉出 00:00 的空页面
|
2026-11-29 20:34:23 +08:00
|
|
|
|
if (!Prefs.lockFullscreen || !TimerCore.running) return
|
|
|
|
|
|
syncLockOverlay()
|
|
|
|
|
|
if (LockOverlay.isShowing()) return
|
|
|
|
|
|
// 无悬浮窗权限的兜底:灭屏投递全屏意图(个别 ROM 直启,其余进抽屉可点)
|
2026-11-29 12:50:37 +08:00
|
|
|
|
val km = getSystemService(Context.KEYGUARD_SERVICE) as KeyguardManager
|
2026-11-29 20:34:23 +08:00
|
|
|
|
if (km.isKeyguardLocked()) Notif.updateFullscreen(this)
|
2026-11-28 00:25:22 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-11-29 13:54:10 +08:00
|
|
|
|
/** 亮屏:先补结算(冻结进程的到点闹钟被推迟时,靠这一下止损,不再滚负数) */
|
|
|
|
|
|
private fun onScreenOn() {
|
|
|
|
|
|
TimerCore.tick()
|
2026-11-29 20:34:23 +08:00
|
|
|
|
syncLockOverlay()
|
2026-11-29 21:02:55 +08:00
|
|
|
|
// 悬浮窗已盖住锁屏时,仍尝试用沉浸式锁屏页接管——
|
|
|
|
|
|
// Activity 有主题级全屏(状态栏彻底遮掉)+ 横竖屏专属排版,
|
|
|
|
|
|
// 直启成功则显示效果更完整;被鸿蒙拦截也无损失,悬浮窗继续顶着
|
|
|
|
|
|
if (Prefs.lockFullscreen && TimerCore.running) {
|
2026-11-29 13:54:10 +08:00
|
|
|
|
val km = getSystemService(Context.KEYGUARD_SERVICE) as KeyguardManager
|
|
|
|
|
|
if (km.isKeyguardLocked()) {
|
|
|
|
|
|
runCatching {
|
|
|
|
|
|
startActivity(
|
|
|
|
|
|
Intent(this, LockScreenActivity::class.java)
|
|
|
|
|
|
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
|
|
|
|
|
|
)
|
|
|
|
|
|
}
|
2026-11-29 21:02:55 +08:00
|
|
|
|
if (!LockOverlay.isShowing() && !LockScreenActivity.showing) {
|
|
|
|
|
|
// 悬浮窗和直启都失败:留全屏意图通知兜底(点进同一页面)
|
|
|
|
|
|
Notif.updateFullscreen(this)
|
|
|
|
|
|
}
|
2026-11-29 13:54:10 +08:00
|
|
|
|
}
|
2026-11-29 21:02:55 +08:00
|
|
|
|
} else if (LockScreenActivity.showing) {
|
|
|
|
|
|
Notif.cancelFullscreen(this)
|
2026-11-29 13:54:10 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-11-29 21:02:55 +08:00
|
|
|
|
/** 系统配置变化(旋转):悬浮窗立刻换横/竖屏排版重建 */
|
|
|
|
|
|
override fun onConfigurationChanged(newConfig: android.content.res.Configuration) {
|
|
|
|
|
|
super.onConfigurationChanged(newConfig)
|
|
|
|
|
|
LockOverlay.update(this)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-11-28 00:25:22 +08:00
|
|
|
|
private val onStateChanged: () -> Unit = {
|
|
|
|
|
|
Notif.updateOngoing(this)
|
2026-11-29 13:54:10 +08:00
|
|
|
|
// 计时结束/暂停时清掉锁屏触发通知,抽屉里不留垃圾
|
|
|
|
|
|
if (!TimerCore.running) Notif.cancelFullscreen(this)
|
2026-11-29 20:34:23 +08:00
|
|
|
|
syncLockOverlay()
|
2026-11-28 10:39:57 +08:00
|
|
|
|
syncTicker()
|
2026-11-28 00:25:22 +08:00
|
|
|
|
syncWakeLock()
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
|
|
|
|
|
|
when (intent?.action) {
|
|
|
|
|
|
ACTION_PAUSE -> {
|
|
|
|
|
|
TimerCore.pause()
|
|
|
|
|
|
stopTicker()
|
|
|
|
|
|
}
|
|
|
|
|
|
ACTION_RESUME -> {
|
|
|
|
|
|
TimerCore.resume()
|
|
|
|
|
|
syncTicker()
|
|
|
|
|
|
}
|
|
|
|
|
|
ACTION_STOP -> {
|
2026-11-28 13:25:28 +08:00
|
|
|
|
// 休息是纯计时:通知上的"结束"也走 endBreak,不产生任何记录
|
|
|
|
|
|
if (TimerCore.mode == TimerCore.MODE_FOCUS) TimerCore.stopEarly() else TimerCore.endBreak()
|
2026-11-28 00:25:22 +08:00
|
|
|
|
stopTicker()
|
|
|
|
|
|
stopSelf()
|
|
|
|
|
|
return START_NOT_STICKY
|
|
|
|
|
|
}
|
|
|
|
|
|
else -> Unit
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 必须在启动后立即进入前台,否则 5 秒内会被系统判定为 ANR 级违规
|
|
|
|
|
|
startForegroundCompat()
|
|
|
|
|
|
syncTicker()
|
|
|
|
|
|
syncWakeLock()
|
|
|
|
|
|
Notif.updateOngoing(this)
|
|
|
|
|
|
return START_NOT_STICKY
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 只有倒计时需要每秒推进(判断是否到点)。
|
|
|
|
|
|
* 正计时靠时间戳现算、通知靠系统 Chronometer 走时,
|
|
|
|
|
|
* 既不用轮询也不用占着 WakeLock,长时间正计时不会白耗电。
|
|
|
|
|
|
*/
|
|
|
|
|
|
private fun syncTicker() {
|
|
|
|
|
|
if (TimerCore.running && !TimerCore.countUp) startTicker() else stopTicker()
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-11-29 10:34:46 +08:00
|
|
|
|
/** EMUI 偶发拒绝前台启动时的自愈重试标记:只重试一次,避免无限循环 */
|
|
|
|
|
|
private var fgRetried = false
|
|
|
|
|
|
|
2026-11-28 00:25:22 +08:00
|
|
|
|
private fun startForegroundCompat() {
|
|
|
|
|
|
val notification = Notif.buildOngoing(this)
|
2026-11-29 10:05:14 +08:00
|
|
|
|
// 必须显式传类型:specialUse 是 Android 14 才引入的,
|
|
|
|
|
|
// Android 12/13 不认识清单里的这个类型位,两参版本(读清单)会被拒绝,
|
|
|
|
|
|
// 服务起不来 → 每秒 tick 停掉 → 息屏后到点无人结算,通知滚成负数。
|
|
|
|
|
|
// <34 用 dataSync(Android 12 原生支持,行为与历史版本一致);
|
|
|
|
|
|
// >=34 用 specialUse 规避 dataSync 的 24 小时 6 小时累计上限。
|
|
|
|
|
|
val type = if (android.os.Build.VERSION.SDK_INT >= 34) {
|
|
|
|
|
|
android.content.pm.ServiceInfo.FOREGROUND_SERVICE_TYPE_SPECIAL_USE
|
|
|
|
|
|
} else {
|
|
|
|
|
|
android.content.pm.ServiceInfo.FOREGROUND_SERVICE_TYPE_DATA_SYNC
|
|
|
|
|
|
}
|
2026-11-28 00:25:22 +08:00
|
|
|
|
runCatching {
|
2026-11-29 10:05:14 +08:00
|
|
|
|
startForeground(Notif.ID_ONGOING, notification, type)
|
2026-11-29 10:34:46 +08:00
|
|
|
|
}.recoverCatching {
|
|
|
|
|
|
// 显式类型被拒时退回两参版本再试一次(读清单组合类型)
|
|
|
|
|
|
startForeground(Notif.ID_ONGOING, notification)
|
|
|
|
|
|
}.onSuccess {
|
|
|
|
|
|
fgRetried = false
|
2026-11-28 00:25:22 +08:00
|
|
|
|
}.onFailure {
|
2026-11-29 10:34:46 +08:00
|
|
|
|
// EMUI 偶发拒绝(省电策略/启动节奏):延迟半秒自动重试一次,
|
|
|
|
|
|
// 用户就不用"反复开关几次 APP"才能看到状态栏图标了;仍失败才放弃
|
|
|
|
|
|
if (!fgRetried) {
|
|
|
|
|
|
fgRetried = true
|
|
|
|
|
|
handler.postDelayed({ startForegroundCompat() }, 500L)
|
|
|
|
|
|
} else {
|
|
|
|
|
|
// 前台服务被系统拒绝时退化为普通服务,
|
|
|
|
|
|
// 计时本身仍在 TimerCore 里走,只是通知可能不显示。
|
|
|
|
|
|
stopSelf()
|
|
|
|
|
|
}
|
2026-11-28 00:25:22 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private fun startTicker() {
|
|
|
|
|
|
handler.removeCallbacks(ticker)
|
|
|
|
|
|
handler.postDelayed(ticker, 1000L)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private fun stopTicker() {
|
|
|
|
|
|
handler.removeCallbacks(ticker)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private fun syncWakeLock() {
|
|
|
|
|
|
if (TimerCore.running && !TimerCore.countUp) {
|
|
|
|
|
|
if (wakeLock?.isHeld != true) {
|
|
|
|
|
|
val pm = getSystemService(Context.POWER_SERVICE) as PowerManager
|
|
|
|
|
|
wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "FocusBox:timer").apply {
|
|
|
|
|
|
setReferenceCounted(false)
|
|
|
|
|
|
// 超时兜底:正常路径会主动释放,这里防止异常退出后一直亮着 CPU
|
|
|
|
|
|
acquire(4 * 60 * 60 * 1000L)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
} else {
|
|
|
|
|
|
releaseWakeLock()
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private fun releaseWakeLock() {
|
|
|
|
|
|
runCatching { if (wakeLock?.isHeld == true) wakeLock?.release() }
|
|
|
|
|
|
wakeLock = null
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
override fun onBind(intent: Intent?): IBinder? = null
|
|
|
|
|
|
|
|
|
|
|
|
override fun onDestroy() {
|
|
|
|
|
|
stopTicker()
|
|
|
|
|
|
releaseWakeLock()
|
2026-11-29 20:34:23 +08:00
|
|
|
|
LockOverlay.hide()
|
2026-11-29 12:50:37 +08:00
|
|
|
|
runCatching { unregisterReceiver(screenReceiver) }
|
2026-11-29 20:34:23 +08:00
|
|
|
|
runCatching { unregisterReceiver(unlockReceiver) }
|
2026-11-29 12:50:37 +08:00
|
|
|
|
Notif.cancelFullscreen(this)
|
2026-11-28 00:25:22 +08:00
|
|
|
|
TimerCore.removeListener(onStateChanged)
|
|
|
|
|
|
super.onDestroy()
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|