OSDN Git Service

DO NOT MERGE. Grant MMS Uri permissions as the calling UID.
[android-x86/frameworks-base.git] / packages / SystemUI / src / com / android / systemui / keyguard / KeyguardViewMediator.java
1 /*
2  * Copyright (C) 2014 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.systemui.keyguard;
18
19 import static android.provider.Settings.System.SCREEN_OFF_TIMEOUT;
20 import static com.android.internal.widget.LockPatternUtils.StrongAuthTracker.SOME_AUTH_REQUIRED_AFTER_USER_REQUEST;
21 import static com.android.internal.widget.LockPatternUtils.StrongAuthTracker.STRONG_AUTH_REQUIRED_AFTER_DPM_LOCK_NOW;
22 import static com.android.internal.widget.LockPatternUtils.StrongAuthTracker.STRONG_AUTH_REQUIRED_AFTER_LOCKOUT;
23
24 import android.app.Activity;
25 import android.app.ActivityManager;
26 import android.app.ActivityManagerNative;
27 import android.app.AlarmManager;
28 import android.app.PendingIntent;
29 import android.app.SearchManager;
30 import android.app.StatusBarManager;
31 import android.app.trust.TrustManager;
32 import android.content.BroadcastReceiver;
33 import android.content.ContentResolver;
34 import android.content.Context;
35 import android.content.Intent;
36 import android.content.IntentFilter;
37 import android.content.pm.UserInfo;
38 import android.media.AudioManager;
39 import android.media.SoundPool;
40 import android.os.Bundle;
41 import android.os.DeadObjectException;
42 import android.os.Handler;
43 import android.os.Looper;
44 import android.os.Message;
45 import android.os.PowerManager;
46 import android.os.RemoteException;
47 import android.os.SystemClock;
48 import android.os.SystemProperties;
49 import android.os.Trace;
50 import android.os.UserHandle;
51 import android.os.UserManager;
52 import android.os.storage.StorageManager;
53 import android.provider.Settings;
54 import android.telephony.SubscriptionManager;
55 import android.telephony.TelephonyManager;
56 import android.util.EventLog;
57 import android.util.Log;
58 import android.util.Slog;
59 import android.view.IWindowManager;
60 import android.view.ViewGroup;
61 import android.view.WindowManagerGlobal;
62 import android.view.WindowManagerPolicy;
63 import android.view.animation.Animation;
64 import android.view.animation.AnimationUtils;
65
66 import com.android.internal.policy.IKeyguardDrawnCallback;
67 import com.android.internal.policy.IKeyguardExitCallback;
68 import com.android.internal.policy.IKeyguardStateCallback;
69 import com.android.internal.telephony.IccCardConstants;
70 import com.android.internal.widget.LockPatternUtils;
71 import com.android.keyguard.KeyguardConstants;
72 import com.android.keyguard.KeyguardDisplayManager;
73 import com.android.keyguard.KeyguardSecurityView;
74 import com.android.keyguard.KeyguardUpdateMonitor;
75 import com.android.keyguard.KeyguardUpdateMonitorCallback;
76 import com.android.keyguard.ViewMediatorCallback;
77 import com.android.systemui.SystemUI;
78 import com.android.systemui.SystemUIFactory;
79 import com.android.systemui.classifier.FalsingManager;
80 import com.android.systemui.statusbar.phone.FingerprintUnlockController;
81 import com.android.systemui.statusbar.phone.PhoneStatusBar;
82 import com.android.systemui.statusbar.phone.ScrimController;
83 import com.android.systemui.statusbar.phone.StatusBarKeyguardViewManager;
84 import com.android.systemui.statusbar.phone.StatusBarWindowManager;
85
86 import java.io.FileDescriptor;
87 import java.io.PrintWriter;
88 import java.util.ArrayList;
89
90 /**
91  * Mediates requests related to the keyguard.  This includes queries about the
92  * state of the keyguard, power management events that effect whether the keyguard
93  * should be shown or reset, callbacks to the phone window manager to notify
94  * it of when the keyguard is showing, and events from the keyguard view itself
95  * stating that the keyguard was succesfully unlocked.
96  *
97  * Note that the keyguard view is shown when the screen is off (as appropriate)
98  * so that once the screen comes on, it will be ready immediately.
99  *
100  * Example queries about the keyguard:
101  * - is {movement, key} one that should wake the keygaurd?
102  * - is the keyguard showing?
103  * - are input events restricted due to the state of the keyguard?
104  *
105  * Callbacks to the phone window manager:
106  * - the keyguard is showing
107  *
108  * Example external events that translate to keyguard view changes:
109  * - screen turned off -> reset the keyguard, and show it so it will be ready
110  *   next time the screen turns on
111  * - keyboard is slid open -> if the keyguard is not secure, hide it
112  *
113  * Events from the keyguard view:
114  * - user succesfully unlocked keyguard -> hide keyguard view, and no longer
115  *   restrict input events.
116  *
117  * Note: in addition to normal power managment events that effect the state of
118  * whether the keyguard should be showing, external apps and services may request
119  * that the keyguard be disabled via {@link #setKeyguardEnabled(boolean)}.  When
120  * false, this will override all other conditions for turning on the keyguard.
121  *
122  * Threading and synchronization:
123  * This class is created by the initialization routine of the {@link android.view.WindowManagerPolicy},
124  * and runs on its thread.  The keyguard UI is created from that thread in the
125  * constructor of this class.  The apis may be called from other threads, including the
126  * {@link com.android.server.input.InputManagerService}'s and {@link android.view.WindowManager}'s.
127  * Therefore, methods on this class are synchronized, and any action that is pointed
128  * directly to the keyguard UI is posted to a {@link android.os.Handler} to ensure it is taken on the UI
129  * thread of the keyguard.
130  */
131 public class KeyguardViewMediator extends SystemUI {
132     private static final int KEYGUARD_DISPLAY_TIMEOUT_DELAY_DEFAULT = 30000;
133     private static final long KEYGUARD_DONE_PENDING_TIMEOUT_MS = 3000;
134
135     private static final boolean DEBUG = KeyguardConstants.DEBUG;
136     private static final boolean DEBUG_SIM_STATES = KeyguardConstants.DEBUG_SIM_STATES;
137     private final static boolean DBG_WAKE = false;
138
139     private final static String TAG = "KeyguardViewMediator";
140
141     private static final String DELAYED_KEYGUARD_ACTION =
142         "com.android.internal.policy.impl.PhoneWindowManager.DELAYED_KEYGUARD";
143     private static final String DELAYED_LOCK_PROFILE_ACTION =
144             "com.android.internal.policy.impl.PhoneWindowManager.DELAYED_LOCK";
145
146     // used for handler messages
147     private static final int SHOW = 2;
148     private static final int HIDE = 3;
149     private static final int RESET = 4;
150     private static final int VERIFY_UNLOCK = 5;
151     private static final int NOTIFY_FINISHED_GOING_TO_SLEEP = 6;
152     private static final int NOTIFY_SCREEN_TURNING_ON = 7;
153     private static final int KEYGUARD_DONE = 9;
154     private static final int KEYGUARD_DONE_DRAWING = 10;
155     private static final int KEYGUARD_DONE_AUTHENTICATING = 11;
156     private static final int SET_OCCLUDED = 12;
157     private static final int KEYGUARD_TIMEOUT = 13;
158     private static final int DISMISS = 17;
159     private static final int START_KEYGUARD_EXIT_ANIM = 18;
160     private static final int ON_ACTIVITY_DRAWN = 19;
161     private static final int KEYGUARD_DONE_PENDING_TIMEOUT = 20;
162     private static final int NOTIFY_STARTED_WAKING_UP = 21;
163     private static final int NOTIFY_SCREEN_TURNED_ON = 22;
164     private static final int NOTIFY_SCREEN_TURNED_OFF = 23;
165     private static final int NOTIFY_STARTED_GOING_TO_SLEEP = 24;
166
167     /**
168      * The default amount of time we stay awake (used for all key input)
169      */
170     public static final int AWAKE_INTERVAL_DEFAULT_MS = 10000;
171
172     /**
173      * How long to wait after the screen turns off due to timeout before
174      * turning on the keyguard (i.e, the user has this much time to turn
175      * the screen back on without having to face the keyguard).
176      */
177     private static final int KEYGUARD_LOCK_AFTER_DELAY_DEFAULT = 5000;
178
179     /**
180      * How long we'll wait for the {@link ViewMediatorCallback#keyguardDoneDrawing()}
181      * callback before unblocking a call to {@link #setKeyguardEnabled(boolean)}
182      * that is reenabling the keyguard.
183      */
184     private static final int KEYGUARD_DONE_DRAWING_TIMEOUT_MS = 2000;
185
186     /**
187      * Secure setting whether analytics are collected on the keyguard.
188      */
189     private static final String KEYGUARD_ANALYTICS_SETTING = "keyguard_analytics";
190
191     /** The stream type that the lock sounds are tied to. */
192     private int mUiSoundsStreamType;
193
194     private AlarmManager mAlarmManager;
195     private AudioManager mAudioManager;
196     private StatusBarManager mStatusBarManager;
197     private boolean mSwitchingUser;
198
199     private boolean mSystemReady;
200     private boolean mBootCompleted;
201     private boolean mBootSendUserPresent;
202
203     /** High level access to the power manager for WakeLocks */
204     private PowerManager mPM;
205
206     /** High level access to the window manager for dismissing keyguard animation */
207     private IWindowManager mWM;
208
209
210     /** TrustManager for letting it know when we change visibility */
211     private TrustManager mTrustManager;
212
213     /** SearchManager for determining whether or not search assistant is available */
214     private SearchManager mSearchManager;
215
216     /**
217      * Used to keep the device awake while to ensure the keyguard finishes opening before
218      * we sleep.
219      */
220     private PowerManager.WakeLock mShowKeyguardWakeLock;
221
222     private StatusBarKeyguardViewManager mStatusBarKeyguardViewManager;
223
224     // these are protected by synchronized (this)
225
226     /**
227      * External apps (like the phone app) can tell us to disable the keygaurd.
228      */
229     private boolean mExternallyEnabled = true;
230
231     /**
232      * Remember if an external call to {@link #setKeyguardEnabled} with value
233      * false caused us to hide the keyguard, so that we need to reshow it once
234      * the keygaurd is reenabled with another call with value true.
235      */
236     private boolean mNeedToReshowWhenReenabled = false;
237
238     // cached value of whether we are showing (need to know this to quickly
239     // answer whether the input should be restricted)
240     private boolean mShowing;
241
242     /** Cached value of #isInputRestricted */
243     private boolean mInputRestricted;
244
245     // true if the keyguard is hidden by another window
246     private boolean mOccluded = false;
247
248     /**
249      * Helps remember whether the screen has turned on since the last time
250      * it turned off due to timeout. see {@link #onScreenTurnedOff(int)}
251      */
252     private int mDelayedShowingSequence;
253
254     /**
255      * Simiar to {@link #mDelayedProfileShowingSequence}, but it is for profile case.
256      */
257     private int mDelayedProfileShowingSequence;
258
259     /**
260      * If the user has disabled the keyguard, then requests to exit, this is
261      * how we'll ultimately let them know whether it was successful.  We use this
262      * var being non-null as an indicator that there is an in progress request.
263      */
264     private IKeyguardExitCallback mExitSecureCallback;
265
266     // the properties of the keyguard
267
268     private KeyguardUpdateMonitor mUpdateMonitor;
269
270     private boolean mDeviceInteractive;
271     private boolean mGoingToSleep;
272
273     // last known state of the cellular connection
274     private String mPhoneState = TelephonyManager.EXTRA_STATE_IDLE;
275
276     /**
277      * Whether a hide is pending an we are just waiting for #startKeyguardExitAnimation to be
278      * called.
279      * */
280     private boolean mHiding;
281
282     /**
283      * we send this intent when the keyguard is dismissed.
284      */
285     private static final Intent USER_PRESENT_INTENT = new Intent(Intent.ACTION_USER_PRESENT)
286             .addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING
287                     | Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
288
289     /**
290      * {@link #setKeyguardEnabled} waits on this condition when it reenables
291      * the keyguard.
292      */
293     private boolean mWaitingUntilKeyguardVisible = false;
294     private LockPatternUtils mLockPatternUtils;
295     private boolean mKeyguardDonePending = false;
296     private boolean mHideAnimationRun = false;
297
298     private SoundPool mLockSounds;
299     private int mLockSoundId;
300     private int mUnlockSoundId;
301     private int mTrustedSoundId;
302     private int mLockSoundStreamId;
303
304     /**
305      * The animation used for hiding keyguard. This is used to fetch the animation timings if
306      * WindowManager is not providing us with them.
307      */
308     private Animation mHideAnimation;
309
310     /**
311      * The volume applied to the lock/unlock sounds.
312      */
313     private float mLockSoundVolume;
314
315     /**
316      * For managing external displays
317      */
318     private KeyguardDisplayManager mKeyguardDisplayManager;
319
320     private final ArrayList<IKeyguardStateCallback> mKeyguardStateCallbacks = new ArrayList<>();
321
322     /**
323      * When starting going to sleep, we figured out that we need to reset Keyguard state and this
324      * should be committed when finished going to sleep.
325      */
326     private boolean mPendingReset;
327
328     /**
329      * When starting going to sleep, we figured out that we need to lock Keyguard and this should be
330      * committed when finished going to sleep.
331      */
332     private boolean mPendingLock;
333
334     private boolean mLockLater;
335
336     private boolean mWakeAndUnlocking;
337     private IKeyguardDrawnCallback mDrawnCallback;
338
339     private boolean mIsPerUserLock;
340
341     KeyguardUpdateMonitorCallback mUpdateCallback = new KeyguardUpdateMonitorCallback() {
342
343         @Override
344         public void onUserSwitching(int userId) {
345             // Note that the mLockPatternUtils user has already been updated from setCurrentUser.
346             // We need to force a reset of the views, since lockNow (called by
347             // ActivityManagerService) will not reconstruct the keyguard if it is already showing.
348             synchronized (KeyguardViewMediator.this) {
349                 mSwitchingUser = true;
350                 resetKeyguardDonePendingLocked();
351                 resetStateLocked();
352                 adjustStatusBarLocked();
353             }
354         }
355
356         @Override
357         public void onUserSwitchComplete(int userId) {
358             mSwitchingUser = false;
359             if (userId != UserHandle.USER_SYSTEM) {
360                 UserInfo info = UserManager.get(mContext).getUserInfo(userId);
361                 if (info != null && (info.isGuest() || info.isDemo())) {
362                     // If we just switched to a guest, try to dismiss keyguard.
363                     dismiss(false /* allowWhileOccluded */);
364                 }
365             }
366         }
367
368         @Override
369         public void onUserInfoChanged(int userId) {
370         }
371
372         @Override
373         public void onPhoneStateChanged(int phoneState) {
374             synchronized (KeyguardViewMediator.this) {
375                 if (TelephonyManager.CALL_STATE_IDLE == phoneState  // call ending
376                         && !mDeviceInteractive                           // screen off
377                         && mExternallyEnabled) {                // not disabled by any app
378
379                     // note: this is a way to gracefully reenable the keyguard when the call
380                     // ends and the screen is off without always reenabling the keyguard
381                     // each time the screen turns off while in call (and having an occasional ugly
382                     // flicker while turning back on the screen and disabling the keyguard again).
383                     if (DEBUG) Log.d(TAG, "screen is off and call ended, let's make sure the "
384                             + "keyguard is showing");
385                     doKeyguardLocked(null);
386                 }
387             }
388         }
389
390         @Override
391         public void onClockVisibilityChanged() {
392             adjustStatusBarLocked();
393         }
394
395         @Override
396         public void onDeviceProvisioned() {
397             sendUserPresentBroadcast();
398             synchronized (KeyguardViewMediator.this) {
399                 // If system user is provisioned, we might want to lock now to avoid showing launcher
400                 if (mustNotUnlockCurrentUser()) {
401                     doKeyguardLocked(null);
402                 }
403             }
404         }
405
406         @Override
407         public void onSimStateChanged(int subId, int slotId, IccCardConstants.State simState) {
408
409             if (DEBUG_SIM_STATES) {
410                 Log.d(TAG, "onSimStateChanged(subId=" + subId + ", slotId=" + slotId
411                         + ",state=" + simState + ")");
412             }
413
414             int size = mKeyguardStateCallbacks.size();
415             boolean simPinSecure = mUpdateMonitor.isSimPinSecure();
416             for (int i = size - 1; i >= 0; i--) {
417                 try {
418                     mKeyguardStateCallbacks.get(i).onSimSecureStateChanged(simPinSecure);
419                 } catch (RemoteException e) {
420                     Slog.w(TAG, "Failed to call onSimSecureStateChanged", e);
421                     if (e instanceof DeadObjectException) {
422                         mKeyguardStateCallbacks.remove(i);
423                     }
424                 }
425             }
426
427             switch (simState) {
428                 case NOT_READY:
429                 case ABSENT:
430                     // only force lock screen in case of missing sim if user hasn't
431                     // gone through setup wizard
432                     synchronized (this) {
433                         if (shouldWaitForProvisioning()) {
434                             if (!mShowing) {
435                                 if (DEBUG_SIM_STATES) Log.d(TAG, "ICC_ABSENT isn't showing,"
436                                         + " we need to show the keyguard since the "
437                                         + "device isn't provisioned yet.");
438                                 doKeyguardLocked(null);
439                             } else {
440                                 resetStateLocked();
441                             }
442                         }
443                     }
444                     break;
445                 case PIN_REQUIRED:
446                 case PUK_REQUIRED:
447                     synchronized (this) {
448                         if (!mShowing) {
449                             if (DEBUG_SIM_STATES) Log.d(TAG,
450                                     "INTENT_VALUE_ICC_LOCKED and keygaurd isn't "
451                                     + "showing; need to show keyguard so user can enter sim pin");
452                             doKeyguardLocked(null);
453                         } else {
454                             resetStateLocked();
455                         }
456                     }
457                     break;
458                 case PERM_DISABLED:
459                     synchronized (this) {
460                         if (!mShowing) {
461                             if (DEBUG_SIM_STATES) Log.d(TAG, "PERM_DISABLED and "
462                                   + "keygaurd isn't showing.");
463                             doKeyguardLocked(null);
464                         } else {
465                             if (DEBUG_SIM_STATES) Log.d(TAG, "PERM_DISABLED, resetStateLocked to"
466                                   + "show permanently disabled message in lockscreen.");
467                             resetStateLocked();
468                         }
469                     }
470                     break;
471                 case READY:
472                     synchronized (this) {
473                         if (mShowing) {
474                             resetStateLocked();
475                         }
476                     }
477                     break;
478                 default:
479                     if (DEBUG_SIM_STATES) Log.v(TAG, "Ignoring state: " + simState);
480                     break;
481             }
482         }
483
484         @Override
485         public void onFingerprintAuthFailed() {
486             final int currentUser = KeyguardUpdateMonitor.getCurrentUser();
487             if (mLockPatternUtils.isSecure(currentUser)) {
488                 mLockPatternUtils.getDevicePolicyManager().reportFailedFingerprintAttempt(
489                         currentUser);
490             }
491         }
492
493         @Override
494         public void onFingerprintAuthenticated(int userId) {
495             if (mLockPatternUtils.isSecure(userId)) {
496                 mLockPatternUtils.getDevicePolicyManager().reportSuccessfulFingerprintAttempt(
497                         userId);
498             }
499         }
500
501         @Override
502         public void onTrustChanged(int userId) {
503             if (userId == KeyguardUpdateMonitor.getCurrentUser()) {
504                 synchronized (KeyguardViewMediator.this) {
505                     notifyTrustedChangedLocked(mUpdateMonitor.getUserHasTrust(userId));
506                 }
507             }
508         }
509
510         @Override
511         public void onHasLockscreenWallpaperChanged(boolean hasLockscreenWallpaper) {
512             synchronized (KeyguardViewMediator.this) {
513                 notifyHasLockscreenWallpaperChanged(hasLockscreenWallpaper);
514             }
515         }
516     };
517
518     ViewMediatorCallback mViewMediatorCallback = new ViewMediatorCallback() {
519
520         @Override
521         public void userActivity() {
522             KeyguardViewMediator.this.userActivity();
523         }
524
525         @Override
526         public void keyguardDone(boolean strongAuth) {
527             if (!mKeyguardDonePending) {
528                 KeyguardViewMediator.this.keyguardDone(true /* authenticated */);
529             }
530             if (strongAuth) {
531                 mUpdateMonitor.reportSuccessfulStrongAuthUnlockAttempt();
532             }
533         }
534
535         @Override
536         public void keyguardDoneDrawing() {
537             Trace.beginSection("KeyguardViewMediator.mViewMediatorCallback#keyguardDoneDrawing");
538             mHandler.sendEmptyMessage(KEYGUARD_DONE_DRAWING);
539             Trace.endSection();
540         }
541
542         @Override
543         public void setNeedsInput(boolean needsInput) {
544             mStatusBarKeyguardViewManager.setNeedsInput(needsInput);
545         }
546
547         @Override
548         public void keyguardDonePending(boolean strongAuth) {
549             Trace.beginSection("KeyguardViewMediator.mViewMediatorCallback#keyguardDonePending");
550             mKeyguardDonePending = true;
551             mHideAnimationRun = true;
552             mStatusBarKeyguardViewManager.startPreHideAnimation(null /* finishRunnable */);
553             mHandler.sendEmptyMessageDelayed(KEYGUARD_DONE_PENDING_TIMEOUT,
554                     KEYGUARD_DONE_PENDING_TIMEOUT_MS);
555             if (strongAuth) {
556                 mUpdateMonitor.reportSuccessfulStrongAuthUnlockAttempt();
557             }
558             Trace.endSection();
559         }
560
561         @Override
562         public void keyguardGone() {
563             Trace.beginSection("KeyguardViewMediator.mViewMediatorCallback#keyguardGone");
564             mKeyguardDisplayManager.hide();
565             Trace.endSection();
566         }
567
568         @Override
569         public void readyForKeyguardDone() {
570             Trace.beginSection("KeyguardViewMediator.mViewMediatorCallback#readyForKeyguardDone");
571             if (mKeyguardDonePending) {
572                 // Somebody has called keyguardDonePending before, which means that we are
573                 // authenticated
574                 KeyguardViewMediator.this.keyguardDone(true /* authenticated */);
575             }
576             Trace.endSection();
577         }
578
579         @Override
580         public void resetKeyguard() {
581             resetStateLocked();
582         }
583
584         @Override
585         public void playTrustedSound() {
586             KeyguardViewMediator.this.playTrustedSound();
587         }
588
589         @Override
590         public boolean isInputRestricted() {
591             return KeyguardViewMediator.this.isInputRestricted();
592         }
593
594         @Override
595         public boolean isScreenOn() {
596             return mDeviceInteractive;
597         }
598
599         @Override
600         public int getBouncerPromptReason() {
601             int currentUser = ActivityManager.getCurrentUser();
602             boolean trust = mTrustManager.isTrustUsuallyManaged(currentUser);
603             boolean fingerprint = mUpdateMonitor.isUnlockWithFingerprintPossible(currentUser);
604             boolean any = trust || fingerprint;
605             KeyguardUpdateMonitor.StrongAuthTracker strongAuthTracker =
606                     mUpdateMonitor.getStrongAuthTracker();
607             int strongAuth = strongAuthTracker.getStrongAuthForUser(currentUser);
608
609             if (any && !strongAuthTracker.hasUserAuthenticatedSinceBoot()) {
610                 return KeyguardSecurityView.PROMPT_REASON_RESTART;
611             } else if (fingerprint && mUpdateMonitor.hasFingerprintUnlockTimedOut(currentUser)) {
612                 return KeyguardSecurityView.PROMPT_REASON_TIMEOUT;
613             } else if (any && (strongAuth & STRONG_AUTH_REQUIRED_AFTER_DPM_LOCK_NOW) != 0) {
614                 return KeyguardSecurityView.PROMPT_REASON_DEVICE_ADMIN;
615             } else if (trust && (strongAuth & SOME_AUTH_REQUIRED_AFTER_USER_REQUEST) != 0) {
616                 return KeyguardSecurityView.PROMPT_REASON_USER_REQUEST;
617             } else if (any && (strongAuth & STRONG_AUTH_REQUIRED_AFTER_LOCKOUT) != 0) {
618                 return KeyguardSecurityView.PROMPT_REASON_AFTER_LOCKOUT;
619             }
620             return KeyguardSecurityView.PROMPT_REASON_NONE;
621         }
622     };
623
624     public void userActivity() {
625         mPM.userActivity(SystemClock.uptimeMillis(), false);
626     }
627
628     boolean mustNotUnlockCurrentUser() {
629         return (UserManager.isSplitSystemUser() || UserManager.isDeviceInDemoMode(mContext))
630                 && KeyguardUpdateMonitor.getCurrentUser() == UserHandle.USER_SYSTEM;
631     }
632
633     private void setupLocked() {
634         mPM = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE);
635         mWM = WindowManagerGlobal.getWindowManagerService();
636         mTrustManager = (TrustManager) mContext.getSystemService(Context.TRUST_SERVICE);
637
638         mShowKeyguardWakeLock = mPM.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "show keyguard");
639         mShowKeyguardWakeLock.setReferenceCounted(false);
640
641         mContext.registerReceiver(mBroadcastReceiver, new IntentFilter(DELAYED_KEYGUARD_ACTION));
642         mContext.registerReceiver(
643                 mBroadcastReceiver, new IntentFilter(DELAYED_LOCK_PROFILE_ACTION));
644
645         mKeyguardDisplayManager = new KeyguardDisplayManager(mContext);
646
647         mAlarmManager = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);
648
649         mUpdateMonitor = KeyguardUpdateMonitor.getInstance(mContext);
650
651         mLockPatternUtils = new LockPatternUtils(mContext);
652         KeyguardUpdateMonitor.setCurrentUser(ActivityManager.getCurrentUser());
653
654         // Assume keyguard is showing (unless it's disabled) until we know for sure...
655         setShowingLocked(!shouldWaitForProvisioning() && !mLockPatternUtils.isLockScreenDisabled(
656                 KeyguardUpdateMonitor.getCurrentUser()));
657         updateInputRestrictedLocked();
658         mTrustManager.reportKeyguardShowingChanged();
659
660         mStatusBarKeyguardViewManager =
661                 SystemUIFactory.getInstance().createStatusBarKeyguardViewManager(mContext,
662                         mViewMediatorCallback, mLockPatternUtils);
663         final ContentResolver cr = mContext.getContentResolver();
664
665         mDeviceInteractive = mPM.isInteractive();
666
667         mLockSounds = new SoundPool(1, AudioManager.STREAM_SYSTEM, 0);
668         String soundPath = Settings.Global.getString(cr, Settings.Global.LOCK_SOUND);
669         if (soundPath != null) {
670             mLockSoundId = mLockSounds.load(soundPath, 1);
671         }
672         if (soundPath == null || mLockSoundId == 0) {
673             Log.w(TAG, "failed to load lock sound from " + soundPath);
674         }
675         soundPath = Settings.Global.getString(cr, Settings.Global.UNLOCK_SOUND);
676         if (soundPath != null) {
677             mUnlockSoundId = mLockSounds.load(soundPath, 1);
678         }
679         if (soundPath == null || mUnlockSoundId == 0) {
680             Log.w(TAG, "failed to load unlock sound from " + soundPath);
681         }
682         soundPath = Settings.Global.getString(cr, Settings.Global.TRUSTED_SOUND);
683         if (soundPath != null) {
684             mTrustedSoundId = mLockSounds.load(soundPath, 1);
685         }
686         if (soundPath == null || mTrustedSoundId == 0) {
687             Log.w(TAG, "failed to load trusted sound from " + soundPath);
688         }
689
690         int lockSoundDefaultAttenuation = mContext.getResources().getInteger(
691                 com.android.internal.R.integer.config_lockSoundVolumeDb);
692         mLockSoundVolume = (float)Math.pow(10, (float)lockSoundDefaultAttenuation/20);
693
694         mHideAnimation = AnimationUtils.loadAnimation(mContext,
695                 com.android.internal.R.anim.lock_screen_behind_enter);
696     }
697
698     @Override
699     public void start() {
700         synchronized (this) {
701             setupLocked();
702         }
703         putComponent(KeyguardViewMediator.class, this);
704     }
705
706     /**
707      * Let us know that the system is ready after startup.
708      */
709     public void onSystemReady() {
710         mSearchManager = (SearchManager) mContext.getSystemService(Context.SEARCH_SERVICE);
711         synchronized (this) {
712             if (DEBUG) Log.d(TAG, "onSystemReady");
713             mSystemReady = true;
714             doKeyguardLocked(null);
715             mUpdateMonitor.registerCallback(mUpdateCallback);
716         }
717         mIsPerUserLock = StorageManager.isFileEncryptedNativeOrEmulated();
718         // Most services aren't available until the system reaches the ready state, so we
719         // send it here when the device first boots.
720         maybeSendUserPresentBroadcast();
721     }
722
723     /**
724      * Called to let us know the screen was turned off.
725      * @param why either {@link android.view.WindowManagerPolicy#OFF_BECAUSE_OF_USER} or
726      *   {@link android.view.WindowManagerPolicy#OFF_BECAUSE_OF_TIMEOUT}.
727      */
728     public void onStartedGoingToSleep(int why) {
729         if (DEBUG) Log.d(TAG, "onStartedGoingToSleep(" + why + ")");
730         synchronized (this) {
731             mDeviceInteractive = false;
732             mGoingToSleep = true;
733
734             // Lock immediately based on setting if secure (user has a pin/pattern/password).
735             // This also "locks" the device when not secure to provide easy access to the
736             // camera while preventing unwanted input.
737             int currentUser = KeyguardUpdateMonitor.getCurrentUser();
738             final boolean lockImmediately =
739                     mLockPatternUtils.getPowerButtonInstantlyLocks(currentUser)
740                             || !mLockPatternUtils.isSecure(currentUser);
741             long timeout = getLockTimeout(KeyguardUpdateMonitor.getCurrentUser());
742             mLockLater = false;
743             if (mExitSecureCallback != null) {
744                 if (DEBUG) Log.d(TAG, "pending exit secure callback cancelled");
745                 try {
746                     mExitSecureCallback.onKeyguardExitResult(false);
747                 } catch (RemoteException e) {
748                     Slog.w(TAG, "Failed to call onKeyguardExitResult(false)", e);
749                 }
750                 mExitSecureCallback = null;
751                 if (!mExternallyEnabled) {
752                     hideLocked();
753                 }
754             } else if (mShowing) {
755                 mPendingReset = true;
756             } else if ((why == WindowManagerPolicy.OFF_BECAUSE_OF_TIMEOUT && timeout > 0)
757                     || (why == WindowManagerPolicy.OFF_BECAUSE_OF_USER && !lockImmediately)) {
758                 doKeyguardLaterLocked(timeout);
759                 mLockLater = true;
760             } else if (!mLockPatternUtils.isLockScreenDisabled(currentUser)) {
761                 mPendingLock = true;
762             }
763
764             if (mPendingLock) {
765                 playSounds(true);
766             }
767         }
768         KeyguardUpdateMonitor.getInstance(mContext).dispatchStartedGoingToSleep(why);
769         notifyStartedGoingToSleep();
770     }
771
772     public void onFinishedGoingToSleep(int why, boolean cameraGestureTriggered) {
773         if (DEBUG) Log.d(TAG, "onFinishedGoingToSleep(" + why + ")");
774         synchronized (this) {
775             mDeviceInteractive = false;
776             mGoingToSleep = false;
777
778             resetKeyguardDonePendingLocked();
779             mHideAnimationRun = false;
780
781             notifyFinishedGoingToSleep();
782
783             if (cameraGestureTriggered) {
784                 Log.i(TAG, "Camera gesture was triggered, preventing Keyguard locking.");
785
786                 // Just to make sure, make sure the device is awake.
787                 mContext.getSystemService(PowerManager.class).wakeUp(SystemClock.uptimeMillis(),
788                         "com.android.systemui:CAMERA_GESTURE_PREVENT_LOCK");
789                 mPendingLock = false;
790                 mPendingReset = false;
791             }
792
793             if (mPendingReset) {
794                 resetStateLocked();
795                 mPendingReset = false;
796             }
797
798             if (mPendingLock) {
799                 doKeyguardLocked(null);
800                 mPendingLock = false;
801             }
802
803             // We do not have timeout and power button instant lock setting for profile lock.
804             // So we use the personal setting if there is any. But if there is no device
805             // we need to make sure we lock it immediately when the screen is off.
806             if (!mLockLater && !cameraGestureTriggered) {
807                 doKeyguardForChildProfilesLocked();
808             }
809
810         }
811         KeyguardUpdateMonitor.getInstance(mContext).dispatchFinishedGoingToSleep(why);
812     }
813
814     private long getLockTimeout(int userId) {
815         // if the screen turned off because of timeout or the user hit the power button
816         // and we don't need to lock immediately, set an alarm
817         // to enable it a little bit later (i.e, give the user a chance
818         // to turn the screen back on within a certain window without
819         // having to unlock the screen)
820         final ContentResolver cr = mContext.getContentResolver();
821
822         // From SecuritySettings
823         final long lockAfterTimeout = Settings.Secure.getInt(cr,
824                 Settings.Secure.LOCK_SCREEN_LOCK_AFTER_TIMEOUT,
825                 KEYGUARD_LOCK_AFTER_DELAY_DEFAULT);
826
827         // From DevicePolicyAdmin
828         final long policyTimeout = mLockPatternUtils.getDevicePolicyManager()
829                 .getMaximumTimeToLockForUserAndProfiles(userId);
830
831         long timeout;
832
833         if (policyTimeout <= 0) {
834             timeout = lockAfterTimeout;
835         } else {
836             // From DisplaySettings
837             long displayTimeout = Settings.System.getInt(cr, SCREEN_OFF_TIMEOUT,
838                     KEYGUARD_DISPLAY_TIMEOUT_DELAY_DEFAULT);
839
840             // policy in effect. Make sure we don't go beyond policy limit.
841             displayTimeout = Math.max(displayTimeout, 0); // ignore negative values
842             timeout = Math.min(policyTimeout - displayTimeout, lockAfterTimeout);
843             timeout = Math.max(timeout, 0);
844         }
845         return timeout;
846     }
847
848     private void doKeyguardLaterLocked() {
849         long timeout = getLockTimeout(KeyguardUpdateMonitor.getCurrentUser());
850         if (timeout == 0) {
851             doKeyguardLocked(null);
852         } else {
853             doKeyguardLaterLocked(timeout);
854         }
855     }
856
857     private void doKeyguardLaterLocked(long timeout) {
858         // Lock in the future
859         long when = SystemClock.elapsedRealtime() + timeout;
860         Intent intent = new Intent(DELAYED_KEYGUARD_ACTION);
861         intent.putExtra("seq", mDelayedShowingSequence);
862         intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
863         PendingIntent sender = PendingIntent.getBroadcast(mContext,
864                 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
865         mAlarmManager.setExactAndAllowWhileIdle(AlarmManager.ELAPSED_REALTIME_WAKEUP, when, sender);
866         if (DEBUG) Log.d(TAG, "setting alarm to turn off keyguard, seq = "
867                          + mDelayedShowingSequence);
868         doKeyguardLaterForChildProfilesLocked();
869     }
870
871     private void doKeyguardLaterForChildProfilesLocked() {
872         UserManager um = UserManager.get(mContext);
873         for (int profileId : um.getEnabledProfileIds(UserHandle.myUserId())) {
874             if (mLockPatternUtils.isSeparateProfileChallengeEnabled(profileId)) {
875                 long userTimeout = getLockTimeout(profileId);
876                 if (userTimeout == 0) {
877                     doKeyguardForChildProfilesLocked();
878                 } else {
879                     long userWhen = SystemClock.elapsedRealtime() + userTimeout;
880                     Intent lockIntent = new Intent(DELAYED_LOCK_PROFILE_ACTION);
881                     lockIntent.putExtra("seq", mDelayedProfileShowingSequence);
882                     lockIntent.putExtra(Intent.EXTRA_USER_ID, profileId);
883                     lockIntent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
884                     PendingIntent lockSender = PendingIntent.getBroadcast(
885                             mContext, 0, lockIntent, PendingIntent.FLAG_CANCEL_CURRENT);
886                     mAlarmManager.setExactAndAllowWhileIdle(AlarmManager.ELAPSED_REALTIME_WAKEUP,
887                             userWhen, lockSender);
888                 }
889             }
890         }
891     }
892
893     private void doKeyguardForChildProfilesLocked() {
894         UserManager um = UserManager.get(mContext);
895         for (int profileId : um.getEnabledProfileIds(UserHandle.myUserId())) {
896             if (mLockPatternUtils.isSeparateProfileChallengeEnabled(profileId)) {
897                 lockProfile(profileId);
898             }
899         }
900     }
901
902     private void cancelDoKeyguardLaterLocked() {
903         mDelayedShowingSequence++;
904     }
905
906     private void cancelDoKeyguardForChildProfilesLocked() {
907         mDelayedProfileShowingSequence++;
908     }
909
910     /**
911      * Let's us know when the device is waking up.
912      */
913     public void onStartedWakingUp() {
914         Trace.beginSection("KeyguardViewMediator#onStartedWakingUp");
915
916         // TODO: Rename all screen off/on references to interactive/sleeping
917         synchronized (this) {
918             mDeviceInteractive = true;
919             cancelDoKeyguardLaterLocked();
920             cancelDoKeyguardForChildProfilesLocked();
921             if (DEBUG) Log.d(TAG, "onStartedWakingUp, seq = " + mDelayedShowingSequence);
922             notifyStartedWakingUp();
923         }
924         KeyguardUpdateMonitor.getInstance(mContext).dispatchStartedWakingUp();
925         maybeSendUserPresentBroadcast();
926         Trace.endSection();
927     }
928
929     public void onScreenTurningOn(IKeyguardDrawnCallback callback) {
930         Trace.beginSection("KeyguardViewMediator#onScreenTurningOn");
931         notifyScreenOn(callback);
932         Trace.endSection();
933     }
934
935     public void onScreenTurnedOn() {
936         Trace.beginSection("KeyguardViewMediator#onScreenTurnedOn");
937         notifyScreenTurnedOn();
938         mUpdateMonitor.dispatchScreenTurnedOn();
939         Trace.endSection();
940     }
941
942     public void onScreenTurnedOff() {
943         notifyScreenTurnedOff();
944         mUpdateMonitor.dispatchScreenTurnedOff();
945     }
946
947     private void maybeSendUserPresentBroadcast() {
948         if (mSystemReady && mLockPatternUtils.isLockScreenDisabled(
949                 KeyguardUpdateMonitor.getCurrentUser())) {
950             // Lock screen is disabled because the user has set the preference to "None".
951             // In this case, send out ACTION_USER_PRESENT here instead of in
952             // handleKeyguardDone()
953             sendUserPresentBroadcast();
954         } else if (mSystemReady && shouldWaitForProvisioning()) {
955             // Skipping the lockscreen because we're not yet provisioned, but we still need to
956             // notify the StrongAuthTracker that it's now safe to run trust agents, in case the
957             // user sets a credential later.
958             getLockPatternUtils().userPresent(KeyguardUpdateMonitor.getCurrentUser());
959         }
960     }
961
962     /**
963      * A dream started.  We should lock after the usual screen-off lock timeout but only
964      * if there is a secure lock pattern.
965      */
966     public void onDreamingStarted() {
967         synchronized (this) {
968             if (mDeviceInteractive
969                     && mLockPatternUtils.isSecure(KeyguardUpdateMonitor.getCurrentUser())) {
970                 doKeyguardLaterLocked();
971             }
972         }
973     }
974
975     /**
976      * A dream stopped.
977      */
978     public void onDreamingStopped() {
979         synchronized (this) {
980             if (mDeviceInteractive) {
981                 cancelDoKeyguardLaterLocked();
982             }
983         }
984     }
985
986     /**
987      * Same semantics as {@link android.view.WindowManagerPolicy#enableKeyguard}; provide
988      * a way for external stuff to override normal keyguard behavior.  For instance
989      * the phone app disables the keyguard when it receives incoming calls.
990      */
991     public void setKeyguardEnabled(boolean enabled) {
992         synchronized (this) {
993             if (DEBUG) Log.d(TAG, "setKeyguardEnabled(" + enabled + ")");
994
995             mExternallyEnabled = enabled;
996
997             if (!enabled && mShowing) {
998                 if (mExitSecureCallback != null) {
999                     if (DEBUG) Log.d(TAG, "in process of verifyUnlock request, ignoring");
1000                     // we're in the process of handling a request to verify the user
1001                     // can get past the keyguard. ignore extraneous requests to disable / reenable
1002                     return;
1003                 }
1004
1005                 // hiding keyguard that is showing, remember to reshow later
1006                 if (DEBUG) Log.d(TAG, "remembering to reshow, hiding keyguard, "
1007                         + "disabling status bar expansion");
1008                 mNeedToReshowWhenReenabled = true;
1009                 updateInputRestrictedLocked();
1010                 hideLocked();
1011             } else if (enabled && mNeedToReshowWhenReenabled) {
1012                 // reenabled after previously hidden, reshow
1013                 if (DEBUG) Log.d(TAG, "previously hidden, reshowing, reenabling "
1014                         + "status bar expansion");
1015                 mNeedToReshowWhenReenabled = false;
1016                 updateInputRestrictedLocked();
1017
1018                 if (mExitSecureCallback != null) {
1019                     if (DEBUG) Log.d(TAG, "onKeyguardExitResult(false), resetting");
1020                     try {
1021                         mExitSecureCallback.onKeyguardExitResult(false);
1022                     } catch (RemoteException e) {
1023                         Slog.w(TAG, "Failed to call onKeyguardExitResult(false)", e);
1024                     }
1025                     mExitSecureCallback = null;
1026                     resetStateLocked();
1027                 } else {
1028                     showLocked(null);
1029
1030                     // block until we know the keygaurd is done drawing (and post a message
1031                     // to unblock us after a timeout so we don't risk blocking too long
1032                     // and causing an ANR).
1033                     mWaitingUntilKeyguardVisible = true;
1034                     mHandler.sendEmptyMessageDelayed(KEYGUARD_DONE_DRAWING, KEYGUARD_DONE_DRAWING_TIMEOUT_MS);
1035                     if (DEBUG) Log.d(TAG, "waiting until mWaitingUntilKeyguardVisible is false");
1036                     while (mWaitingUntilKeyguardVisible) {
1037                         try {
1038                             wait();
1039                         } catch (InterruptedException e) {
1040                             Thread.currentThread().interrupt();
1041                         }
1042                     }
1043                     if (DEBUG) Log.d(TAG, "done waiting for mWaitingUntilKeyguardVisible");
1044                 }
1045             }
1046         }
1047     }
1048
1049     /**
1050      * @see android.app.KeyguardManager#exitKeyguardSecurely
1051      */
1052     public void verifyUnlock(IKeyguardExitCallback callback) {
1053         Trace.beginSection("KeyguardViewMediator#verifyUnlock");
1054         synchronized (this) {
1055             if (DEBUG) Log.d(TAG, "verifyUnlock");
1056             if (shouldWaitForProvisioning()) {
1057                 // don't allow this api when the device isn't provisioned
1058                 if (DEBUG) Log.d(TAG, "ignoring because device isn't provisioned");
1059                 try {
1060                     callback.onKeyguardExitResult(false);
1061                 } catch (RemoteException e) {
1062                     Slog.w(TAG, "Failed to call onKeyguardExitResult(false)", e);
1063                 }
1064             } else if (mExternallyEnabled) {
1065                 // this only applies when the user has externally disabled the
1066                 // keyguard.  this is unexpected and means the user is not
1067                 // using the api properly.
1068                 Log.w(TAG, "verifyUnlock called when not externally disabled");
1069                 try {
1070                     callback.onKeyguardExitResult(false);
1071                 } catch (RemoteException e) {
1072                     Slog.w(TAG, "Failed to call onKeyguardExitResult(false)", e);
1073                 }
1074             } else if (mExitSecureCallback != null) {
1075                 // already in progress with someone else
1076                 try {
1077                     callback.onKeyguardExitResult(false);
1078                 } catch (RemoteException e) {
1079                     Slog.w(TAG, "Failed to call onKeyguardExitResult(false)", e);
1080                 }
1081             } else if (!isSecure()) {
1082
1083                 // Keyguard is not secure, no need to do anything, and we don't need to reshow
1084                 // the Keyguard after the client releases the Keyguard lock.
1085                 mExternallyEnabled = true;
1086                 mNeedToReshowWhenReenabled = false;
1087                 updateInputRestricted();
1088                 try {
1089                     callback.onKeyguardExitResult(true);
1090                 } catch (RemoteException e) {
1091                     Slog.w(TAG, "Failed to call onKeyguardExitResult(false)", e);
1092                 }
1093             } else {
1094
1095                 // Since we prevent apps from hiding the Keyguard if we are secure, this should be
1096                 // a no-op as well.
1097                 try {
1098                     callback.onKeyguardExitResult(false);
1099                 } catch (RemoteException e) {
1100                     Slog.w(TAG, "Failed to call onKeyguardExitResult(false)", e);
1101                 }
1102             }
1103         }
1104         Trace.endSection();
1105     }
1106
1107     /**
1108      * Is the keyguard currently showing and not being force hidden?
1109      */
1110     public boolean isShowingAndNotOccluded() {
1111         return mShowing && !mOccluded;
1112     }
1113
1114     /**
1115      * Notify us when the keyguard is occluded by another window
1116      */
1117     public void setOccluded(boolean isOccluded) {
1118         Trace.beginSection("KeyguardViewMediator#setOccluded");
1119         if (DEBUG) Log.d(TAG, "setOccluded " + isOccluded);
1120         mHandler.removeMessages(SET_OCCLUDED);
1121         Message msg = mHandler.obtainMessage(SET_OCCLUDED, (isOccluded ? 1 : 0), 0);
1122         mHandler.sendMessage(msg);
1123         Trace.endSection();
1124     }
1125
1126     /**
1127      * Handles SET_OCCLUDED message sent by setOccluded()
1128      */
1129     private void handleSetOccluded(boolean isOccluded) {
1130         Trace.beginSection("KeyguardViewMediator#handleSetOccluded");
1131         synchronized (KeyguardViewMediator.this) {
1132             if (mHiding && isOccluded) {
1133                 // We're in the process of going away but WindowManager wants to show a
1134                 // SHOW_WHEN_LOCKED activity instead.
1135                 startKeyguardExitAnimation(0, 0);
1136             }
1137
1138             if (mOccluded != isOccluded) {
1139                 mOccluded = isOccluded;
1140                 mStatusBarKeyguardViewManager.setOccluded(isOccluded);
1141                 updateActivityLockScreenState();
1142                 adjustStatusBarLocked();
1143             }
1144         }
1145         Trace.endSection();
1146     }
1147
1148     /**
1149      * Used by PhoneWindowManager to enable the keyguard due to a user activity timeout.
1150      * This must be safe to call from any thread and with any window manager locks held.
1151      */
1152     public void doKeyguardTimeout(Bundle options) {
1153         mHandler.removeMessages(KEYGUARD_TIMEOUT);
1154         Message msg = mHandler.obtainMessage(KEYGUARD_TIMEOUT, options);
1155         mHandler.sendMessage(msg);
1156     }
1157
1158     /**
1159      * Given the state of the keyguard, is the input restricted?
1160      * Input is restricted when the keyguard is showing, or when the keyguard
1161      * was suppressed by an app that disabled the keyguard or we haven't been provisioned yet.
1162      */
1163     public boolean isInputRestricted() {
1164         return mShowing || mNeedToReshowWhenReenabled;
1165     }
1166
1167     private void updateInputRestricted() {
1168         synchronized (this) {
1169             updateInputRestrictedLocked();
1170         }
1171     }
1172     private void updateInputRestrictedLocked() {
1173         boolean inputRestricted = isInputRestricted();
1174         if (mInputRestricted != inputRestricted) {
1175             mInputRestricted = inputRestricted;
1176             int size = mKeyguardStateCallbacks.size();
1177             for (int i = size - 1; i >= 0; i--) {
1178                 try {
1179                     mKeyguardStateCallbacks.get(i).onInputRestrictedStateChanged(inputRestricted);
1180                 } catch (RemoteException e) {
1181                     Slog.w(TAG, "Failed to call onDeviceProvisioned", e);
1182                     if (e instanceof DeadObjectException) {
1183                         mKeyguardStateCallbacks.remove(i);
1184                     }
1185                 }
1186             }
1187         }
1188     }
1189
1190     /**
1191      * Enable the keyguard if the settings are appropriate.
1192      */
1193     private void doKeyguardLocked(Bundle options) {
1194         // if another app is disabling us, don't show
1195         if (!mExternallyEnabled) {
1196             if (DEBUG) Log.d(TAG, "doKeyguard: not showing because externally disabled");
1197
1198             // note: we *should* set mNeedToReshowWhenReenabled=true here, but that makes
1199             // for an occasional ugly flicker in this situation:
1200             // 1) receive a call with the screen on (no keyguard) or make a call
1201             // 2) screen times out
1202             // 3) user hits key to turn screen back on
1203             // instead, we reenable the keyguard when we know the screen is off and the call
1204             // ends (see the broadcast receiver below)
1205             // TODO: clean this up when we have better support at the window manager level
1206             // for apps that wish to be on top of the keyguard
1207             return;
1208         }
1209
1210         // if the keyguard is already showing, don't bother
1211         if (mStatusBarKeyguardViewManager.isShowing()) {
1212             if (DEBUG) Log.d(TAG, "doKeyguard: not showing because it is already showing");
1213             resetStateLocked();
1214             return;
1215         }
1216
1217         // In split system user mode, we never unlock system user.
1218         if (!mustNotUnlockCurrentUser()
1219                 || !mUpdateMonitor.isDeviceProvisioned()) {
1220
1221             // if the setup wizard hasn't run yet, don't show
1222             final boolean requireSim = !SystemProperties.getBoolean("keyguard.no_require_sim", false);
1223             final boolean absent = SubscriptionManager.isValidSubscriptionId(
1224                     mUpdateMonitor.getNextSubIdForState(IccCardConstants.State.ABSENT));
1225             final boolean disabled = SubscriptionManager.isValidSubscriptionId(
1226                     mUpdateMonitor.getNextSubIdForState(IccCardConstants.State.PERM_DISABLED));
1227             final boolean lockedOrMissing = mUpdateMonitor.isSimPinSecure()
1228                     || ((absent || disabled) && requireSim);
1229
1230             if (!lockedOrMissing && shouldWaitForProvisioning()) {
1231                 if (DEBUG) Log.d(TAG, "doKeyguard: not showing because device isn't provisioned"
1232                         + " and the sim is not locked or missing");
1233                 return;
1234             }
1235
1236             if (mLockPatternUtils.isLockScreenDisabled(KeyguardUpdateMonitor.getCurrentUser())
1237                     && !lockedOrMissing) {
1238                 if (DEBUG) Log.d(TAG, "doKeyguard: not showing because lockscreen is off");
1239                 return;
1240             }
1241
1242             if (mLockPatternUtils.checkVoldPassword(KeyguardUpdateMonitor.getCurrentUser())) {
1243                 if (DEBUG) Log.d(TAG, "Not showing lock screen since just decrypted");
1244                 // Without this, settings is not enabled until the lock screen first appears
1245                 setShowingLocked(false);
1246                 hideLocked();
1247                 mUpdateMonitor.reportSuccessfulStrongAuthUnlockAttempt();
1248                 return;
1249             }
1250         }
1251
1252         if (DEBUG) Log.d(TAG, "doKeyguard: showing the lock screen");
1253         showLocked(options);
1254     }
1255
1256     private void lockProfile(int userId) {
1257         mTrustManager.setDeviceLockedForUser(userId, true);
1258     }
1259
1260     private boolean shouldWaitForProvisioning() {
1261         return !mUpdateMonitor.isDeviceProvisioned() && !isSecure();
1262     }
1263
1264     /**
1265      * Dismiss the keyguard through the security layers.
1266      * @param allowWhileOccluded if true, dismiss the keyguard even if it's currently occluded.
1267      */
1268     public void handleDismiss(boolean allowWhileOccluded) {
1269         if (mShowing && (allowWhileOccluded || !mOccluded)) {
1270             mStatusBarKeyguardViewManager.dismiss();
1271         }
1272     }
1273
1274     public void dismiss(boolean allowWhileOccluded) {
1275         mHandler.obtainMessage(DISMISS, allowWhileOccluded ? 1 : 0, 0).sendToTarget();
1276     }
1277
1278     /**
1279      * Send message to keyguard telling it to reset its state.
1280      * @see #handleReset
1281      */
1282     private void resetStateLocked() {
1283         if (DEBUG) Log.e(TAG, "resetStateLocked");
1284         Message msg = mHandler.obtainMessage(RESET);
1285         mHandler.sendMessage(msg);
1286     }
1287
1288     /**
1289      * Send message to keyguard telling it to verify unlock
1290      * @see #handleVerifyUnlock()
1291      */
1292     private void verifyUnlockLocked() {
1293         if (DEBUG) Log.d(TAG, "verifyUnlockLocked");
1294         mHandler.sendEmptyMessage(VERIFY_UNLOCK);
1295     }
1296
1297     private void notifyStartedGoingToSleep() {
1298         if (DEBUG) Log.d(TAG, "notifyStartedGoingToSleep");
1299         mHandler.sendEmptyMessage(NOTIFY_STARTED_GOING_TO_SLEEP);
1300     }
1301
1302     private void notifyFinishedGoingToSleep() {
1303         if (DEBUG) Log.d(TAG, "notifyFinishedGoingToSleep");
1304         mHandler.sendEmptyMessage(NOTIFY_FINISHED_GOING_TO_SLEEP);
1305     }
1306
1307     private void notifyStartedWakingUp() {
1308         if (DEBUG) Log.d(TAG, "notifyStartedWakingUp");
1309         mHandler.sendEmptyMessage(NOTIFY_STARTED_WAKING_UP);
1310     }
1311
1312     private void notifyScreenOn(IKeyguardDrawnCallback callback) {
1313         if (DEBUG) Log.d(TAG, "notifyScreenOn");
1314         Message msg = mHandler.obtainMessage(NOTIFY_SCREEN_TURNING_ON, callback);
1315         mHandler.sendMessage(msg);
1316     }
1317
1318     private void notifyScreenTurnedOn() {
1319         if (DEBUG) Log.d(TAG, "notifyScreenTurnedOn");
1320         Message msg = mHandler.obtainMessage(NOTIFY_SCREEN_TURNED_ON);
1321         mHandler.sendMessage(msg);
1322     }
1323
1324     private void notifyScreenTurnedOff() {
1325         if (DEBUG) Log.d(TAG, "notifyScreenTurnedOff");
1326         Message msg = mHandler.obtainMessage(NOTIFY_SCREEN_TURNED_OFF);
1327         mHandler.sendMessage(msg);
1328     }
1329
1330     /**
1331      * Send message to keyguard telling it to show itself
1332      * @see #handleShow
1333      */
1334     private void showLocked(Bundle options) {
1335         Trace.beginSection("KeyguardViewMediator#showLocked aqcuiring mShowKeyguardWakeLock");
1336         if (DEBUG) Log.d(TAG, "showLocked");
1337         // ensure we stay awake until we are finished displaying the keyguard
1338         mShowKeyguardWakeLock.acquire();
1339         Message msg = mHandler.obtainMessage(SHOW, options);
1340         mHandler.sendMessage(msg);
1341         Trace.endSection();
1342     }
1343
1344     /**
1345      * Send message to keyguard telling it to hide itself
1346      * @see #handleHide()
1347      */
1348     private void hideLocked() {
1349         Trace.beginSection("KeyguardViewMediator#hideLocked");
1350         if (DEBUG) Log.d(TAG, "hideLocked");
1351         Message msg = mHandler.obtainMessage(HIDE);
1352         mHandler.sendMessage(msg);
1353         Trace.endSection();
1354     }
1355
1356     public boolean isSecure() {
1357         return mLockPatternUtils.isSecure(KeyguardUpdateMonitor.getCurrentUser())
1358             || KeyguardUpdateMonitor.getInstance(mContext).isSimPinSecure();
1359     }
1360
1361     /**
1362      * Update the newUserId. Call while holding WindowManagerService lock.
1363      * NOTE: Should only be called by KeyguardViewMediator in response to the user id changing.
1364      *
1365      * @param newUserId The id of the incoming user.
1366      */
1367     public void setCurrentUser(int newUserId) {
1368         KeyguardUpdateMonitor.setCurrentUser(newUserId);
1369         synchronized (this) {
1370             notifyTrustedChangedLocked(mUpdateMonitor.getUserHasTrust(newUserId));
1371         }
1372     }
1373
1374     private final BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
1375         @Override
1376         public void onReceive(Context context, Intent intent) {
1377             if (DELAYED_KEYGUARD_ACTION.equals(intent.getAction())) {
1378                 final int sequence = intent.getIntExtra("seq", 0);
1379                 if (DEBUG) Log.d(TAG, "received DELAYED_KEYGUARD_ACTION with seq = "
1380                         + sequence + ", mDelayedShowingSequence = " + mDelayedShowingSequence);
1381                 synchronized (KeyguardViewMediator.this) {
1382                     if (mDelayedShowingSequence == sequence) {
1383                         doKeyguardLocked(null);
1384                     }
1385                 }
1386             } else if (DELAYED_LOCK_PROFILE_ACTION.equals(intent.getAction())) {
1387                 final int sequence = intent.getIntExtra("seq", 0);
1388                 int userId = intent.getIntExtra(Intent.EXTRA_USER_ID, 0);
1389                 if (userId != 0) {
1390                     synchronized (KeyguardViewMediator.this) {
1391                         if (mDelayedProfileShowingSequence == sequence) {
1392                             lockProfile(userId);
1393                         }
1394                     }
1395                 }
1396             }
1397         }
1398     };
1399
1400     public void keyguardDone(boolean authenticated) {
1401         Trace.beginSection("KeyguardViewMediator#keyguardDone");
1402         if (DEBUG) Log.d(TAG, "keyguardDone(" + authenticated +")");
1403         userActivity();
1404         EventLog.writeEvent(70000, 2);
1405         Message msg = mHandler.obtainMessage(KEYGUARD_DONE, authenticated ? 1 : 0);
1406         mHandler.sendMessage(msg);
1407         Trace.endSection();
1408     }
1409
1410     /**
1411      * This handler will be associated with the policy thread, which will also
1412      * be the UI thread of the keyguard.  Since the apis of the policy, and therefore
1413      * this class, can be called by other threads, any action that directly
1414      * interacts with the keyguard ui should be posted to this handler, rather
1415      * than called directly.
1416      */
1417     private Handler mHandler = new Handler(Looper.myLooper(), null, true /*async*/) {
1418         @Override
1419         public void handleMessage(Message msg) {
1420             switch (msg.what) {
1421                 case SHOW:
1422                     handleShow((Bundle) msg.obj);
1423                     break;
1424                 case HIDE:
1425                     handleHide();
1426                     break;
1427                 case RESET:
1428                     handleReset();
1429                     break;
1430                 case VERIFY_UNLOCK:
1431                     Trace.beginSection("KeyguardViewMediator#handleMessage VERIFY_UNLOCK");
1432                     handleVerifyUnlock();
1433                     Trace.endSection();
1434                     break;
1435                 case NOTIFY_STARTED_GOING_TO_SLEEP:
1436                     handleNotifyStartedGoingToSleep();
1437                     break;
1438                 case NOTIFY_FINISHED_GOING_TO_SLEEP:
1439                     handleNotifyFinishedGoingToSleep();
1440                     break;
1441                 case NOTIFY_SCREEN_TURNING_ON:
1442                     Trace.beginSection("KeyguardViewMediator#handleMessage NOTIFY_SCREEN_TURNING_ON");
1443                     handleNotifyScreenTurningOn((IKeyguardDrawnCallback) msg.obj);
1444                     Trace.endSection();
1445                     break;
1446                 case NOTIFY_SCREEN_TURNED_ON:
1447                     Trace.beginSection("KeyguardViewMediator#handleMessage NOTIFY_SCREEN_TURNED_ON");
1448                     handleNotifyScreenTurnedOn();
1449                     Trace.endSection();
1450                     break;
1451                 case NOTIFY_SCREEN_TURNED_OFF:
1452                     handleNotifyScreenTurnedOff();
1453                     break;
1454                 case NOTIFY_STARTED_WAKING_UP:
1455                     Trace.beginSection("KeyguardViewMediator#handleMessage NOTIFY_STARTED_WAKING_UP");
1456                     handleNotifyStartedWakingUp();
1457                     Trace.endSection();
1458                     break;
1459                 case KEYGUARD_DONE:
1460                     Trace.beginSection("KeyguardViewMediator#handleMessage KEYGUARD_DONE");
1461                     handleKeyguardDone(msg.arg1 != 0);
1462                     Trace.endSection();
1463                     break;
1464                 case KEYGUARD_DONE_DRAWING:
1465                     Trace.beginSection("KeyguardViewMediator#handleMessage KEYGUARD_DONE_DRAWING");
1466                     handleKeyguardDoneDrawing();
1467                     Trace.endSection();
1468                     break;
1469                 case SET_OCCLUDED:
1470                     Trace.beginSection("KeyguardViewMediator#handleMessage SET_OCCLUDED");
1471                     handleSetOccluded(msg.arg1 != 0);
1472                     Trace.endSection();
1473                     break;
1474                 case KEYGUARD_TIMEOUT:
1475                     synchronized (KeyguardViewMediator.this) {
1476                         doKeyguardLocked((Bundle) msg.obj);
1477                     }
1478                     break;
1479                 case DISMISS:
1480                     handleDismiss(msg.arg1 == 1 ? true : false /* allowWhileOccluded */);
1481                     break;
1482                 case START_KEYGUARD_EXIT_ANIM:
1483                     Trace.beginSection("KeyguardViewMediator#handleMessage START_KEYGUARD_EXIT_ANIM");
1484                     StartKeyguardExitAnimParams params = (StartKeyguardExitAnimParams) msg.obj;
1485                     handleStartKeyguardExitAnimation(params.startTime, params.fadeoutDuration);
1486                     FalsingManager.getInstance(mContext).onSucccessfulUnlock();
1487                     Trace.endSection();
1488                     break;
1489                 case KEYGUARD_DONE_PENDING_TIMEOUT:
1490                     Trace.beginSection("KeyguardViewMediator#handleMessage KEYGUARD_DONE_PENDING_TIMEOUT");
1491                     Log.w(TAG, "Timeout while waiting for activity drawn!");
1492                     Trace.endSection();
1493                     // Fall through.
1494                 case ON_ACTIVITY_DRAWN:
1495                     handleOnActivityDrawn();
1496                     break;
1497             }
1498         }
1499     };
1500
1501     /**
1502      * @see #keyguardDone
1503      * @see #KEYGUARD_DONE
1504      */
1505     private void handleKeyguardDone(boolean authenticated) {
1506         Trace.beginSection("KeyguardViewMediator#handleKeyguardDone");
1507         final int currentUser = KeyguardUpdateMonitor.getCurrentUser();
1508         if (mLockPatternUtils.isSecure(currentUser)) {
1509             mLockPatternUtils.getDevicePolicyManager().reportKeyguardDismissed(currentUser);
1510         }
1511         if (DEBUG) Log.d(TAG, "handleKeyguardDone");
1512         synchronized (this) {
1513             resetKeyguardDonePendingLocked();
1514         }
1515
1516         if (authenticated) {
1517             mUpdateMonitor.clearFailedUnlockAttempts();
1518         }
1519         mUpdateMonitor.clearFingerprintRecognized();
1520
1521         if (mGoingToSleep) {
1522             Log.i(TAG, "Device is going to sleep, aborting keyguardDone");
1523             return;
1524         }
1525         if (mExitSecureCallback != null) {
1526             try {
1527                 mExitSecureCallback.onKeyguardExitResult(authenticated);
1528             } catch (RemoteException e) {
1529                 Slog.w(TAG, "Failed to call onKeyguardExitResult(" + authenticated + ")", e);
1530             }
1531
1532             mExitSecureCallback = null;
1533
1534             if (authenticated) {
1535                 // after succesfully exiting securely, no need to reshow
1536                 // the keyguard when they've released the lock
1537                 mExternallyEnabled = true;
1538                 mNeedToReshowWhenReenabled = false;
1539                 updateInputRestricted();
1540             }
1541         }
1542
1543         handleHide();
1544         Trace.endSection();
1545     }
1546
1547     private void sendUserPresentBroadcast() {
1548         synchronized (this) {
1549             if (mBootCompleted) {
1550                 int currentUserId = KeyguardUpdateMonitor.getCurrentUser();
1551                 final UserHandle currentUser = new UserHandle(currentUserId);
1552                 final UserManager um = (UserManager) mContext.getSystemService(
1553                         Context.USER_SERVICE);
1554                 for (int profileId : um.getProfileIdsWithDisabled(currentUser.getIdentifier())) {
1555                     mContext.sendBroadcastAsUser(USER_PRESENT_INTENT, UserHandle.of(profileId));
1556                 }
1557                 getLockPatternUtils().userPresent(currentUserId);
1558             } else {
1559                 mBootSendUserPresent = true;
1560             }
1561         }
1562     }
1563
1564     /**
1565      * @see #keyguardDone
1566      * @see #KEYGUARD_DONE_DRAWING
1567      */
1568     private void handleKeyguardDoneDrawing() {
1569         Trace.beginSection("KeyguardViewMediator#handleKeyguardDoneDrawing");
1570         synchronized(this) {
1571             if (DEBUG) Log.d(TAG, "handleKeyguardDoneDrawing");
1572             if (mWaitingUntilKeyguardVisible) {
1573                 if (DEBUG) Log.d(TAG, "handleKeyguardDoneDrawing: notifying mWaitingUntilKeyguardVisible");
1574                 mWaitingUntilKeyguardVisible = false;
1575                 notifyAll();
1576
1577                 // there will usually be two of these sent, one as a timeout, and one
1578                 // as a result of the callback, so remove any remaining messages from
1579                 // the queue
1580                 mHandler.removeMessages(KEYGUARD_DONE_DRAWING);
1581             }
1582         }
1583         Trace.endSection();
1584     }
1585
1586     private void playSounds(boolean locked) {
1587         playSound(locked ? mLockSoundId : mUnlockSoundId);
1588     }
1589
1590     private void playSound(int soundId) {
1591         if (soundId == 0) return;
1592         final ContentResolver cr = mContext.getContentResolver();
1593         if (Settings.System.getInt(cr, Settings.System.LOCKSCREEN_SOUNDS_ENABLED, 1) == 1) {
1594
1595             mLockSounds.stop(mLockSoundStreamId);
1596             // Init mAudioManager
1597             if (mAudioManager == null) {
1598                 mAudioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
1599                 if (mAudioManager == null) return;
1600                 mUiSoundsStreamType = mAudioManager.getUiSoundsStreamType();
1601             }
1602             // If the stream is muted, don't play the sound
1603             if (mAudioManager.isStreamMute(mUiSoundsStreamType)) return;
1604
1605             mLockSoundStreamId = mLockSounds.play(soundId,
1606                     mLockSoundVolume, mLockSoundVolume, 1/*priortiy*/, 0/*loop*/, 1.0f/*rate*/);
1607         }
1608     }
1609
1610     private void playTrustedSound() {
1611         playSound(mTrustedSoundId);
1612     }
1613
1614     private void updateActivityLockScreenState() {
1615         Trace.beginSection("KeyguardViewMediator#updateActivityLockScreenState");
1616         try {
1617             ActivityManagerNative.getDefault().setLockScreenShown(mShowing, mOccluded);
1618         } catch (RemoteException e) {
1619         }
1620         Trace.endSection();
1621     }
1622
1623     /**
1624      * Handle message sent by {@link #showLocked}.
1625      * @see #SHOW
1626      */
1627     private void handleShow(Bundle options) {
1628         Trace.beginSection("KeyguardViewMediator#handleShow");
1629         final int currentUser = KeyguardUpdateMonitor.getCurrentUser();
1630         if (mLockPatternUtils.isSecure(currentUser)) {
1631             mLockPatternUtils.getDevicePolicyManager().reportKeyguardSecured(currentUser);
1632         }
1633         synchronized (KeyguardViewMediator.this) {
1634             if (!mSystemReady) {
1635                 if (DEBUG) Log.d(TAG, "ignoring handleShow because system is not ready.");
1636                 return;
1637             } else {
1638                 if (DEBUG) Log.d(TAG, "handleShow");
1639             }
1640
1641             setShowingLocked(true);
1642             mStatusBarKeyguardViewManager.show(options);
1643             mHiding = false;
1644             mWakeAndUnlocking = false;
1645             resetKeyguardDonePendingLocked();
1646             mHideAnimationRun = false;
1647             updateActivityLockScreenState();
1648             adjustStatusBarLocked();
1649             userActivity();
1650
1651             mShowKeyguardWakeLock.release();
1652         }
1653         mKeyguardDisplayManager.show();
1654         Trace.endSection();
1655     }
1656
1657     private final Runnable mKeyguardGoingAwayRunnable = new Runnable() {
1658         @Override
1659         public void run() {
1660             Trace.beginSection("KeyguardViewMediator.mKeyGuardGoingAwayRunnable");
1661             if (DEBUG) Log.d(TAG, "keyguardGoingAway");
1662             try {
1663                 mStatusBarKeyguardViewManager.keyguardGoingAway();
1664
1665                 int flags = 0;
1666                 if (mStatusBarKeyguardViewManager.shouldDisableWindowAnimationsForUnlock()
1667                         || mWakeAndUnlocking) {
1668                     flags |= WindowManagerPolicy.KEYGUARD_GOING_AWAY_FLAG_NO_WINDOW_ANIMATIONS;
1669                 }
1670                 if (mStatusBarKeyguardViewManager.isGoingToNotificationShade()) {
1671                     flags |= WindowManagerPolicy.KEYGUARD_GOING_AWAY_FLAG_TO_SHADE;
1672                 }
1673                 if (mStatusBarKeyguardViewManager.isUnlockWithWallpaper()) {
1674                     flags |= WindowManagerPolicy.KEYGUARD_GOING_AWAY_FLAG_WITH_WALLPAPER;
1675                 }
1676
1677                 // Don't actually hide the Keyguard at the moment, wait for window
1678                 // manager until it tells us it's safe to do so with
1679                 // startKeyguardExitAnimation.
1680                 ActivityManagerNative.getDefault().keyguardGoingAway(flags);
1681             } catch (RemoteException e) {
1682                 Log.e(TAG, "Error while calling WindowManager", e);
1683             }
1684             Trace.endSection();
1685         }
1686     };
1687
1688     /**
1689      * Handle message sent by {@link #hideLocked()}
1690      * @see #HIDE
1691      */
1692     private void handleHide() {
1693         Trace.beginSection("KeyguardViewMediator#handleHide");
1694         synchronized (KeyguardViewMediator.this) {
1695             if (DEBUG) Log.d(TAG, "handleHide");
1696
1697             if (mustNotUnlockCurrentUser()) {
1698                 // In split system user mode, we never unlock system user. The end user has to
1699                 // switch to another user.
1700                 // TODO: We should stop it early by disabling the swipe up flow. Right now swipe up
1701                 // still completes and makes the screen blank.
1702                 if (DEBUG) Log.d(TAG, "Split system user, quit unlocking.");
1703                 return;
1704             }
1705             mHiding = true;
1706             if (mShowing && !mOccluded) {
1707                 if (!mHideAnimationRun) {
1708                     mStatusBarKeyguardViewManager.startPreHideAnimation(mKeyguardGoingAwayRunnable);
1709                 } else {
1710                     mKeyguardGoingAwayRunnable.run();
1711                 }
1712             } else {
1713
1714                 // Don't try to rely on WindowManager - if Keyguard wasn't showing, window
1715                 // manager won't start the exit animation.
1716                 handleStartKeyguardExitAnimation(
1717                         SystemClock.uptimeMillis() + mHideAnimation.getStartOffset(),
1718                         mHideAnimation.getDuration());
1719             }
1720         }
1721         Trace.endSection();
1722     }
1723
1724     private void handleOnActivityDrawn() {
1725         if (DEBUG) Log.d(TAG, "handleOnActivityDrawn: mKeyguardDonePending=" + mKeyguardDonePending);
1726         if (mKeyguardDonePending) {
1727             mStatusBarKeyguardViewManager.onActivityDrawn();
1728         }
1729     }
1730
1731     private void handleStartKeyguardExitAnimation(long startTime, long fadeoutDuration) {
1732         Trace.beginSection("KeyguardViewMediator#handleStartKeyguardExitAnimation");
1733         if (DEBUG) Log.d(TAG, "handleStartKeyguardExitAnimation startTime=" + startTime
1734                 + " fadeoutDuration=" + fadeoutDuration);
1735         synchronized (KeyguardViewMediator.this) {
1736
1737             if (!mHiding) {
1738                 return;
1739             }
1740             mHiding = false;
1741
1742             if (mWakeAndUnlocking && mDrawnCallback != null) {
1743
1744                 // Hack level over 9000: To speed up wake-and-unlock sequence, force it to report
1745                 // the next draw from here so we don't have to wait for window manager to signal
1746                 // this to our ViewRootImpl.
1747                 mStatusBarKeyguardViewManager.getViewRootImpl().setReportNextDraw();
1748                 notifyDrawn(mDrawnCallback);
1749                 mDrawnCallback = null;
1750             }
1751
1752             // only play "unlock" noises if not on a call (since the incall UI
1753             // disables the keyguard)
1754             if (TelephonyManager.EXTRA_STATE_IDLE.equals(mPhoneState)) {
1755                 playSounds(false);
1756             }
1757
1758             mWakeAndUnlocking = false;
1759             setShowingLocked(false);
1760             mStatusBarKeyguardViewManager.hide(startTime, fadeoutDuration);
1761             resetKeyguardDonePendingLocked();
1762             mHideAnimationRun = false;
1763             updateActivityLockScreenState();
1764             adjustStatusBarLocked();
1765             sendUserPresentBroadcast();
1766         }
1767         Trace.endSection();
1768     }
1769
1770     private void adjustStatusBarLocked() {
1771         if (mStatusBarManager == null) {
1772             mStatusBarManager = (StatusBarManager)
1773                     mContext.getSystemService(Context.STATUS_BAR_SERVICE);
1774         }
1775         if (mStatusBarManager == null) {
1776             Log.w(TAG, "Could not get status bar manager");
1777         } else {
1778             // Disable aspects of the system/status/navigation bars that must not be re-enabled by
1779             // windows that appear on top, ever
1780             int flags = StatusBarManager.DISABLE_NONE;
1781             if (mShowing) {
1782                 // Permanently disable components not available when keyguard is enabled
1783                 // (like recents). Temporary enable/disable (e.g. the "back" button) are
1784                 // done in KeyguardHostView.
1785                 flags |= StatusBarManager.DISABLE_RECENT;
1786                 flags |= StatusBarManager.DISABLE_SEARCH;
1787             }
1788             if (isShowingAndNotOccluded()) {
1789                 flags |= StatusBarManager.DISABLE_HOME;
1790             }
1791
1792             if (DEBUG) {
1793                 Log.d(TAG, "adjustStatusBarLocked: mShowing=" + mShowing + " mOccluded=" + mOccluded
1794                         + " isSecure=" + isSecure() + " --> flags=0x" + Integer.toHexString(flags));
1795             }
1796
1797             if (!(mContext instanceof Activity)) {
1798                 mStatusBarManager.disable(flags);
1799             }
1800         }
1801     }
1802
1803     /**
1804      * Handle message sent by {@link #resetStateLocked}
1805      * @see #RESET
1806      */
1807     private void handleReset() {
1808         synchronized (KeyguardViewMediator.this) {
1809             if (DEBUG) Log.d(TAG, "handleReset");
1810             mStatusBarKeyguardViewManager.reset();
1811         }
1812     }
1813
1814     /**
1815      * Handle message sent by {@link #verifyUnlock}
1816      * @see #VERIFY_UNLOCK
1817      */
1818     private void handleVerifyUnlock() {
1819         Trace.beginSection("KeyguardViewMediator#handleVerifyUnlock");
1820         synchronized (KeyguardViewMediator.this) {
1821             if (DEBUG) Log.d(TAG, "handleVerifyUnlock");
1822             setShowingLocked(true);
1823             mStatusBarKeyguardViewManager.verifyUnlock();
1824             updateActivityLockScreenState();
1825         }
1826         Trace.endSection();
1827     }
1828
1829     private void handleNotifyStartedGoingToSleep() {
1830         synchronized (KeyguardViewMediator.this) {
1831             if (DEBUG) Log.d(TAG, "handleNotifyStartedGoingToSleep");
1832             mStatusBarKeyguardViewManager.onStartedGoingToSleep();
1833         }
1834     }
1835
1836     /**
1837      * Handle message sent by {@link #notifyFinishedGoingToSleep()}
1838      * @see #NOTIFY_FINISHED_GOING_TO_SLEEP
1839      */
1840     private void handleNotifyFinishedGoingToSleep() {
1841         synchronized (KeyguardViewMediator.this) {
1842             if (DEBUG) Log.d(TAG, "handleNotifyFinishedGoingToSleep");
1843             mStatusBarKeyguardViewManager.onFinishedGoingToSleep();
1844         }
1845     }
1846
1847     private void handleNotifyStartedWakingUp() {
1848         Trace.beginSection("KeyguardViewMediator#handleMotifyStartedWakingUp");
1849         synchronized (KeyguardViewMediator.this) {
1850             if (DEBUG) Log.d(TAG, "handleNotifyWakingUp");
1851             mStatusBarKeyguardViewManager.onStartedWakingUp();
1852         }
1853         Trace.endSection();
1854     }
1855
1856     private void handleNotifyScreenTurningOn(IKeyguardDrawnCallback callback) {
1857         Trace.beginSection("KeyguardViewMediator#handleNotifyScreenTurningOn");
1858         synchronized (KeyguardViewMediator.this) {
1859             if (DEBUG) Log.d(TAG, "handleNotifyScreenTurningOn");
1860             mStatusBarKeyguardViewManager.onScreenTurningOn();
1861             if (callback != null) {
1862                 if (mWakeAndUnlocking) {
1863                     mDrawnCallback = callback;
1864                 } else {
1865                     notifyDrawn(callback);
1866                 }
1867             }
1868         }
1869         Trace.endSection();
1870     }
1871
1872     private void handleNotifyScreenTurnedOn() {
1873         Trace.beginSection("KeyguardViewMediator#handleNotifyScreenTurnedOn");
1874         synchronized (this) {
1875             if (DEBUG) Log.d(TAG, "handleNotifyScreenTurnedOn");
1876             mStatusBarKeyguardViewManager.onScreenTurnedOn();
1877         }
1878         Trace.endSection();
1879     }
1880
1881     private void handleNotifyScreenTurnedOff() {
1882         synchronized (this) {
1883             if (DEBUG) Log.d(TAG, "handleNotifyScreenTurnedOff");
1884             mStatusBarKeyguardViewManager.onScreenTurnedOff();
1885             mDrawnCallback = null;
1886             mWakeAndUnlocking = false;
1887         }
1888     }
1889
1890     private void notifyDrawn(final IKeyguardDrawnCallback callback) {
1891         Trace.beginSection("KeyguardViewMediator#notifyDrawn");
1892         try {
1893             callback.onDrawn();
1894         } catch (RemoteException e) {
1895             Slog.w(TAG, "Exception calling onDrawn():", e);
1896         }
1897         Trace.endSection();
1898     }
1899
1900     private void resetKeyguardDonePendingLocked() {
1901         mKeyguardDonePending = false;
1902         mHandler.removeMessages(KEYGUARD_DONE_PENDING_TIMEOUT);
1903     }
1904
1905     @Override
1906     public void onBootCompleted() {
1907         mUpdateMonitor.dispatchBootCompleted();
1908         synchronized (this) {
1909             mBootCompleted = true;
1910             if (mBootSendUserPresent) {
1911                 sendUserPresentBroadcast();
1912             }
1913         }
1914     }
1915
1916     public void onWakeAndUnlocking() {
1917         Trace.beginSection("KeyguardViewMediator#onWakeAndUnlocking");
1918         mWakeAndUnlocking = true;
1919         keyguardDone(true /* authenticated */);
1920         Trace.endSection();
1921     }
1922
1923     public StatusBarKeyguardViewManager registerStatusBar(PhoneStatusBar phoneStatusBar,
1924             ViewGroup container, StatusBarWindowManager statusBarWindowManager,
1925             ScrimController scrimController,
1926             FingerprintUnlockController fingerprintUnlockController) {
1927         mStatusBarKeyguardViewManager.registerStatusBar(phoneStatusBar, container,
1928                 statusBarWindowManager, scrimController, fingerprintUnlockController);
1929         return mStatusBarKeyguardViewManager;
1930     }
1931
1932     public void startKeyguardExitAnimation(long startTime, long fadeoutDuration) {
1933         Trace.beginSection("KeyguardViewMediator#startKeyguardExitAnimation");
1934         Message msg = mHandler.obtainMessage(START_KEYGUARD_EXIT_ANIM,
1935                 new StartKeyguardExitAnimParams(startTime, fadeoutDuration));
1936         mHandler.sendMessage(msg);
1937         Trace.endSection();
1938     }
1939
1940     public void onActivityDrawn() {
1941         mHandler.sendEmptyMessage(ON_ACTIVITY_DRAWN);
1942     }
1943
1944     public ViewMediatorCallback getViewMediatorCallback() {
1945         return mViewMediatorCallback;
1946     }
1947
1948     public LockPatternUtils getLockPatternUtils() {
1949         return mLockPatternUtils;
1950     }
1951
1952     @Override
1953     public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1954         pw.print("  mSystemReady: "); pw.println(mSystemReady);
1955         pw.print("  mBootCompleted: "); pw.println(mBootCompleted);
1956         pw.print("  mBootSendUserPresent: "); pw.println(mBootSendUserPresent);
1957         pw.print("  mExternallyEnabled: "); pw.println(mExternallyEnabled);
1958         pw.print("  mNeedToReshowWhenReenabled: "); pw.println(mNeedToReshowWhenReenabled);
1959         pw.print("  mShowing: "); pw.println(mShowing);
1960         pw.print("  mInputRestricted: "); pw.println(mInputRestricted);
1961         pw.print("  mOccluded: "); pw.println(mOccluded);
1962         pw.print("  mDelayedShowingSequence: "); pw.println(mDelayedShowingSequence);
1963         pw.print("  mExitSecureCallback: "); pw.println(mExitSecureCallback);
1964         pw.print("  mDeviceInteractive: "); pw.println(mDeviceInteractive);
1965         pw.print("  mGoingToSleep: "); pw.println(mGoingToSleep);
1966         pw.print("  mHiding: "); pw.println(mHiding);
1967         pw.print("  mWaitingUntilKeyguardVisible: "); pw.println(mWaitingUntilKeyguardVisible);
1968         pw.print("  mKeyguardDonePending: "); pw.println(mKeyguardDonePending);
1969         pw.print("  mHideAnimationRun: "); pw.println(mHideAnimationRun);
1970         pw.print("  mPendingReset: "); pw.println(mPendingReset);
1971         pw.print("  mPendingLock: "); pw.println(mPendingLock);
1972         pw.print("  mWakeAndUnlocking: "); pw.println(mWakeAndUnlocking);
1973         pw.print("  mDrawnCallback: "); pw.println(mDrawnCallback);
1974     }
1975
1976     private static class StartKeyguardExitAnimParams {
1977
1978         long startTime;
1979         long fadeoutDuration;
1980
1981         private StartKeyguardExitAnimParams(long startTime, long fadeoutDuration) {
1982             this.startTime = startTime;
1983             this.fadeoutDuration = fadeoutDuration;
1984         }
1985     }
1986
1987     private void setShowingLocked(boolean showing) {
1988         if (showing != mShowing) {
1989             mShowing = showing;
1990             int size = mKeyguardStateCallbacks.size();
1991             for (int i = size - 1; i >= 0; i--) {
1992                 try {
1993                     mKeyguardStateCallbacks.get(i).onShowingStateChanged(showing);
1994                 } catch (RemoteException e) {
1995                     Slog.w(TAG, "Failed to call onShowingStateChanged", e);
1996                     if (e instanceof DeadObjectException) {
1997                         mKeyguardStateCallbacks.remove(i);
1998                     }
1999                 }
2000             }
2001             updateInputRestrictedLocked();
2002             mTrustManager.reportKeyguardShowingChanged();
2003         }
2004     }
2005
2006     private void notifyTrustedChangedLocked(boolean trusted) {
2007         int size = mKeyguardStateCallbacks.size();
2008         for (int i = size - 1; i >= 0; i--) {
2009             try {
2010                 mKeyguardStateCallbacks.get(i).onTrustedChanged(trusted);
2011             } catch (RemoteException e) {
2012                 Slog.w(TAG, "Failed to call notifyTrustedChangedLocked", e);
2013                 if (e instanceof DeadObjectException) {
2014                     mKeyguardStateCallbacks.remove(i);
2015                 }
2016             }
2017         }
2018     }
2019
2020     private void notifyHasLockscreenWallpaperChanged(boolean hasLockscreenWallpaper) {
2021         int size = mKeyguardStateCallbacks.size();
2022         for (int i = size - 1; i >= 0; i--) {
2023             try {
2024                 mKeyguardStateCallbacks.get(i).onHasLockscreenWallpaperChanged(
2025                         hasLockscreenWallpaper);
2026             } catch (RemoteException e) {
2027                 Slog.w(TAG, "Failed to call onHasLockscreenWallpaperChanged", e);
2028                 if (e instanceof DeadObjectException) {
2029                     mKeyguardStateCallbacks.remove(i);
2030                 }
2031             }
2032         }
2033     }
2034
2035     public void addStateMonitorCallback(IKeyguardStateCallback callback) {
2036         synchronized (this) {
2037             mKeyguardStateCallbacks.add(callback);
2038             try {
2039                 callback.onSimSecureStateChanged(mUpdateMonitor.isSimPinSecure());
2040                 callback.onShowingStateChanged(mShowing);
2041                 callback.onInputRestrictedStateChanged(mInputRestricted);
2042                 callback.onTrustedChanged(mUpdateMonitor.getUserHasTrust(
2043                         KeyguardUpdateMonitor.getCurrentUser()));
2044                 callback.onHasLockscreenWallpaperChanged(mUpdateMonitor.hasLockscreenWallpaper());
2045             } catch (RemoteException e) {
2046                 Slog.w(TAG, "Failed to call to IKeyguardStateCallback", e);
2047             }
2048         }
2049     }
2050 }