android: Split path resolution logic of getUserDirectory into seperate function

This commit is contained in:
OpenSauce04 2026-03-07 18:23:05 +00:00 committed by OpenSauce
parent 97c9a51015
commit 96485a22f8
5 changed files with 22 additions and 19 deletions

View file

@ -693,34 +693,37 @@ object NativeLibrary {
@Keep @Keep
@JvmStatic @JvmStatic
fun getUserDirectory(uriOverride: Uri? = null): String { fun getNativePath(uri: Uri): String {
BuildUtil.assertNotGooglePlay() BuildUtil.assertNotGooglePlay()
val preferences: SharedPreferences =
PreferenceManager.getDefaultSharedPreferences(CitraApplication.appContext)
val dirSep = "/" val dirSep = "/"
val udUri = uriOverride ?:
preferences.getString("CITRA_DIRECTORY", "")!!.toUri()
val udPathSegment = udUri.lastPathSegment!!
val udVirtualPath = udPathSegment.substringAfter(":")
if (udPathSegment.startsWith("primary:")) { // User directory is located in primary storage val pathSegment = uri.lastPathSegment!!
val virtualPath = pathSegment.substringAfter(":")
if (pathSegment.startsWith("primary:")) { // User directory is located in primary storage
val primaryStoragePath = Environment.getExternalStorageDirectory().absolutePath val primaryStoragePath = Environment.getExternalStorageDirectory().absolutePath
return primaryStoragePath + dirSep + udVirtualPath + dirSep return primaryStoragePath + dirSep + virtualPath + dirSep
} else { // User directory probably located on a removable storage device } else { // User directory probably located on a removable storage device
val storageIdString = udPathSegment.substringBefore(":") val storageIdString = pathSegment.substringBefore(":")
val udRemovablePath = RemovableStorageHelper.getRemovableStoragePath(storageIdString) val removablePath = RemovableStorageHelper.getRemovableStoragePath(storageIdString)
if (udRemovablePath == null) { if (removablePath == null) {
android.util.Log.e("NativeLibrary", android.util.Log.e("NativeLibrary",
"Unknown mount location for storage device '$storageIdString' (URI: $udUri)" "Unknown mount location for storage device '$storageIdString' (URI: $uri)"
) )
return "" return ""
} }
return udRemovablePath + dirSep + udVirtualPath + dirSep return removablePath + dirSep + virtualPath
} }
}
@Keep
@JvmStatic
fun getUserDirectory(): String {
val preferences: SharedPreferences =
PreferenceManager.getDefaultSharedPreferences(CitraApplication.appContext)
return getNativePath(preferences.getString("CITRA_DIRECTORY", "")!!.toUri())
} }
@Keep @Keep

View file

@ -567,7 +567,7 @@ class SetupFragment : Fragment() {
} }
if (!BuildUtil.isGooglePlayBuild) { if (!BuildUtil.isGooglePlayBuild) {
if (NativeLibrary.getUserDirectory(result) == "") { if (NativeLibrary.getNativePath(result) == "") {
SelectUserDirectoryDialogFragment.newInstance( SelectUserDirectoryDialogFragment.newInstance(
mainActivity, mainActivity,
R.string.invalid_selection, R.string.invalid_selection,

View file

@ -367,7 +367,7 @@ class MainActivity : AppCompatActivity(), ThemeProvider {
} }
if (!BuildUtil.isGooglePlayBuild) { if (!BuildUtil.isGooglePlayBuild) {
if (NativeLibrary.getUserDirectory(result) == "") { if (NativeLibrary.getNativePath(result) == "") {
SelectUserDirectoryDialogFragment.newInstance( SelectUserDirectoryDialogFragment.newInstance(
this, this,
R.string.invalid_selection, R.string.invalid_selection,

View file

@ -296,7 +296,7 @@ bool MoveAndRenameFile(const std::string& src_full_path, const std::string& dest
std::string TranslateFilePath(const std::string& filepath) { std::string TranslateFilePath(const std::string& filepath) {
std::optional<std::string> userDirLocation = GetUserDirectory(); std::optional<std::string> userDirLocation = GetUserDirectory();
if (userDirLocation) { if (userDirLocation) {
return *userDirLocation + filepath; return *userDirLocation + "/" + filepath;
} }
return ""; return "";
} }

View file

@ -20,7 +20,7 @@
V(GetFilesName, std::vector<std::string>, (const std::string& filepath), get_files_name, \ V(GetFilesName, std::vector<std::string>, (const std::string& filepath), get_files_name, \
"getFilesName", "(Ljava/lang/String;)[Ljava/lang/String;") \ "getFilesName", "(Ljava/lang/String;)[Ljava/lang/String;") \
V(GetUserDirectory, std::optional<std::string>, (), get_user_directory, "getUserDirectory", \ V(GetUserDirectory, std::optional<std::string>, (), get_user_directory, "getUserDirectory", \
"(Landroid/net/Uri;)Ljava/lang/String;") \ "()Ljava/lang/String;") \
V(CopyFile, bool, \ V(CopyFile, bool, \
(const std::string& source, const std::string& destination_path, \ (const std::string& source, const std::string& destination_path, \
const std::string& destination_filename), \ const std::string& destination_filename), \