Skip to content

Commit 47a85e3

Browse files
committed
Move outcome table access to OSOutcomeEventsCache
* OneSignalCacheCleaner shouldn't have access to the outcome table when we have a class responsible for managing the outcome database table
1 parent 31a4d1d commit 47a85e3

File tree

6 files changed

+42
-22
lines changed

6 files changed

+42
-22
lines changed

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.onesignal;
22

33
import android.os.Process;
4+
45
import androidx.annotation.NonNull;
56
import androidx.annotation.Nullable;
67

@@ -21,6 +22,8 @@ class OSOutcomeEventsController {
2122
private static final String OS_SEND_SAVED_OUTCOMES = "OS_SEND_SAVED_OUTCOMES";
2223
private static final String OS_SAVE_UNIQUE_OUTCOME_NOTIFICATIONS = "OS_SAVE_UNIQUE_OUTCOME_NOTIFICATIONS";
2324

25+
private final static String OS_DELETE_CACHED_UNIQUE_OUTCOMES_NOTIFICATIONS_THREAD = "OS_DELETE_CACHED_UNIQUE_OUTCOMES_NOTIFICATIONS_THREAD";
26+
2427
// Keeps track of unique outcome events sent for UNATTRIBUTED sessions on a per session level
2528
private Set<String> unattributedUniqueOutcomeEventsSentOnSession;
2629

@@ -56,6 +59,21 @@ void cleanOutcomes() {
5659
saveUnattributedUniqueOutcomeEvents();
5760
}
5861

62+
/**
63+
* Deletes cached unique outcome notifications whose ids do not exist inside of the NotificationTable.TABLE_NAME
64+
*/
65+
void cleanCachedUniqueOutcomes() {
66+
new Thread(new Runnable() {
67+
@Override
68+
public void run() {
69+
Thread.currentThread().setPriority(Process.THREAD_PRIORITY_BACKGROUND);
70+
71+
outcomeEventsFactory.getRepository().cleanCachedUniqueOutcomeEventNotifications(
72+
OneSignalDbContract.NotificationTable.TABLE_NAME, OneSignalDbContract.NotificationTable.COLUMN_NAME_NOTIFICATION_ID);
73+
}
74+
}, OS_DELETE_CACHED_UNIQUE_OUTCOMES_NOTIFICATIONS_THREAD).start();
75+
}
76+
5977
/**
6078
* Any outcomes cached in local DB will be reattempted to be sent again
6179
* Cached outcomes come from the failure callback of the network request

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -840,6 +840,7 @@ private static void setupContextListeners(boolean wasAppContextNull) {
840840

841841
sessionManager.initSessionFromCache();
842842
outcomeEventsController = new OSOutcomeEventsController(sessionManager, outcomeEventsFactory);
843+
outcomeEventsController.cleanCachedUniqueOutcomes();
843844
// Prefs require a context to save
844845
// If the previous state of appContext was null, kick off write in-case it was waiting
845846
OneSignalPrefs.startDelayedWrite();

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

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ public void run() {
3939
Thread.currentThread().setPriority(Process.THREAD_PRIORITY_BACKGROUND);
4040

4141
cleanCachedNotifications(writableDb);
42-
cleanCachedUniqueOutcomeEventNotifications(writableDb);
4342
}
4443

4544
}, OS_DELETE_CACHED_NOTIFICATIONS_THREAD).start();
@@ -86,25 +85,4 @@ private static void cleanCachedNotifications(OneSignalDbHelper writableDb) {
8685
whereArgs);
8786
}
8887

89-
/**
90-
* Deletes cached unique outcome notifications whose ids do not exist inside of the NotificationTable.TABLE_NAME
91-
* <br/><br/>
92-
* Note: This should only ever be called by {@link OneSignalCacheCleaner#cleanNotificationCache(OneSignalDbHelper)}
93-
* <br/><br/>
94-
*
95-
* @see OneSignalCacheCleaner#cleanNotificationCache(OneSignalDbHelper)
96-
*/
97-
private static void cleanCachedUniqueOutcomeEventNotifications(OneSignalDbHelper writableDb) {
98-
String whereStr = "NOT EXISTS(" +
99-
"SELECT NULL FROM " + NotificationTable.TABLE_NAME + " n " +
100-
"WHERE" + " n." + NotificationTable.COLUMN_NAME_NOTIFICATION_ID + " = " + OutcomesDbContract.CACHE_UNIQUE_OUTCOME_COLUMN_CHANNEL_INFLUENCE_ID +
101-
" AND " + OutcomesDbContract.CACHE_UNIQUE_OUTCOME_COLUMN_CHANNEL_TYPE + " = \"" + OSInfluenceChannel.NOTIFICATION.toString().toLowerCase() +
102-
"\")";
103-
104-
writableDb.delete(
105-
OutcomesDbContract.CACHE_UNIQUE_OUTCOME_TABLE,
106-
whereStr,
107-
null);
108-
}
109-
11088
}

