Skip to content

Commit ca5276b

Browse files
Voice instructions caching using ResourceLoader. (#5790)
* Voice instructions caching using ResourceLoader. - Updated libnavui-voice to load audio data using libnavui-util ResourceLoader * Addressing PR comments. - updated LICENSE file - updated generate-license.py script to inject okhttp license - reverted changes to examples/build.gradle and libnavui-voice/build.gradle - patched DefaultResourceLoader to fail fast when device is offline and request requires internet connection - added missing retrieveVoiceFile failure tests * Addressing PR feedback. Propagating VoiceInstructionsParser error as IllegalStateException.
1 parent 99e8328 commit ca5276b

File tree

24 files changed

+868
-1017
lines changed

24 files changed

+868
-1017
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ Mapbox welcomes participation and contributions from everyone.
66
#### Features
77

88
#### Bug fixes and improvements
9+
- Updated `MapboxSpeechApi` to use persistent cache to decrease the bandwidth consumption. [#5790](https://github.com/mapbox/mapbox-navigation-android/pull/5790)
910

1011
## Mapbox Navigation SDK 2.6.0-rc.1 - June 23, 2022
1112
### Changelog

LICENSE.md

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,20 @@ Mapbox Navigation for Android version 2.0 (“Mapbox Navigation Android SDK“)
1212
For the full license terms, please see the Mapbox Terms of Service at https://www.mapbox.com/legal/tos/
1313

1414
---------------------------------------
15-
URL: [Gradle License Plugin](https://github.com/jaredsburrows/gradle-license-plugin)
15+
Mapbox Navigation uses portions of the Gradle License Plugin.
16+
URL: [https://github.com/jaredsburrows/gradle-license-plugin](https://github.com/jaredsburrows/gradle-license-plugin)
1617
License: [The Apache Software License, Version 2.0](http://www.apache.org/licenses/LICENSE-2.0.txt)
1718

19+
===========================================================================
20+
21+
Mapbox Navigation uses portions of the okhttp (Square’s meticulous HTTP client for Java and Kotlin.).
22+
URL: [https://square.github.io/okhttp/](https://square.github.io/okhttp/)
23+
License: [The Apache Software License, Version 2.0](http://www.apache.org/licenses/LICENSE-2.0.txt)
24+
25+
===========================================================================
26+
27+
28+
1829
#### Hybrid Router SDK module
1930
Mapbox Navigation uses portions of the Android Support Library Annotations (The Support Library is a static library that you can add to your Android application in order to use APIs that are either not available for older platform versions or utility APIs that aren't a part of the framework APIs.).
2031
URL: [http://developer.android.com/tools/extras/support-library.html](http://developer.android.com/tools/extras/support-library.html)
@@ -3610,6 +3621,22 @@ License: [The Apache Software License, Version 2.0](http://www.apache.org/licens
36103621

36113622
===========================================================================
36123623

3624+
Mapbox Navigation uses portions of the auto-value-gson-runtime.
3625+
License: [The Apache Software License, Version 2.0](http://www.apache.org/licenses/LICENSE-2.0.txt)
3626+
3627+
===========================================================================
3628+
3629+
Mapbox Navigation uses portions of the autotransient (A transient annotation for AutoValue extensions.).
3630+
URL: [https://github.com/ZacSweers/AutoTransient/](https://github.com/ZacSweers/AutoTransient/)
3631+
License: [The Apache Software License, Version 2.0](http://www.apache.org/licenses/LICENSE-2.0.txt)
3632+
3633+
===========================================================================
3634+
3635+
Mapbox Navigation uses portions of the Converter: Gson.
3636+
License: [Apache 2.0](https://www.apache.org/licenses/LICENSE-2.0.txt)
3637+
3638+
===========================================================================
3639+
36133640
Mapbox Navigation uses portions of the Core Kotlin Extensions (Kotlin extensions for 'core' artifact).
36143641
URL: [https://developer.android.com/jetpack/androidx/releases/core#1.6.0](https://developer.android.com/jetpack/androidx/releases/core#1.6.0)
36153642
License: [The Apache Software License, Version 2.0](http://www.apache.org/licenses/LICENSE-2.0.txt)
@@ -3622,6 +3649,11 @@ License: [The Apache Software License, Version 2.0](http://www.apache.org/licens
36223649

36233650
===========================================================================
36243651

3652+
Mapbox Navigation uses portions of the Gson.
3653+
License: [Apache 2.0](http://www.apache.org/licenses/LICENSE-2.0.txt)
3654+
3655+
===========================================================================
3656+
36253657
Mapbox Navigation uses portions of the IntelliJ IDEA Annotations (A set of annotations used for code inspection support and code documentation.).
36263658
URL: [http://www.jetbrains.org](http://www.jetbrains.org)
36273659
License: [The Apache Software License, Version 2.0](http://www.apache.org/licenses/LICENSE-2.0.txt)
@@ -3676,12 +3708,41 @@ License: [The Apache Software License, Version 2.0](http://www.apache.org/licens
36763708

36773709
===========================================================================
36783710

3711+
Mapbox Navigation uses portions of the Mapbox Java SDK.
3712+
URL: [https://github.com/mapbox/mapbox-java](https://github.com/mapbox/mapbox-java)
3713+
License: [The Apache Software License, Version 2.0](http://www.apache.org/licenses/LICENSE-2.0.txt)
3714+
3715+
===========================================================================
3716+
36793717
Mapbox Navigation uses portions of the Material Components for Android (Material Components for Android is a static library that you can add to your Android application in order to use APIs that provide implementations of the Material Design specification. Compatible on devices running API 14 or later.).
36803718
URL: [http://developer.android.com/tools/extras/support-library.html](http://developer.android.com/tools/extras/support-library.html)
36813719
License: [The Apache Software License, Version 2.0](http://www.apache.org/licenses/LICENSE-2.0.txt)
36823720

36833721
===========================================================================
36843722

3723+
Mapbox Navigation uses portions of the okhttp (Square’s meticulous HTTP client for Java and Kotlin.).
3724+
URL: [https://square.github.io/okhttp/](https://square.github.io/okhttp/)
3725+
License: [The Apache Software License, Version 2.0](http://www.apache.org/licenses/LICENSE-2.0.txt)
3726+
3727+
===========================================================================
3728+
3729+
Mapbox Navigation uses portions of the okhttp-logging-interceptor (Square’s meticulous HTTP client for Java and Kotlin.).
3730+
URL: [https://square.github.io/okhttp/](https://square.github.io/okhttp/)
3731+
License: [The Apache Software License, Version 2.0](http://www.apache.org/licenses/LICENSE-2.0.txt)
3732+
3733+
===========================================================================
3734+
3735+
Mapbox Navigation uses portions of the Okio (A modern I/O API for Java).
3736+
URL: [https://github.com/square/okio/](https://github.com/square/okio/)
3737+
License: [The Apache Software License, Version 2.0](http://www.apache.org/licenses/LICENSE-2.0.txt)
3738+
3739+
===========================================================================
3740+
3741+
Mapbox Navigation uses portions of the Retrofit.
3742+
License: [Apache 2.0](https://www.apache.org/licenses/LICENSE-2.0.txt)
3743+
3744+
===========================================================================
3745+
36853746
Mapbox Navigation uses portions of the VersionedParcelable (Provides a stable but relatively compact binary serialization format that can be passed across processes or persisted safely.).
36863747
URL: [http://developer.android.com/tools/extras/support-library.html](http://developer.android.com/tools/extras/support-library.html)
36873748
License: [The Apache Software License, Version 2.0](http://www.apache.org/licenses/LICENSE-2.0.txt)

libnavui-dropin/build.gradle

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ dependencies {
4949
api project(":libnavui-tripprogress")
5050
api project(":libnavui-voice")
5151
implementation project(":libnavui-app")
52+
53+
api dependenciesList.mapboxSdkServices
54+
5255
implementation project(":libnavui-resources")
5356
implementation project(":libnavui-util")
5457

libnavui-util/src/main/java/com/mapbox/navigation/ui/utils/internal/resource/DefaultResourceLoader.kt

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
package com.mapbox.navigation.ui.utils.internal.resource
22

33
import com.mapbox.bindgen.Expected
4+
import com.mapbox.bindgen.ExpectedFactory.createError
45
import com.mapbox.common.Cancelable
6+
import com.mapbox.common.NetworkRestriction
7+
import com.mapbox.common.ReachabilityInterface
58
import com.mapbox.common.ResourceDescription
69
import com.mapbox.common.ResourceLoadError
10+
import com.mapbox.common.ResourceLoadErrorType
711
import com.mapbox.common.ResourceLoadOptions
812
import com.mapbox.common.ResourceLoadProgress
913
import com.mapbox.common.ResourceLoadProgressCallback
@@ -25,7 +29,8 @@ import java.util.concurrent.atomic.AtomicLong
2529
* abort load operation.
2630
*/
2731
internal class DefaultResourceLoader(
28-
private val tileStore: TileStore
32+
private val tileStore: TileStore,
33+
private val reachability: ReachabilityInterface
2934
) : ResourceLoader() {
3035

3136
private val nextRequestId = AtomicLong(0L)
@@ -43,20 +48,33 @@ internal class DefaultResourceLoader(
4348
): Long {
4449
val requestId = nextRequestId.incrementAndGet()
4550
val callbackAdapter = CallbackAdapter(request, callback, observers)
51+
val requiresNetwork = request.networkRestriction != NetworkRestriction.DISALLOW_ALL
4652

4753
callbackAdapter.notifyOnStart(request)
48-
cancelableMap[requestId] = tileStore.loadResource(
49-
/* description */ request.toResourceDescription(),
50-
/* options */ request.toResourceLoadOptions("DefaultResourceLoader-$requestId"),
51-
/* progressCallback */ callbackAdapter
52-
) {
53-
cancelableMap.remove(requestId)
54-
callbackAdapter.run(it)
54+
// Since the TileStore (commonSDK 21.3.1) will defer any requests that require internet connection,
55+
// we must verify network requirement here and fail fast if the network is not available.
56+
if (requiresNetwork && !reachability.isReachable) {
57+
callbackAdapter.run(createError(connectionError()))
58+
} else {
59+
cancelableMap[requestId] = tileStore.loadResource(
60+
/* description */ request.toResourceDescription(),
61+
/* options */ request.toResourceLoadOptions("DefaultResourceLoader-$requestId"),
62+
/* progressCallback */ callbackAdapter
63+
) {
64+
cancelableMap.remove(requestId)
65+
callbackAdapter.run(it)
66+
}
5567
}
5668

5769
return requestId
5870
}
5971

72+
private fun connectionError() = ResourceLoadError(
73+
ResourceLoadErrorType.UNSATISFIED,
74+
"No internet connection",
75+
0L
76+
)
77+
6078
override fun cancel(requestId: Long) {
6179
cancelableMap.remove(requestId)?.cancel()
6280
}

libnavui-util/src/main/java/com/mapbox/navigation/ui/utils/internal/resource/ResourceLoaderFactory.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.mapbox.navigation.ui.utils.internal.resource
22

3+
import com.mapbox.common.ReachabilityFactory
34
import com.mapbox.common.TileStore
45

56
/**
@@ -8,7 +9,10 @@ import com.mapbox.common.TileStore
89
object ResourceLoaderFactory {
910

1011
private val sharedLoader: DefaultResourceLoader by lazy {
11-
DefaultResourceLoader(TileStore.create())
12+
DefaultResourceLoader(
13+
TileStore.create(),
14+
ReachabilityFactory.reachability(null)
15+
)
1216
}
1317

1418
/**

libnavui-util/src/test/java/com/mapbox/navigation/ui/utils/internal/resource/DefaultResourceLoaderTest.kt

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import com.mapbox.bindgen.Expected
44
import com.mapbox.bindgen.ExpectedFactory
55
import com.mapbox.common.Cancelable
66
import com.mapbox.common.NetworkRestriction
7+
import com.mapbox.common.ReachabilityInterface
78
import com.mapbox.common.ResourceData
89
import com.mapbox.common.ResourceDescription
910
import com.mapbox.common.ResourceLoadError
@@ -23,19 +24,24 @@ import io.mockk.spyk
2324
import io.mockk.verify
2425
import io.mockk.verifyOrder
2526
import org.junit.Assert.assertEquals
27+
import org.junit.Assert.assertTrue
2628
import org.junit.Before
2729
import org.junit.Test
2830
import java.util.Date
2931

3032
class DefaultResourceLoaderTest {
3133

3234
private lateinit var mockTileStore: TileStore
35+
private lateinit var mockReachability: ReachabilityInterface
3336
private lateinit var sut: ResourceLoader
3437

3538
@Before
3639
fun setUp() {
3740
mockTileStore = mockk<StubTileStore>(relaxed = true)
38-
sut = DefaultResourceLoader(mockTileStore)
41+
mockReachability = mockk(relaxed = true) {
42+
every { isReachable } returns true
43+
}
44+
sut = DefaultResourceLoader(mockTileStore, mockReachability)
3945
}
4046

4147
@Test
@@ -99,6 +105,31 @@ class DefaultResourceLoaderTest {
99105
}
100106
}
101107

108+
@Test
109+
fun `load - should fail fast if request requires network and network is not reachable`() {
110+
val callback = mockk<ResourceLoadCallback>(relaxed = true)
111+
val loadRequest = ResourceLoadRequest("http://example.com/some-resource")
112+
every { mockReachability.isReachable } returns false
113+
sut.load(loadRequest, callback)
114+
115+
val errorCapture = slot<Expected<ResourceLoadError, ResourceLoadResult>>()
116+
verifyOrder {
117+
callback.onStart(loadRequest)
118+
callback.onFinish(loadRequest, capture(errorCapture))
119+
}
120+
assertTrue(errorCapture.captured.isError)
121+
}
122+
123+
@Test
124+
fun `load - should NOT call TileStore if request requires network and network is not reachable`() {
125+
val callback = mockk<ResourceLoadCallback>(relaxed = true)
126+
val loadRequest = ResourceLoadRequest("http://example.com/some-resource")
127+
every { mockReachability.isReachable } returns false
128+
sut.load(loadRequest, callback)
129+
130+
verify(exactly = 0) { mockTileStore.loadResource(any(), any(), any(), any()) }
131+
}
132+
102133
@Test
103134
fun `cancel - should call Cancelable`() {
104135
val loadRequest = ResourceLoadRequest(

libnavui-voice/src/main/java/com/mapbox/navigation/ui/voice/VoiceAction.kt

Lines changed: 0 additions & 21 deletions
This file was deleted.

libnavui-voice/src/main/java/com/mapbox/navigation/ui/voice/VoiceProcessor.kt

Lines changed: 0 additions & 72 deletions
This file was deleted.

libnavui-voice/src/main/java/com/mapbox/navigation/ui/voice/VoiceResult.kt

Lines changed: 0 additions & 24 deletions
This file was deleted.

0 commit comments

Comments
 (0)