v0.3.0:今日筛选含逾期与今日完成、统计今日区间、重复任务自动滚动、通知自定义布局
This commit is contained in:
@@ -7,6 +7,7 @@ import android.app.PendingIntent
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.os.Build
|
||||
import android.widget.RemoteViews
|
||||
import androidx.core.app.NotificationCompat
|
||||
import androidx.core.app.NotificationManagerCompat
|
||||
import com.focusbox.app.R
|
||||
@@ -82,6 +83,9 @@ object Notif {
|
||||
val taskName = if (TimerCore.taskId > 0) {
|
||||
com.focusbox.app.data.Store.task(TimerCore.taskId)?.title
|
||||
} else null
|
||||
// 没有选任务时给一个中性占位,避免第一行留空白
|
||||
val headline = taskName?.takeIf { it.isNotBlank() }
|
||||
?: context.getString(R.string.notif_no_task)
|
||||
|
||||
val b = NotificationCompat.Builder(context, CHANNEL_TIMER)
|
||||
// 透明小图标:状态栏与通知左侧不再出现应用图标
|
||||
@@ -92,46 +96,85 @@ object Notif {
|
||||
.setPriority(NotificationCompat.PRIORITY_LOW)
|
||||
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
|
||||
.setSilent(true)
|
||||
// 系统模板的"应用名 + 时间"行不需要,时间由自定义布局里的 Chronometer 承担
|
||||
.setShowWhen(false)
|
||||
// 模板兜底:ROM 若不支持自定义布局,至少还有任务名与读数
|
||||
.setContentTitle(headline)
|
||||
.setContentText(Dates.clock(TimerCore.displayNow()))
|
||||
|
||||
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 rv = RemoteViews(context.packageName, R.layout.notif_timer)
|
||||
rv.setTextViewText(R.id.tvNotifTask, headline)
|
||||
bindChronometer(rv, running, countUp)
|
||||
rv.setOnClickPendingIntent(
|
||||
R.id.notifPause,
|
||||
serviceAction(
|
||||
context,
|
||||
if (running) TimerService.ACTION_PAUSE else TimerService.ACTION_RESUME,
|
||||
11,
|
||||
),
|
||||
)
|
||||
rv.setImageViewResource(
|
||||
R.id.notifPause,
|
||||
if (running) R.drawable.ic_pause else R.drawable.ic_play,
|
||||
)
|
||||
rv.setOnClickPendingIntent(
|
||||
R.id.notifStop,
|
||||
serviceAction(context, TimerService.ACTION_STOP, 13),
|
||||
)
|
||||
b.setCustomContentView(rv)
|
||||
b.setCustomBigContentView(rv)
|
||||
|
||||
val toggleIcon = if (running) R.drawable.ic_pause else R.drawable.ic_play
|
||||
// 模板动作保留作兜底(自定义布局生效时系统不会重复绘制这两颗按钮)
|
||||
b.addAction(
|
||||
NotificationCompat.Action(
|
||||
toggleIcon, "",
|
||||
if (running) R.drawable.ic_pause else R.drawable.ic_play, "",
|
||||
serviceAction(
|
||||
context,
|
||||
if (running) TimerService.ACTION_PAUSE else TimerService.ACTION_RESUME,
|
||||
11,
|
||||
21,
|
||||
),
|
||||
)
|
||||
)
|
||||
b.addAction(
|
||||
NotificationCompat.Action(
|
||||
R.drawable.ic_stop, "",
|
||||
serviceAction(context, TimerService.ACTION_STOP, 13),
|
||||
serviceAction(context, TimerService.ACTION_STOP, 23),
|
||||
)
|
||||
)
|
||||
return b.build()
|
||||
}
|
||||
|
||||
/**
|
||||
* 把计时读数交给系统 Chronometer。
|
||||
*
|
||||
* 倒计时:base = 结束时刻,配合 countDown 显示"还剩多少";
|
||||
* 正计时:base 往前推已用时长,显示"已经过了多久";
|
||||
* 暂停时 started=false,Chronometer 停在按下暂停那一刻的值。
|
||||
*/
|
||||
private fun bindChronometer(rv: RemoteViews, running: Boolean, countUp: Boolean) {
|
||||
val now = System.currentTimeMillis()
|
||||
val base: Long
|
||||
val countDown: Boolean
|
||||
when {
|
||||
countUp -> {
|
||||
base = now - TimerCore.displayNow()
|
||||
countDown = false
|
||||
}
|
||||
running -> {
|
||||
base = TimerCore.endAtMs()
|
||||
countDown = true
|
||||
}
|
||||
else -> {
|
||||
base = now + TimerCore.remainingNow()
|
||||
countDown = true
|
||||
}
|
||||
}
|
||||
rv.setChronometer(R.id.notifChrono, base, null, running)
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
|
||||
rv.setChronometerCountDown(R.id.notifChrono, countDown)
|
||||
}
|
||||
}
|
||||
|
||||
fun updateOngoing(context: Context) {
|
||||
if (!NotificationManagerCompat.from(context).areNotificationsEnabled()) return
|
||||
runCatching {
|
||||
|
||||
Reference in New Issue
Block a user