fix: build service is not unbound even after application exit

This commit is contained in:
Akash Yadav
2022-12-21 18:25:10 +05:30
parent c09a87e3d1
commit c9af7f1ccb
7 changed files with 76 additions and 62 deletions
@@ -685,4 +685,4 @@ abstract class BaseEditorActivity :
}
}
}
}
}
@@ -17,7 +17,11 @@
package com.itsaky.androidide.activities.editor
import android.content.ComponentName
import android.content.Intent
import android.content.ServiceConnection
import android.os.Bundle
import android.os.IBinder
import android.view.ViewGroup.MarginLayoutParams
import android.widget.CheckBox
import androidx.annotation.GravityInt
@@ -45,6 +49,8 @@ import com.itsaky.androidide.projects.ProjectManager.setupProject
import com.itsaky.androidide.projects.api.Project
import com.itsaky.androidide.projects.builder.BuildService
import com.itsaky.androidide.services.GradleBuildService
import com.itsaky.androidide.services.GradleBuildService.GradleServiceBinder
import com.itsaky.androidide.services.GradleBuildServiceConnnection
import com.itsaky.androidide.tooling.api.messages.result.InitializeResult
import com.itsaky.androidide.utils.DialogUtils.newMaterialDialogBuilder
import com.itsaky.androidide.utils.RecursiveFileSearcher
@@ -71,6 +77,11 @@ abstract class ProjectHandlerActivity : BaseEditorActivity(), IProjectHandler {
}
protected val mBuildEventListener = EditorBuildEventListener()
companion object {
@JvmStatic
private val buildServiceConnection = GradleBuildServiceConnnection()
}
abstract fun doCloseAll(runAfter: () -> Unit)
@@ -102,11 +113,12 @@ abstract class ProjectHandlerActivity : BaseEditorActivity(), IProjectHandler {
if (isDestroying) {
try {
app.unbindGradleBuildService()
unbindService(buildServiceConnection)
} catch (err: Throwable) {
log.error("Unable to unbind service")
} finally {
Lookup.DEFAULT.unregister(BuildService.KEY_BUILD_SERVICE)
viewModel.isBoundToBuildSerice = false
}
}
}
@@ -124,15 +136,17 @@ abstract class ProjectHandlerActivity : BaseEditorActivity(), IProjectHandler {
}
override fun startServices() {
val service = Lookup.DEFAULT.lookup(BuildService.KEY_BUILD_SERVICE) as GradleBuildService?
if (service != null) {
if (viewModel.isBoundToBuildSerice && service != null) {
log.info("Reusing already started Gradle build service")
onGradleBuildServiceConnected(service)
return
}
if (app.bindGradleBuildService(this::onGradleBuildServiceConnected)) {
buildServiceConnection.onConnected = this::onGradleBuildServiceConnected
if (bindService(Intent(this, GradleBuildService::class.java), buildServiceConnection, BIND_AUTO_CREATE or BIND_IMPORTANT)) {
log.info("Bind request for Gradle build service was successful...")
} else {
log.error("Gradle build service doesn't exist or the IDE is not allowed to access it.")
@@ -202,6 +216,7 @@ abstract class ProjectHandlerActivity : BaseEditorActivity(), IProjectHandler {
protected fun onGradleBuildServiceConnected(service: GradleBuildService) {
log.info("Connected to Gradle build service")
viewModel.isBoundToBuildSerice = true
Lookup.DEFAULT.update(BuildService.KEY_BUILD_SERVICE, service)
service.setEventListener(mBuildEventListener)
@@ -17,11 +17,8 @@
*/
package com.itsaky.androidide.app
import android.content.ComponentName
import android.content.Intent
import android.content.ServiceConnection
import android.net.Uri
import android.os.IBinder
import com.blankj.utilcode.util.ThrowableUtils
import com.itsaky.androidide.BuildConfig
import com.itsaky.androidide.activities.CrashHandlerActivity
@@ -29,10 +26,6 @@ import com.itsaky.androidide.events.AppEventsIndex
import com.itsaky.androidide.events.LspApiEventsIndex
import com.itsaky.androidide.events.LspJavaEventsIndex
import com.itsaky.androidide.events.ProjectsApiEventsIndex
import com.itsaky.androidide.lookup.Lookup
import com.itsaky.androidide.projects.builder.BuildService
import com.itsaky.androidide.services.GradleBuildService
import com.itsaky.androidide.services.GradleBuildService.GradleServiceBinder
import com.itsaky.androidide.utils.ILogger
import com.itsaky.toaster.Toaster.Type.ERROR
import com.itsaky.toaster.toast
@@ -43,23 +36,7 @@ import org.greenrobot.eventbus.EventBus
class IDEApplication : BaseApplication() {
private var uncaughtExceptionHandler: UncaughtExceptionHandler? = null
private val log = ILogger.newInstance("IDEApplication")
private var onGradleBuildServiceConnected: ((GradleBuildService) -> Unit)? = null
private val mGradleServiceConnection: ServiceConnection =
object : ServiceConnection {
override fun onServiceConnected(name: ComponentName?, service: IBinder?) {
val buildService = (service as GradleServiceBinder).service
Lookup.DEFAULT.update(BuildService.KEY_BUILD_SERVICE, buildService)
onGradleBuildServiceConnected?.invoke(buildService)
}
override fun onServiceDisconnected(name: ComponentName?) {
log.info("Disconnected from Gradle build service")
}
}
override fun onCreate() {
instance = this
uncaughtExceptionHandler = Thread.getDefaultUncaughtExceptionHandler()
@@ -75,24 +52,6 @@ class IDEApplication : BaseApplication() {
.installDefaultEventBus()
}
fun bindGradleBuildService(onBound: (GradleBuildService) -> Unit): Boolean {
this.onGradleBuildServiceConnected = onBound
return bindService(
Intent(this, GradleBuildService::class.java),
mGradleServiceConnection,
BIND_AUTO_CREATE or BIND_IMPORTANT
)
}
fun unbindGradleBuildService() {
this.onGradleBuildServiceConnected = null
try {
unbindService(mGradleServiceConnection)
} catch (err: Throwable) {
log.error("Unable to unbind Gradle build service")
}
}
private fun handleCrash(thread: Thread, th: Throwable) {
writeException(th)
@@ -0,0 +1,44 @@
/*
* This file is part of AndroidIDE.
*
* AndroidIDE is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* AndroidIDE is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with AndroidIDE. If not, see <https://www.gnu.org/licenses/>.
*/
package com.itsaky.androidide.services
import android.content.ComponentName
import android.content.ServiceConnection
import android.os.IBinder
import com.itsaky.androidide.services.GradleBuildService.GradleServiceBinder
import com.itsaky.androidide.utils.ILogger
/**
* [ServiceConnection] for [GradleBuildService].
*
* @author Akash Yadav
*/
class GradleBuildServiceConnnection : ServiceConnection {
internal var onConnected: (GradleBuildService) -> Unit = {}
private val log = ILogger.newInstance("GradleBuildServiceConnnection")
override fun onServiceConnected(name: ComponentName?, service: IBinder?) {
val buildService = (service as GradleServiceBinder).service
onConnected(buildService)
}
override fun onServiceDisconnected(name: ComponentName?) {
log.info("Disconnected from Gradle build service")
}
}
@@ -407,19 +407,7 @@ public class CodeEditorView extends LinearLayout {
editor.onEditorSelected();
}
}
public void undo() {
if (binding.editor.canUndo()) {
binding.editor.undo();
}
}
public void redo() {
if (binding.editor.canRedo()) {
binding.editor.redo();
}
}
public void beginSearch() {
if (this.binding == null || searchLayout == null) {
LOG.warn("Editor layout is null content=" + binding + ", searchLayout=" + searchLayout);
@@ -30,10 +30,12 @@ class EditorViewModel : ViewModel() {
internal val _isBuildInProgress = MutableLiveData(false)
internal val _isInitializing = MutableLiveData(false)
internal val _isConfigurationChange = MutableLiveData(false)
internal val _statusText = MutableLiveData<Pair<CharSequence, @GravityInt Int>>("" to CENTER)
internal val _displayedFile = MutableLiveData(-1)
internal val _fileTreeDrawerOpened = MutableLiveData(false)
private val _isBoundToBuildService = MutableLiveData(false)
private val _isConfigurationChange = MutableLiveData(false)
private val files = MutableLiveData<MutableList<File>>(ArrayList())
private val fileModified = MutableLiveData(false)
@@ -43,12 +45,18 @@ class EditorViewModel : ViewModel() {
*/
private val mCurrentFile = MutableLiveData<Pair<Int, File?>?>(null)
var isBoundToBuildSerice: Boolean
get() = _isBoundToBuildService.value ?: false
set(value) {
_isBoundToBuildService.value = value
}
var isConfigChange: Boolean
get() = _isConfigurationChange.value ?: false
set(value) {
_isConfigurationChange.value = value
}
var isBuildInProgress: Boolean
get() = _isBuildInProgress.value ?: false
set(value) {
Binary file not shown.