Skip to content

Commit 265703e

Browse files
committed
feat: openAppInPlayStore 유틸 메소드 구현
1 parent 86cd085 commit 265703e

File tree

1 file changed

+70
-0
lines changed
  • presentation/src/main/java/com/threegap/bitnagil/presentation/splash/util

1 file changed

+70
-0
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package com.threegap.bitnagil.presentation.splash.util
2+
3+
import android.content.ActivityNotFoundException
4+
import android.content.Intent
5+
import androidx.activity.ComponentActivity
6+
import androidx.core.net.toUri
7+
import androidx.lifecycle.lifecycleScope
8+
import kotlinx.coroutines.delay
9+
import kotlinx.coroutines.launch
10+
import kotlin.system.exitProcess
11+
12+
private const val PACKAGE_NAME = "com.threegap.bitnagil"
13+
private const val GOOGLE_PLAY_PACKAGE = "com.android.vending"
14+
private const val APP_EXIT_DELAY = 500L
15+
16+
fun openAppInPlayStore(
17+
activity: ComponentActivity?,
18+
shouldFinishApp: Boolean = true,
19+
) {
20+
activity?.let {
21+
val isSuccess = tryOpenPlayStore(it) || tryOpenWebBrowser(it)
22+
23+
if (isSuccess && shouldFinishApp) {
24+
finishAppWithDelay(it)
25+
}
26+
}
27+
}
28+
29+
private fun tryOpenPlayStore(activity: ComponentActivity): Boolean =
30+
try {
31+
val intent = createPlayStoreIntent()
32+
activity.startActivity(intent)
33+
true
34+
} catch (e: ActivityNotFoundException) {
35+
false
36+
}
37+
38+
private fun tryOpenWebBrowser(activity: ComponentActivity): Boolean =
39+
try {
40+
val intent = createWebIntent()
41+
activity.startActivity(intent)
42+
true
43+
} catch (e: Exception) {
44+
false
45+
}
46+
47+
private fun createPlayStoreIntent(): Intent =
48+
Intent(
49+
Intent.ACTION_VIEW,
50+
"market://details?id=$PACKAGE_NAME".toUri(),
51+
).apply {
52+
addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
53+
setPackage(GOOGLE_PLAY_PACKAGE)
54+
}
55+
56+
private fun createWebIntent(): Intent =
57+
Intent(
58+
Intent.ACTION_VIEW,
59+
"https://play.google.com/store/apps/details?id=$PACKAGE_NAME".toUri(),
60+
).apply {
61+
addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
62+
}
63+
64+
private fun finishAppWithDelay(activity: ComponentActivity) {
65+
activity.lifecycleScope.launch {
66+
delay(APP_EXIT_DELAY)
67+
activity.finishAffinity()
68+
exitProcess(0)
69+
}
70+
}

0 commit comments

Comments
 (0)