blob: 914fdc4fd89b785cefd73a0e6c520b567fce63a6 [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;
Jim Millercaf24fc2013-09-10 18:37:01 -070051import android.view.MotionEvent;
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 Millercaf24fc2013-09-10 18:37:01 -0700123 private static final int DISPATCH_EVENT = 15;
Jim Miller138f25d2013-09-25 13:46:58 -0700124 private static final int LAUNCH_CAMERA = 16;
Jim Miller60013792013-10-03 18:31:34 -0700125 private static final int DISMISS = 17;
Jim Miller9c20d0e2010-01-20 15:00:23 -0800126
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800127 /**
128 * The default amount of time we stay awake (used for all key input)
129 */
Jim Miller77274692011-01-17 15:28:21 -0800130 protected static final int AWAKE_INTERVAL_DEFAULT_MS = 10000;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800131
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800132 /**
133 * How long to wait after the screen turns off due to timeout before
134 * turning on the keyguard (i.e, the user has this much time to turn
135 * the screen back on without having to face the keyguard).
136 */
Jim Millerbc4603b2010-08-30 21:21:34 -0700137 private static final int KEYGUARD_LOCK_AFTER_DELAY_DEFAULT = 5000;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800138
139 /**
Craig Mautnerf1b67412012-09-19 13:18:29 -0700140 * How long we'll wait for the {@link ViewMediatorCallback#keyguardDoneDrawing()}
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800141 * callback before unblocking a call to {@link #setKeyguardEnabled(boolean)}
142 * that is reenabling the keyguard.
143 */
144 private static final int KEYGUARD_DONE_DRAWING_TIMEOUT_MS = 2000;
Jim Miller9c20d0e2010-01-20 15:00:23 -0800145
Daniel Sandler74d188c2011-08-10 00:00:31 -0400146 /**
Jeff Sharkeyf52c70b2011-08-30 22:05:47 -0700147 * Allow the user to expand the status bar when the keyguard is engaged
148 * (without a pattern or password).
Daniel Sandler74d188c2011-08-10 00:00:31 -0400149 */
Jeff Sharkeyf52c70b2011-08-30 22:05:47 -0700150 private static final boolean ENABLE_INSECURE_STATUS_BAR_EXPAND = true;
Daniel Sandler74d188c2011-08-10 00:00:31 -0400151
Dan Sandlera5e0f412014-01-23 15:11:54 -0500152 /**
153 * Allow the user to expand the status bar when a SECURE keyguard is engaged
154 * and {@link Settings.Secure#LOCK_SCREEN_ALLOW_NOTIFICATIONS} is set
155 * (private notifications will be masked).
156 */
157 private static final boolean ENABLE_SECURE_STATUS_BAR_EXPAND = true;
158
159 /**
160 * Default value of {@link Settings.Secure#LOCK_SCREEN_ALLOW_NOTIFICATIONS}.
161 */
162 private static final boolean ALLOW_NOTIFICATIONS_DEFAULT = false;
163
Amith Yamasani8cb751b2011-09-30 15:39:41 -0700164 /** The stream type that the lock sounds are tied to. */
Eric Laurent6d517662012-04-23 18:42:39 -0700165 private int mMasterStreamType;
Amith Yamasani8cb751b2011-09-30 15:39:41 -0700166
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800167 private Context mContext;
168 private AlarmManager mAlarmManager;
Amith Yamasani8cb751b2011-09-30 15:39:41 -0700169 private AudioManager mAudioManager;
Mike Lockwood5f892c12009-11-19 23:39:13 -0500170 private StatusBarManager mStatusBarManager;
Chris Wrenf41c61b2012-11-29 15:19:54 -0500171 private boolean mSwitchingUser;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800172
173 private boolean mSystemReady;
Daniel Sandler0060a9b2010-03-15 23:09:57 -0400174
175 // Whether the next call to playSounds() should be skipped. Defaults to
176 // true because the first lock (on boot) should be silent.
177 private boolean mSuppressNextLockSound = true;
178
Jim Miller9c20d0e2010-01-20 15:00:23 -0800179
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800180 /** High level access to the power manager for WakeLocks */
181 private PowerManager mPM;
182
Amith Yamasanib70ff9a2012-09-07 18:28:11 -0700183 /** UserManager for querying number of users */
184 private UserManager mUserManager;
185
John Spurlock43d84512012-11-09 10:27:33 -0500186 /** SearchManager for determining whether or not search assistant is available */
187 private SearchManager mSearchManager;
188
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800189 /**
Mike Lockwood674d3e42009-10-06 09:28:54 -0400190 * Used to keep the device awake while to ensure the keyguard finishes opening before
191 * we sleep.
192 */
193 private PowerManager.WakeLock mShowKeyguardWakeLock;
194
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800195 private KeyguardViewManager mKeyguardViewManager;
196
197 // these are protected by synchronized (this)
198
199 /**
Mike Lockwood5d258b62009-12-02 13:50:45 -0500200 * External apps (like the phone app) can tell us to disable the keygaurd.
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800201 */
202 private boolean mExternallyEnabled = true;
203
204 /**
205 * Remember if an external call to {@link #setKeyguardEnabled} with value
206 * false caused us to hide the keyguard, so that we need to reshow it once
207 * the keygaurd is reenabled with another call with value true.
208 */
209 private boolean mNeedToReshowWhenReenabled = false;
210
211 // cached value of whether we are showing (need to know this to quickly
212 // answer whether the input should be restricted)
Jim Millere5f17ab2013-11-13 15:40:48 -0800213 private boolean mShowing;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800214
Mike Lockwood09a40402009-11-08 00:33:23 -0500215 // true if the keyguard is hidden by another window
216 private boolean mHidden = false;
217
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800218 /**
219 * Helps remember whether the screen has turned on since the last time
220 * it turned off due to timeout. see {@link #onScreenTurnedOff(int)}
221 */
222 private int mDelayedShowingSequence;
223
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800224 /**
225 * If the user has disabled the keyguard, then requests to exit, this is
226 * how we'll ultimately let them know whether it was successful. We use this
227 * var being non-null as an indicator that there is an in progress request.
228 */
Jim Miller25190572013-02-28 17:36:24 -0800229 private IKeyguardExitCallback mExitSecureCallback;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800230
231 // the properties of the keyguard
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800232
233 private KeyguardUpdateMonitor mUpdateMonitor;
234
Daniel Hansson4b716862012-03-29 11:02:05 +0200235 private boolean mScreenOn;
Karl Rosaenab100082009-03-24 22:35:17 -0700236
Daniel Sandlerf2d8e742010-02-22 13:09:48 -0500237 // last known state of the cellular connection
238 private String mPhoneState = TelephonyManager.EXTRA_STATE_IDLE;
239
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800240 /**
The Android Open Source Projectc84bf282009-03-09 11:52:14 -0700241 * we send this intent when the keyguard is dismissed.
242 */
Jim Miller17f509a2013-02-28 18:36:12 -0800243 private static final Intent USER_PRESENT_INTENT = new Intent(Intent.ACTION_USER_PRESENT)
244 .addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING
245 | Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
The Android Open Source Projectc84bf282009-03-09 11:52:14 -0700246
247 /**
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800248 * {@link #setKeyguardEnabled} waits on this condition when it reenables
249 * the keyguard.
250 */
251 private boolean mWaitingUntilKeyguardVisible = false;
Jim Millerbc4603b2010-08-30 21:21:34 -0700252 private LockPatternUtils mLockPatternUtils;
John Spurlock34c4fe52012-11-07 10:12:29 -0500253 private boolean mKeyguardDonePending = false;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800254
Marco Nelissend5545bd2011-09-29 12:49:17 -0700255 private SoundPool mLockSounds;
256 private int mLockSoundId;
257 private int mUnlockSoundId;
258 private int mLockSoundStreamId;
Jean-Michel Trivic55b3932012-06-05 11:57:59 -0700259
260 /**
Dan Sandlera5e0f412014-01-23 15:11:54 -0500261 * Tracks value of {@link Settings.Secure#LOCK_SCREEN_ALLOW_NOTIFICATIONS}.
262 */
263 private boolean mAllowNotificationsWhenSecure;
264
265 /**
Jean-Michel Trivic55b3932012-06-05 11:57:59 -0700266 * The volume applied to the lock/unlock sounds.
267 */
268 private final float mLockSoundVolume;
Marco Nelissend5545bd2011-09-29 12:49:17 -0700269
Jim Millerdcb3d842012-08-23 19:18:12 -0700270 /**
Jim Miller31921482013-11-06 20:43:55 -0800271 * For managing external displays
272 */
273 private KeyguardDisplayManager mKeyguardDisplayManager;
274
275 /**
Amith Yamasani6fc1d4e2013-05-08 16:43:58 -0700276 * Cache of avatar drawables, for use by KeyguardMultiUserAvatar.
277 */
278 private static MultiUserAvatarCache sMultiUserAvatarCache = new MultiUserAvatarCache();
279
280 /**
Jim Millerdcb3d842012-08-23 19:18:12 -0700281 * The callback used by the keyguard view to tell the {@link KeyguardViewMediator}
282 * various things.
283 */
284 public interface ViewMediatorCallback {
Jim Millerdcb3d842012-08-23 19:18:12 -0700285 /**
Jeff Brownc7505bc2012-10-05 21:58:15 -0700286 * Reports user activity and requests that the screen stay on.
287 */
288 void userActivity();
289
290 /**
291 * Reports user activity and requests that the screen stay on for at least
292 * the specified amount of time.
293 * @param millis The amount of time in millis. This value is currently ignored.
Jim Millerdcb3d842012-08-23 19:18:12 -0700294 */
Jeff Brown3dc524b2012-09-30 19:49:11 -0700295 void userActivity(long millis);
Jim Millerdcb3d842012-08-23 19:18:12 -0700296
297 /**
298 * Report that the keyguard is done.
299 * @param authenticated Whether the user securely got past the keyguard.
300 * the only reason for this to be false is if the keyguard was instructed
301 * to appear temporarily to verify the user is supposed to get past the
302 * keyguard, and the user fails to do so.
303 */
304 void keyguardDone(boolean authenticated);
305
306 /**
307 * Report that the keyguard is done drawing.
308 */
309 void keyguardDoneDrawing();
310
311 /**
312 * Tell ViewMediator that the current view needs IME input
313 * @param needsInput
314 */
315 void setNeedsInput(boolean needsInput);
Jeff Brownc7505bc2012-10-05 21:58:15 -0700316
317 /**
318 * Tell view mediator that the keyguard view's desired user activity timeout
319 * has changed and needs to be reapplied to the window.
320 */
321 void onUserActivityTimeoutChanged();
John Spurlock34c4fe52012-11-07 10:12:29 -0500322
323 /**
324 * Report that the keyguard is dismissable, pending the next keyguardDone call.
325 */
326 void keyguardDonePending();
Jim Miller0b9d8ac2013-11-07 19:15:49 -0800327
328 /**
329 * Report when keyguard is actually gone
330 */
331 void keyguardGone();
Jim Millerdcb3d842012-08-23 19:18:12 -0700332 }
333
Jim Millerbbf1a742012-07-17 18:30:30 -0700334 KeyguardUpdateMonitorCallback mUpdateCallback = new KeyguardUpdateMonitorCallback() {
335
336 @Override
Chris Wrenf41c61b2012-11-29 15:19:54 -0500337 public void onUserSwitching(int userId) {
Craig Mautnerf1b67412012-09-19 13:18:29 -0700338 // Note that the mLockPatternUtils user has already been updated from setCurrentUser.
Adam Cohenf7522022012-10-03 20:03:18 -0700339 // We need to force a reset of the views, since lockNow (called by
340 // ActivityManagerService) will not reconstruct the keyguard if it is already showing.
Jim Millerbbf1a742012-07-17 18:30:30 -0700341 synchronized (KeyguardViewMediator.this) {
Chris Wrenf41c61b2012-11-29 15:19:54 -0500342 mSwitchingUser = true;
Winson Chungf7614fc2012-11-26 14:43:24 -0800343 resetStateLocked(null);
John Spurlock4e6922d2012-10-04 14:51:51 -0400344 adjustStatusBarLocked();
Brian Colonnaa5239892013-04-15 11:45:40 -0400345 // When we switch users we want to bring the new user to the biometric unlock even
346 // if the current user has gone to the backup.
347 KeyguardUpdateMonitor.getInstance(mContext).setAlternateUnlockEnabled(true);
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700348 }
Jim Millerbbf1a742012-07-17 18:30:30 -0700349 }
350
351 @Override
Chris Wrenf41c61b2012-11-29 15:19:54 -0500352 public void onUserSwitchComplete(int userId) {
353 mSwitchingUser = false;
354 }
355
356 @Override
Jim Millerbbf1a742012-07-17 18:30:30 -0700357 public void onUserRemoved(int userId) {
358 mLockPatternUtils.removeUser(userId);
Amith Yamasani6fc1d4e2013-05-08 16:43:58 -0700359 sMultiUserAvatarCache.clear(userId);
360 }
361
362 @Override
363 public void onUserInfoChanged(int userId) {
364 sMultiUserAvatarCache.clear(userId);
Jim Millerbbf1a742012-07-17 18:30:30 -0700365 }
366
367 @Override
368 void onPhoneStateChanged(int phoneState) {
369 synchronized (KeyguardViewMediator.this) {
370 if (TelephonyManager.CALL_STATE_IDLE == phoneState // call ending
371 && !mScreenOn // screen off
372 && mExternallyEnabled) { // not disabled by any app
373
374 // note: this is a way to gracefully reenable the keyguard when the call
375 // ends and the screen is off without always reenabling the keyguard
376 // each time the screen turns off while in call (and having an occasional ugly
377 // flicker while turning back on the screen and disabling the keyguard again).
378 if (DEBUG) Log.d(TAG, "screen is off and call ended, let's make sure the "
379 + "keyguard is showing");
Jim Miller5ecd8112013-01-09 18:50:26 -0800380 doKeyguardLocked(null);
Jim Millerbbf1a742012-07-17 18:30:30 -0700381 }
382 }
383 };
Jim Millerb0304762012-03-13 20:01:25 -0700384
385 @Override
386 public void onClockVisibilityChanged() {
387 adjustStatusBarLocked();
388 }
389
390 @Override
391 public void onDeviceProvisioned() {
Jim Miller3fd47af2012-09-21 19:55:27 -0700392 sendUserPresentBroadcast();
Jim Millerb0304762012-03-13 20:01:25 -0700393 }
394
Jim Millerbbf1a742012-07-17 18:30:30 -0700395 @Override
396 public void onSimStateChanged(IccCardConstants.State simState) {
397 if (DEBUG) Log.d(TAG, "onSimStateChanged: " + simState);
398
399 switch (simState) {
400 case NOT_READY:
401 case ABSENT:
402 // only force lock screen in case of missing sim if user hasn't
403 // gone through setup wizard
404 synchronized (this) {
405 if (!mUpdateMonitor.isDeviceProvisioned()) {
406 if (!isShowing()) {
407 if (DEBUG) Log.d(TAG, "ICC_ABSENT isn't showing,"
408 + " we need to show the keyguard since the "
409 + "device isn't provisioned yet.");
Jim Miller5ecd8112013-01-09 18:50:26 -0800410 doKeyguardLocked(null);
Jim Millerbbf1a742012-07-17 18:30:30 -0700411 } else {
Adam Cohenf7522022012-10-03 20:03:18 -0700412 resetStateLocked(null);
Jim Millerbbf1a742012-07-17 18:30:30 -0700413 }
414 }
415 }
416 break;
417 case PIN_REQUIRED:
418 case PUK_REQUIRED:
419 synchronized (this) {
420 if (!isShowing()) {
421 if (DEBUG) Log.d(TAG, "INTENT_VALUE_ICC_LOCKED and keygaurd isn't "
422 + "showing; need to show keyguard so user can enter sim pin");
Jim Miller5ecd8112013-01-09 18:50:26 -0800423 doKeyguardLocked(null);
Jim Millerbbf1a742012-07-17 18:30:30 -0700424 } else {
Adam Cohenf7522022012-10-03 20:03:18 -0700425 resetStateLocked(null);
Jim Millerbbf1a742012-07-17 18:30:30 -0700426 }
427 }
428 break;
429 case PERM_DISABLED:
430 synchronized (this) {
431 if (!isShowing()) {
432 if (DEBUG) Log.d(TAG, "PERM_DISABLED and "
433 + "keygaurd isn't showing.");
Jim Miller5ecd8112013-01-09 18:50:26 -0800434 doKeyguardLocked(null);
Jim Millerbbf1a742012-07-17 18:30:30 -0700435 } else {
436 if (DEBUG) Log.d(TAG, "PERM_DISABLED, resetStateLocked to"
437 + "show permanently disabled message in lockscreen.");
Adam Cohenf7522022012-10-03 20:03:18 -0700438 resetStateLocked(null);
Jim Millerbbf1a742012-07-17 18:30:30 -0700439 }
440 }
441 break;
442 case READY:
443 synchronized (this) {
444 if (isShowing()) {
Adam Cohenf7522022012-10-03 20:03:18 -0700445 resetStateLocked(null);
Jim Millerbbf1a742012-07-17 18:30:30 -0700446 }
447 }
448 break;
449 }
450 }
451
Jim Millerb0304762012-03-13 20:01:25 -0700452 };
453
Jim Millerdcb3d842012-08-23 19:18:12 -0700454 ViewMediatorCallback mViewMediatorCallback = new ViewMediatorCallback() {
Jeff Brownc7505bc2012-10-05 21:58:15 -0700455 public void userActivity() {
456 KeyguardViewMediator.this.userActivity();
457 }
458
Jeff Brown3dc524b2012-09-30 19:49:11 -0700459 public void userActivity(long holdMs) {
460 KeyguardViewMediator.this.userActivity(holdMs);
Jim Millerdcb3d842012-08-23 19:18:12 -0700461 }
462
463 public void keyguardDone(boolean authenticated) {
464 KeyguardViewMediator.this.keyguardDone(authenticated, true);
465 }
466
467 public void keyguardDoneDrawing() {
468 mHandler.sendEmptyMessage(KEYGUARD_DONE_DRAWING);
469 }
470
471 @Override
472 public void setNeedsInput(boolean needsInput) {
473 mKeyguardViewManager.setNeedsInput(needsInput);
474 }
Jeff Brownc7505bc2012-10-05 21:58:15 -0700475
476 @Override
477 public void onUserActivityTimeoutChanged() {
478 mKeyguardViewManager.updateUserActivityTimeout();
479 }
John Spurlock34c4fe52012-11-07 10:12:29 -0500480
481 @Override
482 public void keyguardDonePending() {
483 mKeyguardDonePending = true;
484 }
Jim Miller0b9d8ac2013-11-07 19:15:49 -0800485
486 @Override
487 public void keyguardGone() {
488 mKeyguardDisplayManager.hide();
489 }
Jim Millerdcb3d842012-08-23 19:18:12 -0700490 };
491
Jim Miller25190572013-02-28 17:36:24 -0800492 private void userActivity() {
Jeff Brown3dc524b2012-09-30 19:49:11 -0700493 userActivity(AWAKE_INTERVAL_DEFAULT_MS);
494 }
495
496 public void userActivity(long holdMs) {
497 // We ignore the hold time. Eventually we should remove it.
498 // Instead, the keyguard window has an explicit user activity timeout set on it.
499 mPM.userActivity(SystemClock.uptimeMillis(), false);
Jim Millerdcb3d842012-08-23 19:18:12 -0700500 }
501
502 /**
503 * Construct a KeyguardViewMediator
504 * @param context
505 * @param lockPatternUtils optional mock interface for LockPatternUtils
506 */
507 public KeyguardViewMediator(Context context, LockPatternUtils lockPatternUtils) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800508 mContext = context;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800509 mPM = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
Amith Yamasanib70ff9a2012-09-07 18:28:11 -0700510 mUserManager = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
Mike Lockwood674d3e42009-10-06 09:28:54 -0400511 mShowKeyguardWakeLock = mPM.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "show keyguard");
512 mShowKeyguardWakeLock.setReferenceCounted(false);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800513
Jim Millerbbf1a742012-07-17 18:30:30 -0700514 mContext.registerReceiver(mBroadcastReceiver, new IntentFilter(DELAYED_KEYGUARD_ACTION));
515
Jim Miller31921482013-11-06 20:43:55 -0800516 mKeyguardDisplayManager = new KeyguardDisplayManager(context);
517
Jim Millerbbf1a742012-07-17 18:30:30 -0700518 mAlarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800519
Jim Millerdcb3d842012-08-23 19:18:12 -0700520 mUpdateMonitor = KeyguardUpdateMonitor.getInstance(context);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800521
Jim Millerdcb3d842012-08-23 19:18:12 -0700522 mLockPatternUtils = lockPatternUtils != null
523 ? lockPatternUtils : new LockPatternUtils(mContext);
Craig Mautnerf1b67412012-09-19 13:18:29 -0700524 mLockPatternUtils.setCurrentUser(UserHandle.USER_OWNER);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800525
Jim Millere5f17ab2013-11-13 15:40:48 -0800526 // Assume keyguard is showing (unless it's disabled) until we know for sure...
Jim Miller299422b2013-11-14 14:15:02 -0800527 mShowing = (mUpdateMonitor.isDeviceProvisioned() || mLockPatternUtils.isSecure())
528 && !mLockPatternUtils.isLockScreenDisabled();
Jim Millere5f17ab2013-11-13 15:40:48 -0800529
Jeff Brown98365d72012-08-19 20:30:52 -0700530 WindowManager wm = (WindowManager)context.getSystemService(Context.WINDOW_SERVICE);
Jim Millerdcb3d842012-08-23 19:18:12 -0700531
532 mKeyguardViewManager = new KeyguardViewManager(context, wm, mViewMediatorCallback,
533 mLockPatternUtils);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800534
Daniel Sandler687a3272010-03-13 15:44:47 -0500535 final ContentResolver cr = mContext.getContentResolver();
Marco Nelissend5545bd2011-09-29 12:49:17 -0700536
Daniel Hansson4b716862012-03-29 11:02:05 +0200537 mScreenOn = mPM.isScreenOn();
538
Marco Nelissend5545bd2011-09-29 12:49:17 -0700539 mLockSounds = new SoundPool(1, AudioManager.STREAM_SYSTEM, 0);
Jim Millerb14288d2012-09-30 18:25:05 -0700540 String soundPath = Settings.Global.getString(cr, Settings.Global.LOCK_SOUND);
Marco Nelissend5545bd2011-09-29 12:49:17 -0700541 if (soundPath != null) {
542 mLockSoundId = mLockSounds.load(soundPath, 1);
543 }
544 if (soundPath == null || mLockSoundId == 0) {
Jim Millerb14288d2012-09-30 18:25:05 -0700545 Log.w(TAG, "failed to load lock sound from " + soundPath);
Marco Nelissend5545bd2011-09-29 12:49:17 -0700546 }
Jim Millerb14288d2012-09-30 18:25:05 -0700547 soundPath = Settings.Global.getString(cr, Settings.Global.UNLOCK_SOUND);
Marco Nelissend5545bd2011-09-29 12:49:17 -0700548 if (soundPath != null) {
549 mUnlockSoundId = mLockSounds.load(soundPath, 1);
550 }
551 if (soundPath == null || mUnlockSoundId == 0) {
Jim Millerb14288d2012-09-30 18:25:05 -0700552 Log.w(TAG, "failed to load unlock sound from " + soundPath);
Marco Nelissend5545bd2011-09-29 12:49:17 -0700553 }
Jean-Michel Trivic55b3932012-06-05 11:57:59 -0700554 int lockSoundDefaultAttenuation = context.getResources().getInteger(
555 com.android.internal.R.integer.config_lockSoundVolumeDb);
Jean-Michel Trivif2b0c112012-07-09 11:59:11 -0700556 mLockSoundVolume = (float)Math.pow(10, (float)lockSoundDefaultAttenuation/20);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800557 }
558
559 /**
560 * Let us know that the system is ready after startup.
561 */
562 public void onSystemReady() {
John Spurlock43d84512012-11-09 10:27:33 -0500563 mSearchManager = (SearchManager) mContext.getSystemService(Context.SEARCH_SERVICE);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800564 synchronized (this) {
565 if (DEBUG) Log.d(TAG, "onSystemReady");
566 mSystemReady = true;
Jim Millerbbf1a742012-07-17 18:30:30 -0700567 mUpdateMonitor.registerCallback(mUpdateCallback);
Jim Miller08697702012-10-22 16:49:52 -0700568
Brian Colonnad4a3e5d2012-11-01 16:20:24 -0400569 // Suppress biometric unlock right after boot until things have settled if it is the
570 // selected security method, otherwise unsuppress it. It must be unsuppressed if it is
571 // not the selected security method for the following reason: if the user starts
572 // without a screen lock selected, the biometric unlock would be suppressed the first
573 // time they try to use it.
574 //
575 // Note that the biometric unlock will still not show if it is not the selected method.
576 // Calling setAlternateUnlockEnabled(true) simply says don't suppress it if it is the
577 // selected method.
578 if (mLockPatternUtils.usingBiometricWeak()
579 && mLockPatternUtils.isBiometricWeakInstalled()) {
580 if (DEBUG) Log.d(TAG, "suppressing biometric unlock during boot");
581 mUpdateMonitor.setAlternateUnlockEnabled(false);
582 } else {
583 mUpdateMonitor.setAlternateUnlockEnabled(true);
584 }
Jim Miller08697702012-10-22 16:49:52 -0700585
Jim Miller5ecd8112013-01-09 18:50:26 -0800586 doKeyguardLocked(null);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800587 }
Jim Miller3fd47af2012-09-21 19:55:27 -0700588 // Most services aren't available until the system reaches the ready state, so we
589 // send it here when the device first boots.
590 maybeSendUserPresentBroadcast();
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800591 }
592
593 /**
594 * Called to let us know the screen was turned off.
Mike Lockwoodce277762009-12-03 08:41:44 -0500595 * @param why either {@link WindowManagerPolicy#OFF_BECAUSE_OF_USER},
596 * {@link WindowManagerPolicy#OFF_BECAUSE_OF_TIMEOUT} or
597 * {@link WindowManagerPolicy#OFF_BECAUSE_OF_PROX_SENSOR}.
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800598 */
599 public void onScreenTurnedOff(int why) {
600 synchronized (this) {
Karl Rosaenab100082009-03-24 22:35:17 -0700601 mScreenOn = false;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800602 if (DEBUG) Log.d(TAG, "onScreenTurnedOff(" + why + ")");
603
John Spurlock14adfe42012-11-08 10:29:26 -0500604 mKeyguardDonePending = false;
605
Jim Millera4edd152012-01-06 18:24:04 -0800606 // Lock immediately based on setting if secure (user has a pin/pattern/password).
607 // This also "locks" the device when not secure to provide easy access to the
608 // camera while preventing unwanted input.
609 final boolean lockImmediately =
610 mLockPatternUtils.getPowerButtonInstantlyLocks() || !mLockPatternUtils.isSecure();
611
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800612 if (mExitSecureCallback != null) {
613 if (DEBUG) Log.d(TAG, "pending exit secure callback cancelled");
Jim Miller5ecd8112013-01-09 18:50:26 -0800614 try {
615 mExitSecureCallback.onKeyguardExitResult(false);
616 } catch (RemoteException e) {
Jim Miller25190572013-02-28 17:36:24 -0800617 Slog.w(TAG, "Failed to call onKeyguardExitResult(false)", e);
Jim Miller5ecd8112013-01-09 18:50:26 -0800618 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800619 mExitSecureCallback = null;
620 if (!mExternallyEnabled) {
Jim Miller9c20d0e2010-01-20 15:00:23 -0800621 hideLocked();
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800622 }
623 } else if (mShowing) {
624 notifyScreenOffLocked();
Adam Cohenf7522022012-10-03 20:03:18 -0700625 resetStateLocked(null);
Jim Millera4edd152012-01-06 18:24:04 -0800626 } else if (why == WindowManagerPolicy.OFF_BECAUSE_OF_TIMEOUT
627 || (why == WindowManagerPolicy.OFF_BECAUSE_OF_USER && !lockImmediately)) {
Jeff Brown6aaf2952012-10-05 16:01:08 -0700628 doKeyguardLaterLocked();
Mike Lockwoodce277762009-12-03 08:41:44 -0500629 } else if (why == WindowManagerPolicy.OFF_BECAUSE_OF_PROX_SENSOR) {
630 // Do not enable the keyguard if the prox sensor forced the screen off.
Mike Lockwood016e3972009-09-17 11:45:06 -0400631 } else {
Jim Miller5ecd8112013-01-09 18:50:26 -0800632 doKeyguardLocked(null);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800633 }
634 }
Jim Miller20daffd2013-10-07 14:59:53 -0700635 KeyguardUpdateMonitor.getInstance(mContext).dispatchScreenTurndOff(why);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800636 }
637
Jeff Brown6aaf2952012-10-05 16:01:08 -0700638 private void doKeyguardLaterLocked() {
639 // if the screen turned off because of timeout or the user hit the power button
640 // and we don't need to lock immediately, set an alarm
641 // to enable it a little bit later (i.e, give the user a chance
642 // to turn the screen back on within a certain window without
643 // having to unlock the screen)
644 final ContentResolver cr = mContext.getContentResolver();
645
646 // From DisplaySettings
647 long displayTimeout = Settings.System.getInt(cr, SCREEN_OFF_TIMEOUT,
648 KEYGUARD_DISPLAY_TIMEOUT_DELAY_DEFAULT);
649
650 // From SecuritySettings
651 final long lockAfterTimeout = Settings.Secure.getInt(cr,
652 Settings.Secure.LOCK_SCREEN_LOCK_AFTER_TIMEOUT,
653 KEYGUARD_LOCK_AFTER_DELAY_DEFAULT);
654
655 // From DevicePolicyAdmin
656 final long policyTimeout = mLockPatternUtils.getDevicePolicyManager()
657 .getMaximumTimeToLock(null, mLockPatternUtils.getCurrentUser());
658
659 long timeout;
660 if (policyTimeout > 0) {
661 // policy in effect. Make sure we don't go beyond policy limit.
662 displayTimeout = Math.max(displayTimeout, 0); // ignore negative values
663 timeout = Math.min(policyTimeout - displayTimeout, lockAfterTimeout);
664 } else {
665 timeout = lockAfterTimeout;
666 }
667
668 if (timeout <= 0) {
669 // Lock now
670 mSuppressNextLockSound = true;
Jim Miller5ecd8112013-01-09 18:50:26 -0800671 doKeyguardLocked(null);
Jeff Brown6aaf2952012-10-05 16:01:08 -0700672 } else {
673 // Lock in the future
674 long when = SystemClock.elapsedRealtime() + timeout;
675 Intent intent = new Intent(DELAYED_KEYGUARD_ACTION);
676 intent.putExtra("seq", mDelayedShowingSequence);
677 PendingIntent sender = PendingIntent.getBroadcast(mContext,
678 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
679 mAlarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, when, sender);
680 if (DEBUG) Log.d(TAG, "setting alarm to turn off keyguard, seq = "
681 + mDelayedShowingSequence);
682 }
683 }
684
685 private void cancelDoKeyguardLaterLocked() {
686 mDelayedShowingSequence++;
687 }
688
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800689 /**
690 * Let's us know the screen was turned on.
691 */
Jim Miller25190572013-02-28 17:36:24 -0800692 public void onScreenTurnedOn(IKeyguardShowCallback callback) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800693 synchronized (this) {
Karl Rosaenab100082009-03-24 22:35:17 -0700694 mScreenOn = true;
Jeff Brown6aaf2952012-10-05 16:01:08 -0700695 cancelDoKeyguardLaterLocked();
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800696 if (DEBUG) Log.d(TAG, "onScreenTurnedOn, seq = " + mDelayedShowingSequence);
Jim Miller25190572013-02-28 17:36:24 -0800697 if (callback != null) {
698 notifyScreenOnLocked(callback);
Jim Millerd6523da2012-10-21 16:47:02 -0700699 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800700 }
Jim Miller20daffd2013-10-07 14:59:53 -0700701 KeyguardUpdateMonitor.getInstance(mContext).dispatchScreenTurnedOn();
Jim Miller3fd47af2012-09-21 19:55:27 -0700702 maybeSendUserPresentBroadcast();
703 }
704
705 private void maybeSendUserPresentBroadcast() {
706 if (mSystemReady && mLockPatternUtils.isLockScreenDisabled()
707 && mUserManager.getUsers(true).size() == 1) {
708 // Lock screen is disabled because the user has set the preference to "None".
709 // In this case, send out ACTION_USER_PRESENT here instead of in
710 // handleKeyguardDone()
711 sendUserPresentBroadcast();
712 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800713 }
714
715 /**
Jeff Brown6aaf2952012-10-05 16:01:08 -0700716 * A dream started. We should lock after the usual screen-off lock timeout but only
717 * if there is a secure lock pattern.
718 */
719 public void onDreamingStarted() {
720 synchronized (this) {
721 if (mScreenOn && mLockPatternUtils.isSecure()) {
722 doKeyguardLaterLocked();
723 }
724 }
725 }
726
727 /**
728 * A dream stopped.
729 */
730 public void onDreamingStopped() {
731 synchronized (this) {
732 if (mScreenOn) {
733 cancelDoKeyguardLaterLocked();
734 }
735 }
736 }
737
738 /**
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800739 * Same semantics as {@link WindowManagerPolicy#enableKeyguard}; provide
740 * a way for external stuff to override normal keyguard behavior. For instance
741 * the phone app disables the keyguard when it receives incoming calls.
742 */
743 public void setKeyguardEnabled(boolean enabled) {
744 synchronized (this) {
745 if (DEBUG) Log.d(TAG, "setKeyguardEnabled(" + enabled + ")");
746
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800747 mExternallyEnabled = enabled;
748
749 if (!enabled && mShowing) {
750 if (mExitSecureCallback != null) {
751 if (DEBUG) Log.d(TAG, "in process of verifyUnlock request, ignoring");
752 // we're in the process of handling a request to verify the user
753 // can get past the keyguard. ignore extraneous requests to disable / reenable
754 return;
755 }
756
757 // hiding keyguard that is showing, remember to reshow later
758 if (DEBUG) Log.d(TAG, "remembering to reshow, hiding keyguard, "
759 + "disabling status bar expansion");
760 mNeedToReshowWhenReenabled = true;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800761 hideLocked();
762 } else if (enabled && mNeedToReshowWhenReenabled) {
763 // reenabled after previously hidden, reshow
764 if (DEBUG) Log.d(TAG, "previously hidden, reshowing, reenabling "
765 + "status bar expansion");
766 mNeedToReshowWhenReenabled = false;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800767
768 if (mExitSecureCallback != null) {
769 if (DEBUG) Log.d(TAG, "onKeyguardExitResult(false), resetting");
Jim Miller5ecd8112013-01-09 18:50:26 -0800770 try {
771 mExitSecureCallback.onKeyguardExitResult(false);
772 } catch (RemoteException e) {
Jim Miller25190572013-02-28 17:36:24 -0800773 Slog.w(TAG, "Failed to call onKeyguardExitResult(false)", e);
Jim Miller5ecd8112013-01-09 18:50:26 -0800774 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800775 mExitSecureCallback = null;
Adam Cohenf7522022012-10-03 20:03:18 -0700776 resetStateLocked(null);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800777 } else {
Adam Cohenf7522022012-10-03 20:03:18 -0700778 showLocked(null);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800779
780 // block until we know the keygaurd is done drawing (and post a message
781 // to unblock us after a timeout so we don't risk blocking too long
782 // and causing an ANR).
783 mWaitingUntilKeyguardVisible = true;
784 mHandler.sendEmptyMessageDelayed(KEYGUARD_DONE_DRAWING, KEYGUARD_DONE_DRAWING_TIMEOUT_MS);
785 if (DEBUG) Log.d(TAG, "waiting until mWaitingUntilKeyguardVisible is false");
786 while (mWaitingUntilKeyguardVisible) {
787 try {
788 wait();
789 } catch (InterruptedException e) {
790 Thread.currentThread().interrupt();
791 }
792 }
793 if (DEBUG) Log.d(TAG, "done waiting for mWaitingUntilKeyguardVisible");
794 }
795 }
796 }
797 }
798
799 /**
800 * @see android.app.KeyguardManager#exitKeyguardSecurely
801 */
Jim Miller25190572013-02-28 17:36:24 -0800802 public void verifyUnlock(IKeyguardExitCallback callback) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800803 synchronized (this) {
804 if (DEBUG) Log.d(TAG, "verifyUnlock");
805 if (!mUpdateMonitor.isDeviceProvisioned()) {
806 // don't allow this api when the device isn't provisioned
807 if (DEBUG) Log.d(TAG, "ignoring because device isn't provisioned");
Jim Miller5ecd8112013-01-09 18:50:26 -0800808 try {
Jim Miller25190572013-02-28 17:36:24 -0800809 callback.onKeyguardExitResult(false);
Jim Miller5ecd8112013-01-09 18:50:26 -0800810 } catch (RemoteException e) {
Jim Miller25190572013-02-28 17:36:24 -0800811 Slog.w(TAG, "Failed to call onKeyguardExitResult(false)", e);
Jim Miller5ecd8112013-01-09 18:50:26 -0800812 }
Mike Lockwood5d258b62009-12-02 13:50:45 -0500813 } else if (mExternallyEnabled) {
814 // this only applies when the user has externally disabled the
815 // keyguard. this is unexpected and means the user is not
816 // using the api properly.
817 Log.w(TAG, "verifyUnlock called when not externally disabled");
Jim Miller5ecd8112013-01-09 18:50:26 -0800818 try {
Jim Miller25190572013-02-28 17:36:24 -0800819 callback.onKeyguardExitResult(false);
Jim Miller5ecd8112013-01-09 18:50:26 -0800820 } catch (RemoteException e) {
Jim Miller25190572013-02-28 17:36:24 -0800821 Slog.w(TAG, "Failed to call onKeyguardExitResult(false)", e);
Jim Miller5ecd8112013-01-09 18:50:26 -0800822 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800823 } else if (mExitSecureCallback != null) {
824 // already in progress with someone else
Jim Miller5ecd8112013-01-09 18:50:26 -0800825 try {
Jim Miller25190572013-02-28 17:36:24 -0800826 callback.onKeyguardExitResult(false);
Jim Miller5ecd8112013-01-09 18:50:26 -0800827 } catch (RemoteException e) {
Jim Miller25190572013-02-28 17:36:24 -0800828 Slog.w(TAG, "Failed to call onKeyguardExitResult(false)", e);
Jim Miller5ecd8112013-01-09 18:50:26 -0800829 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800830 } else {
Jim Miller25190572013-02-28 17:36:24 -0800831 mExitSecureCallback = callback;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800832 verifyUnlockLocked();
833 }
834 }
835 }
836
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800837 /**
838 * Is the keyguard currently showing?
839 */
840 public boolean isShowing() {
841 return mShowing;
842 }
843
844 /**
Dianne Hackborn9dc06cc2009-11-17 18:19:23 -0800845 * Is the keyguard currently showing and not being force hidden?
846 */
847 public boolean isShowingAndNotHidden() {
848 return mShowing && !mHidden;
849 }
850
851 /**
Mike Lockwood09a40402009-11-08 00:33:23 -0500852 * Notify us when the keyguard is hidden by another window
853 */
854 public void setHidden(boolean isHidden) {
Mike Lockwoodb17b2fb2009-11-09 16:01:37 -0500855 if (DEBUG) Log.d(TAG, "setHidden " + isHidden);
Danielle Millettf6d0fc12012-10-23 16:16:52 -0400856 mUpdateMonitor.sendKeyguardVisibilityChanged(!isHidden);
Mike Lockwood9200a392009-11-17 20:25:58 -0500857 mHandler.removeMessages(SET_HIDDEN);
858 Message msg = mHandler.obtainMessage(SET_HIDDEN, (isHidden ? 1 : 0), 0);
859 mHandler.sendMessage(msg);
860 }
861
862 /**
863 * Handles SET_HIDDEN message sent by setHidden()
864 */
865 private void handleSetHidden(boolean isHidden) {
Mike Lockwood09a40402009-11-08 00:33:23 -0500866 synchronized (KeyguardViewMediator.this) {
Mike Lockwood9200a392009-11-17 20:25:58 -0500867 if (mHidden != isHidden) {
868 mHidden = isHidden;
Dianne Hackbornff5b1582012-04-12 17:24:07 -0700869 updateActivityLockScreenState();
Mike Lockwood5f892c12009-11-19 23:39:13 -0500870 adjustStatusBarLocked();
Mike Lockwood9200a392009-11-17 20:25:58 -0500871 }
Mike Lockwood09a40402009-11-08 00:33:23 -0500872 }
873 }
874
875 /**
Mike Lockwood28569302010-01-28 11:54:40 -0500876 * Used by PhoneWindowManager to enable the keyguard due to a user activity timeout.
877 * This must be safe to call from any thread and with any window manager locks held.
878 */
Adam Cohenf7522022012-10-03 20:03:18 -0700879 public void doKeyguardTimeout(Bundle options) {
Mike Lockwood28569302010-01-28 11:54:40 -0500880 mHandler.removeMessages(KEYGUARD_TIMEOUT);
Adam Cohenf7522022012-10-03 20:03:18 -0700881 Message msg = mHandler.obtainMessage(KEYGUARD_TIMEOUT, options);
Mike Lockwood28569302010-01-28 11:54:40 -0500882 mHandler.sendMessage(msg);
883 }
884
885 /**
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800886 * Given the state of the keyguard, is the input restricted?
887 * Input is restricted when the keyguard is showing, or when the keyguard
888 * was suppressed by an app that disabled the keyguard or we haven't been provisioned yet.
889 */
890 public boolean isInputRestricted() {
891 return mShowing || mNeedToReshowWhenReenabled || !mUpdateMonitor.isDeviceProvisioned();
892 }
893
Dianne Hackbornb446e972009-09-20 15:23:25 -0700894 /**
Craig Mautnerad09bcc2012-10-08 13:33:11 -0700895 * Enable the keyguard if the settings are appropriate.
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800896 */
Adam Cohenf7522022012-10-03 20:03:18 -0700897 private void doKeyguardLocked(Bundle options) {
Dianne Hackborn29aae6f2011-08-18 18:30:09 -0700898 // if another app is disabling us, don't show
899 if (!mExternallyEnabled) {
900 if (DEBUG) Log.d(TAG, "doKeyguard: not showing because externally disabled");
Karl Rosaenab100082009-03-24 22:35:17 -0700901
Dianne Hackborn29aae6f2011-08-18 18:30:09 -0700902 // note: we *should* set mNeedToReshowWhenReenabled=true here, but that makes
903 // for an occasional ugly flicker in this situation:
904 // 1) receive a call with the screen on (no keyguard) or make a call
905 // 2) screen times out
906 // 3) user hits key to turn screen back on
907 // instead, we reenable the keyguard when we know the screen is off and the call
908 // ends (see the broadcast receiver below)
909 // TODO: clean this up when we have better support at the window manager level
910 // for apps that wish to be on top of the keyguard
Dianne Hackborn38e29a62011-09-18 14:43:08 -0700911 return;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800912 }
Dianne Hackborn29aae6f2011-08-18 18:30:09 -0700913
Dan Sandlera5e0f412014-01-23 15:11:54 -0500914 // note whether notification access should be allowed
915 mAllowNotificationsWhenSecure = ENABLE_SECURE_STATUS_BAR_EXPAND
916 && 0 != Settings.Secure.getInt(
917 mContext.getContentResolver(),
918 Settings.Secure.LOCK_SCREEN_ALLOW_NOTIFICATIONS,
919 ALLOW_NOTIFICATIONS_DEFAULT ? 1 : 0);
920
Dianne Hackborn29aae6f2011-08-18 18:30:09 -0700921 // if the keyguard is already showing, don't bother
922 if (mKeyguardViewManager.isShowing()) {
923 if (DEBUG) Log.d(TAG, "doKeyguard: not showing because it is already showing");
Dianne Hackborn38e29a62011-09-18 14:43:08 -0700924 return;
Dianne Hackborn29aae6f2011-08-18 18:30:09 -0700925 }
926
927 // if the setup wizard hasn't run yet, don't show
928 final boolean requireSim = !SystemProperties.getBoolean("keyguard.no_require_sim",
929 false);
930 final boolean provisioned = mUpdateMonitor.isDeviceProvisioned();
Wink Savillea639b312012-07-10 12:37:54 -0700931 final IccCardConstants.State state = mUpdateMonitor.getSimState();
Dianne Hackborn29aae6f2011-08-18 18:30:09 -0700932 final boolean lockedOrMissing = state.isPinLocked()
Wink Savillea639b312012-07-10 12:37:54 -0700933 || ((state == IccCardConstants.State.ABSENT
934 || state == IccCardConstants.State.PERM_DISABLED)
Jim Miller9fa1ada2011-10-31 18:21:49 -0700935 && requireSim);
Dianne Hackborn29aae6f2011-08-18 18:30:09 -0700936
937 if (!lockedOrMissing && !provisioned) {
938 if (DEBUG) Log.d(TAG, "doKeyguard: not showing because device isn't provisioned"
939 + " and the sim is not locked or missing");
Dianne Hackborn38e29a62011-09-18 14:43:08 -0700940 return;
Dianne Hackborn29aae6f2011-08-18 18:30:09 -0700941 }
942
Amith Yamasani920ace02012-09-20 22:15:37 -0700943 if (mUserManager.getUsers(true).size() < 2
Amith Yamasanib70ff9a2012-09-07 18:28:11 -0700944 && mLockPatternUtils.isLockScreenDisabled() && !lockedOrMissing) {
Dianne Hackborn29aae6f2011-08-18 18:30:09 -0700945 if (DEBUG) Log.d(TAG, "doKeyguard: not showing because lockscreen is off");
Dianne Hackborn38e29a62011-09-18 14:43:08 -0700946 return;
Dianne Hackborn29aae6f2011-08-18 18:30:09 -0700947 }
948
949 if (DEBUG) Log.d(TAG, "doKeyguard: showing the lock screen");
Adam Cohenf7522022012-10-03 20:03:18 -0700950 showLocked(options);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800951 }
952
953 /**
Craig Mautnerad09bcc2012-10-08 13:33:11 -0700954 * Dismiss the keyguard through the security layers.
955 */
Jim Miller60013792013-10-03 18:31:34 -0700956 public void handleDismiss() {
Jim Miller87d03662012-11-05 20:28:09 -0800957 if (mShowing && !mHidden) {
958 mKeyguardViewManager.dismiss();
959 }
Craig Mautnerad09bcc2012-10-08 13:33:11 -0700960 }
961
Jim Miller60013792013-10-03 18:31:34 -0700962 public void dismiss() {
963 mHandler.sendEmptyMessage(DISMISS);
964 }
965
Craig Mautnerad09bcc2012-10-08 13:33:11 -0700966 /**
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800967 * Send message to keyguard telling it to reset its state.
Adam Cohenf7522022012-10-03 20:03:18 -0700968 * @param options options about how to show the keyguard
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800969 * @see #handleReset()
970 */
Adam Cohenf7522022012-10-03 20:03:18 -0700971 private void resetStateLocked(Bundle options) {
Jim Miller4894a012013-04-03 15:23:55 -0700972 if (DEBUG) Log.e(TAG, "resetStateLocked");
Adam Cohenf7522022012-10-03 20:03:18 -0700973 Message msg = mHandler.obtainMessage(RESET, options);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800974 mHandler.sendMessage(msg);
975 }
976
977 /**
978 * Send message to keyguard telling it to verify unlock
979 * @see #handleVerifyUnlock()
980 */
981 private void verifyUnlockLocked() {
982 if (DEBUG) Log.d(TAG, "verifyUnlockLocked");
983 mHandler.sendEmptyMessage(VERIFY_UNLOCK);
984 }
985
986
987 /**
988 * Send a message to keyguard telling it the screen just turned on.
989 * @see #onScreenTurnedOff(int)
990 * @see #handleNotifyScreenOff
991 */
992 private void notifyScreenOffLocked() {
993 if (DEBUG) Log.d(TAG, "notifyScreenOffLocked");
994 mHandler.sendEmptyMessage(NOTIFY_SCREEN_OFF);
995 }
996
997 /**
998 * Send a message to keyguard telling it the screen just turned on.
Jim Miller9c20d0e2010-01-20 15:00:23 -0800999 * @see #onScreenTurnedOn()
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001000 * @see #handleNotifyScreenOn
1001 */
Jim Miller25190572013-02-28 17:36:24 -08001002 private void notifyScreenOnLocked(IKeyguardShowCallback result) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001003 if (DEBUG) Log.d(TAG, "notifyScreenOnLocked");
Jim Miller5ecd8112013-01-09 18:50:26 -08001004 Message msg = mHandler.obtainMessage(NOTIFY_SCREEN_ON, result);
Dianne Hackborn38e29a62011-09-18 14:43:08 -07001005 mHandler.sendMessage(msg);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001006 }
1007
1008 /**
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001009 * Send message to keyguard telling it to show itself
1010 * @see #handleShow()
1011 */
Adam Cohenf7522022012-10-03 20:03:18 -07001012 private void showLocked(Bundle options) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001013 if (DEBUG) Log.d(TAG, "showLocked");
Mike Lockwood674d3e42009-10-06 09:28:54 -04001014 // ensure we stay awake until we are finished displaying the keyguard
1015 mShowKeyguardWakeLock.acquire();
Adam Cohenf7522022012-10-03 20:03:18 -07001016 Message msg = mHandler.obtainMessage(SHOW, options);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001017 mHandler.sendMessage(msg);
1018 }
1019
1020 /**
1021 * Send message to keyguard telling it to hide itself
Jim Miller9c20d0e2010-01-20 15:00:23 -08001022 * @see #handleHide()
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001023 */
1024 private void hideLocked() {
1025 if (DEBUG) Log.d(TAG, "hideLocked");
1026 Message msg = mHandler.obtainMessage(HIDE);
1027 mHandler.sendMessage(msg);
1028 }
1029
Dianne Hackbornb446e972009-09-20 15:23:25 -07001030 public boolean isSecure() {
Jim Millerdcb3d842012-08-23 19:18:12 -07001031 return mLockPatternUtils.isSecure()
1032 || KeyguardUpdateMonitor.getInstance(mContext).isSimPinSecure();
Dianne Hackbornb446e972009-09-20 15:23:25 -07001033 }
Jim Miller9c20d0e2010-01-20 15:00:23 -08001034
Craig Mautnerf1b67412012-09-19 13:18:29 -07001035 /**
1036 * Update the newUserId. Call while holding WindowManagerService lock.
Jim Milleree82f8f2012-10-01 16:26:18 -07001037 * NOTE: Should only be called by KeyguardViewMediator in response to the user id changing.
1038 *
Craig Mautnerf1b67412012-09-19 13:18:29 -07001039 * @param newUserId The id of the incoming user.
1040 */
1041 public void setCurrentUser(int newUserId) {
1042 mLockPatternUtils.setCurrentUser(newUserId);
1043 }
1044
Jim Millerbbf1a742012-07-17 18:30:30 -07001045 private final BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
Amith Yamasani52c489c2012-03-28 11:42:42 -07001046 @Override
1047 public void onReceive(Context context, Intent intent) {
Jim Millerbbf1a742012-07-17 18:30:30 -07001048 if (DELAYED_KEYGUARD_ACTION.equals(intent.getAction())) {
1049 final int sequence = intent.getIntExtra("seq", 0);
Jim Millerf3447352011-08-07 14:00:09 -07001050 if (DEBUG) Log.d(TAG, "received DELAYED_KEYGUARD_ACTION with seq = "
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001051 + sequence + ", mDelayedShowingSequence = " + mDelayedShowingSequence);
Dianne Hackborn29aae6f2011-08-18 18:30:09 -07001052 synchronized (KeyguardViewMediator.this) {
1053 if (mDelayedShowingSequence == sequence) {
Jim Millerbbf1a742012-07-17 18:30:30 -07001054 // Don't play lockscreen SFX if the screen went off due to timeout.
Dianne Hackborn29aae6f2011-08-18 18:30:09 -07001055 mSuppressNextLockSound = true;
Jim Miller5ecd8112013-01-09 18:50:26 -08001056 doKeyguardLocked(null);
Dianne Hackborn29aae6f2011-08-18 18:30:09 -07001057 }
Daniel Sandlerf2d8e742010-02-22 13:09:48 -05001058 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001059 }
1060 }
1061 };
1062
Dianne Hackborn05726582009-09-22 17:28:24 -07001063 public void keyguardDone(boolean authenticated, boolean wakeup) {
Jim Miller60013792013-10-03 18:31:34 -07001064 if (DEBUG) Log.d(TAG, "keyguardDone(" + authenticated + ")");
1065 EventLog.writeEvent(70000, 2);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001066 synchronized (this) {
Jim Miller60013792013-10-03 18:31:34 -07001067 mKeyguardDonePending = false;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001068 }
Jim Miller60013792013-10-03 18:31:34 -07001069 Message msg = mHandler.obtainMessage(KEYGUARD_DONE, authenticated ? 1 : 0, wakeup ? 1 : 0);
1070 mHandler.sendMessage(msg);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001071 }
1072
1073 /**
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001074 * This handler will be associated with the policy thread, which will also
1075 * be the UI thread of the keyguard. Since the apis of the policy, and therefore
1076 * this class, can be called by other threads, any action that directly
1077 * interacts with the keyguard ui should be posted to this handler, rather
1078 * than called directly.
1079 */
Jim Millerdcb3d842012-08-23 19:18:12 -07001080 private Handler mHandler = new Handler(Looper.myLooper(), null, true /*async*/) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001081 @Override
Wink Saville37c124c2009-04-02 01:37:02 -07001082 public void handleMessage(Message msg) {
1083 switch (msg.what) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001084 case SHOW:
Adam Cohenf7522022012-10-03 20:03:18 -07001085 handleShow((Bundle) msg.obj);
Jim Miller60013792013-10-03 18:31:34 -07001086 break;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001087 case HIDE:
1088 handleHide();
Jim Miller60013792013-10-03 18:31:34 -07001089 break;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001090 case RESET:
Adam Cohenf7522022012-10-03 20:03:18 -07001091 handleReset((Bundle) msg.obj);
Jim Miller60013792013-10-03 18:31:34 -07001092 break;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001093 case VERIFY_UNLOCK:
1094 handleVerifyUnlock();
Jim Miller60013792013-10-03 18:31:34 -07001095 break;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001096 case NOTIFY_SCREEN_OFF:
1097 handleNotifyScreenOff();
Jim Miller60013792013-10-03 18:31:34 -07001098 break;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001099 case NOTIFY_SCREEN_ON:
Jim Miller25190572013-02-28 17:36:24 -08001100 handleNotifyScreenOn((IKeyguardShowCallback) msg.obj);
Jim Miller60013792013-10-03 18:31:34 -07001101 break;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001102 case KEYGUARD_DONE:
Jim Millere51cf7ae2013-06-25 18:31:56 -07001103 handleKeyguardDone(msg.arg1 != 0, msg.arg2 != 0);
Jim Miller60013792013-10-03 18:31:34 -07001104 break;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001105 case KEYGUARD_DONE_DRAWING:
1106 handleKeyguardDoneDrawing();
Jim Miller60013792013-10-03 18:31:34 -07001107 break;
Dianne Hackborn39c2d712009-09-22 11:41:31 -07001108 case KEYGUARD_DONE_AUTHENTICATING:
Jim Millerdcb3d842012-08-23 19:18:12 -07001109 keyguardDone(true, true);
Jim Miller60013792013-10-03 18:31:34 -07001110 break;
Mike Lockwood9200a392009-11-17 20:25:58 -05001111 case SET_HIDDEN:
1112 handleSetHidden(msg.arg1 != 0);
1113 break;
Mike Lockwood28569302010-01-28 11:54:40 -05001114 case KEYGUARD_TIMEOUT:
Dianne Hackborn29aae6f2011-08-18 18:30:09 -07001115 synchronized (KeyguardViewMediator.this) {
Adam Cohenf7522022012-10-03 20:03:18 -07001116 doKeyguardLocked((Bundle) msg.obj);
Dianne Hackborn29aae6f2011-08-18 18:30:09 -07001117 }
1118 break;
Jim Miller4eeb4f62012-11-08 00:04:29 -08001119 case SHOW_ASSISTANT:
1120 handleShowAssistant();
1121 break;
Jim Millercaf24fc2013-09-10 18:37:01 -07001122 case DISPATCH_EVENT:
1123 handleDispatchEvent((MotionEvent) msg.obj);
1124 break;
Jim Miller138f25d2013-09-25 13:46:58 -07001125 case LAUNCH_CAMERA:
1126 handleLaunchCamera();
1127 break;
Jim Miller60013792013-10-03 18:31:34 -07001128 case DISMISS:
1129 handleDismiss();
1130 break;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001131 }
1132 }
1133 };
1134
1135 /**
1136 * @see #keyguardDone
1137 * @see #KEYGUARD_DONE
1138 */
Jim Millere51cf7ae2013-06-25 18:31:56 -07001139 private void handleKeyguardDone(boolean authenticated, boolean wakeup) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001140 if (DEBUG) Log.d(TAG, "handleKeyguardDone");
Jim Millere51cf7ae2013-06-25 18:31:56 -07001141
1142 if (authenticated) {
1143 mUpdateMonitor.clearFailedUnlockAttempts();
Dianne Hackborn05726582009-09-22 17:28:24 -07001144 }
Jeff Sharkey6a25cbd2012-08-23 12:14:26 -07001145
Jim Millere51cf7ae2013-06-25 18:31:56 -07001146 if (mExitSecureCallback != null) {
1147 try {
1148 mExitSecureCallback.onKeyguardExitResult(authenticated);
1149 } catch (RemoteException e) {
1150 Slog.w(TAG, "Failed to call onKeyguardExitResult(" + authenticated + ")", e);
1151 }
1152
1153 mExitSecureCallback = null;
1154
1155 if (authenticated) {
1156 // after succesfully exiting securely, no need to reshow
1157 // the keyguard when they've released the lock
1158 mExternallyEnabled = true;
1159 mNeedToReshowWhenReenabled = false;
1160 }
1161 }
1162
1163 handleHide();
Jim Miller3fd47af2012-09-21 19:55:27 -07001164 sendUserPresentBroadcast();
1165 }
1166
Jim Miller138f25d2013-09-25 13:46:58 -07001167 protected void handleLaunchCamera() {
1168 mKeyguardViewManager.launchCamera();
1169 }
1170
Jim Millercaf24fc2013-09-10 18:37:01 -07001171 protected void handleDispatchEvent(MotionEvent event) {
1172 mKeyguardViewManager.dispatch(event);
1173 }
1174
Jim Miller3fd47af2012-09-21 19:55:27 -07001175 private void sendUserPresentBroadcast() {
Jim Miller17f509a2013-02-28 18:36:12 -08001176 final UserHandle currentUser = new UserHandle(mLockPatternUtils.getCurrentUser());
1177 mContext.sendBroadcastAsUser(USER_PRESENT_INTENT, currentUser);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001178 }
1179
1180 /**
1181 * @see #keyguardDoneDrawing
1182 * @see #KEYGUARD_DONE_DRAWING
1183 */
1184 private void handleKeyguardDoneDrawing() {
1185 synchronized(this) {
Jim Miller5ecd8112013-01-09 18:50:26 -08001186 if (DEBUG) Log.d(TAG, "handleKeyguardDoneDrawing");
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001187 if (mWaitingUntilKeyguardVisible) {
1188 if (DEBUG) Log.d(TAG, "handleKeyguardDoneDrawing: notifying mWaitingUntilKeyguardVisible");
1189 mWaitingUntilKeyguardVisible = false;
1190 notifyAll();
1191
1192 // there will usually be two of these sent, one as a timeout, and one
1193 // as a result of the callback, so remove any remaining messages from
1194 // the queue
1195 mHandler.removeMessages(KEYGUARD_DONE_DRAWING);
1196 }
1197 }
1198 }
1199
Daniel Sandlerdb783bd2010-02-11 15:27:37 -05001200 private void playSounds(boolean locked) {
1201 // User feedback for keyguard.
Daniel Sandler0060a9b2010-03-15 23:09:57 -04001202
1203 if (mSuppressNextLockSound) {
1204 mSuppressNextLockSound = false;
1205 return;
1206 }
1207
Daniel Sandlerdb783bd2010-02-11 15:27:37 -05001208 final ContentResolver cr = mContext.getContentResolver();
Amith Yamasani8cb751b2011-09-30 15:39:41 -07001209 if (Settings.System.getInt(cr, Settings.System.LOCKSCREEN_SOUNDS_ENABLED, 1) == 1) {
Marco Nelissend5545bd2011-09-29 12:49:17 -07001210 final int whichSound = locked
1211 ? mLockSoundId
1212 : mUnlockSoundId;
1213 mLockSounds.stop(mLockSoundStreamId);
Amith Yamasani8cb751b2011-09-30 15:39:41 -07001214 // Init mAudioManager
1215 if (mAudioManager == null) {
1216 mAudioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
1217 if (mAudioManager == null) return;
Eric Laurent6d517662012-04-23 18:42:39 -07001218 mMasterStreamType = mAudioManager.getMasterStreamType();
Amith Yamasani8cb751b2011-09-30 15:39:41 -07001219 }
1220 // If the stream is muted, don't play the sound
Eric Laurent6d517662012-04-23 18:42:39 -07001221 if (mAudioManager.isStreamMute(mMasterStreamType)) return;
Amith Yamasani8cb751b2011-09-30 15:39:41 -07001222
Jean-Michel Trivic55b3932012-06-05 11:57:59 -07001223 mLockSoundStreamId = mLockSounds.play(whichSound,
1224 mLockSoundVolume, mLockSoundVolume, 1/*priortiy*/, 0/*loop*/, 1.0f/*rate*/);
Daniel Sandlerdb783bd2010-02-11 15:27:37 -05001225 }
Jim Miller2a98a4c2010-11-19 18:49:26 -08001226 }
Daniel Sandlerdb783bd2010-02-11 15:27:37 -05001227
Dianne Hackbornff5b1582012-04-12 17:24:07 -07001228 private void updateActivityLockScreenState() {
1229 try {
Jim Miller60013792013-10-03 18:31:34 -07001230 ActivityManagerNative.getDefault().setLockScreenShown(mShowing && !mHidden);
Dianne Hackbornff5b1582012-04-12 17:24:07 -07001231 } catch (RemoteException e) {
1232 }
1233 }
1234
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001235 /**
1236 * Handle message sent by {@link #showLocked}.
1237 * @see #SHOW
1238 */
Adam Cohenf7522022012-10-03 20:03:18 -07001239 private void handleShow(Bundle options) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001240 synchronized (KeyguardViewMediator.this) {
Jim Miller5ecd8112013-01-09 18:50:26 -08001241 if (!mSystemReady) {
1242 if (DEBUG) Log.d(TAG, "ignoring handleShow because system is not ready.");
1243 return;
1244 } else {
1245 if (DEBUG) Log.d(TAG, "handleShow");
1246 }
Jim Miller9c20d0e2010-01-20 15:00:23 -08001247
Adam Cohenf7522022012-10-03 20:03:18 -07001248 mKeyguardViewManager.show(options);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001249 mShowing = true;
John Spurlock14adfe42012-11-08 10:29:26 -05001250 mKeyguardDonePending = false;
Dianne Hackbornff5b1582012-04-12 17:24:07 -07001251 updateActivityLockScreenState();
Mike Lockwood5f892c12009-11-19 23:39:13 -05001252 adjustStatusBarLocked();
Jeff Brown3dc524b2012-09-30 19:49:11 -07001253 userActivity();
Dianne Hackborn4994c662009-09-23 22:21:23 -07001254 try {
1255 ActivityManagerNative.getDefault().closeSystemDialogs("lock");
1256 } catch (RemoteException e) {
1257 }
Dianne Hackborn29aae6f2011-08-18 18:30:09 -07001258
1259 // Do this at the end to not slow down display of the keyguard.
1260 playSounds(true);
1261
Mike Lockwood674d3e42009-10-06 09:28:54 -04001262 mShowKeyguardWakeLock.release();
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001263 }
Jim Miller31921482013-11-06 20:43:55 -08001264 mKeyguardDisplayManager.show();
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001265 }
1266
1267 /**
1268 * Handle message sent by {@link #hideLocked()}
1269 * @see #HIDE
1270 */
1271 private void handleHide() {
1272 synchronized (KeyguardViewMediator.this) {
1273 if (DEBUG) Log.d(TAG, "handleHide");
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001274
Daniel Sandlerf2d8e742010-02-22 13:09:48 -05001275 // only play "unlock" noises if not on a call (since the incall UI
1276 // disables the keyguard)
1277 if (TelephonyManager.EXTRA_STATE_IDLE.equals(mPhoneState)) {
1278 playSounds(false);
1279 }
Daniel Sandlerdb783bd2010-02-11 15:27:37 -05001280
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001281 mKeyguardViewManager.hide();
1282 mShowing = false;
John Spurlock14adfe42012-11-08 10:29:26 -05001283 mKeyguardDonePending = false;
Dianne Hackbornff5b1582012-04-12 17:24:07 -07001284 updateActivityLockScreenState();
Mike Lockwood5f892c12009-11-19 23:39:13 -05001285 adjustStatusBarLocked();
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001286 }
1287 }
1288
Mike Lockwood5f892c12009-11-19 23:39:13 -05001289 private void adjustStatusBarLocked() {
1290 if (mStatusBarManager == null) {
1291 mStatusBarManager = (StatusBarManager)
1292 mContext.getSystemService(Context.STATUS_BAR_SERVICE);
1293 }
1294 if (mStatusBarManager == null) {
1295 Log.w(TAG, "Could not get status bar manager");
1296 } else {
Daniel Sandlerdba93562011-10-06 16:39:58 -04001297 // Disable aspects of the system/status/navigation bars that must not be re-enabled by
1298 // windows that appear on top, ever
Jeff Sharkeyf52c70b2011-08-30 22:05:47 -07001299 int flags = StatusBarManager.DISABLE_NONE;
Mike Lockwoode3646dd2011-09-01 12:46:28 -04001300 if (mShowing) {
Jim Millere23ab8b2012-09-16 15:45:44 -07001301 // Permanently disable components not available when keyguard is enabled
1302 // (like recents). Temporary enable/disable (e.g. the "back" button) are
1303 // done in KeyguardHostView.
Daniel Sandlerdba93562011-10-06 16:39:58 -04001304 flags |= StatusBarManager.DISABLE_RECENT;
Dan Sandlera5e0f412014-01-23 15:11:54 -05001305 if ((isSecure() && !mAllowNotificationsWhenSecure)
1306 || !ENABLE_INSECURE_STATUS_BAR_EXPAND) {
Mike Lockwoode3646dd2011-09-01 12:46:28 -04001307 // showing secure lockscreen; disable expanding.
1308 flags |= StatusBarManager.DISABLE_EXPAND;
1309 }
Jeff Sharkey4519a022011-09-07 23:24:53 -07001310 if (isSecure()) {
Dan Sandlera5e0f412014-01-23 15:11:54 -05001311 // showing secure lockscreen; disable ticker and switch private notifications
1312 // to show their public versions, if available.
1313 flags |= StatusBarManager.DISABLE_PRIVATE_NOTIFICATIONS;
Jeff Sharkey4519a022011-09-07 23:24:53 -07001314 }
John Spurlock43d84512012-11-09 10:27:33 -05001315 if (!isAssistantAvailable()) {
1316 flags |= StatusBarManager.DISABLE_SEARCH;
1317 }
Jeff Sharkeyf52c70b2011-08-30 22:05:47 -07001318 }
1319
1320 if (DEBUG) {
Jeff Sharkey4519a022011-09-07 23:24:53 -07001321 Log.d(TAG, "adjustStatusBarLocked: mShowing=" + mShowing + " mHidden=" + mHidden
1322 + " isSecure=" + isSecure() + " --> flags=0x" + Integer.toHexString(flags));
Jeff Sharkeyf52c70b2011-08-30 22:05:47 -07001323 }
1324
Jim Millerd6523da2012-10-21 16:47:02 -07001325 if (!(mContext instanceof Activity)) {
1326 mStatusBarManager.disable(flags);
1327 }
Mike Lockwood5f892c12009-11-19 23:39:13 -05001328 }
1329 }
1330
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001331 /**
Adam Cohenf7522022012-10-03 20:03:18 -07001332 * Handle message sent by {@link #resetStateLocked(Bundle)}
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001333 * @see #RESET
1334 */
Adam Cohenf7522022012-10-03 20:03:18 -07001335 private void handleReset(Bundle options) {
Chris Wrenf41c61b2012-11-29 15:19:54 -05001336 if (options == null) {
1337 options = new Bundle();
1338 }
1339 options.putBoolean(KeyguardViewManager.IS_SWITCHING_USER, mSwitchingUser);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001340 synchronized (KeyguardViewMediator.this) {
1341 if (DEBUG) Log.d(TAG, "handleReset");
Adam Cohenf7522022012-10-03 20:03:18 -07001342 mKeyguardViewManager.reset(options);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001343 }
1344 }
1345
1346 /**
1347 * Handle message sent by {@link #verifyUnlock}
Craig Mautner904732c2012-10-17 15:20:24 -07001348 * @see #VERIFY_UNLOCK
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001349 */
1350 private void handleVerifyUnlock() {
1351 synchronized (KeyguardViewMediator.this) {
1352 if (DEBUG) Log.d(TAG, "handleVerifyUnlock");
1353 mKeyguardViewManager.verifyUnlock();
1354 mShowing = true;
Dianne Hackbornff5b1582012-04-12 17:24:07 -07001355 updateActivityLockScreenState();
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001356 }
1357 }
1358
1359 /**
1360 * Handle message sent by {@link #notifyScreenOffLocked()}
1361 * @see #NOTIFY_SCREEN_OFF
1362 */
1363 private void handleNotifyScreenOff() {
1364 synchronized (KeyguardViewMediator.this) {
1365 if (DEBUG) Log.d(TAG, "handleNotifyScreenOff");
1366 mKeyguardViewManager.onScreenTurnedOff();
1367 }
1368 }
1369
1370 /**
1371 * Handle message sent by {@link #notifyScreenOnLocked()}
1372 * @see #NOTIFY_SCREEN_ON
1373 */
Jim Miller25190572013-02-28 17:36:24 -08001374 private void handleNotifyScreenOn(IKeyguardShowCallback callback) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001375 synchronized (KeyguardViewMediator.this) {
1376 if (DEBUG) Log.d(TAG, "handleNotifyScreenOn");
Jim Miller25190572013-02-28 17:36:24 -08001377 mKeyguardViewManager.onScreenTurnedOn(callback);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001378 }
1379 }
Jeff Sharkey054340d2011-09-01 22:28:03 -07001380
John Spurlock34c4fe52012-11-07 10:12:29 -05001381 public boolean isDismissable() {
1382 return mKeyguardDonePending || !isSecure();
1383 }
1384
Jim Miller4eeb4f62012-11-08 00:04:29 -08001385 public void showAssistant() {
1386 Message msg = mHandler.obtainMessage(SHOW_ASSISTANT);
1387 mHandler.sendMessage(msg);
1388 }
1389
1390 public void handleShowAssistant() {
1391 mKeyguardViewManager.showAssistant();
1392 }
1393
John Spurlock43d84512012-11-09 10:27:33 -05001394 private boolean isAssistantAvailable() {
1395 return mSearchManager != null
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08001396 && mSearchManager.getAssistIntent(mContext, false, UserHandle.USER_CURRENT) != null;
John Spurlock43d84512012-11-09 10:27:33 -05001397 }
Amith Yamasani6fc1d4e2013-05-08 16:43:58 -07001398
1399 public static MultiUserAvatarCache getAvatarCache() {
1400 return sMultiUserAvatarCache;
1401 }
Jim Millercaf24fc2013-09-10 18:37:01 -07001402
1403 public void dispatch(MotionEvent event) {
1404 Message msg = mHandler.obtainMessage(DISPATCH_EVENT, event);
1405 mHandler.sendMessage(msg);
1406 }
Jim Miller138f25d2013-09-25 13:46:58 -07001407
1408 public void launchCamera() {
1409 Message msg = mHandler.obtainMessage(LAUNCH_CAMERA);
1410 mHandler.sendMessage(msg);
1411 }
Jim Millere5f910a2013-10-16 18:15:46 -07001412
1413 public void onBootCompleted() {
1414 mUpdateMonitor.dispatchBootCompleted();
1415 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -08001416}