Skip to content

Commit cce2932

Browse files
committed
Only leave foreground lock when absolutely no more uploads to do
This *might* fix the service dying prematurely in low memory conditions.
1 parent 6a99255 commit cce2932

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

commons/src/main/java/org/wikimedia/commons/UploadService.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,8 @@ public void onProgress(long transferred, long total) {
7777
} else {
7878
curProgressNotification.setProgress(100, (int) (((double) transferred / (double) total) * 100), false);
7979
}
80-
notificationManager.notify(NOTIFICATION_UPLOAD_IN_PROGRESS, curProgressNotification.build());
80+
startForeground(NOTIFICATION_UPLOAD_IN_PROGRESS, curProgressNotification.build());
81+
8182
contribution.setTransferred(transferred);
8283
contribution.save();
8384
}
@@ -125,7 +126,7 @@ public void queue(int what, Contribution contribution) {
125126
if (curProgressNotification != null && toUpload != 1) {
126127
curProgressNotification.setContentText(getResources().getQuantityString(R.plurals.uploads_pending_notification_indicator, toUpload, toUpload));
127128
Log.d("Commons", String.format("%d uploads left", toUpload));
128-
notificationManager.notify(NOTIFICATION_UPLOAD_IN_PROGRESS, curProgressNotification.build());
129+
this.startForeground(NOTIFICATION_UPLOAD_IN_PROGRESS, curProgressNotification.build());
129130
}
130131

131132
super.queue(what, contribution);
@@ -211,10 +212,13 @@ private void uploadContribution(Contribution contribution) {
211212
return;
212213
} finally {
213214
toUpload--;
215+
if(toUpload == 0) {
216+
stopForeground(true);
217+
}
214218
}
215219

216220
Log.d("Commons", "Response is" + Utils.getStringFromDOM(result.getDocument()));
217-
stopForeground(true);
221+
218222
curProgressNotification = null;
219223

220224

@@ -249,7 +253,6 @@ private void uploadContribution(Contribution contribution) {
249253
}
250254

251255
private void showFailedNotification(Contribution contribution) {
252-
stopForeground(true);
253256
Notification failureNotification = new NotificationCompat.Builder(this).setAutoCancel(true)
254257
.setSmallIcon(R.drawable.ic_launcher)
255258
.setAutoCancel(true)

0 commit comments

Comments
 (0)