新增锁屏全屏倒计时:计时中锁屏亮屏即全屏显示日期/时钟/倒计时/任务(v0.16)
This commit is contained in:
@@ -0,0 +1,106 @@
|
||||
package com.focusbox.app.ui.lockscreen
|
||||
|
||||
import android.app.KeyguardManager
|
||||
import android.os.Build
|
||||
import android.os.Bundle
|
||||
import android.os.Handler
|
||||
import android.os.Looper
|
||||
import android.text.format.DateFormat
|
||||
import android.view.WindowManager
|
||||
import android.widget.TextView
|
||||
import androidx.appcompat.app.AppCompatActivity
|
||||
import com.focusbox.app.R
|
||||
import com.focusbox.app.data.Store
|
||||
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 收到 SCREEN_ON →
|
||||
* 检查处于锁屏后发一条全屏意图通知 → 系统把本页直接盖在锁屏上展示。
|
||||
*
|
||||
* 交互极简:黑底上只有日期、时钟、大号倒计时和任务名,
|
||||
* 点按任意处退出;倒计时本身在前台服务里走,退出/熄屏都不受影响,
|
||||
* 计时结束后本页在下一次刷新时自动关闭。
|
||||
*/
|
||||
class LockScreenActivity : AppCompatActivity() {
|
||||
|
||||
private val handler = Handler(Looper.getMainLooper())
|
||||
|
||||
private lateinit var tvDate: TextView
|
||||
private lateinit var tvClock: TextView
|
||||
private lateinit var tvCountdown: TextView
|
||||
private lateinit var tvMode: 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)
|
||||
}
|
||||
}
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
// 盖在锁屏上显示(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
|
||||
)
|
||||
}
|
||||
setContentView(R.layout.activity_lock_screen)
|
||||
|
||||
tvDate = findViewById(R.id.tvLockDate)
|
||||
tvClock = findViewById(R.id.tvLockClock)
|
||||
tvCountdown = findViewById(R.id.tvLockCountdown)
|
||||
tvMode = findViewById(R.id.tvLockMode)
|
||||
tvTask = findViewById(R.id.tvLockTask)
|
||||
|
||||
findViewById<android.view.View>(R.id.lockRoot).setOnClickListener { finish() }
|
||||
}
|
||||
|
||||
override fun onStart() {
|
||||
super.onStart()
|
||||
handler.post(tick)
|
||||
}
|
||||
|
||||
override fun onStop() {
|
||||
handler.removeCallbacks(tick)
|
||||
super.onStop()
|
||||
}
|
||||
|
||||
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)
|
||||
|
||||
tvMode.text = getString(
|
||||
if (TimerCore.mode == TimerCore.MODE_FOCUS) R.string.lock_mode_focus else R.string.lock_mode_break
|
||||
)
|
||||
|
||||
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
|
||||
}
|
||||
@@ -93,6 +93,7 @@ class SettingsFragment : Fragment(R.layout.fragment_settings) {
|
||||
toggle(getString(R.string.sound), Prefs.soundOn) { Prefs.soundOn = it },
|
||||
toggle(getString(R.string.vibrate), Prefs.vibrateOn) { Prefs.vibrateOn = it },
|
||||
toggle(getString(R.string.keep_screen_on), Prefs.keepScreenOn) { Prefs.keepScreenOn = it },
|
||||
toggle(getString(R.string.lock_fullscreen_title), Prefs.lockFullscreen) { Prefs.lockFullscreen = it },
|
||||
)
|
||||
|
||||
section(R.string.section_goal)
|
||||
@@ -373,6 +374,6 @@ class SettingsFragment : Fragment(R.layout.fragment_settings) {
|
||||
}
|
||||
|
||||
companion object {
|
||||
const val VERSION = "0.15"
|
||||
const val VERSION = "0.16"
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user