21#if QT_CONFIG(clipboard)
22#include "qandroidplatformclipboard.h"
24#if QT_CONFIG(accessibility)
25#include "androidjniaccessibility.h"
30#include <android/api-level.h>
31#include <android/asset_manager_jni.h>
32#include <android/bitmap.h>
34#include <QtCore/private/qjnihelpers_p.h>
35#include <QtCore/qbasicatomic.h>
36#include <QtCore/qjnienvironment.h>
37#include <QtCore/qjniobject.h>
38#include <QtCore/qprocess.h>
39#include <QtCore/qresource.h>
40#include <QtCore/qscopeguard.h>
41#include <QtCore/qthread.h>
42#include <QtCore/private/qandroiditemmodelproxy_p.h>
43#include <QtCore/private/qandroidmodelindexproxy_p.h>
44#include <QtGui/private/qguiapplication_p.h>
45#include <QtGui/private/qhighdpiscaling_p.h>
47#include <qpa/qwindowsysteminterface.h>
50using namespace Qt::StringLiterals;
54static jclass m_applicationClass =
nullptr;
73extern "C" typedef int (*
Main)(
int,
char **);
92Q_CONSTINIT
static QBasicAtomicInt startQtAndroidPluginCalled = Q_BASIC_ATOMIC_INITIALIZER(0);
94#if QT_CONFIG(accessibility)
95Q_DECLARE_JNI_CLASS(QtAccessibilityInterface,
"org/qtproject/qt/android/QtAccessibilityInterface");
104 return &m_platformMutex;
114 if (m_pendingApplicationState == Qt::ApplicationActive)
115 QtAndroidPrivate::handleResume();
116 else if (m_pendingApplicationState == Qt::ApplicationInactive)
117 QtAndroidPrivate::handlePause();
118 QWindowSystemInterface::handleApplicationStateChanged(Qt::ApplicationState(m_pendingApplicationState));
141 for (QWindow *w : qGuiApp->allWindows()) {
144 QAndroidPlatformWindow *window =
static_cast<QAndroidPlatformWindow *>(w->handle());
145 if (window->nativeViewId() == windowId)
158 return m_assetManager;
163 return m_applicationClass;
172 JNIEnv *env = QJniEnvironment::getJniEnv();
173 auto activity = QtAndroidPrivate::activity();
174 if (activity.isValid())
175 return env->IsInstanceOf(activity.object(), m_qtActivityClass);
176 auto service = QtAndroidPrivate::service();
177 if (service.isValid())
178 return env->IsInstanceOf(QtAndroidPrivate::service().object(), m_qtServiceClass);
184#if QT_CONFIG(accessibility)
236 QJniObject::callStaticMethod<
void>(m_applicationClass,
237 "notifyNativePluginIntegrationReady",
246 if (img.format() != QImage::Format_RGBA8888 && img.format() != QImage::Format_RGB16)
247 img =
std::move(img).convertToFormat(QImage::Format_RGBA8888);
249 jobject bitmap = env->CallStaticObjectMethod(m_bitmapClass,
250 m_createBitmapMethodID,
253 img.format() == QImage::Format_RGBA8888
254 ? m_ARGB_8888_BitmapConfigValue
255 : m_RGB_565_BitmapConfigValue);
259 AndroidBitmapInfo info;
260 if (AndroidBitmap_getInfo(env, bitmap, &info) < 0) {
261 env->DeleteLocalRef(bitmap);
266 if (AndroidBitmap_lockPixels(env, bitmap, &pixels) < 0) {
267 env->DeleteLocalRef(bitmap);
271 if (info.stride == uint(img.bytesPerLine())
272 && info.width == uint(img.width())
273 && info.height == uint(img.height())) {
274 memcpy(pixels, img.constBits(), info.stride * info.height);
276 uchar *bmpPtr =
static_cast<uchar *>(pixels);
277 const unsigned width = qMin(info.width, (uint)img.width());
278 const unsigned height = qMin(info.height, (uint)img.height());
279 for (
unsigned y = 0; y < height; y++, bmpPtr += info.stride)
280 memcpy(bmpPtr, img.constScanLine(y), width);
282 AndroidBitmap_unlockPixels(env, bitmap);
288 if (format != QImage::Format_RGBA8888
289 && format != QImage::Format_RGB16)
292 return env->CallStaticObjectMethod(m_bitmapClass,
293 m_createBitmapMethodID,
296 format == QImage::Format_RGB16
297 ? m_RGB_565_BitmapConfigValue
298 : m_ARGB_8888_BitmapConfigValue);
303 if (!bitmap || !m_bitmapDrawableClass || !m_resourcesObj)
306 return env->NewObject(m_bitmapDrawableClass,
307 m_bitmapDrawableConstructorMethodID,
334 QString manufacturer = QJniObject::getStaticObjectField(
"android/os/Build",
"MANUFACTURER",
"Ljava/lang/String;").toString();
335 QString model = QJniObject::getStaticObjectField(
"android/os/Build",
"MODEL",
"Ljava/lang/String;").toString();
337 return manufacturer + u' ' + model;
342 QJniObject::callStaticMethod<
void>(m_applicationClass,
344 "(Landroid/view/View;Z)V",
351 static bool block = qEnvironmentVariableIntValue(
"QT_BLOCK_EVENT_LOOPS_WHEN_SUSPENDED");
380 QJniEnvironment qEnv;
381 if (!qEnv.isValid()) {
382 qCritical() <<
"Failed to initialize the JNI Environment";
386 if (!initJavaReferences(qEnv))
399 qCritical() <<
"Failed to init Qt application cleanup semaphores";
412 JNIEnv* env =
nullptr;
413 JavaVMAttachArgs args;
414 args.version = JNI_VERSION_1_6;
415 args.name =
"QtMainThread";
417 JavaVM *vm = QJniEnvironment::javaVM();
419 vm->AttachCurrentThread(&env, &args);
422 const QStringList argsList = QProcess::splitCommand(QJniObject(paramsString).toString());
423 const int argc = argsList.size();
424 QVarLengthArray<
char *> argv(argc + 1);
425 QList<QByteArray> argvData;
426 argvData.reserve(argc);
427 for (
int i = 0; i < argc; ++i) {
428 argvData.append(argsList.at(i).toUtf8());
429 argv[i] = argvData.back().data();
431 argv[argc] =
nullptr;
434 QDir::setCurrent(QDir::homePath());
437 void *mainLibraryHnd =
nullptr;
441 mainLibraryHnd = dlopen(argv.first(), 0);
442 if (Q_UNLIKELY(!mainLibraryHnd)) {
443 qCritical() <<
"dlopen failed:" << dlerror();
448 qWarning(
"No main library was specified; searching entire process (this is slow!)");
452 if (Q_UNLIKELY(!
m_main)) {
453 qCritical() <<
"dlsym failed:" << dlerror() << Qt::endl
454 <<
"Could not find main method";
459 qRegisterMetaType<Qt::ScreenOrientation>(
"Qt::ScreenOrientation");
462 if (QFile{QStringLiteral(
"assets:/android_rcc_bundle.rcc")}.exists())
463 QResource::registerResource(QStringLiteral(
"assets:/android_rcc_bundle.rcc"));
465 startQtAndroidPluginCalled.fetchAndAddRelease(1);
467 const int ret =
m_main(argc, argv.data());
468 qInfo() <<
"main() returned" << ret;
470 if (mainLibraryHnd) {
471 int res = dlclose(mainLibraryHnd);
473 qWarning() <<
"dlclose failed:" << dlerror();
476 QNativeInterface::QAndroidApplication::runOnAndroidMainThread([]() {
477 QtNative::callStaticMethod(
"setStarted",
false);
479 if (QtAndroid::isQtApplication()) {
481 auto activity = QtAndroidPrivate::activity();
482 if (activity.isValid())
483 activity.callMethod(
"finish");
484 auto service = QtAndroidPrivate::service();
485 if (service.isValid())
486 service.callMethod(
"stopSelf");
489 QtNative::callStaticMethod(
"terminateQtNativeApplication");
498 if (!qEnvironmentVariableIsSet(
"QT_ANDROID_NO_EXIT_CALL"))
504 if (m_applicationClass) {
505 env->DeleteGlobalRef(m_applicationClass);
506 m_applicationClass =
nullptr;
509 env->DeleteGlobalRef(m_resourcesObj);
513 env->DeleteGlobalRef(m_bitmapClass);
517 env->DeleteGlobalRef(m_ARGB_8888_BitmapConfigValue);
521 env->DeleteGlobalRef(m_RGB_565_BitmapConfigValue);
525 env->DeleteGlobalRef(m_bitmapDrawableClass);
529 env->DeleteGlobalRef(m_assets);
533 env->DeleteGlobalRef(m_qtActivityClass);
537 env->DeleteGlobalRef(m_qtServiceClass);
547 if (QtAndroidPrivate::service().isValid() && QtAndroid::isQtApplication())
548 QtAndroidPrivate::waitForServiceSetup();
555 if (QAndroidEventDispatcherStopper::instance()->stopped()
556 || QtAndroidPrivate::service().isValid()) {
557 QAndroidEventDispatcherStopper::instance()->startAll();
558 QCoreApplication::quit();
559 QAndroidEventDispatcherStopper::instance()->goingToStop(
false);
562 if (startQtAndroidPluginCalled.loadAcquire())
563 sem_wait(&m_stopQtSemaphore);
565 sem_destroy(&m_stopQtSemaphore);
567 clearJavaReferences(env);
569 m_androidPlatformIntegration =
nullptr;
570 delete m_androidAssetsFileEngineHandler;
571 m_androidAssetsFileEngineHandler =
nullptr;
572 delete m_androidContentFileEngineHandler;
573 m_androidContentFileEngineHandler =
nullptr;
574 delete m_androidApkFileEngineHandler;
575 m_androidApkFileEngineHandler =
nullptr;
576 delete m_backendRegister;
577 m_backendRegister =
nullptr;
578 sem_post(&m_exitSemaphore);
581 QtNative::callStaticMethod<QtThread>(
"getQtThread").callMethod(
"exit");
585 jint availableWidth, jint availableHeight)
587 QMutexLocker lock(&m_platformMutex);
589 const QRect availableGeometry(0, 0, availableWidth, availableHeight);
590 if (m_androidPlatformIntegration)
591 m_androidPlatformIntegration->setAvailableGeometry(availableGeometry);
592 else if (QAndroidPlatformScreen::defaultAvailableGeometry().isNull())
593 QAndroidPlatformScreen::defaultAvailableGeometry() = availableGeometry;
595Q_DECLARE_JNI_NATIVE_METHOD(handleLayoutSizeChanged)
599 QMutexLocker lock(&m_platformMutex);
608 if (state == Qt::ApplicationActive)
609 QtAndroidPrivate::handleResume();
610 else if (state == Qt::ApplicationInactive)
611 QtAndroidPrivate::handlePause();
616 if (state <= Qt::ApplicationInactive) {
626 QWindowSystemInterface::handleApplicationStateChanged(Qt::ApplicationState(state));
627 if (state == Qt::ApplicationSuspended)
631 QWindowSystemInterface::handleApplicationStateChanged(Qt::ApplicationState(state));
638 QCoreApplication::postEvent(QCoreApplication::instance(),
new QEvent(QEvent::LocaleChange));
639 QCoreApplication::postEvent(QCoreApplication::instance(),
new QEvent(QEvent::LanguageChange));
646 static const Qt::ScreenOrientation orientations[] = {
647 Qt::PortraitOrientation,
648 Qt::LandscapeOrientation,
649 Qt::InvertedPortraitOrientation,
650 Qt::InvertedLandscapeOrientation
664 Qt::ScreenOrientation screenOrientation = orientations[(nativeOrientation - 1 + newRotation) % 4];
665 Qt::ScreenOrientation native = orientations[nativeOrientation - 1];
667 QAndroidPlatformIntegration::setScreenOrientation(screenOrientation, native);
668 QMutexLocker lock(&m_platformMutex);
674 QMetaObject::invokeMethod(screen,
"setOrientation", Qt::AutoConnection,
675 Q_ARG(Qt::ScreenOrientation, screenOrientation));
679Q_DECLARE_JNI_NATIVE_METHOD(handleOrientationChanged)
683 if (m_androidPlatformIntegration)
684 m_androidPlatformIntegration->setRefreshRate(refreshRate);
686Q_DECLARE_JNI_NATIVE_METHOD(handleRefreshRateChanged)
690 if (m_androidPlatformIntegration)
691 m_androidPlatformIntegration->handleScreenAdded(displayId);
693Q_DECLARE_JNI_NATIVE_METHOD(handleScreenAdded)
697 if (m_androidPlatformIntegration)
698 m_androidPlatformIntegration->handleScreenChanged(displayId);
700Q_DECLARE_JNI_NATIVE_METHOD(handleScreenChanged)
704 if (m_androidPlatformIntegration)
705 m_androidPlatformIntegration->handleScreenRemoved(displayId);
707Q_DECLARE_JNI_NATIVE_METHOD(handleScreenRemoved)
711 QAndroidPlatformIntegration::updateColorScheme(
712 (newUiMode == 1 ) ? Qt::ColorScheme::Dark : Qt::ColorScheme::Light);
714Q_DECLARE_JNI_NATIVE_METHOD(handleUiDarkModeChanged)
720Q_DECLARE_JNI_NATIVE_METHOD(handleScreenDensityChanged)
727 QtAndroidPrivate::handleActivityResult(requestCode, resultCode, data);
732 QtAndroidPrivate::handleNewIntent(env, data);
737 return QtAndroidPrivate::callOnBindListener(intent);
741 {
"initAndroidQpaPlugin",
"()Z", (
void *)initAndroidQpaPlugin },
742 {
"startQtNativeApplication",
"(Ljava/lang/String;)V", (
void *)startQtNativeApplication },
743 {
"terminateQtNativeApplication",
"()V", (
void *)terminateQtNativeApplication },
744 {
"waitForServiceSetup",
"()V", (
void *)waitForServiceSetup },
745 {
"updateApplicationState",
"(I)V", (
void *)updateApplicationState },
746 {
"onActivityResult",
"(IILandroid/content/Intent;)V", (
void *)onActivityResult },
747 {
"onNewIntent",
"(Landroid/content/Intent;)V", (
void *)onNewIntent },
748 {
"onBind",
"(Landroid/content/Intent;)Landroid/os/IBinder;", (
void *)onBind },
749 {
"updateLocale",
"()V", (
void *)updateLocale },
752#define FIND_AND_CHECK_CLASS(CLASS_NAME) clazz
753 = env->FindClass(CLASS_NAME); if
755 __android_log_print(ANDROID_LOG_FATAL, m_qtTag, m_classErrorMsg, CLASS_NAME);
757}
759#define GET_AND_CHECK_METHOD(VAR, CLASS, METHOD_NAME, METHOD_SIGNATURE) VAR
760 = env->GetMethodID(CLASS, METHOD_NAME, METHOD_SIGNATURE); if
762 __android_log_print(ANDROID_LOG_FATAL, m_qtTag, m_methodErrorMsg, METHOD_NAME, METHOD_SIGNATURE);
764}
766#define GET_AND_CHECK_STATIC_METHOD(VAR, CLASS, METHOD_NAME, METHOD_SIGNATURE) VAR
767 = env->GetStaticMethodID(CLASS, METHOD_NAME, METHOD_SIGNATURE); if
769 __android_log_print(ANDROID_LOG_FATAL, m_qtTag, m_methodErrorMsg, METHOD_NAME, METHOD_SIGNATURE);
771}
773#define GET_AND_CHECK_FIELD(VAR, CLASS, FIELD_NAME, FIELD_SIGNATURE) VAR
774 = env->GetFieldID(CLASS, FIELD_NAME, FIELD_SIGNATURE); if
776 __android_log_print(ANDROID_LOG_FATAL, m_qtTag, m_methodErrorMsg, FIELD_NAME, FIELD_SIGNATURE);
778}
780#define GET_AND_CHECK_STATIC_FIELD(VAR, CLASS, FIELD_NAME, FIELD_SIGNATURE) VAR
781 = env->GetStaticFieldID(CLASS, FIELD_NAME, FIELD_SIGNATURE); if
783 __android_log_print(ANDROID_LOG_FATAL, m_qtTag, m_methodErrorMsg, FIELD_NAME, FIELD_SIGNATURE);
785}
791 bool success = env.registerNativeMethods(m_applicationClass,
793 success &= env.registerNativeMethods(
794 QtJniTypes::Traits<QtJniTypes::QtDisplayManager>::className(),
796 Q_JNI_NATIVE_METHOD(handleLayoutSizeChanged),
797 Q_JNI_NATIVE_METHOD(handleOrientationChanged),
798 Q_JNI_NATIVE_METHOD(handleRefreshRateChanged),
799 Q_JNI_NATIVE_METHOD(handleScreenAdded),
800 Q_JNI_NATIVE_METHOD(handleScreenChanged),
801 Q_JNI_NATIVE_METHOD(handleScreenRemoved),
802 Q_JNI_NATIVE_METHOD(handleUiDarkModeChanged),
803 Q_JNI_NATIVE_METHOD(handleScreenDensityChanged)
807 && QtAndroidInput::registerNatives(env)
808 && QtAndroidMenu::registerNatives(env)
809#if QT_CONFIG(accessibility)
810 && QtAndroidAccessibility::registerNatives(env)
812 && QtAndroidDialogHelpers::registerNatives(env)
813#if QT_CONFIG(clipboard)
814 && QAndroidPlatformClipboard::registerNatives(env)
816 && QAndroidPlatformWindow::registerNatives(env)
817 && QtAndroidWindowEmbedding::registerNatives(env)
818 && AndroidBackendRegister::registerNatives()
819 && QAndroidModelIndexProxy::registerNatives(env)
820 && QAndroidItemModelProxy::registerAbstractNatives(env)
821 && QAndroidItemModelProxy::registerProxyNatives(env);
828 if (m_applicationClass)
833 m_applicationClass =
static_cast<jclass>(env->NewGlobalRef(clazz));
838 jobject contextObject = env->CallStaticObjectMethod(m_applicationClass, methodID);
839 if (!contextObject) {
841 contextObject = env->CallStaticObjectMethod(m_applicationClass, methodID);
844 if (!contextObject) {
845 __android_log_print(ANDROID_LOG_FATAL,
"Qt",
"Failed to get Activity or Service object");
848 const auto releaseContextObject = qScopeGuard([&env, contextObject]{
849 env->DeleteLocalRef(contextObject);
854 m_assets = env->NewGlobalRef(env->CallObjectMethod(contextObject, methodID));
858 m_resourcesObj = env->NewGlobalRef(env->CallObjectMethod(contextObject, methodID));
861 m_bitmapClass =
static_cast<jclass>(env->NewGlobalRef(clazz));
863 "createBitmap",
"(IILandroid/graphics/Bitmap$Config;)Landroid/graphics/Bitmap;");
867 m_ARGB_8888_BitmapConfigValue = env->NewGlobalRef(env->GetStaticObjectField(clazz, fieldId));
869 m_RGB_565_BitmapConfigValue = env->NewGlobalRef(env->GetStaticObjectField(clazz, fieldId));
872 m_bitmapDrawableClass =
static_cast<jclass>(env->NewGlobalRef(clazz));
874 m_bitmapDrawableClass,
875 "<init>",
"(Landroid/content/res/Resources;Landroid/graphics/Bitmap;)V");
878 m_qtActivityClass =
static_cast<jclass>(env->NewGlobalRef(clazz));
880 m_qtServiceClass =
static_cast<jclass>(env->NewGlobalRef(clazz));
883 QThread::currentThread()->setObjectName(
"QtMainLoopThread");
885 QWindowSystemInterfacePrivate::TabletEvent::setPlatformSynthesizesMouse(
false);
892Q_DECL_EXPORT jint JNICALL JNI_OnLoad(JavaVM *,
void *)
894 static bool initialized =
false;
896 return JNI_VERSION_1_6;
902 if (!env.isValid()) {
903 __android_log_print(ANDROID_LOG_FATAL,
"Qt",
"Failed to initialize the JNI Environment");
907 if (!initJavaReferences(env))
910 if (!registerNatives(env)) {
911 __android_log_print(ANDROID_LOG_FATAL,
"Qt",
"registerNatives failed");
915 __android_log_print(ANDROID_LOG_INFO,
"Qt",
"Qt platform plugin started");
916 return JNI_VERSION_1_6;
#define GET_AND_CHECK_STATIC_METHOD(VAR, CLASS, METHOD_NAME, METHOD_SIGNATURE)
static bool initJavaReferences(QJniEnvironment &env)
static const char m_qtTag[]
static sem_t m_exitSemaphore
#define GET_AND_CHECK_STATIC_FIELD(VAR, CLASS, FIELD_NAME, FIELD_SIGNATURE)
static jclass m_qtActivityClass
static AndroidAssetsFileEngineHandler * m_androidAssetsFileEngineHandler
static jclass m_bitmapClass
static void terminateQtNativeApplication(JNIEnv *env, jclass)
static jclass m_bitmapDrawableClass
static void waitForServiceSetup(JNIEnv *env, jclass)
static const char m_staticFieldErrorMsg[]
static void handleLayoutSizeChanged(JNIEnv *, jclass, jint availableWidth, jint availableHeight)
static jobject m_resourcesObj
static jmethodID m_bitmapDrawableConstructorMethodID
static void handleScreenChanged(JNIEnv *, jclass, jint displayId)
static QAndroidPlatformIntegration * m_androidPlatformIntegration
static AndroidContentFileEngineHandler * m_androidContentFileEngineHandler
static bool registerNatives(QJniEnvironment &env)
static JNINativeMethod methods[]
static AndroidBackendRegister * m_backendRegister
static void onNewIntent(JNIEnv *env, jclass, jobject data)
#define FIND_AND_CHECK_CLASS(CLASS_NAME)
#define GET_AND_CHECK_METHOD(VAR, CLASS, METHOD_NAME, METHOD_SIGNATURE)
static void handleUiDarkModeChanged(JNIEnv *, jobject, jint newUiMode)
static int m_pendingApplicationState
static void handleRefreshRateChanged(JNIEnv *, jclass, jfloat refreshRate)
static QBasicMutex m_platformMutex
static void updateApplicationState(JNIEnv *, jobject, jint state)
static void clearJavaReferences(JNIEnv *env)
int(* Main)(int, char **)
static void handleOrientationChanged(JNIEnv *, jobject, jint newRotation, jint nativeOrientation)
static void startQtNativeApplication(JNIEnv *jenv, jobject object, jstring paramsString)
static const char m_methodErrorMsg[]
static bool initAndroidQpaPlugin(JNIEnv *jenv, jobject object)
static void handleScreenRemoved(JNIEnv *, jclass, jint displayId)
static void updateLocale(JNIEnv *, jobject)
static jobject m_RGB_565_BitmapConfigValue
static const char m_classErrorMsg[]
static jobject m_ARGB_8888_BitmapConfigValue
static sem_t m_stopQtSemaphore
static jclass m_qtServiceClass
static AAssetManager * m_assetManager
static jmethodID m_createBitmapMethodID
static jobject onBind(JNIEnv *, jclass, jobject intent)
static void onActivityResult(JNIEnv *, jclass, jint requestCode, jint resultCode, jobject data)
static void handleScreenDensityChanged(JNIEnv *, jclass, jdouble density)
static QAndroidApkFileEngineHandler * m_androidApkFileEngineHandler
static void handleScreenAdded(JNIEnv *, jclass, jint displayId)
static QAndroidEventDispatcherStopper * instance()
void goingToStop(bool stop)
\inmodule QtCore\reentrant
const char * classErrorMsgFmt()
void setViewVisibility(jobject view, bool visible)
void setAndroidPlatformIntegration(QAndroidPlatformIntegration *androidPlatformIntegration)
QBasicMutex * platformInterfaceMutex()
jobject createBitmap(int width, int height, QImage::Format format, JNIEnv *env)
jobject createBitmapDrawable(jobject bitmap, JNIEnv *env=nullptr)
QWindow * topLevelWindowAt(const QPoint &globalPos)
QAndroidPlatformIntegration * androidPlatformIntegration()
jobject createBitmap(QImage img, JNIEnv *env=nullptr)
AndroidBackendRegister * backendRegister()
bool blockEventLoopsWhenSuspended()
QWindow * windowFromId(int windowId)
jclass applicationClass()
void notifyNativePluginIntegrationReady(bool ready)
AAssetManager * assetManager()
const char * staticFieldErrorMsgFmt()
const char * methodErrorMsgFmt()
Q_DECLARE_JNI_CLASS(MotionEvent, "android/view/MotionEvent")