Skip to content

Commit 9ee7aae

Browse files
committed
Rename OneSignalCacheCleaner to OSNotificationDataController
* OSNotificationDataController will be the new class responsible for handling notification table access
1 parent cfcc572 commit 9ee7aae

File tree

3 files changed

+70
-94
lines changed

3 files changed

+70
-94
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.onesignal;
2+
3+
import android.os.Process;
4+
5+
import com.onesignal.OneSignalDbContract.NotificationTable;
6+
7+
class OSNotificationDataController {
8+
9+
private final static long NOTIFICATION_CACHE_DATA_LIFETIME = 604_800L; // 7 days in second
10+
11+
private final static String OS_DELETE_CACHED_NOTIFICATIONS_THREAD = "OS_DELETE_CACHED_NOTIFICATIONS_THREAD";
12+
13+
private final OneSignalDbHelper dbHelper;
14+
15+
public OSNotificationDataController(OneSignalDbHelper dbHelper) {
16+
this.dbHelper = dbHelper;
17+
}
18+
19+
/**
20+
* We clean outdated cache from several places within the OneSignal SDK here
21+
* 1. Notifications & unique outcome events linked to notification ids (1 week)
22+
* 2. Cached In App Messaging Sets in SharedPreferences (impressions, clicks, views) and SQL IAMs
23+
*/
24+
void cleanOldCachedData() {
25+
cleanNotificationCache();
26+
}
27+
28+
/**
29+
* Cleans two notification tables
30+
* 1. NotificationTable.TABLE_NAME
31+
* 2. CachedUniqueOutcomeNotificationTable.TABLE_NAME
32+
*/
33+
private void cleanNotificationCache() {
34+
new Thread(new Runnable() {
35+
@Override
36+
public void run() {
37+
Thread.currentThread().setPriority(Process.THREAD_PRIORITY_BACKGROUND);
38+
39+
cleanCachedNotifications(dbHelper);
40+
}
41+
42+
}, OS_DELETE_CACHED_NOTIFICATIONS_THREAD).start();
43+
}
44+
45+
/**
46+
* Deletes notifications with created timestamps older than 7 days
47+
*/
48+
private void cleanCachedNotifications(OneSignalDb writableDb) {
49+
String whereStr = NotificationTable.COLUMN_NAME_CREATED_TIME + " < ?";
50+
51+
String sevenDaysAgoInSeconds = String.valueOf((OneSignal.getTime().getCurrentTimeMillis() / 1_000L) - NOTIFICATION_CACHE_DATA_LIFETIME);
52+
String[] whereArgs = new String[]{sevenDaysAgoInSeconds};
53+
54+
writableDb.delete(
55+
NotificationTable.TABLE_NAME,
56+
whereStr,
57+
whereArgs);
58+
}
59+
60+
}

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

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
import android.app.AlertDialog;
3232
import android.app.Application;
3333
import android.app.NotificationManager;
34-
import android.app.PendingIntent;
3534
import android.content.ContentValues;
3635
import android.content.Context;
3736
import android.content.Intent;
@@ -433,6 +432,7 @@ static OSInAppMessageController getInAppMessageController() {
433432
private static OSSessionManager sessionManager = new OSSessionManager(sessionListener, trackerFactory, logger);
434433
@Nullable private static OSOutcomeEventsController outcomeEventsController;
435434
@Nullable private static OSOutcomeEventsFactory outcomeEventsFactory;
435+
@Nullable private static OSNotificationDataController notificationCache;
436436

437437
@Nullable private static AdvertisingIdentifierProvider adIdProvider;
438438
private static synchronized @Nullable AdvertisingIdentifierProvider getAdIdProvider() {
@@ -835,17 +835,21 @@ private static void setupContextListeners(boolean wasAppContextNull) {
835835

836836
// Do work here that should only happen once or at the start of a new lifecycle
837837
if (wasAppContextNull) {
838+
// Prefs require a context to save
839+
// If the previous state of appContext was null, kick off write in-case it was waiting
840+
OneSignalPrefs.startDelayedWrite();
841+
notificationCache = new OSNotificationDataController(getDBHelperInstance());
842+
// Cleans out old cached data to prevent over using the storage on devices
843+
notificationCache.cleanOldCachedData();
844+
845+
getInAppMessageController().cleanCachedInAppMessages();
846+
838847
if (outcomeEventsFactory == null)
839848
outcomeEventsFactory = new OSOutcomeEventsFactory(logger, apiClient, getDBHelperInstance(), preferences);
840849

841850
sessionManager.initSessionFromCache();
842851
outcomeEventsController = new OSOutcomeEventsController(sessionManager, outcomeEventsFactory);
843852
outcomeEventsController.cleanCachedUniqueOutcomes();
844-
// Prefs require a context to save
845-
// If the previous state of appContext was null, kick off write in-case it was waiting
846-
OneSignalPrefs.startDelayedWrite();
847-
// Cleans out old cached data to prevent over using the storage on devices
848-
OneSignalCacheCleaner.cleanOldCachedData(appContext);
849853
}
850854
}
851855

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

Lines changed: 0 additions & 88 deletions
This file was deleted.

0 commit comments

Comments
 (0)