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