Skip to content

Instantly share code, notes, and snippets.

@benbahrenburg
Last active August 29, 2015 14:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save benbahrenburg/d4efb4fc6a2de91b1ba8 to your computer and use it in GitHub Desktop.
Save benbahrenburg/d4efb4fc6a2de91b1ba8 to your computer and use it in GitHub Desktop.
Check if passcode enabled
private boolean deviceHasPasscodeEnabled()
{
String LOCKSCREEN_UTILS = "com.android.internal.widget.LockPatternUtils";
try
{
Class<?> lockUtilsClass = Class.forName(LOCKSCREEN_UTILS);
// "this" is a Context, in my case an Activity
Object lockUtils = lockUtilsClass.getConstructor(Context.class).newInstance(this);
Method method = lockUtilsClass.getMethod("getActivePasswordQuality");
int lockProtectionLevel = Integer.valueOf(String.valueOf(method.invoke(lockUtils)));
if(lockProtectionLevel >= DevicePolicyManager.PASSWORD_QUALITY_NUMERIC)
{
return true;
}
}
catch (Exception e)
{
Log.e("reflectInternalUtils", "ex:"+e);
}
return false;
}
-(BOOL) deviceHasPasscodeEnabled {
NSData* secret = [@"Device has passcode set?" dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *attributes = @{ (__bridge id)kSecClass: (__bridge id)kSecClassGenericPassword, (__bridge id)kSecAttrService: @"LocalDeviceServices", (__bridge id)kSecAttrAccount: @"NoAccount", (__bridge id)kSecValueData: secret, (__bridge id)kSecAttrAccessible: (__bridge id)kSecAttrAccessibleWhenPasscodeSetThisDeviceOnly };
OSStatus status = SecItemAdd((__bridge CFDictionaryRef)attributes, NULL);
if (status == errSecSuccess) { // item added okay, passcode has been set
SecItemDelete((__bridge CFDictionaryRef)attributes);
return true;
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment