Handle orientation changes more systematically.
Bug: 4981385

Simplify the orientation changing code path in the
WindowManager.  Instead of the policy calling setRotation()
when the sensor determined orientation changes, it calls
updateRotation(), which figures everything out.  For the most
part, the rotation actually passed to setRotation() was
more or less ignored and just added confusion, particularly
when handling deferred orientation changes.

Ensure that 180 degree rotations are disallowed even when
the application specifies SCREEN_ORIENTATION_SENSOR_*.
These rotations are only enabled when docked upside-down for
some reason or when the application specifies
SCREEN_ORIENTATION_FULL_SENSOR.

Ensure that special modes like HDMI connected, lid switch,
dock and rotation lock all cause the sensor to be ignored
even when the application asks for sensor-based orientation
changes.  The sensor is not relevant in these modes because
some external factor (or the user) is determining the
preferred rotation.

Currently, applications can still override the preferred
rotation even when there are special modes in play that
might say otherwise.  We could tweak this so that some
special modes trump application choices completely
(resulting in a letter-boxed application, perhaps).
I tested this sort of tweak (not included in the patch)
and it seems to work fine, including transitions between
applications with varying orientation.

Delete dead code related to animFlags.

Handle pausing/resuming orientation changes more precisely.
Ensure that a deferred orientation change is performed when
a drag completes, even if endDragLw() is not called because the
drag was aborted before the drop happened.  We pause
the orientation change in register() and resume in unregister()
because those methods appear to always be called as needed.

Change-Id: If0a31de3d057251e581fdee64819f2b19e676e9a
diff --git a/services/java/com/android/server/wm/DragState.java b/services/java/com/android/server/wm/DragState.java
index dd440bf..f2e7485 100644
--- a/services/java/com/android/server/wm/DragState.java
+++ b/services/java/com/android/server/wm/DragState.java
@@ -125,6 +125,12 @@
             mDragWindowHandle.frameTop = 0;
             mDragWindowHandle.frameRight = mService.mCurDisplayWidth;
             mDragWindowHandle.frameBottom = mService.mCurDisplayHeight;
+
+            // Pause rotations before a drag.
+            if (WindowManagerService.DEBUG_ORIENTATION) {
+                Slog.d(WindowManagerService.TAG, "Pausing rotation during drag");
+            }
+            mService.pauseRotationLocked();
         }
     }
 
@@ -142,6 +148,12 @@
 
             mDragWindowHandle = null;
             mDragApplicationHandle = null;
+
+            // Resume rotations after a drag.
+            if (WindowManagerService.DEBUG_ORIENTATION) {
+                Slog.d(WindowManagerService.TAG, "Resuming rotation after drag");
+            }
+            mService.resumeRotationLocked();
         }
     }
 
@@ -257,13 +269,6 @@
         // free our resources and drop all the object references
         mService.mDragState.reset();
         mService.mDragState = null;
-
-        if (WindowManagerService.DEBUG_ORIENTATION) Slog.d(WindowManagerService.TAG, "Performing post-drag rotation");
-        boolean changed = mService.setRotationUncheckedLocked(
-                WindowManagerPolicy.USE_LAST_ROTATION, 0, false);
-        if (changed) {
-            mService.mH.sendEmptyMessage(H.SEND_NEW_CONFIGURATION);
-        }
     }
 
     void notifyMoveLw(float x, float y) {