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 ;
34
35
import fr .free .nrw .commons .contributions .ContributionsContentProvider ;
35
36
import fr .free .nrw .commons .contributions .MainActivity ;
36
37
import fr .free .nrw .commons .mwapi .MediaWikiApi ;
37
- import fr .free .nrw .commons .mwapi .UploadResult ;
38
38
import fr .free .nrw .commons .wikidata .WikidataEditService ;
39
- import io .reactivex .android . schedulers . AndroidSchedulers ;
39
+ import io .reactivex .Single ;
40
40
import io .reactivex .schedulers .Schedulers ;
41
41
import timber .log .Timber ;
42
42
@@ -56,7 +56,7 @@ public class UploadService extends HandlerService<Contribution> {
56
56
@ Inject SessionManager sessionManager ;
57
57
@ Inject ContributionDao contributionDao ;
58
58
59
- private NotificationManager notificationManager ;
59
+ private NotificationManagerCompat notificationManager ;
60
60
private NotificationCompat .Builder curNotification ;
61
61
private int toUpload ;
62
62
@@ -108,7 +108,7 @@ public void onProgress(long transferred, long total) {
108
108
} else {
109
109
curNotification .setProgress (100 , (int ) (((double ) transferred / (double ) total ) * 100 ), false );
110
110
}
111
- notificationManager .notify (NOTIFICATION_UPLOAD_IN_PROGRESS , curNotification .build ());
111
+ notificationManager .notify (notificationTag , NOTIFICATION_UPLOAD_IN_PROGRESS , curNotification .build ());
112
112
113
113
contribution .setTransferred (transferred );
114
114
contributionDao .save (contribution );
@@ -126,7 +126,7 @@ public void onDestroy() {
126
126
public void onCreate () {
127
127
super .onCreate ();
128
128
CommonsApplication .createNotificationChannel (getApplicationContext ());
129
- notificationManager = ( NotificationManager ) getSystemService ( NOTIFICATION_SERVICE );
129
+ notificationManager = NotificationManagerCompat . from ( this );
130
130
curNotification = getNotificationBuilder (CommonsApplication .NOTIFICATION_CHANNEL_ID_ALL );
131
131
}
132
132
@@ -154,7 +154,7 @@ public void queue(int what, Contribution contribution) {
154
154
if (curNotification != null && toUpload != 1 ) {
155
155
curNotification .setContentText (getResources ().getQuantityString (R .plurals .uploads_pending_notification_indicator , toUpload , toUpload ));
156
156
Timber .d ("%d uploads left" , toUpload );
157
- notificationManager .notify (NOTIFICATION_UPLOAD_IN_PROGRESS , curNotification .build ());
157
+ notificationManager .notify (contribution . getLocalUri (). toString (), NOTIFICATION_UPLOAD_IN_PROGRESS , curNotification .build ());
158
158
}
159
159
160
160
super .queue (what , contribution );
@@ -219,15 +219,12 @@ private void uploadContribution(Contribution contribution) {
219
219
curNotification .setContentTitle (getString (R .string .upload_progress_notification_title_start , contribution .getDisplayTitle ()))
220
220
.setContentText (getResources ().getQuantityString (R .plurals .uploads_pending_notification_indicator , toUpload , toUpload ))
221
221
.setTicker (getString (R .string .upload_progress_notification_title_in_progress , contribution .getDisplayTitle ()));
222
- startForeground ( NOTIFICATION_UPLOAD_IN_PROGRESS , curNotification .build ());
222
+ notificationManager . notify ( notificationTag , NOTIFICATION_UPLOAD_IN_PROGRESS , curNotification .build ());
223
223
224
224
String filename = contribution .getFilename ();
225
+
225
226
try {
226
- synchronized (unfinishedUploads ) {
227
- Timber .d ("making sure of uniqueness of name: %s" , filename );
228
- filename = findUniqueFilename (filename );
229
- unfinishedUploads .add (filename );
230
- }
227
+
231
228
if (!mwApi .validateLogin ()) {
232
229
// Need to revalidate!
233
230
if (sessionManager .revalidateAuthToken ()) {
@@ -246,17 +243,39 @@ private void uploadContribution(Contribution contribution) {
246
243
getString (R .string .upload_progress_notification_title_finishing , contribution .getDisplayTitle ()),
247
244
contribution
248
245
);
246
+ String stashFilename = "Temp_" + contribution .hashCode () + filename ;
249
247
mwApi .uploadFile (
250
- filename , fileInputStream ,
251
- contribution .getDataLength (), contribution .getPageContents (getApplicationContext ()),
252
- contribution .getEditSummary (), localUri ,
253
- contribution .getContentProviderUri (), notificationUpdater
254
- )
248
+ stashFilename , fileInputStream , contribution .getDataLength (),
249
+ localUri , contribution .getContentProviderUri (), notificationUpdater )
255
250
.subscribeOn (Schedulers .io ())
256
251
.observeOn (Schedulers .io ())
257
- .subscribe ( uploadResult -> {
252
+ .flatMap ( uploadStash -> {
258
253
notificationManager .cancel (NOTIFICATION_UPLOAD_IN_PROGRESS );
259
- Timber .d ("Response is %s" , uploadResult .toString ());
254
+
255
+ Timber .d ("Stash upload response 1 is %s" , uploadStash .toString ());
256
+
257
+ String resultStatus = uploadStash .getResultStatus ();
258
+ if (!resultStatus .equals ("Success" )) {
259
+ Timber .d ("Contribution upload failed. Wikidata entity won't be edited" );
260
+ showFailedNotification (contribution );
261
+ return Single .never ();
262
+ } else {
263
+ synchronized (unfinishedUploads ) {
264
+ Timber .d ("making sure of uniqueness of name: %s" , filename );
265
+ String uniqueFilename = findUniqueFilename (filename );
266
+ unfinishedUploads .add (uniqueFilename );
267
+ return mwApi .uploadFileFinalize (
268
+ uniqueFilename ,
269
+ uploadStash .getFilekey (),
270
+ contribution .getPageContents (getApplicationContext ()),
271
+ contribution .getEditSummary ());
272
+ }
273
+ }
274
+ })
275
+ .subscribe (uploadResult -> {
276
+ Timber .d ("Stash upload response 2 is %s" , uploadResult .toString ());
277
+
278
+ notificationManager .cancel (notificationTag , NOTIFICATION_UPLOAD_IN_PROGRESS );
260
279
261
280
String resultStatus = uploadResult .getResultStatus ();
262
281
if (!resultStatus .equals ("Success" )) {
@@ -274,10 +293,10 @@ private void uploadContribution(Contribution contribution) {
274
293
contributionDao .save (contribution );
275
294
}
276
295
}, throwable -> {
277
-
296
+ throw new RuntimeException ( throwable );
278
297
});
279
298
} catch (IOException e ) {
280
- Timber .d ( "I have a network fuckup " );
299
+ Timber .w ( e , "IOException during upload " );
281
300
notificationManager .cancel (NOTIFICATION_UPLOAD_IN_PROGRESS );
282
301
showFailedNotification (contribution );
283
302
} finally {
@@ -300,7 +319,7 @@ private void showFailedNotification(Contribution contribution) {
300
319
.setContentTitle (getString (R .string .upload_failed_notification_title , contribution .getDisplayTitle ()))
301
320
.setContentText (getString (R .string .upload_failed_notification_subtitle ))
302
321
.setProgress (0 , 0 , false );
303
- notificationManager .notify (NOTIFICATION_UPLOAD_FAILED , curNotification .build ());
322
+ notificationManager .notify (contribution . getLocalUri (). toString (), NOTIFICATION_UPLOAD_FAILED , curNotification .build ());
304
323
305
324
contribution .setState (Contribution .STATE_FAILED );
306
325
contributionDao .save (contribution );
0 commit comments