Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qandroidplatformwindow.cpp
Go to the documentation of this file.
1// Copyright (C) 2014 BogDan Vatra <bogdan@kde.org>
2// Copyright (C) 2016 The Qt Company Ltd.
3// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
4// Qt-Security score:significant reason:default
5
10
11#include "androidjnimain.h"
12
13#include <qguiapplication.h>
14#include <qpa/qwindowsysteminterface.h>
15#include <qthread.h>
16#include <private/qhighdpiscaling_p.h>
17#include <private/qjnihelpers_p.h>
18
19#include <android/native_window.h>
20#include <android/native_window_jni.h>
21
22#include <memory>
23#include <optional>
24
26
27Q_LOGGING_CATEGORY(lcQpaWindow, "qt.qpa.window")
28
29Q_DECLARE_JNI_CLASS(QtWindowInterface, "org/qtproject/qt/android/QtWindowInterface")
30Q_DECLARE_JNI_CLASS(QtInputInterface, "org/qtproject/qt/android/QtInputInterface")
31Q_DECLARE_JNI_CLASS(QtInputConnectionListener,
32 "org/qtproject/qt/android/QtInputConnection$QtInputConnectionListener")
33Q_DECLARE_JNI_CLASS(QtDisplayManager, "org/qtproject/qt/android/QtWindowInterface")
34Q_DECLARE_JNI_CLASS(QtWindowInsetsController, "org/qtproject/qt/android/QtWindowInsetsController")
35
36QAndroidPlatformWindow::QAndroidPlatformWindow(QWindow *window)
37 : QPlatformWindow(window), m_nativeQtWindow(nullptr),
38 m_surfaceContainerType(SurfaceContainer::TextureView), m_nativeParentQtWindow(nullptr)
39{
40 if (window->surfaceType() == QSurface::RasterSurface)
41 window->setSurfaceType(QSurface::OpenGLSurface);
42}
43
45{
47 return;
48
49 QWindow *window = QPlatformWindow::window();
50
51 if (parent()) {
52 QAndroidPlatformWindow *androidParent = static_cast<QAndroidPlatformWindow*>(parent());
53 if (!androidParent->isEmbeddingContainer())
54 m_nativeParentQtWindow = androidParent->nativeWindow();
55 }
56
58 QtJniTypes::QtInputConnectionListener listener =
59 reg->callInterface<QtJniTypes::QtInputInterface, QtJniTypes::QtInputConnectionListener>(
60 "getInputConnectionListener");
61
62 m_nativeQtWindow = QJniObject::construct<QtJniTypes::QtWindow>(
63 QNativeInterface::QAndroidApplication::context(),
64 isForeignWindow(), m_nativeParentQtWindow, listener);
65 m_nativeViewId = m_nativeQtWindow.callMethod<jint>("getId");
66
67 // the surfaceType is overwritten in QAndroidPlatformOpenGLWindow ctor so let's save
68 // the fact that it's a raster window for now
69 m_isRaster = window->surfaceType() == QSurface::RasterSurface;
70
71 // the following is in relation to the virtual geometry
72 const bool forceMaximize = window->windowStates() & (Qt::WindowMaximized | Qt::WindowFullScreen);
73 const QRect nativeScreenGeometry = platformScreen()->availableGeometry();
74 if (forceMaximize) {
75 setGeometry(nativeScreenGeometry);
76 } else {
77 const QRect requestedNativeGeometry = QHighDpi::toNativePixels(window->geometry(), window);
78 const QRect availableDeviceIndependentGeometry = (window->parent())
79 ? window->parent()->geometry()
80 : QHighDpi::fromNativePixels(nativeScreenGeometry, window);
81 // initialGeometry returns in native pixels
82 const QRect finalNativeGeometry = QPlatformWindow::initialGeometry(
83 window, requestedNativeGeometry, availableDeviceIndependentGeometry.width(),
84 availableDeviceIndependentGeometry.height());
85 setGeometry(finalNativeGeometry);
86 }
87
88 if (window->isTopLevel())
89 platformScreen()->addWindow(this);
90
91 static bool ok = false;
92 static const int value = qEnvironmentVariableIntValue("QT_ANDROID_SURFACE_CONTAINER_TYPE", &ok);
93 if (ok) {
94 static const SurfaceContainer type = static_cast<SurfaceContainer>(value);
97 } else if (platformScreen()->windows().size() <= 1) {
98 // TODO should handle case where this changes at runtime -> need to change existing window
99 // into TextureView (or perhaps not, if the parent window would be SurfaceView, as long as
100 // onTop was false it would stay below the children)
102 }
103 qCDebug(lcQpaWindow) << "Window" << m_nativeViewId << "using surface container type"
104 << static_cast<int>(m_surfaceContainerType);
105
106 // Attach the container now so showing the window needs no extra layout pass.
107 m_nativeQtWindow.callMethod<void>(
108 "configureSurface", bool(window->flags() & Qt::WindowStaysOnTopHint),
109 !format().hasAlpha() && qFuzzyCompare(window->opacity(), qreal(1.0)),
110 m_surfaceContainerType);
111 updateNativeWindow(window->isVisible());
112}
113
115{
117 if (window()->isTopLevel())
118 platformScreen()->removeWindow(this);
119}
120
122{
123 if (m_nativeParentQtWindow.isValid()) {
124 m_nativeParentQtWindow.callMethod<void>("bringChildToBack", nativeViewId());
125 return;
126 }
127 platformScreen()->lower(this);
128}
129
131{
132 if (m_nativeParentQtWindow.isValid()) {
133 m_nativeParentQtWindow.callMethod<void>("bringChildToFront", nativeViewId());
134 return;
135 }
136 applySystemUi(window()->windowStates(), window()->flags());
137 platformScreen()->raise(this);
138}
139
141{
142 return m_safeAreaMargins;
143}
144
145void QAndroidPlatformWindow::setSafeAreaMargins(const QMargins safeMargins)
146{
147 m_safeAreaMargins = safeMargins;
148}
149
150void QAndroidPlatformWindow::setGeometry(const QRect &rect)
151{
152 QWindowSystemInterface::handleGeometryChange(window(), rect);
154 updateNativeWindow(window()->isVisible());
155}
156
158{
159 // The shell gives a safe area only to top-level windows and windows
160 // embedded in a native view. Qt derives it for any other window.
161 const QWindow *parent = window()->parent();
162 return parent && parent->handle()
163 && !static_cast<const QAndroidPlatformWindow *>(parent->handle())->isForeignWindow();
164}
165
167{
169 // Clipped child edges cannot add to the parent's unsafe area.
170 const auto *parent = static_cast<const QAndroidPlatformWindow *>(window()->parent()->handle());
171 const QMargins parentMargins = parent->safeAreaMargins();
172 const QSize parentSize = parent->geometry().size();
173 const QRect rect = geometry();
174 const QMargins margins(
175 qBound(0, parentMargins.left() - rect.x(), parentMargins.left()),
176 qBound(0, parentMargins.top() - rect.y(), parentMargins.top()),
177 qBound(0, parentMargins.right() - (parentSize.width() - rect.x() - rect.width()),
178 parentMargins.right()),
179 qBound(0, parentMargins.bottom() - (parentSize.height() - rect.y() - rect.height()),
180 parentMargins.bottom()));
181 if (margins != m_safeAreaMargins) {
182 m_safeAreaMargins = margins;
184 }
185 }
186 for (QObject *object : window()->children()) {
187 auto *child = qobject_cast<QWindow *>(object);
188 if (child && child->handle())
189 static_cast<QAndroidPlatformWindow *>(child->handle())->updateDerivedSafeAreaMargins();
190 }
191}
192
194{
196 return;
197 // A maximized or full screen window fills the root layout, so Android
198 // resizes it in the pass that rotates, before its Surface is redrawn.
199 const bool fillsLayout = window()->isTopLevel()
200 && window()->windowStates() & (Qt::WindowMaximized | Qt::WindowFullScreen);
201 const QRect rect = fillsLayout ? QRect() : geometry();
202 jint x = 0;
203 jint y = 0;
204 jint w = -1;
205 jint h = -1;
206 if (!rect.isNull()) {
207 x = rect.x();
208 y = rect.y();
209 w = rect.width();
210 h = rect.height();
211 }
212 m_nativeQtWindow.callMethod<void>("updateWindow", x, y, w, h, visible);
213}
214
216{
218 return;
219
220 if (window()->isTopLevel()) {
221 if (!visible && window() == qGuiApp->focusWindow()) {
222 platformScreen()->topVisibleWindowChanged();
223 } else {
224 const Qt::WindowStates states = window()->windowStates();
225 const Qt::WindowFlags flags = window()->flags();
226 applySystemUi(states, flags);
227 if (states & Qt::WindowFullScreen
228 || ((states & Qt::WindowMaximized) && (flags & Qt::ExpandedClientAreaHint)))
229 setGeometry(platformScreen()->geometry());
230 else if (states & Qt::WindowMaximized)
231 setGeometry(platformScreen()->availableGeometry());
233 }
234 }
235
236 if (visible && !isEmbeddingContainer()) {
237 const bool onTop = window()->flags() & Qt::WindowStaysOnTopHint;
238 const bool isOpaque = !format().hasAlpha()
239 && qFuzzyCompare(window()->opacity(), qreal(1.0));
240 m_nativeQtWindow.callMethod<void>("configureSurface", onTop, isOpaque,
241 m_surfaceContainerType);
242 }
244
245 if (geometry().isEmpty() || screen()->availableGeometry().isEmpty())
246 return;
247
248 QPlatformWindow::setVisible(visible);
250}
251
252void QAndroidPlatformWindow::setWindowState(Qt::WindowStates state)
253{
254 QPlatformWindow::setWindowState(state);
255
256 if (window()->isVisible())
257 applySystemUi(state, window()->flags());
258 // Whether the window fills the root layout depends on the state.
259 updateNativeWindow(window()->isVisible());
260}
261
262void QAndroidPlatformWindow::setWindowFlags(Qt::WindowFlags flags)
263{
264 QPlatformWindow::setWindowFlags(flags);
265
266 if (window()->isVisible())
267 applySystemUi(window()->windowStates(), flags);
268}
269
270void QAndroidPlatformWindow::setParent(const QPlatformWindow *window)
271{
272 using namespace QtJniTypes;
273
274 if (window) {
275 auto androidWindow = static_cast<const QAndroidPlatformWindow*>(window);
276 if (androidWindow->isEmbeddingContainer())
277 return;
278 // If we were a top level window, remove from screen
279 if (!m_nativeParentQtWindow.isValid())
280 platformScreen()->removeWindow(this);
281
282 const QtWindow parentWindow = androidWindow->nativeWindow();
283 // If this was a child window of another window, the java method takes care of that
284 m_nativeQtWindow.callMethod<void, QtWindow>("setParent", parentWindow.object());
285 m_nativeParentQtWindow = parentWindow;
286 } else if (QtAndroid::isQtApplication()) {
287 m_nativeQtWindow.callMethod<void, QtWindow>("setParent", nullptr);
288 m_nativeParentQtWindow = QJniObject();
289 platformScreen()->addWindow(this);
290 }
292}
293
295{
296 return m_nativeQtWindow.isValid() ? reinterpret_cast<WId>(m_nativeQtWindow.object()) : 0L;
297}
298
300{
301 return static_cast<QAndroidPlatformScreen *>(window()->screen()->handle());
302}
303
305{
306 //shut up warning from default implementation
307}
308
310{
312 return;
313 raise();
314 // For child windows, raise() only updates "Z-order", so focus must be requested explicitly.
315 if (m_nativeParentQtWindow.isValid())
316 QWindowSystemInterface::handleFocusWindowChanged(window(), Qt::ActiveWindowFocusReason);
317}
318
319namespace {
320// The extended system UI modes Qt can take ownership of. Plain "normal" is not
321// a mode Qt owns, it is the absence of ownership, represented by an empty
322// optional, so window state the app manages itself is never touched by Qt.
323enum class SystemUiMode { Expanded, FullScreen };
324
325std::optional<SystemUiMode> systemUiModeFor(Qt::WindowStates states, Qt::WindowFlags flags)
326{
327 if (states & Qt::WindowFullScreen)
328 return SystemUiMode::FullScreen;
329 if (flags & Qt::ExpandedClientAreaHint)
330 return SystemUiMode::Expanded;
331 return std::nullopt;
332}
333
334// The mode Qt currently owns for the activity window, only ever touched on the
335// gui thread. Empty means Qt owns nothing and leaves the system UI to the app.
336std::optional<SystemUiMode> g_ownedSystemUiMode;
337
338void applySystemUiMode(std::optional<SystemUiMode> mode)
339{
340 g_ownedSystemUiMode = mode;
341
342 auto iface = qGuiApp->nativeInterface<QNativeInterface::QAndroidApplication>();
343 iface->runOnAndroidMainThread([mode, iface]() {
344 using namespace QtJniTypes;
345 auto activity = iface->context().object<Activity>();
346 if (mode == SystemUiMode::FullScreen)
347 QtWindowInsetsController::callStaticMethod("showFullScreen", activity);
348 else if (mode == SystemUiMode::Expanded)
349 QtWindowInsetsController::callStaticMethod("showExpanded", activity);
350 else
351 QtWindowInsetsController::callStaticMethod("showNormal", activity);
352 });
353}
354} // unnamed namespace
355
356void QAndroidPlatformWindow::applySystemUi(Qt::WindowStates states, Qt::WindowFlags flags)
357{
358 const bool isNonRegularWindow = flags & (Qt::Popup | Qt::Dialog | Qt::Sheet) & ~Qt::Window;
359 if (isNonRegularWindow)
360 return;
361
362 const std::optional<SystemUiMode> mode = systemUiModeFor(states, flags);
363 if (mode != g_ownedSystemUiMode)
364 applySystemUiMode(mode);
365}
366
367void QAndroidPlatformWindow::applySystemUiNative(JNIEnv *env, jobject object)
368{
369 Q_UNUSED(env)
370 Q_UNUSED(object)
371
372 if (!qGuiApp)
373 return;
374
375 // Some lifecycle events reset window state on the OS side. Re-assert the
376 // mode Qt owns, if any. When Qt owns nothing the app keeps its own state.
377 QMetaObject::invokeMethod(qGuiApp, [] {
378 if (g_ownedSystemUiMode)
379 applySystemUiMode(g_ownedSystemUiMode);
380 });
381}
382
384{
385 m_nativeQtWindow.callMethod<void>("updateFocusedEditText");
386}
387
389{
390 // Not exposed until Android provides a Surface, so QRhi never starts
391 // without a GPU surface. A child is on screen only while its parent is.
392 const QWindow *parent = window()->parent();
393 const auto state = qApp->applicationState();
394 return m_surfaceValid
395 && (!parent || !parent->handle() || parent->handle()->isExposed())
396 && (state == Qt::ApplicationInactive || state == Qt::ApplicationActive)
397 && window()->isVisible()
398 && !window()->geometry().isEmpty();
399}
400
402{
403 QRegion region;
404 if (isExposed())
405 region = QRect(QPoint(), geometry().size());
406
407 QWindowSystemInterface::handleExposeEvent(window(), region);
409 QWindowSystemInterface::flushWindowSystemEvents();
410}
411
413{
414 // Child exposure depends on this window's, so it changes with it.
415 for (QObject *object : window()->children()) {
416 auto *child = qobject_cast<QWindow *>(object);
417 if (!child || !child->handle())
418 continue;
419 auto *platformChild = static_cast<QAndroidPlatformWindow *>(child->handle());
420 QRegion region;
421 if (platformChild->isExposed())
422 region = QRect(QPoint(), platformChild->geometry().size());
423 QWindowSystemInterface::handleExposeEvent(child, region);
424 platformChild->sendChildExposeEvents();
425 }
426}
427
429{
430 if (m_nativeWindow || QThread::currentThread() != qApp->thread())
431 return;
432 if (!m_nativeQtWindow.isValid())
433 return;
434 // Ask Java for the Surface directly instead of waiting for the queued
435 // surfaceCreated, which would re-enter user code from inside makeCurrent().
436 auto surface = m_nativeQtWindow.callMethod<QtJniTypes::Surface>("getSurface");
437 if (surface.isValid() && surface.callMethod<jboolean>("isValid")) {
438 if (ANativeWindow *native = ANativeWindow_fromSurface(
439 QJniEnvironment::getJniEnv(), surface.object())) {
440 m_nativeWindow = native;
441 }
442 }
443}
444
445void QAndroidPlatformWindow::onSurfaceCreated(QtJniTypes::Surface surface)
446{
447 qCDebug(lcQpaWindow) << "onSurfaceCreated() for windowId" << m_nativeViewId;
448 if (isForeignWindow() || isEmbeddingContainer() || !surface.isValid())
449 return;
450
451 ANativeWindow *native = ANativeWindow_fromSurface(QJniEnvironment::getJniEnv(),
452 surface.object());
453 if (!native) {
454 qCWarning(lcQpaWindow) << "ANativeWindow_fromSurface returned null for windowId"
456 return;
457 }
458
459 if (native == m_nativeWindow) {
460 // Already fetched by ensureNativeWindow() or re-sent by
461 // Android, so keep the GPU surface and drop the extra reference.
462 ANativeWindow_release(native);
463 } else {
464 if (m_nativeWindow) {
465 // New Surface without a surfaceDestroyed. Release the old GPU
466 // surface first, else eglCreateWindowSurface() hits EGL_BAD_ALLOC.
467 QPlatformSurfaceEvent surfaceEvent(QPlatformSurfaceEvent::SurfaceAboutToBeDestroyed);
468 QGuiApplication::sendEvent(window(), &surfaceEvent);
470 }
471 m_nativeWindow = native;
472
473 // Pairs with SurfaceAboutToBeDestroyed. QWidget recreates its native
474 // state (winId) only on SurfaceCreated.
475 QPlatformSurfaceEvent surfaceEvent(QPlatformSurfaceEvent::SurfaceCreated);
476 QGuiApplication::sendEvent(window(), &surfaceEvent);
477 }
478
479 m_surfaceValid = true;
480
481 // Without a non-empty expose the QWindow never becomes exposed and
482 // renders nothing.
483 QWindowSystemInterface::handleExposeEvent(window(),
484 QRegion(QRect(QPoint(), geometry().size())));
486}
487
489{
490 qCDebug(lcQpaWindow) << "onRedrawNeeded() for windowId" << m_nativeViewId;
491 // Synchronous, so the frame for the current size is done when this returns.
492 if (isExposed()) {
493 QWindowSystemInterface::handleExposeEvent<QWindowSystemInterface::SynchronousDelivery>(
494 window(), QRegion(QRect(QPoint(), geometry().size())));
495 }
496}
497
499{
500 qCDebug(lcQpaWindow) << "onSurfaceDestroyed() for windowId" << m_nativeViewId;
501 if (isForeignWindow() || isEmbeddingContainer())
502 return;
503
504 // Unexpose first so the scene graph stops scheduling frames and doesn't
505 // rebuild its swapchain against the missing surface.
506 m_surfaceValid = false;
507 QWindowSystemInterface::handleExposeEvent<QWindowSystemInterface::SynchronousDelivery>(
508 window(), QRegion());
510
511 // Returns once the scene graph released its swapchain, so the render thread
512 // no longer uses the GPU surface.
513 QPlatformSurfaceEvent surfaceEvent(QPlatformSurfaceEvent::SurfaceAboutToBeDestroyed);
514 QGuiApplication::sendEvent(window(), &surfaceEvent);
515
517}
518
520{
522 if (m_nativeWindow) {
523 ANativeWindow_release(m_nativeWindow);
524 m_nativeWindow = nullptr;
525 }
526}
527
529{
531 // Embedding containers never construct the Java window, so there is
532 // nothing to destroy.
533 if (m_nativeQtWindow.isValid())
534 m_nativeQtWindow.callMethod<void>("destroySurface");
535}
536
538{
539 QWindow *modalWindow = QGuiApplication::modalWindow();
540 return modalWindow && modalWindow != window();
541}
542
544{
545 // Returns true if the window is a wrapper for a foreign window solely to allow embedding Qt
546 // into a native Android app, in which case we should not try to control it more than we "need" to
547 return !QtAndroid::isQtApplication() && window()->isTopLevel();
548}
549
550QAndroidPlatformWindow *QAndroidPlatformWindow::findWindowById(jint windowId)
551{
552 const QList<QWindow*> windows = qGuiApp->allWindows();
553 for (QWindow *w : windows) {
554 if (!w->handle())
555 continue;
556 auto *pw = static_cast<QAndroidPlatformWindow *>(w->handle());
557 if (pw->nativeViewId() == windowId)
558 return pw;
559 }
560 return nullptr;
561}
562
563void QAndroidPlatformWindow::surfaceCreated(JNIEnv *env, jobject object, jint windowId,
564 QtJniTypes::Surface surface)
565{
566 Q_UNUSED(env)
567 Q_UNUSED(object)
568 if (!qGuiApp)
569 return;
570 // Queued, not blocking, so the Android UI thread keeps compositing.
571 // Blocking here showed black bands flickering during transitions.
572 QJniObject keepalive(surface);
573 QMetaObject::invokeMethod(qApp, [windowId, keepalive]() {
574 if (auto *pw = findWindowById(windowId))
575 pw->onSurfaceCreated(QtJniTypes::Surface(keepalive));
576 }, Qt::QueuedConnection);
577}
578
579void QAndroidPlatformWindow::surfaceRedrawNeeded(JNIEnv *env, jobject object, jint windowId,
580 QtJniTypes::Runnable drawingFinished)
581{
582 Q_UNUSED(env)
583 Q_UNUSED(object)
584 QJniObject finished(drawingFinished);
585 // A parked Qt thread can't draw, and Android must not wait for it.
586 if (!qGuiApp || (QtAndroid::blockEventLoopsWhenSuspended()
587 && QAndroidEventDispatcherStopper::stopped())) {
588 finished.callMethod<void>("run");
589 return;
590 }
591 // Android keeps the old frame until drawingFinished runs, which may happen
592 // on any thread, so no wait here.
593 QMetaObject::invokeMethod(qApp, [windowId, finished]() {
594 if (auto *pw = findWindowById(windowId))
595 pw->onRedrawNeeded();
596 finished.callMethod<void>("run");
597 }, Qt::QueuedConnection);
598}
599
600void QAndroidPlatformWindow::surfaceDestroyed(JNIEnv *env, jobject object, jint windowId,
601 QtJniTypes::Runnable surfaceReleased)
602{
603 Q_UNUSED(env)
604 Q_UNUSED(object)
605 QJniObject released(surfaceReleased);
606 const auto finish = [released] {
607 if (released.isValid())
608 released.callMethod<void>("run");
609 };
610 if (!qGuiApp) {
611 finish();
612 return;
613 }
614 // Keep the producer alive until GUI-thread teardown, including after resume.
615 QMetaObject::invokeMethod(qApp, [windowId, finish]() {
616 if (auto *pw = findWindowById(windowId))
617 pw->onSurfaceDestroyed();
618 finish();
619 }, Qt::QueuedConnection);
620}
621
622void QAndroidPlatformWindow::windowFocusChanged(JNIEnv *env, jobject object,
623 jboolean focus, jint windowId)
624{
625 Q_UNUSED(env)
626 Q_UNUSED(object)
627 QWindow* window = QtAndroid::windowFromId(windowId);
628 if (!window) {
629 qCWarning(lcQpaWindow,
630 "windowFocusChanged event received for non-existing window %d", windowId);
631 return;
632 }
633
634 if (focus) {
635 QWindowSystemInterface::handleFocusWindowChanged(window);
636 } else if (!focus && window == qGuiApp->focusWindow()) {
637 // Clear focus if current window has lost focus
638 QWindowSystemInterface::handleFocusWindowChanged(nullptr);
639 }
640}
641
642void QAndroidPlatformWindow::safeAreaMarginsChanged(JNIEnv *env, jobject object,
643 QtJniTypes::Insets insets, jint id)
644{
645 Q_UNUSED(env)
646 Q_UNUSED(object)
647
648 if (!qGuiApp)
649 return;
650
651 if (!insets.isValid())
652 return;
653
654 const QMargins safeMargins(insets.getField<int>("left"),
655 insets.getField<int>("top"),
656 insets.getField<int>("right"),
657 insets.getField<int>("bottom"));
658
659 // Queued so margins stay ordered with the geometry events they belong to.
660 QMetaObject::invokeMethod(qApp, [id, safeMargins]() {
661 QAndroidPlatformWindow *pw = findWindowById(id);
662 // A child of a Qt window derives its margins, the layout can be stale.
663 if (!pw || pw->isChildOfQtWindow())
664 return;
665 if (safeMargins != pw->safeAreaMargins()) {
666 pw->setSafeAreaMargins(safeMargins);
667 QWindowSystemInterface::handleSafeAreaMarginsChanged(pw->window());
668 }
669 pw->updateDerivedSafeAreaMargins();
670 }, Qt::QueuedConnection);
671}
672
673static void updateWindows(JNIEnv *env, jobject object)
674{
675 Q_UNUSED(env)
676 Q_UNUSED(object)
677
678 if (QGuiApplication::instance() != nullptr) {
679 const auto tlw = QGuiApplication::topLevelWindows();
680 for (QWindow *w : tlw) {
681
682 // Skip non-platform windows, e.g., offscreen windows.
683 if (!w->handle())
684 continue;
685
686 const QRect availableGeometry = w->screen()->availableGeometry();
687 const QRect geometry = w->geometry();
688 const bool isPositiveGeometry = (geometry.width() > 0 && geometry.height() > 0);
689 const bool isPositiveAvailableGeometry =
690 (availableGeometry.width() > 0 && availableGeometry.height() > 0);
691
692 if (isPositiveGeometry && isPositiveAvailableGeometry) {
693 const QRegion region = QRegion(QRect(QPoint(), w->geometry().size()));
694 QWindowSystemInterface::handleExposeEvent(w, region);
695 }
696 }
697 }
698}
699Q_DECLARE_JNI_NATIVE_METHOD(updateWindows)
700
701bool QAndroidPlatformWindow::registerNatives(QJniEnvironment &env)
702{
703 if (!env.registerNativeMethods(QtJniTypes::Traits<QtJniTypes::QtWindow>::className(),
704 {
705 Q_JNI_NATIVE_METHOD(updateWindows),
706 Q_JNI_NATIVE_SCOPED_METHOD(surfaceCreated, QAndroidPlatformWindow),
707 Q_JNI_NATIVE_SCOPED_METHOD(surfaceRedrawNeeded, QAndroidPlatformWindow),
708 Q_JNI_NATIVE_SCOPED_METHOD(surfaceDestroyed, QAndroidPlatformWindow),
709 Q_JNI_NATIVE_SCOPED_METHOD(windowFocusChanged, QAndroidPlatformWindow),
710 Q_JNI_NATIVE_SCOPED_METHOD(safeAreaMarginsChanged, QAndroidPlatformWindow)
711 })) {
712 qCCritical(lcQpaWindow) << "RegisterNatives failed for"
713 << QtJniTypes::Traits<QtJniTypes::QtWindow>::className();
714 return false;
715 }
716
717 if (!env.registerNativeMethods(
718 QtJniTypes::Traits<QtJniTypes::QtWindowInsetsController>::className(),
719 {
720 Q_JNI_NATIVE_SCOPED_METHOD(applySystemUiNative, QAndroidPlatformWindow)
721 })) {
722 qCCritical(lcQpaWindow) << "RegisterNatives failed for"
723 << QtJniTypes::Traits<QtJniTypes::QtWindowInsetsController>::className();
724 return false;
725 }
726 return true;
727}
728
729QT_END_NAMESPACE
void propagateSizeHints() override
Reimplement to propagate the size hints of the QWindow.
virtual void applicationStateChanged(Qt::ApplicationState)
void setSafeAreaMargins(const QMargins safeMargins)
void setWindowFlags(Qt::WindowFlags flags) override
Requests setting the window flags of this surface to flags.
void setWindowState(Qt::WindowStates state) override
Requests setting the window state of this surface to type.
bool isExposed() const override
Returns if this window is exposed in the windowing system.
SurfaceContainer m_surfaceContainerType
QAndroidPlatformScreen * platformScreen() const
void requestActivateWindow() override
Reimplement to let Qt be able to request activation/focus for a window.
void applySystemUi(Qt::WindowStates states, Qt::WindowFlags flags)
void updateNativeWindow(bool visible)
void initialize() override
Called as part of QWindow::create(), after constructing the window.
void setVisible(bool visible) override
Reimplemented in subclasses to show the surface if visible is true, and hide it if visible is false.
void setParent(const QPlatformWindow *window) override
This function is called to enable native child window in QPA.
void lower() override
Reimplement to be able to let Qt lower windows to the bottom of the desktop.
void onSurfaceCreated(QtJniTypes::Surface surface)
WId winId() const override
Reimplement in subclasses to return a handle to the native window.
void raise() override
Reimplement to be able to let Qt raise windows to the top of the desktop.
void setGeometry(const QRect &rect) override
This function is called by Qt whenever a window is moved or resized using the QWindow API.
QMargins safeAreaMargins() const override
The safe area margins of a window represent the area that is safe to place content within,...
QObject * parent
Definition qobject.h:74
QRect window() const
Returns the window rectangle.
AndroidBackendRegister * backendRegister()
QWindow * windowFromId(int windowId)
bool isQtApplication()
static void updateWindows(JNIEnv *env, jobject object)
#define qApp
Q_LOGGING_CATEGORY(lcEventDispatcher, "qt.eventdispatcher")
#define qGuiApp