How to detect if PIN/password/pattern is required to unlock phone?
Asked Answered
R

3

19

How can I detect if the phone is locked by a password, pin or pattern?

thank you!

Ruffo answered 24/10, 2011 at 17:1 Comment(0)
E
20

Two methods

  1. Check programatically - API 16+

https://gist.github.com/doridori/54c32c66ef4f4e34300f

Note that you dont need to check for face unlock as that requires that a pin/pass fallback is set.

  1. Device Admin Policies

Can also look into the Device Admin Policies which allow restrictions on how the app is setup regarding security including pin/pass set restrictions

As an aside, these are the lock types you want to check for if using an encrypted Keystore. Check here for more info.

Equivoque answered 6/1, 2015 at 14:49 Comment(4)
Seems like you can check for all three lock types through the Keyguard now "LOCK_PATTERN_ENABLED - This constant was deprecated in API level 23. Use KeyguardManager to determine the state and security level of the keyguard. Accessing this setting from an app that is targeting M or later throws a SecurityException." developer.android.com/reference/android/provider/…Religionism
Your right. KeyGuardManager.isKeyGuardSecure() now returns "true if a PIN, pattern or password is set or a SIM card is locked." since 23Equivoque
@Don Do you mean that prior to 23 isKeyguardSecure did not return true when the device was locked with a pattern ? That's not what the documentation says. And it would be a pretty dumb thing to do. It would appear that isKeyguardSecure always returned true for PIN, pattern or password, since API 16 when it was introduced. I did test it on 19 and this is what it does.Costate
@Don So your github code seems too complex for API 16+, isPatternSet is redundant. I gave an answer with a simpler version.Costate
A
5

You can use the Settings.Secure class to query information about the security in place on an android device. For example, to see if the user has a lock pattern enabled you'd do:

ContentResolver cr = getContentResolver();
int lockPatternEnable = 
  Settings.Secure.getInt(cr, Settings.Secure.LOCK_PATTERN_ENABLED);

lockPatternEnable would then have a 0 if it wasn't enabled and a 1 if it was enabled.

Affiant answered 24/10, 2011 at 17:6 Comment(6)
Won't that only give you if lock pattern enabled but not if Pin enabled. I have both Pin and Pattern on my phone, and its actually 2 different states.Imitative
Hmmm. I'll have to do some more research. For now it looks like we only know how to check if the Lock patter is enabled.Affiant
but i am getting exception saying 12-26 17:25:39.176: android.provider.Settings$SettingNotFoundException: lock_pattern_autolock..may i know how to resolve itHalakah
KeyguardManager.isKeyguardSecure() will be true if a pin or password is setEquivoque
lockPatternEnable is giving out 0 even when its set in Moto G5 Plus !! Any ideas how this can be resolved ?Mercerize
It looks like this was depreciated in api 23Mercerize
C
5

This should be OK for Android API 16 and up, according to the documentation. I tested it on 19 and it seems to work.

private boolean IsDeviceSecured () {
    KeyguardManager keyguardManager =
            (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE); //api 16+
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        return keyguardManager.isDeviceSecure();
    }
    return keyguardManager.isKeyguardSecure ();
}
Costate answered 29/1, 2019 at 3:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.