feat: pip preview

This commit is contained in:
Hinnka committed 2026-03-28 17:10:03 +08:00
1 parent cd82714179
commit 39cc72a80b
4 files changed
+129 -1

No files matched your search

@@ -5,17 +5,27 @@ import android.content.BroadcastReceiver
import android.content.Intent
import android.content.IntentFilter
import android.content.res.Configuration
import android.graphics.Bitmap
import android.os.Build
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.util.Rational
import android.view.PixelCopy
import android.view.WindowManager
import androidx.activity.ComponentActivity
import androidx.activity.compose.BackHandler
import androidx.activity.compose.setContent
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.PictureInPictureAlt
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.FilledTonalIconButton
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.Text
import androidx.compose.material3.Surface as ComposeSurface
import androidx.compose.runtime.Composable
@@ -30,9 +40,12 @@ import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.asImageBitmap
import androidx.compose.ui.layout.onSizeChanged
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.viewinterop.AndroidView
import androidx.compose.ui.unit.dp
import androidx.lifecycle.lifecycleScope
import com.hinnka.mycamera.R
import com.hinnka.mycamera.data.ContentRepository
@@ -198,6 +211,27 @@ private fun ScreenCapturePipContent(
}
val captureWidth = remember(windowMetrics) { windowMetrics.bounds.width().coerceAtLeast(1) }
val captureHeight = remember(windowMetrics) { windowMetrics.bounds.height().coerceAtLeast(1) }
var previewSurfaceView by remember { mutableStateOf<CameraGLSurfaceView?>(null) }
var frozenForegroundFrame by remember { mutableStateOf<Bitmap?>(null) }
var hasEnteredPip by remember { mutableStateOf(isInPiPMode) }
var shouldCaptureForegroundFrame by remember { mutableStateOf(false) }
fun replaceFrozenForegroundFrame(bitmap: Bitmap?) {
val previousBitmap = frozenForegroundFrame
frozenForegroundFrame = bitmap
if (previousBitmap != null && previousBitmap !== bitmap && !previousBitmap.isRecycled) {
previousBitmap.recycle()
}
}
fun requestForegroundPip(trigger: String) {
PLog.d("ScreenCapturePip", "$trigger, returning to PiP")
onRequestPip()
}
BackHandler(enabled = !isInPiPMode) {
requestForegroundPip("Back pressed in foreground")
}
LaunchedEffect(uiState.isCapturing) {
if (!uiState.isCapturing) return@LaunchedEffect
@@ -212,12 +246,42 @@ private fun ScreenCapturePipContent(
}
}
LaunchedEffect(isInPiPMode) {
if (isInPiPMode) {
hasEnteredPip = true
shouldCaptureForegroundFrame = false
replaceFrozenForegroundFrame(null)
return@LaunchedEffect
}
if (hasEnteredPip) {
shouldCaptureForegroundFrame = true
}
}
LaunchedEffect(shouldCaptureForegroundFrame, previewSurfaceView) {
val surfaceView = previewSurfaceView ?: return@LaunchedEffect
if (!shouldCaptureForegroundFrame) return@LaunchedEffect
shouldCaptureForegroundFrame = false
captureRenderedFrame(surfaceView) { bitmap ->
replaceFrozenForegroundFrame(bitmap)
}
}
DisposableEffect(Unit) {
onDispose {
controller.detachPreviewSurface(null)
}
}
DisposableEffect(frozenForegroundFrame) {
val bitmapToRecycle = frozenForegroundFrame
onDispose {
bitmapToRecycle?.takeIf { !it.isRecycled }?.recycle()
}
}
val showForegroundFrozenFrame = !isInPiPMode && hasEnteredPip
Box(
modifier = Modifier
.fillMaxSize()
@@ -231,6 +295,7 @@ private fun ScreenCapturePipContent(
AndroidView(
factory = { context ->
CameraGLSurfaceView(context).apply {
previewSurfaceView = this
onSurfaceReady = { surface ->
controller.attachPreviewSurface(
surface = surface,
@@ -245,6 +310,7 @@ private fun ScreenCapturePipContent(
}
},
update = { glSurfaceView ->
previewSurfaceView = glSurfaceView
glSurfaceView.setPreviewSize(captureWidth, captureHeight)
glSurfaceView.setSensorOrientation(0)
glSurfaceView.setLensFacing(1)
@@ -275,7 +341,16 @@ private fun ScreenCapturePipContent(
modifier = Modifier.fillMaxSize()
)
if (!isInPiPMode) {
if (showForegroundFrozenFrame) {
frozenForegroundFrame?.let { bitmap ->
Image(
bitmap = bitmap.asImageBitmap(),
contentDescription = null,
contentScale = ContentScale.Crop,
modifier = Modifier.fillMaxSize()
)
}
} else if (!isInPiPMode) {
Box(
modifier = Modifier
.fillMaxSize()
@@ -285,5 +360,55 @@ private fun ScreenCapturePipContent(
Text(stringResource(R.string.screen_capture_status_running), color = Color.White)
}
}
if (!isInPiPMode) {
FilledTonalIconButton(
onClick = { requestForegroundPip("Foreground PiP button tapped") },
modifier = Modifier
.align(Alignment.TopEnd)
.padding(top = 48.dp, end = 16.dp)
) {
Icon(
imageVector = Icons.Default.PictureInPictureAlt,
contentDescription = stringResource(R.string.screen_capture_return_to_pip)
)
}
}
}
}
private fun captureRenderedFrame(
surfaceView: CameraGLSurfaceView,
onCaptured: (Bitmap?) -> Unit
) {
val width = surfaceView.width
val height = surfaceView.height
if (width <= 0 || height <= 0) {
PLog.w("ScreenCapturePip", "Skip frozen frame capture because surface size is invalid: ${width}x${height}")
onCaptured(null)
return
}
val bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888)
try {
PixelCopy.request(
surfaceView,
bitmap,
{ result ->
if (result == PixelCopy.SUCCESS) {
PLog.d("ScreenCapturePip", "Captured frozen frame for PiP foreground restore")
onCaptured(bitmap)
} else {
PLog.w("ScreenCapturePip", "Failed to capture frozen frame, PixelCopy result=$result")
bitmap.recycle()
onCaptured(null)
}
},
Handler(Looper.getMainLooper())
)
} catch (t: Throwable) {
PLog.e("ScreenCapturePip", "Failed to capture frozen frame for PiP foreground restore", t)
bitmap.recycle()
onCaptured(null)
}
}
@@ -439,6 +439,7 @@
<string name="screen_capture_test_stop">停止</string>
<string name="screen_capture_test_status_idle">准备启动录屏预览。</string>
<string name="screen_capture_status_running">录屏已开启,正在以画中画小窗显示。</string>
<string name="screen_capture_return_to_pip">画中画</string>
<string name="screen_capture_test_status_stopped">录屏已停止。</string>
<string name="screen_capture_test_status_permission_denied">未授予录屏权限。</string>
<string name="screen_capture_test_status_failed">录屏启动失败。</string>
@@ -428,6 +428,7 @@
<string name="screen_capture_test_stop">停止</string>
<string name="screen_capture_test_status_idle">準備啟動錄屏預覽。</string>
<string name="screen_capture_status_running">錄屏已開啟,正在以畫中畫小窗顯示。</string>
<string name="screen_capture_return_to_pip">畫中畫</string>
<string name="screen_capture_test_status_stopped">錄屏已停止。</string>
<string name="screen_capture_test_status_permission_denied">未授予錄屏權限。</string>
<string name="screen_capture_test_status_failed">錄屏啟動失敗。</string>
+1
View File
@@ -439,6 +439,7 @@
<string name="screen_capture_test_stop">Stop</string>
<string name="screen_capture_test_status_idle">Ready to start screen capture preview.</string>
<string name="screen_capture_status_running">Screen capture is active and shown in picture-in-picture.</string>
<string name="screen_capture_return_to_pip">PiP</string>
<string name="screen_capture_test_status_stopped">Screen capture stopped.</string>
<string name="screen_capture_test_status_permission_denied">Screen capture permission denied.</string>
<string name="screen_capture_test_status_failed">Failed to start screen capture.</string>