journeyapps / zxing-android-embedded

Barcode scanner library for Android, based on the ZXing decoder
https://journeyapps.com/
Apache License 2.0
5.68k stars 1.26k forks source link

[Android 11+] StrictMode raising an IllegalAccessException due to usage of non-visual context #688

Open joaolucas95 opened 2 years ago

joaolucas95 commented 2 years ago

Description of the problem:

In Android 11 or above devices, StrictMode is raising an IllegalAccessException due to the new restrictions of non-visual Context usages when handling UI.

This is happening when calling BarcodeView#resume() and it is caused due to usage of application content to get the WindowManager in RotationListener#listen() method.

Which library version are you using? 4.1.0

Which phone/tablet are you using, and which Android version does it run? (e.g. Samsung Galaxy S5, Android 5.0) Pixel 2, Android 12

Does the same happen on other devices or an emulator? It happens on all Android 11 or above devices

In the case of an error do you have a stack trace or adb logs? 2022-01-10 16:31:54.646 15047-15047/------ E/ContextImpl: Tried to access visual service WindowManager from a non-visual Context:--------@1350853 WindowManager should be accessed from Activity or other visual Context. Use an Activity or a Context created with Context#createWindowContext(int, Bundle), which are adjusted to the configuration and visual bounds of an area on screen. java.lang.IllegalAccessException: Tried to access visual service WindowManager from a non-visual Context:-----Application@1350853 at android.app.ContextImpl.getSystemService(ContextImpl.java:2059) at android.content.ContextWrapper.getSystemService(ContextWrapper.java:857) at com.journeyapps.barcodescanner.RotationListener.listen(RotationListener.java:36) at com.journeyapps.barcodescanner.CameraPreview.resume(CameraPreview.java:633)