Delete NotificationAdapter and NotificationAction dead code (#125)

Completes the notifier-pattern cleanup. NotificationAdapter and
NotificationAction had no production call sites — they were exercised
only by their own tests and NotificationIconTest. Their private
buildSessionNotification / buildErrorNotification / buildAlertNotification
helpers were the last classes in :notifications that built-and-returned
a Notification without the startForeground justification.

Every :notifications class now either posts-via-postX (all notifiers) or
is the single documented exception (ForegroundNotifier). Rule satisfied.

NotificationIconTest is rewritten to exercise live notifiers
(ForegroundNotifier, FgsLimitNotifier, SecurityCheckNotifier,
AuthRequestNotifier) instead of the dead adapter. Icon coverage is now
routed through real production code paths.

TunnelForegroundService.updateNotification() still calls
NotificationManager.notify() with the FGS id — Android treats that as an
in-place update of the ongoing foreground notification, so this is an
intentional extension of the startForeground contract. A comment on the
method documents the rationale.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Jason Monk 2026-04-14 19:36:07 -04:00
parent 8395b80716
commit 0bb092d41f
6 changed files with 29 additions and 286 deletions

View file

@ -1,28 +0,0 @@
package com.rousecontext.notifications
/**
* Actions the notification state machine emits.
* The [NotificationAdapter] translates these into Android NotificationManager calls.
*/
sealed interface NotificationAction {
/** Show or update the ongoing foreground service notification. */
data class ShowForeground(val message: String) : NotificationAction
/** Post a session summary after disconnect. */
data class PostSummary(val toolCallCount: Int) : NotificationAction
/** Post a warning (e.g. disconnect with no tool calls). */
data class PostWarning(val message: String) : NotificationAction
/** Post an error notification. */
data class PostError(val message: String, val streamId: Int? = null) : NotificationAction
/** Post a per-tool-call usage notification. */
data class PostToolUsage(val toolName: String, val provider: String) : NotificationAction
/** Post an informational notification. */
data class PostInfo(val message: String) : NotificationAction
/** Post a high-priority security alert. */
data class PostAlert(val message: String) : NotificationAction
}

View file

@ -1,113 +0,0 @@
package com.rousecontext.notifications
import android.app.NotificationManager
import android.content.Context
import androidx.core.app.NotificationCompat
import com.rousecontext.api.R as ApiR
/**
* Translates [NotificationAction]s into Android [NotificationManager] calls.
*/
class NotificationAdapter(private val context: Context) {
private val manager = context.getSystemService(NotificationManager::class.java)
private var nextId = FIRST_DYNAMIC_ID
/**
* Execute a list of notification actions.
*/
fun execute(actions: List<NotificationAction>) {
actions.forEach { execute(it) }
}
/**
* Execute a single notification action.
*/
fun execute(action: NotificationAction) {
when (action) {
is NotificationAction.ShowForeground -> {
val notification = ForegroundNotifier.build(context, action.message)
manager.notify(FOREGROUND_ID, notification)
}
is NotificationAction.PostSummary -> {
val suffix =
if (action.toolCallCount == 1) "" else "s"
val body =
"${action.toolCallCount} tool call$suffix processed"
val notification = buildSessionNotification(
"Session Complete",
body
)
manager.notify(nextId(), notification)
}
is NotificationAction.PostWarning -> {
val notification = buildSessionNotification("Warning", action.message)
manager.notify(nextId(), notification)
}
is NotificationAction.PostError -> {
val text = if (action.streamId != null) {
"Stream ${action.streamId}: ${action.message}"
} else {
action.message
}
val notification = buildErrorNotification(text)
manager.notify(nextId(), notification)
}
is NotificationAction.PostToolUsage -> {
val notification = buildSessionNotification(
"Tool Call",
"${action.toolName} (${action.provider})"
)
manager.notify(nextId(), notification)
}
is NotificationAction.PostInfo -> {
val notification = buildSessionNotification("Info", action.message)
manager.notify(nextId(), notification)
}
is NotificationAction.PostAlert -> {
val notification = buildAlertNotification(action.message)
manager.notify(nextId(), notification)
}
}
}
private fun buildSessionNotification(title: String, text: String) =
NotificationCompat.Builder(context, NotificationChannels.SESSION_CHANNEL_ID)
.setContentTitle(title)
.setContentText(text)
.setSmallIcon(ApiR.drawable.ic_stat_rouse)
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setAutoCancel(true)
.build()
private fun buildErrorNotification(text: String) =
NotificationCompat.Builder(context, NotificationChannels.ERROR_CHANNEL_ID)
.setContentTitle("Error")
.setContentText(text)
.setSmallIcon(ApiR.drawable.ic_stat_rouse)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setAutoCancel(true)
.build()
private fun buildAlertNotification(text: String) =
NotificationCompat.Builder(context, NotificationChannels.ALERT_CHANNEL_ID)
.setContentTitle("Security Alert")
.setContentText(text)
.setSmallIcon(ApiR.drawable.ic_stat_rouse)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setAutoCancel(true)
.build()
private fun nextId(): Int = nextId++
companion object {
const val FOREGROUND_ID = 1
private const val FIRST_DYNAMIC_ID = 100
}
}

View file

@ -1,79 +0,0 @@
package com.rousecontext.notifications
import android.app.NotificationManager
import android.content.Context
import androidx.test.core.app.ApplicationProvider
import org.junit.Assert.assertEquals
import org.junit.Assert.assertNotNull
import org.junit.Assert.assertTrue
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
import org.robolectric.Shadows
@RunWith(RobolectricTestRunner::class)
class NotificationAdapterTest {
private lateinit var context: Context
private lateinit var manager: NotificationManager
private lateinit var adapter: NotificationAdapter
@Before
fun setUp() {
context = ApplicationProvider.getApplicationContext()
manager = context.getSystemService(NotificationManager::class.java)
NotificationChannels.createAll(context)
adapter = NotificationAdapter(context)
}
@Test
fun `ShowForeground posts notification with foreground ID`() {
adapter.execute(NotificationAction.ShowForeground("Connected"))
val shadowManager = Shadows.shadowOf(manager)
val notification = shadowManager.getNotification(NotificationAdapter.FOREGROUND_ID)
assertNotNull(notification)
}
@Test
fun `PostSummary posts notification with session complete title`() {
adapter.execute(NotificationAction.PostSummary(toolCallCount = 3))
val shadowManager = Shadows.shadowOf(manager)
val notifications = shadowManager.allNotifications
// foreground might not be posted, but at least one dynamic notification should exist
assertTrue(notifications.isNotEmpty())
}
@Test
fun `PostError posts notification`() {
adapter.execute(NotificationAction.PostError("Connection failed", streamId = null))
val shadowManager = Shadows.shadowOf(manager)
assertTrue(shadowManager.allNotifications.isNotEmpty())
}
@Test
fun `PostAlert posts notification`() {
adapter.execute(NotificationAction.PostAlert("Suspicious activity"))
val shadowManager = Shadows.shadowOf(manager)
assertTrue(shadowManager.allNotifications.isNotEmpty())
}
@Test
fun `multiple actions post unique notification IDs`() {
adapter.execute(
listOf(
NotificationAction.PostInfo("Info 1"),
NotificationAction.PostInfo("Info 2"),
NotificationAction.PostInfo("Info 3")
)
)
val shadowManager = Shadows.shadowOf(manager)
// Each should have a unique ID, so all 3 should be present
assertEquals(3, shadowManager.allNotifications.size)
}
}

View file

@ -12,104 +12,64 @@ import org.robolectric.RobolectricTestRunner
import org.robolectric.Shadows
/**
* Verifies that every notification type uses [ApiR.drawable.ic_stat_rouse]
* Verifies that every production notifier uses [ApiR.drawable.ic_stat_rouse]
* as the small icon, not a system drawable or placeholder.
*
* Covers each [com.rousecontext.notifications] notifier in one place so icon
* regressions surface even if per-notifier tests drift.
*/
@RunWith(RobolectricTestRunner::class)
class NotificationIconTest {
private lateinit var context: Context
private lateinit var manager: NotificationManager
private lateinit var adapter: NotificationAdapter
@Before
fun setUp() {
context = ApplicationProvider.getApplicationContext()
manager = context.getSystemService(NotificationManager::class.java)
NotificationChannels.createAll(context)
adapter = NotificationAdapter(context)
}
@Test
fun `foreground notification uses ic_stat_rouse`() {
fun `ForegroundNotifier build uses ic_stat_rouse`() {
val notification = ForegroundNotifier.build(context, "Connected")
assertEquals(ApiR.drawable.ic_stat_rouse, notification.smallIcon.resId)
}
@Test
fun `ShowForeground action posts notification with ic_stat_rouse`() {
adapter.execute(NotificationAction.ShowForeground("Active"))
fun `FgsLimitNotifier postLimitReachedNotification uses ic_stat_rouse`() {
FgsLimitNotifier.postLimitReachedNotification(context)
val active = manager.activeNotifications.single()
assertEquals(ApiR.drawable.ic_stat_rouse, active.notification.smallIcon.resId)
}
@Test
fun `SecurityCheckNotifier postAlert uses ic_stat_rouse`() {
val notifier = SecurityCheckNotifier(context)
notifier.postAlert(SecurityCheckNotifier.SecurityCheck.SELF_CERT, "bad cert")
val shadow = Shadows.shadowOf(manager)
val notification = shadow.getNotification(NotificationAdapter.FOREGROUND_ID)
val notification = shadow.getNotification(
SecurityCheckNotifier.NOTIFICATION_ID_SELF_CERT_ALERT
)
assertEquals(ApiR.drawable.ic_stat_rouse, notification.smallIcon.resId)
}
@Test
fun `PostSummary notification uses ic_stat_rouse`() {
adapter.execute(NotificationAction.PostSummary(toolCallCount = 5))
fun `SecurityCheckNotifier postInfo uses ic_stat_rouse`() {
val notifier = SecurityCheckNotifier(context)
notifier.postInfo(SecurityCheckNotifier.SecurityCheck.CT_LOG, "ok")
val shadow = Shadows.shadowOf(manager)
val notification = shadow.allNotifications.first()
val notification = shadow.getNotification(
SecurityCheckNotifier.NOTIFICATION_ID_CT_LOG_INFO
)
assertEquals(ApiR.drawable.ic_stat_rouse, notification.smallIcon.resId)
}
@Test
fun `PostWarning notification uses ic_stat_rouse`() {
adapter.execute(NotificationAction.PostWarning("Low battery"))
val shadow = Shadows.shadowOf(manager)
val notification = shadow.allNotifications.first()
assertEquals(ApiR.drawable.ic_stat_rouse, notification.smallIcon.resId)
}
@Test
fun `PostError notification uses ic_stat_rouse`() {
adapter.execute(NotificationAction.PostError("Connection lost", streamId = null))
val shadow = Shadows.shadowOf(manager)
val notification = shadow.allNotifications.first()
assertEquals(ApiR.drawable.ic_stat_rouse, notification.smallIcon.resId)
}
@Test
fun `PostError with streamId notification uses ic_stat_rouse`() {
adapter.execute(NotificationAction.PostError("TLS failed", streamId = 42))
val shadow = Shadows.shadowOf(manager)
val notification = shadow.allNotifications.first()
assertEquals(ApiR.drawable.ic_stat_rouse, notification.smallIcon.resId)
}
@Test
fun `PostToolUsage notification uses ic_stat_rouse`() {
adapter.execute(NotificationAction.PostToolUsage("get_steps", "health"))
val shadow = Shadows.shadowOf(manager)
val notification = shadow.allNotifications.first()
assertEquals(ApiR.drawable.ic_stat_rouse, notification.smallIcon.resId)
}
@Test
fun `PostInfo notification uses ic_stat_rouse`() {
adapter.execute(NotificationAction.PostInfo("Session started"))
val shadow = Shadows.shadowOf(manager)
val notification = shadow.allNotifications.first()
assertEquals(ApiR.drawable.ic_stat_rouse, notification.smallIcon.resId)
}
@Test
fun `PostAlert notification uses ic_stat_rouse`() {
adapter.execute(NotificationAction.PostAlert("Suspicious activity detected"))
val shadow = Shadows.shadowOf(manager)
val notification = shadow.allNotifications.first()
assertEquals(ApiR.drawable.ic_stat_rouse, notification.smallIcon.resId)
}
@Test
fun `AuthRequestNotifier notification uses ic_stat_rouse`() {
fun `AuthRequestNotifier post uses ic_stat_rouse`() {
val notifier = AuthRequestNotifier(
context = context,
receiverClass = AuthRequestNotifierTest.TestReceiver::class.java,

View file

@ -9,8 +9,8 @@ import androidx.core.app.NotificationCompat
import androidx.test.core.app.ApplicationProvider
import com.github.takahirom.roborazzi.captureRoboImage
import com.rousecontext.api.R as ApiR
import com.rousecontext.notifications.NotificationChannels
import com.rousecontext.notifications.ForegroundNotifier
import com.rousecontext.notifications.NotificationChannels
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith

View file

@ -273,6 +273,9 @@ class TunnelForegroundService : LifecycleService() {
TunnelState.ACTIVE -> "Active session"
TunnelState.DISCONNECTING -> "Disconnecting..."
}
// Update the foreground notification in place via the same id passed to
// startForeground(). Android treats a NotificationManager.notify() with
// the FGS id as an update to the ongoing notification.
val notification = ForegroundNotifier.build(this, message)
val manager = getSystemService(android.app.NotificationManager::class.java)
manager.notify(NOTIFICATION_ID, notification)