Simplify wake management code in keyguard.

Removes onWakeKey/Motion handling from keyguard since it's no longer used.
The legacy code was originally intended to have keyguard filter wake events
which is now done in PhoneWindowManager. Ultimately it just needs to call
PowerManager.wakeUp() since keyguard no longer filters these keys.

Change-Id: I5b8ef9b422abf850a85b57f21944e5eb09fbedc2
diff --git a/core/java/com/android/internal/policy/IKeyguardService.aidl b/core/java/com/android/internal/policy/IKeyguardService.aidl
index 880464d..d1f7fa3 100644
--- a/core/java/com/android/internal/policy/IKeyguardService.aidl
+++ b/core/java/com/android/internal/policy/IKeyguardService.aidl
@@ -30,8 +30,6 @@
     oneway void keyguardDone(boolean authenticated, boolean wakeup);
     oneway void setHidden(boolean isHidden);
     oneway void dismiss();
-    oneway void onWakeKeyWhenKeyguardShowing(int keyCode);
-    oneway void onWakeMotionWhenKeyguardShowing();
     oneway void onDreamingStarted();
     oneway void onDreamingStopped();
     oneway void onScreenTurnedOff(int reason);
diff --git a/packages/Keyguard/src/com/android/keyguard/KeyguardHostView.java b/packages/Keyguard/src/com/android/keyguard/KeyguardHostView.java
index 0970248..2904f4c 100644
--- a/packages/Keyguard/src/com/android/keyguard/KeyguardHostView.java
+++ b/packages/Keyguard/src/com/android/keyguard/KeyguardHostView.java
@@ -119,6 +119,8 @@
 
     protected int mClientGeneration;
 
+    protected boolean mShowSecurityWhenReturn;
+
     /*package*/ interface UserSwitcherCallback {
         void hideSecurityView(int duration);
         void showSecurityView();
@@ -872,14 +874,6 @@
         }
     };
 
-    protected boolean mShowSecurityWhenReturn;
-
-    @Override
-    public void reset() {
-        mIsVerifyUnlockOnly = false;
-        mAppWidgetContainer.setCurrentPage(getWidgetPosition(R.id.keyguard_status_view));
-    }
-
     /**
      * Sets an action to perform when keyguard is dismissed.
      * @param action
@@ -1029,39 +1023,6 @@
         showPrimarySecurityScreen(false);
     }
 
-    private boolean isSecure() {
-        SecurityMode mode = mSecurityModel.getSecurityMode();
-        switch (mode) {
-            case Pattern:
-                return mLockPatternUtils.isLockPatternEnabled();
-            case Password:
-            case PIN:
-                return mLockPatternUtils.isLockPasswordEnabled();
-            case SimPin:
-            case SimPuk:
-            case Account:
-                return true;
-            case None:
-                return false;
-            default:
-                throw new IllegalStateException("Unknown security mode " + mode);
-        }
-    }
-
-    @Override
-    public void wakeWhenReadyTq(int keyCode) {
-        if (DEBUG) Log.d(TAG, "onWakeKey");
-        if (keyCode == KeyEvent.KEYCODE_MENU && isSecure()) {
-            if (DEBUG) Log.d(TAG, "switching screens to unlock screen because wake key was MENU");
-            showSecurityScreen(SecurityMode.None);
-        } else {
-            if (DEBUG) Log.d(TAG, "poking wake lock immediately");
-        }
-        if (mViewMediatorCallback != null) {
-            mViewMediatorCallback.wakeUp();
-        }
-    }
-
     @Override
     public void verifyUnlock() {
         SecurityMode securityMode = mSecurityModel.getSecurityMode();
diff --git a/packages/Keyguard/src/com/android/keyguard/KeyguardService.java b/packages/Keyguard/src/com/android/keyguard/KeyguardService.java
index f89ad65..a70e5bd 100644
--- a/packages/Keyguard/src/com/android/keyguard/KeyguardService.java
+++ b/packages/Keyguard/src/com/android/keyguard/KeyguardService.java
@@ -93,14 +93,6 @@
         public void dismiss() {
             mKeyguardViewMediator.dismiss();
         }
-        public void onWakeKeyWhenKeyguardShowing(int keyCode) {
-            checkPermission();
-            mKeyguardViewMediator.onWakeKeyWhenKeyguardShowing(keyCode);
-        }
-        public void onWakeMotionWhenKeyguardShowing() {
-            checkPermission();
-            mKeyguardViewMediator.onWakeMotionWhenKeyguardShowing();
-        }
         public void onDreamingStarted() {
             checkPermission();
             mKeyguardViewMediator.onDreamingStarted();
diff --git a/packages/Keyguard/src/com/android/keyguard/KeyguardViewBase.java b/packages/Keyguard/src/com/android/keyguard/KeyguardViewBase.java
index 200fb3c..714dfbd 100644
--- a/packages/Keyguard/src/com/android/keyguard/KeyguardViewBase.java
+++ b/packages/Keyguard/src/com/android/keyguard/KeyguardViewBase.java
@@ -90,11 +90,6 @@
     }
 
     /**
-     * Called when you need to reset the state of your view.
-     */
-    abstract public void reset();
-
-    /**
      * Called when the screen turned off.
      */
     abstract public void onScreenTurnedOff();
