The open source OpenXR runtime

a/android: add getFilesDir function

Co-Authored-By: artdeell <ultramaksim@gmail.com>
Co-authored-by: Korcan Hussein <korcan.hussein@collabora.com>
Part-of: <https://gitlab.freedesktop.org/monado/monado/-/merge_requests/2560>

authored by

Simon Zeni
artdeell
Korcan Hussein
and committed by
Korcan Hussein
42a8e346 fdbc220e

+95 -4
+3 -1
src/external/android-jni-wrap/wrap/android.content.cpp
··· 1 - // Copyright 2020-2023, Collabora, Ltd. 1 + // Copyright 2020-2025, Collabora, Ltd. 2 2 // SPDX-License-Identifier: BSL-1.0 3 3 // Author: Rylie Pavlik <rylie.pavlik@collabora.com> 4 4 ··· 22 22 classRef().getMethod("getClassLoader", "()Ljava/lang/ClassLoader;")), 23 23 getExternalFilesDir(classRef().getMethod( 24 24 "getExternalFilesDir", "(Ljava/lang/String;)Ljava/io/File;")), 25 + getFilesDir(classRef().getMethod( 26 + "getFilesDir", "()Ljava/io/File;")), 25 27 startActivity( 26 28 classRef().getMethod("startActivity", "(Landroid/content/Intent;)V")), 27 29 startActivity1(classRef().getMethod(
+13 -1
src/external/android-jni-wrap/wrap/android.content.h
··· 1 - // Copyright 2020-2021, Collabora, Ltd. 1 + // Copyright 2020-2025, Collabora, Ltd. 2 2 // SPDX-License-Identifier: BSL-1.0 3 3 // Author: Rylie Pavlik <rylie.pavlik@collabora.com> 4 4 ··· 148 148 java::io::File getExternalFilesDir(std::string const &type); 149 149 150 150 /*! 151 + * Wrapper for the getFilesDir method 152 + * 153 + * Java prototype: 154 + * `public abstract java.io.File getFilesDir();` 155 + * 156 + * JNI signature: ()Ljava/io/File; 157 + * 158 + */ 159 + java::io::File getFilesDir(); 160 + 161 + /*! 151 162 * Wrapper for the startActivity method 152 163 * 153 164 * Java prototype: ··· 223 234 jni::method_t getApplicationContext; 224 235 jni::method_t getClassLoader; 225 236 jni::method_t getExternalFilesDir; 237 + jni::method_t getFilesDir; 226 238 jni::method_t startActivity; 227 239 jni::method_t startActivity1; 228 240 jni::method_t getSystemService;
+6 -1
src/external/android-jni-wrap/wrap/android.content.impl.h
··· 1 - // Copyright 2020-2023, Collabora, Ltd. 1 + // Copyright 2020-2025, Collabora, Ltd. 2 2 // SPDX-License-Identifier: BSL-1.0 3 3 // Author: Rylie Pavlik <rylie.pavlik@collabora.com> 4 4 // Inline implementations: do not include on its own! ··· 64 64 assert(!isNull()); 65 65 return java::io::File( 66 66 object().call<jni::Object>(Meta::data().getExternalFilesDir, type)); 67 + } 68 + 69 + inline java::io::File Context::getFilesDir() { 70 + assert(!isNull()); 71 + return java::io::File(object().call<jni::Object>(Meta::data().getFilesDir)); 67 72 } 68 73 69 74 inline void Context::startActivity(Intent const &intent) {
+3 -1
src/xrt/auxiliary/android/CMakeLists.txt
··· 1 - # Copyright 2019-2022, Collabora, Ltd. 1 + # Copyright 2019-2025, Collabora, Ltd. 2 2 # SPDX-License-Identifier: BSL-1.0 3 3 4 4 add_library( ··· 13 13 aux_android STATIC 14 14 android_ahardwarebuffer_allocator.c 15 15 android_ahardwarebuffer_allocator.h 16 + android_content.cpp 17 + android_content.h 16 18 android_custom_surface.cpp 17 19 android_custom_surface.h 18 20 android_globals.cpp
+42
src/xrt/auxiliary/android/android_content.cpp
··· 1 + // Copyright 2024-2025, Collabora, Ltd. 2 + // SPDX-License-Identifier: BSL-1.0 3 + /*! 4 + * @file 5 + * @brief Content class function. 6 + * @author Simon Zeni <simon.zeni@collabora.com> 7 + * @ingroup aux_android 8 + */ 9 + 10 + #include "android_content.h" 11 + #include "util/u_logging.h" 12 + 13 + #include "wrap/android.content.h" 14 + #include "wrap/java.io.h" 15 + 16 + #include <string> 17 + 18 + bool 19 + android_content_get_files_dir(void *context, char *dir, size_t size) 20 + { 21 + if (size == 0 || dir == NULL) { 22 + U_LOG_E("Invalid argument"); 23 + return false; 24 + } 25 + 26 + wrap::java::io::File file = wrap::android::content::Context{(jobject)context}.getFilesDir(); 27 + if (file.isNull()) { 28 + U_LOG_E("Failed to get File object"); 29 + return false; 30 + } 31 + 32 + 33 + const std::string dirPath = file.getAbsolutePath(); 34 + if (size < (dirPath.length() + 1)) { 35 + U_LOG_E("Output string is too small"); 36 + return false; 37 + } 38 + 39 + dirPath.copy(dir, dirPath.length()); 40 + dir[dirPath.length()] = '\0'; 41 + return true; 42 + }
+28
src/xrt/auxiliary/android/android_content.h
··· 1 + // Copyright 2024-2025, Collabora, Ltd. 2 + // SPDX-License-Identifier: BSL-1.0 3 + /*! 4 + * @file 5 + * @brief Content class function. 6 + * @author Simon Zeni <simon.zeni@collabora.com> 7 + * @ingroup aux_android 8 + */ 9 + #pragma once 10 + 11 + #include <xrt/xrt_config_os.h> 12 + 13 + #ifdef XRT_OS_ANDROID 14 + #include <stddef.h> 15 + 16 + #ifdef __cplusplus 17 + extern "C" { 18 + #endif 19 + 20 + bool 21 + android_content_get_files_dir(void *context, char *dir, size_t size); 22 + 23 + 24 + #ifdef __cplusplus 25 + } 26 + #endif 27 + 28 + #endif