Skip to content

Commit 7ec035d

Browse files
committed
Move housekeeping scheduling logic to background thread
1 parent ed49b59 commit 7ec035d

File tree

2 files changed

+34
-19
lines changed

2 files changed

+34
-19
lines changed

com.unity.mobile.notifications/Runtime/Android/Plugins/com/unity/androidnotifications/UnityNotificationBackgroundThread.java

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package com.unity.androidnotifications;
22

3+
import static com.unity.androidnotifications.UnityNotificationManager.TAG_UNITY;
4+
35
import android.app.Notification;
46
import android.content.Context;
7+
import android.util.Log;
58

69
import java.util.concurrent.LinkedTransferQueue;
710
import java.util.Set;
@@ -21,8 +24,12 @@ public void run() {
2124
}
2225

2326
private LinkedTransferQueue<Runnable> mTasks = new LinkedTransferQueue();
27+
private int mSentNotificationsSinceHousekeeping = 0;
28+
private int mOtherTasksSinceHousekeeping = 0;
2429

2530
public UnityNotificationBackgroundThread() {
31+
// force housekeeping
32+
mOtherTasksSinceHousekeeping = 2;
2633
enqueueHousekeeping();
2734
}
2835

@@ -50,7 +57,7 @@ public void enqueueCancelAllNotifications() {
5057
});
5158
}
5259

53-
public void enqueueHousekeeping() {
60+
private void enqueueHousekeeping() {
5461
mTasks.add(() -> { performHousekeeping(); });
5562
}
5663

@@ -59,15 +66,40 @@ public void run() {
5966
while (true) {
6067
try {
6168
Runnable task = mTasks.take();
62-
task.run();
69+
executeTask(task);
6370
} catch (InterruptedException e) {
6471
if (mTasks.isEmpty())
6572
break;
6673
}
6774
}
6875
}
6976

77+
private void executeTask(Runnable task) {
78+
try {
79+
ScheduleNotificationTask scheduleTask = null;
80+
if (task instanceof ScheduleNotificationTask)
81+
scheduleTask = (ScheduleNotificationTask)task;
82+
83+
task.run();
84+
85+
if (scheduleTask != null)
86+
++mSentNotificationsSinceHousekeeping;
87+
else
88+
++mOtherTasksSinceHousekeeping;
89+
if (mSentNotificationsSinceHousekeeping >= 50)
90+
enqueueHousekeeping();
91+
} catch (Exception e) {
92+
Log.e(TAG_UNITY, "Exception executing notification task", e);
93+
}
94+
}
95+
7096
private void performHousekeeping() {
97+
// don't do housekeeping if last task we did was housekeeping (other=1)
98+
boolean performHousekeeping = mSentNotificationsSinceHousekeeping > 0 && mOtherTasksSinceHousekeeping > 1;
99+
mSentNotificationsSinceHousekeeping = 0;
100+
mOtherTasksSinceHousekeeping = 0;
101+
if (!performHousekeeping)
102+
return;
71103
Context context = UnityNotificationManager.mUnityNotificationManager.mContext;
72104
Set<String> notificationIds = UnityNotificationManager.getScheduledNotificationIDs(context);
73105
UnityNotificationManager.performNotificationHousekeeping(context, notificationIds);

com.unity.mobile.notifications/Runtime/Android/Plugins/com/unity/androidnotifications/UnityNotificationManager.java

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ public class UnityNotificationManager extends BroadcastReceiver {
3939
protected static UnityNotificationManager mUnityNotificationManager;
4040
private static HashMap<Integer, Notification> mScheduledNotifications = new HashMap();
4141
private static HashSet<Integer> mVisibleNotifications = new HashSet<>();
42-
private static int mSentSinceLastHousekeeping = 0;
4342

4443
public Context mContext = null;
4544
protected Activity mActivity = null;
@@ -373,24 +372,9 @@ private static synchronized Intent buildNotificationIntentUpdateList(Context con
373372
ids = new HashSet<>(ids);
374373
ids.add(String.valueOf(notificationId));
375374
saveScheduledNotificationIDs(context, ids);
376-
scheduleHousekeeping(context, ids);
377375
return intent;
378376
}
379377

380-
private static synchronized void scheduleHousekeeping(Context context, Set<String> ids) {
381-
++mSentSinceLastHousekeeping;
382-
if (mSentSinceLastHousekeeping > 50) {
383-
mSentSinceLastHousekeeping = 0;
384-
triggerHousekeeping();
385-
}
386-
}
387-
388-
private static synchronized void triggerHousekeeping() {
389-
if (mUnityNotificationManager != null) {
390-
mUnityNotificationManager.mBackgroundThread.enqueueHousekeeping();
391-
}
392-
}
393-
394378
protected static void performNotificationHousekeeping(Context context, Set<String> ids) {
395379
Log.d(TAG_UNITY, "Checking for invalid notification IDs still hanging around");
396380

@@ -402,7 +386,6 @@ protected static void performNotificationHousekeeping(Context context, Set<Strin
402386
removeScheduledNotification(Integer.valueOf(id));
403387
}
404388
saveScheduledNotificationIDs(context, currentIds);
405-
mSentSinceLastHousekeeping = 0;
406389
}
407390

408391
// in case we have saved intents, clear them

0 commit comments

Comments
 (0)