StatusBarKeyguardViewManager.java revision 8c8bcc160aba9a5e93c8df2a99a39a856fafffab
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
60    public StatusBarKeyguardViewManager(Context context, ViewMediatorCallback callback,
61            LockPatternUtils lockPatternUtils) {
62        mContext = context;
63        mViewMediatorCallback = callback;
64        mLockPatternUtils = lockPatternUtils;
65    }
66
67    public void registerStatusBar(PhoneStatusBar phoneStatusBar,
68            ViewGroup container, StatusBarWindowManager statusBarWindowManager) {
69        mPhoneStatusBar = phoneStatusBar;
70        mContainer = container;
71        mStatusBarWindowManager = statusBarWindowManager;
72        mBouncer = new KeyguardBouncer(mContext, mViewMediatorCallback, mLockPatternUtils,
73                mStatusBarWindowManager, container);
74    }
75
76    /**
77     * Show the keyguard.  Will handle creating and attaching to the view manager
78     * lazily.
79     */
80    public void show(Bundle options) {
81        mShowing = true;
82        mStatusBarWindowManager.setKeyguardShowing(true);
83        showBouncerOrKeyguard();
84        updateBackButtonState();
85    }
86
87    /**
88     * Shows the notification keyguard or the bouncer depending on
89     * {@link KeyguardBouncer#needsFullscreenBouncer()}.
90     */
91    private void showBouncerOrKeyguard() {
92        if (mBouncer.needsFullscreenBouncer()) {
93
94            // The keyguard might be showing (already). So we need to hide it.
95            mPhoneStatusBar.hideKeyguard();
96            mBouncer.show();
97        } else {
98            mPhoneStatusBar.showKeyguard();
99            mBouncer.hide();
100            mBouncer.prepare();
101        }
102    }
103
104    public void showBouncer() {
105        mBouncer.show();
106        updateBackButtonState();
107    }
108
109    /**
110     * Reset the state of the view.
111     */
112    public void reset() {
113        showBouncerOrKeyguard();
114        updateBackButtonState();
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        if (callback != null) {
125            callbackAfterDraw(callback);
126        }
127    }
128
129    private void callbackAfterDraw(final IKeyguardShowCallback callback) {
130        mContainer.post(new Runnable() {
131            @Override
132            public void run() {
133                try {
134                    callback.onShown(mContainer.getWindowToken());
135                } catch (RemoteException e) {
136                    Slog.w(TAG, "Exception calling onShown():", e);
137                }
138            }
139        });
140    }
141
142    public void verifyUnlock() {
143        dismiss();
144    }
145
146    public void setNeedsInput(boolean needsInput) {
147        mStatusBarWindowManager.setKeyguardNeedsInput(needsInput);
148    }
149
150    public void updateUserActivityTimeout() {
151        mStatusBarWindowManager.setKeyguardUserActivityTimeout(mBouncer.getUserActivityTimeout());
152    }
153
154    public void setOccluded(boolean occluded) {
155        mStatusBarWindowManager.setKeyguardOccluded(occluded);
156    }
157
158    /**
159     * Hides the keyguard view
160     */
161    public void hide() {
162        mShowing = false;
163        mPhoneStatusBar.hideKeyguard();
164        mStatusBarWindowManager.setKeyguardShowing(false);
165        mBouncer.hide();
166        mViewMediatorCallback.keyguardGone();
167    }
168
169    /**
170     * Dismisses the keyguard by going to the next screen or making it gone.
171     */
172    public void dismiss() {
173        if (mScreenOn) {
174            showBouncer();
175        }
176    }
177
178    /**
179     * @return Whether the keyguard is showing
180     */
181    public boolean isShowing() {
182        return mShowing;
183    }
184
185    /**
186     * Notifies this manager that the back button has been pressed.
187     *
188     * @return whether the back press has been handled
189     */
190    public boolean onBackPressed() {
191        if (mBouncer.isShowing()) {
192            mBouncer.hide();
193            mPhoneStatusBar.showKeyguard();
194            updateBackButtonState();
195            return true;
196        }
197        return false;
198    }
199
200    private void updateBackButtonState() {
201        int vis = mContainer.getSystemUiVisibility();
202        boolean bouncerDismissable = mBouncer.isShowing() && !mBouncer.needsFullscreenBouncer();
203        if (bouncerDismissable || !mShowing) {
204            mContainer.setSystemUiVisibility(vis & ~View.STATUS_BAR_DISABLE_BACK);
205        } else {
206            mContainer.setSystemUiVisibility(vis | View.STATUS_BAR_DISABLE_BACK);
207        }
208    }
209
210    public boolean onMenuPressed() {
211        return mBouncer.onMenuPressed();
212    }
213}
214