SensorManager: handle 270 and 180 rotation in the legacy APIs

Technically these APIs are deprecated, however old apps might still be using them
so we might as well make sure they work in all orientations.
diff --git a/core/java/android/hardware/SensorManager.java b/core/java/android/hardware/SensorManager.java
index aebe84e..98172e6 100644
--- a/core/java/android/hardware/SensorManager.java
+++ b/core/java/android/hardware/SensorManager.java
@@ -1447,8 +1447,9 @@
             values[3] = x;
             values[4] = y;
             values[5] = z;
-            // TODO: add support for 180 and 270 orientations
-            if (orientation == Surface.ROTATION_90) {
+
+            if ((orientation & Surface.ROTATION_90) != 0) {
+                // handles 90 and 270 rotation
                 switch (sensor) {
                     case SENSOR_ACCELEROMETER:
                     case SENSOR_MAGNETIC_FIELD:
@@ -1464,6 +1465,26 @@
                         break;
                 }
             }
+            if ((orientation & Surface.ROTATION_180) != 0) {
+                x = values[0];
+                y = values[1];
+                z = values[2];
+                // handles 180 (flip) and 270 (flip + 90) rotation
+                switch (sensor) {
+                    case SENSOR_ACCELEROMETER:
+                    case SENSOR_MAGNETIC_FIELD:
+                        values[0] =-x;
+                        values[1] =-y;
+                        values[2] = z;
+                        break;
+                    case SENSOR_ORIENTATION:
+                    case SENSOR_ORIENTATION_RAW:
+                        values[0] = (x >= 180) ? (x - 180) : (x + 180);
+                        values[1] =-y;
+                        values[2] =-z;
+                        break;
+                }
+            }
         }
     }