KeyguardManager.java revision 50bfeec868157106e8b60abf8964cb24462af182
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.app.trust.ITrustManager;
20import android.content.Context;
21import android.content.Intent;
22import android.os.Binder;
23import android.os.RemoteException;
24import android.os.IBinder;
25import android.os.ServiceManager;
26import android.os.UserHandle;
27import android.view.IWindowManager;
28import android.view.IOnKeyguardExitResult;
29import android.view.WindowManagerGlobal;
30
31/**
32 * Class that can be used to lock and unlock the keyboard. Get an instance of this
33 * class by calling {@link android.content.Context#getSystemService(java.lang.String)}
34 * with argument {@link android.content.Context#KEYGUARD_SERVICE}. The
35 * actual class to control the keyboard locking is
36 * {@link android.app.KeyguardManager.KeyguardLock}.
37 */
38public class KeyguardManager {
39    private IWindowManager mWM;
40    private ITrustManager mTrustManager;
41
42    /**
43     * Intent used to prompt user for device credentials.
44     * @hide
45     */
46    public static final String ACTION_CONFIRM_DEVICE_CREDENTIAL =
47            "android.app.action.CONFIRM_DEVICE_CREDENTIAL";
48
49    /**
50     * A CharSequence dialog title to show to the user when used with a
51     * {@link #ACTION_CONFIRM_DEVICE_CREDENTIAL}.
52     * @hide
53     */
54    public static final String EXTRA_TITLE = "android.app.extra.TITLE";
55
56    /**
57     * A CharSequence description to show to the user when used with
58     * {@link #ACTION_CONFIRM_DEVICE_CREDENTIAL}.
59     * @hide
60     */
61    public static final String EXTRA_DESCRIPTION = "android.app.extra.DESCRIPTION";
62
63    /**
64     * Get an intent to prompt the user to confirm credentials (pin, pattern or password)
65     * for the current user of the device. The caller is expected to launch this activity using
66     * {@link android.app.Activity#startActivityForResult(Intent, int)} and check for
67     * {@link android.app.Activity#RESULT_OK} if the user successfully completes the challenge.
68     *
69     * @return the intent for launching the activity or null if no password is required.
70     **/
71    public Intent createConfirmDeviceCredentialIntent(CharSequence title, CharSequence description) {
72        if (!isKeyguardSecure()) return null;
73        Intent intent = new Intent(ACTION_CONFIRM_DEVICE_CREDENTIAL);
74        intent.putExtra(EXTRA_TITLE, title);
75        intent.putExtra(EXTRA_DESCRIPTION, description);
76        // For security reasons, only allow this to come from system settings.
77        intent.setPackage("com.android.settings");
78        return intent;
79    }
80
81    /**
82     * @deprecated Use {@link android.view.WindowManager.LayoutParams#FLAG_DISMISS_KEYGUARD}
83     * and/or {@link android.view.WindowManager.LayoutParams#FLAG_SHOW_WHEN_LOCKED}
84     * instead; this allows you to seamlessly hide the keyguard as your application
85     * moves in and out of the foreground and does not require that any special
86     * permissions be requested.
87     *
88     * Handle returned by {@link KeyguardManager#newKeyguardLock} that allows
89     * you to disable / reenable the keyguard.
90     */
91    public class KeyguardLock {
92        private final IBinder mToken = new Binder();
93        private final String mTag;
94
95        KeyguardLock(String tag) {
96            mTag = tag;
97        }
98
99        /**
100         * Disable the keyguard from showing.  If the keyguard is currently
101         * showing, hide it.  The keyguard will be prevented from showing again
102         * until {@link #reenableKeyguard()} is called.
103         *
104         * A good place to call this is from {@link android.app.Activity#onResume()}
105         *
106         * Note: This call has no effect while any {@link android.app.admin.DevicePolicyManager}
107         * is enabled that requires a password.
108         *
109         * <p>This method requires the caller to hold the permission
110         * {@link android.Manifest.permission#DISABLE_KEYGUARD}.
111         *
112         * @see #reenableKeyguard()
113         */
114        public void disableKeyguard() {
115            try {
116                mWM.disableKeyguard(mToken, mTag);
117            } catch (RemoteException ex) {
118            }
119        }
120
121        /**
122         * Reenable the keyguard.  The keyguard will reappear if the previous
123         * call to {@link #disableKeyguard()} caused it to be hidden.
124         *
125         * A good place to call this is from {@link android.app.Activity#onPause()}
126         *
127         * Note: This call has no effect while any {@link android.app.admin.DevicePolicyManager}
128         * is enabled that requires a password.
129         *
130         * <p>This method requires the caller to hold the permission
131         * {@link android.Manifest.permission#DISABLE_KEYGUARD}.
132         *
133         * @see #disableKeyguard()
134         */
135        public void reenableKeyguard() {
136            try {
137                mWM.reenableKeyguard(mToken);
138            } catch (RemoteException ex) {
139            }
140        }
141    }
142
143    /**
144     * Callback passed to {@link KeyguardManager#exitKeyguardSecurely} to notify
145     * caller of result.
146     */
147    public interface OnKeyguardExitResult {
148
149        /**
150         * @param success True if the user was able to authenticate, false if
151         *   not.
152         */
153        void onKeyguardExitResult(boolean success);
154    }
155
156
157    KeyguardManager() {
158        mWM = WindowManagerGlobal.getWindowManagerService();
159        mTrustManager = ITrustManager.Stub.asInterface(
160                ServiceManager.getService(Context.TRUST_SERVICE));
161    }
162
163    /**
164     * @deprecated Use {@link android.view.WindowManager.LayoutParams#FLAG_DISMISS_KEYGUARD}
165     * and/or {@link android.view.WindowManager.LayoutParams#FLAG_SHOW_WHEN_LOCKED}
166     * instead; this allows you to seamlessly hide the keyguard as your application
167     * moves in and out of the foreground and does not require that any special
168     * permissions be requested.
169     *
170     * Enables you to lock or unlock the keyboard. Get an instance of this class by
171     * calling {@link android.content.Context#getSystemService(java.lang.String) Context.getSystemService()}.
172     * This class is wrapped by {@link android.app.KeyguardManager KeyguardManager}.
173     * @param tag A tag that informally identifies who you are (for debugging who
174     *   is disabling he keyguard).
175     *
176     * @return A {@link KeyguardLock} handle to use to disable and reenable the
177     *   keyguard.
178     */
179    @Deprecated
180    public KeyguardLock newKeyguardLock(String tag) {
181        return new KeyguardLock(tag);
182    }
183
184    /**
185     * Return whether the keyguard is currently locked.
186     *
187     * @return true if keyguard is locked.
188     */
189    public boolean isKeyguardLocked() {
190        try {
191            return mWM.isKeyguardLocked();
192        } catch (RemoteException ex) {
193            return false;
194        }
195    }
196
197    /**
198     * Return whether the keyguard requires a password to unlock.
199     *
200     * @return true if keyguard is secure.
201     */
202    public boolean isKeyguardSecure() {
203        try {
204            return mWM.isKeyguardSecure();
205        } catch (RemoteException ex) {
206            return false;
207        }
208    }
209
210    /**
211     * If keyguard screen is showing or in restricted key input mode (i.e. in
212     * keyguard password emergency screen). When in such mode, certain keys,
213     * such as the Home key and the right soft keys, don't work.
214     *
215     * @return true if in keyguard restricted input mode.
216     *
217     * @see android.view.WindowManagerPolicy#inKeyguardRestrictedKeyInputMode
218     */
219    public boolean inKeyguardRestrictedInputMode() {
220        try {
221            return mWM.inKeyguardRestrictedInputMode();
222        } catch (RemoteException ex) {
223            return false;
224        }
225    }
226
227    /**
228     * Returns whether the device is currently locked and requires a PIN, pattern or
229     * password to unlock.
230     *
231     * @return true if unlocking the device currently requires a PIN, pattern or
232     * password.
233     */
234    public boolean isDeviceLocked() {
235        return isDeviceLocked(UserHandle.getCallingUserId());
236    }
237
238    /**
239     * Returns whether the device is currently locked and requires a PIN, pattern or
240     * password to unlock.
241     *
242     * @param userId the user for which the locked state should be reported.
243     * @return true if unlocking the device currently requires a PIN, pattern or
244     * password.
245     * @hide
246     */
247    public boolean isDeviceLocked(int userId) {
248        try {
249            return mTrustManager.isDeviceLocked(userId);
250        } catch (RemoteException e) {
251            return false;
252        }
253    }
254
255    /**
256     * @deprecated Use {@link android.view.WindowManager.LayoutParams#FLAG_DISMISS_KEYGUARD}
257     * and/or {@link android.view.WindowManager.LayoutParams#FLAG_SHOW_WHEN_LOCKED}
258     * instead; this allows you to seamlessly hide the keyguard as your application
259     * moves in and out of the foreground and does not require that any special
260     * permissions be requested.
261     *
262     * Exit the keyguard securely.  The use case for this api is that, after
263     * disabling the keyguard, your app, which was granted permission to
264     * disable the keyguard and show a limited amount of information deemed
265     * safe without the user getting past the keyguard, needs to navigate to
266     * something that is not safe to view without getting past the keyguard.
267     *
268     * This will, if the keyguard is secure, bring up the unlock screen of
269     * the keyguard.
270     *
271     * <p>This method requires the caller to hold the permission
272     * {@link android.Manifest.permission#DISABLE_KEYGUARD}.
273     *
274     * @param callback Let's you know whether the operation was succesful and
275     *   it is safe to launch anything that would normally be considered safe
276     *   once the user has gotten past the keyguard.
277     */
278    @Deprecated
279    public void exitKeyguardSecurely(final OnKeyguardExitResult callback) {
280        try {
281            mWM.exitKeyguardSecurely(new IOnKeyguardExitResult.Stub() {
282                public void onKeyguardExitResult(boolean success) throws RemoteException {
283                    if (callback != null) {
284                        callback.onKeyguardExitResult(success);
285                    }
286                }
287            });
288        } catch (RemoteException e) {
289
290        }
291    }
292}
293