From 507d7901bf9531ed7d13fa1770ee443055119f5b Mon Sep 17 00:00:00 2001 From: Ricky Barrette Date: Mon, 16 Jul 2012 13:44:16 -0400 Subject: [PATCH] Updated notifications to use the compat notification builder closes #124 --- ExceptionHandlerLib/project.properties | 2 +- .../res/{drawables => drawable}/.NOTEMPTY | 0 .../android/exception/ExceptionHandler.java | 16 +++++--- .../exception/ReportPostingService.java | 39 ++++++------------- .../VersionInformationPreference.java | 1 - 5 files changed, 23 insertions(+), 35 deletions(-) rename ExceptionHandlerLib/res/{drawables => drawable}/.NOTEMPTY (100%) diff --git a/ExceptionHandlerLib/project.properties b/ExceptionHandlerLib/project.properties index f28bc83..616f300 100644 --- a/ExceptionHandlerLib/project.properties +++ b/ExceptionHandlerLib/project.properties @@ -9,4 +9,4 @@ android.library=true # Project target. -target=android-15 +target=android-16 diff --git a/ExceptionHandlerLib/res/drawables/.NOTEMPTY b/ExceptionHandlerLib/res/drawable/.NOTEMPTY similarity index 100% rename from ExceptionHandlerLib/res/drawables/.NOTEMPTY rename to ExceptionHandlerLib/res/drawable/.NOTEMPTY diff --git a/ExceptionHandlerLib/src/com/TwentyCodes/android/exception/ExceptionHandler.java b/ExceptionHandlerLib/src/com/TwentyCodes/android/exception/ExceptionHandler.java index 206521c..65786ed 100644 --- a/ExceptionHandlerLib/src/com/TwentyCodes/android/exception/ExceptionHandler.java +++ b/ExceptionHandlerLib/src/com/TwentyCodes/android/exception/ExceptionHandler.java @@ -14,7 +14,6 @@ import java.util.Date; import java.util.Properties; import android.app.Activity; -import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; import android.app.Service; @@ -27,6 +26,7 @@ import android.content.pm.PackageManager.NameNotFoundException; import android.content.res.AssetManager; import android.content.res.Resources; import android.os.Build; +import android.support.v4.app.NotificationCompat; import android.util.Log; /** @@ -120,16 +120,20 @@ public class ExceptionHandler implements UncaughtExceptionHandler, Runnable { * displays an notification in the status bar, letting the user know that there was an issue * @param generatedReportIntent */ - @SuppressWarnings("deprecation") private void displayNotification(final Intent generatedReportIntent) { Log.i(TAG, "displayNotification"); final Context context = mContext.getApplicationContext(); final NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); - final Notification notifyDetails = new Notification(android.R.drawable.stat_notify_error, context.getString(R.string.sorry), System.currentTimeMillis()); final PendingIntent intent = PendingIntent.getActivity(context, 0, generatedReportIntent, android.content.Intent.FLAG_ACTIVITY_NEW_TASK); - notifyDetails.setLatestEventInfo(context, context.getString(R.string.crash), context.getString(R.string.sorry), intent); - notifyDetails.flags |= Notification.FLAG_AUTO_CANCEL; - notificationManager.notify(SIMPLE_NOTFICATION_ID, notifyDetails); + final NotificationCompat.Builder builder = new NotificationCompat.Builder(context) + .setContentTitle(context.getString(R.string.crash)) + .setContentText(context.getString(R.string.sorry)) + .setTicker(context.getString(R.string.crash)) + .setSmallIcon(android.R.drawable.stat_notify_error) + .setWhen(System.currentTimeMillis()) + .setAutoCancel(true) + .setContentIntent(intent); + notificationManager.notify(SIMPLE_NOTFICATION_ID, builder.getNotification()); } /** diff --git a/ExceptionHandlerLib/src/com/TwentyCodes/android/exception/ReportPostingService.java b/ExceptionHandlerLib/src/com/TwentyCodes/android/exception/ReportPostingService.java index 2f9ebfe..3966356 100644 --- a/ExceptionHandlerLib/src/com/TwentyCodes/android/exception/ReportPostingService.java +++ b/ExceptionHandlerLib/src/com/TwentyCodes/android/exception/ReportPostingService.java @@ -10,16 +10,14 @@ import java.io.IOException; import org.apache.http.client.ClientProtocolException; -import android.annotation.SuppressLint; import android.annotation.TargetApi; -import android.app.Notification; -import android.app.Notification.Builder; import android.app.NotificationManager; import android.app.PendingIntent; import android.app.Service; import android.content.Context; import android.content.Intent; import android.os.IBinder; +import android.support.v4.app.NotificationCompat; import android.util.Log; /** @@ -48,38 +46,26 @@ public class ReportPostingService extends Service { * @param isOngoing * @author ricky barrette */ - @SuppressLint("NewApi") - @SuppressWarnings("deprecation") private void fireNotification(final String title, final String contentText, final String ticker, final int icon, final Intent intent, final boolean isOngoing) { PendingIntent pendingIntent = null; if(intent != null) pendingIntent = PendingIntent.getService(getApplicationContext(), 0, intent, 0); - /* - * Use the appropriate notificafation methods - */ - if(Integer.valueOf(android.os.Build.VERSION.SDK_INT) > 11){ - final Builder builder = new Notification.Builder(getApplicationContext()) + + final NotificationCompat.Builder builder = new NotificationCompat.Builder(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 { - final 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(getApplicationContext(), title, contentText, pendingIntent); - mNotificationManager.notify(NOTIFICATION_ID, notification); - } + + if(isOngoing) + builder.setOngoing(true); + else + builder.setAutoCancel(true); + + if (intent != null) + builder.setContentIntent(pendingIntent); + mNotificationManager.notify(NOTIFICATION_ID, builder.getNotification()); } /** @@ -115,7 +101,6 @@ public class ReportPostingService extends Service { android.R.drawable.stat_sys_upload, null, true); - } /** diff --git a/ExceptionHandlerLib/src/com/TwentyCodes/android/exception/VersionInformationPreference.java b/ExceptionHandlerLib/src/com/TwentyCodes/android/exception/VersionInformationPreference.java index 5f6197e..14bfb1e 100644 --- a/ExceptionHandlerLib/src/com/TwentyCodes/android/exception/VersionInformationPreference.java +++ b/ExceptionHandlerLib/src/com/TwentyCodes/android/exception/VersionInformationPreference.java @@ -27,7 +27,6 @@ public class VersionInformationPreference extends Preference { private final Context mContext; - /** * creates a preference that is nothing but a text view * @param context