android: Loader - load early (before service thread) both in activity and service.

Loader converted from java to kotlin.

Instead of loading libmpd when the service thread is started,
the service will not start the the thread if libmpd failed to load.

The loader is also accessed by the view data to let
the ui adjust if failed to load, by showing the failure reason
and disabling the Start MPD button.
This commit is contained in:
gd
2025-02-06 14:23:26 +02:00
parent ae1c5e3424
commit f1e43cb498
7 changed files with 155 additions and 60 deletions

View File

@@ -59,6 +59,9 @@ class Main : Service(), Runnable {
}
}
private lateinit var mpdApp: MPDApplication
private lateinit var mpdLoader: Loader
private var mThread: Thread? = null
private var mStatus = MAIN_STATUS_STOPPED
private var mAbort = false
@@ -104,6 +107,11 @@ class Main : Service(), Runnable {
}
}
override fun onCreate() {
super.onCreate()
mpdLoader = Loader
}
@Synchronized
private fun sendMessage(
@Suppress("SameParameterValue") what: Int,
@@ -152,19 +160,6 @@ class Main : Service(), Runnable {
}
override fun run() {
if (!Loader.loaded) {
val error = """
Failed to load the native MPD library.
Report this problem to us, and include the following information:
SUPPORTED_ABIS=${java.lang.String.join(", ", *Build.SUPPORTED_ABIS)}
PRODUCT=${Build.PRODUCT}
FINGERPRINT=${Build.FINGERPRINT}
error=${Loader.error}
""".trimIndent()
setStatus(MAIN_STATUS_ERROR, error)
stopSelf()
return
}
synchronized(this) {
if (mAbort) return
setStatus(MAIN_STATUS_STARTED, null)
@@ -245,7 +240,9 @@ class Main : Service(), Runnable {
.setContentIntent(contentIntent)
.build()
mThread = Thread(this).apply { start() }
if (mpdLoader.isLoaded) {
mThread = Thread(this).apply { start() }
}
val player = MPDPlayer(Looper.getMainLooper())
mMediaSession = MediaSession.Builder(this, player).build()