Skip to content

Commit 1cf2dd7

Browse files
authored
Merge pull request #474 from Iterable/jay/MOB-4666-in-apps-config-memory
[MOB-4666] in apps config memory
2 parents ff412dc + 122badb commit 1cf2dd7

File tree

3 files changed

+48
-4
lines changed

3 files changed

+48
-4
lines changed

iterableapi/src/main/java/com/iterable/iterableapi/IterableApi.java

+5-2
Original file line numberDiff line numberDiff line change
@@ -494,8 +494,11 @@ public static void initialize(@NonNull Context context, @NonNull String apiKey,
494494
IterableActivityMonitor.getInstance().addCallback(sharedInstance.activityMonitorListener);
495495

496496
if (sharedInstance.inAppManager == null) {
497-
sharedInstance.inAppManager = new IterableInAppManager(sharedInstance, sharedInstance.config.inAppHandler,
498-
sharedInstance.config.inAppDisplayInterval);
497+
sharedInstance.inAppManager = new IterableInAppManager(
498+
sharedInstance,
499+
sharedInstance.config.inAppHandler,
500+
sharedInstance.config.inAppDisplayInterval,
501+
sharedInstance.config.useInMemoryStorageForInApps);
499502
}
500503

501504
loadLastSavedConfiguration(context);

iterableapi/src/main/java/com/iterable/iterableapi/IterableConfig.java

+20
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,12 @@ public class IterableConfig {
7171
*/
7272
final String[] allowedProtocols;
7373

74+
/**
75+
* This controls whether the in-app content should be saved to disk, or only kept in memory.
76+
* By default, the SDK will save in-apps to disk.
77+
*/
78+
final boolean useInMemoryStorageForInApps;
79+
7480
private IterableConfig(Builder builder) {
7581
pushIntegrationName = builder.pushIntegrationName;
7682
urlHandler = builder.urlHandler;
@@ -83,6 +89,7 @@ private IterableConfig(Builder builder) {
8389
authHandler = builder.authHandler;
8490
expiringAuthTokenRefreshPeriod = builder.expiringAuthTokenRefreshPeriod;
8591
allowedProtocols = builder.allowedProtocols;
92+
useInMemoryStorageForInApps = builder.useInMemoryStorageForInApps;
8693
}
8794

8895
public static class Builder {
@@ -97,6 +104,8 @@ public static class Builder {
97104
private IterableAuthHandler authHandler;
98105
private long expiringAuthTokenRefreshPeriod = 60000L;
99106
private String[] allowedProtocols = new String[0];
107+
private boolean useInMemoryStorageForInApps = false;
108+
100109
public Builder() {}
101110

102111
/**
@@ -217,6 +226,17 @@ public Builder setAllowedProtocols(@NonNull String[] allowedProtocols) {
217226
return this;
218227
}
219228

229+
/**
230+
* Set whether the SDK should store in-apps only in memory, or in file storage
231+
* @param useInMemoryStorageForInApps `true` will have in-apps be only in memory
232+
*/
233+
234+
@NonNull
235+
public Builder setUseInMemoryStorageForInApps(boolean useInMemoryStorageForInApps) {
236+
this.useInMemoryStorageForInApps = useInMemoryStorageForInApps;
237+
return this;
238+
}
239+
220240
@NonNull
221241
public IterableConfig build() {
222242
return new IterableConfig(this);

iterableapi/src/main/java/com/iterable/iterableapi/IterableInAppManager.java

+23-2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import org.json.JSONException;
1818
import org.json.JSONObject;
1919

20+
import java.io.File;
2021
import java.util.ArrayList;
2122
import java.util.Collections;
2223
import java.util.Comparator;
@@ -50,11 +51,11 @@ public interface Listener {
5051
private long lastInAppShown = 0;
5152
private boolean autoDisplayPaused = false;
5253

53-
IterableInAppManager(IterableApi iterableApi, IterableInAppHandler handler, double inAppDisplayInterval) {
54+
IterableInAppManager(IterableApi iterableApi, IterableInAppHandler handler, double inAppDisplayInterval, boolean useInMemoryStorageForInApps) {
5455
this(iterableApi,
5556
handler,
5657
inAppDisplayInterval,
57-
new IterableInAppFileStorage(iterableApi.getMainActivityContext()),
58+
IterableInAppManager.getInAppStorageModel(iterableApi, useInMemoryStorageForInApps),
5859
IterableActivityMonitor.getInstance(),
5960
new IterableInAppDisplayer(IterableActivityMonitor.getInstance()));
6061
}
@@ -435,6 +436,26 @@ private void handleIterableCustomAction(String actionName, IterableInAppMessage
435436
}
436437
}
437438

439+
private static IterableInAppStorage getInAppStorageModel(IterableApi iterableApi, boolean useInMemoryForInAppStorage) {
440+
if (useInMemoryForInAppStorage) {
441+
checkAndDeleteUnusedInAppFileStorage(iterableApi.getMainActivityContext());
442+
443+
return new IterableInAppMemoryStorage();
444+
} else {
445+
return new IterableInAppFileStorage(iterableApi.getMainActivityContext());
446+
}
447+
}
448+
449+
private static void checkAndDeleteUnusedInAppFileStorage(Context context) {
450+
File sdkFilesDirectory = IterableUtil.getSDKFilesDirectory(context);
451+
File inAppContentFolder = IterableUtil.getDirectory(sdkFilesDirectory, "IterableInAppFileStorage");
452+
File inAppBlob = new File(inAppContentFolder, "itbl_inapp.json");
453+
454+
if (inAppBlob.exists()) {
455+
inAppBlob.delete();
456+
}
457+
}
458+
438459
@Override
439460
public void onSwitchToForeground() {
440461
if (IterableUtil.currentTimeMillis() - lastSyncTime > MOVE_TO_FOREGROUND_SYNC_INTERVAL_MS) {

0 commit comments

Comments
 (0)