| 60 | } // namespace |
| 61 | |
| 62 | AndroidPlatformFunction::AndroidPlatformFunction(JNIEnv* env, jobject javaFunction) |
| 63 | : _jvm(nullptr), _javaFunction(nullptr), |
| 64 | _callSyncMethod(nullptr), _contextClass(nullptr), _contextConstructor(nullptr) { |
| 65 | |
| 66 | env->GetJavaVM(&_jvm); |
| 67 | |
| 68 | _javaFunction = env->NewGlobalRef(javaFunction); |
| 69 | |
| 70 | ScopedLocalRef<jclass> functionClass(env, env->GetObjectClass(javaFunction)); |
| 71 | if (functionClass.get() == nullptr) { |
| 72 | AGENUI_LOG("[AndroidPlatformFunction] constructor: failed to get function class"); |
| 73 | return; |
| 74 | } |
| 75 | |
| 76 | // Cache FunctionCallContext class and constructor |
| 77 | ScopedLocalRef<jclass> ctxClass(env, |
| 78 | env->FindClass("com/amap/agenui/function/FunctionCallContext")); |
| 79 | if (ctxClass.get() == nullptr) { |
| 80 | AGENUI_LOG("[AndroidPlatformFunction] constructor: failed to find FunctionCallContext class"); |
| 81 | return; |
| 82 | } |
| 83 | _contextClass = (jclass)env->NewGlobalRef(ctxClass.get()); |
| 84 | _contextConstructor = env->GetMethodID(_contextClass, "<init>", "(ILjava/lang/String;)V"); |
| 85 | if (_contextConstructor == nullptr) { |
| 86 | AGENUI_LOG("[AndroidPlatformFunction] constructor: failed to get FunctionCallContext constructor"); |
| 87 | return; |
| 88 | } |
| 89 | |
| 90 | // Cache callSync method ID |
| 91 | _callSyncMethod = env->GetMethodID(functionClass.get(), "callSync", |
| 92 | "(Lcom/amap/agenui/function/FunctionCallContext;Ljava/lang/String;)Ljava/lang/String;"); |
| 93 | |
| 94 | if (_callSyncMethod == nullptr) { |
| 95 | AGENUI_LOG("[AndroidPlatformFunction] constructor: failed to get method IDs"); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | AndroidPlatformFunction::~AndroidPlatformFunction() { |
| 100 | if (_jvm != nullptr) { |