Improve orientation sensing when waking up while flat.

This change makes the window manager pass the currently displayed
orientation to the WindowOrientationListener each time to calls to get
the currently sensed orientation.  Now, when the
WindowOrientationListener doesn't know for sure what the orientation
of the device should be (for example, if the sensed orientation is
flat on a table), then it trusts the already-displayed orientation,
rather than trusting whatever sensors reading it most recently
received.

The main use case this addresses is:
* user uses device in portrait
* user turns off device, puts it in pocket
* while in pocket, screen gets turned on, activating orientaiton
  sensor, which senses landscape orientation
* screen timed out
* user takes device out, places it on a table (or looks down at it,
  holding it flat), and unlocks it

Previously, when the device was unlocked, the
WindowOrientationListener would return landscape orientation based on
its previous sensor readings.  Now, it will override those previous
readings because it knows they never actually took effect and the
device is presently flat.

This change also slightly modifies the way we handle flat orientation,
now completely ignoring data when the tilt distrust is nonzero (even
if the current sensor reading shows non-tilted).

Change-Id: Ia4950a41827f8c53a80487d0c2e83b556df781b9
diff --git a/core/java/android/view/WindowOrientationListener.java b/core/java/android/view/WindowOrientationListener.java
index fed55dc..55d11bb 100755
--- a/core/java/android/view/WindowOrientationListener.java
+++ b/core/java/android/view/WindowOrientationListener.java
@@ -103,11 +103,11 @@
         }
     }
 
-    public int getCurrentRotation() {
+    public int getCurrentRotation(int lastRotation) {
         if (mEnabled) {
-            return mSensorEventListener.getCurrentRotation();
+            return mSensorEventListener.getCurrentRotation(lastRotation);
         }
-        return -1;
+        return lastRotation;
     }
 
     /**
@@ -153,9 +153,15 @@
         private static final int ROTATION_270 = 2;
 
         // Mapping our internal aliases into actual Surface rotation values
-        private static final int[] SURFACE_ROTATIONS = new int[] {
+        private static final int[] INTERNAL_TO_SURFACE_ROTATION = new int[] {
             Surface.ROTATION_0, Surface.ROTATION_90, Surface.ROTATION_270};
 
+        // Mapping Surface rotation values to internal aliases.
+        // We have no constant for Surface.ROTATION_180.  That should never happen, but if it
+        // does, we'll arbitrarily choose a mapping.
+        private static final int[] SURFACE_TO_INTERNAL_ROTATION = new int[] {
+            ROTATION_0, ROTATION_90, ROTATION_90, ROTATION_270};
+
         // Threshold ranges of orientation angle to transition into other orientation states.
         // The first list is for transitions from ROTATION_0, the next for ROTATION_90, etc.
         // ROTATE_TO defines the orientation each threshold range transitions to, and must be kept
@@ -243,8 +249,12 @@
             return (float) SAMPLING_PERIOD_MS / (timeConstantMs + SAMPLING_PERIOD_MS);
         }
 
-        int getCurrentRotation() {
-            return SURFACE_ROTATIONS[mRotation];
+        int getCurrentRotation(int lastRotation) {
+            if (mTiltDistrust > 0) {
+                // we really don't know the current orientation, so trust what's currently displayed
+                mRotation = SURFACE_TO_INTERNAL_ROTATION[lastRotation];
+            }
+            return INTERNAL_TO_SURFACE_ROTATION[mRotation];
         }
 
         private void calculateNewRotation(float orientation, float tiltAngle) {
@@ -267,7 +277,7 @@
 
             if (localLOGV) Log.i(TAG, " new rotation = " + rotation);
             mRotation = rotation;
-            mOrientationListener.onOrientationChanged(getCurrentRotation());
+            mOrientationListener.onOrientationChanged(INTERNAL_TO_SURFACE_ROTATION[mRotation]);
         }
 
         private float lowpassFilter(float newValue, float oldValue, float alpha) {
@@ -306,7 +316,8 @@
             mTiltAngle = lowpassFilter(newTiltAngle, mTiltAngle, alpha);
 
             float absoluteTilt = Math.abs(mTiltAngle);
-            if (checkFullyTilted(absoluteTilt)) {
+            checkFullyTilted(absoluteTilt);
+            if (mTiltDistrust > 0) {
                 return; // when fully tilted, ignore orientation entirely
             }
 
@@ -347,11 +358,9 @@
          * get un-tilted.
          *
          * @param absoluteTilt the absolute value of the current tilt angle
-         * @return true if the phone is fully tilted
          */
-        private boolean checkFullyTilted(float absoluteTilt) {
-            boolean fullyTilted = absoluteTilt > MAX_TILT;
-            if (fullyTilted) {
+        private void checkFullyTilted(float absoluteTilt) {
+            if (absoluteTilt > MAX_TILT) {
                 if (mRotation == ROTATION_0) {
                     mOrientationAngle = 0;
                 } else if (mRotation == ROTATION_90) {
@@ -366,7 +375,6 @@
             } else if (mTiltDistrust > 0) {
                 mTiltDistrust--;
             }
-            return fullyTilted;
         }
 
         /**
@@ -389,8 +397,8 @@
          */
         private void filterOrientation(float absoluteTilt, float orientationAngle) {
             float alpha = DEFAULT_LOWPASS_ALPHA;
-            if (mTiltDistrust > 0 || mAccelerationDistrust > 1) {
-                // when fully tilted, or under more than a transient acceleration, distrust heavily
+            if (mAccelerationDistrust > 1) {
+                // when under more than a transient acceleration, distrust heavily
                 alpha = ACCELERATING_LOWPASS_ALPHA;
             } else if (absoluteTilt > PARTIAL_TILT || mAccelerationDistrust == 1) {
                 // when tilted partway, or under transient acceleration, distrust lightly
diff --git a/policy/src/com/android/internal/policy/impl/PhoneWindowManager.java b/policy/src/com/android/internal/policy/impl/PhoneWindowManager.java
index e2e6f1a..274124b 100755
--- a/policy/src/com/android/internal/policy/impl/PhoneWindowManager.java
+++ b/policy/src/com/android/internal/policy/impl/PhoneWindowManager.java
@@ -2076,9 +2076,7 @@
                 return mDeskDockRotation;
             } else {
                 if (useSensorForOrientationLp(orientation)) {
-                    // If the user has enabled auto rotation by default, do it.
-                    int curRotation = mOrientationListener.getCurrentRotation();
-                    return curRotation >= 0 ? curRotation : lastRotation;
+                    return mOrientationListener.getCurrentRotation(lastRotation);
                 }
                 return Surface.ROTATION_0;
             }