diff --git a/sdk/src/main/java/com/deploygate/sdk/DeployGate.java b/sdk/src/main/java/com/deploygate/sdk/DeployGate.java index 410b09a..500b543 100644 --- a/sdk/src/main/java/com/deploygate/sdk/DeployGate.java +++ b/sdk/src/main/java/com/deploygate/sdk/DeployGate.java @@ -55,7 +55,7 @@ public class DeployGate { private static final Object sLock = new Object(); private static CustomAttributes sBuildEnvironment; private static CustomAttributes sRuntimeExtra; - private static CustomAttributes sSdkDeviceStates; + private static SdkDeviceStatesCollector sSdkDeviceStatesCollector; private static DeployGate sInstance; @@ -152,7 +152,7 @@ public void onEvent( cv.put(DeployGateEvent.ATTRIBUTE_KEY_RUNTIME_EXTRAS, runtimeExtraJSON); } - String sdkDeviceStatusJSON = getSdkDeviceStates().getJSONString(); + String sdkDeviceStatusJSON = getSdkDeviceStatesCollector().getJSONString(); if (!sdkDeviceStatusJSON.equals("{}")) { cv.put(DeployGateEvent.ATTRIBUTE_KEY_SDK_DEVICE_STATES, sdkDeviceStatusJSON); } @@ -273,6 +273,8 @@ public void onForeground( long elapsedRealtime, TimeUnit timeUnit ) { + getSdkDeviceStatesCollector().collectLocale(); + Bundle extras = new Bundle(); extras.putLong(DeployGateEvent.EXTRA_VISIBILITY_EVENT_ELAPSED_REAL_TIME_IN_NANOS, timeUnit.toNanos(elapsedRealtime)); extras.putInt(DeployGateEvent.EXTRA_VISIBILITY_EVENT_TYPE, DeployGateEvent.VisibilityType.FOREGROUND); @@ -1709,19 +1711,19 @@ public static CustomAttributes getRuntimeExtra() { return sRuntimeExtra; } - private static CustomAttributes getSdkDeviceStates() { - if (sSdkDeviceStates != null) { - return sSdkDeviceStates; + private static SdkDeviceStatesCollector getSdkDeviceStatesCollector() { + if (sSdkDeviceStatesCollector != null) { + return sSdkDeviceStatesCollector; } synchronized (sLock) { - if (sSdkDeviceStates != null) { - return sSdkDeviceStates; + if (sSdkDeviceStatesCollector != null) { + return sSdkDeviceStatesCollector; } - sSdkDeviceStates = new CustomAttributes(); + sSdkDeviceStatesCollector = new SdkDeviceStatesCollector(); } - return sSdkDeviceStates; + return sSdkDeviceStatesCollector; } /** diff --git a/sdk/src/main/java/com/deploygate/sdk/SdkDeviceStatesCollector.java b/sdk/src/main/java/com/deploygate/sdk/SdkDeviceStatesCollector.java new file mode 100644 index 0000000..b2657f8 --- /dev/null +++ b/sdk/src/main/java/com/deploygate/sdk/SdkDeviceStatesCollector.java @@ -0,0 +1,52 @@ +package com.deploygate.sdk; + +import android.os.Build; + +import com.deploygate.sdk.internal.Logger; + +import org.json.JSONException; +import org.json.JSONObject; + +import java.util.Locale; + +final class SdkDeviceStatesCollector { + + private final JSONObject states = new JSONObject(); + + public String getJSONString() { + return states.toString(); + } + + public void collectLocale() { + Locale defaultLocale = Locale.getDefault(); + String localeClassName = Locale.class.getName(); + putState(localeClassName, "toString", defaultLocale.toString()); + putState(localeClassName, "getLanguage", defaultLocale.getLanguage()); + putState(localeClassName, "getCountry", defaultLocale.getCountry()); + putState(localeClassName, "getVariant", defaultLocale.getVariant()); + putState(localeClassName, "getDisplayCountry", defaultLocale.getDisplayCountry()); + putState(localeClassName, "getDisplayLanguage", defaultLocale.getDisplayLanguage()); + putState(localeClassName, "getDisplayName", defaultLocale.getDisplayName()); + putState(localeClassName, "getDisplayVariant", defaultLocale.getDisplayVariant()); + putState(localeClassName, "getISO3Country", defaultLocale.getISO3Country()); + putState(localeClassName, "getISO3Language", defaultLocale.getISO3Language()); + + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { + putState(localeClassName, "getDisplayScript", defaultLocale.getDisplayScript()); + putState(localeClassName, "toLanguageTag", defaultLocale.toLanguageTag()); + } + + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { + putState(localeClassName, "getScript", defaultLocale.getScript()); + } + } + + private void putState(String fqcn, String paramName, Object data) { + String key = String.format("%s$%s", fqcn, paramName); + try { + states.put(key, data); + } catch (JSONException e) { + Logger.w(e, "Failed to put info: key=%s, value=%s", key, data); + } + } +}