@@ -77,13 +77,11 @@ public void run(Context context, Set<String> notificationIds) {
77
77
}
78
78
}
79
79
80
+ private static final int TASKS_FOR_HOUSEKEEPING = 50 ;
80
81
private LinkedTransferQueue <Task > mTasks = new LinkedTransferQueue ();
81
- private int mSentNotificationsSinceHousekeeping = 0 ;
82
- private int mOtherTasksSinceHousekeeping ;
82
+ private int mTasksSinceHousekeeping = TASKS_FOR_HOUSEKEEPING ; // we want hoursekeeping at the start
83
83
84
84
public UnityNotificationBackgroundThread () {
85
- // force housekeeping
86
- mOtherTasksSinceHousekeeping = 2 ;
87
85
enqueueHousekeeping ();
88
86
}
89
87
@@ -111,6 +109,10 @@ public void run() {
111
109
try {
112
110
Task task = mTasks .take ();
113
111
executeTask (context , task , notificationIds );
112
+ if (!(task instanceof HousekeepingTask ))
113
+ ++mTasksSinceHousekeeping ;
114
+ if (mTasks .size () == 0 )
115
+ enqueueHousekeeping ();
114
116
} catch (InterruptedException e ) {
115
117
if (mTasks .isEmpty ())
116
118
break ;
@@ -120,31 +122,17 @@ public void run() {
120
122
121
123
private void executeTask (Context context , Task task , Set <String > notificationIds ) {
122
124
try {
123
- ScheduleNotificationTask scheduleTask = null ;
124
- if (task instanceof ScheduleNotificationTask )
125
- scheduleTask = (ScheduleNotificationTask )task ;
126
-
127
125
task .run (context , notificationIds );
128
-
129
- if (scheduleTask != null )
130
- ++mSentNotificationsSinceHousekeeping ;
131
- else
132
- ++mOtherTasksSinceHousekeeping ;
133
- if ((mSentNotificationsSinceHousekeeping + mOtherTasksSinceHousekeeping ) >= 50 ) {
134
- enqueueHousekeeping ();
135
- }
136
126
} catch (Exception e ) {
137
127
Log .e (TAG_UNITY , "Exception executing notification task" , e );
138
128
}
139
129
}
140
130
141
131
private void performHousekeeping (Context context , Set <String > notificationIds ) {
142
132
// don't do housekeeping if last task we did was housekeeping (other=1)
143
- boolean performHousekeeping = mSentNotificationsSinceHousekeeping > 0 && mOtherTasksSinceHousekeeping > 1 ;
144
- mSentNotificationsSinceHousekeeping = 0 ;
145
- mOtherTasksSinceHousekeeping = 0 ;
146
- if (!performHousekeeping )
147
- return ;
148
- UnityNotificationManager .performNotificationHousekeeping (context , notificationIds );
133
+ boolean performHousekeeping = mTasksSinceHousekeeping >= TASKS_FOR_HOUSEKEEPING ;
134
+ mTasksSinceHousekeeping = 0 ;
135
+ if (performHousekeeping )
136
+ UnityNotificationManager .performNotificationHousekeeping (context , notificationIds );
149
137
}
150
138
}
0 commit comments