KeyguardManager.java revision 54b6cfa9a9e5b861a9930af873580d6dc20f773c
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     * Handle returned by {@link KeyguardManager#newKeyguardLock} that allows
39     * you to disable / reenable the keyguard.
40     */
41    public class KeyguardLock {
42        private IBinder mToken = new Binder();
43        private String mTag;
44
45        KeyguardLock(String tag) {
46            mTag = tag;
47        }
48
49        /**
50         * Disable the keyguard from showing.  If the keyguard is currently
51         * showing, hide it.  The keyguard will be prevented from showing again
52         * until {@link #reenableKeyguard()} is called.
53         *
54         * A good place to call this is from {@link android.app.Activity#onResume()}
55         *
56         * @see #reenableKeyguard()
57         */
58        public void disableKeyguard() {
59            try {
60                mWM.disableKeyguard(mToken, mTag);
61            } catch (RemoteException ex) {
62            }
63        }
64
65        /**
66         * Reenable the keyguard.  The keyguard will reappear if the previous
67         * call to {@link #disableKeyguard()} caused it it to be hidden.
68         *
69         * A good place to call this is from {@link android.app.Activity#onPause()}
70         *
71         * @see #disableKeyguard()
72         */
73        public void reenableKeyguard() {
74            try {
75                mWM.reenableKeyguard(mToken);
76            } catch (RemoteException ex) {
77            }
78        }
79    }
80
81    /**
82     * Callback passed to {@link KeyguardManager#exitKeyguardSecurely} to notify
83     * caller of result.
84     */
85    public interface OnKeyguardExitResult {
86
87        /**
88         * @param success True if the user was able to authenticate, false if
89         *   not.
90         */
91        void onKeyguardExitResult(boolean success);
92    }
93
94
95    KeyguardManager() {
96        mWM = IWindowManager.Stub.asInterface(ServiceManager.getService(Context.WINDOW_SERVICE));
97    }
98
99    /**
100     * Enables you to lock or unlock the keyboard. Get an instance of this class by
101     * calling {@link android.content.Context#getSystemService(java.lang.String) Context.getSystemService()}.
102     * This class is wrapped by {@link android.app.KeyguardManager KeyguardManager}.
103     * @param tag A tag that informally identifies who you are (for debugging who
104     *   is disabling he keyguard).
105     *
106     * @return A {@link KeyguardLock} handle to use to disable and reenable the
107     *   keyguard.
108     */
109    public KeyguardLock newKeyguardLock(String tag) {
110        return new KeyguardLock(tag);
111    }
112
113    /**
114     * If keyguard screen is showing or in restricted key input mode (i.e. in
115     * keyguard password emergency screen). When in such mode, certain keys,
116     * such as the Home key and the right soft keys, don't work.
117     *
118     * @return true if in keyguard restricted input mode.
119     *
120     * @see android.view.WindowManagerPolicy#inKeyguardRestrictedKeyInputMode
121     */
122    public boolean inKeyguardRestrictedInputMode() {
123        try {
124            return mWM.inKeyguardRestrictedInputMode();
125        } catch (RemoteException ex) {
126            return false;
127        }
128    }
129
130    /**
131     * Exit the keyguard securely.  The use case for this api is that, after
132     * disabling the keyguard, your app, which was granted permission to
133     * disable the keyguard and show a limited amount of information deemed
134     * safe without the user getting past the keyguard, needs to navigate to
135     * something that is not safe to view without getting past the keyguard.
136     *
137     * This will, if the keyguard is secure, bring up the unlock screen of
138     * the keyguard.
139     *
140     * @param callback Let's you know whether the operation was succesful and
141     *   it is safe to launch anything that would normally be considered safe
142     *   once the user has gotten past the keyguard.
143     */
144    public void exitKeyguardSecurely(final OnKeyguardExitResult callback) {
145        try {
146            mWM.exitKeyguardSecurely(new IOnKeyguardExitResult.Stub() {
147                public void onKeyguardExitResult(boolean success) throws RemoteException {
148                    callback.onKeyguardExitResult(success);
149                }
150            });
151        } catch (RemoteException e) {
152
153        }
154    }
155}
156