blob: c2595cfd2cb7fd087fae29ddb698ff421925ae20 [file] [log] [blame]
Jorim Jaggi5cf17872014-03-26 18:31:48 +01001/*
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;
Jorim Jaggi13b67a92014-03-28 23:23:44 +010032import com.android.keyguard.KeyguardHostView;
Jorim Jaggi5cf17872014-03-26 18:31:48 +010033import 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
Jorim Jaggi5cf17872014-03-26 18:31:48 +010053 private ViewGroup mContainer;
54 private StatusBarWindowManager mStatusBarWindowManager;
55
56 private boolean mScreenOn = false;
Jorim Jaggi03c701e2014-04-02 12:39:51 +020057 private KeyguardBouncer mBouncer;
58 private boolean mShowing;
Jorim Jaggia6310292014-04-16 14:11:52 +020059 private boolean mOccluded;
Jorim Jaggi5cf17872014-03-26 18:31:48 +010060
61 public StatusBarKeyguardViewManager(Context context, ViewMediatorCallback callback,
62 LockPatternUtils lockPatternUtils) {
63 mContext = context;
64 mViewMediatorCallback = callback;
65 mLockPatternUtils = lockPatternUtils;
Jorim Jaggi5cf17872014-03-26 18:31:48 +010066 }
67
68 public void registerStatusBar(PhoneStatusBar phoneStatusBar,
69 ViewGroup container, StatusBarWindowManager statusBarWindowManager) {
70 mPhoneStatusBar = phoneStatusBar;
71 mContainer = container;
72 mStatusBarWindowManager = statusBarWindowManager;
Jorim Jaggi03c701e2014-04-02 12:39:51 +020073 mBouncer = new KeyguardBouncer(mContext, mViewMediatorCallback, mLockPatternUtils,
74 mStatusBarWindowManager, container);
Jorim Jaggi5cf17872014-03-26 18:31:48 +010075 }
76
77 /**
78 * Show the keyguard. Will handle creating and attaching to the view manager
79 * lazily.
80 */
81 public void show(Bundle options) {
Jorim Jaggi03c701e2014-04-02 12:39:51 +020082 mShowing = true;
Jorim Jaggicff0acb2014-03-31 16:35:15 +020083 mStatusBarWindowManager.setKeyguardShowing(true);
Jorim Jaggia005f1b2014-04-16 19:06:10 +020084 showBouncerOrKeyguard();
Jorim Jaggie5c7a892014-04-10 20:37:32 +020085 updateBackButtonState();
Jorim Jaggi5cf17872014-03-26 18:31:48 +010086 }
87
Jorim Jaggia005f1b2014-04-16 19:06:10 +020088 /**
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
Jorim Jaggi03c701e2014-04-02 12:39:51 +0200105 public void showBouncer() {
John Spurlock2e50f282014-04-17 23:45:50 +0000106 mBouncer.show();
107 updateBackButtonState();
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100108 }
109
110 /**
111 * Reset the state of the view.
112 */
113 public void reset() {
Jorim Jaggia005f1b2014-04-16 19:06:10 +0200114 showBouncerOrKeyguard();
Jorim Jaggie5c7a892014-04-10 20:37:32 +0200115 updateBackButtonState();
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100116 }
117
118 public void onScreenTurnedOff() {
119 mScreenOn = false;
Jorim Jaggi03c701e2014-04-02 12:39:51 +0200120 mBouncer.onScreenTurnedOff();
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100121 }
122
123 public void onScreenTurnedOn(final IKeyguardShowCallback callback) {
124 mScreenOn = true;
Jorim Jaggi03c701e2014-04-02 12:39:51 +0200125 if (callback != null) {
126 callbackAfterDraw(callback);
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100127 }
128 }
129
130 private void callbackAfterDraw(final IKeyguardShowCallback callback) {
Jorim Jaggi03c701e2014-04-02 12:39:51 +0200131 mContainer.post(new Runnable() {
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100132 @Override
133 public void run() {
134 try {
Jorim Jaggi03c701e2014-04-02 12:39:51 +0200135 callback.onShown(mContainer.getWindowToken());
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100136 } catch (RemoteException e) {
137 Slog.w(TAG, "Exception calling onShown():", e);
138 }
139 }
140 });
141 }
142
143 public void verifyUnlock() {
Jorim Jaggi03c701e2014-04-02 12:39:51 +0200144 dismiss();
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100145 }
146
147 public void setNeedsInput(boolean needsInput) {
Jorim Jaggicff0acb2014-03-31 16:35:15 +0200148 mStatusBarWindowManager.setKeyguardNeedsInput(needsInput);
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100149 }
150
151 public void updateUserActivityTimeout() {
Jorim Jaggi03c701e2014-04-02 12:39:51 +0200152 mStatusBarWindowManager.setKeyguardUserActivityTimeout(mBouncer.getUserActivityTimeout());
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100153 }
154
155 public void setOccluded(boolean occluded) {
Jorim Jaggia6310292014-04-16 14:11:52 +0200156 mOccluded = occluded;
Jorim Jaggicff0acb2014-03-31 16:35:15 +0200157 mStatusBarWindowManager.setKeyguardOccluded(occluded);
Jorim Jaggia6310292014-04-16 14:11:52 +0200158 updateBackButtonState();
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100159 }
160
161 /**
162 * Hides the keyguard view
163 */
164 public void hide() {
Jorim Jaggi03c701e2014-04-02 12:39:51 +0200165 mShowing = false;
166 mPhoneStatusBar.hideKeyguard();
Jorim Jaggicff0acb2014-03-31 16:35:15 +0200167 mStatusBarWindowManager.setKeyguardShowing(false);
Jorim Jaggi03c701e2014-04-02 12:39:51 +0200168 mBouncer.hide();
169 mViewMediatorCallback.keyguardGone();
Jorim Jaggia6310292014-04-16 14:11:52 +0200170 updateBackButtonState();
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100171 }
172
173 /**
174 * Dismisses the keyguard by going to the next screen or making it gone.
175 */
176 public void dismiss() {
177 if (mScreenOn) {
Jorim Jaggi0bed7f22014-04-03 16:12:54 +0200178 showBouncer();
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100179 }
180 }
181
Jorim Jaggi15682502014-04-23 12:01:36 +0200182 public boolean isSecure() {
183 return mBouncer.isSecure();
184 }
185
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100186 /**
187 * @return Whether the keyguard is showing
188 */
189 public boolean isShowing() {
Jorim Jaggi03c701e2014-04-02 12:39:51 +0200190 return mShowing;
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100191 }
Jorim Jaggie5c7a892014-04-10 20:37:32 +0200192
193 /**
194 * Notifies this manager that the back button has been pressed.
195 *
196 * @return whether the back press has been handled
197 */
198 public boolean onBackPressed() {
199 if (mBouncer.isShowing()) {
200 mBouncer.hide();
201 mPhoneStatusBar.showKeyguard();
202 updateBackButtonState();
203 return true;
204 }
205 return false;
206 }
207
208 private void updateBackButtonState() {
209 int vis = mContainer.getSystemUiVisibility();
Jorim Jaggia005f1b2014-04-16 19:06:10 +0200210 boolean bouncerDismissable = mBouncer.isShowing() && !mBouncer.needsFullscreenBouncer();
211 if (bouncerDismissable || !mShowing) {
Jorim Jaggie5c7a892014-04-10 20:37:32 +0200212 mContainer.setSystemUiVisibility(vis & ~View.STATUS_BAR_DISABLE_BACK);
213 } else {
214 mContainer.setSystemUiVisibility(vis | View.STATUS_BAR_DISABLE_BACK);
215 }
Jorim Jaggia6310292014-04-16 14:11:52 +0200216 if (!(mShowing && !mOccluded) || mBouncer.isShowing()) {
217 mPhoneStatusBar.getNavigationBarView().setVisibility(View.VISIBLE);
218 } else {
219 mPhoneStatusBar.getNavigationBarView().setVisibility(View.GONE);
220 }
Jorim Jaggie5c7a892014-04-10 20:37:32 +0200221 }
Jorim Jaggi8c8bcc12014-04-16 21:36:51 +0200222
223 public boolean onMenuPressed() {
224 return mBouncer.onMenuPressed();
225 }
Jorim Jaggi5cf17872014-03-26 18:31:48 +0100226}