12#include <QtCore/private/qcoreapplication_p.h>
13#include <QtCore/private/qlocking_p.h>
15#include <android/log.h>
46 struct GenericMotionEventListeners {
51Q_GLOBAL_STATIC(GenericMotionEventListeners, g_genericMotionEventListeners)
55 jboolean
ret = JNI_FALSE;
57 for (
auto *listener : std::as_const(g_genericMotionEventListeners()->listeners))
58 ret |= listener->handleGenericMotionEvent(
event);
63 struct KeyEventListeners {
72 jboolean
ret = JNI_FALSE;
74 for (
auto *listener : std::as_const(g_keyEventListeners()->listeners))
75 ret |= listener->handleKeyEvent(
event);
82 jclass jQtNative = env->FindClass(
"org/qtproject/qt/android/QtNative");
83 if (QJniEnvironment::checkAndClearExceptions(env))
86 jmethodID activityMethodID =
87 env->GetStaticMethodID(jQtNative,
"activity",
"()Landroid/app/Activity;");
88 if (QJniEnvironment::checkAndClearExceptions(env))
91 jobject activity = env->CallStaticObjectMethod(jQtNative, activityMethodID);
92 if (QJniEnvironment::checkAndClearExceptions(env))
104 env->DeleteLocalRef(activity);
107 env->DeleteLocalRef(jQtNative);
108 if (QJniEnvironment::checkAndClearExceptions(env))
115 class ActivityResultListeners
128 g_activityResultListeners()->listeners.append(listener);
134 g_activityResultListeners()->listeners.removeAll(listener);
141 for (
int i=0;
i<listeners.
size(); ++
i) {
142 if (listeners.
at(
i)->handleActivityResult(requestCode, resultCode,
data))
148 class NewIntentListeners
161 g_newIntentListeners()->listeners.append(listener);
167 g_newIntentListeners()->listeners.removeAll(listener);
174 for (
int i=0;
i<listeners.
size(); ++
i) {
175 if (listeners.
at(
i)->handleNewIntent(env, intent))
181 class ResumePauseListeners
194 g_resumePauseListeners()->listeners.append(listener);
200 g_resumePauseListeners()->listeners.removeAll(listener);
207 for (
int i=0;
i<listeners.
size(); ++
i)
208 listeners.
at(
i)->handlePause();
215 for (
int i=0;
i<listeners.
size(); ++
i)
216 listeners.
at(
i)->handleResume();
223 jclass jQtNative = env->FindClass(
"org/qtproject/qt/android/QtNative");
225 if (QJniEnvironment::checkAndClearExceptions(env))
228 jmethodID activityMethodID = env->GetStaticMethodID(jQtNative,
230 "()Landroid/app/Activity;");
232 if (QJniEnvironment::checkAndClearExceptions(env))
235 jobject
activity = env->CallStaticObjectMethod(jQtNative, activityMethodID);
237 if (QJniEnvironment::checkAndClearExceptions(env))
240 jmethodID serviceMethodID = env->GetStaticMethodID(jQtNative,
242 "()Landroid/app/Service;");
244 if (QJniEnvironment::checkAndClearExceptions(env))
247 jobject
service = env->CallStaticObjectMethod(jQtNative, serviceMethodID);
249 if (QJniEnvironment::checkAndClearExceptions(env))
252 jmethodID classLoaderMethodID = env->GetStaticMethodID(jQtNative,
254 "()Ljava/lang/ClassLoader;");
256 if (QJniEnvironment::checkAndClearExceptions(env))
259 jobject
classLoader = env->CallStaticObjectMethod(jQtNative, classLoaderMethodID);
260 if (QJniEnvironment::checkAndClearExceptions(env))
274 static const JNINativeMethod
methods[] = {
276 {
"dispatchKeyEvent",
"(Landroid/view/KeyEvent;)Z",
reinterpret_cast<void *
>(
dispatchKeyEvent)},
280 const bool regOk = (env->RegisterNatives(jQtNative,
methods,
sizeof(
methods) /
sizeof(
methods[0])) == JNI_OK);
281 env->DeleteLocalRef(jQtNative);
282 if (!regOk && QJniEnvironment::checkAndClearExceptions(env))
328 static jint sdkVersion = 0;
330 sdkVersion = QJniObject::getStaticField<jint>(
"android/os/Build$VERSION",
"SDK_INT");
337 g_genericMotionEventListeners()->listeners.push_back(listener);
343 g_genericMotionEventListeners()->listeners.removeOne(listener);
349 g_keyEventListeners()->listeners.push_back(listener);
355 g_keyEventListeners()->listeners.removeOne(listener);
360 g_waitForServiceSetupSemaphore->acquire();
374 g_waitForServiceSetupSemaphore->release();
403 static const char logTag[] =
"QtCore";
404 static bool initialized =
false;
406 return JNI_VERSION_1_6;
414 __android_log_print(ANDROID_LOG_INFO,
logTag,
"Start");
419 if (vm->GetEnv(&uenv.venv, JNI_VERSION_1_6) != JNI_OK) {
420 __android_log_print(ANDROID_LOG_FATAL,
logTag,
"GetEnv failed");
424 JNIEnv *env = uenv.nenv;
427 __android_log_print(ANDROID_LOG_FATAL,
logTag,
"initJNI failed");
431 return JNI_VERSION_1_6;
static JNINativeMethod methods[]
bool testAndSetAcquire(T expectedValue, T newValue) noexcept
void storeRelease(T newValue) noexcept
qsizetype size() const noexcept
const_reference at(qsizetype i) const noexcept
virtual ~ActivityResultListener()
virtual ~GenericMotionEventListener()
virtual ~KeyEventListener()
virtual ~NewIntentListener()
virtual jobject onBind(jobject intent)=0
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()
Q_CORE_EXPORT void unregisterResumePauseListener(ResumePauseListener *listener)
Q_CORE_EXPORT void unregisterGenericMotionEventListener(GenericMotionEventListener *listener)
Q_CORE_EXPORT bool acquireAndroidDeadlockProtector()
bool registerPermissionNatives()
Q_CORE_EXPORT void unregisterKeyEventListener(KeyEventListener *listener)
Q_CORE_EXPORT QtJniTypes::Context context()
bool registerNativeInterfaceNatives()
Posts the function runnable to the Android thread.
Q_CORE_EXPORT void handleResume()
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 jboolean dispatchGenericMotionEvent(JNIEnv *, jclass, jobject event)
static jobject g_jService
static Q_CONSTINIT QtAndroidPrivate::OnBindListener * g_onBindListener
static jobject g_jClassLoader
static jobject g_jActivity
QT_END_NAMESPACE JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved)
static jboolean dispatchKeyEvent(JNIEnv *, jclass, jobject event)
static Q_CONSTINIT QBasicMutex g_onBindListenerMutex
static Q_CONSTINIT QBasicAtomicInt g_androidDeadlockProtector
static jboolean updateNativeActivity(JNIEnv *env, jclass=nullptr)
static Q_CONSTINIT QBasicAtomicInt g_serviceSetupLockers
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
QT_END_NAMESPACE typedef QT_PREPEND_NAMESPACE(quintptr) WId