@@ -535,50 +535,7 @@ public void onScreenTurnedOff(int why) {
535535 resetStateLocked (null );
536536 } else if (why == WindowManagerPolicy .OFF_BECAUSE_OF_TIMEOUT
537537 || (why == WindowManagerPolicy .OFF_BECAUSE_OF_USER && !lockImmediately )) {
538- // if the screen turned off because of timeout or the user hit the power button
539- // and we don't need to lock immediately, set an alarm
540- // to enable it a little bit later (i.e, give the user a chance
541- // to turn the screen back on within a certain window without
542- // having to unlock the screen)
543- final ContentResolver cr = mContext .getContentResolver ();
544-
545- // From DisplaySettings
546- long displayTimeout = Settings .System .getInt (cr , SCREEN_OFF_TIMEOUT ,
547- KEYGUARD_DISPLAY_TIMEOUT_DELAY_DEFAULT );
548-
549- // From SecuritySettings
550- final long lockAfterTimeout = Settings .Secure .getInt (cr ,
551- Settings .Secure .LOCK_SCREEN_LOCK_AFTER_TIMEOUT ,
552- KEYGUARD_LOCK_AFTER_DELAY_DEFAULT );
553-
554- // From DevicePolicyAdmin
555- final long policyTimeout = mLockPatternUtils .getDevicePolicyManager ()
556- .getMaximumTimeToLock (null , mLockPatternUtils .getCurrentUser ());
557-
558- long timeout ;
559- if (policyTimeout > 0 ) {
560- // policy in effect. Make sure we don't go beyond policy limit.
561- displayTimeout = Math .max (displayTimeout , 0 ); // ignore negative values
562- timeout = Math .min (policyTimeout - displayTimeout , lockAfterTimeout );
563- } else {
564- timeout = lockAfterTimeout ;
565- }
566-
567- if (timeout <= 0 ) {
568- // Lock now
569- mSuppressNextLockSound = true ;
570- doKeyguardLocked ();
571- } else {
572- // Lock in the future
573- long when = SystemClock .elapsedRealtime () + timeout ;
574- Intent intent = new Intent (DELAYED_KEYGUARD_ACTION );
575- intent .putExtra ("seq" , mDelayedShowingSequence );
576- PendingIntent sender = PendingIntent .getBroadcast (mContext ,
577- 0 , intent , PendingIntent .FLAG_CANCEL_CURRENT );
578- mAlarmManager .set (AlarmManager .ELAPSED_REALTIME_WAKEUP , when , sender );
579- if (DEBUG ) Log .d (TAG , "setting alarm to turn off keyguard, seq = "
580- + mDelayedShowingSequence );
581- }
538+ doKeyguardLaterLocked ();
582539 } else if (why == WindowManagerPolicy .OFF_BECAUSE_OF_PROX_SENSOR ) {
583540 // Do not enable the keyguard if the prox sensor forced the screen off.
584541 } else {
@@ -587,13 +544,64 @@ public void onScreenTurnedOff(int why) {
587544 }
588545 }
589546
547+ private void doKeyguardLaterLocked () {
548+ // if the screen turned off because of timeout or the user hit the power button
549+ // and we don't need to lock immediately, set an alarm
550+ // to enable it a little bit later (i.e, give the user a chance
551+ // to turn the screen back on within a certain window without
552+ // having to unlock the screen)
553+ final ContentResolver cr = mContext .getContentResolver ();
554+
555+ // From DisplaySettings
556+ long displayTimeout = Settings .System .getInt (cr , SCREEN_OFF_TIMEOUT ,
557+ KEYGUARD_DISPLAY_TIMEOUT_DELAY_DEFAULT );
558+
559+ // From SecuritySettings
560+ final long lockAfterTimeout = Settings .Secure .getInt (cr ,
561+ Settings .Secure .LOCK_SCREEN_LOCK_AFTER_TIMEOUT ,
562+ KEYGUARD_LOCK_AFTER_DELAY_DEFAULT );
563+
564+ // From DevicePolicyAdmin
565+ final long policyTimeout = mLockPatternUtils .getDevicePolicyManager ()
566+ .getMaximumTimeToLock (null , mLockPatternUtils .getCurrentUser ());
567+
568+ long timeout ;
569+ if (policyTimeout > 0 ) {
570+ // policy in effect. Make sure we don't go beyond policy limit.
571+ displayTimeout = Math .max (displayTimeout , 0 ); // ignore negative values
572+ timeout = Math .min (policyTimeout - displayTimeout , lockAfterTimeout );
573+ } else {
574+ timeout = lockAfterTimeout ;
575+ }
576+
577+ if (timeout <= 0 ) {
578+ // Lock now
579+ mSuppressNextLockSound = true ;
580+ doKeyguardLocked ();
581+ } else {
582+ // Lock in the future
583+ long when = SystemClock .elapsedRealtime () + timeout ;
584+ Intent intent = new Intent (DELAYED_KEYGUARD_ACTION );
585+ intent .putExtra ("seq" , mDelayedShowingSequence );
586+ PendingIntent sender = PendingIntent .getBroadcast (mContext ,
587+ 0 , intent , PendingIntent .FLAG_CANCEL_CURRENT );
588+ mAlarmManager .set (AlarmManager .ELAPSED_REALTIME_WAKEUP , when , sender );
589+ if (DEBUG ) Log .d (TAG , "setting alarm to turn off keyguard, seq = "
590+ + mDelayedShowingSequence );
591+ }
592+ }
593+
594+ private void cancelDoKeyguardLaterLocked () {
595+ mDelayedShowingSequence ++;
596+ }
597+
590598 /**
591599 * Let's us know the screen was turned on.
592600 */
593601 public void onScreenTurnedOn (KeyguardViewManager .ShowListener showListener ) {
594602 synchronized (this ) {
595603 mScreenOn = true ;
596- mDelayedShowingSequence ++ ;
604+ cancelDoKeyguardLaterLocked () ;
597605 if (DEBUG ) Log .d (TAG , "onScreenTurnedOn, seq = " + mDelayedShowingSequence );
598606 if (showListener != null ) {
599607 notifyScreenOnLocked (showListener );
@@ -612,6 +620,29 @@ private void maybeSendUserPresentBroadcast() {
612620 }
613621 }
614622
623+ /**
624+ * A dream started. We should lock after the usual screen-off lock timeout but only
625+ * if there is a secure lock pattern.
626+ */
627+ public void onDreamingStarted () {
628+ synchronized (this ) {
629+ if (mScreenOn && mLockPatternUtils .isSecure ()) {
630+ doKeyguardLaterLocked ();
631+ }
632+ }
633+ }
634+
635+ /**
636+ * A dream stopped.
637+ */
638+ public void onDreamingStopped () {
639+ synchronized (this ) {
640+ if (mScreenOn ) {
641+ cancelDoKeyguardLaterLocked ();
642+ }
643+ }
644+ }
645+
615646 /**
616647 * Same semantics as {@link WindowManagerPolicy#enableKeyguard}; provide
617648 * a way for external stuff to override normal keyguard behavior. For instance
0 commit comments