keymanapp / keyman

Keyman cross platform input methods system running on Android, iOS, Linux, macOS, Windows and mobile and desktop web
https://keyman.com/
Other
377 stars 104 forks source link

bug(android): post-update error - onPackageInstalled where context is null. #10112

Open sentry-io[bot] opened 7 months ago

sentry-io[bot] commented 7 months ago

Sentry Issue: KEYMAN-ANDROID-4AJ

onPackageInstalled where context is null.

@jahorton here. I ran into this while testing #10017 with a version-edited .kmp. I can verify that the following line was reached via debugger when reproducing it:

https://github.com/keymanapp/keyman/blob/864f37884449bac22aff642c0647b61051847898/android/KMEA/app/src/main/java/com/keyman/engine/logic/ResourcesUpdateTool.java#L524

Repro (as done via Android Studio device emulation):

The download process took surprising long to complete when I did this - I had ample time to "exit back" - and my goal for testing #10017 was to verify that the host-page would reset... which is best done by actually having the keyboard host-page active and viewable. (I actually had an 'inspector' open via Chrome to validate that the console was reset... which is suspended unless the host-page is active.) I think it took somewhere between 30 sec to 1 minute for the update's download to complete? Just in case that's relevant.

jahorton commented 7 months ago

In case it somehow ends up mattering, I was emulating a Nexus 5 set to API 32 via Android Studio. The "force-kill" and "restart" operations were triggered by terminating the debugging session via the IDE and starting a new one via the IDE, respectively.

darcywong00 commented 5 months ago

Triaged to 18.0

jahorton commented 3 weeks ago

Decided to investigate this a bit more due to its relationship with #11550; when the repro triggers, the underlying keyboard does, in fact, update - at least, within the Settings menu.

While there was quite the delay in the download completing for the keyboard update, the host page did still reset and reload with the updated version. I verified this via WebView inspection and checking the KBVER property on the loaded keyboard, both before and after. It didn't appear to take effect at first... simply because of the download lag. (I left the inspector up, and when I came back to it, the console logs had cleared due to the reset. Sure enough, the keyboard was updated as well.)

jahorton commented 3 weeks ago

If I stay on the language-specific settings screen, the Sentry event does not trigger. Granted, I didn't see the finalizing notification, but that's because I didn't want to keep my eyes glued to the screen that long.

jahorton commented 3 weeks ago

As best as I can tell, the cause of the error is likely here:

https://github.com/keymanapp/keyman/blob/c85f7748f74f90fda7fc805ef2b0f63919ba88d5/android/KMAPro/kMAPro/src/main/java/com/tavultesoft/kmapro/LanguagesSettingsActivity.java#L130-L136

There are exactly two spots that call the checkForResourceUpdates method, which requires a Context be passed in, and that is one of them. The other one forwards a Context parameter corresponding either to MainActivity or to SystemKeyboard - either of which will last for the app's lifetime. LanguagesSettingsActivity, though... that one only lasts as long as a user is within the appropriate area of the Settings menu.

Why is checkForResourceUpdates significant?

https://github.com/keymanapp/keyman/blob/c85f7748f74f90fda7fc805ef2b0f63919ba88d5/android/KMEA/app/src/main/java/com/keyman/engine/logic/ResourcesUpdateTool.java#L457-L459

That field - currentContext - is only set here. Also note the isContextAvailable function:

https://github.com/keymanapp/keyman/blob/c85f7748f74f90fda7fc805ef2b0f63919ba88d5/android/KMEA/app/src/main/java/com/keyman/engine/logic/ResourcesUpdateTool.java#L446-L450

For it to return false requires that currentContext be set and be in that "finishing" state. Exiting fully from the Settings menu would certainly trigger that if currentContext was left pointing to an instance of LanguagesSettingsActivity.

jahorton commented 3 weeks ago

Retriaging to 19.0 per https://github.com/keymanapp/keyman/issues/11550#issuecomment-2148853989.