Pith - timber
package com.vgmlr.timber

import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.app.Service
import android.content.Context
import android.content.Intent
import android.content.pm.ServiceInfo
import android.graphics.drawable.Icon
import android.os.Build
import android.os.IBinder

class TimberService : Service() {

    override fun onBind(intent: Intent?): IBinder? = null

    override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
        if (intent?.action == ACT_STOP) {
            TimberRecorder.stop(this)
            return START_NOT_STICKY
        }
        runCatching {
            startForeground(NOTE_ID, note(this), ServiceInfo.FOREGROUND_SERVICE_TYPE_MICROPHONE)
        }.onFailure {
            stopSelf()
            return START_NOT_STICKY
        }
        if (!TimberRecorder.active && !TimberRecorder.start(this)) {
            stopForeground(STOP_FOREGROUND_REMOVE)
            stopSelf()
        } else refresh(this)
        return START_NOT_STICKY
    }

    override fun onTaskRemoved(rootIntent: Intent?) {
        TimberRecorder.stop(this)
        stopSelf()
        super.onTaskRemoved(rootIntent)
    }

    override fun onDestroy() {
        if (TimberRecorder.active) TimberRecorder.stop(this)
        super.onDestroy()
    }

    companion object {
        private const val NOTE_ID = 7
        private const val ACT_STOP = "com.vgmlr.timber.STOP"
        private const val CH_REC = "timber_rec"
        private const val CH_QUIET = "timber_quiet"

        fun start(ctx: Context) {
            runCatching { ctx.startForegroundService(Intent(ctx, TimberService::class.java)) }
        }

        fun refresh(ctx: Context) {
            if (!TimberRecorder.active) return
            runCatching {
                ctx.getSystemService(NotificationManager::class.java).notify(NOTE_ID, note(ctx))
            }
        }

        private fun note(ctx: Context): Notification {
            val nm = ctx.getSystemService(NotificationManager::class.java)
            val loud = TimberPrefs(ctx).notify
            val ch = if (loud) CH_REC else CH_QUIET
            if (nm.getNotificationChannel(ch) == null) {
                nm.createNotificationChannel(
                    NotificationChannel(
                        ch,
                        if (loud) "Recording" else "Timer",
                        if (loud) NotificationManager.IMPORTANCE_LOW else NotificationManager.IMPORTANCE_MIN
                    ).apply {
                        setShowBadge(false)
                        setSound(null, null)
                    }
                )
            }
            val open = PendingIntent.getActivity(
                ctx, 0,
                Intent(ctx, TimberMainActivity::class.java),
                PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT
            )
            val b = Notification.Builder(ctx, ch)
                .setSmallIcon(R.drawable.ic_timber_mic)
                .setOngoing(true)
                .setOnlyAlertOnce(true)
                .setContentIntent(open)
            if (Build.VERSION.SDK_INT >= 31) {
                b.setForegroundServiceBehavior(Notification.FOREGROUND_SERVICE_IMMEDIATE)
            }
            if (TimberRecorder.paused) {
                b.setShowWhen(false)
                    .setUsesChronometer(false)
                    .setContentText(clock(TimberRecorder.elapsed))
            } else {
                b.setShowWhen(true)
                    .setUsesChronometer(true)
                    .setWhen(System.currentTimeMillis() - TimberRecorder.elapsed)
            }
            if (loud) {
                val stop = PendingIntent.getService(
                    ctx, 1,
                    Intent(ctx, TimberService::class.java).setAction(ACT_STOP),
                    PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT
                )
                b.setContentTitle(if (TimberRecorder.paused) "Paused" else "Recording")
                    .addAction(
                        Notification.Action.Builder(
                            Icon.createWithResource(ctx, R.drawable.ic_timber_mic),
                            "Stop",
                            stop
                        ).build()
                    )
            }
            return b.build()
        }
    }
}