12
12
13
13
public class UnityNotificationBackgroundThread extends Thread {
14
14
private static abstract class Task {
15
- public abstract void run (Context context , Set <String > notificationIds );
15
+ // returns true if notificationIds was modified (needs to be saved)
16
+ public abstract boolean run (Context context , Set <String > notificationIds );
16
17
}
17
18
18
19
private static class ScheduleNotificationTask extends Task {
@@ -25,14 +26,13 @@ public ScheduleNotificationTask(int id, Notification.Builder builder) {
25
26
}
26
27
27
28
@ Override
28
- public void run (Context context , Set <String > notificationIds ) {
29
+ public boolean run (Context context , Set <String > notificationIds ) {
29
30
String id = String .valueOf (notificationId );
30
31
// are we replacing existing alarm or have capacity to schedule new one
31
32
if (!(notificationIds .contains (id ) || UnityNotificationManager .canScheduleMoreAlarms (notificationIds )))
32
- return ;
33
- notificationIds .add (id );
34
- UnityNotificationManager .saveScheduledNotificationIDs (context , notificationIds );
33
+ return false ;
35
34
UnityNotificationManager .mUnityNotificationManager .performNotificationScheduling (notificationId , notificationBuilder );
35
+ return notificationIds .add (id );
36
36
}
37
37
}
38
38
@@ -44,23 +44,25 @@ public CancelNotificationTask(int id) {
44
44
}
45
45
46
46
@ Override
47
- public void run (Context context , Set <String > notificationIds ) {
47
+ public boolean run (Context context , Set <String > notificationIds ) {
48
48
UnityNotificationManager .cancelPendingNotificationIntent (context , notificationId );
49
49
String id = String .valueOf (notificationId );
50
50
UnityNotificationManager .deleteExpiredNotificationIntent (context , id );
51
51
notificationIds .remove (id );
52
+ return true ;
52
53
}
53
54
}
54
55
55
56
private static class CancelAllNotificationsTask extends Task {
56
57
@ Override
57
- public void run (Context context , Set <String > notificationIds ) {
58
+ public boolean run (Context context , Set <String > notificationIds ) {
58
59
for (String id : notificationIds ) {
59
60
UnityNotificationManager .cancelPendingNotificationIntent (context , Integer .valueOf (id ));
60
61
UnityNotificationManager .deleteExpiredNotificationIntent (context , id );
61
62
}
62
63
63
64
notificationIds .clear ();
65
+ return true ;
64
66
}
65
67
}
66
68
@@ -72,8 +74,9 @@ public HousekeepingTask(UnityNotificationBackgroundThread th) {
72
74
}
73
75
74
76
@ Override
75
- public void run (Context context , Set <String > notificationIds ) {
77
+ public boolean run (Context context , Set <String > notificationIds ) {
76
78
thread .performHousekeeping (context , notificationIds );
79
+ return false ;
77
80
}
78
81
}
79
82
@@ -105,26 +108,30 @@ private void enqueueHousekeeping() {
105
108
public void run () {
106
109
Context context = UnityNotificationManager .mUnityNotificationManager .mContext ;
107
110
HashSet <String > notificationIds = new HashSet (UnityNotificationManager .getScheduledNotificationIDs (context ));
111
+ boolean haveChanges = false ;
108
112
while (true ) {
109
113
try {
110
114
Task task = mTasks .take ();
111
- executeTask (context , task , notificationIds );
115
+ haveChanges |= executeTask (context , task , notificationIds );
112
116
if (!(task instanceof HousekeepingTask ))
113
117
++mTasksSinceHousekeeping ;
114
- if (mTasks .size () == 0 )
118
+ if (mTasks .size () == 0 && haveChanges ) {
119
+ haveChanges = false ;
115
120
enqueueHousekeeping ();
121
+ }
116
122
} catch (InterruptedException e ) {
117
123
if (mTasks .isEmpty ())
118
124
break ;
119
125
}
120
126
}
121
127
}
122
128
123
- private void executeTask (Context context , Task task , Set <String > notificationIds ) {
129
+ private boolean executeTask (Context context , Task task , Set <String > notificationIds ) {
124
130
try {
125
- task .run (context , notificationIds );
131
+ return task .run (context , notificationIds );
126
132
} catch (Exception e ) {
127
133
Log .e (TAG_UNITY , "Exception executing notification task" , e );
134
+ return false ;
128
135
}
129
136
}
130
137
@@ -134,5 +141,6 @@ private void performHousekeeping(Context context, Set<String> notificationIds) {
134
141
mTasksSinceHousekeeping = 0 ;
135
142
if (performHousekeeping )
136
143
UnityNotificationManager .performNotificationHousekeeping (context , notificationIds );
144
+ UnityNotificationManager .saveScheduledNotificationIDs (context , notificationIds );
137
145
}
138
146
}
0 commit comments