2

I'm trying to determine if a password, pin number or pattern is required when logging onto your android smart phone. I can determine if a phone uses a pattern at log on, and I can determine if a password/pin number is used at log on, but how can I tell when a phone uses only passwords and only pin numbers at log on?

The code below is what I have so far:

        //determine if phone uses pattern, pin or password at log on

        //lockPatternEnable returns 1 if pattern lock enabled and 0 if pin/password password enabled
        ContentResolver cr = getBaseContext().getContentResolver();
        lockPatternEnable = Settings.Secure.getInt(cr, Settings.Secure.LOCK_PATTERN_ENABLED, 0);

        //returns 1 if pin/password used. 0 if not
        KeyguardManager keyguardManager = (KeyguardManager) getBaseContext().getSystemService(Context.KEYGUARD_SERVICE);
        if( keyguardManager.isKeyguardSecure()) 
        {
           //it is pin or password protected
           pinPasswordEnable=1;
        } 
        else 
        {
           //it is not pin or password protected 
            pinPasswordEnable=0;
        }//http://stackoverflow.com/questions/6588969/device-password-in-android-is-existing-or-not/18716253#18716253

        if(pinPasswordEnable==0 && lockPatternEnable==1)
        {
            //pattern 
        }
        else if(pinPasswordEnable==1 && lockPatternEnable==0)
        {
            //pin or password
        }
pHorseSpec
  • 1,246
  • 5
  • 19
  • 48
  • Please refer to this anser http://stackoverflow.com/questions/42394996/for-api-16-how-to-detect-if-pin-password-pattern-is-required-to-unlock-phone – vicco Feb 22 '17 at 20:30

1 Answers1

0

Use this class as and just call isDeviceSecured() method.

public class SecurityUtils {
    public static boolean isDeviceSecured(Context context){
        KeyguardManager keyguardManager = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            return keyguardManager.isDeviceSecure();
        }else{
            return (isPatternEnabled(context) || isPassOrPinEnabled(keyguardManager));
        }
    }

    private static boolean isPatternEnabled(Context context){
        return (Settings.System.getInt(context.getContentResolver(), Settings.System.LOCK_PATTERN_ENABLED, 0) == 1);
    }

    private static boolean isPassOrPinEnabled(KeyguardManager keyguardManager){
        return keyguardManager.isKeyguardSecure();
    }
}
vicco
  • 181
  • 2
  • 7