5 Commits
v1.2 ... v1.4

9 changed files with 225 additions and 42 deletions

View File

@@ -1,8 +1,20 @@
# 变更记录
## 2026-11-29 · 项目排序模式序号与置顶;横屏锁屏遮状态栏、排版对称化;版本 1.2
## 2026-11-29 · 锁屏倒计时主链路改悬浮窗:WindowManager 直接画在锁屏上,不受"后台弹出界面"限制(v1.4)
- 同步源码更新 10 个文件(新增/修改 10,删除 0)。
- 根因链补全:鸿蒙 4.0 的"后台弹出界面"策略只拦 startActivity,悬浮窗(WindowManager.addView)不属于 Activity 启动、不在拦截范围——这正是专注清单等 App 锁屏显示稳定的原因(其权限列表含"悬浮窗")
- 新增 SYSTEM_ALERT_WINDOW 权限 + service/LockOverlay.kt:计时中锁屏/灭屏时把黑底倒计时窗口(TYPE_APPLICATION_OVERLAY + FLAG_SHOW_WHEN_LOCKED)直接画在锁屏之上;复用锁屏页布局(自动选竖屏/横屏版本,旋转重建窗口);FLAG_NOT_FOCUSABLE 不影响锁屏密码输入;点按任意处关闭;解锁(USER_PRESENT)自动关闭;计时结束自动关闭
- 三级降级链路:悬浮窗(有权限,主链路)→ 全屏意图通知/服务直启 → 抽屉通知点进锁屏页
- 设置页新增「悬浮窗权限(锁屏显示)」入口:有公开检测 API(canDrawOverlays),显示"已开启/去开启",一键跳本应用授权页
- 版本 1.3 → 1.4(versionCode 38)
## 2026-11-29 · 竖屏删「工作中」提示;横屏时间加大、下方文字统一规格并收紧间距(v1.3)
- 竖屏锁屏页删除「工作中/休息中」字样(代码同步移除 tvLockMode 赋值,横竖屏均无该控件)
- 横屏两个时间(倒计时/时钟)96sp → 130sp,尽可能大
- 横屏时间下方文字(左任务名 / 右日期星期)统一为 15sp、同色 #99FFFFFF,与时间距离由 20dp 收紧到 10dp(约一半)
- 右侧日期星期去掉 letterSpacing,与时钟严格左右居中(与左侧倒计时/任务名的居中方式一致)
- 版本 1.2 → 1.3(versionCode 37)
## 2026-11-29 · 项目排序模式(序号+置顶);横屏锁屏遮状态栏、排版对称化(v1.2)

View File

@@ -15,6 +15,8 @@
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
<!-- 锁屏全屏倒计时:计时中锁屏亮屏时把全屏页盖在锁屏上 -->
<uses-permission android:name="android.permission.USE_FULL_SCREEN_INTENT" />
<!-- 锁屏悬浮窗:鸿蒙拦 Activity 后台启动,改用 WindowManager 直接把倒计时画在锁屏上(addView 不受"后台弹出界面"限制) -->
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />

View File

