StatusBarKeyguardViewManager.java revision 5cf17879a31b7b78c09ec50b727f921840dcf783
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.KeyguardSimpleHostView;
33import com.android.keyguard.R;
34import com.android.keyguard.ViewMediatorCallback;
35import com.android.systemui.keyguard.KeyguardViewMediator;
36
37/**
38 * Manages creating, showing, hiding and resetting the keyguard within the status bar. Calls back
39 * via {@link ViewMediatorCallback} to poke the wake lock and report that the keyguard is done,
40 * which is in turn, reported to this class by the current
41 * {@link com.android.keyguard.KeyguardViewBase}.
42 */
43public class StatusBarKeyguardViewManager {
44    private static String TAG = "StatusBarKeyguardViewManager";
45
46    private final Context mContext;
47
48    private LockPatternUtils mLockPatternUtils;
49    private ViewMediatorCallback mViewMediatorCallback;
50    private PhoneStatusBar mPhoneStatusBar;
51
52    private KeyguardSimpleHostView mKeyguardView;
53    private ViewGroup mRoot;
54    private ViewGroup mContainer;
55    private StatusBarWindowManager mStatusBarWindowManager;
56
57    private boolean mScreenOn = false;
58    private boolean mShowOnRegister;
59
60    public StatusBarKeyguardViewManager(Context context, ViewMediatorCallback callback,
61            LockPatternUtils lockPatternUtils) {
62        mContext = context;
63        mViewMediatorCallback = callback;
64        mLockPatternUtils = lockPatternUtils;
65
66    }
67
68    public void registerStatusBar(PhoneStatusBar phoneStatusBar,
69            ViewGroup container, StatusBarWindowManager statusBarWindowManager) {
70        mPhoneStatusBar = phoneStatusBar;
71        mContainer = container;
72        mStatusBarWindowManager = statusBarWindowManager;
73        if (mShowOnRegister) {
74            mShowOnRegister = false;
75            show(null);
76            if (mScreenOn) {
77                onScreenTurnedOn(null);
78            }
79        }
80    }
81
82    /**
83     * Show the keyguard.  Will handle creating and attaching to the view manager
84     * lazily.
85     */
86    public void show(Bundle options) {
87        if (mStatusBarWindowManager != null) {
88            ensureView();
89            mStatusBarWindowManager.setKeyguardShowing(true);
90            mKeyguardView.requestFocus();
91        } else {
92            mShowOnRegister = true;
93        }
94    }
95
96    private void ensureView() {
97        if (mRoot == null) {
98            inflateView();
99        }
100    }
101
102    private void inflateView() {
103        removeView();
104        mRoot = (ViewGroup) LayoutInflater.from(mContext).inflate(R.layout.keyguard_bouncer, null);
105        mKeyguardView = (KeyguardSimpleHostView) mRoot.findViewById(R.id.keyguard_host_view);
106        mKeyguardView.setLockPatternUtils(mLockPatternUtils);
107        mKeyguardView.setViewMediatorCallback(mViewMediatorCallback);
108        mContainer.addView(mRoot, mContainer.getChildCount());
109        mRoot.setSystemUiVisibility(View.STATUS_BAR_DISABLE_HOME);
110    }
111
112    private void removeView() {
113        if (mRoot != null && mRoot.getParent() == mContainer) {
114            mContainer.removeView(mRoot);
115            mRoot = null;
116        }
117    }
118
119    /**
120     * Reset the state of the view.
121     */
122    public void reset() {
123        inflateView();
124    }
125
126    public void onScreenTurnedOff() {
127        mScreenOn = false;
128        if (mKeyguardView != null) {
129            mKeyguardView.onScreenTurnedOff();
130        }
131    }
132
133    public void onScreenTurnedOn(final IKeyguardShowCallback callback) {
134        mScreenOn = true;
135        if (mKeyguardView != null) {
136            mKeyguardView.onScreenTurnedOn();
137            if (callback != null) {
138                callbackAfterDraw(callback);
139            }
140        } else {
141            try {
142                if (callback != null) {
143                    callback.onShown(null);
144                }
145            } catch (RemoteException e) {
146                Slog.w(TAG, "Exception calling onShown():", e);
147            }
148        }
149    }
150
151    private void callbackAfterDraw(final IKeyguardShowCallback callback) {
152        mKeyguardView.post(new Runnable() {
153            @Override
154            public void run() {
155                try {
156                    callback.onShown(mKeyguardView.getWindowToken());
157                } catch (RemoteException e) {
158                    Slog.w(TAG, "Exception calling onShown():", e);
159                }
160            }
161        });
162    }
163
164    public void verifyUnlock() {
165        show(null);
166        mKeyguardView.verifyUnlock();
167    }
168
169    public void setNeedsInput(boolean needsInput) {
170        if (mStatusBarWindowManager != null) {
171            mStatusBarWindowManager.setKeyguardNeedsInput(needsInput);
172        }
173    }
174
175    public void updateUserActivityTimeout() {
176
177        // Use the user activity timeout requested by the keyguard view, if any.
178        if (mKeyguardView != null) {
179            long timeout = mKeyguardView.getUserActivityTimeout();
180            if (timeout >= 0) {
181                mStatusBarWindowManager.setKeyguardUserActivityTimeout(timeout);
182                return;
183            }
184        }
185
186        // Otherwise, use the default timeout.
187        mStatusBarWindowManager.setKeyguardUserActivityTimeout(
188                KeyguardViewMediator.AWAKE_INTERVAL_DEFAULT_MS);
189    }
190
191    public void setOccluded(boolean occluded) {
192        if (mStatusBarWindowManager != null) {
193            mStatusBarWindowManager.setKeyguardOccluded(occluded);
194        }
195    }
196
197    /**
198     * Hides the keyguard view
199     */
200    public void hide() {
201        if (mPhoneStatusBar != null) {
202            mStatusBarWindowManager.setKeyguardShowing(false);
203            if (mKeyguardView != null) {
204                mKeyguardView.cleanUp();
205                mViewMediatorCallback.keyguardGone();
206            }
207            removeView();
208        }
209        mShowOnRegister = false;
210    }
211
212    /**
213     * Dismisses the keyguard by going to the next screen or making it gone.
214     */
215    public void dismiss() {
216        if (mScreenOn) {
217            mKeyguardView.dismiss();
218        }
219    }
220
221    /**
222     * @return Whether the keyguard is showing
223     */
224    public boolean isShowing() {
225        return mRoot != null && mRoot.getVisibility() == View.VISIBLE;
226    }
227}
228