Search in sources :

Example 1 with AndroidDeviceDetails

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);
}
Also used : AndroidDeviceDetails(com.amazonaws.mobileconnectors.pinpoint.internal.core.system.AndroidDeviceDetails) JSONObject(org.json.JSONObject) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) AndroidAppDetails(com.amazonaws.mobileconnectors.pinpoint.internal.core.system.AndroidAppDetails) JSONException(org.json.JSONException) SDKInfo(com.amazonaws.mobileconnectors.pinpoint.internal.core.util.SDKInfo)

Example 2 with AndroidDeviceDetails

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;
}
Also used : AndroidDeviceDetails(com.amazonaws.mobileconnectors.pinpoint.internal.core.system.AndroidDeviceDetails) PinpointContext(com.amazonaws.mobileconnectors.pinpoint.internal.core.PinpointContext) AndroidPreferencesConfiguration(com.amazonaws.mobileconnectors.pinpoint.internal.core.configuration.AndroidPreferencesConfiguration) AnalyticsContextBuilder(com.amazonaws.mobileconnectors.pinpoint.analytics.utils.AnalyticsContextBuilder)

Aggregations

AndroidDeviceDetails (com.amazonaws.mobileconnectors.pinpoint.internal.core.system.AndroidDeviceDetails)2 AnalyticsContextBuilder (com.amazonaws.mobileconnectors.pinpoint.analytics.utils.AnalyticsContextBuilder)1 PinpointContext (com.amazonaws.mobileconnectors.pinpoint.internal.core.PinpointContext)1 AndroidPreferencesConfiguration (com.amazonaws.mobileconnectors.pinpoint.internal.core.configuration.AndroidPreferencesConfiguration)1 AndroidAppDetails (com.amazonaws.mobileconnectors.pinpoint.internal.core.system.AndroidAppDetails)1 SDKInfo (com.amazonaws.mobileconnectors.pinpoint.internal.core.util.SDKInfo)1 HashMap (java.util.HashMap)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 JSONException (org.json.JSONException)1 JSONObject (org.json.JSONObject)1