diff --git a/ExceptionHandlerLib/res/values/strings.xml b/ExceptionHandlerLib/res/values/strings.xml index 40d0133..4e95149 100644 --- a/ExceptionHandlerLib/res/values/strings.xml +++ b/ExceptionHandlerLib/res/values/strings.xml @@ -2,12 +2,14 @@ Application Fault - I\'m sorry, but we found a problem. Please click here. + We found a problem. Please click here. Sending… Send Optional: Describe what happened Version No Reports Sending Exception Report + Problem sending the report + Please click here to try again \ No newline at end of file diff --git a/ExceptionHandlerLib/src/com/TwentyCodes/android/exception/ReportPostingService.java b/ExceptionHandlerLib/src/com/TwentyCodes/android/exception/ReportPostingService.java index 44bfb5d..890dfbb 100644 --- a/ExceptionHandlerLib/src/com/TwentyCodes/android/exception/ReportPostingService.java +++ b/ExceptionHandlerLib/src/com/TwentyCodes/android/exception/ReportPostingService.java @@ -33,7 +33,52 @@ 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 * @param 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) @@ -51,7 +124,7 @@ public class ReportPostingService extends Service { // Unused return null; } - + /** * Called when the service is being created * Here we want to display a notifcation, @@ -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(); - } catch (IOException e) { - e.printStackTrace(); - } finally { - ReportPostingService.this.stopSelf(mStartId); - } - } - }).start(); + 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); + } } } }