From 97c760ab32ce70c8ebe0b34d0215d01d7824896a Mon Sep 17 00:00:00 2001 From: Akash Yadav Date: Fri, 5 Jan 2024 23:41:41 +0530 Subject: [PATCH] fix: check for activity accessibility for each build callback in EditorBuildEventListener (#1604) --- .../activities/editor/BaseEditorActivity.kt | 4 ++- .../editor/EditorHandlerActivity.kt | 2 +- .../handlers/EditorBuildEventListener.kt | 31 +++++++++++++++++++ 3 files changed, 35 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/com/itsaky/androidide/activities/editor/BaseEditorActivity.kt b/app/src/main/java/com/itsaky/androidide/activities/editor/BaseEditorActivity.kt index 6c1f8530c3..8b6f53ff09 100644 --- a/app/src/main/java/com/itsaky/androidide/activities/editor/BaseEditorActivity.kt +++ b/app/src/main/java/com/itsaky/androidide/activities/editor/BaseEditorActivity.kt @@ -104,7 +104,9 @@ abstract class BaseEditorActivity : protected var diagnosticInfoBinding: LayoutDiagnosticInfoBinding? = null protected var filesTreeFragment: FileTreeFragment? = null protected var editorBottomSheet: BottomSheetBehavior? = null - protected var isDestroying = false + + var isDestroying = false + protected set protected val log: ILogger = ILogger.newInstance("EditorActivity") diff --git a/app/src/main/java/com/itsaky/androidide/activities/editor/EditorHandlerActivity.kt b/app/src/main/java/com/itsaky/androidide/activities/editor/EditorHandlerActivity.kt index 90ef48cbdb..3ec177b0e6 100644 --- a/app/src/main/java/com/itsaky/androidide/activities/editor/EditorHandlerActivity.kt +++ b/app/src/main/java/com/itsaky/androidide/activities/editor/EditorHandlerActivity.kt @@ -266,7 +266,7 @@ open class EditorHandlerActivity : ProjectHandlerActivity(), IEditorHandler { } override fun getEditorAtIndex(index: Int): CodeEditorView? { - return binding.editorContainer.getChildAt(index) as CodeEditorView? + return _binding?.editorContainer?.getChildAt(index) as CodeEditorView? } override fun openFileAndSelect(file: File, selection: Range?) { diff --git a/app/src/main/java/com/itsaky/androidide/handlers/EditorBuildEventListener.kt b/app/src/main/java/com/itsaky/androidide/handlers/EditorBuildEventListener.kt index 57bdb5ee93..e2dae93d5f 100644 --- a/app/src/main/java/com/itsaky/androidide/handlers/EditorBuildEventListener.kt +++ b/app/src/main/java/com/itsaky/androidide/handlers/EditorBuildEventListener.kt @@ -38,11 +38,22 @@ class EditorBuildEventListener : GradleBuildService.EventListener { private var activityReference: WeakReference = WeakReference(null) + private val isActivityInaccessible: Boolean + get() { + return activityReference.get()?.let { activity -> + activity.isDestroyed || activity.isFinishing || activity.isDestroying + } == true + } + fun setActivity(activity: EditorHandlerActivity) { this.activityReference = WeakReference(activity) } override fun prepareBuild(buildInfo: BuildInfo) { + if (!isActivityInaccessible) { + return + } + val isFirstBuild = isFirstBuild activity() .setStatus( @@ -63,6 +74,10 @@ class EditorBuildEventListener : GradleBuildService.EventListener { } override fun onBuildSuccessful(tasks: List) { + if (!isActivityInaccessible) { + return + } + analyzeCurrentFile() isFirstBuild = false @@ -72,12 +87,20 @@ class EditorBuildEventListener : GradleBuildService.EventListener { } override fun onProgressEvent(event: ProgressEvent) { + if (!isActivityInaccessible) { + return + } + if (event is ProjectConfigurationStartEvent || event is TaskStartEvent) { activity().setStatus(event.descriptor.displayName) } } override fun onBuildFailed(tasks: List) { + if (!isActivityInaccessible) { + return + } + analyzeCurrentFile() isFirstBuild = false @@ -87,6 +110,10 @@ class EditorBuildEventListener : GradleBuildService.EventListener { } override fun onOutput(line: String?) { + if (!isActivityInaccessible) { + return + } + line?.let { activity().appendBuildOutput(it) } // TODO This can be handled better when ProgressEvents are received from Tooling API server if (line!!.contains("BUILD SUCCESSFUL") || line.contains("BUILD FAILED")) { @@ -95,6 +122,10 @@ class EditorBuildEventListener : GradleBuildService.EventListener { } private fun analyzeCurrentFile() { + if (!isActivityInaccessible) { + return + } + val editorView = activity().getCurrentEditor() if (editorView != null) { val editor = editorView.editor