From 6e9edcdd50c9164fd8440c219f572a7555f0bfc1 Mon Sep 17 00:00:00 2001 From: Alibek Omarov Date: Thu, 16 Oct 2025 04:10:06 +0500 Subject: [PATCH] android: try to automatically create .nomedia file in directory where games are located --- .../engine/ui/library/LibraryViewModel.kt | 3 ++ .../main/java/su/xash/engine/util/Nomedia.kt | 42 +++++++++++++++++++ 2 files changed, 45 insertions(+) create mode 100644 android/app/src/main/java/su/xash/engine/util/Nomedia.kt diff --git a/android/app/src/main/java/su/xash/engine/ui/library/LibraryViewModel.kt b/android/app/src/main/java/su/xash/engine/ui/library/LibraryViewModel.kt index 91a20b4f..252c6b9f 100644 --- a/android/app/src/main/java/su/xash/engine/ui/library/LibraryViewModel.kt +++ b/android/app/src/main/java/su/xash/engine/ui/library/LibraryViewModel.kt @@ -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) } diff --git a/android/app/src/main/java/su/xash/engine/util/Nomedia.kt b/android/app/src/main/java/su/xash/engine/util/Nomedia.kt new file mode 100644 index 00000000..cdb24db2 --- /dev/null +++ b/android/app/src/main/java/su/xash/engine/util/Nomedia.kt @@ -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 + } + } +}