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