Determining the type of security (lock) settings on an Android device in Xamarin
Feb 16, 2015
If you’re writing an Android application and interacts with sensitive company or user information, you may want to restrict offline data and/or credential storage to devices that meet certain security criteria such as lock screen password settings. In enterprise settings, this is often addressed by distributing software via a mobile device management (MDM) solution, many of which perform security checks and enforce settings at the device level. If that’s not an option in your case, you may want to run these checks yourself and drive the app behavior from that. At first glance, there isn’t an obvious way to do this but by using Reflection on the Java.Lang namespace, you can see the screen lock level is indeed available as an integer value. Below is an example of how to implement a check for the security screen lock type on an Android device using C# and Xamarin.Android.
public static LockTypeLevel GetCurrentLockLevel(Context context)
{
string lockUtilsClassName = "com.android.internal.widget.LockPatternUtils";
Class lockUtilsClass = Class.ForName(lockUtilsClassName);
Object lockUtils = ((Class)lockUtilsClass).GetConstructor(Class.FromType(typeof(Context))).NewInstance(context);
Method getPasswordQuality = lockUtilsClass.GetMethod("getActivePasswordQuality");
return (LockTypeLevel)(int)getPasswordQuality.Invoke(lockUtils);
}
The value returned from this function is an enum
representation of an int
.
public enum LockTypeLevel
{
SliderOrNone = 0,
LowLevelBiometricSecurity = 32768, //implies technologies that can recognize the identity of an individual to about a 3 digit PIN (i.e. face)
PatternPassword = 65536, //any type of password is assigned on the device (i.e. pattern)
NumericPasswordBasic = 131072, //numeric password is assigned to the device
NumericPasswordAdvanced = 196608, //numeric password with no repeating (4444) or ordered (1234,4321,2468,etc) sequences
AlphabeticPassword = 262144, //alphabetic password
AlphanumericPassword = 327680, //alphabetic and numeric password
ComplexPassword = 393216 //alpabetic, numeric, and special character password
}
As the numeric value returned from the above function increases, so too does the strength of the security on the device.