@@ -0,0 +1,129 @@
package com.focusbox.app.service
import android.content.Context
import android.content.res.Configuration
import android.graphics.PixelFormat
import android.os.Build
import android.provider.Settings
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.WindowManager
import android.widget.TextView
import com.focusbox.app.R
import com.focusbox.app.data.Store
import com.focusbox.app.util.Dates
import java.text.SimpleDateFormat
import java.util.Calendar
import java.util.Locale
/**
* 锁屏悬浮窗:计时中锁屏/灭屏时,把黑底倒计时窗口直接画在锁屏之上。
*
* 为什么用悬浮窗:鸿蒙 4.0 对「后台弹出界面」的限制只拦 startActivity
* (实测开了权限也拦,全屏意图灭屏投递也不可靠),而 WindowManager.addView
* 悬浮窗不属于 Activity 启动,只要用户授了「悬浮窗」权限就能稳定盖在锁屏上
* ——这正是专注清单等 App 锁屏显示的做法。
*
* 窗口内容直接复用锁屏页布局(activity_lock_screen),inflate 时系统按当前
* 横竖屏自动选 layout 或 layout-land,旋转后重建窗口即可切换排版。
* FLAG_NOT_FOCUSABLE 不抢按键(不影响锁屏密码输入),点击任意处关闭。
*/
object LockOverlay {
private var root: View? = null
private var wm: WindowManager? = null
private var orientation = Configuration.ORIENTATION_PORTRAIT
private val clockFmt = SimpleDateFormat("HH:mm", Locale.getDefault())
/** 悬浮窗窗口是否正显示在锁屏上 */
fun isShowing(): Boolean = root != null
/** 用户是否已授予「悬浮窗」权限 */
fun canShow(context: Context): Boolean = Settings.canDrawOverlays(context)
/** 显示(幂等):已在显示或无权限时不重复添加 */
fun show(context: Context) {
if (root != null || !canShow(context)) return
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() }
// API 26+ 用应用悬浮窗类型;更旧版本退回 TYPE_PHONE(本应用 minSdk 24)
val type = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY
} else {
@Suppress("DEPRECATION")
WindowManager.LayoutParams.TYPE_PHONE
}
val lp = WindowManager.LayoutParams(
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.MATCH_PARENT,
type,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
or WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
or WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
or WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,
PixelFormat.TRANSLUCENT,
)
lp.gravity = Gravity.CENTER
manager.addView(view, lp)
root = view
wm = manager
orientation = ctx.resources.configuration.orientation
render(view)
} catch (_: Exception) {
root = null
wm = null
}
}
/** 关闭(幂等):窗口不存在时无事发生 */
fun hide() {
val view = root ?: return
root = null
wm?.let { runCatching { it.removeView(view) } }
wm = null
}
/**
* 每秒刷新读数;横竖屏变化时重建窗口(悬浮窗不经过 Activity 重建,
* 只能自己检测 configuration 并换布局重新 inflate)。
*/
fun update(context: Context) {
val view = root ?: return
val current = context.resources.configuration.orientation
if (current != orientation) {
hide()
show(context)
return
}
render(view)
}
private fun render(view: View) {
val now = TimerCore.displayNow()
// 计时结束:窗口使命完成,自动关闭
if (!TimerCore.running && now <= 0) {
hide()
return
}
val cal = Calendar.getInstance()
view.findViewById<TextView>(R.id.tvLockDate)?.text =
"${Dates.mdLong(cal.timeInMillis)} 星期${Dates.weekdayShort(weekdayIndex(cal))}"
view.findViewById<TextView>(R.id.tvLockClock)?.text = clockFmt.format(cal.time)
view.findViewById<TextView>(R.id.tvLockCountdown)?.text = Dates.clock(now)
val task = if (TimerCore.taskId > 0) Store.task(TimerCore.taskId)?.title else null
view.findViewById<TextView>(R.id.tvLockTask)?.text =
task?.takeIf { it.isNotBlank() } ?: view.context.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

@@ -53,6 +53,8 @@ class TimerService : Service() {
// 只推进状态,不再每秒重发通知:通知里的时间是系统的 Chronometer 在走,
// 每秒 notify 反而会让锁屏上的计时被系统限流后"冻住"。
TimerCore.tick()
// 锁屏悬浮窗的读数刷新(窗口存在时每秒重画日期/时钟/倒计时)
LockOverlay.update(this@TimerService)
handler.postDelayed(this, 1000L)
}
}
@@ -60,8 +62,8 @@ class TimerService : Service() {
override fun onCreate() {
super.onCreate()
TimerCore.addListener(onStateChanged)
// 锁屏全屏倒计时:计时中亮屏(用户按电源键偷看)→ 发全屏意图把
// LockScreenActivity 盖在锁屏上;灭屏时撤回触发通知,不在抽屉里堆垃圾
// 锁屏倒计时主链路:悬浮窗(WindowManager 直接画在锁屏上,
// 不受"后台弹出界面"限制);灭屏/亮屏/解锁三个时机同步显隐
ContextCompat.registerReceiver(
this,
screenReceiver,
@@ -71,6 +73,13 @@ class TimerService : Service() {
},
ContextCompat.RECEIVER_NOT_EXPORTED,
)
// 用户解锁(输密码/指纹进入系统)后悬浮窗没有存在意义,立即关闭
ContextCompat.registerReceiver(
this,
unlockReceiver,
IntentFilter(Intent.ACTION_USER_PRESENT),
ContextCompat.RECEIVER_NOT_EXPORTED,
)
}
private val screenReceiver = object : BroadcastReceiver() {
@@ -82,34 +91,52 @@ class TimerService : Service() {
}
}
private val unlockReceiver = object : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
if (intent.action == Intent.ACTION_USER_PRESENT) {
LockOverlay.hide()
Notif.cancelFullscreen(this@TimerService)
}
}
}
/**
* 灭屏瞬间投递全屏意图通知——这是鸿蒙唯一会直接拉起 LockScreenActivity 的窗口。
* 鸿蒙 4.0 的规则:全屏意图只在「锁屏 + 灭屏」时投递才直接把页面盖在锁屏上;
* 亮屏后投递永远只出横幅;服务直接 startActivity 则被"后台弹出界面"策略静默拦截
* (实测开了权限也一样)。所以改成灭屏时就投递:页面(turnScreenOn=false)
* 悄悄盖在锁屏底下、屏幕保持熄灭,用户按电源键亮屏就直接看到倒计时,
* 不会再出现任何通知横幅。
* 悬浮窗显隐的总开关:锁屏中 + 计时未清零 + 开关打开 + 有悬浮窗权限。
* 显示成功时顺手清掉抽屉里的全屏意图触发通知(不再需要)。
*/
private fun onScreenOff() {
if (!Prefs.lockFullscreen || !TimerCore.running) return
TimerCore.tick() // 灭屏一瞬先补结算,避免到点边界上拉出 00:00 的空页面
if (!TimerCore.running) return
private fun syncLockOverlay() {
val km = getSystemService(Context.KEYGUARD_SERVICE) as KeyguardManager
if (!km.isKeyguardLocked()) return
Notif.updateFullscreen(this)
val want = Prefs.lockFullscreen
&& LockOverlay.canShow(this)
&& km.isKeyguardLocked()
&& (TimerCore.running || TimerCore.displayNow() > 0)
if (want) LockOverlay.show(this) else LockOverlay.hide()
if (LockOverlay.isShowing()) Notif.cancelFullscreen(this)
}
/** 灭屏:先补结算,悬浮窗可用就直接盖到锁屏上(保持熄屏,按电源键即见);不可用走全屏意图兜底 */
private fun onScreenOff() {
TimerCore.tick() // 灭屏一瞬先补结算,避免到点边界上拉出 00:00 的空页面
if (!Prefs.lockFullscreen || !TimerCore.running) return
syncLockOverlay()
if (LockOverlay.isShowing()) return
// 无悬浮窗权限的兜底:灭屏投递全屏意图(个别 ROM 直启,其余进抽屉可点)
val km = getSystemService(Context.KEYGUARD_SERVICE) as KeyguardManager
if (km.isKeyguardLocked()) Notif.updateFullscreen(this)
}
/** 亮屏:先补结算(冻结进程的到点闹钟被推迟时,靠这一下止损,不再滚负数) */
private fun onScreenOn() {
TimerCore.tick()
syncLockOverlay()
if (LockOverlay.isShowing()) return
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)
@@ -125,6 +152,7 @@ class TimerService : Service() {
Notif.updateOngoing(this)
// 计时结束/暂停时清掉锁屏触发通知,抽屉里不留垃圾
if (!TimerCore.running) Notif.cancelFullscreen(this)
syncLockOverlay()
syncTicker()
syncWakeLock()
}
@@ -236,7 +264,9 @@ class TimerService : Service() {
override fun onDestroy() {
stopTicker()
releaseWakeLock()
LockOverlay.hide()
runCatching { unregisterReceiver(screenReceiver) }
runCatching { unregisterReceiver(unlockReceiver) }
Notif.cancelFullscreen(this)
TimerCore.removeListener(onStateChanged)
super.onDestroy()

View File

@@ -156,11 +156,6 @@ class LockScreenActivity : AppCompatActivity() {
tvClock.text = clockFmt.format(cal.time)
tvCountdown.text = Dates.clock(now)
// 工作中/休息中提示只有竖屏布局有(横屏排版里删掉了),横屏下 findViewById 返回 null
findViewById<TextView>(R.id.tvLockMode)?.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)
}

View File

@@ -94,7 +94,16 @@ class SettingsFragment : Fragment(R.layout.fragment_settings) {
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 },
// 华为/鸿蒙默认拦截后台拉起页面,锁屏倒计时需要手动开两个权限;
// 悬浮窗权限:锁屏倒计时主链路(鸿蒙拦 Activity 后台启动,悬浮窗不受限)
action(
getString(R.string.overlay_perm_title),
if (com.focusbox.app.service.LockOverlay.canShow(requireContext())) {
getString(R.string.perm_granted)
} else {
getString(R.string.go_set)
},
) { openOverlayPerm() },
// 华为/鸿蒙默认拦截后台拉起页面,无悬浮窗权限时的 Activity 兜底链路也要这两项权限;
// 放在开关正下方,用户开了开关不生效时第一眼就能看到入口
action(getString(R.string.lock_perm_title), getString(R.string.lock_perm_value)) { openLockPerm() },
)
@@ -319,6 +328,20 @@ class SettingsFragment : Fragment(R.layout.fragment_settings) {
dialog.show()
}
/** 悬浮窗权限:标准 Android 特殊权限,有公开检测 API 和授权页,一键直达本应用 */
private fun openOverlayPerm() {
val ctx = requireContext()
if (com.focusbox.app.service.LockOverlay.canShow(ctx)) return
runCatching {
startActivity(
android.content.Intent(
android.provider.Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
android.net.Uri.parse("package:${ctx.packageName}"),
).addFlags(android.content.Intent.FLAG_ACTIVITY_NEW_TASK)
)
}
}
/** 锁屏显示权限:弹说明后直达华为「应用启动管理」,非华为/直达失败退回应用详情页 */
private fun openLockPerm() {
val ctx = requireContext()
@@ -431,6 +454,6 @@ class SettingsFragment : Fragment(R.layout.fragment_settings) {
}
companion object {
const val VERSION = "1.2"
const val VERSION = "1.4"
}
}

View File

@@ -28,19 +28,19 @@
android:layout_height="wrap_content"
android:includeFontPadding="false"
android:textColor="#FFFFFF"
android:textSize="96sp"
android:textSize="130sp"
android:textStyle="bold" />
<TextView
android:id="@+id/tvLockTask"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_marginTop="10dp"
android:ellipsize="end"
android:maxWidth="320dp"
android:maxLines="1"
android:textColor="#CCFFFFFF"
android:textSize="17sp" />
android:textColor="#99FFFFFF"
android:textSize="15sp" />
</LinearLayout>
<!-- 中:细分隔线 -->
@@ -63,15 +63,14 @@
android:layout_height="wrap_content"
android:includeFontPadding="false"
android:textColor="#FFFFFF"
android:textSize="96sp"
android:textSize="130sp"
android:textStyle="bold" />
<TextView
android:id="@+id/tvLockDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:letterSpacing="0.08"
android:layout_marginTop="10dp"
android:textColor="#99FFFFFF"
android:textSize="15sp" />
</LinearLayout>

View File

@@ -38,15 +38,6 @@
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"

View File

@@ -190,6 +190,8 @@
<string name="lock_fullscreen_title">锁屏全屏倒计时</string>
<string name="lock_perm_title">锁屏显示权限</string>
<string name="lock_perm_value">去开启</string>
<string name="overlay_perm_title">悬浮窗权限(锁屏显示)</string>
<string name="perm_granted">已开启</string>
<string name="go_set">去设置</string>
<string name="project_sort_index">排序序号</string>
<string name="sort_index_hint">序号,越小越靠前</string>