Further improvements to window management!

Fix issue #2493497: Stuck in the Emergency dialer - Home/Back keys doesn't work
This was another case of not updating the window focus when needed, this time
when the lock screen was hidden.

Also re-arrange the layout/animate flow to address issues where you would see
a flicker of whatever was behind the lock screen when showing a new activity that
hides the lock screen.  This was because we were deciding to hide the lock screen
during the layout phase, which meant we had to do it without considering whether
it had drawn.  So we could hide the lock screen before the window is shown for the
first time after being drawn.  Now we can do this in the policy during animate, so
we can wait until the window is drawn and actually being shown.

The flow in perform layout is thus significantly changed, where the layout and
animate loops are both under the same repeating loop.  The actual flow from this
should be the same, but it now allows the policy to request a new layout after
the animation loop is done.  This actually cleans up a number of things in this
code as the complexity has increased.

Finally this includes a change to the ui mode manager when switching modes, to do
the resource configuration switch at a different time.  This makes transitions
between modes much cleaner (though not yet perfect).

Change-Id: I5d9e75c1e79df1106108dd522f8ffed6058ef82b
diff --git a/core/java/android/view/WindowManagerPolicy.java b/core/java/android/view/WindowManagerPolicy.java
index ab3260e..b39cb9d 100644
--- a/core/java/android/view/WindowManagerPolicy.java
+++ b/core/java/android/view/WindowManagerPolicy.java
@@ -627,8 +627,9 @@
      * returned, all windows given to layoutWindow() <em>must</em> have had a
      * frame assigned.
      *  
-     * @return Return any bit set of {@link #FINISH_LAYOUT_REDO_LAYOUT}
-     * and {@link #FINISH_LAYOUT_REDO_CONFIG}.
+     * @return Return any bit set of {@link #FINISH_LAYOUT_REDO_LAYOUT},
+     * {@link #FINISH_LAYOUT_REDO_CONFIG}, {@link #FINISH_LAYOUT_REDO_WALLPAPER},
+     * or {@link #FINISH_LAYOUT_REDO_ANIM}.
      */
     public int finishLayoutLw();
 
@@ -638,6 +639,8 @@
     static final int FINISH_LAYOUT_REDO_CONFIG = 0x0002;
     /** Wallpaper may need to move */
     static final int FINISH_LAYOUT_REDO_WALLPAPER = 0x0004;
+    /** Need to recompute animations */
+    static final int FINISH_LAYOUT_REDO_ANIM = 0x0008;
     
     /**
      * Called when animation of the windows is about to start.
@@ -661,10 +664,11 @@
      * something that may have modified the animation state of another window, 
      * be sure to return true in order to perform another animation frame. 
      *  
-     * @return Return true if animation state may have changed (so that another 
-     *         frame of animation will be run).
+     * @return Return any bit set of {@link #FINISH_LAYOUT_REDO_LAYOUT},
+     * {@link #FINISH_LAYOUT_REDO_CONFIG}, {@link #FINISH_LAYOUT_REDO_WALLPAPER},
+     * or {@link #FINISH_LAYOUT_REDO_ANIM}.
      */
-    public boolean finishAnimationLw();
+    public int finishAnimationLw();
 
     /**
      * Return true if it is okay to perform animations for an app transition