use of com.amazonaws.mobileconnectors.pinpoint.internal.core.system.AndroidDeviceDetails in project aws-sdk-android by aws-amplify.
the class AnalyticsEvent method translateToEvent.
/**
* Transforms a JSONObject into an event
*
* @param source The event as a JSONObject
* @return An AnalyticsEvent
* @throws JSONException
*/
public static AnalyticsEvent translateToEvent(final JSONObject source) throws JSONException {
final Map<String, String> attributes = new HashMap<String, String>();
final Map<String, Double> metrics = new HashMap<String, Double>();
final AndroidAppDetails appDetails = new AndroidAppDetails(source.optString("app_package_name"), source.optString("app_version_code"), source.optString("app_version_name"), source.optString("app_title"), source.optString(ClientContext.APP_ID_KEY));
final SDKInfo sdkInfo = new SDKInfo(source.optString("sdk_name"), source.optString("sdk_version"));
final AndroidDeviceDetails deviceDetails = new AndroidDeviceDetails(source.optString("carrier"));
final String eventId = source.getString("event_id");
final String eventType = source.getString("event_type");
final Long timestamp = source.getLong("timestamp");
final String uniqueId = source.getString("unique_id");
String sessionId = "";
Long sessionStart = null;
Long sessionStop = null;
Long sessionDuration = null;
final JSONObject sessionJSON = source.getJSONObject("session");
if (sessionJSON != null) {
sessionId = sessionJSON.getString("id");
sessionStart = sessionJSON.getLong("startTimestamp");
sessionStop = sessionJSON.optLong("stopTimestamp");
sessionDuration = sessionJSON.optLong("duration");
}
final JSONObject attributesJSON = source.optJSONObject("attributes");
if (attributesJSON != null) {
final Iterator<String> keysIterator = attributesJSON.keys();
String key;
while (keysIterator.hasNext()) {
key = keysIterator.next();
attributes.put(key, attributesJSON.optString(key));
}
}
final JSONObject metricsJSON = source.optJSONObject("metrics");
if (metricsJSON != null) {
final Iterator<String> keysIterator = metricsJSON.keys();
String key;
while (keysIterator.hasNext()) {
key = keysIterator.next();
try {
metrics.put(key, metricsJSON.getDouble(key));
} catch (final JSONException e) {
// Do not log e due to potentially sensitive information
log.error("Failed to convert metric back to double from JSON value.");
}
}
}
return AnalyticsEvent.newInstance(eventId, eventType, attributes, metrics, sdkInfo, sessionId, sessionStart, sessionStop, sessionDuration, timestamp, uniqueId, appDetails, deviceDetails);
}
use of com.amazonaws.mobileconnectors.pinpoint.internal.core.system.AndroidDeviceDetails in project aws-sdk-android by aws-amplify.
the class EventLocaleTest method createMockContext.
private static PinpointContext createMockContext(Locale localeToReturn) {
AndroidPreferencesConfiguration mockConfiguration = Mockito.mock(AndroidPreferencesConfiguration.class);
when(mockConfiguration.optString("versionKey", "ver")).thenReturn("ver");
when(mockConfiguration.optBoolean("isAnalyticsEnabled", true)).thenReturn(true);
AndroidDeviceDetails mockDeviceDetails = Mockito.mock(AndroidDeviceDetails.class);
when(mockDeviceDetails.locale()).thenReturn(localeToReturn);
PinpointContext mockContext = new AnalyticsContextBuilder().withSdkInfo(SDK_NAME, SDK_VERSION).withUniqueIdValue(UNIQUE_ID).withConfiguration(mockConfiguration).withDeviceDetails(mockDeviceDetails).withContext(RuntimeEnvironment.application.getApplicationContext()).build();
return mockContext;
}
Aggregations