OneSignalSDK/onesignal/src/main/java/com/onesignal/outcomes/data/OSOutcomeEventsCache.kt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package com.onesignal.outcomes.data
22

33
import android.content.ContentValues
44
import android.database.Cursor
5+
import android.database.sqlite.SQLiteDatabase
56
import androidx.annotation.WorkerThread
67
import com.onesignal.OSLogger
78
import com.onesignal.OSSharedPreferences
@@ -311,4 +312,21 @@ internal class OSOutcomeEventsCache(private val logger: OSLogger,
311312
}
312313
return uniqueInfluences
313314
}
315+
316+
/**
317+
* Deletes cached unique outcome notifications whose ids do not exist inside of the NotificationTable.TABLE_NAME
318+
*/
319+
@WorkerThread
320+
@Synchronized
321+
fun cleanCachedUniqueOutcomeEventNotifications(notificationTableName: String, notificationIdColumnName: String) {
322+
val whereStr = "NOT EXISTS(" +
323+
"SELECT NULL FROM " + notificationTableName + " n " +
324+
"WHERE" + " n." + notificationIdColumnName + " = " + OutcomesDbContract.CACHE_UNIQUE_OUTCOME_COLUMN_CHANNEL_INFLUENCE_ID +
325+
" AND " + OutcomesDbContract.CACHE_UNIQUE_OUTCOME_COLUMN_CHANNEL_TYPE + " = \"" + OSInfluenceChannel.NOTIFICATION.toString().toLowerCase(Locale.ROOT) +
326+
"\")"
327+
dbHelper.delete(
328+
OutcomesDbContract.CACHE_UNIQUE_OUTCOME_TABLE,
329+
whereStr,
330+
null)
331+
}
314332
}

OneSignalSDK/onesignal/src/main/java/com/onesignal/outcomes/data/OSOutcomeEventsRepository.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,8 @@ internal abstract class OSOutcomeEventsRepository(protected val logger: OSLogger
4141
logger.debug("OneSignal save unattributedUniqueOutcomeEvents: $unattributedUniqueOutcomeEvents")
4242
outcomeEventsCache.saveUnattributedUniqueOutcomeEventsSentByChannel(unattributedUniqueOutcomeEvents)
4343
}
44+
45+
override fun cleanCachedUniqueOutcomeEventNotifications(notificationTableName: String, notificationIdColumnName: String) {
46+
outcomeEventsCache.cleanCachedUniqueOutcomeEventNotifications(notificationTableName, notificationIdColumnName);
47+
}
4448
}

OneSignalSDK/onesignal/src/main/java/com/onesignal/outcomes/domain/OSOutcomeEventsRepository.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@ interface OSOutcomeEventsRepository {
1212
fun getNotCachedUniqueOutcome(name: String, influences: List<OSInfluence>): List<OSInfluence>
1313
fun getUnattributedUniqueOutcomeEventsSent(): Set<String>?
1414
fun saveUnattributedUniqueOutcomeEventsSent(unattributedUniqueOutcomeEvents: Set<String>)
15+
fun cleanCachedUniqueOutcomeEventNotifications(notificationTableName: String, notificationIdColumnName: String);
1516
}

0 commit comments

Comments
 (0)