@@ -110,21 +105,6 @@
     abstract public void show();
 
     /**
-     * Called when a key has woken the device to give us a chance to adjust our
-     * state according the the key.  We are responsible for waking the device
-     * (by poking the wake lock) once we are ready.
-     *
-     * The 'Tq' suffix is per the documentation in {@link android.view.WindowManagerPolicy}.
-     * Be sure not to take any action that takes a long time; any significant
-     * action should be posted to a handler.
-     *
-     * @param keyCode The wake key, which may be relevant for configuring the
-     *   keyguard.  May be {@link KeyEvent#KEYCODE_UNKNOWN} if waking for a reason
-     *   other than a key press.
-     */
-    abstract public void wakeWhenReadyTq(int keyCode);
-
-    /**
      * Verify that the user can get past the keyguard securely.  This is called,
      * for example, when the phone disables the keyguard but then wants to launch
      * something else that requires secure access.
diff --git a/packages/Keyguard/src/com/android/keyguard/KeyguardViewManager.java b/packages/Keyguard/src/com/android/keyguard/KeyguardViewManager.java
index b6c35bd..a1d11cd 100644
--- a/packages/Keyguard/src/com/android/keyguard/KeyguardViewManager.java
+++ b/packages/Keyguard/src/com/android/keyguard/KeyguardViewManager.java
@@ -384,27 +384,6 @@
     }
 
     /**
-     * A key has woken the device.  We use this to potentially adjust the state
-     * of the lock screen based on the key.
-     *
-     * The 'Tq' suffix is per the documentation in {@link android.view.WindowManagerPolicy}.
-     * Be sure not to take any action that takes a long time; any significant
-     * action should be posted to a handler.
-     *
-     * @param keyCode The wake key.  May be {@link KeyEvent#KEYCODE_UNKNOWN} if waking
-     * for a reason other than a key press.
-     */
-    public boolean wakeWhenReadyTq(int keyCode) {
-        if (DEBUG) Log.d(TAG, "wakeWhenReady(" + keyCode + ")");
-        if (mKeyguardView != null) {
-            mKeyguardView.wakeWhenReadyTq(keyCode);
-            return true;
-        }
-        Log.w(TAG, "mKeyguardView is null in wakeWhenReadyTq");
-        return false;
-    }
-
-    /**
      * Hides the keyguard view
      */
     public synchronized void hide() {
diff --git a/packages/Keyguard/src/com/android/keyguard/KeyguardViewMediator.java b/packages/Keyguard/src/com/android/keyguard/KeyguardViewMediator.java
index 4ce0dcd..597fb3b 100644
--- a/packages/Keyguard/src/com/android/keyguard/KeyguardViewMediator.java
+++ b/packages/Keyguard/src/com/android/keyguard/KeyguardViewMediator.java
@@ -114,7 +114,6 @@
     private static final int VERIFY_UNLOCK = 5;
     private static final int NOTIFY_SCREEN_OFF = 6;
     private static final int NOTIFY_SCREEN_ON = 7;
-    private static final int WAKE_WHEN_READY = 8;
     private static final int KEYGUARD_DONE = 9;
     private static final int KEYGUARD_DONE_DRAWING = 10;
     private static final int KEYGUARD_DONE_AUTHENTICATING = 11;
@@ -180,14 +179,6 @@
      */
     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.
-     * @see #wakeWhenReady(int)
-     */
-    private PowerManager.WakeLock mWakeAndHandOff;
-
     private KeyguardViewManager mKeyguardViewManager;
 
     // these are protected by synchronized (this)
@@ -268,12 +259,6 @@
      * various things.
      */
     public interface ViewMediatorCallback {
-
-        /**
-         * Wake the device immediately.
-         */
-        void wakeUp();
-
         /**
          * Reports user activity and requests that the screen stay on.
          */
@@ -439,10 +424,6 @@
     };
 
     ViewMediatorCallback mViewMediatorCallback = new ViewMediatorCallback() {
-        public void wakeUp() {
-            KeyguardViewMediator.this.wakeUp();
-        }
-
         public void userActivity() {
             KeyguardViewMediator.this.userActivity();
         }
@@ -475,10 +456,6 @@
         }
     };
 
