Files
FocusBox/src/main/java/com/focusbox/app/service/Notif.kt

168 lines
6.3 KiB
Kotlin
Raw Normal View History

package com.focusbox.app.service
import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.os.Build
import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationManagerCompat
import com.focusbox.app.R
import com.focusbox.app.ui.MainActivity
import com.focusbox.app.util.Dates
/**
* 通知构建集中在这里:常驻计时通知 + 结束提醒,两条通道用途不同。
*
* 常驻通知的时间交给系统 Chronometer 渲染,而不是每秒重发一条通知:
* 锁屏与息屏时第三方进程常被限流,自己刷新文本会"冻住",
* 系统级计时器的走时不受这个限制。
*/
object Notif {
const val CHANNEL_TIMER = "timer_ongoing"
const val CHANNEL_ALERT = "timer_alert"
const val ID_ONGOING = 1001
private const val ID_ALERT = 1002
fun createChannels(context: Context) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) return
val nm = context.getSystemService(NotificationManager::class.java) ?: return
val ongoing = NotificationChannel(
CHANNEL_TIMER,
context.getString(R.string.notif_channel),
NotificationManager.IMPORTANCE_LOW,
).apply {
description = context.getString(R.string.notif_channel_desc)
setShowBadge(false)
enableVibration(false)
setSound(null, null)
}
nm.createNotificationChannel(ongoing)
// 提醒通道交给我们自己播放声音与振动,这里保持静默,避免"响两遍"。
val alert = NotificationChannel(
CHANNEL_ALERT,
"计时结束提醒",
NotificationManager.IMPORTANCE_HIGH,
).apply {
enableVibration(false)
setSound(null, null)
setShowBadge(true)
}
nm.createNotificationChannel(alert)
}
private fun contentIntent(context: Context): PendingIntent {
val i = Intent(context, MainActivity::class.java).apply {
flags = Intent.FLAG_ACTIVITY_SINGLE_TOP or Intent.FLAG_ACTIVITY_CLEAR_TOP
putExtra(MainActivity.EXTRA_TAB, MainActivity.TAB_FOCUS)
}
return PendingIntent.getActivity(
context, 0, i,
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE,
)
}
private fun serviceAction(context: Context, action: String, requestCode: Int): PendingIntent {
val i = Intent(context, TimerService::class.java).setAction(action)
return PendingIntent.getService(
context, requestCode, i,
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE,
)
}
fun buildOngoing(context: Context): Notification {
val running = TimerCore.running
val countUp = TimerCore.countUp
val taskName = if (TimerCore.taskId > 0) {
com.focusbox.app.data.Store.task(TimerCore.taskId)?.title
} else null
val b = NotificationCompat.Builder(context, CHANNEL_TIMER)
// 透明小图标:状态栏与通知左侧不再出现应用图标
.setSmallIcon(R.drawable.ic_notif_blank)
.setContentIntent(contentIntent(context))
.setOngoing(true)
.setOnlyAlertOnce(true)
.setPriority(NotificationCompat.PRIORITY_LOW)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.setSilent(true)
if (running) {
b.setUsesChronometer(true)
if (countUp) {
// 正计时:系统计时器从"已用时"往前推起点,界面上就是一直往上走
b.setWhen(System.currentTimeMillis() - TimerCore.displayNow())
b.setChronometerCountDown(false)
} else {
// 倒计时:渲染到结束时刻,系统自己一秒一秒减
b.setWhen(TimerCore.endAtMs())
b.setChronometerCountDown(true)
}
// 不再出现"专注中/休息中"字样,标题只留任务名(无任务则空白)
b.setContentTitle(taskName ?: " ")
} else {
b.setShowWhen(false)
b.setContentTitle(Dates.clock(TimerCore.displayNow()))
b.setContentText(taskName ?: "")
}
val toggleIcon = if (running) R.drawable.ic_pause else R.drawable.ic_play
b.addAction(
NotificationCompat.Action(
toggleIcon, "",
serviceAction(
context,
if (running) TimerService.ACTION_PAUSE else TimerService.ACTION_RESUME,
11,
),
)
)
b.addAction(
NotificationCompat.Action(
R.drawable.ic_stop, "",
serviceAction(context, TimerService.ACTION_STOP, 13),
)
)
return b.build()
}
fun updateOngoing(context: Context) {
if (!NotificationManagerCompat.from(context).areNotificationsEnabled()) return
runCatching {
NotificationManagerCompat.from(context).notify(ID_ONGOING, buildOngoing(context))
}
}
fun showCompletion(context: Context, finishedMode: Int, breakMinutes: Int) {
if (!NotificationManagerCompat.from(context).areNotificationsEnabled()) return
val focusDone = finishedMode == TimerCore.MODE_FOCUS
val title = context.getString(
if (focusDone) R.string.notif_focus_done else R.string.notif_break_done
)
val text = if (focusDone) {
context.getString(R.string.notif_focus_done_text, breakMinutes)
} else {
context.getString(R.string.notif_break_done_text)
}
val n = NotificationCompat.Builder(context, CHANNEL_ALERT)
.setSmallIcon(R.drawable.ic_check)
.setContentTitle(title)
.setContentText(text)
.setContentIntent(contentIntent(context))
.setAutoCancel(true)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.build()
runCatching { NotificationManagerCompat.from(context).notify(ID_ALERT, n) }
}
fun cancelOngoing(context: Context) {
runCatching { NotificationManagerCompat.from(context).cancel(ID_ONGOING) }
}
}