android: try to automatically create .nomedia file in directory where games are located

This commit is contained in:
Alibek Omarov
2025-10-16 04:10:06 +05:00
committed by a1batross
parent 7a02110a42
commit 6e9edcdd50
2 changed files with 45 additions and 0 deletions

View File

@@ -12,6 +12,7 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import su.xash.engine.model.Game
import su.xash.engine.util.Nomedia
import java.io.File
class LibraryViewModel(application: Application) : AndroidViewModel(application) {
@@ -39,6 +40,8 @@ class LibraryViewModel(application: Application) : AndroidViewModel(application)
?: (Environment.getExternalStorageDirectory().absolutePath + "/xash")
val root = File(rootPath)
Nomedia.ensureNomedia(root)
_installedGames.postValue(Game.getGames(ctx, root))
_isReloading.postValue(false)
}

View File

@@ -0,0 +1,42 @@
/*
Nomedia.kt - ensure that .nomedia file exists in directory
Copyright (C) 2025 Alibek Omarov
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
*/
package su.xash.engine.util
import java.io.File
object Nomedia {
fun ensureNomedia(directory: File): Boolean {
try {
// ensure paths exists
if (!directory.exists() && !directory.mkdirs()) {
return false
}
// check if it's a directory
if (!directory.isDirectory) {
return false
}
val nomediaFile = File(directory, ".nomedia")
// it returns false if file already exists, which is fine for us
nomediaFile.createNewFile()
return true
} catch (e: Exception) {
e.printStackTrace()
return false
}
}
}