Skip to content

Commit

Permalink
Using semaphore rather than synchronizing on generic object.
Browse files Browse the repository at this point in the history
  • Loading branch information
cafesilencio committed Jul 8, 2024
1 parent 9b40acb commit 0f4d961
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package com.mapbox.navigation.examples.mincompile
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import com.mapbox.navigation.examples.mincompile.databinding.LayoutActivityDropinBinding
import com.mapbox.navigation.examples.mincompile.databinding.LayoutActivityMainBinding
import com.mapbox.navigation.examples.mincompile.databinding.LayoutActivityNavigationBinding

class DropInUIActivity : AppCompatActivity() {
private lateinit var binding: LayoutActivityDropinBinding
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package com.mapbox.navigation.base.internal.route

import android.util.Log
import android.util.LruCache
import com.mapbox.api.directions.v5.models.DirectionsRoute
import com.mapbox.navigation.base.route.NavigationRoute

/**
* **Internal** cache object that aims to improve the performance performance of compatibility functions
* **Internal** cache object that aims to improve the performance of compatibility functions
* that transform the [DirectionsRoute] route into a [NavigationRoute].
*
* It caches up to 3 randomly created [NavigationRoute] instances,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package com.mapbox.navigation.core

import android.Manifest.permission.ACCESS_COARSE_LOCATION
import android.Manifest.permission.ACCESS_FINE_LOCATION
import android.util.Log
import androidx.annotation.RequiresPermission
import androidx.annotation.UiThread
import androidx.annotation.VisibleForTesting
Expand Down Expand Up @@ -54,6 +55,7 @@ import com.mapbox.navigation.core.arrival.AutoArrivalController
import com.mapbox.navigation.core.directions.LegacyNavigationRouterAdapter
import com.mapbox.navigation.core.directions.LegacyRouterAdapter
import com.mapbox.navigation.core.directions.session.DirectionsSession
import com.mapbox.navigation.core.directions.session.DirectionsSessionRoutes
import com.mapbox.navigation.core.directions.session.RoutesExtra
import com.mapbox.navigation.core.directions.session.RoutesObserver
import com.mapbox.navigation.core.directions.session.RoutesSetStartedParams
Expand Down Expand Up @@ -353,6 +355,8 @@ class MapboxNavigation @VisibleForTesting internal constructor(
*/
private val navigationVersionSwitchObservers = mutableSetOf<NavigationVersionSwitchObserver>()

private var directionsSessionRoutesToCache: DirectionsSessionRoutes? = null

/**
* [MapboxNavigation.roadObjectsStore] provides methods to get road objects metadata,
* add and remove custom road objects.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,18 @@ internal class MapboxDirectionsSession(
) {
return
}
RouteCompatibilityCache.setDirectionsSessionResult(routes.acceptedRoutes)

// A crash was reported due to an LruCache inconsistency.
// The caching implementation is using java.concurrent
// classes for synchronization. This method is getting called
// from a coroutine. Putting this call on it's own thread
// should make the synchronization in the RouteCompatibilityCache
// effective.
Thread {
run {
RouteCompatibilityCache.setDirectionsSessionResult(routes.acceptedRoutes)
}
}

val result = routes.toRoutesUpdatedResult().also { routesUpdatedResult = it }
onSetNavigationRoutesFinishedObservers.forEach {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1821,7 +1821,6 @@ internal class MapboxNavigationTest : MapboxNavigationBaseTest() {
alternativeWithId("id#2")
)
val acceptedRoutes = listOf(inputRoutes[0], inputRoutes[2])
val ignoredRoutes = listOf(IgnoredRoute(inputRoutes[1], invalidRouteReason))
coEvery {
tripSession.setRoutes(
inputRoutes,
Expand All @@ -1848,9 +1847,6 @@ internal class MapboxNavigationTest : MapboxNavigationBaseTest() {

coVerifyOrder {
tripSession.setRoutes(inputRoutes, setRoutesInfo)
directionsSession.setNavigationRoutesFinished(
DirectionsSessionRoutes(acceptedRoutes, ignoredRoutes, setRoutesInfo)
)
tripSession.setRoutes(acceptedRoutes, setRoutesInfo)
}

Expand Down

0 comments on commit 0f4d961

Please sign in to comment.