Search in sources :

Example 1 with AndroidAppDetails

use of com.amazonaws.mobileconnectors.pinpoint.internal.core.system.AndroidAppDetails 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 AndroidAppDetails

use of com.amazonaws.mobileconnectors.pinpoint.internal.core.system.AndroidAppDetails in project aws-sdk-android by aws-amplify.

the class EventRecorder method buildEventPayload.

void buildEventPayload(AnalyticsEvent internalEvent, Event event) {
    final Session session = new Session();
    session.withId(internalEvent.getSession().getSessionId());
    session.withStartTimestamp(DateUtils.formatISO8601Date(new Date(internalEvent.getSession().getSessionStart())));
    if (internalEvent.getSession().getSessionStop() != null && internalEvent.getSession().getSessionStop() != 0L) {
        session.withStopTimestamp(DateUtils.formatISO8601Date(new Date(internalEvent.getSession().getSessionStop())));
    }
    if (internalEvent.getSession().getSessionDuration() != null && internalEvent.getSession().getSessionDuration() != 0L) {
        session.withDuration(internalEvent.getSession().getSessionDuration().intValue());
    }
    final AndroidAppDetails appDetails = internalEvent.getAppDetails();
    event.withAppPackageName(appDetails.packageName()).withAppTitle(appDetails.getAppTitle()).withAppVersionCode(appDetails.versionCode()).withAttributes(internalEvent.getAllAttributes()).withClientSdkVersion(internalEvent.getSdkVersion()).withEventType(internalEvent.getEventType()).withMetrics(internalEvent.getAllMetrics()).withSdkName(internalEvent.getSdkName()).withSession(session).withTimestamp(DateUtils.formatISO8601Date(new Date(internalEvent.getEventTimestamp())));
}
Also used : AndroidAppDetails(com.amazonaws.mobileconnectors.pinpoint.internal.core.system.AndroidAppDetails) Date(java.util.Date) Session(com.amazonaws.services.pinpoint.model.Session)

Example 3 with AndroidAppDetails

use of com.amazonaws.mobileconnectors.pinpoint.internal.core.system.AndroidAppDetails in project aws-sdk-android by aws-amplify.

the class AnalyticsContextBuilder method build.

public PinpointContext build() {
    final SDKInfo mockSDKInfo = mock(SDKInfo.class);
    when(mockSDKInfo.getName()).thenReturn(sdkName);
    when(mockSDKInfo.getVersion()).thenReturn(sdkVersion);
    final MockUtil mockUtil = new MockUtil();
    if (mockUtil.isMock(mockSystem)) {
        when(mockSystem.getPreferences()).thenReturn(mockPreferences);
        when(mockSystem.getConnectivity()).thenReturn(mockConnectivity);
        final AndroidAppDetails mockAppDetails = new MockAppDetails();
        when(mockSystem.getAppDetails()).thenReturn(mockAppDetails);
        when(mockSystem.getDeviceDetails()).thenReturn(mockDeviceDetails);
    }
    final PinpointContext mockContext = mock(PinpointContext.class);
    when(mockContext.getSDKInfo()).thenReturn(mockSDKInfo);
    when(mockContext.getConfiguration()).thenReturn(mockConfig);
    when(mockContext.getUniqueId()).thenReturn(mockUniqueIdValue);
    when(mockContext.getAnalyticsServiceClient()).thenReturn(mockERS);
    when(mockContext.getPinpointServiceClient()).thenReturn(mockPinpointService);
    when(mockContext.getSystem()).thenReturn(mockSystem);
    // Notification client must be constructed after mock system is set
    mockNotificationClient = new NotificationClient(mockContext);
    when(mockContext.getNotificationClient()).thenReturn(mockNotificationClient);
    when(mockContext.getApplicationContext()).thenReturn(context);
    when(mockContext.getConfiguration()).thenReturn(mockConfig);
    when(mockContext.getNetworkType()).thenCallRealMethod();
    when(mockContext.getTargetingClient()).thenReturn(mockTargetingClient);
    when(mockContext.getPinpointConfiguration()).thenReturn(mockPinpointConfig);
    return mockContext;
}
Also used : MockAppDetails(com.amazonaws.mobileconnectors.pinpoint.internal.core.system.MockAppDetails) PinpointContext(com.amazonaws.mobileconnectors.pinpoint.internal.core.PinpointContext) AndroidAppDetails(com.amazonaws.mobileconnectors.pinpoint.internal.core.system.AndroidAppDetails) MockUtil(org.mockito.internal.util.MockUtil) SDKInfo(com.amazonaws.mobileconnectors.pinpoint.internal.core.util.SDKInfo) NotificationClient(com.amazonaws.mobileconnectors.pinpoint.targeting.notification.NotificationClient)

Aggregations

AndroidAppDetails (com.amazonaws.mobileconnectors.pinpoint.internal.core.system.AndroidAppDetails)3 SDKInfo (com.amazonaws.mobileconnectors.pinpoint.internal.core.util.SDKInfo)2 PinpointContext (com.amazonaws.mobileconnectors.pinpoint.internal.core.PinpointContext)1 AndroidDeviceDetails (com.amazonaws.mobileconnectors.pinpoint.internal.core.system.AndroidDeviceDetails)1 MockAppDetails (com.amazonaws.mobileconnectors.pinpoint.internal.core.system.MockAppDetails)1 NotificationClient (com.amazonaws.mobileconnectors.pinpoint.targeting.notification.NotificationClient)1 Session (com.amazonaws.services.pinpoint.model.Session)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 JSONException (org.json.JSONException)1 JSONObject (org.json.JSONObject)1 MockUtil (org.mockito.internal.util.MockUtil)1