KeyguardBouncer.java revision b13d754898eee54876799b3a306ff6fa453acfda
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 prepare() {
58        ensureView();
59    }
60
61    public void show() {
62        ensureView();
63
64        // Try to dismiss the Keyguard. If no security pattern is set, this will dismiss the whole
65        // Keyguard. If we need to authenticate, show the bouncer.
66        if (!mKeyguardView.dismiss()) {
67            mRoot.setVisibility(View.VISIBLE);
68            mKeyguardView.onResume();
69        }
70    }
71
72    public void showWithDismissAction(OnDismissAction r) {
73        ensureView();
74        mKeyguardView.setOnDismissAction(r);
75        show();
76    }
77
78    public void hide() {
79        if (mKeyguardView != null) {
80            mKeyguardView.cleanUp();
81        }
82        removeView();
83    }
84
85    /**
86     * Reset the state of the view.
87     */
88    public void reset() {
89        inflateView();
90    }
91
92    public void onScreenTurnedOff() {
93        if (mKeyguardView != null && mRoot != null && mRoot.getVisibility() == View.VISIBLE) {
94            mKeyguardView.onPause();
95        }
96    }
97
98    public long getUserActivityTimeout() {
99        if (mKeyguardView != null) {
100            long timeout = mKeyguardView.getUserActivityTimeout();
101            if (timeout >= 0) {
102                return timeout;
103            }
104        }
105        return KeyguardViewMediator.AWAKE_INTERVAL_DEFAULT_MS;
106    }
107
108    public boolean isShowing() {
109        return mRoot != null && mRoot.getVisibility() == View.VISIBLE;
110    }
111
112    private void ensureView() {
113        if (mRoot == null) {
114            inflateView();
115        }
116    }
117
118    private void inflateView() {
119        removeView();
120        mRoot = (ViewGroup) LayoutInflater.from(mContext).inflate(R.layout.keyguard_bouncer, null);
121        mKeyguardView = (KeyguardViewBase) mRoot.findViewById(R.id.keyguard_host_view);
122        mKeyguardView.setLockPatternUtils(mLockPatternUtils);
123        mKeyguardView.setViewMediatorCallback(mCallback);
124        mContainer.addView(mRoot, mContainer.getChildCount());
125        mRoot.setVisibility(View.INVISIBLE);
126        mRoot.setSystemUiVisibility(View.STATUS_BAR_DISABLE_HOME);
127    }
128
129    private void removeView() {
130        if (mRoot != null && mRoot.getParent() == mContainer) {
131            mContainer.removeView(mRoot);
132            mRoot = null;
133        }
134    }
135
136    public boolean onBackPressed() {
137        return mKeyguardView != null && mKeyguardView.handleBackKey();
138    }
139
140    /**
141     * @return True if and only if the current security method should be shown before showing
142     *         the notifications on Keyguard, like SIM PIN/PUK.
143     */
144    public boolean needsFullscreenBouncer() {
145        if (mKeyguardView != null) {
146            SecurityMode mode = mKeyguardView.getSecurityMode();
147            return mode == SecurityMode.SimPin
148                    || mode == SecurityMode.SimPuk;
149        }
150        return false;
151    }
152
153    public boolean isSecure() {
154        return mKeyguardView == null || mKeyguardView.getSecurityMode() != SecurityMode.None;
155    }
156
157    public boolean onMenuPressed() {
158        ensureView();
159        if (mKeyguardView.handleMenuKey()) {
160
161            // We need to show it in case it is secure. If not, it will get dismissed in any case.
162            mRoot.setVisibility(View.VISIBLE);
163            mKeyguardView.requestFocus();
164            mKeyguardView.onResume();
165            return true;
166        } else {
167            return false;
168        }
169    }
170
171    public boolean interceptMediaKey(KeyEvent event) {
172        ensureView();
173        return mKeyguardView.interceptMediaKey(event);
174    }
175}
176