Skip to content

Commit

Permalink
feat: 인앱 업데이트 프롬프트 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
Taewan-P committed Feb 26, 2024
1 parent 94c359d commit 6f792d6
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
43 changes: 43 additions & 0 deletions android/app/src/main/java/app/priceguard/ui/home/HomeActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import android.content.pm.PackageManager
import android.os.Build
import android.os.Bundle
import android.util.Log
import android.widget.Toast
import androidx.activity.result.ActivityResult
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
Expand All @@ -22,6 +24,10 @@ import app.priceguard.ui.util.openNotificationSettings
import com.google.android.gms.common.ConnectionResult
import com.google.android.gms.common.GoogleApiAvailability
import com.google.android.material.snackbar.Snackbar
import com.google.android.play.core.appupdate.AppUpdateManagerFactory
import com.google.android.play.core.appupdate.AppUpdateOptions
import com.google.android.play.core.install.model.AppUpdateType
import com.google.android.play.core.install.model.UpdateAvailability
import dagger.hilt.android.AndroidEntryPoint
import java.util.concurrent.TimeUnit

Expand All @@ -30,6 +36,8 @@ class HomeActivity : AppCompatActivity() {

private lateinit var binding: ActivityHomeBinding
private lateinit var snackbar: Snackbar
private val appUpdateManager = AppUpdateManagerFactory.create(this)
private val updateType = AppUpdateType.IMMEDIATE

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Expand All @@ -47,6 +55,7 @@ class HomeActivity : AppCompatActivity() {
override fun onResume() {
super.onResume()
checkForGooglePlayServices()
checkAppUpdates()

if (ContextCompat.checkSelfPermission(
this,
Expand Down Expand Up @@ -128,6 +137,40 @@ class HomeActivity : AppCompatActivity() {
}
}

private fun checkAppUpdates() {
val appUpdateInfoTask = appUpdateManager.appUpdateInfo
val activityResultLauncher = registerForActivityResult(ActivityResultContracts.StartIntentSenderForResult()) { result: ActivityResult ->
when (result.resultCode) {
RESULT_CANCELED -> {
Toast.makeText(this, getString(R.string.update_cancel_warning), Toast.LENGTH_LONG).show()
}
com.google.android.play.core.install.model.ActivityResult.RESULT_IN_APP_UPDATE_FAILED -> {
Toast.makeText(this, getString(R.string.update_failed), Toast.LENGTH_LONG).show()
}
}
}

appUpdateInfoTask.addOnSuccessListener { info ->
if (info.updateAvailability() == UpdateAvailability.UPDATE_AVAILABLE &&
info.isUpdateTypeAllowed(updateType)
) {
appUpdateManager.startUpdateFlowForResult(
info,
activityResultLauncher,
AppUpdateOptions.newBuilder(updateType).build()
)
}

if (info.updateAvailability() == UpdateAvailability.DEVELOPER_TRIGGERED_UPDATE_IN_PROGRESS) {
appUpdateManager.startUpdateFlowForResult(
info,
activityResultLauncher,
AppUpdateOptions.newBuilder(updateType).build()
)
}
}
}

private fun showNotificationOffSnackbar() {
if (snackbar.isShown) return
snackbar.show()
Expand Down
2 changes: 2 additions & 0 deletions android/app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -152,4 +152,6 @@
<string name="recommended_product_no_product">추천 상품이 준비중입니다.</string>
<string name="sold_out">품절</string>
<string name="error_maximum_count_exceeded">등록 가능한 상품 개수를 초과하였습니다.</string>
<string name="update_cancel_warning">업데이트 미적용시 앱 사용에 지장이 생길 수 있습니다.</string>
<string name="update_failed">업데이트에 실패하였습니다. 다시 시도해 주세요.</string>
</resources>

0 comments on commit 6f792d6

Please sign in to comment.