Modern Apps git

← Modern-Apps.git

communicate: video calling, PiP, a draggable self-view and CallStyle notifications

Commit
83a8dc393d2e72197a20a55f0a20bf0334a6e252
Author
vayun-mathur <[email protected]>
When
2026-08-23 01:29 UTC (3 months ago)
Parents
0e4debf

Diff

communicate/src/main/AndroidManifest.xml
diff --git a/communicate/src/main/AndroidManifest.xml b/communicate/src/main/AndroidManifest.xmlindex 9d5eb1f1a4..0777338b7b 100644--- a/communicate/src/main/AndroidManifest.xml+++ b/communicate/src/main/AndroidManifest.xml@@ -60,6 +60,7 @@             android:name=".MainActivity"             android:configChanges="mcc|mnc|colorMode|density|fontScale|fontWeightAdjustment|keyboard|keyboardHidden|layoutDirection|navigation|orientation|screenLayout|screenSize|smallestScreenSize|touchscreen|uiMode"             android:exported="true"+            android:supportsPictureInPicture="true"             android:theme="@style/Theme.General">             <intent-filter>                 <action android:name="android.intent.action.MAIN" />
communicate/src/main/java/com/vayunmathur/communicate/MainActivity.kt
diff --git a/communicate/src/main/java/com/vayunmathur/communicate/MainActivity.kt b/communicate/src/main/java/com/vayunmathur/communicate/MainActivity.ktindex eff1a7f4ec..0245dcb401 100644--- a/communicate/src/main/java/com/vayunmathur/communicate/MainActivity.kt+++ b/communicate/src/main/java/com/vayunmathur/communicate/MainActivity.kt@@ -1,9 +1,11 @@ package com.vayunmathur.communicate  import android.Manifest+import android.app.PictureInPictureParams import android.content.pm.PackageManager import android.os.Build import android.os.Bundle+import android.util.Rational import androidx.activity.ComponentActivity import androidx.activity.compose.setContent import androidx.activity.enableEdgeToEdge@@ -93,6 +95,25 @@ class MainActivity : ComponentActivity() {         }     } +    /**+     * Shrink a video call into picture-in-picture when leaving the app, so the far end stays visible.+     *+     * Only for video: an audio call has nothing to show, and the ongoing-call notification already covers it.+     */+    override fun onUserLeaveHint() {+        super.onUserLeaveHint()+        val call = InAppCallRegistry.state.value+        val videoActive = call.remoteVideoEnabled || call.localVideoEnabled+        if (!videoActive || call.phase == InAppCallPhase.Idle || call.phase == InAppCallPhase.Ended) return+        runCatching {+            enterPictureInPictureMode(+                PictureInPictureParams.Builder()+                    .setAspectRatio(Rational(9, 16))+                    .build(),+            )+        }+    }+     companion object {         private const val REQUEST_POST_NOTIFICATIONS = 1001     }
communicate/src/main/java/com/vayunmathur/communicate/data/CommunicateRepository.kt
diff --git a/communicate/src/main/java/com/vayunmathur/communicate/data/CommunicateRepository.kt b/communicate/src/main/java/com/vayunmathur/communicate/data/CommunicateRepository.ktindex 5f677cd2a5..12d399f3c8 100644--- a/communicate/src/main/java/com/vayunmathur/communicate/data/CommunicateRepository.kt+++ b/communicate/src/main/java/com/vayunmathur/communicate/data/CommunicateRepository.kt@@ -1397,6 +1397,16 @@ object CommunicateRepository {         CommunicateLine.Signal -> com.vayunmathur.communicate.data.signal.SignalFeature.enabled     } +    /**+     * Whether [line] can place a **video** call. Only the in-app WebRTC lines can; SIM and Google Voice hand+     * the call to Telecom, which carries audio only.+     */+    fun canPlaceVideoCall(line: CommunicateLine): Boolean = when (line) {+        CommunicateLine.WhatsApp -> com.vayunmathur.communicate.data.whatsapp.WhatsAppFeature.enabled+        CommunicateLine.Signal -> com.vayunmathur.communicate.data.signal.SignalFeature.enabled+        else -> false+    }+     suspend fun createSignalGroup(         context: Context,         subject: String,
communicate/src/main/java/com/vayunmathur/communicate/data/call/InAppCall.kt
diff --git a/communicate/src/main/java/com/vayunmathur/communicate/data/call/InAppCall.kt b/communicate/src/main/java/com/vayunmathur/communicate/data/call/InAppCall.ktindex 2614c942a1..5037496ac9 100644--- a/communicate/src/main/java/com/vayunmathur/communicate/data/call/InAppCall.kt+++ b/communicate/src/main/java/com/vayunmathur/communicate/data/call/InAppCall.kt@@ -154,6 +154,10 @@ object InAppCallRegistry {             peerId = peerId,             peerName = peerName.ifBlank { peerId },             isVideo = isVideo,+            // A call placed as video starts with the camera and speaker on; RingRTC is told the same via+            // proceed()'s enableCamera, so the UI must not claim otherwise.+            localVideoEnabled = isVideo && !incoming,+            speaker = isVideo && !incoming,             capabilities = capabilities,         )     }@@ -208,6 +212,12 @@ object InAppCallRegistry {         val next = !_state.value.localVideoEnabled         videoController?.setVideoEnabled(next)         onLocalVideo(next)+        // Sending video implies hands-free: nobody holds a phone to their ear to be seen. The speaker+        // control is hidden while video is on, so the route is forced here rather than left stale.+        if (next && !_state.value.speaker) {+            controller?.setSpeaker(true)+            onSpeaker(true)+        }     }      fun flipCamera() {
communicate/src/main/java/com/vayunmathur/communicate/telephony/InAppCallForegroundService.kt
diff --git a/communicate/src/main/java/com/vayunmathur/communicate/telephony/InAppCallForegroundService.kt b/communicate/src/main/java/com/vayunmathur/communicate/telephony/InAppCallForegroundService.ktindex 5f2ea427e2..90a715121e 100644--- a/communicate/src/main/java/com/vayunmathur/communicate/telephony/InAppCallForegroundService.kt+++ b/communicate/src/main/java/com/vayunmathur/communicate/telephony/InAppCallForegroundService.kt@@ -3,6 +3,7 @@ package com.vayunmathur.communicate.telephony import android.app.Notification import android.app.NotificationManager import android.app.PendingIntent+import android.app.Person import android.app.Service import android.content.Context import android.content.Intent@@ -85,48 +86,50 @@ class InAppCallForegroundService : Service() {         }     } -    private fun buildIncomingCallNotification(peerName: String): Notification =-        Notification.Builder(this, INCOMING_CHANNEL_ID)-            .setContentTitle(getString(R.string.call_state_incoming_generic))-            .setContentText(peerName)+    /**+     * Incoming-call notification using [Notification.CallStyle], which is what gives the system's standard+     * green answer / red decline buttons and full-screen ringing treatment. Hand-built actions would render+     * in the app's theme colours instead, which reads as an ordinary notification rather than a ringing call.+     */+    private fun buildIncomingCallNotification(peerName: String): Notification {+        val caller = Person.Builder()+            .setName(peerName.ifBlank { getString(R.string.call_state_incoming_generic) })+            .setImportant(true)+            .build()+        return Notification.Builder(this, INCOMING_CHANNEL_ID)+            .setStyle(+                Notification.CallStyle.forIncomingCall(+                    caller,+                    serviceActionIntent(ACTION_HANGUP, 3),+                    serviceActionIntent(ACTION_ANSWER, 2),+                ),+            )             .setSmallIcon(android.R.drawable.sym_call_incoming)             .setCategory(Notification.CATEGORY_CALL)-            .setPriority(Notification.PRIORITY_MAX)+            // Colorized plus CallStyle is what promotes it to the ringing treatment.+            .setColorized(true)             .setVisibility(Notification.VISIBILITY_PUBLIC)             .setOngoing(true)             .setAutoCancel(false)-            .setContentIntent(callActivityIntent(0))             .setFullScreenIntent(callActivityIntent(1), true)-            .addAction(-                android.R.drawable.sym_call_incoming,-                getString(R.string.call_answer),-                serviceActionIntent(ACTION_ANSWER, 2),-            )-            .addAction(-                android.R.drawable.ic_menu_close_clear_cancel,-                getString(R.string.call_decline),-                serviceActionIntent(ACTION_HANGUP, 3),-            )             .build()+    }      private fun buildOngoingCallNotification(phase: InAppCallPhase, peerName: String): Notification {+        val caller = Person.Builder().setName(peerName).setImportant(true).build()         val title = when (phase) {             InAppCallPhase.Outgoing -> getString(R.string.call_state_dialing)             InAppCallPhase.Connecting -> getString(R.string.call_state_connecting)             else -> getString(R.string.inapp_ongoing_call)         }         return Notification.Builder(this, CHANNEL_ID)+            .setStyle(Notification.CallStyle.forOngoingCall(caller, serviceActionIntent(ACTION_HANGUP, 3)))             .setContentTitle(title)-            .setContentText(peerName)             .setSmallIcon(android.R.drawable.sym_action_call)             .setCategory(Notification.CATEGORY_CALL)+            .setColorized(true)             .setOngoing(true)             .setContentIntent(callActivityIntent(0))-            .addAction(-                android.R.drawable.ic_menu_close_clear_cancel,-                getString(R.string.call_end),-                serviceActionIntent(ACTION_HANGUP, 3),-            )             .build()     } 
communicate/src/main/java/com/vayunmathur/communicate/ui/ConversationScreen.kt
diff --git a/communicate/src/main/java/com/vayunmathur/communicate/ui/ConversationScreen.kt b/communicate/src/main/java/com/vayunmathur/communicate/ui/ConversationScreen.ktindex 81aa0b9a5d..4ccaba993a 100644--- a/communicate/src/main/java/com/vayunmathur/communicate/ui/ConversationScreen.kt+++ b/communicate/src/main/java/com/vayunmathur/communicate/ui/ConversationScreen.kt@@ -299,6 +299,21 @@ fun ConversationScreen(                         if (!placed) AppMessages.show(context.getString(R.string.call_failed))                     }                 }) { com.vayunmathur.library.ui.IconCall() }+                // Only the in-app WebRTC lines can carry video; Telecom lines cannot.+                if (CommunicateRepository.canPlaceVideoCall(line)) {+                    IconButton(onClick = {+                        scope.launch {+                            val placed = CommunicateRepository.placeCallForLine(+                                context = context,+                                line = line,+                                address = address,+                                remoteId = remoteId,+                                video = true,+                            )+                            if (!placed) AppMessages.show(context.getString(R.string.call_failed))+                        }+                    }) { com.vayunmathur.library.ui.IconVideoCamera() }+                }             }             if (line == CommunicateLine.GoogleVoice && remoteId != null) {                 IconButton(onClick = {
communicate/src/main/java/com/vayunmathur/communicate/ui/call/InAppCallScreen.kt
diff --git a/communicate/src/main/java/com/vayunmathur/communicate/ui/call/InAppCallScreen.kt b/communicate/src/main/java/com/vayunmathur/communicate/ui/call/InAppCallScreen.ktindex 08bed120fa..e596866340 100644--- a/communicate/src/main/java/com/vayunmathur/communicate/ui/call/InAppCallScreen.kt+++ b/communicate/src/main/java/com/vayunmathur/communicate/ui/call/InAppCallScreen.kt@@ -1,14 +1,17 @@ package com.vayunmathur.communicate.ui.call  import androidx.compose.foundation.background+import androidx.compose.foundation.gestures.detectTransformGestures import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Box+import androidx.compose.foundation.layout.BoxWithConstraints import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.Row import androidx.compose.foundation.layout.Spacer import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.foundation.layout.height+import androidx.compose.foundation.layout.offset import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.size import androidx.compose.foundation.layout.statusBarsPadding@@ -19,6 +22,7 @@ import androidx.compose.runtime.Composable import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.collectAsState import androidx.compose.runtime.getValue+import androidx.compose.runtime.mutableFloatStateOf import androidx.compose.runtime.mutableLongStateOf import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember@@ -26,9 +30,14 @@ import androidx.compose.runtime.setValue import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.draw.clip+import androidx.compose.ui.geometry.Offset import androidx.compose.ui.graphics.Color+import androidx.compose.ui.input.pointer.pointerInput+import androidx.compose.ui.platform.LocalDensity import androidx.compose.ui.res.stringResource import androidx.compose.ui.text.font.FontWeight+import androidx.compose.ui.unit.DpSize+import androidx.compose.ui.unit.IntOffset import androidx.compose.ui.unit.dp import androidx.compose.ui.viewinterop.AndroidView import com.vayunmathur.communicate.R@@ -229,7 +238,9 @@ private fun OngoingControls(                     onClick = { InAppCallRegistry.toggleMuted() },                 ) { tint -> if (state.muted) IconMicOff(tint = tint) else IconMic(tint = tint) }             }-            if (caps.speaker) {+            if (caps.speaker && !state.localVideoEnabled) {+                // Sending video implies hands-free, so the route is forced and the control would be a+                // no-op the user could fight with.                 CallToggle(                     label = stringResource(R.string.call_speaker),                     active = state.speaker,@@ -287,12 +298,13 @@ private fun CallAction(             shape = CircleShape,             color = container,             contentColor = content,-            modifier = Modifier.size(size.dp),+            // Both dimensions, so the circle cannot be squashed by the row's constraints.+            modifier = Modifier.size(width = size.dp, height = size.dp),         ) {-            Box(contentAlignment = Alignment.Center) { icon(content) }+            Box(modifier = Modifier.fillMaxSize(), contentAlignment = Alignment.Center) { icon(content) }         }-        Spacer(Modifier.height(Spacing.xs))-        Text(label, style = MaterialTheme.typography.labelSmall)+        Spacer(Modifier.height(Spacing.sm))+        Text(label, style = MaterialTheme.typography.labelMedium)     } } @@ -320,14 +332,14 @@ private fun CallToggle(             shape = CircleShape,             color = container,             contentColor = content,-            modifier = Modifier.size(SECONDARY_ACTION.dp),+            modifier = Modifier.size(width = SECONDARY_ACTION.dp, height = SECONDARY_ACTION.dp),         ) {-            Box(contentAlignment = Alignment.Center) { icon(content) }+            Box(modifier = Modifier.fillMaxSize(), contentAlignment = Alignment.Center) { icon(content) }         }-        Spacer(Modifier.height(Spacing.xs))+        Spacer(Modifier.height(Spacing.sm))         Text(             label,-            style = MaterialTheme.typography.labelSmall,+            style = MaterialTheme.typography.labelMedium,             color = MaterialTheme.colorScheme.onSurfaceVariant,         )     }@@ -374,7 +386,7 @@ private fun CallVideo(showRemote: Boolean, showLocal: Boolean) {     val controller = InAppCallRegistry.videoController ?: return     val eglContext = remember(controller) { controller.eglContext() } ?: return -    Box(modifier = Modifier.fillMaxSize()) {+    BoxWithConstraints(modifier = Modifier.fillMaxSize()) {         if (showRemote) {             AndroidView(                 factory = { context ->@@ -393,28 +405,81 @@ private fun CallVideo(showRemote: Boolean, showLocal: Boolean) {             )         }         if (showLocal) {-            AndroidView(-                factory = { context ->-                    SurfaceViewRenderer(context).apply {-                        init(eglContext, null)-                        setScalingType(RendererCommon.ScalingType.SCALE_ASPECT_FILL)-                        setMirror(true)-                    }-                },-                // Bottom end, clear of the status bar and the header text.-                modifier = Modifier-                    .align(Alignment.BottomEnd)-                    .padding(end = Spacing.lg, bottom = 220.dp)-                    .width(104.dp)-                    .height(140.dp)-                    .clip(RoundedCornerShape(Spacing.md)),-                onRelease = { renderer -> renderer.release() },-                update = { renderer -> controller.attachRenderers(local = renderer, remote = null) },+            SelfView(+                eglContext = eglContext,+                bounds = DpSize(maxWidth, maxHeight),+                onRenderer = { renderer -> controller.attachRenderers(local = renderer, remote = null) },             )         }     } } +/**+ * The self-view: draggable anywhere on screen and pinch-resizable, like a picture-in-picture window.+ *+ * Position and scale are kept in this composable rather than in call state — they are a view preference, not+ * something the call or the other participant cares about. Both are clamped to [bounds] so the window cannot+ * be dragged or grown off-screen where it could not be recovered.+ */+@Composable+private fun SelfView(+    eglContext: org.webrtc.EglBase.Context,+    bounds: DpSize,+    onRenderer: (SurfaceViewRenderer) -> Unit,+) {+    val density = LocalDensity.current+    var scale by remember { mutableFloatStateOf(1f) }+    val width = (SELF_VIEW_WIDTH.dp * scale).coerceIn(SELF_VIEW_MIN.dp, bounds.width * 0.6f)+    val height = width * SELF_VIEW_ASPECT++    // Starts bottom-end, clear of the controls.+    var offset by remember(bounds) {+        mutableStateOf(+            with(density) {+                Offset(+                    x = (bounds.width - width - Spacing.lg).toPx(),+                    y = (bounds.height - height - SELF_VIEW_BOTTOM_INSET.dp).toPx(),+                )+            },+        )+    }++    val maxOffset = with(density) {+        Offset((bounds.width - width).toPx().coerceAtLeast(0f), (bounds.height - height).toPx().coerceAtLeast(0f))+    }+    // Re-clamped after a resize, so growing near an edge does not push it out of reach.+    offset = Offset(offset.x.coerceIn(0f, maxOffset.x), offset.y.coerceIn(0f, maxOffset.y))++    Box(+        modifier = Modifier+            .offset { IntOffset(offset.x.toInt(), offset.y.toInt()) }+            .size(width = width, height = height)+            .clip(RoundedCornerShape(Spacing.md))+            .pointerInput(bounds, width) {+                detectTransformGestures { _, pan, zoom, _ ->+                    scale = (scale * zoom).coerceIn(SELF_VIEW_MIN_SCALE, SELF_VIEW_MAX_SCALE)+                    offset = Offset(+                        (offset.x + pan.x).coerceIn(0f, maxOffset.x),+                        (offset.y + pan.y).coerceIn(0f, maxOffset.y),+                    )+                }+            },+    ) {+        AndroidView(+            factory = { context ->+                SurfaceViewRenderer(context).apply {+                    init(eglContext, null)+                    setScalingType(RendererCommon.ScalingType.SCALE_ASPECT_FILL)+                    setMirror(true)+                }+            },+            modifier = Modifier.fillMaxSize(),+            onRelease = { renderer -> renderer.release() },+            update = onRenderer,+        )+    }+}+ @Composable private fun callStatusText(state: InAppCallState): String {     val lineName = when (state.line) {@@ -441,3 +506,12 @@ private fun callStatusText(state: InAppCallState): String {  private const val PRIMARY_ACTION = 72 private const val SECONDARY_ACTION = 56++private const val SELF_VIEW_WIDTH = 108+private const val SELF_VIEW_MIN = 72+private const val SELF_VIEW_ASPECT = 4f / 3f+private const val SELF_VIEW_MIN_SCALE = 0.7f+private const val SELF_VIEW_MAX_SCALE = 2.5f++/** Initial gap above the control row, so the self-view does not start on top of the buttons. */+private const val SELF_VIEW_BOTTOM_INSET = 220