Skip to content
This repository has been archived by the owner on Oct 18, 2024. It is now read-only.

Commit

Permalink
fix: check for activity accessibility for each build callback in Edit…
Browse files Browse the repository at this point in the history
…orBuildEventListener (#1604)
  • Loading branch information
itsaky committed Jan 5, 2024
1 parent 3606588 commit 97c760a
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,9 @@ abstract class BaseEditorActivity :
protected var diagnosticInfoBinding: LayoutDiagnosticInfoBinding? = null
protected var filesTreeFragment: FileTreeFragment? = null
protected var editorBottomSheet: BottomSheetBehavior<out View?>? = null
protected var isDestroying = false

var isDestroying = false
protected set

protected val log: ILogger = ILogger.newInstance("EditorActivity")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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?) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,22 @@ class EditorBuildEventListener : GradleBuildService.EventListener {

private var activityReference: WeakReference<EditorHandlerActivity> = 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(
Expand All @@ -63,6 +74,10 @@ class EditorBuildEventListener : GradleBuildService.EventListener {
}

override fun onBuildSuccessful(tasks: List<String?>) {
if (!isActivityInaccessible) {
return
}

analyzeCurrentFile()

isFirstBuild = false
Expand All @@ -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<String?>) {
if (!isActivityInaccessible) {
return
}

analyzeCurrentFile()

isFirstBuild = false
Expand All @@ -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")) {
Expand All @@ -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
Expand Down

0 comments on commit 97c760a

Please sign in to comment.