blob: ffd76a7d5367d39817170c9efe96f817668c3417 [file] [log] [blame]
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001/*
Jorim Jaggi5cf17872014-03-26 18:31:48 +01002 * Copyright (C) 2014 The Android Open Source Project
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08003 *
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
Jorim Jaggi5cf17872014-03-26 18:31:48 +010014 * limitations under the License
The Android Open Source Project1f838aa2009-03-03 19:32:13 -080015 */
16
Jorim Jaggi5cf17872014-03-26 18:31:48 +010017package com.android.systemui.keyguard;
Jim Millerbc4603b2010-08-30 21:21:34 -070018
Jim Millerdcb3d842012-08-23 19:18:12 -070019import android.app.Activity;
Dianne Hackborn4994c662009-09-23 22:21:23 -070020import android.app.ActivityManagerNative;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -080021import android.app.AlarmManager;
22import android.app.PendingIntent;
John Spurlock43d84512012-11-09 10:27:33 -050023import android.app.SearchManager;
Mike Lockwood5f892c12009-11-19 23:39:13 -050024import android.app.StatusBarManager;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -080025import android.content.BroadcastReceiver;
Daniel Sandlerdb783bd2010-02-11 15:27:37 -050026import android.content.ContentResolver;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -080027import android.content.Context;
28import android.content.Intent;
29import android.content.IntentFilter;
Daniel Sandleraec967a2010-02-20 01:05:22 -050030import android.media.AudioManager;
Marco Nelissend5545bd2011-09-29 12:49:17 -070031import android.media.SoundPool;
Adrian Roos58b58fe2014-01-31 16:00:39 -080032import android.os.Build;
Adam Cohenf7522022012-10-03 20:03:18 -070033import android.os.Bundle;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -080034import android.os.Handler;
Jeff Brown109025d2012-08-14 20:41:30 -070035import android.os.Looper;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -080036import android.os.Message;
37import android.os.PowerManager;
Dianne Hackborn4994c662009-09-23 22:21:23 -070038import android.os.RemoteException;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -080039import android.os.SystemClock;
Jim Miller1b152022009-10-28 16:08:15 -070040import android.os.SystemProperties;
Jeff Sharkey6a25cbd2012-08-23 12:14:26 -070041import android.os.UserHandle;
Amith Yamasanib70ff9a2012-09-07 18:28:11 -070042import android.os.UserManager;
Daniel Sandlerdb783bd2010-02-11 15:27:37 -050043import android.provider.Settings;
Karl Rosaenab100082009-03-24 22:35:17 -070044import android.telephony.TelephonyManager;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -080045import android.util.EventLog;
Karl Rosaenab100082009-03-24 22:35:17 -070046import android.util.Log;
Jim Miller25190572013-02-28 17:36:24 -080047import android.util.Slog;
Jorim Jaggi5cf17872014-03-26 18:31:48 +010048import android.view.ViewGroup;
Jorim Jaggi0d674622014-05-21 01:34:15 +020049import android.view.IWindowManager;
50import android.view.WindowManagerGlobal;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -080051import android.view.WindowManagerPolicy;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -080052
Jorim Jaggi5cf17872014-03-26 18:31:48 +010053import com.android.internal.policy.IKeyguardExitCallback;
54import com.android.internal.policy.IKeyguardShowCallback;
Adam Cohenf7522022012-10-03 20:03:18 -070055import com.android.internal.telephony.IccCardConstants;
56import com.android.internal.widget.LockPatternUtils;
Jorim Jaggi5cf17872014-03-26 18:31:48 +010057import com.android.keyguard.KeyguardDisplayManager;
58import com.android.keyguard.KeyguardUpdateMonitor;
59import com.android.keyguard.KeyguardUpdateMonitorCallback;
60import com.android.keyguard.MultiUserAvatarCache;
61import com.android.keyguard.ViewMediatorCallback;
Jorim Jaggicff0acb2014-03-31 16:35:15 +020062import com.android.systemui.SystemUI;
Jorim Jaggi5cf17872014-03-26 18:31:48 +010063import com.android.systemui.statusbar.phone.PhoneStatusBar;
Jorim Jaggiecc798e2014-05-26 18:14:37 +020064import com.android.systemui.statusbar.phone.ScrimController;
Jorim Jaggi5cf17872014-03-26 18:31:48 +010065import com.android.systemui.statusbar.phone.StatusBarKeyguardViewManager;
66import com.android.systemui.statusbar.phone.StatusBarWindowManager;
Adrian Roos58b58fe2014-01-31 16:00:39 -080067
68import java.io.File;
Adam Cohenf7522022012-10-03 20:03:18 -070069
Jorim Jaggi5cf17872014-03-26 18:31:48 +010070import static android.provider.Settings.System.SCREEN_OFF_TIMEOUT;
Jorim Jaggi5cf17872014-03-26 18:31:48 +010071
Wink Saville37c124c2009-04-02 01:37:02 -070072
The Android Open Source Project1f838aa2009-03-03 19:32:13 -080073/**
74 * Mediates requests related to the keyguard. This includes queries about the
75 * state of the keyguard, power management events that effect whether the keyguard
76 * should be shown or reset, callbacks to the phone window manager to notify
77 * it of when the keyguard is showing, and events from the keyguard view itself
78 * stating that the keyguard was succesfully unlocked.
79 *
80 * Note that the keyguard view is shown when the screen is off (as appropriate)
81 * so that once the screen comes on, it will be ready immediately.
82 *
83 * Example queries about the keyguard:
84 * - is {movement, key} one that should wake the keygaurd?
85 * - is the keyguard showing?
86 * - are input events restricted due to the state of the keyguard?
87 *
88 * Callbacks to the phone window manager:
89 * - the keyguard is showing
90 *
91 * Example external events that translate to keyguard view changes:
92 * - screen turned off -> reset the keyguard, and show it so it will be ready
93 * next time the screen turns on
94 * - keyboard is slid open -> if the keyguard is not secure, hide it
95 *
96 * Events from the keyguard view:
97 * - user succesfully unlocked keyguard -> hide keyguard view, and no longer
98 * restrict input events.
99 *
100 * Note: in addition to normal power managment events that effect the state of
101 * whether the keyguard should be showing, external apps and services may request
102 * that the keyguard be disabled via {@link #setKeyguardEnabled(boolean)}. When
103 * false, this will override all other conditions for turning on the keyguard.
104 *
105 * Threading and synchronization:
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100106 * This class is created by the initialization routine of the {@link android.view.WindowManagerPolicy},
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800107 * and runs on its thread. The keyguard UI is created from that thread in the
108 * constructor of this class. The apis may be called from other threads, including the
Jeff Brown4532e612012-04-05 14:27:12 -0700109 * {@link com.android.server.input.InputManagerService}'s and {@link android.view.WindowManager}'s.
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800110 * Therefore, methods on this class are synchronized, and any action that is pointed
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100111 * directly to the keyguard UI is posted to a {@link android.os.Handler} to ensure it is taken on the UI
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800112 * thread of the keyguard.
113 */
Jorim Jaggicff0acb2014-03-31 16:35:15 +0200114public class KeyguardViewMediator extends SystemUI {
Jim Millerbc4603b2010-08-30 21:21:34 -0700115 private static final int KEYGUARD_DISPLAY_TIMEOUT_DELAY_DEFAULT = 30000;
Jim Miller9b1db682012-10-25 19:31:19 -0700116 final static boolean DEBUG = false;
Joe Onorato431bb222010-10-18 19:13:23 -0400117 private final static boolean DBG_WAKE = false;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800118
119 private final static String TAG = "KeyguardViewMediator";
120
Jim Miller9c20d0e2010-01-20 15:00:23 -0800121 private static final String DELAYED_KEYGUARD_ACTION =
Wink Saville37c124c2009-04-02 01:37:02 -0700122 "com.android.internal.policy.impl.PhoneWindowManager.DELAYED_KEYGUARD";
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800123
124 // used for handler messages
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800125 private static final int SHOW = 2;
126 private static final int HIDE = 3;
127 private static final int RESET = 4;
128 private static final int VERIFY_UNLOCK = 5;
129 private static final int NOTIFY_SCREEN_OFF = 6;
130 private static final int NOTIFY_SCREEN_ON = 7;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800131 private static final int KEYGUARD_DONE = 9;
132 private static final int KEYGUARD_DONE_DRAWING = 10;
Dianne Hackborn39c2d712009-09-22 11:41:31 -0700133 private static final int KEYGUARD_DONE_AUTHENTICATING = 11;
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100134 private static final int SET_OCCLUDED = 12;
Mike Lockwood28569302010-01-28 11:54:40 -0500135 private static final int KEYGUARD_TIMEOUT = 13;
Jim Miller60013792013-10-03 18:31:34 -0700136 private static final int DISMISS = 17;
Jorim Jaggi0d674622014-05-21 01:34:15 +0200137 private static final int START_KEYGUARD_EXIT_ANIM = 18;
Jim Miller9c20d0e2010-01-20 15:00:23 -0800138
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800139 /**
140 * The default amount of time we stay awake (used for all key input)
141 */
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100142 public static final int AWAKE_INTERVAL_DEFAULT_MS = 10000;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800143
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800144 /**
145 * How long to wait after the screen turns off due to timeout before
146 * turning on the keyguard (i.e, the user has this much time to turn
147 * the screen back on without having to face the keyguard).
148 */
Jim Millerbc4603b2010-08-30 21:21:34 -0700149 private static final int KEYGUARD_LOCK_AFTER_DELAY_DEFAULT = 5000;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800150
151 /**
Craig Mautnerf1b67412012-09-19 13:18:29 -0700152 * How long we'll wait for the {@link ViewMediatorCallback#keyguardDoneDrawing()}
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800153 * callback before unblocking a call to {@link #setKeyguardEnabled(boolean)}
154 * that is reenabling the keyguard.
155 */
156 private static final int KEYGUARD_DONE_DRAWING_TIMEOUT_MS = 2000;
Jim Miller9c20d0e2010-01-20 15:00:23 -0800157
Daniel Sandler74d188c2011-08-10 00:00:31 -0400158 /**
Adrian Roos58b58fe2014-01-31 16:00:39 -0800159 * Secure setting whether analytics are collected on the keyguard.
160 */
161 private static final String KEYGUARD_ANALYTICS_SETTING = "keyguard_analytics";
162
Amith Yamasani8cb751b2011-09-30 15:39:41 -0700163 /** The stream type that the lock sounds are tied to. */
Eric Laurent6d517662012-04-23 18:42:39 -0700164 private int mMasterStreamType;
Amith Yamasani8cb751b2011-09-30 15:39:41 -0700165
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800166 private AlarmManager mAlarmManager;
Amith Yamasani8cb751b2011-09-30 15:39:41 -0700167 private AudioManager mAudioManager;
Mike Lockwood5f892c12009-11-19 23:39:13 -0500168 private StatusBarManager mStatusBarManager;
Chris Wrenf41c61b2012-11-29 15:19:54 -0500169 private boolean mSwitchingUser;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800170
171 private boolean mSystemReady;
Daniel Sandler0060a9b2010-03-15 23:09:57 -0400172
173 // Whether the next call to playSounds() should be skipped. Defaults to
174 // true because the first lock (on boot) should be silent.
175 private boolean mSuppressNextLockSound = true;
176
Jim Miller9c20d0e2010-01-20 15:00:23 -0800177
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800178 /** High level access to the power manager for WakeLocks */
179 private PowerManager mPM;
180
Jorim Jaggi0d674622014-05-21 01:34:15 +0200181 /** High level access to the window manager for dismissing keyguard animation */
182 private IWindowManager mWM;
183
Amith Yamasanib70ff9a2012-09-07 18:28:11 -0700184 /** UserManager for querying number of users */
185 private UserManager mUserManager;
186
John Spurlock43d84512012-11-09 10:27:33 -0500187 /** SearchManager for determining whether or not search assistant is available */
188 private SearchManager mSearchManager;
189
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800190 /**
Mike Lockwood674d3e42009-10-06 09:28:54 -0400191 * Used to keep the device awake while to ensure the keyguard finishes opening before
192 * we sleep.
193 */
194 private PowerManager.WakeLock mShowKeyguardWakeLock;
195
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100196 private StatusBarKeyguardViewManager mStatusBarKeyguardViewManager;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800197
198 // these are protected by synchronized (this)
199
200 /**
Mike Lockwood5d258b62009-12-02 13:50:45 -0500201 * External apps (like the phone app) can tell us to disable the keygaurd.
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800202 */
203 private boolean mExternallyEnabled = true;
204
205 /**
206 * Remember if an external call to {@link #setKeyguardEnabled} with value
207 * false caused us to hide the keyguard, so that we need to reshow it once
208 * the keygaurd is reenabled with another call with value true.
209 */
210 private boolean mNeedToReshowWhenReenabled = false;
211
212 // cached value of whether we are showing (need to know this to quickly
213 // answer whether the input should be restricted)
Jim Millere5f17ab2013-11-13 15:40:48 -0800214 private boolean mShowing;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800215
Mike Lockwood09a40402009-11-08 00:33:23 -0500216 // true if the keyguard is hidden by another window
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100217 private boolean mOccluded = false;
Mike Lockwood09a40402009-11-08 00:33:23 -0500218
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800219 /**
220 * Helps remember whether the screen has turned on since the last time
221 * it turned off due to timeout. see {@link #onScreenTurnedOff(int)}
222 */
223 private int mDelayedShowingSequence;
224
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800225 /**
226 * If the user has disabled the keyguard, then requests to exit, this is
227 * how we'll ultimately let them know whether it was successful. We use this
228 * var being non-null as an indicator that there is an in progress request.
229 */
Jim Miller25190572013-02-28 17:36:24 -0800230 private IKeyguardExitCallback mExitSecureCallback;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800231
232 // the properties of the keyguard
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800233
234 private KeyguardUpdateMonitor mUpdateMonitor;
235
Daniel Hansson4b716862012-03-29 11:02:05 +0200236 private boolean mScreenOn;
Karl Rosaenab100082009-03-24 22:35:17 -0700237
Daniel Sandlerf2d8e742010-02-22 13:09:48 -0500238 // last known state of the cellular connection
239 private String mPhoneState = TelephonyManager.EXTRA_STATE_IDLE;
240
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800241 /**
The Android Open Source Projectc84bf282009-03-09 11:52:14 -0700242 * we send this intent when the keyguard is dismissed.
243 */
Jim Miller17f509a2013-02-28 18:36:12 -0800244 private static final Intent USER_PRESENT_INTENT = new Intent(Intent.ACTION_USER_PRESENT)
245 .addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING
246 | Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
The Android Open Source Projectc84bf282009-03-09 11:52:14 -0700247
248 /**
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800249 * {@link #setKeyguardEnabled} waits on this condition when it reenables
250 * the keyguard.
251 */
252 private boolean mWaitingUntilKeyguardVisible = false;
Jim Millerbc4603b2010-08-30 21:21:34 -0700253 private LockPatternUtils mLockPatternUtils;
John Spurlock34c4fe52012-11-07 10:12:29 -0500254 private boolean mKeyguardDonePending = false;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800255
Marco Nelissend5545bd2011-09-29 12:49:17 -0700256 private SoundPool mLockSounds;
257 private int mLockSoundId;
258 private int mUnlockSoundId;
259 private int mLockSoundStreamId;
Jean-Michel Trivic55b3932012-06-05 11:57:59 -0700260
261 /**
262 * The volume applied to the lock/unlock sounds.
263 */
Jorim Jaggicff0acb2014-03-31 16:35:15 +0200264 private float mLockSoundVolume;
Marco Nelissend5545bd2011-09-29 12:49:17 -0700265
Jim Millerdcb3d842012-08-23 19:18:12 -0700266 /**
Jim Miller31921482013-11-06 20:43:55 -0800267 * For managing external displays
268 */
269 private KeyguardDisplayManager mKeyguardDisplayManager;
270
Jim Millerbbf1a742012-07-17 18:30:30 -0700271 KeyguardUpdateMonitorCallback mUpdateCallback = new KeyguardUpdateMonitorCallback() {
272
273 @Override
Chris Wrenf41c61b2012-11-29 15:19:54 -0500274 public void onUserSwitching(int userId) {
Craig Mautnerf1b67412012-09-19 13:18:29 -0700275 // Note that the mLockPatternUtils user has already been updated from setCurrentUser.
Adam Cohenf7522022012-10-03 20:03:18 -0700276 // We need to force a reset of the views, since lockNow (called by
277 // ActivityManagerService) will not reconstruct the keyguard if it is already showing.
Jim Millerbbf1a742012-07-17 18:30:30 -0700278 synchronized (KeyguardViewMediator.this) {
Chris Wrenf41c61b2012-11-29 15:19:54 -0500279 mSwitchingUser = true;
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100280 resetStateLocked();
John Spurlock4e6922d2012-10-04 14:51:51 -0400281 adjustStatusBarLocked();
Brian Colonnaa5239892013-04-15 11:45:40 -0400282 // When we switch users we want to bring the new user to the biometric unlock even
283 // if the current user has gone to the backup.
284 KeyguardUpdateMonitor.getInstance(mContext).setAlternateUnlockEnabled(true);
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700285 }
Jim Millerbbf1a742012-07-17 18:30:30 -0700286 }
287
288 @Override
Chris Wrenf41c61b2012-11-29 15:19:54 -0500289 public void onUserSwitchComplete(int userId) {
290 mSwitchingUser = false;
291 }
292
293 @Override
Jim Millerbbf1a742012-07-17 18:30:30 -0700294 public void onUserRemoved(int userId) {
295 mLockPatternUtils.removeUser(userId);
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100296 MultiUserAvatarCache.getInstance().clear(userId);
Amith Yamasani6fc1d4e2013-05-08 16:43:58 -0700297 }
298
299 @Override
300 public void onUserInfoChanged(int userId) {
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100301 MultiUserAvatarCache.getInstance().clear(userId);
Jim Millerbbf1a742012-07-17 18:30:30 -0700302 }
303
304 @Override
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100305 public void onPhoneStateChanged(int phoneState) {
Jim Millerbbf1a742012-07-17 18:30:30 -0700306 synchronized (KeyguardViewMediator.this) {
307 if (TelephonyManager.CALL_STATE_IDLE == phoneState // call ending
308 && !mScreenOn // screen off
309 && mExternallyEnabled) { // not disabled by any app
310
311 // note: this is a way to gracefully reenable the keyguard when the call
312 // ends and the screen is off without always reenabling the keyguard
313 // each time the screen turns off while in call (and having an occasional ugly
314 // flicker while turning back on the screen and disabling the keyguard again).
315 if (DEBUG) Log.d(TAG, "screen is off and call ended, let's make sure the "
316 + "keyguard is showing");
Jim Miller5ecd8112013-01-09 18:50:26 -0800317 doKeyguardLocked(null);
Jim Millerbbf1a742012-07-17 18:30:30 -0700318 }
319 }
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100320 }
Jim Millerb0304762012-03-13 20:01:25 -0700321
322 @Override
323 public void onClockVisibilityChanged() {
324 adjustStatusBarLocked();
325 }
326
327 @Override
328 public void onDeviceProvisioned() {
Jim Miller3fd47af2012-09-21 19:55:27 -0700329 sendUserPresentBroadcast();
Jim Millerb0304762012-03-13 20:01:25 -0700330 }
331
Jim Millerbbf1a742012-07-17 18:30:30 -0700332 @Override
333 public void onSimStateChanged(IccCardConstants.State simState) {
334 if (DEBUG) Log.d(TAG, "onSimStateChanged: " + simState);
335
336 switch (simState) {
337 case NOT_READY:
338 case ABSENT:
339 // only force lock screen in case of missing sim if user hasn't
340 // gone through setup wizard
341 synchronized (this) {
342 if (!mUpdateMonitor.isDeviceProvisioned()) {
343 if (!isShowing()) {
344 if (DEBUG) Log.d(TAG, "ICC_ABSENT isn't showing,"
345 + " we need to show the keyguard since the "
346 + "device isn't provisioned yet.");
Jim Miller5ecd8112013-01-09 18:50:26 -0800347 doKeyguardLocked(null);
Jim Millerbbf1a742012-07-17 18:30:30 -0700348 } else {
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100349 resetStateLocked();
Jim Millerbbf1a742012-07-17 18:30:30 -0700350 }
351 }
352 }
353 break;
354 case PIN_REQUIRED:
355 case PUK_REQUIRED:
356 synchronized (this) {
357 if (!isShowing()) {
358 if (DEBUG) Log.d(TAG, "INTENT_VALUE_ICC_LOCKED and keygaurd isn't "
359 + "showing; need to show keyguard so user can enter sim pin");
Jim Miller5ecd8112013-01-09 18:50:26 -0800360 doKeyguardLocked(null);
Jim Millerbbf1a742012-07-17 18:30:30 -0700361 } else {
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100362 resetStateLocked();
Jim Millerbbf1a742012-07-17 18:30:30 -0700363 }
364 }
365 break;
366 case PERM_DISABLED:
367 synchronized (this) {
368 if (!isShowing()) {
369 if (DEBUG) Log.d(TAG, "PERM_DISABLED and "
370 + "keygaurd isn't showing.");
Jim Miller5ecd8112013-01-09 18:50:26 -0800371 doKeyguardLocked(null);
Jim Millerbbf1a742012-07-17 18:30:30 -0700372 } else {
373 if (DEBUG) Log.d(TAG, "PERM_DISABLED, resetStateLocked to"
374 + "show permanently disabled message in lockscreen.");
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100375 resetStateLocked();
Jim Millerbbf1a742012-07-17 18:30:30 -0700376 }
377 }
378 break;
379 case READY:
380 synchronized (this) {
381 if (isShowing()) {
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100382 resetStateLocked();
Jim Millerbbf1a742012-07-17 18:30:30 -0700383 }
384 }
385 break;
386 }
387 }
388
Jim Millerb0304762012-03-13 20:01:25 -0700389 };
390
Jim Millerdcb3d842012-08-23 19:18:12 -0700391 ViewMediatorCallback mViewMediatorCallback = new ViewMediatorCallback() {
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100392
Jeff Brownc7505bc2012-10-05 21:58:15 -0700393 public void userActivity() {
394 KeyguardViewMediator.this.userActivity();
395 }
396
Jeff Brown3dc524b2012-09-30 19:49:11 -0700397 public void userActivity(long holdMs) {
398 KeyguardViewMediator.this.userActivity(holdMs);
Jim Millerdcb3d842012-08-23 19:18:12 -0700399 }
400
401 public void keyguardDone(boolean authenticated) {
402 KeyguardViewMediator.this.keyguardDone(authenticated, true);
403 }
404
405 public void keyguardDoneDrawing() {
406 mHandler.sendEmptyMessage(KEYGUARD_DONE_DRAWING);
407 }
408
409 @Override
410 public void setNeedsInput(boolean needsInput) {
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100411 mStatusBarKeyguardViewManager.setNeedsInput(needsInput);
Jim Millerdcb3d842012-08-23 19:18:12 -0700412 }
Jeff Brownc7505bc2012-10-05 21:58:15 -0700413
414 @Override
415 public void onUserActivityTimeoutChanged() {
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100416 mStatusBarKeyguardViewManager.updateUserActivityTimeout();
Jeff Brownc7505bc2012-10-05 21:58:15 -0700417 }
John Spurlock34c4fe52012-11-07 10:12:29 -0500418
419 @Override
420 public void keyguardDonePending() {
421 mKeyguardDonePending = true;
422 }
Jim Miller0b9d8ac2013-11-07 19:15:49 -0800423
424 @Override
425 public void keyguardGone() {
426 mKeyguardDisplayManager.hide();
427 }
Jim Millerdcb3d842012-08-23 19:18:12 -0700428 };
429
Jim Miller25190572013-02-28 17:36:24 -0800430 private void userActivity() {
Jeff Brown3dc524b2012-09-30 19:49:11 -0700431 userActivity(AWAKE_INTERVAL_DEFAULT_MS);
432 }
433
434 public void userActivity(long holdMs) {
435 // We ignore the hold time. Eventually we should remove it.
436 // Instead, the keyguard window has an explicit user activity timeout set on it.
437 mPM.userActivity(SystemClock.uptimeMillis(), false);
Jim Millerdcb3d842012-08-23 19:18:12 -0700438 }
439
Jorim Jaggicff0acb2014-03-31 16:35:15 +0200440 private void setup() {
441 mPM = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE);
Jorim Jaggi0d674622014-05-21 01:34:15 +0200442 mWM = WindowManagerGlobal.getWindowManagerService();
Amith Yamasanib70ff9a2012-09-07 18:28:11 -0700443 mUserManager = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
Mike Lockwood674d3e42009-10-06 09:28:54 -0400444 mShowKeyguardWakeLock = mPM.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "show keyguard");
445 mShowKeyguardWakeLock.setReferenceCounted(false);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800446
Jim Millerbbf1a742012-07-17 18:30:30 -0700447 mContext.registerReceiver(mBroadcastReceiver, new IntentFilter(DELAYED_KEYGUARD_ACTION));
448
Jorim Jaggicff0acb2014-03-31 16:35:15 +0200449 mKeyguardDisplayManager = new KeyguardDisplayManager(mContext);
Jim Miller31921482013-11-06 20:43:55 -0800450
Jorim Jaggicff0acb2014-03-31 16:35:15 +0200451 mAlarmManager = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800452
Jorim Jaggicff0acb2014-03-31 16:35:15 +0200453 mUpdateMonitor = KeyguardUpdateMonitor.getInstance(mContext);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800454
Jorim Jaggicff0acb2014-03-31 16:35:15 +0200455 mLockPatternUtils = new LockPatternUtils(mContext);
Craig Mautnerf1b67412012-09-19 13:18:29 -0700456 mLockPatternUtils.setCurrentUser(UserHandle.USER_OWNER);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800457
Jim Millere5f17ab2013-11-13 15:40:48 -0800458 // Assume keyguard is showing (unless it's disabled) until we know for sure...
Jim Miller299422b2013-11-14 14:15:02 -0800459 mShowing = (mUpdateMonitor.isDeviceProvisioned() || mLockPatternUtils.isSecure())
460 && !mLockPatternUtils.isLockScreenDisabled();
Jim Millere5f17ab2013-11-13 15:40:48 -0800461
Jorim Jaggicff0acb2014-03-31 16:35:15 +0200462 mStatusBarKeyguardViewManager = new StatusBarKeyguardViewManager(mContext,
463 mViewMediatorCallback, mLockPatternUtils);
Daniel Sandler687a3272010-03-13 15:44:47 -0500464 final ContentResolver cr = mContext.getContentResolver();
Marco Nelissend5545bd2011-09-29 12:49:17 -0700465
Daniel Hansson4b716862012-03-29 11:02:05 +0200466 mScreenOn = mPM.isScreenOn();
467
Marco Nelissend5545bd2011-09-29 12:49:17 -0700468 mLockSounds = new SoundPool(1, AudioManager.STREAM_SYSTEM, 0);
Jim Millerb14288d2012-09-30 18:25:05 -0700469 String soundPath = Settings.Global.getString(cr, Settings.Global.LOCK_SOUND);
Marco Nelissend5545bd2011-09-29 12:49:17 -0700470 if (soundPath != null) {
471 mLockSoundId = mLockSounds.load(soundPath, 1);
472 }
473 if (soundPath == null || mLockSoundId == 0) {
Jim Millerb14288d2012-09-30 18:25:05 -0700474 Log.w(TAG, "failed to load lock sound from " + soundPath);
Marco Nelissend5545bd2011-09-29 12:49:17 -0700475 }
Jim Millerb14288d2012-09-30 18:25:05 -0700476 soundPath = Settings.Global.getString(cr, Settings.Global.UNLOCK_SOUND);
Marco Nelissend5545bd2011-09-29 12:49:17 -0700477 if (soundPath != null) {
478 mUnlockSoundId = mLockSounds.load(soundPath, 1);
479 }
480 if (soundPath == null || mUnlockSoundId == 0) {
Jim Millerb14288d2012-09-30 18:25:05 -0700481 Log.w(TAG, "failed to load unlock sound from " + soundPath);
Marco Nelissend5545bd2011-09-29 12:49:17 -0700482 }
Jorim Jaggicff0acb2014-03-31 16:35:15 +0200483 int lockSoundDefaultAttenuation = mContext.getResources().getInteger(
Jean-Michel Trivic55b3932012-06-05 11:57:59 -0700484 com.android.internal.R.integer.config_lockSoundVolumeDb);
Jean-Michel Trivif2b0c112012-07-09 11:59:11 -0700485 mLockSoundVolume = (float)Math.pow(10, (float)lockSoundDefaultAttenuation/20);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800486 }
487
Jorim Jaggicff0acb2014-03-31 16:35:15 +0200488 @Override
489 public void start() {
490 setup();
491 putComponent(KeyguardViewMediator.class, this);
492 }
493
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800494 /**
495 * Let us know that the system is ready after startup.
496 */
497 public void onSystemReady() {
John Spurlock43d84512012-11-09 10:27:33 -0500498 mSearchManager = (SearchManager) mContext.getSystemService(Context.SEARCH_SERVICE);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800499 synchronized (this) {
500 if (DEBUG) Log.d(TAG, "onSystemReady");
501 mSystemReady = true;
Jim Millerbbf1a742012-07-17 18:30:30 -0700502 mUpdateMonitor.registerCallback(mUpdateCallback);
Jim Miller08697702012-10-22 16:49:52 -0700503
Brian Colonnad4a3e5d2012-11-01 16:20:24 -0400504 // Suppress biometric unlock right after boot until things have settled if it is the
505 // selected security method, otherwise unsuppress it. It must be unsuppressed if it is
506 // not the selected security method for the following reason: if the user starts
507 // without a screen lock selected, the biometric unlock would be suppressed the first
508 // time they try to use it.
509 //
510 // Note that the biometric unlock will still not show if it is not the selected method.
511 // Calling setAlternateUnlockEnabled(true) simply says don't suppress it if it is the
512 // selected method.
513 if (mLockPatternUtils.usingBiometricWeak()
514 && mLockPatternUtils.isBiometricWeakInstalled()) {
515 if (DEBUG) Log.d(TAG, "suppressing biometric unlock during boot");
516 mUpdateMonitor.setAlternateUnlockEnabled(false);
517 } else {
518 mUpdateMonitor.setAlternateUnlockEnabled(true);
519 }
Jim Miller08697702012-10-22 16:49:52 -0700520
Jim Miller5ecd8112013-01-09 18:50:26 -0800521 doKeyguardLocked(null);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800522 }
Jim Miller3fd47af2012-09-21 19:55:27 -0700523 // Most services aren't available until the system reaches the ready state, so we
524 // send it here when the device first boots.
525 maybeSendUserPresentBroadcast();
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800526 }
527
528 /**
529 * Called to let us know the screen was turned off.
Jeff Brownfb0448a2014-05-01 18:19:31 -0700530 * @param why either {@link android.view.WindowManagerPolicy#OFF_BECAUSE_OF_USER} or
531 * {@link android.view.WindowManagerPolicy#OFF_BECAUSE_OF_TIMEOUT}.
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800532 */
533 public void onScreenTurnedOff(int why) {
534 synchronized (this) {
Karl Rosaenab100082009-03-24 22:35:17 -0700535 mScreenOn = false;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800536 if (DEBUG) Log.d(TAG, "onScreenTurnedOff(" + why + ")");
537
John Spurlock14adfe42012-11-08 10:29:26 -0500538 mKeyguardDonePending = false;
539
Jim Millera4edd152012-01-06 18:24:04 -0800540 // Lock immediately based on setting if secure (user has a pin/pattern/password).
541 // This also "locks" the device when not secure to provide easy access to the
542 // camera while preventing unwanted input.
543 final boolean lockImmediately =
544 mLockPatternUtils.getPowerButtonInstantlyLocks() || !mLockPatternUtils.isSecure();
545
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800546 if (mExitSecureCallback != null) {
547 if (DEBUG) Log.d(TAG, "pending exit secure callback cancelled");
Jim Miller5ecd8112013-01-09 18:50:26 -0800548 try {
549 mExitSecureCallback.onKeyguardExitResult(false);
550 } catch (RemoteException e) {
Jim Miller25190572013-02-28 17:36:24 -0800551 Slog.w(TAG, "Failed to call onKeyguardExitResult(false)", e);
Jim Miller5ecd8112013-01-09 18:50:26 -0800552 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800553 mExitSecureCallback = null;
554 if (!mExternallyEnabled) {
Jim Miller9c20d0e2010-01-20 15:00:23 -0800555 hideLocked();
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800556 }
557 } else if (mShowing) {
558 notifyScreenOffLocked();
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100559 resetStateLocked();
Jim Millera4edd152012-01-06 18:24:04 -0800560 } else if (why == WindowManagerPolicy.OFF_BECAUSE_OF_TIMEOUT
561 || (why == WindowManagerPolicy.OFF_BECAUSE_OF_USER && !lockImmediately)) {
Jeff Brown6aaf2952012-10-05 16:01:08 -0700562 doKeyguardLaterLocked();
Mike Lockwood016e3972009-09-17 11:45:06 -0400563 } else {
Jim Miller5ecd8112013-01-09 18:50:26 -0800564 doKeyguardLocked(null);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800565 }
566 }
Jim Miller20daffd2013-10-07 14:59:53 -0700567 KeyguardUpdateMonitor.getInstance(mContext).dispatchScreenTurndOff(why);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800568 }
569
Jeff Brown6aaf2952012-10-05 16:01:08 -0700570 private void doKeyguardLaterLocked() {
571 // if the screen turned off because of timeout or the user hit the power button
572 // and we don't need to lock immediately, set an alarm
573 // to enable it a little bit later (i.e, give the user a chance
574 // to turn the screen back on within a certain window without
575 // having to unlock the screen)
576 final ContentResolver cr = mContext.getContentResolver();
577
578 // From DisplaySettings
579 long displayTimeout = Settings.System.getInt(cr, SCREEN_OFF_TIMEOUT,
580 KEYGUARD_DISPLAY_TIMEOUT_DELAY_DEFAULT);
581
582 // From SecuritySettings
583 final long lockAfterTimeout = Settings.Secure.getInt(cr,
584 Settings.Secure.LOCK_SCREEN_LOCK_AFTER_TIMEOUT,
585 KEYGUARD_LOCK_AFTER_DELAY_DEFAULT);
586
587 // From DevicePolicyAdmin
588 final long policyTimeout = mLockPatternUtils.getDevicePolicyManager()
589 .getMaximumTimeToLock(null, mLockPatternUtils.getCurrentUser());
590
591 long timeout;
592 if (policyTimeout > 0) {
593 // policy in effect. Make sure we don't go beyond policy limit.
594 displayTimeout = Math.max(displayTimeout, 0); // ignore negative values
595 timeout = Math.min(policyTimeout - displayTimeout, lockAfterTimeout);
596 } else {
597 timeout = lockAfterTimeout;
598 }
599
600 if (timeout <= 0) {
601 // Lock now
602 mSuppressNextLockSound = true;
Jim Miller5ecd8112013-01-09 18:50:26 -0800603 doKeyguardLocked(null);
Jeff Brown6aaf2952012-10-05 16:01:08 -0700604 } else {
605 // Lock in the future
606 long when = SystemClock.elapsedRealtime() + timeout;
607 Intent intent = new Intent(DELAYED_KEYGUARD_ACTION);
608 intent.putExtra("seq", mDelayedShowingSequence);
609 PendingIntent sender = PendingIntent.getBroadcast(mContext,
610 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
611 mAlarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, when, sender);
612 if (DEBUG) Log.d(TAG, "setting alarm to turn off keyguard, seq = "
613 + mDelayedShowingSequence);
614 }
615 }
616
617 private void cancelDoKeyguardLaterLocked() {
618 mDelayedShowingSequence++;
619 }
620
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800621 /**
622 * Let's us know the screen was turned on.
623 */
Jim Miller25190572013-02-28 17:36:24 -0800624 public void onScreenTurnedOn(IKeyguardShowCallback callback) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800625 synchronized (this) {
Karl Rosaenab100082009-03-24 22:35:17 -0700626 mScreenOn = true;
Jeff Brown6aaf2952012-10-05 16:01:08 -0700627 cancelDoKeyguardLaterLocked();
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800628 if (DEBUG) Log.d(TAG, "onScreenTurnedOn, seq = " + mDelayedShowingSequence);
Jim Miller25190572013-02-28 17:36:24 -0800629 if (callback != null) {
630 notifyScreenOnLocked(callback);
Jim Millerd6523da2012-10-21 16:47:02 -0700631 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800632 }
Jim Miller20daffd2013-10-07 14:59:53 -0700633 KeyguardUpdateMonitor.getInstance(mContext).dispatchScreenTurnedOn();
Jim Miller3fd47af2012-09-21 19:55:27 -0700634 maybeSendUserPresentBroadcast();
635 }
636
637 private void maybeSendUserPresentBroadcast() {
638 if (mSystemReady && mLockPatternUtils.isLockScreenDisabled()
Kenny Guy1a447532014-02-20 21:55:32 +0000639 && !mUserManager.isUserSwitcherEnabled()) {
Jim Miller3fd47af2012-09-21 19:55:27 -0700640 // Lock screen is disabled because the user has set the preference to "None".
641 // In this case, send out ACTION_USER_PRESENT here instead of in
642 // handleKeyguardDone()
643 sendUserPresentBroadcast();
644 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800645 }
646
647 /**
Jeff Brown6aaf2952012-10-05 16:01:08 -0700648 * A dream started. We should lock after the usual screen-off lock timeout but only
649 * if there is a secure lock pattern.
650 */
651 public void onDreamingStarted() {
652 synchronized (this) {
653 if (mScreenOn && mLockPatternUtils.isSecure()) {
654 doKeyguardLaterLocked();
655 }
656 }
657 }
658
659 /**
660 * A dream stopped.
661 */
662 public void onDreamingStopped() {
663 synchronized (this) {
664 if (mScreenOn) {
665 cancelDoKeyguardLaterLocked();
666 }
667 }
668 }
669
670 /**
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100671 * Same semantics as {@link android.view.WindowManagerPolicy#enableKeyguard}; provide
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800672 * a way for external stuff to override normal keyguard behavior. For instance
673 * the phone app disables the keyguard when it receives incoming calls.
674 */
675 public void setKeyguardEnabled(boolean enabled) {
676 synchronized (this) {
677 if (DEBUG) Log.d(TAG, "setKeyguardEnabled(" + enabled + ")");
678
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800679 mExternallyEnabled = enabled;
680
681 if (!enabled && mShowing) {
682 if (mExitSecureCallback != null) {
683 if (DEBUG) Log.d(TAG, "in process of verifyUnlock request, ignoring");
684 // we're in the process of handling a request to verify the user
685 // can get past the keyguard. ignore extraneous requests to disable / reenable
686 return;
687 }
688
689 // hiding keyguard that is showing, remember to reshow later
690 if (DEBUG) Log.d(TAG, "remembering to reshow, hiding keyguard, "
691 + "disabling status bar expansion");
692 mNeedToReshowWhenReenabled = true;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800693 hideLocked();
694 } else if (enabled && mNeedToReshowWhenReenabled) {
695 // reenabled after previously hidden, reshow
696 if (DEBUG) Log.d(TAG, "previously hidden, reshowing, reenabling "
697 + "status bar expansion");
698 mNeedToReshowWhenReenabled = false;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800699
700 if (mExitSecureCallback != null) {
701 if (DEBUG) Log.d(TAG, "onKeyguardExitResult(false), resetting");
Jim Miller5ecd8112013-01-09 18:50:26 -0800702 try {
703 mExitSecureCallback.onKeyguardExitResult(false);
704 } catch (RemoteException e) {
Jim Miller25190572013-02-28 17:36:24 -0800705 Slog.w(TAG, "Failed to call onKeyguardExitResult(false)", e);
Jim Miller5ecd8112013-01-09 18:50:26 -0800706 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800707 mExitSecureCallback = null;
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100708 resetStateLocked();
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800709 } else {
Adam Cohenf7522022012-10-03 20:03:18 -0700710 showLocked(null);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800711
712 // block until we know the keygaurd is done drawing (and post a message
713 // to unblock us after a timeout so we don't risk blocking too long
714 // and causing an ANR).
715 mWaitingUntilKeyguardVisible = true;
716 mHandler.sendEmptyMessageDelayed(KEYGUARD_DONE_DRAWING, KEYGUARD_DONE_DRAWING_TIMEOUT_MS);
717 if (DEBUG) Log.d(TAG, "waiting until mWaitingUntilKeyguardVisible is false");
718 while (mWaitingUntilKeyguardVisible) {
719 try {
720 wait();
721 } catch (InterruptedException e) {
722 Thread.currentThread().interrupt();
723 }
724 }
725 if (DEBUG) Log.d(TAG, "done waiting for mWaitingUntilKeyguardVisible");
726 }
727 }
728 }
729 }
730
731 /**
732 * @see android.app.KeyguardManager#exitKeyguardSecurely
733 */
Jim Miller25190572013-02-28 17:36:24 -0800734 public void verifyUnlock(IKeyguardExitCallback callback) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800735 synchronized (this) {
736 if (DEBUG) Log.d(TAG, "verifyUnlock");
737 if (!mUpdateMonitor.isDeviceProvisioned()) {
738 // don't allow this api when the device isn't provisioned
739 if (DEBUG) Log.d(TAG, "ignoring because device isn't provisioned");
Jim Miller5ecd8112013-01-09 18:50:26 -0800740 try {
Jim Miller25190572013-02-28 17:36:24 -0800741 callback.onKeyguardExitResult(false);
Jim Miller5ecd8112013-01-09 18:50:26 -0800742 } catch (RemoteException e) {
Jim Miller25190572013-02-28 17:36:24 -0800743 Slog.w(TAG, "Failed to call onKeyguardExitResult(false)", e);
Jim Miller5ecd8112013-01-09 18:50:26 -0800744 }
Mike Lockwood5d258b62009-12-02 13:50:45 -0500745 } else if (mExternallyEnabled) {
746 // this only applies when the user has externally disabled the
747 // keyguard. this is unexpected and means the user is not
748 // using the api properly.
749 Log.w(TAG, "verifyUnlock called when not externally disabled");
Jim Miller5ecd8112013-01-09 18:50:26 -0800750 try {
Jim Miller25190572013-02-28 17:36:24 -0800751 callback.onKeyguardExitResult(false);
Jim Miller5ecd8112013-01-09 18:50:26 -0800752 } catch (RemoteException e) {
Jim Miller25190572013-02-28 17:36:24 -0800753 Slog.w(TAG, "Failed to call onKeyguardExitResult(false)", e);
Jim Miller5ecd8112013-01-09 18:50:26 -0800754 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800755 } else if (mExitSecureCallback != null) {
756 // already in progress with someone else
Jim Miller5ecd8112013-01-09 18:50:26 -0800757 try {
Jim Miller25190572013-02-28 17:36:24 -0800758 callback.onKeyguardExitResult(false);
Jim Miller5ecd8112013-01-09 18:50:26 -0800759 } catch (RemoteException e) {
Jim Miller25190572013-02-28 17:36:24 -0800760 Slog.w(TAG, "Failed to call onKeyguardExitResult(false)", e);
Jim Miller5ecd8112013-01-09 18:50:26 -0800761 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800762 } else {
Jim Miller25190572013-02-28 17:36:24 -0800763 mExitSecureCallback = callback;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800764 verifyUnlockLocked();
765 }
766 }
767 }
768
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800769 /**
770 * Is the keyguard currently showing?
771 */
772 public boolean isShowing() {
773 return mShowing;
774 }
775
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100776 public boolean isOccluded() {
777 return mOccluded;
Dianne Hackborn9dc06cc2009-11-17 18:19:23 -0800778 }
779
780 /**
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100781 * Is the keyguard currently showing and not being force hidden?
Mike Lockwood09a40402009-11-08 00:33:23 -0500782 */
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100783 public boolean isShowingAndNotOccluded() {
784 return mShowing && !mOccluded;
785 }
786
787 /**
788 * Notify us when the keyguard is occluded by another window
789 */
790 public void setOccluded(boolean isOccluded) {
791 if (DEBUG) Log.d(TAG, "setOccluded " + isOccluded);
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100792 mHandler.removeMessages(SET_OCCLUDED);
793 Message msg = mHandler.obtainMessage(SET_OCCLUDED, (isOccluded ? 1 : 0), 0);
Mike Lockwood9200a392009-11-17 20:25:58 -0500794 mHandler.sendMessage(msg);
795 }
796
797 /**
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100798 * Handles SET_OCCLUDED message sent by setOccluded()
Mike Lockwood9200a392009-11-17 20:25:58 -0500799 */
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100800 private void handleSetOccluded(boolean isOccluded) {
Mike Lockwood09a40402009-11-08 00:33:23 -0500801 synchronized (KeyguardViewMediator.this) {
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100802 if (mOccluded != isOccluded) {
803 mOccluded = isOccluded;
804 mStatusBarKeyguardViewManager.setOccluded(isOccluded);
Dianne Hackbornff5b1582012-04-12 17:24:07 -0700805 updateActivityLockScreenState();
Mike Lockwood5f892c12009-11-19 23:39:13 -0500806 adjustStatusBarLocked();
Mike Lockwood9200a392009-11-17 20:25:58 -0500807 }
Mike Lockwood09a40402009-11-08 00:33:23 -0500808 }
809 }
810
811 /**
Mike Lockwood28569302010-01-28 11:54:40 -0500812 * Used by PhoneWindowManager to enable the keyguard due to a user activity timeout.
813 * This must be safe to call from any thread and with any window manager locks held.
814 */
Adam Cohenf7522022012-10-03 20:03:18 -0700815 public void doKeyguardTimeout(Bundle options) {
Mike Lockwood28569302010-01-28 11:54:40 -0500816 mHandler.removeMessages(KEYGUARD_TIMEOUT);
Adam Cohenf7522022012-10-03 20:03:18 -0700817 Message msg = mHandler.obtainMessage(KEYGUARD_TIMEOUT, options);
Mike Lockwood28569302010-01-28 11:54:40 -0500818 mHandler.sendMessage(msg);
819 }
820
821 /**
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800822 * Given the state of the keyguard, is the input restricted?
823 * Input is restricted when the keyguard is showing, or when the keyguard
824 * was suppressed by an app that disabled the keyguard or we haven't been provisioned yet.
825 */
826 public boolean isInputRestricted() {
827 return mShowing || mNeedToReshowWhenReenabled || !mUpdateMonitor.isDeviceProvisioned();
828 }
829
Dianne Hackbornb446e972009-09-20 15:23:25 -0700830 /**
Craig Mautnerad09bcc2012-10-08 13:33:11 -0700831 * Enable the keyguard if the settings are appropriate.
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800832 */
Adam Cohenf7522022012-10-03 20:03:18 -0700833 private void doKeyguardLocked(Bundle options) {
Dianne Hackborn29aae6f2011-08-18 18:30:09 -0700834 // if another app is disabling us, don't show
835 if (!mExternallyEnabled) {
836 if (DEBUG) Log.d(TAG, "doKeyguard: not showing because externally disabled");
Karl Rosaenab100082009-03-24 22:35:17 -0700837
Dianne Hackborn29aae6f2011-08-18 18:30:09 -0700838 // note: we *should* set mNeedToReshowWhenReenabled=true here, but that makes
839 // for an occasional ugly flicker in this situation:
840 // 1) receive a call with the screen on (no keyguard) or make a call
841 // 2) screen times out
842 // 3) user hits key to turn screen back on
843 // instead, we reenable the keyguard when we know the screen is off and the call
844 // ends (see the broadcast receiver below)
845 // TODO: clean this up when we have better support at the window manager level
846 // for apps that wish to be on top of the keyguard
Dianne Hackborn38e29a62011-09-18 14:43:08 -0700847 return;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800848 }
Dianne Hackborn29aae6f2011-08-18 18:30:09 -0700849
850 // if the keyguard is already showing, don't bother
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100851 if (mStatusBarKeyguardViewManager.isShowing()) {
Dianne Hackborn29aae6f2011-08-18 18:30:09 -0700852 if (DEBUG) Log.d(TAG, "doKeyguard: not showing because it is already showing");
Dianne Hackborn38e29a62011-09-18 14:43:08 -0700853 return;
Dianne Hackborn29aae6f2011-08-18 18:30:09 -0700854 }
855
856 // if the setup wizard hasn't run yet, don't show
857 final boolean requireSim = !SystemProperties.getBoolean("keyguard.no_require_sim",
858 false);
859 final boolean provisioned = mUpdateMonitor.isDeviceProvisioned();
Wink Savillea639b312012-07-10 12:37:54 -0700860 final IccCardConstants.State state = mUpdateMonitor.getSimState();
Dianne Hackborn29aae6f2011-08-18 18:30:09 -0700861 final boolean lockedOrMissing = state.isPinLocked()
Wink Savillea639b312012-07-10 12:37:54 -0700862 || ((state == IccCardConstants.State.ABSENT
863 || state == IccCardConstants.State.PERM_DISABLED)
Jim Miller9fa1ada2011-10-31 18:21:49 -0700864 && requireSim);
Dianne Hackborn29aae6f2011-08-18 18:30:09 -0700865
866 if (!lockedOrMissing && !provisioned) {
867 if (DEBUG) Log.d(TAG, "doKeyguard: not showing because device isn't provisioned"
868 + " and the sim is not locked or missing");
Dianne Hackborn38e29a62011-09-18 14:43:08 -0700869 return;
Dianne Hackborn29aae6f2011-08-18 18:30:09 -0700870 }
871
Kenny Guy1a447532014-02-20 21:55:32 +0000872 if (!mUserManager.isUserSwitcherEnabled()
Amith Yamasanib70ff9a2012-09-07 18:28:11 -0700873 && mLockPatternUtils.isLockScreenDisabled() && !lockedOrMissing) {
Dianne Hackborn29aae6f2011-08-18 18:30:09 -0700874 if (DEBUG) Log.d(TAG, "doKeyguard: not showing because lockscreen is off");
Dianne Hackborn38e29a62011-09-18 14:43:08 -0700875 return;
Dianne Hackborn29aae6f2011-08-18 18:30:09 -0700876 }
877
Paul Lawrence945490c2014-03-27 16:37:28 +0000878 if (mLockPatternUtils.checkVoldPassword()) {
879 if (DEBUG) Log.d(TAG, "Not showing lock screen since just decrypted");
880 // Without this, settings is not enabled until the lock screen first appears
Jorim Jaggib9d60792014-06-03 22:34:22 +0200881 mShowing = false;
Paul Lawrence945490c2014-03-27 16:37:28 +0000882 hideLocked();
883 return;
884 }
885
Dianne Hackborn29aae6f2011-08-18 18:30:09 -0700886 if (DEBUG) Log.d(TAG, "doKeyguard: showing the lock screen");
Adam Cohenf7522022012-10-03 20:03:18 -0700887 showLocked(options);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800888 }
889
890 /**
Craig Mautnerad09bcc2012-10-08 13:33:11 -0700891 * Dismiss the keyguard through the security layers.
892 */
Jim Miller60013792013-10-03 18:31:34 -0700893 public void handleDismiss() {
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100894 if (mShowing && !mOccluded) {
895 mStatusBarKeyguardViewManager.dismiss();
Jim Miller87d03662012-11-05 20:28:09 -0800896 }
Craig Mautnerad09bcc2012-10-08 13:33:11 -0700897 }
898
Jim Miller60013792013-10-03 18:31:34 -0700899 public void dismiss() {
900 mHandler.sendEmptyMessage(DISMISS);
901 }
902
Craig Mautnerad09bcc2012-10-08 13:33:11 -0700903 /**
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800904 * Send message to keyguard telling it to reset its state.
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100905 * @see #handleReset
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800906 */
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100907 private void resetStateLocked() {
Jim Miller4894a012013-04-03 15:23:55 -0700908 if (DEBUG) Log.e(TAG, "resetStateLocked");
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100909 Message msg = mHandler.obtainMessage(RESET);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800910 mHandler.sendMessage(msg);
911 }
912
913 /**
914 * Send message to keyguard telling it to verify unlock
915 * @see #handleVerifyUnlock()
916 */
917 private void verifyUnlockLocked() {
918 if (DEBUG) Log.d(TAG, "verifyUnlockLocked");
919 mHandler.sendEmptyMessage(VERIFY_UNLOCK);
920 }
921
922
923 /**
924 * Send a message to keyguard telling it the screen just turned on.
925 * @see #onScreenTurnedOff(int)
926 * @see #handleNotifyScreenOff
927 */
928 private void notifyScreenOffLocked() {
929 if (DEBUG) Log.d(TAG, "notifyScreenOffLocked");
930 mHandler.sendEmptyMessage(NOTIFY_SCREEN_OFF);
931 }
932
933 /**
934 * Send a message to keyguard telling it the screen just turned on.
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100935 * @see #onScreenTurnedOn
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800936 * @see #handleNotifyScreenOn
937 */
Jim Miller25190572013-02-28 17:36:24 -0800938 private void notifyScreenOnLocked(IKeyguardShowCallback result) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800939 if (DEBUG) Log.d(TAG, "notifyScreenOnLocked");
Jim Miller5ecd8112013-01-09 18:50:26 -0800940 Message msg = mHandler.obtainMessage(NOTIFY_SCREEN_ON, result);
Dianne Hackborn38e29a62011-09-18 14:43:08 -0700941 mHandler.sendMessage(msg);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800942 }
943
944 /**
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800945 * Send message to keyguard telling it to show itself
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100946 * @see #handleShow
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800947 */
Adam Cohenf7522022012-10-03 20:03:18 -0700948 private void showLocked(Bundle options) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800949 if (DEBUG) Log.d(TAG, "showLocked");
Mike Lockwood674d3e42009-10-06 09:28:54 -0400950 // ensure we stay awake until we are finished displaying the keyguard
951 mShowKeyguardWakeLock.acquire();
Adam Cohenf7522022012-10-03 20:03:18 -0700952 Message msg = mHandler.obtainMessage(SHOW, options);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800953 mHandler.sendMessage(msg);
954 }
955
956 /**
957 * Send message to keyguard telling it to hide itself
Jim Miller9c20d0e2010-01-20 15:00:23 -0800958 * @see #handleHide()
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800959 */
960 private void hideLocked() {
961 if (DEBUG) Log.d(TAG, "hideLocked");
962 Message msg = mHandler.obtainMessage(HIDE);
963 mHandler.sendMessage(msg);
964 }
965
Dianne Hackbornb446e972009-09-20 15:23:25 -0700966 public boolean isSecure() {
Jim Millerdcb3d842012-08-23 19:18:12 -0700967 return mLockPatternUtils.isSecure()
968 || KeyguardUpdateMonitor.getInstance(mContext).isSimPinSecure();
Dianne Hackbornb446e972009-09-20 15:23:25 -0700969 }
Jim Miller9c20d0e2010-01-20 15:00:23 -0800970
Craig Mautnerf1b67412012-09-19 13:18:29 -0700971 /**
972 * Update the newUserId. Call while holding WindowManagerService lock.
Jim Milleree82f8f2012-10-01 16:26:18 -0700973 * NOTE: Should only be called by KeyguardViewMediator in response to the user id changing.
974 *
Craig Mautnerf1b67412012-09-19 13:18:29 -0700975 * @param newUserId The id of the incoming user.
976 */
977 public void setCurrentUser(int newUserId) {
978 mLockPatternUtils.setCurrentUser(newUserId);
979 }
980
Jim Millerbbf1a742012-07-17 18:30:30 -0700981 private final BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
Amith Yamasani52c489c2012-03-28 11:42:42 -0700982 @Override
983 public void onReceive(Context context, Intent intent) {
Jim Millerbbf1a742012-07-17 18:30:30 -0700984 if (DELAYED_KEYGUARD_ACTION.equals(intent.getAction())) {
985 final int sequence = intent.getIntExtra("seq", 0);
Jim Millerf3447352011-08-07 14:00:09 -0700986 if (DEBUG) Log.d(TAG, "received DELAYED_KEYGUARD_ACTION with seq = "
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800987 + sequence + ", mDelayedShowingSequence = " + mDelayedShowingSequence);
Dianne Hackborn29aae6f2011-08-18 18:30:09 -0700988 synchronized (KeyguardViewMediator.this) {
989 if (mDelayedShowingSequence == sequence) {
Jim Millerbbf1a742012-07-17 18:30:30 -0700990 // Don't play lockscreen SFX if the screen went off due to timeout.
Dianne Hackborn29aae6f2011-08-18 18:30:09 -0700991 mSuppressNextLockSound = true;
Jim Miller5ecd8112013-01-09 18:50:26 -0800992 doKeyguardLocked(null);
Dianne Hackborn29aae6f2011-08-18 18:30:09 -0700993 }
Daniel Sandlerf2d8e742010-02-22 13:09:48 -0500994 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800995 }
996 }
997 };
998
Dianne Hackborn05726582009-09-22 17:28:24 -0700999 public void keyguardDone(boolean authenticated, boolean wakeup) {
Jim Miller60013792013-10-03 18:31:34 -07001000 if (DEBUG) Log.d(TAG, "keyguardDone(" + authenticated + ")");
1001 EventLog.writeEvent(70000, 2);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001002 synchronized (this) {
Jim Miller60013792013-10-03 18:31:34 -07001003 mKeyguardDonePending = false;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001004 }
Jim Miller60013792013-10-03 18:31:34 -07001005 Message msg = mHandler.obtainMessage(KEYGUARD_DONE, authenticated ? 1 : 0, wakeup ? 1 : 0);
1006 mHandler.sendMessage(msg);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001007 }
1008
1009 /**
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001010 * This handler will be associated with the policy thread, which will also
1011 * be the UI thread of the keyguard. Since the apis of the policy, and therefore
1012 * this class, can be called by other threads, any action that directly
1013 * interacts with the keyguard ui should be posted to this handler, rather
1014 * than called directly.
1015 */
Jim Millerdcb3d842012-08-23 19:18:12 -07001016 private Handler mHandler = new Handler(Looper.myLooper(), null, true /*async*/) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001017 @Override
Wink Saville37c124c2009-04-02 01:37:02 -07001018 public void handleMessage(Message msg) {
1019 switch (msg.what) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001020 case SHOW:
Adam Cohenf7522022012-10-03 20:03:18 -07001021 handleShow((Bundle) msg.obj);
Jim Miller60013792013-10-03 18:31:34 -07001022 break;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001023 case HIDE:
1024 handleHide();
Jim Miller60013792013-10-03 18:31:34 -07001025 break;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001026 case RESET:
Jorim Jaggi5cf17872014-03-26 18:31:48 +01001027 handleReset();
Jim Miller60013792013-10-03 18:31:34 -07001028 break;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001029 case VERIFY_UNLOCK:
1030 handleVerifyUnlock();
Jim Miller60013792013-10-03 18:31:34 -07001031 break;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001032 case NOTIFY_SCREEN_OFF:
1033 handleNotifyScreenOff();
Jim Miller60013792013-10-03 18:31:34 -07001034 break;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001035 case NOTIFY_SCREEN_ON:
Jim Miller25190572013-02-28 17:36:24 -08001036 handleNotifyScreenOn((IKeyguardShowCallback) msg.obj);
Jim Miller60013792013-10-03 18:31:34 -07001037 break;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001038 case KEYGUARD_DONE:
Jim Millere51cf7ae2013-06-25 18:31:56 -07001039 handleKeyguardDone(msg.arg1 != 0, msg.arg2 != 0);
Jim Miller60013792013-10-03 18:31:34 -07001040 break;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001041 case KEYGUARD_DONE_DRAWING:
1042 handleKeyguardDoneDrawing();
Jim Miller60013792013-10-03 18:31:34 -07001043 break;
Dianne Hackborn39c2d712009-09-22 11:41:31 -07001044 case KEYGUARD_DONE_AUTHENTICATING:
Jim Millerdcb3d842012-08-23 19:18:12 -07001045 keyguardDone(true, true);
Jim Miller60013792013-10-03 18:31:34 -07001046 break;
Jorim Jaggi5cf17872014-03-26 18:31:48 +01001047 case SET_OCCLUDED:
1048 handleSetOccluded(msg.arg1 != 0);
Mike Lockwood9200a392009-11-17 20:25:58 -05001049 break;
Mike Lockwood28569302010-01-28 11:54:40 -05001050 case KEYGUARD_TIMEOUT:
Dianne Hackborn29aae6f2011-08-18 18:30:09 -07001051 synchronized (KeyguardViewMediator.this) {
Adam Cohenf7522022012-10-03 20:03:18 -07001052 doKeyguardLocked((Bundle) msg.obj);
Dianne Hackborn29aae6f2011-08-18 18:30:09 -07001053 }
1054 break;
Jim Miller60013792013-10-03 18:31:34 -07001055 case DISMISS:
1056 handleDismiss();
1057 break;
Jorim Jaggi0d674622014-05-21 01:34:15 +02001058 case START_KEYGUARD_EXIT_ANIM:
Jorim Jaggie29b2db2014-05-30 23:17:03 +02001059 StartKeyguardExitAnimParams params = (StartKeyguardExitAnimParams) msg.obj;
1060 handleStartKeyguardExitAnimation(params.startTime, params.fadeoutDuration);
Jorim Jaggi0d674622014-05-21 01:34:15 +02001061 break;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001062 }
1063 }
1064 };
1065
1066 /**
1067 * @see #keyguardDone
1068 * @see #KEYGUARD_DONE
1069 */
Jim Millere51cf7ae2013-06-25 18:31:56 -07001070 private void handleKeyguardDone(boolean authenticated, boolean wakeup) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001071 if (DEBUG) Log.d(TAG, "handleKeyguardDone");
Jim Millere51cf7ae2013-06-25 18:31:56 -07001072
1073 if (authenticated) {
1074 mUpdateMonitor.clearFailedUnlockAttempts();
Dianne Hackborn05726582009-09-22 17:28:24 -07001075 }
Jeff Sharkey6a25cbd2012-08-23 12:14:26 -07001076
Jim Millere51cf7ae2013-06-25 18:31:56 -07001077 if (mExitSecureCallback != null) {
1078 try {
1079 mExitSecureCallback.onKeyguardExitResult(authenticated);
1080 } catch (RemoteException e) {
1081 Slog.w(TAG, "Failed to call onKeyguardExitResult(" + authenticated + ")", e);
1082 }
1083
1084 mExitSecureCallback = null;
1085
1086 if (authenticated) {
1087 // after succesfully exiting securely, no need to reshow
1088 // the keyguard when they've released the lock
1089 mExternallyEnabled = true;
1090 mNeedToReshowWhenReenabled = false;
1091 }
1092 }
1093
1094 handleHide();
Jim Miller3fd47af2012-09-21 19:55:27 -07001095 sendUserPresentBroadcast();
1096 }
1097
1098 private void sendUserPresentBroadcast() {
Jim Miller17f509a2013-02-28 18:36:12 -08001099 final UserHandle currentUser = new UserHandle(mLockPatternUtils.getCurrentUser());
1100 mContext.sendBroadcastAsUser(USER_PRESENT_INTENT, currentUser);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001101 }
1102
1103 /**
Jorim Jaggi5cf17872014-03-26 18:31:48 +01001104 * @see #keyguardDone
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001105 * @see #KEYGUARD_DONE_DRAWING
1106 */
1107 private void handleKeyguardDoneDrawing() {
1108 synchronized(this) {
Jim Miller5ecd8112013-01-09 18:50:26 -08001109 if (DEBUG) Log.d(TAG, "handleKeyguardDoneDrawing");
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001110 if (mWaitingUntilKeyguardVisible) {
1111 if (DEBUG) Log.d(TAG, "handleKeyguardDoneDrawing: notifying mWaitingUntilKeyguardVisible");
1112 mWaitingUntilKeyguardVisible = false;
1113 notifyAll();
1114
1115 // there will usually be two of these sent, one as a timeout, and one
1116 // as a result of the callback, so remove any remaining messages from
1117 // the queue
1118 mHandler.removeMessages(KEYGUARD_DONE_DRAWING);
1119 }
1120 }
1121 }
1122
Daniel Sandlerdb783bd2010-02-11 15:27:37 -05001123 private void playSounds(boolean locked) {
1124 // User feedback for keyguard.
Daniel Sandler0060a9b2010-03-15 23:09:57 -04001125
1126 if (mSuppressNextLockSound) {
1127 mSuppressNextLockSound = false;
1128 return;
1129 }
1130
Daniel Sandlerdb783bd2010-02-11 15:27:37 -05001131 final ContentResolver cr = mContext.getContentResolver();
Amith Yamasani8cb751b2011-09-30 15:39:41 -07001132 if (Settings.System.getInt(cr, Settings.System.LOCKSCREEN_SOUNDS_ENABLED, 1) == 1) {
Marco Nelissend5545bd2011-09-29 12:49:17 -07001133 final int whichSound = locked
1134 ? mLockSoundId
1135 : mUnlockSoundId;
1136 mLockSounds.stop(mLockSoundStreamId);
Amith Yamasani8cb751b2011-09-30 15:39:41 -07001137 // Init mAudioManager
1138 if (mAudioManager == null) {
1139 mAudioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
1140 if (mAudioManager == null) return;
Eric Laurent6d517662012-04-23 18:42:39 -07001141 mMasterStreamType = mAudioManager.getMasterStreamType();
Amith Yamasani8cb751b2011-09-30 15:39:41 -07001142 }
1143 // If the stream is muted, don't play the sound
Eric Laurent6d517662012-04-23 18:42:39 -07001144 if (mAudioManager.isStreamMute(mMasterStreamType)) return;
Amith Yamasani8cb751b2011-09-30 15:39:41 -07001145
Jean-Michel Trivic55b3932012-06-05 11:57:59 -07001146 mLockSoundStreamId = mLockSounds.play(whichSound,
1147 mLockSoundVolume, mLockSoundVolume, 1/*priortiy*/, 0/*loop*/, 1.0f/*rate*/);
Daniel Sandlerdb783bd2010-02-11 15:27:37 -05001148 }
Jim Miller2a98a4c2010-11-19 18:49:26 -08001149 }
Daniel Sandlerdb783bd2010-02-11 15:27:37 -05001150
Dianne Hackbornff5b1582012-04-12 17:24:07 -07001151 private void updateActivityLockScreenState() {
1152 try {
Jorim Jaggi5cf17872014-03-26 18:31:48 +01001153 ActivityManagerNative.getDefault().setLockScreenShown(mShowing && !mOccluded);
Dianne Hackbornff5b1582012-04-12 17:24:07 -07001154 } catch (RemoteException e) {
1155 }
1156 }
1157
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001158 /**
1159 * Handle message sent by {@link #showLocked}.
1160 * @see #SHOW
1161 */
Adam Cohenf7522022012-10-03 20:03:18 -07001162 private void handleShow(Bundle options) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001163 synchronized (KeyguardViewMediator.this) {
Jim Miller5ecd8112013-01-09 18:50:26 -08001164 if (!mSystemReady) {
1165 if (DEBUG) Log.d(TAG, "ignoring handleShow because system is not ready.");
1166 return;
1167 } else {
1168 if (DEBUG) Log.d(TAG, "handleShow");
1169 }
Jim Miller9c20d0e2010-01-20 15:00:23 -08001170
Jorim Jaggi5cf17872014-03-26 18:31:48 +01001171 mStatusBarKeyguardViewManager.show(options);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001172 mShowing = true;
John Spurlock14adfe42012-11-08 10:29:26 -05001173 mKeyguardDonePending = false;
Dianne Hackbornff5b1582012-04-12 17:24:07 -07001174 updateActivityLockScreenState();
Mike Lockwood5f892c12009-11-19 23:39:13 -05001175 adjustStatusBarLocked();
Jeff Brown3dc524b2012-09-30 19:49:11 -07001176 userActivity();
Dianne Hackborn29aae6f2011-08-18 18:30:09 -07001177
1178 // Do this at the end to not slow down display of the keyguard.
1179 playSounds(true);
1180
Mike Lockwood674d3e42009-10-06 09:28:54 -04001181 mShowKeyguardWakeLock.release();
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001182 }
Jim Miller31921482013-11-06 20:43:55 -08001183 mKeyguardDisplayManager.show();
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001184 }
1185
1186 /**
1187 * Handle message sent by {@link #hideLocked()}
1188 * @see #HIDE
1189 */
1190 private void handleHide() {
1191 synchronized (KeyguardViewMediator.this) {
1192 if (DEBUG) Log.d(TAG, "handleHide");
Jorim Jaggi0d674622014-05-21 01:34:15 +02001193 try {
1194
Jorim Jaggib9d60792014-06-03 22:34:22 +02001195 if (mShowing) {
1196
1197 // Don't actually hide the Keyguard at the moment, wait for window manager until
1198 // it tells us it's safe to do so with startKeyguardExitAnimation.
1199 mWM.keyguardGoingAway();
1200 } else {
1201
1202 // Don't try to rely on WindowManager - if Keyguard wasn't showing, window
1203 // manager won't start the exit animation.
1204 handleStartKeyguardExitAnimation(0, 0);
1205 }
Jorim Jaggi0d674622014-05-21 01:34:15 +02001206 } catch (RemoteException e) {
1207 Log.e(TAG, "Error while calling WindowManager", e);
1208 }
1209 }
1210 }
1211
Jorim Jaggie29b2db2014-05-30 23:17:03 +02001212 private void handleStartKeyguardExitAnimation(long startTime, long fadeoutDuration) {
Jorim Jaggi0d674622014-05-21 01:34:15 +02001213 synchronized (KeyguardViewMediator.this) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001214
Daniel Sandlerf2d8e742010-02-22 13:09:48 -05001215 // only play "unlock" noises if not on a call (since the incall UI
1216 // disables the keyguard)
1217 if (TelephonyManager.EXTRA_STATE_IDLE.equals(mPhoneState)) {
1218 playSounds(false);
1219 }
Daniel Sandlerdb783bd2010-02-11 15:27:37 -05001220
Jorim Jaggie29b2db2014-05-30 23:17:03 +02001221 mStatusBarKeyguardViewManager.hide(startTime, fadeoutDuration);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001222 mShowing = false;
John Spurlock14adfe42012-11-08 10:29:26 -05001223 mKeyguardDonePending = false;
Dianne Hackbornff5b1582012-04-12 17:24:07 -07001224 updateActivityLockScreenState();
Mike Lockwood5f892c12009-11-19 23:39:13 -05001225 adjustStatusBarLocked();
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001226 }
1227 }
1228
Mike Lockwood5f892c12009-11-19 23:39:13 -05001229 private void adjustStatusBarLocked() {
1230 if (mStatusBarManager == null) {
1231 mStatusBarManager = (StatusBarManager)
1232 mContext.getSystemService(Context.STATUS_BAR_SERVICE);
1233 }
1234 if (mStatusBarManager == null) {
1235 Log.w(TAG, "Could not get status bar manager");
1236 } else {
Daniel Sandlerdba93562011-10-06 16:39:58 -04001237 // Disable aspects of the system/status/navigation bars that must not be re-enabled by
1238 // windows that appear on top, ever
Jeff Sharkeyf52c70b2011-08-30 22:05:47 -07001239 int flags = StatusBarManager.DISABLE_NONE;
Mike Lockwoode3646dd2011-09-01 12:46:28 -04001240 if (mShowing) {
Jim Millere23ab8b2012-09-16 15:45:44 -07001241 // Permanently disable components not available when keyguard is enabled
1242 // (like recents). Temporary enable/disable (e.g. the "back" button) are
1243 // done in KeyguardHostView.
Daniel Sandlerdba93562011-10-06 16:39:58 -04001244 flags |= StatusBarManager.DISABLE_RECENT;
John Spurlock43d84512012-11-09 10:27:33 -05001245 if (!isAssistantAvailable()) {
1246 flags |= StatusBarManager.DISABLE_SEARCH;
1247 }
Jeff Sharkeyf52c70b2011-08-30 22:05:47 -07001248 }
Jorim Jaggia005f1b2014-04-16 19:06:10 +02001249 if (isShowingAndNotOccluded()) {
1250 flags |= StatusBarManager.DISABLE_HOME;
1251 }
Jeff Sharkeyf52c70b2011-08-30 22:05:47 -07001252
1253 if (DEBUG) {
Jorim Jaggi5cf17872014-03-26 18:31:48 +01001254 Log.d(TAG, "adjustStatusBarLocked: mShowing=" + mShowing + " mOccluded=" + mOccluded
Jeff Sharkey4519a022011-09-07 23:24:53 -07001255 + " isSecure=" + isSecure() + " --> flags=0x" + Integer.toHexString(flags));
Jeff Sharkeyf52c70b2011-08-30 22:05:47 -07001256 }
1257
Jim Millerd6523da2012-10-21 16:47:02 -07001258 if (!(mContext instanceof Activity)) {
1259 mStatusBarManager.disable(flags);
1260 }
Mike Lockwood5f892c12009-11-19 23:39:13 -05001261 }
1262 }
1263
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001264 /**
Jorim Jaggi5cf17872014-03-26 18:31:48 +01001265 * Handle message sent by {@link #resetStateLocked}
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001266 * @see #RESET
1267 */
Jorim Jaggi5cf17872014-03-26 18:31:48 +01001268 private void handleReset() {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001269 synchronized (KeyguardViewMediator.this) {
1270 if (DEBUG) Log.d(TAG, "handleReset");
Jorim Jaggi5cf17872014-03-26 18:31:48 +01001271 mStatusBarKeyguardViewManager.reset();
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001272 }
1273 }
1274
1275 /**
1276 * Handle message sent by {@link #verifyUnlock}
Craig Mautner904732c2012-10-17 15:20:24 -07001277 * @see #VERIFY_UNLOCK
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001278 */
1279 private void handleVerifyUnlock() {
1280 synchronized (KeyguardViewMediator.this) {
1281 if (DEBUG) Log.d(TAG, "handleVerifyUnlock");
Jorim Jaggi5cf17872014-03-26 18:31:48 +01001282 mStatusBarKeyguardViewManager.verifyUnlock();
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001283 mShowing = true;
Dianne Hackbornff5b1582012-04-12 17:24:07 -07001284 updateActivityLockScreenState();
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001285 }
1286 }
1287
1288 /**
1289 * Handle message sent by {@link #notifyScreenOffLocked()}
1290 * @see #NOTIFY_SCREEN_OFF
1291 */
1292 private void handleNotifyScreenOff() {
1293 synchronized (KeyguardViewMediator.this) {
1294 if (DEBUG) Log.d(TAG, "handleNotifyScreenOff");
Jorim Jaggi5cf17872014-03-26 18:31:48 +01001295 mStatusBarKeyguardViewManager.onScreenTurnedOff();
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001296 }
1297 }
1298
1299 /**
Jorim Jaggi5cf17872014-03-26 18:31:48 +01001300 * Handle message sent by {@link #notifyScreenOnLocked}
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001301 * @see #NOTIFY_SCREEN_ON
1302 */
Jim Miller25190572013-02-28 17:36:24 -08001303 private void handleNotifyScreenOn(IKeyguardShowCallback callback) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001304 synchronized (KeyguardViewMediator.this) {
1305 if (DEBUG) Log.d(TAG, "handleNotifyScreenOn");
Jorim Jaggi5cf17872014-03-26 18:31:48 +01001306 mStatusBarKeyguardViewManager.onScreenTurnedOn(callback);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001307 }
1308 }
Jeff Sharkey054340d2011-09-01 22:28:03 -07001309
John Spurlock34c4fe52012-11-07 10:12:29 -05001310 public boolean isDismissable() {
1311 return mKeyguardDonePending || !isSecure();
1312 }
1313
John Spurlock43d84512012-11-09 10:27:33 -05001314 private boolean isAssistantAvailable() {
1315 return mSearchManager != null
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08001316 && mSearchManager.getAssistIntent(mContext, false, UserHandle.USER_CURRENT) != null;
John Spurlock43d84512012-11-09 10:27:33 -05001317 }
Amith Yamasani6fc1d4e2013-05-08 16:43:58 -07001318
Jim Millere5f910a2013-10-16 18:15:46 -07001319 public void onBootCompleted() {
1320 mUpdateMonitor.dispatchBootCompleted();
1321 }
Jorim Jaggi5cf17872014-03-26 18:31:48 +01001322
Jorim Jaggi03c701e2014-04-02 12:39:51 +02001323 public StatusBarKeyguardViewManager registerStatusBar(PhoneStatusBar phoneStatusBar,
Jorim Jaggiecc798e2014-05-26 18:14:37 +02001324 ViewGroup container, StatusBarWindowManager statusBarWindowManager,
1325 ScrimController scrimController) {
Jorim Jaggi5cf17872014-03-26 18:31:48 +01001326 mStatusBarKeyguardViewManager.registerStatusBar(phoneStatusBar, container,
Jorim Jaggiecc798e2014-05-26 18:14:37 +02001327 statusBarWindowManager, scrimController);
Jorim Jaggi03c701e2014-04-02 12:39:51 +02001328 return mStatusBarKeyguardViewManager;
1329 }
1330
Jorim Jaggie29b2db2014-05-30 23:17:03 +02001331 public void startKeyguardExitAnimation(long startTime, long fadeoutDuration) {
1332 Message msg = mHandler.obtainMessage(START_KEYGUARD_EXIT_ANIM,
1333 new StartKeyguardExitAnimParams(startTime, fadeoutDuration));
Jorim Jaggi0d674622014-05-21 01:34:15 +02001334 mHandler.sendMessage(msg);
1335 }
1336
Jorim Jaggi03c701e2014-04-02 12:39:51 +02001337 public ViewMediatorCallback getViewMediatorCallback() {
1338 return mViewMediatorCallback;
Jorim Jaggi5cf17872014-03-26 18:31:48 +01001339 }
Jorim Jaggie29b2db2014-05-30 23:17:03 +02001340
1341 private static class StartKeyguardExitAnimParams {
1342
1343 long startTime;
1344 long fadeoutDuration;
1345
1346 private StartKeyguardExitAnimParams(long startTime, long fadeoutDuration) {
1347 this.startTime = startTime;
1348 this.fadeoutDuration = fadeoutDuration;
1349 }
1350 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001351}