@NonNull private NotificationCompat.Builder buildBaseNotification(boolean isPhone) { final NotificationCompat.Action action = getNotificationAction(); final String title = titleForActivityType(context); final String message = messageForActivityType(context, isPhone); final NotificationCompat.Builder builder = new NotificationCompat.Builder(context) .setSmallIcon(notificationIcon) .setDefaults(pomodoroMaster.isOngoing() ? 0 : Notification.DEFAULT_ALL) .setOnlyAlertOnce(true) .setCategory(NotificationCompat.CATEGORY_ALARM) .setPriority(Notification.PRIORITY_MAX) .setOngoing(pomodoroMaster.isOngoing()) .setVisibility(NotificationCompat.VISIBILITY_PUBLIC) .setLocalOnly(true) .setColor(Utils.getPrimaryColor(context, pomodoroMaster)) .setContentTitle(title) .setContentText(message) .addAction(action); if (!pomodoroMaster.isOngoing()) { builder.setDeleteIntent( PendingIntent.getBroadcast( context, ID_DISMISS, new Intent(BaseNotificationService.ACTION_DISMISS), PendingIntent.FLAG_UPDATE_CURRENT)); } return builder; }
private NotificationCompat.Action getNotificationAction() { final NotificationCompat.Action action; if (pomodoroMaster.isOngoing()) { action = buildStopAction(context); } else { ActivityType activityType = pomodoroMaster.getActivityType(); if (activityType == ActivityType.NONE) { activityType = ActivityType.POMODORO; } action = buildStartAction(context, activityType); } return action; }
public String titleForActivityType(@NonNull Context context) { if (pomodoroMaster.isOngoing()) { if (pomodoroMaster.getNextPomodoro() == null) { return null; } final String minutesLeft = Utils.convertDiffToPrettyMinutesLeft( context, pomodoroMaster.getNextPomodoro().minus(DateTime.now().getMillis())); return minutesLeft + " | " + Utils.getActivityTitle(context, pomodoroMaster, /* shorten */ true); } else { if (pomodoroMaster.getActivityType() == ActivityType.NONE) { return context.getString(R.string.title_none); } else { return context.getString(R.string.title_finished); } } }
public String messageForActivityType(@NonNull Context context, boolean isPhone) { if (pomodoroMaster.isOngoing()) { if (isPhone) { return Utils.getActivityTitle(context, pomodoroMaster, /* shorten */ false); } else { return null; } } else { return Utils.getActivityFinishMessage(context, pomodoroMaster); } }
@NonNull public Notification buildNotificationPhone(@NonNull PendingIntent contentIntent) { final DateTime nextPomodoro = pomodoroMaster.getNextPomodoro(); NotificationCompat.Builder builder = buildBaseNotification(/* isPhone */ true) .setWhen(nextPomodoro != null ? nextPomodoro.getMillis() : System.currentTimeMillis()) .setContentIntent(contentIntent); return builder.build(); }
@NonNull public Notification buildNotificationWear(@NonNull Intent displayIntent) { final NotificationCompat.WearableExtender extender = new NotificationCompat.WearableExtender() .setBackground(getBackground()) .setHintHideIcon(true); if (pomodoroMaster.isOngoing()) { final PendingIntent displayPendingIntent = PendingIntent.getActivity( context, ID_ACTIVITY, displayIntent, PendingIntent.FLAG_UPDATE_CURRENT); extender.setDisplayIntent(displayPendingIntent); } else { extender.setContentIcon(R.drawable.ic_action_start_grey); } NotificationCompat.Builder builder = buildBaseNotification(/* isPhone */ false) .addAction(buildResetAction(context)) .extend(extender); return builder.build(); }