Using OSK classes DevicePolicyManager OS 2.2 on Android that support OS 2.1 devices

I want to use some DevicePolicyManager methods in my application. DevicePolicyManager was introduced in OS 2.2, but my application should continue to run on OS 2.1 devices.

Here's the pseudo code for what I want to do:

if (needSecurity) { if (runningOS2.2orGreater) { // Invoke the required security policy, eg setPasswordQuality(myComponentName, PASSWORD_QUALITY_NUMERIC) } else { // Tell the user they can't use this feature } } 

From reading the docs, I think I might also need DeviceAdminReceiver to handle the onPasswordFailed and onPasswordSucceeded callbacks.

Of the other questions related to Stackoverflow (e.g. here ), I believe that I have two options:

1. Reflection

Continue building against the OS 2.1 SDK and use reflection to call classes at runtime, for example.

 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 DeviceAdminReceiver, will the reflection work? How do I handle callbacks in DeviceAdminReceiver and migrate to my own application classes?

2. Loading a conditional class

Edit to create against OS 2.2 SDK. At run time, only OS 2.2 classes are loaded if the current device version is OS 2.2 or later, for example.

 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 create simpler code for support and, apparently, will work with DeviceAdminReceiver as well. Does anyone know of any flaws or complications?

So my questions are:

  • Would you recommend reflection or conditional class loading for using DevicePolicyManager?
  • I need a DeviceAdminReceiver, or I can determine if the user has a suitable password, for example. repeating isActivePasswordSufficient in your application to confirm that it was done?
  • Any other tips, if you have one (for example, this question , suggest that there may be problems causing the user to reset their password).

Thanks!

+1
source share
1 answer

If I need to implement DeviceAdminReceiver, will the reflection work?

Not really. You will need to use conditional class loading, which means you can just go this route to start with.

Does anyone know any flaws or complications?

Speaking on behalf of my little corner “nobody”, I do not know any shortcomings. I would use the VERSION_CODES constants on Build , and not the integer ( 7 ). And, if you do not support 1.5, you can use the SDK_INT in Build , not the SDK .

Could you recommend reflection or conditional class loading for using DevicePolicyManager?

Loading a conditional class.

I need a DeviceAdminReceiver, or I can determine if the user has a suitable password, for example. by calling isActivePassword repeatedly Do you have enough in my application to confirm that it was done?

I cant answer. If you do not get another answer to this question, you can ask it in your own SO question.

Other tips if you have

Never engage in a land war in Asia.

+3
source

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


All Articles