-    public void wakeUp() {
-        mPM.wakeUp(SystemClock.uptimeMillis());
-    }
-
     private void userActivity() {
         userActivity(AWAKE_INTERVAL_DEFAULT_MS);
     }
@@ -501,9 +478,6 @@
         mShowKeyguardWakeLock = mPM.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "show keyguard");
         mShowKeyguardWakeLock.setReferenceCounted(false);
 
-        mWakeAndHandOff = mPM.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "keyguardWakeAndHandOff");
-        mWakeAndHandOff.setReferenceCounted(false);
-
         mContext.registerReceiver(mBroadcastReceiver, new IntentFilter(DELAYED_KEYGUARD_ACTION));
 
         mAlarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
@@ -981,27 +955,6 @@
     }
 
     /**
-     * Send message to keyguard telling it about a wake key so it can adjust
-     * its state accordingly and then poke the wake lock when it is ready.
-     * @param keyCode The wake key.
-     * @see #handleWakeWhenReady
-     * @see #onWakeKeyWhenKeyguardShowing(int)
-     */
-    private void wakeWhenReady(int keyCode) {
-        if (DBG_WAKE) Log.d(TAG, "wakeWhenReady(" + keyCode + ")");
-
-        /**
-         * acquire the handoff lock that will keep the cpu running.  this will
-         * be released once the keyguard has set itself up and poked the other wakelock
-         * in {@link #handleWakeWhenReady(int)}
-         */
-        mWakeAndHandOff.acquire();
-
-        Message msg = mHandler.obtainMessage(WAKE_WHEN_READY, keyCode, 0);
-        mHandler.sendMessage(msg);
-    }
-
-    /**
      * Send message to keyguard telling it to show itself
      * @see #handleShow()
      */
@@ -1056,74 +1009,14 @@
         }
     };
 
