From b90e32fe4e8c50de94af55f85f7684e50c76e314 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Thu, 18 Aug 2022 15:12:18 +0200 Subject: [PATCH] Android/Context: look up methods once during startup --- src/Main.cxx | 3 +++ src/android/Context.cxx | 38 ++++++++++++++++++++------------------ src/android/Context.hxx | 6 ++++++ 3 files changed, 29 insertions(+), 18 deletions(-) diff --git a/src/Main.cxx b/src/Main.cxx index 4e6d56412..0228e8fba 100644 --- a/src/Main.cxx +++ b/src/Main.cxx @@ -614,9 +614,12 @@ Java_org_musicpd_Bridge_run(JNIEnv *env, jclass, jobject _context, jobject _logL Java::Init(env); Java::Object::Initialise(env); Java::File::Initialise(env); + Environment::Initialise(env); AtScopeExit(env) { Environment::Deinitialise(env); }; + Context::Initialise(env); + context = new Context(env, _context); AtScopeExit() { delete context; }; diff --git a/src/android/Context.cxx b/src/android/Context.cxx index 2c51779bd..f86f26bdf 100644 --- a/src/android/Context.cxx +++ b/src/android/Context.cxx @@ -26,19 +26,31 @@ #include "AudioManager.hxx" +static jmethodID getExternalFilesDir_method, + getCacheDir_method, + getSystemService_method; + +void +Context::Initialise(JNIEnv *env) noexcept +{ + Java::Class cls{env, "android/content/Context"}; + + getExternalFilesDir_method = env->GetMethodID(cls, "getExternalFilesDir", + "(Ljava/lang/String;)Ljava/io/File;"); + getCacheDir_method = env->GetMethodID(cls, "getCacheDir", + "()Ljava/io/File;"); + getSystemService_method = env->GetMethodID(cls, "getSystemService", + "(Ljava/lang/String;)Ljava/lang/Object;"); +} + 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()); + jobject file = env->CallObjectMethod(Get(), getExternalFilesDir_method, type.Get()); if (Java::DiscardException(env) || file == nullptr) return nullptr; @@ -50,12 +62,7 @@ Context::GetCacheDir(JNIEnv *env) const noexcept { assert(env != nullptr); - Java::Class cls(env, env->GetObjectClass(Get())); - jmethodID method = env->GetMethodID(cls, "getCacheDir", - "()Ljava/io/File;"); - assert(method); - - jobject file = env->CallObjectMethod(Get(), method); + jobject file = env->CallObjectMethod(Get(), getCacheDir_method); if (Java::DiscardException(env) || file == nullptr) return nullptr; @@ -67,13 +74,8 @@ Context::GetAudioManager(JNIEnv *env) noexcept { assert(env != nullptr); - Java::Class cls(env, env->GetObjectClass(Get())); - jmethodID method = env->GetMethodID(cls, "getSystemService", - "(Ljava/lang/String;)Ljava/lang/Object;"); - assert(method); - Java::String name(env, "audio"); - jobject am = env->CallObjectMethod(Get(), method, name.Get()); + jobject am = env->CallObjectMethod(Get(), getSystemService_method, name.Get()); if (Java::DiscardException(env) || am == nullptr) return nullptr; diff --git a/src/android/Context.hxx b/src/android/Context.hxx index da3d27c05..b771bc69a 100644 --- a/src/android/Context.hxx +++ b/src/android/Context.hxx @@ -27,6 +27,12 @@ class AudioManager; class Context : public Java::GlobalObject { public: + /** + * Global initialisation. Looks up the methods of the + * Context Java class. + */ + static void Initialise(JNIEnv *env) noexcept; + Context(JNIEnv *env, jobject obj) noexcept :Java::GlobalObject(env, obj) {}