Skip to content

Commit 7c1b683

Browse files
ADFA-4436 Make editor decoration registration guard atomic
Claim the one-time listener registration with AtomicBoolean.compareAndSet so concurrent init() callers can't double-register, and roll the flag back on failure so a later init() can retry instead of being permanently stuck.
1 parent ce620d7 commit 7c1b683

1 file changed

Lines changed: 21 additions & 21 deletions

File tree

‎app/src/main/java/com/itsaky/androidide/utils/EditorDecorationBridge.kt‎

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import com.itsaky.androidide.plugins.manager.core.PluginManager
2626
import com.itsaky.androidide.syntax.decoration.EditorDecorationRegistry
2727
import org.greenrobot.eventbus.EventBus
2828
import org.slf4j.LoggerFactory
29+
import java.util.concurrent.atomic.AtomicBoolean
2930

3031
/**
3132
* Bridges editor decoration providers contributed by enabled plugins into the editor's
@@ -43,8 +44,7 @@ object EditorDecorationBridge {
4344

4445
private val log = LoggerFactory.getLogger(EditorDecorationBridge::class.java)
4546

46-
@Volatile
47-
private var registered = false
47+
private val registered = AtomicBoolean(false)
4848

4949
/** Last seen UI night-mode bit, so we only react when day/night actually flips. */
5050
private var lastNightMode = Int.MIN_VALUE
@@ -56,27 +56,27 @@ object EditorDecorationBridge {
5656
*/
5757
@JvmStatic
5858
fun init() {
59-
if (registered) {
60-
refresh()
61-
return
62-
}
63-
registered = true
64-
try {
65-
val app = BaseApplication.baseInstance
66-
lastNightMode = app.resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK
67-
app.registerComponentCallbacks(object : ComponentCallbacks {
68-
override fun onConfigurationChanged(newConfig: Configuration) {
69-
val night = newConfig.uiMode and Configuration.UI_MODE_NIGHT_MASK
70-
if (night != lastNightMode) {
71-
lastNightMode = night
72-
refresh()
59+
// Atomically claim the one-time registration. Only the winning thread registers; if it
60+
// fails, reset the flag so a later init() can retry.
61+
if (registered.compareAndSet(false, true)) {
62+
try {
63+
val app = BaseApplication.baseInstance
64+
lastNightMode = app.resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK
65+
app.registerComponentCallbacks(object : ComponentCallbacks {
66+
override fun onConfigurationChanged(newConfig: Configuration) {
67+
val night = newConfig.uiMode and Configuration.UI_MODE_NIGHT_MASK
68+
if (night != lastNightMode) {
69+
lastNightMode = night
70+
refresh()
71+
}
7372
}
74-
}
7573

76-
override fun onLowMemory() {}
77-
})
78-
} catch (t: Throwable) {
79-
log.error("Failed to register editor decoration theme listener", t)
74+
override fun onLowMemory() {}
75+
})
76+
} catch (t: Throwable) {
77+
registered.set(false)
78+
log.error("Failed to register editor decoration theme listener", t)
79+
}
8080
}
8181
refresh()
8282
}

0 commit comments

Comments
 (0)