From 04aa8d11605704bf8edc81b641cf4c4af930c3c3 Mon Sep 17 00:00:00 2001 From: johan12345 Date: Fri, 28 Jan 2022 22:46:44 +0100 Subject: [PATCH] Android Auto: implement experimental setOnContentRefreshListener API has no effect yet on Android Auto 7.3 --- .../java/net/vonforst/evmap/auto/MapScreen.kt | 32 ++++--------------- 1 file changed, 7 insertions(+), 25 deletions(-) diff --git a/app/src/google/java/net/vonforst/evmap/auto/MapScreen.kt b/app/src/google/java/net/vonforst/evmap/auto/MapScreen.kt index b13b8d68..ed1fd4cc 100644 --- a/app/src/google/java/net/vonforst/evmap/auto/MapScreen.kt +++ b/app/src/google/java/net/vonforst/evmap/auto/MapScreen.kt @@ -47,18 +47,12 @@ import kotlin.math.roundToInt /** * Main map screen showing either nearby chargers or favorites */ +@androidx.car.app.annotations.ExperimentalCarApi class MapScreen(ctx: CarContext, val session: EVMapSession, val favorites: Boolean = false) : - Screen(ctx), LocationAwareScreen { + Screen(ctx), LocationAwareScreen, OnContentRefreshListener { private var updateCoroutine: Job? = null - private var numUpdates = 0 - - /* Updating map contents is disabled - if the user uses Chargeprice from the charger - detail screen, this already means 4 steps, after which the app would crash. - follow https://issuetracker.google.com/issues/176694222 for updates how to solve this. */ - private val maxNumUpdates = 1 private var location: Location? = null - private var lastChargerUpdateLocation: Location? = null private var lastDistanceUpdateTime: Instant? = null private var chargers: List? = null private var prefs = PreferenceDataSource(ctx) @@ -67,7 +61,6 @@ class MapScreen(ctx: CarContext, val session: EVMapSession, val favorites: Boole createApi(prefs.dataSource, ctx) } private val searchRadius = 5 // kilometers - private val chargerUpdateThreshold = 2000 // meters private val distanceUpdateThreshold = Duration.ofSeconds(15) private val availabilityUpdateThreshold = Duration.ofMinutes(1) private var availabilities: MutableMap> = @@ -151,7 +144,6 @@ class MapScreen(ctx: CarContext, val session: EVMapSession, val favorites: Boole .setOnClickListener { screenManager.pushForResult(FilterScreen(carContext)) { chargers = null - numUpdates = 0 filterStatus.value = prefs.filterStatus.takeUnless { it == FILTERS_CUSTOM || it == FILTERS_FAVORITES } ?: FILTERS_DISABLED @@ -162,6 +154,7 @@ class MapScreen(ctx: CarContext, val session: EVMapSession, val favorites: Boole .build()) } build() + setOnContentRefreshListener(this@MapScreen) }.build() } @@ -264,14 +257,6 @@ class MapScreen(ctx: CarContext, val session: EVMapSession, val favorites: Boole // update displayed distances invalidate() } - - if (lastChargerUpdateLocation == null || - location.distanceTo(lastChargerUpdateLocation) > chargerUpdateThreshold - ) { - lastChargerUpdateLocation = location - // update displayed chargers - loadChargers() - } } private fun loadChargers() { @@ -279,13 +264,6 @@ class MapScreen(ctx: CarContext, val session: EVMapSession, val favorites: Boole val referenceData = referenceData.value ?: return val filters = filtersWithValue.value ?: return - numUpdates++ - println(numUpdates) - if (numUpdates > maxNumUpdates) { - /*CarToast.makeText(carContext, R.string.auto_no_refresh_possible, CarToast.LENGTH_LONG) - .show()*/ - return - } updateCoroutine = lifecycleScope.launch { try { // load chargers @@ -380,4 +358,8 @@ class MapScreen(ctx: CarContext, val session: EVMapSession, val favorites: Boole println("Removing energy level listener") hardwareMan.carInfo.removeEnergyLevelListener(::onEnergyLevelUpdated) } + + override fun onContentRefreshRequested() { + loadChargers() + } } \ No newline at end of file