|
| 1 | +package expo.modules.t3agentnotifications |
| 2 | + |
| 3 | +import android.content.Context |
| 4 | +import android.graphics.Typeface |
| 5 | +import android.text.SpannableStringBuilder |
| 6 | +import android.text.Spanned |
| 7 | +import android.text.style.ForegroundColorSpan |
| 8 | +import android.text.style.StyleSpan |
| 9 | +import androidx.core.app.NotificationCompat |
| 10 | +import androidx.core.content.ContextCompat |
| 11 | + |
| 12 | +internal data class ActivityRow(val status: String, val title: String, val project: String) |
| 13 | + |
| 14 | +/** |
| 15 | + * One entry per relay phase. The status label matches the relay's row wording and the |
| 16 | + * tint matches the web sidebar pills and the iOS Live Activity so a thread reads the |
| 17 | + * same on every surface. The icon is always the T3 mark; the chip verb carries the state. |
| 18 | + */ |
| 19 | +internal enum class ActivityPhase( |
| 20 | + val status: String, |
| 21 | + val heading: String, |
| 22 | + val chip: String, |
| 23 | + val action: String, |
| 24 | + val color: Int |
| 25 | +) { |
| 26 | + STARTING("Connecting", "Starting", "Working", "Open", R.color.agent_activity_working), |
| 27 | + RUNNING("Working", "Working", "Working", "Open", R.color.agent_activity_working), |
| 28 | + APPROVAL( |
| 29 | + "Approval", |
| 30 | + "Approval needed", |
| 31 | + "Approve", |
| 32 | + "Approve", |
| 33 | + R.color.agent_activity_attention |
| 34 | + ), |
| 35 | + INPUT( |
| 36 | + "Input", |
| 37 | + "Question for you", |
| 38 | + "Answer", |
| 39 | + "Answer", |
| 40 | + R.color.agent_activity_input |
| 41 | + ), |
| 42 | + STALE( |
| 43 | + "Waiting", |
| 44 | + "Waiting for an update", |
| 45 | + "Waiting", |
| 46 | + "Open", |
| 47 | + R.color.agent_activity_waiting |
| 48 | + ), |
| 49 | + COMPLETED( |
| 50 | + "Done", |
| 51 | + "Finished", |
| 52 | + "Done", |
| 53 | + "Open", |
| 54 | + R.color.agent_activity_done |
| 55 | + ), |
| 56 | + FAILED( |
| 57 | + "Failed", |
| 58 | + "Failed", |
| 59 | + "Failed", |
| 60 | + "Open", |
| 61 | + R.color.agent_activity_failed |
| 62 | + ); |
| 63 | + |
| 64 | + val needsUser get() = this == APPROVAL || this == INPUT |
| 65 | + val finished get() = this == COMPLETED || this == FAILED |
| 66 | + |
| 67 | + companion object { |
| 68 | + fun forStatus(status: String) = entries.firstOrNull { it.status == status } |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +/** The relay orders rows and their deep link together; never reorder them in the client. */ |
| 73 | +internal fun activityRows(data: Map<String, String>) = (0..4).mapNotNull { |
| 74 | + val parts = data["activity_line_$it"]?.split('\t', limit = 3) ?: return@mapNotNull null |
| 75 | + if (parts.size != 3 || parts[1].isBlank()) { |
| 76 | + null |
| 77 | + } else { |
| 78 | + ActivityRow(parts[0].take(40), parts[1].take(120), parts[2].take(120)) |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +internal fun activityPhase(data: Map<String, String>, rows: List<ActivityRow>): ActivityPhase? = |
| 83 | + when (data["activity_phase"]?.takeIf { it.isNotBlank() }) { |
| 84 | + "starting" -> ActivityPhase.STARTING |
| 85 | + "running" -> ActivityPhase.RUNNING |
| 86 | + "waiting_for_approval" -> ActivityPhase.APPROVAL |
| 87 | + "waiting_for_input" -> ActivityPhase.INPUT |
| 88 | + "stale" -> ActivityPhase.STALE |
| 89 | + "completed" -> ActivityPhase.COMPLETED |
| 90 | + "failed" -> ActivityPhase.FAILED |
| 91 | + else -> rows.firstOrNull()?.let { ActivityPhase.forStatus(it.status) } |
| 92 | + } |
| 93 | + |
| 94 | +/** |
| 95 | + * Header carries the state, the title says what needs you (or which thread, when |
| 96 | + * there is only one), and the body lists every thread with its status in front. |
| 97 | + * System UI renders all of it, so the same builder serves the shade, the lock |
| 98 | + * screen and the status bar chip. |
| 99 | + */ |
| 100 | +internal class ActivityPresentation(data: Map<String, String>, private val active: Boolean) { |
| 101 | + private val rows = activityRows(data) |
| 102 | + private val hero = rows.firstOrNull() |
| 103 | + val phase = activityPhase(data, rows) |
| 104 | + private val activeCount = data["activity_active_count"]?.toIntOrNull()?.coerceAtLeast(0) |
| 105 | + ?: rows.count { ActivityPhase.forStatus(it.status)?.finished != true } |
| 106 | + private val attentionCount = data["activity_attention_count"]?.toIntOrNull()?.coerceAtLeast(0) |
| 107 | + ?: rows.count { ActivityPhase.forStatus(it.status)?.needsUser == true } |
| 108 | + private val failedCount = rows.count { it.status == ActivityPhase.FAILED.status } |
| 109 | + val threadCount = |
| 110 | + activeCount + rows.count { ActivityPhase.forStatus(it.status)?.finished == true } |
| 111 | + private val singleProject = rows.map { it.project }.distinct().size == 1 |
| 112 | + private val legacyBody = (0..4).mapNotNull { data["activity_line_$it"]?.take(300) } |
| 113 | + .takeIf { it.isNotEmpty() }?.joinToString("\n") |
| 114 | + ?: data["activity_body"].orEmpty().take(240) |
| 115 | + |
| 116 | + val summary = when { |
| 117 | + hero == null -> data["activity_title"]?.takeIf { it.isNotBlank() }?.take(120) |
| 118 | + ?: "Agent activity" |
| 119 | + rows.size == 1 -> hero.title |
| 120 | + attentionCount == 1 -> "1 needs you" |
| 121 | + attentionCount > 1 -> "$attentionCount need you" |
| 122 | + activeCount > 0 && failedCount > 0 -> "$failedCount failed" |
| 123 | + activeCount > 0 -> "$activeCount working" |
| 124 | + failedCount > 0 -> "Finished, $failedCount failed" |
| 125 | + else -> "All finished" |
| 126 | + } |
| 127 | + |
| 128 | + val chip = when { |
| 129 | + !active -> null |
| 130 | + phase == null -> data["activity_chip"]?.takeIf { it.isNotBlank() }?.take(7) ?: "Active" |
| 131 | + phase == ActivityPhase.RUNNING && activeCount > 1 -> |
| 132 | + "${if (activeCount > 9) "9+" else activeCount} live" |
| 133 | + else -> phase.chip |
| 134 | + } |
| 135 | + |
| 136 | + val action = if (active) phase?.action ?: "Open" else null |
| 137 | + |
| 138 | + fun applyTo(builder: NotificationCompat.Builder, context: Context) { |
| 139 | + val tint = phase?.let { ContextCompat.getColor(context, it.color) } |
| 140 | + builder.setSmallIcon(R.drawable.agent_activity_mark) |
| 141 | + if (tint != null) builder.setColor(tint) |
| 142 | + // Tint the summary only when it names an outcome or a request; a plain |
| 143 | + // "3 working" stays neutral so the accent keeps meaning something. |
| 144 | + val tintedSummary = tint != null && rows.size > 1 && phase != ActivityPhase.RUNNING && |
| 145 | + phase != ActivityPhase.STARTING |
| 146 | + builder.setContentTitle(if (tintedSummary) tinted(summary, tint!!) else summary) |
| 147 | + if (rows.size > 1) { |
| 148 | + builder.setSubText( |
| 149 | + listOfNotNull( |
| 150 | + hero!!.project.takeIf { singleProject && it.isNotBlank() }, |
| 151 | + if (activeCount > 0) "$activeCount active" else "$threadCount threads" |
| 152 | + ).joinToString(" · ") |
| 153 | + ) |
| 154 | + } |
| 155 | + val body = body(context) |
| 156 | + // The collapsed card gets the priority row; the expanded card gets them all. |
| 157 | + val lineBreak = body.indexOf('\n') |
| 158 | + val firstLine = if (lineBreak >= 0) body.subSequence(0, lineBreak) else body |
| 159 | + builder.setContentText(firstLine).setStyle(NotificationCompat.BigTextStyle().bigText(body)) |
| 160 | + // Thread update timestamps can change while an approval remains pending. |
| 161 | + // Leave the timer hidden until the payload has a stable phase-entry timestamp. |
| 162 | + builder.setShowWhen(false).setUsesChronometer(false) |
| 163 | + } |
| 164 | + |
| 165 | + private fun body(context: Context): CharSequence = when { |
| 166 | + hero == null -> legacyBody |
| 167 | + // The title already names the thread; the body only needs its status and project. |
| 168 | + rows.size == 1 -> statusLine(context, hero.copy(title = hero.project), "") |
| 169 | + else -> SpannableStringBuilder().apply { |
| 170 | + rows.forEachIndexed { index, row -> |
| 171 | + if (index > 0) append("\n") |
| 172 | + append(statusLine(context, row, row.project.takeUnless { singleProject }.orEmpty())) |
| 173 | + } |
| 174 | + } |
| 175 | + } |
| 176 | + |
| 177 | + private fun statusLine(context: Context, row: ActivityRow, trailing: String): CharSequence = |
| 178 | + SpannableStringBuilder().apply { |
| 179 | + val status = ActivityPhase.forStatus(row.status) |
| 180 | + val color = ContextCompat.getColor(context, status?.color ?: R.color.agent_activity_waiting) |
| 181 | + append(tinted(row.status, color, bold = true)) |
| 182 | + append(" ").append(row.title) |
| 183 | + // Promoted cards drop text color, so the separator has to do the work of the dimming. |
| 184 | + if (trailing.isNotBlank()) { |
| 185 | + val start = length |
| 186 | + append(" · ").append(trailing) |
| 187 | + setSpan( |
| 188 | + ForegroundColorSpan(ContextCompat.getColor(context, R.color.agent_activity_waiting)), |
| 189 | + start, |
| 190 | + length, |
| 191 | + Spanned.SPAN_EXCLUSIVE_EXCLUSIVE |
| 192 | + ) |
| 193 | + } |
| 194 | + } |
| 195 | + |
| 196 | + private fun tinted(text: String, color: Int, bold: Boolean = false) = |
| 197 | + SpannableStringBuilder(text).apply { |
| 198 | + setSpan(ForegroundColorSpan(color), 0, length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE) |
| 199 | + if (bold) setSpan(StyleSpan(Typeface.BOLD), 0, length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE) |
| 200 | + } |
| 201 | +} |
0 commit comments