diff --git a/app/src/main/java/com/itsaky/androidide/activities/editor/ProjectHandlerActivity.kt b/app/src/main/java/com/itsaky/androidide/activities/editor/ProjectHandlerActivity.kt index ca6f2e7add..25b0a93a9a 100644 --- a/app/src/main/java/com/itsaky/androidide/activities/editor/ProjectHandlerActivity.kt +++ b/app/src/main/java/com/itsaky/androidide/activities/editor/ProjectHandlerActivity.kt @@ -391,6 +391,11 @@ abstract class ProjectHandlerActivity : BaseEditorActivity() { } override fun preDestroy() { + // First, and on every destroy rather than only a finishing one: onConnected is a bound + // reference to this activity, so leaving it set lets a bind that is still pending deliver + // into an instance that is already tearing down. + buildServiceConnection.onConnected = null + syncNotificationFlashbar?.dismiss() syncNotificationFlashbar = null @@ -416,8 +421,7 @@ abstract class ProjectHandlerActivity : BaseEditorActivity() { } try { - unbindService(buildServiceConnection) - buildServiceConnection.onConnected = {} + applicationContext.unbindService(buildServiceConnection) } catch (_: Throwable) { log.error("Unable to unbind service") } finally { @@ -494,9 +498,13 @@ abstract class ProjectHandlerActivity : BaseEditorActivity() { buildServiceConnection.onConnected = this::onGradleBuildServiceConnected + // Bind through the application context, not the activity. LoadedApk.mServices keys its + // ServiceDispatcher on the binding Context, so an activity-scoped bind that outlives the + // instance -- a configuration change skips the unbind in preDestroy -- retains the dead + // activity through the framework's own bookkeeping. if ( - bindService( - Intent(this, GradleBuildService::class.java), + applicationContext.bindService( + Intent(applicationContext, GradleBuildService::class.java), buildServiceConnection, BIND_AUTO_CREATE or BIND_IMPORTANT, ) diff --git a/app/src/test/java/com/itsaky/androidide/activities/editor/BuildServiceCallbackClearedTest.kt b/app/src/test/java/com/itsaky/androidide/activities/editor/BuildServiceCallbackClearedTest.kt new file mode 100644 index 0000000000..f82785d7a6 --- /dev/null +++ b/app/src/test/java/com/itsaky/androidide/activities/editor/BuildServiceCallbackClearedTest.kt @@ -0,0 +1,67 @@ +package com.itsaky.androidide.activities.editor + +import com.google.common.truth.Truth.assertThat +import com.itsaky.androidide.app.BaseApplication +import com.itsaky.androidide.services.builder.GradleBuildService +import com.itsaky.androidide.services.builder.GradleBuildServiceConnnection +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.Robolectric +import org.robolectric.RobolectricTestRunner +import org.robolectric.annotation.Config + +/** + * A configuration change destroys the editor without finishing it, so [isDestroying] is false and + * the finish-only teardown is skipped. The build service callback must be released anyway: it is a + * bound reference to this activity (`this::onGradleBuildServiceConnected`), so a bind still pending + * at destroy would otherwise deliver into a dead instance. + */ +@RunWith(RobolectricTestRunner::class) +@Config(application = BuildServiceCallbackClearedTest.TestApp::class) +class BuildServiceCallbackClearedTest { + open class TestApp : BaseApplication() + + @Test + fun givenANonFinishingDestroy_whenPreDestroyRuns_thenTheBuildServiceCallbackIsCleared() { + assertCallbackClearedWhenDestroying(false) + } + + @Test + fun givenAFinishingDestroy_whenPreDestroyRuns_thenTheBuildServiceCallbackIsCleared() { + assertCallbackClearedWhenDestroying(true) + } + + /** + * Both destroy shapes, because the bug was an asymmetry between them: the clearing used to sit + * inside the `isDestroying` guard, so it ran for a finishing destroy and not for a recreation. + * Pinning only one shape would not have caught that. + */ + private fun assertCallbackClearedWhenDestroying(destroying: Boolean) { + val activity = Robolectric.buildActivity(EditorHandlerActivity::class.java).get() + val connection = connectionField.get(activity) as GradleBuildServiceConnnection + onConnected.set(connection, { _: GradleBuildService -> }) + isDestroying.set(activity, destroying) + + // preDestroy goes on to tear down members that a bare activity never initialised; the + // clearing under test happens first, so what it throws afterwards is not this test's + // concern. If the clearing ever moves back below that teardown, the assertion fails. + runCatching { preDestroy.invoke(activity) } + + assertThat(onConnected.get(connection)).isNull() + } + + private val connectionField = + ProjectHandlerActivity::class + .java + .getDeclaredField("buildServiceConnection") + .apply { isAccessible = true } + private val onConnected = + GradleBuildServiceConnnection::class + .java + .getDeclaredField("onConnected") + .apply { isAccessible = true } + private val isDestroying = + BaseEditorActivity::class.java.getDeclaredField("isDestroying").apply { isAccessible = true } + private val preDestroy = + ProjectHandlerActivity::class.java.getDeclaredMethod("preDestroy").apply { isAccessible = true } +}