Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: collecting device states on SDK #97

Merged
merged 2 commits into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 11 additions & 9 deletions sdk/src/main/java/com/deploygate/sdk/DeployGate.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -273,6 +273,8 @@ public void onForeground(
long elapsedRealtime,
TimeUnit timeUnit
) {
getSdkDeviceStatesCollector().collectLocale();
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We collect Locale information when the host app becomes foreground.


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);
Expand Down Expand Up @@ -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;
}

/**
Expand Down
52 changes: 52 additions & 0 deletions sdk/src/main/java/com/deploygate/sdk/SdkDeviceStatesCollector.java
Original file line number Diff line number Diff line change
@@ -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();
Copy link
Member Author

@satsukies satsukies Sep 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to use AppCompat 1.6.0~ if we collect per-app language preferences with backward compatibility.

But we should avoid adding new dependencies, so I use Locale instead.

In the future, this implementation will be updated if we can collect by another way.

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);
}
}
}
Loading