From bc9f7522c413268ec457d341b24ef76dd4ce0527 Mon Sep 17 00:00:00 2001 From: Fynn Godau Date: Mon, 20 Mar 2023 14:28:00 +0100 Subject: [PATCH] Workaround for custom styles in lite mode Upstream-maplibre: https://github.com/maplibre/maplibre-gl-native/issues/903 --- .../microg/gms/maps/mapbox/LiteGoogleMap.kt | 11 +----- .../org/microg/gms/maps/mapbox/Styles.kt | 39 +++++++++++++++++-- 2 files changed, 36 insertions(+), 14 deletions(-) diff --git a/play-services-maps-core-mapbox/src/main/kotlin/org/microg/gms/maps/mapbox/LiteGoogleMap.kt b/play-services-maps-core-mapbox/src/main/kotlin/org/microg/gms/maps/mapbox/LiteGoogleMap.kt index c5ad88440..9273740fe 100644 --- a/play-services-maps-core-mapbox/src/main/kotlin/org/microg/gms/maps/mapbox/LiteGoogleMap.kt +++ b/play-services-maps-core-mapbox/src/main/kotlin/org/microg/gms/maps/mapbox/LiteGoogleMap.kt @@ -250,8 +250,7 @@ class LiteGoogleMapImpl(context: Context, var options: GoogleMapOptions) : Abstr val pixelWidth = map.width val pixelHeight = map.height - val styleBuilder = Style.Builder().fromUri(getStyleUriByMapType(mapType)) - // TODO should be getStyle(mapContext, mapType, mapStyle) + val styleBuilder = getStyle(mapContext, mapType, mapStyle, styleFromFileWorkaround = true) // Add visible polygons (before polylines, so that they are drawn below their strokes) for (polygon in polygons.filter { it.isVisible }) { @@ -672,11 +671,3 @@ class LiteGoogleMapImpl(context: Context, var options: GoogleMapOptions) : Abstr } } -// TODO custom colors -fun getStyleUriByMapType(mapType: Int) = when (mapType) { - MapsConstants.MAP_TYPE_SATELLITE -> "mapbox://styles/microg/cjxgloted25ap1ct4uex7m6hi" - MapsConstants.MAP_TYPE_TERRAIN -> "mapbox://styles/mapbox/outdoors-v12" - MapsConstants.MAP_TYPE_HYBRID -> "mapbox://styles/microg/cjxgloted25ap1ct4uex7m6hi" - //MAP_TYPE_NONE, MAP_TYPE_NORMAL, - else -> "mapbox://styles/microg/cjui4020201oo1fmca7yuwbor" -} diff --git a/play-services-maps-core-mapbox/src/main/kotlin/org/microg/gms/maps/mapbox/Styles.kt b/play-services-maps-core-mapbox/src/main/kotlin/org/microg/gms/maps/mapbox/Styles.kt index 5cd4f8d47..b1109160d 100644 --- a/play-services-maps-core-mapbox/src/main/kotlin/org/microg/gms/maps/mapbox/Styles.kt +++ b/play-services-maps-core-mapbox/src/main/kotlin/org/microg/gms/maps/mapbox/Styles.kt @@ -14,6 +14,8 @@ import org.json.JSONArray import org.json.JSONObject import org.microg.gms.maps.MapsConstants import org.microg.gms.maps.mapbox.utils.MapContext +import java.io.File +import java.io.IOException import java.lang.NumberFormatException import kotlin.math.pow import kotlin.math.roundToInt @@ -29,13 +31,14 @@ const val KEY_LAYER_METADATA = "metadata" const val KEY_LAYER_PAINT = "paint" - -fun getStyle(context: MapContext, storedMapType: Int, styleOptions: MapStyleOptions?): Style.Builder { +fun getStyle( + context: MapContext, mapType: Int, styleOptions: MapStyleOptions?, styleFromFileWorkaround: Boolean = false +): Style.Builder { // TODO: Serve map style resources locally val styleJson = JSONObject( context.assets.open( - when (storedMapType) { + when (mapType) { MapsConstants.MAP_TYPE_SATELLITE, MapsConstants.MAP_TYPE_HYBRID -> "style-microg-satellite.json" MapsConstants.MAP_TYPE_TERRAIN -> "style-mapbox-outdoors-v12.json" //MAP_TYPE_NONE, MAP_TYPE_NORMAL, @@ -46,7 +49,35 @@ fun getStyle(context: MapContext, storedMapType: Int, styleOptions: MapStyleOpti styleOptions?.apply(styleJson) - return Style.Builder().fromJson(styleJson.toString()) + return if (styleFromFileWorkaround) { + val temporaryFile = File(context.cacheDir, styleJson.hashCode().toString()) + + if (!temporaryFile.exists()) { + temporaryFile.createNewFile() + } + + try { + temporaryFile.bufferedWriter().use { + it.write(styleJson.toString()) + } + Log.d(TAG, "file:/${temporaryFile.absolutePath}") + Style.Builder().fromUri("file:/${temporaryFile.absolutePath}") + } catch (e: IOException) { + e.printStackTrace() + Style.Builder().fromUri(getFallbackStyleOnlineUri(mapType)) + } + } else { + Style.Builder().fromJson(styleJson.toString()) + } +} + + +fun getFallbackStyleOnlineUri(mapType: Int) = when (mapType) { + MapsConstants.MAP_TYPE_SATELLITE -> "mapbox://styles/microg/cjxgloted25ap1ct4uex7m6hi" + MapsConstants.MAP_TYPE_TERRAIN -> "mapbox://styles/mapbox/outdoors-v12" + MapsConstants.MAP_TYPE_HYBRID -> "mapbox://styles/microg/cjxgloted25ap1ct4uex7m6hi" + //MAP_TYPE_NONE, MAP_TYPE_NORMAL, + else -> "mapbox://styles/microg/cjui4020201oo1fmca7yuwbor" } fun MapStyleOptions.apply(style: JSONObject) { -- GitLab