Merge "Rotate overview only if system rotation allowed" into ub-launcher3-rvc-dev
diff --git a/quickstep/recents_ui_overrides/src/com/android/quickstep/views/RecentsView.java b/quickstep/recents_ui_overrides/src/com/android/quickstep/views/RecentsView.java
index 24eae18..18e8768 100644
--- a/quickstep/recents_ui_overrides/src/com/android/quickstep/views/RecentsView.java
+++ b/quickstep/recents_ui_overrides/src/com/android/quickstep/views/RecentsView.java
@@ -285,6 +285,9 @@
         }
     };
 
+    private final RecentsOrientedState.SystemRotationChangeListener mSystemRotationChangeListener =
+            enabled -> toggleOrientationEventListener();
+
     private final PinnedStackAnimationListener mIPinnedStackAnimationListener =
             new PinnedStackAnimationListener();
 
@@ -484,6 +487,7 @@
         SystemUiProxy.INSTANCE.get(getContext()).setPinnedStackAnimationListener(
                 mIPinnedStackAnimationListener);
         mOrientationState.init();
+        mOrientationState.addSystemRotationChangeListener(mSystemRotationChangeListener);
     }
 
     @Override
@@ -498,6 +502,7 @@
         mIdp.removeOnChangeListener(this);
         SystemUiProxy.INSTANCE.get(getContext()).setPinnedStackAnimationListener(null);
         mIPinnedStackAnimationListener.setActivity(null);
+        mOrientationState.removeSystemRotationChangeListener(mSystemRotationChangeListener);
         mOrientationState.destroy();
     }
 
@@ -554,13 +559,6 @@
     }
 
     public void setOverviewStateEnabled(boolean enabled) {
-        if (canEnableOverviewRotationAnimation()) {
-            if (enabled) {
-                mOrientationListener.enable();
-            } else {
-                mOrientationListener.disable();
-            }
-        }
         mOverviewStateEnabled = enabled;
         updateTaskStackListenerState();
         if (!enabled) {
@@ -568,13 +566,26 @@
             // its thumbnail
             mTmpRunningTask = null;
         }
+        toggleOrientationEventListener();
+    }
+
+    private void toggleOrientationEventListener() {
+        boolean canEnable = canEnableOverviewRotationAnimation() && mOverviewStateEnabled;
+        UI_HELPER_EXECUTOR.execute(() -> {
+            if (canEnable) {
+                mOrientationListener.enable();
+            } else {
+                mOrientationListener.disable();
+            }
+        });
     }
 
     private boolean canEnableOverviewRotationAnimation() {
         return supportsVerticalLandscape() // not 3P launcher
                 && !TestProtocol.sDisableSensorRotation // Ignore hardware dependency for tests..
                 && mOrientationListener.canDetectOrientation() // ..but does the hardware even work?
-                && !mOrientationState.canLauncherAutoRotate(); // launcher is going to rotate itself
+                && (mOrientationState.isSystemRotationAllowed() &&
+                    !mOrientationState.canLauncherRotate()); // launcher is going to rotate itself
     }
 
     public void onDigitalWellbeingToastShown() {
diff --git a/quickstep/src/com/android/quickstep/util/RecentsOrientedState.java b/quickstep/src/com/android/quickstep/util/RecentsOrientedState.java
index eefe8ac..bc0d2cc 100644
--- a/quickstep/src/com/android/quickstep/util/RecentsOrientedState.java
+++ b/quickstep/src/com/android/quickstep/util/RecentsOrientedState.java
@@ -51,6 +51,8 @@
 import com.android.launcher3.touch.PortraitPagedViewHandler;
 
 import java.lang.annotation.Retention;
+import java.util.ArrayList;
+import java.util.List;
 
 /**
  * Container to hold orientation/rotation related information for Launcher.
@@ -81,6 +83,10 @@
     private @SurfaceRotation int mDisplayRotation = ROTATION_0;
     private @SurfaceRotation int mLauncherRotation = Surface.ROTATION_0;
 
+    public interface SystemRotationChangeListener {
+        void onSystemRotationChanged(boolean enabled);
+    }
+
     /**
      * If {@code true} we default to {@link PortraitPagedViewHandler} and don't support any fake
      * launcher orientations.
@@ -93,6 +99,7 @@
     private final SharedPreferences mSharedPrefs;
     private final boolean mAllowConfigurationDefaultValue;
 
+    private List<SystemRotationChangeListener> mSystemRotationChangeListeners = new ArrayList<>();
 
     private final Matrix mTmpMatrix = new Matrix();
     private final Matrix mTmpInverseMatrix = new Matrix();
@@ -167,14 +174,6 @@
         return true;
     }
 
-    public boolean areMultipleLayoutOrientationsDisabled() {
-        return mDisableMultipleOrientations;
-    }
-
-    public boolean canLauncherAutoRotate() {
-        return mIsHomeRotationAllowed && mIsSystemRotationAllowed;
-    }
-
     /**
      * Setting this preference renders future calls to {@link #update(int, int, int)} as a no-op.
      */
@@ -198,6 +197,10 @@
         } catch (Settings.SettingNotFoundException e) {
             Log.e(TAG, "autorotate setting not found", e);
         }
+
+        for (SystemRotationChangeListener listener : mSystemRotationChangeListeners) {
+            listener.onSystemRotationChanged(mIsSystemRotationAllowed);
+        }
     }
 
     private void updateHomeRotationSetting() {
@@ -205,6 +208,15 @@
                 mAllowConfigurationDefaultValue);
     }
 
+    public void addSystemRotationChangeListener(SystemRotationChangeListener listener) {
+        mSystemRotationChangeListeners.add(listener);
+        listener.onSystemRotationChanged(mIsSystemRotationAllowed);
+    }
+
+    public void removeSystemRotationChangeListener(SystemRotationChangeListener listener) {
+        mSystemRotationChangeListeners.remove(listener);
+    }
+
     public void init() {
         mSharedPrefs.registerOnSharedPreferenceChangeListener(this);
         mContentResolver.registerContentObserver(
@@ -217,6 +229,7 @@
     public void destroy() {
         mSharedPrefs.unregisterOnSharedPreferenceChangeListener(this);
         mContentResolver.unregisterContentObserver(mSystemAutoRotateObserver);
+        mSystemRotationChangeListeners.clear();
     }
 
     @SurfaceRotation
@@ -229,12 +242,25 @@
         return mTouchRotation;
     }
 
+    @SurfaceRotation
+    public int getLauncherRotation() {
+        return mLauncherRotation;
+    }
+
+    public boolean areMultipleLayoutOrientationsDisabled() {
+        return mDisableMultipleOrientations;
+    }
+
+    public boolean isSystemRotationAllowed() {
+        return mIsSystemRotationAllowed;
+    }
+
     public boolean isHomeRotationAllowed() {
         return mIsHomeRotationAllowed;
     }
 
-    public int getLauncherRotation() {
-        return mLauncherRotation;
+    public boolean canLauncherRotate() {
+        return isSystemRotationAllowed() && isHomeRotationAllowed();
     }
 
     public int getTouchRotationDegrees() {