Fix issue #2519590: Lock screen stuck in landscape mode

Well, mostly.  There is still a problem here where the first time
you show the lock screen it just doesn't draw itself.  I assume
this is something breaking in the view hierarchy as it floounders
around removing and adding new views as it is first being shown...
but no idea at this point what is the actual case.

Change-Id: Iba99ae3242931c8673b17b106c86fc99e2c52abe
diff --git a/core/java/android/view/ViewRoot.java b/core/java/android/view/ViewRoot.java
index 484922d..c87ffee 100644
--- a/core/java/android/view/ViewRoot.java
+++ b/core/java/android/view/ViewRoot.java
@@ -80,6 +80,7 @@
     private static final boolean DEBUG_ORIENTATION = false || LOCAL_LOGV;
     private static final boolean DEBUG_TRACKBALL = false || LOCAL_LOGV;
     private static final boolean DEBUG_IMF = false || LOCAL_LOGV;
+    private static final boolean DEBUG_CONFIGURATION = false || LOCAL_LOGV;
     private static final boolean WATCH_POINTER = false;
 
     private static final boolean MEASURE_LATENCY = false;
@@ -176,6 +177,9 @@
     final ViewTreeObserver.InternalInsetsInfo mLastGivenInsets
             = new ViewTreeObserver.InternalInsetsInfo();
 
+    final Configuration mLastConfiguration = new Configuration();
+    final Configuration mPendingConfiguration = new Configuration();
+    
     class ResizedInfo {
         Rect coveredInsets;
         Rect visibleInsets;
@@ -719,6 +723,7 @@
             attachInfo.mRecomputeGlobalAttributes = false;
             attachInfo.mKeepScreenOn = false;
             viewVisibilityChanged = false;
+            mLastConfiguration.setTo(host.getResources().getConfiguration());
             host.dispatchAttachedToWindow(attachInfo, 0);
             //Log.i(TAG, "Screen on initialized: " + attachInfo.mKeepScreenOn);
 
@@ -904,6 +909,13 @@
                         + " visible=" + mPendingVisibleInsets.toShortString()
                         + " surface=" + mSurface);
 
+                if (mPendingConfiguration.seq != 0) {
+                    if (DEBUG_CONFIGURATION) Log.v(TAG, "Visible with new config: "
+                            + mPendingConfiguration);
+                    updateConfiguration(mPendingConfiguration, !mFirst);
+                    mPendingConfiguration.seq = 0;
+                }
+                
                 contentInsetsChanged = !mPendingContentInsets.equals(
                         mAttachInfo.mContentInsets);
                 visibleInsetsChanged = !mPendingVisibleInsets.equals(
@@ -1627,6 +1639,30 @@
         }
     }
 
+    void updateConfiguration(Configuration config, boolean force) {
+        if (DEBUG_CONFIGURATION) Log.v(TAG,
+                "Applying new config to window "
+                + mWindowAttributes.getTitle()
+                + ": " + config);
+        synchronized (sConfigCallbacks) {
+            for (int i=sConfigCallbacks.size()-1; i>=0; i--) {
+                sConfigCallbacks.get(i).onConfigurationChanged(config);
+            }
+        }
+        if (mView != null) {
+            // At this point the resources have been updated to
+            // have the most recent config, whatever that is.  Use
+            // the on in them which may be newer.
+            if (mView != null) {
+                config = mView.getResources().getConfiguration();
+            }
+            if (force || mLastConfiguration.diff(config) != 0) {
+                mLastConfiguration.setTo(config);
+                mView.dispatchConfigurationChanged(config);
+            }
+        }
+    }
+    
     /**
      * Return true if child is an ancestor of parent, (or equal to the parent).
      */
@@ -1815,14 +1851,7 @@
             if (mAdded) {
                 Configuration config = ((ResizedInfo)msg.obj).newConfig;
                 if (config != null) {
-                    synchronized (sConfigCallbacks) {
-                        for (int i=sConfigCallbacks.size()-1; i>=0; i--) {
-                            sConfigCallbacks.get(i).onConfigurationChanged(config);
-                        }
-                    }
-                    if (mView != null) {
-                        mView.dispatchConfigurationChanged(config);
-                    }
+                    updateConfiguration(config, false);
                 }
                 mWinFrame.left = 0;
                 mWinFrame.right = msg.arg1;
@@ -2500,12 +2529,14 @@
         if (params != null) {
             if (DBG) Log.d(TAG, "WindowLayout in layoutWindow:" + params);
         }
+        mPendingConfiguration.seq = 0;
         int relayoutResult = sWindowSession.relayout(
                 mWindow, params,
                 (int) (mView.mMeasuredWidth * appScale + 0.5f),
                 (int) (mView.mMeasuredHeight * appScale + 0.5f),
                 viewVisibility, insetsPending, mWinFrame,
-                mPendingContentInsets, mPendingVisibleInsets, mSurface);
+                mPendingContentInsets, mPendingVisibleInsets,
+                mPendingConfiguration, mSurface);
         if (restore) {
             params.restore();
         }