|
2 | 2 |
|
3 | 3 | import android.app.AlertDialog;
|
4 | 4 | import android.content.DialogInterface;
|
5 |
| -import android.os.Process; |
6 | 5 |
|
7 | 6 | import androidx.annotation.NonNull;
|
8 | 7 | import androidx.annotation.Nullable;
|
|
23 | 22 | import java.util.Map;
|
24 | 23 | import java.util.Set;
|
25 | 24 |
|
26 |
| -class OSInAppMessageController implements OSDynamicTriggerControllerObserver, OSSystemConditionController.OSSystemConditionObserver { |
| 25 | +class OSInAppMessageController extends OSBackgroundManager implements OSDynamicTriggerControllerObserver, OSSystemConditionController.OSSystemConditionObserver { |
27 | 26 |
|
28 | 27 | 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"; |
31 | 29 | public static final String IN_APP_MESSAGES_JSON_KEY = "in_app_messages";
|
32 | 30 | private static final String LIQUID_TAG_SCRIPT = "\n\n" +
|
33 | 31 | "<script>\n" +
|
@@ -68,7 +66,7 @@ class OSInAppMessageController implements OSDynamicTriggerControllerObserver, OS
|
68 | 66 | // IAMs displayed with last displayed time and quantity of displays data
|
69 | 67 | // This is retrieved from a DB Table that take care of each object to be unique
|
70 | 68 | @NonNull
|
71 |
| - private List<OSInAppMessage> redisplayedInAppMessages; |
| 69 | + private List<OSInAppMessage> redisplayedInAppMessages = new ArrayList<>(); |
72 | 70 |
|
73 | 71 | private OSInAppMessagePrompt currentPrompt = null;
|
74 | 72 | private boolean inAppMessagingEnabled = true;
|
@@ -141,9 +139,15 @@ OSInAppMessageRepository getInAppMessageRepository(OneSignalDbHelper dbHelper) {
|
141 | 139 |
|
142 | 140 | protected void initRedisplayData(OneSignalDbHelper dbHelper) {
|
143 | 141 | 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 | + }; |
145 | 149 |
|
146 |
| - OneSignal.Log(OneSignal.LOG_LEVEL.DEBUG, "redisplayedInAppMessages: " + redisplayedInAppMessages.toString()); |
| 150 | + runRunnableOnThread(getCachedIAMRunnable, OS_IAM_DB_ACCESS); |
147 | 151 | }
|
148 | 152 |
|
149 | 153 | void resetSessionLaunchTime() {
|
@@ -725,13 +729,13 @@ private void persistInAppMessage(final OSInAppMessage message) {
|
725 | 729 | message.setTriggerChanged(false);
|
726 | 730 | message.setDisplayedInSession(true);
|
727 | 731 |
|
728 |
| - new Thread(new Runnable() { |
| 732 | + Runnable saveIAMOnDBRunnable = new BackgroundRunnable() { |
729 | 733 | @Override
|
730 | 734 | public void run() {
|
731 |
| - Thread.currentThread().setPriority(Process.THREAD_PRIORITY_BACKGROUND); |
732 | 735 | inAppMessageRepository.saveInAppMessage(message);
|
733 | 736 | }
|
734 |
| - }, OS_SAVE_IN_APP_MESSAGE).start(); |
| 737 | + }; |
| 738 | + runRunnableOnThread(saveIAMOnDBRunnable, OS_IAM_DB_ACCESS); |
735 | 739 |
|
736 | 740 | // Update the data to enable future re displays
|
737 | 741 | // Avoid calling the repository data again
|
@@ -885,14 +889,14 @@ void onSuccess(String response) {
|
885 | 889 | * 3. Use queried data to clean SharedPreferences
|
886 | 890 | */
|
887 | 891 | void cleanCachedInAppMessages() {
|
888 |
| - new Thread(new Runnable() { |
| 892 | + Runnable cleanCachedIAMRunnable = new BackgroundRunnable() { |
889 | 893 | @Override
|
890 | 894 | public void run() {
|
891 |
| - Thread.currentThread().setPriority(Process.THREAD_PRIORITY_BACKGROUND); |
892 |
| - |
893 | 895 | inAppMessageRepository.cleanCachedInAppMessages();
|
894 | 896 | }
|
895 |
| - }, OS_DELETE_CACHED_REDISPLAYED_IAMS_THREAD).start(); |
| 897 | + }; |
| 898 | + |
| 899 | + runRunnableOnThread(cleanCachedIAMRunnable, OS_IAM_DB_ACCESS); |
896 | 900 | }
|
897 | 901 |
|
898 | 902 | /**
|
@@ -1001,6 +1005,7 @@ public ArrayList<OSInAppMessage> getInAppMessageDisplayQueue() {
|
1001 | 1005 | return messageDisplayQueue;
|
1002 | 1006 | }
|
1003 | 1007 |
|
| 1008 | + // Method for testing purposes |
1004 | 1009 | @NonNull
|
1005 | 1010 | public List<OSInAppMessage> getRedisplayedInAppMessages() {
|
1006 | 1011 | return redisplayedInAppMessages;
|
|
0 commit comments