Hi, I'm working on a research prototype static analyzer targeting Kotlin-based Android apps, and running it against this repo. It detected a small threading issue.
Checklist
Affected app version
Trunk
Checked file:
app/src/main/kotlin/org/fossify/clock/activities/MainActivity.kt
What I found
In MainActivity, onResume() calls checkShortcuts(). Inside checkShortcuts(), the stopwatch shortcut is published like this:
shortcutManager.dynamicShortcuts = listOf(stopWatchShortcutInfo)
This property setter calls ShortcutManager.setDynamicShortcuts(...). Android's API documentation warns that this method may take several seconds to complete and should only be called from a worker thread.
Verified bug trace
org.fossify.clock.activities.MainActivity.onResume()
-> checkShortcuts()
-> MainActivity.kt, around line 133:
shortcutManager.dynamicShortcuts = listOf(stopWatchShortcutInfo)
-> Kotlin property setter calls:
ShortcutManager.setDynamicShortcuts(List<ShortcutInfo>)
-> platform shortcut state is updated through ShortcutManager
-> documented as potentially taking several seconds
-> call is reached from the Activity main-thread lifecycle path
-> possible UI stall / delayed startup / ANR risk
I did not see a worker-thread boundary around this specific shortcut publishing call. There is an ensureBackgroundThread { ... } used elsewhere in the same Activity for alarm rescheduling, but not around shortcutManager.dynamicShortcuts.
Why I think this is worth changing
This probably does not fail every time, and it may be fast on many devices. The problem is that the platform API is documented as potentially slow, and the work may depend on system/launcher behavior. If it takes longer than expected, it can block UI drawing and input handling.
Suggested change
Move just the publishing call to a worker thread:
val shortcuts = listOf(stopWatchShortcutInfo)
lifecycleScope.launch(Dispatchers.IO) {
shortcutManager.dynamicShortcuts = shortcuts
}
This keeps the UI thread free while preserving the same shortcut behavior.
Reference
Android documents ShortcutManager#setDynamicShortcuts(...) as a potentially slow call:
This method may take several seconds to complete, so it should only be called from a worker thread.
https://developer.android.com/reference/android/content/pm/ShortcutManager#setDynamicShortcuts(java.util.List%3Candroid.content.pm.ShortcutInfo%3E)
Hi, I'm working on a research prototype static analyzer targeting Kotlin-based Android apps, and running it against this repo. It detected a small threading issue.
Checklist
Affected app version
Trunk
Checked file:
What I found
In
MainActivity,onResume()callscheckShortcuts(). InsidecheckShortcuts(), the stopwatch shortcut is published like this:This property setter calls
ShortcutManager.setDynamicShortcuts(...). Android's API documentation warns that this method may take several seconds to complete and should only be called from a worker thread.Verified bug trace
I did not see a worker-thread boundary around this specific shortcut publishing call. There is an
ensureBackgroundThread { ... }used elsewhere in the same Activity for alarm rescheduling, but not aroundshortcutManager.dynamicShortcuts.Why I think this is worth changing
This probably does not fail every time, and it may be fast on many devices. The problem is that the platform API is documented as potentially slow, and the work may depend on system/launcher behavior. If it takes longer than expected, it can block UI drawing and input handling.
Suggested change
Move just the publishing call to a worker thread:
This keeps the UI thread free while preserving the same shortcut behavior.
Reference
Android documents
ShortcutManager#setDynamicShortcuts(...)as a potentially slow call:https://developer.android.com/reference/android/content/pm/ShortcutManager#setDynamicShortcuts(java.util.List%3Candroid.content.pm.ShortcutInfo%3E)