Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 26ee49fc authored by Fynn Godau's avatar Fynn Godau
Browse files

Merge branch '1132-lite-custom-colors' into 'master'

Workaround for custom styles in lite mode

See merge request !69
parents a6322863 bc9f7522
Loading
Loading
Loading
Loading
Loading
+1 −10
Original line number Diff line number Diff line
@@ -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"
}
+35 −4
Original line number Diff line number Diff line
@@ -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) {