Refactor MediaPlayerController to Kotlin

and also make MediaPlayerService context-free
This commit is contained in:
tzugen 2021-05-21 22:35:22 +02:00
parent a801e276ee
commit df047dd463
No known key found for this signature in database
GPG Key ID: 61E9C34BC10EC930
4 changed files with 517 additions and 610 deletions

View File

@ -63,7 +63,8 @@ style:
excludePackageStatements: false
excludeImportStatements: false
MagicNumber:
ignoreNumbers: ['-1', '0', '1', '2', '100']
# 100 common in percentage, 1000 in milliseconds
ignoreNumbers: ['-1', '0', '1', '2', '100', '1000']
ignoreEnums: true
ignorePropertyDeclaration: true
UnnecessaryAbstractClass:

View File

@ -26,5 +26,5 @@ val mediaPlayerModule = module {
single { AudioFocusHandler(get()) }
// TODO Ideally this can be cleaned up when all circular references are removed.
single { MediaPlayerController(androidContext(), get(), get(), get(), get(), get()) }
single { MediaPlayerController(get(), get(), get(), get(), get()) }
}

View File

@ -24,8 +24,10 @@ import android.view.KeyEvent
import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationManagerCompat
import org.koin.android.ext.android.inject
import org.koin.core.component.KoinApiExtension
import org.moire.ultrasonic.R
import org.moire.ultrasonic.activity.NavigationActivity
import org.moire.ultrasonic.app.UApp
import org.moire.ultrasonic.domain.MusicDirectory
import org.moire.ultrasonic.domain.PlayerState
import org.moire.ultrasonic.domain.RepeatMode
@ -47,6 +49,7 @@ import timber.log.Timber
* Android Foreground Service for playing music
* while the rest of the Ultrasonic App is in the background.
*/
@KoinApiExtension
@Suppress("LargeClass")
class MediaPlayerService : Service() {
private val binder: IBinder = SimpleServiceBinder(this)
@ -906,7 +909,8 @@ class MediaPlayerService : Service() {
private val instanceLock = Any()
@JvmStatic
fun getInstance(context: Context): MediaPlayerService? {
fun getInstance(): MediaPlayerService? {
val context = UApp.applicationContext()
synchronized(instanceLock) {
for (i in 0..19) {
if (instance != null) return instance
@ -931,20 +935,20 @@ class MediaPlayerService : Service() {
@JvmStatic
fun executeOnStartedMediaPlayerService(
context: Context,
taskToExecute: (MediaPlayerService?) -> Unit
taskToExecute: (MediaPlayerService) -> Unit
) {
val t: Thread = object : Thread() {
override fun run() {
val instance = getInstance(context)
val instance = getInstance()
if (instance == null) {
Timber.e("ExecuteOnStarted.. failed to get a MediaPlayerService instance!")
return
}
} else {
taskToExecute(instance)
}
}
}
t.start()
}
}