I want to use some DevicePolicyManager methods in my app. The DevicePolicyManager was introduced in OS 2.2, but my app must continue to run on OS 2.1 devices.
Here's pseudocode for what I want to do:
if (needSecurity)
{
if (runningOS2.2orGreater)
{
// Invoke the required security policy, e.g.
setPasswordQuality(myComponentName, PASSWORD_QUALITY_NUMERIC)
}
else
{
// Tell the user they can't use this feature
}
}
From reading the docs I think I may also need a DeviceAdminReceiver to handle onPasswordFailed and onPasswordSucceeded callbacks.
From other Stackoverflow questions (e.g. here), I believe I have two options:
1. Reflection
Continue to build against OS 2.1 SDK and use reflection to invoke classes at runtime, e.g.
Class myClass =
ClassLoader.getSystemClassLoader().loadClass("android.app.admin.DevicePolicyManager")
Object DPMInstance = myClass.newInstance();
Method myMethod = myClass.getMethod("setPasswordQuality",
new Class[] { ComponentName.class,
int.class });
myMethod.invoke(DPMInstance,
new Object[] { myComponentName,
PASSWORD_QUALITY_NUMERIC });
If I need to implement a DeviceAdminReceiver, will reflection work? How would I handle callbacks to DeviceAdminReceiver and call back into my own application classes?
2. Conditional class loading
Change to build against the OS 2.2 SDK. At runtime only load the OS 2.2 classes if the current device version is OS 2.2 or newer, e.g.
int sdk = new Integer(Build.VERSION.SDK).intValue();
if (sdk > 7)
{
sLog.info("OS 2.2 or later");
return new myClassImplementsDeviceAdminInterfaces();
}
else
{
sLog.info("OS 2.1 or earlier");
return new myClassDoesNotSupportDeviceAdmin();
}
This approach looks like it will produce easier code to support, and presumably will work with a DeviceAdminReceiver too. Is anybody aware of any drawbacks or complications to it?
So, my questions are:
- Would you recommend reflection or conditional class loading for using DevicePolicyManager?
- Will I need a DeviceAdminReceiver, or can I detect whether the user has a suitable password, e.g. by repeatedly calling isActivePasswordSufficient in my app to confirm it has been done?
- Any other tips if you have them (e.g. this question suggests there might be issues forcing the user to reset their password).
Thanks!