KeyguardViewManager.java revision e28016c28620f070a6737c7fd536437e7b116f84
1/* 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 17package com.android.internal.policy.impl; 18 19import com.android.internal.R; 20 21import android.content.Context; 22import android.content.pm.ActivityInfo; 23import android.graphics.PixelFormat; 24import android.graphics.Canvas; 25import android.util.Log; 26import android.view.View; 27import android.view.ViewGroup; 28import android.view.ViewManager; 29import android.view.WindowManager; 30import android.widget.FrameLayout; 31 32/** 33 * Manages creating, showing, hiding and resetting the keyguard. Calls back 34 * via {@link com.android.internal.policy.impl.KeyguardViewCallback} to poke 35 * the wake lock and report that the keyguard is done, which is in turn, 36 * reported to this class by the current {@link KeyguardViewBase}. 37 */ 38public class KeyguardViewManager implements KeyguardWindowController { 39 private final static boolean DEBUG = false; 40 private static String TAG = "KeyguardViewManager"; 41 42 private final Context mContext; 43 private final ViewManager mViewManager; 44 private final KeyguardViewCallback mCallback; 45 private final KeyguardViewProperties mKeyguardViewProperties; 46 47 private final KeyguardUpdateMonitor mUpdateMonitor; 48 49 private WindowManager.LayoutParams mWindowLayoutParams; 50 private boolean mNeedsInput = false; 51 52 private FrameLayout mKeyguardHost; 53 private KeyguardViewBase mKeyguardView; 54 55 private boolean mScreenOn = false; 56 57 /** 58 * @param context Used to create views. 59 * @param viewManager Keyguard will be attached to this. 60 * @param callback Used to notify of changes. 61 */ 62 public KeyguardViewManager(Context context, ViewManager viewManager, 63 KeyguardViewCallback callback, KeyguardViewProperties keyguardViewProperties, KeyguardUpdateMonitor updateMonitor) { 64 mContext = context; 65 mViewManager = viewManager; 66 mCallback = callback; 67 mKeyguardViewProperties = keyguardViewProperties; 68 69 mUpdateMonitor = updateMonitor; 70 } 71 72 /** 73 * Helper class to host the keyguard view. 74 */ 75 private static class KeyguardViewHost extends FrameLayout { 76 private final KeyguardViewCallback mCallback; 77 78 private KeyguardViewHost(Context context, KeyguardViewCallback callback) { 79 super(context); 80 mCallback = callback; 81 } 82 83 @Override 84 protected void dispatchDraw(Canvas canvas) { 85 super.dispatchDraw(canvas); 86 mCallback.keyguardDoneDrawing(); 87 } 88 } 89 90 /** 91 * Show the keyguard. Will handle creating and attaching to the view manager 92 * lazily. 93 */ 94 public synchronized void show() { 95 if (DEBUG) Log.d(TAG, "show(); mKeyguardView==" + mKeyguardView); 96 97 if (mKeyguardHost == null) { 98 if (DEBUG) Log.d(TAG, "keyguard host is null, creating it..."); 99 100 mKeyguardHost = new KeyguardViewHost(mContext, mCallback); 101 102 final int stretch = ViewGroup.LayoutParams.MATCH_PARENT; 103 int flags = WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN 104 | WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER 105 | WindowManager.LayoutParams.FLAG_KEEP_SURFACE_WHILE_ANIMATING 106 /*| WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN 107 | WindowManager.LayoutParams.FLAG_LAYOUT_INSET_DECOR*/ ; 108 if (!mNeedsInput) { 109 flags |= WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM; 110 } 111 WindowManager.LayoutParams lp = new WindowManager.LayoutParams( 112 stretch, stretch, WindowManager.LayoutParams.TYPE_KEYGUARD, 113 flags, PixelFormat.TRANSLUCENT); 114 lp.softInputMode = WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN; 115 lp.windowAnimations = com.android.internal.R.style.Animation_LockScreen; 116 117 // TODO: Sometimes we get the wrong value for the sensor resource we use to configure 118 // this. However, the current UI design has LockScreen always respond to orientation so 119 // we don't need this for the time-being. 120 // 121 // For reference, the configuration variable is R.bool.config_enableLockScreenRotation 122 lp.screenOrientation = ActivityInfo.SCREEN_ORIENTATION_SENSOR; 123 lp.setTitle("Keyguard"); 124 mWindowLayoutParams = lp; 125 126 mViewManager.addView(mKeyguardHost, lp); 127 } 128 129 if (mKeyguardView == null) { 130 if (DEBUG) Log.d(TAG, "keyguard view is null, creating it..."); 131 mKeyguardView = mKeyguardViewProperties.createKeyguardView(mContext, mUpdateMonitor, this); 132 mKeyguardView.setId(R.id.lock_screen); 133 mKeyguardView.setCallback(mCallback); 134 135 final ViewGroup.LayoutParams lp = new FrameLayout.LayoutParams( 136 ViewGroup.LayoutParams.MATCH_PARENT, 137 ViewGroup.LayoutParams.MATCH_PARENT); 138 139 mKeyguardHost.addView(mKeyguardView, lp); 140 141 if (mScreenOn) { 142 mKeyguardView.onScreenTurnedOn(); 143 } 144 } 145 146 mKeyguardHost.setVisibility(View.VISIBLE); 147 mKeyguardView.requestFocus(); 148 } 149 150 public void setNeedsInput(boolean needsInput) { 151 mNeedsInput = needsInput; 152 if (mWindowLayoutParams != null) { 153 if (needsInput) { 154 mWindowLayoutParams.flags &= 155 ~WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM; 156 } else { 157 mWindowLayoutParams.flags |= 158 WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM; 159 } 160 mViewManager.updateViewLayout(mKeyguardHost, mWindowLayoutParams); 161 } 162 } 163 164 /** 165 * Reset the state of the view. 166 */ 167 public synchronized void reset() { 168 if (DEBUG) Log.d(TAG, "reset()"); 169 if (mKeyguardView != null) { 170 mKeyguardView.reset(); 171 } 172 } 173 174 public synchronized void onScreenTurnedOff() { 175 if (DEBUG) Log.d(TAG, "onScreenTurnedOff()"); 176 mScreenOn = false; 177 if (mKeyguardView != null) { 178 mKeyguardView.onScreenTurnedOff(); 179 } 180 } 181 182 public synchronized void onScreenTurnedOn() { 183 if (DEBUG) Log.d(TAG, "onScreenTurnedOn()"); 184 mScreenOn = true; 185 if (mKeyguardView != null) { 186 mKeyguardView.onScreenTurnedOn(); 187 } 188 } 189 190 public synchronized void verifyUnlock() { 191 if (DEBUG) Log.d(TAG, "verifyUnlock()"); 192 show(); 193 mKeyguardView.verifyUnlock(); 194 } 195 196 /** 197 * A key has woken the device. We use this to potentially adjust the state 198 * of the lock screen based on the key. 199 * 200 * The 'Tq' suffix is per the documentation in {@link android.view.WindowManagerPolicy}. 201 * Be sure not to take any action that takes a long time; any significant 202 * action should be posted to a handler. 203 * 204 * @param keyCode The wake key. 205 */ 206 public boolean wakeWhenReadyTq(int keyCode) { 207 if (DEBUG) Log.d(TAG, "wakeWhenReady(" + keyCode + ")"); 208 if (mKeyguardView != null) { 209 mKeyguardView.wakeWhenReadyTq(keyCode); 210 return true; 211 } else { 212 Log.w(TAG, "mKeyguardView is null in wakeWhenReadyTq"); 213 return false; 214 } 215 } 216 217 /** 218 * Hides the keyguard view 219 */ 220 public synchronized void hide() { 221 if (DEBUG) Log.d(TAG, "hide()"); 222 if (mKeyguardHost != null) { 223 mKeyguardHost.setVisibility(View.GONE); 224 // Don't do this right away, so we can let the view continue to animate 225 // as it goes away. 226 if (mKeyguardView != null) { 227 final KeyguardViewBase lastView = mKeyguardView; 228 mKeyguardView = null; 229 mKeyguardHost.postDelayed(new Runnable() { 230 public void run() { 231 synchronized (KeyguardViewManager.this) { 232 mKeyguardHost.removeView(lastView); 233 lastView.cleanUp(); 234 } 235 } 236 }, 500); 237 } 238 } 239 } 240 241 /** 242 * @return Whether the keyguard is showing 243 */ 244 public synchronized boolean isShowing() { 245 return (mKeyguardHost != null && mKeyguardHost.getVisibility() == View.VISIBLE); 246 } 247} 248