From 6209af67d372f84b158afd4c87eef8a1195b3893 Mon Sep 17 00:00:00 2001 From: Alex Chau Date: Wed, 27 Jul 2022 14:08:23 +0100 Subject: [PATCH] Listen to DisplayController for ignoreAutoRotateSettings - Also skip most of initDeviceProfile/onIdpChanged if DeviceProfile didn't actually change to effectively skip the 2nd unexpected onConfigurationChangeded caused by setRequestedOrientation (b/211763738) Test: Change display size while in app or at home screen Fix: 240019605 Change-Id: If307742639bd269622140a7da0dc900887c67937 --- src/com/android/launcher3/Launcher.java | 18 +++++++++++++++--- .../launcher3/states/RotationHelper.java | 16 ++++++++++------ .../launcher3/util/DisplayController.java | 6 +++--- 3 files changed, 28 insertions(+), 12 deletions(-) diff --git a/src/com/android/launcher3/Launcher.java b/src/com/android/launcher3/Launcher.java index 9c622512a4..d2fe8bd7ea 100644 --- a/src/com/android/launcher3/Launcher.java +++ b/src/com/android/launcher3/Launcher.java @@ -636,7 +636,10 @@ public class Launcher extends StatefulActivity @Override public void onIdpChanged(boolean modelPropertiesChanged) { - initDeviceProfile(mDeviceProfile.inv); + if (!initDeviceProfile(mDeviceProfile.inv)) { + return; + } + dispatchDeviceProfileChanged(); reapplyUi(); mDragLayer.recreateControllers(); @@ -659,9 +662,17 @@ public class Launcher extends StatefulActivity mDragLayer.onOneHandedModeStateChanged(activated); } - protected void initDeviceProfile(InvariantDeviceProfile idp) { + /** + * Returns {@code true} if a new DeviceProfile is initialized, and {@code false} otherwise. + */ + protected boolean initDeviceProfile(InvariantDeviceProfile idp) { // Load configuration-specific DeviceProfile - mDeviceProfile = idp.getDeviceProfile(this); + DeviceProfile deviceProfile = idp.getDeviceProfile(this); + if (mDeviceProfile == deviceProfile) { + return false; + } + + mDeviceProfile = deviceProfile; if (isInMultiWindowMode()) { mDeviceProfile = mDeviceProfile.getMultiWindowProfile( this, getMultiWindowDisplaySize()); @@ -669,6 +680,7 @@ public class Launcher extends StatefulActivity onDeviceProfileInitiated(); mModelWriter = mModel.getWriter(getDeviceProfile().isVerticalBarLayout(), true, this); + return true; } public RotationHelper getRotationHelper() { diff --git a/src/com/android/launcher3/states/RotationHelper.java b/src/com/android/launcher3/states/RotationHelper.java index 38b62d466e..fd8b2e5744 100644 --- a/src/com/android/launcher3/states/RotationHelper.java +++ b/src/com/android/launcher3/states/RotationHelper.java @@ -23,19 +23,21 @@ import static android.util.DisplayMetrics.DENSITY_DEVICE_STABLE; import static com.android.launcher3.Utilities.dpiFromPx; import static com.android.launcher3.util.window.WindowManagerProxy.MIN_TABLET_WIDTH; +import android.content.Context; import android.content.SharedPreferences; import android.content.SharedPreferences.OnSharedPreferenceChangeListener; import com.android.launcher3.BaseActivity; import com.android.launcher3.DeviceProfile; import com.android.launcher3.Utilities; +import com.android.launcher3.util.DisplayController; import com.android.launcher3.util.UiThreadHelper; /** * Utility class to manage launcher rotation */ public class RotationHelper implements OnSharedPreferenceChangeListener, - DeviceProfile.OnDeviceProfileChangeListener { + DisplayController.DisplayInfoChangeListener { private static final String TAG = "RotationHelper"; @@ -119,8 +121,8 @@ public class RotationHelper implements OnSharedPreferenceChangeListener, } @Override - public void onDeviceProfileChanged(DeviceProfile dp) { - boolean ignoreAutoRotateSettings = dp.isTablet; + public void onDisplayInfoChanged(Context context, DisplayController.Info info, int flags) { + boolean ignoreAutoRotateSettings = info.isTablet(info.realBounds); if (mIgnoreAutoRotateSettings != ignoreAutoRotateSettings) { setIgnoreAutoRotateSettings(ignoreAutoRotateSettings); notifyChange(); @@ -157,8 +159,10 @@ public class RotationHelper implements OnSharedPreferenceChangeListener, public void initialize() { if (!mInitialized) { mInitialized = true; - setIgnoreAutoRotateSettings(mActivity.getDeviceProfile().isTablet); - mActivity.addOnDeviceProfileChangeListener(this); + DisplayController displayController = DisplayController.INSTANCE.get(mActivity); + DisplayController.Info info = displayController.getInfo(); + setIgnoreAutoRotateSettings(info.isTablet(info.realBounds)); + displayController.addChangeListener(this); notifyChange(); } } @@ -166,7 +170,7 @@ public class RotationHelper implements OnSharedPreferenceChangeListener, public void destroy() { if (!mDestroyed) { mDestroyed = true; - mActivity.removeOnDeviceProfileChangeListener(this); + DisplayController.INSTANCE.get(mActivity).removeChangeListener(this); mActivity = null; if (mSharedPrefs != null) { mSharedPrefs.unregisterOnSharedPreferenceChangeListener(this); diff --git a/src/com/android/launcher3/util/DisplayController.java b/src/com/android/launcher3/util/DisplayController.java index 15fe1d9205..fbc4cb669d 100644 --- a/src/com/android/launcher3/util/DisplayController.java +++ b/src/com/android/launcher3/util/DisplayController.java @@ -278,11 +278,11 @@ public class DisplayController implements ComponentCallbacks, SafeCloseable { public final float fontScale; private final int densityDpi; public final NavigationMode navigationMode; - private final PortraitSize mScreenSizeDp; + // WindowBounds + public final WindowBounds realBounds; public final Set supportedBounds = new ArraySet<>(); - private final ArrayMap mPerDisplayBounds = new ArrayMap<>(); @@ -310,7 +310,7 @@ public class DisplayController implements ComponentCallbacks, SafeCloseable { mPerDisplayBounds.putAll(perDisplayBoundsCache); WindowBounds[] cachedValue = mPerDisplayBounds.get(normalizedDisplayInfo); - WindowBounds realBounds = wmProxy.getRealBounds(displayInfoContext, displayInfo); + realBounds = wmProxy.getRealBounds(displayInfoContext, displayInfo); if (cachedValue == null) { // Unexpected normalizedDisplayInfo is found, recreate the cache Log.e(TAG, "Unexpected normalizedDisplayInfo found, invalidating cache");