Skip to content

Commit

Permalink
Merge pull request #228 from apptentive/branch_5.7.0
Browse files Browse the repository at this point in the history
Release 5.7.0
  • Loading branch information
ChaseApptentive authored Aug 4, 2021
2 parents 6e499b5 + d5ed341 commit d4671f2
Show file tree
Hide file tree
Showing 12 changed files with 174 additions and 168 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# 2021-08-04 - v5.7.0

#### Improvements

* Add the ability to randomly target a percentage of customers.

# 2021-07-27 - v5.6.4

#### Fixes
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use your app, to talk to them at the right time, and in the right way.

##### [Release Notes](https://learn.apptentive.com/knowledge-base/android-sdk-release-notes/)

##### Binary releases are hosted for Maven [here](http://search.maven.org/#artifactdetails|com.apptentive|apptentive-android|5.6.4|aar)
##### Binary releases are hosted for Maven [here](http://search.maven.org/#artifactdetails|com.apptentive|apptentive-android|5.7.0|aar)

#### Reporting Bugs

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,18 @@
import static org.junit.Assert.*;

public class DefaultRandomPercentProviderTest {
// private final Context context = InstrumentationRegistry.getInstrumentation().getContext();
//
// @Before
// public void before() {
// DefaultRandomPercentProvider.clear(context);
// }
//
// @Test
// public void testRandomPercent() {
// final RandomPercentProvider provider = new DefaultRandomPercentProvider(context, "id");
// final double percent1 = provider.getPercent("key");
// final double percent2 = provider.getPercent("key");
// assertEquals(percent1, percent2, 0.0000001f);
// }
private final Context context = InstrumentationRegistry.getInstrumentation().getContext();

@Before
public void before() {
DefaultRandomPercentProvider.clear(context);
}

@Test
public void testRandomPercent() {
final RandomPercentProvider provider = new DefaultRandomPercentProvider(context, "id");
final double percent1 = provider.getPercent("key");
final double percent2 = provider.getPercent("key");
assertEquals(percent1, percent2, 0.0000001f);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,54 +17,54 @@
import static org.junit.Assert.assertEquals;

public class FieldManagerTest {
// private final Context context = InstrumentationRegistry.getInstrumentation().getContext();
//
// @Test
// public void testRandomPercentWithKey() {
// final FieldManager fieldManager = createFieldManager(50);
// final Comparable expected = new BigDecimal(50);
// final Comparable actual = fieldManager.getValue("random/abc123xyz/percent");
// assertEquals(expected, actual);
// }
//
// @Test
// public void testRandomPercentWithoutKey() {
// final FieldManager fieldManager = createFieldManager(50);
// final Comparable expected = new BigDecimal(50);
// final Comparable actual = fieldManager.getValue("random/percent");
// assertEquals(expected, actual);
// }
//
// @Test
// public void testRandomPercentWithKeyDescription() {
// final FieldManager fieldManager = createFieldManager(50);
// final String expected = "random percent for key 'abc123xyz'";
// final String actual = fieldManager.getDescription("random/abc123xyz/percent");
// assertEquals(expected, actual);
// }
//
// @Test
// public void testRandomPercentWithoutKeyDescription() {
// final FieldManager fieldManager = createFieldManager(50);
// final String expected = "random percent";
// final String actual = fieldManager.getDescription("random/percent");
// assertEquals(expected, actual);
// }
//
// private FieldManager createFieldManager(double percent) {
// return new FieldManager(context, new VersionHistory(), new EventData(), new Person(), new Device(), new AppRelease(), new MockRandomPercentProvider(percent));
// }
//
// private static class MockRandomPercentProvider implements RandomPercentProvider {
// private final double percent;
//
// private MockRandomPercentProvider(double percent) {
// this.percent = percent;
// }
//
// @Override
// public double getPercent(String key) {
// return percent;
// }
// }
private final Context context = InstrumentationRegistry.getInstrumentation().getContext();

@Test
public void testRandomPercentWithKey() {
final FieldManager fieldManager = createFieldManager(50);
final Comparable expected = new BigDecimal(50);
final Comparable actual = fieldManager.getValue("random/abc123xyz/percent");
assertEquals(expected, actual);
}

@Test
public void testRandomPercentWithoutKey() {
final FieldManager fieldManager = createFieldManager(50);
final Comparable expected = new BigDecimal(50);
final Comparable actual = fieldManager.getValue("random/percent");
assertEquals(expected, actual);
}

@Test
public void testRandomPercentWithKeyDescription() {
final FieldManager fieldManager = createFieldManager(50);
final String expected = "random percent for key 'abc123xyz'";
final String actual = fieldManager.getDescription("random/abc123xyz/percent");
assertEquals(expected, actual);
}

@Test
public void testRandomPercentWithoutKeyDescription() {
final FieldManager fieldManager = createFieldManager(50);
final String expected = "random percent";
final String actual = fieldManager.getDescription("random/percent");
assertEquals(expected, actual);
}

private FieldManager createFieldManager(double percent) {
return new FieldManager(context, new VersionHistory(), new EventData(), new Person(), new Device(), new AppRelease(), new MockRandomPercentProvider(percent));
}

private static class MockRandomPercentProvider implements RandomPercentProvider {
private final double percent;

private MockRandomPercentProvider(double percent) {
this.percent = percent;
}

@Override
public double getPercent(String key) {
return percent;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -202,9 +202,9 @@ public String getConversationToken() {
return conversation.getConversationToken();
}

// public String getLocalIdentifier() {
// return conversation.getLocalIdentifier();
// }
public String getLocalIdentifier() {
return conversation.getLocalIdentifier();
}

//endregion
}
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,9 @@ public void onClick(View view) {
LaunchInteractionAction launchInteractionButton = (LaunchInteractionAction) buttonAction;
List<Invocation> invocations = launchInteractionButton.getInvocations();
String interactionIdToLaunch = null;
//final RandomPercentProvider percentProvider = new DefaultRandomPercentProvider(getContext(), getConversation().getLocalIdentifier());
final RandomPercentProvider percentProvider = new DefaultRandomPercentProvider(getContext(), getConversation().getLocalIdentifier());
for (Invocation invocation : invocations) {
FieldManager fieldManager = new FieldManager(getContext(), getConversation().getVersionHistory(), getConversation().getEventData(), getConversation().getPerson(), getConversation().getDevice(), getConversation().getAppRelease()/*,percentProvider*/);
FieldManager fieldManager = new FieldManager(getContext(), getConversation().getVersionHistory(), getConversation().getEventData(), getConversation().getPerson(), getConversation().getDevice(), getConversation().getAppRelease(), percentProvider);
if (invocation.isCriteriaMet(fieldManager, false)) { // TODO: should we print details here as well?
interactionIdToLaunch = invocation.getInteractionId();
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ public String getApplicableInteraction(String eventLabel, boolean verbose) {
Invocation invocation = new Invocation(invocationObject.toString());
Conversation conversation = ApptentiveInternal.getInstance().getConversation();
final Context context = ApptentiveInternal.getInstance().getApplicationContext();
//final RandomPercentProvider percentProvider = new DefaultRandomPercentProvider(context, conversation.getLocalIdentifier());
FieldManager fieldManager = new FieldManager(context, conversation.getVersionHistory(), conversation.getEventData(), conversation.getPerson(), conversation.getDevice(), conversation.getAppRelease()/*,percentProvider*/);
final RandomPercentProvider percentProvider = new DefaultRandomPercentProvider(context, conversation.getLocalIdentifier());
FieldManager fieldManager = new FieldManager(context, conversation.getVersionHistory(), conversation.getEventData(), conversation.getPerson(), conversation.getDevice(), conversation.getAppRelease(), percentProvider);

if (invocation.isCriteriaMet(fieldManager, verbose)) {
return invocation.getInteractionId();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,55 +15,55 @@
* give key once and stores it in shared preferences.
*/
public class DefaultRandomPercentProvider implements RandomPercentProvider {
// private final Context context;
// private final String id;
//
// /**
// * @param id - unique key for making a distinction between same keys used in different conversations.
// */
// public DefaultRandomPercentProvider(Context context, String id) {
// if (context == null) {
// throw new IllegalArgumentException("Context is null");
// }
// if (id == null) {
// throw new IllegalArgumentException("Id is null");
// }
// this.context = context.getApplicationContext();
// this.id = id;
// }
//
// @Override
// public double getPercent(String key) {
// final SharedPreferences prefs = getPrefs(context);
// if (key == null) {
// return getRandomPercent();
// } else {
// final String prefsKey = id + "_" + key;
// if (prefs.contains(prefsKey)) {
// return prefs.getFloat(prefsKey, 0.0f);
// }
// final float percent = getRandomPercent();
// prefs.edit().putFloat(prefsKey, percent).apply();
// return percent;
// }
// }
//
// private float getRandomPercent() {
// ApplicationInfo applicationInfo = RuntimeUtils.getApplicationInfo(context);
//
// if (applicationInfo.isDebuggable()) {
// return (float) 50;
// } else {
// return new Random().nextFloat() * 100;
// }
// }
//
// private static SharedPreferences getPrefs(Context context) {
// return context.getSharedPreferences("com.apptentive.RandomPercentProvider", Context.MODE_PRIVATE);
// }
//
// @VisibleForTesting
// public static void clear(Context context) {
// getPrefs(context).edit().clear().apply();
// }
private final Context context;
private final String id;

/**
* @param id - unique key for making a distinction between same keys used in different conversations.
*/
public DefaultRandomPercentProvider(Context context, String id) {
if (context == null) {
throw new IllegalArgumentException("Context is null");
}
if (id == null) {
throw new IllegalArgumentException("Id is null");
}
this.context = context.getApplicationContext();
this.id = id;
}

@Override
public double getPercent(String key) {
final SharedPreferences prefs = getPrefs(context);
if (key == null) {
return getRandomPercent();
} else {
final String prefsKey = id + "_" + key;
if (prefs.contains(prefsKey)) {
return prefs.getFloat(prefsKey, 0.0f);
}
final float percent = getRandomPercent();
prefs.edit().putFloat(prefsKey, percent).apply();
return percent;
}
}

private float getRandomPercent() {
ApplicationInfo applicationInfo = RuntimeUtils.getApplicationInfo(context);

if (applicationInfo.isDebuggable()) {
return (float) 50;
} else {
return new Random().nextFloat() * 100;
}
}

private static SharedPreferences getPrefs(Context context) {
return context.getSharedPreferences("com.apptentive.RandomPercentProvider", Context.MODE_PRIVATE);
}

@VisibleForTesting
public static void clear(Context context) {
getPrefs(context).edit().clear().apply();
}
}
Loading

0 comments on commit d4671f2

Please sign in to comment.