More accurate feedback about current status of upload!

This commit is contained in:
YuviPanda 2012-10-26 02:44:35 +05:30
parent 034fde1007
commit eb8edb2f06
2 changed files with 29 additions and 3 deletions

View file

@ -23,4 +23,7 @@
<string name="upload_completed_notification_title">%1$s uploaded!</string> <string name="upload_completed_notification_title">%1$s uploaded!</string>
<string name="upload_completed_notification_text">Tap to view your upload</string> <string name="upload_completed_notification_text">Tap to view your upload</string>
<string name="upload_progress_notification_title_start">Starting %1$s upload</string>
<string name="upload_progress_notification_title_in_progress">%1$s uploading</string>
<string name="upload_progress_notification_title_finishing">Finishing uploading %1$s</string>
</resources> </resources>

View file

@ -39,19 +39,38 @@ public class UploadService extends IntentService {
Notification curNotification; Notification curNotification;
String notificationTag; String notificationTag;
boolean notificationTitleChanged;
String notificationProgressTitle;
String notificationFinishingTitle;
private int lastPercent = 0; private int lastPercent = 0;
public NotificationUpdateProgressListener(Notification curNotification, String notificationTag) {
public NotificationUpdateProgressListener(Notification curNotification, String notificationTag, String notificationProgressTitle, String notificationFinishingTitle) {
this.curNotification = curNotification; this.curNotification = curNotification;
this.notificationTag = notificationTag; this.notificationTag = notificationTag;
this.notificationProgressTitle = notificationProgressTitle;
this.notificationFinishingTitle = notificationFinishingTitle;
} }
@Override @Override
public void onProgress(long transferred, long total) { public void onProgress(long transferred, long total) {
RemoteViews curView = curNotification.contentView;
if(!notificationTitleChanged) {
curView.setTextViewText(R.id.uploadNotificationTitle, notificationProgressTitle);
notificationTitleChanged = false;
notificationManager.notify(notificationTag, 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); notificationManager.notify(notificationTag, NOTIFICATION_DOWNLOAD_IN_PROGRESS, curNotification);
lastPercent = percent; lastPercent = percent;
} }
if(percent == 100) {
// Completed!
curView.setTextViewText(R.id.uploadNotificationTitle, notificationFinishingTitle);
notificationManager.notify(notificationTag, NOTIFICATION_DOWNLOAD_IN_PROGRESS, curNotification);
}
} }
} }
@ -92,7 +111,7 @@ public class UploadService extends IntentService {
} }
notificationView = new RemoteViews(getPackageName(), R.layout.layout_upload_progress); notificationView = new RemoteViews(getPackageName(), R.layout.layout_upload_progress);
notificationView.setTextViewText(R.id.uploadNotificationTitle, "Uploading " + filename); notificationView.setTextViewText(R.id.uploadNotificationTitle, String.format(getString(R.string.upload_progress_notification_title_start), filename));
notificationView.setProgressBar(R.id.uploadNotificationProgress, 100, 0, false); notificationView.setProgressBar(R.id.uploadNotificationProgress, 100, 0, false);
Log.d("Commons", "Before execution!"); Log.d("Commons", "Before execution!");
@ -106,8 +125,12 @@ public class UploadService extends IntentService {
notificationManager.notify(notificationTag, NOTIFICATION_DOWNLOAD_IN_PROGRESS, progressNotification); notificationManager.notify(notificationTag, NOTIFICATION_DOWNLOAD_IN_PROGRESS, progressNotification);
Log.d("Commons", "Just before"); Log.d("Commons", "Just before");
NotificationUpdateProgressListener notificationUpdater = new NotificationUpdateProgressListener(progressNotification, notificationTag,
String.format(getString(R.string.upload_progress_notification_title_in_progress), filename),
String.format(getString(R.string.upload_progress_notification_title_finishing), filename)
);
try { try {
result = api.upload(filename, file, length, pageContents, editSummary, new NotificationUpdateProgressListener(progressNotification, notificationTag)); result = api.upload(filename, file, length, pageContents, editSummary, notificationUpdater);
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
throw new RuntimeException(e); throw new RuntimeException(e);