Hold a partial wakelock while showing the keyguard

This ensures that the keyguard is fully displayed before the processor suspends,
which avoids a race condition when the device is powered back up.
Fixes one instance of bug b/2164183

Change-Id: Ifd7f928068cf779ec725937db82af69306c02107
Signed-off-by: Mike Lockwood <lockwood@android.com>
diff --git a/policy/com/android/internal/policy/impl/KeyguardViewMediator.java b/policy/com/android/internal/policy/impl/KeyguardViewMediator.java
index 680f942..34b5e3a 100644
--- a/policy/com/android/internal/policy/impl/KeyguardViewMediator.java
+++ b/policy/com/android/internal/policy/impl/KeyguardViewMediator.java
@@ -150,6 +150,12 @@
     private PowerManager.WakeLock mWakeLock;
 
     /**
+     * Used to keep the device awake while to ensure the keyguard finishes opening before
+     * we sleep.
+     */
+    private PowerManager.WakeLock mShowKeyguardWakeLock;
+
+    /**
      * Does not turn on screen, held while a call to {@link KeyguardViewManager#wakeWhenReadyTq(int)}
      * is called to make sure the device doesn't sleep before it has a chance to poke
      * the wake lock.
@@ -224,6 +230,8 @@
                 PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, 
                 "keyguard");
         mWakeLock.setReferenceCounted(false);
+        mShowKeyguardWakeLock = mPM.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "show keyguard");
+        mShowKeyguardWakeLock.setReferenceCounted(false);
 
         mWakeAndHandOff = mPM.newWakeLock(
                 PowerManager.PARTIAL_WAKE_LOCK,
@@ -542,6 +550,8 @@
      */
     private void showLocked() {
         if (DEBUG) Log.d(TAG, "showLocked");
+        // ensure we stay awake until we are finished displaying the keyguard
+        mShowKeyguardWakeLock.acquire();
         Message msg = mHandler.obtainMessage(SHOW);
         mHandler.sendMessage(msg);
     }
@@ -855,6 +865,7 @@
                 ActivityManagerNative.getDefault().closeSystemDialogs("lock");
             } catch (RemoteException e) {
             }
+            mShowKeyguardWakeLock.release();
         }
     }