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); }
public HttpJsonRequest createFirstLoginRequest(String token, AppRelease appRelease, Sdk sdk, Device device, HttpRequest.Listener<HttpJsonRequest> listener) { if (token == null) { throw new IllegalArgumentException("Token is null"); } ConversationTokenRequest conversationTokenRequest = new ConversationTokenRequest(); conversationTokenRequest.setSdkAndAppRelease(SdkManager.getPayload(sdk), AppReleaseManager.getPayload(appRelease)); conversationTokenRequest.setDevice(DeviceManager.getDiffPayload(null, device)); try { conversationTokenRequest.put("token", token); } catch (JSONException e) { // Can't happen } HttpJsonRequest request = createJsonRequest(ENDPOINT_LOG_IN_TO_NEW_CONVERSATION, conversationTokenRequest, HttpRequestMethod.POST); request.addListener(listener); return request; }
@Test public void criteriaApplicationVersionCode() throws JSONException { AppRelease appRelease = AppReleaseManager.generateCurrentAppRelease(targetContext, null); String json = loadTextAssetAsString(TEST_DATA_DIR + "criteria/testCriteriaApplicationVersionCode.json"); json = json.replace("\"APPLICATION_VERSION_CODE\"", String.valueOf(appRelease.getVersionCode())); InteractionCriteria criteria = new InteractionCriteria(json); FieldManager fieldManager = new FieldManager(targetContext, new VersionHistory(), new EventData(), new Person(), new Device(), appRelease); assertTrue(criteria.isMet(fieldManager)); }
conversation.addPayload(AppReleaseManager.getPayload(sdk, appRelease)); conversation.setAppRelease(appRelease); conversation.setSdk(sdk);
@Test public void criteriaApplicationDebug() throws JSONException { AppRelease appRelease = AppReleaseManager.generateCurrentAppRelease(targetContext, null); String json = loadTextAssetAsString(TEST_DATA_DIR + "criteria/testCriteriaApplicationDebug.json"); json = json.replace("APPLICATION_DEBUG", Boolean.toString(appRelease.isDebug())); InteractionCriteria criteria = new InteractionCriteria(json); FieldManager fieldManager = new FieldManager(targetContext, new VersionHistory(), new EventData(), new Person(), new Device(), appRelease); assertTrue(criteria.isMet(fieldManager)); }
conversationTokenRequest.setSdkAndAppRelease(SdkManager.getPayload(sdk), AppReleaseManager.getPayload(appRelease));
@Test public void criteriaApplicationVersionName() throws JSONException { AppRelease appRelease = AppReleaseManager.generateCurrentAppRelease(targetContext, null); String json = loadTextAssetAsString(TEST_DATA_DIR + "criteria/testCriteriaApplicationVersionName.json"); json = json.replace("APPLICATION_VERSION_NAME", appRelease.getVersionName()); InteractionCriteria criteria = new InteractionCriteria(json); FieldManager fieldManager = new FieldManager(targetContext, new VersionHistory(), new EventData(), new Person(), new Device(), appRelease); assertTrue(criteria.isMet(fieldManager)); }
@Test public void upgradeMessageOnVersionCode() throws JSONException { AppRelease appRelease = AppReleaseManager.generateCurrentAppRelease(targetContext, null); String json = loadTextAssetAsString(TEST_DATA_DIR + "payloads/upgradeMessageOnVersionCode.json") .replace("\"APPLICATION_VERSION_CODE\"", String.valueOf(appRelease.getVersionCode())); InteractionCriteria criteria = new InteractionCriteria(json); VersionHistory versionHistory = new VersionHistory(); FieldManager fieldManager = new FieldManager(targetContext, versionHistory, new EventData(), new Person(), new Device(), appRelease); versionHistory.updateVersionHistory(Util.currentTimeSeconds(), versionCode, versionName); // Test version targeted UpgradeMessage // Saw this build too long ago. ApptentiveLog.e("ONE"); versionHistory.clear(); versionHistory.updateVersionHistory(Util.currentTimeSeconds() - 1000000, 1, "1.0"); versionHistory.updateVersionHistory(Util.currentTimeSeconds() - 600000, versionCode, "2.0.0"); ApptentiveLog.e("Current time: %f", Util.currentTimeSeconds()); assertFalse(criteria.isMet(fieldManager)); // Haven't upgraded ApptentiveLog.e("TWO"); versionHistory.clear(); versionHistory.updateVersionHistory(Util.currentTimeSeconds() - 499500, versionCode, "2.0.0"); assertFalse(criteria.isMet(fieldManager)); // Just right ApptentiveLog.e("THREE"); versionHistory.clear(); versionHistory.updateVersionHistory(Util.currentTimeSeconds() - 1000000, 1, "1.0"); versionHistory.updateVersionHistory(Util.currentTimeSeconds() - 499500, versionCode, "2.0.0"); assertTrue(criteria.isMet(fieldManager)); }
@Test public void upgradeMessageOnVersionName() throws JSONException { AppRelease appRelease = AppReleaseManager.generateCurrentAppRelease(targetContext, null); String json = loadTextAssetAsString(TEST_DATA_DIR + "payloads/upgradeMessageOnVersionName.json") .replace("APPLICATION_VERSION_NAME", appRelease.getVersionName()); InteractionCriteria criteria = new InteractionCriteria(json); VersionHistory versionHistory = new VersionHistory(); EventData eventData = new EventData(); FieldManager fieldManager = new FieldManager(targetContext, versionHistory, eventData, new Person(), new Device(), appRelease); versionHistory.updateVersionHistory(Util.currentTimeSeconds(), versionCode, versionName); eventData.storeEventForCurrentAppVersion(Util.currentTimeSeconds(), versionCode, versionName, "app.launch"); // Test version targeted UpgradeMessage // Saw this build too long ago. ApptentiveLog.e("ONE"); versionHistory.clear(); versionHistory.updateVersionHistory(Util.currentTimeSeconds() - 1000000, 3, "1.0"); versionHistory.updateVersionHistory(Util.currentTimeSeconds() - 600000, 4, "2.0.0"); assertFalse(criteria.isMet(fieldManager)); // Haven't upgraded ApptentiveLog.e("TWO"); versionHistory.clear(); versionHistory.updateVersionHistory(Util.currentTimeSeconds() - 499500, 4, "2.0.0"); assertFalse(criteria.isMet(fieldManager)); // Just right ApptentiveLog.e("THREE"); versionHistory.clear(); versionHistory.updateVersionHistory(Util.currentTimeSeconds() - 1000000, 3, "1.0"); versionHistory.updateVersionHistory(Util.currentTimeSeconds() - 499500, 4, "2.0.0"); assertTrue(criteria.isMet(fieldManager)); }
AppRelease appRelease = AppReleaseManager.generateCurrentAppRelease(targetContext, null); String json = loadTextAssetAsString(TEST_DIR + "testInteractionInvokesTotal.json");