From 470f3de24fae86bf706b734b50abdd85415d94f1 Mon Sep 17 00:00:00 2001 From: Alex Lementuev Date: Wed, 3 Feb 2021 19:46:37 -0500 Subject: [PATCH 01/17] Updated Apptentive dependency to 5.6.2 --- build.gradle | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build.gradle b/build.gradle index eea0aee..b59f324 100644 --- a/build.gradle +++ b/build.gradle @@ -24,5 +24,5 @@ android { } dependencies { - implementation 'com.apptentive:apptentive-android:5.6.1' + implementation 'com.apptentive:apptentive-android:5.6.2' } From 4c1fb0ef588f443622ea9bd616684cc85976dcf5 Mon Sep 17 00:00:00 2001 From: Alex Lementuev Date: Sat, 1 May 2021 18:59:48 -0400 Subject: [PATCH 02/17] Added StringUtils --- .../java/com/mparticle/kits/StringUtils.java | 31 +++++++++++++++++++ .../com/mparticle/kits/StringUtilsTest.java | 17 ++++++++++ 2 files changed, 48 insertions(+) create mode 100644 src/main/java/com/mparticle/kits/StringUtils.java create mode 100644 src/test/java/com/mparticle/kits/StringUtilsTest.java diff --git a/src/main/java/com/mparticle/kits/StringUtils.java b/src/main/java/com/mparticle/kits/StringUtils.java new file mode 100644 index 0000000..500ca5d --- /dev/null +++ b/src/main/java/com/mparticle/kits/StringUtils.java @@ -0,0 +1,31 @@ +package com.mparticle.kits; + +final class StringUtils { + public static Number tryParseNumber(String value) { + Long longValue = tryParseLong(value); + if (longValue != null) { + if (longValue >= Integer.MIN_VALUE && longValue <= Integer.MAX_VALUE) { + return longValue.intValue(); + } + return longValue; + } + + return tryParseDouble(value); + } + + public static Long tryParseLong(String value) { + try { + return Long.valueOf(value); + } catch (NumberFormatException e) { + return null; + } + } + + public static Double tryParseDouble(String value) { + try { + return Double.valueOf(value); + } catch (NumberFormatException e) { + return null; + } + } +} diff --git a/src/test/java/com/mparticle/kits/StringUtilsTest.java b/src/test/java/com/mparticle/kits/StringUtilsTest.java new file mode 100644 index 0000000..004bf66 --- /dev/null +++ b/src/test/java/com/mparticle/kits/StringUtilsTest.java @@ -0,0 +1,17 @@ +package com.mparticle.kits; + +import org.junit.Test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; + +public class StringUtilsTest { + @Test + public void testParseNumber() { + assertEquals(Integer.valueOf(12345), (Integer) StringUtils.tryParseNumber("12345")); + assertEquals(Long.valueOf(21474836479L), (Long) StringUtils.tryParseNumber("21474836479")); + assertEquals(Long.valueOf(-21474836489L), (Long) StringUtils.tryParseNumber("-21474836489")); + assertEquals(Double.valueOf(12345.0), (Double) StringUtils.tryParseNumber("12345.0")); + assertNull(StringUtils.tryParseNumber("test")); + } +} \ No newline at end of file From 7c999bcead4cb96f8e8d787b2ea5250d5823f2eb Mon Sep 17 00:00:00 2001 From: Alex Lementuev Date: Sun, 2 May 2021 16:35:21 -0400 Subject: [PATCH 03/17] Added data transform --- .../com/mparticle/kits/ApptentiveKit.java | 4 +- .../com/mparticle/kits/CustomDataParser.java | 41 ++++++++++++ .../mparticle/kits/CustomDataParserTest.java | 66 +++++++++++++++++++ 3 files changed, 109 insertions(+), 2 deletions(-) create mode 100644 src/main/java/com/mparticle/kits/CustomDataParser.java create mode 100644 src/test/java/com/mparticle/kits/CustomDataParserTest.java diff --git a/src/main/java/com/mparticle/kits/ApptentiveKit.java b/src/main/java/com/mparticle/kits/ApptentiveKit.java index ebb73d8..177e5de 100644 --- a/src/main/java/com/mparticle/kits/ApptentiveKit.java +++ b/src/main/java/com/mparticle/kits/ApptentiveKit.java @@ -170,9 +170,9 @@ public List logException(Exception exception, Map logEvent(MPEvent event) { - Map customData = event.getInfo(); + Map customData = CustomDataParser.parse(event.getInfo()); if (customData != null) { - Apptentive.engage(getContext(), event.getEventName(), Collections.unmodifiableMap(customData)); + Apptentive.engage(getContext(), event.getEventName(), customData); } else { Apptentive.engage(getContext(), event.getEventName()); } diff --git a/src/main/java/com/mparticle/kits/CustomDataParser.java b/src/main/java/com/mparticle/kits/CustomDataParser.java new file mode 100644 index 0000000..3763a9e --- /dev/null +++ b/src/main/java/com/mparticle/kits/CustomDataParser.java @@ -0,0 +1,41 @@ +package com.mparticle.kits; + +import com.apptentive.android.sdk.ApptentiveLog; + +import java.util.HashMap; +import java.util.Map; + +final class CustomDataParser { + public static Map parse(Map map) { + Map res = new HashMap<>(); + for (Map.Entry e : map.entrySet()) { + res.put(e.getKey(), parseValue(e.getValue())); + } + return res; + } + + private static Object parseValue(String value) { + try { + return value != null ? parseValueGuarded(value) : null; + } catch (Exception e) { + ApptentiveLog.e(e, "Unable to parse value: '%s'", value); + return value; + } + } + + private static Object parseValueGuarded(String value) { + // check for boolean + if ("true".equalsIgnoreCase(value) || "false".equalsIgnoreCase(value)) { + return Boolean.parseBoolean(value); + } + + // check for number + Number number = StringUtils.tryParseNumber(value); + if (number != null) { + return number; + } + + // default to the original value + return value; + } +} diff --git a/src/test/java/com/mparticle/kits/CustomDataParserTest.java b/src/test/java/com/mparticle/kits/CustomDataParserTest.java new file mode 100644 index 0000000..519bd45 --- /dev/null +++ b/src/test/java/com/mparticle/kits/CustomDataParserTest.java @@ -0,0 +1,66 @@ +package com.mparticle.kits; + +import org.junit.Test; + +import java.util.HashMap; +import java.util.Map; + +import static org.junit.Assert.*; + +public class CustomDataParserTest { + @Test + public void testParseData() { + Map data = new HashMap<>(); + + // boolean + data.put("key-1", "true"); + data.put("key-2", "True"); + data.put("key-3", "false"); + data.put("key-4", "False"); + + // integer + data.put("key-5", "12345"); + data.put("key-6", "-12345"); + data.put("key-7", Integer.toString(Integer.MIN_VALUE)); + data.put("key-8", Integer.toString(Integer.MAX_VALUE)); + + // long + data.put("key-9", Long.toString(Long.MIN_VALUE)); + data.put("key-10", Long.toString(Long.MAX_VALUE)); + + // double + data.put("key-11", "3.14"); + data.put("key-12", "-3.14"); + + // string + data.put("key-13", "test"); + + Map expected = new HashMap<>(); + + // boolean + expected.put("key-1", Boolean.TRUE); + expected.put("key-2", Boolean.TRUE); + expected.put("key-3", Boolean.FALSE); + expected.put("key-4", Boolean.FALSE); + + // integer + expected.put("key-5", 12345); + expected.put("key-6", -12345); + expected.put("key-7", Integer.MIN_VALUE); + expected.put("key-8", Integer.MAX_VALUE); + + // long + expected.put("key-9", Long.MIN_VALUE); + expected.put("key-10", Long.MAX_VALUE); + + // double + expected.put("key-11", 3.14); + expected.put("key-12", -3.14); + + // string + expected.put("key-13", "test"); + + Map actual = CustomDataParser.parse(data); + assertEquals(expected, actual); + } +} \ No newline at end of file From 669f47cd21917edbdcb737c59acc9f6aaf9220c7 Mon Sep 17 00:00:00 2001 From: Alex Lementuev Date: Mon, 3 May 2021 18:28:28 -0400 Subject: [PATCH 04/17] Fixed parsing person data --- src/main/java/com/mparticle/kits/ApptentiveKit.java | 9 ++++++--- src/main/java/com/mparticle/kits/CustomDataParser.java | 4 ++-- .../java/com/mparticle/kits/CustomDataParserTest.java | 2 +- 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/mparticle/kits/ApptentiveKit.java b/src/main/java/com/mparticle/kits/ApptentiveKit.java index 177e5de..6b95754 100644 --- a/src/main/java/com/mparticle/kits/ApptentiveKit.java +++ b/src/main/java/com/mparticle/kits/ApptentiveKit.java @@ -28,6 +28,9 @@ import java.util.List; import java.util.Map; +import static com.mparticle.kits.CustomDataParser.parseCustomData; +import static com.mparticle.kits.CustomDataParser.parseValue; + public class ApptentiveKit extends KitIntegration implements KitIntegration.EventListener, KitIntegration.CommerceListener, @@ -125,7 +128,7 @@ public void setAllUserAttributes(Map attributes, Map logException(Exception exception, Map logEvent(MPEvent event) { - Map customData = CustomDataParser.parse(event.getInfo()); + Map customData = event.getInfo(); if (customData != null) { - Apptentive.engage(getContext(), event.getEventName(), customData); + Apptentive.engage(getContext(), event.getEventName(), parseCustomData(customData)); } else { Apptentive.engage(getContext(), event.getEventName()); } diff --git a/src/main/java/com/mparticle/kits/CustomDataParser.java b/src/main/java/com/mparticle/kits/CustomDataParser.java index 3763a9e..03a00a5 100644 --- a/src/main/java/com/mparticle/kits/CustomDataParser.java +++ b/src/main/java/com/mparticle/kits/CustomDataParser.java @@ -6,7 +6,7 @@ import java.util.Map; final class CustomDataParser { - public static Map parse(Map map) { + public static Map parseCustomData(Map map) { Map res = new HashMap<>(); for (Map.Entry e : map.entrySet()) { res.put(e.getKey(), parseValue(e.getValue())); @@ -14,7 +14,7 @@ public static Map parse(Map map) { return res; } - private static Object parseValue(String value) { + public static Object parseValue(String value) { try { return value != null ? parseValueGuarded(value) : null; } catch (Exception e) { diff --git a/src/test/java/com/mparticle/kits/CustomDataParserTest.java b/src/test/java/com/mparticle/kits/CustomDataParserTest.java index 519bd45..236f6fd 100644 --- a/src/test/java/com/mparticle/kits/CustomDataParserTest.java +++ b/src/test/java/com/mparticle/kits/CustomDataParserTest.java @@ -60,7 +60,7 @@ public void testParseData() { // string expected.put("key-13", "test"); - Map actual = CustomDataParser.parse(data); + Map actual = CustomDataParser.parseCustomData(data); assertEquals(expected, actual); } } \ No newline at end of file From ec95af41dcd9d234877b70d42468db1afc502f00 Mon Sep 17 00:00:00 2001 From: Alex Lementuev Date: Mon, 3 May 2021 18:47:53 -0400 Subject: [PATCH 05/17] Fixed parsing custom data --- .../com/mparticle/kits/ApptentiveKit.java | 23 ++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/mparticle/kits/ApptentiveKit.java b/src/main/java/com/mparticle/kits/ApptentiveKit.java index 6b95754..17926e3 100644 --- a/src/main/java/com/mparticle/kits/ApptentiveKit.java +++ b/src/main/java/com/mparticle/kits/ApptentiveKit.java @@ -96,7 +96,7 @@ public void setUserAttribute(String attributeKey, String attributeValue) { } else if (attributeKey.equalsIgnoreCase(MParticle.UserAttributes.LASTNAME)) { lastName = attributeValue; } else { - Apptentive.addCustomPersonData(attributeKey, attributeValue); + addCustomPersonData(attributeKey, parseValue(attributeValue)); } String fullName; @@ -128,7 +128,7 @@ public void setAllUserAttributes(Map attributes, Map logEvent(CommerceEvent event) { Map customData = event.getCustomAttributes(); Apptentive.engage(getContext(), String.format("eCommerce - %s", event.getProductAction()), - customData == null ? null : Collections.unmodifiableMap(customData), + customData == null ? null : parseCustomData(customData), apptentiveCommerceData); List messages = new LinkedList(); messages.add(ReportingMessage.fromEvent(this, event)); @@ -290,4 +290,21 @@ public void onReceiveNotification(ApptentiveNotification notification) { } //endregion + + //region Helpers + + /* Apptentive SDK does not provide a function which accepts Object as custom data so we need to cast */ + private void addCustomPersonData(String key, Object value) { + if (value instanceof String) { + Apptentive.addCustomPersonData(key, (String) value); + } else if (value instanceof Boolean) { + Apptentive.addCustomPersonData(key, (Boolean) value); + } else if (value instanceof Number) { + Apptentive.addCustomPersonData(key, (Number) value); + } else { + ApptentiveLog.e("Unexpected custom person data type: %s", value != null ? value.getClass() : null); + } + } + + //endregion } From 9fe3d0233a51033c7748359bfd84a2241652ca58 Mon Sep 17 00:00:00 2001 From: Alex Lementuev Date: Thu, 6 May 2021 11:47:37 -0400 Subject: [PATCH 06/17] Fixed updating person first/last name --- .../java/com/mparticle/kits/ApptentiveKit.java | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/main/java/com/mparticle/kits/ApptentiveKit.java b/src/main/java/com/mparticle/kits/ApptentiveKit.java index 17926e3..0bf1c78 100644 --- a/src/main/java/com/mparticle/kits/ApptentiveKit.java +++ b/src/main/java/com/mparticle/kits/ApptentiveKit.java @@ -39,6 +39,9 @@ public class ApptentiveKit extends KitIntegration implements private static final String APPTENTIVE_APP_KEY = "apptentiveAppKey"; private static final String Apptentive_APP_SIGNATURE = "apptentiveAppSignature"; + private String lastKnownFirstName; + private String lastKnownLastName; + @Override public String getName() { return "Apptentive"; @@ -88,22 +91,19 @@ public void setUserIdentity(MParticle.IdentityType identityType, String id) { @Override public void setUserAttribute(String attributeKey, String attributeValue) { - String firstName = ""; - String lastName = ""; - if (attributeKey.equalsIgnoreCase(MParticle.UserAttributes.FIRSTNAME)) { - firstName = attributeValue; + lastKnownFirstName = attributeValue; } else if (attributeKey.equalsIgnoreCase(MParticle.UserAttributes.LASTNAME)) { - lastName = attributeValue; + lastKnownLastName = attributeValue; } else { addCustomPersonData(attributeKey, parseValue(attributeValue)); } String fullName; - if (!KitUtils.isEmpty(firstName) && !KitUtils.isEmpty(lastName)) { - fullName = firstName + " " + lastName; + if (!KitUtils.isEmpty(lastKnownFirstName) && !KitUtils.isEmpty(lastKnownLastName)) { + fullName = lastKnownFirstName + " " + lastKnownLastName; } else { - fullName = firstName + lastName; + fullName = lastKnownFirstName + lastKnownLastName; } Apptentive.setPersonName(fullName.trim()); } From 5d07655eb029c844475dba27cb2eb007978e9db0 Mon Sep 17 00:00:00 2001 From: Alex Lementuev Date: Thu, 6 May 2021 20:26:51 -0400 Subject: [PATCH 07/17] Fixed setting user name --- src/main/java/com/mparticle/kits/ApptentiveKit.java | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/mparticle/kits/ApptentiveKit.java b/src/main/java/com/mparticle/kits/ApptentiveKit.java index 0bf1c78..e1d4ecb 100644 --- a/src/main/java/com/mparticle/kits/ApptentiveKit.java +++ b/src/main/java/com/mparticle/kits/ApptentiveKit.java @@ -97,13 +97,16 @@ public void setUserAttribute(String attributeKey, String attributeValue) { lastKnownLastName = attributeValue; } else { addCustomPersonData(attributeKey, parseValue(attributeValue)); + return; } - String fullName; - if (!KitUtils.isEmpty(lastKnownFirstName) && !KitUtils.isEmpty(lastKnownLastName)) { - fullName = lastKnownFirstName + " " + lastKnownLastName; - } else { - fullName = lastKnownFirstName + lastKnownLastName; + String fullName = ""; + if (!KitUtils.isEmpty(lastKnownFirstName)) { + fullName += lastKnownFirstName; + } + if (!KitUtils.isEmpty(lastKnownLastName)) { + if (fullName.length() > 0) { fullName += " "; } + fullName += lastKnownLastName; } Apptentive.setPersonName(fullName.trim()); } From de7c228aca539eb0f9dfa950f1bb37c09c1beef5 Mon Sep 17 00:00:00 2001 From: Alex Lementuev Date: Sat, 8 May 2021 15:49:28 -0400 Subject: [PATCH 08/17] Event engagement refactoring --- .../com/mparticle/kits/ApptentiveKit.java | 22 ++++++++++--------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/src/main/java/com/mparticle/kits/ApptentiveKit.java b/src/main/java/com/mparticle/kits/ApptentiveKit.java index e1d4ecb..aaa3e27 100644 --- a/src/main/java/com/mparticle/kits/ApptentiveKit.java +++ b/src/main/java/com/mparticle/kits/ApptentiveKit.java @@ -9,6 +9,7 @@ import com.apptentive.android.sdk.ApptentiveNotifications; import com.apptentive.android.sdk.conversation.Conversation; import com.apptentive.android.sdk.model.CommerceExtendedData; +import com.apptentive.android.sdk.model.ExtendedData; import com.apptentive.android.sdk.notifications.ApptentiveNotification; import com.apptentive.android.sdk.notifications.ApptentiveNotificationCenter; import com.apptentive.android.sdk.notifications.ApptentiveNotificationObserver; @@ -22,7 +23,6 @@ import org.json.JSONException; import java.math.BigDecimal; -import java.util.Collections; import java.util.HashMap; import java.util.LinkedList; import java.util.List; @@ -176,12 +176,7 @@ public List logException(Exception exception, Map logEvent(MPEvent event) { - Map customData = event.getInfo(); - if (customData != null) { - Apptentive.engage(getContext(), event.getEventName(), parseCustomData(customData)); - } else { - Apptentive.engage(getContext(), event.getEventName()); - } + engage(getContext(), event.getEventName(), event.getInfo()); List messageList = new LinkedList(); messageList.add(ReportingMessage.fromEvent(this, event)); return messageList; @@ -254,10 +249,9 @@ public List logEvent(CommerceEvent event) { if (apptentiveCommerceData != null) { - Map customData = event.getCustomAttributes(); - Apptentive.engage(getContext(), + engage(getContext(), String.format("eCommerce - %s", event.getProductAction()), - customData == null ? null : parseCustomData(customData), + event.getCustomAttributes(), apptentiveCommerceData); List messages = new LinkedList(); messages.add(ReportingMessage.fromEvent(this, event)); @@ -296,6 +290,14 @@ public void onReceiveNotification(ApptentiveNotification notification) { //region Helpers + private void engage(Context context, String event, Map customData) { + engage(context, event, customData, (ExtendedData[]) null); + } + + private void engage(Context context, String event, Map customData, ExtendedData... extendedData) { + Apptentive.engage(context, event, parseCustomData(customData), extendedData); + } + /* Apptentive SDK does not provide a function which accepts Object as custom data so we need to cast */ private void addCustomPersonData(String key, Object value) { if (value instanceof String) { From 363d06722546c9144a0146d22071921fdc03fe4c Mon Sep 17 00:00:00 2001 From: Alex Lementuev Date: Sat, 8 May 2021 15:51:56 -0400 Subject: [PATCH 09/17] Log screen events --- src/main/java/com/mparticle/kits/ApptentiveKit.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/com/mparticle/kits/ApptentiveKit.java b/src/main/java/com/mparticle/kits/ApptentiveKit.java index aaa3e27..a7a6107 100644 --- a/src/main/java/com/mparticle/kits/ApptentiveKit.java +++ b/src/main/java/com/mparticle/kits/ApptentiveKit.java @@ -184,6 +184,7 @@ public List logEvent(MPEvent event) { @Override public List logScreen(String screenName, Map eventAttributes) { + engage(getContext(), screenName, eventAttributes); return null; } From 4d44cf4a6623a696752c5d2ea1d24269ab984e3c Mon Sep 17 00:00:00 2001 From: Alex Lementuev Date: Sat, 8 May 2021 16:18:41 -0400 Subject: [PATCH 10/17] Whitespaces fix --- src/main/java/com/mparticle/kits/ApptentiveKit.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/mparticle/kits/ApptentiveKit.java b/src/main/java/com/mparticle/kits/ApptentiveKit.java index a7a6107..1a880ae 100644 --- a/src/main/java/com/mparticle/kits/ApptentiveKit.java +++ b/src/main/java/com/mparticle/kits/ApptentiveKit.java @@ -253,7 +253,8 @@ public List logEvent(CommerceEvent event) { engage(getContext(), String.format("eCommerce - %s", event.getProductAction()), event.getCustomAttributes(), - apptentiveCommerceData); + apptentiveCommerceData + ); List messages = new LinkedList(); messages.add(ReportingMessage.fromEvent(this, event)); return messages; From 43fa196c3d83f961e43ff8ac39eebdf5dd30c54e Mon Sep 17 00:00:00 2001 From: Alex Lementuev Date: Sat, 22 May 2021 19:26:13 -0400 Subject: [PATCH 11/17] Refactoring --- src/main/java/com/mparticle/kits/ApptentiveKit.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/mparticle/kits/ApptentiveKit.java b/src/main/java/com/mparticle/kits/ApptentiveKit.java index 1a880ae..03d9a1f 100644 --- a/src/main/java/com/mparticle/kits/ApptentiveKit.java +++ b/src/main/java/com/mparticle/kits/ApptentiveKit.java @@ -37,7 +37,7 @@ public class ApptentiveKit extends KitIntegration implements KitIntegration.AttributeListener, ApptentiveNotificationObserver { private static final String APPTENTIVE_APP_KEY = "apptentiveAppKey"; - private static final String Apptentive_APP_SIGNATURE = "apptentiveAppSignature"; + private static final String APPTENTIVE_APP_SIGNATURE = "apptentiveAppSignature"; private String lastKnownFirstName; private String lastKnownLastName; @@ -50,7 +50,7 @@ public String getName() { @Override protected List onKitCreate(Map settings, Context context) { String apptentiveAppKey = settings.get(APPTENTIVE_APP_KEY); - String apptentiveAppSignature = settings.get(Apptentive_APP_SIGNATURE); + String apptentiveAppSignature = settings.get(APPTENTIVE_APP_SIGNATURE); if (KitUtils.isEmpty(apptentiveAppKey)) { throw new IllegalArgumentException("Apptentive App Key is required. If you are migrating from a previous version, you may need to enter the new Apptentive App Key and Signature on the mParticle website."); } From 627c3c7880b39c7e6ebea06e7fc1005f8279ac41 Mon Sep 17 00:00:00 2001 From: Alex Lementuev Date: Sat, 22 May 2021 20:43:56 -0400 Subject: [PATCH 12/17] Added type detection --- .../com/mparticle/kits/ApptentiveKit.java | 84 ++++++++++++++++--- .../java/com/mparticle/kits/StringUtils.java | 8 ++ .../mparticle/kits/CustomDataParserTest.java | 64 ++++---------- 3 files changed, 96 insertions(+), 60 deletions(-) diff --git a/src/main/java/com/mparticle/kits/ApptentiveKit.java b/src/main/java/com/mparticle/kits/ApptentiveKit.java index 03d9a1f..9ba0117 100644 --- a/src/main/java/com/mparticle/kits/ApptentiveKit.java +++ b/src/main/java/com/mparticle/kits/ApptentiveKit.java @@ -28,7 +28,7 @@ import java.util.List; import java.util.Map; -import static com.mparticle.kits.CustomDataParser.parseCustomData; +import static com.apptentive.android.sdk.ApptentiveLogTag.UTIL; import static com.mparticle.kits.CustomDataParser.parseValue; public class ApptentiveKit extends KitIntegration implements @@ -36,9 +36,15 @@ public class ApptentiveKit extends KitIntegration implements KitIntegration.CommerceListener, KitIntegration.AttributeListener, ApptentiveNotificationObserver { + private static final String APPTENTIVE_APP_KEY = "apptentiveAppKey"; private static final String APPTENTIVE_APP_SIGNATURE = "apptentiveAppSignature"; + private static final String ENABLE_TYPE_DETECTION = "enableTypeDetection"; + + private static final String SUFFIX_KEY_FLAG = "_flag"; + private static final String SUFFIX_KEY_NUMBER = "_number"; + private boolean enableTypeDetection; private String lastKnownFirstName; private String lastKnownLastName; @@ -58,7 +64,9 @@ protected List onKitCreate(Map settings, Conte throw new IllegalArgumentException("Apptentive App Signature is required. If you are migrating from a previous version, you may need to enter the new Apptentive App Key and Signature on the mParticle website."); } - ApptentiveConfiguration configuration = new ApptentiveConfiguration(apptentiveAppKey, apptentiveAppSignature); + enableTypeDetection = tryParseSettingFlag(settings, ENABLE_TYPE_DETECTION, false); + + ApptentiveConfiguration configuration = new ApptentiveConfiguration(apptentiveAppKey, apptentiveAppSignature); Apptentive.register((Application)context.getApplicationContext(), configuration); ApptentiveNotificationCenter.defaultCenter() .addObserver(ApptentiveNotifications.NOTIFICATION_CONVERSATION_STATE_DID_CHANGE, this); @@ -96,7 +104,7 @@ public void setUserAttribute(String attributeKey, String attributeValue) { } else if (attributeKey.equalsIgnoreCase(MParticle.UserAttributes.LASTNAME)) { lastKnownLastName = attributeValue; } else { - addCustomPersonData(attributeKey, parseValue(attributeValue)); + addCustomPersonData(attributeKey, attributeValue); return; } @@ -131,7 +139,7 @@ public void setAllUserAttributes(Map attributes, Map customDat } /* Apptentive SDK does not provide a function which accepts Object as custom data so we need to cast */ - private void addCustomPersonData(String key, Object value) { - if (value instanceof String) { - Apptentive.addCustomPersonData(key, (String) value); - } else if (value instanceof Boolean) { - Apptentive.addCustomPersonData(key, (Boolean) value); - } else if (value instanceof Number) { - Apptentive.addCustomPersonData(key, (Number) value); - } else { - ApptentiveLog.e("Unexpected custom person data type: %s", value != null ? value.getClass() : null); + private void addCustomPersonData(String key, String value) { + // original key + Apptentive.addCustomPersonData(key, value); + + // typed key + if (enableTypeDetection) { + final Object typedValue = parseValue(value); + if (typedValue instanceof String) { + // the value is already set + } else if (typedValue instanceof Boolean) { + Apptentive.addCustomPersonData(key + SUFFIX_KEY_FLAG, (Boolean) typedValue); + } else if (typedValue instanceof Number) { + Apptentive.addCustomPersonData(key + SUFFIX_KEY_NUMBER, (Number) typedValue); + } else { + ApptentiveLog.e("Unexpected custom person data type: %s", typedValue != null ? typedValue.getClass() : null); + } } } + private Map parseCustomData(Map map) { + if (map != null) { + final Map res = new HashMap<>(); + for (Map.Entry e : map.entrySet()) { + final String key = e.getKey(); + final String value = e.getValue(); + + // original key + res.put(key, value); + + // typed key + if (enableTypeDetection) { + final Object typedValue = parseValue(value); + if (typedValue instanceof String) { + // the value is already set + } else if (typedValue instanceof Boolean) { + res.put(key + SUFFIX_KEY_FLAG, (Boolean) typedValue); + } else if (typedValue instanceof Number) { + res.put(key + SUFFIX_KEY_NUMBER, (Number) typedValue); + } else { + ApptentiveLog.e("Unexpected custom data type: %s", typedValue != null ? typedValue.getClass() : null); + } + } + } + return res; + } + + return null; + } + + private static boolean tryParseSettingFlag(Map settings, String key, boolean defaultValue) { + final String value = settings.get(key); + if (value != null) { + final Boolean flag = StringUtils.tryParseBoolean(value); + if (flag != null) { + return flag; + } + ApptentiveLog.w(UTIL, "Unable to parse boolean flag '%s': %s", key, value); + } + + return defaultValue; + } + //endregion } diff --git a/src/main/java/com/mparticle/kits/StringUtils.java b/src/main/java/com/mparticle/kits/StringUtils.java index 500ca5d..612cbe4 100644 --- a/src/main/java/com/mparticle/kits/StringUtils.java +++ b/src/main/java/com/mparticle/kits/StringUtils.java @@ -28,4 +28,12 @@ public static Double tryParseDouble(String value) { return null; } } + + public static Boolean tryParseBoolean(String value) { + try { + return Boolean.valueOf(value); + } catch (NumberFormatException e) { + return null; + } + } } diff --git a/src/test/java/com/mparticle/kits/CustomDataParserTest.java b/src/test/java/com/mparticle/kits/CustomDataParserTest.java index 236f6fd..82e2538 100644 --- a/src/test/java/com/mparticle/kits/CustomDataParserTest.java +++ b/src/test/java/com/mparticle/kits/CustomDataParserTest.java @@ -2,65 +2,35 @@ import org.junit.Test; -import java.util.HashMap; -import java.util.Map; - -import static org.junit.Assert.*; +import static com.mparticle.kits.CustomDataParser.parseValue; +import static java.lang.Boolean.FALSE; +import static java.lang.Boolean.TRUE; +import static org.junit.Assert.assertEquals; public class CustomDataParserTest { @Test public void testParseData() { - Map data = new HashMap<>(); - - // boolean - data.put("key-1", "true"); - data.put("key-2", "True"); - data.put("key-3", "false"); - data.put("key-4", "False"); - - // integer - data.put("key-5", "12345"); - data.put("key-6", "-12345"); - data.put("key-7", Integer.toString(Integer.MIN_VALUE)); - data.put("key-8", Integer.toString(Integer.MAX_VALUE)); - - // long - data.put("key-9", Long.toString(Long.MIN_VALUE)); - data.put("key-10", Long.toString(Long.MAX_VALUE)); - - // double - data.put("key-11", "3.14"); - data.put("key-12", "-3.14"); - - // string - data.put("key-13", "test"); - - Map expected = new HashMap<>(); - // boolean - expected.put("key-1", Boolean.TRUE); - expected.put("key-2", Boolean.TRUE); - expected.put("key-3", Boolean.FALSE); - expected.put("key-4", Boolean.FALSE); + assertEquals(TRUE, parseValue("true")); + assertEquals(TRUE, parseValue("True")); + assertEquals(FALSE, parseValue("false")); + assertEquals(FALSE, parseValue("False")); // integer - expected.put("key-5", 12345); - expected.put("key-6", -12345); - expected.put("key-7", Integer.MIN_VALUE); - expected.put("key-8", Integer.MAX_VALUE); + assertEquals(12345, parseValue("12345")); + assertEquals(-12345, parseValue("-12345")); + assertEquals(Integer.MIN_VALUE, parseValue(Integer.toString(Integer.MIN_VALUE))); + assertEquals(Integer.MAX_VALUE, parseValue(Integer.toString(Integer.MAX_VALUE))); // long - expected.put("key-9", Long.MIN_VALUE); - expected.put("key-10", Long.MAX_VALUE); + assertEquals(Long.MIN_VALUE, parseValue(Long.toString(Long.MIN_VALUE))); + assertEquals(Long.MAX_VALUE, parseValue(Long.toString(Long.MAX_VALUE))); // double - expected.put("key-11", 3.14); - expected.put("key-12", -3.14); + assertEquals(3.14, parseValue("3.14")); + assertEquals(-3.14, parseValue("-3.14")); // string - expected.put("key-13", "test"); - - Map actual = CustomDataParser.parseCustomData(data); - assertEquals(expected, actual); + assertEquals("test", parseValue("test")); } } \ No newline at end of file From 0902f4c2fb0beb9c1ab43c10ad91682365f0f491 Mon Sep 17 00:00:00 2001 From: Alex Lementuev Date: Sun, 23 May 2021 21:05:34 -0400 Subject: [PATCH 13/17] Refactoring + more tests --- .../com/mparticle/kits/ApptentiveKit.java | 15 +---------- .../java/com/mparticle/kits/StringUtils.java | 19 ++++++++++++++ .../com/mparticle/kits/StringUtilsTest.java | 25 +++++++++++++++++++ 3 files changed, 45 insertions(+), 14 deletions(-) diff --git a/src/main/java/com/mparticle/kits/ApptentiveKit.java b/src/main/java/com/mparticle/kits/ApptentiveKit.java index 9ba0117..2e86c7b 100644 --- a/src/main/java/com/mparticle/kits/ApptentiveKit.java +++ b/src/main/java/com/mparticle/kits/ApptentiveKit.java @@ -28,8 +28,8 @@ import java.util.List; import java.util.Map; -import static com.apptentive.android.sdk.ApptentiveLogTag.UTIL; import static com.mparticle.kits.CustomDataParser.parseValue; +import static com.mparticle.kits.StringUtils.tryParseSettingFlag; public class ApptentiveKit extends KitIntegration implements KitIntegration.EventListener, @@ -358,18 +358,5 @@ private Map parseCustomData(Map map) { return null; } - private static boolean tryParseSettingFlag(Map settings, String key, boolean defaultValue) { - final String value = settings.get(key); - if (value != null) { - final Boolean flag = StringUtils.tryParseBoolean(value); - if (flag != null) { - return flag; - } - ApptentiveLog.w(UTIL, "Unable to parse boolean flag '%s': %s", key, value); - } - - return defaultValue; - } - //endregion } diff --git a/src/main/java/com/mparticle/kits/StringUtils.java b/src/main/java/com/mparticle/kits/StringUtils.java index 612cbe4..d54ba54 100644 --- a/src/main/java/com/mparticle/kits/StringUtils.java +++ b/src/main/java/com/mparticle/kits/StringUtils.java @@ -1,6 +1,25 @@ package com.mparticle.kits; +import com.apptentive.android.sdk.ApptentiveLog; + +import java.util.Map; + +import static com.apptentive.android.sdk.ApptentiveLogTag.UTIL; + final class StringUtils { + public static boolean tryParseSettingFlag(Map settings, String key, boolean defaultValue) { + final String value = settings.get(key); + if (value != null) { + final Boolean flag = StringUtils.tryParseBoolean(value); + if (flag != null) { + return flag; + } + ApptentiveLog.w(UTIL, "Unable to parse boolean flag '%s': %s", key, value); + } + + return defaultValue; + } + public static Number tryParseNumber(String value) { Long longValue = tryParseLong(value); if (longValue != null) { diff --git a/src/test/java/com/mparticle/kits/StringUtilsTest.java b/src/test/java/com/mparticle/kits/StringUtilsTest.java index 004bf66..c889ea6 100644 --- a/src/test/java/com/mparticle/kits/StringUtilsTest.java +++ b/src/test/java/com/mparticle/kits/StringUtilsTest.java @@ -2,8 +2,13 @@ import org.junit.Test; +import java.util.HashMap; +import java.util.Map; + import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; public class StringUtilsTest { @Test @@ -14,4 +19,24 @@ public void testParseNumber() { assertEquals(Double.valueOf(12345.0), (Double) StringUtils.tryParseNumber("12345.0")); assertNull(StringUtils.tryParseNumber("test")); } + + @Test + public void testSettingsFlag() { + Map data = new HashMap<>(); + data.put("key", "true"); + assertTrue(StringUtils.tryParseSettingFlag(data, "key", false)); + } + + @Test + public void testMissingSettingsFlag() { + Map data = new HashMap<>(); + assertFalse(StringUtils.tryParseSettingFlag(data, "key", false)); + } + + @Test + public void testMissingCorruptedSettingsFlag() { + Map data = new HashMap<>(); + data.put("key", "123"); + assertFalse(StringUtils.tryParseSettingFlag(data, "key", false)); + } } \ No newline at end of file From 48d0a84c83ed3a428eb71d142c4c634cbcb9732d Mon Sep 17 00:00:00 2001 From: Melody Jones <34523571+melj32app@users.noreply.github.com> Date: Thu, 3 Jun 2021 12:12:59 -0700 Subject: [PATCH 14/17] Update to default type detection to true It was determined that we want this new field to default to being turned on for all customers. --- src/main/java/com/mparticle/kits/ApptentiveKit.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/mparticle/kits/ApptentiveKit.java b/src/main/java/com/mparticle/kits/ApptentiveKit.java index 2e86c7b..e0f0024 100644 --- a/src/main/java/com/mparticle/kits/ApptentiveKit.java +++ b/src/main/java/com/mparticle/kits/ApptentiveKit.java @@ -64,7 +64,7 @@ protected List onKitCreate(Map settings, Conte throw new IllegalArgumentException("Apptentive App Signature is required. If you are migrating from a previous version, you may need to enter the new Apptentive App Key and Signature on the mParticle website."); } - enableTypeDetection = tryParseSettingFlag(settings, ENABLE_TYPE_DETECTION, false); + enableTypeDetection = tryParseSettingFlag(settings, ENABLE_TYPE_DETECTION, true); ApptentiveConfiguration configuration = new ApptentiveConfiguration(apptentiveAppKey, apptentiveAppSignature); Apptentive.register((Application)context.getApplicationContext(), configuration); From bd0b094f1e3e7f3fe8e125929e54215f93d3b106 Mon Sep 17 00:00:00 2001 From: Melody Jones <34523571+melj32app@users.noreply.github.com> Date: Fri, 11 Jun 2021 15:59:24 -0700 Subject: [PATCH 15/17] Update to show reporting messages for screen views --- src/main/java/com/mparticle/kits/ApptentiveKit.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/mparticle/kits/ApptentiveKit.java b/src/main/java/com/mparticle/kits/ApptentiveKit.java index e0f0024..869f3c5 100644 --- a/src/main/java/com/mparticle/kits/ApptentiveKit.java +++ b/src/main/java/com/mparticle/kits/ApptentiveKit.java @@ -193,7 +193,9 @@ public List logEvent(MPEvent event) { @Override public List logScreen(String screenName, Map eventAttributes) { engage(getContext(), screenName, eventAttributes); - return null; + List messages = new LinkedList(); + messages.add(new ReportingMessage(this, ReportingMessage.MessageType.SCREEN_VIEW, System.currentTimeMillis(), eventAttributes)); + return messages; } @Override From 0611086dd7d96311cc61be1cdad8038c6ec9562d Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Wed, 28 Jul 2021 08:02:13 +0000 Subject: [PATCH 16/17] Bump apptentive-android from 5.6.2 to 5.6.4 Bumps [apptentive-android](https://github.com/apptentive/apptentive-android) from 5.6.2 to 5.6.4. - [Release notes](https://github.com/apptentive/apptentive-android/releases) - [Changelog](https://github.com/apptentive/apptentive-android/blob/master/CHANGELOG.md) - [Commits](https://github.com/apptentive/apptentive-android/compare/v5.6.2...v5.6.4) --- updated-dependencies: - dependency-name: com.apptentive:apptentive-android dependency-type: direct:production update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] --- build.gradle | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build.gradle b/build.gradle index b59f324..8598ac0 100644 --- a/build.gradle +++ b/build.gradle @@ -24,5 +24,5 @@ android { } dependencies { - implementation 'com.apptentive:apptentive-android:5.6.2' + implementation 'com.apptentive:apptentive-android:5.6.4' } From 12bfb1d14d3e5e291e993f5cdaf9ba940d21409d Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Tue, 27 Sep 2022 08:03:06 +0000 Subject: [PATCH 17/17] Bump apptentive-android from 5.6.4 to 5.8.4 Bumps [apptentive-android](https://github.com/apptentive/apptentive-android) from 5.6.4 to 5.8.4. - [Release notes](https://github.com/apptentive/apptentive-android/releases) - [Changelog](https://github.com/apptentive/apptentive-android/blob/master/CHANGELOG.md) - [Commits](https://github.com/apptentive/apptentive-android/compare/v5.6.4...v5.8.4) --- updated-dependencies: - dependency-name: com.apptentive:apptentive-android dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] --- build.gradle | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build.gradle b/build.gradle index 8598ac0..4c28e3b 100644 --- a/build.gradle +++ b/build.gradle @@ -24,5 +24,5 @@ android { } dependencies { - implementation 'com.apptentive:apptentive-android:5.6.4' + implementation 'com.apptentive:apptentive-android:5.8.4' }