4
4
import static com .unity .androidnotifications .UnityNotificationManager .TAG_UNITY ;
5
5
6
6
import android .app .Notification ;
7
- import android .content .Context ;
8
7
import android .util .Log ;
9
8
10
9
import java .util .concurrent .ConcurrentHashMap ;
17
16
public class UnityNotificationBackgroundThread extends Thread {
18
17
private static abstract class Task {
19
18
// returns true if notificationIds was modified (needs to be saved)
20
- public abstract boolean run (Context context , ConcurrentHashMap <Integer , Notification .Builder > notifications );
19
+ public abstract boolean run (UnityNotificationManager manager , ConcurrentHashMap <Integer , Notification .Builder > notifications );
21
20
}
22
21
23
22
private static class ScheduleNotificationTask extends Task {
@@ -32,7 +31,7 @@ public ScheduleNotificationTask(int id, Notification.Builder builder, boolean ad
32
31
}
33
32
34
33
@ Override
35
- public boolean run (Context context , ConcurrentHashMap <Integer , Notification .Builder > notifications ) {
34
+ public boolean run (UnityNotificationManager manager , ConcurrentHashMap <Integer , Notification .Builder > notifications ) {
36
35
String id = String .valueOf (notificationId );
37
36
Integer ID = Integer .valueOf (notificationId );
38
37
boolean didSchedule = false ;
@@ -43,8 +42,8 @@ public boolean run(Context context, ConcurrentHashMap<Integer, Notification.Buil
43
42
// if failed to schedule or replace, remove
44
43
if (!didSchedule ) {
45
44
notifications .remove (notificationId );
46
- UnityNotificationManager .cancelPendingNotificationIntent (context , notificationId );
47
- UnityNotificationManager .deleteExpiredNotificationIntent (context , id );
45
+ manager .cancelPendingNotificationIntent (notificationId );
46
+ manager .deleteExpiredNotificationIntent (id );
48
47
}
49
48
}
50
49
@@ -60,10 +59,10 @@ public CancelNotificationTask(int id) {
60
59
}
61
60
62
61
@ Override
63
- public boolean run (Context context , ConcurrentHashMap <Integer , Notification .Builder > notifications ) {
64
- UnityNotificationManager .cancelPendingNotificationIntent (context , notificationId );
62
+ public boolean run (UnityNotificationManager manager , ConcurrentHashMap <Integer , Notification .Builder > notifications ) {
63
+ manager .cancelPendingNotificationIntent (notificationId );
65
64
if (notifications .remove (notificationId ) != null ) {
66
- UnityNotificationManager .deleteExpiredNotificationIntent (context , String .valueOf (notificationId ));
65
+ manager .deleteExpiredNotificationIntent (String .valueOf (notificationId ));
67
66
return true ;
68
67
}
69
68
@@ -73,15 +72,15 @@ public boolean run(Context context, ConcurrentHashMap<Integer, Notification.Buil
73
72
74
73
private static class CancelAllNotificationsTask extends Task {
75
74
@ Override
76
- public boolean run (Context context , ConcurrentHashMap <Integer , Notification .Builder > notifications ) {
75
+ public boolean run (UnityNotificationManager manager , ConcurrentHashMap <Integer , Notification .Builder > notifications ) {
77
76
if (notifications .isEmpty ())
78
77
return false ;
79
78
80
79
Enumeration <Integer > ids = notifications .keys ();
81
80
while (ids .hasMoreElements ()) {
82
81
Integer notificationId = ids .nextElement ();
83
- UnityNotificationManager .cancelPendingNotificationIntent (context , notificationId );
84
- UnityNotificationManager .deleteExpiredNotificationIntent (context , String .valueOf (notificationId ));
82
+ manager .cancelPendingNotificationIntent (notificationId );
83
+ manager .deleteExpiredNotificationIntent (String .valueOf (notificationId ));
85
84
}
86
85
87
86
notifications .clear ();
@@ -97,25 +96,25 @@ public HousekeepingTask(UnityNotificationBackgroundThread th) {
97
96
}
98
97
99
98
@ Override
100
- public boolean run (Context context , ConcurrentHashMap <Integer , Notification .Builder > notifications ) {
99
+ public boolean run (UnityNotificationManager manager , ConcurrentHashMap <Integer , Notification .Builder > notifications ) {
101
100
HashSet <String > notificationIds = new HashSet <>();
102
101
Enumeration <Integer > ids = notifications .keys ();
103
102
while (ids .hasMoreElements ()) {
104
103
notificationIds .add (String .valueOf (ids .nextElement ()));
105
104
}
106
- thread .performHousekeeping (context , notificationIds );
105
+ thread .performHousekeeping (notificationIds );
107
106
return false ;
108
107
}
109
108
}
110
109
111
110
private static final int TASKS_FOR_HOUSEKEEPING = 50 ;
112
111
private LinkedTransferQueue <Task > mTasks = new LinkedTransferQueue ();
113
112
private ConcurrentHashMap <Integer , Notification .Builder > mScheduledNotifications ;
114
- private static Context mContext ;
113
+ private UnityNotificationManager mManager ;
115
114
private int mTasksSinceHousekeeping = TASKS_FOR_HOUSEKEEPING ; // we want hoursekeeping at the start
116
115
117
- public UnityNotificationBackgroundThread (Context context , ConcurrentHashMap <Integer , Notification .Builder > scheduledNotifications ) {
118
- mContext = context ;
116
+ public UnityNotificationBackgroundThread (UnityNotificationManager manager , ConcurrentHashMap <Integer , Notification .Builder > scheduledNotifications ) {
117
+ mManager = manager ;
119
118
mScheduledNotifications = scheduledNotifications ;
120
119
// rescheduling after reboot may have loaded, otherwise load here
121
120
if (mScheduledNotifications .size () == 0 )
@@ -144,7 +143,7 @@ public void run() {
144
143
while (true ) {
145
144
try {
146
145
Task task = mTasks .take ();
147
- haveChanges |= executeTask (mContext , task , mScheduledNotifications );
146
+ haveChanges |= executeTask (mManager , task , mScheduledNotifications );
148
147
if (!(task instanceof HousekeepingTask ))
149
148
++mTasksSinceHousekeeping ;
150
149
if (mTasks .size () == 0 && haveChanges ) {
@@ -158,26 +157,26 @@ public void run() {
158
157
}
159
158
}
160
159
161
- private boolean executeTask (Context context , Task task , ConcurrentHashMap <Integer , Notification .Builder > notifications ) {
160
+ private boolean executeTask (UnityNotificationManager manager , Task task , ConcurrentHashMap <Integer , Notification .Builder > notifications ) {
162
161
try {
163
- return task .run (context , notifications );
162
+ return task .run (manager , notifications );
164
163
} catch (Exception e ) {
165
164
Log .e (TAG_UNITY , "Exception executing notification task" , e );
166
165
return false ;
167
166
}
168
167
}
169
168
170
- private void performHousekeeping (Context context , Set <String > notificationIds ) {
169
+ private void performHousekeeping (Set <String > notificationIds ) {
171
170
// don't do housekeeping if last task we did was housekeeping (other=1)
172
171
boolean performHousekeeping = mTasksSinceHousekeeping >= TASKS_FOR_HOUSEKEEPING ;
173
172
mTasksSinceHousekeeping = 0 ;
174
173
if (performHousekeeping )
175
- UnityNotificationManager .performNotificationHousekeeping (context , notificationIds );
176
- UnityNotificationManager .saveScheduledNotificationIDs (context , notificationIds );
174
+ mManager .performNotificationHousekeeping (notificationIds );
175
+ mManager .saveScheduledNotificationIDs (notificationIds );
177
176
}
178
177
179
178
private void loadNotifications () {
180
- List <Notification .Builder > notifications = UnityNotificationManager .loadSavedNotifications (mContext );
179
+ List <Notification .Builder > notifications = mManager .loadSavedNotifications ();
181
180
for (Notification .Builder builder : notifications ) {
182
181
int id = builder .getExtras ().getInt (KEY_ID , -1 );
183
182
mScheduledNotifications .put (id , builder );
0 commit comments