diff --git a/app/src/main/java/com/droiddeck/launcher/FrontEndActivity.kt b/app/src/main/java/com/droiddeck/launcher/FrontEndActivity.kt index cbd9ba859b..b094c98915 100644 --- a/app/src/main/java/com/droiddeck/launcher/FrontEndActivity.kt +++ b/app/src/main/java/com/droiddeck/launcher/FrontEndActivity.kt @@ -873,6 +873,7 @@ class FrontEndActivity : ComponentActivity() { android.widget.Toast.makeText(this, R.string.game_link_failed, android.widget.Toast.LENGTH_LONG).show() return false } + Library.recordPlayed(this, game) startActivity(Intent(this, SessionActivity::class.java).setAction(SessionService.ACTION_RESUME) .putExtra(SessionActivity.EXTRA_STEAMVR, steamVr) .returnTo(returnHome) @@ -888,7 +889,15 @@ class FrontEndActivity : ComponentActivity() { putExtra(SessionService.EXTRA_STEAM_URL, com.droiddeck.launcher.frontend.GameLaunchLink.steamUrl(game.gameIdString)) } - }, steamSession = true) + } + .playing(game), steamSession = true) + } + + private fun Intent.playing(game: Library.SteamGame): Intent = + apply { if (game.library == Library.ADDED) putExtra(SessionActivity.EXTRA_PLAYED_GAME, game.gameIdString) } + + private fun recordPlayed(intent: Intent) { + intent.getStringExtra(SessionActivity.EXTRA_PLAYED_GAME)?.let { Library.recordPlayed(this, it) } } private fun Intent.returnTo(returnHome: Boolean): Intent { @@ -915,14 +924,17 @@ class FrontEndActivity : ComponentActivity() { .putExtra(SessionService.EXTRA_PROGRAM, command.program) .putExtra(SessionService.EXTRA_PROGRAM_ARGS, command.args.toTypedArray()) .putExtra(SessionService.EXTRA_GAME_ID, com.droiddeck.launcher.session.NoSteamLaunch.scope(game)) + .playing(game) when { !SessionState.running -> startSessionOrWait(next) SessionState.stopRequested -> android.widget.Toast.makeText(this, R.string.game_link_session_busy, android.widget.Toast.LENGTH_LONG).show() - command.program == com.droiddeck.launcher.session.NoSteamLaunch.BATTLENET && handToBattleNet(command.args, returnHome) { pendingSwitch = next } -> {} - SessionState.mode == SessionService.MODE_RUN && SessionState.program == command.program && SessionState.programArgs == command.args -> + command.program == com.droiddeck.launcher.session.NoSteamLaunch.BATTLENET && handToBattleNet(command.args, next, returnHome) -> {} + SessionState.mode == SessionService.MODE_RUN && SessionState.program == command.program && SessionState.programArgs == command.args -> { + recordPlayed(next) startActivity(Intent(this, SessionActivity::class.java).setAction(SessionService.ACTION_RESUME) .returnTo(returnHome) .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_SINGLE_TOP)) + } else -> pendingSwitch = next } } @@ -2276,14 +2288,14 @@ class FrontEndActivity : ComponentActivity() { when { !SessionState.running -> startSessionOrWait(next) SessionState.stopRequested -> android.widget.Toast.makeText(this, R.string.game_link_session_busy, android.widget.Toast.LENGTH_LONG).show() - handToBattleNet(full) { pendingSwitch = next } -> {} + handToBattleNet(full, next) -> {} else -> pendingSwitch = next } } }, "battlenet-open").start() } - private fun handToBattleNet(args: List, returnHome: Boolean = false, otherwise: () -> Unit): Boolean { + private fun handToBattleNet(args: List, next: Intent, returnHome: Boolean = false): Boolean { if (SessionState.mode != SessionService.MODE_RUN || SessionState.program != com.droiddeck.launcher.session.NoSteamLaunch.BATTLENET) return false val library = args.takeIf { it.firstOrNull() == "--library" }?.getOrNull(1) val sessionLibrary = SessionState.programArgs.takeIf { it.firstOrNull() == "--library" }?.getOrNull(1) @@ -2294,10 +2306,13 @@ class FrontEndActivity : ComponentActivity() { val handed = request.isEmpty() || com.droiddeck.launcher.stores.battlenet.BattleNetClient.request(LinuxRuntime.sessionRoot(app), request) ui.post { if (isDestroyed) return@post - if (!handed) otherwise() - else startActivity(Intent(this, SessionActivity::class.java).setAction(SessionService.ACTION_RESUME) - .returnTo(returnHome) - .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_SINGLE_TOP)) + if (!handed) pendingSwitch = next + else { + recordPlayed(next) + startActivity(Intent(this, SessionActivity::class.java).setAction(SessionService.ACTION_RESUME) + .returnTo(returnHome) + .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_SINGLE_TOP)) + } } }, "battlenet-request").start() return true diff --git a/app/src/main/java/com/droiddeck/launcher/SessionActivity.kt b/app/src/main/java/com/droiddeck/launcher/SessionActivity.kt index 378e554d4a..4d22ffff65 100644 --- a/app/src/main/java/com/droiddeck/launcher/SessionActivity.kt +++ b/app/src/main/java/com/droiddeck/launcher/SessionActivity.kt @@ -2230,6 +2230,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { intent.getStringExtra(SessionService.EXTRA_GAME_ID), sessionActivityDisplayId, ) + intent.getStringExtra(EXTRA_PLAYED_GAME)?.let { com.droiddeck.launcher.frontend.Library.recordPlayed(this, it) } } applyFrameGen() applyGameAlignment() @@ -5443,6 +5444,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { const val EXTRA_RETURN_HOME = "returnHome" const val EXTRA_LINK_TASK = "linkTask" const val EXTRA_STEAMVR = "steamVr" + const val EXTRA_PLAYED_GAME = "com.droiddeck.launcher.PLAYED_GAME" const val EXTRA_REPAIR_RUNTIME = "repairRuntime" private const val LEAVE_TIMEOUT_MS = 1_500L private const val TEXT_FIELD_TAP_MS = 1_000L diff --git a/app/src/main/java/com/droiddeck/launcher/frontend/Library.kt b/app/src/main/java/com/droiddeck/launcher/frontend/Library.kt index ef7ec06a10..b303ecc71e 100644 --- a/app/src/main/java/com/droiddeck/launcher/frontend/Library.kt +++ b/app/src/main/java/com/droiddeck/launcher/frontend/Library.kt @@ -18,9 +18,10 @@ object Library { val source: String = SOURCE_STEAM, val storeId: String? = null, val logo: File? = null, + val installedAt: Long = 0L, ) { fun withArt(art: File?, hero: File?, icon: File?, logo: File? = this.logo) = - SteamGame(appId, name, art, library, gameId, hero, lastPlayed, gameFiles, protonPrefix, icon, source, storeId, logo) + SteamGame(appId, name, art, library, gameId, hero, lastPlayed, gameFiles, protonPrefix, icon, source, storeId, logo, installedAt) val gameIdString: String get() = java.lang.Long.toUnsignedString(gameId) val launchKey: String get() = if (storeId != null) "$source:$storeId" else gameIdString @@ -31,6 +32,7 @@ object Library { } } const val ADDED = "added" + private const val PLAYED = "added_games_played" const val EPIC_LAUNCHER = "epic-launcher" const val EPIC_LAUNCHER_NAME = "Epic Games Launcher" const val BATTLENET_LAUNCHER = "battlenet-launcher" @@ -221,6 +223,18 @@ object Library { return (steam + own.map { g -> addedGame(context, g, roots, played[g.appId] ?: 0L) }).distinctBy { it.gameId } } + fun recordPlayed(context: Context, game: SteamGame) { + if (game.library == ADDED) recordPlayed(context, game.gameIdString) + } + + fun recordPlayed(context: Context, gameId: String) { + context.getSharedPreferences(PLAYED, Context.MODE_PRIVATE).edit() + .putLong(gameId, System.currentTimeMillis() / 1000).apply() + } + + internal fun recordedPlay(context: Context, gameId: Long): Long = + context.getSharedPreferences(PLAYED, Context.MODE_PRIVATE).getLong(java.lang.Long.toUnsignedString(gameId), 0L) + internal fun shortcutsLastPlayed(root: File): Map = runCatching { val config = AddedGames.accountConfig(root) ?: return emptyMap() val bytes = File(config, "shortcuts.vdf").takeIf { it.isFile }?.readBytes() ?: return emptyMap() @@ -249,12 +263,13 @@ object Library { AddedGameArt.resolve(context, g).let { art -> SteamGame( g.steamAppId ?: g.appId.toInt(), g.name, art.portrait ?: art.header, ADDED, g.gameId, - hero = art.hero ?: art.header, lastPlayed = lastPlayed, gameFiles = g.folder, + hero = art.hero ?: art.header, lastPlayed = maxOf(lastPlayed, recordedPlay(context, g.gameId)), gameFiles = g.folder, protonPrefix = if (g.source == com.droiddeck.launcher.stores.Store.BATTLENET.id) File(com.droiddeck.launcher.runtime.BattleNet.prefix(context), "pfx").takeIf { it.isDirectory } else protonPrefix(context, g.steamAppId?.toLong() ?: g.appId, libraries = roots), icon = art.icon?.takeIf { it.extension.lowercase() != "ico" }, source = g.source, storeId = g.storeId, logo = art.logo, + installedAt = g.folder.lastModified(), ) } diff --git a/app/src/main/java/com/droiddeck/launcher/frontend/LibraryCache.kt b/app/src/main/java/com/droiddeck/launcher/frontend/LibraryCache.kt index 6fa35c753a..81434ce2ca 100644 --- a/app/src/main/java/com/droiddeck/launcher/frontend/LibraryCache.kt +++ b/app/src/main/java/com/droiddeck/launcher/frontend/LibraryCache.kt @@ -27,6 +27,7 @@ object LibraryCache { source = o.optString("source").ifEmpty { if (o.getString("library") == Library.ADDED) Library.ADDED else Library.SOURCE_STEAM }, storeId = o.optString("storeId").ifEmpty { null }, logo = o.file("logo"), + installedAt = o.optLong("installedAt"), ) } to x64 }.getOrElse { e -> Log.w(TAG, "unreadable, ignored: ${e.message}"); Pair(emptyList(), false) } @@ -44,7 +45,7 @@ object LibraryCache { JSONObject().put("appId", g.appId).put("name", g.name).put("library", g.library).put("gameId", g.gameId) .put("lastPlayed", g.lastPlayed).putFile("art", g.art).putFile("hero", g.hero) .putFile("gameFiles", g.gameFiles).putFile("protonPrefix", g.protonPrefix).putFile("icon", g.icon).putFile("logo", g.logo) - .put("source", g.source).apply { g.storeId?.let { put("storeId", it) } }, + .put("source", g.source).put("installedAt", g.installedAt).apply { g.storeId?.let { put("storeId", it) } }, ) val f = file(context) val tmp = File(f.path + ".tmp") diff --git a/app/src/main/java/com/droiddeck/launcher/stores/StoreCatalog.kt b/app/src/main/java/com/droiddeck/launcher/stores/StoreCatalog.kt index ad59e511a0..cb46d23601 100644 --- a/app/src/main/java/com/droiddeck/launcher/stores/StoreCatalog.kt +++ b/app/src/main/java/com/droiddeck/launcher/stores/StoreCatalog.kt @@ -65,6 +65,8 @@ private fun releaseDateValue(raw: String): Long = runCatching { java.time.LocalDate.parse(day).toEpochDay() }.getOrDefault(0L) } ?: 0L +internal fun entryTime(first: Long?, fallback: Long?): Long = first?.takeIf { it > 0 } ?: fallback?.takeIf { it > 0 } ?: 0L + fun sortGames( items: List, sort: String, diff --git a/app/src/main/java/com/droiddeck/launcher/stores/battlenet/BattleNetStore.kt b/app/src/main/java/com/droiddeck/launcher/stores/battlenet/BattleNetStore.kt index 737ba3b4ff..403b867538 100644 --- a/app/src/main/java/com/droiddeck/launcher/stores/battlenet/BattleNetStore.kt +++ b/app/src/main/java/com/droiddeck/launcher/stores/battlenet/BattleNetStore.kt @@ -204,7 +204,8 @@ object BattleNetBackend : StoreBackend { fun installedGames(context: Context): List = installed(context).map { game -> val art = BattleNet.art(context, game.title) val cover = art.cover?.let { Uri.fromFile(it).toString() } - InstalledStoreGame(StoreGameSidecar(store, game.title.uid, game.name, "", cover = cover, hero = art.header?.let { Uri.fromFile(it).toString() } ?: cover), game.folder) + InstalledStoreGame(StoreGameSidecar(store, game.title.uid, game.name, "", installedAt = game.folder.lastModified(), + cover = cover, hero = art.header?.let { Uri.fromFile(it).toString() } ?: cover), game.folder) } private fun shelves(context: Context): StoreShelves { diff --git a/app/src/main/java/com/droiddeck/launcher/ui/LibraryPage.kt b/app/src/main/java/com/droiddeck/launcher/ui/LibraryPage.kt index 6550a2fd06..dfba4cf31f 100644 --- a/app/src/main/java/com/droiddeck/launcher/ui/LibraryPage.kt +++ b/app/src/main/java/com/droiddeck/launcher/ui/LibraryPage.kt @@ -109,6 +109,7 @@ import com.droiddeck.launcher.stores.itch.ItchUpdates import com.droiddeck.launcher.stores.gog.GogMaintenance import com.droiddeck.launcher.stores.retainOrder import com.droiddeck.launcher.stores.xbox.XboxLibraryFilter +import com.droiddeck.launcher.stores.entryTime import com.droiddeck.launcher.stores.sortGames import kotlinx.coroutines.launch import androidx.compose.runtime.setValue @@ -296,8 +297,8 @@ internal fun LibraryPage(s: FrontEndState, a: FrontEndActions, selected: String, val sizesPending = sizeSort && !sizesWaited && candidates.any { sizeOf(it) == null } val sorted = sortGames( candidates, sort, title = { it.title }, releaseDate = { it.item?.releaseDate.orEmpty() }, - lastPlayed = { e -> e.game?.lastPlayed ?: e.storeKey?.let { lastPlayed[it] } ?: 0L }, - installedAt = { e -> e.storeKey?.let { installedAt[it] } ?: 0L }, + lastPlayed = { e -> entryTime(e.game?.lastPlayed, e.storeKey?.let { lastPlayed[it] }) }, + installedAt = { e -> entryTime(e.storeKey?.let { installedAt[it] }, e.game?.installedAt) }, size = { e -> sizeOf(e) ?: 0L }, ) var stable by remember { mutableStateOf(emptyList()) } diff --git a/app/src/test/java/com/droiddeck/launcher/frontend/LibraryShortcutsLastPlayedTest.kt b/app/src/test/java/com/droiddeck/launcher/frontend/LibraryShortcutsLastPlayedTest.kt index cc9a1ac7df..baacb804e3 100644 --- a/app/src/test/java/com/droiddeck/launcher/frontend/LibraryShortcutsLastPlayedTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/frontend/LibraryShortcutsLastPlayedTest.kt @@ -45,6 +45,30 @@ class LibraryShortcutsLastPlayedTest { assertEquals(mapOf(3388195926L to 1792931476L, 1234L to 1700000000L), Library.shortcutsLastPlayed(root)) } + @Test fun aLaunchedAddedGameRemembersItsPlayTime() { + val context = org.robolectric.RuntimeEnvironment.getApplication() + val added = Library.SteamGame(-1, "World of Warcraft", null, Library.ADDED, gameId = 0xC0FFEE0002000000uL.toLong()) + val steam = Library.SteamGame(570, "Dota 2", null, "/steam") + assertEquals(0L, Library.recordedPlay(context, added.gameId)) + val before = System.currentTimeMillis() / 1000 + Library.recordPlayed(context, added) + Library.recordPlayed(context, steam) + assert(Library.recordedPlay(context, added.gameId) >= before) + assertEquals(0L, Library.recordedPlay(context, steam.gameId)) + } + + @Test fun anAddedGameBuiltAgainKeepsItsRecordedPlayTime() { + val context = org.robolectric.RuntimeEnvironment.getApplication() + val dir = tmp.newFolder("Celeste") + val exe = File(dir, "Celeste.exe").apply { writeText("x") } + val g = AddedGames.Game(dir, "Celeste", exe, "/g/Celeste.exe", "/g", 0x82000003L, 0x82000003L, emptyList()) + val shown = Library.addedGame(context, g, emptyList()) + assertEquals(0L, shown.lastPlayed) + Library.recordPlayed(context, shown) + assertEquals(Library.recordedPlay(context, g.gameId), Library.addedGame(context, g, emptyList()).lastPlayed) + assertEquals(1900000000L, Library.addedGame(context, g, emptyList(), lastPlayed = 1900000000L).lastPlayed) + } + @Test fun missingOrBrokenShortcutsGiveNothing() { val root = steamRoot() assertEquals(emptyMap(), Library.shortcutsLastPlayed(root)) diff --git a/app/src/test/java/com/droiddeck/launcher/stores/SortCatalogItemsTest.kt b/app/src/test/java/com/droiddeck/launcher/stores/SortCatalogItemsTest.kt index 8a315f3a1a..23a0c16dec 100644 --- a/app/src/test/java/com/droiddeck/launcher/stores/SortCatalogItemsTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/stores/SortCatalogItemsTest.kt @@ -20,6 +20,14 @@ class SortCatalogItemsTest { assertEquals(listOf("Zelda", "Beta", "alpha"), sortCatalogItems(items, "titleDesc").map { it.title }) } + @Test fun anEntryTimeFallsBackWhenTheFirstSourceHasNone() { + assertEquals(30L, entryTime(30L, 20L)) + assertEquals(20L, entryTime(0L, 20L)) + assertEquals(20L, entryTime(null, 20L)) + assertEquals(0L, entryTime(0L, null)) + assertEquals(0L, entryTime(null, 0L)) + } + @Test fun playedAndInstalledComeFirstWithTitlesBreakingTies() { val items = listOf(item("1", "Zelda"), item("2", "alpha"), item("3", "Beta")) val played = mapOf("gog:3" to 900L, "gog:1" to 900L)