QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
AndroidInit.cc
Go to the documentation of this file.
1#include <QtCore/QJniEnvironment>
2#include <QtCore/QJniObject>
3#include <QtCore/QLoggingCategory>
4
5#include "AndroidInterface.h"
6#include "AndroidSerialApp.h"
8#include "VideoBackend.h"
9
10QGC_LOGGING_CATEGORY(AndroidInitLog, "Android.AndroidInit");
11
12static jboolean jniInit(JNIEnv *env, jobject thiz)
13{
14 qCDebug(AndroidInitLog) << Q_FUNC_INFO;
15
16 const jclass context_cls = env->GetObjectClass(thiz);
17 if (!context_cls) {
18 return JNI_FALSE;
19 }
20
21 const jmethodID get_app_context_id = env->GetMethodID(context_cls, "getApplicationContext", "()Landroid/content/Context;");
22 env->DeleteLocalRef(context_cls);
23 if (QJniEnvironment::checkAndClearExceptions(env)) {
24 return JNI_FALSE;
25 }
26
27 const jobject app_context = env->CallObjectMethod(thiz, get_app_context_id);
28 if (QJniEnvironment::checkAndClearExceptions(env) || !app_context) {
29 return JNI_FALSE;
30 }
31
32 const jclass app_context_cls = env->GetObjectClass(app_context);
33 if (!app_context_cls) {
34 env->DeleteLocalRef(app_context);
35 return JNI_FALSE;
36 }
37
38 const jmethodID get_class_loader_id = env->GetMethodID(app_context_cls, "getClassLoader", "()Ljava/lang/ClassLoader;");
39 env->DeleteLocalRef(app_context_cls);
40 if (QJniEnvironment::checkAndClearExceptions(env)) {
41 env->DeleteLocalRef(app_context);
42 return JNI_FALSE;
43 }
44
45 const jobject class_loader = env->CallObjectMethod(app_context, get_class_loader_id);
46 if (QJniEnvironment::checkAndClearExceptions(env)) {
47 env->DeleteLocalRef(app_context);
48 return JNI_FALSE;
49 }
50
51 JavaVM* javaVm = nullptr;
52 if (env->GetJavaVM(&javaVm) == JNI_OK) {
53 VideoBackend::instance().setAndroidJniState(javaVm, app_context, class_loader);
54 }
55
56 env->DeleteLocalRef(app_context);
57 env->DeleteLocalRef(class_loader);
58
59 return QJniEnvironment::checkAndClearExceptions(env) ? JNI_FALSE : JNI_TRUE;
60}
61
63{
64 qCDebug(AndroidInitLog) << Q_FUNC_INFO;
65
66 const JNINativeMethod javaMethods[] {
67 {"nativeInit", "()Z", reinterpret_cast<void *>(jniInit)},
68 };
69
70 QJniEnvironment jniEnv;
71 (void) jniEnv.checkAndClearExceptions();
72
73 jclass objectClass = jniEnv->FindClass(AndroidInterface::kJniQGCActivityClassName);
74 if (!objectClass) {
75 qCWarning(AndroidInitLog) << "Couldn't find class:" << AndroidInterface::kJniQGCActivityClassName;
76 (void) jniEnv.checkAndClearExceptions();
77 return JNI_ERR;
78 }
79
80 const jint val = jniEnv->RegisterNatives(objectClass, javaMethods, std::size(javaMethods));
81 jniEnv->DeleteLocalRef(objectClass);
82 if (val < 0) {
83 qCWarning(AndroidInitLog) << "Error registering methods:" << val;
84 (void) jniEnv.checkAndClearExceptions();
85 return JNI_ERR;
86 }
87
88 qCDebug(AndroidInitLog) << "Main Native Functions Registered";
89
90 (void) jniEnv.checkAndClearExceptions();
91
92 return JNI_OK;
93}
94
95jint JNI_OnLoad(JavaVM* vm, void*)
96{
97 qCDebug(AndroidInitLog) << Q_FUNC_INFO;
98
99 VideoBackend::instance().setAndroidJniState(vm, nullptr, nullptr);
100
101 JNIEnv *env;
102 if (vm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_6) != JNI_OK) {
103 return JNI_ERR;
104 }
105
106 if (jniSetNativeMethods() != JNI_OK) {
107 return JNI_ERR;
108 }
109
111
112 // Without them the first USB serial callback would throw on a Java thread, and take the app down then. Also
113 // in a build without serial links: its Java side tracks the USB serial devices all the same, and tells of
114 // each that is attached or detached.
116 return JNI_ERR;
117 }
118
119 QNativeInterface::QAndroidApplication::hideSplashScreen(333);
120
121 return JNI_VERSION_1_6;
122}
123
124void JNI_OnUnload(JavaVM* vm, void*)
125{
126 qCDebug(AndroidInitLog) << Q_FUNC_INFO;
127
128 VideoBackend::instance().setAndroidJniState(vm, nullptr, nullptr);
129}
jint JNI_OnLoad(JavaVM *vm, void *)
void JNI_OnUnload(JavaVM *vm, void *)
static jboolean jniInit(JNIEnv *env, jobject thiz)
static jint jniSetNativeMethods()
#define QGC_LOGGING_CATEGORY(name, categoryStr)
virtual void setAndroidJniState(void *javaVm, void *appContext, void *classLoader)
static VideoBackend & instance()
constexpr const char * kJniQGCActivityClassName