Android: fix QtDisplayManager getDisplaySize function

This patch fixed QtDisplayManager::getDisplaySize
function to use API's that do not require
MANAGE_APP_TOKENS permission.

The function previously failed on 32-bit architectures
with Android 11 aka SDK 30 aka VERSION R due to
createWindowContext requiring MANAGE_APP_TOKENS
permission. The earlier try-catch lead to next error,
because of invalid values.

Amends: 98120622ff1f6f87664f4c42e830000a21391751

Fixes: QTBUG-137027
Pick-to: 6.10 6.9 6.8 6.5
Change-Id: Ia9c3a64ea41ad0575a34a96858851a1123cfc915
Reviewed-by: Assam Boudjelthia <assam.boudjelthia@qt.io>
This commit is contained in:
Rami Potinkara 2025-06-05 13:13:49 +03:00
parent cee8ea7c13
commit 1141a6fa17

View File

@ -18,6 +18,7 @@ import android.view.View;
import android.view.WindowInsets; import android.view.WindowInsets;
import android.view.WindowManager; import android.view.WindowManager;
import android.view.WindowManager.LayoutParams; import android.view.WindowManager.LayoutParams;
import android.view.WindowMetrics;
import android.view.WindowInsetsController; import android.view.WindowInsetsController;
import android.view.Window; import android.view.Window;
@ -284,16 +285,18 @@ class QtDisplayManager
} else { } else {
try { try {
Context displayContext = context.createDisplayContext(display); Context displayContext = context.createDisplayContext(display);
Context windowsContext = displayContext.createWindowContext( WindowManager windowManager = displayContext.getSystemService(WindowManager.class);
WindowManager.LayoutParams.TYPE_APPLICATION, null); if (windowManager != null) {
WindowManager windowManager = (WindowManager) windowsContext.getSystemService( WindowMetrics metrics = windowManager.getCurrentWindowMetrics();
Context.WINDOW_SERVICE); Rect areaBounds = metrics.getBounds();
Rect bounds = windowManager.getMaximumWindowMetrics().getBounds(); return new Size(areaBounds.width(), areaBounds.height());
return new Size(bounds.width(), bounds.height()); } else {
} catch (SecurityException e) { Log.e(QtTAG, "getDisplaySize(): WindowManager null, display ID" + display.getDisplayId());
}
} catch (Exception e) {
Log.e(QtTAG, "Failed to retrieve display metrics with " + e); Log.e(QtTAG, "Failed to retrieve display metrics with " + e);
return new Size(0, 0);
} }
return new Size(0, 0);
} }
} }