blob: 6aa0a4bfd1c7870830559ef8f48545255ec86721 [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.app.PendingIntent;
20import android.graphics.Bitmap;
21import android.graphics.drawable.BitmapDrawable;
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 Millerdcb3d842012-08-23 19:18:12 -070025import android.app.Activity;
Dianne Hackbornb3756322011-08-12 13:58:13 -070026import android.app.ActivityManager;
Michael Jurka76017ca2012-11-06 16:21:09 -080027import android.appwidget.AppWidgetManager;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -080028import android.content.Context;
29import android.content.pm.ActivityInfo;
Jim Millerdcb3d842012-08-23 19:18:12 -070030import android.content.res.Configuration;
Jim Miller06cc78a2011-06-02 16:10:16 -070031import android.content.res.Resources;
John Spurlock257f2832013-09-21 18:41:53 -040032import android.graphics.Canvas;
33import android.graphics.ColorFilter;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -080034import android.graphics.PixelFormat;
John Spurlock257f2832013-09-21 18:41:53 -040035import android.graphics.PorterDuff;
Jim Miller2b84b812012-11-30 15:42:18 -080036import android.graphics.Rect;
John Spurlock257f2832013-09-21 18:41:53 -040037import android.graphics.drawable.Drawable;
Adam Cohenf7522022012-10-03 20:03:18 -070038import android.os.Bundle;
Dianne Hackborn38e29a62011-09-18 14:43:08 -070039import android.os.IBinder;
Jim Miller6bcd7322012-10-02 16:32:04 -070040import android.os.Parcelable;
Jim Miller5ecd8112013-01-09 18:50:26 -080041import android.os.RemoteException;
Jim Miller56bacd72011-09-09 14:12:54 -070042import android.os.SystemProperties;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -080043import android.util.Log;
Craig Mautner0cbfcff2012-09-17 16:40:28 -070044import android.util.Slog;
Jim Miller6bcd7322012-10-02 16:32:04 -070045import android.util.SparseArray;
Adam Cohenf7522022012-10-03 20:03:18 -070046import android.view.KeyEvent;
Jim Millerdcb3d842012-08-23 19:18:12 -070047import android.view.LayoutInflater;
Jim Millercaf24fc2013-09-10 18:37:01 -070048import android.view.MotionEvent;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -080049import android.view.View;
50import android.view.ViewGroup;
51import android.view.ViewManager;
52import android.view.WindowManager;
53import android.widget.FrameLayout;
54
55/**
56 * Manages creating, showing, hiding and resetting the keyguard. Calls back
Craig Mautner904732c2012-10-17 15:20:24 -070057 * via {@link KeyguardViewMediator.ViewMediatorCallback} to poke
The Android Open Source Project1f838aa2009-03-03 19:32:13 -080058 * the wake lock and report that the keyguard is done, which is in turn,
59 * reported to this class by the current {@link KeyguardViewBase}.
60 */
Jim Millerdcb3d842012-08-23 19:18:12 -070061public class KeyguardViewManager {
Jim Millera71984f2012-10-24 22:08:49 -070062 private final static boolean DEBUG = KeyguardViewMediator.DEBUG;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -080063 private static String TAG = "KeyguardViewManager";
Chris Wrenf41c61b2012-11-29 15:19:54 -050064 public final static String IS_SWITCHING_USER = "is_switching_user";
The Android Open Source Project1f838aa2009-03-03 19:32:13 -080065
Jim Miller0b9d8ac2013-11-07 19:15:49 -080066 // Delay dismissing keyguard to allow animations to complete.
67 private static final int HIDE_KEYGUARD_DELAY = 500;
68
Jim Miller9cf2c522012-10-04 22:02:29 -070069 // Timeout used for keypresses
70 static final int DIGIT_PRESS_WAKE_MILLIS = 5000;
71
The Android Open Source Project1f838aa2009-03-03 19:32:13 -080072 private final Context mContext;
73 private final ViewManager mViewManager;
Jim Millerdcb3d842012-08-23 19:18:12 -070074 private final KeyguardViewMediator.ViewMediatorCallback mViewMediatorCallback;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -080075
76 private WindowManager.LayoutParams mWindowLayoutParams;
77 private boolean mNeedsInput = false;
Jim Miller22dfe722009-10-07 01:30:21 -070078
John Spurlock257f2832013-09-21 18:41:53 -040079 private ViewManagerHost mKeyguardHost;
Jim Millerdcb3d842012-08-23 19:18:12 -070080 private KeyguardHostView mKeyguardView;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -080081
Craig Mautnerad09bcc2012-10-08 13:33:11 -070082 private boolean mScreenOn = false;
Jim Millerdcb3d842012-08-23 19:18:12 -070083 private LockPatternUtils mLockPatternUtils;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -080084
Adam Powell43a372f2013-09-27 17:43:53 -070085 private KeyguardUpdateMonitorCallback mBackgroundChanger = new KeyguardUpdateMonitorCallback() {
86 @Override
87 public void onSetBackground(Bitmap bmp) {
88 mKeyguardHost.setCustomBackground(bmp != null ?
89 new BitmapDrawable(mContext.getResources(), bmp) : null);
Adam Powell94719072013-10-10 20:01:49 -070090 updateShowWallpaper(bmp == null);
Adam Powell43a372f2013-09-27 17:43:53 -070091 }
92 };
93
Dianne Hackborn38e29a62011-09-18 14:43:08 -070094 public interface ShowListener {
95 void onShown(IBinder windowToken);
96 };
97
The Android Open Source Project1f838aa2009-03-03 19:32:13 -080098 /**
99 * @param context Used to create views.
100 * @param viewManager Keyguard will be attached to this.
101 * @param callback Used to notify of changes.
Jim Millerdcb3d842012-08-23 19:18:12 -0700102 * @param lockPatternUtils
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800103 */
104 public KeyguardViewManager(Context context, ViewManager viewManager,
Jim Millerdcb3d842012-08-23 19:18:12 -0700105 KeyguardViewMediator.ViewMediatorCallback callback,
106 LockPatternUtils lockPatternUtils) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800107 mContext = context;
108 mViewManager = viewManager;
Jim Millerdcb3d842012-08-23 19:18:12 -0700109 mViewMediatorCallback = callback;
110 mLockPatternUtils = lockPatternUtils;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800111 }
112
113 /**
114 * Show the keyguard. Will handle creating and attaching to the view manager
115 * lazily.
116 */
Adam Cohenf7522022012-10-03 20:03:18 -0700117 public synchronized void show(Bundle options) {
Daniel Sandler16541e42009-11-13 17:07:50 -0800118 if (DEBUG) Log.d(TAG, "show(); mKeyguardView==" + mKeyguardView);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800119
Jim Millerdcb3d842012-08-23 19:18:12 -0700120 boolean enableScreenRotation = shouldEnableScreenRotation();
121
Jim Millera71984f2012-10-24 22:08:49 -0700122 maybeCreateKeyguardLocked(enableScreenRotation, false, options);
Jim Millerdcb3d842012-08-23 19:18:12 -0700123 maybeEnableScreenRotation(enableScreenRotation);
124
Jim Millere23ab8b2012-09-16 15:45:44 -0700125 // Disable common aspects of the system/status/navigation bars that are not appropriate or
126 // useful on any keyguard screen but can be re-shown by dialogs or SHOW_WHEN_LOCKED
127 // activities. Other disabled bits are handled by the KeyguardViewMediator talking
128 // directly to the status bar service.
John Spurlock257f2832013-09-21 18:41:53 -0400129 int visFlags = View.STATUS_BAR_DISABLE_HOME;
John Spurlockbd957402013-10-03 11:38:39 -0400130 if (shouldEnableTranslucentDecor()) {
131 mWindowLayoutParams.flags |= WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
132 | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION;
John Spurlock257f2832013-09-21 18:41:53 -0400133 }
Jim Millera71984f2012-10-24 22:08:49 -0700134 if (DEBUG) Log.v(TAG, "show:setSystemUiVisibility(" + Integer.toHexString(visFlags)+")");
Jim Millerdcb3d842012-08-23 19:18:12 -0700135 mKeyguardHost.setSystemUiVisibility(visFlags);
136
137 mViewManager.updateViewLayout(mKeyguardHost, mWindowLayoutParams);
138 mKeyguardHost.setVisibility(View.VISIBLE);
139 mKeyguardView.show();
140 mKeyguardView.requestFocus();
141 }
142
143 private boolean shouldEnableScreenRotation() {
Jim Miller06cc78a2011-06-02 16:10:16 -0700144 Resources res = mContext.getResources();
Jim Millerdcb3d842012-08-23 19:18:12 -0700145 return SystemProperties.getBoolean("lockscreen.rot_override",false)
Jim Miller5ecd8112013-01-09 18:50:26 -0800146 || res.getBoolean(R.bool.config_enableLockScreenRotation);
Jim Millerdcb3d842012-08-23 19:18:12 -0700147 }
148
John Spurlockbd957402013-10-03 11:38:39 -0400149 private boolean shouldEnableTranslucentDecor() {
John Spurlock257f2832013-09-21 18:41:53 -0400150 Resources res = mContext.getResources();
John Spurlockbd957402013-10-03 11:38:39 -0400151 return res.getBoolean(R.bool.config_enableLockScreenTranslucentDecor);
John Spurlock257f2832013-09-21 18:41:53 -0400152 }
153
Jim Millerdcb3d842012-08-23 19:18:12 -0700154 class ViewManagerHost extends FrameLayout {
John Spurlock257f2832013-09-21 18:41:53 -0400155 private static final int BACKGROUND_COLOR = 0x70000000;
John Spurlock256ae672013-09-26 12:14:33 -0400156
Adam Powell43a372f2013-09-27 17:43:53 -0700157 private Drawable mCustomBackground;
158
John Spurlock257f2832013-09-21 18:41:53 -0400159 // This is a faster way to draw the background on devices without hardware acceleration
160 private final Drawable mBackgroundDrawable = new Drawable() {
161 @Override
162 public void draw(Canvas canvas) {
Adam Powell43a372f2013-09-27 17:43:53 -0700163 if (mCustomBackground != null) {
164 final Rect bounds = mCustomBackground.getBounds();
165 final int vWidth = getWidth();
166 final int vHeight = getHeight();
167
168 final int restore = canvas.save();
169 canvas.translate(-(bounds.width() - vWidth) / 2,
170 -(bounds.height() - vHeight) / 2);
171 mCustomBackground.draw(canvas);
172 canvas.restoreToCount(restore);
173 } else {
174 canvas.drawColor(BACKGROUND_COLOR, PorterDuff.Mode.SRC);
175 }
John Spurlock257f2832013-09-21 18:41:53 -0400176 }
177
178 @Override
179 public void setAlpha(int alpha) {
180 }
181
182 @Override
183 public void setColorFilter(ColorFilter cf) {
184 }
185
186 @Override
187 public int getOpacity() {
188 return PixelFormat.TRANSLUCENT;
189 }
190 };
John Spurlock256ae672013-09-26 12:14:33 -0400191
192 public ViewManagerHost(Context context) {
Jim Millerdcb3d842012-08-23 19:18:12 -0700193 super(context);
John Spurlock257f2832013-09-21 18:41:53 -0400194 setBackground(mBackgroundDrawable);
Jim Miller2b84b812012-11-30 15:42:18 -0800195 }
196
Adam Powell43a372f2013-09-27 17:43:53 -0700197 public void setCustomBackground(Drawable d) {
198 mCustomBackground = d;
199 if (d != null) {
200 d.setColorFilter(BACKGROUND_COLOR, PorterDuff.Mode.SRC_OVER);
201 }
202 computeCustomBackgroundBounds();
203 invalidate();
204 }
205
206 private void computeCustomBackgroundBounds() {
207 if (mCustomBackground == null) return; // Nothing to do
208 if (!isLaidOut()) return; // We'll do this later
209
210 final int bgWidth = mCustomBackground.getIntrinsicWidth();
211 final int bgHeight = mCustomBackground.getIntrinsicHeight();
212 final int vWidth = getWidth();
213 final int vHeight = getHeight();
214
215 final float bgAspect = (float) bgWidth / bgHeight;
216 final float vAspect = (float) vWidth / vHeight;
217
218 if (bgAspect > vAspect) {
219 mCustomBackground.setBounds(0, 0, (int) (vHeight * bgAspect), vHeight);
220 } else {
221 mCustomBackground.setBounds(0, 0, vWidth, (int) (vWidth / bgAspect));
222 }
223 }
224
225 @Override
226 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
227 super.onSizeChanged(w, h, oldw, oldh);
228 computeCustomBackgroundBounds();
229 }
230
Jim Miller2b84b812012-11-30 15:42:18 -0800231 @Override
Jim Millerdcb3d842012-08-23 19:18:12 -0700232 protected void onConfigurationChanged(Configuration newConfig) {
233 super.onConfigurationChanged(newConfig);
Jim Millera71984f2012-10-24 22:08:49 -0700234 if (mKeyguardHost.getVisibility() == View.VISIBLE) {
235 // only propagate configuration messages if we're currently showing
236 maybeCreateKeyguardLocked(shouldEnableScreenRotation(), true, null);
237 } else {
238 if (DEBUG) Log.v(TAG, "onConfigurationChanged: view not visible");
239 }
Jim Millerdcb3d842012-08-23 19:18:12 -0700240 }
Jim Miller0a34d9e2012-10-05 17:15:41 -0700241
242 @Override
243 public boolean dispatchKeyEvent(KeyEvent event) {
Jim Miller42df15e2012-12-05 15:15:00 -0800244 if (mKeyguardView != null) {
245 // Always process back and menu keys, regardless of focus
246 if (event.getAction() == KeyEvent.ACTION_DOWN) {
247 int keyCode = event.getKeyCode();
248 if (keyCode == KeyEvent.KEYCODE_BACK && mKeyguardView.handleBackKey()) {
249 return true;
250 } else if (keyCode == KeyEvent.KEYCODE_MENU && mKeyguardView.handleMenuKey()) {
251 return true;
252 }
253 }
254 // Always process media keys, regardless of focus
255 if (mKeyguardView.dispatchKeyEvent(event)) {
Jim Miller0a34d9e2012-10-05 17:15:41 -0700256 return true;
257 }
258 }
259 return super.dispatchKeyEvent(event);
260 }
Jim Millerdcb3d842012-08-23 19:18:12 -0700261 }
262
Jim Miller6bcd7322012-10-02 16:32:04 -0700263 SparseArray<Parcelable> mStateContainer = new SparseArray<Parcelable>();
264
Jim Millera71984f2012-10-24 22:08:49 -0700265 private void maybeCreateKeyguardLocked(boolean enableScreenRotation, boolean force,
266 Bundle options) {
Jim Miller6bcd7322012-10-02 16:32:04 -0700267 if (mKeyguardHost != null) {
268 mKeyguardHost.saveHierarchyState(mStateContainer);
269 }
270
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800271 if (mKeyguardHost == null) {
272 if (DEBUG) Log.d(TAG, "keyguard host is null, creating it...");
273
John Spurlock256ae672013-09-26 12:14:33 -0400274 mKeyguardHost = new ViewManagerHost(mContext);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800275
Adam Powell5da64302012-11-05 14:16:59 -0800276 int flags = WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
277 | WindowManager.LayoutParams.FLAG_LAYOUT_INSET_DECOR
Adam Powella9fa92e2012-11-08 00:03:34 -0800278 | WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN
Jim Millerd2b82f72012-09-18 20:52:55 -0700279 | WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
Jim Millerdcb3d842012-08-23 19:18:12 -0700280
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800281 if (!mNeedsInput) {
282 flags |= WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM;
283 }
Jim Millerdcb3d842012-08-23 19:18:12 -0700284
285 final int stretch = ViewGroup.LayoutParams.MATCH_PARENT;
Jim Miller5ecd8112013-01-09 18:50:26 -0800286 final int type = WindowManager.LayoutParams.TYPE_KEYGUARD;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800287 WindowManager.LayoutParams lp = new WindowManager.LayoutParams(
Jim Millerdcb3d842012-08-23 19:18:12 -0700288 stretch, stretch, type, flags, PixelFormat.TRANSLUCENT);
Jim Miller6b05d582011-07-18 13:09:59 -0700289 lp.softInputMode = WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE;
Jim Miller5ecd8112013-01-09 18:50:26 -0800290 lp.windowAnimations = R.style.Animation_LockScreen;
Jim Miller8e26cd82013-03-12 18:45:28 -0700291 lp.screenOrientation = enableScreenRotation ?
292 ActivityInfo.SCREEN_ORIENTATION_USER : ActivityInfo.SCREEN_ORIENTATION_NOSENSOR;
293
Jeff Brown98365d72012-08-19 20:30:52 -0700294 if (ActivityManager.isHighEndGfx()) {
Dianne Hackborn5d927c22011-09-02 12:22:18 -0700295 lp.flags |= WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED;
296 lp.privateFlags |=
297 WindowManager.LayoutParams.PRIVATE_FLAG_FORCE_HARDWARE_ACCELERATED;
298 }
Dianne Hackborn73ab6a42011-12-13 11:16:23 -0800299 lp.privateFlags |= WindowManager.LayoutParams.PRIVATE_FLAG_SET_NEEDS_MENU_KEY;
Jeff Brown3dc524b2012-09-30 19:49:11 -0700300 lp.inputFeatures |= WindowManager.LayoutParams.INPUT_FEATURE_DISABLE_USER_ACTIVITY;
Jim Millercf182aa2013-06-28 16:55:56 -0700301 lp.setTitle("Keyguard");
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800302 mWindowLayoutParams = lp;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800303 mViewManager.addView(mKeyguardHost, lp);
Adam Powell43a372f2013-09-27 17:43:53 -0700304
305 KeyguardUpdateMonitor.getInstance(mContext).registerCallback(mBackgroundChanger);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800306 }
Jeff Brownc7505bc2012-10-05 21:58:15 -0700307
Jim Millera71984f2012-10-24 22:08:49 -0700308 if (force || mKeyguardView == null) {
Adam Powell43a372f2013-09-27 17:43:53 -0700309 mKeyguardHost.setCustomBackground(null);
Jim Miller1de95fb2013-09-23 15:43:07 -0700310 mKeyguardHost.removeAllViews();
Jim Millera71984f2012-10-24 22:08:49 -0700311 inflateKeyguardView(options);
Jim Miller147f9562012-12-03 17:55:16 -0800312 mKeyguardView.requestFocus();
Jim Millera71984f2012-10-24 22:08:49 -0700313 }
Jeff Brownc7505bc2012-10-05 21:58:15 -0700314 updateUserActivityTimeoutInWindowLayoutParams();
Jim Millerdcb3d842012-08-23 19:18:12 -0700315 mViewManager.updateViewLayout(mKeyguardHost, mWindowLayoutParams);
Jim Miller6bcd7322012-10-02 16:32:04 -0700316
317 mKeyguardHost.restoreHierarchyState(mStateContainer);
Jim Millerdcb3d842012-08-23 19:18:12 -0700318 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800319
Adam Cohenf7522022012-10-03 20:03:18 -0700320 private void inflateKeyguardView(Bundle options) {
Craig Mautner00baebe2012-09-28 15:22:39 -0700321 View v = mKeyguardHost.findViewById(R.id.keyguard_host_view);
322 if (v != null) {
323 mKeyguardHost.removeView(v);
Jim Millerdcb3d842012-08-23 19:18:12 -0700324 }
325 final LayoutInflater inflater = LayoutInflater.from(mContext);
326 View view = inflater.inflate(R.layout.keyguard_host_view, mKeyguardHost, true);
327 mKeyguardView = (KeyguardHostView) view.findViewById(R.id.keyguard_host_view);
328 mKeyguardView.setLockPatternUtils(mLockPatternUtils);
329 mKeyguardView.setViewMediatorCallback(mViewMediatorCallback);
Chris Wrenf41c61b2012-11-29 15:19:54 -0500330 mKeyguardView.initializeSwitchingUserState(options != null &&
331 options.getBoolean(IS_SWITCHING_USER));
Jim Millerdcb3d842012-08-23 19:18:12 -0700332
Adam Powell70bc9f22012-10-12 22:02:27 -0700333 // HACK
334 // The keyguard view will have set up window flags in onFinishInflate before we set
335 // the view mediator callback. Make sure it knows the correct IME state.
336 if (mViewMediatorCallback != null) {
337 KeyguardPasswordView kpv = (KeyguardPasswordView) mKeyguardView.findViewById(
338 R.id.keyguard_password_view);
339
340 if (kpv != null) {
341 mViewMediatorCallback.setNeedsInput(kpv.needsInput());
342 }
343 }
344
Jim Miller0ff7f012012-10-11 20:40:01 -0700345 if (options != null) {
Michael Jurka76017ca2012-11-06 16:21:09 -0800346 int widgetToShow = options.getInt(LockPatternUtils.KEYGUARD_SHOW_APPWIDGET,
347 AppWidgetManager.INVALID_APPWIDGET_ID);
348 if (widgetToShow != AppWidgetManager.INVALID_APPWIDGET_ID) {
349 mKeyguardView.goToWidget(widgetToShow);
350 }
Adam Cohenf7522022012-10-03 20:03:18 -0700351 }
Jim Millerdcb3d842012-08-23 19:18:12 -0700352 }
353
Jeff Brownc7505bc2012-10-05 21:58:15 -0700354 public void updateUserActivityTimeout() {
355 updateUserActivityTimeoutInWindowLayoutParams();
356 mViewManager.updateViewLayout(mKeyguardHost, mWindowLayoutParams);
357 }
358
359 private void updateUserActivityTimeoutInWindowLayoutParams() {
360 // Use the user activity timeout requested by the keyguard view, if any.
361 if (mKeyguardView != null) {
362 long timeout = mKeyguardView.getUserActivityTimeout();
363 if (timeout >= 0) {
364 mWindowLayoutParams.userActivityTimeout = timeout;
365 return;
366 }
367 }
368
369 // Otherwise, use the default timeout.
370 mWindowLayoutParams.userActivityTimeout = KeyguardViewMediator.AWAKE_INTERVAL_DEFAULT_MS;
371 }
372
Jim Millerdcb3d842012-08-23 19:18:12 -0700373 private void maybeEnableScreenRotation(boolean enableScreenRotation) {
374 // TODO: move this outside
Jim Miller06cc78a2011-06-02 16:10:16 -0700375 if (enableScreenRotation) {
Jim Millerf3447352011-08-07 14:00:09 -0700376 if (DEBUG) Log.d(TAG, "Rotation sensor for lock screen On!");
Jim Miller82f91962012-07-24 13:33:08 -0700377 mWindowLayoutParams.screenOrientation = ActivityInfo.SCREEN_ORIENTATION_USER;
Jim Miller06cc78a2011-06-02 16:10:16 -0700378 } else {
Jim Millerf3447352011-08-07 14:00:09 -0700379 if (DEBUG) Log.d(TAG, "Rotation sensor for lock screen Off!");
Jim Miller06cc78a2011-06-02 16:10:16 -0700380 mWindowLayoutParams.screenOrientation = ActivityInfo.SCREEN_ORIENTATION_NOSENSOR;
381 }
Jim Miller06cc78a2011-06-02 16:10:16 -0700382 mViewManager.updateViewLayout(mKeyguardHost, mWindowLayoutParams);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800383 }
384
Adam Powell94719072013-10-10 20:01:49 -0700385 void updateShowWallpaper(boolean show) {
386 if (show) {
387 mWindowLayoutParams.flags |= WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
388 } else {
389 mWindowLayoutParams.flags &= ~WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
390 }
391
392 mViewManager.updateViewLayout(mKeyguardHost, mWindowLayoutParams);
393 }
394
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800395 public void setNeedsInput(boolean needsInput) {
396 mNeedsInput = needsInput;
397 if (mWindowLayoutParams != null) {
398 if (needsInput) {
399 mWindowLayoutParams.flags &=
400 ~WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM;
401 } else {
402 mWindowLayoutParams.flags |=
403 WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM;
404 }
Jim Millerdcb3d842012-08-23 19:18:12 -0700405
406 try {
407 mViewManager.updateViewLayout(mKeyguardHost, mWindowLayoutParams);
408 } catch (java.lang.IllegalArgumentException e) {
409 // TODO: Ensure this method isn't called on views that are changing...
410 Log.w(TAG,"Can't update input method on " + mKeyguardHost + " window not attached");
411 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800412 }
413 }
Jim Miller22dfe722009-10-07 01:30:21 -0700414
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800415 /**
416 * Reset the state of the view.
417 */
Adam Cohenf7522022012-10-03 20:03:18 -0700418 public synchronized void reset(Bundle options) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800419 if (DEBUG) Log.d(TAG, "reset()");
Amith Yamasanid979dd72012-09-11 15:53:37 -0700420 // User might have switched, check if we need to go back to keyguard
421 // TODO: It's preferable to stay and show the correct lockscreen or unlock if none
Jim Millera71984f2012-10-24 22:08:49 -0700422 maybeCreateKeyguardLocked(shouldEnableScreenRotation(), true, options);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800423 }
424
425 public synchronized void onScreenTurnedOff() {
426 if (DEBUG) Log.d(TAG, "onScreenTurnedOff()");
Craig Mautnerad09bcc2012-10-08 13:33:11 -0700427 mScreenOn = false;
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800428 if (mKeyguardView != null) {
429 mKeyguardView.onScreenTurnedOff();
Brian Colonna15ea55a2011-09-09 11:48:16 -0400430 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800431 }
432
Jim Miller25190572013-02-28 17:36:24 -0800433 public synchronized void onScreenTurnedOn(final IKeyguardShowCallback callback) {
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800434 if (DEBUG) Log.d(TAG, "onScreenTurnedOn()");
Craig Mautnerad09bcc2012-10-08 13:33:11 -0700435 mScreenOn = true;
Jim Miller6a3cceb2013-10-11 21:03:22 -0700436
Jim Millered80f3f2013-10-15 17:48:15 -0700437 // If keyguard is not showing, we need to inform PhoneWindowManager with a null
Jim Miller6a3cceb2013-10-11 21:03:22 -0700438 // token so it doesn't wait for us to draw...
Jim Millered80f3f2013-10-15 17:48:15 -0700439 final IBinder token = isShowing() ? mKeyguardHost.getWindowToken() : null;
440
441 if (DEBUG && token == null) Slog.v(TAG, "send wm null token: "
442 + (mKeyguardHost == null ? "host was null" : "not showing"));
Jim Miller6a3cceb2013-10-11 21:03:22 -0700443
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800444 if (mKeyguardView != null) {
445 mKeyguardView.onScreenTurnedOn();
Jim Miller6edf2632011-09-05 16:03:14 -0700446
Dianne Hackborn38e29a62011-09-18 14:43:08 -0700447 // Caller should wait for this window to be shown before turning
448 // on the screen.
Jim Miller25190572013-02-28 17:36:24 -0800449 if (callback != null) {
Craig Mautner904732c2012-10-17 15:20:24 -0700450 if (mKeyguardHost.getVisibility() == View.VISIBLE) {
451 // Keyguard may be in the process of being shown, but not yet
452 // updated with the window manager... give it a chance to do so.
453 mKeyguardHost.post(new Runnable() {
454 @Override
455 public void run() {
Jim Miller5ecd8112013-01-09 18:50:26 -0800456 try {
Jim Miller25190572013-02-28 17:36:24 -0800457 callback.onShown(token);
Jim Miller5ecd8112013-01-09 18:50:26 -0800458 } catch (RemoteException e) {
Jim Miller25190572013-02-28 17:36:24 -0800459 Slog.w(TAG, "Exception calling onShown():", e);
Craig Mautner904732c2012-10-17 15:20:24 -0700460 }
Dianne Hackborn38e29a62011-09-18 14:43:08 -0700461 }
Craig Mautner904732c2012-10-17 15:20:24 -0700462 });
463 } else {
Jim Miller5ecd8112013-01-09 18:50:26 -0800464 try {
Craig Mautner4e8a19c2013-10-08 17:26:08 -0700465 callback.onShown(token);
Jim Miller5ecd8112013-01-09 18:50:26 -0800466 } catch (RemoteException e) {
Jim Miller25190572013-02-28 17:36:24 -0800467 Slog.w(TAG, "Exception calling onShown():", e);
Jim Miller5ecd8112013-01-09 18:50:26 -0800468 }
Craig Mautner904732c2012-10-17 15:20:24 -0700469 }
Dianne Hackborn38e29a62011-09-18 14:43:08 -0700470 }
Jim Miller25190572013-02-28 17:36:24 -0800471 } else if (callback != null) {
Jim Miller5ecd8112013-01-09 18:50:26 -0800472 try {
Craig Mautner4e8a19c2013-10-08 17:26:08 -0700473 callback.onShown(token);
Jim Miller5ecd8112013-01-09 18:50:26 -0800474 } catch (RemoteException e) {
Jim Miller25190572013-02-28 17:36:24 -0800475 Slog.w(TAG, "Exception calling onShown():", e);
Jim Miller5ecd8112013-01-09 18:50:26 -0800476 }
Brian Colonna15ea55a2011-09-09 11:48:16 -0400477 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800478 }
479
480 public synchronized void verifyUnlock() {
481 if (DEBUG) Log.d(TAG, "verifyUnlock()");
Adam Cohenf7522022012-10-03 20:03:18 -0700482 show(null);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800483 mKeyguardView.verifyUnlock();
484 }
485
486 /**
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800487 * Hides the keyguard view
488 */
489 public synchronized void hide() {
490 if (DEBUG) Log.d(TAG, "hide()");
Jim Miller6edf2632011-09-05 16:03:14 -0700491
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800492 if (mKeyguardHost != null) {
493 mKeyguardHost.setVisibility(View.GONE);
Jim Miller223ce5c2012-10-05 19:13:23 -0700494
495 // We really only want to preserve keyguard state for configuration changes. Hence
496 // we should clear state of widgets (e.g. Music) when we hide keyguard so it can
497 // start with a fresh state when we return.
498 mStateContainer.clear();
499
Karl Rosaena35d7532009-09-25 10:24:26 -0700500 // Don't do this right away, so we can let the view continue to animate
Dianne Hackborn01ad2f42009-09-24 19:24:56 -0700501 // as it goes away.
Karl Rosaena35d7532009-09-25 10:24:26 -0700502 if (mKeyguardView != null) {
Jim Miller22dfe722009-10-07 01:30:21 -0700503 final KeyguardViewBase lastView = mKeyguardView;
504 mKeyguardView = null;
Karl Rosaena35d7532009-09-25 10:24:26 -0700505 mKeyguardHost.postDelayed(new Runnable() {
Craig Mautner904732c2012-10-17 15:20:24 -0700506 @Override
Karl Rosaena35d7532009-09-25 10:24:26 -0700507 public void run() {
Dianne Hackbornd7f7deb2009-09-25 17:35:10 -0700508 synchronized (KeyguardViewManager.this) {
Jim Miller22dfe722009-10-07 01:30:21 -0700509 lastView.cleanUp();
Adam Powell43a372f2013-09-27 17:43:53 -0700510 // Let go of any large bitmaps.
511 mKeyguardHost.setCustomBackground(null);
Adam Powell94719072013-10-10 20:01:49 -0700512 updateShowWallpaper(true);
Jim Miller8b886fa2011-01-13 17:56:35 -0800513 mKeyguardHost.removeView(lastView);
Jim Miller0b9d8ac2013-11-07 19:15:49 -0800514 mViewMediatorCallback.keyguardGone();
Dianne Hackbornd7f7deb2009-09-25 17:35:10 -0700515 }
Karl Rosaena35d7532009-09-25 10:24:26 -0700516 }
Jim Miller0b9d8ac2013-11-07 19:15:49 -0800517 }, HIDE_KEYGUARD_DELAY);
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800518 }
519 }
520 }
521
522 /**
Craig Mautnerad09bcc2012-10-08 13:33:11 -0700523 * Dismisses the keyguard by going to the next screen or making it gone.
524 */
525 public synchronized void dismiss() {
526 if (mScreenOn) {
527 mKeyguardView.dismiss();
528 }
529 }
530
531 /**
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800532 * @return Whether the keyguard is showing
533 */
534 public synchronized boolean isShowing() {
535 return (mKeyguardHost != null && mKeyguardHost.getVisibility() == View.VISIBLE);
536 }
Jim Miller4eeb4f62012-11-08 00:04:29 -0800537
538 public void showAssistant() {
539 if (mKeyguardView != null) {
540 mKeyguardView.showAssistant();
541 }
542 }
Jim Millercaf24fc2013-09-10 18:37:01 -0700543
544 public void dispatch(MotionEvent event) {
545 if (mKeyguardView != null) {
546 mKeyguardView.dispatch(event);
547 }
548 }
Jim Miller138f25d2013-09-25 13:46:58 -0700549
550 public void launchCamera() {
551 if (mKeyguardView != null) {
552 mKeyguardView.launchCamera();
553 }
554 }
The Android Open Source Project1f838aa2009-03-03 19:32:13 -0800555}