From 8648e8ba53f2175de33750f5b1793644893a9318 Mon Sep 17 00:00:00 2001 From: Marco Romano Date: Thu, 20 Jul 2023 14:29:06 +0200 Subject: [PATCH] Extract maptiler's map ids (#926) Maptiler custom map ids are only useable by the account that create them. So if we hardcode them forkers won't be able to use the maps even if the bring in their own api key (because they can't access our maps with their api key). Requires to set our map ids in `local.properties` for local development: ``` services.maptiler.lightMapId=9bc819c8-e627-474a-a348-ec144fe3d810 services.maptiler.darkMapId=dea61faf-292b-4774-9660-58fcef89a7f3 ``` --- .github/workflows/build.yml | 2 ++ .github/workflows/maestro.yml | 2 ++ .github/workflows/nightly.yml | 2 ++ .github/workflows/release.yml | 4 ++++ features/location/api/build.gradle.kts | 20 ++++++++++++++++--- .../features/location/api/internal/MapUrls.kt | 10 +++++----- 6 files changed, 32 insertions(+), 8 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 962405e782..b300a2d747 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -40,6 +40,8 @@ jobs: - name: Assemble debug APK env: ELEMENT_ANDROID_MAPTILER_API_KEY: ${{ secrets.MAPTILER_KEY }} + ELEMENT_ANDROID_MAPTILER_LIGHT_MAP_ID: ${{ secrets.MAPTILER_LIGHT_MAP_ID }} + ELEMENT_ANDROID_MAPTILER_DARK_MAP_ID: ${{ secrets.MAPTILER_DARK_MAP_ID }} run: ./gradlew assembleDebug $CI_GRADLE_ARG_PROPERTIES - name: Upload debug APKs uses: actions/upload-artifact@v3 diff --git a/.github/workflows/maestro.yml b/.github/workflows/maestro.yml index 0349e373bb..01783a1eb6 100644 --- a/.github/workflows/maestro.yml +++ b/.github/workflows/maestro.yml @@ -38,6 +38,8 @@ jobs: run: ./gradlew assembleDebug $CI_GRADLE_ARG_PROPERTIES env: ELEMENT_ANDROID_MAPTILER_API_KEY: ${{ secrets.MAPTILER_KEY }} + ELEMENT_ANDROID_MAPTILER_LIGHT_MAP_ID: ${{ secrets.MAPTILER_LIGHT_MAP_ID }} + ELEMENT_ANDROID_MAPTILER_DARK_MAP_ID: ${{ secrets.MAPTILER_DARK_MAP_ID }} - name: Upload debug APKs uses: actions/upload-artifact@v3 with: diff --git a/.github/workflows/nightly.yml b/.github/workflows/nightly.yml index 95c2deb8eb..956eebe71d 100644 --- a/.github/workflows/nightly.yml +++ b/.github/workflows/nightly.yml @@ -36,6 +36,8 @@ jobs: ./gradlew assembleNightly appDistributionUploadNightly $CI_GRADLE_ARG_PROPERTIES env: ELEMENT_ANDROID_MAPTILER_API_KEY: ${{ secrets.MAPTILER_KEY }} + ELEMENT_ANDROID_MAPTILER_LIGHT_MAP_ID: ${{ secrets.MAPTILER_LIGHT_MAP_ID }} + ELEMENT_ANDROID_MAPTILER_DARK_MAP_ID: ${{ secrets.MAPTILER_DARK_MAP_ID }} ELEMENT_ANDROID_NIGHTLY_KEYID: ${{ secrets.ELEMENT_ANDROID_NIGHTLY_KEYID }} ELEMENT_ANDROID_NIGHTLY_KEYPASSWORD: ${{ secrets.ELEMENT_ANDROID_NIGHTLY_KEYPASSWORD }} ELEMENT_ANDROID_NIGHTLY_STOREPASSWORD: ${{ secrets.ELEMENT_ANDROID_NIGHTLY_STOREPASSWORD }} diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 800f4c2fe4..ed2d8d4fbd 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -27,6 +27,10 @@ jobs: - name: Configure gradle uses: gradle/gradle-build-action@v2.6.1 - name: Create app bundle + env: + ELEMENT_ANDROID_MAPTILER_API_KEY: ${{ secrets.MAPTILER_KEY }} + ELEMENT_ANDROID_MAPTILER_LIGHT_MAP_ID: ${{ secrets.MAPTILER_LIGHT_MAP_ID }} + ELEMENT_ANDROID_MAPTILER_DARK_MAP_ID: ${{ secrets.MAPTILER_DARK_MAP_ID }} run: ./gradlew bundleRelease $CI_GRADLE_ARG_PROPERTIES - name: Upload bundle as artifact uses: actions/upload-artifact@v3 diff --git a/features/location/api/build.gradle.kts b/features/location/api/build.gradle.kts index 6de297fe77..bef0e77b67 100644 --- a/features/location/api/build.gradle.kts +++ b/features/location/api/build.gradle.kts @@ -22,12 +22,12 @@ plugins { id("kotlin-parcelize") } -fun readLocalProperty(name: String) = Properties().apply { +fun readLocalProperty(name: String): String? = Properties().apply { try { load(rootProject.file("local.properties").reader()) } catch (ignored: java.io.IOException) { } -}[name] +}.getProperty(name) android { namespace = "io.element.android.features.location.api" @@ -37,9 +37,23 @@ android { type = "string", name = "maptiler_api_key", value = System.getenv("ELEMENT_ANDROID_MAPTILER_API_KEY") - ?: readLocalProperty("services.maptiler.apikey") as? String + ?: readLocalProperty("services.maptiler.apikey") ?: "" ) + resValue( + type = "string", + name = "maptiler_light_map_id", + value = System.getenv("ELEMENT_ANDROID_MAPTILER_LIGHT_MAP_ID") + ?: readLocalProperty("services.maptiler.lightMapId") + ?: "basic-v2" // fall back to maptiler's default light map. + ) + resValue( + type = "string", + name = "maptiler_dark_map_id", + value = System.getenv("ELEMENT_ANDROID_MAPTILER_DARK_MAP_ID") + ?: readLocalProperty("services.maptiler.darkMapId") + ?: "basic-v2-dark" // fall back to maptiler's default dark map. + ) } } diff --git a/features/location/api/src/main/kotlin/io/element/android/features/location/api/internal/MapUrls.kt b/features/location/api/src/main/kotlin/io/element/android/features/location/api/internal/MapUrls.kt index b6f21a4512..2640896a78 100644 --- a/features/location/api/src/main/kotlin/io/element/android/features/location/api/internal/MapUrls.kt +++ b/features/location/api/src/main/kotlin/io/element/android/features/location/api/internal/MapUrls.kt @@ -35,7 +35,7 @@ fun staticMapUrl( height: Int, darkMode: Boolean, ): String { - return "${baseUrl(darkMode)}/static/${lon},${lat},${zoom}/${width}x${height}@2x.webp?key=${context.apiKey}&attribution=bottomleft" + return "${context.baseUrl(darkMode)}/static/${lon},${lat},${zoom}/${width}x${height}@2x.webp?key=${context.apiKey}&attribution=bottomleft" } /** @@ -60,15 +60,15 @@ private fun tileStyleUrl( context: Context, darkMode: Boolean, ): String { - return "${baseUrl(darkMode)}/style.json?key=${context.apiKey}" + return "${context.baseUrl(darkMode)}/style.json?key=${context.apiKey}" } -private fun baseUrl(darkMode: Boolean) = +private fun Context.baseUrl(darkMode: Boolean) = "https://api.maptiler.com/maps/" + if (darkMode) - "dea61faf-292b-4774-9660-58fcef89a7f3" + getString(R.string.maptiler_dark_map_id) else - "9bc819c8-e627-474a-a348-ec144fe3d810" + getString(R.string.maptiler_light_map_id) private val Context.apiKey: String get() = getString(R.string.maptiler_api_key)