public static synchronized void updateVersionHistory(int newVersionCode, String newVersionName) { updateVersionHistory(newVersionCode, newVersionName, Util.currentTimeSeconds()); }
@Override public String description(String fieldName, Comparable first, Comparable second) { if (!(second instanceof BigDecimal)) { return StringUtils.format("%s ('%s') before date '%s'", fieldName, toPrettyDate(first), toPrettyDate(second)); } Double offset = ((BigDecimal) second).doubleValue(); Double currentTime = Util.currentTimeSeconds(); return StringUtils.format("%s ('%s') before date '%s'", fieldName, toPrettyDate(first), toPrettyDate(currentTime + offset)); } },
@Override public String description(String fieldName, Comparable first, Comparable second) { if (!(second instanceof BigDecimal)) { return StringUtils.format("%s ('%s') after date '%s'", fieldName, toPrettyDate(first), toPrettyDate(second)); } Double offset = ((BigDecimal) second).doubleValue(); Double currentTime = Util.currentTimeSeconds(); return StringUtils.format("%s ('%s') after date '%s'", fieldName, toPrettyDate(first), toPrettyDate(currentTime + offset)); } },
@Override public boolean apply(Comparable first, Comparable second) { if (!(first instanceof Apptentive.DateTime)) { return false; } // The parameter for $before is an offset in seconds added to the current time. if (!(second instanceof BigDecimal)) { return false; } Double offset = ((BigDecimal) second).doubleValue(); Double currentTime = Util.currentTimeSeconds(); Apptentive.DateTime offsetDateTime = new Apptentive.DateTime(currentTime + offset); return ((Apptentive.DateTime) first).compareTo(offsetDateTime) < 0; }
@Override public boolean apply(Comparable first, Comparable second) { if (!(first instanceof Apptentive.DateTime)) { return false; } // The parameter for $after is an offset in seconds added to the current time. if (!(second instanceof BigDecimal)) { return false; } Double offset = ((BigDecimal) second).doubleValue(); Double currentTime = Util.currentTimeSeconds(); Apptentive.DateTime offsetDateTime = new Apptentive.DateTime(currentTime + offset); return ((Apptentive.DateTime) first).compareTo(offsetDateTime) > 0; }
/** * Returns the timestamp at the first install of this app that Apptentive was aware of. */ public Apptentive.DateTime getTimeAtInstallTotal() { // Simply return the first item's timestamp, if there is one. if (versionHistoryItems.size() > 0) { return new Apptentive.DateTime(versionHistoryItems.get(0).getTimestamp()); } return new Apptentive.DateTime(Util.currentTimeSeconds()); }
protected ConversationItem(PayloadType type) { super(type); double seconds = Util.currentTimeSeconds(); int utcOffset = Util.getUtcOffset(); setClientCreatedAt(seconds); setClientCreatedAtUtcOffset(utcOffset); }
/** * Returns the timestamp at the first install of the current versionCode of this app that Apptentive was aware of. */ public Apptentive.DateTime getTimeAtInstallForVersionCode(int versionCode) { for (VersionHistoryItem item : versionHistoryItems) { if (item.getVersionCode() == versionCode) { return new Apptentive.DateTime(item.getTimestamp()); } } return new Apptentive.DateTime(Util.currentTimeSeconds()); }
/** * Returns the timestamp at the first install of the current versionName of this app that Apptentive was aware of. */ public Apptentive.DateTime getTimeAtInstallForVersionName(String versionName) { for (VersionHistoryItem item : versionHistoryItems) { Apptentive.Version entryVersionName = new Apptentive.Version(); Apptentive.Version currentVersionName = new Apptentive.Version(); entryVersionName.setVersion(item.getVersionName()); currentVersionName.setVersion(versionName); if (entryVersionName.equals(currentVersionName)) { return new Apptentive.DateTime(item.getTimestamp()); } } return new Apptentive.DateTime(Util.currentTimeSeconds()); }
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; }
@Test public void criteriaProcessingPerformance() throws JSONException { if (isRunningOnEmulator()) { ApptentiveLog.e("Running on emulator. Skipping test."); return; } final int iterations = 100; String json = loadTextAssetAsString(TEST_DATA_DIR + "payloads/testListOfVariousInteractions.json"); InteractionCriteria criteria = new InteractionCriteria(json); VersionHistory versionHistory = new VersionHistory(); EventData eventData = new EventData(); FieldManager fieldManager = new FieldManager(targetContext, versionHistory, eventData, new Person(), new Device(), new AppRelease()); versionHistory.updateVersionHistory(Util.currentTimeSeconds(), versionCode, versionName); eventData.storeEventForCurrentAppVersion(Util.currentTimeSeconds(), versionCode, versionName, "app.launch"); eventData.storeEventForCurrentAppVersion(Util.currentTimeSeconds(), versionCode, versionName, "app.launch"); eventData.storeEventForCurrentAppVersion(Util.currentTimeSeconds(), versionCode, versionName, "big.win"); eventData.storeEventForCurrentAppVersion(Util.currentTimeSeconds(), versionCode, versionName, "big.win"); long start = System.currentTimeMillis(); for (int i = 0; i < iterations; i++) { assertTrue(criteria.isMet(fieldManager)); } long end = System.currentTimeMillis(); long duration = end - start; double average = (double) duration / iterations; double limit = 7d; String message = String.format("Finished %d iterations in %,dms, average of %.2fms per run, limit was %.2fms", iterations, duration, average, limit); ApptentiveLog.e(message); assertTrue(message, average < limit); }
@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)); }
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; }
@Test public void savingCodePointAndCheckingForApplicableInteraction() throws JSONException { if (isRunningOnEmulator()) { ApptentiveLog.e("Running on emulator. Skipping test."); return; } final int iterations = 100; String json = loadTextAssetAsString(TEST_DATA_DIR + "payloads/testListOfVariousInteractions.json"); InteractionCriteria criteria = new InteractionCriteria(json); EventData eventData = new EventData(); FieldManager fieldManager = new FieldManager(targetContext, new VersionHistory(), eventData, new Person(), new Device(), new AppRelease()); eventData.storeEventForCurrentAppVersion(Util.currentTimeSeconds(), versionCode, versionName, "app.launch"); eventData.storeEventForCurrentAppVersion(Util.currentTimeSeconds(), versionCode, versionName, "app.launch"); eventData.storeEventForCurrentAppVersion(Util.currentTimeSeconds(), versionCode, versionName, "big.win"); eventData.storeEventForCurrentAppVersion(Util.currentTimeSeconds(), versionCode, versionName, "big.win"); long start = System.currentTimeMillis(); for (int i = 0; i < iterations; i++) { assertTrue(criteria.isMet(fieldManager)); } long end = System.currentTimeMillis(); long duration = end - start; double average = (double) duration / iterations; double limit = 20d; String message = String.format("Finished %d iterations in %,dms, average of %.2fms per run, limit was %.2fms", iterations, duration, average, limit); ApptentiveLog.e(message); assertTrue(message, average < limit); }
@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)); }
@Test public void queriesAgainstDevice() throws JSONException { String json = loadTextAssetAsString(TEST_DATA_DIR + "testQueriesAgainstDevice.json"); json = json.replace("\"OS_API_LEVEL\"", String.valueOf(Build.VERSION.SDK_INT)); InteractionCriteria criteria = new InteractionCriteria(json); Device device = DeviceManager.generateNewDevice(targetContext); device.getCustomData().put("foo", "bar"); EventData eventData = new EventData(); FieldManager fieldManager = new FieldManager(targetContext, new VersionHistory(), eventData, new Person(), device, new AppRelease()); // 0 assertTrue(criteria.isMet(fieldManager)); // 1 eventData.storeEventForCurrentAppVersion(Util.currentTimeSeconds(), versionCode, versionName, "switch.code.point"); assertTrue(criteria.isMet(fieldManager)); // 2 eventData.storeEventForCurrentAppVersion(Util.currentTimeSeconds(), versionCode, versionName, "switch.code.point"); assertFalse(criteria.isMet(fieldManager)); // 3 eventData.storeEventForCurrentAppVersion(Util.currentTimeSeconds(), versionCode, versionName, "switch.code.point"); assertTrue(criteria.isMet(fieldManager)); // 4 eventData.storeEventForCurrentAppVersion(Util.currentTimeSeconds(), versionCode, versionName, "switch.code.point"); assertTrue(criteria.isMet(fieldManager)); }
@Test public void queriesAgainstPerson() throws JSONException { String json = loadTextAssetAsString(TEST_DATA_DIR + "testQueriesAgainstPerson.json"); InteractionCriteria criteria = new InteractionCriteria(json); Person person = new Person(); person.setEmail("example@example.com"); person.getCustomData().put("foo", "bar"); EventData eventData = new EventData(); FieldManager fieldManager = new FieldManager(targetContext, new VersionHistory(), eventData, person, new Device(), new AppRelease()); // 0 assertTrue(criteria.isMet(fieldManager)); // 1 eventData.storeEventForCurrentAppVersion(Util.currentTimeSeconds(), versionCode, versionName, "switch.code.point"); assertFalse(criteria.isMet(fieldManager)); // 2 eventData.storeEventForCurrentAppVersion(Util.currentTimeSeconds(), versionCode, versionName, "switch.code.point"); assertTrue(criteria.isMet(fieldManager)); // 3 person.getCustomData().put("foo", "bar"); eventData.storeEventForCurrentAppVersion(Util.currentTimeSeconds(), versionCode, versionName, "switch.code.point"); assertTrue(criteria.isMet(fieldManager)); // 4 eventData.storeEventForCurrentAppVersion(Util.currentTimeSeconds(), versionCode, versionName, "switch.code.point"); assertFalse(criteria.isMet(fieldManager)); // 5 eventData.storeEventForCurrentAppVersion(Util.currentTimeSeconds(), versionCode, versionName, "switch.code.point"); assertTrue(criteria.isMet(fieldManager)); }
@Test public void queriesAgainstSdk() throws JSONException { String json = loadTextAssetAsString(TEST_DATA_DIR + "testQueriesAgainstSdk.json"); InteractionCriteria criteria = new InteractionCriteria(json); Device device = new Device(); device.getCustomData().put("foo", "bar"); EventData eventData = new EventData(); FieldManager fieldManager = new FieldManager(targetContext, new VersionHistory(), eventData, new Person(), device, new AppRelease()); // 0 assertTrue(criteria.isMet(fieldManager)); // 1 eventData.storeEventForCurrentAppVersion(Util.currentTimeSeconds(), versionCode, versionName, "switch.code.point"); assertTrue(criteria.isMet(fieldManager)); // 2 eventData.storeEventForCurrentAppVersion(Util.currentTimeSeconds(), versionCode, versionName, "switch.code.point"); assertFalse(criteria.isMet(fieldManager)); } }
conversation.getVersionHistory().updateVersionHistory(Util.currentTimeSeconds(), currentVersionCode, currentVersionName);
return new Apptentive.DateTime(Util.currentTimeSeconds());