facebook / fresco

An Android library for managing images and the memory they use.
https://frescolib.org/
MIT License
17.07k stars 3.75k forks source link

Android 11 IncorrectContextUseViolation in GestureDetector by StrictMode #2611

Open obsantos opened 3 years ago

obsantos commented 3 years ago

Description

When using targetSdkVersion 30 to target Android 11 apis, StrictMode is complaining of a non-visual Context usage (in this case Application Context) when trying to access UI constants.

com.example E/ViewConfiguration: Tried to access UI constants from a non-visual Context:com.example.App@a3dee28UI constants, such as display metrics or window metrics, must 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.IllegalArgumentException: Tried to access UI constants from a non-visual Context:com.example.App@a3dee28
        at android.view.ViewConfiguration.get(ViewConfiguration.java:510)
        at com.facebook.drawee.gestures.GestureDetector.<init>(GestureDetector.java:47)
        at com.facebook.drawee.gestures.GestureDetector.newInstance(GestureDetector.java:54)
        at com.facebook.drawee.controller.AbstractDraweeControllerBuilder.maybeBuildAndSetGestureDetector(AbstractDraweeControllerBuilder.java:467)
        at com.facebook.drawee.controller.AbstractDraweeControllerBuilder.maybeBuildAndSetRetryManager(AbstractDraweeControllerBuilder.java:460)
        at com.facebook.drawee.controller.AbstractDraweeControllerBuilder.buildController(AbstractDraweeControllerBuilder.java:339)
        at com.facebook.drawee.controller.AbstractDraweeControllerBuilder.build(AbstractDraweeControllerBuilder.java:314)

Problem seems to be related of using the application context as seen here:

    // we should always use the application context to avoid memory leaks
    context = context.getApplicationContext();
    if (imagePipelineConfig == null) {
      ImagePipelineFactory.initialize(context);
    } else {
      ImagePipelineFactory.initialize(imagePipelineConfig);
    }

StrictMode source code for this warning can be found here

Reproduction

Any usage of Fresco.newDraweeControllerBuilder() as it uses the same Context used to initialize Fresco (app context). But potentially any use of Fresco can lead to this StrictMode warning

Additional Information

stale[bot] commented 2 years ago

Hey there, it looks like there has been no activity on this issue recently. Has the issue been fixed, or does it still require the community's attention? This issue may be closed if no further activity occurs. You may also label this issue as "bug" or "enhancement" and I will leave it open. Thank you for your contributions.

obsantos commented 2 years ago

Got this email form stale bot

Hey there, it looks like there has been no activity on this issue recently. Has the issue been fixed, or does it still require the community's attention? This issue may be closed if no further activity occurs. You may also label this issue as "bug" or "enhancement" and I will leave it open. Thank you for your contributions.

but i'm not allowed to add labels to this issue 🤷‍♂️