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 <petri.virkkunen@qt.io>
This commit is contained in:
Assam Boudjelthia 2025-01-06 03:05:17 +02:00
parent 9ba94b1e62
commit 2d2b9fea7d
3 changed files with 12 additions and 25 deletions

View File

@ -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);

View File

@ -79,7 +79,7 @@ class QtDisplayManager {
};
}
static void handleOrientationChanges(Activity activity)
static void handleOrientationChange(Activity activity)
{
int currentRotation = getDisplayRotation(activity);
if (m_previousRotation == currentRotation)

View File

@ -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);
});
}
}