StatusBarKeyguardViewManager.java revision 25ab3d94387597a24619723df687214320f17e76
1/* 2 * Copyright (C) 2014 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.systemui.statusbar.phone; 18 19import android.content.Context; 20import android.os.Bundle; 21import android.os.IBinder; 22import android.os.RemoteException; 23import android.util.Log; 24import android.util.Slog; 25import android.view.LayoutInflater; 26import android.view.View; 27import android.view.ViewGroup; 28import android.view.ViewTreeObserver; 29 30import com.android.internal.policy.IKeyguardShowCallback; 31import com.android.internal.widget.LockPatternUtils; 32import com.android.keyguard.KeyguardHostView; 33import com.android.keyguard.KeyguardSimpleHostView; 34import com.android.keyguard.R; 35import com.android.keyguard.ViewMediatorCallback; 36import com.android.systemui.keyguard.KeyguardViewMediator; 37 38/** 39 * Manages creating, showing, hiding and resetting the keyguard within the status bar. Calls back 40 * via {@link ViewMediatorCallback} to poke the wake lock and report that the keyguard is done, 41 * which is in turn, reported to this class by the current 42 * {@link com.android.keyguard.KeyguardViewBase}. 43 */ 44public class StatusBarKeyguardViewManager { 45 private static String TAG = "StatusBarKeyguardViewManager"; 46 47 private final Context mContext; 48 49 private LockPatternUtils mLockPatternUtils; 50 private ViewMediatorCallback mViewMediatorCallback; 51 private PhoneStatusBar mPhoneStatusBar; 52 53 private ViewGroup mContainer; 54 private StatusBarWindowManager mStatusBarWindowManager; 55 56 private boolean mScreenOn = false; 57 private KeyguardBouncer mBouncer; 58 private boolean mShowing; 59 private boolean mOccluded = false; 60 61 public StatusBarKeyguardViewManager(Context context, ViewMediatorCallback callback, 62 LockPatternUtils lockPatternUtils) { 63 mContext = context; 64 mViewMediatorCallback = callback; 65 mLockPatternUtils = lockPatternUtils; 66 } 67 68 public void registerStatusBar(PhoneStatusBar phoneStatusBar, 69 ViewGroup container, StatusBarWindowManager statusBarWindowManager) { 70 mPhoneStatusBar = phoneStatusBar; 71 mContainer = container; 72 mStatusBarWindowManager = statusBarWindowManager; 73 mBouncer = new KeyguardBouncer(mContext, mViewMediatorCallback, mLockPatternUtils, 74 mStatusBarWindowManager, container); 75 } 76 77 /** 78 * Show the keyguard. Will handle creating and attaching to the view manager 79 * lazily. 80 */ 81 public void show(Bundle options) { 82 mShowing = true; 83 mStatusBarWindowManager.setKeyguardShowing(true); 84 showBouncerOrKeyguard(); 85 updateBackButtonState(); 86 } 87 88 /** 89 * Shows the notification keyguard or the bouncer depending on 90 * {@link KeyguardBouncer#needsFullscreenBouncer()}. 91 */ 92 private void showBouncerOrKeyguard() { 93 if (mBouncer.needsFullscreenBouncer()) { 94 95 // The keyguard might be showing (already). So we need to hide it. 96 mPhoneStatusBar.hideKeyguard(); 97 mBouncer.show(); 98 } else { 99 mPhoneStatusBar.showKeyguard(); 100 mBouncer.hide(); 101 mBouncer.prepare(); 102 } 103 } 104 105 public void showBouncer() { 106 if (!mOccluded) { 107 mBouncer.show(); 108 updateBackButtonState(); 109 } 110 } 111 112 /** 113 * Reset the state of the view. 114 */ 115 public void reset() { 116 showBouncerOrKeyguard(); 117 updateBackButtonState(); 118 } 119 120 public void onScreenTurnedOff() { 121 mScreenOn = false; 122 mBouncer.onScreenTurnedOff(); 123 } 124 125 public void onScreenTurnedOn(final IKeyguardShowCallback callback) { 126 mScreenOn = true; 127 if (callback != null) { 128 callbackAfterDraw(callback); 129 } 130 } 131 132 private void callbackAfterDraw(final IKeyguardShowCallback callback) { 133 mContainer.post(new Runnable() { 134 @Override 135 public void run() { 136 try { 137 callback.onShown(mContainer.getWindowToken()); 138 } catch (RemoteException e) { 139 Slog.w(TAG, "Exception calling onShown():", e); 140 } 141 } 142 }); 143 } 144 145 public void verifyUnlock() { 146 dismiss(); 147 } 148 149 public void setNeedsInput(boolean needsInput) { 150 mStatusBarWindowManager.setKeyguardNeedsInput(needsInput); 151 } 152 153 public void updateUserActivityTimeout() { 154 mStatusBarWindowManager.setKeyguardUserActivityTimeout(mBouncer.getUserActivityTimeout()); 155 } 156 157 public void setOccluded(boolean occluded) { 158 mOccluded = occluded; 159 if (occluded) { 160 mPhoneStatusBar.hideKeyguard(); 161 mBouncer.hide(); 162 } else { 163 showBouncerOrKeyguard(); 164 } 165 mStatusBarWindowManager.setKeyguardOccluded(occluded); 166 } 167 168 /** 169 * Hides the keyguard view 170 */ 171 public void hide() { 172 mShowing = false; 173 mPhoneStatusBar.hideKeyguard(); 174 mStatusBarWindowManager.setKeyguardShowing(false); 175 mBouncer.hide(); 176 mViewMediatorCallback.keyguardGone(); 177 } 178 179 /** 180 * Dismisses the keyguard by going to the next screen or making it gone. 181 */ 182 public void dismiss() { 183 if (mScreenOn) { 184 showBouncer(); 185 } 186 } 187 188 /** 189 * @return Whether the keyguard is showing 190 */ 191 public boolean isShowing() { 192 return mShowing; 193 } 194 195 /** 196 * Notifies this manager that the back button has been pressed. 197 * 198 * @return whether the back press has been handled 199 */ 200 public boolean onBackPressed() { 201 if (mBouncer.isShowing()) { 202 mBouncer.hide(); 203 mPhoneStatusBar.showKeyguard(); 204 updateBackButtonState(); 205 return true; 206 } 207 return false; 208 } 209 210 private void updateBackButtonState() { 211 int vis = mContainer.getSystemUiVisibility(); 212 boolean bouncerDismissable = mBouncer.isShowing() && !mBouncer.needsFullscreenBouncer(); 213 if (bouncerDismissable || !mShowing) { 214 mContainer.setSystemUiVisibility(vis & ~View.STATUS_BAR_DISABLE_BACK); 215 } else { 216 mContainer.setSystemUiVisibility(vis | View.STATUS_BAR_DISABLE_BACK); 217 } 218 } 219 220 public boolean onMenuPressed() { 221 return mBouncer.onMenuPressed(); 222 } 223} 224