From 740eb27c065671418ed19f44d640e647310bcb6e Mon Sep 17 00:00:00 2001 From: Samuel Mira Date: Mon, 5 Sep 2022 20:57:56 +0300 Subject: Android: Fix flickering on window resize and show keyboard Changed to use display getMetrics which will return the size of the application window, and use getRealMetrics to obtain the size of the largest region accessible to the app. I updated the fullscreen mode to use the new sizes. Task-number: QTBUG-41170 Task-number: QTBUG-66727 Change-Id: Ic25555ed2e1b910b3fdbc0f3a31e3a19763a04eb Reviewed-by: Qt CI Bot Reviewed-by: Rami Potinkara Reviewed-by: BogDan Vatra (cherry picked from commit 072387edecb2269097821e35f1f232da6c657650) Reviewed-by: Qt Cherry-pick Bot --- .../jar/src/org/qtproject/qt/android/QtLayout.java | 67 ++++++++++++---------- src/plugins/platforms/android/androidjnimain.cpp | 21 ++++--- 2 files changed, 50 insertions(+), 38 deletions(-) diff --git a/src/android/jar/src/org/qtproject/qt/android/QtLayout.java b/src/android/jar/src/org/qtproject/qt/android/QtLayout.java index 6c52a00422..04a0f6c378 100644 --- a/src/android/jar/src/org/qtproject/qt/android/QtLayout.java +++ b/src/android/jar/src/org/qtproject/qt/android/QtLayout.java @@ -50,6 +50,7 @@ import android.view.View; import android.view.ViewGroup; import android.view.WindowInsets; import android.view.WindowManager; +import android.graphics.Insets; public class QtLayout extends ViewGroup { @@ -93,14 +94,20 @@ public class QtLayout extends ViewGroup @Override protected void onSizeChanged (int w, int h, int oldw, int oldh) { - WindowInsets insets = getRootWindowInsets(); + Activity activity = (Activity)getContext(); + if (activity == null) + return; - DisplayMetrics realMetrics = new DisplayMetrics(); Display display = (Build.VERSION.SDK_INT < Build.VERSION_CODES.R) - ? ((Activity)getContext()).getWindowManager().getDefaultDisplay() - : ((Activity)getContext()).getDisplay(); + ? activity.getWindowManager().getDefaultDisplay() + : activity.getDisplay(); + + DisplayMetrics realMetrics = new DisplayMetrics(); display.getRealMetrics(realMetrics); + DisplayMetrics appMetrics = new DisplayMetrics(); + display.getMetrics(appMetrics); + if ((realMetrics.widthPixels > realMetrics.heightPixels) != (w > h)) { // This is an intermediate state during display rotation. // The new size is still reported for old orientation, while @@ -112,34 +119,36 @@ public class QtLayout extends ViewGroup return; } - boolean isFullScreenView = h == realMetrics.heightPixels; - // The code uses insets for fullscreen mode only. However in practice - // the insets can be reported incorrectly. Both on Android 6 and Android 11 - // a non-zero bottom inset is reported even when the - // WindowManager.LayoutParams.FLAG_FULLSCREEN flag is set. - // To avoid that, add an extra check for the fullscreen mode. - // The insets-related logic is not removed for the case when - // isFullScreenView == true, but hasFlagFullscreen == false, although - // I can't get such case in my tests. - final int windowFlags = ((Activity)getContext()).getWindow().getAttributes().flags; - final boolean hasFlagFullscreen = - (windowFlags & WindowManager.LayoutParams.FLAG_FULLSCREEN) != 0; - int insetLeft = - (isFullScreenView && !hasFlagFullscreen) ? insets.getSystemWindowInsetLeft() : 0; - int insetTop = - (isFullScreenView && !hasFlagFullscreen) ? insets.getSystemWindowInsetTop() : 0; - int insetRight = - (isFullScreenView && !hasFlagFullscreen) ? insets.getSystemWindowInsetRight() : 0; - int insetBottom = - (isFullScreenView && !hasFlagFullscreen) ? insets.getSystemWindowInsetBottom() : 0; - - int usableAreaWidth = w - insetLeft - insetRight; - int usableAreaHeight = h - insetTop - insetBottom; + WindowInsets rootInsets = getRootWindowInsets(); + + int insetLeft = 0; + int insetTop = 0; + + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { + Insets insets = rootInsets.getInsets(WindowInsets.Type.systemBars()); + insetLeft = insets.left; + insetTop = insets.top; + } else { + insetLeft = rootInsets.getSystemWindowInsetLeft(); + insetTop = rootInsets.getSystemWindowInsetTop(); + } + + int appWidthPixels = appMetrics.widthPixels; + int appHeightPixels = appMetrics.heightPixels; + + final int flag = + activity.getWindow().getAttributes().flags & WindowManager.LayoutParams.FLAG_FULLSCREEN; + + if (flag == WindowManager.LayoutParams.FLAG_FULLSCREEN) { + // immersive mode uses the whole screen + appWidthPixels = realMetrics.widthPixels; + appHeightPixels = realMetrics.heightPixels; + } QtNative.setApplicationDisplayMetrics( realMetrics.widthPixels, realMetrics.heightPixels, insetLeft, insetTop, - usableAreaWidth, usableAreaHeight, realMetrics.xdpi, realMetrics.ydpi, - realMetrics.scaledDensity, realMetrics.density, display.getRefreshRate()); + appWidthPixels, appHeightPixels, appMetrics.xdpi, appMetrics.ydpi, + appMetrics.scaledDensity, appMetrics.density, display.getRefreshRate()); int newRotation = display.getRotation(); if (m_ownDisplayRotation != m_activityDisplayRotation diff --git a/src/plugins/platforms/android/androidjnimain.cpp b/src/plugins/platforms/android/androidjnimain.cpp index 207ace0c12..68ca88b31e 100644 --- a/src/plugins/platforms/android/androidjnimain.cpp +++ b/src/plugins/platforms/android/androidjnimain.cpp @@ -657,24 +657,27 @@ static void setDisplayMetrics(JNIEnv * /*env*/, jclass /*clazz*/, jint screenWid jint availableHeightPixels, jdouble xdpi, jdouble ydpi, jdouble scaledDensity, jdouble density, jfloat refreshRate) { + Q_UNUSED(availableLeftPixels) + Q_UNUSED(availableTopPixels) + m_availableWidthPixels = availableWidthPixels; m_availableHeightPixels = availableHeightPixels; m_scaledDensity = scaledDensity; m_density = density; + const QSize screenSize(screenWidthPixels, screenHeightPixels); + // available geometry always starts from top left + const QRect availableGeometry(0, 0, availableWidthPixels, availableHeightPixels); + const QSize physicalSize(qRound(double(screenWidthPixels) / xdpi * 25.4), + qRound(double(screenHeightPixels) / ydpi * 25.4)); + QMutexLocker lock(&m_platformMutex); if (!m_androidPlatformIntegration) { QAndroidPlatformIntegration::setDefaultDisplayMetrics( - availableLeftPixels, availableTopPixels, availableWidthPixels, - availableHeightPixels, qRound(double(screenWidthPixels) / xdpi * 25.4), - qRound(double(screenHeightPixels) / ydpi * 25.4), screenWidthPixels, - screenHeightPixels); + availableGeometry.left(), availableGeometry.top(), availableGeometry.width(), + availableGeometry.height(), physicalSize.width(), physicalSize.height(), + screenSize.width(), screenSize.height()); } else { - const QSize physicalSize(qRound(double(screenWidthPixels) / xdpi * 25.4), - qRound(double(screenHeightPixels) / ydpi * 25.4)); - const QSize screenSize(screenWidthPixels, screenHeightPixels); - const QRect availableGeometry(availableLeftPixels, availableTopPixels, - availableWidthPixels, availableHeightPixels); m_androidPlatformIntegration->setScreenSizeParameters(physicalSize, screenSize, availableGeometry); m_androidPlatformIntegration->setRefreshRate(refreshRate); -- cgit v1.2.3