KeyguardManager.java revision 37fde0aca01fcfcd8483df70a4e82f622e15fda2
1/*
2 * Copyright (C) 2007 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 android.app;
18
19import android.content.Context;
20import android.os.Binder;
21import android.os.RemoteException;
22import android.os.IBinder;
23import android.os.ServiceManager;
24import android.view.IWindowManager;
25import android.view.IOnKeyguardExitResult;
26
27/**
28 * Class that can be used to lock and unlock the keyboard. Get an instance of this
29 * class by calling {@link android.content.Context#getSystemService(java.lang.String)}
30 * with argument {@link android.content.Context#KEYGUARD_SERVICE}. The
31 * actual class to control the keyboard locking is
32 * {@link android.app.KeyguardManager.KeyguardLock}.
33 */
34public class KeyguardManager {
35    private IWindowManager mWM;
36
37    /**
38     * @deprecated Use {@link android.view.WindowManager.LayoutParams#FLAG_DISMISS_KEYGUARD}
39     * and/or {@link android.view.WindowManager.LayoutParams#FLAG_SHOW_WHEN_LOCKED}
40     * instead; this allows you to seamlessly hide the keyguard as your application
41     * moves in and out of the foreground and does not require that any special
42     * permissions be requested.
43     *
44     * Handle returned by {@link KeyguardManager#newKeyguardLock} that allows
45     * you to disable / reenable the keyguard.
46     */
47    public class KeyguardLock {
48        private IBinder mToken = new Binder();
49        private String mTag;
50
51        KeyguardLock(String tag) {
52            mTag = tag;
53        }
54
55        /**
56         * Disable the keyguard from showing.  If the keyguard is currently
57         * showing, hide it.  The keyguard will be prevented from showing again
58         * until {@link #reenableKeyguard()} is called.
59         *
60         * A good place to call this is from {@link android.app.Activity#onResume()}
61         *
62         * Note: This call has no effect while any {@link android.app.admin.DevicePolicyManager}
63         * is enabled that requires a password.
64         *
65         * @see #reenableKeyguard()
66         */
67        public void disableKeyguard() {
68            try {
69                mWM.disableKeyguard(mToken, mTag);
70            } catch (RemoteException ex) {
71            }
72        }
73
74        /**
75         * Reenable the keyguard.  The keyguard will reappear if the previous
76         * call to {@link #disableKeyguard()} caused it to be hidden.
77         *
78         * A good place to call this is from {@link android.app.Activity#onPause()}
79         *
80         * Note: This call has no effect while any {@link android.app.admin.DevicePolicyManager}
81         * is enabled that requires a password.
82         *
83         * @see #disableKeyguard()
84         */
85        public void reenableKeyguard() {
86            try {
87                mWM.reenableKeyguard(mToken);
88            } catch (RemoteException ex) {
89            }
90        }
91    }
92
93    /**
94     * Callback passed to {@link KeyguardManager#exitKeyguardSecurely} to notify
95     * caller of result.
96     */
97    public interface OnKeyguardExitResult {
98
99        /**
100         * @param success True if the user was able to authenticate, false if
101         *   not.
102         */
103        void onKeyguardExitResult(boolean success);
104    }
105
106
107    KeyguardManager() {
108        mWM = IWindowManager.Stub.asInterface(ServiceManager.getService(Context.WINDOW_SERVICE));
109    }
110
111    /**
112     * @deprecated Use {@link android.view.WindowManager.LayoutParams#FLAG_DISMISS_KEYGUARD}
113     * and/or {@link android.view.WindowManager.LayoutParams#FLAG_SHOW_WHEN_LOCKED}
114     * instead; this allows you to seamlessly hide the keyguard as your application
115     * moves in and out of the foreground and does not require that any special
116     * permissions be requested.
117     *
118     * Enables you to lock or unlock the keyboard. Get an instance of this class by
119     * calling {@link android.content.Context#getSystemService(java.lang.String) Context.getSystemService()}.
120     * This class is wrapped by {@link android.app.KeyguardManager KeyguardManager}.
121     * @param tag A tag that informally identifies who you are (for debugging who
122     *   is disabling he keyguard).
123     *
124     * @return A {@link KeyguardLock} handle to use to disable and reenable the
125     *   keyguard.
126     */
127    @Deprecated
128    public KeyguardLock newKeyguardLock(String tag) {
129        return new KeyguardLock(tag);
130    }
131
132    /**
133     * Return whether the keyguard is currently locked.
134     *
135     * @return true if keyguard is locked.
136     */
137    public boolean isKeyguardLocked() {
138        try {
139            return mWM.isKeyguardLocked();
140        } catch (RemoteException ex) {
141            return false;
142        }
143    }
144
145    /**
146     * Return whether the keyguard requires a password to unlock.
147     *
148     * @return true if keyguard is secure.
149     */
150    public boolean isKeyguardSecure() {
151        try {
152            return mWM.isKeyguardSecure();
153        } catch (RemoteException ex) {
154            return false;
155        }
156    }
157
158    /**
159     * If keyguard screen is showing or in restricted key input mode (i.e. in
160     * keyguard password emergency screen). When in such mode, certain keys,
161     * such as the Home key and the right soft keys, don't work.
162     *
163     * @return true if in keyguard restricted input mode.
164     *
165     * @see android.view.WindowManagerPolicy#inKeyguardRestrictedKeyInputMode
166     */
167    public boolean inKeyguardRestrictedInputMode() {
168        try {
169            return mWM.inKeyguardRestrictedInputMode();
170        } catch (RemoteException ex) {
171            return false;
172        }
173    }
174
175    /**
176     * @deprecated Use {@link android.view.WindowManager.LayoutParams#FLAG_DISMISS_KEYGUARD}
177     * and/or {@link android.view.WindowManager.LayoutParams#FLAG_SHOW_WHEN_LOCKED}
178     * instead; this allows you to seamlessly hide the keyguard as your application
179     * moves in and out of the foreground and does not require that any special
180     * permissions be requested.
181     *
182     * Exit the keyguard securely.  The use case for this api is that, after
183     * disabling the keyguard, your app, which was granted permission to
184     * disable the keyguard and show a limited amount of information deemed
185     * safe without the user getting past the keyguard, needs to navigate to
186     * something that is not safe to view without getting past the keyguard.
187     *
188     * This will, if the keyguard is secure, bring up the unlock screen of
189     * the keyguard.
190     *
191     * @param callback Let's you know whether the operation was succesful and
192     *   it is safe to launch anything that would normally be considered safe
193     *   once the user has gotten past the keyguard.
194     */
195    @Deprecated
196    public void exitKeyguardSecurely(final OnKeyguardExitResult callback) {
197        try {
198            mWM.exitKeyguardSecurely(new IOnKeyguardExitResult.Stub() {
199                public void onKeyguardExitResult(boolean success) throws RemoteException {
200                    callback.onKeyguardExitResult(success);
201                }
202            });
203        } catch (RemoteException e) {
204
205        }
206    }
207}
208