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