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

Commit 1c73e283 authored by Fynn Godau's avatar Fynn Godau
Browse files

Custom styles implementation frame

parent f54d5cfb
Loading
Loading
Loading
Loading
+13549 −0

File added.

Preview size limit exceeded, changes collapsed.

+2394 −0

File added.

Preview size limit exceeded, changes collapsed.

+2361 −0

File added.

Preview size limit exceeded, changes collapsed.

+9 −11
Original line number Diff line number Diff line
@@ -130,6 +130,7 @@ class GoogleMapImpl(private val context: Context, var options: GoogleMapOptions)
    var tileId = 0L

    var storedMapType: Int = options.mapType
    var mapStyle: MapStyleOptions? = null
    val waitingCameraUpdates = mutableListOf<CameraUpdate>()
    var locationEnabled: Boolean = false

@@ -247,6 +248,7 @@ class GoogleMapImpl(private val context: Context, var options: GoogleMapOptions)

    override fun setMapStyle(options: MapStyleOptions?): Boolean {
        Log.d(TAG, "setMapStyle options: " + options?.getJson())
        mapStyle = options
        return true
    }

@@ -359,10 +361,10 @@ class GoogleMapImpl(private val context: Context, var options: GoogleMapOptions)

    override fun setMapType(type: Int) {
        storedMapType = type
        applyMapType()
        applyMapStyle()
    }

    fun applyMapType() {
    fun applyMapStyle() {
        val lines = lineManager?.annotations?.values()
        val fills = fillManager?.annotations?.values()
        val symbols = symbolManager?.annotations?.values()
@@ -372,14 +374,10 @@ class GoogleMapImpl(private val context: Context, var options: GoogleMapOptions)
            symbols?.let { runCatching { symbolManager?.update(it) } }
        }

        // TODO: Serve map styles locally
        when (storedMapType) {
            MAP_TYPE_SATELLITE -> map?.setStyle(Style.Builder().fromUri("mapbox://styles/microg/cjxgloted25ap1ct4uex7m6hi"), update)
            MAP_TYPE_TERRAIN -> map?.setStyle(Style.Builder().fromUri("mapbox://styles/mapbox/outdoors-v12"), update)
            MAP_TYPE_HYBRID -> map?.setStyle(Style.Builder().fromUri("mapbox://styles/microg/cjxgloted25ap1ct4uex7m6hi"), update)
            //MAP_TYPE_NONE, MAP_TYPE_NORMAL,
            else -> map?.setStyle(Style.Builder().fromUrl("mapbox://styles/microg/cjui4020201oo1fmca7yuwbor"), update)
        }
        map?.setStyle(
            getStyle(MapContext(context), storedMapType, mapStyle),
            update
        )

        map?.let { BitmapDescriptorFactoryImpl.registerMap(it) }

@@ -681,7 +679,7 @@ class GoogleMapImpl(private val context: Context, var options: GoogleMapOptions)
            false
        }

        applyMapType()
        applyMapStyle()
        options.minZoomPreference?.let { if (it != 0f) map.setMinZoomPreference(it.toDouble()) }
        options.maxZoomPreference?.let { if (it != 0f) map.setMaxZoomPreference(it.toDouble()) }
        options.latLngBoundsForCameraTarget?.let { map.setLatLngBoundsForCameraTarget(it.toMapbox()) }
+76 −0
Original line number Diff line number Diff line
package org.microg.gms.maps.mapbox

import androidx.annotation.FloatRange
import com.google.android.gms.maps.model.MapStyleOptions
import com.google.gson.Gson
import com.google.gson.JsonSyntaxException
import com.google.gson.annotations.SerializedName
import com.mapbox.mapboxsdk.maps.Style
import org.json.JSONObject
import org.microg.gms.maps.MapsConstants
import org.microg.gms.maps.mapbox.utils.MapContext

const val TAG = "GmsMapStyles"


fun getStyle(context: MapContext, storedMapType: Int, styleOptions: MapStyleOptions?): Style.Builder {

    // TODO: Serve map style resources locally
    val styleJson = JSONObject(context.assets.open(
        when (storedMapType) {
            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,
            else -> "style-microg-normal.json"
        }
    ).bufferedReader().readText())

    styleOptions?.apply(styleJson)

    return Style.Builder().fromJson(styleJson.toString())
}

fun MapStyleOptions.apply(style: JSONObject) {
    try {
        Gson().fromJson(json, Array<StyleOperation>::class.java).let { styleOperations ->

            val layerArray = style.getJSONArray("layers")
            for (i in 0 until layerArray.length()) {
                val layer = layerArray.getJSONObject(i)
                if (layer.has("metadata") && layer.getJSONObject("metadata")
                        .let { it.has("microg:gms-type-feature") && it.has("microg:gms-type-element") }
                ) {
                    val featureType = layer.getJSONObject("metadata").getString("microg:gms-type-feature")
                    val elementType = layer.getJSONObject("metadata").getString("microg:gms-type-element")

                    for (operation in styleOperations) {
                        if (operation.featureType?.startsWith(featureType) != false && // Todo: "all" here as well?
                            (operation.elementType?.startsWith(elementType) != false || operation.elementType == "all")
                        ) {
                            operation.stylers?.forEach { styler -> styler.apply(layer) }
                        }
                    }
                }
            }
        }


    } catch (e: JsonSyntaxException) {
        e.printStackTrace()
    }
}

class StyleOperation(val featureType: String?, val elementType: String?, val stylers: Array<Styler>?)

class Styler(
    val hue: String?,
    @FloatRange(from = -100.0, to = 100.0) val saturation: Float?,
    @FloatRange(from = -100.0, to = 100.0) val lightness: Float?,
    @FloatRange(from = 0.01, to = 10.0) val gamma: Float?,
    @SerializedName("invert_lightness") val invertLightness: Boolean?,
    val visibility: String?,
    val color: String?,
    val weight: Int?
)

fun Styler.apply(layer: JSONObject) { TODO() }
 No newline at end of file