2025-08-16 07:52:15 -04:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2025 Meshtastic LLC
|
|
|
|
|
*
|
|
|
|
|
* This program 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.
|
|
|
|
|
*
|
|
|
|
|
* This program 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 this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
package com.geeksville.mesh
|
|
|
|
|
|
2025-12-12 08:23:35 -06:00
|
|
|
import android.content.Context
|
2025-08-16 07:52:15 -04:00
|
|
|
import androidx.appcompat.app.AppCompatActivity.BIND_ABOVE_CLIENT
|
|
|
|
|
import androidx.appcompat.app.AppCompatActivity.BIND_AUTO_CREATE
|
|
|
|
|
import androidx.lifecycle.DefaultLifecycleObserver
|
|
|
|
|
import androidx.lifecycle.LifecycleOwner
|
|
|
|
|
import androidx.lifecycle.lifecycleScope
|
2025-12-28 08:30:15 -06:00
|
|
|
import co.touchlab.kermit.Logger
|
2025-08-16 07:52:15 -04:00
|
|
|
import com.geeksville.mesh.android.BindFailedException
|
|
|
|
|
import com.geeksville.mesh.android.ServiceClient
|
2025-12-12 13:02:17 -06:00
|
|
|
import com.geeksville.mesh.concurrent.SequentialJob
|
2025-08-16 07:52:15 -04:00
|
|
|
import com.geeksville.mesh.service.MeshService
|
|
|
|
|
import com.geeksville.mesh.service.startService
|
2025-12-12 08:23:35 -06:00
|
|
|
import dagger.hilt.android.qualifiers.ActivityContext
|
2025-08-16 07:52:15 -04:00
|
|
|
import dagger.hilt.android.scopes.ActivityScoped
|
2025-09-30 16:55:56 -04:00
|
|
|
import org.meshtastic.core.service.IMeshService
|
|
|
|
|
import org.meshtastic.core.service.ServiceRepository
|
2025-08-16 07:52:15 -04:00
|
|
|
import javax.inject.Inject
|
|
|
|
|
|
|
|
|
|
/** A Activity-lifecycle-aware [ServiceClient] that binds [MeshService] once the Activity is started. */
|
|
|
|
|
@ActivityScoped
|
|
|
|
|
class MeshServiceClient
|
|
|
|
|
@Inject
|
|
|
|
|
constructor(
|
2025-12-12 08:23:35 -06:00
|
|
|
@ActivityContext private val context: Context,
|
2025-08-16 07:52:15 -04:00
|
|
|
private val serviceRepository: ServiceRepository,
|
2025-12-12 13:02:17 -06:00
|
|
|
private val serviceSetupJob: SequentialJob,
|
2025-08-16 07:52:15 -04:00
|
|
|
) : ServiceClient<IMeshService>(IMeshService.Stub::asInterface),
|
|
|
|
|
DefaultLifecycleObserver {
|
|
|
|
|
|
2025-12-12 08:23:35 -06:00
|
|
|
private val lifecycleOwner: LifecycleOwner = context as LifecycleOwner
|
2025-08-16 07:52:15 -04:00
|
|
|
|
|
|
|
|
init {
|
2025-12-28 08:30:15 -06:00
|
|
|
Logger.d { "Adding self as LifecycleObserver for $lifecycleOwner" }
|
2025-08-16 07:52:15 -04:00
|
|
|
lifecycleOwner.lifecycle.addObserver(this)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// region ServiceClient overrides
|
|
|
|
|
|
|
|
|
|
override fun onConnected(service: IMeshService) {
|
2025-12-12 13:02:17 -06:00
|
|
|
serviceSetupJob.launch(lifecycleOwner.lifecycleScope) {
|
|
|
|
|
serviceRepository.setMeshService(service)
|
2025-12-28 08:30:15 -06:00
|
|
|
Logger.d { "connected to mesh service, connectionState=${serviceRepository.connectionState.value}" }
|
2025-12-12 13:02:17 -06:00
|
|
|
}
|
2025-08-16 07:52:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override fun onDisconnected() {
|
2025-12-12 13:02:17 -06:00
|
|
|
serviceSetupJob.cancel()
|
2025-08-16 07:52:15 -04:00
|
|
|
serviceRepository.setMeshService(null)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// endregion
|
|
|
|
|
|
|
|
|
|
// region DefaultLifecycleObserver overrides
|
|
|
|
|
|
|
|
|
|
override fun onStart(owner: LifecycleOwner) {
|
|
|
|
|
super.onStart(owner)
|
2025-12-28 08:30:15 -06:00
|
|
|
Logger.d { "Lifecycle: ON_START" }
|
2025-08-16 07:52:15 -04:00
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
bindMeshService()
|
|
|
|
|
} catch (ex: BindFailedException) {
|
2025-12-28 08:30:15 -06:00
|
|
|
Logger.e { "Bind of MeshService failed: ${ex.message}" }
|
2025-08-16 07:52:15 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override fun onDestroy(owner: LifecycleOwner) {
|
|
|
|
|
super.onDestroy(owner)
|
2025-12-28 08:30:15 -06:00
|
|
|
Logger.d { "Lifecycle: ON_DESTROY" }
|
2025-08-16 07:52:15 -04:00
|
|
|
|
|
|
|
|
owner.lifecycle.removeObserver(this)
|
2025-12-28 08:30:15 -06:00
|
|
|
Logger.d { "Removed self as LifecycleObserver to $lifecycleOwner" }
|
2025-08-16 07:52:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// endregion
|
|
|
|
|
|
|
|
|
|
@Suppress("TooGenericExceptionCaught")
|
|
|
|
|
private fun bindMeshService() {
|
2025-12-28 08:30:15 -06:00
|
|
|
Logger.d { "Binding to mesh service!" }
|
2025-08-16 07:52:15 -04:00
|
|
|
try {
|
2025-12-12 08:23:35 -06:00
|
|
|
MeshService.startService(context)
|
2025-08-16 07:52:15 -04:00
|
|
|
} catch (ex: Exception) {
|
2025-12-28 08:30:15 -06:00
|
|
|
Logger.e { "Failed to start service from activity - but ignoring because bind will work: ${ex.message}" }
|
2025-08-16 07:52:15 -04:00
|
|
|
}
|
|
|
|
|
|
2025-12-12 08:23:35 -06:00
|
|
|
connect(context, MeshService.createIntent(context), BIND_AUTO_CREATE + BIND_ABOVE_CLIENT)
|
2025-08-16 07:52:15 -04:00
|
|
|
}
|
|
|
|
|
}
|