新增锁屏全屏倒计时:计时中锁屏亮屏即全屏显示日期/时钟/倒计时/任务(v0.16)

This commit is contained in:
XIGE
2026-11-29 12:50:37 +08:00
parent a955318990
commit 0ddd1ec125
10 changed files with 298 additions and 1 deletions

View File

@@ -13,6 +13,8 @@
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_DATA_SYNC" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_SPECIAL_USE" />
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
<!-- 锁屏全屏倒计时:计时中锁屏亮屏时把全屏页盖在锁屏上 -->
<uses-permission android:name="android.permission.USE_FULL_SCREEN_INTENT" />
<uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
@@ -40,6 +42,16 @@
</intent-filter>
</activity>
<activity
android:name=".ui.lockscreen.LockScreenActivity"
android:excludeFromRecents="true"
android:exported="false"
android:screenOrientation="portrait"
android:showWhenLocked="true"
android:taskAffinity=""
android:theme="@style/LockScreenTheme"
android:turnScreenOn="true" />
<service
android:name=".service.TimerService"
android:exported="false"

View File

@@ -30,9 +30,13 @@ object Notif {
const val CHANNEL_TIMER = "timer_ongoing_v2"
private const val LEGACY_CHANNEL_TIMER = "timer_ongoing"
const val CHANNEL_ALERT = "timer_alert"
// 锁屏全屏倒计时的触发渠道:高重要度才有资格发全屏意图,
// 但声音振动仍全关(亮屏本身就是提醒,不需要再响)
const val CHANNEL_FULLSCREEN = "timer_lock"
const val ID_ONGOING = 1001
private const val ID_ALERT = 1002
private const val ID_FULLSCREEN = 1003
fun createChannels(context: Context) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) return
@@ -65,6 +69,19 @@ object Notif {
setShowBadge(true)
}
nm.createNotificationChannel(alert)
// 全屏倒计时触发渠道:仅用于把锁屏页拉起来,本身不出声音
val fullscreen = NotificationChannel(
CHANNEL_FULLSCREEN,
context.getString(R.string.notif_channel),
NotificationManager.IMPORTANCE_HIGH,
).apply {
description = context.getString(R.string.notif_channel_desc)
setShowBadge(false)
enableVibration(false)
setSound(null, null)
}
nm.createNotificationChannel(fullscreen)
}
private fun contentIntent(context: Context): PendingIntent {
@@ -204,4 +221,38 @@ object Notif {
fun cancelOngoing(context: Context) {
runCatching { NotificationManagerCompat.from(context).cancel(ID_ONGOING) }
}
/**
* 锁屏全屏倒计时的触发通知:只在「计时中 + 处于锁屏」时由 TimerService 发出。
* 全屏意图把 LockScreenActivity 直接盖在锁屏上,用户亮屏即见倒计时;
* 若系统/用户禁用了全屏意图,这条通知留在抽屉里,点它也能进同一页面。
*/
fun updateFullscreen(context: Context) {
if (!NotificationManagerCompat.from(context).areNotificationsEnabled()) return
val headline = if (TimerCore.taskId > 0) {
com.focusbox.app.data.Store.task(TimerCore.taskId)?.title
} else null
val pi = android.app.PendingIntent.getActivity(
context, 2,
Intent(context, com.focusbox.app.ui.lockscreen.LockScreenActivity::class.java)
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK),
android.app.PendingIntent.FLAG_UPDATE_CURRENT or android.app.PendingIntent.FLAG_IMMUTABLE,
)
val n = NotificationCompat.Builder(context, CHANNEL_FULLSCREEN)
.setSmallIcon(R.drawable.ic_notif)
.setContentTitle(headline ?: context.getString(R.string.notif_no_task))
.setContentText(Dates.clock(TimerCore.displayNow()))
.setFullScreenIntent(pi, true)
.setContentIntent(pi)
.setOngoing(true)
.setCategory(NotificationCompat.CATEGORY_ALARM)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setAutoCancel(true)
.build()
runCatching { NotificationManagerCompat.from(context).notify(ID_FULLSCREEN, n) }
}
fun cancelFullscreen(context: Context) {
runCatching { NotificationManagerCompat.from(context).cancel(ID_FULLSCREEN) }
}
}

View File

