1

I want to check whether the device screen lock is enabled or not and I am using the standard API provided by Android SDK as shown below. But I am receiving

D/StrictMode: StrictMode policy violation; ~duration=127 ms: android.os.StrictMode$StrictModeDiskReadViolation: policy=4259847 violation=2
        at android.os.StrictMode$AndroidBlockGuardPolicy.onReadFromDisk(StrictMode.java:1449)

Here is the code I tried.

val km = requireContext().getSystemService(Context.KEYGUARD_SERVICE) as KeyguardManager?
    return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        //Returns whether the device is secured with a PIN, pattern or password.
        km?.isDeviceSecure ?: false
    } else {

        //Return whether the keyguard is secured by a PIN, pattern or password or a SIM card is currently locked.
        km?.isKeyguardSecure ?: false
    }

I set up strict mode as shown below on my Application class

 private fun enableStrictMode() {
        StrictMode.setThreadPolicy(
            Builder()
                .detectDiskReads()
                .detectDiskWrites()
                .detectNetwork()
                .penaltyLog()
                .build()
        )
    }

As this is a standard API provided by the Android SDK, why this happen? Anyway, this is just the case of Strictmode and if I disable the strict mode, there will not be any warning on the LogCat.

Am I doing something wrong?

MurugananthamS
  • 2,395
  • 4
  • 20
  • 49
Code Wizard
  • 319
  • 3
  • 8
  • "why this happen?" -- Google rarely tests their own code with `StrictMode`, which is unfortunate. That being said, you could more your check of the keyguard to a background thread. – CommonsWare Nov 07 '19 at 12:10
  • @CommonsWare That's unfortunate. I moved the code to a background thread and it seems fine. I was trying to implement new Biometric AndroidX lib by Google and i found Strict mode violations in that API too. Thank you so much for your quick response. – Code Wizard Nov 08 '19 at 05:49

0 Answers0