react-native-audio-browser 0.2.0-next.46 → 0.2.0-next.48
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/android/gradle.properties +1 -1
- package/android/src/main/java/com/audiobrowser/player/MediaSessionCallback.kt +16 -0
- package/android/src/main/java/com/audiobrowser/util/NetworkConnectivityMonitor.kt +24 -49
- package/android/src/test/java/com/audiobrowser/LegacyRatingTest.kt +97 -0
- package/android/src/test/java/com/audiobrowser/util/NetworkConnectivityMonitorTest.kt +99 -0
- package/ios/HybridAudioBrowser.swift +1 -2
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -34,7 +34,7 @@ Use the same tree in your app UI, or keep it focused on CarPlay and Android Auto
|
|
|
34
34
|
## Requirements
|
|
35
35
|
|
|
36
36
|
- React Native **0.76+** with the **New Architecture** (Fabric + TurboModules)
|
|
37
|
-
- iOS **16+** · Android **API
|
|
37
|
+
- iOS **16+** · Android **API 24+**
|
|
38
38
|
- [`react-native-nitro-modules`](https://nitro.margelo.com) (peer dependency)
|
|
39
39
|
|
|
40
40
|
## Installation
|
|
@@ -213,6 +213,22 @@ class MediaSessionCallback(private val player: Player) :
|
|
|
213
213
|
return super.onSetRating(session, controller, rating)
|
|
214
214
|
}
|
|
215
215
|
|
|
216
|
+
/**
|
|
217
|
+
* Where a legacy controller's `setRating` arrives, naming the current item, and a Media3
|
|
218
|
+
* controller's `setRating(mediaId, rating)`, naming any item: only the current one can be rated.
|
|
219
|
+
*/
|
|
220
|
+
override fun onSetRating(
|
|
221
|
+
session: MediaSession,
|
|
222
|
+
controller: MediaSession.ControllerInfo,
|
|
223
|
+
mediaId: String,
|
|
224
|
+
rating: Rating,
|
|
225
|
+
): ListenableFuture<SessionResult> =
|
|
226
|
+
if (mediaId == session.player.currentMediaItem?.mediaId) {
|
|
227
|
+
onSetRating(session, controller, rating)
|
|
228
|
+
} else {
|
|
229
|
+
super.onSetRating(session, controller, mediaId, rating)
|
|
230
|
+
}
|
|
231
|
+
|
|
216
232
|
@androidx.annotation.OptIn(androidx.media3.common.util.UnstableApi::class)
|
|
217
233
|
override fun onGetLibraryRoot(
|
|
218
234
|
session: MediaLibraryService.MediaLibrarySession,
|
|
@@ -4,7 +4,6 @@ import android.content.Context
|
|
|
4
4
|
import android.net.ConnectivityManager
|
|
5
5
|
import android.net.Network
|
|
6
6
|
import android.net.NetworkCapabilities
|
|
7
|
-
import android.net.NetworkRequest
|
|
8
7
|
import kotlinx.coroutines.CoroutineScope
|
|
9
8
|
import kotlinx.coroutines.flow.MutableStateFlow
|
|
10
9
|
import kotlinx.coroutines.flow.StateFlow
|
|
@@ -14,12 +13,8 @@ import kotlinx.coroutines.flow.onEach
|
|
|
14
13
|
import timber.log.Timber
|
|
15
14
|
|
|
16
15
|
/**
|
|
17
|
-
*
|
|
18
|
-
*
|
|
19
|
-
* This class uses Android's ConnectivityManager.NetworkCallback to detect when the device goes
|
|
20
|
-
* online or offline, which is useful for updating UI in Android Auto and other contexts.
|
|
21
|
-
*
|
|
22
|
-
* Monitoring starts automatically on construction.
|
|
16
|
+
* Whether the device is online: whether its default network has INTERNET and VALIDATED. Monitoring
|
|
17
|
+
* starts on construction.
|
|
23
18
|
*/
|
|
24
19
|
class NetworkConnectivityMonitor(private val context: Context) {
|
|
25
20
|
private val connectivityManager =
|
|
@@ -27,15 +22,11 @@ class NetworkConnectivityMonitor(private val context: Context) {
|
|
|
27
22
|
|
|
28
23
|
private var destroyed = false
|
|
29
24
|
|
|
30
|
-
|
|
31
|
-
|
|
25
|
+
@Volatile private var defaultNetwork: Network? = connectivityManager.activeNetwork
|
|
26
|
+
|
|
27
|
+
private val _isOnline = MutableStateFlow(isOnline(defaultNetwork?.let(::capabilitiesOf)))
|
|
32
28
|
val isOnline: StateFlow<Boolean> = _isOnline.asStateFlow()
|
|
33
29
|
|
|
34
|
-
/**
|
|
35
|
-
* Gets the current network connectivity state.
|
|
36
|
-
*
|
|
37
|
-
* @return true if device is online, false otherwise
|
|
38
|
-
*/
|
|
39
30
|
fun getOnline(): Boolean = _isOnline.value
|
|
40
31
|
|
|
41
32
|
/**
|
|
@@ -46,56 +37,40 @@ class NetworkConnectivityMonitor(private val context: Context) {
|
|
|
46
37
|
isOnline.onEach { isOnline -> onStateChanged(isOnline) }.launchIn(scope)
|
|
47
38
|
}
|
|
48
39
|
|
|
49
|
-
private fun
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
}
|
|
40
|
+
private fun capabilitiesOf(network: Network): NetworkCapabilities? =
|
|
41
|
+
connectivityManager.getNetworkCapabilities(network)
|
|
42
|
+
|
|
43
|
+
private fun isOnline(capabilities: NetworkCapabilities?): Boolean =
|
|
44
|
+
capabilities != null &&
|
|
45
|
+
capabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET) &&
|
|
46
|
+
capabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED)
|
|
57
47
|
|
|
58
48
|
private val networkCallback =
|
|
59
49
|
object : ConnectivityManager.NetworkCallback() {
|
|
60
50
|
override fun onAvailable(network: Network) {
|
|
61
|
-
Timber.d("
|
|
62
|
-
|
|
51
|
+
Timber.d("Default network available: $network")
|
|
52
|
+
defaultNetwork = network
|
|
53
|
+
_isOnline.value = isOnline(capabilitiesOf(network))
|
|
63
54
|
}
|
|
64
55
|
|
|
65
56
|
override fun onLost(network: Network) {
|
|
66
57
|
Timber.d("Network lost: $network")
|
|
67
|
-
|
|
58
|
+
if (network != defaultNetwork) return
|
|
59
|
+
defaultNetwork = null
|
|
60
|
+
_isOnline.value = false
|
|
68
61
|
}
|
|
69
62
|
|
|
70
|
-
override fun onCapabilitiesChanged(
|
|
71
|
-
network
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
networkCapabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)
|
|
76
|
-
val isValidated =
|
|
77
|
-
networkCapabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED)
|
|
78
|
-
Timber.d("Network capabilities changed: hasInternet=$hasInternet, isValidated=$isValidated")
|
|
79
|
-
updateNetworkState(hasInternet && isValidated)
|
|
63
|
+
override fun onCapabilitiesChanged(network: Network, capabilities: NetworkCapabilities) {
|
|
64
|
+
if (network != defaultNetwork) return
|
|
65
|
+
val online = isOnline(capabilities)
|
|
66
|
+
Timber.d("Default network $network capabilities changed: online=$online")
|
|
67
|
+
_isOnline.value = online
|
|
80
68
|
}
|
|
81
69
|
}
|
|
82
70
|
|
|
83
|
-
private fun updateNetworkState(isOnline: Boolean) {
|
|
84
|
-
if (_isOnline.value != isOnline) {
|
|
85
|
-
_isOnline.value = isOnline
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
|
|
89
71
|
init {
|
|
90
72
|
try {
|
|
91
|
-
|
|
92
|
-
NetworkRequest.Builder()
|
|
93
|
-
.addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)
|
|
94
|
-
.addCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED)
|
|
95
|
-
.build()
|
|
96
|
-
|
|
97
|
-
connectivityManager.registerNetworkCallback(networkRequest, networkCallback)
|
|
98
|
-
|
|
73
|
+
connectivityManager.registerDefaultNetworkCallback(networkCallback)
|
|
99
74
|
Timber.d(
|
|
100
75
|
"Network monitoring started, initial state: ${if (_isOnline.value) "online" else "offline"}"
|
|
101
76
|
)
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
package com.audiobrowser
|
|
2
|
+
|
|
3
|
+
import android.content.ComponentName
|
|
4
|
+
import android.os.Binder
|
|
5
|
+
import android.os.Bundle
|
|
6
|
+
import android.os.Looper
|
|
7
|
+
import androidx.media3.common.HeartRating
|
|
8
|
+
import androidx.media3.session.MediaSession
|
|
9
|
+
import androidx.media3.session.SessionResult
|
|
10
|
+
import org.junit.After
|
|
11
|
+
import org.junit.Assert.assertEquals
|
|
12
|
+
import org.junit.Test
|
|
13
|
+
import org.junit.runner.RunWith
|
|
14
|
+
import org.robolectric.Robolectric
|
|
15
|
+
import org.robolectric.RobolectricTestRunner
|
|
16
|
+
import org.robolectric.RuntimeEnvironment
|
|
17
|
+
import org.robolectric.Shadows.shadowOf
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Media3 hands a legacy controller's `setRating` to the callback that names a media id, with the
|
|
21
|
+
* current item's id. Robolectric cannot route a MediaControllerCompat to the session, so these call
|
|
22
|
+
* that callback as Media3 does.
|
|
23
|
+
*/
|
|
24
|
+
@RunWith(RobolectricTestRunner::class)
|
|
25
|
+
class LegacyRatingTest {
|
|
26
|
+
|
|
27
|
+
private val controller = run {
|
|
28
|
+
val application = RuntimeEnvironment.getApplication()
|
|
29
|
+
shadowOf(application)
|
|
30
|
+
.setComponentNameAndServiceForBindService(
|
|
31
|
+
ComponentName(application, HeadlessTaskService::class.java),
|
|
32
|
+
Binder(),
|
|
33
|
+
)
|
|
34
|
+
Robolectric.buildService(Service::class.java).create()
|
|
35
|
+
}
|
|
36
|
+
private val service = controller.get()
|
|
37
|
+
private val player = service.player
|
|
38
|
+
private val legacyController =
|
|
39
|
+
MediaSession.ControllerInfo.createTestOnlyControllerInfo(
|
|
40
|
+
"legacy",
|
|
41
|
+
0,
|
|
42
|
+
0,
|
|
43
|
+
MediaSession.ControllerInfo.LEGACY_CONTROLLER_VERSION,
|
|
44
|
+
MediaSession.ControllerInfo.LEGACY_CONTROLLER_INTERFACE_VERSION,
|
|
45
|
+
false,
|
|
46
|
+
Bundle.EMPTY,
|
|
47
|
+
false,
|
|
48
|
+
)
|
|
49
|
+
private val session = service.onGetSession(legacyController)
|
|
50
|
+
|
|
51
|
+
@After
|
|
52
|
+
fun destroy() {
|
|
53
|
+
controller.destroy()
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
private fun idle() = shadowOf(Looper.getMainLooper()).idle()
|
|
57
|
+
|
|
58
|
+
private fun rate(mediaId: String, heart: Boolean): Int {
|
|
59
|
+
val result =
|
|
60
|
+
player
|
|
61
|
+
.getMediaSessionCallback()
|
|
62
|
+
.onSetRating(session, legacyController, mediaId, HeartRating(heart))
|
|
63
|
+
.get()
|
|
64
|
+
idle()
|
|
65
|
+
return result.resultCode
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
private fun currentMediaId() = session.player.currentMediaItem!!.mediaId
|
|
69
|
+
|
|
70
|
+
private fun loadTrack() {
|
|
71
|
+
controller.bind()
|
|
72
|
+
player.load(TestFixtures.track(id = "a", favorited = false))
|
|
73
|
+
idle()
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
@Test
|
|
77
|
+
fun `a rating of the current item sets its favorite and USER_RATING`() {
|
|
78
|
+
loadTrack()
|
|
79
|
+
|
|
80
|
+
assertEquals(SessionResult.RESULT_SUCCESS, rate(currentMediaId(), true))
|
|
81
|
+
assertEquals(true, player.currentTrack?.favorited)
|
|
82
|
+
assertEquals(HeartRating(true), session.player.mediaMetadata.userRating)
|
|
83
|
+
|
|
84
|
+
assertEquals(SessionResult.RESULT_SUCCESS, rate(currentMediaId(), false))
|
|
85
|
+
assertEquals(false, player.currentTrack?.favorited)
|
|
86
|
+
assertEquals(HeartRating(false), session.player.mediaMetadata.userRating)
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
@Test
|
|
90
|
+
fun `a rating of another item leaves the favorite`() {
|
|
91
|
+
loadTrack()
|
|
92
|
+
|
|
93
|
+
assertEquals(SessionResult.RESULT_ERROR_NOT_SUPPORTED, rate("b", true))
|
|
94
|
+
assertEquals(false, player.currentTrack?.favorited)
|
|
95
|
+
assertEquals(HeartRating(false), session.player.mediaMetadata.userRating)
|
|
96
|
+
}
|
|
97
|
+
}
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
package com.audiobrowser.util
|
|
2
|
+
|
|
3
|
+
import android.content.Context
|
|
4
|
+
import android.net.ConnectivityManager
|
|
5
|
+
import android.net.Network
|
|
6
|
+
import android.net.NetworkCapabilities
|
|
7
|
+
import org.junit.After
|
|
8
|
+
import org.junit.Assert.assertFalse
|
|
9
|
+
import org.junit.Assert.assertTrue
|
|
10
|
+
import org.junit.Before
|
|
11
|
+
import org.junit.Test
|
|
12
|
+
import org.junit.runner.RunWith
|
|
13
|
+
import org.robolectric.RobolectricTestRunner
|
|
14
|
+
import org.robolectric.RuntimeEnvironment
|
|
15
|
+
import org.robolectric.Shadows.shadowOf
|
|
16
|
+
import org.robolectric.shadows.ShadowConnectivityManager
|
|
17
|
+
import org.robolectric.shadows.ShadowNetwork
|
|
18
|
+
import org.robolectric.shadows.ShadowNetworkCapabilities
|
|
19
|
+
|
|
20
|
+
@RunWith(RobolectricTestRunner::class)
|
|
21
|
+
class NetworkConnectivityMonitorTest {
|
|
22
|
+
private lateinit var connectivityManager: ConnectivityManager
|
|
23
|
+
private lateinit var shadow: ShadowConnectivityManager
|
|
24
|
+
private lateinit var monitor: NetworkConnectivityMonitor
|
|
25
|
+
|
|
26
|
+
private val mobile = ShadowNetwork.newInstance(102)
|
|
27
|
+
private val wifi = ShadowNetwork.newInstance(103)
|
|
28
|
+
|
|
29
|
+
private val callback: ConnectivityManager.NetworkCallback
|
|
30
|
+
get() = shadow.networkCallbacks.single()
|
|
31
|
+
|
|
32
|
+
@Before
|
|
33
|
+
fun setUp() {
|
|
34
|
+
val context = RuntimeEnvironment.getApplication()
|
|
35
|
+
connectivityManager =
|
|
36
|
+
context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
|
|
37
|
+
shadow = shadowOf(connectivityManager)
|
|
38
|
+
shadow.setNetworkCapabilities(mobile, validated())
|
|
39
|
+
shadow.setNetworkCapabilities(wifi, validated())
|
|
40
|
+
monitor = NetworkConnectivityMonitor(context)
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
@After fun tearDown() = monitor.destroy()
|
|
44
|
+
|
|
45
|
+
private fun validated() =
|
|
46
|
+
ShadowNetworkCapabilities.newInstance().also {
|
|
47
|
+
shadowOf(it).addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)
|
|
48
|
+
shadowOf(it).addCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED)
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
private fun unvalidated() =
|
|
52
|
+
ShadowNetworkCapabilities.newInstance().also {
|
|
53
|
+
shadowOf(it).addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
private fun becomesDefault(network: Network) {
|
|
57
|
+
callback.onAvailable(network)
|
|
58
|
+
callback.onCapabilitiesChanged(network, connectivityManager.getNetworkCapabilities(network)!!)
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
@Test
|
|
62
|
+
fun `stays online when a network it moved away from is lost`() {
|
|
63
|
+
becomesDefault(mobile)
|
|
64
|
+
becomesDefault(wifi)
|
|
65
|
+
callback.onLost(mobile)
|
|
66
|
+
assertTrue(monitor.getOnline())
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
@Test
|
|
70
|
+
fun `goes offline when the default network is lost`() {
|
|
71
|
+
becomesDefault(wifi)
|
|
72
|
+
callback.onLost(wifi)
|
|
73
|
+
assertFalse(monitor.getOnline())
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
@Test
|
|
77
|
+
fun `is offline while the default network is not validated`() {
|
|
78
|
+
shadow.setNetworkCapabilities(wifi, unvalidated())
|
|
79
|
+
becomesDefault(wifi)
|
|
80
|
+
assertFalse(monitor.getOnline())
|
|
81
|
+
callback.onCapabilitiesChanged(wifi, validated())
|
|
82
|
+
assertTrue(monitor.getOnline())
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
@Test
|
|
86
|
+
fun `takes the capabilities of a new default network on its arrival`() {
|
|
87
|
+
shadow.setNetworkCapabilities(wifi, unvalidated())
|
|
88
|
+
becomesDefault(mobile)
|
|
89
|
+
callback.onAvailable(wifi)
|
|
90
|
+
assertFalse(monitor.getOnline())
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
@Test
|
|
94
|
+
fun `ignores capability changes of a network that is not the default`() {
|
|
95
|
+
becomesDefault(wifi)
|
|
96
|
+
callback.onCapabilitiesChanged(mobile, unvalidated())
|
|
97
|
+
assertTrue(monitor.getOnline())
|
|
98
|
+
}
|
|
99
|
+
}
|
|
@@ -269,9 +269,8 @@ public class HybridAudioBrowser: HybridAudioBrowserSpec, @unchecked Sendable {
|
|
|
269
269
|
networkMonitor.changed.addListener { [weak self] isOnline in
|
|
270
270
|
guard let self else { return }
|
|
271
271
|
MainActor.assumeIsolated {
|
|
272
|
-
guard let player = self.player else { return }
|
|
273
272
|
self.onOnlineChanged(isOnline)
|
|
274
|
-
player
|
|
273
|
+
self.player?.networkStatusDidChange(isOnline: isOnline)
|
|
275
274
|
}
|
|
276
275
|
}
|
|
277
276
|
browseGate.resolve = { [weak self] request in
|