Android: notifications that read like the app's voice bar

The voice service's notification said "In voice on <server>" with Mute
and Disconnect, and the screen-sharing one only "Sharing your screen".
The voice notification now shows what the app's voice card shows: the
channel as its title, the server and how many are there ("Home · 3 in
voice", "just you"), the time in voice as a chronometer, and Mute,
Deafen and Leave, which act on every voice session as the old Mute did.
Several servers at once read "In voice on 2 servers" with their names.
Tapping it opens the voice channel screen (the activity forwards the
action to Rust, which queues it until the window runs). While our
stream is live the screen-sharing notification says where it goes and
how many watch ("Live in Chill Zone", "Home · 2 watching"). Both use
the app's accent colour.

The window takes such platform requests through a small inbox in
voelin-ui (`voelin_ui::request`), handled on the window's thread and
outside the app's borrow, since navigating calls back into Rust.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
selikhovandClaude Opus 5.5 committed 2026-10-03 17:17:38 +03:00
1 parent 5f51eb15a2
commit d4b6baaa42
15 files changed
+529 -98

No files matched your search

@@ -30,12 +30,23 @@ object Bridge {
}
/**
* Run the voice service with this notification text (`muted`: the action
* offers Unmute); null stops it.
* Run the voice service with this notification (`muted`, `deafened`: the
* actions offer Unmute, Undeafen; `sinceMs`: in voice since, Unix ms, 0
* while connecting); a null title stops it.
*/
@JvmStatic
fun setVoiceNotification(text: String?, muted: Boolean) {
if (text == null) VoiceService.stop(app) else VoiceService.start(app, text, muted)
fun setVoiceNotification(title: String?, text: String?, muted: Boolean, deafened: Boolean, sinceMs: Long) {
if (title == null) {
VoiceService.stop(app)
} else {
VoiceService.start(app, VoiceService.Notice(title, text ?: "", muted, deafened, sinceMs))
}
}
/** What the screen-sharing notification says while our stream is live; null: the default. */
@JvmStatic
fun setScreenNotification(title: String?, text: String?) {
ScreenCaptureService.setNotice(title, text)
}
/** Ask the user to share the screen; the answer goes to Native.onScreenCaptureResult. */
@@ -9,7 +9,7 @@ import java.lang.ref.WeakReference
/**
* The window: a NativeActivity running `android_main` of libvoelin_android.so
* (the Slint UI). Kotlin only handles what needs an Activity: permission
* prompts and the screen-capture consent dialog.
* prompts, the screen-capture consent dialog and a tapped notification.
*/
class MainActivity : NativeActivity() {
private var captureFps = 30
@@ -18,6 +18,14 @@ class MainActivity : NativeActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
current = WeakReference(this)
super.onCreate(savedInstanceState)
// Not again when the activity is recreated with the same intent.
if (savedInstanceState == null) handle(intent)
}
override fun onNewIntent(intent: Intent) {
super.onNewIntent(intent)
setIntent(intent)
handle(intent)
}
override fun onDestroy() {
@@ -34,6 +42,13 @@ class MainActivity : NativeActivity() {
moveTaskToBack(true)
}
/** A tapped voice notification. */
private fun handle(intent: Intent?) {
when (intent?.action) {
VoiceService.ACTION_OPEN_VOICE -> Native.onOpenVoice()
}
}
/** Show the system's "start recording or casting?" dialog. */
fun requestScreenCapture(fps: Int, maxSize: Int) {
captureFps = fps
@@ -49,4 +49,12 @@ object Native {
/** "Mute" / "Unmute" in the voice notification. */
@JvmStatic
external fun onToggleMute()
/** "Deafen" / "Undeafen" in the voice notification. */
@JvmStatic
external fun onToggleDeafen()
/** The voice notification was tapped: show the voice channel. */
@JvmStatic
external fun onOpenVoice()
}
@@ -12,6 +12,9 @@ object Notifications {
const val CHANNEL_VOICE = "voice"
const val CHANNEL_SCREEN = "screen"
/** The app's accent (Theme.accent of the UI). */
private const val ACCENT = 0xFF2D6BFF.toInt()
fun createChannels(context: Context) {
val manager = context.getSystemService(NotificationManager::class.java)
manager.createNotificationChannel(
@@ -30,31 +33,45 @@ object Notifications {
)
}
/** Tapping a notification brings the app back. */
private fun openApp(context: Context): PendingIntent = PendingIntent.getActivity(
/** Tapping a notification brings the app back, with `action` for MainActivity. */
fun openApp(context: Context, action: String? = null): PendingIntent = PendingIntent.getActivity(
context,
0,
Intent(context, MainActivity::class.java).addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT),
action.hashCode(),
Intent(context, MainActivity::class.java)
.setAction(action)
.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT),
PendingIntent.FLAG_IMMUTABLE,
)
/** An ongoing notification with `actions` (title, intent). */
/**
* An ongoing notification: `title` and `text`, a chronometer from
* `sinceMs` (Unix ms; 0: none), `actions` (title, intent).
*/
fun ongoing(
context: Context,
channel: String,
title: String,
text: String,
open: PendingIntent,
sinceMs: Long,
vararg actions: Pair<String, PendingIntent>,
): Notification {
val builder = Notification.Builder(context, channel)
.setSmallIcon(R.drawable.ic_notification)
.setContentTitle(context.getString(R.string.app_name))
.setColor(ACCENT)
.setContentTitle(title)
.setContentText(text)
.setContentIntent(openApp(context))
.setContentIntent(open)
.setOngoing(true)
.setOnlyAlertOnce(true)
.setCategory(Notification.CATEGORY_CALL)
for ((title, intent) in actions) {
builder.addAction(Notification.Action.Builder(null, title, intent).build())
if (sinceMs > 0) {
builder.setWhen(sinceMs).setShowWhen(true).setUsesChronometer(true)
} else {
builder.setShowWhen(false)
}
for ((label, intent) in actions) {
builder.addAction(Notification.Action.Builder(null, label, intent).build())
}
return builder.build()
}
@@ -1,6 +1,8 @@
package io.github.faumaray.voelin
import android.Manifest
import android.app.Notification
import android.app.NotificationManager
import android.app.PendingIntent
import android.app.Service
import android.content.Context
@@ -66,21 +68,9 @@ class ScreenCaptureService : Service() {
return START_NOT_STICKY
}
if (projection != null) return START_NOT_STICKY
val stop = PendingIntent.getService(
this,
0,
Intent(this, ScreenCaptureService::class.java).setAction(ACTION_STOP),
PendingIntent.FLAG_IMMUTABLE,
)
val notification = Notifications.ongoing(
this,
Notifications.CHANNEL_SCREEN,
getString(R.string.screen_sharing),
getString(R.string.stop_sharing) to stop,
)
try {
// Must be in the foreground before getMediaProjection (Android 14).
startForeground(ID, notification, ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PROJECTION)
startForeground(ID, notification(this), ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PROJECTION)
val data = if (Build.VERSION.SDK_INT >= 33) {
intent.getParcelableExtra(EXTRA_DATA, Intent::class.java)
} else {
@@ -259,6 +249,36 @@ class ScreenCaptureService : Service() {
@Volatile
private var instance: ScreenCaptureService? = null
/** Where our stream is live and who watches ("Live in Chill Zone"), or null. */
@Volatile
private var notice: Pair<String, String>? = null
private fun notification(context: Context): Notification {
val stop = PendingIntent.getService(
context,
0,
Intent(context, ScreenCaptureService::class.java).setAction(ACTION_STOP),
PendingIntent.FLAG_IMMUTABLE,
)
val (title, text) = notice ?: (context.getString(R.string.screen_sharing) to context.getString(R.string.screen_sharing_text))
return Notifications.ongoing(
context,
Notifications.CHANNEL_SCREEN,
title,
text,
Notifications.openApp(context),
0,
context.getString(R.string.stop_sharing) to stop,
)
}
/** What the notification says while our stream is live (null: the default). */
fun setNotice(title: String?, text: String?) {
notice = if (title != null) title to (text ?: "") else null
val service = instance ?: return
service.getSystemService(NotificationManager::class.java).notify(ID, notification(service))
}
/** Start capturing with the consent the user just gave. */
fun start(context: Context, resultCode: Int, data: Intent, fps: Int, maxSize: Int) {
val intent = Intent(context, ScreenCaptureService::class.java)
@@ -15,8 +15,19 @@ import android.util.Log
* Foreground service while a voice connection exists: Android keeps the
* process, the network and (type microphone) the microphone of an app in
* the background only this way. The Rust side starts, updates and stops it.
* Its notification is the voice bar of the app: the channel, the server and
* how many are there, the time in voice, and Mute, Deafen and Leave.
*/
class VoiceService : Service() {
/** What the notification shows (crates/voelin-android/src/foreground.rs). */
data class Notice(
val title: String,
val text: String,
val muted: Boolean,
val deafened: Boolean,
val sinceMs: Long,
)
override fun onBind(intent: Intent?): IBinder? = null
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
@@ -29,14 +40,17 @@ class VoiceService : Service() {
Native.onToggleMute()
return START_NOT_STICKY
}
ACTION_TOGGLE_DEAFEN -> {
Native.onToggleDeafen()
return START_NOT_STICKY
}
}
val text = intent?.getStringExtra(EXTRA_TEXT) ?: getString(R.string.voice_connected)
val muted = intent?.getBooleanExtra(EXTRA_MUTED, false) ?: false
val notice = notice ?: Notice(getString(R.string.voice_connected), "", false, false, 0)
try {
if (Build.VERSION.SDK_INT >= 30) {
startForeground(ID, notification(this, text, muted), ServiceInfo.FOREGROUND_SERVICE_TYPE_MICROPHONE)
startForeground(ID, notification(this, notice), ServiceInfo.FOREGROUND_SERVICE_TYPE_MICROPHONE)
} else {
startForeground(ID, notification(this, text, muted))
startForeground(ID, notification(this, notice))
}
running = this
} catch (e: RuntimeException) {
@@ -56,14 +70,20 @@ class VoiceService : Service() {
companion object {
private const val TAG = "VoiceService"
private const val ID = 1
private const val EXTRA_TEXT = "text"
private const val EXTRA_MUTED = "muted"
private const val ACTION_DISCONNECT = "io.github.faumaray.voelin.DISCONNECT"
private const val ACTION_TOGGLE_MUTE = "io.github.faumaray.voelin.TOGGLE_MUTE"
private const val ACTION_TOGGLE_DEAFEN = "io.github.faumaray.voelin.TOGGLE_DEAFEN"
/** MainActivity shows the voice channel for this action. */
const val ACTION_OPEN_VOICE = "io.github.faumaray.voelin.OPEN_VOICE"
@Volatile
private var running: VoiceService? = null
/** The latest notice, for a service that is still starting. */
@Volatile
private var notice: Notice? = null
private fun action(context: Context, action: String, request: Int): PendingIntent =
PendingIntent.getService(
context,
@@ -72,30 +92,32 @@ class VoiceService : Service() {
PendingIntent.FLAG_IMMUTABLE,
)
private fun notification(context: Context, text: String, muted: Boolean): Notification =
private fun notification(context: Context, notice: Notice): Notification =
Notifications.ongoing(
context,
Notifications.CHANNEL_VOICE,
text,
context.getString(if (muted) R.string.unmute else R.string.mute) to
notice.title,
notice.text,
Notifications.openApp(context, ACTION_OPEN_VOICE),
notice.sinceMs,
context.getString(if (notice.muted) R.string.unmute else R.string.mute) to
action(context, ACTION_TOGGLE_MUTE, 1),
context.getString(R.string.disconnect) to action(context, ACTION_DISCONNECT, 0),
context.getString(if (notice.deafened) R.string.undeafen else R.string.deafen) to
action(context, ACTION_TOGGLE_DEAFEN, 2),
context.getString(R.string.leave) to action(context, ACTION_DISCONNECT, 0),
)
/** Start the service, or update its notification when it runs. */
fun start(context: Context, text: String, muted: Boolean) {
fun start(context: Context, notice: Notice) {
this.notice = notice
val service = running
if (service != null) {
service.getSystemService(NotificationManager::class.java)
.notify(ID, notification(service, text, muted))
.notify(ID, notification(service, notice))
return
}
try {
context.startForegroundService(
Intent(context, VoiceService::class.java)
.putExtra(EXTRA_TEXT, text)
.putExtra(EXTRA_MUTED, muted),
)
context.startForegroundService(Intent(context, VoiceService::class.java))
} catch (e: RuntimeException) {
// Not allowed from the background (Android 12+). Voice is
// connected from the UI, so this should not happen.
@@ -104,6 +126,7 @@ class VoiceService : Service() {
}
fun stop(context: Context) {
notice = null
context.stopService(Intent(context, VoiceService::class.java))
}
}