From da723d05ed02f908371dadedb83e2f1dd7cf90cd Mon Sep 17 00:00:00 2001 From: YuviPanda Date: Sun, 4 Nov 2012 15:53:48 +0530 Subject: [PATCH] Properly set uploader service as foreground so it isn't killed Commit sitting in a corner of the road near SICSR in a temple --- src/org/wikimedia/commons/UploadService.java | 22 ++++++++++++-------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/src/org/wikimedia/commons/UploadService.java b/src/org/wikimedia/commons/UploadService.java index 2cab582f1..cad3e36e3 100644 --- a/src/org/wikimedia/commons/UploadService.java +++ b/src/org/wikimedia/commons/UploadService.java @@ -39,8 +39,11 @@ public class UploadService extends IntentService { public UploadService() { super("UploadService"); } - public static final int NOTIFICATION_DOWNLOAD_IN_PROGRESS = 0; - public static final int NOTIFICATION_DOWNLOAD_COMPLETE = 1; + // DO NOT HAVE NOTIFICATION ID OF 0 FOR ANYTHING + // See http://stackoverflow.com/questions/8725909/startforeground-does-not-show-my-notification + // Seriously, Android? + public static final int NOTIFICATION_DOWNLOAD_IN_PROGRESS = 1; + public static final int NOTIFICATION_DOWNLOAD_COMPLETE = 2; private class NotificationUpdateProgressListener implements ProgressListener { @@ -65,18 +68,18 @@ public class UploadService extends IntentService { if(!notificationTitleChanged) { curView.setTextViewText(R.id.uploadNotificationTitle, notificationProgressTitle); notificationTitleChanged = false; - notificationManager.notify(notificationTag, NOTIFICATION_DOWNLOAD_IN_PROGRESS, curNotification); + startForeground(NOTIFICATION_DOWNLOAD_IN_PROGRESS, curNotification); } int percent =(int) ((double)transferred / (double)total * 100); if(percent > lastPercent) { curNotification.contentView.setProgressBar(R.id.uploadNotificationProgress, 100, percent, false); - notificationManager.notify(notificationTag, NOTIFICATION_DOWNLOAD_IN_PROGRESS, curNotification); + startForeground(NOTIFICATION_DOWNLOAD_IN_PROGRESS, curNotification); lastPercent = percent; } if(percent == 100) { // Completed! curView.setTextViewText(R.id.uploadNotificationTitle, notificationFinishingTitle); - notificationManager.notify(notificationTag, NOTIFICATION_DOWNLOAD_IN_PROGRESS, curNotification); + startForeground(NOTIFICATION_DOWNLOAD_IN_PROGRESS, curNotification); } } @@ -133,11 +136,11 @@ public class UploadService extends IntentService { .setSmallIcon(R.drawable.ic_launcher) .setAutoCancel(true) .setContent(notificationView) + .setOngoing(true) .setContentIntent(PendingIntent.getActivity(getApplicationContext(), 0, new Intent(), 0)) .getNotification(); - notificationManager.notify(notificationTag, NOTIFICATION_DOWNLOAD_IN_PROGRESS, progressNotification); - + this.startForeground(NOTIFICATION_DOWNLOAD_IN_PROGRESS, progressNotification); Log.d("Commons", "Just before"); NotificationUpdateProgressListener notificationUpdater = new NotificationUpdateProgressListener(progressNotification, notificationTag, @@ -162,11 +165,12 @@ public class UploadService extends IntentService { result = api.upload(filename, file, length, media.getPageContents(), editSummary, notificationUpdater); } catch (IOException e) { e.printStackTrace(); + Log.d("Commons", "I have a network fuckup"); throw new RuntimeException(e); } - Log.d("Commons", app.getStringFromDOM(result.getDocument())); - notificationManager.cancel(notificationTag, NOTIFICATION_DOWNLOAD_IN_PROGRESS); + Log.d("Commons", "Response is" + CommonsApplication.getStringFromDOM(result.getDocument())); + stopForeground(true); String descUrl = result.getString("/api/upload/imageinfo/@descriptionurl");