blob: baf520e0fb4d4bd05c5afa5b2a7b4d643031a2ec [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;
18
Adam Powell43a372f2013-09-27 17:43:53 -070019import android.graphics.Bitmap;
20import android.graphics.drawable.BitmapDrawable;
Jim Miller5e612cf2014-02-03 17:57:23 -080021
Jim Miller25190572013-02-28 17:36:24 -080022import com.android.internal.policy.IKeyguardShowCallback;
Jim Miller5ecd8112013-01-09 18:50:26 -080023import com.android.internal.widget.LockPatternUtils;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -080024
Jim Miller5e612cf2014-02-03 17:57:23 -080025import org.xmlpull.v1.XmlPullParser;
26
Dianne Hackbornb3756322011-08-12 13:58:13 -070027import android.app.ActivityManager;
Michael Jurka76017ca2012-11-06 16:21:09 -080028import android.appwidget.AppWidgetManager;
Jim Millerba7d94b2014-02-05 17:30:50 -080029import android.content.ContentResolver;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -080030import android.content.Context;
31import android.content.pm.ActivityInfo;
Jim Millerdcb3d842012-08-23 19:18:12 -070032import android.content.res.Configuration;
Jim Miller06cc78a2011-06-02 16:10:16 -070033import android.content.res.Resources;
John Spurlock257f2832013-09-21 18:41:53 -040034import android.graphics.Canvas;
35import android.graphics.ColorFilter;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -080036import android.graphics.PixelFormat;
John Spurlock257f2832013-09-21 18:41:53 -040037import android.graphics.PorterDuff;
Jim Miller2b84b812012-11-30 15:42:18 -080038import android.graphics.Rect;
John Spurlock257f2832013-09-21 18:41:53 -040039import android.graphics.drawable.Drawable;
Adam Cohenf7522022012-10-03 20:03:18 -070040import android.os.Bundle;
Dianne Hackborn38e29a62011-09-18 14:43:08 -070041import android.os.IBinder;
Jim Miller6bcd7322012-10-02 16:32:04 -070042import android.os.Parcelable;
Jim Miller5ecd8112013-01-09 18:50:26 -080043import android.os.RemoteException;
Jim Miller56bacd72011-09-09 14:12:54 -070044import android.os.SystemProperties;
Jim Millerba7d94b2014-02-05 17:30:50 -080045import android.provider.Settings;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -080046import android.util.Log;
Craig Mautner0cbfcff2012-09-17 16:40:28 -070047import android.util.Slog;
Jim Miller6bcd7322012-10-02 16:32:04 -070048import android.util.SparseArray;
Adam Cohenf7522022012-10-03 20:03:18 -070049import android.view.KeyEvent;
Jim Millerdcb3d842012-08-23 19:18:12 -070050import android.view.LayoutInflater;
Jim Millercaf24fc2013-09-10 18:37:01 -070051import android.view.MotionEvent;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -080052import android.view.View;
53import android.view.ViewGroup;
54import android.view.ViewManager;
55import android.view.WindowManager;
56import android.widget.FrameLayout;
57
58/**
59 * Manages creating, showing, hiding and resetting the keyguard. Calls back
Craig Mautner904732c2012-10-17 15:20:24 -070060 * via {@link KeyguardViewMediator.ViewMediatorCallback} to poke
The Android Open Source Project1f838aa2009-03-03 19:32:13 -080061 * the wake lock and report that the keyguard is done, which is in turn,
62 * reported to this class by the current {@link KeyguardViewBase}.
63 */
Jim Millerdcb3d842012-08-23 19:18:12 -070064public class KeyguardViewManager {
Jim Millera71984f2012-10-24 22:08:49 -070065 private final static boolean DEBUG = KeyguardViewMediator.DEBUG;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -080066 private static String TAG = "KeyguardViewManager";
Chris Wrenf41c61b2012-11-29 15:19:54 -050067 public final static String IS_SWITCHING_USER = "is_switching_user";
The Android Open Source Project1f838aa2009-03-03 19:32:13 -080068
Jim Miller0b9d8ac2013-11-07 19:15:49 -080069 // Delay dismissing keyguard to allow animations to complete.
70 private static final int HIDE_KEYGUARD_DELAY = 500;
71
Jim Miller9cf2c522012-10-04 22:02:29 -070072 // Timeout used for keypresses
73 static final int DIGIT_PRESS_WAKE_MILLIS = 5000;
74
The Android Open Source Project1f838aa2009-03-03 19:32:13 -080075 private final Context mContext;
76 private final ViewManager mViewManager;
Jim Millerdcb3d842012-08-23 19:18:12 -070077 private final KeyguardViewMediator.ViewMediatorCallback mViewMediatorCallback;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -080078
79 private WindowManager.LayoutParams mWindowLayoutParams;
80 private boolean mNeedsInput = false;
Jim Miller22dfe722009-10-07 01:30:21 -070081
John Spurlock257f2832013-09-21 18:41:53 -040082 private ViewManagerHost mKeyguardHost;
Jim Miller5e612cf2014-02-03 17:57:23 -080083 private KeyguardViewBase mKeyguardView;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -080084
Craig Mautnerad09bcc2012-10-08 13:33:11 -070085 private boolean mScreenOn = false;
Jim Millerdcb3d842012-08-23 19:18:12 -070086 private LockPatternUtils mLockPatternUtils;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -080087
Adam Powell43a372f2013-09-27 17:43:53 -070088 private KeyguardUpdateMonitorCallback mBackgroundChanger = new KeyguardUpdateMonitorCallback() {
89 @Override
90 public void onSetBackground(Bitmap bmp) {
91 mKeyguardHost.setCustomBackground(bmp != null ?
92 new BitmapDrawable(mContext.getResources(), bmp) : null);
Adam Powell94719072013-10-10 20:01:49 -070093 updateShowWallpaper(bmp == null);
Adam Powell43a372f2013-09-27 17:43:53 -070094 }
95 };
96
Dianne Hackborn38e29a62011-09-18 14:43:08 -070097 public interface ShowListener {
98 void onShown(IBinder windowToken);
99 };
100
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800101 /**
102 * @param context Used to create views.
103 * @param viewManager Keyguard will be attached to this.
104 * @param callback Used to notify of changes.
Jim Millerdcb3d842012-08-23 19:18:12 -0700105 * @param lockPatternUtils
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800106 */
107 public KeyguardViewManager(Context context, ViewManager viewManager,
Jim Millerdcb3d842012-08-23 19:18:12 -0700108 KeyguardViewMediator.ViewMediatorCallback callback,
109 LockPatternUtils lockPatternUtils) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800110 mContext = context;
111 mViewManager = viewManager;
Jim Millerdcb3d842012-08-23 19:18:12 -0700112 mViewMediatorCallback = callback;
113 mLockPatternUtils = lockPatternUtils;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800114 }
115
116 /**
117 * Show the keyguard. Will handle creating and attaching to the view manager
118 * lazily.
119 */
Adam Cohenf7522022012-10-03 20:03:18 -0700120 public synchronized void show(Bundle options) {
Daniel Sandler16541e42009-11-13 17:07:50 -0800121 if (DEBUG) Log.d(TAG, "show(); mKeyguardView==" + mKeyguardView);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800122
Jim Millerdcb3d842012-08-23 19:18:12 -0700123 boolean enableScreenRotation = shouldEnableScreenRotation();
124
Jim Millera71984f2012-10-24 22:08:49 -0700125 maybeCreateKeyguardLocked(enableScreenRotation, false, options);
Jim Millerdcb3d842012-08-23 19:18:12 -0700126 maybeEnableScreenRotation(enableScreenRotation);
127
Jim Millere23ab8b2012-09-16 15:45:44 -0700128 // Disable common aspects of the system/status/navigation bars that are not appropriate or
129 // useful on any keyguard screen but can be re-shown by dialogs or SHOW_WHEN_LOCKED
130 // activities. Other disabled bits are handled by the KeyguardViewMediator talking
131 // directly to the status bar service.
John Spurlock257f2832013-09-21 18:41:53 -0400132 int visFlags = View.STATUS_BAR_DISABLE_HOME;
John Spurlockbd957402013-10-03 11:38:39 -0400133 if (shouldEnableTranslucentDecor()) {
134 mWindowLayoutParams.flags |= WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
135 | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION;
John Spurlock257f2832013-09-21 18:41:53 -0400136 }
Jim Millera71984f2012-10-24 22:08:49 -0700137 if (DEBUG) Log.v(TAG, "show:setSystemUiVisibility(" + Integer.toHexString(visFlags)+")");
Jim Millerdcb3d842012-08-23 19:18:12 -0700138 mKeyguardHost.setSystemUiVisibility(visFlags);
139
140 mViewManager.updateViewLayout(mKeyguardHost, mWindowLayoutParams);
141 mKeyguardHost.setVisibility(View.VISIBLE);
142 mKeyguardView.show();
143 mKeyguardView.requestFocus();
144 }
145
146 private boolean shouldEnableScreenRotation() {
Jim Miller06cc78a2011-06-02 16:10:16 -0700147 Resources res = mContext.getResources();
Jim Millerdcb3d842012-08-23 19:18:12 -0700148 return SystemProperties.getBoolean("lockscreen.rot_override",false)
Jim Miller5ecd8112013-01-09 18:50:26 -0800149 || res.getBoolean(R.bool.config_enableLockScreenRotation);
Jim Millerdcb3d842012-08-23 19:18:12 -0700150 }
151
John Spurlockbd957402013-10-03 11:38:39 -0400152 private boolean shouldEnableTranslucentDecor() {
John Spurlock257f2832013-09-21 18:41:53 -0400153 Resources res = mContext.getResources();
John Spurlockbd957402013-10-03 11:38:39 -0400154 return res.getBoolean(R.bool.config_enableLockScreenTranslucentDecor);
John Spurlock257f2832013-09-21 18:41:53 -0400155 }
156
Jim Millerdcb3d842012-08-23 19:18:12 -0700157 class ViewManagerHost extends FrameLayout {
John Spurlock257f2832013-09-21 18:41:53 -0400158 private static final int BACKGROUND_COLOR = 0x70000000;
John Spurlock256ae672013-09-26 12:14:33 -0400159
Adam Powell43a372f2013-09-27 17:43:53 -0700160 private Drawable mCustomBackground;
161
John Spurlock257f2832013-09-21 18:41:53 -0400162 // This is a faster way to draw the background on devices without hardware acceleration
163 private final Drawable mBackgroundDrawable = new Drawable() {
164 @Override
165 public void draw(Canvas canvas) {
Adam Powell43a372f2013-09-27 17:43:53 -0700166 if (mCustomBackground != null) {
167 final Rect bounds = mCustomBackground.getBounds();
168 final int vWidth = getWidth();
169 final int vHeight = getHeight();
170
171 final int restore = canvas.save();
172 canvas.translate(-(bounds.width() - vWidth) / 2,
173 -(bounds.height() - vHeight) / 2);
174 mCustomBackground.draw(canvas);
175 canvas.restoreToCount(restore);
176 } else {
177 canvas.drawColor(BACKGROUND_COLOR, PorterDuff.Mode.SRC);
178 }
John Spurlock257f2832013-09-21 18:41:53 -0400179 }
180
181 @Override
182 public void setAlpha(int alpha) {
183 }
184
185 @Override
186 public void setColorFilter(ColorFilter cf) {
187 }
188
189 @Override
190 public int getOpacity() {
191 return PixelFormat.TRANSLUCENT;
192 }
193 };
John Spurlock256ae672013-09-26 12:14:33 -0400194
195 public ViewManagerHost(Context context) {
Jim Millerdcb3d842012-08-23 19:18:12 -0700196 super(context);
John Spurlock257f2832013-09-21 18:41:53 -0400197 setBackground(mBackgroundDrawable);
Jim Miller2b84b812012-11-30 15:42:18 -0800198 }
199
Adam Powell43a372f2013-09-27 17:43:53 -0700200 public void setCustomBackground(Drawable d) {
201 mCustomBackground = d;
202 if (d != null) {
203 d.setColorFilter(BACKGROUND_COLOR, PorterDuff.Mode.SRC_OVER);
204 }
205 computeCustomBackgroundBounds();
206 invalidate();
207 }
208
209 private void computeCustomBackgroundBounds() {
210 if (mCustomBackground == null) return; // Nothing to do
211 if (!isLaidOut()) return; // We'll do this later
212
213 final int bgWidth = mCustomBackground.getIntrinsicWidth();
214 final int bgHeight = mCustomBackground.getIntrinsicHeight();
215 final int vWidth = getWidth();
216 final int vHeight = getHeight();
217
218 final float bgAspect = (float) bgWidth / bgHeight;
219 final float vAspect = (float) vWidth / vHeight;
220
221 if (bgAspect > vAspect) {
222 mCustomBackground.setBounds(0, 0, (int) (vHeight * bgAspect), vHeight);
223 } else {
224 mCustomBackground.setBounds(0, 0, vWidth, (int) (vWidth / bgAspect));
225 }
226 }
227
228 @Override
229 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
230 super.onSizeChanged(w, h, oldw, oldh);
231 computeCustomBackgroundBounds();
232 }
233
Jim Miller2b84b812012-11-30 15:42:18 -0800234 @Override
Jim Millerdcb3d842012-08-23 19:18:12 -0700235 protected void onConfigurationChanged(Configuration newConfig) {
236 super.onConfigurationChanged(newConfig);
Jim Millera71984f2012-10-24 22:08:49 -0700237 if (mKeyguardHost.getVisibility() == View.VISIBLE) {
238 // only propagate configuration messages if we're currently showing
239 maybeCreateKeyguardLocked(shouldEnableScreenRotation(), true, null);
240 } else {
241 if (DEBUG) Log.v(TAG, "onConfigurationChanged: view not visible");
242 }
Jim Millerdcb3d842012-08-23 19:18:12 -0700243 }
Jim Miller0a34d9e2012-10-05 17:15:41 -0700244
245 @Override
246 public boolean dispatchKeyEvent(KeyEvent event) {
Jim Miller42df15e2012-12-05 15:15:00 -0800247 if (mKeyguardView != null) {
248 // Always process back and menu keys, regardless of focus
249 if (event.getAction() == KeyEvent.ACTION_DOWN) {
250 int keyCode = event.getKeyCode();
251 if (keyCode == KeyEvent.KEYCODE_BACK && mKeyguardView.handleBackKey()) {
252 return true;
253 } else if (keyCode == KeyEvent.KEYCODE_MENU && mKeyguardView.handleMenuKey()) {
254 return true;
255 }
256 }
257 // Always process media keys, regardless of focus
258 if (mKeyguardView.dispatchKeyEvent(event)) {
Jim Miller0a34d9e2012-10-05 17:15:41 -0700259 return true;
260 }
261 }
262 return super.dispatchKeyEvent(event);
263 }
Jim Millerdcb3d842012-08-23 19:18:12 -0700264 }
265
Jim Miller6bcd7322012-10-02 16:32:04 -0700266 SparseArray<Parcelable> mStateContainer = new SparseArray<Parcelable>();
Jim Millerba7d94b2014-02-05 17:30:50 -0800267 private int mCurrentLayout;
Jim Miller6bcd7322012-10-02 16:32:04 -0700268
Jim Millera71984f2012-10-24 22:08:49 -0700269 private void maybeCreateKeyguardLocked(boolean enableScreenRotation, boolean force,
270 Bundle options) {
Jim Miller6bcd7322012-10-02 16:32:04 -0700271 if (mKeyguardHost != null) {
272 mKeyguardHost.saveHierarchyState(mStateContainer);
273 }
274
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800275 if (mKeyguardHost == null) {
276 if (DEBUG) Log.d(TAG, "keyguard host is null, creating it...");
277
John Spurlock256ae672013-09-26 12:14:33 -0400278 mKeyguardHost = new ViewManagerHost(mContext);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800279
Adam Powell5da64302012-11-05 14:16:59 -0800280 int flags = WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
281 | WindowManager.LayoutParams.FLAG_LAYOUT_INSET_DECOR
Adam Powella9fa92e2012-11-08 00:03:34 -0800282 | WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN
Jim Millerd2b82f72012-09-18 20:52:55 -0700283 | WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
Jim Millerdcb3d842012-08-23 19:18:12 -0700284
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800285 if (!mNeedsInput) {
286 flags |= WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM;
287 }
Jim Millerdcb3d842012-08-23 19:18:12 -0700288
289 final int stretch = ViewGroup.LayoutParams.MATCH_PARENT;
Jim Miller5ecd8112013-01-09 18:50:26 -0800290 final int type = WindowManager.LayoutParams.TYPE_KEYGUARD;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800291 WindowManager.LayoutParams lp = new WindowManager.LayoutParams(
Jim Millerdcb3d842012-08-23 19:18:12 -0700292 stretch, stretch, type, flags, PixelFormat.TRANSLUCENT);
Jim Miller6b05d582011-07-18 13:09:59 -0700293 lp.softInputMode = WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE;
Jim Miller5ecd8112013-01-09 18:50:26 -0800294 lp.windowAnimations = R.style.Animation_LockScreen;
Jim Miller8e26cd82013-03-12 18:45:28 -0700295 lp.screenOrientation = enableScreenRotation ?
296 ActivityInfo.SCREEN_ORIENTATION_USER : ActivityInfo.SCREEN_ORIENTATION_NOSENSOR;
297
Jeff Brown98365d72012-08-19 20:30:52 -0700298 if (ActivityManager.isHighEndGfx()) {
Dianne Hackborn5d927c22011-09-02 12:22:18 -0700299 lp.flags |= WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED;
300 lp.privateFlags |=
301 WindowManager.LayoutParams.PRIVATE_FLAG_FORCE_HARDWARE_ACCELERATED;
302 }
Dianne Hackborn73ab6a42011-12-13 11:16:23 -0800303 lp.privateFlags |= WindowManager.LayoutParams.PRIVATE_FLAG_SET_NEEDS_MENU_KEY;
Jeff Brown3dc524b2012-09-30 19:49:11 -0700304 lp.inputFeatures |= WindowManager.LayoutParams.INPUT_FEATURE_DISABLE_USER_ACTIVITY;
Jim Millercf182aa2013-06-28 16:55:56 -0700305 lp.setTitle("Keyguard");
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800306 mWindowLayoutParams = lp;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800307 mViewManager.addView(mKeyguardHost, lp);
Adam Powell43a372f2013-09-27 17:43:53 -0700308
309 KeyguardUpdateMonitor.getInstance(mContext).registerCallback(mBackgroundChanger);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800310 }
Jeff Brownc7505bc2012-10-05 21:58:15 -0700311
Jim Millera71984f2012-10-24 22:08:49 -0700312 if (force || mKeyguardView == null) {
Adam Powell43a372f2013-09-27 17:43:53 -0700313 mKeyguardHost.setCustomBackground(null);
Jim Miller1de95fb2013-09-23 15:43:07 -0700314 mKeyguardHost.removeAllViews();
Jim Millerba7d94b2014-02-05 17:30:50 -0800315 int layout = allowNotificationsOnSecureKeyguard()
316 ? R.layout.keyguard_simple_host_view
317 : R.layout.keyguard_host_view;
318 if (mCurrentLayout != layout) {
319 mStateContainer.clear(); // don't restore to the wrong view hierarchy
320 mCurrentLayout = layout;
321 }
322 mKeyguardView = inflateKeyguardView(options, layout);
Jim Miller147f9562012-12-03 17:55:16 -0800323 mKeyguardView.requestFocus();
Jim Millera71984f2012-10-24 22:08:49 -0700324 }
Jeff Brownc7505bc2012-10-05 21:58:15 -0700325 updateUserActivityTimeoutInWindowLayoutParams();
Jim Millerdcb3d842012-08-23 19:18:12 -0700326 mViewManager.updateViewLayout(mKeyguardHost, mWindowLayoutParams);
Jim Miller6bcd7322012-10-02 16:32:04 -0700327
328 mKeyguardHost.restoreHierarchyState(mStateContainer);
Jim Millerdcb3d842012-08-23 19:18:12 -0700329 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800330
Jim Millerba7d94b2014-02-05 17:30:50 -0800331 private boolean allowNotificationsOnSecureKeyguard() {
332 ContentResolver cr = mContext.getContentResolver();
333 return Settings.Secure.getInt(cr, Settings.Secure.LOCK_SCREEN_ALLOW_NOTIFICATIONS, 0) == 1;
334 }
335
336 private KeyguardViewBase inflateKeyguardView(Bundle options, int layoutId) {
Craig Mautner00baebe2012-09-28 15:22:39 -0700337 View v = mKeyguardHost.findViewById(R.id.keyguard_host_view);
338 if (v != null) {
339 mKeyguardHost.removeView(v);
Jim Millerdcb3d842012-08-23 19:18:12 -0700340 }
341 final LayoutInflater inflater = LayoutInflater.from(mContext);
Jim Millerba7d94b2014-02-05 17:30:50 -0800342 View view = inflater.inflate(layoutId, mKeyguardHost, true);
343 KeyguardViewBase keyguard = (KeyguardViewBase) view.findViewById(R.id.keyguard_host_view);
344 keyguard.setLockPatternUtils(mLockPatternUtils);
345 keyguard.setViewMediatorCallback(mViewMediatorCallback);
346 keyguard.onUserSwitching(options != null && options.getBoolean(IS_SWITCHING_USER));
347 keyguard.onCreateOptions(options);
348 return keyguard;
Jim Millerdcb3d842012-08-23 19:18:12 -0700349 }
350
Jeff Brownc7505bc2012-10-05 21:58:15 -0700351 public void updateUserActivityTimeout() {
352 updateUserActivityTimeoutInWindowLayoutParams();
353 mViewManager.updateViewLayout(mKeyguardHost, mWindowLayoutParams);
354 }
355
356 private void updateUserActivityTimeoutInWindowLayoutParams() {
357 // Use the user activity timeout requested by the keyguard view, if any.
358 if (mKeyguardView != null) {
359 long timeout = mKeyguardView.getUserActivityTimeout();
360 if (timeout >= 0) {
361 mWindowLayoutParams.userActivityTimeout = timeout;
362 return;
363 }
364 }
365
366 // Otherwise, use the default timeout.
367 mWindowLayoutParams.userActivityTimeout = KeyguardViewMediator.AWAKE_INTERVAL_DEFAULT_MS;
368 }
369
Jim Millerdcb3d842012-08-23 19:18:12 -0700370 private void maybeEnableScreenRotation(boolean enableScreenRotation) {
371 // TODO: move this outside
Jim Miller06cc78a2011-06-02 16:10:16 -0700372 if (enableScreenRotation) {
Jim Millerf3447352011-08-07 14:00:09 -0700373 if (DEBUG) Log.d(TAG, "Rotation sensor for lock screen On!");
Jim Miller82f91962012-07-24 13:33:08 -0700374 mWindowLayoutParams.screenOrientation = ActivityInfo.SCREEN_ORIENTATION_USER;
Jim Miller06cc78a2011-06-02 16:10:16 -0700375 } else {
Jim Millerf3447352011-08-07 14:00:09 -0700376 if (DEBUG) Log.d(TAG, "Rotation sensor for lock screen Off!");
Jim Miller06cc78a2011-06-02 16:10:16 -0700377 mWindowLayoutParams.screenOrientation = ActivityInfo.SCREEN_ORIENTATION_NOSENSOR;
378 }
Jim Miller06cc78a2011-06-02 16:10:16 -0700379 mViewManager.updateViewLayout(mKeyguardHost, mWindowLayoutParams);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800380 }
381
Adam Powell94719072013-10-10 20:01:49 -0700382 void updateShowWallpaper(boolean show) {
383 if (show) {
384 mWindowLayoutParams.flags |= WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
385 } else {
386 mWindowLayoutParams.flags &= ~WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
387 }
388
389 mViewManager.updateViewLayout(mKeyguardHost, mWindowLayoutParams);
390 }
391
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800392 public void setNeedsInput(boolean needsInput) {
393 mNeedsInput = needsInput;
394 if (mWindowLayoutParams != null) {
395 if (needsInput) {
396 mWindowLayoutParams.flags &=
397 ~WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM;
398 } else {
399 mWindowLayoutParams.flags |=
400 WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM;
401 }
Jim Millerdcb3d842012-08-23 19:18:12 -0700402
403 try {
404 mViewManager.updateViewLayout(mKeyguardHost, mWindowLayoutParams);
405 } catch (java.lang.IllegalArgumentException e) {
406 // TODO: Ensure this method isn't called on views that are changing...
407 Log.w(TAG,"Can't update input method on " + mKeyguardHost + " window not attached");
408 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800409 }
410 }
Jim Miller22dfe722009-10-07 01:30:21 -0700411
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800412 /**
413 * Reset the state of the view.
414 */
Adam Cohenf7522022012-10-03 20:03:18 -0700415 public synchronized void reset(Bundle options) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800416 if (DEBUG) Log.d(TAG, "reset()");
Amith Yamasanid979dd72012-09-11 15:53:37 -0700417 // User might have switched, check if we need to go back to keyguard
418 // TODO: It's preferable to stay and show the correct lockscreen or unlock if none
Jim Millera71984f2012-10-24 22:08:49 -0700419 maybeCreateKeyguardLocked(shouldEnableScreenRotation(), true, options);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800420 }
421
422 public synchronized void onScreenTurnedOff() {
423 if (DEBUG) Log.d(TAG, "onScreenTurnedOff()");
Craig Mautnerad09bcc2012-10-08 13:33:11 -0700424 mScreenOn = false;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800425 if (mKeyguardView != null) {
426 mKeyguardView.onScreenTurnedOff();
Brian Colonna15ea55a2011-09-09 11:48:16 -0400427 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800428 }
429
Jim Miller25190572013-02-28 17:36:24 -0800430 public synchronized void onScreenTurnedOn(final IKeyguardShowCallback callback) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800431 if (DEBUG) Log.d(TAG, "onScreenTurnedOn()");
Craig Mautnerad09bcc2012-10-08 13:33:11 -0700432 mScreenOn = true;
Jim Miller6a3cceb2013-10-11 21:03:22 -0700433
Jim Millered80f3f2013-10-15 17:48:15 -0700434 // If keyguard is not showing, we need to inform PhoneWindowManager with a null
Jim Miller6a3cceb2013-10-11 21:03:22 -0700435 // token so it doesn't wait for us to draw...
Jim Millered80f3f2013-10-15 17:48:15 -0700436 final IBinder token = isShowing() ? mKeyguardHost.getWindowToken() : null;
437
438 if (DEBUG && token == null) Slog.v(TAG, "send wm null token: "
439 + (mKeyguardHost == null ? "host was null" : "not showing"));
Jim Miller6a3cceb2013-10-11 21:03:22 -0700440
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800441 if (mKeyguardView != null) {
442 mKeyguardView.onScreenTurnedOn();
Jim Miller6edf2632011-09-05 16:03:14 -0700443
Dianne Hackborn38e29a62011-09-18 14:43:08 -0700444 // Caller should wait for this window to be shown before turning
445 // on the screen.
Jim Miller25190572013-02-28 17:36:24 -0800446 if (callback != null) {
Craig Mautner904732c2012-10-17 15:20:24 -0700447 if (mKeyguardHost.getVisibility() == View.VISIBLE) {
448 // Keyguard may be in the process of being shown, but not yet
449 // updated with the window manager... give it a chance to do so.
450 mKeyguardHost.post(new Runnable() {
451 @Override
452 public void run() {
Jim Miller5ecd8112013-01-09 18:50:26 -0800453 try {
Jim Miller25190572013-02-28 17:36:24 -0800454 callback.onShown(token);
Jim Miller5ecd8112013-01-09 18:50:26 -0800455 } catch (RemoteException e) {
Jim Miller25190572013-02-28 17:36:24 -0800456 Slog.w(TAG, "Exception calling onShown():", e);
Craig Mautner904732c2012-10-17 15:20:24 -0700457 }
Dianne Hackborn38e29a62011-09-18 14:43:08 -0700458 }
Craig Mautner904732c2012-10-17 15:20:24 -0700459 });
460 } else {
Jim Miller5ecd8112013-01-09 18:50:26 -0800461 try {
Craig Mautner4e8a19c2013-10-08 17:26:08 -0700462 callback.onShown(token);
Jim Miller5ecd8112013-01-09 18:50:26 -0800463 } catch (RemoteException e) {
Jim Miller25190572013-02-28 17:36:24 -0800464 Slog.w(TAG, "Exception calling onShown():", e);
Jim Miller5ecd8112013-01-09 18:50:26 -0800465 }
Craig Mautner904732c2012-10-17 15:20:24 -0700466 }
Dianne Hackborn38e29a62011-09-18 14:43:08 -0700467 }
Jim Miller25190572013-02-28 17:36:24 -0800468 } else if (callback != null) {
Jim Miller5ecd8112013-01-09 18:50:26 -0800469 try {
Craig Mautner4e8a19c2013-10-08 17:26:08 -0700470 callback.onShown(token);
Jim Miller5ecd8112013-01-09 18:50:26 -0800471 } catch (RemoteException e) {
Jim Miller25190572013-02-28 17:36:24 -0800472 Slog.w(TAG, "Exception calling onShown():", e);
Jim Miller5ecd8112013-01-09 18:50:26 -0800473 }
Brian Colonna15ea55a2011-09-09 11:48:16 -0400474 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800475 }
476
477 public synchronized void verifyUnlock() {
478 if (DEBUG) Log.d(TAG, "verifyUnlock()");
Adam Cohenf7522022012-10-03 20:03:18 -0700479 show(null);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800480 mKeyguardView.verifyUnlock();
481 }
482
483 /**
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800484 * Hides the keyguard view
485 */
486 public synchronized void hide() {
487 if (DEBUG) Log.d(TAG, "hide()");
Jim Miller6edf2632011-09-05 16:03:14 -0700488
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800489 if (mKeyguardHost != null) {
490 mKeyguardHost.setVisibility(View.GONE);
Jim Miller223ce5c2012-10-05 19:13:23 -0700491
492 // We really only want to preserve keyguard state for configuration changes. Hence
493 // we should clear state of widgets (e.g. Music) when we hide keyguard so it can
494 // start with a fresh state when we return.
495 mStateContainer.clear();
496
Karl Rosaena35d7532009-09-25 10:24:26 -0700497 // Don't do this right away, so we can let the view continue to animate
Dianne Hackborn01ad2f42009-09-24 19:24:56 -0700498 // as it goes away.
Karl Rosaena35d7532009-09-25 10:24:26 -0700499 if (mKeyguardView != null) {
Jim Miller22dfe722009-10-07 01:30:21 -0700500 final KeyguardViewBase lastView = mKeyguardView;
501 mKeyguardView = null;
Karl Rosaena35d7532009-09-25 10:24:26 -0700502 mKeyguardHost.postDelayed(new Runnable() {
Craig Mautner904732c2012-10-17 15:20:24 -0700503 @Override
Karl Rosaena35d7532009-09-25 10:24:26 -0700504 public void run() {
Dianne Hackbornd7f7deb2009-09-25 17:35:10 -0700505 synchronized (KeyguardViewManager.this) {
Jim Miller22dfe722009-10-07 01:30:21 -0700506 lastView.cleanUp();
Adam Powell43a372f2013-09-27 17:43:53 -0700507 // Let go of any large bitmaps.
508 mKeyguardHost.setCustomBackground(null);
Adam Powell94719072013-10-10 20:01:49 -0700509 updateShowWallpaper(true);
Jim Miller8b886fa2011-01-13 17:56:35 -0800510 mKeyguardHost.removeView(lastView);
Jim Miller0b9d8ac2013-11-07 19:15:49 -0800511 mViewMediatorCallback.keyguardGone();
Dianne Hackbornd7f7deb2009-09-25 17:35:10 -0700512 }
Karl Rosaena35d7532009-09-25 10:24:26 -0700513 }
Jim Miller0b9d8ac2013-11-07 19:15:49 -0800514 }, HIDE_KEYGUARD_DELAY);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800515 }
516 }
517 }
518
519 /**
Craig Mautnerad09bcc2012-10-08 13:33:11 -0700520 * Dismisses the keyguard by going to the next screen or making it gone.
521 */
522 public synchronized void dismiss() {
523 if (mScreenOn) {
524 mKeyguardView.dismiss();
525 }
526 }
527
528 /**
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800529 * @return Whether the keyguard is showing
530 */
531 public synchronized boolean isShowing() {
532 return (mKeyguardHost != null && mKeyguardHost.getVisibility() == View.VISIBLE);
533 }
Jim Miller4eeb4f62012-11-08 00:04:29 -0800534
535 public void showAssistant() {
536 if (mKeyguardView != null) {
537 mKeyguardView.showAssistant();
538 }
539 }
Jim Millercaf24fc2013-09-10 18:37:01 -0700540
541 public void dispatch(MotionEvent event) {
542 if (mKeyguardView != null) {
Jim Miller5e612cf2014-02-03 17:57:23 -0800543 mKeyguardView.onExternalMotionEvent(event);
Jim Millercaf24fc2013-09-10 18:37:01 -0700544 }
545 }
Jim Miller138f25d2013-09-25 13:46:58 -0700546
547 public void launchCamera() {
548 if (mKeyguardView != null) {
549 mKeyguardView.launchCamera();
550 }
551 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800552}