On orientation change, update rounded corners

When rotating landscape/seascape/upsidedown, the 'top' and 'bottom'
corners are in the correct place

Change-Id: I19858ca42939d81911c5f45bd80d163d0c22eee9
Fixes: 79163187
Test: manual, ScreenDecorationsTest
diff --git a/packages/SystemUI/src/com/android/systemui/ScreenDecorations.java b/packages/SystemUI/src/com/android/systemui/ScreenDecorations.java
index 139215a0..2746a71 100644
--- a/packages/SystemUI/src/com/android/systemui/ScreenDecorations.java
+++ b/packages/SystemUI/src/com/android/systemui/ScreenDecorations.java
@@ -22,8 +22,8 @@
 import static android.view.ViewGroup.LayoutParams.WRAP_CONTENT;
 import static android.view.WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_ALWAYS;
 
-import static com.android.systemui.tuner.TunablePadding.FLAG_START;
 import static com.android.systemui.tuner.TunablePadding.FLAG_END;
+import static com.android.systemui.tuner.TunablePadding.FLAG_START;
 
 import android.annotation.Dimension;
 import android.app.Fragment;
@@ -66,6 +66,7 @@
 import com.android.systemui.tuner.TunablePadding;
 import com.android.systemui.tuner.TunerService;
 import com.android.systemui.tuner.TunerService.Tunable;
