Skip to content

Commit 1c1b57a

Browse files
committed
Use NotificationManagerCompat; add notification tag; assign temporaty stash file name
1 parent 1a304e7 commit 1c1b57a

File tree

1 file changed

+15
-11
lines changed

1 file changed

+15
-11
lines changed

app/src/main/java/fr/free/nrw/commons/upload/UploadService.java

+15-11
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import android.net.Uri;
1111
import android.os.Bundle;
1212
import android.support.v4.app.NotificationCompat;
13+
import android.support.v4.app.NotificationManagerCompat;
1314
import android.widget.Toast;
1415

1516
import java.io.File;
@@ -55,7 +56,7 @@ public class UploadService extends HandlerService<Contribution> {
5556
@Inject SessionManager sessionManager;
5657
@Inject ContributionDao contributionDao;
5758

58-
private NotificationManager notificationManager;
59+
private NotificationManagerCompat notificationManager;
5960
private NotificationCompat.Builder curProgressNotification;
6061
private NotificationCompat.Builder curFailedNotification;
6162
private int toUpload;
@@ -108,7 +109,7 @@ public void onProgress(long transferred, long total) {
108109
} else {
109110
curProgressNotification.setProgress(100, (int) (((double) transferred / (double) total) * 100), false);
110111
}
111-
notificationManager.notify(NOTIFICATION_UPLOAD_IN_PROGRESS, curProgressNotification.build());
112+
notificationManager.notify(notificationTag, NOTIFICATION_UPLOAD_IN_PROGRESS, curProgressNotification.build());
112113

113114
contribution.setTransferred(transferred);
114115
contributionDao.save(contribution);
@@ -126,7 +127,7 @@ public void onDestroy() {
126127
public void onCreate() {
127128
super.onCreate();
128129
CommonsApplication.createNotificationChannel(getApplicationContext());
129-
notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
130+
notificationManager = NotificationManagerCompat.from(this);
130131
curProgressNotification = getProgressNotificationBuilder(CommonsApplication.NOTIFICATION_CHANNEL_ID_ALL);
131132
curFailedNotification = getFailedNotificationBuilder(CommonsApplication.NOTIFICATION_CHANNEL_ID_ALL);
132133
}
@@ -155,7 +156,7 @@ public void queue(int what, Contribution contribution) {
155156
if (curProgressNotification != null && toUpload != 1) {
156157
curProgressNotification.setContentText(getResources().getQuantityString(R.plurals.uploads_pending_notification_indicator, toUpload, toUpload));
157158
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());
159160
}
160161

161162
super.queue(what, contribution);
@@ -229,9 +230,10 @@ private void uploadContribution(Contribution contribution) {
229230
curProgressNotification.setContentTitle(getString(R.string.upload_progress_notification_title_start, contribution.getDisplayTitle()))
230231
.setContentText(getResources().getQuantityString(R.plurals.uploads_pending_notification_indicator, toUpload, toUpload))
231232
.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());
233234

234235
String filename = contribution.getFilename();
236+
235237
try {
236238

237239
if (!mwApi.validateLogin()) {
@@ -252,17 +254,16 @@ private void uploadContribution(Contribution contribution) {
252254
getString(R.string.upload_progress_notification_title_finishing, contribution.getDisplayTitle()),
253255
contribution
254256
);
257+
String stashFilename = filename + contribution.hashCode();
255258
mwApi.uploadFile(
256-
filename, fileInputStream, contribution.getDataLength(),
259+
stashFilename, fileInputStream, contribution.getDataLength(),
257260
localUri, contribution.getContentProviderUri(), notificationUpdater)
258261
.subscribeOn(Schedulers.io())
259262
.observeOn(Schedulers.io())
260263
.flatMap(uploadStash -> {
261264
notificationManager.cancel(NOTIFICATION_UPLOAD_IN_PROGRESS);
262265

263-
Timber.d("Response 1 is %s", uploadStash.toString());
264-
265-
curProgressNotification = null;
266+
Timber.d("Stash upload response 1 is %s", uploadStash.toString());
266267

267268
String resultStatus = uploadStash.getResultStatus();
268269
if (!resultStatus.equals("Success")) {
@@ -283,7 +284,10 @@ private void uploadContribution(Contribution contribution) {
283284
}
284285
})
285286
.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;
287291

288292
String resultStatus = uploadResult.getResultStatus();
289293
if (!resultStatus.equals("Success")) {
@@ -326,7 +330,7 @@ private void showFailedNotification(Contribution contribution) {
326330
curFailedNotification.setTicker(getString(R.string.upload_failed_notification_title, contribution.getDisplayTitle()))
327331
.setContentTitle(getString(R.string.upload_failed_notification_title, contribution.getDisplayTitle()))
328332
.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());
330334

331335
contribution.setState(Contribution.STATE_FAILED);
332336
contributionDao.save(contribution);

0 commit comments

Comments
 (0)