Handle network failures gracefully

This commit is contained in:
YuviPanda 2012-11-09 17:49:15 +05:30
parent f1ef106c8a
commit efecef0a6c
2 changed files with 15 additions and 3 deletions

View file

@ -26,4 +26,6 @@
<string name="upload_progress_notification_title_start">Starting %1$s 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_in_progress">%1$s uploading</string>
<string name="upload_progress_notification_title_finishing">Finishing uploading %1$s</string> <string name="upload_progress_notification_title_finishing">Finishing uploading %1$s</string>
<string name="upload_failed_notification_title">Uploading %1$s failed</string>
<string name="upload_failed_notification_subtitle">Tap to retry</string>
</resources> </resources>

View file

@ -44,6 +44,7 @@ public class UploadService extends IntentService {
// Seriously, Android? // Seriously, Android?
public static final int NOTIFICATION_DOWNLOAD_IN_PROGRESS = 1; public static final int NOTIFICATION_DOWNLOAD_IN_PROGRESS = 1;
public static final int NOTIFICATION_DOWNLOAD_COMPLETE = 2; public static final int NOTIFICATION_DOWNLOAD_COMPLETE = 2;
public static final int NOTIFICATION_UPLOAD_FAILED = 3;
private class NotificationUpdateProgressListener implements ProgressListener { private class NotificationUpdateProgressListener implements ProgressListener {
@ -156,7 +157,7 @@ public class UploadService extends IntentService {
} else { } else {
Log.d("Commons", "Unable to revalidate :("); Log.d("Commons", "Unable to revalidate :(");
// TODO: Put up a new notification, ask them to re-login // TODO: Put up a new notification, ask them to re-login
notificationManager.cancel(notificationTag, NOTIFICATION_DOWNLOAD_IN_PROGRESS); stopForeground(true);
Toast failureToast = Toast.makeText(this, R.string.authentication_failed, Toast.LENGTH_LONG); Toast failureToast = Toast.makeText(this, R.string.authentication_failed, Toast.LENGTH_LONG);
failureToast.show(); failureToast.show();
return; return;
@ -165,9 +166,18 @@ public class UploadService extends IntentService {
Media media = new Media(mediaUri, filename, description, editSummary, app.getCurrentAccount().name, dateCreated); Media media = new Media(mediaUri, filename, description, editSummary, app.getCurrentAccount().name, dateCreated);
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();
Log.d("Commons", "I have a network fuckup"); Log.d("Commons", "I have a network fuckup");
throw new RuntimeException(e); stopForeground(true);
Notification failureNotification = new NotificationCompat.Builder(this).setAutoCancel(true)
.setSmallIcon(R.drawable.ic_launcher)
.setAutoCancel(true)
.setContentIntent(PendingIntent.getService(getApplicationContext(), 0, intent, 0))
.setTicker(String.format(getString(R.string.upload_failed_notification_title), filename))
.setContentTitle(String.format(getString(R.string.upload_failed_notification_title), filename))
.setContentText(getString(R.string.upload_failed_notification_subtitle))
.getNotification();
notificationManager.notify(NOTIFICATION_UPLOAD_FAILED, failureNotification);
return;
} }
Log.d("Commons", "Response is" + CommonsApplication.getStringFromDOM(result.getDocument())); Log.d("Commons", "Response is" + CommonsApplication.getStringFromDOM(result.getDocument()));