appstore: refresh app page on install completion (#714, #715)
- Commit
24ab5fc7d7baba3280cdaf0e9aed8213a791641a- Author
- vayun-mathur <[email protected]>
- When
- 2026-09-15 21:32 UTC (2 months ago)
- Parents
4610844
Diff
appstore/src/main/java/com/vayunmathur/appstore/data/UpdateCheckWorker.kt
diff --git a/appstore/src/main/java/com/vayunmathur/appstore/data/UpdateCheckWorker.kt b/appstore/src/main/java/com/vayunmathur/appstore/data/UpdateCheckWorker.ktindex 5bcab8bb01..1ac2e23ded 100644--- a/appstore/src/main/java/com/vayunmathur/appstore/data/UpdateCheckWorker.kt+++ b/appstore/src/main/java/com/vayunmathur/appstore/data/UpdateCheckWorker.kt@@ -145,7 +145,7 @@ class UpdateCheckWorker( if (autoInstall && updates.isNotEmpty()) { val eligible = updates.filter { canSilentlyUpdate(it.packageName) } if (eligible.isNotEmpty()) {- autoInstall(eligible, db, play, accrescent, grapheneOS)+ autoInstall(eligible, db, play, accrescent, grapheneOS, scope) installedRepo.refresh() } // Only nag about the updates we could not apply on our own.@@ -245,6 +245,7 @@ class UpdateCheckWorker( play: PlayRepository, accrescent: AccrescentRepository, grapheneOS: GrapheneOSRepository,+ scope: CoroutineScope, ) { runCatching { setForeground(installingForegroundInfo(apps.size)) } val installer = InstallCoordinator(@@ -253,6 +254,7 @@ class UpdateCheckWorker( play = play, accrescent = accrescent, grapheneOS = grapheneOS,+ scope = scope, ownSigningCertificates = { ApkCertificates.selfSigners(context) }, ) var installed = 0
appstore/src/main/java/com/vayunmathur/appstore/data/installer/InstallCoordinator.kt
diff --git a/appstore/src/main/java/com/vayunmathur/appstore/data/installer/InstallCoordinator.kt b/appstore/src/main/java/com/vayunmathur/appstore/data/installer/InstallCoordinator.ktindex db9bdceaca..ac92364f8a 100644--- a/appstore/src/main/java/com/vayunmathur/appstore/data/installer/InstallCoordinator.kt+++ b/appstore/src/main/java/com/vayunmathur/appstore/data/installer/InstallCoordinator.kt@@ -17,10 +17,12 @@ import com.vayunmathur.appstore.data.security.InstallRequirement import com.vayunmathur.appstore.data.security.VerificationResult import com.vayunmathur.library.network.NetworkClient import kotlinx.coroutines.CancellationException+import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.StateFlow import kotlinx.coroutines.flow.asStateFlow+import kotlinx.coroutines.launch import kotlinx.coroutines.withContext import java.io.File import java.io.FilterInputStream@@ -61,6 +63,7 @@ class InstallCoordinator( private val play: PlayRepository, private val accrescent: AccrescentRepository, private val grapheneOS: GrapheneOSRepository,+ scope: CoroutineScope, private val ownSigningCertificates: () -> Set<String>, ) { private val sessionInstaller = SessionInstaller(context)@@ -73,6 +76,20 @@ class InstallCoordinator( private val _verification = MutableStateFlow<Map<String, VerificationResult>>(emptyMap()) val verification: StateFlow<Map<String, VerificationResult>> = _verification.asStateFlow() + init {+ // The session is committed on a background thread and the OS reports the real+ // outcome later, at InstallStatusReceiver. Until it arrives the row sits at+ // InstallStage.Installing; this ends that stage once the install has actually+ // finished (or was aborted/failed), rather than leaving the button stuck.+ scope.launch {+ InstallEvents.results.collect { result ->+ if (_stages.value[result.packageName] is InstallStage.Installing) {+ clear(result.packageName)+ }+ }+ }+ }+ /** True while anything is downloading or installing. */ fun isBusy(packageName: String): Boolean = _stages.value[packageName].let { it != null && it !is InstallStage.Failed }@@ -96,7 +113,10 @@ class InstallCoordinator( else -> installFromUrl(app) }.also { outcome -> if (outcome.started) {- clear(app.packageName)+ // The bytes are committed but the OS is still writing them in; hold the+ // row at Installing until InstallStatusReceiver reports the real outcome+ // (a large split install can take several seconds past this point).+ stage(app.packageName, InstallStage.Installing) } else { stage(app.packageName, InstallStage.Failed(outcome.verification.shortReason())) }
appstore/src/main/java/com/vayunmathur/appstore/data/installer/InstallStatusReceiver.kt
diff --git a/appstore/src/main/java/com/vayunmathur/appstore/data/installer/InstallStatusReceiver.kt b/appstore/src/main/java/com/vayunmathur/appstore/data/installer/InstallStatusReceiver.ktindex 696ad9ed4c..9f6c40286d 100644--- a/appstore/src/main/java/com/vayunmathur/appstore/data/installer/InstallStatusReceiver.kt+++ b/appstore/src/main/java/com/vayunmathur/appstore/data/installer/InstallStatusReceiver.kt@@ -8,6 +8,10 @@ import android.util.Log import com.vayunmathur.appstore.R import com.vayunmathur.appstore.data.RestrictedPackages import com.vayunmathur.library.util.AppMessages+import kotlinx.coroutines.channels.BufferOverflow+import kotlinx.coroutines.flow.MutableSharedFlow+import kotlinx.coroutines.flow.SharedFlow+import kotlinx.coroutines.flow.asSharedFlow /** * Receives PackageInstaller commit callbacks.@@ -50,13 +54,16 @@ class InstallStatusReceiver : BroadcastReceiver() { } PackageInstaller.STATUS_SUCCESS -> { Log.i(TAG, "Install success for $pkg")+ pkg?.let { InstallEvents.publish(InstallResult(it, success = true)) } } // The user backing out of the system prompt is not a failure worth reporting back. PackageInstaller.STATUS_FAILURE_ABORTED -> { Log.i(TAG, "Install aborted for $pkg")+ pkg?.let { InstallEvents.publish(InstallResult(it, success = false)) } } else -> { Log.w(TAG, "Install failed for $pkg status=$status message=$message")+ pkg?.let { InstallEvents.publish(InstallResult(it, success = false)) } // A source-restricted refusal is the OS's final answer, so stop offering it. RestrictedPackages.recordIfRestricted(context, pkg, message) val reason = when (status) {@@ -120,3 +127,30 @@ object InstallFailureBatch { return true } }++/** Terminal outcome of a committed PackageInstaller session, per package. */+data class InstallResult(val packageName: String, val success: Boolean)++/**+ * How the OS's install outcome reaches the rest of the app.+ *+ * [SessionInstaller.commit] only hands the session over; the real install finishes later+ * and its outcome arrives at the manifest-declared [InstallStatusReceiver], which the+ * system instantiates fresh each time and so cannot hold a reference to the ViewModel or+ * the [InstallCoordinator]. This process-wide flow bridges that gap: the receiver publishes+ * each terminal result and whoever cares — the coordinator, to end its "Installing" stage,+ * and the ViewModel, to re-read the device — collects it. Replay is off (a result only+ * matters to collectors already listening) but a small buffer keeps a burst from an+ * "update all" run from being dropped.+ */+object InstallEvents {+ private val _results = MutableSharedFlow<InstallResult>(+ extraBufferCapacity = 16,+ onBufferOverflow = BufferOverflow.DROP_OLDEST,+ )+ val results: SharedFlow<InstallResult> = _results.asSharedFlow()++ fun publish(result: InstallResult) {+ _results.tryEmit(result)+ }+}
appstore/src/main/java/com/vayunmathur/appstore/ui/SourcesPage.kt
diff --git a/appstore/src/main/java/com/vayunmathur/appstore/ui/SourcesPage.kt b/appstore/src/main/java/com/vayunmathur/appstore/ui/SourcesPage.ktindex 9549fb57b7..51d015902b 100644--- a/appstore/src/main/java/com/vayunmathur/appstore/ui/SourcesPage.kt+++ b/appstore/src/main/java/com/vayunmathur/appstore/ui/SourcesPage.kt@@ -23,8 +23,6 @@ import com.vayunmathur.appstore.data.ModernAppsRepo import com.vayunmathur.appstore.data.accrescent.AccrescentRepo import com.vayunmathur.appstore.data.security.ApkCertificates import com.vayunmathur.appstore.util.AppStoreViewModel-import com.vayunmathur.appstore.util.setAutoInstallUpdates-import com.vayunmathur.appstore.util.setSourceEnabled import com.vayunmathur.appstore.util.syncSources import com.vayunmathur.library.ui.AppBarAlignment import com.vayunmathur.library.ui.Button
appstore/src/main/java/com/vayunmathur/appstore/util/AppStoreActionOps.kt
diff --git a/appstore/src/main/java/com/vayunmathur/appstore/util/AppStoreActionOps.kt b/appstore/src/main/java/com/vayunmathur/appstore/util/AppStoreActionOps.ktindex 96f9225f40..4bea2ab22b 100644--- a/appstore/src/main/java/com/vayunmathur/appstore/util/AppStoreActionOps.kt+++ b/appstore/src/main/java/com/vayunmathur/appstore/util/AppStoreActionOps.kt@@ -1,5 +1,6 @@ package com.vayunmathur.appstore.util +import com.vayunmathur.appstore.util.AppStoreViewModel.Companion.INSTALL_SETTLE_MS import android.content.Intent import androidx.lifecycle.viewModelScope import com.vayunmathur.appstore.R
appstore/src/main/java/com/vayunmathur/appstore/util/AppStoreHomeOps.kt
diff --git a/appstore/src/main/java/com/vayunmathur/appstore/util/AppStoreHomeOps.kt b/appstore/src/main/java/com/vayunmathur/appstore/util/AppStoreHomeOps.ktindex f53a667f44..f5a58cdfa8 100644--- a/appstore/src/main/java/com/vayunmathur/appstore/util/AppStoreHomeOps.kt+++ b/appstore/src/main/java/com/vayunmathur/appstore/util/AppStoreHomeOps.kt@@ -1,5 +1,8 @@ package com.vayunmathur.appstore.util +import com.vayunmathur.appstore.util.AppStoreViewModel.Companion.CAROUSEL_LIMIT+import com.vayunmathur.appstore.util.AppStoreViewModel.Companion.PLAY_CLUSTER_LIMIT+import com.vayunmathur.appstore.util.AppStoreViewModel.Companion.RECENT_LIMIT import androidx.lifecycle.viewModelScope import com.vayunmathur.appstore.R import com.vayunmathur.appstore.data.AppSource
appstore/src/main/java/com/vayunmathur/appstore/util/AppStoreSearchOps.kt
diff --git a/appstore/src/main/java/com/vayunmathur/appstore/util/AppStoreSearchOps.kt b/appstore/src/main/java/com/vayunmathur/appstore/util/AppStoreSearchOps.ktindex b468bb8350..6ae214397c 100644--- a/appstore/src/main/java/com/vayunmathur/appstore/util/AppStoreSearchOps.kt+++ b/appstore/src/main/java/com/vayunmathur/appstore/util/AppStoreSearchOps.kt@@ -1,5 +1,6 @@ package com.vayunmathur.appstore.util +import com.vayunmathur.appstore.util.AppStoreViewModel.Companion.SEARCH_DEBOUNCE_MS import androidx.lifecycle.viewModelScope import com.vayunmathur.appstore.data.AppSource import com.vayunmathur.appstore.data.UnifiedApp
appstore/src/main/java/com/vayunmathur/appstore/util/AppStoreUpdateOps.kt
diff --git a/appstore/src/main/java/com/vayunmathur/appstore/util/AppStoreUpdateOps.kt b/appstore/src/main/java/com/vayunmathur/appstore/util/AppStoreUpdateOps.ktindex dcbec74a3b..4d29b8c7fa 100644--- a/appstore/src/main/java/com/vayunmathur/appstore/util/AppStoreUpdateOps.kt+++ b/appstore/src/main/java/com/vayunmathur/appstore/util/AppStoreUpdateOps.kt@@ -1,5 +1,8 @@ package com.vayunmathur.appstore.util +import com.vayunmathur.appstore.util.AppStoreViewModel.Companion.CAROUSEL_LIMIT+import com.vayunmathur.appstore.util.AppStoreViewModel.Companion.INSTALL_SETTLE_MS+import com.vayunmathur.appstore.util.AppStoreViewModel.Companion.REPEATED_FAILURE_LIMIT import androidx.lifecycle.viewModelScope import com.vayunmathur.appstore.R import com.vayunmathur.appstore.data.AppSource@@ -109,7 +112,7 @@ internal fun AppStoreViewModel.updateAllImpl() { repeats = 0 continue }- val reason = outcome.verification.reason()+ val reason = reason(outcome.verification) failures += reason repeats = if (reason == previousReason) repeats + 1 else 1 previousReason = reason
appstore/src/main/java/com/vayunmathur/appstore/util/AppStoreViewModel.kt
diff --git a/appstore/src/main/java/com/vayunmathur/appstore/util/AppStoreViewModel.kt b/appstore/src/main/java/com/vayunmathur/appstore/util/AppStoreViewModel.ktindex 0e98fbe13a..a67b8046c3 100644--- a/appstore/src/main/java/com/vayunmathur/appstore/util/AppStoreViewModel.kt+++ b/appstore/src/main/java/com/vayunmathur/appstore/util/AppStoreViewModel.kt@@ -25,6 +25,7 @@ import com.vayunmathur.appstore.data.accrescent.AccrescentRepository import com.vayunmathur.appstore.data.grapheneos.GrapheneOSRepository import com.vayunmathur.appstore.data.grapheneos.toUnifiedApp import com.vayunmathur.appstore.data.installer.InstallCoordinator+import com.vayunmathur.appstore.data.installer.InstallEvents import com.vayunmathur.appstore.data.installer.InstallFailureBatch import com.vayunmathur.appstore.data.installer.InstallStage import com.vayunmathur.appstore.data.play.PlayAuthState@@ -66,7 +67,7 @@ class AppStoreViewModel( internal val installedRepo = InstalledAppsRepository(context) internal val settings = SettingsRepository(context, viewModelScope) internal val installer =- InstallCoordinator(context, db, play, accrescent, grapheneOS) { ownSigningCertificates }+ InstallCoordinator(context, db, play, accrescent, grapheneOS, viewModelScope) { ownSigningCertificates } /** Off-by-default: the periodic check may also download and install updates unattended. */ val autoInstallUpdates: StateFlow<Boolean> = settings.autoInstallUpdates@@ -357,6 +358,17 @@ class AppStoreViewModel( viewModelScope.launch { _categories.value = catalog.categories() }+ viewModelScope.launch {+ // The OS install finishes asynchronously, well after commit for a large app.+ // Re-read the device when it lands so the row switches to Open/Uninstall and+ // picks up the launcher icon, instead of waiting for the next onResume.+ InstallEvents.results.collect { result ->+ if (result.success) {+ installedRepo.refresh()+ refreshPlayInstalledPackages()+ }+ }+ } viewModelScope.launch { // Say so when Play is unreachable. Without this the store just quietly shows // fewer results, which looks like the search finding nothing.@@ -541,14 +553,14 @@ class AppStoreViewModel( // --- Helpers -------------------------------------------------------------------------- - private fun startActivity(intent: Intent): Boolean = try {+ internal fun startActivity(intent: Intent): Boolean = try { context.startActivity(intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)) true } catch (_: Exception) { false } - private fun String.toUri(): Uri = Uri.parse(this)+ internal fun String.toUri(): Uri = Uri.parse(this) /** An installed package as a listing, for the library screen. */ private fun InstalledInfo.toUnifiedApp(source: AppSource) = UnifiedApp(