v3.18 按审查报告修缺陷:口径统一、生命周期补漏、读数与百分比修正

- 项目时长/次数改为按任务当前项目统计,与统计页分布一致
- 进度条不再把重复任务跑一轮算成完成
- 清空数据补清 t_task/t_project 等并解绑内存任务
- 进程被杀补记同时写完成记录;结束本轮清空暂存备注
- 通知「结束」先前台化再 stopSelf
- 正计时锁屏读数按需刷新;悬浮窗 addView 失败先 removeView
- 百分比统一 <1%、分布卡加大下界、清空后刷新专注页
- 删除死代码与 41 个未引用字符串,计时状态一次落盘
This commit is contained in:
XIGE
2026-12-02 10:59:52 +08:00
parent 5e882a77ad
commit 9eacf7d87b
21 changed files with 407 additions and 230 deletions

View File

@@ -46,11 +46,14 @@ object LockOverlay {
/** 显示(幂等):已在显示或无权限时不重复添加 */
fun show(context: Context) {
if (root != null || !canShow(context)) return
val ctx = context.applicationContext
val manager = ctx.getSystemService(Context.WINDOW_SERVICE) as WindowManager
var view: View? = null
var added = false
try {
val ctx = context.applicationContext
val manager = ctx.getSystemService(Context.WINDOW_SERVICE) as WindowManager
val view = LayoutInflater.from(ctx).inflate(R.layout.activity_lock_screen, null)
view.setOnClickListener { hide() }
val v = LayoutInflater.from(ctx).inflate(R.layout.activity_lock_screen, null)
view = v
v.setOnClickListener { hide() }
// API 26+ 用应用悬浮窗类型;更旧版本退回 TYPE_PHONE(本应用 minSdk 24)
val type = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
@@ -81,13 +84,19 @@ object LockOverlay {
lp.gravity = Gravity.TOP
lp.x = 0
lp.y = -statusBar
manager.addView(view, lp)
manager.addView(v, lp)
added = true
root = view
root = v
wm = manager
orientation = ctx.resources.configuration.orientation
render(view)
render(v)
} catch (_: Exception) {
// addView 成功之后再抛异常(例如 render 里找控件)时,窗口已经挂在屏幕上了。
// 之前只把 root 置空,窗口却留着 → isShowing() 返回 false,谁也关不掉它,
// 用户只能重启手机。这里必须先把窗口摘掉再清状态。
val v = view
if (added && v != null) runCatching { manager.removeView(v) }
root = null
wm = null
}

View File

@@ -247,7 +247,8 @@ object Notif {
.setOngoing(true)
.setCategory(NotificationCompat.CATEGORY_ALARM)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setAutoCancel(true)
// 不配 setAutoCancel:与 setOngoing(true) 互相矛盾(常驻通知点不掉),
// 留着只会让人误以为点了会自己消失
.build()
runCatching { NotificationManagerCompat.from(context).notify(ID_FULLSCREEN, n) }
}

View File

@@ -189,11 +189,30 @@ object TimerCore {
// 时间够长就按结束时刻补记,不够就当没发生过,绝不虚增统计。
val finishedAt = endAt.takeIf { it > 0 } ?: System.currentTimeMillis()
val elapsed = finishedAt - startedAt
val counted = mode == MODE_FOCUS && startedAt > 0 && elapsed >= totalMs * 9 / 10
running = false
if (mode == MODE_FOCUS && startedAt > 0 && elapsed >= totalMs * 9 / 10) {
record((totalMs / 1000).toInt(), completed = true, endMs = finishedAt)
if (counted) {
val seconds = (totalMs / 1000).toInt()
record(seconds, completed = true, endMs = finishedAt)
// 与 complete() 的收尾完全对齐:这一轮既然算进统计,完成记录里
// 就必须有一条对应的行,否则会出现「统计里有、记录里没有」,
// 任务在任务页和项目页都还显示未完成。
if (taskId > 0) {
runCatching {
Store.addCompletionRecord(
sourceId = taskId,
note = (roundNotes + ctx.getString(R.string.task_note_recovered))
.joinToString("\n"),
startAt = startedAt,
endAt = finishedAt,
seconds = seconds,
)
}
}
round += 1
}
// 这一轮已经结束(无论算不算数),暂存的暂停备注都不能留给下一轮
clearRoundNotes()
remainingMs = durationFor(mode)
startedAt = 0
endAt = 0
@@ -553,35 +572,6 @@ object TimerCore {
TimerService.stop(ctx)
}
/** 跳过:专注跳过不算完成,休息跳过直接进入专注 */
fun skip() {
if (countUp) {
stopEarly()
return
}
if (mode == MODE_FOCUS) {
val elapsed = elapsedSec()
if (elapsed >= 60 && startedAt > 0) record(elapsed, completed = false)
running = false
endAt = 0
startedAt = 0
advanceFromFocus()
} else {
running = false
endAt = 0
startedAt = 0
mode = MODE_FOCUS
totalMs = durationFor(MODE_FOCUS)
remainingMs = totalMs
}
clearPause()
clearRoundNotes()
save()
cancelDeadlineAlarm()
notifyChanged()
TimerService.stop(ctx)
}
/** 由前台服务每秒调用 */
fun tick() {
if (!running || countUp) return
@@ -697,23 +687,26 @@ object TimerCore {
}
private fun save() {
Prefs.timerMode = mode
Prefs.timerRunning = running
Prefs.timerCountUp = countUp
Prefs.timerTotalMs = totalMs
Prefs.timerRemainingMs = remainingMs
Prefs.timerEndAt = endAt
Prefs.timerEndElapsed = if (endAt > 0) endElapsed else 0L
Prefs.timerClockOffset = clockOffset
Prefs.timerPauseReason = pendingPauseReason
Prefs.timerPauseStartAt = pendingPauseStartAt
Prefs.timerPauseTaskId = pendingPauseTaskId
Prefs.timerRoundNotes = roundNotes.joinToString("\n")
Prefs.timerStartedAt = startedAt
Prefs.timerTaskId = taskId
Prefs.timerProjectId = projectId
Prefs.timerRound = round
Prefs.timerAccumMs = accumMs
// 一次 Editor 提交代替逐字段 setter(原来要 apply() 十七次)
Prefs.saveTimer(
mode = mode,
running = running,
countUp = countUp,
totalMs = totalMs,
remainingMs = remainingMs,
endAt = endAt,
endElapsed = if (endAt > 0) endElapsed else 0L,
clockOffset = clockOffset,
pauseReason = pendingPauseReason,
roundNotes = roundNotes.joinToString("\n"),
pauseStartAt = pendingPauseStartAt,
pauseTaskId = pendingPauseTaskId,
startedAt = startedAt,
taskId = taskId,
projectId = projectId,
round = round,
accumMs = accumMs,
)
}
fun resetRound() {

View File

@@ -55,7 +55,9 @@ class TimerService : Service() {
TimerCore.tick()
// 锁屏悬浮窗的读数刷新(窗口存在时每秒重画日期/时钟/倒计时)
LockOverlay.update(this@TimerService)
handler.postDelayed(this, 1000L)
// 不再无条件续命:正计时只在锁屏窗口存在时才需要每秒重画,
// 窗口关了就该停,长时间正计时不该一直空转
if (needTicker()) handler.postDelayed(this, 1000L) else stopTicker()
}
}
@@ -112,6 +114,8 @@ class TimerService : Service() {
&& (TimerCore.running || TimerCore.displayNow() > 0)
if (want) LockOverlay.show(this) else LockOverlay.hide()
if (LockOverlay.isShowing()) Notif.cancelFullscreen(this)
// 悬浮窗显隐直接决定正计时要不要每秒刷新(见 needTicker)
syncTicker()
}
/** 灭屏:先补结算,悬浮窗可用就直接盖到锁屏上(保持熄屏,按电源键即见);不可用走全屏意图兜底 */
@@ -177,9 +181,15 @@ class TimerService : Service() {
syncTicker()
}
ACTION_STOP -> {
// 先补前台化再收尾:这条 intent 可能由 startForegroundService() 拉起,
// Android 8+ 要求 5 秒内必须 startForeground(),否则抛
// RemoteServiceException("did not then call startForeground")。
// 原实现在这里直接 stopSelf,把前台化整个跳过了。
startForegroundCompat()
// 休息是纯计时:通知上的"结束"也走 endBreak,不产生任何记录
if (TimerCore.mode == TimerCore.MODE_FOCUS) TimerCore.stopEarly() else TimerCore.endBreak()
stopTicker()
stopForeground(STOP_FOREGROUND_REMOVE)
stopSelf()
return START_NOT_STICKY
}
@@ -195,12 +205,20 @@ class TimerService : Service() {
}
/**
* 只有倒计时需要每秒推进(判断是否到点)。
* 正计时靠时间戳现算、通知靠系统 Chronometer 走时,
* 既不用轮询也不用占着 WakeLock,长时间正计时不会白耗电。
* 是否需要每秒跑一次 ticker。
*
* 倒计时必须跑(判断是否到点);正计时靠时间戳现算、通知靠系统 Chronometer 走时,
* 平时不需要轮询——但锁屏悬浮窗/锁屏页的读数是 Canvas/TextView 自己画的,
* 只有 ticker 会去重画。原来 ticker 只对倒计时启动,正计时一锁屏数字就冻住了。
*/
private fun needTicker(): Boolean {
if (!TimerCore.running) return false
if (!TimerCore.countUp) return true
return LockOverlay.isShowing() || LockScreenActivity.showing
}
private fun syncTicker() {
if (TimerCore.running && !TimerCore.countUp) startTicker() else stopTicker()
if (needTicker()) startTicker() else stopTicker()
}
/** EMUI 偶发拒绝前台启动时的自愈重试标记:只重试一次,避免无限循环 */