锁屏倒计时改灭屏投递拉起不再出横幅;沉浸式遮状态栏;横屏专属排版;版本 1.1

This commit is contained in:
XIGE
2026-11-29 13:54:10 +08:00
parent 1d2955c557
commit bc01e0ac67
6 changed files with 221 additions and 38 deletions

View File

@@ -11,6 +11,7 @@ import android.os.IBinder
import android.os.Looper
import android.os.PowerManager
import androidx.core.content.ContextCompat
import com.focusbox.app.ui.lockscreen.LockScreenActivity
import com.focusbox.app.util.Prefs
/**
@@ -75,37 +76,55 @@ class TimerService : Service() {
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)
Intent.ACTION_SCREEN_ON -> onScreenOn()
Intent.ACTION_SCREEN_OFF -> onScreenOff()
}
}
}
private fun showLockPeek() {
// 先补结算:EMUI 冻结进程后到点闹钟可能被推迟,用户按电源键亮屏
// 这一瞬进程已解冻,正好把过期的倒计时立刻结算掉,
// 通知/锁屏卡片就不会再滚负数
TimerCore.tick()
if (!Prefs.lockFullscreen) return
/**
* 灭屏瞬间投递全屏意图通知——这是鸿蒙唯一会直接拉起 LockScreenActivity 的窗口。
* 鸿蒙 4.0 的规则:全屏意图只在「锁屏 + 灭屏」时投递才直接把页面盖在锁屏上;
* 亮屏后投递永远只出横幅;服务直接 startActivity 则被"后台弹出界面"策略静默拦截
* (实测开了权限也一样)。所以改成灭屏时就投递:页面(turnScreenOn=false)
* 悄悄盖在锁屏底下、屏幕保持熄灭,用户按电源键亮屏就直接看到倒计时,
* 不会再出现任何通知横幅。
*/
private fun onScreenOff() {
if (!Prefs.lockFullscreen || !TimerCore.running) return
TimerCore.tick() // 灭屏一瞬先补结算,避免到点边界上拉出 00:00 的空页面
if (!TimerCore.running) return
val km = getSystemService(Context.KEYGUARD_SERVICE) as KeyguardManager
// 只有真在锁屏界面才拉全屏页;用户正在用手机时不打扰
if (!km.inKeyguardRestrictedInputMode()) return
// 鸿蒙上全屏意图只在"锁屏且灭屏时投递"才会直接拉起 Activity,
// 亮屏后投递只会出横幅通知——所以直接从服务启动页面(鸿蒙允许
// 前台服务拉起自己的 Activity),全屏意图通知保留作兜底
runCatching {
startActivity(
Intent(this, com.focusbox.app.ui.lockscreen.LockScreenActivity::class.java)
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
)
}
if (!km.isKeyguardLocked()) return
Notif.updateFullscreen(this)
}
/** 亮屏:先补结算(冻结进程的到点闹钟被推迟时,靠这一下止损,不再滚负数) */
private fun onScreenOn() {
TimerCore.tick()
if (LockScreenActivity.showing) {
// 页面已经盖在锁屏上(灭屏投递拉起的),抽屉里的触发通知清掉
Notif.cancelFullscreen(this)
} else if (Prefs.lockFullscreen && TimerCore.running) {
val km = getSystemService(Context.KEYGUARD_SERVICE) as KeyguardManager
if (km.isKeyguardLocked()) {
// 灭屏投递没拉起来(个别 ROM 拦全屏意图)的兜底:
// 先试服务直启,再留一条全屏意图通知让用户点进同一页面
runCatching {
startActivity(
Intent(this, LockScreenActivity::class.java)
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
)
}
Notif.updateFullscreen(this)
}
}
}
private val onStateChanged: () -> Unit = {
Notif.updateOngoing(this)
// 暂停/结束时同步停掉 1 秒轮询,别让 Handler 空转
// 计时结束/暂停时清掉锁屏触发通知,抽屉里不留垃圾
if (!TimerCore.running) Notif.cancelFullscreen(this)
syncTicker()
syncWakeLock()
}

View File

