Commit 4b6d909d authored by Geoffrey Métais's avatar Geoffrey Métais
Browse files

Replace ConflatedBroadcastChannels by StateFlows

parent 252ab303
Pipeline #17191 passed with stage
in 3 minutes and 41 seconds
...@@ -13,24 +13,20 @@ import androidx.lifecycle.Lifecycle ...@@ -13,24 +13,20 @@ import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleObserver import androidx.lifecycle.LifecycleObserver
import androidx.lifecycle.OnLifecycleEvent import androidx.lifecycle.OnLifecycleEvent
import androidx.lifecycle.ProcessLifecycleOwner import androidx.lifecycle.ProcessLifecycleOwner
import kotlinx.coroutines.channels.ConflatedBroadcastChannel import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.asFlow
import java.net.NetworkInterface import java.net.NetworkInterface
import java.net.SocketException import java.net.SocketException
class NetworkMonitor(private val context: Context) : LifecycleObserver { class NetworkMonitor(private val context: Context) : LifecycleObserver {
private var registered = false private var registered = false
private val cm = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager private val cm = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
val connection = ConflatedBroadcastChannel(Connection(connected = false, mobile = true, vpn = false)) val connectionFlow = MutableStateFlow(Connection(connected = false, mobile = true, vpn = false))
val connectionFlow : Flow<Connection>
get() = connection.asFlow()
val connected : Boolean val connected : Boolean
get() = connection.value.connected get() = connectionFlow.value.connected
val isLan : Boolean val isLan : Boolean
get() = connection.value.run { connected && !mobile } get() = connectionFlow.value.run { connected && !mobile }
val lanAllowed : Boolean val lanAllowed : Boolean
get() = connection.value.run { connected && (!mobile || vpn) } get() = connectionFlow.value.run { connected && (!mobile || vpn) }
val receiver = object : BroadcastReceiver() { val receiver = object : BroadcastReceiver() {
@SuppressLint("MissingPermission") @SuppressLint("MissingPermission")
override fun onReceive(context: Context?, intent: Intent?) { override fun onReceive(context: Context?, intent: Intent?) {
...@@ -40,8 +36,7 @@ class NetworkMonitor(private val context: Context) : LifecycleObserver { ...@@ -40,8 +36,7 @@ class NetworkMonitor(private val context: Context) : LifecycleObserver {
val isConnected = networkInfo != null && networkInfo.isConnected val isConnected = networkInfo != null && networkInfo.isConnected
val isMobile = isConnected && networkInfo!!.type == ConnectivityManager.TYPE_MOBILE val isMobile = isConnected && networkInfo!!.type == ConnectivityManager.TYPE_MOBILE
val isVPN = isConnected && updateVPNStatus() val isVPN = isConnected && updateVPNStatus()
val conn = Connection(isConnected, isMobile, isVPN) connectionFlow.value = Connection(isConnected, isMobile, isVPN)
if (connection.value != conn) connection.offer(conn)
} }
} }
......
...@@ -105,7 +105,7 @@ object ExternalMonitor : BroadcastReceiver(), LifecycleObserver, CoroutineScope ...@@ -105,7 +105,7 @@ object ExternalMonitor : BroadcastReceiver(), LifecycleObserver, CoroutineScope
} }
} }
UsbManager.ACTION_USB_DEVICE_DETACHED -> if (intent.hasExtra(UsbManager.EXTRA_DEVICE)) { UsbManager.ACTION_USB_DEVICE_DETACHED -> if (intent.hasExtra(UsbManager.EXTRA_DEVICE)) {
OtgAccess.otgRoot.offer(null) OtgAccess.otgRoot.value = null
val device = intent.getParcelableExtra<UsbDevice>(UsbManager.EXTRA_DEVICE) val device = intent.getParcelableExtra<UsbDevice>(UsbManager.EXTRA_DEVICE)
devices.remove(device) devices.remove(device)
} }
......
...@@ -52,11 +52,9 @@ import androidx.media.MediaBrowserServiceCompat ...@@ -52,11 +52,9 @@ import androidx.media.MediaBrowserServiceCompat
import androidx.media.session.MediaButtonReceiver import androidx.media.session.MediaButtonReceiver
import kotlinx.coroutines.* import kotlinx.coroutines.*
import kotlinx.coroutines.channels.Channel import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.channels.ConflatedBroadcastChannel
import kotlinx.coroutines.channels.SendChannel import kotlinx.coroutines.channels.SendChannel
import kotlinx.coroutines.channels.actor import kotlinx.coroutines.channels.actor
import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asFlow
import org.videolan.libvlc.MediaPlayer import org.videolan.libvlc.MediaPlayer
import org.videolan.libvlc.RendererItem import org.videolan.libvlc.RendererItem
import org.videolan.libvlc.interfaces.IMedia import org.videolan.libvlc.interfaces.IMedia
...@@ -496,7 +494,7 @@ class PlaybackService : MediaBrowserServiceCompat(), LifecycleOwner { ...@@ -496,7 +494,7 @@ class PlaybackService : MediaBrowserServiceCompat(), LifecycleOwner {
restartPlayer.observe(this, Observer { restartMediaPlayer() }) restartPlayer.observe(this, Observer { restartMediaPlayer() })
headSetDetection.observe(this, Observer { detectHeadset(it) }) headSetDetection.observe(this, Observer { detectHeadset(it) })
equalizer.observe(this, Observer { setEqualizer(it) }) equalizer.observe(this, Observer { setEqualizer(it) })
instanceChannel.safeOffer(this) serviceFlow.value = this
} }
private fun setupScope() { private fun setupScope() {
...@@ -564,7 +562,7 @@ class PlaybackService : MediaBrowserServiceCompat(), LifecycleOwner { ...@@ -564,7 +562,7 @@ class PlaybackService : MediaBrowserServiceCompat(), LifecycleOwner {
} }
override fun onDestroy() { override fun onDestroy() {
instanceChannel.safeOffer(null) serviceFlow.value = null
dispatcher.onServicePreSuperOnDestroy() dispatcher.onServicePreSuperOnDestroy()
super.onDestroy() super.onDestroy()
handler.removeCallbacksAndMessages(null) handler.removeCallbacksAndMessages(null)
...@@ -1316,11 +1314,9 @@ class PlaybackService : MediaBrowserServiceCompat(), LifecycleOwner { ...@@ -1316,11 +1314,9 @@ class PlaybackService : MediaBrowserServiceCompat(), LifecycleOwner {
} }
companion object { companion object {
private val instanceChannel = ConflatedBroadcastChannel<PlaybackService?>(null) val serviceFlow = MutableStateFlow<PlaybackService?>(null)
val instance : PlaybackService? val instance : PlaybackService?
get() = instanceChannel.valueOrNull get() = serviceFlow.value
val serviceFlow : Flow<PlaybackService?>
get() = instanceChannel.asFlow()
val renderer = RendererLiveData() val renderer = RendererLiveData()
val restartPlayer = LiveEvent<Boolean>() val restartPlayer = LiveEvent<Boolean>()
......
...@@ -118,8 +118,7 @@ open class FileBrowserFragment : BaseBrowserFragment() { ...@@ -118,8 +118,7 @@ open class FileBrowserFragment : BaseBrowserFragment() {
browseOtgDevice(rootUri, title) browseOtgDevice(rootUri, title)
} else { } else {
lifecycleScope.launchWhenStarted { lifecycleScope.launchWhenStarted {
val otgRoot = OtgAccess.otgRoot.asFlow() val uri = OtgAccess.otgRoot.filterNotNull().first()
val uri = otgRoot.filterNotNull().first()
browseOtgDevice(uri, title) browseOtgDevice(uri, title)
} }
requireActivity().requestOtgRoot() requireActivity().requestOtgRoot()
......
...@@ -31,10 +31,9 @@ import android.util.Log ...@@ -31,10 +31,9 @@ import android.util.Log
import androidx.annotation.WorkerThread import androidx.annotation.WorkerThread
import androidx.documentfile.provider.DocumentFile import androidx.documentfile.provider.DocumentFile
import androidx.fragment.app.FragmentActivity import androidx.fragment.app.FragmentActivity
import kotlinx.coroutines.channels.ConflatedBroadcastChannel import kotlinx.coroutines.flow.MutableStateFlow
import org.videolan.medialibrary.MLServiceLocator import org.videolan.medialibrary.MLServiceLocator
import org.videolan.medialibrary.interfaces.media.MediaWrapper import org.videolan.medialibrary.interfaces.media.MediaWrapper
import org.videolan.tools.safeOffer
const val SAF_REQUEST = 85 const val SAF_REQUEST = 85
const val TAG = "OtgAccess" const val TAG = "OtgAccess"
...@@ -58,13 +57,13 @@ class OtgAccess : BaseHeadlessFragment() { ...@@ -58,13 +57,13 @@ class OtgAccess : BaseHeadlessFragment() {
} }
override fun onActivityResult(requestCode: Int, resultCode: Int, intent: Intent?) { override fun onActivityResult(requestCode: Int, resultCode: Int, intent: Intent?) {
if (intent != null && requestCode == SAF_REQUEST) otgRoot.safeOffer(intent.data) if (intent != null && requestCode == SAF_REQUEST) otgRoot.value = intent.data
else super.onActivityResult(requestCode, resultCode, intent) else super.onActivityResult(requestCode, resultCode, intent)
exit() exit()
} }
companion object { companion object {
val otgRoot = ConflatedBroadcastChannel<Uri?>(null) val otgRoot = MutableStateFlow<Uri?>(null)
} }
} }
...@@ -74,7 +73,7 @@ fun FragmentActivity.requestOtgRoot() { ...@@ -74,7 +73,7 @@ fun FragmentActivity.requestOtgRoot() {
@WorkerThread @WorkerThread
fun getDocumentFiles(context: Context, path: String) : List<MediaWrapper>? { fun getDocumentFiles(context: Context, path: String) : List<MediaWrapper>? {
val rootUri = OtgAccess.otgRoot.valueOrNull ?: return null val rootUri = OtgAccess.otgRoot.value ?: return null
// else Uri.Builder().scheme("content") // else Uri.Builder().scheme("content")
// .authority(OTG_CONTENT_AUTHORITY) // .authority(OTG_CONTENT_AUTHORITY)
// .path(path.substringBefore(':')) // .path(path.substringBefore(':'))
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment