Try again notification

added a notification for users to try to send the report again

closes #32

Signed-off-by: Ricky Barrette <rickbarrette@gmail.com>
This commit is contained in:
2012-05-05 12:40:14 -04:00
parent 95a7a4869f
commit 641c5b7b2d
2 changed files with 93 additions and 36 deletions

View File

@@ -2,12 +2,14 @@
<resources>
<string name="crash">Application Fault</string>
<string name="sorry">I\'m sorry, but we found a problem. Please click here.</string>
<string name="sorry">We found a problem. Please click here.</string>
<string name="sending">Sending…</string>
<string name="send">Send</string>
<string name="description">Optional: Describe what happened</string>
<string name="version">Version</string>
<string name="no_reports">No Reports</string>
<string name="sending_report">Sending Exception Report</string>
<string name="reporting_error">Problem sending the report</string>
<string name="reporting_error_msg">Please click here to try again</string>
</resources>

View File

@@ -33,6 +33,51 @@ public class ReportPostingService extends Service {
private int mStartId;
private Report mReport;
private boolean isStarted;
private Intent mIntent;
private boolean hasErrored = false;
/**
* Fires of a notification based upon api level
* @param title
* @param contentText
* @param ticker
* @param icon
* @param intent
* @param isOngoing
* @author ricky barrette
*/
@SuppressWarnings("deprecation")
private void fireNotification(String title, String contentText, String ticker, int icon, Intent intent, boolean isOngoing) {
PendingIntent pendingIntent = null;
if(intent != null)
pendingIntent = PendingIntent.getService(this.getApplicationContext(), 0, intent, 0);
/*
* Use the appropriate notificafation methods
*/
if(Integer.valueOf(android.os.Build.VERSION.SDK_INT) > 11){
Builder builder = new Notification.Builder(this.getApplicationContext())
.setContentTitle(title)
.setContentText(contentText)
.setTicker(ticker)
.setSmallIcon(icon)
.setWhen(System.currentTimeMillis());
if(isOngoing)
builder.setOngoing(true);
else
builder.setAutoCancel(true);
if (intent != null)
builder.setContentIntent(pendingIntent);
mNotificationManager.notify(NOTIFICATION_ID, builder.getNotification());
} else {
Notification notification = new Notification(icon, title , System.currentTimeMillis());
if(isOngoing)
notification.flags |= Notification.FLAG_ONGOING_EVENT;
else
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.setLatestEventInfo(this.getApplicationContext(), title, contentText, pendingIntent);
mNotificationManager.notify(NOTIFICATION_ID, notification);
}
}
/**
* Extracts the report object from the intent
@@ -42,6 +87,34 @@ public class ReportPostingService extends Service {
private void getReport(Intent intent) {
mReport = (Report) intent.getParcelableExtra("report");
}
/**
* notifiys the user that we are sending a report
* @author ricky barrette
*/
private void notifyError() {
fireNotification(getString(R.string.reporting_error),
getString(R.string.reporting_error_msg),
getString(R.string.reporting_error_msg),
android.R.drawable.stat_notify_error,
new Intent(this.getApplicationContext(), ReportPostingService.class).putExtras(mIntent),
false);
}
/**
* notifiys the user that we are sending a report
* @author ricky barrette
*/
private void notifySending() {
fireNotification(getString(R.string.sending),
getString(R.string.sending_report),
getString(R.string.sending),
android.R.drawable.stat_sys_upload,
null,
true);
}
/**
* (non-Javadoc)
* @see android.app.Service#onBind(android.content.Intent)
@@ -59,33 +132,14 @@ public class ReportPostingService extends Service {
* (non-Javadoc)
* @see android.app.Service#onCreate()
*/
@SuppressWarnings("deprecation")
@Override
public void onCreate() {
Context context = this.getApplicationContext();
mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
/*
* Use the appropriate notificafation methods
*/
if(Integer.valueOf(android.os.Build.VERSION.SDK_INT) > 11){
Builder builder = new Notification.Builder(context)
.setContentTitle(getText(R.string.sending))
.setContentText(getText(R.string.sending_report))
.setTicker(getText(R.string.sending))
.setOngoing(true)
.setSmallIcon(android.R.drawable.stat_sys_upload)
.setWhen(System.currentTimeMillis());
mNotificationManager.notify(NOTIFICATION_ID, builder.getNotification());
} else {
Notification notification = new Notification(android.R.drawable.stat_sys_upload, getText(R.string.sending) , System.currentTimeMillis());
notification.flags |= Notification.FLAG_ONGOING_EVENT;
notification.setLatestEventInfo(context, getText(R.string.sending_report), getText(R.string.sending), PendingIntent.getActivity(context, 0, null, 0));
mNotificationManager.notify(NOTIFICATION_ID, notification);
}
notifySending();
super.onCreate();
}
/**
* Called when the service is being destroyed
* Here we want to dismiss the notifications
@@ -95,6 +149,8 @@ public class ReportPostingService extends Service {
@Override
public void onDestroy() {
mNotificationManager.cancel(NOTIFICATION_ID);
if (hasErrored)
notifyError();
super.onDestroy();
}
@@ -108,6 +164,7 @@ public class ReportPostingService extends Service {
mStartId = startId;
getReport(intent);
postReport();
mIntent = intent;
super.onStart(intent, startId);
}
@@ -120,6 +177,7 @@ public class ReportPostingService extends Service {
mStartId = startId;
getReport(intent);
postReport();
mIntent = intent;
return super.onStartCommand(intent, Service.START_STICKY, startId);
}
@@ -130,20 +188,17 @@ public class ReportPostingService extends Service {
private void postReport(){
if(!isStarted){
isStarted = true;
new Thread(new Runnable() {
@Override
public void run(){
try {
Log.d(TAG, mReport.file());
} catch (ClientProtocolException e) {
e.printStackTrace();
hasErrored = true;
} catch (IOException e) {
e.printStackTrace();
hasErrored = true;
} finally {
ReportPostingService.this.stopSelf(mStartId);
}
}
}).start();
}
}
}