Restrict rotation when global actions panel is enabled

Once the orientation changes to portrait, the panel will be displayed,
and the orientation will be locked to portrait.

If, when global actions is launched and a panel is available, the user
has their orientation locked to non-portrait, then their orientation
is unlocked, so that they may rotate to portrait in order to see the
panel.

In all cases, the user's orientation setting is restored once global
actions is dismissed.

Bug: 129343749

Test: unlock orientation, launch global actions in portrait, notice
      that orientation is locked, dismiss global actions, notice that
      orientation is unlocked

Test: unlock orientation, launch global actions in non-portrait,
      rotate to portrait, notice that orientation is now locked,
      dismiss global actions, notice that orientation is unlocked

Test: lock phone orientation to non-portrait, launch global actions,
      rotate to portrait, notice that orientation is locked, dismiss
      global actions, notice that orientation has reset and is locked

Change-Id: I3972929aede075de69ea5333dcaf445e6ace59b8
diff --git a/packages/SystemUI/src/com/android/systemui/globalactions/GlobalActionsDialog.java b/packages/SystemUI/src/com/android/systemui/globalactions/GlobalActionsDialog.java
index d6c8971..1ffed4c 100644
--- a/packages/SystemUI/src/com/android/systemui/globalactions/GlobalActionsDialog.java
+++ b/packages/SystemUI/src/com/android/systemui/globalactions/GlobalActionsDialog.java
@@ -80,6 +80,7 @@
 import com.android.internal.util.EmergencyAffordanceManager;
 import com.android.internal.util.ScreenRecordHelper;
 import com.android.internal.util.ScreenshotHelper;
+import com.android.internal.view.RotationPolicy;
 import com.android.internal.widget.LockPatternUtils;
 import com.android.systemui.Dependency;
 import com.android.systemui.Interpolators;
@@ -1498,6 +1499,7 @@
         private boolean mKeyguardShowing;
         private boolean mShowing;
         private float mScrimAlpha;
+        private ResetOrientationData mResetOrientationData;
 
         ActionsDialog(Context context, MyAdapter adapter,
                 GlobalActionsPanelPlugin.PanelViewController plugin) {
@@ -1531,27 +1533,50 @@
         }
 
         private boolean shouldUsePanel() {
-            if (!isPanelEnabled(mContext) || mPanelController == null) {
-                return false;
-            }
-            if (mPanelController.getPanelContent() == null) {
-                return false;
-            }
-            return true;
+            return isPanelEnabled(mContext)
+                    && mPanelController != null
+                    && mPanelController.getPanelContent() != null;
         }
 
         private void initializePanel() {
-            FrameLayout panelContainer = new FrameLayout(mContext);
-            FrameLayout.LayoutParams panelParams =
-                    new FrameLayout.LayoutParams(
-                            FrameLayout.LayoutParams.MATCH_PARENT,
-                            FrameLayout.LayoutParams.WRAP_CONTENT);
-            panelContainer.addView(mPanelController.getPanelContent(), panelParams);
-            addContentView(
-                    panelContainer,
-                    new ViewGroup.LayoutParams(
-                            ViewGroup.LayoutParams.MATCH_PARENT,
-                            ViewGroup.LayoutParams.MATCH_PARENT));
+            int rotation = RotationUtils.getRotation(mContext);
+            boolean rotationLocked = RotationPolicy.isRotationLocked(mContext);
+            if (rotation != RotationUtils.ROTATION_NONE) {
+                if (rotationLocked) {
+                    if (mResetOrientationData == null) {
+                        mResetOrientationData = new ResetOrientationData();
+                        mResetOrientationData.locked = true;
+                        mResetOrientationData.rotation = rotation;
+                    }
+
+                    // Unlock rotation, so user can choose to rotate to portrait to see the panel.
+                    RotationPolicy.setRotationLockAtAngle(
+                            mContext, false, RotationUtils.ROTATION_NONE);
+                }
+            } else {
+                if (!rotationLocked) {
+                    if (mResetOrientationData == null) {
+                        mResetOrientationData = new ResetOrientationData();
+                        mResetOrientationData.locked = false;
+                    }
+
+                    // Lock to portrait, so the user doesn't accidentally hide the panel.
+                    RotationPolicy.setRotationLockAtAngle(
+                            mContext, true, RotationUtils.ROTATION_NONE);
+                }
+
+                FrameLayout panelContainer = new FrameLayout(mContext);
+                FrameLayout.LayoutParams panelParams =
+                        new FrameLayout.LayoutParams(
+                                FrameLayout.LayoutParams.MATCH_PARENT,
+                                FrameLayout.LayoutParams.WRAP_CONTENT);
+                panelContainer.addView(mPanelController.getPanelContent(), panelParams);
+                addContentView(
+                        panelContainer,
+                        new ViewGroup.LayoutParams(
+                                ViewGroup.LayoutParams.MATCH_PARENT,
+                                ViewGroup.LayoutParams.MATCH_PARENT));
+            }
         }
 
         private void initializeLayout() {
@@ -1683,19 +1708,30 @@
                         mBackgroundDrawable.setAlpha(alpha);
                     })
                     .start();
-            if (mPanelController != null) {
-                mPanelController.onDismissed();
-            }
+            dismissPanel();
+            resetOrientation();
         }
 
         void dismissImmediately() {
             super.dismiss();
             mShowing = false;
+            dismissPanel();
+            resetOrientation();
+        }
+
+        private void dismissPanel() {
             if (mPanelController != null) {
                 mPanelController.onDismissed();
             }
         }
 
+        private void resetOrientation() {
+            if (mResetOrientationData != null) {
+                RotationPolicy.setRotationLockAtAngle(mContext, mResetOrientationData.locked,
+                        mResetOrientationData.rotation);
+            }
+        }
+
         @Override
         public void onColorsChanged(ColorExtractor extractor, int which) {
             if (mKeyguardShowing) {
@@ -1725,6 +1761,11 @@
                 refreshDialog();
             }
         }
+
+        private static class ResetOrientationData {
+            public boolean locked;
+            public int rotation;
+        }
     }
 
     /**