Created
October 23, 2013 10:46
-
-
Save blundell/7116438 to your computer and use it in GitHub Desktop.
Otto event bus for Notifications.
Really easy to show a notification from an unrelated piece of code.
Nice and easy to test.
Good use of the bus!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ParseNotificationWatcher { | |
private final ParseNotificationManager notificationManager; | |
private final NotificationBuilder notificationBuilder; | |
public ParseNotificationWatcher(ParseNotificationManager notificationManager, NotificationBuilder notificationBuilder) { | |
this.notificationManager = notificationManager; | |
this.notificationBuilder = notificationBuilder; | |
} | |
@Subscribe | |
public void on(RecipeProcessingHasStarted event) { | |
notificationManager.showNotification(notificationBuilder.build()); | |
} | |
@Subscribe | |
public void on(RecipeProcessingHasFinished event) { | |
notificationManager.hideNotification(); | |
} | |
} | |
public class ParseNotificationManager { | |
private static final int NOTIFICATION_ID = 123; | |
private final NotificationManager notificationManager; | |
public ParseNotificationManager(NotificationManager notificationManager) { | |
this.notificationManager = notificationManager; | |
} | |
public void showNotification(Notification notification) { | |
notificationManager.notify(NOTIFICATION_ID, notification); | |
} | |
public void hideNotification() { | |
notificationManager.cancel(NOTIFICATION_ID); | |
} | |
} | |
public class ParseNotificationBuilder implements NotificationBuilder { | |
private static final int SET = 0; | |
private static final int PROGRESS = 0; | |
private static final boolean INDETERMINATE = true; | |
private final Context context; | |
public ParseNotificationBuilder(Context context) { | |
this.context = context; | |
} | |
@Override | |
public Notification build() { | |
NotificationCompat.BigPictureStyle style = new NotificationCompat.BigPictureStyle(); | |
style.bigPicture(BitmapFactory.decodeResource(context.getResources(), R.drawable.notification_recipes_big_image)); | |
return new NotificationCompat.Builder(context) | |
.setSmallIcon(R.drawable.ic_stat_notify_recipes) | |
.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_stat_notify_recipes)) | |
.setContentTitle(context.getString(R.string.notification_recipe_parse_title)) | |
.setContentText(context.getString(R.string.notification_recipe_parse_message)) | |
.setStyle(style) | |
.setProgress(SET, PROGRESS, INDETERMINATE) | |
.setContentIntent(createShoppingActivityIntent()) | |
.build(); | |
} | |
private PendingIntent createShoppingActivityIntent() { | |
return PendingIntent.getActivity(context, 0, new Intent(context, ShoppingActivity.class), 0); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment