Skip to content

Commit f335eb1

Browse files
authored
Fix the share location URI (#4544)
* Fix the share location URI Use `ENGLISH` locale to make sure the coordinates are formatted using the right decimal separator. Also, when no label parameter was added, the URI just centered the map on some coordinates, instead of adding a marker.
1 parent 915699a commit f335eb1

File tree

2 files changed

+25
-9
lines changed

2 files changed

+25
-9
lines changed

features/location/impl/src/main/kotlin/io/element/android/features/location/impl/common/actions/AndroidLocationActions.kt

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,14 @@ import android.content.Context
1111
import android.content.Intent
1212
import android.net.Uri
1313
import androidx.annotation.VisibleForTesting
14+
import androidx.core.net.toUri
1415
import com.squareup.anvil.annotations.ContributesBinding
1516
import io.element.android.features.location.api.Location
1617
import io.element.android.libraries.androidutils.system.openAppSettingsPage
1718
import io.element.android.libraries.di.AppScope
1819
import io.element.android.libraries.di.ApplicationContext
1920
import timber.log.Timber
21+
import java.util.Locale
2022
import javax.inject.Inject
2123

2224
@ContributesBinding(AppScope::class)
@@ -25,7 +27,7 @@ class AndroidLocationActions @Inject constructor(
2527
) : LocationActions {
2628
override fun share(location: Location, label: String?) {
2729
runCatching {
28-
val uri = Uri.parse(buildUrl(location, label))
30+
val uri = buildUrl(location, label).toUri()
2931
val showMapsIntent = Intent(Intent.ACTION_VIEW).setData(uri)
3032
val chooserIntent = Intent.createChooser(showMapsIntent, null)
3133
chooserIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
@@ -42,17 +44,14 @@ class AndroidLocationActions @Inject constructor(
4244
}
4345
}
4446

47+
// Ref: https://developer.android.com/guide/components/intents-common#ViewMap
4548
@VisibleForTesting
4649
internal fun buildUrl(
4750
location: Location,
4851
label: String?,
4952
urlEncoder: (String) -> String = Uri::encode
5053
): String {
51-
// Ref: https://developer.android.com/guide/components/intents-common#ViewMap
52-
val base = "geo:0,0?q=%.6f,%.6f".format(location.lat, location.lon)
53-
return if (label == null) {
54-
base
55-
} else {
56-
"%s (%s)".format(base, urlEncoder(label))
57-
}
54+
// This is needed so the coordinates are formatted with a dot as decimal separator
55+
val locale = Locale.ENGLISH
56+
return "geo:0,0?q=%.6f,%.6f (%s)".format(locale, location.lat, location.lon, urlEncoder(label.orEmpty()))
5857
}

features/location/impl/src/test/kotlin/io/element/android/features/location/impl/common/actions/AndroidLocationActionsTest.kt

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import com.google.common.truth.Truth.assertThat
1111
import io.element.android.features.location.api.Location
1212
import org.junit.Test
1313
import java.net.URLEncoder
14+
import java.util.Locale
1415

1516
internal class AndroidLocationActionsTest {
1617
// We use an Android-native encoder in the actual app, switch to an equivalent JVM one for the tests
@@ -25,7 +26,7 @@ internal class AndroidLocationActionsTest {
2526
)
2627

2728
val actual = buildUrl(location, null, ::urlEncoder)
28-
val expected = "geo:0,0?q=1.234568,123.456789"
29+
val expected = "geo:0,0?q=1.234568,123.456789 ()"
2930

3031
assertThat(actual).isEqualTo(expected)
3132
}
@@ -57,4 +58,20 @@ internal class AndroidLocationActionsTest {
5758

5859
assertThat(actual).isEqualTo(expected)
5960
}
61+
62+
@Test
63+
fun `buildUrl - URL encodes coordinates in locale with comma decimal separator`() {
64+
val location = Location(
65+
lat = 1.000001,
66+
lon = 2.000001,
67+
accuracy = 0f
68+
)
69+
// Set a locale with comma as decimal separator
70+
Locale.setDefault(Locale.Category.FORMAT, Locale("pt", "BR"))
71+
72+
val actual = buildUrl(location, "(weird/stuff here)", ::urlEncoder)
73+
val expected = "geo:0,0?q=1.000001,2.000001 (%28weird%2Fstuff+here%29)"
74+
75+
assertThat(actual).isEqualTo(expected)
76+
}
6077
}

0 commit comments

Comments
 (0)