mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-11-21 13:55:58 +00:00
fix(stores): moving through the side menu no longer freezes the launcher (#2105)
This commit is contained in:
1 parent
e1c2b0e340
commit
26c562c174
3 files changed
+155
-20
No files matched your search
@@ -74,29 +74,36 @@ object StoresState {
|
||||
|
||||
fun register(backend: StoreBackend) { backends[backend.store] = backend }
|
||||
|
||||
private val refreshRequests = java.util.concurrent.atomic.AtomicInteger()
|
||||
|
||||
fun refresh(context: Context) {
|
||||
val app = context.applicationContext
|
||||
com.droiddeck.launcher.stores.download.SavedQueue.restore(app)
|
||||
background("stores-refresh") {
|
||||
runCatching { com.droiddeck.launcher.stores.battlenet.BattleNetClient.syncAccount(app) }
|
||||
val found = Store.entries.mapNotNull { store -> StoreAccounts.displayName(app, store)?.let { store to it } }
|
||||
val onDisk = StoreInstallRoot.gameFolders(app).mapNotNull { folder ->
|
||||
StoreGameSidecar.read(folder)?.takeIf { it.isInstalled || it.state == StoreGameSidecar.STATE_UPDATING }?.let { InstalledStoreGame(it, folder) }
|
||||
} + runCatching { com.droiddeck.launcher.stores.battlenet.BattleNetBackend.installedGames(app) }.getOrDefault(emptyList())
|
||||
val partial = StoreInstallRoot.unfinished(app)
|
||||
val version = if (StoresNative.available) StoresNative.version ?: "" else ""
|
||||
main.post {
|
||||
androidx.compose.runtime.snapshots.Snapshot.withMutableSnapshot {
|
||||
val restored = found.toMap()
|
||||
accounts.keys.toList().filter { it !in restored }.forEach { accounts.remove(it); libraryAsked.remove(it) }
|
||||
accounts.putAll(restored)
|
||||
installed = onDisk
|
||||
unfinished = partial
|
||||
engine = version
|
||||
accountsLoaded = true
|
||||
if (refreshRequests.getAndIncrement() > 0) return
|
||||
var seen = 0
|
||||
background("stores-refresh", onFailure = { if (refreshRequests.getAndSet(0) > seen) main.post { refresh(app) } }) {
|
||||
do {
|
||||
seen = refreshRequests.get()
|
||||
runCatching { com.droiddeck.launcher.stores.battlenet.BattleNetClient.syncAccount(app) }
|
||||
val found = Store.entries.mapNotNull { store -> StoreAccounts.displayName(app, store)?.let { store to it } }
|
||||
val onDisk = StoreInstallRoot.gameFolders(app).mapNotNull { folder ->
|
||||
StoreGameSidecar.read(folder)?.takeIf { it.isInstalled || it.state == StoreGameSidecar.STATE_UPDATING }?.let { InstalledStoreGame(it, folder) }
|
||||
} + runCatching { com.droiddeck.launcher.stores.battlenet.BattleNetBackend.installedGames(app) }.getOrDefault(emptyList())
|
||||
val partial = StoreInstallRoot.unfinished(app)
|
||||
val version = if (StoresNative.available) StoresNative.version ?: "" else ""
|
||||
main.post {
|
||||
androidx.compose.runtime.snapshots.Snapshot.withMutableSnapshot {
|
||||
val restored = found.toMap()
|
||||
accounts.keys.toList().filter { it !in restored }.forEach { accounts.remove(it); libraryAsked.remove(it) }
|
||||
accounts.putAll(restored)
|
||||
installed = onDisk
|
||||
unfinished = partial
|
||||
engine = version
|
||||
accountsLoaded = true
|
||||
}
|
||||
com.droiddeck.launcher.stores.gog.GogMaintenance.checkInstalled(app, onDisk)
|
||||
}
|
||||
com.droiddeck.launcher.stores.gog.GogMaintenance.checkInstalled(app, onDisk)
|
||||
}
|
||||
} while (!refreshRequests.compareAndSet(seen, 0))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -206,6 +213,7 @@ object StoresState {
|
||||
main.post {
|
||||
if (store == Store.XBOX && StoreAccounts.signedInAs(app, store) != null) return@post
|
||||
accounts.remove(store); library.remove(store); status.remove(store); problems.remove(store); libraryAsked.remove(store)
|
||||
if (store == Store.GOG) com.droiddeck.launcher.stores.gog.GogMaintenance.reset()
|
||||
logLine("${store.label}: signed out")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -41,7 +41,7 @@ internal object GogMaintenance {
|
||||
}
|
||||
|
||||
fun checkInstalled(context: Context, games: List<InstalledStoreGame>) {
|
||||
val user = StoreAccounts.displayName(context, Store.GOG).orEmpty()
|
||||
val user = StoresState.accounts[Store.GOG].orEmpty()
|
||||
if (user != account) { reset(); account = user }
|
||||
if (user.isBlank()) return
|
||||
games.filter { it.sidecar.store == Store.GOG && it.sidecar.isInstalled }.forEach { check(context, it) }
|
||||
|
||||
@@ -0,0 +1,127 @@
|
||||
package com.droiddeck.launcher.stores
|
||||
|
||||
import android.app.Application
|
||||
import com.droiddeck.launcher.stores.gog.GogMaintenance
|
||||
import org.json.JSONObject
|
||||
import org.junit.After
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertNull
|
||||
import org.junit.Assert.assertTrue
|
||||
import org.junit.Before
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.robolectric.RobolectricTestRunner
|
||||
import org.robolectric.RuntimeEnvironment
|
||||
import org.robolectric.annotation.Config
|
||||
import org.robolectric.shadows.ShadowLooper
|
||||
import java.util.concurrent.CountDownLatch
|
||||
import java.util.concurrent.TimeUnit
|
||||
import java.util.concurrent.atomic.AtomicInteger
|
||||
import javax.crypto.SecretKey
|
||||
import javax.crypto.spec.SecretKeySpec
|
||||
import kotlin.concurrent.thread
|
||||
|
||||
@RunWith(RobolectricTestRunner::class)
|
||||
@Config(sdk = [33], application = Application::class)
|
||||
class StoresStateRefreshTest {
|
||||
private val app get() = RuntimeEnvironment.getApplication()
|
||||
private val secret: SecretKey = SecretKeySpec(ByteArray(32), "AES")
|
||||
private val plain = CredentialCipher.KeyProvider { secret }
|
||||
|
||||
private fun opening(onOpen: () -> Unit) = object : CredentialCipher.KeyProvider {
|
||||
override fun key() = secret
|
||||
override fun existing(): SecretKey { onOpen(); return secret }
|
||||
}
|
||||
|
||||
private fun refreshRunning() = Thread.getAllStackTraces().keys.any { it.name == "stores-refresh" }
|
||||
|
||||
private fun awaitRefreshDone() {
|
||||
val deadline = System.nanoTime() + TimeUnit.SECONDS.toNanos(10)
|
||||
while (refreshRunning() && System.nanoTime() < deadline) Thread.sleep(10)
|
||||
assertTrue("refresh did not finish", !refreshRunning())
|
||||
ShadowLooper.idleMainLooper()
|
||||
}
|
||||
|
||||
@Before fun setUp() {
|
||||
StoreAccounts.keys = plain
|
||||
StoreAccounts.keystoreFailed = false
|
||||
StoreAccounts.write(app, Store.EPIC, JSONObject().put("access_token", "token").put("display_name", "Someone"))
|
||||
StoresState.accounts.clear()
|
||||
}
|
||||
|
||||
@After fun tearDown() {
|
||||
StoreAccounts.keys = plain
|
||||
for (store in Store.entries) StoreAccounts.clear(app, store)
|
||||
StoreAccounts.keys = CredentialCipher.Keystore
|
||||
}
|
||||
|
||||
@Test fun gogCheckDoesNotWaitForTheCredentialLock() {
|
||||
val held = CountDownLatch(1)
|
||||
val release = CountDownLatch(1)
|
||||
val holder = thread { synchronized(StoreAccounts) { held.countDown(); release.await() } }
|
||||
held.await()
|
||||
val done = CountDownLatch(1)
|
||||
thread { GogMaintenance.checkInstalled(app, emptyList()); done.countDown() }
|
||||
try {
|
||||
assertTrue("checkInstalled waited for the credential lock", done.await(2, TimeUnit.SECONDS))
|
||||
} finally {
|
||||
release.countDown()
|
||||
holder.join()
|
||||
}
|
||||
}
|
||||
|
||||
@Test fun overlappingRefreshesShareOneMorePass() {
|
||||
val opens = AtomicInteger()
|
||||
val entered = CountDownLatch(1)
|
||||
val release = CountDownLatch(1)
|
||||
StoreAccounts.keys = opening {
|
||||
opens.incrementAndGet()
|
||||
entered.countDown()
|
||||
release.await()
|
||||
}
|
||||
StoresState.refresh(app)
|
||||
try {
|
||||
assertTrue(entered.await(5, TimeUnit.SECONDS))
|
||||
repeat(4) { StoresState.refresh(app) }
|
||||
} finally {
|
||||
release.countDown()
|
||||
}
|
||||
awaitRefreshDone()
|
||||
assertEquals(2, opens.get())
|
||||
assertEquals("Someone", StoresState.accounts[Store.EPIC])
|
||||
}
|
||||
|
||||
@Test fun aRequestMadeDuringAFailedPassStillRuns() {
|
||||
val entered = CountDownLatch(1)
|
||||
val release = CountDownLatch(1)
|
||||
StoreAccounts.keys = opening {
|
||||
entered.countDown()
|
||||
release.await()
|
||||
throw Error("keystore died")
|
||||
}
|
||||
StoresState.refresh(app)
|
||||
try {
|
||||
assertTrue(entered.await(5, TimeUnit.SECONDS))
|
||||
StoresState.refresh(app)
|
||||
StoreAccounts.keys = plain
|
||||
} finally {
|
||||
release.countDown()
|
||||
}
|
||||
awaitRefreshDone()
|
||||
awaitRefreshDone()
|
||||
assertEquals("Someone", StoresState.accounts[Store.EPIC])
|
||||
}
|
||||
|
||||
@Test fun aFailedRefreshDoesNotBlockTheNextOne() {
|
||||
val failed = CountDownLatch(1)
|
||||
StoreAccounts.keys = opening { failed.countDown(); throw Error("keystore died") }
|
||||
StoresState.refresh(app)
|
||||
assertTrue(failed.await(5, TimeUnit.SECONDS))
|
||||
awaitRefreshDone()
|
||||
assertNull(StoresState.accounts[Store.EPIC])
|
||||
StoreAccounts.keys = plain
|
||||
StoresState.refresh(app)
|
||||
awaitRefreshDone()
|
||||
assertEquals("Someone", StoresState.accounts[Store.EPIC])
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user