Fix bug in deciding which rotation to use for an orientation.

Change-Id: Icc928c2188a5865035cafcdab2efd5bae3132b1f
diff --git a/policy/src/com/android/internal/policy/impl/PhoneWindowManager.java b/policy/src/com/android/internal/policy/impl/PhoneWindowManager.java
index 1969945..65321b7 100755
--- a/policy/src/com/android/internal/policy/impl/PhoneWindowManager.java
+++ b/policy/src/com/android/internal/policy/impl/PhoneWindowManager.java
@@ -35,7 +35,6 @@
 import android.graphics.PixelFormat;
 import android.graphics.Rect;
 import android.os.Binder;
-import android.os.Build;
 import android.os.Handler;
 import android.os.IBinder;
 import android.os.LocalPowerManager;
@@ -56,11 +55,9 @@
 import com.android.internal.view.BaseInputHandler;
 import com.android.internal.widget.PointerLocationView;
 
-import android.telephony.TelephonyManager;
 import android.util.EventLog;
 import android.util.Log;
 import android.util.Slog;
-import android.view.Display;
 import android.view.Gravity;
 import android.view.HapticFeedbackConstants;
 import android.view.IWindowManager;
@@ -348,10 +345,10 @@
     // (See Settings.Secure.INCALL_POWER_BUTTON_BEHAVIOR.)
     int mIncallPowerBehavior;
 
-    int mLandscapeRotation = -1; // default landscape rotation
-    int mSeascapeRotation = -1; // "other" landscape rotation, 180 degrees from mLandscapeRotation
-    int mPortraitRotation = -1; // default portrait rotation
-    int mUpsideDownRotation = -1; // "other" portrait rotation
+    int mLandscapeRotation = 0;  // default landscape rotation
+    int mSeascapeRotation = 0;   // "other" landscape rotation, 180 degrees from mLandscapeRotation
+    int mPortraitRotation = 0;   // default portrait rotation
+    int mUpsideDownRotation = 0; // "other" portrait rotation
 
     // Nothing to see here, move along...
     int mFancyRotationAnimation;
@@ -742,6 +739,32 @@
         initializeHdmiState();
     }
 
+    public void setInitialDisplaySize(int width, int height) {
+        if (width > height) {
+            mLandscapeRotation = Surface.ROTATION_0;
+            mSeascapeRotation = Surface.ROTATION_180;
+            if (mContext.getResources().getBoolean(
+                    com.android.internal.R.bool.config_reverseDefaultRotation)) {
+                mPortraitRotation = Surface.ROTATION_90;
+                mUpsideDownRotation = Surface.ROTATION_270;
+            } else {
+                mPortraitRotation = Surface.ROTATION_270;
+                mUpsideDownRotation = Surface.ROTATION_90;
+            }
+        } else {
+            mPortraitRotation = Surface.ROTATION_0;
+            mUpsideDownRotation = Surface.ROTATION_180;
+            if (mContext.getResources().getBoolean(
+                    com.android.internal.R.bool.config_reverseDefaultRotation)) {
+                mLandscapeRotation = Surface.ROTATION_270;
+                mSeascapeRotation = Surface.ROTATION_90;
+            } else {
+                mLandscapeRotation = Surface.ROTATION_90;
+                mSeascapeRotation = Surface.ROTATION_270;
+            }
+        }
+    }
+
     public void updateSettings() {
         ContentResolver resolver = mContext.getContentResolver();
         boolean updateRotation = false;
@@ -2528,7 +2551,7 @@
             }
         }
     }
-
+    
     public int rotationForOrientationLw(int orientation, int lastRotation,
             boolean displayEnabled) {
 
@@ -2541,35 +2564,6 @@
                         );
         }
 
-        if (mPortraitRotation < 0) {
-            // Initialize the rotation angles for each orientation once.
-            Display d = ((WindowManager)mContext.getSystemService(Context.WINDOW_SERVICE))
-                    .getDefaultDisplay();
-            if (d.getWidth() > d.getHeight()) {
-                mLandscapeRotation = Surface.ROTATION_0;
-                mSeascapeRotation = Surface.ROTATION_180;
-                if (mContext.getResources().getBoolean(
-                        com.android.internal.R.bool.config_reverseDefaultRotation)) {
-                    mPortraitRotation = Surface.ROTATION_90;
-                    mUpsideDownRotation = Surface.ROTATION_270;
-                } else {
-                    mPortraitRotation = Surface.ROTATION_270;
-                    mUpsideDownRotation = Surface.ROTATION_90;
-                }
-            } else {
-                mPortraitRotation = Surface.ROTATION_0;
-                mUpsideDownRotation = Surface.ROTATION_180;
-                if (mContext.getResources().getBoolean(
-                        com.android.internal.R.bool.config_reverseDefaultRotation)) {
-                    mLandscapeRotation = Surface.ROTATION_270;
-                    mSeascapeRotation = Surface.ROTATION_90;
-                } else {
-                    mLandscapeRotation = Surface.ROTATION_90;
-                    mSeascapeRotation = Surface.ROTATION_270;
-                }
-            }
-        }
-
         synchronized (mLock) {
             switch (orientation) {
                 case ActivityInfo.SCREEN_ORIENTATION_PORTRAIT: