Android checks if the lock screen is installed

I need to check if there is a lockscreen Pin or something more secure (password, fingerprint, etc.). I can check if there is a Pin, Password or pattern.

KeyguardManager keyguardManager = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
    return keyguardManager.isKeyguardSecure();

My problem is that I cannot determine if the lockscreen is a template or something below. I tried this:

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

but it is outdated and gives me an error. I also tried this:

            long mode2 = Settings.Secure.getLong(contentResolver, "lockscreen.password_type");

but it also means SecurityException.

Is there a way to determine if the lock screen has a pin (or higher), or does it have a lock pattern or something lower? KeyguardManager is not useful to me this way: /

Any help is appreciated! Thank!

/ change

Error for the first:

               Caused by: java.lang.SecurityException: Settings.Secure.lock_pattern_autolock is deprecated and no longer accessible. See API documentation for potential replacements.

Exception for the second: W / System.err: android.provider.Settings $ SettingNotFoundException: lockscreen.password_type

, Marshmallow (https://developer.android.com/reference/android/provider/Settings.Secure.html)

+4
3

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

public boolean isDeviceScreenLocked() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        return isDeviceLocked();
    } else {
        return isPatternSet() || isPassOrPinSet();
    }
}

/**
 * @return true if pattern set, false if not (or if an issue when checking)
 */
private boolean isPatternSet() {
    ContentResolver cr = context.getContentResolver();
    try {
        int lockPatternEnable = Settings.Secure.getInt(cr, Settings.Secure.LOCK_PATTERN_ENABLED);
        return lockPatternEnable == 1;
    } catch (Settings.SettingNotFoundException e) {
        return false;
    }
}

/**
 * @return true if pass or pin set
 */
private boolean isPassOrPinSet() {
    KeyguardManager keyguardManager = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE); //api 16+
    return keyguardManager.isKeyguardSecure();
}

/**
 * @return true if pass or pin or pattern locks screen
 */
@TargetApi(23)
private boolean isDeviceLocked() {
    KeyguardManager keyguardManager = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE); //api 23+
    return keyguardManager.isDeviceSecure();
}
+6

pre post marshmallow, . SDK-.

0

My code only works for Android 16 and higher, so Greg's code is more complete; but mine is much more compact, and in 2019, when I write this, there are very few devices that use the OS version up to 16. Therefore, some people may find it more useful in this way. 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 ();
}
0
source

Source: https://habr.com/ru/post/1659810/


All Articles