@@ -55,8 +55,7 @@ public class UploadService extends HandlerService<Contribution> {
55
55
@ Inject ContributionDao contributionDao ;
56
56
57
57
private NotificationManager notificationManager ;
58
- private NotificationCompat .Builder curProgressNotification ;
59
- private NotificationCompat .Builder curFailedNotification ;
58
+ private NotificationCompat .Builder curNotification ;
60
59
private int toUpload ;
61
60
62
61
/**
@@ -95,19 +94,19 @@ private class NotificationUpdateProgressListener implements MediaWikiApi.Progres
95
94
public void onProgress (long transferred , long total ) {
96
95
Timber .d ("Uploaded %d of %d" , transferred , total );
97
96
if (!notificationTitleChanged ) {
98
- curProgressNotification .setContentTitle (notificationProgressTitle );
97
+ curNotification .setContentTitle (notificationProgressTitle );
99
98
notificationTitleChanged = true ;
100
99
contribution .setState (Contribution .STATE_IN_PROGRESS );
101
100
}
102
101
if (transferred == total ) {
103
102
// Completed!
104
- curProgressNotification .setContentTitle (notificationFinishingTitle )
103
+ curNotification .setContentTitle (notificationFinishingTitle )
105
104
.setTicker (notificationFinishingTitle )
106
105
.setProgress (0 , 100 , true );
107
106
} else {
108
- curProgressNotification .setProgress (100 , (int ) (((double ) transferred / (double ) total ) * 100 ), false );
107
+ curNotification .setProgress (100 , (int ) (((double ) transferred / (double ) total ) * 100 ), false );
109
108
}
110
- notificationManager .notify (NOTIFICATION_UPLOAD_IN_PROGRESS , curProgressNotification .build ());
109
+ notificationManager .notify (NOTIFICATION_UPLOAD_IN_PROGRESS , curNotification .build ());
111
110
112
111
contribution .setTransferred (transferred );
113
112
contributionDao .save (contribution );
@@ -126,8 +125,7 @@ public void onCreate() {
126
125
super .onCreate ();
127
126
CommonsApplication .createNotificationChannel (getApplicationContext ());
128
127
notificationManager = (NotificationManager ) getSystemService (NOTIFICATION_SERVICE );
129
- curProgressNotification = getProgressNotificationBuilder (CommonsApplication .NOTIFICATION_CHANNEL_ID_ALL );
130
- curFailedNotification = getFailedNotificationBuilder (CommonsApplication .NOTIFICATION_CHANNEL_ID_ALL );
128
+ curNotification = getNotificationBuilder (CommonsApplication .NOTIFICATION_CHANNEL_ID_ALL );
131
129
}
132
130
133
131
@ Override
@@ -151,10 +149,10 @@ public void queue(int what, Contribution contribution) {
151
149
contribution .setTransferred (0 );
152
150
contributionDao .save (contribution );
153
151
toUpload ++;
154
- if (curProgressNotification != null && toUpload != 1 ) {
155
- curProgressNotification .setContentText (getResources ().getQuantityString (R .plurals .uploads_pending_notification_indicator , toUpload , toUpload ));
152
+ if (curNotification != null && toUpload != 1 ) {
153
+ curNotification .setContentText (getResources ().getQuantityString (R .plurals .uploads_pending_notification_indicator , toUpload , toUpload ));
156
154
Timber .d ("%d uploads left" , toUpload );
157
- notificationManager .notify (NOTIFICATION_UPLOAD_IN_PROGRESS , curProgressNotification .build ());
155
+ notificationManager .notify (NOTIFICATION_UPLOAD_IN_PROGRESS , curNotification .build ());
158
156
}
159
157
160
158
super .queue (what , contribution );
@@ -184,25 +182,16 @@ public int onStartCommand(Intent intent, int flags, int startId) {
184
182
return START_REDELIVER_INTENT ;
185
183
}
186
184
187
- private NotificationCompat .Builder getProgressNotificationBuilder (String channelId ) {
188
- return getNotificationBuilder (channelId )
189
- .setProgress (100 , 0 , true )
190
- .setOngoing (true );
191
- }
192
-
193
- private NotificationCompat .Builder getFailedNotificationBuilder (String channelId ) {
194
- return getNotificationBuilder (channelId );
195
- }
196
-
197
185
@ SuppressLint ("StringFormatInvalid" )
198
186
private NotificationCompat .Builder getNotificationBuilder (String channelId ) {
199
187
return new NotificationCompat .Builder (this , channelId ).setAutoCancel (true )
200
188
.setSmallIcon (R .drawable .ic_launcher )
201
189
.setLargeIcon (BitmapFactory .decodeResource (getResources (), R .drawable .ic_launcher ))
202
190
.setAutoCancel (true )
203
191
.setOnlyAlertOnce (true )
192
+ .setProgress (100 , 0 , true )
193
+ .setOngoing (true )
204
194
.setContentIntent (PendingIntent .getActivity (this , 0 , new Intent (this , MainActivity .class ), 0 ));
205
-
206
195
}
207
196
208
197
private void uploadContribution (Contribution contribution ) {
@@ -225,10 +214,10 @@ private void uploadContribution(Contribution contribution) {
225
214
}
226
215
227
216
Timber .d ("Before execution!" );
228
- curProgressNotification .setContentTitle (getString (R .string .upload_progress_notification_title_start , contribution .getDisplayTitle ()))
217
+ curNotification .setContentTitle (getString (R .string .upload_progress_notification_title_start , contribution .getDisplayTitle ()))
229
218
.setContentText (getResources ().getQuantityString (R .plurals .uploads_pending_notification_indicator , toUpload , toUpload ))
230
219
.setTicker (getString (R .string .upload_progress_notification_title_in_progress , contribution .getDisplayTitle ()));
231
- startForeground (NOTIFICATION_UPLOAD_IN_PROGRESS , curProgressNotification .build ());
220
+ startForeground (NOTIFICATION_UPLOAD_IN_PROGRESS , curNotification .build ());
232
221
233
222
String filename = contribution .getFilename ();
234
223
try {
@@ -294,10 +283,11 @@ private void uploadContribution(Contribution contribution) {
294
283
@ SuppressLint ("StringFormatInvalid" )
295
284
@ SuppressWarnings ("deprecation" )
296
285
private void showFailedNotification (Contribution contribution ) {
297
- curFailedNotification .setTicker (getString (R .string .upload_failed_notification_title , contribution .getDisplayTitle ()))
286
+ curNotification .setTicker (getString (R .string .upload_failed_notification_title , contribution .getDisplayTitle ()))
298
287
.setContentTitle (getString (R .string .upload_failed_notification_title , contribution .getDisplayTitle ()))
299
- .setContentText (getString (R .string .upload_failed_notification_subtitle ));
300
- notificationManager .notify (NOTIFICATION_UPLOAD_FAILED , curFailedNotification .build ());
288
+ .setContentText (getString (R .string .upload_failed_notification_subtitle ))
289
+ .setProgress (0 , 0 , false );
290
+ notificationManager .notify (NOTIFICATION_UPLOAD_FAILED , curNotification .build ());
301
291
302
292
contribution .setState (Contribution .STATE_FAILED );
303
293
contributionDao .save (contribution );
0 commit comments