|
1 | | -#include "android.h" |
2 | 1 | #include "../simutrans/sys/simsys.h" |
| 2 | +#include "../simutrans/simdebug.h" |
| 3 | + |
| 4 | +#include <jni.h> |
| 5 | +#include <android/log.h> |
| 6 | +#include <string> |
| 7 | + |
| 8 | +#include "android.h" |
| 9 | + |
| 10 | +// Function to find the class and method ID (Must call this once on init because otherwise not found) => cache the results! |
| 11 | +// for some reason (maybe because main SDL runs in other thread) this works only for static functions |
| 12 | +static struct MethodIDs { |
| 13 | + jmethodID downloadfile; |
| 14 | + jclass myClass; |
| 15 | +} methodIDs; |
| 16 | + |
| 17 | +bool getMethodIDs(JNIEnv *env, jobject thisClass) { |
| 18 | + // Find the Java class |
| 19 | + jclass cls = env->FindClass("com/simutrans/Simutrans"); // Use fully qualified name |
| 20 | + if (cls == nullptr) { |
| 21 | + dbg->fatal("getMethodIDs()","Class not found"); |
| 22 | + return false; // Class not found |
| 23 | + } |
| 24 | + |
| 25 | + methodIDs.myClass = static_cast<jclass>(env->NewGlobalRef(cls)); // Prevent GC |
| 26 | + |
| 27 | + // Find the instance method |
| 28 | + methodIDs.downloadfile = env->GetStaticMethodID(methodIDs.myClass, "downloadFile", "(Ljava/lang/String;Ljava/lang/String;)V"); |
| 29 | + if (methodIDs.downloadfile == nullptr) { |
| 30 | + env->ExceptionClear(); // Clear the exception if the method is not found |
| 31 | + __android_log_print(ANDROID_LOG_ERROR, "Simutrans", "methodIDs.downloadfile not found!"); |
| 32 | + return false; // Method not found |
| 33 | + } |
| 34 | + __android_log_print(ANDROID_LOG_INFO, "Simutrans", "methodIDs.downloadfile %p", methodIDs.downloadfile); |
| 35 | + |
| 36 | + return true; |
| 37 | +} |
3 | 38 |
|
4 | 39 | extern "C" { |
5 | | -JNIEXPORT jstring JNICALL Java_com_simutrans_Simutrans_getVersion(JNIEnv* env, jobject thisObject) { |
6 | | - return env->NewStringUTF(get_version()); |
| 40 | + |
| 41 | +// get the version but |
| 42 | +JNIEXPORT jstring JNICALL Java_com_simutrans_Simutrans_getVersion(JNIEnv* env, jobject thisClass) |
| 43 | +{ |
| 44 | + // now we have a valid class reference => use this to get the static memberIDs ... |
| 45 | + getMethodIDs(env,thisClass); |
| 46 | + return env->NewStringUTF(get_version()); |
| 47 | +} |
7 | 48 | } |
8 | 49 |
|
| 50 | + |
| 51 | +#include <SDL_system.h> |
| 52 | + |
| 53 | +const char *download_file(const char *url, const char *filename) |
| 54 | +{ |
| 55 | + const char * error_msg = NULL; |
| 56 | + JNIEnv *env = (JNIEnv *)SDL_AndroidGetJNIEnv(); |
| 57 | + |
| 58 | + jstring jurl = env->NewStringUTF(url); |
| 59 | + jstring jfilename = env->NewStringUTF(filename); |
| 60 | + |
| 61 | + |
| 62 | + // Call the Java method |
| 63 | + env->CallStaticVoidMethod(methodIDs.myClass, methodIDs.downloadfile, jurl, jfilename); |
| 64 | + |
| 65 | + // Check for exceptions |
| 66 | + if (env->ExceptionCheck()) { |
| 67 | + jthrowable exception = env->ExceptionOccurred(); |
| 68 | + env->ExceptionDescribe(); |
| 69 | + env->ExceptionClear(); |
| 70 | + |
| 71 | + jclass exceptionCls = env->GetObjectClass(exception); |
| 72 | + jmethodID getMessage = env->GetMethodID(exceptionCls, "getMessage", "()Ljava/lang/String;"); |
| 73 | + jstring message = (jstring)env->CallObjectMethod(exception, getMessage); |
| 74 | + error_msg = env->GetStringUTFChars(message, nullptr); |
| 75 | + __android_log_print(ANDROID_LOG_INFO, "Simutrans", "downloadfile from %s failed with %s", url, error_msg); |
| 76 | + } |
| 77 | + |
| 78 | + env->DeleteLocalRef(jurl); |
| 79 | + env->DeleteLocalRef(jfilename); |
| 80 | + |
| 81 | + return error_msg; |
9 | 82 | } |
| 83 | + |
| 84 | + |
0 commit comments