Skip to content

Commit a50d2db

Browse files
committed
Make all IAM DB access request out of MainThread
* Make OSInAppMessageController extend OSBackgroundManager
1 parent b8ceb31 commit a50d2db

File tree

1 file changed

+19
-14
lines changed

1 file changed

+19
-14
lines changed

OneSignalSDK/onesignal/src/main/java/com/onesignal/OSInAppMessageController.java

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import android.app.AlertDialog;
44
import android.content.DialogInterface;
5-
import android.os.Process;
65

76
import androidx.annotation.NonNull;
87
import androidx.annotation.Nullable;
@@ -23,11 +22,10 @@
2322
import java.util.Map;
2423
import java.util.Set;
2524

26-
class OSInAppMessageController implements OSDynamicTriggerControllerObserver, OSSystemConditionController.OSSystemConditionObserver {
25+
class OSInAppMessageController extends OSBackgroundManager implements OSDynamicTriggerControllerObserver, OSSystemConditionController.OSSystemConditionObserver {
2726

2827
private static final Object LOCK = new Object();
29-
private final static String OS_DELETE_CACHED_REDISPLAYED_IAMS_THREAD = "OS_DELETE_CACHED_REDISPLAYED_IAMS_THREAD";
30-
private static final String OS_SAVE_IN_APP_MESSAGE = "OS_SAVE_IN_APP_MESSAGE";
28+
private final static String OS_IAM_DB_ACCESS = "OS_IAM_DB_ACCESS";
3129
public static final String IN_APP_MESSAGES_JSON_KEY = "in_app_messages";
3230
private static final String LIQUID_TAG_SCRIPT = "\n\n" +
3331
"<script>\n" +
@@ -68,7 +66,7 @@ class OSInAppMessageController implements OSDynamicTriggerControllerObserver, OS
6866
// IAMs displayed with last displayed time and quantity of displays data
6967
// This is retrieved from a DB Table that take care of each object to be unique
7068
@NonNull
71-
private List<OSInAppMessage> redisplayedInAppMessages;
69+
private List<OSInAppMessage> redisplayedInAppMessages = new ArrayList<>();
7270

7371
private OSInAppMessagePrompt currentPrompt = null;
7472
private boolean inAppMessagingEnabled = true;
@@ -141,9 +139,15 @@ OSInAppMessageRepository getInAppMessageRepository(OneSignalDbHelper dbHelper) {
141139

142140
protected void initRedisplayData(OneSignalDbHelper dbHelper) {
143141
inAppMessageRepository = getInAppMessageRepository(dbHelper);
144-
redisplayedInAppMessages = inAppMessageRepository.getCachedInAppMessages();
142+
Runnable getCachedIAMRunnable = new BackgroundRunnable() {
143+
@Override
144+
public void run() {
145+
redisplayedInAppMessages = inAppMessageRepository.getCachedInAppMessages();
146+
OneSignal.Log(OneSignal.LOG_LEVEL.DEBUG, "redisplayedInAppMessages: " + redisplayedInAppMessages.toString());
147+
}
148+
};
145149

146-
OneSignal.Log(OneSignal.LOG_LEVEL.DEBUG, "redisplayedInAppMessages: " + redisplayedInAppMessages.toString());
150+
runRunnableOnThread(getCachedIAMRunnable, OS_IAM_DB_ACCESS);
147151
}
148152

149153
void resetSessionLaunchTime() {
@@ -725,13 +729,13 @@ private void persistInAppMessage(final OSInAppMessage message) {
725729
message.setTriggerChanged(false);
726730
message.setDisplayedInSession(true);
727731

728-
new Thread(new Runnable() {
732+
Runnable saveIAMOnDBRunnable = new BackgroundRunnable() {
729733
@Override
730734
public void run() {
731-
Thread.currentThread().setPriority(Process.THREAD_PRIORITY_BACKGROUND);
732735
inAppMessageRepository.saveInAppMessage(message);
733736
}
734-
}, OS_SAVE_IN_APP_MESSAGE).start();
737+
};
738+
runRunnableOnThread(saveIAMOnDBRunnable, OS_IAM_DB_ACCESS);
735739

736740
// Update the data to enable future re displays
737741
// Avoid calling the repository data again
@@ -885,14 +889,14 @@ void onSuccess(String response) {
885889
* 3. Use queried data to clean SharedPreferences
886890
*/
887891
void cleanCachedInAppMessages() {
888-
new Thread(new Runnable() {
892+
Runnable cleanCachedIAMRunnable = new BackgroundRunnable() {
889893
@Override
890894
public void run() {
891-
Thread.currentThread().setPriority(Process.THREAD_PRIORITY_BACKGROUND);
892-
893895
inAppMessageRepository.cleanCachedInAppMessages();
894896
}
895-
}, OS_DELETE_CACHED_REDISPLAYED_IAMS_THREAD).start();
897+
};
898+
899+
runRunnableOnThread(cleanCachedIAMRunnable, OS_IAM_DB_ACCESS);
896900
}
897901

898902
/**
@@ -1001,6 +1005,7 @@ public ArrayList<OSInAppMessage> getInAppMessageDisplayQueue() {
10011005
return messageDisplayQueue;
10021006
}
10031007

1008+
// Method for testing purposes
10041009
@NonNull
10051010
public List<OSInAppMessage> getRedisplayedInAppMessages() {
10061011
return redisplayedInAppMessages;

0 commit comments

Comments
 (0)