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