Skip to content

Instantly share code, notes, and snippets.

@doridori
Created July 3, 2015 16:40
Show Gist options
  • Save doridori/54c32c66ef4f4e34300f to your computer and use it in GitHub Desktop.
Save doridori/54c32c66ef4f4e34300f to your computer and use it in GitHub Desktop.
Check if a device lock is set on Android. Code example for http://stackoverflow.com/a/27801128/236743
/**
* <p>Checks to see if the lock screen is set up with either a PIN / PASS / PATTERN</p>
*
* <p>For Api 16+</p>
*
* @return true if PIN, PASS or PATTERN set, false otherwise.
*/
public static boolean doesDeviceHaveSecuritySetup(Context context)
{
return isPatternSet(context) || isPassOrPinSet(context);
}
/**
* @param context
* @return true if pattern set, false if not (or if an issue when checking)
*/
private static boolean isPatternSet(Context context)
{
ContentResolver cr = context.getContentResolver();
try
{
int lockPatternEnable = Settings.Secure.getInt(cr, Settings.Secure.LOCK_PATTERN_ENABLED);
return lockPatternEnable == 1;
}
catch (Settings.SettingNotFoundException e)
{
XLog.e(e.getMessage());
return false;
}
}
/**
* @param context
* @return true if pass or pin set
*/
private static boolean isPassOrPinSet(Context context)
{
KeyguardManager keyguardManager = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE); //api 16+
return keyguardManager.isKeyguardSecure();
}
@gregEstoke
Copy link

gregEstoke commented Sep 28, 2017

LOCK_PATTERN_ENABLED is deprecated from API 23. It throws a SecurityException. is there a workaround to check for a pattern or android doens't consider it as secure enough for device runing API 23 and above ?

update : I'm doing it this way : https://stackoverflow.com/a/46464312/1281350

@mak1nt0sh
Copy link

mak1nt0sh commented Dec 19, 2019

return keyguardManager.isKeyguardSecure(); return true even if SIM card is locked.
See: https://developer.android.com/reference/kotlin/android/app/KeyguardManager.html#iskeyguardsecure

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment