forked from azahar-emu/azahar
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
121 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
99 changes: 99 additions & 0 deletions
99
src/android/app/src/main/java/io/github/lime3ds/android/utils/PlayTimeTracler.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
|
||
// License headers | ||
|
||
package io.github.lime3ds.android.utils | ||
|
||
import androidx.documentfile.provider.DocumentFile | ||
import io.github.lime3ds.android.LimeApplication | ||
import io.github.lime3ds.android.model.Game | ||
import kotlinx.serialization.Serializable | ||
import kotlinx.serialization.encodeToString | ||
import kotlinx.serialization.json.Json | ||
import java.io.BufferedReader | ||
import java.io.InputStreamReader | ||
|
||
@Serializable | ||
data class PlayTimeData( | ||
val titleId: Long, | ||
val title: String, | ||
var totalPlayTimeMs: Long = 0 | ||
) | ||
|
||
object PlayTimeTracker { | ||
private const val PLAYTIME_FILENAME = "playtime.json" | ||
private val playTimes = mutableMapOf<Long, PlayTimeData>() | ||
|
||
init { | ||
loadPlayTimes() | ||
} | ||
|
||
fun addPlayTime(game: Game, sessionTimeMs: Long) { | ||
val data = playTimes.getOrPut(game.titleId) { | ||
PlayTimeData(game.titleId, game.title) | ||
} | ||
data.totalPlayTimeMs += sessionTimeMs | ||
savePlayTimes() | ||
} | ||
|
||
private fun getPlayTime(titleId: Long): Long { | ||
return playTimes[titleId]?.totalPlayTimeMs ?: 0 | ||
} | ||
|
||
fun getFormattedPlayTime(titleId: Long): String { | ||
// Reload playtime in case of manual config file editing | ||
loadPlayTimes() | ||
|
||
val totalSeconds = getPlayTime(titleId) / 1000 | ||
val hours = totalSeconds / 3600 | ||
val minutes = (totalSeconds % 3600) / 60 | ||
val seconds = totalSeconds % 60 | ||
|
||
return when { | ||
hours > 0 -> "${hours}h ${minutes}m ${seconds}s" | ||
minutes > 0 -> "${minutes}m ${seconds}s" | ||
else -> "${seconds}s" | ||
} | ||
} | ||
|
||
private fun loadPlayTimes() { | ||
try { | ||
val root = DocumentFile.fromTreeUri(LimeApplication.appContext, PermissionsHandler.lime3dsDirectory) | ||
val configDir = root?.findFile("config") ?: return | ||
val playTimeFile = configDir.findFile(PLAYTIME_FILENAME) ?: return | ||
|
||
val context = LimeApplication.appContext | ||
val inputStream = context.contentResolver.openInputStream(playTimeFile.uri) ?: return | ||
val reader = BufferedReader(InputStreamReader(inputStream)) | ||
val jsonString = reader.readText() | ||
|
||
val loadedData = Json.decodeFromString<List<PlayTimeData>>(jsonString) | ||
playTimes.clear() | ||
loadedData.forEach { playTimes[it.titleId] = it } | ||
|
||
reader.close() | ||
} catch (e: Exception) { | ||
Log.error("Failed to load play times: ${e.message}") | ||
} | ||
} | ||
|
||
private fun savePlayTimes() { | ||
try { | ||
val root = DocumentFile.fromTreeUri(LimeApplication.appContext, PermissionsHandler.lime3dsDirectory) | ||
val configDir = root?.findFile("config") | ||
?: root?.createDirectory("config") | ||
?: return | ||
|
||
var playTimeFile = configDir.findFile(PLAYTIME_FILENAME) | ||
if (playTimeFile == null) { | ||
playTimeFile = configDir.createFile("application/json", PLAYTIME_FILENAME) ?: return | ||
} | ||
|
||
val jsonString = Json.encodeToString(playTimes.values.toList()) | ||
val outputStream = LimeApplication.appContext.contentResolver.openOutputStream(playTimeFile.uri) ?: return | ||
outputStream.write(jsonString.toByteArray()) | ||
outputStream.close() | ||
} catch (e: Exception) { | ||
Log.error("Failed to save play times: ${e.message}") | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters