diff --git a/feature/afternote/presentation/src/main/kotlin/com/afternote/feature/afternote/presentation/receiver/detail/MemorialReceivedDetailScreen.kt b/feature/afternote/presentation/src/main/kotlin/com/afternote/feature/afternote/presentation/receiver/detail/MemorialReceivedDetailScreen.kt index 24a9e2b14d..b977f59a16 100644 --- a/feature/afternote/presentation/src/main/kotlin/com/afternote/feature/afternote/presentation/receiver/detail/MemorialReceivedDetailScreen.kt +++ b/feature/afternote/presentation/src/main/kotlin/com/afternote/feature/afternote/presentation/receiver/detail/MemorialReceivedDetailScreen.kt @@ -1,7 +1,7 @@ package com.afternote.feature.afternote.presentation.receiver.detail +import android.content.ActivityNotFoundException import android.content.Intent -import android.content.pm.PackageManager import android.widget.Toast import androidx.compose.foundation.Image import androidx.compose.foundation.background @@ -172,21 +172,29 @@ private fun ReceiverVideoSection( // clip 을 clickable 앞에: 눌림 피드백이 InfoCard 의 12dp 둥근 모서리 안에서만 그려지게 (모서리 밖 사각 번짐 방지) .clip(RoundedCornerShape(12.dp)) .clickable { - val intent = Intent(Intent.ACTION_VIEW, memorialVideoUrl.toUri()) - if (context.packageManager.resolveActivity( - intent, - PackageManager.MATCH_DEFAULT_ONLY, - ) != null - ) { - context.startActivity(intent) - } else { - Toast - .makeText( - context, - "영상을 재생할 수 있는 앱이 없습니다.", - Toast.LENGTH_SHORT, - ).show() - } + launchReceivedMemorialVideo( + videoUrl = memorialVideoUrl, + startActivity = context::startActivity, + // 원인이 다르면 문구도 달라야 한다 — 아래 둘을 한 문장으로 덮으면 + // 스킴 차단 상황에 «앱이 없습니다» 라는 거짓 안내가 나간다. + // 채널(Snackbar) 전환·리소스화·표출 테스트는 #1391 건 3. + onRejected = { + Toast + .makeText( + context, + "영상 주소가 올바르지 않아 재생할 수 없습니다.", + Toast.LENGTH_SHORT, + ).show() + }, + onUnavailable = { + Toast + .makeText( + context, + "영상을 재생할 수 있는 앱이 없습니다.", + Toast.LENGTH_SHORT, + ).show() + }, + ) }, ) { ReceiverMemorialVideoThumbnail(thumbnailUrl = memorialThumbnailUrl) @@ -218,6 +226,63 @@ private fun ReceiverVideoSection( } } +/** + * 서버가 준 추모 영상 URL을 외부 재생 앱으로 연다. + * + * 수신자 화면은 발신자가 저장한 값을 여는 쪽인데 서버는 비관리 URL 을 원문 그대로 반환하므로, + * http/https 가 아닌 스킴은 실행하지 않는다 (#1394 — 발신자발 위험 스킴 차단). 불합격 URL 은 + * [onRejected] 로 알린다. + * Android 11+ 패키지 가시성에서는 외부 앱 사전 조회가 실제 처리 가능한 앱이 있어도 실패할 수 + * 있다. 따라서 http/https URL만 선별한 뒤 실행을 직접 시도하고, OS가 명시적으로 거부한 경우에만 + * [onUnavailable] 로 폴백한다. + * + * 두 콜백을 나눈 이유는 **원인이 다르면 안내도 달라야 하기 때문**이다. 하나로 합치면 URL 이 막힌 + * 경우에도 «재생할 앱이 없습니다» 가 나가는데, 그건 앱 유무와 무관한 거짓이다. + * + * 작성자 쪽 상세의 `launchMemorialVideo`(PR #1336, develop 머지됨)와 같은 패턴이나 — 그쪽은 + * afternote 모듈 `internal` 이라 공유 없이 이식했다 — 콜백 분리는 이 판에만 있어 본문이 갈라져 + * 있다. `core:common` 승격(#1436)은 이 시그니처를 계약으로 삼고, 승격 시 작성자 판도 같이 + * 갈라야 한다. + */ +internal fun launchReceivedMemorialVideo( + videoUrl: String, + startActivity: (Intent) -> Unit, + onRejected: () -> Unit, + onUnavailable: () -> Unit, +) { + val uri = + try { + videoUrl + .takeUnless { it.isBlank() || it.any(Char::isWhitespace) } + ?.toUri() + ?.takeIf { + val scheme = it.scheme + ( + scheme.equals("http", ignoreCase = true) || + scheme.equals("https", ignoreCase = true) + ) && + !it.host.isNullOrBlank() + } + } catch (_: IllegalArgumentException) { + null + } + + if (uri == null) { + onRejected() + return + } + + try { + startActivity(Intent(Intent.ACTION_VIEW, uri)) + } catch (_: ActivityNotFoundException) { + onUnavailable() + } catch (_: SecurityException) { + onUnavailable() + } catch (_: IllegalArgumentException) { + onUnavailable() + } +} + @Composable private fun ReceiverMemorialVideoThumbnail(thumbnailUrl: String?) { Box( diff --git a/feature/afternote/presentation/src/test/kotlin/com/afternote/feature/afternote/presentation/receiver/detail/ReceivedMemorialVideoLauncherTest.kt b/feature/afternote/presentation/src/test/kotlin/com/afternote/feature/afternote/presentation/receiver/detail/ReceivedMemorialVideoLauncherTest.kt new file mode 100644 index 0000000000..7cdc292d37 --- /dev/null +++ b/feature/afternote/presentation/src/test/kotlin/com/afternote/feature/afternote/presentation/receiver/detail/ReceivedMemorialVideoLauncherTest.kt @@ -0,0 +1,97 @@ +package com.afternote.feature.afternote.presentation.receiver.detail + +import android.content.ActivityNotFoundException +import android.content.Intent +import org.junit.Assert.assertEquals +import org.junit.Assert.assertTrue +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.RobolectricTestRunner +import org.robolectric.annotation.Config + +@RunWith(RobolectricTestRunner::class) +@Config(sdk = [35]) +class ReceivedMemorialVideoLauncherTest { + @Test + fun `http와 https URL은 ACTION_VIEW로 직접 실행한다`() { + val urls = + listOf( + "https://cdn.example.com/memorial.mp4", + "http://cdn.example.com/memorial.mp4", + ) + val startedIntents = mutableListOf() + var rejectedCount = 0 + var unavailableCount = 0 + + urls.forEach { url -> + launchReceivedMemorialVideo( + videoUrl = url, + startActivity = { startedIntents += it }, + onRejected = { rejectedCount += 1 }, + onUnavailable = { unavailableCount += 1 }, + ) + } + + assertEquals(urls, startedIntents.map { it.dataString }) + assertTrue(startedIntents.all { it.action == Intent.ACTION_VIEW }) + assertEquals(0, rejectedCount) + assertEquals(0, unavailableCount) + } + + @Test + fun `http와 https가 아닌 URL이나 잘못된 URL은 실행하지 않고 거부로 안내한다`() { + val rejectedUrls = + listOf( + "", + "not a url", + "ftp://cdn.example.com/memorial.mp4", + "content://videos/memorial", + "javascript:alert(1)", + "intent://memorial#Intent;scheme=https;end", + "tel:0212345678", + "https:///missing-host.mp4", + ) + var startedCount = 0 + var rejectedCount = 0 + var unavailableCount = 0 + + rejectedUrls.forEach { url -> + launchReceivedMemorialVideo( + videoUrl = url, + startActivity = { startedCount += 1 }, + onRejected = { rejectedCount += 1 }, + onUnavailable = { unavailableCount += 1 }, + ) + } + + assertEquals(0, startedCount) + assertEquals(rejectedUrls.size, rejectedCount) + // URL 이 막힌 것을 «재생할 앱이 없다» 로 안내하면 거짓이다 — 갈래가 섞이지 않아야 한다. + assertEquals(0, unavailableCount) + } + + @Test + fun `외부 실행이 거부되면 실행 불가로 안내한다`() { + val failures = + listOf( + ActivityNotFoundException("no player"), + SecurityException("blocked"), + IllegalArgumentException("bad uri"), + ) + var rejectedCount = 0 + var unavailableCount = 0 + + failures.forEach { failure -> + launchReceivedMemorialVideo( + videoUrl = "https://cdn.example.com/memorial.mp4", + startActivity = { throw failure }, + onRejected = { rejectedCount += 1 }, + onUnavailable = { unavailableCount += 1 }, + ) + } + + assertEquals(failures.size, unavailableCount) + // 실행까지 갔다가 막힌 것을 «주소가 잘못됐다» 로 안내하면 거짓이다. + assertEquals(0, rejectedCount) + } +}