StatusBarKeyguardViewManager.java revision cff0acb6b1eea23c3f44a078a0a5e81c11faea35
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 KeyguardHostView mKeyguardView;
54    private ViewGroup mRoot;
55    private ViewGroup mContainer;
56    private StatusBarWindowManager mStatusBarWindowManager;
57
58    private boolean mScreenOn = false;
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    }
74
75    /**
76     * Show the keyguard.  Will handle creating and attaching to the view manager
77     * lazily.
78     */
79    public void show(Bundle options) {
80        ensureView();
81        mStatusBarWindowManager.setKeyguardShowing(true);
82        mKeyguardView.requestFocus();
83    }
84
85    private void ensureView() {
86        if (mRoot == null) {
87            inflateView();
88        }
89    }
90
91    private void inflateView() {
92        removeView();
93        mRoot = (ViewGroup) LayoutInflater.from(mContext).inflate(R.layout.keyguard_bouncer, null);
94        mKeyguardView = (KeyguardHostView) mRoot.findViewById(R.id.keyguard_host_view);
95        mKeyguardView.setLockPatternUtils(mLockPatternUtils);
96        mKeyguardView.setViewMediatorCallback(mViewMediatorCallback);
97        mContainer.addView(mRoot, mContainer.getChildCount());
98        mRoot.setSystemUiVisibility(View.STATUS_BAR_DISABLE_HOME);
99    }
100
101    private void removeView() {
102        if (mRoot != null && mRoot.getParent() == mContainer) {
103            mContainer.removeView(mRoot);
104            mRoot = null;
105        }
106    }
107
108    /**
109     * Reset the state of the view.
110     */
111    public void reset() {
112        inflateView();
113    }
114
115    public void onScreenTurnedOff() {
116        mScreenOn = false;
117        if (mKeyguardView != null) {
118            mKeyguardView.onScreenTurnedOff();
119        }
120    }
121
122    public void onScreenTurnedOn(final IKeyguardShowCallback callback) {
123        mScreenOn = true;
124        if (mKeyguardView != null) {
125            mKeyguardView.onScreenTurnedOn();
126            if (callback != null) {
127                callbackAfterDraw(callback);
128            }
129        } else {
130            try {
131                if (callback != null) {
132                    callback.onShown(null);
133                }
134            } catch (RemoteException e) {
135                Slog.w(TAG, "Exception calling onShown():", e);
136            }
137        }
138    }
139
140    private void callbackAfterDraw(final IKeyguardShowCallback callback) {
141        mKeyguardView.post(new Runnable() {
142            @Override
143            public void run() {
144                try {
145                    callback.onShown(mKeyguardView.getWindowToken());
146                } catch (RemoteException e) {
147                    Slog.w(TAG, "Exception calling onShown():", e);
148                }
149            }
150        });
151    }
152
153    public void verifyUnlock() {
154        show(null);
155        mKeyguardView.verifyUnlock();
156    }
157
158    public void setNeedsInput(boolean needsInput) {
159        mStatusBarWindowManager.setKeyguardNeedsInput(needsInput);
160    }
161
162    public void updateUserActivityTimeout() {
163
164        // Use the user activity timeout requested by the keyguard view, if any.
165        if (mKeyguardView != null) {
166            long timeout = mKeyguardView.getUserActivityTimeout();
167            if (timeout >= 0) {
168                mStatusBarWindowManager.setKeyguardUserActivityTimeout(timeout);
169                return;
170            }
171        }
172
173        // Otherwise, use the default timeout.
174        mStatusBarWindowManager.setKeyguardUserActivityTimeout(
175                KeyguardViewMediator.AWAKE_INTERVAL_DEFAULT_MS);
176    }
177
178    public void setOccluded(boolean occluded) {
179        mStatusBarWindowManager.setKeyguardOccluded(occluded);
180    }
181
182    /**
183     * Hides the keyguard view
184     */
185    public void hide() {
186        mStatusBarWindowManager.setKeyguardShowing(false);
187        if (mKeyguardView != null) {
188            mKeyguardView.cleanUp();
189            mViewMediatorCallback.keyguardGone();
190        }
191        removeView();
192    }
193
194    /**
195     * Dismisses the keyguard by going to the next screen or making it gone.
196     */
197    public void dismiss() {
198        if (mScreenOn) {
199            mKeyguardView.dismiss();
200        }
201    }
202
203    /**
204     * @return Whether the keyguard is showing
205     */
206    public boolean isShowing() {
207        return mRoot != null && mRoot.getVisibility() == View.VISIBLE;
208    }
209}
210