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