Last active
October 28, 2024 14:32
-
-
Save saikiran91/6788ad4d00edca30dad3f51aa47a4c5c to your computer and use it in GitHub Desktop.
Android officially announced the in-app updates. https://developer.android.com/guide/app-bundle/in-app-updates. This is the code sample for handling both IMMEDIATE and FLEXIBLE updates in a single activity; Kotlin way.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.app.Activity | |
import android.content.Intent | |
import android.content.IntentSender | |
import android.os.Bundle | |
import android.widget.Toast | |
import androidx.appcompat.app.AppCompatActivity | |
import androidx.core.content.ContextCompat | |
import com.google.android.material.snackbar.Snackbar | |
import com.google.android.play.core.appupdate.AppUpdateManager | |
import com.google.android.play.core.appupdate.AppUpdateManagerFactory | |
import com.google.android.play.core.install.InstallState | |
import com.google.android.play.core.install.InstallStateUpdatedListener | |
import com.google.android.play.core.install.model.AppUpdateType | |
import com.google.android.play.core.install.model.InstallStatus | |
import com.google.android.play.core.install.model.UpdateAvailability | |
import timber.log.Timber | |
class BaseUpdateCheckActivity : AppCompatActivity() { | |
private val appUpdateManager: AppUpdateManager by lazy { AppUpdateManagerFactory.create(this) } | |
private val appUpdatedListener: InstallStateUpdatedListener by lazy { | |
object : InstallStateUpdatedListener { | |
override fun onStateUpdate(installState: InstallState) { | |
when { | |
installState.installStatus() == InstallStatus.DOWNLOADED -> popupSnackbarForCompleteUpdate() | |
installState.installStatus() == InstallStatus.INSTALLED -> appUpdateManager.unregisterListener(this) | |
else -> Timber.d("InstallStateUpdatedListener: state: %s", installState.installStatus()) | |
} | |
} | |
} | |
} | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContentView(R.layout.main_ad_view) | |
checkForAppUpdate() | |
} | |
private fun checkForAppUpdate() { | |
// Returns an intent object that you use to check for an update. | |
val appUpdateInfoTask = appUpdateManager.appUpdateInfo | |
// Checks that the platform will allow the specified type of update. | |
appUpdateInfoTask.addOnSuccessListener { appUpdateInfo -> | |
if (appUpdateInfo.updateAvailability() == UpdateAvailability.UPDATE_AVAILABLE) { | |
// Request the update. | |
try { | |
val installType = when { | |
appUpdateInfo.isUpdateTypeAllowed(AppUpdateType.FLEXIBLE) -> AppUpdateType.FLEXIBLE | |
appUpdateInfo.isUpdateTypeAllowed(AppUpdateType.IMMEDIATE) -> AppUpdateType.IMMEDIATE | |
else -> null | |
} | |
if (installType == AppUpdateType.FLEXIBLE) appUpdateManager.registerListener(appUpdatedListener) | |
appUpdateManager.startUpdateFlowForResult( | |
appUpdateInfo, | |
installType!!, | |
this, | |
APP_UPDATE_REQUEST_CODE) | |
} catch (e: IntentSender.SendIntentException) { | |
e.printStackTrace() | |
} | |
} | |
} | |
} | |
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { | |
super.onActivityResult(requestCode, resultCode, data) | |
if (requestCode == APP_UPDATE_REQUEST_CODE) { | |
if (resultCode != Activity.RESULT_OK) { | |
Toast.makeText(this, | |
"App Update failed, please try again on the next app launch.", | |
Toast.LENGTH_SHORT) | |
.show() | |
} | |
} | |
} | |
private fun popupSnackbarForCompleteUpdate() { | |
val snackbar = Snackbar.make( | |
findViewById(R.id.drawer_layout), | |
"An update has just been downloaded.", | |
Snackbar.LENGTH_INDEFINITE) | |
snackbar.setAction("RESTART") { appUpdateManager.completeUpdate() } | |
snackbar.setActionTextColor(ContextCompat.getColor(this, R.color.accent)) | |
snackbar.show() | |
} | |
override fun onResume() { | |
super.onResume() | |
appUpdateManager | |
.appUpdateInfo | |
.addOnSuccessListener { appUpdateInfo -> | |
// If the update is downloaded but not installed, | |
// notify the user to complete the update. | |
if (appUpdateInfo.installStatus() == InstallStatus.DOWNLOADED) { | |
popupSnackbarForCompleteUpdate() | |
} | |
//Check if Immediate update is required | |
try { | |
if (appUpdateInfo.updateAvailability() == UpdateAvailability.DEVELOPER_TRIGGERED_UPDATE_IN_PROGRESS) { | |
// If an in-app update is already running, resume the update. | |
appUpdateManager.startUpdateFlowForResult( | |
appUpdateInfo, | |
AppUpdateType.IMMEDIATE, | |
this, | |
APP_UPDATE_REQUEST_CODE) | |
} | |
} catch (e: IntentSender.SendIntentException) { | |
e.printStackTrace() | |
} | |
} | |
} | |
companion object { | |
private const val APP_UPDATE_REQUEST_CODE = 1991 | |
} | |
} |
I am trying this piece of code in a fragment, I am not getting an update screen. Can you please help on this, Thanks!! Where activity references are present, there i am using this.activity.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Remove that import and import the R generated for your project