From 2d2b9fea7d9a7a89a19cdf42f811c13eb86668b0 Mon Sep 17 00:00:00 2001 From: Assam Boudjelthia Date: Mon, 6 Jan 2025 03:05:17 +0200 Subject: [PATCH] Android: simplify orientation change handling Currently we rely on both onConfigurationChanged() and onSizeChanged() in QtRootLayout to report orientation changes, because the former might be invoked when the new size is not updated yet, so we're doing many calculations to ensure we don't end up missing or calling change handler more than we should. However, instead of all that, we could simply post the handler under onConfigurationChanged() which would end up eventually invoked after onSizeChanged() has been called already. Task-number: QTBUG-132718 Change-Id: I9fbc826518b81a9e4417eba6cd3cf999637201b9 Reviewed-by: Petri Virkkunen --- .../qt/android/QtActivityDelegate.java | 2 +- .../qt/android/QtDisplayManager.java | 2 +- .../qtproject/qt/android/QtRootLayout.java | 33 ++++++------------- 3 files changed, 12 insertions(+), 25 deletions(-) diff --git a/src/android/jar/src/org/qtproject/qt/android/QtActivityDelegate.java b/src/android/jar/src/org/qtproject/qt/android/QtActivityDelegate.java index 07da2f124a9..fc8b4355a89 100644 --- a/src/android/jar/src/org/qtproject/qt/android/QtActivityDelegate.java +++ b/src/android/jar/src/org/qtproject/qt/android/QtActivityDelegate.java @@ -141,7 +141,7 @@ class QtActivityDelegate extends QtActivityDelegateBase m_activity.setContentView(m_layout, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)); - QtDisplayManager.handleOrientationChanges(m_activity); + QtDisplayManager.handleOrientationChange(m_activity); handleUiModeChange(m_activity.getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK); diff --git a/src/android/jar/src/org/qtproject/qt/android/QtDisplayManager.java b/src/android/jar/src/org/qtproject/qt/android/QtDisplayManager.java index 9c8e74594df..77c7bf16b32 100644 --- a/src/android/jar/src/org/qtproject/qt/android/QtDisplayManager.java +++ b/src/android/jar/src/org/qtproject/qt/android/QtDisplayManager.java @@ -79,7 +79,7 @@ class QtDisplayManager { }; } - static void handleOrientationChanges(Activity activity) + static void handleOrientationChange(Activity activity) { int currentRotation = getDisplayRotation(activity); if (m_previousRotation == currentRotation) diff --git a/src/android/jar/src/org/qtproject/qt/android/QtRootLayout.java b/src/android/jar/src/org/qtproject/qt/android/QtRootLayout.java index c036fec7211..71fb750b9e9 100644 --- a/src/android/jar/src/org/qtproject/qt/android/QtRootLayout.java +++ b/src/android/jar/src/org/qtproject/qt/android/QtRootLayout.java @@ -7,7 +7,6 @@ package org.qtproject.qt.android; import android.app.Activity; import android.content.Context; import android.content.res.Configuration; -import android.view.Surface; /** A layout which corresponds to one Activity, i.e. is the root layout where the top level window @@ -15,44 +14,32 @@ import android.view.Surface; */ class QtRootLayout extends QtLayout { - private int m_previousRotation = -1; - QtRootLayout(Context context) { super(context); } @Override - protected void onSizeChanged (int w, int h, int oldw, int oldh) + protected void onSizeChanged(int w, int h, int oldw, int oldh) { - Activity activity = (Activity)getContext(); + Activity activity = (Activity) getContext(); if (activity == null) return; QtDisplayManager.setApplicationDisplayMetrics(activity, w, h); - QtDisplayManager.handleOrientationChanges(activity); } @Override public void onConfigurationChanged(Configuration configuration) { - Context context = getContext(); - if (context instanceof Activity) { - Activity activity = (Activity)context; - //if orientation change is betwen invertedPortrait and portrait or - //invertedLandscape and landscape, we do not get sizeChanged callback. - int rotation = QtDisplayManager.getDisplayRotation(activity); - if (isSameSizeForOrientations(rotation, m_previousRotation)) - QtDisplayManager.handleOrientationChanges(activity); - m_previousRotation = rotation; - } - } + Activity activity = (Activity) getContext(); + if (activity == null) + return; - boolean isSameSizeForOrientations(int r1, int r2) { - return (r1 == r2) || - (r1 == Surface.ROTATION_0 && r2 == Surface.ROTATION_180) - || (r1 == Surface.ROTATION_180 && r2 == Surface.ROTATION_0) - || (r1 == Surface.ROTATION_90 && r2 == Surface.ROTATION_270) - || (r1 == Surface.ROTATION_270 && r2 == Surface.ROTATION_90); + // Post the orientation handling just in case the onSizeChanged() is + // called a bit late (QTBUG-94459). + QtNative.runAction(() -> { + QtDisplayManager.handleOrientationChange(activity); + }); } }