From 60dd3b71bbee30d71a6b695395dc6a87e8734ffe Mon Sep 17 00:00:00 2001 From: Assam Boudjelthia Date: Mon, 5 Jun 2023 15:42:02 +0300 Subject: [PATCH] Android: Fix reporting of QScreen's size() and physicalSize() The screen's DPI need to be accounted for when calculating the size in mm from pixels. This was missing after multi-displays support was added for Android. Amends fbf586db2c587e7ba83cf1bfe8e5b912310d6bdb. Fixes: QTBUG-112742 Change-Id: I31814faa8de68e5193757d52e264b8ed90ae56b6 Reviewed-by: Ville Voutilainen (cherry picked from commit 436923a76c4c60ad7271a66821768b06573310ce) --- .../android/qandroidplatformscreen.cpp | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/src/plugins/platforms/android/qandroidplatformscreen.cpp b/src/plugins/platforms/android/qandroidplatformscreen.cpp index b2fa2ed3e54..4d046685ff6 100644 --- a/src/plugins/platforms/android/qandroidplatformscreen.cpp +++ b/src/plugins/platforms/android/qandroidplatformscreen.cpp @@ -53,6 +53,7 @@ private: #endif Q_DECLARE_JNI_CLASS(Display, "android/view/Display") +Q_DECLARE_JNI_CLASS(DisplayMetrics, "android/util/DisplayMetrics") Q_DECLARE_JNI_TYPE(DisplayMode, "Landroid/view/Display$Mode;") @@ -79,12 +80,21 @@ QAndroidPlatformScreen::QAndroidPlatformScreen(const QJniObject &displayObject) if (!displayObject.isValid()) return; - m_size = QSize(displayObject.callMethod("getWidth"), displayObject.callMethod("getHeight")); m_name = displayObject.callObjectMethod("getName").toString(); m_refreshRate = displayObject.callMethod("getRefreshRate"); m_displayId = displayObject.callMethod("getDisplayId"); + QJniObject displayMetricsObj(QtJniTypes::className()); + displayObject.callMethod("getRealMetrics", displayMetricsObj.object()); + + const int widthPixels = displayMetricsObj.getField("widthPixels"); + const int heightPixels = displayMetricsObj.getField("heightPixels"); + m_size = QSize(widthPixels, heightPixels); + if (QNativeInterface::QAndroidApplication::sdkVersion() >= 23) { + const qreal xdpi = displayMetricsObj.getField("xdpi"); + const qreal ydpi = displayMetricsObj.getField("ydpi"); + const QJniObject currentMode = displayObject.callObjectMethod("getMode"); const jint currentModeId = currentMode.callMethod("getModeId"); @@ -96,8 +106,9 @@ QAndroidPlatformScreen::QAndroidPlatformScreen(const QJniObject &displayObject) const auto size = env->GetArrayLength(modeArray); for (jsize i = 0; i < size; ++i) { const auto mode = QJniObject::fromLocalRef(env->GetObjectArrayElement(modeArray, i)); - const int physicalWidth = mode.callMethod("getPhysicalWidth"); - const int physicalHeight = mode.callMethod("getPhysicalHeight"); + // Physical sizes in millimeters + const int physicalWidth = qRound(mode.callMethod("getPhysicalWidth") / xdpi * 25.4); + const int physicalHeight = qRound(mode.callMethod("getPhysicalHeight") / ydpi * 25.4); if (currentModeId == mode.callMethod("getModeId")) { m_currentMode = i;