blob: 597fb3bf7638147197ff7d6c295579b0c7db5132 [file] [log] [blame]
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Jim Miller5ecd8112013-01-09 18:50:26 -080017package com.android.keyguard;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -080018
Jim Miller25190572013-02-28 17:36:24 -080019import com.android.internal.policy.IKeyguardExitCallback;
20import com.android.internal.policy.IKeyguardShowCallback;
Jim Millerbc4603b2010-08-30 21:21:34 -070021import static android.provider.Settings.System.SCREEN_OFF_TIMEOUT;
22
Jim Millerdcb3d842012-08-23 19:18:12 -070023import android.app.Activity;
Dianne Hackborn4994c662009-09-23 22:21:23 -070024import android.app.ActivityManagerNative;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -080025import android.app.AlarmManager;
26import android.app.PendingIntent;
John Spurlock43d84512012-11-09 10:27:33 -050027import android.app.SearchManager;
Mike Lockwood5f892c12009-11-19 23:39:13 -050028import android.app.StatusBarManager;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -080029import android.content.BroadcastReceiver;
Daniel Sandlerdb783bd2010-02-11 15:27:37 -050030import android.content.ContentResolver;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -080031import android.content.Context;
32import android.content.Intent;
33import android.content.IntentFilter;
Daniel Sandleraec967a2010-02-20 01:05:22 -050034import android.media.AudioManager;
Marco Nelissend5545bd2011-09-29 12:49:17 -070035import android.media.SoundPool;
Adam Cohenf7522022012-10-03 20:03:18 -070036import android.os.Bundle;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -080037import android.os.Handler;
Jeff Brown109025d2012-08-14 20:41:30 -070038import android.os.Looper;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -080039import android.os.Message;
40import android.os.PowerManager;
Dianne Hackborn4994c662009-09-23 22:21:23 -070041import android.os.RemoteException;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -080042import android.os.SystemClock;
Jim Miller1b152022009-10-28 16:08:15 -070043import android.os.SystemProperties;
Jeff Sharkey6a25cbd2012-08-23 12:14:26 -070044import android.os.UserHandle;
Amith Yamasanib70ff9a2012-09-07 18:28:11 -070045import android.os.UserManager;
Daniel Sandlerdb783bd2010-02-11 15:27:37 -050046import android.provider.Settings;
Karl Rosaenab100082009-03-24 22:35:17 -070047import android.telephony.TelephonyManager;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -080048import android.util.EventLog;
Karl Rosaenab100082009-03-24 22:35:17 -070049import android.util.Log;
Jim Miller25190572013-02-28 17:36:24 -080050import android.util.Slog;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -080051import android.view.KeyEvent;
Jeff Brown98365d72012-08-19 20:30:52 -070052import android.view.WindowManager;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -080053import android.view.WindowManagerPolicy;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -080054
Adam Cohenf7522022012-10-03 20:03:18 -070055import com.android.internal.telephony.IccCardConstants;
56import com.android.internal.widget.LockPatternUtils;
57
Wink Saville37c124c2009-04-02 01:37:02 -070058
The Android Open Source Project1f838aa2009-03-03 19:32:13 -080059/**
60 * Mediates requests related to the keyguard. This includes queries about the
61 * state of the keyguard, power management events that effect whether the keyguard
62 * should be shown or reset, callbacks to the phone window manager to notify
63 * it of when the keyguard is showing, and events from the keyguard view itself
64 * stating that the keyguard was succesfully unlocked.
65 *
66 * Note that the keyguard view is shown when the screen is off (as appropriate)
67 * so that once the screen comes on, it will be ready immediately.
68 *
69 * Example queries about the keyguard:
70 * - is {movement, key} one that should wake the keygaurd?
71 * - is the keyguard showing?
72 * - are input events restricted due to the state of the keyguard?
73 *
74 * Callbacks to the phone window manager:
75 * - the keyguard is showing
76 *
77 * Example external events that translate to keyguard view changes:
78 * - screen turned off -> reset the keyguard, and show it so it will be ready
79 * next time the screen turns on
80 * - keyboard is slid open -> if the keyguard is not secure, hide it
81 *
82 * Events from the keyguard view:
83 * - user succesfully unlocked keyguard -> hide keyguard view, and no longer
84 * restrict input events.
85 *
86 * Note: in addition to normal power managment events that effect the state of
87 * whether the keyguard should be showing, external apps and services may request
88 * that the keyguard be disabled via {@link #setKeyguardEnabled(boolean)}. When
89 * false, this will override all other conditions for turning on the keyguard.
90 *
91 * Threading and synchronization:
92 * This class is created by the initialization routine of the {@link WindowManagerPolicy},
93 * and runs on its thread. The keyguard UI is created from that thread in the
94 * constructor of this class. The apis may be called from other threads, including the
Jeff Brown4532e612012-04-05 14:27:12 -070095 * {@link com.android.server.input.InputManagerService}'s and {@link android.view.WindowManager}'s.
The Android Open Source Project1f838aa2009-03-03 19:32:13 -080096 * Therefore, methods on this class are synchronized, and any action that is pointed
97 * directly to the keyguard UI is posted to a {@link Handler} to ensure it is taken on the UI
98 * thread of the keyguard.
99 */
Jim Millerdcb3d842012-08-23 19:18:12 -0700100public class KeyguardViewMediator {
Jim Millerbc4603b2010-08-30 21:21:34 -0700101 private static final int KEYGUARD_DISPLAY_TIMEOUT_DELAY_DEFAULT = 30000;
Jim Miller9b1db682012-10-25 19:31:19 -0700102 final static boolean DEBUG = false;
Joe Onorato431bb222010-10-18 19:13:23 -0400103 private final static boolean DBG_WAKE = false;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800104
105 private final static String TAG = "KeyguardViewMediator";
106
Jim Miller9c20d0e2010-01-20 15:00:23 -0800107 private static final String DELAYED_KEYGUARD_ACTION =
Wink Saville37c124c2009-04-02 01:37:02 -0700108 "com.android.internal.policy.impl.PhoneWindowManager.DELAYED_KEYGUARD";
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800109
110 // used for handler messages
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800111 private static final int SHOW = 2;
112 private static final int HIDE = 3;
113 private static final int RESET = 4;
114 private static final int VERIFY_UNLOCK = 5;
115 private static final int NOTIFY_SCREEN_OFF = 6;
116 private static final int NOTIFY_SCREEN_ON = 7;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800117 private static final int KEYGUARD_DONE = 9;
118 private static final int KEYGUARD_DONE_DRAWING = 10;
Dianne Hackborn39c2d712009-09-22 11:41:31 -0700119 private static final int KEYGUARD_DONE_AUTHENTICATING = 11;
Mike Lockwood9200a392009-11-17 20:25:58 -0500120 private static final int SET_HIDDEN = 12;
Mike Lockwood28569302010-01-28 11:54:40 -0500121 private static final int KEYGUARD_TIMEOUT = 13;
Jim Miller4eeb4f62012-11-08 00:04:29 -0800122 private static final int SHOW_ASSISTANT = 14;
Jim Miller9c20d0e2010-01-20 15:00:23 -0800123
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800124 /**
125 * The default amount of time we stay awake (used for all key input)
126 */
Jim Miller77274692011-01-17 15:28:21 -0800127 protected static final int AWAKE_INTERVAL_DEFAULT_MS = 10000;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800128
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800129 /**
130 * How long to wait after the screen turns off due to timeout before
131 * turning on the keyguard (i.e, the user has this much time to turn
132 * the screen back on without having to face the keyguard).
133 */
Jim Millerbc4603b2010-08-30 21:21:34 -0700134 private static final int KEYGUARD_LOCK_AFTER_DELAY_DEFAULT = 5000;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800135
136 /**
Craig Mautnerf1b67412012-09-19 13:18:29 -0700137 * How long we'll wait for the {@link ViewMediatorCallback#keyguardDoneDrawing()}
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800138 * callback before unblocking a call to {@link #setKeyguardEnabled(boolean)}
139 * that is reenabling the keyguard.
140 */
141 private static final int KEYGUARD_DONE_DRAWING_TIMEOUT_MS = 2000;
Jim Miller9c20d0e2010-01-20 15:00:23 -0800142
Daniel Sandler74d188c2011-08-10 00:00:31 -0400143 /**
Jeff Sharkeyf52c70b2011-08-30 22:05:47 -0700144 * Allow the user to expand the status bar when the keyguard is engaged
145 * (without a pattern or password).
Daniel Sandler74d188c2011-08-10 00:00:31 -0400146 */
Jeff Sharkeyf52c70b2011-08-30 22:05:47 -0700147 private static final boolean ENABLE_INSECURE_STATUS_BAR_EXPAND = true;
Daniel Sandler74d188c2011-08-10 00:00:31 -0400148
Amith Yamasani8cb751b2011-09-30 15:39:41 -0700149 /** The stream type that the lock sounds are tied to. */
Eric Laurent6d517662012-04-23 18:42:39 -0700150 private int mMasterStreamType;
Amith Yamasani8cb751b2011-09-30 15:39:41 -0700151
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800152 private Context mContext;
153 private AlarmManager mAlarmManager;
Amith Yamasani8cb751b2011-09-30 15:39:41 -0700154 private AudioManager mAudioManager;
Mike Lockwood5f892c12009-11-19 23:39:13 -0500155 private StatusBarManager mStatusBarManager;
Joe Onorato0cbda992010-05-02 16:28:15 -0700156 private boolean mShowLockIcon;
157 private boolean mShowingLockIcon;
Chris Wrenf41c61b2012-11-29 15:19:54 -0500158 private boolean mSwitchingUser;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800159
160 private boolean mSystemReady;
Daniel Sandler0060a9b2010-03-15 23:09:57 -0400161
162 // Whether the next call to playSounds() should be skipped. Defaults to
163 // true because the first lock (on boot) should be silent.
164 private boolean mSuppressNextLockSound = true;
165
Jim Miller9c20d0e2010-01-20 15:00:23 -0800166
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800167 /** High level access to the power manager for WakeLocks */
168 private PowerManager mPM;
169
Amith Yamasanib70ff9a2012-09-07 18:28:11 -0700170 /** UserManager for querying number of users */
171 private UserManager mUserManager;
172
John Spurlock43d84512012-11-09 10:27:33 -0500173 /** SearchManager for determining whether or not search assistant is available */
174 private SearchManager mSearchManager;
175
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800176 /**
Mike Lockwood674d3e42009-10-06 09:28:54 -0400177 * Used to keep the device awake while to ensure the keyguard finishes opening before
178 * we sleep.
179 */
180 private PowerManager.WakeLock mShowKeyguardWakeLock;
181
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800182 private KeyguardViewManager mKeyguardViewManager;
183
184 // these are protected by synchronized (this)
185
186 /**
Mike Lockwood5d258b62009-12-02 13:50:45 -0500187 * External apps (like the phone app) can tell us to disable the keygaurd.
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800188 */
189 private boolean mExternallyEnabled = true;
190
191 /**
192 * Remember if an external call to {@link #setKeyguardEnabled} with value
193 * false caused us to hide the keyguard, so that we need to reshow it once
194 * the keygaurd is reenabled with another call with value true.
195 */
196 private boolean mNeedToReshowWhenReenabled = false;
197
198 // cached value of whether we are showing (need to know this to quickly
199 // answer whether the input should be restricted)
200 private boolean mShowing = false;
201
Mike Lockwood09a40402009-11-08 00:33:23 -0500202 // true if the keyguard is hidden by another window
203 private boolean mHidden = false;
204
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800205 /**
206 * Helps remember whether the screen has turned on since the last time
207 * it turned off due to timeout. see {@link #onScreenTurnedOff(int)}
208 */
209 private int mDelayedShowingSequence;
210
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800211 /**
212 * If the user has disabled the keyguard, then requests to exit, this is
213 * how we'll ultimately let them know whether it was successful. We use this
214 * var being non-null as an indicator that there is an in progress request.
215 */
Jim Miller25190572013-02-28 17:36:24 -0800216 private IKeyguardExitCallback mExitSecureCallback;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800217
218 // the properties of the keyguard
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800219
220 private KeyguardUpdateMonitor mUpdateMonitor;
221
Daniel Hansson4b716862012-03-29 11:02:05 +0200222 private boolean mScreenOn;
Karl Rosaenab100082009-03-24 22:35:17 -0700223
Daniel Sandlerf2d8e742010-02-22 13:09:48 -0500224 // last known state of the cellular connection
225 private String mPhoneState = TelephonyManager.EXTRA_STATE_IDLE;
226
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800227 /**
The Android Open Source Projectc84bf282009-03-09 11:52:14 -0700228 * we send this intent when the keyguard is dismissed.
229 */
Jim Miller17f509a2013-02-28 18:36:12 -0800230 private static final Intent USER_PRESENT_INTENT = new Intent(Intent.ACTION_USER_PRESENT)
231 .addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING
232 | Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
The Android Open Source Projectc84bf282009-03-09 11:52:14 -0700233
234 /**
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800235 * {@link #setKeyguardEnabled} waits on this condition when it reenables
236 * the keyguard.
237 */
238 private boolean mWaitingUntilKeyguardVisible = false;
Jim Millerbc4603b2010-08-30 21:21:34 -0700239 private LockPatternUtils mLockPatternUtils;
John Spurlock34c4fe52012-11-07 10:12:29 -0500240 private boolean mKeyguardDonePending = false;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800241
Marco Nelissend5545bd2011-09-29 12:49:17 -0700242 private SoundPool mLockSounds;
243 private int mLockSoundId;
244 private int mUnlockSoundId;
245 private int mLockSoundStreamId;
Jean-Michel Trivic55b3932012-06-05 11:57:59 -0700246
247 /**
248 * The volume applied to the lock/unlock sounds.
249 */
250 private final float mLockSoundVolume;
Marco Nelissend5545bd2011-09-29 12:49:17 -0700251
Jim Millerdcb3d842012-08-23 19:18:12 -0700252 /**
Amith Yamasani6fc1d4e2013-05-08 16:43:58 -0700253 * Cache of avatar drawables, for use by KeyguardMultiUserAvatar.
254 */
255 private static MultiUserAvatarCache sMultiUserAvatarCache = new MultiUserAvatarCache();
256
257 /**
Jim Millerdcb3d842012-08-23 19:18:12 -0700258 * The callback used by the keyguard view to tell the {@link KeyguardViewMediator}
259 * various things.
260 */
261 public interface ViewMediatorCallback {
Jim Millerdcb3d842012-08-23 19:18:12 -0700262 /**
Jeff Brownc7505bc2012-10-05 21:58:15 -0700263 * Reports user activity and requests that the screen stay on.
264 */
265 void userActivity();
266
267 /**
268 * Reports user activity and requests that the screen stay on for at least
269 * the specified amount of time.
270 * @param millis The amount of time in millis. This value is currently ignored.
Jim Millerdcb3d842012-08-23 19:18:12 -0700271 */
Jeff Brown3dc524b2012-09-30 19:49:11 -0700272 void userActivity(long millis);
Jim Millerdcb3d842012-08-23 19:18:12 -0700273
274 /**
275 * Report that the keyguard is done.
276 * @param authenticated Whether the user securely got past the keyguard.
277 * the only reason for this to be false is if the keyguard was instructed
278 * to appear temporarily to verify the user is supposed to get past the
279 * keyguard, and the user fails to do so.
280 */
281 void keyguardDone(boolean authenticated);
282
283 /**
284 * Report that the keyguard is done drawing.
285 */
286 void keyguardDoneDrawing();
287
288 /**
289 * Tell ViewMediator that the current view needs IME input
290 * @param needsInput
291 */
292 void setNeedsInput(boolean needsInput);
Jeff Brownc7505bc2012-10-05 21:58:15 -0700293
294 /**
295 * Tell view mediator that the keyguard view's desired user activity timeout
296 * has changed and needs to be reapplied to the window.
297 */
298 void onUserActivityTimeoutChanged();
John Spurlock34c4fe52012-11-07 10:12:29 -0500299
300 /**
301 * Report that the keyguard is dismissable, pending the next keyguardDone call.
302 */
303 void keyguardDonePending();
Jim Millerdcb3d842012-08-23 19:18:12 -0700304 }
305
Jim Millerbbf1a742012-07-17 18:30:30 -0700306 KeyguardUpdateMonitorCallback mUpdateCallback = new KeyguardUpdateMonitorCallback() {
307
308 @Override
Chris Wrenf41c61b2012-11-29 15:19:54 -0500309 public void onUserSwitching(int userId) {
Craig Mautnerf1b67412012-09-19 13:18:29 -0700310 // Note that the mLockPatternUtils user has already been updated from setCurrentUser.
Adam Cohenf7522022012-10-03 20:03:18 -0700311 // We need to force a reset of the views, since lockNow (called by
312 // ActivityManagerService) will not reconstruct the keyguard if it is already showing.
Jim Millerbbf1a742012-07-17 18:30:30 -0700313 synchronized (KeyguardViewMediator.this) {
Chris Wrenf41c61b2012-11-29 15:19:54 -0500314 mSwitchingUser = true;
Winson Chungf7614fc2012-11-26 14:43:24 -0800315 resetStateLocked(null);
John Spurlock4e6922d2012-10-04 14:51:51 -0400316 adjustStatusBarLocked();
Brian Colonnaa5239892013-04-15 11:45:40 -0400317 // When we switch users we want to bring the new user to the biometric unlock even
318 // if the current user has gone to the backup.
319 KeyguardUpdateMonitor.getInstance(mContext).setAlternateUnlockEnabled(true);
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700320 }
Jim Millerbbf1a742012-07-17 18:30:30 -0700321 }
322
323 @Override
Chris Wrenf41c61b2012-11-29 15:19:54 -0500324 public void onUserSwitchComplete(int userId) {
325 mSwitchingUser = false;
326 }
327
328 @Override
Jim Millerbbf1a742012-07-17 18:30:30 -0700329 public void onUserRemoved(int userId) {
330 mLockPatternUtils.removeUser(userId);
Amith Yamasani6fc1d4e2013-05-08 16:43:58 -0700331 sMultiUserAvatarCache.clear(userId);
332 }
333
334 @Override
335 public void onUserInfoChanged(int userId) {
336 sMultiUserAvatarCache.clear(userId);
Jim Millerbbf1a742012-07-17 18:30:30 -0700337 }
338
339 @Override
340 void onPhoneStateChanged(int phoneState) {
341 synchronized (KeyguardViewMediator.this) {
342 if (TelephonyManager.CALL_STATE_IDLE == phoneState // call ending
343 && !mScreenOn // screen off
344 && mExternallyEnabled) { // not disabled by any app
345
346 // note: this is a way to gracefully reenable the keyguard when the call
347 // ends and the screen is off without always reenabling the keyguard
348 // each time the screen turns off while in call (and having an occasional ugly
349 // flicker while turning back on the screen and disabling the keyguard again).
350 if (DEBUG) Log.d(TAG, "screen is off and call ended, let's make sure the "
351 + "keyguard is showing");
Jim Miller5ecd8112013-01-09 18:50:26 -0800352 doKeyguardLocked(null);
Jim Millerbbf1a742012-07-17 18:30:30 -0700353 }
354 }
355 };
Jim Millerb0304762012-03-13 20:01:25 -0700356
357 @Override
358 public void onClockVisibilityChanged() {
359 adjustStatusBarLocked();
360 }
361
362 @Override
363 public void onDeviceProvisioned() {
Jim Miller3fd47af2012-09-21 19:55:27 -0700364 sendUserPresentBroadcast();
Jim Millerb0304762012-03-13 20:01:25 -0700365 }
366
Jim Millerbbf1a742012-07-17 18:30:30 -0700367 @Override
368 public void onSimStateChanged(IccCardConstants.State simState) {
369 if (DEBUG) Log.d(TAG, "onSimStateChanged: " + simState);
370
371 switch (simState) {
372 case NOT_READY:
373 case ABSENT:
374 // only force lock screen in case of missing sim if user hasn't
375 // gone through setup wizard
376 synchronized (this) {
377 if (!mUpdateMonitor.isDeviceProvisioned()) {
378 if (!isShowing()) {
379 if (DEBUG) Log.d(TAG, "ICC_ABSENT isn't showing,"
380 + " we need to show the keyguard since the "
381 + "device isn't provisioned yet.");
Jim Miller5ecd8112013-01-09 18:50:26 -0800382 doKeyguardLocked(null);
Jim Millerbbf1a742012-07-17 18:30:30 -0700383 } else {
Adam Cohenf7522022012-10-03 20:03:18 -0700384 resetStateLocked(null);
Jim Millerbbf1a742012-07-17 18:30:30 -0700385 }
386 }
387 }
388 break;
389 case PIN_REQUIRED:
390 case PUK_REQUIRED:
391 synchronized (this) {
392 if (!isShowing()) {
393 if (DEBUG) Log.d(TAG, "INTENT_VALUE_ICC_LOCKED and keygaurd isn't "
394 + "showing; need to show keyguard so user can enter sim pin");
Jim Miller5ecd8112013-01-09 18:50:26 -0800395 doKeyguardLocked(null);
Jim Millerbbf1a742012-07-17 18:30:30 -0700396 } else {
Adam Cohenf7522022012-10-03 20:03:18 -0700397 resetStateLocked(null);
Jim Millerbbf1a742012-07-17 18:30:30 -0700398 }
399 }
400 break;
401 case PERM_DISABLED:
402 synchronized (this) {
403 if (!isShowing()) {
404 if (DEBUG) Log.d(TAG, "PERM_DISABLED and "
405 + "keygaurd isn't showing.");
Jim Miller5ecd8112013-01-09 18:50:26 -0800406 doKeyguardLocked(null);
Jim Millerbbf1a742012-07-17 18:30:30 -0700407 } else {
408 if (DEBUG) Log.d(TAG, "PERM_DISABLED, resetStateLocked to"
409 + "show permanently disabled message in lockscreen.");
Adam Cohenf7522022012-10-03 20:03:18 -0700410 resetStateLocked(null);
Jim Millerbbf1a742012-07-17 18:30:30 -0700411 }
412 }
413 break;
414 case READY:
415 synchronized (this) {
416 if (isShowing()) {
Adam Cohenf7522022012-10-03 20:03:18 -0700417 resetStateLocked(null);
Jim Millerbbf1a742012-07-17 18:30:30 -0700418 }
419 }
420 break;
421 }
422 }
423
Jim Millerb0304762012-03-13 20:01:25 -0700424 };
425
Jim Millerdcb3d842012-08-23 19:18:12 -0700426 ViewMediatorCallback mViewMediatorCallback = new ViewMediatorCallback() {
Jeff Brownc7505bc2012-10-05 21:58:15 -0700427 public void userActivity() {
428 KeyguardViewMediator.this.userActivity();
429 }
430
Jeff Brown3dc524b2012-09-30 19:49:11 -0700431 public void userActivity(long holdMs) {
432 KeyguardViewMediator.this.userActivity(holdMs);
Jim Millerdcb3d842012-08-23 19:18:12 -0700433 }
434
435 public void keyguardDone(boolean authenticated) {
436 KeyguardViewMediator.this.keyguardDone(authenticated, true);
437 }
438
439 public void keyguardDoneDrawing() {
440 mHandler.sendEmptyMessage(KEYGUARD_DONE_DRAWING);
441 }
442
443 @Override
444 public void setNeedsInput(boolean needsInput) {
445 mKeyguardViewManager.setNeedsInput(needsInput);
446 }
Jeff Brownc7505bc2012-10-05 21:58:15 -0700447
448 @Override
449 public void onUserActivityTimeoutChanged() {
450 mKeyguardViewManager.updateUserActivityTimeout();
451 }
John Spurlock34c4fe52012-11-07 10:12:29 -0500452
453 @Override
454 public void keyguardDonePending() {
455 mKeyguardDonePending = true;
456 }
Jim Millerdcb3d842012-08-23 19:18:12 -0700457 };
458
Jim Miller25190572013-02-28 17:36:24 -0800459 private void userActivity() {
Jeff Brown3dc524b2012-09-30 19:49:11 -0700460 userActivity(AWAKE_INTERVAL_DEFAULT_MS);
461 }
462
463 public void userActivity(long holdMs) {
464 // We ignore the hold time. Eventually we should remove it.
465 // Instead, the keyguard window has an explicit user activity timeout set on it.
466 mPM.userActivity(SystemClock.uptimeMillis(), false);
Jim Millerdcb3d842012-08-23 19:18:12 -0700467 }
468
469 /**
470 * Construct a KeyguardViewMediator
471 * @param context
472 * @param lockPatternUtils optional mock interface for LockPatternUtils
473 */
474 public KeyguardViewMediator(Context context, LockPatternUtils lockPatternUtils) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800475 mContext = context;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800476 mPM = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
Amith Yamasanib70ff9a2012-09-07 18:28:11 -0700477 mUserManager = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
Mike Lockwood674d3e42009-10-06 09:28:54 -0400478 mShowKeyguardWakeLock = mPM.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "show keyguard");
479 mShowKeyguardWakeLock.setReferenceCounted(false);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800480
Jim Millerbbf1a742012-07-17 18:30:30 -0700481 mContext.registerReceiver(mBroadcastReceiver, new IntentFilter(DELAYED_KEYGUARD_ACTION));
482
483 mAlarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800484
Jim Millerdcb3d842012-08-23 19:18:12 -0700485 mUpdateMonitor = KeyguardUpdateMonitor.getInstance(context);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800486
Jim Millerdcb3d842012-08-23 19:18:12 -0700487 mLockPatternUtils = lockPatternUtils != null
488 ? lockPatternUtils : new LockPatternUtils(mContext);
Craig Mautnerf1b67412012-09-19 13:18:29 -0700489 mLockPatternUtils.setCurrentUser(UserHandle.USER_OWNER);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800490
Jeff Brown98365d72012-08-19 20:30:52 -0700491 WindowManager wm = (WindowManager)context.getSystemService(Context.WINDOW_SERVICE);
Jim Millerdcb3d842012-08-23 19:18:12 -0700492
493 mKeyguardViewManager = new KeyguardViewManager(context, wm, mViewMediatorCallback,
494 mLockPatternUtils);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800495
Daniel Sandler687a3272010-03-13 15:44:47 -0500496 final ContentResolver cr = mContext.getContentResolver();
497 mShowLockIcon = (Settings.System.getInt(cr, "show_status_bar_lock", 0) == 1);
Marco Nelissend5545bd2011-09-29 12:49:17 -0700498
Daniel Hansson4b716862012-03-29 11:02:05 +0200499 mScreenOn = mPM.isScreenOn();
500
Marco Nelissend5545bd2011-09-29 12:49:17 -0700501 mLockSounds = new SoundPool(1, AudioManager.STREAM_SYSTEM, 0);
Jim Millerb14288d2012-09-30 18:25:05 -0700502 String soundPath = Settings.Global.getString(cr, Settings.Global.LOCK_SOUND);
Marco Nelissend5545bd2011-09-29 12:49:17 -0700503 if (soundPath != null) {
504 mLockSoundId = mLockSounds.load(soundPath, 1);
505 }
506 if (soundPath == null || mLockSoundId == 0) {
Jim Millerb14288d2012-09-30 18:25:05 -0700507 Log.w(TAG, "failed to load lock sound from " + soundPath);
Marco Nelissend5545bd2011-09-29 12:49:17 -0700508 }
Jim Millerb14288d2012-09-30 18:25:05 -0700509 soundPath = Settings.Global.getString(cr, Settings.Global.UNLOCK_SOUND);
Marco Nelissend5545bd2011-09-29 12:49:17 -0700510 if (soundPath != null) {
511 mUnlockSoundId = mLockSounds.load(soundPath, 1);
512 }
513 if (soundPath == null || mUnlockSoundId == 0) {
Jim Millerb14288d2012-09-30 18:25:05 -0700514 Log.w(TAG, "failed to load unlock sound from " + soundPath);
Marco Nelissend5545bd2011-09-29 12:49:17 -0700515 }
Jean-Michel Trivic55b3932012-06-05 11:57:59 -0700516 int lockSoundDefaultAttenuation = context.getResources().getInteger(
517 com.android.internal.R.integer.config_lockSoundVolumeDb);
Jean-Michel Trivif2b0c112012-07-09 11:59:11 -0700518 mLockSoundVolume = (float)Math.pow(10, (float)lockSoundDefaultAttenuation/20);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800519 }
520
521 /**
522 * Let us know that the system is ready after startup.
523 */
524 public void onSystemReady() {
John Spurlock43d84512012-11-09 10:27:33 -0500525 mSearchManager = (SearchManager) mContext.getSystemService(Context.SEARCH_SERVICE);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800526 synchronized (this) {
527 if (DEBUG) Log.d(TAG, "onSystemReady");
528 mSystemReady = true;
Jim Millerbbf1a742012-07-17 18:30:30 -0700529 mUpdateMonitor.registerCallback(mUpdateCallback);
Jim Miller08697702012-10-22 16:49:52 -0700530
Brian Colonnad4a3e5d2012-11-01 16:20:24 -0400531 // Suppress biometric unlock right after boot until things have settled if it is the
532 // selected security method, otherwise unsuppress it. It must be unsuppressed if it is
533 // not the selected security method for the following reason: if the user starts
534 // without a screen lock selected, the biometric unlock would be suppressed the first
535 // time they try to use it.
536 //
537 // Note that the biometric unlock will still not show if it is not the selected method.
538 // Calling setAlternateUnlockEnabled(true) simply says don't suppress it if it is the
539 // selected method.
540 if (mLockPatternUtils.usingBiometricWeak()
541 && mLockPatternUtils.isBiometricWeakInstalled()) {
542 if (DEBUG) Log.d(TAG, "suppressing biometric unlock during boot");
543 mUpdateMonitor.setAlternateUnlockEnabled(false);
544 } else {
545 mUpdateMonitor.setAlternateUnlockEnabled(true);
546 }
Jim Miller08697702012-10-22 16:49:52 -0700547
Jim Miller5ecd8112013-01-09 18:50:26 -0800548 doKeyguardLocked(null);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800549 }
Jim Miller3fd47af2012-09-21 19:55:27 -0700550 // Most services aren't available until the system reaches the ready state, so we
551 // send it here when the device first boots.
552 maybeSendUserPresentBroadcast();
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800553 }
554
555 /**
556 * Called to let us know the screen was turned off.
Mike Lockwoodce277762009-12-03 08:41:44 -0500557 * @param why either {@link WindowManagerPolicy#OFF_BECAUSE_OF_USER},
558 * {@link WindowManagerPolicy#OFF_BECAUSE_OF_TIMEOUT} or
559 * {@link WindowManagerPolicy#OFF_BECAUSE_OF_PROX_SENSOR}.
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800560 */
561 public void onScreenTurnedOff(int why) {
562 synchronized (this) {
Karl Rosaenab100082009-03-24 22:35:17 -0700563 mScreenOn = false;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800564 if (DEBUG) Log.d(TAG, "onScreenTurnedOff(" + why + ")");
565
John Spurlock14adfe42012-11-08 10:29:26 -0500566 mKeyguardDonePending = false;
567
Jim Millera4edd152012-01-06 18:24:04 -0800568 // Lock immediately based on setting if secure (user has a pin/pattern/password).
569 // This also "locks" the device when not secure to provide easy access to the
570 // camera while preventing unwanted input.
571 final boolean lockImmediately =
572 mLockPatternUtils.getPowerButtonInstantlyLocks() || !mLockPatternUtils.isSecure();
573
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800574 if (mExitSecureCallback != null) {
575 if (DEBUG) Log.d(TAG, "pending exit secure callback cancelled");
Jim Miller5ecd8112013-01-09 18:50:26 -0800576 try {
577 mExitSecureCallback.onKeyguardExitResult(false);
578 } catch (RemoteException e) {
Jim Miller25190572013-02-28 17:36:24 -0800579 Slog.w(TAG, "Failed to call onKeyguardExitResult(false)", e);
Jim Miller5ecd8112013-01-09 18:50:26 -0800580 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800581 mExitSecureCallback = null;
582 if (!mExternallyEnabled) {
Jim Miller9c20d0e2010-01-20 15:00:23 -0800583 hideLocked();
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800584 }
585 } else if (mShowing) {
586 notifyScreenOffLocked();
Adam Cohenf7522022012-10-03 20:03:18 -0700587 resetStateLocked(null);
Jim Millera4edd152012-01-06 18:24:04 -0800588 } else if (why == WindowManagerPolicy.OFF_BECAUSE_OF_TIMEOUT
589 || (why == WindowManagerPolicy.OFF_BECAUSE_OF_USER && !lockImmediately)) {
Jeff Brown6aaf2952012-10-05 16:01:08 -0700590 doKeyguardLaterLocked();
Mike Lockwoodce277762009-12-03 08:41:44 -0500591 } else if (why == WindowManagerPolicy.OFF_BECAUSE_OF_PROX_SENSOR) {
592 // Do not enable the keyguard if the prox sensor forced the screen off.
Mike Lockwood016e3972009-09-17 11:45:06 -0400593 } else {
Jim Miller5ecd8112013-01-09 18:50:26 -0800594 doKeyguardLocked(null);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800595 }
596 }
597 }
598
Jeff Brown6aaf2952012-10-05 16:01:08 -0700599 private void doKeyguardLaterLocked() {
600 // if the screen turned off because of timeout or the user hit the power button
601 // and we don't need to lock immediately, set an alarm
602 // to enable it a little bit later (i.e, give the user a chance
603 // to turn the screen back on within a certain window without
604 // having to unlock the screen)
605 final ContentResolver cr = mContext.getContentResolver();
606
607 // From DisplaySettings
608 long displayTimeout = Settings.System.getInt(cr, SCREEN_OFF_TIMEOUT,
609 KEYGUARD_DISPLAY_TIMEOUT_DELAY_DEFAULT);
610
611 // From SecuritySettings
612 final long lockAfterTimeout = Settings.Secure.getInt(cr,
613 Settings.Secure.LOCK_SCREEN_LOCK_AFTER_TIMEOUT,
614 KEYGUARD_LOCK_AFTER_DELAY_DEFAULT);
615
616 // From DevicePolicyAdmin
617 final long policyTimeout = mLockPatternUtils.getDevicePolicyManager()
618 .getMaximumTimeToLock(null, mLockPatternUtils.getCurrentUser());
619
620 long timeout;
621 if (policyTimeout > 0) {
622 // policy in effect. Make sure we don't go beyond policy limit.
623 displayTimeout = Math.max(displayTimeout, 0); // ignore negative values
624 timeout = Math.min(policyTimeout - displayTimeout, lockAfterTimeout);
625 } else {
626 timeout = lockAfterTimeout;
627 }
628
629 if (timeout <= 0) {
630 // Lock now
631 mSuppressNextLockSound = true;
Jim Miller5ecd8112013-01-09 18:50:26 -0800632 doKeyguardLocked(null);
Jeff Brown6aaf2952012-10-05 16:01:08 -0700633 } else {
634 // Lock in the future
635 long when = SystemClock.elapsedRealtime() + timeout;
636 Intent intent = new Intent(DELAYED_KEYGUARD_ACTION);
637 intent.putExtra("seq", mDelayedShowingSequence);
638 PendingIntent sender = PendingIntent.getBroadcast(mContext,
639 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
640 mAlarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, when, sender);
641 if (DEBUG) Log.d(TAG, "setting alarm to turn off keyguard, seq = "
642 + mDelayedShowingSequence);
643 }
644 }
645
646 private void cancelDoKeyguardLaterLocked() {
647 mDelayedShowingSequence++;
648 }
649
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800650 /**
651 * Let's us know the screen was turned on.
652 */
Jim Miller25190572013-02-28 17:36:24 -0800653 public void onScreenTurnedOn(IKeyguardShowCallback callback) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800654 synchronized (this) {
Karl Rosaenab100082009-03-24 22:35:17 -0700655 mScreenOn = true;
Jeff Brown6aaf2952012-10-05 16:01:08 -0700656 cancelDoKeyguardLaterLocked();
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800657 if (DEBUG) Log.d(TAG, "onScreenTurnedOn, seq = " + mDelayedShowingSequence);
Jim Miller25190572013-02-28 17:36:24 -0800658 if (callback != null) {
659 notifyScreenOnLocked(callback);
Jim Millerd6523da2012-10-21 16:47:02 -0700660 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800661 }
Jim Miller3fd47af2012-09-21 19:55:27 -0700662 maybeSendUserPresentBroadcast();
663 }
664
665 private void maybeSendUserPresentBroadcast() {
666 if (mSystemReady && mLockPatternUtils.isLockScreenDisabled()
667 && mUserManager.getUsers(true).size() == 1) {
668 // Lock screen is disabled because the user has set the preference to "None".
669 // In this case, send out ACTION_USER_PRESENT here instead of in
670 // handleKeyguardDone()
671 sendUserPresentBroadcast();
672 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800673 }
674
675 /**
Jeff Brown6aaf2952012-10-05 16:01:08 -0700676 * A dream started. We should lock after the usual screen-off lock timeout but only
677 * if there is a secure lock pattern.
678 */
679 public void onDreamingStarted() {
680 synchronized (this) {
681 if (mScreenOn && mLockPatternUtils.isSecure()) {
682 doKeyguardLaterLocked();
683 }
684 }
685 }
686
687 /**
688 * A dream stopped.
689 */
690 public void onDreamingStopped() {
691 synchronized (this) {
692 if (mScreenOn) {
693 cancelDoKeyguardLaterLocked();
694 }
695 }
696 }
697
698 /**
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800699 * Same semantics as {@link WindowManagerPolicy#enableKeyguard}; provide
700 * a way for external stuff to override normal keyguard behavior. For instance
701 * the phone app disables the keyguard when it receives incoming calls.
702 */
703 public void setKeyguardEnabled(boolean enabled) {
704 synchronized (this) {
705 if (DEBUG) Log.d(TAG, "setKeyguardEnabled(" + enabled + ")");
706
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800707 mExternallyEnabled = enabled;
708
709 if (!enabled && mShowing) {
710 if (mExitSecureCallback != null) {
711 if (DEBUG) Log.d(TAG, "in process of verifyUnlock request, ignoring");
712 // we're in the process of handling a request to verify the user
713 // can get past the keyguard. ignore extraneous requests to disable / reenable
714 return;
715 }
716
717 // hiding keyguard that is showing, remember to reshow later
718 if (DEBUG) Log.d(TAG, "remembering to reshow, hiding keyguard, "
719 + "disabling status bar expansion");
720 mNeedToReshowWhenReenabled = true;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800721 hideLocked();
722 } else if (enabled && mNeedToReshowWhenReenabled) {
723 // reenabled after previously hidden, reshow
724 if (DEBUG) Log.d(TAG, "previously hidden, reshowing, reenabling "
725 + "status bar expansion");
726 mNeedToReshowWhenReenabled = false;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800727
728 if (mExitSecureCallback != null) {
729 if (DEBUG) Log.d(TAG, "onKeyguardExitResult(false), resetting");
Jim Miller5ecd8112013-01-09 18:50:26 -0800730 try {
731 mExitSecureCallback.onKeyguardExitResult(false);
732 } catch (RemoteException e) {
Jim Miller25190572013-02-28 17:36:24 -0800733 Slog.w(TAG, "Failed to call onKeyguardExitResult(false)", e);
Jim Miller5ecd8112013-01-09 18:50:26 -0800734 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800735 mExitSecureCallback = null;
Adam Cohenf7522022012-10-03 20:03:18 -0700736 resetStateLocked(null);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800737 } else {
Adam Cohenf7522022012-10-03 20:03:18 -0700738 showLocked(null);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800739
740 // block until we know the keygaurd is done drawing (and post a message
741 // to unblock us after a timeout so we don't risk blocking too long
742 // and causing an ANR).
743 mWaitingUntilKeyguardVisible = true;
744 mHandler.sendEmptyMessageDelayed(KEYGUARD_DONE_DRAWING, KEYGUARD_DONE_DRAWING_TIMEOUT_MS);
745 if (DEBUG) Log.d(TAG, "waiting until mWaitingUntilKeyguardVisible is false");
746 while (mWaitingUntilKeyguardVisible) {
747 try {
748 wait();
749 } catch (InterruptedException e) {
750 Thread.currentThread().interrupt();
751 }
752 }
753 if (DEBUG) Log.d(TAG, "done waiting for mWaitingUntilKeyguardVisible");
754 }
755 }
756 }
757 }
758
759 /**
760 * @see android.app.KeyguardManager#exitKeyguardSecurely
761 */
Jim Miller25190572013-02-28 17:36:24 -0800762 public void verifyUnlock(IKeyguardExitCallback callback) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800763 synchronized (this) {
764 if (DEBUG) Log.d(TAG, "verifyUnlock");
765 if (!mUpdateMonitor.isDeviceProvisioned()) {
766 // don't allow this api when the device isn't provisioned
767 if (DEBUG) Log.d(TAG, "ignoring because device isn't provisioned");
Jim Miller5ecd8112013-01-09 18:50:26 -0800768 try {
Jim Miller25190572013-02-28 17:36:24 -0800769 callback.onKeyguardExitResult(false);
Jim Miller5ecd8112013-01-09 18:50:26 -0800770 } catch (RemoteException e) {
Jim Miller25190572013-02-28 17:36:24 -0800771 Slog.w(TAG, "Failed to call onKeyguardExitResult(false)", e);
Jim Miller5ecd8112013-01-09 18:50:26 -0800772 }
Mike Lockwood5d258b62009-12-02 13:50:45 -0500773 } else if (mExternallyEnabled) {
774 // this only applies when the user has externally disabled the
775 // keyguard. this is unexpected and means the user is not
776 // using the api properly.
777 Log.w(TAG, "verifyUnlock called when not externally disabled");
Jim Miller5ecd8112013-01-09 18:50:26 -0800778 try {
Jim Miller25190572013-02-28 17:36:24 -0800779 callback.onKeyguardExitResult(false);
Jim Miller5ecd8112013-01-09 18:50:26 -0800780 } catch (RemoteException e) {
Jim Miller25190572013-02-28 17:36:24 -0800781 Slog.w(TAG, "Failed to call onKeyguardExitResult(false)", e);
Jim Miller5ecd8112013-01-09 18:50:26 -0800782 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800783 } else if (mExitSecureCallback != null) {
784 // already in progress with someone else
Jim Miller5ecd8112013-01-09 18:50:26 -0800785 try {
Jim Miller25190572013-02-28 17:36:24 -0800786 callback.onKeyguardExitResult(false);
Jim Miller5ecd8112013-01-09 18:50:26 -0800787 } catch (RemoteException e) {
Jim Miller25190572013-02-28 17:36:24 -0800788 Slog.w(TAG, "Failed to call onKeyguardExitResult(false)", e);
Jim Miller5ecd8112013-01-09 18:50:26 -0800789 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800790 } else {
Jim Miller25190572013-02-28 17:36:24 -0800791 mExitSecureCallback = callback;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800792 verifyUnlockLocked();
793 }
794 }
795 }
796
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800797 /**
798 * Is the keyguard currently showing?
799 */
800 public boolean isShowing() {
801 return mShowing;
802 }
803
804 /**
Dianne Hackborn9dc06cc2009-11-17 18:19:23 -0800805 * Is the keyguard currently showing and not being force hidden?
806 */
807 public boolean isShowingAndNotHidden() {
808 return mShowing && !mHidden;
809 }
810
811 /**
Mike Lockwood09a40402009-11-08 00:33:23 -0500812 * Notify us when the keyguard is hidden by another window
813 */
814 public void setHidden(boolean isHidden) {
Mike Lockwoodb17b2fb2009-11-09 16:01:37 -0500815 if (DEBUG) Log.d(TAG, "setHidden " + isHidden);
Danielle Millettf6d0fc12012-10-23 16:16:52 -0400816 mUpdateMonitor.sendKeyguardVisibilityChanged(!isHidden);
Mike Lockwood9200a392009-11-17 20:25:58 -0500817 mHandler.removeMessages(SET_HIDDEN);
818 Message msg = mHandler.obtainMessage(SET_HIDDEN, (isHidden ? 1 : 0), 0);
819 mHandler.sendMessage(msg);
820 }
821
822 /**
823 * Handles SET_HIDDEN message sent by setHidden()
824 */
825 private void handleSetHidden(boolean isHidden) {
Mike Lockwood09a40402009-11-08 00:33:23 -0500826 synchronized (KeyguardViewMediator.this) {
Mike Lockwood9200a392009-11-17 20:25:58 -0500827 if (mHidden != isHidden) {
828 mHidden = isHidden;
Dianne Hackbornff5b1582012-04-12 17:24:07 -0700829 updateActivityLockScreenState();
Mike Lockwood5f892c12009-11-19 23:39:13 -0500830 adjustStatusBarLocked();
Mike Lockwood9200a392009-11-17 20:25:58 -0500831 }
Mike Lockwood09a40402009-11-08 00:33:23 -0500832 }
833 }
834
835 /**
Mike Lockwood28569302010-01-28 11:54:40 -0500836 * Used by PhoneWindowManager to enable the keyguard due to a user activity timeout.
837 * This must be safe to call from any thread and with any window manager locks held.
838 */
Adam Cohenf7522022012-10-03 20:03:18 -0700839 public void doKeyguardTimeout(Bundle options) {
Mike Lockwood28569302010-01-28 11:54:40 -0500840 mHandler.removeMessages(KEYGUARD_TIMEOUT);
Adam Cohenf7522022012-10-03 20:03:18 -0700841 Message msg = mHandler.obtainMessage(KEYGUARD_TIMEOUT, options);
Mike Lockwood28569302010-01-28 11:54:40 -0500842 mHandler.sendMessage(msg);
843 }
844
845 /**
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800846 * Given the state of the keyguard, is the input restricted?
847 * Input is restricted when the keyguard is showing, or when the keyguard
848 * was suppressed by an app that disabled the keyguard or we haven't been provisioned yet.
849 */
850 public boolean isInputRestricted() {
851 return mShowing || mNeedToReshowWhenReenabled || !mUpdateMonitor.isDeviceProvisioned();
852 }
853
Dianne Hackbornb446e972009-09-20 15:23:25 -0700854 /**
Craig Mautnerad09bcc2012-10-08 13:33:11 -0700855 * Enable the keyguard if the settings are appropriate.
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800856 */
Adam Cohenf7522022012-10-03 20:03:18 -0700857 private void doKeyguardLocked(Bundle options) {
Dianne Hackborn29aae6f2011-08-18 18:30:09 -0700858 // if another app is disabling us, don't show
859 if (!mExternallyEnabled) {
860 if (DEBUG) Log.d(TAG, "doKeyguard: not showing because externally disabled");
Karl Rosaenab100082009-03-24 22:35:17 -0700861
Dianne Hackborn29aae6f2011-08-18 18:30:09 -0700862 // note: we *should* set mNeedToReshowWhenReenabled=true here, but that makes
863 // for an occasional ugly flicker in this situation:
864 // 1) receive a call with the screen on (no keyguard) or make a call
865 // 2) screen times out
866 // 3) user hits key to turn screen back on
867 // instead, we reenable the keyguard when we know the screen is off and the call
868 // ends (see the broadcast receiver below)
869 // TODO: clean this up when we have better support at the window manager level
870 // for apps that wish to be on top of the keyguard
Dianne Hackborn38e29a62011-09-18 14:43:08 -0700871 return;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800872 }
Dianne Hackborn29aae6f2011-08-18 18:30:09 -0700873
874 // if the keyguard is already showing, don't bother
875 if (mKeyguardViewManager.isShowing()) {
876 if (DEBUG) Log.d(TAG, "doKeyguard: not showing because it is already showing");
Dianne Hackborn38e29a62011-09-18 14:43:08 -0700877 return;
Dianne Hackborn29aae6f2011-08-18 18:30:09 -0700878 }
879
880 // if the setup wizard hasn't run yet, don't show
881 final boolean requireSim = !SystemProperties.getBoolean("keyguard.no_require_sim",
882 false);
883 final boolean provisioned = mUpdateMonitor.isDeviceProvisioned();
Wink Savillea639b312012-07-10 12:37:54 -0700884 final IccCardConstants.State state = mUpdateMonitor.getSimState();
Dianne Hackborn29aae6f2011-08-18 18:30:09 -0700885 final boolean lockedOrMissing = state.isPinLocked()
Wink Savillea639b312012-07-10 12:37:54 -0700886 || ((state == IccCardConstants.State.ABSENT
887 || state == IccCardConstants.State.PERM_DISABLED)
Jim Miller9fa1ada2011-10-31 18:21:49 -0700888 && requireSim);
Dianne Hackborn29aae6f2011-08-18 18:30:09 -0700889
890 if (!lockedOrMissing && !provisioned) {
891 if (DEBUG) Log.d(TAG, "doKeyguard: not showing because device isn't provisioned"
892 + " and the sim is not locked or missing");
Dianne Hackborn38e29a62011-09-18 14:43:08 -0700893 return;
Dianne Hackborn29aae6f2011-08-18 18:30:09 -0700894 }
895
Amith Yamasani920ace02012-09-20 22:15:37 -0700896 if (mUserManager.getUsers(true).size() < 2
Amith Yamasanib70ff9a2012-09-07 18:28:11 -0700897 && mLockPatternUtils.isLockScreenDisabled() && !lockedOrMissing) {
Dianne Hackborn29aae6f2011-08-18 18:30:09 -0700898 if (DEBUG) Log.d(TAG, "doKeyguard: not showing because lockscreen is off");
Dianne Hackborn38e29a62011-09-18 14:43:08 -0700899 return;
Dianne Hackborn29aae6f2011-08-18 18:30:09 -0700900 }
901
902 if (DEBUG) Log.d(TAG, "doKeyguard: showing the lock screen");
Adam Cohenf7522022012-10-03 20:03:18 -0700903 showLocked(options);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800904 }
905
906 /**
Craig Mautnerad09bcc2012-10-08 13:33:11 -0700907 * Dismiss the keyguard through the security layers.
908 */
909 public void dismiss() {
Jim Miller87d03662012-11-05 20:28:09 -0800910 if (mShowing && !mHidden) {
911 mKeyguardViewManager.dismiss();
912 }
Craig Mautnerad09bcc2012-10-08 13:33:11 -0700913 }
914
915 /**
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800916 * Send message to keyguard telling it to reset its state.
Adam Cohenf7522022012-10-03 20:03:18 -0700917 * @param options options about how to show the keyguard
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800918 * @see #handleReset()
919 */
Adam Cohenf7522022012-10-03 20:03:18 -0700920 private void resetStateLocked(Bundle options) {
Jim Miller4894a012013-04-03 15:23:55 -0700921 if (DEBUG) Log.e(TAG, "resetStateLocked");
Adam Cohenf7522022012-10-03 20:03:18 -0700922 Message msg = mHandler.obtainMessage(RESET, options);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800923 mHandler.sendMessage(msg);
924 }
925
926 /**
927 * Send message to keyguard telling it to verify unlock
928 * @see #handleVerifyUnlock()
929 */
930 private void verifyUnlockLocked() {
931 if (DEBUG) Log.d(TAG, "verifyUnlockLocked");
932 mHandler.sendEmptyMessage(VERIFY_UNLOCK);
933 }
934
935
936 /**
937 * Send a message to keyguard telling it the screen just turned on.
938 * @see #onScreenTurnedOff(int)
939 * @see #handleNotifyScreenOff
940 */
941 private void notifyScreenOffLocked() {
942 if (DEBUG) Log.d(TAG, "notifyScreenOffLocked");
943 mHandler.sendEmptyMessage(NOTIFY_SCREEN_OFF);
944 }
945
946 /**
947 * Send a message to keyguard telling it the screen just turned on.
Jim Miller9c20d0e2010-01-20 15:00:23 -0800948 * @see #onScreenTurnedOn()
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800949 * @see #handleNotifyScreenOn
950 */
Jim Miller25190572013-02-28 17:36:24 -0800951 private void notifyScreenOnLocked(IKeyguardShowCallback result) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800952 if (DEBUG) Log.d(TAG, "notifyScreenOnLocked");
Jim Miller5ecd8112013-01-09 18:50:26 -0800953 Message msg = mHandler.obtainMessage(NOTIFY_SCREEN_ON, result);
Dianne Hackborn38e29a62011-09-18 14:43:08 -0700954 mHandler.sendMessage(msg);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800955 }
956
957 /**
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800958 * Send message to keyguard telling it to show itself
959 * @see #handleShow()
960 */
Adam Cohenf7522022012-10-03 20:03:18 -0700961 private void showLocked(Bundle options) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800962 if (DEBUG) Log.d(TAG, "showLocked");
Mike Lockwood674d3e42009-10-06 09:28:54 -0400963 // ensure we stay awake until we are finished displaying the keyguard
964 mShowKeyguardWakeLock.acquire();
Adam Cohenf7522022012-10-03 20:03:18 -0700965 Message msg = mHandler.obtainMessage(SHOW, options);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800966 mHandler.sendMessage(msg);
967 }
968
969 /**
970 * Send message to keyguard telling it to hide itself
Jim Miller9c20d0e2010-01-20 15:00:23 -0800971 * @see #handleHide()
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800972 */
973 private void hideLocked() {
974 if (DEBUG) Log.d(TAG, "hideLocked");
975 Message msg = mHandler.obtainMessage(HIDE);
976 mHandler.sendMessage(msg);
977 }
978
Dianne Hackbornb446e972009-09-20 15:23:25 -0700979 public boolean isSecure() {
Jim Millerdcb3d842012-08-23 19:18:12 -0700980 return mLockPatternUtils.isSecure()
981 || KeyguardUpdateMonitor.getInstance(mContext).isSimPinSecure();
Dianne Hackbornb446e972009-09-20 15:23:25 -0700982 }
Jim Miller9c20d0e2010-01-20 15:00:23 -0800983
Craig Mautnerf1b67412012-09-19 13:18:29 -0700984 /**
985 * Update the newUserId. Call while holding WindowManagerService lock.
Jim Milleree82f8f2012-10-01 16:26:18 -0700986 * NOTE: Should only be called by KeyguardViewMediator in response to the user id changing.
987 *
Craig Mautnerf1b67412012-09-19 13:18:29 -0700988 * @param newUserId The id of the incoming user.
989 */
990 public void setCurrentUser(int newUserId) {
991 mLockPatternUtils.setCurrentUser(newUserId);
992 }
993
Jim Millerbbf1a742012-07-17 18:30:30 -0700994 private final BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
Amith Yamasani52c489c2012-03-28 11:42:42 -0700995 @Override
996 public void onReceive(Context context, Intent intent) {
Jim Millerbbf1a742012-07-17 18:30:30 -0700997 if (DELAYED_KEYGUARD_ACTION.equals(intent.getAction())) {
998 final int sequence = intent.getIntExtra("seq", 0);
Jim Millerf3447352011-08-07 14:00:09 -0700999 if (DEBUG) Log.d(TAG, "received DELAYED_KEYGUARD_ACTION with seq = "
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001000 + sequence + ", mDelayedShowingSequence = " + mDelayedShowingSequence);
Dianne Hackborn29aae6f2011-08-18 18:30:09 -07001001 synchronized (KeyguardViewMediator.this) {
1002 if (mDelayedShowingSequence == sequence) {
Jim Millerbbf1a742012-07-17 18:30:30 -07001003 // Don't play lockscreen SFX if the screen went off due to timeout.
Dianne Hackborn29aae6f2011-08-18 18:30:09 -07001004 mSuppressNextLockSound = true;
Jim Miller5ecd8112013-01-09 18:50:26 -08001005 doKeyguardLocked(null);
Dianne Hackborn29aae6f2011-08-18 18:30:09 -07001006 }
Daniel Sandlerf2d8e742010-02-22 13:09:48 -05001007 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001008 }
1009 }
1010 };
1011
Dianne Hackborn05726582009-09-22 17:28:24 -07001012 public void keyguardDone(boolean authenticated, boolean wakeup) {
John Spurlock34c4fe52012-11-07 10:12:29 -05001013 mKeyguardDonePending = false;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001014 synchronized (this) {
Jim Miller9c20d0e2010-01-20 15:00:23 -08001015 EventLog.writeEvent(70000, 2);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001016 if (DEBUG) Log.d(TAG, "keyguardDone(" + authenticated + ")");
Jim Millere51cf7ae2013-06-25 18:31:56 -07001017 Message msg = mHandler.obtainMessage(KEYGUARD_DONE, authenticated ? 1 : 0,
1018 wakeup ? 1 : 0);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001019 mHandler.sendMessage(msg);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001020 }
1021 }
1022
1023 /**
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001024 * This handler will be associated with the policy thread, which will also
1025 * be the UI thread of the keyguard. Since the apis of the policy, and therefore
1026 * this class, can be called by other threads, any action that directly
1027 * interacts with the keyguard ui should be posted to this handler, rather
1028 * than called directly.
1029 */
Jim Millerdcb3d842012-08-23 19:18:12 -07001030 private Handler mHandler = new Handler(Looper.myLooper(), null, true /*async*/) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001031 @Override
Wink Saville37c124c2009-04-02 01:37:02 -07001032 public void handleMessage(Message msg) {
1033 switch (msg.what) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001034 case SHOW:
Adam Cohenf7522022012-10-03 20:03:18 -07001035 handleShow((Bundle) msg.obj);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001036 return ;
1037 case HIDE:
1038 handleHide();
1039 return ;
1040 case RESET:
Adam Cohenf7522022012-10-03 20:03:18 -07001041 handleReset((Bundle) msg.obj);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001042 return ;
1043 case VERIFY_UNLOCK:
1044 handleVerifyUnlock();
1045 return;
1046 case NOTIFY_SCREEN_OFF:
1047 handleNotifyScreenOff();
1048 return;
1049 case NOTIFY_SCREEN_ON:
Jim Miller25190572013-02-28 17:36:24 -08001050 handleNotifyScreenOn((IKeyguardShowCallback) msg.obj);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001051 return;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001052 case KEYGUARD_DONE:
Jim Millere51cf7ae2013-06-25 18:31:56 -07001053 handleKeyguardDone(msg.arg1 != 0, msg.arg2 != 0);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001054 return;
1055 case KEYGUARD_DONE_DRAWING:
1056 handleKeyguardDoneDrawing();
Dianne Hackbornb446e972009-09-20 15:23:25 -07001057 return;
Dianne Hackborn39c2d712009-09-22 11:41:31 -07001058 case KEYGUARD_DONE_AUTHENTICATING:
Jim Millerdcb3d842012-08-23 19:18:12 -07001059 keyguardDone(true, true);
Dianne Hackbornb446e972009-09-20 15:23:25 -07001060 return;
Mike Lockwood9200a392009-11-17 20:25:58 -05001061 case SET_HIDDEN:
1062 handleSetHidden(msg.arg1 != 0);
1063 break;
Mike Lockwood28569302010-01-28 11:54:40 -05001064 case KEYGUARD_TIMEOUT:
Dianne Hackborn29aae6f2011-08-18 18:30:09 -07001065 synchronized (KeyguardViewMediator.this) {
Adam Cohenf7522022012-10-03 20:03:18 -07001066 doKeyguardLocked((Bundle) msg.obj);
Dianne Hackborn29aae6f2011-08-18 18:30:09 -07001067 }
1068 break;
Jim Miller4eeb4f62012-11-08 00:04:29 -08001069 case SHOW_ASSISTANT:
1070 handleShowAssistant();
1071 break;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001072 }
1073 }
1074 };
1075
1076 /**
1077 * @see #keyguardDone
1078 * @see #KEYGUARD_DONE
1079 */
Jim Millere51cf7ae2013-06-25 18:31:56 -07001080 private void handleKeyguardDone(boolean authenticated, boolean wakeup) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001081 if (DEBUG) Log.d(TAG, "handleKeyguardDone");
Jim Millere51cf7ae2013-06-25 18:31:56 -07001082
1083 if (authenticated) {
1084 mUpdateMonitor.clearFailedUnlockAttempts();
Dianne Hackborn05726582009-09-22 17:28:24 -07001085 }
Jeff Sharkey6a25cbd2012-08-23 12:14:26 -07001086
Jim Millere51cf7ae2013-06-25 18:31:56 -07001087 if (mExitSecureCallback != null) {
1088 try {
1089 mExitSecureCallback.onKeyguardExitResult(authenticated);
1090 } catch (RemoteException e) {
1091 Slog.w(TAG, "Failed to call onKeyguardExitResult(" + authenticated + ")", e);
1092 }
1093
1094 mExitSecureCallback = null;
1095
1096 if (authenticated) {
1097 // after succesfully exiting securely, no need to reshow
1098 // the keyguard when they've released the lock
1099 mExternallyEnabled = true;
1100 mNeedToReshowWhenReenabled = false;
1101 }
1102 }
1103
1104 handleHide();
Jim Miller3fd47af2012-09-21 19:55:27 -07001105 sendUserPresentBroadcast();
1106 }
1107
1108 private void sendUserPresentBroadcast() {
Jim Miller17f509a2013-02-28 18:36:12 -08001109 final UserHandle currentUser = new UserHandle(mLockPatternUtils.getCurrentUser());
1110 mContext.sendBroadcastAsUser(USER_PRESENT_INTENT, currentUser);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001111 }
1112
1113 /**
1114 * @see #keyguardDoneDrawing
1115 * @see #KEYGUARD_DONE_DRAWING
1116 */
1117 private void handleKeyguardDoneDrawing() {
1118 synchronized(this) {
Jim Miller5ecd8112013-01-09 18:50:26 -08001119 if (DEBUG) Log.d(TAG, "handleKeyguardDoneDrawing");
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001120 if (mWaitingUntilKeyguardVisible) {
1121 if (DEBUG) Log.d(TAG, "handleKeyguardDoneDrawing: notifying mWaitingUntilKeyguardVisible");
1122 mWaitingUntilKeyguardVisible = false;
1123 notifyAll();
1124
1125 // there will usually be two of these sent, one as a timeout, and one
1126 // as a result of the callback, so remove any remaining messages from
1127 // the queue
1128 mHandler.removeMessages(KEYGUARD_DONE_DRAWING);
1129 }
1130 }
1131 }
1132
Daniel Sandlerdb783bd2010-02-11 15:27:37 -05001133 private void playSounds(boolean locked) {
1134 // User feedback for keyguard.
Daniel Sandler0060a9b2010-03-15 23:09:57 -04001135
1136 if (mSuppressNextLockSound) {
1137 mSuppressNextLockSound = false;
1138 return;
1139 }
1140
Daniel Sandlerdb783bd2010-02-11 15:27:37 -05001141 final ContentResolver cr = mContext.getContentResolver();
Amith Yamasani8cb751b2011-09-30 15:39:41 -07001142 if (Settings.System.getInt(cr, Settings.System.LOCKSCREEN_SOUNDS_ENABLED, 1) == 1) {
Marco Nelissend5545bd2011-09-29 12:49:17 -07001143 final int whichSound = locked
1144 ? mLockSoundId
1145 : mUnlockSoundId;
1146 mLockSounds.stop(mLockSoundStreamId);
Amith Yamasani8cb751b2011-09-30 15:39:41 -07001147 // Init mAudioManager
1148 if (mAudioManager == null) {
1149 mAudioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
1150 if (mAudioManager == null) return;
Eric Laurent6d517662012-04-23 18:42:39 -07001151 mMasterStreamType = mAudioManager.getMasterStreamType();
Amith Yamasani8cb751b2011-09-30 15:39:41 -07001152 }
1153 // If the stream is muted, don't play the sound
Eric Laurent6d517662012-04-23 18:42:39 -07001154 if (mAudioManager.isStreamMute(mMasterStreamType)) return;
Amith Yamasani8cb751b2011-09-30 15:39:41 -07001155
Jean-Michel Trivic55b3932012-06-05 11:57:59 -07001156 mLockSoundStreamId = mLockSounds.play(whichSound,
1157 mLockSoundVolume, mLockSoundVolume, 1/*priortiy*/, 0/*loop*/, 1.0f/*rate*/);
Daniel Sandlerdb783bd2010-02-11 15:27:37 -05001158 }
Jim Miller2a98a4c2010-11-19 18:49:26 -08001159 }
Daniel Sandlerdb783bd2010-02-11 15:27:37 -05001160
Dianne Hackbornff5b1582012-04-12 17:24:07 -07001161 private void updateActivityLockScreenState() {
1162 try {
1163 ActivityManagerNative.getDefault().setLockScreenShown(
1164 mShowing && !mHidden);
1165 } catch (RemoteException e) {
1166 }
1167 }
1168
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001169 /**
1170 * Handle message sent by {@link #showLocked}.
1171 * @see #SHOW
1172 */
Adam Cohenf7522022012-10-03 20:03:18 -07001173 private void handleShow(Bundle options) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001174 synchronized (KeyguardViewMediator.this) {
Jim Miller5ecd8112013-01-09 18:50:26 -08001175 if (!mSystemReady) {
1176 if (DEBUG) Log.d(TAG, "ignoring handleShow because system is not ready.");
1177 return;
1178 } else {
1179 if (DEBUG) Log.d(TAG, "handleShow");
1180 }
Jim Miller9c20d0e2010-01-20 15:00:23 -08001181
Adam Cohenf7522022012-10-03 20:03:18 -07001182 mKeyguardViewManager.show(options);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001183 mShowing = true;
John Spurlock14adfe42012-11-08 10:29:26 -05001184 mKeyguardDonePending = false;
Dianne Hackbornff5b1582012-04-12 17:24:07 -07001185 updateActivityLockScreenState();
Mike Lockwood5f892c12009-11-19 23:39:13 -05001186 adjustStatusBarLocked();
Jeff Brown3dc524b2012-09-30 19:49:11 -07001187 userActivity();
Dianne Hackborn4994c662009-09-23 22:21:23 -07001188 try {
1189 ActivityManagerNative.getDefault().closeSystemDialogs("lock");
1190 } catch (RemoteException e) {
1191 }
Dianne Hackborn29aae6f2011-08-18 18:30:09 -07001192
1193 // Do this at the end to not slow down display of the keyguard.
1194 playSounds(true);
1195
Mike Lockwood674d3e42009-10-06 09:28:54 -04001196 mShowKeyguardWakeLock.release();
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001197 }
1198 }
1199
1200 /**
1201 * Handle message sent by {@link #hideLocked()}
1202 * @see #HIDE
1203 */
1204 private void handleHide() {
1205 synchronized (KeyguardViewMediator.this) {
1206 if (DEBUG) Log.d(TAG, "handleHide");
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001207
Daniel Sandlerf2d8e742010-02-22 13:09:48 -05001208 // only play "unlock" noises if not on a call (since the incall UI
1209 // disables the keyguard)
1210 if (TelephonyManager.EXTRA_STATE_IDLE.equals(mPhoneState)) {
1211 playSounds(false);
1212 }
Daniel Sandlerdb783bd2010-02-11 15:27:37 -05001213
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001214 mKeyguardViewManager.hide();
1215 mShowing = false;
John Spurlock14adfe42012-11-08 10:29:26 -05001216 mKeyguardDonePending = false;
Dianne Hackbornff5b1582012-04-12 17:24:07 -07001217 updateActivityLockScreenState();
Mike Lockwood5f892c12009-11-19 23:39:13 -05001218 adjustStatusBarLocked();
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001219 }
1220 }
1221
Mike Lockwood5f892c12009-11-19 23:39:13 -05001222 private void adjustStatusBarLocked() {
1223 if (mStatusBarManager == null) {
1224 mStatusBarManager = (StatusBarManager)
1225 mContext.getSystemService(Context.STATUS_BAR_SERVICE);
1226 }
1227 if (mStatusBarManager == null) {
1228 Log.w(TAG, "Could not get status bar manager");
1229 } else {
Daniel Sandler687a3272010-03-13 15:44:47 -05001230 if (mShowLockIcon) {
1231 // Give feedback to user when secure keyguard is active and engaged
1232 if (mShowing && isSecure()) {
Joe Onorato0cbda992010-05-02 16:28:15 -07001233 if (!mShowingLockIcon) {
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001234 String contentDescription = mContext.getString(
1235 com.android.internal.R.string.status_bar_device_locked);
Joe Onorato0cbda992010-05-02 16:28:15 -07001236 mStatusBarManager.setIcon("secure",
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001237 com.android.internal.R.drawable.stat_sys_secure, 0,
1238 contentDescription);
Joe Onorato0cbda992010-05-02 16:28:15 -07001239 mShowingLockIcon = true;
Daniel Sandler687a3272010-03-13 15:44:47 -05001240 }
1241 } else {
Joe Onorato0cbda992010-05-02 16:28:15 -07001242 if (mShowingLockIcon) {
1243 mStatusBarManager.removeIcon("secure");
1244 mShowingLockIcon = false;
Daniel Sandler687a3272010-03-13 15:44:47 -05001245 }
Daniel Sandlere5fbe9b2010-02-05 13:26:28 -08001246 }
1247 }
1248
Daniel Sandlerdba93562011-10-06 16:39:58 -04001249 // Disable aspects of the system/status/navigation bars that must not be re-enabled by
1250 // windows that appear on top, ever
Jeff Sharkeyf52c70b2011-08-30 22:05:47 -07001251 int flags = StatusBarManager.DISABLE_NONE;
Mike Lockwoode3646dd2011-09-01 12:46:28 -04001252 if (mShowing) {
Jim Millere23ab8b2012-09-16 15:45:44 -07001253 // Permanently disable components not available when keyguard is enabled
1254 // (like recents). Temporary enable/disable (e.g. the "back" button) are
1255 // done in KeyguardHostView.
Daniel Sandlerdba93562011-10-06 16:39:58 -04001256 flags |= StatusBarManager.DISABLE_RECENT;
Mike Lockwoode3646dd2011-09-01 12:46:28 -04001257 if (isSecure() || !ENABLE_INSECURE_STATUS_BAR_EXPAND) {
1258 // showing secure lockscreen; disable expanding.
1259 flags |= StatusBarManager.DISABLE_EXPAND;
1260 }
Jeff Sharkey4519a022011-09-07 23:24:53 -07001261 if (isSecure()) {
1262 // showing secure lockscreen; disable ticker.
1263 flags |= StatusBarManager.DISABLE_NOTIFICATION_TICKER;
1264 }
John Spurlock43d84512012-11-09 10:27:33 -05001265 if (!isAssistantAvailable()) {
1266 flags |= StatusBarManager.DISABLE_SEARCH;
1267 }
Jeff Sharkeyf52c70b2011-08-30 22:05:47 -07001268 }
1269
1270 if (DEBUG) {
Jeff Sharkey4519a022011-09-07 23:24:53 -07001271 Log.d(TAG, "adjustStatusBarLocked: mShowing=" + mShowing + " mHidden=" + mHidden
1272 + " isSecure=" + isSecure() + " --> flags=0x" + Integer.toHexString(flags));
Jeff Sharkeyf52c70b2011-08-30 22:05:47 -07001273 }
1274
Jim Millerd6523da2012-10-21 16:47:02 -07001275 if (!(mContext instanceof Activity)) {
1276 mStatusBarManager.disable(flags);
1277 }
Mike Lockwood5f892c12009-11-19 23:39:13 -05001278 }
1279 }
1280
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001281 /**
Adam Cohenf7522022012-10-03 20:03:18 -07001282 * Handle message sent by {@link #resetStateLocked(Bundle)}
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001283 * @see #RESET
1284 */
Adam Cohenf7522022012-10-03 20:03:18 -07001285 private void handleReset(Bundle options) {
Chris Wrenf41c61b2012-11-29 15:19:54 -05001286 if (options == null) {
1287 options = new Bundle();
1288 }
1289 options.putBoolean(KeyguardViewManager.IS_SWITCHING_USER, mSwitchingUser);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001290 synchronized (KeyguardViewMediator.this) {
1291 if (DEBUG) Log.d(TAG, "handleReset");
Adam Cohenf7522022012-10-03 20:03:18 -07001292 mKeyguardViewManager.reset(options);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001293 }
1294 }
1295
1296 /**
1297 * Handle message sent by {@link #verifyUnlock}
Craig Mautner904732c2012-10-17 15:20:24 -07001298 * @see #VERIFY_UNLOCK
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001299 */
1300 private void handleVerifyUnlock() {
1301 synchronized (KeyguardViewMediator.this) {
1302 if (DEBUG) Log.d(TAG, "handleVerifyUnlock");
1303 mKeyguardViewManager.verifyUnlock();
1304 mShowing = true;
Dianne Hackbornff5b1582012-04-12 17:24:07 -07001305 updateActivityLockScreenState();
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001306 }
1307 }
1308
1309 /**
1310 * Handle message sent by {@link #notifyScreenOffLocked()}
1311 * @see #NOTIFY_SCREEN_OFF
1312 */
1313 private void handleNotifyScreenOff() {
1314 synchronized (KeyguardViewMediator.this) {
1315 if (DEBUG) Log.d(TAG, "handleNotifyScreenOff");
1316 mKeyguardViewManager.onScreenTurnedOff();
1317 }
1318 }
1319
1320 /**
1321 * Handle message sent by {@link #notifyScreenOnLocked()}
1322 * @see #NOTIFY_SCREEN_ON
1323 */
Jim Miller25190572013-02-28 17:36:24 -08001324 private void handleNotifyScreenOn(IKeyguardShowCallback callback) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001325 synchronized (KeyguardViewMediator.this) {
1326 if (DEBUG) Log.d(TAG, "handleNotifyScreenOn");
Jim Miller25190572013-02-28 17:36:24 -08001327 mKeyguardViewManager.onScreenTurnedOn(callback);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001328 }
1329 }
Jeff Sharkey054340d2011-09-01 22:28:03 -07001330
John Spurlock34c4fe52012-11-07 10:12:29 -05001331 public boolean isDismissable() {
1332 return mKeyguardDonePending || !isSecure();
1333 }
1334
Jim Miller4eeb4f62012-11-08 00:04:29 -08001335 public void showAssistant() {
1336 Message msg = mHandler.obtainMessage(SHOW_ASSISTANT);
1337 mHandler.sendMessage(msg);
1338 }
1339
1340 public void handleShowAssistant() {
1341 mKeyguardViewManager.showAssistant();
1342 }
1343
John Spurlock43d84512012-11-09 10:27:33 -05001344 private boolean isAssistantAvailable() {
1345 return mSearchManager != null
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08001346 && mSearchManager.getAssistIntent(mContext, false, UserHandle.USER_CURRENT) != null;
John Spurlock43d84512012-11-09 10:27:33 -05001347 }
Amith Yamasani6fc1d4e2013-05-08 16:43:58 -07001348
1349 public static MultiUserAvatarCache getAvatarCache() {
1350 return sMultiUserAvatarCache;
1351 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001352}