Skip to content

Commit 12872ee

Browse files
committed
Queue housekeeping to separate thread
1 parent 1bc796a commit 12872ee

File tree

3 files changed

+84
-21
lines changed

3 files changed

+84
-21
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.unity.androidnotifications;
2+
3+
import java.util.concurrent.LinkedTransferQueue;
4+
5+
public class UnityNotificationBackgroundThread extends Thread {
6+
private LinkedTransferQueue<Runnable> mTasks = new LinkedTransferQueue();
7+
8+
public void enqueueTask(Runnable task) {
9+
mTasks.add(task);
10+
}
11+
12+
@Override
13+
public void run() {
14+
while (true) {
15+
try {
16+
Runnable task = mTasks.take();
17+
task.run();
18+
android.util.Log.d("Unity", "Notification background task done");
19+
} catch (InterruptedException e) {
20+
if (mTasks.isEmpty())
21+
break;
22+
}
23+
}
24+
25+
android.util.Log.d("Unity", "Notification background thread exited");
26+
}
27+
}

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

Lines changed: 32 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ public class UnityNotificationManager extends BroadcastReceiver {
4545
public Context mContext = null;
4646
protected Activity mActivity = null;
4747
protected Class mOpenActivity = null;
48+
protected UnityNotificationBackgroundThread mBackgroundThread;
4849

4950
protected static final int SAMSUNG_NOTIFICATION_LIMIT = 500;
5051
protected static final String TAG_UNITY = "UnityNotifications";
@@ -75,6 +76,8 @@ public UnityNotificationManager(Context context, Activity activity) {
7576
super();
7677
mContext = context;
7778
mActivity = activity;
79+
mBackgroundThread = new UnityNotificationBackgroundThread();
80+
mBackgroundThread.start();
7881

7982
try {
8083
ApplicationInfo ai = activity.getPackageManager().getApplicationInfo(activity.getPackageName(), PackageManager.GET_META_DATA);
@@ -385,28 +388,29 @@ private static synchronized void triggerHousekeeping(Context context, Set<String
385388

386389
// needed for lamda
387390
final Set<String> notificationIds = ids;
388-
Thread housekeepingThread = new Thread(() -> {
389-
try {
390-
// when scheduling lots of notifications at once we can have more than one housekeeping thread running
391-
// synchronize them and chain to happen one after the other
392-
synchronized (UnityNotificationManager.class) {
393-
while (mPerformingHousekeeping) {
394-
UnityNotificationManager.class.wait();
391+
if (mUnityNotificationManager != null) {
392+
mUnityNotificationManager.mBackgroundThread.enqueueTask(() -> {
393+
try {
394+
// when scheduling lots of notifications at once we can have more than one housekeeping thread running
395+
// synchronize them and chain to happen one after the other
396+
synchronized (UnityNotificationManager.class) {
397+
while (mPerformingHousekeeping) {
398+
UnityNotificationManager.class.wait();
399+
}
400+
mPerformingHousekeeping = true;
395401
}
396-
mPerformingHousekeeping = true;
397-
}
398402

399-
performNotificationHousekeeping(context, notificationIds);
400-
} catch (InterruptedException e) {
401-
Log.e(TAG_UNITY, "Notification housekeeping interrupted");
402-
} finally {
403-
synchronized (UnityNotificationManager.class) {
404-
mPerformingHousekeeping = false;
405-
UnityNotificationManager.class.notify();
403+
performNotificationHousekeeping(context, notificationIds);
404+
} catch (InterruptedException e) {
405+
Log.e(TAG_UNITY, "Notification housekeeping interrupted");
406+
} finally {
407+
synchronized (UnityNotificationManager.class) {
408+
mPerformingHousekeeping = false;
409+
UnityNotificationManager.class.notify();
410+
}
406411
}
407-
}
408-
});
409-
housekeepingThread.start();
412+
});
413+
}
410414
}
411415

412416
private static void performNotificationHousekeeping(Context context, Set<String> ids) {
@@ -599,13 +603,13 @@ public void cancelAllPendingNotificationIntents() {
599603

600604
if (ids.size() > 0) {
601605
Context context = mContext;
602-
new Thread(() -> {
606+
mBackgroundThread.enqueueTask(() -> {
603607
for (String id : ids) {
604608
cancelPendingNotificationIntent(context, Integer.valueOf(id));
605609
deleteExpiredNotificationIntent(context, id);
606610
}
607611
triggerHousekeeping(context, null);
608-
}).start();
612+
});
609613
}
610614
}
611615

0 commit comments

Comments
 (0)