From 1f4df2a64dc003973431a96dd0902d535ffce064 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Thu, 18 Aug 2022 18:07:29 +0200 Subject: [PATCH] android/Environment: pass JNIEnv to all functions --- src/Main.cxx | 6 +++--- src/android/Environment.cxx | 8 ++------ src/android/Environment.hxx | 4 ++-- src/fs/StandardDirectory.cxx | 3 ++- 4 files changed, 9 insertions(+), 12 deletions(-) diff --git a/src/Main.cxx b/src/Main.cxx index 77575a008..4e6d56412 100644 --- a/src/Main.cxx +++ b/src/Main.cxx @@ -591,12 +591,12 @@ MainConfigured(const CommandLineOptions &options, #ifdef ANDROID static void -AndroidMain() +AndroidMain(JNIEnv *env) { CommandLineOptions options; ConfigData raw_config; - const auto sdcard = Environment::getExternalStorageDirectory(); + const auto sdcard = Environment::getExternalStorageDirectory(env); if (!sdcard.IsNull()) { const auto config_path = sdcard / Path::FromFS("mpd.conf"); @@ -625,7 +625,7 @@ Java_org_musicpd_Bridge_run(JNIEnv *env, jclass, jobject _context, jobject _logL AtScopeExit() { delete logListener; }; try { - AndroidMain(); + AndroidMain(env); } catch (...) { LogError(std::current_exception()); } diff --git a/src/android/Environment.cxx b/src/android/Environment.cxx index 9a5f51095..6075d6e73 100644 --- a/src/android/Environment.cxx +++ b/src/android/Environment.cxx @@ -51,10 +51,8 @@ Deinitialise(JNIEnv *env) noexcept } AllocatedPath -getExternalStorageDirectory() noexcept +getExternalStorageDirectory(JNIEnv *env) noexcept { - JNIEnv *env = Java::GetEnv(); - jobject file = env->CallStaticObjectMethod(cls, getExternalStorageDirectory_method); @@ -65,14 +63,12 @@ getExternalStorageDirectory() noexcept } AllocatedPath -getExternalStoragePublicDirectory(const char *type) noexcept +getExternalStoragePublicDirectory(JNIEnv *env, const char *type) noexcept { if (getExternalStoragePublicDirectory_method == nullptr) /* needs API level 8 */ return nullptr; - JNIEnv *env = Java::GetEnv(); - Java::String type2(env, type); jobject file = env->CallStaticObjectMethod(cls, getExternalStoragePublicDirectory_method, diff --git a/src/android/Environment.hxx b/src/android/Environment.hxx index c0a5c0323..9a089699a 100644 --- a/src/android/Environment.hxx +++ b/src/android/Environment.hxx @@ -36,10 +36,10 @@ Deinitialise(JNIEnv *env) noexcept; */ [[gnu::pure]] AllocatedPath -getExternalStorageDirectory() noexcept; +getExternalStorageDirectory(JNIEnv *env) noexcept; [[gnu::pure]] AllocatedPath -getExternalStoragePublicDirectory(const char *type) noexcept; +getExternalStoragePublicDirectory(JNIEnv *env, const char *type) noexcept; } // namespace Environment diff --git a/src/fs/StandardDirectory.cxx b/src/fs/StandardDirectory.cxx index 66785361e..2767d2c49 100644 --- a/src/fs/StandardDirectory.cxx +++ b/src/fs/StandardDirectory.cxx @@ -254,7 +254,8 @@ GetUserMusicDir() noexcept #elif defined(USE_XDG) return GetUserDir("XDG_MUSIC_DIR"); #elif defined(ANDROID) - return Environment::getExternalStoragePublicDirectory("Music"); + return Environment::getExternalStoragePublicDirectory(Java::GetEnv(), + "Music"); #else return nullptr; #endif