-    /**
-     * When a key is received when the screen is off and the keyguard is showing,
-     * we need to decide whether to actually turn on the screen, and if so, tell
-     * the keyguard to prepare itself and poke the wake lock when it is ready.
-     *
-     * The 'Tq' suffix is per the documentation in {@link WindowManagerPolicy}.
-     * Be sure not to take any action that takes a long time; any significant
-     * action should be posted to a handler.
-     *
-     * @param keyCode The keycode of the key that woke the device
-     */
-    public void onWakeKeyWhenKeyguardShowing(int keyCode) {
-        if (DEBUG) Log.d(TAG, "onWakeKeyWhenKeyguardShowing(" + keyCode + ")");
-
-        // give the keyguard view manager a chance to adjust the state of the
-        // keyguard based on the key that woke the device before poking
-        // the wake lock
-        wakeWhenReady(keyCode);
-    }
-
-    /**
-     * When a wake motion such as an external mouse movement is received when the screen
-     * is off and the keyguard is showing, we need to decide whether to actually turn
-     * on the screen, and if so, tell the keyguard to prepare itself and poke the wake
-     * lock when it is ready.
-     *
-     * The 'Tq' suffix is per the documentation in {@link WindowManagerPolicy}.
-     * Be sure not to take any action that takes a long time; any significant
-     * action should be posted to a handler.
-     */
-    public void onWakeMotionWhenKeyguardShowing() {
-        if (DEBUG) Log.d(TAG, "onWakeMotionWhenKeyguardShowing()");
-
-        // give the keyguard view manager a chance to adjust the state of the
-        // keyguard based on the key that woke the device before poking
-        // the wake lock
-        wakeWhenReady(KeyEvent.KEYCODE_UNKNOWN);
-    }
-
     public void keyguardDone(boolean authenticated, boolean wakeup) {
         mKeyguardDonePending = false;
         synchronized (this) {
             EventLog.writeEvent(70000, 2);
             if (DEBUG) Log.d(TAG, "keyguardDone(" + authenticated + ")");
-            Message msg = mHandler.obtainMessage(KEYGUARD_DONE);
-            msg.arg1 = wakeup ? 1 : 0;
+            Message msg = mHandler.obtainMessage(KEYGUARD_DONE, authenticated ? 1 : 0,
+                    wakeup ? 1 : 0);
             mHandler.sendMessage(msg);
-
-            if (authenticated) {
-                mUpdateMonitor.clearFailedUnlockAttempts();
-            }
-
-            if (mExitSecureCallback != null) {
-                try {
-                    mExitSecureCallback.onKeyguardExitResult(authenticated);
-                } catch (RemoteException e) {
-                    Slog.w(TAG, "Failed to call onKeyguardExitResult(" + authenticated + ")", e);
-                }
-
-                mExitSecureCallback = null;
-
-                if (authenticated) {
-                    // after succesfully exiting securely, no need to reshow
-                    // the keyguard when they've released the lock
-                    mExternallyEnabled = true;
-                    mNeedToReshowWhenReenabled = false;
-                }
-            }
         }
     }
 
@@ -1156,11 +1049,8 @@
                 case NOTIFY_SCREEN_ON:
                     handleNotifyScreenOn((IKeyguardShowCallback) msg.obj);
                     return;
-                case WAKE_WHEN_READY:
-                    handleWakeWhenReady(msg.arg1);
-                    return;
                 case KEYGUARD_DONE:
-                    handleKeyguardDone(msg.arg1 != 0);
+                    handleKeyguardDone(msg.arg1 != 0, msg.arg2 != 0);
                     return;
                 case KEYGUARD_DONE_DRAWING:
                     handleKeyguardDoneDrawing();
@@ -1187,13 +1077,31 @@
      * @see #keyguardDone
      * @see #KEYGUARD_DONE
      */
