blob: 4086f84271a3413c61190c4036e948c1e0edc078 [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
Adam Powell43a372f2013-09-27 17:43:53 -070019import android.graphics.Bitmap;
Jim Miller25190572013-02-28 17:36:24 -080020import com.android.internal.policy.IKeyguardExitCallback;
21import com.android.internal.policy.IKeyguardShowCallback;
Jim Millerbc4603b2010-08-30 21:21:34 -070022import static android.provider.Settings.System.SCREEN_OFF_TIMEOUT;
23
Jim Millerdcb3d842012-08-23 19:18:12 -070024import android.app.Activity;
Dianne Hackborn4994c662009-09-23 22:21:23 -070025import android.app.ActivityManagerNative;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -080026import android.app.AlarmManager;
27import android.app.PendingIntent;
John Spurlock43d84512012-11-09 10:27:33 -050028import android.app.SearchManager;
Mike Lockwood5f892c12009-11-19 23:39:13 -050029import android.app.StatusBarManager;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -080030import android.content.BroadcastReceiver;
Daniel Sandlerdb783bd2010-02-11 15:27:37 -050031import android.content.ContentResolver;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -080032import android.content.Context;
33import android.content.Intent;
34import android.content.IntentFilter;
Daniel Sandleraec967a2010-02-20 01:05:22 -050035import android.media.AudioManager;
Marco Nelissend5545bd2011-09-29 12:49:17 -070036import android.media.SoundPool;
Adam Cohenf7522022012-10-03 20:03:18 -070037import android.os.Bundle;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -080038import android.os.Handler;
Jeff Brown109025d2012-08-14 20:41:30 -070039import android.os.Looper;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -080040import android.os.Message;
41import android.os.PowerManager;
Dianne Hackborn4994c662009-09-23 22:21:23 -070042import android.os.RemoteException;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -080043import android.os.SystemClock;
Jim Miller1b152022009-10-28 16:08:15 -070044import android.os.SystemProperties;
Jeff Sharkey6a25cbd2012-08-23 12:14:26 -070045import android.os.UserHandle;
Amith Yamasanib70ff9a2012-09-07 18:28:11 -070046import android.os.UserManager;
Daniel Sandlerdb783bd2010-02-11 15:27:37 -050047import android.provider.Settings;
Karl Rosaenab100082009-03-24 22:35:17 -070048import android.telephony.TelephonyManager;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -080049import android.util.EventLog;
Karl Rosaenab100082009-03-24 22:35:17 -070050import android.util.Log;
Jim Miller25190572013-02-28 17:36:24 -080051import android.util.Slog;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -080052import android.view.KeyEvent;
Jim Millercaf24fc2013-09-10 18:37:01 -070053import android.view.MotionEvent;
Jeff Brown98365d72012-08-19 20:30:52 -070054import android.view.WindowManager;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -080055import android.view.WindowManagerPolicy;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -080056
Adam Cohenf7522022012-10-03 20:03:18 -070057import com.android.internal.telephony.IccCardConstants;
58import com.android.internal.widget.LockPatternUtils;
59
Wink Saville37c124c2009-04-02 01:37:02 -070060
The Android Open Source Project1f838aa2009-03-03 19:32:13 -080061/**
62 * Mediates requests related to the keyguard. This includes queries about the
63 * state of the keyguard, power management events that effect whether the keyguard
64 * should be shown or reset, callbacks to the phone window manager to notify
65 * it of when the keyguard is showing, and events from the keyguard view itself
66 * stating that the keyguard was succesfully unlocked.
67 *
68 * Note that the keyguard view is shown when the screen is off (as appropriate)
69 * so that once the screen comes on, it will be ready immediately.
70 *
71 * Example queries about the keyguard:
72 * - is {movement, key} one that should wake the keygaurd?
73 * - is the keyguard showing?
74 * - are input events restricted due to the state of the keyguard?
75 *
76 * Callbacks to the phone window manager:
77 * - the keyguard is showing
78 *
79 * Example external events that translate to keyguard view changes:
80 * - screen turned off -> reset the keyguard, and show it so it will be ready
81 * next time the screen turns on
82 * - keyboard is slid open -> if the keyguard is not secure, hide it
83 *
84 * Events from the keyguard view:
85 * - user succesfully unlocked keyguard -> hide keyguard view, and no longer
86 * restrict input events.
87 *
88 * Note: in addition to normal power managment events that effect the state of
89 * whether the keyguard should be showing, external apps and services may request
90 * that the keyguard be disabled via {@link #setKeyguardEnabled(boolean)}. When
91 * false, this will override all other conditions for turning on the keyguard.
92 *
93 * Threading and synchronization:
94 * This class is created by the initialization routine of the {@link WindowManagerPolicy},
95 * and runs on its thread. The keyguard UI is created from that thread in the
96 * constructor of this class. The apis may be called from other threads, including the
Jeff Brown4532e612012-04-05 14:27:12 -070097 * {@link com.android.server.input.InputManagerService}'s and {@link android.view.WindowManager}'s.
The Android Open Source Project1f838aa2009-03-03 19:32:13 -080098 * Therefore, methods on this class are synchronized, and any action that is pointed
99 * directly to the keyguard UI is posted to a {@link Handler} to ensure it is taken on the UI
100 * thread of the keyguard.
101 */
Jim Millerdcb3d842012-08-23 19:18:12 -0700102public class KeyguardViewMediator {
Jim Millerbc4603b2010-08-30 21:21:34 -0700103 private static final int KEYGUARD_DISPLAY_TIMEOUT_DELAY_DEFAULT = 30000;
Jim Miller9b1db682012-10-25 19:31:19 -0700104 final static boolean DEBUG = false;
Joe Onorato431bb222010-10-18 19:13:23 -0400105 private final static boolean DBG_WAKE = false;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800106
107 private final static String TAG = "KeyguardViewMediator";
108
Jim Miller9c20d0e2010-01-20 15:00:23 -0800109 private static final String DELAYED_KEYGUARD_ACTION =
Wink Saville37c124c2009-04-02 01:37:02 -0700110 "com.android.internal.policy.impl.PhoneWindowManager.DELAYED_KEYGUARD";
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800111
112 // used for handler messages
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800113 private static final int SHOW = 2;
114 private static final int HIDE = 3;
115 private static final int RESET = 4;
116 private static final int VERIFY_UNLOCK = 5;
117 private static final int NOTIFY_SCREEN_OFF = 6;
118 private static final int NOTIFY_SCREEN_ON = 7;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800119 private static final int KEYGUARD_DONE = 9;
120 private static final int KEYGUARD_DONE_DRAWING = 10;
Dianne Hackborn39c2d712009-09-22 11:41:31 -0700121 private static final int KEYGUARD_DONE_AUTHENTICATING = 11;
Mike Lockwood9200a392009-11-17 20:25:58 -0500122 private static final int SET_HIDDEN = 12;
Mike Lockwood28569302010-01-28 11:54:40 -0500123 private static final int KEYGUARD_TIMEOUT = 13;
Jim Miller4eeb4f62012-11-08 00:04:29 -0800124 private static final int SHOW_ASSISTANT = 14;
Jim Millercaf24fc2013-09-10 18:37:01 -0700125 private static final int DISPATCH_EVENT = 15;
Jim Miller138f25d2013-09-25 13:46:58 -0700126 private static final int LAUNCH_CAMERA = 16;
Jim Miller60013792013-10-03 18:31:34 -0700127 private static final int DISMISS = 17;
Jim Miller9c20d0e2010-01-20 15:00:23 -0800128
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800129 /**
130 * The default amount of time we stay awake (used for all key input)
131 */
Jim Miller77274692011-01-17 15:28:21 -0800132 protected static final int AWAKE_INTERVAL_DEFAULT_MS = 10000;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800133
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800134 /**
135 * How long to wait after the screen turns off due to timeout before
136 * turning on the keyguard (i.e, the user has this much time to turn
137 * the screen back on without having to face the keyguard).
138 */
Jim Millerbc4603b2010-08-30 21:21:34 -0700139 private static final int KEYGUARD_LOCK_AFTER_DELAY_DEFAULT = 5000;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800140
141 /**
Craig Mautnerf1b67412012-09-19 13:18:29 -0700142 * How long we'll wait for the {@link ViewMediatorCallback#keyguardDoneDrawing()}
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800143 * callback before unblocking a call to {@link #setKeyguardEnabled(boolean)}
144 * that is reenabling the keyguard.
145 */
146 private static final int KEYGUARD_DONE_DRAWING_TIMEOUT_MS = 2000;
Jim Miller9c20d0e2010-01-20 15:00:23 -0800147
Daniel Sandler74d188c2011-08-10 00:00:31 -0400148 /**
Jeff Sharkeyf52c70b2011-08-30 22:05:47 -0700149 * Allow the user to expand the status bar when the keyguard is engaged
150 * (without a pattern or password).
Daniel Sandler74d188c2011-08-10 00:00:31 -0400151 */
Jeff Sharkeyf52c70b2011-08-30 22:05:47 -0700152 private static final boolean ENABLE_INSECURE_STATUS_BAR_EXPAND = true;
Daniel Sandler74d188c2011-08-10 00:00:31 -0400153
Amith Yamasani8cb751b2011-09-30 15:39:41 -0700154 /** The stream type that the lock sounds are tied to. */
Eric Laurent6d517662012-04-23 18:42:39 -0700155 private int mMasterStreamType;
Amith Yamasani8cb751b2011-09-30 15:39:41 -0700156
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800157 private Context mContext;
158 private AlarmManager mAlarmManager;
Amith Yamasani8cb751b2011-09-30 15:39:41 -0700159 private AudioManager mAudioManager;
Mike Lockwood5f892c12009-11-19 23:39:13 -0500160 private StatusBarManager mStatusBarManager;
Chris Wrenf41c61b2012-11-29 15:19:54 -0500161 private boolean mSwitchingUser;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800162
163 private boolean mSystemReady;
Daniel Sandler0060a9b2010-03-15 23:09:57 -0400164
165 // Whether the next call to playSounds() should be skipped. Defaults to
166 // true because the first lock (on boot) should be silent.
167 private boolean mSuppressNextLockSound = true;
168
Jim Miller9c20d0e2010-01-20 15:00:23 -0800169
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800170 /** High level access to the power manager for WakeLocks */
171 private PowerManager mPM;
172
Amith Yamasanib70ff9a2012-09-07 18:28:11 -0700173 /** UserManager for querying number of users */
174 private UserManager mUserManager;
175
John Spurlock43d84512012-11-09 10:27:33 -0500176 /** SearchManager for determining whether or not search assistant is available */
177 private SearchManager mSearchManager;
178
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800179 /**
Mike Lockwood674d3e42009-10-06 09:28:54 -0400180 * Used to keep the device awake while to ensure the keyguard finishes opening before
181 * we sleep.
182 */
183 private PowerManager.WakeLock mShowKeyguardWakeLock;
184
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800185 private KeyguardViewManager mKeyguardViewManager;
186
187 // these are protected by synchronized (this)
188
189 /**
Mike Lockwood5d258b62009-12-02 13:50:45 -0500190 * External apps (like the phone app) can tell us to disable the keygaurd.
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800191 */
192 private boolean mExternallyEnabled = true;
193
194 /**
195 * Remember if an external call to {@link #setKeyguardEnabled} with value
196 * false caused us to hide the keyguard, so that we need to reshow it once
197 * the keygaurd is reenabled with another call with value true.
198 */
199 private boolean mNeedToReshowWhenReenabled = false;
200
201 // cached value of whether we are showing (need to know this to quickly
202 // answer whether the input should be restricted)
Jim Millere5f17ab2013-11-13 15:40:48 -0800203 private boolean mShowing;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800204
Mike Lockwood09a40402009-11-08 00:33:23 -0500205 // true if the keyguard is hidden by another window
206 private boolean mHidden = false;
207
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800208 /**
209 * Helps remember whether the screen has turned on since the last time
210 * it turned off due to timeout. see {@link #onScreenTurnedOff(int)}
211 */
212 private int mDelayedShowingSequence;
213
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800214 /**
215 * If the user has disabled the keyguard, then requests to exit, this is
216 * how we'll ultimately let them know whether it was successful. We use this
217 * var being non-null as an indicator that there is an in progress request.
218 */
Jim Miller25190572013-02-28 17:36:24 -0800219 private IKeyguardExitCallback mExitSecureCallback;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800220
221 // the properties of the keyguard
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800222
223 private KeyguardUpdateMonitor mUpdateMonitor;
224
Daniel Hansson4b716862012-03-29 11:02:05 +0200225 private boolean mScreenOn;
Karl Rosaenab100082009-03-24 22:35:17 -0700226
Daniel Sandlerf2d8e742010-02-22 13:09:48 -0500227 // last known state of the cellular connection
228 private String mPhoneState = TelephonyManager.EXTRA_STATE_IDLE;
229
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800230 /**
The Android Open Source Projectc84bf282009-03-09 11:52:14 -0700231 * we send this intent when the keyguard is dismissed.
232 */
Jim Miller17f509a2013-02-28 18:36:12 -0800233 private static final Intent USER_PRESENT_INTENT = new Intent(Intent.ACTION_USER_PRESENT)
234 .addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING
235 | Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
The Android Open Source Projectc84bf282009-03-09 11:52:14 -0700236
237 /**
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800238 * {@link #setKeyguardEnabled} waits on this condition when it reenables
239 * the keyguard.
240 */
241 private boolean mWaitingUntilKeyguardVisible = false;
Jim Millerbc4603b2010-08-30 21:21:34 -0700242 private LockPatternUtils mLockPatternUtils;
John Spurlock34c4fe52012-11-07 10:12:29 -0500243 private boolean mKeyguardDonePending = false;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800244
Marco Nelissend5545bd2011-09-29 12:49:17 -0700245 private SoundPool mLockSounds;
246 private int mLockSoundId;
247 private int mUnlockSoundId;
248 private int mLockSoundStreamId;
Jean-Michel Trivic55b3932012-06-05 11:57:59 -0700249
250 /**
251 * The volume applied to the lock/unlock sounds.
252 */
253 private final float mLockSoundVolume;
Marco Nelissend5545bd2011-09-29 12:49:17 -0700254
Jim Millerdcb3d842012-08-23 19:18:12 -0700255 /**
Jim Miller31921482013-11-06 20:43:55 -0800256 * For managing external displays
257 */
258 private KeyguardDisplayManager mKeyguardDisplayManager;
259
260 /**
Amith Yamasani6fc1d4e2013-05-08 16:43:58 -0700261 * Cache of avatar drawables, for use by KeyguardMultiUserAvatar.
262 */
263 private static MultiUserAvatarCache sMultiUserAvatarCache = new MultiUserAvatarCache();
264
265 /**
Jim Millerdcb3d842012-08-23 19:18:12 -0700266 * The callback used by the keyguard view to tell the {@link KeyguardViewMediator}
267 * various things.
268 */
269 public interface ViewMediatorCallback {
Jim Millerdcb3d842012-08-23 19:18:12 -0700270 /**
Jeff Brownc7505bc2012-10-05 21:58:15 -0700271 * Reports user activity and requests that the screen stay on.
272 */
273 void userActivity();
274
275 /**
276 * Reports user activity and requests that the screen stay on for at least
277 * the specified amount of time.
278 * @param millis The amount of time in millis. This value is currently ignored.
Jim Millerdcb3d842012-08-23 19:18:12 -0700279 */
Jeff Brown3dc524b2012-09-30 19:49:11 -0700280 void userActivity(long millis);
Jim Millerdcb3d842012-08-23 19:18:12 -0700281
282 /**
283 * Report that the keyguard is done.
284 * @param authenticated Whether the user securely got past the keyguard.
285 * the only reason for this to be false is if the keyguard was instructed
286 * to appear temporarily to verify the user is supposed to get past the
287 * keyguard, and the user fails to do so.
288 */
289 void keyguardDone(boolean authenticated);
290
291 /**
292 * Report that the keyguard is done drawing.
293 */
294 void keyguardDoneDrawing();
295
296 /**
297 * Tell ViewMediator that the current view needs IME input
298 * @param needsInput
299 */
300 void setNeedsInput(boolean needsInput);
Jeff Brownc7505bc2012-10-05 21:58:15 -0700301
302 /**
303 * Tell view mediator that the keyguard view's desired user activity timeout
304 * has changed and needs to be reapplied to the window.
305 */
306 void onUserActivityTimeoutChanged();
John Spurlock34c4fe52012-11-07 10:12:29 -0500307
308 /**
309 * Report that the keyguard is dismissable, pending the next keyguardDone call.
310 */
311 void keyguardDonePending();
Jim Miller0b9d8ac2013-11-07 19:15:49 -0800312
313 /**
314 * Report when keyguard is actually gone
315 */
316 void keyguardGone();
Jim Millerdcb3d842012-08-23 19:18:12 -0700317 }
318
Jim Millerbbf1a742012-07-17 18:30:30 -0700319 KeyguardUpdateMonitorCallback mUpdateCallback = new KeyguardUpdateMonitorCallback() {
320
321 @Override
Chris Wrenf41c61b2012-11-29 15:19:54 -0500322 public void onUserSwitching(int userId) {
Craig Mautnerf1b67412012-09-19 13:18:29 -0700323 // Note that the mLockPatternUtils user has already been updated from setCurrentUser.
Adam Cohenf7522022012-10-03 20:03:18 -0700324 // We need to force a reset of the views, since lockNow (called by
325 // ActivityManagerService) will not reconstruct the keyguard if it is already showing.
Jim Millerbbf1a742012-07-17 18:30:30 -0700326 synchronized (KeyguardViewMediator.this) {
Chris Wrenf41c61b2012-11-29 15:19:54 -0500327 mSwitchingUser = true;
Winson Chungf7614fc2012-11-26 14:43:24 -0800328 resetStateLocked(null);
John Spurlock4e6922d2012-10-04 14:51:51 -0400329 adjustStatusBarLocked();
Brian Colonnaa5239892013-04-15 11:45:40 -0400330 // When we switch users we want to bring the new user to the biometric unlock even
331 // if the current user has gone to the backup.
332 KeyguardUpdateMonitor.getInstance(mContext).setAlternateUnlockEnabled(true);
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700333 }
Jim Millerbbf1a742012-07-17 18:30:30 -0700334 }
335
336 @Override
Chris Wrenf41c61b2012-11-29 15:19:54 -0500337 public void onUserSwitchComplete(int userId) {
338 mSwitchingUser = false;
339 }
340
341 @Override
Jim Millerbbf1a742012-07-17 18:30:30 -0700342 public void onUserRemoved(int userId) {
343 mLockPatternUtils.removeUser(userId);
Amith Yamasani6fc1d4e2013-05-08 16:43:58 -0700344 sMultiUserAvatarCache.clear(userId);
345 }
346
347 @Override
348 public void onUserInfoChanged(int userId) {
349 sMultiUserAvatarCache.clear(userId);
Jim Millerbbf1a742012-07-17 18:30:30 -0700350 }
351
352 @Override
353 void onPhoneStateChanged(int phoneState) {
354 synchronized (KeyguardViewMediator.this) {
355 if (TelephonyManager.CALL_STATE_IDLE == phoneState // call ending
356 && !mScreenOn // screen off
357 && mExternallyEnabled) { // not disabled by any app
358
359 // note: this is a way to gracefully reenable the keyguard when the call
360 // ends and the screen is off without always reenabling the keyguard
361 // each time the screen turns off while in call (and having an occasional ugly
362 // flicker while turning back on the screen and disabling the keyguard again).
363 if (DEBUG) Log.d(TAG, "screen is off and call ended, let's make sure the "
364 + "keyguard is showing");
Jim Miller5ecd8112013-01-09 18:50:26 -0800365 doKeyguardLocked(null);
Jim Millerbbf1a742012-07-17 18:30:30 -0700366 }
367 }
368 };
Jim Millerb0304762012-03-13 20:01:25 -0700369
370 @Override
371 public void onClockVisibilityChanged() {
372 adjustStatusBarLocked();
373 }
374
375 @Override
376 public void onDeviceProvisioned() {
Jim Miller3fd47af2012-09-21 19:55:27 -0700377 sendUserPresentBroadcast();
Jim Millerb0304762012-03-13 20:01:25 -0700378 }
379
Jim Millerbbf1a742012-07-17 18:30:30 -0700380 @Override
381 public void onSimStateChanged(IccCardConstants.State simState) {
382 if (DEBUG) Log.d(TAG, "onSimStateChanged: " + simState);
383
384 switch (simState) {
385 case NOT_READY:
386 case ABSENT:
387 // only force lock screen in case of missing sim if user hasn't
388 // gone through setup wizard
389 synchronized (this) {
390 if (!mUpdateMonitor.isDeviceProvisioned()) {
391 if (!isShowing()) {
392 if (DEBUG) Log.d(TAG, "ICC_ABSENT isn't showing,"
393 + " we need to show the keyguard since the "
394 + "device isn't provisioned yet.");
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 }
401 break;
402 case PIN_REQUIRED:
403 case PUK_REQUIRED:
404 synchronized (this) {
405 if (!isShowing()) {
406 if (DEBUG) Log.d(TAG, "INTENT_VALUE_ICC_LOCKED and keygaurd isn't "
407 + "showing; need to show keyguard so user can enter sim pin");
Jim Miller5ecd8112013-01-09 18:50:26 -0800408 doKeyguardLocked(null);
Jim Millerbbf1a742012-07-17 18:30:30 -0700409 } else {
Adam Cohenf7522022012-10-03 20:03:18 -0700410 resetStateLocked(null);
Jim Millerbbf1a742012-07-17 18:30:30 -0700411 }
412 }
413 break;
414 case PERM_DISABLED:
415 synchronized (this) {
416 if (!isShowing()) {
417 if (DEBUG) Log.d(TAG, "PERM_DISABLED and "
418 + "keygaurd isn't showing.");
Jim Miller5ecd8112013-01-09 18:50:26 -0800419 doKeyguardLocked(null);
Jim Millerbbf1a742012-07-17 18:30:30 -0700420 } else {
421 if (DEBUG) Log.d(TAG, "PERM_DISABLED, resetStateLocked to"
422 + "show permanently disabled message in lockscreen.");
Adam Cohenf7522022012-10-03 20:03:18 -0700423 resetStateLocked(null);
Jim Millerbbf1a742012-07-17 18:30:30 -0700424 }
425 }
426 break;
427 case READY:
428 synchronized (this) {
429 if (isShowing()) {
Adam Cohenf7522022012-10-03 20:03:18 -0700430 resetStateLocked(null);
Jim Millerbbf1a742012-07-17 18:30:30 -0700431 }
432 }
433 break;
434 }
435 }
436
Jim Millerb0304762012-03-13 20:01:25 -0700437 };
438
Jim Millerdcb3d842012-08-23 19:18:12 -0700439 ViewMediatorCallback mViewMediatorCallback = new ViewMediatorCallback() {
Jeff Brownc7505bc2012-10-05 21:58:15 -0700440 public void userActivity() {
441 KeyguardViewMediator.this.userActivity();
442 }
443
Jeff Brown3dc524b2012-09-30 19:49:11 -0700444 public void userActivity(long holdMs) {
445 KeyguardViewMediator.this.userActivity(holdMs);
Jim Millerdcb3d842012-08-23 19:18:12 -0700446 }
447
448 public void keyguardDone(boolean authenticated) {
449 KeyguardViewMediator.this.keyguardDone(authenticated, true);
450 }
451
452 public void keyguardDoneDrawing() {
453 mHandler.sendEmptyMessage(KEYGUARD_DONE_DRAWING);
454 }
455
456 @Override
457 public void setNeedsInput(boolean needsInput) {
458 mKeyguardViewManager.setNeedsInput(needsInput);
459 }
Jeff Brownc7505bc2012-10-05 21:58:15 -0700460
461 @Override
462 public void onUserActivityTimeoutChanged() {
463 mKeyguardViewManager.updateUserActivityTimeout();
464 }
John Spurlock34c4fe52012-11-07 10:12:29 -0500465
466 @Override
467 public void keyguardDonePending() {
468 mKeyguardDonePending = true;
469 }
Jim Miller0b9d8ac2013-11-07 19:15:49 -0800470
471 @Override
472 public void keyguardGone() {
473 mKeyguardDisplayManager.hide();
474 }
Jim Millerdcb3d842012-08-23 19:18:12 -0700475 };
476
Jim Miller25190572013-02-28 17:36:24 -0800477 private void userActivity() {
Jeff Brown3dc524b2012-09-30 19:49:11 -0700478 userActivity(AWAKE_INTERVAL_DEFAULT_MS);
479 }
480
481 public void userActivity(long holdMs) {
482 // We ignore the hold time. Eventually we should remove it.
483 // Instead, the keyguard window has an explicit user activity timeout set on it.
484 mPM.userActivity(SystemClock.uptimeMillis(), false);
Jim Millerdcb3d842012-08-23 19:18:12 -0700485 }
486
487 /**
488 * Construct a KeyguardViewMediator
489 * @param context
490 * @param lockPatternUtils optional mock interface for LockPatternUtils
491 */
492 public KeyguardViewMediator(Context context, LockPatternUtils lockPatternUtils) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800493 mContext = context;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800494 mPM = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
Amith Yamasanib70ff9a2012-09-07 18:28:11 -0700495 mUserManager = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
Mike Lockwood674d3e42009-10-06 09:28:54 -0400496 mShowKeyguardWakeLock = mPM.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "show keyguard");
497 mShowKeyguardWakeLock.setReferenceCounted(false);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800498
Jim Millerbbf1a742012-07-17 18:30:30 -0700499 mContext.registerReceiver(mBroadcastReceiver, new IntentFilter(DELAYED_KEYGUARD_ACTION));
500
Jim Miller31921482013-11-06 20:43:55 -0800501 mKeyguardDisplayManager = new KeyguardDisplayManager(context);
502
Jim Millerbbf1a742012-07-17 18:30:30 -0700503 mAlarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800504
Jim Millerdcb3d842012-08-23 19:18:12 -0700505 mUpdateMonitor = KeyguardUpdateMonitor.getInstance(context);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800506
Jim Millerdcb3d842012-08-23 19:18:12 -0700507 mLockPatternUtils = lockPatternUtils != null
508 ? lockPatternUtils : new LockPatternUtils(mContext);
Craig Mautnerf1b67412012-09-19 13:18:29 -0700509 mLockPatternUtils.setCurrentUser(UserHandle.USER_OWNER);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800510
Jim Millere5f17ab2013-11-13 15:40:48 -0800511 // Assume keyguard is showing (unless it's disabled) until we know for sure...
Jim Miller299422b2013-11-14 14:15:02 -0800512 mShowing = (mUpdateMonitor.isDeviceProvisioned() || mLockPatternUtils.isSecure())
513 && !mLockPatternUtils.isLockScreenDisabled();
Jim Millere5f17ab2013-11-13 15:40:48 -0800514
Jeff Brown98365d72012-08-19 20:30:52 -0700515 WindowManager wm = (WindowManager)context.getSystemService(Context.WINDOW_SERVICE);
Jim Millerdcb3d842012-08-23 19:18:12 -0700516
517 mKeyguardViewManager = new KeyguardViewManager(context, wm, mViewMediatorCallback,
518 mLockPatternUtils);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800519
Daniel Sandler687a3272010-03-13 15:44:47 -0500520 final ContentResolver cr = mContext.getContentResolver();
Marco Nelissend5545bd2011-09-29 12:49:17 -0700521
Daniel Hansson4b716862012-03-29 11:02:05 +0200522 mScreenOn = mPM.isScreenOn();
523
Marco Nelissend5545bd2011-09-29 12:49:17 -0700524 mLockSounds = new SoundPool(1, AudioManager.STREAM_SYSTEM, 0);
Jim Millerb14288d2012-09-30 18:25:05 -0700525 String soundPath = Settings.Global.getString(cr, Settings.Global.LOCK_SOUND);
Marco Nelissend5545bd2011-09-29 12:49:17 -0700526 if (soundPath != null) {
527 mLockSoundId = mLockSounds.load(soundPath, 1);
528 }
529 if (soundPath == null || mLockSoundId == 0) {
Jim Millerb14288d2012-09-30 18:25:05 -0700530 Log.w(TAG, "failed to load lock sound from " + soundPath);
Marco Nelissend5545bd2011-09-29 12:49:17 -0700531 }
Jim Millerb14288d2012-09-30 18:25:05 -0700532 soundPath = Settings.Global.getString(cr, Settings.Global.UNLOCK_SOUND);
Marco Nelissend5545bd2011-09-29 12:49:17 -0700533 if (soundPath != null) {
534 mUnlockSoundId = mLockSounds.load(soundPath, 1);
535 }
536 if (soundPath == null || mUnlockSoundId == 0) {
Jim Millerb14288d2012-09-30 18:25:05 -0700537 Log.w(TAG, "failed to load unlock sound from " + soundPath);
Marco Nelissend5545bd2011-09-29 12:49:17 -0700538 }
Jean-Michel Trivic55b3932012-06-05 11:57:59 -0700539 int lockSoundDefaultAttenuation = context.getResources().getInteger(
540 com.android.internal.R.integer.config_lockSoundVolumeDb);
Jean-Michel Trivif2b0c112012-07-09 11:59:11 -0700541 mLockSoundVolume = (float)Math.pow(10, (float)lockSoundDefaultAttenuation/20);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800542 }
543
544 /**
545 * Let us know that the system is ready after startup.
546 */
547 public void onSystemReady() {
John Spurlock43d84512012-11-09 10:27:33 -0500548 mSearchManager = (SearchManager) mContext.getSystemService(Context.SEARCH_SERVICE);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800549 synchronized (this) {
550 if (DEBUG) Log.d(TAG, "onSystemReady");
551 mSystemReady = true;
Jim Millerbbf1a742012-07-17 18:30:30 -0700552 mUpdateMonitor.registerCallback(mUpdateCallback);
Jim Miller08697702012-10-22 16:49:52 -0700553
Brian Colonnad4a3e5d2012-11-01 16:20:24 -0400554 // Suppress biometric unlock right after boot until things have settled if it is the
555 // selected security method, otherwise unsuppress it. It must be unsuppressed if it is
556 // not the selected security method for the following reason: if the user starts
557 // without a screen lock selected, the biometric unlock would be suppressed the first
558 // time they try to use it.
559 //
560 // Note that the biometric unlock will still not show if it is not the selected method.
561 // Calling setAlternateUnlockEnabled(true) simply says don't suppress it if it is the
562 // selected method.
563 if (mLockPatternUtils.usingBiometricWeak()
564 && mLockPatternUtils.isBiometricWeakInstalled()) {
565 if (DEBUG) Log.d(TAG, "suppressing biometric unlock during boot");
566 mUpdateMonitor.setAlternateUnlockEnabled(false);
567 } else {
568 mUpdateMonitor.setAlternateUnlockEnabled(true);
569 }
Jim Miller08697702012-10-22 16:49:52 -0700570
Jim Miller5ecd8112013-01-09 18:50:26 -0800571 doKeyguardLocked(null);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800572 }
Jim Miller3fd47af2012-09-21 19:55:27 -0700573 // Most services aren't available until the system reaches the ready state, so we
574 // send it here when the device first boots.
575 maybeSendUserPresentBroadcast();
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800576 }
577
578 /**
579 * Called to let us know the screen was turned off.
Mike Lockwoodce277762009-12-03 08:41:44 -0500580 * @param why either {@link WindowManagerPolicy#OFF_BECAUSE_OF_USER},
581 * {@link WindowManagerPolicy#OFF_BECAUSE_OF_TIMEOUT} or
582 * {@link WindowManagerPolicy#OFF_BECAUSE_OF_PROX_SENSOR}.
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800583 */
584 public void onScreenTurnedOff(int why) {
585 synchronized (this) {
Karl Rosaenab100082009-03-24 22:35:17 -0700586 mScreenOn = false;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800587 if (DEBUG) Log.d(TAG, "onScreenTurnedOff(" + why + ")");
588
John Spurlock14adfe42012-11-08 10:29:26 -0500589 mKeyguardDonePending = false;
590
Jim Millera4edd152012-01-06 18:24:04 -0800591 // Lock immediately based on setting if secure (user has a pin/pattern/password).
592 // This also "locks" the device when not secure to provide easy access to the
593 // camera while preventing unwanted input.
594 final boolean lockImmediately =
595 mLockPatternUtils.getPowerButtonInstantlyLocks() || !mLockPatternUtils.isSecure();
596
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800597 if (mExitSecureCallback != null) {
598 if (DEBUG) Log.d(TAG, "pending exit secure callback cancelled");
Jim Miller5ecd8112013-01-09 18:50:26 -0800599 try {
600 mExitSecureCallback.onKeyguardExitResult(false);
601 } catch (RemoteException e) {
Jim Miller25190572013-02-28 17:36:24 -0800602 Slog.w(TAG, "Failed to call onKeyguardExitResult(false)", e);
Jim Miller5ecd8112013-01-09 18:50:26 -0800603 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800604 mExitSecureCallback = null;
605 if (!mExternallyEnabled) {
Jim Miller9c20d0e2010-01-20 15:00:23 -0800606 hideLocked();
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800607 }
608 } else if (mShowing) {
609 notifyScreenOffLocked();
Adam Cohenf7522022012-10-03 20:03:18 -0700610 resetStateLocked(null);
Jim Millera4edd152012-01-06 18:24:04 -0800611 } else if (why == WindowManagerPolicy.OFF_BECAUSE_OF_TIMEOUT
612 || (why == WindowManagerPolicy.OFF_BECAUSE_OF_USER && !lockImmediately)) {
Jeff Brown6aaf2952012-10-05 16:01:08 -0700613 doKeyguardLaterLocked();
Mike Lockwoodce277762009-12-03 08:41:44 -0500614 } else if (why == WindowManagerPolicy.OFF_BECAUSE_OF_PROX_SENSOR) {
615 // Do not enable the keyguard if the prox sensor forced the screen off.
Mike Lockwood016e3972009-09-17 11:45:06 -0400616 } else {
Jim Miller5ecd8112013-01-09 18:50:26 -0800617 doKeyguardLocked(null);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800618 }
619 }
Jim Miller20daffd2013-10-07 14:59:53 -0700620 KeyguardUpdateMonitor.getInstance(mContext).dispatchScreenTurndOff(why);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800621 }
622
Jeff Brown6aaf2952012-10-05 16:01:08 -0700623 private void doKeyguardLaterLocked() {
624 // if the screen turned off because of timeout or the user hit the power button
625 // and we don't need to lock immediately, set an alarm
626 // to enable it a little bit later (i.e, give the user a chance
627 // to turn the screen back on within a certain window without
628 // having to unlock the screen)
629 final ContentResolver cr = mContext.getContentResolver();
630
631 // From DisplaySettings
632 long displayTimeout = Settings.System.getInt(cr, SCREEN_OFF_TIMEOUT,
633 KEYGUARD_DISPLAY_TIMEOUT_DELAY_DEFAULT);
634
635 // From SecuritySettings
636 final long lockAfterTimeout = Settings.Secure.getInt(cr,
637 Settings.Secure.LOCK_SCREEN_LOCK_AFTER_TIMEOUT,
638 KEYGUARD_LOCK_AFTER_DELAY_DEFAULT);
639
640 // From DevicePolicyAdmin
641 final long policyTimeout = mLockPatternUtils.getDevicePolicyManager()
642 .getMaximumTimeToLock(null, mLockPatternUtils.getCurrentUser());
643
644 long timeout;
645 if (policyTimeout > 0) {
646 // policy in effect. Make sure we don't go beyond policy limit.
647 displayTimeout = Math.max(displayTimeout, 0); // ignore negative values
648 timeout = Math.min(policyTimeout - displayTimeout, lockAfterTimeout);
649 } else {
650 timeout = lockAfterTimeout;
651 }
652
653 if (timeout <= 0) {
654 // Lock now
655 mSuppressNextLockSound = true;
Jim Miller5ecd8112013-01-09 18:50:26 -0800656 doKeyguardLocked(null);
Jeff Brown6aaf2952012-10-05 16:01:08 -0700657 } else {
658 // Lock in the future
659 long when = SystemClock.elapsedRealtime() + timeout;
660 Intent intent = new Intent(DELAYED_KEYGUARD_ACTION);
661 intent.putExtra("seq", mDelayedShowingSequence);
662 PendingIntent sender = PendingIntent.getBroadcast(mContext,
663 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
664 mAlarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, when, sender);
665 if (DEBUG) Log.d(TAG, "setting alarm to turn off keyguard, seq = "
666 + mDelayedShowingSequence);
667 }
668 }
669
670 private void cancelDoKeyguardLaterLocked() {
671 mDelayedShowingSequence++;
672 }
673
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800674 /**
675 * Let's us know the screen was turned on.
676 */
Jim Miller25190572013-02-28 17:36:24 -0800677 public void onScreenTurnedOn(IKeyguardShowCallback callback) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800678 synchronized (this) {
Karl Rosaenab100082009-03-24 22:35:17 -0700679 mScreenOn = true;
Jeff Brown6aaf2952012-10-05 16:01:08 -0700680 cancelDoKeyguardLaterLocked();
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800681 if (DEBUG) Log.d(TAG, "onScreenTurnedOn, seq = " + mDelayedShowingSequence);
Jim Miller25190572013-02-28 17:36:24 -0800682 if (callback != null) {
683 notifyScreenOnLocked(callback);
Jim Millerd6523da2012-10-21 16:47:02 -0700684 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800685 }
Jim Miller20daffd2013-10-07 14:59:53 -0700686 KeyguardUpdateMonitor.getInstance(mContext).dispatchScreenTurnedOn();
Jim Miller3fd47af2012-09-21 19:55:27 -0700687 maybeSendUserPresentBroadcast();
688 }
689
690 private void maybeSendUserPresentBroadcast() {
691 if (mSystemReady && mLockPatternUtils.isLockScreenDisabled()
692 && mUserManager.getUsers(true).size() == 1) {
693 // Lock screen is disabled because the user has set the preference to "None".
694 // In this case, send out ACTION_USER_PRESENT here instead of in
695 // handleKeyguardDone()
696 sendUserPresentBroadcast();
697 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800698 }
699
700 /**
Jeff Brown6aaf2952012-10-05 16:01:08 -0700701 * A dream started. We should lock after the usual screen-off lock timeout but only
702 * if there is a secure lock pattern.
703 */
704 public void onDreamingStarted() {
705 synchronized (this) {
706 if (mScreenOn && mLockPatternUtils.isSecure()) {
707 doKeyguardLaterLocked();
708 }
709 }
710 }
711
712 /**
713 * A dream stopped.
714 */
715 public void onDreamingStopped() {
716 synchronized (this) {
717 if (mScreenOn) {
718 cancelDoKeyguardLaterLocked();
719 }
720 }
721 }
722
723 /**
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800724 * Same semantics as {@link WindowManagerPolicy#enableKeyguard}; provide
725 * a way for external stuff to override normal keyguard behavior. For instance
726 * the phone app disables the keyguard when it receives incoming calls.
727 */
728 public void setKeyguardEnabled(boolean enabled) {
729 synchronized (this) {
730 if (DEBUG) Log.d(TAG, "setKeyguardEnabled(" + enabled + ")");
731
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800732 mExternallyEnabled = enabled;
733
734 if (!enabled && mShowing) {
735 if (mExitSecureCallback != null) {
736 if (DEBUG) Log.d(TAG, "in process of verifyUnlock request, ignoring");
737 // we're in the process of handling a request to verify the user
738 // can get past the keyguard. ignore extraneous requests to disable / reenable
739 return;
740 }
741
742 // hiding keyguard that is showing, remember to reshow later
743 if (DEBUG) Log.d(TAG, "remembering to reshow, hiding keyguard, "
744 + "disabling status bar expansion");
745 mNeedToReshowWhenReenabled = true;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800746 hideLocked();
747 } else if (enabled && mNeedToReshowWhenReenabled) {
748 // reenabled after previously hidden, reshow
749 if (DEBUG) Log.d(TAG, "previously hidden, reshowing, reenabling "
750 + "status bar expansion");
751 mNeedToReshowWhenReenabled = false;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800752
753 if (mExitSecureCallback != null) {
754 if (DEBUG) Log.d(TAG, "onKeyguardExitResult(false), resetting");
Jim Miller5ecd8112013-01-09 18:50:26 -0800755 try {
756 mExitSecureCallback.onKeyguardExitResult(false);
757 } catch (RemoteException e) {
Jim Miller25190572013-02-28 17:36:24 -0800758 Slog.w(TAG, "Failed to call onKeyguardExitResult(false)", e);
Jim Miller5ecd8112013-01-09 18:50:26 -0800759 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800760 mExitSecureCallback = null;
Adam Cohenf7522022012-10-03 20:03:18 -0700761 resetStateLocked(null);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800762 } else {
Adam Cohenf7522022012-10-03 20:03:18 -0700763 showLocked(null);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800764
765 // block until we know the keygaurd is done drawing (and post a message
766 // to unblock us after a timeout so we don't risk blocking too long
767 // and causing an ANR).
768 mWaitingUntilKeyguardVisible = true;
769 mHandler.sendEmptyMessageDelayed(KEYGUARD_DONE_DRAWING, KEYGUARD_DONE_DRAWING_TIMEOUT_MS);
770 if (DEBUG) Log.d(TAG, "waiting until mWaitingUntilKeyguardVisible is false");
771 while (mWaitingUntilKeyguardVisible) {
772 try {
773 wait();
774 } catch (InterruptedException e) {
775 Thread.currentThread().interrupt();
776 }
777 }
778 if (DEBUG) Log.d(TAG, "done waiting for mWaitingUntilKeyguardVisible");
779 }
780 }
781 }
782 }
783
784 /**
785 * @see android.app.KeyguardManager#exitKeyguardSecurely
786 */
Jim Miller25190572013-02-28 17:36:24 -0800787 public void verifyUnlock(IKeyguardExitCallback callback) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800788 synchronized (this) {
789 if (DEBUG) Log.d(TAG, "verifyUnlock");
790 if (!mUpdateMonitor.isDeviceProvisioned()) {
791 // don't allow this api when the device isn't provisioned
792 if (DEBUG) Log.d(TAG, "ignoring because device isn't provisioned");
Jim Miller5ecd8112013-01-09 18:50:26 -0800793 try {
Jim Miller25190572013-02-28 17:36:24 -0800794 callback.onKeyguardExitResult(false);
Jim Miller5ecd8112013-01-09 18:50:26 -0800795 } catch (RemoteException e) {
Jim Miller25190572013-02-28 17:36:24 -0800796 Slog.w(TAG, "Failed to call onKeyguardExitResult(false)", e);
Jim Miller5ecd8112013-01-09 18:50:26 -0800797 }
Mike Lockwood5d258b62009-12-02 13:50:45 -0500798 } else if (mExternallyEnabled) {
799 // this only applies when the user has externally disabled the
800 // keyguard. this is unexpected and means the user is not
801 // using the api properly.
802 Log.w(TAG, "verifyUnlock called when not externally disabled");
Jim Miller5ecd8112013-01-09 18:50:26 -0800803 try {
Jim Miller25190572013-02-28 17:36:24 -0800804 callback.onKeyguardExitResult(false);
Jim Miller5ecd8112013-01-09 18:50:26 -0800805 } catch (RemoteException e) {
Jim Miller25190572013-02-28 17:36:24 -0800806 Slog.w(TAG, "Failed to call onKeyguardExitResult(false)", e);
Jim Miller5ecd8112013-01-09 18:50:26 -0800807 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800808 } else if (mExitSecureCallback != null) {
809 // already in progress with someone else
Jim Miller5ecd8112013-01-09 18:50:26 -0800810 try {
Jim Miller25190572013-02-28 17:36:24 -0800811 callback.onKeyguardExitResult(false);
Jim Miller5ecd8112013-01-09 18:50:26 -0800812 } catch (RemoteException e) {
Jim Miller25190572013-02-28 17:36:24 -0800813 Slog.w(TAG, "Failed to call onKeyguardExitResult(false)", e);
Jim Miller5ecd8112013-01-09 18:50:26 -0800814 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800815 } else {
Jim Miller25190572013-02-28 17:36:24 -0800816 mExitSecureCallback = callback;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800817 verifyUnlockLocked();
818 }
819 }
820 }
821
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800822 /**
823 * Is the keyguard currently showing?
824 */
825 public boolean isShowing() {
826 return mShowing;
827 }
828
829 /**
Dianne Hackborn9dc06cc2009-11-17 18:19:23 -0800830 * Is the keyguard currently showing and not being force hidden?
831 */
832 public boolean isShowingAndNotHidden() {
833 return mShowing && !mHidden;
834 }
835
836 /**
Mike Lockwood09a40402009-11-08 00:33:23 -0500837 * Notify us when the keyguard is hidden by another window
838 */
839 public void setHidden(boolean isHidden) {
Mike Lockwoodb17b2fb2009-11-09 16:01:37 -0500840 if (DEBUG) Log.d(TAG, "setHidden " + isHidden);
Danielle Millettf6d0fc12012-10-23 16:16:52 -0400841 mUpdateMonitor.sendKeyguardVisibilityChanged(!isHidden);
Mike Lockwood9200a392009-11-17 20:25:58 -0500842 mHandler.removeMessages(SET_HIDDEN);
843 Message msg = mHandler.obtainMessage(SET_HIDDEN, (isHidden ? 1 : 0), 0);
844 mHandler.sendMessage(msg);
845 }
846
847 /**
848 * Handles SET_HIDDEN message sent by setHidden()
849 */
850 private void handleSetHidden(boolean isHidden) {
Mike Lockwood09a40402009-11-08 00:33:23 -0500851 synchronized (KeyguardViewMediator.this) {
Mike Lockwood9200a392009-11-17 20:25:58 -0500852 if (mHidden != isHidden) {
853 mHidden = isHidden;
Dianne Hackbornff5b1582012-04-12 17:24:07 -0700854 updateActivityLockScreenState();
Mike Lockwood5f892c12009-11-19 23:39:13 -0500855 adjustStatusBarLocked();
Mike Lockwood9200a392009-11-17 20:25:58 -0500856 }
Mike Lockwood09a40402009-11-08 00:33:23 -0500857 }
858 }
859
860 /**
Mike Lockwood28569302010-01-28 11:54:40 -0500861 * Used by PhoneWindowManager to enable the keyguard due to a user activity timeout.
862 * This must be safe to call from any thread and with any window manager locks held.
863 */
Adam Cohenf7522022012-10-03 20:03:18 -0700864 public void doKeyguardTimeout(Bundle options) {
Mike Lockwood28569302010-01-28 11:54:40 -0500865 mHandler.removeMessages(KEYGUARD_TIMEOUT);
Adam Cohenf7522022012-10-03 20:03:18 -0700866 Message msg = mHandler.obtainMessage(KEYGUARD_TIMEOUT, options);
Mike Lockwood28569302010-01-28 11:54:40 -0500867 mHandler.sendMessage(msg);
868 }
869
870 /**
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800871 * Given the state of the keyguard, is the input restricted?
872 * Input is restricted when the keyguard is showing, or when the keyguard
873 * was suppressed by an app that disabled the keyguard or we haven't been provisioned yet.
874 */
875 public boolean isInputRestricted() {
876 return mShowing || mNeedToReshowWhenReenabled || !mUpdateMonitor.isDeviceProvisioned();
877 }
878
Dianne Hackbornb446e972009-09-20 15:23:25 -0700879 /**
Craig Mautnerad09bcc2012-10-08 13:33:11 -0700880 * Enable the keyguard if the settings are appropriate.
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800881 */
Adam Cohenf7522022012-10-03 20:03:18 -0700882 private void doKeyguardLocked(Bundle options) {
Dianne Hackborn29aae6f2011-08-18 18:30:09 -0700883 // if another app is disabling us, don't show
884 if (!mExternallyEnabled) {
885 if (DEBUG) Log.d(TAG, "doKeyguard: not showing because externally disabled");
Karl Rosaenab100082009-03-24 22:35:17 -0700886
Dianne Hackborn29aae6f2011-08-18 18:30:09 -0700887 // note: we *should* set mNeedToReshowWhenReenabled=true here, but that makes
888 // for an occasional ugly flicker in this situation:
889 // 1) receive a call with the screen on (no keyguard) or make a call
890 // 2) screen times out
891 // 3) user hits key to turn screen back on
892 // instead, we reenable the keyguard when we know the screen is off and the call
893 // ends (see the broadcast receiver below)
894 // TODO: clean this up when we have better support at the window manager level
895 // for apps that wish to be on top of the keyguard
Dianne Hackborn38e29a62011-09-18 14:43:08 -0700896 return;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800897 }
Dianne Hackborn29aae6f2011-08-18 18:30:09 -0700898
899 // if the keyguard is already showing, don't bother
900 if (mKeyguardViewManager.isShowing()) {
901 if (DEBUG) Log.d(TAG, "doKeyguard: not showing because it is already showing");
Dianne Hackborn38e29a62011-09-18 14:43:08 -0700902 return;
Dianne Hackborn29aae6f2011-08-18 18:30:09 -0700903 }
904
905 // if the setup wizard hasn't run yet, don't show
906 final boolean requireSim = !SystemProperties.getBoolean("keyguard.no_require_sim",
907 false);
908 final boolean provisioned = mUpdateMonitor.isDeviceProvisioned();
Wink Savillea639b312012-07-10 12:37:54 -0700909 final IccCardConstants.State state = mUpdateMonitor.getSimState();
Dianne Hackborn29aae6f2011-08-18 18:30:09 -0700910 final boolean lockedOrMissing = state.isPinLocked()
Wink Savillea639b312012-07-10 12:37:54 -0700911 || ((state == IccCardConstants.State.ABSENT
912 || state == IccCardConstants.State.PERM_DISABLED)
Jim Miller9fa1ada2011-10-31 18:21:49 -0700913 && requireSim);
Dianne Hackborn29aae6f2011-08-18 18:30:09 -0700914
915 if (!lockedOrMissing && !provisioned) {
916 if (DEBUG) Log.d(TAG, "doKeyguard: not showing because device isn't provisioned"
917 + " and the sim is not locked or missing");
Dianne Hackborn38e29a62011-09-18 14:43:08 -0700918 return;
Dianne Hackborn29aae6f2011-08-18 18:30:09 -0700919 }
920
Amith Yamasani920ace02012-09-20 22:15:37 -0700921 if (mUserManager.getUsers(true).size() < 2
Amith Yamasanib70ff9a2012-09-07 18:28:11 -0700922 && mLockPatternUtils.isLockScreenDisabled() && !lockedOrMissing) {
Dianne Hackborn29aae6f2011-08-18 18:30:09 -0700923 if (DEBUG) Log.d(TAG, "doKeyguard: not showing because lockscreen is off");
Dianne Hackborn38e29a62011-09-18 14:43:08 -0700924 return;
Dianne Hackborn29aae6f2011-08-18 18:30:09 -0700925 }
926
927 if (DEBUG) Log.d(TAG, "doKeyguard: showing the lock screen");
Adam Cohenf7522022012-10-03 20:03:18 -0700928 showLocked(options);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800929 }
930
931 /**
Craig Mautnerad09bcc2012-10-08 13:33:11 -0700932 * Dismiss the keyguard through the security layers.
933 */
Jim Miller60013792013-10-03 18:31:34 -0700934 public void handleDismiss() {
Jim Miller87d03662012-11-05 20:28:09 -0800935 if (mShowing && !mHidden) {
936 mKeyguardViewManager.dismiss();
937 }
Craig Mautnerad09bcc2012-10-08 13:33:11 -0700938 }
939
Jim Miller60013792013-10-03 18:31:34 -0700940 public void dismiss() {
941 mHandler.sendEmptyMessage(DISMISS);
942 }
943
Craig Mautnerad09bcc2012-10-08 13:33:11 -0700944 /**
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800945 * Send message to keyguard telling it to reset its state.
Adam Cohenf7522022012-10-03 20:03:18 -0700946 * @param options options about how to show the keyguard
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800947 * @see #handleReset()
948 */
Adam Cohenf7522022012-10-03 20:03:18 -0700949 private void resetStateLocked(Bundle options) {
Jim Miller4894a012013-04-03 15:23:55 -0700950 if (DEBUG) Log.e(TAG, "resetStateLocked");
Adam Cohenf7522022012-10-03 20:03:18 -0700951 Message msg = mHandler.obtainMessage(RESET, options);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800952 mHandler.sendMessage(msg);
953 }
954
955 /**
956 * Send message to keyguard telling it to verify unlock
957 * @see #handleVerifyUnlock()
958 */
959 private void verifyUnlockLocked() {
960 if (DEBUG) Log.d(TAG, "verifyUnlockLocked");
961 mHandler.sendEmptyMessage(VERIFY_UNLOCK);
962 }
963
964
965 /**
966 * Send a message to keyguard telling it the screen just turned on.
967 * @see #onScreenTurnedOff(int)
968 * @see #handleNotifyScreenOff
969 */
970 private void notifyScreenOffLocked() {
971 if (DEBUG) Log.d(TAG, "notifyScreenOffLocked");
972 mHandler.sendEmptyMessage(NOTIFY_SCREEN_OFF);
973 }
974
975 /**
976 * Send a message to keyguard telling it the screen just turned on.
Jim Miller9c20d0e2010-01-20 15:00:23 -0800977 * @see #onScreenTurnedOn()
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800978 * @see #handleNotifyScreenOn
979 */
Jim Miller25190572013-02-28 17:36:24 -0800980 private void notifyScreenOnLocked(IKeyguardShowCallback result) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800981 if (DEBUG) Log.d(TAG, "notifyScreenOnLocked");
Jim Miller5ecd8112013-01-09 18:50:26 -0800982 Message msg = mHandler.obtainMessage(NOTIFY_SCREEN_ON, result);
Dianne Hackborn38e29a62011-09-18 14:43:08 -0700983 mHandler.sendMessage(msg);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800984 }
985
986 /**
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800987 * Send message to keyguard telling it to show itself
988 * @see #handleShow()
989 */
Adam Cohenf7522022012-10-03 20:03:18 -0700990 private void showLocked(Bundle options) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800991 if (DEBUG) Log.d(TAG, "showLocked");
Mike Lockwood674d3e42009-10-06 09:28:54 -0400992 // ensure we stay awake until we are finished displaying the keyguard
993 mShowKeyguardWakeLock.acquire();
Adam Cohenf7522022012-10-03 20:03:18 -0700994 Message msg = mHandler.obtainMessage(SHOW, options);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800995 mHandler.sendMessage(msg);
996 }
997
998 /**
999 * Send message to keyguard telling it to hide itself
Jim Miller9c20d0e2010-01-20 15:00:23 -08001000 * @see #handleHide()
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001001 */
1002 private void hideLocked() {
1003 if (DEBUG) Log.d(TAG, "hideLocked");
1004 Message msg = mHandler.obtainMessage(HIDE);
1005 mHandler.sendMessage(msg);
1006 }
1007
Dianne Hackbornb446e972009-09-20 15:23:25 -07001008 public boolean isSecure() {
Jim Millerdcb3d842012-08-23 19:18:12 -07001009 return mLockPatternUtils.isSecure()
1010 || KeyguardUpdateMonitor.getInstance(mContext).isSimPinSecure();
Dianne Hackbornb446e972009-09-20 15:23:25 -07001011 }
Jim Miller9c20d0e2010-01-20 15:00:23 -08001012
Craig Mautnerf1b67412012-09-19 13:18:29 -07001013 /**
1014 * Update the newUserId. Call while holding WindowManagerService lock.
Jim Milleree82f8f2012-10-01 16:26:18 -07001015 * NOTE: Should only be called by KeyguardViewMediator in response to the user id changing.
1016 *
Craig Mautnerf1b67412012-09-19 13:18:29 -07001017 * @param newUserId The id of the incoming user.
1018 */
1019 public void setCurrentUser(int newUserId) {
1020 mLockPatternUtils.setCurrentUser(newUserId);
1021 }
1022
Jim Millerbbf1a742012-07-17 18:30:30 -07001023 private final BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
Amith Yamasani52c489c2012-03-28 11:42:42 -07001024 @Override
1025 public void onReceive(Context context, Intent intent) {
Jim Millerbbf1a742012-07-17 18:30:30 -07001026 if (DELAYED_KEYGUARD_ACTION.equals(intent.getAction())) {
1027 final int sequence = intent.getIntExtra("seq", 0);
Jim Millerf3447352011-08-07 14:00:09 -07001028 if (DEBUG) Log.d(TAG, "received DELAYED_KEYGUARD_ACTION with seq = "
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001029 + sequence + ", mDelayedShowingSequence = " + mDelayedShowingSequence);
Dianne Hackborn29aae6f2011-08-18 18:30:09 -07001030 synchronized (KeyguardViewMediator.this) {
1031 if (mDelayedShowingSequence == sequence) {
Jim Millerbbf1a742012-07-17 18:30:30 -07001032 // Don't play lockscreen SFX if the screen went off due to timeout.
Dianne Hackborn29aae6f2011-08-18 18:30:09 -07001033 mSuppressNextLockSound = true;
Jim Miller5ecd8112013-01-09 18:50:26 -08001034 doKeyguardLocked(null);
Dianne Hackborn29aae6f2011-08-18 18:30:09 -07001035 }
Daniel Sandlerf2d8e742010-02-22 13:09:48 -05001036 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001037 }
1038 }
1039 };
1040
Dianne Hackborn05726582009-09-22 17:28:24 -07001041 public void keyguardDone(boolean authenticated, boolean wakeup) {
Jim Miller60013792013-10-03 18:31:34 -07001042 if (DEBUG) Log.d(TAG, "keyguardDone(" + authenticated + ")");
1043 EventLog.writeEvent(70000, 2);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001044 synchronized (this) {
Jim Miller60013792013-10-03 18:31:34 -07001045 mKeyguardDonePending = false;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001046 }
Jim Miller60013792013-10-03 18:31:34 -07001047 Message msg = mHandler.obtainMessage(KEYGUARD_DONE, authenticated ? 1 : 0, wakeup ? 1 : 0);
1048 mHandler.sendMessage(msg);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001049 }
1050
1051 /**
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001052 * This handler will be associated with the policy thread, which will also
1053 * be the UI thread of the keyguard. Since the apis of the policy, and therefore
1054 * this class, can be called by other threads, any action that directly
1055 * interacts with the keyguard ui should be posted to this handler, rather
1056 * than called directly.
1057 */
Jim Millerdcb3d842012-08-23 19:18:12 -07001058 private Handler mHandler = new Handler(Looper.myLooper(), null, true /*async*/) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001059 @Override
Wink Saville37c124c2009-04-02 01:37:02 -07001060 public void handleMessage(Message msg) {
1061 switch (msg.what) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001062 case SHOW:
Adam Cohenf7522022012-10-03 20:03:18 -07001063 handleShow((Bundle) msg.obj);
Jim Miller60013792013-10-03 18:31:34 -07001064 break;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001065 case HIDE:
1066 handleHide();
Jim Miller60013792013-10-03 18:31:34 -07001067 break;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001068 case RESET:
Adam Cohenf7522022012-10-03 20:03:18 -07001069 handleReset((Bundle) msg.obj);
Jim Miller60013792013-10-03 18:31:34 -07001070 break;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001071 case VERIFY_UNLOCK:
1072 handleVerifyUnlock();
Jim Miller60013792013-10-03 18:31:34 -07001073 break;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001074 case NOTIFY_SCREEN_OFF:
1075 handleNotifyScreenOff();
Jim Miller60013792013-10-03 18:31:34 -07001076 break;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001077 case NOTIFY_SCREEN_ON:
Jim Miller25190572013-02-28 17:36:24 -08001078 handleNotifyScreenOn((IKeyguardShowCallback) msg.obj);
Jim Miller60013792013-10-03 18:31:34 -07001079 break;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001080 case KEYGUARD_DONE:
Jim Millere51cf7ae2013-06-25 18:31:56 -07001081 handleKeyguardDone(msg.arg1 != 0, msg.arg2 != 0);
Jim Miller60013792013-10-03 18:31:34 -07001082 break;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001083 case KEYGUARD_DONE_DRAWING:
1084 handleKeyguardDoneDrawing();
Jim Miller60013792013-10-03 18:31:34 -07001085 break;
Dianne Hackborn39c2d712009-09-22 11:41:31 -07001086 case KEYGUARD_DONE_AUTHENTICATING:
Jim Millerdcb3d842012-08-23 19:18:12 -07001087 keyguardDone(true, true);
Jim Miller60013792013-10-03 18:31:34 -07001088 break;
Mike Lockwood9200a392009-11-17 20:25:58 -05001089 case SET_HIDDEN:
1090 handleSetHidden(msg.arg1 != 0);
1091 break;
Mike Lockwood28569302010-01-28 11:54:40 -05001092 case KEYGUARD_TIMEOUT:
Dianne Hackborn29aae6f2011-08-18 18:30:09 -07001093 synchronized (KeyguardViewMediator.this) {
Adam Cohenf7522022012-10-03 20:03:18 -07001094 doKeyguardLocked((Bundle) msg.obj);
Dianne Hackborn29aae6f2011-08-18 18:30:09 -07001095 }
1096 break;
Jim Miller4eeb4f62012-11-08 00:04:29 -08001097 case SHOW_ASSISTANT:
1098 handleShowAssistant();
1099 break;
Jim Millercaf24fc2013-09-10 18:37:01 -07001100 case DISPATCH_EVENT:
1101 handleDispatchEvent((MotionEvent) msg.obj);
1102 break;
Jim Miller138f25d2013-09-25 13:46:58 -07001103 case LAUNCH_CAMERA:
1104 handleLaunchCamera();
1105 break;
Jim Miller60013792013-10-03 18:31:34 -07001106 case DISMISS:
1107 handleDismiss();
1108 break;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001109 }
1110 }
1111 };
1112
1113 /**
1114 * @see #keyguardDone
1115 * @see #KEYGUARD_DONE
1116 */
Jim Millere51cf7ae2013-06-25 18:31:56 -07001117 private void handleKeyguardDone(boolean authenticated, boolean wakeup) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001118 if (DEBUG) Log.d(TAG, "handleKeyguardDone");
Jim Millere51cf7ae2013-06-25 18:31:56 -07001119
1120 if (authenticated) {
1121 mUpdateMonitor.clearFailedUnlockAttempts();
Dianne Hackborn05726582009-09-22 17:28:24 -07001122 }
Jeff Sharkey6a25cbd2012-08-23 12:14:26 -07001123
Jim Millere51cf7ae2013-06-25 18:31:56 -07001124 if (mExitSecureCallback != null) {
1125 try {
1126 mExitSecureCallback.onKeyguardExitResult(authenticated);
1127 } catch (RemoteException e) {
1128 Slog.w(TAG, "Failed to call onKeyguardExitResult(" + authenticated + ")", e);
1129 }
1130
1131 mExitSecureCallback = null;
1132
1133 if (authenticated) {
1134 // after succesfully exiting securely, no need to reshow
1135 // the keyguard when they've released the lock
1136 mExternallyEnabled = true;
1137 mNeedToReshowWhenReenabled = false;
1138 }
1139 }
1140
1141 handleHide();
Jim Miller3fd47af2012-09-21 19:55:27 -07001142 sendUserPresentBroadcast();
1143 }
1144
Jim Miller138f25d2013-09-25 13:46:58 -07001145 protected void handleLaunchCamera() {
1146 mKeyguardViewManager.launchCamera();
1147 }
1148
Jim Millercaf24fc2013-09-10 18:37:01 -07001149 protected void handleDispatchEvent(MotionEvent event) {
1150 mKeyguardViewManager.dispatch(event);
1151 }
1152
Jim Miller3fd47af2012-09-21 19:55:27 -07001153 private void sendUserPresentBroadcast() {
Jim Miller17f509a2013-02-28 18:36:12 -08001154 final UserHandle currentUser = new UserHandle(mLockPatternUtils.getCurrentUser());
1155 mContext.sendBroadcastAsUser(USER_PRESENT_INTENT, currentUser);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001156 }
1157
1158 /**
1159 * @see #keyguardDoneDrawing
1160 * @see #KEYGUARD_DONE_DRAWING
1161 */
1162 private void handleKeyguardDoneDrawing() {
1163 synchronized(this) {
Jim Miller5ecd8112013-01-09 18:50:26 -08001164 if (DEBUG) Log.d(TAG, "handleKeyguardDoneDrawing");
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001165 if (mWaitingUntilKeyguardVisible) {
1166 if (DEBUG) Log.d(TAG, "handleKeyguardDoneDrawing: notifying mWaitingUntilKeyguardVisible");
1167 mWaitingUntilKeyguardVisible = false;
1168 notifyAll();
1169
1170 // there will usually be two of these sent, one as a timeout, and one
1171 // as a result of the callback, so remove any remaining messages from
1172 // the queue
1173 mHandler.removeMessages(KEYGUARD_DONE_DRAWING);
1174 }
1175 }
1176 }
1177
Daniel Sandlerdb783bd2010-02-11 15:27:37 -05001178 private void playSounds(boolean locked) {
1179 // User feedback for keyguard.
Daniel Sandler0060a9b2010-03-15 23:09:57 -04001180
1181 if (mSuppressNextLockSound) {
1182 mSuppressNextLockSound = false;
1183 return;
1184 }
1185
Daniel Sandlerdb783bd2010-02-11 15:27:37 -05001186 final ContentResolver cr = mContext.getContentResolver();
Amith Yamasani8cb751b2011-09-30 15:39:41 -07001187 if (Settings.System.getInt(cr, Settings.System.LOCKSCREEN_SOUNDS_ENABLED, 1) == 1) {
Marco Nelissend5545bd2011-09-29 12:49:17 -07001188 final int whichSound = locked
1189 ? mLockSoundId
1190 : mUnlockSoundId;
1191 mLockSounds.stop(mLockSoundStreamId);
Amith Yamasani8cb751b2011-09-30 15:39:41 -07001192 // Init mAudioManager
1193 if (mAudioManager == null) {
1194 mAudioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
1195 if (mAudioManager == null) return;
Eric Laurent6d517662012-04-23 18:42:39 -07001196 mMasterStreamType = mAudioManager.getMasterStreamType();
Amith Yamasani8cb751b2011-09-30 15:39:41 -07001197 }
1198 // If the stream is muted, don't play the sound
Eric Laurent6d517662012-04-23 18:42:39 -07001199 if (mAudioManager.isStreamMute(mMasterStreamType)) return;
Amith Yamasani8cb751b2011-09-30 15:39:41 -07001200
Jean-Michel Trivic55b3932012-06-05 11:57:59 -07001201 mLockSoundStreamId = mLockSounds.play(whichSound,
1202 mLockSoundVolume, mLockSoundVolume, 1/*priortiy*/, 0/*loop*/, 1.0f/*rate*/);
Daniel Sandlerdb783bd2010-02-11 15:27:37 -05001203 }
Jim Miller2a98a4c2010-11-19 18:49:26 -08001204 }
Daniel Sandlerdb783bd2010-02-11 15:27:37 -05001205
Dianne Hackbornff5b1582012-04-12 17:24:07 -07001206 private void updateActivityLockScreenState() {
1207 try {
Jim Miller60013792013-10-03 18:31:34 -07001208 ActivityManagerNative.getDefault().setLockScreenShown(mShowing && !mHidden);
Dianne Hackbornff5b1582012-04-12 17:24:07 -07001209 } catch (RemoteException e) {
1210 }
1211 }
1212
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001213 /**
1214 * Handle message sent by {@link #showLocked}.
1215 * @see #SHOW
1216 */
Adam Cohenf7522022012-10-03 20:03:18 -07001217 private void handleShow(Bundle options) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001218 synchronized (KeyguardViewMediator.this) {
Jim Miller5ecd8112013-01-09 18:50:26 -08001219 if (!mSystemReady) {
1220 if (DEBUG) Log.d(TAG, "ignoring handleShow because system is not ready.");
1221 return;
1222 } else {
1223 if (DEBUG) Log.d(TAG, "handleShow");
1224 }
Jim Miller9c20d0e2010-01-20 15:00:23 -08001225
Adam Cohenf7522022012-10-03 20:03:18 -07001226 mKeyguardViewManager.show(options);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001227 mShowing = true;
John Spurlock14adfe42012-11-08 10:29:26 -05001228 mKeyguardDonePending = false;
Dianne Hackbornff5b1582012-04-12 17:24:07 -07001229 updateActivityLockScreenState();
Mike Lockwood5f892c12009-11-19 23:39:13 -05001230 adjustStatusBarLocked();
Jeff Brown3dc524b2012-09-30 19:49:11 -07001231 userActivity();
Dianne Hackborn4994c662009-09-23 22:21:23 -07001232 try {
1233 ActivityManagerNative.getDefault().closeSystemDialogs("lock");
1234 } catch (RemoteException e) {
1235 }
Dianne Hackborn29aae6f2011-08-18 18:30:09 -07001236
1237 // Do this at the end to not slow down display of the keyguard.
1238 playSounds(true);
1239
Mike Lockwood674d3e42009-10-06 09:28:54 -04001240 mShowKeyguardWakeLock.release();
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001241 }
Jim Miller31921482013-11-06 20:43:55 -08001242 mKeyguardDisplayManager.show();
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001243 }
1244
1245 /**
1246 * Handle message sent by {@link #hideLocked()}
1247 * @see #HIDE
1248 */
1249 private void handleHide() {
1250 synchronized (KeyguardViewMediator.this) {
1251 if (DEBUG) Log.d(TAG, "handleHide");
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001252
Daniel Sandlerf2d8e742010-02-22 13:09:48 -05001253 // only play "unlock" noises if not on a call (since the incall UI
1254 // disables the keyguard)
1255 if (TelephonyManager.EXTRA_STATE_IDLE.equals(mPhoneState)) {
1256 playSounds(false);
1257 }
Daniel Sandlerdb783bd2010-02-11 15:27:37 -05001258
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001259 mKeyguardViewManager.hide();
1260 mShowing = false;
John Spurlock14adfe42012-11-08 10:29:26 -05001261 mKeyguardDonePending = false;
Dianne Hackbornff5b1582012-04-12 17:24:07 -07001262 updateActivityLockScreenState();
Mike Lockwood5f892c12009-11-19 23:39:13 -05001263 adjustStatusBarLocked();
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001264 }
1265 }
1266
Mike Lockwood5f892c12009-11-19 23:39:13 -05001267 private void adjustStatusBarLocked() {
1268 if (mStatusBarManager == null) {
1269 mStatusBarManager = (StatusBarManager)
1270 mContext.getSystemService(Context.STATUS_BAR_SERVICE);
1271 }
1272 if (mStatusBarManager == null) {
1273 Log.w(TAG, "Could not get status bar manager");
1274 } else {
Daniel Sandlerdba93562011-10-06 16:39:58 -04001275 // Disable aspects of the system/status/navigation bars that must not be re-enabled by
1276 // windows that appear on top, ever
Jeff Sharkeyf52c70b2011-08-30 22:05:47 -07001277 int flags = StatusBarManager.DISABLE_NONE;
Mike Lockwoode3646dd2011-09-01 12:46:28 -04001278 if (mShowing) {
Jim Millere23ab8b2012-09-16 15:45:44 -07001279 // Permanently disable components not available when keyguard is enabled
1280 // (like recents). Temporary enable/disable (e.g. the "back" button) are
1281 // done in KeyguardHostView.
Daniel Sandlerdba93562011-10-06 16:39:58 -04001282 flags |= StatusBarManager.DISABLE_RECENT;
Mike Lockwoode3646dd2011-09-01 12:46:28 -04001283 if (isSecure() || !ENABLE_INSECURE_STATUS_BAR_EXPAND) {
1284 // showing secure lockscreen; disable expanding.
1285 flags |= StatusBarManager.DISABLE_EXPAND;
1286 }
Jeff Sharkey4519a022011-09-07 23:24:53 -07001287 if (isSecure()) {
1288 // showing secure lockscreen; disable ticker.
1289 flags |= StatusBarManager.DISABLE_NOTIFICATION_TICKER;
1290 }
John Spurlock43d84512012-11-09 10:27:33 -05001291 if (!isAssistantAvailable()) {
1292 flags |= StatusBarManager.DISABLE_SEARCH;
1293 }
Jeff Sharkeyf52c70b2011-08-30 22:05:47 -07001294 }
1295
1296 if (DEBUG) {
Jeff Sharkey4519a022011-09-07 23:24:53 -07001297 Log.d(TAG, "adjustStatusBarLocked: mShowing=" + mShowing + " mHidden=" + mHidden
1298 + " isSecure=" + isSecure() + " --> flags=0x" + Integer.toHexString(flags));
Jeff Sharkeyf52c70b2011-08-30 22:05:47 -07001299 }
1300
Jim Millerd6523da2012-10-21 16:47:02 -07001301 if (!(mContext instanceof Activity)) {
1302 mStatusBarManager.disable(flags);
1303 }
Mike Lockwood5f892c12009-11-19 23:39:13 -05001304 }
1305 }
1306
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001307 /**
Adam Cohenf7522022012-10-03 20:03:18 -07001308 * Handle message sent by {@link #resetStateLocked(Bundle)}
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001309 * @see #RESET
1310 */
Adam Cohenf7522022012-10-03 20:03:18 -07001311 private void handleReset(Bundle options) {
Chris Wrenf41c61b2012-11-29 15:19:54 -05001312 if (options == null) {
1313 options = new Bundle();
1314 }
1315 options.putBoolean(KeyguardViewManager.IS_SWITCHING_USER, mSwitchingUser);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001316 synchronized (KeyguardViewMediator.this) {
1317 if (DEBUG) Log.d(TAG, "handleReset");
Adam Cohenf7522022012-10-03 20:03:18 -07001318 mKeyguardViewManager.reset(options);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001319 }
1320 }
1321
1322 /**
1323 * Handle message sent by {@link #verifyUnlock}
Craig Mautner904732c2012-10-17 15:20:24 -07001324 * @see #VERIFY_UNLOCK
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001325 */
1326 private void handleVerifyUnlock() {
1327 synchronized (KeyguardViewMediator.this) {
1328 if (DEBUG) Log.d(TAG, "handleVerifyUnlock");
1329 mKeyguardViewManager.verifyUnlock();
1330 mShowing = true;
Dianne Hackbornff5b1582012-04-12 17:24:07 -07001331 updateActivityLockScreenState();
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001332 }
1333 }
1334
1335 /**
1336 * Handle message sent by {@link #notifyScreenOffLocked()}
1337 * @see #NOTIFY_SCREEN_OFF
1338 */
1339 private void handleNotifyScreenOff() {
1340 synchronized (KeyguardViewMediator.this) {
1341 if (DEBUG) Log.d(TAG, "handleNotifyScreenOff");
1342 mKeyguardViewManager.onScreenTurnedOff();
1343 }
1344 }
1345
1346 /**
1347 * Handle message sent by {@link #notifyScreenOnLocked()}
1348 * @see #NOTIFY_SCREEN_ON
1349 */
Jim Miller25190572013-02-28 17:36:24 -08001350 private void handleNotifyScreenOn(IKeyguardShowCallback callback) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001351 synchronized (KeyguardViewMediator.this) {
1352 if (DEBUG) Log.d(TAG, "handleNotifyScreenOn");
Jim Miller25190572013-02-28 17:36:24 -08001353 mKeyguardViewManager.onScreenTurnedOn(callback);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001354 }
1355 }
Jeff Sharkey054340d2011-09-01 22:28:03 -07001356
John Spurlock34c4fe52012-11-07 10:12:29 -05001357 public boolean isDismissable() {
1358 return mKeyguardDonePending || !isSecure();
1359 }
1360
Jim Miller4eeb4f62012-11-08 00:04:29 -08001361 public void showAssistant() {
1362 Message msg = mHandler.obtainMessage(SHOW_ASSISTANT);
1363 mHandler.sendMessage(msg);
1364 }
1365
1366 public void handleShowAssistant() {
1367 mKeyguardViewManager.showAssistant();
1368 }
1369
John Spurlock43d84512012-11-09 10:27:33 -05001370 private boolean isAssistantAvailable() {
1371 return mSearchManager != null
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08001372 && mSearchManager.getAssistIntent(mContext, false, UserHandle.USER_CURRENT) != null;
John Spurlock43d84512012-11-09 10:27:33 -05001373 }
Amith Yamasani6fc1d4e2013-05-08 16:43:58 -07001374
1375 public static MultiUserAvatarCache getAvatarCache() {
1376 return sMultiUserAvatarCache;
1377 }
Jim Millercaf24fc2013-09-10 18:37:01 -07001378
1379 public void dispatch(MotionEvent event) {
1380 Message msg = mHandler.obtainMessage(DISPATCH_EVENT, event);
1381 mHandler.sendMessage(msg);
1382 }
Jim Miller138f25d2013-09-25 13:46:58 -07001383
1384 public void launchCamera() {
1385 Message msg = mHandler.obtainMessage(LAUNCH_CAMERA);
1386 mHandler.sendMessage(msg);
1387 }
Jim Millere5f910a2013-10-16 18:15:46 -07001388
1389 public void onBootCompleted() {
1390 mUpdateMonitor.dispatchBootCompleted();
1391 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001392}