@@ -1,13 +1,17 @@
package com.focusbox.app.service
import android.app.KeyguardManager
import android.app.Service
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.os.Handler
import android.os.IBinder
import android.os.Looper
import android.os.PowerManager
import androidx.core.content.ContextCompat
import com.focusbox.app.util.Prefs
/**
* 计时前台服务。
@@ -55,6 +59,35 @@ class TimerService : Service() {
override fun onCreate() {
super.onCreate()
TimerCore.addListener(onStateChanged)
// 锁屏全屏倒计时:计时中亮屏(用户按电源键偷看)→ 发全屏意图把
// LockScreenActivity 盖在锁屏上;灭屏时撤回触发通知,不在抽屉里堆垃圾
ContextCompat.registerReceiver(
this,
screenReceiver,
IntentFilter().apply {
addAction(Intent.ACTION_SCREEN_ON)
addAction(Intent.ACTION_SCREEN_OFF)
},
ContextCompat.RECEIVER_NOT_EXPORTED,
)
}
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)
}
}
}
private fun showLockPeek() {
if (!Prefs.lockFullscreen) return
if (!TimerCore.running) return
val km = getSystemService(Context.KEYGUARD_SERVICE) as KeyguardManager
// 只有真在锁屏界面才拉全屏页;用户正在用手机时不打扰
if (!km.inKeyguardRestrictedInputMode()) return
Notif.updateFullscreen(this)
}
private val onStateChanged: () -> Unit = {
@@ -171,6 +204,8 @@ class TimerService : Service() {
override fun onDestroy() {
stopTicker()
releaseWakeLock()
runCatching { unregisterReceiver(screenReceiver) }
Notif.cancelFullscreen(this)
TimerCore.removeListener(onStateChanged)
super.onDestroy()
}

View File

@@ -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
}

View File

@@ -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"
}
}

View File

@@ -56,6 +56,11 @@ object Prefs {
get() = sp.getBoolean("keep_screen", true)
set(v) = sp.edit().putBoolean("keep_screen", v).apply()
/** 计时中锁屏亮屏时,全屏显示日期/时钟/倒计时/任务 */
var lockFullscreen: Boolean
get() = sp.getBoolean("lock_fullscreen", true)
set(v) = sp.edit().putBoolean("lock_fullscreen", v).apply()
// ---- 目标 ----
var dailyGoalMinutes: Int
get() = sp.getInt("daily_goal", 120)

View File

@@ -0,0 +1,70 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- 锁屏全屏倒计时:纯黑底极简排版,只有日期/时钟/倒计时/任务 -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/lockRoot"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/black"
android:clickable="true"
android:focusable="true"
android:gravity="center"
android:orientation="vertical">
<TextView
android:id="@+id/tvLockDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#99FFFFFF"
android:textSize="15sp"
android:letterSpacing="0.08" />
<TextView
android:id="@+id/tvLockClock"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="6dp"
android:includeFontPadding="false"
android:textColor="#CCFFFFFF"
android:textSize="52sp"
android:textStyle="bold" />
<TextView
android:id="@+id/tvLockCountdown"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="56dp"
android:includeFontPadding="false"
android:textColor="#FFFFFF"
android:textSize="96sp"
android:textStyle="bold" />
<TextView
android:id="@+id/tvLockMode"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="14dp"
android:letterSpacing="0.2"
android:textColor="#66FFFFFF"
android:textSize="13sp" />
<TextView
android:id="@+id/tvLockTask"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:ellipsize="end"
android:maxWidth="280dp"
android:maxLines="1"
android:textColor="#CCFFFFFF"
android:textSize="17sp" />
<TextView
android:id="@+id/tvLockHint"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="64dp"
android:text="@string/lock_hint"
android:textColor="#40FFFFFF"
android:textSize="12sp" />
</LinearLayout>

View File

@@ -187,6 +187,10 @@
<string name="sound">结束提示音</string>
<string name="vibrate">振动</string>
<string name="keep_screen_on">计时时保持屏幕常亮</string>
<string name="lock_fullscreen_title">锁屏全屏倒计时</string>
<string name="lock_mode_focus">专注中</string>
<string name="lock_mode_break">休息中</string>
<string name="lock_hint">点按任意处退出 · 倒计时在后台继续</string>
<string name="daily_goal">每日目标</string>
<string name="theme">深浅色</string>
<string name="theme_follow">跟随系统</string>

View File

@@ -17,6 +17,15 @@
<item name="android:colorControlHighlight">@color/ripple_surface</item>
</style>
<!-- 锁屏全屏倒计时页:纯黑底、白色字,不随深浅色主题切换 -->
<style name="LockScreenTheme" parent="Theme.AppCompat.NoActionBar">
<item name="android:windowBackground">@android:color/black</item>
<item name="android:statusBarColor">@android:color/black</item>
<item name="android:navigationBarColor">@android:color/black</item>
<item name="android:windowLightStatusBar">false</item>
<item name="android:windowLightNavigationBar" tools:targetApi="27">false</item>
</style>
<!--
样式名里的点号会被 aapt 当作隐式父样式(T.Title 的父是 T),
这里必须先声明空的 T,否则资源链接直接报 style/T not found。