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