Modern Apps git

← Modern-Apps.git

appstore: skip packages the OS reserves for its own installer (#601)

Commit
6b946703882fb677fb5c80602b73b659bd69fb66
Author
vayun-mathur <[email protected]>
When
2026-08-26 17:42 UTC (3 months ago)
Parents
c6f8f50

Diff

appstore/src/main/java/com/vayunmathur/appstore/data/InstalledAppsRepository.kt
diff --git a/appstore/src/main/java/com/vayunmathur/appstore/data/InstalledAppsRepository.kt b/appstore/src/main/java/com/vayunmathur/appstore/data/InstalledAppsRepository.ktindex 0e9471699c..372dc690b5 100644--- a/appstore/src/main/java/com/vayunmathur/appstore/data/InstalledAppsRepository.kt+++ b/appstore/src/main/java/com/vayunmathur/appstore/data/InstalledAppsRepository.kt@@ -23,6 +23,16 @@ class InstalledAppsRepository(private val context: Context) {     private val _apps = MutableStateFlow<List<InstalledInfo>>(emptyList())     val apps: StateFlow<List<InstalledInfo>> = _apps.asStateFlow() +    private val _updatable = MutableStateFlow<List<InstalledInfo>>(emptyList())++    /**+     * [apps] minus the packages this device won't let the store install ([RestrictedPackages]).+     *+     * They stay in [apps] — they are installed, and the library should say so — but every+     * update check reads this instead, so the store never offers an update it cannot apply.+     */+    val updatable: StateFlow<List<InstalledInfo>> = _updatable.asStateFlow()+     private val _icons = MutableStateFlow<Map<String, Drawable>>(emptyMap())     val icons: StateFlow<Map<String, Drawable>> = _icons.asStateFlow() @@ -36,8 +46,12 @@ class InstalledAppsRepository(private val context: Context) {             emptyList<ApplicationInfo>()         } -        _apps.value = userApps.mapNotNull { ai -> pm.readInstalledInfo(ai) }+        val apps = userApps.mapNotNull { ai -> pm.readInstalledInfo(ai) }             .sortedBy { it.name.lowercase() }+        _apps.value = apps++        val restricted = RestrictedPackages.forDevice(context)+        _updatable.value = apps.filterNot { it.packageName in restricted }          _icons.value = userApps.mapNotNull { ai ->             try {
appstore/src/main/java/com/vayunmathur/appstore/data/RestrictedPackages.kt
diff --git a/appstore/src/main/java/com/vayunmathur/appstore/data/RestrictedPackages.kt b/appstore/src/main/java/com/vayunmathur/appstore/data/RestrictedPackages.ktnew file mode 100644index 0000000000..42a4f9c21e--- /dev/null+++ b/appstore/src/main/java/com/vayunmathur/appstore/data/RestrictedPackages.kt@@ -0,0 +1,65 @@+package com.vayunmathur.appstore.data++import android.content.Context+import android.content.pm.ApplicationInfo+import androidx.core.content.edit++/**+ * Packages the OS will not let this store install, so it stops offering them updates.+ *+ * GrapheneOS reserves a few Google packages for its own first-party package source. Committing+ * a session for one fails with `INSTALL_FAILED_SESSION_INVALID: Only the first party package+ * source and shell are allowed to install <package>`, which is not something the user can act+ * on — so a pending update for it is a notification that can only ever be dismissed.+ *+ * [KNOWN] is consulted on GrapheneOS only; on stock Android these are ordinary updatable apps.+ * [recordIfRestricted] adds whatever else the installer finds out the hard way, so a+ * restriction that isn't listed here costs one failed install rather than one per update check.+ */+object RestrictedPackages {++    /** Android Auto, which GrapheneOS moved to its first-party source. */+    const val ANDROID_AUTO = "com.google.android.projection.gearhead"++    private val KNOWN = setOf(ANDROID_AUTO)++    /**+     * GrapheneOS's own app store, preinstalled on every build — and the first-party source the+     * restricted installs are reserved for. Present as a system package only on GrapheneOS,+     * which makes it a cheaper and steadier signal than any build fingerprint or property.+     */+    private const val GRAPHENEOS_APPS = "app.grapheneos.apps"++    /** The distinguishing part of the OS's refusal, matched case-insensitively. */+    private const val RESTRICTED_MESSAGE = "first party package source"++    private const val PREFS = "appstore-restricted-packages"+    private const val KEY_LEARNED = "restricted_packages"++    /** Packages to leave out of update checks on this device. */+    fun forDevice(context: Context): Set<String> =+        learned(context) + if (isGrapheneOS(context)) KNOWN else emptySet()++    /**+     * Remember [packageName] when [message] is the OS refusing the install as source-restricted.+     *+     * Anything else — an incompatible signer, no storage — is a failure the user can do+     * something about, and must not stop the store offering the update again.+     */+    fun recordIfRestricted(context: Context, packageName: String?, message: String?) {+        if (packageName.isNullOrEmpty()) return+        if (message?.contains(RESTRICTED_MESSAGE, ignoreCase = true) != true) return+        prefs(context).edit { putStringSet(KEY_LEARNED, learned(context) + packageName) }+    }++    private fun learned(context: Context): Set<String> =+        prefs(context).getStringSet(KEY_LEARNED, emptySet()).orEmpty()++    private fun prefs(context: Context) =+        context.getSharedPreferences(PREFS, Context.MODE_PRIVATE)++    private fun isGrapheneOS(context: Context): Boolean = runCatching {+        val info = context.packageManager.getApplicationInfo(GRAPHENEOS_APPS, 0)+        (info.flags and ApplicationInfo.FLAG_SYSTEM) != 0+    }.getOrDefault(false)+}
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 fc63581bdd..fa9f399b4a 100644--- a/appstore/src/main/java/com/vayunmathur/appstore/data/UpdateCheckWorker.kt+++ b/appstore/src/main/java/com/vayunmathur/appstore/data/UpdateCheckWorker.kt@@ -69,7 +69,7 @@ class UpdateCheckWorker(         installedRepo.refresh()         play.restore() -        val installed = installedRepo.apps.value+        val installed = installedRepo.updatable.value         val fromCatalog = catalog.updatesFor(installed)          // Only Play can answer for packages the offline catalogues have never heard of — except
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 a950640482..9a6d3c2ee2 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@@ -6,6 +6,7 @@ import android.content.Intent import android.content.pm.PackageInstaller import android.util.Log import com.vayunmathur.appstore.R+import com.vayunmathur.appstore.data.RestrictedPackages import com.vayunmathur.library.util.AppMessages  /**@@ -56,6 +57,8 @@ class InstallStatusReceiver : BroadcastReceiver() {             }             else -> {                 Log.w(TAG, "Install failed for $pkg status=$status message=$message")+                // A source-restricted refusal is the OS's final answer, so stop offering it.+                RestrictedPackages.recordIfRestricted(context, pkg, message)                 val reason = when (status) {                     PackageInstaller.STATUS_FAILURE_INCOMPATIBLE ->                         context.getString(R.string.install_failure_incompatible)
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 dad637d46c..224259fa28 100644--- a/appstore/src/main/java/com/vayunmathur/appstore/util/AppStoreViewModel.kt+++ b/appstore/src/main/java/com/vayunmathur/appstore/util/AppStoreViewModel.kt@@ -309,7 +309,7 @@ class AppStoreViewModel(         viewModelScope.launch {             // Recompute catalogue-side updates whenever either half changes. The Play             // half needs a network call and is driven by checkForUpdates() instead.-            combine(catalog.packageIndex, installedRepo.apps) { _, installed -> installed }+            combine(catalog.packageIndex, installedRepo.updatable) { _, installed -> installed }                 .collect { installed -> _catalogUpdates.value = catalog.updatesFor(installed) }         }         viewModelScope.launch {@@ -789,7 +789,7 @@ class AppStoreViewModel(             _statusMessage.value = context.getString(R.string.updates_checking)              installedRepo.refresh()-            _catalogUpdates.value = catalog.updatesFor(installedRepo.apps.value)+            _catalogUpdates.value = catalog.updatesFor(installedRepo.updatable.value)              // Only ask Play about packages neither offline source lists — for the rest the             // catalogue already answered, and Play would just re-answer it over the network.@@ -797,7 +797,7 @@ class AppStoreViewModel(             // of all three, but only GrapheneOS's are the ones this device can use, so no             // update is better than the wrong one until its release metadata is synced.             val index = catalog.packageIndex.value-            val installed = installedRepo.apps.value+            val installed = installedRepo.updatable.value             val playCandidates = installed                 .filter {                     it.packageName !in index &&