1
1
package fr .free .nrw .commons .upload ;
2
2
3
3
import android .annotation .SuppressLint ;
4
- import android .app .Notification ;
5
4
import android .app .NotificationManager ;
6
5
import android .app .PendingIntent ;
7
6
import android .content .ContentResolver ;
@@ -57,6 +56,7 @@ public class UploadService extends HandlerService<Contribution> {
57
56
58
57
private NotificationManager notificationManager ;
59
58
private NotificationCompat .Builder curProgressNotification ;
59
+ private NotificationCompat .Builder curFailedNotification ;
60
60
private int toUpload ;
61
61
62
62
/**
@@ -101,12 +101,13 @@ public void onProgress(long transferred, long total) {
101
101
}
102
102
if (transferred == total ) {
103
103
// Completed!
104
- curProgressNotification .setContentTitle (notificationFinishingTitle );
105
- curProgressNotification .setProgress (0 , 100 , true );
104
+ curProgressNotification .setContentTitle (notificationFinishingTitle )
105
+ .setTicker (notificationFinishingTitle )
106
+ .setProgress (0 , 100 , true );
106
107
} else {
107
108
curProgressNotification .setProgress (100 , (int ) (((double ) transferred / (double ) total ) * 100 ), false );
108
109
}
109
- startForeground (NOTIFICATION_UPLOAD_IN_PROGRESS , curProgressNotification .build ());
110
+ notificationManager . notify (NOTIFICATION_UPLOAD_IN_PROGRESS , curProgressNotification .build ());
110
111
111
112
contribution .setTransferred (transferred );
112
113
contributionDao .save (contribution );
@@ -125,6 +126,8 @@ public void onCreate() {
125
126
super .onCreate ();
126
127
CommonsApplication .createNotificationChannel (getApplicationContext ());
127
128
notificationManager = (NotificationManager ) getSystemService (NOTIFICATION_SERVICE );
129
+ curProgressNotification = getProgressNotificationBuilder (CommonsApplication .NOTIFICATION_CHANNEL_ID_ALL );
130
+ curFailedNotification = getFailedNotificationBuilder (CommonsApplication .NOTIFICATION_CHANNEL_ID_ALL );
128
131
}
129
132
130
133
@ Override
@@ -151,7 +154,7 @@ public void queue(int what, Contribution contribution) {
151
154
if (curProgressNotification != null && toUpload != 1 ) {
152
155
curProgressNotification .setContentText (getResources ().getQuantityString (R .plurals .uploads_pending_notification_indicator , toUpload , toUpload ));
153
156
Timber .d ("%d uploads left" , toUpload );
154
- this . startForeground (NOTIFICATION_UPLOAD_IN_PROGRESS , curProgressNotification .build ());
157
+ notificationManager . notify (NOTIFICATION_UPLOAD_IN_PROGRESS , curProgressNotification .build ());
155
158
}
156
159
157
160
super .queue (what , contribution );
@@ -181,18 +184,25 @@ public int onStartCommand(Intent intent, int flags, int startId) {
181
184
return START_REDELIVER_INTENT ;
182
185
}
183
186
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
+
184
197
@ SuppressLint ("StringFormatInvalid" )
185
- private NotificationCompat .Builder getNotificationBuilder (Contribution contribution , String channelId ) {
198
+ private NotificationCompat .Builder getNotificationBuilder (String channelId ) {
186
199
return new NotificationCompat .Builder (this , channelId ).setAutoCancel (true )
187
200
.setSmallIcon (R .drawable .ic_launcher )
188
201
.setLargeIcon (BitmapFactory .decodeResource (getResources (), R .drawable .ic_launcher ))
189
202
.setAutoCancel (true )
190
- .setContentTitle (getString (R .string .upload_progress_notification_title_start , contribution .getDisplayTitle ()))
191
- .setContentText (getResources ().getQuantityString (R .plurals .uploads_pending_notification_indicator , toUpload , toUpload ))
192
- .setOngoing (true )
193
- .setProgress (100 , 0 , true )
194
- .setContentIntent (PendingIntent .getActivity (this , 0 , new Intent (this , MainActivity .class ), 0 ))
195
- .setTicker (getString (R .string .upload_progress_notification_title_in_progress , contribution .getDisplayTitle ()));
203
+ .setOnlyAlertOnce (true )
204
+ .setContentIntent (PendingIntent .getActivity (this , 0 , new Intent (this , MainActivity .class ), 0 ));
205
+
196
206
}
197
207
198
208
private void uploadContribution (Contribution contribution ) {
@@ -215,10 +225,10 @@ private void uploadContribution(Contribution contribution) {
215
225
}
216
226
217
227
Timber .d ("Before execution!" );
218
- curProgressNotification = getNotificationBuilder (
219
- contribution ,
220
- CommonsApplication . NOTIFICATION_CHANNEL_ID_ALL );
221
- this . startForeground (NOTIFICATION_UPLOAD_IN_PROGRESS , curProgressNotification .build ());
228
+ curProgressNotification . setContentTitle ( getString ( R . string . upload_progress_notification_title_start , contribution . getDisplayTitle ()))
229
+ . setContentText ( getResources (). getQuantityString ( R . plurals . uploads_pending_notification_indicator , toUpload , toUpload ))
230
+ . setTicker ( getString ( R . string . upload_progress_notification_title_in_progress , contribution . getDisplayTitle ()) );
231
+ startForeground (NOTIFICATION_UPLOAD_IN_PROGRESS , curProgressNotification .build ());
222
232
223
233
String filename = contribution .getFilename ();
224
234
try {
@@ -248,10 +258,9 @@ private void uploadContribution(Contribution contribution) {
248
258
UploadResult uploadResult = mwApi .uploadFile (filename , fileInputStream , contribution .getDataLength (),
249
259
contribution .getPageContents (getApplicationContext ()), contribution .getEditSummary (), localUri , contribution .getContentProviderUri (), notificationUpdater );
250
260
261
+ notificationManager .cancel (NOTIFICATION_UPLOAD_IN_PROGRESS );
251
262
Timber .d ("Response is %s" , uploadResult .toString ());
252
263
253
- curProgressNotification = null ;
254
-
255
264
String resultStatus = uploadResult .getResultStatus ();
256
265
if (!resultStatus .equals ("Success" )) {
257
266
Timber .d ("Contribution upload failed. Wikidata entity won't be edited" );
@@ -267,6 +276,7 @@ private void uploadContribution(Contribution contribution) {
267
276
}
268
277
} catch (IOException e ) {
269
278
Timber .d ("I have a network fuckup" );
279
+ notificationManager .cancel (NOTIFICATION_UPLOAD_IN_PROGRESS );
270
280
showFailedNotification (contribution );
271
281
} finally {
272
282
if (filename != null ) {
@@ -284,15 +294,10 @@ private void uploadContribution(Contribution contribution) {
284
294
@ SuppressLint ("StringFormatInvalid" )
285
295
@ SuppressWarnings ("deprecation" )
286
296
private void showFailedNotification (Contribution contribution ) {
287
- Notification failureNotification = new NotificationCompat .Builder (this , CommonsApplication .NOTIFICATION_CHANNEL_ID_ALL ).setAutoCancel (true )
288
- .setSmallIcon (R .drawable .ic_launcher )
289
- .setAutoCancel (true )
290
- .setContentIntent (PendingIntent .getActivity (this , 0 , new Intent (this , MainActivity .class ), 0 ))
291
- .setTicker (getString (R .string .upload_failed_notification_title , contribution .getDisplayTitle ()))
297
+ curFailedNotification .setTicker (getString (R .string .upload_failed_notification_title , contribution .getDisplayTitle ()))
292
298
.setContentTitle (getString (R .string .upload_failed_notification_title , contribution .getDisplayTitle ()))
293
- .setContentText (getString (R .string .upload_failed_notification_subtitle ))
294
- .build ();
295
- notificationManager .notify (NOTIFICATION_UPLOAD_FAILED , failureNotification );
299
+ .setContentText (getString (R .string .upload_failed_notification_subtitle ));
300
+ notificationManager .notify (NOTIFICATION_UPLOAD_FAILED , curFailedNotification .build ());
296
301
297
302
contribution .setState (Contribution .STATE_FAILED );
298
303
contributionDao .save (contribution );
0 commit comments