From 9d83bafa7b246e680062f5f98edb8b6d0707b82e Mon Sep 17 00:00:00 2001 From: Onuray Sahin Date: Mon, 7 Feb 2022 13:38:54 +0300 Subject: [PATCH 1/2] Retrieve map style url from .well-known. --- changelog.d/5175.feature | 1 + .../location/LocationPreviewFragment.kt | 7 +++++-- .../location/LocationSharingFragment.kt | 6 +++++- .../app/features/location/UrlMapProvider.kt | 20 ++++++++++++++----- .../raw/wellknown/ElementWellKnown.kt | 11 +++++++++- 5 files changed, 36 insertions(+), 9 deletions(-) create mode 100644 changelog.d/5175.feature diff --git a/changelog.d/5175.feature b/changelog.d/5175.feature new file mode 100644 index 0000000000..ffbe65c10d --- /dev/null +++ b/changelog.d/5175.feature @@ -0,0 +1 @@ +Retrieve map style url from .well-known \ No newline at end of file diff --git a/vector/src/main/java/im/vector/app/features/location/LocationPreviewFragment.kt b/vector/src/main/java/im/vector/app/features/location/LocationPreviewFragment.kt index d993c76b0e..db837f4823 100644 --- a/vector/src/main/java/im/vector/app/features/location/LocationPreviewFragment.kt +++ b/vector/src/main/java/im/vector/app/features/location/LocationPreviewFragment.kt @@ -54,8 +54,11 @@ class LocationPreviewFragment @Inject constructor( mapView = WeakReference(views.mapView) views.mapView.onCreate(savedInstanceState) - views.mapView.initialize(urlMapProvider.mapUrl) - loadPinDrawable() + + lifecycleScope.launchWhenCreated { + views.mapView.initialize(urlMapProvider.getMapUrl()) + loadPinDrawable() + } } override fun onResume() { diff --git a/vector/src/main/java/im/vector/app/features/location/LocationSharingFragment.kt b/vector/src/main/java/im/vector/app/features/location/LocationSharingFragment.kt index 7099bec9f0..a7e93a3f6c 100644 --- a/vector/src/main/java/im/vector/app/features/location/LocationSharingFragment.kt +++ b/vector/src/main/java/im/vector/app/features/location/LocationSharingFragment.kt @@ -21,6 +21,7 @@ import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import androidx.core.view.isGone +import androidx.lifecycle.lifecycleScope import com.airbnb.mvrx.fragmentViewModel import com.airbnb.mvrx.withState import com.google.android.material.dialog.MaterialAlertDialogBuilder @@ -53,7 +54,10 @@ class LocationSharingFragment @Inject constructor( mapView = WeakReference(views.mapView) views.mapView.onCreate(savedInstanceState) - views.mapView.initialize(urlMapProvider.mapUrl) + + lifecycleScope.launchWhenCreated { + views.mapView.initialize(urlMapProvider.getMapUrl()) + } views.shareLocationContainer.debouncedClicks { viewModel.handle(LocationSharingAction.OnShareLocation) diff --git a/vector/src/main/java/im/vector/app/features/location/UrlMapProvider.kt b/vector/src/main/java/im/vector/app/features/location/UrlMapProvider.kt index c9d9cf8112..9f66ce4d24 100644 --- a/vector/src/main/java/im/vector/app/features/location/UrlMapProvider.kt +++ b/vector/src/main/java/im/vector/app/features/location/UrlMapProvider.kt @@ -19,17 +19,27 @@ package im.vector.app.features.location import im.vector.app.BuildConfig import im.vector.app.core.resources.LocaleProvider import im.vector.app.core.resources.isRTL +import im.vector.app.features.raw.wellknown.getElementWellknown +import org.matrix.android.sdk.api.extensions.tryOrNull +import org.matrix.android.sdk.api.raw.RawService +import org.matrix.android.sdk.api.session.Session import javax.inject.Inject class UrlMapProvider @Inject constructor( - private val localeProvider: LocaleProvider + private val localeProvider: LocaleProvider, + private val session: Session, + private val rawService: RawService ) { private val keyParam = "?key=${BuildConfig.mapTilerKey}" - // This is static so no need for a fun - val mapUrl = buildString { - append(MAP_BASE_URL) - append(keyParam) + suspend fun getMapUrl(): String { + return tryOrNull { rawService.getElementWellknown(session.sessionParams) } + ?.mapTileServerConfig + ?.mapStyleUrl + ?: buildString { + append(MAP_BASE_URL) + append(keyParam) + } } fun buildStaticMapUrl(locationData: LocationData, diff --git a/vector/src/main/java/im/vector/app/features/raw/wellknown/ElementWellKnown.kt b/vector/src/main/java/im/vector/app/features/raw/wellknown/ElementWellKnown.kt index dc8090bc7c..0ae2a16b71 100644 --- a/vector/src/main/java/im/vector/app/features/raw/wellknown/ElementWellKnown.kt +++ b/vector/src/main/java/im/vector/app/features/raw/wellknown/ElementWellKnown.kt @@ -35,7 +35,10 @@ data class ElementWellKnown( val elementE2E: E2EWellKnownConfig? = null, @Json(name = "im.vector.riot.e2ee") - val riotE2E: E2EWellKnownConfig? = null + val riotE2E: E2EWellKnownConfig? = null, + + @Json(name = "org.matrix.msc3488.tile_server") + val mapTileServerConfig: MapTileServerConfig? = null ) @JsonClass(generateAdapter = true) @@ -53,3 +56,9 @@ data class WellKnownPreferredConfig( @Json(name = "preferredDomain") val preferredDomain: String? = null ) + +@JsonClass(generateAdapter = true) +data class MapTileServerConfig( + @Json(name = "map_style_url") + val mapStyleUrl: String? = null +) From 22a03abdc4d81462674ea47af17589ca410d9449 Mon Sep 17 00:00:00 2001 From: Onuray Sahin Date: Mon, 7 Feb 2022 20:08:31 +0300 Subject: [PATCH 2/2] Code review fix. --- .../vector/app/features/location/UrlMapProvider.kt | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/vector/src/main/java/im/vector/app/features/location/UrlMapProvider.kt b/vector/src/main/java/im/vector/app/features/location/UrlMapProvider.kt index 9f66ce4d24..adb5c27a02 100644 --- a/vector/src/main/java/im/vector/app/features/location/UrlMapProvider.kt +++ b/vector/src/main/java/im/vector/app/features/location/UrlMapProvider.kt @@ -32,14 +32,16 @@ class UrlMapProvider @Inject constructor( ) { private val keyParam = "?key=${BuildConfig.mapTilerKey}" + private val fallbackMapUrl = buildString { + append(MAP_BASE_URL) + append(keyParam) + } + suspend fun getMapUrl(): String { - return tryOrNull { rawService.getElementWellknown(session.sessionParams) } + val upstreamMapUrl = tryOrNull { rawService.getElementWellknown(session.sessionParams) } ?.mapTileServerConfig ?.mapStyleUrl - ?: buildString { - append(MAP_BASE_URL) - append(keyParam) - } + return upstreamMapUrl ?: fallbackMapUrl } fun buildStaticMapUrl(locationData: LocationData,