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 Conversation getConversation() { return conversationManager.getActiveConversation(); }
public @Nullable ConversationProxy getConversationProxy() { return conversationManager.getActiveConversationProxy(); }
if (appIsInForeground) { conversation.fetchInteractions(getContext()); fetchAppConfiguration(conversation); updateMetadataItems(conversation); if (ApptentiveLog.canLog(VERBOSE)) { printMetadata(conversationMetadata, "Updated Metadata");
notifyFetchStarted(conversation); final Context context = getContext(); if (context == null) { ApptentiveLog.w(CONVERSATION, "Unable to fetch conversation token: context reference is lost"); notifyFetchFinished(conversation, false); return null; HttpRequest existingRequest = getHttpClient().findRequest(TAG_FETCH_CONVERSATION_TOKEN_REQUEST); if (existingRequest != null) { ApptentiveLog.d(CONVERSATION, "Conversation already fetching"); conversationTokenRequest.setSdkAndAppRelease(SdkManager.getPayload(sdk), AppReleaseManager.getPayload(appRelease)); HttpRequest request = getHttpClient() .createConversationTokenRequest(conversationTokenRequest, new HttpRequest.Listener<HttpJsonRequest>() { @Override
sendFirstLoginRequest(userId, token, callback); return; sendLoginRequest(conversationItem.getConversationId(), userId, token, callback); return; fetchConversationToken(activeConversation) : fetchLegacyConversation(activeConversation); if (fetchRequest == null) { ApptentiveLog.e(CONVERSATION, "Unable to login: fetch request failed to send"); sendLoginRequest(activeConversation.getConversationId(), userId, token, callback); break; case LOGGED_IN: if (StringUtils.equal(activeConversation.getUserId(), userId)) { ApptentiveLog.w(CONVERSATION, "Refreshing auth token for logged in user: \"%s\"", userId); sendLoginRequest(activeConversation.getConversationId(), userId, token, callback); return;
if (item != null) { ApptentiveLog.i(CONVERSATION, "Loading 'logged-in' conversation..."); return loadConversation(item); if (item != null) { ApptentiveLog.i(CONVERSATION, "Loading 'anonymous' conversation..."); return loadConversation(item); if (item != null) { ApptentiveLog.i(CONVERSATION, "Loading 'anonymous pending' conversation..."); final Conversation conversation = loadConversation(item); fetchConversationToken(conversation); return conversation; if (item != null) { ApptentiveLog.i(CONVERSATION, "Loading 'legacy pending' conversation..."); final Conversation conversation = loadConversation(item); fetchLegacyConversation(conversation); return conversation;
ApptentiveLog.i(CONVERSATION, "Creating 'legacy' conversation..."); File dataFile = generateConversationDataFilename(); File messagesFile = generateMessagesFilename(); Conversation conversation = new Conversation(dataFile, messagesFile, encryptionKey); conversation.setState(LEGACY_PENDING); fetchLegacyConversation(conversation);
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."); } }
final Sdk sdk = SdkManager.generateCurrentSdk(getContext()); final Device device = DeviceManager.generateNewDevice(getContext()); HttpJsonRequest request = getHttpClient().createFirstLoginRequest(token, appRelease, sdk, device, new HttpRequest.Listener<HttpJsonRequest>() { @Override public void onFinish(HttpJsonRequest request) {
private void fetchAppConfiguration(Conversation conversation) { checkConversationQueue(); try { fetchAppConfigurationGuarded(conversation); } catch (Exception e) { ApptentiveLog.e(CONVERSATION, e, "Exception while fetching app configuration"); logException(e); } }
private ApptentiveInternal(Application application, String apptentiveKey, String apptentiveSignature, String serverUrl, boolean shouldEncryptStorage) { if (StringUtils.isNullOrEmpty(apptentiveKey)) { throw new IllegalArgumentException("Apptentive Key is null or empty"); } if (StringUtils.isNullOrEmpty(apptentiveSignature)) { throw new IllegalArgumentException("Apptentive Signature is null or empty"); } this.apptentiveKey = apptentiveKey; this.apptentiveSignature = apptentiveSignature; this.serverUrl = serverUrl; SecurityManager.init(application.getApplicationContext(), shouldEncryptStorage); appContext = application.getApplicationContext(); globalSharedPrefs = application.getSharedPreferences(Constants.PREF_NAME, Context.MODE_PRIVATE); apptentiveHttpClient = new ApptentiveHttpClient(apptentiveKey, apptentiveSignature, getEndpointBase(globalSharedPrefs)); conversationManager = new ConversationManager(appContext, Util.getInternalDir(appContext, CONVERSATIONS_DIR, true), SecurityManager.getMasterKey()); appRelease = AppReleaseManager.generateCurrentAppRelease(application, this); taskManager = new ApptentiveTaskManager(appContext, apptentiveHttpClient, SecurityManager.getMasterKey()); ApptentiveNotificationCenter.defaultCenter() .addObserver(NOTIFICATION_CONVERSATION_STATE_DID_CHANGE, this) .addObserver(NOTIFICATION_CONVERSATION_WILL_LOGOUT, this) .addObserver(NOTIFICATION_AUTHENTICATION_FAILED, this) .addObserver(NOTIFICATION_INTERACTION_MANIFEST_FETCHED, this) .addObserver(NOTIFICATION_APP_ENTERED_FOREGROUND, this) .addObserver(NOTIFICATION_APP_ENTERED_BACKGROUND, this) .addObserver(NOTIFICATION_CONFIGURATION_FETCH_DID_FINISH, this); }
private void fetchAppConfigurationGuarded(final Conversation conversation) { ApptentiveLog.d(APP_CONFIGURATION, "Fetching app configuration..."); HttpRequest existingRequest = getHttpClient().findRequest(TAG_FETCH_APP_CONFIGURATION_REQUEST); if (existingRequest != null) { ApptentiveLog.d(APP_CONFIGURATION, "Can't fetch app configuration: another request already pending"); boolean debuggable = RuntimeUtils.isAppDebuggable(getContext()); if (!debuggable) { ApptentiveLog.d(APP_CONFIGURATION, "Can't fetch app configuration: the old configuration is still valid"); HttpJsonRequest request = getHttpClient() .createAppConfigurationRequest(conversation.getConversationId(), conversation.getConversationToken(), new HttpRequest.Listener<HttpJsonRequest>() {