Remove reliance on getDefaultDisplay() to determine screen rotation.

Replaced the calls to getWindowManager().getDefaultDisplay() with code
to determine the screen rotation angle based on the angle ranges.

Bug: 21208380
Change-Id: Ie91d38f9c0b4227bb2148a193c00339b5d8f4ce6
diff --git a/src/com/android/incallui/InCallActivity.java b/src/com/android/incallui/InCallActivity.java
index 289bf63..d1bebe7 100644
--- a/src/com/android/incallui/InCallActivity.java
+++ b/src/com/android/incallui/InCallActivity.java
@@ -39,6 +39,7 @@
 import android.view.Display;
 import android.view.MenuItem;
 import android.view.OrientationEventListener;
+import android.view.Surface;
 import android.view.animation.Animation;
 import android.view.animation.AnimationUtils;
 import android.view.KeyEvent;
@@ -203,10 +204,29 @@
                 SensorManager.SENSOR_DELAY_NORMAL) {
             @Override
             public void onOrientationChanged(int orientation) {
+                // Device is flat, don't change orientation.
+                if (orientation == OrientationEventListener.ORIENTATION_UNKNOWN) {
+                    return;
+                }
+
+                int newRotation = Surface.ROTATION_0;
+                // We only shift if we're within 22.5 (23) degrees of the target
+                // orientation. This avoids flopping back and forth when holding
+                // the device at 45 degrees or so.
+                if (orientation >= 337 || orientation <= 23) {
+                    newRotation = Surface.ROTATION_0;
+                } else if (orientation >= 67 && orientation <= 113) {
+                    // Why not 90? Because screen and sensor orientation are
+                    // reversed.
+                    newRotation = Surface.ROTATION_270;
+                } else if (orientation >= 157 && orientation <= 203) {
+                    newRotation = Surface.ROTATION_180;
+                } else if (orientation >= 247 && orientation <= 293) {
+                    newRotation = Surface.ROTATION_90;
+                }
+
                 // Orientation is the current device orientation in degrees.  Ultimately we want
                 // the rotation (in fixed 90 degree intervals).
-                Display display = getWindowManager().getDefaultDisplay();
-                int newRotation = display.getRotation();
                 if (newRotation != sPreviousRotation) {
                     doOrientationChanged(newRotation);
                 }
@@ -500,7 +520,9 @@
     /**
      * Handles changes in device rotation.
      *
-     * @param rotation The new device rotation.
+     * @param rotation The new device rotation (one of: {@link Surface#ROTATION_0},
+     *      {@link Surface#ROTATION_90}, {@link Surface#ROTATION_180},
+     *      {@link Surface#ROTATION_270}).
      */
     private void doOrientationChanged(int rotation) {
         Log.d(this, "doOrientationChanged prevOrientation=" + sPreviousRotation +
diff --git a/src/com/android/incallui/InCallPresenter.java b/src/com/android/incallui/InCallPresenter.java
index 022651c..18b2686 100644
--- a/src/com/android/incallui/InCallPresenter.java
+++ b/src/com/android/incallui/InCallPresenter.java
@@ -1339,7 +1339,9 @@
     /**
      * Handles changes to the device rotation.
      *
-     * @param rotation The device rotation.
+     * @param rotation The device rotation (one of: {@link Surface#ROTATION_0},
+     *      {@link Surface#ROTATION_90}, {@link Surface#ROTATION_180},
+     *      {@link Surface#ROTATION_270}).
      */
     public void onDeviceRotationChange(int rotation) {
         Log.d(this, "onDeviceRotationChange: rotation=" + rotation);
@@ -1353,7 +1355,9 @@
 
     /**
      * Converts rotation constants to rotation in degrees.
-     * @param rotation Rotation constants.
+     * @param rotation Rotation constants (one of: {@link Surface#ROTATION_0},
+     *      {@link Surface#ROTATION_90}, {@link Surface#ROTATION_180},
+     *      {@link Surface#ROTATION_270}).
      */
     public static int toRotationAngle(int rotation) {
         int rotationAngle;
@@ -1379,7 +1383,9 @@
     /**
      * Notifies listeners of changes in orientation (e.g. portrait/landscape).
      *
-     * @param orientation The orientation of the device.
+     * @param orientation The orientation of the device (one of: {@link Surface#ROTATION_0},
+     *      {@link Surface#ROTATION_90}, {@link Surface#ROTATION_180},
+     *      {@link Surface#ROTATION_270}).
      */
     public void onDeviceOrientationChange(int orientation) {
         for (InCallOrientationListener listener : mOrientationListeners) {
diff --git a/src/com/android/incallui/InCallVideoCallCallback.java b/src/com/android/incallui/InCallVideoCallCallback.java
index a35404a..87b886d 100644
--- a/src/com/android/incallui/InCallVideoCallCallback.java
+++ b/src/com/android/incallui/InCallVideoCallCallback.java
@@ -28,7 +28,7 @@
 public class InCallVideoCallCallback extends VideoCall.Callback {
 
     /**
-     * The call associated with this {@link InCallVideoClient}.
+     * The call associated with this {@link InCallVideoCallCallback}.
      */
     private Call mCall;
 
diff --git a/src/com/android/incallui/VideoCallPresenter.java b/src/com/android/incallui/VideoCallPresenter.java
index ed7edf9..20f3912 100644
--- a/src/com/android/incallui/VideoCallPresenter.java
+++ b/src/com/android/incallui/VideoCallPresenter.java
@@ -91,13 +91,6 @@
     };
 
     /**
-     * Determines the device orientation (portrait/lanscape).
-     */
-    public int getDeviceOrientation() {
-        return mDeviceOrientation;
-    }
-
-    /**
      * Defines the state of the preview surface negotiation with the telephony layer.
      */
     private class PreviewSurfaceState {
@@ -947,8 +940,10 @@
 
     /**
      * Handles changes to the device orientation.
-     * See: {@link Configuration.ORIENTATION_LANDSCAPE}, {@link Configuration.ORIENTATION_PORTRAIT}
-     * @param orientation The device orientation.
+     *
+     * @param orientation The device orientation (one of: {@link Surface#ROTATION_0},
+     *      {@link Surface#ROTATION_90}, {@link Surface#ROTATION_180},
+     *      {@link Surface#ROTATION_270}).
      */
     @Override
     public void onDeviceOrientationChanged(int orientation) {
@@ -1009,9 +1004,10 @@
 
     /**
      * Sets the preview surface size based on the current device orientation.
-     * See: {@link Configuration.ORIENTATION_LANDSCAPE}, {@link Configuration.ORIENTATION_PORTRAIT}
      *
-     * @param orientation The device orientation.
+     * @param orientation The device orientation (one of: {@link Surface#ROTATION_0},
+     *      {@link Surface#ROTATION_90}, {@link Surface#ROTATION_180},
+     *      {@link Surface#ROTATION_270}).
      * @param aspectRatio The aspect ratio of the camera (width / height).
      */
     private void setPreviewSize(int orientation, float aspectRatio) {
@@ -1023,10 +1019,12 @@
         int height;
         int width;
 
-        if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
+        if (orientation == Surface.ROTATION_90 || orientation == Surface.ROTATION_270) {
+            // Landscape or reverse landscape orientation.
             width = (int) (mMinimumVideoDimension * aspectRatio);
             height = (int) mMinimumVideoDimension;
         } else {
+            // Portrait or reverse portrait orientation.
             width = (int) mMinimumVideoDimension;
             height = (int) (mMinimumVideoDimension * aspectRatio);
         }