Properly set uploader service as foreground so it isn't killed

Commit sitting in a corner of the road near SICSR in a temple
This commit is contained in:
YuviPanda 2012-11-04 15:53:48 +05:30
parent 88cb714b02
commit da723d05ed

View file

@ -39,8 +39,11 @@ public class UploadService extends IntentService {
public UploadService() { public UploadService() {
super("UploadService"); super("UploadService");
} }
public static final int NOTIFICATION_DOWNLOAD_IN_PROGRESS = 0; // DO NOT HAVE NOTIFICATION ID OF 0 FOR ANYTHING
public static final int NOTIFICATION_DOWNLOAD_COMPLETE = 1; // 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 { private class NotificationUpdateProgressListener implements ProgressListener {
@ -65,18 +68,18 @@ public class UploadService extends IntentService {
if(!notificationTitleChanged) { if(!notificationTitleChanged) {
curView.setTextViewText(R.id.uploadNotificationTitle, notificationProgressTitle); curView.setTextViewText(R.id.uploadNotificationTitle, notificationProgressTitle);
notificationTitleChanged = false; notificationTitleChanged = false;
notificationManager.notify(notificationTag, NOTIFICATION_DOWNLOAD_IN_PROGRESS, curNotification); startForeground(NOTIFICATION_DOWNLOAD_IN_PROGRESS, curNotification);
} }
int percent =(int) ((double)transferred / (double)total * 100); int percent =(int) ((double)transferred / (double)total * 100);
if(percent > lastPercent) { if(percent > lastPercent) {
curNotification.contentView.setProgressBar(R.id.uploadNotificationProgress, 100, percent, false); 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; lastPercent = percent;
} }
if(percent == 100) { if(percent == 100) {
// Completed! // Completed!
curView.setTextViewText(R.id.uploadNotificationTitle, notificationFinishingTitle); 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) .setSmallIcon(R.drawable.ic_launcher)
.setAutoCancel(true) .setAutoCancel(true)
.setContent(notificationView) .setContent(notificationView)
.setOngoing(true)
.setContentIntent(PendingIntent.getActivity(getApplicationContext(), 0, new Intent(), 0)) .setContentIntent(PendingIntent.getActivity(getApplicationContext(), 0, new Intent(), 0))
.getNotification(); .getNotification();
notificationManager.notify(notificationTag, NOTIFICATION_DOWNLOAD_IN_PROGRESS, progressNotification); this.startForeground(NOTIFICATION_DOWNLOAD_IN_PROGRESS, progressNotification);
Log.d("Commons", "Just before"); Log.d("Commons", "Just before");
NotificationUpdateProgressListener notificationUpdater = new NotificationUpdateProgressListener(progressNotification, notificationTag, 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); result = api.upload(filename, file, length, media.getPageContents(), editSummary, notificationUpdater);
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
Log.d("Commons", "I have a network fuckup");
throw new RuntimeException(e); throw new RuntimeException(e);
} }
Log.d("Commons", app.getStringFromDOM(result.getDocument())); Log.d("Commons", "Response is" + CommonsApplication.getStringFromDOM(result.getDocument()));
notificationManager.cancel(notificationTag, NOTIFICATION_DOWNLOAD_IN_PROGRESS); stopForeground(true);
String descUrl = result.getString("/api/upload/imageinfo/@descriptionurl"); String descUrl = result.getString("/api/upload/imageinfo/@descriptionurl");