10
10
import android .net .Uri ;
11
11
import android .os .Bundle ;
12
12
import android .support .v4 .app .NotificationCompat ;
13
+ import android .support .v4 .app .NotificationManagerCompat ;
13
14
import android .widget .Toast ;
14
15
15
16
import java .io .File ;
@@ -55,7 +56,7 @@ public class UploadService extends HandlerService<Contribution> {
55
56
@ Inject SessionManager sessionManager ;
56
57
@ Inject ContributionDao contributionDao ;
57
58
58
- private NotificationManager notificationManager ;
59
+ private NotificationManagerCompat notificationManager ;
59
60
private NotificationCompat .Builder curProgressNotification ;
60
61
private NotificationCompat .Builder curFailedNotification ;
61
62
private int toUpload ;
@@ -108,7 +109,7 @@ public void onProgress(long transferred, long total) {
108
109
} else {
109
110
curProgressNotification .setProgress (100 , (int ) (((double ) transferred / (double ) total ) * 100 ), false );
110
111
}
111
- notificationManager .notify (NOTIFICATION_UPLOAD_IN_PROGRESS , curProgressNotification .build ());
112
+ notificationManager .notify (notificationTag , NOTIFICATION_UPLOAD_IN_PROGRESS , curProgressNotification .build ());
112
113
113
114
contribution .setTransferred (transferred );
114
115
contributionDao .save (contribution );
@@ -126,7 +127,7 @@ public void onDestroy() {
126
127
public void onCreate () {
127
128
super .onCreate ();
128
129
CommonsApplication .createNotificationChannel (getApplicationContext ());
129
- notificationManager = ( NotificationManager ) getSystemService ( NOTIFICATION_SERVICE );
130
+ notificationManager = NotificationManagerCompat . from ( this );
130
131
curProgressNotification = getProgressNotificationBuilder (CommonsApplication .NOTIFICATION_CHANNEL_ID_ALL );
131
132
curFailedNotification = getFailedNotificationBuilder (CommonsApplication .NOTIFICATION_CHANNEL_ID_ALL );
132
133
}
@@ -155,7 +156,7 @@ public void queue(int what, Contribution contribution) {
155
156
if (curProgressNotification != null && toUpload != 1 ) {
156
157
curProgressNotification .setContentText (getResources ().getQuantityString (R .plurals .uploads_pending_notification_indicator , toUpload , toUpload ));
157
158
Timber .d ("%d uploads left" , toUpload );
158
- notificationManager .notify (NOTIFICATION_UPLOAD_IN_PROGRESS , curProgressNotification .build ());
159
+ notificationManager .notify (contribution . getLocalUri (). toString (), NOTIFICATION_UPLOAD_IN_PROGRESS , curProgressNotification .build ());
159
160
}
160
161
161
162
super .queue (what , contribution );
@@ -229,9 +230,10 @@ private void uploadContribution(Contribution contribution) {
229
230
curProgressNotification .setContentTitle (getString (R .string .upload_progress_notification_title_start , contribution .getDisplayTitle ()))
230
231
.setContentText (getResources ().getQuantityString (R .plurals .uploads_pending_notification_indicator , toUpload , toUpload ))
231
232
.setTicker (getString (R .string .upload_progress_notification_title_in_progress , contribution .getDisplayTitle ()));
232
- startForeground ( NOTIFICATION_UPLOAD_IN_PROGRESS , curProgressNotification .build ());
233
+ notificationManager . notify ( notificationTag , NOTIFICATION_UPLOAD_IN_PROGRESS , curProgressNotification .build ());
233
234
234
235
String filename = contribution .getFilename ();
236
+
235
237
try {
236
238
237
239
if (!mwApi .validateLogin ()) {
@@ -252,17 +254,16 @@ private void uploadContribution(Contribution contribution) {
252
254
getString (R .string .upload_progress_notification_title_finishing , contribution .getDisplayTitle ()),
253
255
contribution
254
256
);
257
+ String stashFilename = filename + contribution .hashCode ();
255
258
mwApi .uploadFile (
256
- filename , fileInputStream , contribution .getDataLength (),
259
+ stashFilename , fileInputStream , contribution .getDataLength (),
257
260
localUri , contribution .getContentProviderUri (), notificationUpdater )
258
261
.subscribeOn (Schedulers .io ())
259
262
.observeOn (Schedulers .io ())
260
263
.flatMap (uploadStash -> {
261
264
notificationManager .cancel (NOTIFICATION_UPLOAD_IN_PROGRESS );
262
265
263
- Timber .d ("Response 1 is %s" , uploadStash .toString ());
264
-
265
- curProgressNotification = null ;
266
+ Timber .d ("Stash upload response 1 is %s" , uploadStash .toString ());
266
267
267
268
String resultStatus = uploadStash .getResultStatus ();
268
269
if (!resultStatus .equals ("Success" )) {
@@ -283,7 +284,10 @@ private void uploadContribution(Contribution contribution) {
283
284
}
284
285
})
285
286
.subscribe (uploadResult -> {
286
- Timber .d ("Response 2 is %s" , uploadResult .toString ());
287
+ Timber .d ("Stash upload response 2 is %s" , uploadResult .toString ());
288
+
289
+ notificationManager .cancel (notificationTag , NOTIFICATION_UPLOAD_IN_PROGRESS );
290
+ curProgressNotification = null ;
287
291
288
292
String resultStatus = uploadResult .getResultStatus ();
289
293
if (!resultStatus .equals ("Success" )) {
@@ -326,7 +330,7 @@ private void showFailedNotification(Contribution contribution) {
326
330
curFailedNotification .setTicker (getString (R .string .upload_failed_notification_title , contribution .getDisplayTitle ()))
327
331
.setContentTitle (getString (R .string .upload_failed_notification_title , contribution .getDisplayTitle ()))
328
332
.setContentText (getString (R .string .upload_failed_notification_subtitle ));
329
- notificationManager .notify (NOTIFICATION_UPLOAD_FAILED , curFailedNotification .build ());
333
+ notificationManager .notify (contribution . getLocalUri (). toString (), NOTIFICATION_UPLOAD_FAILED , curFailedNotification .build ());
330
334
331
335
contribution .setState (Contribution .STATE_FAILED );
332
336
contributionDao .save (contribution );
0 commit comments