@@ -1,16 +1,22 @@
package com.focusbox.app.ui.lockscreen
import android.app.KeyguardManager
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.os.Build
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.text.format.DateFormat
import android.view.View
import android.view.WindowManager
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
import com.focusbox.app.R
import com.focusbox.app.data.Store
import com.focusbox.app.service.Notif
import com.focusbox.app.service.TimerCore
import com.focusbox.app.util.Dates
import java.text.SimpleDateFormat
@@ -20,15 +26,26 @@ import java.util.Locale
/**
* 锁屏全屏倒计时页。
*
* 触发链路:计时中按电源键亮屏 → TimerService 收到 SCREEN_ON →
* 检查处于锁屏后发一条全屏意图通知 → 系统把本页直接盖在锁屏上展示。
* 触发链路:计时中用户锁屏(灭屏)→ TimerService 灭屏瞬间发全屏意图通知 →
* 鸿蒙直接把本页盖在锁屏上(屏幕保持熄灭,本页不设 turnScreenOn)→
* 用户按电源键亮屏即见倒计时,全程不出通知横幅。
*
* 交互极简:黑底上只有日期、时钟、大号倒计时和任务名,
* 点按任意处退出;倒计时本身在前台服务里走,退出/熄屏都不受影响,
* 计时结束后本页在下一次刷新时自动关闭。
* 点按任意处退出、解锁时自动退出;倒计时本身在前台服务里走,
* 退出/熄屏都不受影响,计时结束后本页在下一次刷新时自动关闭。
*
* 沉浸式:STATUS_BAR_HIDDEN + LAYOUT_NO_LIMITS 把顶部的时间/信号/电池
* 一起遮掉,整块屏幕只有页面内容。
*/
class LockScreenActivity : AppCompatActivity() {
companion object {
/** 服务用来判断页面是否已盖在锁屏上(避免重复拉起/多报通知) */
@Volatile
var showing: Boolean = false
private set
}
private val handler = Handler(Looper.getMainLooper())
private lateinit var tvDate: TextView
@@ -46,18 +63,23 @@ class LockScreenActivity : AppCompatActivity() {
}
}
/** 用户解锁(输密码/指纹进系统)后页面没有存在意义,立即退出 */
private val unlockReceiver = object : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
if (intent.action == Intent.ACTION_USER_PRESENT) finish()
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// 盖在锁屏上显示(API 27+ 用官方方法,清单里的 showWhenLocked/turnScreenOn 双保险)
// 盖在锁屏上显示(API 27+ 用官方方法,清单里的 showWhenLocked 双保险)。
// 注意不能开 turnScreenOn:灭屏投递拉起本页时屏幕要保持熄灭,
// 等用户按电源键才亮,否则每次锁屏都会自动亮屏耗电
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) {
setShowWhenLocked(true)
setTurnScreenOn(true)
} else {
@Suppress("DEPRECATION")
window.addFlags(
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
or WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
)
window.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED)
}
setContentView(R.layout.activity_lock_screen)
@@ -67,19 +89,60 @@ class LockScreenActivity : AppCompatActivity() {
tvMode = findViewById(R.id.tvLockMode)
tvTask = findViewById(R.id.tvLockTask)
findViewById<android.view.View>(R.id.lockRoot).setOnClickListener { finish() }
findViewById<View>(R.id.lockRoot).setOnClickListener { finish() }
ContextCompat.registerReceiver(
this,
unlockReceiver,
IntentFilter(Intent.ACTION_USER_PRESENT),
ContextCompat.RECEIVER_NOT_EXPORTED,
)
}
override fun onStart() {
super.onStart()
showing = true
// 页面已经实际盖到锁屏上了,抽屉里那条触发通知清掉
Notif.cancelFullscreen(this)
applyImmersive()
handler.post(tick)
}
override fun onStop() {
showing = false
handler.removeCallbacks(tick)
super.onStop()
}
override fun onDestroy() {
runCatching { unregisterReceiver(unlockReceiver) }
super.onDestroy()
}
/** 沉浸式全屏:状态栏/导航栏全隐藏,且布局延伸到状态栏底下(时间信号电池彻底不见) */
private fun applyImmersive() {
@Suppress("DEPRECATION")
window.setFlags(
WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,
WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,
)
@Suppress("DEPRECATION")
window.decorView.systemUiVisibility = (
View.SYSTEM_UI_FLAG_LAYOUT_STABLE
or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
or View.SYSTEM_UI_FLAG_FULLSCREEN
or View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
)
}
/** 从别的窗口/通知横幅抢回焦点后,沉浸式标记会被系统清掉,重新套一遍 */
override fun onWindowFocusChanged(hasFocus: Boolean) {
super.onWindowFocusChanged(hasFocus)
if (hasFocus) applyImmersive()
}
private fun render() {
val now = TimerCore.displayNow()
// 计时已结束且没有剩余读数:页面使命完成,自动关闭

View File

@@ -431,6 +431,6 @@ class SettingsFragment : Fragment(R.layout.fragment_settings) {
}
companion object {
const val VERSION = "1.0"
const val VERSION = "1.1"
}
}