From 9a565f37f39b80726afd905ac4810664844dd6ce Mon Sep 17 00:00:00 2001 From: Manoel Aranda Neto Date: Thu, 9 Dec 2021 11:09:48 +0100 Subject: [PATCH 1/5] Feat: Add locale to device context and deprecate language --- CHANGELOG.md | 1 + .../core/DefaultAndroidEventProcessor.java | 7 +++++- .../core/DefaultAndroidEventProcessorTest.kt | 13 +++++++++++ .../main/java/io/sentry/protocol/Device.java | 22 ++++++++++++++++++- 4 files changed, 41 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 24a7c9236c..8e85dba0ff 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ## Unreleased * Ref: Rename Fragment span operation from `ui.fragment.load` to `ui.load` (#1824) +* Feat: Add locale to device context and deprecate language (#) ## 5.4.3 diff --git a/sentry-android-core/src/main/java/io/sentry/android/core/DefaultAndroidEventProcessor.java b/sentry-android-core/src/main/java/io/sentry/android/core/DefaultAndroidEventProcessor.java index 4606736fb6..0341f95282 100644 --- a/sentry-android-core/src/main/java/io/sentry/android/core/DefaultAndroidEventProcessor.java +++ b/sentry-android-core/src/main/java/io/sentry/android/core/DefaultAndroidEventProcessor.java @@ -310,8 +310,13 @@ private void setArchitectures(final @NotNull Device device) { if (device.getId() == null) { device.setId(getDeviceId()); } + + final Locale locale = Locale.getDefault(); if (device.getLanguage() == null) { - device.setLanguage(Locale.getDefault().toString()); // eg en_US + device.setLanguage(locale.getLanguage()); + } + if (device.getLocale() == null) { + device.setLocale(locale.toString()); // eg en_US } return device; diff --git a/sentry-android-core/src/test/java/io/sentry/android/core/DefaultAndroidEventProcessorTest.kt b/sentry-android-core/src/test/java/io/sentry/android/core/DefaultAndroidEventProcessorTest.kt index ce1697cbcf..fc183b6ac3 100644 --- a/sentry-android-core/src/test/java/io/sentry/android/core/DefaultAndroidEventProcessorTest.kt +++ b/sentry-android-core/src/test/java/io/sentry/android/core/DefaultAndroidEventProcessorTest.kt @@ -27,6 +27,7 @@ import io.sentry.protocol.SentryTransaction import io.sentry.protocol.User import io.sentry.test.getCtor import org.junit.runner.RunWith +import java.util.* import kotlin.test.BeforeTest import kotlin.test.Test import kotlin.test.assertEquals @@ -376,4 +377,16 @@ class DefaultAndroidEventProcessorTest { // assertNotNull(device.connectionType) } } + + @Test + fun `Event sets language and locale`() { + val sut = fixture.getSut(context) + val locale = Locale.getDefault() + + assertNotNull(sut.process(SentryEvent(), null)) { + val device = it.contexts.device!! + assertEquals(device.language, locale.language) + assertEquals(device.locale, locale.toString()) + } + } } diff --git a/sentry/src/main/java/io/sentry/protocol/Device.java b/sentry/src/main/java/io/sentry/protocol/Device.java index d36361f05b..265471e001 100644 --- a/sentry/src/main/java/io/sentry/protocol/Device.java +++ b/sentry/src/main/java/io/sentry/protocol/Device.java @@ -93,7 +93,18 @@ public final class Device implements IUnknownPropertiesConsumer { private @Nullable TimeZone timezone; private @Nullable String id; - private @Nullable String language; + + /** + * This method returns the language code for this locale, which will either be the empty string or + * a lowercase ISO 639 code. + * + * @deprecated use {@link Device#getLocale()} + */ + @Deprecated private @Nullable String language; + + /** The locale of the device. For example, en-US. */ + private @Nullable String locale; + private @Nullable String connectionType; /** battery's temperature in celsius */ @@ -135,6 +146,7 @@ public Device() {} this.batteryLevel = device.batteryLevel; final String[] archsRef = device.archs; this.archs = archsRef != null ? archsRef.clone() : null; + this.setLocale(device.getLocale()); final TimeZone timezoneRef = device.timezone; this.timezone = timezoneRef != null ? (TimeZone) timezoneRef.clone() : null; @@ -384,6 +396,14 @@ public void setBatteryTemperature(final @Nullable Float batteryTemperature) { this.batteryTemperature = batteryTemperature; } + public @Nullable String getLocale() { + return locale; + } + + public void setLocale(final @Nullable String locale) { + this.locale = locale; + } + @TestOnly @Nullable Map getUnknown() { From 3816a6a22b60d7f1f8ff6305a7d276e73a9977ad Mon Sep 17 00:00:00 2001 From: Manoel Aranda Neto Date: Thu, 9 Dec 2021 11:10:04 +0100 Subject: [PATCH 2/5] add pr id --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8e85dba0ff..a5caec4743 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,7 +3,7 @@ ## Unreleased * Ref: Rename Fragment span operation from `ui.fragment.load` to `ui.load` (#1824) -* Feat: Add locale to device context and deprecate language (#) +* Feat: Add locale to device context and deprecate language (#1832) ## 5.4.3 From a82755e06f5cfbe49654537dfef9c630ac093bf8 Mon Sep 17 00:00:00 2001 From: Manoel Aranda Neto Date: Thu, 9 Dec 2021 11:11:55 +0100 Subject: [PATCH 3/5] fix --- .../io/sentry/android/core/DefaultAndroidEventProcessorTest.kt | 2 +- sentry/src/main/java/io/sentry/protocol/Device.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/sentry-android-core/src/test/java/io/sentry/android/core/DefaultAndroidEventProcessorTest.kt b/sentry-android-core/src/test/java/io/sentry/android/core/DefaultAndroidEventProcessorTest.kt index fc183b6ac3..f742321a55 100644 --- a/sentry-android-core/src/test/java/io/sentry/android/core/DefaultAndroidEventProcessorTest.kt +++ b/sentry-android-core/src/test/java/io/sentry/android/core/DefaultAndroidEventProcessorTest.kt @@ -27,7 +27,7 @@ import io.sentry.protocol.SentryTransaction import io.sentry.protocol.User import io.sentry.test.getCtor import org.junit.runner.RunWith -import java.util.* +import java.util.Locale import kotlin.test.BeforeTest import kotlin.test.Test import kotlin.test.assertEquals diff --git a/sentry/src/main/java/io/sentry/protocol/Device.java b/sentry/src/main/java/io/sentry/protocol/Device.java index 265471e001..2a16faa410 100644 --- a/sentry/src/main/java/io/sentry/protocol/Device.java +++ b/sentry/src/main/java/io/sentry/protocol/Device.java @@ -146,7 +146,7 @@ public Device() {} this.batteryLevel = device.batteryLevel; final String[] archsRef = device.archs; this.archs = archsRef != null ? archsRef.clone() : null; - this.setLocale(device.getLocale()); + this.locale = device.locale; final TimeZone timezoneRef = device.timezone; this.timezone = timezoneRef != null ? (TimeZone) timezoneRef.clone() : null; From 25cb80e44992d626917a0913a550005ae36b3e2a Mon Sep 17 00:00:00 2001 From: Manoel Aranda Neto Date: Thu, 9 Dec 2021 11:15:59 +0100 Subject: [PATCH 4/5] api --- sentry/api/sentry.api | 2 ++ 1 file changed, 2 insertions(+) diff --git a/sentry/api/sentry.api b/sentry/api/sentry.api index 51e89b8755..4d183bb41e 100644 --- a/sentry/api/sentry.api +++ b/sentry/api/sentry.api @@ -1532,6 +1532,7 @@ public final class io/sentry/protocol/Device : io/sentry/IUnknownPropertiesConsu public fun getFreeStorage ()Ljava/lang/Long; public fun getId ()Ljava/lang/String; public fun getLanguage ()Ljava/lang/String; + public fun getLocale ()Ljava/lang/String; public fun getManufacturer ()Ljava/lang/String; public fun getMemorySize ()Ljava/lang/Long; public fun getModel ()Ljava/lang/String; @@ -1563,6 +1564,7 @@ public final class io/sentry/protocol/Device : io/sentry/IUnknownPropertiesConsu public fun setFreeStorage (Ljava/lang/Long;)V public fun setId (Ljava/lang/String;)V public fun setLanguage (Ljava/lang/String;)V + public fun setLocale (Ljava/lang/String;)V public fun setLowMemory (Ljava/lang/Boolean;)V public fun setManufacturer (Ljava/lang/String;)V public fun setMemorySize (Ljava/lang/Long;)V From 15315f47ffb33ab91b4307feceec3ef992455cc8 Mon Sep 17 00:00:00 2001 From: Manoel Aranda Neto Date: Thu, 9 Dec 2021 13:07:18 +0100 Subject: [PATCH 5/5] set and assert locale --- .../android/core/DefaultAndroidEventProcessorTest.kt | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/sentry-android-core/src/test/java/io/sentry/android/core/DefaultAndroidEventProcessorTest.kt b/sentry-android-core/src/test/java/io/sentry/android/core/DefaultAndroidEventProcessorTest.kt index f742321a55..904a2c6f1e 100644 --- a/sentry-android-core/src/test/java/io/sentry/android/core/DefaultAndroidEventProcessorTest.kt +++ b/sentry-android-core/src/test/java/io/sentry/android/core/DefaultAndroidEventProcessorTest.kt @@ -45,6 +45,10 @@ class DefaultAndroidEventProcessorTest { private val className = "io.sentry.android.core.DefaultAndroidEventProcessor" private val ctorTypes = arrayOf(Context::class.java, ILogger::class.java, IBuildInfoProvider::class.java) + init { + Locale.setDefault(Locale.US) + } + private class Fixture { val buildInfo = mock() val options = SentryOptions().apply { @@ -381,12 +385,11 @@ class DefaultAndroidEventProcessorTest { @Test fun `Event sets language and locale`() { val sut = fixture.getSut(context) - val locale = Locale.getDefault() assertNotNull(sut.process(SentryEvent(), null)) { val device = it.contexts.device!! - assertEquals(device.language, locale.language) - assertEquals(device.locale, locale.toString()) + assertEquals("en", device.language) + assertEquals("en_US", device.locale) } } }