-    private void handleKeyguardDone(boolean wakeup) {
+    private void handleKeyguardDone(boolean authenticated, boolean wakeup) {
         if (DEBUG) Log.d(TAG, "handleKeyguardDone");
-        handleHide();
-        if (wakeup) {
-            wakeUp();
+
+        if (authenticated) {
+            mUpdateMonitor.clearFailedUnlockAttempts();
         }
 
+        if (mExitSecureCallback != null) {
+            try {
+                mExitSecureCallback.onKeyguardExitResult(authenticated);
+            } catch (RemoteException e) {
+                Slog.w(TAG, "Failed to call onKeyguardExitResult(" + authenticated + ")", e);
+            }
+
+            mExitSecureCallback = null;
+
+            if (authenticated) {
+                // after succesfully exiting securely, no need to reshow
+                // the keyguard when they've released the lock
+                mExternallyEnabled = true;
+                mNeedToReshowWhenReenabled = false;
+            }
+        }
+
+        handleHide();
         sendUserPresentBroadcast();
     }
 
@@ -1296,10 +1204,6 @@
     private void handleHide() {
         synchronized (KeyguardViewMediator.this) {
             if (DEBUG) Log.d(TAG, "handleHide");
-            if (mWakeAndHandOff.isHeld()) {
-                Log.w(TAG, "attempt to hide the keyguard while waking, ignored");
-                return;
-            }
 
             // only play "unlock" noises if not on a call (since the incall UI
             // disables the keyguard)
@@ -1375,31 +1279,6 @@
     }
 
     /**
-     * Handle message sent by {@link #wakeWhenReady(int)}
-     * @param keyCode The key that woke the device.
-     * @see #WAKE_WHEN_READY
-     */
-    private void handleWakeWhenReady(int keyCode) {
-        synchronized (KeyguardViewMediator.this) {
-            if (DBG_WAKE) Log.d(TAG, "handleWakeWhenReady(" + keyCode + ")");
-
-            // this should result in a call to 'poke wakelock' which will set a timeout
-            // on releasing the wakelock
-            if (!mKeyguardViewManager.wakeWhenReadyTq(keyCode)) {
-                // poke wakelock ourselves if keyguard is no longer active
-                Log.w(TAG, "mKeyguardViewManager.wakeWhenReadyTq did not poke wake lock, so poke it ourselves");
-                userActivity();
-            }
-
-            /**
-             * Now that the keyguard is ready and has poked the wake lock, we can
-             * release the handoff wakelock
-             */
-            mWakeAndHandOff.release();
-        }
-    }
-
-    /**
      * Handle message sent by {@link #resetStateLocked(Bundle)}
      * @see #RESET
      */
diff --git a/policy/src/com/android/internal/policy/impl/PhoneWindowManager.java b/policy/src/com/android/internal/policy/impl/PhoneWindowManager.java
index 56d0ec0..0b8b028 100644
--- a/policy/src/com/android/internal/policy/impl/PhoneWindowManager.java
+++ b/policy/src/com/android/internal/policy/impl/PhoneWindowManager.java
@@ -3523,11 +3523,7 @@
         updateRotation(true);
 
         if (lidOpen) {
-            if (keyguardIsShowingTq()) {
-                mKeyguardDelegate.onWakeKeyWhenKeyguardShowingTq(KeyEvent.KEYCODE_POWER);
-            } else {
-                mPowerManager.wakeUp(SystemClock.uptimeMillis());
-            }
+            mPowerManager.wakeUp(SystemClock.uptimeMillis());
         } else if (!mLidControlsSleep) {
             mPowerManager.userActivity(SystemClock.uptimeMillis(), false);
         }
@@ -3745,13 +3741,7 @@
             // to wake the device but don't pass the key to the application.
             result = 0;
             if (down && isWakeKey && isWakeKeyWhenScreenOff(keyCode)) {
-                if (keyguardActive) {
-                    // If the keyguard is showing, let it wake the device when ready.
-                    mKeyguardDelegate.onWakeKeyWhenKeyguardShowingTq(keyCode);
-                } else {
-                    // Otherwise, wake the device ourselves.
-                    result |= ACTION_WAKE_UP;
-                }
+                result |= ACTION_WAKE_UP;
             }
         }
 
@@ -4017,13 +4007,7 @@
         final boolean isWakeMotion = (policyFlags
                 & (WindowManagerPolicy.FLAG_WAKE | WindowManagerPolicy.FLAG_WAKE_DROPPED)) != 0;
         if (isWakeMotion) {
-            if (mKeyguardDelegate != null && mKeyguardDelegate.isShowing()) {
-                // If the keyguard is showing, let it decide what to do with the wake motion.
-                mKeyguardDelegate.onWakeMotionWhenKeyguardShowing();
-            } else {
-                // Otherwise, wake the device ourselves.
-                result |= ACTION_WAKE_UP;
-            }
+            result |= ACTION_WAKE_UP;
         }
         return result;
     }
diff --git a/policy/src/com/android/internal/policy/impl/keyguard/KeyguardServiceDelegate.java b/policy/src/com/android/internal/policy/impl/keyguard/KeyguardServiceDelegate.java
index 2bb94be..874076a 100644
--- a/policy/src/com/android/internal/policy/impl/keyguard/KeyguardServiceDelegate.java
+++ b/policy/src/com/android/internal/policy/impl/keyguard/KeyguardServiceDelegate.java
@@ -12,6 +12,7 @@
 import android.os.UserHandle;
 import android.util.Log;
 import android.util.Slog;
+import android.view.KeyEvent;
 import android.view.View;
 import android.view.ViewGroup;
 import android.view.WindowManager;
@@ -21,7 +22,6 @@
 import com.android.internal.policy.IKeyguardShowCallback;
 import com.android.internal.policy.IKeyguardService;
 import com.android.internal.widget.LockPatternUtils;
-import com.android.internal.policy.impl.KeyguardServiceWrapper;
 
 /**
  * A local class that keeps a cache of keyguard state that can be restored in the event
@@ -178,18 +178,6 @@
         return mKeyguardState.secure;
     }
 
-    public void onWakeKeyWhenKeyguardShowingTq(int keycodePower) {
-        if (mKeyguardService != null) {
-            mKeyguardService.onWakeKeyWhenKeyguardShowing(keycodePower);
-        }
-    }
-
-    public void onWakeMotionWhenKeyguardShowing() {
-        if (mKeyguardService != null) {
-            mKeyguardService.onWakeMotionWhenKeyguardShowing();
-        }
-    }
-
     public void onDreamingStarted() {
         if (mKeyguardService != null) {
             mKeyguardService.onDreamingStarted();
diff --git a/policy/src/com/android/internal/policy/impl/KeyguardServiceWrapper.java b/policy/src/com/android/internal/policy/impl/keyguard/KeyguardServiceWrapper.java
similarity index 91%
rename from policy/src/com/android/internal/policy/impl/KeyguardServiceWrapper.java
rename to policy/src/com/android/internal/policy/impl/keyguard/KeyguardServiceWrapper.java
index e649125..6b9c7df 100644
--- a/policy/src/com/android/internal/policy/impl/KeyguardServiceWrapper.java
+++ b/policy/src/com/android/internal/policy/impl/keyguard/KeyguardServiceWrapper.java
@@ -14,7 +14,7 @@
  * limitations under the License.
  */
 
-package com.android.internal.policy.impl;
+package com.android.internal.policy.impl.keyguard;
 
 import android.os.Bundle;
 import android.os.IBinder;
@@ -115,22 +115,6 @@
         }
     }
 
-    public void onWakeKeyWhenKeyguardShowing(int keyCode) {
-        try {
-            mService.onWakeKeyWhenKeyguardShowing(keyCode);
-        } catch (RemoteException e) {
-            Slog.w(TAG , "Remote Exception", e);
-        }
-    }
-
-    public void onWakeMotionWhenKeyguardShowing() {
-        try {
-            mService.onWakeMotionWhenKeyguardShowing();
-        } catch (RemoteException e) {
-            Slog.w(TAG , "Remote Exception", e);
-        }
-    }
-
     public void onDreamingStarted() {
         try {
             mService.onDreamingStarted();