From 8ce1f19bf0860049b700d5ff1561792ee514f7b3 Mon Sep 17 00:00:00 2001 From: reuk Date: Tue, 28 Nov 2023 13:35:32 +0000 Subject: [PATCH] JNI: Tidying --- .../native/juce_Oboe_android.cpp | 4 +-- .../native/juce_JNIHelpers_android.cpp | 33 +++++++++---------- .../native/juce_SystemStats_android.cpp | 2 +- .../juce_core/native/juce_Threads_android.cpp | 6 ++-- .../native/juce_Messaging_android.cpp | 33 +++++++++---------- .../native/juce_ContentSharer_android.cpp | 4 +-- .../native/juce_Windowing_android.cpp | 19 +++++------ .../native/juce_PushNotifications_android.cpp | 20 +++++------ .../juce_video/native/juce_Video_android.h | 2 +- 9 files changed, 60 insertions(+), 63 deletions(-) diff --git a/modules/juce_audio_devices/native/juce_Oboe_android.cpp b/modules/juce_audio_devices/native/juce_Oboe_android.cpp index 8507db5d92..0ee468f287 100644 --- a/modules/juce_audio_devices/native/juce_Oboe_android.cpp +++ b/modules/juce_audio_devices/native/juce_Oboe_android.cpp @@ -1146,7 +1146,7 @@ public: auto* env = getEnv(); - jclass audioManagerClass = env->FindClass ("android/media/AudioManager"); + LocalRef audioManagerClass { env->FindClass ("android/media/AudioManager") }; // We should be really entering here only if API supports it. jassert (audioManagerClass != nullptr); @@ -1202,7 +1202,7 @@ public: void addDevice (const LocalRef& device, JNIEnv* env) { - auto deviceClass = LocalRef ((jclass) env->FindClass ("android/media/AudioDeviceInfo")); + LocalRef deviceClass { env->FindClass ("android/media/AudioDeviceInfo") }; jmethodID getProductNameMethod = env->GetMethodID (deviceClass, "getProductName", "()Ljava/lang/CharSequence;"); diff --git a/modules/juce_core/native/juce_JNIHelpers_android.cpp b/modules/juce_core/native/juce_JNIHelpers_android.cpp index be5f790b93..c7f09dbee3 100644 --- a/modules/juce_core/native/juce_JNIHelpers_android.cpp +++ b/modules/juce_core/native/juce_JNIHelpers_android.cpp @@ -286,7 +286,7 @@ void JNIClassBase::initialise (JNIEnv* env, jobject context) if (sdkVersion >= minSDK) { - LocalRef classNameAndPackage (javaString (String (classPath).replaceCharacter (L'/', L'.'))); + const auto classNameAndPackage = javaString (String (classPath).replaceCharacter (L'/', L'.')); static Array byteCodeLoaders; if (! SystemJavaClassComparator::isSystemClass (this)) @@ -294,8 +294,9 @@ void JNIClassBase::initialise (JNIEnv* env, jobject context) // We use the context's class loader, rather than the 'system' class loader, because we // may need to load classes from our library dependencies (such as the BillingClient // library), and the system class loader is not aware of those libraries. + const LocalRef contextClass { env->FindClass ("android/content/Context") }; const LocalRef defaultClassLoader { env->CallObjectMethod (context, - env->GetMethodID (env->FindClass ("android/content/Context"), + env->GetMethodID (contextClass, "getClassLoader", "()Ljava/lang/ClassLoader;")) }; @@ -466,12 +467,12 @@ LocalRef CreateJavaInterface (AndroidInterfaceImplementer* implementer, // you need to override at least one interface jassert (interfaceNames.size() > 0); - auto classArray = LocalRef (env->NewObjectArray (interfaceNames.size(), JavaClass, nullptr)); + LocalRef classArray { env->NewObjectArray (interfaceNames.size(), JavaClass, nullptr) }; LocalRef classLoader; for (auto i = 0; i < interfaceNames.size(); ++i) { - auto aClass = LocalRef (env->FindClass (interfaceNames[i].toRawUTF8())); + LocalRef aClass { env->FindClass (interfaceNames[i].toRawUTF8()) }; if (aClass != nullptr) { @@ -487,8 +488,8 @@ LocalRef CreateJavaInterface (AndroidInterfaceImplementer* implementer, } } - auto invocationHandler = LocalRef (env->NewObject (JuceInvocationHandler, JuceInvocationHandler.constructor, - reinterpret_cast (implementer))); + LocalRef invocationHandler { env->NewObject (JuceInvocationHandler, JuceInvocationHandler.constructor, + reinterpret_cast (implementer)) }; // CreateJavaInterface() is expected to be called just once for a given implementer jassert (implementer->invocationHandler == nullptr); @@ -631,7 +632,7 @@ int getAndroidSDKVersion() // when this method is used auto* env = getEnv(); - auto buildVersion = env->FindClass ("android/os/Build$VERSION"); + LocalRef buildVersion { env->FindClass ("android/os/Build$VERSION") }; jassert (buildVersion != nullptr); auto sdkVersionField = env->GetStaticFieldID (buildVersion, "SDK_INT", "I"); @@ -825,19 +826,17 @@ String audioManagerGetProperty (const String& property) LocalRef audioManager (env->CallObjectMethod (getAppContext().get(), AndroidContext.getSystemService, javaString ("audio").get())); - if (audioManager != nullptr) - { - LocalRef jProperty (javaString (property)); + if (audioManager == nullptr) + return {}; - auto methodID = env->GetMethodID (AndroidAudioManager, "getProperty", "(Ljava/lang/String;)Ljava/lang/String;"); + auto methodID = env->GetMethodID (AndroidAudioManager, "getProperty", "(Ljava/lang/String;)Ljava/lang/String;"); - if (methodID != nullptr) - return juceString (LocalRef ((jstring) env->CallObjectMethod (audioManager.get(), - methodID, - javaString (property).get()))); - } + if (methodID == nullptr) + return {}; - return {}; + return juceString (LocalRef ((jstring) env->CallObjectMethod (audioManager.get(), + methodID, + javaString (property).get()))); } } diff --git a/modules/juce_core/native/juce_SystemStats_android.cpp b/modules/juce_core/native/juce_SystemStats_android.cpp index 45ae28bf9a..4354c21398 100644 --- a/modules/juce_core/native/juce_SystemStats_android.cpp +++ b/modules/juce_core/native/juce_SystemStats_android.cpp @@ -63,7 +63,7 @@ namespace AndroidStatsHelpers { auto* env = getEnv(); - if (auto settings = (jclass) env->FindClass ("android/provider/Settings$Secure")) + if (LocalRef settings { env->FindClass ("android/provider/Settings$Secure") }) { if (auto fId = env->GetStaticFieldID (settings, "ANDROID_ID", "Ljava/lang/String;")) { diff --git a/modules/juce_core/native/juce_Threads_android.cpp b/modules/juce_core/native/juce_Threads_android.cpp index d1e16bad22..9d9aaf58dd 100644 --- a/modules/juce_core/native/juce_Threads_android.cpp +++ b/modules/juce_core/native/juce_Threads_android.cpp @@ -84,7 +84,7 @@ extern "C" jint JNIEXPORT JNI_OnLoad (JavaVM* vm, void*) auto* env = getEnv(); // register the initialisation function - auto juceJavaClass = env->FindClass ("com/rmsl/juce/Java"); + LocalRef juceJavaClass { env->FindClass ("com/rmsl/juce/Java") }; if (juceJavaClass != nullptr) { @@ -195,7 +195,7 @@ private: if (mainActivity == nullptr) { - LocalRef appContext (getAppContext()); + auto appContext = getAppContext(); auto mainActivityPath = getMainActivityClassPath(); if (mainActivityPath.isNotEmpty()) @@ -217,7 +217,7 @@ private: if (mainActivityClassPath.isEmpty()) { - LocalRef appContext (getAppContext()); + auto appContext = getAppContext(); if (appContext != nullptr) { diff --git a/modules/juce_events/native/juce_Messaging_android.cpp b/modules/juce_events/native/juce_Messaging_android.cpp index 061bd52c1a..2034d06fa7 100644 --- a/modules/juce_events/native/juce_Messaging_android.cpp +++ b/modules/juce_events/native/juce_Messaging_android.cpp @@ -62,7 +62,7 @@ namespace Android struct Handler { - Handler() : nativeHandler (LocalRef (getEnv()->NewObject (AndroidHandler, AndroidHandler.constructor))) {} + Handler() = default; ~Handler() { clearSingletonInstance(); } JUCE_DECLARE_SINGLETON_INLINE (Handler, false) @@ -72,7 +72,7 @@ namespace Android return (getEnv()->CallBooleanMethod (nativeHandler.get(), AndroidHandler.post, runnable) != 0); } - GlobalRef nativeHandler; + GlobalRef nativeHandler { LocalRef { getEnv()->NewObject (AndroidHandler, AndroidHandler.constructor) } }; }; } @@ -81,10 +81,7 @@ struct AndroidMessageQueue final : private Android::Runnable { JUCE_DECLARE_SINGLETON_SINGLETHREADED_INLINE (AndroidMessageQueue, true) - AndroidMessageQueue() - : self (CreateJavaInterface (this, "java/lang/Runnable")) - { - } + AndroidMessageQueue() = default; ~AndroidMessageQueue() override { @@ -115,11 +112,11 @@ private: } } - // the this pointer to this class in Java land - GlobalRef self; - ReferenceCountedArray queue; Android::Handler handler; + + // the this pointer to this class in Java land + GlobalRef self { CreateJavaInterface (this, "java/lang/Runnable") }; }; //============================================================================== @@ -149,7 +146,7 @@ void MessageManager::stopDispatchLoop() void messageCallback() override { auto* env = getEnv(); - LocalRef activity (getCurrentActivity()); + auto activity = getCurrentActivity(); if (activity != nullptr) { @@ -180,7 +177,9 @@ void MessageManager::stopDispatchLoop() class JuceAppLifecycle final : public ActivityLifecycleCallbacks { public: - JuceAppLifecycle (juce::JUCEApplicationBase* (*initSymbolAddr)()) + using CreateApp = juce::JUCEApplicationBase* (*)(); + + explicit JuceAppLifecycle (CreateApp initSymbolAddr) : createApplicationSymbol (initSymbolAddr) { } @@ -200,8 +199,8 @@ public: JUCEApplicationBase::appWillTerminateByForce(); JNIClassBase::releaseAllClasses (env); - jclass systemClass = (jclass) env->FindClass ("java/lang/System"); - jmethodID exitMethod = env->GetStaticMethodID (systemClass, "exit", "(I)V"); + LocalRef systemClass { env->FindClass ("java/lang/System") }; + jmethodID exitMethod = env->GetStaticMethodID (systemClass.get(), "exit", "(I)V"); env->CallStaticVoidMethod (systemClass, exitMethod, 0); } } @@ -225,7 +224,7 @@ public: app->resumed(); } - static JuceAppLifecycle& getInstance (juce::JUCEApplicationBase* (*initSymbolAddr)()) + static JuceAppLifecycle& getInstance (CreateApp initSymbolAddr) { static JuceAppLifecycle juceAppLifecycle (initSymbolAddr); return juceAppLifecycle; @@ -267,7 +266,7 @@ private: std::optional initialiser; GlobalRef myself; - juce::JUCEApplicationBase* (*createApplicationSymbol)(); + CreateApp createApplicationSymbol{}; bool hasBeenInitialised = false; ActivityLifecycleCallbackForwarder forwarder { GlobalRef { getAppContext() }, this }; }; @@ -279,8 +278,8 @@ void juce_juceEventsAndroidStartApp(); void juce_juceEventsAndroidStartApp() { auto dllPath = juce_getExecutableFile().getFullPathName(); - auto addr = reinterpret_cast (DynamicLibrary (dllPath) - .getFunction ("juce_CreateApplication")); + auto addr = reinterpret_cast (DynamicLibrary (dllPath) + .getFunction ("juce_CreateApplication")); if (addr != nullptr) JuceAppLifecycle::getInstance (addr); diff --git a/modules/juce_gui_basics/native/juce_ContentSharer_android.cpp b/modules/juce_gui_basics/native/juce_ContentSharer_android.cpp index 306577e33a..94b63feadd 100644 --- a/modules/juce_gui_basics/native/juce_ContentSharer_android.cpp +++ b/modules/juce_gui_basics/native/juce_ContentSharer_android.cpp @@ -375,8 +375,8 @@ private: const auto context = getAppContext(); - auto* klass = env->FindClass ("com/rmsl/juce/Receiver"); - const LocalRef replyIntent (env->NewObject (AndroidIntent, AndroidIntent.constructorWithContextAndClass, context.get(), klass)); + LocalRef klass { env->FindClass ("com/rmsl/juce/Receiver") }; + const LocalRef replyIntent (env->NewObject (AndroidIntent, AndroidIntent.constructorWithContextAndClass, context.get(), klass.get())); getEnv()->CallObjectMethod (replyIntent, AndroidIntent.putExtraInt, javaString ("com.rmsl.juce.JUCE_REQUEST_CODE").get(), request); const auto flags = FLAG_UPDATE_CURRENT | FLAG_IMMUTABLE; diff --git a/modules/juce_gui_basics/native/juce_Windowing_android.cpp b/modules/juce_gui_basics/native/juce_Windowing_android.cpp index 29aa446cd5..2215fdbd0b 100644 --- a/modules/juce_gui_basics/native/juce_Windowing_android.cpp +++ b/modules/juce_gui_basics/native/juce_Windowing_android.cpp @@ -1329,7 +1329,7 @@ public: { // we don't know if the user is holding on to a local ref to this, so // explicitly create a new one - auto nativeView = LocalRef (env->NewLocalRef (static_cast (nativeViewHandle))); + LocalRef nativeView { env->NewLocalRef (static_cast (nativeViewHandle)) }; if (env->IsInstanceOf (nativeView.get(), AndroidActivity)) { @@ -1838,7 +1838,7 @@ public: if (! handled) { - LocalRef activity (getCurrentActivity()); + auto activity = getCurrentActivity(); if (activity != nullptr) { @@ -2003,7 +2003,7 @@ public: } //============================================================================== - static void handleDoFrameCallback (JNIEnv*, AndroidComponentPeer& t, [[maybe_unused]] int64 frameTimeNanos) + static void handleDoFrameCallback (JNIEnv*, AndroidComponentPeer& t, [[maybe_unused]] jlong frameTimeNanos) { const auto timestampSec = (double) frameTimeNanos / (double) 1'000'000'000; t.callVBlankListeners (timestampSec); @@ -2631,13 +2631,13 @@ Desktop::DisplayOrientation Desktop::getCurrentOrientation() const }; JNIEnv* env = getEnv(); - LocalRef windowServiceString (javaString ("window")); + const auto windowServiceString = javaString ("window"); - LocalRef windowManager = LocalRef (env->CallObjectMethod (getAppContext().get(), AndroidContext.getSystemService, windowServiceString.get())); + LocalRef windowManager { env->CallObjectMethod (getAppContext().get(), AndroidContext.getSystemService, windowServiceString.get()) }; if (windowManager.get() != nullptr) { - LocalRef display = LocalRef (env->CallObjectMethod (windowManager, AndroidWindowManager.getDefaultDisplay)); + LocalRef display { env->CallObjectMethod (windowManager, AndroidWindowManager.getDefaultDisplay) }; if (display.get() != nullptr) { @@ -2688,7 +2688,7 @@ bool KeyPress::isKeyCurrentlyDown (int /*keyCode*/) JUCE_API void JUCE_CALLTYPE Process::hide() { auto* env = getEnv(); - LocalRef currentActivity (getCurrentActivity().get()); + auto currentActivity = getCurrentActivity(); if (env->CallBooleanMethod (currentActivity.get(), AndroidActivity.moveTaskToBack, true) == 0) { @@ -2751,7 +2751,7 @@ public: if (methodName == "onCancel" || methodName == "onClick") { - auto* dialog = env->GetObjectArrayElement (args, 0); + LocalRef dialog { env->GetObjectArrayElement (args, 0) }; env->CallVoidMethod (dialog, AndroidDialogInterface.dismiss); NullCheckedInvocation::invoke (callback); @@ -2853,8 +2853,7 @@ void Displays::findDisplays (float masterScale) { auto* env = getEnv(); - LocalRef windowServiceString (javaString ("window")); - + const auto windowServiceString = javaString ("window"); LocalRef windowManager (env->CallObjectMethod (getAppContext(), AndroidContext.getSystemService, windowServiceString.get())); LocalRef display (env->CallObjectMethod (windowManager, AndroidWindowManager.getDefaultDisplay)); diff --git a/modules/juce_gui_extra/native/juce_PushNotifications_android.cpp b/modules/juce_gui_extra/native/juce_PushNotifications_android.cpp index c3b5146ae3..37f2f1e34c 100644 --- a/modules/juce_gui_extra/native/juce_PushNotifications_android.cpp +++ b/modules/juce_gui_extra/native/juce_PushNotifications_android.cpp @@ -337,7 +337,7 @@ struct PushNotifications::Impl void notifyListenersAboutLocalNotification (const LocalRef& intent) { auto* env = getEnv(); - LocalRef context (getMainActivity()); + auto context = getMainActivity(); auto bundle = LocalRef (env->CallObjectMethod (intent, AndroidIntent.getExtras)); @@ -575,7 +575,7 @@ struct PushNotifications::Impl static LocalRef getNotificationManager() { auto* env = getEnv(); - LocalRef context (getMainActivity()); + auto context = getMainActivity(); return LocalRef (env->CallObjectMethod (context.get(), AndroidContext.getSystemService, @@ -601,9 +601,9 @@ struct PushNotifications::Impl static LocalRef createNotificationBuilder (const Notification& n) { auto* env = getEnv(); - LocalRef context (getMainActivity()); + auto context = getMainActivity(); - jclass builderClass = env->FindClass ("android/app/Notification$Builder"); + LocalRef builderClass { env->FindClass ("android/app/Notification$Builder") }; jassert (builderClass != nullptr); if (builderClass == nullptr) @@ -621,7 +621,7 @@ struct PushNotifications::Impl jassert (builderConstructor != nullptr); if (builderConstructor == nullptr) - return LocalRef (nullptr); + return LocalRef(); if (apiAtLeast26) return LocalRef (env->NewObject (builderClass, builderConstructor, @@ -638,7 +638,7 @@ struct PushNotifications::Impl return notificationBuilder; auto* env = getEnv(); - LocalRef context (getMainActivity()); + auto context = getMainActivity(); auto activityClass = LocalRef (env->CallObjectMethod (context.get(), JavaObject.getClass)); auto notifyIntent = LocalRef (env->NewObject (AndroidIntent, AndroidIntent.constructorWithContextAndClass, context.get(), activityClass.get())); @@ -875,7 +875,7 @@ struct PushNotifications::Impl return notificationBuilder; auto* env = getEnv(); - LocalRef context (getMainActivity()); + auto context = getMainActivity(); auto activityClass = LocalRef (env->CallObjectMethod (context.get(), JavaObject.getClass)); auto deleteIntent = LocalRef (env->NewObject (AndroidIntent, AndroidIntent.constructorWithContextAndClass, context.get(), activityClass.get())); @@ -905,7 +905,7 @@ struct PushNotifications::Impl return notificationBuilder; auto* env = getEnv(); - LocalRef context (getMainActivity()); + auto context = getMainActivity(); for (const auto [actionIndex, action] : enumerate (n.actions)) { @@ -998,7 +998,7 @@ struct PushNotifications::Impl static LocalRef juceUrlToAndroidUri (const URL& url) { auto* env = getEnv(); - LocalRef context (getMainActivity()); + auto context = getMainActivity(); auto packageNameString = LocalRef ((jstring) (env->CallObjectMethod (context.get(), AndroidContext.getPackageName))); @@ -1462,7 +1462,7 @@ struct PushNotifications::Impl static bool intentActionContainsAnyOf (jobject intent, const StringArray& strings, bool includePackageName) { auto* env = getEnv(); - LocalRef context (getMainActivity()); + auto context = getMainActivity(); String packageName = includePackageName ? juceString ((jstring) env->CallObjectMethod (context.get(), AndroidContext.getPackageName)) diff --git a/modules/juce_video/native/juce_Video_android.h b/modules/juce_video/native/juce_Video_android.h index de869f092c..a7c6140f7b 100644 --- a/modules/juce_video/native/juce_Video_android.h +++ b/modules/juce_video/native/juce_Video_android.h @@ -1484,7 +1484,7 @@ private: auto* env = getEnv(); - auto requestBuilderClass = LocalRef (env->FindClass ("android/media/AudioFocusRequest$Builder")); + LocalRef requestBuilderClass { env->FindClass ("android/media/AudioFocusRequest$Builder") }; static jmethodID constructor = env->GetMethodID (requestBuilderClass, "", "(I)V"); static jmethodID buildMethod = env->GetMethodID (requestBuilderClass, "build", "()Landroid/media/AudioFocusRequest;");