From a1b0e73ac5bd8e4b4998900fb6ac65a2de475e62 Mon Sep 17 00:00:00 2001 From: Jonathan De Wachter Date: Mon, 24 Feb 2014 13:28:59 +0100 Subject: [PATCH] [Android] SFML activity allows a custom library name for the user application --- examples/android/AndroidManifest.xml | 3 +- src/SFML/Main/SFMLActivity.cpp | 41 +++++++++++++++++++++++++++- 2 files changed, 42 insertions(+), 2 deletions(-) diff --git a/examples/android/AndroidManifest.xml b/examples/android/AndroidManifest.xml index dc43718d..bf361ab0 100644 --- a/examples/android/AndroidManifest.xml +++ b/examples/android/AndroidManifest.xml @@ -18,7 +18,8 @@ android:icon="@drawable/sfml_logo" android:configChanges="keyboardHidden|screenSize"> - + + diff --git a/src/SFML/Main/SFMLActivity.cpp b/src/SFML/Main/SFMLActivity.cpp index 60654d08..ce3f8ac5 100644 --- a/src/SFML/Main/SFMLActivity.cpp +++ b/src/SFML/Main/SFMLActivity.cpp @@ -22,6 +22,7 @@ // //////////////////////////////////////////////////////////// +#include #include #include #include @@ -31,6 +32,42 @@ #define LOGE(...) ((void)__android_log_print(ANDROID_LOG_INFO, "sfml-activity", __VA_ARGS__)) +std::string getLibraryName(JNIEnv* lJNIEnv, jobject& objectActivityInfo) +{ + // This function reads the value of meta-data "sfml.app.lib_name" + // found in the Android Manifest file and returns it. It performs the + // following java code using the JNI interface: + // + // ai.metaData.getString("sfml.app.lib_name"); + + // Get metaData instance from the ActivityInfo object + jclass classActivityInfo = lJNIEnv->FindClass("android/content/pm/ActivityInfo"); + jfieldID fieldMetaData = lJNIEnv->GetFieldID(classActivityInfo, "metaData", "Landroid/os/Bundle;"); + jobject objectMetaData = lJNIEnv->GetObjectField(objectActivityInfo, fieldMetaData); + + // Create a java string object containing "sfml.app.lib_name" + jobject objectName = lJNIEnv->NewStringUTF("sfml.app.lib_name"); + + // Get the value of meta-data named "sfml.app.lib_name" + jclass classBundle = lJNIEnv->FindClass("android/os/Bundle"); + jmethodID methodGetString = lJNIEnv->GetMethodID(classBundle, "getString", "(Ljava/lang/String;)Ljava/lang/String;"); + jobject objectValue = lJNIEnv->CallObjectMethod(objectMetaData, methodGetString, objectName); + + // No meta-data "sfml.app.lib_name" was found so we abord and inform the user + if (objectValue == NULL) + { + LOGE("No meta-data 'sfml.app.lib_name' found in AndroidManifest.xml file"); + exit(1); + } + + // Convert the application name to a C++ string and return it + const char* applicationName = lJNIEnv->GetStringUTFChars(objectValue, NULL); + std::string ret(applicationName); + lJNIEnv->ReleaseStringUTFChars(objectValue, applicationName); + + return ret; +} + void* loadLibrary(const char* libraryName, JNIEnv* lJNIEnv, jobject& ObjectActivityInfo) { // Find out the absolute path of the library @@ -142,7 +179,9 @@ void ANativeActivity_onCreate(ANativeActivity* activity, void* savedState, size_ loadLibrary("openal", lJNIEnv, ObjectActivityInfo); loadLibrary("sfml-audio", lJNIEnv, ObjectActivityInfo); loadLibrary("sfml-network", lJNIEnv, ObjectActivityInfo); - void* handle = loadLibrary("sfml-example", lJNIEnv, ObjectActivityInfo); + + std::string libName = getLibraryName(lJNIEnv, ObjectActivityInfo); + void* handle = loadLibrary(libName.c_str(), lJNIEnv, ObjectActivityInfo); // todo: should we detach the current thread ? because if we do, it // crashes (lJavaVM->DetachCurrentThread();)