导入 v0.1.0 / v0.2.0 源码:五模块首版、交互重构、重复任务、任务分布
- 源码镜像 src/main(81 个文件):首页双模式、任务、项目、统计、设置 - 补齐 README / LICENSE / CHANGELOG / CONTRIBUTING / .gitignore - 不含构建配置、签名材料、编译产物与备份数据
This commit is contained in:
167
src/main/java/com/focusbox/app/service/Notif.kt
Normal file
167
src/main/java/com/focusbox/app/service/Notif.kt
Normal file
@@ -0,0 +1,167 @@
|
||||
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) }
|
||||
}
|
||||
}
|
||||
449
src/main/java/com/focusbox/app/service/TimerCore.kt
Normal file
449
src/main/java/com/focusbox/app/service/TimerCore.kt
Normal file
File diff suppressed because it is too large
Load Diff
152
src/main/java/com/focusbox/app/service/TimerService.kt
Normal file
152
src/main/java/com/focusbox/app/service/TimerService.kt
Normal file
@@ -0,0 +1,152 @@
|
||||
package com.focusbox.app.service
|
||||
|
||||
import android.app.Service
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.os.Handler
|
||||
import android.os.IBinder
|
||||
import android.os.Looper
|
||||
import android.os.PowerManager
|
||||
import androidx.core.content.ContextCompat
|
||||
|
||||
/**
|
||||
* 计时前台服务。
|
||||
*
|
||||
* 之所以必须用前台服务而不是页面里的 Handler:息屏后 Activity 会被暂停、
|
||||
* 进程随时可能被回收,只有带常驻通知的前台服务能稳定维持计时与到时提醒。
|
||||
*
|
||||
* 真正的计时逻辑在 [TimerCore],这里只做三件事:撑住进程、每秒推进、刷新通知。
|
||||
*/
|
||||
class TimerService : Service() {
|
||||
|
||||
companion object {
|
||||
const val ACTION_SYNC = "com.focusbox.app.SYNC"
|
||||
const val ACTION_PAUSE = "com.focusbox.app.PAUSE"
|
||||
const val ACTION_RESUME = "com.focusbox.app.RESUME"
|
||||
const val ACTION_STOP = "com.focusbox.app.STOP"
|
||||
|
||||
fun ensureRunning(context: Context) {
|
||||
val intent = Intent(context, TimerService::class.java).setAction(ACTION_SYNC)
|
||||
runCatching { ContextCompat.startForegroundService(context, intent) }
|
||||
}
|
||||
|
||||
fun refresh(context: Context) {
|
||||
Notif.updateOngoing(context)
|
||||
}
|
||||
|
||||
fun stop(context: Context) {
|
||||
runCatching { context.stopService(Intent(context, TimerService::class.java)) }
|
||||
Notif.cancelOngoing(context)
|
||||
}
|
||||
}
|
||||
|
||||
private val handler = Handler(Looper.getMainLooper())
|
||||
private var wakeLock: PowerManager.WakeLock? = null
|
||||
|
||||
private val ticker = object : Runnable {
|
||||
override fun run() {
|
||||
// 只推进状态,不再每秒重发通知:通知里的时间是系统的 Chronometer 在走,
|
||||
// 每秒 notify 反而会让锁屏上的计时被系统限流后"冻住"。
|
||||
TimerCore.tick()
|
||||
handler.postDelayed(this, 1000L)
|
||||
}
|
||||
}
|
||||
|
||||
override fun onCreate() {
|
||||
super.onCreate()
|
||||
TimerCore.addListener(onStateChanged)
|
||||
}
|
||||
|
||||
private val onStateChanged: () -> Unit = {
|
||||
Notif.updateOngoing(this)
|
||||
syncWakeLock()
|
||||
}
|
||||
|
||||
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
|
||||
when (intent?.action) {
|
||||
ACTION_PAUSE -> {
|
||||
TimerCore.pause()
|
||||
stopTicker()
|
||||
}
|
||||
ACTION_RESUME -> {
|
||||
TimerCore.resume()
|
||||
syncTicker()
|
||||
}
|
||||
ACTION_STOP -> {
|
||||
TimerCore.stopEarly()
|
||||
stopTicker()
|
||||
stopSelf()
|
||||
return START_NOT_STICKY
|
||||
}
|
||||
else -> Unit
|
||||
}
|
||||
|
||||
// 必须在启动后立即进入前台,否则 5 秒内会被系统判定为 ANR 级违规
|
||||
startForegroundCompat()
|
||||
syncTicker()
|
||||
syncWakeLock()
|
||||
Notif.updateOngoing(this)
|
||||
return START_NOT_STICKY
|
||||
}
|
||||
|
||||
/**
|
||||
* 只有倒计时需要每秒推进(判断是否到点)。
|
||||
* 正计时靠时间戳现算、通知靠系统 Chronometer 走时,
|
||||
* 既不用轮询也不用占着 WakeLock,长时间正计时不会白耗电。
|
||||
*/
|
||||
private fun syncTicker() {
|
||||
if (TimerCore.running && !TimerCore.countUp) startTicker() else stopTicker()
|
||||
}
|
||||
|
||||
private fun startForegroundCompat() {
|
||||
val notification = Notif.buildOngoing(this)
|
||||
runCatching {
|
||||
// 两参版本会读清单里声明的前台服务类型(dataSync)。
|
||||
// 不用三参 + SPECIAL_USE:该类型 Android 14 才引入,
|
||||
// 在 P30 的 Android 12 上传进去会直接抛 IllegalArgumentException。
|
||||
startForeground(Notif.ID_ONGOING, notification)
|
||||
}.onFailure {
|
||||
// 前台服务被系统拒绝(省电策略/权限)时退化为普通服务,
|
||||
// 计时本身仍在 TimerCore 里走,只是通知可能不显示。
|
||||
stopSelf()
|
||||
}
|
||||
}
|
||||
|
||||
private fun startTicker() {
|
||||
handler.removeCallbacks(ticker)
|
||||
handler.postDelayed(ticker, 1000L)
|
||||
}
|
||||
|
||||
private fun stopTicker() {
|
||||
handler.removeCallbacks(ticker)
|
||||
}
|
||||
|
||||
private fun syncWakeLock() {
|
||||
if (TimerCore.running && !TimerCore.countUp) {
|
||||
if (wakeLock?.isHeld != true) {
|
||||
val pm = getSystemService(Context.POWER_SERVICE) as PowerManager
|
||||
wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "FocusBox:timer").apply {
|
||||
setReferenceCounted(false)
|
||||
// 超时兜底:正常路径会主动释放,这里防止异常退出后一直亮着 CPU
|
||||
acquire(4 * 60 * 60 * 1000L)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
releaseWakeLock()
|
||||
}
|
||||
}
|
||||
|
||||
private fun releaseWakeLock() {
|
||||
runCatching { if (wakeLock?.isHeld == true) wakeLock?.release() }
|
||||
wakeLock = null
|
||||
}
|
||||
|
||||
override fun onBind(intent: Intent?): IBinder? = null
|
||||
|
||||
override fun onDestroy() {
|
||||
stopTicker()
|
||||
releaseWakeLock()
|
||||
TimerCore.removeListener(onStateChanged)
|
||||
super.onDestroy()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user