Last active
August 29, 2015 14:10
-
-
Save benbahrenburg/d4efb4fc6a2de91b1ba8 to your computer and use it in GitHub Desktop.
Check if passcode enabled
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-(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