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

Change-Id: Icc928c2188a5865035cafcdab2efd5bae3132b1f
diff --git a/core/java/android/content/res/Configuration.java b/core/java/android/content/res/Configuration.java
index ad48786..93b3429 100644
--- a/core/java/android/content/res/Configuration.java
+++ b/core/java/android/content/res/Configuration.java
@@ -303,9 +303,9 @@
     
     public String toString() {
         StringBuilder sb = new StringBuilder(128);
-        sb.append("{ fnt=");
+        sb.append("{");
         sb.append(fontScale);
-        sb.append(" imsi=");
+        sb.append("x imsi=");
         sb.append(mcc);
         sb.append("/");
         sb.append(mnc);
diff --git a/core/java/android/view/WindowManagerPolicy.java b/core/java/android/view/WindowManagerPolicy.java
index 9d00d02..8f7bb8c 100644
--- a/core/java/android/view/WindowManagerPolicy.java
+++ b/core/java/android/view/WindowManagerPolicy.java
@@ -396,6 +396,12 @@
             LocalPowerManager powerManager);
 
     /**
+     * Called by window manager once it has the initial, default native
+     * display dimensions.
+     */
+    public void setInitialDisplaySize(int width, int height);
+    
+    /**
      * Check permissions when adding a window.
      * 
      * @param attrs The window's LayoutParams. 
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:
diff --git a/services/java/com/android/server/wm/DimSurface.java b/services/java/com/android/server/wm/DimSurface.java
index 084ac6f..220c7fb 100644
--- a/services/java/com/android/server/wm/DimSurface.java
+++ b/services/java/com/android/server/wm/DimSurface.java
@@ -89,7 +89,7 @@
     public void printTo(String prefix, PrintWriter pw) {
         pw.print(prefix); pw.print("mDimSurface="); pw.println(mDimSurface);
         pw.print(prefix); pw.print("mDimShown="); pw.print(mDimShown);
-                pw.print(" mLayer="); pw.println(mLayer);
+                pw.print(" mLayer="); pw.print(mLayer);
                 pw.print(" mDimColor=0x"); pw.println(Integer.toHexString(mDimColor));
         pw.print(prefix); pw.print("mLastDimWidth="); pw.print(mLastDimWidth);
                 pw.print(" mLastDimWidth="); pw.println(mLastDimWidth);
diff --git a/services/java/com/android/server/wm/WindowManagerService.java b/services/java/com/android/server/wm/WindowManagerService.java
index b2e78f1..538e38c 100644
--- a/services/java/com/android/server/wm/WindowManagerService.java
+++ b/services/java/com/android/server/wm/WindowManagerService.java
@@ -5853,6 +5853,7 @@
             mInitialDisplayWidth = mCurDisplayWidth = mDisplay.getRealWidth();
             mInitialDisplayHeight = mCurDisplayHeight = mDisplay.getRealHeight();
             mInputManager.setDisplaySize(0, mDisplay.getRawWidth(), mDisplay.getRawHeight());
+            mPolicy.setInitialDisplaySize(mInitialDisplayWidth, mInitialDisplayHeight);
         }
 
         try {
@@ -8711,21 +8712,21 @@
                     pw.print(", mForcedAppOrientation="); pw.print(mForcedAppOrientation);
                     pw.print(", mRequestedRotation="); pw.println(mRequestedRotation);
             pw.print("  mDeferredRotation="); pw.print(mDeferredRotation);
-                    pw.print(", mDeferredRotationAnimFlags="); pw.print(mDeferredRotationAnimFlags);
+                    pw.print(", mDeferredRotationAnimFlags="); pw.println(mDeferredRotationAnimFlags);
             pw.print("  mAnimationPending="); pw.print(mAnimationPending);
                     pw.print(" mWindowAnimationScale="); pw.print(mWindowAnimationScale);
                     pw.print(" mTransitionWindowAnimationScale="); pw.println(mTransitionAnimationScale);
             pw.print("  mNextAppTransition=0x");
                     pw.print(Integer.toHexString(mNextAppTransition));
-                    pw.print(", mAppTransitionReady="); pw.print(mAppTransitionReady);
-                    pw.print(", mAppTransitionRunning="); pw.print(mAppTransitionRunning);
-                    pw.print(", mAppTransitionTimeout="); pw.println( mAppTransitionTimeout);
+                    pw.print(" mAppTransitionReady="); pw.println(mAppTransitionReady);
+            pw.print("  mAppTransitionRunning="); pw.print(mAppTransitionRunning);
+                    pw.print(" mAppTransitionTimeout="); pw.println( mAppTransitionTimeout);
             if (mNextAppTransitionPackage != null) {
                 pw.print("  mNextAppTransitionPackage=");
                     pw.print(mNextAppTransitionPackage);
-                    pw.print(", mNextAppTransitionEnter=0x");
+                    pw.print(" mNextAppTransitionEnter=0x");
                     pw.print(Integer.toHexString(mNextAppTransitionEnter));
-                    pw.print(", mNextAppTransitionExit=0x");
+                    pw.print(" mNextAppTransitionExit=0x");
                     pw.print(Integer.toHexString(mNextAppTransitionExit));
             }
             pw.print("  mStartingIconInTransition="); pw.print(mStartingIconInTransition);