@Override protected void execute() { conversation.addPayload(payload); } });
public synchronized AppRelease getAppRelease() { return conversation.getAppRelease(); // TODO: make conversation immutable }
private static boolean canShowInteraction(Conversation conversation, String eventLabel) { checkConversationQueue(); if (conversation == null) { throw new IllegalArgumentException("Conversation is null"); } Interaction interaction = conversation.getApplicableInteraction(eventLabel, false); return interaction != null; }
public String getApplicableInteraction(String eventLabel, boolean verbose) { JSONArray invocations = optJSONArray(eventLabel); if (invocations != null) { for (int i = 0; i < invocations.length(); i++) { JSONObject invocationObject = invocations.optJSONObject(i); if (invocationObject != null) { try { Invocation invocation = new Invocation(invocationObject.toString()); Conversation conversation = ApptentiveInternal.getInstance().getConversation(); FieldManager fieldManager = new FieldManager(ApptentiveInternal.getInstance().getApplicationContext(), conversation.getVersionHistory(), conversation.getEventData(), conversation.getPerson(), conversation.getDevice(), conversation.getAppRelease()); if (invocation.isCriteriaMet(fieldManager, verbose)) { return invocation.getInteractionId(); } } catch (JSONException e) { logException(e); } } } } ApptentiveLog.v(INTERACTIONS, "No runnable Interactions for EventLabel: %s", eventLabel); return null; } }
private Conversation loadConversation(ConversationMetadataItem item) throws SerializerException, ConversationLoadException { checkConversationQueue(); // logged-in conversations should use an encryption key which was received from the backend. EncryptionKey conversationEncryptionKey = encryptionKey; if (LOGGED_IN.equals(item.getConversationState())) { conversationEncryptionKey = item.getConversationEncryptionKey(); if (conversationEncryptionKey == null) { throw new ConversationLoadException("Missing conversation encryption key"); } } // TODO: use same serialization logic across the project final Conversation conversation = new Conversation(item.getDataFile(), item.getMessagesFile(), conversationEncryptionKey); conversation.setState(item.getConversationState()); // set the state same as the item's state conversation.setUserId(item.getUserId()); conversation.setConversationToken(item.getConversationToken()); // TODO: this would be overwritten by the next call // try to migrate legacy conversation first boolean migrated = conversation.migrateConversationData(); // if failed - load from encrypted data if (!migrated) { conversation.loadConversationData(); } // check inconsistency conversation.checkInternalConsistency(); return conversation; }
boolean sdkChanged = false; final VersionHistoryItem lastVersionItemSeen = conversation.getVersionHistory().getLastVersionSeen(); final int currentVersionCode = appRelease.getVersionCode(); final String currentVersionName = appRelease.getVersionName(); final String lastSeenSdkVersion = conversation.getLastSeenSdkVersion(); final String currentSdkVersion = Constants.getApptentiveSdkVersion(); if (!StringUtils.equal(lastSeenSdkVersion, currentSdkVersion)) { conversation.getVersionHistory().updateVersionHistory(Util.currentTimeSeconds(), currentVersionCode, currentVersionName); if (sdkChanged) { ApptentiveLog.i(CONVERSATION, "SDK version was changed: %s => %s", lastSeenSdkVersion, currentSdkVersion); conversation.setLastSeenSdkVersion(currentSdkVersion); conversation.setSdk(sdk); conversation.addPayload(AppReleaseManager.getPayload(sdk, appRelease)); conversation.setAppRelease(appRelease); conversation.setSdk(sdk); invalidateCaches(conversation);
public void setConversationToken(String conversationToken) { getConversationData().setConversationToken(conversationToken); }
checkConversationQueue(); assertTrue(conversation != null && !conversation.hasState(UNDEFINED)); if (conversation != null && !conversation.hasState(UNDEFINED)) { NOTIFICATION_KEY_CONVERSATION, conversation); if (conversation.hasActiveState()) { if (appIsInForeground) { conversation.fetchInteractions(getContext()); conversation.getMessageManager().attemptToStartMessagePolling(); String pushToken = prefs.getString(Constants.PREF_KEY_PUSH_TOKEN, null); if (pushProvider != -1 && pushToken != null) { conversation.setPushIntegration(pushProvider, pushToken);
if (conversation.hasActiveState()) { if (!conversation.hasSession()) { conversation.startSession(); conversation.addPayload(new LogoutPayload()); } else if (notification.hasName(NOTIFICATION_AUTHENTICATION_FAILED)) { String conversationIdOfFailedRequest = notification.getUserInfo(NOTIFICATION_KEY_CONVERSATION_ID, String.class);
Conversation conversation = new Conversation(dataFile, messagesFile, encryptionKey); conversation.setState(LEGACY_PENDING); conversation.setConversationToken(legacyConversationToken);
public void logout() { checkConversationQueue(); if (activeConversation != null) { switch (activeConversation.getState()) { case LOGGED_IN: ApptentiveLog.d(CONVERSATION, "Ending active conversation."); EngagementModule.engageInternal(getContext(), activeConversation, "logout"); // Post synchronously to ensure logout payload can be sent before destroying the logged in conversation. ApptentiveNotificationCenter.defaultCenter().postNotification(NOTIFICATION_CONVERSATION_WILL_LOGOUT, ObjectUtils.toMap(NOTIFICATION_KEY_CONVERSATION, activeConversation)); activeConversation.destroy(); activeConversation.setState(LOGGED_OUT); handleConversationStateChange(activeConversation); setActiveConversation(null); ApptentiveInternal.dismissAllInteractions(); break; default: ApptentiveLog.w(CONVERSATION, "Attempted to logout() from Conversation, but the Active Conversation was not in LOGGED_IN state."); break; } } else { ApptentiveLog.w(CONVERSATION, "Attempted to logout(), but there was no Active Conversation."); } }
@Override public void onReceiveNotification(ApptentiveNotification notification) { checkConversationQueue(); appIsInForeground = true; if (activeConversation != null && activeConversation.hasActiveState()) { ApptentiveLog.v(CONVERSATION, "App entered foreground notification received. Trying to fetch app configuration and interactions..."); final Context context = getContext(); if (context != null) { fetchAppConfiguration(activeConversation); activeConversation.fetchInteractions(context); } else { ApptentiveLog.w(CONVERSATION, "Can't fetch app configuration and conversation interactions: context is lost"); } } } });
private static boolean doEngage(Conversation conversation, Context context, String eventLabel) { checkConversationQueue(); Interaction interaction = conversation.getApplicableInteraction(eventLabel, true); if (interaction != null) { String versionName = ApptentiveInternal.getInstance().getApplicationVersionName(); int versionCode = ApptentiveInternal.getInstance().getApplicationVersionCode(); conversation.getEventData().storeInteractionForCurrentAppVersion(Util.currentTimeSeconds(), versionCode, versionName, interaction.getId()); launchInteraction(context, interaction); return true; } ApptentiveLog.d(INTERACTIONS, "No interaction to show for event: '%s'", eventLabel); return false; }
private @Nullable Conversation loadActiveConversationGuarded() throws ConversationLoadException { // try to load an active conversation from metadata first try { if (conversationMetadata.hasItems()) { return loadConversationFromMetadata(conversationMetadata); } // try to load legacy conversation Conversation legacyConversation = migrateLegacyConversation(getContext()); if (legacyConversation != null) { return legacyConversation; } } catch (Exception e) { ApptentiveLog.e(e, "Exception while loading conversation"); logException(e); // do not re-create a conversation if the last loading was unsuccessful throw new ConversationLoadException("Unable to load conversation", e); } // no active conversations: create a new one ApptentiveLog.i(CONVERSATION, "Creating 'anonymous' conversation..."); File dataFile = generateConversationDataFilename(); File messagesFile = generateMessagesFilename(); Conversation conversation = new Conversation(dataFile, messagesFile, encryptionKey); conversation.setState(ANONYMOUS_PENDING); fetchConversationToken(conversation); return conversation; }
public static synchronized boolean engage(Context context, Conversation conversation, String vendor, String interaction, String interactionId, String eventName, String data, Map<String, Object> customData, ExtendedData... extendedData) { checkConversationQueue(); if (context == null) { throw new IllegalArgumentException("Context is null"); } if (conversation == null) { throw new IllegalArgumentException("Conversation is null"); } Assert.assertTrue(ApptentiveInternal.isApptentiveRegistered()); if (!ApptentiveInternal.isApptentiveRegistered()) { return false; } try { String eventLabel = generateEventLabel(vendor, interaction, eventName); ApptentiveLog.i(INTERACTIONS, "Engage event: '%s'", eventLabel); String versionName = ApptentiveInternal.getInstance().getApplicationVersionName(); int versionCode = ApptentiveInternal.getInstance().getApplicationVersionCode(); conversation.getEventData().storeEventForCurrentAppVersion(Util.currentTimeSeconds(), versionCode, versionName, eventLabel); conversation.addPayload(new EventPayload(eventLabel, interactionId, data, customData, extendedData)); return doEngage(conversation, context, eventLabel); } catch (Exception e) { ApptentiveLog.e(INTERACTIONS, e, "Exception while engaging event '%s'", eventName); logException(e); } return false; }
public void startSession() { assertNull(sessionId, "Another session is active"); sessionId = generateSessionId(); ApptentiveLog.d(CONVERSATION, "Started session '%s'", sessionId); }
private void onAppExit(final Context appContext) { checkConversationQueue(); if (isConversationActive()) { engageInternal(appContext, EventPayload.EventLabel.app__exit.getLabelName()); getConversation().endSession(); } }
FieldManager fieldManager = new FieldManager(context, conversation.getVersionHistory(), conversation.getEventData(), conversation.getPerson(), conversation.getDevice(), conversation.getAppRelease()); if (invocation.isCriteriaMet(fieldManager, true)) { interactionIdToLaunch = invocation.getInteractionId(); String interactionsString = conversation.getInteractions(); if (interactionsString != null) { try {
public void setConversationId(String conversationId) { getConversationData().setConversationId(conversationId); }
public Device getDevice() { return getConversationData().getDevice(); }