From 2dba06dc34eb4b01463e1666bde883d8f2ddc8f2 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Tue, 24 Aug 2021 21:58:09 +0200 Subject: [PATCH] android/Context: add GetExternalFilesDir() --- src/android/Context.cxx | 19 +++++++++++++++++++ src/android/Context.hxx | 4 ++++ 2 files changed, 23 insertions(+) diff --git a/src/android/Context.cxx b/src/android/Context.cxx index 03c0cd52a..2c51779bd 100644 --- a/src/android/Context.cxx +++ b/src/android/Context.cxx @@ -26,6 +26,25 @@ #include "AudioManager.hxx" +AllocatedPath +Context::GetExternalFilesDir(JNIEnv *env, const char *_type) noexcept +{ + assert(_type != nullptr); + + Java::Class cls{env, env->GetObjectClass(Get())}; + jmethodID method = env->GetMethodID(cls, "getExternalFilesDir", + "(Ljava/lang/String;)Ljava/io/File;"); + assert(method); + + Java::String type{env, _type}; + + jobject file = env->CallObjectMethod(Get(), method, type.Get()); + if (Java::DiscardException(env) || file == nullptr) + return nullptr; + + return Java::File::ToAbsolutePath(env, file); +} + AllocatedPath Context::GetCacheDir(JNIEnv *env) const noexcept { diff --git a/src/android/Context.hxx b/src/android/Context.hxx index 6be0735a2..da3d27c05 100644 --- a/src/android/Context.hxx +++ b/src/android/Context.hxx @@ -30,6 +30,10 @@ public: Context(JNIEnv *env, jobject obj) noexcept :Java::GlobalObject(env, obj) {} + [[gnu::pure]] + AllocatedPath GetExternalFilesDir(JNIEnv *env, + const char *type) noexcept; + [[gnu::pure]] AllocatedPath GetCacheDir(JNIEnv *env) const noexcept;