mirror of
https://github.com/Michatec/Radio.git
synced 2026-11-16 07:41:38 +01:00
refactor: replace LocalBroadcastManager with AppEventBus for internal communication
This commit is contained in:
1 parent
6570f673f9
commit
575af5f53a
5 files changed
+89
-122
No files matched your search
@@ -7,31 +7,35 @@ import android.content.SharedPreferences
|
||||
import android.content.pm.PackageManager
|
||||
import android.content.res.Configuration
|
||||
import android.net.Uri
|
||||
import android.util.TypedValue
|
||||
import android.os.Build
|
||||
import android.os.Bundle
|
||||
import android.os.Handler
|
||||
import android.os.Looper
|
||||
import android.provider.Settings
|
||||
import android.util.TypedValue
|
||||
import android.view.View
|
||||
import androidx.activity.enableEdgeToEdge
|
||||
import androidx.activity.result.contract.ActivityResultContracts
|
||||
import androidx.appcompat.app.AppCompatActivity
|
||||
import androidx.core.app.ActivityCompat
|
||||
import androidx.core.view.isVisible
|
||||
import androidx.localbroadcastmanager.content.LocalBroadcastManager
|
||||
import androidx.lifecycle.Lifecycle
|
||||
import androidx.lifecycle.lifecycleScope
|
||||
import androidx.lifecycle.repeatOnLifecycle
|
||||
import androidx.navigation.fragment.NavHostFragment
|
||||
import androidx.navigation.ui.AppBarConfiguration
|
||||
import androidx.navigation.ui.NavigationUI
|
||||
import androidx.navigation.ui.navigateUp
|
||||
import android.content.*
|
||||
import com.google.android.material.snackbar.Snackbar
|
||||
import com.michatec.radio.databinding.ActivityMainBinding
|
||||
import com.michatec.radio.helpers.AppEvent
|
||||
import com.michatec.radio.helpers.AppEventBus
|
||||
import com.michatec.radio.helpers.AppThemeHelper
|
||||
import com.michatec.radio.helpers.FileHelper
|
||||
import com.michatec.radio.helpers.LanguageHelper
|
||||
import com.michatec.radio.helpers.PreferencesHelper
|
||||
import com.michatec.radio.helpers.ThemeHelper
|
||||
import kotlinx.coroutines.launch
|
||||
import org.woheller69.freeDroidWarn.FreeDroidWarn
|
||||
import java.util.Locale
|
||||
|
||||
@@ -105,6 +109,9 @@ class MainActivity : AppCompatActivity() {
|
||||
// set up views
|
||||
applyCustomTheme()
|
||||
|
||||
// observe application events
|
||||
observeAppEvents()
|
||||
|
||||
// create .nomedia file - if not yet existing
|
||||
FileHelper.createNomediaFile(getExternalFilesDir(null))
|
||||
|
||||
@@ -136,23 +143,27 @@ class MainActivity : AppCompatActivity() {
|
||||
}
|
||||
}
|
||||
|
||||
override fun onStart() {
|
||||
super.onStart()
|
||||
// register remote server receivers
|
||||
val filter = IntentFilter().apply {
|
||||
addAction(Keys.ACTION_REMOTE_SERVER_ERROR)
|
||||
addAction(Keys.ACTION_REMOTE_AUTH_FAILED)
|
||||
private fun observeAppEvents() {
|
||||
lifecycleScope.launch {
|
||||
repeatOnLifecycle(Lifecycle.State.STARTED) {
|
||||
AppEventBus.events.collect { event ->
|
||||
val message = when (event) {
|
||||
is AppEvent.RemoteServerError -> getString(R.string.error_webserver)
|
||||
is AppEvent.RemoteAuthFailed -> getString(R.string.snackbar_remote_auth_failed, event.ip)
|
||||
else -> return@collect
|
||||
}
|
||||
val snackbar = Snackbar.make(
|
||||
binding.mainRoot,
|
||||
message,
|
||||
Snackbar.LENGTH_LONG
|
||||
)
|
||||
findViewById<View>(R.id.bottom_sheet)?.let {
|
||||
snackbar.anchorView = it
|
||||
}
|
||||
snackbar.show()
|
||||
}
|
||||
}
|
||||
}
|
||||
LocalBroadcastManager.getInstance(this).registerReceiver(
|
||||
remoteServerReceiver,
|
||||
filter
|
||||
)
|
||||
}
|
||||
|
||||
override fun onStop() {
|
||||
super.onStop()
|
||||
// unregister remote server receiver
|
||||
LocalBroadcastManager.getInstance(this).unregisterReceiver(remoteServerReceiver)
|
||||
}
|
||||
|
||||
/* Hides the loading/splash overlay */
|
||||
@@ -244,34 +255,4 @@ class MainActivity : AppCompatActivity() {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Receiver for remote server events
|
||||
*/
|
||||
private val remoteServerReceiver: BroadcastReceiver = object : BroadcastReceiver() {
|
||||
override fun onReceive(context: Context, intent: Intent) {
|
||||
val message = when (intent.action) {
|
||||
Keys.ACTION_REMOTE_SERVER_ERROR -> getString(R.string.error_webserver)
|
||||
Keys.ACTION_REMOTE_AUTH_FAILED -> {
|
||||
val ip = intent.getStringExtra("REMOTE_IP") ?: "unknown"
|
||||
getString(R.string.snackbar_remote_auth_failed, ip)
|
||||
}
|
||||
else -> return
|
||||
}
|
||||
|
||||
val snackbar = Snackbar.make(
|
||||
binding.mainRoot,
|
||||
message,
|
||||
Snackbar.LENGTH_LONG
|
||||
)
|
||||
this@MainActivity.findViewById<View>(R.id.bottom_sheet)?.let {
|
||||
snackbar.anchorView = it
|
||||
}
|
||||
snackbar.show()
|
||||
}
|
||||
}
|
||||
/*
|
||||
* End of declaration
|
||||
*/
|
||||
|
||||
}
|
||||
@@ -10,7 +10,6 @@ import android.os.CountDownTimer
|
||||
import android.os.Handler
|
||||
import android.os.Looper
|
||||
import android.util.Log
|
||||
import androidx.localbroadcastmanager.content.LocalBroadcastManager
|
||||
import androidx.media3.cast.CastPlayer
|
||||
import androidx.media3.common.*
|
||||
import androidx.media3.common.util.UnstableApi
|
||||
@@ -91,16 +90,12 @@ class PlayerService : MediaLibraryService(), SharedPreferences.OnSharedPreferenc
|
||||
|
||||
onError = { error ->
|
||||
Log.e(TAG, "RemoteControlServer error: $error")
|
||||
val intent = Intent(Keys.ACTION_REMOTE_SERVER_ERROR)
|
||||
LocalBroadcastManager.getInstance(this@PlayerService).sendBroadcast(intent)
|
||||
AppEventBus.emit(AppEvent.RemoteServerError)
|
||||
}
|
||||
|
||||
onAuthFailed = { ip ->
|
||||
Log.w(TAG, "RemoteControlServer: Multiple failed auth attempts from $ip")
|
||||
val intent = Intent(Keys.ACTION_REMOTE_AUTH_FAILED).apply {
|
||||
putExtra("REMOTE_IP", ip)
|
||||
}
|
||||
LocalBroadcastManager.getInstance(this@PlayerService).sendBroadcast(intent)
|
||||
AppEventBus.emit(AppEvent.RemoteAuthFailed(ip))
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -108,17 +103,28 @@ class PlayerService : MediaLibraryService(), SharedPreferences.OnSharedPreferenc
|
||||
// Native Audio Processor instance
|
||||
private val nativeAudioProcessor = NativeAudioProcessor()
|
||||
|
||||
// Coroutine scope for service
|
||||
private val serviceJob = SupervisorJob()
|
||||
private val serviceScope = CoroutineScope(Main + serviceJob)
|
||||
|
||||
|
||||
/* Overrides onCreate from Service */
|
||||
override fun onCreate() {
|
||||
super.onCreate()
|
||||
// load collection
|
||||
collection = FileHelper.readCollection(this)
|
||||
// create and register collection changed receiver
|
||||
LocalBroadcastManager.getInstance(application).registerReceiver(
|
||||
collectionChangedReceiver,
|
||||
IntentFilter(Keys.ACTION_COLLECTION_CHANGED)
|
||||
)
|
||||
// observe collection changed events
|
||||
serviceScope.launch {
|
||||
AppEventBus.events.collect { event ->
|
||||
if (event is AppEvent.CollectionChanged) {
|
||||
val date = Date(event.modificationDateMillis)
|
||||
if (date.after(collection.modificationDate)) {
|
||||
Log.v(TAG, "PlayerService - reload collection after event received.")
|
||||
loadCollection(this@PlayerService)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// initialize player and session
|
||||
initializePlayer()
|
||||
initializeSession()
|
||||
@@ -144,6 +150,7 @@ class PlayerService : MediaLibraryService(), SharedPreferences.OnSharedPreferenc
|
||||
|
||||
/* Overrides onDestroy from Service */
|
||||
override fun onDestroy() {
|
||||
serviceJob.cancel()
|
||||
// unregister preference change listener
|
||||
PreferencesHelper.unregisterPreferenceChangeListener(this)
|
||||
|
||||
@@ -792,23 +799,6 @@ class PlayerService : MediaLibraryService(), SharedPreferences.OnSharedPreferenc
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Custom receiver that handles Keys.ACTION_COLLECTION_CHANGED
|
||||
*/
|
||||
private val collectionChangedReceiver: BroadcastReceiver = object : BroadcastReceiver() {
|
||||
override fun onReceive(context: Context, intent: Intent) {
|
||||
if (intent.hasExtra(Keys.EXTRA_COLLECTION_MODIFICATION_DATE)) {
|
||||
val date = Date(intent.getLongExtra(Keys.EXTRA_COLLECTION_MODIFICATION_DATE, 0L))
|
||||
|
||||
if (date.after(collection.modificationDate)) {
|
||||
Log.v(TAG, "PlayerService - reload collection after broadcast received.")
|
||||
loadCollection(context)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Custom AnalyticsListener that enables AudioFX equalizer integration
|
||||
*/
|
||||
|
||||
@@ -1,20 +1,16 @@
|
||||
package com.michatec.radio.collection
|
||||
|
||||
import android.app.Application
|
||||
import android.content.BroadcastReceiver
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.content.IntentFilter
|
||||
import android.util.Log
|
||||
import androidx.lifecycle.AndroidViewModel
|
||||
import androidx.lifecycle.MutableLiveData
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import androidx.localbroadcastmanager.content.LocalBroadcastManager
|
||||
import com.michatec.radio.Keys
|
||||
import com.michatec.radio.core.Collection
|
||||
import com.michatec.radio.helpers.AppEvent
|
||||
import com.michatec.radio.helpers.AppEventBus
|
||||
import com.michatec.radio.helpers.FileHelper
|
||||
import kotlinx.coroutines.launch
|
||||
import java.util.*
|
||||
import java.util.Date
|
||||
|
||||
|
||||
/*
|
||||
@@ -29,42 +25,21 @@ class CollectionViewModel(application: Application) : AndroidViewModel(applicati
|
||||
val collectionLiveData: MutableLiveData<Collection> = MutableLiveData<Collection>()
|
||||
val collectionSizeLiveData: MutableLiveData<Int> = MutableLiveData<Int>()
|
||||
private var modificationDateViewModel: Date = Date()
|
||||
private var collectionChangedReceiver: BroadcastReceiver
|
||||
|
||||
|
||||
/* Init constructor */
|
||||
init {
|
||||
// load collection
|
||||
loadCollection()
|
||||
// create and register collection changed receiver
|
||||
collectionChangedReceiver = createCollectionChangedReceiver()
|
||||
LocalBroadcastManager.getInstance(application).registerReceiver(
|
||||
collectionChangedReceiver,
|
||||
IntentFilter(Keys.ACTION_COLLECTION_CHANGED)
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
/* Overrides onCleared */
|
||||
override fun onCleared() {
|
||||
super.onCleared()
|
||||
LocalBroadcastManager.getInstance(getApplication())
|
||||
.unregisterReceiver(collectionChangedReceiver)
|
||||
}
|
||||
|
||||
|
||||
/* Creates the collectionChangedReceiver - handles Keys.ACTION_COLLECTION_CHANGED */
|
||||
private fun createCollectionChangedReceiver(): BroadcastReceiver {
|
||||
return object : BroadcastReceiver() {
|
||||
override fun onReceive(context: Context, intent: Intent) {
|
||||
if (intent.hasExtra(Keys.EXTRA_COLLECTION_MODIFICATION_DATE)) {
|
||||
val date =
|
||||
Date(intent.getLongExtra(Keys.EXTRA_COLLECTION_MODIFICATION_DATE, 0L))
|
||||
// check if reload is necessary
|
||||
// observe collection changed events
|
||||
viewModelScope.launch {
|
||||
AppEventBus.events.collect { event ->
|
||||
if (event is AppEvent.CollectionChanged) {
|
||||
val date = Date(event.modificationDateMillis)
|
||||
if (date.after(modificationDateViewModel)) {
|
||||
Log.v(
|
||||
TAG,
|
||||
"CollectionViewModel - reload collection after broadcast received."
|
||||
"CollectionViewModel - reload collection after event received."
|
||||
)
|
||||
loadCollection()
|
||||
}
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.michatec.radio.helpers
|
||||
|
||||
import kotlinx.coroutines.flow.MutableSharedFlow
|
||||
import kotlinx.coroutines.flow.SharedFlow
|
||||
import kotlinx.coroutines.flow.asSharedFlow
|
||||
|
||||
sealed interface AppEvent {
|
||||
data class CollectionChanged(val modificationDateMillis: Long) : AppEvent
|
||||
data object RemoteServerError : AppEvent
|
||||
data class RemoteAuthFailed(val ip: String) : AppEvent
|
||||
}
|
||||
|
||||
object AppEventBus {
|
||||
private val _events = MutableSharedFlow<AppEvent>(extraBufferCapacity = 64)
|
||||
val events: SharedFlow<AppEvent> = _events.asSharedFlow()
|
||||
|
||||
fun emit(event: AppEvent) {
|
||||
_events.tryEmit(event)
|
||||
}
|
||||
}
|
||||
@@ -9,7 +9,6 @@ import android.util.Log
|
||||
import android.widget.Toast
|
||||
import androidx.core.net.toFile
|
||||
import androidx.core.net.toUri
|
||||
import androidx.localbroadcastmanager.content.LocalBroadcastManager
|
||||
import androidx.media3.common.MediaItem
|
||||
import androidx.media3.common.MediaMetadata
|
||||
import com.michatec.radio.Keys
|
||||
@@ -621,16 +620,18 @@ object CollectionHelper {
|
||||
/* Sends a broadcast containing the collection as parcel */
|
||||
fun sendCollectionBroadcast(context: Context, modificationDate: Date) {
|
||||
Log.v(TAG, "Broadcasting that collection has changed.")
|
||||
val collectionChangedIntent = Intent()
|
||||
collectionChangedIntent.action = Keys.ACTION_COLLECTION_CHANGED
|
||||
collectionChangedIntent.setPackage(context.packageName)
|
||||
collectionChangedIntent.putExtra(
|
||||
Keys.EXTRA_COLLECTION_MODIFICATION_DATE,
|
||||
modificationDate.time
|
||||
)
|
||||
// Send local broadcast
|
||||
LocalBroadcastManager.getInstance(context).sendBroadcast(collectionChangedIntent)
|
||||
// Emit app event bus event
|
||||
AppEventBus.emit(AppEvent.CollectionChanged(modificationDate.time))
|
||||
|
||||
// Send global broadcast for widgets
|
||||
val collectionChangedIntent = Intent().apply {
|
||||
action = Keys.ACTION_COLLECTION_CHANGED
|
||||
setPackage(context.packageName)
|
||||
putExtra(
|
||||
Keys.EXTRA_COLLECTION_MODIFICATION_DATE,
|
||||
modificationDate.time
|
||||
)
|
||||
}
|
||||
context.sendBroadcast(collectionChangedIntent)
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user