Set default launcher orientation to portrait

Also stop OrientationTouchTransformer from
spamming logs
b/150214193 should track properly handling
this in the future

Fixes: 146176182
Test: New vertical layout showed up when
swiping to Overview w/ landscape app.
Works for both autorotate on and off.

Change-Id: I3707cf7b5693be0fcf4d63491901f4ec00793f4f
diff --git a/src/com/android/launcher3/PagedView.java b/src/com/android/launcher3/PagedView.java
index ef6bd3d..a6180a6 100644
--- a/src/com/android/launcher3/PagedView.java
+++ b/src/com/android/launcher3/PagedView.java
@@ -47,6 +47,7 @@
 import com.android.launcher3.config.FeatureFlags;
 import com.android.launcher3.model.PagedViewOrientedState;
 import com.android.launcher3.pageindicators.PageIndicator;
+import com.android.launcher3.states.RotationHelper;
 import com.android.launcher3.touch.PortraitPagedViewHandler;
 import com.android.launcher3.touch.OverScroll;
 import com.android.launcher3.touch.PagedOrientationHandler;
diff --git a/src/com/android/launcher3/states/RotationHelper.java b/src/com/android/launcher3/states/RotationHelper.java
index b47ef72..43d54eb 100644
--- a/src/com/android/launcher3/states/RotationHelper.java
+++ b/src/com/android/launcher3/states/RotationHelper.java
@@ -17,8 +17,10 @@
 
 import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_LOCKED;
 import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_NOSENSOR;
+import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
 import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
 import static android.util.DisplayMetrics.DENSITY_DEVICE_STABLE;
+
 import static com.android.launcher3.util.Executors.MAIN_EXECUTOR;
 
 import android.content.ContentResolver;
@@ -41,6 +43,9 @@
 import com.android.launcher3.config.FeatureFlags;
 import com.android.launcher3.util.UiThreadHelper;
 
+import java.util.ArrayList;
+import java.util.List;
+
 /**
  * Utility class to manage launcher rotation
  */
@@ -49,16 +54,38 @@
     public static final String ALLOW_ROTATION_PREFERENCE_KEY = "pref_allowRotation";
 
     public static final String FIXED_ROTATION_TRANSFORM_SETTING_NAME = "fixed_rotation_transform";
+    private final ContentResolver mContentResolver;
+
+    /**
+     * Listener to receive changes when {@link #FIXED_ROTATION_TRANSFORM_SETTING_NAME} flag changes.
+     */
+    public interface ForcedRotationChangedListener {
+        void onForcedRotationChanged(boolean isForcedRotation);
+    }
 
     public static boolean getAllowRotationDefaultValue() {
-        // If the device was scaled, used the original dimensions to determine if rotation
-        // is allowed of not.
+        // If the device's pixel density was scaled (usually via settings for A11y), use the
+        // original dimensions to determine if rotation is allowed of not.
         Resources res = Resources.getSystem();
         int originalSmallestWidth = res.getConfiguration().smallestScreenWidthDp
                 * res.getDisplayMetrics().densityDpi / DENSITY_DEVICE_STABLE;
         return originalSmallestWidth >= 600;
     }
 
+
+    private final ContentObserver mContentObserver =
+        new ContentObserver(MAIN_EXECUTOR.getHandler()) {
+            @Override
+            public void onChange(boolean selfChange) {
+                boolean forcedRotation = Utilities.isForcedRotation(mLauncher);
+                PagedView.sFlagForcedRotation = forcedRotation;
+                updateForcedRotation();
+                for (ForcedRotationChangedListener listener : mForcedRotationChangedListeners) {
+                    listener.onForcedRotationChanged(forcedRotation);
+                }
+            }
+        };
+
     public static final int REQUEST_NONE = 0;
     public static final int REQUEST_ROTATE = 1;
     public static final int REQUEST_LOCK = 2;
@@ -68,6 +95,8 @@
 
     private boolean mIgnoreAutoRotateSettings;
     private boolean mAutoRotateEnabled;
+    private boolean mForcedRotation;
+    private List<ForcedRotationChangedListener> mForcedRotationChangedListeners = new ArrayList<>();
 
     /**
      * Rotation request made by
@@ -96,6 +125,7 @@
 
         // On large devices we do not handle auto-rotate differently.
         mIgnoreAutoRotateSettings = mLauncher.getResources().getBoolean(R.bool.allow_rotation);
+        updateForcedRotation();
         if (!mIgnoreAutoRotateSettings) {
             mPrefs = Utilities.getPrefs(mLauncher);
             mPrefs.registerOnSharedPreferenceChangeListener(this);
@@ -106,16 +136,24 @@
         }
 
         // TODO(b/150260456) Add this in home settings as well
-        final ContentResolver resolver = launcher.getContentResolver();
-        final ContentObserver observer = new ContentObserver(MAIN_EXECUTOR.getHandler()) {
-            @Override
-            public void onChange(boolean selfChange) {
-                PagedView.sFlagForcedRotation = Utilities.isForcedRotation(mLauncher);
-            }
-        };
-        resolver.registerContentObserver(Settings.Global.getUriFor(
-            FIXED_ROTATION_TRANSFORM_SETTING_NAME),
-            false, observer);
+        mContentResolver = launcher.getContentResolver();
+        mContentResolver.registerContentObserver(Settings.Global.getUriFor(
+            FIXED_ROTATION_TRANSFORM_SETTING_NAME), false, mContentObserver);
+    }
+
+    private void updateForcedRotation() {
+        mForcedRotation = !getAllowRotationDefaultValue() && Utilities.isForcedRotation(mLauncher);
+    }
+
+    /**
+     * will not be called when first registering the listener.
+     */
+    public void addForcedRotationCallback(ForcedRotationChangedListener listener) {
+        mForcedRotationChangedListeners.add(listener);
+    }
+
+    public void removeForcedRotationCallback(ForcedRotationChangedListener listener) {
+        mForcedRotationChangedListeners.remove(listener);
     }
 
     public void setRotationHadDifferentUI(boolean rotationHasDifferentUI) {
@@ -197,6 +235,10 @@
             if (mPrefs != null) {
                 mPrefs.unregisterOnSharedPreferenceChangeListener(this);
             }
+            if (mContentResolver != null) {
+                mContentResolver.unregisterContentObserver(mContentObserver);
+            }
+            mForcedRotationChangedListeners.clear();
         }
     }
 
@@ -206,7 +248,10 @@
         }
 
         final int activityFlags;
-        if (mStateHandlerRequest != REQUEST_NONE) {
+        if (mForcedRotation) {
+            // TODO(b/150214193) Properly address this
+            activityFlags = SCREEN_ORIENTATION_PORTRAIT;
+        } else if (mStateHandlerRequest != REQUEST_NONE) {
             activityFlags = mStateHandlerRequest == REQUEST_LOCK ?
                     SCREEN_ORIENTATION_LOCKED : SCREEN_ORIENTATION_UNSPECIFIED;
         } else if (mCurrentTransitionRequest != REQUEST_NONE) {