mapbox / mapbox/mapbox-maps-android
StrictModeViolation: IncorrectContextUseViolation
Nobody has claimed this yet.
- 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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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