KeyguardBouncer.java revision c14f829506a5273e8022b461db2d61038b18ac4b
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.view.KeyEvent;
21import android.view.LayoutInflater;
22import android.view.View;
23import android.view.ViewGroup;
24
25import com.android.internal.widget.LockPatternUtils;
26import com.android.keyguard.KeyguardViewBase;
27import com.android.keyguard.R;
28import com.android.keyguard.ViewMediatorCallback;
29import com.android.systemui.keyguard.KeyguardViewMediator;
30
31import static com.android.keyguard.KeyguardHostView.OnDismissAction;
32import static com.android.keyguard.KeyguardSecurityModel.*;
33
34/**
35 * A class which manages the bouncer on the lockscreen.
36 */
37public class KeyguardBouncer {
38
39    private Context mContext;
40    private ViewMediatorCallback mCallback;
41    private LockPatternUtils mLockPatternUtils;
42    private ViewGroup mContainer;
43    private StatusBarWindowManager mWindowManager;
44    private KeyguardViewBase mKeyguardView;
45    private ViewGroup mRoot;
46
47    public KeyguardBouncer(Context context, ViewMediatorCallback callback,
48            LockPatternUtils lockPatternUtils, StatusBarWindowManager windowManager,
49            ViewGroup container) {
50        mContext = context;
51        mCallback = callback;
52        mLockPatternUtils = lockPatternUtils;
53        mContainer = container;
54        mWindowManager = windowManager;
55    }
56
57    public void show() {
58        ensureView();
59        if (mRoot.getVisibility() == View.VISIBLE) {
60            return;
61        }
62
63        // Try to dismiss the Keyguard. If no security pattern is set, this will dismiss the whole
64        // Keyguard. If we need to authenticate, show the bouncer.
65        if (!mKeyguardView.dismiss()) {
66            mRoot.setVisibility(View.VISIBLE);
67            mKeyguardView.onResume();
68            mKeyguardView.startAppearAnimation();
69        }
70    }
71
72    public void showWithDismissAction(OnDismissAction r) {
73        ensureView();
74        mKeyguardView.setOnDismissAction(r);
75        show();
76    }
77
78    public void hide(boolean destroyView) {
79        if (mKeyguardView != null) {
80            mKeyguardView.cleanUp();
81        }
82        if (destroyView) {
83            removeView();
84        } else if (mRoot != null) {
85            mRoot.setVisibility(View.INVISIBLE);
86        }
87    }
88
89    /**
90     * Reset the state of the view.
91     */
92    public void reset() {
93        inflateView();
94    }
95
96    public void onScreenTurnedOff() {
97        if (mKeyguardView != null && mRoot != null && mRoot.getVisibility() == View.VISIBLE) {
98            mKeyguardView.onPause();
99        }
100    }
101
102    public long getUserActivityTimeout() {
103        if (mKeyguardView != null) {
104            long timeout = mKeyguardView.getUserActivityTimeout();
105            if (timeout >= 0) {
106                return timeout;
107            }
108        }
109        return KeyguardViewMediator.AWAKE_INTERVAL_DEFAULT_MS;
110    }
111
112    public boolean isShowing() {
113        return mRoot != null && mRoot.getVisibility() == View.VISIBLE;
114    }
115
116    public void prepare() {
117        ensureView();
118    }
119
120    private void ensureView() {
121        if (mRoot == null) {
122            inflateView();
123        }
124    }
125
126    private void inflateView() {
127        removeView();
128        mRoot = (ViewGroup) LayoutInflater.from(mContext).inflate(R.layout.keyguard_bouncer, null);
129        mKeyguardView = (KeyguardViewBase) mRoot.findViewById(R.id.keyguard_host_view);
130        mKeyguardView.setLockPatternUtils(mLockPatternUtils);
131        mKeyguardView.setViewMediatorCallback(mCallback);
132        mContainer.addView(mRoot, mContainer.getChildCount());
133        mRoot.setVisibility(View.INVISIBLE);
134        mRoot.setSystemUiVisibility(View.STATUS_BAR_DISABLE_HOME);
135    }
136
137    private void removeView() {
138        if (mRoot != null && mRoot.getParent() == mContainer) {
139            mContainer.removeView(mRoot);
140            mRoot = null;
141        }
142    }
143
144    public boolean onBackPressed() {
145        return mKeyguardView != null && mKeyguardView.handleBackKey();
146    }
147
148    /**
149     * @return True if and only if the current security method should be shown before showing
150     *         the notifications on Keyguard, like SIM PIN/PUK.
151     */
152    public boolean needsFullscreenBouncer() {
153        if (mKeyguardView != null) {
154            SecurityMode mode = mKeyguardView.getSecurityMode();
155            return mode == SecurityMode.SimPin
156                    || mode == SecurityMode.SimPuk;
157        }
158        return false;
159    }
160
161    public boolean isSecure() {
162        return mKeyguardView == null || mKeyguardView.getSecurityMode() != SecurityMode.None;
163    }
164
165    public boolean onMenuPressed() {
166        ensureView();
167        if (mKeyguardView.handleMenuKey()) {
168
169            // We need to show it in case it is secure. If not, it will get dismissed in any case.
170            mRoot.setVisibility(View.VISIBLE);
171            mKeyguardView.requestFocus();
172            mKeyguardView.onResume();
173            return true;
174        } else {
175            return false;
176        }
177    }
178
179    public boolean interceptMediaKey(KeyEvent event) {
180        ensureView();
181        return mKeyguardView.interceptMediaKey(event);
182    }
183}
184