1/* 2 * Copyright (C) 2007 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package com.android.internal.policy.impl.keyguard; 18 19import static android.provider.Settings.System.SCREEN_OFF_TIMEOUT; 20 21import android.app.Activity; 22import android.app.ActivityManagerNative; 23import android.app.AlarmManager; 24import android.app.PendingIntent; 25import android.app.StatusBarManager; 26import android.content.BroadcastReceiver; 27import android.content.ContentResolver; 28import android.content.Context; 29import android.content.Intent; 30import android.content.IntentFilter; 31import android.media.AudioManager; 32import android.media.SoundPool; 33import android.os.Bundle; 34import android.os.Handler; 35import android.os.Looper; 36import android.os.Message; 37import android.os.PowerManager; 38import android.os.RemoteException; 39import android.os.SystemClock; 40import android.os.SystemProperties; 41import android.os.UserHandle; 42import android.os.UserManager; 43import android.provider.Settings; 44import android.telephony.TelephonyManager; 45import android.util.EventLog; 46import android.util.Log; 47import android.view.KeyEvent; 48import android.view.WindowManager; 49import android.view.WindowManagerPolicy; 50 51import com.android.internal.telephony.IccCardConstants; 52import com.android.internal.widget.LockPatternUtils; 53 54 55/** 56 * Mediates requests related to the keyguard. This includes queries about the 57 * state of the keyguard, power management events that effect whether the keyguard 58 * should be shown or reset, callbacks to the phone window manager to notify 59 * it of when the keyguard is showing, and events from the keyguard view itself 60 * stating that the keyguard was succesfully unlocked. 61 * 62 * Note that the keyguard view is shown when the screen is off (as appropriate) 63 * so that once the screen comes on, it will be ready immediately. 64 * 65 * Example queries about the keyguard: 66 * - is {movement, key} one that should wake the keygaurd? 67 * - is the keyguard showing? 68 * - are input events restricted due to the state of the keyguard? 69 * 70 * Callbacks to the phone window manager: 71 * - the keyguard is showing 72 * 73 * Example external events that translate to keyguard view changes: 74 * - screen turned off -> reset the keyguard, and show it so it will be ready 75 * next time the screen turns on 76 * - keyboard is slid open -> if the keyguard is not secure, hide it 77 * 78 * Events from the keyguard view: 79 * - user succesfully unlocked keyguard -> hide keyguard view, and no longer 80 * restrict input events. 81 * 82 * Note: in addition to normal power managment events that effect the state of 83 * whether the keyguard should be showing, external apps and services may request 84 * that the keyguard be disabled via {@link #setKeyguardEnabled(boolean)}. When 85 * false, this will override all other conditions for turning on the keyguard. 86 * 87 * Threading and synchronization: 88 * This class is created by the initialization routine of the {@link WindowManagerPolicy}, 89 * and runs on its thread. The keyguard UI is created from that thread in the 90 * constructor of this class. The apis may be called from other threads, including the 91 * {@link com.android.server.input.InputManagerService}'s and {@link android.view.WindowManager}'s. 92 * Therefore, methods on this class are synchronized, and any action that is pointed 93 * directly to the keyguard UI is posted to a {@link Handler} to ensure it is taken on the UI 94 * thread of the keyguard. 95 */ 96public class KeyguardViewMediator { 97 private static final int KEYGUARD_DISPLAY_TIMEOUT_DELAY_DEFAULT = 30000; 98 final static boolean DEBUG = false; 99 private final static boolean DBG_WAKE = false; 100 101 private final static String TAG = "KeyguardViewMediator"; 102 103 private static final String DELAYED_KEYGUARD_ACTION = 104 "com.android.internal.policy.impl.PhoneWindowManager.DELAYED_KEYGUARD"; 105 106 // used for handler messages 107 private static final int SHOW = 2; 108 private static final int HIDE = 3; 109 private static final int RESET = 4; 110 private static final int VERIFY_UNLOCK = 5; 111 private static final int NOTIFY_SCREEN_OFF = 6; 112 private static final int NOTIFY_SCREEN_ON = 7; 113 private static final int WAKE_WHEN_READY = 8; 114 private static final int KEYGUARD_DONE = 9; 115 private static final int KEYGUARD_DONE_DRAWING = 10; 116 private static final int KEYGUARD_DONE_AUTHENTICATING = 11; 117 private static final int SET_HIDDEN = 12; 118 private static final int KEYGUARD_TIMEOUT = 13; 119 private static final int SHOW_ASSISTANT = 14; 120 121 /** 122 * The default amount of time we stay awake (used for all key input) 123 */ 124 protected static final int AWAKE_INTERVAL_DEFAULT_MS = 10000; 125 126 /** 127 * How long to wait after the screen turns off due to timeout before 128 * turning on the keyguard (i.e, the user has this much time to turn 129 * the screen back on without having to face the keyguard). 130 */ 131 private static final int KEYGUARD_LOCK_AFTER_DELAY_DEFAULT = 5000; 132 133 /** 134 * How long we'll wait for the {@link ViewMediatorCallback#keyguardDoneDrawing()} 135 * callback before unblocking a call to {@link #setKeyguardEnabled(boolean)} 136 * that is reenabling the keyguard. 137 */ 138 private static final int KEYGUARD_DONE_DRAWING_TIMEOUT_MS = 2000; 139 140 /** 141 * Allow the user to expand the status bar when the keyguard is engaged 142 * (without a pattern or password). 143 */ 144 private static final boolean ENABLE_INSECURE_STATUS_BAR_EXPAND = true; 145 146 /** The stream type that the lock sounds are tied to. */ 147 private int mMasterStreamType; 148 149 private Context mContext; 150 private AlarmManager mAlarmManager; 151 private AudioManager mAudioManager; 152 private StatusBarManager mStatusBarManager; 153 private boolean mShowLockIcon; 154 private boolean mShowingLockIcon; 155 156 private boolean mSystemReady; 157 158 // Whether the next call to playSounds() should be skipped. Defaults to 159 // true because the first lock (on boot) should be silent. 160 private boolean mSuppressNextLockSound = true; 161 162 163 /** High level access to the power manager for WakeLocks */ 164 private PowerManager mPM; 165 166 /** UserManager for querying number of users */ 167 private UserManager mUserManager; 168 169 /** 170 * Used to keep the device awake while to ensure the keyguard finishes opening before 171 * we sleep. 172 */ 173 private PowerManager.WakeLock mShowKeyguardWakeLock; 174 175 /** 176 * Does not turn on screen, held while a call to {@link KeyguardViewManager#wakeWhenReadyTq(int)} 177 * is called to make sure the device doesn't sleep before it has a chance to poke 178 * the wake lock. 179 * @see #wakeWhenReady(int) 180 */ 181 private PowerManager.WakeLock mWakeAndHandOff; 182 183 private KeyguardViewManager mKeyguardViewManager; 184 185 // these are protected by synchronized (this) 186 187 /** 188 * External apps (like the phone app) can tell us to disable the keygaurd. 189 */ 190 private boolean mExternallyEnabled = true; 191 192 /** 193 * Remember if an external call to {@link #setKeyguardEnabled} with value 194 * false caused us to hide the keyguard, so that we need to reshow it once 195 * the keygaurd is reenabled with another call with value true. 196 */ 197 private boolean mNeedToReshowWhenReenabled = false; 198 199 // cached value of whether we are showing (need to know this to quickly 200 // answer whether the input should be restricted) 201 private boolean mShowing = false; 202 203 // true if the keyguard is hidden by another window 204 private boolean mHidden = false; 205 206 /** 207 * Helps remember whether the screen has turned on since the last time 208 * it turned off due to timeout. see {@link #onScreenTurnedOff(int)} 209 */ 210 private int mDelayedShowingSequence; 211 212 /** 213 * If the user has disabled the keyguard, then requests to exit, this is 214 * how we'll ultimately let them know whether it was successful. We use this 215 * var being non-null as an indicator that there is an in progress request. 216 */ 217 private WindowManagerPolicy.OnKeyguardExitResult mExitSecureCallback; 218 219 // the properties of the keyguard 220 221 private KeyguardUpdateMonitor mUpdateMonitor; 222 223 private boolean mScreenOn; 224 225 // last known state of the cellular connection 226 private String mPhoneState = TelephonyManager.EXTRA_STATE_IDLE; 227 228 /** 229 * we send this intent when the keyguard is dismissed. 230 */ 231 private Intent mUserPresentIntent; 232 233 /** 234 * {@link #setKeyguardEnabled} waits on this condition when it reenables 235 * the keyguard. 236 */ 237 private boolean mWaitingUntilKeyguardVisible = false; 238 private LockPatternUtils mLockPatternUtils; 239 private boolean mKeyguardDonePending = false; 240 241 private SoundPool mLockSounds; 242 private int mLockSoundId; 243 private int mUnlockSoundId; 244 private int mLockSoundStreamId; 245 246 /** 247 * The volume applied to the lock/unlock sounds. 248 */ 249 private final float mLockSoundVolume; 250 251 /** 252 * The callback used by the keyguard view to tell the {@link KeyguardViewMediator} 253 * various things. 254 */ 255 public interface ViewMediatorCallback { 256 257 /** 258 * Wake the device immediately. 259 */ 260 void wakeUp(); 261 262 /** 263 * Reports user activity and requests that the screen stay on. 264 */ 265 void userActivity(); 266 267 /** 268 * Reports user activity and requests that the screen stay on for at least 269 * the specified amount of time. 270 * @param millis The amount of time in millis. This value is currently ignored. 271 */ 272 void userActivity(long millis); 273 274 /** 275 * Report that the keyguard is done. 276 * @param authenticated Whether the user securely got past the keyguard. 277 * the only reason for this to be false is if the keyguard was instructed 278 * to appear temporarily to verify the user is supposed to get past the 279 * keyguard, and the user fails to do so. 280 */ 281 void keyguardDone(boolean authenticated); 282 283 /** 284 * Report that the keyguard is done drawing. 285 */ 286 void keyguardDoneDrawing(); 287 288 /** 289 * Tell ViewMediator that the current view needs IME input 290 * @param needsInput 291 */ 292 void setNeedsInput(boolean needsInput); 293 294 /** 295 * Tell view mediator that the keyguard view's desired user activity timeout 296 * has changed and needs to be reapplied to the window. 297 */ 298 void onUserActivityTimeoutChanged(); 299 300 /** 301 * Report that the keyguard is dismissable, pending the next keyguardDone call. 302 */ 303 void keyguardDonePending(); 304 } 305 306 KeyguardUpdateMonitorCallback mUpdateCallback = new KeyguardUpdateMonitorCallback() { 307 308 @Override 309 public void onUserSwitched(int userId) { 310 // Note that the mLockPatternUtils user has already been updated from setCurrentUser. 311 // We need to force a reset of the views, since lockNow (called by 312 // ActivityManagerService) will not reconstruct the keyguard if it is already showing. 313 synchronized (KeyguardViewMediator.this) { 314 Bundle options = new Bundle(); 315 options.putBoolean(LockPatternUtils.KEYGUARD_SHOW_USER_SWITCHER, true); 316 options.putBoolean(LockPatternUtils.KEYGUARD_SHOW_SECURITY_CHALLENGE, true); 317 resetStateLocked(options); 318 adjustStatusBarLocked(); 319 // Disable face unlock when the user switches. 320 KeyguardUpdateMonitor.getInstance(mContext).setAlternateUnlockEnabled(false); 321 } 322 } 323 324 @Override 325 public void onUserRemoved(int userId) { 326 mLockPatternUtils.removeUser(userId); 327 } 328 329 @Override 330 void onPhoneStateChanged(int phoneState) { 331 synchronized (KeyguardViewMediator.this) { 332 if (TelephonyManager.CALL_STATE_IDLE == phoneState // call ending 333 && !mScreenOn // screen off 334 && mExternallyEnabled) { // not disabled by any app 335 336 // note: this is a way to gracefully reenable the keyguard when the call 337 // ends and the screen is off without always reenabling the keyguard 338 // each time the screen turns off while in call (and having an occasional ugly 339 // flicker while turning back on the screen and disabling the keyguard again). 340 if (DEBUG) Log.d(TAG, "screen is off and call ended, let's make sure the " 341 + "keyguard is showing"); 342 doKeyguardLocked(); 343 } 344 } 345 }; 346 347 @Override 348 public void onClockVisibilityChanged() { 349 adjustStatusBarLocked(); 350 } 351 352 @Override 353 public void onDeviceProvisioned() { 354 sendUserPresentBroadcast(); 355 } 356 357 @Override 358 public void onSimStateChanged(IccCardConstants.State simState) { 359 if (DEBUG) Log.d(TAG, "onSimStateChanged: " + simState); 360 361 switch (simState) { 362 case NOT_READY: 363 case ABSENT: 364 // only force lock screen in case of missing sim if user hasn't 365 // gone through setup wizard 366 synchronized (this) { 367 if (!mUpdateMonitor.isDeviceProvisioned()) { 368 if (!isShowing()) { 369 if (DEBUG) Log.d(TAG, "ICC_ABSENT isn't showing," 370 + " we need to show the keyguard since the " 371 + "device isn't provisioned yet."); 372 doKeyguardLocked(); 373 } else { 374 resetStateLocked(null); 375 } 376 } 377 } 378 break; 379 case PIN_REQUIRED: 380 case PUK_REQUIRED: 381 synchronized (this) { 382 if (!isShowing()) { 383 if (DEBUG) Log.d(TAG, "INTENT_VALUE_ICC_LOCKED and keygaurd isn't " 384 + "showing; need to show keyguard so user can enter sim pin"); 385 doKeyguardLocked(); 386 } else { 387 resetStateLocked(null); 388 } 389 } 390 break; 391 case PERM_DISABLED: 392 synchronized (this) { 393 if (!isShowing()) { 394 if (DEBUG) Log.d(TAG, "PERM_DISABLED and " 395 + "keygaurd isn't showing."); 396 doKeyguardLocked(); 397 } else { 398 if (DEBUG) Log.d(TAG, "PERM_DISABLED, resetStateLocked to" 399 + "show permanently disabled message in lockscreen."); 400 resetStateLocked(null); 401 } 402 } 403 break; 404 case READY: 405 synchronized (this) { 406 if (isShowing()) { 407 resetStateLocked(null); 408 } 409 } 410 break; 411 } 412 } 413 414 }; 415 416 ViewMediatorCallback mViewMediatorCallback = new ViewMediatorCallback() { 417 public void wakeUp() { 418 KeyguardViewMediator.this.wakeUp(); 419 } 420 421 public void userActivity() { 422 KeyguardViewMediator.this.userActivity(); 423 } 424 425 public void userActivity(long holdMs) { 426 KeyguardViewMediator.this.userActivity(holdMs); 427 } 428 429 public void keyguardDone(boolean authenticated) { 430 KeyguardViewMediator.this.keyguardDone(authenticated, true); 431 } 432 433 public void keyguardDoneDrawing() { 434 mHandler.sendEmptyMessage(KEYGUARD_DONE_DRAWING); 435 } 436 437 @Override 438 public void setNeedsInput(boolean needsInput) { 439 mKeyguardViewManager.setNeedsInput(needsInput); 440 } 441 442 @Override 443 public void onUserActivityTimeoutChanged() { 444 mKeyguardViewManager.updateUserActivityTimeout(); 445 } 446 447 @Override 448 public void keyguardDonePending() { 449 mKeyguardDonePending = true; 450 } 451 }; 452 453 public void wakeUp() { 454 mPM.wakeUp(SystemClock.uptimeMillis()); 455 } 456 457 public void userActivity() { 458 userActivity(AWAKE_INTERVAL_DEFAULT_MS); 459 } 460 461 public void userActivity(long holdMs) { 462 // We ignore the hold time. Eventually we should remove it. 463 // Instead, the keyguard window has an explicit user activity timeout set on it. 464 mPM.userActivity(SystemClock.uptimeMillis(), false); 465 } 466 467 /** 468 * Construct a KeyguardViewMediator 469 * @param context 470 * @param lockPatternUtils optional mock interface for LockPatternUtils 471 */ 472 public KeyguardViewMediator(Context context, LockPatternUtils lockPatternUtils) { 473 mContext = context; 474 mPM = (PowerManager) context.getSystemService(Context.POWER_SERVICE); 475 mUserManager = (UserManager) mContext.getSystemService(Context.USER_SERVICE); 476 mShowKeyguardWakeLock = mPM.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "show keyguard"); 477 mShowKeyguardWakeLock.setReferenceCounted(false); 478 479 mWakeAndHandOff = mPM.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "keyguardWakeAndHandOff"); 480 mWakeAndHandOff.setReferenceCounted(false); 481 482 mContext.registerReceiver(mBroadcastReceiver, new IntentFilter(DELAYED_KEYGUARD_ACTION)); 483 484 mAlarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); 485 486 mUpdateMonitor = KeyguardUpdateMonitor.getInstance(context); 487 488 mLockPatternUtils = lockPatternUtils != null 489 ? lockPatternUtils : new LockPatternUtils(mContext); 490 mLockPatternUtils.setCurrentUser(UserHandle.USER_OWNER); 491 492 WindowManager wm = (WindowManager)context.getSystemService(Context.WINDOW_SERVICE); 493 494 mKeyguardViewManager = new KeyguardViewManager(context, wm, mViewMediatorCallback, 495 mLockPatternUtils); 496 497 mUserPresentIntent = new Intent(Intent.ACTION_USER_PRESENT); 498 mUserPresentIntent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING 499 | Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT); 500 501 final ContentResolver cr = mContext.getContentResolver(); 502 mShowLockIcon = (Settings.System.getInt(cr, "show_status_bar_lock", 0) == 1); 503 504 mScreenOn = mPM.isScreenOn(); 505 506 mLockSounds = new SoundPool(1, AudioManager.STREAM_SYSTEM, 0); 507 String soundPath = Settings.Global.getString(cr, Settings.Global.LOCK_SOUND); 508 if (soundPath != null) { 509 mLockSoundId = mLockSounds.load(soundPath, 1); 510 } 511 if (soundPath == null || mLockSoundId == 0) { 512 Log.w(TAG, "failed to load lock sound from " + soundPath); 513 } 514 soundPath = Settings.Global.getString(cr, Settings.Global.UNLOCK_SOUND); 515 if (soundPath != null) { 516 mUnlockSoundId = mLockSounds.load(soundPath, 1); 517 } 518 if (soundPath == null || mUnlockSoundId == 0) { 519 Log.w(TAG, "failed to load unlock sound from " + soundPath); 520 } 521 int lockSoundDefaultAttenuation = context.getResources().getInteger( 522 com.android.internal.R.integer.config_lockSoundVolumeDb); 523 mLockSoundVolume = (float)Math.pow(10, (float)lockSoundDefaultAttenuation/20); 524 } 525 526 /** 527 * Let us know that the system is ready after startup. 528 */ 529 public void onSystemReady() { 530 synchronized (this) { 531 if (DEBUG) Log.d(TAG, "onSystemReady"); 532 mSystemReady = true; 533 mUpdateMonitor.registerCallback(mUpdateCallback); 534 535 // Suppress biometric unlock right after boot until things have settled if it is the 536 // selected security method, otherwise unsuppress it. It must be unsuppressed if it is 537 // not the selected security method for the following reason: if the user starts 538 // without a screen lock selected, the biometric unlock would be suppressed the first 539 // time they try to use it. 540 // 541 // Note that the biometric unlock will still not show if it is not the selected method. 542 // Calling setAlternateUnlockEnabled(true) simply says don't suppress it if it is the 543 // selected method. 544 if (mLockPatternUtils.usingBiometricWeak() 545 && mLockPatternUtils.isBiometricWeakInstalled()) { 546 if (DEBUG) Log.d(TAG, "suppressing biometric unlock during boot"); 547 mUpdateMonitor.setAlternateUnlockEnabled(false); 548 } else { 549 mUpdateMonitor.setAlternateUnlockEnabled(true); 550 } 551 552 doKeyguardLocked(); 553 } 554 // Most services aren't available until the system reaches the ready state, so we 555 // send it here when the device first boots. 556 maybeSendUserPresentBroadcast(); 557 } 558 559 /** 560 * Called to let us know the screen was turned off. 561 * @param why either {@link WindowManagerPolicy#OFF_BECAUSE_OF_USER}, 562 * {@link WindowManagerPolicy#OFF_BECAUSE_OF_TIMEOUT} or 563 * {@link WindowManagerPolicy#OFF_BECAUSE_OF_PROX_SENSOR}. 564 */ 565 public void onScreenTurnedOff(int why) { 566 synchronized (this) { 567 mScreenOn = false; 568 if (DEBUG) Log.d(TAG, "onScreenTurnedOff(" + why + ")"); 569 570 mKeyguardDonePending = false; 571 572 // Lock immediately based on setting if secure (user has a pin/pattern/password). 573 // This also "locks" the device when not secure to provide easy access to the 574 // camera while preventing unwanted input. 575 final boolean lockImmediately = 576 mLockPatternUtils.getPowerButtonInstantlyLocks() || !mLockPatternUtils.isSecure(); 577 578 if (mExitSecureCallback != null) { 579 if (DEBUG) Log.d(TAG, "pending exit secure callback cancelled"); 580 mExitSecureCallback.onKeyguardExitResult(false); 581 mExitSecureCallback = null; 582 if (!mExternallyEnabled) { 583 hideLocked(); 584 } 585 } else if (mShowing) { 586 notifyScreenOffLocked(); 587 resetStateLocked(null); 588 } else if (why == WindowManagerPolicy.OFF_BECAUSE_OF_TIMEOUT 589 || (why == WindowManagerPolicy.OFF_BECAUSE_OF_USER && !lockImmediately)) { 590 doKeyguardLaterLocked(); 591 } else if (why == WindowManagerPolicy.OFF_BECAUSE_OF_PROX_SENSOR) { 592 // Do not enable the keyguard if the prox sensor forced the screen off. 593 } else { 594 doKeyguardLocked(); 595 } 596 } 597 } 598 599 private void doKeyguardLaterLocked() { 600 // if the screen turned off because of timeout or the user hit the power button 601 // and we don't need to lock immediately, set an alarm 602 // to enable it a little bit later (i.e, give the user a chance 603 // to turn the screen back on within a certain window without 604 // having to unlock the screen) 605 final ContentResolver cr = mContext.getContentResolver(); 606 607 // From DisplaySettings 608 long displayTimeout = Settings.System.getInt(cr, SCREEN_OFF_TIMEOUT, 609 KEYGUARD_DISPLAY_TIMEOUT_DELAY_DEFAULT); 610 611 // From SecuritySettings 612 final long lockAfterTimeout = Settings.Secure.getInt(cr, 613 Settings.Secure.LOCK_SCREEN_LOCK_AFTER_TIMEOUT, 614 KEYGUARD_LOCK_AFTER_DELAY_DEFAULT); 615 616 // From DevicePolicyAdmin 617 final long policyTimeout = mLockPatternUtils.getDevicePolicyManager() 618 .getMaximumTimeToLock(null, mLockPatternUtils.getCurrentUser()); 619 620 long timeout; 621 if (policyTimeout > 0) { 622 // policy in effect. Make sure we don't go beyond policy limit. 623 displayTimeout = Math.max(displayTimeout, 0); // ignore negative values 624 timeout = Math.min(policyTimeout - displayTimeout, lockAfterTimeout); 625 } else { 626 timeout = lockAfterTimeout; 627 } 628 629 if (timeout <= 0) { 630 // Lock now 631 mSuppressNextLockSound = true; 632 doKeyguardLocked(); 633 } else { 634 // Lock in the future 635 long when = SystemClock.elapsedRealtime() + timeout; 636 Intent intent = new Intent(DELAYED_KEYGUARD_ACTION); 637 intent.putExtra("seq", mDelayedShowingSequence); 638 PendingIntent sender = PendingIntent.getBroadcast(mContext, 639 0, intent, PendingIntent.FLAG_CANCEL_CURRENT); 640 mAlarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, when, sender); 641 if (DEBUG) Log.d(TAG, "setting alarm to turn off keyguard, seq = " 642 + mDelayedShowingSequence); 643 } 644 } 645 646 private void cancelDoKeyguardLaterLocked() { 647 mDelayedShowingSequence++; 648 } 649 650 /** 651 * Let's us know the screen was turned on. 652 */ 653 public void onScreenTurnedOn(KeyguardViewManager.ShowListener showListener) { 654 synchronized (this) { 655 mScreenOn = true; 656 cancelDoKeyguardLaterLocked(); 657 if (DEBUG) Log.d(TAG, "onScreenTurnedOn, seq = " + mDelayedShowingSequence); 658 if (showListener != null) { 659 notifyScreenOnLocked(showListener); 660 } 661 } 662 maybeSendUserPresentBroadcast(); 663 } 664 665 private void maybeSendUserPresentBroadcast() { 666 if (mSystemReady && mLockPatternUtils.isLockScreenDisabled() 667 && mUserManager.getUsers(true).size() == 1) { 668 // Lock screen is disabled because the user has set the preference to "None". 669 // In this case, send out ACTION_USER_PRESENT here instead of in 670 // handleKeyguardDone() 671 sendUserPresentBroadcast(); 672 } 673 } 674 675 /** 676 * A dream started. We should lock after the usual screen-off lock timeout but only 677 * if there is a secure lock pattern. 678 */ 679 public void onDreamingStarted() { 680 synchronized (this) { 681 if (mScreenOn && mLockPatternUtils.isSecure()) { 682 doKeyguardLaterLocked(); 683 } 684 } 685 } 686 687 /** 688 * A dream stopped. 689 */ 690 public void onDreamingStopped() { 691 synchronized (this) { 692 if (mScreenOn) { 693 cancelDoKeyguardLaterLocked(); 694 } 695 } 696 } 697 698 /** 699 * Same semantics as {@link WindowManagerPolicy#enableKeyguard}; provide 700 * a way for external stuff to override normal keyguard behavior. For instance 701 * the phone app disables the keyguard when it receives incoming calls. 702 */ 703 public void setKeyguardEnabled(boolean enabled) { 704 synchronized (this) { 705 if (DEBUG) Log.d(TAG, "setKeyguardEnabled(" + enabled + ")"); 706 707 mExternallyEnabled = enabled; 708 709 if (!enabled && mShowing) { 710 if (mExitSecureCallback != null) { 711 if (DEBUG) Log.d(TAG, "in process of verifyUnlock request, ignoring"); 712 // we're in the process of handling a request to verify the user 713 // can get past the keyguard. ignore extraneous requests to disable / reenable 714 return; 715 } 716 717 // hiding keyguard that is showing, remember to reshow later 718 if (DEBUG) Log.d(TAG, "remembering to reshow, hiding keyguard, " 719 + "disabling status bar expansion"); 720 mNeedToReshowWhenReenabled = true; 721 hideLocked(); 722 } else if (enabled && mNeedToReshowWhenReenabled) { 723 // reenabled after previously hidden, reshow 724 if (DEBUG) Log.d(TAG, "previously hidden, reshowing, reenabling " 725 + "status bar expansion"); 726 mNeedToReshowWhenReenabled = false; 727 728 if (mExitSecureCallback != null) { 729 if (DEBUG) Log.d(TAG, "onKeyguardExitResult(false), resetting"); 730 mExitSecureCallback.onKeyguardExitResult(false); 731 mExitSecureCallback = null; 732 resetStateLocked(null); 733 } else { 734 showLocked(null); 735 736 // block until we know the keygaurd is done drawing (and post a message 737 // to unblock us after a timeout so we don't risk blocking too long 738 // and causing an ANR). 739 mWaitingUntilKeyguardVisible = true; 740 mHandler.sendEmptyMessageDelayed(KEYGUARD_DONE_DRAWING, KEYGUARD_DONE_DRAWING_TIMEOUT_MS); 741 if (DEBUG) Log.d(TAG, "waiting until mWaitingUntilKeyguardVisible is false"); 742 while (mWaitingUntilKeyguardVisible) { 743 try { 744 wait(); 745 } catch (InterruptedException e) { 746 Thread.currentThread().interrupt(); 747 } 748 } 749 if (DEBUG) Log.d(TAG, "done waiting for mWaitingUntilKeyguardVisible"); 750 } 751 } 752 } 753 } 754 755 /** 756 * @see android.app.KeyguardManager#exitKeyguardSecurely 757 */ 758 public void verifyUnlock(WindowManagerPolicy.OnKeyguardExitResult callback) { 759 synchronized (this) { 760 if (DEBUG) Log.d(TAG, "verifyUnlock"); 761 if (!mUpdateMonitor.isDeviceProvisioned()) { 762 // don't allow this api when the device isn't provisioned 763 if (DEBUG) Log.d(TAG, "ignoring because device isn't provisioned"); 764 callback.onKeyguardExitResult(false); 765 } else if (mExternallyEnabled) { 766 // this only applies when the user has externally disabled the 767 // keyguard. this is unexpected and means the user is not 768 // using the api properly. 769 Log.w(TAG, "verifyUnlock called when not externally disabled"); 770 callback.onKeyguardExitResult(false); 771 } else if (mExitSecureCallback != null) { 772 // already in progress with someone else 773 callback.onKeyguardExitResult(false); 774 } else { 775 mExitSecureCallback = callback; 776 verifyUnlockLocked(); 777 } 778 } 779 } 780 781 /** 782 * Is the keyguard currently showing? 783 */ 784 public boolean isShowing() { 785 return mShowing; 786 } 787 788 /** 789 * Is the keyguard currently showing and not being force hidden? 790 */ 791 public boolean isShowingAndNotHidden() { 792 return mShowing && !mHidden; 793 } 794 795 /** 796 * Notify us when the keyguard is hidden by another window 797 */ 798 public void setHidden(boolean isHidden) { 799 if (DEBUG) Log.d(TAG, "setHidden " + isHidden); 800 mUpdateMonitor.sendKeyguardVisibilityChanged(!isHidden); 801 mHandler.removeMessages(SET_HIDDEN); 802 Message msg = mHandler.obtainMessage(SET_HIDDEN, (isHidden ? 1 : 0), 0); 803 mHandler.sendMessage(msg); 804 } 805 806 /** 807 * Handles SET_HIDDEN message sent by setHidden() 808 */ 809 private void handleSetHidden(boolean isHidden) { 810 synchronized (KeyguardViewMediator.this) { 811 if (mHidden != isHidden) { 812 mHidden = isHidden; 813 updateActivityLockScreenState(); 814 adjustStatusBarLocked(); 815 } 816 } 817 } 818 819 /** 820 * Used by PhoneWindowManager to enable the keyguard due to a user activity timeout. 821 * This must be safe to call from any thread and with any window manager locks held. 822 */ 823 public void doKeyguardTimeout(Bundle options) { 824 mHandler.removeMessages(KEYGUARD_TIMEOUT); 825 Message msg = mHandler.obtainMessage(KEYGUARD_TIMEOUT, options); 826 mHandler.sendMessage(msg); 827 } 828 829 /** 830 * Given the state of the keyguard, is the input restricted? 831 * Input is restricted when the keyguard is showing, or when the keyguard 832 * was suppressed by an app that disabled the keyguard or we haven't been provisioned yet. 833 */ 834 public boolean isInputRestricted() { 835 return mShowing || mNeedToReshowWhenReenabled || !mUpdateMonitor.isDeviceProvisioned(); 836 } 837 838 private void doKeyguardLocked() { 839 doKeyguardLocked(null); 840 } 841 842 /** 843 * Enable the keyguard if the settings are appropriate. 844 */ 845 private void doKeyguardLocked(Bundle options) { 846 // if another app is disabling us, don't show 847 if (!mExternallyEnabled) { 848 if (DEBUG) Log.d(TAG, "doKeyguard: not showing because externally disabled"); 849 850 // note: we *should* set mNeedToReshowWhenReenabled=true here, but that makes 851 // for an occasional ugly flicker in this situation: 852 // 1) receive a call with the screen on (no keyguard) or make a call 853 // 2) screen times out 854 // 3) user hits key to turn screen back on 855 // instead, we reenable the keyguard when we know the screen is off and the call 856 // ends (see the broadcast receiver below) 857 // TODO: clean this up when we have better support at the window manager level 858 // for apps that wish to be on top of the keyguard 859 return; 860 } 861 862 // if the keyguard is already showing, don't bother 863 if (mKeyguardViewManager.isShowing()) { 864 if (DEBUG) Log.d(TAG, "doKeyguard: not showing because it is already showing"); 865 return; 866 } 867 868 // if the setup wizard hasn't run yet, don't show 869 final boolean requireSim = !SystemProperties.getBoolean("keyguard.no_require_sim", 870 false); 871 final boolean provisioned = mUpdateMonitor.isDeviceProvisioned(); 872 final IccCardConstants.State state = mUpdateMonitor.getSimState(); 873 final boolean lockedOrMissing = state.isPinLocked() 874 || ((state == IccCardConstants.State.ABSENT 875 || state == IccCardConstants.State.PERM_DISABLED) 876 && requireSim); 877 878 if (!lockedOrMissing && !provisioned) { 879 if (DEBUG) Log.d(TAG, "doKeyguard: not showing because device isn't provisioned" 880 + " and the sim is not locked or missing"); 881 return; 882 } 883 884 if (mUserManager.getUsers(true).size() < 2 885 && mLockPatternUtils.isLockScreenDisabled() && !lockedOrMissing) { 886 if (DEBUG) Log.d(TAG, "doKeyguard: not showing because lockscreen is off"); 887 return; 888 } 889 890 if (DEBUG) Log.d(TAG, "doKeyguard: showing the lock screen"); 891 showLocked(options); 892 } 893 894 /** 895 * Dismiss the keyguard through the security layers. 896 */ 897 public void dismiss() { 898 if (mShowing && !mHidden) { 899 mKeyguardViewManager.dismiss(); 900 } 901 } 902 903 /** 904 * Send message to keyguard telling it to reset its state. 905 * @param options options about how to show the keyguard 906 * @see #handleReset() 907 */ 908 private void resetStateLocked(Bundle options) { 909 if (DEBUG) Log.d(TAG, "resetStateLocked"); 910 Message msg = mHandler.obtainMessage(RESET, options); 911 mHandler.sendMessage(msg); 912 } 913 914 /** 915 * Send message to keyguard telling it to verify unlock 916 * @see #handleVerifyUnlock() 917 */ 918 private void verifyUnlockLocked() { 919 if (DEBUG) Log.d(TAG, "verifyUnlockLocked"); 920 mHandler.sendEmptyMessage(VERIFY_UNLOCK); 921 } 922 923 924 /** 925 * Send a message to keyguard telling it the screen just turned on. 926 * @see #onScreenTurnedOff(int) 927 * @see #handleNotifyScreenOff 928 */ 929 private void notifyScreenOffLocked() { 930 if (DEBUG) Log.d(TAG, "notifyScreenOffLocked"); 931 mHandler.sendEmptyMessage(NOTIFY_SCREEN_OFF); 932 } 933 934 /** 935 * Send a message to keyguard telling it the screen just turned on. 936 * @see #onScreenTurnedOn() 937 * @see #handleNotifyScreenOn 938 */ 939 private void notifyScreenOnLocked(KeyguardViewManager.ShowListener showListener) { 940 if (DEBUG) Log.d(TAG, "notifyScreenOnLocked"); 941 Message msg = mHandler.obtainMessage(NOTIFY_SCREEN_ON, showListener); 942 mHandler.sendMessage(msg); 943 } 944 945 /** 946 * Send message to keyguard telling it about a wake key so it can adjust 947 * its state accordingly and then poke the wake lock when it is ready. 948 * @param keyCode The wake key. 949 * @see #handleWakeWhenReady 950 * @see #onWakeKeyWhenKeyguardShowingTq(int) 951 */ 952 private void wakeWhenReady(int keyCode) { 953 if (DBG_WAKE) Log.d(TAG, "wakeWhenReady(" + keyCode + ")"); 954 955 /** 956 * acquire the handoff lock that will keep the cpu running. this will 957 * be released once the keyguard has set itself up and poked the other wakelock 958 * in {@link #handleWakeWhenReady(int)} 959 */ 960 mWakeAndHandOff.acquire(); 961 962 Message msg = mHandler.obtainMessage(WAKE_WHEN_READY, keyCode, 0); 963 mHandler.sendMessage(msg); 964 } 965 966 /** 967 * Send message to keyguard telling it to show itself 968 * @see #handleShow() 969 */ 970 private void showLocked(Bundle options) { 971 if (DEBUG) Log.d(TAG, "showLocked"); 972 // ensure we stay awake until we are finished displaying the keyguard 973 mShowKeyguardWakeLock.acquire(); 974 Message msg = mHandler.obtainMessage(SHOW, options); 975 mHandler.sendMessage(msg); 976 } 977 978 /** 979 * Send message to keyguard telling it to hide itself 980 * @see #handleHide() 981 */ 982 private void hideLocked() { 983 if (DEBUG) Log.d(TAG, "hideLocked"); 984 Message msg = mHandler.obtainMessage(HIDE); 985 mHandler.sendMessage(msg); 986 } 987 988 public boolean isSecure() { 989 return mLockPatternUtils.isSecure() 990 || KeyguardUpdateMonitor.getInstance(mContext).isSimPinSecure(); 991 } 992 993 /** 994 * Update the newUserId. Call while holding WindowManagerService lock. 995 * NOTE: Should only be called by KeyguardViewMediator in response to the user id changing. 996 * 997 * @param newUserId The id of the incoming user. 998 */ 999 public void setCurrentUser(int newUserId) { 1000 mLockPatternUtils.setCurrentUser(newUserId); 1001 } 1002 1003 private final BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() { 1004 @Override 1005 public void onReceive(Context context, Intent intent) { 1006 if (DELAYED_KEYGUARD_ACTION.equals(intent.getAction())) { 1007 final int sequence = intent.getIntExtra("seq", 0); 1008 if (DEBUG) Log.d(TAG, "received DELAYED_KEYGUARD_ACTION with seq = " 1009 + sequence + ", mDelayedShowingSequence = " + mDelayedShowingSequence); 1010 synchronized (KeyguardViewMediator.this) { 1011 if (mDelayedShowingSequence == sequence) { 1012 // Don't play lockscreen SFX if the screen went off due to timeout. 1013 mSuppressNextLockSound = true; 1014 doKeyguardLocked(); 1015 } 1016 } 1017 } 1018 } 1019 }; 1020 1021 /** 1022 * When a key is received when the screen is off and the keyguard is showing, 1023 * we need to decide whether to actually turn on the screen, and if so, tell 1024 * the keyguard to prepare itself and poke the wake lock when it is ready. 1025 * 1026 * The 'Tq' suffix is per the documentation in {@link WindowManagerPolicy}. 1027 * Be sure not to take any action that takes a long time; any significant 1028 * action should be posted to a handler. 1029 * 1030 * @param keyCode The keycode of the key that woke the device 1031 */ 1032 public void onWakeKeyWhenKeyguardShowingTq(int keyCode) { 1033 if (DEBUG) Log.d(TAG, "onWakeKeyWhenKeyguardShowing(" + keyCode + ")"); 1034 1035 // give the keyguard view manager a chance to adjust the state of the 1036 // keyguard based on the key that woke the device before poking 1037 // the wake lock 1038 wakeWhenReady(keyCode); 1039 } 1040 1041 /** 1042 * When a wake motion such as an external mouse movement is received when the screen 1043 * is off and the keyguard is showing, we need to decide whether to actually turn 1044 * on the screen, and if so, tell the keyguard to prepare itself and poke the wake 1045 * lock when it is ready. 1046 * 1047 * The 'Tq' suffix is per the documentation in {@link WindowManagerPolicy}. 1048 * Be sure not to take any action that takes a long time; any significant 1049 * action should be posted to a handler. 1050 */ 1051 public void onWakeMotionWhenKeyguardShowingTq() { 1052 if (DEBUG) Log.d(TAG, "onWakeMotionWhenKeyguardShowing()"); 1053 1054 // give the keyguard view manager a chance to adjust the state of the 1055 // keyguard based on the key that woke the device before poking 1056 // the wake lock 1057 wakeWhenReady(KeyEvent.KEYCODE_UNKNOWN); 1058 } 1059 1060 public void keyguardDone(boolean authenticated, boolean wakeup) { 1061 mKeyguardDonePending = false; 1062 synchronized (this) { 1063 EventLog.writeEvent(70000, 2); 1064 if (DEBUG) Log.d(TAG, "keyguardDone(" + authenticated + ")"); 1065 Message msg = mHandler.obtainMessage(KEYGUARD_DONE); 1066 msg.arg1 = wakeup ? 1 : 0; 1067 mHandler.sendMessage(msg); 1068 1069 if (authenticated) { 1070 mUpdateMonitor.clearFailedUnlockAttempts(); 1071 } 1072 1073 if (mExitSecureCallback != null) { 1074 mExitSecureCallback.onKeyguardExitResult(authenticated); 1075 mExitSecureCallback = null; 1076 1077 if (authenticated) { 1078 // after succesfully exiting securely, no need to reshow 1079 // the keyguard when they've released the lock 1080 mExternallyEnabled = true; 1081 mNeedToReshowWhenReenabled = false; 1082 } 1083 } 1084 } 1085 } 1086 1087 /** 1088 * This handler will be associated with the policy thread, which will also 1089 * be the UI thread of the keyguard. Since the apis of the policy, and therefore 1090 * this class, can be called by other threads, any action that directly 1091 * interacts with the keyguard ui should be posted to this handler, rather 1092 * than called directly. 1093 */ 1094 private Handler mHandler = new Handler(Looper.myLooper(), null, true /*async*/) { 1095 @Override 1096 public void handleMessage(Message msg) { 1097 switch (msg.what) { 1098 case SHOW: 1099 handleShow((Bundle) msg.obj); 1100 return ; 1101 case HIDE: 1102 handleHide(); 1103 return ; 1104 case RESET: 1105 handleReset((Bundle) msg.obj); 1106 return ; 1107 case VERIFY_UNLOCK: 1108 handleVerifyUnlock(); 1109 return; 1110 case NOTIFY_SCREEN_OFF: 1111 handleNotifyScreenOff(); 1112 return; 1113 case NOTIFY_SCREEN_ON: 1114 handleNotifyScreenOn((KeyguardViewManager.ShowListener)msg.obj); 1115 return; 1116 case WAKE_WHEN_READY: 1117 handleWakeWhenReady(msg.arg1); 1118 return; 1119 case KEYGUARD_DONE: 1120 handleKeyguardDone(msg.arg1 != 0); 1121 return; 1122 case KEYGUARD_DONE_DRAWING: 1123 handleKeyguardDoneDrawing(); 1124 return; 1125 case KEYGUARD_DONE_AUTHENTICATING: 1126 keyguardDone(true, true); 1127 return; 1128 case SET_HIDDEN: 1129 handleSetHidden(msg.arg1 != 0); 1130 break; 1131 case KEYGUARD_TIMEOUT: 1132 synchronized (KeyguardViewMediator.this) { 1133 doKeyguardLocked((Bundle) msg.obj); 1134 } 1135 break; 1136 case SHOW_ASSISTANT: 1137 handleShowAssistant(); 1138 break; 1139 } 1140 } 1141 }; 1142 1143 /** 1144 * @see #keyguardDone 1145 * @see #KEYGUARD_DONE 1146 */ 1147 private void handleKeyguardDone(boolean wakeup) { 1148 if (DEBUG) Log.d(TAG, "handleKeyguardDone"); 1149 handleHide(); 1150 if (wakeup) { 1151 wakeUp(); 1152 } 1153 1154 sendUserPresentBroadcast(); 1155 } 1156 1157 private void sendUserPresentBroadcast() { 1158 if (!(mContext instanceof Activity)) { 1159 final UserHandle currentUser = new UserHandle(mLockPatternUtils.getCurrentUser()); 1160 mContext.sendBroadcastAsUser(mUserPresentIntent, currentUser); 1161 } 1162 } 1163 1164 /** 1165 * @see #keyguardDoneDrawing 1166 * @see #KEYGUARD_DONE_DRAWING 1167 */ 1168 private void handleKeyguardDoneDrawing() { 1169 synchronized(this) { 1170 if (false) Log.d(TAG, "handleKeyguardDoneDrawing"); 1171 if (mWaitingUntilKeyguardVisible) { 1172 if (DEBUG) Log.d(TAG, "handleKeyguardDoneDrawing: notifying mWaitingUntilKeyguardVisible"); 1173 mWaitingUntilKeyguardVisible = false; 1174 notifyAll(); 1175 1176 // there will usually be two of these sent, one as a timeout, and one 1177 // as a result of the callback, so remove any remaining messages from 1178 // the queue 1179 mHandler.removeMessages(KEYGUARD_DONE_DRAWING); 1180 } 1181 } 1182 } 1183 1184 private void playSounds(boolean locked) { 1185 // User feedback for keyguard. 1186 1187 if (mSuppressNextLockSound) { 1188 mSuppressNextLockSound = false; 1189 return; 1190 } 1191 1192 final ContentResolver cr = mContext.getContentResolver(); 1193 if (Settings.System.getInt(cr, Settings.System.LOCKSCREEN_SOUNDS_ENABLED, 1) == 1) { 1194 final int whichSound = locked 1195 ? mLockSoundId 1196 : mUnlockSoundId; 1197 mLockSounds.stop(mLockSoundStreamId); 1198 // Init mAudioManager 1199 if (mAudioManager == null) { 1200 mAudioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE); 1201 if (mAudioManager == null) return; 1202 mMasterStreamType = mAudioManager.getMasterStreamType(); 1203 } 1204 // If the stream is muted, don't play the sound 1205 if (mAudioManager.isStreamMute(mMasterStreamType)) return; 1206 1207 mLockSoundStreamId = mLockSounds.play(whichSound, 1208 mLockSoundVolume, mLockSoundVolume, 1/*priortiy*/, 0/*loop*/, 1.0f/*rate*/); 1209 } 1210 } 1211 1212 private void updateActivityLockScreenState() { 1213 try { 1214 ActivityManagerNative.getDefault().setLockScreenShown( 1215 mShowing && !mHidden); 1216 } catch (RemoteException e) { 1217 } 1218 } 1219 1220 /** 1221 * Handle message sent by {@link #showLocked}. 1222 * @see #SHOW 1223 */ 1224 private void handleShow(Bundle options) { 1225 synchronized (KeyguardViewMediator.this) { 1226 if (DEBUG) Log.d(TAG, "handleShow"); 1227 if (!mSystemReady) return; 1228 1229 mKeyguardViewManager.show(options); 1230 mShowing = true; 1231 mKeyguardDonePending = false; 1232 updateActivityLockScreenState(); 1233 adjustStatusBarLocked(); 1234 userActivity(); 1235 try { 1236 ActivityManagerNative.getDefault().closeSystemDialogs("lock"); 1237 } catch (RemoteException e) { 1238 } 1239 1240 // Do this at the end to not slow down display of the keyguard. 1241 playSounds(true); 1242 1243 mShowKeyguardWakeLock.release(); 1244 } 1245 } 1246 1247 /** 1248 * Handle message sent by {@link #hideLocked()} 1249 * @see #HIDE 1250 */ 1251 private void handleHide() { 1252 synchronized (KeyguardViewMediator.this) { 1253 if (DEBUG) Log.d(TAG, "handleHide"); 1254 if (mWakeAndHandOff.isHeld()) { 1255 Log.w(TAG, "attempt to hide the keyguard while waking, ignored"); 1256 return; 1257 } 1258 1259 // only play "unlock" noises if not on a call (since the incall UI 1260 // disables the keyguard) 1261 if (TelephonyManager.EXTRA_STATE_IDLE.equals(mPhoneState)) { 1262 playSounds(false); 1263 } 1264 1265 mKeyguardViewManager.hide(); 1266 mShowing = false; 1267 mKeyguardDonePending = false; 1268 updateActivityLockScreenState(); 1269 adjustStatusBarLocked(); 1270 } 1271 } 1272 1273 private void adjustStatusBarLocked() { 1274 if (mStatusBarManager == null) { 1275 mStatusBarManager = (StatusBarManager) 1276 mContext.getSystemService(Context.STATUS_BAR_SERVICE); 1277 } 1278 if (mStatusBarManager == null) { 1279 Log.w(TAG, "Could not get status bar manager"); 1280 } else { 1281 if (mShowLockIcon) { 1282 // Give feedback to user when secure keyguard is active and engaged 1283 if (mShowing && isSecure()) { 1284 if (!mShowingLockIcon) { 1285 String contentDescription = mContext.getString( 1286 com.android.internal.R.string.status_bar_device_locked); 1287 mStatusBarManager.setIcon("secure", 1288 com.android.internal.R.drawable.stat_sys_secure, 0, 1289 contentDescription); 1290 mShowingLockIcon = true; 1291 } 1292 } else { 1293 if (mShowingLockIcon) { 1294 mStatusBarManager.removeIcon("secure"); 1295 mShowingLockIcon = false; 1296 } 1297 } 1298 } 1299 1300 // Disable aspects of the system/status/navigation bars that must not be re-enabled by 1301 // windows that appear on top, ever 1302 int flags = StatusBarManager.DISABLE_NONE; 1303 if (mShowing) { 1304 // Permanently disable components not available when keyguard is enabled 1305 // (like recents). Temporary enable/disable (e.g. the "back" button) are 1306 // done in KeyguardHostView. 1307 flags |= StatusBarManager.DISABLE_RECENT; 1308 if (isSecure() || !ENABLE_INSECURE_STATUS_BAR_EXPAND) { 1309 // showing secure lockscreen; disable expanding. 1310 flags |= StatusBarManager.DISABLE_EXPAND; 1311 } 1312 if (isSecure()) { 1313 // showing secure lockscreen; disable ticker. 1314 flags |= StatusBarManager.DISABLE_NOTIFICATION_TICKER; 1315 } 1316 } 1317 1318 if (DEBUG) { 1319 Log.d(TAG, "adjustStatusBarLocked: mShowing=" + mShowing + " mHidden=" + mHidden 1320 + " isSecure=" + isSecure() + " --> flags=0x" + Integer.toHexString(flags)); 1321 } 1322 1323 if (!(mContext instanceof Activity)) { 1324 mStatusBarManager.disable(flags); 1325 } 1326 } 1327 } 1328 1329 /** 1330 * Handle message sent by {@link #wakeWhenReady(int)} 1331 * @param keyCode The key that woke the device. 1332 * @see #WAKE_WHEN_READY 1333 */ 1334 private void handleWakeWhenReady(int keyCode) { 1335 synchronized (KeyguardViewMediator.this) { 1336 if (DBG_WAKE) Log.d(TAG, "handleWakeWhenReady(" + keyCode + ")"); 1337 1338 // this should result in a call to 'poke wakelock' which will set a timeout 1339 // on releasing the wakelock 1340 if (!mKeyguardViewManager.wakeWhenReadyTq(keyCode)) { 1341 // poke wakelock ourselves if keyguard is no longer active 1342 Log.w(TAG, "mKeyguardViewManager.wakeWhenReadyTq did not poke wake lock, so poke it ourselves"); 1343 userActivity(); 1344 } 1345 1346 /** 1347 * Now that the keyguard is ready and has poked the wake lock, we can 1348 * release the handoff wakelock 1349 */ 1350 mWakeAndHandOff.release(); 1351 } 1352 } 1353 1354 /** 1355 * Handle message sent by {@link #resetStateLocked(Bundle)} 1356 * @see #RESET 1357 */ 1358 private void handleReset(Bundle options) { 1359 synchronized (KeyguardViewMediator.this) { 1360 if (DEBUG) Log.d(TAG, "handleReset"); 1361 mKeyguardViewManager.reset(options); 1362 } 1363 } 1364 1365 /** 1366 * Handle message sent by {@link #verifyUnlock} 1367 * @see #VERIFY_UNLOCK 1368 */ 1369 private void handleVerifyUnlock() { 1370 synchronized (KeyguardViewMediator.this) { 1371 if (DEBUG) Log.d(TAG, "handleVerifyUnlock"); 1372 mKeyguardViewManager.verifyUnlock(); 1373 mShowing = true; 1374 updateActivityLockScreenState(); 1375 } 1376 } 1377 1378 /** 1379 * Handle message sent by {@link #notifyScreenOffLocked()} 1380 * @see #NOTIFY_SCREEN_OFF 1381 */ 1382 private void handleNotifyScreenOff() { 1383 synchronized (KeyguardViewMediator.this) { 1384 if (DEBUG) Log.d(TAG, "handleNotifyScreenOff"); 1385 mKeyguardViewManager.onScreenTurnedOff(); 1386 } 1387 } 1388 1389 /** 1390 * Handle message sent by {@link #notifyScreenOnLocked()} 1391 * @see #NOTIFY_SCREEN_ON 1392 */ 1393 private void handleNotifyScreenOn(KeyguardViewManager.ShowListener showListener) { 1394 synchronized (KeyguardViewMediator.this) { 1395 if (DEBUG) Log.d(TAG, "handleNotifyScreenOn"); 1396 mKeyguardViewManager.onScreenTurnedOn(showListener); 1397 } 1398 } 1399 1400 public boolean isDismissable() { 1401 return mKeyguardDonePending || !isSecure(); 1402 } 1403 1404 public void showAssistant() { 1405 Message msg = mHandler.obtainMessage(SHOW_ASSISTANT); 1406 mHandler.sendMessage(msg); 1407 } 1408 1409 public void handleShowAssistant() { 1410 mKeyguardViewManager.showAssistant(); 1411 } 1412 1413} 1414