diff --git a/app/src/main/java/com/hinnka/mycamera/screencapture/ScreenCapturePipActivity.kt b/app/src/main/java/com/hinnka/mycamera/screencapture/ScreenCapturePipActivity.kt index 0bab7365..efcd59b3 100644 --- a/app/src/main/java/com/hinnka/mycamera/screencapture/ScreenCapturePipActivity.kt +++ b/app/src/main/java/com/hinnka/mycamera/screencapture/ScreenCapturePipActivity.kt @@ -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(null) } + var frozenForegroundFrame by remember { mutableStateOf(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) } } diff --git a/app/src/main/res/values-zh-rCN/strings.xml b/app/src/main/res/values-zh-rCN/strings.xml index c3d3f799..ee179d6f 100644 --- a/app/src/main/res/values-zh-rCN/strings.xml +++ b/app/src/main/res/values-zh-rCN/strings.xml @@ -439,6 +439,7 @@ 停止 准备启动录屏预览。 录屏已开启,正在以画中画小窗显示。 + 画中画 录屏已停止。 未授予录屏权限。 录屏启动失败。 diff --git a/app/src/main/res/values-zh-rHK/strings.xml b/app/src/main/res/values-zh-rHK/strings.xml index 8b44ade6..28cd8d08 100644 --- a/app/src/main/res/values-zh-rHK/strings.xml +++ b/app/src/main/res/values-zh-rHK/strings.xml @@ -428,6 +428,7 @@ 停止 準備啟動錄屏預覽。 錄屏已開啟,正在以畫中畫小窗顯示。 + 畫中畫 錄屏已停止。 未授予錄屏權限。 錄屏啟動失敗。 diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index d97557da..09a58e1c 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -439,6 +439,7 @@ Stop Ready to start screen capture preview. Screen capture is active and shown in picture-in-picture. + PiP Screen capture stopped. Screen capture permission denied. Failed to start screen capture.