From cce2932a5a1896900b74ca2005a4326e5fd1ae1c Mon Sep 17 00:00:00 2001 From: YuviPanda Date: Sat, 16 Mar 2013 02:19:19 +0530 Subject: [PATCH] Only leave foreground lock when absolutely no more uploads to do This *might* fix the service dying prematurely in low memory conditions. --- .../java/org/wikimedia/commons/UploadService.java | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/commons/src/main/java/org/wikimedia/commons/UploadService.java b/commons/src/main/java/org/wikimedia/commons/UploadService.java index b20493283..0037a2aa5 100644 --- a/commons/src/main/java/org/wikimedia/commons/UploadService.java +++ b/commons/src/main/java/org/wikimedia/commons/UploadService.java @@ -77,7 +77,8 @@ public class UploadService extends HandlerService { } else { curProgressNotification.setProgress(100, (int) (((double) transferred / (double) total) * 100), false); } - notificationManager.notify(NOTIFICATION_UPLOAD_IN_PROGRESS, curProgressNotification.build()); + startForeground(NOTIFICATION_UPLOAD_IN_PROGRESS, curProgressNotification.build()); + contribution.setTransferred(transferred); contribution.save(); } @@ -125,7 +126,7 @@ public class UploadService extends HandlerService { if (curProgressNotification != null && toUpload != 1) { curProgressNotification.setContentText(getResources().getQuantityString(R.plurals.uploads_pending_notification_indicator, toUpload, toUpload)); Log.d("Commons", String.format("%d uploads left", toUpload)); - notificationManager.notify(NOTIFICATION_UPLOAD_IN_PROGRESS, curProgressNotification.build()); + this.startForeground(NOTIFICATION_UPLOAD_IN_PROGRESS, curProgressNotification.build()); } super.queue(what, contribution); @@ -211,10 +212,13 @@ public class UploadService extends HandlerService { return; } finally { toUpload--; + if(toUpload == 0) { + stopForeground(true); + } } Log.d("Commons", "Response is" + Utils.getStringFromDOM(result.getDocument())); - stopForeground(true); + curProgressNotification = null; @@ -249,7 +253,6 @@ public class UploadService extends HandlerService { } private void showFailedNotification(Contribution contribution) { - stopForeground(true); Notification failureNotification = new NotificationCompat.Builder(this).setAutoCancel(true) .setSmallIcon(R.drawable.ic_launcher) .setAutoCancel(true)