@@ -26,6 +26,7 @@ import com.itsaky.androidide.tooling.api.messages.result.BuildInfo
26
26
import com.itsaky.androidide.tooling.events.ProgressEvent
27
27
import com.itsaky.androidide.tooling.events.configuration.ProjectConfigurationStartEvent
28
28
import com.itsaky.androidide.tooling.events.task.TaskStartEvent
29
+ import com.itsaky.androidide.utils.ILogger
29
30
import com.itsaky.androidide.utils.flashError
30
31
import com.itsaky.androidide.utils.flashSuccess
31
32
import java.lang.ref.WeakReference
@@ -36,77 +37,109 @@ import java.lang.ref.WeakReference
36
37
*/
37
38
class EditorBuildEventListener : GradleBuildService .EventListener {
38
39
40
+ private var enabled = true
39
41
private var activityReference: WeakReference <EditorHandlerActivity > = WeakReference (null )
40
42
43
+ companion object {
44
+
45
+ private val log = ILogger .newInstance(" EditorBuildEventListener" )
46
+ }
47
+
48
+ private val _activity : EditorHandlerActivity ?
49
+ get() = activityReference.get()
50
+ private val activity: EditorHandlerActivity
51
+ get() = checkNotNull(activityReference.get()) { " Activity reference has been destroyed!" }
52
+
41
53
fun setActivity (activity : EditorHandlerActivity ) {
42
54
this .activityReference = WeakReference (activity)
55
+ this .enabled = true
56
+ }
57
+
58
+ fun release () {
59
+ activityReference.clear()
60
+ this .enabled = false
43
61
}
44
62
45
63
override fun prepareBuild (buildInfo : BuildInfo ) {
64
+ checkActivity(" prepareBuild" ) ? : return
65
+
46
66
val isFirstBuild = isFirstBuild
47
- activity()
67
+ activity
48
68
.setStatus(
49
- activity() .getString(if (isFirstBuild) string.preparing_first else string.preparing)
69
+ activity.getString(if (isFirstBuild) string.preparing_first else string.preparing)
50
70
)
51
71
52
72
if (isFirstBuild) {
53
- activity() .showFirstBuildNotice()
73
+ activity.showFirstBuildNotice()
54
74
}
55
75
56
- activity() .editorViewModel.isBuildInProgress = true
57
- activity() .binding.bottomSheet.clearBuildOutput()
76
+ activity.editorViewModel.isBuildInProgress = true
77
+ activity.binding.bottomSheet.clearBuildOutput()
58
78
59
79
if (buildInfo.tasks.isNotEmpty()) {
60
- activity() .binding.bottomSheet.appendBuildOut(
61
- activity() .getString(R .string.title_run_tasks) + " : " + buildInfo.tasks)
80
+ activity.binding.bottomSheet.appendBuildOut(
81
+ activity.getString(R .string.title_run_tasks) + " : " + buildInfo.tasks)
62
82
}
63
83
}
64
84
65
85
override fun onBuildSuccessful (tasks : List <String ?>) {
86
+ checkActivity(" onBuildSuccessful" ) ? : return
87
+
66
88
analyzeCurrentFile()
67
89
68
90
isFirstBuild = false
69
- activity() .editorViewModel.isBuildInProgress = false
91
+ activity.editorViewModel.isBuildInProgress = false
70
92
71
- activity() .flashSuccess(R .string.build_status_sucess)
93
+ activity.flashSuccess(R .string.build_status_sucess)
72
94
}
73
95
74
96
override fun onProgressEvent (event : ProgressEvent ) {
97
+ checkActivity(" onProgressEvent" ) ? : return
98
+
75
99
if (event is ProjectConfigurationStartEvent || event is TaskStartEvent ) {
76
- activity() .setStatus(event.descriptor.displayName)
100
+ activity.setStatus(event.descriptor.displayName)
77
101
}
78
102
}
79
103
80
104
override fun onBuildFailed (tasks : List <String ?>) {
105
+ checkActivity(" onBuildFailed" ) ? : return
81
106
82
107
analyzeCurrentFile()
83
108
84
109
isFirstBuild = false
85
- activity() .editorViewModel.isBuildInProgress = false
110
+ activity.editorViewModel.isBuildInProgress = false
86
111
87
- activity() .flashError(R .string.build_status_failed)
112
+ activity.flashError(R .string.build_status_failed)
88
113
}
89
114
90
115
override fun onOutput (line : String? ) {
116
+ checkActivity(" onOutput" ) ? : return
91
117
92
- line?.let { activity() .appendBuildOutput(it) }
118
+ line?.let { activity.appendBuildOutput(it) }
93
119
// TODO This can be handled better when ProgressEvents are received from Tooling API server
94
120
if (line!! .contains(" BUILD SUCCESSFUL" ) || line.contains(" BUILD FAILED" )) {
95
- activity() .setStatus(line)
121
+ activity.setStatus(line)
96
122
}
97
123
}
98
124
99
125
private fun analyzeCurrentFile () {
126
+ checkActivity(" analyzeCurrentFile" ) ? : return
100
127
101
- val editorView = activity() .getCurrentEditor()
128
+ val editorView = _activity ? .getCurrentEditor()
102
129
if (editorView != null ) {
103
130
val editor = editorView.editor
104
131
editor?.analyze()
105
132
}
106
133
}
107
134
108
- fun activity (): EditorHandlerActivity {
109
- return activityReference.get()
110
- ? : throw IllegalStateException (" Activity reference has been destroyed!" )
135
+ private fun checkActivity (action : String ): EditorHandlerActivity ? {
136
+ if (! enabled) return null
137
+
138
+ return _activity .also {
139
+ if (it == null ) {
140
+ log.warn(" [$action ] Activity reference has been destroyed!" )
141
+ enabled = false
142
+ }
143
+ }
111
144
}
112
145
}
0 commit comments