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