AI 143162: am: CL 142858 Fix framework orientation issues
  Define orientation limits for various cases.
  Values between 235 and 295 are considered landscape,
  0 - 60 are considered portrait.
  0-235 are ignored since we don't support Surfaces for 180 and 270 yet.
  A linear threshold value is calculated for values between 265 and 355 to switch between landscape and portrait.(different for both cases)
  Based on current mode we not only calculate the threshold but also compare accordingly based on increasing or decreasing orientation
  BUG = 1734325
  Original author: asuchitra
  Merged from: //branches/cupcake/...

Automated import of CL 143162
diff --git a/core/java/android/view/WindowOrientationListener.java b/core/java/android/view/WindowOrientationListener.java
index 1b7cf8f..ac321db 100755
--- a/core/java/android/view/WindowOrientationListener.java
+++ b/core/java/android/view/WindowOrientationListener.java
@@ -116,13 +116,17 @@
         // the device is leaning forward.
         private static final int PIVOT_LOWER = -10;
         // Upper threshold limit for switching from portrait to landscape
-        private static final int PL_UPPER = 285;
+        private static final int PL_UPPER = 295;
         // Lower threshold limit for switching from landscape to portrait
         private static final int LP_LOWER = 320;
         // Lower threshold limt for switching from portrait to landscape
-        private static final int PL_LOWER = 265;
+        private static final int PL_LOWER = 270;
         // Upper threshold limit for switching from landscape to portrait
-        private static final int LP_UPPER = 355;
+        private static final int LP_UPPER = 359;
+        // Minimum angle which is considered landscape
+        private static final int LANDSCAPE_LOWER = 235;
+        // Minimum angle which is considered portrait
+        private static final int PORTRAIT_LOWER = 60;
         
         // Internal value used for calculating linear variant
         private static final float PL_LF_UPPER =
@@ -143,7 +147,7 @@
             float OneEightyOverPi = 57.29577957855f;
             float gravity = (float) Math.sqrt(X*X+Y*Y+Z*Z);
             float zyangle = (float)Math.asin(Z/gravity)*OneEightyOverPi;
-            int rotation = mSensorRotation;
+            int rotation = -1;
             if ((zyangle <= PIVOT_UPPER) && (zyangle >= PIVOT_LOWER)) {
                 // Check orientation only if the phone is flat enough
                 // Don't trust the angle if the magnitude is small compared to the y value
@@ -156,33 +160,40 @@
                 while (orientation < 0) {
                     orientation += 360;
                 }
-                float delta = zyangle - PIVOT;
-                if (((orientation >= 0) && (orientation <= LP_UPPER)) ||
-                        (orientation >= PL_LOWER)) {
+                // Orientation values between  LANDSCAPE_LOWER and PL_LOWER
+                // are considered landscape.
+                // Ignore orientation values between 0 and LANDSCAPE_LOWER
+                // For orientation values between LP_UPPER and PL_LOWER,
+                // the threshold gets set linearly around PIVOT.
+                if ((orientation >= PL_LOWER) && (orientation <= LP_UPPER)) {
                     float threshold;
+                    float delta = zyangle - PIVOT;
                     if (mSensorRotation == Surface.ROTATION_90) {
                         if (delta < 0) {
-                            // delta is negative
+                            // Delta is negative
                             threshold = LP_LOWER - (LP_LF_LOWER * delta);
-                            //threshold = LP_LOWER + (LP_LF_LOWER * delta);
                         } else {
                             threshold = LP_LOWER + (LP_LF_UPPER * delta);
                         }
+                        rotation = (orientation >= threshold) ? Surface.ROTATION_0 : Surface.ROTATION_90;
                     } else {
                         if (delta < 0) {
-                            //delta is negative
+                            // Delta is negative
                             threshold = PL_UPPER+(PL_LF_LOWER * delta);
                         } else {
                             threshold = PL_UPPER-(PL_LF_UPPER * delta);
                         }
+                        rotation = (orientation <= threshold) ? Surface.ROTATION_90: Surface.ROTATION_0;
                     }
-                    rotation = (orientation >= PL_LOWER &&
-                            orientation <= threshold) ? Surface.ROTATION_90 : Surface.ROTATION_0;
+                } else if ((orientation >= LANDSCAPE_LOWER) && (orientation < LP_LOWER)) {
+                    rotation = Surface.ROTATION_90;
+                } else if ((orientation >= PL_UPPER) || (orientation <= PORTRAIT_LOWER)) {
+                    rotation = Surface.ROTATION_0;
                 }
-            }
-            if (rotation != mSensorRotation) {
-                mSensorRotation = rotation;
-                onOrientationChanged(mSensorRotation);
+                if ((rotation != -1) && (rotation != mSensorRotation)) {
+                    mSensorRotation = rotation;
+                    onOrientationChanged(mSensorRotation);
+                }
             }
         }