wip: Mute action in the voice notification, shared capture clock

This commit is contained in:
Claude committed 2026-09-26 15:20:20 +00:00
1 parent 7a8c901a08
commit 67dd8e04d6
11 files changed
+159 -81

No files matched your search

@@ -28,10 +28,13 @@ object Bridge {
}
}
/** Run the voice service with this notification text; null stops it. */
/**
* Run the voice service with this notification text (`muted`: the action
* offers Unmute); null stops it.
*/
@JvmStatic
fun setVoiceNotification(text: String?) {
if (text == null) VoiceService.stop(app) else VoiceService.start(app, text)
fun setVoiceNotification(text: String?, muted: Boolean) {
if (text == null) VoiceService.stop(app) else VoiceService.start(app, text, muted)
}
/** Ask the user to share the screen; the answer goes to Native.onScreenCaptureResult. */
@@ -38,4 +38,8 @@ object Native {
/** "Disconnect" in the voice notification. */
@JvmStatic
external fun onDisconnectVoice()
/** "Mute" / "Unmute" in the voice notification. */
@JvmStatic
external fun onToggleMute()
}
@@ -38,19 +38,24 @@ object Notifications {
PendingIntent.FLAG_IMMUTABLE,
)
/** An ongoing notification with `actions` (title, intent). */
fun ongoing(
context: Context,
channel: String,
text: String,
actionTitle: String,
action: PendingIntent,
): Notification = Notification.Builder(context, channel)
.setSmallIcon(R.drawable.ic_notification)
.setContentTitle(context.getString(R.string.app_name))
.setContentText(text)
.setContentIntent(openApp(context))
.setOngoing(true)
.setCategory(Notification.CATEGORY_CALL)
.addAction(Notification.Action.Builder(null, actionTitle, action).build())
.build()
vararg actions: Pair<String, PendingIntent>,
): Notification {
val builder = Notification.Builder(context, channel)
.setSmallIcon(R.drawable.ic_notification)
.setContentTitle(context.getString(R.string.app_name))
.setContentText(text)
.setContentIntent(openApp(context))
.setOngoing(true)
.setOnlyAlertOnce(true)
.setCategory(Notification.CATEGORY_CALL)
for ((title, intent) in actions) {
builder.addAction(Notification.Action.Builder(null, title, intent).build())
}
return builder.build()
}
}
@@ -65,8 +65,7 @@ class ScreenCaptureService : Service() {
this,
Notifications.CHANNEL_SCREEN,
getString(R.string.screen_sharing),
getString(R.string.stop_sharing),
stop,
getString(R.string.stop_sharing) to stop,
)
try {
// Must be in the foreground before getMediaProjection (Android 14).
@@ -20,16 +20,23 @@ class VoiceService : Service() {
override fun onBind(intent: Intent?): IBinder? = null
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
if (intent?.action == ACTION_DISCONNECT) {
Native.onDisconnectVoice()
return START_NOT_STICKY
when (intent?.action) {
ACTION_DISCONNECT -> {
Native.onDisconnectVoice()
return START_NOT_STICKY
}
ACTION_TOGGLE_MUTE -> {
Native.onToggleMute()
return START_NOT_STICKY
}
}
val text = intent?.getStringExtra(EXTRA_TEXT) ?: getString(R.string.voice_connected)
val muted = intent?.getBooleanExtra(EXTRA_MUTED, false) ?: false
try {
if (Build.VERSION.SDK_INT >= 30) {
startForeground(ID, notification(this, text), ServiceInfo.FOREGROUND_SERVICE_TYPE_MICROPHONE)
startForeground(ID, notification(this, text, muted), ServiceInfo.FOREGROUND_SERVICE_TYPE_MICROPHONE)
} else {
startForeground(ID, notification(this, text))
startForeground(ID, notification(this, text, muted))
}
running = this
} catch (e: RuntimeException) {
@@ -50,38 +57,44 @@ class VoiceService : Service() {
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.tsc.DISCONNECT"
private const val ACTION_TOGGLE_MUTE = "io.github.faumaray.tsc.TOGGLE_MUTE"
@Volatile
private var running: VoiceService? = null
private fun notification(context: Context, text: String): Notification {
val disconnect = PendingIntent.getService(
private fun action(context: Context, action: String, request: Int): PendingIntent =
PendingIntent.getService(
context,
0,
Intent(context, VoiceService::class.java).setAction(ACTION_DISCONNECT),
request,
Intent(context, VoiceService::class.java).setAction(action),
PendingIntent.FLAG_IMMUTABLE,
)
return Notifications.ongoing(
private fun notification(context: Context, text: String, muted: Boolean): Notification =
Notifications.ongoing(
context,
Notifications.CHANNEL_VOICE,
text,
context.getString(R.string.disconnect),
disconnect,
context.getString(if (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),
)
}
/** Start the service, or update its notification when it runs. */
fun start(context: Context, text: String) {
fun start(context: Context, text: String, muted: Boolean) {
val service = running
if (service != null) {
service.getSystemService(NotificationManager::class.java)
.notify(ID, notification(service, text))
.notify(ID, notification(service, text, muted))
return
}
try {
context.startForegroundService(
Intent(context, VoiceService::class.java).putExtra(EXTRA_TEXT, text),
Intent(context, VoiceService::class.java)
.putExtra(EXTRA_TEXT, text)
.putExtra(EXTRA_MUTED, muted),
)
} catch (e: RuntimeException) {
// Not allowed from the background (Android 12+). Voice is