diff options
-rw-r--r-- | src/com/android/deskclock/Alarms.java | 40 | ||||
-rw-r--r-- | src/com/android/deskclock/SetAlarm.java | 28 |
2 files changed, 49 insertions, 19 deletions
diff --git a/src/com/android/deskclock/Alarms.java b/src/com/android/deskclock/Alarms.java index b6a22f4f7..ebb337b48 100644 --- a/src/com/android/deskclock/Alarms.java +++ b/src/com/android/deskclock/Alarms.java @@ -17,6 +17,7 @@ package com.android.deskclock; import android.app.AlarmManager; +import android.app.NotificationManager; import android.app.PendingIntent; import android.content.ContentResolver; import android.content.ContentValues; @@ -157,8 +158,9 @@ public class Alarms { * @param vibrate corresponds to the VIBRATE column * @param message corresponds to the MESSAGE column * @param alert corresponds to the ALERT column + * @return Time when the alarm will fire. */ - public static void setAlarm( + public static long setAlarm( Context context, int id, boolean enabled, int hour, int minutes, Alarm.DaysOfWeek daysOfWeek, boolean vibrate, String message, String alert) { @@ -187,7 +189,23 @@ public class Alarms { resolver.update(ContentUris.withAppendedId(Alarm.Columns.CONTENT_URI, id), values, null, null); + long timeInMillis = + calculateAlarm(hour, minutes, daysOfWeek).getTimeInMillis(); + + if (enabled) { + // If this alarm fires before the next snooze, clear the snooze to + // enable this alarm. + SharedPreferences prefs = context.getSharedPreferences( + AlarmClock.PREFERENCES, 0); + long snoozeTime = prefs.getLong(PREF_SNOOZE_TIME, 0); + if (timeInMillis < snoozeTime) { + clearSnoozePreference(context, prefs); + } + } + setNextAlert(context); + + return timeInMillis; } /** @@ -367,10 +385,10 @@ public class Alarms { final long time) { SharedPreferences prefs = context.getSharedPreferences( AlarmClock.PREFERENCES, 0); - SharedPreferences.Editor ed = prefs.edit(); if (id == -1) { - clearSnoozePreference(ed); + clearSnoozePreference(context, prefs); } else { + SharedPreferences.Editor ed = prefs.edit(); ed.putInt(PREF_SNOOZE_ID, id); ed.putLong(PREF_SNOOZE_TIME, time); ed.commit(); @@ -391,13 +409,23 @@ public class Alarms { return; } else if (snoozeId == id) { // This is the same id so clear the shared prefs. - clearSnoozePreference(prefs.edit()); + clearSnoozePreference(context, prefs); } } // Helper to remove the snooze preference. Do not use clear because that - // will erase the clock preferences. - private static void clearSnoozePreference(final SharedPreferences.Editor ed) { + // will erase the clock preferences. Also clear the snooze notification in + // the window shade. + private static void clearSnoozePreference(final Context context, + final SharedPreferences prefs) { + final int alarmId = prefs.getInt(PREF_SNOOZE_ID, -1); + if (alarmId != -1) { + NotificationManager nm = (NotificationManager) + context.getSystemService(Context.NOTIFICATION_SERVICE); + nm.cancel(alarmId); + } + + final SharedPreferences.Editor ed = prefs.edit(); ed.remove(PREF_SNOOZE_ID); ed.remove(PREF_SNOOZE_TIME); ed.commit(); diff --git a/src/com/android/deskclock/SetAlarm.java b/src/com/android/deskclock/SetAlarm.java index 1acc1d790..7d84bb493 100644 --- a/src/com/android/deskclock/SetAlarm.java +++ b/src/com/android/deskclock/SetAlarm.java @@ -187,19 +187,19 @@ public class SetAlarm extends PreferenceActivity private void saveAlarm() { final String alert = mAlarmPref.getAlertString(); - Alarms.setAlarm(this, mId, mEnabled, mHour, mMinutes, + long time = Alarms.setAlarm(this, mId, mEnabled, mHour, mMinutes, mRepeatPref.getDaysOfWeek(), mVibratePref.isChecked(), mLabel.getText(), alert); if (mEnabled) { - popAlarmSetToast(this, mHour, mMinutes, - mRepeatPref.getDaysOfWeek()); + popAlarmSetToast(this, time); } } /** * Write alarm out to persistent store and pops toast if alarm - * enabled + * enabled. + * Used only in test code. */ private static void saveAlarm( Context context, int id, boolean enabled, int hour, int minute, @@ -209,11 +209,11 @@ public class SetAlarm extends PreferenceActivity + " " + hour + " " + minute + " vibe " + vibrate); // Fix alert string first - Alarms.setAlarm(context, id, enabled, hour, minute, daysOfWeek, vibrate, - label, alert); + long time = Alarms.setAlarm(context, id, enabled, hour, minute, + daysOfWeek, vibrate, label, alert); if (enabled && popToast) { - popAlarmSetToast(context, hour, minute, daysOfWeek); + popAlarmSetToast(context, time); } } @@ -223,8 +223,13 @@ public class SetAlarm extends PreferenceActivity */ static void popAlarmSetToast(Context context, int hour, int minute, Alarm.DaysOfWeek daysOfWeek) { + popAlarmSetToast(context, + Alarms.calculateAlarm(hour, minute, daysOfWeek) + .getTimeInMillis()); + } - String toastText = formatToast(context, hour, minute, daysOfWeek); + private static void popAlarmSetToast(Context context, long timeInMillis) { + String toastText = formatToast(context, timeInMillis); Toast toast = Toast.makeText(context, toastText, Toast.LENGTH_LONG); ToastMaster.setToast(toast); toast.show(); @@ -234,11 +239,8 @@ public class SetAlarm extends PreferenceActivity * format "Alarm set for 2 days 7 hours and 53 minutes from * now" */ - static String formatToast(Context context, int hour, int minute, - Alarm.DaysOfWeek daysOfWeek) { - long alarm = Alarms.calculateAlarm(hour, minute, - daysOfWeek).getTimeInMillis(); - long delta = alarm - System.currentTimeMillis();; + static String formatToast(Context context, long timeInMillis) { + long delta = timeInMillis - System.currentTimeMillis(); long hours = delta / (1000 * 60 * 60); long minutes = delta / (1000 * 60) % 60; long days = hours / 24; |