mapbox / mapbox/mapbox-maps-android

StrictModeViolation: IncorrectContextUseViolation

Open
#2,597 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

auto-triaged bug :beetle: ticketed
Dominant language
Kotlin
Stars
578
Forks
161
PR merge metrics
No merged PRs in 30d

Description

Environment

  • Android OS version: 35
  • Devices affected: Should be all
  • Maps SDK Version: 11.10.2

Observed behavior and steps to reproduce

  • Open a screen with MapBoxMap composable
  • Wait for a moment
  • With StrictMode enabled with detectIncorrectContextUse() and penaltyDeath(), app would crash with violation
StrictMode policy violation: android.os.strictmode.IncorrectContextUseViolation: 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. (Ask Gemini)
                                                                                                    	at android.os.StrictMode.onIncorrectContextUsed(StrictMode.java:2260)
                                                                                                    	at android.app.ContextImpl.getSystemService(ContextImpl.java:2116)
                                                                                                    	at android.content.ContextWrapper.getSystemService(ContextWrapper.java:900)
                                                                                                    	at com.mapbox.maps.module.telemetry.PhoneState.obtainDisplayDensity(PhoneState.kt:82)
                                                                                                    	at com.mapbox.maps.module.telemetry.PhoneState.<init>(PhoneState.kt:67)
                                                                                                    	at com.mapbox.maps.module.telemetry.MapTelemetryImpl$sendMapLoadEvent$1.invokeSuspend(MapTelemetryImpl.kt:112)
                                                                                                    	at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
                                                                                                    	at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:100)
                                                                                                    	at kotlinx.coroutines.internal.LimitedDispatcher$Worker.run(LimitedDispatcher.kt:113)
                                                                                                    	at kotlinx.coroutines.scheduling.TaskImpl.run(Tasks.kt:89)
                                                                                                    	at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:586)
                                                                                                    	at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:820)
                                                                                                    	at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:717)
                                                                                                    	at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:704)
                                                                                                    Caused by: java.lang.IllegalAccessException: Tried to access visual service WindowManager from a non-visual Context:com.privatetech.matrix.MainApplication@9b81b38
                                                                                                    	at android.app.ContextImpl.getSystemService(ContextImpl.java:2115)
                                                                                                    	at android.content.ContextWrapper.getSystemService(ContextWrapper.java:900) 
                                                                                                    	at com.mapbox.maps.module.telemetry.PhoneState.obtainDisplayDensity(PhoneState.kt:82) 
                                                                                                    	at com.mapbox.maps.module.telemetry.PhoneState.<init>(PhoneState.kt:67) 
                                                                                                    	at com.mapbox.maps.module.telemetry.MapTelemetryImpl$sendMapLoadEvent$1.invokeSuspend(MapTelemetryImpl.kt:112) 
                                                                                                    	at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33) 
                                                                                                    	at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:100) 
                                                                                                    	at kotlinx.coroutines.internal.LimitedDispatcher$Worker.run(LimitedDispatcher.kt:113) 
                                                                                                    	at kotlinx.coroutines.scheduling.TaskImpl.run(Tasks.kt:89) 
                                                                                                    	at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:586) 
                                                                                                    	at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:820) 
                                                                                                    	at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:717) 
                                                                                                    	at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:704) 

Expected behavior

There should not be a strict mode violation

Notes / preliminary analysis

https://github.com/mapbox/mapbox-maps-android/blob/d1008e41563e6d084cde236bdad7fcabd5d27aae/module-telemetry/src/main/java/com/mapbox/maps/module/telemetry/PhoneState.kt#L79-L85
uses WindowManager to extract the screen density.
But for telemetry, the PhoneState is derived from application context, which is not a visual service
https://github.com/mapbox/mapbox-maps-android/blob/d1008e41563e6d084cde236bdad7fcabd5d27aae/module-telemetry/src/main/java/com/mapbox/maps/module/telemetry/MapTelemetryImpl.kt#L166

Additional links and references

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with module-telemetry/src/main/java/com/mapbox/maps/module/telemetry/PhoneState.kt around lines 79-85 and MapTelemetryImpl.kt around line 166. Reproduce the MapBoxMap composable case with StrictMode detectIncorrectContextUse() and penaltyDeath(). Done means telemetry can still obtain screen density without triggering the incorrect-context violation.

Written by the indexing model from the issue text.

Assessment

Tech stack
kotlin
Domain
mobile-dev
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Clearly specified
Newbie friendliness
52/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.