11
11
import java .util .Set ;
12
12
13
13
public class UnityNotificationBackgroundThread extends Thread {
14
- private static class ScheduleNotificationTask implements Runnable {
14
+ private static abstract class Task {
15
+ public abstract void run (Context context );
16
+ }
17
+
18
+ private static class ScheduleNotificationTask extends Task {
15
19
private int notificationId ;
16
20
private Notification .Builder notificationBuilder ;
17
21
@@ -21,8 +25,7 @@ public ScheduleNotificationTask(int id, Notification.Builder builder) {
21
25
}
22
26
23
27
@ Override
24
- public void run () {
25
- Context context = UnityNotificationManager .mUnityNotificationManager .mContext ;
28
+ public void run (Context context ) {
26
29
Set <String > ids = UnityNotificationManager .getScheduledNotificationIDs (context );
27
30
String id = String .valueOf (notificationId );
28
31
// are we replacing existing alarm or have capacity to schedule new one
@@ -35,7 +38,45 @@ public void run() {
35
38
}
36
39
}
37
40
38
- private LinkedTransferQueue <Runnable > mTasks = new LinkedTransferQueue ();
41
+ private static class CancelNotificationTask extends Task {
42
+ private int notificationId ;
43
+
44
+ public CancelNotificationTask (int id ) {
45
+ notificationId = id ;
46
+ }
47
+
48
+ @ Override
49
+ public void run (Context context ) {
50
+ UnityNotificationManager .cancelPendingNotificationIntent (context , notificationId );
51
+ }
52
+ }
53
+
54
+ private static class CancelAllNotificationsTask extends Task {
55
+ @ Override
56
+ public void run (Context context ) {
57
+ Set <String > ids = UnityNotificationManager .getScheduledNotificationIDs (context );
58
+
59
+ for (String id : ids ) {
60
+ UnityNotificationManager .cancelPendingNotificationIntent (context , Integer .valueOf (id ));
61
+ UnityNotificationManager .deleteExpiredNotificationIntent (context , id );
62
+ }
63
+ }
64
+ }
65
+
66
+ private static class HousekeepingTask extends Task {
67
+ UnityNotificationBackgroundThread thread ;
68
+
69
+ public HousekeepingTask (UnityNotificationBackgroundThread th ) {
70
+ thread = th ;
71
+ }
72
+
73
+ @ Override
74
+ public void run (Context context ) {
75
+ thread .performHousekeeping (context );
76
+ }
77
+ }
78
+
79
+ private LinkedTransferQueue <Task > mTasks = new LinkedTransferQueue ();
39
80
private int mSentNotificationsSinceHousekeeping = 0 ;
40
81
private int mOtherTasksSinceHousekeeping = 0 ;
41
82
@@ -50,49 +91,38 @@ public void enqueueNotification(int id, Notification.Builder notificationBuilder
50
91
}
51
92
52
93
public void enqueueCancelNotification (int id ) {
53
- mTasks .add (() -> {
54
- UnityNotificationManager .cancelPendingNotificationIntent (UnityNotificationManager .mUnityNotificationManager .mContext , id );
55
- });
94
+ mTasks .add (new CancelNotificationTask (id ));
56
95
}
57
96
58
97
public void enqueueCancelAllNotifications () {
59
- mTasks .add (() -> {
60
- Context context = UnityNotificationManager .mUnityNotificationManager .mContext ;
61
- Set <String > ids = UnityNotificationManager .getScheduledNotificationIDs (context );
62
-
63
- if (ids .size () > 0 ) {
64
- for (String id : ids ) {
65
- UnityNotificationManager .cancelPendingNotificationIntent (context , Integer .valueOf (id ));
66
- UnityNotificationManager .deleteExpiredNotificationIntent (context , id );
67
- }
68
- }
69
- });
98
+ mTasks .add (new CancelAllNotificationsTask ());
70
99
}
71
100
72
101
private void enqueueHousekeeping () {
73
- mTasks .add (() -> { performHousekeeping (); } );
102
+ mTasks .add (new HousekeepingTask ( this ) );
74
103
}
75
104
76
105
@ Override
77
106
public void run () {
107
+ Context context = UnityNotificationManager .mUnityNotificationManager .mContext ;
78
108
while (true ) {
79
109
try {
80
- Runnable task = mTasks .take ();
81
- executeTask (task );
110
+ Task task = mTasks .take ();
111
+ executeTask (context , task );
82
112
} catch (InterruptedException e ) {
83
113
if (mTasks .isEmpty ())
84
114
break ;
85
115
}
86
116
}
87
117
}
88
118
89
- private void executeTask (Runnable task ) {
119
+ private void executeTask (Context context , Task task ) {
90
120
try {
91
121
ScheduleNotificationTask scheduleTask = null ;
92
122
if (task instanceof ScheduleNotificationTask )
93
123
scheduleTask = (ScheduleNotificationTask )task ;
94
124
95
- task .run ();
125
+ task .run (context );
96
126
97
127
if (scheduleTask != null )
98
128
++mSentNotificationsSinceHousekeeping ;
@@ -105,14 +135,13 @@ private void executeTask(Runnable task) {
105
135
}
106
136
}
107
137
108
- private void performHousekeeping () {
138
+ private void performHousekeeping (Context context ) {
109
139
// don't do housekeeping if last task we did was housekeeping (other=1)
110
140
boolean performHousekeeping = mSentNotificationsSinceHousekeeping > 0 && mOtherTasksSinceHousekeeping > 1 ;
111
141
mSentNotificationsSinceHousekeeping = 0 ;
112
142
mOtherTasksSinceHousekeeping = 0 ;
113
143
if (!performHousekeeping )
114
144
return ;
115
- Context context = UnityNotificationManager .mUnityNotificationManager .mContext ;
116
145
Set <String > notificationIds = UnityNotificationManager .getScheduledNotificationIDs (context );
117
146
UnityNotificationManager .performNotificationHousekeeping (context , notificationIds );
118
147
}
0 commit comments