KeyguardManager.java revision bde3d18dd5748bd24e527c653504346ad012c2dc
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.Intent; 20import android.os.Binder; 21import android.os.RemoteException; 22import android.os.IBinder; 23import android.view.IWindowManager; 24import android.view.IOnKeyguardExitResult; 25import android.view.WindowManagerGlobal; 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 * Intent used to prompt user for device credentials. 39 * @hide 40 */ 41 public static final String ACTION_CONFIRM_DEVICE_CREDENTIAL = 42 "android.app.action.CONFIRM_DEVICE_CREDENTIAL"; 43 44 /** 45 * A CharSequence dialog title to show to the user when used with a 46 * {@link #ACTION_CONFIRM_DEVICE_CREDENTIAL}. 47 * @hide 48 */ 49 public static final String EXTRA_TITLE = "android.app.extra.TITLE"; 50 51 /** 52 * A CharSequence description to show to the user when used with 53 * {@link #ACTION_CONFIRM_DEVICE_CREDENTIAL}. 54 * @hide 55 */ 56 public static final String EXTRA_DESCRIPTION = "android.app.extra.DESCRIPTION"; 57 58 /** 59 * @removed 60 */ 61 public Intent getConfirmDeviceCredentialIntent(CharSequence title, CharSequence description) { 62 return createConfirmDeviceCredentialIntent(title, description); 63 } 64 65 /** 66 * Get an intent to prompt the user to confirm credentials (pin, pattern or password) 67 * for the current user of the device. The caller is expected to launch this activity using 68 * {@link android.app.Activity#startActivityForResult(Intent, int)} and check for 69 * {@link android.app.Activity#RESULT_OK} if the user successfully completes the challenge. 70 * 71 * @return the intent for launching the activity or null if no password is required. 72 **/ 73 public Intent createConfirmDeviceCredentialIntent(CharSequence title, CharSequence description) { 74 if (!isKeyguardSecure()) return null; 75 Intent intent = new Intent(ACTION_CONFIRM_DEVICE_CREDENTIAL); 76 intent.putExtra(EXTRA_TITLE, title); 77 intent.putExtra(EXTRA_DESCRIPTION, description); 78 // For security reasons, only allow this to come from system settings. 79 intent.setPackage("com.android.settings"); 80 return intent; 81 } 82 83 /** 84 * @deprecated Use {@link android.view.WindowManager.LayoutParams#FLAG_DISMISS_KEYGUARD} 85 * and/or {@link android.view.WindowManager.LayoutParams#FLAG_SHOW_WHEN_LOCKED} 86 * instead; this allows you to seamlessly hide the keyguard as your application 87 * moves in and out of the foreground and does not require that any special 88 * permissions be requested. 89 * 90 * Handle returned by {@link KeyguardManager#newKeyguardLock} that allows 91 * you to disable / reenable the keyguard. 92 */ 93 public class KeyguardLock { 94 private final IBinder mToken = new Binder(); 95 private final String mTag; 96 97 KeyguardLock(String tag) { 98 mTag = tag; 99 } 100 101 /** 102 * Disable the keyguard from showing. If the keyguard is currently 103 * showing, hide it. The keyguard will be prevented from showing again 104 * until {@link #reenableKeyguard()} is called. 105 * 106 * A good place to call this is from {@link android.app.Activity#onResume()} 107 * 108 * Note: This call has no effect while any {@link android.app.admin.DevicePolicyManager} 109 * is enabled that requires a password. 110 * 111 * <p>This method requires the caller to hold the permission 112 * {@link android.Manifest.permission#DISABLE_KEYGUARD}. 113 * 114 * @see #reenableKeyguard() 115 */ 116 public void disableKeyguard() { 117 try { 118 mWM.disableKeyguard(mToken, mTag); 119 } catch (RemoteException ex) { 120 } 121 } 122 123 /** 124 * Reenable the keyguard. The keyguard will reappear if the previous 125 * call to {@link #disableKeyguard()} caused it to be hidden. 126 * 127 * A good place to call this is from {@link android.app.Activity#onPause()} 128 * 129 * Note: This call has no effect while any {@link android.app.admin.DevicePolicyManager} 130 * is enabled that requires a password. 131 * 132 * <p>This method requires the caller to hold the permission 133 * {@link android.Manifest.permission#DISABLE_KEYGUARD}. 134 * 135 * @see #disableKeyguard() 136 */ 137 public void reenableKeyguard() { 138 try { 139 mWM.reenableKeyguard(mToken); 140 } catch (RemoteException ex) { 141 } 142 } 143 } 144 145 /** 146 * Callback passed to {@link KeyguardManager#exitKeyguardSecurely} to notify 147 * caller of result. 148 */ 149 public interface OnKeyguardExitResult { 150 151 /** 152 * @param success True if the user was able to authenticate, false if 153 * not. 154 */ 155 void onKeyguardExitResult(boolean success); 156 } 157 158 159 KeyguardManager() { 160 mWM = WindowManagerGlobal.getWindowManagerService(); 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 * @deprecated Use {@link android.view.WindowManager.LayoutParams#FLAG_DISMISS_KEYGUARD} 229 * and/or {@link android.view.WindowManager.LayoutParams#FLAG_SHOW_WHEN_LOCKED} 230 * instead; this allows you to seamlessly hide the keyguard as your application 231 * moves in and out of the foreground and does not require that any special 232 * permissions be requested. 233 * 234 * Exit the keyguard securely. The use case for this api is that, after 235 * disabling the keyguard, your app, which was granted permission to 236 * disable the keyguard and show a limited amount of information deemed 237 * safe without the user getting past the keyguard, needs to navigate to 238 * something that is not safe to view without getting past the keyguard. 239 * 240 * This will, if the keyguard is secure, bring up the unlock screen of 241 * the keyguard. 242 * 243 * <p>This method requires the caller to hold the permission 244 * {@link android.Manifest.permission#DISABLE_KEYGUARD}. 245 * 246 * @param callback Let's you know whether the operation was succesful and 247 * it is safe to launch anything that would normally be considered safe 248 * once the user has gotten past the keyguard. 249 */ 250 @Deprecated 251 public void exitKeyguardSecurely(final OnKeyguardExitResult callback) { 252 try { 253 mWM.exitKeyguardSecurely(new IOnKeyguardExitResult.Stub() { 254 public void onKeyguardExitResult(boolean success) throws RemoteException { 255 if (callback != null) { 256 callback.onKeyguardExitResult(success); 257 } 258 } 259 }); 260 } catch (RemoteException e) { 261 262 } 263 } 264} 265