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; 18 19import com.android.internal.telephony.IccCard; 20import com.android.internal.widget.LockPatternUtils; 21 22import android.app.AlarmManager; 23import android.app.PendingIntent; 24import android.app.StatusBarManager; 25import static android.app.StatusBarManager.DISABLE_EXPAND; 26import static android.app.StatusBarManager.DISABLE_NONE; 27import android.content.BroadcastReceiver; 28import android.content.Context; 29import android.content.Intent; 30import android.content.IntentFilter; 31import android.os.Handler; 32import android.os.LocalPowerManager; 33import android.os.Message; 34import android.os.PowerManager; 35import android.os.SystemClock; 36import android.telephony.TelephonyManager; 37import android.util.Config; 38import android.util.EventLog; 39import android.util.Log; 40import android.view.KeyEvent; 41import android.view.WindowManagerImpl; 42import android.view.WindowManagerPolicy; 43 44 45/** 46 * Mediates requests related to the keyguard. This includes queries about the 47 * state of the keyguard, power management events that effect whether the keyguard 48 * should be shown or reset, callbacks to the phone window manager to notify 49 * it of when the keyguard is showing, and events from the keyguard view itself 50 * stating that the keyguard was succesfully unlocked. 51 * 52 * Note that the keyguard view is shown when the screen is off (as appropriate) 53 * so that once the screen comes on, it will be ready immediately. 54 * 55 * Example queries about the keyguard: 56 * - is {movement, key} one that should wake the keygaurd? 57 * - is the keyguard showing? 58 * - are input events restricted due to the state of the keyguard? 59 * 60 * Callbacks to the phone window manager: 61 * - the keyguard is showing 62 * 63 * Example external events that translate to keyguard view changes: 64 * - screen turned off -> reset the keyguard, and show it so it will be ready 65 * next time the screen turns on 66 * - keyboard is slid open -> if the keyguard is not secure, hide it 67 * 68 * Events from the keyguard view: 69 * - user succesfully unlocked keyguard -> hide keyguard view, and no longer 70 * restrict input events. 71 * 72 * Note: in addition to normal power managment events that effect the state of 73 * whether the keyguard should be showing, external apps and services may request 74 * that the keyguard be disabled via {@link #setKeyguardEnabled(boolean)}. When 75 * false, this will override all other conditions for turning on the keyguard. 76 * 77 * Threading and synchronization: 78 * This class is created by the initialization routine of the {@link WindowManagerPolicy}, 79 * and runs on its thread. The keyguard UI is created from that thread in the 80 * constructor of this class. The apis may be called from other threads, including the 81 * {@link com.android.server.KeyInputQueue}'s and {@link android.view.WindowManager}'s. 82 * Therefore, methods on this class are synchronized, and any action that is pointed 83 * directly to the keyguard UI is posted to a {@link Handler} to ensure it is taken on the UI 84 * thread of the keyguard. 85 */ 86public class KeyguardViewMediator implements KeyguardViewCallback, 87 KeyguardUpdateMonitor.ConfigurationChangeCallback, KeyguardUpdateMonitor.SimStateCallback { 88 private final static boolean DEBUG = false && Config.LOGD; 89 private final static boolean DBG_WAKE = DEBUG || true; 90 91 private final static String TAG = "KeyguardViewMediator"; 92 93 private static final String DELAYED_KEYGUARD_ACTION = 94 "com.android.internal.policy.impl.PhoneWindowManager.DELAYED_KEYGUARD"; 95 96 // used for handler messages 97 private static final int TIMEOUT = 1; 98 private static final int SHOW = 2; 99 private static final int HIDE = 3; 100 private static final int RESET = 4; 101 private static final int VERIFY_UNLOCK = 5; 102 private static final int NOTIFY_SCREEN_OFF = 6; 103 private static final int NOTIFY_SCREEN_ON = 7; 104 private static final int WAKE_WHEN_READY = 8; 105 private static final int KEYGUARD_DONE = 9; 106 private static final int KEYGUARD_DONE_DRAWING = 10; 107 108 /** 109 * The default amount of time we stay awake (used for all key input) 110 */ 111 protected static final int AWAKE_INTERVAL_DEFAULT_MS = 5000; 112 113 114 /** 115 * The default amount of time we stay awake (used for all key input) when 116 * the keyboard is open 117 */ 118 protected static final int AWAKE_INTERVAL_DEFAULT_KEYBOARD_OPEN_MS = 10000; 119 120 /** 121 * How long to wait after the screen turns off due to timeout before 122 * turning on the keyguard (i.e, the user has this much time to turn 123 * the screen back on without having to face the keyguard). 124 */ 125 private static final int KEYGUARD_DELAY_MS = 0; 126 127 /** 128 * How long we'll wait for the {@link KeyguardViewCallback#keyguardDoneDrawing()} 129 * callback before unblocking a call to {@link #setKeyguardEnabled(boolean)} 130 * that is reenabling the keyguard. 131 */ 132 private static final int KEYGUARD_DONE_DRAWING_TIMEOUT_MS = 2000; 133 134 private Context mContext; 135 private AlarmManager mAlarmManager; 136 137 private boolean mSystemReady; 138 139 /** Low level access to the power manager for enableUserActivity. Having this 140 * requires that we run in the system process. */ 141 LocalPowerManager mRealPowerManager; 142 143 /** High level access to the power manager for WakeLocks */ 144 private PowerManager mPM; 145 146 /** 147 * Used to keep the device awake while the keyguard is showing, i.e for 148 * calls to {@link #pokeWakelock()} 149 */ 150 private PowerManager.WakeLock mWakeLock; 151 152 /** 153 * Does not turn on screen, held while a call to {@link KeyguardViewManager#wakeWhenReadyTq(int)} 154 * is called to make sure the device doesn't sleep before it has a chance to poke 155 * the wake lock. 156 * @see #wakeWhenReadyLocked(int) 157 */ 158 private PowerManager.WakeLock mWakeAndHandOff; 159 160 /** 161 * Used to disable / reenable status bar expansion. 162 */ 163 private StatusBarManager mStatusBarManager; 164 165 private KeyguardViewManager mKeyguardViewManager; 166 167 // these are protected by synchronized (this) 168 169 /** 170 * External apps (like the phone app) can tell us to disable the keygaurd. 171 */ 172 private boolean mExternallyEnabled = true; 173 174 /** 175 * Remember if an external call to {@link #setKeyguardEnabled} with value 176 * false caused us to hide the keyguard, so that we need to reshow it once 177 * the keygaurd is reenabled with another call with value true. 178 */ 179 private boolean mNeedToReshowWhenReenabled = false; 180 181 // cached value of whether we are showing (need to know this to quickly 182 // answer whether the input should be restricted) 183 private boolean mShowing = false; 184 185 /** 186 * Helps remember whether the screen has turned on since the last time 187 * it turned off due to timeout. see {@link #onScreenTurnedOff(int)} 188 */ 189 private int mDelayedShowingSequence; 190 191 private int mWakelockSequence; 192 193 private PhoneWindowManager mCallback; 194 195 /** 196 * If the user has disabled the keyguard, then requests to exit, this is 197 * how we'll ultimately let them know whether it was successful. We use this 198 * var being non-null as an indicator that there is an in progress request. 199 */ 200 private WindowManagerPolicy.OnKeyguardExitResult mExitSecureCallback; 201 202 // the properties of the keyguard 203 private KeyguardViewProperties mKeyguardViewProperties; 204 205 private KeyguardUpdateMonitor mUpdateMonitor; 206 207 private boolean mKeyboardOpen = false; 208 209 private boolean mScreenOn = false; 210 211 /** 212 * we send this intent when the keyguard is dismissed. 213 */ 214 private Intent mUserPresentIntent; 215 216 /** 217 * {@link #setKeyguardEnabled} waits on this condition when it reenables 218 * the keyguard. 219 */ 220 private boolean mWaitingUntilKeyguardVisible = false; 221 222 public KeyguardViewMediator(Context context, PhoneWindowManager callback, 223 LocalPowerManager powerManager) { 224 mContext = context; 225 226 mRealPowerManager = powerManager; 227 mPM = (PowerManager) context.getSystemService(Context.POWER_SERVICE); 228 mWakeLock = mPM.newWakeLock( 229 PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, 230 "keyguard"); 231 mWakeLock.setReferenceCounted(false); 232 233 mWakeAndHandOff = mPM.newWakeLock( 234 PowerManager.PARTIAL_WAKE_LOCK, 235 "keyguardWakeAndHandOff"); 236 mWakeAndHandOff.setReferenceCounted(false); 237 238 IntentFilter filter = new IntentFilter(); 239 filter.addAction(DELAYED_KEYGUARD_ACTION); 240 filter.addAction(TelephonyManager.ACTION_PHONE_STATE_CHANGED); 241 context.registerReceiver(mBroadCastReceiver, filter); 242 mAlarmManager = (AlarmManager) context 243 .getSystemService(Context.ALARM_SERVICE); 244 mCallback = callback; 245 246 mUpdateMonitor = new KeyguardUpdateMonitor(context); 247 248 mUpdateMonitor.registerConfigurationChangeCallback(this); 249 mUpdateMonitor.registerSimStateCallback(this); 250 251 mKeyguardViewProperties = 252 new LockPatternKeyguardViewProperties( 253 new LockPatternUtils(mContext.getContentResolver()), 254 mUpdateMonitor); 255 256 mKeyguardViewManager = new KeyguardViewManager( 257 context, WindowManagerImpl.getDefault(), this, 258 mKeyguardViewProperties, mUpdateMonitor); 259 260 mUserPresentIntent = new Intent(Intent.ACTION_USER_PRESENT); 261 } 262 263 /** 264 * Let us know that the system is ready after startup. 265 */ 266 public void onSystemReady() { 267 synchronized (this) { 268 if (DEBUG) Log.d(TAG, "onSystemReady"); 269 mSystemReady = true; 270 doKeyguard(); 271 } 272 } 273 274 /** 275 * Called to let us know the screen was turned off. 276 * @param why either {@link WindowManagerPolicy#OFF_BECAUSE_OF_USER} or 277 * {@link WindowManagerPolicy#OFF_BECAUSE_OF_TIMEOUT}. 278 */ 279 public void onScreenTurnedOff(int why) { 280 synchronized (this) { 281 mScreenOn = false; 282 if (DEBUG) Log.d(TAG, "onScreenTurnedOff(" + why + ")"); 283 284 if (mExitSecureCallback != null) { 285 if (DEBUG) Log.d(TAG, "pending exit secure callback cancelled"); 286 mExitSecureCallback.onKeyguardExitResult(false); 287 mExitSecureCallback = null; 288 if (!mExternallyEnabled) { 289 hideLocked(); 290 } 291 } else if (mShowing) { 292 notifyScreenOffLocked(); 293 resetStateLocked(); 294 } else if (why == WindowManagerPolicy.OFF_BECAUSE_OF_TIMEOUT) { 295 // if the screen turned off because of timeout, set an alarm 296 // to enable it a little bit later (i.e, give the user a chance 297 // to turn the screen back on within a certain window without 298 // having to unlock the screen) 299 long when = SystemClock.elapsedRealtime() + KEYGUARD_DELAY_MS; 300 Intent intent = new Intent(DELAYED_KEYGUARD_ACTION); 301 intent.putExtra("seq", mDelayedShowingSequence); 302 PendingIntent sender = PendingIntent.getBroadcast(mContext, 303 0, intent, PendingIntent.FLAG_CANCEL_CURRENT); 304 mAlarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, when, 305 sender); 306 if (DEBUG) Log.d(TAG, "setting alarm to turn off keyguard, seq = " 307 + mDelayedShowingSequence); 308 } else { 309 doKeyguard(); 310 } 311 } 312 } 313 314 /** 315 * Let's us know the screen was turned on. 316 */ 317 public void onScreenTurnedOn() { 318 synchronized (this) { 319 mScreenOn = true; 320 mDelayedShowingSequence++; 321 if (DEBUG) Log.d(TAG, "onScreenTurnedOn, seq = " + mDelayedShowingSequence); 322 notifyScreenOnLocked(); 323 } 324 } 325 326 /** 327 * Same semantics as {@link WindowManagerPolicy#enableKeyguard}; provide 328 * a way for external stuff to override normal keyguard behavior. For instance 329 * the phone app disables the keyguard when it receives incoming calls. 330 */ 331 public void setKeyguardEnabled(boolean enabled) { 332 synchronized (this) { 333 if (DEBUG) Log.d(TAG, "setKeyguardEnabled(" + enabled + ")"); 334 335 336 mExternallyEnabled = enabled; 337 338 if (!enabled && mShowing) { 339 if (mExitSecureCallback != null) { 340 if (DEBUG) Log.d(TAG, "in process of verifyUnlock request, ignoring"); 341 // we're in the process of handling a request to verify the user 342 // can get past the keyguard. ignore extraneous requests to disable / reenable 343 return; 344 } 345 346 // hiding keyguard that is showing, remember to reshow later 347 if (DEBUG) Log.d(TAG, "remembering to reshow, hiding keyguard, " 348 + "disabling status bar expansion"); 349 mNeedToReshowWhenReenabled = true; 350 setStatusBarExpandable(false); 351 hideLocked(); 352 } else if (enabled && mNeedToReshowWhenReenabled) { 353 // reenabled after previously hidden, reshow 354 if (DEBUG) Log.d(TAG, "previously hidden, reshowing, reenabling " 355 + "status bar expansion"); 356 mNeedToReshowWhenReenabled = false; 357 setStatusBarExpandable(true); 358 359 if (mExitSecureCallback != null) { 360 if (DEBUG) Log.d(TAG, "onKeyguardExitResult(false), resetting"); 361 mExitSecureCallback.onKeyguardExitResult(false); 362 mExitSecureCallback = null; 363 resetStateLocked(); 364 } else { 365 showLocked(); 366 367 // block until we know the keygaurd is done drawing (and post a message 368 // to unblock us after a timeout so we don't risk blocking too long 369 // and causing an ANR). 370 mWaitingUntilKeyguardVisible = true; 371 mHandler.sendEmptyMessageDelayed(KEYGUARD_DONE_DRAWING, KEYGUARD_DONE_DRAWING_TIMEOUT_MS); 372 if (DEBUG) Log.d(TAG, "waiting until mWaitingUntilKeyguardVisible is false"); 373 while (mWaitingUntilKeyguardVisible) { 374 try { 375 wait(); 376 } catch (InterruptedException e) { 377 Thread.currentThread().interrupt(); 378 } 379 } 380 if (DEBUG) Log.d(TAG, "done waiting for mWaitingUntilKeyguardVisible"); 381 } 382 } 383 } 384 } 385 386 /** 387 * @see android.app.KeyguardManager#exitKeyguardSecurely 388 */ 389 public void verifyUnlock(WindowManagerPolicy.OnKeyguardExitResult callback) { 390 synchronized (this) { 391 if (DEBUG) Log.d(TAG, "verifyUnlock"); 392 if (!mUpdateMonitor.isDeviceProvisioned()) { 393 // don't allow this api when the device isn't provisioned 394 if (DEBUG) Log.d(TAG, "ignoring because device isn't provisioned"); 395 callback.onKeyguardExitResult(false); 396 } else if (mExternallyEnabled) { 397 // this only applies when the user has externally disabled the 398 // keyguard. this is unexpected and means the user is not 399 // using the api properly. 400 Log.w(TAG, "verifyUnlock called when not externally disabled"); 401 callback.onKeyguardExitResult(false); 402 } else if (mExitSecureCallback != null) { 403 // already in progress with someone else 404 callback.onKeyguardExitResult(false); 405 } else { 406 mExitSecureCallback = callback; 407 verifyUnlockLocked(); 408 } 409 } 410 } 411 412 413 private void setStatusBarExpandable(boolean isExpandable) { 414 if (mStatusBarManager == null) { 415 mStatusBarManager = 416 (StatusBarManager) mContext.getSystemService(Context.STATUS_BAR_SERVICE); 417 } 418 mStatusBarManager.disable(isExpandable ? DISABLE_NONE : DISABLE_EXPAND); 419 } 420 421 /** 422 * Is the keyguard currently showing? 423 */ 424 public boolean isShowing() { 425 return mShowing; 426 } 427 428 /** 429 * Given the state of the keyguard, is the input restricted? 430 * Input is restricted when the keyguard is showing, or when the keyguard 431 * was suppressed by an app that disabled the keyguard or we haven't been provisioned yet. 432 */ 433 public boolean isInputRestricted() { 434 return mShowing || mNeedToReshowWhenReenabled || !mUpdateMonitor.isDeviceProvisioned(); 435 } 436 437 438 /** 439 * Enable the keyguard if the settings are appropriate. 440 */ 441 private void doKeyguard() { 442 synchronized (this) { 443 // if another app is disabling us, don't show 444 if (!mExternallyEnabled) { 445 if (DEBUG) Log.d(TAG, "doKeyguard: not showing because externally disabled"); 446 447 // note: we *should* set mNeedToReshowWhenReenabled=true here, but that makes 448 // for an occasional ugly flicker in this situation: 449 // 1) receive a call with the screen on (no keyguard) or make a call 450 // 2) screen times out 451 // 3) user hits key to turn screen back on 452 // instead, we reenable the keyguard when we know the screen is off and the call 453 // ends (see the broadcast receiver below) 454 // TODO: clean this up when we have better support at the window manager level 455 // for apps that wish to be on top of the keyguard 456 return; 457 } 458 459 // if the keyguard is already showing, don't bother 460 if (mKeyguardViewManager.isShowing()) { 461 if (DEBUG) Log.d(TAG, "doKeyguard: not showing because it is already showing"); 462 return; 463 } 464 465 // if the setup wizard hasn't run yet, don't show 466 final boolean provisioned = mUpdateMonitor.isDeviceProvisioned(); 467 final IccCard.State state = mUpdateMonitor.getSimState(); 468 final boolean lockedOrMissing = state.isPinLocked() || (state == IccCard.State.ABSENT); 469 if (!lockedOrMissing && !provisioned) { 470 if (DEBUG) Log.d(TAG, "doKeyguard: not showing because device isn't provisioned" 471 + " and the sim is not locked or missing"); 472 return; 473 } 474 475 if (DEBUG) Log.d(TAG, "doKeyguard: showing the lock screen"); 476 showLocked(); 477 } 478 } 479 480 /** 481 * Send message to keyguard telling it to reset its state. 482 * @see #handleReset() 483 */ 484 private void resetStateLocked() { 485 if (DEBUG) Log.d(TAG, "resetStateLocked"); 486 Message msg = mHandler.obtainMessage(RESET); 487 mHandler.sendMessage(msg); 488 } 489 490 /** 491 * Send message to keyguard telling it to verify unlock 492 * @see #handleVerifyUnlock() 493 */ 494 private void verifyUnlockLocked() { 495 if (DEBUG) Log.d(TAG, "verifyUnlockLocked"); 496 mHandler.sendEmptyMessage(VERIFY_UNLOCK); 497 } 498 499 500 /** 501 * Send a message to keyguard telling it the screen just turned on. 502 * @see #onScreenTurnedOff(int) 503 * @see #handleNotifyScreenOff 504 */ 505 private void notifyScreenOffLocked() { 506 if (DEBUG) Log.d(TAG, "notifyScreenOffLocked"); 507 mHandler.sendEmptyMessage(NOTIFY_SCREEN_OFF); 508 } 509 510 /** 511 * Send a message to keyguard telling it the screen just turned on. 512 * @see #onScreenTurnedOn() 513 * @see #handleNotifyScreenOn 514 */ 515 private void notifyScreenOnLocked() { 516 if (DEBUG) Log.d(TAG, "notifyScreenOnLocked"); 517 mHandler.sendEmptyMessage(NOTIFY_SCREEN_ON); 518 } 519 520 /** 521 * Send message to keyguard telling it about a wake key so it can adjust 522 * its state accordingly and then poke the wake lock when it is ready. 523 * @param keyCode The wake key. 524 * @see #handleWakeWhenReady 525 * @see #onWakeKeyWhenKeyguardShowingTq(int) 526 */ 527 private void wakeWhenReadyLocked(int keyCode) { 528 if (DBG_WAKE) Log.d(TAG, "wakeWhenReadyLocked(" + keyCode + ")"); 529 530 /** 531 * acquire the handoff lock that will keep the cpu running. this will 532 * be released once the keyguard has set itself up and poked the other wakelock 533 * in {@link #handleWakeWhenReady(int)} 534 */ 535 mWakeAndHandOff.acquire(); 536 537 Message msg = mHandler.obtainMessage(WAKE_WHEN_READY, keyCode, 0); 538 mHandler.sendMessage(msg); 539 } 540 541 /** 542 * Send message to keyguard telling it to show itself 543 * @see #handleShow() 544 */ 545 private void showLocked() { 546 if (DEBUG) Log.d(TAG, "showLocked"); 547 Message msg = mHandler.obtainMessage(SHOW); 548 mHandler.sendMessage(msg); 549 } 550 551 /** 552 * Send message to keyguard telling it to hide itself 553 * @see #handleHide() 554 */ 555 private void hideLocked() { 556 if (DEBUG) Log.d(TAG, "hideLocked"); 557 Message msg = mHandler.obtainMessage(HIDE); 558 mHandler.sendMessage(msg); 559 } 560 561 /** 562 * {@link KeyguardUpdateMonitor} callbacks. 563 */ 564 565 /** {@inheritDoc} */ 566 public void onOrientationChange(boolean inPortrait) { 567 568 } 569 570 /** {@inheritDoc} */ 571 public void onKeyboardChange(boolean isKeyboardOpen) { 572 mKeyboardOpen = isKeyboardOpen; 573 574 if (mKeyboardOpen && !mKeyguardViewProperties.isSecure() 575 && mKeyguardViewManager.isShowing()) { 576 if (DEBUG) Log.d(TAG, "bypassing keyguard on sliding open of keyboard with non-secure keyguard"); 577 keyguardDone(true); 578 } 579 } 580 581 /** {@inheritDoc} */ 582 public void onSimStateChanged(IccCard.State simState) { 583 if (DEBUG) Log.d(TAG, "onSimStateChanged: " + simState); 584 585 switch (simState) { 586 case ABSENT: 587 // only force lock screen in case of missing sim if user hasn't 588 // gone through setup wizard 589 if (!mUpdateMonitor.isDeviceProvisioned()) { 590 if (!isShowing()) { 591 if (DEBUG) Log.d(TAG, "INTENT_VALUE_ICC_ABSENT and keygaurd isn't showing, we need " 592 + "to show the keyguard since the device isn't provisioned yet."); 593 doKeyguard(); 594 } else { 595 resetStateLocked(); 596 } 597 } 598 break; 599 case PIN_REQUIRED: 600 case PUK_REQUIRED: 601 if (!isShowing()) { 602 if (DEBUG) Log.d(TAG, "INTENT_VALUE_ICC_LOCKED and keygaurd isn't showing, we need " 603 + "to show the keyguard so the user can enter their sim pin"); 604 doKeyguard(); 605 } else { 606 resetStateLocked(); 607 } 608 609 break; 610 case READY: 611 if (isShowing()) { 612 resetStateLocked(); 613 } 614 break; 615 } 616 } 617 618 private BroadcastReceiver mBroadCastReceiver = new BroadcastReceiver() { 619 @Override 620 public void onReceive(Context context, Intent intent) { 621 final String action = intent.getAction(); 622 if (action.equals(DELAYED_KEYGUARD_ACTION)) { 623 624 int sequence = intent.getIntExtra("seq", 0); 625 626 if (false) Log.d(TAG, "received DELAYED_KEYGUARD_ACTION with seq = " 627 + sequence + ", mDelayedShowingSequence = " + mDelayedShowingSequence); 628 629 if (mDelayedShowingSequence == sequence) { 630 doKeyguard(); 631 } 632 } else if (TelephonyManager.ACTION_PHONE_STATE_CHANGED.equals(action) 633 && TelephonyManager.EXTRA_STATE_IDLE.equals(intent.getStringExtra( 634 TelephonyManager.EXTRA_STATE)) // call ending 635 && !mScreenOn // screen off 636 && mExternallyEnabled) { // not disabled by any app 637 638 // note: this is a way to gracefully reenable the keyguard when the call 639 // ends and the screen is off without always reenabling the keyguard 640 // each time the screen turns off while in call (and having an occasional ugly 641 // flicker while turning back on the screen and disabling the keyguard again). 642 if (DEBUG) Log.d(TAG, "screen is off and call ended, let's make sure the " 643 + "keyguard is showing"); 644 doKeyguard(); 645 } 646 } 647 }; 648 649 650 /** 651 * When a key is received when the screen is off and the keyguard is showing, 652 * we need to decide whether to actually turn on the screen, and if so, tell 653 * the keyguard to prepare itself and poke the wake lock when it is ready. 654 * 655 * The 'Tq' suffix is per the documentation in {@link WindowManagerPolicy}. 656 * Be sure not to take any action that takes a long time; any significant 657 * action should be posted to a handler. 658 * 659 * @param keyCode The keycode of the key that woke the device 660 * @return Whether we poked the wake lock (and turned the screen on) 661 */ 662 public boolean onWakeKeyWhenKeyguardShowingTq(int keyCode) { 663 if (DEBUG) Log.d(TAG, "onWakeKeyWhenKeyguardShowing(" + keyCode + ")"); 664 665 if (isWakeKeyWhenKeyguardShowing(keyCode)) { 666 // give the keyguard view manager a chance to adjust the state of the 667 // keyguard based on the key that woke the device before poking 668 // the wake lock 669 wakeWhenReadyLocked(keyCode); 670 return true; 671 } else { 672 return false; 673 } 674 } 675 676 private boolean isWakeKeyWhenKeyguardShowing(int keyCode) { 677 switch (keyCode) { 678 case KeyEvent.KEYCODE_VOLUME_UP: 679 case KeyEvent.KEYCODE_VOLUME_DOWN: 680 case KeyEvent.KEYCODE_MUTE: 681 case KeyEvent.KEYCODE_HEADSETHOOK: 682 case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE: 683 case KeyEvent.KEYCODE_MEDIA_STOP: 684 case KeyEvent.KEYCODE_MEDIA_NEXT: 685 case KeyEvent.KEYCODE_MEDIA_PREVIOUS: 686 case KeyEvent.KEYCODE_MEDIA_REWIND: 687 case KeyEvent.KEYCODE_MEDIA_FAST_FORWARD: 688 case KeyEvent.KEYCODE_CAMERA: 689 return false; 690 } 691 return true; 692 } 693 694 /** 695 * Callbacks from {@link KeyguardViewManager}. 696 */ 697 698 /** {@inheritDoc} */ 699 public void pokeWakelock() { 700 pokeWakelock(mKeyboardOpen ? 701 AWAKE_INTERVAL_DEFAULT_KEYBOARD_OPEN_MS : AWAKE_INTERVAL_DEFAULT_MS); 702 } 703 704 /** {@inheritDoc} */ 705 public void pokeWakelock(int holdMs) { 706 synchronized (this) { 707 if (DBG_WAKE) Log.d(TAG, "pokeWakelock(" + holdMs + ")"); 708 mWakeLock.acquire(); 709 mHandler.removeMessages(TIMEOUT); 710 mWakelockSequence++; 711 Message msg = mHandler.obtainMessage(TIMEOUT, mWakelockSequence, 0); 712 mHandler.sendMessageDelayed(msg, holdMs); 713 } 714 } 715 716 /** 717 * {@inheritDoc} 718 * 719 * @see #handleKeyguardDone 720 */ 721 public void keyguardDone(boolean authenticated) { 722 synchronized (this) { 723 EventLog.writeEvent(70000, 2); 724 if (DEBUG) Log.d(TAG, "keyguardDone(" + authenticated + ")"); 725 Message msg = mHandler.obtainMessage(KEYGUARD_DONE); 726 mHandler.sendMessage(msg); 727 728 if (authenticated) { 729 mUpdateMonitor.clearFailedAttempts(); 730 } 731 732 if (mExitSecureCallback != null) { 733 mExitSecureCallback.onKeyguardExitResult(authenticated); 734 mExitSecureCallback = null; 735 736 if (authenticated) { 737 // after succesfully exiting securely, no need to reshow 738 // the keyguard when they've released the lock 739 mExternallyEnabled = true; 740 mNeedToReshowWhenReenabled = false; 741 setStatusBarExpandable(true); 742 } 743 } 744 } 745 } 746 747 /** 748 * {@inheritDoc} 749 * 750 * @see #handleKeyguardDoneDrawing 751 */ 752 public void keyguardDoneDrawing() { 753 mHandler.sendEmptyMessage(KEYGUARD_DONE_DRAWING); 754 } 755 756 /** 757 * This handler will be associated with the policy thread, which will also 758 * be the UI thread of the keyguard. Since the apis of the policy, and therefore 759 * this class, can be called by other threads, any action that directly 760 * interacts with the keyguard ui should be posted to this handler, rather 761 * than called directly. 762 */ 763 private Handler mHandler = new Handler() { 764 @Override 765 public void handleMessage(Message msg) { 766 switch (msg.what) { 767 case TIMEOUT: 768 handleTimeout(msg.arg1); 769 return ; 770 case SHOW: 771 handleShow(); 772 return ; 773 case HIDE: 774 handleHide(); 775 return ; 776 case RESET: 777 handleReset(); 778 return ; 779 case VERIFY_UNLOCK: 780 handleVerifyUnlock(); 781 return; 782 case NOTIFY_SCREEN_OFF: 783 handleNotifyScreenOff(); 784 return; 785 case NOTIFY_SCREEN_ON: 786 handleNotifyScreenOn(); 787 return; 788 case WAKE_WHEN_READY: 789 handleWakeWhenReady(msg.arg1); 790 return; 791 case KEYGUARD_DONE: 792 handleKeyguardDone(); 793 return; 794 case KEYGUARD_DONE_DRAWING: 795 handleKeyguardDoneDrawing(); 796 } 797 } 798 }; 799 800 /** 801 * @see #keyguardDone 802 * @see #KEYGUARD_DONE 803 */ 804 private void handleKeyguardDone() { 805 if (DEBUG) Log.d(TAG, "handleKeyguardDone"); 806 handleHide(); 807 mPM.userActivity(SystemClock.uptimeMillis(), true); 808 mWakeLock.release(); 809 mContext.sendBroadcast(mUserPresentIntent); 810 } 811 812 /** 813 * @see #keyguardDoneDrawing 814 * @see #KEYGUARD_DONE_DRAWING 815 */ 816 private void handleKeyguardDoneDrawing() { 817 synchronized(this) { 818 if (false) Log.d(TAG, "handleKeyguardDoneDrawing"); 819 if (mWaitingUntilKeyguardVisible) { 820 if (DEBUG) Log.d(TAG, "handleKeyguardDoneDrawing: notifying mWaitingUntilKeyguardVisible"); 821 mWaitingUntilKeyguardVisible = false; 822 notifyAll(); 823 824 // there will usually be two of these sent, one as a timeout, and one 825 // as a result of the callback, so remove any remaining messages from 826 // the queue 827 mHandler.removeMessages(KEYGUARD_DONE_DRAWING); 828 } 829 } 830 } 831 832 /** 833 * Handles the message sent by {@link #pokeWakelock} 834 * @param seq used to determine if anything has changed since the message 835 * was sent. 836 * @see #TIMEOUT 837 */ 838 private void handleTimeout(int seq) { 839 synchronized (KeyguardViewMediator.this) { 840 if (DEBUG) Log.d(TAG, "handleTimeout"); 841 if (seq == mWakelockSequence) { 842 mWakeLock.release(); 843 } 844 } 845 } 846 847 /** 848 * Handle message sent by {@link #showLocked}. 849 * @see #SHOW 850 */ 851 private void handleShow() { 852 synchronized (KeyguardViewMediator.this) { 853 if (DEBUG) Log.d(TAG, "handleShow"); 854 if (!mSystemReady) return; 855 856 // while we're showing, we control the wake state, so ask the power 857 // manager not to honor request for userActivity. 858 mRealPowerManager.enableUserActivity(false); 859 860 mKeyguardViewManager.show(); 861 mShowing = true; 862 } 863 } 864 865 /** 866 * Handle message sent by {@link #hideLocked()} 867 * @see #HIDE 868 */ 869 private void handleHide() { 870 synchronized (KeyguardViewMediator.this) { 871 if (DEBUG) Log.d(TAG, "handleHide"); 872 // When we go away, tell the poewr manager to honor requests from userActivity. 873 mRealPowerManager.enableUserActivity(true); 874 875 mKeyguardViewManager.hide(); 876 mShowing = false; 877 } 878 } 879 880 /** 881 * Handle message sent by {@link #wakeWhenReadyLocked(int)} 882 * @param keyCode The key that woke the device. 883 * @see #WAKE_WHEN_READY 884 */ 885 private void handleWakeWhenReady(int keyCode) { 886 synchronized (KeyguardViewMediator.this) { 887 if (DBG_WAKE) Log.d(TAG, "handleWakeWhenReady(" + keyCode + ")"); 888 889 // this should result in a call to 'poke wakelock' which will set a timeout 890 // on releasing the wakelock 891 mKeyguardViewManager.wakeWhenReadyTq(keyCode); 892 893 /** 894 * Now that the keyguard is ready and has poked the wake lock, we can 895 * release the handoff wakelock 896 */ 897 mWakeAndHandOff.release(); 898 899 if (!mWakeLock.isHeld()) { 900 Log.w(TAG, "mKeyguardViewManager.wakeWhenReadyTq did not poke wake lock"); 901 } 902 } 903 } 904 905 /** 906 * Handle message sent by {@link #resetStateLocked()} 907 * @see #RESET 908 */ 909 private void handleReset() { 910 synchronized (KeyguardViewMediator.this) { 911 if (DEBUG) Log.d(TAG, "handleReset"); 912 mKeyguardViewManager.reset(); 913 } 914 } 915 916 /** 917 * Handle message sent by {@link #verifyUnlock} 918 * @see #RESET 919 */ 920 private void handleVerifyUnlock() { 921 synchronized (KeyguardViewMediator.this) { 922 if (DEBUG) Log.d(TAG, "handleVerifyUnlock"); 923 mKeyguardViewManager.verifyUnlock(); 924 mShowing = true; 925 } 926 } 927 928 /** 929 * Handle message sent by {@link #notifyScreenOffLocked()} 930 * @see #NOTIFY_SCREEN_OFF 931 */ 932 private void handleNotifyScreenOff() { 933 synchronized (KeyguardViewMediator.this) { 934 if (DEBUG) Log.d(TAG, "handleNotifyScreenOff"); 935 mKeyguardViewManager.onScreenTurnedOff(); 936 } 937 } 938 939 /** 940 * Handle message sent by {@link #notifyScreenOnLocked()} 941 * @see #NOTIFY_SCREEN_ON 942 */ 943 private void handleNotifyScreenOn() { 944 synchronized (KeyguardViewMediator.this) { 945 if (DEBUG) Log.d(TAG, "handleNotifyScreenOn"); 946 mKeyguardViewManager.onScreenTurnedOn(); 947 } 948 } 949} 950 951 952