新增锁屏全屏倒计时:计时中锁屏亮屏即全屏显示日期/时钟/倒计时/任务(v0.16)
This commit is contained in:
@@ -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) }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user