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 <qt_ci_bot@qt-project.org>
Reviewed-by: Rami Potinkara <rami.potinkara@qt.io>
Reviewed-by: BogDan Vatra <bogdan@kdab.com>
(cherry picked from commit 072387edecb2269097821e35f1f232da6c657650)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Samuel Mira 2022-09-05 20:57:56 +03:00 committed by Qt Cherry-pick Bot
parent 0459b572b3
commit 52af31fbbd
2 changed files with 49 additions and 37 deletions

View File

@ -14,6 +14,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
{
@ -57,14 +58,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;
Display display = (Build.VERSION.SDK_INT < Build.VERSION_CODES.R)
? activity.getWindowManager().getDefaultDisplay()
: activity.getDisplay();
DisplayMetrics realMetrics = new DisplayMetrics();
Display display = (Build.VERSION.SDK_INT < Build.VERSION_CODES.R)
? ((Activity)getContext()).getWindowManager().getDefaultDisplay()
: ((Activity)getContext()).getDisplay();
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
@ -76,34 +83,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;
WindowInsets rootInsets = getRootWindowInsets();
int usableAreaWidth = w - insetLeft - insetRight;
int usableAreaHeight = h - insetTop - insetBottom;
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

View File

@ -621,24 +621,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);