Cleanup Keyguard handling when turning off screen

Before, Keyguard was shown whenever PhoneWindowManager went to sleep.
However, this was too early on some devices, and too late on others.
Now, the callbacks are split up into two phases: startedGoingToSleep
and finishedGoingToSleep. In the first callback, we decide what to
do and play the lock sound if necessary. In the second callback, we
actually show the Keyguard, or reset its state necessary, so it
doesn't interfere with the screen off animation.

Bug: 17929748
Bug: 20782303
Change-Id: I4c4406595b07b7589d64f380cd3fa96bed3d20ee
diff --git a/services/core/java/com/android/server/policy/PhoneWindowManager.java b/services/core/java/com/android/server/policy/PhoneWindowManager.java
index a1e1aef..9eb7431 100644
--- a/services/core/java/com/android/server/policy/PhoneWindowManager.java
+++ b/services/core/java/com/android/server/policy/PhoneWindowManager.java
@@ -5236,6 +5236,9 @@
     @Override
     public void startedGoingToSleep(int why) {
         if (DEBUG_WAKEUP) Slog.i(TAG, "Started going to sleep... (why=" + why + ")");
+        if (mKeyguardDelegate != null) {
+            mKeyguardDelegate.onStartedGoingToSleep(why);
+        }
     }
 
     // Called on the PowerManager's Notifier thread.
@@ -5253,9 +5256,8 @@
             updateOrientationListenerLp();
             updateLockScreenTimeout();
         }
-
         if (mKeyguardDelegate != null) {
-            mKeyguardDelegate.onScreenTurnedOff(why);
+            mKeyguardDelegate.onFinishedGoingToSleep(why);
         }
     }
 
@@ -5283,7 +5285,7 @@
         }
 
         if (mKeyguardDelegate != null) {
-            mKeyguardDelegate.onScreenTurnedOn(mKeyguardDelegateCallback);
+            mKeyguardDelegate.onStartedWakingUp(mKeyguardDelegateCallback);
             // ... eventually calls finishKeyguardDrawn
         } else {
             if (DEBUG_WAKEUP) Slog.d(TAG, "null mKeyguardDelegate: setting mKeyguardDrawComplete.");