166 lines
6.0 KiB
Kotlin
166 lines
6.0 KiB
Kotlin
package com.focusbox.app.ui.lockscreen
|
||
|
||
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
|
||
import java.util.Calendar
|
||
import java.util.Locale
|
||
|
||
/**
|
||
* 锁屏全屏倒计时页。
|
||
*
|
||
* 触发链路:计时中用户锁屏(灭屏)→ 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
|
||
private lateinit var tvClock: TextView
|
||
private lateinit var tvCountdown: TextView
|
||
private lateinit var tvTask: TextView
|
||
|
||
private val clockFmt = SimpleDateFormat("HH:mm", Locale.getDefault())
|
||
|
||
private val tick = object : Runnable {
|
||
override fun run() {
|
||
render()
|
||
handler.postDelayed(this, 500L)
|
||
}
|
||
}
|
||
|
||
/** 用户解锁(输密码/指纹进系统)后页面没有存在意义,立即退出 */
|
||
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:灭屏投递拉起本页时屏幕要保持熄灭,
|
||
// 等用户按电源键才亮,否则每次锁屏都会自动亮屏耗电
|
||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) {
|
||
setShowWhenLocked(true)
|
||
} else {
|
||
@Suppress("DEPRECATION")
|
||
window.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED)
|
||
}
|
||
setContentView(R.layout.activity_lock_screen)
|
||
|
||
tvDate = findViewById(R.id.tvLockDate)
|
||
tvClock = findViewById(R.id.tvLockClock)
|
||
tvCountdown = findViewById(R.id.tvLockCountdown)
|
||
tvTask = findViewById(R.id.tvLockTask)
|
||
|
||
findViewById<View>(R.id.lockRoot).setOnClickListener { finish() }
|
||
|
||
applyImmersive()
|
||
|
||
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()
|
||
// 计时已结束且没有剩余读数:页面使命完成,自动关闭
|
||
if (!TimerCore.running && now <= 0) {
|
||
finish()
|
||
return
|
||
}
|
||
|
||
val cal = Calendar.getInstance()
|
||
tvDate.text = "${Dates.mdLong(cal.timeInMillis)} 星期${Dates.weekdayShort(weekdayIndex(cal))}"
|
||
tvClock.text = clockFmt.format(cal.time)
|
||
tvCountdown.text = Dates.clock(now)
|
||
|
||
val task = if (TimerCore.taskId > 0) Store.task(TimerCore.taskId)?.title else null
|
||
tvTask.text = task?.takeIf { it.isNotBlank() } ?: getString(R.string.notif_no_task)
|
||
}
|
||
|
||
/** Calendar 的 DAY_OF_WEEK(周日=1)转成 weekdayShort 的下标(周一=0 … 周日=6) */
|
||
private fun weekdayIndex(cal: Calendar): Int = (cal.get(Calendar.DAY_OF_WEEK) + 5) % 7
|
||
}
|