+import com.android.systemui.util.leak.RotationUtils;
 
 /**
  * An overlay that draws screen decorations in software (e.g for rounded corners or display cutout)
@@ -77,6 +78,9 @@
     private static final boolean DEBUG_SCREENSHOT_ROUNDED_CORNERS =
             SystemProperties.getBoolean("debug.screenshot_rounded_corners", false);
 
+    private DisplayManager mDisplayManager;
+    private DisplayManager.DisplayListener mDisplayListener;
+
     private int mRoundedDefault;
     private int mRoundedDefaultTop;
     private int mRoundedDefaultBottom;
@@ -84,7 +88,7 @@
     private View mBottomOverlay;
     private float mDensity;
     private WindowManager mWindowManager;
-    private boolean mLandscape;
+    private int mRotation;
 
     @Override
     public void start() {
@@ -104,6 +108,28 @@
         if (padding != 0) {
             setupPadding(padding);
         }
+
+        mDisplayListener = new DisplayManager.DisplayListener() {
+            @Override
+            public void onDisplayAdded(int displayId) {
+                // do nothing
+            }
+
+            @Override
+            public void onDisplayRemoved(int displayId) {
+                // do nothing
+            }
+
+            @Override
+            public void onDisplayChanged(int displayId) {
+                updateOrientation();
+            }
+        };
+
+        mRotation = -1;
+        mDisplayManager = (DisplayManager) mContext.getSystemService(
+                Context.DISPLAY_SERVICE);
+        mDisplayManager.registerDisplayListener(mDisplayListener, null);
     }
 
     private void setupDecorations() {
@@ -169,17 +195,22 @@
 
     @Override
     protected void onConfigurationChanged(Configuration newConfig) {
-        boolean newLanscape = newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE;
-        if (newLanscape != mLandscape) {
-            mLandscape = newLanscape;
+        updateOrientation();
+    }
+
+    protected void updateOrientation() {
+        int newRotation = RotationUtils.getExactRotation(mContext);
+        if (newRotation != mRotation) {
+            mRotation = newRotation;
 
             if (mOverlay != null) {
                 updateLayoutParams();
                 updateViews();
             }
-        }
-        if (shouldDrawCutout() && mOverlay == null) {
-            setupDecorations();
+
+            if (shouldDrawCutout() && mOverlay == null) {
+                setupDecorations();
+            }
         }
     }
 
@@ -188,16 +219,28 @@
         View topRight = mOverlay.findViewById(R.id.right);
         View bottomLeft = mBottomOverlay.findViewById(R.id.left);
         View bottomRight = mBottomOverlay.findViewById(R.id.right);
-        if (mLandscape) {
-            // Flip corners
-            View tmp = topRight;
-            topRight = bottomLeft;
-            bottomLeft = tmp;
+
+        if (mRotation == RotationUtils.ROTATION_NONE) {
+            updateView(topLeft, Gravity.TOP | Gravity.LEFT, 0);
+            updateView(topRight, Gravity.TOP | Gravity.RIGHT, 90);
+            updateView(bottomLeft, Gravity.BOTTOM | Gravity.LEFT, 270);
+            updateView(bottomRight, Gravity.BOTTOM | Gravity.RIGHT, 180);
+        } else if (mRotation == RotationUtils.ROTATION_LANDSCAPE) {
+            updateView(topLeft, Gravity.TOP | Gravity.LEFT, 0);
+            updateView(topRight, Gravity.BOTTOM | Gravity.LEFT, 270);
+            updateView(bottomLeft, Gravity.TOP | Gravity.RIGHT, 90);;
+            updateView(bottomRight, Gravity.BOTTOM | Gravity.RIGHT, 180);
+        } else if (mRotation == RotationUtils.ROTATION_UPSIDE_DOWN) {
+            updateView(topLeft, Gravity.BOTTOM | Gravity.LEFT, 270);
+            updateView(topRight, Gravity.BOTTOM | Gravity.RIGHT, 180);
+            updateView(bottomLeft, Gravity.TOP | Gravity.LEFT, 0);
+            updateView(bottomRight, Gravity.TOP | Gravity.RIGHT, 90);
+        } else if (mRotation == RotationUtils.ROTATION_SEASCAPE) {
+            updateView(topLeft, Gravity.BOTTOM | Gravity.RIGHT, 180);
+            updateView(topRight, Gravity.TOP | Gravity.RIGHT, 90);
+            updateView(bottomLeft, Gravity.BOTTOM | Gravity.LEFT, 270);
+            updateView(bottomRight, Gravity.TOP | Gravity.LEFT, 0);
         }
-        updateView(topLeft, Gravity.TOP | Gravity.LEFT, 0);
-        updateView(topRight, Gravity.TOP | Gravity.RIGHT, 90);
-        updateView(bottomLeft, Gravity.BOTTOM | Gravity.LEFT, 270);
-        updateView(bottomRight, Gravity.BOTTOM | Gravity.RIGHT, 180);
 
         updateWindowVisibilities();
     }
@@ -269,9 +312,14 @@
         }
 
         lp.setTitle("ScreenDecorOverlay");
-        lp.gravity = Gravity.TOP | Gravity.LEFT;
+        if (mRotation == RotationUtils.ROTATION_SEASCAPE
+                || mRotation == RotationUtils.ROTATION_UPSIDE_DOWN) {
+            lp.gravity = Gravity.BOTTOM | Gravity.RIGHT;
+        } else {
+            lp.gravity = Gravity.TOP | Gravity.LEFT;
+        }
         lp.layoutInDisplayCutoutMode = LAYOUT_IN_DISPLAY_CUTOUT_MODE_ALWAYS;
-        if (mLandscape) {
+        if (isLandscape(mRotation)) {
             lp.width = WRAP_CONTENT;
             lp.height = MATCH_PARENT;
         }
@@ -281,7 +329,12 @@
     private WindowManager.LayoutParams getBottomLayoutParams() {
         WindowManager.LayoutParams lp = getWindowLayoutParams();
         lp.setTitle("ScreenDecorOverlayBottom");
-        lp.gravity = Gravity.BOTTOM | Gravity.RIGHT;
+        if (mRotation == RotationUtils.ROTATION_SEASCAPE
+                || mRotation == RotationUtils.ROTATION_UPSIDE_DOWN) {
+            lp.gravity = Gravity.TOP | Gravity.LEFT;
+        } else {
+            lp.gravity = Gravity.BOTTOM | Gravity.RIGHT;
+        }
         return lp;
     }
 
@@ -568,4 +621,9 @@
             return cutoutBounds;
         }
     }
+
+    private boolean isLandscape(int rotation) {
+        return rotation == RotationUtils.ROTATION_LANDSCAPE || rotation ==
+                RotationUtils.ROTATION_SEASCAPE;
+    }
 }