12#include <QtCore/private/qcoreapplication_p.h>
13#include <QtCore/private/qlocking_p.h>
15#include <android/log.h>
50 jclass jQtNative = env->FindClass(
"org/qtproject/qt/android/QtNative");
51 if (QJniEnvironment::checkAndClearExceptions(env))
54 jmethodID activityMethodID =
55 env->GetStaticMethodID(jQtNative,
"activity",
"()Landroid/app/Activity;");
56 if (QJniEnvironment::checkAndClearExceptions(env))
59 jobject activity = env->CallStaticObjectMethod(jQtNative, activityMethodID);
60 if (QJniEnvironment::checkAndClearExceptions(env))
72 env->DeleteLocalRef(activity);
75 env->DeleteLocalRef(jQtNative);
76 if (QJniEnvironment::checkAndClearExceptions(env))
83 class ActivityResultListeners
87 QList<QtAndroidPrivate::ActivityResultListener *> listeners;
96 g_activityResultListeners()->listeners.append(listener);
102 g_activityResultListeners()->listeners.removeAll(listener);
108 const QList<QtAndroidPrivate::ActivityResultListener *> &listeners = g_activityResultListeners()->listeners;
109 for (
int i=0;
i<listeners.size(); ++
i) {
110 if (listeners.at(
i)->handleActivityResult(requestCode, resultCode,
data))
116 class NewIntentListeners
120 QList<QtAndroidPrivate::NewIntentListener *> listeners;
129 g_newIntentListeners()->listeners.append(listener);
135 g_newIntentListeners()->listeners.removeAll(listener);
141 const QList<QtAndroidPrivate::NewIntentListener *> &listeners = g_newIntentListeners()->listeners;
142 for (
int i=0;
i<listeners.size(); ++
i) {
143 if (listeners.at(
i)->handleNewIntent(env, intent))
150struct GenericMotionEventListeners {
152 QList<QtAndroidPrivate::GenericMotionEventListener *> listeners;
155Q_GLOBAL_STATIC(GenericMotionEventListeners, g_genericMotionEventListeners)
159 jboolean
ret = JNI_FALSE;
161 for (
auto *listener : std::as_const(g_genericMotionEventListeners()->listeners))
162 ret |= listener->handleGenericMotionEvent(
event.object());
169struct KeyEventListeners {
171 QList<QtAndroidPrivate::KeyEventListener *> listeners;
178 jboolean
ret = JNI_FALSE;
180 for (
auto *listener : std::as_const(g_keyEventListeners()->listeners))
181 ret |= listener->handleKeyEvent(
event.object());
189 g_genericMotionEventListeners()->listeners.push_back(listener);
195 g_genericMotionEventListeners()->listeners.removeOne(listener);
201 g_keyEventListeners()->listeners.push_back(listener);
207 g_keyEventListeners()->listeners.removeOne(listener);
211 class ResumePauseListeners
215 QList<QtAndroidPrivate::ResumePauseListener *> listeners;
224 g_resumePauseListeners()->listeners.append(listener);
230 g_resumePauseListeners()->listeners.removeAll(listener);
236 const QList<QtAndroidPrivate::ResumePauseListener *> &listeners = g_resumePauseListeners()->listeners;
237 for (
int i=0;
i<listeners.size(); ++
i)
238 listeners.at(
i)->handlePause();
244 const QList<QtAndroidPrivate::ResumePauseListener *> &listeners = g_resumePauseListeners()->listeners;
245 for (
int i=0;
i<listeners.size(); ++
i)
246 listeners.at(
i)->handleResume();
253 jclass jQtNative = env->FindClass(
"org/qtproject/qt/android/QtNative");
255 if (QJniEnvironment::checkAndClearExceptions(env))
258 jmethodID activityMethodID = env->GetStaticMethodID(jQtNative,
260 "()Landroid/app/Activity;");
262 if (QJniEnvironment::checkAndClearExceptions(env))
265 jobject
activity = env->CallStaticObjectMethod(jQtNative, activityMethodID);
267 if (QJniEnvironment::checkAndClearExceptions(env))
270 jmethodID serviceMethodID = env->GetStaticMethodID(jQtNative,
272 "()Landroid/app/Service;");
274 if (QJniEnvironment::checkAndClearExceptions(env))
277 jobject
service = env->CallStaticObjectMethod(jQtNative, serviceMethodID);
279 if (QJniEnvironment::checkAndClearExceptions(env))
282 jmethodID classLoaderMethodID = env->GetStaticMethodID(jQtNative,
284 "()Ljava/lang/ClassLoader;");
286 if (QJniEnvironment::checkAndClearExceptions(env))
289 jobject
classLoader = env->CallStaticObjectMethod(jQtNative, classLoaderMethodID);
290 if (QJniEnvironment::checkAndClearExceptions(env))
304 static const JNINativeMethod
methods[] = {
308 const bool regOk = (env->RegisterNatives(jQtNative,
methods,
sizeof(
methods) /
sizeof(
methods[0])) == JNI_OK);
309 env->DeleteLocalRef(jQtNative);
310 if (!regOk && QJniEnvironment::checkAndClearExceptions(env))
315 if (!QtInputDelegate::registerNativeMethods(
318 qCritical() <<
"Failed to register natives methods for"
319 << Traits<QtInputDelegate>::className();
381 static jint sdkVersion = 0;
383 sdkVersion = QJniObject::getStaticField<jint>(
"android/os/Build$VERSION",
"SDK_INT");
389 g_waitForServiceSetupSemaphore->acquire();
403 g_waitForServiceSetupSemaphore->release();
432 static const char logTag[] =
"QtCore";
433 static bool initialized =
false;
435 return JNI_VERSION_1_6;
443 __android_log_print(ANDROID_LOG_INFO,
logTag,
"Start");
448 if (vm->GetEnv(&uenv.venv, JNI_VERSION_1_6) != JNI_OK) {
449 __android_log_print(ANDROID_LOG_FATAL,
logTag,
"GetEnv failed");
453 JNIEnv *env = uenv.nenv;
456 __android_log_print(ANDROID_LOG_FATAL,
logTag,
"initJNI failed");
460 return JNI_VERSION_1_6;
static JNINativeMethod methods[]
virtual ~ActivityResultListener()
virtual ~GenericMotionEventListener()
virtual ~KeyEventListener()
virtual ~NewIntentListener()
virtual void handlePause()
virtual void handleResume()
virtual ~ResumePauseListener()
static const char logTag[]
Combined button and popup list for selecting options.
\preliminary \inmodule QtCorePrivate
Q_CORE_EXPORT void unregisterNewIntentListener(NewIntentListener *listener)
Q_CORE_EXPORT int acuqireServiceSetup(int flags)
Q_CORE_EXPORT jobject callOnBindListener(jobject intent)
Q_CORE_EXPORT jint initJNI(JavaVM *vm, JNIEnv *env)
Q_CORE_EXPORT jint androidSdkVersion()
bool registerNativeInterfaceNatives(QJniEnvironment &env)
Posts the function runnable to the Android thread.
Q_CORE_EXPORT void unregisterResumePauseListener(ResumePauseListener *listener)
Q_CORE_EXPORT void unregisterGenericMotionEventListener(GenericMotionEventListener *listener)
Q_CORE_EXPORT bool acquireAndroidDeadlockProtector()
Q_CORE_EXPORT void unregisterKeyEventListener(KeyEventListener *listener)
Q_CORE_EXPORT QtJniTypes::Context context()
bool registerExtrasNatives(QJniEnvironment &env)
bool registerPermissionNatives(QJniEnvironment &env)
Q_CORE_EXPORT void handleResume()
Q_CORE_EXPORT void registerResumePauseListener(ResumePauseListener *listener)
Q_CORE_EXPORT QtJniTypes::Activity activity()
Q_CORE_EXPORT void releaseAndroidDeadlockProtector()
Q_CORE_EXPORT void handleNewIntent(JNIEnv *env, jobject intent)
Q_CORE_EXPORT void setOnBindListener(OnBindListener *listener)
Q_CORE_EXPORT void registerKeyEventListener(KeyEventListener *listener)
Q_CORE_EXPORT void handlePause()
Q_CORE_EXPORT void unregisterActivityResultListener(ActivityResultListener *listener)
Q_CORE_EXPORT void registerGenericMotionEventListener(GenericMotionEventListener *listener)
Q_CORE_EXPORT QtJniTypes::Service service()
Q_CORE_EXPORT JavaVM * javaVM()
Q_CORE_EXPORT void handleActivityResult(jint requestCode, jint resultCode, jobject data)
Q_CORE_EXPORT void waitForServiceSetup()
#define Q_BASIC_ATOMIC_INITIALIZER(a)
DBusConnection const char DBusError DBusBusType DBusError return DBusConnection DBusHandleMessageFunction void DBusFreeFunction return DBusConnection return DBusConnection return const char DBusError return DBusConnection DBusMessage dbus_uint32_t return DBusConnection dbus_bool_t DBusConnection DBusAddWatchFunction DBusRemoveWatchFunction DBusWatchToggledFunction void DBusFreeFunction return DBusConnection DBusDispatchStatusFunction void DBusFreeFunction DBusTimeout return DBusTimeout return DBusWatch return DBusWatch unsigned int return DBusError const DBusError return const DBusMessage return DBusMessage return DBusMessage return DBusMessage return DBusMessage return DBusMessage return DBusMessageIter int const void return DBusMessageIter DBusMessageIter return DBusMessageIter void DBusMessageIter void int return DBusMessage DBusMessageIter return DBusMessageIter return DBusMessageIter DBusMessageIter const char const char const char const char return DBusMessage return DBusMessage const char return DBusMessage dbus_bool_t return DBusMessage dbus_uint32_t return DBusMessage void
#define Q_GLOBAL_STATIC(TYPE, NAME,...)
static jobject g_jService
Q_DECLARE_JNI_NATIVE_METHOD(dispatchGenericMotionEvent)
static Q_CONSTINIT QtAndroidPrivate::OnBindListener * g_onBindListener
static jboolean dispatchGenericMotionEvent(JNIEnv *, jclass, QtJniTypes::MotionEvent event)
Q_CORE_EXPORT jobject qt_androidActivity()
static jobject g_jClassLoader
static jobject g_jActivity
QT_END_NAMESPACE JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved)
static Q_CONSTINIT QBasicMutex g_onBindListenerMutex
static Q_CONSTINIT QBasicAtomicInt g_androidDeadlockProtector
static jboolean updateNativeActivity(JNIEnv *env, jclass=nullptr)
static jboolean dispatchKeyEvent(JNIEnv *, jclass, QtJniTypes::KeyEvent event)
QT_BEGIN_NAMESPACE Q_DECLARE_JNI_CLASS(QtInputDelegate, "org/qtproject/qt/android/QtInputDelegate")
Q_CORE_EXPORT jobject qt_androidService()
static Q_CONSTINIT QBasicAtomicInt g_serviceSetupLockers
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data