public void keyguardDone(boolean authenticated, boolean wakeup) {
    synchronized (this) {
      EventLog.writeEvent(70000, 2);
      if (DEBUG) Log.d(TAG, "keyguardDone(" + authenticated + ")");
      Message msg = mHandler.obtainMessage(KEYGUARD_DONE);
      msg.arg1 = wakeup ? 1 : 0;
      mHandler.sendMessage(msg);

      if (authenticated) {
        mUpdateMonitor.clearFailedAttempts();
      }

      if (mExitSecureCallback != null) {
        mExitSecureCallback.onKeyguardExitResult(authenticated);
        mExitSecureCallback = null;

        if (authenticated) {
          // after succesfully exiting securely, no need to reshow
          // the keyguard when they've released the lock
          mExternallyEnabled = true;
          mNeedToReshowWhenReenabled = false;
        }
      }
    }
  }
 /** @see android.app.KeyguardManager#exitKeyguardSecurely */
 public void verifyUnlock(WindowManagerPolicy.OnKeyguardExitResult callback) {
   synchronized (this) {
     if (DEBUG) Log.d(TAG, "verifyUnlock");
     if (!mUpdateMonitor.isDeviceProvisioned()) {
       // don't allow this api when the device isn't provisioned
       if (DEBUG) Log.d(TAG, "ignoring because device isn't provisioned");
       callback.onKeyguardExitResult(false);
     } else if (mExternallyEnabled) {
       // this only applies when the user has externally disabled the
       // keyguard.  this is unexpected and means the user is not
       // using the api properly.
       Log.w(TAG, "verifyUnlock called when not externally disabled");
       callback.onKeyguardExitResult(false);
     } else if (mExitSecureCallback != null) {
       // already in progress with someone else
       callback.onKeyguardExitResult(false);
     } else {
       mExitSecureCallback = callback;
       verifyUnlockLocked();
     }
   }
 }
  /**
   * Same semantics as {@link WindowManagerPolicy#enableKeyguard}; provide a way for external stuff
   * to override normal keyguard behavior. For instance the phone app disables the keyguard when it
   * receives incoming calls.
   */
  public void setKeyguardEnabled(boolean enabled) {
    synchronized (this) {
      if (DEBUG) Log.d(TAG, "setKeyguardEnabled(" + enabled + ")");

      mExternallyEnabled = enabled;

      if (!enabled && mShowing) {
        if (mExitSecureCallback != null) {
          if (DEBUG) Log.d(TAG, "in process of verifyUnlock request, ignoring");
          // we're in the process of handling a request to verify the user
          // can get past the keyguard. ignore extraneous requests to disable / reenable
          return;
        }

        // hiding keyguard that is showing, remember to reshow later
        if (DEBUG)
          Log.d(TAG, "remembering to reshow, hiding keyguard, " + "disabling status bar expansion");
        mNeedToReshowWhenReenabled = true;
        hideLocked();
      } else if (enabled && mNeedToReshowWhenReenabled) {
        // reenabled after previously hidden, reshow
        if (DEBUG) Log.d(TAG, "previously hidden, reshowing, reenabling " + "status bar expansion");
        mNeedToReshowWhenReenabled = false;

        if (mExitSecureCallback != null) {
          if (DEBUG) Log.d(TAG, "onKeyguardExitResult(false), resetting");
          mExitSecureCallback.onKeyguardExitResult(false);
          mExitSecureCallback = null;
          resetStateLocked();
        } else {
          showLocked();

          // block until we know the keygaurd is done drawing (and post a message
          // to unblock us after a timeout so we don't risk blocking too long
          // and causing an ANR).
          mWaitingUntilKeyguardVisible = true;
          mHandler.sendEmptyMessageDelayed(KEYGUARD_DONE_DRAWING, KEYGUARD_DONE_DRAWING_TIMEOUT_MS);
          if (DEBUG) Log.d(TAG, "waiting until mWaitingUntilKeyguardVisible is false");
          while (mWaitingUntilKeyguardVisible) {
            try {
              wait();
            } catch (InterruptedException e) {
              Thread.currentThread().interrupt();
            }
          }
          if (DEBUG) Log.d(TAG, "done waiting for mWaitingUntilKeyguardVisible");
        }
      }
    }
  }
  /**
   * Called to let us know the screen was turned off.
   *
   * @param why either {@link WindowManagerPolicy#OFF_BECAUSE_OF_USER}, {@link
   *     WindowManagerPolicy#OFF_BECAUSE_OF_TIMEOUT} or {@link
   *     WindowManagerPolicy#OFF_BECAUSE_OF_PROX_SENSOR}.
   */
  public void onScreenTurnedOff(int why) {
    synchronized (this) {
      mScreenOn = false;
      if (DEBUG) Log.d(TAG, "onScreenTurnedOff(" + why + ")");

      if (mExitSecureCallback != null) {
        if (DEBUG) Log.d(TAG, "pending exit secure callback cancelled");
        mExitSecureCallback.onKeyguardExitResult(false);
        mExitSecureCallback = null;
        if (!mExternallyEnabled) {
          hideLocked();
        }
      } else if (mShowing) {
        notifyScreenOffLocked();
        resetStateLocked();
      } else if (why == WindowManagerPolicy.OFF_BECAUSE_OF_TIMEOUT) {
        // if the screen turned off because of timeout, set an alarm
        // to enable it a little bit later (i.e, give the user a chance
        // to turn the screen back on within a certain window without
        // having to unlock the screen)
        long when = SystemClock.elapsedRealtime() + KEYGUARD_DELAY_MS;
        Intent intent = new Intent(DELAYED_KEYGUARD_ACTION);
        intent.putExtra("seq", mDelayedShowingSequence);
        PendingIntent sender =
            PendingIntent.getBroadcast(mContext, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
        mAlarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, when, sender);
        if (DEBUG)
          Log.d(TAG, "setting alarm to turn off keyguard, seq = " + mDelayedShowingSequence);

      } else if (why == WindowManagerPolicy.OFF_BECAUSE_OF_PROX_SENSOR) {
        // Do not enable the keyguard if the prox sensor forced the screen off.
      } else {
        doKeyguard();
      }
    }
  }
  /**
   * Called to let us know the screen was turned off.
   *
   * @param why either {@link WindowManagerPolicy#OFF_BECAUSE_OF_USER}, {@link
   *     WindowManagerPolicy#OFF_BECAUSE_OF_TIMEOUT} or {@link
   *     WindowManagerPolicy#OFF_BECAUSE_OF_PROX_SENSOR}.
   */
  public void onScreenTurnedOff(int why) {
    synchronized (this) {
      mScreenOn = false;
      if (DEBUG) Log.d(TAG, "onScreenTurnedOff(" + why + ")");

      if (mExitSecureCallback != null) {
        if (DEBUG) Log.d(TAG, "pending exit secure callback cancelled");
        mExitSecureCallback.onKeyguardExitResult(false);
        mExitSecureCallback = null;
        if (!mExternallyEnabled) {
          hideLocked();
        }
      } else if (mShowing) {
        notifyScreenOffLocked();
        resetStateLocked();
      } else if (why == WindowManagerPolicy.OFF_BECAUSE_OF_TIMEOUT) {
        // always do keyguard show before suspend
        doKeyguardLocked();
        /*
        // if the screen turned off because of timeout, set an alarm
        // to enable it a little bit later (i.e, give the user a chance
        // to turn the screen back on within a certain window without
        // having to unlock the screen)
        final ContentResolver cr = mContext.getContentResolver();

        // From DisplaySettings
        long displayTimeout = Settings.System.getInt(cr, SCREEN_OFF_TIMEOUT,
                KEYGUARD_DISPLAY_TIMEOUT_DELAY_DEFAULT);

        // From SecuritySettings
        final long lockAfterTimeout = Settings.Secure.getInt(cr,
                Settings.Secure.LOCK_SCREEN_LOCK_AFTER_TIMEOUT,
                KEYGUARD_LOCK_AFTER_DELAY_DEFAULT);

        // From DevicePolicyAdmin
        final long policyTimeout = mLockPatternUtils.getDevicePolicyManager()
                .getMaximumTimeToLock(null);

        long timeout;
        if (policyTimeout > 0) {
            // policy in effect. Make sure we don't go beyond policy limit.
            displayTimeout = Math.max(displayTimeout, 0); // ignore negative values
            timeout = Math.min(policyTimeout - displayTimeout, lockAfterTimeout);
        } else {
            timeout = lockAfterTimeout;
        }

        if (timeout <= 0) {
            // Lock now
            mSuppressNextLockSound = true;
            doKeyguardLocked();
        } else {
            // Lock in the future
            long when = SystemClock.elapsedRealtime() + timeout;
            Intent intent = new Intent(DELAYED_KEYGUARD_ACTION);
            intent.putExtra("seq", mDelayedShowingSequence);
            PendingIntent sender = PendingIntent.getBroadcast(mContext,
                    0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
            //mAlarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, when,
            //        sender);

            mAlarmManager.set(AlarmManager.ELAPSED_REALTIME, when,
                    sender);
            if (DEBUG) Log.d(TAG, "setting alarm to turn off keyguard, seq = "
                             + mDelayedShowingSequence);
        }
        */
      } else if (why == WindowManagerPolicy.OFF_BECAUSE_OF_PROX_SENSOR) {
        // Do not enable the keyguard if the prox sensor forced the screen off.
      } else {
        doKeyguardLocked();
      }
    }
  }