MainActivity: fix einkUpdate/hapticFeedback crash when called from NativeThread#596
MainActivity: fix einkUpdate/hapticFeedback crash when called from NativeThread#596backcountrymountains wants to merge 2 commits into
Conversation
window.decorView.findViewById() must not be called from a non-UI thread. einkUpdate() and performHapticFeedback() are invoked from the NativeThread (the LuaJIT event loop), so accessing window.decorView there is unsafe and can crash on some devices. Pre-cache the root view on the main thread during onCreate() and use the cached reference in all EPD/haptic calls. If the view is not yet ready the call returns early rather than crashing.
Ensures the cached root view is always fresh when the surface is (re)created, rather than relying solely on the onCreate assignment. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
|
So, Claude suggested this but I don't have any direct evidence that it is necessary. This is way above my knowledge grade. I asked for evidence that this PR was necessary and Claude said:
With regard to a test, which I can attempt to perform if you want, Claude said:
|
Unless there is a confirmed crash or a reproducible issue, it is better to keep the existing implementation. Changes in MainActivity can potentially impact many devices, so it is preferable to avoid them until we know they're actually necessary. |
|
Indeed, I had interpreted it as something that had actually happened on that device. |
|
Agree with all. Closing |
Summary
einkUpdateandperformHapticFeedbackare called from the NativeThread (Lua/C side). Both previously resolved the root view inline viawindow.decorView.findViewById(android.R.id.content), which is a UI-thread-only call and triggers aCalledFromWrongThreadExceptionon affected devices.Fix: cache the root view as
epdRootViewinonCreate(UI thread), and refresh it insurfaceCreatedso it stays current if the surface is ever recreated. The call sites are updated to useepdRootView ?: return— a no-op if somehow called before the surface is ready.Changes
MainActivity.kt: addepdRootViewfield, set inonCreateand refreshed insurfaceCreated; replace the three inlinewindow.decorView.findViewByid(...)calls ineinkUpdate(×2) andperformHapticFeedbackwith the cached valueNotes
This is a general Android fix, not device-specific. Split out of #592 (Nook GL4+ lights support) at reviewer request.
🤖 Generated with Claude Code
This change is