web: kill closed tabs from closed windows
- Commit
b4227330250009eec5cfae505cce216f21b37686- Author
- Vayun Mathur <[email protected]>
- When
- 2026-08-04 17:01 UTC (4 months ago)
- Parents
4b70e8f
Diff
web/src/main/java/com/vayunmathur/web/MainActivity.kt
diff --git a/web/src/main/java/com/vayunmathur/web/MainActivity.kt b/web/src/main/java/com/vayunmathur/web/MainActivity.ktindex d31a39d75b..1e8875b70d 100644--- a/web/src/main/java/com/vayunmathur/web/MainActivity.kt+++ b/web/src/main/java/com/vayunmathur/web/MainActivity.kt@@ -54,6 +54,9 @@ class MainActivity : ComponentActivity() { val windowId = intent?.getStringExtra(EXTRA_WINDOW_ID) ?: WebViewModel.DEFAULT_WINDOW_ID val incognito = intent?.getBooleanExtra(EXTRA_INCOGNITO, false) ?: false + // Reap tab-sets left behind by windows the user has since closed (swiped from Recents).+ pruneClosedWindowTabs()+ lifecycleScope.launch(Dispatchers.IO) { val db = buildDatabase<WebDatabase>(dbName = DB_NAME) val factory = WebViewModelFactory(@@ -116,6 +119,45 @@ class MainActivity : ComponentActivity() { return null } + /**+ * Removes persisted tab-sets whose window is no longer a live task. Each non-incognito window+ * stores its tabs under keys suffixed with its window id (see [WebViewModel]); when the user+ * swipes a window out of Recents those keys would otherwise linger forever.+ */+ private fun pruneClosedWindowTabs() {+ // Must mirror WebViewModel's namespaced key format: "<P_SAVED_TABS>_<windowId>" etc.+ val savedTabsPrefix = "web_saved_tabs_"+ val activeTabPrefix = "web_active_tab_id_"+ lifecycleScope.launch(Dispatchers.IO) {+ try {+ val am = getSystemService(ACTIVITY_SERVICE) as? android.app.ActivityManager ?: return@launch+ val liveWindowIds = am.appTasks.mapNotNull { task ->+ val base = runCatching { task.taskInfo.baseIntent }.getOrNull()+ base?.getStringExtra(EXTRA_WINDOW_ID)+ ?: base?.data?.takeIf { it.scheme == "web-window" }?.host+ }.toSet()++ val sp = getSharedPreferences("web_prefs", MODE_PRIVATE)+ val editor = sp.edit()+ var changed = false+ for (key in sp.all.keys) {+ val windowId = when {+ key.startsWith(savedTabsPrefix) -> key.removePrefix(savedTabsPrefix)+ key.startsWith(activeTabPrefix) -> key.removePrefix(activeTabPrefix)+ else -> null+ } ?: continue+ if (windowId !in liveWindowIds) {+ editor.remove(key)+ changed = true+ }+ }+ if (changed) editor.apply()+ } catch (e: Exception) {+ android.util.Log.e("MainActivity", "pruneClosedWindowTabs failed", e)+ }+ }+ }+ companion object { const val EXTRA_WINDOW_ID = "com.vayunmathur.web.WINDOW_ID" const val EXTRA_INCOGNITO = "com.vayunmathur.web.INCOGNITO"