Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -216,5 +216,12 @@ dependencies {
annotationProcessor 'androidx.room:room-compiler:2.6.1' // For Java projects, replace with the correct version
ksp 'androidx.room:room-compiler:2.6.1'

// This dependency is downloaded from the Google's Maven repository.
// So, make sure you also include that repository in your project's build.gradle file.
implementation("com.google.android.play:app-update:2.1.0")

// For Kotlin users also import the Kotlin extensions library for Play In-App Update:
implementation("com.google.android.play:app-update-ktx:2.1.0")

implementation(platform("org.jetbrains.kotlin:kotlin-bom:2.1.21"))
}
80 changes: 79 additions & 1 deletion app/src/main/java/org/cis_india/wsreader/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@
package org.cis_india.wsreader

import android.os.Bundle
import android.widget.Toast
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.activity.result.ActivityResult
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.MaterialTheme
Expand All @@ -35,16 +38,29 @@ import org.cis_india.wsreader.ui.theme.AdjustEdgeToEdge
import org.cis_india.wsreader.ui.theme.WikisourceReaderTheme
import dagger.hilt.android.AndroidEntryPoint
import androidx.navigation.NavController
import com.google.android.play.core.appupdate.AppUpdateManager
import com.google.android.play.core.appupdate.AppUpdateManagerFactory
import com.google.android.play.core.appupdate.AppUpdateOptions
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


@AndroidEntryPoint
class MainActivity : AppCompatActivity() {

private lateinit var appUpdateManager: AppUpdateManager
private lateinit var networkObserver: NetworkObserver
lateinit var settingsViewModel: SettingsViewModel
private lateinit var mainViewModel: MainViewModel
private lateinit var navController: NavController

private val installStateUpdatedListener = InstallStateUpdatedListener { state ->
if (state.installStatus() == InstallStatus.DOWNLOADED) {
popupSnackbarForCompleteUpdate()
}
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

Expand All @@ -59,6 +75,11 @@ class MainActivity : AppCompatActivity() {

enableEdgeToEdge() // enable edge to edge for the activity.

appUpdateManager = AppUpdateManagerFactory.create(this)

appUpdateManager.registerListener(installStateUpdatedListener)
checkUpdates()

setContent {
WikisourceReaderTheme(settingsViewModel = settingsViewModel) {
AdjustEdgeToEdge(
Expand All @@ -85,6 +106,63 @@ class MainActivity : AppCompatActivity() {
}
}

private fun checkUpdates(){
val appUpdateInfoTask = appUpdateManager.appUpdateInfo
appUpdateInfoTask.addOnSuccessListener { appUpdateInfo ->
if (appUpdateInfo.updateAvailability() == UpdateAvailability.UPDATE_AVAILABLE
&& appUpdateInfo.isUpdateTypeAllowed(AppUpdateType.FLEXIBLE)) {

appUpdateManager.startUpdateFlowForResult(
// Pass the intent that is returned by 'getAppUpdateInfo()'.
appUpdateInfo,
// an activity result launcher registered via registerForActivityResult
activityResultLauncher,
// Or pass 'AppUpdateType.FLEXIBLE' to newBuilder() for
// flexible updates.
AppUpdateOptions.newBuilder(AppUpdateType.FLEXIBLE).build())
}
}
}


private val activityResultLauncher = registerForActivityResult(
ActivityResultContracts.StartIntentSenderForResult()
){ result : ActivityResult ->

if (result.resultCode != RESULT_OK) {
println("Update flow failed! Result code: " + result.resultCode);
// If the update is canceled or fails,
// you can request to start the update again.
}

}

private fun popupSnackbarForCompleteUpdate() {
// Since you are using Compose, you might prefer calling a function in your
// MainViewModel to show a Compose Snackbar, but here is a standard one:
Toast.makeText(
this,
"An update has just been downloaded.",
Toast.LENGTH_LONG
).show()

appUpdateManager.completeUpdate()
}

override fun onResume() {
super.onResume()
appUpdateManager.appUpdateInfo.addOnSuccessListener { info ->
if (info.installStatus() == InstallStatus.DOWNLOADED) {
popupSnackbarForCompleteUpdate()
}
}
}

override fun onDestroy() {
super.onDestroy()
appUpdateManager.unregisterListener(installStateUpdatedListener)
}

override fun onPause() {
super.onPause()
}
Expand Down