12
12
13
13
public class UnityNotificationBackgroundThread extends Thread {
14
14
private static abstract class Task {
15
- public abstract void run (Context context );
15
+ public abstract void run (Context context , Set < String > notificationIds );
16
16
}
17
17
18
18
private static class ScheduleNotificationTask extends Task {
@@ -25,15 +25,13 @@ public ScheduleNotificationTask(int id, Notification.Builder builder) {
25
25
}
26
26
27
27
@ Override
28
- public void run (Context context ) {
29
- Set <String > ids = UnityNotificationManager .getScheduledNotificationIDs (context );
28
+ public void run (Context context , Set <String > notificationIds ) {
30
29
String id = String .valueOf (notificationId );
31
30
// are we replacing existing alarm or have capacity to schedule new one
32
- if (!(ids .contains (id ) || UnityNotificationManager .canScheduleMoreAlarms (ids )))
31
+ if (!(notificationIds .contains (id ) || UnityNotificationManager .canScheduleMoreAlarms (notificationIds )))
33
32
return ;
34
- ids = new HashSet <>(ids );
35
- ids .add (id );
36
- UnityNotificationManager .saveScheduledNotificationIDs (context , ids );
33
+ notificationIds .add (id );
34
+ UnityNotificationManager .saveScheduledNotificationIDs (context , notificationIds );
37
35
UnityNotificationManager .mUnityNotificationManager .performNotificationScheduling (notificationId , notificationBuilder );
38
36
}
39
37
}
@@ -46,20 +44,23 @@ public CancelNotificationTask(int id) {
46
44
}
47
45
48
46
@ Override
49
- public void run (Context context ) {
47
+ public void run (Context context , Set < String > notificationIds ) {
50
48
UnityNotificationManager .cancelPendingNotificationIntent (context , notificationId );
49
+ String id = String .valueOf (notificationId );
50
+ UnityNotificationManager .deleteExpiredNotificationIntent (context , id );
51
+ notificationIds .remove (id );
51
52
}
52
53
}
53
54
54
55
private static class CancelAllNotificationsTask extends Task {
55
56
@ Override
56
- public void run (Context context ) {
57
- Set <String > ids = UnityNotificationManager .getScheduledNotificationIDs (context );
58
-
59
- for (String id : ids ) {
57
+ public void run (Context context , Set <String > notificationIds ) {
58
+ for (String id : notificationIds ) {
60
59
UnityNotificationManager .cancelPendingNotificationIntent (context , Integer .valueOf (id ));
61
60
UnityNotificationManager .deleteExpiredNotificationIntent (context , id );
62
61
}
62
+
63
+ notificationIds .clear ();
63
64
}
64
65
}
65
66
@@ -71,8 +72,8 @@ public HousekeepingTask(UnityNotificationBackgroundThread th) {
71
72
}
72
73
73
74
@ Override
74
- public void run (Context context ) {
75
- thread .performHousekeeping (context );
75
+ public void run (Context context , Set < String > notificationIds ) {
76
+ thread .performHousekeeping (context , notificationIds );
76
77
}
77
78
}
78
79
@@ -105,24 +106,25 @@ private void enqueueHousekeeping() {
105
106
@ Override
106
107
public void run () {
107
108
Context context = UnityNotificationManager .mUnityNotificationManager .mContext ;
109
+ HashSet <String > notificationIds = new HashSet (UnityNotificationManager .getScheduledNotificationIDs (context ));
108
110
while (true ) {
109
111
try {
110
112
Task task = mTasks .take ();
111
- executeTask (context , task );
113
+ executeTask (context , task , notificationIds );
112
114
} catch (InterruptedException e ) {
113
115
if (mTasks .isEmpty ())
114
116
break ;
115
117
}
116
118
}
117
119
}
118
120
119
- private void executeTask (Context context , Task task ) {
121
+ private void executeTask (Context context , Task task , Set < String > notificationIds ) {
120
122
try {
121
123
ScheduleNotificationTask scheduleTask = null ;
122
124
if (task instanceof ScheduleNotificationTask )
123
125
scheduleTask = (ScheduleNotificationTask )task ;
124
126
125
- task .run (context );
127
+ task .run (context , notificationIds );
126
128
127
129
if (scheduleTask != null )
128
130
++mSentNotificationsSinceHousekeeping ;
@@ -136,14 +138,13 @@ private void executeTask(Context context, Task task) {
136
138
}
137
139
}
138
140
139
- private void performHousekeeping (Context context ) {
141
+ private void performHousekeeping (Context context , Set < String > notificationIds ) {
140
142
// don't do housekeeping if last task we did was housekeeping (other=1)
141
143
boolean performHousekeeping = mSentNotificationsSinceHousekeeping > 0 && mOtherTasksSinceHousekeeping > 1 ;
142
144
mSentNotificationsSinceHousekeeping = 0 ;
143
145
mOtherTasksSinceHousekeeping = 0 ;
144
146
if (!performHousekeeping )
145
147
return ;
146
- Set <String > notificationIds = UnityNotificationManager .getScheduledNotificationIDs (context );
147
148
UnityNotificationManager .performNotificationHousekeeping (context , notificationIds );
148
149
}
149
150
}
0 commit comments