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