From 05d96ca48ec346ed5999f56c3fd22bcc65f8846c Mon Sep 17 00:00:00 2001 From: Phillip Hsu Date: Wed, 1 Mar 2017 13:30:20 -0800 Subject: [PATCH] Adjust if-block for clarity. --- .../background/UpcomingAlarmReceiver.java | 68 +++++++++---------- 1 file changed, 33 insertions(+), 35 deletions(-) diff --git a/app/src/main/java/com/philliphsu/clock2/alarms/background/UpcomingAlarmReceiver.java b/app/src/main/java/com/philliphsu/clock2/alarms/background/UpcomingAlarmReceiver.java index 7170a77..5c7b75f 100644 --- a/app/src/main/java/com/philliphsu/clock2/alarms/background/UpcomingAlarmReceiver.java +++ b/app/src/main/java/com/philliphsu/clock2/alarms/background/UpcomingAlarmReceiver.java @@ -59,45 +59,43 @@ public class UpcomingAlarmReceiver extends BroadcastReceiver { if (ACTION_CANCEL_NOTIFICATION.equals(intent.getAction())) { nm.cancel(TAG, (int) id); + } else if (ACTION_DISMISS_NOW.equals(intent.getAction())) { + new AlarmController(context, null).cancelAlarm(alarm, false, true); } else { - if (ACTION_DISMISS_NOW.equals(intent.getAction())) { - new AlarmController(context, null).cancelAlarm(alarm, false, true); + // Prepare notification + // http://stackoverflow.com/a/15803726/5055032 + // Notifications aren't updated on the UI thread, so we could have + // done this in the background. However, no lengthy operations are + // done here, so doing so is a premature optimization. + String title; + String text; + if (ACTION_SHOW_SNOOZING.equals(intent.getAction())) { + if (!alarm.isSnoozed()) + throw new IllegalStateException("Can't show snoozing notif. if alarm not snoozed!"); + title = alarm.label().isEmpty() ? context.getString(R.string.alarm) : alarm.label(); + text = context.getString(R.string.title_snoozing_until, + formatTime(context, alarm.snoozingUntil())); } else { - // Prepare notification - // http://stackoverflow.com/a/15803726/5055032 - // Notifications aren't updated on the UI thread, so we could have - // done this in the background. However, no lengthy operations are - // done here, so doing so is a premature optimization. - String title; - String text; - if (ACTION_SHOW_SNOOZING.equals(intent.getAction())) { - if (!alarm.isSnoozed()) - throw new IllegalStateException("Can't show snoozing notif. if alarm not snoozed!"); - title = alarm.label().isEmpty() ? context.getString(R.string.alarm) : alarm.label(); - text = context.getString(R.string.title_snoozing_until, - formatTime(context, alarm.snoozingUntil())); - } else { - // No intent action required for default behavior - title = context.getString(R.string.upcoming_alarm); - text = formatTime(context, alarm.ringsAt()); - if (!alarm.label().isEmpty()) { - text = alarm.label() + ", " + text; - } + // No intent action required for default behavior + title = context.getString(R.string.upcoming_alarm); + text = formatTime(context, alarm.ringsAt()); + if (!alarm.label().isEmpty()) { + text = alarm.label() + ", " + text; } - - Intent dismissIntent = new Intent(context, UpcomingAlarmReceiver.class) - .setAction(ACTION_DISMISS_NOW) - .putExtra(EXTRA_ALARM, ParcelableUtil.marshall(alarm)); - PendingIntent piDismiss = PendingIntent.getBroadcast(context, (int) id, dismissIntent, FLAG_ONE_SHOT); - Notification note = new NotificationCompat.Builder(context) - .setSmallIcon(R.drawable.ic_alarm_24dp) - .setContentTitle(title) - .setContentText(text) - .setContentIntent(ContentIntentUtils.create(context, MainActivity.PAGE_ALARMS, id)) - .addAction(R.drawable.ic_dismiss_alarm_24dp, context.getString(R.string.dismiss_now), piDismiss) - .build(); - nm.notify(TAG, (int) id, note); } + + Intent dismissIntent = new Intent(context, UpcomingAlarmReceiver.class) + .setAction(ACTION_DISMISS_NOW) + .putExtra(EXTRA_ALARM, ParcelableUtil.marshall(alarm)); + PendingIntent piDismiss = PendingIntent.getBroadcast(context, (int) id, dismissIntent, FLAG_ONE_SHOT); + Notification note = new NotificationCompat.Builder(context) + .setSmallIcon(R.drawable.ic_alarm_24dp) + .setContentTitle(title) + .setContentText(text) + .setContentIntent(ContentIntentUtils.create(context, MainActivity.PAGE_ALARMS, id)) + .addAction(R.drawable.ic_dismiss_alarm_24dp, context.getString(R.string.dismiss_now), piDismiss) + .build(); + nm.notify(TAG, (int) id, note); } } }