|
| 1 | +package com.threegap.bitnagil.presentation.common.playstore |
| 2 | + |
| 3 | +import android.content.ActivityNotFoundException |
| 4 | +import android.content.Context |
| 5 | +import android.content.Intent |
| 6 | +import androidx.activity.ComponentActivity |
| 7 | +import androidx.compose.runtime.Composable |
| 8 | +import androidx.compose.runtime.LaunchedEffect |
| 9 | +import androidx.compose.runtime.getValue |
| 10 | +import androidx.compose.runtime.mutableStateOf |
| 11 | +import androidx.compose.runtime.remember |
| 12 | +import androidx.compose.runtime.setValue |
| 13 | +import androidx.compose.ui.platform.LocalContext |
| 14 | +import androidx.core.net.toUri |
| 15 | +import androidx.lifecycle.lifecycleScope |
| 16 | +import com.google.android.play.core.appupdate.AppUpdateManagerFactory |
| 17 | +import com.google.android.play.core.install.model.AppUpdateType |
| 18 | +import com.google.android.play.core.install.model.UpdateAvailability |
| 19 | +import com.threegap.bitnagil.presentation.BuildConfig |
| 20 | +import kotlinx.coroutines.delay |
| 21 | +import kotlinx.coroutines.launch |
| 22 | +import kotlinx.coroutines.suspendCancellableCoroutine |
| 23 | +import kotlin.coroutines.resume |
| 24 | +import kotlin.system.exitProcess |
| 25 | + |
| 26 | +private const val PACKAGE_NAME = BuildConfig.APPLICATION_ID |
| 27 | +private const val GOOGLE_PLAY_PACKAGE = "com.android.vending" |
| 28 | +private const val APP_EXIT_DELAY = 500L |
| 29 | + |
| 30 | +fun openAppInPlayStore( |
| 31 | + activity: ComponentActivity?, |
| 32 | + shouldFinishApp: Boolean = true, |
| 33 | +) { |
| 34 | + activity?.let { |
| 35 | + val isSuccess = tryOpenPlayStore(it) || tryOpenWebBrowser(it) |
| 36 | + |
| 37 | + if (isSuccess && shouldFinishApp) { |
| 38 | + finishAppWithDelay(it) |
| 39 | + } |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +private fun tryOpenPlayStore(activity: ComponentActivity): Boolean = |
| 44 | + try { |
| 45 | + val intent = createPlayStoreIntent() |
| 46 | + activity.startActivity(intent) |
| 47 | + true |
| 48 | + } catch (e: ActivityNotFoundException) { |
| 49 | + false |
| 50 | + } |
| 51 | + |
| 52 | +private fun tryOpenWebBrowser(activity: ComponentActivity): Boolean = |
| 53 | + try { |
| 54 | + val intent = createWebIntent() |
| 55 | + activity.startActivity(intent) |
| 56 | + true |
| 57 | + } catch (e: Exception) { |
| 58 | + false |
| 59 | + } |
| 60 | + |
| 61 | +private fun createPlayStoreIntent(): Intent = |
| 62 | + Intent( |
| 63 | + Intent.ACTION_VIEW, |
| 64 | + "market://details?id=$PACKAGE_NAME".toUri(), |
| 65 | + ).apply { |
| 66 | + addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) |
| 67 | + setPackage(GOOGLE_PLAY_PACKAGE) |
| 68 | + } |
| 69 | + |
| 70 | +private fun createWebIntent(): Intent = |
| 71 | + Intent( |
| 72 | + Intent.ACTION_VIEW, |
| 73 | + "https://play.google.com/store/apps/details?id=$PACKAGE_NAME".toUri(), |
| 74 | + ).apply { |
| 75 | + addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) |
| 76 | + } |
| 77 | + |
| 78 | +private fun finishAppWithDelay(activity: ComponentActivity) { |
| 79 | + activity.lifecycleScope.launch { |
| 80 | + delay(APP_EXIT_DELAY) |
| 81 | + activity.finishAffinity() |
| 82 | + exitProcess(0) |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +@Composable |
| 87 | +fun updateAvailable(): UpdateAvailableState { |
| 88 | + val context = LocalContext.current |
| 89 | + var isUpdateAvailable by remember { mutableStateOf(UpdateAvailableState.NONE) } |
| 90 | + |
| 91 | + LaunchedEffect(Unit) { |
| 92 | + isUpdateAvailable = context.checkForUpdateAvailability() |
| 93 | + } |
| 94 | + |
| 95 | + return isUpdateAvailable |
| 96 | +} |
| 97 | + |
| 98 | +private suspend fun Context.checkForUpdateAvailability(): UpdateAvailableState = suspendCancellableCoroutine { continuation -> |
| 99 | + val appUpdateManager = AppUpdateManagerFactory.create(this) |
| 100 | + val appUpdateInfoTask = appUpdateManager.appUpdateInfo |
| 101 | + |
| 102 | + appUpdateInfoTask.addOnSuccessListener { appUpdateInfo -> |
| 103 | + if (!continuation.isActive) return@addOnSuccessListener |
| 104 | + |
| 105 | + val isAvailable = appUpdateInfo.updateAvailability() == UpdateAvailability.UPDATE_AVAILABLE |
| 106 | + |
| 107 | + val isAllowed = appUpdateInfo.isUpdateTypeAllowed(AppUpdateType.IMMEDIATE) || |
| 108 | + appUpdateInfo.isUpdateTypeAllowed(AppUpdateType.FLEXIBLE) |
| 109 | + |
| 110 | + val state = if (isAvailable && isAllowed) UpdateAvailableState.NEED_UPDATE else UpdateAvailableState.LATEST |
| 111 | + continuation.resume(state) |
| 112 | + } |
| 113 | + |
| 114 | + appUpdateInfoTask.addOnFailureListener { |
| 115 | + if (continuation.isActive) { |
| 116 | + continuation.resume(UpdateAvailableState.NONE) |
| 117 | + } |
| 118 | + } |
| 119 | +} |
0 commit comments